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