1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2023 Alexander V. Chernikov <melifaro@FreeBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _NETLINK_NETLINK_SNL_ROUTE_PARSERS_H_
#define _NETLINK_NETLINK_SNL_ROUTE_PARSERS_H_
#include <netlink/netlink_snl.h>
#include <netlink/netlink_snl_route.h>
#include <netlink/route/nexthop.h>
/* TODO: this file should be generated automatically */
static inline void
finalize_sockaddr(struct sockaddr *sa, uint32_t ifindex)
{
if (sa != NULL && sa->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)sa;
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
sin6->sin6_scope_id = ifindex;
}
}
/* RTM_<NEW|DEL|GET>ROUTE message parser */
struct rta_mpath_nh {
struct sockaddr *gw;
uint32_t ifindex;
uint8_t rtnh_flags;
uint8_t rtnh_weight;
uint32_t rtax_mtu;
uint32_t rta_rtflags;
};
#define _IN(_field) offsetof(struct rtnexthop, _field)
#define _OUT(_field) offsetof(struct rta_mpath_nh, _field)
static const struct snl_attr_parser _nla_p_mp_nh_metrics[] = {
{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 },
};
SNL_DECLARE_ATTR_PARSER(_metrics_mp_nh_parser, _nla_p_mp_nh_metrics);
static const struct snl_attr_parser _nla_p_mp_nh[] = {
{ .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = snl_attr_get_ip },
{ .type = NL_RTA_METRICS, .arg = &_metrics_mp_nh_parser, .cb = snl_attr_get_nested },
{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_VIA, .off = _OUT(gw), .cb = snl_attr_get_ipvia },
};
static const struct snl_field_parser _fp_p_mp_nh[] = {
{ .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = snl_field_get_uint8 },
{ .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = snl_field_get_uint8 },
{ .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifindex), .cb = snl_field_get_uint32 },
};
static inline bool
_cb_p_mp_nh(struct snl_state *ss __unused, void *_target)
{
struct rta_mpath_nh *target = (struct rta_mpath_nh *)_target;
finalize_sockaddr(target->gw, target->ifindex);
return (true);
}
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER_EXT(_mpath_nh_parser, sizeof(struct rtnexthop),
sizeof(struct rta_mpath_nh), _fp_p_mp_nh, _nla_p_mp_nh,
_cb_p_mp_nh);
struct rta_mpath {
uint32_t num_nhops;
struct rta_mpath_nh **nhops;
};
static bool
nlattr_get_multipath(struct snl_state *ss, struct nlattr *nla,
const void *arg __unused, void *target)
{
uint32_t start_size = 4;
while (start_size < NLA_DATA_LEN(nla) / sizeof(struct rtnexthop))
start_size *= 2;
return (snl_attr_get_parray_sz(ss, nla, start_size, &_mpath_nh_parser, target));
}
struct snl_parsed_route {
struct sockaddr *rta_dst;
struct sockaddr *rta_gw;
struct nlattr *rta_metrics;
struct rta_mpath rta_multipath;
uint32_t rta_expires;
uint32_t rta_oif;
uint32_t rta_expire;
uint32_t rta_table;
uint32_t rta_knh_id;
uint32_t rta_nh_id;
uint32_t rta_rtflags;
uint32_t rtax_mtu;
uint32_t rtax_weight;
uint8_t rtm_family;
uint8_t rtm_type;
uint8_t rtm_protocol;
uint8_t rtm_dst_len;
};
#define _IN(_field) offsetof(struct rtmsg, _field)
#define _OUT(_field) offsetof(struct snl_parsed_route, _field)
static const struct snl_attr_parser _nla_p_rtmetrics[] = {
{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = snl_attr_get_uint32 },
};
SNL_DECLARE_ATTR_PARSER(_metrics_parser, _nla_p_rtmetrics);
static const struct snl_attr_parser _nla_p_route[] = {
{ .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = snl_attr_get_ip },
{ .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = snl_attr_get_ip },
{ .type = NL_RTA_METRICS, .arg = &_metrics_parser, .cb = snl_attr_get_nested },
{ .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath },
{ .type = NL_RTA_KNH_ID, .off = _OUT(rta_knh_id), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_WEIGHT, .off = _OUT(rtax_weight), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = snl_attr_get_ipvia },
{ .type = NL_RTA_EXPIRES, .off = _OUT(rta_expire), .cb = snl_attr_get_uint32 },
{ .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = snl_attr_get_uint32 },
};
static const struct snl_field_parser _fp_p_route[] = {
{.off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = snl_field_get_uint8 },
{.off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = snl_field_get_uint8 },
{.off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = snl_field_get_uint8 },
{.off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = snl_field_get_uint8 },
};
static inline bool
_cb_p_route(struct snl_state *ss __unused, void *_target)
{
struct snl_parsed_route *target = (struct snl_parsed_route *)_target;
finalize_sockaddr(target->rta_dst, target->rta_oif);
finalize_sockaddr(target->rta_gw, target->rta_oif);
return (true);
}
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER_EXT(snl_rtm_route_parser, sizeof(struct rtmsg),
sizeof(struct snl_parsed_route), _fp_p_route, _nla_p_route,
_cb_p_route);
/* RTM_<NEW|DEL|GET>LINK message parser */
struct snl_parsed_link {
uint32_t ifi_index;
uint32_t ifi_flags;
uint32_t ifi_change;
uint16_t ifi_type;
uint8_t ifla_operstate;
uint8_t ifla_carrier;
uint32_t ifla_mtu;
char *ifla_ifname;
struct nlattr *ifla_address;
struct nlattr *ifla_broadcast;
char *ifla_ifalias;
uint32_t ifla_promiscuity;
struct rtnl_link_stats64 *ifla_stats64;
struct nlattr *iflaf_orig_hwaddr;
struct snl_attr_bitset iflaf_caps;
};
#define _IN(_field) offsetof(struct ifinfomsg, _field)
#define _OUT(_field) offsetof(struct snl_parsed_link, _field)
static const struct snl_attr_parser _nla_p_link_fbsd[] = {
{ .type = IFLAF_ORIG_HWADDR, .off = _OUT(iflaf_orig_hwaddr), .cb = snl_attr_dup_nla },
{ .type = IFLAF_CAPS, .off = _OUT(iflaf_caps), .cb = snl_attr_get_bitset_c },
};
SNL_DECLARE_ATTR_PARSER(_link_fbsd_parser, _nla_p_link_fbsd);
static const struct snl_attr_parser _nla_p_link[] = {
{ .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = snl_attr_dup_nla },
{ .type = IFLA_BROADCAST, .off = _OUT(ifla_broadcast), .cb = snl_attr_dup_nla },
{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_dup_string },
{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
{ .type = IFLA_OPERSTATE, .off = _OUT(ifla_operstate), .cb = snl_attr_get_uint8 },
{ .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = snl_attr_dup_string },
{ .type = IFLA_STATS64, .off = _OUT(ifla_stats64), .cb = snl_attr_dup_struct },
{ .type = IFLA_PROMISCUITY, .off = _OUT(ifla_promiscuity), .cb = snl_attr_get_uint32 },
{ .type = IFLA_CARRIER, .off = _OUT(ifla_carrier), .cb = snl_attr_get_uint8 },
{ .type = IFLA_FREEBSD, .arg = &_link_fbsd_parser, .cb = snl_attr_get_nested },
};
static const struct snl_field_parser _fp_p_link[] = {
{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
{.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 },
{.off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = snl_field_get_uint32 },
{.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 },
};
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER(snl_rtm_link_parser, struct ifinfomsg, _fp_p_link, _nla_p_link);
struct snl_parsed_link_simple {
uint32_t ifi_index;
uint32_t ifla_mtu;
uint16_t ifi_type;
uint32_t ifi_flags;
char *ifla_ifname;
};
#define _IN(_field) offsetof(struct ifinfomsg, _field)
#define _OUT(_field) offsetof(struct snl_parsed_link_simple, _field)
static struct snl_attr_parser _nla_p_link_s[] = {
{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_dup_string },
{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
};
static struct snl_field_parser _fp_p_link_s[] = {
{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
{.off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = snl_field_get_uint16 },
{.off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = snl_field_get_uint32 },
};
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER(snl_rtm_link_parser_simple, struct ifinfomsg, _fp_p_link_s, _nla_p_link_s);
struct snl_parsed_neigh {
uint8_t ndm_family;
uint8_t ndm_flags;
uint16_t ndm_state;
uint32_t nda_ifindex;
uint32_t nda_probes;
uint32_t ndaf_next_ts;
struct sockaddr *nda_dst;
struct nlattr *nda_lladdr;
};
#define _IN(_field) offsetof(struct ndmsg, _field)
#define _OUT(_field) offsetof(struct snl_parsed_neigh, _field)
static const struct snl_attr_parser _nla_p_neigh_fbsd[] = {
{ .type = NDAF_NEXT_STATE_TS, .off = _OUT(ndaf_next_ts), .cb = snl_attr_get_uint32 },
};
SNL_DECLARE_ATTR_PARSER(_neigh_fbsd_parser, _nla_p_neigh_fbsd);
static struct snl_attr_parser _nla_p_neigh_s[] = {
{ .type = NDA_DST, .off = _OUT(nda_dst), .cb = snl_attr_get_ip },
{ .type = NDA_LLADDR , .off = _OUT(nda_lladdr), .cb = snl_attr_dup_nla },
{ .type = NDA_PROBES, .off = _OUT(nda_probes), .cb = snl_attr_get_uint32 },
{ .type = NDA_IFINDEX, .off = _OUT(nda_ifindex), .cb = snl_attr_get_uint32 },
{ .type = NDA_FREEBSD, .arg = &_neigh_fbsd_parser, .cb = snl_attr_get_nested },
};
static struct snl_field_parser _fp_p_neigh_s[] = {
{.off_in = _IN(ndm_family), .off_out = _OUT(ndm_family), .cb = snl_field_get_uint8 },
{.off_in = _IN(ndm_flags), .off_out = _OUT(ndm_flags), .cb = snl_field_get_uint8 },
{.off_in = _IN(ndm_state), .off_out = _OUT(ndm_state), .cb = snl_field_get_uint16 },
{.off_in = _IN(ndm_ifindex), .off_out = _OUT(nda_ifindex), .cb = snl_field_get_uint32 },
};
static inline bool
_cb_p_neigh(struct snl_state *ss __unused, void *_target)
{
struct snl_parsed_neigh *target = (struct snl_parsed_neigh *)_target;
finalize_sockaddr(target->nda_dst, target->nda_ifindex);
return (true);
}
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER_EXT(snl_rtm_neigh_parser, sizeof(struct ndmsg),
sizeof(struct snl_parsed_neigh), _fp_p_neigh_s, _nla_p_neigh_s,
_cb_p_neigh);
struct snl_parsed_addr {
uint8_t ifa_family;
uint8_t ifa_prefixlen;
uint32_t ifa_index;
struct sockaddr *ifa_local;
struct sockaddr *ifa_address;
struct sockaddr *ifa_broadcast;
char *ifa_label;
struct ifa_cacheinfo *ifa_cacheinfo;
uint32_t ifaf_vhid;
uint32_t ifaf_flags;
};
#define _IN(_field) offsetof(struct ifaddrmsg, _field)
#define _OUT(_field) offsetof(struct snl_parsed_addr, _field)
static const struct snl_attr_parser _nla_p_addr_fbsd[] = {
{ .type = IFAF_VHID, .off = _OUT(ifaf_vhid), .cb = snl_attr_get_uint32 },
{ .type = IFAF_FLAGS, .off = _OUT(ifaf_flags), .cb = snl_attr_get_uint32 },
};
SNL_DECLARE_ATTR_PARSER(_addr_fbsd_parser, _nla_p_addr_fbsd);
static const struct snl_attr_parser _nla_p_addr_s[] = {
{ .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = snl_attr_get_ip },
{ .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = snl_attr_get_ip },
{ .type = IFA_LABEL, .off = _OUT(ifa_label), .cb = snl_attr_dup_string },
{ .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = snl_attr_get_ip },
{ .type = IFA_CACHEINFO, .off = _OUT(ifa_cacheinfo), .cb = snl_attr_dup_struct },
{ .type = IFA_FREEBSD, .arg = &_addr_fbsd_parser, .cb = snl_attr_get_nested },
};
static const struct snl_field_parser _fp_p_addr_s[] = {
{.off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = snl_field_get_uint8 },
{.off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = snl_field_get_uint8 },
{.off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = snl_field_get_uint32 },
};
static inline bool
_cb_p_addr(struct snl_state *ss __unused, void *_target)
{
struct snl_parsed_addr *target = (struct snl_parsed_addr *)_target;
finalize_sockaddr(target->ifa_address, target->ifa_index);
finalize_sockaddr(target->ifa_local, target->ifa_index);
return (true);
}
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER_EXT(snl_rtm_addr_parser, sizeof(struct ifaddrmsg),
sizeof(struct snl_parsed_addr), _fp_p_addr_s, _nla_p_addr_s,
_cb_p_addr);
struct snl_parsed_nhop {
uint32_t nha_id;
uint8_t nha_blackhole;
uint8_t nha_groups;
uint8_t nhaf_knhops;
uint8_t nhaf_family;
uint32_t nha_oif;
struct sockaddr *nha_gw;
uint8_t nh_family;
uint8_t nh_protocol;
uint32_t nhaf_table;
uint32_t nhaf_kid;
uint32_t nhaf_aif;
};
#define _IN(_field) offsetof(struct nhmsg, _field)
#define _OUT(_field) offsetof(struct snl_parsed_nhop, _field)
static struct snl_attr_parser _nla_p_nh_fbsd[] = {
{ .type = NHAF_KNHOPS, .off = _OUT(nhaf_knhops), .cb = snl_attr_get_flag },
{ .type = NHAF_TABLE, .off = _OUT(nhaf_table), .cb = snl_attr_get_uint32 },
{ .type = NHAF_KID, .off = _OUT(nhaf_kid), .cb = snl_attr_get_uint32 },
{ .type = NHAF_AIF, .off = _OUT(nhaf_aif), .cb = snl_attr_get_uint32 },
};
SNL_DECLARE_ATTR_PARSER(_nh_fbsd_parser, _nla_p_nh_fbsd);
static const struct snl_field_parser _fp_p_nh[] = {
{ .off_in = _IN(nh_family), .off_out = _OUT(nh_family), .cb = snl_field_get_uint8 },
{ .off_in = _IN(nh_protocol), .off_out = _OUT(nh_protocol), .cb = snl_field_get_uint8 },
};
static const struct snl_attr_parser _nla_p_nh[] = {
{ .type = NHA_ID, .off = _OUT(nha_id), .cb = snl_attr_get_uint32 },
{ .type = NHA_BLACKHOLE, .off = _OUT(nha_blackhole), .cb = snl_attr_get_flag },
{ .type = NHA_OIF, .off = _OUT(nha_oif), .cb = snl_attr_get_uint32 },
{ .type = NHA_GATEWAY, .off = _OUT(nha_gw), .cb = snl_attr_get_ip },
{ .type = NHA_FREEBSD, .arg = &_nh_fbsd_parser, .cb = snl_attr_get_nested },
};
static inline bool
_cb_p_nh(struct snl_state *ss __unused, void *_target)
{
struct snl_parsed_nhop *target = (struct snl_parsed_nhop *)_target;
finalize_sockaddr(target->nha_gw, target->nha_oif);
return (true);
}
#undef _IN
#undef _OUT
SNL_DECLARE_PARSER_EXT(snl_nhmsg_parser, sizeof(struct nhmsg),
sizeof(struct snl_parsed_nhop), _fp_p_nh, _nla_p_nh, _cb_p_nh);
#endif
|