]> git.proxmox.com Git - mirror_iproute2.git/blame - include/libnetlink.h
libnetlink: introduce rtnl_listen_filter_t
[mirror_iproute2.git] / include / libnetlink.h
CommitLineData
aba5acdf
SH
1#ifndef __LIBNETLINK_H__
2#define __LIBNETLINK_H__ 1
3
5bd9dd49 4#include <stdio.h>
f5b830dc 5#include <string.h>
aba5acdf
SH
6#include <asm/types.h>
7#include <linux/netlink.h>
8#include <linux/rtnetlink.h>
ead2ba70
SH
9#include <linux/if_link.h>
10#include <linux/if_addr.h>
11#include <linux/neighbour.h>
9d0efc10 12#include <linux/netconf.h>
aba5acdf
SH
13
14struct rtnl_handle
15{
16 int fd;
17 struct sockaddr_nl local;
18 struct sockaddr_nl peer;
19 __u32 seq;
20 __u32 dump;
8a4025f6 21 int proto;
486ccd99 22 FILE *dump_fp;
aba5acdf
SH
23};
24
7f03191f
PM
25extern int rcvbuf;
26
d2468da0
SH
27extern int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
28 __attribute__((warn_unused_result));
29
30extern int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
31 int protocol)
32 __attribute__((warn_unused_result));
33
aba5acdf 34extern void rtnl_close(struct rtnl_handle *rth);
d2468da0
SH
35extern int rtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type)
36 __attribute__((warn_unused_result));
9eff0e5c 37extern int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int fam, int type,
d2468da0
SH
38 __u32 filt_mask)
39 __attribute__((warn_unused_result));
40extern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
41 int len)
42 __attribute__((warn_unused_result));
6dc9f016 43
0628cddd
ND
44struct rtnl_ctrl_data {
45};
46
ae665a52 47typedef int (*rtnl_filter_t)(const struct sockaddr_nl *,
50772dc5 48 struct nlmsghdr *n, void *);
b49240ec 49
0628cddd
ND
50typedef int (*rtnl_listen_filter_t)(const struct sockaddr_nl *,
51 struct rtnl_ctrl_data *,
52 struct nlmsghdr *n, void *);
53
b49240ec
SH
54struct rtnl_dump_filter_arg
55{
56 rtnl_filter_t filter;
57 void *arg1;
b49240ec
SH
58};
59
60extern int rtnl_dump_filter_l(struct rtnl_handle *rth,
61 const struct rtnl_dump_filter_arg *arg);
6dc9f016 62extern int rtnl_dump_filter(struct rtnl_handle *rth, rtnl_filter_t filter,
cd70f3f5 63 void *arg);
aba5acdf 64extern int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
d2468da0
SH
65 unsigned groups, struct nlmsghdr *answer)
66 __attribute__((warn_unused_result));
67extern int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
68 __attribute__((warn_unused_result));
69extern int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
70 __attribute__((warn_unused_result));
aba5acdf 71
2aa3dd29
SH
72extern int addattr(struct nlmsghdr *n, int maxlen, int type);
73extern int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
74extern int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
aba5acdf 75extern int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
2aa3dd29
SH
76extern int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
77extern int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
78
6dc9f016 79extern int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, int alen);
a10ab08b 80extern int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
2f90c9c0
PM
81extern struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
82extern int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
83extern struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type, const void *data, int len);
84extern int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *nest);
aba5acdf 85extern int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data);
6dc9f016 86extern int rta_addattr_l(struct rtattr *rta, int maxlen, int type, const void *data, int alen);
aba5acdf
SH
87
88extern int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len);
b1b7ce0f
VY
89extern int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
90 int len, unsigned short flags);
c7699875 91extern int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len);
decbb437 92extern struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len);
2f90c9c0 93extern int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, int len);
aba5acdf 94
753fca4f 95#define parse_rtattr_nested(tb, max, rta) \
96 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
97
decbb437
JP
98#define parse_rtattr_one_nested(type, rta) \
99 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
100
2f90c9c0 101#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
cd70f3f5
SH
102 ({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
103 __parse_rtattr_nested_compat(tb, max, rta, len); })
2f90c9c0 104
7dd03712
SH
105static inline __u8 rta_getattr_u8(const struct rtattr *rta)
106{
107 return *(__u8 *)RTA_DATA(rta);
108}
46c5d64d
SH
109static inline __u16 rta_getattr_u16(const struct rtattr *rta)
110{
111 return *(__u16 *)RTA_DATA(rta);
112}
7dd03712 113static inline __u32 rta_getattr_u32(const struct rtattr *rta)
46c5d64d
SH
114{
115 return *(__u32 *)RTA_DATA(rta);
116}
117static inline __u64 rta_getattr_u64(const struct rtattr *rta)
118{
119 __u64 tmp;
120 memcpy(&tmp, RTA_DATA(rta), sizeof(__u64));
121 return tmp;
122}
123static inline const char *rta_getattr_str(const struct rtattr *rta)
124{
cfd2cbd1 125 return (const char *)RTA_DATA(rta);
46c5d64d
SH
126}
127
0628cddd 128extern int rtnl_listen(struct rtnl_handle *, rtnl_listen_filter_t handler,
aba5acdf 129 void *jarg);
0628cddd 130extern int rtnl_from_file(FILE *, rtnl_listen_filter_t handler,
aba5acdf
SH
131 void *jarg);
132
370d67ba 133#define NLMSG_TAIL(nmsg) \
134 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
135
ead2ba70
SH
136#ifndef IFA_RTA
137#define IFA_RTA(r) \
138 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
139#endif
140#ifndef IFA_PAYLOAD
141#define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ifaddrmsg))
142#endif
143
144#ifndef IFLA_RTA
145#define IFLA_RTA(r) \
146 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
147#endif
148#ifndef IFLA_PAYLOAD
149#define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ifinfomsg))
150#endif
151
152#ifndef NDA_RTA
153#define NDA_RTA(r) \
154 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
155#endif
156#ifndef NDA_PAYLOAD
157#define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ndmsg))
158#endif
159
160#ifndef NDTA_RTA
161#define NDTA_RTA(r) \
162 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
163#endif
164#ifndef NDTA_PAYLOAD
165#define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ndtmsg))
166#endif
167
d182ee13
ND
168#ifndef NETNS_RTA
169#define NETNS_RTA(r) \
170 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
171#endif
172#ifndef NETNS_PAYLOAD
173#define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct rtgenmsg))
174#endif
175
27b14f2e
VK
176/* User defined nlmsg_type which is used mostly for logging netlink
177 * messages from dump file */
178#define NLMSG_TSTAMP 15
179
aba5acdf
SH
180#endif /* __LIBNETLINK_H__ */
181