Subversion Repositories Kolibri OS

Rev

Rev 425 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 425 Rev 431
1
$Revision: 425 $
1
$Revision: 431 $
2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3
;;
-
 
-
 
3
;;                                                              ;;
-
 
4
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
-
 
5
;; Distributed under terms of the GNU General Public License    ;;
-
 
6
;;                                                              ;;
4
;;  ICMP.INC
7
;;  ICMP.INC                                                    ;;
5
;;
-
 
-
 
8
;;                                                              ;;
6
;;  Internet Control Message Protocol ( RFC 792 )
9
;;  Internet Control Message Protocol ( RFC 792 )               ;;
7
;;
-
 
-
 
10
;;                                                              ;;
8
;;  Last revision: 11.11.2006
11
;;  Last revision: 11.11.2006                                   ;;
9
;;
-
 
-
 
12
;;                                                              ;;
10
;;  This file contains the following:
13
;;  This file contains the following:                           ;;
11
;;   icmp_rx - processes ICMP-packets received by the IP layer
14
;;   icmp_rx - processes ICMP-packets received by the IP layer  ;;
12
;;
-
 
-
 
15
;;                                                              ;;
13
;;  Changes history:
16
;;  Changes history:                                            ;;
14
;;   22.09.2003 - [Mike Hibbett] : mikeh@oceanfree.net
17
;;   22.09.2003 - [Mike Hibbett] : mikeh@oceanfree.net          ;;
15
;;   11.11.2006 - [Johnny_B] and [smb]
18
;;   11.11.2006 - [Johnny_B] and [smb]                          ;;
16
;;
-
 
-
 
19
;;                                                              ;;
17
;;   Current status:
20
;;   Current status:                                            ;;
18
;;	This implemetation of ICMP proto supports message of ECHO type.
21
;;	This implemetation of ICMP proto supports message of ECHO type.
19
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20
 
23
 
21
 
24
 
22
struc ICMP_PACKET
25
struc ICMP_PACKET
23
{  .Type            db   ?  ;+00
26
{  .Type            db   ?  ;+00
24
   .Code            db   ?  ;+01
27
   .Code            db   ?  ;+01
25
   .Checksum        dw   ?  ;+02
28
   .Checksum        dw   ?  ;+02
26
   .Identifier      dw   ?  ;+04
29
   .Identifier      dw   ?  ;+04
27
   .SequenceNumber  dw   ?  ;+06
30
   .SequenceNumber  dw   ?  ;+06
28
   .Data            db   ?  ;+08
31
   .Data            db   ?  ;+08
29
}
32
}
30
 
33
 
31
virtual at 0
34
virtual at 0
32
  ICMP_PACKET ICMP_PACKET
35
  ICMP_PACKET ICMP_PACKET
33
end virtual
36
end virtual
34
 
37
 
35
 
38
 
36
;   Example:
39
;   Example:
37
;	ECHO message format
40
;	ECHO message format
38
;
41
;
39
;
42
;
40
;           0               1               2               3
43
;           0               1               2               3
41
;    0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
44
;    0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
42
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43
;   |     Type      |     Code      |          Checksum             |
46
;   |     Type      |     Code      |          Checksum             |
44
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45
;   |           Identifier          |        Sequence Number        |
48
;   |           Identifier          |        Sequence Number        |
46
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47
;   |     Data ...
50
;   |     Data ...
48
;   +-+-+-+-+-
51
;   +-+-+-+-+-
49
;
52
;
50
 
53
 
51
;
54
;
52
; ICMP types & codes, RFC 792 and FreeBSD's ICMP sources
55
; ICMP types & codes, RFC 792 and FreeBSD's ICMP sources
53
;
56
;
54
 
57
 
55
ICMP_ECHOREPLY		equ	0		; echo reply message
58
ICMP_ECHOREPLY		equ	0		; echo reply message
56
 
59
 
57
ICMP_UNREACH		equ	3
60
ICMP_UNREACH		equ	3
58
	ICMP_UNREACH_NET	equ	0		; bad net 
61
	ICMP_UNREACH_NET	equ	0		; bad net
59
	ICMP_UNREACH_HOST	equ	1		; bad host 
62
	ICMP_UNREACH_HOST	equ	1		; bad host
60
	ICMP_UNREACH_PROTOCOL	equ	2		; bad protocol 
63
	ICMP_UNREACH_PROTOCOL	equ	2		; bad protocol
61
	ICMP_UNREACH_PORT	equ	3		; bad port
64
	ICMP_UNREACH_PORT	equ	3		; bad port
62
	ICMP_UNREACH_NEEDFRAG	equ	4		; IP_DF caused drop
65
	ICMP_UNREACH_NEEDFRAG	equ	4		; IP_DF caused drop
63
	ICMP_UNREACH_SRCFAIL	equ	5		; src route failed
