Subversion Repositories HelenOS

Rev

Rev 4163 | Rev 4243 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4163 Rev 4192
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 arp
29
/** @addtogroup arp
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  ARP module implementation.
34
 *  ARP module implementation.
35
 *  @see arp.h
35
 *  @see arp.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 <rwlock.h>
41
#include <rwlock.h>
41
#include <stdio.h>
42
#include <stdio.h>
42
#include <string.h>
-
 
43
 
43
 
44
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
45
#include <ipc/services.h>
46
 
46
 
47
#include "../../err.h"
47
#include "../../err.h"
48
#include "../../messages.h"
48
#include "../../messages.h"
49
#include "../../modules.h"
49
#include "../../modules.h"
50
 
50
 
51
#include "../../include/protocol_map.h"
51
#include "../../include/protocol_map.h"
52
#include "../../netif/device.h"
52
#include "../../netif/device.h"
53
 
53
 
54
#include "../../structures/measured_strings.h"
54
#include "../../structures/measured_strings.h"
55
#include "../../structures/packet/packet.h"
55
#include "../../structures/packet/packet.h"
56
#include "../../structures/packet/packet_client.h"
56
#include "../../structures/packet/packet_client.h"
57
 
57
 
58
#include "arp.h"
58
#include "arp.h"
59
#include "arp_header.h"
59
#include "arp_header.h"
60
#include "arp_oc.h"
60
#include "arp_oc.h"
61
//#include "arp_messages.h"
61
//#include "arp_messages.h"
62
#include "arp_module.h"
62
#include "arp_module.h"
63
 
63
 
64
/** Returns the device identifier message parameter.
64
/** Returns the device identifier message parameter.
65
 */
65
 */
66
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
66
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
67
 
67
 
68
/** Returns the packet identifier message parameter.
68
/** Returns the packet identifier message parameter.
69
 */
69
 */
70
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG2( * call )
70
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG2( * call )
71
 
71
 
72
/** Returns the protocol service message parameter.
72
/** Returns the protocol service message parameter.
73
 */
73
 */
74
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
74
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
75
 
75
 
76
/** Returns the device driver service message parameter.
76
/** Returns the device driver service message parameter.
77
 */
77
 */
78
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG3( * call )
78
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG3( * call )
79
 
79
 
80
/** ARP global data.
80
/** ARP global data.
81
 */
81
 */
82
arp_globals_t   arp_globals;
82
arp_globals_t   arp_globals;
83
 
83
 
84
/** Creates new protocol specific data.
84
/** Creates new protocol specific data.
85
 *  @param proto Protocol specific data. Output parameter.
85
 *  @param proto Protocol specific data. Output parameter.
86
 *  @param service Protocol module service. Input parameter.
86
 *  @param service Protocol module service. Input parameter.
87
 *  @param address Actual protocol device address. Input parameter.
87
 *  @param address Actual protocol device address. Input parameter.
88
 *  @returns EOK on success.
88
 *  @returns EOK on success.
89
 *  @returns ENOMEM if there is not enough memory left.
89
 *  @returns ENOMEM if there is not enough memory left.
90
 */
90
 */
91
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
91
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
92
 
92
 
93
/** Registers the device.
93
/** Registers the device.
94
 *  Creates new device entry in the cache or updates the protocol address if the device with the device identifier and the driver service exists.
94
 *  Creates new device entry in the cache or updates the protocol address if the device with the device identifier and the driver service exists.
95
 *  @param device_id The device identifier. Input parameter.
95
 *  @param device_id The device identifier. Input parameter.
96
 *  @param service The device driver service. Input parameter.
96
 *  @param service The device driver service. Input parameter.
97
 *  @param protocol The protocol service. Input parameter.
97
 *  @param protocol The protocol service. Input parameter.
98
 *  @param address The actual device protocol address.
98
 *  @param address The actual device protocol address.
99
 *  @returns EOK on success.
99
 *  @returns EOK on success.
100
 *  @returns EEXIST if another device with the same device identifier and different driver service exists.
100
 *  @returns EEXIST if another device with the same device identifier and different driver service exists.
101
 *  @returns ENOMEM if there is not enough memory left.
101
 *  @returns ENOMEM if there is not enough memory left.
102
 *  @returns Other error codes as defined for the measured_strings_return() function.
102
 *  @returns Other error codes as defined for the measured_strings_return() function.
103
 */
103
 */
104
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
104
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
105
 
105
 
