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