Subversion Repositories HelenOS

Rev

Rev 4155 | Rev 4192 | 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
 
29
/** @addtogroup net
3912 mejdrech 30
 *  @{
3466 mejdrech 31
 */
32
 
33
/** @file
34
 */
35
 
36
#include <async.h>
3666 mejdrech 37
#include <ctype.h>
4163 mejdrech 38
#include <ddi.h>
3466 mejdrech 39
#include <errno.h>
3666 mejdrech 40
#include <malloc.h>
3466 mejdrech 41
#include <stdio.h>
42
#include <task.h>
3666 mejdrech 43
#include <unistd.h>
3846 mejdrech 44
 
3466 mejdrech 45
#include <ipc/ipc.h>
46
#include <ipc/services.h>
47
 
3666 mejdrech 48
#include "../err.h"
49
#include "../messages.h"
3466 mejdrech 50
#include "../modules.h"
3666 mejdrech 51
//#include "../self_test.h"
3466 mejdrech 52
 
3886 mejdrech 53
#include "../structures/char_map.h"
54
#include "../structures/generic_char_map.h"
55
#include "../structures/measured_strings.h"
3901 mejdrech 56
#include "../structures/packet/packet.h"
57
#include "../structures/packet/packet_server.h"
3886 mejdrech 58
 
59
#include "../il/ip/ip_messages.h"
3846 mejdrech 60
#include "../netif/device.h"
3666 mejdrech 61
 
3846 mejdrech 62
#if IP_BUNDLE
3466 mejdrech 63
 
3846 mejdrech 64
    #include "../ip/ip_module.h"
3466 mejdrech 65
 
66
#endif
67
 
3846 mejdrech 68
#if TCP_BUNDLE
69
 
70
    #include "../tcp/tcp_module.h"
71
 
72
#endif
73
 
3901 mejdrech 74
#define NAME    "Networking"
75
 
3666 mejdrech 76
#define LO_NAME             "lo"
4155 mejdrech 77
#define LO_FILENAME         "/srv/lo"
78
#define DP8390_NAME         "dp8390"
4163 mejdrech 79
#define DP8390_FILENAME     "/srv/dp8390"
80
#define ETHERNET_NAME       "ethernet"
81
#define ETHERNET_FILENAME   "/srv/eth"
3666 mejdrech 82
#define IP_NAME             "ip"
4155 mejdrech 83
#define IP_FILENAME         "/srv/ip"
3466 mejdrech 84
 
4163 mejdrech 85
#define CONF_NAME           "NAME"
86
#define CONF_NETIF          "NETIF"
87
#define CONF_NIL            "NIL"
88
#define CONF_IL             "IL"
89
#define CONF_IRQ            "IRQ"
90
#define CONF_IO             "IO"
91
#define CONF_MTU            "MTU"
92
 
3901 mejdrech 93
#define IPC_GET_DEVICE( call )  ( device_id_t ) IPC_GET_ARG1( * call )
94
#define IPC_GET_COUNT( call )   ( int ) IPC_GET_ARG2( * call )
95
 
3666 mejdrech 96
typedef struct module_struct    module_t;
97
typedef module_t *      module_ref;
3466 mejdrech 98
 
3666 mejdrech 99
typedef struct netif        netif_t;
100
typedef netif_t *       netif_ref;
3466 mejdrech 101
 
3666 mejdrech 102
typedef struct networking_globals   networking_globals_t;
3466 mejdrech 103
 
3666 mejdrech 104
DEVICE_MAP_DECLARE( netifs, netif_t )
3466 mejdrech 105
 
3666 mejdrech 106
GENERIC_CHAR_MAP_DECLARE( measured_strings, measured_string_t )
3466 mejdrech 107
 
3666 mejdrech 108
GENERIC_CHAR_MAP_DECLARE( modules, module_t )
3466 mejdrech 109
 
3666 mejdrech 110
struct module_struct{
111
    task_id_t   task_id;
112
    services_t  service;
4163 mejdrech 113
    int         phone;
114
    int         usage;
115
    char *      name;
116
    char *      filename;
3666 mejdrech 117
};
3466 mejdrech 118
 
3666 mejdrech 119
/** A present network interface device.
120
 */
