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);
280 if (snl_parse_errmsg(ss, hdr, &e)) {
283 warnx("route has not been found");
288 warnc(e.error, "message indicates error");
299 rtmsg_nl(int cmd, int rtm_flags, int fib, int rtm_addrs,
300 struct sockaddr_storage *so, struct rt_metrics *rt_metrics)
302 struct nl_helper h = {};
305 int error = rtmsg_nl_int(&h, cmd, rtm_flags, fib, rtm_addrs, so, rt_metrics);
312 get_ifdata(struct nl_helper *h, uint32_t ifindex, struct snl_parsed_link_simple *link)
314 struct snl_state *ss = &h->ss_cmd;
315 struct snl_writer nw;
317 snl_init_writer(ss, &nw);
318 struct nlmsghdr *hdr = snl_create_msg_request(&nw, NL_RTM_GETLINK);
319 struct ifinfomsg *ifmsg = snl_reserve_msg_object(&nw, struct ifinfomsg);
321 ifmsg->ifi_index = ifindex;
322 if (!snl_finalize_msg(&nw) || !snl_send_message(ss, hdr))
325 hdr = snl_read_reply(ss, hdr->nlmsg_seq);
327 if (hdr != NULL && hdr->nlmsg_type == RTM_NEWLINK) {
328 snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser_simple, link);
331 if (link->ifla_ifname == NULL) {
334 snprintf(ifname, sizeof(ifname), "if#%u", ifindex);
335 int len = strlen(ifname);
336 char *buf = snl_allocz(ss, len + 1);
337 strlcpy(buf, ifname, len + 1);
338 link->ifla_ifname = buf;
343 print_getmsg(struct nl_helper *h, struct nlmsghdr *hdr, struct sockaddr *dst)
345 struct snl_state *ss = &h->ss_cmd;
347 struct snl_parsed_route r = { .rtax_weight = RT_DEFAULT_WEIGHT };
349 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &r))
352 struct snl_parsed_link_simple link = {};
353 get_ifdata(h, r.rta_oif, &link);
356 r.rtax_mtu = link.ifla_mtu;
357 r.rta_rtflags |= (RTF_UP | RTF_DONE);
359 (void)printf(" route to: %s\n", routename(dst));
362 (void)printf("destination: %s\n", routename(r.rta_dst));
363 struct sockaddr *mask = get_netmask(ss, r.rtm_family, r.rtm_dst_len);
365 (void)printf(" mask: %s\n", routename(mask));
366 if (r.rta_gw && (r.rta_rtflags & RTF_GATEWAY))
367 (void)printf(" gateway: %s\n", routename(r.rta_gw));
368 (void)printf(" fib: %u\n", (unsigned int)r.rta_table);
369 if (link.ifla_ifname)
370 (void)printf(" interface: %s\n", link.ifla_ifname);
371 (void)printf(" flags: ");
372 printb(r.rta_rtflags, routeflags);
374 struct rt_metrics rmx = {
375 .rmx_mtu = r.rtax_mtu,
376 .rmx_weight = r.rtax_weight,
377 .rmx_expire = r.rta_expire,
380 printf("\n%9s %9s %9s %9s %9s %10s %9s\n", "recvpipe",
381 "sendpipe", "ssthresh", "rtt,msec", "mtu ", "weight", "expire");
382 printf("%8lu ", rmx.rmx_recvpipe);
383 printf("%8lu ", rmx.rmx_sendpipe);
384 printf("%8lu ", rmx.rmx_ssthresh);
385 printf("%8lu ", 0UL);
386 printf("%8lu ", rmx.rmx_mtu);
387 printf("%8lu ", rmx.rmx_weight);
388 if (rmx.rmx_expire > 0)
389 clock_gettime(CLOCK_REALTIME_FAST, &ts);
392 printf("%8ld \n", (long)(rmx.rmx_expire - ts.tv_sec));
396 print_prefix(struct nl_helper *h, char *buf, int bufsize, struct sockaddr *sa, int plen)
401 snprintf(buf, bufsize, "<NULL>");
405 switch (sa->sa_family) {
408 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
409 char abuf[INET_ADDRSTRLEN];
411 inet_ntop(AF_INET, &sin->sin_addr, abuf, sizeof(abuf));
412 sz = snprintf(buf, bufsize, "%s", abuf);
417 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
418 char abuf[INET6_ADDRSTRLEN];
421 inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof(abuf));
422 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
423 struct snl_parsed_link_simple link = {};
425 if (sin6->sin6_scope_id != 0) {
426 get_ifdata(h, sin6->sin6_scope_id, &link);
427 ifname = link.ifla_ifname;
431 sz = snprintf(buf, bufsize, "%s", abuf);
433 sz = snprintf(buf, bufsize, "%s%%%s", abuf, ifname);
437 snprintf(buf, bufsize, "unknown_af#%d", sa->sa_family);
442 snprintf(buf + sz, bufsize - sz, "/%d", plen);
446 print_line_prefix(struct nlmsghdr *hdr, struct snl_msg_info *cinfo,
447 const char *cmd, const char *name)
453 clock_gettime(CLOCK_REALTIME, &tp);
454 localtime_r(&tp.tv_sec, &tm);
456 strftime(buf, sizeof(buf), "%T", &tm);
457 int len = printf("%s.%03ld PID %4u %s %s ", buf, tp.tv_nsec / 1000000,
458 cinfo->process_id, cmd, name);
464 get_action_name(struct nlmsghdr *hdr, int new_cmd)
466 if (hdr->nlmsg_type == new_cmd) {
467 //return ((hdr->nlmsg_flags & NLM_F_REPLACE) ? "replace" : "add");
474 print_nlmsg_route_nhop(struct nl_helper *h, struct snl_parsed_route *r,
475 struct rta_mpath_nh *nh, bool first)
477 // gw 10.0.0.1 ifp vtnet0 mtu 1500 table inet.0
478 if (nh->gw != NULL) {
480 print_prefix(h, gwbuf, sizeof(gwbuf), nh->gw, -1);
481 printf("gw %s ", gwbuf);
484 if (nh->ifindex != 0) {
485 struct snl_parsed_link_simple link = {};
487 get_ifdata(h, nh->ifindex, &link);
488 if (nh->rtax_mtu == 0)
489 nh->rtax_mtu = link.ifla_mtu;
490 printf("iface %s ", link.ifla_ifname);
491 if (nh->rtax_mtu != 0)
492 printf("mtu %d ", nh->rtax_mtu);
496 switch (r->rtm_family) {
498 printf("table inet.%d", r->rta_table);
501 printf("table inet6.%d", r->rta_table);
510 print_nlmsg_route(struct nl_helper *h, struct nlmsghdr *hdr,
511 struct snl_msg_info *cinfo)
513 struct snl_parsed_route r = { .rtax_weight = RT_DEFAULT_WEIGHT };
514 struct snl_state *ss = &h->ss_cmd;
516 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &r))
519 // 20:19:41.333 add route 10.0.0.0/24 gw 10.0.0.1 ifp vtnet0 mtu 1500 table inet.0
521 const char *cmd = get_action_name(hdr, RTM_NEWROUTE);
522 int len = print_line_prefix(hdr, cinfo, cmd, "route");
525 print_prefix(h, buf, sizeof(buf), r.rta_dst, r.rtm_dst_len);
526 len += strlen(buf) + 1;
529 switch (r.rtm_type) {
531 printf("blackhole\n");
533 case RTN_UNREACHABLE:
534 printf("unreach(reject)\n");
537 printf("prohibit(reject)\n");
541 if (r.rta_multipath.num_nhops != 0) {
544 memset(buf, ' ', sizeof(buf));
547 for (uint32_t i = 0; i < r.rta_multipath.num_nhops; i++) {
548 struct rta_mpath_nh *nh = r.rta_multipath.nhops[i];
552 print_nlmsg_route_nhop(h, &r, nh, first);
556 struct rta_mpath_nh nh = {
558 .ifindex = r.rta_oif,
559 .rtax_mtu = r.rtax_mtu,
562 print_nlmsg_route_nhop(h, &r, &nh, true);
566 static const char *operstate[] = {
567 "UNKNOWN", /* 0, IF_OPER_UNKNOWN */
568 "NOTPRESENT", /* 1, IF_OPER_NOTPRESENT */
569 "DOWN", /* 2, IF_OPER_DOWN */
570 "LLDOWN", /* 3, IF_OPER_LOWERLAYERDOWN */
571 "TESTING", /* 4, IF_OPER_TESTING */
572 "DORMANT", /* 5, IF_OPER_DORMANT */
573 "UP", /* 6, IF_OPER_UP */
577 print_nlmsg_link(struct nl_helper *h, struct nlmsghdr *hdr,
578 struct snl_msg_info *cinfo)
580 struct snl_parsed_link l = {};
581 struct snl_state *ss = &h->ss_cmd;
583 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser, &l))
586 // 20:19:41.333 add iface#3 vtnet0 admin UP oper UP mtu 1500 table inet.0
587 const char *cmd = get_action_name(hdr, RTM_NEWLINK);
588 print_line_prefix(hdr, cinfo, cmd, "iface");
590 printf("iface#%u %s ", l.ifi_index, l.ifla_ifname);
591 printf("admin %s ", (l.ifi_flags & IFF_UP) ? "UP" : "DOWN");
592 if (l.ifla_operstate < NL_ARRAY_LEN(operstate))
593 printf("oper %s ", operstate[l.ifla_operstate]);
595 printf("mtu %u ", l.ifla_mtu);
601 print_nlmsg_addr(struct nl_helper *h, struct nlmsghdr *hdr,
602 struct snl_msg_info *cinfo)
604 struct snl_parsed_addr attrs = {};
605 struct snl_state *ss = &h->ss_cmd;
607 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_addr_parser, &attrs))
610 // add addr 192.168.1.1/24 iface vtnet0
611 const char *cmd = get_action_name(hdr, RTM_NEWADDR);
612 print_line_prefix(hdr, cinfo, cmd, "addr");
615 struct sockaddr *addr = attrs.ifa_local ? attrs.ifa_local : attrs.ifa_address;
616 print_prefix(h, buf, sizeof(buf), addr, attrs.ifa_prefixlen);
619 struct snl_parsed_link_simple link = {};
620 get_ifdata(h, attrs.ifa_index, &link);
622 if (link.ifi_flags & IFF_POINTOPOINT) {
624 print_prefix(h, buf, sizeof(buf), attrs.ifa_address, -1);
625 printf("-> %s ", buf);
628 printf("iface %s ", link.ifla_ifname);
633 static const char *nudstate[] = {
634 "INCOMPLETE", /* 0x01(0) */
635 "REACHABLE", /* 0x02(1) */
636 "STALE", /* 0x04(2) */
637 "DELAY", /* 0x08(3) */
638 "PROBE", /* 0x10(4) */
639 "FAILED", /* 0x20(5) */
642 #define NUD_INCOMPLETE 0x01 /* No lladdr, address resolution in progress */
643 #define NUD_REACHABLE 0x02 /* reachable & recently resolved */
644 #define NUD_STALE 0x04 /* has lladdr but it's stale */
645 #define NUD_DELAY 0x08 /* has lladdr, is stale, probes delayed */
646 #define NUD_PROBE 0x10 /* has lladdr, is stale, probes sent */
647 #define NUD_FAILED 0x20 /* unused */
651 print_nlmsg_neigh(struct nl_helper *h, struct nlmsghdr *hdr,
652 struct snl_msg_info *cinfo)
654 struct snl_parsed_neigh attrs = {};
655 struct snl_state *ss = &h->ss_cmd;
657 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_neigh_parser, &attrs))
660 // add addr 192.168.1.1 state %s lladdr %s iface vtnet0
661 const char *cmd = get_action_name(hdr, RTM_NEWNEIGH);
662 print_line_prefix(hdr, cinfo, cmd, "neigh");
665 print_prefix(h, buf, sizeof(buf), attrs.nda_dst, -1);
668 struct snl_parsed_link_simple link = {};
669 get_ifdata(h, attrs.nda_ifindex, &link);
671 for (unsigned int i = 0; i < NL_ARRAY_LEN(nudstate); i++) {
672 if ((1 << i) & attrs.ndm_state) {
673 printf("state %s ", nudstate[i]);
678 if (attrs.nda_lladdr != NULL) {
679 int if_type = link.ifi_type;
681 if ((if_type == IFT_ETHER || if_type == IFT_L2VLAN || if_type == IFT_BRIDGE) &&
682 NLA_DATA_LEN(attrs.nda_lladdr) == ETHER_ADDR_LEN) {
683 struct ether_addr *ll;
685 ll = (struct ether_addr *)NLA_DATA(attrs.nda_lladdr);
686 printf("lladdr %s ", ether_ntoa(ll));
688 struct sockaddr_dl sdl = {
689 .sdl_len = sizeof(sdl),
690 .sdl_family = AF_LINK,
691 .sdl_index = attrs.nda_ifindex,
693 .sdl_alen = NLA_DATA_LEN(attrs.nda_lladdr),
695 if (sdl.sdl_alen < sizeof(sdl.sdl_data)) {
696 void *ll = NLA_DATA(attrs.nda_lladdr);
698 memcpy(sdl.sdl_data, ll, sdl.sdl_alen);
699 printf("lladdr %s ", link_ntoa(&sdl));
704 if (link.ifla_ifname != NULL)
705 printf("iface %s ", link.ifla_ifname);
710 print_nlmsg_generic(struct nl_helper *h, struct nlmsghdr *hdr, struct snl_msg_info *cinfo)
712 const char *cmd = get_action_name(hdr, 0);
713 print_line_prefix(hdr, cinfo, cmd, "unknown message");
714 printf(" type %u\n", hdr->nlmsg_type);
718 print_nlmsg(struct nl_helper *h, struct nlmsghdr *hdr, struct snl_msg_info *cinfo)
720 switch (hdr->nlmsg_type) {
723 print_nlmsg_link(h, hdr, cinfo);
727 print_nlmsg_addr(h, hdr, cinfo);
731 print_nlmsg_route(h, hdr, cinfo);
735 print_nlmsg_neigh(h, hdr, cinfo);
738 print_nlmsg_generic(h, hdr, cinfo);
741 snl_clear_lb(&h->ss_cmd);
747 struct snl_state ss_event = {};
750 nl_init_socket(&ss_event);
768 socklen_t optlen = sizeof(optval);
769 setsockopt(ss_event.fd, SOL_NETLINK, NETLINK_MSG_INFO, &optval, optlen);
771 for (unsigned int i = 0; i < NL_ARRAY_LEN(groups); i++) {
773 int optval = groups[i];
774 socklen_t optlen = sizeof(optval);
775 error = setsockopt(ss_event.fd, SOL_NETLINK,
776 NETLINK_ADD_MEMBERSHIP, &optval, optlen);
778 warn("Unable to subscribe to group %d", optval);
781 struct snl_msg_info attrs = {};
782 struct nlmsghdr *hdr;
783 while ((hdr = snl_read_message_dbg(&ss_event, &attrs)) != NULL)
785 print_nlmsg(&h, hdr, &attrs);
786 snl_clear_lb(&h.ss_cmd);
787 snl_clear_lb(&ss_event);
796 print_flushed_route(struct snl_parsed_route *r, struct sockaddr *gw)
798 struct sockaddr *sa = r->rta_dst;
800 printf("%-20.20s ", r->rta_rtflags & RTF_HOST ?
801 routename(sa) : netname(sa));
803 printf("%-20.20s ", routename(sa));
804 printf("-fib %-3d ", r->rta_table);
809 flushroute_one(struct nl_helper *h, struct snl_parsed_route *r)
811 struct snl_state *ss = &h->ss_cmd;
812 struct snl_errmsg_data e = {};
813 struct snl_writer nw;
815 snl_init_writer(ss, &nw);
817 struct nlmsghdr *hdr = snl_create_msg_request(&nw, NL_RTM_DELROUTE);
818 struct rtmsg *rtm = snl_reserve_msg_object(&nw, struct rtmsg);
819 rtm->rtm_family = r->rtm_family;
820 rtm->rtm_dst_len = r->rtm_dst_len;
822 snl_add_msg_attr_u32(&nw, RTA_TABLE, r->rta_table);
823 snl_add_msg_attr_ip(&nw, RTA_DST, r->rta_dst);
825 if (!snl_finalize_msg(&nw) || !snl_send_message(ss, hdr))
828 if (!snl_read_reply_code(ss, hdr->nlmsg_seq, &e)) {
830 if (e.error == EPERM)
831 errc(1, e.error, "RTM_DELROUTE failed:");
833 warnc(e.error, "RTM_DELROUTE failed:");
838 struct snl_msg_info attrs = {};
839 print_nlmsg(h, hdr, &attrs);
842 if (r->rta_multipath.num_nhops != 0) {
843 for (uint32_t i = 0; i < r->rta_multipath.num_nhops; i++) {
844 struct rta_mpath_nh *nh = r->rta_multipath.nhops[i];
846 print_flushed_route(r, nh->gw);
850 print_flushed_route(r, r->rta_gw);
857 flushroutes_fib_nl(int fib, int af)
859 struct snl_state ss = {};
860 struct snl_writer nw;
861 struct nl_helper h = {};
864 snl_init_writer(&ss, &nw);
866 struct nlmsghdr *hdr = snl_create_msg_request(&nw, NL_RTM_GETROUTE);
867 hdr->nlmsg_flags |= NLM_F_DUMP;
868 struct rtmsg *rtm = snl_reserve_msg_object(&nw, struct rtmsg);
869 rtm->rtm_family = af;
870 snl_add_msg_attr_u32(&nw, RTA_TABLE, fib);
872 if (!snl_finalize_msg(&nw) || !snl_send_message(&ss, hdr)) {
877 struct snl_errmsg_data e = {};
878 uint32_t nlm_seq = hdr->nlmsg_seq;
882 while ((hdr = snl_read_reply_multi(&ss, nlm_seq, &e)) != NULL) {
883 struct snl_parsed_route r = { .rtax_weight = RT_DEFAULT_WEIGHT };
886 if (!snl_parse_nlmsg(&ss, hdr, &snl_rtm_route_parser, &r))
889 struct snl_msg_info attrs = {};
890 print_nlmsg(&h, hdr, &attrs);
892 if (r.rta_table != (uint32_t)fib || r.rtm_family != af)
894 if ((r.rta_rtflags & RTF_GATEWAY) == 0)
899 if ((error = flushroute_one(&h, &r)) != 0) {
901 errc(1, error, "RTM_DELROUTE failed:");
903 warnc(error, "RTM_DELROUTE failed:");
905 snl_clear_lb(&h.ss_cmd);