Subversion Repositories Kolibri OS

Rev

Rev 1635 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1635 Rev 2434
Line 10... Line 10...
10
;;                                                                 ;;
10
;;                                                                 ;;
11
;;  See file COPYING for details                                   ;;
11
;;  See file COPYING for details                                   ;;
12
;;                                                                 ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 14... Line 14...
14
 
14
 
Line 15... Line 15...
15
$Revision: 922 $
15
$Revision: 2434 $
16
 
16
 
17
 
17
 
18
; IP underlying protocols numbers
18
; IP underlying protocols numbers
Line 19... Line 19...
19
PROTOCOL_ICMP	  equ	   1
19
PROTOCOL_ICMP     equ      1
20
PROTOCOL_TCP	  equ	   6
20
PROTOCOL_TCP      equ      6
21
PROTOCOL_UDP	  equ	   17
21
PROTOCOL_UDP      equ      17
22
 
22
 
23
struc IP_PACKET
23
struc IP_PACKET
24
{  .VersionAndIHL	    db	 ?  ;+00 - Version[0-3 bits] and IHL(header length)[4-7 bits]
24
{  .VersionAndIHL           db   ?  ;+00 - Version[0-3 bits] and IHL(header length)[4-7 bits]
25
   .TypeOfService	    db	 ?  ;+01
25
   .TypeOfService           db   ?  ;+01
26
   .TotalLength 	    dw	 ?  ;+02
26
   .TotalLength             dw   ?  ;+02
27
   .Identification	    dw	 ?  ;+04
27
   .Identification          dw   ?  ;+04
28
   .FlagsAndFragmentOffset  dw	 ?  ;+06 - Flags[0-2] and FragmentOffset[3-15]
28
   .FlagsAndFragmentOffset  dw   ?  ;+06 - Flags[0-2] and FragmentOffset[3-15]
29
   .TimeToLive		    db	 ?  ;+08
29
   .TimeToLive              db   ?  ;+08
30
   .Protocol		    db	 ?  ;+09
30
   .Protocol                db   ?  ;+09
31
   .HeaderChecksum	    dw	 ?  ;+10
31
   .HeaderChecksum          dw   ?  ;+10
Line 32... Line 32...
32
   .SourceAddress	    dd	 ?  ;+12
32
   .SourceAddress           dd   ?  ;+12
33
   .DestinationAddress	    dd	 ?  ;+16
33
   .DestinationAddress      dd   ?  ;+16
34
   .DataOrOptional	    dd	 ?  ;+20
34
   .DataOrOptional          dd   ?  ;+20
Line 73... Line 73...
73
;
73
;
74
;    [smb] attention! according to RFC 791 IP packet may have 'options' sections,
74
;    [smb] attention! according to RFC 791 IP packet may have 'options' sections,
75
; so we can't simply think, that data have offset 20. We must calculate offset from
75
; so we can't simply think, that data have offset 20. We must calculate offset from
76
; IHL field
76
; IHL field
77
;
77
;
78
macro	GET_IHL reg, header_addr
78
macro   GET_IHL reg, header_addr
79
{
79
{
80
	movzx	reg, byte [header_addr]
80
        movzx   reg, byte [header_addr]
81
	
81
        
82
	; we need 4-7 bits, so....
82
        ; we need 4-7 bits, so....
83
	and	reg, 0x0000000F
83
        and     reg, 0x0000000F
84
	
84
        
85
	; IHL keeps number of octets, so we need to << 2 'reg'
85
        ; IHL keeps number of octets, so we need to << 2 'reg'
86
	shl	reg, 2
86
        shl     reg, 2
87
}
87
}
Line 88... Line 88...
88
 
88
 
89
 
89
 
Line 103... Line 103...
103
;***************************************************************************
103
;***************************************************************************
104
proc ip_rx stdcall
104
proc ip_rx stdcall
105
local buffer_number dd ?
105
local buffer_number dd ?
Line 106... Line 106...
106
 
106
 
107
    ; Look for a buffer to tx
107
    ; Look for a buffer to tx
108
    mov     eax, IPIN_QUEUE
108
        mov     eax, IPIN_QUEUE
109
    call    dequeue
109
        call    dequeue
110
    cmp     ax, NO_BUFFER
110
        cmp     ax, NO_BUFFER
Line 111... Line 111...
111
    je	    .exit	  ; Exit if no buffer available
111
        je      .exit     ; Exit if no buffer available
Line 112... Line 112...
112
 
112
 
113
    mov     [buffer_number], eax    ;save buffer number
113
        mov     [buffer_number], eax;save buffer number
114
 
114
 
Line 115... Line 115...
115
    ; convert buffer pointer eax to the absolute address
115
    ; convert buffer pointer eax to the absolute address
Line 116... Line 116...
116
    imul    eax, IPBUFFSIZE
116
        imul    eax, IPBUFFSIZE
Line 117... Line 117...
117
    add     eax, IPbuffs
117
        add     eax, IPbuffs
118
 
118
 
119
    mov     ebx, eax  ; ebx=pointer to IP_PACKET
119
        mov     ebx, eax; ebx=pointer to IP_PACKET
Line 120... Line 120...
120
 
120
 
121
;        DEBUGF  1, "K : ip_rx - proto: %u\n", [ebx + IP_PACKET.Protocol]:1
121
;        DEBUGF  1, "K : ip_rx - proto: %u\n", [ebx + IP_PACKET.Protocol]:1
122
 
122
 
123
    ; Validate the IP checksum
123
    ; Validate the IP checksum
Line 124... Line 124...
124
    mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
124
        mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
125
    xchg    dh,dl	   ; Get the checksum in intel format
125
        xchg    dh, dl     ; Get the checksum in intel format
126
  
126
  
Line 127... Line 127...
127
    mov     [ebx + IP_PACKET.HeaderChecksum], 0  ; clear checksum field - need to when
127
        mov     [ebx + IP_PACKET.HeaderChecksum], 0; clear checksum field - need to when
Line 128... Line 128...
128
				; recalculating checksum
128
                                ; recalculating checksum
129
    ;  this needs two data pointers and two size #.
129
    ;  this needs two data pointers and two size #.
Line 130... Line 130...
130
    ;  2nd pointer can be of length 0
130
    ;  2nd pointer can be of length 0
Line 131... Line 131...
131
 
131
 
132
    GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
132
    GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
133
    stdcall checksum_jb, ebx, ecx   ;buf_ptr, buf_size
133
        stdcall checksum_jb, ebx, ecx;buf_ptr, buf_size
134
    cmp     dx, ax
134
        cmp     dx, ax
Line 135... Line 135...
135
 
135
 
136
;        DEBUGF  1, "K : ip_rx - checksums: %x - %x\n", dx, ax
136
;        DEBUGF  1, "K : ip_rx - checksums: %x - %x\n", dx, ax
Line 137... Line 137...
137
 
137
 
138
    jnz     .dump.1  ;if CHECKSUM isn't valid then dump packet
138
        jnz     .dump.1;if CHECKSUM isn't valid then dump packet
139
    mov     edx, ebx ; EDX (IP-BUFFER POINTER) WILL BE USED FOR *_rx HANDLERS BELOW!!!
139
        mov     edx, ebx; EDX (IP-BUFFER POINTER) WILL BE USED FOR *_rx HANDLERS BELOW!!!
140
 
140
 
141
;        DEBUGF  1, "K : ip_rx - dest: %x - %x\n", [ebx + IP_PACKET.DestinationAddress], [stack_ip]
141
;        DEBUGF  1, "K : ip_rx - dest: %x - %x\n", [ebx + IP_PACKET.DestinationAddress], [stack_ip]
142
 
142
 
143
    ; Validate the IP address, if it isn't broadcast
143
    ; Validate the IP address, if it isn't broadcast
144
    mov     eax, [stack_ip]
144
        mov     eax, [stack_ip]
145
    cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
145
        cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
146
    je	    @f
146
        je      @f
147
 
147
 
148
    ; If the IP address is 255.255.255.255, accept it
148
    ; If the IP address is 255.255.255.255, accept it
Line 149... Line 149...
149
    ; - it is a broadcast packet, which we need for dhcp
149
    ; - it is a broadcast packet, which we need for dhcp
150
 
150
 
151
	mov	eax, [ebx + IP_PACKET.DestinationAddress]
151
        mov     eax, [ebx + IP_PACKET.DestinationAddress]
152
	cmp	eax, 0xffffffff
152
        cmp     eax, 0xffffffff
153
	je	@f
153
        je      @f
154
	mov	ecx, [stack_ip]
154
        mov     ecx, [stack_ip]
Line 155... Line 155...
155
	and	eax, [subnet_mask]
155
        and     eax, [subnet_mask]
Line 156... Line 156...
156
	and	ecx, [subnet_mask]
156
        and     ecx, [subnet_mask]
157
	cmp	eax, ecx
157
        cmp     eax, ecx
Line 158... Line 158...
158
	jne	.dump.2
158
        jne     .dump.2
159
	mov	eax, [ebx + IP_PACKET.DestinationAddress]
159
        mov     eax, [ebx + IP_PACKET.DestinationAddress]
160
	or	eax, [subnet_mask]
160
        or      eax, [subnet_mask]
161
	cmp	eax, 0xffffffff
161
        cmp     eax, 0xffffffff
162
	jne	.dump.2
162
        jne     .dump.2
Line 163... Line 163...
163
 
163
 
164
  @@:
164
  @@:
Line 165... Line 165...
165
    mov     al, [ebx + IP_PACKET.VersionAndIHL]
165
        mov     al, [ebx + IP_PACKET.VersionAndIHL]
Line 166... Line 166...
166
    and     al, 0x0f  ;get IHL(header length)
166
        and     al, 0x0f;get IHL(header length)
167
    cmp     al, 0x05  ;if IHL!= 5*4(20 bytes)
167
        cmp     al, 0x05;if IHL!= 5*4(20 bytes)
168
;        DEBUGF  1, "K : ip_rx - ihl: %x - 05\n", al
168
;        DEBUGF  1, "K : ip_rx - ihl: %x - 05\n", al
169
    jnz     .dump.3	;then dump it
169
        jnz     .dump.3 ;then dump it
170
 
170
 
171
;        DEBUGF  1, "K : ip_rx - ttl: %x - 00\n", [ebx + IP_PACKET.TimeToLive]:2
171
;        DEBUGF  1, "K : ip_rx - ttl: %x - 00\n", [ebx + IP_PACKET.TimeToLive]:2
Line 172... Line 172...
172
 
172
 
173
    cmp     [ebx + IP_PACKET.TimeToLive], 0
173
        cmp     [ebx + IP_PACKET.TimeToLive], 0
174
    je	    .dump.4	;if TTL==0 then dump it
174
        je      .dump.4 ;if TTL==0 then dump it
175
 
175
 
176
    mov     ax, [ebx + IP_PACKET.FlagsAndFragmentOffset]
176
        mov     ax, [ebx + IP_PACKET.FlagsAndFragmentOffset]
177
    and     ax, 0xFFBF	 ;get flags
177
        and     ax, 0xFFBF;get flags
178
;        DEBUGF  1, "K : ip_rx - flags: %x - 0000\n", ax
178
;        DEBUGF  1, "K : ip_rx - flags: %x - 0000\n", ax
Line 179... Line 179...
179
    cmp     ax, 0	 ;if some flags was set then we dump this packet
179
        cmp     ax, 0    ;if some flags was set then we dump this packet
180
    jnz     .dump.5	   ;the flags should be used for fragmented packets
180
        jnz     .dump.5    ;the flags should be used for fragmented packets
181
 
181
 
Line 182... Line 182...
182
    ; Check the protocol, and call the appropriate handler
182
    ; Check the protocol, and call the appropriate handler
183
    ; Each handler will re-use or free the queue buffer as appropriate
183
    ; Each handler will re-use or free the queue buffer as appropriate
184
 
184
 
185
    mov     al, [ebx + IP_PACKET.Protocol]
185
        mov     al, [ebx + IP_PACKET.Protocol]
186
 
186
 
Line 187... Line 187...
187
    cmp     al , PROTOCOL_TCP
187
        cmp     al , PROTOCOL_TCP
188
    jne     .not_tcp
188
        jne     .not_tcp
189
;    DEBUGF  1,"K : ip_rx - TCP packet\n"
189
;    DEBUGF  1,"K : ip_rx - TCP packet\n"
Line 190... Line 190...
190
    mov     eax, dword[buffer_number]
190
        mov     eax, dword[buffer_number]
191
    call    tcp_rx
191
        call    tcp_rx
192
    jmp     .exit
192
        jmp     .exit
Line 193... Line 193...
193
 
193
 
194
  .not_tcp:
194
  .not_tcp:
195
    cmp     al, PROTOCOL_UDP
195
        cmp     al, PROTOCOL_UDP
Line 196... Line 196...
196
    jne     .not_udp
196
        jne     .not_udp
197
;    DEBUGF  1,"K : ip_rx - UDP packet\n"
197
;    DEBUGF  1,"K : ip_rx - UDP packet\n"
198
    mov     eax, dword[buffer_number]
198
        mov     eax, dword[buffer_number]
Line 199... Line 199...
199
    call    udp_rx
199
        call    udp_rx
200
    jmp     .exit
200
        jmp     .exit
201
 
201
 
Line 202... Line 202...
202
  .not_udp:
202
  .not_udp:
203
    cmp     al, PROTOCOL_ICMP
203
        cmp     al, PROTOCOL_ICMP
Line 204... Line 204...
204
    jne     .dump.6		 ;protocol ain't supported
204
        jne     .dump.6          ;protocol ain't supported
205
 
205
 
206
;    DEBUGF  1,"K : ip_rx - ICMP packet\n"
206
;    DEBUGF  1,"K : ip_rx - ICMP packet\n"
207
    ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
207
    ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
Line 208... Line 208...
208
    mov     eax, dword[buffer_number]
208
        mov     eax, dword[buffer_number]
209
    stdcall icmp_rx,eax,ebx,ecx  ;buffer_number,IPPacketBase,IPHeaderLength
209
        stdcall icmp_rx, eax, ebx, ecx;buffer_number,IPPacketBase,IPHeaderLength
210
    jmp     .exit
210
        jmp     .exit