Subversion Repositories HelenOS

Rev

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

Rev 4396 Rev 4501
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup arp
29
/** @addtogroup arp
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  ARP module implementation.
34
 *  ARP module implementation.
35
 *  @see arp.h
35
 *  @see arp.h
36
 */
36
 */
37
 
37
 
38
#include <async.h>
38
#include <async.h>
39
#include <malloc.h>
39
#include <malloc.h>
40
#include <mem.h>
40
#include <mem.h>
41
#include <rwlock.h>
41
#include <rwlock.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#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
/** Clears the whole cache.
-
 
76
 *  @returns EOK on success.
-
 
77
 */
-
 
78
int arp_clean_cache_req( int arp_phone );
-
 
79
 
-
 
80
/** Clears the device specific data from the cache.
-
 
81
 *  @param arp_phone The ARP module phone used for (semi)remote calls. Input parameter.
-
 
82
 *  @param device_id The device identifier. Input parameter.
-
 
83
 *  @returns EOK on success.
-
 
84
 *  @returns ENOENT  if the device is not found in the cache.
-
 
85
 */
-
 
86
int arp_clear_device_req( int arp_phone, device_id_t device_id );
-
 
87
 
-
 
88
/** Creates new protocol specific data.
75
/** Creates new protocol specific data.
89
 *  @param proto Protocol specific data. Output parameter.
76
 *  @param proto Protocol specific data. Output parameter.
90
 *  @param service Protocol module service. Input parameter.
77
 *  @param service Protocol module service. Input parameter.
91
 *  @param address Actual protocol device address. Input parameter.
78
 *  @param address Actual protocol device address. Input parameter.
92
 *  @returns EOK on success.
79
 *  @returns EOK on success.
93
 *  @returns ENOMEM if there is not enough memory left.
80
 *  @returns ENOMEM if there is not enough memory left.
94
 */
81
 */
95
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 );
96
 
83
 
97
/** Registers the device.
84
/** Registers the device.
98
 *  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.
99
 *  @param device_id The device identifier. Input parameter.
86
 *  @param device_id The device identifier. Input parameter.
100
 *  @param service The device driver service. Input parameter.
87
 *  @param service The device driver service. Input parameter.
101
 *  @param protocol The protocol service. Input parameter.
88
 *  @param protocol The protocol service. Input parameter.
102
 *  @param address The actual device protocol address.
89
 *  @param address The actual device protocol address.
103
 *  @returns EOK on success.
90
 *  @returns EOK on success.
104
 *  @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.
105
 *  @returns ENOMEM if there is not enough memory left.
92
 *  @returns ENOMEM if there is not enough memory left.
106
 *  @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.
107
 */
94
 */
108
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 );
109
 
96
 
110
/** Returns the hardware address for the given protocol address.
97
/** Returns the hardware address for the given protocol address.
111
 *  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.
112
 *  @param device_id The device identifier. Input parameter.
99
 *  @param device_id The device identifier. Input parameter.
113
 *  @param protocol The protocol service. Input parameter.
100
 *  @param protocol The protocol service. Input parameter.
114
 *  @param target The target protocol address. Input parameter.
101
 *  @param target The target protocol address. Input parameter.
115
 *  @returns The hardware address of the target.
102
 *  @returns The hardware address of the target.
116
 *  @returns NULL if the target parameter is NULL.
103
 *  @returns NULL if the target parameter is NULL.
117
 *  @returns NULL if the device is not found.
104
 *  @returns NULL if the device is not found.
118
 *  @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.
119
 *  @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.
120
 */
107
 */
121
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 );
122
 
109
 