66
	ICMP_UNREACH_SRCFAIL	equ	5		; src route failed
64
	ICMP_UNREACH_NET_UNKNOWN equ	6		; unknown net
67
	ICMP_UNREACH_NET_UNKNOWN equ	6		; unknown net
65
	ICMP_UNREACH_HOST_UNKNOWN equ	7		; unknown host
68
	ICMP_UNREACH_HOST_UNKNOWN equ	7		; unknown host
66
	ICMP_UNREACH_ISOLATED	equ	8		; src host isolated 
69
	ICMP_UNREACH_ISOLATED	equ	8		; src host isolated
67
	ICMP_UNREACH_NET_PROHIB	equ	9		; prohibited access 
70
	ICMP_UNREACH_NET_PROHIB	equ	9		; prohibited access
68
	ICMP_UNREACH_HOST_PROHIB equ	10		; ditto 
71
	ICMP_UNREACH_HOST_PROHIB equ	10		; ditto
69
	ICMP_UNREACH_TOSNET	equ	11		; bad tos for net 
72
	ICMP_UNREACH_TOSNET	equ	11		; bad tos for net
70
	ICMP_UNREACH_TOSHOST	equ	12		; bad tos for host 
73
	ICMP_UNREACH_TOSHOST	equ	12		; bad tos for host
71
	ICMP_UNREACH_FILTER_PROHIB equ	13		; admin prohib 
74
	ICMP_UNREACH_FILTER_PROHIB equ	13		; admin prohib
72
	ICMP_UNREACH_HOST_PRECEDENCE equ 14		; host prec vio. 
75
	ICMP_UNREACH_HOST_PRECEDENCE equ 14		; host prec vio.
73
	ICMP_UNREACH_PRECEDENCE_CUTOFF equ 15		; prec cutoff 
76
	ICMP_UNREACH_PRECEDENCE_CUTOFF equ 15		; prec cutoff
74
 
77
 
75
ICMP_SOURCEQUENCH	equ	4		; packet lost, slow down 
78
ICMP_SOURCEQUENCH	equ	4		; packet lost, slow down
76
 
79
 
77
ICMP_REDIRECT		equ	5		; shorter route, codes: 
80
ICMP_REDIRECT		equ	5		; shorter route, codes:
78
	ICMP_REDIRECT_NET	equ	0		; for network 
81
	ICMP_REDIRECT_NET	equ	0		; for network
79
	ICMP_REDIRECT_HOST	equ	1		; for host 
82
	ICMP_REDIRECT_HOST	equ	1		; for host
80
	ICMP_REDIRECT_TOSNET	equ	2		; for tos and net 
83
	ICMP_REDIRECT_TOSNET	equ	2		; for tos and net
81
	ICMP_REDIRECT_TOSHOST	equ	3		; for tos and host 
84
	ICMP_REDIRECT_TOSHOST	equ	3		; for tos and host
82
	
85
 
83
ICMP_ALTHOSTADDR	equ	6		; alternate host address 
86
ICMP_ALTHOSTADDR	equ	6		; alternate host address
84
ICMP_ECHO		equ	8		; echo service 
87
ICMP_ECHO		equ	8		; echo service
85
ICMP_ROUTERADVERT	equ	9		; router advertisement 
88
ICMP_ROUTERADVERT	equ	9		; router advertisement
86
	ICMP_ROUTERADVERT_NORMAL equ 0			; normal advertisement 
89
	ICMP_ROUTERADVERT_NORMAL equ 0			; normal advertisement
87
	ICMP_ROUTERADVERT_NOROUTE_COMMON equ 16		; selective routing 
90
	ICMP_ROUTERADVERT_NOROUTE_COMMON equ 16		; selective routing
88
 
91
 
89
ICMP_ROUTERSOLICIT	equ	10		; router solicitation 
92
ICMP_ROUTERSOLICIT	equ	10		; router solicitation
90
ICMP_TIMXCEED		equ	11		; time exceeded, code: 
93
ICMP_TIMXCEED		equ	11		; time exceeded, code:
91
    ICMP_TIMXCEED_INTRANS	equ	0		; ttl==0 in transit 
94
    ICMP_TIMXCEED_INTRANS	equ	0		; ttl==0 in transit
92
    ICMP_TIMXCEED_REASS	equ	1		; ttl==0 in reass 
95
    ICMP_TIMXCEED_REASS	equ	1		; ttl==0 in reass
93
 
96
 
94
ICMP_PARAMPROB            equ  12		; ip header bad 
97
ICMP_PARAMPROB            equ  12		; ip header bad
95
    ICMP_PARAMPROB_ERRATPTR   equ  0		; error at param ptr 
98
    ICMP_PARAMPROB_ERRATPTR   equ  0		; error at param ptr
