1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 static inline void 37 finalize_sockaddr(struct sockaddr *sa, uint32_t ifindex) 38 { 39 if (sa != NULL && sa->sa_family == AF_INET6) { 40 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)sa; 41 42 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 43 sin6->sin6_scope_id = ifindex; 44 } 45 } 46 47 /* RTM_<NEW|DEL|GET>ROUTE message parser */ 48 49 struct rta_mpath_nh { 50 struct sockaddr *gw; 51 uint32_t ifindex; 52 uint8_t rtnh_flags; 53 uint8_t rtnh_weight; 54 uint32_t rtax_mtu; 55 uint32_t rta_rtflags; 56 }; 57 58 #define _IN(_field) offsetof(struct rtnexthop, _field) 59 #define _OUT(_field) offsetof(struct rta_mpath_nh, _field) 60 static const struct snl_attr_parser _nla_p_mp_nh_metrics[] = { 61 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 }, 62 }; 63 SNL_DECLARE_ATTR_PARSER(_metrics_mp_nh_parser, _nla_p_mp_nh_metrics); 64 65 static const struct snl_attr_parser _nla_p_mp_nh[] = { 66 { .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = snl_attr_get_ip }, 67 { .type = NL_RTA_METRICS, .arg = &_metrics_mp_nh_parser, .cb = snl_attr_get_nested }, 68 { .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 }, 69 { .type = NL_RTA_VIA, .off = _OUT(gw), .cb = snl_attr_get_ipvia }, 70 }; 71 72 static const struct snl_field_parser _fp_p_mp_nh[] = { 73 { .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = snl_field_get_uint8 }, 74 { .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = snl_field_get_uint8 }, 75 { .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifindex), .cb = snl_field_get_uint32 }, 76 }; 77 78 static inline bool 79 _cb_p_mp_nh(struct snl_state *ss __unused, void *_target) 80 { 81 struct rta_mpath_nh *target = _target; 82 83 finalize_sockaddr(target->gw, target->ifindex); 84 return (true); 85 } 86 #undef _IN 87 #undef _OUT 88 SNL_DECLARE_PARSER_EXT(_mpath_nh_parser, struct rtnexthop, _fp_p_mp_nh, _nla_p_mp_nh, _cb_p_mp_nh); 89 90 struct rta_mpath { 91 int num_nhops; 92 struct rta_mpath_nh nhops[0]; 93 }; 94 95 static bool 96 nlattr_get_multipath(struct snl_state *ss, struct nlattr *nla, const void *arg __unused, 97 void *target) 98 { 99 int data_len = nla->nla_len - sizeof(struct nlattr); 100 struct rtnexthop *rtnh; 101 102 int max_nhops = data_len / sizeof(struct rtnexthop); 103 size_t sz = (max_nhops + 2) * sizeof(struct rta_mpath_nh); 104 105 struct rta_mpath *mp = snl_allocz(ss, sz); 106 if (mp == NULL) 107 return (false); 108 mp->num_nhops = 0; 109 110 for (rtnh = (struct rtnexthop *)(void *)(nla + 1); data_len > 0; ) { 111 struct rta_mpath_nh *mpnh = &mp->nhops[mp->num_nhops++]; 112 113 if (!snl_parse_header(ss, rtnh, rtnh->rtnh_len, &_mpath_nh_parser, mpnh)) 114 return (false); 115 116 int len = NL_ITEM_ALIGN(rtnh->rtnh_len); 117 data_len -= len; 118 rtnh = (struct rtnexthop *)(void *)((char *)rtnh + len); 119 } 120 if (data_len != 0 || mp->num_nhops == 0) { 121 return (false); 122 } 123 124 *((struct rta_mpath **)target) = mp; 125 return (true); 126 } 127 128 struct snl_parsed_route { 129 struct sockaddr *rta_dst; 130 struct sockaddr *rta_gw; 131 struct nlattr *rta_metrics; 132 struct rta_mpath *rta_multipath; 133 uint32_t rta_expires; 134 uint32_t rta_oif; 135 uint32_t rta_expire; 136 uint32_t rta_table; 137 uint32_t rta_knh_id; 138 uint32_t rta_nh_id; 139 uint32_t rta_rtflags; 140 uint32_t rtax_mtu; 141 uint32_t rtax_weight; 142 uint8_t rtm_family; 143 uint8_t rtm_type; 144 uint8_t rtm_protocol; 145 uint8_t rtm_dst_len; 146 }; 147 148 #define _IN(_field) offsetof(struct rtmsg, _field) 149 #define _OUT(_field) offsetof(struct snl_parsed_route, _field) 150 static const struct snl_attr_parser _nla_p_rtmetrics[] = { 151 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 }, 152 }; 153 SNL_DECLARE_ATTR_PARSER(_metrics_parser, _nla_p_rtmetrics); 154 155 static const struct snl_attr_parser _nla_p_route[] = { 156 { .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = snl_attr_get_ip }, 157 { .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = snl_attr_get_uint32 }, 158 { .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = snl_attr_get_ip }, 159 { .type = NL_RTA_METRICS, .arg = &_metrics_parser, .cb = snl_attr_get_nested }, 160 { .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath }, 161 { .type = NL_RTA_KNH_ID, .off = _OUT(rta_knh_id), .cb = snl_attr_get_uint32 }, 162 { .type = NL_RTA_WEIGHT, .off = _OUT(rtax_weight), .cb = snl_attr_get_uint32 }, 163 { .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 }, 164 { .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = snl_attr_get_uint32 }, 165 { .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = snl_attr_get_ipvia }, 166 { .type = NL_RTA_EXPIRES, .off = _OUT(rta_expire), .cb = snl_attr_get_uint32 }, 167 { .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = snl_attr_get_uint32 }, 168 }; 169 170 static const struct snl_field_parser _fp_p_route[] = { 171 {.off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = snl_field_get_uint8 }, 172 {.off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = snl_field_get_uint8 }, 173 {.off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = snl_field_get_uint8 }, 174 {.off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = snl_field_get_uint8 }, 175 }; 176 177 static inline bool 178 _cb_p_route(struct snl_state *ss __unused, void *_target) 179 { 180 struct snl_parsed_route *target = _target; 181 182 finalize_sockaddr(target->rta_dst, target->rta_oif); 183 finalize_sockaddr(target->rta_gw, target->rta_oif); 184 return (true); 185 } 186 #undef _IN 187 #undef _OUT 188 SNL_DECLARE_PARSER_EXT(snl_rtm_route_parser, struct rtmsg, _fp_p_route, _nla_p_route, _cb_p_route); 189 190 /* RTM_<NEW|DEL|GET>LINK message parser */ 191 struct snl_parsed_link { 192 uint32_t ifi_index; 193 uint32_t ifi_flags; 194 uint32_t ifi_change; 195 uint16_t ifi_type; 196 uint8_t ifla_operstate; 197 uint8_t ifla_carrier; 198 uint32_t ifla_mtu; 199 char *ifla_ifname; 200 struct nlattr *ifla_address; 201 struct nlattr *ifla_broadcast; 202 char *ifla_ifalias; 203 uint32_t ifla_promiscuity; 204 struct rtnl_link_stats64 *ifla_stats64; 205 struct nlattr *iflaf_orig_hwaddr; 206 }; 207 208 #define _IN(_field) offsetof(struct ifinfomsg, _field) 209 #define _OUT(_field) offsetof(struct snl_parsed_link, _field) 210 static const struct snl_attr_parser _nla_p_link_fbsd[] = { 211 { .type = IFLAF_ORIG_HWADDR, .off = _OUT(iflaf_orig_hwaddr), .cb = snl_attr_dup_nla }, 212 }; 213 SNL_DECLARE_ATTR_PARSER(_link_fbsd_parser, _nla_p_link_fbsd); 214 215 static const struct snl_attr_parser _nla_p_link[] = { 216 { .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = snl_attr_dup_nla }, 217 { .type = IFLA_BROADCAST, .off = _OUT(ifla_broadcast), .cb = snl_attr_dup_nla }, 218 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_dup_string }, 219 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 }, 220 { .type = IFLA_OPERSTATE, .off = _OUT(ifla_operstate), .cb = snl_attr_get_uint8 }, 221 { .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = snl_attr_dup_string }, 222 { .type = IFLA_STATS64, .off = _OUT(ifla_stats64), .cb = snl_attr_dup_struct }, 223 { .type = IFLA_PROMISCUITY, .off = _OUT(ifla_promiscuity), .cb = snl_attr_get_uint32 }, 224 { .type = IFLA_CARRIER, .off = _OUT(ifla_carrier), .cb = snl_attr_get_uint8 }, 225 { .type = IFLA_FREEBSD, .arg = &_link_fbsd_parser, .cb = snl_attr_get_nested }, 226 }; 227 static const struct snl_field_parser _fp_p_link[] = { 228 {.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 }, 229 {.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 }, 230 {.off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = snl_field_get_uint32 }, 231 {.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 }, 232 }; 233 #undef _IN 234 #undef _OUT 235 SNL_DECLARE_PARSER(snl_rtm_link_parser, struct ifinfomsg, _fp_p_link, _nla_p_link); 236 237 struct snl_parsed_link_simple { 238 uint32_t ifi_index; 239 uint32_t ifla_mtu; 240 uint16_t ifi_type; 241 uint32_t ifi_flags; 242 char *ifla_ifname; 243 }; 244 245 #define _IN(_field) offsetof(struct ifinfomsg, _field) 246 #define _OUT(_field) offsetof(struct snl_parsed_link_simple, _field) 247 static struct snl_attr_parser _nla_p_link_s[] = { 248 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_dup_string }, 249 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 }, 250 }; 251 static struct snl_field_parser _fp_p_link_s[] = { 252 {.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 }, 253 {.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 }, 254 {.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 }, 255 }; 256 #undef _IN 257 #undef _OUT 258 SNL_DECLARE_PARSER(snl_rtm_link_parser_simple, struct ifinfomsg, _fp_p_link_s, _nla_p_link_s); 259 260 struct snl_parsed_neigh { 261 uint8_t ndm_family; 262 uint8_t ndm_flags; 263 uint16_t ndm_state; 264 uint32_t nda_ifindex; 265 uint32_t nda_probes; 266 uint32_t ndaf_next_ts; 267 struct sockaddr *nda_dst; 268 struct nlattr *nda_lladdr; 269 }; 270 271 #define _IN(_field) offsetof(struct ndmsg, _field) 272 #define _OUT(_field) offsetof(struct snl_parsed_neigh, _field) 273 static const struct snl_attr_parser _nla_p_neigh_fbsd[] = { 274 { .type = NDAF_NEXT_STATE_TS, .off = _OUT(ndaf_next_ts), .cb = snl_attr_get_uint32 }, 275 }; 276 SNL_DECLARE_ATTR_PARSER(_neigh_fbsd_parser, _nla_p_neigh_fbsd); 277 278 static struct snl_attr_parser _nla_p_neigh_s[] = { 279 { .type = NDA_DST, .off = _OUT(nda_dst), .cb = snl_attr_get_ip }, 280 { .type = NDA_LLADDR , .off = _OUT(nda_lladdr), .cb = snl_attr_dup_nla }, 281 { .type = NDA_PROBES, .off = _OUT(nda_probes), .cb = snl_attr_get_uint32 }, 282 { .type = NDA_IFINDEX, .off = _OUT(nda_ifindex), .cb = snl_attr_get_uint32 }, 283 { .type = NDA_FREEBSD, .arg = &_neigh_fbsd_parser, .cb = snl_attr_get_nested }, 284 }; 285 static struct snl_field_parser _fp_p_neigh_s[] = { 286 {.off_in = _IN(ndm_family), .off_out = _OUT(ndm_family), .cb = snl_field_get_uint8 }, 287 {.off_in = _IN(ndm_flags), .off_out = _OUT(ndm_flags), .cb = snl_field_get_uint8 }, 288 {.off_in = _IN(ndm_state), .off_out = _OUT(ndm_state), .cb = snl_field_get_uint16 }, 289 {.off_in = _IN(ndm_ifindex), .off_out = _OUT(nda_ifindex), .cb = snl_field_get_uint32 }, 290 }; 291 292 static inline bool 293 _cb_p_neigh(struct snl_state *ss __unused, void *_target) 294 { 295 struct snl_parsed_neigh *target = _target; 296 297 finalize_sockaddr(target->nda_dst, target->nda_ifindex); 298 return (true); 299 } 300 #undef _IN 301 #undef _OUT 302 SNL_DECLARE_PARSER_EXT(snl_rtm_neigh_parser, struct ndmsg, _fp_p_neigh_s, _nla_p_neigh_s, _cb_p_neigh); 303 304 struct snl_parsed_addr { 305 uint8_t ifa_family; 306 uint8_t ifa_prefixlen; 307 uint32_t ifa_index; 308 struct sockaddr *ifa_local; 309 struct sockaddr *ifa_address; 310 struct sockaddr *ifa_broadcast; 311 char *ifa_label; 312 struct ifa_cacheinfo *ifa_cacheinfo; 313 uint32_t ifaf_vhid; 314 uint32_t ifaf_flags; 315 }; 316 317 #define _IN(_field) offsetof(struct ifaddrmsg, _field) 318 #define _OUT(_field) offsetof(struct snl_parsed_addr, _field) 319 static const struct snl_attr_parser _nla_p_addr_fbsd[] = { 320 { .type = IFAF_VHID, .off = _OUT(ifaf_vhid), .cb = snl_attr_get_uint32 }, 321 { .type = IFAF_FLAGS, .off = _OUT(ifaf_flags), .cb = snl_attr_get_uint32 }, 322 }; 323 SNL_DECLARE_ATTR_PARSER(_addr_fbsd_parser, _nla_p_addr_fbsd); 324 325 static const struct snl_attr_parser _nla_p_addr_s[] = { 326 { .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = snl_attr_get_ip }, 327 { .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = snl_attr_get_ip }, 328 { .type = IFA_LABEL, .off = _OUT(ifa_label), .cb = snl_attr_dup_string }, 329 { .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = snl_attr_get_ip }, 330 { .type = IFA_CACHEINFO, .off = _OUT(ifa_cacheinfo), .cb = snl_attr_dup_struct }, 331 { .type = IFA_FREEBSD, .arg = &_addr_fbsd_parser, .cb = snl_attr_get_nested }, 332 }; 333 static const struct snl_field_parser _fp_p_addr_s[] = { 334 {.off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = snl_field_get_uint8 }, 335 {.off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = snl_field_get_uint8 }, 336 {.off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = snl_field_get_uint32 }, 337 }; 338 339 static inline bool 340 _cb_p_addr(struct snl_state *ss __unused, void *_target) 341 { 342 struct snl_parsed_addr *target = _target; 343 344 finalize_sockaddr(target->ifa_address, target->ifa_index); 345 finalize_sockaddr(target->ifa_local, target->ifa_index); 346 return (true); 347 } 348 #undef _IN 349 #undef _OUT 350 SNL_DECLARE_PARSER_EXT(snl_rtm_addr_parser, struct ifaddrmsg, _fp_p_addr_s, _nla_p_addr_s, _cb_p_addr); 351 352 struct snl_parsed_nhop { 353 uint32_t nha_id; 354 uint8_t nha_blackhole; 355 uint8_t nha_groups; 356 uint8_t nhaf_knhops; 357 uint8_t nhaf_family; 358 uint32_t nha_oif; 359 struct sockaddr *nha_gw; 360 uint8_t nh_family; 361 uint8_t nh_protocol; 362 uint32_t nhaf_table; 363 uint32_t nhaf_kid; 364 uint32_t nhaf_aif; 365 }; 366 367 #define _IN(_field) offsetof(struct nhmsg, _field) 368 #define _OUT(_field) offsetof(struct snl_parsed_nhop, _field) 369 static struct snl_attr_parser _nla_p_nh_fbsd[] = { 370 { .type = NHAF_KNHOPS, .off = _OUT(nhaf_knhops), .cb = snl_attr_get_flag }, 371 { .type = NHAF_TABLE, .off = _OUT(nhaf_table), .cb = snl_attr_get_uint32 }, 372 { .type = NHAF_KID, .off = _OUT(nhaf_kid), .cb = snl_attr_get_uint32 }, 373 { .type = NHAF_AIF, .off = _OUT(nhaf_aif), .cb = snl_attr_get_uint32 }, 374 }; 375 SNL_DECLARE_ATTR_PARSER(_nh_fbsd_parser, _nla_p_nh_fbsd); 376 377 static const struct snl_field_parser _fp_p_nh[] = { 378 { .off_in = _IN(nh_family), .off_out = _OUT(nh_family), .cb = snl_field_get_uint8 }, 379 { .off_in = _IN(nh_protocol), .off_out = _OUT(nh_protocol), .cb = snl_field_get_uint8 }, 380 }; 381 382 static const struct snl_attr_parser _nla_p_nh[] = { 383 { .type = NHA_ID, .off = _OUT(nha_id), .cb = snl_attr_get_uint32 }, 384 { .type = NHA_BLACKHOLE, .off = _OUT(nha_blackhole), .cb = snl_attr_get_flag }, 385 { .type = NHA_OIF, .off = _OUT(nha_oif), .cb = snl_attr_get_uint32 }, 386 { .type = NHA_GATEWAY, .off = _OUT(nha_gw), .cb = snl_attr_get_ip }, 387 { .type = NHA_FREEBSD, .arg = &_nh_fbsd_parser, .cb = snl_attr_get_nested }, 388 }; 389 390 static inline bool 391 _cb_p_nh(struct snl_state *ss __unused, void *_target) 392 { 393 struct snl_parsed_nhop *target = _target; 394 395 finalize_sockaddr(target->nha_gw, target->nha_oif); 396 return (true); 397 } 398 #undef _IN 399 #undef _OUT 400 SNL_DECLARE_PARSER_EXT(snl_nhmsg_parser, struct nhmsg, _fp_p_nh, _nla_p_nh, _cb_p_nh); 401 402 static const struct snl_hdr_parser *snl_all_route_parsers[] = { 403 &_metrics_mp_nh_parser, &_mpath_nh_parser, &_metrics_parser, &snl_rtm_route_parser, 404 &_link_fbsd_parser, &snl_rtm_link_parser, &snl_rtm_link_parser_simple, 405 &_neigh_fbsd_parser, &snl_rtm_neigh_parser, 406 &_addr_fbsd_parser, &snl_rtm_addr_parser, &_nh_fbsd_parser, &snl_nhmsg_parser, 407 }; 408 409 #endif 410