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