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 593
1
$Revision: 425 $
-
 
2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3
;;                                                                 ;;
2
;;                                                                 ;;
4
;;  IP.INC                                                         ;;
3
;;  IP.INC                                                         ;;
5
;;                                                                 ;;
4
;;                                                                 ;;
6
;;  IP Processes for Menuet OS  TCP/IP stack                       ;;
5
;;  IP Processes for Menuet OS  TCP/IP stack                       ;;
7
;;                                                                 ;;
6
;;                                                                 ;;
8
;;  Version 0.3  29 August 2002                                    ;;
7
;;  Version 0.3  29 August 2002                                    ;;
9
;;                                                                 ;;
8
;;                                                                 ;;
10
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net               ;;
9
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net               ;;
11
;;                                                                 ;;
10
;;                                                                 ;;
12
;;  See file COPYING for details                                   ;;
11
;;  See file COPYING for details                                   ;;
13
;;                                                                 ;;
12
;;                                                                 ;;
14
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
 
14
 
-
 
15
$Revision: 593 $
-
 
16
 
15
 
17
 
16
; IP underlying protocols numbers
18
; IP underlying protocols numbers
17
PROTOCOL_ICMP     equ      1
19
PROTOCOL_ICMP     equ      1
18
PROTOCOL_TCP      equ      6
20
PROTOCOL_TCP      equ      6
19
PROTOCOL_UDP      equ      17
21
PROTOCOL_UDP      equ      17
20
 
22
 
21
struc IP_PACKET
23
struc IP_PACKET
22
{  .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]
23
   .TypeOfService           db   ?  ;+01
25
   .TypeOfService           db   ?  ;+01
24
   .TotalLength             dw   ?  ;+02
26
   .TotalLength             dw   ?  ;+02
25
   .Identification          dw   ?  ;+04
27
   .Identification          dw   ?  ;+04
26
   .FlagsAndFragmentOffset  dw   ?  ;+06 - Flags[0-2] and FragmentOffset[3-15]
28
   .FlagsAndFragmentOffset  dw   ?  ;+06 - Flags[0-2] and FragmentOffset[3-15]
27
   .TimeToLive              db   ?  ;+08
29
   .TimeToLive              db   ?  ;+08
28
   .Protocol                db   ?  ;+09
30
   .Protocol                db   ?  ;+09
29
   .HeaderChecksum          dw   ?  ;+10
31
   .HeaderChecksum          dw   ?  ;+10
30
   .SourceAddress           dd   ?  ;+12
32
   .SourceAddress           dd   ?  ;+12
31
   .DestinationAddress      dd   ?  ;+16
33
   .DestinationAddress      dd   ?  ;+16
32
   .DataOrOptional          dd   ?  ;+20
34
   .DataOrOptional          dd   ?  ;+20
33
}
35
}
34
 
36
 
35
virtual at 0
37
virtual at 0
36
  IP_PACKET IP_PACKET
38
  IP_PACKET IP_PACKET
37
end virtual
39
end virtual
38
 
40
 
39
 
41
 
40
;*******************************************************************
42
;*******************************************************************
41
;   Interface
43
;   Interface
42
;
44
;
43
;       ip_rx       processes all packets received by the network layer
45
;       ip_rx       processes all packets received by the network layer
44
;                   It calls the appropriate protocol handler
46
;                   It calls the appropriate protocol handler
45
;
47
;
46
;
48
;
47
;
49
;
48
;*******************************************************************
50
;*******************************************************************
49
 
51
 
50
 
52
 
