blob: 5ac859fcce64b959e3a77bc693db1abfa1ba3c80 [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>
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000025#include <arpa/inet.h>
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000026#include <linux/netlink.h>
tarun.kundua4393a72023-04-13 13:08:22 -070027#include <linux/netfilter/ipset/ip_set.h>
28#include <linux/netfilter/nfnetlink.h>
Simon Kelleyd5052fb2013-04-25 12:44:20 +010029
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000030/* data structure size in here is fixed */
31#define BUFF_SZ 256
32
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000033static const struct sockaddr_nl snl = { .nl_family = AF_NETLINK };
34static int ipset_sock, old_kernel;
35static char *buffer;
36
37static inline void add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data)
38{
tarun.kundua4393a72023-04-13 13:08:22 -070039 struct nlattr *attr = (void *)nlh + NLA_ALIGN(nlh->nlmsg_len);
40 uint16_t payload_len = NLA_ALIGN(sizeof(struct nlattr)) + len;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000041 attr->nla_type = type;
42 attr->nla_len = payload_len;
tarun.kundua4393a72023-04-13 13:08:22 -070043 memcpy((void *)attr + NLA_ALIGN(sizeof(struct nlattr)), data, len);
44 nlh->nlmsg_len += NLA_ALIGN(payload_len);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000045}
46
47void ipset_init(void)
48{
Simon Kelley0506a5e2020-03-19 21:56:45 +000049 old_kernel = (daemon->kernel_version < KERNEL_VERSION(2,6,32));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000050
51 if (old_kernel && (ipset_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) != -1)
52 return;
53
54 if (!old_kernel &&
55 (buffer = safe_malloc(BUFF_SZ)) &&
56 (ipset_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER)) != -1 &&
57 (bind(ipset_sock, (struct sockaddr *)&snl, sizeof(snl)) != -1))
58 return;
59
60 die (_("failed to create IPset control socket: %s"), NULL, EC_MISC);
61}
62
Simon Kelleycc921df2019-01-02 22:48:59 +000063static 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 +000064{
65 struct nlmsghdr *nlh;
tarun.kundua4393a72023-04-13 13:08:22 -070066 struct nfgenmsg *nfg;
67 struct nlattr *nested[2];
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000068 uint8_t proto;
Kevin Darbyshire-Bryant3becf462018-12-12 12:00:19 +000069 int addrsz = (af == AF_INET6) ? IN6ADDRSZ : INADDRSZ;
Simon Kelleyab6ede72013-02-23 19:22:37 +000070
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000071 if (strlen(setname) >= IPSET_MAXNAMELEN)
72 {
73 errno = ENAMETOOLONG;
74 return -1;
75 }
76
Dave Reisner4582c0e2013-04-18 09:47:49 +010077 memset(buffer, 0, BUFF_SZ);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000078
79 nlh = (struct nlmsghdr *)buffer;
tarun.kundua4393a72023-04-13 13:08:22 -070080 nlh->nlmsg_len = NLA_ALIGN(sizeof(struct nlmsghdr));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000081 nlh->nlmsg_type = (remove ? IPSET_CMD_DEL : IPSET_CMD_ADD) | (NFNL_SUBSYS_IPSET << 8);
82 nlh->nlmsg_flags = NLM_F_REQUEST;
83
tarun.kundua4393a72023-04-13 13:08:22 -070084 nfg = (struct nfgenmsg *)(buffer + nlh->nlmsg_len);
85 nlh->nlmsg_len += NLA_ALIGN(sizeof(struct nfgenmsg));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000086 nfg->nfgen_family = af;
87 nfg->version = NFNETLINK_V0;
88 nfg->res_id = htons(0);
89
90 proto = IPSET_PROTOCOL;
91 add_attr(nlh, IPSET_ATTR_PROTOCOL, sizeof(proto), &proto);
92 add_attr(nlh, IPSET_ATTR_SETNAME, strlen(setname) + 1, setname);
tarun.kundua4393a72023-04-13 13:08:22 -070093 nested[0] = (struct nlattr *)(buffer + NLA_ALIGN(nlh->nlmsg_len));
94 nlh->nlmsg_len += NLA_ALIGN(sizeof(struct nlattr));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000095 nested[0]->nla_type = NLA_F_NESTED | IPSET_ATTR_DATA;
tarun.kundua4393a72023-04-13 13:08:22 -070096 nested[1] = (struct nlattr *)(buffer + NLA_ALIGN(nlh->nlmsg_len));
97 nlh->nlmsg_len += NLA_ALIGN(sizeof(struct nlattr));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000098 nested[1]->nla_type = NLA_F_NESTED | IPSET_ATTR_IP;
99 add_attr(nlh,
100 (af == AF_INET ? IPSET_ATTR_IPADDR_IPV4 : IPSET_ATTR_IPADDR_IPV6) | NLA_F_NET_BYTEORDER,
Simon Kelleycc921df2019-01-02 22:48:59 +0000101 addrsz, ipaddr);
tarun.kundua4393a72023-04-13 13:08:22 -0700102 nested[1]->nla_len = (void *)buffer + NLA_ALIGN(nlh->nlmsg_len) - (void *)nested[1];
103 nested[0]->nla_len = (void *)buffer + NLA_ALIGN(nlh->nlmsg_len) - (void *)nested[0];
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000104
Simon Kelleyff841eb2015-03-11 21:36:30 +0000105 while (retry_send(sendto(ipset_sock, buffer, nlh->nlmsg_len, 0,
106 (struct sockaddr *)&snl, sizeof(snl))));
107
108 return errno == 0 ? 0 : -1;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000109}
110
111
Simon Kelleycc921df2019-01-02 22:48:59 +0000112static int old_add_to_ipset(const char *setname, const union all_addr *ipaddr, int remove)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000113{
114 socklen_t size;
115 struct ip_set_req_adt_get {
116 unsigned op;
117 unsigned version;
118 union {
119 char name[IPSET_MAXNAMELEN];
120 uint16_t index;
121 } set;
122 char typename[IPSET_MAXNAMELEN];
123 } req_adt_get;
124 struct ip_set_req_adt {
125 unsigned op;
126 uint16_t index;
127 uint32_t ip;
128 } req_adt;
129
130 if (strlen(setname) >= sizeof(req_adt_get.set.name))
131 {
132 errno = ENAMETOOLONG;
133 return -1;
134 }
135
136 req_adt_get.op = 0x10;
137 req_adt_get.version = 3;
138 strcpy(req_adt_get.set.name, setname);
139 size = sizeof(req_adt_get);
140 if (getsockopt(ipset_sock, SOL_IP, 83, &req_adt_get, &size) < 0)
141 return -1;
142 req_adt.op = remove ? 0x102 : 0x101;
143 req_adt.index = req_adt_get.set.index;
Simon Kelleycc921df2019-01-02 22:48:59 +0000144 req_adt.ip = ntohl(ipaddr->addr4.s_addr);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000145 if (setsockopt(ipset_sock, SOL_IP, 83, &req_adt, sizeof(req_adt)) < 0)
146 return -1;
147
148 return 0;
149}
150
151
152
Simon Kelleycc921df2019-01-02 22:48:59 +0000153int add_to_ipset(const char *setname, const union all_addr *ipaddr, int flags, int remove)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000154{
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100155 int ret = 0, af = AF_INET;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000156
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000157 if (flags & F_IPV6)
158 {
159 af = AF_INET6;
160 /* old method only supports IPv4 */
161 if (old_kernel)
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100162 {
163 errno = EAFNOSUPPORT ;
164 ret = -1;
165 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000166 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000167
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100168 if (ret != -1)
169 ret = old_kernel ? old_add_to_ipset(setname, ipaddr, remove) : new_add_to_ipset(setname, ipaddr, af, remove);
170
171 if (ret == -1)
172 my_syslog(LOG_ERR, _("failed to update ipset %s: %s"), setname, strerror(errno));
173
174 return ret;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000175}
176
177#endif