iproute2/include/libnetlink.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2#ifndef __LIBNETLINK_H__
   3#define __LIBNETLINK_H__ 1
   4
   5#include <stdio.h>
   6#include <string.h>
   7#include <endian.h>
   8#include <asm/types.h>
   9#include <linux/netlink.h>
  10#include <linux/rtnetlink.h>
  11#include <linux/if_link.h>
  12#include <linux/if_addr.h>
  13#include <linux/neighbour.h>
  14#include <linux/netconf.h>
  15#include <arpa/inet.h>
  16
  17struct rtnl_handle {
  18        int                     fd;
  19        struct sockaddr_nl      local;
  20        struct sockaddr_nl      peer;
  21        __u32                   seq;
  22        __u32                   dump;
  23        int                     proto;
  24        FILE                   *dump_fp;
  25#define RTNL_HANDLE_F_LISTEN_ALL_NSID           0x01
  26#define RTNL_HANDLE_F_SUPPRESS_NLERR            0x02
  27#define RTNL_HANDLE_F_STRICT_CHK                0x04
  28        int                     flags;
  29};
  30
  31struct nlmsg_list {
  32        struct nlmsg_list *next;
  33        struct nlmsghdr   h;
  34};
  35
  36struct nlmsg_chain {
  37        struct nlmsg_list *head;
  38        struct nlmsg_list *tail;
  39};
  40
  41struct ipstats_req {
  42        struct nlmsghdr nlh;
  43        struct if_stats_msg ifsm;
  44        char buf[128];
  45};
  46
  47extern int rcvbuf;
  48
  49int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
  50        __attribute__((warn_unused_result));
  51
  52int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
  53                             int protocol)
  54        __attribute__((warn_unused_result));
  55int rtnl_add_nl_group(struct rtnl_handle *rth, unsigned int group)
  56        __attribute__((warn_unused_result));
  57void rtnl_close(struct rtnl_handle *rth);
  58void rtnl_set_strict_dump(struct rtnl_handle *rth);
  59
  60typedef int (*req_filter_fn_t)(struct nlmsghdr *nlh, int reqlen);
  61
  62int rtnl_addrdump_req(struct rtnl_handle *rth, int family,
  63                      req_filter_fn_t filter_fn)
  64        __attribute__((warn_unused_result));
  65int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family)
  66        __attribute__((warn_unused_result));
  67int rtnl_routedump_req(struct rtnl_handle *rth, int family,
  68                       req_filter_fn_t filter_fn)
  69        __attribute__((warn_unused_result));
  70int rtnl_ruledump_req(struct rtnl_handle *rth, int family)
  71        __attribute__((warn_unused_result));
  72int rtnl_neighdump_req(struct rtnl_handle *rth, int family,
  73                       req_filter_fn_t filter_fn)
  74        __attribute__((warn_unused_result));
  75int rtnl_neightbldump_req(struct rtnl_handle *rth, int family)
  76        __attribute__((warn_unused_result));
  77int rtnl_mdbdump_req(struct rtnl_handle *rth, int family)
  78        __attribute__((warn_unused_result));
  79int rtnl_brvlandump_req(struct rtnl_handle *rth, int family, __u32 dump_flags)
  80        __attribute__((warn_unused_result));
  81int rtnl_netconfdump_req(struct rtnl_handle *rth, int family)
  82        __attribute__((warn_unused_result));
  83
  84int rtnl_linkdump_req(struct rtnl_handle *rth, int fam)
  85        __attribute__((warn_unused_result));
  86int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
  87        __attribute__((warn_unused_result));
  88
  89int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int fam,
  90                                req_filter_fn_t fn)
  91        __attribute__((warn_unused_result));
  92int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle *rth,
  93                                    req_filter_fn_t filter_fn)
  94        __attribute__((warn_unused_result));
  95int rtnl_nsiddump_req_filter_fn(struct rtnl_handle *rth, int family,
  96                                req_filter_fn_t filter_fn)
  97        __attribute__((warn_unused_result));
  98int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask,
  99                              int (*filter_fn)(struct ipstats_req *req,
 100                                               void *data),
 101                              void *filter_data)
 102        __attribute__((warn_unused_result));
 103int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
 104                             int len)
 105        __attribute__((warn_unused_result));
 106int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
 107        __attribute__((warn_unused_result));
 108
 109int rtnl_nexthopdump_req(struct rtnl_handle *rth, int family,
 110                         req_filter_fn_t filter_fn)
 111        __attribute__((warn_unused_result));
 112int rtnl_nexthop_bucket_dump_req(struct rtnl_handle *rth, int family,
 113                                 req_filter_fn_t filter_fn)
 114        __attribute__((warn_unused_result));
 115
 116int rtnl_tunneldump_req(struct rtnl_handle *rth, int family, int ifindex,
 117                        __u8 flags)
 118        __attribute__((warn_unused_result));
 119
 120struct rtnl_ctrl_data {
 121        int     nsid;
 122};
 123
 124typedef int (*rtnl_filter_t)(struct nlmsghdr *n, void *);
 125
 126/**
 127 * rtnl error handler called from
 128 *      rtnl_dump_done()
 129 *      rtnl_dump_error()
 130 *
 131 * Return value is a bitmask of the following values:
 132 * RTNL_LET_NLERR
 133 *      error handled as usual
 134 * RTNL_SUPPRESS_NLMSG_DONE_NLERR
 135 *      error in nlmsg_type == NLMSG_DONE will be suppressed
 136 * RTNL_SUPPRESS_NLMSG_ERROR_NLERR
 137 *      error in nlmsg_type == NLMSG_ERROR will be suppressed
 138 *      and nlmsg will be skipped
 139 * RTNL_SUPPRESS_NLERR - suppress error in both previous cases
 140 */
 141#define RTNL_LET_NLERR                          0x01
 142#define RTNL_SUPPRESS_NLMSG_DONE_NLERR          0x02
 143#define RTNL_SUPPRESS_NLMSG_ERROR_NLERR         0x04
 144#define RTNL_SUPPRESS_NLERR                     0x06
 145typedef int (*rtnl_err_hndlr_t)(struct nlmsghdr *n, void *);
 146
 147typedef int (*rtnl_listen_filter_t)(struct rtnl_ctrl_data *,
 148                                    struct nlmsghdr *n, void *);
 149
 150typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off,
 151                               const struct nlmsghdr *inner_nlh);
 152
 153struct rtnl_dump_filter_arg {
 154        rtnl_filter_t filter;
 155        void *arg1;
 156        rtnl_err_hndlr_t errhndlr;
 157        void *arg2;
 158        __u16 nc_flags;
 159};
 160
 161int rtnl_dump_filter_nc(struct rtnl_handle *rth,
 162                        rtnl_filter_t filter,
 163                        void *arg, __u16 nc_flags);
 164#define rtnl_dump_filter(rth, filter, arg) \
 165        rtnl_dump_filter_nc(rth, filter, arg, 0)
 166int rtnl_dump_filter_errhndlr_nc(struct rtnl_handle *rth,
 167                                 rtnl_filter_t filter,
 168                                 void *arg1,
 169                                 rtnl_err_hndlr_t errhndlr,
 170                                 void *arg2,
 171                                 __u16 nc_flags);
 172#define rtnl_dump_filter_errhndlr(rth, filter, farg, errhndlr, earg) \
 173        rtnl_dump_filter_errhndlr_nc(rth, filter, farg, errhndlr, earg, 0)
 174
 175int rtnl_echo_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, int json,
 176                   int (*print_info)(struct nlmsghdr *n, void *arg))
 177        __attribute__((warn_unused_result));
 178int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
 179              struct nlmsghdr **answer)
 180        __attribute__((warn_unused_result));
 181int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
 182                                   struct nlmsghdr **answer)
 183        __attribute__((warn_unused_result));
 184int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
 185        __attribute__((warn_unused_result));
 186int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
 187        __attribute__((warn_unused_result));
 188int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn);
 189int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, unsigned int offset, int error);
 190
 191int addattr(struct nlmsghdr *n, int maxlen, int type);
 192int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
 193int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
 194int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
 195int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
 196int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
 197
 198int addattr_l(struct nlmsghdr *n, int maxlen, int type,
 199              const void *data, int alen);
 200int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
 201struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
 202int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
 203struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
 204                                   const void *data, int len);
 205int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *nest);
 206int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data);
 207int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data);
 208int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data);
 209int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data);
 210int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
 211                  const void *data, int alen);
 212
 213int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len);
 214int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
 215                              int len, unsigned short flags);
 216struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len);
 217int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, int len);
 218
 219struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type);
 220int rta_nest_end(struct rtattr *rta, struct rtattr *nest);
 221
 222#define RTA_TAIL(rta) \
 223                ((struct rtattr *) (((void *) (rta)) + \
 224                                    RTA_ALIGN((rta)->rta_len)))
 225
 226#define parse_rtattr_nested(tb, max, rta) \
 227        (parse_rtattr_flags((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta), \
 228                            NLA_F_NESTED))
 229
 230#define parse_rtattr_one_nested(type, rta) \
 231        (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
 232
 233#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
 234        ({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL;       \
 235                __parse_rtattr_nested_compat(tb, max, rta, len); })
 236
 237static inline __u8 rta_getattr_u8(const struct rtattr *rta)
 238{
 239        return *(__u8 *)RTA_DATA(rta);
 240}
 241static inline __u16 rta_getattr_u16(const struct rtattr *rta)
 242{
 243        return *(__u16 *)RTA_DATA(rta);
 244}
 245static inline __be16 rta_getattr_be16(const struct rtattr *rta)
 246{
 247        return ntohs(rta_getattr_u16(rta));
 248}
 249static inline __u32 rta_getattr_u32(const struct rtattr *rta)
 250{
 251        return *(__u32 *)RTA_DATA(rta);
 252}
 253static inline __be32 rta_getattr_be32(const struct rtattr *rta)
 254{
 255        return ntohl(rta_getattr_u32(rta));
 256}
 257static inline __u64 rta_getattr_u64(const struct rtattr *rta)
 258{
 259        __u64 tmp;
 260
 261        memcpy(&tmp, RTA_DATA(rta), sizeof(__u64));
 262        return tmp;
 263}
 264static inline __u64 rta_getattr_uint(const struct rtattr *rta)
 265{
 266        switch (RTA_PAYLOAD(rta)) {
 267        case sizeof(__u8):
 268                return rta_getattr_u8(rta);
 269        case sizeof(__u16):
 270                return rta_getattr_u16(rta);
 271        case sizeof(__u32):
 272                return rta_getattr_u32(rta);
 273        case sizeof(__u64):
 274                return rta_getattr_u64(rta);
 275        }
 276        return -1ULL;
 277}
 278
 279static inline __be64 rta_getattr_be64(const struct rtattr *rta)
 280{
 281        return htobe64(rta_getattr_u64(rta));
 282}
 283
 284static inline __s32 rta_getattr_s32(const struct rtattr *rta)
 285{
 286        return *(__s32 *)RTA_DATA(rta);
 287}
 288static inline __s64 rta_getattr_s64(const struct rtattr *rta)
 289{
 290        __s64 tmp;
 291
 292        memcpy(&tmp, RTA_DATA(rta), sizeof(tmp));
 293        return tmp;
 294}
 295static inline const char *rta_getattr_str(const struct rtattr *rta)
 296{
 297        return (const char *)RTA_DATA(rta);
 298}
 299static inline int rta_type(const struct rtattr *rta)
 300{
 301        return rta->rta_type & NLA_TYPE_MASK;
 302}
 303
 304int rtnl_listen_all_nsid(struct rtnl_handle *);
 305int rtnl_listen(struct rtnl_handle *, rtnl_listen_filter_t handler,
 306                void *jarg);
 307int rtnl_from_file(FILE *, rtnl_listen_filter_t handler,
 308                   void *jarg);
 309
 310#define NLMSG_TAIL(nmsg) \
 311        ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
 312
 313#ifndef IFA_RTA
 314#define IFA_RTA(r) \
 315        ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
 316#endif
 317#ifndef IFA_PAYLOAD
 318#define IFA_PAYLOAD(n)  NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg))
 319#endif
 320
 321#ifndef IFLA_RTA
 322#define IFLA_RTA(r) \
 323        ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
 324#endif
 325#ifndef IFLA_PAYLOAD
 326#define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg))
 327#endif
 328
 329#ifndef NDA_RTA
 330#define NDA_RTA(r) \
 331        ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
 332#endif
 333#ifndef NDA_PAYLOAD
 334#define NDA_PAYLOAD(n)  NLMSG_PAYLOAD(n, sizeof(struct ndmsg))
 335#endif
 336
 337#ifndef NDTA_RTA
 338#define NDTA_RTA(r) \
 339        ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
 340#endif
 341#ifndef NDTA_PAYLOAD
 342#define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg))
 343#endif
 344
 345#ifndef NETNS_RTA
 346#define NETNS_RTA(r) \
 347        ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
 348#endif
 349#ifndef NETNS_PAYLOAD
 350#define NETNS_PAYLOAD(n)        NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg))
 351#endif
 352
 353#ifndef IFLA_STATS_RTA
 354#define IFLA_STATS_RTA(r) \
 355        ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg))))
 356#endif
 357
 358#ifndef BRVLAN_RTA
 359#define BRVLAN_RTA(r) \
 360        ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct br_vlan_msg))))
 361#endif
 362
 363#ifndef TUNNEL_RTA
 364#define TUNNEL_RTA(r) \
 365        ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct tunnel_msg))))
 366#endif
 367
 368/* User defined nlmsg_type which is used mostly for logging netlink
 369 * messages from dump file */
 370#define NLMSG_TSTAMP    15
 371
 372#define rtattr_for_each_nested(attr, nest) \
 373        for ((attr) = (void *)RTA_DATA(nest); \
 374             RTA_OK(attr, RTA_PAYLOAD(nest) - ((char *)(attr) - (char *)RTA_DATA((nest)))); \
 375             (attr) = RTA_TAIL((attr)))
 376
 377void nl_print_policy(const struct rtattr *attr, FILE *fp);
 378
 379#endif /* __LIBNETLINK_H__ */
 380