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