Subversion Repositories Kolibri OS

Rev

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

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