blob: 94fb3b27a2c54393091602e0e96b2634ff8ceb1b [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
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_core.h>
18#include <net/netfilter/nf_tables.h>
19
Patrick McHardy96518512013-10-14 11:00:02 +020020static void nft_payload_eval(const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +010021 struct nft_regs *regs,
Patrick McHardy96518512013-10-14 11:00:02 +020022 const struct nft_pktinfo *pkt)
23{
24 const struct nft_payload *priv = nft_expr_priv(expr);
25 const struct sk_buff *skb = pkt->skb;
Patrick McHardy49499c32015-04-11 02:27:37 +010026 u32 *dest = &regs->data[priv->dreg];
Patrick McHardy96518512013-10-14 11:00:02 +020027 int offset;
28
29 switch (priv->base) {
30 case NFT_PAYLOAD_LL_HEADER:
31 if (!skb_mac_header_was_set(skb))
32 goto err;
33 offset = skb_mac_header(skb) - skb->data;
34 break;
35 case NFT_PAYLOAD_NETWORK_HEADER:
36 offset = skb_network_offset(skb);
37 break;
38 case NFT_PAYLOAD_TRANSPORT_HEADER:
Pablo Neira Ayusoc54032e2013-10-11 10:00:22 +020039 offset = pkt->xt.thoff;
Patrick McHardy96518512013-10-14 11:00:02 +020040 break;
41 default:
42 BUG();
43 }
44 offset += priv->offset;
45
Patrick McHardy49499c32015-04-11 02:27:37 +010046 dest[priv->len / NFT_REG32_SIZE] = 0;
Patrick McHardyfad136e2015-04-11 02:27:33 +010047 if (skb_copy_bits(skb, offset, dest, priv->len) < 0)
Patrick McHardy96518512013-10-14 11:00:02 +020048 goto err;
49 return;
50err:
Patrick McHardya55e22e2015-04-11 02:27:31 +010051 regs->verdict.code = NFT_BREAK;
Patrick McHardy96518512013-10-14 11:00:02 +020052}
53
54static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
55 [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 },
56 [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 },
57 [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 },
58 [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 },
59};
60
61static int nft_payload_init(const struct nft_ctx *ctx,
62 const struct nft_expr *expr,
63 const struct nlattr * const tb[])
64{
65 struct nft_payload *priv = nft_expr_priv(expr);
Patrick McHardy96518512013-10-14 11:00:02 +020066
Patrick McHardyc29b72e2013-10-10 11:06:41 +020067 priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
Patrick McHardy96518512013-10-14 11:00:02 +020068 priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
69 priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010070 priv->dreg = nft_parse_register(tb[NFTA_PAYLOAD_DREG]);
Patrick McHardy96518512013-10-14 11:00:02 +020071
Patrick McHardy1ec10212015-04-11 02:27:27 +010072 return nft_validate_register_store(ctx, priv->dreg, NULL,
73 NFT_DATA_VALUE, priv->len);
Patrick McHardy96518512013-10-14 11:00:02 +020074}
75
76static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr)
77{
78 const struct nft_payload *priv = nft_expr_priv(expr);
79
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010080 if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) ||
Patrick McHardy96518512013-10-14 11:00:02 +020081 nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
82 nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
83 nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)))
84 goto nla_put_failure;
85 return 0;
86
87nla_put_failure:
88 return -1;
89}
90
Patrick McHardyef1f7df2013-10-10 11:41:20 +020091static struct nft_expr_type nft_payload_type;
92static const struct nft_expr_ops nft_payload_ops = {
93 .type = &nft_payload_type,
Patrick McHardy96518512013-10-14 11:00:02 +020094 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
Patrick McHardy96518512013-10-14 11:00:02 +020095 .eval = nft_payload_eval,
96 .init = nft_payload_init,
97 .dump = nft_payload_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +020098};
99
Patrick McHardyc29b72e2013-10-10 11:06:41 +0200100const struct nft_expr_ops nft_payload_fast_ops = {
101 .type = &nft_payload_type,
102 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
103 .eval = nft_payload_eval,
104 .init = nft_payload_init,
105 .dump = nft_payload_dump,
106};
107
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200108static const struct nft_expr_ops *
109nft_payload_select_ops(const struct nft_ctx *ctx,
110 const struct nlattr * const tb[])
Patrick McHardyc29b72e2013-10-10 11:06:41 +0200111{
112 enum nft_payload_bases base;
113 unsigned int offset, len;
114
115 if (tb[NFTA_PAYLOAD_DREG] == NULL ||
116 tb[NFTA_PAYLOAD_BASE] == NULL ||
117 tb[NFTA_PAYLOAD_OFFSET] == NULL ||
118 tb[NFTA_PAYLOAD_LEN] == NULL)
119 return ERR_PTR(-EINVAL);
120
121 base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
122 switch (base) {
123 case NFT_PAYLOAD_LL_HEADER:
124 case NFT_PAYLOAD_NETWORK_HEADER:
125 case NFT_PAYLOAD_TRANSPORT_HEADER:
126 break;
127 default:
128 return ERR_PTR(-EOPNOTSUPP);
129 }
130
131 offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
Patrick McHardy45d9bcd2015-04-11 02:27:26 +0100132 len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
Patrick McHardyc29b72e2013-10-10 11:06:41 +0200133
Nikolay Aleksandrovf627ed92014-02-16 14:01:58 +0100134 if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) &&
135 base != NFT_PAYLOAD_LL_HEADER)
Patrick McHardyc29b72e2013-10-10 11:06:41 +0200136 return &nft_payload_fast_ops;
137 else
138 return &nft_payload_ops;
139}
140
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200141static struct nft_expr_type nft_payload_type __read_mostly = {
142 .name = "payload",
Patrick McHardyc29b72e2013-10-10 11:06:41 +0200143 .select_ops = nft_payload_select_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200144 .policy = nft_payload_policy,
145 .maxattr = NFTA_PAYLOAD_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200146 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200147};
148
149int __init nft_payload_module_init(void)
150{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200151 return nft_register_expr(&nft_payload_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200152}
153
154void nft_payload_module_exit(void)
155{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200156 nft_unregister_expr(&nft_payload_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200157}