Subversion Repositories HelenOS

Rev

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

Rev 4708 Rev 4723
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 <fibril_sync.h>
41
#include <fibril_sync.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <string.h>
43
#include <string.h>
44
#include <task.h>
44
#include <task.h>
45
 
45
 
46
#include <ipc/ipc.h>
46
#include <ipc/ipc.h>
47
#include <ipc/services.h>
47
#include <ipc/services.h>
48
 
48
 
49
#include "../../err.h"
49
#include "../../err.h"
50
#include "../../messages.h"
50
#include "../../messages.h"
51
#include "../../modules.h"
51
#include "../../modules.h"
52
 
52
 
53
#include "../../include/byteorder.h"
53
#include "../../include/byteorder.h"
54
#include "../../include/device.h"
54
#include "../../include/device.h"
55
#include "../../include/arp_interface.h"
55
#include "../../include/arp_interface.h"
56
#include "../../include/nil_interface.h"
56
#include "../../include/nil_interface.h"
57
#include "../../include/protocol_map.h"
57
#include "../../include/protocol_map.h"
58
 
58
 
59
#include "../../structures/measured_strings.h"
59
#include "../../structures/measured_strings.h"
60
#include "../../structures/packet/packet.h"
60
#include "../../structures/packet/packet.h"
61
#include "../../structures/packet/packet_client.h"
61
#include "../../structures/packet/packet_client.h"
62
 
62
 
63
#include "../il_messages.h"
63
#include "../il_messages.h"
64
 
64
 
65
#include "arp.h"
65
#include "arp.h"
66
#include "arp_header.h"
66
#include "arp_header.h"
67
#include "arp_oc.h"
67
#include "arp_oc.h"
68
#include "arp_module.h"
68
#include "arp_module.h"
69
#include "arp_messages.h"
69
#include "arp_messages.h"
70
 
70
 
71
/** ARP global data.
71
/** ARP global data.
72
 */
72
 */
73
arp_globals_t   arp_globals;
73
arp_globals_t   arp_globals;
74
 
74
 
75
/** Creates new protocol specific data.
75
/** Creates new protocol specific data.
-
 
76
 *  Allocates and returns the needed memory block as the proto parameter.
76
 *  @param proto Protocol specific data. Output parameter.
77
 *  @param proto The allocated protocol specific data. Output parameter.
77
 *  @param service Protocol module service. Input parameter.
78
 *  @param service The protocol module service. Input parameter.
78
 *  @param address Actual protocol device address. Input parameter.
79
 *  @param address The actual protocol device address. Input parameter.
79
 *  @returns EOK on success.
80
 *  @returns EOK on success.
80
 *  @returns ENOMEM if there is not enough memory left.
81
 *  @returns ENOMEM if there is not enough memory left.
81
 */
82
 */
82
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
83
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
83
 
84
 
84
/** Clears the device specific data.
85
/** Clears the device specific data.
85
 *  @param device The device specific data.
86
 *  @param device The device specific data.
86
 */
87
 */
87
void    clear_device( arp_device_ref device );
88
void    clear_device( arp_device_ref device );
88
 
89
 
89
/** @name Message processing functions
90
/** @name Message processing functions
90
 */
91
 */
91
/*@{*/
92
/*@{*/
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 and the packet is no longer needed.
125
 *  @returns EOK on success and the packet is no longer needed.
125
 *  @returns 1 on success and the packet has been reused.
126
 *  @returns 1 on success and the packet has been reused.
126
 *  @returns EINVAL if the packet is too small to carry an ARP packet.
127
 *  @returns EINVAL if the packet is too small to carry an ARP packet.
127
 *  @returns EINVAL if the received address lengths differs from the registered values.
128
 *  @returns EINVAL if the received address lengths differs from the registered values.
128
 *  @returns ENOENT if the device is not found in the cache.
129
 *  @returns ENOENT if the device is not found in the cache.
129
 *  @returns ENOENT if the protocol for the device is not found in the cache.
130
 *  @returns ENOENT if the protocol for the device is not found in the cache.
130
 *  @returns ENOMEM if there is not enough memory left.
131
 *  @returns ENOMEM if there is not enough memory left.
131
 */
132
 */
132
int arp_receive_message( device_id_t device_id, packet_t packet );
133
int arp_receive_message( device_id_t device_id, packet_t packet );
133
 
134
 
134
/** Updates the device content length according to the new MTU value.
135
/** Updates the device content length according to the new MTU value.
135
 *  @param device_id The device identifier. Input parameter.
136
 *  @param device_id The device identifier. Input parameter.
136
 *  @param mtu The new mtu value. Input parameter.
137
 *  @param mtu The new mtu value. Input parameter.
137
 *  @returns ENOENT if device is not found.
138
 *  @returns ENOENT if device is not found.
138
 *  @returns EOK on success.
139
 *  @returns EOK on success.
139
 */
