]> git.proxmox.com Git - mirror_iproute2.git/blob - include/libnetlink.h
bridge: fdb: add support for src_vni option
[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
49 void rtnl_close(struct rtnl_handle *rth);
50 void rtnl_set_strict_dump(struct rtnl_handle *rth);
51
52 typedef int (*req_filter_fn_t)(struct nlmsghdr *nlh, int reqlen);
53
54 int rtnl_addrdump_req(struct rtnl_handle *rth, int family,
55 req_filter_fn_t filter_fn)
56 __attribute__((warn_unused_result));
57 int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family)
58 __attribute__((warn_unused_result));
59 int rtnl_routedump_req(struct rtnl_handle *rth, int family,
60 req_filter_fn_t filter_fn)
61 __attribute__((warn_unused_result));
62 int rtnl_ruledump_req(struct rtnl_handle *rth, int family)
63 __attribute__((warn_unused_result));
64 int rtnl_neighdump_req(struct rtnl_handle *rth, int family,
65 req_filter_fn_t filter_fn)
66 __attribute__((warn_unused_result));
67 int rtnl_neightbldump_req(struct rtnl_handle *rth, int family)
68 __attribute__((warn_unused_result));
69 int rtnl_mdbdump_req(struct rtnl_handle *rth, int family)
70 __attribute__((warn_unused_result));
71 int rtnl_netconfdump_req(struct rtnl_handle *rth, int family)
72 __attribute__((warn_unused_result));
73 int rtnl_nsiddump_req(struct rtnl_handle *rth, int family)
74 __attribute__((warn_unused_result));
75
76 int rtnl_linkdump_req(struct rtnl_handle *rth, int fam)
77 __attribute__((warn_unused_result));
78 int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
79 __attribute__((warn_unused_result));
80
81 int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int fam,
82 req_filter_fn_t fn)
83 __attribute__((warn_unused_result));
84 int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle *rth,
85 req_filter_fn_t filter_fn)
86 __attribute__((warn_unused_result));
87 int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
88 __attribute__((warn_unused_result));
89 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
90 int len)
91 __attribute__((warn_unused_result));
92 int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
93 __attribute__((warn_unused_result));
94
95 struct rtnl_ctrl_data {
96 int nsid;
97 };
98
99 typedef int (*rtnl_filter_t)(struct nlmsghdr *n, void *);
100
101 typedef int (*rtnl_listen_filter_t)(struct rtnl_ctrl_data *,
102 struct nlmsghdr *n, void *);
103
104 typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off,
105 const struct nlmsghdr *inner_nlh);
106
107 struct rtnl_dump_filter_arg {
108 rtnl_filter_t filter;
109 void *arg1;
110 __u16 nc_flags;
111 };
112
113 int rtnl_dump_filter_nc(struct rtnl_handle *rth,
114 rtnl_filter_t filter,
115 void *arg, __u16 nc_flags);
116 #define rtnl_dump_filter(rth, filter, arg) \
117 rtnl_dump_filter_nc(rth, filter, arg, 0)
118 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
119 struct nlmsghdr **answer)
120 __attribute__((warn_unused_result));
121 int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
122 struct nlmsghdr **answer)
123 __attribute__((warn_unused_result));
124 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
125 struct nlmsghdr **answer)
126 __attribute__((warn_unused_result));
127 int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
128 __attribute__((warn_unused_result));
129 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
130 __attribute__((warn_unused_result));
131 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn);
132
133 int addattr(struct nlmsghdr *n, int maxlen, int type);
134 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
135 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
136 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
137 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
138 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
139
140 int addattr_l(struct nlmsghdr *n, int maxlen, int type,
141 const void *data, int alen);
142 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
143 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
144 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
145 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
146 const void *data, int len);
147 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *nest);
148 int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data);
149 int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data);
150 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data);
151 int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data);
152 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
153 const void *data, int alen);
154
155 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len);
156 int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
157 int len, unsigned short flags);
158 struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len);
159 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, int len);
160
161 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type);
162 int rta_nest_end(struct rtattr *rta, struct rtattr *nest);
163
164 #define RTA_TAIL(rta) \
165 ((struct rtattr *) (((void *) (rta)) + \
166 RTA_ALIGN((rta)->rta_len)))
167
168 #define parse_rtattr_nested(tb, max, rta) \
169 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
170
171 #define parse_rtattr_one_nested(type, rta) \
172 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
173
174 #define parse_rtattr_nested_compat(tb, max, rta, data, len) \
175 ({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
176 __parse_rtattr_nested_compat(tb, max, rta, len); })
177
178 static inline __u8 rta_getattr_u8(const struct rtattr *rta)
179 {
180 return *(__u8 *)RTA_DATA(rta);
181 }
182 static inline __u16 rta_getattr_u16(const struct rtattr *rta)
183 {
184 return *(__u16 *)RTA_DATA(rta);
185 }
186 static inline __be16 rta_getattr_be16(const struct rtattr *rta)
187 {
188 return ntohs(rta_getattr_u16(rta));
189 }
190 static inline __u32 rta_getattr_u32(const struct rtattr *rta)
191 {
192 return *(__u32 *)RTA_DATA(rta);
193 }
194 static inline __be32 rta_getattr_be32(const struct rtattr *rta)
195 {
196 return ntohl(rta_getattr_u32(rta));
197 }
198 static inline __u64 rta_getattr_u64(const struct rtattr *rta)
199 {
200 __u64 tmp;
201
202 memcpy(&tmp, RTA_DATA(rta), sizeof(__u64));
203 return tmp;
204 }
205 static inline __s32 rta_getattr_s32(const struct rtattr *rta)
206 {
207 return *(__s32 *)RTA_DATA(rta);
208 }
209 static inline __s64 rta_getattr_s64(const struct rtattr *rta)
210 {
211 __s64 tmp;
212
213 memcpy(&tmp, RTA_DATA(rta), sizeof(tmp));
214 return tmp;
215 }
216 static inline const char *rta_getattr_str(const struct rtattr *rta)
217 {
218 return (const char *)RTA_DATA(rta);
219 }
220
221 int rtnl_listen_all_nsid(struct rtnl_handle *);
222 int rtnl_listen(struct rtnl_handle *, rtnl_listen_filter_t handler,
223 void *jarg);
224 int rtnl_from_file(FILE *, rtnl_listen_filter_t handler,
225 void *jarg);
226
227 #define NLMSG_TAIL(nmsg) \
228 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
229
230 #ifndef IFA_RTA
231 #define IFA_RTA(r) \
232 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
233 #endif
234 #ifndef IFA_PAYLOAD
235 #define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg))
236 #endif
237
238 #ifndef IFLA_RTA
239 #define IFLA_RTA(r) \
240 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
241 #endif
242 #ifndef IFLA_PAYLOAD
243 #define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg))
244 #endif
245
246 #ifndef NDA_RTA
247 #define NDA_RTA(r) \
248 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
249 #endif
250 #ifndef NDA_PAYLOAD
251 #define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndmsg))
252 #endif
253
254 #ifndef NDTA_RTA
255 #define NDTA_RTA(r) \
256 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
257 #endif
258 #ifndef NDTA_PAYLOAD
259 #define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg))
260 #endif
261
262 #ifndef NETNS_RTA
263 #define NETNS_RTA(r) \
264 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
265 #endif
266 #ifndef NETNS_PAYLOAD
267 #define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg))
268 #endif
269
270 #ifndef IFLA_STATS_RTA
271 #define IFLA_STATS_RTA(r) \
272 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg))))
273 #endif
274
275 /* User defined nlmsg_type which is used mostly for logging netlink
276 * messages from dump file */
277 #define NLMSG_TSTAMP 15
278
279 #endif /* __LIBNETLINK_H__ */