Subversion Repositories Kolibri OS

Rev

Rev 1733 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1733 Rev 1763
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2010. All rights reserved.    ;;
3
;; Copyright (C) KolibriOS team 2004-2011. 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
;;  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
;;     Some parts of code are based on the work of:                ;;
-
 
13
;;      Mike Hibbett (menuetos network stack)                      ;;
-
 
14
;;      Eugen Brasoveanu (solar os network stack and drivers)      ;;
-
 
15
;;      mike.dld (kolibrios socket code)                           ;;
13
;;     but also Paolo Franchetti                                   ;;
16
;;                                                                 ;;
-
 
17
;;     TCP part is based on 4.4BSD                                 ;;
14
;;                                                                 ;;
18
;;                                                                 ;;
15
;;          GNU GENERAL PUBLIC LICENSE                             ;;
19
;;          GNU GENERAL PUBLIC LICENSE                             ;;
16
;;             Version 2, June 1991                                ;;
20
;;             Version 2, June 1991                                ;;
17
;;                                                                 ;;
21
;;                                                                 ;;
18
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19
 
23
 
20
$Revision: 1733 $
-
 
21
 
-
 
22
__DEBUG_LEVEL_OLD__  equ __DEBUG_LEVEL__
24
$Revision: 1763 $
-
 
25
 
23
 
26
__DEBUG_LEVEL_OLD__	equ __DEBUG_LEVEL__	; use seperate debug level for network part of kernel
24
__DEBUG_LEVEL__ equ 1		; this sets the debug level for network part of kernel
27
__DEBUG_LEVEL__ 	equ 1
25
 
28
 
26
uglobal
29
uglobal
27
	net_10ms       dd ?
30
	net_10ms	dd ?
28
	net_tmr_count  dw ?
31
	net_tmr_count	dw ?
29
endg
32
endg
30
 
33
 
31
MAX_NET_DEVICES 	equ 16
34
MAX_NET_DEVICES 	equ 16
32
 
35
 
33
MIN_EPHEMERAL_PORT equ 49152
36
MIN_EPHEMERAL_PORT	equ 49152
34
MAX_EPHEMERAL_PORT equ 61000
37
MAX_EPHEMERAL_PORT	equ 61000
35
 
38
 
36
; Ethernet protocol numbers
39
; Ethernet protocol numbers
37
ETHER_ARP		equ 0x0608
40
ETHER_ARP		equ 0x0608
38
ETHER_IPv4		equ 0x0008
41
ETHER_IPv4		equ 0x0008
39
ETHER_PPP_DISCOVERY	equ 0x6388
42
ETHER_PPP_DISCOVERY	equ 0x6388
40
ETHER_PPP_SESSION	equ 0x6488
43
ETHER_PPP_SESSION	equ 0x6488
41
 
44
 
42
;Protocol family
45
;Protocol family
43
AF_UNSPEC	equ 0
46
AF_UNSPEC	equ 0
44
AF_UNIX 	equ 1
47
AF_UNIX 	equ 1
45
AF_INET4	equ 2
48
AF_INET4	equ 2
46
;AF_AX25         equ 3
-
 
47
;AF_IPX          equ 4
-
 
48
;AF_APPLETALK    equ 5
-
 
49
;AF_NETROM       equ 6
-
 
50
;AF_BRIDGE       equ 7
-
 
51
;AF_AAL5         equ 8
-
 
52
;AF_X25          equ 9
-
 
53
AF_INET6	equ 10
49
AF_INET6	equ 10
54
 
50
 
55
; Internet protocol numbers
51
; Internet protocol numbers
56
IP_PROTO_IP	equ 0
52
IP_PROTO_IP	equ 0
57
IP_PROTO_ICMP	equ 1
53
IP_PROTO_ICMP	equ 1
58
IP_PROTO_TCP	equ 6
54
IP_PROTO_TCP	equ 6
59
IP_PROTO_UDP	equ 17
55
IP_PROTO_UDP	equ 17
60
 
56
 
61
; Socket types
57
; Socket types
62
SOCK_STREAM	equ 1
58
SOCK_STREAM	equ 1
63
SOCK_DGRAM	equ 2
59
SOCK_DGRAM	equ 2
64
SOCK_RAW	equ 3
60
SOCK_RAW	equ 3
65
 
61
 
66
; Socket options
62
; Socket options
67
SO_ACCEPTCON	equ 1
63
SO_ACCEPTCON	equ 1
68
 
64
 
69
SOCKET_MAXDATA	equ 4096*32	; must be 4096*(power of 2) where 'power of 2' is at least 8
65
SOCKET_MAXDATA	equ 4096*32	; must be 4096*(power of 2) where 'power of 2' is at least 8
70
 
