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