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 | ||
33 | static struct sk_buff **esp6_gro_receive(struct sk_buff **head, | |
34 | struct sk_buff *skb) | |
35 | { | |
36 | int offset = skb_gro_offset(skb); | |
37 | struct xfrm_offload *xo; | |
38 | struct xfrm_state *x; | |
39 | __be32 seq; | |
40 | __be32 spi; | |
41 | int err; | |
42 | ||
43 | skb_pull(skb, offset); | |
44 | ||
45 | if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0) | |
46 | goto out; | |
47 | ||
48 | err = secpath_set(skb); | |
49 | if (err) | |
50 | goto out; | |
51 | ||
52 | if (skb->sp->len == XFRM_MAX_DEPTH) | |
53 | goto out; | |
54 | ||
55 | x = xfrm_state_lookup(dev_net(skb->dev), skb->mark, | |
56 | (xfrm_address_t *)&ipv6_hdr(skb)->daddr, | |
57 | spi, IPPROTO_ESP, AF_INET6); | |
58 | if (!x) | |
59 | goto out; | |
60 | ||
61 | skb->sp->xvec[skb->sp->len++] = x; | |
62 | skb->sp->olen++; | |
63 | ||
64 | xo = xfrm_offload(skb); | |
65 | if (!xo) { | |
66 | xfrm_state_put(x); | |
67 | goto out; | |
68 | } | |
69 | xo->flags |= XFRM_GRO; | |
70 | ||
71 | XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL; | |
72 | XFRM_SPI_SKB_CB(skb)->family = AF_INET6; | |
73 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr); | |
74 | XFRM_SPI_SKB_CB(skb)->seq = seq; | |
75 | ||
76 | /* We don't need to handle errors from xfrm_input, it does all | |
77 | * the error handling and frees the resources on error. */ | |
78 | xfrm_input(skb, IPPROTO_ESP, spi, -2); | |
79 | ||
80 | return ERR_PTR(-EINPROGRESS); | |
81 | out: | |
82 | skb_push(skb, offset); | |
83 | NAPI_GRO_CB(skb)->same_flow = 0; | |
84 | NAPI_GRO_CB(skb)->flush = 1; | |
85 | ||
86 | return NULL; | |
87 | } | |
88 | ||
7862b405 SK |
89 | static void esp6_gso_encap(struct xfrm_state *x, struct sk_buff *skb) |
90 | { | |
91 | struct ip_esp_hdr *esph; | |
92 | struct ipv6hdr *iph = ipv6_hdr(skb); | |
93 | struct xfrm_offload *xo = xfrm_offload(skb); | |
94 | int proto = iph->nexthdr; | |
95 | ||
96 | skb_push(skb, -skb_network_offset(skb)); | |
97 | esph = ip_esp_hdr(skb); | |
98 | *skb_mac_header(skb) = IPPROTO_ESP; | |
99 | ||
100 | esph->spi = x->id.spi; | |
101 | esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); | |
102 | ||
103 | xo->proto = proto; | |
104 | } | |
105 | ||
106 | static struct sk_buff *esp6_gso_segment(struct sk_buff *skb, | |
107 | netdev_features_t features) | |
108 | { | |
109 | __u32 seq; | |
110 | int err = 0; | |
111 | struct sk_buff *skb2; | |
112 | struct xfrm_state *x; | |
113 | struct ip_esp_hdr *esph; | |
114 | struct crypto_aead *aead; | |
115 | struct sk_buff *segs = ERR_PTR(-EINVAL); | |
116 | netdev_features_t esp_features = features; | |
117 | struct xfrm_offload *xo = xfrm_offload(skb); | |
118 | ||
119 | if (xo) | |
120 | goto out; | |
121 | ||
122 | seq = xo->seq.low; | |
123 | ||
124 | x = skb->sp->xvec[skb->sp->len - 1]; | |
125 | aead = x->data; | |
126 | esph = ip_esp_hdr(skb); | |
127 | ||
128 | if (esph->spi != x->id.spi) | |
129 | goto out; | |
130 | ||
131 | if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead))) | |
132 | goto out; | |
133 | ||
134 | __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)); | |
135 | ||
136 | skb->encap_hdr_csum = 1; | |
137 | ||
138 | if (!(features & NETIF_F_HW_ESP)) | |
139 | esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK); | |
140 | ||
141 | segs = x->outer_mode->gso_segment(x, skb, esp_features); | |
142 | if (IS_ERR_OR_NULL(segs)) | |
143 | goto out; | |
144 | ||
145 | __skb_pull(skb, skb->data - skb_mac_header(skb)); | |
146 | ||
147 | skb2 = segs; | |
148 | do { | |
149 | struct sk_buff *nskb = skb2->next; | |
150 | ||
151 | xo = xfrm_offload(skb2); | |
152 | xo->flags |= XFRM_GSO_SEGMENT; | |
153 | xo->seq.low = seq; | |
154 | xo->seq.hi = xfrm_replay_seqhi(x, seq); | |
155 | ||
156 | if(!(features & NETIF_F_HW_ESP)) | |
157 | xo->flags |= CRYPTO_FALLBACK; | |
158 | ||
159 | x->outer_mode->xmit(x, skb2); | |
160 | ||
161 | err = x->type_offload->xmit(x, skb2, esp_features); | |
162 | if (err) { | |
163 | kfree_skb_list(segs); | |
164 | return ERR_PTR(err); | |
165 | } | |
166 | ||
167 | if (!skb_is_gso(skb2)) | |
168 | seq++; | |
169 | else | |
170 | seq += skb_shinfo(skb2)->gso_segs; | |
171 | ||
172 | skb_push(skb2, skb2->mac_len); | |
173 | skb2 = nskb; | |
174 | } while (skb2); | |
175 | ||
176 | out: | |
177 | return segs; | |
178 | } | |
179 | ||
383d0350 SK |
180 | static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb) |
181 | { | |
182 | struct crypto_aead *aead = x->data; | |
183 | ||
184 | if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead))) | |
185 | return -EINVAL; | |
186 | ||
187 | skb->ip_summed = CHECKSUM_NONE; | |
188 | ||
189 | return esp6_input_done2(skb, 0); | |
190 | } | |
191 | ||
192 | static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features) | |
193 | { | |
194 | int err; | |
195 | int alen; | |
196 | int blksize; | |
197 | struct xfrm_offload *xo; | |
198 | struct ip_esp_hdr *esph; | |
199 | struct crypto_aead *aead; | |
200 | struct esp_info esp; | |
201 | bool hw_offload = true; | |
202 | ||
203 | esp.inplace = true; | |
204 | ||
205 | xo = xfrm_offload(skb); | |
206 | ||
207 | if (!xo) | |
208 | return -EINVAL; | |
209 | ||
210 | if (!(features & NETIF_F_HW_ESP) || | |
211 | (x->xso.offload_handle && x->xso.dev != skb->dev)) { | |
212 | xo->flags |= CRYPTO_FALLBACK; | |
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 | ||
236 | esph = ip_esp_hdr(skb); | |
237 | esph->spi = x->id.spi; | |
238 | ||
239 | skb_push(skb, -skb_network_offset(skb)); | |
240 | ||
241 | if (xo->flags & XFRM_GSO_SEGMENT) { | |
242 | esph->seq_no = htonl(xo->seq.low); | |
243 | } else { | |
244 | int len; | |
245 | ||
246 | len = skb->len - sizeof(struct ipv6hdr); | |
247 | if (len > IPV6_MAXPLEN) | |
248 | len = 0; | |
249 | ||
250 | ipv6_hdr(skb)->payload_len = htons(len); | |
251 | } | |
252 | ||
253 | if (x->xso.offload_handle && !(xo->flags & CRYPTO_FALLBACK)) | |
254 | return 0; | |
255 | ||
256 | esp.seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32)); | |
257 | ||
258 | err = esp6_output_tail(x, skb, &esp); | |
259 | if (err < 0) | |
260 | return err; | |
261 | ||
262 | secpath_reset(skb); | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
7785bba2 SK |
267 | static const struct net_offload esp6_offload = { |
268 | .callbacks = { | |
269 | .gro_receive = esp6_gro_receive, | |
7862b405 | 270 | .gso_segment = esp6_gso_segment, |
7785bba2 SK |
271 | }, |
272 | }; | |
273 | ||
383d0350 SK |
274 | static const struct xfrm_type_offload esp6_type_offload = { |
275 | .description = "ESP6 OFFLOAD", | |
276 | .owner = THIS_MODULE, | |
277 | .proto = IPPROTO_ESP, | |
278 | .input_tail = esp6_input_tail, | |
279 | .xmit = esp6_xmit, | |
7862b405 | 280 | .encap = esp6_gso_encap, |
383d0350 SK |
281 | }; |
282 | ||
7785bba2 SK |
283 | static int __init esp6_offload_init(void) |
284 | { | |
383d0350 SK |
285 | if (xfrm_register_type_offload(&esp6_type_offload, AF_INET6) < 0) { |
286 | pr_info("%s: can't add xfrm type offload\n", __func__); | |
287 | return -EAGAIN; | |
288 | } | |
289 | ||
7785bba2 SK |
290 | return inet6_add_offload(&esp6_offload, IPPROTO_ESP); |
291 | } | |
292 | ||
293 | static void __exit esp6_offload_exit(void) | |
294 | { | |
383d0350 SK |
295 | if (xfrm_unregister_type_offload(&esp6_type_offload, AF_INET6) < 0) |
296 | pr_info("%s: can't remove xfrm type offload\n", __func__); | |
297 | ||
7785bba2 SK |
298 | inet6_del_offload(&esp6_offload, IPPROTO_ESP); |
299 | } | |
300 | ||
301 | module_init(esp6_offload_init); | |
302 | module_exit(esp6_offload_exit); | |
303 | MODULE_LICENSE("GPL"); | |
304 | MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); |