Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1 | /* |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 2 | * Copyright (c) 2008-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/kernel.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/netlink.h> |
| 15 | #include <linux/netfilter.h> |
| 16 | #include <linux/netfilter/nf_tables.h> |
| 17 | #include <net/netfilter/nf_tables.h> |
| 18 | #include <net/netfilter/nf_conntrack.h> |
Florian Westphal | 48f66c9 | 2016-01-07 21:34:24 +0100 | [diff] [blame] | 19 | #include <net/netfilter/nf_conntrack_acct.h> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 20 | #include <net/netfilter/nf_conntrack_tuple.h> |
| 21 | #include <net/netfilter/nf_conntrack_helper.h> |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 22 | #include <net/netfilter/nf_conntrack_ecache.h> |
Florian Westphal | d2bf2f3 | 2014-02-18 15:25:32 +0100 | [diff] [blame] | 23 | #include <net/netfilter/nf_conntrack_labels.h> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 24 | |
| 25 | struct nft_ct { |
| 26 | enum nft_ct_keys key:8; |
| 27 | enum ip_conntrack_dir dir:8; |
Patrick McHardy | d46f2cd | 2014-03-07 19:08:32 +0100 | [diff] [blame] | 28 | union { |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 29 | enum nft_registers dreg:8; |
| 30 | enum nft_registers sreg:8; |
| 31 | }; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Florian Westphal | 48f66c9 | 2016-01-07 21:34:24 +0100 | [diff] [blame] | 34 | static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c, |
| 35 | enum nft_ct_keys k, |
| 36 | enum ip_conntrack_dir d) |
| 37 | { |
| 38 | if (d < IP_CT_DIR_MAX) |
| 39 | return k == NFT_CT_BYTES ? atomic64_read(&c[d].bytes) : |
| 40 | atomic64_read(&c[d].packets); |
| 41 | |
| 42 | return nft_ct_get_eval_counter(c, k, IP_CT_DIR_ORIGINAL) + |
| 43 | nft_ct_get_eval_counter(c, k, IP_CT_DIR_REPLY); |
| 44 | } |
| 45 | |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 46 | static void nft_ct_get_eval(const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 47 | struct nft_regs *regs, |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 48 | const struct nft_pktinfo *pkt) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 49 | { |
| 50 | const struct nft_ct *priv = nft_expr_priv(expr); |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 51 | u32 *dest = ®s->data[priv->dreg]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 52 | enum ip_conntrack_info ctinfo; |
| 53 | const struct nf_conn *ct; |
| 54 | const struct nf_conn_help *help; |
| 55 | const struct nf_conntrack_tuple *tuple; |
| 56 | const struct nf_conntrack_helper *helper; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 57 | unsigned int state; |
| 58 | |
| 59 | ct = nf_ct_get(pkt->skb, &ctinfo); |
| 60 | |
| 61 | switch (priv->key) { |
| 62 | case NFT_CT_STATE: |
| 63 | if (ct == NULL) |
| 64 | state = NF_CT_STATE_INVALID_BIT; |
| 65 | else if (nf_ct_is_untracked(ct)) |
| 66 | state = NF_CT_STATE_UNTRACKED_BIT; |
| 67 | else |
| 68 | state = NF_CT_STATE_BIT(ctinfo); |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 69 | *dest = state; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 70 | return; |
David Miller | c1f8667 | 2015-04-07 23:05:42 -0400 | [diff] [blame] | 71 | default: |
| 72 | break; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (ct == NULL) |
| 76 | goto err; |
| 77 | |
| 78 | switch (priv->key) { |
| 79 | case NFT_CT_DIRECTION: |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 80 | *dest = CTINFO2DIR(ctinfo); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 81 | return; |
| 82 | case NFT_CT_STATUS: |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 83 | *dest = ct->status; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 84 | return; |
| 85 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 86 | case NFT_CT_MARK: |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 87 | *dest = ct->mark; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 88 | return; |
| 89 | #endif |
| 90 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
| 91 | case NFT_CT_SECMARK: |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 92 | *dest = ct->secmark; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 93 | return; |
| 94 | #endif |
| 95 | case NFT_CT_EXPIRATION: |
Florian Westphal | c8607e0 | 2016-07-06 14:53:06 +0200 | [diff] [blame] | 96 | *dest = jiffies_to_msecs(nf_ct_expires(ct)); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 97 | return; |
| 98 | case NFT_CT_HELPER: |
| 99 | if (ct->master == NULL) |
| 100 | goto err; |
| 101 | help = nfct_help(ct->master); |
| 102 | if (help == NULL) |
| 103 | goto err; |
| 104 | helper = rcu_dereference(help->helper); |
| 105 | if (helper == NULL) |
| 106 | goto err; |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 107 | strncpy((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 108 | return; |
Florian Westphal | d2bf2f3 | 2014-02-18 15:25:32 +0100 | [diff] [blame] | 109 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
| 110 | case NFT_CT_LABELS: { |
| 111 | struct nf_conn_labels *labels = nf_ct_labels_find(ct); |
Florian Westphal | d2bf2f3 | 2014-02-18 15:25:32 +0100 | [diff] [blame] | 112 | |
Florian Westphal | 2301401 | 2016-07-21 12:51:16 +0200 | [diff] [blame] | 113 | if (labels) |
| 114 | memcpy(dest, labels->bits, NF_CT_LABELS_MAX_SIZE); |
| 115 | else |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 116 | memset(dest, 0, NF_CT_LABELS_MAX_SIZE); |
Florian Westphal | d2bf2f3 | 2014-02-18 15:25:32 +0100 | [diff] [blame] | 117 | return; |
| 118 | } |
Pablo Neira Ayuso | efaea94 | 2016-01-13 17:34:38 +0100 | [diff] [blame] | 119 | #endif |
Florian Westphal | 48f66c9 | 2016-01-07 21:34:24 +0100 | [diff] [blame] | 120 | case NFT_CT_BYTES: /* fallthrough */ |
| 121 | case NFT_CT_PKTS: { |
| 122 | const struct nf_conn_acct *acct = nf_conn_acct_find(ct); |
| 123 | u64 count = 0; |
| 124 | |
| 125 | if (acct) |
| 126 | count = nft_ct_get_eval_counter(acct->counter, |
| 127 | priv->key, priv->dir); |
| 128 | memcpy(dest, &count, sizeof(count)); |
| 129 | return; |
| 130 | } |
Liping Zhang | d767ff2 | 2016-09-22 22:28:51 +0800 | [diff] [blame^] | 131 | case NFT_CT_L3PROTOCOL: |
| 132 | *dest = nf_ct_l3num(ct); |
| 133 | return; |
| 134 | case NFT_CT_PROTOCOL: |
| 135 | *dest = nf_ct_protonum(ct); |
| 136 | return; |
David Miller | c1f8667 | 2015-04-07 23:05:42 -0400 | [diff] [blame] | 137 | default: |
| 138 | break; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | tuple = &ct->tuplehash[priv->dir].tuple; |
| 142 | switch (priv->key) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 143 | case NFT_CT_SRC: |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 144 | memcpy(dest, tuple->src.u3.all, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 145 | nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16); |
| 146 | return; |
| 147 | case NFT_CT_DST: |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 148 | memcpy(dest, tuple->dst.u3.all, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 149 | nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16); |
| 150 | return; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 151 | case NFT_CT_PROTO_SRC: |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 152 | *dest = (__force __u16)tuple->src.u.all; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 153 | return; |
| 154 | case NFT_CT_PROTO_DST: |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 155 | *dest = (__force __u16)tuple->dst.u.all; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 156 | return; |
David Miller | c1f8667 | 2015-04-07 23:05:42 -0400 | [diff] [blame] | 157 | default: |
| 158 | break; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 159 | } |
| 160 | return; |
| 161 | err: |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 162 | regs->verdict.code = NFT_BREAK; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 165 | static void nft_ct_set_eval(const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 166 | struct nft_regs *regs, |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 167 | const struct nft_pktinfo *pkt) |
| 168 | { |
| 169 | const struct nft_ct *priv = nft_expr_priv(expr); |
| 170 | struct sk_buff *skb = pkt->skb; |
Kristian Evensen | 847c8e2 | 2014-01-10 21:43:20 +0100 | [diff] [blame] | 171 | #ifdef CONFIG_NF_CONNTRACK_MARK |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 172 | u32 value = regs->data[priv->sreg]; |
Kristian Evensen | 847c8e2 | 2014-01-10 21:43:20 +0100 | [diff] [blame] | 173 | #endif |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 174 | enum ip_conntrack_info ctinfo; |
| 175 | struct nf_conn *ct; |
| 176 | |
| 177 | ct = nf_ct_get(skb, &ctinfo); |
| 178 | if (ct == NULL) |
| 179 | return; |
| 180 | |
| 181 | switch (priv->key) { |
| 182 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 183 | case NFT_CT_MARK: |
| 184 | if (ct->mark != value) { |
| 185 | ct->mark = value; |
| 186 | nf_conntrack_event_cache(IPCT_MARK, ct); |
| 187 | } |
| 188 | break; |
| 189 | #endif |
Florian Westphal | 1ad8f48 | 2016-04-26 11:59:53 +0200 | [diff] [blame] | 190 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
| 191 | case NFT_CT_LABELS: |
| 192 | nf_connlabels_replace(ct, |
| 193 | ®s->data[priv->sreg], |
| 194 | ®s->data[priv->sreg], |
| 195 | NF_CT_LABELS_MAX_SIZE / sizeof(u32)); |
| 196 | break; |
| 197 | #endif |
David Miller | c1f8667 | 2015-04-07 23:05:42 -0400 | [diff] [blame] | 198 | default: |
| 199 | break; |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 203 | static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = { |
| 204 | [NFTA_CT_DREG] = { .type = NLA_U32 }, |
| 205 | [NFTA_CT_KEY] = { .type = NLA_U32 }, |
| 206 | [NFTA_CT_DIRECTION] = { .type = NLA_U8 }, |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 207 | [NFTA_CT_SREG] = { .type = NLA_U32 }, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
Patrick McHardy | 9638f33 | 2014-01-06 18:09:49 +0000 | [diff] [blame] | 210 | static int nft_ct_l3proto_try_module_get(uint8_t family) |
| 211 | { |
| 212 | int err; |
| 213 | |
| 214 | if (family == NFPROTO_INET) { |
| 215 | err = nf_ct_l3proto_try_module_get(NFPROTO_IPV4); |
| 216 | if (err < 0) |
| 217 | goto err1; |
| 218 | err = nf_ct_l3proto_try_module_get(NFPROTO_IPV6); |
| 219 | if (err < 0) |
| 220 | goto err2; |
| 221 | } else { |
| 222 | err = nf_ct_l3proto_try_module_get(family); |
| 223 | if (err < 0) |
| 224 | goto err1; |
| 225 | } |
| 226 | return 0; |
| 227 | |
| 228 | err2: |
| 229 | nf_ct_l3proto_module_put(NFPROTO_IPV4); |
| 230 | err1: |
| 231 | return err; |
| 232 | } |
| 233 | |
| 234 | static void nft_ct_l3proto_module_put(uint8_t family) |
| 235 | { |
| 236 | if (family == NFPROTO_INET) { |
| 237 | nf_ct_l3proto_module_put(NFPROTO_IPV4); |
| 238 | nf_ct_l3proto_module_put(NFPROTO_IPV6); |
| 239 | } else |
| 240 | nf_ct_l3proto_module_put(family); |
| 241 | } |
| 242 | |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 243 | static int nft_ct_get_init(const struct nft_ctx *ctx, |
| 244 | const struct nft_expr *expr, |
| 245 | const struct nlattr * const tb[]) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 246 | { |
| 247 | struct nft_ct *priv = nft_expr_priv(expr); |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 248 | unsigned int len; |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 249 | int err; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 250 | |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 251 | priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY])); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 252 | switch (priv->key) { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 253 | case NFT_CT_DIRECTION: |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 254 | if (tb[NFTA_CT_DIRECTION] != NULL) |
| 255 | return -EINVAL; |
| 256 | len = sizeof(u8); |
| 257 | break; |
| 258 | case NFT_CT_STATE: |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 259 | case NFT_CT_STATUS: |
| 260 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 261 | case NFT_CT_MARK: |
| 262 | #endif |
| 263 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
| 264 | case NFT_CT_SECMARK: |
| 265 | #endif |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 266 | case NFT_CT_EXPIRATION: |
| 267 | if (tb[NFTA_CT_DIRECTION] != NULL) |
| 268 | return -EINVAL; |
| 269 | len = sizeof(u32); |
| 270 | break; |
Florian Westphal | d2bf2f3 | 2014-02-18 15:25:32 +0100 | [diff] [blame] | 271 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
| 272 | case NFT_CT_LABELS: |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 273 | if (tb[NFTA_CT_DIRECTION] != NULL) |
| 274 | return -EINVAL; |
| 275 | len = NF_CT_LABELS_MAX_SIZE; |
| 276 | break; |
Florian Westphal | d2bf2f3 | 2014-02-18 15:25:32 +0100 | [diff] [blame] | 277 | #endif |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 278 | case NFT_CT_HELPER: |
| 279 | if (tb[NFTA_CT_DIRECTION] != NULL) |
| 280 | return -EINVAL; |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 281 | len = NF_CT_HELPER_NAME_LEN; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 282 | break; |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 283 | |
Patrick McHardy | 51292c0 | 2014-02-05 15:03:36 +0000 | [diff] [blame] | 284 | case NFT_CT_L3PROTOCOL: |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 285 | case NFT_CT_PROTOCOL: |
Liping Zhang | d767ff2 | 2016-09-22 22:28:51 +0800 | [diff] [blame^] | 286 | /* For compatibility, do not report error if NFTA_CT_DIRECTION |
| 287 | * attribute is specified. |
| 288 | */ |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 289 | len = sizeof(u8); |
| 290 | break; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 291 | case NFT_CT_SRC: |
| 292 | case NFT_CT_DST: |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 293 | if (tb[NFTA_CT_DIRECTION] == NULL) |
| 294 | return -EINVAL; |
| 295 | |
| 296 | switch (ctx->afi->family) { |
| 297 | case NFPROTO_IPV4: |
| 298 | len = FIELD_SIZEOF(struct nf_conntrack_tuple, |
| 299 | src.u3.ip); |
| 300 | break; |
| 301 | case NFPROTO_IPV6: |
| 302 | case NFPROTO_INET: |
| 303 | len = FIELD_SIZEOF(struct nf_conntrack_tuple, |
| 304 | src.u3.ip6); |
| 305 | break; |
| 306 | default: |
| 307 | return -EAFNOSUPPORT; |
| 308 | } |
| 309 | break; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 310 | case NFT_CT_PROTO_SRC: |
| 311 | case NFT_CT_PROTO_DST: |
| 312 | if (tb[NFTA_CT_DIRECTION] == NULL) |
| 313 | return -EINVAL; |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 314 | len = FIELD_SIZEOF(struct nf_conntrack_tuple, src.u.all); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 315 | break; |
Florian Westphal | 48f66c9 | 2016-01-07 21:34:24 +0100 | [diff] [blame] | 316 | case NFT_CT_BYTES: |
| 317 | case NFT_CT_PKTS: |
| 318 | /* no direction? return sum of original + reply */ |
| 319 | if (tb[NFTA_CT_DIRECTION] == NULL) |
| 320 | priv->dir = IP_CT_DIR_MAX; |
| 321 | len = sizeof(u64); |
| 322 | break; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 323 | default: |
| 324 | return -EOPNOTSUPP; |
| 325 | } |
| 326 | |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 327 | if (tb[NFTA_CT_DIRECTION] != NULL) { |
| 328 | priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]); |
| 329 | switch (priv->dir) { |
| 330 | case IP_CT_DIR_ORIGINAL: |
| 331 | case IP_CT_DIR_REPLY: |
| 332 | break; |
| 333 | default: |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | } |
| 337 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 338 | priv->dreg = nft_parse_register(tb[NFTA_CT_DREG]); |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 339 | err = nft_validate_register_store(ctx, priv->dreg, NULL, |
| 340 | NFT_DATA_VALUE, len); |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 341 | if (err < 0) |
| 342 | return err; |
| 343 | |
| 344 | err = nft_ct_l3proto_try_module_get(ctx->afi->family); |
| 345 | if (err < 0) |
| 346 | return err; |
| 347 | |
Liping Zhang | 3f8b61b | 2016-07-05 23:23:00 +0800 | [diff] [blame] | 348 | if (priv->key == NFT_CT_BYTES || priv->key == NFT_CT_PKTS) |
| 349 | nf_ct_set_acct(ctx->net, true); |
| 350 | |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 354 | static int nft_ct_set_init(const struct nft_ctx *ctx, |
| 355 | const struct nft_expr *expr, |
| 356 | const struct nlattr * const tb[]) |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 357 | { |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 358 | struct nft_ct *priv = nft_expr_priv(expr); |
Liping Zhang | 590025a | 2016-07-16 14:27:21 +0800 | [diff] [blame] | 359 | bool label_got = false; |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 360 | unsigned int len; |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 361 | int err; |
| 362 | |
| 363 | priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY])); |
| 364 | switch (priv->key) { |
Patrick McHardy | e88e514 | 2014-03-29 10:43:01 +0000 | [diff] [blame] | 365 | #ifdef CONFIG_NF_CONNTRACK_MARK |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 366 | case NFT_CT_MARK: |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 367 | len = FIELD_SIZEOF(struct nf_conn, mark); |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 368 | break; |
Patrick McHardy | e88e514 | 2014-03-29 10:43:01 +0000 | [diff] [blame] | 369 | #endif |
Florian Westphal | 1ad8f48 | 2016-04-26 11:59:53 +0200 | [diff] [blame] | 370 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
| 371 | case NFT_CT_LABELS: |
| 372 | if (tb[NFTA_CT_DIRECTION]) |
| 373 | return -EINVAL; |
| 374 | len = NF_CT_LABELS_MAX_SIZE; |
| 375 | err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1); |
| 376 | if (err) |
| 377 | return err; |
Liping Zhang | 590025a | 2016-07-16 14:27:21 +0800 | [diff] [blame] | 378 | label_got = true; |
Florian Westphal | 1ad8f48 | 2016-04-26 11:59:53 +0200 | [diff] [blame] | 379 | break; |
| 380 | #endif |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 381 | default: |
| 382 | return -EOPNOTSUPP; |
| 383 | } |
| 384 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 385 | priv->sreg = nft_parse_register(tb[NFTA_CT_SREG]); |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 386 | err = nft_validate_register_load(priv->sreg, len); |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 387 | if (err < 0) |
Liping Zhang | 590025a | 2016-07-16 14:27:21 +0800 | [diff] [blame] | 388 | goto err1; |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 389 | |
Patrick McHardy | 9638f33 | 2014-01-06 18:09:49 +0000 | [diff] [blame] | 390 | err = nft_ct_l3proto_try_module_get(ctx->afi->family); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 391 | if (err < 0) |
Liping Zhang | 590025a | 2016-07-16 14:27:21 +0800 | [diff] [blame] | 392 | goto err1; |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 393 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 394 | return 0; |
Liping Zhang | 590025a | 2016-07-16 14:27:21 +0800 | [diff] [blame] | 395 | |
| 396 | err1: |
| 397 | if (label_got) |
| 398 | nf_connlabels_put(ctx->net); |
| 399 | return err; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Liping Zhang | 590025a | 2016-07-16 14:27:21 +0800 | [diff] [blame] | 402 | static void nft_ct_get_destroy(const struct nft_ctx *ctx, |
| 403 | const struct nft_expr *expr) |
| 404 | { |
| 405 | nft_ct_l3proto_module_put(ctx->afi->family); |
| 406 | } |
| 407 | |
| 408 | static void nft_ct_set_destroy(const struct nft_ctx *ctx, |
| 409 | const struct nft_expr *expr) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 410 | { |
Florian Westphal | 1ad8f48 | 2016-04-26 11:59:53 +0200 | [diff] [blame] | 411 | struct nft_ct *priv = nft_expr_priv(expr); |
| 412 | |
| 413 | switch (priv->key) { |
| 414 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
| 415 | case NFT_CT_LABELS: |
| 416 | nf_connlabels_put(ctx->net); |
| 417 | break; |
| 418 | #endif |
| 419 | default: |
| 420 | break; |
| 421 | } |
| 422 | |
Patrick McHardy | d46f2cd | 2014-03-07 19:08:32 +0100 | [diff] [blame] | 423 | nft_ct_l3proto_module_put(ctx->afi->family); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 426 | static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 427 | { |
| 428 | const struct nft_ct *priv = nft_expr_priv(expr); |
| 429 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 430 | if (nft_dump_register(skb, NFTA_CT_DREG, priv->dreg)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 431 | goto nla_put_failure; |
| 432 | if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key))) |
| 433 | goto nla_put_failure; |
Arturo Borrero | 2a53bfb | 2014-01-17 02:28:45 +0100 | [diff] [blame] | 434 | |
| 435 | switch (priv->key) { |
Arturo Borrero | 2a53bfb | 2014-01-17 02:28:45 +0100 | [diff] [blame] | 436 | case NFT_CT_SRC: |
| 437 | case NFT_CT_DST: |
| 438 | case NFT_CT_PROTO_SRC: |
| 439 | case NFT_CT_PROTO_DST: |
| 440 | if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir)) |
| 441 | goto nla_put_failure; |
Florian Westphal | 48f66c9 | 2016-01-07 21:34:24 +0100 | [diff] [blame] | 442 | break; |
| 443 | case NFT_CT_BYTES: |
| 444 | case NFT_CT_PKTS: |
| 445 | if (priv->dir < IP_CT_DIR_MAX && |
| 446 | nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir)) |
| 447 | goto nla_put_failure; |
| 448 | break; |
Arturo Borrero | 2a53bfb | 2014-01-17 02:28:45 +0100 | [diff] [blame] | 449 | default: |
| 450 | break; |
| 451 | } |
| 452 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 453 | return 0; |
| 454 | |
| 455 | nla_put_failure: |
| 456 | return -1; |
| 457 | } |
| 458 | |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 459 | static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 460 | { |
| 461 | const struct nft_ct *priv = nft_expr_priv(expr); |
| 462 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 463 | if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg)) |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 464 | goto nla_put_failure; |
| 465 | if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key))) |
| 466 | goto nla_put_failure; |
| 467 | return 0; |
| 468 | |
| 469 | nla_put_failure: |
| 470 | return -1; |
| 471 | } |
| 472 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 473 | static struct nft_expr_type nft_ct_type; |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 474 | static const struct nft_expr_ops nft_ct_get_ops = { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 475 | .type = &nft_ct_type, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 476 | .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)), |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 477 | .eval = nft_ct_get_eval, |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 478 | .init = nft_ct_get_init, |
Liping Zhang | 590025a | 2016-07-16 14:27:21 +0800 | [diff] [blame] | 479 | .destroy = nft_ct_get_destroy, |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 480 | .dump = nft_ct_get_dump, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 481 | }; |
| 482 | |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 483 | static const struct nft_expr_ops nft_ct_set_ops = { |
| 484 | .type = &nft_ct_type, |
| 485 | .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)), |
| 486 | .eval = nft_ct_set_eval, |
Patrick McHardy | fe92ca4 | 2014-03-29 10:43:03 +0000 | [diff] [blame] | 487 | .init = nft_ct_set_init, |
Liping Zhang | 590025a | 2016-07-16 14:27:21 +0800 | [diff] [blame] | 488 | .destroy = nft_ct_set_destroy, |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 489 | .dump = nft_ct_set_dump, |
| 490 | }; |
| 491 | |
| 492 | static const struct nft_expr_ops * |
| 493 | nft_ct_select_ops(const struct nft_ctx *ctx, |
| 494 | const struct nlattr * const tb[]) |
| 495 | { |
| 496 | if (tb[NFTA_CT_KEY] == NULL) |
| 497 | return ERR_PTR(-EINVAL); |
| 498 | |
| 499 | if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG]) |
| 500 | return ERR_PTR(-EINVAL); |
| 501 | |
| 502 | if (tb[NFTA_CT_DREG]) |
| 503 | return &nft_ct_get_ops; |
| 504 | |
| 505 | if (tb[NFTA_CT_SREG]) |
| 506 | return &nft_ct_set_ops; |
| 507 | |
| 508 | return ERR_PTR(-EINVAL); |
| 509 | } |
| 510 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 511 | static struct nft_expr_type nft_ct_type __read_mostly = { |
| 512 | .name = "ct", |
Kristian Evensen | c4ede3d | 2014-01-07 16:43:54 +0100 | [diff] [blame] | 513 | .select_ops = &nft_ct_select_ops, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 514 | .policy = nft_ct_policy, |
| 515 | .maxattr = NFTA_CT_MAX, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 516 | .owner = THIS_MODULE, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | static int __init nft_ct_module_init(void) |
| 520 | { |
Florian Westphal | adff6c6 | 2016-04-12 18:14:25 +0200 | [diff] [blame] | 521 | BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > NFT_REG_SIZE); |
| 522 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 523 | return nft_register_expr(&nft_ct_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static void __exit nft_ct_module_exit(void) |
| 527 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 528 | nft_unregister_expr(&nft_ct_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | module_init(nft_ct_module_init); |
| 532 | module_exit(nft_ct_module_exit); |
| 533 | |
| 534 | MODULE_LICENSE("GPL"); |
| 535 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 536 | MODULE_ALIAS_NFT_EXPR("ct"); |