66
 
71
; Network driver types
67
; Network driver types
72
NET_TYPE_ETH	equ 1
68
NET_TYPE_ETH	equ 1
73
NET_TYPE_SLIP	equ 2
69
NET_TYPE_SLIP	equ 2
74
 
70
 
75
MAX_backlog	equ 20		; maximum backlog for stream sockets
71
MAX_backlog	equ 20		; maximum backlog for stream sockets
76
 
72
 
77
; Error Codes
73
; Error Codes
78
ENOBUFS 	equ 55
74
ENOBUFS 	equ 55
79
ECONNREFUSED	equ 61
75
ECONNREFUSED	equ 61
80
ECONNRESET	equ 52
76
ECONNRESET	equ 52
81
ETIMEDOUT	equ 60
77
ETIMEDOUT	equ 60
82
ECONNABORTED	equ 53
78
ECONNABORTED	equ 53
83
 
79
 
84
 
80
 
85
 
81
 
86
virtual at 0
82
virtual at 0
87
 
83
 
88
	NET_DEVICE:
84
	NET_DEVICE:
89
 
85
 
90
	.type		dd ?	; Type field
86
	.type		dd ?	; Type field
91
	.mtu		dd ?	; Maximal Transmission Unit
87
	.mtu		dd ?	; Maximal Transmission Unit
92
	.name		dd ?	; Ptr to 0 terminated string
88
	.name		dd ?	; Ptr to 0 terminated string
93
 
89
 
94
	.unload 	dd ?	; Ptrs to driver functions
90
	.unload 	dd ?	; Ptrs to driver functions
95
	.reset		dd ?	;
91
	.reset		dd ?	;
96
	.transmit	dd ?	;
92
	.transmit	dd ?	;
97
 
93
 
98
	.bytes_tx	dq ?	; Statistics, updated by the driver
94
	.bytes_tx	dq ?	; Statistics, updated by the driver
99
	.bytes_rx	dq ?	;
95
	.bytes_rx	dq ?	;
100
	.packets_tx	dd ?	;
96
	.packets_tx	dd ?	;
101
	.packets_rx	dd ?	;
97
	.packets_rx	dd ?	;
102
 
98
 
103
;       .chksum         dd ?    ; bitmask stating available checksum routines on hardware
99
;       .hwacc          dd ?    ; bitmask stating available hardware accelerations (offload engines)
104
 
100
 
105
	.end:
101
	.end:
106
 
102
 
107
end virtual
103
end virtual
108
 
104
 
109
 
105
 
110
; Exactly as it says..
106
; Exactly as it says..
111
macro pseudo_random reg {
107
macro pseudo_random reg {
112
	add	reg, [esp]
108
	add	reg, [esp]
113
	rol	reg, 5
109
	rol	reg, 5
114
	xor	reg, [timer_ticks]
110
	xor	reg, [timer_ticks]
115
	add	reg, [CPU_FREQ]
111
	add	reg, [CPU_FREQ]
116
	imul	reg, 214013
112
	imul	reg, 214013
117
	xor	reg, 0xdeadbeef
113
	xor	reg, 0xdeadbeef
118
	rol	reg, 9
114
	rol	reg, 9
119
}
115
}
120
 
116
 
121
macro ntohd reg {
117
macro ntohd reg {
122
 
118
 
123
	rol	word reg, 8
119
	rol	word reg, 8
124
	rol	dword reg, 16
120
	rol	dword reg, 16
125
	rol	word reg , 8
121
	rol	word reg , 8
126
 
122
 
127
}
123
}
128
 
124
 
129
macro ntohw reg {
125
macro ntohw reg {
130
 
126
 
131
	rol	word reg, 8
127
	rol	word reg, 8
132
 
128
 
133
}
129
}
134
 
130
 
135
 
131
 
136
macro packet_to_debug { 	; set esi to packet you want to print, ecx to number of bytes
132
macro packet_to_debug { 	; set esi to packet you want to print, ecx to number of bytes
137
 
133
 
138
local	.loop
134
local	.loop
139
 
135
 
140
  .loop:
136
  .loop:
141
	lodsb
137
	lodsb
142
	DEBUGF	1,"%x ", eax:2
138
	DEBUGF	1,"%x ", eax:2
143
	loop	@r
139
	loop	@r
144
 
140
 
145
}
141
}
146
 
142
 
147
 
143
 
148
include "queue.inc"
144
include "queue.inc"
149
 
145
 
150
include "ethernet.inc"
146
include "ethernet.inc"
151
 
147
 
152
;include "slip.inc"
148
;include "slip.inc"
153
;include "pppoe.inc"
149
;include "pppoe.inc"
154
 
150
 