140
 */
140
int arp_mtu_changed_message( device_id_t device_id, size_t mtu );
141
int arp_mtu_changed_message( device_id_t device_id, size_t mtu );
141
 
142
 
142
/*@}*/
143
/*@}*/
143
 
144
 
144
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
145
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
145
 
146
 
146
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
147
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
147
 
148
 
148
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
149
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
149
 
150
 
150
task_id_t arp_task_get_id( void ){
151
task_id_t arp_task_get_id( void ){
151
    return task_get_id();
152
    return task_get_id();
152
}
153
}
153
 
154
 
154
int arp_clear_device_req( int arp_phone, device_id_t device_id ){
155
int arp_clear_device_req( int arp_phone, device_id_t device_id ){
155
    arp_device_ref  device;
156
    arp_device_ref  device;
156
 
157
 
157
    fibril_rwlock_write_lock( & arp_globals.lock );
158
    fibril_rwlock_write_lock( & arp_globals.lock );
158
    device = arp_cache_find( & arp_globals.cache, device_id );
159
    device = arp_cache_find( & arp_globals.cache, device_id );
159
    if( ! device ){
160
    if( ! device ){
160
        fibril_rwlock_write_unlock( & arp_globals.lock );
161
        fibril_rwlock_write_unlock( & arp_globals.lock );
161
        return ENOENT;
162
        return ENOENT;
162
    }
163
    }
163
    clear_device( device );
164
    clear_device( device );
164
    printf( "Device %d cleared\n", device_id );
165
    printf( "Device %d cleared\n", device_id );
165
    fibril_rwlock_write_unlock( & arp_globals.lock );
166
    fibril_rwlock_write_unlock( & arp_globals.lock );
166
    return EOK;
167
    return EOK;
167
}
168
}
168
 
169
 
169
int arp_clear_address_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address ){
170
int arp_clear_address_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address ){
170
    arp_device_ref  device;
171
    arp_device_ref  device;
171
    arp_proto_ref   proto;
172
    arp_proto_ref   proto;
172
 
173
 
173
    fibril_rwlock_write_lock( & arp_globals.lock );
174
    fibril_rwlock_write_lock( & arp_globals.lock );
174
    device = arp_cache_find( & arp_globals.cache, device_id );
175
    device = arp_cache_find( & arp_globals.cache, device_id );
175
    if( ! device ){
176
    if( ! device ){
176
        fibril_rwlock_write_unlock( & arp_globals.lock );
177
        fibril_rwlock_write_unlock( & arp_globals.lock );
177
        return ENOENT;
178
        return ENOENT;
178
    }
179
    }
179
    proto = arp_protos_find( & device->protos, protocol );
180
    proto = arp_protos_find( & device->protos, protocol );
180
    if( ! proto ){
181
    if( ! proto ){
181
        fibril_rwlock_write_unlock( & arp_globals.lock );
182
        fibril_rwlock_write_unlock( & arp_globals.lock );
182
        return ENOENT;
183
        return ENOENT;
183
    }
184
    }
184
    arp_addr_exclude( & proto->addresses, address->value, address->length );
185
    arp_addr_exclude( & proto->addresses, address->value, address->length );
185
    fibril_rwlock_write_unlock( & arp_globals.lock );
186
    fibril_rwlock_write_unlock( & arp_globals.lock );
186
    return EOK;
187
    return EOK;
187
}
188
}
188
 
189
 
189
int arp_clean_cache_req( int arp_phone ){
190
int arp_clean_cache_req( int arp_phone ){
190
    int             count;
191
    int             count;
191
    arp_device_ref  device;
192
    arp_device_ref  device;
192
 
193
 
193
    fibril_rwlock_write_lock( & arp_globals.lock );
194
    fibril_rwlock_write_lock( & arp_globals.lock );
194
    for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){
195
    for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){
195
        device = arp_cache_get_index( & arp_globals.cache, count );
196
        device = arp_cache_get_index( & arp_globals.cache, count );
196
        if( device ){
197
        if( device ){
197
            clear_device( device );
198
            clear_device( device );
198
            if( device->addr_data ) free( device->addr_data );
199
            if( device->addr_data ) free( device->addr_data );
199
            if( device->broadcast_data ) free( device->broadcast_data );
200
            if( device->broadcast_data ) free( device->broadcast_data );
200
        }
201
        }
201
    }
202
    }
202
    arp_cache_clear( & arp_globals.cache );
203
    arp_cache_clear( & arp_globals.cache );
203
    fibril_rwlock_write_unlock( & arp_globals.lock );