121
struct netif{
122
    /** A system-unique network interface identifier.
123
     */
3846 mejdrech 124
    device_id_t     id;
3666 mejdrech 125
    /** A serving network interface driver module index.
126
     */
3846 mejdrech 127
    module_ref      driver;
3666 mejdrech 128
    /** A serving link layer module index.
129
     */
3846 mejdrech 130
    module_ref      nil;
3666 mejdrech 131
    /** A serving internet layer module index.
132
     */
3846 mejdrech 133
    module_ref      il;
3666 mejdrech 134
    /** A system-unique network interface name.
135
     */
136
    char *          name;
137
    /** Configuration.
138
     */
139
    measured_strings_t  configuration;
140
};
3466 mejdrech 141
 
3666 mejdrech 142
/** A networking module global variables.
143
 */
144
struct networking_globals{
145
    /** Present network interfaces.
146
     */
147
    netifs_t        netifs;
148
    /** Network interface structure indices by names.
149
     */
150
    char_map_t      netif_names;
151
    /** Available modules.
152
     */
153
    modules_t       modules;
154
    /** Global configuration.
155
     */
156
    measured_strings_t  configuration;
157
};
3466 mejdrech 158
 
3901 mejdrech 159
void    networking_print_name( void );
4163 mejdrech 160
int     add_module( module_ref * module, modules_ref modules, char * name, char * filename, services_t service, task_id_t task_id );
3666 mejdrech 161
measured_string_ref configuration_find( measured_strings_ref configuration, const char * name );
3901 mejdrech 162
int networking_start_module( async_client_conn_t client_connection );
3666 mejdrech 163
int     networking_initialize( void );
3901 mejdrech 164
int networking_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
165
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
3666 mejdrech 166
//int       parse_line( measured_strings_ref configuration, char * line );
167
int     add_configuration( measured_strings_ref configuration, const char * name, const char * value );
168
int     read_configuration( void );
4163 mejdrech 169
task_id_t   spawn( char * fname );
3666 mejdrech 170
int     startup( void );
3846 mejdrech 171
device_id_t generate_new_device_id( void );
3666 mejdrech 172
 
173
static networking_globals_t networking_globals;
174
 
175
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
176
 
177
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
178
 
179
GENERIC_CHAR_MAP_IMPLEMENT( modules, module_t )
180
 
3901 mejdrech 181
void networking_print_name( void ){
182
    printf( NAME );
183
}
184
 
4163 mejdrech 185
int add_module( module_ref * module, modules_ref modules, char * name, char * filename, services_t service, task_id_t task_id ){
3466 mejdrech 186
    ERROR_DECLARE;
187
 
3666 mejdrech 188
    module_ref  tmp_module;
3466 mejdrech 189
 
3666 mejdrech 190
    tmp_module = ( module_ref ) malloc( sizeof( module_t ));
191
    if( ! tmp_module ) return ENOMEM;
192
    tmp_module->task_id = task_id;
193
    tmp_module->phone = 0;
194
    tmp_module->usage = 0;
195
    tmp_module->name = name;
196
    tmp_module->filename = filename;
197
    tmp_module->service = service;
3912 mejdrech 198
    if( ERROR_OCCURRED( modules_add( modules, tmp_module->name, 0, tmp_module ))){
3666 mejdrech 199
        free( tmp_module );
200
        return ERROR_CODE;
201
    }
202
    if( module ) * module = tmp_module;
3466 mejdrech 203
    return EOK;
204
}
205
 
3901 mejdrech 206
int networking_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
3466 mejdrech 207
#ifdef NETWORKING_module
3901 mejdrech 208
        //TODO map to *_message
