Subversion Repositories HelenOS

Rev

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

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