Subversion Repositories Kolibri OS

Rev

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

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