Subversion Repositories HelenOS

Rev

Rev 3886 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3846 mejdrech 1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup arp
30
 *  @{
31
 */
32
 
33
/** @file
34
 */
35
 
36
#include <as.h>
37
#include <async.h>
38
#include <malloc.h>
39
#include <stdio.h>
40
#include <string.h>
41
 
42
#include <ipc/ipc.h>
43
#include <ipc/services.h>
44
 
45
#include "../err.h"
46
#include "../measured_strings.h"
47
#include "../messages.h"
48
#include "../modules.h"
49
#include "../packet.h"
50
 
51
#include "../include/protocol_map.h"
52
#include "../netif/device.h"
53
 
54
#include "arp.h"
55
#include "arp_header.h"
56
#include "arp_oc.h"
57
//#include "arp_messages.h"
58
#include "arp_module.h"
59
 
60
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
61
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
62
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG3( * call )
63
 
64
arp_globals_t   arp_globals;
65
 
66
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
67
 
68
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
69
 
70
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
71
 
72
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
73
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
74
measured_string_ref arp_translate_message( device_id_t device, services_t protocol, measured_string_ref target );
75
int arp_receive_message( device_id_t device_id, packet_t packet );
76
int arp_clear_device_message( device_id_t device_id );
77
void clear_device( arp_device_ref device );
78
int arp_clean_cache_message( void );
79
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
80
 
81
/** Initializes the ARP module.
82
 */
83
int arp_initialize( void ){
84
    arp_cache_initialize( & arp_globals.cache );
85
    return EOK;
86
}
87
 
88
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
89
    ERROR_DECLARE;
90
 
91
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
92
    if( !( * proto )) return ENOMEM;
93
    ( ** proto ).service = service;
94
    ( ** proto ).addr = address;
95
    ( ** proto ).addr_data = address->value;
96
    if( ERROR_OCCURED( arp_addr_initialize( &( ** proto).addresses ))){
97
        free( * proto );
98
        return ERROR_CODE;
99
    }
100
    return EOK;
101
}
102
 
103
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
104
    ERROR_DECLARE;
105
 
106
    arp_device_ref  device;
107
    aid_t           message;
108
    ipc_call_t      answer;
109
    ipcarg_t        result;
110
    arp_proto_ref   proto;
111
 
112
    // an existing device?
113
    device = arp_cache_find( & arp_globals.cache, device_id );
114
    if( device ){
115
        if( device->service != service ) return EEXIST;
116
        proto = arp_protos_find( & device->protos, protocol );
117
        if( proto ){
118
            free( proto->addr );
119
            free( proto->addr_data );
120
            proto->addr = address;
121
            proto->addr_data = address->value;
122
        }else{
123
            ERROR_PROPAGATE( arp_proto_create( & proto, protocol, address ));
124
            if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
125
                free( proto );
126
                return ERROR_CODE;
127
            }
128
        }
129
        return EOK;
130
    }else{
131
        // create a new device
132
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
133
        if( ! device ) return ENOMEM;
134
        device->device_id = device_id;
135
        if( ERROR_OCCURED( arp_protos_initialize( & device->protos ))
136
        || ERROR_OCCURED( arp_proto_create( & proto, protocol, address ))){
137
            free( device );
138
            return ERROR_CODE;
139
        }
140
        if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
141
            arp_protos_destroy( & device->protos );
142
            free( device );
143
            return ERROR_CODE;
144
        }
145
        device->service = service;
146
        // bind the new one
147
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
148
        // get packet dimensions
149
        if( ERROR_OCCURED( async_req_1_3( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->prefix, & device->content, & device->sufix ))){
150
            arp_protos_destroy( & device->protos );
151
            free( device );
152
            return ERROR_CODE;
153
        }
154
        // get hardware address
155
        message = async_send_1( device->phone, NET_NIL_ADDR, device->device_id, & answer );
156
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
157
            arp_protos_destroy( & device->protos );
158
            free( device );
159
            async_wait_for( message, NULL );
160
            return ERROR_CODE;
161
        }
162
        async_wait_for( message, & result );
163
        if( ERROR_OCCURED( result )){
164
            free( device->addr );
165
            free( device->addr_data );
166
            arp_protos_destroy( & device->protos );
167
            free( device );
168
            return ERROR_CODE;
169
        }
170
        // get broadcast address
171
        message = async_send_1( device->phone, NET_NIL_BROADCAST_ADDR, device->device_id, & answer );
172
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->broadcast_addr, & device->broadcast_data, 1 ))){
173
            free( device->addr );
174
            free( device->addr_data );
175
            arp_protos_destroy( & device->protos );
176
            free( device );
177
            async_wait_for( message, NULL );
178
            return ERROR_CODE;
179
        }