123
/** Processes the received ARP packet.
110
/** Processes the received ARP packet.
124
 *  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.
125
 *  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.
126
 *  @param device_id The source device identifier. Input parameter.
113
 *  @param device_id The source device identifier. Input parameter.
127
 *  @param packet The received packet. Input/output parameter.
114
 *  @param packet The received packet. Input/output parameter.
128
 *  @returns EOK on success and the packet is no longer needed.
115
 *  @returns EOK on success and the packet is no longer needed.
129
 *  @returns 1 on success and the packet has been reused.
116
 *  @returns 1 on success and the packet has been reused.
130
 *  @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.
131
 *  @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.
132
 *  @returns ENOENT if the device is not found in the cache.
119
 *  @returns ENOENT if the device is not found in the cache.
133
 *  @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.
134
 *  @returns ENOMEM if there is not enough memory left.
121
 *  @returns ENOMEM if there is not enough memory left.
135
 */
122
 */
136
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 );
137
 
124
 
138
/** Clears the device specific data.
125
/** Clears the device specific data.
139
 *  @param device The device specific data.
126
 *  @param device The device specific data.
140
 */
127
 */
141
void    clear_device( arp_device_ref device );
128
void    clear_device( arp_device_ref device );
142
 
129
 
143
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
130
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
144
 
131
 
145
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
132
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
146
 
133
 
147
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
134
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
148
 
135
 
149
int arp_task_get_id( void ){
136
task_id_t arp_task_get_id( void ){
150
    return task_get_id();
137
    return task_get_id();
151
}
138
}
152
 
139
 
153
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 ){
154
    arp_device_ref  device;
141
    arp_device_ref  device;
155
 
142
 
156
    rwlock_write_lock( & arp_globals.lock );
143
    rwlock_write_lock( & arp_globals.lock );
157
    device = arp_cache_find( & arp_globals.cache, device_id );
144
    device = arp_cache_find( & arp_globals.cache, device_id );
158
    if( ! device ){
145
    if( ! device ){
159
        rwlock_write_unlock( & arp_globals.lock );
146
        rwlock_write_unlock( & arp_globals.lock );
160
        return ENOENT;
147
        return ENOENT;
161
    }
148
    }
162
    clear_device( device );
149
    clear_device( device );
163
    printf( "Device %d cleared\n", device_id );
150
    printf( "Device %d cleared\n", device_id );
164
    rwlock_write_unlock( & arp_globals.lock );
151
    rwlock_write_unlock( & arp_globals.lock );
165
    return EOK;
152
    return EOK;
166
}
153
}
167
 
154
 
168
int arp_clean_cache_req( int arp_phone ){
155
int arp_clean_cache_req( int arp_phone ){
169
    int             count;
156
    int             count;
170
    arp_device_ref  device;
157
    arp_device_ref  device;
171
 
158
 
172
    rwlock_write_lock( & arp_globals.lock );
159
    rwlock_write_lock( & arp_globals.lock );
173
    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 ){
174
        device = arp_cache_get_index( & arp_globals.cache, count );
161
        device = arp_cache_get_index( & arp_globals.cache, count );
175
        if( device ){
162
        if( device ){
176
            clear_device( device );
163
            clear_device( device );
177
            if( device->addr_data ) free( device->addr_data );
164
            if( device->addr_data ) free( device->addr_data );
178
            if( device->broadcast_data ) free( device->broadcast_data );
165
            if( device->broadcast_data ) free( device->broadcast_data );
179
        }
166
        }
180
    }
167
    }
181
    arp_cache_clear( & arp_globals.cache );
168
    arp_cache_clear( & arp_globals.cache );
182
    rwlock_write_unlock( & arp_globals.lock );
169
    rwlock_write_unlock( & arp_globals.lock );
183
    printf( "Cache cleaned\n" );
170
    printf( "Cache cleaned\n" );
184
    return EOK;
171
    return EOK;
185
}
172
}
186
 
173
 
187
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 ){
188
    ERROR_DECLARE;
175
    ERROR_DECLARE;
189
 
176
 
190
    measured_string_ref tmp;
177
    measured_string_ref tmp;
191
 
178
 
192
    tmp = measured_string_copy( address );
179
    tmp = measured_string_copy( address );
193
    if( ERROR_OCCURRED( arp_device_message( device_id, netif, protocol, tmp ))){
180
    if( ERROR_OCCURRED( arp_device_message( device_id, netif, protocol, tmp ))){
194
        free( tmp->value );
181
        free( tmp->value );
195
        free( tmp );
182
        free( tmp );
196
    }
183
    }
197
    return ERROR_CODE;
184
    return ERROR_CODE;
198
}
185
}
199
 
