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