Rev 4075 | Rev 4163 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4075 | Rev 4077 | ||
---|---|---|---|
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 <stdio.h> |
40 | #include <stdio.h> |
41 | #include <string.h> |
41 | #include <string.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 "../../netif/device.h" |
55 | #include "../../netif/device.h" |
56 | 56 | ||
57 | #include "../../structures/measured_strings.h" |
57 | #include "../../structures/measured_strings.h" |
58 | #include "../../structures/packet/packet.h" |
58 | #include "../../structures/packet/packet.h" |
59 | #include "../../structures/packet/packet_client.h" |
59 | #include "../../structures/packet/packet_client.h" |
60 | 60 | ||
61 | #include "eth.h" |
61 | #include "eth.h" |
62 | #include "eth_header.h" |
62 | #include "eth_header.h" |
63 | //#include "eth_messages.h" |
63 | //#include "eth_messages.h" |
64 | #include "eth_module.h" |
64 | #include "eth_module.h" |
65 | 65 | ||
66 | #define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )) |
66 | #define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )) |
67 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
67 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
68 | #define ETH_MAX_CONTENT 1500 |
68 | #define ETH_MAX_CONTENT 1500 |
69 | #define ETH_MIN_CONTENT 46 |
69 | #define ETH_MIN_CONTENT 46 |
70 | 70 | ||
71 | /** Returns the device identifier message parameter. |
71 | /** Returns the device identifier message parameter. |
72 | */ |
72 | */ |
73 | #define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call ) |
73 | #define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call ) |
74 | 74 | ||
75 | /** Returns the packet identifier message parameter. |
75 | /** Returns the packet identifier message parameter. |
76 | */ |
76 | */ |
77 | #define IPC_GET_PACKET( call ) ( packet_id_t ) IPC_GET_ARG2( * call ) |
77 | #define IPC_GET_PACKET( call ) ( packet_id_t ) IPC_GET_ARG2( * call ) |
78 | 78 | ||
79 | /** Returns the protocol service message parameter. |
79 | /** Returns the protocol service message parameter. |
80 | */ |
80 | */ |
81 | #define IPC_GET_PROTO( call ) ( services_t ) IPC_GET_ARG1( * call ) |
81 | #define IPC_GET_PROTO( call ) ( services_t ) IPC_GET_ARG1( * call ) |
82 | 82 | ||
83 | /** Returns the device driver service message parameter. |
83 | /** Returns the device driver service message parameter. |
84 | */ |
84 | */ |
85 | #define IPC_GET_SERVICE( call ) ( services_t ) IPC_GET_ARG2( * call ) |
85 | #define IPC_GET_SERVICE( call ) ( services_t ) IPC_GET_ARG2( * call ) |
86 | 86 | ||
87 | #define IPC_GET_MTU( call ) ( size_t ) IPC_GET_ARG3( * call ) |
87 | #define IPC_GET_MTU( call ) ( size_t ) IPC_GET_ARG3( * call ) |
88 | 88 | ||
89 | #define IPC_GET_PHONE( call ) ( int ) IPC_GET_ARG5( * call ) |
89 | #define IPC_GET_PHONE( call ) ( int ) IPC_GET_ARG5( * call ) |
90 | 90 | ||
91 | #define IPC_SET_ADDR( answer ) (( size_t * ) & IPC_GET_ARG1( * answer )) |
91 | #define IPC_SET_ADDR( answer ) (( size_t * ) & IPC_GET_ARG1( * answer )) |
92 | #define IPC_SET_PREFIX( answer ) (( size_t * ) & IPC_GET_ARG2( * answer )) |
92 | #define IPC_SET_PREFIX( answer ) (( size_t * ) & IPC_GET_ARG2( * answer )) |
93 | #define IPC_SET_CONTENT( answer ) (( size_t * ) & IPC_GET_ARG3( * answer )) |
93 | #define IPC_SET_CONTENT( answer ) (( size_t * ) & IPC_GET_ARG3( * answer )) |
94 | #define IPC_SET_SUFFIX( answer ) (( size_t * ) & IPC_GET_ARG4( * answer )) |
94 | #define IPC_SET_SUFFIX( answer ) (( size_t * ) & IPC_GET_ARG4( * answer )) |
95 | 95 | ||
96 | typedef enum eth_addr_type eth_addr_type_t; |
96 | typedef enum eth_addr_type eth_addr_type_t; |
97 | typedef eth_addr_type_t * eth_addr_type_ref; |
97 | typedef eth_addr_type_t * eth_addr_type_ref; |
98 | 98 | ||
99 | enum eth_addr_type{ |
99 | enum eth_addr_type{ |
100 | ETH_LOCAL_ADDR, |
100 | ETH_LOCAL_ADDR, |
101 | ETH_BROADCAST_ADDR |
101 | ETH_BROADCAST_ADDR |
102 | }; |
102 | }; |
103 | 103 | ||
104 | /** Ethernet global data. |
104 | /** Ethernet global data. |
105 | */ |
105 | */ |
106 | eth_globals_t eth_globals; |
106 | eth_globals_t eth_globals; |
107 | 107 | ||
108 | /** Processes IPC messages from the registered device driver modules in an infinite loop. |
108 | /** Processes IPC messages from the registered device driver modules in an infinite loop. |
109 | * @param iid The message identifier. Input parameter. |
109 | * @param iid The message identifier. Input parameter. |
110 | * @param icall The message parameters. Input/output parameter. |
110 | * @param icall The message parameters. Input/output parameter. |
111 | */ |
111 | */ |
112 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
112 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
113 | 113 | ||
114 | DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t ) |
114 | DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t ) |
115 | 115 | ||
116 | INT_MAP_IMPLEMENT( eth_protos, eth_proto_t ) |
116 | INT_MAP_IMPLEMENT( eth_protos, eth_proto_t ) |
117 | 117 | ||
118 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ); |
118 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ); |
119 | int eth_receive_message( device_id_t device_id, packet_t packet ); |
119 | int eth_receive_message( device_id_t device_id, packet_t packet ); |
120 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); |
120 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); |
121 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ); |
121 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ); |
122 | int eth_register_message( services_t service, int phone ); |
122 | int eth_register_message( services_t service, int phone ); |
123 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ); |
123 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ); |
124 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ); |
124 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ); |
125 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
125 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
126 | eth_proto_ref eth_proccess_packet( packet_t packet ); |
126 | eth_proto_ref eth_proccess_packet( packet_t packet ); |
127 | int eth_prepare_packet( packet_t packet, uint8_t * src_addr, int ethertype ); |
127 | int eth_prepare_packet( packet_t packet, uint8_t * src_addr, int ethertype ); |
128 | 128 | ||
129 | int eth_initialize( void ){ |
129 | int eth_initialize( void ){ |
130 | ERROR_DECLARE; |
130 | ERROR_DECLARE; |
131 | 131 | ||
132 | rwlock_initialize( & eth_globals.devices_lock ); |
132 | rwlock_initialize( & eth_globals.devices_lock ); |
133 | rwlock_initialize( & eth_globals.protos_lock ); |
133 | rwlock_initialize( & eth_globals.protos_lock ); |
134 | rwlock_write_lock( & eth_globals.devices_lock ); |
134 | rwlock_write_lock( & eth_globals.devices_lock ); |
135 | rwlock_write_lock( & eth_globals.protos_lock ); |
135 | rwlock_write_lock( & eth_globals.protos_lock ); |
136 | eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR )); |
136 | eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR )); |
137 | if( ! eth_globals.broadcast_addr ) return ENOMEM; |
137 | if( ! eth_globals.broadcast_addr ) return ENOMEM; |
138 | ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices )); |
138 | ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices )); |
139 | if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){ |
139 | if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){ |
140 | eth_devices_destroy( & eth_globals.devices ); |
140 | eth_devices_destroy( & eth_globals.devices ); |
141 | return ERROR_CODE; |
141 | return ERROR_CODE; |
142 | } |
142 | } |
143 | rwlock_write_unlock( & eth_globals.protos_lock ); |
143 | rwlock_write_unlock( & eth_globals.protos_lock ); |
144 | rwlock_write_unlock( & eth_globals.devices_lock ); |
144 | rwlock_write_unlock( & eth_globals.devices_lock ); |
145 | return EOK; |
145 | return EOK; |
146 | } |
146 | } |
147 | 147 | ||
148 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){ |
148 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){ |
149 | ERROR_DECLARE; |
149 | ERROR_DECLARE; |
150 | 150 | ||
151 | aid_t message; |
151 | aid_t message; |
152 | ipc_call_t answer; |
152 | ipc_call_t answer; |
153 | eth_device_ref device; |
153 | eth_device_ref device; |
154 | int result; |
154 | int result; |
155 | 155 | ||
156 | rwlock_write_lock( & eth_globals.devices_lock ); |
156 | rwlock_write_lock( & eth_globals.devices_lock ); |
157 | // an existing device? |
157 | // an existing device? |
158 | device = eth_devices_find( & eth_globals.devices, device_id ); |
158 | device = eth_devices_find( & eth_globals.devices, device_id ); |
159 | if( device ){ |
159 | if( device ){ |
160 | if( device->service == service ){ |
160 | if( device->service == service ){ |
161 | // update mtu |
161 | // update mtu |
162 | device->mtu = mtu; |
162 | device->mtu = mtu; |
163 | rwlock_write_unlock( & eth_globals.devices_lock ); |
163 | rwlock_write_unlock( & eth_globals.devices_lock ); |
164 | return EOK; |
164 | return EOK; |
165 | }else{ |
165 | }else{ |
166 | rwlock_write_unlock( & eth_globals.devices_lock ); |
166 | rwlock_write_unlock( & eth_globals.devices_lock ); |
167 | return EEXIST; |
167 | return EEXIST; |
168 | } |
168 | } |
169 | }else{ |
169 | }else{ |
170 | // create a new device |
170 | // create a new device |
171 | device = ( eth_device_ref ) malloc( sizeof( eth_device_t )); |
171 | device = ( eth_device_ref ) malloc( sizeof( eth_device_t )); |
172 | if( ! device ) return ENOMEM; |
172 | if( ! device ) return ENOMEM; |
173 | device->device_id = device_id; |
173 | device->device_id = device_id; |
174 | device->service = service; |
174 | device->service = service; |
175 | device->mtu = mtu; |
175 | device->mtu = mtu; |
176 | // bind the device driver |
176 | // bind the device driver |
177 | device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver ); |
177 | device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver ); |
178 | // get hardware address |
178 | // get hardware address |
179 | message = async_send_1( device->phone, NET_NETIF_GET_ADDR, device->device_id, & answer ); |
179 | message = async_send_1( device->phone, NET_NETIF_GET_ADDR, device->device_id, & answer ); |
180 | if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){ |
180 | if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){ |
181 | rwlock_write_unlock( & eth_globals.devices_lock ); |
181 | rwlock_write_unlock( & eth_globals.devices_lock ); |
182 | free( device ); |
182 | free( device ); |
183 | async_wait_for( message, NULL ); |
183 | async_wait_for( message, NULL ); |
184 | return ERROR_CODE; |
184 | return ERROR_CODE; |
185 | } |
185 | } |
186 | async_wait_for( message, ( ipcarg_t * ) & result ); |
186 | async_wait_for( message, ( ipcarg_t * ) & result ); |
187 | if( ERROR_OCCURRED( result )){ |
187 | if( ERROR_OCCURRED( result )){ |
188 | rwlock_write_unlock( & eth_globals.devices_lock ); |
188 | rwlock_write_unlock( & eth_globals.devices_lock ); |
189 | free( device->addr ); |
189 | free( device->addr ); |
190 | free( device->addr_data ); |
190 | free( device->addr_data ); |
191 | free( device ); |
191 | free( device ); |
192 | return ERROR_CODE; |
192 | return ERROR_CODE; |
193 | } |
193 | } |
194 | // add to the cache |
194 | // add to the cache |
195 | if( ERROR_OCCURRED( eth_devices_add( & eth_globals.devices, device->device_id, device ))){ |
195 | if( ERROR_OCCURRED( eth_devices_add( & eth_globals.devices, device->device_id, device ))){ |
196 | rwlock_write_unlock( & eth_globals.devices_lock ); |
196 | rwlock_write_unlock( & eth_globals.devices_lock ); |
197 | free( device->addr ); |
197 | free( device->addr ); |
198 | free( device->addr_data ); |
198 | free( device->addr_data ); |
199 | free( device ); |
199 | free( device ); |
200 | return ERROR_CODE; |
200 | return ERROR_CODE; |
201 | } |
201 | } |
202 | } |
202 | } |
203 | rwlock_write_unlock( & eth_globals.devices_lock ); |
203 | rwlock_write_unlock( & eth_globals.devices_lock ); |
204 | return EOK; |
204 | return EOK; |
205 | } |
205 | } |
206 | 206 | ||
207 | eth_proto_ref eth_proccess_packet( packet_t packet ){ |
207 | eth_proto_ref eth_proccess_packet( packet_t packet ){ |
208 | ERROR_DECLARE; |
208 | ERROR_DECLARE; |
209 | 209 | ||
210 | eth_header_ex_ref header; |
210 | eth_header_ex_ref header; |
211 | size_t length; |
211 | size_t length; |
212 | int type; |
212 | int type; |
213 | size_t prefix; |
213 | size_t prefix; |
214 | size_t suffix; |
214 | size_t suffix; |
215 | eth_fcs_ref fcs; |
215 | eth_fcs_ref fcs; |
216 | 216 | ||
217 | length = packet_get_data_length( packet ); |
217 | length = packet_get_data_length( packet ); |
218 | if( length <= sizeof( eth_header_t ) + ETH_MIN_CONTENT + ETH_SUFFIX ) return NULL; |
218 | if( length <= sizeof( eth_header_t ) + ETH_MIN_CONTENT + ETH_SUFFIX ) return NULL; |
219 | header = ( eth_header_ex_ref ) packet_get_data( packet ); |
219 | header = ( eth_header_ex_ref ) packet_get_data( packet ); |
220 | type = ntohs( header->header.ethertype ); |
220 | type = ntohs( header->header.ethertype ); |
221 | if( type >= ETH_MIN_PROTO ){ |
221 | if( type >= ETH_MIN_PROTO ){ |
222 | // DIX Ethernet |
222 | // DIX Ethernet |
223 | prefix = sizeof( eth_header_t ); |
223 | prefix = sizeof( eth_header_t ); |
224 | suffix = sizeof( eth_fcs_t ); |
224 | suffix = sizeof( eth_fcs_t ); |
225 | fcs = (( void * ) header ) + length - suffix; |
225 | fcs = (( void * ) header ) + length - suffix; |
226 | }else if( type <= ETH_MAX_CONTENT ){ |
226 | }else if( type <= ETH_MAX_CONTENT ){ |
227 | // translate "LSAP" values |
227 | // translate "LSAP" values |
228 | if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){ |
228 | if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){ |
229 | // raw packet |
229 | // raw packet |
230 | // discard |
230 | // discard |
231 | return NULL; |
231 | return NULL; |
232 | }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){ |
232 | }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){ |
233 | // IEEE 802.3 + 802.2 + LSAP + SNAP |
233 | // IEEE 802.3 + 802.2 + LSAP + SNAP |
234 | // organization code not supported |
234 | // organization code not supported |
235 | type = ntohs( header->snap.ethertype ); |
235 | type = ntohs( header->snap.ethertype ); |
236 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t) + sizeof( eth_header_snap_t); |
236 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t) + sizeof( eth_header_snap_t); |
237 | }else{ |
237 | }else{ |
238 | // IEEE 802.3 + 802.2 LSAP |
238 | // IEEE 802.3 + 802.2 LSAP |
239 | type = lsap_map( header->lsap.dsap ); |
239 | type = lsap_map( header->lsap.dsap ); |
240 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t); |
240 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t); |
241 | } |
241 | } |
242 | suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0; |
242 | suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0; |
243 | fcs = (( void * ) header ) + prefix + type + suffix; |
243 | fcs = (( void * ) header ) + prefix + type + suffix; |
244 | suffix += length - prefix - type; |
244 | suffix += length - prefix - type; |
245 | }else{ |
245 | }else{ |
246 | // invalid length/type, should not occurr |
246 | // invalid length/type, should not occurr |
247 | return NULL; |
247 | return NULL; |
248 | } |
248 | } |
249 | // TODO compute crc with fcs to erase? |
249 | // TODO compute crc with fcs to erase? |
250 | if(( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )) != ( * fcs )){ |
250 | if(( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )) != ntohl( * fcs )){ |
251 | return NULL; |
251 | return NULL; |
252 | } |
252 | } |
253 | if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR )) |
253 | if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR )) |
254 | || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){ |
254 | || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){ |
255 | return NULL; |
255 | return NULL; |
256 | } |
256 | } |
257 | return eth_protos_find( & eth_globals.protos, type ); |
257 | return eth_protos_find( & eth_globals.protos, type ); |
258 | } |
258 | } |
259 | 259 | ||
260 | int eth_receive_message( device_id_t device_id, packet_t packet ){ |
260 | int eth_receive_message( device_id_t device_id, packet_t packet ){ |
261 | eth_proto_ref proto; |
261 | eth_proto_ref proto; |
262 | packet_t next; |
262 | packet_t next; |
263 | 263 | ||
264 | rwlock_read_lock( & eth_globals.protos_lock ); |
264 | rwlock_read_lock( & eth_globals.protos_lock ); |
265 | do{ |
265 | do{ |
266 | next = pq_detach( packet ); |
266 | next = pq_detach( packet ); |
267 | proto = eth_proccess_packet( packet ); |
267 | proto = eth_proccess_packet( packet ); |
268 | if( proto ){ |
268 | if( proto ){ |
269 | async_msg_2( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet )); |
269 | async_msg_2( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet )); |
270 | }else{ |
270 | }else{ |
271 | // drop invalid/unknown |
271 | // drop invalid/unknown |
272 | packet_release( eth_globals.networking_phone, packet_get_id( packet )); |
272 | packet_release( eth_globals.networking_phone, packet_get_id( packet )); |
273 | } |
273 | } |
274 | packet = next; |
274 | packet = next; |
275 | }while( packet ); |
275 | }while( packet ); |
276 | rwlock_read_unlock( & eth_globals.protos_lock ); |
276 | rwlock_read_unlock( & eth_globals.protos_lock ); |
277 | return EOK; |
277 | return EOK; |
278 | } |
278 | } |
279 | 279 | ||
280 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ |
280 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ |
281 | eth_device_ref device; |
281 | eth_device_ref device; |
282 | 282 | ||
283 | if( !( addr_len && prefix && content && suffix )) return EINVAL; |
283 | if( !( addr_len && prefix && content && suffix )) return EINVAL; |
284 | rwlock_write_lock( & eth_globals.devices_lock ); |
284 | rwlock_write_lock( & eth_globals.devices_lock ); |
285 | device = eth_devices_find( & eth_globals.devices, device_id ); |
285 | device = eth_devices_find( & eth_globals.devices, device_id ); |
286 | if( ! device ){ |
286 | if( ! device ){ |
287 | rwlock_write_unlock( & eth_globals.devices_lock ); |
287 | rwlock_write_unlock( & eth_globals.devices_lock ); |
288 | return ENOENT; |
288 | return ENOENT; |
289 | } |
289 | } |
290 | * content = ( ETH_MAX_CONTENT > device->mtu ) ? device->mtu : ETH_MAX_CONTENT; |
290 | * content = ( ETH_MAX_CONTENT > device->mtu ) ? device->mtu : ETH_MAX_CONTENT; |
291 | rwlock_write_unlock( & eth_globals.devices_lock ); |
291 | rwlock_write_unlock( & eth_globals.devices_lock ); |
292 | * addr_len = ETH_ADDR; |
292 | * addr_len = ETH_ADDR; |
293 | * prefix = ETH_PREFIX; |
293 | * prefix = ETH_PREFIX; |
294 | * suffix = ETH_MIN_CONTENT + ETH_SUFFIX; |
294 | * suffix = ETH_MIN_CONTENT + ETH_SUFFIX; |
295 | return EOK; |
295 | return EOK; |
296 | } |
296 | } |
297 | 297 | ||
298 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){ |
298 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){ |
299 | eth_device_ref device; |
299 | eth_device_ref device; |
300 | 300 | ||
301 | if( ! address ) return EINVAL; |
301 | if( ! address ) return EINVAL; |
302 | if( type == ETH_BROADCAST_ADDR ){ |
302 | if( type == ETH_BROADCAST_ADDR ){ |
303 | * address = eth_globals.broadcast_addr; |
303 | * address = eth_globals.broadcast_addr; |
304 | }else{ |
304 | }else{ |
305 | rwlock_write_lock( & eth_globals.devices_lock ); |
305 | rwlock_write_lock( & eth_globals.devices_lock ); |
306 | device = eth_devices_find( & eth_globals.devices, device_id ); |
306 | device = eth_devices_find( & eth_globals.devices, device_id ); |
307 | if( ! device ){ |
307 | if( ! device ){ |
308 | rwlock_write_unlock( & eth_globals.devices_lock ); |
308 | rwlock_write_unlock( & eth_globals.devices_lock ); |
309 | return ENOENT; |
309 | return ENOENT; |
310 | } |
310 | } |
311 | * address = device->addr; |
311 | * address = device->addr; |
312 | rwlock_write_unlock( & eth_globals.devices_lock ); |
312 | rwlock_write_unlock( & eth_globals.devices_lock ); |
313 | } |
313 | } |
314 | return ( * address ) ? EOK : ENOENT; |
314 | return ( * address ) ? EOK : ENOENT; |
315 | } |
315 | } |
316 | 316 | ||
317 | int eth_register_message( services_t service, int phone ){ |
317 | int eth_register_message( services_t service, int phone ){ |
318 | ERROR_DECLARE; |
318 | ERROR_DECLARE; |
319 | 319 | ||
320 | eth_proto_ref proto; |
320 | eth_proto_ref proto; |
321 | int protocol; |
321 | int protocol; |
322 | 322 | ||
323 | protocol = protocol_map( SERVICE_ETHERNET, service ); |
323 | protocol = protocol_map( SERVICE_ETHERNET, service ); |
324 | if( ! protocol ) return ENOENT; |
324 | if( ! protocol ) return ENOENT; |
325 | rwlock_write_lock( & eth_globals.protos_lock ); |
325 | rwlock_write_lock( & eth_globals.protos_lock ); |
326 | proto = eth_protos_find( & eth_globals.protos, protocol ); |
326 | proto = eth_protos_find( & eth_globals.protos, protocol ); |
327 | if( proto ){ |
327 | if( proto ){ |
328 | proto->phone = phone; |
328 | proto->phone = phone; |
329 | rwlock_write_unlock( & eth_globals.protos_lock ); |
329 | rwlock_write_unlock( & eth_globals.protos_lock ); |
330 | return EOK; |
330 | return EOK; |
331 | }else{ |
331 | }else{ |
332 | proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t )); |
332 | proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t )); |
333 | if( ! proto ){ |
333 | if( ! proto ){ |
334 | rwlock_write_unlock( & eth_globals.protos_lock ); |
334 | rwlock_write_unlock( & eth_globals.protos_lock ); |
335 | return ENOMEM; |
335 | return ENOMEM; |
336 | } |
336 | } |
337 | proto->service = service; |
337 | proto->service = service; |
338 | proto->protocol = protocol; |
338 | proto->protocol = protocol; |
339 | proto->phone = phone; |
339 | proto->phone = phone; |
340 | if( ERROR_OCCURRED( eth_protos_add( & eth_globals.protos, protocol, proto ))){ |
340 | if( ERROR_OCCURRED( eth_protos_add( & eth_globals.protos, protocol, proto ))){ |
341 | rwlock_write_unlock( & eth_globals.protos_lock ); |
341 | rwlock_write_unlock( & eth_globals.protos_lock ); |
342 | free( proto ); |
342 | free( proto ); |
343 | return ERROR_CODE; |
343 | return ERROR_CODE; |
344 | } |
344 | } |
345 | } |
345 | } |
346 | rwlock_write_unlock( & eth_globals.protos_lock ); |
346 | rwlock_write_unlock( & eth_globals.protos_lock ); |
347 | return EOK; |
347 | return EOK; |
348 | } |
348 | } |
349 | 349 | ||
350 | int eth_prepare_packet( packet_t packet, uint8_t * src_addr, int ethertype ){ |
350 | int eth_prepare_packet( packet_t packet, uint8_t * src_addr, int ethertype ){ |
351 | eth_header_ex_ref header; |
351 | eth_header_ex_ref header; |
352 | eth_fcs_ref fcs; |
352 | eth_fcs_ref fcs; |
353 | uint8_t * src; |
353 | uint8_t * src; |
354 | uint8_t * dest; |
354 | uint8_t * dest; |
355 | int length; |
355 | int length; |
356 | int i; |
356 | int i; |
357 | void * padding; |
357 | void * padding; |
358 | 358 | ||
359 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
359 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
360 | if( ! header ) return ENOMEM; |
360 | if( ! header ) return ENOMEM; |
361 | for( i = 0; i < 7; ++ i ) header->header.preamble[ i ] = ETH_PREAMBLE; |
361 | for( i = 0; i < 7; ++ i ) header->header.preamble[ i ] = ETH_PREAMBLE; |
362 | header->header.sfd = ETH_SFD; |
362 | header->header.sfd = ETH_SFD; |
363 | length = packet_get_addr( packet, & src, & dest ); |
363 | length = packet_get_addr( packet, & src, & dest ); |
364 | if( length < 0 ) return length; |
364 | if( length < 0 ) return length; |
365 | if( length < ETH_ADDR ) return EINVAL; |
365 | if( length < ETH_ADDR ) return EINVAL; |
366 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
366 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
367 | memcpy( & header->header.dest, dest, ETH_ADDR ); |
367 | memcpy( & header->header.dest, dest, ETH_ADDR ); |
368 | length = packet_get_data_length( packet ); |
368 | length = packet_get_data_length( packet ); |
369 | if( length > ETH_MAX_CONTENT ) return EINVAL; |
369 | if( length > ETH_MAX_CONTENT ) return EINVAL; |
370 | if( length < ETH_MIN_CONTENT ){ |
370 | if( length < ETH_MIN_CONTENT ){ |
371 | padding = packet_suffix( packet, ETH_MIN_CONTENT - length ); |
371 | padding = packet_suffix( packet, ETH_MIN_CONTENT - length ); |
372 | if( ! padding ) return ENOMEM; |
372 | if( ! padding ) return ENOMEM; |
373 | memset( padding, 0, ETH_MIN_CONTENT - length ); |
373 | memset( padding, 0, ETH_MIN_CONTENT - length ); |
374 | } |
374 | } |
375 | header->header.ethertype = htons( length ); |
375 | header->header.ethertype = htons( length ); |
376 | header->lsap.dsap = 0xAA; |
376 | header->lsap.dsap = 0xAA; |
377 | header->lsap.ssap = header->lsap.dsap; |
377 | header->lsap.ssap = header->lsap.dsap; |
378 | header->lsap.ctrl = 0; |
378 | header->lsap.ctrl = 0; |
379 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
379 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
380 | header->snap.ethertype = ethertype; |
380 | header->snap.ethertype = ethertype; |
381 | fcs = PACKET_SUFFIX( packet, eth_fcs_t ); |
381 | fcs = PACKET_SUFFIX( packet, eth_fcs_t ); |
382 | if( ! fcs ) return ENOMEM; |
382 | if( ! fcs ) return ENOMEM; |
383 | * fcs = ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 ); |
383 | * fcs = htonl( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )); |
384 | return EOK; |
384 | return EOK; |
385 | } |
385 | } |
386 | 386 | ||
387 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){ |
387 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){ |
388 | ERROR_DECLARE; |
388 | ERROR_DECLARE; |
389 | 389 | ||
390 | eth_device_ref device; |
390 | eth_device_ref device; |
391 | packet_t next; |
391 | packet_t next; |
392 | packet_t tmp; |
392 | packet_t tmp; |
393 | int ethertype; |
393 | int ethertype; |
394 | 394 | ||
395 | ethertype = htons( protocol_map( SERVICE_ETHERNET, sender )); |
395 | ethertype = htons( protocol_map( SERVICE_ETHERNET, sender )); |
396 | if( ! ethertype ){ |
396 | if( ! ethertype ){ |
397 | packet_release( eth_globals.networking_phone, packet_get_id( packet )); |
397 | packet_release( eth_globals.networking_phone, packet_get_id( packet )); |
398 | return EINVAL; |
398 | return EINVAL; |
399 | } |
399 | } |
400 | rwlock_read_lock( & eth_globals.devices_lock ); |
400 | rwlock_read_lock( & eth_globals.devices_lock ); |
401 | device = eth_devices_find( & eth_globals.devices, device_id ); |
401 | device = eth_devices_find( & eth_globals.devices, device_id ); |
402 | if( ! device ){ |
402 | if( ! device ){ |
403 | rwlock_read_unlock( & eth_globals.devices_lock ); |
403 | rwlock_read_unlock( & eth_globals.devices_lock ); |
404 | return ENOENT; |
404 | return ENOENT; |
405 | } |
405 | } |
406 | // proccess packet queue |
406 | // proccess packet queue |
407 | next = packet; |
407 | next = packet; |
408 | do{ |
408 | do{ |
409 | if( ERROR_OCCURRED( eth_prepare_packet( next, ( uint8_t * ) device->addr->value, ethertype ))){ |
409 | if( ERROR_OCCURRED( eth_prepare_packet( next, ( uint8_t * ) device->addr->value, ethertype ))){ |
410 | // release invalid packet |
410 | // release invalid packet |
411 | tmp = pq_detach( next ); |
411 | tmp = pq_detach( next ); |
412 | packet_release( eth_globals.networking_phone, packet_get_id( next )); |
412 | packet_release( eth_globals.networking_phone, packet_get_id( next )); |
413 | next = tmp; |
413 | next = tmp; |
414 | }else{ |
414 | }else{ |
415 | next = pq_next( next ); |
415 | next = pq_next( next ); |
416 | } |
416 | } |
417 | }while( next ); |
417 | }while( next ); |
418 | // send packet queue |
418 | // send packet queue |
419 | async_msg_2( device->phone, NET_NETIF_SEND, device_id, packet_get_id( packet )); |
419 | async_msg_2( device->phone, NET_NETIF_SEND, device_id, packet_get_id( packet )); |
420 | rwlock_read_unlock( & eth_globals.devices_lock ); |
420 | rwlock_read_unlock( & eth_globals.devices_lock ); |
421 | return EOK; |
421 | return EOK; |
422 | } |
422 | } |
423 | 423 | ||
424 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
424 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
425 | ERROR_DECLARE; |
425 | ERROR_DECLARE; |
426 | 426 | ||
427 | measured_string_ref address; |
427 | measured_string_ref address; |
428 | packet_t packet; |
428 | packet_t packet; |
429 | 429 | ||
430 | * answer_count = 0; |
430 | * answer_count = 0; |
431 | switch( IPC_GET_METHOD( * call )){ |
431 | switch( IPC_GET_METHOD( * call )){ |
432 | case IPC_M_PHONE_HUNGUP: |
432 | case IPC_M_PHONE_HUNGUP: |
433 | return EOK; |
433 | return EOK; |
434 | case NET_NIL_DEVICE: |
434 | case NET_NIL_DEVICE: |
435 | return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call )); |
435 | return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call )); |
436 | case NET_NIL_SEND: |
436 | case NET_NIL_SEND: |
437 | ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call ))); |
437 | ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call ))); |
438 | return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); |
438 | return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); |
439 | case NET_NIL_PACKET_SPACE: |
439 | case NET_NIL_PACKET_SPACE: |
440 | 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 ))); |
440 | 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 ))); |
441 | * answer_count = 3; |
441 | * answer_count = 3; |
442 | return EOK; |
442 | return EOK; |
443 | case NET_NIL_ADDR: |
443 | case NET_NIL_ADDR: |
444 | rwlock_read_lock( & eth_globals.devices_lock ); |
444 | rwlock_read_lock( & eth_globals.devices_lock ); |
445 | if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ))){ |
445 | if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ))){ |
446 | ERROR_CODE = measured_strings_reply( address, 1 ); |
446 | ERROR_CODE = measured_strings_reply( address, 1 ); |
447 | } |
447 | } |
448 | rwlock_read_unlock( & eth_globals.devices_lock ); |
448 | rwlock_read_unlock( & eth_globals.devices_lock ); |
449 | return ERROR_CODE; |
449 | return ERROR_CODE; |
450 | case NET_NIL_BROADCAST_ADDR: |
450 | case NET_NIL_BROADCAST_ADDR: |
451 | rwlock_read_lock( & eth_globals.devices_lock ); |
451 | rwlock_read_lock( & eth_globals.devices_lock ); |
452 | if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ))){ |
452 | if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ))){ |
453 | ERROR_CODE = measured_strings_reply( address, 1 ); |
453 | ERROR_CODE = measured_strings_reply( address, 1 ); |
454 | } |
454 | } |
455 | rwlock_read_unlock( & eth_globals.devices_lock ); |
455 | rwlock_read_unlock( & eth_globals.devices_lock ); |
456 | return ERROR_CODE; |
456 | return ERROR_CODE; |
457 | case IPC_M_CONNECT_TO_ME: |
457 | case IPC_M_CONNECT_TO_ME: |
458 | return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call )); |
458 | return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call )); |
459 | } |
459 | } |
460 | return ENOTSUP; |
460 | return ENOTSUP; |
461 | } |
461 | } |
462 | 462 | ||
463 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
463 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
464 | ERROR_DECLARE; |
464 | ERROR_DECLARE; |
465 | 465 | ||
466 | packet_t packet; |
466 | packet_t packet; |
467 | 467 | ||
468 | while( true ){ |
468 | while( true ){ |
469 | switch( IPC_GET_METHOD( * icall )){ |
469 | switch( IPC_GET_METHOD( * icall )){ |
470 | case NET_NIL_DEVICE_STATE: |
470 | case NET_NIL_DEVICE_STATE: |
471 | //TODO clear device if off? |
471 | //TODO clear device if off? |
472 | break; |
472 | break; |
473 | case NET_NIL_RECEIVED: |
473 | case NET_NIL_RECEIVED: |
474 | if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){ |
474 | if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){ |
475 | ERROR_CODE = eth_receive_message( IPC_GET_DEVICE( icall ), packet ); |
475 | ERROR_CODE = eth_receive_message( IPC_GET_DEVICE( icall ), packet ); |
476 | } |
476 | } |
477 | ipc_answer_0( iid, ERROR_CODE ); |
477 | ipc_answer_0( iid, ERROR_CODE ); |
478 | break; |
478 | break; |
479 | default: |
479 | default: |
480 | ipc_answer_0( iid, ENOTSUP ); |
480 | ipc_answer_0( iid, ENOTSUP ); |
481 | } |
481 | } |
482 | iid = async_get_call( icall ); |
482 | iid = async_get_call( icall ); |
483 | } |
483 | } |
484 | } |
484 | } |
485 | 485 | ||
486 | /** @} |
486 | /** @} |
487 | */ |
487 | */ |
488 | 488 |