Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1 | /* |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2 | * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/netlink.h> |
| 16 | #include <linux/netfilter.h> |
| 17 | #include <linux/netfilter/nfnetlink.h> |
| 18 | #include <linux/netfilter/nf_tables.h> |
| 19 | #include <net/netfilter/nf_tables_core.h> |
| 20 | #include <net/netfilter/nf_tables.h> |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 21 | #include <net/net_namespace.h> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 22 | #include <net/sock.h> |
| 23 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 24 | static LIST_HEAD(nf_tables_expressions); |
| 25 | |
| 26 | /** |
| 27 | * nft_register_afinfo - register nf_tables address family info |
| 28 | * |
| 29 | * @afi: address family info to register |
| 30 | * |
| 31 | * Register the address family for use with nf_tables. Returns zero on |
| 32 | * success or a negative errno code otherwise. |
| 33 | */ |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 34 | int nft_register_afinfo(struct net *net, struct nft_af_info *afi) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 35 | { |
| 36 | INIT_LIST_HEAD(&afi->tables); |
| 37 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 38 | list_add_tail_rcu(&afi->list, &net->nft.af_info); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 39 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 40 | return 0; |
| 41 | } |
| 42 | EXPORT_SYMBOL_GPL(nft_register_afinfo); |
| 43 | |
Pablo Neira Ayuso | df05ef8 | 2015-12-15 19:39:32 +0100 | [diff] [blame^] | 44 | static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi); |
| 45 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 46 | /** |
| 47 | * nft_unregister_afinfo - unregister nf_tables address family info |
| 48 | * |
| 49 | * @afi: address family info to unregister |
| 50 | * |
| 51 | * Unregister the address family for use with nf_tables. |
| 52 | */ |
Pablo Neira Ayuso | df05ef8 | 2015-12-15 19:39:32 +0100 | [diff] [blame^] | 53 | void nft_unregister_afinfo(struct net *net, struct nft_af_info *afi) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 54 | { |
| 55 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | df05ef8 | 2015-12-15 19:39:32 +0100 | [diff] [blame^] | 56 | __nft_release_afinfo(net, afi); |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 57 | list_del_rcu(&afi->list); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 58 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(nft_unregister_afinfo); |
| 61 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 62 | static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 63 | { |
| 64 | struct nft_af_info *afi; |
| 65 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 66 | list_for_each_entry(afi, &net->nft.af_info, list) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 67 | if (afi->family == family) |
| 68 | return afi; |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 73 | static struct nft_af_info * |
| 74 | nf_tables_afinfo_lookup(struct net *net, int family, bool autoload) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 75 | { |
| 76 | struct nft_af_info *afi; |
| 77 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 78 | afi = nft_afinfo_lookup(net, family); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 79 | if (afi != NULL) |
| 80 | return afi; |
| 81 | #ifdef CONFIG_MODULES |
| 82 | if (autoload) { |
| 83 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 84 | request_module("nft-afinfo-%u", family); |
| 85 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 86 | afi = nft_afinfo_lookup(net, family); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 87 | if (afi != NULL) |
| 88 | return ERR_PTR(-EAGAIN); |
| 89 | } |
| 90 | #endif |
| 91 | return ERR_PTR(-EAFNOSUPPORT); |
| 92 | } |
| 93 | |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 94 | static void nft_ctx_init(struct nft_ctx *ctx, |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 95 | struct net *net, |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 96 | const struct sk_buff *skb, |
| 97 | const struct nlmsghdr *nlh, |
| 98 | struct nft_af_info *afi, |
| 99 | struct nft_table *table, |
| 100 | struct nft_chain *chain, |
| 101 | const struct nlattr * const *nla) |
| 102 | { |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 103 | ctx->net = net; |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 104 | ctx->afi = afi; |
| 105 | ctx->table = table; |
| 106 | ctx->chain = chain; |
| 107 | ctx->nla = nla; |
| 108 | ctx->portid = NETLINK_CB(skb).portid; |
| 109 | ctx->report = nlmsg_report(nlh); |
| 110 | ctx->seq = nlh->nlmsg_seq; |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 113 | static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type, |
| 114 | u32 size) |
Pablo Neira Ayuso | 1081d11 | 2014-04-04 01:24:07 +0200 | [diff] [blame] | 115 | { |
| 116 | struct nft_trans *trans; |
| 117 | |
| 118 | trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL); |
| 119 | if (trans == NULL) |
| 120 | return NULL; |
| 121 | |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 122 | trans->msg_type = msg_type; |
Pablo Neira Ayuso | 1081d11 | 2014-04-04 01:24:07 +0200 | [diff] [blame] | 123 | trans->ctx = *ctx; |
| 124 | |
| 125 | return trans; |
| 126 | } |
| 127 | |
| 128 | static void nft_trans_destroy(struct nft_trans *trans) |
| 129 | { |
| 130 | list_del(&trans->list); |
| 131 | kfree(trans); |
| 132 | } |
| 133 | |
Pablo Neira Ayuso | 835b803 | 2015-06-15 12:12:01 +0200 | [diff] [blame] | 134 | int nft_register_basechain(struct nft_base_chain *basechain, |
| 135 | unsigned int hook_nops) |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 136 | { |
Eric W. Biederman | fd2ecda | 2015-07-10 18:15:44 -0500 | [diff] [blame] | 137 | struct net *net = read_pnet(&basechain->pnet); |
| 138 | |
Pablo Neira Ayuso | 835b803 | 2015-06-15 12:12:01 +0200 | [diff] [blame] | 139 | if (basechain->flags & NFT_BASECHAIN_DISABLED) |
| 140 | return 0; |
| 141 | |
Eric W. Biederman | fd2ecda | 2015-07-10 18:15:44 -0500 | [diff] [blame] | 142 | return nf_register_net_hooks(net, basechain->ops, hook_nops); |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 143 | } |
Pablo Neira Ayuso | 835b803 | 2015-06-15 12:12:01 +0200 | [diff] [blame] | 144 | EXPORT_SYMBOL_GPL(nft_register_basechain); |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 145 | |
Pablo Neira Ayuso | 835b803 | 2015-06-15 12:12:01 +0200 | [diff] [blame] | 146 | void nft_unregister_basechain(struct nft_base_chain *basechain, |
| 147 | unsigned int hook_nops) |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 148 | { |
Eric W. Biederman | fd2ecda | 2015-07-10 18:15:44 -0500 | [diff] [blame] | 149 | struct net *net = read_pnet(&basechain->pnet); |
| 150 | |
Pablo Neira Ayuso | 835b803 | 2015-06-15 12:12:01 +0200 | [diff] [blame] | 151 | if (basechain->flags & NFT_BASECHAIN_DISABLED) |
| 152 | return; |
| 153 | |
Eric W. Biederman | fd2ecda | 2015-07-10 18:15:44 -0500 | [diff] [blame] | 154 | nf_unregister_net_hooks(net, basechain->ops, hook_nops); |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 155 | } |
Pablo Neira Ayuso | 835b803 | 2015-06-15 12:12:01 +0200 | [diff] [blame] | 156 | EXPORT_SYMBOL_GPL(nft_unregister_basechain); |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 157 | |
| 158 | static int nf_tables_register_hooks(const struct nft_table *table, |
| 159 | struct nft_chain *chain, |
| 160 | unsigned int hook_nops) |
| 161 | { |
| 162 | if (table->flags & NFT_TABLE_F_DORMANT || |
| 163 | !(chain->flags & NFT_BASE_CHAIN)) |
| 164 | return 0; |
| 165 | |
| 166 | return nft_register_basechain(nft_base_chain(chain), hook_nops); |
| 167 | } |
| 168 | |
Arturo Borrero | c559879 | 2014-09-02 16:42:23 +0200 | [diff] [blame] | 169 | static void nf_tables_unregister_hooks(const struct nft_table *table, |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 170 | struct nft_chain *chain, |
Arturo Borrero | c559879 | 2014-09-02 16:42:23 +0200 | [diff] [blame] | 171 | unsigned int hook_nops) |
| 172 | { |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 173 | if (table->flags & NFT_TABLE_F_DORMANT || |
| 174 | !(chain->flags & NFT_BASE_CHAIN)) |
| 175 | return; |
| 176 | |
| 177 | nft_unregister_basechain(nft_base_chain(chain), hook_nops); |
Arturo Borrero | c559879 | 2014-09-02 16:42:23 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 180 | /* Internal table flags */ |
| 181 | #define NFT_TABLE_INACTIVE (1 << 15) |
| 182 | |
| 183 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
| 184 | { |
| 185 | struct nft_trans *trans; |
| 186 | |
| 187 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); |
| 188 | if (trans == NULL) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | if (msg_type == NFT_MSG_NEWTABLE) |
| 192 | ctx->table->flags |= NFT_TABLE_INACTIVE; |
| 193 | |
| 194 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int nft_deltable(struct nft_ctx *ctx) |
| 199 | { |
| 200 | int err; |
| 201 | |
| 202 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); |
| 203 | if (err < 0) |
| 204 | return err; |
| 205 | |
| 206 | list_del_rcu(&ctx->table->list); |
| 207 | return err; |
| 208 | } |
| 209 | |
| 210 | static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
| 211 | { |
| 212 | struct nft_trans *trans; |
| 213 | |
| 214 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); |
| 215 | if (trans == NULL) |
| 216 | return -ENOMEM; |
| 217 | |
| 218 | if (msg_type == NFT_MSG_NEWCHAIN) |
| 219 | ctx->chain->flags |= NFT_CHAIN_INACTIVE; |
| 220 | |
| 221 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int nft_delchain(struct nft_ctx *ctx) |
| 226 | { |
| 227 | int err; |
| 228 | |
| 229 | err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
| 230 | if (err < 0) |
| 231 | return err; |
| 232 | |
| 233 | ctx->table->use--; |
| 234 | list_del_rcu(&ctx->chain->list); |
| 235 | |
| 236 | return err; |
| 237 | } |
| 238 | |
| 239 | static inline bool |
| 240 | nft_rule_is_active(struct net *net, const struct nft_rule *rule) |
| 241 | { |
Patrick McHardy | ea4bd99 | 2015-03-25 14:08:49 +0000 | [diff] [blame] | 242 | return (rule->genmask & nft_genmask_cur(net)) == 0; |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static inline int |
| 246 | nft_rule_is_active_next(struct net *net, const struct nft_rule *rule) |
| 247 | { |
Patrick McHardy | ea4bd99 | 2015-03-25 14:08:49 +0000 | [diff] [blame] | 248 | return (rule->genmask & nft_genmask_next(net)) == 0; |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static inline void |
| 252 | nft_rule_activate_next(struct net *net, struct nft_rule *rule) |
| 253 | { |
| 254 | /* Now inactive, will be active in the future */ |
Patrick McHardy | ea4bd99 | 2015-03-25 14:08:49 +0000 | [diff] [blame] | 255 | rule->genmask = nft_genmask_cur(net); |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static inline void |
| 259 | nft_rule_deactivate_next(struct net *net, struct nft_rule *rule) |
| 260 | { |
Patrick McHardy | ea4bd99 | 2015-03-25 14:08:49 +0000 | [diff] [blame] | 261 | rule->genmask = nft_genmask_next(net); |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static inline void nft_rule_clear(struct net *net, struct nft_rule *rule) |
| 265 | { |
Patrick McHardy | ea4bd99 | 2015-03-25 14:08:49 +0000 | [diff] [blame] | 266 | rule->genmask &= ~nft_genmask_next(net); |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static int |
| 270 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) |
| 271 | { |
| 272 | /* You cannot delete the same rule twice */ |
| 273 | if (nft_rule_is_active_next(ctx->net, rule)) { |
| 274 | nft_rule_deactivate_next(ctx->net, rule); |
| 275 | ctx->chain->use--; |
| 276 | return 0; |
| 277 | } |
| 278 | return -ENOENT; |
| 279 | } |
| 280 | |
| 281 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, |
| 282 | struct nft_rule *rule) |
| 283 | { |
| 284 | struct nft_trans *trans; |
| 285 | |
| 286 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); |
| 287 | if (trans == NULL) |
| 288 | return NULL; |
| 289 | |
| 290 | nft_trans_rule(trans) = rule; |
| 291 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| 292 | |
| 293 | return trans; |
| 294 | } |
| 295 | |
| 296 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) |
| 297 | { |
| 298 | struct nft_trans *trans; |
| 299 | int err; |
| 300 | |
| 301 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); |
| 302 | if (trans == NULL) |
| 303 | return -ENOMEM; |
| 304 | |
| 305 | err = nf_tables_delrule_deactivate(ctx, rule); |
| 306 | if (err < 0) { |
| 307 | nft_trans_destroy(trans); |
| 308 | return err; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static int nft_delrule_by_chain(struct nft_ctx *ctx) |
| 315 | { |
| 316 | struct nft_rule *rule; |
| 317 | int err; |
| 318 | |
| 319 | list_for_each_entry(rule, &ctx->chain->rules, list) { |
| 320 | err = nft_delrule(ctx, rule); |
| 321 | if (err < 0) |
| 322 | return err; |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* Internal set flag */ |
| 328 | #define NFT_SET_INACTIVE (1 << 15) |
| 329 | |
| 330 | static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type, |
| 331 | struct nft_set *set) |
| 332 | { |
| 333 | struct nft_trans *trans; |
| 334 | |
| 335 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); |
| 336 | if (trans == NULL) |
| 337 | return -ENOMEM; |
| 338 | |
| 339 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { |
| 340 | nft_trans_set_id(trans) = |
| 341 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); |
| 342 | set->flags |= NFT_SET_INACTIVE; |
| 343 | } |
| 344 | nft_trans_set(trans) = set; |
| 345 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static int nft_delset(struct nft_ctx *ctx, struct nft_set *set) |
| 351 | { |
| 352 | int err; |
| 353 | |
| 354 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); |
| 355 | if (err < 0) |
| 356 | return err; |
| 357 | |
| 358 | list_del_rcu(&set->list); |
| 359 | ctx->table->use--; |
| 360 | |
| 361 | return err; |
| 362 | } |
| 363 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 364 | /* |
| 365 | * Tables |
| 366 | */ |
| 367 | |
| 368 | static struct nft_table *nft_table_lookup(const struct nft_af_info *afi, |
| 369 | const struct nlattr *nla) |
| 370 | { |
| 371 | struct nft_table *table; |
| 372 | |
| 373 | list_for_each_entry(table, &afi->tables, list) { |
| 374 | if (!nla_strcmp(nla, table->name)) |
| 375 | return table; |
| 376 | } |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi, |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 381 | const struct nlattr *nla) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 382 | { |
| 383 | struct nft_table *table; |
| 384 | |
| 385 | if (nla == NULL) |
| 386 | return ERR_PTR(-EINVAL); |
| 387 | |
| 388 | table = nft_table_lookup(afi, nla); |
| 389 | if (table != NULL) |
| 390 | return table; |
| 391 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 392 | return ERR_PTR(-ENOENT); |
| 393 | } |
| 394 | |
| 395 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
| 396 | { |
| 397 | return ++table->hgenerator; |
| 398 | } |
| 399 | |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 400 | static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX]; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 401 | |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 402 | static const struct nf_chain_type * |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 403 | __nf_tables_chain_type_lookup(int family, const struct nlattr *nla) |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 404 | { |
| 405 | int i; |
| 406 | |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 407 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 408 | if (chain_type[family][i] != NULL && |
| 409 | !nla_strcmp(nla, chain_type[family][i]->name)) |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 410 | return chain_type[family][i]; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 411 | } |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 412 | return NULL; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 415 | static const struct nf_chain_type * |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 416 | nf_tables_chain_type_lookup(const struct nft_af_info *afi, |
| 417 | const struct nlattr *nla, |
| 418 | bool autoload) |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 419 | { |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 420 | const struct nf_chain_type *type; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 421 | |
| 422 | type = __nf_tables_chain_type_lookup(afi->family, nla); |
Patrick McHardy | 93b0806 | 2014-01-09 18:42:36 +0000 | [diff] [blame] | 423 | if (type != NULL) |
| 424 | return type; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 425 | #ifdef CONFIG_MODULES |
Patrick McHardy | 93b0806 | 2014-01-09 18:42:36 +0000 | [diff] [blame] | 426 | if (autoload) { |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 427 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | 2fec6bb | 2014-03-31 12:26:39 +0200 | [diff] [blame] | 428 | request_module("nft-chain-%u-%.*s", afi->family, |
| 429 | nla_len(nla), (const char *)nla_data(nla)); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 430 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| 431 | type = __nf_tables_chain_type_lookup(afi->family, nla); |
Patrick McHardy | 93b0806 | 2014-01-09 18:42:36 +0000 | [diff] [blame] | 432 | if (type != NULL) |
| 433 | return ERR_PTR(-EAGAIN); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 434 | } |
| 435 | #endif |
Patrick McHardy | 93b0806 | 2014-01-09 18:42:36 +0000 | [diff] [blame] | 436 | return ERR_PTR(-ENOENT); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 439 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
Pablo Neira Ayuso | 1cae565 | 2015-03-05 15:05:36 +0100 | [diff] [blame] | 440 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
| 441 | .len = NFT_TABLE_MAXNAMELEN - 1 }, |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 442 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 443 | }; |
| 444 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 445 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
| 446 | u32 portid, u32 seq, int event, u32 flags, |
| 447 | int family, const struct nft_table *table) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 448 | { |
| 449 | struct nlmsghdr *nlh; |
| 450 | struct nfgenmsg *nfmsg; |
| 451 | |
| 452 | event |= NFNL_SUBSYS_NFTABLES << 8; |
| 453 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
| 454 | if (nlh == NULL) |
| 455 | goto nla_put_failure; |
| 456 | |
| 457 | nfmsg = nlmsg_data(nlh); |
| 458 | nfmsg->nfgen_family = family; |
| 459 | nfmsg->version = NFNETLINK_V0; |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 460 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 461 | |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 462 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
Tomasz Bursztyka | d8bcc768 | 2013-12-12 15:00:42 +0200 | [diff] [blame] | 463 | nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) || |
| 464 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use))) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 465 | goto nla_put_failure; |
| 466 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 467 | nlmsg_end(skb, nlh); |
| 468 | return 0; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 469 | |
| 470 | nla_put_failure: |
| 471 | nlmsg_trim(skb, nlh); |
| 472 | return -1; |
| 473 | } |
| 474 | |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 475 | static int nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 476 | { |
| 477 | struct sk_buff *skb; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 478 | int err; |
| 479 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 480 | if (!ctx->report && |
| 481 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 482 | return 0; |
| 483 | |
| 484 | err = -ENOBUFS; |
| 485 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 486 | if (skb == NULL) |
| 487 | goto err; |
| 488 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 489 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
| 490 | event, 0, ctx->afi->family, ctx->table); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 491 | if (err < 0) { |
| 492 | kfree_skb(skb); |
| 493 | goto err; |
| 494 | } |
| 495 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 496 | err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| 497 | ctx->report, GFP_KERNEL); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 498 | err: |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 499 | if (err < 0) { |
| 500 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| 501 | err); |
| 502 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 503 | return err; |
| 504 | } |
| 505 | |
| 506 | static int nf_tables_dump_tables(struct sk_buff *skb, |
| 507 | struct netlink_callback *cb) |
| 508 | { |
| 509 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
| 510 | const struct nft_af_info *afi; |
| 511 | const struct nft_table *table; |
| 512 | unsigned int idx = 0, s_idx = cb->args[0]; |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 513 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 514 | int family = nfmsg->nfgen_family; |
| 515 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 516 | rcu_read_lock(); |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 517 | cb->seq = net->nft.base_seq; |
| 518 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 519 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 520 | if (family != NFPROTO_UNSPEC && family != afi->family) |
| 521 | continue; |
| 522 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 523 | list_for_each_entry_rcu(table, &afi->tables, list) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 524 | if (idx < s_idx) |
| 525 | goto cont; |
| 526 | if (idx > s_idx) |
| 527 | memset(&cb->args[1], 0, |
| 528 | sizeof(cb->args) - sizeof(cb->args[0])); |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 529 | if (nf_tables_fill_table_info(skb, net, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 530 | NETLINK_CB(cb->skb).portid, |
| 531 | cb->nlh->nlmsg_seq, |
| 532 | NFT_MSG_NEWTABLE, |
| 533 | NLM_F_MULTI, |
| 534 | afi->family, table) < 0) |
| 535 | goto done; |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 536 | |
| 537 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 538 | cont: |
| 539 | idx++; |
| 540 | } |
| 541 | } |
| 542 | done: |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 543 | rcu_read_unlock(); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 544 | cb->args[0] = idx; |
| 545 | return skb->len; |
| 546 | } |
| 547 | |
| 548 | static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb, |
| 549 | const struct nlmsghdr *nlh, |
| 550 | const struct nlattr * const nla[]) |
| 551 | { |
| 552 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| 553 | const struct nft_af_info *afi; |
| 554 | const struct nft_table *table; |
| 555 | struct sk_buff *skb2; |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 556 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 557 | int family = nfmsg->nfgen_family; |
| 558 | int err; |
| 559 | |
| 560 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 561 | struct netlink_dump_control c = { |
| 562 | .dump = nf_tables_dump_tables, |
| 563 | }; |
| 564 | return netlink_dump_start(nlsk, skb, nlh, &c); |
| 565 | } |
| 566 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 567 | afi = nf_tables_afinfo_lookup(net, family, false); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 568 | if (IS_ERR(afi)) |
| 569 | return PTR_ERR(afi); |
| 570 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 571 | table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 572 | if (IS_ERR(table)) |
| 573 | return PTR_ERR(table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 574 | if (table->flags & NFT_TABLE_INACTIVE) |
| 575 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 576 | |
| 577 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 578 | if (!skb2) |
| 579 | return -ENOMEM; |
| 580 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 581 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 582 | nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0, |
| 583 | family, table); |
| 584 | if (err < 0) |
| 585 | goto err; |
| 586 | |
| 587 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); |
| 588 | |
| 589 | err: |
| 590 | kfree_skb(skb2); |
| 591 | return err; |
| 592 | } |
| 593 | |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 594 | static int nf_tables_table_enable(const struct nft_af_info *afi, |
| 595 | struct nft_table *table) |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 596 | { |
| 597 | struct nft_chain *chain; |
| 598 | int err, i = 0; |
| 599 | |
| 600 | list_for_each_entry(chain, &table->chains, list) { |
Pablo Neira Ayuso | d201297 | 2013-12-27 10:44:23 +0100 | [diff] [blame] | 601 | if (!(chain->flags & NFT_BASE_CHAIN)) |
| 602 | continue; |
| 603 | |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 604 | err = nft_register_basechain(nft_base_chain(chain), afi->nops); |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 605 | if (err < 0) |
| 606 | goto err; |
| 607 | |
| 608 | i++; |
| 609 | } |
| 610 | return 0; |
| 611 | err: |
| 612 | list_for_each_entry(chain, &table->chains, list) { |
Pablo Neira Ayuso | d201297 | 2013-12-27 10:44:23 +0100 | [diff] [blame] | 613 | if (!(chain->flags & NFT_BASE_CHAIN)) |
| 614 | continue; |
| 615 | |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 616 | if (i-- <= 0) |
| 617 | break; |
| 618 | |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 619 | nft_unregister_basechain(nft_base_chain(chain), afi->nops); |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 620 | } |
| 621 | return err; |
| 622 | } |
| 623 | |
Pablo Neira Ayuso | f75edf5 | 2014-03-30 14:04:52 +0200 | [diff] [blame] | 624 | static void nf_tables_table_disable(const struct nft_af_info *afi, |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 625 | struct nft_table *table) |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 626 | { |
| 627 | struct nft_chain *chain; |
| 628 | |
Pablo Neira Ayuso | d201297 | 2013-12-27 10:44:23 +0100 | [diff] [blame] | 629 | list_for_each_entry(chain, &table->chains, list) { |
| 630 | if (chain->flags & NFT_BASE_CHAIN) |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 631 | nft_unregister_basechain(nft_base_chain(chain), |
| 632 | afi->nops); |
Pablo Neira Ayuso | d201297 | 2013-12-27 10:44:23 +0100 | [diff] [blame] | 633 | } |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 636 | static int nf_tables_updtable(struct nft_ctx *ctx) |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 637 | { |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 638 | struct nft_trans *trans; |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 639 | u32 flags; |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 640 | int ret = 0; |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 641 | |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 642 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
| 643 | return 0; |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 644 | |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 645 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
| 646 | if (flags & ~NFT_TABLE_F_DORMANT) |
| 647 | return -EINVAL; |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 648 | |
Pablo Neira Ayuso | 63283dd | 2014-06-27 18:51:39 +0200 | [diff] [blame] | 649 | if (flags == ctx->table->flags) |
| 650 | return 0; |
| 651 | |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 652 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
| 653 | sizeof(struct nft_trans_table)); |
| 654 | if (trans == NULL) |
| 655 | return -ENOMEM; |
| 656 | |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 657 | if ((flags & NFT_TABLE_F_DORMANT) && |
| 658 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 659 | nft_trans_table_enable(trans) = false; |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 660 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
| 661 | ctx->table->flags & NFT_TABLE_F_DORMANT) { |
| 662 | ret = nf_tables_table_enable(ctx->afi, ctx->table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 663 | if (ret >= 0) { |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 664 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 665 | nft_trans_table_enable(trans) = true; |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 666 | } |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 667 | } |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 668 | if (ret < 0) |
| 669 | goto err; |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 670 | |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 671 | nft_trans_table_update(trans) = true; |
| 672 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| 673 | return 0; |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 674 | err: |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 675 | nft_trans_destroy(trans); |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 676 | return ret; |
| 677 | } |
| 678 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 679 | static int nf_tables_newtable(struct net *net, struct sock *nlsk, |
| 680 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 681 | const struct nlattr * const nla[]) |
| 682 | { |
| 683 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| 684 | const struct nlattr *name; |
| 685 | struct nft_af_info *afi; |
| 686 | struct nft_table *table; |
| 687 | int family = nfmsg->nfgen_family; |
Patrick McHardy | c5c1f97 | 2014-01-09 18:42:39 +0000 | [diff] [blame] | 688 | u32 flags = 0; |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 689 | struct nft_ctx ctx; |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 690 | int err; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 691 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 692 | afi = nf_tables_afinfo_lookup(net, family, true); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 693 | if (IS_ERR(afi)) |
| 694 | return PTR_ERR(afi); |
| 695 | |
| 696 | name = nla[NFTA_TABLE_NAME]; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 697 | table = nf_tables_table_lookup(afi, name); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 698 | if (IS_ERR(table)) { |
| 699 | if (PTR_ERR(table) != -ENOENT) |
| 700 | return PTR_ERR(table); |
| 701 | table = NULL; |
| 702 | } |
| 703 | |
| 704 | if (table != NULL) { |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 705 | if (table->flags & NFT_TABLE_INACTIVE) |
| 706 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 707 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 708 | return -EEXIST; |
| 709 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 710 | return -EOPNOTSUPP; |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 711 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 712 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); |
Pablo Neira Ayuso | e1aaca9 | 2014-03-30 14:04:53 +0200 | [diff] [blame] | 713 | return nf_tables_updtable(&ctx); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Patrick McHardy | c5c1f97 | 2014-01-09 18:42:39 +0000 | [diff] [blame] | 716 | if (nla[NFTA_TABLE_FLAGS]) { |
| 717 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); |
| 718 | if (flags & ~NFT_TABLE_F_DORMANT) |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 722 | err = -EAFNOSUPPORT; |
Patrick McHardy | 7047f9d | 2014-01-09 18:42:40 +0000 | [diff] [blame] | 723 | if (!try_module_get(afi->owner)) |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 724 | goto err1; |
Patrick McHardy | 7047f9d | 2014-01-09 18:42:40 +0000 | [diff] [blame] | 725 | |
Pablo Neira Ayuso | ffdb210 | 2015-03-17 19:53:23 +0100 | [diff] [blame] | 726 | err = -ENOMEM; |
Pablo Neira Ayuso | 1cae565 | 2015-03-05 15:05:36 +0100 | [diff] [blame] | 727 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
Pablo Neira Ayuso | ffdb210 | 2015-03-17 19:53:23 +0100 | [diff] [blame] | 728 | if (table == NULL) |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 729 | goto err2; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 730 | |
Pablo Neira Ayuso | 1cae565 | 2015-03-05 15:05:36 +0100 | [diff] [blame] | 731 | nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 732 | INIT_LIST_HEAD(&table->chains); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 733 | INIT_LIST_HEAD(&table->sets); |
Patrick McHardy | c5c1f97 | 2014-01-09 18:42:39 +0000 | [diff] [blame] | 734 | table->flags = flags; |
Pablo Neira Ayuso | 9ddf632 | 2013-10-10 13:26:33 +0200 | [diff] [blame] | 735 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 736 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 737 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
Pablo Neira Ayuso | ffdb210 | 2015-03-17 19:53:23 +0100 | [diff] [blame] | 738 | if (err < 0) |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 739 | goto err3; |
Pablo Neira Ayuso | ffdb210 | 2015-03-17 19:53:23 +0100 | [diff] [blame] | 740 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 741 | list_add_tail_rcu(&table->list, &afi->tables); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 742 | return 0; |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 743 | err3: |
Pablo Neira Ayuso | ffdb210 | 2015-03-17 19:53:23 +0100 | [diff] [blame] | 744 | kfree(table); |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 745 | err2: |
Pablo Neira Ayuso | ffdb210 | 2015-03-17 19:53:23 +0100 | [diff] [blame] | 746 | module_put(afi->owner); |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 747 | err1: |
Pablo Neira Ayuso | ffdb210 | 2015-03-17 19:53:23 +0100 | [diff] [blame] | 748 | return err; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 749 | } |
| 750 | |
Arturo Borrero | b9ac12e | 2014-09-02 16:42:26 +0200 | [diff] [blame] | 751 | static int nft_flush_table(struct nft_ctx *ctx) |
| 752 | { |
| 753 | int err; |
| 754 | struct nft_chain *chain, *nc; |
| 755 | struct nft_set *set, *ns; |
| 756 | |
Pablo Neira Ayuso | a2f18db | 2015-01-04 15:14:22 +0100 | [diff] [blame] | 757 | list_for_each_entry(chain, &ctx->table->chains, list) { |
Arturo Borrero | b9ac12e | 2014-09-02 16:42:26 +0200 | [diff] [blame] | 758 | ctx->chain = chain; |
| 759 | |
| 760 | err = nft_delrule_by_chain(ctx); |
| 761 | if (err < 0) |
| 762 | goto out; |
Arturo Borrero | b9ac12e | 2014-09-02 16:42:26 +0200 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { |
| 766 | if (set->flags & NFT_SET_ANONYMOUS && |
| 767 | !list_empty(&set->bindings)) |
| 768 | continue; |
| 769 | |
| 770 | err = nft_delset(ctx, set); |
| 771 | if (err < 0) |
| 772 | goto out; |
| 773 | } |
| 774 | |
Pablo Neira Ayuso | a2f18db | 2015-01-04 15:14:22 +0100 | [diff] [blame] | 775 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
| 776 | ctx->chain = chain; |
| 777 | |
| 778 | err = nft_delchain(ctx); |
| 779 | if (err < 0) |
| 780 | goto out; |
| 781 | } |
| 782 | |
Arturo Borrero | b9ac12e | 2014-09-02 16:42:26 +0200 | [diff] [blame] | 783 | err = nft_deltable(ctx); |
| 784 | out: |
| 785 | return err; |
| 786 | } |
| 787 | |
| 788 | static int nft_flush(struct nft_ctx *ctx, int family) |
| 789 | { |
| 790 | struct nft_af_info *afi; |
| 791 | struct nft_table *table, *nt; |
| 792 | const struct nlattr * const *nla = ctx->nla; |
| 793 | int err = 0; |
| 794 | |
| 795 | list_for_each_entry(afi, &ctx->net->nft.af_info, list) { |
| 796 | if (family != AF_UNSPEC && afi->family != family) |
| 797 | continue; |
| 798 | |
| 799 | ctx->afi = afi; |
| 800 | list_for_each_entry_safe(table, nt, &afi->tables, list) { |
| 801 | if (nla[NFTA_TABLE_NAME] && |
| 802 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) |
| 803 | continue; |
| 804 | |
| 805 | ctx->table = table; |
| 806 | |
| 807 | err = nft_flush_table(ctx); |
| 808 | if (err < 0) |
| 809 | goto out; |
| 810 | } |
| 811 | } |
| 812 | out: |
| 813 | return err; |
| 814 | } |
| 815 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 816 | static int nf_tables_deltable(struct net *net, struct sock *nlsk, |
| 817 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 818 | const struct nlattr * const nla[]) |
| 819 | { |
| 820 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| 821 | struct nft_af_info *afi; |
| 822 | struct nft_table *table; |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 823 | int family = nfmsg->nfgen_family; |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 824 | struct nft_ctx ctx; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 825 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 826 | nft_ctx_init(&ctx, net, skb, nlh, NULL, NULL, NULL, nla); |
Arturo Borrero | b9ac12e | 2014-09-02 16:42:26 +0200 | [diff] [blame] | 827 | if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL) |
| 828 | return nft_flush(&ctx, family); |
| 829 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 830 | afi = nf_tables_afinfo_lookup(net, family, false); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 831 | if (IS_ERR(afi)) |
| 832 | return PTR_ERR(afi); |
| 833 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 834 | table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 835 | if (IS_ERR(table)) |
| 836 | return PTR_ERR(table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 837 | if (table->flags & NFT_TABLE_INACTIVE) |
| 838 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 839 | |
Arturo Borrero | b9ac12e | 2014-09-02 16:42:26 +0200 | [diff] [blame] | 840 | ctx.afi = afi; |
| 841 | ctx.table = table; |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 842 | |
Arturo Borrero | b9ac12e | 2014-09-02 16:42:26 +0200 | [diff] [blame] | 843 | return nft_flush_table(&ctx); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 846 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
| 847 | { |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 848 | BUG_ON(ctx->table->use > 0); |
| 849 | |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 850 | kfree(ctx->table); |
| 851 | module_put(ctx->afi->owner); |
| 852 | } |
| 853 | |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 854 | int nft_register_chain_type(const struct nf_chain_type *ctype) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 855 | { |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 856 | int err = 0; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 857 | |
| 858 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 859 | if (chain_type[ctype->family][ctype->type] != NULL) { |
| 860 | err = -EBUSY; |
| 861 | goto out; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 862 | } |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 863 | chain_type[ctype->family][ctype->type] = ctype; |
| 864 | out: |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 865 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 866 | return err; |
| 867 | } |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 868 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 869 | |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 870 | void nft_unregister_chain_type(const struct nf_chain_type *ctype) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 871 | { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 872 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 873 | chain_type[ctype->family][ctype->type] = NULL; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 874 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 875 | } |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 876 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 877 | |
| 878 | /* |
| 879 | * Chains |
| 880 | */ |
| 881 | |
| 882 | static struct nft_chain * |
| 883 | nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle) |
| 884 | { |
| 885 | struct nft_chain *chain; |
| 886 | |
| 887 | list_for_each_entry(chain, &table->chains, list) { |
| 888 | if (chain->handle == handle) |
| 889 | return chain; |
| 890 | } |
| 891 | |
| 892 | return ERR_PTR(-ENOENT); |
| 893 | } |
| 894 | |
| 895 | static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table, |
| 896 | const struct nlattr *nla) |
| 897 | { |
| 898 | struct nft_chain *chain; |
| 899 | |
| 900 | if (nla == NULL) |
| 901 | return ERR_PTR(-EINVAL); |
| 902 | |
| 903 | list_for_each_entry(chain, &table->chains, list) { |
| 904 | if (!nla_strcmp(nla, chain->name)) |
| 905 | return chain; |
| 906 | } |
| 907 | |
| 908 | return ERR_PTR(-ENOENT); |
| 909 | } |
| 910 | |
| 911 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { |
| 912 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING }, |
| 913 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
| 914 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, |
| 915 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, |
| 916 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 917 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
Pablo Neira | 4c1f781 | 2014-03-31 17:43:47 +0200 | [diff] [blame] | 918 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING }, |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 919 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 920 | }; |
| 921 | |
| 922 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { |
| 923 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, |
| 924 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 925 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
| 926 | .len = IFNAMSIZ - 1 }, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 927 | }; |
| 928 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 929 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
| 930 | { |
| 931 | struct nft_stats *cpu_stats, total; |
| 932 | struct nlattr *nest; |
Eric Dumazet | ce355e2 | 2014-07-09 15:14:06 +0200 | [diff] [blame] | 933 | unsigned int seq; |
| 934 | u64 pkts, bytes; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 935 | int cpu; |
| 936 | |
| 937 | memset(&total, 0, sizeof(total)); |
| 938 | for_each_possible_cpu(cpu) { |
| 939 | cpu_stats = per_cpu_ptr(stats, cpu); |
Eric Dumazet | ce355e2 | 2014-07-09 15:14:06 +0200 | [diff] [blame] | 940 | do { |
| 941 | seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
| 942 | pkts = cpu_stats->pkts; |
| 943 | bytes = cpu_stats->bytes; |
| 944 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); |
| 945 | total.pkts += pkts; |
| 946 | total.bytes += bytes; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 947 | } |
| 948 | nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS); |
| 949 | if (nest == NULL) |
| 950 | goto nla_put_failure; |
| 951 | |
| 952 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) || |
| 953 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes))) |
| 954 | goto nla_put_failure; |
| 955 | |
| 956 | nla_nest_end(skb, nest); |
| 957 | return 0; |
| 958 | |
| 959 | nla_put_failure: |
| 960 | return -ENOSPC; |
| 961 | } |
| 962 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 963 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
| 964 | u32 portid, u32 seq, int event, u32 flags, |
| 965 | int family, const struct nft_table *table, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 966 | const struct nft_chain *chain) |
| 967 | { |
| 968 | struct nlmsghdr *nlh; |
| 969 | struct nfgenmsg *nfmsg; |
| 970 | |
| 971 | event |= NFNL_SUBSYS_NFTABLES << 8; |
| 972 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
| 973 | if (nlh == NULL) |
| 974 | goto nla_put_failure; |
| 975 | |
| 976 | nfmsg = nlmsg_data(nlh); |
| 977 | nfmsg->nfgen_family = family; |
| 978 | nfmsg->version = NFNETLINK_V0; |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 979 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 980 | |
| 981 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) |
| 982 | goto nla_put_failure; |
| 983 | if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle))) |
| 984 | goto nla_put_failure; |
| 985 | if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) |
| 986 | goto nla_put_failure; |
| 987 | |
| 988 | if (chain->flags & NFT_BASE_CHAIN) { |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 989 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 990 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 991 | struct nlattr *nest; |
| 992 | |
| 993 | nest = nla_nest_start(skb, NFTA_CHAIN_HOOK); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 994 | if (nest == NULL) |
| 995 | goto nla_put_failure; |
| 996 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) |
| 997 | goto nla_put_failure; |
| 998 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) |
| 999 | goto nla_put_failure; |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1000 | if (basechain->dev_name[0] && |
| 1001 | nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name)) |
| 1002 | goto nla_put_failure; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1003 | nla_nest_end(skb, nest); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1004 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1005 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
| 1006 | htonl(basechain->policy))) |
| 1007 | goto nla_put_failure; |
| 1008 | |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 1009 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
| 1010 | goto nla_put_failure; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1011 | |
| 1012 | if (nft_dump_stats(skb, nft_base_chain(chain)->stats)) |
| 1013 | goto nla_put_failure; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1016 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
| 1017 | goto nla_put_failure; |
| 1018 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1019 | nlmsg_end(skb, nlh); |
| 1020 | return 0; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1021 | |
| 1022 | nla_put_failure: |
| 1023 | nlmsg_trim(skb, nlh); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 1027 | static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1028 | { |
| 1029 | struct sk_buff *skb; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1030 | int err; |
| 1031 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 1032 | if (!ctx->report && |
| 1033 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1034 | return 0; |
| 1035 | |
| 1036 | err = -ENOBUFS; |
| 1037 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1038 | if (skb == NULL) |
| 1039 | goto err; |
| 1040 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 1041 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
| 1042 | event, 0, ctx->afi->family, ctx->table, |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 1043 | ctx->chain); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1044 | if (err < 0) { |
| 1045 | kfree_skb(skb); |
| 1046 | goto err; |
| 1047 | } |
| 1048 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 1049 | err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| 1050 | ctx->report, GFP_KERNEL); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1051 | err: |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 1052 | if (err < 0) { |
| 1053 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| 1054 | err); |
| 1055 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1056 | return err; |
| 1057 | } |
| 1058 | |
| 1059 | static int nf_tables_dump_chains(struct sk_buff *skb, |
| 1060 | struct netlink_callback *cb) |
| 1061 | { |
| 1062 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
| 1063 | const struct nft_af_info *afi; |
| 1064 | const struct nft_table *table; |
| 1065 | const struct nft_chain *chain; |
| 1066 | unsigned int idx = 0, s_idx = cb->args[0]; |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 1067 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1068 | int family = nfmsg->nfgen_family; |
| 1069 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1070 | rcu_read_lock(); |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 1071 | cb->seq = net->nft.base_seq; |
| 1072 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1073 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1074 | if (family != NFPROTO_UNSPEC && family != afi->family) |
| 1075 | continue; |
| 1076 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1077 | list_for_each_entry_rcu(table, &afi->tables, list) { |
| 1078 | list_for_each_entry_rcu(chain, &table->chains, list) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1079 | if (idx < s_idx) |
| 1080 | goto cont; |
| 1081 | if (idx > s_idx) |
| 1082 | memset(&cb->args[1], 0, |
| 1083 | sizeof(cb->args) - sizeof(cb->args[0])); |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 1084 | if (nf_tables_fill_chain_info(skb, net, |
| 1085 | NETLINK_CB(cb->skb).portid, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1086 | cb->nlh->nlmsg_seq, |
| 1087 | NFT_MSG_NEWCHAIN, |
| 1088 | NLM_F_MULTI, |
| 1089 | afi->family, table, chain) < 0) |
| 1090 | goto done; |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 1091 | |
| 1092 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1093 | cont: |
| 1094 | idx++; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | done: |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1099 | rcu_read_unlock(); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1100 | cb->args[0] = idx; |
| 1101 | return skb->len; |
| 1102 | } |
| 1103 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1104 | static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb, |
| 1105 | const struct nlmsghdr *nlh, |
| 1106 | const struct nlattr * const nla[]) |
| 1107 | { |
| 1108 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| 1109 | const struct nft_af_info *afi; |
| 1110 | const struct nft_table *table; |
| 1111 | const struct nft_chain *chain; |
| 1112 | struct sk_buff *skb2; |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 1113 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1114 | int family = nfmsg->nfgen_family; |
| 1115 | int err; |
| 1116 | |
| 1117 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 1118 | struct netlink_dump_control c = { |
| 1119 | .dump = nf_tables_dump_chains, |
| 1120 | }; |
| 1121 | return netlink_dump_start(nlsk, skb, nlh, &c); |
| 1122 | } |
| 1123 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 1124 | afi = nf_tables_afinfo_lookup(net, family, false); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1125 | if (IS_ERR(afi)) |
| 1126 | return PTR_ERR(afi); |
| 1127 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1128 | table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1129 | if (IS_ERR(table)) |
| 1130 | return PTR_ERR(table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 1131 | if (table->flags & NFT_TABLE_INACTIVE) |
| 1132 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1133 | |
| 1134 | chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]); |
| 1135 | if (IS_ERR(chain)) |
| 1136 | return PTR_ERR(chain); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1137 | if (chain->flags & NFT_CHAIN_INACTIVE) |
| 1138 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1139 | |
| 1140 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1141 | if (!skb2) |
| 1142 | return -ENOMEM; |
| 1143 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 1144 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1145 | nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0, |
| 1146 | family, table, chain); |
| 1147 | if (err < 0) |
| 1148 | goto err; |
| 1149 | |
| 1150 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); |
| 1151 | |
| 1152 | err: |
| 1153 | kfree_skb(skb2); |
| 1154 | return err; |
| 1155 | } |
| 1156 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1157 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
| 1158 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, |
| 1159 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, |
| 1160 | }; |
| 1161 | |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1162 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1163 | { |
| 1164 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; |
| 1165 | struct nft_stats __percpu *newstats; |
| 1166 | struct nft_stats *stats; |
| 1167 | int err; |
| 1168 | |
| 1169 | err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy); |
| 1170 | if (err < 0) |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1171 | return ERR_PTR(err); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1172 | |
| 1173 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1174 | return ERR_PTR(-EINVAL); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1175 | |
Eric Dumazet | ce355e2 | 2014-07-09 15:14:06 +0200 | [diff] [blame] | 1176 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1177 | if (newstats == NULL) |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1178 | return ERR_PTR(-ENOMEM); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1179 | |
| 1180 | /* Restore old counters on this cpu, no problem. Per-cpu statistics |
| 1181 | * are not exposed to userspace. |
| 1182 | */ |
Pablo Neira Ayuso | e8781f7 | 2015-01-21 18:04:18 +0100 | [diff] [blame] | 1183 | preempt_disable(); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1184 | stats = this_cpu_ptr(newstats); |
| 1185 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); |
| 1186 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); |
Pablo Neira Ayuso | e8781f7 | 2015-01-21 18:04:18 +0100 | [diff] [blame] | 1187 | preempt_enable(); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1188 | |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1189 | return newstats; |
| 1190 | } |
| 1191 | |
| 1192 | static void nft_chain_stats_replace(struct nft_base_chain *chain, |
| 1193 | struct nft_stats __percpu *newstats) |
| 1194 | { |
Pablo Neira Ayuso | b88825d | 2014-08-05 17:25:59 +0200 | [diff] [blame] | 1195 | if (newstats == NULL) |
| 1196 | return; |
| 1197 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1198 | if (chain->stats) { |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1199 | struct nft_stats __percpu *oldstats = |
Patrick McHardy | 67a8fc2 | 2014-02-18 18:06:49 +0000 | [diff] [blame] | 1200 | nft_dereference(chain->stats); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1201 | |
| 1202 | rcu_assign_pointer(chain->stats, newstats); |
| 1203 | synchronize_rcu(); |
| 1204 | free_percpu(oldstats); |
| 1205 | } else |
| 1206 | rcu_assign_pointer(chain->stats, newstats); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1207 | } |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1208 | |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1209 | static void nf_tables_chain_destroy(struct nft_chain *chain) |
| 1210 | { |
| 1211 | BUG_ON(chain->use > 0); |
| 1212 | |
| 1213 | if (chain->flags & NFT_BASE_CHAIN) { |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1214 | struct nft_base_chain *basechain = nft_base_chain(chain); |
| 1215 | |
| 1216 | module_put(basechain->type->owner); |
| 1217 | free_percpu(basechain->stats); |
| 1218 | if (basechain->ops[0].dev != NULL) |
| 1219 | dev_put(basechain->ops[0].dev); |
| 1220 | kfree(basechain); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1221 | } else { |
| 1222 | kfree(chain); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 1226 | static int nf_tables_newchain(struct net *net, struct sock *nlsk, |
| 1227 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1228 | const struct nlattr * const nla[]) |
| 1229 | { |
| 1230 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| 1231 | const struct nlattr * uninitialized_var(name); |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 1232 | struct nft_af_info *afi; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1233 | struct nft_table *table; |
| 1234 | struct nft_chain *chain; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1235 | struct nft_base_chain *basechain = NULL; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1236 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; |
| 1237 | int family = nfmsg->nfgen_family; |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1238 | struct net_device *dev = NULL; |
Patrick McHardy | 57de2a0 | 2014-01-09 18:42:31 +0000 | [diff] [blame] | 1239 | u8 policy = NF_ACCEPT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1240 | u64 handle = 0; |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 1241 | unsigned int i; |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1242 | struct nft_stats __percpu *stats; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1243 | int err; |
| 1244 | bool create; |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1245 | struct nft_ctx ctx; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1246 | |
| 1247 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; |
| 1248 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 1249 | afi = nf_tables_afinfo_lookup(net, family, true); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1250 | if (IS_ERR(afi)) |
| 1251 | return PTR_ERR(afi); |
| 1252 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1253 | table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1254 | if (IS_ERR(table)) |
| 1255 | return PTR_ERR(table); |
| 1256 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1257 | chain = NULL; |
| 1258 | name = nla[NFTA_CHAIN_NAME]; |
| 1259 | |
| 1260 | if (nla[NFTA_CHAIN_HANDLE]) { |
| 1261 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); |
| 1262 | chain = nf_tables_chain_lookup_byhandle(table, handle); |
| 1263 | if (IS_ERR(chain)) |
| 1264 | return PTR_ERR(chain); |
| 1265 | } else { |
| 1266 | chain = nf_tables_chain_lookup(table, name); |
| 1267 | if (IS_ERR(chain)) { |
| 1268 | if (PTR_ERR(chain) != -ENOENT) |
| 1269 | return PTR_ERR(chain); |
| 1270 | chain = NULL; |
| 1271 | } |
| 1272 | } |
| 1273 | |
Patrick McHardy | 57de2a0 | 2014-01-09 18:42:31 +0000 | [diff] [blame] | 1274 | if (nla[NFTA_CHAIN_POLICY]) { |
| 1275 | if ((chain != NULL && |
Pablo Neira Ayuso | d6b6cb1 | 2015-03-17 13:21:42 +0100 | [diff] [blame] | 1276 | !(chain->flags & NFT_BASE_CHAIN))) |
| 1277 | return -EOPNOTSUPP; |
| 1278 | |
| 1279 | if (chain == NULL && |
Patrick McHardy | 57de2a0 | 2014-01-09 18:42:31 +0000 | [diff] [blame] | 1280 | nla[NFTA_CHAIN_HOOK] == NULL) |
| 1281 | return -EOPNOTSUPP; |
| 1282 | |
Pablo Neira Ayuso | 8f46df1 | 2014-01-10 15:11:25 +0100 | [diff] [blame] | 1283 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
Patrick McHardy | 57de2a0 | 2014-01-09 18:42:31 +0000 | [diff] [blame] | 1284 | switch (policy) { |
| 1285 | case NF_DROP: |
| 1286 | case NF_ACCEPT: |
| 1287 | break; |
| 1288 | default: |
| 1289 | return -EINVAL; |
| 1290 | } |
| 1291 | } |
| 1292 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1293 | if (chain != NULL) { |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1294 | struct nft_stats *stats = NULL; |
| 1295 | struct nft_trans *trans; |
| 1296 | |
| 1297 | if (chain->flags & NFT_CHAIN_INACTIVE) |
| 1298 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1299 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 1300 | return -EEXIST; |
| 1301 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 1302 | return -EOPNOTSUPP; |
| 1303 | |
| 1304 | if (nla[NFTA_CHAIN_HANDLE] && name && |
| 1305 | !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]))) |
| 1306 | return -EEXIST; |
| 1307 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1308 | if (nla[NFTA_CHAIN_COUNTERS]) { |
| 1309 | if (!(chain->flags & NFT_BASE_CHAIN)) |
| 1310 | return -EOPNOTSUPP; |
| 1311 | |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1312 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); |
| 1313 | if (IS_ERR(stats)) |
| 1314 | return PTR_ERR(stats); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 1317 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1318 | trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN, |
| 1319 | sizeof(struct nft_trans_chain)); |
Pablo Neira Ayuso | f5553c1 | 2015-01-29 19:08:09 +0100 | [diff] [blame] | 1320 | if (trans == NULL) { |
| 1321 | free_percpu(stats); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1322 | return -ENOMEM; |
Pablo Neira Ayuso | f5553c1 | 2015-01-29 19:08:09 +0100 | [diff] [blame] | 1323 | } |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1324 | |
| 1325 | nft_trans_chain_stats(trans) = stats; |
| 1326 | nft_trans_chain_update(trans) = true; |
| 1327 | |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1328 | if (nla[NFTA_CHAIN_POLICY]) |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1329 | nft_trans_chain_policy(trans) = policy; |
| 1330 | else |
| 1331 | nft_trans_chain_policy(trans) = -1; |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1332 | |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1333 | if (nla[NFTA_CHAIN_HANDLE] && name) { |
| 1334 | nla_strlcpy(nft_trans_chain_name(trans), name, |
| 1335 | NFT_CHAIN_MAXNAMELEN); |
| 1336 | } |
| 1337 | list_add_tail(&trans->list, &net->nft.commit_list); |
| 1338 | return 0; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
Patrick McHardy | 7582067 | 2014-01-09 18:42:33 +0000 | [diff] [blame] | 1341 | if (table->use == UINT_MAX) |
| 1342 | return -EOVERFLOW; |
| 1343 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1344 | if (nla[NFTA_CHAIN_HOOK]) { |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 1345 | const struct nf_chain_type *type; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1346 | struct nf_hook_ops *ops; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1347 | nf_hookfn *hookfn; |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 1348 | u32 hooknum, priority; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1349 | |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 1350 | type = chain_type[family][NFT_CHAIN_T_DEFAULT]; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1351 | if (nla[NFTA_CHAIN_TYPE]) { |
| 1352 | type = nf_tables_chain_type_lookup(afi, |
| 1353 | nla[NFTA_CHAIN_TYPE], |
| 1354 | create); |
Patrick McHardy | 93b0806 | 2014-01-09 18:42:36 +0000 | [diff] [blame] | 1355 | if (IS_ERR(type)) |
| 1356 | return PTR_ERR(type); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1357 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1358 | |
| 1359 | err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK], |
| 1360 | nft_hook_policy); |
| 1361 | if (err < 0) |
| 1362 | return err; |
| 1363 | if (ha[NFTA_HOOK_HOOKNUM] == NULL || |
| 1364 | ha[NFTA_HOOK_PRIORITY] == NULL) |
| 1365 | return -EINVAL; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1366 | |
| 1367 | hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); |
| 1368 | if (hooknum >= afi->nhooks) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1369 | return -EINVAL; |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 1370 | priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1371 | |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 1372 | if (!(type->hook_mask & (1 << hooknum))) |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1373 | return -EOPNOTSUPP; |
Patrick McHardy | fa2c1de | 2014-01-09 18:42:38 +0000 | [diff] [blame] | 1374 | if (!try_module_get(type->owner)) |
Patrick McHardy | baae3e6 | 2014-01-09 18:42:34 +0000 | [diff] [blame] | 1375 | return -ENOENT; |
Patrick McHardy | fa2c1de | 2014-01-09 18:42:38 +0000 | [diff] [blame] | 1376 | hookfn = type->hooks[hooknum]; |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1377 | |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1378 | if (afi->flags & NFT_AF_NEEDS_DEV) { |
| 1379 | char ifname[IFNAMSIZ]; |
| 1380 | |
| 1381 | if (!ha[NFTA_HOOK_DEV]) { |
| 1382 | module_put(type->owner); |
| 1383 | return -EOPNOTSUPP; |
| 1384 | } |
| 1385 | |
| 1386 | nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ); |
| 1387 | dev = dev_get_by_name(net, ifname); |
| 1388 | if (!dev) { |
| 1389 | module_put(type->owner); |
| 1390 | return -ENOENT; |
| 1391 | } |
| 1392 | } else if (ha[NFTA_HOOK_DEV]) { |
| 1393 | module_put(type->owner); |
| 1394 | return -EOPNOTSUPP; |
| 1395 | } |
| 1396 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1397 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL); |
Pablo Neira Ayuso | f5553c1 | 2015-01-29 19:08:09 +0100 | [diff] [blame] | 1398 | if (basechain == NULL) { |
| 1399 | module_put(type->owner); |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1400 | if (dev != NULL) |
| 1401 | dev_put(dev); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1402 | return -ENOMEM; |
Pablo Neira Ayuso | f5553c1 | 2015-01-29 19:08:09 +0100 | [diff] [blame] | 1403 | } |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1404 | |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1405 | if (dev != NULL) |
| 1406 | strncpy(basechain->dev_name, dev->name, IFNAMSIZ); |
| 1407 | |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1408 | if (nla[NFTA_CHAIN_COUNTERS]) { |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1409 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); |
| 1410 | if (IS_ERR(stats)) { |
Patrick McHardy | fa2c1de | 2014-01-09 18:42:38 +0000 | [diff] [blame] | 1411 | module_put(type->owner); |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1412 | kfree(basechain); |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1413 | if (dev != NULL) |
| 1414 | dev_put(dev); |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1415 | return PTR_ERR(stats); |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1416 | } |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1417 | basechain->stats = stats; |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1418 | } else { |
Eric Dumazet | ce355e2 | 2014-07-09 15:14:06 +0200 | [diff] [blame] | 1419 | stats = netdev_alloc_pcpu_stats(struct nft_stats); |
Sabrina Dubroca | c123bb7 | 2014-10-21 11:08:21 +0200 | [diff] [blame] | 1420 | if (stats == NULL) { |
Patrick McHardy | fa2c1de | 2014-01-09 18:42:38 +0000 | [diff] [blame] | 1421 | module_put(type->owner); |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1422 | kfree(basechain); |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1423 | if (dev != NULL) |
| 1424 | dev_put(dev); |
Sabrina Dubroca | c123bb7 | 2014-10-21 11:08:21 +0200 | [diff] [blame] | 1425 | return -ENOMEM; |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1426 | } |
Pablo Neira Ayuso | ff3cd7b | 2014-04-09 11:53:06 +0200 | [diff] [blame] | 1427 | rcu_assign_pointer(basechain->stats, stats); |
Patrick McHardy | 4401a86 | 2014-01-09 18:42:32 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Patrick McHardy | 5ebb335 | 2015-03-21 15:19:15 +0000 | [diff] [blame] | 1430 | write_pnet(&basechain->pnet, net); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1431 | basechain->type = type; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1432 | chain = &basechain->chain; |
| 1433 | |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 1434 | for (i = 0; i < afi->nops; i++) { |
| 1435 | ops = &basechain->ops[i]; |
| 1436 | ops->pf = family; |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 1437 | ops->hooknum = hooknum; |
| 1438 | ops->priority = priority; |
| 1439 | ops->priv = chain; |
| 1440 | ops->hook = afi->hooks[ops->hooknum]; |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame] | 1441 | ops->dev = dev; |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 1442 | if (hookfn) |
| 1443 | ops->hook = hookfn; |
| 1444 | if (afi->hook_ops_init) |
| 1445 | afi->hook_ops_init(ops, i); |
| 1446 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1447 | |
| 1448 | chain->flags |= NFT_BASE_CHAIN; |
Patrick McHardy | 57de2a0 | 2014-01-09 18:42:31 +0000 | [diff] [blame] | 1449 | basechain->policy = policy; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1450 | } else { |
| 1451 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); |
| 1452 | if (chain == NULL) |
| 1453 | return -ENOMEM; |
| 1454 | } |
| 1455 | |
| 1456 | INIT_LIST_HEAD(&chain->rules); |
| 1457 | chain->handle = nf_tables_alloc_handle(table); |
Pablo Neira Ayuso | b5bc89b | 2013-10-10 16:49:19 +0200 | [diff] [blame] | 1458 | chain->table = table; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1459 | nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN); |
| 1460 | |
Pablo Neira Ayuso | d8ee8f7 | 2015-06-15 02:42:31 +0200 | [diff] [blame] | 1461 | err = nf_tables_register_hooks(table, chain, afi->nops); |
| 1462 | if (err < 0) |
| 1463 | goto err1; |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1464 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 1465 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1466 | err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN); |
| 1467 | if (err < 0) |
| 1468 | goto err2; |
| 1469 | |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 1470 | table->use++; |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1471 | list_add_tail_rcu(&chain->list, &table->chains); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1472 | return 0; |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1473 | err2: |
Arturo Borrero | c559879 | 2014-09-02 16:42:23 +0200 | [diff] [blame] | 1474 | nf_tables_unregister_hooks(table, chain, afi->nops); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1475 | err1: |
| 1476 | nf_tables_chain_destroy(chain); |
| 1477 | return err; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 1480 | static int nf_tables_delchain(struct net *net, struct sock *nlsk, |
| 1481 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1482 | const struct nlattr * const nla[]) |
| 1483 | { |
| 1484 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 1485 | struct nft_af_info *afi; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1486 | struct nft_table *table; |
| 1487 | struct nft_chain *chain; |
| 1488 | int family = nfmsg->nfgen_family; |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1489 | struct nft_ctx ctx; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1490 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 1491 | afi = nf_tables_afinfo_lookup(net, family, false); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1492 | if (IS_ERR(afi)) |
| 1493 | return PTR_ERR(afi); |
| 1494 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1495 | table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1496 | if (IS_ERR(table)) |
| 1497 | return PTR_ERR(table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 1498 | if (table->flags & NFT_TABLE_INACTIVE) |
| 1499 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1500 | |
| 1501 | chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]); |
| 1502 | if (IS_ERR(chain)) |
| 1503 | return PTR_ERR(chain); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1504 | if (chain->flags & NFT_CHAIN_INACTIVE) |
| 1505 | return -ENOENT; |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 1506 | if (chain->use > 0) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1507 | return -EBUSY; |
| 1508 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 1509 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1510 | |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 1511 | return nft_delchain(&ctx); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1514 | /* |
| 1515 | * Expressions |
| 1516 | */ |
| 1517 | |
| 1518 | /** |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1519 | * nft_register_expr - register nf_tables expr type |
| 1520 | * @ops: expr type |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1521 | * |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1522 | * Registers the expr type for use with nf_tables. Returns zero on |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1523 | * success or a negative errno code otherwise. |
| 1524 | */ |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1525 | int nft_register_expr(struct nft_expr_type *type) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1526 | { |
| 1527 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Tomasz Bursztyka | 758dbce | 2014-04-14 15:41:26 +0300 | [diff] [blame] | 1528 | if (type->family == NFPROTO_UNSPEC) |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1529 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
Tomasz Bursztyka | 758dbce | 2014-04-14 15:41:26 +0300 | [diff] [blame] | 1530 | else |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1531 | list_add_rcu(&type->list, &nf_tables_expressions); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1532 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 1533 | return 0; |
| 1534 | } |
| 1535 | EXPORT_SYMBOL_GPL(nft_register_expr); |
| 1536 | |
| 1537 | /** |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1538 | * nft_unregister_expr - unregister nf_tables expr type |
| 1539 | * @ops: expr type |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1540 | * |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1541 | * Unregisters the expr typefor use with nf_tables. |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1542 | */ |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1543 | void nft_unregister_expr(struct nft_expr_type *type) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1544 | { |
| 1545 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1546 | list_del_rcu(&type->list); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1547 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 1548 | } |
| 1549 | EXPORT_SYMBOL_GPL(nft_unregister_expr); |
| 1550 | |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 1551 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
| 1552 | struct nlattr *nla) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1553 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1554 | const struct nft_expr_type *type; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1555 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1556 | list_for_each_entry(type, &nf_tables_expressions, list) { |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 1557 | if (!nla_strcmp(nla, type->name) && |
| 1558 | (!type->family || type->family == family)) |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1559 | return type; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1560 | } |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 1564 | static const struct nft_expr_type *nft_expr_type_get(u8 family, |
| 1565 | struct nlattr *nla) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1566 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1567 | const struct nft_expr_type *type; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1568 | |
| 1569 | if (nla == NULL) |
| 1570 | return ERR_PTR(-EINVAL); |
| 1571 | |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 1572 | type = __nft_expr_type_get(family, nla); |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1573 | if (type != NULL && try_module_get(type->owner)) |
| 1574 | return type; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1575 | |
| 1576 | #ifdef CONFIG_MODULES |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1577 | if (type == NULL) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1578 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 1579 | request_module("nft-expr-%u-%.*s", family, |
| 1580 | nla_len(nla), (char *)nla_data(nla)); |
| 1581 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| 1582 | if (__nft_expr_type_get(family, nla)) |
| 1583 | return ERR_PTR(-EAGAIN); |
| 1584 | |
| 1585 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1586 | request_module("nft-expr-%.*s", |
| 1587 | nla_len(nla), (char *)nla_data(nla)); |
| 1588 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 1589 | if (__nft_expr_type_get(family, nla)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1590 | return ERR_PTR(-EAGAIN); |
| 1591 | } |
| 1592 | #endif |
| 1593 | return ERR_PTR(-ENOENT); |
| 1594 | } |
| 1595 | |
| 1596 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { |
| 1597 | [NFTA_EXPR_NAME] = { .type = NLA_STRING }, |
| 1598 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
| 1599 | }; |
| 1600 | |
| 1601 | static int nf_tables_fill_expr_info(struct sk_buff *skb, |
| 1602 | const struct nft_expr *expr) |
| 1603 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1604 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1605 | goto nla_put_failure; |
| 1606 | |
| 1607 | if (expr->ops->dump) { |
| 1608 | struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA); |
| 1609 | if (data == NULL) |
| 1610 | goto nla_put_failure; |
| 1611 | if (expr->ops->dump(skb, expr) < 0) |
| 1612 | goto nla_put_failure; |
| 1613 | nla_nest_end(skb, data); |
| 1614 | } |
| 1615 | |
| 1616 | return skb->len; |
| 1617 | |
| 1618 | nla_put_failure: |
| 1619 | return -1; |
| 1620 | }; |
| 1621 | |
Patrick McHardy | 0b2d8a7 | 2015-04-11 10:46:38 +0100 | [diff] [blame] | 1622 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
| 1623 | const struct nft_expr *expr) |
| 1624 | { |
| 1625 | struct nlattr *nest; |
| 1626 | |
| 1627 | nest = nla_nest_start(skb, attr); |
| 1628 | if (!nest) |
| 1629 | goto nla_put_failure; |
| 1630 | if (nf_tables_fill_expr_info(skb, expr) < 0) |
| 1631 | goto nla_put_failure; |
| 1632 | nla_nest_end(skb, nest); |
| 1633 | return 0; |
| 1634 | |
| 1635 | nla_put_failure: |
| 1636 | return -1; |
| 1637 | } |
| 1638 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1639 | struct nft_expr_info { |
| 1640 | const struct nft_expr_ops *ops; |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1641 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1642 | }; |
| 1643 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1644 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
| 1645 | const struct nlattr *nla, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1646 | struct nft_expr_info *info) |
| 1647 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1648 | const struct nft_expr_type *type; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1649 | const struct nft_expr_ops *ops; |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1650 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1651 | int err; |
| 1652 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1653 | err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1654 | if (err < 0) |
| 1655 | return err; |
| 1656 | |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 1657 | type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]); |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1658 | if (IS_ERR(type)) |
| 1659 | return PTR_ERR(type); |
| 1660 | |
| 1661 | if (tb[NFTA_EXPR_DATA]) { |
| 1662 | err = nla_parse_nested(info->tb, type->maxattr, |
| 1663 | tb[NFTA_EXPR_DATA], type->policy); |
| 1664 | if (err < 0) |
| 1665 | goto err1; |
| 1666 | } else |
| 1667 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); |
| 1668 | |
| 1669 | if (type->select_ops != NULL) { |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1670 | ops = type->select_ops(ctx, |
| 1671 | (const struct nlattr * const *)info->tb); |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1672 | if (IS_ERR(ops)) { |
| 1673 | err = PTR_ERR(ops); |
| 1674 | goto err1; |
| 1675 | } |
| 1676 | } else |
| 1677 | ops = type->ops; |
| 1678 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1679 | info->ops = ops; |
| 1680 | return 0; |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1681 | |
| 1682 | err1: |
| 1683 | module_put(type->owner); |
| 1684 | return err; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | static int nf_tables_newexpr(const struct nft_ctx *ctx, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1688 | const struct nft_expr_info *info, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1689 | struct nft_expr *expr) |
| 1690 | { |
| 1691 | const struct nft_expr_ops *ops = info->ops; |
| 1692 | int err; |
| 1693 | |
| 1694 | expr->ops = ops; |
| 1695 | if (ops->init) { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1696 | err = ops->init(ctx, expr, (const struct nlattr **)info->tb); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1697 | if (err < 0) |
| 1698 | goto err1; |
| 1699 | } |
| 1700 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1701 | return 0; |
| 1702 | |
| 1703 | err1: |
| 1704 | expr->ops = NULL; |
| 1705 | return err; |
| 1706 | } |
| 1707 | |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 1708 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
| 1709 | struct nft_expr *expr) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1710 | { |
| 1711 | if (expr->ops->destroy) |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 1712 | expr->ops->destroy(ctx, expr); |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 1713 | module_put(expr->ops->type->owner); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
Patrick McHardy | 0b2d8a7 | 2015-04-11 10:46:38 +0100 | [diff] [blame] | 1716 | struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
| 1717 | const struct nlattr *nla) |
| 1718 | { |
| 1719 | struct nft_expr_info info; |
| 1720 | struct nft_expr *expr; |
| 1721 | int err; |
| 1722 | |
| 1723 | err = nf_tables_expr_parse(ctx, nla, &info); |
| 1724 | if (err < 0) |
| 1725 | goto err1; |
| 1726 | |
| 1727 | err = -ENOMEM; |
| 1728 | expr = kzalloc(info.ops->size, GFP_KERNEL); |
| 1729 | if (expr == NULL) |
| 1730 | goto err2; |
| 1731 | |
| 1732 | err = nf_tables_newexpr(ctx, &info, expr); |
| 1733 | if (err < 0) |
| 1734 | goto err2; |
| 1735 | |
| 1736 | return expr; |
| 1737 | err2: |
| 1738 | module_put(info.ops->type->owner); |
| 1739 | err1: |
| 1740 | return ERR_PTR(err); |
| 1741 | } |
| 1742 | |
| 1743 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
| 1744 | { |
| 1745 | nf_tables_expr_destroy(ctx, expr); |
| 1746 | kfree(expr); |
| 1747 | } |
| 1748 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1749 | /* |
| 1750 | * Rules |
| 1751 | */ |
| 1752 | |
| 1753 | static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain, |
| 1754 | u64 handle) |
| 1755 | { |
| 1756 | struct nft_rule *rule; |
| 1757 | |
| 1758 | // FIXME: this sucks |
| 1759 | list_for_each_entry(rule, &chain->rules, list) { |
| 1760 | if (handle == rule->handle) |
| 1761 | return rule; |
| 1762 | } |
| 1763 | |
| 1764 | return ERR_PTR(-ENOENT); |
| 1765 | } |
| 1766 | |
| 1767 | static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain, |
| 1768 | const struct nlattr *nla) |
| 1769 | { |
| 1770 | if (nla == NULL) |
| 1771 | return ERR_PTR(-EINVAL); |
| 1772 | |
| 1773 | return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
| 1774 | } |
| 1775 | |
| 1776 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { |
| 1777 | [NFTA_RULE_TABLE] = { .type = NLA_STRING }, |
| 1778 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
| 1779 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, |
| 1780 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, |
| 1781 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1782 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
Eric Leblond | 5e94846 | 2013-10-10 13:41:44 +0200 | [diff] [blame] | 1783 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
Pablo Neira Ayuso | 0768b3b | 2014-02-19 17:27:06 +0100 | [diff] [blame] | 1784 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
| 1785 | .len = NFT_USERDATA_MAXLEN }, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1786 | }; |
| 1787 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 1788 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
| 1789 | u32 portid, u32 seq, int event, |
| 1790 | u32 flags, int family, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1791 | const struct nft_table *table, |
| 1792 | const struct nft_chain *chain, |
| 1793 | const struct nft_rule *rule) |
| 1794 | { |
| 1795 | struct nlmsghdr *nlh; |
| 1796 | struct nfgenmsg *nfmsg; |
| 1797 | const struct nft_expr *expr, *next; |
| 1798 | struct nlattr *list; |
Eric Leblond | 5e94846 | 2013-10-10 13:41:44 +0200 | [diff] [blame] | 1799 | const struct nft_rule *prule; |
| 1800 | int type = event | NFNL_SUBSYS_NFTABLES << 8; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1801 | |
Eric Leblond | 5e94846 | 2013-10-10 13:41:44 +0200 | [diff] [blame] | 1802 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1803 | flags); |
| 1804 | if (nlh == NULL) |
| 1805 | goto nla_put_failure; |
| 1806 | |
| 1807 | nfmsg = nlmsg_data(nlh); |
| 1808 | nfmsg->nfgen_family = family; |
| 1809 | nfmsg->version = NFNETLINK_V0; |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 1810 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1811 | |
| 1812 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
| 1813 | goto nla_put_failure; |
| 1814 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) |
| 1815 | goto nla_put_failure; |
| 1816 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle))) |
| 1817 | goto nla_put_failure; |
| 1818 | |
Eric Leblond | 5e94846 | 2013-10-10 13:41:44 +0200 | [diff] [blame] | 1819 | if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) { |
| 1820 | prule = list_entry(rule->list.prev, struct nft_rule, list); |
| 1821 | if (nla_put_be64(skb, NFTA_RULE_POSITION, |
| 1822 | cpu_to_be64(prule->handle))) |
| 1823 | goto nla_put_failure; |
| 1824 | } |
| 1825 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1826 | list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS); |
| 1827 | if (list == NULL) |
| 1828 | goto nla_put_failure; |
| 1829 | nft_rule_for_each_expr(expr, next, rule) { |
Patrick McHardy | 0b2d8a7 | 2015-04-11 10:46:38 +0100 | [diff] [blame] | 1830 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1831 | goto nla_put_failure; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1832 | } |
| 1833 | nla_nest_end(skb, list); |
| 1834 | |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 1835 | if (rule->udata) { |
| 1836 | struct nft_userdata *udata = nft_userdata(rule); |
| 1837 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, |
| 1838 | udata->data) < 0) |
| 1839 | goto nla_put_failure; |
| 1840 | } |
Pablo Neira Ayuso | 0768b3b | 2014-02-19 17:27:06 +0100 | [diff] [blame] | 1841 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1842 | nlmsg_end(skb, nlh); |
| 1843 | return 0; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1844 | |
| 1845 | nla_put_failure: |
| 1846 | nlmsg_trim(skb, nlh); |
| 1847 | return -1; |
| 1848 | } |
| 1849 | |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 1850 | static int nf_tables_rule_notify(const struct nft_ctx *ctx, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1851 | const struct nft_rule *rule, |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 1852 | int event) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1853 | { |
| 1854 | struct sk_buff *skb; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1855 | int err; |
| 1856 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 1857 | if (!ctx->report && |
| 1858 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1859 | return 0; |
| 1860 | |
| 1861 | err = -ENOBUFS; |
| 1862 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1863 | if (skb == NULL) |
| 1864 | goto err; |
| 1865 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 1866 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
| 1867 | event, 0, ctx->afi->family, ctx->table, |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 1868 | ctx->chain, rule); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1869 | if (err < 0) { |
| 1870 | kfree_skb(skb); |
| 1871 | goto err; |
| 1872 | } |
| 1873 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 1874 | err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| 1875 | ctx->report, GFP_KERNEL); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1876 | err: |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 1877 | if (err < 0) { |
| 1878 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| 1879 | err); |
| 1880 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1881 | return err; |
| 1882 | } |
| 1883 | |
| 1884 | static int nf_tables_dump_rules(struct sk_buff *skb, |
| 1885 | struct netlink_callback *cb) |
| 1886 | { |
| 1887 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
| 1888 | const struct nft_af_info *afi; |
| 1889 | const struct nft_table *table; |
| 1890 | const struct nft_chain *chain; |
| 1891 | const struct nft_rule *rule; |
| 1892 | unsigned int idx = 0, s_idx = cb->args[0]; |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 1893 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1894 | int family = nfmsg->nfgen_family; |
| 1895 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1896 | rcu_read_lock(); |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 1897 | cb->seq = net->nft.base_seq; |
| 1898 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1899 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1900 | if (family != NFPROTO_UNSPEC && family != afi->family) |
| 1901 | continue; |
| 1902 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1903 | list_for_each_entry_rcu(table, &afi->tables, list) { |
| 1904 | list_for_each_entry_rcu(chain, &table->chains, list) { |
| 1905 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 1906 | if (!nft_rule_is_active(net, rule)) |
| 1907 | goto cont; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1908 | if (idx < s_idx) |
| 1909 | goto cont; |
| 1910 | if (idx > s_idx) |
| 1911 | memset(&cb->args[1], 0, |
| 1912 | sizeof(cb->args) - sizeof(cb->args[0])); |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 1913 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1914 | cb->nlh->nlmsg_seq, |
| 1915 | NFT_MSG_NEWRULE, |
| 1916 | NLM_F_MULTI | NLM_F_APPEND, |
| 1917 | afi->family, table, chain, rule) < 0) |
| 1918 | goto done; |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 1919 | |
| 1920 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1921 | cont: |
| 1922 | idx++; |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | done: |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 1928 | rcu_read_unlock(); |
| 1929 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1930 | cb->args[0] = idx; |
| 1931 | return skb->len; |
| 1932 | } |
| 1933 | |
| 1934 | static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb, |
| 1935 | const struct nlmsghdr *nlh, |
| 1936 | const struct nlattr * const nla[]) |
| 1937 | { |
| 1938 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| 1939 | const struct nft_af_info *afi; |
| 1940 | const struct nft_table *table; |
| 1941 | const struct nft_chain *chain; |
| 1942 | const struct nft_rule *rule; |
| 1943 | struct sk_buff *skb2; |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 1944 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1945 | int family = nfmsg->nfgen_family; |
| 1946 | int err; |
| 1947 | |
| 1948 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 1949 | struct netlink_dump_control c = { |
| 1950 | .dump = nf_tables_dump_rules, |
| 1951 | }; |
| 1952 | return netlink_dump_start(nlsk, skb, nlh, &c); |
| 1953 | } |
| 1954 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 1955 | afi = nf_tables_afinfo_lookup(net, family, false); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1956 | if (IS_ERR(afi)) |
| 1957 | return PTR_ERR(afi); |
| 1958 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 1959 | table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1960 | if (IS_ERR(table)) |
| 1961 | return PTR_ERR(table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 1962 | if (table->flags & NFT_TABLE_INACTIVE) |
| 1963 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1964 | |
| 1965 | chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]); |
| 1966 | if (IS_ERR(chain)) |
| 1967 | return PTR_ERR(chain); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 1968 | if (chain->flags & NFT_CHAIN_INACTIVE) |
| 1969 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1970 | |
| 1971 | rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
| 1972 | if (IS_ERR(rule)) |
| 1973 | return PTR_ERR(rule); |
| 1974 | |
| 1975 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1976 | if (!skb2) |
| 1977 | return -ENOMEM; |
| 1978 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 1979 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1980 | nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
| 1981 | family, table, chain, rule); |
| 1982 | if (err < 0) |
| 1983 | goto err; |
| 1984 | |
| 1985 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); |
| 1986 | |
| 1987 | err: |
| 1988 | kfree_skb(skb2); |
| 1989 | return err; |
| 1990 | } |
| 1991 | |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 1992 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
| 1993 | struct nft_rule *rule) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1994 | { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1995 | struct nft_expr *expr; |
| 1996 | |
| 1997 | /* |
| 1998 | * Careful: some expressions might not be initialized in case this |
| 1999 | * is called on error from nf_tables_newrule(). |
| 2000 | */ |
| 2001 | expr = nft_expr_first(rule); |
| 2002 | while (expr->ops && expr != nft_expr_last(rule)) { |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 2003 | nf_tables_expr_destroy(ctx, expr); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2004 | expr = nft_expr_next(expr); |
| 2005 | } |
| 2006 | kfree(rule); |
| 2007 | } |
| 2008 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2009 | #define NFT_RULE_MAXEXPRS 128 |
| 2010 | |
| 2011 | static struct nft_expr_info *info; |
| 2012 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2013 | static int nf_tables_newrule(struct net *net, struct sock *nlsk, |
| 2014 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2015 | const struct nlattr * const nla[]) |
| 2016 | { |
| 2017 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 2018 | struct nft_af_info *afi; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2019 | struct nft_table *table; |
| 2020 | struct nft_chain *chain; |
| 2021 | struct nft_rule *rule, *old_rule = NULL; |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 2022 | struct nft_userdata *udata; |
Pablo Neira Ayuso | 1081d11 | 2014-04-04 01:24:07 +0200 | [diff] [blame] | 2023 | struct nft_trans *trans = NULL; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2024 | struct nft_expr *expr; |
| 2025 | struct nft_ctx ctx; |
| 2026 | struct nlattr *tmp; |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 2027 | unsigned int size, i, n, ulen = 0, usize = 0; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2028 | int err, rem; |
| 2029 | bool create; |
Eric Leblond | 5e94846 | 2013-10-10 13:41:44 +0200 | [diff] [blame] | 2030 | u64 handle, pos_handle; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2031 | |
| 2032 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; |
| 2033 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 2034 | afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2035 | if (IS_ERR(afi)) |
| 2036 | return PTR_ERR(afi); |
| 2037 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 2038 | table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2039 | if (IS_ERR(table)) |
| 2040 | return PTR_ERR(table); |
| 2041 | |
| 2042 | chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]); |
| 2043 | if (IS_ERR(chain)) |
| 2044 | return PTR_ERR(chain); |
| 2045 | |
| 2046 | if (nla[NFTA_RULE_HANDLE]) { |
| 2047 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); |
| 2048 | rule = __nf_tables_rule_lookup(chain, handle); |
| 2049 | if (IS_ERR(rule)) |
| 2050 | return PTR_ERR(rule); |
| 2051 | |
| 2052 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 2053 | return -EEXIST; |
| 2054 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 2055 | old_rule = rule; |
| 2056 | else |
| 2057 | return -EOPNOTSUPP; |
| 2058 | } else { |
| 2059 | if (!create || nlh->nlmsg_flags & NLM_F_REPLACE) |
| 2060 | return -EINVAL; |
| 2061 | handle = nf_tables_alloc_handle(table); |
Pablo Neira Ayuso | a0a7379 | 2014-06-10 10:53:01 +0200 | [diff] [blame] | 2062 | |
| 2063 | if (chain->use == UINT_MAX) |
| 2064 | return -EOVERFLOW; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2065 | } |
| 2066 | |
Eric Leblond | 5e94846 | 2013-10-10 13:41:44 +0200 | [diff] [blame] | 2067 | if (nla[NFTA_RULE_POSITION]) { |
| 2068 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) |
| 2069 | return -EOPNOTSUPP; |
| 2070 | |
| 2071 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); |
| 2072 | old_rule = __nf_tables_rule_lookup(chain, pos_handle); |
| 2073 | if (IS_ERR(old_rule)) |
| 2074 | return PTR_ERR(old_rule); |
| 2075 | } |
| 2076 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2077 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 2078 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2079 | n = 0; |
| 2080 | size = 0; |
| 2081 | if (nla[NFTA_RULE_EXPRESSIONS]) { |
| 2082 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
| 2083 | err = -EINVAL; |
| 2084 | if (nla_type(tmp) != NFTA_LIST_ELEM) |
| 2085 | goto err1; |
| 2086 | if (n == NFT_RULE_MAXEXPRS) |
| 2087 | goto err1; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 2088 | err = nf_tables_expr_parse(&ctx, tmp, &info[n]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2089 | if (err < 0) |
| 2090 | goto err1; |
| 2091 | size += info[n].ops->size; |
| 2092 | n++; |
| 2093 | } |
| 2094 | } |
Patrick McHardy | 9889840 | 2015-03-03 20:04:19 +0000 | [diff] [blame] | 2095 | /* Check for overflow of dlen field */ |
| 2096 | err = -EFBIG; |
| 2097 | if (size >= 1 << 12) |
| 2098 | goto err1; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2099 | |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 2100 | if (nla[NFTA_RULE_USERDATA]) { |
Pablo Neira Ayuso | 0768b3b | 2014-02-19 17:27:06 +0100 | [diff] [blame] | 2101 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 2102 | if (ulen > 0) |
| 2103 | usize = sizeof(struct nft_userdata) + ulen; |
| 2104 | } |
Pablo Neira Ayuso | 0768b3b | 2014-02-19 17:27:06 +0100 | [diff] [blame] | 2105 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2106 | err = -ENOMEM; |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 2107 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2108 | if (rule == NULL) |
| 2109 | goto err1; |
| 2110 | |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2111 | nft_rule_activate_next(net, rule); |
| 2112 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2113 | rule->handle = handle; |
| 2114 | rule->dlen = size; |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 2115 | rule->udata = ulen ? 1 : 0; |
Pablo Neira Ayuso | 0768b3b | 2014-02-19 17:27:06 +0100 | [diff] [blame] | 2116 | |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 2117 | if (ulen) { |
| 2118 | udata = nft_userdata(rule); |
| 2119 | udata->len = ulen - 1; |
| 2120 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); |
| 2121 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2122 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2123 | expr = nft_expr_first(rule); |
| 2124 | for (i = 0; i < n; i++) { |
| 2125 | err = nf_tables_newexpr(&ctx, &info[i], expr); |
| 2126 | if (err < 0) |
| 2127 | goto err2; |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 2128 | info[i].ops = NULL; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2129 | expr = nft_expr_next(expr); |
| 2130 | } |
| 2131 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2132 | if (nlh->nlmsg_flags & NLM_F_REPLACE) { |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2133 | if (nft_rule_is_active_next(net, old_rule)) { |
Pablo Neira Ayuso | ac904ac | 2014-06-10 10:53:03 +0200 | [diff] [blame] | 2134 | trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE, |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 2135 | old_rule); |
Pablo Neira Ayuso | 1081d11 | 2014-04-04 01:24:07 +0200 | [diff] [blame] | 2136 | if (trans == NULL) { |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2137 | err = -ENOMEM; |
| 2138 | goto err2; |
| 2139 | } |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 2140 | nft_rule_deactivate_next(net, old_rule); |
Pablo Neira Ayuso | ac34b86 | 2014-06-10 10:53:02 +0200 | [diff] [blame] | 2141 | chain->use--; |
Pablo Neira Ayuso | 5bc5c30 | 2014-06-10 10:53:00 +0200 | [diff] [blame] | 2142 | list_add_tail_rcu(&rule->list, &old_rule->list); |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2143 | } else { |
| 2144 | err = -ENOENT; |
| 2145 | goto err2; |
| 2146 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2147 | } else if (nlh->nlmsg_flags & NLM_F_APPEND) |
Eric Leblond | 5e94846 | 2013-10-10 13:41:44 +0200 | [diff] [blame] | 2148 | if (old_rule) |
| 2149 | list_add_rcu(&rule->list, &old_rule->list); |
| 2150 | else |
| 2151 | list_add_tail_rcu(&rule->list, &chain->rules); |
| 2152 | else { |
| 2153 | if (old_rule) |
| 2154 | list_add_tail_rcu(&rule->list, &old_rule->list); |
| 2155 | else |
| 2156 | list_add_rcu(&rule->list, &chain->rules); |
| 2157 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2158 | |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 2159 | if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) { |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2160 | err = -ENOMEM; |
| 2161 | goto err3; |
| 2162 | } |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 2163 | chain->use++; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2164 | return 0; |
| 2165 | |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2166 | err3: |
| 2167 | list_del_rcu(&rule->list); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2168 | err2: |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 2169 | nf_tables_rule_destroy(&ctx, rule); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2170 | err1: |
| 2171 | for (i = 0; i < n; i++) { |
| 2172 | if (info[i].ops != NULL) |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 2173 | module_put(info[i].ops->type->owner); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2174 | } |
| 2175 | return err; |
| 2176 | } |
| 2177 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2178 | static int nf_tables_delrule(struct net *net, struct sock *nlsk, |
| 2179 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2180 | const struct nlattr * const nla[]) |
| 2181 | { |
| 2182 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 2183 | struct nft_af_info *afi; |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 2184 | struct nft_table *table; |
Pablo Neira Ayuso | cf9dc09 | 2013-11-24 20:39:10 +0100 | [diff] [blame] | 2185 | struct nft_chain *chain = NULL; |
| 2186 | struct nft_rule *rule; |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2187 | int family = nfmsg->nfgen_family, err = 0; |
| 2188 | struct nft_ctx ctx; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2189 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 2190 | afi = nf_tables_afinfo_lookup(net, family, false); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2191 | if (IS_ERR(afi)) |
| 2192 | return PTR_ERR(afi); |
| 2193 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 2194 | table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2195 | if (IS_ERR(table)) |
| 2196 | return PTR_ERR(table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 2197 | if (table->flags & NFT_TABLE_INACTIVE) |
| 2198 | return -ENOENT; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2199 | |
Pablo Neira Ayuso | cf9dc09 | 2013-11-24 20:39:10 +0100 | [diff] [blame] | 2200 | if (nla[NFTA_RULE_CHAIN]) { |
| 2201 | chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]); |
| 2202 | if (IS_ERR(chain)) |
| 2203 | return PTR_ERR(chain); |
| 2204 | } |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2205 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2206 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2207 | |
Pablo Neira Ayuso | cf9dc09 | 2013-11-24 20:39:10 +0100 | [diff] [blame] | 2208 | if (chain) { |
| 2209 | if (nla[NFTA_RULE_HANDLE]) { |
| 2210 | rule = nf_tables_rule_lookup(chain, |
| 2211 | nla[NFTA_RULE_HANDLE]); |
| 2212 | if (IS_ERR(rule)) |
| 2213 | return PTR_ERR(rule); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2214 | |
Arturo Borrero | 5e266fe | 2014-09-02 16:42:21 +0200 | [diff] [blame] | 2215 | err = nft_delrule(&ctx, rule); |
Pablo Neira Ayuso | cf9dc09 | 2013-11-24 20:39:10 +0100 | [diff] [blame] | 2216 | } else { |
Arturo Borrero | ce24b72 | 2014-09-02 16:42:24 +0200 | [diff] [blame] | 2217 | err = nft_delrule_by_chain(&ctx); |
Pablo Neira Ayuso | cf9dc09 | 2013-11-24 20:39:10 +0100 | [diff] [blame] | 2218 | } |
| 2219 | } else { |
| 2220 | list_for_each_entry(chain, &table->chains, list) { |
| 2221 | ctx.chain = chain; |
Arturo Borrero | ce24b72 | 2014-09-02 16:42:24 +0200 | [diff] [blame] | 2222 | err = nft_delrule_by_chain(&ctx); |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2223 | if (err < 0) |
| 2224 | break; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2225 | } |
| 2226 | } |
| 2227 | |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 2228 | return err; |
| 2229 | } |
| 2230 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2231 | /* |
| 2232 | * Sets |
| 2233 | */ |
| 2234 | |
| 2235 | static LIST_HEAD(nf_tables_set_ops); |
| 2236 | |
| 2237 | int nft_register_set(struct nft_set_ops *ops) |
| 2238 | { |
| 2239 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2240 | list_add_tail_rcu(&ops->list, &nf_tables_set_ops); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2241 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 2242 | return 0; |
| 2243 | } |
| 2244 | EXPORT_SYMBOL_GPL(nft_register_set); |
| 2245 | |
| 2246 | void nft_unregister_set(struct nft_set_ops *ops) |
| 2247 | { |
| 2248 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2249 | list_del_rcu(&ops->list); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2250 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 2251 | } |
| 2252 | EXPORT_SYMBOL_GPL(nft_unregister_set); |
| 2253 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2254 | /* |
| 2255 | * Select a set implementation based on the data characteristics and the |
| 2256 | * given policy. The total memory use might not be known if no size is |
| 2257 | * given, in that case the amount of memory per element is used. |
| 2258 | */ |
| 2259 | static const struct nft_set_ops * |
| 2260 | nft_select_set_ops(const struct nlattr * const nla[], |
| 2261 | const struct nft_set_desc *desc, |
| 2262 | enum nft_set_policies policy) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2263 | { |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2264 | const struct nft_set_ops *ops, *bops; |
| 2265 | struct nft_set_estimate est, best; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2266 | u32 features; |
| 2267 | |
| 2268 | #ifdef CONFIG_MODULES |
| 2269 | if (list_empty(&nf_tables_set_ops)) { |
| 2270 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| 2271 | request_module("nft-set"); |
| 2272 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| 2273 | if (!list_empty(&nf_tables_set_ops)) |
| 2274 | return ERR_PTR(-EAGAIN); |
| 2275 | } |
| 2276 | #endif |
| 2277 | features = 0; |
| 2278 | if (nla[NFTA_SET_FLAGS] != NULL) { |
| 2279 | features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); |
Patrick McHardy | 4a8678e | 2015-04-05 14:41:05 +0200 | [diff] [blame] | 2280 | features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2281 | } |
| 2282 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2283 | bops = NULL; |
| 2284 | best.size = ~0; |
| 2285 | best.class = ~0; |
| 2286 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2287 | list_for_each_entry(ops, &nf_tables_set_ops, list) { |
| 2288 | if ((ops->features & features) != features) |
| 2289 | continue; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2290 | if (!ops->estimate(desc, features, &est)) |
| 2291 | continue; |
| 2292 | |
| 2293 | switch (policy) { |
| 2294 | case NFT_SET_POL_PERFORMANCE: |
| 2295 | if (est.class < best.class) |
| 2296 | break; |
| 2297 | if (est.class == best.class && est.size < best.size) |
| 2298 | break; |
| 2299 | continue; |
| 2300 | case NFT_SET_POL_MEMORY: |
| 2301 | if (est.size < best.size) |
| 2302 | break; |
| 2303 | if (est.size == best.size && est.class < best.class) |
| 2304 | break; |
| 2305 | continue; |
| 2306 | default: |
| 2307 | break; |
| 2308 | } |
| 2309 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2310 | if (!try_module_get(ops->owner)) |
| 2311 | continue; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2312 | if (bops != NULL) |
| 2313 | module_put(bops->owner); |
| 2314 | |
| 2315 | bops = ops; |
| 2316 | best = est; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2319 | if (bops != NULL) |
| 2320 | return bops; |
| 2321 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2322 | return ERR_PTR(-EOPNOTSUPP); |
| 2323 | } |
| 2324 | |
| 2325 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { |
| 2326 | [NFTA_SET_TABLE] = { .type = NLA_STRING }, |
Pablo Neira Ayuso | a9bdd83 | 2014-03-24 15:10:37 +0100 | [diff] [blame] | 2327 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
| 2328 | .len = IFNAMSIZ - 1 }, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2329 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
| 2330 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, |
| 2331 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, |
| 2332 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, |
| 2333 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2334 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
| 2335 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2336 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
Patrick McHardy | 761da29 | 2015-03-26 12:39:36 +0000 | [diff] [blame] | 2337 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
| 2338 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2339 | }; |
| 2340 | |
| 2341 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { |
| 2342 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2343 | }; |
| 2344 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2345 | static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2346 | const struct sk_buff *skb, |
| 2347 | const struct nlmsghdr *nlh, |
| 2348 | const struct nlattr * const nla[]) |
| 2349 | { |
| 2350 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 2351 | struct nft_af_info *afi = NULL; |
| 2352 | struct nft_table *table = NULL; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2353 | |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2354 | if (nfmsg->nfgen_family != NFPROTO_UNSPEC) { |
| 2355 | afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false); |
| 2356 | if (IS_ERR(afi)) |
| 2357 | return PTR_ERR(afi); |
| 2358 | } |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2359 | |
| 2360 | if (nla[NFTA_SET_TABLE] != NULL) { |
Patrick McHardy | ec2c993 | 2014-02-05 15:03:35 +0000 | [diff] [blame] | 2361 | if (afi == NULL) |
| 2362 | return -EAFNOSUPPORT; |
| 2363 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 2364 | table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2365 | if (IS_ERR(table)) |
| 2366 | return PTR_ERR(table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 2367 | if (table->flags & NFT_TABLE_INACTIVE) |
| 2368 | return -ENOENT; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2369 | } |
| 2370 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2371 | nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2372 | return 0; |
| 2373 | } |
| 2374 | |
| 2375 | struct nft_set *nf_tables_set_lookup(const struct nft_table *table, |
| 2376 | const struct nlattr *nla) |
| 2377 | { |
| 2378 | struct nft_set *set; |
| 2379 | |
| 2380 | if (nla == NULL) |
| 2381 | return ERR_PTR(-EINVAL); |
| 2382 | |
| 2383 | list_for_each_entry(set, &table->sets, list) { |
| 2384 | if (!nla_strcmp(nla, set->name)) |
| 2385 | return set; |
| 2386 | } |
| 2387 | return ERR_PTR(-ENOENT); |
| 2388 | } |
| 2389 | |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2390 | struct nft_set *nf_tables_set_lookup_byid(const struct net *net, |
| 2391 | const struct nlattr *nla) |
| 2392 | { |
| 2393 | struct nft_trans *trans; |
| 2394 | u32 id = ntohl(nla_get_be32(nla)); |
| 2395 | |
| 2396 | list_for_each_entry(trans, &net->nft.commit_list, list) { |
| 2397 | if (trans->msg_type == NFT_MSG_NEWSET && |
| 2398 | id == nft_trans_set_id(trans)) |
| 2399 | return nft_trans_set(trans); |
| 2400 | } |
| 2401 | return ERR_PTR(-ENOENT); |
| 2402 | } |
| 2403 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2404 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
| 2405 | const char *name) |
| 2406 | { |
| 2407 | const struct nft_set *i; |
| 2408 | const char *p; |
| 2409 | unsigned long *inuse; |
Patrick McHardy | 60eb189 | 2014-03-07 12:34:05 +0100 | [diff] [blame] | 2410 | unsigned int n = 0, min = 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2411 | |
| 2412 | p = strnchr(name, IFNAMSIZ, '%'); |
| 2413 | if (p != NULL) { |
| 2414 | if (p[1] != 'd' || strchr(p + 2, '%')) |
| 2415 | return -EINVAL; |
| 2416 | |
| 2417 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); |
| 2418 | if (inuse == NULL) |
| 2419 | return -ENOMEM; |
Patrick McHardy | 60eb189 | 2014-03-07 12:34:05 +0100 | [diff] [blame] | 2420 | cont: |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2421 | list_for_each_entry(i, &ctx->table->sets, list) { |
Daniel Borkmann | 1466291 | 2013-12-31 12:40:05 +0100 | [diff] [blame] | 2422 | int tmp; |
| 2423 | |
| 2424 | if (!sscanf(i->name, name, &tmp)) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2425 | continue; |
Patrick McHardy | 60eb189 | 2014-03-07 12:34:05 +0100 | [diff] [blame] | 2426 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2427 | continue; |
Daniel Borkmann | 1466291 | 2013-12-31 12:40:05 +0100 | [diff] [blame] | 2428 | |
Patrick McHardy | 60eb189 | 2014-03-07 12:34:05 +0100 | [diff] [blame] | 2429 | set_bit(tmp - min, inuse); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2430 | } |
| 2431 | |
Patrick McHardy | 53b7028 | 2014-02-05 12:26:22 +0100 | [diff] [blame] | 2432 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
Patrick McHardy | 60eb189 | 2014-03-07 12:34:05 +0100 | [diff] [blame] | 2433 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
| 2434 | min += BITS_PER_BYTE * PAGE_SIZE; |
| 2435 | memset(inuse, 0, PAGE_SIZE); |
| 2436 | goto cont; |
| 2437 | } |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2438 | free_page((unsigned long)inuse); |
| 2439 | } |
| 2440 | |
Patrick McHardy | 60eb189 | 2014-03-07 12:34:05 +0100 | [diff] [blame] | 2441 | snprintf(set->name, sizeof(set->name), name, min + n); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2442 | list_for_each_entry(i, &ctx->table->sets, list) { |
| 2443 | if (!strcmp(set->name, i->name)) |
| 2444 | return -ENFILE; |
| 2445 | } |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
| 2449 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
| 2450 | const struct nft_set *set, u16 event, u16 flags) |
| 2451 | { |
| 2452 | struct nfgenmsg *nfmsg; |
| 2453 | struct nlmsghdr *nlh; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2454 | struct nlattr *desc; |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 2455 | u32 portid = ctx->portid; |
| 2456 | u32 seq = ctx->seq; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2457 | |
| 2458 | event |= NFNL_SUBSYS_NFTABLES << 8; |
| 2459 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
| 2460 | flags); |
| 2461 | if (nlh == NULL) |
| 2462 | goto nla_put_failure; |
| 2463 | |
| 2464 | nfmsg = nlmsg_data(nlh); |
| 2465 | nfmsg->nfgen_family = ctx->afi->family; |
| 2466 | nfmsg->version = NFNETLINK_V0; |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 2467 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2468 | |
| 2469 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
| 2470 | goto nla_put_failure; |
| 2471 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) |
| 2472 | goto nla_put_failure; |
| 2473 | if (set->flags != 0) |
| 2474 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) |
| 2475 | goto nla_put_failure; |
| 2476 | |
| 2477 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) |
| 2478 | goto nla_put_failure; |
| 2479 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) |
| 2480 | goto nla_put_failure; |
| 2481 | if (set->flags & NFT_SET_MAP) { |
| 2482 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) |
| 2483 | goto nla_put_failure; |
| 2484 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) |
| 2485 | goto nla_put_failure; |
| 2486 | } |
| 2487 | |
Patrick McHardy | 761da29 | 2015-03-26 12:39:36 +0000 | [diff] [blame] | 2488 | if (set->timeout && |
| 2489 | nla_put_be64(skb, NFTA_SET_TIMEOUT, cpu_to_be64(set->timeout))) |
| 2490 | goto nla_put_failure; |
| 2491 | if (set->gc_int && |
| 2492 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) |
| 2493 | goto nla_put_failure; |
| 2494 | |
Arturo Borrero | 9363dc4 | 2014-09-23 13:30:41 +0200 | [diff] [blame] | 2495 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
| 2496 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) |
| 2497 | goto nla_put_failure; |
| 2498 | } |
| 2499 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2500 | desc = nla_nest_start(skb, NFTA_SET_DESC); |
| 2501 | if (desc == NULL) |
| 2502 | goto nla_put_failure; |
| 2503 | if (set->size && |
| 2504 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) |
| 2505 | goto nla_put_failure; |
| 2506 | nla_nest_end(skb, desc); |
| 2507 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2508 | nlmsg_end(skb, nlh); |
| 2509 | return 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2510 | |
| 2511 | nla_put_failure: |
| 2512 | nlmsg_trim(skb, nlh); |
| 2513 | return -1; |
| 2514 | } |
| 2515 | |
| 2516 | static int nf_tables_set_notify(const struct nft_ctx *ctx, |
| 2517 | const struct nft_set *set, |
Pablo Neira Ayuso | 31f8441 | 2014-05-29 10:29:58 +0200 | [diff] [blame] | 2518 | int event, gfp_t gfp_flags) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2519 | { |
| 2520 | struct sk_buff *skb; |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 2521 | u32 portid = ctx->portid; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2522 | int err; |
| 2523 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 2524 | if (!ctx->report && |
| 2525 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2526 | return 0; |
| 2527 | |
| 2528 | err = -ENOBUFS; |
Pablo Neira Ayuso | 31f8441 | 2014-05-29 10:29:58 +0200 | [diff] [blame] | 2529 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2530 | if (skb == NULL) |
| 2531 | goto err; |
| 2532 | |
| 2533 | err = nf_tables_fill_set(skb, ctx, set, event, 0); |
| 2534 | if (err < 0) { |
| 2535 | kfree_skb(skb); |
| 2536 | goto err; |
| 2537 | } |
| 2538 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 2539 | err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, |
Pablo Neira Ayuso | 31f8441 | 2014-05-29 10:29:58 +0200 | [diff] [blame] | 2540 | ctx->report, gfp_flags); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2541 | err: |
| 2542 | if (err < 0) |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 2543 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2544 | return err; |
| 2545 | } |
| 2546 | |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2547 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2548 | { |
| 2549 | const struct nft_set *set; |
| 2550 | unsigned int idx, s_idx = cb->args[0]; |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 2551 | struct nft_af_info *afi; |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2552 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
| 2553 | struct net *net = sock_net(skb->sk); |
| 2554 | int cur_family = cb->args[3]; |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2555 | struct nft_ctx *ctx = cb->data, ctx_set; |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2556 | |
| 2557 | if (cb->args[1]) |
| 2558 | return skb->len; |
| 2559 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2560 | rcu_read_lock(); |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 2561 | cb->seq = net->nft.base_seq; |
| 2562 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2563 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2564 | if (ctx->afi && ctx->afi != afi) |
| 2565 | continue; |
| 2566 | |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2567 | if (cur_family) { |
| 2568 | if (afi->family != cur_family) |
| 2569 | continue; |
| 2570 | |
| 2571 | cur_family = 0; |
| 2572 | } |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2573 | list_for_each_entry_rcu(table, &afi->tables, list) { |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2574 | if (ctx->table && ctx->table != table) |
| 2575 | continue; |
| 2576 | |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2577 | if (cur_table) { |
| 2578 | if (cur_table != table) |
| 2579 | continue; |
| 2580 | |
| 2581 | cur_table = NULL; |
| 2582 | } |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2583 | idx = 0; |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2584 | list_for_each_entry_rcu(set, &table->sets, list) { |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2585 | if (idx < s_idx) |
| 2586 | goto cont; |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2587 | |
| 2588 | ctx_set = *ctx; |
| 2589 | ctx_set.table = table; |
| 2590 | ctx_set.afi = afi; |
| 2591 | if (nf_tables_fill_set(skb, &ctx_set, set, |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2592 | NFT_MSG_NEWSET, |
| 2593 | NLM_F_MULTI) < 0) { |
| 2594 | cb->args[0] = idx; |
| 2595 | cb->args[2] = (unsigned long) table; |
| 2596 | cb->args[3] = afi->family; |
| 2597 | goto done; |
| 2598 | } |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 2599 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2600 | cont: |
| 2601 | idx++; |
| 2602 | } |
| 2603 | if (s_idx) |
| 2604 | s_idx = 0; |
| 2605 | } |
| 2606 | } |
| 2607 | cb->args[1] = 1; |
| 2608 | done: |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2609 | rcu_read_unlock(); |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2610 | return skb->len; |
| 2611 | } |
| 2612 | |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2613 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2614 | { |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2615 | kfree(cb->data); |
| 2616 | return 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb, |
| 2620 | const struct nlmsghdr *nlh, |
| 2621 | const struct nlattr * const nla[]) |
| 2622 | { |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2623 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2624 | const struct nft_set *set; |
| 2625 | struct nft_ctx ctx; |
| 2626 | struct sk_buff *skb2; |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2627 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2628 | int err; |
| 2629 | |
stephen hemminger | 01cfa0a | 2014-10-29 22:57:19 -0700 | [diff] [blame] | 2630 | /* Verify existence before starting dump */ |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2631 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2632 | if (err < 0) |
| 2633 | return err; |
| 2634 | |
| 2635 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 2636 | struct netlink_dump_control c = { |
| 2637 | .dump = nf_tables_dump_sets, |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2638 | .done = nf_tables_dump_sets_done, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2639 | }; |
Pablo Neira Ayuso | 5b96af7 | 2014-07-16 17:35:18 +0200 | [diff] [blame] | 2640 | struct nft_ctx *ctx_dump; |
| 2641 | |
| 2642 | ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL); |
| 2643 | if (ctx_dump == NULL) |
| 2644 | return -ENOMEM; |
| 2645 | |
| 2646 | *ctx_dump = ctx; |
| 2647 | c.data = ctx_dump; |
| 2648 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2649 | return netlink_dump_start(nlsk, skb, nlh, &c); |
| 2650 | } |
| 2651 | |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2652 | /* Only accept unspec with dump */ |
| 2653 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) |
| 2654 | return -EAFNOSUPPORT; |
| 2655 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2656 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]); |
| 2657 | if (IS_ERR(set)) |
| 2658 | return PTR_ERR(set); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2659 | if (set->flags & NFT_SET_INACTIVE) |
| 2660 | return -ENOENT; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2661 | |
| 2662 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 2663 | if (skb2 == NULL) |
| 2664 | return -ENOMEM; |
| 2665 | |
| 2666 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); |
| 2667 | if (err < 0) |
| 2668 | goto err; |
| 2669 | |
| 2670 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); |
| 2671 | |
| 2672 | err: |
| 2673 | kfree_skb(skb2); |
| 2674 | return err; |
| 2675 | } |
| 2676 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2677 | static int nf_tables_set_desc_parse(const struct nft_ctx *ctx, |
| 2678 | struct nft_set_desc *desc, |
| 2679 | const struct nlattr *nla) |
| 2680 | { |
| 2681 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; |
| 2682 | int err; |
| 2683 | |
| 2684 | err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy); |
| 2685 | if (err < 0) |
| 2686 | return err; |
| 2687 | |
| 2688 | if (da[NFTA_SET_DESC_SIZE] != NULL) |
| 2689 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); |
| 2690 | |
| 2691 | return 0; |
| 2692 | } |
| 2693 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2694 | static int nf_tables_newset(struct net *net, struct sock *nlsk, |
| 2695 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2696 | const struct nlattr * const nla[]) |
| 2697 | { |
| 2698 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| 2699 | const struct nft_set_ops *ops; |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 2700 | struct nft_af_info *afi; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2701 | struct nft_table *table; |
| 2702 | struct nft_set *set; |
| 2703 | struct nft_ctx ctx; |
| 2704 | char name[IFNAMSIZ]; |
| 2705 | unsigned int size; |
| 2706 | bool create; |
Patrick McHardy | 761da29 | 2015-03-26 12:39:36 +0000 | [diff] [blame] | 2707 | u64 timeout; |
| 2708 | u32 ktype, dtype, flags, policy, gc_int; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2709 | struct nft_set_desc desc; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2710 | int err; |
| 2711 | |
| 2712 | if (nla[NFTA_SET_TABLE] == NULL || |
| 2713 | nla[NFTA_SET_NAME] == NULL || |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2714 | nla[NFTA_SET_KEY_LEN] == NULL || |
| 2715 | nla[NFTA_SET_ID] == NULL) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2716 | return -EINVAL; |
| 2717 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2718 | memset(&desc, 0, sizeof(desc)); |
| 2719 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2720 | ktype = NFT_DATA_VALUE; |
| 2721 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { |
| 2722 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); |
| 2723 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) |
| 2724 | return -EINVAL; |
| 2725 | } |
| 2726 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2727 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 2728 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2729 | return -EINVAL; |
| 2730 | |
| 2731 | flags = 0; |
| 2732 | if (nla[NFTA_SET_FLAGS] != NULL) { |
| 2733 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); |
| 2734 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | |
Patrick McHardy | 7c6c6e9 | 2015-04-11 10:46:41 +0100 | [diff] [blame] | 2735 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
| 2736 | NFT_SET_MAP | NFT_SET_EVAL)) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2737 | return -EINVAL; |
Patrick McHardy | 7c6c6e9 | 2015-04-11 10:46:41 +0100 | [diff] [blame] | 2738 | /* Only one of both operations is supported */ |
| 2739 | if ((flags & (NFT_SET_MAP | NFT_SET_EVAL)) == |
| 2740 | (NFT_SET_MAP | NFT_SET_EVAL)) |
| 2741 | return -EOPNOTSUPP; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | dtype = 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2745 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
| 2746 | if (!(flags & NFT_SET_MAP)) |
| 2747 | return -EINVAL; |
| 2748 | |
| 2749 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); |
| 2750 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && |
| 2751 | dtype != NFT_DATA_VERDICT) |
| 2752 | return -EINVAL; |
| 2753 | |
| 2754 | if (dtype != NFT_DATA_VERDICT) { |
| 2755 | if (nla[NFTA_SET_DATA_LEN] == NULL) |
| 2756 | return -EINVAL; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2757 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 2758 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2759 | return -EINVAL; |
| 2760 | } else |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 2761 | desc.dlen = sizeof(struct nft_verdict); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2762 | } else if (flags & NFT_SET_MAP) |
| 2763 | return -EINVAL; |
| 2764 | |
Patrick McHardy | 761da29 | 2015-03-26 12:39:36 +0000 | [diff] [blame] | 2765 | timeout = 0; |
| 2766 | if (nla[NFTA_SET_TIMEOUT] != NULL) { |
| 2767 | if (!(flags & NFT_SET_TIMEOUT)) |
| 2768 | return -EINVAL; |
| 2769 | timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_TIMEOUT])); |
| 2770 | } |
| 2771 | gc_int = 0; |
| 2772 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { |
| 2773 | if (!(flags & NFT_SET_TIMEOUT)) |
| 2774 | return -EINVAL; |
| 2775 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); |
| 2776 | } |
| 2777 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2778 | policy = NFT_SET_POL_PERFORMANCE; |
| 2779 | if (nla[NFTA_SET_POLICY] != NULL) |
| 2780 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); |
| 2781 | |
| 2782 | if (nla[NFTA_SET_DESC] != NULL) { |
| 2783 | err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]); |
| 2784 | if (err < 0) |
| 2785 | return err; |
| 2786 | } |
| 2787 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2788 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; |
| 2789 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 2790 | afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2791 | if (IS_ERR(afi)) |
| 2792 | return PTR_ERR(afi); |
| 2793 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 2794 | table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2795 | if (IS_ERR(table)) |
| 2796 | return PTR_ERR(table); |
| 2797 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2798 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2799 | |
| 2800 | set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]); |
| 2801 | if (IS_ERR(set)) { |
| 2802 | if (PTR_ERR(set) != -ENOENT) |
| 2803 | return PTR_ERR(set); |
| 2804 | set = NULL; |
| 2805 | } |
| 2806 | |
| 2807 | if (set != NULL) { |
| 2808 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 2809 | return -EEXIST; |
| 2810 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 2811 | return -EOPNOTSUPP; |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
| 2815 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) |
| 2816 | return -ENOENT; |
| 2817 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2818 | ops = nft_select_set_ops(nla, &desc, policy); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2819 | if (IS_ERR(ops)) |
| 2820 | return PTR_ERR(ops); |
| 2821 | |
| 2822 | size = 0; |
| 2823 | if (ops->privsize != NULL) |
| 2824 | size = ops->privsize(nla); |
| 2825 | |
| 2826 | err = -ENOMEM; |
| 2827 | set = kzalloc(sizeof(*set) + size, GFP_KERNEL); |
| 2828 | if (set == NULL) |
| 2829 | goto err1; |
| 2830 | |
| 2831 | nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name)); |
| 2832 | err = nf_tables_set_alloc_name(&ctx, set, name); |
| 2833 | if (err < 0) |
| 2834 | goto err2; |
| 2835 | |
| 2836 | INIT_LIST_HEAD(&set->bindings); |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 2837 | write_pnet(&set->pnet, net); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2838 | set->ops = ops; |
| 2839 | set->ktype = ktype; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2840 | set->klen = desc.klen; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2841 | set->dtype = dtype; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2842 | set->dlen = desc.dlen; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2843 | set->flags = flags; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2844 | set->size = desc.size; |
Arturo Borrero | 9363dc4 | 2014-09-23 13:30:41 +0200 | [diff] [blame] | 2845 | set->policy = policy; |
Patrick McHardy | 761da29 | 2015-03-26 12:39:36 +0000 | [diff] [blame] | 2846 | set->timeout = timeout; |
| 2847 | set->gc_int = gc_int; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2848 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 2849 | err = ops->init(set, &desc, nla); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2850 | if (err < 0) |
| 2851 | goto err2; |
| 2852 | |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2853 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2854 | if (err < 0) |
| 2855 | goto err2; |
| 2856 | |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2857 | list_add_tail_rcu(&set->list, &table->sets); |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 2858 | table->use++; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2859 | return 0; |
| 2860 | |
| 2861 | err2: |
| 2862 | kfree(set); |
| 2863 | err1: |
| 2864 | module_put(ops->owner); |
| 2865 | return err; |
| 2866 | } |
| 2867 | |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2868 | static void nft_set_destroy(struct nft_set *set) |
| 2869 | { |
| 2870 | set->ops->destroy(set); |
| 2871 | module_put(set->ops->owner); |
| 2872 | kfree(set); |
| 2873 | } |
| 2874 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2875 | static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
| 2876 | { |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2877 | list_del_rcu(&set->list); |
Pablo Neira Ayuso | 31f8441 | 2014-05-29 10:29:58 +0200 | [diff] [blame] | 2878 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2879 | nft_set_destroy(set); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2880 | } |
| 2881 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2882 | static int nf_tables_delset(struct net *net, struct sock *nlsk, |
| 2883 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2884 | const struct nlattr * const nla[]) |
| 2885 | { |
Pablo Neira Ayuso | c9c8e48 | 2013-12-26 16:49:03 +0100 | [diff] [blame] | 2886 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2887 | struct nft_set *set; |
| 2888 | struct nft_ctx ctx; |
| 2889 | int err; |
| 2890 | |
Patrick McHardy | ec2c993 | 2014-02-05 15:03:35 +0000 | [diff] [blame] | 2891 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) |
| 2892 | return -EAFNOSUPPORT; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2893 | if (nla[NFTA_SET_TABLE] == NULL) |
| 2894 | return -EINVAL; |
| 2895 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 2896 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2897 | if (err < 0) |
| 2898 | return err; |
| 2899 | |
| 2900 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]); |
| 2901 | if (IS_ERR(set)) |
| 2902 | return PTR_ERR(set); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2903 | if (set->flags & NFT_SET_INACTIVE) |
| 2904 | return -ENOENT; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2905 | if (!list_empty(&set->bindings)) |
| 2906 | return -EBUSY; |
| 2907 | |
Arturo Borrero | ee01d54 | 2014-09-02 16:42:25 +0200 | [diff] [blame] | 2908 | return nft_delset(&ctx, set); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, |
| 2912 | const struct nft_set *set, |
| 2913 | const struct nft_set_iter *iter, |
| 2914 | const struct nft_set_elem *elem) |
| 2915 | { |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 2916 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2917 | enum nft_registers dreg; |
| 2918 | |
| 2919 | dreg = nft_type_to_reg(set->dtype); |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 2920 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
| 2921 | set->dtype == NFT_DATA_VERDICT ? |
| 2922 | NFT_DATA_VERDICT : NFT_DATA_VALUE, |
| 2923 | set->dlen); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
| 2927 | struct nft_set_binding *binding) |
| 2928 | { |
| 2929 | struct nft_set_binding *i; |
| 2930 | struct nft_set_iter iter; |
| 2931 | |
| 2932 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS) |
| 2933 | return -EBUSY; |
| 2934 | |
Patrick McHardy | 11113e1 | 2015-04-05 14:41:07 +0200 | [diff] [blame] | 2935 | if (binding->flags & NFT_SET_MAP) { |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2936 | /* If the set is already bound to the same chain all |
| 2937 | * jumps are already validated for that chain. |
| 2938 | */ |
| 2939 | list_for_each_entry(i, &set->bindings, list) { |
Patrick McHardy | 11113e1 | 2015-04-05 14:41:07 +0200 | [diff] [blame] | 2940 | if (binding->flags & NFT_SET_MAP && |
| 2941 | i->chain == binding->chain) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2942 | goto bind; |
| 2943 | } |
| 2944 | |
| 2945 | iter.skip = 0; |
| 2946 | iter.count = 0; |
| 2947 | iter.err = 0; |
| 2948 | iter.fn = nf_tables_bind_check_setelem; |
| 2949 | |
| 2950 | set->ops->walk(ctx, set, &iter); |
| 2951 | if (iter.err < 0) { |
| 2952 | /* Destroy anonymous sets if binding fails */ |
| 2953 | if (set->flags & NFT_SET_ANONYMOUS) |
| 2954 | nf_tables_set_destroy(ctx, set); |
| 2955 | |
| 2956 | return iter.err; |
| 2957 | } |
| 2958 | } |
| 2959 | bind: |
| 2960 | binding->chain = ctx->chain; |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2961 | list_add_tail_rcu(&binding->list, &set->bindings); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2962 | return 0; |
| 2963 | } |
| 2964 | |
| 2965 | void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
| 2966 | struct nft_set_binding *binding) |
| 2967 | { |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 2968 | list_del_rcu(&binding->list); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2969 | |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 2970 | if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS && |
| 2971 | !(set->flags & NFT_SET_INACTIVE)) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 2972 | nf_tables_set_destroy(ctx, set); |
| 2973 | } |
| 2974 | |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 2975 | const struct nft_set_ext_type nft_set_ext_types[] = { |
| 2976 | [NFT_SET_EXT_KEY] = { |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 2977 | .align = __alignof__(u32), |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 2978 | }, |
| 2979 | [NFT_SET_EXT_DATA] = { |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 2980 | .align = __alignof__(u32), |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 2981 | }, |
Patrick McHardy | f25ad2e | 2015-04-11 10:46:39 +0100 | [diff] [blame] | 2982 | [NFT_SET_EXT_EXPR] = { |
| 2983 | .align = __alignof__(struct nft_expr), |
| 2984 | }, |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 2985 | [NFT_SET_EXT_FLAGS] = { |
| 2986 | .len = sizeof(u8), |
| 2987 | .align = __alignof__(u8), |
| 2988 | }, |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 2989 | [NFT_SET_EXT_TIMEOUT] = { |
| 2990 | .len = sizeof(u64), |
| 2991 | .align = __alignof__(u64), |
| 2992 | }, |
| 2993 | [NFT_SET_EXT_EXPIRATION] = { |
| 2994 | .len = sizeof(unsigned long), |
| 2995 | .align = __alignof__(unsigned long), |
| 2996 | }, |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 2997 | [NFT_SET_EXT_USERDATA] = { |
| 2998 | .len = sizeof(struct nft_userdata), |
| 2999 | .align = __alignof__(struct nft_userdata), |
| 3000 | }, |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 3001 | }; |
| 3002 | EXPORT_SYMBOL_GPL(nft_set_ext_types); |
| 3003 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3004 | /* |
| 3005 | * Set elements |
| 3006 | */ |
| 3007 | |
| 3008 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { |
| 3009 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, |
| 3010 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, |
| 3011 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 3012 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 3013 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
| 3014 | .len = NFT_USERDATA_MAXLEN }, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3015 | }; |
| 3016 | |
| 3017 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { |
| 3018 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING }, |
| 3019 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING }, |
| 3020 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3021 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3022 | }; |
| 3023 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3024 | static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3025 | const struct sk_buff *skb, |
| 3026 | const struct nlmsghdr *nlh, |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 3027 | const struct nlattr * const nla[], |
| 3028 | bool trans) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3029 | { |
| 3030 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 3031 | struct nft_af_info *afi; |
| 3032 | struct nft_table *table; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3033 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 3034 | afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3035 | if (IS_ERR(afi)) |
| 3036 | return PTR_ERR(afi); |
| 3037 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 3038 | table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3039 | if (IS_ERR(table)) |
| 3040 | return PTR_ERR(table); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 3041 | if (!trans && (table->flags & NFT_TABLE_INACTIVE)) |
| 3042 | return -ENOENT; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3043 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3044 | nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3045 | return 0; |
| 3046 | } |
| 3047 | |
| 3048 | static int nf_tables_fill_setelem(struct sk_buff *skb, |
| 3049 | const struct nft_set *set, |
| 3050 | const struct nft_set_elem *elem) |
| 3051 | { |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3052 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3053 | unsigned char *b = skb_tail_pointer(skb); |
| 3054 | struct nlattr *nest; |
| 3055 | |
| 3056 | nest = nla_nest_start(skb, NFTA_LIST_ELEM); |
| 3057 | if (nest == NULL) |
| 3058 | goto nla_put_failure; |
| 3059 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3060 | if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), |
| 3061 | NFT_DATA_VALUE, set->klen) < 0) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3062 | goto nla_put_failure; |
| 3063 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3064 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
| 3065 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3066 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
| 3067 | set->dlen) < 0) |
| 3068 | goto nla_put_failure; |
| 3069 | |
Patrick McHardy | f25ad2e | 2015-04-11 10:46:39 +0100 | [diff] [blame] | 3070 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) && |
| 3071 | nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0) |
| 3072 | goto nla_put_failure; |
| 3073 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3074 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
| 3075 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, |
| 3076 | htonl(*nft_set_ext_flags(ext)))) |
| 3077 | goto nla_put_failure; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3078 | |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 3079 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
| 3080 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, |
| 3081 | cpu_to_be64(*nft_set_ext_timeout(ext)))) |
| 3082 | goto nla_put_failure; |
| 3083 | |
| 3084 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
| 3085 | unsigned long expires, now = jiffies; |
| 3086 | |
| 3087 | expires = *nft_set_ext_expiration(ext); |
| 3088 | if (time_before(now, expires)) |
| 3089 | expires -= now; |
| 3090 | else |
| 3091 | expires = 0; |
| 3092 | |
| 3093 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, |
| 3094 | cpu_to_be64(jiffies_to_msecs(expires)))) |
| 3095 | goto nla_put_failure; |
| 3096 | } |
| 3097 | |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 3098 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
| 3099 | struct nft_userdata *udata; |
| 3100 | |
| 3101 | udata = nft_set_ext_userdata(ext); |
| 3102 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, |
| 3103 | udata->len + 1, udata->data)) |
| 3104 | goto nla_put_failure; |
| 3105 | } |
| 3106 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3107 | nla_nest_end(skb, nest); |
| 3108 | return 0; |
| 3109 | |
| 3110 | nla_put_failure: |
| 3111 | nlmsg_trim(skb, b); |
| 3112 | return -EMSGSIZE; |
| 3113 | } |
| 3114 | |
| 3115 | struct nft_set_dump_args { |
| 3116 | const struct netlink_callback *cb; |
| 3117 | struct nft_set_iter iter; |
| 3118 | struct sk_buff *skb; |
| 3119 | }; |
| 3120 | |
| 3121 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, |
| 3122 | const struct nft_set *set, |
| 3123 | const struct nft_set_iter *iter, |
| 3124 | const struct nft_set_elem *elem) |
| 3125 | { |
| 3126 | struct nft_set_dump_args *args; |
| 3127 | |
| 3128 | args = container_of(iter, struct nft_set_dump_args, iter); |
| 3129 | return nf_tables_fill_setelem(args->skb, set, elem); |
| 3130 | } |
| 3131 | |
| 3132 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
| 3133 | { |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3134 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3135 | const struct nft_set *set; |
| 3136 | struct nft_set_dump_args args; |
| 3137 | struct nft_ctx ctx; |
| 3138 | struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1]; |
| 3139 | struct nfgenmsg *nfmsg; |
| 3140 | struct nlmsghdr *nlh; |
| 3141 | struct nlattr *nest; |
| 3142 | u32 portid, seq; |
| 3143 | int event, err; |
| 3144 | |
Michal Nazarewicz | 720e0df | 2014-01-01 06:27:19 +0100 | [diff] [blame] | 3145 | err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla, |
| 3146 | NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3147 | if (err < 0) |
| 3148 | return err; |
| 3149 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3150 | err = nft_ctx_init_from_elemattr(&ctx, net, cb->skb, cb->nlh, |
| 3151 | (void *)nla, false); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3152 | if (err < 0) |
| 3153 | return err; |
| 3154 | |
| 3155 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]); |
| 3156 | if (IS_ERR(set)) |
| 3157 | return PTR_ERR(set); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3158 | if (set->flags & NFT_SET_INACTIVE) |
| 3159 | return -ENOENT; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3160 | |
| 3161 | event = NFT_MSG_NEWSETELEM; |
| 3162 | event |= NFNL_SUBSYS_NFTABLES << 8; |
| 3163 | portid = NETLINK_CB(cb->skb).portid; |
| 3164 | seq = cb->nlh->nlmsg_seq; |
| 3165 | |
| 3166 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
| 3167 | NLM_F_MULTI); |
| 3168 | if (nlh == NULL) |
| 3169 | goto nla_put_failure; |
| 3170 | |
| 3171 | nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | 6403d96 | 2014-06-11 19:05:28 +0200 | [diff] [blame] | 3172 | nfmsg->nfgen_family = ctx.afi->family; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3173 | nfmsg->version = NFNETLINK_V0; |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 3174 | nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3175 | |
| 3176 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name)) |
| 3177 | goto nla_put_failure; |
| 3178 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) |
| 3179 | goto nla_put_failure; |
| 3180 | |
| 3181 | nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
| 3182 | if (nest == NULL) |
| 3183 | goto nla_put_failure; |
| 3184 | |
| 3185 | args.cb = cb; |
| 3186 | args.skb = skb; |
| 3187 | args.iter.skip = cb->args[0]; |
| 3188 | args.iter.count = 0; |
| 3189 | args.iter.err = 0; |
| 3190 | args.iter.fn = nf_tables_dump_setelem; |
| 3191 | set->ops->walk(&ctx, set, &args.iter); |
| 3192 | |
| 3193 | nla_nest_end(skb, nest); |
| 3194 | nlmsg_end(skb, nlh); |
| 3195 | |
| 3196 | if (args.iter.err && args.iter.err != -EMSGSIZE) |
| 3197 | return args.iter.err; |
| 3198 | if (args.iter.count == cb->args[0]) |
| 3199 | return 0; |
| 3200 | |
| 3201 | cb->args[0] = args.iter.count; |
| 3202 | return skb->len; |
| 3203 | |
| 3204 | nla_put_failure: |
| 3205 | return -ENOSPC; |
| 3206 | } |
| 3207 | |
| 3208 | static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb, |
| 3209 | const struct nlmsghdr *nlh, |
| 3210 | const struct nlattr * const nla[]) |
| 3211 | { |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3212 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3213 | const struct nft_set *set; |
| 3214 | struct nft_ctx ctx; |
| 3215 | int err; |
| 3216 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3217 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, false); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3218 | if (err < 0) |
| 3219 | return err; |
| 3220 | |
| 3221 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]); |
| 3222 | if (IS_ERR(set)) |
| 3223 | return PTR_ERR(set); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3224 | if (set->flags & NFT_SET_INACTIVE) |
| 3225 | return -ENOENT; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3226 | |
| 3227 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 3228 | struct netlink_dump_control c = { |
| 3229 | .dump = nf_tables_dump_set, |
| 3230 | }; |
| 3231 | return netlink_dump_start(nlsk, skb, nlh, &c); |
| 3232 | } |
| 3233 | return -EOPNOTSUPP; |
| 3234 | } |
| 3235 | |
Arturo Borrero | d60ce62 | 2014-04-01 14:06:07 +0200 | [diff] [blame] | 3236 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
| 3237 | const struct nft_ctx *ctx, u32 seq, |
| 3238 | u32 portid, int event, u16 flags, |
| 3239 | const struct nft_set *set, |
| 3240 | const struct nft_set_elem *elem) |
| 3241 | { |
| 3242 | struct nfgenmsg *nfmsg; |
| 3243 | struct nlmsghdr *nlh; |
| 3244 | struct nlattr *nest; |
| 3245 | int err; |
| 3246 | |
| 3247 | event |= NFNL_SUBSYS_NFTABLES << 8; |
| 3248 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
| 3249 | flags); |
| 3250 | if (nlh == NULL) |
| 3251 | goto nla_put_failure; |
| 3252 | |
| 3253 | nfmsg = nlmsg_data(nlh); |
| 3254 | nfmsg->nfgen_family = ctx->afi->family; |
| 3255 | nfmsg->version = NFNETLINK_V0; |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 3256 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
Arturo Borrero | d60ce62 | 2014-04-01 14:06:07 +0200 | [diff] [blame] | 3257 | |
| 3258 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
| 3259 | goto nla_put_failure; |
| 3260 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) |
| 3261 | goto nla_put_failure; |
| 3262 | |
| 3263 | nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
| 3264 | if (nest == NULL) |
| 3265 | goto nla_put_failure; |
| 3266 | |
| 3267 | err = nf_tables_fill_setelem(skb, set, elem); |
| 3268 | if (err < 0) |
| 3269 | goto nla_put_failure; |
| 3270 | |
| 3271 | nla_nest_end(skb, nest); |
| 3272 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 3273 | nlmsg_end(skb, nlh); |
| 3274 | return 0; |
Arturo Borrero | d60ce62 | 2014-04-01 14:06:07 +0200 | [diff] [blame] | 3275 | |
| 3276 | nla_put_failure: |
| 3277 | nlmsg_trim(skb, nlh); |
| 3278 | return -1; |
| 3279 | } |
| 3280 | |
| 3281 | static int nf_tables_setelem_notify(const struct nft_ctx *ctx, |
| 3282 | const struct nft_set *set, |
| 3283 | const struct nft_set_elem *elem, |
| 3284 | int event, u16 flags) |
| 3285 | { |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 3286 | struct net *net = ctx->net; |
| 3287 | u32 portid = ctx->portid; |
Arturo Borrero | d60ce62 | 2014-04-01 14:06:07 +0200 | [diff] [blame] | 3288 | struct sk_buff *skb; |
| 3289 | int err; |
| 3290 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 3291 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
Arturo Borrero | d60ce62 | 2014-04-01 14:06:07 +0200 | [diff] [blame] | 3292 | return 0; |
| 3293 | |
| 3294 | err = -ENOBUFS; |
| 3295 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 3296 | if (skb == NULL) |
| 3297 | goto err; |
| 3298 | |
| 3299 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, |
| 3300 | set, elem); |
| 3301 | if (err < 0) { |
| 3302 | kfree_skb(skb); |
| 3303 | goto err; |
| 3304 | } |
| 3305 | |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 3306 | err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report, |
Arturo Borrero | d60ce62 | 2014-04-01 14:06:07 +0200 | [diff] [blame] | 3307 | GFP_KERNEL); |
| 3308 | err: |
| 3309 | if (err < 0) |
| 3310 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err); |
| 3311 | return err; |
| 3312 | } |
| 3313 | |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3314 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
| 3315 | int msg_type, |
| 3316 | struct nft_set *set) |
| 3317 | { |
| 3318 | struct nft_trans *trans; |
| 3319 | |
| 3320 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); |
| 3321 | if (trans == NULL) |
| 3322 | return NULL; |
| 3323 | |
| 3324 | nft_trans_elem_set(trans) = set; |
| 3325 | return trans; |
| 3326 | } |
| 3327 | |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 3328 | void *nft_set_elem_init(const struct nft_set *set, |
| 3329 | const struct nft_set_ext_tmpl *tmpl, |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 3330 | const u32 *key, const u32 *data, |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 3331 | u64 timeout, gfp_t gfp) |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3332 | { |
| 3333 | struct nft_set_ext *ext; |
| 3334 | void *elem; |
| 3335 | |
| 3336 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); |
| 3337 | if (elem == NULL) |
| 3338 | return NULL; |
| 3339 | |
| 3340 | ext = nft_set_elem_ext(set, elem); |
| 3341 | nft_set_ext_init(ext, tmpl); |
| 3342 | |
| 3343 | memcpy(nft_set_ext_key(ext), key, set->klen); |
| 3344 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
| 3345 | memcpy(nft_set_ext_data(ext), data, set->dlen); |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 3346 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) |
| 3347 | *nft_set_ext_expiration(ext) = |
| 3348 | jiffies + msecs_to_jiffies(timeout); |
| 3349 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
| 3350 | *nft_set_ext_timeout(ext) = timeout; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3351 | |
| 3352 | return elem; |
| 3353 | } |
| 3354 | |
Patrick McHardy | 61edafb | 2015-03-25 14:08:47 +0000 | [diff] [blame] | 3355 | void nft_set_elem_destroy(const struct nft_set *set, void *elem) |
| 3356 | { |
| 3357 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); |
| 3358 | |
| 3359 | nft_data_uninit(nft_set_ext_key(ext), NFT_DATA_VALUE); |
| 3360 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
| 3361 | nft_data_uninit(nft_set_ext_data(ext), set->dtype); |
Patrick McHardy | f25ad2e | 2015-04-11 10:46:39 +0100 | [diff] [blame] | 3362 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) |
| 3363 | nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext)); |
Patrick McHardy | 61edafb | 2015-03-25 14:08:47 +0000 | [diff] [blame] | 3364 | |
| 3365 | kfree(elem); |
| 3366 | } |
| 3367 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); |
| 3368 | |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3369 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3370 | const struct nlattr *attr) |
| 3371 | { |
| 3372 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
| 3373 | struct nft_data_desc d1, d2; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3374 | struct nft_set_ext_tmpl tmpl; |
| 3375 | struct nft_set_ext *ext; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3376 | struct nft_set_elem elem; |
| 3377 | struct nft_set_binding *binding; |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 3378 | struct nft_userdata *udata; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3379 | struct nft_data data; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3380 | enum nft_registers dreg; |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3381 | struct nft_trans *trans; |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 3382 | u64 timeout; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3383 | u32 flags; |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 3384 | u8 ulen; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3385 | int err; |
| 3386 | |
| 3387 | err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr, |
| 3388 | nft_set_elem_policy); |
| 3389 | if (err < 0) |
| 3390 | return err; |
| 3391 | |
| 3392 | if (nla[NFTA_SET_ELEM_KEY] == NULL) |
| 3393 | return -EINVAL; |
| 3394 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3395 | nft_set_ext_prepare(&tmpl); |
| 3396 | |
| 3397 | flags = 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3398 | if (nla[NFTA_SET_ELEM_FLAGS] != NULL) { |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3399 | flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS])); |
| 3400 | if (flags & ~NFT_SET_ELEM_INTERVAL_END) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3401 | return -EINVAL; |
Patrick McHardy | 55df35d | 2015-03-21 15:19:16 +0000 | [diff] [blame] | 3402 | if (!(set->flags & NFT_SET_INTERVAL) && |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3403 | flags & NFT_SET_ELEM_INTERVAL_END) |
Patrick McHardy | 55df35d | 2015-03-21 15:19:16 +0000 | [diff] [blame] | 3404 | return -EINVAL; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3405 | if (flags != 0) |
| 3406 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | if (set->flags & NFT_SET_MAP) { |
| 3410 | if (nla[NFTA_SET_ELEM_DATA] == NULL && |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3411 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3412 | return -EINVAL; |
Pablo Neira Ayuso | bd7fc64 | 2014-02-07 12:53:07 +0100 | [diff] [blame] | 3413 | if (nla[NFTA_SET_ELEM_DATA] != NULL && |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3414 | flags & NFT_SET_ELEM_INTERVAL_END) |
Pablo Neira Ayuso | bd7fc64 | 2014-02-07 12:53:07 +0100 | [diff] [blame] | 3415 | return -EINVAL; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3416 | } else { |
| 3417 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
| 3418 | return -EINVAL; |
| 3419 | } |
| 3420 | |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 3421 | timeout = 0; |
| 3422 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { |
| 3423 | if (!(set->flags & NFT_SET_TIMEOUT)) |
| 3424 | return -EINVAL; |
| 3425 | timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_ELEM_TIMEOUT])); |
| 3426 | } else if (set->flags & NFT_SET_TIMEOUT) { |
| 3427 | timeout = set->timeout; |
| 3428 | } |
| 3429 | |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 3430 | err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1, |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 3431 | nla[NFTA_SET_ELEM_KEY]); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3432 | if (err < 0) |
| 3433 | goto err1; |
| 3434 | err = -EINVAL; |
| 3435 | if (d1.type != NFT_DATA_VALUE || d1.len != set->klen) |
| 3436 | goto err2; |
| 3437 | |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 3438 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len); |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 3439 | if (timeout > 0) { |
| 3440 | nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); |
| 3441 | if (timeout != set->timeout) |
| 3442 | nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); |
| 3443 | } |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3444 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3445 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 3446 | err = nft_data_init(ctx, &data, sizeof(data), &d2, |
| 3447 | nla[NFTA_SET_ELEM_DATA]); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3448 | if (err < 0) |
| 3449 | goto err2; |
| 3450 | |
| 3451 | err = -EINVAL; |
| 3452 | if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen) |
| 3453 | goto err3; |
| 3454 | |
| 3455 | dreg = nft_type_to_reg(set->dtype); |
| 3456 | list_for_each_entry(binding, &set->bindings, list) { |
| 3457 | struct nft_ctx bind_ctx = { |
| 3458 | .afi = ctx->afi, |
| 3459 | .table = ctx->table, |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 3460 | .chain = (struct nft_chain *)binding->chain, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3461 | }; |
| 3462 | |
Patrick McHardy | 11113e1 | 2015-04-05 14:41:07 +0200 | [diff] [blame] | 3463 | if (!(binding->flags & NFT_SET_MAP)) |
| 3464 | continue; |
| 3465 | |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 3466 | err = nft_validate_register_store(&bind_ctx, dreg, |
| 3467 | &data, |
| 3468 | d2.type, d2.len); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3469 | if (err < 0) |
| 3470 | goto err3; |
| 3471 | } |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3472 | |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 3473 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3474 | } |
| 3475 | |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 3476 | /* The full maximum length of userdata can exceed the maximum |
| 3477 | * offset value (U8_MAX) for following extensions, therefor it |
| 3478 | * must be the last extension added. |
| 3479 | */ |
| 3480 | ulen = 0; |
| 3481 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { |
| 3482 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); |
| 3483 | if (ulen > 0) |
| 3484 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, |
| 3485 | ulen); |
| 3486 | } |
| 3487 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3488 | err = -ENOMEM; |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 3489 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data, |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 3490 | timeout, GFP_KERNEL); |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3491 | if (elem.priv == NULL) |
| 3492 | goto err3; |
| 3493 | |
| 3494 | ext = nft_set_elem_ext(set, elem.priv); |
| 3495 | if (flags) |
| 3496 | *nft_set_ext_flags(ext) = flags; |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 3497 | if (ulen > 0) { |
| 3498 | udata = nft_set_ext_userdata(ext); |
| 3499 | udata->len = ulen - 1; |
| 3500 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); |
| 3501 | } |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3502 | |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3503 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
| 3504 | if (trans == NULL) |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3505 | goto err4; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3506 | |
Patrick McHardy | 6908665 | 2015-03-26 12:39:39 +0000 | [diff] [blame] | 3507 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3508 | err = set->ops->insert(set, &elem); |
| 3509 | if (err < 0) |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3510 | goto err5; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3511 | |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3512 | nft_trans_elem(trans) = elem; |
Pablo Neira Ayuso | 46bbafc | 2014-05-22 12:36:03 +0200 | [diff] [blame] | 3513 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3514 | return 0; |
| 3515 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3516 | err5: |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3517 | kfree(trans); |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3518 | err4: |
| 3519 | kfree(elem.priv); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3520 | err3: |
| 3521 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3522 | nft_data_uninit(&data, d2.type); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3523 | err2: |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 3524 | nft_data_uninit(&elem.key.val, d1.type); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3525 | err1: |
| 3526 | return err; |
| 3527 | } |
| 3528 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3529 | static int nf_tables_newsetelem(struct net *net, struct sock *nlsk, |
| 3530 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3531 | const struct nlattr * const nla[]) |
| 3532 | { |
| 3533 | const struct nlattr *attr; |
| 3534 | struct nft_set *set; |
| 3535 | struct nft_ctx ctx; |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3536 | int rem, err = 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3537 | |
Pablo Neira Ayuso | 7d5570c | 2014-07-25 13:15:36 +0200 | [diff] [blame] | 3538 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
| 3539 | return -EINVAL; |
| 3540 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3541 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, true); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3542 | if (err < 0) |
| 3543 | return err; |
| 3544 | |
| 3545 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3546 | if (IS_ERR(set)) { |
| 3547 | if (nla[NFTA_SET_ELEM_LIST_SET_ID]) { |
| 3548 | set = nf_tables_set_lookup_byid(net, |
| 3549 | nla[NFTA_SET_ELEM_LIST_SET_ID]); |
| 3550 | } |
| 3551 | if (IS_ERR(set)) |
| 3552 | return PTR_ERR(set); |
| 3553 | } |
| 3554 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3555 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
| 3556 | return -EBUSY; |
| 3557 | |
| 3558 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
Patrick McHardy | 3dd0673 | 2015-04-05 14:41:06 +0200 | [diff] [blame] | 3559 | if (set->size && |
| 3560 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) |
| 3561 | return -ENFILE; |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 3562 | |
Patrick McHardy | 3dd0673 | 2015-04-05 14:41:06 +0200 | [diff] [blame] | 3563 | err = nft_add_set_elem(&ctx, set, attr); |
| 3564 | if (err < 0) { |
| 3565 | atomic_dec(&set->nelems); |
| 3566 | break; |
| 3567 | } |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3568 | } |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3569 | return err; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3570 | } |
| 3571 | |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3572 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3573 | const struct nlattr *attr) |
| 3574 | { |
| 3575 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
| 3576 | struct nft_data_desc desc; |
| 3577 | struct nft_set_elem elem; |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3578 | struct nft_trans *trans; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3579 | int err; |
| 3580 | |
| 3581 | err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr, |
| 3582 | nft_set_elem_policy); |
| 3583 | if (err < 0) |
| 3584 | goto err1; |
| 3585 | |
| 3586 | err = -EINVAL; |
| 3587 | if (nla[NFTA_SET_ELEM_KEY] == NULL) |
| 3588 | goto err1; |
| 3589 | |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 3590 | err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc, |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 3591 | nla[NFTA_SET_ELEM_KEY]); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3592 | if (err < 0) |
| 3593 | goto err1; |
| 3594 | |
| 3595 | err = -EINVAL; |
| 3596 | if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) |
| 3597 | goto err2; |
| 3598 | |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3599 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
Julia Lawall | 609ccf0 | 2014-08-07 14:49:08 +0200 | [diff] [blame] | 3600 | if (trans == NULL) { |
| 3601 | err = -ENOMEM; |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3602 | goto err2; |
Julia Lawall | 609ccf0 | 2014-08-07 14:49:08 +0200 | [diff] [blame] | 3603 | } |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3604 | |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 3605 | elem.priv = set->ops->deactivate(set, &elem); |
| 3606 | if (elem.priv == NULL) { |
| 3607 | err = -ENOENT; |
| 3608 | goto err3; |
| 3609 | } |
| 3610 | |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3611 | nft_trans_elem(trans) = elem; |
Pablo Neira Ayuso | 46bbafc | 2014-05-22 12:36:03 +0200 | [diff] [blame] | 3612 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
Thomas Graf | 0dc1362 | 2014-08-01 17:25:38 +0200 | [diff] [blame] | 3613 | return 0; |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 3614 | |
| 3615 | err3: |
| 3616 | kfree(trans); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3617 | err2: |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 3618 | nft_data_uninit(&elem.key.val, desc.type); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3619 | err1: |
| 3620 | return err; |
| 3621 | } |
| 3622 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3623 | static int nf_tables_delsetelem(struct net *net, struct sock *nlsk, |
| 3624 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3625 | const struct nlattr * const nla[]) |
| 3626 | { |
| 3627 | const struct nlattr *attr; |
| 3628 | struct nft_set *set; |
| 3629 | struct nft_ctx ctx; |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3630 | int rem, err = 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3631 | |
Pablo Neira Ayuso | 7d5570c | 2014-07-25 13:15:36 +0200 | [diff] [blame] | 3632 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
| 3633 | return -EINVAL; |
| 3634 | |
Pablo Neira Ayuso | 633c9a8 | 2015-12-09 12:08:26 +0100 | [diff] [blame] | 3635 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, false); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3636 | if (err < 0) |
| 3637 | return err; |
| 3638 | |
| 3639 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]); |
| 3640 | if (IS_ERR(set)) |
| 3641 | return PTR_ERR(set); |
| 3642 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
| 3643 | return -EBUSY; |
| 3644 | |
| 3645 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
| 3646 | err = nft_del_setelem(&ctx, set, attr); |
| 3647 | if (err < 0) |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3648 | break; |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 3649 | |
Patrick McHardy | 3dd0673 | 2015-04-05 14:41:06 +0200 | [diff] [blame] | 3650 | set->ndeact++; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3651 | } |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3652 | return err; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3653 | } |
| 3654 | |
Patrick McHardy | cfed7e1 | 2015-03-26 12:39:38 +0000 | [diff] [blame] | 3655 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
| 3656 | { |
| 3657 | struct nft_set_gc_batch *gcb; |
| 3658 | unsigned int i; |
| 3659 | |
| 3660 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); |
| 3661 | for (i = 0; i < gcb->head.cnt; i++) |
| 3662 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i]); |
| 3663 | kfree(gcb); |
| 3664 | } |
| 3665 | EXPORT_SYMBOL_GPL(nft_set_gc_batch_release); |
| 3666 | |
| 3667 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, |
| 3668 | gfp_t gfp) |
| 3669 | { |
| 3670 | struct nft_set_gc_batch *gcb; |
| 3671 | |
| 3672 | gcb = kzalloc(sizeof(*gcb), gfp); |
| 3673 | if (gcb == NULL) |
| 3674 | return gcb; |
| 3675 | gcb->head.set = set; |
| 3676 | return gcb; |
| 3677 | } |
| 3678 | EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc); |
| 3679 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 3680 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
| 3681 | u32 portid, u32 seq) |
| 3682 | { |
| 3683 | struct nlmsghdr *nlh; |
| 3684 | struct nfgenmsg *nfmsg; |
| 3685 | int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN; |
| 3686 | |
| 3687 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0); |
| 3688 | if (nlh == NULL) |
| 3689 | goto nla_put_failure; |
| 3690 | |
| 3691 | nfmsg = nlmsg_data(nlh); |
| 3692 | nfmsg->nfgen_family = AF_UNSPEC; |
| 3693 | nfmsg->version = NFNETLINK_V0; |
| 3694 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
| 3695 | |
| 3696 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq))) |
| 3697 | goto nla_put_failure; |
| 3698 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 3699 | nlmsg_end(skb, nlh); |
| 3700 | return 0; |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 3701 | |
| 3702 | nla_put_failure: |
| 3703 | nlmsg_trim(skb, nlh); |
| 3704 | return -EMSGSIZE; |
| 3705 | } |
| 3706 | |
| 3707 | static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event) |
| 3708 | { |
| 3709 | struct nlmsghdr *nlh = nlmsg_hdr(skb); |
| 3710 | struct sk_buff *skb2; |
| 3711 | int err; |
| 3712 | |
| 3713 | if (nlmsg_report(nlh) && |
| 3714 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
| 3715 | return 0; |
| 3716 | |
| 3717 | err = -ENOBUFS; |
| 3718 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 3719 | if (skb2 == NULL) |
| 3720 | goto err; |
| 3721 | |
| 3722 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, |
| 3723 | nlh->nlmsg_seq); |
| 3724 | if (err < 0) { |
| 3725 | kfree_skb(skb2); |
| 3726 | goto err; |
| 3727 | } |
| 3728 | |
| 3729 | err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, |
| 3730 | NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL); |
| 3731 | err: |
| 3732 | if (err < 0) { |
| 3733 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
| 3734 | err); |
| 3735 | } |
| 3736 | return err; |
| 3737 | } |
| 3738 | |
| 3739 | static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb, |
| 3740 | const struct nlmsghdr *nlh, |
| 3741 | const struct nlattr * const nla[]) |
| 3742 | { |
| 3743 | struct net *net = sock_net(skb->sk); |
| 3744 | struct sk_buff *skb2; |
| 3745 | int err; |
| 3746 | |
| 3747 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 3748 | if (skb2 == NULL) |
| 3749 | return -ENOMEM; |
| 3750 | |
| 3751 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, |
| 3752 | nlh->nlmsg_seq); |
| 3753 | if (err < 0) |
| 3754 | goto err; |
| 3755 | |
| 3756 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); |
| 3757 | err: |
| 3758 | kfree_skb(skb2); |
| 3759 | return err; |
| 3760 | } |
| 3761 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3762 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
| 3763 | [NFT_MSG_NEWTABLE] = { |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 3764 | .call_batch = nf_tables_newtable, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3765 | .attr_count = NFTA_TABLE_MAX, |
| 3766 | .policy = nft_table_policy, |
| 3767 | }, |
| 3768 | [NFT_MSG_GETTABLE] = { |
| 3769 | .call = nf_tables_gettable, |
| 3770 | .attr_count = NFTA_TABLE_MAX, |
| 3771 | .policy = nft_table_policy, |
| 3772 | }, |
| 3773 | [NFT_MSG_DELTABLE] = { |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 3774 | .call_batch = nf_tables_deltable, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3775 | .attr_count = NFTA_TABLE_MAX, |
| 3776 | .policy = nft_table_policy, |
| 3777 | }, |
| 3778 | [NFT_MSG_NEWCHAIN] = { |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 3779 | .call_batch = nf_tables_newchain, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3780 | .attr_count = NFTA_CHAIN_MAX, |
| 3781 | .policy = nft_chain_policy, |
| 3782 | }, |
| 3783 | [NFT_MSG_GETCHAIN] = { |
| 3784 | .call = nf_tables_getchain, |
| 3785 | .attr_count = NFTA_CHAIN_MAX, |
| 3786 | .policy = nft_chain_policy, |
| 3787 | }, |
| 3788 | [NFT_MSG_DELCHAIN] = { |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 3789 | .call_batch = nf_tables_delchain, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3790 | .attr_count = NFTA_CHAIN_MAX, |
| 3791 | .policy = nft_chain_policy, |
| 3792 | }, |
| 3793 | [NFT_MSG_NEWRULE] = { |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 3794 | .call_batch = nf_tables_newrule, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3795 | .attr_count = NFTA_RULE_MAX, |
| 3796 | .policy = nft_rule_policy, |
| 3797 | }, |
| 3798 | [NFT_MSG_GETRULE] = { |
| 3799 | .call = nf_tables_getrule, |
| 3800 | .attr_count = NFTA_RULE_MAX, |
| 3801 | .policy = nft_rule_policy, |
| 3802 | }, |
| 3803 | [NFT_MSG_DELRULE] = { |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 3804 | .call_batch = nf_tables_delrule, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3805 | .attr_count = NFTA_RULE_MAX, |
| 3806 | .policy = nft_rule_policy, |
| 3807 | }, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3808 | [NFT_MSG_NEWSET] = { |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3809 | .call_batch = nf_tables_newset, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3810 | .attr_count = NFTA_SET_MAX, |
| 3811 | .policy = nft_set_policy, |
| 3812 | }, |
| 3813 | [NFT_MSG_GETSET] = { |
| 3814 | .call = nf_tables_getset, |
| 3815 | .attr_count = NFTA_SET_MAX, |
| 3816 | .policy = nft_set_policy, |
| 3817 | }, |
| 3818 | [NFT_MSG_DELSET] = { |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3819 | .call_batch = nf_tables_delset, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3820 | .attr_count = NFTA_SET_MAX, |
| 3821 | .policy = nft_set_policy, |
| 3822 | }, |
| 3823 | [NFT_MSG_NEWSETELEM] = { |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3824 | .call_batch = nf_tables_newsetelem, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3825 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
| 3826 | .policy = nft_set_elem_list_policy, |
| 3827 | }, |
| 3828 | [NFT_MSG_GETSETELEM] = { |
| 3829 | .call = nf_tables_getsetelem, |
| 3830 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
| 3831 | .policy = nft_set_elem_list_policy, |
| 3832 | }, |
| 3833 | [NFT_MSG_DELSETELEM] = { |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3834 | .call_batch = nf_tables_delsetelem, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 3835 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
| 3836 | .policy = nft_set_elem_list_policy, |
| 3837 | }, |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 3838 | [NFT_MSG_GETGEN] = { |
| 3839 | .call = nf_tables_getgen, |
| 3840 | }, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3841 | }; |
| 3842 | |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 3843 | static void nft_chain_commit_update(struct nft_trans *trans) |
| 3844 | { |
| 3845 | struct nft_base_chain *basechain; |
| 3846 | |
| 3847 | if (nft_trans_chain_name(trans)[0]) |
| 3848 | strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
| 3849 | |
| 3850 | if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN)) |
| 3851 | return; |
| 3852 | |
| 3853 | basechain = nft_base_chain(trans->ctx.chain); |
| 3854 | nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans)); |
| 3855 | |
| 3856 | switch (nft_trans_chain_policy(trans)) { |
| 3857 | case NF_DROP: |
| 3858 | case NF_ACCEPT: |
| 3859 | basechain->policy = nft_trans_chain_policy(trans); |
| 3860 | break; |
| 3861 | } |
| 3862 | } |
| 3863 | |
Pablo Neira Ayuso | b326dd3 | 2014-11-10 21:14:12 +0100 | [diff] [blame] | 3864 | static void nf_tables_commit_release(struct nft_trans *trans) |
Pablo Neira Ayuso | c7c32e7 | 2014-04-10 00:31:10 +0200 | [diff] [blame] | 3865 | { |
Pablo Neira Ayuso | c7c32e7 | 2014-04-10 00:31:10 +0200 | [diff] [blame] | 3866 | switch (trans->msg_type) { |
| 3867 | case NFT_MSG_DELTABLE: |
| 3868 | nf_tables_table_destroy(&trans->ctx); |
| 3869 | break; |
| 3870 | case NFT_MSG_DELCHAIN: |
| 3871 | nf_tables_chain_destroy(trans->ctx.chain); |
| 3872 | break; |
| 3873 | case NFT_MSG_DELRULE: |
| 3874 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); |
| 3875 | break; |
| 3876 | case NFT_MSG_DELSET: |
| 3877 | nft_set_destroy(nft_trans_set(trans)); |
| 3878 | break; |
Patrick McHardy | 61edafb | 2015-03-25 14:08:47 +0000 | [diff] [blame] | 3879 | case NFT_MSG_DELSETELEM: |
| 3880 | nft_set_elem_destroy(nft_trans_elem_set(trans), |
| 3881 | nft_trans_elem(trans).priv); |
| 3882 | break; |
Pablo Neira Ayuso | c7c32e7 | 2014-04-10 00:31:10 +0200 | [diff] [blame] | 3883 | } |
| 3884 | kfree(trans); |
| 3885 | } |
| 3886 | |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3887 | static int nf_tables_commit(struct sk_buff *skb) |
| 3888 | { |
| 3889 | struct net *net = sock_net(skb->sk); |
| 3890 | struct nft_trans *trans, *next; |
Pablo Neira Ayuso | a3716e7 | 2014-08-01 19:32:41 +0200 | [diff] [blame] | 3891 | struct nft_trans_elem *te; |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3892 | |
| 3893 | /* Bump generation counter, invalidate any dump in progress */ |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 3894 | while (++net->nft.base_seq == 0); |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3895 | |
| 3896 | /* A new generation has just started */ |
Patrick McHardy | ea4bd99 | 2015-03-25 14:08:49 +0000 | [diff] [blame] | 3897 | net->nft.gencursor = nft_gencursor_next(net); |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3898 | |
| 3899 | /* Make sure all packets have left the previous generation before |
| 3900 | * purging old rules. |
| 3901 | */ |
| 3902 | synchronize_rcu(); |
| 3903 | |
| 3904 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 3905 | switch (trans->msg_type) { |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 3906 | case NFT_MSG_NEWTABLE: |
| 3907 | if (nft_trans_table_update(trans)) { |
| 3908 | if (!nft_trans_table_enable(trans)) { |
| 3909 | nf_tables_table_disable(trans->ctx.afi, |
| 3910 | trans->ctx.table); |
| 3911 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
| 3912 | } |
| 3913 | } else { |
| 3914 | trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE; |
| 3915 | } |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 3916 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 3917 | nft_trans_destroy(trans); |
| 3918 | break; |
| 3919 | case NFT_MSG_DELTABLE: |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 3920 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 3921 | break; |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 3922 | case NFT_MSG_NEWCHAIN: |
| 3923 | if (nft_trans_chain_update(trans)) |
| 3924 | nft_chain_commit_update(trans); |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 3925 | else |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 3926 | trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE; |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 3927 | |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 3928 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 3929 | nft_trans_destroy(trans); |
| 3930 | break; |
| 3931 | case NFT_MSG_DELCHAIN: |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 3932 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
Arturo Borrero | c559879 | 2014-09-02 16:42:23 +0200 | [diff] [blame] | 3933 | nf_tables_unregister_hooks(trans->ctx.table, |
| 3934 | trans->ctx.chain, |
| 3935 | trans->ctx.afi->nops); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 3936 | break; |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 3937 | case NFT_MSG_NEWRULE: |
| 3938 | nft_rule_clear(trans->ctx.net, nft_trans_rule(trans)); |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 3939 | nf_tables_rule_notify(&trans->ctx, |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3940 | nft_trans_rule(trans), |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 3941 | NFT_MSG_NEWRULE); |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3942 | nft_trans_destroy(trans); |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 3943 | break; |
| 3944 | case NFT_MSG_DELRULE: |
| 3945 | list_del_rcu(&nft_trans_rule(trans)->list); |
Pablo Neira Ayuso | 35151d8 | 2014-05-05 17:12:40 +0200 | [diff] [blame] | 3946 | nf_tables_rule_notify(&trans->ctx, |
| 3947 | nft_trans_rule(trans), |
| 3948 | NFT_MSG_DELRULE); |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 3949 | break; |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3950 | case NFT_MSG_NEWSET: |
| 3951 | nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE; |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 3952 | /* This avoids hitting -EBUSY when deleting the table |
| 3953 | * from the transaction. |
| 3954 | */ |
| 3955 | if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS && |
| 3956 | !list_empty(&nft_trans_set(trans)->bindings)) |
| 3957 | trans->ctx.table->use--; |
| 3958 | |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3959 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
Pablo Neira Ayuso | 31f8441 | 2014-05-29 10:29:58 +0200 | [diff] [blame] | 3960 | NFT_MSG_NEWSET, GFP_KERNEL); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3961 | nft_trans_destroy(trans); |
| 3962 | break; |
| 3963 | case NFT_MSG_DELSET: |
| 3964 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
Pablo Neira Ayuso | 31f8441 | 2014-05-29 10:29:58 +0200 | [diff] [blame] | 3965 | NFT_MSG_DELSET, GFP_KERNEL); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 3966 | break; |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3967 | case NFT_MSG_NEWSETELEM: |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 3968 | te = (struct nft_trans_elem *)trans->data; |
| 3969 | |
| 3970 | te->set->ops->activate(te->set, &te->elem); |
| 3971 | nf_tables_setelem_notify(&trans->ctx, te->set, |
| 3972 | &te->elem, |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3973 | NFT_MSG_NEWSETELEM, 0); |
| 3974 | nft_trans_destroy(trans); |
| 3975 | break; |
| 3976 | case NFT_MSG_DELSETELEM: |
Pablo Neira Ayuso | a3716e7 | 2014-08-01 19:32:41 +0200 | [diff] [blame] | 3977 | te = (struct nft_trans_elem *)trans->data; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 3978 | |
Pablo Neira Ayuso | a3716e7 | 2014-08-01 19:32:41 +0200 | [diff] [blame] | 3979 | nf_tables_setelem_notify(&trans->ctx, te->set, |
| 3980 | &te->elem, |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3981 | NFT_MSG_DELSETELEM, 0); |
Pablo Neira Ayuso | 02263db | 2015-02-20 17:11:10 +0100 | [diff] [blame] | 3982 | te->set->ops->remove(te->set, &te->elem); |
Patrick McHardy | 3dd0673 | 2015-04-05 14:41:06 +0200 | [diff] [blame] | 3983 | atomic_dec(&te->set->nelems); |
| 3984 | te->set->ndeact--; |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 3985 | break; |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3986 | } |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3987 | } |
| 3988 | |
Pablo Neira Ayuso | b326dd3 | 2014-11-10 21:14:12 +0100 | [diff] [blame] | 3989 | synchronize_rcu(); |
| 3990 | |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3991 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { |
Pablo Neira Ayuso | c7c32e7 | 2014-04-10 00:31:10 +0200 | [diff] [blame] | 3992 | list_del(&trans->list); |
Pablo Neira Ayuso | b326dd3 | 2014-11-10 21:14:12 +0100 | [diff] [blame] | 3993 | nf_tables_commit_release(trans); |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3994 | } |
| 3995 | |
Pablo Neira Ayuso | 84d7fce | 2014-09-04 14:30:22 +0200 | [diff] [blame] | 3996 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
| 3997 | |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 3998 | return 0; |
| 3999 | } |
| 4000 | |
Pablo Neira Ayuso | b326dd3 | 2014-11-10 21:14:12 +0100 | [diff] [blame] | 4001 | static void nf_tables_abort_release(struct nft_trans *trans) |
Pablo Neira Ayuso | c7c32e7 | 2014-04-10 00:31:10 +0200 | [diff] [blame] | 4002 | { |
Pablo Neira Ayuso | c7c32e7 | 2014-04-10 00:31:10 +0200 | [diff] [blame] | 4003 | switch (trans->msg_type) { |
| 4004 | case NFT_MSG_NEWTABLE: |
| 4005 | nf_tables_table_destroy(&trans->ctx); |
| 4006 | break; |
| 4007 | case NFT_MSG_NEWCHAIN: |
| 4008 | nf_tables_chain_destroy(trans->ctx.chain); |
| 4009 | break; |
| 4010 | case NFT_MSG_NEWRULE: |
| 4011 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); |
| 4012 | break; |
| 4013 | case NFT_MSG_NEWSET: |
| 4014 | nft_set_destroy(nft_trans_set(trans)); |
| 4015 | break; |
Patrick McHardy | 61edafb | 2015-03-25 14:08:47 +0000 | [diff] [blame] | 4016 | case NFT_MSG_NEWSETELEM: |
| 4017 | nft_set_elem_destroy(nft_trans_elem_set(trans), |
| 4018 | nft_trans_elem(trans).priv); |
| 4019 | break; |
Pablo Neira Ayuso | c7c32e7 | 2014-04-10 00:31:10 +0200 | [diff] [blame] | 4020 | } |
| 4021 | kfree(trans); |
| 4022 | } |
| 4023 | |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 4024 | static int nf_tables_abort(struct sk_buff *skb) |
| 4025 | { |
| 4026 | struct net *net = sock_net(skb->sk); |
| 4027 | struct nft_trans *trans, *next; |
Pablo Neira Ayuso | 02263db | 2015-02-20 17:11:10 +0100 | [diff] [blame] | 4028 | struct nft_trans_elem *te; |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 4029 | |
Xin Long | a907e36 | 2015-12-07 18:48:07 +0800 | [diff] [blame] | 4030 | list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list, |
| 4031 | list) { |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 4032 | switch (trans->msg_type) { |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 4033 | case NFT_MSG_NEWTABLE: |
| 4034 | if (nft_trans_table_update(trans)) { |
| 4035 | if (nft_trans_table_enable(trans)) { |
| 4036 | nf_tables_table_disable(trans->ctx.afi, |
| 4037 | trans->ctx.table); |
| 4038 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
| 4039 | } |
| 4040 | nft_trans_destroy(trans); |
| 4041 | } else { |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 4042 | list_del_rcu(&trans->ctx.table->list); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 4043 | } |
| 4044 | break; |
| 4045 | case NFT_MSG_DELTABLE: |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 4046 | list_add_tail_rcu(&trans->ctx.table->list, |
| 4047 | &trans->ctx.afi->tables); |
Pablo Neira Ayuso | 55dd6f9 | 2014-04-03 11:53:37 +0200 | [diff] [blame] | 4048 | nft_trans_destroy(trans); |
| 4049 | break; |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 4050 | case NFT_MSG_NEWCHAIN: |
| 4051 | if (nft_trans_chain_update(trans)) { |
Markus Elfring | 982f405 | 2014-11-18 20:37:05 +0100 | [diff] [blame] | 4052 | free_percpu(nft_trans_chain_stats(trans)); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 4053 | |
| 4054 | nft_trans_destroy(trans); |
| 4055 | } else { |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 4056 | trans->ctx.table->use--; |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 4057 | list_del_rcu(&trans->ctx.chain->list); |
Arturo Borrero | c559879 | 2014-09-02 16:42:23 +0200 | [diff] [blame] | 4058 | nf_tables_unregister_hooks(trans->ctx.table, |
| 4059 | trans->ctx.chain, |
| 4060 | trans->ctx.afi->nops); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 4061 | } |
| 4062 | break; |
| 4063 | case NFT_MSG_DELCHAIN: |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 4064 | trans->ctx.table->use++; |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 4065 | list_add_tail_rcu(&trans->ctx.chain->list, |
| 4066 | &trans->ctx.table->chains); |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 4067 | nft_trans_destroy(trans); |
| 4068 | break; |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 4069 | case NFT_MSG_NEWRULE: |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 4070 | trans->ctx.chain->use--; |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 4071 | list_del_rcu(&nft_trans_rule(trans)->list); |
| 4072 | break; |
| 4073 | case NFT_MSG_DELRULE: |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 4074 | trans->ctx.chain->use++; |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 4075 | nft_rule_clear(trans->ctx.net, nft_trans_rule(trans)); |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 4076 | nft_trans_destroy(trans); |
Pablo Neira Ayuso | b380e5c | 2014-04-04 01:38:51 +0200 | [diff] [blame] | 4077 | break; |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 4078 | case NFT_MSG_NEWSET: |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 4079 | trans->ctx.table->use--; |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 4080 | list_del_rcu(&nft_trans_set(trans)->list); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 4081 | break; |
| 4082 | case NFT_MSG_DELSET: |
Pablo Neira Ayuso | 4fefee5 | 2014-05-23 12:39:26 +0200 | [diff] [blame] | 4083 | trans->ctx.table->use++; |
Pablo Neira Ayuso | e688a7f | 2014-07-01 11:49:18 +0200 | [diff] [blame] | 4084 | list_add_tail_rcu(&nft_trans_set(trans)->list, |
| 4085 | &trans->ctx.table->sets); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 4086 | nft_trans_destroy(trans); |
| 4087 | break; |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 4088 | case NFT_MSG_NEWSETELEM: |
Pablo Neira Ayuso | 02263db | 2015-02-20 17:11:10 +0100 | [diff] [blame] | 4089 | te = (struct nft_trans_elem *)trans->data; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 4090 | |
Pablo Neira Ayuso | 02263db | 2015-02-20 17:11:10 +0100 | [diff] [blame] | 4091 | te->set->ops->remove(te->set, &te->elem); |
Patrick McHardy | 3dd0673 | 2015-04-05 14:41:06 +0200 | [diff] [blame] | 4092 | atomic_dec(&te->set->nelems); |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 4093 | break; |
| 4094 | case NFT_MSG_DELSETELEM: |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 4095 | te = (struct nft_trans_elem *)trans->data; |
| 4096 | |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 4097 | te->set->ops->activate(te->set, &te->elem); |
Patrick McHardy | 3dd0673 | 2015-04-05 14:41:06 +0200 | [diff] [blame] | 4098 | te->set->ndeact--; |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 4099 | |
Pablo Neira Ayuso | 60319eb | 2014-04-04 03:36:42 +0200 | [diff] [blame] | 4100 | nft_trans_destroy(trans); |
| 4101 | break; |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 4102 | } |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 4103 | } |
| 4104 | |
Pablo Neira Ayuso | b326dd3 | 2014-11-10 21:14:12 +0100 | [diff] [blame] | 4105 | synchronize_rcu(); |
| 4106 | |
Pablo Neira Ayuso | a1cee07 | 2014-05-23 11:09:42 +0200 | [diff] [blame] | 4107 | list_for_each_entry_safe_reverse(trans, next, |
| 4108 | &net->nft.commit_list, list) { |
Pablo Neira Ayuso | c7c32e7 | 2014-04-10 00:31:10 +0200 | [diff] [blame] | 4109 | list_del(&trans->list); |
Pablo Neira Ayuso | b326dd3 | 2014-11-10 21:14:12 +0100 | [diff] [blame] | 4110 | nf_tables_abort_release(trans); |
Pablo Neira Ayuso | 37082f9 | 2014-04-03 11:56:37 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | return 0; |
| 4114 | } |
| 4115 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4116 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
| 4117 | .name = "nf_tables", |
| 4118 | .subsys_id = NFNL_SUBSYS_NFTABLES, |
| 4119 | .cb_count = NFT_MSG_MAX, |
| 4120 | .cb = nf_tables_cb, |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 4121 | .commit = nf_tables_commit, |
| 4122 | .abort = nf_tables_abort, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4123 | }; |
| 4124 | |
Pablo Neira Ayuso | 7210e4e | 2014-10-13 19:50:22 +0200 | [diff] [blame] | 4125 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
| 4126 | enum nft_chain_type type) |
| 4127 | { |
| 4128 | const struct nft_base_chain *basechain; |
| 4129 | |
| 4130 | if (chain->flags & NFT_BASE_CHAIN) { |
| 4131 | basechain = nft_base_chain(chain); |
| 4132 | if (basechain->type->type != type) |
| 4133 | return -EOPNOTSUPP; |
| 4134 | } |
| 4135 | return 0; |
| 4136 | } |
| 4137 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); |
| 4138 | |
Pablo Neira Ayuso | 75e8d06 | 2015-01-14 15:33:57 +0100 | [diff] [blame] | 4139 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
| 4140 | unsigned int hook_flags) |
| 4141 | { |
| 4142 | struct nft_base_chain *basechain; |
| 4143 | |
| 4144 | if (chain->flags & NFT_BASE_CHAIN) { |
| 4145 | basechain = nft_base_chain(chain); |
| 4146 | |
| 4147 | if ((1 << basechain->ops[0].hooknum) & hook_flags) |
| 4148 | return 0; |
| 4149 | |
| 4150 | return -EOPNOTSUPP; |
| 4151 | } |
| 4152 | |
| 4153 | return 0; |
| 4154 | } |
| 4155 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); |
| 4156 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4157 | /* |
| 4158 | * Loop detection - walk through the ruleset beginning at the destination chain |
| 4159 | * of a new jump until either the source chain is reached (loop) or all |
| 4160 | * reachable chains have been traversed. |
| 4161 | * |
| 4162 | * The loop check is performed whenever a new jump verdict is added to an |
| 4163 | * expression or verdict map or a verdict map is bound to a new chain. |
| 4164 | */ |
| 4165 | |
| 4166 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
| 4167 | const struct nft_chain *chain); |
| 4168 | |
| 4169 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, |
| 4170 | const struct nft_set *set, |
| 4171 | const struct nft_set_iter *iter, |
| 4172 | const struct nft_set_elem *elem) |
| 4173 | { |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 4174 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
| 4175 | const struct nft_data *data; |
| 4176 | |
| 4177 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
| 4178 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) |
Pablo Neira Ayuso | 62f9c8b | 2014-02-07 14:45:01 +0100 | [diff] [blame] | 4179 | return 0; |
| 4180 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 4181 | data = nft_set_ext_data(ext); |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4182 | switch (data->verdict.code) { |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4183 | case NFT_JUMP: |
| 4184 | case NFT_GOTO: |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4185 | return nf_tables_check_loops(ctx, data->verdict.chain); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4186 | default: |
| 4187 | return 0; |
| 4188 | } |
| 4189 | } |
| 4190 | |
| 4191 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
| 4192 | const struct nft_chain *chain) |
| 4193 | { |
| 4194 | const struct nft_rule *rule; |
| 4195 | const struct nft_expr *expr, *last; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4196 | const struct nft_set *set; |
| 4197 | struct nft_set_binding *binding; |
| 4198 | struct nft_set_iter iter; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4199 | |
| 4200 | if (ctx->chain == chain) |
| 4201 | return -ELOOP; |
| 4202 | |
| 4203 | list_for_each_entry(rule, &chain->rules, list) { |
| 4204 | nft_rule_for_each_expr(expr, last, rule) { |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 4205 | const struct nft_data *data = NULL; |
| 4206 | int err; |
| 4207 | |
| 4208 | if (!expr->ops->validate) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4209 | continue; |
| 4210 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 4211 | err = expr->ops->validate(ctx, expr, &data); |
| 4212 | if (err < 0) |
| 4213 | return err; |
| 4214 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4215 | if (data == NULL) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 4216 | continue; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4217 | |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4218 | switch (data->verdict.code) { |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4219 | case NFT_JUMP: |
| 4220 | case NFT_GOTO: |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4221 | err = nf_tables_check_loops(ctx, |
| 4222 | data->verdict.chain); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4223 | if (err < 0) |
| 4224 | return err; |
| 4225 | default: |
| 4226 | break; |
| 4227 | } |
| 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | list_for_each_entry(set, &ctx->table->sets, list) { |
| 4232 | if (!(set->flags & NFT_SET_MAP) || |
| 4233 | set->dtype != NFT_DATA_VERDICT) |
| 4234 | continue; |
| 4235 | |
| 4236 | list_for_each_entry(binding, &set->bindings, list) { |
Patrick McHardy | 11113e1 | 2015-04-05 14:41:07 +0200 | [diff] [blame] | 4237 | if (!(binding->flags & NFT_SET_MAP) || |
| 4238 | binding->chain != chain) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4239 | continue; |
| 4240 | |
| 4241 | iter.skip = 0; |
| 4242 | iter.count = 0; |
| 4243 | iter.err = 0; |
| 4244 | iter.fn = nf_tables_loop_check_setelem; |
| 4245 | |
| 4246 | set->ops->walk(ctx, set, &iter); |
| 4247 | if (iter.err < 0) |
| 4248 | return iter.err; |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | return 0; |
| 4253 | } |
| 4254 | |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4255 | /** |
| 4256 | * nft_parse_register - parse a register value from a netlink attribute |
| 4257 | * |
| 4258 | * @attr: netlink attribute |
| 4259 | * |
| 4260 | * Parse and translate a register value from a netlink attribute. |
| 4261 | * Registers used to be 128 bit wide, these register numbers will be |
| 4262 | * mapped to the corresponding 32 bit register numbers. |
| 4263 | */ |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 4264 | unsigned int nft_parse_register(const struct nlattr *attr) |
| 4265 | { |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4266 | unsigned int reg; |
| 4267 | |
| 4268 | reg = ntohl(nla_get_be32(attr)); |
| 4269 | switch (reg) { |
| 4270 | case NFT_REG_VERDICT...NFT_REG_4: |
| 4271 | return reg * NFT_REG_SIZE / NFT_REG32_SIZE; |
| 4272 | default: |
| 4273 | return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; |
| 4274 | } |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 4275 | } |
| 4276 | EXPORT_SYMBOL_GPL(nft_parse_register); |
| 4277 | |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4278 | /** |
| 4279 | * nft_dump_register - dump a register value to a netlink attribute |
| 4280 | * |
| 4281 | * @skb: socket buffer |
| 4282 | * @attr: attribute number |
| 4283 | * @reg: register number |
| 4284 | * |
| 4285 | * Construct a netlink attribute containing the register number. For |
| 4286 | * compatibility reasons, register numbers being a multiple of 4 are |
| 4287 | * translated to the corresponding 128 bit register numbers. |
| 4288 | */ |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 4289 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
| 4290 | { |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4291 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
| 4292 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); |
| 4293 | else |
| 4294 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; |
| 4295 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 4296 | return nla_put_be32(skb, attr, htonl(reg)); |
| 4297 | } |
| 4298 | EXPORT_SYMBOL_GPL(nft_dump_register); |
| 4299 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4300 | /** |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 4301 | * nft_validate_register_load - validate a load from a register |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4302 | * |
| 4303 | * @reg: the register number |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 4304 | * @len: the length of the data |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4305 | * |
| 4306 | * Validate that the input register is one of the general purpose |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 4307 | * registers and that the length of the load is within the bounds. |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4308 | */ |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 4309 | int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4310 | { |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4311 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4312 | return -EINVAL; |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 4313 | if (len == 0) |
| 4314 | return -EINVAL; |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4315 | if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data)) |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 4316 | return -ERANGE; |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4317 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4318 | return 0; |
| 4319 | } |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 4320 | EXPORT_SYMBOL_GPL(nft_validate_register_load); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4321 | |
| 4322 | /** |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 4323 | * nft_validate_register_store - validate an expressions' register store |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4324 | * |
| 4325 | * @ctx: context of the expression performing the load |
| 4326 | * @reg: the destination register number |
| 4327 | * @data: the data to load |
| 4328 | * @type: the data type |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 4329 | * @len: the length of the data |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4330 | * |
| 4331 | * Validate that a data load uses the appropriate data type for |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 4332 | * the destination register and the length is within the bounds. |
| 4333 | * A value of NULL for the data means that its runtime gathered |
Patrick McHardy | 58f40ab | 2015-04-11 02:27:28 +0100 | [diff] [blame] | 4334 | * data. |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4335 | */ |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 4336 | int nft_validate_register_store(const struct nft_ctx *ctx, |
| 4337 | enum nft_registers reg, |
| 4338 | const struct nft_data *data, |
| 4339 | enum nft_data_types type, unsigned int len) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4340 | { |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4341 | int err; |
| 4342 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4343 | switch (reg) { |
| 4344 | case NFT_REG_VERDICT: |
Patrick McHardy | 58f40ab | 2015-04-11 02:27:28 +0100 | [diff] [blame] | 4345 | if (type != NFT_DATA_VERDICT) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4346 | return -EINVAL; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4347 | |
Patrick McHardy | 58f40ab | 2015-04-11 02:27:28 +0100 | [diff] [blame] | 4348 | if (data != NULL && |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4349 | (data->verdict.code == NFT_GOTO || |
| 4350 | data->verdict.code == NFT_JUMP)) { |
| 4351 | err = nf_tables_check_loops(ctx, data->verdict.chain); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4352 | if (err < 0) |
| 4353 | return err; |
| 4354 | |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4355 | if (ctx->chain->level + 1 > |
| 4356 | data->verdict.chain->level) { |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4357 | if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE) |
| 4358 | return -EMLINK; |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4359 | data->verdict.chain->level = ctx->chain->level + 1; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 4360 | } |
| 4361 | } |
| 4362 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4363 | return 0; |
| 4364 | default: |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4365 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
Patrick McHardy | 27e6d20 | 2015-04-11 02:27:29 +0100 | [diff] [blame] | 4366 | return -EINVAL; |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 4367 | if (len == 0) |
| 4368 | return -EINVAL; |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 4369 | if (reg * NFT_REG32_SIZE + len > |
| 4370 | FIELD_SIZEOF(struct nft_regs, data)) |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 4371 | return -ERANGE; |
Patrick McHardy | 27e6d20 | 2015-04-11 02:27:29 +0100 | [diff] [blame] | 4372 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4373 | if (data != NULL && type != NFT_DATA_VALUE) |
| 4374 | return -EINVAL; |
| 4375 | return 0; |
| 4376 | } |
| 4377 | } |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 4378 | EXPORT_SYMBOL_GPL(nft_validate_register_store); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4379 | |
| 4380 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { |
| 4381 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, |
| 4382 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, |
| 4383 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, |
| 4384 | }; |
| 4385 | |
| 4386 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, |
| 4387 | struct nft_data_desc *desc, const struct nlattr *nla) |
| 4388 | { |
| 4389 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
| 4390 | struct nft_chain *chain; |
| 4391 | int err; |
| 4392 | |
| 4393 | err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy); |
| 4394 | if (err < 0) |
| 4395 | return err; |
| 4396 | |
| 4397 | if (!tb[NFTA_VERDICT_CODE]) |
| 4398 | return -EINVAL; |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4399 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4400 | |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4401 | switch (data->verdict.code) { |
Patrick McHardy | e0abdad | 2014-02-18 18:06:50 +0000 | [diff] [blame] | 4402 | default: |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4403 | switch (data->verdict.code & NF_VERDICT_MASK) { |
Patrick McHardy | e0abdad | 2014-02-18 18:06:50 +0000 | [diff] [blame] | 4404 | case NF_ACCEPT: |
| 4405 | case NF_DROP: |
| 4406 | case NF_QUEUE: |
| 4407 | break; |
| 4408 | default: |
| 4409 | return -EINVAL; |
| 4410 | } |
| 4411 | /* fall through */ |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4412 | case NFT_CONTINUE: |
| 4413 | case NFT_BREAK: |
| 4414 | case NFT_RETURN: |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4415 | break; |
| 4416 | case NFT_JUMP: |
| 4417 | case NFT_GOTO: |
| 4418 | if (!tb[NFTA_VERDICT_CHAIN]) |
| 4419 | return -EINVAL; |
| 4420 | chain = nf_tables_chain_lookup(ctx->table, |
| 4421 | tb[NFTA_VERDICT_CHAIN]); |
| 4422 | if (IS_ERR(chain)) |
| 4423 | return PTR_ERR(chain); |
| 4424 | if (chain->flags & NFT_BASE_CHAIN) |
| 4425 | return -EOPNOTSUPP; |
| 4426 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4427 | chain->use++; |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4428 | data->verdict.chain = chain; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4429 | break; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4430 | } |
| 4431 | |
Florian Westphal | 4c4ed07 | 2015-04-14 16:44:14 +0200 | [diff] [blame] | 4432 | desc->len = sizeof(data->verdict); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4433 | desc->type = NFT_DATA_VERDICT; |
| 4434 | return 0; |
| 4435 | } |
| 4436 | |
| 4437 | static void nft_verdict_uninit(const struct nft_data *data) |
| 4438 | { |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4439 | switch (data->verdict.code) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4440 | case NFT_JUMP: |
| 4441 | case NFT_GOTO: |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4442 | data->verdict.chain->use--; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4443 | break; |
| 4444 | } |
| 4445 | } |
| 4446 | |
Florian Westphal | 33d5a7b | 2015-11-28 21:53:04 +0100 | [diff] [blame] | 4447 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4448 | { |
| 4449 | struct nlattr *nest; |
| 4450 | |
Florian Westphal | 33d5a7b | 2015-11-28 21:53:04 +0100 | [diff] [blame] | 4451 | nest = nla_nest_start(skb, type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4452 | if (!nest) |
| 4453 | goto nla_put_failure; |
| 4454 | |
Florian Westphal | 33d5a7b | 2015-11-28 21:53:04 +0100 | [diff] [blame] | 4455 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4456 | goto nla_put_failure; |
| 4457 | |
Florian Westphal | 33d5a7b | 2015-11-28 21:53:04 +0100 | [diff] [blame] | 4458 | switch (v->code) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4459 | case NFT_JUMP: |
| 4460 | case NFT_GOTO: |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 4461 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
Florian Westphal | 33d5a7b | 2015-11-28 21:53:04 +0100 | [diff] [blame] | 4462 | v->chain->name)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4463 | goto nla_put_failure; |
| 4464 | } |
| 4465 | nla_nest_end(skb, nest); |
| 4466 | return 0; |
| 4467 | |
| 4468 | nla_put_failure: |
| 4469 | return -1; |
| 4470 | } |
| 4471 | |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 4472 | static int nft_value_init(const struct nft_ctx *ctx, |
| 4473 | struct nft_data *data, unsigned int size, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4474 | struct nft_data_desc *desc, const struct nlattr *nla) |
| 4475 | { |
| 4476 | unsigned int len; |
| 4477 | |
| 4478 | len = nla_len(nla); |
| 4479 | if (len == 0) |
| 4480 | return -EINVAL; |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 4481 | if (len > size) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4482 | return -EOVERFLOW; |
| 4483 | |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 4484 | nla_memcpy(data->data, nla, len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4485 | desc->type = NFT_DATA_VALUE; |
| 4486 | desc->len = len; |
| 4487 | return 0; |
| 4488 | } |
| 4489 | |
| 4490 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, |
| 4491 | unsigned int len) |
| 4492 | { |
| 4493 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); |
| 4494 | } |
| 4495 | |
| 4496 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 4497 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4498 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
| 4499 | }; |
| 4500 | |
| 4501 | /** |
| 4502 | * nft_data_init - parse nf_tables data netlink attributes |
| 4503 | * |
| 4504 | * @ctx: context of the expression using the data |
| 4505 | * @data: destination struct nft_data |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 4506 | * @size: maximum data length |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4507 | * @desc: data description |
| 4508 | * @nla: netlink attribute containing data |
| 4509 | * |
| 4510 | * Parse the netlink data attributes and initialize a struct nft_data. |
| 4511 | * The type and length of data are returned in the data description. |
| 4512 | * |
| 4513 | * The caller can indicate that it only wants to accept data of type |
| 4514 | * NFT_DATA_VALUE by passing NULL for the ctx argument. |
| 4515 | */ |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 4516 | int nft_data_init(const struct nft_ctx *ctx, |
| 4517 | struct nft_data *data, unsigned int size, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4518 | struct nft_data_desc *desc, const struct nlattr *nla) |
| 4519 | { |
| 4520 | struct nlattr *tb[NFTA_DATA_MAX + 1]; |
| 4521 | int err; |
| 4522 | |
| 4523 | err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy); |
| 4524 | if (err < 0) |
| 4525 | return err; |
| 4526 | |
| 4527 | if (tb[NFTA_DATA_VALUE]) |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 4528 | return nft_value_init(ctx, data, size, desc, |
| 4529 | tb[NFTA_DATA_VALUE]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4530 | if (tb[NFTA_DATA_VERDICT] && ctx != NULL) |
| 4531 | return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); |
| 4532 | return -EINVAL; |
| 4533 | } |
| 4534 | EXPORT_SYMBOL_GPL(nft_data_init); |
| 4535 | |
| 4536 | /** |
| 4537 | * nft_data_uninit - release a nft_data item |
| 4538 | * |
| 4539 | * @data: struct nft_data to release |
| 4540 | * @type: type of data |
| 4541 | * |
| 4542 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, |
| 4543 | * all others need to be released by calling this function. |
| 4544 | */ |
| 4545 | void nft_data_uninit(const struct nft_data *data, enum nft_data_types type) |
| 4546 | { |
Mirek Kratochvil | 960bd2c | 2015-05-15 21:15:29 +0200 | [diff] [blame] | 4547 | if (type < NFT_DATA_VERDICT) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4548 | return; |
Mirek Kratochvil | 960bd2c | 2015-05-15 21:15:29 +0200 | [diff] [blame] | 4549 | switch (type) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4550 | case NFT_DATA_VERDICT: |
| 4551 | return nft_verdict_uninit(data); |
| 4552 | default: |
| 4553 | WARN_ON(1); |
| 4554 | } |
| 4555 | } |
| 4556 | EXPORT_SYMBOL_GPL(nft_data_uninit); |
| 4557 | |
| 4558 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, |
| 4559 | enum nft_data_types type, unsigned int len) |
| 4560 | { |
| 4561 | struct nlattr *nest; |
| 4562 | int err; |
| 4563 | |
| 4564 | nest = nla_nest_start(skb, attr); |
| 4565 | if (nest == NULL) |
| 4566 | return -1; |
| 4567 | |
| 4568 | switch (type) { |
| 4569 | case NFT_DATA_VALUE: |
| 4570 | err = nft_value_dump(skb, data, len); |
| 4571 | break; |
| 4572 | case NFT_DATA_VERDICT: |
Florian Westphal | 33d5a7b | 2015-11-28 21:53:04 +0100 | [diff] [blame] | 4573 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4574 | break; |
| 4575 | default: |
| 4576 | err = -EINVAL; |
| 4577 | WARN_ON(1); |
| 4578 | } |
| 4579 | |
| 4580 | nla_nest_end(skb, nest); |
| 4581 | return err; |
| 4582 | } |
| 4583 | EXPORT_SYMBOL_GPL(nft_data_dump); |
| 4584 | |
Pablo Neira Ayuso | df05ef8 | 2015-12-15 19:39:32 +0100 | [diff] [blame^] | 4585 | static int __net_init nf_tables_init_net(struct net *net) |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 4586 | { |
| 4587 | INIT_LIST_HEAD(&net->nft.af_info); |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 4588 | INIT_LIST_HEAD(&net->nft.commit_list); |
Pablo Neira Ayuso | 38e029f | 2014-07-01 12:23:12 +0200 | [diff] [blame] | 4589 | net->nft.base_seq = 1; |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 4590 | return 0; |
| 4591 | } |
| 4592 | |
Pablo Neira Ayuso | df05ef8 | 2015-12-15 19:39:32 +0100 | [diff] [blame^] | 4593 | /* Called by nft_unregister_afinfo() from __net_exit path, nfnl_lock is held. */ |
| 4594 | static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi) |
| 4595 | { |
| 4596 | struct nft_table *table, *nt; |
| 4597 | struct nft_chain *chain, *nc; |
| 4598 | struct nft_rule *rule, *nr; |
| 4599 | struct nft_set *set, *ns; |
| 4600 | struct nft_ctx ctx = { |
| 4601 | .net = net, |
| 4602 | .afi = afi, |
| 4603 | }; |
| 4604 | |
| 4605 | list_for_each_entry_safe(table, nt, &afi->tables, list) { |
| 4606 | list_for_each_entry(chain, &table->chains, list) |
| 4607 | nf_tables_unregister_hooks(table, chain, afi->nops); |
| 4608 | /* No packets are walking on these chains anymore. */ |
| 4609 | ctx.table = table; |
| 4610 | list_for_each_entry(chain, &table->chains, list) { |
| 4611 | ctx.chain = chain; |
| 4612 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { |
| 4613 | list_del(&rule->list); |
| 4614 | chain->use--; |
| 4615 | nf_tables_rule_destroy(&ctx, rule); |
| 4616 | } |
| 4617 | } |
| 4618 | list_for_each_entry_safe(set, ns, &table->sets, list) { |
| 4619 | list_del(&set->list); |
| 4620 | table->use--; |
| 4621 | nft_set_destroy(set); |
| 4622 | } |
| 4623 | list_for_each_entry_safe(chain, nc, &table->chains, list) { |
| 4624 | list_del(&chain->list); |
| 4625 | table->use--; |
| 4626 | nf_tables_chain_destroy(chain); |
| 4627 | } |
| 4628 | list_del(&table->list); |
| 4629 | nf_tables_table_destroy(&ctx); |
| 4630 | } |
| 4631 | } |
| 4632 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 4633 | static struct pernet_operations nf_tables_net_ops = { |
| 4634 | .init = nf_tables_init_net, |
| 4635 | }; |
| 4636 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4637 | static int __init nf_tables_module_init(void) |
| 4638 | { |
| 4639 | int err; |
| 4640 | |
| 4641 | info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS, |
| 4642 | GFP_KERNEL); |
| 4643 | if (info == NULL) { |
| 4644 | err = -ENOMEM; |
| 4645 | goto err1; |
| 4646 | } |
| 4647 | |
| 4648 | err = nf_tables_core_module_init(); |
| 4649 | if (err < 0) |
| 4650 | goto err2; |
| 4651 | |
| 4652 | err = nfnetlink_subsys_register(&nf_tables_subsys); |
| 4653 | if (err < 0) |
| 4654 | goto err3; |
| 4655 | |
| 4656 | pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n"); |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 4657 | return register_pernet_subsys(&nf_tables_net_ops); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4658 | err3: |
| 4659 | nf_tables_core_module_exit(); |
| 4660 | err2: |
| 4661 | kfree(info); |
| 4662 | err1: |
| 4663 | return err; |
| 4664 | } |
| 4665 | |
| 4666 | static void __exit nf_tables_module_exit(void) |
| 4667 | { |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 4668 | unregister_pernet_subsys(&nf_tables_net_ops); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4669 | nfnetlink_subsys_unregister(&nf_tables_subsys); |
Pablo Neira Ayuso | 1b1bc49 | 2014-10-01 13:53:20 +0200 | [diff] [blame] | 4670 | rcu_barrier(); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4671 | nf_tables_core_module_exit(); |
| 4672 | kfree(info); |
| 4673 | } |
| 4674 | |
| 4675 | module_init(nf_tables_module_init); |
| 4676 | module_exit(nf_tables_module_exit); |
| 4677 | |
| 4678 | MODULE_LICENSE("GPL"); |
| 4679 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 4680 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |