Subversion Repositories Kolibri OS

Rev

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

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