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