1*ce444a00SDaniel Borkmann /* SPDX-License-Identifier: GPL-2.0-or-later */
2*ce444a00SDaniel Borkmann #ifndef NETLINK_HELPERS_H
3*ce444a00SDaniel Borkmann #define NETLINK_HELPERS_H
4*ce444a00SDaniel Borkmann 
5*ce444a00SDaniel Borkmann #include <string.h>
6*ce444a00SDaniel Borkmann #include <linux/netlink.h>
7*ce444a00SDaniel Borkmann #include <linux/rtnetlink.h>
8*ce444a00SDaniel Borkmann 
9*ce444a00SDaniel Borkmann struct rtnl_handle {
10*ce444a00SDaniel Borkmann 	int			fd;
11*ce444a00SDaniel Borkmann 	struct sockaddr_nl	local;
12*ce444a00SDaniel Borkmann 	struct sockaddr_nl	peer;
13*ce444a00SDaniel Borkmann 	__u32			seq;
14*ce444a00SDaniel Borkmann 	__u32			dump;
15*ce444a00SDaniel Borkmann 	int			proto;
16*ce444a00SDaniel Borkmann 	FILE			*dump_fp;
17*ce444a00SDaniel Borkmann #define RTNL_HANDLE_F_LISTEN_ALL_NSID		0x01
18*ce444a00SDaniel Borkmann #define RTNL_HANDLE_F_SUPPRESS_NLERR		0x02
19*ce444a00SDaniel Borkmann #define RTNL_HANDLE_F_STRICT_CHK		0x04
20*ce444a00SDaniel Borkmann 	int			flags;
21*ce444a00SDaniel Borkmann };
22*ce444a00SDaniel Borkmann 
23*ce444a00SDaniel Borkmann #define NLMSG_TAIL(nmsg) \
24*ce444a00SDaniel Borkmann 	((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
25*ce444a00SDaniel Borkmann 
26*ce444a00SDaniel Borkmann typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off,
27*ce444a00SDaniel Borkmann 			       const struct nlmsghdr *inner_nlh);
28*ce444a00SDaniel Borkmann 
29*ce444a00SDaniel Borkmann int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
30*ce444a00SDaniel Borkmann 	      __attribute__((warn_unused_result));
31*ce444a00SDaniel Borkmann void rtnl_close(struct rtnl_handle *rth);
32*ce444a00SDaniel Borkmann int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
33*ce444a00SDaniel Borkmann 	      struct nlmsghdr **answer)
34*ce444a00SDaniel Borkmann 	      __attribute__((warn_unused_result));
35*ce444a00SDaniel Borkmann 
36*ce444a00SDaniel Borkmann int addattr(struct nlmsghdr *n, int maxlen, int type);
37*ce444a00SDaniel Borkmann int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
38*ce444a00SDaniel Borkmann int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
39*ce444a00SDaniel Borkmann int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
40*ce444a00SDaniel Borkmann int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
41*ce444a00SDaniel Borkmann int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
42*ce444a00SDaniel Borkmann int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, int alen);
43*ce444a00SDaniel Borkmann int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
44*ce444a00SDaniel Borkmann struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
45*ce444a00SDaniel Borkmann int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
46*ce444a00SDaniel Borkmann #endif /* NETLINK_HELPERS_H */
47