blob: ea9ee5cce5cf6d0cc44adb1881247c404e449bcb [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
Eric Dumazet03645a12015-07-14 08:10:22 +0200142static int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
Ian Morris67ba4152014-08-24 21:53:10 +0100145 struct inet_sock *inet = inet_sk(sk);
146 struct ipv6_pinfo *np = inet6_sk(sk);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700147 struct in6_addr *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int addr_type;
149 int err;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700150 __be32 fl6_flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 if (usin->sin6_family == AF_INET) {
153 if (__ipv6_only_sock(sk))
154 return -EAFNOSUPPORT;
Eric Dumazet03645a12015-07-14 08:10:22 +0200155 err = __ip4_datagram_connect(sk, uaddr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto ipv4_connected;
157 }
158
159 if (addr_len < SIN6_LEN_RFC2133)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900160 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900162 if (usin->sin6_family != AF_INET6)
163 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700165 if (np->sndflow)
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700166 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 addr_type = ipv6_addr_type(&usin->sin6_addr);
169
170 if (addr_type == IPV6_ADDR_ANY) {
171 /*
172 * connect to self
173 */
174 usin->sin6_addr.s6_addr[15] = 0x01;
175 }
176
177 daddr = &usin->sin6_addr;
178
179 if (addr_type == IPV6_ADDR_MAPPED) {
180 struct sockaddr_in sin;
181
182 if (__ipv6_only_sock(sk)) {
183 err = -ENETUNREACH;
184 goto out;
185 }
186 sin.sin_family = AF_INET;
187 sin.sin_addr.s_addr = daddr->s6_addr32[3];
188 sin.sin_port = usin->sin6_port;
189
Eric Dumazet03645a12015-07-14 08:10:22 +0200190 err = __ip4_datagram_connect(sk,
191 (struct sockaddr *) &sin,
192 sizeof(sin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194ipv4_connected:
195 if (err)
196 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900197
Eric Dumazetefe42082013-10-03 15:42:29 -0700198 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Max Matveevc15fea22011-08-05 03:56:30 -0700200 if (ipv6_addr_any(&np->saddr) ||
201 ipv6_mapped_addr_any(&np->saddr))
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000202 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Eric Dumazetefe42082013-10-03 15:42:29 -0700204 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
205 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000206 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
Eric Dumazetefe42082013-10-03 15:42:29 -0700207 &sk->sk_v6_rcv_saddr);
Eric Dumazet719f8352010-09-08 05:08:44 +0000208 if (sk->sk_prot->rehash)
209 sk->sk_prot->rehash(sk);
210 }
Brian Haleyb301e822009-10-07 13:58:25 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 goto out;
213 }
214
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000215 if (__ipv6_addr_needs_scope_id(addr_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (addr_len >= sizeof(struct sockaddr_in6) &&
217 usin->sin6_scope_id) {
218 if (sk->sk_bound_dev_if &&
219 sk->sk_bound_dev_if != usin->sin6_scope_id) {
220 err = -EINVAL;
221 goto out;
222 }
223 sk->sk_bound_dev_if = usin->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Brian Haley1ac4f002008-01-08 23:52:21 -0800226 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
227 sk->sk_bound_dev_if = np->mcast_oif;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Connect to link-local address requires an interface */
230 if (!sk->sk_bound_dev_if) {
231 err = -EINVAL;
232 goto out;
233 }
234 }
235
Eric Dumazetefe42082013-10-03 15:42:29 -0700236 sk->sk_v6_daddr = *daddr;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700237 np->flow_label = fl6_flowlabel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000239 inet->inet_dport = usin->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /*
242 * Check for a route to destination an obtain the
243 * destination cache for it.
244 */
245
Martin KaFai Lau33c162a2016-04-11 15:29:36 -0700246 err = ip6_datagram_dst_update(sk, true);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700247 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 sk->sk_state = TCP_ESTABLISHED;
Tom Herbert877d1f62015-07-28 16:02:05 -0700251 sk_set_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return err;
254}
Eric Dumazet03645a12015-07-14 08:10:22 +0200255
256int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
257{
258 int res;
259
260 lock_sock(sk);
261 res = __ip6_datagram_connect(sk, uaddr, addr_len);
262 release_sock(sk);
263 return res;
264}
Chris Elstona495f832012-04-29 21:48:53 +0000265EXPORT_SYMBOL_GPL(ip6_datagram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Hannes Frederic Sowa82b276c2014-01-20 05:16:39 +0100267int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
268 int addr_len)
269{
270 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
271 if (sin6->sin6_family != AF_INET6)
272 return -EAFNOSUPPORT;
273 return ip6_datagram_connect(sk, uaddr, addr_len);
274}
275EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
276
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900277void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4adc2006-11-14 20:56:00 -0800278 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300281 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct sock_exterr_skb *serr;
283
284 if (!np->recverr)
285 return;
286
287 skb = skb_clone(skb, GFP_ATOMIC);
288 if (!skb)
289 return;
290
Brian Haleyd40a4de2010-05-03 15:44:27 +0000291 skb->protocol = htons(ETH_P_IPV6);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 serr = SKB_EXT_ERR(skb);
294 serr->ee.ee_errno = err;
295 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900296 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 serr->ee.ee_code = icmph->icmp6_code;
298 serr->ee.ee_pad = 0;
299 serr->ee.ee_info = info;
300 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700301 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
302 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 serr->port = port;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 __skb_pull(skb, payload - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300306 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (sock_queue_err_skb(sk, skb))
309 kfree_skb(skb);
310}
311
David S. Miller4c9483b2011-03-12 16:22:43 -0500312void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700314 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct sock_exterr_skb *serr;
316 struct ipv6hdr *iph;
317 struct sk_buff *skb;
318
319 if (!np->recverr)
320 return;
321
322 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
323 if (!skb)
324 return;
325
Brian Haleyd40a4de2010-05-03 15:44:27 +0000326 skb->protocol = htons(ETH_P_IPV6);
327
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300328 skb_put(skb, sizeof(struct ipv6hdr));
329 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700330 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000331 iph->daddr = fl6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 serr = SKB_EXT_ERR(skb);
334 serr->ee.ee_errno = err;
335 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900336 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 serr->ee.ee_code = 0;
338 serr->ee.ee_pad = 0;
339 serr->ee.ee_info = info;
340 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700341 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500342 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700344 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300345 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (sock_queue_err_skb(sk, skb))
348 kfree_skb(skb);
349}
350
David S. Miller4c9483b2011-03-12 16:22:43 -0500351void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000352{
353 struct ipv6_pinfo *np = inet6_sk(sk);
354 struct ipv6hdr *iph;
355 struct sk_buff *skb;
356 struct ip6_mtuinfo *mtu_info;
357
358 if (!np->rxopt.bits.rxpmtu)
359 return;
360
361 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
362 if (!skb)
363 return;
364
365 skb_put(skb, sizeof(struct ipv6hdr));
366 skb_reset_network_header(skb);
367 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000368 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000369
370 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000371
372 mtu_info->ip6m_mtu = mtu;
373 mtu_info->ip6m_addr.sin6_family = AF_INET6;
374 mtu_info->ip6m_addr.sin6_port = 0;
375 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500376 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000377 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000378
379 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
380 skb_reset_transport_header(skb);
381
382 skb = xchg(&np->rxpmtu, skb);
383 kfree_skb(skb);
384}
385
Julian Anastasov34b99df2015-06-23 08:34:39 +0300386/* For some errors we have valid addr_offset even with zero payload and
387 * zero port. Also, addr_offset should be supported if port is set.
388 */
389static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
390{
391 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
392 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
393 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
394}
395
Willem de Bruijnc247f052015-03-07 20:33:22 -0500396/* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
397 *
398 * At one point, excluding local errors was a quick test to identify icmp/icmp6
399 * errors. This is no longer true, but the test remained, so the v6 stack,
400 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
401 *
402 * Timestamp code paths do not initialize the fields expected by cmsg:
403 * the PKTINFO fields in skb->cb[]. Fill those in here.
404 */
405static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
406 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500407{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500408 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
409 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
410 return true;
411
412 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
413 return false;
414
415 if (!skb->dev)
416 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500417
418 if (skb->protocol == htons(ETH_P_IPV6))
Willem de Bruijnc247f052015-03-07 20:33:22 -0500419 IP6CB(skb)->iif = skb->dev->ifindex;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500420 else
Willem de Bruijnc247f052015-03-07 20:33:22 -0500421 PKTINFO_SKB_CB(skb)->ipi_ifindex = skb->dev->ifindex;
422
423 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;
Eric Dumazet747465e2012-01-16 19:27:39 +0000699 __be16 *ports = (__be16 *) skb_transport_header(skb);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200700
701 if (skb_transport_offset(skb) + 4 <= skb->len) {
702 /* All current transport protocols have the port numbers in the
703 * first four bytes of the transport header and this function is
704 * written with this assumption in mind.
705 */
706
707 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000708 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200709 sin6.sin6_port = ports[1];
710 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000711 sin6.sin6_scope_id =
712 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
713 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200714
715 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
716 }
717 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100718}
719
720void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
721 struct sk_buff *skb)
722{
723 ip6_datagram_recv_common_ctl(sk, msg, skb);
724 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Tom Parkin8e72d372013-01-31 01:02:25 +0000726EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Tom Parkin73df66f2013-01-31 01:02:24 +0000728int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
729 struct msghdr *msg, struct flowi6 *fl6,
730 struct ipv6_txoptions *opt,
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400731 int *hlimit, int *tclass, int *dontfrag,
732 struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct in6_pktinfo *src_info;
735 struct cmsghdr *cmsg;
736 struct ipv6_rt_hdr *rthdr;
737 struct ipv6_opt_hdr *hdr;
738 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) {
750 if (__sock_cmsg_send(sk, msg, cmsg, sockc))
751 return -EINVAL;
752 continue;
753 }
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (cmsg->cmsg_level != SOL_IPV6)
756 continue;
757
758 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900759 case IPV6_PKTINFO:
760 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900761 {
762 struct net_device *dev = NULL;
763
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900764 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 err = -EINVAL;
766 goto exit_f;
767 }
768
769 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500772 if (fl6->flowi6_oif &&
773 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500775 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900778 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Eric Dumazet536b2e92009-11-02 12:21:06 +0100780 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500781 if (fl6->flowi6_oif) {
782 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100783 if (!dev) {
784 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900785 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100786 }
787 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
788 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900789 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100790 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900791
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900792 if (addr_type != IPV6_ADDR_ANY) {
793 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000794 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000795 !ipv6_chk_addr(net, &src_info->ipi6_addr,
FX Le Bail7c90cc22014-01-22 07:42:37 +0100796 strict ? dev : NULL, 0) &&
797 !ipv6_chk_acast_addr_src(net, dev,
798 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900799 err = -EINVAL;
800 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000801 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900803
Eric Dumazet536b2e92009-11-02 12:21:06 +0100804 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900806 if (err)
807 goto exit_f;
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900813 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 err = -EINVAL;
815 goto exit_f;
816 }
817
David S. Miller4c9483b2011-03-12 16:22:43 -0500818 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
819 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 err = -EINVAL;
821 goto exit_f;
822 }
823 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500824 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 break;
826
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900827 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900829 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 err = -EINVAL;
831 goto exit_f;
832 }
833
834 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
835 len = ((hdr->hdrlen + 1) << 3);
836 if (cmsg->cmsg_len < CMSG_LEN(len)) {
837 err = -EINVAL;
838 goto exit_f;
839 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000840 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 err = -EPERM;
842 goto exit_f;
843 }
844 opt->opt_nflen += len;
845 opt->hopopt = hdr;
846 break;
847
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900848 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900849 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 err = -EINVAL;
851 goto exit_f;
852 }
853
854 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
855 len = ((hdr->hdrlen + 1) << 3);
856 if (cmsg->cmsg_len < CMSG_LEN(len)) {
857 err = -EINVAL;
858 goto exit_f;
859 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000860 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 err = -EPERM;
862 goto exit_f;
863 }
864 if (opt->dst1opt) {
865 err = -EINVAL;
866 goto exit_f;
867 }
868 opt->opt_flen += len;
869 opt->dst1opt = hdr;
870 break;
871
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900872 case IPV6_DSTOPTS:
873 case IPV6_RTHDRDSTOPTS:
874 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
875 err = -EINVAL;
876 goto exit_f;
877 }
878
879 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
880 len = ((hdr->hdrlen + 1) << 3);
881 if (cmsg->cmsg_len < CMSG_LEN(len)) {
882 err = -EINVAL;
883 goto exit_f;
884 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000885 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900886 err = -EPERM;
887 goto exit_f;
888 }
889 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
890 opt->opt_flen += len;
891 opt->dst1opt = hdr;
892 } else {
893 opt->opt_nflen += len;
894 opt->dst0opt = hdr;
895 }
896 break;
897
898 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900900 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 err = -EINVAL;
902 goto exit_f;
903 }
904
905 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
906
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700907 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000908#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700909 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800910 if (rthdr->hdrlen != 2 ||
911 rthdr->segments_left != 1) {
912 err = -EINVAL;
913 goto exit_f;
914 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700915 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700916#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700917 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 err = -EINVAL;
919 goto exit_f;
920 }
921
922 len = ((rthdr->hdrlen + 1) << 3);
923
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900924 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 err = -EINVAL;
926 goto exit_f;
927 }
928
929 /* segments left must also match */
930 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
931 err = -EINVAL;
932 goto exit_f;
933 }
934
935 opt->opt_nflen += len;
936 opt->srcrt = rthdr;
937
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900938 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
940
941 opt->opt_nflen += dsthdrlen;
942 opt->dst0opt = opt->dst1opt;
943 opt->dst1opt = NULL;
944 opt->opt_flen -= dsthdrlen;
945 }
946
947 break;
948
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900949 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case IPV6_HOPLIMIT:
951 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
952 err = -EINVAL;
953 goto exit_f;
954 }
955
956 *hlimit = *(int *)CMSG_DATA(cmsg);
Shan Weie8766fc2008-06-10 15:50:55 +0800957 if (*hlimit < -1 || *hlimit > 0xff) {
958 err = -EINVAL;
959 goto exit_f;
960 }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
963
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900964 case IPV6_TCLASS:
965 {
966 int tc;
967
968 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000969 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900970 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900971
972 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700973 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900974 goto exit_f;
975
976 err = 0;
977 *tclass = tc;
978
979 break;
980 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000981
982 case IPV6_DONTFRAG:
983 {
984 int df;
985
986 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000987 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +0000988 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +0000989
990 df = *(int *)CMSG_DATA(cmsg);
991 if (df < 0 || df > 1)
992 goto exit_f;
993
994 err = 0;
995 *dontfrag = df;
996
997 break;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -08001000 net_dbg_ratelimited("invalid cmsg type: %d\n",
1001 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -07001003 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
1007exit_f:
1008 return err;
1009}
Tom Parkin73df66f2013-01-31 01:02:24 +00001010EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001011
1012void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1013 __u16 srcp, __u16 destp, int bucket)
1014{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001015 const struct in6_addr *dest, *src;
1016
Eric Dumazetefe42082013-10-03 15:42:29 -07001017 dest = &sp->sk_v6_daddr;
1018 src = &sp->sk_v6_rcv_saddr;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001019 seq_printf(seq,
1020 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001021 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001022 bucket,
1023 src->s6_addr32[0], src->s6_addr32[1],
1024 src->s6_addr32[2], src->s6_addr32[3], srcp,
1025 dest->s6_addr32[0], dest->s6_addr32[1],
1026 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1027 sp->sk_state,
1028 sk_wmem_alloc_get(sp),
1029 sk_rmem_alloc_get(sp),
1030 0, 0L, 0,
1031 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1032 0,
1033 sock_i_ino(sp),
1034 atomic_read(&sp->sk_refcnt), sp,
1035 atomic_read(&sp->sk_drops));
1036}