Subversion Repositories Kolibri OS

Rev

Rev 1773 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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