Rev 1200 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1200 | Rev 1206 | ||
---|---|---|---|
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; STACK.INC ;; |
6 | ;; STACK.INC ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; BASIC TCP/IP stack for KolibriOS ;; |
8 | ;; BASIC TCP/IP stack for KolibriOS ;; |
9 | ;; ;; |
9 | ;; ;; |
10 | ;; Written by hidnplayr@kolibrios.org ;; |
10 | ;; Written by hidnplayr@kolibrios.org ;; |
11 | ;; ;; |
11 | ;; ;; |
12 | ;; based on the work of Mike Hibbett, mikeh@oceanfree.net ;; |
12 | ;; based on the work of Mike Hibbett, mikeh@oceanfree.net ;; |
13 | ;; but also Paolo Franchetti ;; |
13 | ;; but also Paolo Franchetti ;; |
14 | ;; ;; |
14 | ;; ;; |
15 | ;; GNU GENERAL PUBLIC LICENSE ;; |
15 | ;; GNU GENERAL PUBLIC LICENSE ;; |
16 | ;; Version 2, June 1991 ;; |
16 | ;; Version 2, June 1991 ;; |
17 | ;; ;; |
17 | ;; ;; |
18 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
18 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
19 | 19 | ||
20 | $Revision: 983 $ |
20 | $Revision: 1206 $ |
21 | 21 | ||
22 | uglobal |
22 | uglobal |
23 | last_1sTick db ? |
23 | last_1sTick db ? |
24 | last_1hsTick dd ? |
24 | last_1hsTick dd ? |
25 | endg |
25 | endg |
26 | 26 | ||
27 | MAX_NET_DEVICES equ 16 |
27 | MAX_NET_DEVICES equ 16 |
28 | 28 | ||
29 | MIN_EPHEMERAL_PORT equ 49152 |
29 | MIN_EPHEMERAL_PORT equ 49152 |
30 | MAX_EPHEMERAL_PORT equ 61000 |
30 | MAX_EPHEMERAL_PORT equ 61000 |
31 | 31 | ||
32 | ETHER equ 1337 |
32 | ETHER equ 1337 |
33 | ETHER_ARP equ 0x0608 |
33 | ETHER_ARP equ 0x0608 |
34 | 34 | ||
35 | ;AF_UNSPEC equ 0 |
35 | ;AF_UNSPEC equ 0 |
36 | ;AF_UNIX equ 1 |
36 | ;AF_UNIX equ 1 |
37 | AF_INET4 equ 2 |
37 | AF_INET4 equ 2 |
38 | ;AF_AX25 equ 3 |
38 | ;AF_AX25 equ 3 |
39 | ;AF_IPX equ 4 |
39 | ;AF_IPX equ 4 |
40 | ;AF_APPLETALK equ 5 |
40 | ;AF_APPLETALK equ 5 |
41 | ;AF_NETROM equ 6 |
41 | ;AF_NETROM equ 6 |
42 | ;AF_BRIDGE equ 7 |
42 | ;AF_BRIDGE equ 7 |
43 | ;AF_AAL5 equ 8 |
43 | ;AF_AAL5 equ 8 |
44 | ;AF_X25 equ 9 |
44 | ;AF_X25 equ 9 |
45 | ;AF_INET6 equ 10 |
45 | ;AF_INET6 equ 10 |
46 | ;AF_MAX equ 12 |
46 | ;AF_MAX equ 12 |
47 | 47 | ||
48 | IP_PROTO_IP equ 0 |
48 | IP_PROTO_IP equ 0 |
49 | IP_PROTO_ICMP equ 1 |
49 | IP_PROTO_ICMP equ 1 |
50 | IP_PROTO_TCP equ 6 |
50 | IP_PROTO_TCP equ 6 |
51 | IP_PROTO_UDP equ 17 |
51 | IP_PROTO_UDP equ 17 |
52 | 52 | ||
53 | ; Socket types |
53 | ; Socket types |
54 | SOCK_STREAM = 1 |
54 | SOCK_STREAM = 1 |
55 | SOCK_DGRAM = 2 |
55 | SOCK_DGRAM = 2 |
56 | SOCK_RAW = 3 |
56 | SOCK_RAW = 3 |
57 | 57 | ||
58 | ; TCP opening modes |
58 | ; TCP opening modes |
59 | SOCKET_PASSIVE equ 0 |
59 | SOCKET_PASSIVE equ 0 |
60 | SOCKET_ACTIVE equ 1 |
60 | SOCKET_ACTIVE equ 1 |
61 | 61 | ||
62 | include "queue.inc" |
62 | include "queue.inc" |
63 | include "ARP.inc" |
63 | include "ARP.inc" |
64 | include "IPv4.inc" |
64 | include "IPv4.inc" |
65 | include "ethernet.inc" |
65 | include "ethernet.inc" |
66 | include "socket.inc" |
66 | include "socket.inc" |
67 | ;include "tcp.inc" |
67 | ;include "tcp.inc" |
68 | include "udp.inc" |
68 | include "udp.inc" |
69 | include "icmp.inc" |
69 | include "icmp.inc" |
70 | 70 | ||
71 | ;----------------------------------------------- |
71 | ;----------------------------------------------- |
72 | ; |
72 | ; |
73 | ; stack_init |
73 | ; stack_init |
74 | ; |
74 | ; |
75 | ; This function calls all network init procedures |
75 | ; This function calls all network init procedures |
76 | ; |
76 | ; |
77 | ; IN: / |
77 | ; IN: / |
78 | ; OUT: / |
78 | ; OUT: / |
79 | ; |
79 | ; |
80 | ;----------------------------------------------- |
80 | ;----------------------------------------------- |
81 | 81 | ||
82 | align 4 |
82 | align 4 |
83 | stack_init: |
83 | stack_init: |
84 | 84 | ||
85 | call ETH_init |
85 | call ETH_init |
86 | call IPv4_init |
86 | call IPv4_init |
87 | call ARP_init |
87 | call ARP_init |
88 | call UDP_init |
88 | call UDP_init |
89 | call ICMP_init |
89 | call ICMP_init |
90 | call socket_init |
90 | call socket_init |
91 | 91 | ||
92 | mov al, 0x0 ; set up 1s timer |
92 | mov al, 0x0 ; set up 1s timer |
93 | out 0x70, al |
93 | out 0x70, al |
94 | in al, 0x71 |
94 | in al, 0x71 |
95 | mov [last_1sTick], al |
95 | mov [last_1sTick], al |
96 | 96 | ||
97 | ret |
97 | ret |
98 | 98 | ||
99 | 99 | ||
100 | 100 | ||
101 | ;----------------------------------------------- |
101 | ;----------------------------------------------- |
102 | ; |
102 | ; |
103 | ; stack_handler |
103 | ; stack_handler |
104 | ; |
104 | ; |
105 | ; This function calls all network init procedures |
105 | ; This function calls all network init procedures |
106 | ; |
106 | ; |
107 | ; IN: / |
107 | ; IN: / |
108 | ; OUT: / |
108 | ; OUT: / |
109 | ; |
109 | ; |
110 | ;----------------------------------------------- |
110 | ;----------------------------------------------- |
111 | 111 | ||
112 | align 4 |
112 | align 4 |
113 | stack_handler: |
113 | stack_handler: |
114 | 114 | ||
115 | cmp [ETH_RUNNING], 0 |
115 | cmp [ETH_RUNNING], 0 |
116 | je .exit |
116 | je .exit |
117 | 117 | ||
118 | call ETH_handler ; handle all queued ethernet packets |
118 | call ETH_handler ; handle all queued ethernet packets |
119 | call ETH_send_queued |
119 | call ETH_send_queued |
120 | 120 | ||
121 | ; Test for 10ms tick, call tcp timer |
121 | ; Test for 10ms tick, call tcp timer |
122 | mov eax, [timer_ticks] |
122 | mov eax, [timer_ticks] |
123 | cmp eax, [last_1hsTick] |
123 | cmp eax, [last_1hsTick] |
124 | je .exit |
124 | je .exit |
125 | 125 | ||
126 | mov [last_1hsTick], eax |
126 | mov [last_1hsTick], eax |
127 | ; call tcp_tx_handler |
127 | ; call tcp_tx_handler |
128 | 128 | ||
129 | .sec_tick: |
129 | .sec_tick: |
130 | 130 | ||
131 | ; Test for 1 second event, call 1s timer functions |
131 | ; Test for 1 second event, call 1s timer functions |
132 | mov al, 0x0 ;second |
132 | mov al, 0x0 ;second |
133 | out 0x70, al |
133 | out 0x70, al |
134 | in al, 0x71 |
134 | in al, 0x71 |
135 | cmp al, [last_1sTick] |
135 | cmp al, [last_1sTick] |
136 | je .exit |
136 | je .exit |
137 | 137 | ||
138 | mov [last_1sTick], al |
138 | mov [last_1sTick], al |
139 | 139 | ||
140 | call ARP_decrease_entry_ttls |
140 | call ARP_decrease_entry_ttls |
141 | call IPv4_decrease_fragment_ttls |
141 | call IPv4_decrease_fragment_ttls |
142 | ; call tcp_tcb_handler |
142 | ; call tcp_tcb_handler |
143 | 143 | ||
144 | .exit: |
144 | .exit: |
145 | ret |
145 | ret |
146 | 146 | ||
147 | 147 | ||
148 | 148 | ||
149 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
149 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
150 | ;; Checksum [by Johnny_B] |
150 | ;; Checksum [by Johnny_B] |
151 | ;; IN: |
151 | ;; IN: |
152 | ;; buf_ptr=POINTER to buffer |
152 | ;; buf_ptr=POINTER to buffer |
153 | ;; buf_size=SIZE of buffer |
153 | ;; buf_size=SIZE of buffer |
154 | ;; OUT: |
154 | ;; OUT: |
155 | ;; AX=16-bit checksum |
155 | ;; AX=16-bit checksum |
156 | ;; Saves all used registers |
156 | ;; Saves all used registers |
157 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
157 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
158 | proc checksum_jb stdcall uses ebx esi ecx,\ |
158 | proc checksum_jb stdcall uses ebx esi ecx,\ |
159 | buf_ptr:DWORD, buf_size:DWORD |
159 | buf_ptr:DWORD, buf_size:DWORD |
160 | 160 | ||
161 | xor eax, eax |
161 | xor eax, eax |
162 | xor ebx, ebx ;accumulator |
162 | xor ebx, ebx ;accumulator |
163 | mov esi, dword[buf_ptr] |
163 | mov esi, dword[buf_ptr] |
164 | mov ecx, dword[buf_size] |
164 | mov ecx, dword[buf_size] |
165 | shr ecx, 1 ; ecx=ecx/2 |
165 | shr ecx, 1 ; ecx=ecx/2 |
166 | jnc @f ; if CF==0 then size is even number |
166 | jnc @f ; if CF==0 then size is even number |
167 | mov bh, byte[esi + ecx*2] |
167 | mov bh, byte[esi + ecx*2] |
168 | @@: |
168 | @@: |
169 | cld |
169 | cld |
170 | 170 | ||
171 | .loop: |
171 | .loop: |
172 | lodsw ;eax=word[esi],esi=esi+2 |
172 | lodsw ;eax=word[esi],esi=esi+2 |
173 | xchg ah,al ;cause must be a net byte-order |
173 | xchg ah,al ;cause must be a net byte-order |
174 | add ebx, eax |
174 | add ebx, eax |
175 | loop .loop |
175 | loop .loop |
176 | 176 | ||
177 | mov eax, ebx |
177 | mov eax, ebx |
178 | shr eax, 16 |
178 | shr eax, 16 |
179 | add ax, bx |
179 | add ax, bx |
180 | not ax |
180 | not ax |
181 | 181 | ||
182 | ret |
182 | ret |
183 | endp |
183 | endp |
184 | 184 | ||
185 | 185 | ||
186 | 186 | ||
187 | ;---------------------------------------------------------------- |
187 | ;---------------------------------------------------------------- |
188 | ; |
188 | ; |
189 | ; System function to work with network devices (73) |
189 | ; System function to work with network devices (73) |
190 | ; |
190 | ; |
191 | ;---------------------------------------------------------------- |
191 | ;---------------------------------------------------------------- |
192 | 192 | ||
193 | align 4 |
193 | align 4 |
194 | sys_network: |
194 | sys_network: |
195 | 195 | ||
196 | cmp ebx, -1 |
196 | cmp ebx, -1 |
197 | jne @f |
197 | jne @f |
198 | 198 | ||
199 | mov eax, [ETH_RUNNING] |
199 | mov eax, [ETH_RUNNING] |
200 | jmp .return |
200 | jmp .return |
201 | 201 | ||
202 | @@: |
202 | @@: |
203 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
203 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
204 | jge .doesnt_exist |
204 | jge .doesnt_exist |
205 | 205 | ||
206 | mov esi, ebx |
206 | mov esi, ebx |
207 | and esi, 0x0000ff00 |
207 | and esi, 0x0000ff00 |
208 | shr esi, 6 |
208 | shr esi, 6 |
209 | 209 | ||
210 | cmp dword [esi + ETH_DRV_LIST], 0 ; check if driver is running |
210 | cmp dword [esi + ETH_DRV_LIST], 0 ; check if driver is running |
211 | je .doesnt_exist |
211 | je .doesnt_exist |
212 | 212 | ||
213 | test bl, bl ; 0 = Get device type (ethernet/token ring/...) |
213 | test bl, bl ; 0 = Get device type (ethernet/token ring/...) |
214 | jnz @f |
214 | jnz @f |
215 | ; todo |
215 | ; todo |
216 | xor eax, eax |
216 | xor eax, eax |
217 | jmp .return |
217 | jmp .return |
218 | 218 | ||
219 | 219 | ||
220 | @@: |
220 | @@: |
221 | dec bl ; 1 = Get device name |
221 | dec bl ; 1 = Get device name |
222 | jnz @f |
222 | jnz @f |
223 | 223 | ||
224 | mov esi, [esi + ETH_DRV_LIST] |
224 | mov esi, [esi + ETH_DRV_LIST] |
225 | mov esi, [esi + ETH_DEVICE.name] |
225 | mov esi, [esi + ETH_DEVICE.name] |
226 | mov edi, ecx |
226 | mov edi, ecx |
227 | 227 | ||
228 | mov ecx, 64 ; max length |
228 | mov ecx, 64 ; max length |
229 | repnz movsb |
229 | repnz movsb |
230 | 230 | ||
231 | xor eax, eax |
231 | xor eax, eax |
232 | jmp .return |
232 | jmp .return |
233 | 233 | ||
234 | @@: |
234 | @@: |
235 | 235 | ||
236 | dec bl ; 2 = Reset the device |
236 | dec bl ; 2 = Reset the device |
237 | jnz @f |
237 | jnz @f |
238 | 238 | ||
239 | mov esi, [esi + ETH_DRV_LIST] |
239 | mov esi, [esi + ETH_DRV_LIST] |
240 | call [esi + ETH_DEVICE.reset] |
240 | call [esi + ETH_DEVICE.reset] |
241 | jmp .return |
241 | jmp .return |
242 | 242 | ||
243 | @@: |
243 | @@: |
244 | 244 | ||
245 | dec bl ; 3 = Stop driver for this device |
245 | dec bl ; 3 = Stop driver for this device |
246 | jnz @f |
246 | jnz @f |
247 | 247 | ||
248 | mov esi, [esi + ETH_DRV_LIST] |
248 | mov esi, [esi + ETH_DRV_LIST] |
249 | call [esi + ETH_DEVICE.unload] |
249 | call [esi + ETH_DEVICE.unload] |
250 | jmp .return |
250 | jmp .return |
251 | 251 | ||
252 | @@: |
252 | @@: |
253 | 253 | ||
254 | .doesnt_exist: |
254 | .doesnt_exist: |
255 | DEBUGF 1,"sys_network: invalid device/function specified!\n" |
255 | DEBUGF 1,"sys_network: invalid device/function specified!\n" |
256 | mov eax, -1 |
256 | mov eax, -1 |
257 | 257 | ||
258 | .return: |
258 | .return: |
259 | mov [esp+28+4], eax |
259 | mov [esp+28+4], eax |
260 | ret |
260 | ret |
261 | 261 | ||
262 | 262 | ||
263 | ;---------------------------------------------------------------- |
263 | ;---------------------------------------------------------------- |
264 | ; |
264 | ; |
265 | ; System Function To work with Protocols (75) |
265 | ; System Function To work with Protocols (75) |
266 | ; |
266 | ; |
267 | ;---------------------------------------------------------------- |
267 | ;---------------------------------------------------------------- |
268 | 268 | ||
269 | align 4 |
269 | align 4 |
270 | sys_protocols: |
270 | sys_protocols: |
271 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
271 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
272 | jge .doesnt_exist |
272 | jge .doesnt_exist |
273 | 273 | ||
274 | mov esi, ebx |
274 | mov esi, ebx |
275 | and esi, 0x0000ff00 |
275 | and esi, 0x0000ff00 |
276 | shr esi, 6 |
276 | shr esi, 6 |
277 | cmp dword [esi + ETH_DRV_LIST], 0 ; check if driver is running TODO: check other lists too |
277 | cmp dword [esi + ETH_DRV_LIST], 0 ; check if driver is running TODO: check other lists too |
278 | je .doesnt_exist |
278 | je .doesnt_exist |
279 | 279 | ||
280 | push .return ; return address (we will be using jumps instead of calls) |
280 | push .return ; return address (we will be using jumps instead of calls) |
281 | 281 | ||
282 | mov eax, ebx ; set ax to protocol number |
282 | mov eax, ebx ; set ax to protocol number |
283 | shr eax, 16 ; |
283 | shr eax, 16 ; |
284 | 284 | ||
285 | cmp ax , IP_PROTO_IP |
285 | cmp ax , IP_PROTO_IP |
286 | je IPv4_API |
286 | je IPv4_API |
287 | 287 | ||
288 | cmp ax , IP_PROTO_ICMP |
288 | cmp ax , IP_PROTO_ICMP |
289 | je ICMP_API |
289 | je ICMP_API |
290 | 290 | ||
291 | cmp ax , IP_PROTO_UDP |
291 | cmp ax , IP_PROTO_UDP |
292 | je UDP_API |
292 | je UDP_API |
293 | 293 | ||
294 | cmp ax , IP_PROTO_TCP |
294 | cmp ax , IP_PROTO_TCP |
295 | ; je TCP_API |
295 | ; je TCP_API |
296 | 296 | ||
297 | cmp ax , ETHER_ARP |
297 | cmp ax , ETHER_ARP |
298 | je ARP_API |
298 | je ARP_API |
299 | 299 | ||
300 | cmp ax , ETHER |
300 | cmp ax , ETHER |
301 | je ETH_API |
301 | je ETH_API |
302 | 302 | ||
303 | add esp, 4 ; if we reached here, no function was called, so we need to balance stack |
303 | add esp, 4 ; if we reached here, no function was called, so we need to balance stack |
304 | 304 | ||
305 | .doesnt_exist: |
305 | .doesnt_exist: |
306 | DEBUGF 1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh |
306 | DEBUGF 1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh |
307 | mov eax, -1 |
307 | mov eax, -1 |
308 | 308 | ||
309 | .return: |
309 | .return: |
310 | mov [esp+28+4], eax |
310 | mov [esp+28+4], eax |
311 | ret |
311 | ret |