3666 mejdrech 209
        if( IS_NET_IL_MESSAGE( call ) || IS_NET_IP_MESSAGE( call )){
3901 mejdrech 210
            return ip_message( callid, call, answer, answer_count );
3466 mejdrech 211
/*      }else if( IS_NET_ARP_MESSAGE( call )){
3901 mejdrech 212
            return arp_message( callid, call, answer, answer_count );
3466 mejdrech 213
*//*        }else if( IS_NET_RARP_MESSAGE( call )){
3901 mejdrech 214
            return rarp_message( callid, call, answer, answer_count );
3466 mejdrech 215
*//*        }else if( IS_NET_ICMP_MESSAGE( call )){
3901 mejdrech 216
            return icmp_message( callid, call, answer, answer_count );
3466 mejdrech 217
*//*        }else if( IS_NET_UDP_MESSAGE( call )){
3901 mejdrech 218
            return udp_message( callid, call, answer, answer_count );
3466 mejdrech 219
*/      }else if( IS_NET_TCP_MESSAGE( call )){
3901 mejdrech 220
            return tcp_message( callid, call, answer, answer_count );
3466 mejdrech 221
/*      }else if( IS_NET_SOCKET_MESSAGE( call )){
3901 mejdrech 222
            return socket_message( callid, call, answer, answer_count );
3846 mejdrech 223
*//*        }else if( IS_NET_NIL_MESSAGE( call ) || IS_NET_ETHERNET_MESSAGE( call )){
3901 mejdrech 224
            return ethernet_message( callid, call, answer, answer_count );
3466 mejdrech 225
*/      }else{
226
#endif
3901 mejdrech 227
            if( IS_NET_PACKET_MESSAGE( call )){
228
                return packet_server_message( callid, call, answer, answer_count );
229
            }else{
230
                return net_message( callid, call, answer, answer_count );
231
            }
3466 mejdrech 232
#ifdef NETWORKING_module
233
        }
234
#endif
235
}
236
 
3901 mejdrech 237
int networking_start_module( async_client_conn_t client_connection ){
3846 mejdrech 238
    ERROR_DECLARE;
3466 mejdrech 239
 
3846 mejdrech 240
    ipcarg_t    phonehash;
241
 
242
    async_set_client_connection( client_connection );
3901 mejdrech 243
    ERROR_PROPAGATE( pm_init());
3914 mejdrech 244
    if( ERROR_OCCURRED( networking_initialize())
245
    || ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){
246
        pm_destroy();
247
        return ERROR_CODE;
248
    }
249
 
3846 mejdrech 250
    async_manager();
251
 
3914 mejdrech 252
    pm_destroy();
3666 mejdrech 253
    return EOK;
254
}
255
 
256
int networking_initialize( void ){
257
    ERROR_DECLARE;
258
 
259
    task_id_t   task_id;
260
 
261
    netifs_initialize( & networking_globals.netifs );
262
    char_map_initialize( & networking_globals.netif_names );
263
    modules_initialize( & networking_globals.modules );
264
    measured_strings_initialize( & networking_globals.configuration );
265
 
266
    // run self tests
267
//  ERROR_PROPAGATE( self_test());
268
 
269
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, LO_NAME, LO_FILENAME, SERVICE_LO, 0 ));
4155 mejdrech 270
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, DP8390_NAME, DP8390_FILENAME, SERVICE_DP8390, 0 ));
3666 mejdrech 271
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0 ));
272
 
273
#ifdef NETWORKING_modular
4155 mejdrech 274
    task_id = spawn( "/srv/ip" );
3666 mejdrech 275
    if( ! task_id ) return EINVAL;
276
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_id ));
4155 mejdrech 277
//  if( ! spawn( "/srv/udp" )) return EINVAL;
4163 mejdrech 278
//  if( ! spawn( "/srv/tcp" )) return EINVAL;
4155 mejdrech 279
//  if( ! spawn( "/srv/socket" )) return EINVAL;
3666 mejdrech 280
//  not always necesssary
4155 mejdrech 281
//  if( ! spawn( "/srv/arp" )) return EINVAL;
282
//  if( ! spawn( "/srv/rarp" )) return EINVAL;
283
//  if( ! spawn( "/srv/icmp" )) return EINVAL;
3666 mejdrech 284
 
285
#else
286
#ifdef NETWORKING_module
287
    ipcarg_t    phonehash;
288
 
289
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_IP, & phonehash ));
290
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_get_id()));
291
    ERROR_PROPAGATE( ip_initialize());
