1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * BSD interface driver for dhcpcd 4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name> 5 * All rights reserved 6 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/ioctl.h> 30 #include <sys/param.h> 31 #include <sys/socket.h> 32 #include <sys/stat.h> 33 #include <sys/sysctl.h> 34 #include <sys/time.h> 35 #include <sys/types.h> 36 #include <sys/uio.h> 37 #include <sys/utsname.h> 38 39 #include "config.h" 40 41 #include <arpa/inet.h> 42 #include <net/bpf.h> 43 #include <net/if.h> 44 #include <net/if_dl.h> 45 #include <net/if_media.h> 46 #include <net/route.h> 47 #include <netinet/if_ether.h> 48 #include <netinet/in.h> 49 #include <netinet/in_var.h> 50 #include <netinet6/in6_var.h> 51 #include <netinet6/nd6.h> 52 #ifdef __NetBSD__ 53 #include <net/if_vlanvar.h> /* Needs netinet/if_ether.h */ 54 #elif defined(__DragonFly__) 55 #include <net/vlan/if_vlan_var.h> 56 #else 57 #include <net/if_vlan_var.h> 58 #endif 59 #ifdef __DragonFly__ 60 # include <netproto/802_11/ieee80211_ioctl.h> 61 #else 62 # include <net80211/ieee80211.h> 63 # include <net80211/ieee80211_ioctl.h> 64 #endif 65 66 #include <assert.h> 67 #include <errno.h> 68 #include <fcntl.h> 69 #include <fnmatch.h> 70 #include <paths.h> 71 #include <stddef.h> 72 #include <stdio.h> 73 #include <stdlib.h> 74 #include <string.h> 75 #include <unistd.h> 76 77 #if defined(OpenBSD) && OpenBSD >= 201411 78 /* OpenBSD dropped the global setting from sysctl but left the #define 79 * which causes a EPERM error when trying to use it. 80 * I think both the error and keeping the define are wrong, so we #undef it. */ 81 #undef IPV6CTL_ACCEPT_RTADV 82 #endif 83 84 #include "common.h" 85 #include "dhcp.h" 86 #include "if.h" 87 #include "if-options.h" 88 #include "ipv4.h" 89 #include "ipv4ll.h" 90 #include "ipv6.h" 91 #include "ipv6nd.h" 92 #include "logerr.h" 93 #include "privsep.h" 94 #include "route.h" 95 #include "sa.h" 96 97 #ifndef RT_ROUNDUP 98 #define RT_ROUNDUP(a) \ 99 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 100 #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len)) 101 #endif 102 103 /* Ignore these interface names which look like ethernet but are virtual. */ 104 static const char * const ifnames_ignore[] = { 105 "bridge", 106 "fwe", /* Firewire */ 107 "tap", 108 "xvif", /* XEN DOM0 -> guest interface */ 109 NULL 110 }; 111 112 #ifdef INET6 113 static void ifa_setscope(struct sockaddr_in6 *, unsigned int); 114 static unsigned int ifa_getscope(const struct sockaddr_in6 *); 115 #endif 116 117 struct priv { 118 int pf_inet6_fd; 119 }; 120 121 struct rtm 122 { 123 struct rt_msghdr hdr; 124 char buffer[sizeof(struct sockaddr_storage) * RTAX_MAX]; 125 }; 126 127 int 128 if_init(__unused struct interface *iface) 129 { 130 /* BSD promotes secondary address by default */ 131 return 0; 132 } 133 134 int 135 if_conf(__unused struct interface *iface) 136 { 137 /* No extra checks needed on BSD */ 138 return 0; 139 } 140 141 int 142 if_opensockets_os(struct dhcpcd_ctx *ctx) 143 { 144 struct priv *priv; 145 int n; 146 #if defined(RO_MSGFILTER) || defined(ROUTE_MSGFILTER) 147 unsigned char msgfilter[] = { 148 RTM_IFINFO, 149 #ifdef RTM_IFANNOUNCE 150 RTM_IFANNOUNCE, 151 #endif 152 RTM_ADD, RTM_CHANGE, RTM_DELETE, RTM_MISS, 153 #ifdef RTM_CHGADDR 154 RTM_CHGADDR, 155 #endif 156 RTM_NEWADDR, RTM_DELADDR 157 }; 158 #ifdef ROUTE_MSGFILTER 159 unsigned int i, msgfilter_mask; 160 #endif 161 #endif 162 163 if ((priv = malloc(sizeof(*priv))) == NULL) 164 return -1; 165 ctx->priv = priv; 166 167 #ifdef INET6 168 priv->pf_inet6_fd = xsocket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); 169 /* Don't return an error so we at least work on kernels witout INET6 170 * even though we expect INET6 support. 171 * We will fail noisily elsewhere anyway. */ 172 #else 173 priv->pf_inet6_fd = -1; 174 #endif 175 176 #define SOCK_FLAGS (SOCK_CLOEXEC | SOCK_NONBLOCK) 177 ctx->link_fd = xsocket(PF_ROUTE, SOCK_RAW | SOCK_FLAGS, AF_UNSPEC); 178 #undef SOCK_FLAGS 179 if (ctx->link_fd == -1) 180 return -1; 181 182 /* Ignore our own route(4) messages. 183 * Sadly there is no way of doing this for route(4) messages 184 * generated from addresses we add/delete. */ 185 n = 0; 186 if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_USELOOPBACK, 187 &n, sizeof(n)) == -1) 188 logerr("%s: SO_USELOOPBACK", __func__); 189 190 #if defined(RO_MSGFILTER) 191 if (setsockopt(ctx->link_fd, PF_ROUTE, RO_MSGFILTER, 192 &msgfilter, sizeof(msgfilter)) == -1) 193 logerr(__func__); 194 #elif defined(ROUTE_MSGFILTER) 195 /* Convert the array into a bitmask. */ 196 msgfilter_mask = 0; 197 for (i = 0; i < __arraycount(msgfilter); i++) 198 msgfilter_mask |= ROUTE_FILTER(msgfilter[i]); 199 if (setsockopt(ctx->link_fd, PF_ROUTE, ROUTE_MSGFILTER, 200 &msgfilter_mask, sizeof(msgfilter_mask)) == -1) 201 logerr(__func__); 202 #else 203 #warning kernel does not support route message filtering 204 #endif 205 206 return 0; 207 } 208 209 void 210 if_closesockets_os(struct dhcpcd_ctx *ctx) 211 { 212 struct priv *priv; 213 214 priv = (struct priv *)ctx->priv; 215 if (priv->pf_inet6_fd != -1) 216 close(priv->pf_inet6_fd); 217 free(priv); 218 ctx->priv = NULL; 219 } 220 221 #if defined(SIOCALIFADDR) && defined(IFLR_ACTIVE) /*NetBSD */ 222 static int 223 if_ioctllink(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len) 224 { 225 int s; 226 int retval; 227 228 #ifdef PRIVSEP 229 if (ctx->options & DHCPCD_PRIVSEP) 230 return (int)ps_root_ioctllink(ctx, req, data, len); 231 #else 232 UNUSED(ctx); 233 #endif 234 235 s = socket(PF_LINK, SOCK_DGRAM, 0); 236 if (s == -1) 237 return -1; 238 retval = ioctl(s, req, data, len); 239 close(s); 240 return retval; 241 } 242 #endif 243 244 int 245 if_setmac(struct interface *ifp, void *mac, uint8_t maclen) 246 { 247 248 if (ifp->hwlen != maclen) { 249 errno = EINVAL; 250 return -1; 251 } 252 253 #if defined(SIOCALIFADDR) && defined(IFLR_ACTIVE) /*NetBSD */ 254 struct if_laddrreq iflr = { .flags = IFLR_ACTIVE }; 255 struct sockaddr_dl *sdl = satosdl(&iflr.addr); 256 int retval; 257 258 strlcpy(iflr.iflr_name, ifp->name, sizeof(iflr.iflr_name)); 259 sdl->sdl_family = AF_LINK; 260 sdl->sdl_len = sizeof(*sdl); 261 sdl->sdl_alen = maclen; 262 memcpy(LLADDR(sdl), mac, maclen); 263 retval = if_ioctllink(ifp->ctx, SIOCALIFADDR, &iflr, sizeof(iflr)); 264 265 /* Try and remove the old address */ 266 memcpy(LLADDR(sdl), ifp->hwaddr, ifp->hwlen); 267 if_ioctllink(ifp->ctx, SIOCDLIFADDR, &iflr, sizeof(iflr)); 268 269 return retval; 270 #else 271 struct ifreq ifr = { 272 .ifr_addr.sa_family = AF_LINK, 273 .ifr_addr.sa_len = maclen, 274 }; 275 276 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name)); 277 memcpy(ifr.ifr_addr.sa_data, mac, maclen); 278 return if_ioctl(ifp->ctx, SIOCSIFLLADDR, &ifr, sizeof(ifr)); 279 #endif 280 } 281 282 static bool 283 if_ignore1(const char *drvname) 284 { 285 const char * const *p; 286 287 for (p = ifnames_ignore; *p; p++) { 288 if (strcmp(*p, drvname) == 0) 289 return true; 290 } 291 return false; 292 } 293 294 bool 295 if_ignore(struct dhcpcd_ctx *ctx, const char *ifname) 296 { 297 struct if_spec spec; 298 299 if (if_nametospec(ifname, &spec) != 0) 300 return false; 301 302 if (if_ignore1(spec.drvname)) 303 return true; 304 305 #ifdef SIOCGIFGROUP 306 struct ifgroupreq ifgr = { .ifgr_len = 0 }; 307 struct ifg_req *ifg; 308 size_t ifg_len; 309 310 /* Sadly it is possible to remove the device name 311 * from the interface groups, but hopefully this 312 * will be very unlikely.... */ 313 314 strlcpy(ifgr.ifgr_name, ifname, sizeof(ifgr.ifgr_name)); 315 if (ioctl(ctx->pf_inet_fd, SIOCGIFGROUP, &ifgr) == -1 || 316 (ifgr.ifgr_groups = malloc(ifgr.ifgr_len)) == NULL || 317 ioctl(ctx->pf_inet_fd, SIOCGIFGROUP, &ifgr) == -1) 318 { 319 logerr(__func__); 320 return false; 321 } 322 323 for (ifg = ifgr.ifgr_groups, ifg_len = ifgr.ifgr_len; 324 ifg && ifg_len >= sizeof(*ifg); 325 ifg++, ifg_len -= sizeof(*ifg)) 326 { 327 if (if_ignore1(ifg->ifgrq_group)) 328 return true; 329 } 330 #else 331 UNUSED(ctx); 332 #endif 333 334 return false; 335 } 336 337 int 338 if_carrier(struct interface *ifp) 339 { 340 struct ifmediareq ifmr = { .ifm_status = 0 }; 341 342 strlcpy(ifmr.ifm_name, ifp->name, sizeof(ifmr.ifm_name)); 343 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr) == -1 || 344 !(ifmr.ifm_status & IFM_AVALID)) 345 return LINK_UNKNOWN; 346 347 return (ifmr.ifm_status & IFM_ACTIVE) ? LINK_UP : LINK_DOWN; 348 } 349 350 static void 351 if_linkaddr(struct sockaddr_dl *sdl, const struct interface *ifp) 352 { 353 354 memset(sdl, 0, sizeof(*sdl)); 355 sdl->sdl_family = AF_LINK; 356 sdl->sdl_len = sizeof(*sdl); 357 sdl->sdl_nlen = sdl->sdl_alen = sdl->sdl_slen = 0; 358 sdl->sdl_index = (unsigned short)ifp->index; 359 } 360 361 #if defined(SIOCG80211NWID) || defined(SIOCGETVLAN) 362 static int if_direct_ioctl(int s, const char *ifname, 363 unsigned long cmd, void *data) 364 { 365 366 strlcpy(data, ifname, IFNAMSIZ); 367 return ioctl(s, cmd, data); 368 } 369 370 static int if_indirect_ioctl(int s, const char *ifname, 371 unsigned long cmd, void *data) 372 { 373 struct ifreq ifr; 374 375 memset(&ifr, 0, sizeof(ifr)); 376 ifr.ifr_data = data; 377 return if_direct_ioctl(s, ifname, cmd, &ifr); 378 } 379 #endif 380 381 static int 382 if_getssid1(int s, const char *ifname, void *ssid) 383 { 384 int retval = -1; 385 #if defined(SIOCG80211NWID) 386 struct ieee80211_nwid nwid; 387 #elif defined(IEEE80211_IOC_SSID) 388 struct ieee80211req ireq; 389 char nwid[IEEE80211_NWID_LEN]; 390 #endif 391 392 #if defined(SIOCG80211NWID) /* NetBSD */ 393 memset(&nwid, 0, sizeof(nwid)); 394 if (if_indirect_ioctl(s, ifname, SIOCG80211NWID, &nwid) == 0) { 395 if (ssid == NULL) 396 retval = nwid.i_len; 397 else if (nwid.i_len > IF_SSIDLEN) 398 errno = ENOBUFS; 399 else { 400 retval = nwid.i_len; 401 memcpy(ssid, nwid.i_nwid, nwid.i_len); 402 } 403 } 404 #elif defined(IEEE80211_IOC_SSID) /* FreeBSD */ 405 memset(&ireq, 0, sizeof(ireq)); 406 strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name)); 407 ireq.i_type = IEEE80211_IOC_SSID; 408 ireq.i_val = -1; 409 memset(nwid, 0, sizeof(nwid)); 410 ireq.i_data = &nwid; 411 if (ioctl(s, SIOCG80211, &ireq) == 0) { 412 if (ssid == NULL) 413 retval = ireq.i_len; 414 else if (ireq.i_len > IF_SSIDLEN) 415 errno = ENOBUFS; 416 else { 417 retval = ireq.i_len; 418 memcpy(ssid, nwid, ireq.i_len); 419 } 420 } 421 #else 422 errno = ENOSYS; 423 #endif 424 425 return retval; 426 } 427 428 int 429 if_getssid(struct interface *ifp) 430 { 431 int r; 432 433 r = if_getssid1(ifp->ctx->pf_inet_fd, ifp->name, ifp->ssid); 434 if (r != -1) 435 ifp->ssid_len = (unsigned int)r; 436 else 437 ifp->ssid_len = 0; 438 ifp->ssid[ifp->ssid_len] = '\0'; 439 return r; 440 } 441 442 /* 443 * FreeBSD allows for Virtual Access Points 444 * We need to check if the interface is a Virtual Interface Master 445 * and if so, don't use it. 446 * This check is made by virtue of being a IEEE80211 device but 447 * returning the SSID gives an error. 448 */ 449 int 450 if_vimaster(const struct dhcpcd_ctx *ctx, const char *ifname) 451 { 452 int r; 453 struct ifmediareq ifmr; 454 455 memset(&ifmr, 0, sizeof(ifmr)); 456 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); 457 r = ioctl(ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr); 458 if (r == -1) 459 return -1; 460 if (ifmr.ifm_status & IFM_AVALID && 461 IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211) 462 { 463 if (if_getssid1(ctx->pf_inet_fd, ifname, NULL) == -1) 464 return 1; 465 } 466 return 0; 467 } 468 469 unsigned short 470 if_vlanid(const struct interface *ifp) 471 { 472 #ifdef SIOCGETVLAN 473 struct vlanreq vlr; 474 475 memset(&vlr, 0, sizeof(vlr)); 476 if (if_indirect_ioctl(ifp->ctx->pf_inet_fd, 477 ifp->name, SIOCGETVLAN, &vlr) != 0) 478 return 0; /* 0 means no VLANID */ 479 return vlr.vlr_tag; 480 #elif defined(SIOCGVNETID) 481 struct ifreq ifr; 482 483 memset(&ifr, 0, sizeof(ifr)); 484 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name)); 485 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGVNETID, &ifr) != 0) 486 return 0; /* 0 means no VLANID */ 487 return ifr.ifr_vnetid; 488 #else 489 UNUSED(ifp); 490 return 0; /* 0 means no VLANID */ 491 #endif 492 } 493 494 static int 495 get_addrs(int type, const void *data, size_t data_len, 496 const struct sockaddr **sa) 497 { 498 const char *cp, *ep; 499 int i; 500 501 cp = data; 502 ep = cp + data_len; 503 for (i = 0; i < RTAX_MAX; i++) { 504 if (type & (1 << i)) { 505 if (cp >= ep) { 506 errno = EINVAL; 507 return -1; 508 } 509 sa[i] = (const struct sockaddr *)cp; 510 RT_ADVANCE(cp, sa[i]); 511 } else 512 sa[i] = NULL; 513 } 514 515 return 0; 516 } 517 518 static struct interface * 519 if_findsdl(struct dhcpcd_ctx *ctx, const struct sockaddr_dl *sdl) 520 { 521 522 if (sdl->sdl_index) 523 return if_findindex(ctx->ifaces, sdl->sdl_index); 524 525 if (sdl->sdl_nlen) { 526 char ifname[IF_NAMESIZE]; 527 528 memcpy(ifname, sdl->sdl_data, sdl->sdl_nlen); 529 ifname[sdl->sdl_nlen] = '\0'; 530 return if_find(ctx->ifaces, ifname); 531 } 532 if (sdl->sdl_alen) { 533 struct interface *ifp; 534 535 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 536 if (ifp->hwlen == sdl->sdl_alen && 537 memcmp(ifp->hwaddr, 538 sdl->sdl_data, sdl->sdl_alen) == 0) 539 return ifp; 540 } 541 } 542 543 errno = ENOENT; 544 return NULL; 545 } 546 547 static struct interface * 548 if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa) 549 { 550 if (sa == NULL) { 551 errno = EINVAL; 552 return NULL; 553 } 554 555 switch (sa->sa_family) { 556 case AF_LINK: 557 { 558 const struct sockaddr_dl *sdl; 559 560 sdl = (const void *)sa; 561 return if_findsdl(ctx, sdl); 562 } 563 #ifdef INET 564 case AF_INET: 565 { 566 const struct sockaddr_in *sin; 567 struct ipv4_addr *ia; 568 569 sin = (const void *)sa; 570 if ((ia = ipv4_findmaskaddr(ctx, &sin->sin_addr))) 571 return ia->iface; 572 break; 573 } 574 #endif 575 #ifdef INET6 576 case AF_INET6: 577 { 578 const struct sockaddr_in6 *sin; 579 unsigned int scope; 580 struct ipv6_addr *ia; 581 582 sin = (const void *)sa; 583 scope = ifa_getscope(sin); 584 if (scope != 0) 585 return if_findindex(ctx->ifaces, scope); 586 if ((ia = ipv6_findmaskaddr(ctx, &sin->sin6_addr))) 587 return ia->iface; 588 break; 589 } 590 #endif 591 default: 592 errno = EAFNOSUPPORT; 593 return NULL; 594 } 595 596 errno = ENOENT; 597 return NULL; 598 } 599 600 static void 601 if_copysa(struct sockaddr *dst, const struct sockaddr *src) 602 { 603 604 assert(dst != NULL); 605 assert(src != NULL); 606 607 memcpy(dst, src, src->sa_len); 608 #if defined(INET6) && defined(__KAME__) 609 if (dst->sa_family == AF_INET6) { 610 struct in6_addr *in6; 611 612 in6 = &satosin6(dst)->sin6_addr; 613 if (IN6_IS_ADDR_LINKLOCAL(in6)) 614 in6->s6_addr[2] = in6->s6_addr[3] = '\0'; 615 } 616 #endif 617 } 618 619 int 620 if_route(unsigned char cmd, const struct rt *rt) 621 { 622 struct dhcpcd_ctx *ctx; 623 struct rtm rtmsg; 624 struct rt_msghdr *rtm = &rtmsg.hdr; 625 char *bp = rtmsg.buffer; 626 struct sockaddr_dl sdl; 627 bool gateway_unspec; 628 629 assert(rt != NULL); 630 assert(rt->rt_ifp != NULL); 631 assert(rt->rt_ifp->ctx != NULL); 632 ctx = rt->rt_ifp->ctx; 633 634 #define ADDSA(sa) do { \ 635 memcpy(bp, (sa), (sa)->sa_len); \ 636 bp += RT_ROUNDUP((sa)->sa_len); \ 637 } while (0 /* CONSTCOND */) 638 639 memset(&rtmsg, 0, sizeof(rtmsg)); 640 rtm->rtm_version = RTM_VERSION; 641 rtm->rtm_type = cmd; 642 #ifdef __OpenBSD__ 643 rtm->rtm_pid = getpid(); 644 #endif 645 rtm->rtm_seq = ++ctx->seq; 646 rtm->rtm_flags = (int)rt->rt_flags; 647 rtm->rtm_addrs = RTA_DST; 648 #ifdef RTF_PINNED 649 if (cmd != RTM_ADD) 650 rtm->rtm_flags |= RTF_PINNED; 651 #endif 652 653 gateway_unspec = sa_is_unspecified(&rt->rt_gateway); 654 655 if (cmd == RTM_ADD || cmd == RTM_CHANGE) { 656 bool netmask_bcast = sa_is_allones(&rt->rt_netmask); 657 658 rtm->rtm_flags |= RTF_UP; 659 rtm->rtm_addrs |= RTA_GATEWAY; 660 if (!(rtm->rtm_flags & RTF_REJECT) && 661 !sa_is_loopback(&rt->rt_gateway)) 662 { 663 rtm->rtm_index = (unsigned short)rt->rt_ifp->index; 664 /* 665 * OpenBSD rejects the message for on-link routes. 666 * FreeBSD-12 kernel apparently panics. 667 * I can't replicate the panic, but better safe than sorry! 668 * https://roy.marples.name/archives/dhcpcd-discuss/0002286.html 669 * 670 * Neither OS currently allows IPv6 address sharing anyway, so let's 671 * try to encourage someone to fix that by logging a waring during compile. 672 */ 673 #if defined(__FreeBSD__) || defined(__OpenBSD__) 674 #warning kernel does not allow IPv6 address sharing 675 if (!gateway_unspec || rt->rt_dest.sa_family!=AF_INET6) 676 #endif 677 rtm->rtm_addrs |= RTA_IFP; 678 if (!sa_is_unspecified(&rt->rt_ifa)) 679 rtm->rtm_addrs |= RTA_IFA; 680 } 681 if (netmask_bcast) 682 rtm->rtm_flags |= RTF_HOST; 683 /* Network routes are cloning or connected if supported. 684 * All other routes are static. */ 685 if (gateway_unspec) { 686 #ifdef RTF_CLONING 687 rtm->rtm_flags |= RTF_CLONING; 688 #endif 689 #ifdef RTF_CONNECTED 690 rtm->rtm_flags |= RTF_CONNECTED; 691 #endif 692 #ifdef RTP_CONNECTED 693 rtm->rtm_priority = RTP_CONNECTED; 694 #endif 695 #ifdef RTF_CLONING 696 if (netmask_bcast) { 697 /* 698 * We add a cloning network route for a single 699 * host. Traffic to the host will generate a 700 * cloned route and the hardware address will 701 * resolve correctly. 702 * It might be more correct to use RTF_HOST 703 * instead of RTF_CLONING, and that does work, 704 * but some OS generate an arp warning 705 * diagnostic which we don't want to do. 706 */ 707 rtm->rtm_flags &= ~RTF_HOST; 708 } 709 #endif 710 } else 711 rtm->rtm_flags |= RTF_GATEWAY; 712 713 /* Emulate the kernel by marking address generated 714 * network routes non-static. */ 715 if (!(rt->rt_dflags & RTDF_IFA_ROUTE)) 716 rtm->rtm_flags |= RTF_STATIC; 717 718 if (rt->rt_mtu != 0) { 719 rtm->rtm_inits |= RTV_MTU; 720 rtm->rtm_rmx.rmx_mtu = rt->rt_mtu; 721 } 722 } 723 724 if (!(rtm->rtm_flags & RTF_HOST)) 725 rtm->rtm_addrs |= RTA_NETMASK; 726 727 if_linkaddr(&sdl, rt->rt_ifp); 728 729 ADDSA(&rt->rt_dest); 730 731 if (rtm->rtm_addrs & RTA_GATEWAY) { 732 if (gateway_unspec) 733 ADDSA((struct sockaddr *)&sdl); 734 else { 735 union sa_ss gateway; 736 737 if_copysa(&gateway.sa, &rt->rt_gateway); 738 #ifdef INET6 739 if (gateway.sa.sa_family == AF_INET6) 740 ifa_setscope(&gateway.sin6, rt->rt_ifp->index); 741 #endif 742 ADDSA(&gateway.sa); 743 } 744 } 745 746 if (rtm->rtm_addrs & RTA_NETMASK) 747 ADDSA(&rt->rt_netmask); 748 749 if (rtm->rtm_addrs & RTA_IFP) 750 ADDSA((struct sockaddr *)&sdl); 751 752 if (rtm->rtm_addrs & RTA_IFA) 753 ADDSA(&rt->rt_ifa); 754 755 #undef ADDSA 756 757 rtm->rtm_msglen = (unsigned short)(bp - (char *)rtm); 758 759 #ifdef PRIVSEP 760 if (ctx->options & DHCPCD_PRIVSEP) { 761 if (ps_root_route(ctx, rtm, rtm->rtm_msglen) == -1) 762 return -1; 763 return 0; 764 } 765 #endif 766 if (write(ctx->link_fd, rtm, rtm->rtm_msglen) == -1) 767 return -1; 768 return 0; 769 } 770 771 static bool 772 if_realroute(const struct rt_msghdr *rtm) 773 { 774 775 #ifdef RTF_CLONED 776 if (rtm->rtm_flags & RTF_CLONED) 777 return false; 778 #endif 779 #ifdef RTF_WASCLONED 780 if (rtm->rtm_flags & RTF_WASCLONED) 781 return false; 782 #endif 783 #ifdef RTF_LOCAL 784 if (rtm->rtm_flags & RTF_LOCAL) 785 return false; 786 #endif 787 #ifdef RTF_BROADCAST 788 if (rtm->rtm_flags & RTF_BROADCAST) 789 return false; 790 #endif 791 return true; 792 } 793 794 static int 795 if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm) 796 { 797 const struct sockaddr *rti_info[RTAX_MAX]; 798 799 if (!(rtm->rtm_addrs & RTA_DST)) { 800 errno = EINVAL; 801 return -1; 802 } 803 if (rtm->rtm_type != RTM_MISS && !(rtm->rtm_addrs & RTA_GATEWAY)) { 804 errno = EINVAL; 805 return -1; 806 } 807 808 if (get_addrs(rtm->rtm_addrs, (const char *)rtm + sizeof(*rtm), 809 rtm->rtm_msglen - sizeof(*rtm), rti_info) == -1) 810 return -1; 811 memset(rt, 0, sizeof(*rt)); 812 813 rt->rt_flags = (unsigned int)rtm->rtm_flags; 814 if_copysa(&rt->rt_dest, rti_info[RTAX_DST]); 815 if (rtm->rtm_addrs & RTA_NETMASK) { 816 if_copysa(&rt->rt_netmask, rti_info[RTAX_NETMASK]); 817 if (rt->rt_netmask.sa_family == 255) /* Why? */ 818 rt->rt_netmask.sa_family = rt->rt_dest.sa_family; 819 } 820 821 /* dhcpcd likes an unspecified gateway to indicate via the link. 822 * However we need to know if gateway was a link with an address. */ 823 if (rtm->rtm_addrs & RTA_GATEWAY) { 824 if (rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) { 825 const struct sockaddr_dl *sdl; 826 827 sdl = (const struct sockaddr_dl*) 828 (const void *)rti_info[RTAX_GATEWAY]; 829 if (sdl->sdl_alen != 0) 830 rt->rt_dflags |= RTDF_GATELINK; 831 } else if (rtm->rtm_flags & RTF_GATEWAY) 832 if_copysa(&rt->rt_gateway, rti_info[RTAX_GATEWAY]); 833 } 834 835 if (rtm->rtm_addrs & RTA_IFA) 836 if_copysa(&rt->rt_ifa, rti_info[RTAX_IFA]); 837 838 rt->rt_mtu = (unsigned int)rtm->rtm_rmx.rmx_mtu; 839 840 if (rtm->rtm_index) 841 rt->rt_ifp = if_findindex(ctx->ifaces, rtm->rtm_index); 842 else if (rtm->rtm_addrs & RTA_IFP) 843 rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_IFP]); 844 else if (rtm->rtm_addrs & RTA_GATEWAY) 845 rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_GATEWAY]); 846 else 847 rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_DST]); 848 849 if (rt->rt_ifp == NULL && rtm->rtm_type == RTM_MISS) 850 rt->rt_ifp = if_find(ctx->ifaces, "lo0"); 851 852 if (rt->rt_ifp == NULL) { 853 errno = ESRCH; 854 return -1; 855 } 856 return 0; 857 } 858 859 int 860 if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *kroutes, int af) 861 { 862 struct rt_msghdr *rtm; 863 int mib[6]; 864 size_t needed; 865 char *buf, *p, *end; 866 struct rt rt, *rtn; 867 868 mib[0] = CTL_NET; 869 mib[1] = PF_ROUTE; 870 mib[2] = 0; 871 mib[3] = af; 872 mib[4] = NET_RT_DUMP; 873 mib[5] = 0; 874 875 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) 876 return -1; 877 if (needed == 0) 878 return 0; 879 if ((buf = malloc(needed)) == NULL) 880 return -1; 881 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { 882 free(buf); 883 return -1; 884 } 885 886 end = buf + needed; 887 for (p = buf; p < end; p += rtm->rtm_msglen) { 888 rtm = (void *)p; 889 if (p + rtm->rtm_msglen >= end) { 890 errno = EINVAL; 891 break; 892 } 893 if (!if_realroute(rtm)) 894 continue; 895 if (if_copyrt(ctx, &rt, rtm) != 0) 896 continue; 897 if ((rtn = rt_new(rt.rt_ifp)) == NULL) { 898 logerr(__func__); 899 break; 900 } 901 memcpy(rtn, &rt, sizeof(*rtn)); 902 if (rb_tree_insert_node(kroutes, rtn) != rtn) 903 rt_free(rtn); 904 } 905 free(buf); 906 return p == end ? 0 : -1; 907 } 908 909 #ifdef INET 910 int 911 if_address(unsigned char cmd, const struct ipv4_addr *ia) 912 { 913 int r; 914 struct in_aliasreq ifra; 915 struct dhcpcd_ctx *ctx = ia->iface->ctx; 916 917 memset(&ifra, 0, sizeof(ifra)); 918 strlcpy(ifra.ifra_name, ia->iface->name, sizeof(ifra.ifra_name)); 919 920 #define ADDADDR(var, addr) do { \ 921 (var)->sin_family = AF_INET; \ 922 (var)->sin_len = sizeof(*(var)); \ 923 (var)->sin_addr = *(addr); \ 924 } while (/*CONSTCOND*/0) 925 ADDADDR(&ifra.ifra_addr, &ia->addr); 926 ADDADDR(&ifra.ifra_mask, &ia->mask); 927 if (cmd == RTM_NEWADDR && ia->brd.s_addr != INADDR_ANY) 928 ADDADDR(&ifra.ifra_broadaddr, &ia->brd); 929 #undef ADDADDR 930 931 r = if_ioctl(ctx, 932 cmd == RTM_DELADDR ? SIOCDIFADDR : SIOCAIFADDR, &ifra,sizeof(ifra)); 933 return r; 934 } 935 936 #if !(defined(HAVE_IFADDRS_ADDRFLAGS) && defined(HAVE_IFAM_ADDRFLAGS)) 937 int 938 if_addrflags(const struct interface *ifp, const struct in_addr *addr, 939 __unused const char *alias) 940 { 941 #ifdef SIOCGIFAFLAG_IN 942 struct ifreq ifr; 943 struct sockaddr_in *sin; 944 945 memset(&ifr, 0, sizeof(ifr)); 946 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name)); 947 sin = (void *)&ifr.ifr_addr; 948 sin->sin_family = AF_INET; 949 sin->sin_addr = *addr; 950 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFAFLAG_IN, &ifr) == -1) 951 return -1; 952 return ifr.ifr_addrflags; 953 #else 954 UNUSED(ifp); 955 UNUSED(addr); 956 return 0; 957 #endif 958 } 959 #endif 960 #endif /* INET */ 961 962 #ifdef INET6 963 static void 964 ifa_setscope(struct sockaddr_in6 *sin, unsigned int ifindex) 965 { 966 967 #ifdef __KAME__ 968 /* KAME based systems want to store the scope inside the sin6_addr 969 * for link local addresses */ 970 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 971 uint16_t scope = htons((uint16_t)ifindex); 972 memcpy(&sin->sin6_addr.s6_addr[2], &scope, 973 sizeof(scope)); 974 } 975 sin->sin6_scope_id = 0; 976 #else 977 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) 978 sin->sin6_scope_id = ifindex; 979 else 980 sin->sin6_scope_id = 0; 981 #endif 982 } 983 984 static unsigned int 985 ifa_getscope(const struct sockaddr_in6 *sin) 986 { 987 #ifdef __KAME__ 988 uint16_t scope; 989 #endif 990 991 if (!IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) 992 return 0; 993 #ifdef __KAME__ 994 memcpy(&scope, &sin->sin6_addr.s6_addr[2], sizeof(scope)); 995 return (unsigned int)ntohs(scope); 996 #else 997 return (unsigned int)sin->sin6_scope_id; 998 #endif 999 } 1000 1001 static int 1002 if_ioctl6(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len) 1003 { 1004 struct priv *priv; 1005 1006 #ifdef PRIVSEP 1007 if (ctx->options & DHCPCD_PRIVSEP) 1008 return (int)ps_root_ioctl6(ctx, req, data, len); 1009 #endif 1010 1011 priv = ctx->priv; 1012 return ioctl(priv->pf_inet6_fd, req, data, len); 1013 } 1014 1015 int 1016 if_address6(unsigned char cmd, const struct ipv6_addr *ia) 1017 { 1018 struct in6_aliasreq ifa; 1019 struct in6_addr mask; 1020 struct dhcpcd_ctx *ctx = ia->iface->ctx; 1021 1022 memset(&ifa, 0, sizeof(ifa)); 1023 strlcpy(ifa.ifra_name, ia->iface->name, sizeof(ifa.ifra_name)); 1024 /* 1025 * We should not set IN6_IFF_TENTATIVE as the kernel should be 1026 * able to work out if it's a new address or not. 1027 * 1028 * We should set IN6_IFF_AUTOCONF, but the kernel won't let us. 1029 * This is probably a safety measure, but still it's not entirely right 1030 * either. 1031 */ 1032 #if 0 1033 if (ia->autoconf) 1034 ifa.ifra_flags |= IN6_IFF_AUTOCONF; 1035 #endif 1036 #if defined(__FreeBSD__) || defined(__DragonFly__) 1037 if (ia->addr_flags & IN6_IFF_TENTATIVE) 1038 ifa.ifra_flags |= IN6_IFF_TENTATIVE; 1039 #endif 1040 #ifdef IPV6_MANGETEMPADDR 1041 if (ia->flags & IPV6_AF_TEMPORARY) 1042 ifa.ifra_flags |= IN6_IFF_TEMPORARY; 1043 #endif 1044 1045 #define ADDADDR(v, addr) { \ 1046 (v)->sin6_family = AF_INET6; \ 1047 (v)->sin6_len = sizeof(*v); \ 1048 (v)->sin6_addr = *(addr); \ 1049 } 1050 1051 ADDADDR(&ifa.ifra_addr, &ia->addr); 1052 ifa_setscope(&ifa.ifra_addr, ia->iface->index); 1053 ipv6_mask(&mask, ia->prefix_len); 1054 ADDADDR(&ifa.ifra_prefixmask, &mask); 1055 1056 #undef ADDADDR 1057 1058 /* 1059 * Every BSD kernel wants to add the prefix of the address to it's 1060 * list of RA received prefixes. 1061 * THIS IS WRONG because there (as the comments in the kernel state) 1062 * is no API for managing prefix lifetime and the kernel should not 1063 * pretend it's from a RA either. 1064 * 1065 * The issue is that the very first assigned prefix will inherit the 1066 * lifetime of the address, but any subsequent alteration of the 1067 * address OR it's lifetime will not affect the prefix lifetime. 1068 * As such, we cannot stop the prefix from timing out and then 1069 * constantly removing the prefix route dhcpcd is capable of adding 1070 * in it's absense. 1071 * 1072 * What we can do to mitigate the issue is to add the address with 1073 * infinite lifetimes, so the prefix route will never time out. 1074 * Once done, we can then set lifetimes on the address and all is good. 1075 * The downside of this approach is that we need to manually remove 1076 * the kernel route because it has no lifetime, but this is OK as 1077 * dhcpcd will handle this too. 1078 * 1079 * This issue is discussed on the NetBSD mailing lists here: 1080 * http://mail-index.netbsd.org/tech-net/2016/08/05/msg006044.html 1081 * 1082 * Fixed in NetBSD-7.99.36 1083 * NOT fixed in FreeBSD - bug 195197 1084 * Fixed in OpenBSD-5.9 1085 */ 1086 1087 #if !((defined(__NetBSD_Version__) && __NetBSD_Version__ >= 799003600) || \ 1088 (defined(__OpenBSD__) && OpenBSD >= 201605)) 1089 if (cmd == RTM_NEWADDR && !(ia->flags & IPV6_AF_ADDED)) { 1090 ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 1091 ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 1092 (void)if_ioctl6(ctx, SIOCAIFADDR_IN6, &ifa, sizeof(ifa)); 1093 } 1094 #endif 1095 1096 #if defined(__OpenBSD__) && OpenBSD <= 201705 1097 /* BUT OpenBSD older than 6.2 does not reset the address lifetime 1098 * for subsequent calls... 1099 * Luckily dhcpcd will remove the lease when it expires so 1100 * just set an infinite lifetime, unless a temporary address. */ 1101 if (ifa.ifra_flags & IN6_IFF_PRIVACY) { 1102 ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime; 1103 ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime; 1104 } else { 1105 ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 1106 ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 1107 } 1108 #else 1109 ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime; 1110 ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime; 1111 #endif 1112 1113 return if_ioctl6(ctx, 1114 cmd == RTM_DELADDR ? SIOCDIFADDR_IN6 : SIOCAIFADDR_IN6, 1115 &ifa, sizeof(ifa)); 1116 } 1117 1118 int 1119 if_addrflags6(const struct interface *ifp, const struct in6_addr *addr, 1120 __unused const char *alias) 1121 { 1122 int flags; 1123 struct in6_ifreq ifr6; 1124 struct priv *priv; 1125 1126 memset(&ifr6, 0, sizeof(ifr6)); 1127 strlcpy(ifr6.ifr_name, ifp->name, sizeof(ifr6.ifr_name)); 1128 ifr6.ifr_addr.sin6_family = AF_INET6; 1129 ifr6.ifr_addr.sin6_addr = *addr; 1130 ifa_setscope(&ifr6.ifr_addr, ifp->index); 1131 priv = (struct priv *)ifp->ctx->priv; 1132 if (ioctl(priv->pf_inet6_fd, SIOCGIFAFLAG_IN6, &ifr6) != -1) 1133 flags = ifr6.ifr_ifru.ifru_flags6; 1134 else 1135 flags = -1; 1136 return flags; 1137 } 1138 1139 int 1140 if_getlifetime6(struct ipv6_addr *ia) 1141 { 1142 struct in6_ifreq ifr6; 1143 time_t t; 1144 struct in6_addrlifetime *lifetime; 1145 struct priv *priv; 1146 1147 memset(&ifr6, 0, sizeof(ifr6)); 1148 strlcpy(ifr6.ifr_name, ia->iface->name, sizeof(ifr6.ifr_name)); 1149 ifr6.ifr_addr.sin6_family = AF_INET6; 1150 ifr6.ifr_addr.sin6_addr = ia->addr; 1151 ifa_setscope(&ifr6.ifr_addr, ia->iface->index); 1152 priv = (struct priv *)ia->iface->ctx->priv; 1153 if (ioctl(priv->pf_inet6_fd, SIOCGIFALIFETIME_IN6, &ifr6) == -1) 1154 return -1; 1155 clock_gettime(CLOCK_MONOTONIC, &ia->created); 1156 1157 #if defined(__FreeBSD__) || defined(__DragonFly__) 1158 t = ia->created.tv_sec; 1159 #else 1160 t = time(NULL); 1161 #endif 1162 1163 lifetime = &ifr6.ifr_ifru.ifru_lifetime; 1164 if (lifetime->ia6t_preferred) 1165 ia->prefix_pltime = (uint32_t)(lifetime->ia6t_preferred - 1166 MIN(t, lifetime->ia6t_preferred)); 1167 else 1168 ia->prefix_pltime = ND6_INFINITE_LIFETIME; 1169 if (lifetime->ia6t_expire) { 1170 ia->prefix_vltime = (uint32_t)(lifetime->ia6t_expire - 1171 MIN(t, lifetime->ia6t_expire)); 1172 /* Calculate the created time */ 1173 ia->created.tv_sec -= lifetime->ia6t_vltime - ia->prefix_vltime; 1174 } else 1175 ia->prefix_vltime = ND6_INFINITE_LIFETIME; 1176 return 0; 1177 } 1178 #endif 1179 1180 static int 1181 if_announce(struct dhcpcd_ctx *ctx, const struct if_announcemsghdr *ifan) 1182 { 1183 1184 if (ifan->ifan_msglen < sizeof(*ifan)) { 1185 errno = EINVAL; 1186 return -1; 1187 } 1188 1189 switch(ifan->ifan_what) { 1190 case IFAN_ARRIVAL: 1191 return dhcpcd_handleinterface(ctx, 1, ifan->ifan_name); 1192 case IFAN_DEPARTURE: 1193 return dhcpcd_handleinterface(ctx, -1, ifan->ifan_name); 1194 } 1195 1196 return 0; 1197 } 1198 1199 static int 1200 if_ifinfo(struct dhcpcd_ctx *ctx, const struct if_msghdr *ifm) 1201 { 1202 struct interface *ifp; 1203 int link_state; 1204 1205 if (ifm->ifm_msglen < sizeof(*ifm)) { 1206 errno = EINVAL; 1207 return -1; 1208 } 1209 1210 if ((ifp = if_findindex(ctx->ifaces, ifm->ifm_index)) == NULL) 1211 return 0; 1212 1213 switch (ifm->ifm_data.ifi_link_state) { 1214 case LINK_STATE_UNKNOWN: 1215 link_state = LINK_UNKNOWN; 1216 break; 1217 #ifdef LINK_STATE_FULL_DUPLEX 1218 case LINK_STATE_HALF_DUPLEX: /* FALLTHROUGH */ 1219 case LINK_STATE_FULL_DUPLEX: /* FALLTHROUGH */ 1220 #endif 1221 case LINK_STATE_UP: 1222 link_state = LINK_UP; 1223 break; 1224 default: 1225 link_state = LINK_DOWN; 1226 break; 1227 } 1228 1229 dhcpcd_handlecarrier(ctx, link_state, 1230 (unsigned int)ifm->ifm_flags, ifp->name); 1231 return 0; 1232 } 1233 1234 static int 1235 if_rtm(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm) 1236 { 1237 struct rt rt; 1238 1239 if (rtm->rtm_msglen < sizeof(*rtm)) { 1240 errno = EINVAL; 1241 return -1; 1242 } 1243 1244 /* Ignore errors. */ 1245 if (rtm->rtm_errno != 0) 1246 return 0; 1247 1248 /* Ignore messages from ourself. */ 1249 #ifdef PRIVSEP 1250 if (ctx->ps_root_pid != 0) { 1251 if (rtm->rtm_pid == ctx->ps_root_pid) 1252 return 0; 1253 } 1254 #endif 1255 1256 if (if_copyrt(ctx, &rt, rtm) == -1) 1257 return errno == ENOTSUP ? 0 : -1; 1258 1259 #ifdef INET6 1260 /* 1261 * BSD announces host routes. 1262 * As such, we should be notified of reachability by its 1263 * existance with a hardware address. 1264 * Ensure we don't call this for a newly incomplete state. 1265 */ 1266 if (rt.rt_dest.sa_family == AF_INET6 && 1267 (rt.rt_flags & RTF_HOST || rtm->rtm_type == RTM_MISS) && 1268 !(rtm->rtm_type == RTM_ADD && !(rt.rt_dflags & RTDF_GATELINK))) 1269 { 1270 bool reachable; 1271 1272 reachable = (rtm->rtm_type == RTM_ADD || 1273 rtm->rtm_type == RTM_CHANGE) && 1274 rt.rt_dflags & RTDF_GATELINK; 1275 ipv6nd_neighbour(ctx, &rt.rt_ss_dest.sin6.sin6_addr, reachable); 1276 } 1277 #endif 1278 1279 if (rtm->rtm_type != RTM_MISS && if_realroute(rtm)) 1280 rt_recvrt(rtm->rtm_type, &rt, rtm->rtm_pid); 1281 return 0; 1282 } 1283 1284 static int 1285 if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) 1286 { 1287 struct interface *ifp; 1288 const struct sockaddr *rti_info[RTAX_MAX]; 1289 int flags; 1290 pid_t pid; 1291 1292 if (ifam->ifam_msglen < sizeof(*ifam)) { 1293 errno = EINVAL; 1294 return -1; 1295 } 1296 1297 #ifdef HAVE_IFAM_PID 1298 /* Ignore address deletions from ourself. 1299 * We need to process address flag changes though. */ 1300 if (ifam->ifam_type == RTM_DELADDR) { 1301 #ifdef PRIVSEP 1302 if (ctx->ps_root_pid != 0) { 1303 if (ifam->ifam_pid == ctx->ps_root_pid) 1304 return 0; 1305 } else 1306 #endif 1307 /* address management is done via ioctl, 1308 * so SO_USELOOPBACK has no effect, 1309 * so we do need to check the pid. */ 1310 if (ifam->ifam_pid == getpid()) 1311 return 0; 1312 } 1313 pid = ifam->ifam_pid; 1314 #else 1315 pid = 0; 1316 #endif 1317 1318 if (~ifam->ifam_addrs & RTA_IFA) 1319 return 0; 1320 if ((ifp = if_findindex(ctx->ifaces, ifam->ifam_index)) == NULL) 1321 return 0; 1322 1323 if (get_addrs(ifam->ifam_addrs, (const char *)ifam + sizeof(*ifam), 1324 ifam->ifam_msglen - sizeof(*ifam), rti_info) == -1) 1325 return -1; 1326 1327 switch (rti_info[RTAX_IFA]->sa_family) { 1328 case AF_LINK: 1329 { 1330 struct sockaddr_dl sdl; 1331 1332 #ifdef RTM_CHGADDR 1333 if (ifam->ifam_type != RTM_CHGADDR) 1334 break; 1335 #else 1336 if (ifam->ifam_type != RTM_NEWADDR) 1337 break; 1338 #endif 1339 memcpy(&sdl, rti_info[RTAX_IFA], rti_info[RTAX_IFA]->sa_len); 1340 dhcpcd_handlehwaddr(ctx, ifp->name, CLLADDR(&sdl),sdl.sdl_alen); 1341 break; 1342 } 1343 #ifdef INET 1344 case AF_INET: 1345 case 255: /* FIXME: Why 255? */ 1346 { 1347 const struct sockaddr_in *sin; 1348 struct in_addr addr, mask, bcast; 1349 1350 sin = (const void *)rti_info[RTAX_IFA]; 1351 addr.s_addr = sin != NULL && sin->sin_family == AF_INET ? 1352 sin->sin_addr.s_addr : INADDR_ANY; 1353 sin = (const void *)rti_info[RTAX_NETMASK]; 1354 mask.s_addr = sin != NULL && sin->sin_family == AF_INET ? 1355 sin->sin_addr.s_addr : INADDR_ANY; 1356 sin = (const void *)rti_info[RTAX_BRD]; 1357 bcast.s_addr = sin != NULL && sin->sin_family == AF_INET ? 1358 sin->sin_addr.s_addr : INADDR_ANY; 1359 1360 /* 1361 * NetBSD-7 and older send an invalid broadcast address. 1362 * So we need to query the actual address to get 1363 * the right one. 1364 * We can also use this to test if the address 1365 * has really been added or deleted. 1366 */ 1367 #ifdef SIOCGIFALIAS 1368 struct in_aliasreq ifra; 1369 1370 memset(&ifra, 0, sizeof(ifra)); 1371 strlcpy(ifra.ifra_name, ifp->name, sizeof(ifra.ifra_name)); 1372 ifra.ifra_addr.sin_family = AF_INET; 1373 ifra.ifra_addr.sin_len = sizeof(ifra.ifra_addr); 1374 ifra.ifra_addr.sin_addr = addr; 1375 if (ioctl(ctx->pf_inet_fd, SIOCGIFALIAS, &ifra) == -1) { 1376 if (errno != ENXIO && errno != EADDRNOTAVAIL) 1377 logerr("%s: SIOCGIFALIAS", __func__); 1378 if (ifam->ifam_type != RTM_DELADDR) 1379 break; 1380 } else { 1381 if (ifam->ifam_type == RTM_DELADDR) 1382 break; 1383 #if defined(__NetBSD_Version__) && __NetBSD_Version__ < 800000000 1384 bcast = ifra.ifra_broadaddr.sin_addr; 1385 #endif 1386 } 1387 #else 1388 #warning No SIOCGIFALIAS support 1389 /* 1390 * No SIOCGIFALIAS? That sucks! 1391 * This makes this call very heavy weight, but we 1392 * really need to know if the message is late or not. 1393 */ 1394 const struct sockaddr *sa; 1395 struct ifaddrs *ifaddrs = NULL, *ifa; 1396 1397 sa = rti_info[RTAX_IFA]; 1398 getifaddrs(&ifaddrs); 1399 for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) { 1400 if (ifa->ifa_addr == NULL) 1401 continue; 1402 if (sa_cmp(ifa->ifa_addr, sa) == 0 && 1403 strcmp(ifa->ifa_name, ifp->name) == 0) 1404 break; 1405 } 1406 freeifaddrs(ifaddrs); 1407 if (ifam->ifam_type == RTM_DELADDR) { 1408 if (ifa != NULL) 1409 break; 1410 } else { 1411 if (ifa == NULL) 1412 break; 1413 } 1414 #endif 1415 1416 #ifdef HAVE_IFAM_ADDRFLAGS 1417 flags = ifam->ifam_addrflags; 1418 #else 1419 flags = 0; 1420 #endif 1421 1422 ipv4_handleifa(ctx, ifam->ifam_type, NULL, ifp->name, 1423 &addr, &mask, &bcast, flags, pid); 1424 break; 1425 } 1426 #endif 1427 #ifdef INET6 1428 case AF_INET6: 1429 { 1430 struct in6_addr addr6, mask6; 1431 const struct sockaddr_in6 *sin6; 1432 1433 sin6 = (const void *)rti_info[RTAX_IFA]; 1434 addr6 = sin6->sin6_addr; 1435 sin6 = (const void *)rti_info[RTAX_NETMASK]; 1436 mask6 = sin6->sin6_addr; 1437 1438 /* 1439 * If the address was deleted, lets check if it's 1440 * a late message and it still exists (maybe modified). 1441 * If so, ignore it as deleting an address causes 1442 * dhcpcd to drop any lease to which it belongs. 1443 * Also check an added address was really added. 1444 */ 1445 flags = if_addrflags6(ifp, &addr6, NULL); 1446 if (flags == -1) { 1447 if (errno != ENXIO && errno != EADDRNOTAVAIL) 1448 logerr("%s: if_addrflags6", __func__); 1449 if (ifam->ifam_type != RTM_DELADDR) 1450 break; 1451 flags = 0; 1452 } else if (ifam->ifam_type == RTM_DELADDR) 1453 break; 1454 1455 #ifdef __KAME__ 1456 if (IN6_IS_ADDR_LINKLOCAL(&addr6)) 1457 /* Remove the scope from the address */ 1458 addr6.s6_addr[2] = addr6.s6_addr[3] = '\0'; 1459 #endif 1460 1461 ipv6_handleifa(ctx, ifam->ifam_type, NULL, 1462 ifp->name, &addr6, ipv6_prefixlen(&mask6), flags, pid); 1463 break; 1464 } 1465 #endif 1466 } 1467 1468 return 0; 1469 } 1470 1471 static int 1472 if_dispatch(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm) 1473 { 1474 1475 if (rtm->rtm_version != RTM_VERSION) 1476 return 0; 1477 1478 switch(rtm->rtm_type) { 1479 #ifdef RTM_IFANNOUNCE 1480 case RTM_IFANNOUNCE: 1481 return if_announce(ctx, (const void *)rtm); 1482 #endif 1483 case RTM_IFINFO: 1484 return if_ifinfo(ctx, (const void *)rtm); 1485 case RTM_ADD: /* FALLTHROUGH */ 1486 case RTM_CHANGE: /* FALLTHROUGH */ 1487 case RTM_DELETE: /* FALLTHROUGH */ 1488 case RTM_MISS: 1489 return if_rtm(ctx, (const void *)rtm); 1490 #ifdef RTM_CHGADDR 1491 case RTM_CHGADDR: /* FALLTHROUGH */ 1492 #endif 1493 case RTM_DELADDR: /* FALLTHROUGH */ 1494 case RTM_NEWADDR: 1495 return if_ifa(ctx, (const void *)rtm); 1496 #ifdef RTM_DESYNC 1497 case RTM_DESYNC: 1498 dhcpcd_linkoverflow(ctx); 1499 #elif !defined(SO_RERROR) 1500 #warning cannot detect route socket overflow within kernel 1501 #endif 1502 } 1503 1504 return 0; 1505 } 1506 1507 static int 1508 if_missfilter0(struct dhcpcd_ctx *ctx, struct interface *ifp, 1509 struct sockaddr *sa) 1510 { 1511 size_t salen = (size_t)RT_ROUNDUP(sa->sa_len); 1512 size_t newlen = ctx->rt_missfilterlen + salen; 1513 size_t diff = salen - (sa->sa_len); 1514 uint8_t *cp; 1515 1516 if (ctx->rt_missfiltersize < newlen) { 1517 void *n = realloc(ctx->rt_missfilter, newlen); 1518 if (n == NULL) 1519 return -1; 1520 ctx->rt_missfilter = n; 1521 ctx->rt_missfiltersize = newlen; 1522 } 1523 1524 #ifdef INET6 1525 if (sa->sa_family == AF_INET6) 1526 ifa_setscope(satosin6(sa), ifp->index); 1527 #endif 1528 1529 cp = ctx->rt_missfilter + ctx->rt_missfilterlen; 1530 memcpy(cp, sa, sa->sa_len); 1531 if (diff != 0) 1532 memset(cp + sa->sa_len, 0, diff); 1533 ctx->rt_missfilterlen += salen; 1534 1535 #ifdef INET6 1536 if (sa->sa_family == AF_INET6) 1537 ifa_setscope(satosin6(sa), 0); 1538 #endif 1539 1540 return 0; 1541 } 1542 1543 int 1544 if_missfilter(struct interface *ifp, struct sockaddr *sa) 1545 { 1546 1547 return if_missfilter0(ifp->ctx, ifp, sa); 1548 } 1549 1550 int 1551 if_missfilter_apply(struct dhcpcd_ctx *ctx) 1552 { 1553 #ifdef RO_MISSFILTER 1554 if (ctx->rt_missfilterlen == 0) { 1555 struct sockaddr sa = { 1556 .sa_family = AF_UNSPEC, 1557 .sa_len = sizeof(sa), 1558 }; 1559 1560 if (if_missfilter0(ctx, NULL, &sa) == -1) 1561 return -1; 1562 } 1563 1564 return setsockopt(ctx->link_fd, PF_ROUTE, RO_MISSFILTER, 1565 ctx->rt_missfilter, (socklen_t)ctx->rt_missfilterlen); 1566 #else 1567 #warning kernel does not support RTM_MISS DST filtering 1568 UNUSED(ctx); 1569 errno = ENOTSUP; 1570 return -1; 1571 #endif 1572 } 1573 1574 __CTASSERT(offsetof(struct rt_msghdr, rtm_msglen) == 0); 1575 int 1576 if_handlelink(struct dhcpcd_ctx *ctx) 1577 { 1578 struct rtm rtm; 1579 ssize_t len; 1580 1581 len = read(ctx->link_fd, &rtm, sizeof(rtm)); 1582 if (len == -1) 1583 return -1; 1584 if (len == 0) 1585 return 0; 1586 if ((size_t)len < sizeof(rtm.hdr.rtm_msglen) || 1587 len != rtm.hdr.rtm_msglen) 1588 { 1589 errno = EINVAL; 1590 return -1; 1591 } 1592 /* 1593 * Coverity thinks that the data could be tainted from here. 1594 * I have no idea how because the length of the data we read 1595 * is guarded by len and checked to match rtm_msglen. 1596 * The issue seems to be related to extracting the addresses 1597 * at the end of the header, but seems to have no issues with the 1598 * equivalent call in if_initrt. 1599 */ 1600 /* coverity[tainted_data] */ 1601 return if_dispatch(ctx, &rtm.hdr); 1602 } 1603 1604 #ifndef SYS_NMLN /* OSX */ 1605 # define SYS_NMLN 256 1606 #endif 1607 #ifndef HW_MACHINE_ARCH 1608 # ifdef HW_MODEL /* OpenBSD */ 1609 # define HW_MACHINE_ARCH HW_MODEL 1610 # endif 1611 #endif 1612 int 1613 if_machinearch(char *str, size_t len) 1614 { 1615 int mib[2] = { CTL_HW, HW_MACHINE_ARCH }; 1616 char march[SYS_NMLN]; 1617 size_t marchlen = sizeof(march); 1618 1619 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), 1620 march, &marchlen, NULL, 0) != 0) 1621 return -1; 1622 return snprintf(str, len, ":%s", march); 1623 } 1624 1625 #ifdef INET6 1626 #if (defined(IPV6CTL_ACCEPT_RTADV) && !defined(ND6_IFF_ACCEPT_RTADV)) || \ 1627 defined(IPV6CTL_USETEMPADDR) || defined(IPV6CTL_TEMPVLTIME) || \ 1628 defined(IPV6CTL_FORWARDING) 1629 #define get_inet6_sysctl(code) inet6_sysctl(code, 0, 0) 1630 #define set_inet6_sysctl(code, val) inet6_sysctl(code, val, 1) 1631 static int 1632 inet6_sysctl(int code, int val, int action) 1633 { 1634 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 }; 1635 size_t size; 1636 1637 mib[3] = code; 1638 size = sizeof(val); 1639 if (action) { 1640 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), 1641 NULL, 0, &val, size) == -1) 1642 return -1; 1643 return 0; 1644 } 1645 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &val, &size, NULL, 0) == -1) 1646 return -1; 1647 return val; 1648 } 1649 #endif 1650 1651 int 1652 if_applyra(const struct ra *rap) 1653 { 1654 #ifdef SIOCSIFINFO_IN6 1655 struct in6_ndireq nd = { .ndi.chlim = 0 }; 1656 struct dhcpcd_ctx *ctx = rap->iface->ctx; 1657 struct priv *priv = ctx->priv; 1658 int error; 1659 1660 strlcpy(nd.ifname, rap->iface->name, sizeof(nd.ifname)); 1661 if (ioctl(priv->pf_inet6_fd, SIOCGIFINFO_IN6, &nd, sizeof(nd)) == -1) 1662 return -1; 1663 1664 nd.ndi.linkmtu = rap->mtu; 1665 nd.ndi.chlim = rap->hoplimit; 1666 nd.ndi.retrans = rap->retrans; 1667 nd.ndi.basereachable = rap->reachable; 1668 error = if_ioctl6(ctx, SIOCSIFINFO_IN6, &nd, sizeof(nd)); 1669 if (error == -1 && errno == EINVAL) { 1670 /* 1671 * Very likely that this is caused by a dodgy MTU 1672 * setting specific to the interface. 1673 * Let's set it to "unspecified" and try again. 1674 * Doesn't really matter as we fix the MTU against the 1675 * routes we add as not all OS support SIOCSIFINFO_IN6. 1676 */ 1677 nd.ndi.linkmtu = 0; 1678 error = if_ioctl6(ctx, SIOCSIFINFO_IN6, &nd, sizeof(nd)); 1679 } 1680 return error; 1681 #else 1682 #warning OS does not allow setting of RA bits hoplimit, retrans or reachable 1683 UNUSED(rap); 1684 return 0; 1685 #endif 1686 } 1687 1688 #ifdef IPV6_MANAGETEMPADDR 1689 #if !defined(IPV6CTL_TEMPVLTIME) && !defined(__OpenBSD__) 1690 #define get_inet6_sysctlbyname(code) inet6_sysctlbyname(code, 0, 0) 1691 #define set_inet6_sysctlbyname(code, val) inet6_sysctlbyname(code, val, 1) 1692 static int 1693 inet6_sysctlbyname(const char *name, int val, int action) 1694 { 1695 size_t size; 1696 1697 size = sizeof(val); 1698 if (action) { 1699 if (sysctlbyname(name, NULL, 0, &val, size) == -1) 1700 return -1; 1701 return 0; 1702 } 1703 if (sysctlbyname(name, &val, &size, NULL, 0) == -1) 1704 return -1; 1705 return val; 1706 } 1707 #endif 1708 1709 #ifdef __OpenBSD__ 1710 int 1711 ip6_use_tempaddr(const char *ifname) 1712 { 1713 int s, r; 1714 struct ifreq ifr; 1715 1716 s = socket(PF_INET6, SOCK_DGRAM, 0); /* XXX Not efficient */ 1717 if (s == -1) 1718 return -1; 1719 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 1720 r = ioctl(s, SIOCGIFXFLAGS, &ifr); 1721 close(s); 1722 if (r == -1) 1723 return -1; 1724 return ifr.ifr_flags & IFXF_INET6_NOPRIVACY ? 0 : 1; 1725 } 1726 1727 int 1728 ip6_temp_preferred_lifetime(__unused const char *ifname) 1729 { 1730 1731 return ND6_PRIV_PREFERRED_LIFETIME; 1732 } 1733 1734 int 1735 ip6_temp_valid_lifetime(__unused const char *ifname) 1736 { 1737 1738 return ND6_PRIV_VALID_LIFETIME; 1739 } 1740 1741 #else /* __OpenBSD__ */ 1742 1743 int 1744 ip6_use_tempaddr(__unused const char *ifname) 1745 { 1746 int val; 1747 1748 #ifdef IPV6CTL_USETEMPADDR 1749 val = get_inet6_sysctl(IPV6CTL_USETEMPADDR); 1750 #else 1751 val = get_inet6_sysctlbyname("net.inet6.ip6.use_tempaddr"); 1752 #endif 1753 return val == -1 ? 0 : val; 1754 } 1755 1756 int 1757 ip6_temp_preferred_lifetime(__unused const char *ifname) 1758 { 1759 int val; 1760 1761 #ifdef IPV6CTL_TEMPPLTIME 1762 val = get_inet6_sysctl(IPV6CTL_TEMPPLTIME); 1763 #else 1764 val = get_inet6_sysctlbyname("net.inet6.ip6.temppltime"); 1765 #endif 1766 return val < 0 ? TEMP_PREFERRED_LIFETIME : val; 1767 } 1768 1769 int 1770 ip6_temp_valid_lifetime(__unused const char *ifname) 1771 { 1772 int val; 1773 1774 #ifdef IPV6CTL_TEMPVLTIME 1775 val = get_inet6_sysctl(IPV6CTL_TEMPVLTIME); 1776 #else 1777 val = get_inet6_sysctlbyname("net.inet6.ip6.tempvltime"); 1778 #endif 1779 return val < 0 ? TEMP_VALID_LIFETIME : val; 1780 } 1781 #endif /* !__OpenBSD__ */ 1782 #endif 1783 1784 int 1785 ip6_forwarding(__unused const char *ifname) 1786 { 1787 int val; 1788 1789 #ifdef IPV6CTL_FORWARDING 1790 val = get_inet6_sysctl(IPV6CTL_FORWARDING); 1791 #else 1792 val = get_inet6_sysctlbyname("net.inet6.ip6.forwarding"); 1793 #endif 1794 return val < 0 ? 0 : val; 1795 } 1796 1797 #ifdef SIOCIFAFATTACH 1798 static int 1799 if_af_attach(const struct interface *ifp, int af) 1800 { 1801 struct if_afreq ifar; 1802 1803 strlcpy(ifar.ifar_name, ifp->name, sizeof(ifar.ifar_name)); 1804 ifar.ifar_af = af; 1805 return if_ioctl6(ifp->ctx, SIOCIFAFATTACH, &ifar, sizeof(ifar)); 1806 } 1807 #endif 1808 1809 #ifdef SIOCGIFXFLAGS 1810 static int 1811 if_set_ifxflags(const struct interface *ifp) 1812 { 1813 struct ifreq ifr; 1814 int flags; 1815 struct priv *priv = ifp->ctx->priv; 1816 1817 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name)); 1818 if (ioctl(priv->pf_inet6_fd, SIOCGIFXFLAGS, &ifr) == -1) 1819 return -1; 1820 flags = ifr.ifr_flags; 1821 #ifdef IFXF_NOINET6 1822 flags &= ~IFXF_NOINET6; 1823 #endif 1824 /* 1825 * If not doing autoconf, don't disable the kernel from doing it. 1826 * If we need to, we should have another option actively disable it. 1827 * 1828 * OpenBSD moved from kernel based SLAAC to userland via slaacd(8). 1829 * It has a similar featureset to dhcpcd such as stable private 1830 * addresses, but lacks the ability to handle DNS inside the RA 1831 * which is a serious shortfall in this day and age. 1832 * Appease their user base by working alongside slaacd(8) if 1833 * dhcpcd is instructed not to do auto configuration of addresses. 1834 */ 1835 #if defined(ND6_IFF_ACCEPT_RTADV) 1836 #define BSD_AUTOCONF DHCPCD_IPV6RS 1837 #else 1838 #define BSD_AUTOCONF DHCPCD_IPV6RA_AUTOCONF 1839 #endif 1840 if (ifp->options->options & BSD_AUTOCONF) 1841 flags &= ~IFXF_AUTOCONF6; 1842 if (ifr.ifr_flags == flags) 1843 return 0; 1844 ifr.ifr_flags = flags; 1845 return if_ioctl6(ifp->ctx, SIOCSIFXFLAGS, &ifr, sizeof(ifr)); 1846 } 1847 #endif 1848 1849 /* OpenBSD removed ND6 flags entirely, so we need to check for their 1850 * existance. */ 1851 #if defined(ND6_IFF_AUTO_LINKLOCAL) || \ 1852 defined(ND6_IFF_PERFORMNUD) || \ 1853 defined(ND6_IFF_ACCEPT_RTADV) || \ 1854 defined(ND6_IFF_OVERRIDE_RTADV) || \ 1855 defined(ND6_IFF_IFDISABLED) 1856 #define ND6_NDI_FLAGS 1857 #endif 1858 1859 void 1860 if_disable_rtadv(void) 1861 { 1862 #if defined(IPV6CTL_ACCEPT_RTADV) && !defined(ND6_IFF_ACCEPT_RTADV) 1863 int ra = get_inet6_sysctl(IPV6CTL_ACCEPT_RTADV); 1864 1865 if (ra == -1) { 1866 if (errno != ENOENT) 1867 logerr("IPV6CTL_ACCEPT_RTADV"); 1868 else if (ra != 0) 1869 if (set_inet6_sysctl(IPV6CTL_ACCEPT_RTADV, 0) == -1) 1870 logerr("IPV6CTL_ACCEPT_RTADV"); 1871 } 1872 #endif 1873 } 1874 1875 void 1876 if_setup_inet6(const struct interface *ifp) 1877 { 1878 struct priv *priv; 1879 int s; 1880 #ifdef ND6_NDI_FLAGS 1881 struct in6_ndireq nd; 1882 int flags; 1883 #endif 1884 1885 priv = (struct priv *)ifp->ctx->priv; 1886 s = priv->pf_inet6_fd; 1887 1888 #ifdef ND6_NDI_FLAGS 1889 memset(&nd, 0, sizeof(nd)); 1890 strlcpy(nd.ifname, ifp->name, sizeof(nd.ifname)); 1891 if (ioctl(s, SIOCGIFINFO_IN6, &nd) == -1) 1892 logerr("%s: SIOCGIFINFO_FLAGS", ifp->name); 1893 flags = (int)nd.ndi.flags; 1894 #endif 1895 1896 #ifdef ND6_IFF_AUTO_LINKLOCAL 1897 /* Unlike the kernel, 1898 * dhcpcd make make a stable private address. */ 1899 flags &= ~ND6_IFF_AUTO_LINKLOCAL; 1900 #endif 1901 1902 #ifdef ND6_IFF_PERFORMNUD 1903 /* NUD is kind of essential. */ 1904 flags |= ND6_IFF_PERFORMNUD; 1905 #endif 1906 1907 #ifdef ND6_IFF_IFDISABLED 1908 /* Ensure the interface is not disabled. */ 1909 flags &= ~ND6_IFF_IFDISABLED; 1910 #endif 1911 1912 /* 1913 * If not doing autoconf, don't disable the kernel from doing it. 1914 * If we need to, we should have another option actively disable it. 1915 */ 1916 #ifdef ND6_IFF_ACCEPT_RTADV 1917 if (ifp->options->options & DHCPCD_IPV6RS) 1918 flags &= ~ND6_IFF_ACCEPT_RTADV; 1919 #ifdef ND6_IFF_OVERRIDE_RTADV 1920 if (ifp->options->options & DHCPCD_IPV6RS) 1921 flags |= ND6_IFF_OVERRIDE_RTADV; 1922 #endif 1923 #endif 1924 1925 #ifdef ND6_NDI_FLAGS 1926 if (nd.ndi.flags != (uint32_t)flags) { 1927 nd.ndi.flags = (uint32_t)flags; 1928 if (if_ioctl6(ifp->ctx, SIOCSIFINFO_FLAGS, 1929 &nd, sizeof(nd)) == -1) 1930 logerr("%s: SIOCSIFINFO_FLAGS", ifp->name); 1931 } 1932 #endif 1933 1934 /* Enabling IPv6 by whatever means must be the 1935 * last action undertaken to ensure kernel RS and 1936 * LLADDR auto configuration are disabled where applicable. */ 1937 #ifdef SIOCIFAFATTACH 1938 if (if_af_attach(ifp, AF_INET6) == -1) 1939 logerr("%s: if_af_attach", ifp->name); 1940 #endif 1941 1942 #ifdef SIOCGIFXFLAGS 1943 if (if_set_ifxflags(ifp) == -1) 1944 logerr("%s: set_ifxflags", ifp->name); 1945 #endif 1946 1947 #if defined(IPV6CTL_ACCEPT_RTADV) || defined(ND6_IFF_ACCEPT_RTADV) 1948 /* Flush the kernel knowledge of advertised routers 1949 * and prefixes so the kernel does not expire prefixes 1950 * and default routes we are trying to own. */ 1951 if (ifp->options->options & DHCPCD_IPV6RS) { 1952 struct in6_ifreq ifr; 1953 1954 memset(&ifr, 0, sizeof(ifr)); 1955 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name)); 1956 if (if_ioctl6(ifp->ctx, SIOCSRTRFLUSH_IN6, 1957 &ifr, sizeof(ifr)) == -1 && 1958 errno != ENOTSUP) 1959 logwarn("SIOCSRTRFLUSH_IN6"); 1960 if (if_ioctl6(ifp->ctx, SIOCSPFXFLUSH_IN6, 1961 &ifr, sizeof(ifr)) == -1 && 1962 errno != ENOTSUP) 1963 logwarn("SIOCSPFXFLUSH_IN6"); 1964 } 1965 #endif 1966 } 1967 #endif 1968