blob: 129b7a8794cadc3259533cabad21be55f240da18 [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;
Lorenzo Colitti50442922016-11-04 02:23:43 +090057 fl6->flowi6_uid = sk->sk_uid;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -070058
59 if (!fl6->flowi6_oif)
60 fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
61
62 if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr))
63 fl6->flowi6_oif = np->mcast_oif;
64
65 security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
66}
67
Martin KaFai Lau33c162a2016-04-11 15:29:36 -070068int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -070069{
70 struct ip6_flowlabel *flowlabel = NULL;
71 struct in6_addr *final_p, final;
72 struct ipv6_txoptions *opt;
73 struct dst_entry *dst;
74 struct inet_sock *inet = inet_sk(sk);
75 struct ipv6_pinfo *np = inet6_sk(sk);
76 struct flowi6 fl6;
77 int err = 0;
78
79 if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
80 flowlabel = fl6_sock_lookup(sk, np->flow_label);
81 if (!flowlabel)
82 return -EINVAL;
83 }
84 ip6_datagram_flow_key_init(&fl6, sk);
85
86 rcu_read_lock();
87 opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
88 final_p = fl6_update_dst(&fl6, opt, &final);
89 rcu_read_unlock();
90
91 dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
92 if (IS_ERR(dst)) {
93 err = PTR_ERR(dst);
94 goto out;
95 }
96
Martin KaFai Lau33c162a2016-04-11 15:29:36 -070097 if (fix_sk_saddr) {
98 if (ipv6_addr_any(&np->saddr))
99 np->saddr = fl6.saddr;
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700100
Martin KaFai Lau33c162a2016-04-11 15:29:36 -0700101 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
102 sk->sk_v6_rcv_saddr = fl6.saddr;
103 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
104 if (sk->sk_prot->rehash)
105 sk->sk_prot->rehash(sk);
106 }
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700107 }
108
109 ip6_dst_store(sk, dst,
110 ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
111 &sk->sk_v6_daddr : NULL,
112#ifdef CONFIG_IPV6_SUBTREES
113 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
114 &np->saddr :
115#endif
116 NULL);
117
118out:
119 fl6_sock_release(flowlabel);
120 return err;
121}
122
Martin KaFai Laue646b652016-04-11 15:29:37 -0700123void ip6_datagram_release_cb(struct sock *sk)
124{
125 struct dst_entry *dst;
126
127 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
128 return;
129
130 rcu_read_lock();
131 dst = __sk_dst_get(sk);
132 if (!dst || !dst->obsolete ||
133 dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
134 rcu_read_unlock();
135 return;
136 }
137 rcu_read_unlock();
138
139 ip6_datagram_dst_update(sk, false);
140}
141EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
142
Guillaume Nault0382a252016-11-29 13:09:44 +0100143int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
144 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
Ian Morris67ba4152014-08-24 21:53:10 +0100147 struct inet_sock *inet = inet_sk(sk);
148 struct ipv6_pinfo *np = inet6_sk(sk);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700149 struct in6_addr *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int addr_type;
151 int err;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700152 __be32 fl6_flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (usin->sin6_family == AF_INET) {
155 if (__ipv6_only_sock(sk))
156 return -EAFNOSUPPORT;
Eric Dumazet03645a12015-07-14 08:10:22 +0200157 err = __ip4_datagram_connect(sk, uaddr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 goto ipv4_connected;
159 }
160
161 if (addr_len < SIN6_LEN_RFC2133)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900162 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900164 if (usin->sin6_family != AF_INET6)
165 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700167 if (np->sndflow)
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700168 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jonathan T. Leighton12ec2562017-05-23 21:53:33 -0400170 if (ipv6_addr_any(&usin->sin6_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /*
172 * connect to self
173 */
Jonathan T. Leighton12ec2562017-05-23 21:53:33 -0400174 if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
175 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
176 &usin->sin6_addr);
177 else
178 usin->sin6_addr = in6addr_loopback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180
Jonathan T. Leighton12ec2562017-05-23 21:53:33 -0400181 addr_type = ipv6_addr_type(&usin->sin6_addr);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 daddr = &usin->sin6_addr;
184
Jonathan T. Leighton12ec2562017-05-23 21:53:33 -0400185 if (addr_type & IPV6_ADDR_MAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct sockaddr_in sin;
187
188 if (__ipv6_only_sock(sk)) {
189 err = -ENETUNREACH;
190 goto out;
191 }
192 sin.sin_family = AF_INET;
193 sin.sin_addr.s_addr = daddr->s6_addr32[3];
194 sin.sin_port = usin->sin6_port;
195
Eric Dumazet03645a12015-07-14 08:10:22 +0200196 err = __ip4_datagram_connect(sk,
197 (struct sockaddr *) &sin,
198 sizeof(sin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200ipv4_connected:
201 if (err)
202 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900203
Eric Dumazetefe42082013-10-03 15:42:29 -0700204 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Max Matveevc15fea22011-08-05 03:56:30 -0700206 if (ipv6_addr_any(&np->saddr) ||
207 ipv6_mapped_addr_any(&np->saddr))
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000208 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Eric Dumazetefe42082013-10-03 15:42:29 -0700210 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
211 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000212 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
Eric Dumazetefe42082013-10-03 15:42:29 -0700213 &sk->sk_v6_rcv_saddr);
Eric Dumazet719f8352010-09-08 05:08:44 +0000214 if (sk->sk_prot->rehash)
215 sk->sk_prot->rehash(sk);
216 }
Brian Haleyb301e822009-10-07 13:58:25 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 goto out;
219 }
220
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000221 if (__ipv6_addr_needs_scope_id(addr_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (addr_len >= sizeof(struct sockaddr_in6) &&
223 usin->sin6_scope_id) {
224 if (sk->sk_bound_dev_if &&
225 sk->sk_bound_dev_if != usin->sin6_scope_id) {
226 err = -EINVAL;
227 goto out;
228 }
229 sk->sk_bound_dev_if = usin->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
Brian Haley1ac4f002008-01-08 23:52:21 -0800232 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
233 sk->sk_bound_dev_if = np->mcast_oif;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Connect to link-local address requires an interface */
236 if (!sk->sk_bound_dev_if) {
237 err = -EINVAL;
238 goto out;
239 }
240 }
241
Eric Dumazetefe42082013-10-03 15:42:29 -0700242 sk->sk_v6_daddr = *daddr;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700243 np->flow_label = fl6_flowlabel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000245 inet->inet_dport = usin->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /*
248 * Check for a route to destination an obtain the
249 * destination cache for it.
250 */
251
Martin KaFai Lau33c162a2016-04-11 15:29:36 -0700252 err = ip6_datagram_dst_update(sk, true);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700253 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 sk->sk_state = TCP_ESTABLISHED;
Tom Herbert877d1f62015-07-28 16:02:05 -0700257 sk_set_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return err;
260}
Guillaume Nault0382a252016-11-29 13:09:44 +0100261EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
Eric Dumazet03645a12015-07-14 08:10:22 +0200262
263int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
264{
265 int res;
266
267 lock_sock(sk);
268 res = __ip6_datagram_connect(sk, uaddr, addr_len);
269 release_sock(sk);
270 return res;
271}
Chris Elstona495f832012-04-29 21:48:53 +0000272EXPORT_SYMBOL_GPL(ip6_datagram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Hannes Frederic Sowa82b276c2014-01-20 05:16:39 +0100274int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
275 int addr_len)
276{
277 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
278 if (sin6->sin6_family != AF_INET6)
279 return -EAFNOSUPPORT;
280 return ip6_datagram_connect(sk, uaddr, addr_len);
281}
282EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
283
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900284void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4adc2006-11-14 20:56:00 -0800285 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300288 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct sock_exterr_skb *serr;
290
291 if (!np->recverr)
292 return;
293
294 skb = skb_clone(skb, GFP_ATOMIC);
295 if (!skb)
296 return;
297
Brian Haleyd40a4de2010-05-03 15:44:27 +0000298 skb->protocol = htons(ETH_P_IPV6);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 serr = SKB_EXT_ERR(skb);
301 serr->ee.ee_errno = err;
302 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900303 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 serr->ee.ee_code = icmph->icmp6_code;
305 serr->ee.ee_pad = 0;
306 serr->ee.ee_info = info;
307 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700308 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
309 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 serr->port = port;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 __skb_pull(skb, payload - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300313 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (sock_queue_err_skb(sk, skb))
316 kfree_skb(skb);
317}
318
David S. Miller4c9483b2011-03-12 16:22:43 -0500319void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700321 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct sock_exterr_skb *serr;
323 struct ipv6hdr *iph;
324 struct sk_buff *skb;
325
326 if (!np->recverr)
327 return;
328
329 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
330 if (!skb)
331 return;
332
Brian Haleyd40a4de2010-05-03 15:44:27 +0000333 skb->protocol = htons(ETH_P_IPV6);
334
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300335 skb_put(skb, sizeof(struct ipv6hdr));
336 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700337 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000338 iph->daddr = fl6->daddr;
Eric Dumazet7492dfe2019-01-08 04:06:14 -0800339 ip6_flow_hdr(iph, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 serr = SKB_EXT_ERR(skb);
342 serr->ee.ee_errno = err;
343 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900344 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 serr->ee.ee_code = 0;
346 serr->ee.ee_pad = 0;
347 serr->ee.ee_info = info;
348 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700349 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500350 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700352 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300353 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (sock_queue_err_skb(sk, skb))
356 kfree_skb(skb);
357}
358
David S. Miller4c9483b2011-03-12 16:22:43 -0500359void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000360{
361 struct ipv6_pinfo *np = inet6_sk(sk);
362 struct ipv6hdr *iph;
363 struct sk_buff *skb;
364 struct ip6_mtuinfo *mtu_info;
365
366 if (!np->rxopt.bits.rxpmtu)
367 return;
368
369 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
370 if (!skb)
371 return;
372
373 skb_put(skb, sizeof(struct ipv6hdr));
374 skb_reset_network_header(skb);
375 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000376 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000377
378 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000379
380 mtu_info->ip6m_mtu = mtu;
381 mtu_info->ip6m_addr.sin6_family = AF_INET6;
382 mtu_info->ip6m_addr.sin6_port = 0;
383 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500384 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000385 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000386
387 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
388 skb_reset_transport_header(skb);
389
390 skb = xchg(&np->rxpmtu, skb);
391 kfree_skb(skb);
392}
393
Julian Anastasov34b99df2015-06-23 08:34:39 +0300394/* For some errors we have valid addr_offset even with zero payload and
395 * zero port. Also, addr_offset should be supported if port is set.
396 */
397static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
398{
399 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
400 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
401 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
402}
403
Willem de Bruijnc247f052015-03-07 20:33:22 -0500404/* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
405 *
406 * At one point, excluding local errors was a quick test to identify icmp/icmp6
407 * errors. This is no longer true, but the test remained, so the v6 stack,
408 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
Willem de Bruijnc247f052015-03-07 20:33:22 -0500409 */
410static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
411 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500412{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500413 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
414 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
415 return true;
416
417 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
418 return false;
419
Willem de Bruijn8c04e2a2017-04-12 19:24:35 -0400420 if (!IP6CB(skb)->iif)
Willem de Bruijnc247f052015-03-07 20:33:22 -0500421 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500422
Willem de Bruijnc247f052015-03-07 20:33:22 -0500423 return true;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500424}
425
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900426/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * Handle MSG_ERRQUEUE
428 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100429int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct ipv6_pinfo *np = inet6_sk(sk);
432 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400433 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100434 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct {
436 struct sock_extended_err ee;
437 struct sockaddr_in6 offender;
438 } errhdr;
439 int err;
440 int copied;
441
442 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400443 skb = sock_dequeue_err_skb(sk);
Ian Morris63159f22015-03-29 14:00:04 +0100444 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto out;
446
447 copied = skb->len;
448 if (copied > len) {
449 msg->msg_flags |= MSG_TRUNC;
450 copied = len;
451 }
David S. Miller51f3d022014-11-05 16:46:40 -0500452 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700453 if (unlikely(err)) {
454 kfree_skb(skb);
455 return err;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 sock_recv_timestamp(msg, sk, skb);
458
459 serr = SKB_EXT_ERR(skb);
460
Julian Anastasov34b99df2015-06-23 08:34:39 +0300461 if (sin && ipv6_datagram_support_addr(serr)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700462 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 sin->sin6_family = AF_INET6;
464 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900465 sin->sin6_port = serr->port;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000466 if (skb->protocol == htons(ETH_P_IPV6)) {
YOSHIFUJI Hideaki / 吉藤英明6c40d102013-01-08 06:44:23 +0000467 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
468 struct ipv6hdr, daddr);
469 sin->sin6_addr = ip6h->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (np->sndflow)
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000471 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000472 sin->sin6_scope_id =
473 ipv6_iface_scope_id(&sin->sin6_addr,
474 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700476 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
477 &sin->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000478 sin->sin6_scope_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100480 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
483 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
484 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500485 memset(sin, 0, sizeof(*sin));
Willem de Bruijnc247f052015-03-07 20:33:22 -0500486
487 if (ip6_datagram_support_cmsg(skb, serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 sin->sin6_family = AF_INET6;
Willem de Bruijnc247f052015-03-07 20:33:22 -0500489 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100490 ip6_datagram_recv_common_ctl(sk, msg, skb);
Brian Haleyd40a4de2010-05-03 15:44:27 +0000491 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000492 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100494 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000495 sin->sin6_scope_id =
496 ipv6_iface_scope_id(&sin->sin6_addr,
497 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700499 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
500 &sin->sin6_addr);
Willem de Bruijnf8121162015-01-15 13:18:40 -0500501 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 ip_cmsg_recv(msg, skb);
503 }
504 }
505
506 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
507
508 /* Now we could try to dump offended packet options */
509
510 msg->msg_flags |= MSG_ERRQUEUE;
511 err = copied;
512
Eric Dumazet960a2622016-04-21 22:27:32 -0700513 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514out:
515 return err;
516}
Chris Elstona495f832012-04-29 21:48:53 +0000517EXPORT_SYMBOL_GPL(ipv6_recv_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Brian Haley4b340ae2010-04-23 11:26:09 +0000519/*
520 * Handle IPV6_RECVPATHMTU
521 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100522int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
523 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000524{
525 struct ipv6_pinfo *np = inet6_sk(sk);
526 struct sk_buff *skb;
Brian Haley4b340ae2010-04-23 11:26:09 +0000527 struct ip6_mtuinfo mtu_info;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100528 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Brian Haley4b340ae2010-04-23 11:26:09 +0000529 int err;
530 int copied;
531
532 err = -EAGAIN;
533 skb = xchg(&np->rxpmtu, NULL);
Ian Morris63159f22015-03-29 14:00:04 +0100534 if (!skb)
Brian Haley4b340ae2010-04-23 11:26:09 +0000535 goto out;
536
537 copied = skb->len;
538 if (copied > len) {
539 msg->msg_flags |= MSG_TRUNC;
540 copied = len;
541 }
David S. Miller51f3d022014-11-05 16:46:40 -0500542 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Brian Haley4b340ae2010-04-23 11:26:09 +0000543 if (err)
544 goto out_free_skb;
545
546 sock_recv_timestamp(msg, sk, skb);
547
548 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
549
Brian Haley4b340ae2010-04-23 11:26:09 +0000550 if (sin) {
551 sin->sin6_family = AF_INET6;
552 sin->sin6_flowinfo = 0;
553 sin->sin6_port = 0;
554 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000555 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100556 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000557 }
558
559 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
560
561 err = copied;
562
563out_free_skb:
564 kfree_skb(skb);
565out:
566 return err;
567}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100570void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
571 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct ipv6_pinfo *np = inet6_sk(sk);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100574 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (np->rxopt.bits.rxinfo) {
577 struct in6_pktinfo src_info;
578
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100579 if (is_ipv6) {
580 src_info.ipi6_ifindex = IP6CB(skb)->iif;
581 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
582 } else {
583 src_info.ipi6_ifindex =
584 PKTINFO_SKB_CB(skb)->ipi_ifindex;
585 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
586 &src_info.ipi6_addr);
587 }
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500588
589 if (src_info.ipi6_ifindex >= 0)
590 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
591 sizeof(src_info), &src_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100593}
594
595void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
596 struct sk_buff *skb)
597{
598 struct ipv6_pinfo *np = inet6_sk(sk);
599 struct inet6_skb_parm *opt = IP6CB(skb);
600 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700603 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
605 }
606
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900607 if (np->rxopt.bits.rxtclass) {
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +0000608 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900609 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
610 }
611
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000612 if (np->rxopt.bits.rxflow) {
613 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
614 if (flowinfo)
615 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900617
618 /* HbH is allowed only once */
Florian Westphal8b58a392015-07-08 23:32:12 +0200619 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
620 u8 *ptr = nh + sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
622 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900623
624 if (opt->lastopt &&
625 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
626 /*
627 * Silly enough, but we need to reparse in order to
628 * report extension headers (except for HbH)
629 * in order.
630 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900631 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900632 * (and WILL NOT be) defined because
633 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
634 */
635 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700636 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900637
638 while (off <= opt->lastopt) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000639 unsigned int len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700640 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900641
Eldad Zackb5a42572012-04-01 07:49:03 +0000642 switch (nexthdr) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900643 case IPPROTO_DSTOPTS:
644 nexthdr = ptr[0];
645 len = (ptr[1] + 1) << 3;
646 if (np->rxopt.bits.dstopts)
647 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
648 break;
649 case IPPROTO_ROUTING:
650 nexthdr = ptr[0];
651 len = (ptr[1] + 1) << 3;
652 if (np->rxopt.bits.srcrt)
653 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
654 break;
655 case IPPROTO_AH:
656 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900657 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900658 break;
659 default:
660 nexthdr = ptr[0];
661 len = (ptr[1] + 1) << 3;
662 break;
663 }
664
665 off += len;
666 }
667 }
668
669 /* socket options in old style */
670 if (np->rxopt.bits.rxoinfo) {
671 struct in6_pktinfo src_info;
672
673 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000674 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900675 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
676 }
677 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700678 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900679 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
680 }
Florian Westphal8b58a392015-07-08 23:32:12 +0200681 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
682 u8 *ptr = nh + sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900683 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
684 }
685 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700686 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900687 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900689 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700690 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900691 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900693 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700694 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900695 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200697 if (np->rxopt.bits.rxorigdstaddr) {
698 struct sockaddr_in6 sin6;
Willem de Bruijn9cd05632019-01-07 16:47:33 -0500699 __be16 _ports[2], *ports;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200700
Willem de Bruijn9cd05632019-01-07 16:47:33 -0500701 ports = skb_header_pointer(skb, skb_transport_offset(skb),
702 sizeof(_ports), &_ports);
703 if (ports) {
Balazs Scheidler6c468622010-10-21 16:08:28 +0200704 /* All current transport protocols have the port numbers in the
705 * first four bytes of the transport header and this function is
706 * written with this assumption in mind.
707 */
Balazs Scheidler6c468622010-10-21 16:08:28 +0200708 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000709 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200710 sin6.sin6_port = ports[1];
711 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000712 sin6.sin6_scope_id =
713 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
714 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200715
716 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
717 }
718 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100719}
720
721void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
722 struct sk_buff *skb)
723{
724 ip6_datagram_recv_common_ctl(sk, msg, skb);
725 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
Tom Parkin8e72d372013-01-31 01:02:25 +0000727EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Tom Parkin73df66f2013-01-31 01:02:24 +0000729int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
730 struct msghdr *msg, struct flowi6 *fl6,
Wei Wang26879da2016-05-02 21:40:07 -0700731 struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 struct in6_pktinfo *src_info;
734 struct cmsghdr *cmsg;
735 struct ipv6_rt_hdr *rthdr;
736 struct ipv6_opt_hdr *hdr;
Wei Wang26879da2016-05-02 21:40:07 -0700737 struct ipv6_txoptions *opt = ipc6->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int len;
739 int err = 0;
740
Gu Zhengf95b4142014-12-11 11:22:04 +0800741 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if (!CMSG_OK(msg, cmsg)) {
745 err = -EINVAL;
746 goto exit_f;
747 }
748
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400749 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700750 err = __sock_cmsg_send(sk, msg, cmsg, sockc);
751 if (err)
752 return err;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400753 continue;
754 }
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (cmsg->cmsg_level != SOL_IPV6)
757 continue;
758
759 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900760 case IPV6_PKTINFO:
761 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900762 {
763 struct net_device *dev = NULL;
764
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900765 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 err = -EINVAL;
767 goto exit_f;
768 }
769
770 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500773 if (fl6->flowi6_oif &&
774 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500776 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900779 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Eric Dumazet536b2e92009-11-02 12:21:06 +0100781 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500782 if (fl6->flowi6_oif) {
783 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100784 if (!dev) {
785 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900786 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100787 }
788 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
789 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900790 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100791 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900792
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900793 if (addr_type != IPV6_ADDR_ANY) {
794 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000795 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000796 !ipv6_chk_addr(net, &src_info->ipi6_addr,
FX Le Bail7c90cc22014-01-22 07:42:37 +0100797 strict ? dev : NULL, 0) &&
798 !ipv6_chk_acast_addr_src(net, dev,
799 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900800 err = -EINVAL;
801 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000802 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900804
Eric Dumazet536b2e92009-11-02 12:21:06 +0100805 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900807 if (err)
808 goto exit_f;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900814 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 err = -EINVAL;
816 goto exit_f;
817 }
818
David S. Miller4c9483b2011-03-12 16:22:43 -0500819 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
820 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 err = -EINVAL;
822 goto exit_f;
823 }
824 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500825 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
827
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900828 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900830 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 err = -EINVAL;
832 goto exit_f;
833 }
834
835 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
836 len = ((hdr->hdrlen + 1) << 3);
837 if (cmsg->cmsg_len < CMSG_LEN(len)) {
838 err = -EINVAL;
839 goto exit_f;
840 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000841 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 err = -EPERM;
843 goto exit_f;
844 }
845 opt->opt_nflen += len;
846 opt->hopopt = hdr;
847 break;
848
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900849 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900850 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 err = -EINVAL;
852 goto exit_f;
853 }
854
855 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
856 len = ((hdr->hdrlen + 1) << 3);
857 if (cmsg->cmsg_len < CMSG_LEN(len)) {
858 err = -EINVAL;
859 goto exit_f;
860 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000861 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 err = -EPERM;
863 goto exit_f;
864 }
865 if (opt->dst1opt) {
866 err = -EINVAL;
867 goto exit_f;
868 }
869 opt->opt_flen += len;
870 opt->dst1opt = hdr;
871 break;
872
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900873 case IPV6_DSTOPTS:
874 case IPV6_RTHDRDSTOPTS:
875 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
876 err = -EINVAL;
877 goto exit_f;
878 }
879
880 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
881 len = ((hdr->hdrlen + 1) << 3);
882 if (cmsg->cmsg_len < CMSG_LEN(len)) {
883 err = -EINVAL;
884 goto exit_f;
885 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000886 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900887 err = -EPERM;
888 goto exit_f;
889 }
890 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
891 opt->opt_flen += len;
892 opt->dst1opt = hdr;
893 } else {
894 opt->opt_nflen += len;
895 opt->dst0opt = hdr;
896 }
897 break;
898
899 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900901 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 err = -EINVAL;
903 goto exit_f;
904 }
905
906 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
907
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700908 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000909#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700910 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800911 if (rthdr->hdrlen != 2 ||
912 rthdr->segments_left != 1) {
913 err = -EINVAL;
914 goto exit_f;
915 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700916 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700917#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700918 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 err = -EINVAL;
920 goto exit_f;
921 }
922
923 len = ((rthdr->hdrlen + 1) << 3);
924
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900925 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 err = -EINVAL;
927 goto exit_f;
928 }
929
930 /* segments left must also match */
931 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
932 err = -EINVAL;
933 goto exit_f;
934 }
935
936 opt->opt_nflen += len;
937 opt->srcrt = rthdr;
938
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900939 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
941
942 opt->opt_nflen += dsthdrlen;
943 opt->dst0opt = opt->dst1opt;
944 opt->dst1opt = NULL;
945 opt->opt_flen -= dsthdrlen;
946 }
947
948 break;
949
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900950 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 case IPV6_HOPLIMIT:
952 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
953 err = -EINVAL;
954 goto exit_f;
955 }
956
Wei Wang26879da2016-05-02 21:40:07 -0700957 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
958 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
Shan Weie8766fc2008-06-10 15:50:55 +0800959 err = -EINVAL;
960 goto exit_f;
961 }
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
964
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900965 case IPV6_TCLASS:
966 {
967 int tc;
968
969 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000970 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900971 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900972
973 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700974 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900975 goto exit_f;
976
977 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700978 ipc6->tclass = tc;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900979
980 break;
981 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000982
983 case IPV6_DONTFRAG:
984 {
985 int df;
986
987 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000988 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +0000989 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +0000990
991 df = *(int *)CMSG_DATA(cmsg);
992 if (df < 0 || df > 1)
993 goto exit_f;
994
995 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700996 ipc6->dontfrag = df;
Brian Haley13b52cd2010-04-23 11:26:08 +0000997
998 break;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -08001001 net_dbg_ratelimited("invalid cmsg type: %d\n",
1002 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -07001004 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
1008exit_f:
1009 return err;
1010}
Tom Parkin73df66f2013-01-31 01:02:24 +00001011EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001012
1013void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1014 __u16 srcp, __u16 destp, int bucket)
1015{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001016 const struct in6_addr *dest, *src;
1017
Eric Dumazetefe42082013-10-03 15:42:29 -07001018 dest = &sp->sk_v6_daddr;
1019 src = &sp->sk_v6_rcv_saddr;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001020 seq_printf(seq,
1021 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001022 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001023 bucket,
1024 src->s6_addr32[0], src->s6_addr32[1],
1025 src->s6_addr32[2], src->s6_addr32[3], srcp,
1026 dest->s6_addr32[0], dest->s6_addr32[1],
1027 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1028 sp->sk_state,
1029 sk_wmem_alloc_get(sp),
1030 sk_rmem_alloc_get(sp),
1031 0, 0L, 0,
1032 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1033 0,
1034 sock_i_ino(sp),
1035 atomic_read(&sp->sk_refcnt), sp,
1036 atomic_read(&sp->sk_drops));
1037}