96
    ICMP_PARAMPROB_OPTABSENT  equ  1		; req. opt. absent 
99
    ICMP_PARAMPROB_OPTABSENT  equ  1		; req. opt. absent
97
    ICMP_PARAMPROB_LENGTH     equ  2		; bad length 
100
    ICMP_PARAMPROB_LENGTH     equ  2		; bad length
98
	
101
 
99
ICMP_TSTAMP		equ	13		; timestamp request 
102
ICMP_TSTAMP		equ	13		; timestamp request
100
ICMP_TSTAMPREPLY	equ	14		; timestamp reply 
103
ICMP_TSTAMPREPLY	equ	14		; timestamp reply
101
ICMP_IREQ		equ	15		; information request 
104
ICMP_IREQ		equ	15		; information request
102
ICMP_IREQREPLY		equ	16		; information reply 
105
ICMP_IREQREPLY		equ	16		; information reply
103
ICMP_MASKREQ		equ	17		; address mask request 
106
ICMP_MASKREQ		equ	17		; address mask request
104
ICMP_MASKREPLY		equ	18		; address mask reply 
107
ICMP_MASKREPLY		equ	18		; address mask reply
105
ICMP_TRACEROUTE		equ	30		; traceroute 
108
ICMP_TRACEROUTE		equ	30		; traceroute
106
ICMP_DATACONVERR	equ	31		; data conversion error 
109
ICMP_DATACONVERR	equ	31		; data conversion error
107
ICMP_MOBILE_REDIRECT	equ	32		; mobile host redirect 
110
ICMP_MOBILE_REDIRECT	equ	32		; mobile host redirect
108
ICMP_IPV6_WHEREAREYOU	equ	33		; IPv6 where-are-you 
111
ICMP_IPV6_WHEREAREYOU	equ	33		; IPv6 where-are-you
109
 ICMP_IPV6_IAMHERE	equ	34		; IPv6 i-am-here 
112
 ICMP_IPV6_IAMHERE	equ	34		; IPv6 i-am-here
110
ICMP_MOBILE_REGREQUEST	equ	35		; mobile registration req 
113
ICMP_MOBILE_REGREQUEST	equ	35		; mobile registration req
111
ICMP_MOBILE_REGREPLY	equ	36		; mobile registreation reply 
114
ICMP_MOBILE_REGREPLY	equ	36		; mobile registreation reply
112
ICMP_SKIP		equ	39		; SKIP 
115
ICMP_SKIP		equ	39		; SKIP
113
 
116
 
114
ICMP_PHOTURIS		equ	40		; Photuris 
117
ICMP_PHOTURIS		equ	40		; Photuris
115
    ICMP_PHOTURIS_UNKNOWN_INDEX   equ  1		; unknown sec index 
118
    ICMP_PHOTURIS_UNKNOWN_INDEX   equ  1		; unknown sec index
116
    ICMP_PHOTURIS_AUTH_FAILED     equ  2		; auth failed 
119
    ICMP_PHOTURIS_AUTH_FAILED     equ  2		; auth failed
117
    ICMP_PHOTURIS_DECRYPT_FAILED  equ  3		; decrypt failed 
120
    ICMP_PHOTURIS_DECRYPT_FAILED  equ  3		; decrypt failed
118
 
121
 
119
 
122
 
120
;***************************************************************************
123
;***************************************************************************
121
;   Function
124
;   Function
122
;      icmp_rx  [by Johnny_B]
125
;      icmp_rx  [by Johnny_B]
123
;
126
;
124
;   Description
127
;   Description
125
;       ICMP protocol handler
128
;       ICMP protocol handler
126
;       This is a kernel function, called by ip_rx
129
;       This is a kernel function, called by ip_rx
127
;
130
;
128
;  IN:
131
;  IN:
129
;    buffer_number  - # of IP-buffer. This buffer must be reused or marked as empty afterwards
132
;    buffer_number  - # of IP-buffer. This buffer must be reused or marked as empty afterwards
130
;    IPPacketBase   - IP_PACKET base address
133
;    IPPacketBase   - IP_PACKET base address
131
;    IPHeaderLength - Header length of IP_PACKET
134
;    IPHeaderLength - Header length of IP_PACKET
132
;
135
;
133
;  OUT:
136
;  OUT:
134
;    EAX=not defined
137
;    EAX=not defined
135
;
138
;
136
;           All used registers will be saved
139
;           All used registers will be saved
137
;
140
;
138
;***************************************************************************
141
;***************************************************************************
139
proc icmp_rx  stdcall  uses ebx esi edi,\
142
proc icmp_rx  stdcall  uses ebx esi edi,\
140
    buffer_number:DWORD,IPPacketBase:DWORD,IPHeaderLength:DWORD