186
 
200
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 ){
201
    measured_string_ref tmp;
188
    measured_string_ref tmp;
202
 
189
 
203
    rwlock_read_lock( & arp_globals.lock );
190
    rwlock_read_lock( & arp_globals.lock );
204
    tmp = arp_translate_message( device_id, protocol, address );
191
    tmp = arp_translate_message( device_id, protocol, address );
205
    if( tmp ){
192
    if( tmp ){
206
        * translation = measured_string_copy( tmp );
193
        * translation = measured_string_copy( tmp );
207
        rwlock_read_unlock( & arp_globals.lock );
194
        rwlock_read_unlock( & arp_globals.lock );
208
        if( * translation ){
195
        if( * translation ){
209
            * data = ( ** translation ).value;
196
            * data = ( ** translation ).value;
210
            return EOK;
197
            return EOK;
211
        }else{
198
        }else{
212
            return ENOMEM;
199
            return ENOMEM;
213
        }
200
        }
214
    }else{
201
    }else{
215
        rwlock_read_unlock( & arp_globals.lock );
202
        rwlock_read_unlock( & arp_globals.lock );
216
        return ENOENT;
203
        return ENOENT;
217
    }
204
    }
218
}
205
}
219
 
206
 
220
int arp_initialize( async_client_conn_t client_connection ){
207
int arp_initialize( async_client_conn_t client_connection ){
221
    ERROR_DECLARE;
208
    ERROR_DECLARE;
222
 
209
 
223
    rwlock_initialize( & arp_globals.lock );
210
    rwlock_initialize( & arp_globals.lock );
224
    rwlock_write_lock( & arp_globals.lock );
211
    rwlock_write_lock( & arp_globals.lock );
225
    arp_globals.client_connection = client_connection;
212
    arp_globals.client_connection = client_connection;
226
    ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache ));
213
    ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache ));
227
    rwlock_write_unlock( & arp_globals.lock );
214
    rwlock_write_unlock( & arp_globals.lock );
228
    return EOK;
215
    return EOK;
229
}
216
}
230
 
217
 
231
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 ){
232
    ERROR_DECLARE;
219
    ERROR_DECLARE;
233
 
220
 
234
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
221
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
235
    if( !( * proto )) return ENOMEM;
222
    if( !( * proto )) return ENOMEM;
236
    ( ** proto ).service = service;
223
    ( ** proto ).service = service;
237
    ( ** proto ).addr = address;
224
    ( ** proto ).addr = address;
238
    ( ** proto ).addr_data = address->value;
225
    ( ** proto ).addr_data = address->value;
239
    if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){
226
    if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){
240
        free( * proto );
227
        free( * proto );
241
        return ERROR_CODE;
228
        return ERROR_CODE;
242
    }
229
    }
243
    return EOK;
230
    return EOK;
244
}
231
}
245
 
232
 
