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