| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 83 |
|
| 84 |
static void multistack_daemon(void *argv); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
extern int _nofdfake; |
| 89 |
|
| 90 |
|
| 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 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
void lwip_fini(void) |
| 238 |
{ |
| 239 |
} |
| 240 |
|
| 241 |
static char *nullstring=""; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 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 |
|
| 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 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 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 |
|
| 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 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 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 |
|
| 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 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
int lwip_ifup_flags(struct netif *netif, int flags) |
| 453 |
{ |
| 454 |
netif_set_up(netif, flags); |
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
int lwip_ifup(struct netif *netif) |
| 460 |
{ |
| 461 |
lwip_ifup_flags(netif, 0); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
int lwip_ifdown(struct netif *netif) |
| 479 |
{ |
| 480 |
netif_set_down(netif); |
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 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 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 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 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 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 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 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 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 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 |
|