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