blob: 2fd3d990d992fe26ddfa3ea870e0940ce58dec0d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xu406ef772007-10-08 17:16:30 -07002/*
3 * xfrm_output.c - Common IPsec encapsulation code.
4 *
5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xu406ef772007-10-08 17:16:30 -07006 */
7
8#include <linux/errno.h>
9#include <linux/module.h>
10#include <linux/netdevice.h>
Herbert Xu862b82c2007-11-13 21:43:11 -080011#include <linux/netfilter.h>
Herbert Xu406ef772007-10-08 17:16:30 -070012#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Herbert Xu406ef772007-10-08 17:16:30 -070014#include <linux/spinlock.h>
Herbert Xu406ef772007-10-08 17:16:30 -070015#include <net/dst.h>
Florian Westphal1de70832019-03-29 21:16:29 +010016#include <net/inet_ecn.h>
Herbert Xu406ef772007-10-08 17:16:30 -070017#include <net/xfrm.h>
18
Florian Westphal1de70832019-03-29 21:16:29 +010019#include "xfrm_inout.h"
20
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050021static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb);
Florian Westphal0c620e92019-03-29 21:16:25 +010022static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
Herbert Xuc6581a42007-11-13 21:43:43 -080023
Steffen Klassert26b20722012-03-21 23:32:39 +000024static int xfrm_skb_check_space(struct sk_buff *skb)
Herbert Xu83815de2007-10-08 17:25:08 -070025{
Eric Dumazetadf30902009-06-02 05:19:30 +000026 struct dst_entry *dst = skb_dst(skb);
Herbert Xu550ade82007-11-13 21:33:01 -080027 int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
Herbert Xu83815de2007-10-08 17:25:08 -070028 - skb_headroom(skb);
Johannes Bergf5184d22008-05-12 20:48:31 -070029 int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
Herbert Xu83815de2007-10-08 17:25:08 -070030
Herbert Xud01dbeb2008-09-30 02:03:19 -070031 if (nhead <= 0) {
32 if (ntail <= 0)
33 return 0;
34 nhead = 0;
35 } else if (ntail < 0)
36 ntail = 0;
Herbert Xu83815de2007-10-08 17:25:08 -070037
Herbert Xud01dbeb2008-09-30 02:03:19 -070038 return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
Herbert Xu83815de2007-10-08 17:25:08 -070039}
40
Ying Xue9449c3c2015-05-12 18:29:44 +080041/* Children define the path of the packet through the
42 * Linux networking. Thus, destinations are stackable.
43 */
44
45static struct dst_entry *skb_dst_pop(struct sk_buff *skb)
46{
David Millerb92cf4a2017-11-28 15:40:22 -050047 struct dst_entry *child = dst_clone(xfrm_dst_child(skb_dst(skb)));
Ying Xue9449c3c2015-05-12 18:29:44 +080048
49 skb_dst_drop(skb);
50 return child;
51}
52
Florian Westphal0c620e92019-03-29 21:16:25 +010053/* Add encapsulation header.
54 *
55 * The IP header will be moved forward to make space for the encapsulation
56 * header.
57 */
58static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
59{
Florian Westphal0c620e92019-03-29 21:16:25 +010060 struct iphdr *iph = ip_hdr(skb);
61 int ihl = iph->ihl * 4;
62
63 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
64
65 skb_set_network_header(skb, -x->props.header_len);
66 skb->mac_header = skb->network_header +
67 offsetof(struct iphdr, protocol);
68 skb->transport_header = skb->network_header + ihl;
69 __skb_pull(skb, ihl);
70 memmove(skb_network_header(skb), iph, ihl);
71 return 0;
Florian Westphal0c620e92019-03-29 21:16:25 +010072}
73
74/* Add encapsulation header.
75 *
76 * The IP header and mutable extension headers will be moved forward to make
77 * space for the encapsulation header.
78 */
79static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
80{
Florian Westphal4c145dc2019-03-29 21:16:31 +010081#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal0c620e92019-03-29 21:16:25 +010082 struct ipv6hdr *iph;
83 u8 *prevhdr;
84 int hdr_len;
85
86 iph = ipv6_hdr(skb);
87 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
88
89 hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
90 if (hdr_len < 0)
91 return hdr_len;
92 skb_set_mac_header(skb,
93 (prevhdr - x->props.header_len) - skb->data);
94 skb_set_network_header(skb, -x->props.header_len);
95 skb->transport_header = skb->network_header + hdr_len;
96 __skb_pull(skb, hdr_len);
97 memmove(ipv6_hdr(skb), iph, hdr_len);
98 return 0;
99#else
100 WARN_ON_ONCE(1);
Florian Westphal4c145dc2019-03-29 21:16:31 +0100101 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100102#endif
103}
104
105/* Add route optimization header space.
106 *
107 * The IP header and mutable extension headers will be moved forward to make
108 * space for the route optimization header.
109 */
110static int xfrm6_ro_output(struct xfrm_state *x, struct sk_buff *skb)
111{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100112#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100113 struct ipv6hdr *iph;
114 u8 *prevhdr;
115 int hdr_len;
116
117 iph = ipv6_hdr(skb);
118
119 hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
120 if (hdr_len < 0)
121 return hdr_len;
122 skb_set_mac_header(skb,
123 (prevhdr - x->props.header_len) - skb->data);
124 skb_set_network_header(skb, -x->props.header_len);
125 skb->transport_header = skb->network_header + hdr_len;
126 __skb_pull(skb, hdr_len);
127 memmove(ipv6_hdr(skb), iph, hdr_len);
128
129 x->lastused = ktime_get_real_seconds();
130
131 return 0;
132#else
133 WARN_ON_ONCE(1);
Florian Westphal4c145dc2019-03-29 21:16:31 +0100134 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100135#endif
136}
137
Florian Westphal1de70832019-03-29 21:16:29 +0100138/* Add encapsulation header.
139 *
140 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
141 */
142static int xfrm4_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb)
143{
144 struct ip_beet_phdr *ph;
145 struct iphdr *top_iph;
146 int hdrlen, optlen;
147
148 hdrlen = 0;
149 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
150 if (unlikely(optlen))
151 hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
152
153 skb_set_network_header(skb, -x->props.header_len - hdrlen +
154 (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph)));
155 if (x->sel.family != AF_INET6)
156 skb->network_header += IPV4_BEET_PHMAXLEN;
157 skb->mac_header = skb->network_header +
158 offsetof(struct iphdr, protocol);
159 skb->transport_header = skb->network_header + sizeof(*top_iph);
160
161 xfrm4_beet_make_header(skb);
162
163 ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen);
164
165 top_iph = ip_hdr(skb);
166
167 if (unlikely(optlen)) {
168 if (WARN_ON(optlen < 0))
169 return -EINVAL;
170
171 ph->padlen = 4 - (optlen & 4);
172 ph->hdrlen = optlen / 8;
173 ph->nexthdr = top_iph->protocol;
174 if (ph->padlen)
175 memset(ph + 1, IPOPT_NOP, ph->padlen);
176
177 top_iph->protocol = IPPROTO_BEETPH;
178 top_iph->ihl = sizeof(struct iphdr) / 4;
179 }
180
181 top_iph->saddr = x->props.saddr.a4;
182 top_iph->daddr = x->id.daddr.a4;
183
184 return 0;
185}
186
187/* Add encapsulation header.
188 *
189 * The top IP header will be constructed per RFC 2401.
190 */
191static int xfrm4_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb)
192{
193 struct dst_entry *dst = skb_dst(skb);
194 struct iphdr *top_iph;
195 int flags;
196
197 skb_set_inner_network_header(skb, skb_network_offset(skb));
198 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
199
200 skb_set_network_header(skb, -x->props.header_len);
201 skb->mac_header = skb->network_header +
202 offsetof(struct iphdr, protocol);
203 skb->transport_header = skb->network_header + sizeof(*top_iph);
204 top_iph = ip_hdr(skb);
205
206 top_iph->ihl = 5;
207 top_iph->version = 4;
208
209 top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family);
210
211 /* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */
212 if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
213 top_iph->tos = 0;
214 else
215 top_iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
216 top_iph->tos = INET_ECN_encapsulate(top_iph->tos,
217 XFRM_MODE_SKB_CB(skb)->tos);
218
219 flags = x->props.flags;
220 if (flags & XFRM_STATE_NOECN)
221 IP_ECN_clear(top_iph);
222
223 top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
224 0 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF));
225
226 top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst));
227
228 top_iph->saddr = x->props.saddr.a4;
229 top_iph->daddr = x->id.daddr.a4;
230 ip_select_ident(dev_net(dst->dev), skb, NULL);
231
232 return 0;
233}
234
235#if IS_ENABLED(CONFIG_IPV6)
236static int xfrm6_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb)
237{
238 struct dst_entry *dst = skb_dst(skb);
239 struct ipv6hdr *top_iph;
240 int dsfield;
241
242 skb_set_inner_network_header(skb, skb_network_offset(skb));
243 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
244
245 skb_set_network_header(skb, -x->props.header_len);
246 skb->mac_header = skb->network_header +
247 offsetof(struct ipv6hdr, nexthdr);
248 skb->transport_header = skb->network_header + sizeof(*top_iph);
249 top_iph = ipv6_hdr(skb);
250
251 top_iph->version = 6;
252
253 memcpy(top_iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl,
254 sizeof(top_iph->flow_lbl));
255 top_iph->nexthdr = xfrm_af2proto(skb_dst(skb)->ops->family);
256
257 if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
258 dsfield = 0;
259 else
260 dsfield = XFRM_MODE_SKB_CB(skb)->tos;
261 dsfield = INET_ECN_encapsulate(dsfield, XFRM_MODE_SKB_CB(skb)->tos);
262 if (x->props.flags & XFRM_STATE_NOECN)
263 dsfield &= ~INET_ECN_MASK;
264 ipv6_change_dsfield(top_iph, 0, dsfield);
265 top_iph->hop_limit = ip6_dst_hoplimit(xfrm_dst_child(dst));
266 top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
267 top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
268 return 0;
269}
270
271static int xfrm6_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb)
272{
273 struct ipv6hdr *top_iph;
274 struct ip_beet_phdr *ph;
275 int optlen, hdr_len;
276
277 hdr_len = 0;
278 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
279 if (unlikely(optlen))
280 hdr_len += IPV4_BEET_PHMAXLEN - (optlen & 4);
281
282 skb_set_network_header(skb, -x->props.header_len - hdr_len);
283 if (x->sel.family != AF_INET6)
284 skb->network_header += IPV4_BEET_PHMAXLEN;
285 skb->mac_header = skb->network_header +
286 offsetof(struct ipv6hdr, nexthdr);
287 skb->transport_header = skb->network_header + sizeof(*top_iph);
288 ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdr_len);
289
290 xfrm6_beet_make_header(skb);
291
292 top_iph = ipv6_hdr(skb);
293 if (unlikely(optlen)) {
294 if (WARN_ON(optlen < 0))
295 return -EINVAL;
296
297 ph->padlen = 4 - (optlen & 4);
298 ph->hdrlen = optlen / 8;
299 ph->nexthdr = top_iph->nexthdr;
300 if (ph->padlen)
301 memset(ph + 1, IPOPT_NOP, ph->padlen);
302
303 top_iph->nexthdr = IPPROTO_BEETPH;
304 }
305
306 top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
307 top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
308 return 0;
309}
310#endif
311
312/* Add encapsulation header.
313 *
314 * On exit, the transport header will be set to the start of the
315 * encapsulation header to be filled in by x->type->output and the mac
316 * header will be set to the nextheader (protocol for IPv4) field of the
317 * extension header directly preceding the encapsulation header, or in
318 * its absence, that of the top IP header.
319 * The value of the network header will always point to the top IP header
320 * while skb->data will point to the payload.
321 */
Florian Westphal0c620e92019-03-29 21:16:25 +0100322static int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
323{
324 int err;
325
326 err = xfrm_inner_extract_output(x, skb);
327 if (err)
328 return err;
329
330 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
331 skb->protocol = htons(ETH_P_IP);
332
Florian Westphalc9500d72019-03-29 21:16:32 +0100333 switch (x->outer_mode.encap) {
Florian Westphal1de70832019-03-29 21:16:29 +0100334 case XFRM_MODE_BEET:
335 return xfrm4_beet_encap_add(x, skb);
336 case XFRM_MODE_TUNNEL:
337 return xfrm4_tunnel_encap_add(x, skb);
338 }
339
340 WARN_ON_ONCE(1);
341 return -EOPNOTSUPP;
Florian Westphal0c620e92019-03-29 21:16:25 +0100342}
343
344static int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
345{
346#if IS_ENABLED(CONFIG_IPV6)
347 int err;
348
349 err = xfrm_inner_extract_output(x, skb);
350 if (err)
351 return err;
352
353 skb->ignore_df = 1;
354 skb->protocol = htons(ETH_P_IPV6);
355
Florian Westphalc9500d72019-03-29 21:16:32 +0100356 switch (x->outer_mode.encap) {
Florian Westphal1de70832019-03-29 21:16:29 +0100357 case XFRM_MODE_BEET:
358 return xfrm6_beet_encap_add(x, skb);
359 case XFRM_MODE_TUNNEL:
360 return xfrm6_tunnel_encap_add(x, skb);
361 default:
362 WARN_ON_ONCE(1);
363 return -EOPNOTSUPP;
364 }
Florian Westphal0c620e92019-03-29 21:16:25 +0100365#endif
Florian Westphal1de70832019-03-29 21:16:29 +0100366 WARN_ON_ONCE(1);
367 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100368}
369
370static int xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
371{
Florian Westphalc9500d72019-03-29 21:16:32 +0100372 switch (x->outer_mode.encap) {
Florian Westphal0c620e92019-03-29 21:16:25 +0100373 case XFRM_MODE_BEET:
374 case XFRM_MODE_TUNNEL:
Florian Westphalc9500d72019-03-29 21:16:32 +0100375 if (x->outer_mode.family == AF_INET)
Florian Westphal0c620e92019-03-29 21:16:25 +0100376 return xfrm4_prepare_output(x, skb);
Florian Westphalc9500d72019-03-29 21:16:32 +0100377 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100378 return xfrm6_prepare_output(x, skb);
379 break;
380 case XFRM_MODE_TRANSPORT:
Florian Westphalc9500d72019-03-29 21:16:32 +0100381 if (x->outer_mode.family == AF_INET)
Florian Westphal0c620e92019-03-29 21:16:25 +0100382 return xfrm4_transport_output(x, skb);
Florian Westphalc9500d72019-03-29 21:16:32 +0100383 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100384 return xfrm6_transport_output(x, skb);
385 break;
386 case XFRM_MODE_ROUTEOPTIMIZATION:
Florian Westphalc9500d72019-03-29 21:16:32 +0100387 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100388 return xfrm6_ro_output(x, skb);
389 WARN_ON_ONCE(1);
390 break;
391 default:
392 WARN_ON_ONCE(1);
393 break;
394 }
395
396 return -EOPNOTSUPP;
397}
398
399#if IS_ENABLED(CONFIG_NET_PKTGEN)
400int pktgen_xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
401{
402 return xfrm_outer_mode_output(x, skb);
403}
404EXPORT_SYMBOL_GPL(pktgen_xfrm_outer_mode_output);
405#endif
406
Herbert Xuc6581a42007-11-13 21:43:43 -0800407static int xfrm_output_one(struct sk_buff *skb, int err)
Herbert Xu406ef772007-10-08 17:16:30 -0700408{
Eric Dumazetadf30902009-06-02 05:19:30 +0000409 struct dst_entry *dst = skb_dst(skb);
Herbert Xu406ef772007-10-08 17:16:30 -0700410 struct xfrm_state *x = dst->xfrm;
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800411 struct net *net = xs_net(x);
Herbert Xu406ef772007-10-08 17:16:30 -0700412
Herbert Xuc6581a42007-11-13 21:43:43 -0800413 if (err <= 0)
414 goto resume;
Herbert Xu406ef772007-10-08 17:16:30 -0700415
416 do {
Steffen Klassert26b20722012-03-21 23:32:39 +0000417 err = xfrm_skb_check_space(skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800418 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800419 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu910ef702007-12-18 22:14:25 -0800420 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800421 }
Herbert Xu910ef702007-12-18 22:14:25 -0800422
Steffen Klassert9b42c1f2018-06-12 12:44:26 +0200423 skb->mark = xfrm_smark_get(skb->mark, x);
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900424
Florian Westphal0c620e92019-03-29 21:16:25 +0100425 err = xfrm_outer_mode_output(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800426 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800427 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Herbert Xu910ef702007-12-18 22:14:25 -0800428 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800429 }
Herbert Xua2deb6d22007-11-13 21:39:38 -0800430
Herbert Xu406ef772007-10-08 17:16:30 -0700431 spin_lock_bh(&x->lock);
Li RongQingbb65a9c2012-12-28 16:06:28 +0800432
433 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
434 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
Timo Teräs497574c2013-05-22 01:40:47 +0000435 err = -EINVAL;
Li RongQingfa8599d2013-02-01 13:17:16 +0800436 goto error;
Li RongQingbb65a9c2012-12-28 16:06:28 +0800437 }
438
Herbert Xu910ef702007-12-18 22:14:25 -0800439 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800440 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800441 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -0700442 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800443 }
Herbert Xu406ef772007-10-08 17:16:30 -0700444
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000445 err = x->repl->overflow(x, skb);
446 if (err) {
447 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
448 goto error;
Herbert Xu436a0a42007-10-08 17:25:53 -0700449 }
450
Herbert Xu406ef772007-10-08 17:16:30 -0700451 x->curlft.bytes += skb->len;
452 x->curlft.packets++;
453
Herbert Xu406ef772007-10-08 17:16:30 -0700454 spin_unlock_bh(&x->lock);
455
Steffen Klassert3bc07322011-03-15 21:08:28 +0000456 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +0200457 if (!skb_dst(skb)) {
458 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Wei Yongjun533555e2018-10-27 06:12:06 +0000459 err = -EHOSTUNREACH;
Steffen Klassert9e143792018-09-11 10:31:15 +0200460 goto error_nolock;
461 }
Steffen Klassert3bc07322011-03-15 21:08:28 +0000462
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200463 if (xfrm_offload(skb)) {
464 x->type_offload->encap(x, skb);
465 } else {
Steffen Klassert73b9fc42017-10-30 10:04:04 +0100466 /* Inner headers are invalid now. */
467 skb->encapsulation = 0;
468
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200469 err = x->type->output(x, skb);
470 if (err == -EINPROGRESS)
471 goto out;
472 }
Herbert Xuc6581a42007-11-13 21:43:43 -0800473
474resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800475 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800476 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c6538c2007-10-09 13:33:35 -0700477 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800478 }
Herbert Xub7c6538c2007-10-09 13:33:35 -0700479
Steffen Klassert8764ab22010-06-04 01:57:38 +0000480 dst = skb_dst_pop(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000481 if (!dst) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800482 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -0700483 err = -EHOSTUNREACH;
484 goto error_nolock;
485 }
Steffen Klasserte4334302011-03-15 21:09:32 +0000486 skb_dst_set(skb, dst);
Herbert Xu406ef772007-10-08 17:16:30 -0700487 x = dst->xfrm;
Florian Westphalc9500d72019-03-29 21:16:32 +0100488 } while (x && !(x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700489
Jean Sacrenebd46872013-06-01 16:23:15 +0000490 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700491
Herbert Xu406ef772007-10-08 17:16:30 -0700492error:
493 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800494error_nolock:
495 kfree_skb(skb);
Jean Sacrenebd46872013-06-01 16:23:15 +0000496out:
497 return err;
Herbert Xu862b82c2007-11-13 21:43:11 -0800498}
499
Herbert Xuc6581a42007-11-13 21:43:43 -0800500int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800501{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500502 struct net *net = xs_net(skb_dst(skb)->xfrm);
Eric W. Biedermanbe10de02015-09-17 17:21:31 -0500503
Herbert Xuc6581a42007-11-13 21:43:43 -0800504 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Florian Westphal895b5c92019-09-29 20:54:03 +0200505 nf_reset_ct(skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800506
Eric W. Biedermancf91a992015-10-07 16:48:45 -0500507 err = skb_dst(skb)->ops->local_out(net, skb->sk, skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800508 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800509 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800510
Eric Dumazetadf30902009-06-02 05:19:30 +0000511 if (!skb_dst(skb)->xfrm)
Eric W. Biederman13206b62015-10-07 16:48:35 -0500512 return dst_output(net, skb->sk, skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800513
Eric Dumazetadf30902009-06-02 05:19:30 +0000514 err = nf_hook(skb_dst(skb)->ops->family,
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500515 NF_INET_POST_ROUTING, net, skb->sk, skb,
Eric Dumazetadf30902009-06-02 05:19:30 +0000516 NULL, skb_dst(skb)->dev, xfrm_output2);
Herbert Xu862b82c2007-11-13 21:43:11 -0800517 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800518 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800519 }
520
Herbert Xuc6581a42007-11-13 21:43:43 -0800521 if (err == -EINPROGRESS)
522 err = 0;
523
524out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800525 return err;
526}
Herbert Xuc6581a42007-11-13 21:43:43 -0800527EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800528
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500529static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800530{
531 return xfrm_output_resume(skb, 1);
532}
533
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500534static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800535{
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500536 struct sk_buff *segs, *nskb;
Herbert Xu862b82c2007-11-13 21:43:11 -0800537
Cambda Zhua08e7fd2020-03-26 15:33:14 +0800538 BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET);
539 BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_GSO_CB_OFFSET);
Herbert Xu862b82c2007-11-13 21:43:11 -0800540 segs = skb_gso_segment(skb, 0);
541 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700542 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800543 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200544 if (segs == NULL)
545 return -EINVAL;
Herbert Xu862b82c2007-11-13 21:43:11 -0800546
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500547 skb_list_walk_safe(segs, segs, nskb) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800548 int err;
549
David S. Millera8305bf2018-07-29 20:42:53 -0700550 skb_mark_not_on_list(segs);
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500551 err = xfrm_output2(net, sk, segs);
Herbert Xu862b82c2007-11-13 21:43:11 -0800552
553 if (unlikely(err)) {
Florian Westphal46cfd722014-09-10 01:08:46 +0200554 kfree_skb_list(nskb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800555 return err;
556 }
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500557 }
Herbert Xu862b82c2007-11-13 21:43:11 -0800558
559 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700560}
Herbert Xuc6581a42007-11-13 21:43:43 -0800561
David Miller7026b1d2015-04-05 22:19:04 -0400562int xfrm_output(struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800563{
Eric Dumazetadf30902009-06-02 05:19:30 +0000564 struct net *net = dev_net(skb_dst(skb)->dev);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200565 struct xfrm_state *x = skb_dst(skb)->xfrm;
Herbert Xuc6581a42007-11-13 21:43:43 -0800566 int err;
567
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200568 secpath_reset(skb);
569
570 if (xfrm_dev_offload_ok(skb, x)) {
571 struct sec_path *sp;
572
Florian Westphala84e3f52018-12-18 17:15:26 +0100573 sp = secpath_set(skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200574 if (!sp) {
575 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
576 kfree_skb(skb);
577 return -ENOMEM;
578 }
Ilan Tayarif1bd7d62017-04-14 10:07:39 +0200579 skb->encapsulation = 1;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200580
581 sp->olen++;
Florian Westphala84e3f52018-12-18 17:15:26 +0100582 sp->xvec[sp->len++] = x;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200583 xfrm_state_hold(x);
584
585 if (skb_is_gso(skb)) {
586 skb_shinfo(skb)->gso_type |= SKB_GSO_ESP;
587
588 return xfrm_output2(net, sk, skb);
589 }
590
591 if (x->xso.dev && x->xso.dev->features & NETIF_F_HW_ESP_TX_CSUM)
592 goto out;
593 }
594
Herbert Xuc6581a42007-11-13 21:43:43 -0800595 if (skb_is_gso(skb))
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500596 return xfrm_output_gso(net, sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800597
598 if (skb->ip_summed == CHECKSUM_PARTIAL) {
599 err = skb_checksum_help(skb);
600 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800601 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800602 kfree_skb(skb);
603 return err;
604 }
605 }
606
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200607out:
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500608 return xfrm_output2(net, sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800609}
Fabian Frederickfc680862014-05-12 19:54:34 +0200610EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700611
Florian Westphal0c620e92019-03-29 21:16:25 +0100612static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700613{
Florian Westphal733a5fa2019-03-29 21:16:30 +0100614 const struct xfrm_state_afinfo *afinfo;
Florian Westphal4c145dc2019-03-29 21:16:31 +0100615 const struct xfrm_mode *inner_mode;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100616 int err = -EAFNOSUPPORT;
617
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700618 if (x->sel.family == AF_UNSPEC)
619 inner_mode = xfrm_ip2inner_mode(x,
Eric Dumazetadf30902009-06-02 05:19:30 +0000620 xfrm_af2proto(skb_dst(skb)->ops->family));
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700621 else
Florian Westphalc9500d72019-03-29 21:16:32 +0100622 inner_mode = &x->inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700623
624 if (inner_mode == NULL)
625 return -EAFNOSUPPORT;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100626
627 rcu_read_lock();
628 afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family);
629 if (likely(afinfo))
630 err = afinfo->extract_output(x, skb);
631 rcu_read_unlock();
632
633 return err;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700634}
635
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200636void xfrm_local_error(struct sk_buff *skb, int mtu)
637{
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200638 unsigned int proto;
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200639 struct xfrm_state_afinfo *afinfo;
640
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200641 if (skb->protocol == htons(ETH_P_IP))
642 proto = AF_INET;
643 else if (skb->protocol == htons(ETH_P_IPV6))
644 proto = AF_INET6;
645 else
646 return;
647
648 afinfo = xfrm_state_get_afinfo(proto);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900649 if (afinfo) {
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100650 afinfo->local_error(skb, mtu);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900651 rcu_read_unlock();
652 }
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200653}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200654EXPORT_SYMBOL_GPL(xfrm_local_error);