297
#if defined(HAVE_IPV6)
298
static int create_ipv6_listener(struct listener **link, int port)
358
static int make_sock(union mysockaddr *addr, int type)
300
union mysockaddr addr;
305
memset(&addr, 0, sizeof(addr));
306
addr.in6.sin6_family = AF_INET6;
307
addr.in6.sin6_addr = in6addr_any;
308
addr.in6.sin6_port = htons(port);
309
#ifdef HAVE_SOCKADDR_SA_LEN
310
addr.in6.sin6_len = sizeof(addr.in6);
313
/* No error of the kernel doesn't support IPv6 */
314
if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
315
return (errno == EPROTONOSUPPORT ||
316
errno == EAFNOSUPPORT ||
319
if ((tcpfd = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
322
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
323
setsockopt(tcpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
324
setsockopt(fd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1 ||
325
setsockopt(tcpfd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1 ||
328
bind(tcpfd, (struct sockaddr *)&addr, sa_len(&addr)) == -1 ||
329
listen(tcpfd, 5) == -1 ||
330
bind(fd, (struct sockaddr *)&addr, sa_len(&addr)) == -1)
333
/* The API changed around Linux 2.6.14 but the old ABI is still supported:
334
handle all combinations of headers and kernel.
335
OpenWrt note that this fixes the problem addressed by your very broken patch. */
337
daemon->v6pktinfo = IPV6_PKTINFO;
339
#ifdef IPV6_RECVPKTINFO
340
# ifdef IPV6_2292PKTINFO
341
if (setsockopt(fd, IPV6_LEVEL, IPV6_RECVPKTINFO, &opt, sizeof(opt)) == -1)
343
if (errno == ENOPROTOOPT && setsockopt(fd, IPV6_LEVEL, IPV6_2292PKTINFO, &opt, sizeof(opt)) != -1)
344
daemon->v6pktinfo = IPV6_2292PKTINFO;
360
int family = addr->sa.sa_family;
363
static int dad_count = 0;
366
if ((fd = socket(family, type, 0)) == -1)
370
/* No error if the kernel just doesn't support this IP flavour */
371
if (errno == EPROTONOSUPPORT ||
372
errno == EAFNOSUPPORT ||
377
port = prettyprint_addr(addr, daemon->namebuff);
378
if (!option_bool(OPT_NOWILD))
379
sprintf(daemon->namebuff, "port %d", port);
380
die(_("failed to create listening socket for %s: %s"),
381
daemon->namebuff, EC_BADNET);
384
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 || !fix_fd(fd))
388
if (family == AF_INET6 && setsockopt(fd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1)
394
if ((rc = bind(fd, (struct sockaddr *)addr, sa_len(addr))) != -1)
398
/* An interface may have an IPv6 address which is still undergoing DAD.
399
If so, the bind will fail until the DAD completes, so we try over 20 seconds
401
if (family == AF_INET6 &&
402
(errno == ENODEV || errno == EADDRNOTAVAIL) &&
403
dad_count++ < DAD_WAIT)
415
if (type == SOCK_STREAM)
417
if (listen(fd, 5) == -1)
420
else if (!option_bool(OPT_NOWILD))
422
if (family == AF_INET)
424
#if defined(HAVE_LINUX_NETWORK)
425
if (setsockopt(fd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1)
427
#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
428
if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 ||
429
setsockopt(fd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1)
436
/* The API changed around Linux 2.6.14 but the old ABI is still supported:
437
handle all combinations of headers and kernel.
438
OpenWrt note that this fixes the problem addressed by your very broken patch. */
439
daemon->v6pktinfo = IPV6_PKTINFO;
441
# ifdef IPV6_RECVPKTINFO
442
# ifdef IPV6_2292PKTINFO
443
if (setsockopt(fd, IPV6_LEVEL, IPV6_RECVPKTINFO, &opt, sizeof(opt)) == -1)
445
if (errno == ENOPROTOOPT && setsockopt(fd, IPV6_LEVEL, IPV6_2292PKTINFO, &opt, sizeof(opt)) != -1)
446
daemon->v6pktinfo = IPV6_2292PKTINFO;
451
if (setsockopt(fd, IPV6_LEVEL, IPV6_RECVPKTINFO, &opt, sizeof(opt)) == -1)
349
if (setsockopt(fd, IPV6_LEVEL, IPV6_RECVPKTINFO, &opt, sizeof(opt)) == -1)
353
if (setsockopt(fd, IPV6_LEVEL, IPV6_PKTINFO, &opt, sizeof(opt)) == -1)
357
l = safe_malloc(sizeof(struct listener));
361
l->family = AF_INET6;
455
if (setsockopt(fd, IPV6_LEVEL, IPV6_PKTINFO, &opt, sizeof(opt)) == -1)
465
static struct listener *create_listeners(union mysockaddr *addr, int do_tftp)
467
struct listener *l = NULL;
468
int fd = -1, tcpfd = -1, tftpfd = -1;
470
if (daemon->port != 0)
472
fd = make_sock(addr, SOCK_DGRAM);
473
tcpfd = make_sock(addr, SOCK_STREAM);
479
if (addr->sa.sa_family == AF_INET)
481
/* port must be restored to DNS port for TCP code */
482
short save = addr->in.sin_port;
483
addr->in.sin_port = htons(TFTP_PORT);
484
tftpfd = make_sock(addr, SOCK_DGRAM);
485
addr->in.sin_port = save;
490
short save = addr->in6.sin6_port;
491
addr->in6.sin6_port = htons(TFTP_PORT);
492
tftpfd = make_sock(addr, SOCK_DGRAM);
493
addr->in6.sin6_port = save;
499
if (fd != -1 || tcpfd != -1 || tftpfd != -1)
501
l = safe_malloc(sizeof(struct listener));
503
l->family = addr->sa.sa_family;
369
512
struct listener *create_wildcard_listeners(void)
371
514
union mysockaddr addr;
373
struct listener *l, *l6 = NULL;
374
int tcpfd = -1, fd = -1, tftpfd = -1;
516
int tftp_enabled = daemon->tftp_unlimited || daemon->tftp_interfaces;
376
518
memset(&addr, 0, sizeof(addr));
519
#ifdef HAVE_SOCKADDR_SA_LEN
520
addr.in.sin_len = sizeof(addr.in);
377
522
addr.in.sin_family = AF_INET;
378
523
addr.in.sin_addr.s_addr = INADDR_ANY;
379
524
addr.in.sin_port = htons(daemon->port);
380
#ifdef HAVE_SOCKADDR_SA_LEN
381
addr.in.sin_len = sizeof(struct sockaddr_in);
384
if (daemon->port != 0)
387
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 ||
388
(tcpfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
391
if (setsockopt(tcpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
392
bind(tcpfd, (struct sockaddr *)&addr, sa_len(&addr)) == -1 ||
393
listen(tcpfd, 5) == -1 ||
526
l = create_listeners(&addr, tftp_enabled);
396
!create_ipv6_listener(&l6, daemon->port) ||
398
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
400
#if defined(HAVE_LINUX_NETWORK)
401
setsockopt(fd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1 ||
402
#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
403
setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 ||
404
setsockopt(fd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1 ||
406
bind(fd, (struct sockaddr *)&addr, sa_len(&addr)) == -1)
411
if (daemon->tftp_unlimited || daemon->tftp_interfaces)
413
addr.in.sin_port = htons(TFTP_PORT);
414
if ((tftpfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
417
if (!fix_fd(tftpfd) ||
418
#if defined(HAVE_LINUX_NETWORK)
419
setsockopt(tftpfd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1 ||
420
#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
421
setsockopt(tftpfd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 ||
422
setsockopt(tftpfd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1 ||
424
bind(tftpfd, (struct sockaddr *)&addr, sa_len(&addr)) == -1)
429
l = safe_malloc(sizeof(struct listener));
529
memset(&addr, 0, sizeof(addr));
530
# ifdef HAVE_SOCKADDR_SA_LEN
531
addr.in6.sin6_len = sizeof(addr.in6);
533
addr.in6.sin6_family = AF_INET6;
534
addr.in6.sin6_addr = in6addr_any;
535
addr.in6.sin6_port = htons(daemon->port);
538
l->next = create_listeners(&addr, tftp_enabled);
540
l = create_listeners(&addr, tftp_enabled);
439
546
struct listener *create_bound_listeners(void)
441
struct listener *listeners = NULL;
548
struct listener *new, *listeners = NULL;
442
549
struct irec *iface;
445
static int dad_count = 0;
448
551
for (iface = daemon->interfaces; iface; iface = iface->next)
450
struct listener *new = safe_malloc(sizeof(struct listener));
451
new->family = iface->addr.sa.sa_family;
453
new->next = listeners;
459
if (daemon->port != 0)
461
if ((new->tcpfd = socket(iface->addr.sa.sa_family, SOCK_STREAM, 0)) == -1 ||
462
(new->fd = socket(iface->addr.sa.sa_family, SOCK_DGRAM, 0)) == -1 ||
463
setsockopt(new->fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
464
setsockopt(new->tcpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
465
!fix_fd(new->tcpfd) ||
467
die(_("failed to create listening socket: %s"), NULL, EC_BADNET);
470
if (iface->addr.sa.sa_family == AF_INET6)
472
if (setsockopt(new->fd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1 ||
473
setsockopt(new->tcpfd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1)
474
die(_("failed to set IPV6 options on listening socket: %s"), NULL, EC_BADNET);
480
if ((rc = bind(new->fd, &iface->addr.sa, sa_len(&iface->addr))) != -1)
484
/* An interface may have an IPv6 address which is still undergoing DAD.
485
If so, the bind will fail until the DAD completes, so we try over 20 seconds
487
if (iface->addr.sa.sa_family == AF_INET6 && (errno == ENODEV || errno == EADDRNOTAVAIL) &&
488
dad_count++ < DAD_WAIT)
497
if (rc == -1 || bind(new->tcpfd, &iface->addr.sa, sa_len(&iface->addr)) == -1)
499
prettyprint_addr(&iface->addr, daemon->namebuff);
500
die(_("failed to bind listening socket for %s: %s"),
501
daemon->namebuff, EC_BADNET);
504
if (listen(new->tcpfd, 5) == -1)
505
die(_("failed to listen on socket: %s"), NULL, EC_BADNET);
509
if (iface->addr.sa.sa_family == AF_INET && iface->tftp_ok)
511
short save = iface->addr.in.sin_port;
512
iface->addr.in.sin_port = htons(TFTP_PORT);
513
if ((new->tftpfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 ||
514
setsockopt(new->tftpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
515
!fix_fd(new->tftpfd) ||
516
bind(new->tftpfd, &iface->addr.sa, sa_len(&iface->addr)) == -1)
517
die(_("failed to create TFTP socket: %s"), NULL, EC_BADNET);
518
iface->addr.in.sin_port = save;
552
if ((new = create_listeners(&iface->addr, iface->tftp_ok)))
555
new->next = listeners;
524
559
return listeners;