246
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 ){
247
    ERROR_DECLARE;
234
    ERROR_DECLARE;
248
 
235
 
249
    arp_device_ref  device;
236
    arp_device_ref  device;
250
    arp_proto_ref   proto;
237
    arp_proto_ref   proto;
251
    int             index;
238
    int             index;
252
 
239
 
253
    rwlock_write_lock( & arp_globals.lock );
240
    rwlock_write_lock( & arp_globals.lock );
254
    // an existing device?
241
    // an existing device?
255
    device = arp_cache_find( & arp_globals.cache, device_id );
242
    device = arp_cache_find( & arp_globals.cache, device_id );
256
    if( device ){
243
    if( device ){
257
        if( device->service != service ){
244
        if( device->service != service ){
258
            printf( "Device %d already exists\n", device->device_id );
245
            printf( "Device %d already exists\n", device->device_id );
259
            rwlock_write_unlock( & arp_globals.lock );
246
            rwlock_write_unlock( & arp_globals.lock );
260
            return EEXIST;
247
            return EEXIST;
261
        }
248
        }
262
        proto = arp_protos_find( & device->protos, protocol );
249
        proto = arp_protos_find( & device->protos, protocol );
263
        if( proto ){
250
        if( proto ){
264
            free( proto->addr );
251
            free( proto->addr );
265
            free( proto->addr_data );
252
            free( proto->addr_data );
266
            proto->addr = address;
253
            proto->addr = address;
267
            proto->addr_data = address->value;
254
            proto->addr_data = address->value;
268
        }else{
255
        }else{
269
            if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
256
            if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
270
                rwlock_write_unlock( & arp_globals.lock );
257
                rwlock_write_unlock( & arp_globals.lock );
271
                return ERROR_CODE;
258
                return ERROR_CODE;
272
            }
259
            }
273
            index = arp_protos_add( & device->protos, proto->service, proto );
260
            index = arp_protos_add( & device->protos, proto->service, proto );
274
            if( index < 0 ){
261
            if( index < 0 ){
275
                rwlock_write_unlock( & arp_globals.lock );
262
                rwlock_write_unlock( & arp_globals.lock );
276
                free( proto );
263
                free( proto );
277
                return index;
264
                return index;
278
            }
265
            }
279
            printf( "New protocol added:\n\tdevice id\t= %d\n\tproto\t= %d", device_id, protocol );
266
            printf( "New protocol added:\n\tdevice id\t= %d\n\tproto\t= %d", device_id, protocol );
280
        }
267
        }
281
    }else{
268
    }else{
282
        index = hardware_map( service );
269
        index = hardware_map( service );
283
        if( ! index ) return ENOENT;
270
        if( ! index ) return ENOENT;
284
        // create a new device
271
        // create a new device
285
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
272
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
286
        if( ! device ){
273
        if( ! device ){
287
            rwlock_write_unlock( & arp_globals.lock );
274
            rwlock_write_unlock( & arp_globals.lock );
288
            return ENOMEM;
275
            return ENOMEM;
289
        }
276
        }
290
        device->hardware = index;
277
        device->hardware = index;
291
        device->device_id = device_id;
278
        device->device_id = device_id;
292
        if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
279
        if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
293
        || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
280
        || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
294
            rwlock_write_unlock( & arp_globals.lock );
281
            rwlock_write_unlock( & arp_globals.lock );
295
            free( device );
282
            free( device );
296
            return ERROR_CODE;
283
            return ERROR_CODE;
297
        }
284
        }
298
        index = arp_protos_add( & device->protos, proto->service, proto );
285
        index = arp_protos_add( & device->protos, proto->service, proto );
299
        if( index < 0 ){
286
        if( index < 0 ){
300
            rwlock_write_unlock( & arp_globals.lock );
287
            rwlock_write_unlock( & arp_globals.lock );
301
            arp_protos_destroy( & device->protos );
288
            arp_protos_destroy( & device->protos );
302
            free( device );
289
            free( device );
303
            return index;
290
            return index;
304
        }
291
        }
305
        device->service = service;
292
        device->service = service;
306
        // bind the new one
293
        // bind the new one
307
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_globals.client_connection );
294
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_globals.client_connection );
308
        if( device->phone < 0 ){
295
        if( device->phone < 0 ){
309
            rwlock_write_unlock( & arp_globals.lock );
296
            rwlock_write_unlock( & arp_globals.lock );
310
            arp_protos_destroy( & device->protos );
297
            arp_protos_destroy( & device->protos );
311
            free( device );
298
            free( device );
312
            return EREFUSED;
299
            return EREFUSED;
313
        }
300
        }
314
        // get packet dimensions
301
        // get packet dimensions
315
        if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
302
        if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
316
            rwlock_write_unlock( & arp_globals.lock );
303
            rwlock_write_unlock( & arp_globals.lock );
