blob: 9c43b8dd80fb4478ec5c9462b84f3a276121f0b3 [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 Westphal6d64be32020-05-04 10:06:03 +020016#include <net/icmp.h>
Florian Westphal1de70832019-03-29 21:16:29 +010017#include <net/inet_ecn.h>
Herbert Xu406ef772007-10-08 17:16:30 -070018#include <net/xfrm.h>
19
Florian Westphalf3075f482020-05-04 10:06:08 +020020#if IS_ENABLED(CONFIG_IPV6)
21#include <net/ip6_route.h>
22#include <net/ipv6_stubs.h>
23#endif
24
Florian Westphal1de70832019-03-29 21:16:29 +010025#include "xfrm_inout.h"
26
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050027static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb);
Florian Westphal0c620e92019-03-29 21:16:25 +010028static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
Herbert Xuc6581a42007-11-13 21:43:43 -080029
Steffen Klassert26b20722012-03-21 23:32:39 +000030static int xfrm_skb_check_space(struct sk_buff *skb)
Herbert Xu83815de2007-10-08 17:25:08 -070031{
Eric Dumazetadf30902009-06-02 05:19:30 +000032 struct dst_entry *dst = skb_dst(skb);
Herbert Xu550ade82007-11-13 21:33:01 -080033 int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
Herbert Xu83815de2007-10-08 17:25:08 -070034 - skb_headroom(skb);
Johannes Bergf5184d22008-05-12 20:48:31 -070035 int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
Herbert Xu83815de2007-10-08 17:25:08 -070036
Herbert Xud01dbeb2008-09-30 02:03:19 -070037 if (nhead <= 0) {
38 if (ntail <= 0)
39 return 0;
40 nhead = 0;
41 } else if (ntail < 0)
42 ntail = 0;
Herbert Xu83815de2007-10-08 17:25:08 -070043
Herbert Xud01dbeb2008-09-30 02:03:19 -070044 return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
Herbert Xu83815de2007-10-08 17:25:08 -070045}
46
Ying Xue9449c3c2015-05-12 18:29:44 +080047/* Children define the path of the packet through the
48 * Linux networking. Thus, destinations are stackable.
49 */
50
51static struct dst_entry *skb_dst_pop(struct sk_buff *skb)
52{
David Millerb92cf4a2017-11-28 15:40:22 -050053 struct dst_entry *child = dst_clone(xfrm_dst_child(skb_dst(skb)));
Ying Xue9449c3c2015-05-12 18:29:44 +080054
55 skb_dst_drop(skb);
56 return child;
57}
58
Florian Westphal0c620e92019-03-29 21:16:25 +010059/* Add encapsulation header.
60 *
61 * The IP header will be moved forward to make space for the encapsulation
62 * header.
63 */
64static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
65{
Florian Westphal0c620e92019-03-29 21:16:25 +010066 struct iphdr *iph = ip_hdr(skb);
67 int ihl = iph->ihl * 4;
68
69 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
70
71 skb_set_network_header(skb, -x->props.header_len);
72 skb->mac_header = skb->network_header +
73 offsetof(struct iphdr, protocol);
74 skb->transport_header = skb->network_header + ihl;
75 __skb_pull(skb, ihl);
76 memmove(skb_network_header(skb), iph, ihl);
77 return 0;
Florian Westphal0c620e92019-03-29 21:16:25 +010078}
79
80/* Add encapsulation header.
81 *
82 * The IP header and mutable extension headers will be moved forward to make
83 * space for the encapsulation header.
84 */
85static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
86{
Florian Westphal4c145dc2019-03-29 21:16:31 +010087#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal0c620e92019-03-29 21:16:25 +010088 struct ipv6hdr *iph;
89 u8 *prevhdr;
90 int hdr_len;
91
92 iph = ipv6_hdr(skb);
93 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
94
95 hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
96 if (hdr_len < 0)
97 return hdr_len;
98 skb_set_mac_header(skb,
99 (prevhdr - x->props.header_len) - skb->data);
100 skb_set_network_header(skb, -x->props.header_len);
101 skb->transport_header = skb->network_header + hdr_len;
102 __skb_pull(skb, hdr_len);
103 memmove(ipv6_hdr(skb), iph, hdr_len);
104 return 0;
105#else
106 WARN_ON_ONCE(1);
Florian Westphal4c145dc2019-03-29 21:16:31 +0100107 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100108#endif
109}
110
111/* Add route optimization header space.
112 *
113 * The IP header and mutable extension headers will be moved forward to make
114 * space for the route optimization header.
115 */
116static int xfrm6_ro_output(struct xfrm_state *x, struct sk_buff *skb)
117{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100118#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100119 struct ipv6hdr *iph;
120 u8 *prevhdr;
121 int hdr_len;
122
123 iph = ipv6_hdr(skb);
124
125 hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
126 if (hdr_len < 0)
127 return hdr_len;
128 skb_set_mac_header(skb,
129 (prevhdr - x->props.header_len) - skb->data);
130 skb_set_network_header(skb, -x->props.header_len);
131 skb->transport_header = skb->network_header + hdr_len;
132 __skb_pull(skb, hdr_len);
133 memmove(ipv6_hdr(skb), iph, hdr_len);
134
135 x->lastused = ktime_get_real_seconds();
136
137 return 0;
138#else
139 WARN_ON_ONCE(1);
Florian Westphal4c145dc2019-03-29 21:16:31 +0100140 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100141#endif
142}
143
Florian Westphal1de70832019-03-29 21:16:29 +0100144/* Add encapsulation header.
145 *
146 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
147 */
148static int xfrm4_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb)
149{
150 struct ip_beet_phdr *ph;
151 struct iphdr *top_iph;
152 int hdrlen, optlen;
153
154 hdrlen = 0;
155 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
156 if (unlikely(optlen))
157 hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
158
159 skb_set_network_header(skb, -x->props.header_len - hdrlen +
160 (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph)));
161 if (x->sel.family != AF_INET6)
162 skb->network_header += IPV4_BEET_PHMAXLEN;
163 skb->mac_header = skb->network_header +
164 offsetof(struct iphdr, protocol);
165 skb->transport_header = skb->network_header + sizeof(*top_iph);
166
167 xfrm4_beet_make_header(skb);
168
169 ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen);
170
171 top_iph = ip_hdr(skb);
172
173 if (unlikely(optlen)) {
174 if (WARN_ON(optlen < 0))
175 return -EINVAL;
176
177 ph->padlen = 4 - (optlen & 4);
178 ph->hdrlen = optlen / 8;
179 ph->nexthdr = top_iph->protocol;
180 if (ph->padlen)
181 memset(ph + 1, IPOPT_NOP, ph->padlen);
182
183 top_iph->protocol = IPPROTO_BEETPH;
184 top_iph->ihl = sizeof(struct iphdr) / 4;
185 }
186
187 top_iph->saddr = x->props.saddr.a4;
188 top_iph->daddr = x->id.daddr.a4;
189
190 return 0;
191}
192
193/* Add encapsulation header.
194 *
195 * The top IP header will be constructed per RFC 2401.
196 */
197static int xfrm4_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb)
198{
199 struct dst_entry *dst = skb_dst(skb);
200 struct iphdr *top_iph;
201 int flags;
202
203 skb_set_inner_network_header(skb, skb_network_offset(skb));
204 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
205
206 skb_set_network_header(skb, -x->props.header_len);
207 skb->mac_header = skb->network_header +
208 offsetof(struct iphdr, protocol);
209 skb->transport_header = skb->network_header + sizeof(*top_iph);
210 top_iph = ip_hdr(skb);
211
212 top_iph->ihl = 5;
213 top_iph->version = 4;
214
215 top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family);
216
217 /* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */
218 if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
219 top_iph->tos = 0;
220 else
221 top_iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
222 top_iph->tos = INET_ECN_encapsulate(top_iph->tos,
223 XFRM_MODE_SKB_CB(skb)->tos);
224
225 flags = x->props.flags;
226 if (flags & XFRM_STATE_NOECN)
227 IP_ECN_clear(top_iph);
228
229 top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
230 0 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF));
231
232 top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst));
233
234 top_iph->saddr = x->props.saddr.a4;
235 top_iph->daddr = x->id.daddr.a4;
236 ip_select_ident(dev_net(dst->dev), skb, NULL);
237
238 return 0;
239}
240
241#if IS_ENABLED(CONFIG_IPV6)
242static int xfrm6_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb)
243{
244 struct dst_entry *dst = skb_dst(skb);
245 struct ipv6hdr *top_iph;
246 int dsfield;
247
248 skb_set_inner_network_header(skb, skb_network_offset(skb));
249 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
250
251 skb_set_network_header(skb, -x->props.header_len);
252 skb->mac_header = skb->network_header +
253 offsetof(struct ipv6hdr, nexthdr);
254 skb->transport_header = skb->network_header + sizeof(*top_iph);
255 top_iph = ipv6_hdr(skb);
256
257 top_iph->version = 6;
258
259 memcpy(top_iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl,
260 sizeof(top_iph->flow_lbl));
261 top_iph->nexthdr = xfrm_af2proto(skb_dst(skb)->ops->family);
262
263 if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
264 dsfield = 0;
265 else
266 dsfield = XFRM_MODE_SKB_CB(skb)->tos;
267 dsfield = INET_ECN_encapsulate(dsfield, XFRM_MODE_SKB_CB(skb)->tos);
268 if (x->props.flags & XFRM_STATE_NOECN)
269 dsfield &= ~INET_ECN_MASK;
270 ipv6_change_dsfield(top_iph, 0, dsfield);
271 top_iph->hop_limit = ip6_dst_hoplimit(xfrm_dst_child(dst));
272 top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
273 top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
274 return 0;
275}
276
277static int xfrm6_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb)
278{
279 struct ipv6hdr *top_iph;
280 struct ip_beet_phdr *ph;
281 int optlen, hdr_len;
282
283 hdr_len = 0;
284 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
285 if (unlikely(optlen))
286 hdr_len += IPV4_BEET_PHMAXLEN - (optlen & 4);
287
288 skb_set_network_header(skb, -x->props.header_len - hdr_len);
289 if (x->sel.family != AF_INET6)
290 skb->network_header += IPV4_BEET_PHMAXLEN;
291 skb->mac_header = skb->network_header +
292 offsetof(struct ipv6hdr, nexthdr);
293 skb->transport_header = skb->network_header + sizeof(*top_iph);
294 ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdr_len);
295
296 xfrm6_beet_make_header(skb);
297
298 top_iph = ipv6_hdr(skb);
299 if (unlikely(optlen)) {
300 if (WARN_ON(optlen < 0))
301 return -EINVAL;
302
303 ph->padlen = 4 - (optlen & 4);
304 ph->hdrlen = optlen / 8;
305 ph->nexthdr = top_iph->nexthdr;
306 if (ph->padlen)
307 memset(ph + 1, IPOPT_NOP, ph->padlen);
308
309 top_iph->nexthdr = IPPROTO_BEETPH;
310 }
311
312 top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
313 top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
314 return 0;
315}
316#endif
317
318/* Add encapsulation header.
319 *
320 * On exit, the transport header will be set to the start of the
321 * encapsulation header to be filled in by x->type->output and the mac
322 * header will be set to the nextheader (protocol for IPv4) field of the
323 * extension header directly preceding the encapsulation header, or in
324 * its absence, that of the top IP header.
325 * The value of the network header will always point to the top IP header
326 * while skb->data will point to the payload.
327 */
Florian Westphal0c620e92019-03-29 21:16:25 +0100328static int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
329{
330 int err;
331
332 err = xfrm_inner_extract_output(x, skb);
333 if (err)
334 return err;
335
336 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
337 skb->protocol = htons(ETH_P_IP);
338
Florian Westphalc9500d72019-03-29 21:16:32 +0100339 switch (x->outer_mode.encap) {
Florian Westphal1de70832019-03-29 21:16:29 +0100340 case XFRM_MODE_BEET:
341 return xfrm4_beet_encap_add(x, skb);
342 case XFRM_MODE_TUNNEL:
343 return xfrm4_tunnel_encap_add(x, skb);
344 }
345
346 WARN_ON_ONCE(1);
347 return -EOPNOTSUPP;
Florian Westphal0c620e92019-03-29 21:16:25 +0100348}
349
350static int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
351{
352#if IS_ENABLED(CONFIG_IPV6)
353 int err;
354
355 err = xfrm_inner_extract_output(x, skb);
356 if (err)
357 return err;
358
359 skb->ignore_df = 1;
360 skb->protocol = htons(ETH_P_IPV6);
361
Florian Westphalc9500d72019-03-29 21:16:32 +0100362 switch (x->outer_mode.encap) {
Florian Westphal1de70832019-03-29 21:16:29 +0100363 case XFRM_MODE_BEET:
364 return xfrm6_beet_encap_add(x, skb);
365 case XFRM_MODE_TUNNEL:
366 return xfrm6_tunnel_encap_add(x, skb);
367 default:
368 WARN_ON_ONCE(1);
369 return -EOPNOTSUPP;
370 }
Florian Westphal0c620e92019-03-29 21:16:25 +0100371#endif
Florian Westphal1de70832019-03-29 21:16:29 +0100372 WARN_ON_ONCE(1);
373 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100374}
375
376static int xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
377{
Florian Westphalc9500d72019-03-29 21:16:32 +0100378 switch (x->outer_mode.encap) {
Florian Westphal0c620e92019-03-29 21:16:25 +0100379 case XFRM_MODE_BEET:
380 case XFRM_MODE_TUNNEL:
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_prepare_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_prepare_output(x, skb);
385 break;
386 case XFRM_MODE_TRANSPORT:
Florian Westphalc9500d72019-03-29 21:16:32 +0100387 if (x->outer_mode.family == AF_INET)
Florian Westphal0c620e92019-03-29 21:16:25 +0100388 return xfrm4_transport_output(x, skb);
Florian Westphalc9500d72019-03-29 21:16:32 +0100389 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100390 return xfrm6_transport_output(x, skb);
391 break;
392 case XFRM_MODE_ROUTEOPTIMIZATION:
Florian Westphalc9500d72019-03-29 21:16:32 +0100393 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100394 return xfrm6_ro_output(x, skb);
395 WARN_ON_ONCE(1);
396 break;
397 default:
398 WARN_ON_ONCE(1);
399 break;
400 }
401
402 return -EOPNOTSUPP;
403}
404
405#if IS_ENABLED(CONFIG_NET_PKTGEN)
406int pktgen_xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
407{
408 return xfrm_outer_mode_output(x, skb);
409}
410EXPORT_SYMBOL_GPL(pktgen_xfrm_outer_mode_output);
411#endif
412
Herbert Xuc6581a42007-11-13 21:43:43 -0800413static int xfrm_output_one(struct sk_buff *skb, int err)
Herbert Xu406ef772007-10-08 17:16:30 -0700414{
Eric Dumazetadf30902009-06-02 05:19:30 +0000415 struct dst_entry *dst = skb_dst(skb);
Herbert Xu406ef772007-10-08 17:16:30 -0700416 struct xfrm_state *x = dst->xfrm;
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800417 struct net *net = xs_net(x);
Herbert Xu406ef772007-10-08 17:16:30 -0700418
Herbert Xuc6581a42007-11-13 21:43:43 -0800419 if (err <= 0)
420 goto resume;
Herbert Xu406ef772007-10-08 17:16:30 -0700421
422 do {
Steffen Klassert26b20722012-03-21 23:32:39 +0000423 err = xfrm_skb_check_space(skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800424 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800425 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu910ef702007-12-18 22:14:25 -0800426 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800427 }
Herbert Xu910ef702007-12-18 22:14:25 -0800428
Steffen Klassert9b42c1f2018-06-12 12:44:26 +0200429 skb->mark = xfrm_smark_get(skb->mark, x);
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900430
Florian Westphal0c620e92019-03-29 21:16:25 +0100431 err = xfrm_outer_mode_output(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800432 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800433 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Herbert Xu910ef702007-12-18 22:14:25 -0800434 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800435 }
Herbert Xua2deb6d22007-11-13 21:39:38 -0800436
Herbert Xu406ef772007-10-08 17:16:30 -0700437 spin_lock_bh(&x->lock);
Li RongQingbb65a9c2012-12-28 16:06:28 +0800438
439 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
440 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
Timo Teräs497574c2013-05-22 01:40:47 +0000441 err = -EINVAL;
Li RongQingfa8599d2013-02-01 13:17:16 +0800442 goto error;
Li RongQingbb65a9c2012-12-28 16:06:28 +0800443 }
444
Herbert Xu910ef702007-12-18 22:14:25 -0800445 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800446 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800447 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -0700448 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800449 }
Herbert Xu406ef772007-10-08 17:16:30 -0700450
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000451 err = x->repl->overflow(x, skb);
452 if (err) {
453 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
454 goto error;
Herbert Xu436a0a42007-10-08 17:25:53 -0700455 }
456
Herbert Xu406ef772007-10-08 17:16:30 -0700457 x->curlft.bytes += skb->len;
458 x->curlft.packets++;
459
Herbert Xu406ef772007-10-08 17:16:30 -0700460 spin_unlock_bh(&x->lock);
461
Steffen Klassert3bc07322011-03-15 21:08:28 +0000462 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +0200463 if (!skb_dst(skb)) {
464 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Wei Yongjun533555e2018-10-27 06:12:06 +0000465 err = -EHOSTUNREACH;
Steffen Klassert9e143792018-09-11 10:31:15 +0200466 goto error_nolock;
467 }
Steffen Klassert3bc07322011-03-15 21:08:28 +0000468
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200469 if (xfrm_offload(skb)) {
470 x->type_offload->encap(x, skb);
471 } else {
Steffen Klassert73b9fc42017-10-30 10:04:04 +0100472 /* Inner headers are invalid now. */
473 skb->encapsulation = 0;
474
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200475 err = x->type->output(x, skb);
476 if (err == -EINPROGRESS)
477 goto out;
478 }
Herbert Xuc6581a42007-11-13 21:43:43 -0800479
480resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800481 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800482 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c6538c2007-10-09 13:33:35 -0700483 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800484 }
Herbert Xub7c6538c2007-10-09 13:33:35 -0700485
Steffen Klassert8764ab22010-06-04 01:57:38 +0000486 dst = skb_dst_pop(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000487 if (!dst) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800488 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -0700489 err = -EHOSTUNREACH;
490 goto error_nolock;
491 }
Steffen Klasserte4334302011-03-15 21:09:32 +0000492 skb_dst_set(skb, dst);
Herbert Xu406ef772007-10-08 17:16:30 -0700493 x = dst->xfrm;
Florian Westphalc9500d72019-03-29 21:16:32 +0100494 } while (x && !(x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700495
Jean Sacrenebd46872013-06-01 16:23:15 +0000496 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700497
Herbert Xu406ef772007-10-08 17:16:30 -0700498error:
499 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800500error_nolock:
501 kfree_skb(skb);
Jean Sacrenebd46872013-06-01 16:23:15 +0000502out:
503 return err;
Herbert Xu862b82c2007-11-13 21:43:11 -0800504}
505
Herbert Xuc6581a42007-11-13 21:43:43 -0800506int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800507{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500508 struct net *net = xs_net(skb_dst(skb)->xfrm);
Eric W. Biedermanbe10de02015-09-17 17:21:31 -0500509
Herbert Xuc6581a42007-11-13 21:43:43 -0800510 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Florian Westphal895b5c92019-09-29 20:54:03 +0200511 nf_reset_ct(skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800512
Eric W. Biedermancf91a992015-10-07 16:48:45 -0500513 err = skb_dst(skb)->ops->local_out(net, skb->sk, skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800514 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800515 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800516
Eric Dumazetadf30902009-06-02 05:19:30 +0000517 if (!skb_dst(skb)->xfrm)
Eric W. Biederman13206b62015-10-07 16:48:35 -0500518 return dst_output(net, skb->sk, skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800519
Eric Dumazetadf30902009-06-02 05:19:30 +0000520 err = nf_hook(skb_dst(skb)->ops->family,
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500521 NF_INET_POST_ROUTING, net, skb->sk, skb,
Eric Dumazetadf30902009-06-02 05:19:30 +0000522 NULL, skb_dst(skb)->dev, xfrm_output2);
Herbert Xu862b82c2007-11-13 21:43:11 -0800523 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800524 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800525 }
526
Herbert Xuc6581a42007-11-13 21:43:43 -0800527 if (err == -EINPROGRESS)
528 err = 0;
529
530out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800531 return err;
532}
Herbert Xuc6581a42007-11-13 21:43:43 -0800533EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800534
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500535static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800536{
537 return xfrm_output_resume(skb, 1);
538}
539
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500540static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800541{
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500542 struct sk_buff *segs, *nskb;
Herbert Xu862b82c2007-11-13 21:43:11 -0800543
Cambda Zhua08e7fd2020-03-26 15:33:14 +0800544 BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET);
545 BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_GSO_CB_OFFSET);
Herbert Xu862b82c2007-11-13 21:43:11 -0800546 segs = skb_gso_segment(skb, 0);
547 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700548 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800549 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200550 if (segs == NULL)
551 return -EINVAL;
Herbert Xu862b82c2007-11-13 21:43:11 -0800552
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500553 skb_list_walk_safe(segs, segs, nskb) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800554 int err;
555
David S. Millera8305bf2018-07-29 20:42:53 -0700556 skb_mark_not_on_list(segs);
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500557 err = xfrm_output2(net, sk, segs);
Herbert Xu862b82c2007-11-13 21:43:11 -0800558
559 if (unlikely(err)) {
Florian Westphal46cfd722014-09-10 01:08:46 +0200560 kfree_skb_list(nskb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800561 return err;
562 }
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500563 }
Herbert Xu862b82c2007-11-13 21:43:11 -0800564
565 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700566}
Herbert Xuc6581a42007-11-13 21:43:43 -0800567
David Miller7026b1d2015-04-05 22:19:04 -0400568int xfrm_output(struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800569{
Eric Dumazetadf30902009-06-02 05:19:30 +0000570 struct net *net = dev_net(skb_dst(skb)->dev);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200571 struct xfrm_state *x = skb_dst(skb)->xfrm;
Herbert Xuc6581a42007-11-13 21:43:43 -0800572 int err;
573
Florian Westphal2ab60962020-05-04 10:06:09 +0200574 switch (x->outer_mode.family) {
575 case AF_INET:
576 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
577#ifdef CONFIG_NETFILTER
578 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
579#endif
580 break;
581 case AF_INET6:
582 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
583
584#ifdef CONFIG_NETFILTER
585 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
586#endif
587 break;
588 }
589
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200590 secpath_reset(skb);
591
592 if (xfrm_dev_offload_ok(skb, x)) {
593 struct sec_path *sp;
594
Florian Westphala84e3f52018-12-18 17:15:26 +0100595 sp = secpath_set(skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200596 if (!sp) {
597 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
598 kfree_skb(skb);
599 return -ENOMEM;
600 }
Ilan Tayarif1bd7d62017-04-14 10:07:39 +0200601 skb->encapsulation = 1;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200602
603 sp->olen++;
Florian Westphala84e3f52018-12-18 17:15:26 +0100604 sp->xvec[sp->len++] = x;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200605 xfrm_state_hold(x);
606
607 if (skb_is_gso(skb)) {
608 skb_shinfo(skb)->gso_type |= SKB_GSO_ESP;
609
610 return xfrm_output2(net, sk, skb);
611 }
612
613 if (x->xso.dev && x->xso.dev->features & NETIF_F_HW_ESP_TX_CSUM)
614 goto out;
615 }
616
Herbert Xuc6581a42007-11-13 21:43:43 -0800617 if (skb_is_gso(skb))
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500618 return xfrm_output_gso(net, sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800619
620 if (skb->ip_summed == CHECKSUM_PARTIAL) {
621 err = skb_checksum_help(skb);
622 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800623 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800624 kfree_skb(skb);
625 return err;
626 }
627 }
628
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200629out:
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500630 return xfrm_output2(net, sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800631}
Fabian Frederickfc680862014-05-12 19:54:34 +0200632EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700633
Florian Westphal6d64be32020-05-04 10:06:03 +0200634static int xfrm4_tunnel_check_size(struct sk_buff *skb)
635{
636 int mtu, ret = 0;
637
638 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
639 goto out;
640
641 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
642 goto out;
643
644 mtu = dst_mtu(skb_dst(skb));
645 if ((!skb_is_gso(skb) && skb->len > mtu) ||
646 (skb_is_gso(skb) &&
647 !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) {
648 skb->protocol = htons(ETH_P_IP);
649
650 if (skb->sk)
651 xfrm_local_error(skb, mtu);
652 else
653 icmp_send(skb, ICMP_DEST_UNREACH,
654 ICMP_FRAG_NEEDED, htonl(mtu));
655 ret = -EMSGSIZE;
656 }
657out:
658 return ret;
659}
660
661static int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
662{
663 int err;
664
665 err = xfrm4_tunnel_check_size(skb);
666 if (err)
667 return err;
668
669 XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
670
671 xfrm4_extract_header(skb);
672 return 0;
673}
674
Florian Westphalf3075f482020-05-04 10:06:08 +0200675#if IS_ENABLED(CONFIG_IPV6)
676static int xfrm6_tunnel_check_size(struct sk_buff *skb)
677{
678 int mtu, ret = 0;
679 struct dst_entry *dst = skb_dst(skb);
680
681 if (skb->ignore_df)
682 goto out;
683
684 mtu = dst_mtu(dst);
685 if (mtu < IPV6_MIN_MTU)
686 mtu = IPV6_MIN_MTU;
687
688 if ((!skb_is_gso(skb) && skb->len > mtu) ||
689 (skb_is_gso(skb) &&
690 !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
691 skb->dev = dst->dev;
692 skb->protocol = htons(ETH_P_IPV6);
693
694 if (xfrm6_local_dontfrag(skb->sk))
695 ipv6_stub->xfrm6_local_rxpmtu(skb, mtu);
696 else if (skb->sk)
697 xfrm_local_error(skb, mtu);
698 else
699 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
700 ret = -EMSGSIZE;
701 }
702out:
703 return ret;
704}
705#endif
706
707static int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
708{
709#if IS_ENABLED(CONFIG_IPV6)
710 int err;
711
712 err = xfrm6_tunnel_check_size(skb);
713 if (err)
714 return err;
715
716 XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
717
718 xfrm6_extract_header(skb);
719 return 0;
720#else
721 WARN_ON_ONCE(1);
722 return -EAFNOSUPPORT;
723#endif
724}
725
Florian Westphal0c620e92019-03-29 21:16:25 +0100726static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700727{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100728 const struct xfrm_mode *inner_mode;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100729
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700730 if (x->sel.family == AF_UNSPEC)
731 inner_mode = xfrm_ip2inner_mode(x,
Eric Dumazetadf30902009-06-02 05:19:30 +0000732 xfrm_af2proto(skb_dst(skb)->ops->family));
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700733 else
Florian Westphalc9500d72019-03-29 21:16:32 +0100734 inner_mode = &x->inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700735
736 if (inner_mode == NULL)
737 return -EAFNOSUPPORT;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100738
Florian Westphal6d64be32020-05-04 10:06:03 +0200739 switch (inner_mode->family) {
740 case AF_INET:
741 return xfrm4_extract_output(x, skb);
Florian Westphalf3075f482020-05-04 10:06:08 +0200742 case AF_INET6:
743 return xfrm6_extract_output(x, skb);
Florian Westphal6d64be32020-05-04 10:06:03 +0200744 }
Florian Westphal733a5fa2019-03-29 21:16:30 +0100745
Florian Westphalf3075f482020-05-04 10:06:08 +0200746 return -EAFNOSUPPORT;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700747}
748
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200749void xfrm_local_error(struct sk_buff *skb, int mtu)
750{
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200751 unsigned int proto;
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200752 struct xfrm_state_afinfo *afinfo;
753
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200754 if (skb->protocol == htons(ETH_P_IP))
755 proto = AF_INET;
756 else if (skb->protocol == htons(ETH_P_IPV6))
757 proto = AF_INET6;
758 else
759 return;
760
761 afinfo = xfrm_state_get_afinfo(proto);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900762 if (afinfo) {
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100763 afinfo->local_error(skb, mtu);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900764 rcu_read_unlock();
765 }
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200766}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200767EXPORT_SYMBOL_GPL(xfrm_local_error);