xref: /linux/net/ipv6/xfrm6_output.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * xfrm6_output.c - Common IPsec encapsulation code for IPv6.
3  * Copyright (C) 2002 USAGI/WIDE Project
4  * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/if_ether.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
16 #include <linux/icmpv6.h>
17 #include <linux/netfilter_ipv6.h>
18 #include <net/dst.h>
19 #include <net/ipv6.h>
20 #include <net/ip6_route.h>
21 #include <net/xfrm.h>
22 
23 int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
24 			  u8 **prevhdr)
25 {
26 	return ip6_find_1stfragopt(skb, prevhdr);
27 }
28 EXPORT_SYMBOL(xfrm6_find_1stfragopt);
29 
30 static int xfrm6_local_dontfrag(struct sk_buff *skb)
31 {
32 	int proto;
33 	struct sock *sk = skb->sk;
34 
35 	if (sk) {
36 		if (sk->sk_family != AF_INET6)
37 			return 0;
38 
39 		proto = sk->sk_protocol;
40 		if (proto == IPPROTO_UDP || proto == IPPROTO_RAW)
41 			return inet6_sk(sk)->dontfrag;
42 	}
43 
44 	return 0;
45 }
46 
47 static void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
48 {
49 	struct flowi6 fl6;
50 	struct sock *sk = skb->sk;
51 
52 	fl6.flowi6_oif = sk->sk_bound_dev_if;
53 	fl6.daddr = ipv6_hdr(skb)->daddr;
54 
55 	ipv6_local_rxpmtu(sk, &fl6, mtu);
56 }
57 
58 void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
59 {
60 	struct flowi6 fl6;
61 	const struct ipv6hdr *hdr;
62 	struct sock *sk = skb->sk;
63 
64 	hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
65 	fl6.fl6_dport = inet_sk(sk)->inet_dport;
66 	fl6.daddr = hdr->daddr;
67 
68 	ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
69 }
70 
71 static int xfrm6_tunnel_check_size(struct sk_buff *skb)
72 {
73 	int mtu, ret = 0;
74 	struct dst_entry *dst = skb_dst(skb);
75 
76 	mtu = dst_mtu(dst);
77 	if (mtu < IPV6_MIN_MTU)
78 		mtu = IPV6_MIN_MTU;
79 
80 	if (!skb->ignore_df && skb->len > mtu) {
81 		skb->dev = dst->dev;
82 		skb->protocol = htons(ETH_P_IPV6);
83 
84 		if (xfrm6_local_dontfrag(skb))
85 			xfrm6_local_rxpmtu(skb, mtu);
86 		else if (skb->sk)
87 			xfrm_local_error(skb, mtu);
88 		else
89 			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
90 		ret = -EMSGSIZE;
91 	}
92 
93 	return ret;
94 }
95 
96 int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
97 {
98 	int err;
99 
100 	err = xfrm6_tunnel_check_size(skb);
101 	if (err)
102 		return err;
103 
104 	XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
105 
106 	return xfrm6_extract_header(skb);
107 }
108 
109 int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
110 {
111 	int err;
112 
113 	err = xfrm_inner_extract_output(x, skb);
114 	if (err)
115 		return err;
116 
117 	skb->ignore_df = 1;
118 	skb->protocol = htons(ETH_P_IPV6);
119 
120 	return x->outer_mode->output2(x, skb);
121 }
122 EXPORT_SYMBOL(xfrm6_prepare_output);
123 
124 int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb)
125 {
126 	memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
127 
128 #ifdef CONFIG_NETFILTER
129 	IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
130 #endif
131 
132 	return xfrm_output(sk, skb);
133 }
134 
135 static int __xfrm6_output(struct sock *sk, struct sk_buff *skb)
136 {
137 	struct dst_entry *dst = skb_dst(skb);
138 	struct xfrm_state *x = dst->xfrm;
139 	int mtu;
140 	bool toobig;
141 
142 #ifdef CONFIG_NETFILTER
143 	if (!x) {
144 		IP6CB(skb)->flags |= IP6SKB_REROUTED;
145 		return dst_output_sk(sk, skb);
146 	}
147 #endif
148 
149 	if (x->props.mode != XFRM_MODE_TUNNEL)
150 		goto skip_frag;
151 
152 	if (skb->protocol == htons(ETH_P_IPV6))
153 		mtu = ip6_skb_dst_mtu(skb);
154 	else
155 		mtu = dst_mtu(skb_dst(skb));
156 
157 	toobig = skb->len > mtu && !skb_is_gso(skb);
158 
159 	if (toobig && xfrm6_local_dontfrag(skb)) {
160 		xfrm6_local_rxpmtu(skb, mtu);
161 		return -EMSGSIZE;
162 	} else if (!skb->ignore_df && toobig && skb->sk) {
163 		xfrm_local_error(skb, mtu);
164 		return -EMSGSIZE;
165 	}
166 
167 	if (toobig || dst_allfrag(skb_dst(skb)))
168 		return ip6_fragment(sk, skb,
169 				    x->outer_mode->afinfo->output_finish);
170 
171 skip_frag:
172 	return x->outer_mode->afinfo->output_finish(sk, skb);
173 }
174 
175 int xfrm6_output(struct sock *sk, struct sk_buff *skb)
176 {
177 	return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, sk, skb,
178 			    NULL, skb_dst(skb)->dev, __xfrm6_output,
179 			    !(IP6CB(skb)->flags & IP6SKB_REROUTED));
180 }
181