GNU Linux-libre 4.4.285-gnu1
[releases.git] / net / netfilter / nft_payload.c
1 /*
2  * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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/if_vlan.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables_core.h>
19 #include <net/netfilter/nf_tables.h>
20
21 /* add vlan header into the user buffer for if tag was removed by offloads */
22 static bool
23 nft_payload_copy_vlan(u32 *d, const struct sk_buff *skb, u8 offset, u8 len)
24 {
25         int mac_off = skb_mac_header(skb) - skb->data;
26         u8 vlan_len, *vlanh, *dst_u8 = (u8 *) d;
27         struct vlan_ethhdr veth;
28
29         vlanh = (u8 *) &veth;
30         if (offset < ETH_HLEN) {
31                 u8 ethlen = min_t(u8, len, ETH_HLEN - offset);
32
33                 if (skb_copy_bits(skb, mac_off, &veth, ETH_HLEN))
34                         return false;
35
36                 veth.h_vlan_proto = skb->vlan_proto;
37
38                 memcpy(dst_u8, vlanh + offset, ethlen);
39
40                 len -= ethlen;
41                 if (len == 0)
42                         return true;
43
44                 dst_u8 += ethlen;
45                 offset = ETH_HLEN;
46         } else if (offset >= VLAN_ETH_HLEN) {
47                 offset -= VLAN_HLEN;
48                 goto skip;
49         }
50
51         veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
52         veth.h_vlan_encapsulated_proto = skb->protocol;
53
54         vlanh += offset;
55
56         vlan_len = min_t(u8, len, VLAN_ETH_HLEN - offset);
57         memcpy(dst_u8, vlanh, vlan_len);
58
59         len -= vlan_len;
60         if (!len)
61                 return true;
62
63         dst_u8 += vlan_len;
64  skip:
65         return skb_copy_bits(skb, offset + mac_off, dst_u8, len) == 0;
66 }
67
68 static void nft_payload_eval(const struct nft_expr *expr,
69                              struct nft_regs *regs,
70                              const struct nft_pktinfo *pkt)
71 {
72         const struct nft_payload *priv = nft_expr_priv(expr);
73         const struct sk_buff *skb = pkt->skb;
74         u32 *dest = &regs->data[priv->dreg];
75         int offset;
76
77         if (priv->len % NFT_REG32_SIZE)
78                 dest[priv->len / NFT_REG32_SIZE] = 0;
79
80         switch (priv->base) {
81         case NFT_PAYLOAD_LL_HEADER:
82                 if (!skb_mac_header_was_set(skb))
83                         goto err;
84
85                 if (skb_vlan_tag_present(skb)) {
86                         if (!nft_payload_copy_vlan(dest, skb,
87                                                    priv->offset, priv->len))
88                                 goto err;
89                         return;
90                 }
91                 offset = skb_mac_header(skb) - skb->data;
92                 break;
93         case NFT_PAYLOAD_NETWORK_HEADER:
94                 offset = skb_network_offset(skb);
95                 break;
96         case NFT_PAYLOAD_TRANSPORT_HEADER:
97                 offset = pkt->xt.thoff;
98                 break;
99         default:
100                 BUG();
101         }
102         offset += priv->offset;
103
104         if (skb_copy_bits(skb, offset, dest, priv->len) < 0)
105                 goto err;
106         return;
107 err:
108         regs->verdict.code = NFT_BREAK;
109 }
110
111 static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
112         [NFTA_PAYLOAD_DREG]     = { .type = NLA_U32 },
113         [NFTA_PAYLOAD_BASE]     = { .type = NLA_U32 },
114         [NFTA_PAYLOAD_OFFSET]   = { .type = NLA_U32 },
115         [NFTA_PAYLOAD_LEN]      = { .type = NLA_U32 },
116 };
117
118 static int nft_payload_init(const struct nft_ctx *ctx,
119                             const struct nft_expr *expr,
120                             const struct nlattr * const tb[])
121 {
122         struct nft_payload *priv = nft_expr_priv(expr);
123
124         priv->base   = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
125         priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
126         priv->len    = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
127         priv->dreg   = nft_parse_register(tb[NFTA_PAYLOAD_DREG]);
128
129         return nft_validate_register_store(ctx, priv->dreg, NULL,
130                                            NFT_DATA_VALUE, priv->len);
131 }
132
133 static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr)
134 {
135         const struct nft_payload *priv = nft_expr_priv(expr);
136
137         if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) ||
138             nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
139             nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
140             nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)))
141                 goto nla_put_failure;
142         return 0;
143
144 nla_put_failure:
145         return -1;
146 }
147
148 static struct nft_expr_type nft_payload_type;
149 static const struct nft_expr_ops nft_payload_ops = {
150         .type           = &nft_payload_type,
151         .size           = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
152         .eval           = nft_payload_eval,
153         .init           = nft_payload_init,
154         .dump           = nft_payload_dump,
155 };
156
157 const struct nft_expr_ops nft_payload_fast_ops = {
158         .type           = &nft_payload_type,
159         .size           = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
160         .eval           = nft_payload_eval,
161         .init           = nft_payload_init,
162         .dump           = nft_payload_dump,
163 };
164
165 static const struct nft_expr_ops *
166 nft_payload_select_ops(const struct nft_ctx *ctx,
167                        const struct nlattr * const tb[])
168 {
169         enum nft_payload_bases base;
170         unsigned int offset, len;
171
172         if (tb[NFTA_PAYLOAD_DREG] == NULL ||
173             tb[NFTA_PAYLOAD_BASE] == NULL ||
174             tb[NFTA_PAYLOAD_OFFSET] == NULL ||
175             tb[NFTA_PAYLOAD_LEN] == NULL)
176                 return ERR_PTR(-EINVAL);
177
178         base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
179         switch (base) {
180         case NFT_PAYLOAD_LL_HEADER:
181         case NFT_PAYLOAD_NETWORK_HEADER:
182         case NFT_PAYLOAD_TRANSPORT_HEADER:
183                 break;
184         default:
185                 return ERR_PTR(-EOPNOTSUPP);
186         }
187
188         offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
189         len    = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
190
191         if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) &&
192             base != NFT_PAYLOAD_LL_HEADER)
193                 return &nft_payload_fast_ops;
194         else
195                 return &nft_payload_ops;
196 }
197
198 static struct nft_expr_type nft_payload_type __read_mostly = {
199         .name           = "payload",
200         .select_ops     = nft_payload_select_ops,
201         .policy         = nft_payload_policy,
202         .maxattr        = NFTA_PAYLOAD_MAX,
203         .owner          = THIS_MODULE,
204 };
205
206 int __init nft_payload_module_init(void)
207 {
208         return nft_register_expr(&nft_payload_type);
209 }
210
211 void nft_payload_module_exit(void)
212 {
213         nft_unregister_expr(&nft_payload_type);
214 }