Subversion Repositories HelenOS

Rev

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