204
    fibril_rwlock_write_unlock( & arp_globals.lock );
204
    printf( "Cache cleaned\n" );
205
    printf( "Cache cleaned\n" );
205
    return EOK;
206
    return EOK;
206
}
207
}
207
 
208
 
208
int arp_device_req( int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ){
209
int arp_device_req( int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ){
209
    ERROR_DECLARE;
210
    ERROR_DECLARE;
210
 
211
 
211
    measured_string_ref tmp;
212
    measured_string_ref tmp;
212
 
213
 
-
 
214
    // copy the given address for exclusive use
213
    tmp = measured_string_copy( address );
215
    tmp = measured_string_copy( address );
214
    if( ERROR_OCCURRED( arp_device_message( device_id, netif, protocol, tmp ))){
216
    if( ERROR_OCCURRED( arp_device_message( device_id, netif, protocol, tmp ))){
215
        free( tmp->value );
217
        free( tmp->value );
216
        free( tmp );
218
        free( tmp );
217
    }
219
    }
218
    return ERROR_CODE;
220
    return ERROR_CODE;
219
}
221
}
220
 
222
 
221
int arp_translate_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address, measured_string_ref * translation, char ** data ){
223
int arp_translate_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address, measured_string_ref * translation, char ** data ){
222
    measured_string_ref tmp;
224
    measured_string_ref tmp;
223
 
225
 
224
    fibril_rwlock_read_lock( & arp_globals.lock );
226
    fibril_rwlock_read_lock( & arp_globals.lock );
225
    tmp = arp_translate_message( device_id, protocol, address );
227
    tmp = arp_translate_message( device_id, protocol, address );
226
    if( tmp ){
228
    if( tmp ){
227
        * translation = measured_string_copy( tmp );
229
        * translation = measured_string_copy( tmp );
228
        fibril_rwlock_read_unlock( & arp_globals.lock );
230
        fibril_rwlock_read_unlock( & arp_globals.lock );
229
        if( * translation ){
231
        if( * translation ){
230
            * data = ( ** translation ).value;
232
            * data = ( ** translation ).value;
231
            return EOK;
233
            return EOK;
232
        }else{
234
        }else{
233
            return ENOMEM;
235
            return ENOMEM;
234
        }
236
        }
235
    }else{
237
    }else{
236
        fibril_rwlock_read_unlock( & arp_globals.lock );
238
        fibril_rwlock_read_unlock( & arp_globals.lock );
237
        return ENOENT;
239
        return ENOENT;
238
    }
240
    }
239
}
241
}
240
 
242
 
241
int arp_initialize( async_client_conn_t client_connection ){
243
int arp_initialize( async_client_conn_t client_connection ){
242
    ERROR_DECLARE;
244
    ERROR_DECLARE;
243
 
245
 
244
    fibril_rwlock_initialize( & arp_globals.lock );
246
    fibril_rwlock_initialize( & arp_globals.lock );
245
    fibril_rwlock_write_lock( & arp_globals.lock );
247
    fibril_rwlock_write_lock( & arp_globals.lock );
246
    arp_globals.client_connection = client_connection;
248
    arp_globals.client_connection = client_connection;
247
    ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache ));
249
    ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache ));
248
    fibril_rwlock_write_unlock( & arp_globals.lock );
250
    fibril_rwlock_write_unlock( & arp_globals.lock );
249
    return EOK;
251
    return EOK;
250
}
252
}
251
 
253
 
252
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
254
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
253
    ERROR_DECLARE;
255
    ERROR_DECLARE;
254
 
256
 
255
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
257
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
256
    if( !( * proto )) return ENOMEM;
258
    if( !( * proto )) return ENOMEM;
257
    ( ** proto ).service = service;
259
    ( ** proto ).service = service;
258
    ( ** proto ).addr = address;
260
    ( ** proto ).addr = address;
259
    ( ** proto ).addr_data = address->value;
261
    ( ** proto ).addr_data = address->value;
260
    if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){
262
    if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){
261
        free( * proto );
263
        free( * proto );
262
        return ERROR_CODE;
264
        return ERROR_CODE;
263
    }
265
    }
264
    return EOK;
266
    return EOK;
265
}
267
}
266
 
268
 
267
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
269
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
268
    ERROR_DECLARE;
270
    ERROR_DECLARE;
269
 
271
 
270
    arp_device_ref  device;
272
    arp_device_ref  device;
271
    arp_proto_ref   proto;
273
    arp_proto_ref   proto;
272
    int             index;
274
    int             index;
273
    hw_type_t       hardware;
275
    hw_type_t       hardware;
274
 
276
 
275
    fibril_rwlock_write_lock( & arp_globals.lock );
