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