blob: 8d6c53265f54e3f3cc64600c720cc86ea64d58bd [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
tarun.kundubd265042023-04-13 13:53:44 -070037static inline struct nlattr *add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000038{
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.kundubd265042023-04-13 13:53:44 -070043 if (len > 0) {
44 memcpy((void *)attr + NLA_ALIGN(sizeof(struct nlattr)), data, len);
45 }
tarun.kundua4393a72023-04-13 13:08:22 -070046 nlh->nlmsg_len += NLA_ALIGN(payload_len);
tarun.kundubd265042023-04-13 13:53:44 -070047 return attr;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000048}
49
50void ipset_init(void)
51{
Simon Kelley0506a5e2020-03-19 21:56:45 +000052 old_kernel = (daemon->kernel_version < KERNEL_VERSION(2,6,32));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000053
54 if (old_kernel && (ipset_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) != -1)
55 return;
56
57 if (!old_kernel &&
58 (buffer = safe_malloc(BUFF_SZ)) &&
59 (ipset_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER)) != -1 &&
60 (bind(ipset_sock, (struct sockaddr *)&snl, sizeof(snl)) != -1))
61 return;
62
63 die (_("failed to create IPset control socket: %s"), NULL, EC_MISC);
64}
65
tarun.kundubd265042023-04-13 13:53:44 -070066/*
67 * <------- NLA_HDRLEN ------> <-- NLA_ALIGN(payload)-->
68 * +---------------------+- - -+- - - - - - - - - -+- - -+
69 * | Header | Pad | Payload | Pad |
70 * | (struct nlattr) | ing | | ing |
71 * +---------------------+- - -+- - - - - - - - - -+- - -+
72 * <-------------- nlattr->nla_len -------------->
73 *
74 * For nested attributes, nla_len of IPSET_ATTR_DATA nlattr
75 * need to be updated appropriately after adding each attribute
76 *
77 * nla_type (16 bits)
78 * +---+---+-------------------------------+
79 * | N | O | Attribute Type |
80 * +---+---+-------------------------------+
81 * N := Carries nested attributes
82 * O := Payload stored in network byte order
83 *
84 * Note: The N and O flag are mutually exclusive.
85 */
86
Simon Kelleycc921df2019-01-02 22:48:59 +000087static 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 +000088{
89 struct nlmsghdr *nlh;
tarun.kundua4393a72023-04-13 13:08:22 -070090 struct nfgenmsg *nfg;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000091 uint8_t proto;
Kevin Darbyshire-Bryant3becf462018-12-12 12:00:19 +000092 int addrsz = (af == AF_INET6) ? IN6ADDRSZ : INADDRSZ;
tarun.kundubd265042023-04-13 13:53:44 -070093 struct nlattr *ipset_attr_data_ptr = NULL;
94 struct nlattr *ipset_attr_ip_ptr = NULL;
95 struct nlattr *ipset_attr_ip_addr_ptr = NULL;
Simon Kelleyab6ede72013-02-23 19:22:37 +000096
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +000097 if (strlen(setname) >= IPSET_MAXNAMELEN)
98 {
99 errno = ENAMETOOLONG;
100 return -1;
101 }
102
Dave Reisner4582c0e2013-04-18 09:47:49 +0100103 memset(buffer, 0, BUFF_SZ);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000104
105 nlh = (struct nlmsghdr *)buffer;
tarun.kundua4393a72023-04-13 13:08:22 -0700106 nlh->nlmsg_len = NLA_ALIGN(sizeof(struct nlmsghdr));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000107 nlh->nlmsg_type = (remove ? IPSET_CMD_DEL : IPSET_CMD_ADD) | (NFNL_SUBSYS_IPSET << 8);
108 nlh->nlmsg_flags = NLM_F_REQUEST;
109
tarun.kundua4393a72023-04-13 13:08:22 -0700110 nfg = (struct nfgenmsg *)(buffer + nlh->nlmsg_len);
111 nlh->nlmsg_len += NLA_ALIGN(sizeof(struct nfgenmsg));
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000112 nfg->nfgen_family = af;
113 nfg->version = NFNETLINK_V0;
114 nfg->res_id = htons(0);
115
116 proto = IPSET_PROTOCOL;
117 add_attr(nlh, IPSET_ATTR_PROTOCOL, sizeof(proto), &proto);
118 add_attr(nlh, IPSET_ATTR_SETNAME, strlen(setname) + 1, setname);
tarun.kundubd265042023-04-13 13:53:44 -0700119 ipset_attr_data_ptr = add_attr(nlh, NLA_F_NESTED | IPSET_ATTR_DATA, 0, NULL);
120 ipset_attr_ip_ptr = add_attr(nlh, NLA_F_NESTED | IPSET_ATTR_IP, 0, NULL);
121 ipset_attr_data_ptr->nla_len += ipset_attr_ip_ptr->nla_len;
122 ipset_attr_ip_addr_ptr = add_attr(nlh,
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000123 (af == AF_INET ? IPSET_ATTR_IPADDR_IPV4 : IPSET_ATTR_IPADDR_IPV6) | NLA_F_NET_BYTEORDER,
Simon Kelleycc921df2019-01-02 22:48:59 +0000124 addrsz, ipaddr);
tarun.kundubd265042023-04-13 13:53:44 -0700125 ipset_attr_ip_ptr->nla_len += ipset_attr_ip_addr_ptr->nla_len;
126 ipset_attr_data_ptr->nla_len += ipset_attr_ip_addr_ptr->nla_len;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000127
Simon Kelleyff841eb2015-03-11 21:36:30 +0000128 while (retry_send(sendto(ipset_sock, buffer, nlh->nlmsg_len, 0,
129 (struct sockaddr *)&snl, sizeof(snl))));
130
131 return errno == 0 ? 0 : -1;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000132}
133
134
Simon Kelleycc921df2019-01-02 22:48:59 +0000135static int old_add_to_ipset(const char *setname, const union all_addr *ipaddr, int remove)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000136{
137 socklen_t size;
138 struct ip_set_req_adt_get {
139 unsigned op;
140 unsigned version;
141 union {
142 char name[IPSET_MAXNAMELEN];
143 uint16_t index;
144 } set;
145 char typename[IPSET_MAXNAMELEN];
146 } req_adt_get;
147 struct ip_set_req_adt {
148 unsigned op;
149 uint16_t index;
150 uint32_t ip;
151 } req_adt;
152
153 if (strlen(setname) >= sizeof(req_adt_get.set.name))
154 {
155 errno = ENAMETOOLONG;
156 return -1;
157 }
158
159 req_adt_get.op = 0x10;
160 req_adt_get.version = 3;
161 strcpy(req_adt_get.set.name, setname);
162 size = sizeof(req_adt_get);
163 if (getsockopt(ipset_sock, SOL_IP, 83, &req_adt_get, &size) < 0)
164 return -1;
165 req_adt.op = remove ? 0x102 : 0x101;
166 req_adt.index = req_adt_get.set.index;
Simon Kelleycc921df2019-01-02 22:48:59 +0000167 req_adt.ip = ntohl(ipaddr->addr4.s_addr);
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000168 if (setsockopt(ipset_sock, SOL_IP, 83, &req_adt, sizeof(req_adt)) < 0)
169 return -1;
170
171 return 0;
172}
173
174
175
Simon Kelleycc921df2019-01-02 22:48:59 +0000176int add_to_ipset(const char *setname, const union all_addr *ipaddr, int flags, int remove)
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000177{
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100178 int ret = 0, af = AF_INET;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000179
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000180 if (flags & F_IPV6)
181 {
182 af = AF_INET6;
183 /* old method only supports IPv4 */
184 if (old_kernel)
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100185 {
186 errno = EAFNOSUPPORT ;
187 ret = -1;
188 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000189 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000190
Simon Kelleyb5e33ae2016-08-28 21:26:42 +0100191 if (ret != -1)
192 ret = old_kernel ? old_add_to_ipset(setname, ipaddr, remove) : new_add_to_ipset(setname, ipaddr, af, remove);
193
194 if (ret == -1)
195 my_syslog(LOG_ERR, _("failed to update ipset %s: %s"), setname, strerror(errno));
196
197 return ret;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000198}
199
200#endif