]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
ef1f7df9 | 3 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
18140969 | 4 | * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org> |
96518518 | 5 | * |
96518518 PM |
6 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
7 | */ | |
8 | ||
9 | #include <linux/kernel.h> | |
8cfd23e6 | 10 | #include <linux/if_vlan.h> |
96518518 PM |
11 | #include <linux/init.h> |
12 | #include <linux/module.h> | |
13 | #include <linux/netlink.h> | |
14 | #include <linux/netfilter.h> | |
15 | #include <linux/netfilter/nf_tables.h> | |
16 | #include <net/netfilter/nf_tables_core.h> | |
17 | #include <net/netfilter/nf_tables.h> | |
c9626a2c | 18 | #include <net/netfilter/nf_tables_offload.h> |
18140969 PNA |
19 | /* For layer 4 checksum field offset. */ |
20 | #include <linux/tcp.h> | |
21 | #include <linux/udp.h> | |
22 | #include <linux/icmpv6.h> | |
c9626a2c PNA |
23 | #include <linux/ip.h> |
24 | #include <linux/ipv6.h> | |
346e320c | 25 | #include <net/sctp/checksum.h> |
96518518 | 26 | |
8dfd8b09 PNA |
27 | static bool nft_payload_rebuild_vlan_hdr(const struct sk_buff *skb, int mac_off, |
28 | struct vlan_ethhdr *veth) | |
29 | { | |
30 | if (skb_copy_bits(skb, mac_off, veth, ETH_HLEN)) | |
31 | return false; | |
32 | ||
33 | veth->h_vlan_proto = skb->vlan_proto; | |
34 | veth->h_vlan_TCI = htons(skb_vlan_tag_get(skb)); | |
35 | veth->h_vlan_encapsulated_proto = skb->protocol; | |
36 | ||
37 | return true; | |
38 | } | |
39 | ||
8cfd23e6 FW |
40 | /* add vlan header into the user buffer for if tag was removed by offloads */ |
41 | static bool | |
42 | nft_payload_copy_vlan(u32 *d, const struct sk_buff *skb, u8 offset, u8 len) | |
43 | { | |
44 | int mac_off = skb_mac_header(skb) - skb->data; | |
f41f72d0 | 45 | u8 *vlanh, *dst_u8 = (u8 *) d; |
8cfd23e6 | 46 | struct vlan_ethhdr veth; |
f6ae9f12 PNA |
47 | u8 vlan_hlen = 0; |
48 | ||
49 | if ((skb->protocol == htons(ETH_P_8021AD) || | |
50 | skb->protocol == htons(ETH_P_8021Q)) && | |
51 | offset >= VLAN_ETH_HLEN && offset < VLAN_ETH_HLEN + VLAN_HLEN) | |
52 | vlan_hlen += VLAN_HLEN; | |
8cfd23e6 FW |
53 | |
54 | vlanh = (u8 *) &veth; | |
f6ae9f12 | 55 | if (offset < VLAN_ETH_HLEN + vlan_hlen) { |
f41f72d0 | 56 | u8 ethlen = len; |
8cfd23e6 | 57 | |
f6ae9f12 PNA |
58 | if (vlan_hlen && |
59 | skb_copy_bits(skb, mac_off, &veth, VLAN_ETH_HLEN) < 0) | |
60 | return false; | |
61 | else if (!nft_payload_rebuild_vlan_hdr(skb, mac_off, &veth)) | |
8cfd23e6 FW |
62 | return false; |
63 | ||
f6ae9f12 PNA |
64 | if (offset + len > VLAN_ETH_HLEN + vlan_hlen) |
65 | ethlen -= offset + len - VLAN_ETH_HLEN + vlan_hlen; | |
8cfd23e6 | 66 | |
f6ae9f12 | 67 | memcpy(dst_u8, vlanh + offset - vlan_hlen, ethlen); |
8cfd23e6 FW |
68 | |
69 | len -= ethlen; | |
70 | if (len == 0) | |
71 | return true; | |
72 | ||
73 | dst_u8 += ethlen; | |
f6ae9f12 | 74 | offset = ETH_HLEN + vlan_hlen; |
f41f72d0 | 75 | } else { |
f6ae9f12 | 76 | offset -= VLAN_HLEN + vlan_hlen; |
8cfd23e6 FW |
77 | } |
78 | ||
8cfd23e6 FW |
79 | return skb_copy_bits(skb, offset + mac_off, dst_u8, len) == 0; |
80 | } | |
81 | ||
10870dd8 FW |
82 | void nft_payload_eval(const struct nft_expr *expr, |
83 | struct nft_regs *regs, | |
84 | const struct nft_pktinfo *pkt) | |
96518518 PM |
85 | { |
86 | const struct nft_payload *priv = nft_expr_priv(expr); | |
87 | const struct sk_buff *skb = pkt->skb; | |
49499c3e | 88 | u32 *dest = ®s->data[priv->dreg]; |
96518518 PM |
89 | int offset; |
90 | ||
1e105e6a FW |
91 | if (priv->len % NFT_REG32_SIZE) |
92 | dest[priv->len / NFT_REG32_SIZE] = 0; | |
93 | ||
96518518 PM |
94 | switch (priv->base) { |
95 | case NFT_PAYLOAD_LL_HEADER: | |
96 | if (!skb_mac_header_was_set(skb)) | |
97 | goto err; | |
8cfd23e6 FW |
98 | |
99 | if (skb_vlan_tag_present(skb)) { | |
100 | if (!nft_payload_copy_vlan(dest, skb, | |
101 | priv->offset, priv->len)) | |
102 | goto err; | |
103 | return; | |
104 | } | |
96518518 PM |
105 | offset = skb_mac_header(skb) - skb->data; |
106 | break; | |
107 | case NFT_PAYLOAD_NETWORK_HEADER: | |
108 | offset = skb_network_offset(skb); | |
109 | break; | |
110 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
a20877b5 LZ |
111 | if (!pkt->tprot_set) |
112 | goto err; | |
2d7b4ace | 113 | offset = nft_thoff(pkt); |
96518518 PM |
114 | break; |
115 | default: | |
116 | BUG(); | |
117 | } | |
118 | offset += priv->offset; | |
119 | ||
fad136ea | 120 | if (skb_copy_bits(skb, offset, dest, priv->len) < 0) |
96518518 PM |
121 | goto err; |
122 | return; | |
123 | err: | |
a55e22e9 | 124 | regs->verdict.code = NFT_BREAK; |
96518518 PM |
125 | } |
126 | ||
127 | static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = { | |
7ec3f7b4 PM |
128 | [NFTA_PAYLOAD_SREG] = { .type = NLA_U32 }, |
129 | [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 }, | |
130 | [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 }, | |
131 | [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 }, | |
132 | [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 }, | |
133 | [NFTA_PAYLOAD_CSUM_TYPE] = { .type = NLA_U32 }, | |
134 | [NFTA_PAYLOAD_CSUM_OFFSET] = { .type = NLA_U32 }, | |
9d6effb2 | 135 | [NFTA_PAYLOAD_CSUM_FLAGS] = { .type = NLA_U32 }, |
96518518 PM |
136 | }; |
137 | ||
138 | static int nft_payload_init(const struct nft_ctx *ctx, | |
139 | const struct nft_expr *expr, | |
140 | const struct nlattr * const tb[]) | |
141 | { | |
142 | struct nft_payload *priv = nft_expr_priv(expr); | |
96518518 | 143 | |
c29b72e0 | 144 | priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); |
96518518 PM |
145 | priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); |
146 | priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); | |
96518518 | 147 | |
345023b0 PNA |
148 | return nft_parse_register_store(ctx, tb[NFTA_PAYLOAD_DREG], |
149 | &priv->dreg, NULL, NFT_DATA_VALUE, | |
150 | priv->len); | |
96518518 PM |
151 | } |
152 | ||
153 | static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
154 | { | |
155 | const struct nft_payload *priv = nft_expr_priv(expr); | |
156 | ||
b1c96ed3 | 157 | if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) || |
96518518 PM |
158 | nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) || |
159 | nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) || | |
160 | nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len))) | |
161 | goto nla_put_failure; | |
162 | return 0; | |
163 | ||
164 | nla_put_failure: | |
165 | return -1; | |
166 | } | |
167 | ||
a5d45bc0 PNA |
168 | static bool nft_payload_offload_mask(struct nft_offload_reg *reg, |
169 | u32 priv_len, u32 field_len) | |
170 | { | |
171 | unsigned int remainder, delta, k; | |
172 | struct nft_data mask = {}; | |
173 | __be32 remainder_mask; | |
174 | ||
175 | if (priv_len == field_len) { | |
176 | memset(®->mask, 0xff, priv_len); | |
177 | return true; | |
178 | } else if (priv_len > field_len) { | |
179 | return false; | |
180 | } | |
181 | ||
182 | memset(&mask, 0xff, field_len); | |
183 | remainder = priv_len % sizeof(u32); | |
184 | if (remainder) { | |
185 | k = priv_len / sizeof(u32); | |
186 | delta = field_len - priv_len; | |
187 | remainder_mask = htonl(~((1 << (delta * BITS_PER_BYTE)) - 1)); | |
188 | mask.data[k] = (__force u32)remainder_mask; | |
189 | } | |
190 | ||
191 | memcpy(®->mask, &mask, field_len); | |
192 | ||
193 | return true; | |
194 | } | |
195 | ||
c9626a2c PNA |
196 | static int nft_payload_offload_ll(struct nft_offload_ctx *ctx, |
197 | struct nft_flow_rule *flow, | |
198 | const struct nft_payload *priv) | |
199 | { | |
200 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
201 | ||
202 | switch (priv->offset) { | |
203 | case offsetof(struct ethhdr, h_source): | |
a5d45bc0 | 204 | if (!nft_payload_offload_mask(reg, priv->len, ETH_ALEN)) |
a69a85da | 205 | return -EOPNOTSUPP; |
206 | ||
c9626a2c PNA |
207 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs, |
208 | src, ETH_ALEN, reg); | |
209 | break; | |
210 | case offsetof(struct ethhdr, h_dest): | |
a5d45bc0 | 211 | if (!nft_payload_offload_mask(reg, priv->len, ETH_ALEN)) |
a69a85da | 212 | return -EOPNOTSUPP; |
213 | ||
c9626a2c PNA |
214 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs, |
215 | dst, ETH_ALEN, reg); | |
216 | break; | |
a82055af | 217 | case offsetof(struct ethhdr, h_proto): |
a5d45bc0 | 218 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
a82055af PNA |
219 | return -EOPNOTSUPP; |
220 | ||
221 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, | |
222 | n_proto, sizeof(__be16), reg); | |
223 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_NETWORK); | |
224 | break; | |
225 | case offsetof(struct vlan_ethhdr, h_vlan_TCI): | |
a5d45bc0 | 226 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
a82055af PNA |
227 | return -EOPNOTSUPP; |
228 | ||
ff4d90a8 PNA |
229 | NFT_OFFLOAD_MATCH_FLAGS(FLOW_DISSECTOR_KEY_VLAN, vlan, |
230 | vlan_tci, sizeof(__be16), reg, | |
231 | NFT_OFFLOAD_F_NETWORK2HOST); | |
a82055af PNA |
232 | break; |
233 | case offsetof(struct vlan_ethhdr, h_vlan_encapsulated_proto): | |
a5d45bc0 | 234 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
a82055af PNA |
235 | return -EOPNOTSUPP; |
236 | ||
237 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_VLAN, vlan, | |
238 | vlan_tpid, sizeof(__be16), reg); | |
89d8fd44 PNA |
239 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_NETWORK); |
240 | break; | |
241 | case offsetof(struct vlan_ethhdr, h_vlan_TCI) + sizeof(struct vlan_hdr): | |
a5d45bc0 | 242 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
89d8fd44 PNA |
243 | return -EOPNOTSUPP; |
244 | ||
ff4d90a8 PNA |
245 | NFT_OFFLOAD_MATCH_FLAGS(FLOW_DISSECTOR_KEY_CVLAN, cvlan, |
246 | vlan_tci, sizeof(__be16), reg, | |
247 | NFT_OFFLOAD_F_NETWORK2HOST); | |
89d8fd44 PNA |
248 | break; |
249 | case offsetof(struct vlan_ethhdr, h_vlan_encapsulated_proto) + | |
250 | sizeof(struct vlan_hdr): | |
a5d45bc0 | 251 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
89d8fd44 PNA |
252 | return -EOPNOTSUPP; |
253 | ||
14c20643 | 254 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_CVLAN, cvlan, |
89d8fd44 | 255 | vlan_tpid, sizeof(__be16), reg); |
14c20643 | 256 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_NETWORK); |
a82055af | 257 | break; |
a69a85da | 258 | default: |
259 | return -EOPNOTSUPP; | |
c9626a2c PNA |
260 | } |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static int nft_payload_offload_ip(struct nft_offload_ctx *ctx, | |
266 | struct nft_flow_rule *flow, | |
267 | const struct nft_payload *priv) | |
268 | { | |
269 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
270 | ||
271 | switch (priv->offset) { | |
272 | case offsetof(struct iphdr, saddr): | |
a5d45bc0 PNA |
273 | if (!nft_payload_offload_mask(reg, priv->len, |
274 | sizeof(struct in_addr))) | |
a69a85da | 275 | return -EOPNOTSUPP; |
276 | ||
c9626a2c PNA |
277 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, src, |
278 | sizeof(struct in_addr), reg); | |
3c78e9e0 | 279 | nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV4_ADDRS); |
c9626a2c PNA |
280 | break; |
281 | case offsetof(struct iphdr, daddr): | |
a5d45bc0 PNA |
282 | if (!nft_payload_offload_mask(reg, priv->len, |
283 | sizeof(struct in_addr))) | |
a69a85da | 284 | return -EOPNOTSUPP; |
285 | ||
c9626a2c PNA |
286 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, dst, |
287 | sizeof(struct in_addr), reg); | |
3c78e9e0 | 288 | nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV4_ADDRS); |
c9626a2c PNA |
289 | break; |
290 | case offsetof(struct iphdr, protocol): | |
a5d45bc0 | 291 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__u8))) |
a69a85da | 292 | return -EOPNOTSUPP; |
293 | ||
c9626a2c PNA |
294 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto, |
295 | sizeof(__u8), reg); | |
296 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT); | |
297 | break; | |
298 | default: | |
299 | return -EOPNOTSUPP; | |
300 | } | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | static int nft_payload_offload_ip6(struct nft_offload_ctx *ctx, | |
306 | struct nft_flow_rule *flow, | |
307 | const struct nft_payload *priv) | |
308 | { | |
309 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
310 | ||
311 | switch (priv->offset) { | |
312 | case offsetof(struct ipv6hdr, saddr): | |
a5d45bc0 PNA |
313 | if (!nft_payload_offload_mask(reg, priv->len, |
314 | sizeof(struct in6_addr))) | |
a69a85da | 315 | return -EOPNOTSUPP; |
316 | ||
c9626a2c PNA |
317 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, src, |
318 | sizeof(struct in6_addr), reg); | |
3c78e9e0 | 319 | nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV6_ADDRS); |
c9626a2c PNA |
320 | break; |
321 | case offsetof(struct ipv6hdr, daddr): | |
a5d45bc0 PNA |
322 | if (!nft_payload_offload_mask(reg, priv->len, |
323 | sizeof(struct in6_addr))) | |
a69a85da | 324 | return -EOPNOTSUPP; |
325 | ||
c9626a2c PNA |
326 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, dst, |
327 | sizeof(struct in6_addr), reg); | |
3c78e9e0 | 328 | nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV6_ADDRS); |
c9626a2c PNA |
329 | break; |
330 | case offsetof(struct ipv6hdr, nexthdr): | |
a5d45bc0 | 331 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__u8))) |
a69a85da | 332 | return -EOPNOTSUPP; |
333 | ||
c9626a2c PNA |
334 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto, |
335 | sizeof(__u8), reg); | |
336 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT); | |
337 | break; | |
338 | default: | |
339 | return -EOPNOTSUPP; | |
340 | } | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
345 | static int nft_payload_offload_nh(struct nft_offload_ctx *ctx, | |
346 | struct nft_flow_rule *flow, | |
347 | const struct nft_payload *priv) | |
348 | { | |
349 | int err; | |
350 | ||
351 | switch (ctx->dep.l3num) { | |
352 | case htons(ETH_P_IP): | |
353 | err = nft_payload_offload_ip(ctx, flow, priv); | |
354 | break; | |
355 | case htons(ETH_P_IPV6): | |
356 | err = nft_payload_offload_ip6(ctx, flow, priv); | |
357 | break; | |
358 | default: | |
359 | return -EOPNOTSUPP; | |
360 | } | |
361 | ||
362 | return err; | |
363 | } | |
364 | ||
365 | static int nft_payload_offload_tcp(struct nft_offload_ctx *ctx, | |
366 | struct nft_flow_rule *flow, | |
367 | const struct nft_payload *priv) | |
368 | { | |
369 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
370 | ||
371 | switch (priv->offset) { | |
372 | case offsetof(struct tcphdr, source): | |
a5d45bc0 | 373 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
a69a85da | 374 | return -EOPNOTSUPP; |
375 | ||
c9626a2c PNA |
376 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src, |
377 | sizeof(__be16), reg); | |
378 | break; | |
379 | case offsetof(struct tcphdr, dest): | |
a5d45bc0 | 380 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
a69a85da | 381 | return -EOPNOTSUPP; |
382 | ||
c9626a2c PNA |
383 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst, |
384 | sizeof(__be16), reg); | |
385 | break; | |
386 | default: | |
387 | return -EOPNOTSUPP; | |
388 | } | |
389 | ||
390 | return 0; | |
391 | } | |
392 | ||
393 | static int nft_payload_offload_udp(struct nft_offload_ctx *ctx, | |
394 | struct nft_flow_rule *flow, | |
395 | const struct nft_payload *priv) | |
396 | { | |
397 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
398 | ||
399 | switch (priv->offset) { | |
400 | case offsetof(struct udphdr, source): | |
a5d45bc0 | 401 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
a69a85da | 402 | return -EOPNOTSUPP; |
403 | ||
c9626a2c PNA |
404 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src, |
405 | sizeof(__be16), reg); | |
406 | break; | |
407 | case offsetof(struct udphdr, dest): | |
a5d45bc0 | 408 | if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16))) |
a69a85da | 409 | return -EOPNOTSUPP; |
410 | ||
c9626a2c PNA |
411 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst, |
412 | sizeof(__be16), reg); | |
413 | break; | |
414 | default: | |
415 | return -EOPNOTSUPP; | |
416 | } | |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
421 | static int nft_payload_offload_th(struct nft_offload_ctx *ctx, | |
422 | struct nft_flow_rule *flow, | |
423 | const struct nft_payload *priv) | |
424 | { | |
425 | int err; | |
426 | ||
427 | switch (ctx->dep.protonum) { | |
428 | case IPPROTO_TCP: | |
429 | err = nft_payload_offload_tcp(ctx, flow, priv); | |
430 | break; | |
431 | case IPPROTO_UDP: | |
432 | err = nft_payload_offload_udp(ctx, flow, priv); | |
433 | break; | |
434 | default: | |
435 | return -EOPNOTSUPP; | |
436 | } | |
437 | ||
438 | return err; | |
439 | } | |
440 | ||
441 | static int nft_payload_offload(struct nft_offload_ctx *ctx, | |
442 | struct nft_flow_rule *flow, | |
443 | const struct nft_expr *expr) | |
444 | { | |
445 | const struct nft_payload *priv = nft_expr_priv(expr); | |
446 | int err; | |
447 | ||
448 | switch (priv->base) { | |
449 | case NFT_PAYLOAD_LL_HEADER: | |
450 | err = nft_payload_offload_ll(ctx, flow, priv); | |
451 | break; | |
452 | case NFT_PAYLOAD_NETWORK_HEADER: | |
453 | err = nft_payload_offload_nh(ctx, flow, priv); | |
454 | break; | |
455 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
456 | err = nft_payload_offload_th(ctx, flow, priv); | |
457 | break; | |
458 | default: | |
459 | err = -EOPNOTSUPP; | |
460 | break; | |
461 | } | |
462 | return err; | |
463 | } | |
464 | ||
ef1f7df9 PM |
465 | static const struct nft_expr_ops nft_payload_ops = { |
466 | .type = &nft_payload_type, | |
96518518 | 467 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)), |
96518518 PM |
468 | .eval = nft_payload_eval, |
469 | .init = nft_payload_init, | |
470 | .dump = nft_payload_dump, | |
c9626a2c | 471 | .offload = nft_payload_offload, |
ef1f7df9 PM |
472 | }; |
473 | ||
c29b72e0 PM |
474 | const struct nft_expr_ops nft_payload_fast_ops = { |
475 | .type = &nft_payload_type, | |
476 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)), | |
477 | .eval = nft_payload_eval, | |
478 | .init = nft_payload_init, | |
479 | .dump = nft_payload_dump, | |
c9626a2c | 480 | .offload = nft_payload_offload, |
c29b72e0 PM |
481 | }; |
482 | ||
18140969 PNA |
483 | static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum) |
484 | { | |
485 | *sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), fsum), tsum)); | |
486 | if (*sum == 0) | |
487 | *sum = CSUM_MANGLED_0; | |
488 | } | |
489 | ||
490 | static bool nft_payload_udp_checksum(struct sk_buff *skb, unsigned int thoff) | |
491 | { | |
492 | struct udphdr *uh, _uh; | |
493 | ||
494 | uh = skb_header_pointer(skb, thoff, sizeof(_uh), &_uh); | |
495 | if (!uh) | |
496 | return false; | |
497 | ||
5fd02ebe | 498 | return (__force bool)uh->check; |
18140969 PNA |
499 | } |
500 | ||
501 | static int nft_payload_l4csum_offset(const struct nft_pktinfo *pkt, | |
502 | struct sk_buff *skb, | |
503 | unsigned int *l4csum_offset) | |
504 | { | |
20044428 PNA |
505 | if (pkt->fragoff) |
506 | return -1; | |
507 | ||
18140969 PNA |
508 | switch (pkt->tprot) { |
509 | case IPPROTO_TCP: | |
510 | *l4csum_offset = offsetof(struct tcphdr, check); | |
511 | break; | |
512 | case IPPROTO_UDP: | |
2d7b4ace | 513 | if (!nft_payload_udp_checksum(skb, nft_thoff(pkt))) |
18140969 | 514 | return -1; |
954d8297 | 515 | fallthrough; |
18140969 PNA |
516 | case IPPROTO_UDPLITE: |
517 | *l4csum_offset = offsetof(struct udphdr, check); | |
518 | break; | |
519 | case IPPROTO_ICMPV6: | |
520 | *l4csum_offset = offsetof(struct icmp6hdr, icmp6_cksum); | |
521 | break; | |
522 | default: | |
523 | return -1; | |
524 | } | |
525 | ||
2d7b4ace | 526 | *l4csum_offset += nft_thoff(pkt); |
18140969 PNA |
527 | return 0; |
528 | } | |
529 | ||
346e320c DC |
530 | static int nft_payload_csum_sctp(struct sk_buff *skb, int offset) |
531 | { | |
532 | struct sctphdr *sh; | |
533 | ||
534 | if (skb_ensure_writable(skb, offset + sizeof(*sh))) | |
535 | return -1; | |
536 | ||
537 | sh = (struct sctphdr *)(skb->data + offset); | |
538 | sh->checksum = sctp_compute_cksum(skb, offset); | |
539 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
540 | return 0; | |
541 | } | |
542 | ||
18140969 PNA |
543 | static int nft_payload_l4csum_update(const struct nft_pktinfo *pkt, |
544 | struct sk_buff *skb, | |
545 | __wsum fsum, __wsum tsum) | |
546 | { | |
547 | int l4csum_offset; | |
548 | __sum16 sum; | |
549 | ||
550 | /* If we cannot determine layer 4 checksum offset or this packet doesn't | |
551 | * require layer 4 checksum recalculation, skip this packet. | |
552 | */ | |
553 | if (nft_payload_l4csum_offset(pkt, skb, &l4csum_offset) < 0) | |
554 | return 0; | |
555 | ||
556 | if (skb_copy_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0) | |
557 | return -1; | |
558 | ||
559 | /* Checksum mangling for an arbitrary amount of bytes, based on | |
560 | * inet_proto_csum_replace*() functions. | |
561 | */ | |
562 | if (skb->ip_summed != CHECKSUM_PARTIAL) { | |
563 | nft_csum_replace(&sum, fsum, tsum); | |
564 | if (skb->ip_summed == CHECKSUM_COMPLETE) { | |
565 | skb->csum = ~csum_add(csum_sub(~(skb->csum), fsum), | |
566 | tsum); | |
567 | } | |
568 | } else { | |
569 | sum = ~csum_fold(csum_add(csum_sub(csum_unfold(sum), fsum), | |
570 | tsum)); | |
571 | } | |
572 | ||
7418ee4c | 573 | if (skb_ensure_writable(skb, l4csum_offset + sizeof(sum)) || |
18140969 PNA |
574 | skb_store_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0) |
575 | return -1; | |
576 | ||
577 | return 0; | |
578 | } | |
579 | ||
053d20f5 PNA |
580 | static int nft_payload_csum_inet(struct sk_buff *skb, const u32 *src, |
581 | __wsum fsum, __wsum tsum, int csum_offset) | |
582 | { | |
583 | __sum16 sum; | |
584 | ||
585 | if (skb_copy_bits(skb, csum_offset, &sum, sizeof(sum)) < 0) | |
586 | return -1; | |
587 | ||
588 | nft_csum_replace(&sum, fsum, tsum); | |
7418ee4c | 589 | if (skb_ensure_writable(skb, csum_offset + sizeof(sum)) || |
053d20f5 PNA |
590 | skb_store_bits(skb, csum_offset, &sum, sizeof(sum)) < 0) |
591 | return -1; | |
592 | ||
593 | return 0; | |
594 | } | |
595 | ||
7ec3f7b4 PM |
596 | static void nft_payload_set_eval(const struct nft_expr *expr, |
597 | struct nft_regs *regs, | |
598 | const struct nft_pktinfo *pkt) | |
599 | { | |
600 | const struct nft_payload_set *priv = nft_expr_priv(expr); | |
601 | struct sk_buff *skb = pkt->skb; | |
602 | const u32 *src = ®s->data[priv->sreg]; | |
603 | int offset, csum_offset; | |
604 | __wsum fsum, tsum; | |
7ec3f7b4 PM |
605 | |
606 | switch (priv->base) { | |
607 | case NFT_PAYLOAD_LL_HEADER: | |
608 | if (!skb_mac_header_was_set(skb)) | |
609 | goto err; | |
610 | offset = skb_mac_header(skb) - skb->data; | |
611 | break; | |
612 | case NFT_PAYLOAD_NETWORK_HEADER: | |
613 | offset = skb_network_offset(skb); | |
614 | break; | |
615 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
a20877b5 LZ |
616 | if (!pkt->tprot_set) |
617 | goto err; | |
2d7b4ace | 618 | offset = nft_thoff(pkt); |
7ec3f7b4 PM |
619 | break; |
620 | default: | |
621 | BUG(); | |
622 | } | |
623 | ||
624 | csum_offset = offset + priv->csum_offset; | |
625 | offset += priv->offset; | |
626 | ||
053d20f5 | 627 | if ((priv->csum_type == NFT_PAYLOAD_CSUM_INET || priv->csum_flags) && |
7ec3f7b4 PM |
628 | (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER || |
629 | skb->ip_summed != CHECKSUM_PARTIAL)) { | |
7ec3f7b4 PM |
630 | fsum = skb_checksum(skb, offset, priv->len, 0); |
631 | tsum = csum_partial(src, priv->len, 0); | |
7ec3f7b4 | 632 | |
053d20f5 PNA |
633 | if (priv->csum_type == NFT_PAYLOAD_CSUM_INET && |
634 | nft_payload_csum_inet(skb, src, fsum, tsum, csum_offset)) | |
7ec3f7b4 | 635 | goto err; |
18140969 PNA |
636 | |
637 | if (priv->csum_flags && | |
638 | nft_payload_l4csum_update(pkt, skb, fsum, tsum) < 0) | |
639 | goto err; | |
7ec3f7b4 PM |
640 | } |
641 | ||
7418ee4c | 642 | if (skb_ensure_writable(skb, max(offset + priv->len, 0)) || |
7ec3f7b4 PM |
643 | skb_store_bits(skb, offset, src, priv->len) < 0) |
644 | goto err; | |
645 | ||
346e320c DC |
646 | if (priv->csum_type == NFT_PAYLOAD_CSUM_SCTP && |
647 | pkt->tprot == IPPROTO_SCTP && | |
648 | skb->ip_summed != CHECKSUM_PARTIAL) { | |
2d7b4ace | 649 | if (nft_payload_csum_sctp(skb, nft_thoff(pkt))) |
346e320c DC |
650 | goto err; |
651 | } | |
652 | ||
7ec3f7b4 PM |
653 | return; |
654 | err: | |
655 | regs->verdict.code = NFT_BREAK; | |
656 | } | |
657 | ||
658 | static int nft_payload_set_init(const struct nft_ctx *ctx, | |
659 | const struct nft_expr *expr, | |
660 | const struct nlattr * const tb[]) | |
661 | { | |
662 | struct nft_payload_set *priv = nft_expr_priv(expr); | |
663 | ||
664 | priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); | |
665 | priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); | |
666 | priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); | |
7ec3f7b4 PM |
667 | |
668 | if (tb[NFTA_PAYLOAD_CSUM_TYPE]) | |
669 | priv->csum_type = | |
670 | ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_TYPE])); | |
671 | if (tb[NFTA_PAYLOAD_CSUM_OFFSET]) | |
672 | priv->csum_offset = | |
673 | ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_OFFSET])); | |
18140969 PNA |
674 | if (tb[NFTA_PAYLOAD_CSUM_FLAGS]) { |
675 | u32 flags; | |
676 | ||
677 | flags = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_FLAGS])); | |
678 | if (flags & ~NFT_PAYLOAD_L4CSUM_PSEUDOHDR) | |
679 | return -EINVAL; | |
680 | ||
681 | priv->csum_flags = flags; | |
682 | } | |
7ec3f7b4 PM |
683 | |
684 | switch (priv->csum_type) { | |
685 | case NFT_PAYLOAD_CSUM_NONE: | |
686 | case NFT_PAYLOAD_CSUM_INET: | |
687 | break; | |
346e320c DC |
688 | case NFT_PAYLOAD_CSUM_SCTP: |
689 | if (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER) | |
690 | return -EINVAL; | |
691 | ||
692 | if (priv->csum_offset != offsetof(struct sctphdr, checksum)) | |
693 | return -EINVAL; | |
694 | break; | |
7ec3f7b4 PM |
695 | default: |
696 | return -EOPNOTSUPP; | |
697 | } | |
698 | ||
4f16d25c PNA |
699 | return nft_parse_register_load(tb[NFTA_PAYLOAD_SREG], &priv->sreg, |
700 | priv->len); | |
7ec3f7b4 PM |
701 | } |
702 | ||
703 | static int nft_payload_set_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
704 | { | |
705 | const struct nft_payload_set *priv = nft_expr_priv(expr); | |
706 | ||
707 | if (nft_dump_register(skb, NFTA_PAYLOAD_SREG, priv->sreg) || | |
708 | nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) || | |
709 | nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) || | |
710 | nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)) || | |
711 | nla_put_be32(skb, NFTA_PAYLOAD_CSUM_TYPE, htonl(priv->csum_type)) || | |
712 | nla_put_be32(skb, NFTA_PAYLOAD_CSUM_OFFSET, | |
18140969 PNA |
713 | htonl(priv->csum_offset)) || |
714 | nla_put_be32(skb, NFTA_PAYLOAD_CSUM_FLAGS, htonl(priv->csum_flags))) | |
7ec3f7b4 PM |
715 | goto nla_put_failure; |
716 | return 0; | |
717 | ||
718 | nla_put_failure: | |
719 | return -1; | |
720 | } | |
721 | ||
722 | static const struct nft_expr_ops nft_payload_set_ops = { | |
723 | .type = &nft_payload_type, | |
724 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload_set)), | |
725 | .eval = nft_payload_set_eval, | |
726 | .init = nft_payload_set_init, | |
727 | .dump = nft_payload_set_dump, | |
728 | }; | |
729 | ||
0ca743a5 PNA |
730 | static const struct nft_expr_ops * |
731 | nft_payload_select_ops(const struct nft_ctx *ctx, | |
732 | const struct nlattr * const tb[]) | |
c29b72e0 PM |
733 | { |
734 | enum nft_payload_bases base; | |
735 | unsigned int offset, len; | |
736 | ||
7ec3f7b4 | 737 | if (tb[NFTA_PAYLOAD_BASE] == NULL || |
c29b72e0 PM |
738 | tb[NFTA_PAYLOAD_OFFSET] == NULL || |
739 | tb[NFTA_PAYLOAD_LEN] == NULL) | |
740 | return ERR_PTR(-EINVAL); | |
741 | ||
742 | base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); | |
743 | switch (base) { | |
744 | case NFT_PAYLOAD_LL_HEADER: | |
745 | case NFT_PAYLOAD_NETWORK_HEADER: | |
746 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
747 | break; | |
748 | default: | |
749 | return ERR_PTR(-EOPNOTSUPP); | |
750 | } | |
751 | ||
7ec3f7b4 PM |
752 | if (tb[NFTA_PAYLOAD_SREG] != NULL) { |
753 | if (tb[NFTA_PAYLOAD_DREG] != NULL) | |
754 | return ERR_PTR(-EINVAL); | |
755 | return &nft_payload_set_ops; | |
756 | } | |
757 | ||
758 | if (tb[NFTA_PAYLOAD_DREG] == NULL) | |
759 | return ERR_PTR(-EINVAL); | |
760 | ||
c29b72e0 | 761 | offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); |
45d9bcda | 762 | len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); |
c29b72e0 | 763 | |
f627ed91 NA |
764 | if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) && |
765 | base != NFT_PAYLOAD_LL_HEADER) | |
c29b72e0 PM |
766 | return &nft_payload_fast_ops; |
767 | else | |
768 | return &nft_payload_ops; | |
769 | } | |
770 | ||
4e24877e | 771 | struct nft_expr_type nft_payload_type __read_mostly = { |
ef1f7df9 | 772 | .name = "payload", |
c29b72e0 | 773 | .select_ops = nft_payload_select_ops, |
96518518 PM |
774 | .policy = nft_payload_policy, |
775 | .maxattr = NFTA_PAYLOAD_MAX, | |
ef1f7df9 | 776 | .owner = THIS_MODULE, |
96518518 | 777 | }; |