1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1988, 1993 5 * The Regents of the University of California. 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 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/protosw.h> 37 #include <sys/socket.h> 38 #include <sys/socketvar.h> 39 #include <sys/sysctl.h> 40 #include <sys/time.h> 41 42 #include <net/ethernet.h> 43 #include <net/if.h> 44 #include <net/if_dl.h> 45 #include <net/if_types.h> 46 #include <netlink/netlink.h> 47 #include <netlink/netlink_route.h> 48 #include <netlink/netlink_snl.h> 49 #include <netlink/netlink_snl_route.h> 50 51 #include <netinet/in.h> 52 #include <netgraph/ng_socket.h> 53 54 #include <arpa/inet.h> 55 #include <ifaddrs.h> 56 #include <libutil.h> 57 #include <netdb.h> 58 #include <stdbool.h> 59 #include <stdint.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <stdbool.h> 63 #include <string.h> 64 #include <sysexits.h> 65 #include <unistd.h> 66 #include <err.h> 67 #include <libxo/xo.h> 68 #include "netstat.h" 69 #include "common.h" 70 #include "nl_defs.h" 71 72 73 static void p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr); 74 75 static struct ifmap_entry *ifmap; 76 static size_t ifmap_size; 77 78 struct nl_parsed_link { 79 uint32_t ifi_index; 80 uint32_t ifla_mtu; 81 char *ifla_ifname; 82 }; 83 84 #define _IN(_field) offsetof(struct ifinfomsg, _field) 85 #define _OUT(_field) offsetof(struct nl_parsed_link, _field) 86 static struct snl_attr_parser ap_link[] = { 87 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string }, 88 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 }, 89 }; 90 static struct snl_field_parser fp_link[] = { 91 {.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 }, 92 }; 93 #undef _IN 94 #undef _OUT 95 SNL_DECLARE_PARSER(link_parser, struct ifinfomsg, fp_link, ap_link); 96 97 /* Generate ifmap using netlink */ 98 static struct ifmap_entry * 99 prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size) 100 { 101 struct { 102 struct nlmsghdr hdr; 103 struct ifinfomsg ifmsg; 104 } msg = { 105 .hdr.nlmsg_type = RTM_GETLINK, 106 .hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 107 .hdr.nlmsg_seq = snl_get_seq(ss), 108 }; 109 msg.hdr.nlmsg_len = sizeof(msg); 110 111 if (!snl_send(ss, &msg, sizeof(msg))) { 112 snl_free(ss); 113 return (NULL); 114 } 115 116 struct ifmap_entry *ifmap = NULL; 117 uint32_t ifmap_size = 0; 118 struct nlmsghdr *hdr; 119 while ((hdr = snl_read_message(ss)) != NULL && hdr->nlmsg_type != NLMSG_DONE) { 120 if (hdr->nlmsg_seq != msg.hdr.nlmsg_seq) 121 continue; 122 /* 123 if (hdr->nlmsg_type == NLMSG_ERROR) 124 break; 125 */ 126 struct nl_parsed_link link = {}; 127 if (!snl_parse_nlmsg(ss, hdr, &link_parser, &link)) 128 continue; 129 if (link.ifi_index >= ifmap_size) { 130 size_t size = roundup2(link.ifi_index + 1, 32) * sizeof(struct ifmap_entry); 131 if ((ifmap = realloc(ifmap, size)) == NULL) 132 errx(2, "realloc(%zu) failed", size); 133 memset(&ifmap[ifmap_size], 0, 134 size - ifmap_size * 135 sizeof(struct ifmap_entry)); 136 ifmap_size = roundup2(link.ifi_index + 1, 32); 137 } 138 if (*ifmap[link.ifi_index].ifname != '\0') 139 continue; 140 strlcpy(ifmap[link.ifi_index].ifname, link.ifla_ifname, IFNAMSIZ); 141 ifmap[link.ifi_index].mtu = link.ifla_mtu; 142 } 143 *pifmap_size = ifmap_size; 144 return (ifmap); 145 } 146 147 struct rta_mpath_nh { 148 struct sockaddr *gw; 149 uint32_t ifindex; 150 uint8_t rtnh_flags; 151 uint8_t rtnh_weight; 152 uint32_t rtax_mtu; 153 uint32_t rta_knh_id; 154 uint32_t rta_rtflags; 155 }; 156 157 #define _IN(_field) offsetof(struct rtnexthop, _field) 158 #define _OUT(_field) offsetof(struct rta_mpath_nh, _field) 159 static const struct snl_attr_parser nla_p_mp_rtmetrics[] = { 160 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 }, 161 }; 162 SNL_DECLARE_ATTR_PARSER(metrics_mp_parser, nla_p_mp_rtmetrics); 163 164 static const struct snl_attr_parser psnh[] = { 165 { .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = snl_attr_get_ip }, 166 { .type = NL_RTA_METRICS, .arg = &metrics_mp_parser, .cb = snl_attr_get_nested }, 167 { .type = NL_RTA_KNH_ID, .off = _OUT(rta_knh_id), .cb = snl_attr_get_uint32 }, 168 { .type = NL_RTA_RTFLAGS, .off = _OUT(gw), .cb = snl_attr_get_uint32 }, 169 { .type = NL_RTA_VIA, .off = _OUT(gw), .cb = snl_attr_get_ipvia }, 170 }; 171 172 static const struct snl_field_parser fpnh[] = { 173 { .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = snl_field_get_uint8 }, 174 { .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = snl_field_get_uint8 }, 175 { .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifindex), .cb = snl_field_get_uint32 }, 176 }; 177 #undef _IN 178 #undef _OUT 179 180 SNL_DECLARE_PARSER(mpath_parser, struct rtnexthop, fpnh, psnh); 181 182 struct rta_mpath { 183 int num_nhops; 184 struct rta_mpath_nh nhops[0]; 185 }; 186 187 static bool 188 nlattr_get_multipath(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target) 189 { 190 int data_len = nla->nla_len - sizeof(struct nlattr); 191 struct rtnexthop *rtnh; 192 193 int max_nhops = data_len / sizeof(struct rtnexthop); 194 size_t sz = (max_nhops + 2) * sizeof(struct rta_mpath_nh); 195 196 struct rta_mpath *mp = snl_allocz(ss, sz); 197 mp->num_nhops = 0; 198 199 for (rtnh = (struct rtnexthop *)(nla + 1); data_len > 0; ) { 200 struct rta_mpath_nh *mpnh = &mp->nhops[mp->num_nhops++]; 201 202 if (!snl_parse_header(ss, rtnh, rtnh->rtnh_len, &mpath_parser, mpnh)) 203 return (false); 204 205 int len = NL_ITEM_ALIGN(rtnh->rtnh_len); 206 data_len -= len; 207 rtnh = (struct rtnexthop *)((char *)rtnh + len); 208 } 209 if (data_len != 0 || mp->num_nhops == 0) { 210 return (false); 211 } 212 213 *((struct rta_mpath **)target) = mp; 214 return (true); 215 } 216 217 218 struct nl_parsed_route { 219 struct sockaddr *rta_dst; 220 struct sockaddr *rta_gw; 221 struct nlattr *rta_metrics; 222 struct rta_mpath *rta_multipath; 223 uint32_t rta_expires; 224 uint32_t rta_oif; 225 uint32_t rta_expire; 226 uint32_t rta_table; 227 uint32_t rta_knh_id; 228 uint32_t rta_rtflags; 229 uint32_t rtax_mtu; 230 uint32_t rtax_weight; 231 uint8_t rtm_family; 232 uint8_t rtm_type; 233 uint8_t rtm_protocol; 234 uint8_t rtm_dst_len; 235 }; 236 237 #define _IN(_field) offsetof(struct rtmsg, _field) 238 #define _OUT(_field) offsetof(struct nl_parsed_route, _field) 239 static const struct snl_attr_parser nla_p_rtmetrics[] = { 240 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 }, 241 }; 242 SNL_DECLARE_ATTR_PARSER(metrics_parser, nla_p_rtmetrics); 243 244 static const struct snl_attr_parser ps[] = { 245 { .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = snl_attr_get_ip }, 246 { .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = snl_attr_get_uint32 }, 247 { .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = snl_attr_get_ip }, 248 { .type = NL_RTA_METRICS, .arg = &metrics_parser, .cb = snl_attr_get_nested }, 249 { .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath }, 250 { .type = NL_RTA_KNH_ID, .off = _OUT(rta_knh_id), .cb = snl_attr_get_uint32 }, 251 { .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 }, 252 { .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = snl_attr_get_uint32 }, 253 { .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = snl_attr_get_ipvia }, 254 { .type = NL_RTA_EXPIRES, .off = _OUT(rta_expire), .cb = snl_attr_get_uint32 }, 255 }; 256 257 static const struct snl_field_parser fprt[] = { 258 {.off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = snl_field_get_uint8 }, 259 {.off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = snl_field_get_uint8 }, 260 {.off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = snl_field_get_uint8 }, 261 {.off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = snl_field_get_uint8 }, 262 }; 263 #undef _IN 264 #undef _OUT 265 SNL_DECLARE_PARSER(rtm_parser, struct rtmsg, fprt, ps); 266 267 #define RTF_UP 0x1 268 #define RTF_GATEWAY 0x2 269 #define RTF_HOST 0x4 270 #define RTF_REJECT 0x8 271 #define RTF_DYNAMIC 0x10 272 #define RTF_STATIC 0x800 273 #define RTF_BLACKHOLE 0x1000 274 #define RTF_PROTO2 0x4000 275 #define RTF_PROTO1 0x8000 276 #define RTF_PROTO3 0x40000 277 #define RTF_FIXEDMTU 0x80000 278 #define RTF_PINNED 0x100000 279 280 static void 281 ip6_writemask(struct in6_addr *addr6, uint8_t mask) 282 { 283 uint32_t *cp; 284 285 for (cp = (uint32_t *)addr6; mask >= 32; mask -= 32) 286 *cp++ = 0xFFFFFFFF; 287 if (mask > 0) 288 *cp = htonl(mask ? ~((1 << (32 - mask)) - 1) : 0); 289 } 290 291 static void 292 gen_mask(int family, int plen, struct sockaddr *sa) 293 { 294 if (family == AF_INET6) { 295 struct sockaddr_in6 sin6 = { 296 .sin6_family = AF_INET6, 297 .sin6_len = sizeof(struct sockaddr_in6), 298 }; 299 ip6_writemask(&sin6.sin6_addr, plen); 300 *((struct sockaddr_in6 *)sa) = sin6; 301 } else if (family == AF_INET) { 302 struct sockaddr_in sin = { 303 .sin_family = AF_INET, 304 .sin_len = sizeof(struct sockaddr_in), 305 .sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0), 306 }; 307 *((struct sockaddr_in *)sa) = sin; 308 } 309 } 310 311 struct sockaddr_dl_short { 312 u_char sdl_len; /* Total length of sockaddr */ 313 u_char sdl_family; /* AF_LINK */ 314 u_short sdl_index; /* if != 0, system given index for interface */ 315 u_char sdl_type; /* interface type */ 316 u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */ 317 u_char sdl_alen; /* link level address length */ 318 u_char sdl_slen; /* link layer selector length */ 319 char sdl_data[8]; /* unused */ 320 }; 321 322 static void 323 p_path(struct nl_parsed_route *rt) 324 { 325 struct sockaddr_in6 mask6; 326 struct sockaddr *pmask = (struct sockaddr *)&mask6; 327 char buffer[128]; 328 char prettyname[128]; 329 int protrusion; 330 331 gen_mask(rt->rtm_family, rt->rtm_dst_len, pmask); 332 protrusion = p_sockaddr("destination", rt->rta_dst, pmask, rt->rta_rtflags, wid.dst); 333 protrusion = p_sockaddr("gateway", rt->rta_gw, NULL, RTF_HOST, 334 wid.gw - protrusion); 335 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ", 336 wid.flags - protrusion); 337 p_flags(rt->rta_rtflags | RTF_UP, buffer); 338 /* Output path weight as non-visual property */ 339 xo_emit("{e:weight/%u}", rt->rtax_weight); 340 341 memset(prettyname, 0, sizeof(prettyname)); 342 if (rt->rta_oif < ifmap_size) { 343 strlcpy(prettyname, ifmap[rt->rta_oif].ifname, 344 sizeof(prettyname)); 345 if (*prettyname == '\0') 346 strlcpy(prettyname, "---", sizeof(prettyname)); 347 if (rt->rtax_mtu == 0) 348 rt->rtax_mtu = ifmap[rt->rta_oif].mtu; 349 } 350 351 if (Wflag) { 352 /* XXX: use=0? */ 353 xo_emit("{t:nhop/%*lu} ", wid.mtu, rt->rta_knh_id); 354 355 if (rt->rtax_mtu != 0) 356 xo_emit("{t:mtu/%*lu} ", wid.mtu, rt->rtax_mtu); 357 else { 358 /* use interface mtu */ 359 xo_emit("{P:/%*s} ", wid.mtu, ""); 360 } 361 362 } 363 364 if (Wflag) 365 xo_emit("{t:interface-name/%*s}", wid.iface, prettyname); 366 else 367 xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface, 368 prettyname); 369 if (rt->rta_expires > 0) { 370 xo_emit(" {:expire-time/%*u}", wid.expire, rt->rta_expires); 371 } 372 } 373 374 static void 375 p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr) 376 { 377 378 struct nl_parsed_route rt = {}; 379 if (!snl_parse_nlmsg(ss, hdr, &rtm_parser, &rt)) 380 return; 381 382 if (rt.rta_multipath != NULL) { 383 uint32_t orig_rtflags = rt.rta_rtflags; 384 uint32_t orig_mtu = rt.rtax_mtu; 385 for (int i = 0; i < rt.rta_multipath->num_nhops; i++) { 386 struct rta_mpath_nh *nhop = &rt.rta_multipath->nhops[i]; 387 388 rt.rta_gw = nhop->gw; 389 rt.rta_oif = nhop->ifindex; 390 rt.rtax_weight = nhop->rtnh_weight; 391 rt.rta_rtflags = nhop->rta_rtflags ? nhop->rta_rtflags : orig_rtflags; 392 rt.rta_knh_id = nhop->rta_knh_id; 393 rt.rtax_mtu = nhop->rtax_mtu ? nhop->rtax_mtu : orig_mtu; 394 395 xo_open_instance(name); 396 p_path(&rt); 397 xo_emit("\n"); 398 xo_close_instance(name); 399 } 400 return; 401 } 402 403 struct sockaddr_dl_short sdl_gw = { 404 .sdl_family = AF_LINK, 405 .sdl_len = sizeof(struct sockaddr_dl_short), 406 .sdl_index = rt.rta_oif, 407 }; 408 if (rt.rta_gw == NULL) 409 rt.rta_gw = (struct sockaddr *)&sdl_gw; 410 411 xo_open_instance(name); 412 p_path(&rt); 413 xo_emit("\n"); 414 xo_close_instance(name); 415 } 416 417 static const struct snl_hdr_parser *all_parsers[] = { 418 &link_parser, &metrics_mp_parser, &mpath_parser, &metrics_parser, &rtm_parser 419 }; 420 421 bool 422 p_rtable_netlink(int fibnum, int af) 423 { 424 int fam = AF_UNSPEC; 425 int need_table_close = false; 426 struct nlmsghdr *hdr; 427 428 struct snl_state ss = {}; 429 430 SNL_VERIFY_PARSERS(all_parsers); 431 432 if (!snl_init(&ss, NETLINK_ROUTE)) 433 return (false); 434 435 ifmap = prepare_ifmap_netlink(&ss, &ifmap_size); 436 437 struct { 438 struct nlmsghdr hdr; 439 struct rtmsg rtmsg; 440 struct nlattr nla_fibnum; 441 uint32_t fibnum; 442 } msg = { 443 .hdr.nlmsg_type = RTM_GETROUTE, 444 .hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 445 .hdr.nlmsg_seq = snl_get_seq(&ss), 446 .rtmsg.rtm_family = af, 447 .nla_fibnum.nla_len = sizeof(struct nlattr) + sizeof(uint32_t), 448 .nla_fibnum.nla_type = RTA_TABLE, 449 .fibnum = fibnum, 450 }; 451 msg.hdr.nlmsg_len = sizeof(msg); 452 453 if (!snl_send(&ss, &msg, sizeof(msg))) { 454 snl_free(&ss); 455 return (false); 456 } 457 458 xo_open_container("route-table"); 459 xo_open_list("rt-family"); 460 while ((hdr = snl_read_message(&ss)) != NULL && hdr->nlmsg_type != NLMSG_DONE) { 461 if (hdr->nlmsg_seq != msg.hdr.nlmsg_seq) 462 continue; 463 struct rtmsg *rtm = (struct rtmsg *)(hdr + 1); 464 /* Only print family first time. */ 465 if (fam != rtm->rtm_family) { 466 if (need_table_close) { 467 xo_close_list("rt-entry"); 468 xo_close_instance("rt-family"); 469 } 470 need_table_close = true; 471 fam = rtm->rtm_family; 472 set_wid(fam); 473 xo_open_instance("rt-family"); 474 pr_family(fam); 475 xo_open_list("rt-entry"); 476 pr_rthdr(fam); 477 } 478 p_rtentry_netlink(&ss, "rt-entry", hdr); 479 snl_clear_lb(&ss); 480 } 481 if (need_table_close) { 482 xo_close_list("rt-entry"); 483 xo_close_instance("rt-family"); 484 } 485 xo_close_list("rt-family"); 486 xo_close_container("route-table"); 487 snl_free(&ss); 488 return (true); 489 } 490 491 492