317
            arp_protos_destroy( & device->protos );
304
            arp_protos_destroy( & device->protos );
318
            free( device );
305
            free( device );
319
            return ERROR_CODE;
306
            return ERROR_CODE;
320
        }
307
        }
321
        // get hardware address
308
        // get hardware address
322
        if( ERROR_OCCURRED( nil_get_addr( device->phone, device_id, & device->addr, & device->addr_data ))){
309
        if( ERROR_OCCURRED( nil_get_addr( device->phone, device_id, & device->addr, & device->addr_data ))){
323
            rwlock_write_unlock( & arp_globals.lock );
310
            rwlock_write_unlock( & arp_globals.lock );
324
            arp_protos_destroy( & device->protos );
311
            arp_protos_destroy( & device->protos );
325
            free( device );
312
            free( device );
326
            return ERROR_CODE;
313
            return ERROR_CODE;
327
        }
314
        }
328
        // get broadcast address
315
        // get broadcast address
329
        if( ERROR_OCCURRED( nil_get_broadcast_addr( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){
316
        if( ERROR_OCCURRED( nil_get_broadcast_addr( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){
330
            rwlock_write_unlock( & arp_globals.lock );
317
            rwlock_write_unlock( & arp_globals.lock );
331
            free( device->addr );
318
            free( device->addr );
332
            free( device->addr_data );
319
            free( device->addr_data );
333
            arp_protos_destroy( & device->protos );
320
            arp_protos_destroy( & device->protos );
334
            free( device );
321
            free( device );
335
            return ERROR_CODE;
322
            return ERROR_CODE;
336
        }
323
        }
337
        if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
324
        if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
338
            rwlock_write_unlock( & arp_globals.lock );
325
            rwlock_write_unlock( & arp_globals.lock );
339
            free( device->addr );
326
            free( device->addr );
340
            free( device->addr_data );
327
            free( device->addr_data );
341
            free( device->broadcast_addr );
328
            free( device->broadcast_addr );
342
            free( device->broadcast_data );
329
            free( device->broadcast_data );
343
            arp_protos_destroy( & device->protos );
330
            arp_protos_destroy( & device->protos );
344
            free( device );
331
            free( device );
345
            return ERROR_CODE;
332
            return ERROR_CODE;
346
        }
333
        }
347
        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 );
334
        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 );
348
    }
335
    }
349
    rwlock_write_unlock( & arp_globals.lock );
336
    rwlock_write_unlock( & arp_globals.lock );
350
    return EOK;
337
    return EOK;
351
}
338
}
352
 
339
 
353
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
340
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
354
    arp_device_ref      device;
341
    arp_device_ref      device;
355
    arp_proto_ref       proto;
342
    arp_proto_ref       proto;
356
    measured_string_ref addr;
343
    measured_string_ref addr;
357
    size_t              length;
344
    size_t              length;
358
    packet_t            packet;
345
    packet_t            packet;
359
    arp_header_ref      header;
346
    arp_header_ref      header;
360
 
347
 
361
    if( ! target ) return NULL;
348
    if( ! target ) return NULL;
362
    device = arp_cache_find( & arp_globals.cache, device_id );
349
    device = arp_cache_find( & arp_globals.cache, device_id );
363
    if( ! device ) return NULL;
350
    if( ! device ) return NULL;
364
    proto = arp_protos_find( & device->protos, protocol );
351
    proto = arp_protos_find( & device->protos, protocol );
365
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
352
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
366
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
353
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
367
    if( addr ) return addr;
354
    if( addr ) return addr;
368
    // ARP packet content size = header + ( address + translation ) * 2
355
    // ARP packet content size = header + ( address + translation ) * 2
369
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
356
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
370
    if( length > device->content ) return NULL;
357
    if( length > device->content ) return NULL;
371
    packet = packet_get_4( arp_globals.net_phone, device->addr_len, device->prefix, length, device->suffix );
358
    packet = packet_get_4( arp_globals.net_phone, device->addr_len, device->prefix, length, device->suffix );
