Rev 1256 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1171 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
||
4 | ;; Distributed under terms of the GNU General Public License ;; |
||
5 | ;; ;; |
||
6 | ;; STACK.INC ;; |
||
7 | ;; ;; |
||
8 | ;; BASIC TCP/IP stack for KolibriOS ;; |
||
9 | ;; ;; |
||
10 | ;; Written by hidnplayr@kolibrios.org ;; |
||
11 | ;; ;; |
||
12 | ;; based on the work of Mike Hibbett, mikeh@oceanfree.net ;; |
||
13 | ;; but also Paolo Franchetti ;; |
||
14 | ;; ;; |
||
15 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
16 | ;; Version 2, June 1991 ;; |
||
17 | ;; ;; |
||
18 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
1159 | hidnplayr | 19 | |
1206 | hidnplayr | 20 | $Revision: 1257 $ |
1159 | hidnplayr | 21 | |
22 | uglobal |
||
23 | last_1sTick db ? |
||
24 | last_1hsTick dd ? |
||
25 | endg |
||
26 | |||
27 | MAX_NET_DEVICES equ 16 |
||
28 | |||
1185 | hidnplayr | 29 | MIN_EPHEMERAL_PORT equ 49152 |
30 | MAX_EPHEMERAL_PORT equ 61000 |
||
1159 | hidnplayr | 31 | |
1257 | hidnplayr | 32 | ETHER equ 1337 ; TODO: find another value for this (how does it work in posix ?) |
1185 | hidnplayr | 33 | ETHER_ARP equ 0x0608 |
34 | |||
1254 | hidnplayr | 35 | AF_UNSPEC equ 0 |
1249 | hidnplayr | 36 | AF_UNIX equ 1 |
1159 | hidnplayr | 37 | AF_INET4 equ 2 |
38 | ;AF_AX25 equ 3 |
||
39 | ;AF_IPX equ 4 |
||
40 | ;AF_APPLETALK equ 5 |
||
41 | ;AF_NETROM equ 6 |
||
42 | ;AF_BRIDGE equ 7 |
||
43 | ;AF_AAL5 equ 8 |
||
44 | ;AF_X25 equ 9 |
||
45 | ;AF_INET6 equ 10 |
||
46 | ;AF_MAX equ 12 |
||
47 | |||
48 | IP_PROTO_IP equ 0 |
||
49 | IP_PROTO_ICMP equ 1 |
||
50 | IP_PROTO_TCP equ 6 |
||
51 | IP_PROTO_UDP equ 17 |
||
52 | |||
1200 | hidnplayr | 53 | ; Socket types |
54 | SOCK_STREAM = 1 |
||
55 | SOCK_DGRAM = 2 |
||
56 | SOCK_RAW = 3 |
||
57 | |||
1254 | hidnplayr | 58 | TCB_LISTEN equ 1 |
59 | TCB_SYN_SENT equ 2 |
||
60 | TCB_SYN_RECEIVED equ 3 |
||
61 | TCB_ESTABLISHED equ 4 |
||
62 | TCB_FIN_WAIT_1 equ 5 |
||
63 | TCB_FIN_WAIT_2 equ 6 |
||
64 | TCB_CLOSE_WAIT equ 7 |
||
65 | TCB_CLOSING equ 8 |
||
66 | TCB_LAST_ACK equ 9 |
||
67 | TCB_TIMED_WAIT equ 10 |
||
68 | TCB_CLOSED equ 11 |
||
1159 | hidnplayr | 69 | |
1254 | hidnplayr | 70 | TH_FIN equ 1 shl 0 |
71 | TH_SYN equ 1 shl 1 |
||
72 | TH_RST equ 1 shl 2 |
||
73 | TH_PUSH equ 1 shl 3 |
||
74 | TH_ACK equ 1 shl 4 |
||
75 | TH_URG equ 1 shl 5 |
||
76 | |||
77 | |||
78 | macro inc_INET reg { |
||
79 | |||
1256 | clevermous | 80 | add byte [reg + 3], 1 |
1254 | hidnplayr | 81 | adc byte [reg + 2], 0 |
82 | adc byte [reg + 1], 0 |
||
1256 | clevermous | 83 | adc byte [reg], 0 |
1254 | hidnplayr | 84 | |
85 | } |
||
86 | |||
87 | |||
88 | macro add_INET reg { |
||
1256 | clevermous | 89 | add byte [reg + 3], cl |
90 | adc byte [reg + 2], ch |
||
91 | adc byte [reg + 1], 0 |
||
92 | adc byte [reg], 0 |
||
1254 | hidnplayr | 93 | rol ecx, 16 |
1256 | clevermous | 94 | add byte [reg + 1], cl |
95 | adc byte [reg], ch |
||
1254 | hidnplayr | 96 | rol ecx, 16 |
97 | } |
||
98 | |||
1159 | hidnplayr | 99 | include "queue.inc" |
1187 | hidnplayr | 100 | include "ARP.inc" |
101 | include "IPv4.inc" |
||
1159 | hidnplayr | 102 | include "ethernet.inc" |
103 | include "socket.inc" |
||
1249 | hidnplayr | 104 | include "tcp.inc" |
1185 | hidnplayr | 105 | include "udp.inc" |
106 | include "icmp.inc" |
||
1159 | hidnplayr | 107 | |
1257 | hidnplayr | 108 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 109 | ; |
110 | ; stack_init |
||
111 | ; |
||
112 | ; This function calls all network init procedures |
||
113 | ; |
||
114 | ; IN: / |
||
115 | ; OUT: / |
||
116 | ; |
||
1257 | hidnplayr | 117 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 118 | align 4 |
119 | stack_init: |
||
120 | |||
121 | call ETH_init |
||
122 | call IPv4_init |
||
123 | call ARP_init |
||
124 | call UDP_init |
||
1249 | hidnplayr | 125 | call TCP_init |
1159 | hidnplayr | 126 | call ICMP_init |
127 | call socket_init |
||
128 | |||
1254 | hidnplayr | 129 | mov al, 0 ; set up 1s timer |
1159 | hidnplayr | 130 | out 0x70, al |
131 | in al, 0x71 |
||
132 | mov [last_1sTick], al |
||
133 | |||
134 | ret |
||
135 | |||
136 | |||
137 | |||
1257 | hidnplayr | 138 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 139 | ; |
140 | ; stack_handler |
||
141 | ; |
||
142 | ; This function calls all network init procedures |
||
143 | ; |
||
144 | ; IN: / |
||
145 | ; OUT: / |
||
146 | ; |
||
1257 | hidnplayr | 147 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 148 | align 4 |
149 | stack_handler: |
||
150 | |||
1257 | hidnplayr | 151 | cmp [ETH_RUNNING], 0 |
152 | je .exit |
||
1159 | hidnplayr | 153 | |
1257 | hidnplayr | 154 | ; Test for 1/100 s (10ms) tick |
155 | mov eax, [timer_ticks] |
||
156 | cmp eax, [last_1hsTick] |
||
157 | je .exit |
||
1159 | hidnplayr | 158 | |
1257 | hidnplayr | 159 | mov [last_1hsTick], eax |
1159 | hidnplayr | 160 | |
1257 | hidnplayr | 161 | call ETH_handler ; handle all queued ethernet packets |
162 | call ETH_send_queued |
||
163 | call TCP_send_queued |
||
1249 | hidnplayr | 164 | |
1159 | hidnplayr | 165 | .sec_tick: |
166 | |||
1257 | hidnplayr | 167 | ; Test for 1 second tick |
168 | mov al, 0 |
||
169 | out 0x70, al |
||
170 | in al, 0x71 |
||
171 | cmp al, [last_1sTick] |
||
172 | je .exit |
||
1159 | hidnplayr | 173 | |
1257 | hidnplayr | 174 | mov [last_1sTick], al |
1159 | hidnplayr | 175 | |
1257 | hidnplayr | 176 | call ARP_decrease_entry_ttls |
177 | call IPv4_decrease_fragment_ttls |
||
178 | call TCP_decrease_socket_ttls |
||
1159 | hidnplayr | 179 | |
180 | .exit: |
||
1257 | hidnplayr | 181 | ret |
1159 | hidnplayr | 182 | |
183 | |||
1249 | hidnplayr | 184 | ;----------------------------------------------------------------- |
185 | ; |
||
186 | ; checksum_1 |
||
187 | ; |
||
188 | ; This is the first of two functions needed to calculate the TCP checksum. |
||
189 | ; |
||
190 | ; IN: edx = start offeset for semi-checksum |
||
191 | ; esi = pointer to data |
||
192 | ; ecx = data size |
||
193 | ; OUT: edx = semi-checksum |
||
194 | ; |
||
195 | ;----------------------------------------------------------------- |
||
196 | align 4 |
||
197 | checksum_1: |
||
1159 | hidnplayr | 198 | |
1249 | hidnplayr | 199 | xor eax, eax |
200 | shr ecx, 1 |
||
201 | pushf |
||
202 | .loop: |
||
203 | lodsw |
||
204 | xchg al, ah |
||
205 | add edx, eax |
||
206 | loop .loop |
||
1159 | hidnplayr | 207 | |
1249 | hidnplayr | 208 | popf |
209 | jnc .end |
||
1159 | hidnplayr | 210 | |
1251 | clevermous | 211 | add dh, [esi] |
212 | adc edx, 0 |
||
1159 | hidnplayr | 213 | |
1249 | hidnplayr | 214 | .end: |
215 | |||
216 | ret |
||
217 | |||
218 | |||
219 | ;----------------------------------------------------------------- |
||
220 | ; |
||
221 | ; checksum_2 |
||
222 | ; |
||
223 | ; This function calculates the final ip/tcp/udp checksum for you |
||
224 | ; |
||
225 | ; IN: edx = semi-checksum |
||
226 | ; OUT: dx = checksum (in INET byte order) |
||
227 | ; |
||
228 | ;----------------------------------------------------------------- |
||
229 | align 4 |
||
230 | checksum_2: |
||
231 | |||
232 | mov ecx, edx |
||
233 | shr ecx, 16 |
||
234 | and edx, 0xffff |
||
235 | add edx, ecx |
||
236 | mov eax, edx |
||
237 | shr eax, 16 |
||
238 | add edx, eax |
||
239 | |||
240 | not dx |
||
241 | jnz .not_zero |
||
242 | dec dx |
||
243 | .not_zero: |
||
244 | xchg dl, dh |
||
245 | |||
246 | DEBUGF 1,"Checksum: %x\n",dx |
||
247 | |||
248 | ret |
||
249 | |||
250 | |||
251 | |||
1159 | hidnplayr | 252 | ;---------------------------------------------------------------- |
253 | ; |
||
1171 | hidnplayr | 254 | ; System function to work with network devices (73) |
1159 | hidnplayr | 255 | ; |
256 | ;---------------------------------------------------------------- |
||
257 | align 4 |
||
258 | sys_network: |
||
259 | |||
1196 | hidnplayr | 260 | cmp ebx, -1 |
261 | jne @f |
||
262 | |||
263 | mov eax, [ETH_RUNNING] |
||
264 | jmp .return |
||
265 | |||
266 | @@: |
||
1159 | hidnplayr | 267 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
268 | jge .doesnt_exist |
||
269 | |||
270 | mov esi, ebx |
||
271 | and esi, 0x0000ff00 |
||
272 | shr esi, 6 |
||
273 | |||
274 | cmp dword [esi + ETH_DRV_LIST], 0 ; check if driver is running |
||
275 | je .doesnt_exist |
||
276 | |||
277 | test bl, bl ; 0 = Get device type (ethernet/token ring/...) |
||
278 | jnz @f |
||
1196 | hidnplayr | 279 | ; todo |
1192 | hidnplayr | 280 | xor eax, eax |
281 | jmp .return |
||
1159 | hidnplayr | 282 | |
283 | |||
284 | @@: |
||
285 | dec bl ; 1 = Get device name |
||
286 | jnz @f |
||
287 | |||
288 | mov esi, [esi + ETH_DRV_LIST] |
||
289 | mov esi, [esi + ETH_DEVICE.name] |
||
290 | mov edi, ecx |
||
291 | |||
292 | mov ecx, 64 ; max length |
||
293 | repnz movsb |
||
294 | |||
1192 | hidnplayr | 295 | xor eax, eax |
296 | jmp .return |
||
1159 | hidnplayr | 297 | |
1192 | hidnplayr | 298 | @@: |
1159 | hidnplayr | 299 | |
1192 | hidnplayr | 300 | dec bl ; 2 = Reset the device |
301 | jnz @f |
||
302 | |||
303 | mov esi, [esi + ETH_DRV_LIST] |
||
304 | call [esi + ETH_DEVICE.reset] |
||
305 | jmp .return |
||
306 | |||
1159 | hidnplayr | 307 | @@: |
1192 | hidnplayr | 308 | |
309 | dec bl ; 3 = Stop driver for this device |
||
310 | jnz @f |
||
311 | |||
312 | mov esi, [esi + ETH_DRV_LIST] |
||
313 | call [esi + ETH_DEVICE.unload] |
||
314 | jmp .return |
||
315 | |||
316 | @@: |
||
1249 | hidnplayr | 317 | dec bl ; 4 = Get driver pointer |
318 | jnz @f |
||
1192 | hidnplayr | 319 | |
1249 | hidnplayr | 320 | ; ..; |
321 | |||
322 | |||
323 | @@: |
||
324 | ; ... ; 5 Get driver name |
||
325 | |||
1159 | hidnplayr | 326 | .doesnt_exist: |
327 | DEBUGF 1,"sys_network: invalid device/function specified!\n" |
||
328 | mov eax, -1 |
||
329 | |||
1192 | hidnplayr | 330 | .return: |
331 | mov [esp+28+4], eax |
||
1159 | hidnplayr | 332 | ret |
333 | |||
334 | |||
335 | ;---------------------------------------------------------------- |
||
336 | ; |
||
1171 | hidnplayr | 337 | ; System Function To work with Protocols (75) |
1159 | hidnplayr | 338 | ; |
339 | ;---------------------------------------------------------------- |
||
340 | align 4 |
||
341 | sys_protocols: |
||
342 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
||
343 | jge .doesnt_exist |
||
344 | |||
345 | mov esi, ebx |
||
346 | and esi, 0x0000ff00 |
||
347 | shr esi, 6 |
||
1171 | hidnplayr | 348 | cmp dword [esi + ETH_DRV_LIST], 0 ; check if driver is running TODO: check other lists too |
1159 | hidnplayr | 349 | je .doesnt_exist |
350 | |||
351 | push .return ; return address (we will be using jumps instead of calls) |
||
352 | |||
353 | mov eax, ebx ; set ax to protocol number |
||
354 | shr eax, 16 ; |
||
355 | |||
356 | cmp ax , IP_PROTO_IP |
||
357 | je IPv4_API |
||
358 | |||
359 | cmp ax , IP_PROTO_ICMP |
||
360 | je ICMP_API |
||
361 | |||
362 | cmp ax , IP_PROTO_UDP |
||
363 | je UDP_API |
||
364 | |||
1171 | hidnplayr | 365 | cmp ax , IP_PROTO_TCP |
1254 | hidnplayr | 366 | je TCP_API |
1159 | hidnplayr | 367 | |
1171 | hidnplayr | 368 | cmp ax , ETHER_ARP |
1159 | hidnplayr | 369 | je ARP_API |
370 | |||
1185 | hidnplayr | 371 | cmp ax , ETHER |
1159 | hidnplayr | 372 | je ETH_API |
373 | |||
1171 | hidnplayr | 374 | add esp, 4 ; if we reached here, no function was called, so we need to balance stack |
1159 | hidnplayr | 375 | |
376 | .doesnt_exist: |
||
1171 | hidnplayr | 377 | DEBUGF 1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh |
1159 | hidnplayr | 378 | mov eax, -1 |
379 | |||
380 | .return: |
||
1171 | hidnplayr | 381 | mov [esp+28+4], eax |
1257 | hidnplayr | 382 | ret |