Rev 7679 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
9017 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;; |
3545 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; ETHERNET.INC ;; |
||
7 | ;; ;; |
||
8 | ;; Ethernet network layer for KolibriOS ;; |
||
9 | ;; ;; |
||
10 | ;; Written by hidnplayr@kolibrios.org ;; |
||
11 | ;; ;; |
||
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
13 | ;; Version 2, June 1991 ;; |
||
14 | ;; ;; |
||
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
16 | |||
4850 | mario79 | 17 | $Revision: 9017 $ |
3545 | hidnplayr | 18 | |
19 | ETH_FRAME_MINIMUM = 60 |
||
3982 | hidnplayr | 20 | ETH_QUEUE_SIZE = 255 |
3545 | hidnplayr | 21 | |
22 | struct ETH_header |
||
23 | |||
24 | DstMAC dp ? ; destination MAC-address |
||
25 | SrcMAC dp ? ; source MAC-address |
||
26 | Type dw ? ; type of the upper-layer protocol |
||
27 | |||
28 | ends |
||
29 | |||
30 | struct ETH_DEVICE NET_DEVICE |
||
31 | |||
32 | mac dp ? |
||
33 | |||
34 | ends |
||
35 | |||
5522 | hidnplayr | 36 | iglobal |
37 | align 4 |
||
3982 | hidnplayr | 38 | |
5522 | hidnplayr | 39 | ETH_BROADCAST dp 0xffffffffffff |
3982 | hidnplayr | 40 | |
5522 | hidnplayr | 41 | ETH_frame_queued dd 0 ; Number of queued frames |
3982 | hidnplayr | 42 | |
5522 | hidnplayr | 43 | ETH_frame_head dd ETH_frame_head ; Pointer to next frame in the linked list |
44 | ETH_frame_tail dd ETH_frame_head ; Pointer to last frame in the linked list |
||
3545 | hidnplayr | 45 | |
46 | endg |
||
47 | |||
3982 | hidnplayr | 48 | uglobal |
49 | align 4 |
||
50 | ETH_input_event dd ? |
||
51 | endg |
||
52 | |||
6011 | hidnplayr | 53 | macro eth_init { |
3982 | hidnplayr | 54 | |
55 | movi ebx, 1 |
||
6011 | hidnplayr | 56 | mov ecx, eth_process_input |
3982 | hidnplayr | 57 | call new_sys_threads |
58 | test eax, eax |
||
59 | jns @f |
||
60 | DEBUGF DEBUG_NETWORK_ERROR,'K : cannot create kernel thread for ethernet, error %d\n', eax |
||
61 | @@: |
||
62 | |||
63 | } |
||
64 | |||
6011 | hidnplayr | 65 | ;-----------------------------------------------------------------; |
66 | ; ; |
||
67 | ; eth_input: This function is called by ethernet drivers. ; |
||
68 | ; Push the received ethernet packet onto the ethernet input queue.; |
||
69 | ; ; |
||
70 | ; IN: [esp] = Pointer to buffer ; |
||
71 | ; ; |
||
72 | ; OUT: / ; |
||
73 | ; ; |
||
74 | ;-----------------------------------------------------------------; |
||
3545 | hidnplayr | 75 | align 4 |
6011 | hidnplayr | 76 | eth_input: |
3545 | hidnplayr | 77 | |
5522 | hidnplayr | 78 | pop eax |
3982 | hidnplayr | 79 | |
9017 | hidnplayr | 80 | if defined NETWORK_SANITY_CHECKS |
81 | cmp eax, [net_buffs_low] |
||
82 | jb .assert_mbuff |
||
83 | cmp eax, [net_buffs_high] |
||
84 | ja .assert_mbuff |
||
85 | test eax, 0x7ff |
||
86 | jnz .assert_mbuff |
||
87 | end if |
||
88 | |||
89 | spin_lock_irqsave |
||
90 | |||
5522 | hidnplayr | 91 | cmp [ETH_frame_queued], ETH_QUEUE_SIZE |
92 | jae .full |
||
93 | inc [ETH_frame_queued] |
||
3982 | hidnplayr | 94 | |
5522 | hidnplayr | 95 | ; Add frame to the end of the linked list |
96 | mov [eax + NET_BUFF.NextPtr], ETH_frame_head |
||
97 | |||
98 | mov ebx, [ETH_frame_tail] |
||
99 | mov [eax + NET_BUFF.PrevPtr], ebx |
||
100 | |||
101 | mov [ETH_frame_tail], eax |
||
102 | mov [ebx + NET_BUFF.NextPtr], eax |
||
103 | |||
9017 | hidnplayr | 104 | spin_unlock_irqrestore |
5522 | hidnplayr | 105 | |
7536 | hidnplayr | 106 | ; Mark it as being an Ethernet Frame |
107 | mov [eax + NET_BUFF.type], NET_BUFF_ETH |
||
108 | |||
5522 | hidnplayr | 109 | ; Now queue an event to process it |
3982 | hidnplayr | 110 | xor edx, edx |
111 | mov eax, [ETH_input_event] |
||
112 | mov ebx, [eax + EVENT.id] |
||
113 | xor esi, esi |
||
114 | call raise_event |
||
115 | |||
116 | ret |
||
117 | |||
5522 | hidnplayr | 118 | .full: |
9017 | hidnplayr | 119 | mov ebx, [eax + NET_BUFF.device] |
120 | inc [ebx + NET_DEVICE.packets_rx_ovr] |
||
121 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH incoming queue is full, discarding packet!\n" |
||
122 | spin_unlock_irqrestore |
||
123 | stdcall net_buff_free, eax |
||
3982 | hidnplayr | 124 | ret |
125 | |||
9017 | hidnplayr | 126 | if defined NETWORK_SANITY_CHECKS |
127 | .assert_mbuff: |
||
128 | DEBUGF DEBUG_NETWORK_ERROR, "eth_input: invalid buffer 0x%x\n", eax |
||
129 | DEBUGF DEBUG_NETWORK_ERROR, "eth_input: caller=0x%x\n", [esp+4] |
||
130 | xor eax, eax |
||
131 | ret |
||
132 | end if |
||
3982 | hidnplayr | 133 | |
134 | |||
9017 | hidnplayr | 135 | |
6011 | hidnplayr | 136 | ;-----------------------------------------------------------------; |
137 | ; ; |
||
138 | ; eth_process_input: Process packets from ethernet input queue. ; |
||
139 | ; ; |
||
140 | ; IN: / ; |
||
141 | ; ; |
||
142 | ; OUT: / ; |
||
143 | ; ; |
||
144 | ;-----------------------------------------------------------------; |
||
3982 | hidnplayr | 145 | align 4 |
6011 | hidnplayr | 146 | eth_process_input: |
3982 | hidnplayr | 147 | |
148 | xor esi, esi |
||
149 | mov ecx, MANUAL_DESTROY |
||
150 | call create_event |
||
151 | mov [ETH_input_event], eax |
||
5522 | hidnplayr | 152 | pushf |
3982 | hidnplayr | 153 | .wait: |
5522 | hidnplayr | 154 | popf |
3982 | hidnplayr | 155 | mov eax, [ETH_input_event] |
156 | mov ebx, [eax + EVENT.id] |
||
157 | call wait_event |
||
158 | |||
159 | .loop: |
||
5522 | hidnplayr | 160 | pushf |
161 | cli |
||
162 | cmp [ETH_frame_queued], 0 |
||
163 | je .wait |
||
3982 | hidnplayr | 164 | |
5522 | hidnplayr | 165 | dec [ETH_frame_queued] |
3982 | hidnplayr | 166 | |
5522 | hidnplayr | 167 | mov esi, [ETH_frame_head] |
168 | mov ebx, [esi + NET_BUFF.NextPtr] |
||
3982 | hidnplayr | 169 | |
5522 | hidnplayr | 170 | mov [ETH_frame_head], ebx |
171 | mov [ebx + NET_BUFF.PrevPtr], ETH_frame_head |
||
172 | |||
173 | popf |
||
174 | |||
175 | mov eax, [esi + NET_BUFF.offset] |
||
176 | add eax, esi |
||
177 | mov ecx, [esi + NET_BUFF.length] |
||
178 | mov ebx, [esi + NET_BUFF.device] |
||
179 | |||
180 | pushd .loop ; return address for protocol handler |
||
181 | push esi ; keep pointer to NET_BUFF on stack |
||
182 | |||
3982 | hidnplayr | 183 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: size=%u\n", ecx |
3643 | hidnplayr | 184 | sub ecx, sizeof.ETH_header |
9017 | hidnplayr | 185 | jb .err |
3545 | hidnplayr | 186 | |
5522 | hidnplayr | 187 | ; Set registers for protocol handlers |
3545 | hidnplayr | 188 | lea edx, [eax + sizeof.ETH_header] |
189 | mov ax, [eax + ETH_header.Type] |
||
190 | |||
5522 | hidnplayr | 191 | ; Place protocol handlers here |
3600 | hidnplayr | 192 | cmp ax, ETHER_PROTO_IPv4 |
6011 | hidnplayr | 193 | je ipv4_input |
3545 | hidnplayr | 194 | |
3601 | hidnplayr | 195 | cmp ax, ETHER_PROTO_ARP |
6011 | hidnplayr | 196 | je arp_input |
3545 | hidnplayr | 197 | |
5001 | hidnplayr | 198 | ; cmp ax, ETHER_PROTO_IPv6 |
6011 | hidnplayr | 199 | ; je ipv6_input |
3545 | hidnplayr | 200 | |
5001 | hidnplayr | 201 | ; cmp ax, ETHER_PROTO_PPP_DISCOVERY |
6011 | hidnplayr | 202 | ; je pppoe_discovery_input |
3545 | hidnplayr | 203 | |
5001 | hidnplayr | 204 | ; cmp ax, ETHER_PROTO_PPP_SESSION |
6011 | hidnplayr | 205 | ; je pppoe_session_input |
3545 | hidnplayr | 206 | |
4256 | hidnplayr | 207 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: Unknown packet type=%x\n", ax |
3545 | hidnplayr | 208 | |
9017 | hidnplayr | 209 | .drop: |
210 | mov eax, [esp] |
||
211 | mov eax, [eax + NET_BUFF.device] |
||
212 | inc [eax + NET_DEVICE.packets_rx_drop] |
||
213 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: dropping\n" |
||
6011 | hidnplayr | 214 | call net_buff_free |
3545 | hidnplayr | 215 | ret |
216 | |||
9017 | hidnplayr | 217 | .err: |
218 | mov eax, [esp] |
||
219 | mov eax, [eax + NET_BUFF.device] |
||
220 | inc [eax + NET_DEVICE.packets_rx_err] |
||
221 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: invalid frame received\n" |
||
222 | call net_buff_free |
||
223 | ret |
||
6011 | hidnplayr | 224 | |
225 | |||
9017 | hidnplayr | 226 | |
227 | |||
6011 | hidnplayr | 228 | ;-----------------------------------------------------------------; |
229 | ; ; |
||
230 | ; eth_output ; |
||
231 | ; ; |
||
232 | ; IN: ax = protocol ; |
||
233 | ; ebx = device ptr ; |
||
234 | ; ecx = payload size ; |
||
235 | ; edx = pointer to destination mac ; |
||
236 | ; ; |
||
237 | ; OUT: eax = start of net frame / 0 on error ; |
||
238 | ; ebx = device ptr ; |
||
239 | ; ecx = payload size ; |
||
240 | ; edi = start of payload ; |
||
241 | ; ; |
||
242 | ;-----------------------------------------------------------------; |
||
3545 | hidnplayr | 243 | align 4 |
6011 | hidnplayr | 244 | eth_output: |
3545 | hidnplayr | 245 | |
3556 | hidnplayr | 246 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: size=%u device=%x\n", ecx, ebx |
3545 | hidnplayr | 247 | |
5015 | hidnplayr | 248 | cmp ecx, [ebx + ETH_DEVICE.mtu] |
9017 | hidnplayr | 249 | ja .too_large |
3545 | hidnplayr | 250 | |
251 | push ecx |
||
5015 | hidnplayr | 252 | push ax edx |
3545 | hidnplayr | 253 | |
5522 | hidnplayr | 254 | add ecx, sizeof.ETH_header + NET_BUFF.data |
6011 | hidnplayr | 255 | stdcall net_buff_alloc, ecx |
3545 | hidnplayr | 256 | test eax, eax |
257 | jz .out_of_ram |
||
9017 | hidnplayr | 258 | |
5522 | hidnplayr | 259 | mov [eax + NET_BUFF.type], NET_BUFF_ETH |
260 | mov [eax + NET_BUFF.device], ebx |
||
261 | mov [eax + NET_BUFF.offset], NET_BUFF.data |
||
262 | lea edi, [eax + NET_BUFF.data] |
||
3545 | hidnplayr | 263 | |
264 | pop esi |
||
265 | movsd |
||
266 | movsw |
||
5015 | hidnplayr | 267 | lea esi, [ebx + ETH_DEVICE.mac] |
3545 | hidnplayr | 268 | movsd |
269 | movsw |
||
270 | pop ax |
||
271 | stosw |
||
272 | |||
5522 | hidnplayr | 273 | lea eax, [edi - sizeof.ETH_header - NET_BUFF.data] ; Set eax to buffer start |
3545 | hidnplayr | 274 | pop ecx |
5522 | hidnplayr | 275 | |
3545 | hidnplayr | 276 | lea edx, [ecx + sizeof.ETH_header] ; Set edx to complete buffer size |
277 | cmp edx, ETH_FRAME_MINIMUM |
||
278 | jbe .adjust_size |
||
279 | .done: |
||
5522 | hidnplayr | 280 | mov [eax + NET_BUFF.length], edx |
3556 | hidnplayr | 281 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: ptr=%x size=%u\n", eax, edx |
3545 | hidnplayr | 282 | ret |
283 | |||
284 | .adjust_size: |
||
285 | mov edx, ETH_FRAME_MINIMUM |
||
5015 | hidnplayr | 286 | test edx, edx ; clear zero flag |
3545 | hidnplayr | 287 | jmp .done |
288 | |||
289 | .out_of_ram: |
||
9017 | hidnplayr | 290 | inc [ebx + NET_DEVICE.packets_tx_drop] |
291 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: Out of ram!\n" |
||
292 | add esp, 4+2 |
||
293 | pop ecx |
||
5015 | hidnplayr | 294 | xor eax, eax |
3545 | hidnplayr | 295 | ret |
296 | |||
9017 | hidnplayr | 297 | .too_large: |
298 | inc [eax + NET_DEVICE.packets_tx_err] |
||
299 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: Packet too large!\n" |
||
5015 | hidnplayr | 300 | xor eax, eax |
3545 | hidnplayr | 301 | ret |
302 | |||
303 | |||
304 | |||
6011 | hidnplayr | 305 | ;-----------------------------------------------------------------; |
306 | ; ; |
||
307 | ; eth_api: Part of system function 76. ; |
||
308 | ; ; |
||
309 | ; IN: bl = subfunction number ; |
||
310 | ; bh = device number ; |
||
311 | ; ecx, edx, .. depends on subfunction ; |
||
312 | ; ; |
||
313 | ; OUT: depends on subfunction ; |
||
314 | ; ; |
||
315 | ;-----------------------------------------------------------------; |
||
3545 | hidnplayr | 316 | align 4 |
6011 | hidnplayr | 317 | eth_api: |
3545 | hidnplayr | 318 | |
3600 | hidnplayr | 319 | cmp bh, NET_DEVICES_MAX |
3545 | hidnplayr | 320 | ja .error |
321 | movzx eax, bh |
||
7679 | hidnplayr | 322 | mov eax, dword [net_device_list + 4*eax] |
3600 | hidnplayr | 323 | cmp [eax + NET_DEVICE.device_type], NET_DEVICE_ETH |
3545 | hidnplayr | 324 | jne .error |
325 | |||
326 | and ebx, 0xff |
||
327 | cmp ebx, .number |
||
328 | ja .error |
||
329 | jmp dword [.table + 4*ebx] |
||
330 | |||
331 | .table: |
||
3601 | hidnplayr | 332 | dd .read_mac ; 0 |
3545 | hidnplayr | 333 | .number = ($ - .table) / 4 - 1 |
334 | |||
335 | .error: |
||
336 | or eax, -1 |
||
337 | ret |
||
338 | |||
339 | |||
340 | .read_mac: |
||
341 | movzx ebx, word [eax + ETH_DEVICE.mac] |
||
342 | mov eax, dword [eax + ETH_DEVICE.mac + 2] |
||
5522 | hidnplayr | 343 | mov [esp+20+4], ebx ; FIXME |
3545 | hidnplayr | 344 | ret |
345 |