]> git.proxmox.com Git - mirror_iproute2.git/blame - include/libnetlink.h
iproute: Set ip/ip6 lwtunnel flags
[mirror_iproute2.git] / include / libnetlink.h
CommitLineData
6054c1eb 1/* SPDX-License-Identifier: GPL-2.0 */
aba5acdf
SH
2#ifndef __LIBNETLINK_H__
3#define __LIBNETLINK_H__ 1
4
5bd9dd49 5#include <stdio.h>
f5b830dc 6#include <string.h>
aba5acdf
SH
7#include <asm/types.h>
8#include <linux/netlink.h>
9#include <linux/rtnetlink.h>
ead2ba70
SH
10#include <linux/if_link.h>
11#include <linux/if_addr.h>
12#include <linux/neighbour.h>
9d0efc10 13#include <linux/netconf.h>
aab0f610 14#include <arpa/inet.h>
aba5acdf 15
e9e9365b 16struct rtnl_handle {
aba5acdf
SH
17 int fd;
18 struct sockaddr_nl local;
19 struct sockaddr_nl peer;
20 __u32 seq;
21 __u32 dump;
8a4025f6 22 int proto;
486ccd99 23 FILE *dump_fp;
449b824a 24#define RTNL_HANDLE_F_LISTEN_ALL_NSID 0x01
3ad6d176 25#define RTNL_HANDLE_F_SUPPRESS_NLERR 0x02
285033bf 26#define RTNL_HANDLE_F_STRICT_CHK 0x04
449b824a 27 int flags;
aba5acdf
SH
28};
29
4ad87594
DA
30struct nlmsg_list {
31 struct nlmsg_list *next;
32 struct nlmsghdr h;
33};
34
35struct nlmsg_chain {
36 struct nlmsg_list *head;
37 struct nlmsg_list *tail;
38};
39
7f03191f
PM
40extern int rcvbuf;
41
e9e9365b 42int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
d2468da0
SH
43 __attribute__((warn_unused_result));
44
e9e9365b 45int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
d2468da0
SH
46 int protocol)
47 __attribute__((warn_unused_result));
48
892e2124 49void rtnl_close(struct rtnl_handle *rth);
aea41afc 50void rtnl_set_strict_dump(struct rtnl_handle *rth);
46917d08 51
43fd93ae
DA
52typedef int (*req_filter_fn_t)(struct nlmsghdr *nlh, int reqlen);
53
88470978
DA
54int rtnl_addrdump_req(struct rtnl_handle *rth, int family,
55 req_filter_fn_t filter_fn)
46917d08 56 __attribute__((warn_unused_result));
39360023
DA
57int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family)
58 __attribute__((warn_unused_result));
43fd93ae
DA
59int rtnl_routedump_req(struct rtnl_handle *rth, int family,
60 req_filter_fn_t filter_fn)
bfb27dfa 61 __attribute__((warn_unused_result));
b05d9a3d
DA
62int rtnl_ruledump_req(struct rtnl_handle *rth, int family)
63 __attribute__((warn_unused_result));
f255ab12
DA
64int rtnl_neighdump_req(struct rtnl_handle *rth, int family,
65 req_filter_fn_t filter_fn)
9e0ab19c 66 __attribute__((warn_unused_result));
ff41db8a
DA
67int rtnl_neightbldump_req(struct rtnl_handle *rth, int family)
68 __attribute__((warn_unused_result));
9dbe6df4
DA
69int rtnl_mdbdump_req(struct rtnl_handle *rth, int family)
70 __attribute__((warn_unused_result));
ddee16bc
DA
71int rtnl_netconfdump_req(struct rtnl_handle *rth, int family)
72 __attribute__((warn_unused_result));
efb0b383
DA
73int rtnl_nsiddump_req(struct rtnl_handle *rth, int family)
74 __attribute__((warn_unused_result));
46917d08 75
31ae2912 76int rtnl_linkdump_req(struct rtnl_handle *rth, int fam)
d2468da0 77 __attribute__((warn_unused_result));
31ae2912 78int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
d2468da0 79 __attribute__((warn_unused_result));
b0a4ce62 80
31ae2912 81int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int fam,
b0a4ce62
DA
82 req_filter_fn_t fn)
83 __attribute__((warn_unused_result));
56eeeda9 84int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
7abf5de6 85 __attribute__((warn_unused_result));
892e2124 86int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
d2468da0
SH
87 int len)
88 __attribute__((warn_unused_result));
0d238ca2
DA
89int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
90 __attribute__((warn_unused_result));
6dc9f016 91
0628cddd 92struct rtnl_ctrl_data {
449b824a 93 int nsid;
0628cddd
ND
94};
95
cd554f2c 96typedef int (*rtnl_filter_t)(struct nlmsghdr *n, void *);
b49240ec 97
cd554f2c 98typedef int (*rtnl_listen_filter_t)(struct rtnl_ctrl_data *,
0628cddd
ND
99 struct nlmsghdr *n, void *);
100
b6432e68
SH
101typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off,
102 const struct nlmsghdr *inner_nlh);
103
e9e9365b 104struct rtnl_dump_filter_arg {
b49240ec
SH
105 rtnl_filter_t filter;
106 void *arg1;
8e72880f 107 __u16 nc_flags;
b49240ec
SH
108};
109
8e72880f
PS
110int rtnl_dump_filter_nc(struct rtnl_handle *rth,
111 rtnl_filter_t filter,
112 void *arg, __u16 nc_flags);
113#define rtnl_dump_filter(rth, filter, arg) \
114 rtnl_dump_filter_nc(rth, filter, arg, 0)
892e2124 115int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 116 struct nlmsghdr **answer)
d2468da0 117 __attribute__((warn_unused_result));
72a2ff39
CM
118int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
119 struct nlmsghdr **answer)
120 __attribute__((warn_unused_result));
463d9efa 121int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 122 struct nlmsghdr **answer)
463d9efa 123 __attribute__((warn_unused_result));
892e2124 124int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
d2468da0 125 __attribute__((warn_unused_result));
892e2124 126int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
d2468da0 127 __attribute__((warn_unused_result));
049c5853 128int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn);
aba5acdf 129
892e2124
SH
130int addattr(struct nlmsghdr *n, int maxlen, int type);
131int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
132int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
133int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
134int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
135int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
136
137int addattr_l(struct nlmsghdr *n, int maxlen, int type,
138 const void *data, int alen);
139int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
140struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
141int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
142struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
143 const void *data, int len);
144int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *nest);
303cc9cb
RP
145int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data);
146int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data);
892e2124 147int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data);
303cc9cb 148int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data);
892e2124
SH
149int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
150 const void *data, int alen);
151
152int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len);
153int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
b1b7ce0f 154 int len, unsigned short flags);
892e2124
SH
155struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len);
156int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, int len);
aba5acdf 157
303cc9cb
RP
158struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type);
159int rta_nest_end(struct rtattr *rta, struct rtattr *nest);
160
161#define RTA_TAIL(rta) \
162 ((struct rtattr *) (((void *) (rta)) + \
163 RTA_ALIGN((rta)->rta_len)))
164
753fca4f 165#define parse_rtattr_nested(tb, max, rta) \
166 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
167
decbb437
JP
168#define parse_rtattr_one_nested(type, rta) \
169 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
170
2f90c9c0 171#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
cd70f3f5
SH
172 ({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
173 __parse_rtattr_nested_compat(tb, max, rta, len); })
2f90c9c0 174
7dd03712
SH
175static inline __u8 rta_getattr_u8(const struct rtattr *rta)
176{
177 return *(__u8 *)RTA_DATA(rta);
178}
46c5d64d
SH
179static inline __u16 rta_getattr_u16(const struct rtattr *rta)
180{
181 return *(__u16 *)RTA_DATA(rta);
182}
aab0f610
AV
183static inline __be16 rta_getattr_be16(const struct rtattr *rta)
184{
185 return ntohs(rta_getattr_u16(rta));
186}
7dd03712 187static inline __u32 rta_getattr_u32(const struct rtattr *rta)
46c5d64d
SH
188{
189 return *(__u32 *)RTA_DATA(rta);
190}
aab0f610
AV
191static inline __be32 rta_getattr_be32(const struct rtattr *rta)
192{
193 return ntohl(rta_getattr_u32(rta));
194}
46c5d64d
SH
195static inline __u64 rta_getattr_u64(const struct rtattr *rta)
196{
197 __u64 tmp;
e9e9365b 198
46c5d64d
SH
199 memcpy(&tmp, RTA_DATA(rta), sizeof(__u64));
200 return tmp;
201}
d791f3ad
JSP
202static inline __s32 rta_getattr_s32(const struct rtattr *rta)
203{
204 return *(__s32 *)RTA_DATA(rta);
205}
de63cd90
VCG
206static inline __s64 rta_getattr_s64(const struct rtattr *rta)
207{
208 __s64 tmp;
209
210 memcpy(&tmp, RTA_DATA(rta), sizeof(tmp));
211 return tmp;
212}
46c5d64d
SH
213static inline const char *rta_getattr_str(const struct rtattr *rta)
214{
cfd2cbd1 215 return (const char *)RTA_DATA(rta);
46c5d64d
SH
216}
217
892e2124
SH
218int rtnl_listen_all_nsid(struct rtnl_handle *);
219int rtnl_listen(struct rtnl_handle *, rtnl_listen_filter_t handler,
220 void *jarg);
221int rtnl_from_file(FILE *, rtnl_listen_filter_t handler,
222 void *jarg);
aba5acdf 223
370d67ba 224#define NLMSG_TAIL(nmsg) \
225 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
226
ead2ba70
SH
227#ifndef IFA_RTA
228#define IFA_RTA(r) \
e9e9365b 229 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
ead2ba70
SH
230#endif
231#ifndef IFA_PAYLOAD
e9e9365b 232#define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg))
ead2ba70
SH
233#endif
234
235#ifndef IFLA_RTA
236#define IFLA_RTA(r) \
e9e9365b 237 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
ead2ba70
SH
238#endif
239#ifndef IFLA_PAYLOAD
e9e9365b 240#define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg))
ead2ba70
SH
241#endif
242
243#ifndef NDA_RTA
244#define NDA_RTA(r) \
e9e9365b 245 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
ead2ba70
SH
246#endif
247#ifndef NDA_PAYLOAD
e9e9365b 248#define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndmsg))
ead2ba70
SH
249#endif
250
251#ifndef NDTA_RTA
252#define NDTA_RTA(r) \
e9e9365b 253 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
ead2ba70
SH
254#endif
255#ifndef NDTA_PAYLOAD
e9e9365b 256#define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg))
ead2ba70
SH
257#endif
258
d182ee13
ND
259#ifndef NETNS_RTA
260#define NETNS_RTA(r) \
e9e9365b 261 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
d182ee13
ND
262#endif
263#ifndef NETNS_PAYLOAD
e9e9365b 264#define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg))
d182ee13
ND
265#endif
266
7abf5de6
NA
267#ifndef IFLA_STATS_RTA
268#define IFLA_STATS_RTA(r) \
269 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg))))
270#endif
271
27b14f2e
VK
272/* User defined nlmsg_type which is used mostly for logging netlink
273 * messages from dump file */
274#define NLMSG_TSTAMP 15
275
aba5acdf 276#endif /* __LIBNETLINK_H__ */