1// SPDX-License-Identifier: GPL-2.0-only 2/* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> 3 * Copyright (C) 2013 Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa> 4 */ 5 6/* Kernel module implementing an IP set type: the hash:net type */ 7 8#include <linux/jhash.h> 9#include <linux/module.h> 10#include <linux/ip.h> 11#include <linux/skbuff.h> 12#include <linux/errno.h> 13#include <linux/random.h> 14#include <net/ip.h> 15#include <net/ipv6.h> 16#include <net/netlink.h> 17 18#include <linux/netfilter.h> 19#include <linux/netfilter/ipset/pfxlen.h> 20#include <linux/netfilter/ipset/ip_set.h> 21#include <linux/netfilter/ipset/ip_set_hash.h> 22 23#define IPSET_TYPE_REV_MIN 0 24/* 1 Forceadd support added */ 25/* 2 skbinfo support added */ 26/* 3 bucketsize, initval support added */ 27#define IPSET_TYPE_REV_MAX 4 /* bitmask support added */ 28 29MODULE_LICENSE("GPL"); 30MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>"); 31IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); 32MODULE_ALIAS("ip_set_hash:net,net"); 33 34/* Type specific function prefix */ 35#define HTYPE hash_netnet 36#define IP_SET_HASH_WITH_NETS 37#define IP_SET_HASH_WITH_NETMASK 38#define IP_SET_HASH_WITH_BITMASK 39#define IPSET_NET_COUNT 2 40 41/* IPv4 variants */ 42 43/* Member elements */ 44struct hash_netnet4_elem { 45 union { 46 __be32 ip[2]; 47 __be64 ipcmp; 48 }; 49 u8 nomatch; 50 u8 padding; 51 union { 52 u8 cidr[2]; 53 u16 ccmp; 54 }; 55}; 56 57/* Common functions */ 58 59static bool 60hash_netnet4_data_equal(const struct hash_netnet4_elem *ip1, 61 const struct hash_netnet4_elem *ip2, 62 u32 *multi) 63{ 64 return ip1->ipcmp == ip2->ipcmp && 65 ip1->ccmp == ip2->ccmp; 66} 67 68static int 69hash_netnet4_do_data_match(const struct hash_netnet4_elem *elem) 70{ 71 return elem->nomatch ? -ENOTEMPTY : 1; 72} 73 74static void 75hash_netnet4_data_set_flags(struct hash_netnet4_elem *elem, u32 flags) 76{ 77 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH; 78} 79 80static void 81hash_netnet4_data_reset_flags(struct hash_netnet4_elem *elem, u8 *flags) 82{ 83 swap(*flags, elem->nomatch); 84} 85 86static void 87hash_netnet4_data_reset_elem(struct hash_netnet4_elem *elem, 88 struct hash_netnet4_elem *orig) 89{ 90 elem->ip[1] = orig->ip[1]; 91} 92 93static void 94hash_netnet4_data_netmask(struct hash_netnet4_elem *elem, u8 cidr, bool inner) 95{ 96 if (inner) { 97 elem->ip[1] &= ip_set_netmask(cidr); 98 elem->cidr[1] = cidr; 99 } else { 100 elem->ip[0] &= ip_set_netmask(cidr); 101 elem->cidr[0] = cidr; 102 } 103} 104 105static bool 106hash_netnet4_data_list(struct sk_buff *skb, 107 const struct hash_netnet4_elem *data) 108{ 109 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; 110 111 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) || 112 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) || 113 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) || 114 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) || 115 (flags && 116 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) 117 goto nla_put_failure; 118 return false; 119 120nla_put_failure: 121 return true; 122} 123 124static void 125hash_netnet4_data_next(struct hash_netnet4_elem *next, 126 const struct hash_netnet4_elem *d) 127{ 128 next->ipcmp = d->ipcmp; 129} 130 131#define MTYPE hash_netnet4 132#define HOST_MASK 32 133#include "ip_set_hash_gen.h" 134 135static void 136hash_netnet4_init(struct hash_netnet4_elem *e) 137{ 138 e->cidr[0] = HOST_MASK; 139 e->cidr[1] = HOST_MASK; 140} 141 142static int 143hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb, 144 const struct xt_action_param *par, 145 enum ipset_adt adt, struct ip_set_adt_opt *opt) 146{ 147 const struct hash_netnet4 *h = set->data; 148 ipset_adtfn adtfn = set->variant->adt[adt]; 149 struct hash_netnet4_elem e = { }; 150 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); 151 152 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK); 153 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK); 154 if (adt == IPSET_TEST) 155 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK; 156 157 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]); 158 ip4addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1]); 159 e.ip[0] &= (ip_set_netmask(e.cidr[0]) & h->bitmask.ip); 160 e.ip[1] &= (ip_set_netmask(e.cidr[1]) & h->bitmask.ip); 161 162 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); 163} 164 165static int 166hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[], 167 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) 168{ 169 struct hash_netnet4 *h = set->data; 170 ipset_adtfn adtfn = set->variant->adt[adt]; 171 struct hash_netnet4_elem e = { }; 172 struct ip_set_ext ext = IP_SET_INIT_UEXT(set); 173 u32 ip = 0, ip_to = 0; 174 u32 ip2 = 0, ip2_from = 0, ip2_to = 0, i = 0; 175 int ret; 176 177 if (tb[IPSET_ATTR_LINENO]) 178 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); 179 180 hash_netnet4_init(&e); 181 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || 182 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) 183 return -IPSET_ERR_PROTOCOL; 184 185 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip); 186 if (ret) 187 return ret; 188 189 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from); 190 if (ret) 191 return ret; 192 193 ret = ip_set_get_extensions(set, tb, &ext); 194 if (ret) 195 return ret; 196 197 if (tb[IPSET_ATTR_CIDR]) { 198 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]); 199 if (!e.cidr[0] || e.cidr[0] > HOST_MASK) 200 return -IPSET_ERR_INVALID_CIDR; 201 } 202 203 if (tb[IPSET_ATTR_CIDR2]) { 204 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]); 205 if (!e.cidr[1] || e.cidr[1] > HOST_MASK) 206 return -IPSET_ERR_INVALID_CIDR; 207 } 208 209 if (tb[IPSET_ATTR_CADT_FLAGS]) { 210 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 211 212 if (cadt_flags & IPSET_FLAG_NOMATCH) 213 flags |= (IPSET_FLAG_NOMATCH << 16); 214 } 215 216 if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] || 217 tb[IPSET_ATTR_IP2_TO])) { 218 e.ip[0] = htonl(ip & ntohl(h->bitmask.ip) & ip_set_hostmask(e.cidr[0])); 219 e.ip[1] = htonl(ip2_from & ntohl(h->bitmask.ip) & ip_set_hostmask(e.cidr[1])); 220 ret = adtfn(set, &e, &ext, &ext, flags); 221 return ip_set_enomatch(ret, flags, adt, set) ? -ret : 222 ip_set_eexist(ret, flags) ? 0 : ret; 223 } 224 225 ip_to = ip; 226 if (tb[IPSET_ATTR_IP_TO]) { 227 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); 228 if (ret) 229 return ret; 230 if (ip_to < ip) 231 swap(ip, ip_to); 232 if (unlikely(ip + UINT_MAX == ip_to)) 233 return -IPSET_ERR_HASH_RANGE; 234 } else { 235 ip_set_mask_from_to(ip, ip_to, e.cidr[0]); 236 } 237 238 ip2_to = ip2_from; 239 if (tb[IPSET_ATTR_IP2_TO]) { 240 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to); 241 if (ret) 242 return ret; 243 if (ip2_to < ip2_from) 244 swap(ip2_from, ip2_to); 245 if (unlikely(ip2_from + UINT_MAX == ip2_to)) 246 return -IPSET_ERR_HASH_RANGE; 247 } else { 248 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]); 249 } 250 251 if (retried) { 252 ip = ntohl(h->next.ip[0]); 253 ip2 = ntohl(h->next.ip[1]); 254 } else { 255 ip2 = ip2_from; 256 } 257 258 do { 259 e.ip[0] = htonl(ip); 260 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]); 261 do { 262 i++; 263 e.ip[1] = htonl(ip2); 264 if (i > IPSET_MAX_RANGE) { 265 hash_netnet4_data_next(&h->next, &e); 266 return -ERANGE; 267 } 268 ip2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]); 269 ret = adtfn(set, &e, &ext, &ext, flags); 270 if (ret && !ip_set_eexist(ret, flags)) 271 return ret; 272 273 ret = 0; 274 } while (ip2++ < ip2_to); 275 ip2 = ip2_from; 276 } while (ip++ < ip_to); 277 return ret; 278} 279 280/* IPv6 variants */ 281 282struct hash_netnet6_elem { 283 union nf_inet_addr ip[2]; 284 u8 nomatch; 285 u8 padding; 286 union { 287 u8 cidr[2]; 288 u16 ccmp; 289 }; 290}; 291 292/* Common functions */ 293 294static bool 295hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1, 296 const struct hash_netnet6_elem *ip2, 297 u32 *multi) 298{ 299 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) && 300 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) && 301 ip1->ccmp == ip2->ccmp; 302} 303 304static int 305hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem) 306{ 307 return elem->nomatch ? -ENOTEMPTY : 1; 308} 309 310static void 311hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags) 312{ 313 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH; 314} 315 316static void 317hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags) 318{ 319 swap(*flags, elem->nomatch); 320} 321 322static void 323hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem, 324 struct hash_netnet6_elem *orig) 325{ 326 elem->ip[1] = orig->ip[1]; 327} 328 329static void 330hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner) 331{ 332 if (inner) { 333 ip6_netmask(&elem->ip[1], cidr); 334 elem->cidr[1] = cidr; 335 } else { 336 ip6_netmask(&elem->ip[0], cidr); 337 elem->cidr[0] = cidr; 338 } 339} 340 341static bool 342hash_netnet6_data_list(struct sk_buff *skb, 343 const struct hash_netnet6_elem *data) 344{ 345 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; 346 347 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) || 348 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) || 349 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) || 350 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) || 351 (flags && 352 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) 353 goto nla_put_failure; 354 return false; 355 356nla_put_failure: 357 return true; 358} 359 360static void 361hash_netnet6_data_next(struct hash_netnet6_elem *next, 362 const struct hash_netnet6_elem *d) 363{ 364} 365 366#undef MTYPE 367#undef HOST_MASK 368 369#define MTYPE hash_netnet6 370#define HOST_MASK 128 371#define IP_SET_EMIT_CREATE 372#include "ip_set_hash_gen.h" 373 374static void 375hash_netnet6_init(struct hash_netnet6_elem *e) 376{ 377 e->cidr[0] = HOST_MASK; 378 e->cidr[1] = HOST_MASK; 379} 380 381static int 382hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb, 383 const struct xt_action_param *par, 384 enum ipset_adt adt, struct ip_set_adt_opt *opt) 385{ 386 const struct hash_netnet6 *h = set->data; 387 ipset_adtfn adtfn = set->variant->adt[adt]; 388 struct hash_netnet6_elem e = { }; 389 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); 390 391 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK); 392 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK); 393 if (adt == IPSET_TEST) 394 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK; 395 396 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6); 397 ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6); 398 ip6_netmask(&e.ip[0], e.cidr[0]); 399 ip6_netmask(&e.ip[1], e.cidr[1]); 400 401 nf_inet_addr_mask_inplace(&e.ip[0], &h->bitmask); 402 nf_inet_addr_mask_inplace(&e.ip[1], &h->bitmask); 403 if (e.cidr[0] == HOST_MASK && ipv6_addr_any(&e.ip[0].in6)) 404 return -EINVAL; 405 406 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); 407} 408 409static int 410hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[], 411 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) 412{ 413 ipset_adtfn adtfn = set->variant->adt[adt]; 414 struct hash_netnet6_elem e = { }; 415 struct ip_set_ext ext = IP_SET_INIT_UEXT(set); 416 const struct hash_netnet6 *h = set->data; 417 int ret; 418 419 if (tb[IPSET_ATTR_LINENO]) 420 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); 421 422 hash_netnet6_init(&e); 423 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || 424 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) 425 return -IPSET_ERR_PROTOCOL; 426 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO])) 427 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; 428 429 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]); 430 if (ret) 431 return ret; 432 433 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]); 434 if (ret) 435 return ret; 436 437 ret = ip_set_get_extensions(set, tb, &ext); 438 if (ret) 439 return ret; 440 441 if (tb[IPSET_ATTR_CIDR]) { 442 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]); 443 if (!e.cidr[0] || e.cidr[0] > HOST_MASK) 444 return -IPSET_ERR_INVALID_CIDR; 445 } 446 447 if (tb[IPSET_ATTR_CIDR2]) { 448 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]); 449 if (!e.cidr[1] || e.cidr[1] > HOST_MASK) 450 return -IPSET_ERR_INVALID_CIDR; 451 } 452 453 ip6_netmask(&e.ip[0], e.cidr[0]); 454 ip6_netmask(&e.ip[1], e.cidr[1]); 455 456 nf_inet_addr_mask_inplace(&e.ip[0], &h->bitmask); 457 nf_inet_addr_mask_inplace(&e.ip[1], &h->bitmask); 458 if (e.cidr[0] == HOST_MASK && ipv6_addr_any(&e.ip[0].in6)) 459 return -IPSET_ERR_HASH_ELEM; 460 461 if (tb[IPSET_ATTR_CADT_FLAGS]) { 462 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 463 464 if (cadt_flags & IPSET_FLAG_NOMATCH) 465 flags |= (IPSET_FLAG_NOMATCH << 16); 466 } 467 468 ret = adtfn(set, &e, &ext, &ext, flags); 469 470 return ip_set_enomatch(ret, flags, adt, set) ? -ret : 471 ip_set_eexist(ret, flags) ? 0 : ret; 472} 473 474static struct ip_set_type hash_netnet_type __read_mostly = { 475 .name = "hash:net,net", 476 .protocol = IPSET_PROTOCOL, 477 .features = IPSET_TYPE_IP | IPSET_TYPE_IP2 | IPSET_TYPE_NOMATCH, 478 .dimension = IPSET_DIM_TWO, 479 .family = NFPROTO_UNSPEC, 480 .revision_min = IPSET_TYPE_REV_MIN, 481 .revision_max = IPSET_TYPE_REV_MAX, 482 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE, 483 .create = hash_netnet_create, 484 .create_policy = { 485 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 486 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, 487 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 }, 488 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 }, 489 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, 490 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, 491 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 492 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 }, 493 [IPSET_ATTR_BITMASK] = { .type = NLA_NESTED }, 494 }, 495 .adt_policy = { 496 [IPSET_ATTR_IP] = { .type = NLA_NESTED }, 497 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, 498 [IPSET_ATTR_IP2] = { .type = NLA_NESTED }, 499 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED }, 500 [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, 501 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 }, 502 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, 503 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 504 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 505 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 506 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 507 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING, 508 .len = IPSET_MAX_COMMENT_SIZE }, 509 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, 510 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, 511 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, 512 }, 513 .me = THIS_MODULE, 514}; 515 516static int __init 517hash_netnet_init(void) 518{ 519 return ip_set_type_register(&hash_netnet_type); 520} 521 522static void __exit 523hash_netnet_fini(void) 524{ 525 rcu_barrier(); 526 ip_set_type_unregister(&hash_netnet_type); 527} 528 529module_init(hash_netnet_init); 530module_exit(hash_netnet_fini); 531