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