292
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ARP, & phonehash ));
293
//  ERROR_PROPAGATE( arp_initialize());
294
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_RARP, & phonehash ));
295
//  ERROR_PROPAGATE( rarp_initialize());
296
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ICMP, & phonehash ));
297
//  ERROR_PROPAGATE( icmp_initialize());
298
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_UDP, & phonehash ));
299
//  ERROR_PROPAGATE( udp_initialize());
300
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_TCP, & phonehash ));
301
    ERROR_PROPAGATE( tcp_initialize());
302
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_SOCKET, & phonehash ));
303
//  ERROR_PROPAGATE( socket_initialize());
304
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ETHERNET, & phonehash ));
305
//  ERROR_PROPAGATE( ethernet_initialize());
306
#endif
307
#endif
308
 
309
    return EOK;
310
}
311
 
3901 mejdrech 312
//TODO as ip.c
313
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
3666 mejdrech 314
    ERROR_DECLARE;
315
 
316
    measured_string_ref strings;
317
    char *          data;
318
    int         index;
319
    measured_string_ref setting;
320
    measured_strings_ref    configuration;
321
    netif_ref       netif;
322
 
3901 mejdrech 323
    * answer_count = 0;
324
    switch( IPC_GET_METHOD( * call )){
3666 mejdrech 325
        case IPC_M_PHONE_HUNGUP:
326
            return EOK;
3846 mejdrech 327
        case NET_NET_DEVICE:
3666 mejdrech 328
            // TODO configure, register
3901 mejdrech 329
            printf( "\nNetworking: new netif %d", IPC_GET_DEVICE( call ));
3666 mejdrech 330
            return EOK;
3846 mejdrech 331
        case NET_NET_GET_DEVICE_CONF:
3901 mejdrech 332
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
333
            netif = netifs_find( & networking_globals.netifs, IPC_GET_DEVICE( call ));
3666 mejdrech 334
            if( netif ){
335
                configuration = & netif->configuration;
336
            }else{
337
                configuration = NULL;
338
            }
3901 mejdrech 339
            for( index = 0; index < IPC_GET_COUNT( call ); ++ index ){
3846 mejdrech 340
                setting = measured_strings_find( configuration, strings[ index ].value, 0 );
3666 mejdrech 341
                if( ! setting ){
3846 mejdrech 342
                    setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 );
3666 mejdrech 343
                }
344
                if( setting ){
345
                    strings[ index ].length = setting->length;
346
                    strings[ index ].value = setting->value;
347
                }else{
348
                    strings[ index ].length = 0;
349
                    strings[ index ].value = NULL;
350
                }
351
            }
352
            // strings should not contain received data anymore
353
            free( data );
3901 mejdrech 354
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
3666 mejdrech 355
            free( strings );
356
            return ERROR_CODE;
3846 mejdrech 357
        case NET_NET_GET_CONF:
3666 mejdrech 358
            // arg1 = count
3901 mejdrech 359
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
360
            for( index = 0; index < IPC_GET_COUNT( call ); ++ index ){
3846 mejdrech 361
                setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 );
3666 mejdrech 362
                if( setting ){
363
                    strings[ index ].length = setting->length;
364
                    strings[ index ].value = setting->value;
365
                }else{
366
                    strings[ index ].length = 0;
367
                    strings[ index ].value = NULL;
368
                }
369
            }
370
            // strings should not contain received data anymore
371
            free( data );
3901 mejdrech 372
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
3666 mejdrech 373
            free( strings );
374
            return ERROR_CODE;
3846 mejdrech 375
        case NET_NET_STARTUP:
3666 mejdrech 376
            return startup();
377
    }
378
    return ENOTSUP;
379
}
380
 
