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