blob: 7071ec6d4264582fdaa072e09956be1fbda3e08d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * common UDP/RAW code
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
Randy Dunlap4fc268d2006-01-11 12:17:47 -080014#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
19#include <linux/socket.h>
20#include <linux/sockios.h>
21#include <linux/in6.h>
22#include <linux/ipv6.h>
23#include <linux/route.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Chris Elstona495f832012-04-29 21:48:53 +000025#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <net/ipv6.h>
28#include <net/ndisc.h>
29#include <net/addrconf.h>
30#include <net/transp_v6.h>
31#include <net/ip6_route.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070032#include <net/tcp_states.h>
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +000033#include <net/dsfield.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <linux/errqueue.h>
36#include <asm/uaccess.h>
37
Eric Dumazeta50feda2012-05-18 18:57:34 +000038static bool ipv6_mapped_addr_any(const struct in6_addr *a)
Max Matveevc15fea22011-08-05 03:56:30 -070039{
Eric Dumazeta50feda2012-05-18 18:57:34 +000040 return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
Max Matveevc15fea22011-08-05 03:56:30 -070041}
42
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -070043static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk)
44{
45 struct inet_sock *inet = inet_sk(sk);
46 struct ipv6_pinfo *np = inet6_sk(sk);
47
48 memset(fl6, 0, sizeof(*fl6));
49 fl6->flowi6_proto = sk->sk_protocol;
50 fl6->daddr = sk->sk_v6_daddr;
51 fl6->saddr = np->saddr;
52 fl6->flowi6_oif = sk->sk_bound_dev_if;
53 fl6->flowi6_mark = sk->sk_mark;
54 fl6->fl6_dport = inet->inet_dport;
55 fl6->fl6_sport = inet->inet_sport;
56 fl6->flowlabel = np->flow_label;
57
58 if (!fl6->flowi6_oif)
59 fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
60
61 if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr))
62 fl6->flowi6_oif = np->mcast_oif;
63
64 security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
65}
66
Martin KaFai Lau33c162a2016-04-11 15:29:36 -070067int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -070068{
69 struct ip6_flowlabel *flowlabel = NULL;
70 struct in6_addr *final_p, final;
71 struct ipv6_txoptions *opt;
72 struct dst_entry *dst;
73 struct inet_sock *inet = inet_sk(sk);
74 struct ipv6_pinfo *np = inet6_sk(sk);
75 struct flowi6 fl6;
76 int err = 0;
77
78 if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
79 flowlabel = fl6_sock_lookup(sk, np->flow_label);
80 if (!flowlabel)
81 return -EINVAL;
82 }
83 ip6_datagram_flow_key_init(&fl6, sk);
84
85 rcu_read_lock();
86 opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
87 final_p = fl6_update_dst(&fl6, opt, &final);
88 rcu_read_unlock();
89
90 dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
91 if (IS_ERR(dst)) {
92 err = PTR_ERR(dst);
93 goto out;
94 }
95
Martin KaFai Lau33c162a2016-04-11 15:29:36 -070096 if (fix_sk_saddr) {
97 if (ipv6_addr_any(&np->saddr))
98 np->saddr = fl6.saddr;
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -070099
Martin KaFai Lau33c162a2016-04-11 15:29:36 -0700100 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
101 sk->sk_v6_rcv_saddr = fl6.saddr;
102 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
103 if (sk->sk_prot->rehash)
104 sk->sk_prot->rehash(sk);
105 }
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700106 }
107
108 ip6_dst_store(sk, dst,
109 ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
110 &sk->sk_v6_daddr : NULL,
111#ifdef CONFIG_IPV6_SUBTREES
112 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
113 &np->saddr :
114#endif
115 NULL);
116
117out:
118 fl6_sock_release(flowlabel);
119 return err;
120}
121
Martin KaFai Laue646b652016-04-11 15:29:37 -0700122void ip6_datagram_release_cb(struct sock *sk)
123{
124 struct dst_entry *dst;
125
126 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
127 return;
128
129 rcu_read_lock();
130 dst = __sk_dst_get(sk);
131 if (!dst || !dst->obsolete ||
132 dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
133 rcu_read_unlock();
134 return;
135 }
136 rcu_read_unlock();
137
138 ip6_datagram_dst_update(sk, false);
139}
140EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
141
Guillaume Nault0382a252016-11-29 13:09:44 +0100142int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
143 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
Ian Morris67ba4152014-08-24 21:53:10 +0100146 struct inet_sock *inet = inet_sk(sk);
147 struct ipv6_pinfo *np = inet6_sk(sk);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700148 struct in6_addr *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int addr_type;
150 int err;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700151 __be32 fl6_flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (usin->sin6_family == AF_INET) {
154 if (__ipv6_only_sock(sk))
155 return -EAFNOSUPPORT;
Eric Dumazet03645a12015-07-14 08:10:22 +0200156 err = __ip4_datagram_connect(sk, uaddr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 goto ipv4_connected;
158 }
159
160 if (addr_len < SIN6_LEN_RFC2133)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900161 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900163 if (usin->sin6_family != AF_INET6)
164 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700166 if (np->sndflow)
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700167 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jonathan T. Leighton12ec2562017-05-23 21:53:33 -0400169 if (ipv6_addr_any(&usin->sin6_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /*
171 * connect to self
172 */
Jonathan T. Leighton12ec2562017-05-23 21:53:33 -0400173 if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
174 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
175 &usin->sin6_addr);
176 else
177 usin->sin6_addr = in6addr_loopback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
Jonathan T. Leighton12ec2562017-05-23 21:53:33 -0400180 addr_type = ipv6_addr_type(&usin->sin6_addr);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 daddr = &usin->sin6_addr;
183
Jonathan T. Leighton12ec2562017-05-23 21:53:33 -0400184 if (addr_type & IPV6_ADDR_MAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct sockaddr_in sin;
186
187 if (__ipv6_only_sock(sk)) {
188 err = -ENETUNREACH;
189 goto out;
190 }
191 sin.sin_family = AF_INET;
192 sin.sin_addr.s_addr = daddr->s6_addr32[3];
193 sin.sin_port = usin->sin6_port;
194
Eric Dumazet03645a12015-07-14 08:10:22 +0200195 err = __ip4_datagram_connect(sk,
196 (struct sockaddr *) &sin,
197 sizeof(sin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199ipv4_connected:
200 if (err)
201 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900202
Eric Dumazetefe42082013-10-03 15:42:29 -0700203 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Max Matveevc15fea22011-08-05 03:56:30 -0700205 if (ipv6_addr_any(&np->saddr) ||
206 ipv6_mapped_addr_any(&np->saddr))
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000207 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Eric Dumazetefe42082013-10-03 15:42:29 -0700209 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
210 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000211 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
Eric Dumazetefe42082013-10-03 15:42:29 -0700212 &sk->sk_v6_rcv_saddr);
Eric Dumazet719f8352010-09-08 05:08:44 +0000213 if (sk->sk_prot->rehash)
214 sk->sk_prot->rehash(sk);
215 }
Brian Haleyb301e822009-10-07 13:58:25 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto out;
218 }
219
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000220 if (__ipv6_addr_needs_scope_id(addr_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (addr_len >= sizeof(struct sockaddr_in6) &&
222 usin->sin6_scope_id) {
223 if (sk->sk_bound_dev_if &&
224 sk->sk_bound_dev_if != usin->sin6_scope_id) {
225 err = -EINVAL;
226 goto out;
227 }
228 sk->sk_bound_dev_if = usin->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
Brian Haley1ac4f002008-01-08 23:52:21 -0800231 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
232 sk->sk_bound_dev_if = np->mcast_oif;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Connect to link-local address requires an interface */
235 if (!sk->sk_bound_dev_if) {
236 err = -EINVAL;
237 goto out;
238 }
239 }
240
Eric Dumazetefe42082013-10-03 15:42:29 -0700241 sk->sk_v6_daddr = *daddr;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700242 np->flow_label = fl6_flowlabel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000244 inet->inet_dport = usin->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /*
247 * Check for a route to destination an obtain the
248 * destination cache for it.
249 */
250
Martin KaFai Lau33c162a2016-04-11 15:29:36 -0700251 err = ip6_datagram_dst_update(sk, true);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700252 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 sk->sk_state = TCP_ESTABLISHED;
Tom Herbert877d1f62015-07-28 16:02:05 -0700256 sk_set_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return err;
259}
Guillaume Nault0382a252016-11-29 13:09:44 +0100260EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
Eric Dumazet03645a12015-07-14 08:10:22 +0200261
262int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
263{
264 int res;
265
266 lock_sock(sk);
267 res = __ip6_datagram_connect(sk, uaddr, addr_len);
268 release_sock(sk);
269 return res;
270}
Chris Elstona495f832012-04-29 21:48:53 +0000271EXPORT_SYMBOL_GPL(ip6_datagram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Hannes Frederic Sowa82b276c2014-01-20 05:16:39 +0100273int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
274 int addr_len)
275{
276 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
277 if (sin6->sin6_family != AF_INET6)
278 return -EAFNOSUPPORT;
279 return ip6_datagram_connect(sk, uaddr, addr_len);
280}
281EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
282
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900283void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4adc2006-11-14 20:56:00 -0800284 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300287 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct sock_exterr_skb *serr;
289
290 if (!np->recverr)
291 return;
292
293 skb = skb_clone(skb, GFP_ATOMIC);
294 if (!skb)
295 return;
296
Brian Haleyd40a4de2010-05-03 15:44:27 +0000297 skb->protocol = htons(ETH_P_IPV6);
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 serr = SKB_EXT_ERR(skb);
300 serr->ee.ee_errno = err;
301 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900302 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 serr->ee.ee_code = icmph->icmp6_code;
304 serr->ee.ee_pad = 0;
305 serr->ee.ee_info = info;
306 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700307 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
308 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 serr->port = port;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 __skb_pull(skb, payload - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300312 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (sock_queue_err_skb(sk, skb))
315 kfree_skb(skb);
316}
317
David S. Miller4c9483b2011-03-12 16:22:43 -0500318void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700320 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct sock_exterr_skb *serr;
322 struct ipv6hdr *iph;
323 struct sk_buff *skb;
324
325 if (!np->recverr)
326 return;
327
328 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
329 if (!skb)
330 return;
331
Brian Haleyd40a4de2010-05-03 15:44:27 +0000332 skb->protocol = htons(ETH_P_IPV6);
333
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300334 skb_put(skb, sizeof(struct ipv6hdr));
335 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700336 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000337 iph->daddr = fl6->daddr;
Eric Dumazet7492dfe2019-01-08 04:06:14 -0800338 ip6_flow_hdr(iph, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 serr = SKB_EXT_ERR(skb);
341 serr->ee.ee_errno = err;
342 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900343 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 serr->ee.ee_code = 0;
345 serr->ee.ee_pad = 0;
346 serr->ee.ee_info = info;
347 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700348 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500349 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700351 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300352 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (sock_queue_err_skb(sk, skb))
355 kfree_skb(skb);
356}
357
David S. Miller4c9483b2011-03-12 16:22:43 -0500358void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000359{
360 struct ipv6_pinfo *np = inet6_sk(sk);
361 struct ipv6hdr *iph;
362 struct sk_buff *skb;
363 struct ip6_mtuinfo *mtu_info;
364
365 if (!np->rxopt.bits.rxpmtu)
366 return;
367
368 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
369 if (!skb)
370 return;
371
372 skb_put(skb, sizeof(struct ipv6hdr));
373 skb_reset_network_header(skb);
374 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000375 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000376
377 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000378
379 mtu_info->ip6m_mtu = mtu;
380 mtu_info->ip6m_addr.sin6_family = AF_INET6;
381 mtu_info->ip6m_addr.sin6_port = 0;
382 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500383 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000384 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000385
386 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
387 skb_reset_transport_header(skb);
388
389 skb = xchg(&np->rxpmtu, skb);
390 kfree_skb(skb);
391}
392
Julian Anastasov34b99df2015-06-23 08:34:39 +0300393/* For some errors we have valid addr_offset even with zero payload and
394 * zero port. Also, addr_offset should be supported if port is set.
395 */
396static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
397{
398 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
399 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
400 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
401}
402
Willem de Bruijnc247f052015-03-07 20:33:22 -0500403/* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
404 *
405 * At one point, excluding local errors was a quick test to identify icmp/icmp6
406 * errors. This is no longer true, but the test remained, so the v6 stack,
407 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
Willem de Bruijnc247f052015-03-07 20:33:22 -0500408 */
409static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
410 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500411{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500412 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
413 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
414 return true;
415
416 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
417 return false;
418
Willem de Bruijn8c04e2a2017-04-12 19:24:35 -0400419 if (!IP6CB(skb)->iif)
Willem de Bruijnc247f052015-03-07 20:33:22 -0500420 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500421
Willem de Bruijnc247f052015-03-07 20:33:22 -0500422 return true;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500423}
424
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900425/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * Handle MSG_ERRQUEUE
427 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100428int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct ipv6_pinfo *np = inet6_sk(sk);
431 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400432 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100433 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct {
435 struct sock_extended_err ee;
436 struct sockaddr_in6 offender;
437 } errhdr;
438 int err;
439 int copied;
440
441 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400442 skb = sock_dequeue_err_skb(sk);
Ian Morris63159f22015-03-29 14:00:04 +0100443 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 goto out;
445
446 copied = skb->len;
447 if (copied > len) {
448 msg->msg_flags |= MSG_TRUNC;
449 copied = len;
450 }
David S. Miller51f3d022014-11-05 16:46:40 -0500451 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700452 if (unlikely(err)) {
453 kfree_skb(skb);
454 return err;
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 sock_recv_timestamp(msg, sk, skb);
457
458 serr = SKB_EXT_ERR(skb);
459
Julian Anastasov34b99df2015-06-23 08:34:39 +0300460 if (sin && ipv6_datagram_support_addr(serr)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700461 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 sin->sin6_family = AF_INET6;
463 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900464 sin->sin6_port = serr->port;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000465 if (skb->protocol == htons(ETH_P_IPV6)) {
YOSHIFUJI Hideaki / 吉藤英明6c40d102013-01-08 06:44:23 +0000466 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
467 struct ipv6hdr, daddr);
468 sin->sin6_addr = ip6h->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (np->sndflow)
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000470 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000471 sin->sin6_scope_id =
472 ipv6_iface_scope_id(&sin->sin6_addr,
473 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700475 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
476 &sin->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000477 sin->sin6_scope_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100479 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
483 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500484 memset(sin, 0, sizeof(*sin));
Willem de Bruijnc247f052015-03-07 20:33:22 -0500485
486 if (ip6_datagram_support_cmsg(skb, serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 sin->sin6_family = AF_INET6;
Willem de Bruijnc247f052015-03-07 20:33:22 -0500488 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100489 ip6_datagram_recv_common_ctl(sk, msg, skb);
Brian Haleyd40a4de2010-05-03 15:44:27 +0000490 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000491 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100493 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000494 sin->sin6_scope_id =
495 ipv6_iface_scope_id(&sin->sin6_addr,
496 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700498 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
499 &sin->sin6_addr);
Willem de Bruijnf8121162015-01-15 13:18:40 -0500500 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 ip_cmsg_recv(msg, skb);
502 }
503 }
504
505 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
506
507 /* Now we could try to dump offended packet options */
508
509 msg->msg_flags |= MSG_ERRQUEUE;
510 err = copied;
511
Eric Dumazet960a2622016-04-21 22:27:32 -0700512 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513out:
514 return err;
515}
Chris Elstona495f832012-04-29 21:48:53 +0000516EXPORT_SYMBOL_GPL(ipv6_recv_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Brian Haley4b340ae2010-04-23 11:26:09 +0000518/*
519 * Handle IPV6_RECVPATHMTU
520 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100521int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
522 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000523{
524 struct ipv6_pinfo *np = inet6_sk(sk);
525 struct sk_buff *skb;
Brian Haley4b340ae2010-04-23 11:26:09 +0000526 struct ip6_mtuinfo mtu_info;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100527 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Brian Haley4b340ae2010-04-23 11:26:09 +0000528 int err;
529 int copied;
530
531 err = -EAGAIN;
532 skb = xchg(&np->rxpmtu, NULL);
Ian Morris63159f22015-03-29 14:00:04 +0100533 if (!skb)
Brian Haley4b340ae2010-04-23 11:26:09 +0000534 goto out;
535
536 copied = skb->len;
537 if (copied > len) {
538 msg->msg_flags |= MSG_TRUNC;
539 copied = len;
540 }
David S. Miller51f3d022014-11-05 16:46:40 -0500541 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Brian Haley4b340ae2010-04-23 11:26:09 +0000542 if (err)
543 goto out_free_skb;
544
545 sock_recv_timestamp(msg, sk, skb);
546
547 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
548
Brian Haley4b340ae2010-04-23 11:26:09 +0000549 if (sin) {
550 sin->sin6_family = AF_INET6;
551 sin->sin6_flowinfo = 0;
552 sin->sin6_port = 0;
553 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000554 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100555 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000556 }
557
558 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
559
560 err = copied;
561
562out_free_skb:
563 kfree_skb(skb);
564out:
565 return err;
566}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100569void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
570 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct ipv6_pinfo *np = inet6_sk(sk);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100573 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (np->rxopt.bits.rxinfo) {
576 struct in6_pktinfo src_info;
577
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100578 if (is_ipv6) {
579 src_info.ipi6_ifindex = IP6CB(skb)->iif;
580 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
581 } else {
582 src_info.ipi6_ifindex =
583 PKTINFO_SKB_CB(skb)->ipi_ifindex;
584 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
585 &src_info.ipi6_addr);
586 }
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500587
588 if (src_info.ipi6_ifindex >= 0)
589 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
590 sizeof(src_info), &src_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100592}
593
594void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
595 struct sk_buff *skb)
596{
597 struct ipv6_pinfo *np = inet6_sk(sk);
598 struct inet6_skb_parm *opt = IP6CB(skb);
599 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700602 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
604 }
605
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900606 if (np->rxopt.bits.rxtclass) {
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +0000607 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900608 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
609 }
610
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000611 if (np->rxopt.bits.rxflow) {
612 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
613 if (flowinfo)
614 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900616
617 /* HbH is allowed only once */
Florian Westphal8b58a392015-07-08 23:32:12 +0200618 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
619 u8 *ptr = nh + sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
621 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900622
623 if (opt->lastopt &&
624 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
625 /*
626 * Silly enough, but we need to reparse in order to
627 * report extension headers (except for HbH)
628 * in order.
629 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900630 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900631 * (and WILL NOT be) defined because
632 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
633 */
634 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700635 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900636
637 while (off <= opt->lastopt) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000638 unsigned int len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700639 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900640
Eldad Zackb5a42572012-04-01 07:49:03 +0000641 switch (nexthdr) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900642 case IPPROTO_DSTOPTS:
643 nexthdr = ptr[0];
644 len = (ptr[1] + 1) << 3;
645 if (np->rxopt.bits.dstopts)
646 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
647 break;
648 case IPPROTO_ROUTING:
649 nexthdr = ptr[0];
650 len = (ptr[1] + 1) << 3;
651 if (np->rxopt.bits.srcrt)
652 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
653 break;
654 case IPPROTO_AH:
655 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900656 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900657 break;
658 default:
659 nexthdr = ptr[0];
660 len = (ptr[1] + 1) << 3;
661 break;
662 }
663
664 off += len;
665 }
666 }
667
668 /* socket options in old style */
669 if (np->rxopt.bits.rxoinfo) {
670 struct in6_pktinfo src_info;
671
672 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000673 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900674 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
675 }
676 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700677 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900678 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
679 }
Florian Westphal8b58a392015-07-08 23:32:12 +0200680 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
681 u8 *ptr = nh + sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900682 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
683 }
684 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700685 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900686 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900688 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700689 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900690 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900692 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700693 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900694 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200696 if (np->rxopt.bits.rxorigdstaddr) {
697 struct sockaddr_in6 sin6;
Willem de Bruijn03fbf2b2018-07-23 19:36:48 -0400698 __be16 *ports;
699 int end;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200700
Willem de Bruijn03fbf2b2018-07-23 19:36:48 -0400701 end = skb_transport_offset(skb) + 4;
702 if (end <= 0 || pskb_may_pull(skb, end)) {
Balazs Scheidler6c468622010-10-21 16:08:28 +0200703 /* All current transport protocols have the port numbers in the
704 * first four bytes of the transport header and this function is
705 * written with this assumption in mind.
706 */
Willem de Bruijn03fbf2b2018-07-23 19:36:48 -0400707 ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200708
709 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000710 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200711 sin6.sin6_port = ports[1];
712 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000713 sin6.sin6_scope_id =
714 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
715 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200716
717 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
718 }
719 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100720}
721
722void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
723 struct sk_buff *skb)
724{
725 ip6_datagram_recv_common_ctl(sk, msg, skb);
726 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
Tom Parkin8e72d372013-01-31 01:02:25 +0000728EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Tom Parkin73df66f2013-01-31 01:02:24 +0000730int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
731 struct msghdr *msg, struct flowi6 *fl6,
Wei Wang26879da2016-05-02 21:40:07 -0700732 struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct in6_pktinfo *src_info;
735 struct cmsghdr *cmsg;
736 struct ipv6_rt_hdr *rthdr;
737 struct ipv6_opt_hdr *hdr;
Wei Wang26879da2016-05-02 21:40:07 -0700738 struct ipv6_txoptions *opt = ipc6->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int len;
740 int err = 0;
741
Gu Zhengf95b4142014-12-11 11:22:04 +0800742 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (!CMSG_OK(msg, cmsg)) {
746 err = -EINVAL;
747 goto exit_f;
748 }
749
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400750 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700751 err = __sock_cmsg_send(sk, msg, cmsg, sockc);
752 if (err)
753 return err;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400754 continue;
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (cmsg->cmsg_level != SOL_IPV6)
758 continue;
759
760 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900761 case IPV6_PKTINFO:
762 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900763 {
764 struct net_device *dev = NULL;
765
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900766 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 err = -EINVAL;
768 goto exit_f;
769 }
770
771 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500774 if (fl6->flowi6_oif &&
775 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500777 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900780 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Eric Dumazet536b2e92009-11-02 12:21:06 +0100782 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500783 if (fl6->flowi6_oif) {
784 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100785 if (!dev) {
786 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900787 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100788 }
789 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
790 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900791 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100792 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900793
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900794 if (addr_type != IPV6_ADDR_ANY) {
795 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000796 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000797 !ipv6_chk_addr(net, &src_info->ipi6_addr,
FX Le Bail7c90cc22014-01-22 07:42:37 +0100798 strict ? dev : NULL, 0) &&
799 !ipv6_chk_acast_addr_src(net, dev,
800 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900801 err = -EINVAL;
802 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000803 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900805
Eric Dumazet536b2e92009-11-02 12:21:06 +0100806 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900808 if (err)
809 goto exit_f;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900815 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 err = -EINVAL;
817 goto exit_f;
818 }
819
David S. Miller4c9483b2011-03-12 16:22:43 -0500820 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
821 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 err = -EINVAL;
823 goto exit_f;
824 }
825 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500826 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 break;
828
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900829 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900831 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 err = -EINVAL;
833 goto exit_f;
834 }
835
836 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
837 len = ((hdr->hdrlen + 1) << 3);
838 if (cmsg->cmsg_len < CMSG_LEN(len)) {
839 err = -EINVAL;
840 goto exit_f;
841 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000842 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 err = -EPERM;
844 goto exit_f;
845 }
846 opt->opt_nflen += len;
847 opt->hopopt = hdr;
848 break;
849
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900850 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900851 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 err = -EINVAL;
853 goto exit_f;
854 }
855
856 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
857 len = ((hdr->hdrlen + 1) << 3);
858 if (cmsg->cmsg_len < CMSG_LEN(len)) {
859 err = -EINVAL;
860 goto exit_f;
861 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000862 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 err = -EPERM;
864 goto exit_f;
865 }
866 if (opt->dst1opt) {
867 err = -EINVAL;
868 goto exit_f;
869 }
870 opt->opt_flen += len;
871 opt->dst1opt = hdr;
872 break;
873
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900874 case IPV6_DSTOPTS:
875 case IPV6_RTHDRDSTOPTS:
876 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
877 err = -EINVAL;
878 goto exit_f;
879 }
880
881 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
882 len = ((hdr->hdrlen + 1) << 3);
883 if (cmsg->cmsg_len < CMSG_LEN(len)) {
884 err = -EINVAL;
885 goto exit_f;
886 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000887 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900888 err = -EPERM;
889 goto exit_f;
890 }
891 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
892 opt->opt_flen += len;
893 opt->dst1opt = hdr;
894 } else {
895 opt->opt_nflen += len;
896 opt->dst0opt = hdr;
897 }
898 break;
899
900 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900902 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 err = -EINVAL;
904 goto exit_f;
905 }
906
907 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
908
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700909 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000910#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700911 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800912 if (rthdr->hdrlen != 2 ||
913 rthdr->segments_left != 1) {
914 err = -EINVAL;
915 goto exit_f;
916 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700917 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700918#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700919 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 err = -EINVAL;
921 goto exit_f;
922 }
923
924 len = ((rthdr->hdrlen + 1) << 3);
925
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900926 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 err = -EINVAL;
928 goto exit_f;
929 }
930
931 /* segments left must also match */
932 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
933 err = -EINVAL;
934 goto exit_f;
935 }
936
937 opt->opt_nflen += len;
938 opt->srcrt = rthdr;
939
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900940 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
942
943 opt->opt_nflen += dsthdrlen;
944 opt->dst0opt = opt->dst1opt;
945 opt->dst1opt = NULL;
946 opt->opt_flen -= dsthdrlen;
947 }
948
949 break;
950
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900951 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 case IPV6_HOPLIMIT:
953 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
954 err = -EINVAL;
955 goto exit_f;
956 }
957
Wei Wang26879da2016-05-02 21:40:07 -0700958 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
959 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
Shan Weie8766fc2008-06-10 15:50:55 +0800960 err = -EINVAL;
961 goto exit_f;
962 }
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
965
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900966 case IPV6_TCLASS:
967 {
968 int tc;
969
970 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000971 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900972 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900973
974 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700975 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900976 goto exit_f;
977
978 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700979 ipc6->tclass = tc;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900980
981 break;
982 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000983
984 case IPV6_DONTFRAG:
985 {
986 int df;
987
988 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000989 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +0000990 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +0000991
992 df = *(int *)CMSG_DATA(cmsg);
993 if (df < 0 || df > 1)
994 goto exit_f;
995
996 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700997 ipc6->dontfrag = df;
Brian Haley13b52cd2010-04-23 11:26:08 +0000998
999 break;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -08001002 net_dbg_ratelimited("invalid cmsg type: %d\n",
1003 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -07001005 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
1009exit_f:
1010 return err;
1011}
Tom Parkin73df66f2013-01-31 01:02:24 +00001012EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001013
1014void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1015 __u16 srcp, __u16 destp, int bucket)
1016{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001017 const struct in6_addr *dest, *src;
1018
Eric Dumazetefe42082013-10-03 15:42:29 -07001019 dest = &sp->sk_v6_daddr;
1020 src = &sp->sk_v6_rcv_saddr;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001021 seq_printf(seq,
1022 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001023 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001024 bucket,
1025 src->s6_addr32[0], src->s6_addr32[1],
1026 src->s6_addr32[2], src->s6_addr32[3], srcp,
1027 dest->s6_addr32[0], dest->s6_addr32[1],
1028 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1029 sp->sk_state,
1030 sk_wmem_alloc_get(sp),
1031 sk_rmem_alloc_get(sp),
1032 0, 0L, 0,
1033 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1034 0,
1035 sock_i_ino(sp),
1036 atomic_read(&sp->sk_refcnt), sp,
1037 atomic_read(&sp->sk_drops));
1038}