372
    if( ! packet ) return NULL;
359
    if( ! packet ) return NULL;
373
    header = ( arp_header_ref ) packet_suffix( packet, length );
360
    header = ( arp_header_ref ) packet_suffix( packet, length );
374
    if( ! header ){
361
    if( ! header ){
375
        pq_release( arp_globals.net_phone, packet_get_id( packet ));
362
        pq_release( arp_globals.net_phone, packet_get_id( packet ));
376
        return NULL;
363
        return NULL;
377
    }
364
    }
378
    header->hardware = htons( device->hardware );
365
    header->hardware = htons( device->hardware );
379
    header->hardware_length = device->addr->length;
366
    header->hardware_length = device->addr->length;
380
    header->protocol = htons( protocol_map( device->service, protocol ));
367
    header->protocol = htons( protocol_map( device->service, protocol ));
381
    header->protocol_length = proto->addr->length;
368
    header->protocol_length = proto->addr->length;
382
    header->operation = htons( ARPOP_REQUEST );
369
    header->operation = htons( ARPOP_REQUEST );
383
    length = sizeof( arp_header_t );
370
    length = sizeof( arp_header_t );
384
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
371
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
385
    length += device->addr->length;
372
    length += device->addr->length;
386
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
373
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
387
    length += proto->addr->length;
374
    length += proto->addr->length;
388
    bzero((( uint8_t * ) header ) + length, device->addr->length );
375
    bzero((( uint8_t * ) header ) + length, device->addr->length );
389
    length += device->addr->length;
376
    length += device->addr->length;
390
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
377
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
391
    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 ){
378
    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 ){
392
        pq_release( arp_globals.net_phone, packet_get_id( packet ));
379
        pq_release( arp_globals.net_phone, packet_get_id( packet ));
393
        return NULL;
380
        return NULL;
394
    }
381
    }
395
    nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
382
    nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
396
    return NULL;
383
    return NULL;
397
}
384
}
398
 
385
 
399
int arp_receive_message( device_id_t device_id, packet_t packet ){
386
int arp_receive_message( device_id_t device_id, packet_t packet ){
400
    ERROR_DECLARE;
387
    ERROR_DECLARE;
401
 
388
 
402
    size_t              length;
389
    size_t              length;
403
    arp_header_ref      header;
390
    arp_header_ref      header;
404
    arp_device_ref      device;
391
    arp_device_ref      device;
405
    arp_proto_ref       proto;
392
    arp_proto_ref       proto;
406
    measured_string_ref hw_source;
393
    measured_string_ref hw_source;
407
    uint8_t *           src_hw;
394
    uint8_t *           src_hw;
408
    uint8_t *           src_proto;
395
    uint8_t *           src_proto;
409
    uint8_t *           des_hw;
396
    uint8_t *           des_hw;
410
    uint8_t *           des_proto;
397
    uint8_t *           des_proto;
411
 
398
 
412
    length = packet_get_data_length( packet );
399
    length = packet_get_data_length( packet );
413
    if( length <= sizeof( arp_header_t )) return EINVAL;
400
    if( length <= sizeof( arp_header_t )) return EINVAL;
414
    device = arp_cache_find( & arp_globals.cache, device_id );
401
    device = arp_cache_find( & arp_globals.cache, device_id );
415
    if( ! device ) return ENOENT;
402
    if( ! device ) return ENOENT;
416
    header = ( arp_header_ref ) packet_get_data( packet );
403
    header = ( arp_header_ref ) packet_get_data( packet );
417
    if(( ntohs( header->hardware ) != device->hardware )
404
    if(( ntohs( header->hardware ) != device->hardware )
418
    || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){
405
    || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){
419
        return EINVAL;
406
        return EINVAL;
420
    }
407
    }
421
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol )));
408
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol )));
422
    if( ! proto ) return ENOENT;
409
    if( ! proto ) return ENOENT;
423
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
410
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
424
    src_proto = src_hw + header->hardware_length;
