1aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#ifndef __LIBNETLINK_H__
2aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#define __LIBNETLINK_H__ 1
3aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
4f5b830dc5dab07149157eb007de9a858a57e880bJan Engelhardt#include <string.h>
5aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <asm/types.h>
6aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <linux/netlink.h>
7aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <linux/rtnetlink.h>
8ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#include <linux/if_link.h>
9ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#include <linux/if_addr.h>
10ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#include <linux/neighbour.h>
11aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
12aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerstruct rtnl_handle
13aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger{
14aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	int			fd;
15aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	struct sockaddr_nl	local;
16aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	struct sockaddr_nl	peer;
17aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	__u32			seq;
18aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	__u32			dump;
19aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger};
20aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
217f03191fda39ff09640f093b7ad84f461ffd65b2Patrick McHardyextern int rcvbuf;
227f03191fda39ff09640f093b7ad84f461ffd65b2Patrick McHardy
23aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions);
24c7699875bee00fbcd057fc62c30d6560b044e007net[shemminger]!shemmingerextern int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions, int protocol);
25aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern void rtnl_close(struct rtnl_handle *rth);
26aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int rtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type);
27aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len);
286dc9f016347441fbf94cf851c054b0f45ba32c1cosdl.net!shemminger
29ae665a522bd46bea44c5ea84c89c8b1731954170Stephen Hemmingertypedef int (*rtnl_filter_t)(const struct sockaddr_nl *,
3050772dc51ac02239958e1ebcdb21277fcdf133a7osdl.net!shemminger			     struct nlmsghdr *n, void *);
31b49240ec7eab91418d2f090536bf8cd2205988d6Simon Horman
32b49240ec7eab91418d2f090536bf8cd2205988d6Simon Hormanstruct rtnl_dump_filter_arg
33b49240ec7eab91418d2f090536bf8cd2205988d6Simon Horman{
34b49240ec7eab91418d2f090536bf8cd2205988d6Simon Horman	rtnl_filter_t filter;
35b49240ec7eab91418d2f090536bf8cd2205988d6Simon Horman	void *arg1;
36b49240ec7eab91418d2f090536bf8cd2205988d6Simon Horman};
37b49240ec7eab91418d2f090536bf8cd2205988d6Simon Horman
38b49240ec7eab91418d2f090536bf8cd2205988d6Simon Hormanextern int rtnl_dump_filter_l(struct rtnl_handle *rth,
39b49240ec7eab91418d2f090536bf8cd2205988d6Simon Horman			      const struct rtnl_dump_filter_arg *arg);
406dc9f016347441fbf94cf851c054b0f45ba32c1cosdl.net!shemmingerextern int rtnl_dump_filter(struct rtnl_handle *rth, rtnl_filter_t filter,
41cd70f3f522e04b4d2fa80ae10292379bf223a53bStephen Hemminger			    void *arg);
42aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
43cd70f3f522e04b4d2fa80ae10292379bf223a53bStephen Hemminger		     unsigned groups, struct nlmsghdr *answer);
446cf8398f5f487762586801c25539d8fe5bb33b39Stephen Hemmingerextern int rtnl_send(struct rtnl_handle *rth, const void *buf, int);
456cf8398f5f487762586801c25539d8fe5bb33b39Stephen Hemmingerextern int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int);
46aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
472aa3dd29a75c494bf969586da5489d4dc7d07839Stephen Hemmingerextern int addattr(struct nlmsghdr *n, int maxlen, int type);
482aa3dd29a75c494bf969586da5489d4dc7d07839Stephen Hemmingerextern int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
492aa3dd29a75c494bf969586da5489d4dc7d07839Stephen Hemmingerextern int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
50aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
512aa3dd29a75c494bf969586da5489d4dc7d07839Stephen Hemmingerextern int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
522aa3dd29a75c494bf969586da5489d4dc7d07839Stephen Hemmingerextern int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
532aa3dd29a75c494bf969586da5489d4dc7d07839Stephen Hemminger
546dc9f016347441fbf94cf851c054b0f45ba32c1cosdl.net!shemmingerextern int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, int alen);
55a10ab08b002dfd25a2d48c6f493835140467a272struct nlmsghdr *n, int maxlen, const void *data, int len);extern int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
562f90c9c0bb0d8ed4841cb4c77c2d707ec07f20bfPatrick McHardyextern struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
572f90c9c0bb0d8ed4841cb4c77c2d707ec07f20bfPatrick McHardyextern int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
582f90c9c0bb0d8ed4841cb4c77c2d707ec07f20bfPatrick McHardyextern struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type, const void *data, int len);
592f90c9c0bb0d8ed4841cb4c77c2d707ec07f20bfPatrick McHardyextern int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *nest);
60aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data);
616dc9f016347441fbf94cf851c054b0f45ba32c1cosdl.net!shemmingerextern int rta_addattr_l(struct rtattr *rta, int maxlen, int type, const void *data, int alen);
62aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
63aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len);
64c7699875bee00fbcd057fc62c30d6560b044e007net[shemminger]!shemmingerextern int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len);
652f90c9c0bb0d8ed4841cb4c77c2d707ec07f20bfPatrick McHardyextern int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, int len);
66aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
67753fca4f54bdc4178fac97954746ded6553920a7tb, max, rta) \#define parse_rtattr_nested(tb, max, rta) \
68753fca4f54bdc4178fac97954746ded6553920a7parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))	(parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
692f90c9c0bb0d8ed4841cb4c77c2d707ec07f20bfPatrick McHardy
70cd70f3f522e04b4d2fa80ae10292379bf223a53bStephen Hemminger#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
71cd70f3f522e04b4d2fa80ae10292379bf223a53bStephen Hemminger	({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL;	\
722f90c9c0bb0d8ed4841cb4c77c2d707ec07f20bfPatrick McHardy		__parse_rtattr_nested_compat(tb, max, rta, len); })
737dd03712226d2943e46f427217158707c148d2b3Stephen Hemminger
747dd03712226d2943e46f427217158707c148d2b3Stephen Hemmingerstatic inline __u8 rta_getattr_u8(const struct rtattr *rta)
757dd03712226d2943e46f427217158707c148d2b3Stephen Hemminger{
767dd03712226d2943e46f427217158707c148d2b3Stephen Hemminger	return *(__u8 *)RTA_DATA(rta);
7746c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger}
7846c5d64d6967da4401122159a8d9488c1878be65Stephen Hemmingerstatic inline __u16 rta_getattr_u16(const struct rtattr *rta)
7946c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger{
8046c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger	return *(__u16 *)RTA_DATA(rta);
817dd03712226d2943e46f427217158707c148d2b3Stephen Hemminger}
8246c5d64d6967da4401122159a8d9488c1878be65Stephen Hemmingerstatic inline __u32 rta_getattr_u32(const struct rtattr *rta)
8346c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger{
8446c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger	return *(__u32 *)RTA_DATA(rta);
8546c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger}
8646c5d64d6967da4401122159a8d9488c1878be65Stephen Hemmingerstatic inline __u64 rta_getattr_u64(const struct rtattr *rta)
8746c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger{
8846c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger	__u64 tmp;
8946c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger	memcpy(&tmp, RTA_DATA(rta), sizeof(__u64));
9046c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger	return tmp;
9146c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger}
9246c5d64d6967da4401122159a8d9488c1878be65Stephen Hemmingerstatic inline const char *rta_getattr_str(const struct rtattr *rta)
93cfd2cbd15f03e26fdb8617a9f37a0a25425adfe2Stephen Hemminger{
9446c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger	return (const char *)RTA_DATA(rta);
9546c5d64d6967da4401122159a8d9488c1878be65Stephen Hemminger}
96ae665a522bd46bea44c5ea84c89c8b1731954170Stephen Hemminger
97aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int rtnl_listen(struct rtnl_handle *, rtnl_filter_t handler,
986dc9f016347441fbf94cf851c054b0f45ba32c1cosdl.net!shemminger		       void *jarg);
99aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerextern int rtnl_from_file(FILE *, rtnl_filter_t handler,
100aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		       void *jarg);
101370d67ba5678b222ccbdd19647ae81ccc3c97801nmsg) \
102370d67ba5678b222ccbdd19647ae81ccc3c97801(struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))#define NLMSG_TAIL(nmsg) \
103ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger	((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
104ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger
105ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#ifndef IFA_RTA
106ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#define IFA_RTA(r) \
107ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger	((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
108ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#endif
109ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#ifndef IFA_PAYLOAD
110ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#define IFA_PAYLOAD(n)	NLMSG_PAYLOAD(n,sizeof(struct ifaddrmsg))
111ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#endif
112ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger
113ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#ifndef IFLA_RTA
114ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#define IFLA_RTA(r) \
115ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger	((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
116ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#endif
117ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#ifndef IFLA_PAYLOAD
118ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#define IFLA_PAYLOAD(n)	NLMSG_PAYLOAD(n,sizeof(struct ifinfomsg))
119ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#endif
120ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger
121ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#ifndef NDA_RTA
122ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#define NDA_RTA(r) \
123ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger	((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
124ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#endif
125ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#ifndef NDA_PAYLOAD
126ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#define NDA_PAYLOAD(n)	NLMSG_PAYLOAD(n,sizeof(struct ndmsg))
127ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#endif
128ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger
129ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#ifndef NDTA_RTA
130ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#define NDTA_RTA(r) \
131ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger	((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
132ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#endif
133ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#ifndef NDTA_PAYLOAD
134ead2ba7008c9db02954dae6f180aed8770a0560dStephen Hemminger#define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ndtmsg))
135aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#endif
136aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
137#endif /* __LIBNETLINK_H__ */
138
139