]>
Commit | Line | Data |
---|---|---|
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> | |
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 | ||
96518518 PM |
20 | static void nft_payload_eval(const struct nft_expr *expr, |
21 | struct nft_data data[NFT_REG_MAX + 1], | |
22 | const struct nft_pktinfo *pkt) | |
23 | { | |
24 | const struct nft_payload *priv = nft_expr_priv(expr); | |
25 | const struct sk_buff *skb = pkt->skb; | |
26 | struct nft_data *dest = &data[priv->dreg]; | |
27 | 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: | |
c54032e0 | 39 | offset = pkt->xt.thoff; |
96518518 PM |
40 | break; |
41 | default: | |
42 | BUG(); | |
43 | } | |
44 | offset += priv->offset; | |
45 | ||
46 | if (skb_copy_bits(skb, offset, dest->data, priv->len) < 0) | |
47 | goto err; | |
48 | return; | |
49 | err: | |
50 | data[NFT_REG_VERDICT].verdict = NFT_BREAK; | |
51 | } | |
52 | ||
53 | static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = { | |
54 | [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 }, | |
55 | [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 }, | |
56 | [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 }, | |
57 | [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 }, | |
58 | }; | |
59 | ||
60 | static int nft_payload_init(const struct nft_ctx *ctx, | |
61 | const struct nft_expr *expr, | |
62 | const struct nlattr * const tb[]) | |
63 | { | |
64 | struct nft_payload *priv = nft_expr_priv(expr); | |
96518518 | 65 | |
c29b72e0 | 66 | priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); |
96518518 PM |
67 | priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); |
68 | priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); | |
27e6d201 | 69 | priv->dreg = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_DREG])); |
96518518 | 70 | |
1ec10212 PM |
71 | return nft_validate_register_store(ctx, priv->dreg, NULL, |
72 | NFT_DATA_VALUE, priv->len); | |
96518518 PM |
73 | } |
74 | ||
75 | static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
76 | { | |
77 | const struct nft_payload *priv = nft_expr_priv(expr); | |
78 | ||
79 | if (nla_put_be32(skb, NFTA_PAYLOAD_DREG, htonl(priv->dreg)) || | |
80 | nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) || | |
81 | nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) || | |
82 | nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len))) | |
83 | goto nla_put_failure; | |
84 | return 0; | |
85 | ||
86 | nla_put_failure: | |
87 | return -1; | |
88 | } | |
89 | ||
ef1f7df9 PM |
90 | static struct nft_expr_type nft_payload_type; |
91 | static const struct nft_expr_ops nft_payload_ops = { | |
92 | .type = &nft_payload_type, | |
96518518 | 93 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)), |
96518518 PM |
94 | .eval = nft_payload_eval, |
95 | .init = nft_payload_init, | |
96 | .dump = nft_payload_dump, | |
ef1f7df9 PM |
97 | }; |
98 | ||
c29b72e0 PM |
99 | const struct nft_expr_ops nft_payload_fast_ops = { |
100 | .type = &nft_payload_type, | |
101 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)), | |
102 | .eval = nft_payload_eval, | |
103 | .init = nft_payload_init, | |
104 | .dump = nft_payload_dump, | |
105 | }; | |
106 | ||
0ca743a5 PNA |
107 | static const struct nft_expr_ops * |
108 | nft_payload_select_ops(const struct nft_ctx *ctx, | |
109 | const struct nlattr * const tb[]) | |
c29b72e0 PM |
110 | { |
111 | enum nft_payload_bases base; | |
112 | unsigned int offset, len; | |
113 | ||
114 | if (tb[NFTA_PAYLOAD_DREG] == NULL || | |
115 | tb[NFTA_PAYLOAD_BASE] == NULL || | |
116 | tb[NFTA_PAYLOAD_OFFSET] == NULL || | |
117 | tb[NFTA_PAYLOAD_LEN] == NULL) | |
118 | return ERR_PTR(-EINVAL); | |
119 | ||
120 | base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); | |
121 | switch (base) { | |
122 | case NFT_PAYLOAD_LL_HEADER: | |
123 | case NFT_PAYLOAD_NETWORK_HEADER: | |
124 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
125 | break; | |
126 | default: | |
127 | return ERR_PTR(-EOPNOTSUPP); | |
128 | } | |
129 | ||
130 | offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); | |
45d9bcda | 131 | len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); |
c29b72e0 | 132 | |
f627ed91 NA |
133 | if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) && |
134 | base != NFT_PAYLOAD_LL_HEADER) | |
c29b72e0 PM |
135 | return &nft_payload_fast_ops; |
136 | else | |
137 | return &nft_payload_ops; | |
138 | } | |
139 | ||
ef1f7df9 PM |
140 | static struct nft_expr_type nft_payload_type __read_mostly = { |
141 | .name = "payload", | |
c29b72e0 | 142 | .select_ops = nft_payload_select_ops, |
96518518 PM |
143 | .policy = nft_payload_policy, |
144 | .maxattr = NFTA_PAYLOAD_MAX, | |
ef1f7df9 | 145 | .owner = THIS_MODULE, |
96518518 PM |
146 | }; |
147 | ||
148 | int __init nft_payload_module_init(void) | |
149 | { | |
ef1f7df9 | 150 | return nft_register_expr(&nft_payload_type); |
96518518 PM |
151 | } |
152 | ||
153 | void nft_payload_module_exit(void) | |
154 | { | |
ef1f7df9 | 155 | nft_unregister_expr(&nft_payload_type); |
96518518 | 156 | } |