From nobody Sat Mar 18 13:11:24 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Pf1bc3Nbnz3yYlp; Sat, 18 Mar 2023 13:11:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Pf1bc2pJfz47cJ; Sat, 18 Mar 2023 13:11:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679145084; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XZG0xpekrlW31EwDLbstbMjsOfdDJJEas2QJG1hoZss=; b=YCVI2YORniWylA/dwy81pZHiruFmRgoFmr20GAyv2m+W2sJev7pBnrYelNPUSXq6h/XAyj a3JU7yJytpiknRMFbylQ1rc03KjR+qSZPXjCe/Yb90+tCB4ajuJxuuBQg2VdR9SgowJHEP PZeDD0yO95AMPzbMjEbkIxn4srwOJ9FuB10kXZcOGPogEe4jyfq2cRhG7yF9ygXQdKG30X i0+LX8+xEM+wpntdvIAN/V9FEUrT8cVQd75/40kvf5i+chDYJqFWJN4wzp0nWF5bHDx6bS hTplDO6QVdRqpA+RtRK2Ht1IRY+6oUUm/H6/bzhUsYih4/nYed5I+SeQAkSX6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679145084; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XZG0xpekrlW31EwDLbstbMjsOfdDJJEas2QJG1hoZss=; b=Vepc7i6BPIUYeZVfqQkKB5IujI8P+/PLcNu1pTqfnMVcKRk2k/crNNvAZ5yxFEfyflEdnt 0T+RUxp8Rhb35rquH+wWtsr03M3YXN7ug06YpdY/t3sQEjo/cPRio8r3/NsjzPeqEZTjWW f0PnFsX5QiGhdMqWyi5pi5R4B2oqE+a779YAMevwmy/07YkJJQOeqmEE9TNpOobmfCZZeM Z1JPVd+DFbJn7MTzofO8emb06ylmKQZCSSHiHjpwlghLanL9fVjYu+Yzgdq6BndKbhXOWC KE8i5neX6g6tuVNx7+j3N6r2euSd9T0kX1bstuGhBjPkU0HftxMWlnVMYTHINA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679145084; a=rsa-sha256; cv=none; b=n2gQZusUmI7GqWwWUU3ql5l8bsOEH9QFtD7/xkakEXtX3CqCCvQ48PTD6LoobRh0Ro8tq0 DLnWCyJ0FGmy4RTPVXesDPyO6jnKSLuLauO7bcF29Dm2I7hxnSxTLNi0h5K5/VdIGE0zcM po4RT5A4xeJI8miNnh9LwbhCJkDYdGk8R3a8uMa0YqHOq6D1Q9fSWXA5Gw332lnsyw7D0x FXrluHMMcZQDWKxPuyVYW3Autkpr7sZrjlrH4y5rpl8MZ5JAyIByP8JLXZpx36TRUVo6YF SWphj5/6bSFGZII9lvR4X9RAaVVgN2kvfxjfRtTRybQj/zkynVhc8QVRPifkNg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Pf1bc1n3mz1710; Sat, 18 Mar 2023 13:11:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 32IDBOiC009805; Sat, 18 Mar 2023 13:11:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32IDBOld009804; Sat, 18 Mar 2023 13:11:24 GMT (envelope-from git) Date: Sat, 18 Mar 2023 13:11:24 GMT Message-Id: <202303181311.32IDBOld009804@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: aee2f11bf4b4 - main - netstat: simplify netlink route printing code. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: aee2f11bf4b44b286a88b6726347bd7466c5e750 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=aee2f11bf4b44b286a88b6726347bd7466c5e750 commit aee2f11bf4b44b286a88b6726347bd7466c5e750 Author: Alexander V. Chernikov AuthorDate: 2023-03-18 13:05:41 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-03-18 13:05:41 +0000 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. --- usr.bin/netstat/route_netlink.c | 207 ++++------------------------------------ 1 file changed, 17 insertions(+), 190 deletions(-) diff --git a/usr.bin/netstat/route_netlink.c b/usr.bin/netstat/route_netlink.c index ea50d8076182..3546603ca1cc 100644 --- a/usr.bin/netstat/route_netlink.c +++ b/usr.bin/netstat/route_netlink.c @@ -47,6 +47,8 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include +#include #include #include @@ -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) {