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_snl_route.h> 32 #include <netlink/route/nexthop.h> 33 34 /* TODO: this file should be generated automatically */ 35 36 /* RTM_<NEW|DEL|GET>ROUTE message parser */ 37 38 struct rta_mpath_nh { 39 struct sockaddr *gw; 40 uint32_t ifindex; 41 uint8_t rtnh_flags; 42 uint8_t rtnh_weight; 43 uint32_t rtax_mtu; 44 uint32_t rta_rtflags; 45 }; 46 47 #define _IN(_field) offsetof(struct rtnexthop, _field) 48 #define _OUT(_field) offsetof(struct rta_mpath_nh, _field) 49 static const struct snl_attr_parser _nla_p_mp_nh_metrics[] = { 50 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 }, 51 }; 52 SNL_DECLARE_ATTR_PARSER(_metrics_mp_nh_parser, _nla_p_mp_nh_metrics); 53 54 static const struct snl_attr_parser _nla_p_mp_nh[] = { 55 { .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = snl_attr_get_ip }, 56 { .type = NL_RTA_METRICS, .arg = &_metrics_mp_nh_parser, .cb = snl_attr_get_nested }, 57 { .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 }, 58 { .type = NL_RTA_VIA, .off = _OUT(gw), .cb = snl_attr_get_ipvia }, 59 }; 60 61 static const struct snl_field_parser _fp_p_mp_nh[] = { 62 { .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = snl_field_get_uint8 }, 63 { .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = snl_field_get_uint8 }, 64 { .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifindex), .cb = snl_field_get_uint32 }, 65 }; 66 #undef _IN 67 #undef _OUT 68 SNL_DECLARE_PARSER(_mpath_nh_parser, struct rtnexthop, _fp_p_mp_nh, _nla_p_mp_nh); 69 70 struct rta_mpath { 71 int num_nhops; 72 struct rta_mpath_nh nhops[0]; 73 }; 74 75 static bool 76 nlattr_get_multipath(struct snl_state *ss, struct nlattr *nla, const void *arg __unused, 77 void *target) 78 { 79 int data_len = nla->nla_len - sizeof(struct nlattr); 80 struct rtnexthop *rtnh; 81 82 int max_nhops = data_len / sizeof(struct rtnexthop); 83 size_t sz = (max_nhops + 2) * sizeof(struct rta_mpath_nh); 84 85 struct rta_mpath *mp = snl_allocz(ss, sz); 86 if (mp == NULL) 87 return (false); 88 mp->num_nhops = 0; 89 90 for (rtnh = (struct rtnexthop *)(void *)(nla + 1); data_len > 0; ) { 91 struct rta_mpath_nh *mpnh = &mp->nhops[mp->num_nhops++]; 92 93 if (!snl_parse_header(ss, rtnh, rtnh->rtnh_len, &_mpath_nh_parser, mpnh)) 94 return (false); 95 96 int len = NL_ITEM_ALIGN(rtnh->rtnh_len); 97 data_len -= len; 98 rtnh = (struct rtnexthop *)(void *)((char *)rtnh + len); 99 } 100 if (data_len != 0 || mp->num_nhops == 0) { 101 return (false); 102 } 103 104 *((struct rta_mpath **)target) = mp; 105 return (true); 106 } 107 108 struct snl_parsed_route { 109 struct sockaddr *rta_dst; 110 struct sockaddr *rta_gw; 111 struct nlattr *rta_metrics; 112 struct rta_mpath *rta_multipath; 113 uint32_t rta_expires; 114 uint32_t rta_oif; 115 uint32_t rta_expire; 116 uint32_t rta_table; 117 uint32_t rta_knh_id; 118 uint32_t rta_nh_id; 119 uint32_t rta_rtflags; 120 uint32_t rtax_mtu; 121 uint32_t rtax_weight; 122 uint8_t rtm_family; 123 uint8_t rtm_type; 124 uint8_t rtm_protocol; 125 uint8_t rtm_dst_len; 126 }; 127 128 #define _IN(_field) offsetof(struct rtmsg, _field) 129 #define _OUT(_field) offsetof(struct snl_parsed_route, _field) 130 static const struct snl_attr_parser _nla_p_rtmetrics[] = { 131 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 }, 132 }; 133 SNL_DECLARE_ATTR_PARSER(_metrics_parser, _nla_p_rtmetrics); 134 135 static 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 }; 149 150 static 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 }; 156 #undef _IN 157 #undef _OUT 158 SNL_DECLARE_PARSER(snl_rtm_route_parser, struct rtmsg, _fp_p_route, _nla_p_route); 159 160 /* RTM_<NEW|DEL|GET>LINK message parser */ 161 struct snl_parsed_link { 162 uint32_t ifi_index; 163 uint32_t ifi_flags; 164 uint32_t ifi_change; 165 uint16_t ifi_type; 166 uint8_t ifla_operstate; 167 uint8_t ifla_carrier; 168 uint32_t ifla_mtu; 169 char *ifla_ifname; 170 struct nlattr *ifla_address; 171 struct nlattr *ifla_broadcast; 172 char *ifla_ifalias; 173 uint32_t ifla_promiscuity; 174 struct rtnl_link_stats64 *ifla_stats64; 175 }; 176 177 #define _IN(_field) offsetof(struct ifinfomsg, _field) 178 #define _OUT(_field) offsetof(struct snl_parsed_link, _field) 179 static const struct snl_attr_parser _nla_p_link[] = { 180 { .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = snl_attr_get_nla }, 181 { .type = IFLA_BROADCAST, .off = _OUT(ifla_broadcast), .cb = snl_attr_get_nla }, 182 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string }, 183 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 }, 184 { .type = IFLA_OPERSTATE, .off = _OUT(ifla_operstate), .cb = snl_attr_get_uint8 }, 185 { .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = snl_attr_get_string }, 186 { .type = IFLA_STATS64, .off = _OUT(ifla_stats64), .cb = snl_attr_copy_struct }, 187 { .type = IFLA_PROMISCUITY, .off = _OUT(ifla_promiscuity), .cb = snl_attr_get_uint32 }, 188 { .type = IFLA_CARRIER, .off = _OUT(ifla_carrier), .cb = snl_attr_get_uint8 }, 189 }; 190 static const struct snl_field_parser _fp_p_link[] = { 191 {.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 }, 192 {.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 }, 193 {.off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = snl_field_get_uint32 }, 194 {.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 }, 195 }; 196 #undef _IN 197 #undef _OUT 198 SNL_DECLARE_PARSER(snl_rtm_link_parser, struct ifinfomsg, _fp_p_link, _nla_p_link); 199 200 struct snl_parsed_link_simple { 201 uint32_t ifi_index; 202 uint32_t ifla_mtu; 203 uint16_t ifi_type; 204 uint32_t ifi_flags; 205 char *ifla_ifname; 206 }; 207 208 #define _IN(_field) offsetof(struct ifinfomsg, _field) 209 #define _OUT(_field) offsetof(struct snl_parsed_link_simple, _field) 210 static struct snl_attr_parser _nla_p_link_s[] = { 211 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string }, 212 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 }, 213 }; 214 static struct snl_field_parser _fp_p_link_s[] = { 215 {.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 }, 216 {.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 }, 217 {.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 }, 218 }; 219 #undef _IN 220 #undef _OUT 221 SNL_DECLARE_PARSER(snl_rtm_link_parser_simple, struct ifinfomsg, _fp_p_link_s, _nla_p_link_s); 222 223 struct snl_parsed_neigh { 224 uint8_t ndm_family; 225 uint8_t ndm_flags; 226 uint16_t ndm_state; 227 uint32_t nda_ifindex; 228 uint32_t nda_probes; 229 uint32_t ndaf_next_ts; 230 struct sockaddr *nda_dst; 231 struct nlattr *nda_lladdr; 232 }; 233 234 #define _IN(_field) offsetof(struct ndmsg, _field) 235 #define _OUT(_field) offsetof(struct snl_parsed_neigh, _field) 236 static const struct snl_attr_parser _nla_p_neigh_fbsd[] = { 237 { .type = NDAF_NEXT_STATE_TS, .off = _OUT(ndaf_next_ts), .cb = snl_attr_get_uint32 }, 238 }; 239 SNL_DECLARE_ATTR_PARSER(_neigh_fbsd_parser, _nla_p_neigh_fbsd); 240 241 static struct snl_attr_parser _nla_p_neigh_s[] = { 242 { .type = NDA_DST, .off = _OUT(nda_dst), .cb = snl_attr_get_ip }, 243 { .type = NDA_LLADDR , .off = _OUT(nda_lladdr), .cb = snl_attr_get_nla }, 244 { .type = NDA_PROBES, .off = _OUT(nda_probes), .cb = snl_attr_get_uint32 }, 245 { .type = NDA_IFINDEX, .off = _OUT(nda_ifindex), .cb = snl_attr_get_uint32 }, 246 { .type = NDA_FREEBSD, .arg = &_neigh_fbsd_parser, .cb = snl_attr_get_nested }, 247 }; 248 static struct snl_field_parser _fp_p_neigh_s[] = { 249 {.off_in = _IN(ndm_family), .off_out = _OUT(ndm_family), .cb = snl_field_get_uint8 }, 250 {.off_in = _IN(ndm_flags), .off_out = _OUT(ndm_flags), .cb = snl_field_get_uint8 }, 251 {.off_in = _IN(ndm_state), .off_out = _OUT(ndm_state), .cb = snl_field_get_uint16 }, 252 {.off_in = _IN(ndm_ifindex), .off_out = _OUT(nda_ifindex), .cb = snl_field_get_uint32 }, 253 }; 254 #undef _IN 255 #undef _OUT 256 SNL_DECLARE_PARSER(snl_rtm_neigh_parser, struct ndmsg, _fp_p_neigh_s, _nla_p_neigh_s); 257 258 struct snl_parsed_addr { 259 uint8_t ifa_family; 260 uint8_t ifa_prefixlen; 261 uint32_t ifa_index; 262 struct sockaddr *ifa_local; 263 struct sockaddr *ifa_address; 264 struct sockaddr *ifa_broadcast; 265 char *ifa_label; 266 }; 267 268 #define _IN(_field) offsetof(struct ifaddrmsg, _field) 269 #define _OUT(_field) offsetof(struct snl_parsed_addr, _field) 270 static struct snl_attr_parser _nla_p_addr_s[] = { 271 { .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = snl_attr_get_ip }, 272 { .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = snl_attr_get_ip }, 273 { .type = IFA_LABEL, .off = _OUT(ifa_label), .cb = snl_attr_get_string }, 274 { .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = snl_attr_get_ip }, 275 }; 276 static struct snl_field_parser _fp_p_addr_s[] = { 277 {.off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = snl_field_get_uint8 }, 278 {.off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = snl_field_get_uint8 }, 279 {.off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = snl_field_get_uint32 }, 280 }; 281 #undef _IN 282 #undef _OUT 283 SNL_DECLARE_PARSER(snl_rtm_addr_parser, struct ifaddrmsg, _fp_p_addr_s, _nla_p_addr_s); 284 285 286 struct snl_parsed_nhop { 287 uint32_t nha_id; 288 uint8_t nha_blackhole; 289 uint8_t nha_groups; 290 uint8_t nhaf_knhops; 291 uint8_t nhaf_family; 292 uint32_t nha_oif; 293 struct sockaddr *nha_gw; 294 uint8_t nh_family; 295 uint8_t nh_protocol; 296 uint32_t nhaf_table; 297 uint32_t nhaf_kid; 298 uint32_t nhaf_aif; 299 }; 300 301 #define _IN(_field) offsetof(struct nhmsg, _field) 302 #define _OUT(_field) offsetof(struct snl_parsed_nhop, _field) 303 static struct snl_attr_parser _nla_p_nh_fbsd[] = { 304 { .type = NHAF_KNHOPS, .off = _OUT(nhaf_knhops), .cb = snl_attr_get_flag }, 305 { .type = NHAF_TABLE, .off = _OUT(nhaf_table), .cb = snl_attr_get_uint32 }, 306 { .type = NHAF_KID, .off = _OUT(nhaf_kid), .cb = snl_attr_get_uint32 }, 307 { .type = NHAF_AIF, .off = _OUT(nhaf_aif), .cb = snl_attr_get_uint32 }, 308 }; 309 SNL_DECLARE_ATTR_PARSER(_nh_fbsd_parser, _nla_p_nh_fbsd); 310 311 static const struct snl_field_parser _fp_p_nh[] = { 312 { .off_in = _IN(nh_family), .off_out = _OUT(nh_family), .cb = snl_field_get_uint8 }, 313 { .off_in = _IN(nh_protocol), .off_out = _OUT(nh_protocol), .cb = snl_field_get_uint8 }, 314 }; 315 316 static const struct snl_attr_parser _nla_p_nh[] = { 317 { .type = NHA_ID, .off = _OUT(nha_id), .cb = snl_attr_get_uint32 }, 318 { .type = NHA_BLACKHOLE, .off = _OUT(nha_blackhole), .cb = snl_attr_get_flag }, 319 { .type = NHA_OIF, .off = _OUT(nha_oif), .cb = snl_attr_get_uint32 }, 320 { .type = NHA_GATEWAY, .off = _OUT(nha_gw), .cb = snl_attr_get_ip }, 321 { .type = NHA_FREEBSD, .arg = &_nh_fbsd_parser, .cb = snl_attr_get_nested }, 322 }; 323 #undef _IN 324 #undef _OUT 325 SNL_DECLARE_PARSER(snl_nhmsg_parser, struct nhmsg, _fp_p_nh, _nla_p_nh); 326 327 static const struct snl_hdr_parser *snl_all_route_parsers[] = { 328 &_metrics_mp_nh_parser, &_mpath_nh_parser, &_metrics_parser, &snl_rtm_route_parser, 329 &snl_rtm_link_parser, &snl_rtm_link_parser_simple, 330 &_neigh_fbsd_parser, &snl_rtm_neigh_parser, 331 &snl_rtm_addr_parser, &_nh_fbsd_parser, &snl_nhmsg_parser, 332 }; 333 334 #endif 335