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 | |
tarun.kundu | bd26504 | 2023-04-13 13:53:44 -0700 | [diff] [blame^] | 37 | static inline struct nlattr *add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data) |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 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 | bd26504 | 2023-04-13 13:53:44 -0700 | [diff] [blame^] | 43 | if (len > 0) { |
| 44 | memcpy((void *)attr + NLA_ALIGN(sizeof(struct nlattr)), data, len); |
| 45 | } |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame] | 46 | nlh->nlmsg_len += NLA_ALIGN(payload_len); |
tarun.kundu | bd26504 | 2023-04-13 13:53:44 -0700 | [diff] [blame^] | 47 | return attr; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void ipset_init(void) |
| 51 | { |
Simon Kelley | 0506a5e | 2020-03-19 21:56:45 +0000 | [diff] [blame] | 52 | old_kernel = (daemon->kernel_version < KERNEL_VERSION(2,6,32)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 53 | |
| 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.kundu | bd26504 | 2023-04-13 13:53:44 -0700 | [diff] [blame^] | 66 | /* |
| 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 87 | 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] | 88 | { |
| 89 | struct nlmsghdr *nlh; |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame] | 90 | struct nfgenmsg *nfg; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 91 | uint8_t proto; |
Kevin Darbyshire-Bryant | 3becf46 | 2018-12-12 12:00:19 +0000 | [diff] [blame] | 92 | int addrsz = (af == AF_INET6) ? IN6ADDRSZ : INADDRSZ; |
tarun.kundu | bd26504 | 2023-04-13 13:53:44 -0700 | [diff] [blame^] | 93 | 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 Kelley | ab6ede7 | 2013-02-23 19:22:37 +0000 | [diff] [blame] | 96 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 97 | if (strlen(setname) >= IPSET_MAXNAMELEN) |
| 98 | { |
| 99 | errno = ENAMETOOLONG; |
| 100 | return -1; |
| 101 | } |
| 102 | |
Dave Reisner | 4582c0e | 2013-04-18 09:47:49 +0100 | [diff] [blame] | 103 | memset(buffer, 0, BUFF_SZ); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 104 | |
| 105 | nlh = (struct nlmsghdr *)buffer; |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame] | 106 | nlh->nlmsg_len = NLA_ALIGN(sizeof(struct nlmsghdr)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 107 | nlh->nlmsg_type = (remove ? IPSET_CMD_DEL : IPSET_CMD_ADD) | (NFNL_SUBSYS_IPSET << 8); |
| 108 | nlh->nlmsg_flags = NLM_F_REQUEST; |
| 109 | |
tarun.kundu | a4393a7 | 2023-04-13 13:08:22 -0700 | [diff] [blame] | 110 | nfg = (struct nfgenmsg *)(buffer + nlh->nlmsg_len); |
| 111 | nlh->nlmsg_len += NLA_ALIGN(sizeof(struct nfgenmsg)); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 112 | 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.kundu | bd26504 | 2023-04-13 13:53:44 -0700 | [diff] [blame^] | 119 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 123 | (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] | 124 | addrsz, ipaddr); |
tarun.kundu | bd26504 | 2023-04-13 13:53:44 -0700 | [diff] [blame^] | 125 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 127 | |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 128 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 135 | 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] | 136 | { |
| 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 167 | req_adt.ip = ntohl(ipaddr->addr4.s_addr); |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 168 | 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 Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 176 | 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] | 177 | { |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 178 | int ret = 0, af = AF_INET; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 179 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 180 | if (flags & F_IPV6) |
| 181 | { |
| 182 | af = AF_INET6; |
| 183 | /* old method only supports IPv4 */ |
| 184 | if (old_kernel) |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 185 | { |
| 186 | errno = EAFNOSUPPORT ; |
| 187 | ret = -1; |
| 188 | } |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 189 | } |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 190 | |
Simon Kelley | b5e33ae | 2016-08-28 21:26:42 +0100 | [diff] [blame] | 191 | 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. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | #endif |