Subversion Repositories Kolibri OS

Rev

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