Subversion Repositories Kolibri OS

Rev

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

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