1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu> 3 * Patrick Schaaf <bof@bof.de> 4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> 5 */ 6 7 /* Kernel module for IP set management */ 8 9 #include <linux/init.h> 10 #include <linux/module.h> 11 #include <linux/moduleparam.h> 12 #include <linux/ip.h> 13 #include <linux/skbuff.h> 14 #include <linux/spinlock.h> 15 #include <linux/rculist.h> 16 #include <net/netlink.h> 17 #include <net/net_namespace.h> 18 #include <net/netns/generic.h> 19 20 #include <linux/netfilter.h> 21 #include <linux/netfilter/x_tables.h> 22 #include <linux/netfilter/nfnetlink.h> 23 #include <linux/netfilter/ipset/ip_set.h> 24 25 static LIST_HEAD(ip_set_type_list); /* all registered set types */ 26 static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */ 27 static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */ 28 29 struct ip_set_net { 30 struct ip_set * __rcu *ip_set_list; /* all individual sets */ 31 ip_set_id_t ip_set_max; /* max number of sets */ 32 bool is_deleted; /* deleted by ip_set_net_exit */ 33 bool is_destroyed; /* all sets are destroyed */ 34 }; 35 36 static unsigned int ip_set_net_id __read_mostly; 37 38 static struct ip_set_net *ip_set_pernet(struct net *net) 39 { 40 return net_generic(net, ip_set_net_id); 41 } 42 43 #define IP_SET_INC 64 44 #define STRNCMP(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0) 45 46 static unsigned int max_sets; 47 48 module_param(max_sets, int, 0600); 49 MODULE_PARM_DESC(max_sets, "maximal number of sets"); 50 MODULE_LICENSE("GPL"); 51 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>"); 52 MODULE_DESCRIPTION("core IP set support"); 53 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET); 54 55 /* When the nfnl mutex or ip_set_ref_lock is held: */ 56 #define ip_set_dereference(inst) \ 57 rcu_dereference_protected((inst)->ip_set_list, \ 58 lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET) || \ 59 lockdep_is_held(&ip_set_ref_lock) || \ 60 (inst)->is_deleted) 61 #define ip_set(inst, id) \ 62 ip_set_dereference(inst)[id] 63 #define ip_set_ref_netlink(inst,id) \ 64 rcu_dereference_raw((inst)->ip_set_list)[id] 65 #define ip_set_dereference_nfnl(p) \ 66 rcu_dereference_check(p, lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET)) 67 68 /* The set types are implemented in modules and registered set types 69 * can be found in ip_set_type_list. Adding/deleting types is 70 * serialized by ip_set_type_mutex. 71 */ 72 73 static void 74 ip_set_type_lock(void) 75 { 76 mutex_lock(&ip_set_type_mutex); 77 } 78 79 static void 80 ip_set_type_unlock(void) 81 { 82 mutex_unlock(&ip_set_type_mutex); 83 } 84 85 /* Register and deregister settype */ 86 87 static struct ip_set_type * 88 find_set_type(const char *name, u8 family, u8 revision) 89 { 90 struct ip_set_type *type; 91 92 list_for_each_entry_rcu(type, &ip_set_type_list, list, 93 lockdep_is_held(&ip_set_type_mutex)) 94 if (STRNCMP(type->name, name) && 95 (type->family == family || 96 type->family == NFPROTO_UNSPEC) && 97 revision >= type->revision_min && 98 revision <= type->revision_max) 99 return type; 100 return NULL; 101 } 102 103 /* Unlock, try to load a set type module and lock again */ 104 static bool 105 load_settype(const char *name) 106 { 107 if (!try_module_get(THIS_MODULE)) 108 return false; 109 110 nfnl_unlock(NFNL_SUBSYS_IPSET); 111 pr_debug("try to load ip_set_%s\n", name); 112 if (request_module("ip_set_%s", name) < 0) { 113 pr_warn("Can't find ip_set type %s\n", name); 114 nfnl_lock(NFNL_SUBSYS_IPSET); 115 module_put(THIS_MODULE); 116 return false; 117 } 118 nfnl_lock(NFNL_SUBSYS_IPSET); 119 module_put(THIS_MODULE); 120 return true; 121 } 122 123 /* Find a set type and reference it */ 124 #define find_set_type_get(name, family, revision, found) \ 125 __find_set_type_get(name, family, revision, found, false) 126 127 static int 128 __find_set_type_get(const char *name, u8 family, u8 revision, 129 struct ip_set_type **found, bool retry) 130 { 131 struct ip_set_type *type; 132 int err; 133 134 if (retry && !load_settype(name)) 135 return -IPSET_ERR_FIND_TYPE; 136 137 rcu_read_lock(); 138 *found = find_set_type(name, family, revision); 139 if (*found) { 140 err = !try_module_get((*found)->me) ? -EFAULT : 0; 141 goto unlock; 142 } 143 /* Make sure the type is already loaded 144 * but we don't support the revision 145 */ 146 list_for_each_entry_rcu(type, &ip_set_type_list, list) 147 if (STRNCMP(type->name, name)) { 148 err = -IPSET_ERR_FIND_TYPE; 149 goto unlock; 150 } 151 rcu_read_unlock(); 152 153 return retry ? -IPSET_ERR_FIND_TYPE : 154 __find_set_type_get(name, family, revision, found, true); 155 156 unlock: 157 rcu_read_unlock(); 158 return err; 159 } 160 161 /* Find a given set type by name and family. 162 * If we succeeded, the supported minimal and maximum revisions are 163 * filled out. 164 */ 165 #define find_set_type_minmax(name, family, min, max) \ 166 __find_set_type_minmax(name, family, min, max, false) 167 168 static int 169 __find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max, 170 bool retry) 171 { 172 struct ip_set_type *type; 173 bool found = false; 174 175 if (retry && !load_settype(name)) 176 return -IPSET_ERR_FIND_TYPE; 177 178 *min = 255; *max = 0; 179 rcu_read_lock(); 180 list_for_each_entry_rcu(type, &ip_set_type_list, list) 181 if (STRNCMP(type->name, name) && 182 (type->family == family || 183 type->family == NFPROTO_UNSPEC)) { 184 found = true; 185 if (type->revision_min < *min) 186 *min = type->revision_min; 187 if (type->revision_max > *max) 188 *max = type->revision_max; 189 } 190 rcu_read_unlock(); 191 if (found) 192 return 0; 193 194 return retry ? -IPSET_ERR_FIND_TYPE : 195 __find_set_type_minmax(name, family, min, max, true); 196 } 197 198 #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \ 199 (f) == NFPROTO_IPV6 ? "inet6" : "any") 200 201 /* Register a set type structure. The type is identified by 202 * the unique triple of name, family and revision. 203 */ 204 int 205 ip_set_type_register(struct ip_set_type *type) 206 { 207 int ret = 0; 208 209 if (type->protocol != IPSET_PROTOCOL) { 210 pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n", 211 type->name, family_name(type->family), 212 type->revision_min, type->revision_max, 213 type->protocol, IPSET_PROTOCOL); 214 return -EINVAL; 215 } 216 217 ip_set_type_lock(); 218 if (find_set_type(type->name, type->family, type->revision_min)) { 219 /* Duplicate! */ 220 pr_warn("ip_set type %s, family %s with revision min %u already registered!\n", 221 type->name, family_name(type->family), 222 type->revision_min); 223 ip_set_type_unlock(); 224 return -EINVAL; 225 } 226 list_add_rcu(&type->list, &ip_set_type_list); 227 pr_debug("type %s, family %s, revision %u:%u registered.\n", 228 type->name, family_name(type->family), 229 type->revision_min, type->revision_max); 230 ip_set_type_unlock(); 231 232 return ret; 233 } 234 EXPORT_SYMBOL_GPL(ip_set_type_register); 235 236 /* Unregister a set type. There's a small race with ip_set_create */ 237 void 238 ip_set_type_unregister(struct ip_set_type *type) 239 { 240 ip_set_type_lock(); 241 if (!find_set_type(type->name, type->family, type->revision_min)) { 242 pr_warn("ip_set type %s, family %s with revision min %u not registered\n", 243 type->name, family_name(type->family), 244 type->revision_min); 245 ip_set_type_unlock(); 246 return; 247 } 248 list_del_rcu(&type->list); 249 pr_debug("type %s, family %s with revision min %u unregistered.\n", 250 type->name, family_name(type->family), type->revision_min); 251 ip_set_type_unlock(); 252 253 synchronize_rcu(); 254 } 255 EXPORT_SYMBOL_GPL(ip_set_type_unregister); 256 257 /* Utility functions */ 258 void * 259 ip_set_alloc(size_t size) 260 { 261 return kvzalloc(size, GFP_KERNEL_ACCOUNT); 262 } 263 EXPORT_SYMBOL_GPL(ip_set_alloc); 264 265 void 266 ip_set_free(void *members) 267 { 268 pr_debug("%p: free with %s\n", members, 269 is_vmalloc_addr(members) ? "vfree" : "kfree"); 270 kvfree(members); 271 } 272 EXPORT_SYMBOL_GPL(ip_set_free); 273 274 static bool 275 flag_nested(const struct nlattr *nla) 276 { 277 return nla->nla_type & NLA_F_NESTED; 278 } 279 280 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = { 281 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 }, 282 [IPSET_ATTR_IPADDR_IPV6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 283 }; 284 285 int 286 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr) 287 { 288 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1]; 289 290 if (unlikely(!flag_nested(nla))) 291 return -IPSET_ERR_PROTOCOL; 292 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, 293 ipaddr_policy, NULL)) 294 return -IPSET_ERR_PROTOCOL; 295 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4))) 296 return -IPSET_ERR_PROTOCOL; 297 298 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]); 299 return 0; 300 } 301 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4); 302 303 int 304 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr) 305 { 306 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1]; 307 308 if (unlikely(!flag_nested(nla))) 309 return -IPSET_ERR_PROTOCOL; 310 311 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, 312 ipaddr_policy, NULL)) 313 return -IPSET_ERR_PROTOCOL; 314 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6))) 315 return -IPSET_ERR_PROTOCOL; 316 317 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]), 318 sizeof(struct in6_addr)); 319 return 0; 320 } 321 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6); 322 323 static u32 324 ip_set_timeout_get(const unsigned long *timeout) 325 { 326 u32 t; 327 328 if (*timeout == IPSET_ELEM_PERMANENT) 329 return 0; 330 331 t = jiffies_to_msecs(*timeout - jiffies) / MSEC_PER_SEC; 332 /* Zero value in userspace means no timeout */ 333 return t == 0 ? 1 : t; 334 } 335 336 static char * 337 ip_set_comment_uget(struct nlattr *tb) 338 { 339 return nla_data(tb); 340 } 341 342 /* Called from uadd only, protected by the set spinlock. 343 * The kadt functions don't use the comment extensions in any way. 344 */ 345 void 346 ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment, 347 const struct ip_set_ext *ext) 348 { 349 struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1); 350 size_t len = ext->comment ? strlen(ext->comment) : 0; 351 352 if (unlikely(c)) { 353 set->ext_size -= sizeof(*c) + strlen(c->str) + 1; 354 rcu_assign_pointer(comment->c, NULL); 355 kfree_rcu(c, rcu); 356 } 357 if (!len) 358 return; 359 if (unlikely(len > IPSET_MAX_COMMENT_SIZE)) 360 len = IPSET_MAX_COMMENT_SIZE; 361 c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC); 362 if (unlikely(!c)) 363 return; 364 strscpy(c->str, ext->comment, len + 1); 365 set->ext_size += sizeof(*c) + strlen(c->str) + 1; 366 rcu_assign_pointer(comment->c, c); 367 } 368 EXPORT_SYMBOL_GPL(ip_set_init_comment); 369 370 /* Used only when dumping a set, protected by rcu_read_lock() */ 371 static int 372 ip_set_put_comment(struct sk_buff *skb, const struct ip_set_comment *comment) 373 { 374 struct ip_set_comment_rcu *c = rcu_dereference(comment->c); 375 376 if (!c) 377 return 0; 378 return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str); 379 } 380 381 /* Called from uadd/udel, flush or the garbage collectors protected 382 * by the set spinlock. 383 * Called when the set is destroyed and when there can't be any user 384 * of the set data anymore. 385 */ 386 static void 387 ip_set_comment_free(struct ip_set *set, void *ptr) 388 { 389 struct ip_set_comment *comment = ptr; 390 struct ip_set_comment_rcu *c; 391 392 c = rcu_dereference_protected(comment->c, 1); 393 if (unlikely(!c)) 394 return; 395 set->ext_size -= sizeof(*c) + strlen(c->str) + 1; 396 rcu_assign_pointer(comment->c, NULL); 397 kfree_rcu(c, rcu); 398 } 399 400 typedef void (*destroyer)(struct ip_set *, void *); 401 /* ipset data extension types, in size order */ 402 403 const struct ip_set_ext_type ip_set_extensions[] = { 404 [IPSET_EXT_ID_COUNTER] = { 405 .type = IPSET_EXT_COUNTER, 406 .flag = IPSET_FLAG_WITH_COUNTERS, 407 .len = sizeof(struct ip_set_counter), 408 .align = __alignof__(struct ip_set_counter), 409 }, 410 [IPSET_EXT_ID_TIMEOUT] = { 411 .type = IPSET_EXT_TIMEOUT, 412 .len = sizeof(unsigned long), 413 .align = __alignof__(unsigned long), 414 }, 415 [IPSET_EXT_ID_SKBINFO] = { 416 .type = IPSET_EXT_SKBINFO, 417 .flag = IPSET_FLAG_WITH_SKBINFO, 418 .len = sizeof(struct ip_set_skbinfo), 419 .align = __alignof__(struct ip_set_skbinfo), 420 }, 421 [IPSET_EXT_ID_COMMENT] = { 422 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY, 423 .flag = IPSET_FLAG_WITH_COMMENT, 424 .len = sizeof(struct ip_set_comment), 425 .align = __alignof__(struct ip_set_comment), 426 .destroy = ip_set_comment_free, 427 }, 428 }; 429 EXPORT_SYMBOL_GPL(ip_set_extensions); 430 431 static bool 432 add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[]) 433 { 434 return ip_set_extensions[id].flag ? 435 (flags & ip_set_extensions[id].flag) : 436 !!tb[IPSET_ATTR_TIMEOUT]; 437 } 438 439 size_t 440 ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len, 441 size_t align) 442 { 443 enum ip_set_ext_id id; 444 u32 cadt_flags = 0; 445 446 if (tb[IPSET_ATTR_CADT_FLAGS]) 447 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 448 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD) 449 set->flags |= IPSET_CREATE_FLAG_FORCEADD; 450 if (!align) 451 align = 1; 452 for (id = 0; id < IPSET_EXT_ID_MAX; id++) { 453 if (!add_extension(id, cadt_flags, tb)) 454 continue; 455 if (align < ip_set_extensions[id].align) 456 align = ip_set_extensions[id].align; 457 len = ALIGN(len, ip_set_extensions[id].align); 458 set->offset[id] = len; 459 set->extensions |= ip_set_extensions[id].type; 460 len += ip_set_extensions[id].len; 461 } 462 return ALIGN(len, align); 463 } 464 EXPORT_SYMBOL_GPL(ip_set_elem_len); 465 466 int 467 ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[], 468 struct ip_set_ext *ext) 469 { 470 u64 fullmark; 471 472 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || 473 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) || 474 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) || 475 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) || 476 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) || 477 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE))) 478 return -IPSET_ERR_PROTOCOL; 479 480 if (tb[IPSET_ATTR_TIMEOUT]) { 481 if (!SET_WITH_TIMEOUT(set)) 482 return -IPSET_ERR_TIMEOUT; 483 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); 484 } 485 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) { 486 if (!SET_WITH_COUNTER(set)) 487 return -IPSET_ERR_COUNTER; 488 if (tb[IPSET_ATTR_BYTES]) 489 ext->bytes = be64_to_cpu(nla_get_be64( 490 tb[IPSET_ATTR_BYTES])); 491 if (tb[IPSET_ATTR_PACKETS]) 492 ext->packets = be64_to_cpu(nla_get_be64( 493 tb[IPSET_ATTR_PACKETS])); 494 } 495 if (tb[IPSET_ATTR_COMMENT]) { 496 if (!SET_WITH_COMMENT(set)) 497 return -IPSET_ERR_COMMENT; 498 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]); 499 } 500 if (tb[IPSET_ATTR_SKBMARK]) { 501 if (!SET_WITH_SKBINFO(set)) 502 return -IPSET_ERR_SKBINFO; 503 fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK])); 504 ext->skbinfo.skbmark = fullmark >> 32; 505 ext->skbinfo.skbmarkmask = fullmark & 0xffffffff; 506 } 507 if (tb[IPSET_ATTR_SKBPRIO]) { 508 if (!SET_WITH_SKBINFO(set)) 509 return -IPSET_ERR_SKBINFO; 510 ext->skbinfo.skbprio = 511 be32_to_cpu(nla_get_be32(tb[IPSET_ATTR_SKBPRIO])); 512 } 513 if (tb[IPSET_ATTR_SKBQUEUE]) { 514 if (!SET_WITH_SKBINFO(set)) 515 return -IPSET_ERR_SKBINFO; 516 ext->skbinfo.skbqueue = 517 be16_to_cpu(nla_get_be16(tb[IPSET_ATTR_SKBQUEUE])); 518 } 519 return 0; 520 } 521 EXPORT_SYMBOL_GPL(ip_set_get_extensions); 522 523 static u64 524 ip_set_get_bytes(const struct ip_set_counter *counter) 525 { 526 return (u64)atomic64_read(&(counter)->bytes); 527 } 528 529 static u64 530 ip_set_get_packets(const struct ip_set_counter *counter) 531 { 532 return (u64)atomic64_read(&(counter)->packets); 533 } 534 535 static bool 536 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter) 537 { 538 return nla_put_net64(skb, IPSET_ATTR_BYTES, 539 cpu_to_be64(ip_set_get_bytes(counter)), 540 IPSET_ATTR_PAD) || 541 nla_put_net64(skb, IPSET_ATTR_PACKETS, 542 cpu_to_be64(ip_set_get_packets(counter)), 543 IPSET_ATTR_PAD); 544 } 545 546 static bool 547 ip_set_put_skbinfo(struct sk_buff *skb, const struct ip_set_skbinfo *skbinfo) 548 { 549 /* Send nonzero parameters only */ 550 return ((skbinfo->skbmark || skbinfo->skbmarkmask) && 551 nla_put_net64(skb, IPSET_ATTR_SKBMARK, 552 cpu_to_be64((u64)skbinfo->skbmark << 32 | 553 skbinfo->skbmarkmask), 554 IPSET_ATTR_PAD)) || 555 (skbinfo->skbprio && 556 nla_put_net32(skb, IPSET_ATTR_SKBPRIO, 557 cpu_to_be32(skbinfo->skbprio))) || 558 (skbinfo->skbqueue && 559 nla_put_net16(skb, IPSET_ATTR_SKBQUEUE, 560 cpu_to_be16(skbinfo->skbqueue))); 561 } 562 563 int 564 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set, 565 const void *e, bool active) 566 { 567 if (SET_WITH_TIMEOUT(set)) { 568 unsigned long *timeout = ext_timeout(e, set); 569 570 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT, 571 htonl(active ? ip_set_timeout_get(timeout) 572 : *timeout))) 573 return -EMSGSIZE; 574 } 575 if (SET_WITH_COUNTER(set) && 576 ip_set_put_counter(skb, ext_counter(e, set))) 577 return -EMSGSIZE; 578 if (SET_WITH_COMMENT(set) && 579 ip_set_put_comment(skb, ext_comment(e, set))) 580 return -EMSGSIZE; 581 if (SET_WITH_SKBINFO(set) && 582 ip_set_put_skbinfo(skb, ext_skbinfo(e, set))) 583 return -EMSGSIZE; 584 return 0; 585 } 586 EXPORT_SYMBOL_GPL(ip_set_put_extensions); 587 588 static bool 589 ip_set_match_counter(u64 counter, u64 match, u8 op) 590 { 591 switch (op) { 592 case IPSET_COUNTER_NONE: 593 return true; 594 case IPSET_COUNTER_EQ: 595 return counter == match; 596 case IPSET_COUNTER_NE: 597 return counter != match; 598 case IPSET_COUNTER_LT: 599 return counter < match; 600 case IPSET_COUNTER_GT: 601 return counter > match; 602 } 603 return false; 604 } 605 606 static void 607 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter) 608 { 609 atomic64_add((long long)bytes, &(counter)->bytes); 610 } 611 612 static void 613 ip_set_add_packets(u64 packets, struct ip_set_counter *counter) 614 { 615 atomic64_add((long long)packets, &(counter)->packets); 616 } 617 618 static void 619 ip_set_update_counter(struct ip_set_counter *counter, 620 const struct ip_set_ext *ext, u32 flags) 621 { 622 if (ext->packets != ULLONG_MAX && 623 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) { 624 ip_set_add_bytes(ext->bytes, counter); 625 ip_set_add_packets(ext->packets, counter); 626 } 627 } 628 629 static void 630 ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo, 631 const struct ip_set_ext *ext, 632 struct ip_set_ext *mext, u32 flags) 633 { 634 mext->skbinfo = *skbinfo; 635 } 636 637 bool 638 ip_set_match_extensions(struct ip_set *set, const struct ip_set_ext *ext, 639 struct ip_set_ext *mext, u32 flags, void *data) 640 { 641 if (SET_WITH_TIMEOUT(set) && 642 ip_set_timeout_expired(ext_timeout(data, set))) 643 return false; 644 if (SET_WITH_COUNTER(set)) { 645 struct ip_set_counter *counter = ext_counter(data, set); 646 647 ip_set_update_counter(counter, ext, flags); 648 649 if (flags & IPSET_FLAG_MATCH_COUNTERS && 650 !(ip_set_match_counter(ip_set_get_packets(counter), 651 mext->packets, mext->packets_op) && 652 ip_set_match_counter(ip_set_get_bytes(counter), 653 mext->bytes, mext->bytes_op))) 654 return false; 655 } 656 if (SET_WITH_SKBINFO(set)) 657 ip_set_get_skbinfo(ext_skbinfo(data, set), 658 ext, mext, flags); 659 return true; 660 } 661 EXPORT_SYMBOL_GPL(ip_set_match_extensions); 662 663 /* Creating/destroying/renaming/swapping affect the existence and 664 * the properties of a set. All of these can be executed from userspace 665 * only and serialized by the nfnl mutex indirectly from nfnetlink. 666 * 667 * Sets are identified by their index in ip_set_list and the index 668 * is used by the external references (set/SET netfilter modules). 669 * 670 * The set behind an index may change by swapping only, from userspace. 671 */ 672 673 static void 674 __ip_set_get(struct ip_set *set) 675 { 676 write_lock_bh(&ip_set_ref_lock); 677 set->ref++; 678 write_unlock_bh(&ip_set_ref_lock); 679 } 680 681 static void 682 __ip_set_put(struct ip_set *set) 683 { 684 write_lock_bh(&ip_set_ref_lock); 685 BUG_ON(set->ref == 0); 686 set->ref--; 687 write_unlock_bh(&ip_set_ref_lock); 688 } 689 690 /* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need 691 * a separate reference counter 692 */ 693 static void 694 __ip_set_get_netlink(struct ip_set *set) 695 { 696 write_lock_bh(&ip_set_ref_lock); 697 set->ref_netlink++; 698 write_unlock_bh(&ip_set_ref_lock); 699 } 700 701 static void 702 __ip_set_put_netlink(struct ip_set *set) 703 { 704 write_lock_bh(&ip_set_ref_lock); 705 BUG_ON(set->ref_netlink == 0); 706 set->ref_netlink--; 707 write_unlock_bh(&ip_set_ref_lock); 708 } 709 710 /* Add, del and test set entries from kernel. 711 * 712 * The set behind the index must exist and must be referenced 713 * so it can't be destroyed (or changed) under our foot. 714 */ 715 716 static struct ip_set * 717 ip_set_rcu_get(struct net *net, ip_set_id_t index) 718 { 719 struct ip_set_net *inst = ip_set_pernet(net); 720 721 /* ip_set_list and the set pointer need to be protected */ 722 return ip_set_dereference_nfnl(inst->ip_set_list)[index]; 723 } 724 725 static inline void 726 ip_set_lock(struct ip_set *set) 727 { 728 if (!set->variant->region_lock) 729 spin_lock_bh(&set->lock); 730 } 731 732 static inline void 733 ip_set_unlock(struct ip_set *set) 734 { 735 if (!set->variant->region_lock) 736 spin_unlock_bh(&set->lock); 737 } 738 739 int 740 ip_set_test(ip_set_id_t index, const struct sk_buff *skb, 741 const struct xt_action_param *par, struct ip_set_adt_opt *opt) 742 { 743 struct ip_set *set = ip_set_rcu_get(xt_net(par), index); 744 int ret = 0; 745 746 BUG_ON(!set); 747 pr_debug("set %s, index %u\n", set->name, index); 748 749 if (opt->dim < set->type->dimension || 750 !(opt->family == set->family || set->family == NFPROTO_UNSPEC)) 751 return 0; 752 753 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt); 754 755 if (ret == -EAGAIN) { 756 /* Type requests element to be completed */ 757 pr_debug("element must be completed, ADD is triggered\n"); 758 ip_set_lock(set); 759 set->variant->kadt(set, skb, par, IPSET_ADD, opt); 760 ip_set_unlock(set); 761 ret = 1; 762 } else { 763 /* --return-nomatch: invert matched element */ 764 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) && 765 (set->type->features & IPSET_TYPE_NOMATCH) && 766 (ret > 0 || ret == -ENOTEMPTY)) 767 ret = -ret; 768 } 769 770 /* Convert error codes to nomatch */ 771 return (ret < 0 ? 0 : ret); 772 } 773 EXPORT_SYMBOL_GPL(ip_set_test); 774 775 int 776 ip_set_add(ip_set_id_t index, const struct sk_buff *skb, 777 const struct xt_action_param *par, struct ip_set_adt_opt *opt) 778 { 779 struct ip_set *set = ip_set_rcu_get(xt_net(par), index); 780 int ret; 781 782 BUG_ON(!set); 783 pr_debug("set %s, index %u\n", set->name, index); 784 785 if (opt->dim < set->type->dimension || 786 !(opt->family == set->family || set->family == NFPROTO_UNSPEC)) 787 return -IPSET_ERR_TYPE_MISMATCH; 788 789 ip_set_lock(set); 790 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt); 791 ip_set_unlock(set); 792 793 return ret; 794 } 795 EXPORT_SYMBOL_GPL(ip_set_add); 796 797 int 798 ip_set_del(ip_set_id_t index, const struct sk_buff *skb, 799 const struct xt_action_param *par, struct ip_set_adt_opt *opt) 800 { 801 struct ip_set *set = ip_set_rcu_get(xt_net(par), index); 802 int ret = 0; 803 804 BUG_ON(!set); 805 pr_debug("set %s, index %u\n", set->name, index); 806 807 if (opt->dim < set->type->dimension || 808 !(opt->family == set->family || set->family == NFPROTO_UNSPEC)) 809 return -IPSET_ERR_TYPE_MISMATCH; 810 811 ip_set_lock(set); 812 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt); 813 ip_set_unlock(set); 814 815 return ret; 816 } 817 EXPORT_SYMBOL_GPL(ip_set_del); 818 819 /* Find set by name, reference it once. The reference makes sure the 820 * thing pointed to, does not go away under our feet. 821 * 822 */ 823 ip_set_id_t 824 ip_set_get_byname(struct net *net, const struct nlattr *name, struct ip_set **set) 825 { 826 ip_set_id_t i, index = IPSET_INVALID_ID; 827 struct ip_set *s; 828 struct ip_set_net *inst = ip_set_pernet(net); 829 830 rcu_read_lock(); 831 for (i = 0; i < inst->ip_set_max; i++) { 832 s = rcu_dereference(inst->ip_set_list)[i]; 833 if (s && nla_strcmp(name, s->name) == 0) { 834 __ip_set_get(s); 835 index = i; 836 *set = s; 837 break; 838 } 839 } 840 rcu_read_unlock(); 841 842 return index; 843 } 844 EXPORT_SYMBOL_GPL(ip_set_get_byname); 845 846 /* If the given set pointer points to a valid set, decrement 847 * reference count by 1. The caller shall not assume the index 848 * to be valid, after calling this function. 849 * 850 */ 851 852 static void 853 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index) 854 { 855 struct ip_set *set; 856 857 rcu_read_lock(); 858 set = rcu_dereference(inst->ip_set_list)[index]; 859 if (set) 860 __ip_set_put(set); 861 rcu_read_unlock(); 862 } 863 864 void 865 ip_set_put_byindex(struct net *net, ip_set_id_t index) 866 { 867 struct ip_set_net *inst = ip_set_pernet(net); 868 869 __ip_set_put_byindex(inst, index); 870 } 871 EXPORT_SYMBOL_GPL(ip_set_put_byindex); 872 873 /* Get the name of a set behind a set index. 874 * Set itself is protected by RCU, but its name isn't: to protect against 875 * renaming, grab ip_set_ref_lock as reader (see ip_set_rename()) and copy the 876 * name. 877 */ 878 void 879 ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name) 880 { 881 struct ip_set *set = ip_set_rcu_get(net, index); 882 883 BUG_ON(!set); 884 885 read_lock_bh(&ip_set_ref_lock); 886 strscpy_pad(name, set->name, IPSET_MAXNAMELEN); 887 read_unlock_bh(&ip_set_ref_lock); 888 } 889 EXPORT_SYMBOL_GPL(ip_set_name_byindex); 890 891 /* Routines to call by external subsystems, which do not 892 * call nfnl_lock for us. 893 */ 894 895 /* Find set by index, reference it once. The reference makes sure the 896 * thing pointed to, does not go away under our feet. 897 * 898 * The nfnl mutex is used in the function. 899 */ 900 ip_set_id_t 901 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index) 902 { 903 struct ip_set *set; 904 struct ip_set_net *inst = ip_set_pernet(net); 905 906 if (index >= inst->ip_set_max) 907 return IPSET_INVALID_ID; 908 909 nfnl_lock(NFNL_SUBSYS_IPSET); 910 set = ip_set(inst, index); 911 if (set) 912 __ip_set_get(set); 913 else 914 index = IPSET_INVALID_ID; 915 nfnl_unlock(NFNL_SUBSYS_IPSET); 916 917 return index; 918 } 919 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex); 920 921 /* If the given set pointer points to a valid set, decrement 922 * reference count by 1. The caller shall not assume the index 923 * to be valid, after calling this function. 924 * 925 * The nfnl mutex is used in the function. 926 */ 927 void 928 ip_set_nfnl_put(struct net *net, ip_set_id_t index) 929 { 930 struct ip_set *set; 931 struct ip_set_net *inst = ip_set_pernet(net); 932 933 nfnl_lock(NFNL_SUBSYS_IPSET); 934 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */ 935 set = ip_set(inst, index); 936 if (set) 937 __ip_set_put(set); 938 } 939 nfnl_unlock(NFNL_SUBSYS_IPSET); 940 } 941 EXPORT_SYMBOL_GPL(ip_set_nfnl_put); 942 943 /* Communication protocol with userspace over netlink. 944 * 945 * The commands are serialized by the nfnl mutex. 946 */ 947 948 static inline u8 protocol(const struct nlattr * const tb[]) 949 { 950 return nla_get_u8(tb[IPSET_ATTR_PROTOCOL]); 951 } 952 953 static inline bool 954 protocol_failed(const struct nlattr * const tb[]) 955 { 956 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) != IPSET_PROTOCOL; 957 } 958 959 static inline bool 960 protocol_min_failed(const struct nlattr * const tb[]) 961 { 962 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) < IPSET_PROTOCOL_MIN; 963 } 964 965 static inline u32 966 flag_exist(const struct nlmsghdr *nlh) 967 { 968 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST; 969 } 970 971 static struct nlmsghdr * 972 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags, 973 enum ipset_cmd cmd) 974 { 975 return nfnl_msg_put(skb, portid, seq, 976 nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd), flags, 977 NFPROTO_IPV4, NFNETLINK_V0, 0); 978 } 979 980 /* Create a set */ 981 982 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = { 983 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 }, 984 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING, 985 .len = IPSET_MAXNAMELEN - 1 }, 986 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING, 987 .len = IPSET_MAXNAMELEN - 1}, 988 [IPSET_ATTR_REVISION] = NLA_POLICY_MAX(NLA_U8, IPSET_REVISION_MAX), 989 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 }, 990 [IPSET_ATTR_DATA] = { .type = NLA_NESTED }, 991 }; 992 993 static struct ip_set * 994 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id) 995 { 996 struct ip_set *set = NULL; 997 ip_set_id_t i; 998 999 *id = IPSET_INVALID_ID; 1000 for (i = 0; i < inst->ip_set_max; i++) { 1001 set = ip_set(inst, i); 1002 if (set && STRNCMP(set->name, name)) { 1003 *id = i; 1004 break; 1005 } 1006 } 1007 return (*id == IPSET_INVALID_ID ? NULL : set); 1008 } 1009 1010 static inline struct ip_set * 1011 find_set(struct ip_set_net *inst, const char *name) 1012 { 1013 ip_set_id_t id; 1014 1015 return find_set_and_id(inst, name, &id); 1016 } 1017 1018 static int 1019 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index, 1020 struct ip_set **set) 1021 { 1022 struct ip_set *s; 1023 ip_set_id_t i; 1024 1025 *index = IPSET_INVALID_ID; 1026 for (i = 0; i < inst->ip_set_max; i++) { 1027 s = ip_set(inst, i); 1028 if (!s) { 1029 if (*index == IPSET_INVALID_ID) 1030 *index = i; 1031 } else if (STRNCMP(name, s->name)) { 1032 /* Name clash */ 1033 *set = s; 1034 return -EEXIST; 1035 } 1036 } 1037 if (*index == IPSET_INVALID_ID) 1038 /* No free slot remained */ 1039 return -IPSET_ERR_MAX_SETS; 1040 return 0; 1041 } 1042 1043 static int ip_set_none(struct sk_buff *skb, const struct nfnl_info *info, 1044 const struct nlattr * const attr[]) 1045 { 1046 return -EOPNOTSUPP; 1047 } 1048 1049 static int ip_set_create(struct sk_buff *skb, const struct nfnl_info *info, 1050 const struct nlattr * const attr[]) 1051 { 1052 struct ip_set_net *inst = ip_set_pernet(info->net); 1053 struct ip_set *set, *clash = NULL; 1054 ip_set_id_t index = IPSET_INVALID_ID; 1055 struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {}; 1056 const char *name, *typename; 1057 u8 family, revision; 1058 u32 flags = flag_exist(info->nlh); 1059 int ret = 0; 1060 1061 if (unlikely(protocol_min_failed(attr) || 1062 !attr[IPSET_ATTR_SETNAME] || 1063 !attr[IPSET_ATTR_TYPENAME] || 1064 !attr[IPSET_ATTR_REVISION] || 1065 !attr[IPSET_ATTR_FAMILY] || 1066 (attr[IPSET_ATTR_DATA] && 1067 !flag_nested(attr[IPSET_ATTR_DATA])))) 1068 return -IPSET_ERR_PROTOCOL; 1069 1070 name = nla_data(attr[IPSET_ATTR_SETNAME]); 1071 typename = nla_data(attr[IPSET_ATTR_TYPENAME]); 1072 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]); 1073 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]); 1074 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n", 1075 name, typename, family_name(family), revision); 1076 1077 /* First, and without any locks, allocate and initialize 1078 * a normal base set structure. 1079 */ 1080 set = kzalloc_obj(*set); 1081 if (!set) 1082 return -ENOMEM; 1083 spin_lock_init(&set->lock); 1084 strscpy(set->name, name, IPSET_MAXNAMELEN); 1085 set->family = family; 1086 set->revision = revision; 1087 1088 /* Next, check that we know the type, and take 1089 * a reference on the type, to make sure it stays available 1090 * while constructing our new set. 1091 * 1092 * After referencing the type, we try to create the type 1093 * specific part of the set without holding any locks. 1094 */ 1095 ret = find_set_type_get(typename, family, revision, &set->type); 1096 if (ret) 1097 goto out; 1098 1099 /* Without holding any locks, create private part. */ 1100 if (attr[IPSET_ATTR_DATA] && 1101 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA], 1102 set->type->create_policy, NULL)) { 1103 ret = -IPSET_ERR_PROTOCOL; 1104 goto put_out; 1105 } 1106 /* Set create flags depending on the type revision */ 1107 set->flags |= set->type->create_flags[revision]; 1108 1109 ret = set->type->create(info->net, set, tb, flags); 1110 if (ret != 0) 1111 goto put_out; 1112 1113 /* BTW, ret==0 here. */ 1114 1115 /* Here, we have a valid, constructed set and we are protected 1116 * by the nfnl mutex. Find the first free index in ip_set_list 1117 * and check clashing. 1118 */ 1119 ret = find_free_id(inst, set->name, &index, &clash); 1120 if (ret == -EEXIST) { 1121 /* If this is the same set and requested, ignore error */ 1122 if ((flags & IPSET_FLAG_EXIST) && 1123 STRNCMP(set->type->name, clash->type->name) && 1124 set->type->family == clash->type->family && 1125 set->type->revision_min == clash->type->revision_min && 1126 set->type->revision_max == clash->type->revision_max && 1127 set->variant->same_set(set, clash)) 1128 ret = 0; 1129 goto cleanup; 1130 } else if (ret == -IPSET_ERR_MAX_SETS) { 1131 struct ip_set **list, **tmp; 1132 ip_set_id_t i = inst->ip_set_max + IP_SET_INC; 1133 1134 if (i < inst->ip_set_max || i == IPSET_INVALID_ID) 1135 /* Wraparound */ 1136 goto cleanup; 1137 1138 list = kvzalloc_objs(struct ip_set *, i); 1139 if (!list) 1140 goto cleanup; 1141 /* nfnl mutex is held, both lists are valid */ 1142 tmp = ip_set_dereference(inst); 1143 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max); 1144 rcu_assign_pointer(inst->ip_set_list, list); 1145 /* Make sure all current packets have passed through */ 1146 synchronize_net(); 1147 /* Use new list */ 1148 index = inst->ip_set_max; 1149 inst->ip_set_max = i; 1150 kvfree(tmp); 1151 ret = 0; 1152 } else if (ret) { 1153 goto cleanup; 1154 } 1155 1156 /* Finally! Add our shiny new set to the list, and be done. */ 1157 pr_debug("create: '%s' created with index %u!\n", set->name, index); 1158 ip_set(inst, index) = set; 1159 1160 return ret; 1161 1162 cleanup: 1163 set->variant->cancel_gc(set); 1164 set->variant->destroy(set); 1165 put_out: 1166 module_put(set->type->me); 1167 out: 1168 kfree(set); 1169 return ret; 1170 } 1171 1172 /* Destroy sets */ 1173 1174 static const struct nla_policy 1175 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = { 1176 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 }, 1177 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING, 1178 .len = IPSET_MAXNAMELEN - 1 }, 1179 }; 1180 1181 /* In order to return quickly when destroying a single set, it is split 1182 * into two stages: 1183 * - Cancel garbage collector 1184 * - Destroy the set itself via call_rcu() 1185 */ 1186 1187 static void 1188 ip_set_destroy_set_rcu(struct rcu_head *head) 1189 { 1190 struct ip_set *set = container_of(head, struct ip_set, rcu); 1191 1192 set->variant->destroy(set); 1193 module_put(set->type->me); 1194 kfree(set); 1195 } 1196 1197 static void 1198 _destroy_all_sets(struct ip_set_net *inst) 1199 { 1200 struct ip_set *set; 1201 ip_set_id_t i; 1202 bool need_wait = false; 1203 1204 /* First cancel gc's: set:list sets are flushed as well */ 1205 for (i = 0; i < inst->ip_set_max; i++) { 1206 set = ip_set(inst, i); 1207 if (set) { 1208 set->variant->cancel_gc(set); 1209 if (set->type->features & IPSET_TYPE_NAME) 1210 need_wait = true; 1211 } 1212 } 1213 /* Must wait for flush to be really finished */ 1214 if (need_wait) 1215 rcu_barrier(); 1216 for (i = 0; i < inst->ip_set_max; i++) { 1217 set = ip_set(inst, i); 1218 if (set) { 1219 ip_set(inst, i) = NULL; 1220 set->variant->destroy(set); 1221 module_put(set->type->me); 1222 kfree(set); 1223 } 1224 } 1225 } 1226 1227 static int ip_set_destroy(struct sk_buff *skb, const struct nfnl_info *info, 1228 const struct nlattr * const attr[]) 1229 { 1230 struct ip_set_net *inst = ip_set_pernet(info->net); 1231 struct ip_set *s; 1232 ip_set_id_t i; 1233 int ret = 0; 1234 1235 if (unlikely(protocol_min_failed(attr))) 1236 return -IPSET_ERR_PROTOCOL; 1237 1238 /* Commands are serialized and references are 1239 * protected by the ip_set_ref_lock. 1240 * External systems (i.e. xt_set) must call 1241 * ip_set_nfnl_get_* functions, that way we 1242 * can safely check references here. 1243 * 1244 * list:set timer can only decrement the reference 1245 * counter, so if it's already zero, we can proceed 1246 * without holding the lock. 1247 */ 1248 if (!attr[IPSET_ATTR_SETNAME]) { 1249 read_lock_bh(&ip_set_ref_lock); 1250 for (i = 0; i < inst->ip_set_max; i++) { 1251 s = ip_set(inst, i); 1252 if (s && (s->ref || s->ref_netlink)) { 1253 ret = -IPSET_ERR_BUSY; 1254 goto out; 1255 } 1256 } 1257 inst->is_destroyed = true; 1258 read_unlock_bh(&ip_set_ref_lock); 1259 _destroy_all_sets(inst); 1260 /* Modified by ip_set_destroy() only, which is serialized */ 1261 inst->is_destroyed = false; 1262 } else { 1263 u32 flags = flag_exist(info->nlh); 1264 u16 features = 0; 1265 1266 read_lock_bh(&ip_set_ref_lock); 1267 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), 1268 &i); 1269 if (!s) { 1270 if (!(flags & IPSET_FLAG_EXIST)) 1271 ret = -ENOENT; 1272 goto out; 1273 } else if (s->ref || s->ref_netlink) { 1274 ret = -IPSET_ERR_BUSY; 1275 goto out; 1276 } 1277 features = s->type->features; 1278 ip_set(inst, i) = NULL; 1279 read_unlock_bh(&ip_set_ref_lock); 1280 /* Must cancel garbage collectors */ 1281 s->variant->cancel_gc(s); 1282 if (features & IPSET_TYPE_NAME) { 1283 /* Must wait for flush to be really finished */ 1284 rcu_barrier(); 1285 } 1286 call_rcu(&s->rcu, ip_set_destroy_set_rcu); 1287 } 1288 return 0; 1289 out: 1290 read_unlock_bh(&ip_set_ref_lock); 1291 return ret; 1292 } 1293 1294 /* Flush sets */ 1295 1296 static void 1297 ip_set_flush_set(struct ip_set *set) 1298 { 1299 pr_debug("set: %s\n", set->name); 1300 1301 ip_set_lock(set); 1302 set->variant->flush(set); 1303 ip_set_unlock(set); 1304 } 1305 1306 static int ip_set_flush(struct sk_buff *skb, const struct nfnl_info *info, 1307 const struct nlattr * const attr[]) 1308 { 1309 struct ip_set_net *inst = ip_set_pernet(info->net); 1310 struct ip_set *s; 1311 ip_set_id_t i; 1312 1313 if (unlikely(protocol_min_failed(attr))) 1314 return -IPSET_ERR_PROTOCOL; 1315 1316 if (!attr[IPSET_ATTR_SETNAME]) { 1317 for (i = 0; i < inst->ip_set_max; i++) { 1318 s = ip_set(inst, i); 1319 if (s) 1320 ip_set_flush_set(s); 1321 } 1322 } else { 1323 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME])); 1324 if (!s) 1325 return -ENOENT; 1326 1327 ip_set_flush_set(s); 1328 } 1329 1330 return 0; 1331 } 1332 1333 /* Rename a set */ 1334 1335 static const struct nla_policy 1336 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = { 1337 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 }, 1338 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING, 1339 .len = IPSET_MAXNAMELEN - 1 }, 1340 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING, 1341 .len = IPSET_MAXNAMELEN - 1 }, 1342 }; 1343 1344 static int ip_set_rename(struct sk_buff *skb, const struct nfnl_info *info, 1345 const struct nlattr * const attr[]) 1346 { 1347 struct ip_set_net *inst = ip_set_pernet(info->net); 1348 struct ip_set *set, *s; 1349 const char *name2; 1350 ip_set_id_t i; 1351 int ret = 0; 1352 1353 if (unlikely(protocol_min_failed(attr) || 1354 !attr[IPSET_ATTR_SETNAME] || 1355 !attr[IPSET_ATTR_SETNAME2])) 1356 return -IPSET_ERR_PROTOCOL; 1357 1358 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME])); 1359 if (!set) 1360 return -ENOENT; 1361 1362 write_lock_bh(&ip_set_ref_lock); 1363 if (set->ref != 0 || set->ref_netlink != 0) { 1364 ret = -IPSET_ERR_REFERENCED; 1365 goto out; 1366 } 1367 1368 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]); 1369 for (i = 0; i < inst->ip_set_max; i++) { 1370 s = ip_set(inst, i); 1371 if (s && STRNCMP(s->name, name2)) { 1372 ret = -IPSET_ERR_EXIST_SETNAME2; 1373 goto out; 1374 } 1375 } 1376 strscpy_pad(set->name, name2, IPSET_MAXNAMELEN); 1377 1378 out: 1379 write_unlock_bh(&ip_set_ref_lock); 1380 return ret; 1381 } 1382 1383 /* Swap two sets so that name/index points to the other. 1384 * References and set names are also swapped. 1385 * 1386 * The commands are serialized by the nfnl mutex and references are 1387 * protected by the ip_set_ref_lock. The kernel interfaces 1388 * do not hold the mutex but the pointer settings are atomic 1389 * so the ip_set_list always contains valid pointers to the sets. 1390 */ 1391 1392 static int ip_set_swap(struct sk_buff *skb, const struct nfnl_info *info, 1393 const struct nlattr * const attr[]) 1394 { 1395 struct ip_set_net *inst = ip_set_pernet(info->net); 1396 struct ip_set *from, *to; 1397 ip_set_id_t from_id, to_id; 1398 char from_name[IPSET_MAXNAMELEN]; 1399 1400 if (unlikely(protocol_min_failed(attr) || 1401 !attr[IPSET_ATTR_SETNAME] || 1402 !attr[IPSET_ATTR_SETNAME2])) 1403 return -IPSET_ERR_PROTOCOL; 1404 1405 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), 1406 &from_id); 1407 if (!from) 1408 return -ENOENT; 1409 1410 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]), 1411 &to_id); 1412 if (!to) 1413 return -IPSET_ERR_EXIST_SETNAME2; 1414 1415 /* Features must not change. 1416 * Not an artifical restriction anymore, as we must prevent 1417 * possible loops created by swapping in setlist type of sets. 1418 */ 1419 if (!(from->type->features == to->type->features && 1420 from->family == to->family)) 1421 return -IPSET_ERR_TYPE_MISMATCH; 1422 1423 write_lock_bh(&ip_set_ref_lock); 1424 1425 if (from->ref_netlink || to->ref_netlink) { 1426 write_unlock_bh(&ip_set_ref_lock); 1427 return -EBUSY; 1428 } 1429 1430 strscpy_pad(from_name, from->name, IPSET_MAXNAMELEN); 1431 strscpy_pad(from->name, to->name, IPSET_MAXNAMELEN); 1432 strscpy_pad(to->name, from_name, IPSET_MAXNAMELEN); 1433 1434 swap(from->ref, to->ref); 1435 ip_set(inst, from_id) = to; 1436 ip_set(inst, to_id) = from; 1437 write_unlock_bh(&ip_set_ref_lock); 1438 1439 return 0; 1440 } 1441 1442 /* List/save set data */ 1443 1444 #define DUMP_INIT 0 1445 #define DUMP_ALL 1 1446 #define DUMP_ONE 2 1447 #define DUMP_LAST 3 1448 1449 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF) 1450 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16) 1451 1452 int 1453 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set) 1454 { 1455 u32 cadt_flags = 0; 1456 1457 if (SET_WITH_TIMEOUT(set)) 1458 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT, 1459 htonl(set->timeout)))) 1460 return -EMSGSIZE; 1461 if (SET_WITH_COUNTER(set)) 1462 cadt_flags |= IPSET_FLAG_WITH_COUNTERS; 1463 if (SET_WITH_COMMENT(set)) 1464 cadt_flags |= IPSET_FLAG_WITH_COMMENT; 1465 if (SET_WITH_SKBINFO(set)) 1466 cadt_flags |= IPSET_FLAG_WITH_SKBINFO; 1467 if (SET_WITH_FORCEADD(set)) 1468 cadt_flags |= IPSET_FLAG_WITH_FORCEADD; 1469 1470 if (!cadt_flags) 1471 return 0; 1472 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags)); 1473 } 1474 EXPORT_SYMBOL_GPL(ip_set_put_flags); 1475 1476 static int 1477 ip_set_dump_done(struct netlink_callback *cb) 1478 { 1479 if (cb->args[IPSET_CB_ARG0]) { 1480 struct ip_set_net *inst = 1481 (struct ip_set_net *)cb->args[IPSET_CB_NET]; 1482 ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX]; 1483 struct ip_set *set = ip_set_ref_netlink(inst, index); 1484 1485 if (set->variant->uref) 1486 set->variant->uref(set, cb, false); 1487 pr_debug("release set %s\n", set->name); 1488 __ip_set_put_netlink(set); 1489 } 1490 return 0; 1491 } 1492 1493 static inline void 1494 dump_attrs(struct nlmsghdr *nlh) 1495 { 1496 const struct nlattr *attr; 1497 int rem; 1498 1499 pr_debug("dump nlmsg\n"); 1500 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) { 1501 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len); 1502 } 1503 } 1504 1505 static const struct nla_policy 1506 ip_set_dump_policy[IPSET_ATTR_CMD_MAX + 1] = { 1507 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 }, 1508 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING, 1509 .len = IPSET_MAXNAMELEN - 1 }, 1510 [IPSET_ATTR_FLAGS] = { .type = NLA_U32 }, 1511 }; 1512 1513 static int 1514 ip_set_dump_start(struct netlink_callback *cb) 1515 { 1516 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb); 1517 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); 1518 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1]; 1519 struct nlattr *attr = (void *)nlh + min_len; 1520 struct sk_buff *skb = cb->skb; 1521 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk)); 1522 u32 dump_type; 1523 int ret; 1524 1525 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, attr, 1526 nlh->nlmsg_len - min_len, 1527 ip_set_dump_policy, NULL); 1528 if (ret) 1529 goto error; 1530 1531 cb->args[IPSET_CB_PROTO] = nla_get_u8(cda[IPSET_ATTR_PROTOCOL]); 1532 if (cda[IPSET_ATTR_SETNAME]) { 1533 ip_set_id_t index; 1534 struct ip_set *set; 1535 1536 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]), 1537 &index); 1538 if (!set) { 1539 ret = -ENOENT; 1540 goto error; 1541 } 1542 dump_type = DUMP_ONE; 1543 cb->args[IPSET_CB_INDEX] = index; 1544 } else { 1545 dump_type = DUMP_ALL; 1546 } 1547 1548 if (cda[IPSET_ATTR_FLAGS]) { 1549 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]); 1550 1551 dump_type |= (f << 16); 1552 } 1553 cb->args[IPSET_CB_NET] = (unsigned long)inst; 1554 cb->args[IPSET_CB_DUMP] = dump_type; 1555 1556 return 0; 1557 1558 error: 1559 /* We have to create and send the error message manually :-( */ 1560 if (nlh->nlmsg_flags & NLM_F_ACK) { 1561 netlink_ack(cb->skb, nlh, ret, NULL); 1562 } 1563 return ret; 1564 } 1565 1566 static int 1567 ip_set_dump_do(struct sk_buff *skb, struct netlink_callback *cb) 1568 { 1569 ip_set_id_t index = IPSET_INVALID_ID, max; 1570 struct ip_set *set = NULL; 1571 struct nlmsghdr *nlh = NULL; 1572 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0; 1573 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk)); 1574 u32 dump_type, dump_flags; 1575 bool is_destroyed; 1576 int ret = 0; 1577 1578 if (!cb->args[IPSET_CB_DUMP]) 1579 return -EINVAL; 1580 1581 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max) 1582 goto out; 1583 1584 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]); 1585 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]); 1586 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1 1587 : inst->ip_set_max; 1588 dump_last: 1589 pr_debug("dump type, flag: %u %u index: %ld\n", 1590 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]); 1591 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) { 1592 index = (ip_set_id_t)cb->args[IPSET_CB_INDEX]; 1593 write_lock_bh(&ip_set_ref_lock); 1594 set = ip_set(inst, index); 1595 is_destroyed = inst->is_destroyed; 1596 if (!set || is_destroyed) { 1597 write_unlock_bh(&ip_set_ref_lock); 1598 if (dump_type == DUMP_ONE) { 1599 ret = -ENOENT; 1600 goto out; 1601 } 1602 if (is_destroyed) { 1603 /* All sets are just being destroyed */ 1604 ret = 0; 1605 goto out; 1606 } 1607 continue; 1608 } 1609 /* When dumping all sets, we must dump "sorted" 1610 * so that lists (unions of sets) are dumped last. 1611 */ 1612 if (dump_type != DUMP_ONE && 1613 ((dump_type == DUMP_ALL) == 1614 !!(set->type->features & IPSET_DUMP_LAST))) { 1615 write_unlock_bh(&ip_set_ref_lock); 1616 set = NULL; 1617 continue; 1618 } 1619 pr_debug("List set: %s\n", set->name); 1620 if (!cb->args[IPSET_CB_ARG0]) { 1621 /* Start listing: make sure set won't be destroyed */ 1622 pr_debug("reference set\n"); 1623 set->ref_netlink++; 1624 } 1625 write_unlock_bh(&ip_set_ref_lock); 1626 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid, 1627 cb->nlh->nlmsg_seq, flags, 1628 IPSET_CMD_LIST); 1629 if (!nlh) { 1630 ret = -EMSGSIZE; 1631 goto release_refcount; 1632 } 1633 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL, 1634 cb->args[IPSET_CB_PROTO]) || 1635 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name)) 1636 goto nla_put_failure; 1637 if (dump_flags & IPSET_FLAG_LIST_SETNAME) 1638 goto next_set; 1639 switch (cb->args[IPSET_CB_ARG0]) { 1640 case 0: 1641 /* Core header data */ 1642 if (nla_put_string(skb, IPSET_ATTR_TYPENAME, 1643 set->type->name) || 1644 nla_put_u8(skb, IPSET_ATTR_FAMILY, 1645 set->family) || 1646 nla_put_u8(skb, IPSET_ATTR_REVISION, 1647 set->revision)) 1648 goto nla_put_failure; 1649 if (cb->args[IPSET_CB_PROTO] > IPSET_PROTOCOL_MIN && 1650 nla_put_net16(skb, IPSET_ATTR_INDEX, htons(index))) 1651 goto nla_put_failure; 1652 if (set->variant->uref) 1653 set->variant->uref(set, cb, true); 1654 ret = set->variant->head(set, skb); 1655 if (ret < 0) 1656 goto release_refcount; 1657 if (dump_flags & IPSET_FLAG_LIST_HEADER) 1658 goto next_set; 1659 fallthrough; 1660 default: 1661 ret = set->variant->list(set, skb, cb); 1662 if (!cb->args[IPSET_CB_ARG0]) 1663 /* Set is done, proceed with next one */ 1664 goto next_set; 1665 goto release_refcount; 1666 } 1667 } 1668 /* If we dump all sets, continue with dumping last ones */ 1669 if (dump_type == DUMP_ALL) { 1670 dump_type = DUMP_LAST; 1671 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16); 1672 cb->args[IPSET_CB_INDEX] = 0; 1673 if (set && set->variant->uref) 1674 set->variant->uref(set, cb, false); 1675 goto dump_last; 1676 } 1677 goto out; 1678 1679 nla_put_failure: 1680 ret = -EFAULT; 1681 next_set: 1682 if (dump_type == DUMP_ONE) 1683 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID; 1684 else 1685 cb->args[IPSET_CB_INDEX]++; 1686 release_refcount: 1687 /* If there was an error or set is done, release set */ 1688 if (ret || !cb->args[IPSET_CB_ARG0]) { 1689 set = ip_set_ref_netlink(inst, index); 1690 if (set->variant->uref) 1691 set->variant->uref(set, cb, false); 1692 pr_debug("release set %s\n", set->name); 1693 __ip_set_put_netlink(set); 1694 cb->args[IPSET_CB_ARG0] = 0; 1695 } 1696 out: 1697 if (nlh) { 1698 nlmsg_end(skb, nlh); 1699 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len); 1700 dump_attrs(nlh); 1701 } 1702 1703 return ret < 0 ? ret : skb->len; 1704 } 1705 1706 static int ip_set_dump(struct sk_buff *skb, const struct nfnl_info *info, 1707 const struct nlattr * const attr[]) 1708 { 1709 if (unlikely(protocol_min_failed(attr))) 1710 return -IPSET_ERR_PROTOCOL; 1711 1712 { 1713 struct netlink_dump_control c = { 1714 .start = ip_set_dump_start, 1715 .dump = ip_set_dump_do, 1716 .done = ip_set_dump_done, 1717 }; 1718 return netlink_dump_start(info->sk, skb, info->nlh, &c); 1719 } 1720 } 1721 1722 /* Add, del and test */ 1723 1724 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = { 1725 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 }, 1726 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING, 1727 .len = IPSET_MAXNAMELEN - 1 }, 1728 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 1729 [IPSET_ATTR_DATA] = { .type = NLA_NESTED }, 1730 [IPSET_ATTR_ADT] = { .type = NLA_NESTED }, 1731 }; 1732 1733 static int 1734 call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb, 1735 struct ip_set *set, struct nlattr *tb[], enum ipset_adt adt, 1736 u32 flags, bool use_lineno) 1737 { 1738 int ret; 1739 u32 lineno = 0; 1740 bool eexist = flags & IPSET_FLAG_EXIST, retried = false; 1741 1742 do { 1743 if (retried) { 1744 __ip_set_get_netlink(set); 1745 nfnl_unlock(NFNL_SUBSYS_IPSET); 1746 cond_resched(); 1747 nfnl_lock(NFNL_SUBSYS_IPSET); 1748 __ip_set_put_netlink(set); 1749 } 1750 1751 ip_set_lock(set); 1752 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried); 1753 ip_set_unlock(set); 1754 retried = true; 1755 } while (ret == -ERANGE || 1756 (ret == -EAGAIN && 1757 set->variant->resize && 1758 (ret = set->variant->resize(set, retried)) == 0)); 1759 1760 if (!ret || (ret == -IPSET_ERR_EXIST && eexist)) 1761 return 0; 1762 if (lineno && use_lineno) { 1763 /* Error in restore/batch mode: send back lineno */ 1764 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb); 1765 struct sk_buff *skb2; 1766 struct nlmsgerr *errmsg; 1767 size_t payload = min(SIZE_MAX, 1768 sizeof(*errmsg) + nlmsg_len(nlh)); 1769 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); 1770 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1]; 1771 struct nlattr *cmdattr; 1772 u32 *errline; 1773 1774 skb2 = nlmsg_new(payload, GFP_KERNEL); 1775 if (!skb2) 1776 return -ENOMEM; 1777 rep = nlmsg_put(skb2, NETLINK_CB(skb).portid, 1778 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0); 1779 errmsg = nlmsg_data(rep); 1780 errmsg->error = ret; 1781 unsafe_memcpy(&errmsg->msg, nlh, nlh->nlmsg_len, 1782 /* Bounds checked by the skb layer. */); 1783 1784 cmdattr = (void *)&errmsg->msg + min_len; 1785 1786 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, cmdattr, 1787 nlh->nlmsg_len - min_len, ip_set_adt_policy, 1788 NULL); 1789 1790 if (ret) { 1791 nlmsg_free(skb2); 1792 return ret; 1793 } 1794 errline = nla_data(cda[IPSET_ATTR_LINENO]); 1795 1796 *errline = lineno; 1797 1798 nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); 1799 /* Signal netlink not to send its ACK/errmsg. */ 1800 return -EINTR; 1801 } 1802 1803 return ret; 1804 } 1805 1806 static int ip_set_ad(struct net *net, struct sock *ctnl, 1807 struct sk_buff *skb, 1808 enum ipset_adt adt, 1809 const struct nlmsghdr *nlh, 1810 const struct nlattr * const attr[], 1811 struct netlink_ext_ack *extack) 1812 { 1813 struct ip_set_net *inst = ip_set_pernet(net); 1814 struct ip_set *set; 1815 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {}; 1816 const struct nlattr *nla; 1817 u32 flags = flag_exist(nlh); 1818 bool use_lineno; 1819 int ret = 0; 1820 1821 if (unlikely(protocol_min_failed(attr) || 1822 !attr[IPSET_ATTR_SETNAME] || 1823 !((attr[IPSET_ATTR_DATA] != NULL) ^ 1824 (attr[IPSET_ATTR_ADT] != NULL)) || 1825 (attr[IPSET_ATTR_DATA] && 1826 !flag_nested(attr[IPSET_ATTR_DATA])) || 1827 (attr[IPSET_ATTR_ADT] && 1828 (!flag_nested(attr[IPSET_ATTR_ADT]) || 1829 !attr[IPSET_ATTR_LINENO])))) 1830 return -IPSET_ERR_PROTOCOL; 1831 1832 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME])); 1833 if (!set) 1834 return -ENOENT; 1835 1836 use_lineno = !!attr[IPSET_ATTR_LINENO]; 1837 if (attr[IPSET_ATTR_DATA]) { 1838 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, 1839 attr[IPSET_ATTR_DATA], 1840 set->type->adt_policy, NULL)) 1841 return -IPSET_ERR_PROTOCOL; 1842 ret = call_ad(net, ctnl, skb, set, tb, adt, flags, 1843 use_lineno); 1844 } else { 1845 int nla_rem; 1846 1847 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) { 1848 if (nla_type(nla) != IPSET_ATTR_DATA || 1849 !flag_nested(nla) || 1850 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla, 1851 set->type->adt_policy, NULL)) 1852 return -IPSET_ERR_PROTOCOL; 1853 ret = call_ad(net, ctnl, skb, set, tb, adt, 1854 flags, use_lineno); 1855 if (ret < 0) 1856 return ret; 1857 } 1858 } 1859 return ret; 1860 } 1861 1862 static int ip_set_uadd(struct sk_buff *skb, const struct nfnl_info *info, 1863 const struct nlattr * const attr[]) 1864 { 1865 return ip_set_ad(info->net, info->sk, skb, 1866 IPSET_ADD, info->nlh, attr, info->extack); 1867 } 1868 1869 static int ip_set_udel(struct sk_buff *skb, const struct nfnl_info *info, 1870 const struct nlattr * const attr[]) 1871 { 1872 return ip_set_ad(info->net, info->sk, skb, 1873 IPSET_DEL, info->nlh, attr, info->extack); 1874 } 1875 1876 static int ip_set_utest(struct sk_buff *skb, const struct nfnl_info *info, 1877 const struct nlattr * const attr[]) 1878 { 1879 struct ip_set_net *inst = ip_set_pernet(info->net); 1880 struct ip_set *set; 1881 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {}; 1882 int ret = 0; 1883 u32 lineno; 1884 1885 if (unlikely(protocol_min_failed(attr) || 1886 !attr[IPSET_ATTR_SETNAME] || 1887 !attr[IPSET_ATTR_DATA] || 1888 !flag_nested(attr[IPSET_ATTR_DATA]))) 1889 return -IPSET_ERR_PROTOCOL; 1890 1891 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME])); 1892 if (!set) 1893 return -ENOENT; 1894 1895 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA], 1896 set->type->adt_policy, NULL)) 1897 return -IPSET_ERR_PROTOCOL; 1898 1899 rcu_read_lock_bh(); 1900 ret = set->variant->uadt(set, tb, IPSET_TEST, &lineno, 0, 0); 1901 rcu_read_unlock_bh(); 1902 /* Userspace can't trigger element to be re-added */ 1903 if (ret == -EAGAIN) 1904 ret = 1; 1905 1906 return ret > 0 ? 0 : -IPSET_ERR_EXIST; 1907 } 1908 1909 /* Get headed data of a set */ 1910 1911 static int ip_set_header(struct sk_buff *skb, const struct nfnl_info *info, 1912 const struct nlattr * const attr[]) 1913 { 1914 struct ip_set_net *inst = ip_set_pernet(info->net); 1915 const struct ip_set *set; 1916 struct sk_buff *skb2; 1917 struct nlmsghdr *nlh2; 1918 1919 if (unlikely(protocol_min_failed(attr) || 1920 !attr[IPSET_ATTR_SETNAME])) 1921 return -IPSET_ERR_PROTOCOL; 1922 1923 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME])); 1924 if (!set) 1925 return -ENOENT; 1926 1927 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1928 if (!skb2) 1929 return -ENOMEM; 1930 1931 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0, 1932 IPSET_CMD_HEADER); 1933 if (!nlh2) 1934 goto nlmsg_failure; 1935 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) || 1936 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) || 1937 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) || 1938 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) || 1939 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision)) 1940 goto nla_put_failure; 1941 nlmsg_end(skb2, nlh2); 1942 1943 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); 1944 1945 nla_put_failure: 1946 nlmsg_cancel(skb2, nlh2); 1947 nlmsg_failure: 1948 kfree_skb(skb2); 1949 return -EMSGSIZE; 1950 } 1951 1952 /* Get type data */ 1953 1954 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = { 1955 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 }, 1956 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING, 1957 .len = IPSET_MAXNAMELEN - 1 }, 1958 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 }, 1959 }; 1960 1961 static int ip_set_type(struct sk_buff *skb, const struct nfnl_info *info, 1962 const struct nlattr * const attr[]) 1963 { 1964 struct sk_buff *skb2; 1965 struct nlmsghdr *nlh2; 1966 u8 family, min, max; 1967 const char *typename; 1968 int ret = 0; 1969 1970 if (unlikely(protocol_min_failed(attr) || 1971 !attr[IPSET_ATTR_TYPENAME] || 1972 !attr[IPSET_ATTR_FAMILY])) 1973 return -IPSET_ERR_PROTOCOL; 1974 1975 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]); 1976 typename = nla_data(attr[IPSET_ATTR_TYPENAME]); 1977 ret = find_set_type_minmax(typename, family, &min, &max); 1978 if (ret) 1979 return ret; 1980 1981 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1982 if (!skb2) 1983 return -ENOMEM; 1984 1985 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0, 1986 IPSET_CMD_TYPE); 1987 if (!nlh2) 1988 goto nlmsg_failure; 1989 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) || 1990 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) || 1991 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) || 1992 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) || 1993 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min)) 1994 goto nla_put_failure; 1995 nlmsg_end(skb2, nlh2); 1996 1997 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len); 1998 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); 1999 2000 nla_put_failure: 2001 nlmsg_cancel(skb2, nlh2); 2002 nlmsg_failure: 2003 kfree_skb(skb2); 2004 return -EMSGSIZE; 2005 } 2006 2007 /* Get protocol version */ 2008 2009 static const struct nla_policy 2010 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = { 2011 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 }, 2012 }; 2013 2014 static int ip_set_protocol(struct sk_buff *skb, const struct nfnl_info *info, 2015 const struct nlattr * const attr[]) 2016 { 2017 struct sk_buff *skb2; 2018 struct nlmsghdr *nlh2; 2019 2020 if (unlikely(!attr[IPSET_ATTR_PROTOCOL])) 2021 return -IPSET_ERR_PROTOCOL; 2022 2023 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2024 if (!skb2) 2025 return -ENOMEM; 2026 2027 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0, 2028 IPSET_CMD_PROTOCOL); 2029 if (!nlh2) 2030 goto nlmsg_failure; 2031 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL)) 2032 goto nla_put_failure; 2033 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL_MIN, IPSET_PROTOCOL_MIN)) 2034 goto nla_put_failure; 2035 nlmsg_end(skb2, nlh2); 2036 2037 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); 2038 2039 nla_put_failure: 2040 nlmsg_cancel(skb2, nlh2); 2041 nlmsg_failure: 2042 kfree_skb(skb2); 2043 return -EMSGSIZE; 2044 } 2045 2046 /* Get set by name or index, from userspace */ 2047 2048 static int ip_set_byname(struct sk_buff *skb, const struct nfnl_info *info, 2049 const struct nlattr * const attr[]) 2050 { 2051 struct ip_set_net *inst = ip_set_pernet(info->net); 2052 struct sk_buff *skb2; 2053 struct nlmsghdr *nlh2; 2054 ip_set_id_t id = IPSET_INVALID_ID; 2055 const struct ip_set *set; 2056 2057 if (unlikely(protocol_failed(attr) || 2058 !attr[IPSET_ATTR_SETNAME])) 2059 return -IPSET_ERR_PROTOCOL; 2060 2061 set = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), &id); 2062 if (id == IPSET_INVALID_ID) 2063 return -ENOENT; 2064 2065 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2066 if (!skb2) 2067 return -ENOMEM; 2068 2069 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0, 2070 IPSET_CMD_GET_BYNAME); 2071 if (!nlh2) 2072 goto nlmsg_failure; 2073 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) || 2074 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) || 2075 nla_put_net16(skb2, IPSET_ATTR_INDEX, htons(id))) 2076 goto nla_put_failure; 2077 nlmsg_end(skb2, nlh2); 2078 2079 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); 2080 2081 nla_put_failure: 2082 nlmsg_cancel(skb2, nlh2); 2083 nlmsg_failure: 2084 kfree_skb(skb2); 2085 return -EMSGSIZE; 2086 } 2087 2088 static const struct nla_policy ip_set_index_policy[IPSET_ATTR_CMD_MAX + 1] = { 2089 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 }, 2090 [IPSET_ATTR_INDEX] = { .type = NLA_U16 }, 2091 }; 2092 2093 static int ip_set_byindex(struct sk_buff *skb, const struct nfnl_info *info, 2094 const struct nlattr * const attr[]) 2095 { 2096 struct ip_set_net *inst = ip_set_pernet(info->net); 2097 struct sk_buff *skb2; 2098 struct nlmsghdr *nlh2; 2099 ip_set_id_t id = IPSET_INVALID_ID; 2100 const struct ip_set *set; 2101 2102 if (unlikely(protocol_failed(attr) || 2103 !attr[IPSET_ATTR_INDEX])) 2104 return -IPSET_ERR_PROTOCOL; 2105 2106 id = ip_set_get_h16(attr[IPSET_ATTR_INDEX]); 2107 if (id >= inst->ip_set_max) 2108 return -ENOENT; 2109 set = ip_set(inst, id); 2110 if (set == NULL) 2111 return -ENOENT; 2112 2113 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2114 if (!skb2) 2115 return -ENOMEM; 2116 2117 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0, 2118 IPSET_CMD_GET_BYINDEX); 2119 if (!nlh2) 2120 goto nlmsg_failure; 2121 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) || 2122 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name)) 2123 goto nla_put_failure; 2124 nlmsg_end(skb2, nlh2); 2125 2126 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); 2127 2128 nla_put_failure: 2129 nlmsg_cancel(skb2, nlh2); 2130 nlmsg_failure: 2131 kfree_skb(skb2); 2132 return -EMSGSIZE; 2133 } 2134 2135 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = { 2136 [IPSET_CMD_NONE] = { 2137 .call = ip_set_none, 2138 .type = NFNL_CB_MUTEX, 2139 .attr_count = IPSET_ATTR_CMD_MAX, 2140 }, 2141 [IPSET_CMD_CREATE] = { 2142 .call = ip_set_create, 2143 .type = NFNL_CB_MUTEX, 2144 .attr_count = IPSET_ATTR_CMD_MAX, 2145 .policy = ip_set_create_policy, 2146 }, 2147 [IPSET_CMD_DESTROY] = { 2148 .call = ip_set_destroy, 2149 .type = NFNL_CB_MUTEX, 2150 .attr_count = IPSET_ATTR_CMD_MAX, 2151 .policy = ip_set_setname_policy, 2152 }, 2153 [IPSET_CMD_FLUSH] = { 2154 .call = ip_set_flush, 2155 .type = NFNL_CB_MUTEX, 2156 .attr_count = IPSET_ATTR_CMD_MAX, 2157 .policy = ip_set_setname_policy, 2158 }, 2159 [IPSET_CMD_RENAME] = { 2160 .call = ip_set_rename, 2161 .type = NFNL_CB_MUTEX, 2162 .attr_count = IPSET_ATTR_CMD_MAX, 2163 .policy = ip_set_setname2_policy, 2164 }, 2165 [IPSET_CMD_SWAP] = { 2166 .call = ip_set_swap, 2167 .type = NFNL_CB_MUTEX, 2168 .attr_count = IPSET_ATTR_CMD_MAX, 2169 .policy = ip_set_setname2_policy, 2170 }, 2171 [IPSET_CMD_LIST] = { 2172 .call = ip_set_dump, 2173 .type = NFNL_CB_MUTEX, 2174 .attr_count = IPSET_ATTR_CMD_MAX, 2175 .policy = ip_set_dump_policy, 2176 }, 2177 [IPSET_CMD_SAVE] = { 2178 .call = ip_set_dump, 2179 .type = NFNL_CB_MUTEX, 2180 .attr_count = IPSET_ATTR_CMD_MAX, 2181 .policy = ip_set_setname_policy, 2182 }, 2183 [IPSET_CMD_ADD] = { 2184 .call = ip_set_uadd, 2185 .type = NFNL_CB_MUTEX, 2186 .attr_count = IPSET_ATTR_CMD_MAX, 2187 .policy = ip_set_adt_policy, 2188 }, 2189 [IPSET_CMD_DEL] = { 2190 .call = ip_set_udel, 2191 .type = NFNL_CB_MUTEX, 2192 .attr_count = IPSET_ATTR_CMD_MAX, 2193 .policy = ip_set_adt_policy, 2194 }, 2195 [IPSET_CMD_TEST] = { 2196 .call = ip_set_utest, 2197 .type = NFNL_CB_MUTEX, 2198 .attr_count = IPSET_ATTR_CMD_MAX, 2199 .policy = ip_set_adt_policy, 2200 }, 2201 [IPSET_CMD_HEADER] = { 2202 .call = ip_set_header, 2203 .type = NFNL_CB_MUTEX, 2204 .attr_count = IPSET_ATTR_CMD_MAX, 2205 .policy = ip_set_setname_policy, 2206 }, 2207 [IPSET_CMD_TYPE] = { 2208 .call = ip_set_type, 2209 .type = NFNL_CB_MUTEX, 2210 .attr_count = IPSET_ATTR_CMD_MAX, 2211 .policy = ip_set_type_policy, 2212 }, 2213 [IPSET_CMD_PROTOCOL] = { 2214 .call = ip_set_protocol, 2215 .type = NFNL_CB_MUTEX, 2216 .attr_count = IPSET_ATTR_CMD_MAX, 2217 .policy = ip_set_protocol_policy, 2218 }, 2219 [IPSET_CMD_GET_BYNAME] = { 2220 .call = ip_set_byname, 2221 .type = NFNL_CB_MUTEX, 2222 .attr_count = IPSET_ATTR_CMD_MAX, 2223 .policy = ip_set_setname_policy, 2224 }, 2225 [IPSET_CMD_GET_BYINDEX] = { 2226 .call = ip_set_byindex, 2227 .type = NFNL_CB_MUTEX, 2228 .attr_count = IPSET_ATTR_CMD_MAX, 2229 .policy = ip_set_index_policy, 2230 }, 2231 }; 2232 2233 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = { 2234 .name = "ip_set", 2235 .subsys_id = NFNL_SUBSYS_IPSET, 2236 .cb_count = IPSET_MSG_MAX, 2237 .cb = ip_set_netlink_subsys_cb, 2238 }; 2239 2240 /* Interface to iptables/ip6tables */ 2241 2242 static int 2243 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len) 2244 { 2245 unsigned int *op; 2246 void *data; 2247 int copylen = *len, ret = 0; 2248 struct net *net = sock_net(sk); 2249 struct ip_set_net *inst = ip_set_pernet(net); 2250 2251 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2252 return -EPERM; 2253 if (optval != SO_IP_SET) 2254 return -EBADF; 2255 if (*len < sizeof(unsigned int)) 2256 return -EINVAL; 2257 2258 data = vmalloc(*len); 2259 if (!data) 2260 return -ENOMEM; 2261 if (copy_from_user(data, user, *len) != 0) { 2262 ret = -EFAULT; 2263 goto done; 2264 } 2265 op = data; 2266 2267 if (*op < IP_SET_OP_VERSION) { 2268 /* Check the version at the beginning of operations */ 2269 struct ip_set_req_version *req_version = data; 2270 2271 if (*len < sizeof(struct ip_set_req_version)) { 2272 ret = -EINVAL; 2273 goto done; 2274 } 2275 2276 if (req_version->version < IPSET_PROTOCOL_MIN) { 2277 ret = -EPROTO; 2278 goto done; 2279 } 2280 } 2281 2282 switch (*op) { 2283 case IP_SET_OP_VERSION: { 2284 struct ip_set_req_version *req_version = data; 2285 2286 if (*len != sizeof(struct ip_set_req_version)) { 2287 ret = -EINVAL; 2288 goto done; 2289 } 2290 2291 req_version->version = IPSET_PROTOCOL; 2292 if (copy_to_user(user, req_version, 2293 sizeof(struct ip_set_req_version))) 2294 ret = -EFAULT; 2295 goto done; 2296 } 2297 case IP_SET_OP_GET_BYNAME: { 2298 struct ip_set_req_get_set *req_get = data; 2299 ip_set_id_t id; 2300 2301 if (*len != sizeof(struct ip_set_req_get_set)) { 2302 ret = -EINVAL; 2303 goto done; 2304 } 2305 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0'; 2306 nfnl_lock(NFNL_SUBSYS_IPSET); 2307 find_set_and_id(inst, req_get->set.name, &id); 2308 req_get->set.index = id; 2309 nfnl_unlock(NFNL_SUBSYS_IPSET); 2310 goto copy; 2311 } 2312 case IP_SET_OP_GET_FNAME: { 2313 struct ip_set_req_get_set_family *req_get = data; 2314 ip_set_id_t id; 2315 2316 if (*len != sizeof(struct ip_set_req_get_set_family)) { 2317 ret = -EINVAL; 2318 goto done; 2319 } 2320 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0'; 2321 nfnl_lock(NFNL_SUBSYS_IPSET); 2322 find_set_and_id(inst, req_get->set.name, &id); 2323 req_get->set.index = id; 2324 if (id != IPSET_INVALID_ID) 2325 req_get->family = ip_set(inst, id)->family; 2326 nfnl_unlock(NFNL_SUBSYS_IPSET); 2327 goto copy; 2328 } 2329 case IP_SET_OP_GET_BYINDEX: { 2330 struct ip_set_req_get_set *req_get = data; 2331 struct ip_set *set; 2332 2333 if (*len != sizeof(struct ip_set_req_get_set) || 2334 req_get->set.index >= inst->ip_set_max) { 2335 ret = -EINVAL; 2336 goto done; 2337 } 2338 nfnl_lock(NFNL_SUBSYS_IPSET); 2339 set = ip_set(inst, req_get->set.index); 2340 ret = strscpy(req_get->set.name, set ? set->name : "", 2341 IPSET_MAXNAMELEN); 2342 nfnl_unlock(NFNL_SUBSYS_IPSET); 2343 if (ret < 0) 2344 goto done; 2345 goto copy; 2346 } 2347 default: 2348 ret = -EBADMSG; 2349 goto done; 2350 } /* end of switch(op) */ 2351 2352 copy: 2353 if (copy_to_user(user, data, copylen)) 2354 ret = -EFAULT; 2355 2356 done: 2357 vfree(data); 2358 if (ret > 0) 2359 ret = 0; 2360 return ret; 2361 } 2362 2363 static struct nf_sockopt_ops so_set __read_mostly = { 2364 .pf = PF_INET, 2365 .get_optmin = SO_IP_SET, 2366 .get_optmax = SO_IP_SET + 1, 2367 .get = ip_set_sockfn_get, 2368 .owner = THIS_MODULE, 2369 }; 2370 2371 static int __net_init 2372 ip_set_net_init(struct net *net) 2373 { 2374 struct ip_set_net *inst = ip_set_pernet(net); 2375 struct ip_set **list; 2376 2377 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX; 2378 if (inst->ip_set_max >= IPSET_INVALID_ID) 2379 inst->ip_set_max = IPSET_INVALID_ID - 1; 2380 2381 list = kvzalloc_objs(struct ip_set *, inst->ip_set_max); 2382 if (!list) 2383 return -ENOMEM; 2384 inst->is_deleted = false; 2385 inst->is_destroyed = false; 2386 rcu_assign_pointer(inst->ip_set_list, list); 2387 return 0; 2388 } 2389 2390 static void __net_exit 2391 ip_set_net_pre_exit(struct net *net) 2392 { 2393 struct ip_set_net *inst = ip_set_pernet(net); 2394 2395 inst->is_deleted = true; /* flag for ip_set_nfnl_put */ 2396 } 2397 2398 static void __net_exit 2399 ip_set_net_exit(struct net *net) 2400 { 2401 struct ip_set_net *inst = ip_set_pernet(net); 2402 2403 _destroy_all_sets(inst); 2404 kvfree(rcu_dereference_protected(inst->ip_set_list, 1)); 2405 } 2406 2407 static struct pernet_operations ip_set_net_ops = { 2408 .init = ip_set_net_init, 2409 .pre_exit = ip_set_net_pre_exit, 2410 .exit = ip_set_net_exit, 2411 .id = &ip_set_net_id, 2412 .size = sizeof(struct ip_set_net), 2413 }; 2414 2415 static int __init 2416 ip_set_init(void) 2417 { 2418 int ret = register_pernet_subsys(&ip_set_net_ops); 2419 2420 if (ret) { 2421 pr_err("ip_set: cannot register pernet_subsys.\n"); 2422 return ret; 2423 } 2424 2425 ret = nfnetlink_subsys_register(&ip_set_netlink_subsys); 2426 if (ret != 0) { 2427 pr_err("ip_set: cannot register with nfnetlink.\n"); 2428 unregister_pernet_subsys(&ip_set_net_ops); 2429 return ret; 2430 } 2431 2432 ret = nf_register_sockopt(&so_set); 2433 if (ret != 0) { 2434 pr_err("SO_SET registry failed: %d\n", ret); 2435 nfnetlink_subsys_unregister(&ip_set_netlink_subsys); 2436 unregister_pernet_subsys(&ip_set_net_ops); 2437 return ret; 2438 } 2439 2440 return 0; 2441 } 2442 2443 static void __exit 2444 ip_set_fini(void) 2445 { 2446 nf_unregister_sockopt(&so_set); 2447 nfnetlink_subsys_unregister(&ip_set_netlink_subsys); 2448 unregister_pernet_subsys(&ip_set_net_ops); 2449 2450 /* Wait for call_rcu() in destroy */ 2451 rcu_barrier(); 2452 2453 pr_debug("these are the famous last words\n"); 2454 } 2455 2456 module_init(ip_set_init); 2457 module_exit(ip_set_fini); 2458 2459 MODULE_DESCRIPTION("ip_set: protocol " __stringify(IPSET_PROTOCOL)); 2460