Subversion Repositories Kolibri OS

Rev

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

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