155
include "ARP.inc"
151
include "ARP.inc"
156
include "IPv4.inc"
152
include "IPv4.inc"
157
 
153
 
158
include "icmp.inc"
154
include "icmp.inc"
159
include "udp.inc"
155
include "udp.inc"
160
include "tcp.inc"
156
include "tcp.inc"
161
 
157
 
162
include "socket.inc"
158
include "socket.inc"
163
 
159
 
164
 
160
 
165
 
161
 
166
align 4
162
align 4
167
uglobal
163
uglobal
168
 
164
 
169
	NET_RUNNING	dd  ?
165
	NET_RUNNING	dd  ?
170
	NET_DRV_LIST	rd  MAX_NET_DEVICES
166
	NET_DRV_LIST	rd  MAX_NET_DEVICES
171
 
167
 
172
endg
168
endg
173
 
169
 
174
 
170
 
175
;-----------------------------------------------------------------
171
;-----------------------------------------------------------------
176
;
172
;
177
; stack_init
173
; stack_init
178
;
174
;
179
;  This function calls all network init procedures
175
;  This function calls all network init procedures
180
;
176
;
181
;  IN:  /
177
;  IN:  /
182
;  OUT: /
178
;  OUT: /
183
;
179
;
184
;-----------------------------------------------------------------
180
;-----------------------------------------------------------------
185
align 4
181
align 4
186
stack_init:
182
stack_init:
187
 
183
 
188
; Init the network drivers list
184
; Init the network drivers list
189
	xor	eax, eax
185
	xor	eax, eax
190
	mov	edi, NET_RUNNING
186
	mov	edi, NET_RUNNING
191
	mov	ecx, MAX_NET_DEVICES + 1
187
	mov	ecx, MAX_NET_DEVICES + 1
192
	rep	stosd
188
	rep	stosd
193
 
189
 
194
	ETH_init
190
	ETH_init
195
;        SLIP_init
191
;        SLIP_init
196
;        PPPOE_init
192
;        PPPOE_init
197
 
193
 
198
	IPv4_init
194
	IPv4_init
199
	ICMP_init
195
	ICMP_init
200
 
196
 
201
	ARP_init
197
	ARP_init
202
	UDP_init
198
	UDP_init
203
	TCP_init
199
	TCP_init
204
 
200
 
205
	SOCKET_init
201
	SOCKET_init
206
 
202
 
207
	mov	[net_tmr_count], 0
203
	mov	[net_tmr_count], 0
208
 
204
 
209
	ret
205
	ret
210
 
206
 
211
 
207
 
212
;-----------------------------------------------------------------
208
;-----------------------------------------------------------------
213
;
209
;
214
; stack_handler
210
; stack_handler
215
;
211
;
216
;  This function is called in kernel loop
212
;  This function is called in kernel loop
217
;
213
;
218
;  IN:  /
214
;  IN:  /
219
;  OUT: /
215
;  OUT: /
220
;
216
;
221
;-----------------------------------------------------------------
217
;-----------------------------------------------------------------
222
align 4
218
align 4
223
stack_handler:
219
stack_handler:
224
 
220
 
225
	cmp	[NET_RUNNING], 0
221
	cmp	[NET_RUNNING], 0
226
	je	.exit
222
	je	.exit
227
 
223
 
228
	; Test for 10ms tick
224
	; Test for 10ms tick
229
	mov	eax, [timer_ticks]
225
	mov	eax, [timer_ticks]
230
	cmp	eax, [net_10ms]
226
	cmp	eax, [net_10ms]
231
	je	.exit
227
	je	.exit
232
	mov	[net_10ms], eax
228
	mov	[net_10ms], eax
233
 
229
 
234
	test	[net_10ms], 0x0f	; 160ms
230
	test	[net_10ms], 0x0f	; 160ms
235
	jnz	.exit
231
	jnz	.exit
236
 
232
 
237
	TCP_timer_160ms
233
	TCP_timer_160ms
238
 
234
 
239
	test	[net_10ms], 0x3f	; 640ms
235
	test	[net_10ms], 0x3f	; 640ms
240
	jnz	.exit
236
	jnz	.exit
241
 
237
 
242
	TCP_timer_640ms
238
	TCP_timer_640ms
243
	ARP_decrease_entry_ttls
239
	ARP_decrease_entry_ttls
244
	IPv4_decrease_fragment_ttls
240
	IPv4_decrease_fragment_ttls
245
 
241
 
246
  .exit:
242
  .exit:
247
	ret
243
	ret
248
 
244
 
249
 
245
 
250
 
246
 