106
/** Returns the hardware address for the given protocol address.
106
/** Returns the hardware address for the given protocol address.
107
 *  Sends the ARP request packet if the hardware address is not found in the cache.
107
 *  Sends the ARP request packet if the hardware address is not found in the cache.
108
 *  @param device_id The device identifier. Input parameter.
108
 *  @param device_id The device identifier. Input parameter.
109
 *  @param protocol The protocol service. Input parameter.
109
 *  @param protocol The protocol service. Input parameter.
110
 *  @param target The target protocol address. Input parameter.
110
 *  @param target The target protocol address. Input parameter.
111
 *  @returns The hardware address of the target.
111
 *  @returns The hardware address of the target.
112
 *  @returns NULL if the target parameter is NULL.
112
 *  @returns NULL if the target parameter is NULL.
113
 *  @returns NULL if the device is not found.
113
 *  @returns NULL if the device is not found.
114
 *  @returns NULL if the device packet is too small to send a&nbsp;request.
114
 *  @returns NULL if the device packet is too small to send a&nbsp;request.
115
 *  @returns NULL if the hardware address is not found in the cache.
115
 *  @returns NULL if the hardware address is not found in the cache.
116
 */
116
 */
117
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target );
117
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target );
118
 
118
 
119
/** Processes the received ARP packet.
119
/** Processes the received ARP packet.
120
 *  Updates the source hardware address if the source entry exists or the packet is targeted to my protocol address.
120
 *  Updates the source hardware address if the source entry exists or the packet is targeted to my protocol address.
121
 *  Responses to the ARP request if the packet is the ARP request and is targeted to my address.
121
 *  Responses to the ARP request if the packet is the ARP request and is targeted to my address.
122
 *  @param device_id The source device identifier. Input parameter.
122
 *  @param device_id The source device identifier. Input parameter.
123
 *  @param packet The received packet. Input/output parameter.
123
 *  @param packet The received packet. Input/output parameter.
124
 *  @returns EOK on success.
124
 *  @returns EOK on success.
125
 *  @returns EINVAL if the packet is too small to carry the ARP packet.
125
 *  @returns EINVAL if the packet is too small to carry the ARP packet.
126
 *  @returns EINVAL if the received address lengths differs from the registered values.
126
 *  @returns EINVAL if the received address lengths differs from the registered values.
127
 *  @returns ENOENT if the device is not found in the cache.
127
 *  @returns ENOENT if the device is not found in the cache.
128
 *  @returns ENOENT if the protocol for the device is not found in the cache.
128
 *  @returns ENOENT if the protocol for the device is not found in the cache.
129
 *  @returns ENOMEM if there is not enough memory left.
129
 *  @returns ENOMEM if there is not enough memory left.
130
 */
130
 */
131
int arp_receive_message( device_id_t device_id, packet_t packet );
131
int arp_receive_message( device_id_t device_id, packet_t packet );
132
 
132
 
133
/** Clears the device specific data from the cache.
133
/** Clears the device specific data from the cache.
134
 *  @param device_id The device identifier. Input parameter.
134
 *  @param device_id The device identifier. Input parameter.
135
 *  @returns EOK on success.
135
 *  @returns EOK on success.
136
 *  @returns ENOENT  if the device is not found in the cache.
136
 *  @returns ENOENT  if the device is not found in the cache.
137
 */
137
 */
138
int arp_clear_device_message( device_id_t device_id );
138
int arp_clear_device_message( device_id_t device_id );
139
 
139
 
140
/** Clears the device specific data.
140
/** Clears the device specific data.
141
 *  @param device The device specific data.
141
 *  @param device The device specific data.
142
 */
142
 */
143
void    clear_device( arp_device_ref device );
143
void    clear_device( arp_device_ref device );
144
 
144
 
145
/** Clears the whole cache.
145
/** Clears the whole cache.
146
 *  @returns EOK on success.
146
 *  @returns EOK on success.
147
 */
147
 */
148
int arp_clean_cache_message( void );
148
int arp_clean_cache_message( void );
149
 
149
 
150
/** Processes IPC messages from the registered device driver modules in an infinite loop.
150
/** Processes IPC messages from the registered device driver modules in an infinite loop.
151
 *  @param iid The message identifier. Input parameter.
151
 *  @param iid The message identifier. Input parameter.
152
 *  @param icall The message parameters. Input/output parameter.
152
 *  @param icall The message parameters. Input/output parameter.
153
 */
153
 */
154
void    arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
154
void    arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
155
 
155
 
156
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
156
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
157
 
157
 
158
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
158
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
159
 
159
 
160
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
160
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
161
 
161
 
