Path: blob/main/sys/netlink/netlink_snl_route_parsers.h
39475 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2023 Alexander V. Chernikov <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/26#ifndef _NETLINK_NETLINK_SNL_ROUTE_PARSERS_H_27#define _NETLINK_NETLINK_SNL_ROUTE_PARSERS_H_2829#include <netlink/netlink_snl.h>30#include <netlink/netlink_snl_route.h>31#include <netlink/route/nexthop.h>3233/* TODO: this file should be generated automatically */3435static inline void36finalize_sockaddr(struct sockaddr *sa, uint32_t ifindex)37{38if (sa != NULL && sa->sa_family == AF_INET6) {39struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)sa;4041if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))42sin6->sin6_scope_id = ifindex;43}44}4546/* RTM_<NEW|DEL|GET>ROUTE message parser */4748struct rta_mpath_nh {49struct sockaddr *gw;50uint32_t ifindex;51uint8_t rtnh_flags;52uint8_t rtnh_weight;53uint32_t rtax_mtu;54uint32_t rta_rtflags;55};5657#define _IN(_field) offsetof(struct rtnexthop, _field)58#define _OUT(_field) offsetof(struct rta_mpath_nh, _field)59static const struct snl_attr_parser _nla_p_mp_nh_metrics[] = {60{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 },61};62SNL_DECLARE_ATTR_PARSER(_metrics_mp_nh_parser, _nla_p_mp_nh_metrics);6364static const struct snl_attr_parser _nla_p_mp_nh[] = {65{ .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = snl_attr_get_ip },66{ .type = NL_RTA_METRICS, .arg = &_metrics_mp_nh_parser, .cb = snl_attr_get_nested },67{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 },68{ .type = NL_RTA_VIA, .off = _OUT(gw), .cb = snl_attr_get_ipvia },69};7071static const struct snl_field_parser _fp_p_mp_nh[] = {72{ .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = snl_field_get_uint8 },73{ .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = snl_field_get_uint8 },74{ .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifindex), .cb = snl_field_get_uint32 },75};7677static inline bool78_cb_p_mp_nh(struct snl_state *ss __unused, void *_target)79{80struct rta_mpath_nh *target = (struct rta_mpath_nh *)_target;8182finalize_sockaddr(target->gw, target->ifindex);83return (true);84}85#undef _IN86#undef _OUT87SNL_DECLARE_PARSER_EXT(_mpath_nh_parser, sizeof(struct rtnexthop),88sizeof(struct rta_mpath_nh), _fp_p_mp_nh, _nla_p_mp_nh,89_cb_p_mp_nh);9091struct rta_mpath {92uint32_t num_nhops;93struct rta_mpath_nh **nhops;94};9596static bool97nlattr_get_multipath(struct snl_state *ss, struct nlattr *nla,98const void *arg __unused, void *target)99{100uint32_t start_size = 4;101102while (start_size < NLA_DATA_LEN(nla) / sizeof(struct rtnexthop))103start_size *= 2;104105return (snl_attr_get_parray_sz(ss, nla, start_size, &_mpath_nh_parser, target));106}107108struct snl_parsed_route {109struct sockaddr *rta_dst;110struct sockaddr *rta_gw;111struct nlattr *rta_metrics;112struct rta_mpath rta_multipath;113uint32_t rta_expires;114uint32_t rta_oif;115uint32_t rta_expire;116uint32_t rta_table;117uint32_t rta_knh_id;118uint32_t rta_nh_id;119uint32_t rta_rtflags;120uint32_t rtax_mtu;121uint32_t rtax_weight;122uint8_t rtm_family;123uint8_t rtm_type;124uint8_t rtm_protocol;125uint8_t rtm_dst_len;126};127128#define _IN(_field) offsetof(struct rtmsg, _field)129#define _OUT(_field) offsetof(struct snl_parsed_route, _field)130static const struct snl_attr_parser _nla_p_rtmetrics[] = {131{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 },132};133SNL_DECLARE_ATTR_PARSER(_metrics_parser, _nla_p_rtmetrics);134135static const struct snl_attr_parser _nla_p_route[] = {136{ .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = snl_attr_get_ip },137{ .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = snl_attr_get_uint32 },138{ .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = snl_attr_get_ip },139{ .type = NL_RTA_METRICS, .arg = &_metrics_parser, .cb = snl_attr_get_nested },140{ .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath },141{ .type = NL_RTA_KNH_ID, .off = _OUT(rta_knh_id), .cb = snl_attr_get_uint32 },142{ .type = NL_RTA_WEIGHT, .off = _OUT(rtax_weight), .cb = snl_attr_get_uint32 },143{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 },144{ .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = snl_attr_get_uint32 },145{ .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = snl_attr_get_ipvia },146{ .type = NL_RTA_EXPIRES, .off = _OUT(rta_expire), .cb = snl_attr_get_uint32 },147{ .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = snl_attr_get_uint32 },148};149150static const struct snl_field_parser _fp_p_route[] = {151{.off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = snl_field_get_uint8 },152{.off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = snl_field_get_uint8 },153{.off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = snl_field_get_uint8 },154{.off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = snl_field_get_uint8 },155};156157static inline bool158_cb_p_route(struct snl_state *ss __unused, void *_target)159{160struct snl_parsed_route *target = (struct snl_parsed_route *)_target;161162finalize_sockaddr(target->rta_dst, target->rta_oif);163finalize_sockaddr(target->rta_gw, target->rta_oif);164return (true);165}166#undef _IN167#undef _OUT168SNL_DECLARE_PARSER_EXT(snl_rtm_route_parser, sizeof(struct rtmsg),169sizeof(struct snl_parsed_route), _fp_p_route, _nla_p_route,170_cb_p_route);171172/* RTM_<NEW|DEL|GET>LINK message parser */173struct snl_parsed_link {174uint32_t ifi_index;175uint32_t ifi_flags;176uint32_t ifi_change;177uint16_t ifi_type;178uint8_t ifla_operstate;179uint8_t ifla_carrier;180uint32_t ifla_mtu;181char *ifla_ifname;182struct nlattr *ifla_address;183struct nlattr *ifla_broadcast;184char *ifla_ifalias;185uint32_t ifla_promiscuity;186struct rtnl_link_stats64 *ifla_stats64;187struct nlattr *iflaf_orig_hwaddr;188struct snl_attr_bitset iflaf_caps;189};190191#define _IN(_field) offsetof(struct ifinfomsg, _field)192#define _OUT(_field) offsetof(struct snl_parsed_link, _field)193static const struct snl_attr_parser _nla_p_link_fbsd[] = {194{ .type = IFLAF_ORIG_HWADDR, .off = _OUT(iflaf_orig_hwaddr), .cb = snl_attr_dup_nla },195{ .type = IFLAF_CAPS, .off = _OUT(iflaf_caps), .cb = snl_attr_get_bitset_c },196};197SNL_DECLARE_ATTR_PARSER(_link_fbsd_parser, _nla_p_link_fbsd);198199static const struct snl_attr_parser _nla_p_link[] = {200{ .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = snl_attr_dup_nla },201{ .type = IFLA_BROADCAST, .off = _OUT(ifla_broadcast), .cb = snl_attr_dup_nla },202{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_dup_string },203{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },204{ .type = IFLA_OPERSTATE, .off = _OUT(ifla_operstate), .cb = snl_attr_get_uint8 },205{ .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = snl_attr_dup_string },206{ .type = IFLA_STATS64, .off = _OUT(ifla_stats64), .cb = snl_attr_dup_struct },207{ .type = IFLA_PROMISCUITY, .off = _OUT(ifla_promiscuity), .cb = snl_attr_get_uint32 },208{ .type = IFLA_CARRIER, .off = _OUT(ifla_carrier), .cb = snl_attr_get_uint8 },209{ .type = IFLA_FREEBSD, .arg = &_link_fbsd_parser, .cb = snl_attr_get_nested },210};211static const struct snl_field_parser _fp_p_link[] = {212{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },213{.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 },214{.off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = snl_field_get_uint32 },215{.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 },216};217#undef _IN218#undef _OUT219SNL_DECLARE_PARSER(snl_rtm_link_parser, struct ifinfomsg, _fp_p_link, _nla_p_link);220221struct snl_parsed_link_simple {222uint32_t ifi_index;223uint32_t ifla_mtu;224uint16_t ifi_type;225uint32_t ifi_flags;226char *ifla_ifname;227};228229#define _IN(_field) offsetof(struct ifinfomsg, _field)230#define _OUT(_field) offsetof(struct snl_parsed_link_simple, _field)231static struct snl_attr_parser _nla_p_link_s[] = {232{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_dup_string },233{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },234};235static struct snl_field_parser _fp_p_link_s[] = {236{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },237{.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 },238{.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 },239};240#undef _IN241#undef _OUT242SNL_DECLARE_PARSER(snl_rtm_link_parser_simple, struct ifinfomsg, _fp_p_link_s, _nla_p_link_s);243244struct snl_parsed_neigh {245uint8_t ndm_family;246uint8_t ndm_flags;247uint16_t ndm_state;248uint32_t nda_ifindex;249uint32_t nda_probes;250uint32_t ndaf_next_ts;251struct sockaddr *nda_dst;252struct nlattr *nda_lladdr;253};254255#define _IN(_field) offsetof(struct ndmsg, _field)256#define _OUT(_field) offsetof(struct snl_parsed_neigh, _field)257static const struct snl_attr_parser _nla_p_neigh_fbsd[] = {258{ .type = NDAF_NEXT_STATE_TS, .off = _OUT(ndaf_next_ts), .cb = snl_attr_get_uint32 },259};260SNL_DECLARE_ATTR_PARSER(_neigh_fbsd_parser, _nla_p_neigh_fbsd);261262static struct snl_attr_parser _nla_p_neigh_s[] = {263{ .type = NDA_DST, .off = _OUT(nda_dst), .cb = snl_attr_get_ip },264{ .type = NDA_LLADDR , .off = _OUT(nda_lladdr), .cb = snl_attr_dup_nla },265{ .type = NDA_PROBES, .off = _OUT(nda_probes), .cb = snl_attr_get_uint32 },266{ .type = NDA_IFINDEX, .off = _OUT(nda_ifindex), .cb = snl_attr_get_uint32 },267{ .type = NDA_FREEBSD, .arg = &_neigh_fbsd_parser, .cb = snl_attr_get_nested },268};269static struct snl_field_parser _fp_p_neigh_s[] = {270{.off_in = _IN(ndm_family), .off_out = _OUT(ndm_family), .cb = snl_field_get_uint8 },271{.off_in = _IN(ndm_flags), .off_out = _OUT(ndm_flags), .cb = snl_field_get_uint8 },272{.off_in = _IN(ndm_state), .off_out = _OUT(ndm_state), .cb = snl_field_get_uint16 },273{.off_in = _IN(ndm_ifindex), .off_out = _OUT(nda_ifindex), .cb = snl_field_get_uint32 },274};275276static inline bool277_cb_p_neigh(struct snl_state *ss __unused, void *_target)278{279struct snl_parsed_neigh *target = (struct snl_parsed_neigh *)_target;280281finalize_sockaddr(target->nda_dst, target->nda_ifindex);282return (true);283}284#undef _IN285#undef _OUT286SNL_DECLARE_PARSER_EXT(snl_rtm_neigh_parser, sizeof(struct ndmsg),287sizeof(struct snl_parsed_neigh), _fp_p_neigh_s, _nla_p_neigh_s,288_cb_p_neigh);289290struct snl_parsed_addr {291uint8_t ifa_family;292uint8_t ifa_prefixlen;293uint32_t ifa_index;294struct sockaddr *ifa_local;295struct sockaddr *ifa_address;296struct sockaddr *ifa_broadcast;297char *ifa_label;298struct ifa_cacheinfo *ifa_cacheinfo;299uint32_t ifaf_vhid;300uint32_t ifaf_flags;301};302303#define _IN(_field) offsetof(struct ifaddrmsg, _field)304#define _OUT(_field) offsetof(struct snl_parsed_addr, _field)305static const struct snl_attr_parser _nla_p_addr_fbsd[] = {306{ .type = IFAF_VHID, .off = _OUT(ifaf_vhid), .cb = snl_attr_get_uint32 },307{ .type = IFAF_FLAGS, .off = _OUT(ifaf_flags), .cb = snl_attr_get_uint32 },308};309SNL_DECLARE_ATTR_PARSER(_addr_fbsd_parser, _nla_p_addr_fbsd);310311static const struct snl_attr_parser _nla_p_addr_s[] = {312{ .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = snl_attr_get_ip },313{ .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = snl_attr_get_ip },314{ .type = IFA_LABEL, .off = _OUT(ifa_label), .cb = snl_attr_dup_string },315{ .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = snl_attr_get_ip },316{ .type = IFA_CACHEINFO, .off = _OUT(ifa_cacheinfo), .cb = snl_attr_dup_struct },317{ .type = IFA_FREEBSD, .arg = &_addr_fbsd_parser, .cb = snl_attr_get_nested },318};319static const struct snl_field_parser _fp_p_addr_s[] = {320{.off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = snl_field_get_uint8 },321{.off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = snl_field_get_uint8 },322{.off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = snl_field_get_uint32 },323};324325static inline bool326_cb_p_addr(struct snl_state *ss __unused, void *_target)327{328struct snl_parsed_addr *target = (struct snl_parsed_addr *)_target;329330finalize_sockaddr(target->ifa_address, target->ifa_index);331finalize_sockaddr(target->ifa_local, target->ifa_index);332return (true);333}334#undef _IN335#undef _OUT336SNL_DECLARE_PARSER_EXT(snl_rtm_addr_parser, sizeof(struct ifaddrmsg),337sizeof(struct snl_parsed_addr), _fp_p_addr_s, _nla_p_addr_s,338_cb_p_addr);339340struct snl_parsed_nhop {341uint32_t nha_id;342uint8_t nha_blackhole;343uint8_t nha_groups;344uint8_t nhaf_knhops;345uint8_t nhaf_family;346uint32_t nha_oif;347struct sockaddr *nha_gw;348uint8_t nh_family;349uint8_t nh_protocol;350uint32_t nhaf_table;351uint32_t nhaf_kid;352uint32_t nhaf_aif;353};354355#define _IN(_field) offsetof(struct nhmsg, _field)356#define _OUT(_field) offsetof(struct snl_parsed_nhop, _field)357static struct snl_attr_parser _nla_p_nh_fbsd[] = {358{ .type = NHAF_KNHOPS, .off = _OUT(nhaf_knhops), .cb = snl_attr_get_flag },359{ .type = NHAF_TABLE, .off = _OUT(nhaf_table), .cb = snl_attr_get_uint32 },360{ .type = NHAF_KID, .off = _OUT(nhaf_kid), .cb = snl_attr_get_uint32 },361{ .type = NHAF_AIF, .off = _OUT(nhaf_aif), .cb = snl_attr_get_uint32 },362};363SNL_DECLARE_ATTR_PARSER(_nh_fbsd_parser, _nla_p_nh_fbsd);364365static const struct snl_field_parser _fp_p_nh[] = {366{ .off_in = _IN(nh_family), .off_out = _OUT(nh_family), .cb = snl_field_get_uint8 },367{ .off_in = _IN(nh_protocol), .off_out = _OUT(nh_protocol), .cb = snl_field_get_uint8 },368};369370static const struct snl_attr_parser _nla_p_nh[] = {371{ .type = NHA_ID, .off = _OUT(nha_id), .cb = snl_attr_get_uint32 },372{ .type = NHA_BLACKHOLE, .off = _OUT(nha_blackhole), .cb = snl_attr_get_flag },373{ .type = NHA_OIF, .off = _OUT(nha_oif), .cb = snl_attr_get_uint32 },374{ .type = NHA_GATEWAY, .off = _OUT(nha_gw), .cb = snl_attr_get_ip },375{ .type = NHA_FREEBSD, .arg = &_nh_fbsd_parser, .cb = snl_attr_get_nested },376};377378static inline bool379_cb_p_nh(struct snl_state *ss __unused, void *_target)380{381struct snl_parsed_nhop *target = (struct snl_parsed_nhop *)_target;382383finalize_sockaddr(target->nha_gw, target->nha_oif);384return (true);385}386#undef _IN387#undef _OUT388SNL_DECLARE_PARSER_EXT(snl_nhmsg_parser, sizeof(struct nhmsg),389sizeof(struct snl_parsed_nhop), _fp_p_nh, _nla_p_nh, _cb_p_nh);390391#endif392393394