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