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