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