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