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 };
190 
191 #define	_IN(_field)	offsetof(struct ifinfomsg, _field)
192 #define	_OUT(_field)	offsetof(struct snl_parsed_link, _field)
193 static const struct snl_attr_parser _nla_p_link_fbsd[] = {
194 	{ .type = IFLAF_ORIG_HWADDR, .off = _OUT(iflaf_orig_hwaddr), .cb = snl_attr_dup_nla },
195 };
196 SNL_DECLARE_ATTR_PARSER(_link_fbsd_parser, _nla_p_link_fbsd);
197 
198 static const struct snl_attr_parser _nla_p_link[] = {
199 	{ .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = snl_attr_dup_nla },
200 	{ .type = IFLA_BROADCAST, .off = _OUT(ifla_broadcast), .cb = snl_attr_dup_nla },
201 	{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_dup_string },
202 	{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
203 	{ .type = IFLA_OPERSTATE, .off = _OUT(ifla_operstate), .cb = snl_attr_get_uint8 },
204 	{ .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = snl_attr_dup_string },
205 	{ .type = IFLA_STATS64, .off = _OUT(ifla_stats64), .cb = snl_attr_dup_struct },
206 	{ .type = IFLA_PROMISCUITY, .off = _OUT(ifla_promiscuity), .cb = snl_attr_get_uint32 },
207 	{ .type = IFLA_CARRIER, .off = _OUT(ifla_carrier), .cb = snl_attr_get_uint8 },
208 	{ .type = IFLA_FREEBSD, .arg = &_link_fbsd_parser, .cb = snl_attr_get_nested },
209 };
210 static const struct snl_field_parser _fp_p_link[] = {
211 	{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
212 	{.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 },
213 	{.off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = snl_field_get_uint32 },
214 	{.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 },
215 };
216 #undef _IN
217 #undef _OUT
218 SNL_DECLARE_PARSER(snl_rtm_link_parser, struct ifinfomsg, _fp_p_link, _nla_p_link);
219 
220 struct snl_parsed_link_simple {
221 	uint32_t		ifi_index;
222 	uint32_t		ifla_mtu;
223 	uint16_t		ifi_type;
224 	uint32_t		ifi_flags;
225 	char			*ifla_ifname;
226 };
227 
228 #define	_IN(_field)	offsetof(struct ifinfomsg, _field)
229 #define	_OUT(_field)	offsetof(struct snl_parsed_link_simple, _field)
230 static struct snl_attr_parser _nla_p_link_s[] = {
231 	{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_dup_string },
232 	{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
233 };
234 static struct snl_field_parser _fp_p_link_s[] = {
235 	{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
236 	{.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 },
237 	{.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 },
238 };
239 #undef _IN
240 #undef _OUT
241 SNL_DECLARE_PARSER(snl_rtm_link_parser_simple, struct ifinfomsg, _fp_p_link_s, _nla_p_link_s);
242 
243 struct snl_parsed_neigh {
244 	uint8_t		ndm_family;
245 	uint8_t		ndm_flags;
246 	uint16_t	ndm_state;
247 	uint32_t	nda_ifindex;
248 	uint32_t	nda_probes;
249 	uint32_t	ndaf_next_ts;
250 	struct sockaddr	*nda_dst;
251 	struct nlattr	*nda_lladdr;
252 };
253 
254 #define	_IN(_field)	offsetof(struct ndmsg, _field)
255 #define	_OUT(_field)	offsetof(struct snl_parsed_neigh, _field)
256 static const struct snl_attr_parser _nla_p_neigh_fbsd[] = {
257 	{ .type = NDAF_NEXT_STATE_TS, .off = _OUT(ndaf_next_ts), .cb = snl_attr_get_uint32 },
258 };
259 SNL_DECLARE_ATTR_PARSER(_neigh_fbsd_parser, _nla_p_neigh_fbsd);
260 
261 static struct snl_attr_parser _nla_p_neigh_s[] = {
262 	{ .type = NDA_DST, .off = _OUT(nda_dst), .cb = snl_attr_get_ip },
263 	{ .type = NDA_LLADDR , .off = _OUT(nda_lladdr), .cb = snl_attr_dup_nla },
264 	{ .type = NDA_PROBES, .off = _OUT(nda_probes), .cb = snl_attr_get_uint32 },
265 	{ .type = NDA_IFINDEX, .off = _OUT(nda_ifindex), .cb = snl_attr_get_uint32 },
266 	{ .type = NDA_FREEBSD, .arg = &_neigh_fbsd_parser, .cb = snl_attr_get_nested },
267 };
268 static struct snl_field_parser _fp_p_neigh_s[] = {
269 	{.off_in = _IN(ndm_family), .off_out = _OUT(ndm_family), .cb = snl_field_get_uint8 },
270 	{.off_in = _IN(ndm_flags), .off_out = _OUT(ndm_flags), .cb = snl_field_get_uint8 },
271 	{.off_in = _IN(ndm_state), .off_out = _OUT(ndm_state), .cb = snl_field_get_uint16 },
272 	{.off_in = _IN(ndm_ifindex), .off_out = _OUT(nda_ifindex), .cb = snl_field_get_uint32 },
273 };
274 
275 static inline bool
276 _cb_p_neigh(struct snl_state *ss __unused, void *_target)
277 {
278 	struct snl_parsed_neigh *target = _target;
279 
280 	finalize_sockaddr(target->nda_dst, target->nda_ifindex);
281 	return (true);
282 }
283 #undef _IN
284 #undef _OUT
285 SNL_DECLARE_PARSER_EXT(snl_rtm_neigh_parser, sizeof(struct ndmsg),
286 		sizeof(struct snl_parsed_neigh), _fp_p_neigh_s, _nla_p_neigh_s,
287 		_cb_p_neigh);
288 
289 struct snl_parsed_addr {
290 	uint8_t		ifa_family;
291 	uint8_t		ifa_prefixlen;
292 	uint32_t	ifa_index;
293 	struct sockaddr	*ifa_local;
294 	struct sockaddr	*ifa_address;
295 	struct sockaddr	*ifa_broadcast;
296 	char		*ifa_label;
297 	struct ifa_cacheinfo	*ifa_cacheinfo;
298 	uint32_t	ifaf_vhid;
299 	uint32_t	ifaf_flags;
300 };
301 
302 #define	_IN(_field)	offsetof(struct ifaddrmsg, _field)
303 #define	_OUT(_field)	offsetof(struct snl_parsed_addr, _field)
304 static const struct snl_attr_parser _nla_p_addr_fbsd[] = {
305 	{ .type = IFAF_VHID, .off = _OUT(ifaf_vhid), .cb = snl_attr_get_uint32 },
306 	{ .type = IFAF_FLAGS, .off = _OUT(ifaf_flags), .cb = snl_attr_get_uint32 },
307 };
308 SNL_DECLARE_ATTR_PARSER(_addr_fbsd_parser, _nla_p_addr_fbsd);
309 
310 static const struct snl_attr_parser _nla_p_addr_s[] = {
311 	{ .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = snl_attr_get_ip },
312 	{ .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = snl_attr_get_ip },
313 	{ .type = IFA_LABEL, .off = _OUT(ifa_label), .cb = snl_attr_dup_string },
314 	{ .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = snl_attr_get_ip },
315 	{ .type = IFA_CACHEINFO, .off = _OUT(ifa_cacheinfo), .cb = snl_attr_dup_struct },
316 	{ .type = IFA_FREEBSD, .arg = &_addr_fbsd_parser, .cb = snl_attr_get_nested },
317 };
318 static const struct snl_field_parser _fp_p_addr_s[] = {
319 	{.off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = snl_field_get_uint8 },
320 	{.off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = snl_field_get_uint8 },
321 	{.off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = snl_field_get_uint32 },
322 };
323 
324 static inline bool
325 _cb_p_addr(struct snl_state *ss __unused, void *_target)
326 {
327 	struct snl_parsed_addr *target = _target;
328 
329 	finalize_sockaddr(target->ifa_address, target->ifa_index);
330 	finalize_sockaddr(target->ifa_local, target->ifa_index);
331 	return (true);
332 }
333 #undef _IN
334 #undef _OUT
335 SNL_DECLARE_PARSER_EXT(snl_rtm_addr_parser, sizeof(struct ifaddrmsg),
336 		sizeof(struct snl_parsed_addr), _fp_p_addr_s, _nla_p_addr_s,
337 		_cb_p_addr);
338 
339 struct snl_parsed_nhop {
340 	uint32_t	nha_id;
341 	uint8_t		nha_blackhole;
342 	uint8_t		nha_groups;
343 	uint8_t		nhaf_knhops;
344 	uint8_t		nhaf_family;
345 	uint32_t	nha_oif;
346 	struct sockaddr	*nha_gw;
347 	uint8_t		nh_family;
348 	uint8_t		nh_protocol;
349 	uint32_t	nhaf_table;
350 	uint32_t	nhaf_kid;
351 	uint32_t	nhaf_aif;
352 };
353 
354 #define	_IN(_field)	offsetof(struct nhmsg, _field)
355 #define	_OUT(_field)	offsetof(struct snl_parsed_nhop, _field)
356 static struct snl_attr_parser _nla_p_nh_fbsd[] = {
357 	{ .type = NHAF_KNHOPS, .off = _OUT(nhaf_knhops), .cb = snl_attr_get_flag },
358 	{ .type = NHAF_TABLE, .off = _OUT(nhaf_table), .cb = snl_attr_get_uint32 },
359 	{ .type = NHAF_KID, .off = _OUT(nhaf_kid), .cb = snl_attr_get_uint32 },
360 	{ .type = NHAF_AIF, .off = _OUT(nhaf_aif), .cb = snl_attr_get_uint32 },
361 };
362 SNL_DECLARE_ATTR_PARSER(_nh_fbsd_parser, _nla_p_nh_fbsd);
363 
364 static const struct snl_field_parser _fp_p_nh[] = {
365 	{ .off_in = _IN(nh_family), .off_out = _OUT(nh_family), .cb = snl_field_get_uint8 },
366 	{ .off_in = _IN(nh_protocol), .off_out = _OUT(nh_protocol), .cb = snl_field_get_uint8 },
367 };
368 
369 static const struct snl_attr_parser _nla_p_nh[] = {
370 	{ .type = NHA_ID, .off = _OUT(nha_id), .cb = snl_attr_get_uint32 },
371 	{ .type = NHA_BLACKHOLE, .off = _OUT(nha_blackhole), .cb = snl_attr_get_flag },
372 	{ .type = NHA_OIF, .off = _OUT(nha_oif), .cb = snl_attr_get_uint32 },
373 	{ .type = NHA_GATEWAY, .off = _OUT(nha_gw), .cb = snl_attr_get_ip },
374 	{ .type = NHA_FREEBSD, .arg = &_nh_fbsd_parser, .cb = snl_attr_get_nested },
375 };
376 
377 static inline bool
378 _cb_p_nh(struct snl_state *ss __unused, void *_target)
379 {
380 	struct snl_parsed_nhop *target = _target;
381 
382 	finalize_sockaddr(target->nha_gw, target->nha_oif);
383 	return (true);
384 }
385 #undef _IN
386 #undef _OUT
387 SNL_DECLARE_PARSER_EXT(snl_nhmsg_parser, sizeof(struct nhmsg),
388 		sizeof(struct snl_parsed_nhop), _fp_p_nh, _nla_p_nh, _cb_p_nh);
389 
390 static const struct snl_hdr_parser *snl_all_route_parsers[] = {
391 	&_metrics_mp_nh_parser, &_mpath_nh_parser, &_metrics_parser, &snl_rtm_route_parser,
392 	&_link_fbsd_parser, &snl_rtm_link_parser, &snl_rtm_link_parser_simple,
393 	&_neigh_fbsd_parser, &snl_rtm_neigh_parser,
394 	&_addr_fbsd_parser, &snl_rtm_addr_parser, &_nh_fbsd_parser, &snl_nhmsg_parser,
395 };
396 
397 #endif
398