251
;-----------------------------------------------------------------
247
;-----------------------------------------------------------------
252
;
248
;
253
; NET_Add_Device:
249
; NET_Add_Device:
254
;
250
;
255
;  This function is called by the network drivers,
251
;  This function is called by the network drivers,
256
;  to register each running NIC to the kernel
252
;  to register each running NIC to the kernel
257
;
253
;
258
;  IN:  Pointer to device structure in ebx
254
;  IN:  Pointer to device structure in ebx
259
;  OUT: Device num in eax, -1 on error
255
;  OUT: Device num in eax, -1 on error
260
;
256
;
261
;-----------------------------------------------------------------
257
;-----------------------------------------------------------------
262
align 4
258
align 4
263
NET_add_device:
259
NET_add_device:
264
 
260
 
265
	DEBUGF	1,"NET_Add_Device: %x\n", ebx
261
	DEBUGF	1,"NET_Add_Device: %x\n", ebx
266
 
262
 
267
	mov	eax, [NET_RUNNING]
263
	mov	eax, [NET_RUNNING]
268
	cmp	eax, MAX_NET_DEVICES
264
	cmp	eax, MAX_NET_DEVICES
269
	jge	.error
265
	jge	.error
270
 
266
 
271
;----------------------------------
267
;----------------------------------
272
; Check if device is already listed
268
; Check if device is already listed
273
	mov	eax, ebx
269
	mov	eax, ebx
274
	mov	ecx, MAX_NET_DEVICES	  ; We need to check whole list because a device may be removed without re-organizing list
270
	mov	ecx, MAX_NET_DEVICES	  ; We need to check whole list because a device may be removed without re-organizing list
275
	mov	edi, NET_DRV_LIST
271
	mov	edi, NET_DRV_LIST
276
 
272
 
277
	repne	scasd			  ; See if device is already in the list
273
	repne	scasd			  ; See if device is already in the list
278
	jz	.error
274
	jz	.error
279
 
275
 
280
;----------------------------
276
;----------------------------
281
; Find empty slot in the list
277
; Find empty slot in the list
282
	xor	eax, eax
278
	xor	eax, eax
283
	mov	ecx, MAX_NET_DEVICES
279
	mov	ecx, MAX_NET_DEVICES
284
	mov	edi, NET_DRV_LIST
280
	mov	edi, NET_DRV_LIST
285
 
281
 
286
	repne	scasd
282
	repne	scasd
287
	jnz	.error
283
	jnz	.error
288
 
284
 
289
	sub	edi, 4
285
	sub	edi, 4
290
 
286
 
291
	cmp	[ebx + NET_DEVICE.type], NET_TYPE_ETH
287
	cmp	[ebx + NET_DEVICE.type], NET_TYPE_ETH
292
	je	.ethernet
288
	je	.ethernet
293
 
289
 
294
	cmp	[ebx + NET_DEVICE.type], NET_TYPE_SLIP
290
	cmp	[ebx + NET_DEVICE.type], NET_TYPE_SLIP
295
	je	.slip
291
	je	.slip
296
 
292
 
297
	DEBUGF	1,"Unknown network device type: %u\n", [ebx + NET_DEVICE.type]
293
	DEBUGF	1,"Unknown network device type: %u\n", [ebx + NET_DEVICE.type]
298
	jmp	.error
294
	jmp	.error
299
 
295
 
300
  .ethernet:
296
  .ethernet:
301
	DEBUGF	1,"Trying to add an ethernet device\n"
297
	DEBUGF	1,"Trying to add an ethernet device\n"
302
 
298
 
303
	inc	[ETH_RUNNING]		  ; Indicate that one more ethernet device is up and running
299
	inc	[ETH_RUNNING]		  ; Indicate that one more ethernet device is up and running
304
	jmp	.add_it
300
	jmp	.add_it
305
 
301
 
306
  .slip:
302
  .slip:
307
	DEBUGF	1,"Trying to add a slip device\n"
303
	DEBUGF	1,"Trying to add a slip device\n"
308
	;;;;
304
	;;;;
309
	jmp	.error
305
	jmp	.error
310
 
306
 
311
 
307
 
312
  .add_it:
308
  .add_it:
313
 
309
 
314
;-----------------------------
310
;-----------------------------
315
; Add device to the found slot
311
; Add device to the found slot
316
	mov	[edi], ebx		  ; add device to list
312
	mov	[edi], ebx		  ; add device to list
317
 
313
 
318
	sub	edi, NET_DRV_LIST	  ; Calculate device number in eax
314
	sub	edi, NET_DRV_LIST	  ; Calculate device number in eax
319
	mov	eax, edi		  ;
315
	mov	eax, edi		  ;
320
	shr	eax, 2
316
	shr	eax, 2
321
 
317
 
322
	inc	[NET_RUNNING]		  ; Indicate that one more network device is up and running
