blob: ecff3fce9807365123a07f78322ae3d28340514a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
4 * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Herbert Xu09b8f7a2006-06-22 03:08:03 -07007#include <linux/if_ether.h>
8#include <linux/kernel.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -08009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/skbuff.h>
Patrick McHardy16a66772006-01-06 23:01:48 -080011#include <linux/netfilter_ipv4.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080012#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <net/ip.h>
14#include <net/xfrm.h>
15#include <net/icmp.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017static int xfrm4_tunnel_check_size(struct sk_buff *skb)
18{
19 int mtu, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
22 goto out;
23
WANG Cong60ff7462014-05-04 16:39:18 -070024 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 goto out;
26
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +020027 mtu = dst_mtu(skb_dst(skb));
Steffen Klassertd77e38e2017-04-14 10:06:10 +020028 if ((!skb_is_gso(skb) && skb->len > mtu) ||
Daniel Axtens80f59742018-03-01 17:13:39 +110029 (skb_is_gso(skb) &&
30 !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) {
Steffen Klassertca064bd2015-10-19 10:30:05 +020031 skb->protocol = htons(ETH_P_IP);
32
Steffen Klassertb00897b82011-06-29 23:20:41 +000033 if (skb->sk)
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020034 xfrm_local_error(skb, mtu);
Steffen Klassertb00897b82011-06-29 23:20:41 +000035 else
36 icmp_send(skb, ICMP_DEST_UNREACH,
37 ICMP_FRAG_NEEDED, htonl(mtu));
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 ret = -EMSGSIZE;
39 }
40out:
41 return ret;
42}
43
Herbert Xu36cf9ac2007-11-13 21:40:52 -080044int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 int err;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090047
Herbert Xu36cf9ac2007-11-13 21:40:52 -080048 err = xfrm4_tunnel_check_size(skb);
49 if (err)
50 return err;
51
Herbert Xu60d5fcf2007-11-19 18:47:58 -080052 XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
53
Herbert Xu36cf9ac2007-11-13 21:40:52 -080054 return xfrm4_extract_header(skb);
55}
56
David Miller7026b1d2015-04-05 22:19:04 -040057int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb)
Herbert Xu09b8f7a2006-06-22 03:08:03 -070058{
Steffen Klassert55967322014-04-07 08:08:52 +020059 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Herbert Xu862b82c2007-11-13 21:43:11 -080060
Steffen Klassert55967322014-04-07 08:08:52 +020061#ifdef CONFIG_NETFILTER
Herbert Xu862b82c2007-11-13 21:43:11 -080062 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
Herbert Xu09b8f7a2006-06-22 03:08:03 -070063#endif
64
David Miller7026b1d2015-04-05 22:19:04 -040065 return xfrm_output(sk, skb);
Herbert Xu09b8f7a2006-06-22 03:08:03 -070066}
67
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050068static int __xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klassert55967322014-04-07 08:08:52 +020069{
70 struct xfrm_state *x = skb_dst(skb)->xfrm;
Florian Westphal733a5fa2019-03-29 21:16:30 +010071 const struct xfrm_state_afinfo *afinfo;
72 int ret = -EAFNOSUPPORT;
Steffen Klassert55967322014-04-07 08:08:52 +020073
74#ifdef CONFIG_NETFILTER
75 if (!x) {
76 IPCB(skb)->flags |= IPSKB_REROUTED;
Eric W. Biederman13206b62015-10-07 16:48:35 -050077 return dst_output(net, sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +020078 }
79#endif
80
Florian Westphal733a5fa2019-03-29 21:16:30 +010081 rcu_read_lock();
Florian Westphalc9500d72019-03-29 21:16:32 +010082 afinfo = xfrm_state_afinfo_get_rcu(x->outer_mode.family);
Florian Westphal733a5fa2019-03-29 21:16:30 +010083 if (likely(afinfo))
84 ret = afinfo->output_finish(sk, skb);
85 else
86 kfree_skb(skb);
87 rcu_read_unlock();
88
89 return ret;
Steffen Klassert55967322014-04-07 08:08:52 +020090}
91
Eric W. Biedermanede20592015-10-07 16:48:47 -050092int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -080093{
Eric W. Biederman29a26a52015-09-15 20:04:16 -050094 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
95 net, sk, skb, NULL, skb_dst(skb)->dev,
96 __xfrm4_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -080097 !(IPCB(skb)->flags & IPSKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -080098}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020099
100void xfrm4_local_error(struct sk_buff *skb, u32 mtu)
101{
102 struct iphdr *hdr;
103
104 hdr = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
105 ip_local_error(skb->sk, EMSGSIZE, hdr->daddr,
106 inet_sk(skb->sk)->inet_dport, mtu);
107}