Subversion Repositories HelenOS

Rev

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

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