277
    fibril_rwlock_write_lock( & arp_globals.lock );
276
    // an existing device?
278
    // an existing device?
277
    device = arp_cache_find( & arp_globals.cache, device_id );
279
    device = arp_cache_find( & arp_globals.cache, device_id );
278
    if( device ){
280
    if( device ){
279
        if( device->service != service ){
281
        if( device->service != service ){
280
            printf( "Device %d already exists\n", device->device_id );
282
            printf( "Device %d already exists\n", device->device_id );
281
            fibril_rwlock_write_unlock( & arp_globals.lock );
283
            fibril_rwlock_write_unlock( & arp_globals.lock );
282
            return EEXIST;
284
            return EEXIST;
283
        }
285
        }
284
        proto = arp_protos_find( & device->protos, protocol );
286
        proto = arp_protos_find( & device->protos, protocol );
285
        if( proto ){
287
        if( proto ){
286
            free( proto->addr );
288
            free( proto->addr );
287
            free( proto->addr_data );
289
            free( proto->addr_data );
288
            proto->addr = address;
290
            proto->addr = address;
289
            proto->addr_data = address->value;
291
            proto->addr_data = address->value;
290
        }else{
292
        }else{
291
            if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
293
            if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
292
                fibril_rwlock_write_unlock( & arp_globals.lock );
294
                fibril_rwlock_write_unlock( & arp_globals.lock );
293
                return ERROR_CODE;
295
                return ERROR_CODE;
294
            }
296
            }
295
            index = arp_protos_add( & device->protos, proto->service, proto );
297
            index = arp_protos_add( & device->protos, proto->service, proto );
296
            if( index < 0 ){
298
            if( index < 0 ){
297
                fibril_rwlock_write_unlock( & arp_globals.lock );
299
                fibril_rwlock_write_unlock( & arp_globals.lock );
298
                free( proto );
300
                free( proto );
299
                return index;
301
                return index;
300
            }
302
            }
301
            printf( "New protocol added:\n\tdevice id\t= %d\n\tproto\t= %d", device_id, protocol );
303
            printf( "New protocol added:\n\tdevice id\t= %d\n\tproto\t= %d", device_id, protocol );
302
        }
304
        }
303
    }else{
305
    }else{
304
        hardware = hardware_map( service );
306
        hardware = hardware_map( service );
305
        if( ! hardware ) return ENOENT;
307
        if( ! hardware ) return ENOENT;
306
        // create a new device
308
        // create a new device
307
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
309
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
308
        if( ! device ){
310
        if( ! device ){
309
            fibril_rwlock_write_unlock( & arp_globals.lock );
311
            fibril_rwlock_write_unlock( & arp_globals.lock );
310
            return ENOMEM;
312
            return ENOMEM;
311
        }
313
        }
312
        device->hardware = hardware;
314
        device->hardware = hardware;
313
        device->device_id = device_id;
315
        device->device_id = device_id;
314
        if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
316
        if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
315
        || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
317
        || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
316
            fibril_rwlock_write_unlock( & arp_globals.lock );
318
            fibril_rwlock_write_unlock( & arp_globals.lock );
317
            free( device );
319
            free( device );
318
            return ERROR_CODE;
320
            return ERROR_CODE;
319
        }
321
        }
320
        index = arp_protos_add( & device->protos, proto->service, proto );
322
        index = arp_protos_add( & device->protos, proto->service, proto );
321
        if( index < 0 ){
323
        if( index < 0 ){
322
            fibril_rwlock_write_unlock( & arp_globals.lock );
324
            fibril_rwlock_write_unlock( & arp_globals.lock );
323
            arp_protos_destroy( & device->protos );
325
            arp_protos_destroy( & device->protos );
324
            free( device );
326
            free( device );
325
            return index;
327
            return index;
326
        }
328
        }
327
        device->service = service;
329
        device->service = service;
328
        // bind the new one
330
        // bind the new one
329
        device->phone = bind_service( device->service, ( ipcarg_t ) device->device_id, SERVICE_ARP, 0, arp_globals.client_connection );
331
        device->phone = bind_service( device->service, ( ipcarg_t ) device->device_id, SERVICE_ARP, 0, arp_globals.client_connection );
330
        if( device->phone < 0 ){
332
        if( device->phone < 0 ){
331
            fibril_rwlock_write_unlock( & arp_globals.lock );
333
            fibril_rwlock_write_unlock( & arp_globals.lock );
332
            arp_protos_destroy( & device->protos );
334
            arp_protos_destroy( & device->protos );
333
            free( device );
335
            free( device );
334
            return EREFUSED;
336
            return EREFUSED;
335
        }
337
        }
336
        // get packet dimensions
338
        // get packet dimensions
337
        if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
339
        if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
