netstat: simplify netlink route printing code.

A number of improvements has commited to snl(3) recently.
A notable one is snl(3) build-in parsers for all of the objects
 exported by the kernel.

This change updates netlink handling code to the latest available snl(3)
API.
This commit is contained in:
Alexander V. Chernikov 2023-03-18 13:05:41 +00:00
parent 046acc2bfd
commit aee2f11bf4

View File

@ -47,6 +47,8 @@ __FBSDID("$FreeBSD$");
#include <netlink/netlink_route.h>
#include <netlink/netlink_snl.h>
#include <netlink/netlink_snl_route.h>
#include <netlink/netlink_snl_route_parsers.h>
#include <netlink/netlink_snl_route_compat.h>
#include <netinet/in.h>
#include <netgraph/ng_socket.h>
@ -75,25 +77,6 @@ static void p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlm
static struct ifmap_entry *ifmap;
static size_t ifmap_size;
struct nl_parsed_link {
uint32_t ifi_index;
uint32_t ifla_mtu;
char *ifla_ifname;
};
#define _IN(_field) offsetof(struct ifinfomsg, _field)
#define _OUT(_field) offsetof(struct nl_parsed_link, _field)
static struct snl_attr_parser ap_link[] = {
{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string },
{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
};
static struct snl_field_parser fp_link[] = {
{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
};
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER(link_parser, struct ifinfomsg, fp_link, ap_link);
/* Generate ifmap using netlink */
static struct ifmap_entry *
prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
@ -108,7 +91,7 @@ prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
};
msg.hdr.nlmsg_len = sizeof(msg);
if (!snl_send(ss, &msg, sizeof(msg))) {
if (!snl_send_message(ss, &msg.hdr)) {
snl_free(ss);
return (NULL);
}
@ -116,15 +99,12 @@ prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
struct ifmap_entry *ifmap = NULL;
uint32_t ifmap_size = 0;
struct nlmsghdr *hdr;
while ((hdr = snl_read_message(ss)) != NULL && hdr->nlmsg_type != NLMSG_DONE) {
if (hdr->nlmsg_seq != msg.hdr.nlmsg_seq)
continue;
/*
if (hdr->nlmsg_type == NLMSG_ERROR)
break;
*/
struct nl_parsed_link link = {};
if (!snl_parse_nlmsg(ss, hdr, &link_parser, &link))
struct snl_errmsg_data e = {};
while ((hdr = snl_read_reply_multi(ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
struct snl_parsed_link_simple link = {};
if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser_simple, &link))
continue;
if (link.ifi_index >= ifmap_size) {
size_t size = roundup2(link.ifi_index + 1, 32) * sizeof(struct ifmap_entry);
@ -144,140 +124,6 @@ prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
return (ifmap);
}
struct rta_mpath_nh {
struct sockaddr *gw;
uint32_t ifindex;
uint8_t rtnh_flags;
uint8_t rtnh_weight;
uint32_t rtax_mtu;
uint32_t rta_rtflags;
};
#define _IN(_field) offsetof(struct rtnexthop, _field)
#define _OUT(_field) offsetof(struct rta_mpath_nh, _field)
static const struct snl_attr_parser nla_p_mp_rtmetrics[] = {
{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 },
};
SNL_DECLARE_ATTR_PARSER(metrics_mp_parser, nla_p_mp_rtmetrics);
static const struct snl_attr_parser psnh[] = {
{ .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = snl_attr_get_ip },
{ .type = NL_RTA_METRICS, .arg = &metrics_mp_parser, .cb = snl_attr_get_nested },
{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_VIA, .off = _OUT(gw), .cb = snl_attr_get_ipvia },
};
static const struct snl_field_parser fpnh[] = {
{ .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = snl_field_get_uint8 },
{ .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = snl_field_get_uint8 },
{ .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifindex), .cb = snl_field_get_uint32 },
};
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER(mpath_parser, struct rtnexthop, fpnh, psnh);
struct rta_mpath {
int num_nhops;
struct rta_mpath_nh nhops[0];
};
static bool
nlattr_get_multipath(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
{
int data_len = nla->nla_len - sizeof(struct nlattr);
struct rtnexthop *rtnh;
int max_nhops = data_len / sizeof(struct rtnexthop);
size_t sz = (max_nhops + 2) * sizeof(struct rta_mpath_nh);
struct rta_mpath *mp = snl_allocz(ss, sz);
mp->num_nhops = 0;
for (rtnh = (struct rtnexthop *)(nla + 1); data_len > 0; ) {
struct rta_mpath_nh *mpnh = &mp->nhops[mp->num_nhops++];
if (!snl_parse_header(ss, rtnh, rtnh->rtnh_len, &mpath_parser, mpnh))
return (false);
int len = NL_ITEM_ALIGN(rtnh->rtnh_len);
data_len -= len;
rtnh = (struct rtnexthop *)((char *)rtnh + len);
}
if (data_len != 0 || mp->num_nhops == 0) {
return (false);
}
*((struct rta_mpath **)target) = mp;
return (true);
}
struct nl_parsed_route {
struct sockaddr *rta_dst;
struct sockaddr *rta_gw;
struct nlattr *rta_metrics;
struct rta_mpath *rta_multipath;
uint32_t rta_expires;
uint32_t rta_oif;
uint32_t rta_expire;
uint32_t rta_table;
uint32_t rta_knh_id;
uint32_t rta_nh_id;
uint32_t rta_rtflags;
uint32_t rtax_mtu;
uint32_t rtax_weight;
uint8_t rtm_family;
uint8_t rtm_type;
uint8_t rtm_protocol;
uint8_t rtm_dst_len;
};
#define _IN(_field) offsetof(struct rtmsg, _field)
#define _OUT(_field) offsetof(struct nl_parsed_route, _field)
static const struct snl_attr_parser nla_p_rtmetrics[] = {
{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 },
};
SNL_DECLARE_ATTR_PARSER(metrics_parser, nla_p_rtmetrics);
static const struct snl_attr_parser ps[] = {
{ .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = snl_attr_get_ip },
{ .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = snl_attr_get_ip },
{ .type = NL_RTA_METRICS, .arg = &metrics_parser, .cb = snl_attr_get_nested },
{ .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath },
{ .type = NL_RTA_KNH_ID, .off = _OUT(rta_knh_id), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_WEIGHT, .off = _OUT(rtax_weight), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = snl_attr_get_ipvia },
{ .type = NL_RTA_EXPIRES, .off = _OUT(rta_expire), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = snl_attr_get_uint32 },
};
static const struct snl_field_parser fprt[] = {
{.off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = snl_field_get_uint8 },
{.off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = snl_field_get_uint8 },
{.off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = snl_field_get_uint8 },
{.off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = snl_field_get_uint8 },
};
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER(rtm_parser, struct rtmsg, fprt, ps);
#define RTF_UP 0x1
#define RTF_GATEWAY 0x2
#define RTF_HOST 0x4
#define RTF_REJECT 0x8
#define RTF_DYNAMIC 0x10
#define RTF_STATIC 0x800
#define RTF_BLACKHOLE 0x1000
#define RTF_PROTO2 0x4000
#define RTF_PROTO1 0x8000
#define RTF_PROTO3 0x40000
#define RTF_FIXEDMTU 0x80000
#define RTF_PINNED 0x100000
static void
ip6_writemask(struct in6_addr *addr6, uint8_t mask)
{
@ -309,17 +155,6 @@ gen_mask(int family, int plen, struct sockaddr *sa)
}
}
struct sockaddr_dl_short {
u_char sdl_len; /* Total length of sockaddr */
u_char sdl_family; /* AF_LINK */
u_short sdl_index; /* if != 0, system given index for interface */
u_char sdl_type; /* interface type */
u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */
u_char sdl_alen; /* link level address length */
u_char sdl_slen; /* link layer selector length */
char sdl_data[8]; /* unused */
};
static void
add_scopeid(struct sockaddr *sa, int ifindex)
{
@ -331,7 +166,7 @@ add_scopeid(struct sockaddr *sa, int ifindex)
}
static void
p_path(struct nl_parsed_route *rt, bool is_mpath)
p_path(struct snl_parsed_route *rt, bool is_mpath)
{
struct sockaddr_in6 mask6;
struct sockaddr *pmask = (struct sockaddr *)&mask6;
@ -398,8 +233,8 @@ static void
p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr)
{
struct nl_parsed_route rt = {};
if (!snl_parse_nlmsg(ss, hdr, &rtm_parser, &rt))
struct snl_parsed_route rt = {};
if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &rt))
return;
if (rt.rtax_weight == 0)
rt.rtax_weight = rt_default_weight;
@ -424,9 +259,9 @@ p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr)
return;
}
struct sockaddr_dl_short sdl_gw = {
struct sockaddr_dl sdl_gw = {
.sdl_family = AF_LINK,
.sdl_len = sizeof(struct sockaddr_dl_short),
.sdl_len = sizeof(struct sockaddr_dl),
.sdl_index = rt.rta_oif,
};
if (rt.rta_gw == NULL)
@ -438,21 +273,15 @@ p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr)
xo_close_instance(name);
}
static const struct snl_hdr_parser *all_parsers[] = {
&link_parser, &metrics_mp_parser, &mpath_parser, &metrics_parser, &rtm_parser
};
bool
p_rtable_netlink(int fibnum, int af)
{
int fam = AF_UNSPEC;
int need_table_close = false;
struct nlmsghdr *hdr;
struct snl_errmsg_data e = {};
struct snl_state ss = {};
SNL_VERIFY_PARSERS(all_parsers);
if (!snl_init(&ss, NETLINK_ROUTE))
return (false);
@ -474,16 +303,14 @@ p_rtable_netlink(int fibnum, int af)
};
msg.hdr.nlmsg_len = sizeof(msg);
if (!snl_send(&ss, &msg, sizeof(msg))) {
if (!snl_send_message(&ss, &msg.hdr)) {
snl_free(&ss);
return (false);
}
xo_open_container("route-table");
xo_open_list("rt-family");
while ((hdr = snl_read_message(&ss)) != NULL && hdr->nlmsg_type != NLMSG_DONE) {
if (hdr->nlmsg_seq != msg.hdr.nlmsg_seq)
continue;
while ((hdr = snl_read_reply_multi(&ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
struct rtmsg *rtm = (struct rtmsg *)(hdr + 1);
/* Only print family first time. */
if (fam != rtm->rtm_family) {