180
        async_wait_for( message, & result );
181
        // add to the cache
182
        if( ERROR_OCCURED( result )
183
        || ERROR_OCCURED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
184
            free( device->addr );
185
            free( device->addr_data );
186
            free( device->broadcast_addr );
187
            free( device->broadcast_data );
188
            arp_protos_destroy( & device->protos );
189
            free( device );
190
            return ERROR_CODE;
191
        }
192
    }
193
    return EOK;
194
}
195
 
196
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
197
//  ERROR_DECLARE;
198
 
199
    arp_device_ref      device;
200
    arp_proto_ref       proto;
201
    measured_string_ref addr;
202
    size_t              length;
203
    packet_t            packet;
204
    arp_header_ref      header;
205
 
206
    if( ! target ) return NULL;
207
    device = arp_cache_find( & arp_globals.cache, device_id );
208
    if( ! device ) return NULL;
209
    proto = arp_protos_find( & device->protos, protocol );
210
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
211
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
212
    if( addr ) return addr;
213
    // ARP packet content size = header + ( address + translation ) * 2
214
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
215
    if( length > device->content ){
216
        return NULL;
217
    }
218
    packet = packet_create( device->prefix, length, device->sufix );
219
    if( ! packet ) return NULL;
220
    header = ( arp_header_ref ) packet_append( packet, length );
221
    header->hardware = device->hardware;
222
    header->hardware_length = device->addr->length;
223
    header->protocol = protocol_map( device->service, protocol );
224
    header->protocol_length = proto->addr->length;
225
    header->operation = ARPOP_REQUEST;
226
    length = sizeof( arp_header_t );
227
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
228
    length += device->addr->length;
229
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
230
    length += proto->addr->length;
231
    memset((( uint8_t * ) header ) + length, 0, device->addr->length );
232
    length += device->addr->length;
233
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
234
    // TODO send to the device->broadcast_addr as arp protocol
235
    packet_send( packet, device->phone );
236
    return NULL;
237
}
238
 
239
int arp_receive_message( device_id_t device_id, packet_t packet ){
240
    ERROR_DECLARE;
241
 
242
    size_t              length;
243
    arp_header_ref      header;
244
    arp_device_ref      device;
245
    arp_proto_ref       proto;
246
//  arp_addr_ref        addr;
247
    measured_string_ref hw_source;
248
/*  measured_string_t   proto_target;
249
    aid_t               message;
250
    ipcarg_t            result;
251
    int                 index;
252
    ipc_call_t          answer;
253
*/  int8_t *            src_hw;
254
    int8_t *            src_proto;
255
    int8_t *            des_hw;
256
    int8_t *            des_proto;
257
 
258
    length = packet_get_data_length( packet );
259
    if( length <= sizeof( arp_header_t )) return EINVAL;
260
    device = arp_cache_find( & arp_globals.cache, device_id );
261
    if( ! device ) return ENOENT;
262
    header = ( arp_header_ref ) packet_get_data( packet );
263
    if( header->hardware != device->hardware ) return EINVAL;
264
    if( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 ) return EINVAL;
265
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
266
    if( ! proto ) return ENOENT;
267
    src_hw = (( int8_t * ) header ) + sizeof( arp_header_t );
268
    src_proto = src_hw + header->hardware_length;
269
    des_hw = src_proto + header->protocol_length;
270
    des_proto = des_hw + header->hardware_length;
271
    hw_source = arp_addr_find( & proto->addresses, src_proto, header->protocol_length );
272
    // exists?
273
    if( hw_source ){
274
        if( hw_source->length != header->hardware_length ) return EINVAL;
275
        memcpy( hw_source->value, src_hw, header->hardware_length );
276
    }
277
    // is my protocol address?
278
/*  proto_target.value = des_proto;
279
    proto_target.length = header->protocol_length;
280
    // TODO send necessary?
281
    message = async_send_0( proto->phone, NET_IL_MY_ADDR, & answer );
282
    if( ERROR_OCCURED( measured_strings_send( device->phone, & proto_target, 1 ))){
283
        async_wait_for( message, NULL );
284
        return ERROR_CODE;
285
    }
286
    async_wait_for( message, & result );
287
    if( result == EOK ){
288
*/  if( proto->addr->length != header->hardware_length ) return EINVAL;
289
    if( ! strncmp( proto->addr->value, des_proto, proto->addr->length )){
290
        // not already upadted?
291
        if( ! hw_source ){
292
            hw_source = measured_string_create_bulk( src_hw, header->hardware_length );
293
            if( ! hw_source ) return ENOMEM;
294
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, src_proto, header->protocol_length, hw_source ));
295
        }
