1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include "opt_netlink.h" 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 #include "opt_inet.h" 33 #include "opt_inet6.h" 34 #include <sys/types.h> 35 #include <sys/eventhandler.h> 36 #include <sys/kernel.h> 37 #include <sys/jail.h> 38 #include <sys/malloc.h> 39 #include <sys/socket.h> 40 #include <sys/sockio.h> 41 #include <sys/syslog.h> 42 43 #include <net/if.h> 44 #include <net/if_dl.h> 45 #include <net/if_media.h> 46 #include <net/if_var.h> 47 #include <net/if_clone.h> 48 #include <net/route.h> 49 #include <net/route/nhop.h> 50 #include <net/route/route_ctl.h> 51 #include <netinet/in_var.h> 52 #include <netinet6/in6_var.h> 53 #include <netinet6/scope6_var.h> /* scope deembedding */ 54 #include <netlink/netlink.h> 55 #include <netlink/netlink_ctl.h> 56 #include <netlink/netlink_route.h> 57 #include <netlink/route/route_var.h> 58 59 #define DEBUG_MOD_NAME nl_iface 60 #define DEBUG_MAX_LEVEL LOG_DEBUG3 61 #include <netlink/netlink_debug.h> 62 _DECLARE_DEBUG(LOG_INFO); 63 64 struct netlink_walkargs { 65 struct nl_writer *nw; 66 struct nlmsghdr hdr; 67 struct nlpcb *so; 68 struct ucred *cred; 69 uint32_t fibnum; 70 int family; 71 int error; 72 int count; 73 int dumped; 74 }; 75 76 static eventhandler_tag ifdetach_event, ifattach_event, iflink_event, ifaddr_event; 77 78 static SLIST_HEAD(, nl_cloner) nl_cloners = SLIST_HEAD_INITIALIZER(nl_cloners); 79 80 static struct sx rtnl_cloner_lock; 81 SX_SYSINIT(rtnl_cloner_lock, &rtnl_cloner_lock, "rtnl cloner lock"); 82 83 /* These are external hooks for CARP. */ 84 extern int (*carp_get_vhid_p)(struct ifaddr *); 85 86 /* 87 * RTM_GETLINK request 88 * sendto(3, {{len=32, type=RTM_GETLINK, flags=NLM_F_REQUEST|NLM_F_DUMP, seq=1641940952, pid=0}, 89 * {ifi_family=AF_INET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}}, 32, 0, NULL, 0) = 32 90 * 91 * Reply: 92 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_ETHER, ifi_index=if_nametoindex("enp0s31f6"), ifi_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST|IFF_LOWER_UP, ifi_change=0}, 93 {{nla_len=10, nla_type=IFLA_ADDRESS}, "\xfe\x54\x00\x52\x3e\x90"} 94 95 [ 96 {{nla_len=14, nla_type=IFLA_IFNAME}, "enp0s31f6"}, 97 {{nla_len=8, nla_type=IFLA_TXQLEN}, 1000}, 98 {{nla_len=5, nla_type=IFLA_OPERSTATE}, 6}, 99 {{nla_len=5, nla_type=IFLA_LINKMODE}, 0}, 100 {{nla_len=8, nla_type=IFLA_MTU}, 1500}, 101 {{nla_len=8, nla_type=IFLA_MIN_MTU}, 68}, 102 {{nla_len=8, nla_type=IFLA_MAX_MTU}, 9000}, 103 {{nla_len=8, nla_type=IFLA_GROUP}, 0}, 104 {{nla_len=8, nla_type=IFLA_PROMISCUITY}, 0}, 105 {{nla_len=8, nla_type=IFLA_NUM_TX_QUEUES}, 1}, 106 {{nla_len=8, nla_type=IFLA_GSO_MAX_SEGS}, 65535}, 107 {{nla_len=8, nla_type=IFLA_GSO_MAX_SIZE}, 65536}, 108 {{nla_len=8, nla_type=IFLA_NUM_RX_QUEUES}, 1}, 109 {{nla_len=5, nla_type=IFLA_CARRIER}, 1}, 110 {{nla_len=13, nla_type=IFLA_QDISC}, "fq_codel"}, 111 {{nla_len=8, nla_type=IFLA_CARRIER_CHANGES}, 2}, 112 {{nla_len=5, nla_type=IFLA_PROTO_DOWN}, 0}, 113 {{nla_len=8, nla_type=IFLA_CARRIER_UP_COUNT}, 1}, 114 {{nla_len=8, nla_type=IFLA_CARRIER_DOWN_COUNT}, 1}, 115 */ 116 117 struct if_state { 118 uint8_t ifla_operstate; 119 uint8_t ifla_carrier; 120 }; 121 122 static void 123 get_operstate_ether(struct ifnet *ifp, struct if_state *pstate) 124 { 125 struct ifmediareq ifmr = {}; 126 int error; 127 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (void *)&ifmr); 128 129 if (error != 0) { 130 NL_LOG(LOG_DEBUG, "error calling SIOCGIFMEDIA on %s: %d", 131 if_name(ifp), error); 132 return; 133 } 134 135 switch (IFM_TYPE(ifmr.ifm_active)) { 136 case IFM_ETHER: 137 if (ifmr.ifm_status & IFM_ACTIVE) { 138 pstate->ifla_carrier = 1; 139 if (ifp->if_flags & IFF_MONITOR) 140 pstate->ifla_operstate = IF_OPER_DORMANT; 141 else 142 pstate->ifla_operstate = IF_OPER_UP; 143 } else 144 pstate->ifla_operstate = IF_OPER_DOWN; 145 } 146 } 147 148 static bool 149 get_stats(struct nl_writer *nw, struct ifnet *ifp) 150 { 151 struct rtnl_link_stats64 *stats; 152 153 int nla_len = sizeof(struct nlattr) + sizeof(*stats); 154 struct nlattr *nla = nlmsg_reserve_data(nw, nla_len, struct nlattr); 155 if (nla == NULL) 156 return (false); 157 nla->nla_type = IFLA_STATS64; 158 nla->nla_len = nla_len; 159 stats = (struct rtnl_link_stats64 *)(nla + 1); 160 161 stats->rx_packets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS); 162 stats->tx_packets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS); 163 stats->rx_bytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES); 164 stats->tx_bytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES); 165 stats->rx_errors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS); 166 stats->tx_errors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS); 167 stats->rx_dropped = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS); 168 stats->tx_dropped = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS); 169 stats->multicast = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS); 170 stats->rx_nohandler = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO); 171 172 return (true); 173 } 174 175 static void 176 get_operstate(struct ifnet *ifp, struct if_state *pstate) 177 { 178 pstate->ifla_operstate = IF_OPER_UNKNOWN; 179 pstate->ifla_carrier = 0; /* no carrier */ 180 181 switch (ifp->if_type) { 182 case IFT_ETHER: 183 case IFT_L2VLAN: 184 get_operstate_ether(ifp, pstate); 185 break; 186 default: 187 /* Map admin state to the operstate */ 188 if (ifp->if_flags & IFF_UP) { 189 pstate->ifla_operstate = IF_OPER_UP; 190 pstate->ifla_carrier = 1; 191 } else 192 pstate->ifla_operstate = IF_OPER_DOWN; 193 break; 194 } 195 } 196 197 static void 198 get_hwaddr(struct nl_writer *nw, struct ifnet *ifp) 199 { 200 struct ifreq ifr = {}; 201 202 if (if_gethwaddr(ifp, &ifr) == 0) { 203 nlattr_add(nw, IFLAF_ORIG_HWADDR, if_getaddrlen(ifp), 204 ifr.ifr_addr.sa_data); 205 } 206 } 207 208 static unsigned 209 ifp_flags_to_netlink(const struct ifnet *ifp) 210 { 211 return (ifp->if_flags | ifp->if_drv_flags); 212 } 213 214 #define LLADDR_CONST(s) ((const void *)((s)->sdl_data + (s)->sdl_nlen)) 215 static bool 216 dump_sa(struct nl_writer *nw, int attr, const struct sockaddr *sa) 217 { 218 uint32_t addr_len = 0; 219 const void *addr_data = NULL; 220 #ifdef INET6 221 struct in6_addr addr6; 222 #endif 223 224 if (sa == NULL) 225 return (true); 226 227 switch (sa->sa_family) { 228 #ifdef INET 229 case AF_INET: 230 addr_len = sizeof(struct in_addr); 231 addr_data = &((const struct sockaddr_in *)sa)->sin_addr; 232 break; 233 #endif 234 #ifdef INET6 235 case AF_INET6: 236 in6_splitscope(&((const struct sockaddr_in6 *)sa)->sin6_addr, &addr6, &addr_len); 237 addr_len = sizeof(struct in6_addr); 238 addr_data = &addr6; 239 break; 240 #endif 241 case AF_LINK: 242 addr_len = ((const struct sockaddr_dl *)sa)->sdl_alen; 243 addr_data = LLADDR_CONST((const struct sockaddr_dl *)sa); 244 break; 245 case AF_UNSPEC: 246 /* Ignore empty SAs without warning */ 247 return (true); 248 default: 249 NL_LOG(LOG_DEBUG2, "unsupported family: %d, skipping", sa->sa_family); 250 return (true); 251 } 252 253 return (nlattr_add(nw, attr, addr_len, addr_data)); 254 } 255 256 static bool 257 dump_iface_caps(struct nl_writer *nw, struct ifnet *ifp) 258 { 259 int off = nlattr_add_nested(nw, IFLAF_CAPS); 260 uint32_t active_caps[roundup2(IFCAP_B_SIZE, 32) / 32] = {}; 261 uint32_t all_caps[roundup2(IFCAP_B_SIZE, 32) / 32] = {}; 262 263 MPASS(sizeof(active_caps) >= 8); 264 MPASS(sizeof(all_caps) >= 8); 265 266 if (off == 0) 267 return (false); 268 269 active_caps[0] = (uint32_t)if_getcapabilities(ifp); 270 all_caps[0] = (uint32_t)if_getcapenable(ifp); 271 active_caps[1] = (uint32_t)if_getcapabilities2(ifp); 272 all_caps[1] = (uint32_t)if_getcapenable2(ifp); 273 274 nlattr_add_u32(nw, NLA_BITSET_SIZE, IFCAP_B_SIZE); 275 nlattr_add(nw, NLA_BITSET_MASK, sizeof(all_caps), all_caps); 276 nlattr_add(nw, NLA_BITSET_VALUE, sizeof(active_caps), active_caps); 277 278 nlattr_set_len(nw, off); 279 280 return (true); 281 } 282 283 /* 284 * Dumps interface state, properties and metrics. 285 * @nw: message writer 286 * @ifp: target interface 287 * @hdr: template header 288 * @if_flags_mask: changed if_[drv]_flags bitmask 289 * 290 * This function is called without epoch and MAY sleep. 291 */ 292 static bool 293 dump_iface(struct nl_writer *nw, struct ifnet *ifp, const struct nlmsghdr *hdr, 294 int if_flags_mask) 295 { 296 struct ifinfomsg *ifinfo; 297 298 NL_LOG(LOG_DEBUG3, "dumping interface %s data", if_name(ifp)); 299 300 if (!nlmsg_reply(nw, hdr, sizeof(struct ifinfomsg))) 301 goto enomem; 302 303 ifinfo = nlmsg_reserve_object(nw, struct ifinfomsg); 304 ifinfo->ifi_family = AF_UNSPEC; 305 ifinfo->__ifi_pad = 0; 306 ifinfo->ifi_type = ifp->if_type; 307 ifinfo->ifi_index = ifp->if_index; 308 ifinfo->ifi_flags = ifp_flags_to_netlink(ifp); 309 ifinfo->ifi_change = if_flags_mask; 310 311 struct if_state ifs = {}; 312 get_operstate(ifp, &ifs); 313 314 if (ifs.ifla_operstate == IF_OPER_UP) 315 ifinfo->ifi_flags |= IFF_LOWER_UP; 316 317 nlattr_add_string(nw, IFLA_IFNAME, if_name(ifp)); 318 nlattr_add_u8(nw, IFLA_OPERSTATE, ifs.ifla_operstate); 319 nlattr_add_u8(nw, IFLA_CARRIER, ifs.ifla_carrier); 320 321 /* 322 nlattr_add_u8(nw, IFLA_PROTO_DOWN, val); 323 nlattr_add_u8(nw, IFLA_LINKMODE, val); 324 */ 325 if (if_getaddrlen(ifp) != 0) { 326 struct ifaddr *ifa = if_getifaddr(ifp); 327 328 dump_sa(nw, IFLA_ADDRESS, ifa->ifa_addr); 329 } 330 331 if ((ifp->if_broadcastaddr != NULL)) { 332 nlattr_add(nw, IFLA_BROADCAST, ifp->if_addrlen, 333 ifp->if_broadcastaddr); 334 } 335 336 nlattr_add_u32(nw, IFLA_MTU, ifp->if_mtu); 337 /* 338 nlattr_add_u32(nw, IFLA_MIN_MTU, 60); 339 nlattr_add_u32(nw, IFLA_MAX_MTU, 9000); 340 nlattr_add_u32(nw, IFLA_GROUP, 0); 341 */ 342 343 if (ifp->if_description != NULL) 344 nlattr_add_string(nw, IFLA_IFALIAS, ifp->if_description); 345 346 /* Store FreeBSD-specific attributes */ 347 int off = nlattr_add_nested(nw, IFLA_FREEBSD); 348 if (off != 0) { 349 get_hwaddr(nw, ifp); 350 dump_iface_caps(nw, ifp); 351 352 nlattr_set_len(nw, off); 353 } 354 355 get_stats(nw, ifp); 356 357 uint32_t val = (ifp->if_flags & IFF_PROMISC) != 0; 358 nlattr_add_u32(nw, IFLA_PROMISCUITY, val); 359 360 ifc_dump_ifp_nl(ifp, nw); 361 362 if (nlmsg_end(nw)) 363 return (true); 364 365 enomem: 366 NL_LOG(LOG_DEBUG, "unable to dump interface %s state (ENOMEM)", if_name(ifp)); 367 nlmsg_abort(nw); 368 return (false); 369 } 370 371 static bool 372 check_ifmsg(void *hdr, struct nl_pstate *npt) 373 { 374 struct ifinfomsg *ifm = hdr; 375 376 if (ifm->__ifi_pad != 0 || ifm->ifi_type != 0 || 377 ifm->ifi_flags != 0 || ifm->ifi_change != 0) { 378 nlmsg_report_err_msg(npt, 379 "strict checking: non-zero values in ifinfomsg header"); 380 return (false); 381 } 382 383 return (true); 384 } 385 386 #define _IN(_field) offsetof(struct ifinfomsg, _field) 387 #define _OUT(_field) offsetof(struct nl_parsed_link, _field) 388 static const struct nlfield_parser nlf_p_if[] = { 389 { .off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = nlf_get_u16 }, 390 { .off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = nlf_get_u32 }, 391 { .off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = nlf_get_u32 }, 392 { .off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = nlf_get_u32 }, 393 }; 394 395 static const struct nlattr_parser nla_p_linfo[] = { 396 { .type = IFLA_INFO_KIND, .off = _OUT(ifla_cloner), .cb = nlattr_get_stringn }, 397 { .type = IFLA_INFO_DATA, .off = _OUT(ifla_idata), .cb = nlattr_get_nla }, 398 }; 399 NL_DECLARE_ATTR_PARSER(linfo_parser, nla_p_linfo); 400 401 static const struct nlattr_parser nla_p_if[] = { 402 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string }, 403 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = nlattr_get_uint32 }, 404 { .type = IFLA_LINK, .off = _OUT(ifla_link), .cb = nlattr_get_uint32 }, 405 { .type = IFLA_LINKINFO, .arg = &linfo_parser, .cb = nlattr_get_nested }, 406 { .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = nlattr_get_string }, 407 { .type = IFLA_GROUP, .off = _OUT(ifla_group), .cb = nlattr_get_string }, 408 { .type = IFLA_ALT_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string }, 409 }; 410 #undef _IN 411 #undef _OUT 412 NL_DECLARE_STRICT_PARSER(ifmsg_parser, struct ifinfomsg, check_ifmsg, nlf_p_if, nla_p_if); 413 414 static bool 415 match_iface(struct ifnet *ifp, void *_arg) 416 { 417 struct nl_parsed_link *attrs = (struct nl_parsed_link *)_arg; 418 419 if (attrs->ifi_index != 0 && attrs->ifi_index != ifp->if_index) 420 return (false); 421 if (attrs->ifi_type != 0 && attrs->ifi_index != ifp->if_type) 422 return (false); 423 if (attrs->ifla_ifname != NULL && strcmp(attrs->ifla_ifname, if_name(ifp))) 424 return (false); 425 /* TODO: add group match */ 426 427 return (true); 428 } 429 430 static int 431 dump_cb(struct ifnet *ifp, void *_arg) 432 { 433 struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg; 434 if (!dump_iface(wa->nw, ifp, &wa->hdr, 0)) 435 return (ENOMEM); 436 return (0); 437 } 438 439 /* 440 * {nlmsg_len=52, nlmsg_type=RTM_GETLINK, nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=1662842818, nlmsg_pid=0}, 441 * {ifi_family=AF_PACKET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}, 442 * [ 443 * [{nla_len=10, nla_type=IFLA_IFNAME}, "vnet9"], 444 * [{nla_len=8, nla_type=IFLA_EXT_MASK}, RTEXT_FILTER_VF] 445 * ] 446 */ 447 static int 448 rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt) 449 { 450 struct epoch_tracker et; 451 struct ifnet *ifp; 452 int error = 0; 453 454 struct nl_parsed_link attrs = {}; 455 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs); 456 if (error != 0) 457 return (error); 458 459 struct netlink_walkargs wa = { 460 .so = nlp, 461 .nw = npt->nw, 462 .hdr.nlmsg_pid = hdr->nlmsg_pid, 463 .hdr.nlmsg_seq = hdr->nlmsg_seq, 464 .hdr.nlmsg_flags = hdr->nlmsg_flags, 465 .hdr.nlmsg_type = NL_RTM_NEWLINK, 466 }; 467 468 /* Fast track for an interface w/ explicit name or index match */ 469 if ((attrs.ifi_index != 0) || (attrs.ifla_ifname != NULL)) { 470 if (attrs.ifi_index != 0) { 471 NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching index %u", 472 attrs.ifi_index); 473 NET_EPOCH_ENTER(et); 474 ifp = ifnet_byindex_ref(attrs.ifi_index); 475 NET_EPOCH_EXIT(et); 476 } else { 477 NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching name %s", 478 attrs.ifla_ifname); 479 ifp = ifunit_ref(attrs.ifla_ifname); 480 } 481 482 if (ifp != NULL) { 483 if (match_iface(ifp, &attrs)) { 484 if (!dump_iface(wa.nw, ifp, &wa.hdr, 0)) 485 error = ENOMEM; 486 } else 487 error = ENODEV; 488 if_rele(ifp); 489 } else 490 error = ENODEV; 491 return (error); 492 } 493 494 /* Always treat non-direct-match as a multipart message */ 495 wa.hdr.nlmsg_flags |= NLM_F_MULTI; 496 497 /* 498 * Fetching some link properties require performing ioctl's that may be blocking. 499 * Address it by saving referenced pointers of the matching links, 500 * exiting from epoch and going through the list one-by-one. 501 */ 502 503 NL_LOG(LOG_DEBUG2, "Start dump"); 504 if_foreach_sleep(match_iface, &attrs, dump_cb, &wa); 505 NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped); 506 507 if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) { 508 NL_LOG(LOG_DEBUG, "Unable to finalize the dump"); 509 return (ENOMEM); 510 } 511 512 return (error); 513 } 514 515 /* 516 * sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base=[ 517 * {nlmsg_len=60, nlmsg_type=RTM_NEWLINK, nlmsg_flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, nlmsg_seq=1662715618, nlmsg_pid=0}, 518 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}, 519 * {nla_len=11, nla_type=IFLA_IFNAME}, "dummy0"], 520 * [ 521 * {nla_len=16, nla_type=IFLA_LINKINFO}, 522 * [ 523 * {nla_len=9, nla_type=IFLA_INFO_KIND}, "dummy"... 524 * ] 525 * ] 526 */ 527 528 static int 529 rtnl_handle_dellink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt) 530 { 531 struct epoch_tracker et; 532 struct ifnet *ifp; 533 int error; 534 535 struct nl_parsed_link attrs = {}; 536 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs); 537 if (error != 0) 538 return (error); 539 540 NET_EPOCH_ENTER(et); 541 ifp = ifnet_byindex_ref(attrs.ifi_index); 542 NET_EPOCH_EXIT(et); 543 if (ifp == NULL) { 544 NLP_LOG(LOG_DEBUG, nlp, "unable to find interface %u", attrs.ifi_index); 545 return (ENOENT); 546 } 547 NLP_LOG(LOG_DEBUG3, nlp, "mapped ifindex %u to %s", attrs.ifi_index, if_name(ifp)); 548 549 sx_xlock(&ifnet_detach_sxlock); 550 error = if_clone_destroy(if_name(ifp)); 551 sx_xunlock(&ifnet_detach_sxlock); 552 553 NLP_LOG(LOG_DEBUG2, nlp, "deleting interface %s returned %d", if_name(ifp), error); 554 555 if_rele(ifp); 556 return (error); 557 } 558 559 /* 560 * New link: 561 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1668185590, pid=0}, 562 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0} 563 * [ 564 * {{nla_len=8, nla_type=IFLA_MTU}, 123}, 565 * {{nla_len=10, nla_type=IFLA_IFNAME}, "vlan1"}, 566 * {{nla_len=24, nla_type=IFLA_LINKINFO}, 567 * [ 568 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...}, 569 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x7b\x00\x00\x00"}]}]} 570 * 571 * Update link: 572 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=1668185923, pid=0}, 573 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=if_nametoindex("lo"), ifi_flags=0, ifi_change=0}, 574 * {{nla_len=8, nla_type=IFLA_MTU}, 123}} 575 * 576 * 577 * Check command availability: 578 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=0, pid=0}, 579 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0} 580 */ 581 582 583 static int 584 create_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs, 585 struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt) 586 { 587 if (lattrs->ifla_ifname == NULL || strlen(lattrs->ifla_ifname) == 0) { 588 NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_IFNAME attribute"); 589 return (EINVAL); 590 } 591 if (lattrs->ifla_cloner == NULL || strlen(lattrs->ifla_cloner) == 0) { 592 NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_INFO_KIND attribute"); 593 return (EINVAL); 594 } 595 596 struct ifc_data_nl ifd = { 597 .flags = IFC_F_CREATE, 598 .lattrs = lattrs, 599 .bm = bm, 600 .npt = npt, 601 }; 602 if (ifc_create_ifp_nl(lattrs->ifla_ifname, &ifd) && ifd.error == 0) 603 nl_store_ifp_cookie(npt, ifd.ifp); 604 605 return (ifd.error); 606 } 607 608 static int 609 modify_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs, 610 struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt) 611 { 612 struct ifnet *ifp = NULL; 613 struct epoch_tracker et; 614 615 if (lattrs->ifi_index == 0 && lattrs->ifla_ifname == NULL) { 616 /* 617 * Applications like ip(8) verify RTM_NEWLINK command 618 * existence by calling it with empty arguments. Always 619 * return "innocent" error in that case. 620 */ 621 NLMSG_REPORT_ERR_MSG(npt, "empty ifi_index field"); 622 return (EPERM); 623 } 624 625 if (lattrs->ifi_index != 0) { 626 NET_EPOCH_ENTER(et); 627 ifp = ifnet_byindex_ref(lattrs->ifi_index); 628 NET_EPOCH_EXIT(et); 629 if (ifp == NULL) { 630 NLMSG_REPORT_ERR_MSG(npt, "unable to find interface #%u", 631 lattrs->ifi_index); 632 return (ENOENT); 633 } 634 } 635 636 if (ifp == NULL && lattrs->ifla_ifname != NULL) { 637 ifp = ifunit_ref(lattrs->ifla_ifname); 638 if (ifp == NULL) { 639 NLMSG_REPORT_ERR_MSG(npt, "unable to find interface %s", 640 lattrs->ifla_ifname); 641 return (ENOENT); 642 } 643 } 644 645 MPASS(ifp != NULL); 646 647 /* 648 * Modification request can address either 649 * 1) cloned interface, in which case we call the cloner-specific 650 * modification routine 651 * or 652 * 2) non-cloned (e.g. "physical") interface, in which case we call 653 * generic modification routine 654 */ 655 struct ifc_data_nl ifd = { .lattrs = lattrs, .bm = bm, .npt = npt }; 656 if (!ifc_modify_ifp_nl(ifp, &ifd)) 657 ifd.error = nl_modify_ifp_generic(ifp, lattrs, bm, npt); 658 659 if_rele(ifp); 660 661 return (ifd.error); 662 } 663 664 665 static int 666 rtnl_handle_newlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt) 667 { 668 struct nlattr_bmask bm; 669 int error; 670 671 struct nl_parsed_link attrs = {}; 672 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs); 673 if (error != 0) 674 return (error); 675 nl_get_attrs_bmask_nlmsg(hdr, &ifmsg_parser, &bm); 676 677 if (hdr->nlmsg_flags & NLM_F_CREATE) 678 return (create_link(hdr, &attrs, &bm, nlp, npt)); 679 else 680 return (modify_link(hdr, &attrs, &bm, nlp, npt)); 681 } 682 683 static void 684 set_scope6(struct sockaddr *sa, uint32_t ifindex) 685 { 686 #ifdef INET6 687 if (sa != NULL && sa->sa_family == AF_INET6) { 688 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; 689 690 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) 691 in6_set_unicast_scopeid(&sa6->sin6_addr, ifindex); 692 } 693 #endif 694 } 695 696 static bool 697 check_sa_family(const struct sockaddr *sa, int family, const char *attr_name, 698 struct nl_pstate *npt) 699 { 700 if (sa == NULL || sa->sa_family == family) 701 return (true); 702 703 nlmsg_report_err_msg(npt, "wrong family for %s attribute: %d != %d", 704 attr_name, family, sa->sa_family); 705 return (false); 706 } 707 708 struct nl_parsed_ifa { 709 uint8_t ifa_family; 710 uint8_t ifa_prefixlen; 711 uint8_t ifa_scope; 712 uint32_t ifa_index; 713 uint32_t ifa_flags; 714 uint32_t ifaf_vhid; 715 uint32_t ifaf_flags; 716 struct sockaddr *ifa_address; 717 struct sockaddr *ifa_local; 718 struct sockaddr *ifa_broadcast; 719 struct ifa_cacheinfo *ifa_cacheinfo; 720 struct sockaddr *f_ifa_addr; 721 struct sockaddr *f_ifa_dst; 722 }; 723 724 static int 725 nlattr_get_cinfo(struct nlattr *nla, struct nl_pstate *npt, 726 const void *arg __unused, void *target) 727 { 728 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(struct ifa_cacheinfo))) { 729 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not ifa_cacheinfo", 730 nla->nla_type, NLA_DATA_LEN(nla)); 731 return (EINVAL); 732 } 733 *((struct ifa_cacheinfo **)target) = (struct ifa_cacheinfo *)NL_RTA_DATA(nla); 734 return (0); 735 } 736 737 #define _IN(_field) offsetof(struct ifaddrmsg, _field) 738 #define _OUT(_field) offsetof(struct nl_parsed_ifa, _field) 739 static const struct nlfield_parser nlf_p_ifa[] = { 740 { .off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = nlf_get_u8 }, 741 { .off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = nlf_get_u8 }, 742 { .off_in = _IN(ifa_scope), .off_out = _OUT(ifa_scope), .cb = nlf_get_u8 }, 743 { .off_in = _IN(ifa_flags), .off_out = _OUT(ifa_flags), .cb = nlf_get_u8_u32 }, 744 { .off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = nlf_get_u32 }, 745 }; 746 747 static const struct nlattr_parser nla_p_ifa_fbsd[] = { 748 { .type = IFAF_VHID, .off = _OUT(ifaf_vhid), .cb = nlattr_get_uint32 }, 749 { .type = IFAF_FLAGS, .off = _OUT(ifaf_flags), .cb = nlattr_get_uint32 }, 750 }; 751 NL_DECLARE_ATTR_PARSER(ifa_fbsd_parser, nla_p_ifa_fbsd); 752 753 static const struct nlattr_parser nla_p_ifa[] = { 754 { .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = nlattr_get_ip }, 755 { .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = nlattr_get_ip }, 756 { .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = nlattr_get_ip }, 757 { .type = IFA_CACHEINFO, .off = _OUT(ifa_cacheinfo), .cb = nlattr_get_cinfo }, 758 { .type = IFA_FLAGS, .off = _OUT(ifa_flags), .cb = nlattr_get_uint32 }, 759 { .type = IFA_FREEBSD, .arg = &ifa_fbsd_parser, .cb = nlattr_get_nested }, 760 }; 761 #undef _IN 762 #undef _OUT 763 764 static bool 765 post_p_ifa(void *_attrs, struct nl_pstate *npt) 766 { 767 struct nl_parsed_ifa *attrs = (struct nl_parsed_ifa *)_attrs; 768 769 if (!check_sa_family(attrs->ifa_address, attrs->ifa_family, "IFA_ADDRESS", npt)) 770 return (false); 771 if (!check_sa_family(attrs->ifa_local, attrs->ifa_family, "IFA_LOCAL", npt)) 772 return (false); 773 if (!check_sa_family(attrs->ifa_broadcast, attrs->ifa_family, "IFA_BROADADDR", npt)) 774 return (false); 775 776 set_scope6(attrs->ifa_address, attrs->ifa_index); 777 set_scope6(attrs->ifa_local, attrs->ifa_index); 778 779 return (true); 780 } 781 782 NL_DECLARE_PARSER_EXT(ifa_parser, struct ifaddrmsg, NULL, nlf_p_ifa, nla_p_ifa, post_p_ifa); 783 784 785 /* 786 787 {ifa_family=AF_INET, ifa_prefixlen=8, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_HOST, ifa_index=if_nametoindex("lo")}, 788 [ 789 {{nla_len=8, nla_type=IFA_ADDRESS}, inet_addr("127.0.0.1")}, 790 {{nla_len=8, nla_type=IFA_LOCAL}, inet_addr("127.0.0.1")}, 791 {{nla_len=7, nla_type=IFA_LABEL}, "lo"}, 792 {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT}, 793 {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=3619, tstamp=3619}}]}, 794 --- 795 796 {{len=72, type=RTM_NEWADDR, flags=NLM_F_MULTI, seq=1642191126, pid=566735}, 797 {ifa_family=AF_INET6, ifa_prefixlen=96, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_UNIVERSE, ifa_index=if_nametoindex("virbr0")}, 798 [ 799 {{nla_len=20, nla_type=IFA_ADDRESS}, inet_pton(AF_INET6, "2a01:4f8:13a:70c:ffff::1")}, 800 {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=4283, tstamp=4283}}, 801 {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT}]}, 802 */ 803 804 static uint8_t 805 ifa_get_scope(const struct ifaddr *ifa) 806 { 807 const struct sockaddr *sa; 808 uint8_t addr_scope = RT_SCOPE_UNIVERSE; 809 810 sa = ifa->ifa_addr; 811 switch (sa->sa_family) { 812 #ifdef INET 813 case AF_INET: 814 { 815 struct in_addr addr; 816 addr = ((const struct sockaddr_in *)sa)->sin_addr; 817 if (IN_LOOPBACK(addr.s_addr)) 818 addr_scope = RT_SCOPE_HOST; 819 else if (IN_LINKLOCAL(addr.s_addr)) 820 addr_scope = RT_SCOPE_LINK; 821 break; 822 } 823 #endif 824 #ifdef INET6 825 case AF_INET6: 826 { 827 const struct in6_addr *addr; 828 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr; 829 if (IN6_IS_ADDR_LOOPBACK(addr)) 830 addr_scope = RT_SCOPE_HOST; 831 else if (IN6_IS_ADDR_LINKLOCAL(addr)) 832 addr_scope = RT_SCOPE_LINK; 833 break; 834 } 835 #endif 836 } 837 838 return (addr_scope); 839 } 840 841 #ifdef INET6 842 static uint8_t 843 inet6_get_plen(const struct in6_addr *addr) 844 { 845 846 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) + 847 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3])); 848 } 849 #endif 850 851 static uint8_t 852 get_sa_plen(const struct sockaddr *sa) 853 { 854 #ifdef INET 855 const struct in_addr *paddr; 856 #endif 857 #ifdef INET6 858 const struct in6_addr *paddr6; 859 #endif 860 861 switch (sa->sa_family) { 862 #ifdef INET 863 case AF_INET: 864 paddr = &(((const struct sockaddr_in *)sa)->sin_addr); 865 return bitcount32(paddr->s_addr);; 866 #endif 867 #ifdef INET6 868 case AF_INET6: 869 paddr6 = &(((const struct sockaddr_in6 *)sa)->sin6_addr); 870 return inet6_get_plen(paddr6); 871 #endif 872 } 873 874 return (0); 875 } 876 877 #ifdef INET6 878 static uint32_t 879 in6_flags_to_nl(uint32_t flags) 880 { 881 uint32_t nl_flags = 0; 882 883 if (flags & IN6_IFF_TEMPORARY) 884 nl_flags |= IFA_F_TEMPORARY; 885 if (flags & IN6_IFF_NODAD) 886 nl_flags |= IFA_F_NODAD; 887 if (flags & IN6_IFF_DEPRECATED) 888 nl_flags |= IFA_F_DEPRECATED; 889 if (flags & IN6_IFF_TENTATIVE) 890 nl_flags |= IFA_F_TENTATIVE; 891 if ((flags & (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) == 0) 892 flags |= IFA_F_PERMANENT; 893 if (flags & IN6_IFF_DUPLICATED) 894 flags |= IFA_F_DADFAILED; 895 return (nl_flags); 896 } 897 898 static uint32_t 899 nl_flags_to_in6(uint32_t flags) 900 { 901 uint32_t in6_flags = 0; 902 903 if (flags & IFA_F_TEMPORARY) 904 in6_flags |= IN6_IFF_TEMPORARY; 905 if (flags & IFA_F_NODAD) 906 in6_flags |= IN6_IFF_NODAD; 907 if (flags & IFA_F_DEPRECATED) 908 in6_flags |= IN6_IFF_DEPRECATED; 909 if (flags & IFA_F_TENTATIVE) 910 in6_flags |= IN6_IFF_TENTATIVE; 911 if (flags & IFA_F_DADFAILED) 912 in6_flags |= IN6_IFF_DUPLICATED; 913 914 return (in6_flags); 915 } 916 917 static void 918 export_cache_info6(struct nl_writer *nw, const struct in6_ifaddr *ia) 919 { 920 struct ifa_cacheinfo ci = { 921 .cstamp = ia->ia6_createtime * 1000, 922 .tstamp = ia->ia6_updatetime * 1000, 923 .ifa_prefered = ia->ia6_lifetime.ia6t_pltime, 924 .ifa_valid = ia->ia6_lifetime.ia6t_vltime, 925 }; 926 927 nlattr_add(nw, IFA_CACHEINFO, sizeof(ci), &ci); 928 } 929 #endif 930 931 static void 932 export_cache_info(struct nl_writer *nw, struct ifaddr *ifa) 933 { 934 switch (ifa->ifa_addr->sa_family) { 935 #ifdef INET6 936 case AF_INET6: 937 export_cache_info6(nw, (struct in6_ifaddr *)ifa); 938 break; 939 #endif 940 } 941 } 942 943 /* 944 * {'attrs': [('IFA_ADDRESS', '12.0.0.1'), 945 ('IFA_LOCAL', '12.0.0.1'), 946 ('IFA_LABEL', 'eth10'), 947 ('IFA_FLAGS', 128), 948 ('IFA_CACHEINFO', {'ifa_preferred': 4294967295, 'ifa_valid': 4294967295, 'cstamp': 63745746, 'tstamp': 63745746})], 949 */ 950 static bool 951 dump_iface_addr(struct nl_writer *nw, struct ifnet *ifp, struct ifaddr *ifa, 952 const struct nlmsghdr *hdr) 953 { 954 struct ifaddrmsg *ifamsg; 955 struct sockaddr *sa = ifa->ifa_addr; 956 struct sockaddr *sa_dst = ifa->ifa_dstaddr; 957 958 NL_LOG(LOG_DEBUG3, "dumping ifa %p type %s(%d) for interface %s", 959 ifa, rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp)); 960 961 if (!nlmsg_reply(nw, hdr, sizeof(struct ifaddrmsg))) 962 goto enomem; 963 964 ifamsg = nlmsg_reserve_object(nw, struct ifaddrmsg); 965 ifamsg->ifa_family = sa->sa_family; 966 ifamsg->ifa_prefixlen = get_sa_plen(ifa->ifa_netmask); 967 ifamsg->ifa_flags = 0; // ifa_flags is useless 968 ifamsg->ifa_scope = ifa_get_scope(ifa); 969 ifamsg->ifa_index = ifp->if_index; 970 971 if ((ifp->if_flags & IFF_POINTOPOINT) && sa_dst != NULL && sa_dst->sa_family != 0) { 972 /* P2P interface may have IPv6 LL with no dst address */ 973 dump_sa(nw, IFA_ADDRESS, sa_dst); 974 dump_sa(nw, IFA_LOCAL, sa); 975 } else { 976 dump_sa(nw, IFA_ADDRESS, sa); 977 #ifdef INET 978 /* 979 * In most cases, IFA_ADDRESS == IFA_LOCAL 980 * Skip IFA_LOCAL for anything except INET 981 */ 982 if (sa->sa_family == AF_INET) 983 dump_sa(nw, IFA_LOCAL, sa); 984 #endif 985 } 986 if (ifp->if_flags & IFF_BROADCAST) 987 dump_sa(nw, IFA_BROADCAST, ifa->ifa_broadaddr); 988 989 nlattr_add_string(nw, IFA_LABEL, if_name(ifp)); 990 991 uint32_t nl_ifa_flags = 0; 992 #ifdef INET6 993 if (sa->sa_family == AF_INET6) { 994 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 995 nl_ifa_flags = in6_flags_to_nl(ia->ia6_flags); 996 } 997 #endif 998 nlattr_add_u32(nw, IFA_FLAGS, nl_ifa_flags); 999 1000 export_cache_info(nw, ifa); 1001 1002 /* Store FreeBSD-specific attributes */ 1003 int off = nlattr_add_nested(nw, IFA_FREEBSD); 1004 if (off != 0) { 1005 if (ifa->ifa_carp != NULL && carp_get_vhid_p != NULL) { 1006 uint32_t vhid = (uint32_t)(*carp_get_vhid_p)(ifa); 1007 nlattr_add_u32(nw, IFAF_VHID, vhid); 1008 } 1009 #ifdef INET6 1010 if (sa->sa_family == AF_INET6) { 1011 uint32_t ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags; 1012 1013 nlattr_add_u32(nw, IFAF_FLAGS, ifa_flags); 1014 } 1015 #endif 1016 1017 nlattr_set_len(nw, off); 1018 } 1019 1020 if (nlmsg_end(nw)) 1021 return (true); 1022 enomem: 1023 NL_LOG(LOG_DEBUG, "Failed to dump ifa type %s(%d) for interface %s", 1024 rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp)); 1025 nlmsg_abort(nw); 1026 return (false); 1027 } 1028 1029 static int 1030 dump_iface_addrs(struct netlink_walkargs *wa, struct ifnet *ifp) 1031 { 1032 struct ifaddr *ifa; 1033 1034 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1035 if (wa->family != 0 && wa->family != ifa->ifa_addr->sa_family) 1036 continue; 1037 if (ifa->ifa_addr->sa_family == AF_LINK) 1038 continue; 1039 if (prison_if(wa->cred, ifa->ifa_addr) != 0) 1040 continue; 1041 wa->count++; 1042 if (!dump_iface_addr(wa->nw, ifp, ifa, &wa->hdr)) 1043 return (ENOMEM); 1044 wa->dumped++; 1045 } 1046 1047 return (0); 1048 } 1049 1050 static int 1051 rtnl_handle_getaddr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt) 1052 { 1053 struct ifnet *ifp; 1054 int error = 0; 1055 1056 struct nl_parsed_ifa attrs = {}; 1057 error = nl_parse_nlmsg(hdr, &ifa_parser, npt, &attrs); 1058 if (error != 0) 1059 return (error); 1060 1061 struct netlink_walkargs wa = { 1062 .so = nlp, 1063 .nw = npt->nw, 1064 .cred = nlp_get_cred(nlp), 1065 .family = attrs.ifa_family, 1066 .hdr.nlmsg_pid = hdr->nlmsg_pid, 1067 .hdr.nlmsg_seq = hdr->nlmsg_seq, 1068 .hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI, 1069 .hdr.nlmsg_type = NL_RTM_NEWADDR, 1070 }; 1071 1072 NL_LOG(LOG_DEBUG2, "Start dump"); 1073 1074 if (attrs.ifa_index != 0) { 1075 ifp = ifnet_byindex(attrs.ifa_index); 1076 if (ifp == NULL) 1077 error = ENOENT; 1078 else 1079 error = dump_iface_addrs(&wa, ifp); 1080 } else { 1081 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1082 error = dump_iface_addrs(&wa, ifp); 1083 if (error != 0) 1084 break; 1085 } 1086 } 1087 1088 NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped); 1089 1090 if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) { 1091 NL_LOG(LOG_DEBUG, "Unable to finalize the dump"); 1092 return (ENOMEM); 1093 } 1094 1095 return (error); 1096 } 1097 1098 #ifdef INET 1099 static int 1100 handle_newaddr_inet(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs, 1101 struct ifnet *ifp, struct nlpcb *nlp, struct nl_pstate *npt) 1102 { 1103 int plen = attrs->ifa_prefixlen; 1104 int if_flags = if_getflags(ifp); 1105 struct sockaddr_in *addr, *dst; 1106 1107 if (plen > 32) { 1108 nlmsg_report_err_msg(npt, "invalid ifa_prefixlen"); 1109 return (EINVAL); 1110 }; 1111 1112 if (if_flags & IFF_POINTOPOINT) { 1113 /* 1114 * Only P2P IFAs are allowed by the implementation. 1115 */ 1116 if (attrs->ifa_address == NULL || attrs->ifa_local == NULL) { 1117 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS"); 1118 return (EINVAL); 1119 } 1120 addr = (struct sockaddr_in *)attrs->ifa_local; 1121 dst = (struct sockaddr_in *)attrs->ifa_address; 1122 } else { 1123 /* 1124 * Map the Netlink attributes to FreeBSD ifa layout. 1125 * If only IFA_ADDRESS or IFA_LOCAL is set OR 1126 * both are set to the same value => ifa is not p2p 1127 * and the attribute value contains interface address. 1128 * 1129 * Otherwise (both IFA_ADDRESS and IFA_LOCAL are set and 1130 * different), IFA_LOCAL contains an interface address and 1131 * IFA_ADDRESS contains peer address. 1132 */ 1133 addr = (struct sockaddr_in *)attrs->ifa_local; 1134 if (addr == NULL) 1135 addr = (struct sockaddr_in *)attrs->ifa_address; 1136 1137 if (addr == NULL) { 1138 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS"); 1139 return (EINVAL); 1140 } 1141 1142 /* Generate broadcast address if not set */ 1143 if ((if_flags & IFF_BROADCAST) && attrs->ifa_broadcast == NULL) { 1144 uint32_t s_baddr; 1145 struct sockaddr_in *sin_brd; 1146 1147 if (plen == 31) 1148 s_baddr = INADDR_BROADCAST; /* RFC 3021 */ 1149 else { 1150 uint32_t s_mask; 1151 1152 s_mask = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0); 1153 s_baddr = addr->sin_addr.s_addr | ~s_mask; 1154 } 1155 1156 sin_brd = (struct sockaddr_in *)npt_alloc(npt, sizeof(*sin_brd)); 1157 if (sin_brd == NULL) 1158 return (ENOMEM); 1159 sin_brd->sin_family = AF_INET; 1160 sin_brd->sin_len = sizeof(*sin_brd); 1161 sin_brd->sin_addr.s_addr = s_baddr; 1162 attrs->ifa_broadcast = (struct sockaddr *)sin_brd; 1163 } 1164 dst = (struct sockaddr_in *)attrs->ifa_broadcast; 1165 } 1166 1167 struct sockaddr_in mask = { 1168 .sin_len = sizeof(struct sockaddr_in), 1169 .sin_family = AF_INET, 1170 .sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0), 1171 }; 1172 struct in_aliasreq req = { 1173 .ifra_addr = *addr, 1174 .ifra_mask = mask, 1175 .ifra_vhid = attrs->ifaf_vhid, 1176 }; 1177 if (dst != NULL) 1178 req.ifra_dstaddr = *dst; 1179 1180 return (in_control(NULL, SIOCAIFADDR, &req, ifp, curthread)); 1181 } 1182 1183 static int 1184 handle_deladdr_inet(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs, 1185 struct ifnet *ifp, struct nlpcb *nlp, struct nl_pstate *npt) 1186 { 1187 struct sockaddr_in *addr = (struct sockaddr_in *)attrs->ifa_local; 1188 1189 if (addr == NULL) 1190 addr = (struct sockaddr_in *)attrs->ifa_address; 1191 1192 if (addr == NULL) { 1193 nlmsg_report_err_msg(npt, "empty IFA_ADDRESS/IFA_LOCAL"); 1194 return (EINVAL); 1195 } 1196 1197 struct in_aliasreq req = { .ifra_addr = *addr }; 1198 1199 return (in_control(NULL, SIOCDIFADDR, &req, ifp, curthread)); 1200 } 1201 #endif 1202 1203 #ifdef INET6 1204 static int 1205 handle_newaddr_inet6(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs, 1206 struct ifnet *ifp, struct nlpcb *nlp, struct nl_pstate *npt) 1207 { 1208 struct sockaddr_in6 *addr, *dst; 1209 1210 if (attrs->ifa_prefixlen > 128) { 1211 nlmsg_report_err_msg(npt, "invalid ifa_prefixlen"); 1212 return (EINVAL); 1213 } 1214 1215 /* 1216 * In IPv6 implementation, adding non-P2P address to the P2P interface 1217 * is allowed. 1218 */ 1219 addr = (struct sockaddr_in6 *)(attrs->ifa_local); 1220 dst = (struct sockaddr_in6 *)(attrs->ifa_address); 1221 1222 if (addr == NULL) { 1223 addr = dst; 1224 dst = NULL; 1225 } else if (dst != NULL) { 1226 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr, &dst->sin6_addr)) { 1227 /* 1228 * Sometimes Netlink users fills in both attributes 1229 * with the same address. It still means "non-p2p". 1230 */ 1231 dst = NULL; 1232 } 1233 } 1234 1235 if (addr == NULL) { 1236 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS"); 1237 return (EINVAL); 1238 } 1239 1240 uint32_t flags = nl_flags_to_in6(attrs->ifa_flags) | attrs->ifaf_flags; 1241 1242 uint32_t pltime = 0, vltime = 0; 1243 if (attrs->ifa_cacheinfo != 0) { 1244 pltime = attrs->ifa_cacheinfo->ifa_prefered; 1245 vltime = attrs->ifa_cacheinfo->ifa_valid; 1246 } 1247 1248 struct sockaddr_in6 mask = { 1249 .sin6_len = sizeof(struct sockaddr_in6), 1250 .sin6_family = AF_INET6, 1251 }; 1252 ip6_writemask(&mask.sin6_addr, attrs->ifa_prefixlen); 1253 1254 struct in6_aliasreq req = { 1255 .ifra_addr = *addr, 1256 .ifra_prefixmask = mask, 1257 .ifra_flags = flags, 1258 .ifra_lifetime = { .ia6t_vltime = vltime, .ia6t_pltime = pltime }, 1259 .ifra_vhid = attrs->ifaf_vhid, 1260 }; 1261 if (dst != NULL) 1262 req.ifra_dstaddr = *dst; 1263 1264 return (in6_control(NULL, SIOCAIFADDR_IN6, &req, ifp, curthread)); 1265 } 1266 1267 static int 1268 handle_deladdr_inet6(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs, 1269 struct ifnet *ifp, struct nlpcb *nlp, struct nl_pstate *npt) 1270 { 1271 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)attrs->ifa_local; 1272 1273 if (addr == NULL) 1274 addr = (struct sockaddr_in6 *)(attrs->ifa_address); 1275 1276 if (addr == NULL) { 1277 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS"); 1278 return (EINVAL); 1279 } 1280 1281 struct in6_aliasreq req = { .ifra_addr = *addr }; 1282 1283 return (in6_control(NULL, SIOCDIFADDR_IN6, &req, ifp, curthread)); 1284 } 1285 #endif 1286 1287 1288 static int 1289 rtnl_handle_addr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt) 1290 { 1291 struct epoch_tracker et; 1292 int error; 1293 1294 struct nl_parsed_ifa attrs = {}; 1295 error = nl_parse_nlmsg(hdr, &ifa_parser, npt, &attrs); 1296 if (error != 0) 1297 return (error); 1298 1299 NET_EPOCH_ENTER(et); 1300 struct ifnet *ifp = ifnet_byindex_ref(attrs.ifa_index); 1301 NET_EPOCH_EXIT(et); 1302 1303 if (ifp == NULL) { 1304 nlmsg_report_err_msg(npt, "Unable to find interface with index %u", 1305 attrs.ifa_index); 1306 return (ENOENT); 1307 } 1308 int if_flags = if_getflags(ifp); 1309 1310 #if defined(INET) || defined(INET6) 1311 bool new = hdr->nlmsg_type == NL_RTM_NEWADDR; 1312 #endif 1313 1314 /* 1315 * TODO: Properly handle NLM_F_CREATE / NLM_F_EXCL. 1316 * The current ioctl-based KPI always does an implicit create-or-replace. 1317 * It is not possible to specify fine-grained options. 1318 */ 1319 1320 switch (attrs.ifa_family) { 1321 #ifdef INET 1322 case AF_INET: 1323 if (new) 1324 error = handle_newaddr_inet(hdr, &attrs, ifp, nlp, npt); 1325 else 1326 error = handle_deladdr_inet(hdr, &attrs, ifp, nlp, npt); 1327 break; 1328 #endif 1329 #ifdef INET6 1330 case AF_INET6: 1331 if (new) 1332 error = handle_newaddr_inet6(hdr, &attrs, ifp, nlp, npt); 1333 else 1334 error = handle_deladdr_inet6(hdr, &attrs, ifp, nlp, npt); 1335 break; 1336 #endif 1337 default: 1338 error = EAFNOSUPPORT; 1339 } 1340 1341 if (error == 0 && !(if_flags & IFF_UP) && (if_getflags(ifp) & IFF_UP)) 1342 if_up(ifp); 1343 1344 if_rele(ifp); 1345 1346 return (error); 1347 } 1348 1349 1350 static void 1351 rtnl_handle_ifaddr(void *arg __unused, struct ifaddr *ifa, int cmd) 1352 { 1353 struct nlmsghdr hdr = {}; 1354 struct nl_writer nw = {}; 1355 uint32_t group = 0; 1356 1357 switch (ifa->ifa_addr->sa_family) { 1358 #ifdef INET 1359 case AF_INET: 1360 group = RTNLGRP_IPV4_IFADDR; 1361 break; 1362 #endif 1363 #ifdef INET6 1364 case AF_INET6: 1365 group = RTNLGRP_IPV6_IFADDR; 1366 break; 1367 #endif 1368 default: 1369 NL_LOG(LOG_DEBUG2, "ifa notification for unknown AF: %d", 1370 ifa->ifa_addr->sa_family); 1371 return; 1372 } 1373 1374 if (!nl_has_listeners(NETLINK_ROUTE, group)) 1375 return; 1376 1377 if (!nlmsg_get_group_writer(&nw, NLMSG_LARGE, NETLINK_ROUTE, group)) { 1378 NL_LOG(LOG_DEBUG, "error allocating group writer"); 1379 return; 1380 } 1381 1382 hdr.nlmsg_type = (cmd == RTM_DELETE) ? NL_RTM_DELADDR : NL_RTM_NEWADDR; 1383 1384 dump_iface_addr(&nw, ifa->ifa_ifp, ifa, &hdr); 1385 nlmsg_flush(&nw); 1386 } 1387 1388 static void 1389 rtnl_handle_ifevent(struct ifnet *ifp, int nlmsg_type, int if_flags_mask) 1390 { 1391 struct nlmsghdr hdr = { .nlmsg_type = nlmsg_type }; 1392 struct nl_writer nw = {}; 1393 1394 if (!nl_has_listeners(NETLINK_ROUTE, RTNLGRP_LINK)) 1395 return; 1396 1397 if (!nlmsg_get_group_writer(&nw, NLMSG_LARGE, NETLINK_ROUTE, RTNLGRP_LINK)) { 1398 NL_LOG(LOG_DEBUG, "error allocating mbuf"); 1399 return; 1400 } 1401 dump_iface(&nw, ifp, &hdr, if_flags_mask); 1402 nlmsg_flush(&nw); 1403 } 1404 1405 static void 1406 rtnl_handle_ifattach(void *arg, struct ifnet *ifp) 1407 { 1408 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp)); 1409 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0); 1410 } 1411 1412 static void 1413 rtnl_handle_ifdetach(void *arg, struct ifnet *ifp) 1414 { 1415 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp)); 1416 rtnl_handle_ifevent(ifp, NL_RTM_DELLINK, 0); 1417 } 1418 1419 static void 1420 rtnl_handle_iflink(void *arg, struct ifnet *ifp) 1421 { 1422 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp)); 1423 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0); 1424 } 1425 1426 void 1427 rtnl_handle_ifnet_event(struct ifnet *ifp, int if_flags_mask) 1428 { 1429 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp)); 1430 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, if_flags_mask); 1431 } 1432 1433 static const struct rtnl_cmd_handler cmd_handlers[] = { 1434 { 1435 .cmd = NL_RTM_GETLINK, 1436 .name = "RTM_GETLINK", 1437 .cb = &rtnl_handle_getlink, 1438 .flags = RTNL_F_NOEPOCH | RTNL_F_ALLOW_NONVNET_JAIL, 1439 }, 1440 { 1441 .cmd = NL_RTM_DELLINK, 1442 .name = "RTM_DELLINK", 1443 .cb = &rtnl_handle_dellink, 1444 .priv = PRIV_NET_IFDESTROY, 1445 .flags = RTNL_F_NOEPOCH, 1446 }, 1447 { 1448 .cmd = NL_RTM_NEWLINK, 1449 .name = "RTM_NEWLINK", 1450 .cb = &rtnl_handle_newlink, 1451 .priv = PRIV_NET_IFCREATE, 1452 .flags = RTNL_F_NOEPOCH, 1453 }, 1454 { 1455 .cmd = NL_RTM_GETADDR, 1456 .name = "RTM_GETADDR", 1457 .cb = &rtnl_handle_getaddr, 1458 .flags = RTNL_F_ALLOW_NONVNET_JAIL, 1459 }, 1460 { 1461 .cmd = NL_RTM_NEWADDR, 1462 .name = "RTM_NEWADDR", 1463 .cb = &rtnl_handle_addr, 1464 .priv = PRIV_NET_ADDIFADDR, 1465 .flags = RTNL_F_NOEPOCH, 1466 }, 1467 { 1468 .cmd = NL_RTM_DELADDR, 1469 .name = "RTM_DELADDR", 1470 .cb = &rtnl_handle_addr, 1471 .priv = PRIV_NET_DELIFADDR, 1472 .flags = RTNL_F_NOEPOCH, 1473 }, 1474 }; 1475 1476 static const struct nlhdr_parser *all_parsers[] = { 1477 &ifmsg_parser, &ifa_parser, &ifa_fbsd_parser, 1478 }; 1479 1480 void 1481 rtnl_iface_add_cloner(struct nl_cloner *cloner) 1482 { 1483 sx_xlock(&rtnl_cloner_lock); 1484 SLIST_INSERT_HEAD(&nl_cloners, cloner, next); 1485 sx_xunlock(&rtnl_cloner_lock); 1486 } 1487 1488 void 1489 rtnl_iface_del_cloner(struct nl_cloner *cloner) 1490 { 1491 sx_xlock(&rtnl_cloner_lock); 1492 SLIST_REMOVE(&nl_cloners, cloner, nl_cloner, next); 1493 sx_xunlock(&rtnl_cloner_lock); 1494 } 1495 1496 void 1497 rtnl_ifaces_init(void) 1498 { 1499 ifattach_event = EVENTHANDLER_REGISTER( 1500 ifnet_arrival_event, rtnl_handle_ifattach, NULL, 1501 EVENTHANDLER_PRI_ANY); 1502 ifdetach_event = EVENTHANDLER_REGISTER( 1503 ifnet_departure_event, rtnl_handle_ifdetach, NULL, 1504 EVENTHANDLER_PRI_ANY); 1505 ifaddr_event = EVENTHANDLER_REGISTER( 1506 rt_addrmsg, rtnl_handle_ifaddr, NULL, 1507 EVENTHANDLER_PRI_ANY); 1508 iflink_event = EVENTHANDLER_REGISTER( 1509 ifnet_link_event, rtnl_handle_iflink, NULL, 1510 EVENTHANDLER_PRI_ANY); 1511 NL_VERIFY_PARSERS(all_parsers); 1512 rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers)); 1513 } 1514 1515 void 1516 rtnl_ifaces_destroy(void) 1517 { 1518 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ifattach_event); 1519 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_event); 1520 EVENTHANDLER_DEREGISTER(rt_addrmsg, ifaddr_event); 1521 EVENTHANDLER_DEREGISTER(ifnet_link_event, iflink_event); 1522 } 1523