blob: e321fc63a2e9d5c209793ea720e62aa3e0c41738 [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
Evan Nimmo9ab12652021-03-02 08:00:04 +1300506int xfrm_output_resume(struct sock *sk, 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
Evan Nimmo9ab12652021-03-02 08:00:04 +1300513 err = skb_dst(skb)->ops->local_out(net, 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)
Evan Nimmo9ab12652021-03-02 08:00:04 +1300518 return dst_output(net, 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,
Evan Nimmo9ab12652021-03-02 08:00:04 +1300521 NF_INET_POST_ROUTING, net, 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{
Evan Nimmo9ab12652021-03-02 08:00:04 +1300537 return xfrm_output_resume(sk, skb, 1);
Herbert Xuc6581a42007-11-13 21:43:43 -0800538}
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
Huy Nguyenfa453522021-06-14 17:33:48 +0300568/* For partial checksum offload, the outer header checksum is calculated
569 * by software and the inner header checksum is calculated by hardware.
570 * This requires hardware to know the inner packet type to calculate
571 * the inner header checksum. Save inner ip protocol here to avoid
572 * traversing the packet in the vendor's xmit code.
573 * If the encap type is IPIP, just save skb->inner_ipproto. Otherwise,
574 * get the ip protocol from the IP header.
575 */
576static void xfrm_get_inner_ipproto(struct sk_buff *skb)
577{
578 struct xfrm_offload *xo = xfrm_offload(skb);
579 const struct ethhdr *eth;
580
581 if (!xo)
582 return;
583
584 if (skb->inner_protocol_type == ENCAP_TYPE_IPPROTO) {
585 xo->inner_ipproto = skb->inner_ipproto;
586 return;
587 }
588
589 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER)
590 return;
591
592 eth = (struct ethhdr *)skb_inner_mac_header(skb);
593
594 switch (ntohs(eth->h_proto)) {
595 case ETH_P_IPV6:
596 xo->inner_ipproto = inner_ipv6_hdr(skb)->nexthdr;
597 break;
598 case ETH_P_IP:
599 xo->inner_ipproto = inner_ip_hdr(skb)->protocol;
600 break;
601 }
602}
603
David Miller7026b1d2015-04-05 22:19:04 -0400604int xfrm_output(struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800605{
Eric Dumazetadf30902009-06-02 05:19:30 +0000606 struct net *net = dev_net(skb_dst(skb)->dev);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200607 struct xfrm_state *x = skb_dst(skb)->xfrm;
Herbert Xuc6581a42007-11-13 21:43:43 -0800608 int err;
609
Florian Westphal2ab60962020-05-04 10:06:09 +0200610 switch (x->outer_mode.family) {
611 case AF_INET:
612 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Florian Westphal2ab60962020-05-04 10:06:09 +0200613 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
Florian Westphal2ab60962020-05-04 10:06:09 +0200614 break;
615 case AF_INET6:
616 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
617
Florian Westphal2ab60962020-05-04 10:06:09 +0200618 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
Florian Westphal2ab60962020-05-04 10:06:09 +0200619 break;
620 }
621
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200622 secpath_reset(skb);
623
624 if (xfrm_dev_offload_ok(skb, x)) {
625 struct sec_path *sp;
626
Florian Westphala84e3f52018-12-18 17:15:26 +0100627 sp = secpath_set(skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200628 if (!sp) {
629 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
630 kfree_skb(skb);
631 return -ENOMEM;
632 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200633
634 sp->olen++;
Florian Westphala84e3f52018-12-18 17:15:26 +0100635 sp->xvec[sp->len++] = x;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200636 xfrm_state_hold(x);
637
Huy Nguyenfa453522021-06-14 17:33:48 +0300638 if (skb->encapsulation)
639 xfrm_get_inner_ipproto(skb);
640 skb->encapsulation = 1;
641
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200642 if (skb_is_gso(skb)) {
Xin Longa204aef2020-04-20 21:51:09 +0800643 if (skb->inner_protocol)
644 return xfrm_output_gso(net, sk, skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200645
Xin Longa204aef2020-04-20 21:51:09 +0800646 skb_shinfo(skb)->gso_type |= SKB_GSO_ESP;
647 goto out;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200648 }
649
650 if (x->xso.dev && x->xso.dev->features & NETIF_F_HW_ESP_TX_CSUM)
651 goto out;
Xin Longa204aef2020-04-20 21:51:09 +0800652 } else {
653 if (skb_is_gso(skb))
654 return xfrm_output_gso(net, sk, skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200655 }
656
Herbert Xuc6581a42007-11-13 21:43:43 -0800657 if (skb->ip_summed == CHECKSUM_PARTIAL) {
658 err = skb_checksum_help(skb);
659 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800660 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800661 kfree_skb(skb);
662 return err;
663 }
664 }
665
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200666out:
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500667 return xfrm_output2(net, sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800668}
Fabian Frederickfc680862014-05-12 19:54:34 +0200669EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700670
Florian Westphal6d64be32020-05-04 10:06:03 +0200671static int xfrm4_tunnel_check_size(struct sk_buff *skb)
672{
673 int mtu, ret = 0;
674
675 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
676 goto out;
677
678 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
679 goto out;
680
681 mtu = dst_mtu(skb_dst(skb));
682 if ((!skb_is_gso(skb) && skb->len > mtu) ||
683 (skb_is_gso(skb) &&
684 !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) {
685 skb->protocol = htons(ETH_P_IP);
686
687 if (skb->sk)
688 xfrm_local_error(skb, mtu);
689 else
690 icmp_send(skb, ICMP_DEST_UNREACH,
691 ICMP_FRAG_NEEDED, htonl(mtu));
692 ret = -EMSGSIZE;
693 }
694out:
695 return ret;
696}
697
698static int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
699{
700 int err;
701
Xin Long68dc0222021-03-19 18:27:58 +0800702 if (x->outer_mode.encap == XFRM_MODE_BEET &&
703 ip_is_fragment(ip_hdr(skb))) {
704 net_warn_ratelimited("BEET mode doesn't support inner IPv4 fragments\n");
705 return -EAFNOSUPPORT;
706 }
707
Florian Westphal6d64be32020-05-04 10:06:03 +0200708 err = xfrm4_tunnel_check_size(skb);
709 if (err)
710 return err;
711
712 XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
713
714 xfrm4_extract_header(skb);
715 return 0;
716}
717
Florian Westphalf3075f482020-05-04 10:06:08 +0200718#if IS_ENABLED(CONFIG_IPV6)
719static int xfrm6_tunnel_check_size(struct sk_buff *skb)
720{
721 int mtu, ret = 0;
722 struct dst_entry *dst = skb_dst(skb);
723
724 if (skb->ignore_df)
725 goto out;
726
727 mtu = dst_mtu(dst);
728 if (mtu < IPV6_MIN_MTU)
729 mtu = IPV6_MIN_MTU;
730
731 if ((!skb_is_gso(skb) && skb->len > mtu) ||
732 (skb_is_gso(skb) &&
733 !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
734 skb->dev = dst->dev;
735 skb->protocol = htons(ETH_P_IPV6);
736
737 if (xfrm6_local_dontfrag(skb->sk))
738 ipv6_stub->xfrm6_local_rxpmtu(skb, mtu);
739 else if (skb->sk)
740 xfrm_local_error(skb, mtu);
741 else
742 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
743 ret = -EMSGSIZE;
744 }
745out:
746 return ret;
747}
748#endif
749
750static int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
751{
752#if IS_ENABLED(CONFIG_IPV6)
Xin Long68dc0222021-03-19 18:27:58 +0800753 unsigned int ptr = 0;
Florian Westphalf3075f482020-05-04 10:06:08 +0200754 int err;
755
Xin Long68dc0222021-03-19 18:27:58 +0800756 if (x->outer_mode.encap == XFRM_MODE_BEET &&
757 ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL, NULL) >= 0) {
758 net_warn_ratelimited("BEET mode doesn't support inner IPv6 fragments\n");
759 return -EAFNOSUPPORT;
760 }
761
Florian Westphalf3075f482020-05-04 10:06:08 +0200762 err = xfrm6_tunnel_check_size(skb);
763 if (err)
764 return err;
765
766 XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
767
768 xfrm6_extract_header(skb);
769 return 0;
770#else
771 WARN_ON_ONCE(1);
772 return -EAFNOSUPPORT;
773#endif
774}
775
Florian Westphal0c620e92019-03-29 21:16:25 +0100776static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700777{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100778 const struct xfrm_mode *inner_mode;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100779
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700780 if (x->sel.family == AF_UNSPEC)
781 inner_mode = xfrm_ip2inner_mode(x,
Eric Dumazetadf30902009-06-02 05:19:30 +0000782 xfrm_af2proto(skb_dst(skb)->ops->family));
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700783 else
Florian Westphalc9500d72019-03-29 21:16:32 +0100784 inner_mode = &x->inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700785
786 if (inner_mode == NULL)
787 return -EAFNOSUPPORT;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100788
Florian Westphal6d64be32020-05-04 10:06:03 +0200789 switch (inner_mode->family) {
790 case AF_INET:
791 return xfrm4_extract_output(x, skb);
Florian Westphalf3075f482020-05-04 10:06:08 +0200792 case AF_INET6:
793 return xfrm6_extract_output(x, skb);
Florian Westphal6d64be32020-05-04 10:06:03 +0200794 }
Florian Westphal733a5fa2019-03-29 21:16:30 +0100795
Florian Westphalf3075f482020-05-04 10:06:08 +0200796 return -EAFNOSUPPORT;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700797}
798
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200799void xfrm_local_error(struct sk_buff *skb, int mtu)
800{
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200801 unsigned int proto;
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200802 struct xfrm_state_afinfo *afinfo;
803
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200804 if (skb->protocol == htons(ETH_P_IP))
805 proto = AF_INET;
Xin Longf6a23d82020-05-26 17:41:46 +0800806 else if (skb->protocol == htons(ETH_P_IPV6) &&
807 skb->sk->sk_family == AF_INET6)
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200808 proto = AF_INET6;
809 else
810 return;
811
812 afinfo = xfrm_state_get_afinfo(proto);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900813 if (afinfo) {
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100814 afinfo->local_error(skb, mtu);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900815 rcu_read_unlock();
816 }
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200817}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200818EXPORT_SYMBOL_GPL(xfrm_local_error);