Subversion Repositories Kolibri OS

Rev

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

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