blob: 59e01f27db9f6aa7dc8dd005fe499db3a3bc3254 [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
Eric Dumazet03645a12015-07-14 08:10:22 +0200122static int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
Ian Morris67ba4152014-08-24 21:53:10 +0100125 struct inet_sock *inet = inet_sk(sk);
126 struct ipv6_pinfo *np = inet6_sk(sk);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700127 struct in6_addr *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int addr_type;
129 int err;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700130 __be32 fl6_flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (usin->sin6_family == AF_INET) {
133 if (__ipv6_only_sock(sk))
134 return -EAFNOSUPPORT;
Eric Dumazet03645a12015-07-14 08:10:22 +0200135 err = __ip4_datagram_connect(sk, uaddr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto ipv4_connected;
137 }
138
139 if (addr_len < SIN6_LEN_RFC2133)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900140 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900142 if (usin->sin6_family != AF_INET6)
143 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700145 if (np->sndflow)
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700146 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 addr_type = ipv6_addr_type(&usin->sin6_addr);
149
150 if (addr_type == IPV6_ADDR_ANY) {
151 /*
152 * connect to self
153 */
154 usin->sin6_addr.s6_addr[15] = 0x01;
155 }
156
157 daddr = &usin->sin6_addr;
158
159 if (addr_type == IPV6_ADDR_MAPPED) {
160 struct sockaddr_in sin;
161
162 if (__ipv6_only_sock(sk)) {
163 err = -ENETUNREACH;
164 goto out;
165 }
166 sin.sin_family = AF_INET;
167 sin.sin_addr.s_addr = daddr->s6_addr32[3];
168 sin.sin_port = usin->sin6_port;
169
Eric Dumazet03645a12015-07-14 08:10:22 +0200170 err = __ip4_datagram_connect(sk,
171 (struct sockaddr *) &sin,
172 sizeof(sin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174ipv4_connected:
175 if (err)
176 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900177
Eric Dumazetefe42082013-10-03 15:42:29 -0700178 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Max Matveevc15fea22011-08-05 03:56:30 -0700180 if (ipv6_addr_any(&np->saddr) ||
181 ipv6_mapped_addr_any(&np->saddr))
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000182 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Eric Dumazetefe42082013-10-03 15:42:29 -0700184 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
185 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000186 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
Eric Dumazetefe42082013-10-03 15:42:29 -0700187 &sk->sk_v6_rcv_saddr);
Eric Dumazet719f8352010-09-08 05:08:44 +0000188 if (sk->sk_prot->rehash)
189 sk->sk_prot->rehash(sk);
190 }
Brian Haleyb301e822009-10-07 13:58:25 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto out;
193 }
194
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000195 if (__ipv6_addr_needs_scope_id(addr_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (addr_len >= sizeof(struct sockaddr_in6) &&
197 usin->sin6_scope_id) {
198 if (sk->sk_bound_dev_if &&
199 sk->sk_bound_dev_if != usin->sin6_scope_id) {
200 err = -EINVAL;
201 goto out;
202 }
203 sk->sk_bound_dev_if = usin->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
Brian Haley1ac4f002008-01-08 23:52:21 -0800206 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
207 sk->sk_bound_dev_if = np->mcast_oif;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Connect to link-local address requires an interface */
210 if (!sk->sk_bound_dev_if) {
211 err = -EINVAL;
212 goto out;
213 }
214 }
215
Eric Dumazetefe42082013-10-03 15:42:29 -0700216 sk->sk_v6_daddr = *daddr;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700217 np->flow_label = fl6_flowlabel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000219 inet->inet_dport = usin->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /*
222 * Check for a route to destination an obtain the
223 * destination cache for it.
224 */
225
Martin KaFai Lau33c162a2016-04-11 15:29:36 -0700226 err = ip6_datagram_dst_update(sk, true);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700227 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 sk->sk_state = TCP_ESTABLISHED;
Tom Herbert877d1f62015-07-28 16:02:05 -0700231 sk_set_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return err;
234}
Eric Dumazet03645a12015-07-14 08:10:22 +0200235
236int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
237{
238 int res;
239
240 lock_sock(sk);
241 res = __ip6_datagram_connect(sk, uaddr, addr_len);
242 release_sock(sk);
243 return res;
244}
Chris Elstona495f832012-04-29 21:48:53 +0000245EXPORT_SYMBOL_GPL(ip6_datagram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Hannes Frederic Sowa82b276c2014-01-20 05:16:39 +0100247int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
248 int addr_len)
249{
250 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
251 if (sin6->sin6_family != AF_INET6)
252 return -EAFNOSUPPORT;
253 return ip6_datagram_connect(sk, uaddr, addr_len);
254}
255EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
256
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900257void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4adc2006-11-14 20:56:00 -0800258 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300261 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct sock_exterr_skb *serr;
263
264 if (!np->recverr)
265 return;
266
267 skb = skb_clone(skb, GFP_ATOMIC);
268 if (!skb)
269 return;
270
Brian Haleyd40a4de2010-05-03 15:44:27 +0000271 skb->protocol = htons(ETH_P_IPV6);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 serr = SKB_EXT_ERR(skb);
274 serr->ee.ee_errno = err;
275 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900276 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 serr->ee.ee_code = icmph->icmp6_code;
278 serr->ee.ee_pad = 0;
279 serr->ee.ee_info = info;
280 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700281 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
282 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 serr->port = port;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 __skb_pull(skb, payload - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300286 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (sock_queue_err_skb(sk, skb))
289 kfree_skb(skb);
290}
291
David S. Miller4c9483b2011-03-12 16:22:43 -0500292void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700294 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct sock_exterr_skb *serr;
296 struct ipv6hdr *iph;
297 struct sk_buff *skb;
298
299 if (!np->recverr)
300 return;
301
302 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
303 if (!skb)
304 return;
305
Brian Haleyd40a4de2010-05-03 15:44:27 +0000306 skb->protocol = htons(ETH_P_IPV6);
307
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300308 skb_put(skb, sizeof(struct ipv6hdr));
309 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700310 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000311 iph->daddr = fl6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 serr = SKB_EXT_ERR(skb);
314 serr->ee.ee_errno = err;
315 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900316 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 serr->ee.ee_code = 0;
318 serr->ee.ee_pad = 0;
319 serr->ee.ee_info = info;
320 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700321 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500322 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700324 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300325 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (sock_queue_err_skb(sk, skb))
328 kfree_skb(skb);
329}
330
David S. Miller4c9483b2011-03-12 16:22:43 -0500331void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000332{
333 struct ipv6_pinfo *np = inet6_sk(sk);
334 struct ipv6hdr *iph;
335 struct sk_buff *skb;
336 struct ip6_mtuinfo *mtu_info;
337
338 if (!np->rxopt.bits.rxpmtu)
339 return;
340
341 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
342 if (!skb)
343 return;
344
345 skb_put(skb, sizeof(struct ipv6hdr));
346 skb_reset_network_header(skb);
347 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000348 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000349
350 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000351
352 mtu_info->ip6m_mtu = mtu;
353 mtu_info->ip6m_addr.sin6_family = AF_INET6;
354 mtu_info->ip6m_addr.sin6_port = 0;
355 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500356 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000357 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000358
359 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
360 skb_reset_transport_header(skb);
361
362 skb = xchg(&np->rxpmtu, skb);
363 kfree_skb(skb);
364}
365
Julian Anastasov34b99df2015-06-23 08:34:39 +0300366/* For some errors we have valid addr_offset even with zero payload and
367 * zero port. Also, addr_offset should be supported if port is set.
368 */
369static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
370{
371 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
372 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
373 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
374}
375
Willem de Bruijnc247f052015-03-07 20:33:22 -0500376/* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
377 *
378 * At one point, excluding local errors was a quick test to identify icmp/icmp6
379 * errors. This is no longer true, but the test remained, so the v6 stack,
380 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
381 *
382 * Timestamp code paths do not initialize the fields expected by cmsg:
383 * the PKTINFO fields in skb->cb[]. Fill those in here.
384 */
385static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
386 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500387{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500388 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
389 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
390 return true;
391
392 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
393 return false;
394
395 if (!skb->dev)
396 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500397
398 if (skb->protocol == htons(ETH_P_IPV6))
Willem de Bruijnc247f052015-03-07 20:33:22 -0500399 IP6CB(skb)->iif = skb->dev->ifindex;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500400 else
Willem de Bruijnc247f052015-03-07 20:33:22 -0500401 PKTINFO_SKB_CB(skb)->ipi_ifindex = skb->dev->ifindex;
402
403 return true;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500404}
405
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900406/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * Handle MSG_ERRQUEUE
408 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100409int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct ipv6_pinfo *np = inet6_sk(sk);
412 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400413 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100414 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct {
416 struct sock_extended_err ee;
417 struct sockaddr_in6 offender;
418 } errhdr;
419 int err;
420 int copied;
421
422 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400423 skb = sock_dequeue_err_skb(sk);
Ian Morris63159f22015-03-29 14:00:04 +0100424 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto out;
426
427 copied = skb->len;
428 if (copied > len) {
429 msg->msg_flags |= MSG_TRUNC;
430 copied = len;
431 }
David S. Miller51f3d022014-11-05 16:46:40 -0500432 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (err)
434 goto out_free_skb;
435
436 sock_recv_timestamp(msg, sk, skb);
437
438 serr = SKB_EXT_ERR(skb);
439
Julian Anastasov34b99df2015-06-23 08:34:39 +0300440 if (sin && ipv6_datagram_support_addr(serr)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700441 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 sin->sin6_family = AF_INET6;
443 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900444 sin->sin6_port = serr->port;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000445 if (skb->protocol == htons(ETH_P_IPV6)) {
YOSHIFUJI Hideaki / 吉藤英明6c40d102013-01-08 06:44:23 +0000446 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
447 struct ipv6hdr, daddr);
448 sin->sin6_addr = ip6h->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (np->sndflow)
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000450 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000451 sin->sin6_scope_id =
452 ipv6_iface_scope_id(&sin->sin6_addr,
453 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700455 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
456 &sin->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000457 sin->sin6_scope_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100459 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
463 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500464 memset(sin, 0, sizeof(*sin));
Willem de Bruijnc247f052015-03-07 20:33:22 -0500465
466 if (ip6_datagram_support_cmsg(skb, serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 sin->sin6_family = AF_INET6;
Willem de Bruijnc247f052015-03-07 20:33:22 -0500468 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100469 ip6_datagram_recv_common_ctl(sk, msg, skb);
Brian Haleyd40a4de2010-05-03 15:44:27 +0000470 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000471 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100473 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000474 sin->sin6_scope_id =
475 ipv6_iface_scope_id(&sin->sin6_addr,
476 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700478 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
479 &sin->sin6_addr);
Willem de Bruijnf8121162015-01-15 13:18:40 -0500480 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ip_cmsg_recv(msg, skb);
482 }
483 }
484
485 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
486
487 /* Now we could try to dump offended packet options */
488
489 msg->msg_flags |= MSG_ERRQUEUE;
490 err = copied;
491
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900492out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 kfree_skb(skb);
494out:
495 return err;
496}
Chris Elstona495f832012-04-29 21:48:53 +0000497EXPORT_SYMBOL_GPL(ipv6_recv_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Brian Haley4b340ae2010-04-23 11:26:09 +0000499/*
500 * Handle IPV6_RECVPATHMTU
501 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100502int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
503 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000504{
505 struct ipv6_pinfo *np = inet6_sk(sk);
506 struct sk_buff *skb;
Brian Haley4b340ae2010-04-23 11:26:09 +0000507 struct ip6_mtuinfo mtu_info;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100508 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Brian Haley4b340ae2010-04-23 11:26:09 +0000509 int err;
510 int copied;
511
512 err = -EAGAIN;
513 skb = xchg(&np->rxpmtu, NULL);
Ian Morris63159f22015-03-29 14:00:04 +0100514 if (!skb)
Brian Haley4b340ae2010-04-23 11:26:09 +0000515 goto out;
516
517 copied = skb->len;
518 if (copied > len) {
519 msg->msg_flags |= MSG_TRUNC;
520 copied = len;
521 }
David S. Miller51f3d022014-11-05 16:46:40 -0500522 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Brian Haley4b340ae2010-04-23 11:26:09 +0000523 if (err)
524 goto out_free_skb;
525
526 sock_recv_timestamp(msg, sk, skb);
527
528 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
529
Brian Haley4b340ae2010-04-23 11:26:09 +0000530 if (sin) {
531 sin->sin6_family = AF_INET6;
532 sin->sin6_flowinfo = 0;
533 sin->sin6_port = 0;
534 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000535 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100536 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000537 }
538
539 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
540
541 err = copied;
542
543out_free_skb:
544 kfree_skb(skb);
545out:
546 return err;
547}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100550void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
551 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct ipv6_pinfo *np = inet6_sk(sk);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100554 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (np->rxopt.bits.rxinfo) {
557 struct in6_pktinfo src_info;
558
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100559 if (is_ipv6) {
560 src_info.ipi6_ifindex = IP6CB(skb)->iif;
561 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
562 } else {
563 src_info.ipi6_ifindex =
564 PKTINFO_SKB_CB(skb)->ipi_ifindex;
565 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
566 &src_info.ipi6_addr);
567 }
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500568
569 if (src_info.ipi6_ifindex >= 0)
570 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
571 sizeof(src_info), &src_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100573}
574
575void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
576 struct sk_buff *skb)
577{
578 struct ipv6_pinfo *np = inet6_sk(sk);
579 struct inet6_skb_parm *opt = IP6CB(skb);
580 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700583 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
585 }
586
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900587 if (np->rxopt.bits.rxtclass) {
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +0000588 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900589 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
590 }
591
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000592 if (np->rxopt.bits.rxflow) {
593 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
594 if (flowinfo)
595 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900597
598 /* HbH is allowed only once */
Florian Westphal8b58a392015-07-08 23:32:12 +0200599 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
600 u8 *ptr = nh + sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
602 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900603
604 if (opt->lastopt &&
605 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
606 /*
607 * Silly enough, but we need to reparse in order to
608 * report extension headers (except for HbH)
609 * in order.
610 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900611 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900612 * (and WILL NOT be) defined because
613 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
614 */
615 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700616 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900617
618 while (off <= opt->lastopt) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000619 unsigned int len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700620 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900621
Eldad Zackb5a42572012-04-01 07:49:03 +0000622 switch (nexthdr) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900623 case IPPROTO_DSTOPTS:
624 nexthdr = ptr[0];
625 len = (ptr[1] + 1) << 3;
626 if (np->rxopt.bits.dstopts)
627 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
628 break;
629 case IPPROTO_ROUTING:
630 nexthdr = ptr[0];
631 len = (ptr[1] + 1) << 3;
632 if (np->rxopt.bits.srcrt)
633 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
634 break;
635 case IPPROTO_AH:
636 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900637 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900638 break;
639 default:
640 nexthdr = ptr[0];
641 len = (ptr[1] + 1) << 3;
642 break;
643 }
644
645 off += len;
646 }
647 }
648
649 /* socket options in old style */
650 if (np->rxopt.bits.rxoinfo) {
651 struct in6_pktinfo src_info;
652
653 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000654 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900655 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
656 }
657 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700658 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900659 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
660 }
Florian Westphal8b58a392015-07-08 23:32:12 +0200661 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
662 u8 *ptr = nh + sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900663 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
664 }
665 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700666 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900667 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900669 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700670 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900671 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900673 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700674 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900675 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200677 if (np->rxopt.bits.rxorigdstaddr) {
678 struct sockaddr_in6 sin6;
Eric Dumazet747465e2012-01-16 19:27:39 +0000679 __be16 *ports = (__be16 *) skb_transport_header(skb);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200680
681 if (skb_transport_offset(skb) + 4 <= skb->len) {
682 /* All current transport protocols have the port numbers in the
683 * first four bytes of the transport header and this function is
684 * written with this assumption in mind.
685 */
686
687 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000688 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200689 sin6.sin6_port = ports[1];
690 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000691 sin6.sin6_scope_id =
692 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
693 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200694
695 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
696 }
697 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100698}
699
700void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
701 struct sk_buff *skb)
702{
703 ip6_datagram_recv_common_ctl(sk, msg, skb);
704 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
Tom Parkin8e72d372013-01-31 01:02:25 +0000706EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Tom Parkin73df66f2013-01-31 01:02:24 +0000708int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
709 struct msghdr *msg, struct flowi6 *fl6,
710 struct ipv6_txoptions *opt,
711 int *hlimit, int *tclass, int *dontfrag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct in6_pktinfo *src_info;
714 struct cmsghdr *cmsg;
715 struct ipv6_rt_hdr *rthdr;
716 struct ipv6_opt_hdr *hdr;
717 int len;
718 int err = 0;
719
Gu Zhengf95b4142014-12-11 11:22:04 +0800720 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (!CMSG_OK(msg, cmsg)) {
724 err = -EINVAL;
725 goto exit_f;
726 }
727
728 if (cmsg->cmsg_level != SOL_IPV6)
729 continue;
730
731 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900732 case IPV6_PKTINFO:
733 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900734 {
735 struct net_device *dev = NULL;
736
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900737 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 err = -EINVAL;
739 goto exit_f;
740 }
741
742 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500745 if (fl6->flowi6_oif &&
746 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500748 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900751 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Eric Dumazet536b2e92009-11-02 12:21:06 +0100753 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500754 if (fl6->flowi6_oif) {
755 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100756 if (!dev) {
757 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900758 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100759 }
760 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
761 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900762 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100763 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900764
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900765 if (addr_type != IPV6_ADDR_ANY) {
766 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000767 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000768 !ipv6_chk_addr(net, &src_info->ipi6_addr,
FX Le Bail7c90cc22014-01-22 07:42:37 +0100769 strict ? dev : NULL, 0) &&
770 !ipv6_chk_acast_addr_src(net, dev,
771 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900772 err = -EINVAL;
773 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000774 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900776
Eric Dumazet536b2e92009-11-02 12:21:06 +0100777 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900779 if (err)
780 goto exit_f;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900786 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 err = -EINVAL;
788 goto exit_f;
789 }
790
David S. Miller4c9483b2011-03-12 16:22:43 -0500791 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
792 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 err = -EINVAL;
794 goto exit_f;
795 }
796 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500797 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900800 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900802 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 err = -EINVAL;
804 goto exit_f;
805 }
806
807 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
808 len = ((hdr->hdrlen + 1) << 3);
809 if (cmsg->cmsg_len < CMSG_LEN(len)) {
810 err = -EINVAL;
811 goto exit_f;
812 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000813 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 err = -EPERM;
815 goto exit_f;
816 }
817 opt->opt_nflen += len;
818 opt->hopopt = hdr;
819 break;
820
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900821 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900822 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 err = -EINVAL;
824 goto exit_f;
825 }
826
827 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
828 len = ((hdr->hdrlen + 1) << 3);
829 if (cmsg->cmsg_len < CMSG_LEN(len)) {
830 err = -EINVAL;
831 goto exit_f;
832 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000833 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 err = -EPERM;
835 goto exit_f;
836 }
837 if (opt->dst1opt) {
838 err = -EINVAL;
839 goto exit_f;
840 }
841 opt->opt_flen += len;
842 opt->dst1opt = hdr;
843 break;
844
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900845 case IPV6_DSTOPTS:
846 case IPV6_RTHDRDSTOPTS:
847 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
848 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)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900859 err = -EPERM;
860 goto exit_f;
861 }
862 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
863 opt->opt_flen += len;
864 opt->dst1opt = hdr;
865 } else {
866 opt->opt_nflen += len;
867 opt->dst0opt = hdr;
868 }
869 break;
870
871 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900873 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 err = -EINVAL;
875 goto exit_f;
876 }
877
878 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
879
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700880 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000881#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700882 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800883 if (rthdr->hdrlen != 2 ||
884 rthdr->segments_left != 1) {
885 err = -EINVAL;
886 goto exit_f;
887 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700888 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700889#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700890 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 err = -EINVAL;
892 goto exit_f;
893 }
894
895 len = ((rthdr->hdrlen + 1) << 3);
896
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900897 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 err = -EINVAL;
899 goto exit_f;
900 }
901
902 /* segments left must also match */
903 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
904 err = -EINVAL;
905 goto exit_f;
906 }
907
908 opt->opt_nflen += len;
909 opt->srcrt = rthdr;
910
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900911 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
913
914 opt->opt_nflen += dsthdrlen;
915 opt->dst0opt = opt->dst1opt;
916 opt->dst1opt = NULL;
917 opt->opt_flen -= dsthdrlen;
918 }
919
920 break;
921
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900922 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 case IPV6_HOPLIMIT:
924 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
925 err = -EINVAL;
926 goto exit_f;
927 }
928
929 *hlimit = *(int *)CMSG_DATA(cmsg);
Shan Weie8766fc2008-06-10 15:50:55 +0800930 if (*hlimit < -1 || *hlimit > 0xff) {
931 err = -EINVAL;
932 goto exit_f;
933 }
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
936
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900937 case IPV6_TCLASS:
938 {
939 int tc;
940
941 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000942 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900943 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900944
945 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700946 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900947 goto exit_f;
948
949 err = 0;
950 *tclass = tc;
951
952 break;
953 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000954
955 case IPV6_DONTFRAG:
956 {
957 int df;
958
959 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000960 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +0000961 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +0000962
963 df = *(int *)CMSG_DATA(cmsg);
964 if (df < 0 || df > 1)
965 goto exit_f;
966
967 err = 0;
968 *dontfrag = df;
969
970 break;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800973 net_dbg_ratelimited("invalid cmsg type: %d\n",
974 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -0700976 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
980exit_f:
981 return err;
982}
Tom Parkin73df66f2013-01-31 01:02:24 +0000983EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +0000984
985void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
986 __u16 srcp, __u16 destp, int bucket)
987{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +0000988 const struct in6_addr *dest, *src;
989
Eric Dumazetefe42082013-10-03 15:42:29 -0700990 dest = &sp->sk_v6_daddr;
991 src = &sp->sk_v6_rcv_saddr;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +0000992 seq_printf(seq,
993 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +0200994 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +0000995 bucket,
996 src->s6_addr32[0], src->s6_addr32[1],
997 src->s6_addr32[2], src->s6_addr32[3], srcp,
998 dest->s6_addr32[0], dest->s6_addr32[1],
999 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1000 sp->sk_state,
1001 sk_wmem_alloc_get(sp),
1002 sk_rmem_alloc_get(sp),
1003 0, 0L, 0,
1004 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1005 0,
1006 sock_i_ino(sp),
1007 atomic_read(&sp->sk_refcnt), sp,
1008 atomic_read(&sp->sk_drops));
1009}