1 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> 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 version 2 as 5 * published by the Free Software Foundation. 6 */ 7 8 /* Kernel module implementing an IP set type: the hash:ip,port,net type */ 9 10 #include <linux/jhash.h> 11 #include <linux/module.h> 12 #include <linux/ip.h> 13 #include <linux/skbuff.h> 14 #include <linux/errno.h> 15 #include <linux/random.h> 16 #include <net/ip.h> 17 #include <net/ipv6.h> 18 #include <net/netlink.h> 19 #include <net/tcp.h> 20 21 #include <linux/netfilter.h> 22 #include <linux/netfilter/ipset/pfxlen.h> 23 #include <linux/netfilter/ipset/ip_set.h> 24 #include <linux/netfilter/ipset/ip_set_getport.h> 25 #include <linux/netfilter/ipset/ip_set_hash.h> 26 27 #define IPSET_TYPE_REV_MIN 0 28 /* 1 SCTP and UDPLITE support added */ 29 /* 2 Range as input support for IPv4 added */ 30 /* 3 nomatch flag support added */ 31 /* 4 Counters support added */ 32 /* 5 Comments support added */ 33 /* 6 Forceadd support added */ 34 #define IPSET_TYPE_REV_MAX 7 /* skbinfo support added */ 35 36 MODULE_LICENSE("GPL"); 37 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 38 IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); 39 MODULE_ALIAS("ip_set_hash:ip,port,net"); 40 41 /* Type specific function prefix */ 42 #define HTYPE hash_ipportnet 43 44 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0 45 * However this way we have to store internally cidr - 1, 46 * dancing back and forth. 47 */ 48 #define IP_SET_HASH_WITH_NETS_PACKED 49 #define IP_SET_HASH_WITH_PROTO 50 #define IP_SET_HASH_WITH_NETS 51 52 /* IPv4 variant */ 53 54 /* Member elements */ 55 struct hash_ipportnet4_elem { 56 __be32 ip; 57 __be32 ip2; 58 __be16 port; 59 u8 cidr:7; 60 u8 nomatch:1; 61 u8 proto; 62 }; 63 64 /* Common functions */ 65 66 static inline bool 67 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem *ip1, 68 const struct hash_ipportnet4_elem *ip2, 69 u32 *multi) 70 { 71 return ip1->ip == ip2->ip && 72 ip1->ip2 == ip2->ip2 && 73 ip1->cidr == ip2->cidr && 74 ip1->port == ip2->port && 75 ip1->proto == ip2->proto; 76 } 77 78 static inline int 79 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem *elem) 80 { 81 return elem->nomatch ? -ENOTEMPTY : 1; 82 } 83 84 static inline void 85 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem *elem, u32 flags) 86 { 87 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); 88 } 89 90 static inline void 91 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem *elem, u8 *flags) 92 { 93 swap(*flags, elem->nomatch); 94 } 95 96 static inline void 97 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem *elem, u8 cidr) 98 { 99 elem->ip2 &= ip_set_netmask(cidr); 100 elem->cidr = cidr - 1; 101 } 102 103 static bool 104 hash_ipportnet4_data_list(struct sk_buff *skb, 105 const struct hash_ipportnet4_elem *data) 106 { 107 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; 108 109 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) || 110 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) || 111 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || 112 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) || 113 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || 114 (flags && 115 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) 116 goto nla_put_failure; 117 return false; 118 119 nla_put_failure: 120 return true; 121 } 122 123 static inline void 124 hash_ipportnet4_data_next(struct hash_ipportnet4_elem *next, 125 const struct hash_ipportnet4_elem *d) 126 { 127 next->ip = d->ip; 128 next->port = d->port; 129 next->ip2 = d->ip2; 130 } 131 132 #define MTYPE hash_ipportnet4 133 #define HOST_MASK 32 134 #include "ip_set_hash_gen.h" 135 136 static int 137 hash_ipportnet4_kadt(struct ip_set *set, const struct sk_buff *skb, 138 const struct xt_action_param *par, 139 enum ipset_adt adt, struct ip_set_adt_opt *opt) 140 { 141 const struct hash_ipportnet *h = set->data; 142 ipset_adtfn adtfn = set->variant->adt[adt]; 143 struct hash_ipportnet4_elem e = { 144 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1, 145 }; 146 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); 147 148 if (adt == IPSET_TEST) 149 e.cidr = HOST_MASK - 1; 150 151 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, 152 &e.port, &e.proto)) 153 return -EINVAL; 154 155 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip); 156 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2); 157 e.ip2 &= ip_set_netmask(e.cidr + 1); 158 159 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); 160 } 161 162 static int 163 hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[], 164 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) 165 { 166 const struct hash_ipportnet *h = set->data; 167 ipset_adtfn adtfn = set->variant->adt[adt]; 168 struct hash_ipportnet4_elem e = { .cidr = HOST_MASK - 1 }; 169 struct ip_set_ext ext = IP_SET_INIT_UEXT(set); 170 u32 ip = 0, ip_to = 0, p = 0, port, port_to; 171 u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2; 172 bool with_ports = false; 173 u8 cidr; 174 int ret; 175 176 if (tb[IPSET_ATTR_LINENO]) 177 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); 178 179 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || 180 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || 181 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || 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_extensions(set, tb, &ext); 190 if (ret) 191 return ret; 192 193 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from); 194 if (ret) 195 return ret; 196 197 if (tb[IPSET_ATTR_CIDR2]) { 198 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]); 199 if (!cidr || cidr > HOST_MASK) 200 return -IPSET_ERR_INVALID_CIDR; 201 e.cidr = cidr - 1; 202 } 203 204 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); 205 206 if (tb[IPSET_ATTR_PROTO]) { 207 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); 208 with_ports = ip_set_proto_with_ports(e.proto); 209 210 if (e.proto == 0) 211 return -IPSET_ERR_INVALID_PROTO; 212 } else 213 return -IPSET_ERR_MISSING_PROTO; 214 215 if (!(with_ports || e.proto == IPPROTO_ICMP)) 216 e.port = 0; 217 218 if (tb[IPSET_ATTR_CADT_FLAGS]) { 219 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 220 if (cadt_flags & IPSET_FLAG_NOMATCH) 221 flags |= (IPSET_FLAG_NOMATCH << 16); 222 } 223 224 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO]; 225 if (adt == IPSET_TEST || 226 !(tb[IPSET_ATTR_CIDR] || tb[IPSET_ATTR_IP_TO] || with_ports || 227 tb[IPSET_ATTR_IP2_TO])) { 228 e.ip = htonl(ip); 229 e.ip2 = htonl(ip2_from & ip_set_hostmask(e.cidr + 1)); 230 ret = adtfn(set, &e, &ext, &ext, flags); 231 return ip_set_enomatch(ret, flags, adt, set) ? -ret : 232 ip_set_eexist(ret, flags) ? 0 : ret; 233 } 234 235 ip_to = ip; 236 if (tb[IPSET_ATTR_IP_TO]) { 237 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); 238 if (ret) 239 return ret; 240 if (ip > ip_to) 241 swap(ip, ip_to); 242 } else if (tb[IPSET_ATTR_CIDR]) { 243 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); 244 245 if (!cidr || cidr > HOST_MASK) 246 return -IPSET_ERR_INVALID_CIDR; 247 ip_set_mask_from_to(ip, ip_to, cidr); 248 } 249 250 port_to = port = ntohs(e.port); 251 if (tb[IPSET_ATTR_PORT_TO]) { 252 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); 253 if (port > port_to) 254 swap(port, port_to); 255 } 256 257 ip2_to = ip2_from; 258 if (tb[IPSET_ATTR_IP2_TO]) { 259 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to); 260 if (ret) 261 return ret; 262 if (ip2_from > ip2_to) 263 swap(ip2_from, ip2_to); 264 if (ip2_from + UINT_MAX == ip2_to) 265 return -IPSET_ERR_HASH_RANGE; 266 } else 267 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr + 1); 268 269 if (retried) 270 ip = ntohl(h->next.ip); 271 for (; !before(ip_to, ip); ip++) { 272 e.ip = htonl(ip); 273 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port) 274 : port; 275 for (; p <= port_to; p++) { 276 e.port = htons(p); 277 ip2 = retried && 278 ip == ntohl(h->next.ip) && 279 p == ntohs(h->next.port) 280 ? ntohl(h->next.ip2) : ip2_from; 281 while (!after(ip2, ip2_to)) { 282 e.ip2 = htonl(ip2); 283 ip2_last = ip_set_range_to_cidr(ip2, ip2_to, 284 &cidr); 285 e.cidr = cidr - 1; 286 ret = adtfn(set, &e, &ext, &ext, flags); 287 288 if (ret && !ip_set_eexist(ret, flags)) 289 return ret; 290 else 291 ret = 0; 292 ip2 = ip2_last + 1; 293 } 294 } 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 inline 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 inline int 325 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem) 326 { 327 return elem->nomatch ? -ENOTEMPTY : 1; 328 } 329 330 static inline 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 inline void 337 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags) 338 { 339 swap(*flags, elem->nomatch); 340 } 341 342 static inline 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 inline void 370 hash_ipportnet6_data_next(struct hash_ipportnet4_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_ipportnet *h = set->data; 390 ipset_adtfn adtfn = set->variant->adt[adt]; 391 struct hash_ipportnet6_elem e = { 392 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1, 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_ipportnet *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 u8 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 if (!(with_ports || e.proto == IPPROTO_ICMPV6)) 473 e.port = 0; 474 475 if (tb[IPSET_ATTR_CADT_FLAGS]) { 476 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 477 if (cadt_flags & IPSET_FLAG_NOMATCH) 478 flags |= (IPSET_FLAG_NOMATCH << 16); 479 } 480 481 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) { 482 ret = adtfn(set, &e, &ext, &ext, flags); 483 return ip_set_enomatch(ret, flags, adt, set) ? -ret : 484 ip_set_eexist(ret, flags) ? 0 : ret; 485 } 486 487 port = ntohs(e.port); 488 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); 489 if (port > port_to) 490 swap(port, port_to); 491 492 if (retried) 493 port = ntohs(h->next.port); 494 for (; port <= port_to; port++) { 495 e.port = htons(port); 496 ret = adtfn(set, &e, &ext, &ext, flags); 497 498 if (ret && !ip_set_eexist(ret, flags)) 499 return ret; 500 else 501 ret = 0; 502 } 503 return ret; 504 } 505 506 static struct ip_set_type hash_ipportnet_type __read_mostly = { 507 .name = "hash:ip,port,net", 508 .protocol = IPSET_PROTOCOL, 509 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 | 510 IPSET_TYPE_NOMATCH, 511 .dimension = IPSET_DIM_THREE, 512 .family = NFPROTO_UNSPEC, 513 .revision_min = IPSET_TYPE_REV_MIN, 514 .revision_max = IPSET_TYPE_REV_MAX, 515 .create = hash_ipportnet_create, 516 .create_policy = { 517 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 518 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, 519 [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, 520 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, 521 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, 522 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 523 }, 524 .adt_policy = { 525 [IPSET_ATTR_IP] = { .type = NLA_NESTED }, 526 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, 527 [IPSET_ATTR_IP2] = { .type = NLA_NESTED }, 528 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED }, 529 [IPSET_ATTR_PORT] = { .type = NLA_U16 }, 530 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 }, 531 [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, 532 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 }, 533 [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, 534 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 535 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, 536 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 537 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 538 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 539 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING, 540 .len = IPSET_MAX_COMMENT_SIZE }, 541 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, 542 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, 543 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, 544 }, 545 .me = THIS_MODULE, 546 }; 547 548 static int __init 549 hash_ipportnet_init(void) 550 { 551 return ip_set_type_register(&hash_ipportnet_type); 552 } 553 554 static void __exit 555 hash_ipportnet_fini(void) 556 { 557 ip_set_type_unregister(&hash_ipportnet_type); 558 } 559 560 module_init(hash_ipportnet_init); 561 module_exit(hash_ipportnet_fini); 562