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