411
    src_proto = src_hw + header->hardware_length;
425
    des_hw = src_proto + header->protocol_length;
412
    des_hw = src_proto + header->protocol_length;
426
    des_proto = des_hw + header->hardware_length;
413
    des_proto = des_hw + header->hardware_length;
427
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
414
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
428
    // exists?
415
    // exists?
429
    if( hw_source ){
416
    if( hw_source ){
430
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
417
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
431
            return EINVAL;
418
            return EINVAL;
432
        }
419
        }
433
        memcpy( hw_source->value, src_hw, hw_source->length );
420
        memcpy( hw_source->value, src_hw, hw_source->length );
434
    }
421
    }
435
    // is my protocol address?
422
    // is my protocol address?
436
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->protocol_length )){
423
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->protocol_length )){
437
        return EINVAL;
424
        return EINVAL;
438
    }
425
    }
439
    if( ! str_lcmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
426
    if( ! str_lcmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
440
        // not already upadted?
427
        // not already upadted?
441
        if( ! hw_source ){
428
        if( ! hw_source ){
442
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
429
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
443
            if( ! hw_source ) return ENOMEM;
430
            if( ! hw_source ) return ENOMEM;
444
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ));
431
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ));
445
        }
432
        }
446
        if( ntohs( header->operation ) == ARPOP_REQUEST ){
433
        if( ntohs( header->operation ) == ARPOP_REQUEST ){
447
            header->operation = htons( ARPOP_REPLY );
434
            header->operation = htons( ARPOP_REPLY );
448
            memcpy( des_proto, src_proto, header->protocol_length );
435
            memcpy( des_proto, src_proto, header->protocol_length );
449
            memcpy( src_proto, proto->addr->value, header->protocol_length );
436
            memcpy( src_proto, proto->addr->value, header->protocol_length );
450
            memcpy( src_hw, device->addr->value, device->addr_len );
437
            memcpy( src_hw, device->addr->value, device->addr_len );
451
            memcpy( des_hw, hw_source->value, header->hardware_length );
438
            memcpy( des_hw, hw_source->value, header->hardware_length );
452
            ERROR_PROPAGATE( packet_set_addr( packet, src_hw, des_hw, header->hardware_length ));
439
            ERROR_PROPAGATE( packet_set_addr( packet, src_hw, des_hw, header->hardware_length ));
453
            nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
440
            nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
454
            return 1;
441
            return 1;
455
        }
442
        }
456
    }
443
    }
457
    return EOK;
444
    return EOK;
458
}
445
}
459
 
446
 
460
void clear_device( arp_device_ref device ){
447
void clear_device( arp_device_ref device ){
461
    int             count;
448
    int             count;
462
    arp_proto_ref   proto;
449
    arp_proto_ref   proto;
463
 
450
 
464
    for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){
451
    for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){
465
        proto = arp_protos_get_index( & device->protos, count );
452
        proto = arp_protos_get_index( & device->protos, count );
466
        if( proto ){
453
        if( proto ){
467
            if( proto->addr ) free( proto->addr );
454
            if( proto->addr ) free( proto->addr );
468
            if( proto->addr_data ) free( proto->addr_data );
455
            if( proto->addr_data ) free( proto->addr_data );
469
            arp_addr_destroy( & proto->addresses );
456
            arp_addr_destroy( & proto->addresses );
470
        }
457
        }
471
    }
458
    }
472
    arp_protos_clear( & device->protos );
459
    arp_protos_clear( & device->protos );
473
}
460
}
474
 
461
 
475
int arp_connect_module( services_t service ){
462
int arp_connect_module( services_t service ){
476
    return EOK;
463
    return EOK;
477
}
464
}
478
 
465
 
479
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
466
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
480
    ERROR_DECLARE;
467
    ERROR_DECLARE;
481
 
468
 
482
    measured_string_ref address;
469
    measured_string_ref address;
483
    measured_string_ref translation;
470
    measured_string_ref translation;
484
    char *              data;
