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