338
            fibril_rwlock_write_unlock( & arp_globals.lock );
340
            fibril_rwlock_write_unlock( & arp_globals.lock );
339
            arp_protos_destroy( & device->protos );
341
            arp_protos_destroy( & device->protos );
340
            free( device );
342
            free( device );
341
            return ERROR_CODE;
343
            return ERROR_CODE;
342
        }
344
        }
343
        // get hardware address
345
        // get hardware address
344
        if( ERROR_OCCURRED( nil_get_addr( device->phone, device_id, & device->addr, & device->addr_data ))){
346
        if( ERROR_OCCURRED( nil_get_addr( device->phone, device_id, & device->addr, & device->addr_data ))){
345
            fibril_rwlock_write_unlock( & arp_globals.lock );
347
            fibril_rwlock_write_unlock( & arp_globals.lock );
346
            arp_protos_destroy( & device->protos );
348
            arp_protos_destroy( & device->protos );
347
            free( device );
349
            free( device );
348
            return ERROR_CODE;
350
            return ERROR_CODE;
349
        }
351
        }
350
        // get broadcast address
352
        // get broadcast address
351
        if( ERROR_OCCURRED( nil_get_broadcast_addr( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){
353
        if( ERROR_OCCURRED( nil_get_broadcast_addr( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){
352
            fibril_rwlock_write_unlock( & arp_globals.lock );
354
            fibril_rwlock_write_unlock( & arp_globals.lock );
353
            free( device->addr );
355
            free( device->addr );
354
            free( device->addr_data );
356
            free( device->addr_data );
355
            arp_protos_destroy( & device->protos );
357
            arp_protos_destroy( & device->protos );
356
            free( device );
358
            free( device );
357
            return ERROR_CODE;
359
            return ERROR_CODE;
358
        }
360
        }
359
        if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
361
        if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
360
            fibril_rwlock_write_unlock( & arp_globals.lock );
362
            fibril_rwlock_write_unlock( & arp_globals.lock );
361
            free( device->addr );
363
            free( device->addr );
362
            free( device->addr_data );
364
            free( device->addr_data );
363
            free( device->broadcast_addr );
365
            free( device->broadcast_addr );
364
            free( device->broadcast_data );
366
            free( device->broadcast_data );
365
            arp_protos_destroy( & device->protos );
367
            arp_protos_destroy( & device->protos );
366
            free( device );
368
            free( device );
367
            return ERROR_CODE;
369
            return ERROR_CODE;
368
        }
370
        }
369
        printf( "New device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d\n\tproto\t= %d\n", device->device_id, device->hardware, device->service, protocol );
371
        printf( "New device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d\n\tproto\t= %d\n", device->device_id, device->hardware, device->service, protocol );
370
    }
372
    }
371
    fibril_rwlock_write_unlock( & arp_globals.lock );
373
    fibril_rwlock_write_unlock( & arp_globals.lock );
372
    return EOK;
374
    return EOK;
373
}
375
}
374
 
376
 
375
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
377
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
376
    arp_device_ref      device;
378
    arp_device_ref      device;
377
    arp_proto_ref       proto;
379
    arp_proto_ref       proto;
378
    measured_string_ref addr;
380
    measured_string_ref addr;
379
    size_t              length;
381
    size_t              length;
380
    packet_t            packet;
382
    packet_t            packet;
381
    arp_header_ref      header;
383
    arp_header_ref      header;
382
 
384
 
383
    if( ! target ) return NULL;
385
    if( ! target ) return NULL;
384
    device = arp_cache_find( & arp_globals.cache, device_id );
386
    device = arp_cache_find( & arp_globals.cache, device_id );
385
    if( ! device ) return NULL;
387
    if( ! device ) return NULL;
386
    proto = arp_protos_find( & device->protos, protocol );
388
    proto = arp_protos_find( & device->protos, protocol );
387
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
389
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
388
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
390
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
389
    if( addr ) return addr;
391
    if( addr ) return addr;
390
    // ARP packet content size = header + ( address + translation ) * 2
392
    // ARP packet content size = header + ( address + translation ) * 2
391
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
393
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
392
    if( length > device->content ) return NULL;
394
    if( length > device->content ) return NULL;
393
    packet = packet_get_4( arp_globals.net_phone, device->addr_len, device->prefix, length, device->suffix );
395
    packet = packet_get_4( arp_globals.net_phone, device->addr_len, device->prefix, length, device->suffix );
394
    if( ! packet ) return NULL;
396
    if( ! packet ) return NULL;
395
    header = ( arp_header_ref ) packet_suffix( packet, length );
397
    header = ( arp_header_ref ) packet_suffix( packet, length );
