| /* |
| * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 as |
| * published by the Free Software Foundation. |
| * |
| * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| */ |
| |
| #include <linux/module.h> |
| #include <linux/init.h> |
| #include <linux/list.h> |
| #include <linux/skbuff.h> |
| #include <linux/netlink.h> |
| #include <linux/vmalloc.h> |
| #include <linux/netfilter.h> |
| #include <linux/netfilter/nfnetlink.h> |
| #include <linux/netfilter/nf_tables.h> |
| #include <net/netfilter/nf_tables_core.h> |
| #include <net/netfilter/nf_tables.h> |
| #include <net/net_namespace.h> |
| #include <net/sock.h> |
| |
| static LIST_HEAD(nf_tables_expressions); |
| static LIST_HEAD(nf_tables_objects); |
| |
| /** |
| * nft_register_afinfo - register nf_tables address family info |
| * |
| * @afi: address family info to register |
| * |
| * Register the address family for use with nf_tables. Returns zero on |
| * success or a negative errno code otherwise. |
| */ |
| int nft_register_afinfo(struct net *net, struct nft_af_info *afi) |
| { |
| INIT_LIST_HEAD(&afi->tables); |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| list_add_tail_rcu(&afi->list, &net->nft.af_info); |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| return 0; |
| } |
| EXPORT_SYMBOL_GPL(nft_register_afinfo); |
| |
| static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi); |
| |
| /** |
| * nft_unregister_afinfo - unregister nf_tables address family info |
| * |
| * @afi: address family info to unregister |
| * |
| * Unregister the address family for use with nf_tables. |
| */ |
| void nft_unregister_afinfo(struct net *net, struct nft_af_info *afi) |
| { |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| __nft_release_afinfo(net, afi); |
| list_del_rcu(&afi->list); |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| } |
| EXPORT_SYMBOL_GPL(nft_unregister_afinfo); |
| |
| static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family) |
| { |
| struct nft_af_info *afi; |
| |
| list_for_each_entry(afi, &net->nft.af_info, list) { |
| if (afi->family == family) |
| return afi; |
| } |
| return NULL; |
| } |
| |
| static struct nft_af_info * |
| nf_tables_afinfo_lookup(struct net *net, int family, bool autoload) |
| { |
| struct nft_af_info *afi; |
| |
| afi = nft_afinfo_lookup(net, family); |
| if (afi != NULL) |
| return afi; |
| #ifdef CONFIG_MODULES |
| if (autoload) { |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| request_module("nft-afinfo-%u", family); |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| afi = nft_afinfo_lookup(net, family); |
| if (afi != NULL) |
| return ERR_PTR(-EAGAIN); |
| } |
| #endif |
| return ERR_PTR(-EAFNOSUPPORT); |
| } |
| |
| static void nft_ctx_init(struct nft_ctx *ctx, |
| struct net *net, |
| const struct sk_buff *skb, |
| const struct nlmsghdr *nlh, |
| struct nft_af_info *afi, |
| struct nft_table *table, |
| struct nft_chain *chain, |
| const struct nlattr * const *nla) |
| { |
| ctx->net = net; |
| ctx->afi = afi; |
| ctx->table = table; |
| ctx->chain = chain; |
| ctx->nla = nla; |
| ctx->portid = NETLINK_CB(skb).portid; |
| ctx->report = nlmsg_report(nlh); |
| ctx->seq = nlh->nlmsg_seq; |
| } |
| |
| static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
| int msg_type, u32 size, gfp_t gfp) |
| { |
| struct nft_trans *trans; |
| |
| trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
| if (trans == NULL) |
| return NULL; |
| |
| trans->msg_type = msg_type; |
| trans->ctx = *ctx; |
| |
| return trans; |
| } |
| |
| static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
| int msg_type, u32 size) |
| { |
| return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); |
| } |
| |
| static void nft_trans_destroy(struct nft_trans *trans) |
| { |
| list_del(&trans->list); |
| kfree(trans); |
| } |
| |
| static int nf_tables_register_hooks(struct net *net, |
| const struct nft_table *table, |
| struct nft_chain *chain, |
| unsigned int hook_nops) |
| { |
| if (table->flags & NFT_TABLE_F_DORMANT || |
| !nft_is_base_chain(chain)) |
| return 0; |
| |
| return nf_register_net_hooks(net, nft_base_chain(chain)->ops, |
| hook_nops); |
| } |
| |
| static void nf_tables_unregister_hooks(struct net *net, |
| const struct nft_table *table, |
| struct nft_chain *chain, |
| unsigned int hook_nops) |
| { |
| if (table->flags & NFT_TABLE_F_DORMANT || |
| !nft_is_base_chain(chain)) |
| return; |
| |
| nf_unregister_net_hooks(net, nft_base_chain(chain)->ops, hook_nops); |
| } |
| |
| static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
| { |
| struct nft_trans *trans; |
| |
| trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); |
| if (trans == NULL) |
| return -ENOMEM; |
| |
| if (msg_type == NFT_MSG_NEWTABLE) |
| nft_activate_next(ctx->net, ctx->table); |
| |
| list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| return 0; |
| } |
| |
| static int nft_deltable(struct nft_ctx *ctx) |
| { |
| int err; |
| |
| err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); |
| if (err < 0) |
| return err; |
| |
| nft_deactivate_next(ctx->net, ctx->table); |
| return err; |
| } |
| |
| static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
| { |
| struct nft_trans *trans; |
| |
| trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); |
| if (trans == NULL) |
| return -ENOMEM; |
| |
| if (msg_type == NFT_MSG_NEWCHAIN) |
| nft_activate_next(ctx->net, ctx->chain); |
| |
| list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| return 0; |
| } |
| |
| static int nft_delchain(struct nft_ctx *ctx) |
| { |
| int err; |
| |
| err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
| if (err < 0) |
| return err; |
| |
| ctx->table->use--; |
| nft_deactivate_next(ctx->net, ctx->chain); |
| |
| return err; |
| } |
| |
| static int |
| nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) |
| { |
| /* You cannot delete the same rule twice */ |
| if (nft_is_active_next(ctx->net, rule)) { |
| nft_deactivate_next(ctx->net, rule); |
| ctx->chain->use--; |
| return 0; |
| } |
| return -ENOENT; |
| } |
| |
| static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, |
| struct nft_rule *rule) |
| { |
| struct nft_trans *trans; |
| |
| trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); |
| if (trans == NULL) |
| return NULL; |
| |
| if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
| nft_trans_rule_id(trans) = |
| ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); |
| } |
| nft_trans_rule(trans) = rule; |
| list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| |
| return trans; |
| } |
| |
| static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) |
| { |
| struct nft_trans *trans; |
| int err; |
| |
| trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); |
| if (trans == NULL) |
| return -ENOMEM; |
| |
| err = nf_tables_delrule_deactivate(ctx, rule); |
| if (err < 0) { |
| nft_trans_destroy(trans); |
| return err; |
| } |
| |
| return 0; |
| } |
| |
| static int nft_delrule_by_chain(struct nft_ctx *ctx) |
| { |
| struct nft_rule *rule; |
| int err; |
| |
| list_for_each_entry(rule, &ctx->chain->rules, list) { |
| err = nft_delrule(ctx, rule); |
| if (err < 0) |
| return err; |
| } |
| return 0; |
| } |
| |
| static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type, |
| struct nft_set *set) |
| { |
| struct nft_trans *trans; |
| |
| trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); |
| if (trans == NULL) |
| return -ENOMEM; |
| |
| if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { |
| nft_trans_set_id(trans) = |
| ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); |
| nft_activate_next(ctx->net, set); |
| } |
| nft_trans_set(trans) = set; |
| list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| |
| return 0; |
| } |
| |
| static int nft_delset(struct nft_ctx *ctx, struct nft_set *set) |
| { |
| int err; |
| |
| err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); |
| if (err < 0) |
| return err; |
| |
| nft_deactivate_next(ctx->net, set); |
| ctx->table->use--; |
| |
| return err; |
| } |
| |
| static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
| struct nft_object *obj) |
| { |
| struct nft_trans *trans; |
| |
| trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); |
| if (trans == NULL) |
| return -ENOMEM; |
| |
| if (msg_type == NFT_MSG_NEWOBJ) |
| nft_activate_next(ctx->net, obj); |
| |
| nft_trans_obj(trans) = obj; |
| list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| |
| return 0; |
| } |
| |
| static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) |
| { |
| int err; |
| |
| err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); |
| if (err < 0) |
| return err; |
| |
| nft_deactivate_next(ctx->net, obj); |
| ctx->table->use--; |
| |
| return err; |
| } |
| |
| /* |
| * Tables |
| */ |
| |
| static struct nft_table *nft_table_lookup(const struct nft_af_info *afi, |
| const struct nlattr *nla, |
| u8 genmask) |
| { |
| struct nft_table *table; |
| |
| list_for_each_entry(table, &afi->tables, list) { |
| if (!nla_strcmp(nla, table->name) && |
| nft_active_genmask(table, genmask)) |
| return table; |
| } |
| return NULL; |
| } |
| |
| static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi, |
| const struct nlattr *nla, |
| u8 genmask) |
| { |
| struct nft_table *table; |
| |
| if (nla == NULL) |
| return ERR_PTR(-EINVAL); |
| |
| table = nft_table_lookup(afi, nla, genmask); |
| if (table != NULL) |
| return table; |
| |
| return ERR_PTR(-ENOENT); |
| } |
| |
| static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
| { |
| return ++table->hgenerator; |
| } |
| |
| static const struct nf_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
| |
| static const struct nf_chain_type * |
| __nf_tables_chain_type_lookup(int family, const struct nlattr *nla) |
| { |
| int i; |
| |
| for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
| if (chain_type[family][i] != NULL && |
| !nla_strcmp(nla, chain_type[family][i]->name)) |
| return chain_type[family][i]; |
| } |
| return NULL; |
| } |
| |
| static const struct nf_chain_type * |
| nf_tables_chain_type_lookup(const struct nft_af_info *afi, |
| const struct nlattr *nla, |
| bool autoload) |
| { |
| const struct nf_chain_type *type; |
| |
| type = __nf_tables_chain_type_lookup(afi->family, nla); |
| if (type != NULL) |
| return type; |
| #ifdef CONFIG_MODULES |
| if (autoload) { |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| request_module("nft-chain-%u-%.*s", afi->family, |
| nla_len(nla), (const char *)nla_data(nla)); |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| type = __nf_tables_chain_type_lookup(afi->family, nla); |
| if (type != NULL) |
| return ERR_PTR(-EAGAIN); |
| } |
| #endif |
| return ERR_PTR(-ENOENT); |
| } |
| |
| static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
| [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
| .len = NFT_TABLE_MAXNAMELEN - 1 }, |
| [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
| }; |
| |
| static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
| u32 portid, u32 seq, int event, u32 flags, |
| int family, const struct nft_table *table) |
| { |
| struct nlmsghdr *nlh; |
| struct nfgenmsg *nfmsg; |
| |
| event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
| nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
| if (nlh == NULL) |
| goto nla_put_failure; |
| |
| nfmsg = nlmsg_data(nlh); |
| nfmsg->nfgen_family = family; |
| nfmsg->version = NFNETLINK_V0; |
| nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
| |
| if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
| nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) || |
| nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use))) |
| goto nla_put_failure; |
| |
| nlmsg_end(skb, nlh); |
| return 0; |
| |
| nla_put_failure: |
| nlmsg_trim(skb, nlh); |
| return -1; |
| } |
| |
| static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
| { |
| struct sk_buff *skb; |
| int err; |
| |
| if (!ctx->report && |
| !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
| return; |
| |
| skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| if (skb == NULL) |
| goto err; |
| |
| err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
| event, 0, ctx->afi->family, ctx->table); |
| if (err < 0) { |
| kfree_skb(skb); |
| goto err; |
| } |
| |
| nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| ctx->report, GFP_KERNEL); |
| return; |
| err: |
| nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
| } |
| |
| static int nf_tables_dump_tables(struct sk_buff *skb, |
| struct netlink_callback *cb) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
| const struct nft_af_info *afi; |
| const struct nft_table *table; |
| unsigned int idx = 0, s_idx = cb->args[0]; |
| struct net *net = sock_net(skb->sk); |
| int family = nfmsg->nfgen_family; |
| |
| rcu_read_lock(); |
| cb->seq = net->nft.base_seq; |
| |
| list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
| if (family != NFPROTO_UNSPEC && family != afi->family) |
| continue; |
| |
| list_for_each_entry_rcu(table, &afi->tables, list) { |
| if (idx < s_idx) |
| goto cont; |
| if (idx > s_idx) |
| memset(&cb->args[1], 0, |
| sizeof(cb->args) - sizeof(cb->args[0])); |
| if (!nft_is_active(net, table)) |
| continue; |
| if (nf_tables_fill_table_info(skb, net, |
| NETLINK_CB(cb->skb).portid, |
| cb->nlh->nlmsg_seq, |
| NFT_MSG_NEWTABLE, |
| NLM_F_MULTI, |
| afi->family, table) < 0) |
| goto done; |
| |
| nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
| cont: |
| idx++; |
| } |
| } |
| done: |
| rcu_read_unlock(); |
| cb->args[0] = idx; |
| return skb->len; |
| } |
| |
| static int nf_tables_gettable(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| u8 genmask = nft_genmask_cur(net); |
| const struct nft_af_info *afi; |
| const struct nft_table *table; |
| struct sk_buff *skb2; |
| int family = nfmsg->nfgen_family; |
| int err; |
| |
| if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| struct netlink_dump_control c = { |
| .dump = nf_tables_dump_tables, |
| }; |
| return netlink_dump_start(nlsk, skb, nlh, &c); |
| } |
| |
| afi = nf_tables_afinfo_lookup(net, family, false); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| |
| skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| if (!skb2) |
| return -ENOMEM; |
| |
| err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
| nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0, |
| family, table); |
| if (err < 0) |
| goto err; |
| |
| return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); |
| |
| err: |
| kfree_skb(skb2); |
| return err; |
| } |
| |
| static void _nf_tables_table_disable(struct net *net, |
| const struct nft_af_info *afi, |
| struct nft_table *table, |
| u32 cnt) |
| { |
| struct nft_chain *chain; |
| u32 i = 0; |
| |
| list_for_each_entry(chain, &table->chains, list) { |
| if (!nft_is_active_next(net, chain)) |
| continue; |
| if (!nft_is_base_chain(chain)) |
| continue; |
| |
| if (cnt && i++ == cnt) |
| break; |
| |
| nf_unregister_net_hooks(net, nft_base_chain(chain)->ops, |
| afi->nops); |
| } |
| } |
| |
| static int nf_tables_table_enable(struct net *net, |
| const struct nft_af_info *afi, |
| struct nft_table *table) |
| { |
| struct nft_chain *chain; |
| int err, i = 0; |
| |
| list_for_each_entry(chain, &table->chains, list) { |
| if (!nft_is_active_next(net, chain)) |
| continue; |
| if (!nft_is_base_chain(chain)) |
| continue; |
| |
| err = nf_register_net_hooks(net, nft_base_chain(chain)->ops, |
| afi->nops); |
| if (err < 0) |
| goto err; |
| |
| i++; |
| } |
| return 0; |
| err: |
| if (i) |
| _nf_tables_table_disable(net, afi, table, i); |
| return err; |
| } |
| |
| static void nf_tables_table_disable(struct net *net, |
| const struct nft_af_info *afi, |
| struct nft_table *table) |
| { |
| _nf_tables_table_disable(net, afi, table, 0); |
| } |
| |
| static int nf_tables_updtable(struct nft_ctx *ctx) |
| { |
| struct nft_trans *trans; |
| u32 flags; |
| int ret = 0; |
| |
| if (!ctx->nla[NFTA_TABLE_FLAGS]) |
| return 0; |
| |
| flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
| if (flags & ~NFT_TABLE_F_DORMANT) |
| return -EINVAL; |
| |
| if (flags == ctx->table->flags) |
| return 0; |
| |
| trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
| sizeof(struct nft_trans_table)); |
| if (trans == NULL) |
| return -ENOMEM; |
| |
| if ((flags & NFT_TABLE_F_DORMANT) && |
| !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { |
| nft_trans_table_enable(trans) = false; |
| } else if (!(flags & NFT_TABLE_F_DORMANT) && |
| ctx->table->flags & NFT_TABLE_F_DORMANT) { |
| ret = nf_tables_table_enable(ctx->net, ctx->afi, ctx->table); |
| if (ret >= 0) { |
| ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
| nft_trans_table_enable(trans) = true; |
| } |
| } |
| if (ret < 0) |
| goto err; |
| |
| nft_trans_table_update(trans) = true; |
| list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| return 0; |
| err: |
| nft_trans_destroy(trans); |
| return ret; |
| } |
| |
| static int nf_tables_newtable(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| u8 genmask = nft_genmask_next(net); |
| const struct nlattr *name; |
| struct nft_af_info *afi; |
| struct nft_table *table; |
| int family = nfmsg->nfgen_family; |
| u32 flags = 0; |
| struct nft_ctx ctx; |
| int err; |
| |
| afi = nf_tables_afinfo_lookup(net, family, true); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| name = nla[NFTA_TABLE_NAME]; |
| table = nf_tables_table_lookup(afi, name, genmask); |
| if (IS_ERR(table)) { |
| if (PTR_ERR(table) != -ENOENT) |
| return PTR_ERR(table); |
| } else { |
| if (nlh->nlmsg_flags & NLM_F_EXCL) |
| return -EEXIST; |
| if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| return -EOPNOTSUPP; |
| |
| nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); |
| return nf_tables_updtable(&ctx); |
| } |
| |
| if (nla[NFTA_TABLE_FLAGS]) { |
| flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); |
| if (flags & ~NFT_TABLE_F_DORMANT) |
| return -EINVAL; |
| } |
| |
| err = -EAFNOSUPPORT; |
| if (!try_module_get(afi->owner)) |
| goto err1; |
| |
| err = -ENOMEM; |
| table = kzalloc(sizeof(*table), GFP_KERNEL); |
| if (table == NULL) |
| goto err2; |
| |
| table->name = nla_strdup(name, GFP_KERNEL); |
| if (table->name == NULL) |
| goto err3; |
| |
| INIT_LIST_HEAD(&table->chains); |
| INIT_LIST_HEAD(&table->sets); |
| INIT_LIST_HEAD(&table->objects); |
| table->flags = flags; |
| |
| nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); |
| err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
| if (err < 0) |
| goto err4; |
| |
| list_add_tail_rcu(&table->list, &afi->tables); |
| return 0; |
| err4: |
| kfree(table->name); |
| err3: |
| kfree(table); |
| err2: |
| module_put(afi->owner); |
| err1: |
| return err; |
| } |
| |
| static int nft_flush_table(struct nft_ctx *ctx) |
| { |
| int err; |
| struct nft_chain *chain, *nc; |
| struct nft_object *obj, *ne; |
| struct nft_set *set, *ns; |
| |
| list_for_each_entry(chain, &ctx->table->chains, list) { |
| if (!nft_is_active_next(ctx->net, chain)) |
| continue; |
| |
| ctx->chain = chain; |
| |
| err = nft_delrule_by_chain(ctx); |
| if (err < 0) |
| goto out; |
| } |
| |
| list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { |
| if (!nft_is_active_next(ctx->net, set)) |
| continue; |
| |
| if (set->flags & NFT_SET_ANONYMOUS && |
| !list_empty(&set->bindings)) |
| continue; |
| |
| err = nft_delset(ctx, set); |
| if (err < 0) |
| goto out; |
| } |
| |
| list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
| err = nft_delobj(ctx, obj); |
| if (err < 0) |
| goto out; |
| } |
| |
| list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
| if (!nft_is_active_next(ctx->net, chain)) |
| continue; |
| |
| ctx->chain = chain; |
| |
| err = nft_delchain(ctx); |
| if (err < 0) |
| goto out; |
| } |
| |
| err = nft_deltable(ctx); |
| out: |
| return err; |
| } |
| |
| static int nft_flush(struct nft_ctx *ctx, int family) |
| { |
| struct nft_af_info *afi; |
| struct nft_table *table, *nt; |
| const struct nlattr * const *nla = ctx->nla; |
| int err = 0; |
| |
| list_for_each_entry(afi, &ctx->net->nft.af_info, list) { |
| if (family != AF_UNSPEC && afi->family != family) |
| continue; |
| |
| ctx->afi = afi; |
| list_for_each_entry_safe(table, nt, &afi->tables, list) { |
| if (!nft_is_active_next(ctx->net, table)) |
| continue; |
| |
| if (nla[NFTA_TABLE_NAME] && |
| nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) |
| continue; |
| |
| ctx->table = table; |
| |
| err = nft_flush_table(ctx); |
| if (err < 0) |
| goto out; |
| } |
| } |
| out: |
| return err; |
| } |
| |
| static int nf_tables_deltable(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| u8 genmask = nft_genmask_next(net); |
| struct nft_af_info *afi; |
| struct nft_table *table; |
| int family = nfmsg->nfgen_family; |
| struct nft_ctx ctx; |
| |
| nft_ctx_init(&ctx, net, skb, nlh, NULL, NULL, NULL, nla); |
| if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL) |
| return nft_flush(&ctx, family); |
| |
| afi = nf_tables_afinfo_lookup(net, family, false); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| |
| if (nlh->nlmsg_flags & NLM_F_NONREC && |
| table->use > 0) |
| return -EBUSY; |
| |
| ctx.afi = afi; |
| ctx.table = table; |
| |
| return nft_flush_table(&ctx); |
| } |
| |
| static void nf_tables_table_destroy(struct nft_ctx *ctx) |
| { |
| BUG_ON(ctx->table->use > 0); |
| |
| kfree(ctx->table->name); |
| kfree(ctx->table); |
| module_put(ctx->afi->owner); |
| } |
| |
| int nft_register_chain_type(const struct nf_chain_type *ctype) |
| { |
| int err = 0; |
| |
| if (WARN_ON(ctype->family >= NFPROTO_NUMPROTO)) |
| return -EINVAL; |
| |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| if (chain_type[ctype->family][ctype->type] != NULL) { |
| err = -EBUSY; |
| goto out; |
| } |
| chain_type[ctype->family][ctype->type] = ctype; |
| out: |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| return err; |
| } |
| EXPORT_SYMBOL_GPL(nft_register_chain_type); |
| |
| void nft_unregister_chain_type(const struct nf_chain_type *ctype) |
| { |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| chain_type[ctype->family][ctype->type] = NULL; |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| } |
| EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
| |
| /* |
| * Chains |
| */ |
| |
| static struct nft_chain * |
| nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle, |
| u8 genmask) |
| { |
| struct nft_chain *chain; |
| |
| list_for_each_entry(chain, &table->chains, list) { |
| if (chain->handle == handle && |
| nft_active_genmask(chain, genmask)) |
| return chain; |
| } |
| |
| return ERR_PTR(-ENOENT); |
| } |
| |
| static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table, |
| const struct nlattr *nla, |
| u8 genmask) |
| { |
| struct nft_chain *chain; |
| |
| if (nla == NULL) |
| return ERR_PTR(-EINVAL); |
| |
| list_for_each_entry(chain, &table->chains, list) { |
| if (!nla_strcmp(nla, chain->name) && |
| nft_active_genmask(chain, genmask)) |
| return chain; |
| } |
| |
| return ERR_PTR(-ENOENT); |
| } |
| |
| static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { |
| [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
| .len = NFT_TABLE_MAXNAMELEN - 1 }, |
| [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
| [NFTA_CHAIN_NAME] = { .type = NLA_STRING, |
| .len = NFT_CHAIN_MAXNAMELEN - 1 }, |
| [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, |
| [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
| [NFTA_CHAIN_TYPE] = { .type = NLA_STRING }, |
| [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
| }; |
| |
| static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { |
| [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, |
| [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, |
| [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
| .len = IFNAMSIZ - 1 }, |
| }; |
| |
| static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
| { |
| struct nft_stats *cpu_stats, total; |
| struct nlattr *nest; |
| unsigned int seq; |
| u64 pkts, bytes; |
| int cpu; |
| |
| memset(&total, 0, sizeof(total)); |
| for_each_possible_cpu(cpu) { |
| cpu_stats = per_cpu_ptr(stats, cpu); |
| do { |
| seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
| pkts = cpu_stats->pkts; |
| bytes = cpu_stats->bytes; |
| } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); |
| total.pkts += pkts; |
| total.bytes += bytes; |
| } |
| nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS); |
| if (nest == NULL) |
| goto nla_put_failure; |
| |
| if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
| NFTA_COUNTER_PAD) || |
| nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), |
| NFTA_COUNTER_PAD)) |
| goto nla_put_failure; |
| |
| nla_nest_end(skb, nest); |
| return 0; |
| |
| nla_put_failure: |
| return -ENOSPC; |
| } |
| |
| static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
| u32 portid, u32 seq, int event, u32 flags, |
| int family, const struct nft_table *table, |
| const struct nft_chain *chain) |
| { |
| struct nlmsghdr *nlh; |
| struct nfgenmsg *nfmsg; |
| |
| event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
| nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
| if (nlh == NULL) |
| goto nla_put_failure; |
| |
| nfmsg = nlmsg_data(nlh); |
| nfmsg->nfgen_family = family; |
| nfmsg->version = NFNETLINK_V0; |
| nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
| |
| if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) |
| goto nla_put_failure; |
| if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), |
| NFTA_CHAIN_PAD)) |
| goto nla_put_failure; |
| if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) |
| goto nla_put_failure; |
| |
| if (nft_is_base_chain(chain)) { |
| const struct nft_base_chain *basechain = nft_base_chain(chain); |
| const struct nf_hook_ops *ops = &basechain->ops[0]; |
| struct nlattr *nest; |
| |
| nest = nla_nest_start(skb, NFTA_CHAIN_HOOK); |
| if (nest == NULL) |
| goto nla_put_failure; |
| if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) |
| goto nla_put_failure; |
| if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) |
| goto nla_put_failure; |
| if (basechain->dev_name[0] && |
| nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name)) |
| goto nla_put_failure; |
| nla_nest_end(skb, nest); |
| |
| if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
| htonl(basechain->policy))) |
| goto nla_put_failure; |
| |
| if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
| goto nla_put_failure; |
| |
| if (basechain->stats && nft_dump_stats(skb, basechain->stats)) |
| goto nla_put_failure; |
| } |
| |
| if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
| goto nla_put_failure; |
| |
| nlmsg_end(skb, nlh); |
| return 0; |
| |
| nla_put_failure: |
| nlmsg_trim(skb, nlh); |
| return -1; |
| } |
| |
| static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
| { |
| struct sk_buff *skb; |
| int err; |
| |
| if (!ctx->report && |
| !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
| return; |
| |
| skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| if (skb == NULL) |
| goto err; |
| |
| err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
| event, 0, ctx->afi->family, ctx->table, |
| ctx->chain); |
| if (err < 0) { |
| kfree_skb(skb); |
| goto err; |
| } |
| |
| nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| ctx->report, GFP_KERNEL); |
| return; |
| err: |
| nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
| } |
| |
| static int nf_tables_dump_chains(struct sk_buff *skb, |
| struct netlink_callback *cb) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
| const struct nft_af_info *afi; |
| const struct nft_table *table; |
| const struct nft_chain *chain; |
| unsigned int idx = 0, s_idx = cb->args[0]; |
| struct net *net = sock_net(skb->sk); |
| int family = nfmsg->nfgen_family; |
| |
| rcu_read_lock(); |
| cb->seq = net->nft.base_seq; |
| |
| list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
| if (family != NFPROTO_UNSPEC && family != afi->family) |
| continue; |
| |
| list_for_each_entry_rcu(table, &afi->tables, list) { |
| list_for_each_entry_rcu(chain, &table->chains, list) { |
| if (idx < s_idx) |
| goto cont; |
| if (idx > s_idx) |
| memset(&cb->args[1], 0, |
| sizeof(cb->args) - sizeof(cb->args[0])); |
| if (!nft_is_active(net, chain)) |
| continue; |
| if (nf_tables_fill_chain_info(skb, net, |
| NETLINK_CB(cb->skb).portid, |
| cb->nlh->nlmsg_seq, |
| NFT_MSG_NEWCHAIN, |
| NLM_F_MULTI, |
| afi->family, table, chain) < 0) |
| goto done; |
| |
| nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
| cont: |
| idx++; |
| } |
| } |
| } |
| done: |
| rcu_read_unlock(); |
| cb->args[0] = idx; |
| return skb->len; |
| } |
| |
| static int nf_tables_getchain(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| u8 genmask = nft_genmask_cur(net); |
| const struct nft_af_info *afi; |
| const struct nft_table *table; |
| const struct nft_chain *chain; |
| struct sk_buff *skb2; |
| int family = nfmsg->nfgen_family; |
| int err; |
| |
| if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| struct netlink_dump_control c = { |
| .dump = nf_tables_dump_chains, |
| }; |
| return netlink_dump_start(nlsk, skb, nlh, &c); |
| } |
| |
| afi = nf_tables_afinfo_lookup(net, family, false); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| |
| chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask); |
| if (IS_ERR(chain)) |
| return PTR_ERR(chain); |
| |
| skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| if (!skb2) |
| return -ENOMEM; |
| |
| err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
| nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0, |
| family, table, chain); |
| if (err < 0) |
| goto err; |
| |
| return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); |
| |
| err: |
| kfree_skb(skb2); |
| return err; |
| } |
| |
| static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
| [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, |
| [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, |
| }; |
| |
| static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
| { |
| struct nlattr *tb[NFTA_COUNTER_MAX+1]; |
| struct nft_stats __percpu *newstats; |
| struct nft_stats *stats; |
| int err; |
| |
| err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy, |
| NULL); |
| if (err < 0) |
| return ERR_PTR(err); |
| |
| if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) |
| return ERR_PTR(-EINVAL); |
| |
| newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
| if (newstats == NULL) |
| return ERR_PTR(-ENOMEM); |
| |
| /* Restore old counters on this cpu, no problem. Per-cpu statistics |
| * are not exposed to userspace. |
| */ |
| preempt_disable(); |
| stats = this_cpu_ptr(newstats); |
| stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); |
| stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); |
| preempt_enable(); |
| |
| return newstats; |
| } |
| |
| static void nft_chain_stats_replace(struct nft_base_chain *chain, |
| struct nft_stats __percpu *newstats) |
| { |
| if (newstats == NULL) |
| return; |
| |
| if (chain->stats) { |
| struct nft_stats __percpu *oldstats = |
| nft_dereference(chain->stats); |
| |
| rcu_assign_pointer(chain->stats, newstats); |
| synchronize_rcu(); |
| free_percpu(oldstats); |
| } else |
| rcu_assign_pointer(chain->stats, newstats); |
| } |
| |
| static void nf_tables_chain_destroy(struct nft_chain *chain) |
| { |
| BUG_ON(chain->use > 0); |
| |
| if (nft_is_base_chain(chain)) { |
| struct nft_base_chain *basechain = nft_base_chain(chain); |
| |
| module_put(basechain->type->owner); |
| free_percpu(basechain->stats); |
| if (basechain->stats) |
| static_branch_dec(&nft_counters_enabled); |
| if (basechain->ops[0].dev != NULL) |
| dev_put(basechain->ops[0].dev); |
| kfree(chain->name); |
| kfree(basechain); |
| } else { |
| kfree(chain->name); |
| kfree(chain); |
| } |
| } |
| |
| struct nft_chain_hook { |
| u32 num; |
| u32 priority; |
| const struct nf_chain_type *type; |
| struct net_device *dev; |
| }; |
| |
| static int nft_chain_parse_hook(struct net *net, |
| const struct nlattr * const nla[], |
| struct nft_af_info *afi, |
| struct nft_chain_hook *hook, bool create) |
| { |
| struct nlattr *ha[NFTA_HOOK_MAX + 1]; |
| const struct nf_chain_type *type; |
| struct net_device *dev; |
| int err; |
| |
| err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK], |
| nft_hook_policy, NULL); |
| if (err < 0) |
| return err; |
| |
| if (ha[NFTA_HOOK_HOOKNUM] == NULL || |
| ha[NFTA_HOOK_PRIORITY] == NULL) |
| return -EINVAL; |
| |
| hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); |
| if (hook->num >= afi->nhooks) |
| return -EINVAL; |
| |
| hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); |
| |
| type = chain_type[afi->family][NFT_CHAIN_T_DEFAULT]; |
| if (nla[NFTA_CHAIN_TYPE]) { |
| type = nf_tables_chain_type_lookup(afi, nla[NFTA_CHAIN_TYPE], |
| create); |
| if (IS_ERR(type)) |
| return PTR_ERR(type); |
| } |
| if (!(type->hook_mask & (1 << hook->num))) |
| return -EOPNOTSUPP; |
| if (!try_module_get(type->owner)) |
| return -ENOENT; |
| |
| hook->type = type; |
| |
| hook->dev = NULL; |
| if (afi->flags & NFT_AF_NEEDS_DEV) { |
| char ifname[IFNAMSIZ]; |
| |
| if (!ha[NFTA_HOOK_DEV]) { |
| module_put(type->owner); |
| return -EOPNOTSUPP; |
| } |
| |
| nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ); |
| dev = dev_get_by_name(net, ifname); |
| if (!dev) { |
| module_put(type->owner); |
| return -ENOENT; |
| } |
| hook->dev = dev; |
| } else if (ha[NFTA_HOOK_DEV]) { |
| module_put(type->owner); |
| return -EOPNOTSUPP; |
| } |
| |
| return 0; |
| } |
| |
| static void nft_chain_release_hook(struct nft_chain_hook *hook) |
| { |
| module_put(hook->type->owner); |
| if (hook->dev != NULL) |
| dev_put(hook->dev); |
| } |
| |
| static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
| u8 policy, bool create) |
| { |
| const struct nlattr * const *nla = ctx->nla; |
| struct nft_table *table = ctx->table; |
| struct nft_af_info *afi = ctx->afi; |
| struct nft_base_chain *basechain; |
| struct nft_stats __percpu *stats; |
| struct net *net = ctx->net; |
| struct nft_chain *chain; |
| unsigned int i; |
| int err; |
| |
| if (table->use == UINT_MAX) |
| return -EOVERFLOW; |
| |
| if (nla[NFTA_CHAIN_HOOK]) { |
| struct nft_chain_hook hook; |
| struct nf_hook_ops *ops; |
| nf_hookfn *hookfn; |
| |
| err = nft_chain_parse_hook(net, nla, afi, &hook, create); |
| if (err < 0) |
| return err; |
| |
| basechain = kzalloc(sizeof(*basechain), GFP_KERNEL); |
| if (basechain == NULL) { |
| nft_chain_release_hook(&hook); |
| return -ENOMEM; |
| } |
| |
| if (hook.dev != NULL) |
| strncpy(basechain->dev_name, hook.dev->name, IFNAMSIZ); |
| |
| if (nla[NFTA_CHAIN_COUNTERS]) { |
| stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); |
| if (IS_ERR(stats)) { |
| nft_chain_release_hook(&hook); |
| kfree(basechain); |
| return PTR_ERR(stats); |
| } |
| basechain->stats = stats; |
| static_branch_inc(&nft_counters_enabled); |
| } |
| |
| hookfn = hook.type->hooks[hook.num]; |
| basechain->type = hook.type; |
| chain = &basechain->chain; |
| |
| for (i = 0; i < afi->nops; i++) { |
| ops = &basechain->ops[i]; |
| ops->pf = family; |
| ops->hooknum = hook.num; |
| ops->priority = hook.priority; |
| ops->priv = chain; |
| ops->hook = afi->hooks[ops->hooknum]; |
| ops->dev = hook.dev; |
| if (hookfn) |
| ops->hook = hookfn; |
| if (afi->hook_ops_init) |
| afi->hook_ops_init(ops, i); |
| } |
| |
| chain->flags |= NFT_BASE_CHAIN; |
| basechain->policy = policy; |
| } else { |
| chain = kzalloc(sizeof(*chain), GFP_KERNEL); |
| if (chain == NULL) |
| return -ENOMEM; |
| } |
| INIT_LIST_HEAD(&chain->rules); |
| chain->handle = nf_tables_alloc_handle(table); |
| chain->table = table; |
| chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); |
| if (!chain->name) { |
| err = -ENOMEM; |
| goto err1; |
| } |
| |
| err = nf_tables_register_hooks(net, table, chain, afi->nops); |
| if (err < 0) |
| goto err1; |
| |
| ctx->chain = chain; |
| err = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
| if (err < 0) |
| goto err2; |
| |
| table->use++; |
| list_add_tail_rcu(&chain->list, &table->chains); |
| |
| return 0; |
| err2: |
| nf_tables_unregister_hooks(net, table, chain, afi->nops); |
| err1: |
| nf_tables_chain_destroy(chain); |
| |
| return err; |
| } |
| |
| static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
| bool create) |
| { |
| const struct nlattr * const *nla = ctx->nla; |
| struct nft_table *table = ctx->table; |
| struct nft_chain *chain = ctx->chain; |
| struct nft_af_info *afi = ctx->afi; |
| struct nft_base_chain *basechain; |
| struct nft_stats *stats = NULL; |
| struct nft_chain_hook hook; |
| const struct nlattr *name; |
| struct nf_hook_ops *ops; |
| struct nft_trans *trans; |
| int err, i; |
| |
| if (nla[NFTA_CHAIN_HOOK]) { |
| if (!nft_is_base_chain(chain)) |
| return -EBUSY; |
| |
| err = nft_chain_parse_hook(ctx->net, nla, ctx->afi, &hook, |
| create); |
| if (err < 0) |
| return err; |
| |
| basechain = nft_base_chain(chain); |
| if (basechain->type != hook.type) { |
| nft_chain_release_hook(&hook); |
| return -EBUSY; |
| } |
| |
| for (i = 0; i < afi->nops; i++) { |
| ops = &basechain->ops[i]; |
| if (ops->hooknum != hook.num || |
| ops->priority != hook.priority || |
| ops->dev != hook.dev) { |
| nft_chain_release_hook(&hook); |
| return -EBUSY; |
| } |
| } |
| nft_chain_release_hook(&hook); |
| } |
| |
| if (nla[NFTA_CHAIN_HANDLE] && |
| nla[NFTA_CHAIN_NAME]) { |
| struct nft_chain *chain2; |
| |
| chain2 = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], |
| genmask); |
| if (!IS_ERR(chain2)) |
| return -EEXIST; |
| } |
| |
| if (nla[NFTA_CHAIN_COUNTERS]) { |
| if (!nft_is_base_chain(chain)) |
| return -EOPNOTSUPP; |
| |
| stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); |
| if (IS_ERR(stats)) |
| return PTR_ERR(stats); |
| } |
| |
| trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
| sizeof(struct nft_trans_chain)); |
| if (trans == NULL) { |
| free_percpu(stats); |
| return -ENOMEM; |
| } |
| |
| nft_trans_chain_stats(trans) = stats; |
| nft_trans_chain_update(trans) = true; |
| |
| if (nla[NFTA_CHAIN_POLICY]) |
| nft_trans_chain_policy(trans) = policy; |
| else |
| nft_trans_chain_policy(trans) = -1; |
| |
| name = nla[NFTA_CHAIN_NAME]; |
| if (nla[NFTA_CHAIN_HANDLE] && name) { |
| nft_trans_chain_name(trans) = |
| nla_strdup(name, GFP_KERNEL); |
| if (!nft_trans_chain_name(trans)) { |
| kfree(trans); |
| free_percpu(stats); |
| return -ENOMEM; |
| } |
| } |
| list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
| |
| return 0; |
| } |
| |
| static int nf_tables_newchain(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| const struct nlattr * uninitialized_var(name); |
| u8 genmask = nft_genmask_next(net); |
| int family = nfmsg->nfgen_family; |
| struct nft_af_info *afi; |
| struct nft_table *table; |
| struct nft_chain *chain; |
| u8 policy = NF_ACCEPT; |
| struct nft_ctx ctx; |
| u64 handle = 0; |
| bool create; |
| |
| create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; |
| |
| afi = nf_tables_afinfo_lookup(net, family, true); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| |
| chain = NULL; |
| name = nla[NFTA_CHAIN_NAME]; |
| |
| if (nla[NFTA_CHAIN_HANDLE]) { |
| handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); |
| chain = nf_tables_chain_lookup_byhandle(table, handle, genmask); |
| if (IS_ERR(chain)) |
| return PTR_ERR(chain); |
| } else { |
| chain = nf_tables_chain_lookup(table, name, genmask); |
| if (IS_ERR(chain)) { |
| if (PTR_ERR(chain) != -ENOENT) |
| return PTR_ERR(chain); |
| chain = NULL; |
| } |
| } |
| |
| if (nla[NFTA_CHAIN_POLICY]) { |
| if (chain != NULL && |
| !nft_is_base_chain(chain)) |
| return -EOPNOTSUPP; |
| |
| if (chain == NULL && |
| nla[NFTA_CHAIN_HOOK] == NULL) |
| return -EOPNOTSUPP; |
| |
| policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
| switch (policy) { |
| case NF_DROP: |
| case NF_ACCEPT: |
| break; |
| default: |
| return -EINVAL; |
| } |
| } |
| |
| nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
| |
| if (chain != NULL) { |
| if (nlh->nlmsg_flags & NLM_F_EXCL) |
| return -EEXIST; |
| if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| return -EOPNOTSUPP; |
| |
| return nf_tables_updchain(&ctx, genmask, policy, create); |
| } |
| |
| return nf_tables_addchain(&ctx, family, genmask, policy, create); |
| } |
| |
| static int nf_tables_delchain(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| u8 genmask = nft_genmask_next(net); |
| struct nft_af_info *afi; |
| struct nft_table *table; |
| struct nft_chain *chain; |
| struct nft_rule *rule; |
| int family = nfmsg->nfgen_family; |
| struct nft_ctx ctx; |
| u32 use; |
| int err; |
| |
| afi = nf_tables_afinfo_lookup(net, family, false); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| |
| chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask); |
| if (IS_ERR(chain)) |
| return PTR_ERR(chain); |
| |
| if (nlh->nlmsg_flags & NLM_F_NONREC && |
| chain->use > 0) |
| return -EBUSY; |
| |
| nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
| |
| use = chain->use; |
| list_for_each_entry(rule, &chain->rules, list) { |
| if (!nft_is_active_next(net, rule)) |
| continue; |
| use--; |
| |
| err = nft_delrule(&ctx, rule); |
| if (err < 0) |
| return err; |
| } |
| |
| /* There are rules and elements that are still holding references to us, |
| * we cannot do a recursive removal in this case. |
| */ |
| if (use > 0) |
| return -EBUSY; |
| |
| return nft_delchain(&ctx); |
| } |
| |
| /* |
| * Expressions |
| */ |
| |
| /** |
| * nft_register_expr - register nf_tables expr type |
| * @ops: expr type |
| * |
| * Registers the expr type for use with nf_tables. Returns zero on |
| * success or a negative errno code otherwise. |
| */ |
| int nft_register_expr(struct nft_expr_type *type) |
| { |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| if (type->family == NFPROTO_UNSPEC) |
| list_add_tail_rcu(&type->list, &nf_tables_expressions); |
| else |
| list_add_rcu(&type->list, &nf_tables_expressions); |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| return 0; |
| } |
| EXPORT_SYMBOL_GPL(nft_register_expr); |
| |
| /** |
| * nft_unregister_expr - unregister nf_tables expr type |
| * @ops: expr type |
| * |
| * Unregisters the expr typefor use with nf_tables. |
| */ |
| void nft_unregister_expr(struct nft_expr_type *type) |
| { |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| list_del_rcu(&type->list); |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| } |
| EXPORT_SYMBOL_GPL(nft_unregister_expr); |
| |
| static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
| struct nlattr *nla) |
| { |
| const struct nft_expr_type *type; |
| |
| list_for_each_entry(type, &nf_tables_expressions, list) { |
| if (!nla_strcmp(nla, type->name) && |
| (!type->family || type->family == family)) |
| return type; |
| } |
| return NULL; |
| } |
| |
| static const struct nft_expr_type *nft_expr_type_get(u8 family, |
| struct nlattr *nla) |
| { |
| const struct nft_expr_type *type; |
| |
| if (nla == NULL) |
| return ERR_PTR(-EINVAL); |
| |
| type = __nft_expr_type_get(family, nla); |
| if (type != NULL && try_module_get(type->owner)) |
| return type; |
| |
| #ifdef CONFIG_MODULES |
| if (type == NULL) { |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| request_module("nft-expr-%u-%.*s", family, |
| nla_len(nla), (char *)nla_data(nla)); |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| if (__nft_expr_type_get(family, nla)) |
| return ERR_PTR(-EAGAIN); |
| |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| request_module("nft-expr-%.*s", |
| nla_len(nla), (char *)nla_data(nla)); |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| if (__nft_expr_type_get(family, nla)) |
| return ERR_PTR(-EAGAIN); |
| } |
| #endif |
| return ERR_PTR(-ENOENT); |
| } |
| |
| static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { |
| [NFTA_EXPR_NAME] = { .type = NLA_STRING }, |
| [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
| }; |
| |
| static int nf_tables_fill_expr_info(struct sk_buff *skb, |
| const struct nft_expr *expr) |
| { |
| if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
| goto nla_put_failure; |
| |
| if (expr->ops->dump) { |
| struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA); |
| if (data == NULL) |
| goto nla_put_failure; |
| if (expr->ops->dump(skb, expr) < 0) |
| goto nla_put_failure; |
| nla_nest_end(skb, data); |
| } |
| |
| return skb->len; |
| |
| nla_put_failure: |
| return -1; |
| }; |
| |
| int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
| const struct nft_expr *expr) |
| { |
| struct nlattr *nest; |
| |
| nest = nla_nest_start(skb, attr); |
| if (!nest) |
| goto nla_put_failure; |
| if (nf_tables_fill_expr_info(skb, expr) < 0) |
| goto nla_put_failure; |
| nla_nest_end(skb, nest); |
| return 0; |
| |
| nla_put_failure: |
| return -1; |
| } |
| |
| struct nft_expr_info { |
| const struct nft_expr_ops *ops; |
| struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
| }; |
| |
| static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
| const struct nlattr *nla, |
| struct nft_expr_info *info) |
| { |
| const struct nft_expr_type *type; |
| const struct nft_expr_ops *ops; |
| struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
| int err; |
| |
| err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy, NULL); |
| if (err < 0) |
| return err; |
| |
| type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]); |
| if (IS_ERR(type)) |
| return PTR_ERR(type); |
| |
| if (tb[NFTA_EXPR_DATA]) { |
| err = nla_parse_nested(info->tb, type->maxattr, |
| tb[NFTA_EXPR_DATA], type->policy, NULL); |
| if (err < 0) |
| goto err1; |
| } else |
| memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); |
| |
| if (type->select_ops != NULL) { |
| ops = type->select_ops(ctx, |
| (const struct nlattr * const *)info->tb); |
| if (IS_ERR(ops)) { |
| err = PTR_ERR(ops); |
| goto err1; |
| } |
| } else |
| ops = type->ops; |
| |
| info->ops = ops; |
| return 0; |
| |
| err1: |
| module_put(type->owner); |
| return err; |
| } |
| |
| static int nf_tables_newexpr(const struct nft_ctx *ctx, |
| const struct nft_expr_info *info, |
| struct nft_expr *expr) |
| { |
| const struct nft_expr_ops *ops = info->ops; |
| int err; |
| |
| expr->ops = ops; |
| if (ops->init) { |
| err = ops->init(ctx, expr, (const struct nlattr **)info->tb); |
| if (err < 0) |
| goto err1; |
| } |
| |
| if (ops->validate) { |
| const struct nft_data *data = NULL; |
| |
| err = ops->validate(ctx, expr, &data); |
| if (err < 0) |
| goto err2; |
| } |
| |
| return 0; |
| |
| err2: |
| if (ops->destroy) |
| ops->destroy(ctx, expr); |
| err1: |
| expr->ops = NULL; |
| return err; |
| } |
| |
| static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
| struct nft_expr *expr) |
| { |
| if (expr->ops->destroy) |
| expr->ops->destroy(ctx, expr); |
| module_put(expr->ops->type->owner); |
| } |
| |
| struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
| const struct nlattr *nla) |
| { |
| struct nft_expr_info info; |
| struct nft_expr *expr; |
| int err; |
| |
| err = nf_tables_expr_parse(ctx, nla, &info); |
| if (err < 0) |
| goto err1; |
| |
| err = -ENOMEM; |
| expr = kzalloc(info.ops->size, GFP_KERNEL); |
| if (expr == NULL) |
| goto err2; |
| |
| err = nf_tables_newexpr(ctx, &info, expr); |
| if (err < 0) |
| goto err3; |
| |
| return expr; |
| err3: |
| kfree(expr); |
| err2: |
| module_put(info.ops->type->owner); |
| err1: |
| return ERR_PTR(err); |
| } |
| |
| void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
| { |
| nf_tables_expr_destroy(ctx, expr); |
| kfree(expr); |
| } |
| |
| /* |
| * Rules |
| */ |
| |
| static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain, |
| u64 handle) |
| { |
| struct nft_rule *rule; |
| |
| // FIXME: this sucks |
| list_for_each_entry(rule, &chain->rules, list) { |
| if (handle == rule->handle) |
| return rule; |
| } |
| |
| return ERR_PTR(-ENOENT); |
| } |
| |
| static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain, |
| const struct nlattr *nla) |
| { |
| if (nla == NULL) |
| return ERR_PTR(-EINVAL); |
| |
| return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
| } |
| |
| static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { |
| [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
| .len = NFT_TABLE_MAXNAMELEN - 1 }, |
| [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
| .len = NFT_CHAIN_MAXNAMELEN - 1 }, |
| [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, |
| [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, |
| [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
| [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
| [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
| .len = NFT_USERDATA_MAXLEN }, |
| }; |
| |
| static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
| u32 portid, u32 seq, int event, |
| u32 flags, int family, |
| const struct nft_table *table, |
| const struct nft_chain *chain, |
| const struct nft_rule *rule) |
| { |
| struct nlmsghdr *nlh; |
| struct nfgenmsg *nfmsg; |
| const struct nft_expr *expr, *next; |
| struct nlattr *list; |
| const struct nft_rule *prule; |
| u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
| |
| nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags); |
| if (nlh == NULL) |
| goto nla_put_failure; |
| |
| nfmsg = nlmsg_data(nlh); |
| nfmsg->nfgen_family = family; |
| nfmsg->version = NFNETLINK_V0; |
| nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
| |
| if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
| goto nla_put_failure; |
| if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) |
| goto nla_put_failure; |
| if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
| NFTA_RULE_PAD)) |
| goto nla_put_failure; |
| |
| if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) { |
| prule = list_prev_entry(rule, list); |
| if (nla_put_be64(skb, NFTA_RULE_POSITION, |
| cpu_to_be64(prule->handle), |
| NFTA_RULE_PAD)) |
| goto nla_put_failure; |
| } |
| |
| list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS); |
| if (list == NULL) |
| goto nla_put_failure; |
| nft_rule_for_each_expr(expr, next, rule) { |
| if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
| goto nla_put_failure; |
| } |
| nla_nest_end(skb, list); |
| |
| if (rule->udata) { |
| struct nft_userdata *udata = nft_userdata(rule); |
| if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, |
| udata->data) < 0) |
| goto nla_put_failure; |
| } |
| |
| nlmsg_end(skb, nlh); |
| return 0; |
| |
| nla_put_failure: |
| nlmsg_trim(skb, nlh); |
| return -1; |
| } |
| |
| static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
| const struct nft_rule *rule, int event) |
| { |
| struct sk_buff *skb; |
| int err; |
| |
| if (!ctx->report && |
| !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
| return; |
| |
| skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| if (skb == NULL) |
| goto err; |
| |
| err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
| event, 0, ctx->afi->family, ctx->table, |
| ctx->chain, rule); |
| if (err < 0) { |
| kfree_skb(skb); |
| goto err; |
| } |
| |
| nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
| ctx->report, GFP_KERNEL); |
| return; |
| err: |
| nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
| } |
| |
| struct nft_rule_dump_ctx { |
| char *table; |
| char *chain; |
| }; |
| |
| static int nf_tables_dump_rules(struct sk_buff *skb, |
| struct netlink_callback *cb) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
| const struct nft_rule_dump_ctx *ctx = cb->data; |
| const struct nft_af_info *afi; |
| const struct nft_table *table; |
| const struct nft_chain *chain; |
| const struct nft_rule *rule; |
| unsigned int idx = 0, s_idx = cb->args[0]; |
| struct net *net = sock_net(skb->sk); |
| int family = nfmsg->nfgen_family; |
| |
| rcu_read_lock(); |
| cb->seq = net->nft.base_seq; |
| |
| list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
| if (family != NFPROTO_UNSPEC && family != afi->family) |
| continue; |
| |
| list_for_each_entry_rcu(table, &afi->tables, list) { |
| if (ctx && ctx->table && |
| strcmp(ctx->table, table->name) != 0) |
| continue; |
| |
| list_for_each_entry_rcu(chain, &table->chains, list) { |
| if (ctx && ctx->chain[0] && |
| strcmp(ctx->chain, chain->name) != 0) |
| continue; |
| |
| list_for_each_entry_rcu(rule, &chain->rules, list) { |
| if (!nft_is_active(net, rule)) |
| goto cont; |
| if (idx < s_idx) |
| goto cont; |
| if (idx > s_idx) |
| memset(&cb->args[1], 0, |
| sizeof(cb->args) - sizeof(cb->args[0])); |
| if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
| cb->nlh->nlmsg_seq, |
| NFT_MSG_NEWRULE, |
| NLM_F_MULTI | NLM_F_APPEND, |
| afi->family, table, chain, rule) < 0) |
| goto done; |
| |
| nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
| cont: |
| idx++; |
| } |
| } |
| } |
| } |
| done: |
| rcu_read_unlock(); |
| |
| cb->args[0] = idx; |
| return skb->len; |
| } |
| |
| static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
| { |
| struct nft_rule_dump_ctx *ctx = cb->data; |
| |
| if (ctx) { |
| kfree(ctx->table); |
| kfree(ctx->chain); |
| kfree(ctx); |
| } |
| return 0; |
| } |
| |
| static int nf_tables_getrule(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| u8 genmask = nft_genmask_cur(net); |
| const struct nft_af_info *afi; |
| const struct nft_table *table; |
| const struct nft_chain *chain; |
| const struct nft_rule *rule; |
| struct sk_buff *skb2; |
| int family = nfmsg->nfgen_family; |
| int err; |
| |
| if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| struct netlink_dump_control c = { |
| .dump = nf_tables_dump_rules, |
| .done = nf_tables_dump_rules_done, |
| }; |
| |
| if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { |
| struct nft_rule_dump_ctx *ctx; |
| |
| ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| if (!ctx) |
| return -ENOMEM; |
| |
| if (nla[NFTA_RULE_TABLE]) { |
| ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], |
| GFP_KERNEL); |
| if (!ctx->table) { |
| kfree(ctx); |
| return -ENOMEM; |
| } |
| } |
| if (nla[NFTA_RULE_CHAIN]) { |
| ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], |
| GFP_KERNEL); |
| if (!ctx->chain) { |
| kfree(ctx->table); |
| kfree(ctx); |
| return -ENOMEM; |
| } |
| } |
| c.data = ctx; |
| } |
| |
| return netlink_dump_start(nlsk, skb, nlh, &c); |
| } |
| |
| afi = nf_tables_afinfo_lookup(net, family, false); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| |
| chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask); |
| if (IS_ERR(chain)) |
| return PTR_ERR(chain); |
| |
| rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
| if (IS_ERR(rule)) |
| return PTR_ERR(rule); |
| |
| skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| if (!skb2) |
| return -ENOMEM; |
| |
| err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
| nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
| family, table, chain, rule); |
| if (err < 0) |
| goto err; |
| |
| return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); |
| |
| err: |
| kfree_skb(skb2); |
| return err; |
| } |
| |
| static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
| struct nft_rule *rule) |
| { |
| struct nft_expr *expr; |
| |
| /* |
| * Careful: some expressions might not be initialized in case this |
| * is called on error from nf_tables_newrule(). |
| */ |
| expr = nft_expr_first(rule); |
| while (expr != nft_expr_last(rule) && expr->ops) { |
| nf_tables_expr_destroy(ctx, expr); |
| expr = nft_expr_next(expr); |
| } |
| kfree(rule); |
| } |
| |
| #define NFT_RULE_MAXEXPRS 128 |
| |
| static struct nft_expr_info *info; |
| |
| static int nf_tables_newrule(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| u8 genmask = nft_genmask_next(net); |
| struct nft_af_info *afi; |
| struct nft_table *table; |
| struct nft_chain *chain; |
| struct nft_rule *rule, *old_rule = NULL; |
| struct nft_userdata *udata; |
| struct nft_trans *trans = NULL; |
| struct nft_expr *expr; |
| struct nft_ctx ctx; |
| struct nlattr *tmp; |
| unsigned int size, i, n, ulen = 0, usize = 0; |
| int err, rem; |
| bool create; |
| u64 handle, pos_handle; |
| |
| create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; |
| |
| afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| |
| chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask); |
| if (IS_ERR(chain)) |
| return PTR_ERR(chain); |
| |
| if (nla[NFTA_RULE_HANDLE]) { |
| handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); |
| rule = __nf_tables_rule_lookup(chain, handle); |
| if (IS_ERR(rule)) |
| return PTR_ERR(rule); |
| |
| if (nlh->nlmsg_flags & NLM_F_EXCL) |
| return -EEXIST; |
| if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| old_rule = rule; |
| else |
| return -EOPNOTSUPP; |
| } else { |
| if (!create || nlh->nlmsg_flags & NLM_F_REPLACE) |
| return -EINVAL; |
| handle = nf_tables_alloc_handle(table); |
| |
| if (chain->use == UINT_MAX) |
| return -EOVERFLOW; |
| } |
| |
| if (nla[NFTA_RULE_POSITION]) { |
| if (!(nlh->nlmsg_flags & NLM_F_CREATE)) |
| return -EOPNOTSUPP; |
| |
| pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); |
| old_rule = __nf_tables_rule_lookup(chain, pos_handle); |
| if (IS_ERR(old_rule)) |
| return PTR_ERR(old_rule); |
| } |
| |
| nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
| |
| n = 0; |
| size = 0; |
| if (nla[NFTA_RULE_EXPRESSIONS]) { |
| nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
| err = -EINVAL; |
| if (nla_type(tmp) != NFTA_LIST_ELEM) |
| goto err1; |
| if (n == NFT_RULE_MAXEXPRS) |
| goto err1; |
| err = nf_tables_expr_parse(&ctx, tmp, &info[n]); |
| if (err < 0) |
| goto err1; |
| size += info[n].ops->size; |
| n++; |
| } |
| } |
| /* Check for overflow of dlen field */ |
| err = -EFBIG; |
| if (size >= 1 << 12) |
| goto err1; |
| |
| if (nla[NFTA_RULE_USERDATA]) { |
| ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
| if (ulen > 0) |
| usize = sizeof(struct nft_userdata) + ulen; |
| } |
| |
| err = -ENOMEM; |
| rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL); |
| if (rule == NULL) |
| goto err1; |
| |
| nft_activate_next(net, rule); |
| |
| rule->handle = handle; |
| rule->dlen = size; |
| rule->udata = ulen ? 1 : 0; |
| |
| if (ulen) { |
| udata = nft_userdata(rule); |
| udata->len = ulen - 1; |
| nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); |
| } |
| |
| expr = nft_expr_first(rule); |
| for (i = 0; i < n; i++) { |
| err = nf_tables_newexpr(&ctx, &info[i], expr); |
| if (err < 0) |
| goto err2; |
| info[i].ops = NULL; |
| expr = nft_expr_next(expr); |
| } |
| |
| if (nlh->nlmsg_flags & NLM_F_REPLACE) { |
| if (nft_is_active_next(net, old_rule)) { |
| trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE, |
| old_rule); |
| if (trans == NULL) { |
| err = -ENOMEM; |
| goto err2; |
| } |
| nft_deactivate_next(net, old_rule); |
| chain->use--; |
| list_add_tail_rcu(&rule->list, &old_rule->list); |
| } else { |
| err = -ENOENT; |
| goto err2; |
| } |
| } else if (nlh->nlmsg_flags & NLM_F_APPEND) |
| if (old_rule) |
| list_add_rcu(&rule->list, &old_rule->list); |
| else |
| list_add_tail_rcu(&rule->list, &chain->rules); |
| else { |
| if (old_rule) |
| list_add_tail_rcu(&rule->list, &old_rule->list); |
| else |
| list_add_rcu(&rule->list, &chain->rules); |
| } |
| |
| if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) { |
| err = -ENOMEM; |
| goto err3; |
| } |
| chain->use++; |
| return 0; |
| |
| err3: |
| list_del_rcu(&rule->list); |
| err2: |
| nf_tables_rule_destroy(&ctx, rule); |
| err1: |
| for (i = 0; i < n; i++) { |
| if (info[i].ops != NULL) |
| module_put(info[i].ops->type->owner); |
| } |
| return err; |
| } |
| |
| static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
| const struct nlattr *nla) |
| { |
| u32 id = ntohl(nla_get_be32(nla)); |
| struct nft_trans *trans; |
| |
| list_for_each_entry(trans, &net->nft.commit_list, list) { |
| struct nft_rule *rule = nft_trans_rule(trans); |
| |
| if (trans->msg_type == NFT_MSG_NEWRULE && |
| id == nft_trans_rule_id(trans)) |
| return rule; |
| } |
| return ERR_PTR(-ENOENT); |
| } |
| |
| static int nf_tables_delrule(struct net *net, struct sock *nlsk, |
| struct sk_buff *skb, const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| struct netlink_ext_ack *extack) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| u8 genmask = nft_genmask_next(net); |
| struct nft_af_info *afi; |
| struct nft_table *table; |
| struct nft_chain *chain = NULL; |
| struct nft_rule *rule; |
| int family = nfmsg->nfgen_family, err = 0; |
| struct nft_ctx ctx; |
| |
| afi = nf_tables_afinfo_lookup(net, family, false); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| |
| if (nla[NFTA_RULE_CHAIN]) { |
| chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], |
| genmask); |
| if (IS_ERR(chain)) |
| return PTR_ERR(chain); |
| } |
| |
| nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
| |
| if (chain) { |
| if (nla[NFTA_RULE_HANDLE]) { |
| rule = nf_tables_rule_lookup(chain, |
| nla[NFTA_RULE_HANDLE]); |
| if (IS_ERR(rule)) |
| return PTR_ERR(rule); |
| |
| err = nft_delrule(&ctx, rule); |
| } else if (nla[NFTA_RULE_ID]) { |
| rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]); |
| if (IS_ERR(rule)) |
| return PTR_ERR(rule); |
| |
| err = nft_delrule(&ctx, rule); |
| } else { |
| err = nft_delrule_by_chain(&ctx); |
| } |
| } else { |
| list_for_each_entry(chain, &table->chains, list) { |
| if (!nft_is_active_next(net, chain)) |
| continue; |
| |
| ctx.chain = chain; |
| err = nft_delrule_by_chain(&ctx); |
| if (err < 0) |
| break; |
| } |
| } |
| |
| return err; |
| } |
| |
| /* |
| * Sets |
| */ |
| |
| static LIST_HEAD(nf_tables_set_types); |
| |
| int nft_register_set(struct nft_set_type *type) |
| { |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| list_add_tail_rcu(&type->list, &nf_tables_set_types); |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| return 0; |
| } |
| EXPORT_SYMBOL_GPL(nft_register_set); |
| |
| void nft_unregister_set(struct nft_set_type *type) |
| { |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| list_del_rcu(&type->list); |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| } |
| EXPORT_SYMBOL_GPL(nft_unregister_set); |
| |
| #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
| NFT_SET_TIMEOUT | NFT_SET_OBJECT) |
| |
| static bool nft_set_ops_candidate(const struct nft_set_ops *ops, u32 flags) |
| { |
| return (flags & ops->features) == (flags & NFT_SET_FEATURES); |
| } |
| |
| /* |
| * Select a set implementation based on the data characteristics and the |
| * given policy. The total memory use might not be known if no size is |
| * given, in that case the amount of memory per element is used. |
| */ |
| static const struct nft_set_ops * |
| nft_select_set_ops(const struct nft_ctx *ctx, |
| const struct nlattr * const nla[], |
| const struct nft_set_desc *desc, |
| enum nft_set_policies policy) |
| { |
| const struct nft_set_ops *ops, *bops; |
| struct nft_set_estimate est, best; |
| const struct nft_set_type *type; |
| u32 flags = 0; |
| |
| #ifdef CONFIG_MODULES |
| if (list_empty(&nf_tables_set_types)) { |
| nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
| request_module("nft-set"); |
| nfnl_lock(NFNL_SUBSYS_NFTABLES); |
| if (!list_empty(&nf_tables_set_types)) |
| return ERR_PTR(-EAGAIN); |
| } |
| #endif |
| if (nla[NFTA_SET_FLAGS] != NULL) |
| flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); |
| |
| bops = NULL; |
| best.size = ~0; |
| best.lookup = ~0; |
| best.space = ~0; |
| |
| list_for_each_entry(type, &nf_tables_set_types, list) { |
| if (!type->select_ops) |
| ops = type->ops; |
| else |
| ops = type->select_ops(ctx, desc, flags); |
| if (!ops) |
| continue; |
| |
| if (!nft_set_ops_candidate(ops, flags)) |
| continue; |
| if (!ops->estimate(desc, flags, &est)) |
| continue; |
| |
| switch (policy) { |
| case NFT_SET_POL_PERFORMANCE: |
| if (est.lookup < best.lookup) |
| break; |
| if (est.lookup == best.lookup) { |
| if (!desc->size) { |
| if (est.space < best.space) |
| break; |
| } else if (est.size < best.size) { |
| break; |
| } |
| } |
| continue; |
| case NFT_SET_POL_MEMORY: |
| if (!desc->size) { |
| if (est.space < best.space) |
| break; |
| if (est.space == best.space && |
| est.lookup < best.lookup) |
| break; |
| } else if (est.size < best.size) { |
| break; |
| } |
| continue; |
| default: |
| break; |
| } |
| |
| if (!try_module_get(type->owner)) |
| continue; |
| if (bops != NULL) |
| module_put(bops->type->owner); |
| |
| bops = ops; |
| best = est; |
| } |
| |
| if (bops != NULL) |
| return bops; |
| |
| return ERR_PTR(-EOPNOTSUPP); |
| } |
| |
| static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { |
| [NFTA_SET_TABLE] = { .type = NLA_STRING, |
| .len = NFT_TABLE_MAXNAMELEN - 1 }, |
| [NFTA_SET_NAME] = { .type = NLA_STRING, |
| .len = NFT_SET_MAXNAMELEN - 1 }, |
| [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
| [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, |
| [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, |
| [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, |
| [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, |
| [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
| [NFTA_SET_DESC] = { .type = NLA_NESTED }, |
| [NFTA_SET_ID] = { .type = NLA_U32 }, |
| [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
| [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, |
| [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
| .len = NFT_USERDATA_MAXLEN }, |
| [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
| }; |
| |
| static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { |
| [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, |
| }; |
| |
| static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net, |
| const struct sk_buff *skb, |
| const struct nlmsghdr *nlh, |
| const struct nlattr * const nla[], |
| u8 genmask) |
| { |
| const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
| struct nft_af_info *afi = NULL; |
| struct nft_table *table = NULL; |
| |
| if (nfmsg->nfgen_family != NFPROTO_UNSPEC) { |
| afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false); |
| if (IS_ERR(afi)) |
| return PTR_ERR(afi); |
| } |
| |
| if (nla[NFTA_SET_TABLE] != NULL) { |
| if (afi == NULL) |
| return -EAFNOSUPPORT; |
| |
| table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], |
| genmask); |
| if (IS_ERR(table)) |
| return PTR_ERR(table); |
| } |
| |
| nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla); |
| return 0; |
| } |
| |
| static struct nft_set *nf_tables_set_lookup(const struct nft_table *table, |
| const struct nlattr *nla, u8 genmask) |
| { |
| struct nft_set *set; |
| |
| if (nla == NULL) |
| return ERR_PTR(-EINVAL); |
| |
| list_for_each_entry(set, &table->sets, list) { |
| if (!nla_strcmp(nla, set->name) && |
| nft_active_genmask(set, genmask)) |
| return set; |
| } |
| return ERR_PTR(-ENOENT); |
| } |
| |
| static struct nft_set *nf_tables_set_lookup_byid(const struct net *net, |
| const struct nlattr *nla, |
| u8 genmask) |
| { |
| struct nft_trans *trans; |
| u32 id = ntohl(nla_get_be32(nla)); |
| |
| list_for_each_entry(trans, &net->nft.commit_list, list) { |
| struct nft_set *set = nft_trans_set(trans); |
| |
| if (trans->msg_type == NFT_MSG_NEWSET && |
| id == nft_trans_set_id(trans) && |
| nft_active_genmask(set, genmask)) |
| return set; |
| } |
| return ERR_PTR(-ENOENT); |
| } |
| |
| struct nft_set *nft_set_lookup(const struct net *net, |
| const struct nft_table *table, |
| const struct nlattr *nla_set_name, |
| const struct nlattr *nla_set_id, |
| u8 genmask) |
| { |
| struct nft_set *set; |
| |
| set = nf_tables_set_lookup(table, nla_set_name, genmask); |
| if (IS_ERR(set)) { |
| if (!nla_set_id) |
| return set; |
| |
| set = nf_tables_set_lookup_byid(net, nla_set_id, genmask); |
| } |
| return set; |
| } |
| EXPORT_SYMBOL_GPL(nft_set_lookup); |
| |
| static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
| const char *name) |
| { |
| const struct nft_set *i; |
| const char *p; |
| unsigned long *inuse; |
| unsigned int n = 0, min = 0; |
| |
| p = strchr(name, '%'); |
| if (p != NULL) { |
| if (p[1] != 'd' || strchr(p + 2, '%')) |
| return -EINVAL; |
| |
| inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); |
| if (inuse == NULL) |
| return -ENOMEM; |
| cont: |
| list_for_each_entry(i, &ctx->table->sets, list) { |
| int tmp; |
| |
| if (!nft_is_active_next(ctx->net, set)) |
| continue; |
| if (!sscanf(i->name, name, &tmp)) |
| continue; |
| if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
| continue; |
| |
| set_bit(tmp - min, inuse); |
| } |
| |
| n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
| if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
| min += BITS_PER_BYTE * PAGE_SIZE; |
| memset(inuse, 0, PAGE_SIZE); |
| goto cont; |
| } |
| free_page((unsigned long)inuse); |
| } |
| |
| set->name = kasprintf(GFP_KERNEL, name, min + n); |
| if (!set->name) |
| return -ENOMEM; |
| |
| list_for_each_entry(i, &ctx->table->sets, list) { |
| if (!nft_is_active_next(ctx->net, i)) |
| continue; |
| if (!strcmp(set->name, i->name)) { |
| kfree(set->name); |
| return -ENFILE; |
| } |
| } |
| return 0; |
| } |
| |
| static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
| const struct nft_set *set, u16 event, u16 flags) |
| { |
| struct nfgenmsg *nfmsg; |
| struct nlmsghdr *nlh; |
| struct nlattr *desc; |
| u32 portid = ctx->portid; |
| u32 seq = ctx->seq; |
| |
| event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
| nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
| flags); |
| if (nlh == NULL) |
| goto nla_put_failure; |
| |
| nfmsg = nlmsg_data(nlh); |
| nfmsg->nfgen_family = ctx->afi->family; |
| nfmsg->version = NFNETLINK_V0; |
| nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
| |
| if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
| goto nla_put_failure; |
| if (nla_put_string(skb, NFTA_SET_NAME, set->name)) |
| goto nla_put_failure; |
| if (set->flags != 0) |
| if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) |
| goto nla_put_failure; |
| |
| if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) |
| goto nla_put_failure; |
| if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) |
| goto nla_put_failure; |
| if (set->flags & NFT_SET_MAP) { |
| if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) |
| goto nla_put_failure; |
| if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) |
| goto nla_put_failure; |
| } |
| if (set->flags & NFT_SET_OBJECT && |
| nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) |
| goto nla_put_failure; |
| |
| if (set->timeout && |
| nla_put_be64(skb, NFTA_SET_TIMEOUT, |
| cpu_to_be64(jiffies_to_msecs(set->timeout)), |
| NFTA_SET_PAD)) |
| goto nla_put_failure; |
| if (set->gc_int && |
| nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) |
| goto nla_put_failure; |
| |
| if (set->policy != NFT_SET_POL_PERFORMANCE) { |
| if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) |
| goto nla_put_failure; |
| } |
| |
| if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) |
| goto nla_put_failure; |
| |
| desc = nla_nest_start(skb, NFTA_SET_DESC); |
| if (desc == NULL) |
| goto nla_put_failure; |
| if (set->size && |
| nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) |
| goto nla_put_failure; |
| nla_nest_end(skb, desc); |
| |
| nlmsg_end(skb, nlh); |
| return 0; |
| |
| nla_put_failure: |
| nlmsg_trim(skb, nlh); |
| return -1; |
| } |
| |
| static void nf_tables_set_notify(const struct nft_ctx *ctx, |
| const struct nft_set *set, int event, |
| gfp_t gfp_flags) |
| { |
| struct sk_buff *skb; |
| u32 portid = ctx->portid; |
| int err; |
| |
| if (!ctx->report && |
| !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
| return; |
| |
| skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
| if (skb == NULL) |
| goto err; |
| |
| err = nf_tables_fill_set(skb, ctx, set, event, 0); |
| if (err < 0) { |
| kfree_skb(skb); |
| goto err; |
| } |
| |
| nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, ctx->report, |
| gfp_flags); |
| return; |
| err: |
| nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
| } |
| |
| static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
| { |
| const struct nft_set *set; |
| unsigned int idx, s_idx = cb->args[0]; |
| struct nft_af_info *afi; |
| struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
| struct net *net = sock_net(skb->sk); |
| int cur_family = cb->args[3]; |
| struct nft_ctx *ctx = cb->data, ctx_set; |
| |
| if (cb->args[1]) |
| return skb->len; |
| |
| rcu_read_lock(); |
| cb->seq = net->nft.base_seq; |
| |
| list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
| if (ctx->afi && ctx->afi != afi) |
| continue; |
| |
| if (cur_family) { |
| if (afi->family != cur_family) |
| continue; |
| |
| cur_family = 0; |
| } |
| list_for_each_entry_rcu(table, &afi->tables, list) { |
| if (ctx->table && ctx->table != table) |
| continue; |
| |
| if (cur_table) { |
| if (cur_table != table) |
| continue; |
| |
| cur_table = NULL; |
| } |
| idx = 0; |
| list_for_each_entry_rcu(set, &table->sets, list) { |
| if (idx < s_idx) |
| goto cont; |
| if (!nft_is_active(net, set)) |
| goto cont; |
| |
| ctx_set = *ctx; |
| ctx_set.table = table; |
| ctx_set.afi = afi; |
| if (nf_tables_fill_set(skb, &ctx_set, set, |
| NFT_MSG_NEWSET, |
| NLM_F_MULTI) < 0) { |
| cb |