version 1.1, 2013/07/29 19:37:40
|
version 1.1.1.3, 2016/11/02 09:57:01
|
Line 16
|
Line 16
|
|
|
#include "dnsmasq.h" |
#include "dnsmasq.h" |
|
|
#ifdef HAVE_IPSET | #if defined(HAVE_IPSET) && defined(HAVE_LINUX_NETWORK) |
|
|
#include <string.h> |
#include <string.h> |
#include <errno.h> |
#include <errno.h> |
Line 26
|
Line 26
|
#include <arpa/inet.h> |
#include <arpa/inet.h> |
#include <linux/version.h> |
#include <linux/version.h> |
#include <linux/netlink.h> |
#include <linux/netlink.h> |
#include <linux/netfilter/nfnetlink.h> | |
#ifndef NFNL_SUBSYS_IPSET | /* We want to be able to compile against old header files |
| Kernel version is handled at run-time. */ |
| |
#define NFNL_SUBSYS_IPSET 6 |
#define NFNL_SUBSYS_IPSET 6 |
|
|
#define IPSET_ATTR_DATA 7 |
#define IPSET_ATTR_DATA 7 |
#define IPSET_ATTR_IP 1 |
#define IPSET_ATTR_IP 1 |
#define IPSET_ATTR_IPADDR_IPV4 1 |
#define IPSET_ATTR_IPADDR_IPV4 1 |
Line 39
|
Line 42
|
#define IPSET_CMD_DEL 10 |
#define IPSET_CMD_DEL 10 |
#define IPSET_MAXNAMELEN 32 |
#define IPSET_MAXNAMELEN 32 |
#define IPSET_PROTOCOL 6 |
#define IPSET_PROTOCOL 6 |
#else | |
#include <linux/netfilter/ipset/ip_set.h> | #ifndef NFNETLINK_V0 |
| #define NFNETLINK_V0 0 |
#endif |
#endif |
|
|
|
#ifndef NLA_F_NESTED |
|
#define NLA_F_NESTED (1 << 15) |
|
#endif |
|
|
|
#ifndef NLA_F_NET_BYTEORDER |
|
#define NLA_F_NET_BYTEORDER (1 << 14) |
|
#endif |
|
|
|
struct my_nlattr { |
|
__u16 nla_len; |
|
__u16 nla_type; |
|
}; |
|
|
|
struct my_nfgenmsg { |
|
__u8 nfgen_family; /* AF_xxx */ |
|
__u8 version; /* nfnetlink version */ |
|
__be16 res_id; /* resource id */ |
|
}; |
|
|
|
|
/* data structure size in here is fixed */ |
/* data structure size in here is fixed */ |
#define BUFF_SZ 256 |
#define BUFF_SZ 256 |
|
|
Line 53 static char *buffer;
|
Line 77 static char *buffer;
|
|
|
static inline void add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data) |
static inline void add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data) |
{ |
{ |
struct nlattr *attr = (void *)nlh + NL_ALIGN(nlh->nlmsg_len); | struct my_nlattr *attr = (void *)nlh + NL_ALIGN(nlh->nlmsg_len); |
uint16_t payload_len = NL_ALIGN(sizeof(struct nlattr)) + len; | uint16_t payload_len = NL_ALIGN(sizeof(struct my_nlattr)) + len; |
attr->nla_type = type; |
attr->nla_type = type; |
attr->nla_len = payload_len; |
attr->nla_len = payload_len; |
memcpy((void *)attr + NL_ALIGN(sizeof(struct nlattr)), data, len); | memcpy((void *)attr + NL_ALIGN(sizeof(struct my_nlattr)), data, len); |
nlh->nlmsg_len += NL_ALIGN(payload_len); |
nlh->nlmsg_len += NL_ALIGN(payload_len); |
} |
} |
|
|
Line 93 void ipset_init(void)
|
Line 117 void ipset_init(void)
|
static int new_add_to_ipset(const char *setname, const struct all_addr *ipaddr, int af, int remove) |
static int new_add_to_ipset(const char *setname, const struct all_addr *ipaddr, int af, int remove) |
{ |
{ |
struct nlmsghdr *nlh; |
struct nlmsghdr *nlh; |
struct nfgenmsg *nfg; | struct my_nfgenmsg *nfg; |
struct nlattr *nested[2]; | struct my_nlattr *nested[2]; |
uint8_t proto; |
uint8_t proto; |
int addrsz = INADDRSZ; |
int addrsz = INADDRSZ; |
ssize_t rc; |
|
|
|
#ifdef HAVE_IPV6 |
#ifdef HAVE_IPV6 |
if (af == AF_INET6) |
if (af == AF_INET6) |
Line 110 static int new_add_to_ipset(const char *setname, const
|
Line 133 static int new_add_to_ipset(const char *setname, const
|
return -1; |
return -1; |
} |
} |
|
|
memset(buffer, 0, sizeof(buffer)); | memset(buffer, 0, BUFF_SZ); |
|
|
nlh = (struct nlmsghdr *)buffer; |
nlh = (struct nlmsghdr *)buffer; |
nlh->nlmsg_len = NL_ALIGN(sizeof(struct nlmsghdr)); |
nlh->nlmsg_len = NL_ALIGN(sizeof(struct nlmsghdr)); |
nlh->nlmsg_type = (remove ? IPSET_CMD_DEL : IPSET_CMD_ADD) | (NFNL_SUBSYS_IPSET << 8); |
nlh->nlmsg_type = (remove ? IPSET_CMD_DEL : IPSET_CMD_ADD) | (NFNL_SUBSYS_IPSET << 8); |
nlh->nlmsg_flags = NLM_F_REQUEST; |
nlh->nlmsg_flags = NLM_F_REQUEST; |
|
|
nfg = (struct nfgenmsg *)(buffer + nlh->nlmsg_len); | nfg = (struct my_nfgenmsg *)(buffer + nlh->nlmsg_len); |
nlh->nlmsg_len += NL_ALIGN(sizeof(struct nfgenmsg)); | nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nfgenmsg)); |
nfg->nfgen_family = af; |
nfg->nfgen_family = af; |
nfg->version = NFNETLINK_V0; |
nfg->version = NFNETLINK_V0; |
nfg->res_id = htons(0); |
nfg->res_id = htons(0); |
Line 126 static int new_add_to_ipset(const char *setname, const
|
Line 149 static int new_add_to_ipset(const char *setname, const
|
proto = IPSET_PROTOCOL; |
proto = IPSET_PROTOCOL; |
add_attr(nlh, IPSET_ATTR_PROTOCOL, sizeof(proto), &proto); |
add_attr(nlh, IPSET_ATTR_PROTOCOL, sizeof(proto), &proto); |
add_attr(nlh, IPSET_ATTR_SETNAME, strlen(setname) + 1, setname); |
add_attr(nlh, IPSET_ATTR_SETNAME, strlen(setname) + 1, setname); |
nested[0] = (struct nlattr *)(buffer + NL_ALIGN(nlh->nlmsg_len)); | nested[0] = (struct my_nlattr *)(buffer + NL_ALIGN(nlh->nlmsg_len)); |
nlh->nlmsg_len += NL_ALIGN(sizeof(struct nlattr)); | nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nlattr)); |
nested[0]->nla_type = NLA_F_NESTED | IPSET_ATTR_DATA; |
nested[0]->nla_type = NLA_F_NESTED | IPSET_ATTR_DATA; |
nested[1] = (struct nlattr *)(buffer + NL_ALIGN(nlh->nlmsg_len)); | nested[1] = (struct my_nlattr *)(buffer + NL_ALIGN(nlh->nlmsg_len)); |
nlh->nlmsg_len += NL_ALIGN(sizeof(struct nlattr)); | nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nlattr)); |
nested[1]->nla_type = NLA_F_NESTED | IPSET_ATTR_IP; |
nested[1]->nla_type = NLA_F_NESTED | IPSET_ATTR_IP; |
add_attr(nlh, |
add_attr(nlh, |
(af == AF_INET ? IPSET_ATTR_IPADDR_IPV4 : IPSET_ATTR_IPADDR_IPV6) | NLA_F_NET_BYTEORDER, |
(af == AF_INET ? IPSET_ATTR_IPADDR_IPV4 : IPSET_ATTR_IPADDR_IPV6) | NLA_F_NET_BYTEORDER, |
Line 138 static int new_add_to_ipset(const char *setname, const
|
Line 161 static int new_add_to_ipset(const char *setname, const
|
nested[1]->nla_len = (void *)buffer + NL_ALIGN(nlh->nlmsg_len) - (void *)nested[1]; |
nested[1]->nla_len = (void *)buffer + NL_ALIGN(nlh->nlmsg_len) - (void *)nested[1]; |
nested[0]->nla_len = (void *)buffer + NL_ALIGN(nlh->nlmsg_len) - (void *)nested[0]; |
nested[0]->nla_len = (void *)buffer + NL_ALIGN(nlh->nlmsg_len) - (void *)nested[0]; |
|
|
while ((rc = sendto(ipset_sock, buffer, nlh->nlmsg_len, 0, | while (retry_send(sendto(ipset_sock, buffer, nlh->nlmsg_len, 0, |
(struct sockaddr *)&snl, sizeof(snl))) == -1 && retry_send()); | (struct sockaddr *)&snl, sizeof(snl)))); |
return rc; | |
| return errno == 0 ? 0 : -1; |
} |
} |
|
|
|
|