blob: 38062f403ceb3cb573f7f24880a6d688a49bc099 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 serr = SKB_EXT_ERR(skb);
340 serr->ee.ee_errno = err;
341 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900342 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 serr->ee.ee_code = 0;
344 serr->ee.ee_pad = 0;
345 serr->ee.ee_info = info;
346 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700347 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500348 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700350 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300351 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (sock_queue_err_skb(sk, skb))
354 kfree_skb(skb);
355}
356
David S. Miller4c9483b2011-03-12 16:22:43 -0500357void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000358{
359 struct ipv6_pinfo *np = inet6_sk(sk);
360 struct ipv6hdr *iph;
361 struct sk_buff *skb;
362 struct ip6_mtuinfo *mtu_info;
363
364 if (!np->rxopt.bits.rxpmtu)
365 return;
366
367 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
368 if (!skb)
369 return;
370
371 skb_put(skb, sizeof(struct ipv6hdr));
372 skb_reset_network_header(skb);
373 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000374 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000375
376 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000377
378 mtu_info->ip6m_mtu = mtu;
379 mtu_info->ip6m_addr.sin6_family = AF_INET6;
380 mtu_info->ip6m_addr.sin6_port = 0;
381 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500382 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000383 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000384
385 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
386 skb_reset_transport_header(skb);
387
388 skb = xchg(&np->rxpmtu, skb);
389 kfree_skb(skb);
390}
391
Julian Anastasov34b99df2015-06-23 08:34:39 +0300392/* For some errors we have valid addr_offset even with zero payload and
393 * zero port. Also, addr_offset should be supported if port is set.
394 */
395static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
396{
397 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
398 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
399 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
400}
401
Willem de Bruijnc247f052015-03-07 20:33:22 -0500402/* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
403 *
404 * At one point, excluding local errors was a quick test to identify icmp/icmp6
405 * errors. This is no longer true, but the test remained, so the v6 stack,
406 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
Willem de Bruijnc247f052015-03-07 20:33:22 -0500407 */
408static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
409 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500410{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500411 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
412 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
413 return true;
414
415 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
416 return false;
417
Willem de Bruijn8c04e2a2017-04-12 19:24:35 -0400418 if (!IP6CB(skb)->iif)
Willem de Bruijnc247f052015-03-07 20:33:22 -0500419 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500420
Willem de Bruijnc247f052015-03-07 20:33:22 -0500421 return true;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500422}
423
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900424/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * Handle MSG_ERRQUEUE
426 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100427int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct ipv6_pinfo *np = inet6_sk(sk);
430 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400431 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100432 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct {
434 struct sock_extended_err ee;
435 struct sockaddr_in6 offender;
436 } errhdr;
437 int err;
438 int copied;
439
440 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400441 skb = sock_dequeue_err_skb(sk);
Ian Morris63159f22015-03-29 14:00:04 +0100442 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto out;
444
445 copied = skb->len;
446 if (copied > len) {
447 msg->msg_flags |= MSG_TRUNC;
448 copied = len;
449 }
David S. Miller51f3d022014-11-05 16:46:40 -0500450 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700451 if (unlikely(err)) {
452 kfree_skb(skb);
453 return err;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 sock_recv_timestamp(msg, sk, skb);
456
457 serr = SKB_EXT_ERR(skb);
458
Julian Anastasov34b99df2015-06-23 08:34:39 +0300459 if (sin && ipv6_datagram_support_addr(serr)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700460 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 sin->sin6_family = AF_INET6;
462 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900463 sin->sin6_port = serr->port;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000464 if (skb->protocol == htons(ETH_P_IPV6)) {
YOSHIFUJI Hideaki / 吉藤英明6c40d102013-01-08 06:44:23 +0000465 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
466 struct ipv6hdr, daddr);
467 sin->sin6_addr = ip6h->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (np->sndflow)
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000469 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000470 sin->sin6_scope_id =
471 ipv6_iface_scope_id(&sin->sin6_addr,
472 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700474 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
475 &sin->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000476 sin->sin6_scope_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100478 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
481 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
482 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500483 memset(sin, 0, sizeof(*sin));
Willem de Bruijnc247f052015-03-07 20:33:22 -0500484
485 if (ip6_datagram_support_cmsg(skb, serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 sin->sin6_family = AF_INET6;
Willem de Bruijnc247f052015-03-07 20:33:22 -0500487 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100488 ip6_datagram_recv_common_ctl(sk, msg, skb);
Brian Haleyd40a4de2010-05-03 15:44:27 +0000489 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000490 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100492 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000493 sin->sin6_scope_id =
494 ipv6_iface_scope_id(&sin->sin6_addr,
495 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700497 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
498 &sin->sin6_addr);
Willem de Bruijnf8121162015-01-15 13:18:40 -0500499 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ip_cmsg_recv(msg, skb);
501 }
502 }
503
504 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
505
506 /* Now we could try to dump offended packet options */
507
508 msg->msg_flags |= MSG_ERRQUEUE;
509 err = copied;
510
Eric Dumazet960a2622016-04-21 22:27:32 -0700511 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512out:
513 return err;
514}
Chris Elstona495f832012-04-29 21:48:53 +0000515EXPORT_SYMBOL_GPL(ipv6_recv_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Brian Haley4b340ae2010-04-23 11:26:09 +0000517/*
518 * Handle IPV6_RECVPATHMTU
519 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100520int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
521 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000522{
523 struct ipv6_pinfo *np = inet6_sk(sk);
524 struct sk_buff *skb;
Brian Haley4b340ae2010-04-23 11:26:09 +0000525 struct ip6_mtuinfo mtu_info;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100526 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Brian Haley4b340ae2010-04-23 11:26:09 +0000527 int err;
528 int copied;
529
530 err = -EAGAIN;
531 skb = xchg(&np->rxpmtu, NULL);
Ian Morris63159f22015-03-29 14:00:04 +0100532 if (!skb)
Brian Haley4b340ae2010-04-23 11:26:09 +0000533 goto out;
534
535 copied = skb->len;
536 if (copied > len) {
537 msg->msg_flags |= MSG_TRUNC;
538 copied = len;
539 }
David S. Miller51f3d022014-11-05 16:46:40 -0500540 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Brian Haley4b340ae2010-04-23 11:26:09 +0000541 if (err)
542 goto out_free_skb;
543
544 sock_recv_timestamp(msg, sk, skb);
545
546 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
547
Brian Haley4b340ae2010-04-23 11:26:09 +0000548 if (sin) {
549 sin->sin6_family = AF_INET6;
550 sin->sin6_flowinfo = 0;
551 sin->sin6_port = 0;
552 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000553 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100554 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000555 }
556
557 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
558
559 err = copied;
560
561out_free_skb:
562 kfree_skb(skb);
563out:
564 return err;
565}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100568void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
569 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct ipv6_pinfo *np = inet6_sk(sk);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100572 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (np->rxopt.bits.rxinfo) {
575 struct in6_pktinfo src_info;
576
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100577 if (is_ipv6) {
578 src_info.ipi6_ifindex = IP6CB(skb)->iif;
579 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
580 } else {
581 src_info.ipi6_ifindex =
582 PKTINFO_SKB_CB(skb)->ipi_ifindex;
583 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
584 &src_info.ipi6_addr);
585 }
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500586
587 if (src_info.ipi6_ifindex >= 0)
588 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
589 sizeof(src_info), &src_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100591}
592
593void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
594 struct sk_buff *skb)
595{
596 struct ipv6_pinfo *np = inet6_sk(sk);
597 struct inet6_skb_parm *opt = IP6CB(skb);
598 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700601 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
603 }
604
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900605 if (np->rxopt.bits.rxtclass) {
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +0000606 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900607 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
608 }
609
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000610 if (np->rxopt.bits.rxflow) {
611 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
612 if (flowinfo)
613 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900615
616 /* HbH is allowed only once */
Florian Westphal8b58a392015-07-08 23:32:12 +0200617 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
618 u8 *ptr = nh + sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
620 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900621
622 if (opt->lastopt &&
623 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
624 /*
625 * Silly enough, but we need to reparse in order to
626 * report extension headers (except for HbH)
627 * in order.
628 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900629 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900630 * (and WILL NOT be) defined because
631 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
632 */
633 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700634 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900635
636 while (off <= opt->lastopt) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000637 unsigned int len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700638 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900639
Eldad Zackb5a42572012-04-01 07:49:03 +0000640 switch (nexthdr) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900641 case IPPROTO_DSTOPTS:
642 nexthdr = ptr[0];
643 len = (ptr[1] + 1) << 3;
644 if (np->rxopt.bits.dstopts)
645 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
646 break;
647 case IPPROTO_ROUTING:
648 nexthdr = ptr[0];
649 len = (ptr[1] + 1) << 3;
650 if (np->rxopt.bits.srcrt)
651 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
652 break;
653 case IPPROTO_AH:
654 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900655 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900656 break;
657 default:
658 nexthdr = ptr[0];
659 len = (ptr[1] + 1) << 3;
660 break;
661 }
662
663 off += len;
664 }
665 }
666
667 /* socket options in old style */
668 if (np->rxopt.bits.rxoinfo) {
669 struct in6_pktinfo src_info;
670
671 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000672 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900673 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
674 }
675 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700676 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900677 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
678 }
Florian Westphal8b58a392015-07-08 23:32:12 +0200679 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
680 u8 *ptr = nh + sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900681 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
682 }
683 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700684 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900685 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900687 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700688 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900689 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900691 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700692 u8 *ptr = nh + opt->dst1;
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 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200695 if (np->rxopt.bits.rxorigdstaddr) {
696 struct sockaddr_in6 sin6;
Eric Dumazet747465e2012-01-16 19:27:39 +0000697 __be16 *ports = (__be16 *) skb_transport_header(skb);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200698
Willem de Bruijnd36a1cb2016-12-22 18:19:16 -0500699 if (skb_transport_offset(skb) + 4 <= (int)skb->len) {
Balazs Scheidler6c468622010-10-21 16:08:28 +0200700 /* All current transport protocols have the port numbers in the
701 * first four bytes of the transport header and this function is
702 * written with this assumption in mind.
703 */
704
705 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000706 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200707 sin6.sin6_port = ports[1];
708 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000709 sin6.sin6_scope_id =
710 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
711 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200712
713 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
714 }
715 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100716}
717
718void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
719 struct sk_buff *skb)
720{
721 ip6_datagram_recv_common_ctl(sk, msg, skb);
722 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
Tom Parkin8e72d372013-01-31 01:02:25 +0000724EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Tom Parkin73df66f2013-01-31 01:02:24 +0000726int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
727 struct msghdr *msg, struct flowi6 *fl6,
Wei Wang26879da2016-05-02 21:40:07 -0700728 struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct in6_pktinfo *src_info;
731 struct cmsghdr *cmsg;
732 struct ipv6_rt_hdr *rthdr;
733 struct ipv6_opt_hdr *hdr;
Wei Wang26879da2016-05-02 21:40:07 -0700734 struct ipv6_txoptions *opt = ipc6->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int len;
736 int err = 0;
737
Gu Zhengf95b4142014-12-11 11:22:04 +0800738 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (!CMSG_OK(msg, cmsg)) {
742 err = -EINVAL;
743 goto exit_f;
744 }
745
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400746 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700747 err = __sock_cmsg_send(sk, msg, cmsg, sockc);
748 if (err)
749 return err;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400750 continue;
751 }
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (cmsg->cmsg_level != SOL_IPV6)
754 continue;
755
756 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900757 case IPV6_PKTINFO:
758 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900759 {
760 struct net_device *dev = NULL;
761
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900762 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 err = -EINVAL;
764 goto exit_f;
765 }
766
767 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500770 if (fl6->flowi6_oif &&
771 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500773 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900776 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Eric Dumazet536b2e92009-11-02 12:21:06 +0100778 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500779 if (fl6->flowi6_oif) {
780 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100781 if (!dev) {
782 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900783 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100784 }
785 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
786 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900787 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100788 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900789
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900790 if (addr_type != IPV6_ADDR_ANY) {
791 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000792 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000793 !ipv6_chk_addr(net, &src_info->ipi6_addr,
FX Le Bail7c90cc22014-01-22 07:42:37 +0100794 strict ? dev : NULL, 0) &&
795 !ipv6_chk_acast_addr_src(net, dev,
796 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900797 err = -EINVAL;
798 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000799 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900801
Eric Dumazet536b2e92009-11-02 12:21:06 +0100802 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900804 if (err)
805 goto exit_f;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900811 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 err = -EINVAL;
813 goto exit_f;
814 }
815
David S. Miller4c9483b2011-03-12 16:22:43 -0500816 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
817 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 err = -EINVAL;
819 goto exit_f;
820 }
821 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500822 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 break;
824
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900825 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900827 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 err = -EINVAL;
829 goto exit_f;
830 }
831
832 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
833 len = ((hdr->hdrlen + 1) << 3);
834 if (cmsg->cmsg_len < CMSG_LEN(len)) {
835 err = -EINVAL;
836 goto exit_f;
837 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000838 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 err = -EPERM;
840 goto exit_f;
841 }
842 opt->opt_nflen += len;
843 opt->hopopt = hdr;
844 break;
845
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900846 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900847 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 err = -EINVAL;
849 goto exit_f;
850 }
851
852 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
853 len = ((hdr->hdrlen + 1) << 3);
854 if (cmsg->cmsg_len < CMSG_LEN(len)) {
855 err = -EINVAL;
856 goto exit_f;
857 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000858 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 err = -EPERM;
860 goto exit_f;
861 }
862 if (opt->dst1opt) {
863 err = -EINVAL;
864 goto exit_f;
865 }
866 opt->opt_flen += len;
867 opt->dst1opt = hdr;
868 break;
869
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900870 case IPV6_DSTOPTS:
871 case IPV6_RTHDRDSTOPTS:
872 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
873 err = -EINVAL;
874 goto exit_f;
875 }
876
877 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
878 len = ((hdr->hdrlen + 1) << 3);
879 if (cmsg->cmsg_len < CMSG_LEN(len)) {
880 err = -EINVAL;
881 goto exit_f;
882 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000883 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900884 err = -EPERM;
885 goto exit_f;
886 }
887 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
888 opt->opt_flen += len;
889 opt->dst1opt = hdr;
890 } else {
891 opt->opt_nflen += len;
892 opt->dst0opt = hdr;
893 }
894 break;
895
896 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900898 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 err = -EINVAL;
900 goto exit_f;
901 }
902
903 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
904
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700905 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000906#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700907 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800908 if (rthdr->hdrlen != 2 ||
909 rthdr->segments_left != 1) {
910 err = -EINVAL;
911 goto exit_f;
912 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700913 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700914#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700915 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 err = -EINVAL;
917 goto exit_f;
918 }
919
920 len = ((rthdr->hdrlen + 1) << 3);
921
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900922 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 err = -EINVAL;
924 goto exit_f;
925 }
926
927 /* segments left must also match */
928 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
929 err = -EINVAL;
930 goto exit_f;
931 }
932
933 opt->opt_nflen += len;
934 opt->srcrt = rthdr;
935
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900936 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
938
939 opt->opt_nflen += dsthdrlen;
940 opt->dst0opt = opt->dst1opt;
941 opt->dst1opt = NULL;
942 opt->opt_flen -= dsthdrlen;
943 }
944
945 break;
946
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900947 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 case IPV6_HOPLIMIT:
949 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
950 err = -EINVAL;
951 goto exit_f;
952 }
953
Wei Wang26879da2016-05-02 21:40:07 -0700954 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
955 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
Shan Weie8766fc2008-06-10 15:50:55 +0800956 err = -EINVAL;
957 goto exit_f;
958 }
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 break;
961
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900962 case IPV6_TCLASS:
963 {
964 int tc;
965
966 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000967 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900968 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900969
970 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700971 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900972 goto exit_f;
973
974 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700975 ipc6->tclass = tc;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900976
977 break;
978 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000979
980 case IPV6_DONTFRAG:
981 {
982 int df;
983
984 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000985 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +0000986 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +0000987
988 df = *(int *)CMSG_DATA(cmsg);
989 if (df < 0 || df > 1)
990 goto exit_f;
991
992 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700993 ipc6->dontfrag = df;
Brian Haley13b52cd2010-04-23 11:26:08 +0000994
995 break;
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800998 net_dbg_ratelimited("invalid cmsg type: %d\n",
999 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -07001001 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
1005exit_f:
1006 return err;
1007}
Tom Parkin73df66f2013-01-31 01:02:24 +00001008EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001009
1010void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1011 __u16 srcp, __u16 destp, int bucket)
1012{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001013 const struct in6_addr *dest, *src;
1014
Eric Dumazetefe42082013-10-03 15:42:29 -07001015 dest = &sp->sk_v6_daddr;
1016 src = &sp->sk_v6_rcv_saddr;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001017 seq_printf(seq,
1018 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001019 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001020 bucket,
1021 src->s6_addr32[0], src->s6_addr32[1],
1022 src->s6_addr32[2], src->s6_addr32[3], srcp,
1023 dest->s6_addr32[0], dest->s6_addr32[1],
1024 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1025 sp->sk_state,
1026 sk_wmem_alloc_get(sp),
1027 sk_rmem_alloc_get(sp),
1028 0, 0L, 0,
1029 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1030 0,
1031 sock_i_ino(sp),
1032 atomic_read(&sp->sk_refcnt), sp,
1033 atomic_read(&sp->sk_drops));
1034}