51
;
53
;
52
;   IP Packet after reception - Normal IP packet format
54
;   IP Packet after reception - Normal IP packet format
53
;
55
;
54
;           0               1               2               3
56
;           0               1               2               3
55
;    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
57
;    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
56
;
58
;
57
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58
;0  |Version|  IHL  |Type of Service|       Total Length            |
60
;0  |Version|  IHL  |Type of Service|       Total Length            |
59
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60
;4  |         Identification        |Flags|      Fragment Offset    |
62
;4  |         Identification        |Flags|      Fragment Offset    |
61
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62
;8  |  Time to Live |    Protocol   |         Header Checksum       |
64
;8  |  Time to Live |    Protocol   |         Header Checksum       |
63
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64
;12 |                       Source Address                          |
66
;12 |                       Source Address                          |
65
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66
;16 |                    Destination Address                        |
68
;16 |                    Destination Address                        |
67
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68
;20 |      Data                                                     |
70
;20 |      Data                                                     |
69
;   +-+-+-..........                                               -+
71
;   +-+-+-..........                                               -+
70
;
72
;
71
;
73
;
72
;    [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,
73
; 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
74
; IHL field
76
; IHL field
75
;
77
;
76
macro	GET_IHL reg, header_addr
78
macro	GET_IHL reg, header_addr
77
{
79
{
78
	movzx	reg, byte [header_addr]
80
	movzx	reg, byte [header_addr]
79
	
81
	
80
	; we need 4-7 bits, so....
82
	; we need 4-7 bits, so....
81
	and	reg, 0x0000000F
83
	and	reg, 0x0000000F
82
	
84
	
83
	; IHL keeps number of octets, so we need to << 2 'reg'
85
	; IHL keeps number of octets, so we need to << 2 'reg'
84
	shl     reg, 2
86
	shl     reg, 2
85
}
87
}
86
 
88
 
87
 
89
 
88
;***************************************************************************
90
;***************************************************************************
89
;   Function
91
;   Function
90
;      ip_rx
92
;      ip_rx
91
;
93
;
92
;   Description
94
;   Description
93
;       This is a kernel function, called by stack_handler
95
;       This is a kernel function, called by stack_handler
94
;       Processes all IP-packets received by the network layer
96
;       Processes all IP-packets received by the network layer
95
;       It calls the appropriate protocol handler
97
;       It calls the appropriate protocol handler
96
;
98
;
97
;***************************************************************************
99
;***************************************************************************
98
proc ip_rx stdcall
100
proc ip_rx stdcall
99
local buffer_number dd ?
101
local buffer_number dd ?
100
 
102
 
101
    ; Look for a buffer to tx
103
    ; Look for a buffer to tx
102
    mov     eax, IPIN_QUEUE
104
    mov     eax, IPIN_QUEUE
103
    call    dequeue
105
    call    dequeue
104
    cmp     ax, NO_BUFFER
106
    cmp     ax, NO_BUFFER
105
    je      .exit         ; Exit if no buffer available
107
    je      .exit         ; Exit if no buffer available
106
 
108
 
107
    mov     [buffer_number], eax    ;save buffer number
109
    mov     [buffer_number], eax    ;save buffer number
108
 
110
 
109
    ; convert buffer pointer eax to the absolute address
111
    ; convert buffer pointer eax to the absolute address
110
    imul    eax, IPBUFFSIZE
112
    imul    eax, IPBUFFSIZE
111
    add     eax, IPbuffs
113
    add     eax, IPbuffs
112
 
114
 
113
    mov     ebx, eax  ; ebx=pointer to IP_PACKET
115
    mov     ebx, eax  ; ebx=pointer to IP_PACKET
114
 
116
 
115
    ; Validate the IP checksum
117
    ; Validate the IP checksum
116
    mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
118
    mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
117
    xchg    dh,dl          ; Get the checksum in intel format
119
    xchg    dh,dl          ; Get the checksum in intel format
118
  
120
  
119
    mov     [ebx + IP_PACKET.HeaderChecksum], word 0  ; clear checksum field - need to when
121
    mov     [ebx + IP_PACKET.HeaderChecksum], word 0  ; clear checksum field - need to when
120
                                ; recalculating checksum
122
                                ; recalculating checksum
121
    ;  this needs two data pointers and two size #.
123
    ;  this needs two data pointers and two size #.
122
    ;  2nd pointer can be of length 0
124
    ;  2nd pointer can be of length 0
123
 
125
 
124
    GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
126
    GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
125
    stdcall checksum_jb, ebx, ecx   ;buf_ptr, buf_size
127
    stdcall checksum_jb, ebx, ecx   ;buf_ptr, buf_size
126
    cmp     dx, ax
128
    cmp     dx, ax
127
 
129
 
128
    mov     edx, ebx ; EDX (IP-BUFFER POINTER) WILL BE USED FOR *_rx HANDLERS BELOW!!!
130
    mov     edx, ebx ; EDX (IP-BUFFER POINTER) WILL BE USED FOR *_rx HANDLERS BELOW!!!
129
    jnz     .dump  ;if CHECKSUM isn't valid then dump packet
131
    jnz     .dump  ;if CHECKSUM isn't valid then dump packet
130
 
132
 
131
    ; Validate the IP address, if it isn't broadcast
133
    ; Validate the IP address, if it isn't broadcast
132
    mov     eax, [stack_ip]
134
    mov     eax, [stack_ip]
133
    cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
135
    cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
134
    je      @f
136
    je      @f
135
 
137
 
136
    ; If the IP address is 255.255.255.255, accept it
138
    ; If the IP address is 255.255.255.255, accept it
137
    ; - it is a broadcast packet, which we need for dhcp
139
    ; - it is a broadcast packet, which we need for dhcp
138
    cmp     dword[ebx + IP_PACKET.DestinationAddress], 0xffffffff
140
    cmp     dword[ebx + IP_PACKET.DestinationAddress], 0xffffffff
139
    jne     .dump
141
    jne     .dump
140
 
142
 
141
  @@:
143
  @@:
142
    mov     al, [ebx + IP_PACKET.VersionAndIHL]
144
    mov     al, [ebx + IP_PACKET.VersionAndIHL]
143
    and     al, 0x0f  ;get IHL(header length)
145
    and     al, 0x0f  ;get IHL(header length)
144
    cmp     al, 0x05  ;if IHL!= 5*4(20 bytes)
146
    cmp     al, 0x05  ;if IHL!= 5*4(20 bytes)
145
    jnz     .dump     ;then dump it
147
    jnz     .dump     ;then dump it
146
 
148
 
147
    cmp     byte[ebx + IP_PACKET.TimeToLive], byte 0
149
    cmp     byte[ebx + IP_PACKET.TimeToLive], byte 0
148
    je      .dump     ;if TTL==0 then dump it
150
    je      .dump     ;if TTL==0 then dump it
149
 
151
 
150
    mov     ax, word[ebx + IP_PACKET.FlagsAndFragmentOffset]
152
    mov     ax, word[ebx + IP_PACKET.FlagsAndFragmentOffset]
151
    and     ax, 0xFFBF   ;get flags
153
    and     ax, 0xFFBF   ;get flags
152
    cmp     ax, 0        ;if some flags was set then we dump this packet
154
    cmp     ax, 0        ;if some flags was set then we dump this packet
153
    jnz     .dump        ;the flags should be used for fragmented packets
155
    jnz     .dump        ;the flags should be used for fragmented packets
154
 
156
 
155
    ; Check the protocol, and call the appropriate handler
157
    ; Check the protocol, and call the appropriate handler
156
    ; Each handler will re-use or free the queue buffer as appropriate
158
    ; Each handler will re-use or free the queue buffer as appropriate
157
 
159
 
158
    mov     al, [ebx + IP_PACKET.Protocol]
160
    mov     al, [ebx + IP_PACKET.Protocol]
159
 
161
 
160
    cmp     al , PROTOCOL_TCP
162
    cmp     al , PROTOCOL_TCP
161
    jne     .not_tcp
163
    jne     .not_tcp
162
    DEBUGF  1,"K : ip_rx - TCP packet\n"
164
    DEBUGF  1,"K : ip_rx - TCP packet\n"
163
    mov     eax, dword[buffer_number]
165
    mov     eax, dword[buffer_number]
164
    call    tcp_rx
166
    call    tcp_rx
165
    jmp     .exit
167
    jmp     .exit
166
 
168
 
167
  .not_tcp:
169
  .not_tcp:
168
    cmp     al, PROTOCOL_UDP
170
    cmp     al, PROTOCOL_UDP
169
    jne     .not_udp
171
    jne     .not_udp
170
    DEBUGF  1,"K : ip_rx - UDP packet\n"
172
    DEBUGF  1,"K : ip_rx - UDP packet\n"
171
    mov     eax, dword[buffer_number]
173
    mov     eax, dword[buffer_number]
172
    call    udp_rx
174
    call    udp_rx
173
    jmp     .exit
175
    jmp     .exit
174
 
176
 
175
  .not_udp:
177
  .not_udp:
176
    cmp     al , PROTOCOL_ICMP
178
    cmp     al , PROTOCOL_ICMP
177
    jne     .dump              ;protocol ain't supported
179
    jne     .dump              ;protocol ain't supported
178
 
180
 
179
    DEBUGF  1,"K : ip_rx - ICMP packet\n"
181
    DEBUGF  1,"K : ip_rx - ICMP packet\n"
180
    ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
182
    ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
181
    mov     eax, dword[buffer_number]
183
    mov     eax, dword[buffer_number]
182
    stdcall icmp_rx,eax,ebx,ecx  ;buffer_number,IPPacketBase,IPHeaderLength
184
    stdcall icmp_rx,eax,ebx,ecx  ;buffer_number,IPPacketBase,IPHeaderLength
183
    jmp     .exit
185
    jmp     .exit
184
 
186
 
185
 
187
 
186
.dump:
188
.dump:
187
    ; No protocol handler available, so
189
    ; No protocol handler available, so
188
    ; silently dump the packet, freeing up the queue buffer
190
    ; silently dump the packet, freeing up the queue buffer
189
 
191
 
190
    inc     dword [dumped_rx_count]
192
    inc     dword [dumped_rx_count]
191
 
193
 
192
    mov     eax, dword[buffer_number]
194
    mov     eax, dword[buffer_number]
193
    call    freeBuff
195
    call    freeBuff
194
 
196
 
195
  .exit:
197
  .exit:
196
    ret
198
    ret
197
endp
199
endp