Subversion Repositories Kolibri OS

Rev

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

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