Add a minimal netlink helper library for the BPF selftests. This has been taken and cut down and cleaned up from iproute2. This covers basics such as netdevice creation which we need for BPF selftests / BPF CI given iproute2 package cannot cover it yet. Stanislav Fomichev suggested that this could be replaced in future by ynl tool generated C code once it has RTNL support to create devices. Once we get to this point the BPF CI would also need to add libmnl. If no further extensions are needed, a second option could be that we remove this code again once iproute2 package has support. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Martin KaFai Lau <martin.lau@kernel.org> Link: https://lore.kernel.org/r/20231024214904.29825-7-daniel@iogearbox.net Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
47 lines
1.7 KiB
C
47 lines
1.7 KiB
C
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
#ifndef NETLINK_HELPERS_H
|
|
#define NETLINK_HELPERS_H
|
|
|
|
#include <string.h>
|
|
#include <linux/netlink.h>
|
|
#include <linux/rtnetlink.h>
|
|
|
|
struct rtnl_handle {
|
|
int fd;
|
|
struct sockaddr_nl local;
|
|
struct sockaddr_nl peer;
|
|
__u32 seq;
|
|
__u32 dump;
|
|
int proto;
|
|
FILE *dump_fp;
|
|
#define RTNL_HANDLE_F_LISTEN_ALL_NSID 0x01
|
|
#define RTNL_HANDLE_F_SUPPRESS_NLERR 0x02
|
|
#define RTNL_HANDLE_F_STRICT_CHK 0x04
|
|
int flags;
|
|
};
|
|
|
|
#define NLMSG_TAIL(nmsg) \
|
|
((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
|
|
|
|
typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off,
|
|
const struct nlmsghdr *inner_nlh);
|
|
|
|
int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
|
|
__attribute__((warn_unused_result));
|
|
void rtnl_close(struct rtnl_handle *rth);
|
|
int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
|
|
struct nlmsghdr **answer)
|
|
__attribute__((warn_unused_result));
|
|
|
|
int addattr(struct nlmsghdr *n, int maxlen, int type);
|
|
int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
|
|
int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
|
|
int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
|
|
int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
|
|
int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
|
|
int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, int alen);
|
|
int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
|
|
struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
|
|
int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
|
|
#endif /* NETLINK_HELPERS_H */
|