162
int arp_initialize( void ){
162
int arp_initialize( void ){
163
    ERROR_DECLARE;
163
    ERROR_DECLARE;
164
 
164
 
165
    rwlock_initialize( & arp_globals.lock );
165
    rwlock_initialize( & arp_globals.lock );
166
    rwlock_write_lock( & arp_globals.lock );
166
    rwlock_write_lock( & arp_globals.lock );
167
    ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache ));
167
    ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache ));
168
    rwlock_write_unlock( & arp_globals.lock );
168
    rwlock_write_unlock( & arp_globals.lock );
169
    return EOK;
169
    return EOK;
170
}
170
}
171
 
171
 
172
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
172
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
173
    ERROR_DECLARE;
173
    ERROR_DECLARE;
174
 
174
 
175
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
175
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
176
    if( !( * proto )) return ENOMEM;
176
    if( !( * proto )) return ENOMEM;
177
    ( ** proto ).service = service;
177
    ( ** proto ).service = service;
178
    ( ** proto ).addr = address;
178
    ( ** proto ).addr = address;
179
    ( ** proto ).addr_data = address->value;
179
    ( ** proto ).addr_data = address->value;
180
    if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){
180
    if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){
181
        free( * proto );
181
        free( * proto );
182
        return ERROR_CODE;
182
        return ERROR_CODE;
183
    }
183
    }
184
    return EOK;
184
    return EOK;
185
}
185
}
186
 
186
 
187
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
187
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
188
    ERROR_DECLARE;
188
    ERROR_DECLARE;
189
 
189
 
190
    arp_device_ref  device;
190
    arp_device_ref  device;
191
    aid_t           message;
191
    aid_t           message;
192
    ipc_call_t      answer;
192
    ipc_call_t      answer;
193
    ipcarg_t        result;
193
    ipcarg_t        result;
194
    arp_proto_ref   proto;
194
    arp_proto_ref   proto;
-
 
195
    int             index;
195
 
196
 
196
    rwlock_write_lock( & arp_globals.lock );
197
    rwlock_write_lock( & arp_globals.lock );
197
    // an existing device?
198
    // an existing device?
198
    device = arp_cache_find( & arp_globals.cache, device_id );
199
    device = arp_cache_find( & arp_globals.cache, device_id );
199
    if( device ){
200
    if( device ){
200
        if( device->service != service ){
201
        if( device->service != service ){
201
            printf( "\nDevice %d already exists", device->device_id );
202
            printf( "\nDevice %d already exists", device->device_id );
202
            rwlock_write_unlock( & arp_globals.lock );
203
            rwlock_write_unlock( & arp_globals.lock );
203
            return EEXIST;
204
            return EEXIST;
204
        }
205
        }
205
        proto = arp_protos_find( & device->protos, protocol );
206
        proto = arp_protos_find( & device->protos, protocol );
206
        if( proto ){
207
        if( proto ){
207
            free( proto->addr );
208
            free( proto->addr );
208
            free( proto->addr_data );
209
            free( proto->addr_data );
209
            proto->addr = address;
210
            proto->addr = address;
210
            proto->addr_data = address->value;
211
            proto->addr_data = address->value;
211
        }else{
212
        }else{
212
            if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
213
            if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
213
                rwlock_write_unlock( & arp_globals.lock );
214
                rwlock_write_unlock( & arp_globals.lock );
214
                return ERROR_CODE;
215
                return ERROR_CODE;
215
            }
216
            }
216
            if( ERROR_OCCURRED( arp_protos_add( & device->protos, proto->service, proto ))){
217
            index = arp_protos_add( & device->protos, proto->service, proto );
-
 
218
            if( index < 0 ){
217
                rwlock_write_unlock( & arp_globals.lock );
219
                rwlock_write_unlock( & arp_globals.lock );
218
                free( proto );
220
                free( proto );
219
                return ERROR_CODE;
221
                return result;
220
            }
222
            }
221
        }
223
        }
222
        printf( "\nCache of the existing device %d cleaned", device->device_id );
224
        printf( "\nCache of the existing device %d cleaned", device->device_id );
223
    }else{
225
    }else{
-
 
226
        index = protocol_map( service, protocol );
-
 
227
        if( ! index ) return ENOENT;
224
        // create a new device
228
        // create a new device
225
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
229
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
226
        if( ! device ){
230
        if( ! device ){
227
            rwlock_write_unlock( & arp_globals.lock );
231
            rwlock_write_unlock( & arp_globals.lock );
228
            return ENOMEM;
232
            return ENOMEM;
229
        }
233
        }
-
 
234
        device->hardware = index;
230
        device->device_id = device_id;
235
        device->device_id = device_id;
231
        if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
236
        if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
232
        || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
237
        || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
233
            rwlock_write_unlock( & arp_globals.lock );
238
            rwlock_write_unlock( & arp_globals.lock );
234
            free( device );
239
            free( device );
235
            return ERROR_CODE;
240
            return ERROR_CODE;
236
        }
