]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * xfrm6_output.c - Common IPsec encapsulation code for IPv6. | |
4 | * Copyright (C) 2002 USAGI/WIDE Project | |
5 | * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au> | |
1da177e4 LT |
6 | */ |
7 | ||
406ef77c | 8 | #include <linux/if_ether.h> |
36cf9acf HX |
9 | #include <linux/kernel.h> |
10 | #include <linux/module.h> | |
1da177e4 | 11 | #include <linux/skbuff.h> |
1da177e4 | 12 | #include <linux/icmpv6.h> |
16a6677f | 13 | #include <linux/netfilter_ipv6.h> |
36cf9acf | 14 | #include <net/dst.h> |
1da177e4 | 15 | #include <net/ipv6.h> |
ad0081e4 | 16 | #include <net/ip6_route.h> |
1da177e4 LT |
17 | #include <net/xfrm.h> |
18 | ||
aee5adb4 MN |
19 | int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb, |
20 | u8 **prevhdr) | |
21 | { | |
22 | return ip6_find_1stfragopt(skb, prevhdr); | |
23 | } | |
7159039a YH |
24 | EXPORT_SYMBOL(xfrm6_find_1stfragopt); |
25 | ||
dd767856 SK |
26 | static int xfrm6_local_dontfrag(struct sk_buff *skb) |
27 | { | |
28 | int proto; | |
29 | struct sock *sk = skb->sk; | |
30 | ||
31 | if (sk) { | |
628e341f HFS |
32 | if (sk->sk_family != AF_INET6) |
33 | return 0; | |
dd767856 | 34 | |
628e341f | 35 | proto = sk->sk_protocol; |
dd767856 SK |
36 | if (proto == IPPROTO_UDP || proto == IPPROTO_RAW) |
37 | return inet6_sk(sk)->dontfrag; | |
38 | } | |
39 | ||
40 | return 0; | |
41 | } | |
42 | ||
43 | static void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu) | |
44 | { | |
45 | struct flowi6 fl6; | |
46 | struct sock *sk = skb->sk; | |
47 | ||
48 | fl6.flowi6_oif = sk->sk_bound_dev_if; | |
4e3fd7a0 | 49 | fl6.daddr = ipv6_hdr(skb)->daddr; |
dd767856 SK |
50 | |
51 | ipv6_local_rxpmtu(sk, &fl6, mtu); | |
52 | } | |
53 | ||
628e341f | 54 | void xfrm6_local_error(struct sk_buff *skb, u32 mtu) |
dd767856 SK |
55 | { |
56 | struct flowi6 fl6; | |
5d0ff542 | 57 | const struct ipv6hdr *hdr; |
dd767856 SK |
58 | struct sock *sk = skb->sk; |
59 | ||
5d0ff542 | 60 | hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb); |
dd767856 | 61 | fl6.fl6_dport = inet_sk(sk)->inet_dport; |
5d0ff542 | 62 | fl6.daddr = hdr->daddr; |
dd767856 SK |
63 | |
64 | ipv6_local_error(sk, EMSGSIZE, &fl6, mtu); | |
65 | } | |
66 | ||
1da177e4 LT |
67 | static int xfrm6_tunnel_check_size(struct sk_buff *skb) |
68 | { | |
69 | int mtu, ret = 0; | |
adf30907 | 70 | struct dst_entry *dst = skb_dst(skb); |
1da177e4 | 71 | |
d77e38e6 SK |
72 | if (skb->ignore_df) |
73 | goto out; | |
74 | ||
1da177e4 LT |
75 | mtu = dst_mtu(dst); |
76 | if (mtu < IPV6_MIN_MTU) | |
77 | mtu = IPV6_MIN_MTU; | |
78 | ||
d77e38e6 SK |
79 | if ((!skb_is_gso(skb) && skb->len > mtu) || |
80 | (skb_is_gso(skb) && | |
80f5974d | 81 | !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) { |
180e4250 | 82 | skb->dev = dst->dev; |
ca064bd8 | 83 | skb->protocol = htons(ETH_P_IPV6); |
dd767856 SK |
84 | |
85 | if (xfrm6_local_dontfrag(skb)) | |
86 | xfrm6_local_rxpmtu(skb, mtu); | |
87 | else if (skb->sk) | |
628e341f | 88 | xfrm_local_error(skb, mtu); |
dd767856 SK |
89 | else |
90 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); | |
1da177e4 LT |
91 | ret = -EMSGSIZE; |
92 | } | |
d77e38e6 | 93 | out: |
1da177e4 LT |
94 | return ret; |
95 | } | |
96 | ||
36cf9acf HX |
97 | int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb) |
98 | { | |
99 | int err; | |
100 | ||
101 | err = xfrm6_tunnel_check_size(skb); | |
102 | if (err) | |
103 | return err; | |
104 | ||
60d5fcfb HX |
105 | XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr; |
106 | ||
36cf9acf HX |
107 | return xfrm6_extract_header(skb); |
108 | } | |
109 | ||
7026b1dd | 110 | int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb) |
09b8f7a9 | 111 | { |
5596732f | 112 | memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
5596732f | 113 | |
862b82c6 | 114 | IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; |
09b8f7a9 | 115 | |
7026b1dd | 116 | return xfrm_output(sk, skb); |
09b8f7a9 HX |
117 | } |
118 | ||
733a5fac FW |
119 | static int __xfrm6_output_state_finish(struct xfrm_state *x, struct sock *sk, |
120 | struct sk_buff *skb) | |
121 | { | |
122 | const struct xfrm_state_afinfo *afinfo; | |
123 | int ret = -EAFNOSUPPORT; | |
124 | ||
125 | rcu_read_lock(); | |
c9500d7b | 126 | afinfo = xfrm_state_afinfo_get_rcu(x->outer_mode.family); |
733a5fac FW |
127 | if (likely(afinfo)) |
128 | ret = afinfo->output_finish(sk, skb); | |
129 | else | |
130 | kfree_skb(skb); | |
131 | rcu_read_unlock(); | |
132 | ||
133 | return ret; | |
134 | } | |
135 | ||
7d8c6e39 EB |
136 | static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
137 | { | |
138 | struct xfrm_state *x = skb_dst(skb)->xfrm; | |
b1842ffd | 139 | |
733a5fac | 140 | return __xfrm6_output_state_finish(x, sk, skb); |
7d8c6e39 EB |
141 | } |
142 | ||
b285a089 LW |
143 | static int xfrm6_noneed_fragment(struct sk_buff *skb) |
144 | { | |
145 | struct frag_hdr *fh; | |
146 | u8 prevhdr = ipv6_hdr(skb)->nexthdr; | |
147 | ||
148 | if (prevhdr != NEXTHDR_FRAGMENT) | |
149 | return 0; | |
150 | fh = (struct frag_hdr *)(skb->data + sizeof(struct ipv6hdr)); | |
151 | if (fh->nexthdr == NEXTHDR_ESP || fh->nexthdr == NEXTHDR_AUTH) | |
152 | return 1; | |
153 | return 0; | |
154 | } | |
155 | ||
0c4b51f0 | 156 | static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
ad0081e4 DS |
157 | { |
158 | struct dst_entry *dst = skb_dst(skb); | |
159 | struct xfrm_state *x = dst->xfrm; | |
27c28d1e | 160 | unsigned int mtu; |
93efac3f | 161 | bool toobig; |
0ea9d5e3 | 162 | |
5596732f SK |
163 | #ifdef CONFIG_NETFILTER |
164 | if (!x) { | |
165 | IP6CB(skb)->flags |= IP6SKB_REROUTED; | |
13206b6b | 166 | return dst_output(net, sk, skb); |
5596732f SK |
167 | } |
168 | #endif | |
169 | ||
93efac3f HX |
170 | if (x->props.mode != XFRM_MODE_TUNNEL) |
171 | goto skip_frag; | |
172 | ||
5a25cf1e HFS |
173 | if (skb->protocol == htons(ETH_P_IPV6)) |
174 | mtu = ip6_skb_dst_mtu(skb); | |
175 | else | |
176 | mtu = dst_mtu(skb_dst(skb)); | |
dd767856 | 177 | |
93efac3f HX |
178 | toobig = skb->len > mtu && !skb_is_gso(skb); |
179 | ||
180 | if (toobig && xfrm6_local_dontfrag(skb)) { | |
dd767856 | 181 | xfrm6_local_rxpmtu(skb, mtu); |
215ab0f0 | 182 | kfree_skb(skb); |
dd767856 | 183 | return -EMSGSIZE; |
b285a089 LW |
184 | } else if (toobig && xfrm6_noneed_fragment(skb)) { |
185 | skb->ignore_df = 1; | |
186 | goto skip_frag; | |
93efac3f | 187 | } else if (!skb->ignore_df && toobig && skb->sk) { |
628e341f | 188 | xfrm_local_error(skb, mtu); |
215ab0f0 | 189 | kfree_skb(skb); |
dd767856 SK |
190 | return -EMSGSIZE; |
191 | } | |
ad0081e4 | 192 | |
93efac3f | 193 | if (toobig || dst_allfrag(skb_dst(skb))) |
7d8c6e39 EB |
194 | return ip6_fragment(net, sk, skb, |
195 | __xfrm6_output_finish); | |
93efac3f HX |
196 | |
197 | skip_frag: | |
733a5fac | 198 | return __xfrm6_output_state_finish(x, sk, skb); |
ad0081e4 DS |
199 | } |
200 | ||
ede2059d | 201 | int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
16a6677f | 202 | { |
29a26a56 EB |
203 | return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, |
204 | net, sk, skb, NULL, skb_dst(skb)->dev, | |
205 | __xfrm6_output, | |
5596732f | 206 | !(IP6CB(skb)->flags & IP6SKB_REROUTED)); |
16a6677f | 207 | } |