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> |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 25 | #include <arpa/inet.h> |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 26 | #include <linux/netlink.h> |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 27 | #include <linux/netfilter/ipset/ip_set.h> |
| 28 | #include <linux/netfilter/nfnetlink.h> |
Simon Kelley | d5052fb | 2013-04-25 12:44:20 +0100 | [diff] [blame] | 29 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 30 | /* data structure size in here is fixed */ |
| 31 | #define BUFF_SZ 256 |
| 32 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 33 | static const struct sockaddr_nl snl = { .nl_family = AF_NETLINK }; |
| 34 | static int ipset_sock, old_kernel; |
| 35 | static char *buffer; |
| 36 | |
| 37 | static inline void add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data) |
| 38 | { |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 39 | struct nlattr *attr = (void *)nlh + NLA_ALIGN(nlh->nlmsg_len); |
| 40 | uint16_t payload_len = NLA_ALIGN(sizeof(struct nlattr)) + len; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 41 | attr->nla_type = type; |
| 42 | attr->nla_len = payload_len; |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 43 | memcpy((void *)attr + NLA_ALIGN(sizeof(struct nlattr)), data, len); |
| 44 | nlh->nlmsg_len += NLA_ALIGN(payload_len); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void ipset_init(void) |
| 48 | { |
Simon Kelley | 0506a5e | 2020-03-19 21:56:45 +0000 | [diff] [blame] | 49 | old_kernel = (daemon->kernel_version < KERNEL_VERSION(2,6,32)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 50 | |
| 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 63 | 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] | 64 | { |
| 65 | struct nlmsghdr *nlh; |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 66 | struct nfgenmsg *nfg; |
| 67 | struct nlattr *nested[2]; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 68 | uint8_t proto; |
Kevin Darbyshire-Bryant | 3becf46 | 2018-12-12 12:00:19 +0000 | [diff] [blame] | 69 | int addrsz = (af == AF_INET6) ? IN6ADDRSZ : INADDRSZ; |
Simon Kelley | ab6ede7 | 2013-02-23 19:22:37 +0000 | [diff] [blame] | 70 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 71 | if (strlen(setname) >= IPSET_MAXNAMELEN) |
| 72 | { |
| 73 | errno = ENAMETOOLONG; |
| 74 | return -1; |
| 75 | } |
| 76 | |
Dave Reisner | 4582c0e | 2013-04-18 09:47:49 +0100 | [diff] [blame] | 77 | memset(buffer, 0, BUFF_SZ); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 78 | |
| 79 | nlh = (struct nlmsghdr *)buffer; |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 80 | nlh->nlmsg_len = NLA_ALIGN(sizeof(struct nlmsghdr)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 81 | nlh->nlmsg_type = (remove ? IPSET_CMD_DEL : IPSET_CMD_ADD) | (NFNL_SUBSYS_IPSET << 8); |
| 82 | nlh->nlmsg_flags = NLM_F_REQUEST; |
| 83 | |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 84 | nfg = (struct nfgenmsg *)(buffer + nlh->nlmsg_len); |
| 85 | nlh->nlmsg_len += NLA_ALIGN(sizeof(struct nfgenmsg)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 86 | 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.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 93 | nested[0] = (struct nlattr *)(buffer + NLA_ALIGN(nlh->nlmsg_len)); |
| 94 | nlh->nlmsg_len += NLA_ALIGN(sizeof(struct nlattr)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 95 | nested[0]->nla_type = NLA_F_NESTED | IPSET_ATTR_DATA; |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 96 | nested[1] = (struct nlattr *)(buffer + NLA_ALIGN(nlh->nlmsg_len)); |
| 97 | nlh->nlmsg_len += NLA_ALIGN(sizeof(struct nlattr)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 98 | 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 101 | addrsz, ipaddr); |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame^] | 102 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 104 | |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 105 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 112 | 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] | 113 | { |
| 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 144 | req_adt.ip = ntohl(ipaddr->addr4.s_addr); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 145 | 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 153 | 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] | 154 | { |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 155 | int ret = 0, af = AF_INET; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 156 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 157 | if (flags & F_IPV6) |
| 158 | { |
| 159 | af = AF_INET6; |
| 160 | /* old method only supports IPv4 */ |
| 161 | if (old_kernel) |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 162 | { |
| 163 | errno = EAFNOSUPPORT ; |
| 164 | ret = -1; |
| 165 | } |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 166 | } |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 167 | |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 168 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | #endif |