396
    if( ! header ){
398
    if( ! header ){
397
        pq_release( arp_globals.net_phone, packet_get_id( packet ));
399
        pq_release( arp_globals.net_phone, packet_get_id( packet ));
398
        return NULL;
400
        return NULL;
399
    }
401
    }
400
    header->hardware = htons( device->hardware );
402
    header->hardware = htons( device->hardware );
401
    header->hardware_length = ( uint8_t ) device->addr->length;
403
    header->hardware_length = ( uint8_t ) device->addr->length;
402
    header->protocol = htons( protocol_map( device->service, protocol ));
404
    header->protocol = htons( protocol_map( device->service, protocol ));
403
    header->protocol_length = ( uint8_t ) proto->addr->length;
405
    header->protocol_length = ( uint8_t ) proto->addr->length;
404
    header->operation = htons( ARPOP_REQUEST );
406
    header->operation = htons( ARPOP_REQUEST );
405
    length = sizeof( arp_header_t );
407
    length = sizeof( arp_header_t );
406
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
408
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
407
    length += device->addr->length;
409
    length += device->addr->length;
408
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
410
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
409
    length += proto->addr->length;
411
    length += proto->addr->length;
410
    bzero((( uint8_t * ) header ) + length, device->addr->length );
412
    bzero((( uint8_t * ) header ) + length, device->addr->length );
411
    length += device->addr->length;
413
    length += device->addr->length;
412
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
414
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
413
    if( packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length )) != EOK ){
415
    if( packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length )) != EOK ){
414
        pq_release( arp_globals.net_phone, packet_get_id( packet ));
416
        pq_release( arp_globals.net_phone, packet_get_id( packet ));
415
        return NULL;
417
        return NULL;
416
    }
418
    }
417
    nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
419
    nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
418
    return NULL;
420
    return NULL;
419
}
421
}
420
 
422
 
421
int arp_receive_message( device_id_t device_id, packet_t packet ){
423
int arp_receive_message( device_id_t device_id, packet_t packet ){
422
    ERROR_DECLARE;
424
    ERROR_DECLARE;
423
 
425
 
424
    size_t              length;
426
    size_t              length;
425
    arp_header_ref      header;
427
    arp_header_ref      header;
426
    arp_device_ref      device;
428
    arp_device_ref      device;
427
    arp_proto_ref       proto;
429
    arp_proto_ref       proto;
428
    measured_string_ref hw_source;
430
    measured_string_ref hw_source;
429
    uint8_t *           src_hw;
431
    uint8_t *           src_hw;
430
    uint8_t *           src_proto;
432
    uint8_t *           src_proto;
431
    uint8_t *           des_hw;
433
    uint8_t *           des_hw;
432
    uint8_t *           des_proto;
434
    uint8_t *           des_proto;
433
 
435
 
434
    length = packet_get_data_length( packet );
436
    length = packet_get_data_length( packet );
435
    if( length <= sizeof( arp_header_t )) return EINVAL;
437
    if( length <= sizeof( arp_header_t )) return EINVAL;
436
    device = arp_cache_find( & arp_globals.cache, device_id );
438
    device = arp_cache_find( & arp_globals.cache, device_id );
437
    if( ! device ) return ENOENT;
439
    if( ! device ) return ENOENT;
438
    header = ( arp_header_ref ) packet_get_data( packet );
440
    header = ( arp_header_ref ) packet_get_data( packet );
439
    if(( ntohs( header->hardware ) != device->hardware )
441
    if(( ntohs( header->hardware ) != device->hardware )
440
    || ( length < sizeof( arp_header_t ) + header->hardware_length * 2u + header->protocol_length * 2u )){
442
    || ( length < sizeof( arp_header_t ) + header->hardware_length * 2u + header->protocol_length * 2u )){
441
        return EINVAL;
443
        return EINVAL;
442
    }
444
    }
443
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol )));
445
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol )));
444
    if( ! proto ) return ENOENT;
446
    if( ! proto ) return ENOENT;
445
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
447
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
446
    src_proto = src_hw + header->hardware_length;
448
    src_proto = src_hw + header->hardware_length;
447
    des_hw = src_proto + header->protocol_length;
449
    des_hw = src_proto + header->protocol_length;
448
    des_proto = des_hw + header->hardware_length;
450
    des_proto = des_hw + header->hardware_length;
449
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
451
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
450
    // exists?
452
    // exists?
451
    if( hw_source ){
453
    if( hw_source ){
452
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
454
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
453
            return EINVAL;
455
            return EINVAL;
454
        }
456
        }
455
        memcpy( hw_source->value, src_hw, hw_source->length );
457
        memcpy( hw_source->value, src_hw, hw_source->length );
456
    }
458
    }
457
    // is my protocol address?
