Subversion Repositories HelenOS

Rev

Rev 4243 | Rev 4271 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3466 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3466 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
 
3912 mejdrech 29
/** @addtogroup ip
30
 *  @{
3466 mejdrech 31
 */
32
 
33
/** @file
34
 */
3666 mejdrech 35
 
3466 mejdrech 36
#include <async.h>
37
#include <errno.h>
38
#include <stdio.h>
3846 mejdrech 39
 
3466 mejdrech 40
#include <ipc/ipc.h>
41
#include <ipc/services.h>
42
 
3886 mejdrech 43
#include "../../err.h"
44
#include "../../messages.h"
45
#include "../../modules.h"
3466 mejdrech 46
 
3886 mejdrech 47
#include "../../include/sockaddr.h"
48
#include "../../include/socket.h"
4243 mejdrech 49
#include "../../include/device.h"
4261 mejdrech 50
#include "../../include/nil_messages.h"
3886 mejdrech 51
#include "../../structures/measured_strings.h"
4192 mejdrech 52
#include "../../structures/module_map.h"
3901 mejdrech 53
#include "../../structures/packet/packet_client.h"
3846 mejdrech 54
 
3466 mejdrech 55
#include "ip.h"
3846 mejdrech 56
#include "ip_messages.h"
57
#include "ip_module.h"
3466 mejdrech 58
 
3846 mejdrech 59
#define DEFAULT_IPV     4
3685 mejdrech 60
 
4192 mejdrech 61
#define ARP_NAME                "arp"
62
#define ARP_FILENAME            "/srv/arp"
63
 
3846 mejdrech 64
#define IPC_GET_PROTO( call )       ( int ) IPC_GET_ARG1( * call )
65
#define IPC_GET_STATE( call )       ( device_state_t ) IPC_GET_ARG2( * call )
66
#define IPC_GET_PHONE( call )       ( int ) IPC_GET_ARG5( * call )
67
 
3666 mejdrech 68
ip_globals_t    ip_globals;
3466 mejdrech 69
 
3666 mejdrech 70
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
71
 
3846 mejdrech 72
INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )
3685 mejdrech 73
 
3846 mejdrech 74
int ip_register_message( int protocol, int phone );
75
int ip_state_message( device_id_t device_id, device_state_t state );
76
void    ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall );
77
 
3466 mejdrech 78
/** Initializes the module.
79
 */
80
int ip_initialize( void ){
4192 mejdrech 81
    ERROR_DECLARE;
82
 
83
    ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
84
    ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
85
    ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
86
    ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, 0 ));
3466 mejdrech 87
    return EOK;
88
}
89
 
3846 mejdrech 90
int ip_echo_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t * answer1, ipcarg_t * answer2, ipcarg_t * answer3, ipcarg_t * answer4, ipcarg_t * answer5 ){
91
    if( answer1 ) * answer1 = arg1;
92
    if( answer2 ) * answer2 = arg2;
93
    if( answer3 ) * answer3 = arg3;
94
    if( answer4 ) * answer4 = arg4;
95
    if( answer5 ) * answer5 = arg5;
3466 mejdrech 96
    return EOK;
97
}
98
 
3846 mejdrech 99
int ip_device_message( device_id_t device_id, services_t service ){
3666 mejdrech 100
    ERROR_DECLARE;
101
 
4192 mejdrech 102
    ip_netif_ref    ip_netif;
3685 mejdrech 103
    aid_t           message;
104
    ipc_call_t      answer;
105
    measured_string_t   configuration[ 9 ] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "NETMASK", 7 }, { "GATEWAY", 7 }, { "BROADCAST", 9 }, { "DNS1", 4 }, { "DNS2", 4 }, { "ARP", 3 }};
106
    int         count = 9;
107
    measured_string_ref settings;
108
    char *          data;
4192 mejdrech 109
    ipcarg_t        result;
110
    int             index;
3666 mejdrech 111
 
3846 mejdrech 112
    ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
