Rev 4558 | Rev 4704 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4558 | Rev 4582 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Lukas Mejdrech |
2 | * Copyright (c) 2009 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup eth |
29 | /** @addtogroup eth |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * Ethernet module implementation. |
34 | * Ethernet module implementation. |
35 | * @see eth.h |
35 | * @see eth.h |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <async.h> |
38 | #include <async.h> |
39 | #include <malloc.h> |
39 | #include <malloc.h> |
40 | #include <mem.h> |
40 | #include <mem.h> |
41 | #include <stdio.h> |
41 | #include <stdio.h> |
- | 42 | #include <string.h> |
|
42 | 43 | ||
43 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
44 | #include <ipc/services.h> |
45 | #include <ipc/services.h> |
45 | 46 | ||
46 | #include "../../err.h" |
47 | #include "../../err.h" |
47 | #include "../../messages.h" |
48 | #include "../../messages.h" |
48 | #include "../../modules.h" |
49 | #include "../../modules.h" |
49 | 50 | ||
50 | #include "../../include/byteorder.h" |
51 | #include "../../include/byteorder.h" |
51 | #include "../../include/crc.h" |
52 | #include "../../include/crc.h" |
52 | #include "../../include/ethernet_lsap.h" |
53 | #include "../../include/ethernet_lsap.h" |
53 | #include "../../include/ethernet_protocols.h" |
54 | #include "../../include/ethernet_protocols.h" |
54 | #include "../../include/protocol_map.h" |
55 | #include "../../include/protocol_map.h" |
55 | #include "../../include/device.h" |
56 | #include "../../include/device.h" |
56 | #include "../../include/netif_interface.h" |
57 | #include "../../include/netif_interface.h" |
57 | #include "../../include/net_interface.h" |
58 | #include "../../include/net_interface.h" |
58 | #include "../../include/nil_interface.h" |
59 | #include "../../include/nil_interface.h" |
59 | #include "../../include/il_interface.h" |
60 | #include "../../include/il_interface.h" |
60 | 61 | ||
61 | #include "../../structures/measured_strings.h" |
62 | #include "../../structures/measured_strings.h" |
62 | #include "../../structures/packet/packet_client.h" |
63 | #include "../../structures/packet/packet_client.h" |
63 | 64 | ||
64 | #include "../nil_module.h" |
65 | #include "../nil_module.h" |
65 | 66 | ||
66 | #include "eth.h" |
67 | #include "eth.h" |
67 | #include "eth_header.h" |
68 | #include "eth_header.h" |
68 | 69 | ||
69 | /** Reserved packet prefix length. |
70 | /** Reserved packet prefix length. |
70 | */ |
71 | */ |
71 | #define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )) |
72 | #define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )) |
72 | 73 | ||
73 | /** Reserved packet suffix length. |
74 | /** Reserved packet suffix length. |
74 | */ |
75 | */ |
75 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
76 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
76 | 77 | ||
77 | /** Maximum packet content length. |
78 | /** Maximum packet content length. |
78 | */ |
79 | */ |
79 | #define ETH_MAX_CONTENT 1500 |
80 | #define ETH_MAX_CONTENT 1500 |
80 | 81 | ||
81 | /** Minimum packet content length. |
82 | /** Minimum packet content length. |
82 | */ |
83 | */ |
83 | #define ETH_MIN_CONTENT 46u |
84 | #define ETH_MIN_CONTENT 46u |
84 | 85 | ||
85 | /** Maximum tagged packet content length. |
86 | /** Maximum tagged packet content length. |
86 | */ |
87 | */ |
87 | #define ETH_MAX_TAGGED_CONTENT( flags ) ( ETH_MAX_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 )) |
88 | #define ETH_MAX_TAGGED_CONTENT( flags ) ( ETH_MAX_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 )) |
88 | 89 | ||
89 | /** Minimum tagged packet content length. |
90 | /** Minimum tagged packet content length. |
90 | */ |
91 | */ |
91 | #define ETH_MIN_TAGGED_CONTENT( flags ) ( ETH_MIN_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 )) |
92 | #define ETH_MIN_TAGGED_CONTENT( flags ) ( ETH_MIN_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 )) |
92 | 93 | ||
93 | /** Dummy flag shift value. |
94 | /** Dummy flag shift value. |
94 | */ |
95 | */ |
95 | #define ETH_DUMMY_SHIFT 0 |
96 | #define ETH_DUMMY_SHIFT 0 |
96 | 97 | ||
97 | /** Mode flag shift value. |
98 | /** Mode flag shift value. |
98 | */ |
99 | */ |
99 | #define ETH_MODE_SHIFT 1 |
100 | #define ETH_MODE_SHIFT 1 |
100 | 101 | ||
101 | /** Dummy device flag. |
102 | /** Dummy device flag. |
102 | * Preamble and FCS are mandatory part of the packets. |
103 | * Preamble and FCS are mandatory part of the packets. |
103 | */ |
104 | */ |
104 | #define ETH_DUMMY ( 1 << ETH_DUMMY_SHIFT ) |
105 | #define ETH_DUMMY ( 1 << ETH_DUMMY_SHIFT ) |
105 | 106 | ||
106 | /** Returns the dummy flag. |
107 | /** Returns the dummy flag. |
107 | * @see ETH_DUMMY |
108 | * @see ETH_DUMMY |
108 | */ |
109 | */ |
109 | #define IS_DUMMY( flags ) (( flags ) & ETH_DUMMY ) |
110 | #define IS_DUMMY( flags ) (( flags ) & ETH_DUMMY ) |
110 | 111 | ||
111 | /** Device mode flags. |
112 | /** Device mode flags. |
112 | * @see ETH_DIX |
113 | * @see ETH_DIX |
113 | * @see ETH_8023_2_LSAP |
114 | * @see ETH_8023_2_LSAP |
114 | * @see ETH_8023_2_SNAP |
115 | * @see ETH_8023_2_SNAP |
115 | */ |
116 | */ |
116 | #define ETH_MODE_MASK ( 3 << ETH_MODE_SHIFT ) |
117 | #define ETH_MODE_MASK ( 3 << ETH_MODE_SHIFT ) |
117 | 118 | ||
118 | /** DIX Ethernet mode flag. |
119 | /** DIX Ethernet mode flag. |
119 | */ |
120 | */ |
120 | #define ETH_DIX ( 1 << ETH_MODE_SHIFT ) |
121 | #define ETH_DIX ( 1 << ETH_MODE_SHIFT ) |
121 | 122 | ||
122 | /** Returns whether the DIX Ethernet mode flag is set. |
123 | /** Returns whether the DIX Ethernet mode flag is set. |
123 | * @param flags The ethernet flags. Input parameter. |
124 | * @param flags The ethernet flags. Input parameter. |
124 | * @see ETH_DIX |
125 | * @see ETH_DIX |
125 | */ |
126 | */ |
126 | #define IS_DIX( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_DIX ) |
127 | #define IS_DIX( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_DIX ) |
127 | 128 | ||
128 | /** 802.3 + 802.2 + LSAP mode flag. |
129 | /** 802.3 + 802.2 + LSAP mode flag. |
129 | */ |
130 | */ |
130 | #define ETH_8023_2_LSAP ( 2 << ETH_MODE_SHIFT ) |
131 | #define ETH_8023_2_LSAP ( 2 << ETH_MODE_SHIFT ) |
131 | 132 | ||
132 | /** Returns whether the 802.3 + 802.2 + LSAP mode flag is set. |
133 | /** Returns whether the 802.3 + 802.2 + LSAP mode flag is set. |
133 | * @param flags The ethernet flags. Input parameter. |
134 | * @param flags The ethernet flags. Input parameter. |
134 | * @see ETH_8023_2_LSAP |
135 | * @see ETH_8023_2_LSAP |
135 | */ |
136 | */ |
136 | #define IS_8023_2_LSAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_LSAP ) |
137 | #define IS_8023_2_LSAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_LSAP ) |
137 | 138 | ||
138 | /** 802.3 + 802.2 + LSAP + SNAP mode flag. |
139 | /** 802.3 + 802.2 + LSAP + SNAP mode flag. |
139 | */ |
140 | */ |
140 | #define ETH_8023_2_SNAP ( 3 << ETH_MODE_SHIFT ) |
141 | #define ETH_8023_2_SNAP ( 3 << ETH_MODE_SHIFT ) |
141 | 142 | ||
142 | /** Returns whether the 802.3 + 802.2 + LSAP + SNAP mode flag is set. |
143 | /** Returns whether the 802.3 + 802.2 + LSAP + SNAP mode flag is set. |
143 | * @param flags The ethernet flags. Input parameter. |
144 | * @param flags The ethernet flags. Input parameter. |
144 | * @see ETH_8023_2_SNAP |
145 | * @see ETH_8023_2_SNAP |
145 | */ |
146 | */ |
146 | #define IS_8023_2_SNAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_SNAP ) |
147 | #define IS_8023_2_SNAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_SNAP ) |
147 | 148 | ||
148 | /** Type definition of the ethernet address type. |
149 | /** Type definition of the ethernet address type. |
149 | * @see eth_addr_type |
150 | * @see eth_addr_type |
150 | */ |
151 | */ |
151 | typedef enum eth_addr_type eth_addr_type_t; |
152 | typedef enum eth_addr_type eth_addr_type_t; |
152 | 153 | ||
153 | /** Type definition of the ethernet address type pointer. |
154 | /** Type definition of the ethernet address type pointer. |
154 | * @see eth_addr_type |
155 | * @see eth_addr_type |
155 | */ |
156 | */ |
156 | typedef eth_addr_type_t * eth_addr_type_ref; |
157 | typedef eth_addr_type_t * eth_addr_type_ref; |
157 | 158 | ||
158 | /** Ethernet address type. |
159 | /** Ethernet address type. |
159 | */ |
160 | */ |
160 | enum eth_addr_type{ |
161 | enum eth_addr_type{ |
161 | /** Local address. |
162 | /** Local address. |
162 | */ |
163 | */ |
163 | ETH_LOCAL_ADDR, |
164 | ETH_LOCAL_ADDR, |
164 | /** Broadcast address. |
165 | /** Broadcast address. |
165 | */ |
166 | */ |
166 | ETH_BROADCAST_ADDR |
167 | ETH_BROADCAST_ADDR |
167 | }; |
168 | }; |
168 | 169 | ||
169 | /** Ethernet module global data. |
170 | /** Ethernet module global data. |
170 | */ |
171 | */ |
171 | eth_globals_t eth_globals; |
172 | eth_globals_t eth_globals; |
172 | 173 | ||
173 | /** Processes IPC messages from the registered device driver modules in an infinite loop. |
174 | /** Processes IPC messages from the registered device driver modules in an infinite loop. |
174 | * @param iid The message identifier. Input parameter. |
175 | * @param iid The message identifier. Input parameter. |
175 | * @param icall The message parameters. Input/output parameter. |
176 | * @param icall The message parameters. Input/output parameter. |
176 | */ |
177 | */ |
177 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
178 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
178 | 179 | ||
179 | /** Registers new device or updates the MTU of an existing one. |
180 | /** Registers new device or updates the MTU of an existing one. |
180 | * Determines the device local hardware address. |
181 | * Determines the device local hardware address. |
181 | * @param device_id The new device identifier. Input parameter. |
182 | * @param device_id The new device identifier. Input parameter. |
182 | * @param service The device driver service. Input parameter. |
183 | * @param service The device driver service. Input parameter. |
183 | * @param mtu The device maximum transmission unit. Input parameter. |
184 | * @param mtu The device maximum transmission unit. Input parameter. |
184 | * @returns EOK on success. |
185 | * @returns EOK on success. |
185 | * @returns EEXIST if the device with the different service exists. |
186 | * @returns EEXIST if the device with the different service exists. |
186 | * @returns ENOMEM if there is not enough memory left. |
187 | * @returns ENOMEM if there is not enough memory left. |
187 | * @returns Other error codes as defined for the net_get_device_conf_req() function. |
188 | * @returns Other error codes as defined for the net_get_device_conf_req() function. |
188 | * @returns Other error codes as defined for the netif_bind_service() function. |
189 | * @returns Other error codes as defined for the netif_bind_service() function. |
189 | * @returns Other error codes as defined for the netif_get_addr() function. |
190 | * @returns Other error codes as defined for the netif_get_addr() function. |
190 | */ |
191 | */ |
191 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ); |
192 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ); |
192 | 193 | ||
193 | /** Registers receiving module service. |
194 | /** Registers receiving module service. |
194 | * Passes received packets for this service. |
195 | * Passes received packets for this service. |
195 | * @param service The module service. Input parameter. |
196 | * @param service The module service. Input parameter. |
196 | * @param phone The service phone. Input parameter. |
197 | * @param phone The service phone. Input parameter. |
197 | * @returns EOK on success. |
198 | * @returns EOK on success. |
198 | * @returns ENOENT if the service is not known. |
199 | * @returns ENOENT if the service is not known. |
199 | * @returns ENOMEM if there is not enough memory left. |
200 | * @returns ENOMEM if there is not enough memory left. |
200 | */ |
201 | */ |
201 | int nil_register_message( services_t service, int phone ); |
202 | int nil_register_message( services_t service, int phone ); |
202 | 203 | ||
203 | /** Returns the device packet dimensions for sending. |
204 | /** Returns the device packet dimensions for sending. |
204 | * @param device_id The device identifier. Input parameter. |
205 | * @param device_id The device identifier. Input parameter. |
205 | * @param addr_len The minimum reserved address length. Output parameter. |
206 | * @param addr_len The minimum reserved address length. Output parameter. |
206 | * @param prefix The minimum reserved prefix size. Output parameter. |
207 | * @param prefix The minimum reserved prefix size. Output parameter. |
207 | * @param content The maximum content size. Output parameter. |
208 | * @param content The maximum content size. Output parameter. |
208 | * @param suffix The minimum reserved suffix size. Output parameter. |
209 | * @param suffix The minimum reserved suffix size. Output parameter. |
209 | * @returns EOK on success. |
210 | * @returns EOK on success. |
210 | * @returns EBADMEM if either one of the parameters is NULL. |
211 | * @returns EBADMEM if either one of the parameters is NULL. |
211 | * @returns ENOENT if there is no such device. |
212 | * @returns ENOENT if there is no such device. |
212 | */ |
213 | */ |
213 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); |
214 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); |
214 | 215 | ||
215 | /** Returns the device hardware address. |
216 | /** Returns the device hardware address. |
216 | * @param device_id The device identifier. Input parameter. |
217 | * @param device_id The device identifier. Input parameter. |
217 | * @param type Type of the desired address. Input parameter |
218 | * @param type Type of the desired address. Input parameter |
218 | * @param address The device hardware address. Output parameter. |
219 | * @param address The device hardware address. Output parameter. |
219 | * @returns EOK on success. |
220 | * @returns EOK on success. |
220 | * @returns EBADMEM if the address parameter is NULL. |
221 | * @returns EBADMEM if the address parameter is NULL. |
221 | * @returns ENOENT if there no such device. |
222 | * @returns ENOENT if there no such device. |
222 | */ |
223 | */ |
223 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ); |
224 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ); |
224 | 225 | ||
225 | /** Sends the packet queue. |
226 | /** Sends the packet queue. |
226 | * Sends only packet successfully processed by the eth_prepare_packet() function. |
227 | * Sends only packet successfully processed by the eth_prepare_packet() function. |
227 | * @param device_id The device identifier. Input parameter. |
228 | * @param device_id The device identifier. Input parameter. |
228 | * @param packet The packet queue. Input parameter. |
229 | * @param packet The packet queue. Input parameter. |
229 | * @param sender The sending module service. Input parameter. |
230 | * @param sender The sending module service. Input parameter. |
230 | * @returns EOK on success. |
231 | * @returns EOK on success. |
231 | * @returns ENOENT if there no such device. |
232 | * @returns ENOENT if there no such device. |
232 | * @returns EINVAL if the service parameter is not known. |
233 | * @returns EINVAL if the service parameter is not known. |
233 | */ |
234 | */ |
234 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ); |
235 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ); |
235 | 236 | ||
236 | /** Processes the received packet and chooses the target registered module. |
237 | /** Processes the received packet and chooses the target registered module. |
237 | * @param flags The device flags. Input parameter. |
238 | * @param flags The device flags. Input parameter. |
238 | * @param packet The packet. Input parameter. |
239 | * @param packet The packet. Input parameter. |
239 | * @returns The target registered module. |
240 | * @returns The target registered module. |
240 | * @returns NULL if the packet is not long enough. |
241 | * @returns NULL if the packet is not long enough. |
241 | * @returns NULL if the packet is too long. |
242 | * @returns NULL if the packet is too long. |
242 | * @returns NULL if the raw ethernet protocol is used. |
243 | * @returns NULL if the raw ethernet protocol is used. |
243 | * @returns NULL if the dummy device FCS checksum is invalid. |
244 | * @returns NULL if the dummy device FCS checksum is invalid. |
244 | * @returns NULL if the packet address length is not big enough. |
245 | * @returns NULL if the packet address length is not big enough. |
245 | */ |
246 | */ |
246 | eth_proto_ref eth_process_packet( int flags, packet_t packet ); |
247 | eth_proto_ref eth_process_packet( int flags, packet_t packet ); |
247 | 248 | ||
248 | /** Prepares the packet for sending. |
249 | /** Prepares the packet for sending. |
249 | * @param flags The device flags. Input parameter. |
250 | * @param flags The device flags. Input parameter. |
250 | * @param packet The packet. Input parameter. |
251 | * @param packet The packet. Input parameter. |
251 | * @param src_addr The source hardware address. Input parameter. |
252 | * @param src_addr The source hardware address. Input parameter. |
252 | * @param ethertype The ethernet protocol type. Input parameter. |
253 | * @param ethertype The ethernet protocol type. Input parameter. |
253 | * @param mtu The device maximum transmission unit. Input parameter. |
254 | * @param mtu The device maximum transmission unit. Input parameter. |
254 | * @returns EOK on success. |
255 | * @returns EOK on success. |
255 | * @returns EINVAL if the packet addresses length is not long enough. |
256 | * @returns EINVAL if the packet addresses length is not long enough. |
256 | * @returns EINVAL if the packet is bigger than the device MTU. |
257 | * @returns EINVAL if the packet is bigger than the device MTU. |
257 | * @returns ENOMEM if there is not enough memory in the packet. |
258 | * @returns ENOMEM if there is not enough memory in the packet. |
258 | */ |
259 | */ |
259 | int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ); |
260 | int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ); |
260 | 261 | ||
261 | DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t ) |
262 | DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t ) |
262 | 263 | ||
263 | INT_MAP_IMPLEMENT( eth_protos, eth_proto_t ) |
264 | INT_MAP_IMPLEMENT( eth_protos, eth_proto_t ) |
264 | 265 | ||
265 | int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){ |
266 | int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){ |
266 | int index; |
267 | int index; |
267 | eth_proto_ref proto; |
268 | eth_proto_ref proto; |
268 | 269 | ||
269 | //TODO clear device if off? |
270 | //TODO clear device if off? |
270 | rwlock_read_lock( & eth_globals.protos_lock ); |
271 | fibril_rwlock_read_lock( & eth_globals.protos_lock ); |
271 | for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){ |
272 | for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){ |
272 | proto = eth_protos_get_index( & eth_globals.protos, index ); |
273 | proto = eth_protos_get_index( & eth_globals.protos, index ); |
273 | if( proto && proto->phone ) il_device_state_msg( proto->phone, device_id, state, proto->service ); |
274 | if( proto && proto->phone ) il_device_state_msg( proto->phone, device_id, state, proto->service ); |
274 | } |
275 | } |
275 | rwlock_read_unlock( & eth_globals.protos_lock ); |
276 | fibril_rwlock_read_unlock( & eth_globals.protos_lock ); |
276 | return EOK; |
277 | return EOK; |
277 | } |
278 | } |
278 | 279 | ||
279 | int nil_initialize( int net_phone ){ |
280 | int nil_initialize( int net_phone ){ |
280 | ERROR_DECLARE; |
281 | ERROR_DECLARE; |
281 | 282 | ||
282 | rwlock_initialize( & eth_globals.devices_lock ); |
283 | fibril_rwlock_initialize( & eth_globals.devices_lock ); |
283 | rwlock_initialize( & eth_globals.protos_lock ); |
284 | fibril_rwlock_initialize( & eth_globals.protos_lock ); |
284 | rwlock_write_lock( & eth_globals.devices_lock ); |
285 | fibril_rwlock_write_lock( & eth_globals.devices_lock ); |
285 | rwlock_write_lock( & eth_globals.protos_lock ); |
286 | fibril_rwlock_write_lock( & eth_globals.protos_lock ); |
286 | eth_globals.net_phone = net_phone; |
287 | eth_globals.net_phone = net_phone; |
287 | eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR )); |
288 | eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR )); |
288 | if( ! eth_globals.broadcast_addr ) return ENOMEM; |
289 | if( ! eth_globals.broadcast_addr ) return ENOMEM; |
289 | ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices )); |
290 | ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices )); |
290 | if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){ |
291 | if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){ |
291 | eth_devices_destroy( & eth_globals.devices ); |
292 | eth_devices_destroy( & eth_globals.devices ); |
292 | return ERROR_CODE; |
293 | return ERROR_CODE; |
293 | } |
294 | } |
294 | rwlock_write_unlock( & eth_globals.protos_lock ); |
295 | fibril_rwlock_write_unlock( & eth_globals.protos_lock ); |
295 | rwlock_write_unlock( & eth_globals.devices_lock ); |
296 | fibril_rwlock_write_unlock( & eth_globals.devices_lock ); |
296 | return EOK; |
297 | return EOK; |
297 | } |
298 | } |
298 | 299 | ||
299 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){ |
300 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){ |
300 | ERROR_DECLARE; |
301 | ERROR_DECLARE; |
301 | 302 | ||
302 | eth_device_ref device; |
303 | eth_device_ref device; |
303 | int index; |
304 | int index; |
304 | measured_string_t names[ 2 ] = {{ "ETH_MODE", 8 }, { "ETH_DUMMY", 9 }}; |
305 | measured_string_t names[ 2 ] = {{ "ETH_MODE", 8 }, { "ETH_DUMMY", 9 }}; |
305 | measured_string_ref configuration; |
306 | measured_string_ref configuration; |
306 | size_t count = sizeof( names ) / sizeof( measured_string_t ); |
307 | size_t count = sizeof( names ) / sizeof( measured_string_t ); |
307 | char * data; |
308 | char * data; |
308 | 309 | ||
309 | rwlock_write_lock( & eth_globals.devices_lock ); |
310 | fibril_rwlock_write_lock( & eth_globals.devices_lock ); |
310 | // an existing device? |
311 | // an existing device? |
311 | device = eth_devices_find( & eth_globals.devices, device_id ); |
312 | device = eth_devices_find( & eth_globals.devices, device_id ); |
312 | if( device ){ |
313 | if( device ){ |
313 | if( device->service != service ){ |
314 | if( device->service != service ){ |
314 | printf( "Device %d already exists\n", device->device_id ); |
315 | printf( "Device %d already exists\n", device->device_id ); |
315 | rwlock_write_unlock( & eth_globals.devices_lock ); |
316 | fibril_rwlock_write_unlock( & eth_globals.devices_lock ); |
316 | return EEXIST; |
317 | return EEXIST; |
317 | }else{ |
318 | }else{ |
318 | // update mtu |
319 | // update mtu |
319 | device->mtu = mtu; |
320 | device->mtu = mtu; |
320 | printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu ); |
321 | printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu ); |
321 | } |
322 | } |
322 | }else{ |
323 | }else{ |
323 | // create a new device |
324 | // create a new device |
324 | device = ( eth_device_ref ) malloc( sizeof( eth_device_t )); |
325 | device = ( eth_device_ref ) malloc( sizeof( eth_device_t )); |
325 | if( ! device ) return ENOMEM; |
326 | if( ! device ) return ENOMEM; |
326 | device->device_id = device_id; |
327 | device->device_id = device_id; |
327 | device->service = service; |
328 | device->service = service; |
328 | device->flags = 0; |
329 | device->flags = 0; |
329 | device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))) ? mtu : ETH_MAX_TAGGED_CONTENT( device->flags ); |
330 | device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))) ? mtu : ETH_MAX_TAGGED_CONTENT( device->flags ); |
330 | configuration = & names[ 0 ]; |
331 | configuration = & names[ 0 ]; |
331 | if( ERROR_OCCURRED( net_get_device_conf_req( eth_globals.net_phone, device->device_id, & configuration, count, & data ))){ |
332 | if( ERROR_OCCURRED( net_get_device_conf_req( eth_globals.net_phone, device->device_id, & configuration, count, & data ))){ |
332 | rwlock_write_unlock( & eth_globals.devices_lock ); |
333 | fibril_rwlock_write_unlock( & eth_globals.devices_lock ); |
333 | free( device ); |
334 | free( device ); |
334 | return ERROR_CODE; |
335 | return ERROR_CODE; |
335 | } |
336 | } |
336 | if( configuration ){ |
337 | if( configuration ){ |
337 | if( ! str_lcmp( configuration[ 0 ].value, "DIX", configuration[ 0 ].length )){ |
338 | if( ! str_lcmp( configuration[ 0 ].value, "DIX", configuration[ 0 ].length )){ |
338 | device->flags |= ETH_DIX; |
339 | device->flags |= ETH_DIX; |
339 | }else if( ! str_lcmp( configuration[ 0 ].value, "8023_2_LSAP", configuration[ 0 ].length )){ |
340 | }else if( ! str_lcmp( configuration[ 0 ].value, "8023_2_LSAP", configuration[ 0 ].length )){ |
340 | // TODO 8023_2_LSAP |
341 | // TODO 8023_2_LSAP |
341 | printf( "8023_2_LSAP is not supported (yet?), DIX used instead\n" ); |
342 | printf( "8023_2_LSAP is not supported (yet?), DIX used instead\n" ); |
342 | device->flags |= ETH_DIX; |
343 | device->flags |= ETH_DIX; |
343 | }else device->flags |= ETH_8023_2_SNAP; |
344 | }else device->flags |= ETH_8023_2_SNAP; |
344 | if(( configuration[ 1 ].value ) && ( configuration[ 1 ].value[ 0 ] == 'y' )){ |
345 | if(( configuration[ 1 ].value ) && ( configuration[ 1 ].value[ 0 ] == 'y' )){ |
345 | device->flags |= ETH_DUMMY; |
346 | device->flags |= ETH_DUMMY; |
346 | } |
347 | } |
347 | net_free_settings( configuration, data ); |
348 | net_free_settings( configuration, data ); |
348 | }else{ |
349 | }else{ |
349 | device->flags |= ETH_8023_2_SNAP; |
350 | device->flags |= ETH_8023_2_SNAP; |
350 | } |
351 | } |
351 | // bind the device driver |
352 | // bind the device driver |
352 | device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, eth_receiver ); |
353 | device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, eth_receiver ); |
353 | if( device->phone < 0 ){ |
354 | if( device->phone < 0 ){ |
354 | rwlock_write_unlock( & eth_globals.devices_lock ); |
355 | fibril_rwlock_write_unlock( & eth_globals.devices_lock ); |
355 | free( device ); |
356 | free( device ); |
356 | return device->phone; |
357 | return device->phone; |
357 | } |
358 | } |
358 | // get hardware address |
359 | // get hardware address |
359 | if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){ |
360 | if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){ |
360 | rwlock_write_unlock( & eth_globals.devices_lock ); |
361 | fibril_rwlock_write_unlock( & eth_globals.devices_lock ); |
361 | free( device ); |
362 | free( device ); |
362 | return ERROR_CODE; |
363 | return ERROR_CODE; |
363 | } |
364 | } |
364 | // add to the cache |
365 | // add to the cache |
365 | index = eth_devices_add( & eth_globals.devices, device->device_id, device ); |
366 | index = eth_devices_add( & eth_globals.devices, device->device_id, device ); |
366 | if( index < 0 ){ |
367 | if( index < 0 ){ |
367 | rwlock_write_unlock( & eth_globals.devices_lock ); |
368 | fibril_rwlock_write_unlock( & eth_globals.devices_lock ); |
368 | free( device->addr ); |
369 | free( device->addr ); |
369 | free( device->addr_data ); |
370 | free( device->addr_data ); |
370 | free( device ); |
371 | free( device ); |
371 | return index; |
372 | return index; |
372 | } |
373 | } |
373 | printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X\n\tflags\t= 0x%x\n", device->device_id, device->service, device->mtu, device->addr_data[ 0 ], device->addr_data[ 1 ], device->addr_data[ 2 ], device->addr_data[ 3 ], device->addr_data[ 4 ], device->addr_data[ 5 ], device->flags ); |
374 | printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X\n\tflags\t= 0x%x\n", device->device_id, device->service, device->mtu, device->addr_data[ 0 ], device->addr_data[ 1 ], device->addr_data[ 2 ], device->addr_data[ 3 ], device->addr_data[ 4 ], device->addr_data[ 5 ], device->flags ); |
374 | } |
375 | } |
375 | rwlock_write_unlock( & eth_globals.devices_lock ); |
376 | fibril_rwlock_write_unlock( & eth_globals.devices_lock ); |
376 | return EOK; |
377 | return EOK; |
377 | } |
378 | } |
378 | 379 | ||
379 | eth_proto_ref eth_process_packet( int flags, packet_t packet ){ |
380 | eth_proto_ref eth_process_packet( int flags, packet_t packet ){ |
380 | ERROR_DECLARE; |
381 | ERROR_DECLARE; |
381 | 382 | ||
382 | eth_header_ex_ref header; |
383 | eth_header_ex_ref header; |
383 | size_t length; |
384 | size_t length; |
384 | eth_type_t type; |
385 | eth_type_t type; |
385 | size_t prefix; |
386 | size_t prefix; |
386 | size_t suffix; |
387 | size_t suffix; |
387 | eth_fcs_ref fcs; |
388 | eth_fcs_ref fcs; |
388 | uint8_t * data; |
389 | uint8_t * data; |
389 | 390 | ||
390 | length = packet_get_data_length( packet ); |
391 | length = packet_get_data_length( packet ); |
391 | if( IS_DUMMY( flags )){ |
392 | if( IS_DUMMY( flags )){ |
392 | packet_trim( packet, sizeof( eth_preamble_t ), 0 ); |
393 | packet_trim( packet, sizeof( eth_preamble_t ), 0 ); |
393 | } |
394 | } |
394 | if( length < sizeof( eth_header_t ) + ETH_MIN_CONTENT + ( IS_DUMMY( flags ) ? ETH_SUFFIX : 0 )) return NULL; |
395 | if( length < sizeof( eth_header_t ) + ETH_MIN_CONTENT + ( IS_DUMMY( flags ) ? ETH_SUFFIX : 0 )) return NULL; |
395 | data = packet_get_data( packet ); |
396 | data = packet_get_data( packet ); |
396 | header = ( eth_header_ex_ref ) data; |
397 | header = ( eth_header_ex_ref ) data; |
397 | type = ntohs( header->header.ethertype ); |
398 | type = ntohs( header->header.ethertype ); |
398 | if( type >= ETH_MIN_PROTO ){ |
399 | if( type >= ETH_MIN_PROTO ){ |
399 | // DIX Ethernet |
400 | // DIX Ethernet |
400 | prefix = sizeof( eth_header_t ); |
401 | prefix = sizeof( eth_header_t ); |
401 | suffix = 0; |
402 | suffix = 0; |
402 | fcs = ( eth_fcs_ref ) data + length - sizeof( eth_fcs_t ); |
403 | fcs = ( eth_fcs_ref ) data + length - sizeof( eth_fcs_t ); |
403 | length -= sizeof( eth_fcs_t ); |
404 | length -= sizeof( eth_fcs_t ); |
404 | }else if( type <= ETH_MAX_CONTENT ){ |
405 | }else if( type <= ETH_MAX_CONTENT ){ |
405 | // translate "LSAP" values |
406 | // translate "LSAP" values |
406 | if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){ |
407 | if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){ |
407 | // raw packet |
408 | // raw packet |
408 | // discard |
409 | // discard |
409 | return NULL; |
410 | return NULL; |
410 | }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){ |
411 | }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){ |
411 | // IEEE 802.3 + 802.2 + LSAP + SNAP |
412 | // IEEE 802.3 + 802.2 + LSAP + SNAP |
412 | // organization code not supported |
413 | // organization code not supported |
413 | type = ntohs( header->snap.ethertype ); |
414 | type = ntohs( header->snap.ethertype ); |
414 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ); |
415 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ); |
415 | }else{ |
416 | }else{ |
416 | // IEEE 802.3 + 802.2 LSAP |
417 | // IEEE 802.3 + 802.2 LSAP |
417 | type = lsap_map( header->lsap.dsap ); |
418 | type = lsap_map( header->lsap.dsap ); |
418 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t); |
419 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t); |
419 | } |
420 | } |
420 | suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0u; |
421 | suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0u; |
421 | fcs = ( eth_fcs_ref ) data + prefix + type + suffix; |
422 | fcs = ( eth_fcs_ref ) data + prefix + type + suffix; |
422 | suffix += length - prefix - type; |
423 | suffix += length - prefix - type; |
423 | length = prefix + type + suffix; |
424 | length = prefix + type + suffix; |
424 | }else{ |
425 | }else{ |
425 | // invalid length/type, should not occurr |
426 | // invalid length/type, should not occurr |
426 | return NULL; |
427 | return NULL; |
427 | } |
428 | } |
428 | if( IS_DUMMY( flags )){ |
429 | if( IS_DUMMY( flags )){ |
429 | if(( ~ compute_crc32( ~ 0u, data, length * 8 )) != ntohl( * fcs )){ |
430 | if(( ~ compute_crc32( ~ 0u, data, length * 8 )) != ntohl( * fcs )){ |
430 | return NULL; |
431 | return NULL; |
431 | } |
432 | } |
432 | suffix += sizeof( eth_fcs_t ); |
433 | suffix += sizeof( eth_fcs_t ); |
433 | } |
434 | } |
434 | if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR )) |
435 | if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR )) |
435 | || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){ |
436 | || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){ |
436 | return NULL; |
437 | return NULL; |
437 | } |
438 | } |
438 | return eth_protos_find( & eth_globals.protos, type ); |
439 | return eth_protos_find( & eth_globals.protos, type ); |
439 | } |
440 | } |
440 | 441 | ||
441 | int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){ |
442 | int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){ |
442 | eth_proto_ref proto; |
443 | eth_proto_ref proto; |
443 | packet_t next; |
444 | packet_t next; |
444 | eth_device_ref device; |
445 | eth_device_ref device; |
445 | int flags; |
446 | int flags; |
446 | 447 | ||
447 | rwlock_read_lock( & eth_globals.devices_lock ); |
448 | fibril_rwlock_read_lock( & eth_globals.devices_lock ); |
448 | device = eth_devices_find( & eth_globals.devices, device_id ); |
449 | device = eth_devices_find( & eth_globals.devices, device_id ); |
449 | if( ! device ){ |
450 | if( ! device ){ |
450 | rwlock_read_unlock( & eth_globals.devices_lock ); |
451 | fibril_rwlock_read_unlock( & eth_globals.devices_lock ); |
451 | return ENOENT; |
452 | return ENOENT; |
452 | } |
453 | } |
453 | flags = device->flags; |
454 | flags = device->flags; |
454 | rwlock_read_unlock( & eth_globals.devices_lock ); |
455 | fibril_rwlock_read_unlock( & eth_globals.devices_lock ); |
455 | rwlock_read_lock( & eth_globals.protos_lock ); |
456 | fibril_rwlock_read_lock( & eth_globals.protos_lock ); |
456 | do{ |
457 | do{ |
457 | next = pq_detach( packet ); |
458 | next = pq_detach( packet ); |
458 | proto = eth_process_packet( flags, packet ); |
459 | proto = eth_process_packet( flags, packet ); |
459 | if( proto ){ |
460 | if( proto ){ |
460 | il_received_msg( proto->phone, device_id, packet, proto->service ); |
461 | il_received_msg( proto->phone, device_id, packet, proto->service ); |
461 | }else{ |
462 | }else{ |
462 | // drop invalid/unknown |
463 | // drop invalid/unknown |
463 | pq_release( eth_globals.net_phone, packet_get_id( packet )); |
464 | pq_release( eth_globals.net_phone, packet_get_id( packet )); |
464 | } |
465 | } |
465 | packet = next; |
466 | packet = next; |
466 | }while( packet ); |
467 | }while( packet ); |
467 | rwlock_read_unlock( & eth_globals.protos_lock ); |
468 | fibril_rwlock_read_unlock( & eth_globals.protos_lock ); |
468 | return EOK; |
469 | return EOK; |
469 | } |
470 | } |
470 | 471 | ||
471 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ |
472 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ |
472 | eth_device_ref device; |
473 | eth_device_ref device; |
473 | 474 | ||
474 | if( !( addr_len && prefix && content && suffix )) return EBADMEM; |
475 | if( !( addr_len && prefix && content && suffix )) return EBADMEM; |
475 | rwlock_read_lock( & eth_globals.devices_lock ); |
476 | fibril_rwlock_read_lock( & eth_globals.devices_lock ); |
476 | device = eth_devices_find( & eth_globals.devices, device_id ); |
477 | device = eth_devices_find( & eth_globals.devices, device_id ); |
477 | if( ! device ){ |
478 | if( ! device ){ |
478 | rwlock_read_unlock( & eth_globals.devices_lock ); |
479 | fibril_rwlock_read_unlock( & eth_globals.devices_lock ); |
479 | return ENOENT; |
480 | return ENOENT; |
480 | } |
481 | } |
481 | * content = device->mtu; |
482 | * content = device->mtu; |
482 | rwlock_read_unlock( & eth_globals.devices_lock ); |
483 | fibril_rwlock_read_unlock( & eth_globals.devices_lock ); |
483 | * addr_len = ETH_ADDR; |
484 | * addr_len = ETH_ADDR; |
484 | * prefix = ETH_PREFIX; |
485 | * prefix = ETH_PREFIX; |
485 | * suffix = ETH_MIN_CONTENT + ETH_SUFFIX; |
486 | * suffix = ETH_MIN_CONTENT + ETH_SUFFIX; |
486 | return EOK; |
487 | return EOK; |
487 | } |
488 | } |
488 | 489 | ||
489 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){ |
490 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){ |
490 | eth_device_ref device; |
491 | eth_device_ref device; |
491 | 492 | ||
492 | if( ! address ) return EBADMEM; |
493 | if( ! address ) return EBADMEM; |
493 | if( type == ETH_BROADCAST_ADDR ){ |
494 | if( type == ETH_BROADCAST_ADDR ){ |
494 | * address = eth_globals.broadcast_addr; |
495 | * address = eth_globals.broadcast_addr; |
495 | }else{ |
496 | }else{ |
496 | rwlock_read_lock( & eth_globals.devices_lock ); |
497 | fibril_rwlock_read_lock( & eth_globals.devices_lock ); |
497 | device = eth_devices_find( & eth_globals.devices, device_id ); |
498 | device = eth_devices_find( & eth_globals.devices, device_id ); |
498 | if( ! device ){ |
499 | if( ! device ){ |
499 | rwlock_read_unlock( & eth_globals.devices_lock ); |
500 | fibril_rwlock_read_unlock( & eth_globals.devices_lock ); |
500 | return ENOENT; |
501 | return ENOENT; |
501 | } |
502 | } |
502 | * address = device->addr; |
503 | * address = device->addr; |
503 | rwlock_read_unlock( & eth_globals.devices_lock ); |
504 | fibril_rwlock_read_unlock( & eth_globals.devices_lock ); |
504 | } |
505 | } |
505 | return ( * address ) ? EOK : ENOENT; |
506 | return ( * address ) ? EOK : ENOENT; |
506 | } |
507 | } |
507 | 508 | ||
508 | int nil_register_message( services_t service, int phone ){ |
509 | int nil_register_message( services_t service, int phone ){ |
509 | eth_proto_ref proto; |
510 | eth_proto_ref proto; |
510 | int protocol; |
511 | int protocol; |
511 | int index; |
512 | int index; |
512 | 513 | ||
513 | protocol = protocol_map( SERVICE_ETHERNET, service ); |
514 | protocol = protocol_map( SERVICE_ETHERNET, service ); |
514 | if( ! protocol ) return ENOENT; |
515 | if( ! protocol ) return ENOENT; |
515 | rwlock_write_lock( & eth_globals.protos_lock ); |
516 | fibril_rwlock_write_lock( & eth_globals.protos_lock ); |
516 | proto = eth_protos_find( & eth_globals.protos, protocol ); |
517 | proto = eth_protos_find( & eth_globals.protos, protocol ); |
517 | if( proto ){ |
518 | if( proto ){ |
518 | proto->phone = phone; |
519 | proto->phone = phone; |
519 | rwlock_write_unlock( & eth_globals.protos_lock ); |
520 | fibril_rwlock_write_unlock( & eth_globals.protos_lock ); |
520 | return EOK; |
521 | return EOK; |
521 | }else{ |
522 | }else{ |
522 | proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t )); |
523 | proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t )); |
523 | if( ! proto ){ |
524 | if( ! proto ){ |
524 | rwlock_write_unlock( & eth_globals.protos_lock ); |
525 | fibril_rwlock_write_unlock( & eth_globals.protos_lock ); |
525 | return ENOMEM; |
526 | return ENOMEM; |
526 | } |
527 | } |
527 | proto->service = service; |
528 | proto->service = service; |
528 | proto->protocol = protocol; |
529 | proto->protocol = protocol; |
529 | proto->phone = phone; |
530 | proto->phone = phone; |
530 | index = eth_protos_add( & eth_globals.protos, protocol, proto ); |
531 | index = eth_protos_add( & eth_globals.protos, protocol, proto ); |
531 | if( index < 0 ){ |
532 | if( index < 0 ){ |
532 | rwlock_write_unlock( & eth_globals.protos_lock ); |
533 | fibril_rwlock_write_unlock( & eth_globals.protos_lock ); |
533 | free( proto ); |
534 | free( proto ); |
534 | return index; |
535 | return index; |
535 | } |
536 | } |
536 | } |
537 | } |
537 | printf( "New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone ); |
538 | printf( "New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone ); |
538 | rwlock_write_unlock( & eth_globals.protos_lock ); |
539 | fibril_rwlock_write_unlock( & eth_globals.protos_lock ); |
539 | return EOK; |
540 | return EOK; |
540 | } |
541 | } |
541 | 542 | ||
542 | int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ){ |
543 | int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ){ |
543 | eth_header_ex_ref header; |
544 | eth_header_ex_ref header; |
544 | eth_header_ref header_dix; |
545 | eth_header_ref header_dix; |
545 | eth_fcs_ref fcs; |
546 | eth_fcs_ref fcs; |
546 | uint8_t * src; |
547 | uint8_t * src; |
547 | uint8_t * dest; |
548 | uint8_t * dest; |
548 | size_t length; |
549 | size_t length; |
549 | int i; |
550 | int i; |
550 | void * padding; |
551 | void * padding; |
551 | eth_preamble_ref preamble; |
552 | eth_preamble_ref preamble; |
552 | 553 | ||
553 | i = packet_get_addr( packet, & src, & dest ); |
554 | i = packet_get_addr( packet, & src, & dest ); |
554 | if( i < 0 ) return i; |
555 | if( i < 0 ) return i; |
555 | if( i != ETH_ADDR ) return EINVAL; |
556 | if( i != ETH_ADDR ) return EINVAL; |
556 | length = packet_get_data_length( packet ); |
557 | length = packet_get_data_length( packet ); |
557 | if( length > mtu ) return EINVAL; |
558 | if( length > mtu ) return EINVAL; |
558 | if( length < ETH_MIN_TAGGED_CONTENT( flags )){ |
559 | if( length < ETH_MIN_TAGGED_CONTENT( flags )){ |
559 | padding = packet_suffix( packet, ETH_MIN_TAGGED_CONTENT( flags ) - length ); |
560 | padding = packet_suffix( packet, ETH_MIN_TAGGED_CONTENT( flags ) - length ); |
560 | if( ! padding ) return ENOMEM; |
561 | if( ! padding ) return ENOMEM; |
561 | bzero( padding, ETH_MIN_TAGGED_CONTENT( flags ) - length ); |
562 | bzero( padding, ETH_MIN_TAGGED_CONTENT( flags ) - length ); |
562 | } |
563 | } |
563 | if( IS_DUMMY( flags )){ |
564 | if( IS_DUMMY( flags )){ |
564 | preamble = PACKET_PREFIX( packet, eth_preamble_t ); |
565 | preamble = PACKET_PREFIX( packet, eth_preamble_t ); |
565 | if( ! preamble ) return ENOMEM; |
566 | if( ! preamble ) return ENOMEM; |
566 | for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE; |
567 | for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE; |
567 | preamble->sfd = ETH_SFD; |
568 | preamble->sfd = ETH_SFD; |
568 | } |
569 | } |
569 | // TODO LSAP only device |
570 | // TODO LSAP only device |
570 | if( IS_DIX( flags ) || IS_8023_2_LSAP( flags )){ |
571 | if( IS_DIX( flags ) || IS_8023_2_LSAP( flags )){ |
571 | header_dix = PACKET_PREFIX( packet, eth_header_t ); |
572 | header_dix = PACKET_PREFIX( packet, eth_header_t ); |
572 | if( ! header_dix ) return ENOMEM; |
573 | if( ! header_dix ) return ENOMEM; |
573 | header_dix->ethertype = ( uint16_t ) ethertype; |
574 | header_dix->ethertype = ( uint16_t ) ethertype; |
574 | memcpy( header_dix->src, src_addr, ETH_ADDR ); |
575 | memcpy( header_dix->src, src_addr, ETH_ADDR ); |
575 | memcpy( header_dix->dest, dest, ETH_ADDR ); |
576 | memcpy( header_dix->dest, dest, ETH_ADDR ); |
576 | src = & header_dix->dest[ 0 ]; |
577 | src = & header_dix->dest[ 0 ]; |
577 | }else if( IS_8023_2_SNAP( flags )){ |
578 | }else if( IS_8023_2_SNAP( flags )){ |
578 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
579 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
579 | if( ! header ) return ENOMEM; |
580 | if( ! header ) return ENOMEM; |
580 | header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )); |
581 | header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )); |
581 | header->lsap.dsap = ( uint16_t ) ETH_LSAP_SNAP; |
582 | header->lsap.dsap = ( uint16_t ) ETH_LSAP_SNAP; |
582 | header->lsap.ssap = header->lsap.dsap; |
583 | header->lsap.ssap = header->lsap.dsap; |
583 | header->lsap.ctrl = 0; |
584 | header->lsap.ctrl = 0; |
584 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
585 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
585 | header->snap.ethertype = ( uint16_t ) ethertype; |
586 | header->snap.ethertype = ( uint16_t ) ethertype; |
586 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
587 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
587 | memcpy( header->header.dest, dest, ETH_ADDR ); |
588 | memcpy( header->header.dest, dest, ETH_ADDR ); |
588 | src = & header->header.dest[ 0 ]; |
589 | src = & header->header.dest[ 0 ]; |
589 | } |
590 | } |
590 | if( IS_DUMMY( flags )){ |
591 | if( IS_DUMMY( flags )){ |
591 | fcs = PACKET_SUFFIX( packet, eth_fcs_t ); |
592 | fcs = PACKET_SUFFIX( packet, eth_fcs_t ); |
592 | if( ! fcs ) return ENOMEM; |
593 | if( ! fcs ) return ENOMEM; |
593 | * fcs = htonl( ~ compute_crc32( ~ 0u, src, length * 8 )); |
594 | * fcs = htonl( ~ compute_crc32( ~ 0u, src, length * 8 )); |
594 | } |
595 | } |
595 | return EOK; |
596 | return EOK; |
596 | } |
597 | } |
597 | 598 | ||
598 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){ |
599 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){ |
599 | ERROR_DECLARE; |
600 | ERROR_DECLARE; |
600 | 601 | ||
601 | eth_device_ref device; |
602 | eth_device_ref device; |
602 | packet_t next; |
603 | packet_t next; |
603 | packet_t tmp; |
604 | packet_t tmp; |
604 | int ethertype; |
605 | int ethertype; |
605 | 606 | ||
606 | ethertype = htons( protocol_map( SERVICE_ETHERNET, sender )); |
607 | ethertype = htons( protocol_map( SERVICE_ETHERNET, sender )); |
607 | if( ! ethertype ){ |
608 | if( ! ethertype ){ |
608 | pq_release( eth_globals.net_phone, packet_get_id( packet )); |
609 | pq_release( eth_globals.net_phone, packet_get_id( packet )); |
609 | return EINVAL; |
610 | return EINVAL; |
610 | } |
611 | } |
611 | rwlock_read_lock( & eth_globals.devices_lock ); |
612 | fibril_rwlock_read_lock( & eth_globals.devices_lock ); |
612 | device = eth_devices_find( & eth_globals.devices, device_id ); |
613 | device = eth_devices_find( & eth_globals.devices, device_id ); |
613 | if( ! device ){ |
614 | if( ! device ){ |
614 | rwlock_read_unlock( & eth_globals.devices_lock ); |
615 | fibril_rwlock_read_unlock( & eth_globals.devices_lock ); |
615 | return ENOENT; |
616 | return ENOENT; |
616 | } |
617 | } |
617 | // process packet queue |
618 | // process packet queue |
618 | next = packet; |
619 | next = packet; |
619 | do{ |
620 | do{ |
620 | if( ERROR_OCCURRED( eth_prepare_packet( device->flags, next, ( uint8_t * ) device->addr->value, ethertype, device->mtu ))){ |
621 | if( ERROR_OCCURRED( eth_prepare_packet( device->flags, next, ( uint8_t * ) device->addr->value, ethertype, device->mtu ))){ |
621 | // release invalid packet |
622 | // release invalid packet |
622 | tmp = pq_detach( next ); |
623 | tmp = pq_detach( next ); |
623 | if( next == packet ) packet = tmp; |
624 | if( next == packet ) packet = tmp; |
624 | pq_release( eth_globals.net_phone, packet_get_id( next )); |
625 | pq_release( eth_globals.net_phone, packet_get_id( next )); |
625 | next = tmp; |
626 | next = tmp; |
626 | }else{ |
627 | }else{ |
627 | next = pq_next( next ); |
628 | next = pq_next( next ); |
628 | } |
629 | } |
629 | }while( next ); |
630 | }while( next ); |
630 | // send packet queue |
631 | // send packet queue |
631 | if( packet ){ |
632 | if( packet ){ |
632 | netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET ); |
633 | netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET ); |
633 | } |
634 | } |
634 | rwlock_read_unlock( & eth_globals.devices_lock ); |
635 | fibril_rwlock_read_unlock( & eth_globals.devices_lock ); |
635 | return EOK; |
636 | return EOK; |
636 | } |
637 | } |
637 | 638 | ||
638 | int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
639 | int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
639 | ERROR_DECLARE; |
640 | ERROR_DECLARE; |
640 | 641 | ||
641 | measured_string_ref address; |
642 | measured_string_ref address; |
642 | packet_t packet; |
643 | packet_t packet; |
643 | 644 | ||
644 | // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST ); |
645 | // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST ); |
645 | * answer_count = 0; |
646 | * answer_count = 0; |
646 | switch( IPC_GET_METHOD( * call )){ |
647 | switch( IPC_GET_METHOD( * call )){ |
647 | case IPC_M_PHONE_HUNGUP: |
648 | case IPC_M_PHONE_HUNGUP: |
648 | return EOK; |
649 | return EOK; |
649 | case NET_NIL_DEVICE: |
650 | case NET_NIL_DEVICE: |
650 | return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), NIL_GET_MTU( call )); |
651 | return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), NIL_GET_MTU( call )); |
651 | case NET_NIL_SEND: |
652 | case NET_NIL_SEND: |
652 | ERROR_PROPAGATE( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
653 | ERROR_PROPAGATE( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
653 | return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); |
654 | return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); |
654 | case NET_NIL_PACKET_SPACE: |
655 | case NET_NIL_PACKET_SPACE: |
655 | ERROR_PROPAGATE( eth_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer ))); |
656 | ERROR_PROPAGATE( eth_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer ))); |
656 | * answer_count = 4; |
657 | * answer_count = 4; |
657 | return EOK; |
658 | return EOK; |
658 | case NET_NIL_ADDR: |
659 | case NET_NIL_ADDR: |
659 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address )); |
660 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address )); |
660 | return measured_strings_reply( address, 1 ); |
661 | return measured_strings_reply( address, 1 ); |
661 | case NET_NIL_BROADCAST_ADDR: |
662 | case NET_NIL_BROADCAST_ADDR: |
662 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address )); |
663 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address )); |
663 | return measured_strings_reply( address, 1 ); |
664 | return measured_strings_reply( address, 1 ); |
664 | case IPC_M_CONNECT_TO_ME: |
665 | case IPC_M_CONNECT_TO_ME: |
665 | return nil_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call )); |
666 | return nil_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call )); |
666 | } |
667 | } |
667 | return ENOTSUP; |
668 | return ENOTSUP; |
668 | } |
669 | } |
669 | 670 | ||
670 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
671 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
671 | ERROR_DECLARE; |
672 | ERROR_DECLARE; |
672 | 673 | ||
673 | packet_t packet; |
674 | packet_t packet; |
674 | 675 | ||
675 | while( true ){ |
676 | while( true ){ |
676 | // printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST ); |
677 | // printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST ); |
677 | switch( IPC_GET_METHOD( * icall )){ |
678 | switch( IPC_GET_METHOD( * icall )){ |
678 | case NET_NIL_DEVICE_STATE: |
679 | case NET_NIL_DEVICE_STATE: |
679 | nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall )); |
680 | nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall )); |
680 | ipc_answer_0( iid, EOK ); |
681 | ipc_answer_0( iid, EOK ); |
681 | break; |
682 | break; |
682 | case NET_NIL_RECEIVED: |
683 | case NET_NIL_RECEIVED: |
683 | if( ! ERROR_OCCURRED( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){ |
684 | if( ! ERROR_OCCURRED( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){ |
684 | ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 ); |
685 | ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 ); |
685 | } |
686 | } |
686 | ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE ); |
687 | ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE ); |
687 | break; |
688 | break; |
688 | default: |
689 | default: |
689 | ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP ); |
690 | ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP ); |
690 | } |
691 | } |
691 | iid = async_get_call( icall ); |
692 | iid = async_get_call( icall ); |
692 | } |
693 | } |
693 | } |
694 | } |
694 | 695 | ||
695 | /** @} |
696 | /** @} |
696 | */ |
697 | */ |
697 | 698 |