1 #ifndef __LIBNETLINK_H__
2 #define __LIBNETLINK_H__ 1
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>
17 struct sockaddr_nl local
;
18 struct sockaddr_nl peer
;
23 #define RTNL_HANDLE_F_LISTEN_ALL_NSID 0x01
24 #define RTNL_HANDLE_F_SUPPRESS_NLERR 0x02
29 struct nlmsg_list
*next
;
34 struct nlmsg_list
*head
;
35 struct nlmsg_list
*tail
;
40 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
41 __attribute__((warn_unused_result
));
43 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
45 __attribute__((warn_unused_result
));
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
,
52 __attribute__((warn_unused_result
));
54 typedef int (*req_filter_fn_t
)(struct nlmsghdr
*nlh
, int reqlen
);
56 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int fam
, int type
,
58 __attribute__((warn_unused_result
));
59 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
61 __attribute__((warn_unused_result
));
62 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
,
64 __attribute__((warn_unused_result
));
65 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
66 __attribute__((warn_unused_result
));
68 struct rtnl_ctrl_data
{
72 typedef int (*rtnl_filter_t
)(const struct sockaddr_nl
*,
73 struct nlmsghdr
*n
, void *);
75 typedef int (*rtnl_listen_filter_t
)(const struct sockaddr_nl
*,
76 struct rtnl_ctrl_data
*,
77 struct nlmsghdr
*n
, void *);
79 struct rtnl_dump_filter_arg
{
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
,
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
));
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
);
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
);
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
);
133 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
);
134 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
);
136 #define RTA_TAIL(rta) \
137 ((struct rtattr *) (((void *) (rta)) + \
138 RTA_ALIGN((rta)->rta_len)))
140 #define parse_rtattr_nested(tb, max, rta) \
141 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
143 #define parse_rtattr_one_nested(type, rta) \
144 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
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); })
150 static inline __u8
rta_getattr_u8(const struct rtattr
*rta
)
152 return *(__u8
*)RTA_DATA(rta
);
154 static inline __u16
rta_getattr_u16(const struct rtattr
*rta
)
156 return *(__u16
*)RTA_DATA(rta
);
158 static inline __be16
rta_getattr_be16(const struct rtattr
*rta
)
160 return ntohs(rta_getattr_u16(rta
));
162 static inline __u32
rta_getattr_u32(const struct rtattr
*rta
)
164 return *(__u32
*)RTA_DATA(rta
);
166 static inline __be32
rta_getattr_be32(const struct rtattr
*rta
)
168 return ntohl(rta_getattr_u32(rta
));
170 static inline __u64
rta_getattr_u64(const struct rtattr
*rta
)
174 memcpy(&tmp
, RTA_DATA(rta
), sizeof(__u64
));
177 static inline const char *rta_getattr_str(const struct rtattr
*rta
)
179 return (const char *)RTA_DATA(rta
);
182 int rtnl_listen_all_nsid(struct rtnl_handle
*);
183 int rtnl_listen(struct rtnl_handle
*, rtnl_listen_filter_t handler
,
185 int rtnl_from_file(FILE *, rtnl_listen_filter_t handler
,
188 #define NLMSG_TAIL(nmsg) \
189 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
193 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
196 #define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg))
200 #define IFLA_RTA(r) \
201 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
204 #define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg))
209 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
212 #define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndmsg))
216 #define NDTA_RTA(r) \
217 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
220 #define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg))
224 #define NETNS_RTA(r) \
225 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
227 #ifndef NETNS_PAYLOAD
228 #define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg))
231 #ifndef IFLA_STATS_RTA
232 #define IFLA_STATS_RTA(r) \
233 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg))))
236 /* User defined nlmsg_type which is used mostly for logging netlink
237 * messages from dump file */
238 #define NLMSG_TSTAMP 15
240 #endif /* __LIBNETLINK_H__ */