]> git.proxmox.com Git - mirror_iproute2.git/blame - include/libnetlink.h
libnetlink: Convert GETADDRLABEL dumps to use rtnl_addrlbldump_req
[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
449b824a 26 int flags;
aba5acdf
SH
27};
28
4ad87594
DA
29struct nlmsg_list {
30 struct nlmsg_list *next;
31 struct nlmsghdr h;
32};
33
34struct nlmsg_chain {
35 struct nlmsg_list *head;
36 struct nlmsg_list *tail;
37};
38
7f03191f
PM
39extern int rcvbuf;
40
e9e9365b 41int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
d2468da0
SH
42 __attribute__((warn_unused_result));
43
e9e9365b 44int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
d2468da0
SH
45 int protocol)
46 __attribute__((warn_unused_result));
47
892e2124 48void rtnl_close(struct rtnl_handle *rth);
46917d08
DA
49
50int rtnl_addrdump_req(struct rtnl_handle *rth, int family)
51 __attribute__((warn_unused_result));
39360023
DA
52int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family)
53 __attribute__((warn_unused_result));
bfb27dfa
DA
54int rtnl_routedump_req(struct rtnl_handle *rth, int family)
55 __attribute__((warn_unused_result));
46917d08 56
892e2124 57int rtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type)
d2468da0 58 __attribute__((warn_unused_result));
892e2124 59int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int fam, int type,
d2468da0
SH
60 __u32 filt_mask)
61 __attribute__((warn_unused_result));
b0a4ce62
DA
62
63typedef int (*req_filter_fn_t)(struct nlmsghdr *nlh, int reqlen);
64
65int rtnl_wilddump_req_filter_fn(struct rtnl_handle *rth, int fam, int type,
66 req_filter_fn_t fn)
67 __attribute__((warn_unused_result));
7abf5de6
NA
68int rtnl_wilddump_stats_req_filter(struct rtnl_handle *rth, int fam, int type,
69 __u32 filt_mask)
70 __attribute__((warn_unused_result));
892e2124 71int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
d2468da0
SH
72 int len)
73 __attribute__((warn_unused_result));
0d238ca2
DA
74int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
75 __attribute__((warn_unused_result));
6dc9f016 76
0628cddd 77struct rtnl_ctrl_data {
449b824a 78 int nsid;
0628cddd
ND
79};
80
ae665a52 81typedef int (*rtnl_filter_t)(const struct sockaddr_nl *,
50772dc5 82 struct nlmsghdr *n, void *);
b49240ec 83
0628cddd
ND
84typedef int (*rtnl_listen_filter_t)(const struct sockaddr_nl *,
85 struct rtnl_ctrl_data *,
86 struct nlmsghdr *n, void *);
87
b6432e68
SH
88typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off,
89 const struct nlmsghdr *inner_nlh);
90
e9e9365b 91struct rtnl_dump_filter_arg {
b49240ec
SH
92 rtnl_filter_t filter;
93 void *arg1;
8e72880f 94 __u16 nc_flags;
b49240ec
SH
95};
96
892e2124 97int rtnl_dump_filter_l(struct rtnl_handle *rth,
b49240ec 98 const struct rtnl_dump_filter_arg *arg);
8e72880f
PS
99int rtnl_dump_filter_nc(struct rtnl_handle *rth,
100 rtnl_filter_t filter,
101 void *arg, __u16 nc_flags);
102#define rtnl_dump_filter(rth, filter, arg) \
103 rtnl_dump_filter_nc(rth, filter, arg, 0)
892e2124 104int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 105 struct nlmsghdr **answer)
d2468da0 106 __attribute__((warn_unused_result));
72a2ff39
CM
107int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
108 struct nlmsghdr **answer)
109 __attribute__((warn_unused_result));
b6432e68 110int rtnl_talk_extack(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 111 struct nlmsghdr **answer, nl_ext_ack_fn_t errfn)
b6432e68 112 __attribute__((warn_unused_result));
463d9efa 113int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 114 struct nlmsghdr **answer)
463d9efa 115 __attribute__((warn_unused_result));
892e2124 116int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
d2468da0 117 __attribute__((warn_unused_result));
892e2124 118int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
d2468da0 119 __attribute__((warn_unused_result));
049c5853 120int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn);
aba5acdf 121
892e2124
SH
122int addattr(struct nlmsghdr *n, int maxlen, int type);
123int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
124int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
125int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
126int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
127int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
128
129int addattr_l(struct nlmsghdr *n, int maxlen, int type,
130 const void *data, int alen);
131int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
132struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
133int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
134struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
135 const void *data, int len);
136int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *nest);
303cc9cb
RP
137int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data);
138int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data);
892e2124 139int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data);
303cc9cb 140int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data);
892e2124
SH
141int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
142 const void *data, int alen);
143
144int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len);
145int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
b1b7ce0f 146 int len, unsigned short flags);
892e2124
SH
147int parse_rtattr_byindex(struct rtattr *tb[], int max,
148 struct rtattr *rta, int len);
149struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len);
150int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, int len);
aba5acdf 151
303cc9cb
RP
152struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type);
153int rta_nest_end(struct rtattr *rta, struct rtattr *nest);
154
155#define RTA_TAIL(rta) \
156 ((struct rtattr *) (((void *) (rta)) + \
157 RTA_ALIGN((rta)->rta_len)))
158
753fca4f 159#define parse_rtattr_nested(tb, max, rta) \
160 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
161
decbb437
JP
162#define parse_rtattr_one_nested(type, rta) \
163 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
164
2f90c9c0 165#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
cd70f3f5
SH
166 ({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
167 __parse_rtattr_nested_compat(tb, max, rta, len); })
2f90c9c0 168
7dd03712
SH
169static inline __u8 rta_getattr_u8(const struct rtattr *rta)
170{
171 return *(__u8 *)RTA_DATA(rta);
172}
46c5d64d
SH
173static inline __u16 rta_getattr_u16(const struct rtattr *rta)
174{
175 return *(__u16 *)RTA_DATA(rta);
176}
aab0f610
AV
177static inline __be16 rta_getattr_be16(const struct rtattr *rta)
178{
179 return ntohs(rta_getattr_u16(rta));
180}
7dd03712 181static inline __u32 rta_getattr_u32(const struct rtattr *rta)
46c5d64d
SH
182{
183 return *(__u32 *)RTA_DATA(rta);
184}
aab0f610
AV
185static inline __be32 rta_getattr_be32(const struct rtattr *rta)
186{
187 return ntohl(rta_getattr_u32(rta));
188}
46c5d64d
SH
189static inline __u64 rta_getattr_u64(const struct rtattr *rta)
190{
191 __u64 tmp;
e9e9365b 192
46c5d64d
SH
193 memcpy(&tmp, RTA_DATA(rta), sizeof(__u64));
194 return tmp;
195}
196static inline const char *rta_getattr_str(const struct rtattr *rta)
197{
cfd2cbd1 198 return (const char *)RTA_DATA(rta);
46c5d64d
SH
199}
200
892e2124
SH
201int rtnl_listen_all_nsid(struct rtnl_handle *);
202int rtnl_listen(struct rtnl_handle *, rtnl_listen_filter_t handler,
203 void *jarg);
204int rtnl_from_file(FILE *, rtnl_listen_filter_t handler,
205 void *jarg);
aba5acdf 206
370d67ba 207#define NLMSG_TAIL(nmsg) \
208 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
209
ead2ba70
SH
210#ifndef IFA_RTA
211#define IFA_RTA(r) \
e9e9365b 212 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
ead2ba70
SH
213#endif
214#ifndef IFA_PAYLOAD
e9e9365b 215#define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg))
ead2ba70
SH
216#endif
217
218#ifndef IFLA_RTA
219#define IFLA_RTA(r) \
e9e9365b 220 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
ead2ba70
SH
221#endif
222#ifndef IFLA_PAYLOAD
e9e9365b 223#define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg))
ead2ba70
SH
224#endif
225
226#ifndef NDA_RTA
227#define NDA_RTA(r) \
e9e9365b 228 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
ead2ba70
SH
229#endif
230#ifndef NDA_PAYLOAD
e9e9365b 231#define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndmsg))
ead2ba70
SH
232#endif
233
234#ifndef NDTA_RTA
235#define NDTA_RTA(r) \
e9e9365b 236 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
ead2ba70
SH
237#endif
238#ifndef NDTA_PAYLOAD
e9e9365b 239#define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg))
ead2ba70
SH
240#endif
241
d182ee13
ND
242#ifndef NETNS_RTA
243#define NETNS_RTA(r) \
e9e9365b 244 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
d182ee13
ND
245#endif
246#ifndef NETNS_PAYLOAD
e9e9365b 247#define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg))
d182ee13
ND
248#endif
249
7abf5de6
NA
250#ifndef IFLA_STATS_RTA
251#define IFLA_STATS_RTA(r) \
252 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg))))
253#endif
254
27b14f2e
VK
255/* User defined nlmsg_type which is used mostly for logging netlink
256 * messages from dump file */
257#define NLMSG_TSTAMP 15
258
aba5acdf 259#endif /* __LIBNETLINK_H__ */