113
    if( ! ip_netif ) return ENOMEM;
114
    ip_netif->device_id = device_id;
115
    // get configuration
116
    // TODO mapping
117
    message = async_send_2( ip_globals.networking_phone, NET_NET_GET_DEVICE_CONF, ip_netif->device_id, count, & answer );
118
    // send names and get settings
3912 mejdrech 119
    if( ERROR_OCCURRED( measured_strings_send( ip_globals.networking_phone, configuration, count ))
120
    || ERROR_OCCURRED( measured_strings_return( ip_globals.networking_phone, & settings, & data, count ))){
3846 mejdrech 121
        async_wait_for( message, NULL );
4192 mejdrech 122
        free( ip_netif );
3846 mejdrech 123
        return ERROR_CODE;
124
    }
4192 mejdrech 125
    async_wait_for( message, & result );
126
    if( ERROR_OCCURRED( result )){
127
        if( settings ){
128
            free( settings );
129
            free( data );
130
        };
131
        free( ip_netif );
132
        return ERROR_CODE;
133
    }
3846 mejdrech 134
    if( settings ){
135
        if( settings[ 0 ].value ){
136
            ip_netif->ipv = strtol( settings[ 0 ].value, NULL, 0 );
137
        }else{
138
            ip_netif->ipv = DEFAULT_IPV;
139
        }
4192 mejdrech 140
        ip_netif->dhcp = ! strncmp( settings[ 1 ].value, "dhcp", 4 );
3846 mejdrech 141
        if( ip_netif->dhcp ){
142
            // TODO dhcp
4192 mejdrech 143
            free( settings );
144
            free( data );
3846 mejdrech 145
            free( ip_netif );
146
            return ENOTSUP;
147
        }else if( ip_netif->ipv == 4 ){
3912 mejdrech 148
            if( ERROR_OCCURRED( inet_pton( AF_INET, settings[ 2 ].value, ( uint8_t * ) & ip_netif->address ))
149
            || ERROR_OCCURRED( inet_pton( AF_INET, settings[ 3 ].value, ( uint8_t * ) & ip_netif->netmask ))
3846 mejdrech 150
            || ( inet_pton( AF_INET, settings[ 4 ].value, ( uint8_t * ) & ip_netif->gateway ) == EINVAL )
151
            || ( inet_pton( AF_INET, settings[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL )
152
            || ( inet_pton( AF_INET, settings[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL )
153
            || ( inet_pton( AF_INET, settings[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){
4192 mejdrech 154
                free( settings );
155
                free( data );
3846 mejdrech 156
                free( ip_netif );
157
                return EINVAL;
158
            }
159
        }else{
160
            // TODO ipv6
4192 mejdrech 161
            free( settings );
162
            free( data );
3846 mejdrech 163
            free( ip_netif );
164
            return ENOTSUP;
165
        }
4192 mejdrech 166
        if( settings[ 8 ].value ){
167
            ip_netif->arp = get_running_module( & ip_globals.modules, settings[ 8 ].value );
168
            if( ! ip_netif->arp ){
169
                printf( "\nFailed to start the arp %s", settings[ 8 ].value );
170
                free( settings );
171
                free( data );
172
                free( ip_netif );
173
                return EINVAL;
174
            }
175
        }else{
176
            ip_netif->arp = NULL;
177
        }
178
        free( settings );
179
        free( data );
3846 mejdrech 180
    }
4192 mejdrech 181
    ip_netif->phone = bind_service( service, ip_netif->device_id, SERVICE_IP, 0, ip_driver_receiver );
182
    if( ip_netif->phone < 0 ){
183
        printf( "\nFailed to contact the nil service %d", service );
184
        free( ip_netif );
185
        return ip_netif->phone;
186
    }
187
    if( ip_netif->arp ){
188
        message = async_send_3( ip_netif->arp->phone, NET_ARP_DEVICE, ip_netif->device_id, SERVICE_IP, service, & answer );
189
        configuration[ 0 ].value = ( char * ) & ip_netif->address;
190
        configuration[ 0 ].length = CONVERT_SIZE( in_addr_t, char, 1 );
191
        if( ERROR_OCCURRED( measured_strings_send( ip_netif->arp->phone, & configuration[ 0 ], 1 ))){
192
            free( ip_netif );
193
            return ERROR_CODE;
194
        }
195
        async_wait_for( message, & result );
196
        if( ERROR_OCCURRED( result )){
197
            free( ip_netif );
198
            return ERROR_CODE;
199
        }
200
    }
201
    index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
202
    if( index < 0 ){
203
        free( ip_netif );
204
        return index;
205
    }
206
    if( ip_netif->arp ) ++ ip_netif->arp->usage;
3846 mejdrech 207
    // print the settings
4163 mejdrech 208
    printf( "\nNew device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
209
    printf( "\n\tconfiguration\t= %s", ip_netif->dhcp ? "dhcp" : "static" );
4192 mejdrech 210
    // TODO ipv6 addresses
3846 mejdrech 211
    data = malloc( INET_ADDRSTRLEN );
212
    if( data ){
213
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->address, data, INET_ADDRSTRLEN );
4163 mejdrech 214
        printf( "\n\taddress\t= %s", data );
3846 mejdrech 215
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->netmask, data, INET_ADDRSTRLEN );
4163 mejdrech 216
        printf( "\n\tnetmask\t= %s", data );
3846 mejdrech 217
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->gateway, data, INET_ADDRSTRLEN );
4163 mejdrech 218
        printf( "\n\tgateway\t= %s", data );
3846 mejdrech 219
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast, data, INET_ADDRSTRLEN );
4163 mejdrech 220
        printf( "\n\tbroadcast\t= %s", data );
3846 mejdrech 221
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns1, data, INET_ADDRSTRLEN );
4163 mejdrech 222
        printf( "\n\tdns1\t= %s", data );
3846 mejdrech 223
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN );
4163 mejdrech 224
        printf( "\n\tdns2\t= %s", data );
3846 mejdrech 225
        free( data );
226
    }
227
    return EOK;
228
}
229
 
230
void ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall ){
231
    ERROR_DECLARE;
232
 
233
    ipc_callid_t    callid;
234
    ipc_call_t  call;
235
//  ipc_call_t  answer;
236
//  int     count;
237
    int     result;
238
    packet_t    packet;
239
 
240
    /*
241
     * Accept the connection
242
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
243
     */
244
    ipc_answer_0( iid, EOK );
245
 
246
    while( true ){
247
/*      // refresh data
248
        count = 0;
249
        IPC_SET_RETVAL( answer, 0 );
250
        // just to be precize
251
        IPC_SET_RETVAL( answer, 0 );
252
        IPC_SET_ARG1( answer, 0 );
253
        IPC_SET_ARG2( answer, 0 );
254
        IPC_SET_ARG3( answer, 0 );
255
        IPC_SET_ARG4( answer, 0 );
256
        IPC_SET_ARG5( answer, 0 );
257
*/
258
        callid = async_get_call( & call );
259
        switch( IPC_GET_METHOD( call )){
260
            case NET_IL_DEVICE_STATE:
261
            case NET_NIL_DEVICE_STATE:
262
                result = ip_state_message( IPC_GET_DEVICE( & call ), IPC_GET_STATE( & call ));
263
                ipc_answer_0( callid, result );
264
            // TODO packer received
265
            case NET_IL_RECEIVED:
266
            case NET_NIL_RECEIVED:
3912 mejdrech 267
                if( ! ERROR_OCCURRED( result = packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( & call )))){
3901 mejdrech 268
                    //result = ip_receive_message( IPC_GET_DEVICE( call ), packet );
3846 mejdrech 269
                }
270
                ipc_answer_0( callid, result );
271
        }
272
    }