241
        }
237
        if( ERROR_OCCURRED( arp_protos_add( & device->protos, proto->service, proto ))){
242
        index = arp_protos_add( & device->protos, proto->service, proto );
-
 
243
        if( index < 0 ){
238
            rwlock_write_unlock( & arp_globals.lock );
244
            rwlock_write_unlock( & arp_globals.lock );
239
            arp_protos_destroy( & device->protos );
245
            arp_protos_destroy( & device->protos );
240
            free( device );
246
            free( device );
241
            return ERROR_CODE;
247
            return index;
242
        }
248
        }
243
        device->service = service;
249
        device->service = service;
244
        // bind the new one
250
        // bind the new one
245
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
251
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
-
 
252
        if( device->phone < 0 ){
-
 
253
            rwlock_write_unlock( & arp_globals.lock );
-
 
254
            arp_protos_destroy( & device->protos );
-
 
255
            free( device );
-
 
256
            return EREFUSED;
-
 
257
        }
246
        // get packet dimensions
258
        // get packet dimensions
247
        if( ERROR_OCCURRED( async_req_1_4( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
259
        if( ERROR_OCCURRED( async_req_1_4( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
248
            rwlock_write_unlock( & arp_globals.lock );
260
            rwlock_write_unlock( & arp_globals.lock );
249
            arp_protos_destroy( & device->protos );
261
            arp_protos_destroy( & device->protos );
250
            free( device );
262
            free( device );
251
            return ERROR_CODE;
263
            return ERROR_CODE;
252
        }
264
        }
253
        // get hardware address
265
        // get hardware address
254
        message = async_send_1( device->phone, NET_NIL_ADDR, device->device_id, & answer );
266
        message = async_send_1( device->phone, NET_NIL_ADDR, device->device_id, & answer );
255
        if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
267
        if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
256
            rwlock_write_unlock( & arp_globals.lock );
268
            rwlock_write_unlock( & arp_globals.lock );
257
            arp_protos_destroy( & device->protos );
269
            arp_protos_destroy( & device->protos );
258
            free( device );
270
            free( device );
259
            async_wait_for( message, NULL );
271
            async_wait_for( message, NULL );
260
            return ERROR_CODE;
272
            return ERROR_CODE;
261
        }
273
        }
262
        async_wait_for( message, & result );
274
        async_wait_for( message, & result );
263
        if( ERROR_OCCURRED( result )){
275
        if( ERROR_OCCURRED( result )){
264
            rwlock_write_unlock( & arp_globals.lock );
276
            rwlock_write_unlock( & arp_globals.lock );
265
            free( device->addr );
277
            free( device->addr );
266
            free( device->addr_data );
278
            free( device->addr_data );
267
            arp_protos_destroy( & device->protos );
279
            arp_protos_destroy( & device->protos );
268
            free( device );
280
            free( device );
269
            return ERROR_CODE;
281
            return ERROR_CODE;
270
        }
282
        }
271
        // get broadcast address
283
        // get broadcast address
272
        message = async_send_1( device->phone, NET_NIL_BROADCAST_ADDR, device->device_id, & answer );
284
        message = async_send_1( device->phone, NET_NIL_BROADCAST_ADDR, device->device_id, & answer );
273
        if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->broadcast_addr, & device->broadcast_data, 1 ))){
285
        if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->broadcast_addr, & device->broadcast_data, 1 ))){
274
            rwlock_write_unlock( & arp_globals.lock );
286
            rwlock_write_unlock( & arp_globals.lock );
275
            free( device->addr );
287
            free( device->addr );
276
            free( device->addr_data );
288
            free( device->addr_data );
277
            arp_protos_destroy( & device->protos );
289
            arp_protos_destroy( & device->protos );
278
            free( device );
290
            free( device );
279
            async_wait_for( message, NULL );
291
            async_wait_for( message, NULL );
280
            return ERROR_CODE;
292
            return ERROR_CODE;
281
        }
293
        }
282
        async_wait_for( message, & result );
294
        async_wait_for( message, & result );
283
        // add to the cache
295
        // add to the cache
