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