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 typedef int (*nl_ext_ack_fn_t
)(const char *errmsg
, uint32_t off
,
80 const struct nlmsghdr
*inner_nlh
);
82 struct rtnl_dump_filter_arg
{
88 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
89 const struct rtnl_dump_filter_arg
*arg
);
90 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
92 void *arg
, __u16 nc_flags
);
93 #define rtnl_dump_filter(rth, filter, arg) \
94 rtnl_dump_filter_nc(rth, filter, arg, 0)
95 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
96 struct nlmsghdr
**answer
)
97 __attribute__((warn_unused_result
));
98 int rtnl_talk_extack(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
99 struct nlmsghdr
**answer
, nl_ext_ack_fn_t errfn
)
100 __attribute__((warn_unused_result
));
101 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
102 struct nlmsghdr
**answer
)
103 __attribute__((warn_unused_result
));
104 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int)
105 __attribute__((warn_unused_result
));
106 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int)
107 __attribute__((warn_unused_result
));
109 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
);
110 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
);
111 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
);
112 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
);
113 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
);
114 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *data
);
116 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
,
117 const void *data
, int alen
);
118 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
);
119 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
);
120 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
);
121 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
122 const void *data
, int len
);
123 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*nest
);
124 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
);
125 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
);
126 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
);
127 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
);
128 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
129 const void *data
, int alen
);
131 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
);
132 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
133 int len
, unsigned short flags
);
134 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
135 struct rtattr
*rta
, int len
);
136 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
);
137 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
);
139 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
);
140 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
);
142 #define RTA_TAIL(rta) \
143 ((struct rtattr *) (((void *) (rta)) + \
144 RTA_ALIGN((rta)->rta_len)))
146 #define parse_rtattr_nested(tb, max, rta) \
147 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
149 #define parse_rtattr_one_nested(type, rta) \
150 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
152 #define parse_rtattr_nested_compat(tb, max, rta, data, len) \
153 ({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
154 __parse_rtattr_nested_compat(tb, max, rta, len); })
156 static inline __u8
rta_getattr_u8(const struct rtattr
*rta
)
158 return *(__u8
*)RTA_DATA(rta
);
160 static inline __u16
rta_getattr_u16(const struct rtattr
*rta
)
162 return *(__u16
*)RTA_DATA(rta
);
164 static inline __be16
rta_getattr_be16(const struct rtattr
*rta
)
166 return ntohs(rta_getattr_u16(rta
));
168 static inline __u32
rta_getattr_u32(const struct rtattr
*rta
)
170 return *(__u32
*)RTA_DATA(rta
);
172 static inline __be32
rta_getattr_be32(const struct rtattr
*rta
)
174 return ntohl(rta_getattr_u32(rta
));
176 static inline __u64
rta_getattr_u64(const struct rtattr
*rta
)
180 memcpy(&tmp
, RTA_DATA(rta
), sizeof(__u64
));
183 static inline const char *rta_getattr_str(const struct rtattr
*rta
)
185 return (const char *)RTA_DATA(rta
);
188 int rtnl_listen_all_nsid(struct rtnl_handle
*);
189 int rtnl_listen(struct rtnl_handle
*, rtnl_listen_filter_t handler
,
191 int rtnl_from_file(FILE *, rtnl_listen_filter_t handler
,
194 #define NLMSG_TAIL(nmsg) \
195 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
199 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
202 #define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg))
206 #define IFLA_RTA(r) \
207 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
210 #define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg))
215 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
218 #define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndmsg))
222 #define NDTA_RTA(r) \
223 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
226 #define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg))
230 #define NETNS_RTA(r) \
231 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
233 #ifndef NETNS_PAYLOAD
234 #define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg))
237 #ifndef IFLA_STATS_RTA
238 #define IFLA_STATS_RTA(r) \
239 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg))))
242 /* User defined nlmsg_type which is used mostly for logging netlink
243 * messages from dump file */
244 #define NLMSG_TSTAMP 15
246 #endif /* __LIBNETLINK_H__ */