273
}
274
 
275
int ip_state_message( device_id_t device_id, device_state_t state ){
4243 mejdrech 276
    ERROR_DECLARE;
277
 
278
    ip_netif_ref    netif;
279
 
280
    aid_t           message;
281
    ipc_call_t      answer;
282
    measured_string_t address;
283
    measured_string_ref translation;
284
    char *          data;
285
    ipcarg_t        result;
286
 
287
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
288
    if( ! netif ) return ENOENT;
3846 mejdrech 289
    // TODO state
290
    printf( "\nip - device %d changed state to %d\n", device_id, state );
4243 mejdrech 291
    if( netif->arp ){
292
        message = async_send_2( netif->arp->phone, NET_ARP_TRANSLATE, netif->device_id, SERVICE_IP, & answer );
293
        address.value = ( char * ) & netif->gateway;
294
        address.length = CONVERT_SIZE( in_addr_t, char, 1 );
295
        if( ERROR_OCCURRED( measured_strings_send( netif->arp->phone, & address, 1 ))
296
        || ERROR_OCCURRED( measured_strings_return( netif->arp->phone, & translation, & data, 1 ))){
297
            async_wait_for( message, & result );
298
            return result;
299
        }
300
        async_wait_for( message, & result );
301
        if( ! ERROR_OCCURRED( result )){
302
            printf( "\n\tgateway translated to\t= %X:%X:%X:%X:%X:%X", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ] );
303
        }
304
        free( translation );
305
        free( data );
306
        return result;
307
    }
