blob: 3cb8134bd2bc50d0ea663da203750fda3c3e9131 [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);
Wei Wangd61f7af2017-06-23 15:25:37 -0700253 if (err) {
254 /* Reset daddr and dport so that udp_v6_early_demux()
255 * fails to find this socket
256 */
257 memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr));
258 inet->inet_dport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto out;
Wei Wangd61f7af2017-06-23 15:25:37 -0700260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 sk->sk_state = TCP_ESTABLISHED;
Tom Herbert877d1f62015-07-28 16:02:05 -0700263 sk_set_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return err;
266}
Guillaume Nault0382a252016-11-29 13:09:44 +0100267EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
Eric Dumazet03645a12015-07-14 08:10:22 +0200268
269int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
270{
271 int res;
272
273 lock_sock(sk);
274 res = __ip6_datagram_connect(sk, uaddr, addr_len);
275 release_sock(sk);
276 return res;
277}
Chris Elstona495f832012-04-29 21:48:53 +0000278EXPORT_SYMBOL_GPL(ip6_datagram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Hannes Frederic Sowa82b276c2014-01-20 05:16:39 +0100280int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
281 int addr_len)
282{
283 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
284 if (sin6->sin6_family != AF_INET6)
285 return -EAFNOSUPPORT;
286 return ip6_datagram_connect(sk, uaddr, addr_len);
287}
288EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
289
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900290void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4adc2006-11-14 20:56:00 -0800291 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300294 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct sock_exterr_skb *serr;
296
297 if (!np->recverr)
298 return;
299
300 skb = skb_clone(skb, GFP_ATOMIC);
301 if (!skb)
302 return;
303
Brian Haleyd40a4de2010-05-03 15:44:27 +0000304 skb->protocol = htons(ETH_P_IPV6);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 serr = SKB_EXT_ERR(skb);
307 serr->ee.ee_errno = err;
308 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900309 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 serr->ee.ee_code = icmph->icmp6_code;
311 serr->ee.ee_pad = 0;
312 serr->ee.ee_info = info;
313 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700314 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
315 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 serr->port = port;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 __skb_pull(skb, payload - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300319 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 if (sock_queue_err_skb(sk, skb))
322 kfree_skb(skb);
323}
324
David S. Miller4c9483b2011-03-12 16:22:43 -0500325void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700327 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct sock_exterr_skb *serr;
329 struct ipv6hdr *iph;
330 struct sk_buff *skb;
331
332 if (!np->recverr)
333 return;
334
335 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
336 if (!skb)
337 return;
338
Brian Haleyd40a4de2010-05-03 15:44:27 +0000339 skb->protocol = htons(ETH_P_IPV6);
340
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300341 skb_put(skb, sizeof(struct ipv6hdr));
342 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700343 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000344 iph->daddr = fl6->daddr;
Eric Dumazet7492dfe2019-01-08 04:06:14 -0800345 ip6_flow_hdr(iph, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 serr = SKB_EXT_ERR(skb);
348 serr->ee.ee_errno = err;
349 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900350 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 serr->ee.ee_code = 0;
352 serr->ee.ee_pad = 0;
353 serr->ee.ee_info = info;
354 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700355 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500356 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700358 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300359 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (sock_queue_err_skb(sk, skb))
362 kfree_skb(skb);
363}
364
David S. Miller4c9483b2011-03-12 16:22:43 -0500365void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000366{
367 struct ipv6_pinfo *np = inet6_sk(sk);
368 struct ipv6hdr *iph;
369 struct sk_buff *skb;
370 struct ip6_mtuinfo *mtu_info;
371
372 if (!np->rxopt.bits.rxpmtu)
373 return;
374
375 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
376 if (!skb)
377 return;
378
379 skb_put(skb, sizeof(struct ipv6hdr));
380 skb_reset_network_header(skb);
381 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000382 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000383
384 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000385
386 mtu_info->ip6m_mtu = mtu;
387 mtu_info->ip6m_addr.sin6_family = AF_INET6;
388 mtu_info->ip6m_addr.sin6_port = 0;
389 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500390 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000391 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000392
393 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
394 skb_reset_transport_header(skb);
395
396 skb = xchg(&np->rxpmtu, skb);
397 kfree_skb(skb);
398}
399
Julian Anastasov34b99df2015-06-23 08:34:39 +0300400/* For some errors we have valid addr_offset even with zero payload and
401 * zero port. Also, addr_offset should be supported if port is set.
402 */
403static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
404{
405 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
406 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
407 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
408}
409
Willem de Bruijnc247f052015-03-07 20:33:22 -0500410/* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
411 *
412 * At one point, excluding local errors was a quick test to identify icmp/icmp6
413 * errors. This is no longer true, but the test remained, so the v6 stack,
414 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
Willem de Bruijnc247f052015-03-07 20:33:22 -0500415 */
416static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
417 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500418{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500419 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
420 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
421 return true;
422
423 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
424 return false;
425
Willem de Bruijn8c04e2a2017-04-12 19:24:35 -0400426 if (!IP6CB(skb)->iif)
Willem de Bruijnc247f052015-03-07 20:33:22 -0500427 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500428
Willem de Bruijnc247f052015-03-07 20:33:22 -0500429 return true;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500430}
431
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900432/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * Handle MSG_ERRQUEUE
434 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100435int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct ipv6_pinfo *np = inet6_sk(sk);
438 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400439 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100440 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct {
442 struct sock_extended_err ee;
443 struct sockaddr_in6 offender;
444 } errhdr;
445 int err;
446 int copied;
447
448 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400449 skb = sock_dequeue_err_skb(sk);
Ian Morris63159f22015-03-29 14:00:04 +0100450 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto out;
452
453 copied = skb->len;
454 if (copied > len) {
455 msg->msg_flags |= MSG_TRUNC;
456 copied = len;
457 }
David S. Miller51f3d022014-11-05 16:46:40 -0500458 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700459 if (unlikely(err)) {
460 kfree_skb(skb);
461 return err;
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 sock_recv_timestamp(msg, sk, skb);
464
465 serr = SKB_EXT_ERR(skb);
466
Julian Anastasov34b99df2015-06-23 08:34:39 +0300467 if (sin && ipv6_datagram_support_addr(serr)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700468 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 sin->sin6_family = AF_INET6;
470 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900471 sin->sin6_port = serr->port;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000472 if (skb->protocol == htons(ETH_P_IPV6)) {
YOSHIFUJI Hideaki / 吉藤英明6c40d102013-01-08 06:44:23 +0000473 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
474 struct ipv6hdr, daddr);
475 sin->sin6_addr = ip6h->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (np->sndflow)
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000477 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000478 sin->sin6_scope_id =
479 ipv6_iface_scope_id(&sin->sin6_addr,
480 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700482 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
483 &sin->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000484 sin->sin6_scope_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100486 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
490 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500491 memset(sin, 0, sizeof(*sin));
Willem de Bruijnc247f052015-03-07 20:33:22 -0500492
493 if (ip6_datagram_support_cmsg(skb, serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 sin->sin6_family = AF_INET6;
Willem de Bruijnc247f052015-03-07 20:33:22 -0500495 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100496 ip6_datagram_recv_common_ctl(sk, msg, skb);
Brian Haleyd40a4de2010-05-03 15:44:27 +0000497 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000498 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100500 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000501 sin->sin6_scope_id =
502 ipv6_iface_scope_id(&sin->sin6_addr,
503 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700505 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
506 &sin->sin6_addr);
Willem de Bruijnf8121162015-01-15 13:18:40 -0500507 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ip_cmsg_recv(msg, skb);
509 }
510 }
511
512 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
513
514 /* Now we could try to dump offended packet options */
515
516 msg->msg_flags |= MSG_ERRQUEUE;
517 err = copied;
518
Eric Dumazet960a2622016-04-21 22:27:32 -0700519 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520out:
521 return err;
522}
Chris Elstona495f832012-04-29 21:48:53 +0000523EXPORT_SYMBOL_GPL(ipv6_recv_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Brian Haley4b340ae2010-04-23 11:26:09 +0000525/*
526 * Handle IPV6_RECVPATHMTU
527 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100528int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
529 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000530{
531 struct ipv6_pinfo *np = inet6_sk(sk);
532 struct sk_buff *skb;
Brian Haley4b340ae2010-04-23 11:26:09 +0000533 struct ip6_mtuinfo mtu_info;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100534 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Brian Haley4b340ae2010-04-23 11:26:09 +0000535 int err;
536 int copied;
537
538 err = -EAGAIN;
539 skb = xchg(&np->rxpmtu, NULL);
Ian Morris63159f22015-03-29 14:00:04 +0100540 if (!skb)
Brian Haley4b340ae2010-04-23 11:26:09 +0000541 goto out;
542
543 copied = skb->len;
544 if (copied > len) {
545 msg->msg_flags |= MSG_TRUNC;
546 copied = len;
547 }
David S. Miller51f3d022014-11-05 16:46:40 -0500548 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Brian Haley4b340ae2010-04-23 11:26:09 +0000549 if (err)
550 goto out_free_skb;
551
552 sock_recv_timestamp(msg, sk, skb);
553
554 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
555
Brian Haley4b340ae2010-04-23 11:26:09 +0000556 if (sin) {
557 sin->sin6_family = AF_INET6;
558 sin->sin6_flowinfo = 0;
559 sin->sin6_port = 0;
560 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000561 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100562 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000563 }
564
565 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
566
567 err = copied;
568
569out_free_skb:
570 kfree_skb(skb);
571out:
572 return err;
573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100576void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
577 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct ipv6_pinfo *np = inet6_sk(sk);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100580 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (np->rxopt.bits.rxinfo) {
583 struct in6_pktinfo src_info;
584
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100585 if (is_ipv6) {
586 src_info.ipi6_ifindex = IP6CB(skb)->iif;
587 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
588 } else {
589 src_info.ipi6_ifindex =
590 PKTINFO_SKB_CB(skb)->ipi_ifindex;
591 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
592 &src_info.ipi6_addr);
593 }
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500594
595 if (src_info.ipi6_ifindex >= 0)
596 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
597 sizeof(src_info), &src_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100599}
600
601void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
602 struct sk_buff *skb)
603{
604 struct ipv6_pinfo *np = inet6_sk(sk);
605 struct inet6_skb_parm *opt = IP6CB(skb);
606 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700609 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
611 }
612
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900613 if (np->rxopt.bits.rxtclass) {
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +0000614 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900615 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
616 }
617
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000618 if (np->rxopt.bits.rxflow) {
619 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
620 if (flowinfo)
621 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900623
624 /* HbH is allowed only once */
Florian Westphal8b58a392015-07-08 23:32:12 +0200625 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
626 u8 *ptr = nh + sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
628 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900629
630 if (opt->lastopt &&
631 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
632 /*
633 * Silly enough, but we need to reparse in order to
634 * report extension headers (except for HbH)
635 * in order.
636 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900637 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900638 * (and WILL NOT be) defined because
639 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
640 */
641 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700642 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900643
644 while (off <= opt->lastopt) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000645 unsigned int len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700646 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900647
Eldad Zackb5a42572012-04-01 07:49:03 +0000648 switch (nexthdr) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900649 case IPPROTO_DSTOPTS:
650 nexthdr = ptr[0];
651 len = (ptr[1] + 1) << 3;
652 if (np->rxopt.bits.dstopts)
653 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
654 break;
655 case IPPROTO_ROUTING:
656 nexthdr = ptr[0];
657 len = (ptr[1] + 1) << 3;
658 if (np->rxopt.bits.srcrt)
659 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
660 break;
661 case IPPROTO_AH:
662 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900663 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900664 break;
665 default:
666 nexthdr = ptr[0];
667 len = (ptr[1] + 1) << 3;
668 break;
669 }
670
671 off += len;
672 }
673 }
674
675 /* socket options in old style */
676 if (np->rxopt.bits.rxoinfo) {
677 struct in6_pktinfo src_info;
678
679 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000680 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900681 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
682 }
683 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700684 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900685 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
686 }
Florian Westphal8b58a392015-07-08 23:32:12 +0200687 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
688 u8 *ptr = nh + sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900689 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
690 }
691 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700692 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900693 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900695 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700696 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900697 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900699 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700700 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900701 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200703 if (np->rxopt.bits.rxorigdstaddr) {
704 struct sockaddr_in6 sin6;
Willem de Bruijn9cd05632019-01-07 16:47:33 -0500705 __be16 _ports[2], *ports;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200706
Willem de Bruijn9cd05632019-01-07 16:47:33 -0500707 ports = skb_header_pointer(skb, skb_transport_offset(skb),
708 sizeof(_ports), &_ports);
709 if (ports) {
Balazs Scheidler6c468622010-10-21 16:08:28 +0200710 /* All current transport protocols have the port numbers in the
711 * first four bytes of the transport header and this function is
712 * written with this assumption in mind.
713 */
Balazs Scheidler6c468622010-10-21 16:08:28 +0200714 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000715 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200716 sin6.sin6_port = ports[1];
717 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000718 sin6.sin6_scope_id =
719 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
720 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200721
722 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
723 }
724 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100725}
726
727void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
728 struct sk_buff *skb)
729{
730 ip6_datagram_recv_common_ctl(sk, msg, skb);
731 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
Tom Parkin8e72d372013-01-31 01:02:25 +0000733EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Tom Parkin73df66f2013-01-31 01:02:24 +0000735int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
736 struct msghdr *msg, struct flowi6 *fl6,
Wei Wang26879da2016-05-02 21:40:07 -0700737 struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 struct in6_pktinfo *src_info;
740 struct cmsghdr *cmsg;
741 struct ipv6_rt_hdr *rthdr;
742 struct ipv6_opt_hdr *hdr;
Wei Wang26879da2016-05-02 21:40:07 -0700743 struct ipv6_txoptions *opt = ipc6->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int len;
745 int err = 0;
746
Gu Zhengf95b4142014-12-11 11:22:04 +0800747 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (!CMSG_OK(msg, cmsg)) {
751 err = -EINVAL;
752 goto exit_f;
753 }
754
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400755 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700756 err = __sock_cmsg_send(sk, msg, cmsg, sockc);
757 if (err)
758 return err;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400759 continue;
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (cmsg->cmsg_level != SOL_IPV6)
763 continue;
764
765 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900766 case IPV6_PKTINFO:
767 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900768 {
769 struct net_device *dev = NULL;
770
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900771 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 err = -EINVAL;
773 goto exit_f;
774 }
775
776 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500779 if (fl6->flowi6_oif &&
780 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500782 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900785 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Eric Dumazet536b2e92009-11-02 12:21:06 +0100787 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500788 if (fl6->flowi6_oif) {
789 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100790 if (!dev) {
791 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900792 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100793 }
794 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
795 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900796 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100797 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900798
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900799 if (addr_type != IPV6_ADDR_ANY) {
800 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000801 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000802 !ipv6_chk_addr(net, &src_info->ipi6_addr,
FX Le Bail7c90cc22014-01-22 07:42:37 +0100803 strict ? dev : NULL, 0) &&
804 !ipv6_chk_acast_addr_src(net, dev,
805 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900806 err = -EINVAL;
807 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000808 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900810
Eric Dumazet536b2e92009-11-02 12:21:06 +0100811 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900813 if (err)
814 goto exit_f;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900820 if (cmsg->cmsg_len < CMSG_LEN(4)) {
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 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
826 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 err = -EINVAL;
828 goto exit_f;
829 }
830 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500831 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 break;
833
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900834 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900836 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 err = -EINVAL;
838 goto exit_f;
839 }
840
841 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
842 len = ((hdr->hdrlen + 1) << 3);
843 if (cmsg->cmsg_len < CMSG_LEN(len)) {
844 err = -EINVAL;
845 goto exit_f;
846 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000847 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 err = -EPERM;
849 goto exit_f;
850 }
851 opt->opt_nflen += len;
852 opt->hopopt = hdr;
853 break;
854
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900855 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900856 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 err = -EINVAL;
858 goto exit_f;
859 }
860
861 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
862 len = ((hdr->hdrlen + 1) << 3);
863 if (cmsg->cmsg_len < CMSG_LEN(len)) {
864 err = -EINVAL;
865 goto exit_f;
866 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000867 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 err = -EPERM;
869 goto exit_f;
870 }
871 if (opt->dst1opt) {
872 err = -EINVAL;
873 goto exit_f;
874 }
875 opt->opt_flen += len;
876 opt->dst1opt = hdr;
877 break;
878
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900879 case IPV6_DSTOPTS:
880 case IPV6_RTHDRDSTOPTS:
881 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
882 err = -EINVAL;
883 goto exit_f;
884 }
885
886 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
887 len = ((hdr->hdrlen + 1) << 3);
888 if (cmsg->cmsg_len < CMSG_LEN(len)) {
889 err = -EINVAL;
890 goto exit_f;
891 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000892 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900893 err = -EPERM;
894 goto exit_f;
895 }
896 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
897 opt->opt_flen += len;
898 opt->dst1opt = hdr;
899 } else {
900 opt->opt_nflen += len;
901 opt->dst0opt = hdr;
902 }
903 break;
904
905 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900907 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 err = -EINVAL;
909 goto exit_f;
910 }
911
912 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
913
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700914 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000915#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700916 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800917 if (rthdr->hdrlen != 2 ||
918 rthdr->segments_left != 1) {
919 err = -EINVAL;
920 goto exit_f;
921 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700922 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700923#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700924 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 err = -EINVAL;
926 goto exit_f;
927 }
928
929 len = ((rthdr->hdrlen + 1) << 3);
930
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900931 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 err = -EINVAL;
933 goto exit_f;
934 }
935
936 /* segments left must also match */
937 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
938 err = -EINVAL;
939 goto exit_f;
940 }
941
942 opt->opt_nflen += len;
943 opt->srcrt = rthdr;
944
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900945 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
947
948 opt->opt_nflen += dsthdrlen;
949 opt->dst0opt = opt->dst1opt;
950 opt->dst1opt = NULL;
951 opt->opt_flen -= dsthdrlen;
952 }
953
954 break;
955
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900956 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 case IPV6_HOPLIMIT:
958 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
959 err = -EINVAL;
960 goto exit_f;
961 }
962
Wei Wang26879da2016-05-02 21:40:07 -0700963 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
964 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
Shan Weie8766fc2008-06-10 15:50:55 +0800965 err = -EINVAL;
966 goto exit_f;
967 }
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
970
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900971 case IPV6_TCLASS:
972 {
973 int tc;
974
975 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000976 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900977 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900978
979 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700980 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900981 goto exit_f;
982
983 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700984 ipc6->tclass = tc;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900985
986 break;
987 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000988
989 case IPV6_DONTFRAG:
990 {
991 int df;
992
993 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000994 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +0000995 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +0000996
997 df = *(int *)CMSG_DATA(cmsg);
998 if (df < 0 || df > 1)
999 goto exit_f;
1000
1001 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -07001002 ipc6->dontfrag = df;
Brian Haley13b52cd2010-04-23 11:26:08 +00001003
1004 break;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -08001007 net_dbg_ratelimited("invalid cmsg type: %d\n",
1008 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -07001010 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
1014exit_f:
1015 return err;
1016}
Tom Parkin73df66f2013-01-31 01:02:24 +00001017EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001018
1019void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1020 __u16 srcp, __u16 destp, int bucket)
1021{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001022 const struct in6_addr *dest, *src;
Subash Abhinov Kasiviswanathanf65f7a42015-06-05 13:23:01 -06001023 __u8 state = sp->sk_state;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001024
Eric Dumazetefe42082013-10-03 15:42:29 -07001025 dest = &sp->sk_v6_daddr;
1026 src = &sp->sk_v6_rcv_saddr;
Subash Abhinov Kasiviswanathanf65f7a42015-06-05 13:23:01 -06001027
1028 if (inet_sk(sp) && inet_sk(sp)->transparent)
1029 state |= 0x80;
1030
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001031 seq_printf(seq,
1032 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001033 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001034 bucket,
1035 src->s6_addr32[0], src->s6_addr32[1],
1036 src->s6_addr32[2], src->s6_addr32[3], srcp,
1037 dest->s6_addr32[0], dest->s6_addr32[1],
1038 dest->s6_addr32[2], dest->s6_addr32[3], destp,
Subash Abhinov Kasiviswanathanf65f7a42015-06-05 13:23:01 -06001039 state,
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001040 sk_wmem_alloc_get(sp),
1041 sk_rmem_alloc_get(sp),
1042 0, 0L, 0,
1043 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1044 0,
1045 sock_i_ino(sp),
1046 atomic_read(&sp->sk_refcnt), sp,
1047 atomic_read(&sp->sk_drops));
1048}