blob: 7b97ea83eddd92b3ed3abbaa5137b168c1c05bc4 [file] [log] [blame]
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +00001/* ipset.c is Copyright (c) 2013 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17#include "dnsmasq.h"
18
Simon Kelleyc4a09372014-06-02 20:30:07 +010019#if defined(HAVE_IPSET) && defined(HAVE_LINUX_NETWORK)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000020
21#include <string.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/socket.h>
25#include <sys/utsname.h>
26#include <arpa/inet.h>
27#include <linux/version.h>
28#include <linux/netlink.h>
Simon Kelleyd5052fb2013-04-25 12:44:20 +010029
30/* We want to be able to compile against old header files
31 Kernel version is handled at run-time. */
32
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000033#define NFNL_SUBSYS_IPSET 6
Simon Kelleyd5052fb2013-04-25 12:44:20 +010034
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000035#define IPSET_ATTR_DATA 7
36#define IPSET_ATTR_IP 1
37#define IPSET_ATTR_IPADDR_IPV4 1
38#define IPSET_ATTR_IPADDR_IPV6 2
39#define IPSET_ATTR_PROTOCOL 1
40#define IPSET_ATTR_SETNAME 2
41#define IPSET_CMD_ADD 9
42#define IPSET_CMD_DEL 10
43#define IPSET_MAXNAMELEN 32
44#define IPSET_PROTOCOL 6
Simon Kelleyd5052fb2013-04-25 12:44:20 +010045
46#ifndef NFNETLINK_V0
47#define NFNETLINK_V0 0
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000048#endif
49
Simon Kelleyd5052fb2013-04-25 12:44:20 +010050#ifndef NLA_F_NESTED
51#define NLA_F_NESTED (1 << 15)
52#endif
53
54#ifndef NLA_F_NET_BYTEORDER
55#define NLA_F_NET_BYTEORDER (1 << 14)
56#endif
57
58struct my_nlattr {
59 __u16 nla_len;
60 __u16 nla_type;
61};
62
63struct my_nfgenmsg {
64 __u8 nfgen_family; /* AF_xxx */
65 __u8 version; /* nfnetlink version */
66 __be16 res_id; /* resource id */
67};
68
69
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000070/* data structure size in here is fixed */
71#define BUFF_SZ 256
72
73#define NL_ALIGN(len) (((len)+3) & ~(3))
74static const struct sockaddr_nl snl = { .nl_family = AF_NETLINK };
75static int ipset_sock, old_kernel;
76static char *buffer;
77
78static inline void add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data)
79{
Simon Kelleyd5052fb2013-04-25 12:44:20 +010080 struct my_nlattr *attr = (void *)nlh + NL_ALIGN(nlh->nlmsg_len);
81 uint16_t payload_len = NL_ALIGN(sizeof(struct my_nlattr)) + len;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000082 attr->nla_type = type;
83 attr->nla_len = payload_len;
Simon Kelleyd5052fb2013-04-25 12:44:20 +010084 memcpy((void *)attr + NL_ALIGN(sizeof(struct my_nlattr)), data, len);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000085 nlh->nlmsg_len += NL_ALIGN(payload_len);
86}
87
88void ipset_init(void)
89{
90 struct utsname utsname;
91 int version;
92 char *split;
93
94 if (uname(&utsname) < 0)
95 die(_("failed to find kernel version: %s"), NULL, EC_MISC);
96
97 split = strtok(utsname.release, ".");
98 version = (split ? atoi(split) : 0);
99 split = strtok(NULL, ".");
100 version = version * 256 + (split ? atoi(split) : 0);
101 split = strtok(NULL, ".");
102 version = version * 256 + (split ? atoi(split) : 0);
103 old_kernel = (version < KERNEL_VERSION(2,6,32));
104
105 if (old_kernel && (ipset_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) != -1)
106 return;
107
108 if (!old_kernel &&
109 (buffer = safe_malloc(BUFF_SZ)) &&
110 (ipset_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER)) != -1 &&
111 (bind(ipset_sock, (struct sockaddr *)&snl, sizeof(snl)) != -1))
112 return;
113
114 die (_("failed to create IPset control socket: %s"), NULL, EC_MISC);
115}
116
Simon Kelleycc921df2019-01-02 22:48:59 +0000117static int new_add_to_ipset(const char *setname, const union all_addr *ipaddr, int af, int remove)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000118{
119 struct nlmsghdr *nlh;
Simon Kelleyd5052fb2013-04-25 12:44:20 +0100120 struct my_nfgenmsg *nfg;
121 struct my_nlattr *nested[2];
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000122 uint8_t proto;
Kevin Darbyshire-Bryant3becf462018-12-12 12:00:19 +0000123 int addrsz = (af == AF_INET6) ? IN6ADDRSZ : INADDRSZ;
Simon Kelleyab6ede72013-02-23 19:22:37 +0000124
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000125 if (strlen(setname) >= IPSET_MAXNAMELEN)
126 {
127 errno = ENAMETOOLONG;
128 return -1;
129 }
130
Dave Reisner4582c0e2013-04-18 09:47:49 +0100131 memset(buffer, 0, BUFF_SZ);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000132
133 nlh = (struct nlmsghdr *)buffer;
134 nlh->nlmsg_len = NL_ALIGN(sizeof(struct nlmsghdr));
135 nlh->nlmsg_type = (remove ? IPSET_CMD_DEL : IPSET_CMD_ADD) | (NFNL_SUBSYS_IPSET << 8);
136 nlh->nlmsg_flags = NLM_F_REQUEST;
137
Simon Kelleyd5052fb2013-04-25 12:44:20 +0100138 nfg = (struct my_nfgenmsg *)(buffer + nlh->nlmsg_len);
139 nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nfgenmsg));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000140 nfg->nfgen_family = af;
141 nfg->version = NFNETLINK_V0;
142 nfg->res_id = htons(0);
143
144 proto = IPSET_PROTOCOL;
145 add_attr(nlh, IPSET_ATTR_PROTOCOL, sizeof(proto), &proto);
146 add_attr(nlh, IPSET_ATTR_SETNAME, strlen(setname) + 1, setname);
Simon Kelleyd5052fb2013-04-25 12:44:20 +0100147 nested[0] = (struct my_nlattr *)(buffer + NL_ALIGN(nlh->nlmsg_len));
148 nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nlattr));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000149 nested[0]->nla_type = NLA_F_NESTED | IPSET_ATTR_DATA;
Simon Kelleyd5052fb2013-04-25 12:44:20 +0100150 nested[1] = (struct my_nlattr *)(buffer + NL_ALIGN(nlh->nlmsg_len));
151 nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nlattr));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000152 nested[1]->nla_type = NLA_F_NESTED | IPSET_ATTR_IP;
153 add_attr(nlh,
154 (af == AF_INET ? IPSET_ATTR_IPADDR_IPV4 : IPSET_ATTR_IPADDR_IPV6) | NLA_F_NET_BYTEORDER,
Simon Kelleycc921df2019-01-02 22:48:59 +0000155 addrsz, ipaddr);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000156 nested[1]->nla_len = (void *)buffer + NL_ALIGN(nlh->nlmsg_len) - (void *)nested[1];
157 nested[0]->nla_len = (void *)buffer + NL_ALIGN(nlh->nlmsg_len) - (void *)nested[0];
158
Simon Kelleyff841eb2015-03-11 21:36:30 +0000159 while (retry_send(sendto(ipset_sock, buffer, nlh->nlmsg_len, 0,
160 (struct sockaddr *)&snl, sizeof(snl))));
161
162 return errno == 0 ? 0 : -1;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000163}
164
165
Simon Kelleycc921df2019-01-02 22:48:59 +0000166static int old_add_to_ipset(const char *setname, const union all_addr *ipaddr, int remove)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000167{
168 socklen_t size;
169 struct ip_set_req_adt_get {
170 unsigned op;
171 unsigned version;
172 union {
173 char name[IPSET_MAXNAMELEN];
174 uint16_t index;
175 } set;
176 char typename[IPSET_MAXNAMELEN];
177 } req_adt_get;
178 struct ip_set_req_adt {
179 unsigned op;
180 uint16_t index;
181 uint32_t ip;
182 } req_adt;
183
184 if (strlen(setname) >= sizeof(req_adt_get.set.name))
185 {
186 errno = ENAMETOOLONG;
187 return -1;
188 }
189
190 req_adt_get.op = 0x10;
191 req_adt_get.version = 3;
192 strcpy(req_adt_get.set.name, setname);
193 size = sizeof(req_adt_get);
194 if (getsockopt(ipset_sock, SOL_IP, 83, &req_adt_get, &size) < 0)
195 return -1;
196 req_adt.op = remove ? 0x102 : 0x101;
197 req_adt.index = req_adt_get.set.index;
Simon Kelleycc921df2019-01-02 22:48:59 +0000198 req_adt.ip = ntohl(ipaddr->addr4.s_addr);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000199 if (setsockopt(ipset_sock, SOL_IP, 83, &req_adt, sizeof(req_adt)) < 0)
200 return -1;
201
202 return 0;
203}
204
205
206
Simon Kelleycc921df2019-01-02 22:48:59 +0000207int add_to_ipset(const char *setname, const union all_addr *ipaddr, int flags, int remove)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000208{
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100209 int ret = 0, af = AF_INET;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000210
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000211 if (flags & F_IPV6)
212 {
213 af = AF_INET6;
214 /* old method only supports IPv4 */
215 if (old_kernel)
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100216 {
217 errno = EAFNOSUPPORT ;
218 ret = -1;
219 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000220 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000221
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100222 if (ret != -1)
223 ret = old_kernel ? old_add_to_ipset(setname, ipaddr, remove) : new_add_to_ipset(setname, ipaddr, af, remove);
224
225 if (ret == -1)
226 my_syslog(LOG_ERR, _("failed to update ipset %s: %s"), setname, strerror(errno));
227
228 return ret;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000229}
230
231#endif