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