blob: 206f66310a88d1d4e792dadc8e11553781fb6564 [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 * common UDP/RAW code
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Randy Dunlap4fc268d2006-01-11 12:17:47 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
12#include <linux/types.h>
13#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/interrupt.h>
15#include <linux/socket.h>
16#include <linux/sockios.h>
17#include <linux/in6.h>
18#include <linux/ipv6.h>
19#include <linux/route.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Elstona495f832012-04-29 21:48:53 +000021#include <linux/export.h>
Willem de Bruijn01370432020-07-24 09:03:10 -040022#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <net/ipv6.h>
25#include <net/ndisc.h>
26#include <net/addrconf.h>
27#include <net/transp_v6.h>
28#include <net/ip6_route.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070029#include <net/tcp_states.h>
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +000030#include <net/dsfield.h>
Willem de Bruijnacdcecc2019-09-12 21:16:39 -040031#include <net/sock_reuseport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/errqueue.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080034#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Eric Dumazeta50feda2012-05-18 18:57:34 +000036static bool ipv6_mapped_addr_any(const struct in6_addr *a)
Max Matveevc15fea22011-08-05 03:56:30 -070037{
Eric Dumazeta50feda2012-05-18 18:57:34 +000038 return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
Max Matveevc15fea22011-08-05 03:56:30 -070039}
40
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -070041static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk)
42{
43 struct inet_sock *inet = inet_sk(sk);
44 struct ipv6_pinfo *np = inet6_sk(sk);
45
46 memset(fl6, 0, sizeof(*fl6));
47 fl6->flowi6_proto = sk->sk_protocol;
48 fl6->daddr = sk->sk_v6_daddr;
49 fl6->saddr = np->saddr;
50 fl6->flowi6_oif = sk->sk_bound_dev_if;
51 fl6->flowi6_mark = sk->sk_mark;
52 fl6->fl6_dport = inet->inet_dport;
53 fl6->fl6_sport = inet->inet_sport;
54 fl6->flowlabel = np->flow_label;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +090055 fl6->flowi6_uid = sk->sk_uid;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -070056
57 if (!fl6->flowi6_oif)
58 fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
59
60 if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr))
61 fl6->flowi6_oif = np->mcast_oif;
62
Paul Moore3df98d72020-09-27 22:38:26 -040063 security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -070064}
65
Martin KaFai Lau33c162a2016-04-11 15:29:36 -070066int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -070067{
68 struct ip6_flowlabel *flowlabel = NULL;
69 struct in6_addr *final_p, final;
70 struct ipv6_txoptions *opt;
71 struct dst_entry *dst;
72 struct inet_sock *inet = inet_sk(sk);
73 struct ipv6_pinfo *np = inet6_sk(sk);
74 struct flowi6 fl6;
75 int err = 0;
76
77 if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
78 flowlabel = fl6_sock_lookup(sk, np->flow_label);
Eric Dumazet8975a3a2019-07-10 06:40:10 -070079 if (IS_ERR(flowlabel))
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -070080 return -EINVAL;
81 }
82 ip6_datagram_flow_key_init(&fl6, sk);
83
84 rcu_read_lock();
85 opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
86 final_p = fl6_update_dst(&fl6, opt, &final);
87 rcu_read_unlock();
88
Sabrina Dubrocac4e85f72019-12-04 15:35:52 +010089 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -070090 if (IS_ERR(dst)) {
91 err = PTR_ERR(dst);
92 goto out;
93 }
94
Martin KaFai Lau33c162a2016-04-11 15:29:36 -070095 if (fix_sk_saddr) {
96 if (ipv6_addr_any(&np->saddr))
97 np->saddr = fl6.saddr;
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -070098
Martin KaFai Lau33c162a2016-04-11 15:29:36 -070099 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
100 sk->sk_v6_rcv_saddr = fl6.saddr;
101 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
102 if (sk->sk_prot->rehash)
103 sk->sk_prot->rehash(sk);
104 }
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700105 }
106
Alexey Kodanev7d6850f2018-04-03 15:00:07 +0300107 ip6_sk_dst_store_flow(sk, dst, &fl6);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700108
109out:
110 fl6_sock_release(flowlabel);
111 return err;
112}
113
Martin KaFai Laue646b652016-04-11 15:29:37 -0700114void ip6_datagram_release_cb(struct sock *sk)
115{
116 struct dst_entry *dst;
117
118 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
119 return;
120
121 rcu_read_lock();
122 dst = __sk_dst_get(sk);
123 if (!dst || !dst->obsolete ||
124 dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
125 rcu_read_unlock();
126 return;
127 }
128 rcu_read_unlock();
129
130 ip6_datagram_dst_update(sk, false);
131}
132EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
133
Guillaume Nault0382a252016-11-29 13:09:44 +0100134int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
135 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
Ian Morris67ba4152014-08-24 21:53:10 +0100138 struct inet_sock *inet = inet_sk(sk);
139 struct ipv6_pinfo *np = inet6_sk(sk);
Paolo Abeni2f987a72018-03-12 14:54:23 +0100140 struct in6_addr *daddr, old_daddr;
141 __be32 fl6_flowlabel = 0;
142 __be32 old_fl6_flowlabel;
Stefano Brivio5f2fb802018-03-19 11:24:58 +0100143 __be16 old_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int addr_type;
145 int err;
146
147 if (usin->sin6_family == AF_INET) {
148 if (__ipv6_only_sock(sk))
149 return -EAFNOSUPPORT;
Eric Dumazet03645a12015-07-14 08:10:22 +0200150 err = __ip4_datagram_connect(sk, uaddr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto ipv4_connected;
152 }
153
154 if (addr_len < SIN6_LEN_RFC2133)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900157 if (usin->sin6_family != AF_INET6)
158 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700160 if (np->sndflow)
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700161 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jonathan T. Leighton052d2362017-02-12 17:26:07 -0500163 if (ipv6_addr_any(&usin->sin6_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * connect to self
166 */
Jonathan T. Leighton052d2362017-02-12 17:26:07 -0500167 if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
168 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
169 &usin->sin6_addr);
170 else
171 usin->sin6_addr = in6addr_loopback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
Jonathan T. Leighton052d2362017-02-12 17:26:07 -0500174 addr_type = ipv6_addr_type(&usin->sin6_addr);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 daddr = &usin->sin6_addr;
177
Jonathan T. Leighton052d2362017-02-12 17:26:07 -0500178 if (addr_type & IPV6_ADDR_MAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct sockaddr_in sin;
180
181 if (__ipv6_only_sock(sk)) {
182 err = -ENETUNREACH;
183 goto out;
184 }
185 sin.sin_family = AF_INET;
186 sin.sin_addr.s_addr = daddr->s6_addr32[3];
187 sin.sin_port = usin->sin6_port;
188
Eric Dumazet03645a12015-07-14 08:10:22 +0200189 err = __ip4_datagram_connect(sk,
190 (struct sockaddr *) &sin,
191 sizeof(sin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193ipv4_connected:
194 if (err)
195 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900196
Eric Dumazetefe42082013-10-03 15:42:29 -0700197 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Max Matveevc15fea22011-08-05 03:56:30 -0700199 if (ipv6_addr_any(&np->saddr) ||
200 ipv6_mapped_addr_any(&np->saddr))
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000201 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Eric Dumazetefe42082013-10-03 15:42:29 -0700203 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
204 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000205 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
Eric Dumazetefe42082013-10-03 15:42:29 -0700206 &sk->sk_v6_rcv_saddr);
Eric Dumazet719f8352010-09-08 05:08:44 +0000207 if (sk->sk_prot->rehash)
208 sk->sk_prot->rehash(sk);
209 }
Brian Haleyb301e822009-10-07 13:58:25 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto out;
212 }
213
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000214 if (__ipv6_addr_needs_scope_id(addr_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (addr_len >= sizeof(struct sockaddr_in6) &&
216 usin->sin6_scope_id) {
David Ahern54dc3e32018-01-04 14:03:54 -0800217 if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 err = -EINVAL;
219 goto out;
220 }
221 sk->sk_bound_dev_if = usin->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Brian Haley1ac4f002008-01-08 23:52:21 -0800224 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
225 sk->sk_bound_dev_if = np->mcast_oif;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* Connect to link-local address requires an interface */
228 if (!sk->sk_bound_dev_if) {
229 err = -EINVAL;
230 goto out;
231 }
232 }
233
Paolo Abeni2f987a72018-03-12 14:54:23 +0100234 /* save the current peer information before updating it */
235 old_daddr = sk->sk_v6_daddr;
236 old_fl6_flowlabel = np->flow_label;
237 old_dport = inet->inet_dport;
238
Eric Dumazetefe42082013-10-03 15:42:29 -0700239 sk->sk_v6_daddr = *daddr;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700240 np->flow_label = fl6_flowlabel;
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000241 inet->inet_dport = usin->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /*
244 * Check for a route to destination an obtain the
245 * destination cache for it.
246 */
247
Martin KaFai Lau33c162a2016-04-11 15:29:36 -0700248 err = ip6_datagram_dst_update(sk, true);
Wei Wang85cb73f2017-06-23 15:25:37 -0700249 if (err) {
Paolo Abeni2f987a72018-03-12 14:54:23 +0100250 /* Restore the socket peer info, to keep it consistent with
251 * the old socket state
Wei Wang85cb73f2017-06-23 15:25:37 -0700252 */
Paolo Abeni2f987a72018-03-12 14:54:23 +0100253 sk->sk_v6_daddr = old_daddr;
254 np->flow_label = old_fl6_flowlabel;
255 inet->inet_dport = old_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto out;
Wei Wang85cb73f2017-06-23 15:25:37 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Willem de Bruijnacdcecc2019-09-12 21:16:39 -0400259 reuseport_has_conns(sk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 sk->sk_state = TCP_ESTABLISHED;
Tom Herbert877d1f62015-07-28 16:02:05 -0700261 sk_set_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return err;
264}
Guillaume Nault0382a252016-11-29 13:09:44 +0100265EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
Eric Dumazet03645a12015-07-14 08:10:22 +0200266
267int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
268{
269 int res;
270
271 lock_sock(sk);
272 res = __ip6_datagram_connect(sk, uaddr, addr_len);
273 release_sock(sk);
274 return res;
275}
Chris Elstona495f832012-04-29 21:48:53 +0000276EXPORT_SYMBOL_GPL(ip6_datagram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Hannes Frederic Sowa82b276c2014-01-20 05:16:39 +0100278int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
279 int addr_len)
280{
281 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
282 if (sin6->sin6_family != AF_INET6)
283 return -EAFNOSUPPORT;
284 return ip6_datagram_connect(sk, uaddr, addr_len);
285}
286EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
287
Willem de Bruijn01370432020-07-24 09:03:10 -0400288static void ipv6_icmp_error_rfc4884(const struct sk_buff *skb,
289 struct sock_ee_data_rfc4884 *out)
290{
291 switch (icmp6_hdr(skb)->icmp6_type) {
292 case ICMPV6_TIME_EXCEED:
293 case ICMPV6_DEST_UNREACH:
294 ip_icmp_error_rfc4884(skb, out, sizeof(struct icmp6hdr),
295 icmp6_hdr(skb)->icmp6_datagram_len * 8);
296 }
297}
298
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900299void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4ad2006-11-14 20:56:00 -0800300 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300303 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct sock_exterr_skb *serr;
305
306 if (!np->recverr)
307 return;
308
309 skb = skb_clone(skb, GFP_ATOMIC);
310 if (!skb)
311 return;
312
Brian Haleyd40a4de2010-05-03 15:44:27 +0000313 skb->protocol = htons(ETH_P_IPV6);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 serr = SKB_EXT_ERR(skb);
316 serr->ee.ee_errno = err;
317 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900318 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 serr->ee.ee_code = icmph->icmp6_code;
320 serr->ee.ee_pad = 0;
321 serr->ee.ee_info = info;
322 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700323 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
324 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 serr->port = port;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 __skb_pull(skb, payload - skb->data);
Willem de Bruijn01370432020-07-24 09:03:10 -0400328
329 if (inet6_sk(sk)->recverr_rfc4884)
330 ipv6_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
331
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300332 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (sock_queue_err_skb(sk, skb))
335 kfree_skb(skb);
336}
337
David S. Miller4c9483b2011-03-12 16:22:43 -0500338void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700340 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct sock_exterr_skb *serr;
342 struct ipv6hdr *iph;
343 struct sk_buff *skb;
344
345 if (!np->recverr)
346 return;
347
348 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
349 if (!skb)
350 return;
351
Brian Haleyd40a4de2010-05-03 15:44:27 +0000352 skb->protocol = htons(ETH_P_IPV6);
353
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300354 skb_put(skb, sizeof(struct ipv6hdr));
355 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700356 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000357 iph->daddr = fl6->daddr;
Eric Dumazet7d033c92019-01-08 04:06:14 -0800358 ip6_flow_hdr(iph, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 serr = SKB_EXT_ERR(skb);
361 serr->ee.ee_errno = err;
362 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900363 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 serr->ee.ee_code = 0;
365 serr->ee.ee_pad = 0;
366 serr->ee.ee_info = info;
367 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700368 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500369 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700371 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300372 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (sock_queue_err_skb(sk, skb))
375 kfree_skb(skb);
376}
377
David S. Miller4c9483b2011-03-12 16:22:43 -0500378void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000379{
380 struct ipv6_pinfo *np = inet6_sk(sk);
381 struct ipv6hdr *iph;
382 struct sk_buff *skb;
383 struct ip6_mtuinfo *mtu_info;
384
385 if (!np->rxopt.bits.rxpmtu)
386 return;
387
388 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
389 if (!skb)
390 return;
391
392 skb_put(skb, sizeof(struct ipv6hdr));
393 skb_reset_network_header(skb);
394 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000395 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000396
397 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000398
399 mtu_info->ip6m_mtu = mtu;
400 mtu_info->ip6m_addr.sin6_family = AF_INET6;
401 mtu_info->ip6m_addr.sin6_port = 0;
402 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500403 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000404 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000405
406 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
407 skb_reset_transport_header(skb);
408
409 skb = xchg(&np->rxpmtu, skb);
410 kfree_skb(skb);
411}
412
Julian Anastasov34b99df2015-06-23 08:34:39 +0300413/* For some errors we have valid addr_offset even with zero payload and
414 * zero port. Also, addr_offset should be supported if port is set.
415 */
416static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
417{
418 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
419 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
420 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
421}
422
Willem de Bruijnc247f052015-03-07 20:33:22 -0500423/* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
424 *
425 * At one point, excluding local errors was a quick test to identify icmp/icmp6
426 * errors. This is no longer true, but the test remained, so the v6 stack,
427 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
Willem de Bruijnc247f052015-03-07 20:33:22 -0500428 */
429static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
430 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500431{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500432 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
433 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
434 return true;
435
436 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
437 return false;
438
Willem de Bruijn1862d622017-04-12 19:24:35 -0400439 if (!IP6CB(skb)->iif)
Willem de Bruijnc247f052015-03-07 20:33:22 -0500440 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500441
Willem de Bruijnc247f052015-03-07 20:33:22 -0500442 return true;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500443}
444
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900445/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * Handle MSG_ERRQUEUE
447 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100448int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct ipv6_pinfo *np = inet6_sk(sk);
451 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400452 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100453 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct {
455 struct sock_extended_err ee;
456 struct sockaddr_in6 offender;
457 } errhdr;
458 int err;
459 int copied;
460
461 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400462 skb = sock_dequeue_err_skb(sk);
Ian Morris63159f22015-03-29 14:00:04 +0100463 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out;
465
466 copied = skb->len;
467 if (copied > len) {
468 msg->msg_flags |= MSG_TRUNC;
469 copied = len;
470 }
David S. Miller51f3d022014-11-05 16:46:40 -0500471 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700472 if (unlikely(err)) {
473 kfree_skb(skb);
474 return err;
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sock_recv_timestamp(msg, sk, skb);
477
478 serr = SKB_EXT_ERR(skb);
479
Julian Anastasov34b99df2015-06-23 08:34:39 +0300480 if (sin && ipv6_datagram_support_addr(serr)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700481 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 sin->sin6_family = AF_INET6;
483 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900484 sin->sin6_port = serr->port;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000485 if (skb->protocol == htons(ETH_P_IPV6)) {
YOSHIFUJI Hideaki / 吉藤英明6c40d102013-01-08 06:44:23 +0000486 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
487 struct ipv6hdr, daddr);
488 sin->sin6_addr = ip6h->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (np->sndflow)
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000490 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000491 sin->sin6_scope_id =
492 ipv6_iface_scope_id(&sin->sin6_addr,
493 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700495 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
496 &sin->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000497 sin->sin6_scope_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100499 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
503 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500504 memset(sin, 0, sizeof(*sin));
Willem de Bruijnc247f052015-03-07 20:33:22 -0500505
506 if (ip6_datagram_support_cmsg(skb, serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 sin->sin6_family = AF_INET6;
Willem de Bruijnc247f052015-03-07 20:33:22 -0500508 if (np->rxopt.all)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100509 ip6_datagram_recv_common_ctl(sk, msg, skb);
Brian Haleyd40a4de2010-05-03 15:44:27 +0000510 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000511 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (np->rxopt.all)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100513 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000514 sin->sin6_scope_id =
515 ipv6_iface_scope_id(&sin->sin6_addr,
516 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700518 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
519 &sin->sin6_addr);
Willem de Bruijnf8121162015-01-15 13:18:40 -0500520 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 ip_cmsg_recv(msg, skb);
522 }
523 }
524
525 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
526
527 /* Now we could try to dump offended packet options */
528
529 msg->msg_flags |= MSG_ERRQUEUE;
530 err = copied;
531
Eric Dumazet960a2622016-04-21 22:27:32 -0700532 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533out:
534 return err;
535}
Chris Elstona495f832012-04-29 21:48:53 +0000536EXPORT_SYMBOL_GPL(ipv6_recv_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Brian Haley4b340ae2010-04-23 11:26:09 +0000538/*
539 * Handle IPV6_RECVPATHMTU
540 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100541int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
542 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000543{
544 struct ipv6_pinfo *np = inet6_sk(sk);
545 struct sk_buff *skb;
Brian Haley4b340ae2010-04-23 11:26:09 +0000546 struct ip6_mtuinfo mtu_info;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100547 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Brian Haley4b340ae2010-04-23 11:26:09 +0000548 int err;
549 int copied;
550
551 err = -EAGAIN;
552 skb = xchg(&np->rxpmtu, NULL);
Ian Morris63159f22015-03-29 14:00:04 +0100553 if (!skb)
Brian Haley4b340ae2010-04-23 11:26:09 +0000554 goto out;
555
556 copied = skb->len;
557 if (copied > len) {
558 msg->msg_flags |= MSG_TRUNC;
559 copied = len;
560 }
David S. Miller51f3d022014-11-05 16:46:40 -0500561 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Brian Haley4b340ae2010-04-23 11:26:09 +0000562 if (err)
563 goto out_free_skb;
564
565 sock_recv_timestamp(msg, sk, skb);
566
567 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
568
Brian Haley4b340ae2010-04-23 11:26:09 +0000569 if (sin) {
570 sin->sin6_family = AF_INET6;
571 sin->sin6_flowinfo = 0;
572 sin->sin6_port = 0;
573 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000574 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100575 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000576 }
577
578 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
579
580 err = copied;
581
582out_free_skb:
583 kfree_skb(skb);
584out:
585 return err;
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100589void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
590 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct ipv6_pinfo *np = inet6_sk(sk);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100593 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (np->rxopt.bits.rxinfo) {
596 struct in6_pktinfo src_info;
597
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100598 if (is_ipv6) {
599 src_info.ipi6_ifindex = IP6CB(skb)->iif;
600 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
601 } else {
602 src_info.ipi6_ifindex =
603 PKTINFO_SKB_CB(skb)->ipi_ifindex;
604 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
605 &src_info.ipi6_addr);
606 }
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500607
608 if (src_info.ipi6_ifindex >= 0)
609 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
610 sizeof(src_info), &src_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100612}
613
614void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
615 struct sk_buff *skb)
616{
617 struct ipv6_pinfo *np = inet6_sk(sk);
618 struct inet6_skb_parm *opt = IP6CB(skb);
619 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700622 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
624 }
625
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900626 if (np->rxopt.bits.rxtclass) {
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +0000627 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900628 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
629 }
630
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000631 if (np->rxopt.bits.rxflow) {
632 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
633 if (flowinfo)
634 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900636
637 /* HbH is allowed only once */
Florian Westphal8b58a392015-07-08 23:32:12 +0200638 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
639 u8 *ptr = nh + sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
641 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900642
643 if (opt->lastopt &&
644 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
645 /*
646 * Silly enough, but we need to reparse in order to
647 * report extension headers (except for HbH)
648 * in order.
649 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900650 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900651 * (and WILL NOT be) defined because
652 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
653 */
654 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700655 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900656
657 while (off <= opt->lastopt) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000658 unsigned int len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700659 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900660
Eldad Zackb5a42572012-04-01 07:49:03 +0000661 switch (nexthdr) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900662 case IPPROTO_DSTOPTS:
663 nexthdr = ptr[0];
664 len = (ptr[1] + 1) << 3;
665 if (np->rxopt.bits.dstopts)
666 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
667 break;
668 case IPPROTO_ROUTING:
669 nexthdr = ptr[0];
670 len = (ptr[1] + 1) << 3;
671 if (np->rxopt.bits.srcrt)
672 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
673 break;
674 case IPPROTO_AH:
675 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900676 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900677 break;
678 default:
679 nexthdr = ptr[0];
680 len = (ptr[1] + 1) << 3;
681 break;
682 }
683
684 off += len;
685 }
686 }
687
688 /* socket options in old style */
689 if (np->rxopt.bits.rxoinfo) {
690 struct in6_pktinfo src_info;
691
692 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000693 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900694 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
695 }
696 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700697 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900698 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
699 }
Florian Westphal8b58a392015-07-08 23:32:12 +0200700 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
701 u8 *ptr = nh + sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900702 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
703 }
704 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700705 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900706 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900708 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700709 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900710 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900712 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700713 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900714 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200716 if (np->rxopt.bits.rxorigdstaddr) {
717 struct sockaddr_in6 sin6;
Willem de Bruijn4a06fa62019-01-07 16:47:33 -0500718 __be16 _ports[2], *ports;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200719
Willem de Bruijn4a06fa62019-01-07 16:47:33 -0500720 ports = skb_header_pointer(skb, skb_transport_offset(skb),
721 sizeof(_ports), &_ports);
722 if (ports) {
Balazs Scheidler6c468622010-10-21 16:08:28 +0200723 /* All current transport protocols have the port numbers in the
724 * first four bytes of the transport header and this function is
725 * written with this assumption in mind.
726 */
Balazs Scheidler6c468622010-10-21 16:08:28 +0200727 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000728 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200729 sin6.sin6_port = ports[1];
730 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000731 sin6.sin6_scope_id =
732 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
733 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200734
735 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
736 }
737 }
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400738 if (np->rxopt.bits.recvfragsize && opt->frag_max_size) {
739 int val = opt->frag_max_size;
740
741 put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val);
742 }
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100743}
744
745void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
746 struct sk_buff *skb)
747{
748 ip6_datagram_recv_common_ctl(sk, msg, skb);
749 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
Tom Parkin8e72d372013-01-31 01:02:25 +0000751EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Tom Parkin73df66f2013-01-31 01:02:24 +0000753int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
754 struct msghdr *msg, struct flowi6 *fl6,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400755 struct ipcm6_cookie *ipc6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct in6_pktinfo *src_info;
758 struct cmsghdr *cmsg;
759 struct ipv6_rt_hdr *rthdr;
760 struct ipv6_opt_hdr *hdr;
Wei Wang26879da2016-05-02 21:40:07 -0700761 struct ipv6_txoptions *opt = ipc6->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 int len;
763 int err = 0;
764
Gu Zhengf95b4142014-12-11 11:22:04 +0800765 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (!CMSG_OK(msg, cmsg)) {
769 err = -EINVAL;
770 goto exit_f;
771 }
772
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400773 if (cmsg->cmsg_level == SOL_SOCKET) {
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400774 err = __sock_cmsg_send(sk, msg, cmsg, &ipc6->sockc);
Eric Dumazet26326162016-05-13 06:14:37 -0700775 if (err)
776 return err;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400777 continue;
778 }
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (cmsg->cmsg_level != SOL_IPV6)
781 continue;
782
783 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900784 case IPV6_PKTINFO:
785 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900786 {
787 struct net_device *dev = NULL;
Mike Manning6da5b0f2018-11-07 15:36:04 +0000788 int src_idx;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900789
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900790 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 err = -EINVAL;
792 goto exit_f;
793 }
794
795 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
Mike Manning6da5b0f2018-11-07 15:36:04 +0000796 src_idx = src_info->ipi6_ifindex;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900797
Mike Manning6da5b0f2018-11-07 15:36:04 +0000798 if (src_idx) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500799 if (fl6->flowi6_oif &&
Mike Manning6da5b0f2018-11-07 15:36:04 +0000800 src_idx != fl6->flowi6_oif &&
801 (sk->sk_bound_dev_if != fl6->flowi6_oif ||
802 !sk_dev_equal_l3scope(sk, src_idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EINVAL;
Mike Manning6da5b0f2018-11-07 15:36:04 +0000804 fl6->flowi6_oif = src_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900807 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Eric Dumazet536b2e92009-11-02 12:21:06 +0100809 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500810 if (fl6->flowi6_oif) {
811 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100812 if (!dev) {
813 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900814 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100815 }
816 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
817 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900818 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100819 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900820
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900821 if (addr_type != IPV6_ADDR_ANY) {
822 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Vincent Bernat83ba4642018-07-31 21:18:11 +0200823 if (!ipv6_can_nonlocal_bind(net, inet_sk(sk)) &&
David Ahern232378e2018-03-13 08:29:37 -0700824 !ipv6_chk_addr_and_flags(net, &src_info->ipi6_addr,
825 dev, !strict, 0,
826 IFA_F_TENTATIVE) &&
FX Le Bail7c90cc22014-01-22 07:42:37 +0100827 !ipv6_chk_acast_addr_src(net, dev,
828 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900829 err = -EINVAL;
830 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000831 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900833
Eric Dumazet536b2e92009-11-02 12:21:06 +0100834 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900836 if (err)
837 goto exit_f;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900843 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 err = -EINVAL;
845 goto exit_f;
846 }
847
David S. Miller4c9483b2011-03-12 16:22:43 -0500848 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
849 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 err = -EINVAL;
851 goto exit_f;
852 }
853 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500854 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 break;
856
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900857 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900859 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 err = -EINVAL;
861 goto exit_f;
862 }
863
864 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
865 len = ((hdr->hdrlen + 1) << 3);
866 if (cmsg->cmsg_len < CMSG_LEN(len)) {
867 err = -EINVAL;
868 goto exit_f;
869 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000870 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 err = -EPERM;
872 goto exit_f;
873 }
874 opt->opt_nflen += len;
875 opt->hopopt = hdr;
876 break;
877
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900878 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900879 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 err = -EINVAL;
881 goto exit_f;
882 }
883
884 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
885 len = ((hdr->hdrlen + 1) << 3);
886 if (cmsg->cmsg_len < CMSG_LEN(len)) {
887 err = -EINVAL;
888 goto exit_f;
889 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000890 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 err = -EPERM;
892 goto exit_f;
893 }
894 if (opt->dst1opt) {
895 err = -EINVAL;
896 goto exit_f;
897 }
898 opt->opt_flen += len;
899 opt->dst1opt = hdr;
900 break;
901
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900902 case IPV6_DSTOPTS:
903 case IPV6_RTHDRDSTOPTS:
904 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
905 err = -EINVAL;
906 goto exit_f;
907 }
908
909 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
910 len = ((hdr->hdrlen + 1) << 3);
911 if (cmsg->cmsg_len < CMSG_LEN(len)) {
912 err = -EINVAL;
913 goto exit_f;
914 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000915 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900916 err = -EPERM;
917 goto exit_f;
918 }
919 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
920 opt->opt_flen += len;
921 opt->dst1opt = hdr;
922 } else {
923 opt->opt_nflen += len;
924 opt->dst0opt = hdr;
925 }
926 break;
927
928 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900930 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 err = -EINVAL;
932 goto exit_f;
933 }
934
935 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
936
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700937 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000938#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700939 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800940 if (rthdr->hdrlen != 2 ||
941 rthdr->segments_left != 1) {
942 err = -EINVAL;
943 goto exit_f;
944 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700945 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700946#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700947 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 err = -EINVAL;
949 goto exit_f;
950 }
951
952 len = ((rthdr->hdrlen + 1) << 3);
953
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900954 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 err = -EINVAL;
956 goto exit_f;
957 }
958
959 /* segments left must also match */
960 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
961 err = -EINVAL;
962 goto exit_f;
963 }
964
965 opt->opt_nflen += len;
966 opt->srcrt = rthdr;
967
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900968 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
970
971 opt->opt_nflen += dsthdrlen;
972 opt->dst0opt = opt->dst1opt;
973 opt->dst1opt = NULL;
974 opt->opt_flen -= dsthdrlen;
975 }
976
977 break;
978
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900979 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 case IPV6_HOPLIMIT:
981 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
982 err = -EINVAL;
983 goto exit_f;
984 }
985
Wei Wang26879da2016-05-02 21:40:07 -0700986 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
987 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
Shan Weie8766fc2008-06-10 15:50:55 +0800988 err = -EINVAL;
989 goto exit_f;
990 }
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
993
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900994 case IPV6_TCLASS:
995 {
996 int tc;
997
998 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000999 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001000 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001001
1002 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -07001003 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001004 goto exit_f;
1005
1006 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -07001007 ipc6->tclass = tc;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001008
1009 break;
1010 }
Brian Haley13b52cd2010-04-23 11:26:08 +00001011
1012 case IPV6_DONTFRAG:
1013 {
1014 int df;
1015
1016 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +00001017 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +00001018 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +00001019
1020 df = *(int *)CMSG_DATA(cmsg);
1021 if (df < 0 || df > 1)
1022 goto exit_f;
1023
1024 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -07001025 ipc6->dontfrag = df;
Brian Haley13b52cd2010-04-23 11:26:08 +00001026
1027 break;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -08001030 net_dbg_ratelimited("invalid cmsg type: %d\n",
1031 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -07001033 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037exit_f:
1038 return err;
1039}
Tom Parkin73df66f2013-01-31 01:02:24 +00001040EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001041
Paolo Abeni6c206b22018-06-08 11:35:40 +02001042void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1043 __u16 srcp, __u16 destp, int rqueue, int bucket)
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001044{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001045 const struct in6_addr *dest, *src;
1046
Eric Dumazetefe42082013-10-03 15:42:29 -07001047 dest = &sp->sk_v6_daddr;
1048 src = &sp->sk_v6_rcv_saddr;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001049 seq_printf(seq,
1050 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Patrick Talbertea9a0372019-05-17 17:11:28 +02001051 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001052 bucket,
1053 src->s6_addr32[0], src->s6_addr32[1],
1054 src->s6_addr32[2], src->s6_addr32[3], srcp,
1055 dest->s6_addr32[0], dest->s6_addr32[1],
1056 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1057 sp->sk_state,
1058 sk_wmem_alloc_get(sp),
Paolo Abeni6c206b22018-06-08 11:35:40 +02001059 rqueue,
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001060 0, 0L, 0,
1061 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1062 0,
1063 sock_i_ino(sp),
Reshetova, Elena41c6d652017-06-30 13:08:01 +03001064 refcount_read(&sp->sk_refcnt), sp,
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001065 atomic_read(&sp->sk_drops));
1066}