318
	inc	[NET_RUNNING]		  ; Indicate that one more network device is up and running
323
 
319
 
324
	DEBUGF	1,"Device number: %u\n",eax
320
	DEBUGF	1,"Device number: %u\n",eax
325
	ret
321
	ret
326
 
322
 
327
  .error:
323
  .error:
328
	or	eax, -1
324
	or	eax, -1
329
	DEBUGF	2,"Adding network device failed\n"
325
	DEBUGF	2,"Adding network device failed\n"
330
	ret
326
	ret
331
 
327
 
332
 
328
 
333
 
329
 
334
;-----------------------------------------------------------------
330
;-----------------------------------------------------------------
335
;
331
;
336
; NET_Remove_Device:
332
; NET_Remove_Device:
337
;
333
;
338
;  This function is called by etwork drivers,
334
;  This function is called by etwork drivers,
339
;  to unregister network devices from the kernel
335
;  to unregister network devices from the kernel
340
;
336
;
341
;  IN:  Pointer to device structure in ebx
337
;  IN:  Pointer to device structure in ebx
342
;  OUT: eax: -1 on error
338
;  OUT: eax: -1 on error
343
;
339
;
344
;-----------------------------------------------------------------
340
;-----------------------------------------------------------------
345
align 4
341
align 4
346
NET_remove_device:
342
NET_remove_device:
347
 
343
 
348
	cmp	[NET_RUNNING], 0
344
	cmp	[NET_RUNNING], 0
349
	je	.error
345
	je	.error
350
 
346
 
351
;----------------------------
347
;----------------------------
352
; Find the driver in the list
348
; Find the driver in the list
353
 
349
 
354
	mov	eax, ebx
350
	mov	eax, ebx
355
	mov	ecx, MAX_NET_DEVICES
351
	mov	ecx, MAX_NET_DEVICES
356
	mov	edi, NET_DRV_LIST
352
	mov	edi, NET_DRV_LIST
357
 
353
 
358
	repne	scasd
354
	repne	scasd
359
	jnz	.error
355
	jnz	.error
360
 
356
 
361
;------------------------
357
;------------------------
362
; Remove it from the list
358
; Remove it from the list
363
 
359
 
364
	xor	eax, eax
360
	xor	eax, eax
365
	mov	dword [edi-4], eax
361
	mov	dword [edi-4], eax
366
 
362
 
367
	dec	[NET_RUNNING]
363
	dec	[NET_RUNNING]
368
	ret
364
	ret
369
 
365
 
370
  .error:
366
  .error:
371
	or	eax, -1
367
	or	eax, -1
372
	ret
368
	ret
373
 
369
 
374
 
370
 
375
 
371
 
376
;-----------------------------------------------------------------
372
;-----------------------------------------------------------------
377
;
373
;
378
; NET_ptr_to_num
374
; NET_ptr_to_num
379
;
375
;
380
; IN:  ebx = ptr to device struct
376
; IN:  ebx = ptr to device struct
381
; OUT: edi = -1 on error, device number otherwise
377
; OUT: edi = -1 on error, device number otherwise
382
;
378
;
383
;-----------------------------------------------------------------
379
;-----------------------------------------------------------------
384
align 4
380
align 4
385
NET_ptr_to_num:
381
NET_ptr_to_num:
386
	push	ecx
382
	push	ecx
387
 
383
 
388
	mov	ecx, MAX_NET_DEVICES
384
	mov	ecx, MAX_NET_DEVICES
389
	mov	edi, NET_DRV_LIST
385
	mov	edi, NET_DRV_LIST
390
 
386
 
391
  .loop:
387
  .loop:
392
	cmp	ebx, [edi]
388
	cmp	ebx, [edi]
393
	jz	.found
389
	jz	.found
394
	add	edi, 4
390
	add	edi, 4
395
	dec	ecx
391
	dec	ecx
396
	jnz	.loop
392
	jnz	.loop
397
 
393
 
398
	; repnz  scasd could work too if eax is used instead of ebx!
394
	; repnz  scasd could work too if eax is used instead of ebx!
399
 
395
 
400
	or	edi, -1
396
	or	edi, -1
401
 
397
 
402
	pop	ecx
398
	pop	ecx
403
	ret
399
	ret
404
 
400
 
405
  .found:
401
  .found:
406
	sub	edi, NET_DRV_LIST
402
	sub	edi, NET_DRV_LIST
407
	shr	edi, 2
403
	shr	edi, 2
408
 
404
 
409
	pop	ecx
405
	pop	ecx
410
	ret
406
	ret
411
 
407
 
