Rev 3555 | Rev 3626 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3555 | Serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved. ;; |
||
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 | |||
17 | $Revision: 3346 $ |
||
18 | |||
19 | ETH_FRAME_MINIMUM = 60 |
||
20 | |||
21 | struct ETH_header |
||
22 | |||
23 | DstMAC dp ? ; destination MAC-address |
||
24 | SrcMAC dp ? ; source MAC-address |
||
25 | Type dw ? ; type of the upper-layer protocol |
||
26 | |||
27 | ends |
||
28 | |||
29 | struct ETH_DEVICE NET_DEVICE |
||
30 | |||
31 | mac dp ? |
||
32 | |||
33 | ends |
||
34 | |||
35 | align 4 |
||
36 | iglobal |
||
37 | |||
38 | ETH_BROADCAST dp 0xffffffffffff |
||
39 | endg |
||
40 | |||
41 | ;----------------------------------------------------------------- |
||
42 | ; |
||
43 | ; ETH_input |
||
44 | ; |
||
45 | ; This function is called by ethernet drivers, |
||
46 | ; It pushes the received ethernet packets onto the eth_in_queue |
||
47 | ; |
||
48 | ; IN: [esp] = Pointer to buffer |
||
49 | ; [esp+4] = size of buffer |
||
50 | ; ebx = pointer to eth_device |
||
51 | ; OUT: / |
||
52 | ; |
||
53 | ;----------------------------------------------------------------- |
||
54 | align 4 |
||
55 | ETH_input: |
||
56 | mov eax, [esp] |
||
57 | mov ecx, [esp+4] |
||
58 | |||
3589 | Serge | 59 | DEBUGF DEBUG_NETWORK_VERBOSE,"ETH_input: size=%u\n", ecx |
3555 | Serge | 60 | cmp ecx, ETH_FRAME_MINIMUM |
61 | jb .dump |
||
62 | sub ecx, sizeof.ETH_header |
||
63 | |||
64 | lea edx, [eax + sizeof.ETH_header] |
||
65 | mov ax, [eax + ETH_header.Type] |
||
66 | |||
67 | cmp ax, ETHER_IPv4 |
||
68 | je IPv4_input |
||
69 | |||
70 | cmp ax, ETHER_ARP |
||
71 | je ARP_input |
||
72 | |||
73 | cmp ax, ETHER_IPv6 |
||
74 | je IPv6_input |
||
75 | |||
76 | cmp ax, ETHER_PPP_DISCOVERY |
||
77 | je PPPoE_discovery_input |
||
78 | |||
79 | cmp ax, ETHER_PPP_SESSION |
||
80 | je PPPoE_session_input |
||
81 | |||
3589 | Serge | 82 | DEBUGF DEBUG_NETWORK_ERROR, "ETH_input: Unknown packet type=%x\n", ax |
3555 | Serge | 83 | |
84 | .dump: |
||
3589 | Serge | 85 | DEBUGF DEBUG_NETWORK_VERBOSE,"ETH_input: dumping\n" |
3555 | Serge | 86 | call kernel_free |
87 | add esp, 4 |
||
88 | ret |
||
89 | |||
90 | ;----------------------------------------------------------------- |
||
91 | ; |
||
92 | ; ETH_output |
||
93 | ; |
||
94 | ; IN: eax = pointer to source mac |
||
95 | ; ebx = device ptr |
||
96 | ; ecx = packet size |
||
97 | ; edx = pointer to destination mac |
||
98 | ; di = protocol |
||
99 | ; |
||
100 | ; OUT: edi = 0 on error, pointer to buffer otherwise |
||
101 | ; eax = buffer start |
||
102 | ; ebx = to device structure |
||
103 | ; ecx = unchanged (packet size of embedded data) |
||
104 | ; edx = size of complete buffer |
||
105 | ; |
||
106 | ;----------------------------------------------------------------- |
||
107 | align 4 |
||
108 | ETH_output: |
||
109 | |||
3589 | Serge | 110 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: size=%u device=%x\n", ecx, ebx |
3555 | Serge | 111 | |
112 | cmp ecx, [ebx + NET_DEVICE.mtu] |
||
113 | ja .exit |
||
114 | |||
115 | push ecx |
||
116 | push di eax edx |
||
117 | |||
118 | add ecx, sizeof.ETH_header |
||
119 | stdcall kernel_alloc, ecx |
||
120 | test eax, eax |
||
121 | jz .out_of_ram |
||
122 | mov edi, eax |
||
123 | |||
124 | pop esi |
||
125 | movsd |
||
126 | movsw |
||
127 | pop esi |
||
128 | movsd |
||
129 | movsw |
||
130 | pop ax |
||
131 | stosw |
||
132 | |||
133 | lea eax, [edi - sizeof.ETH_header] ; Set eax to buffer start |
||
134 | pop ecx |
||
135 | lea edx, [ecx + sizeof.ETH_header] ; Set edx to complete buffer size |
||
136 | |||
137 | cmp edx, ETH_FRAME_MINIMUM |
||
138 | jbe .adjust_size |
||
139 | .done: |
||
3589 | Serge | 140 | DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: ptr=%x size=%u\n", eax, edx |
3555 | Serge | 141 | ret |
142 | |||
143 | .adjust_size: |
||
144 | mov edx, ETH_FRAME_MINIMUM |
||
145 | test edx, edx ; clear zero flag |
||
146 | jmp .done |
||
147 | |||
148 | .out_of_ram: |
||
3589 | Serge | 149 | DEBUGF DEBUG_NETWORK_ERROR, "ETH_output: Out of ram!\n" |
3555 | Serge | 150 | add esp, 4+4+2+4 |
151 | sub edi, edi |
||
152 | ret |
||
153 | |||
154 | .exit: |
||
3589 | Serge | 155 | DEBUGF DEBUG_NETWORK_ERROR, "ETH_output: Packet too large!\n" |
3555 | Serge | 156 | sub edi, edi |
157 | ret |
||
158 | |||
159 | |||
160 | |||
161 | ;----------------------------------------------------------------- |
||
162 | ; |
||
163 | ; ETH_API |
||
164 | ; |
||
165 | ; This function is called by system function 75 |
||
166 | ; |
||
167 | ; IN: subfunction number in bl |
||
168 | ; device number in bh |
||
169 | ; ecx, edx, .. depends on subfunction |
||
170 | ; |
||
171 | ; OUT: |
||
172 | ; |
||
173 | ;----------------------------------------------------------------- |
||
174 | align 4 |
||
175 | ETH_api: |
||
176 | |||
177 | cmp bh, MAX_NET_DEVICES |
||
178 | ja .error |
||
179 | movzx eax, bh |
||
180 | mov eax, dword [NET_DRV_LIST + 4*eax] |
||
181 | cmp [eax + NET_DEVICE.type], NET_TYPE_ETH |
||
182 | jne .error |
||
183 | |||
184 | and ebx, 0xff |
||
185 | cmp ebx, .number |
||
186 | ja .error |
||
187 | jmp dword [.table + 4*ebx] |
||
188 | |||
189 | .table: |
||
190 | dd .packets_tx ; 0 |
||
191 | dd .packets_rx ; 1 |
||
192 | dd .bytes_tx ; 2 |
||
193 | dd .bytes_rx ; 3 |
||
194 | dd .read_mac ; 4 |
||
195 | dd .state ; 5 |
||
196 | .number = ($ - .table) / 4 - 1 |
||
197 | |||
198 | .error: |
||
199 | or eax, -1 |
||
200 | ret |
||
201 | |||
202 | .packets_tx: |
||
203 | mov eax, [eax + NET_DEVICE.packets_tx] |
||
204 | |||
205 | ret |
||
206 | |||
207 | .packets_rx: |
||
208 | mov eax, [eax + NET_DEVICE.packets_rx] |
||
209 | ret |
||
210 | |||
211 | .bytes_tx: |
||
212 | mov ebx, dword [eax + NET_DEVICE.bytes_tx + 4] |
||
213 | mov eax, dword [eax + NET_DEVICE.bytes_tx] |
||
214 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
||
215 | ret |
||
216 | |||
217 | .bytes_rx: |
||
218 | mov ebx, dword [eax + NET_DEVICE.bytes_rx + 4] |
||
219 | mov eax, dword [eax + NET_DEVICE.bytes_rx] |
||
220 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
||
221 | ret |
||
222 | |||
223 | |||
224 | .read_mac: |
||
225 | movzx ebx, word [eax + ETH_DEVICE.mac] |
||
226 | mov eax, dword [eax + ETH_DEVICE.mac + 2] |
||
227 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
||
228 | ret |
||
229 | |||
230 | .state: |
||
231 | mov eax, [eax + NET_DEVICE.state] |
||
232 | ret |
||
233 |