blob: 6b44b6e738f746ebe7155a3e4de646dbe1f23b45 [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
Florian Westphal9acf4d32021-06-11 12:50:10 +020080static int xfrm6_hdr_offset(struct xfrm_state *x, struct sk_buff *skb, u8 **prevhdr)
81{
82 return x->type->hdr_offset(x, skb, prevhdr);
83}
84
Florian Westphal0c620e92019-03-29 21:16:25 +010085/* Add encapsulation header.
86 *
87 * The IP header and mutable extension headers will be moved forward to make
88 * space for the encapsulation header.
89 */
90static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
91{
Florian Westphal4c145dc2019-03-29 21:16:31 +010092#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal0c620e92019-03-29 21:16:25 +010093 struct ipv6hdr *iph;
94 u8 *prevhdr;
95 int hdr_len;
96
97 iph = ipv6_hdr(skb);
98 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
99
Florian Westphal9acf4d32021-06-11 12:50:10 +0200100 hdr_len = xfrm6_hdr_offset(x, skb, &prevhdr);
Florian Westphal0c620e92019-03-29 21:16:25 +0100101 if (hdr_len < 0)
102 return hdr_len;
103 skb_set_mac_header(skb,
104 (prevhdr - x->props.header_len) - skb->data);
105 skb_set_network_header(skb, -x->props.header_len);
106 skb->transport_header = skb->network_header + hdr_len;
107 __skb_pull(skb, hdr_len);
108 memmove(ipv6_hdr(skb), iph, hdr_len);
109 return 0;
110#else
111 WARN_ON_ONCE(1);
Florian Westphal4c145dc2019-03-29 21:16:31 +0100112 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100113#endif
114}
115
116/* Add route optimization header space.
117 *
118 * The IP header and mutable extension headers will be moved forward to make
119 * space for the route optimization header.
120 */
121static int xfrm6_ro_output(struct xfrm_state *x, struct sk_buff *skb)
122{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100123#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100124 struct ipv6hdr *iph;
125 u8 *prevhdr;
126 int hdr_len;
127
128 iph = ipv6_hdr(skb);
129
Florian Westphal9acf4d32021-06-11 12:50:10 +0200130 hdr_len = xfrm6_hdr_offset(x, skb, &prevhdr);
Florian Westphal0c620e92019-03-29 21:16:25 +0100131 if (hdr_len < 0)
132 return hdr_len;
133 skb_set_mac_header(skb,
134 (prevhdr - x->props.header_len) - skb->data);
135 skb_set_network_header(skb, -x->props.header_len);
136 skb->transport_header = skb->network_header + hdr_len;
137 __skb_pull(skb, hdr_len);
138 memmove(ipv6_hdr(skb), iph, hdr_len);
139
140 x->lastused = ktime_get_real_seconds();
141
142 return 0;
143#else
144 WARN_ON_ONCE(1);
Florian Westphal4c145dc2019-03-29 21:16:31 +0100145 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100146#endif
147}
148
Florian Westphal1de70832019-03-29 21:16:29 +0100149/* Add encapsulation header.
150 *
151 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
152 */
153static int xfrm4_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb)
154{
155 struct ip_beet_phdr *ph;
156 struct iphdr *top_iph;
157 int hdrlen, optlen;
158
159 hdrlen = 0;
160 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
161 if (unlikely(optlen))
162 hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
163
164 skb_set_network_header(skb, -x->props.header_len - hdrlen +
165 (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph)));
166 if (x->sel.family != AF_INET6)
167 skb->network_header += IPV4_BEET_PHMAXLEN;
168 skb->mac_header = skb->network_header +
169 offsetof(struct iphdr, protocol);
170 skb->transport_header = skb->network_header + sizeof(*top_iph);
171
172 xfrm4_beet_make_header(skb);
173
174 ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen);
175
176 top_iph = ip_hdr(skb);
177
178 if (unlikely(optlen)) {
179 if (WARN_ON(optlen < 0))
180 return -EINVAL;
181
182 ph->padlen = 4 - (optlen & 4);
183 ph->hdrlen = optlen / 8;
184 ph->nexthdr = top_iph->protocol;
185 if (ph->padlen)
186 memset(ph + 1, IPOPT_NOP, ph->padlen);
187
188 top_iph->protocol = IPPROTO_BEETPH;
189 top_iph->ihl = sizeof(struct iphdr) / 4;
190 }
191
192 top_iph->saddr = x->props.saddr.a4;
193 top_iph->daddr = x->id.daddr.a4;
194
195 return 0;
196}
197
198/* Add encapsulation header.
199 *
200 * The top IP header will be constructed per RFC 2401.
201 */
202static int xfrm4_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb)
203{
204 struct dst_entry *dst = skb_dst(skb);
205 struct iphdr *top_iph;
206 int flags;
207
208 skb_set_inner_network_header(skb, skb_network_offset(skb));
209 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
210
211 skb_set_network_header(skb, -x->props.header_len);
212 skb->mac_header = skb->network_header +
213 offsetof(struct iphdr, protocol);
214 skb->transport_header = skb->network_header + sizeof(*top_iph);
215 top_iph = ip_hdr(skb);
216
217 top_iph->ihl = 5;
218 top_iph->version = 4;
219
220 top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family);
221
222 /* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */
223 if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
224 top_iph->tos = 0;
225 else
226 top_iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
227 top_iph->tos = INET_ECN_encapsulate(top_iph->tos,
228 XFRM_MODE_SKB_CB(skb)->tos);
229
230 flags = x->props.flags;
231 if (flags & XFRM_STATE_NOECN)
232 IP_ECN_clear(top_iph);
233
234 top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
235 0 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF));
236
237 top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst));
238
239 top_iph->saddr = x->props.saddr.a4;
240 top_iph->daddr = x->id.daddr.a4;
241 ip_select_ident(dev_net(dst->dev), skb, NULL);
242
243 return 0;
244}
245
246#if IS_ENABLED(CONFIG_IPV6)
247static int xfrm6_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb)
248{
249 struct dst_entry *dst = skb_dst(skb);
250 struct ipv6hdr *top_iph;
251 int dsfield;
252
253 skb_set_inner_network_header(skb, skb_network_offset(skb));
254 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
255
256 skb_set_network_header(skb, -x->props.header_len);
257 skb->mac_header = skb->network_header +
258 offsetof(struct ipv6hdr, nexthdr);
259 skb->transport_header = skb->network_header + sizeof(*top_iph);
260 top_iph = ipv6_hdr(skb);
261
262 top_iph->version = 6;
263
264 memcpy(top_iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl,
265 sizeof(top_iph->flow_lbl));
266 top_iph->nexthdr = xfrm_af2proto(skb_dst(skb)->ops->family);
267
268 if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
269 dsfield = 0;
270 else
271 dsfield = XFRM_MODE_SKB_CB(skb)->tos;
272 dsfield = INET_ECN_encapsulate(dsfield, XFRM_MODE_SKB_CB(skb)->tos);
273 if (x->props.flags & XFRM_STATE_NOECN)
274 dsfield &= ~INET_ECN_MASK;
275 ipv6_change_dsfield(top_iph, 0, dsfield);
276 top_iph->hop_limit = ip6_dst_hoplimit(xfrm_dst_child(dst));
277 top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
278 top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
279 return 0;
280}
281
282static int xfrm6_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb)
283{
284 struct ipv6hdr *top_iph;
285 struct ip_beet_phdr *ph;
286 int optlen, hdr_len;
287
288 hdr_len = 0;
289 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
290 if (unlikely(optlen))
291 hdr_len += IPV4_BEET_PHMAXLEN - (optlen & 4);
292
293 skb_set_network_header(skb, -x->props.header_len - hdr_len);
294 if (x->sel.family != AF_INET6)
295 skb->network_header += IPV4_BEET_PHMAXLEN;
296 skb->mac_header = skb->network_header +
297 offsetof(struct ipv6hdr, nexthdr);
298 skb->transport_header = skb->network_header + sizeof(*top_iph);
299 ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdr_len);
300
301 xfrm6_beet_make_header(skb);
302
303 top_iph = ipv6_hdr(skb);
304 if (unlikely(optlen)) {
305 if (WARN_ON(optlen < 0))
306 return -EINVAL;
307
308 ph->padlen = 4 - (optlen & 4);
309 ph->hdrlen = optlen / 8;
310 ph->nexthdr = top_iph->nexthdr;
311 if (ph->padlen)
312 memset(ph + 1, IPOPT_NOP, ph->padlen);
313
314 top_iph->nexthdr = IPPROTO_BEETPH;
315 }
316
317 top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
318 top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
319 return 0;
320}
321#endif
322
323/* Add encapsulation header.
324 *
325 * On exit, the transport header will be set to the start of the
326 * encapsulation header to be filled in by x->type->output and the mac
327 * header will be set to the nextheader (protocol for IPv4) field of the
328 * extension header directly preceding the encapsulation header, or in
329 * its absence, that of the top IP header.
330 * The value of the network header will always point to the top IP header
331 * while skb->data will point to the payload.
332 */
Florian Westphal0c620e92019-03-29 21:16:25 +0100333static int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
334{
335 int err;
336
337 err = xfrm_inner_extract_output(x, skb);
338 if (err)
339 return err;
340
341 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
342 skb->protocol = htons(ETH_P_IP);
343
Florian Westphalc9500d72019-03-29 21:16:32 +0100344 switch (x->outer_mode.encap) {
Florian Westphal1de70832019-03-29 21:16:29 +0100345 case XFRM_MODE_BEET:
346 return xfrm4_beet_encap_add(x, skb);
347 case XFRM_MODE_TUNNEL:
348 return xfrm4_tunnel_encap_add(x, skb);
349 }
350
351 WARN_ON_ONCE(1);
352 return -EOPNOTSUPP;
Florian Westphal0c620e92019-03-29 21:16:25 +0100353}
354
355static int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
356{
357#if IS_ENABLED(CONFIG_IPV6)
358 int err;
359
360 err = xfrm_inner_extract_output(x, skb);
361 if (err)
362 return err;
363
364 skb->ignore_df = 1;
365 skb->protocol = htons(ETH_P_IPV6);
366
Florian Westphalc9500d72019-03-29 21:16:32 +0100367 switch (x->outer_mode.encap) {
Florian Westphal1de70832019-03-29 21:16:29 +0100368 case XFRM_MODE_BEET:
369 return xfrm6_beet_encap_add(x, skb);
370 case XFRM_MODE_TUNNEL:
371 return xfrm6_tunnel_encap_add(x, skb);
372 default:
373 WARN_ON_ONCE(1);
374 return -EOPNOTSUPP;
375 }
Florian Westphal0c620e92019-03-29 21:16:25 +0100376#endif
Florian Westphal1de70832019-03-29 21:16:29 +0100377 WARN_ON_ONCE(1);
378 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100379}
380
381static int xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
382{
Florian Westphalc9500d72019-03-29 21:16:32 +0100383 switch (x->outer_mode.encap) {
Florian Westphal0c620e92019-03-29 21:16:25 +0100384 case XFRM_MODE_BEET:
385 case XFRM_MODE_TUNNEL:
Florian Westphalc9500d72019-03-29 21:16:32 +0100386 if (x->outer_mode.family == AF_INET)
Florian Westphal0c620e92019-03-29 21:16:25 +0100387 return xfrm4_prepare_output(x, skb);
Florian Westphalc9500d72019-03-29 21:16:32 +0100388 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100389 return xfrm6_prepare_output(x, skb);
390 break;
391 case XFRM_MODE_TRANSPORT:
Florian Westphalc9500d72019-03-29 21:16:32 +0100392 if (x->outer_mode.family == AF_INET)
Florian Westphal0c620e92019-03-29 21:16:25 +0100393 return xfrm4_transport_output(x, skb);
Florian Westphalc9500d72019-03-29 21:16:32 +0100394 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100395 return xfrm6_transport_output(x, skb);
396 break;
397 case XFRM_MODE_ROUTEOPTIMIZATION:
Florian Westphalc9500d72019-03-29 21:16:32 +0100398 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100399 return xfrm6_ro_output(x, skb);
400 WARN_ON_ONCE(1);
401 break;
402 default:
403 WARN_ON_ONCE(1);
404 break;
405 }
406
407 return -EOPNOTSUPP;
408}
409
410#if IS_ENABLED(CONFIG_NET_PKTGEN)
411int pktgen_xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
412{
413 return xfrm_outer_mode_output(x, skb);
414}
415EXPORT_SYMBOL_GPL(pktgen_xfrm_outer_mode_output);
416#endif
417
Herbert Xuc6581a42007-11-13 21:43:43 -0800418static int xfrm_output_one(struct sk_buff *skb, int err)
Herbert Xu406ef772007-10-08 17:16:30 -0700419{
Eric Dumazetadf30902009-06-02 05:19:30 +0000420 struct dst_entry *dst = skb_dst(skb);
Herbert Xu406ef772007-10-08 17:16:30 -0700421 struct xfrm_state *x = dst->xfrm;
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800422 struct net *net = xs_net(x);
Herbert Xu406ef772007-10-08 17:16:30 -0700423
Herbert Xuc6581a42007-11-13 21:43:43 -0800424 if (err <= 0)
425 goto resume;
Herbert Xu406ef772007-10-08 17:16:30 -0700426
427 do {
Steffen Klassert26b20722012-03-21 23:32:39 +0000428 err = xfrm_skb_check_space(skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800429 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800430 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu910ef702007-12-18 22:14:25 -0800431 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800432 }
Herbert Xu910ef702007-12-18 22:14:25 -0800433
Steffen Klassert9b42c1f2018-06-12 12:44:26 +0200434 skb->mark = xfrm_smark_get(skb->mark, x);
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900435
Florian Westphal0c620e92019-03-29 21:16:25 +0100436 err = xfrm_outer_mode_output(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800437 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800438 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Herbert Xu910ef702007-12-18 22:14:25 -0800439 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800440 }
Herbert Xua2deb6d22007-11-13 21:39:38 -0800441
Herbert Xu406ef772007-10-08 17:16:30 -0700442 spin_lock_bh(&x->lock);
Li RongQingbb65a9c2012-12-28 16:06:28 +0800443
444 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
445 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
Timo Teräs497574c2013-05-22 01:40:47 +0000446 err = -EINVAL;
Li RongQingfa8599d2013-02-01 13:17:16 +0800447 goto error;
Li RongQingbb65a9c2012-12-28 16:06:28 +0800448 }
449
Herbert Xu910ef702007-12-18 22:14:25 -0800450 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800451 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800452 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -0700453 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800454 }
Herbert Xu406ef772007-10-08 17:16:30 -0700455
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000456 err = x->repl->overflow(x, skb);
457 if (err) {
458 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
459 goto error;
Herbert Xu436a0a42007-10-08 17:25:53 -0700460 }
461
Herbert Xu406ef772007-10-08 17:16:30 -0700462 x->curlft.bytes += skb->len;
463 x->curlft.packets++;
464
Herbert Xu406ef772007-10-08 17:16:30 -0700465 spin_unlock_bh(&x->lock);
466
Steffen Klassert3bc07322011-03-15 21:08:28 +0000467 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +0200468 if (!skb_dst(skb)) {
469 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Wei Yongjun533555e2018-10-27 06:12:06 +0000470 err = -EHOSTUNREACH;
Steffen Klassert9e143792018-09-11 10:31:15 +0200471 goto error_nolock;
472 }
Steffen Klassert3bc07322011-03-15 21:08:28 +0000473
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200474 if (xfrm_offload(skb)) {
475 x->type_offload->encap(x, skb);
476 } else {
Steffen Klassert73b9fc42017-10-30 10:04:04 +0100477 /* Inner headers are invalid now. */
478 skb->encapsulation = 0;
479
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200480 err = x->type->output(x, skb);
481 if (err == -EINPROGRESS)
482 goto out;
483 }
Herbert Xuc6581a42007-11-13 21:43:43 -0800484
485resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800486 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800487 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c6538c2007-10-09 13:33:35 -0700488 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800489 }
Herbert Xub7c6538c2007-10-09 13:33:35 -0700490
Steffen Klassert8764ab22010-06-04 01:57:38 +0000491 dst = skb_dst_pop(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000492 if (!dst) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800493 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -0700494 err = -EHOSTUNREACH;
495 goto error_nolock;
496 }
Steffen Klasserte4334302011-03-15 21:09:32 +0000497 skb_dst_set(skb, dst);
Herbert Xu406ef772007-10-08 17:16:30 -0700498 x = dst->xfrm;
Florian Westphalc9500d72019-03-29 21:16:32 +0100499 } while (x && !(x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700500
Jean Sacrenebd46872013-06-01 16:23:15 +0000501 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700502
Herbert Xu406ef772007-10-08 17:16:30 -0700503error:
504 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800505error_nolock:
506 kfree_skb(skb);
Jean Sacrenebd46872013-06-01 16:23:15 +0000507out:
508 return err;
Herbert Xu862b82c2007-11-13 21:43:11 -0800509}
510
Evan Nimmo9ab12652021-03-02 08:00:04 +1300511int xfrm_output_resume(struct sock *sk, struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800512{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500513 struct net *net = xs_net(skb_dst(skb)->xfrm);
Eric W. Biedermanbe10de02015-09-17 17:21:31 -0500514
Herbert Xuc6581a42007-11-13 21:43:43 -0800515 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Florian Westphal895b5c92019-09-29 20:54:03 +0200516 nf_reset_ct(skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800517
Evan Nimmo9ab12652021-03-02 08:00:04 +1300518 err = skb_dst(skb)->ops->local_out(net, sk, skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800519 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800520 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800521
Eric Dumazetadf30902009-06-02 05:19:30 +0000522 if (!skb_dst(skb)->xfrm)
Evan Nimmo9ab12652021-03-02 08:00:04 +1300523 return dst_output(net, sk, skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800524
Eric Dumazetadf30902009-06-02 05:19:30 +0000525 err = nf_hook(skb_dst(skb)->ops->family,
Evan Nimmo9ab12652021-03-02 08:00:04 +1300526 NF_INET_POST_ROUTING, net, sk, skb,
Eric Dumazetadf30902009-06-02 05:19:30 +0000527 NULL, skb_dst(skb)->dev, xfrm_output2);
Herbert Xu862b82c2007-11-13 21:43:11 -0800528 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800529 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800530 }
531
Herbert Xuc6581a42007-11-13 21:43:43 -0800532 if (err == -EINPROGRESS)
533 err = 0;
534
535out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800536 return err;
537}
Herbert Xuc6581a42007-11-13 21:43:43 -0800538EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800539
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500540static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800541{
Evan Nimmo9ab12652021-03-02 08:00:04 +1300542 return xfrm_output_resume(sk, skb, 1);
Herbert Xuc6581a42007-11-13 21:43:43 -0800543}
544
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500545static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800546{
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500547 struct sk_buff *segs, *nskb;
Herbert Xu862b82c2007-11-13 21:43:11 -0800548
Cambda Zhua08e7fd2020-03-26 15:33:14 +0800549 BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET);
550 BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_GSO_CB_OFFSET);
Herbert Xu862b82c2007-11-13 21:43:11 -0800551 segs = skb_gso_segment(skb, 0);
552 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700553 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800554 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200555 if (segs == NULL)
556 return -EINVAL;
Herbert Xu862b82c2007-11-13 21:43:11 -0800557
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500558 skb_list_walk_safe(segs, segs, nskb) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800559 int err;
560
David S. Millera8305bf2018-07-29 20:42:53 -0700561 skb_mark_not_on_list(segs);
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500562 err = xfrm_output2(net, sk, segs);
Herbert Xu862b82c2007-11-13 21:43:11 -0800563
564 if (unlikely(err)) {
Florian Westphal46cfd722014-09-10 01:08:46 +0200565 kfree_skb_list(nskb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800566 return err;
567 }
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500568 }
Herbert Xu862b82c2007-11-13 21:43:11 -0800569
570 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700571}
Herbert Xuc6581a42007-11-13 21:43:43 -0800572
David Miller7026b1d2015-04-05 22:19:04 -0400573int xfrm_output(struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800574{
Eric Dumazetadf30902009-06-02 05:19:30 +0000575 struct net *net = dev_net(skb_dst(skb)->dev);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200576 struct xfrm_state *x = skb_dst(skb)->xfrm;
Herbert Xuc6581a42007-11-13 21:43:43 -0800577 int err;
578
Florian Westphal2ab60962020-05-04 10:06:09 +0200579 switch (x->outer_mode.family) {
580 case AF_INET:
581 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Florian Westphal2ab60962020-05-04 10:06:09 +0200582 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
Florian Westphal2ab60962020-05-04 10:06:09 +0200583 break;
584 case AF_INET6:
585 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
586
Florian Westphal2ab60962020-05-04 10:06:09 +0200587 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
Florian Westphal2ab60962020-05-04 10:06:09 +0200588 break;
589 }
590
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200591 secpath_reset(skb);
592
593 if (xfrm_dev_offload_ok(skb, x)) {
594 struct sec_path *sp;
595
Florian Westphala84e3f52018-12-18 17:15:26 +0100596 sp = secpath_set(skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200597 if (!sp) {
598 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
599 kfree_skb(skb);
600 return -ENOMEM;
601 }
Ilan Tayarif1bd7d62017-04-14 10:07:39 +0200602 skb->encapsulation = 1;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200603
604 sp->olen++;
Florian Westphala84e3f52018-12-18 17:15:26 +0100605 sp->xvec[sp->len++] = x;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200606 xfrm_state_hold(x);
607
608 if (skb_is_gso(skb)) {
Xin Longa204aef2020-04-20 21:51:09 +0800609 if (skb->inner_protocol)
610 return xfrm_output_gso(net, sk, skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200611
Xin Longa204aef2020-04-20 21:51:09 +0800612 skb_shinfo(skb)->gso_type |= SKB_GSO_ESP;
613 goto out;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200614 }
615
616 if (x->xso.dev && x->xso.dev->features & NETIF_F_HW_ESP_TX_CSUM)
617 goto out;
Xin Longa204aef2020-04-20 21:51:09 +0800618 } else {
619 if (skb_is_gso(skb))
620 return xfrm_output_gso(net, sk, skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200621 }
622
Herbert Xuc6581a42007-11-13 21:43:43 -0800623 if (skb->ip_summed == CHECKSUM_PARTIAL) {
624 err = skb_checksum_help(skb);
625 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800626 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800627 kfree_skb(skb);
628 return err;
629 }
630 }
631
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200632out:
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500633 return xfrm_output2(net, sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800634}
Fabian Frederickfc680862014-05-12 19:54:34 +0200635EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700636
Florian Westphal6d64be32020-05-04 10:06:03 +0200637static int xfrm4_tunnel_check_size(struct sk_buff *skb)
638{
639 int mtu, ret = 0;
640
641 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
642 goto out;
643
644 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
645 goto out;
646
647 mtu = dst_mtu(skb_dst(skb));
648 if ((!skb_is_gso(skb) && skb->len > mtu) ||
649 (skb_is_gso(skb) &&
650 !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) {
651 skb->protocol = htons(ETH_P_IP);
652
653 if (skb->sk)
654 xfrm_local_error(skb, mtu);
655 else
656 icmp_send(skb, ICMP_DEST_UNREACH,
657 ICMP_FRAG_NEEDED, htonl(mtu));
658 ret = -EMSGSIZE;
659 }
660out:
661 return ret;
662}
663
664static int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
665{
666 int err;
667
Xin Long68dc0222021-03-19 18:27:58 +0800668 if (x->outer_mode.encap == XFRM_MODE_BEET &&
669 ip_is_fragment(ip_hdr(skb))) {
670 net_warn_ratelimited("BEET mode doesn't support inner IPv4 fragments\n");
671 return -EAFNOSUPPORT;
672 }
673
Florian Westphal6d64be32020-05-04 10:06:03 +0200674 err = xfrm4_tunnel_check_size(skb);
675 if (err)
676 return err;
677
678 XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
679
680 xfrm4_extract_header(skb);
681 return 0;
682}
683
Florian Westphalf3075f482020-05-04 10:06:08 +0200684#if IS_ENABLED(CONFIG_IPV6)
685static int xfrm6_tunnel_check_size(struct sk_buff *skb)
686{
687 int mtu, ret = 0;
688 struct dst_entry *dst = skb_dst(skb);
689
690 if (skb->ignore_df)
691 goto out;
692
693 mtu = dst_mtu(dst);
694 if (mtu < IPV6_MIN_MTU)
695 mtu = IPV6_MIN_MTU;
696
697 if ((!skb_is_gso(skb) && skb->len > mtu) ||
698 (skb_is_gso(skb) &&
699 !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
700 skb->dev = dst->dev;
701 skb->protocol = htons(ETH_P_IPV6);
702
703 if (xfrm6_local_dontfrag(skb->sk))
704 ipv6_stub->xfrm6_local_rxpmtu(skb, mtu);
705 else if (skb->sk)
706 xfrm_local_error(skb, mtu);
707 else
708 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
709 ret = -EMSGSIZE;
710 }
711out:
712 return ret;
713}
714#endif
715
716static int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
717{
718#if IS_ENABLED(CONFIG_IPV6)
Xin Long68dc0222021-03-19 18:27:58 +0800719 unsigned int ptr = 0;
Florian Westphalf3075f482020-05-04 10:06:08 +0200720 int err;
721
Xin Long68dc0222021-03-19 18:27:58 +0800722 if (x->outer_mode.encap == XFRM_MODE_BEET &&
723 ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL, NULL) >= 0) {
724 net_warn_ratelimited("BEET mode doesn't support inner IPv6 fragments\n");
725 return -EAFNOSUPPORT;
726 }
727
Florian Westphalf3075f482020-05-04 10:06:08 +0200728 err = xfrm6_tunnel_check_size(skb);
729 if (err)
730 return err;
731
732 XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
733
734 xfrm6_extract_header(skb);
735 return 0;
736#else
737 WARN_ON_ONCE(1);
738 return -EAFNOSUPPORT;
739#endif
740}
741
Florian Westphal0c620e92019-03-29 21:16:25 +0100742static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700743{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100744 const struct xfrm_mode *inner_mode;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100745
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700746 if (x->sel.family == AF_UNSPEC)
747 inner_mode = xfrm_ip2inner_mode(x,
Eric Dumazetadf30902009-06-02 05:19:30 +0000748 xfrm_af2proto(skb_dst(skb)->ops->family));
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700749 else
Florian Westphalc9500d72019-03-29 21:16:32 +0100750 inner_mode = &x->inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700751
752 if (inner_mode == NULL)
753 return -EAFNOSUPPORT;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100754
Florian Westphal6d64be32020-05-04 10:06:03 +0200755 switch (inner_mode->family) {
756 case AF_INET:
757 return xfrm4_extract_output(x, skb);
Florian Westphalf3075f482020-05-04 10:06:08 +0200758 case AF_INET6:
759 return xfrm6_extract_output(x, skb);
Florian Westphal6d64be32020-05-04 10:06:03 +0200760 }
Florian Westphal733a5fa2019-03-29 21:16:30 +0100761
Florian Westphalf3075f482020-05-04 10:06:08 +0200762 return -EAFNOSUPPORT;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700763}
764
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200765void xfrm_local_error(struct sk_buff *skb, int mtu)
766{
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200767 unsigned int proto;
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200768 struct xfrm_state_afinfo *afinfo;
769
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200770 if (skb->protocol == htons(ETH_P_IP))
771 proto = AF_INET;
Xin Longf6a23d82020-05-26 17:41:46 +0800772 else if (skb->protocol == htons(ETH_P_IPV6) &&
773 skb->sk->sk_family == AF_INET6)
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200774 proto = AF_INET6;
775 else
776 return;
777
778 afinfo = xfrm_state_get_afinfo(proto);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900779 if (afinfo) {
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100780 afinfo->local_error(skb, mtu);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900781 rcu_read_unlock();
782 }
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200783}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200784EXPORT_SYMBOL_GPL(xfrm_local_error);