3846 mejdrech 308
    return EOK;
309
}
310
 
311
int ip_register_message( int protocol, int phone ){
312
    ip_proto_ref    proto;
4192 mejdrech 313
    int             index;
3846 mejdrech 314
 
315
    proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
316
    if( ! proto ) return ENOMEM;
317
    proto->protocol = protocol;
318
    proto->phone = phone;
4192 mejdrech 319
    index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
320
    if( index < 0 ){
3846 mejdrech 321
        free( proto );
4192 mejdrech 322
        return index;
3846 mejdrech 323
    }
4163 mejdrech 324
    printf( "\nNew protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d", proto->protocol, proto->phone );
3846 mejdrech 325
    return EOK;
326
}
327
 
328
int ip_send_message( device_id_t device_id, packet_t packet ){
329
    // TODO send packet
330
    printf( "Packet to send via %d: %s", device_id, packet_get_data( packet ));
4192 mejdrech 331
    pq_release( ip_globals.networking_phone, packet_get_id( packet ));
3846 mejdrech 332
    return EOK;
333
}
334
 
335
int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
336
    ERROR_DECLARE;
337
 
338
    packet_t    packet;
339
 
340
    * answer_count = 0;
341
    switch( IPC_GET_METHOD( * call )){
3466 mejdrech 342
        case IPC_M_PHONE_HUNGUP:
343
            return EOK;
344
        case NET_IP_ECHO:
3846 mejdrech 345
            * answer_count = 5;
346
            return ip_echo_message( IPC_GET_ARG1( * call ), IPC_GET_ARG2( * call ), IPC_GET_ARG3( * call ), IPC_GET_ARG4( * call ), IPC_GET_ARG5( * call ), & IPC_GET_ARG1( * answer ), & IPC_GET_ARG2( * answer ), & IPC_GET_ARG3( * answer ), & IPC_GET_ARG4( * answer ), & IPC_GET_ARG5( * answer ) );
3666 mejdrech 347
        case NET_IL_DEVICE:
3846 mejdrech 348
            return ip_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
349
        case IPC_M_CONNECT_TO_ME:
350
            return ip_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
351
        case NET_IP_SEND:
4192 mejdrech 352
            ERROR_PROPAGATE( packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
3846 mejdrech 353
            return ip_send_message( IPC_GET_DEVICE( call ), packet );
3466 mejdrech 354
    }
355
    return ENOTSUP;
356
}
357
 
358
/** @}
359
 */