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