412
;-----------------------------------------------------------------
408
;-----------------------------------------------------------------
413
;
409
;
414
; checksum_1
410
; checksum_1
415
;
411
;
416
;  This is the first of two functions needed to calculate a checksum.
412
;  This is the first of two functions needed to calculate a checksum.
417
;
413
;
418
;  IN:  edx = start offset for semi-checksum
414
;  IN:  edx = start offset for semi-checksum
419
;       esi = pointer to data
415
;       esi = pointer to data
420
;       ecx = data size
416
;       ecx = data size
421
;  OUT: edx = semi-checksum
417
;  OUT: edx = semi-checksum
422
;
418
;
423
;
419
;
424
; Code was optimized by diamond
420
; Code was optimized by diamond
425
;
421
;
426
;-----------------------------------------------------------------
422
;-----------------------------------------------------------------
427
align 4
423
align 4
428
checksum_1:
424
checksum_1:
429
 
425
 
430
	shr	ecx, 1
426
	shr	ecx, 1
431
	pushf
427
	pushf
432
	jz	.no_2
428
	jz	.no_2
433
 
429
 
434
	shr	ecx, 1
430
	shr	ecx, 1
435
	pushf
431
	pushf
436
	jz	.no_4
432
	jz	.no_4
437
 
433
 
438
	shr	ecx, 1
434
	shr	ecx, 1
439
	pushf
435
	pushf
440
	jz	.no_8
436
	jz	.no_8
441
 
437
 
442
  .loop:
438
  .loop:
443
	add	dl, [esi+1]
439
	add	dl, [esi+1]
444
	adc	dh, [esi+0]
440
	adc	dh, [esi+0]
445
 
441
 
446
	adc	dl, [esi+3]
442
	adc	dl, [esi+3]
447
	adc	dh, [esi+2]
443
	adc	dh, [esi+2]
448
 
444
 
449
	adc	dl, [esi+5]
445
	adc	dl, [esi+5]
450
	adc	dh, [esi+4]
446
	adc	dh, [esi+4]
451
 
447
 
452
	adc	dl, [esi+7]
448
	adc	dl, [esi+7]
453
	adc	dh, [esi+6]
449
	adc	dh, [esi+6]
454
 
450
 
455
	adc	edx, 0
451
	adc	edx, 0
456
	add	esi, 8
452
	add	esi, 8
457
 
453
 
458
	dec	ecx
454
	dec	ecx
459
	jnz	.loop
455
	jnz	.loop
460
 
456
 
461
	adc	edx, 0
457
	adc	edx, 0
462
 
458
 
463
  .no_8:
459
  .no_8:
464
	popf
460
	popf
465
	jnc	.no_4
461
	jnc	.no_4
466
 
462
 
467
	add	dl, [esi+1]
463
	add	dl, [esi+1]
468
	adc	dh, [esi+0]
464
	adc	dh, [esi+0]
469
 
465
 
470
	adc	dl, [esi+3]
466
	adc	dl, [esi+3]
471
	adc	dh, [esi+2]
467
	adc	dh, [esi+2]
472
 
468
 
473
	adc	edx, 0
469
	adc	edx, 0
474
	add	esi, 4
470
	add	esi, 4
475
 
471
 
476
  .no_4:
472
  .no_4:
477
	popf
473
	popf
478
	jnc	.no_2
474
	jnc	.no_2
479
 
475
 
480
	add	dl, [esi+1]
476
	add	dl, [esi+1]
481
	adc	dh, [esi+0]
477
	adc	dh, [esi+0]
482
 
478
 
483
	adc	edx, 0
479
	adc	edx, 0
484
	inc	esi
480
	inc	esi
485
	inc	esi
481
	inc	esi
486
 
482
 
487
  .no_2:
483
  .no_2:
488
	popf
484
	popf
489
	jnc	.end
485
	jnc	.end
490
 
486
 
491
	add	dh, [esi+0]
487
	add	dh, [esi+0]
492
	adc	edx, 0
488
	adc	edx, 0
493
  .end:
489
  .end:
494
	ret
490
	ret
495
 
491
 
496
;-----------------------------------------------------------------
492
;-----------------------------------------------------------------
497
;
493
;
498
; checksum_2
494
; checksum_2
499
;
495
;
500
;  This function calculates the final ip/tcp/udp checksum for you
496
;  This function calculates the final ip/tcp/udp checksum for you
501
;
497
;
502
;  IN:  edx = semi-checksum
498
;  IN:  edx = semi-checksum
503
;  OUT: dx = checksum (in INET byte order)
499
;  OUT: dx = checksum (in INET byte order)
504
;
500
;
505
;-----------------------------------------------------------------
501
;-----------------------------------------------------------------
506
align 4
502
align 4
507
checksum_2:
503
checksum_2:
508
 
504
 
509
	mov	ecx, edx
