1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2021 Ng Peng Nam Sean 5 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org> 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/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 #include "opt_inet.h" 32 #include "opt_inet6.h" 33 #include "opt_route.h" 34 #include <sys/types.h> 35 #include <sys/malloc.h> 36 #include <sys/rmlock.h> 37 #include <sys/socket.h> 38 39 #include <net/if.h> 40 #include <net/route.h> 41 #include <net/route/nhop.h> 42 #include <net/route/route_ctl.h> 43 #include <net/route/route_var.h> 44 #include <netinet6/scope6_var.h> 45 #include <netlink/netlink.h> 46 #include <netlink/netlink_ctl.h> 47 #include <netlink/netlink_route.h> 48 #include <netlink/route/route_var.h> 49 50 #define DEBUG_MOD_NAME nl_route 51 #define DEBUG_MAX_LEVEL LOG_DEBUG3 52 #include <netlink/netlink_debug.h> 53 _DECLARE_DEBUG(LOG_DEBUG); 54 55 static unsigned char 56 get_rtm_type(const struct nhop_object *nh) 57 { 58 int nh_flags = nh->nh_flags; 59 60 /* Use the fact that nhg runtime flags are only NHF_MULTIPATH */ 61 if (nh_flags & NHF_BLACKHOLE) 62 return (RTN_BLACKHOLE); 63 else if (nh_flags & NHF_REJECT) 64 return (RTN_PROHIBIT); 65 return (RTN_UNICAST); 66 } 67 68 static uint8_t 69 nl_get_rtm_protocol(const struct nhop_object *nh) 70 { 71 #ifdef ROUTE_MPATH 72 if (NH_IS_NHGRP(nh)) { 73 const struct nhgrp_object *nhg = (const struct nhgrp_object *)nh; 74 uint8_t origin = nhgrp_get_origin(nhg); 75 if (origin != RTPROT_UNSPEC) 76 return (origin); 77 nh = nhg->nhops[0]; 78 } 79 #endif 80 uint8_t origin = nhop_get_origin(nh); 81 if (origin != RTPROT_UNSPEC) 82 return (origin); 83 /* TODO: remove guesswork once all kernel users fill in origin */ 84 int rt_flags = nhop_get_rtflags(nh); 85 if (rt_flags & RTF_PROTO1) 86 return (RTPROT_ZEBRA); 87 if (rt_flags & RTF_STATIC) 88 return (RTPROT_STATIC); 89 return (RTPROT_KERNEL); 90 } 91 92 static int 93 get_rtmsg_type_from_rtsock(int cmd) 94 { 95 switch (cmd) { 96 case RTM_ADD: 97 case RTM_CHANGE: 98 case RTM_GET: 99 return NL_RTM_NEWROUTE; 100 case RTM_DELETE: 101 return NL_RTM_DELROUTE; 102 } 103 104 return (0); 105 } 106 107 /* 108 * fibnum heuristics 109 * 110 * if (dump && rtm_table == 0 && !rta_table) RT_ALL_FIBS 111 * msg rtm_table RTA_TABLE result 112 * RTM_GETROUTE/dump 0 - RT_ALL_FIBS 113 * RTM_GETROUTE/dump 1 - 1 114 * RTM_GETROUTE/get 0 - 0 115 * 116 */ 117 118 static struct nhop_object * 119 rc_get_nhop(const struct rib_cmd_info *rc) 120 { 121 return ((rc->rc_cmd == RTM_DELETE) ? rc->rc_nh_old : rc->rc_nh_new); 122 } 123 124 static void 125 dump_rc_nhop_gw(struct nl_writer *nw, const struct nhop_object *nh) 126 { 127 #ifdef INET6 128 int upper_family; 129 #endif 130 131 switch (nhop_get_neigh_family(nh)) { 132 case AF_LINK: 133 /* onlink prefix, skip */ 134 break; 135 case AF_INET: 136 nlattr_add(nw, NL_RTA_GATEWAY, 4, &nh->gw4_sa.sin_addr); 137 break; 138 #ifdef INET6 139 case AF_INET6: 140 upper_family = nhop_get_upper_family(nh); 141 if (upper_family == AF_INET6) { 142 struct in6_addr gw6 = nh->gw6_sa.sin6_addr; 143 in6_clearscope(&gw6); 144 145 nlattr_add(nw, NL_RTA_GATEWAY, 16, &gw6); 146 } else if (upper_family == AF_INET) { 147 /* IPv4 over IPv6 */ 148 struct in6_addr gw6 = nh->gw6_sa.sin6_addr; 149 in6_clearscope(&gw6); 150 151 char buf[20]; 152 struct rtvia *via = (struct rtvia *)&buf[0]; 153 via->rtvia_family = AF_INET6; 154 memcpy(via->rtvia_addr, &gw6, 16); 155 nlattr_add(nw, NL_RTA_VIA, 17, via); 156 } 157 break; 158 #endif 159 } 160 } 161 162 static void 163 dump_rc_nhop_mtu(struct nl_writer *nw, const struct nhop_object *nh) 164 { 165 int nla_len = sizeof(struct nlattr) * 2 + sizeof(uint32_t); 166 struct nlattr *nla = nlmsg_reserve_data(nw, nla_len, struct nlattr); 167 168 if (nla == NULL) 169 return; 170 nla->nla_type = NL_RTA_METRICS; 171 nla->nla_len = nla_len; 172 nla++; 173 nla->nla_type = NL_RTAX_MTU; 174 nla->nla_len = sizeof(struct nlattr) + sizeof(uint32_t); 175 *((uint32_t *)(nla + 1)) = nh->nh_mtu; 176 } 177 178 #ifdef ROUTE_MPATH 179 static void 180 dump_rc_nhg(struct nl_writer *nw, const struct nhgrp_object *nhg, struct rtmsg *rtm) 181 { 182 uint32_t uidx = nhgrp_get_uidx(nhg); 183 uint32_t num_nhops; 184 const struct weightened_nhop *wn = nhgrp_get_nhops(nhg, &num_nhops); 185 uint32_t base_rtflags = nhop_get_rtflags(wn[0].nh); 186 187 if (uidx != 0) 188 nlattr_add_u32(nw, NL_RTA_NH_ID, uidx); 189 nlattr_add_u32(nw, NL_RTA_KNH_ID, nhgrp_get_idx(nhg)); 190 191 nlattr_add_u32(nw, NL_RTA_RTFLAGS, base_rtflags); 192 int off = nlattr_add_nested(nw, NL_RTA_MULTIPATH); 193 if (off == 0) 194 return; 195 196 for (int i = 0; i < num_nhops; i++) { 197 int nh_off = nlattr_save_offset(nw); 198 struct rtnexthop *rtnh = nlmsg_reserve_object(nw, struct rtnexthop); 199 if (rtnh == NULL) 200 return; 201 rtnh->rtnh_flags = 0; 202 rtnh->rtnh_ifindex = wn[i].nh->nh_ifp->if_index; 203 rtnh->rtnh_hops = wn[i].weight; 204 dump_rc_nhop_gw(nw, wn[i].nh); 205 uint32_t rtflags = nhop_get_rtflags(wn[i].nh); 206 if (rtflags != base_rtflags) 207 nlattr_add_u32(nw, NL_RTA_RTFLAGS, rtflags); 208 if (rtflags & RTF_FIXEDMTU) 209 dump_rc_nhop_mtu(nw, wn[i].nh); 210 rtnh = nlattr_restore_offset(nw, nh_off, struct rtnexthop); 211 /* 212 * nlattr_add() allocates 4-byte aligned storage, no need to aligh 213 * length here 214 * */ 215 rtnh->rtnh_len = nlattr_save_offset(nw) - nh_off; 216 } 217 nlattr_set_len(nw, off); 218 } 219 #endif 220 221 static void 222 dump_rc_nhop(struct nl_writer *nw, const struct route_nhop_data *rnd, struct rtmsg *rtm) 223 { 224 #ifdef ROUTE_MPATH 225 if (NH_IS_NHGRP(rnd->rnd_nhop)) { 226 dump_rc_nhg(nw, rnd->rnd_nhgrp, rtm); 227 return; 228 } 229 #endif 230 const struct nhop_object *nh = rnd->rnd_nhop; 231 uint32_t rtflags = nhop_get_rtflags(nh); 232 233 /* 234 * IPv4 over IPv6 235 * ('RTA_VIA', {'family': 10, 'addr': 'fe80::20c:29ff:fe67:2dd'}), ('RTA_OIF', 2), 236 * IPv4 w/ gw 237 * ('RTA_GATEWAY', '172.16.107.131'), ('RTA_OIF', 2)], 238 * Direct route: 239 * ('RTA_OIF', 2) 240 */ 241 if (nh->nh_flags & NHF_GATEWAY) 242 dump_rc_nhop_gw(nw, nh); 243 244 uint32_t uidx = nhop_get_uidx(nh); 245 if (uidx != 0) 246 nlattr_add_u32(nw, NL_RTA_NH_ID, uidx); 247 nlattr_add_u32(nw, NL_RTA_KNH_ID, nhop_get_idx(nh)); 248 nlattr_add_u32(nw, NL_RTA_RTFLAGS, rtflags); 249 250 if (rtflags & RTF_FIXEDMTU) 251 dump_rc_nhop_mtu(nw, nh); 252 uint32_t nh_expire = nhop_get_expire(nh); 253 if (nh_expire > 0) 254 nlattr_add_u32(nw, NL_RTA_EXPIRES, nh_expire - time_uptime); 255 256 /* In any case, fill outgoing interface */ 257 nlattr_add_u32(nw, NL_RTA_OIF, nh->nh_ifp->if_index); 258 259 if (rnd->rnd_weight != RT_DEFAULT_WEIGHT) 260 nlattr_add_u32(nw, NL_RTA_WEIGHT, rnd->rnd_weight); 261 } 262 263 /* 264 * Dumps output from a rib command into an rtmsg 265 */ 266 267 static int 268 dump_px(uint32_t fibnum, const struct nlmsghdr *hdr, 269 const struct rtentry *rt, struct route_nhop_data *rnd, 270 struct nl_writer *nw) 271 { 272 struct rtmsg *rtm; 273 int error = 0; 274 275 NET_EPOCH_ASSERT(); 276 277 if (!nlmsg_reply(nw, hdr, sizeof(struct rtmsg))) 278 goto enomem; 279 280 int family = rt_get_family(rt); 281 int rtm_off = nlattr_save_offset(nw); 282 rtm = nlmsg_reserve_object(nw, struct rtmsg); 283 rtm->rtm_family = family; 284 rtm->rtm_dst_len = 0; 285 rtm->rtm_src_len = 0; 286 rtm->rtm_tos = 0; 287 if (fibnum < 255) 288 rtm->rtm_table = (unsigned char)fibnum; 289 rtm->rtm_scope = RT_SCOPE_UNIVERSE; 290 rtm->rtm_protocol = nl_get_rtm_protocol(rnd->rnd_nhop); 291 rtm->rtm_type = get_rtm_type(rnd->rnd_nhop); 292 293 nlattr_add_u32(nw, NL_RTA_TABLE, fibnum); 294 295 int plen = 0; 296 #if defined(INET) || defined(INET6) 297 uint32_t scopeid; 298 #endif 299 switch (family) { 300 #ifdef INET 301 case AF_INET: 302 { 303 struct in_addr addr; 304 rt_get_inet_prefix_plen(rt, &addr, &plen, &scopeid); 305 nlattr_add(nw, NL_RTA_DST, 4, &addr); 306 break; 307 } 308 #endif 309 #ifdef INET6 310 case AF_INET6: 311 { 312 struct in6_addr addr; 313 rt_get_inet6_prefix_plen(rt, &addr, &plen, &scopeid); 314 nlattr_add(nw, NL_RTA_DST, 16, &addr); 315 break; 316 } 317 #endif 318 default: 319 FIB_LOG(LOG_NOTICE, fibnum, family, "unsupported rt family: %d", family); 320 error = EAFNOSUPPORT; 321 goto flush; 322 } 323 324 rtm = nlattr_restore_offset(nw, rtm_off, struct rtmsg); 325 if (plen > 0) 326 rtm->rtm_dst_len = plen; 327 dump_rc_nhop(nw, rnd, rtm); 328 329 if (nlmsg_end(nw)) 330 return (0); 331 enomem: 332 error = ENOMEM; 333 flush: 334 nlmsg_abort(nw); 335 return (error); 336 } 337 338 static int 339 family_to_group(int family) 340 { 341 switch (family) { 342 case AF_INET: 343 return (RTNLGRP_IPV4_ROUTE); 344 case AF_INET6: 345 return (RTNLGRP_IPV6_ROUTE); 346 } 347 return (0); 348 } 349 350 351 static void 352 report_operation(uint32_t fibnum, struct rib_cmd_info *rc, 353 struct nlpcb *nlp, struct nlmsghdr *hdr) 354 { 355 struct nl_writer nw = {}; 356 uint32_t group_id = family_to_group(rt_get_family(rc->rc_rt)); 357 358 if (nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_ROUTE, group_id)) { 359 struct route_nhop_data rnd = { 360 .rnd_nhop = rc_get_nhop(rc), 361 .rnd_weight = rc->rc_nh_weight, 362 }; 363 hdr->nlmsg_flags &= ~(NLM_F_REPLACE | NLM_F_CREATE); 364 hdr->nlmsg_flags &= ~(NLM_F_EXCL | NLM_F_APPEND); 365 switch (rc->rc_cmd) { 366 case RTM_ADD: 367 hdr->nlmsg_type = NL_RTM_NEWROUTE; 368 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL; 369 break; 370 case RTM_CHANGE: 371 hdr->nlmsg_type = NL_RTM_NEWROUTE; 372 hdr->nlmsg_flags |= NLM_F_REPLACE; 373 break; 374 case RTM_DELETE: 375 hdr->nlmsg_type = NL_RTM_DELROUTE; 376 break; 377 } 378 dump_px(fibnum, hdr, rc->rc_rt, &rnd, &nw); 379 nlmsg_flush(&nw); 380 } 381 382 rtsock_callback_p->route_f(fibnum, rc); 383 } 384 385 struct rta_mpath_nh { 386 struct sockaddr *gw; 387 struct ifnet *ifp; 388 uint8_t rtnh_flags; 389 uint8_t rtnh_weight; 390 }; 391 392 #define _IN(_field) offsetof(struct rtnexthop, _field) 393 #define _OUT(_field) offsetof(struct rta_mpath_nh, _field) 394 const static struct nlattr_parser nla_p_rtnh[] = { 395 { .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = nlattr_get_ip }, 396 { .type = NL_RTA_VIA, .off = _OUT(gw), .cb = nlattr_get_ipvia }, 397 }; 398 const static struct nlfield_parser nlf_p_rtnh[] = { 399 { .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = nlf_get_u8 }, 400 { .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = nlf_get_u8 }, 401 { .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifp), .cb = nlf_get_ifpz }, 402 }; 403 #undef _IN 404 #undef _OUT 405 NL_DECLARE_PARSER(mpath_parser, struct rtnexthop, nlf_p_rtnh, nla_p_rtnh); 406 407 struct rta_mpath { 408 int num_nhops; 409 struct rta_mpath_nh nhops[0]; 410 }; 411 412 static int 413 nlattr_get_multipath(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target) 414 { 415 int data_len = nla->nla_len - sizeof(struct nlattr); 416 struct rtnexthop *rtnh; 417 418 int max_nhops = data_len / sizeof(struct rtnexthop); 419 420 struct rta_mpath *mp = npt_alloc(npt, (max_nhops + 2) * sizeof(struct rta_mpath_nh)); 421 mp->num_nhops = 0; 422 423 for (rtnh = (struct rtnexthop *)(nla + 1); data_len > 0; ) { 424 struct rta_mpath_nh *mpnh = &mp->nhops[mp->num_nhops++]; 425 426 int error = nl_parse_header(rtnh, rtnh->rtnh_len, &mpath_parser, 427 npt, mpnh); 428 if (error != 0) { 429 NLMSG_REPORT_ERR_MSG(npt, "RTA_MULTIPATH: nexhop %d: parse failed", 430 mp->num_nhops - 1); 431 return (error); 432 } 433 434 int len = NL_ITEM_ALIGN(rtnh->rtnh_len); 435 data_len -= len; 436 rtnh = (struct rtnexthop *)((char *)rtnh + len); 437 } 438 if (data_len != 0 || mp->num_nhops == 0) { 439 NLMSG_REPORT_ERR_MSG(npt, "invalid RTA_MULTIPATH attr"); 440 return (EINVAL); 441 } 442 443 *((struct rta_mpath **)target) = mp; 444 return (0); 445 } 446 447 448 struct nl_parsed_route { 449 struct sockaddr *rta_dst; 450 struct sockaddr *rta_gw; 451 struct ifnet *rta_oif; 452 struct rta_mpath *rta_multipath; 453 uint32_t rta_table; 454 uint32_t rta_rtflags; 455 uint32_t rta_nh_id; 456 uint32_t rta_weight; 457 uint32_t rtax_mtu; 458 uint8_t rtm_family; 459 uint8_t rtm_dst_len; 460 uint8_t rtm_protocol; 461 uint8_t rtm_type; 462 }; 463 464 #define _IN(_field) offsetof(struct rtmsg, _field) 465 #define _OUT(_field) offsetof(struct nl_parsed_route, _field) 466 static struct nlattr_parser nla_p_rtmetrics[] = { 467 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = nlattr_get_uint32 }, 468 }; 469 NL_DECLARE_ATTR_PARSER(metrics_parser, nla_p_rtmetrics); 470 471 static const struct nlattr_parser nla_p_rtmsg[] = { 472 { .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = nlattr_get_ip }, 473 { .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = nlattr_get_ifp }, 474 { .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = nlattr_get_ip }, 475 { .type = NL_RTA_METRICS, .arg = &metrics_parser, .cb = nlattr_get_nested }, 476 { .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath }, 477 { .type = NL_RTA_WEIGHT, .off = _OUT(rta_weight), .cb = nlattr_get_uint32 }, 478 { .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = nlattr_get_uint32 }, 479 { .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = nlattr_get_uint32 }, 480 { .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = nlattr_get_ipvia }, 481 { .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = nlattr_get_uint32 }, 482 }; 483 484 static const struct nlfield_parser nlf_p_rtmsg[] = { 485 { .off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = nlf_get_u8 }, 486 { .off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = nlf_get_u8 }, 487 { .off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = nlf_get_u8 }, 488 { .off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = nlf_get_u8 }, 489 }; 490 #undef _IN 491 #undef _OUT 492 NL_DECLARE_PARSER(rtm_parser, struct rtmsg, nlf_p_rtmsg, nla_p_rtmsg); 493 494 struct netlink_walkargs { 495 struct nl_writer *nw; 496 struct route_nhop_data rnd; 497 struct nlmsghdr hdr; 498 struct nlpcb *nlp; 499 uint32_t fibnum; 500 int family; 501 int error; 502 int count; 503 int dumped; 504 int dumped_tables; 505 }; 506 507 static int 508 dump_rtentry(struct rtentry *rt, void *_arg) 509 { 510 struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg; 511 int error; 512 513 wa->count++; 514 if (wa->error != 0) 515 return (0); 516 if (!rt_is_exportable(rt, nlp_get_cred(wa->nlp))) 517 return (0); 518 wa->dumped++; 519 520 rt_get_rnd(rt, &wa->rnd); 521 522 error = dump_px(wa->fibnum, &wa->hdr, rt, &wa->rnd, wa->nw); 523 524 IF_DEBUG_LEVEL(LOG_DEBUG3) { 525 char rtbuf[INET6_ADDRSTRLEN + 5]; 526 FIB_LOG(LOG_DEBUG3, wa->fibnum, wa->family, 527 "Dump %s, offset %u, error %d", 528 rt_print_buf(rt, rtbuf, sizeof(rtbuf)), 529 wa->nw->offset, error); 530 } 531 wa->error = error; 532 533 return (0); 534 } 535 536 static void 537 dump_rtable_one(struct netlink_walkargs *wa, uint32_t fibnum, int family) 538 { 539 FIB_LOG(LOG_DEBUG2, fibnum, family, "Start dump"); 540 wa->count = 0; 541 wa->dumped = 0; 542 543 rib_walk(fibnum, family, false, dump_rtentry, wa); 544 545 wa->dumped_tables++; 546 547 FIB_LOG(LOG_DEBUG2, fibnum, family, "End dump, iterated %d dumped %d", 548 wa->count, wa->dumped); 549 NL_LOG(LOG_DEBUG2, "Current offset: %d", wa->nw->offset); 550 } 551 552 static int 553 dump_rtable_fib(struct netlink_walkargs *wa, uint32_t fibnum, int family) 554 { 555 wa->fibnum = fibnum; 556 557 if (family == AF_UNSPEC) { 558 for (int i = 0; i < AF_MAX; i++) { 559 if (rt_tables_get_rnh(fibnum, i) != 0) { 560 wa->family = i; 561 dump_rtable_one(wa, fibnum, i); 562 if (wa->error != 0) 563 break; 564 } 565 } 566 } else { 567 if (rt_tables_get_rnh(fibnum, family) != 0) { 568 wa->family = family; 569 dump_rtable_one(wa, fibnum, family); 570 } 571 } 572 573 return (wa->error); 574 } 575 576 static int 577 handle_rtm_getroute(struct nlpcb *nlp, struct nl_parsed_route *attrs, 578 struct nlmsghdr *hdr, struct nl_pstate *npt) 579 { 580 RIB_RLOCK_TRACKER; 581 struct rib_head *rnh; 582 struct rtentry *rt; 583 uint32_t fibnum = attrs->rta_table; 584 sa_family_t family = attrs->rtm_family; 585 586 if (attrs->rta_dst == NULL) { 587 NLMSG_REPORT_ERR_MSG(npt, "No RTA_DST supplied"); 588 return (EINVAL); 589 } 590 591 FIB_LOG(LOG_DEBUG, fibnum, family, "getroute called"); 592 593 rnh = rt_tables_get_rnh(fibnum, family); 594 if (rnh == NULL) 595 return (EAFNOSUPPORT); 596 597 RIB_RLOCK(rnh); 598 599 rt = (struct rtentry *)rnh->rnh_matchaddr(attrs->rta_dst, &rnh->head); 600 if (rt == NULL) { 601 RIB_RUNLOCK(rnh); 602 return (ESRCH); 603 } 604 605 struct route_nhop_data rnd; 606 rt_get_rnd(rt, &rnd); 607 rnd.rnd_nhop = nhop_select_func(rnd.rnd_nhop, 0); 608 609 RIB_RUNLOCK(rnh); 610 611 if (!rt_is_exportable(rt, nlp_get_cred(nlp))) 612 return (ESRCH); 613 614 IF_DEBUG_LEVEL(LOG_DEBUG2) { 615 char rtbuf[NHOP_PRINT_BUFSIZE] __unused, nhbuf[NHOP_PRINT_BUFSIZE] __unused; 616 FIB_LOG(LOG_DEBUG2, fibnum, family, "getroute completed: got %s for %s", 617 nhop_print_buf_any(rnd.rnd_nhop, nhbuf, sizeof(nhbuf)), 618 rt_print_buf(rt, rtbuf, sizeof(rtbuf))); 619 } 620 621 hdr->nlmsg_type = NL_RTM_NEWROUTE; 622 dump_px(fibnum, hdr, rt, &rnd, npt->nw); 623 624 return (0); 625 } 626 627 static int 628 handle_rtm_dump(struct nlpcb *nlp, uint32_t fibnum, int family, 629 struct nlmsghdr *hdr, struct nl_writer *nw) 630 { 631 struct netlink_walkargs wa = { 632 .nlp = nlp, 633 .nw = nw, 634 .hdr.nlmsg_pid = hdr->nlmsg_pid, 635 .hdr.nlmsg_seq = hdr->nlmsg_seq, 636 .hdr.nlmsg_type = NL_RTM_NEWROUTE, 637 .hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI, 638 }; 639 640 if (fibnum == RT_TABLE_UNSPEC) { 641 for (int i = 0; i < V_rt_numfibs; i++) { 642 dump_rtable_fib(&wa, fibnum, family); 643 if (wa.error != 0) 644 break; 645 } 646 } else 647 dump_rtable_fib(&wa, fibnum, family); 648 649 if (wa.error == 0 && wa.dumped_tables == 0) { 650 FIB_LOG(LOG_DEBUG, fibnum, family, "incorrect fibnum/family"); 651 wa.error = ESRCH; 652 // How do we propagate it? 653 } 654 655 if (!nlmsg_end_dump(wa.nw, wa.error, &wa.hdr)) { 656 NL_LOG(LOG_DEBUG, "Unable to finalize the dump"); 657 return (ENOMEM); 658 } 659 660 return (wa.error); 661 } 662 663 static struct nhop_object * 664 finalize_nhop(struct nhop_object *nh, int *perror) 665 { 666 /* 667 * The following MUST be filled: 668 * nh_ifp, nh_ifa, nh_gw 669 */ 670 if (nh->gw_sa.sa_family == 0) { 671 /* 672 * Empty gateway. Can be direct route with RTA_OIF set. 673 */ 674 if (nh->nh_ifp != NULL) 675 nhop_set_direct_gw(nh, nh->nh_ifp); 676 else { 677 NL_LOG(LOG_DEBUG, "empty gateway and interface, skipping"); 678 *perror = EINVAL; 679 return (NULL); 680 } 681 /* Both nh_ifp and gateway are set */ 682 } else { 683 /* Gateway is set up, we can derive ifp if not set */ 684 if (nh->nh_ifp == NULL) { 685 struct ifaddr *ifa = ifa_ifwithnet(&nh->gw_sa, 1, nhop_get_fibnum(nh)); 686 if (ifa == NULL) { 687 NL_LOG(LOG_DEBUG, "Unable to determine ifp, skipping"); 688 *perror = EINVAL; 689 return (NULL); 690 } 691 nhop_set_transmit_ifp(nh, ifa->ifa_ifp); 692 } 693 } 694 /* Both nh_ifp and gateway are set */ 695 if (nh->nh_ifa == NULL) { 696 struct ifaddr *ifa = ifaof_ifpforaddr(&nh->gw_sa, nh->nh_ifp); 697 if (ifa == NULL) { 698 NL_LOG(LOG_DEBUG, "Unable to determine ifa, skipping"); 699 *perror = EINVAL; 700 return (NULL); 701 } 702 nhop_set_src(nh, ifa); 703 } 704 705 return (nhop_get_nhop(nh, perror)); 706 } 707 708 static int 709 get_pxflag(const struct nl_parsed_route *attrs) 710 { 711 int pxflag = 0; 712 switch (attrs->rtm_family) { 713 case AF_INET: 714 if (attrs->rtm_dst_len == 32) 715 pxflag = NHF_HOST; 716 else if (attrs->rtm_dst_len == 0) 717 pxflag = NHF_DEFAULT; 718 break; 719 case AF_INET6: 720 if (attrs->rtm_dst_len == 32) 721 pxflag = NHF_HOST; 722 else if (attrs->rtm_dst_len == 0) 723 pxflag = NHF_DEFAULT; 724 break; 725 } 726 727 return (pxflag); 728 } 729 730 static int 731 get_op_flags(int nlm_flags) 732 { 733 int op_flags = 0; 734 735 op_flags |= (nlm_flags & NLM_F_REPLACE) ? RTM_F_REPLACE : 0; 736 op_flags |= (nlm_flags & NLM_F_EXCL) ? RTM_F_EXCL : 0; 737 op_flags |= (nlm_flags & NLM_F_CREATE) ? RTM_F_CREATE : 0; 738 op_flags |= (nlm_flags & NLM_F_APPEND) ? RTM_F_APPEND : 0; 739 740 return (op_flags); 741 } 742 743 #ifdef ROUTE_MPATH 744 static int 745 create_nexthop_one(struct nl_parsed_route *attrs, struct rta_mpath_nh *mpnh, 746 struct nl_pstate *npt, struct nhop_object **pnh) 747 { 748 int error; 749 750 if (mpnh->gw == NULL) 751 return (EINVAL); 752 753 struct nhop_object *nh = nhop_alloc(attrs->rta_table, attrs->rtm_family); 754 if (nh == NULL) 755 return (ENOMEM); 756 757 error = nl_set_nexthop_gw(nh, mpnh->gw, mpnh->ifp, npt); 758 if (error != 0) { 759 nhop_free(nh); 760 return (error); 761 } 762 if (mpnh->ifp != NULL) 763 nhop_set_transmit_ifp(nh, mpnh->ifp); 764 nhop_set_rtflags(nh, attrs->rta_rtflags); 765 if (attrs->rtm_protocol > RTPROT_STATIC) 766 nhop_set_origin(nh, attrs->rtm_protocol); 767 768 *pnh = finalize_nhop(nh, &error); 769 770 return (error); 771 } 772 #endif 773 774 static struct nhop_object * 775 create_nexthop_from_attrs(struct nl_parsed_route *attrs, 776 struct nl_pstate *npt, int *perror) 777 { 778 struct nhop_object *nh = NULL; 779 int error = 0; 780 781 if (attrs->rta_multipath != NULL) { 782 #ifdef ROUTE_MPATH 783 /* Multipath w/o explicit nexthops */ 784 int num_nhops = attrs->rta_multipath->num_nhops; 785 struct weightened_nhop *wn = npt_alloc(npt, sizeof(*wn) * num_nhops); 786 787 for (int i = 0; i < num_nhops; i++) { 788 struct rta_mpath_nh *mpnh = &attrs->rta_multipath->nhops[i]; 789 790 error = create_nexthop_one(attrs, mpnh, npt, &wn[i].nh); 791 if (error != 0) { 792 for (int j = 0; j < i; j++) 793 nhop_free(wn[j].nh); 794 break; 795 } 796 wn[i].weight = mpnh->rtnh_weight > 0 ? mpnh->rtnh_weight : 1; 797 } 798 if (error == 0) { 799 struct rib_head *rh = nhop_get_rh(wn[0].nh); 800 struct nhgrp_object *nhg; 801 802 nhg = nhgrp_alloc(rh->rib_fibnum, rh->rib_family, 803 wn, num_nhops, perror); 804 if (nhg != NULL) { 805 if (attrs->rtm_protocol > RTPROT_STATIC) 806 nhgrp_set_origin(nhg, attrs->rtm_protocol); 807 nhg = nhgrp_get_nhgrp(nhg, perror); 808 } 809 for (int i = 0; i < num_nhops; i++) 810 nhop_free(wn[i].nh); 811 if (nhg != NULL) 812 return ((struct nhop_object *)nhg); 813 error = *perror; 814 } 815 #else 816 error = ENOTSUP; 817 #endif 818 *perror = error; 819 } else { 820 nh = nhop_alloc(attrs->rta_table, attrs->rtm_family); 821 if (nh == NULL) { 822 *perror = ENOMEM; 823 return (NULL); 824 } 825 if (attrs->rta_gw != NULL) { 826 *perror = nl_set_nexthop_gw(nh, attrs->rta_gw, attrs->rta_oif, npt); 827 if (*perror != 0) { 828 nhop_free(nh); 829 return (NULL); 830 } 831 } 832 if (attrs->rta_oif != NULL) 833 nhop_set_transmit_ifp(nh, attrs->rta_oif); 834 if (attrs->rtax_mtu != 0) 835 nhop_set_mtu(nh, attrs->rtax_mtu, true); 836 if (attrs->rta_rtflags & RTF_BROADCAST) 837 nhop_set_broadcast(nh, true); 838 if (attrs->rtm_protocol > RTPROT_STATIC) 839 nhop_set_origin(nh, attrs->rtm_protocol); 840 nhop_set_rtflags(nh, attrs->rta_rtflags); 841 842 switch (attrs->rtm_type) { 843 case RTN_UNICAST: 844 break; 845 case RTN_BLACKHOLE: 846 nhop_set_blackhole(nh, RTF_BLACKHOLE); 847 break; 848 case RTN_PROHIBIT: 849 case RTN_UNREACHABLE: 850 nhop_set_blackhole(nh, RTF_REJECT); 851 break; 852 /* TODO: return ENOTSUP for other types if strict option is set */ 853 } 854 855 nh = finalize_nhop(nh, perror); 856 } 857 858 return (nh); 859 } 860 861 static int 862 rtnl_handle_newroute(struct nlmsghdr *hdr, struct nlpcb *nlp, 863 struct nl_pstate *npt) 864 { 865 struct rib_cmd_info rc = {}; 866 struct nhop_object *nh = NULL; 867 int error; 868 869 struct nl_parsed_route attrs = {}; 870 error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs); 871 if (error != 0) 872 return (error); 873 874 /* Check if we have enough data */ 875 if (attrs.rta_dst == NULL) { 876 NL_LOG(LOG_DEBUG, "missing RTA_DST"); 877 return (EINVAL); 878 } 879 880 if (attrs.rta_table >= V_rt_numfibs) { 881 NLMSG_REPORT_ERR_MSG(npt, "invalid fib"); 882 return (EINVAL); 883 } 884 885 if (attrs.rta_nh_id != 0) { 886 /* Referenced uindex */ 887 int pxflag = get_pxflag(&attrs); 888 nh = nl_find_nhop(attrs.rta_table, attrs.rtm_family, attrs.rta_nh_id, 889 pxflag, &error); 890 if (error != 0) 891 return (error); 892 } else { 893 nh = create_nexthop_from_attrs(&attrs, npt, &error); 894 if (error != 0) { 895 NL_LOG(LOG_DEBUG, "Error creating nexthop"); 896 return (error); 897 } 898 } 899 900 if (!NH_IS_NHGRP(nh) && attrs.rta_weight == 0) 901 attrs.rta_weight = RT_DEFAULT_WEIGHT; 902 struct route_nhop_data rnd = { .rnd_nhop = nh, .rnd_weight = attrs.rta_weight }; 903 int op_flags = get_op_flags(hdr->nlmsg_flags); 904 905 error = rib_add_route_px(attrs.rta_table, attrs.rta_dst, attrs.rtm_dst_len, 906 &rnd, op_flags, &rc); 907 if (error == 0) 908 report_operation(attrs.rta_table, &rc, nlp, hdr); 909 return (error); 910 } 911 912 static int 913 path_match_func(const struct rtentry *rt, const struct nhop_object *nh, void *_data) 914 { 915 struct nl_parsed_route *attrs = (struct nl_parsed_route *)_data; 916 917 if ((attrs->rta_gw != NULL) && !rib_match_gw(rt, nh, attrs->rta_gw)) 918 return (0); 919 920 if ((attrs->rta_oif != NULL) && (attrs->rta_oif != nh->nh_ifp)) 921 return (0); 922 923 return (1); 924 } 925 926 static int 927 rtnl_handle_delroute(struct nlmsghdr *hdr, struct nlpcb *nlp, 928 struct nl_pstate *npt) 929 { 930 struct rib_cmd_info rc; 931 int error; 932 933 struct nl_parsed_route attrs = {}; 934 error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs); 935 if (error != 0) 936 return (error); 937 938 if (attrs.rta_dst == NULL) { 939 NLMSG_REPORT_ERR_MSG(npt, "RTA_DST is not set"); 940 return (ESRCH); 941 } 942 943 if (attrs.rta_table >= V_rt_numfibs) { 944 NLMSG_REPORT_ERR_MSG(npt, "invalid fib"); 945 return (EINVAL); 946 } 947 948 error = rib_del_route_px(attrs.rta_table, attrs.rta_dst, 949 attrs.rtm_dst_len, path_match_func, &attrs, 0, &rc); 950 if (error == 0) 951 report_operation(attrs.rta_table, &rc, nlp, hdr); 952 return (error); 953 } 954 955 static int 956 rtnl_handle_getroute(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt) 957 { 958 int error; 959 960 struct nl_parsed_route attrs = {}; 961 error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs); 962 if (error != 0) 963 return (error); 964 965 if (attrs.rta_table >= V_rt_numfibs) { 966 NLMSG_REPORT_ERR_MSG(npt, "invalid fib"); 967 return (EINVAL); 968 } 969 970 if (hdr->nlmsg_flags & NLM_F_DUMP) 971 error = handle_rtm_dump(nlp, attrs.rta_table, attrs.rtm_family, hdr, npt->nw); 972 else 973 error = handle_rtm_getroute(nlp, &attrs, hdr, npt); 974 975 return (error); 976 } 977 978 void 979 rtnl_handle_route_event(uint32_t fibnum, const struct rib_cmd_info *rc) 980 { 981 struct nl_writer nw = {}; 982 int family, nlm_flags = 0; 983 984 family = rt_get_family(rc->rc_rt); 985 986 /* XXX: check if there are active listeners first */ 987 988 /* TODO: consider passing PID/type/seq */ 989 switch (rc->rc_cmd) { 990 case RTM_ADD: 991 nlm_flags = NLM_F_EXCL | NLM_F_CREATE; 992 break; 993 case RTM_CHANGE: 994 nlm_flags = NLM_F_REPLACE; 995 break; 996 case RTM_DELETE: 997 nlm_flags = 0; 998 break; 999 } 1000 IF_DEBUG_LEVEL(LOG_DEBUG2) { 1001 char rtbuf[NHOP_PRINT_BUFSIZE] __unused; 1002 FIB_LOG(LOG_DEBUG2, fibnum, family, 1003 "received event %s for %s / nlm_flags=%X", 1004 rib_print_cmd(rc->rc_cmd), 1005 rt_print_buf(rc->rc_rt, rtbuf, sizeof(rtbuf)), 1006 nlm_flags); 1007 } 1008 1009 struct nlmsghdr hdr = { 1010 .nlmsg_flags = nlm_flags, 1011 .nlmsg_type = get_rtmsg_type_from_rtsock(rc->rc_cmd), 1012 }; 1013 1014 struct route_nhop_data rnd = { 1015 .rnd_nhop = rc_get_nhop(rc), 1016 .rnd_weight = rc->rc_nh_weight, 1017 }; 1018 1019 uint32_t group_id = family_to_group(family); 1020 if (!nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_ROUTE, group_id)) { 1021 NL_LOG(LOG_DEBUG, "error allocating event buffer"); 1022 return; 1023 } 1024 1025 dump_px(fibnum, &hdr, rc->rc_rt, &rnd, &nw); 1026 nlmsg_flush(&nw); 1027 } 1028 1029 static const struct rtnl_cmd_handler cmd_handlers[] = { 1030 { 1031 .cmd = NL_RTM_GETROUTE, 1032 .name = "RTM_GETROUTE", 1033 .cb = &rtnl_handle_getroute, 1034 .flags = RTNL_F_ALLOW_NONVNET_JAIL, 1035 }, 1036 { 1037 .cmd = NL_RTM_DELROUTE, 1038 .name = "RTM_DELROUTE", 1039 .cb = &rtnl_handle_delroute, 1040 .priv = PRIV_NET_ROUTE, 1041 }, 1042 { 1043 .cmd = NL_RTM_NEWROUTE, 1044 .name = "RTM_NEWROUTE", 1045 .cb = &rtnl_handle_newroute, 1046 .priv = PRIV_NET_ROUTE, 1047 } 1048 }; 1049 1050 static const struct nlhdr_parser *all_parsers[] = {&mpath_parser, &metrics_parser, &rtm_parser}; 1051 1052 void 1053 rtnl_routes_init(void) 1054 { 1055 NL_VERIFY_PARSERS(all_parsers); 1056 rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers)); 1057 } 1058