381
/*
382
int parse_line( measured_strings_ref configuration, char * line ){
383
    ERROR_DECLARE;
384
 
385
    measured_string_ref setting;
386
    char *          name;
387
    char *          value;
388
 
389
    // from the beginning
390
    name = line;
391
    // skip spaces
392
    while( isspace( * name )) ++ name;
393
    // remember the name start
394
    value = name;
395
    // skip the name
396
    while( isalnum( * value ) || ( * value == '_' )){
397
        // make uppercase
398
//      * value = toupper( * value );
399
        ++ value;
400
    }
401
    if( * value == '=' ){
402
        // terminate the name
403
        * value = '\0';
404
    }else{
405
        // terminate the name
406
        * value = '\0';
407
        // skip until '='
408
        ++ value;
409
        while(( * value ) && ( * value != '=' )) ++ value;
410
        // not found?
411
        if( * value != '=' ) return EINVAL;
412
    }
413
    ++ value;
414
    // skip spaces
415
    while( isspace( * value )) ++ value;
416
    // create a bulk measured string till the end
417
    setting = measured_string_create_bulk( value, -1 );
418
    if( ! setting ) return ENOMEM;
419
    // add the configuration setting
3912 mejdrech 420
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 421
        free( setting );
422
        return ERROR_CODE;
423
    }
424
    return EOK;
425
}
426
*/
427
 
428
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
429
    ERROR_DECLARE;
430
 
431
    measured_string_ref setting;
432
 
433
    setting = measured_string_create_bulk( value, 0 );
434
    if( ! setting ) return ENOMEM;
435
    // add the configuration setting
3912 mejdrech 436
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 437
        free( setting );
438
        return ERROR_CODE;
439
    }
440
    return EOK;
441
}
442
 
3846 mejdrech 443
device_id_t generate_new_device_id( void ){
4163 mejdrech 444
    return device_assign_devno();
3666 mejdrech 445
}
446
 
447
int read_configuration( void ){
448
    ERROR_DECLARE;
449
 
4163 mejdrech 450
    netif_ref           netif;
3666 mejdrech 451
    measured_string_ref setting;
4163 mejdrech 452
    services_t          internet_service;
453
    int                 index;
454
    int                 irq;
455
    int                 io;
456
    int                 mtu;
3666 mejdrech 457
 
458
    // read general configuration
459
    ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "IPV", "4" ));
4163 mejdrech 460
    ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "MTU", "1500" ));
3666 mejdrech 461
 
462
    // read network interfaces configuration
463
 
464
    netif = ( netif_ref ) malloc( sizeof( netif_t ));
465
    if( ! netif ) return ENOMEM;
466
    netif->id = generate_new_device_id();
467
    ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
4163 mejdrech 468
    if( ERROR_OCCURRED( add_configuration( & netif->configuration, "NAME", "eth0" ))
469
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "NETIF", DP8390_NAME ))
470
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "NIL", ETHERNET_NAME ))
471
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IL", IP_NAME ))
472
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IRQ", "9" ))
473
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IO", "300" ))
474
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IP_CONFIG", "STATIC" ))
475
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IP_ADDR", "10.0.2.5" ))
476
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "NETMASK", "255.255.255.0" ))
477
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "BROADCAST", "10.0.2.255" ))
478
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "GATEWAY", "10.0.2.2" ))
479
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "DNS1", "10.0.2.2" ))
480
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "DNS2", "10.0.2.2" ))
481
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "ARP", "ARP" ))){
482
        measured_strings_destroy( & netif->configuration );
483
        free( netif );
484
        return ERROR_CODE;
485
    }
486
/*  if( ERROR_OCCURRED( add_configuration( & netif->configuration, "NAME", LO_NAME ))
3912 mejdrech 487
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "NETIF", LO_NAME ))
488
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IL", IP_NAME ))
489
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IP_CONFIG", "STATIC" ))
490
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IP_ADDR", "127.0.0.1" ))
491
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "NETMASK", "255.0.0.0" ))){
3666 mejdrech 492
        measured_strings_destroy( & netif->configuration );
493
        free( netif );
494
        return ERROR_CODE;
495
    }
4163 mejdrech 496
*/  // mandatory name
497
    setting = measured_strings_find( & netif->configuration, CONF_NAME, 0 );
3666 mejdrech 498
    if( ! setting ){
4163 mejdrech 499
        printf( "\nThe name is missing" );
3666 mejdrech 500
        measured_strings_destroy( & netif->configuration );
501
        free( netif );
502
        return EINVAL;
503
    }
504
    netif->name = setting->value;
505
    // mandatory netif
4163 mejdrech 506
    setting = measured_strings_find( & netif->configuration, CONF_NETIF, 0 );
3666 mejdrech 507
    if( ! setting ){
4163 mejdrech 508
        printf( "\nThe network interface driver is missing" );
3666 mejdrech 509
        measured_strings_destroy( & netif->configuration );
510
        free( netif );
511
        return EINVAL;
512
    }
