Subversion Repositories HelenOS

Rev

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

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