Subversion Repositories HelenOS

Rev

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

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