Subversion Repositories Kolibri OS

Rev

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

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