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 typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off, 80 const struct nlmsghdr *inner_nlh); 81 82 struct rtnl_dump_filter_arg { 83 rtnl_filter_t filter; 84 void *arg1; 85 __u16 nc_flags; 86 }; 87 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, 91 rtnl_filter_t filter, 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, size_t len) 97 __attribute__((warn_unused_result)); 98 int rtnl_talk_extack(struct rtnl_handle *rtnl, struct nlmsghdr *n, 99 struct nlmsghdr *answer, size_t len, 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, size_t len) 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)); 108 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); 115 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); 130 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); 138 139 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type); 140 int rta_nest_end(struct rtattr *rta, struct rtattr *nest); 141 142 #define RTA_TAIL(rta) \ 143 ((struct rtattr *) (((void *) (rta)) + \ 144 RTA_ALIGN((rta)->rta_len))) 145 146 #define parse_rtattr_nested(tb, max, rta) \ 147 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))) 148 149 #define parse_rtattr_one_nested(type, rta) \ 150 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta))) 151 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); }) 155 156 static inline __u8 rta_getattr_u8(const struct rtattr *rta) 157 { 158 return *(__u8 *)RTA_DATA(rta); 159 } 160 static inline __u16 rta_getattr_u16(const struct rtattr *rta) 161 { 162 return *(__u16 *)RTA_DATA(rta); 163 } 164 static inline __be16 rta_getattr_be16(const struct rtattr *rta) 165 { 166 return ntohs(rta_getattr_u16(rta)); 167 } 168 static inline __u32 rta_getattr_u32(const struct rtattr *rta) 169 { 170 return *(__u32 *)RTA_DATA(rta); 171 } 172 static inline __be32 rta_getattr_be32(const struct rtattr *rta) 173 { 174 return ntohl(rta_getattr_u32(rta)); 175 } 176 static inline __u64 rta_getattr_u64(const struct rtattr *rta) 177 { 178 __u64 tmp; 179 180 memcpy(&tmp, RTA_DATA(rta), sizeof(__u64)); 181 return tmp; 182 } 183 static inline const char *rta_getattr_str(const struct rtattr *rta) 184 { 185 return (const char *)RTA_DATA(rta); 186 } 187 188 int rtnl_listen_all_nsid(struct rtnl_handle *); 189 int rtnl_listen(struct rtnl_handle *, rtnl_listen_filter_t handler, 190 void *jarg); 191 int rtnl_from_file(FILE *, rtnl_listen_filter_t handler, 192 void *jarg); 193 194 #define NLMSG_TAIL(nmsg) \ 195 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) 196 197 #ifndef IFA_RTA 198 #define IFA_RTA(r) \ 199 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg)))) 200 #endif 201 #ifndef IFA_PAYLOAD 202 #define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg)) 203 #endif 204 205 #ifndef IFLA_RTA 206 #define IFLA_RTA(r) \ 207 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg)))) 208 #endif 209 #ifndef IFLA_PAYLOAD 210 #define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg)) 211 #endif 212 213 #ifndef NDA_RTA 214 #define NDA_RTA(r) \ 215 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg)))) 216 #endif 217 #ifndef NDA_PAYLOAD 218 #define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndmsg)) 219 #endif 220 221 #ifndef NDTA_RTA 222 #define NDTA_RTA(r) \ 223 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg)))) 224 #endif 225 #ifndef NDTA_PAYLOAD 226 #define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg)) 227 #endif 228 229 #ifndef NETNS_RTA 230 #define NETNS_RTA(r) \ 231 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg)))) 232 #endif 233 #ifndef NETNS_PAYLOAD 234 #define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg)) 235 #endif 236 237 #ifndef IFLA_STATS_RTA 238 #define IFLA_STATS_RTA(r) \ 239 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg)))) 240 #endif 241 242 /* User defined nlmsg_type which is used mostly for logging netlink 243 * messages from dump file */ 244 #define NLMSG_TSTAMP 15 245 246 #endif /* __LIBNETLINK_H__ */ 247