471
    char *              data;
485
    packet_t            packet;
472
    packet_t            packet;
486
    packet_t            next;
473
    packet_t            next;
487
 
474
 
488
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_ARP_FIRST );
475
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_ARP_FIRST );
489
    * answer_count = 0;
476
    * answer_count = 0;
490
    switch( IPC_GET_METHOD( * call )){
477
    switch( IPC_GET_METHOD( * call )){
491
        case IPC_M_PHONE_HUNGUP:
478
        case IPC_M_PHONE_HUNGUP:
492
            return EOK;
479
            return EOK;
493
        case NET_ARP_DEVICE:
480
        case NET_ARP_DEVICE:
494
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
481
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
495
            if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_NETIF( call ), address ))){
482
            if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_NETIF( call ), address ))){
496
                free( address );
483
                free( address );
497
                free( data );
484
                free( data );
498
            }
485
            }
499
            return ERROR_CODE;
486
            return ERROR_CODE;
500
        case NET_ARP_TRANSLATE:
487
        case NET_ARP_TRANSLATE:
501
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
488
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
502
            rwlock_read_lock( & arp_globals.lock );
489
            rwlock_read_lock( & arp_globals.lock );
503
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address );
490
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address );
504
            free( address );
491
            free( address );
505
            free( data );
492
            free( data );
506
            if( ! translation ){
493
            if( ! translation ){
507
                rwlock_read_unlock( & arp_globals.lock );
494
                rwlock_read_unlock( & arp_globals.lock );
508
                return ENOENT;
495
                return ENOENT;
509
            }
496
            }
510
            ERROR_CODE = measured_strings_reply( translation, 1 );
497
            ERROR_CODE = measured_strings_reply( translation, 1 );
511
            rwlock_read_unlock( & arp_globals.lock );
498
            rwlock_read_unlock( & arp_globals.lock );
512
            return ERROR_CODE;
499
            return ERROR_CODE;
513
        case NET_ARP_CLEAR_DEVICE:
500
        case NET_ARP_CLEAR_DEVICE:
514
            return arp_clear_device_req( 0, IPC_GET_DEVICE( call ));
501
            return arp_clear_device_req( 0, IPC_GET_DEVICE( call ));
515
        case NET_ARP_CLEAN_CACHE:
502
        case NET_ARP_CLEAN_CACHE:
516
            return arp_clean_cache_req( 0 );
503
            return arp_clean_cache_req( 0 );
517
        case NET_IL_DEVICE_STATE:
504
        case NET_IL_DEVICE_STATE:
518
            // do nothing - keep the cache
505
            // do nothing - keep the cache
519
            return EOK;
506
            return EOK;
520
        case NET_IL_RECEIVED:
507
        case NET_IL_RECEIVED:
521
            if( ! ERROR_OCCURRED( packet_translate( arp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
508
            if( ! ERROR_OCCURRED( packet_translate( arp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
522
                rwlock_read_lock( & arp_globals.lock );
509
                rwlock_read_lock( & arp_globals.lock );
523
                do{
510
                do{
524
                    next = pq_next( packet );
511
                    next = pq_detach( packet );
525
                    pq_detach( packet );
-
 
526
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( call ), packet );
512
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( call ), packet );
527
                    if( ERROR_CODE != 1 ) pq_release( arp_globals.net_phone, packet_get_id( packet ));
513
                    if( ERROR_CODE != 1 ) pq_release( arp_globals.net_phone, packet_get_id( packet ));
528
                    packet = next;
514
                    packet = next;
529
                }while( packet );
515
                }while( packet );
530
                rwlock_read_unlock( & arp_globals.lock );
516
                rwlock_read_unlock( & arp_globals.lock );
531
            }
517
            }
532
            return ERROR_CODE;
518
            return ERROR_CODE;
533
    }
519
    }
534
    return ENOTSUP;
520
    return ENOTSUP;
535
}
521
}
536
 
522
 
537
/** @}
523
/** @}
538
 */
524
 */
539
 
525