459
    // is my protocol address?
458
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->protocol_length )){
460
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->protocol_length )){
459
        return EINVAL;
461
        return EINVAL;
460
    }
462
    }
461
    if( ! str_lcmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
463
    if( ! str_lcmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
462
        // not already upadted?
464
        // not already upadted?
463
        if( ! hw_source ){
465
        if( ! hw_source ){
464
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
466
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
465
            if( ! hw_source ) return ENOMEM;
467
            if( ! hw_source ) return ENOMEM;
466
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ));
468
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ));
467
        }
469
        }
468
        if( ntohs( header->operation ) == ARPOP_REQUEST ){
470
        if( ntohs( header->operation ) == ARPOP_REQUEST ){
469
            header->operation = htons( ARPOP_REPLY );
471
            header->operation = htons( ARPOP_REPLY );
470
            memcpy( des_proto, src_proto, header->protocol_length );
472
            memcpy( des_proto, src_proto, header->protocol_length );
471
            memcpy( src_proto, proto->addr->value, header->protocol_length );
473
            memcpy( src_proto, proto->addr->value, header->protocol_length );
472
            memcpy( src_hw, device->addr->value, device->addr_len );
474
            memcpy( src_hw, device->addr->value, device->addr_len );
473
            memcpy( des_hw, hw_source->value, header->hardware_length );
475
            memcpy( des_hw, hw_source->value, header->hardware_length );
474
            ERROR_PROPAGATE( packet_set_addr( packet, src_hw, des_hw, header->hardware_length ));
476
            ERROR_PROPAGATE( packet_set_addr( packet, src_hw, des_hw, header->hardware_length ));
475
            nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
477
            nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
476
            return 1;
478
            return 1;
477
        }
479
        }
478
    }
480
    }
479
    return EOK;
481
    return EOK;
480
}
482
}
481
 
483
 
482
void clear_device( arp_device_ref device ){
484
void clear_device( arp_device_ref device ){
483
    int             count;
485
    int             count;
484
    arp_proto_ref   proto;
486
    arp_proto_ref   proto;
485
 
487
 
486
    for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){
488
    for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){
487
        proto = arp_protos_get_index( & device->protos, count );
489
        proto = arp_protos_get_index( & device->protos, count );
488
        if( proto ){
490
        if( proto ){
489
            if( proto->addr ) free( proto->addr );
491
            if( proto->addr ) free( proto->addr );
490
            if( proto->addr_data ) free( proto->addr_data );
492
            if( proto->addr_data ) free( proto->addr_data );
491
            arp_addr_destroy( & proto->addresses );
493
            arp_addr_destroy( & proto->addresses );
492
        }
494
        }
493
    }
495
    }
494
    arp_protos_clear( & device->protos );
496
    arp_protos_clear( & device->protos );
495
}
497
}
496
 
498
 
497
int arp_connect_module( services_t service ){
499
int arp_connect_module( services_t service ){
498
    if( service != SERVICE_ARP ) return EINVAL;
500
    if( service != SERVICE_ARP ) return EINVAL;
499
    return EOK;
501
    return EOK;
500
}
502
}
501
 
503
 
502
int arp_mtu_changed_message( device_id_t device_id, size_t mtu ){
504
int arp_mtu_changed_message( device_id_t device_id, size_t mtu ){
503
    arp_device_ref  device;
505
    arp_device_ref  device;
504
 
506
 
505
    fibril_rwlock_write_lock( & arp_globals.lock );
507
    fibril_rwlock_write_lock( & arp_globals.lock );
506
    device = arp_cache_find( & arp_globals.cache, device_id );
508
    device = arp_cache_find( & arp_globals.cache, device_id );
507
    if( ! device ){
509
    if( ! device ){
508
        fibril_rwlock_write_unlock( & arp_globals.lock );
510
        fibril_rwlock_write_unlock( & arp_globals.lock );
509
        return ENOENT;
511
        return ENOENT;
510
    }
512
    }
511
    device->content = mtu;
513
    device->content = mtu;
512
    printf( "arp - device %d changed mtu to %d\n\n", device_id, mtu );
514
    printf( "arp - device %d changed mtu to %d\n\n", device_id, mtu );
513
    fibril_rwlock_write_unlock( & arp_globals.lock );
515
    fibril_rwlock_write_unlock( & arp_globals.lock );
514
    return EOK;
516
    return EOK;
515
}
517
}
516
 
518
 
517
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
519
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
518
    ERROR_DECLARE;
520
    ERROR_DECLARE;
519
 
521
 
520
    measured_string_ref address;
522
    measured_string_ref address;
521
    measured_string_ref translation;
523
    measured_string_ref translation;
522
    char *              data;
524
    char *              data;
