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