]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nft_payload.c
netfilter: nf_tables: remove unused struct members
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nft_payload.c
CommitLineData
96518518 1/*
ef1f7df9 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
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>
8cfd23e6 12#include <linux/if_vlan.h>
96518518
PM
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
8cfd23e6
FW
21/* add vlan header into the user buffer for if tag was removed by offloads */
22static bool
23nft_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
96518518 68static void nft_payload_eval(const struct nft_expr *expr,
a55e22e9 69 struct nft_regs *regs,
96518518
PM
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;
49499c3e 74 u32 *dest = &regs->data[priv->dreg];
96518518
PM
75 int offset;
76
8cfd23e6 77 dest[priv->len / NFT_REG32_SIZE] = 0;
96518518
PM
78 switch (priv->base) {
79 case NFT_PAYLOAD_LL_HEADER:
80 if (!skb_mac_header_was_set(skb))
81 goto err;
8cfd23e6
FW
82
83 if (skb_vlan_tag_present(skb)) {
84 if (!nft_payload_copy_vlan(dest, skb,
85 priv->offset, priv->len))
86 goto err;
87 return;
88 }
96518518
PM
89 offset = skb_mac_header(skb) - skb->data;
90 break;
91 case NFT_PAYLOAD_NETWORK_HEADER:
92 offset = skb_network_offset(skb);
93 break;
94 case NFT_PAYLOAD_TRANSPORT_HEADER:
c54032e0 95 offset = pkt->xt.thoff;
96518518
PM
96 break;
97 default:
98 BUG();
99 }
100 offset += priv->offset;
101
fad136ea 102 if (skb_copy_bits(skb, offset, dest, priv->len) < 0)
96518518
PM
103 goto err;
104 return;
105err:
a55e22e9 106 regs->verdict.code = NFT_BREAK;
96518518
PM
107}
108
109static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
110 [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 },
111 [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 },
112 [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 },
113 [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 },
114};
115
116static int nft_payload_init(const struct nft_ctx *ctx,
117 const struct nft_expr *expr,
118 const struct nlattr * const tb[])
119{
120 struct nft_payload *priv = nft_expr_priv(expr);
96518518 121
c29b72e0 122 priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
96518518
PM
123 priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
124 priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
b1c96ed3 125 priv->dreg = nft_parse_register(tb[NFTA_PAYLOAD_DREG]);
96518518 126
1ec10212
PM
127 return nft_validate_register_store(ctx, priv->dreg, NULL,
128 NFT_DATA_VALUE, priv->len);
96518518
PM
129}
130
131static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr)
132{
133 const struct nft_payload *priv = nft_expr_priv(expr);
134
b1c96ed3 135 if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) ||
96518518
PM
136 nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
137 nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
138 nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)))
139 goto nla_put_failure;
140 return 0;
141
142nla_put_failure:
143 return -1;
144}
145
ef1f7df9
PM
146static struct nft_expr_type nft_payload_type;
147static const struct nft_expr_ops nft_payload_ops = {
148 .type = &nft_payload_type,
96518518 149 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
96518518
PM
150 .eval = nft_payload_eval,
151 .init = nft_payload_init,
152 .dump = nft_payload_dump,
ef1f7df9
PM
153};
154
c29b72e0
PM
155const struct nft_expr_ops nft_payload_fast_ops = {
156 .type = &nft_payload_type,
157 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
158 .eval = nft_payload_eval,
159 .init = nft_payload_init,
160 .dump = nft_payload_dump,
161};
162
0ca743a5
PNA
163static const struct nft_expr_ops *
164nft_payload_select_ops(const struct nft_ctx *ctx,
165 const struct nlattr * const tb[])
c29b72e0
PM
166{
167 enum nft_payload_bases base;
168 unsigned int offset, len;
169
170 if (tb[NFTA_PAYLOAD_DREG] == NULL ||
171 tb[NFTA_PAYLOAD_BASE] == NULL ||
172 tb[NFTA_PAYLOAD_OFFSET] == NULL ||
173 tb[NFTA_PAYLOAD_LEN] == NULL)
174 return ERR_PTR(-EINVAL);
175
176 base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
177 switch (base) {
178 case NFT_PAYLOAD_LL_HEADER:
179 case NFT_PAYLOAD_NETWORK_HEADER:
180 case NFT_PAYLOAD_TRANSPORT_HEADER:
181 break;
182 default:
183 return ERR_PTR(-EOPNOTSUPP);
184 }
185
186 offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
45d9bcda 187 len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
c29b72e0 188
f627ed91
NA
189 if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) &&
190 base != NFT_PAYLOAD_LL_HEADER)
c29b72e0
PM
191 return &nft_payload_fast_ops;
192 else
193 return &nft_payload_ops;
194}
195
ef1f7df9
PM
196static struct nft_expr_type nft_payload_type __read_mostly = {
197 .name = "payload",
c29b72e0 198 .select_ops = nft_payload_select_ops,
96518518
PM
199 .policy = nft_payload_policy,
200 .maxattr = NFTA_PAYLOAD_MAX,
ef1f7df9 201 .owner = THIS_MODULE,
96518518
PM
202};
203
204int __init nft_payload_module_init(void)
205{
ef1f7df9 206 return nft_register_expr(&nft_payload_type);
96518518
PM
207}
208
209void nft_payload_module_exit(void)
210{
ef1f7df9 211 nft_unregister_expr(&nft_payload_type);
96518518 212}