505
	mov	ecx, edx
510
	shr	ecx, 16
506
	shr	ecx, 16
511
	and	edx, 0xffff
507
	and	edx, 0xffff
512
	add	edx, ecx
508
	add	edx, ecx
513
 
509
 
514
	mov	ecx, edx
510
	mov	ecx, edx
515
	shr	ecx, 16
511
	shr	ecx, 16
516
	add	dx, cx
512
	add	dx, cx
517
	test	dx, dx		; it seems that ZF is not set when CF is set :(
513
	test	dx, dx		; it seems that ZF is not set when CF is set :(
518
	not	dx
514
	not	dx
519
	jnz	.not_zero
515
	jnz	.not_zero
520
	dec	dx
516
	dec	dx
521
  .not_zero:
517
  .not_zero:
522
	xchg	dl, dh
518
	xchg	dl, dh
523
 
519
 
524
	DEBUGF 1,"Checksum: %x\n", dx
520
	DEBUGF 1,"Checksum: %x\n", dx
525
 
521
 
526
	ret
522
	ret
527
 
523
 
528
 
524
 
529
 
525
 
530
;----------------------------------------------------------------
526
;----------------------------------------------------------------
531
;
527
;
532
;  System function to work with network devices (73)
528
;  System function to work with network devices (73)
533
;
529
;
534
;----------------------------------------------------------------
530
;----------------------------------------------------------------
535
align 4
531
align 4
536
sys_network:
532
sys_network:
537
 
533
 
538
	cmp	ebx, -1
534
	cmp	ebx, -1
539
	jne	@f
535
	jne	@f
540
 
536
 
541
	mov	eax, [NET_RUNNING]
537
	mov	eax, [NET_RUNNING]
542
	jmp	.return
538
	jmp	.return
543
 
539
 
544
   @@:
540
   @@:
545
	cmp	bh, MAX_NET_DEVICES		 ; Check if device number exists
541
	cmp	bh, MAX_NET_DEVICES		 ; Check if device number exists
546
	jge	.doesnt_exist
542
	jge	.doesnt_exist
547
 
543
 
548
	mov	esi, ebx
544
	mov	esi, ebx
549
	and	esi, 0x0000ff00
545
	and	esi, 0x0000ff00
550
	shr	esi, 6
546
	shr	esi, 6
551
 
547
 
552
	cmp	dword [esi + NET_DRV_LIST], 0 ; check if driver is running
548
	cmp	dword [esi + NET_DRV_LIST], 0 ; check if driver is running
553
	je	.doesnt_exist
549
	je	.doesnt_exist
554
 
550
 
555
	test	bl, bl			 ; 0 = Get device type (ethernet/token ring/...)
551
	test	bl, bl			 ; 0 = Get device type (ethernet/token ring/...)
556
	jnz	@f
552
	jnz	@f
557
 
553
 
558
	xor	eax, eax
554
	xor	eax, eax
559
	jmp	.return
555
	jmp	.return
560
 
556
 
561
 
557
 
562
  @@:
558
  @@:
563
	dec	bl			; 1 = Get device name
559
	dec	bl			; 1 = Get device name
564
	jnz	@f
560
	jnz	@f
565
 
561
 
566
	mov	esi, [esi + NET_DRV_LIST]
562
	mov	esi, [esi + NET_DRV_LIST]
567
	mov	esi, [esi + NET_DEVICE.name]
563
	mov	esi, [esi + NET_DEVICE.name]
568
	mov	edi, ecx
564
	mov	edi, ecx
569
 
565
 
570
	mov	ecx, 64 ; max length
566
	mov	ecx, 64 ; max length
571
	repnz	movsb
567
	repnz	movsb
572
 
568
 
573
	xor	eax, eax
569
	xor	eax, eax
574
	jmp	.return
570
	jmp	.return
575
 
571
 
576
  @@:
572
  @@:
577
 
573
 
578
	dec	bl			; 2 = Reset the device
574
	dec	bl			; 2 = Reset the device
579
	jnz	@f
575
	jnz	@f
580
 
576
 
581
	mov	esi, [esi + NET_DRV_LIST]
577
	mov	esi, [esi + NET_DRV_LIST]
582
	call	[esi + NET_DEVICE.reset]
578
	call	[esi + NET_DEVICE.reset]
583
	jmp	.return
579
	jmp	.return
584
 
580
 
585
  @@:
581
  @@:
586
 
582
 
587
	dec	bl			; 3 = Stop driver for this device
583
	dec	bl			; 3 = Stop driver for this device
588
	jnz	@f
584
	jnz	@f
589
 
585
 
590
	mov	esi, [esi + NET_DRV_LIST]
586
	mov	esi, [esi + NET_DRV_LIST]
591
	call	[esi + NET_DEVICE.unload]
587
	call	[esi + NET_DEVICE.unload]
592
	jmp	.return
588
	jmp	.return
593
 
589
 
594
  @@:
590
  @@:
595
	dec	bl			; 4 = Get driver pointer
591
	dec	bl			; 4 = Get driver pointer
596
	jnz	@f
592
	jnz	@f
597
 
593
 
598
	; ..;
594
	; ..;
599
 
595
 
600
 
596
 
601
  @@:
597
  @@:
602
;  ...                                   ; 5 Get driver name
598
;  ...                                   ; 5 Get driver name
603
 
599
 
604
  .doesnt_exist:
600
  .doesnt_exist:
605
	DEBUGF	1,"sys_network: invalid device/function specified!\n"
601
	DEBUGF	1,"sys_network: invalid device/function specified!\n"
606
	mov	eax, -1
602
	mov	eax, -1
607
 
603
 
608
  .return:
604
  .return:
609
	mov	[esp+28+4], eax
605
	mov	[esp+28+4], eax
610
	ret
606
	ret
611
 
607
 
612
 
608
 
613
;----------------------------------------------------------------
609
;----------------------------------------------------------------
614
;
610
;
615
;  System function to work with protocols  (75)
611
;  System function to work with protocols  (75)
616
;
612
;
617
;----------------------------------------------------------------
613
;----------------------------------------------------------------
618
align 4
614
align 4
619
sys_protocols:
615
sys_protocols:
620
	cmp	bh, MAX_NET_DEVICES		; Check if device number exists
616
	cmp	bh, MAX_NET_DEVICES		; Check if device number exists
621
	jge	.doesnt_exist
617
	jge	.doesnt_exist
622
 
618
 
623
	mov	esi, ebx
619
	mov	esi, ebx
624
	and	esi, 0x0000ff00
620
	and	esi, 0x0000ff00
625
	shr	esi, 6				; now we have the device num * 4 in esi
621
	shr	esi, 6				; now we have the device num * 4 in esi
626
	cmp	dword [esi + NET_DRV_LIST], 0	; check if driver is running
622
	cmp	dword [esi + NET_DRV_LIST], 0	; check if driver is running
627
	je	.doesnt_exist
623
	je	.doesnt_exist
628
 
624
 
629
	push	.return 			; return address (we will be using jumps instead of calls)
625
	push	.return 			; return address (we will be using jumps instead of calls)
630
 
626
 
631
	mov	eax, ebx			; set ax to protocol number
627
	mov	eax, ebx			; set ax to protocol number
632
	shr	eax, 16 			;
628
	shr	eax, 16 			;
633
 
629
 
634
	cmp	ax , IP_PROTO_IP
630
	cmp	ax , IP_PROTO_IP
635
	je	IPv4_API
631
	je	IPv4_API
636
 
632
 
637
	cmp	ax , IP_PROTO_ICMP
633
	cmp	ax , IP_PROTO_ICMP
638
	je	ICMP_API
634
	je	ICMP_API
639
 
635
 
640
	cmp	ax , IP_PROTO_UDP
636
	cmp	ax , IP_PROTO_UDP
641
	je	UDP_API
637
	je	UDP_API
642
 
638
 
643
	cmp	ax , IP_PROTO_TCP
639
	cmp	ax , IP_PROTO_TCP
644
	je	TCP_API
640
	je	TCP_API
645
 
641
 
646
	cmp	ax , ETHER_ARP
642
	cmp	ax , ETHER_ARP
647
	je	ARP_API
643
	je	ARP_API
648
 
644
 
649
	cmp	ax , 1337  ;;;;;
645
	cmp	ax , 1337  ;;;;;
650
	je	ETH_API
646
	je	ETH_API
651
 
647
 
652
	add	esp, 4				 ; if we reached here, no function was called, so we need to balance stack
648
	add	esp, 4				 ; if we reached here, no function was called, so we need to balance stack
653
 
649
 
654
  .doesnt_exist:
650
  .doesnt_exist:
655
	DEBUGF	1,"sys_protocols: protocol %u doesnt exist on device %u!\n", ax, bh
651
	DEBUGF	1,"sys_protocols: protocol %u doesnt exist on device %u!\n", ax, bh
656
	mov	eax, -1
652
	mov	eax, -1
657
 
653
 
658
  .return:
654
  .return:
659
	mov	[esp+28+4], eax
655
	mov	[esp+28+4], eax
660
	ret
656
	ret
661
 
657
 
662
 
658
 
663
__DEBUG_LEVEL__ equ __DEBUG_LEVEL_OLD__
659
__DEBUG_LEVEL__ equ __DEBUG_LEVEL_OLD__