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 
33 /* TODO: this file should be generated automatically */
34 
35 /* RTM_<NEW|DEL|GET>ROUTE message parser */
36 
37 struct rta_mpath_nh {
38 	struct sockaddr	*gw;
39 	uint32_t	ifindex;
40 	uint8_t		rtnh_flags;
41 	uint8_t		rtnh_weight;
42 	uint32_t	rtax_mtu;
43 	uint32_t	rta_rtflags;
44 };
45 
46 #define	_IN(_field)	offsetof(struct rtnexthop, _field)
47 #define	_OUT(_field)	offsetof(struct rta_mpath_nh, _field)
48 static const struct snl_attr_parser _nla_p_mp_nh_metrics[] = {
49 	{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 },
50 };
51 SNL_DECLARE_ATTR_PARSER(_metrics_mp_nh_parser, _nla_p_mp_nh_metrics);
52 
53 static const struct snl_attr_parser _nla_p_mp_nh[] = {
54 	{ .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = snl_attr_get_ip },
55 	{ .type = NL_RTA_METRICS, .arg = &_metrics_mp_nh_parser, .cb = snl_attr_get_nested },
56 	{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 },
57 	{ .type = NL_RTA_VIA, .off = _OUT(gw), .cb = snl_attr_get_ipvia },
58 };
59 
60 static const struct snl_field_parser _fp_p_mp_nh[] = {
61 	{ .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = snl_field_get_uint8 },
62 	{ .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = snl_field_get_uint8 },
63 	{ .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifindex), .cb = snl_field_get_uint32 },
64 };
65 #undef _IN
66 #undef _OUT
67 SNL_DECLARE_PARSER(_mpath_nh_parser, struct rtnexthop, _fp_p_mp_nh, _nla_p_mp_nh);
68 
69 struct rta_mpath {
70 	int num_nhops;
71 	struct rta_mpath_nh nhops[0];
72 };
73 
74 static bool
75 nlattr_get_multipath(struct snl_state *ss, struct nlattr *nla, const void *arg __unused,
76     void *target)
77 {
78 	int data_len = nla->nla_len - sizeof(struct nlattr);
79 	struct rtnexthop *rtnh;
80 
81 	int max_nhops = data_len / sizeof(struct rtnexthop);
82 	size_t sz = (max_nhops + 2) * sizeof(struct rta_mpath_nh);
83 
84 	struct rta_mpath *mp = snl_allocz(ss, sz);
85 	if (mp == NULL)
86 		return (false);
87 	mp->num_nhops = 0;
88 
89 	for (rtnh = (struct rtnexthop *)(void *)(nla + 1); data_len > 0; ) {
90 		struct rta_mpath_nh *mpnh = &mp->nhops[mp->num_nhops++];
91 
92 		if (!snl_parse_header(ss, rtnh, rtnh->rtnh_len, &_mpath_nh_parser, mpnh))
93 			return (false);
94 
95 		int len = NL_ITEM_ALIGN(rtnh->rtnh_len);
96 		data_len -= len;
97 		rtnh = (struct rtnexthop *)(void *)((char *)rtnh + len);
98 	}
99 	if (data_len != 0 || mp->num_nhops == 0) {
100 		return (false);
101 	}
102 
103 	*((struct rta_mpath **)target) = mp;
104 	return (true);
105 }
106 
107 struct snl_parsed_route {
108 	struct sockaddr		*rta_dst;
109 	struct sockaddr		*rta_gw;
110 	struct nlattr		*rta_metrics;
111 	struct rta_mpath	*rta_multipath;
112 	uint32_t		rta_expires;
113 	uint32_t		rta_oif;
114 	uint32_t		rta_expire;
115 	uint32_t		rta_table;
116 	uint32_t		rta_knh_id;
117 	uint32_t		rta_nh_id;
118 	uint32_t		rta_rtflags;
119 	uint32_t		rtax_mtu;
120 	uint32_t		rtax_weight;
121 	uint8_t			rtm_family;
122 	uint8_t			rtm_type;
123 	uint8_t			rtm_protocol;
124 	uint8_t			rtm_dst_len;
125 };
126 
127 #define	_IN(_field)	offsetof(struct rtmsg, _field)
128 #define	_OUT(_field)	offsetof(struct snl_parsed_route, _field)
129 static const struct snl_attr_parser _nla_p_rtmetrics[] = {
130 	{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 },
131 };
132 SNL_DECLARE_ATTR_PARSER(_metrics_parser, _nla_p_rtmetrics);
133 
134 static const struct snl_attr_parser _nla_p_route[] = {
135 	{ .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = snl_attr_get_ip },
136 	{ .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = snl_attr_get_uint32 },
137 	{ .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = snl_attr_get_ip },
138 	{ .type = NL_RTA_METRICS, .arg = &_metrics_parser, .cb = snl_attr_get_nested },
139 	{ .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath },
140 	{ .type = NL_RTA_KNH_ID, .off = _OUT(rta_knh_id), .cb = snl_attr_get_uint32 },
141 	{ .type = NL_RTA_WEIGHT, .off = _OUT(rtax_weight), .cb = snl_attr_get_uint32 },
142 	{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 },
143 	{ .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = snl_attr_get_uint32 },
144 	{ .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = snl_attr_get_ipvia },
145 	{ .type = NL_RTA_EXPIRES, .off = _OUT(rta_expire), .cb = snl_attr_get_uint32 },
146 	{ .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = snl_attr_get_uint32 },
147 };
148 
149 static const struct snl_field_parser _fp_p_route[] = {
150 	{.off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = snl_field_get_uint8 },
151 	{.off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = snl_field_get_uint8 },
152 	{.off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = snl_field_get_uint8 },
153 	{.off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = snl_field_get_uint8 },
154 };
155 #undef _IN
156 #undef _OUT
157 SNL_DECLARE_PARSER(snl_rtm_route_parser, struct rtmsg, _fp_p_route, _nla_p_route);
158 
159 /* RTM_<NEW|DEL|GET>LINK message parser */
160 struct snl_parsed_link {
161 	uint32_t			ifi_index;
162 	uint32_t			ifi_flags;
163 	uint32_t			ifi_change;
164 	uint16_t			ifi_type;
165 	uint8_t				ifla_operstate;
166 	uint8_t				ifla_carrier;
167 	uint32_t			ifla_mtu;
168 	char				*ifla_ifname;
169 	struct nlattr			*ifla_address;
170 	struct nlattr			*ifla_broadcast;
171 	char				*ifla_ifalias;
172 	uint32_t			ifla_promiscuity;
173 	struct rtnl_link_stats64	*ifla_stats64;
174 };
175 
176 #define	_IN(_field)	offsetof(struct ifinfomsg, _field)
177 #define	_OUT(_field)	offsetof(struct snl_parsed_link, _field)
178 static const struct snl_attr_parser _nla_p_link[] = {
179 	{ .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = snl_attr_get_nla },
180 	{ .type = IFLA_BROADCAST, .off = _OUT(ifla_broadcast), .cb = snl_attr_get_nla },
181 	{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string },
182 	{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
183 	{ .type = IFLA_OPERSTATE, .off = _OUT(ifla_operstate), .cb = snl_attr_get_uint8 },
184 	{ .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = snl_attr_get_string },
185 	{ .type = IFLA_STATS64, .off = _OUT(ifla_stats64), .cb = snl_attr_copy_struct },
186 	{ .type = IFLA_PROMISCUITY, .off = _OUT(ifla_promiscuity), .cb = snl_attr_get_uint32 },
187 	{ .type = IFLA_CARRIER, .off = _OUT(ifla_carrier), .cb = snl_attr_get_uint8 },
188 };
189 static const struct snl_field_parser _fp_p_link[] = {
190 	{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
191 	{.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 },
192 	{.off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = snl_field_get_uint32 },
193 	{.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 },
194 };
195 #undef _IN
196 #undef _OUT
197 SNL_DECLARE_PARSER(snl_rtm_link_parser, struct ifinfomsg, _fp_p_link, _nla_p_link);
198 
199 struct snl_parsed_link_simple {
200 	uint32_t		ifi_index;
201 	uint32_t		ifla_mtu;
202 	uint16_t		ifi_type;
203 	uint32_t		ifi_flags;
204 	char			*ifla_ifname;
205 };
206 
207 #define	_IN(_field)	offsetof(struct ifinfomsg, _field)
208 #define	_OUT(_field)	offsetof(struct snl_parsed_link_simple, _field)
209 static struct snl_attr_parser _nla_p_link_s[] = {
210 	{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string },
211 	{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
212 };
213 static struct snl_field_parser _fp_p_link_s[] = {
214 	{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
215 	{.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 },
216 	{.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 },
217 };
218 #undef _IN
219 #undef _OUT
220 SNL_DECLARE_PARSER(snl_rtm_link_parser_simple, struct ifinfomsg, _fp_p_link_s, _nla_p_link_s);
221 
222 struct snl_parsed_neigh {
223 	uint8_t		ndm_family;
224 	uint8_t		ndm_flags;
225 	uint16_t	ndm_state;
226 	uint32_t	nda_ifindex;
227 	struct sockaddr	*nda_dst;
228 	struct nlattr	*nda_lladdr;
229 };
230 
231 #define	_IN(_field)	offsetof(struct ndmsg, _field)
232 #define	_OUT(_field)	offsetof(struct snl_parsed_neigh, _field)
233 static struct snl_attr_parser _nla_p_neigh_s[] = {
234 	{ .type = NDA_DST, .off = _OUT(nda_dst), .cb = snl_attr_get_ip },
235 	{ .type = NDA_LLADDR , .off = _OUT(nda_lladdr), .cb = snl_attr_get_nla },
236 	{ .type = NDA_IFINDEX, .off = _OUT(nda_ifindex), .cb = snl_attr_get_uint32 },
237 };
238 static struct snl_field_parser _fp_p_neigh_s[] = {
239 	{.off_in = _IN(ndm_family), .off_out = _OUT(ndm_family), .cb = snl_field_get_uint8 },
240 	{.off_in = _IN(ndm_flags), .off_out = _OUT(ndm_flags), .cb = snl_field_get_uint8 },
241 	{.off_in = _IN(ndm_state), .off_out = _OUT(ndm_state), .cb = snl_field_get_uint16 },
242 };
243 #undef _IN
244 #undef _OUT
245 SNL_DECLARE_PARSER(snl_rtm_neigh_parser, struct ndmsg, _fp_p_neigh_s, _nla_p_neigh_s);
246 
247 struct snl_parsed_addr {
248 	uint8_t		ifa_family;
249 	uint8_t		ifa_prefixlen;
250 	uint32_t	ifa_index;
251 	struct sockaddr	*ifa_local;
252 	struct sockaddr	*ifa_address;
253 	struct sockaddr	*ifa_broadcast;
254 	char		*ifa_label;
255 };
256 
257 #define	_IN(_field)	offsetof(struct ifaddrmsg, _field)
258 #define	_OUT(_field)	offsetof(struct snl_parsed_addr, _field)
259 static struct snl_attr_parser _nla_p_addr_s[] = {
260 	{ .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = snl_attr_get_ip },
261 	{ .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = snl_attr_get_ip },
262 	{ .type = IFA_LABEL, .off = _OUT(ifa_label), .cb = snl_attr_get_string },
263 	{ .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = snl_attr_get_ip },
264 };
265 static struct snl_field_parser _fp_p_addr_s[] = {
266 	{.off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = snl_field_get_uint8 },
267 	{.off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = snl_field_get_uint8 },
268 	{.off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = snl_field_get_uint32 },
269 };
270 #undef _IN
271 #undef _OUT
272 SNL_DECLARE_PARSER(snl_rtm_addr_parser, struct ifaddrmsg, _fp_p_addr_s, _nla_p_addr_s);
273 
274 static const struct snl_hdr_parser *snl_all_route_parsers[] = {
275 	&_metrics_mp_nh_parser, &_mpath_nh_parser, &_metrics_parser, &snl_rtm_route_parser,
276 	&snl_rtm_link_parser, &snl_rtm_link_parser_simple, &snl_rtm_neigh_parser,
277 	&snl_rtm_addr_parser,
278 };
279 
280 #endif
281