blob: 94b8702603bc54f36542977ed0d9c3b93d43b6b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
3 * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 */
10
Herbert Xu09b8f7a2006-06-22 03:08:03 -070011#include <linux/if_ether.h>
12#include <linux/kernel.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
Patrick McHardy16a66772006-01-06 23:01:48 -080015#include <linux/netfilter_ipv4.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080016#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/ip.h>
18#include <net/xfrm.h>
19#include <net/icmp.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static int xfrm4_tunnel_check_size(struct sk_buff *skb)
22{
23 int mtu, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
26 goto out;
27
WANG Cong60ff7462014-05-04 16:39:18 -070028 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 goto out;
30
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +020031 mtu = dst_mtu(skb_dst(skb));
Steffen Klassertd77e38e2017-04-14 10:06:10 +020032 if ((!skb_is_gso(skb) && skb->len > mtu) ||
33 (skb_is_gso(skb) && skb_gso_network_seglen(skb) > ip_skb_dst_mtu(skb->sk, skb))) {
Steffen Klassertca064bd2015-10-19 10:30:05 +020034 skb->protocol = htons(ETH_P_IP);
35
Steffen Klassertb00897b2011-06-29 23:20:41 +000036 if (skb->sk)
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020037 xfrm_local_error(skb, mtu);
Steffen Klassertb00897b2011-06-29 23:20:41 +000038 else
39 icmp_send(skb, ICMP_DEST_UNREACH,
40 ICMP_FRAG_NEEDED, htonl(mtu));
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 ret = -EMSGSIZE;
42 }
43out:
44 return ret;
45}
46
Herbert Xu36cf9ac2007-11-13 21:40:52 -080047int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int err;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050
Herbert Xu36cf9ac2007-11-13 21:40:52 -080051 err = xfrm4_tunnel_check_size(skb);
52 if (err)
53 return err;
54
Herbert Xu60d5fcf2007-11-19 18:47:58 -080055 XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
56
Herbert Xu36cf9ac2007-11-13 21:40:52 -080057 return xfrm4_extract_header(skb);
58}
59
60int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
61{
62 int err;
63
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -070064 err = xfrm_inner_extract_output(x, skb);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080065 if (err)
66 return err;
67
Steffen Klassert55967322014-04-07 08:08:52 +020068 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
Steffen Klassert044a8322015-01-12 13:38:49 +010069 skb->protocol = htons(ETH_P_IP);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080070
71 return x->outer_mode->output2(x, skb);
72}
73EXPORT_SYMBOL(xfrm4_prepare_output);
74
David Miller7026b1d2015-04-05 22:19:04 -040075int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb)
Herbert Xu09b8f7a2006-06-22 03:08:03 -070076{
Steffen Klassert55967322014-04-07 08:08:52 +020077 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Herbert Xu862b82c2007-11-13 21:43:11 -080078
Steffen Klassert55967322014-04-07 08:08:52 +020079#ifdef CONFIG_NETFILTER
Herbert Xu862b82c2007-11-13 21:43:11 -080080 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
Herbert Xu09b8f7a2006-06-22 03:08:03 -070081#endif
82
David Miller7026b1d2015-04-05 22:19:04 -040083 return xfrm_output(sk, skb);
Herbert Xu09b8f7a2006-06-22 03:08:03 -070084}
85
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050086static int __xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klassert55967322014-04-07 08:08:52 +020087{
88 struct xfrm_state *x = skb_dst(skb)->xfrm;
89
90#ifdef CONFIG_NETFILTER
91 if (!x) {
92 IPCB(skb)->flags |= IPSKB_REROUTED;
Eric W. Biederman13206b62015-10-07 16:48:35 -050093 return dst_output(net, sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +020094 }
95#endif
96
David Miller7026b1d2015-04-05 22:19:04 -040097 return x->outer_mode->afinfo->output_finish(sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +020098}
99
Eric W. Biedermanede20592015-10-07 16:48:47 -0500100int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -0800101{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500102 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
103 net, sk, skb, NULL, skb_dst(skb)->dev,
104 __xfrm4_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800105 !(IPCB(skb)->flags & IPSKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -0800106}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200107
108void xfrm4_local_error(struct sk_buff *skb, u32 mtu)
109{
110 struct iphdr *hdr;
111
112 hdr = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
113 ip_local_error(skb->sk, EMSGSIZE, hdr->daddr,
114 inet_sk(skb->sk)->inet_dport, mtu);
115}