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 33 MODULE_LICENSE("GPL"); 34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>"); 35 IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); 36 MODULE_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 */ 52 struct 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 63 static bool 64 hash_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 75 static int 76 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem *elem) 77 { 78 return elem->nomatch ? -ENOTEMPTY : 1; 79 } 80 81 static void 82 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem *elem, u32 flags) 83 { 84 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); 85 } 86 87 static void 88 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem *elem, u8 *flags) 89 { 90 swap(*flags, elem->nomatch); 91 } 92 93 static void 94 hash_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 100 static bool 101 hash_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 116 nla_put_failure: 117 return true; 118 } 119 120 static void 121 hash_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 133 static int 134 hash_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 159 static int 160 hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[], 161 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) 162 { 163 const 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; 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 e.ip2 = htonl(ip2); 283 ip2 = ip_set_range_to_cidr(ip2, ip2_to, &cidr); 284 e.cidr = cidr - 1; 285 ret = adtfn(set, &e, &ext, &ext, flags); 286 287 if (ret && !ip_set_eexist(ret, flags)) 288 return ret; 289 290 ret = 0; 291 } while (ip2++ < ip2_to); 292 ip2 = ip2_from; 293 } 294 p = port; 295 } 296 return ret; 297 } 298 299 /* IPv6 variant */ 300 301 struct hash_ipportnet6_elem { 302 union nf_inet_addr ip; 303 union nf_inet_addr ip2; 304 __be16 port; 305 u8 cidr:7; 306 u8 nomatch:1; 307 u8 proto; 308 }; 309 310 /* Common functions */ 311 312 static bool 313 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1, 314 const struct hash_ipportnet6_elem *ip2, 315 u32 *multi) 316 { 317 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) && 318 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) && 319 ip1->cidr == ip2->cidr && 320 ip1->port == ip2->port && 321 ip1->proto == ip2->proto; 322 } 323 324 static int 325 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem) 326 { 327 return elem->nomatch ? -ENOTEMPTY : 1; 328 } 329 330 static void 331 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem *elem, u32 flags) 332 { 333 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); 334 } 335 336 static void 337 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags) 338 { 339 swap(*flags, elem->nomatch); 340 } 341 342 static void 343 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr) 344 { 345 ip6_netmask(&elem->ip2, cidr); 346 elem->cidr = cidr - 1; 347 } 348 349 static bool 350 hash_ipportnet6_data_list(struct sk_buff *skb, 351 const struct hash_ipportnet6_elem *data) 352 { 353 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; 354 355 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) || 356 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) || 357 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || 358 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) || 359 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || 360 (flags && 361 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) 362 goto nla_put_failure; 363 return false; 364 365 nla_put_failure: 366 return true; 367 } 368 369 static void 370 hash_ipportnet6_data_next(struct hash_ipportnet6_elem *next, 371 const struct hash_ipportnet6_elem *d) 372 { 373 next->port = d->port; 374 } 375 376 #undef MTYPE 377 #undef HOST_MASK 378 379 #define MTYPE hash_ipportnet6 380 #define HOST_MASK 128 381 #define IP_SET_EMIT_CREATE 382 #include "ip_set_hash_gen.h" 383 384 static int 385 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb, 386 const struct xt_action_param *par, 387 enum ipset_adt adt, struct ip_set_adt_opt *opt) 388 { 389 const struct hash_ipportnet6 *h = set->data; 390 ipset_adtfn adtfn = set->variant->adt[adt]; 391 struct hash_ipportnet6_elem e = { 392 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK), 393 }; 394 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); 395 396 if (adt == IPSET_TEST) 397 e.cidr = HOST_MASK - 1; 398 399 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, 400 &e.port, &e.proto)) 401 return -EINVAL; 402 403 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6); 404 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6); 405 ip6_netmask(&e.ip2, e.cidr + 1); 406 407 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); 408 } 409 410 static int 411 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[], 412 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) 413 { 414 const struct hash_ipportnet6 *h = set->data; 415 ipset_adtfn adtfn = set->variant->adt[adt]; 416 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 }; 417 struct ip_set_ext ext = IP_SET_INIT_UEXT(set); 418 u32 port, port_to; 419 bool with_ports = false; 420 u8 cidr; 421 int ret; 422 423 if (tb[IPSET_ATTR_LINENO]) 424 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); 425 426 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || 427 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || 428 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || 429 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) 430 return -IPSET_ERR_PROTOCOL; 431 if (unlikely(tb[IPSET_ATTR_IP_TO])) 432 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; 433 if (unlikely(tb[IPSET_ATTR_CIDR])) { 434 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); 435 436 if (cidr != HOST_MASK) 437 return -IPSET_ERR_INVALID_CIDR; 438 } 439 440 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip); 441 if (ret) 442 return ret; 443 444 ret = ip_set_get_extensions(set, tb, &ext); 445 if (ret) 446 return ret; 447 448 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2); 449 if (ret) 450 return ret; 451 452 if (tb[IPSET_ATTR_CIDR2]) { 453 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]); 454 if (!cidr || cidr > HOST_MASK) 455 return -IPSET_ERR_INVALID_CIDR; 456 e.cidr = cidr - 1; 457 } 458 459 ip6_netmask(&e.ip2, e.cidr + 1); 460 461 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); 462 463 if (tb[IPSET_ATTR_PROTO]) { 464 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); 465 with_ports = ip_set_proto_with_ports(e.proto); 466 467 if (e.proto == 0) 468 return -IPSET_ERR_INVALID_PROTO; 469 } else { 470 return -IPSET_ERR_MISSING_PROTO; 471 } 472 473 if (!(with_ports || e.proto == IPPROTO_ICMPV6)) 474 e.port = 0; 475 476 if (tb[IPSET_ATTR_CADT_FLAGS]) { 477 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 478 479 if (cadt_flags & IPSET_FLAG_NOMATCH) 480 flags |= (IPSET_FLAG_NOMATCH << 16); 481 } 482 483 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) { 484 ret = adtfn(set, &e, &ext, &ext, flags); 485 return ip_set_enomatch(ret, flags, adt, set) ? -ret : 486 ip_set_eexist(ret, flags) ? 0 : ret; 487 } 488 489 port = ntohs(e.port); 490 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); 491 if (port > port_to) 492 swap(port, port_to); 493 494 if (retried) 495 port = ntohs(h->next.port); 496 for (; port <= port_to; port++) { 497 e.port = htons(port); 498 ret = adtfn(set, &e, &ext, &ext, flags); 499 500 if (ret && !ip_set_eexist(ret, flags)) 501 return ret; 502 503 ret = 0; 504 } 505 return ret; 506 } 507 508 static struct ip_set_type hash_ipportnet_type __read_mostly = { 509 .name = "hash:ip,port,net", 510 .protocol = IPSET_PROTOCOL, 511 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 | 512 IPSET_TYPE_NOMATCH, 513 .dimension = IPSET_DIM_THREE, 514 .family = NFPROTO_UNSPEC, 515 .revision_min = IPSET_TYPE_REV_MIN, 516 .revision_max = IPSET_TYPE_REV_MAX, 517 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE, 518 .create = hash_ipportnet_create, 519 .create_policy = { 520 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 521 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, 522 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 }, 523 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 }, 524 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, 525 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, 526 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 527 }, 528 .adt_policy = { 529 [IPSET_ATTR_IP] = { .type = NLA_NESTED }, 530 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, 531 [IPSET_ATTR_IP2] = { .type = NLA_NESTED }, 532 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED }, 533 [IPSET_ATTR_PORT] = { .type = NLA_U16 }, 534 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 }, 535 [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, 536 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 }, 537 [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, 538 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 539 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, 540 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 541 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 542 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 543 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING, 544 .len = IPSET_MAX_COMMENT_SIZE }, 545 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, 546 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, 547 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, 548 }, 549 .me = THIS_MODULE, 550 }; 551 552 static int __init 553 hash_ipportnet_init(void) 554 { 555 return ip_set_type_register(&hash_ipportnet_type); 556 } 557 558 static void __exit 559 hash_ipportnet_fini(void) 560 { 561 rcu_barrier(); 562 ip_set_type_unregister(&hash_ipportnet_type); 563 } 564 565 module_init(hash_ipportnet_init); 566 module_exit(hash_ipportnet_fini); 567