523
    packet_t            packet;
525
    packet_t            packet;
524
    packet_t            next;
526
    packet_t            next;
525
 
527
 
526
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_ARP_FIRST );
528
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_ARP_FIRST );
527
    * answer_count = 0;
529
    * answer_count = 0;
528
    switch( IPC_GET_METHOD( * call )){
530
    switch( IPC_GET_METHOD( * call )){
529
        case IPC_M_PHONE_HUNGUP:
531
        case IPC_M_PHONE_HUNGUP:
530
            return EOK;
532
            return EOK;
531
        case NET_ARP_DEVICE:
533
        case NET_ARP_DEVICE:
532
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
534
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
533
            if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_NETIF( call ), address ))){
535
            if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_NETIF( call ), address ))){
534
                free( address );
536
                free( address );
535
                free( data );
537
                free( data );
536
            }
538
            }
537
            return ERROR_CODE;
539
            return ERROR_CODE;
538
        case NET_ARP_TRANSLATE:
540
        case NET_ARP_TRANSLATE:
539
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
541
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
540
            fibril_rwlock_read_lock( & arp_globals.lock );
542
            fibril_rwlock_read_lock( & arp_globals.lock );
541
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address );
543
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address );
542
            free( address );
544
            free( address );
543
            free( data );
545
            free( data );
544
            if( ! translation ){
546
            if( ! translation ){
545
                fibril_rwlock_read_unlock( & arp_globals.lock );
547
                fibril_rwlock_read_unlock( & arp_globals.lock );
546
                return ENOENT;
548
                return ENOENT;
547
            }
549
            }
548
            ERROR_CODE = measured_strings_reply( translation, 1 );
550
            ERROR_CODE = measured_strings_reply( translation, 1 );
549
            fibril_rwlock_read_unlock( & arp_globals.lock );
551
            fibril_rwlock_read_unlock( & arp_globals.lock );
550
            return ERROR_CODE;
552
            return ERROR_CODE;
551
        case NET_ARP_CLEAR_DEVICE:
553
        case NET_ARP_CLEAR_DEVICE:
552
            return arp_clear_device_req( 0, IPC_GET_DEVICE( call ));
554
            return arp_clear_device_req( 0, IPC_GET_DEVICE( call ));
553
        case NET_ARP_CLEAR_ADDRESS:
555
        case NET_ARP_CLEAR_ADDRESS:
554
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
556
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
555
            arp_clear_address_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address );
557
            arp_clear_address_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address );
556
            free( address );
558
            free( address );
557
            free( data );
559
            free( data );
558
            return EOK;
560
            return EOK;
559
        case NET_ARP_CLEAN_CACHE:
561
        case NET_ARP_CLEAN_CACHE:
560
            return arp_clean_cache_req( 0 );
562
            return arp_clean_cache_req( 0 );
561
        case NET_IL_DEVICE_STATE:
563
        case NET_IL_DEVICE_STATE:
562
            // do nothing - keep the cache
564
            // do nothing - keep the cache
563
            return EOK;
565
            return EOK;
564
        case NET_IL_RECEIVED:
566
        case NET_IL_RECEIVED:
565
            if( ! ERROR_OCCURRED( packet_translate( arp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
567
            if( ! ERROR_OCCURRED( packet_translate( arp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
566
                fibril_rwlock_read_lock( & arp_globals.lock );
568
                fibril_rwlock_read_lock( & arp_globals.lock );
567
                do{
569
                do{
568
                    next = pq_detach( packet );
570
                    next = pq_detach( packet );
569
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( call ), packet );
571
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( call ), packet );
570
                    if( ERROR_CODE != 1 ) pq_release( arp_globals.net_phone, packet_get_id( packet ));
572
                    if( ERROR_CODE != 1 ) pq_release( arp_globals.net_phone, packet_get_id( packet ));
571
                    packet = next;
573
                    packet = next;
572
                }while( packet );
574
                }while( packet );
573
                fibril_rwlock_read_unlock( & arp_globals.lock );
575
                fibril_rwlock_read_unlock( & arp_globals.lock );
574
            }
576
            }
575
            return ERROR_CODE;
577
            return ERROR_CODE;
576
        case NET_IL_MTU_CHANGED:
578
        case NET_IL_MTU_CHANGED:
577
            return arp_mtu_changed_message( IPC_GET_DEVICE( call ), IPC_GET_MTU( call ));
579
            return arp_mtu_changed_message( IPC_GET_DEVICE( call ), IPC_GET_MTU( call ));
578
    }
580
    }
579
    return ENOTSUP;
581
    return ENOTSUP;
580
}
582
}
581
 
583
 
582
/** @}
584
/** @}
583
 */
585
 */
584
 
586