Subversion Repositories HelenOS

Rev

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

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