284
        if( ERROR_OCCURRED( result )
296
        if( ERROR_OCCURRED( result )
285
        || ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
297
        || ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
286
            rwlock_write_unlock( & arp_globals.lock );
298
            rwlock_write_unlock( & arp_globals.lock );
287
            free( device->addr );
299
            free( device->addr );
288
            free( device->addr_data );
300
            free( device->addr_data );
289
            free( device->broadcast_addr );
301
            free( device->broadcast_addr );
290
            free( device->broadcast_data );
302
            free( device->broadcast_data );
291
            arp_protos_destroy( & device->protos );
303
            arp_protos_destroy( & device->protos );
292
            free( device );
304
            free( device );
293
            return ERROR_CODE;
305
            return ERROR_CODE;
294
        }
306
        }
295
        printf( "\nNew device registered:\n\tid\t= %d\n\ttype\t= %x\n\tservice\t= %d", device->device_id, device->hardware, device->service );
307
        printf( "\nNew device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d", device->device_id, device->hardware, device->service );
296
    }
308
    }
297
    rwlock_write_unlock( & arp_globals.lock );
309
    rwlock_write_unlock( & arp_globals.lock );
298
    return EOK;
310
    return EOK;
299
}
311
}
300
 
312
 
301
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
313
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
302
    arp_device_ref      device;
314
    arp_device_ref      device;
303
    arp_proto_ref       proto;
315
    arp_proto_ref       proto;
304
    measured_string_ref addr;
316
    measured_string_ref addr;
305
    size_t              length;
317
    size_t              length;
306
    packet_t            packet;
318
    packet_t            packet;
307
    arp_header_ref      header;
319
    arp_header_ref      header;
308
 
320
 
309
    if( ! target ) return NULL;
321
    if( ! target ) return NULL;
310
    rwlock_read_lock( & arp_globals.lock );
322
    rwlock_read_lock( & arp_globals.lock );
311
    device = arp_cache_find( & arp_globals.cache, device_id );
323
    device = arp_cache_find( & arp_globals.cache, device_id );
312
    if( ! device ){
324
    if( ! device ){
313
        rwlock_read_unlock( & arp_globals.lock );
325
        rwlock_read_unlock( & arp_globals.lock );
314
        return NULL;
326
        return NULL;
315
    }
327
    }
316
    proto = arp_protos_find( & device->protos, protocol );
328
    proto = arp_protos_find( & device->protos, protocol );
317
    if(( ! proto ) || ( proto->addr->length != target->length )){
329
    if(( ! proto ) || ( proto->addr->length != target->length )){
318
        rwlock_read_unlock( & arp_globals.lock );
330
        rwlock_read_unlock( & arp_globals.lock );
319
        return NULL;
331
        return NULL;
320
    }
332
    }
321
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
333
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
322
    if( addr ){
334
    if( addr ){
323
        rwlock_read_unlock( & arp_globals.lock );
335
        rwlock_read_unlock( & arp_globals.lock );
324
        return addr;
336
        return addr;
325
    }
337
    }
326
    // ARP packet content size = header + ( address + translation ) * 2
338
    // ARP packet content size = header + ( address + translation ) * 2
327
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
339
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
328
    if( length > device->content ){
340
    if( length > device->content ){
329
        rwlock_read_unlock( & arp_globals.lock );
341
        rwlock_read_unlock( & arp_globals.lock );
330
        return NULL;
342
        return NULL;
331
    }
343
    }
332
    packet = packet_get_4( arp_globals.networking_phone, device->addr_len, device->prefix, length, device->suffix );
344
    packet = packet_get_4( arp_globals.networking_phone, device->addr_len, device->prefix, length, device->suffix );
333
    if( ! packet ){
345
    if( ! packet ){
334
        rwlock_read_unlock( & arp_globals.lock );
346
        rwlock_read_unlock( & arp_globals.lock );
335
        return NULL;
347
        return NULL;
336
    }
348
    }
337
    header = ( arp_header_ref ) packet_suffix( packet, length );
349
    header = ( arp_header_ref ) packet_suffix( packet, length );
338
    header->hardware = device->hardware;
350
    header->hardware = device->hardware;
339
    header->hardware_length = device->addr->length;
351
    header->hardware_length = device->addr->length;
340
    header->protocol = protocol_map( device->service, protocol );
352
    header->protocol = protocol_map( device->service, protocol );
341
    header->protocol_length = proto->addr->length;
353
    header->protocol_length = proto->addr->length;
342
    header->operation = ARPOP_REQUEST;
354
    header->operation = ARPOP_REQUEST;
343
    length = sizeof( arp_header_t );
355
    length = sizeof( arp_header_t );
344
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
356
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
345
    length += device->addr->length;
357
    length += device->addr->length;
