Subversion Repositories Kolibri OS

Rev

Rev 1482 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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