296
        if( header->operation == ARPOP_REQUEST ){
297
            header->operation = ARPOP_REPLY;
298
/*          for( index = 0; index + header->hardware_length < header->protocol_length; index += header->hardware_length ){
299
                memcpy( src_hw, src_proto + index, header->hardware_length );
300
                memcpy( src_proto + index, des_proto + index, header->hardware_length );
301
                memcpy( des_proto + index, src_hw, header->hardware_length );
302
            }
303
            memcpy( src_hw, src_proto + index, header->hardware_length - header->protocol_length );
304
            memcpy( src_proto + index, des_proto + index, header->hardware_length - header->protocol_length );
305
            memcpy( des_proto + index, src_hw, header->hardware_length - header->protocol_length );
306
            memcpy( src_hw, des_hw, header->hardware_length );
307
            memcpy( des_hw, hw_source->value, hw_source->length );
308
*/          memcpy( des_proto, src_proto, header->protocol_length );
309
            memcpy( src_proto, proto->addr->value, header->protocol_length );
310
            memcpy( src_hw, des_hw, header->hardware_length );
311
            memcpy( des_hw, hw_source->value, hw_source->length );
312
            // TODO send to the hw_source as arp protocol
313
            packet_send( packet, device->phone );
314
        }
315
    }
316
    return EOK;
317
}
318
 
319
int arp_clear_device_message( device_id_t device_id ){
320
    arp_device_ref  device;
321
 
322
    device = arp_cache_find( & arp_globals.cache, device_id );
323
    if( ! device ) return ENOENT;
324
    clear_device( device );
325
    return EOK;
326
}
327
 
328
void clear_device( arp_device_ref device ){
329
    int             count;
330
    arp_proto_ref   proto;
331
 
332
    count = arp_protos_count( & device->protos );
333
    while( count > 0 ){
334
        proto = arp_protos_get_index( & device->protos, count );
335
        if( proto->addr ) free( proto->addr );
336
        if( proto->addr_data ) free( proto->addr_data );
337
        arp_addr_destroy( & proto->addresses );
338
        -- count;
339
    }
340
    arp_protos_clear( & device->protos );
341
}
342
 
343
int arp_clean_cache_message( void ){
344
    int             count;
345
    arp_device_ref  device;
346
 
347
    count = arp_cache_count( & arp_globals.cache );
348
    while( count > 0 ){
349
        device = arp_cache_get_index( & arp_globals.cache, count );
350
        if( device ){
351
            clear_device( device );
352
            if( device->broadcast_addr ) free( device->broadcast_addr );
353
            if( device->broadcast_data ) free( device->broadcast_data );
354
        }
355
    }
356
    arp_cache_clear( & arp_globals.cache );
357
    return EOK;
358
}
359
 
360
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
361
    ERROR_DECLARE;
362
 
363
//  packet_t            packet;
364
    measured_string_ref address;
365
    measured_string_ref translation;
366
    char *              data;
367
 
368
    * answer_count = 0;
369
    switch( IPC_GET_METHOD( * call )){
370
        case IPC_M_PHONE_HUNGUP:
371
            return EOK;
372
        case NET_ARP_DEVICE:
373
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
374
            if( ERROR_OCCURED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_PROTO( call ), address ))){
375
                free( address );
376
                free( data );
377
            }
378
            return ERROR_CODE;
379
        case NET_ARP_TRANSLATE:
380
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
381
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
382
            free( address );
383
            free( data );
384
            if( ! translation ) return ENOENT;
385
            return measured_strings_reply( translation, 1 );
386
        case NET_ARP_CLEAR_DEVICE:
387
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
388
        case NET_ARP_CLEAN_CACHE:
389
            return arp_clean_cache_message();
390
    }
391
    return ENOTSUP;
392
}
393
 
394
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
395
    ERROR_DECLARE;
396
 
397
    ipc_callid_t    callid;
398
    ipc_call_t      call;
399
//  int             result;
400
    packet_t        packet;
401
 
402
    /*
403
     * Accept the connection
404
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
405
     */
406
    //TODO auto accept?
407
    //ipc_answer_0( iid, EOK );
408
 
409
    while( true ){
410
        callid = async_get_call( & call );
411
        switch( IPC_GET_METHOD( call )){
412
            case NET_IL_DEVICE_STATE:
413
                //TODO clear device if off?
414
                break;
415
            case NET_IL_RECEIVED:
416
                if( ! ERROR_OCCURED( packet_receive( & packet ))){
417
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( & call ), packet );
418
                }
419
                ipc_answer_0( callid, ERROR_CODE );
420
                break;
421
            default:
422
                ipc_answer_0( callid, ENOTSUP );
423
        }
424
    }
425
}
426
 
427
/** @}
428
 */