346
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
358
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
347
    length += proto->addr->length;
359
    length += proto->addr->length;
348
    memset((( uint8_t * ) header ) + length, 0, device->addr->length );
360
    bzero((( uint8_t * ) header ) + length, device->addr->length );
349
    length += device->addr->length;
361
    length += device->addr->length;
350
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
362
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
351
    packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length ));
363
    packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length ));
352
    async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet ));
364
    async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet ));
353
    rwlock_read_unlock( & arp_globals.lock );
365
    rwlock_read_unlock( & arp_globals.lock );
354
    return NULL;
366
    return NULL;
355
}
367
}
356
 
368
 
357
int arp_receive_message( device_id_t device_id, packet_t packet ){
369
int arp_receive_message( device_id_t device_id, packet_t packet ){
358
    ERROR_DECLARE;
370
    ERROR_DECLARE;
359
 
371
 
360
    size_t              length;
372
    size_t              length;
361
    arp_header_ref      header;
373
    arp_header_ref      header;
362
    arp_device_ref      device;
374
    arp_device_ref      device;
363
    arp_proto_ref       proto;
375
    arp_proto_ref       proto;
364
    measured_string_ref hw_source;
376
    measured_string_ref hw_source;
365
    uint8_t *           src_hw;
377
    uint8_t *           src_hw;
366
    uint8_t *           src_proto;
378
    uint8_t *           src_proto;
367
    uint8_t *           des_hw;
379
    uint8_t *           des_hw;
368
    uint8_t *           des_proto;
380
    uint8_t *           des_proto;
369
 
381
 
370
    length = packet_get_data_length( packet );
382
    length = packet_get_data_length( packet );
371
    if( length <= sizeof( arp_header_t )) return EINVAL;
383
    if( length <= sizeof( arp_header_t )) return EINVAL;
372
    rwlock_read_lock( & arp_globals.lock );
384
    rwlock_read_lock( & arp_globals.lock );
373
    device = arp_cache_find( & arp_globals.cache, device_id );
385
    device = arp_cache_find( & arp_globals.cache, device_id );
374
    if( ! device ){
386
    if( ! device ){
375
        rwlock_read_unlock( & arp_globals.lock );
387
        rwlock_read_unlock( & arp_globals.lock );
376
        return ENOENT;
388
        return ENOENT;
377
    }
389
    }
378
    header = ( arp_header_ref ) packet_get_data( packet );
390
    header = ( arp_header_ref ) packet_get_data( packet );
379
    if(( header->hardware != device->hardware )
391
    if(( header->hardware != device->hardware )
380
    || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){
392
    || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){
381
        rwlock_read_unlock( & arp_globals.lock );
393
        rwlock_read_unlock( & arp_globals.lock );
382
        return EINVAL;
394
        return EINVAL;
383
    }
395
    }
384
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
396
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
385
    if( ! proto ){
397
    if( ! proto ){
386
        rwlock_read_unlock( & arp_globals.lock );
398
        rwlock_read_unlock( & arp_globals.lock );
387
        return ENOENT;
399
        return ENOENT;
388
    }
400
    }
389
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
401
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
390
    src_proto = src_hw + header->hardware_length;
402
    src_proto = src_hw + header->hardware_length;
391
    des_hw = src_proto + header->protocol_length;
403
    des_hw = src_proto + header->protocol_length;
392
    des_proto = des_hw + header->hardware_length;
404
    des_proto = des_hw + header->hardware_length;
393
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
405
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
394
    // exists?
406
    // exists?
395
    if( hw_source ){
407
    if( hw_source ){
396
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
408
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
397
            rwlock_read_unlock( & arp_globals.lock );
409
            rwlock_read_unlock( & arp_globals.lock );
398
            return EINVAL;
410
            return EINVAL;
399
        }
411
        }
400
        memcpy( hw_source->value, src_hw, hw_source->length );
412
        memcpy( hw_source->value, src_hw, hw_source->length );
401
    }
413
    }
402
    // is my protocol address?
414
    // is my protocol address?
403
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
415
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
404
        rwlock_read_unlock( & arp_globals.lock );
416
        rwlock_read_unlock( & arp_globals.lock );
405
        return EINVAL;
417
        return EINVAL;
406
    }
418
    }
