7 #include <sys/bitcount.h>
9 #include <sys/linker.h>
10 #include <sys/module.h>
11 #include <sys/socket.h>
12 #include <sys/sysctl.h>
14 #include <sys/types.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
19 #include <net/ethernet.h>
21 #include <net/if_dl.h>
22 #include <net/if_types.h>
23 #include <netlink/netlink.h>
24 #include <netlink/netlink_route.h>
25 #include <netlink/netlink_snl.h>
26 #include <netlink/netlink_snl_route.h>
27 #include <netlink/netlink_snl_route_compat.h>
28 #include <netlink/netlink_snl_route_parsers.h>
30 const char *routename(struct sockaddr *);
31 const char *netname(struct sockaddr *);
32 void printb(int, const char *);
33 extern const char routeflags[];
34 extern int verbose, debugonly;
36 int rtmsg_nl(int cmd, int rtm_flags, int fib, int rtm_addrs, struct sockaddr_storage *so,
37 struct rt_metrics *rt_metrics);
38 int flushroutes_fib_nl(int fib, int af);
39 void monitor_nl(int fib);
43 static void print_getmsg(struct nl_helper *h, struct nlmsghdr *hdr,
44 struct sockaddr *dst);
45 static void print_nlmsg(struct nl_helper *h, struct nlmsghdr *hdr,
46 struct snl_msg_info *cinfo);
48 #define s6_addr32 __u6_addr.__u6_addr32
49 #define bitcount32(x) __bitcount32((uint32_t)(x))
51 inet6_get_plen(const struct in6_addr *addr)
54 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
55 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
59 ip6_writemask(struct in6_addr *addr6, uint8_t mask)
63 for (cp = (uint32_t *)addr6; mask >= 32; mask -= 32)
66 *cp = htonl(mask ? ~((1 << (32 - mask)) - 1) : 0);
69 static struct sockaddr *
70 get_netmask(struct snl_state *ss, int family, int plen)
72 if (family == AF_INET) {
76 struct sockaddr_in *sin = snl_allocz(ss, sizeof(*sin));
78 sin->sin_len = sizeof(*sin);
79 sin->sin_family = family;
80 sin->sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0);
82 return (struct sockaddr *)sin;
83 } else if (family == AF_INET6) {
87 struct sockaddr_in6 *sin6 = snl_allocz(ss, sizeof(*sin6));
89 sin6->sin6_len = sizeof(*sin6);
90 sin6->sin6_family = family;
91 ip6_writemask(&sin6->sin6_addr, plen);
93 return (struct sockaddr *)sin6;
99 nl_init_socket(struct snl_state *ss)
101 if (snl_init(ss, NETLINK_ROUTE))
104 if (modfind("netlink") == -1 && errno == ENOENT) {
106 if (kldload("netlink") == -1)
107 err(1, "netlink is not loaded and load attempt failed");
108 if (snl_init(ss, NETLINK_ROUTE))
112 err(1, "unable to open netlink socket");
116 struct snl_state ss_cmd;
120 nl_helper_init(struct nl_helper *h)
122 nl_init_socket(&h->ss_cmd);
126 nl_helper_free(struct nl_helper *h)
128 snl_free(&h->ss_cmd);
131 static struct sockaddr *
132 get_addr(struct sockaddr_storage *so, int rtm_addrs, int addr_type)
134 struct sockaddr *sa = NULL;
136 if (rtm_addrs & (1 << addr_type))
137 sa = (struct sockaddr *)&so[addr_type];
142 rtmsg_nl_int(struct nl_helper *h, int cmd, int rtm_flags, int fib, int rtm_addrs,
143 struct sockaddr_storage *so, struct rt_metrics *rt_metrics)
145 struct snl_state *ss = &h->ss_cmd;
146 struct snl_writer nw;
147 int nl_type = 0, nl_flags = 0;
149 snl_init_writer(ss, &nw);
153 nl_type = RTM_NEWROUTE;
154 nl_flags = NLM_F_CREATE | NLM_F_APPEND; /* Do append by default */
156 case RTSOCK_RTM_CHANGE:
157 nl_type = RTM_NEWROUTE;
158 nl_flags = NLM_F_REPLACE;
160 case RTSOCK_RTM_DELETE:
161 nl_type = RTM_DELROUTE;
164 nl_type = RTM_GETROUTE;
170 struct sockaddr *dst = get_addr(so, rtm_addrs, RTAX_DST);
171 struct sockaddr *mask = get_addr(so, rtm_addrs, RTAX_NETMASK);
172 struct sockaddr *gw = get_addr(so, rtm_addrs, RTAX_GATEWAY);
177 struct nlmsghdr *hdr = snl_create_msg_request(&nw, nl_type);
178 hdr->nlmsg_flags |= nl_flags;
181 int rtm_type = RTN_UNICAST;
183 switch (dst->sa_family) {
186 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
188 if ((rtm_flags & RTF_HOST) == 0 && mask4 != NULL)
189 plen = bitcount32(mask4->sin_addr.s_addr);
196 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
198 if ((rtm_flags & RTF_HOST) == 0 && mask6 != NULL)
199 plen = inet6_get_plen(&mask6->sin6_addr);
208 if (rtm_flags & RTF_REJECT)
209 rtm_type = RTN_PROHIBIT;
210 else if (rtm_flags & RTF_BLACKHOLE)
211 rtm_type = RTN_BLACKHOLE;
213 struct rtmsg *rtm = snl_reserve_msg_object(&nw, struct rtmsg);
214 rtm->rtm_family = dst->sa_family;
215 rtm->rtm_protocol = RTPROT_STATIC;
216 rtm->rtm_type = rtm_type;
217 rtm->rtm_dst_len = plen;
219 /* Request exact prefix match if mask is set */
220 if ((cmd == RTSOCK_RTM_GET) && (mask != NULL))
221 rtm->rtm_flags = RTM_F_PREFIX;
223 snl_add_msg_attr_ip(&nw, RTA_DST, dst);
224 snl_add_msg_attr_u32(&nw, RTA_TABLE, fib);
226 uint32_t rta_oif = 0;
229 if (rtm_flags & RTF_GATEWAY) {
230 if (gw->sa_family == dst->sa_family)
231 snl_add_msg_attr_ip(&nw, RTA_GATEWAY, gw);
233 snl_add_msg_attr_ipvia(&nw, RTA_VIA, gw);
234 if (gw->sa_family == AF_INET6) {
235 struct sockaddr_in6 *gw6 = (struct sockaddr_in6 *)gw;
237 if (IN6_IS_ADDR_LINKLOCAL(&gw6->sin6_addr))
238 rta_oif = gw6->sin6_scope_id;
241 /* Should be AF_LINK */
242 struct sockaddr_dl *sdl = (struct sockaddr_dl *)gw;
243 if (sdl->sdl_index != 0)
244 rta_oif = sdl->sdl_index;
248 if (dst->sa_family == AF_INET6 && rta_oif == 0) {
249 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
251 if (IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr))
252 rta_oif = dst6->sin6_scope_id;
256 snl_add_msg_attr_u32(&nw, RTA_OIF, rta_oif);
258 snl_add_msg_attr_u32(&nw, NL_RTA_RTFLAGS, rtm_flags);
260 if (rt_metrics->rmx_mtu > 0) {
261 int off = snl_add_msg_attr_nested(&nw, RTA_METRICS);
262 snl_add_msg_attr_u32(&nw, RTAX_MTU, rt_metrics->rmx_mtu);
263 snl_end_attr_nested(&nw, off);
266 if (rt_metrics->rmx_weight > 0)
267 snl_add_msg_attr_u32(&nw, NL_RTA_WEIGHT, rt_metrics->rmx_weight);
269 if (snl_finalize_msg(&nw) && snl_send_message(ss, hdr)) {
270 struct snl_errmsg_data e = {};
272 hdr = snl_read_reply(ss, hdr->nlmsg_seq);
273 if (nl_type == NL_RTM_GETROUTE) {
274 if (hdr->nlmsg_type == NL_RTM_NEWROUTE)
275 print_getmsg(h, hdr, dst);
277 snl_parse_errmsg(ss, hdr, &e);
278 if (e.error == ESRCH)
279 warn("route has not been found");
281 warn("message indicates error %d", e.error);
287 if (snl_parse_errmsg(ss, hdr, &e))
294 rtmsg_nl(int cmd, int rtm_flags, int fib, int rtm_addrs,
295 struct sockaddr_storage *so, struct rt_metrics *rt_metrics)
297 struct nl_helper h = {};
300 int error = rtmsg_nl_int(&h, cmd, rtm_flags, fib, rtm_addrs, so, rt_metrics);
307 get_ifdata(struct nl_helper *h, uint32_t ifindex, struct snl_parsed_link_simple *link)
309 struct snl_state *ss = &h->ss_cmd;
310 struct snl_writer nw;
312 snl_init_writer(ss, &nw);
313 struct nlmsghdr *hdr = snl_create_msg_request(&nw, NL_RTM_GETLINK);
314 struct ifinfomsg *ifmsg = snl_reserve_msg_object(&nw, struct ifinfomsg);
316 ifmsg->ifi_index = ifindex;
317 if (!snl_finalize_msg(&nw) || !snl_send_message(ss, hdr))
320 hdr = snl_read_reply(ss, hdr->nlmsg_seq);
322 if (hdr != NULL && hdr->nlmsg_type == RTM_NEWLINK) {
323 snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser_simple, link);
326 if (link->ifla_ifname == NULL) {
329 snprintf(ifname, sizeof(ifname), "if#%u", ifindex);
330 int len = strlen(ifname);
331 char *buf = snl_allocz(ss, len + 1);
332 strlcpy(buf, ifname, len + 1);
333 link->ifla_ifname = buf;
338 print_getmsg(struct nl_helper *h, struct nlmsghdr *hdr, struct sockaddr *dst)
340 struct snl_state *ss = &h->ss_cmd;
342 struct snl_parsed_route r = { .rtax_weight = RT_DEFAULT_WEIGHT };
344 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &r))
347 struct snl_parsed_link_simple link = {};
348 get_ifdata(h, r.rta_oif, &link);
351 r.rtax_mtu = link.ifla_mtu;
352 r.rta_rtflags |= (RTF_UP | RTF_DONE);
354 (void)printf(" route to: %s\n", routename(dst));
357 (void)printf("destination: %s\n", routename(r.rta_dst));
358 struct sockaddr *mask = get_netmask(ss, r.rtm_family, r.rtm_dst_len);
360 (void)printf(" mask: %s\n", routename(mask));
361 if (r.rta_gw && (r.rta_rtflags & RTF_GATEWAY))
362 (void)printf(" gateway: %s\n", routename(r.rta_gw));
363 (void)printf(" fib: %u\n", (unsigned int)r.rta_table);
364 if (link.ifla_ifname)
365 (void)printf(" interface: %s\n", link.ifla_ifname);
366 (void)printf(" flags: ");
367 printb(r.rta_rtflags, routeflags);
369 struct rt_metrics rmx = {
370 .rmx_mtu = r.rtax_mtu,
371 .rmx_weight = r.rtax_weight,
372 .rmx_expire = r.rta_expire,
375 printf("\n%9s %9s %9s %9s %9s %10s %9s\n", "recvpipe",
376 "sendpipe", "ssthresh", "rtt,msec", "mtu ", "weight", "expire");
377 printf("%8lu ", rmx.rmx_recvpipe);
378 printf("%8lu ", rmx.rmx_sendpipe);
379 printf("%8lu ", rmx.rmx_ssthresh);
380 printf("%8lu ", 0UL);
381 printf("%8lu ", rmx.rmx_mtu);
382 printf("%8lu ", rmx.rmx_weight);
383 if (rmx.rmx_expire > 0)
384 clock_gettime(CLOCK_REALTIME_FAST, &ts);
387 printf("%8ld \n", (long)(rmx.rmx_expire - ts.tv_sec));
391 print_prefix(struct nl_helper *h, char *buf, int bufsize, struct sockaddr *sa, int plen)
396 snprintf(buf, bufsize, "<NULL>");
400 switch (sa->sa_family) {
403 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
404 char abuf[INET_ADDRSTRLEN];
406 inet_ntop(AF_INET, &sin->sin_addr, abuf, sizeof(abuf));
407 sz = snprintf(buf, bufsize, "%s", abuf);
412 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
413 char abuf[INET6_ADDRSTRLEN];
416 inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof(abuf));
417 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
418 struct snl_parsed_link_simple link = {};
420 if (sin6->sin6_scope_id != 0) {
421 get_ifdata(h, sin6->sin6_scope_id, &link);
422 ifname = link.ifla_ifname;
426 sz = snprintf(buf, bufsize, "%s", abuf);
428 sz = snprintf(buf, bufsize, "%s%%%s", abuf, ifname);
432 snprintf(buf, bufsize, "unknown_af#%d", sa->sa_family);
437 snprintf(buf + sz, bufsize - sz, "/%d", plen);
441 print_line_prefix(struct nlmsghdr *hdr, struct snl_msg_info *cinfo,
442 const char *cmd, const char *name)
448 clock_gettime(CLOCK_REALTIME, &tp);
449 localtime_r(&tp.tv_sec, &tm);
451 strftime(buf, sizeof(buf), "%T", &tm);
452 int len = printf("%s.%03ld PID %4u %s %s ", buf, tp.tv_nsec / 1000000,
453 cinfo->process_id, cmd, name);
459 get_action_name(struct nlmsghdr *hdr, int new_cmd)
461 if (hdr->nlmsg_type == new_cmd) {
462 //return ((hdr->nlmsg_flags & NLM_F_REPLACE) ? "replace" : "add");
469 print_nlmsg_route_nhop(struct nl_helper *h, struct snl_parsed_route *r,
470 struct rta_mpath_nh *nh, bool first)
472 // gw 10.0.0.1 ifp vtnet0 mtu 1500 table inet.0
473 if (nh->gw != NULL) {
475 print_prefix(h, gwbuf, sizeof(gwbuf), nh->gw, -1);
476 printf("gw %s ", gwbuf);
479 if (nh->ifindex != 0) {
480 struct snl_parsed_link_simple link = {};
482 get_ifdata(h, nh->ifindex, &link);
483 if (nh->rtax_mtu == 0)
484 nh->rtax_mtu = link.ifla_mtu;
485 printf("iface %s ", link.ifla_ifname);
486 if (nh->rtax_mtu != 0)
487 printf("mtu %d ", nh->rtax_mtu);
491 switch (r->rtm_family) {
493 printf("table inet.%d", r->rta_table);
496 printf("table inet6.%d", r->rta_table);
505 print_nlmsg_route(struct nl_helper *h, struct nlmsghdr *hdr,
506 struct snl_msg_info *cinfo)
508 struct snl_parsed_route r = { .rtax_weight = RT_DEFAULT_WEIGHT };
509 struct snl_state *ss = &h->ss_cmd;
511 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &r))
514 // 20:19:41.333 add route 10.0.0.0/24 gw 10.0.0.1 ifp vtnet0 mtu 1500 table inet.0
516 const char *cmd = get_action_name(hdr, RTM_NEWROUTE);
517 int len = print_line_prefix(hdr, cinfo, cmd, "route");
520 print_prefix(h, buf, sizeof(buf), r.rta_dst, r.rtm_dst_len);
521 len += strlen(buf) + 1;
524 switch (r.rtm_type) {
526 printf("blackhole\n");
528 case RTN_UNREACHABLE:
529 printf("unreach(reject)\n");
532 printf("prohibit(reject)\n");
536 if (r.rta_multipath.num_nhops != 0) {
539 memset(buf, ' ', sizeof(buf));
542 for (uint32_t i = 0; i < r.rta_multipath.num_nhops; i++) {
543 struct rta_mpath_nh *nh = r.rta_multipath.nhops[i];
547 print_nlmsg_route_nhop(h, &r, nh, first);
551 struct rta_mpath_nh nh = {
553 .ifindex = r.rta_oif,
554 .rtax_mtu = r.rtax_mtu,
557 print_nlmsg_route_nhop(h, &r, &nh, true);
561 static const char *operstate[] = {
562 "UNKNOWN", /* 0, IF_OPER_UNKNOWN */
563 "NOTPRESENT", /* 1, IF_OPER_NOTPRESENT */
564 "DOWN", /* 2, IF_OPER_DOWN */
565 "LLDOWN", /* 3, IF_OPER_LOWERLAYERDOWN */
566 "TESTING", /* 4, IF_OPER_TESTING */
567 "DORMANT", /* 5, IF_OPER_DORMANT */
568 "UP", /* 6, IF_OPER_UP */
572 print_nlmsg_link(struct nl_helper *h, struct nlmsghdr *hdr,
573 struct snl_msg_info *cinfo)
575 struct snl_parsed_link l = {};
576 struct snl_state *ss = &h->ss_cmd;
578 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser, &l))
581 // 20:19:41.333 add iface#3 vtnet0 admin UP oper UP mtu 1500 table inet.0
582 const char *cmd = get_action_name(hdr, RTM_NEWLINK);
583 print_line_prefix(hdr, cinfo, cmd, "iface");
585 printf("iface#%u %s ", l.ifi_index, l.ifla_ifname);
586 printf("admin %s ", (l.ifi_flags & IFF_UP) ? "UP" : "DOWN");
587 if (l.ifla_operstate < NL_ARRAY_LEN(operstate))
588 printf("oper %s ", operstate[l.ifla_operstate]);
590 printf("mtu %u ", l.ifla_mtu);
596 print_nlmsg_addr(struct nl_helper *h, struct nlmsghdr *hdr,
597 struct snl_msg_info *cinfo)
599 struct snl_parsed_addr attrs = {};
600 struct snl_state *ss = &h->ss_cmd;
602 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_addr_parser, &attrs))
605 // add addr 192.168.1.1/24 iface vtnet0
606 const char *cmd = get_action_name(hdr, RTM_NEWADDR);
607 print_line_prefix(hdr, cinfo, cmd, "addr");
610 struct sockaddr *addr = attrs.ifa_local ? attrs.ifa_local : attrs.ifa_address;
611 print_prefix(h, buf, sizeof(buf), addr, attrs.ifa_prefixlen);
614 struct snl_parsed_link_simple link = {};
615 get_ifdata(h, attrs.ifa_index, &link);
617 if (link.ifi_flags & IFF_POINTOPOINT) {
619 print_prefix(h, buf, sizeof(buf), attrs.ifa_address, -1);
620 printf("-> %s ", buf);
623 printf("iface %s ", link.ifla_ifname);
628 static const char *nudstate[] = {
629 "INCOMPLETE", /* 0x01(0) */
630 "REACHABLE", /* 0x02(1) */
631 "STALE", /* 0x04(2) */
632 "DELAY", /* 0x08(3) */
633 "PROBE", /* 0x10(4) */
634 "FAILED", /* 0x20(5) */
637 #define NUD_INCOMPLETE 0x01 /* No lladdr, address resolution in progress */
638 #define NUD_REACHABLE 0x02 /* reachable & recently resolved */
639 #define NUD_STALE 0x04 /* has lladdr but it's stale */
640 #define NUD_DELAY 0x08 /* has lladdr, is stale, probes delayed */
641 #define NUD_PROBE 0x10 /* has lladdr, is stale, probes sent */
642 #define NUD_FAILED 0x20 /* unused */
646 print_nlmsg_neigh(struct nl_helper *h, struct nlmsghdr *hdr,
647 struct snl_msg_info *cinfo)
649 struct snl_parsed_neigh attrs = {};
650 struct snl_state *ss = &h->ss_cmd;
652 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_neigh_parser, &attrs))
655 // add addr 192.168.1.1 state %s lladdr %s iface vtnet0
656 const char *cmd = get_action_name(hdr, RTM_NEWNEIGH);
657 print_line_prefix(hdr, cinfo, cmd, "neigh");
660 print_prefix(h, buf, sizeof(buf), attrs.nda_dst, -1);
663 struct snl_parsed_link_simple link = {};
664 get_ifdata(h, attrs.nda_ifindex, &link);
666 for (unsigned int i = 0; i < NL_ARRAY_LEN(nudstate); i++) {
667 if ((1 << i) & attrs.ndm_state) {
668 printf("state %s ", nudstate[i]);
673 if (attrs.nda_lladdr != NULL) {
674 int if_type = link.ifi_type;
676 if ((if_type == IFT_ETHER || if_type == IFT_L2VLAN || if_type == IFT_BRIDGE) &&
677 NLA_DATA_LEN(attrs.nda_lladdr) == ETHER_ADDR_LEN) {
678 struct ether_addr *ll;
680 ll = (struct ether_addr *)NLA_DATA(attrs.nda_lladdr);
681 printf("lladdr %s ", ether_ntoa(ll));
683 struct sockaddr_dl sdl = {
684 .sdl_len = sizeof(sdl),
685 .sdl_family = AF_LINK,
686 .sdl_index = attrs.nda_ifindex,
688 .sdl_alen = NLA_DATA_LEN(attrs.nda_lladdr),
690 if (sdl.sdl_alen < sizeof(sdl.sdl_data)) {
691 void *ll = NLA_DATA(attrs.nda_lladdr);
693 memcpy(sdl.sdl_data, ll, sdl.sdl_alen);
694 printf("lladdr %s ", link_ntoa(&sdl));
699 if (link.ifla_ifname != NULL)
700 printf("iface %s ", link.ifla_ifname);
705 print_nlmsg_generic(struct nl_helper *h, struct nlmsghdr *hdr, struct snl_msg_info *cinfo)
707 const char *cmd = get_action_name(hdr, 0);
708 print_line_prefix(hdr, cinfo, cmd, "unknown message");
709 printf(" type %u\n", hdr->nlmsg_type);
713 print_nlmsg(struct nl_helper *h, struct nlmsghdr *hdr, struct snl_msg_info *cinfo)
715 switch (hdr->nlmsg_type) {
718 print_nlmsg_link(h, hdr, cinfo);
722 print_nlmsg_addr(h, hdr, cinfo);
726 print_nlmsg_route(h, hdr, cinfo);
730 print_nlmsg_neigh(h, hdr, cinfo);
733 print_nlmsg_generic(h, hdr, cinfo);
736 snl_clear_lb(&h->ss_cmd);
742 struct snl_state ss_event = {};
745 nl_init_socket(&ss_event);
763 socklen_t optlen = sizeof(optval);
764 setsockopt(ss_event.fd, SOL_NETLINK, NETLINK_MSG_INFO, &optval, optlen);
766 for (unsigned int i = 0; i < NL_ARRAY_LEN(groups); i++) {
768 int optval = groups[i];
769 socklen_t optlen = sizeof(optval);
770 error = setsockopt(ss_event.fd, SOL_NETLINK,
771 NETLINK_ADD_MEMBERSHIP, &optval, optlen);
773 warn("Unable to subscribe to group %d", optval);
776 struct snl_msg_info attrs = {};
777 struct nlmsghdr *hdr;
778 while ((hdr = snl_read_message_dbg(&ss_event, &attrs)) != NULL)
780 print_nlmsg(&h, hdr, &attrs);
781 snl_clear_lb(&h.ss_cmd);
782 snl_clear_lb(&ss_event);
791 print_flushed_route(struct snl_parsed_route *r, struct sockaddr *gw)
793 struct sockaddr *sa = r->rta_dst;
795 printf("%-20.20s ", r->rta_rtflags & RTF_HOST ?
796 routename(sa) : netname(sa));
798 printf("%-20.20s ", routename(sa));
799 printf("-fib %-3d ", r->rta_table);
804 flushroute_one(struct nl_helper *h, struct snl_parsed_route *r)
806 struct snl_state *ss = &h->ss_cmd;
807 struct snl_errmsg_data e = {};
808 struct snl_writer nw;
810 snl_init_writer(ss, &nw);
812 struct nlmsghdr *hdr = snl_create_msg_request(&nw, NL_RTM_DELROUTE);
813 struct rtmsg *rtm = snl_reserve_msg_object(&nw, struct rtmsg);
814 rtm->rtm_family = r->rtm_family;
815 rtm->rtm_dst_len = r->rtm_dst_len;
817 snl_add_msg_attr_u32(&nw, RTA_TABLE, r->rta_table);
818 snl_add_msg_attr_ip(&nw, RTA_DST, r->rta_dst);
820 if (!snl_finalize_msg(&nw) || !snl_send_message(ss, hdr))
823 if (!snl_read_reply_code(ss, hdr->nlmsg_seq, &e)) {
825 if (e.error == EPERM)
826 errc(1, e.error, "RTM_DELROUTE failed:");
828 warnc(e.error, "RTM_DELROUTE failed:");
833 struct snl_msg_info attrs = {};
834 print_nlmsg(h, hdr, &attrs);
837 if (r->rta_multipath.num_nhops != 0) {
838 for (uint32_t i = 0; i < r->rta_multipath.num_nhops; i++) {
839 struct rta_mpath_nh *nh = r->rta_multipath.nhops[i];
841 print_flushed_route(r, nh->gw);
845 print_flushed_route(r, r->rta_gw);
852 flushroutes_fib_nl(int fib, int af)
854 struct snl_state ss = {};
855 struct snl_writer nw;
856 struct nl_helper h = {};
859 snl_init_writer(&ss, &nw);
861 struct nlmsghdr *hdr = snl_create_msg_request(&nw, NL_RTM_GETROUTE);
862 hdr->nlmsg_flags |= NLM_F_DUMP;
863 struct rtmsg *rtm = snl_reserve_msg_object(&nw, struct rtmsg);
864 rtm->rtm_family = af;
865 snl_add_msg_attr_u32(&nw, RTA_TABLE, fib);
867 if (!snl_finalize_msg(&nw) || !snl_send_message(&ss, hdr)) {
872 struct snl_errmsg_data e = {};
873 uint32_t nlm_seq = hdr->nlmsg_seq;
877 while ((hdr = snl_read_reply_multi(&ss, nlm_seq, &e)) != NULL) {
878 struct snl_parsed_route r = { .rtax_weight = RT_DEFAULT_WEIGHT };
881 if (!snl_parse_nlmsg(&ss, hdr, &snl_rtm_route_parser, &r))
884 struct snl_msg_info attrs = {};
885 print_nlmsg(&h, hdr, &attrs);
887 if (r.rta_table != (uint32_t)fib || r.rtm_family != af)
889 if ((r.rta_rtflags & RTF_GATEWAY) == 0)
894 if ((error = flushroute_one(&h, &r)) != 0) {
896 errc(1, error, "RTM_DELROUTE failed:");
898 warnc(error, "RTM_DELROUTE failed:");
900 snl_clear_lb(&h.ss_cmd);