Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 1 | /* 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 Kelley | c4a0937 | 2014-06-02 20:30:07 +0100 | [diff] [blame] | 19 | #if defined(HAVE_IPSET) && defined(HAVE_LINUX_NETWORK) |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 20 | |
| 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 Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 29 | |
| 30 | /* We want to be able to compile against old header files |
| 31 | Kernel version is handled at run-time. */ |
| 32 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 33 | #define NFNL_SUBSYS_IPSET 6 |
Simon Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 34 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 35 | #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 Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 45 | |
| 46 | #ifndef NFNETLINK_V0 |
| 47 | #define NFNETLINK_V0 0 |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Simon Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 50 | #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 | |
| 58 | struct my_nlattr { |
| 59 | __u16 nla_len; |
| 60 | __u16 nla_type; |
| 61 | }; |
| 62 | |
| 63 | struct my_nfgenmsg { |
| 64 | __u8 nfgen_family; /* AF_xxx */ |
| 65 | __u8 version; /* nfnetlink version */ |
| 66 | __be16 res_id; /* resource id */ |
| 67 | }; |
| 68 | |
| 69 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 70 | /* data structure size in here is fixed */ |
| 71 | #define BUFF_SZ 256 |
| 72 | |
| 73 | #define NL_ALIGN(len) (((len)+3) & ~(3)) |
| 74 | static const struct sockaddr_nl snl = { .nl_family = AF_NETLINK }; |
| 75 | static int ipset_sock, old_kernel; |
| 76 | static char *buffer; |
| 77 | |
| 78 | static inline void add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data) |
| 79 | { |
Simon Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 80 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 82 | attr->nla_type = type; |
| 83 | attr->nla_len = payload_len; |
Simon Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 84 | memcpy((void *)attr + NL_ALIGN(sizeof(struct my_nlattr)), data, len); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 85 | nlh->nlmsg_len += NL_ALIGN(payload_len); |
| 86 | } |
| 87 | |
| 88 | void 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame^] | 117 | static int new_add_to_ipset(const char *setname, const union all_addr *ipaddr, int af, int remove) |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 118 | { |
| 119 | struct nlmsghdr *nlh; |
Simon Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 120 | struct my_nfgenmsg *nfg; |
| 121 | struct my_nlattr *nested[2]; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 122 | uint8_t proto; |
Kevin Darbyshire-Bryant | 3becf46 | 2018-12-12 12:00:19 +0000 | [diff] [blame] | 123 | int addrsz = (af == AF_INET6) ? IN6ADDRSZ : INADDRSZ; |
Simon Kelley | ab6ede7 | 2013-02-23 19:22:37 +0000 | [diff] [blame] | 124 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 125 | if (strlen(setname) >= IPSET_MAXNAMELEN) |
| 126 | { |
| 127 | errno = ENAMETOOLONG; |
| 128 | return -1; |
| 129 | } |
| 130 | |
Dave Reisner | 4582c0e | 2013-04-18 09:47:49 +0100 | [diff] [blame] | 131 | memset(buffer, 0, BUFF_SZ); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 132 | |
| 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 Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 138 | nfg = (struct my_nfgenmsg *)(buffer + nlh->nlmsg_len); |
| 139 | nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nfgenmsg)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 140 | 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 Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 147 | nested[0] = (struct my_nlattr *)(buffer + NL_ALIGN(nlh->nlmsg_len)); |
| 148 | nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nlattr)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 149 | nested[0]->nla_type = NLA_F_NESTED | IPSET_ATTR_DATA; |
Simon Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 150 | nested[1] = (struct my_nlattr *)(buffer + NL_ALIGN(nlh->nlmsg_len)); |
| 151 | nlh->nlmsg_len += NL_ALIGN(sizeof(struct my_nlattr)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 152 | 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame^] | 155 | addrsz, ipaddr); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 156 | 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 Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 159 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame^] | 166 | static int old_add_to_ipset(const char *setname, const union all_addr *ipaddr, int remove) |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 167 | { |
| 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame^] | 198 | req_adt.ip = ntohl(ipaddr->addr4.s_addr); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 199 | 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame^] | 207 | int add_to_ipset(const char *setname, const union all_addr *ipaddr, int flags, int remove) |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 208 | { |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 209 | int ret = 0, af = AF_INET; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 210 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 211 | if (flags & F_IPV6) |
| 212 | { |
| 213 | af = AF_INET6; |
| 214 | /* old method only supports IPv4 */ |
| 215 | if (old_kernel) |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 216 | { |
| 217 | errno = EAFNOSUPPORT ; |
| 218 | ret = -1; |
| 219 | } |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 220 | } |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 221 | |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 222 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | #endif |