407
    if( ! strncmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
419
    if( ! strncmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
408
        // not already upadted?
420
        // not already upadted?
409
        if( ! hw_source ){
421
        if( ! hw_source ){
410
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
422
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
411
            if( ! hw_source ){
423
            if( ! hw_source ){
412
                rwlock_read_unlock( & arp_globals.lock );
424
                rwlock_read_unlock( & arp_globals.lock );
413
                return ENOMEM;
425
                return ENOMEM;
414
            }
426
            }
415
            if( ERROR_OCCURRED( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ))){
427
            if( ERROR_OCCURRED( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ))){
416
                rwlock_read_unlock( & arp_globals.lock );
428
                rwlock_read_unlock( & arp_globals.lock );
417
                return ERROR_CODE;
429
                return ERROR_CODE;
418
            }
430
            }
419
        }
431
        }
420
        if( header->operation == ARPOP_REQUEST ){
432
        if( header->operation == ARPOP_REQUEST ){
421
            header->operation = ARPOP_REPLY;
433
            header->operation = ARPOP_REPLY;
422
            memcpy( des_proto, src_proto, header->protocol_length );
434
            memcpy( des_proto, src_proto, header->protocol_length );
423
            memcpy( src_proto, proto->addr->value, header->protocol_length );
435
            memcpy( src_proto, proto->addr->value, header->protocol_length );
424
            memcpy( src_hw, des_hw, header->hardware_length );
436
            memcpy( src_hw, des_hw, header->hardware_length );
425
            memcpy( des_hw, hw_source->value, header->hardware_length );
437
            memcpy( des_hw, hw_source->value, header->hardware_length );
426
            packet_set_addr( packet, src_hw, des_hw, header->hardware_length );
438
            packet_set_addr( packet, src_hw, des_hw, header->hardware_length );
427
            async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet ));
439
            async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet ));
428
            rwlock_read_unlock( & arp_globals.lock );
440
            rwlock_read_unlock( & arp_globals.lock );
429
        }else{
441
        }else{
430
            rwlock_read_unlock( & arp_globals.lock );
442
            rwlock_read_unlock( & arp_globals.lock );
431
            packet_release( arp_globals.networking_phone, packet_get_id( packet ));
443
            pq_release( arp_globals.networking_phone, packet_get_id( packet ));
432
        }
444
        }
433
    }
445
    }
434
    return EOK;
446
    return EOK;
435
}
447
}
436
 
448
 
437
int arp_clear_device_message( device_id_t device_id ){
449
int arp_clear_device_message( device_id_t device_id ){
438
    arp_device_ref  device;
450
    arp_device_ref  device;
439
 
451
 
440
    rwlock_write_lock( & arp_globals.lock );
452
    rwlock_write_lock( & arp_globals.lock );
441
    device = arp_cache_find( & arp_globals.cache, device_id );
453
    device = arp_cache_find( & arp_globals.cache, device_id );
442
    if( ! device ){
454
    if( ! device ){
443
        rwlock_write_unlock( & arp_globals.lock );
455
        rwlock_write_unlock( & arp_globals.lock );
444
        return ENOENT;
456
        return ENOENT;
445
    }
457
    }
446
    clear_device( device );
458
    clear_device( device );
447
    printf( "\nDevice %d cleared", device_id );
459
    printf( "\nDevice %d cleared", device_id );
448
    rwlock_write_unlock( & arp_globals.lock );
460
    rwlock_write_unlock( & arp_globals.lock );
449
    return EOK;
461
    return EOK;
450
}
462
}
451
 
463
 
452
void clear_device( arp_device_ref device ){
464
void clear_device( arp_device_ref device ){
453
    int             count;
465
    int             count;
454
    arp_proto_ref   proto;
466
    arp_proto_ref   proto;
455
 
467
 
456
    count = arp_protos_count( & device->protos );
468
    for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){
457
    while( count > 0 ){
-
 
458
        proto = arp_protos_get_index( & device->protos, count );
469
        proto = arp_protos_get_index( & device->protos, count );
-
 
470
        if( proto ){
459
        if( proto->addr ) free( proto->addr );
471
            if( proto->addr ) free( proto->addr );
460
        if( proto->addr_data ) free( proto->addr_data );
472
            if( proto->addr_data ) free( proto->addr_data );
461
        arp_addr_destroy( & proto->addresses );
473
            arp_addr_destroy( & proto->addresses );
462
        -- count;
474
        }
463
    }
475
    }
464
    arp_protos_clear( & device->protos );
476
    arp_protos_clear( & device->protos );
465
}
477
}
466
 
478
 
