1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2023 Alexander V. Chernikov <melifaro@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 #ifndef _NETLINK_NETLINK_SNL_ROUTE_PARSERS_H_ 28 #define _NETLINK_NETLINK_SNL_ROUTE_PARSERS_H_ 29 30 #include <netlink/netlink_snl.h> 31 #include <netlink/netlink_route.h> 32 33 /* TODO: this file should be generated automatically */ 34 35 /* RTM_<NEW|DEL|GET>ROUTE message parser */ 36 37 struct rta_mpath_nh { 38 struct sockaddr *gw; 39 uint32_t ifindex; 40 uint8_t rtnh_flags; 41 uint8_t rtnh_weight; 42 uint32_t rtax_mtu; 43 uint32_t rta_rtflags; 44 }; 45 46 #define _IN(_field) offsetof(struct rtnexthop, _field) 47 #define _OUT(_field) offsetof(struct rta_mpath_nh, _field) 48 static const struct snl_attr_parser _nla_p_mp_nh_metrics[] = { 49 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 }, 50 }; 51 SNL_DECLARE_ATTR_PARSER(_metrics_mp_nh_parser, _nla_p_mp_nh_metrics); 52 53 static const struct snl_attr_parser _nla_p_mp_nh[] = { 54 { .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = snl_attr_get_ip }, 55 { .type = NL_RTA_METRICS, .arg = &_metrics_mp_nh_parser, .cb = snl_attr_get_nested }, 56 { .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 }, 57 { .type = NL_RTA_VIA, .off = _OUT(gw), .cb = snl_attr_get_ipvia }, 58 }; 59 60 static const struct snl_field_parser _fp_p_mp_nh[] = { 61 { .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = snl_field_get_uint8 }, 62 { .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = snl_field_get_uint8 }, 63 { .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifindex), .cb = snl_field_get_uint32 }, 64 }; 65 #undef _IN 66 #undef _OUT 67 SNL_DECLARE_PARSER(_mpath_nh_parser, struct rtnexthop, _fp_p_mp_nh, _nla_p_mp_nh); 68 69 struct rta_mpath { 70 int num_nhops; 71 struct rta_mpath_nh nhops[0]; 72 }; 73 74 static bool 75 nlattr_get_multipath(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target) 76 { 77 int data_len = nla->nla_len - sizeof(struct nlattr); 78 struct rtnexthop *rtnh; 79 80 int max_nhops = data_len / sizeof(struct rtnexthop); 81 size_t sz = (max_nhops + 2) * sizeof(struct rta_mpath_nh); 82 83 struct rta_mpath *mp = snl_allocz(ss, sz); 84 mp->num_nhops = 0; 85 86 for (rtnh = (struct rtnexthop *)(nla + 1); data_len > 0; ) { 87 struct rta_mpath_nh *mpnh = &mp->nhops[mp->num_nhops++]; 88 89 if (!snl_parse_header(ss, rtnh, rtnh->rtnh_len, &_mpath_nh_parser, mpnh)) 90 return (false); 91 92 int len = NL_ITEM_ALIGN(rtnh->rtnh_len); 93 data_len -= len; 94 rtnh = (struct rtnexthop *)((char *)rtnh + len); 95 } 96 if (data_len != 0 || mp->num_nhops == 0) { 97 return (false); 98 } 99 100 *((struct rta_mpath **)target) = mp; 101 return (true); 102 } 103 104 struct snl_parsed_route { 105 struct sockaddr *rta_dst; 106 struct sockaddr *rta_gw; 107 struct nlattr *rta_metrics; 108 struct rta_mpath *rta_multipath; 109 uint32_t rta_expires; 110 uint32_t rta_oif; 111 uint32_t rta_expire; 112 uint32_t rta_table; 113 uint32_t rta_knh_id; 114 uint32_t rta_nh_id; 115 uint32_t rta_rtflags; 116 uint32_t rtax_mtu; 117 uint32_t rtax_weight; 118 uint8_t rtm_family; 119 uint8_t rtm_type; 120 uint8_t rtm_protocol; 121 uint8_t rtm_dst_len; 122 }; 123 124 #define _IN(_field) offsetof(struct rtmsg, _field) 125 #define _OUT(_field) offsetof(struct snl_parsed_route, _field) 126 static const struct snl_attr_parser _nla_p_rtmetrics[] = { 127 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 }, 128 }; 129 SNL_DECLARE_ATTR_PARSER(_metrics_parser, _nla_p_rtmetrics); 130 131 static const struct snl_attr_parser _nla_p_route[] = { 132 { .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = snl_attr_get_ip }, 133 { .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = snl_attr_get_uint32 }, 134 { .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = snl_attr_get_ip }, 135 { .type = NL_RTA_METRICS, .arg = &_metrics_parser, .cb = snl_attr_get_nested }, 136 { .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath }, 137 { .type = NL_RTA_KNH_ID, .off = _OUT(rta_knh_id), .cb = snl_attr_get_uint32 }, 138 { .type = NL_RTA_WEIGHT, .off = _OUT(rtax_weight), .cb = snl_attr_get_uint32 }, 139 { .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 }, 140 { .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = snl_attr_get_uint32 }, 141 { .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = snl_attr_get_ipvia }, 142 { .type = NL_RTA_EXPIRES, .off = _OUT(rta_expire), .cb = snl_attr_get_uint32 }, 143 { .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = snl_attr_get_uint32 }, 144 }; 145 146 static const struct snl_field_parser _fp_p_route[] = { 147 {.off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = snl_field_get_uint8 }, 148 {.off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = snl_field_get_uint8 }, 149 {.off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = snl_field_get_uint8 }, 150 {.off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = snl_field_get_uint8 }, 151 }; 152 #undef _IN 153 #undef _OUT 154 SNL_DECLARE_PARSER(snl_rtm_route_parser, struct rtmsg, _fp_p_route, _nla_p_route); 155 156 /* RTM_<NEW|DEL|GET>LINK message parser */ 157 struct snl_parsed_link { 158 uint32_t ifi_index; 159 uint32_t ifi_flags; 160 uint32_t ifi_change; 161 uint16_t ifi_type; 162 uint8_t ifla_operstate; 163 uint8_t ifla_carrier; 164 uint32_t ifla_mtu; 165 char *ifla_ifname; 166 struct nlattr *ifla_address; 167 struct nlattr *ifla_broadcast; 168 char *ifla_ifalias; 169 uint32_t ifla_promiscuity; 170 struct rtnl_link_stats64 *ifla_stats64; 171 }; 172 173 #define _IN(_field) offsetof(struct ifinfomsg, _field) 174 #define _OUT(_field) offsetof(struct snl_parsed_link, _field) 175 static const struct snl_attr_parser _nla_p_link[] = { 176 { .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = snl_attr_get_nla }, 177 { .type = IFLA_BROADCAST, .off = _OUT(ifla_broadcast), .cb = snl_attr_get_nla }, 178 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string }, 179 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 }, 180 { .type = IFLA_OPERSTATE, .off = _OUT(ifla_operstate), .cb = snl_attr_get_uint8 }, 181 { .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = snl_attr_get_string }, 182 { .type = IFLA_STATS64, .off = _OUT(ifla_stats64), .cb = snl_attr_copy_struct }, 183 { .type = IFLA_PROMISCUITY, .off = _OUT(ifla_promiscuity), .cb = snl_attr_get_uint32 }, 184 { .type = IFLA_CARRIER, .off = _OUT(ifla_carrier), .cb = snl_attr_get_uint8 }, 185 }; 186 static const struct snl_field_parser _fp_p_link[] = { 187 {.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 }, 188 {.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 }, 189 {.off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = snl_field_get_uint32 }, 190 {.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 }, 191 }; 192 #undef _IN 193 #undef _OUT 194 SNL_DECLARE_PARSER(snl_rtm_link_parser, struct ifinfomsg, _fp_p_link, _nla_p_link); 195 196 struct snl_parsed_link_simple { 197 uint32_t ifi_index; 198 uint32_t ifla_mtu; 199 uint16_t ifi_type; 200 char *ifla_ifname; 201 }; 202 203 #define _IN(_field) offsetof(struct ifinfomsg, _field) 204 #define _OUT(_field) offsetof(struct snl_parsed_link_simple, _field) 205 static struct snl_attr_parser _nla_p_link_s[] = { 206 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string }, 207 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 }, 208 }; 209 static struct snl_field_parser _fp_p_link_s[] = { 210 {.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 }, 211 {.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 }, 212 }; 213 #undef _IN 214 #undef _OUT 215 SNL_DECLARE_PARSER(snl_rtm_link_parser_simple, struct ifinfomsg, _fp_p_link_s, _nla_p_link_s); 216 217 static const struct snl_hdr_parser *snl_all_route_parsers[] = { 218 &_metrics_mp_nh_parser, &_mpath_nh_parser, &_metrics_parser, &snl_rtm_route_parser, 219 &snl_rtm_link_parser, &snl_rtm_link_parser_simple, 220 }; 221 222 #endif 223