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