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