Subversion Repositories HelenOS

Rev

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

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