3846 mejdrech 513
    netif->driver = modules_find( & networking_globals.modules, setting->value, 0 );
514
    if( ! netif->driver ){
4163 mejdrech 515
        printf( "\nThe network interface driver is unknown" );
3666 mejdrech 516
        // TODO register the unknown one
517
        measured_strings_destroy( & netif->configuration );
518
        free( netif );
519
        return EINVAL;
520
    }
3846 mejdrech 521
    if( ! netif->driver->task_id ){
522
        netif->driver->task_id = spawn( netif->driver->filename );
523
        if( ! netif->driver->task_id ){
4163 mejdrech 524
            printf( "\nFailed to start the network interface driver" );
3666 mejdrech 525
            measured_strings_destroy( & netif->configuration );
526
            free( netif );
527
            return EINVAL;
528
        }
529
    }
4163 mejdrech 530
    // optional network interface layer
531
    setting = measured_strings_find( & netif->configuration, CONF_NIL, 0 );
3666 mejdrech 532
    if( setting ){
3846 mejdrech 533
        netif->nil = modules_find( & networking_globals.modules, setting->value, 0 );
534
        if( ! netif->nil ){
4163 mejdrech 535
            printf( "\nThe network interface layer is unknown" );
3666 mejdrech 536
            // TODO register the unknown one
3846 mejdrech 537
            -- netif->driver->usage;
3666 mejdrech 538
            measured_strings_destroy( & netif->configuration );
539
            free( netif );
540
            return EINVAL;
541
        }
3846 mejdrech 542
        if( ! netif->nil->task_id ){
543
            netif->nil->task_id = spawn( netif->nil->filename );
544
            if( ! netif->nil->task_id ){
4163 mejdrech 545
                printf( "\nFailed to start the network interface layer" );
3846 mejdrech 546
                -- netif->driver->usage;
3666 mejdrech 547
                measured_strings_destroy( & netif->configuration );
548
                free( netif );
549
                return EINVAL;
550
            }
551
        }
552
    }else{
3846 mejdrech 553
        netif->nil = NULL;
3666 mejdrech 554
    }
555
    // mandatory internet layer
4163 mejdrech 556
    setting = measured_strings_find( & netif->configuration, CONF_IL, 0 );
3666 mejdrech 557
    if( ! setting ){
4163 mejdrech 558
        printf( "\nThe internet layer is missing" );
3846 mejdrech 559
        -- netif->driver->usage;
560
        -- netif->nil->usage;
3666 mejdrech 561
        measured_strings_destroy( & netif->configuration );
562
        free( netif );
563
        return EINVAL;
564
    }
3846 mejdrech 565
    netif->il = modules_find( & networking_globals.modules, setting->value, 0 );
566
    if( ! netif->il ){
4163 mejdrech 567
        printf( "\nThe internet layer is unknown" );
3666 mejdrech 568
        // TODO register the unknown one
3846 mejdrech 569
        -- netif->driver->usage;
570
        -- netif->nil->usage;
3666 mejdrech 571
        measured_strings_destroy( & netif->configuration );
572
        free( netif );
573
        return EINVAL;
574
    }
3846 mejdrech 575
    if( ! netif->il->task_id ){
576
        netif->il->task_id = spawn( netif->il->filename );
577
        if( ! netif->il->task_id ){
4163 mejdrech 578
            printf( "\nFailed to start the internet layer" );
3846 mejdrech 579
            -- netif->driver->usage;
580
            -- netif->nil->usage;
3666 mejdrech 581
            measured_strings_destroy( & netif->configuration );
582
            free( netif );
583
            return EINVAL;
584
        }
585
    }
3685 mejdrech 586
    index = netifs_add( & networking_globals.netifs, netif->id, netif );
587
    if( index < 0 ){
3666 mejdrech 588
        free( netif );
4163 mejdrech 589
        return index;
3666 mejdrech 590
    }
3912 mejdrech 591
    if( ERROR_OCCURRED( char_map_add( & networking_globals.netif_names, netif->name, 0, index ))){
3685 mejdrech 592
        netifs_exclude_index( & networking_globals.netifs, index );
3666 mejdrech 593
        return ERROR_CODE;
594
    }