467
int arp_clean_cache_message( void ){
479
int arp_clean_cache_message( void ){
468
    int             count;
480
    int             count;
469
    arp_device_ref  device;
481
    arp_device_ref  device;
470
 
482
 
471
    rwlock_write_lock( & arp_globals.lock );
483
    rwlock_write_lock( & arp_globals.lock );
472
    count = arp_cache_count( & arp_globals.cache );
484
    for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){
473
    while( count > 0 ){
-
 
474
        device = arp_cache_get_index( & arp_globals.cache, count );
485
        device = arp_cache_get_index( & arp_globals.cache, count );
475
        if( device ){
486
        if( device ){
476
            clear_device( device );
487
            clear_device( device );
477
            if( device->addr_data ) free( device->addr_data );
488
            if( device->addr_data ) free( device->addr_data );
478
            if( device->broadcast_data ) free( device->broadcast_data );
489
            if( device->broadcast_data ) free( device->broadcast_data );
479
        }
490
        }
480
    }
491
    }
481
    arp_cache_clear( & arp_globals.cache );
492
    arp_cache_clear( & arp_globals.cache );
482
    rwlock_write_unlock( & arp_globals.lock );
493
    rwlock_write_unlock( & arp_globals.lock );
483
    printf( "\nCache cleaned" );
494
    printf( "\nCache cleaned" );
484
    return EOK;
495
    return EOK;
485
}
496
}
486
 
497
 
487
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
498
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
488
    ERROR_DECLARE;
499
    ERROR_DECLARE;
489
 
500
 
490
    measured_string_ref address;
501
    measured_string_ref address;
491
    measured_string_ref translation;
502
    measured_string_ref translation;
492
    char *              data;
503
    char *              data;
493
 
504
 
494
    * answer_count = 0;
505
    * answer_count = 0;
495
    switch( IPC_GET_METHOD( * call )){
506
    switch( IPC_GET_METHOD( * call )){
496
        case IPC_M_PHONE_HUNGUP:
507
        case IPC_M_PHONE_HUNGUP:
497
            return EOK;
508
            return EOK;
498
        case NET_ARP_DEVICE:
509
        case NET_ARP_DEVICE:
499
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
510
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
500
            if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_PROTO( call ), address ))){
511
            if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_PROTO( call ), address ))){
501
                free( address );
512
                free( address );
502
                free( data );
513
                free( data );
503
            }
514
            }
504
            return ERROR_CODE;
515
            return ERROR_CODE;
505
        case NET_ARP_TRANSLATE:
516
        case NET_ARP_TRANSLATE:
506
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
517
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
507
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
518
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
508
            free( address );
519
            free( address );
509
            free( data );
520
            free( data );
510
            if( ! translation ) return ENOENT;
521
            if( ! translation ) return ENOENT;
511
            return measured_strings_reply( translation, 1 );
522
            return measured_strings_reply( translation, 1 );
512
        case NET_ARP_CLEAR_DEVICE:
523
        case NET_ARP_CLEAR_DEVICE:
513
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
524
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
514
        case NET_ARP_CLEAN_CACHE:
525
        case NET_ARP_CLEAN_CACHE:
515
            return arp_clean_cache_message();
526
            return arp_clean_cache_message();
516
    }
527
    }
517
    return ENOTSUP;
528
    return ENOTSUP;
518
}
529
}
519
 
530
 
520
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
531
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
521
    ERROR_DECLARE;
532
    ERROR_DECLARE;
522
 
533
 
523
    packet_t        packet;
534
    packet_t        packet;
524
 
535
 
525
    while( true ){
536
    while( true ){
526
        switch( IPC_GET_METHOD( * icall )){
537
        switch( IPC_GET_METHOD( * icall )){
527
            case NET_IL_DEVICE_STATE:
538
            case NET_IL_DEVICE_STATE:
528
                // do nothing - keep the cache
539
                // do nothing - keep the cache
529
                ipc_answer_0( iid, EOK );
540
                ipc_answer_0( iid, EOK );
530
                break;
541
                break;
531
            case NET_IL_RECEIVED:
542
            case NET_IL_RECEIVED:
532
                if( ! ERROR_OCCURRED( packet_translate( arp_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
543
                if( ! ERROR_OCCURRED( packet_translate( arp_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
533
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet );
544
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet );
534
                }
545
                }
535
                ipc_answer_0( iid, ERROR_CODE );
546
                ipc_answer_0( iid, ERROR_CODE );
536
                break;
547
                break;
537
            default:
548
            default:
538
                ipc_answer_0( iid, ENOTSUP );
549
                ipc_answer_0( iid, ENOTSUP );
539
        }
550
        }
540
        iid = async_get_call( icall );
551
        iid = async_get_call( icall );
541
    }
552
    }
542
}
553
}
543
 
554
 
544
/** @}
555
/** @}
545
 */
556
 */
546
 
557