Commit | Line | Data |
---|---|---|
7785bba2 SK |
1 | /* |
2 | * IPV6 GSO/GRO offload support | |
3 | * Linux INET implementation | |
4 | * | |
5 | * Copyright (C) 2016 secunet Security Networks AG | |
6 | * Author: Steffen Klassert <steffen.klassert@secunet.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms and conditions of the GNU General Public License, | |
10 | * version 2, as published by the Free Software Foundation. | |
11 | * | |
12 | * ESP GRO support | |
13 | */ | |
14 | ||
15 | #include <linux/skbuff.h> | |
16 | #include <linux/init.h> | |
17 | #include <net/protocol.h> | |
18 | #include <crypto/aead.h> | |
19 | #include <crypto/authenc.h> | |
20 | #include <linux/err.h> | |
21 | #include <linux/module.h> | |
22 | #include <net/ip.h> | |
23 | #include <net/xfrm.h> | |
24 | #include <net/esp.h> | |
25 | #include <linux/scatterlist.h> | |
26 | #include <linux/kernel.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/spinlock.h> | |
29 | #include <net/ip6_route.h> | |
30 | #include <net/ipv6.h> | |
31 | #include <linux/icmpv6.h> | |
32 | ||
ca3a1b85 YK |
33 | static __u16 esp6_nexthdr_esp_offset(struct ipv6hdr *ipv6_hdr, int nhlen) |
34 | { | |
35 | int off = sizeof(struct ipv6hdr); | |
36 | struct ipv6_opt_hdr *exthdr; | |
37 | ||
38 | if (likely(ipv6_hdr->nexthdr == NEXTHDR_ESP)) | |
39 | return offsetof(struct ipv6hdr, nexthdr); | |
40 | ||
41 | while (off < nhlen) { | |
42 | exthdr = (void *)ipv6_hdr + off; | |
43 | if (exthdr->nexthdr == NEXTHDR_ESP) | |
44 | return off; | |
45 | ||
46 | off += ipv6_optlen(exthdr); | |
47 | } | |
48 | ||
49 | return 0; | |
50 | } | |
51 | ||
d4546c25 DM |
52 | static struct sk_buff *esp6_gro_receive(struct list_head *head, |
53 | struct sk_buff *skb) | |
7785bba2 SK |
54 | { |
55 | int offset = skb_gro_offset(skb); | |
56 | struct xfrm_offload *xo; | |
57 | struct xfrm_state *x; | |
58 | __be32 seq; | |
59 | __be32 spi; | |
ca3a1b85 | 60 | int nhoff; |
7785bba2 SK |
61 | int err; |
62 | ||
374d1b5a SK |
63 | if (!pskb_pull(skb, offset)) |
64 | return NULL; | |
7785bba2 SK |
65 | |
66 | if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0) | |
67 | goto out; | |
68 | ||
bcd1f8a4 SK |
69 | xo = xfrm_offload(skb); |
70 | if (!xo || !(xo->flags & CRYPTO_DONE)) { | |
0ca64da1 FW |
71 | struct sec_path *sp = secpath_set(skb); |
72 | ||
73 | if (!sp) | |
bcd1f8a4 | 74 | goto out; |
7785bba2 | 75 | |
0ca64da1 | 76 | if (sp->len == XFRM_MAX_DEPTH) |
bcd1f8a4 | 77 | goto out; |
7785bba2 | 78 | |
bcd1f8a4 SK |
79 | x = xfrm_state_lookup(dev_net(skb->dev), skb->mark, |
80 | (xfrm_address_t *)&ipv6_hdr(skb)->daddr, | |
81 | spi, IPPROTO_ESP, AF_INET6); | |
82 | if (!x) | |
83 | goto out; | |
7785bba2 | 84 | |
0ca64da1 FW |
85 | sp->xvec[sp->len++] = x; |
86 | sp->olen++; | |
7785bba2 | 87 | |
bcd1f8a4 SK |
88 | xo = xfrm_offload(skb); |
89 | if (!xo) { | |
90 | xfrm_state_put(x); | |
91 | goto out; | |
92 | } | |
7785bba2 | 93 | } |
bcd1f8a4 | 94 | |
7785bba2 SK |
95 | xo->flags |= XFRM_GRO; |
96 | ||
ca3a1b85 YK |
97 | nhoff = esp6_nexthdr_esp_offset(ipv6_hdr(skb), offset); |
98 | if (!nhoff) | |
99 | goto out; | |
100 | ||
101 | IP6CB(skb)->nhoff = nhoff; | |
7785bba2 SK |
102 | XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL; |
103 | XFRM_SPI_SKB_CB(skb)->family = AF_INET6; | |
104 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr); | |
105 | XFRM_SPI_SKB_CB(skb)->seq = seq; | |
106 | ||
107 | /* We don't need to handle errors from xfrm_input, it does all | |
108 | * the error handling and frees the resources on error. */ | |
109 | xfrm_input(skb, IPPROTO_ESP, spi, -2); | |
110 | ||
111 | return ERR_PTR(-EINPROGRESS); | |
112 | out: | |
113 | skb_push(skb, offset); | |
114 | NAPI_GRO_CB(skb)->same_flow = 0; | |
115 | NAPI_GRO_CB(skb)->flush = 1; | |
116 | ||
117 | return NULL; | |
118 | } | |
119 | ||
7862b405 SK |
120 | static void esp6_gso_encap(struct xfrm_state *x, struct sk_buff *skb) |
121 | { | |
122 | struct ip_esp_hdr *esph; | |
123 | struct ipv6hdr *iph = ipv6_hdr(skb); | |
124 | struct xfrm_offload *xo = xfrm_offload(skb); | |
125 | int proto = iph->nexthdr; | |
126 | ||
127 | skb_push(skb, -skb_network_offset(skb)); | |
128 | esph = ip_esp_hdr(skb); | |
129 | *skb_mac_header(skb) = IPPROTO_ESP; | |
130 | ||
131 | esph->spi = x->id.spi; | |
132 | esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); | |
133 | ||
134 | xo->proto = proto; | |
135 | } | |
136 | ||
137 | static struct sk_buff *esp6_gso_segment(struct sk_buff *skb, | |
138 | netdev_features_t features) | |
139 | { | |
7862b405 SK |
140 | struct xfrm_state *x; |
141 | struct ip_esp_hdr *esph; | |
142 | struct crypto_aead *aead; | |
7862b405 SK |
143 | netdev_features_t esp_features = features; |
144 | struct xfrm_offload *xo = xfrm_offload(skb); | |
145 | ||
ffa6f571 | 146 | if (!xo) |
3dca3f38 | 147 | return ERR_PTR(-EINVAL); |
7862b405 | 148 | |
121d57af | 149 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP)) |
5ca11440 | 150 | return ERR_PTR(-EINVAL); |
7862b405 SK |
151 | |
152 | x = skb->sp->xvec[skb->sp->len - 1]; | |
153 | aead = x->data; | |
154 | esph = ip_esp_hdr(skb); | |
155 | ||
156 | if (esph->spi != x->id.spi) | |
3dca3f38 | 157 | return ERR_PTR(-EINVAL); |
7862b405 SK |
158 | |
159 | if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead))) | |
3dca3f38 | 160 | return ERR_PTR(-EINVAL); |
7862b405 SK |
161 | |
162 | __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)); | |
163 | ||
164 | skb->encap_hdr_csum = 1; | |
165 | ||
fcb662de | 166 | if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) |
7862b405 | 167 | esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK); |
5211fcfb SN |
168 | else if (!(features & NETIF_F_HW_ESP_TX_CSUM)) |
169 | esp_features = features & ~NETIF_F_CSUM_MASK; | |
7862b405 | 170 | |
3dca3f38 | 171 | xo->flags |= XFRM_GSO_SEGMENT; |
7862b405 | 172 | |
3dca3f38 | 173 | return x->outer_mode->gso_segment(x, skb, esp_features); |
7862b405 SK |
174 | } |
175 | ||
383d0350 SK |
176 | static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb) |
177 | { | |
178 | struct crypto_aead *aead = x->data; | |
e51a6472 | 179 | struct xfrm_offload *xo = xfrm_offload(skb); |
383d0350 SK |
180 | |
181 | if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead))) | |
182 | return -EINVAL; | |
183 | ||
e51a6472 IT |
184 | if (!(xo->flags & CRYPTO_DONE)) |
185 | skb->ip_summed = CHECKSUM_NONE; | |
383d0350 SK |
186 | |
187 | return esp6_input_done2(skb, 0); | |
188 | } | |
189 | ||
190 | static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features) | |
191 | { | |
3dca3f38 | 192 | int len; |
383d0350 SK |
193 | int err; |
194 | int alen; | |
195 | int blksize; | |
196 | struct xfrm_offload *xo; | |
197 | struct ip_esp_hdr *esph; | |
198 | struct crypto_aead *aead; | |
199 | struct esp_info esp; | |
200 | bool hw_offload = true; | |
3dca3f38 | 201 | __u32 seq; |
383d0350 SK |
202 | |
203 | esp.inplace = true; | |
204 | ||
205 | xo = xfrm_offload(skb); | |
206 | ||
207 | if (!xo) | |
208 | return -EINVAL; | |
209 | ||
fcb662de | 210 | if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) { |
383d0350 | 211 | xo->flags |= CRYPTO_FALLBACK; |
8f92e03e | 212 | hw_offload = false; |
383d0350 SK |
213 | } |
214 | ||
215 | esp.proto = xo->proto; | |
216 | ||
217 | /* skb is pure payload to encrypt */ | |
218 | ||
219 | aead = x->data; | |
220 | alen = crypto_aead_authsize(aead); | |
221 | ||
222 | esp.tfclen = 0; | |
223 | /* XXX: Add support for tfc padding here. */ | |
224 | ||
225 | blksize = ALIGN(crypto_aead_blocksize(aead), 4); | |
226 | esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize); | |
227 | esp.plen = esp.clen - skb->len - esp.tfclen; | |
228 | esp.tailen = esp.tfclen + esp.plen + alen; | |
229 | ||
230 | if (!hw_offload || (hw_offload && !skb_is_gso(skb))) { | |
231 | esp.nfrags = esp6_output_head(x, skb, &esp); | |
232 | if (esp.nfrags < 0) | |
233 | return esp.nfrags; | |
234 | } | |
235 | ||
3dca3f38 SK |
236 | seq = xo->seq.low; |
237 | ||
383d0350 SK |
238 | esph = ip_esp_hdr(skb); |
239 | esph->spi = x->id.spi; | |
240 | ||
241 | skb_push(skb, -skb_network_offset(skb)); | |
242 | ||
243 | if (xo->flags & XFRM_GSO_SEGMENT) { | |
3dca3f38 | 244 | esph->seq_no = htonl(seq); |
383d0350 | 245 | |
3dca3f38 SK |
246 | if (!skb_is_gso(skb)) |
247 | xo->seq.low++; | |
248 | else | |
249 | xo->seq.low += skb_shinfo(skb)->gso_segs; | |
383d0350 SK |
250 | } |
251 | ||
3dca3f38 SK |
252 | esp.seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32)); |
253 | ||
254 | len = skb->len - sizeof(struct ipv6hdr); | |
255 | if (len > IPV6_MAXPLEN) | |
256 | len = 0; | |
257 | ||
258 | ipv6_hdr(skb)->payload_len = htons(len); | |
259 | ||
8f92e03e | 260 | if (hw_offload) |
383d0350 SK |
261 | return 0; |
262 | ||
383d0350 | 263 | err = esp6_output_tail(x, skb, &esp); |
4ff0308f | 264 | if (err) |
383d0350 SK |
265 | return err; |
266 | ||
267 | secpath_reset(skb); | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
7785bba2 SK |
272 | static const struct net_offload esp6_offload = { |
273 | .callbacks = { | |
274 | .gro_receive = esp6_gro_receive, | |
7862b405 | 275 | .gso_segment = esp6_gso_segment, |
7785bba2 SK |
276 | }, |
277 | }; | |
278 | ||
383d0350 SK |
279 | static const struct xfrm_type_offload esp6_type_offload = { |
280 | .description = "ESP6 OFFLOAD", | |
281 | .owner = THIS_MODULE, | |
282 | .proto = IPPROTO_ESP, | |
283 | .input_tail = esp6_input_tail, | |
284 | .xmit = esp6_xmit, | |
7862b405 | 285 | .encap = esp6_gso_encap, |
383d0350 SK |
286 | }; |
287 | ||
7785bba2 SK |
288 | static int __init esp6_offload_init(void) |
289 | { | |
383d0350 SK |
290 | if (xfrm_register_type_offload(&esp6_type_offload, AF_INET6) < 0) { |
291 | pr_info("%s: can't add xfrm type offload\n", __func__); | |
292 | return -EAGAIN; | |
293 | } | |
294 | ||
7785bba2 SK |
295 | return inet6_add_offload(&esp6_offload, IPPROTO_ESP); |
296 | } | |
297 | ||
298 | static void __exit esp6_offload_exit(void) | |
299 | { | |
383d0350 SK |
300 | if (xfrm_unregister_type_offload(&esp6_type_offload, AF_INET6) < 0) |
301 | pr_info("%s: can't remove xfrm type offload\n", __func__); | |
302 | ||
7785bba2 SK |
303 | inet6_del_offload(&esp6_offload, IPPROTO_ESP); |
304 | } | |
305 | ||
306 | module_init(esp6_offload_init); | |
307 | module_exit(esp6_offload_exit); | |
308 | MODULE_LICENSE("GPL"); | |
309 | MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); | |
ffdb5211 | 310 | MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET6, XFRM_PROTO_ESP); |