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