Debian Codegraph @

lwip-contrib/ports/unix/proj/lib/unixlib.c

raw symbols SCIP
1 /**
2 * @file
3 * LwIPv6 TCP/IP Stack library
4 *
5 * The initialisation functions for a shared library
6 *
7 * You may need to configure this file to your own needs - it is only an example
8 * of how lwIP can be used as a self initialising shared library.
9 *
10 *
11 */
12
13 /*
14 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without modification,
18 * are permitted provided that the following conditions are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright notice,
21 * this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright notice,
23 * this list of conditions and the following disclaimer in the documentation
24 * and/or other materials provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
31 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
33 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
37 * OF SUCH DAMAGE.
38 *
39 * Author: Kieran Mansley <[email protected]>
40 *
41 * $Id: unixlib.c 1040 2011-12-13 08:34:27Z rd235 $
42 */
43
44 /*-----------------------------------------------------------------------------------*/
45 /* unixlib.c
46 *
47 * The initialisation functions for a shared library
48 *
49 * You may need to configure this file to your own needs - it is only an example
50 * of how lwIP can be used as a self initialising shared library.
51 *
52 * In particular, you should change the gateway, ipaddr, and netmask to be the values
53 * you would like the stack to use.
54 */
55 /*-----------------------------------------------------------------------------------*/
56 #include <stdio.h>
57 #include <unistd.h>
58 #include <time.h>
59
60 #include "lwip/sys.h"
61 #include "lwip/mem.h"
62 #include "lwip/memp.h"
63 #include "lwip/pbuf.h"
64 #include "lwip/tcp.h"
65 #include "lwip/tcpip.h"
66 #include "lwip/netif.h"
67 #include "lwip/stats.h"
68 #include "lwip/stack.h"
69
70 #include "netif/vdeif.h"
71 #include "netif/tunif.h"
72 #include "netif/tapif.h"
73 #include "netif/loopif.h"
74 #include "netif/slirpif.h"
75
76 #include "lwip/sockets.h"
77
78 #if IPv6_RADVCONF
79 #include "lwip/radvconf.h"
80 #endif
81
82 /* #define MULTISTACKDEBUG*/
83
84 static void multistack_daemon(void *argv);
85
86 /*--------------------------------------------------------------------------*/
87
88 extern int _nofdfake;
89
90 /*static struct stack *mainstack;*/
91
92 static void lwip_add_loopif(struct stack *stack);
93
94 static void
95 init_done(void *arg)
96 {
97 sys_sem_t *sem = (sys_sem_t *) arg;
98 sys_sem_signal(*sem);
99 }
100
101 static void
102 shutdown_done(void *arg)
103 {
104 sys_sem_t *sem = (sys_sem_t *) arg;
105 sys_sem_signal(*sem);
106 }
107
108 /**
109 * Initializes the LwIPv6 Stack.
110 *
111 * Initializes all memory structures used by LwIPv6 and
112 * launchs the main TCPIP stack thread.
113 *
114 * The loopback interface (lo) is created before return.
115 *
116 * @note The dynamic linker call this function when the dynamic library is loaded.
117 */
118 void lwip_init(void)
119 {
120 if (getenv("_INSIDE_VIEWOS_MODULE") != NULL) {
121 _nofdfake = 1;
122 }
123
124 srand(getpid()+time(NULL));
125
126 /* Init stack's structures */
127 stats_init();
128 sys_init();
129 mem_init();
130 memp_init();
131 pbuf_init();
132
133 tcpip_init();
134 }
135
136 #if LWIP_CAPABILITIES
137 struct stack *lwip_add_stack_cap(unsigned long flags, lwip_capfun capfun)
138 #else
139 struct stack *lwip_add_stack(unsigned long flags)
140 #endif
141 {
142 sys_sem_t sem;
143 struct stack *newstack;
144
145 /* Start the main stack */
146 sem = sys_sem_new(0);
147 #if LWIP_CAPABILITIES
148 newstack = tcpip_start(init_done, &sem, flags, capfun);
149 #else
150 newstack = tcpip_start(init_done, &sem, flags);
151 #endif
152
153 sys_sem_wait(sem);
154 sys_sem_free(sem);
155
156 /* Add loop interface at least */
157 lwip_add_loopif(newstack);
158
159 #ifdef MULTISTACKDEBUG
160 printf("%s: new %p\n", __func__, newstack);
161 #endif
162
163 return newstack;
164 }
165
166 #if LWIP_CAPABILITIES
167 struct stack *lwip_add_stack(unsigned long flags)
168 {
169 return lwip_add_stack_cap(flags, NULL);
170 }
171 #else
172 struct stack *lwip_add_stack_cap(unsigned long flags, void *must_be_null)
173 {
174 if (must_be_null != NULL)
175 return NULL;
176 else
177 return lwip_add_stack_cap(flags, NULL);
178 }
179 #endif
180
181 struct stack *lwip_stack_new(void)
182 {
183 return lwip_add_stack(0);
184 }
185
186 struct stack *lwip_stack_get(void)
187 {
188 return tcpip_stack_get();
189 }
190
191 void lwip_stack_set(struct stack *stackid)
192 {
193 #ifdef MULTISTACKDEBUG
194 printf("%s: %p\n", __func__, stackid);
195 #endif
196 tcpip_stack_set(stackid);
197 }
198
199 void lwip_del_stack(struct stack * stackid)
200 {
201 sys_sem_t sem;
202 #ifdef MULTISTACKDEBUG
203 printf("%s: %p...\n", __func__, stackid);
204 #endif
205 sem = sys_sem_new(0);
206 tcpip_shutdown(stackid, shutdown_done, &sem);
207 sys_sem_wait(sem);
208 sys_sem_free(sem);
209 #ifdef MULTISTACKDEBUG
210 printf("%s: %p done!\n", __func__, stackid);
211 #endif
212 }
213
214 void lwip_stack_free(struct stack * stackid)
215 {
216 lwip_del_stack(stackid);
217 }
218
219 unsigned long lwip_stack_flags_get(struct stack *stackid)
220 {
221 return stackid->stack_flags;
222 }
223
224 void lwip_stack_flags_set(struct stack *stackid, unsigned long flags)
225 {
226 stackid->stack_flags=flags;
227 }
228
229 /**
230 * Shutdown the LwIPv6 Stack.
231 *
232 * Signals to the main TCPIP thread to exit and waits
233 * until termination of the thread.
234 *
235 * @note The dynamic linker call this function when the dynamic library is unloaded.
236 */
237 void lwip_fini(void)
238 {
239 }
240
241 static char *nullstring="";
242 /*--------------------------------------------------------------------------*/
243
244 /**
245 * Creates and adds a new VDE network interface to the stack.
246 *
247 * @param arg The ID of the VDE Switch connected with the interface.
248 * The ID is the filename of the unix socket of the switch.
249 *
250 * Allocs a new virtual interface and attachs it to the VDE Switch with
251 * ID = arg.
252 *
253 * @return Returns the pointer to the new interface, NULL on failure.
254 *
255 * @note If IPv6 Stateless Autoconfiguration Protocol is not enabled,
256 * a Link-scope IPv6 address will be assigned to the new interface.
257 */
258 struct netif *lwip_add_vdeif(struct stack *stack, void *arg, int flags)
259 {
260 struct ip_addr ipaddr, netmask;
261 struct netif *pnetif;
262 pnetif = mem_malloc(sizeof (struct netif));
263
264 pnetif->flags = flags & NETIF_ADD_FLAGS;
265
266 if (arg == NULL) arg = nullstring;
267 if (tcpip_netif_add(stack, pnetif, arg, vdeif_init, tcpip_input, tcpip_notify) == NULL) {
268 mem_free(pnetif);
269 return NULL;
270 }
271
272 if
273 #if IPv6_AUTO_CONFIGURATION
274 (!(flags & NETIF_FLAG_AUTOCONF))
275 #else
276 (1)
277 #endif
278 {
279 /* Link-scope address */
280 IP6_ADDR_LINKSCOPE(&ipaddr, pnetif->hwaddr);
281 IP6_ADDR(&netmask, 0xffff,0xffff,0xffff,0xffff,0x0,0x0,0x0,0x0);
282 netif_add_addr(pnetif, &ipaddr, &netmask);
283 }
284
285 return(pnetif);
286 }
287
288 struct netif *lwip_vdeif_add(struct stack *stack, void *arg)
289 {
290 return lwip_add_vdeif(stack, arg, NETIF_STD_FLAGS);
291 }
292
293 /**
294 * Creates and adds a new TAP network interface to the stack.
295 *
296 * @param arg, is the if name.
297 *
298 * Allocs a new virtual interface and creates a TAP interface on your host.
299 *
300 * @return Returns the pointer to the new interface, NULL on failure.
301 *
302 * @note If IPv6 Stateless Autoconfiguration Protocol is not enabled,
303 * a Link-scope IPv6 address will be assigned to the new interface.
304 *
305 * @note You need the TUNTAP driver active with your kernel.
306 *
307 * @note You need to configure the host side of the TAP link.
308 */
309 struct netif *lwip_add_tapif(struct stack *stack, void *arg, int flags)
310 {
311 struct ip_addr ipaddr, netmask;
312 struct netif *pnetif;
313 pnetif = mem_malloc(sizeof (struct netif));
314
315 pnetif->flags = flags & NETIF_ADD_FLAGS;
316 pnetif->flags |= NETIF_FLAG_POINTTOPOINT;
317
318 if (arg == NULL) arg = nullstring;
319 if (tcpip_netif_add(stack, pnetif, arg, tapif_init, tcpip_input, tcpip_notify) == NULL) {
320 mem_free(pnetif);
321 return NULL;
322 }
323
324 if
325 #if IPv6_AUTO_CONFIGURATION
326 (!(flags & NETIF_FLAG_AUTOCONF))
327 #else
328 (1)
329 #endif
330 {
331 /* Link-scope address */
332 IP6_ADDR_LINKSCOPE(&ipaddr, pnetif->hwaddr);
333 IP6_ADDR(&netmask, 0xffff,0xffff,0xffff,0xffff,0x0,0x0,0x0,0x0);
334 netif_add_addr(pnetif, &ipaddr, &netmask);
335 }
336
337 return(pnetif);
338 }
339
340 struct netif *lwip_tapif_add(struct stack *stack, void *arg)
341 {
342 return lwip_add_tapif(stack, arg, NETIF_STD_FLAGS);
343 }
344
345 /**
346 * Creates and adds a new TUN network interface to the stack.
347 *
348 * @param arg, is the if name.
349 *
350 * Allocs a new virtual interface and creates a TAP interface on your host.
351 *
352 * @return Returns the pointer to the new interface, NULL on failure.
353 *
354 * @note If IPv6 Stateless Autoconfiguration Protocol is not enabled,
355 * a Link-scope IPv6 address will be assigned to the new interface.
356 *
357 * @note You need to configure the host side of the TUN link.
358 */
359 struct netif *lwip_add_tunif(struct stack *stack, void *arg, int flags)
360 {
361 struct ip_addr ipaddr, netmask;
362 struct netif *pnetif;
363 pnetif = mem_malloc(sizeof (struct netif));
364
365 pnetif->flags = flags & NETIF_ADD_FLAGS;
366 pnetif->flags |= NETIF_FLAG_POINTTOPOINT;
367
368 if (arg == NULL) arg = nullstring;
369 if (tcpip_netif_add(stack, pnetif, arg, tunif_init, tcpip_input, tcpip_notify) == NULL) {
370 mem_free(pnetif);
371 return NULL;
372 }
373
374 if
375 #if IPv6_AUTO_CONFIGURATION
376 (!(flags & NETIF_FLAG_AUTOCONF))
377 #else
378 (1)
379 #endif
380 {
381 /* Link-scope address */
382 IP6_ADDR_LINKSCOPE(&ipaddr, pnetif->hwaddr);
383 IP6_ADDR(&netmask, 0xffff,0xffff,0xffff,0xffff,0x0,0x0,0x0,0x0);
384 netif_add_addr(pnetif, &ipaddr, &netmask);
385 }
386
387 return(pnetif);
388 }
389
390 struct netif *lwip_tunif_add(struct stack *stack, void *arg)
391 {
392 return lwip_add_tunif(stack, arg, NETIF_STD_FLAGS);
393 }
394
395 #ifdef LWSLIRP
396 struct netif *lwip_add_slirpif(struct stack *stack, void *arg, int flags)
397 {
398 struct netif *pnetif;
399 pnetif = mem_malloc(sizeof (struct netif));
400
401 pnetif->flags = flags & NETIF_ADD_FLAGS;
402 if (arg == NULL) arg = nullstring;
403 if (tcpip_netif_add(stack, pnetif, arg, slirpif_init, tcpip_input, tcpip_notify) == NULL) {
404 mem_free(pnetif);
405 return NULL;
406 }
407
408 return(pnetif);
409 }
410
411 struct netif *lwip_slirpif_add(struct stack *stack, void *arg)
412 {
413 return lwip_add_slirpif(stack, arg, 0);
414 }
415 #endif
416
417 static void lwip_add_loopif(struct stack *stack)
418 {
419 struct netif *loopif;
420 struct ip_addr ipaddr, netmask;
421
422 loopif = mem_malloc(sizeof (struct netif));
423 bzero(loopif, sizeof(struct netif) );
424
425 tcpip_netif_add(stack, loopif,NULL, loopif_init, tcpip_input, NULL);
426
427 IP6_ADDR(&ipaddr, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1);
428 IP6_ADDR(&netmask, 0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff);
429 netif_add_addr(loopif,&ipaddr, &netmask);
430
431 IP64_ADDR(&ipaddr, 127,0,0,1);
432 IP64_MASKADDR(&netmask, 255,0,0,0);
433 netif_add_addr(loopif,&ipaddr, &netmask);
434 }
435
436 /*--------------------------------------------------------------------------*/
437
438 /**
439 * Bring the input network interface up.
440 *
441 * @param netif The interface to bring up.
442 *
443 * Bring the input network interface up. It doesn't check if input is NULL.
444 *
445 * @return It returns always 0.
446 *
447 * @note If IPv6 Stateless Address Autoconfiguration is enabled, the
448 * autoconfiguration protocol will start.
449 *
450 * @bug It doesn't check if the interface is already up.
451 */
452 int lwip_ifup_flags(struct netif *netif, int flags)
453 {
454 netif_set_up(netif, flags);
455 return 0;
456 }
457
458 /* deprecated */
459 int lwip_ifup(struct netif *netif)
460 {
461 lwip_ifup_flags(netif, 0);
462 }
463
464 /**
465 * Bring the input network interface down.
466 *
467 * @param netif The interface to bring down.
468 *
469 * Bring the input network interface down. It doesn't check if input is NULL.
470 *
471 * @return It returns always 0.
472 *
473 * @note If IPv6 Stateless Address Autoconfiguration is enabled, the
474 * autoconfigurated address will be removed.
475 *
476 * @bug It doesn't check if the interface is already up.
477 */
478 int lwip_ifdown(struct netif *netif)
479 {
480 netif_set_down(netif);
481 return 0;
482 }
483
484 /*--------------------------------------------------------------------------*/
485
486 /**
487 * Add a new address to a network interface.
488 *
489 * @param netif a pre-allocated netif structure
490 * @param ipaddr IP address for the new netif
491 * @param netmask network mask for the new netif
492 *
493 * @return 0 on success, < 0 on failure.
494 */
495 int lwip_add_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask)
496 {
497 return netif_add_addr(netif,ipaddr,netmask);
498 }
499
500 /**
501 * Delete a network address.
502 *
503 * @param netif a pre-allocated netif structure
504 * @param ipaddr IP address for the new netif
505 * @param netmask network mask for the new netif
506 *
507 * @return 0 on success, < 0 on failure.
508 */
509 int lwip_del_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask)
510 {
511 return netif_del_addr(netif,ipaddr,netmask);
512 }
513
514 /*--------------------------------------------------------------------------*/
515
516 /**
517 * Adds a new route entry to the LwIPv6 routing table.
518 *
519 * @param addr The destination prefix IP address.
520 * @param netmask The destination prefix netmask.
521 * @param nexthop The address of the nexthop router.
522 * @param neitf The output interface.
523 * @param flags
524 *
525 * Adds a new route to the LwIPv6 routing table.
526 *
527 * @return 0 on success, < 0 on failure.
528 *
529 */
530 int lwip_add_route(struct stack *stack,struct ip_addr *addr, struct ip_addr *netmask, struct ip_addr *nexthop, struct netif *netif, int flags)
531 {
532 if (netif == NULL)
533 netif = netif_find_direct_destination(stack, nexthop);
534 if (netif == NULL)
535 return -ENETUNREACH;
536 else
537 return ip_route_list_add(stack, addr,netmask,nexthop,netif,flags);
538 }
539
540 /**
541 * Deletes a route entry from the LwIPv6 routing table.
542 *
543 * @param addr The destination prefix IP address.
544 * @param netmask The destination prefix netmask.
545 * @param nexthop The address of the nexthop router.
546 * @param neitf The output interface.
547 * @param flags
548 *
549 * Adds a new route to the LwIPv6 routing table.
550 *
551 * @return 0 on success, < 0 on failure.
552 *
553 */
554 int lwip_del_route(struct stack *stack,struct ip_addr *addr, struct ip_addr *netmask, struct ip_addr *nexthop, struct netif *netif, int flags)
555 {
556 if (netif == NULL)
557 netif = netif_find_direct_destination(stack, nexthop);
558 if (netif == NULL)
559 return -ENETUNREACH;
560 else
561 return ip_route_list_del(stack, addr,netmask,nexthop,netif,flags);
562 }
563
564 /*--------------------------------------------------------------------------*/
565
566 /**
567 * Loads the Router Advertising configuration parameters from the input file.
568 *
569 * @param arg Name of the configuration file.
570 *
571 * Loads the Router Advertising configuration parameters from the input file.
572 * If the configuration file syntax or any interface parameters are wrong
573 * the advertising service will be disabled for one or all interfaces.
574 *
575 * @return -1 If IPv6 Router Advertising support is not enabled.
576 * @return 0 if unable to open the configuration file.
577 * @return 1 On success
578 *
579 * @bug It doesn't check if 'arg' is NULL.
580 */
581 int lwip_radv_load_configfile(struct stack *stack,void *arg)
582 {
583 #if IPv6_RADVCONF
584 return radv_load_configfile(stack, (char*)arg);
585 #else
586 return -1;
587 #endif
588 }
589
590 void lwip_radv_load_config(struct stack *stack,FILE *filein)
591 {
592 #if IPv6_RADVCONF
593 radv_load_config(stack, filein);
594 #endif
595 }
596
597 void lwip_thread_new(void (* thread)(void *arg), void *arg)
598 {
599 sys_thread_new(thread, arg, DEFAULT_THREAD_PRIO);
600 }
601
602