143
    buffer_number:DWORD,IPPacketBase:DWORD,IPHeaderLength:DWORD
141
 
144
 
142
    mov     esi,[IPPacketBase]   ;esi=IP_PACKET base address
145
    mov     esi,[IPPacketBase]   ;esi=IP_PACKET base address
143
    mov     edi, esi
146
    mov     edi, esi
144
    add     edi,[IPHeaderLength] ;edi=ICMP_PACKET base address
147
    add     edi,[IPHeaderLength] ;edi=ICMP_PACKET base address
145
 
148
 
146
    cmp     byte[edi + ICMP_PACKET.Type], ICMP_ECHO ; Is this an echo request? discard if not
149
    cmp     byte[edi + ICMP_PACKET.Type], ICMP_ECHO ; Is this an echo request? discard if not
147
    jz      .icmp_echo
150
    jz      .icmp_echo
148
 
151
 
149
    mov     eax, [buffer_number]
152
    mov     eax, [buffer_number]
150
    call    freeBuff
153
    call    freeBuff
151
    jmp     .exit
154
    jmp     .exit
152
 
155
 
153
  .icmp_echo:
156
  .icmp_echo:
154
 
157
 
155
    ; swap the source and destination addresses
158
    ; swap the source and destination addresses
156
    mov     ecx, [esi + IP_PACKET.DestinationAddress]
159
    mov     ecx, [esi + IP_PACKET.DestinationAddress]
157
    mov     ebx, [esi + IP_PACKET.SourceAddress]
160
    mov     ebx, [esi + IP_PACKET.SourceAddress]
158
    mov     [esi + IP_PACKET.DestinationAddress], ebx
161
    mov     [esi + IP_PACKET.DestinationAddress], ebx
159
    mov     [esi + IP_PACKET.SourceAddress], ecx
162
    mov     [esi + IP_PACKET.SourceAddress], ecx
160
 
163
 
161
    ; recalculate the IP header checksum
164
    ; recalculate the IP header checksum
162
    mov     eax,[IPHeaderLength]
165
    mov     eax,[IPHeaderLength]
163
    stdcall checksum_jb,esi,eax   ;buf_ptr,buf_size
166
    stdcall checksum_jb,esi,eax   ;buf_ptr,buf_size
164
 
167
 
165
    mov     byte[esi + IP_PACKET.HeaderChecksum], ah
168
    mov     byte[esi + IP_PACKET.HeaderChecksum], ah
166
    mov     byte[esi + IP_PACKET.HeaderChecksum + 1], al      ; ?? correct byte order?
169
    mov     byte[esi + IP_PACKET.HeaderChecksum + 1], al      ; ?? correct byte order?
167
 
170
 
168
    mov     byte[edi + ICMP_PACKET.Type], ICMP_ECHOREPLY  ; change the request to a response
171
    mov     byte[edi + ICMP_PACKET.Type], ICMP_ECHOREPLY  ; change the request to a response
169
    mov     word[edi + ICMP_PACKET.Checksum], 0  ; clear ICMP checksum prior to re-calc
172
    mov     word[edi + ICMP_PACKET.Checksum], 0  ; clear ICMP checksum prior to re-calc
170
 
173
 
171
    ; Calculate the length of the ICMP data ( IP payload)
174
    ; Calculate the length of the ICMP data ( IP payload)
172
    xor     eax, eax
175
    xor     eax, eax
173
    mov     ah, byte[esi + IP_PACKET.TotalLength]
176
    mov     ah, byte[esi + IP_PACKET.TotalLength]
174
    mov     al, byte[esi + IP_PACKET.TotalLength + 1]
177
    mov     al, byte[esi + IP_PACKET.TotalLength + 1]
175
    sub     ax, word[IPHeaderLength]  ;ax=ICMP-packet length
178
    sub     ax, word[IPHeaderLength]  ;ax=ICMP-packet length
176
 
179
 
177
    stdcall checksum_jb,edi,eax   ;buf_ptr,buf_size
180
    stdcall checksum_jb,edi,eax   ;buf_ptr,buf_size
178
 
181
 
179
    mov     byte[edi + ICMP_PACKET.Checksum], ah
182
    mov     byte[edi + ICMP_PACKET.Checksum], ah
180
    mov     byte[edi + ICMP_PACKET.Checksum + 1], al
183
    mov     byte[edi + ICMP_PACKET.Checksum + 1], al
181
 
184
 
182
    ; Queue packet for transmission
185
    ; Queue packet for transmission
183
    mov     ebx, [buffer_number]
186
    mov     ebx, [buffer_number]
184
    mov     eax, NET1OUT_QUEUE
187
    mov     eax, NET1OUT_QUEUE
185
    call    queue
188
    call    queue
186
 
189
 
187
  .exit:
190
  .exit:
188
    ret
191
    ret
189
endp
192
endp