3685 mejdrech 595
    // end of the static loopback initialization
596
    // startup the loopback interface
3846 mejdrech 597
    if( ! netif->driver->phone ){
598
        netif->driver->phone = connect_to_service( netif->driver->service );
3685 mejdrech 599
    }
4163 mejdrech 600
    setting = measured_strings_find( & netif->configuration, CONF_IRQ, 0 );
601
    irq = setting ? strtol( setting->value, NULL, 10 ) : 0;
602
    setting = measured_strings_find( & netif->configuration, CONF_IO, 0 );
603
    io = setting ? strtol( setting->value, NULL, 16 ) : 0;
604
    ERROR_PROPAGATE( async_req_3_0( netif->driver->phone, NET_NETIF_PROBE, netif->id, irq, io ));
3846 mejdrech 605
    ++ netif->driver->usage;
606
    if( netif->nil ){
607
        if( ! netif->nil->phone ){
608
            netif->nil->phone = connect_to_service( netif->nil->service );
3685 mejdrech 609
        }
4163 mejdrech 610
        setting = measured_strings_find( & netif->configuration, CONF_MTU, 0 );
611
        if( ! setting ){
612
            setting = measured_strings_find( & networking_globals.configuration, CONF_MTU, 0 );
613
        }
614
        mtu = setting ? strtol( setting->value, NULL, 10 ) : 0;
615
        ERROR_PROPAGATE( async_req_3_0( netif->nil->phone, NET_NIL_DEVICE, netif->id, netif->driver->service, mtu ));
3846 mejdrech 616
        ++ netif->nil->usage;
617
        internet_service = netif->nil->service;
3685 mejdrech 618
    }else{
3846 mejdrech 619
        internet_service = netif->driver->service;
3685 mejdrech 620
    }
3846 mejdrech 621
    if( ! netif->il->phone ){
622
        netif->il->phone = connect_to_service( netif->il->service );
3685 mejdrech 623
    }
3846 mejdrech 624
    // TODO IL_BUNDLE
625
    ERROR_PROPAGATE( async_req_2_0( netif->il->phone, NET_IL_DEVICE, netif->id, internet_service ));
626
    ++ netif->il->usage;
627
    // TODO startup?
628
    ERROR_PROPAGATE( async_req_1_0( netif->driver->phone, NET_NETIF_START, netif->id ));
4163 mejdrech 629
    printf( "\nNew network interface started:\n\tname\t= %s\n\tid\t= %d\n\tdriver\t= %s\n\tnil\t= %s\n\til\t= %s", netif->name, netif->id, netif->driver->name, netif->nil ? netif->nil->name : NULL, netif->il->name );
3666 mejdrech 630
    return EOK;
631
}
632
 
4163 mejdrech 633
task_id_t spawn( char * fname ){
634
    char * argv[ 2 ];
635
//  char * argv[ 4 ];
3666 mejdrech 636
    task_id_t   res;
637
 
638
//  printf( "Spawning %s\n", fname );
4163 mejdrech 639
    argv[ 0 ] = fname;
640
    argv[ 1 ] = NULL;
3666 mejdrech 641
    res = task_spawn( fname, argv );
4163 mejdrech 642
/*  argv[ 0 ] = "/app/trace";
643
    argv[ 1 ] = "+ti";
644
    argv[ 2 ] = fname;
645
    argv[ 3 ] = NULL;
646
    res = task_spawn( "/app/trace", argv );
647
*/
3666 mejdrech 648
    if( res != 0 ){
649
        /* Success */
650
        usleep( 50000 );
651
    }
652
    return res;
653
}
654
 
655
int startup( void ){
656
    ERROR_DECLARE;
657
 
658
    // read configuration files
3912 mejdrech 659
    if( ERROR_OCCURRED( read_configuration())) return ERROR_CODE;
3666 mejdrech 660
 
661
    // start network interfaces and needed modules
662
//  start_device( "/sbin/lo", "/sbin/dummy_link_layer" );
663
    return EOK;
664
}
665
 
3466 mejdrech 666
/** @}
667
 */