blob: ccf40550c475d31bcbc0565daa4a81428e5c9c58 [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
169 addr_type = ipv6_addr_type(&usin->sin6_addr);
170
171 if (addr_type == IPV6_ADDR_ANY) {
172 /*
173 * connect to self
174 */
175 usin->sin6_addr.s6_addr[15] = 0x01;
176 }
177
178 daddr = &usin->sin6_addr;
179
180 if (addr_type == IPV6_ADDR_MAPPED) {
181 struct sockaddr_in sin;
182
183 if (__ipv6_only_sock(sk)) {
184 err = -ENETUNREACH;
185 goto out;
186 }
187 sin.sin_family = AF_INET;
188 sin.sin_addr.s_addr = daddr->s6_addr32[3];
189 sin.sin_port = usin->sin6_port;
190
Eric Dumazet03645a12015-07-14 08:10:22 +0200191 err = __ip4_datagram_connect(sk,
192 (struct sockaddr *) &sin,
193 sizeof(sin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195ipv4_connected:
196 if (err)
197 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900198
Eric Dumazetefe42082013-10-03 15:42:29 -0700199 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Max Matveevc15fea22011-08-05 03:56:30 -0700201 if (ipv6_addr_any(&np->saddr) ||
202 ipv6_mapped_addr_any(&np->saddr))
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000203 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Eric Dumazetefe42082013-10-03 15:42:29 -0700205 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
206 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000207 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
Eric Dumazetefe42082013-10-03 15:42:29 -0700208 &sk->sk_v6_rcv_saddr);
Eric Dumazet719f8352010-09-08 05:08:44 +0000209 if (sk->sk_prot->rehash)
210 sk->sk_prot->rehash(sk);
211 }
Brian Haleyb301e822009-10-07 13:58:25 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto out;
214 }
215
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000216 if (__ipv6_addr_needs_scope_id(addr_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (addr_len >= sizeof(struct sockaddr_in6) &&
218 usin->sin6_scope_id) {
219 if (sk->sk_bound_dev_if &&
220 sk->sk_bound_dev_if != usin->sin6_scope_id) {
221 err = -EINVAL;
222 goto out;
223 }
224 sk->sk_bound_dev_if = usin->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
Brian Haley1ac4f002008-01-08 23:52:21 -0800227 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
228 sk->sk_bound_dev_if = np->mcast_oif;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Connect to link-local address requires an interface */
231 if (!sk->sk_bound_dev_if) {
232 err = -EINVAL;
233 goto out;
234 }
235 }
236
Eric Dumazetefe42082013-10-03 15:42:29 -0700237 sk->sk_v6_daddr = *daddr;
Martin KaFai Lau80fbdb22016-04-11 15:29:34 -0700238 np->flow_label = fl6_flowlabel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000240 inet->inet_dport = usin->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /*
243 * Check for a route to destination an obtain the
244 * destination cache for it.
245 */
246
Martin KaFai Lau33c162a2016-04-11 15:29:36 -0700247 err = ip6_datagram_dst_update(sk, true);
Martin KaFai Lau7e2040d2016-04-11 15:29:35 -0700248 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 sk->sk_state = TCP_ESTABLISHED;
Tom Herbert877d1f62015-07-28 16:02:05 -0700252 sk_set_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return err;
255}
Guillaume Nault0382a252016-11-29 13:09:44 +0100256EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
Eric Dumazet03645a12015-07-14 08:10:22 +0200257
258int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
259{
260 int res;
261
262 lock_sock(sk);
263 res = __ip6_datagram_connect(sk, uaddr, addr_len);
264 release_sock(sk);
265 return res;
266}
Chris Elstona495f832012-04-29 21:48:53 +0000267EXPORT_SYMBOL_GPL(ip6_datagram_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Hannes Frederic Sowa82b276c2014-01-20 05:16:39 +0100269int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
270 int addr_len)
271{
272 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
273 if (sin6->sin6_family != AF_INET6)
274 return -EAFNOSUPPORT;
275 return ip6_datagram_connect(sk, uaddr, addr_len);
276}
277EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
278
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900279void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4adc2006-11-14 20:56:00 -0800280 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300283 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct sock_exterr_skb *serr;
285
286 if (!np->recverr)
287 return;
288
289 skb = skb_clone(skb, GFP_ATOMIC);
290 if (!skb)
291 return;
292
Brian Haleyd40a4de2010-05-03 15:44:27 +0000293 skb->protocol = htons(ETH_P_IPV6);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 serr = SKB_EXT_ERR(skb);
296 serr->ee.ee_errno = err;
297 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900298 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 serr->ee.ee_code = icmph->icmp6_code;
300 serr->ee.ee_pad = 0;
301 serr->ee.ee_info = info;
302 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700303 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
304 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 serr->port = port;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 __skb_pull(skb, payload - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300308 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if (sock_queue_err_skb(sk, skb))
311 kfree_skb(skb);
312}
313
David S. Miller4c9483b2011-03-12 16:22:43 -0500314void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700316 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct sock_exterr_skb *serr;
318 struct ipv6hdr *iph;
319 struct sk_buff *skb;
320
321 if (!np->recverr)
322 return;
323
324 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
325 if (!skb)
326 return;
327
Brian Haleyd40a4de2010-05-03 15:44:27 +0000328 skb->protocol = htons(ETH_P_IPV6);
329
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300330 skb_put(skb, sizeof(struct ipv6hdr));
331 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700332 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000333 iph->daddr = fl6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 serr = SKB_EXT_ERR(skb);
336 serr->ee.ee_errno = err;
337 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900338 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 serr->ee.ee_code = 0;
340 serr->ee.ee_pad = 0;
341 serr->ee.ee_info = info;
342 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700343 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500344 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700346 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300347 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (sock_queue_err_skb(sk, skb))
350 kfree_skb(skb);
351}
352
David S. Miller4c9483b2011-03-12 16:22:43 -0500353void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000354{
355 struct ipv6_pinfo *np = inet6_sk(sk);
356 struct ipv6hdr *iph;
357 struct sk_buff *skb;
358 struct ip6_mtuinfo *mtu_info;
359
360 if (!np->rxopt.bits.rxpmtu)
361 return;
362
363 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
364 if (!skb)
365 return;
366
367 skb_put(skb, sizeof(struct ipv6hdr));
368 skb_reset_network_header(skb);
369 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000370 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000371
372 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000373
374 mtu_info->ip6m_mtu = mtu;
375 mtu_info->ip6m_addr.sin6_family = AF_INET6;
376 mtu_info->ip6m_addr.sin6_port = 0;
377 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500378 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000379 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000380
381 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
382 skb_reset_transport_header(skb);
383
384 skb = xchg(&np->rxpmtu, skb);
385 kfree_skb(skb);
386}
387
Julian Anastasov34b99df2015-06-23 08:34:39 +0300388/* For some errors we have valid addr_offset even with zero payload and
389 * zero port. Also, addr_offset should be supported if port is set.
390 */
391static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
392{
393 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
394 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
395 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
396}
397
Willem de Bruijnc247f052015-03-07 20:33:22 -0500398/* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
399 *
400 * At one point, excluding local errors was a quick test to identify icmp/icmp6
401 * errors. This is no longer true, but the test remained, so the v6 stack,
402 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
403 *
404 * Timestamp code paths do not initialize the fields expected by cmsg:
405 * the PKTINFO fields in skb->cb[]. Fill those in here.
406 */
407static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
408 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500409{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500410 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
411 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
412 return true;
413
414 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
415 return false;
416
417 if (!skb->dev)
418 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500419
420 if (skb->protocol == htons(ETH_P_IPV6))
Willem de Bruijnc247f052015-03-07 20:33:22 -0500421 IP6CB(skb)->iif = skb->dev->ifindex;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500422 else
Willem de Bruijnc247f052015-03-07 20:33:22 -0500423 PKTINFO_SKB_CB(skb)->ipi_ifindex = skb->dev->ifindex;
424
425 return true;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500426}
427
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * Handle MSG_ERRQUEUE
430 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100431int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct ipv6_pinfo *np = inet6_sk(sk);
434 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400435 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100436 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct {
438 struct sock_extended_err ee;
439 struct sockaddr_in6 offender;
440 } errhdr;
441 int err;
442 int copied;
443
444 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400445 skb = sock_dequeue_err_skb(sk);
Ian Morris63159f22015-03-29 14:00:04 +0100446 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 goto out;
448
449 copied = skb->len;
450 if (copied > len) {
451 msg->msg_flags |= MSG_TRUNC;
452 copied = len;
453 }
David S. Miller51f3d022014-11-05 16:46:40 -0500454 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700455 if (unlikely(err)) {
456 kfree_skb(skb);
457 return err;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 sock_recv_timestamp(msg, sk, skb);
460
461 serr = SKB_EXT_ERR(skb);
462
Julian Anastasov34b99df2015-06-23 08:34:39 +0300463 if (sin && ipv6_datagram_support_addr(serr)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700464 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 sin->sin6_family = AF_INET6;
466 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900467 sin->sin6_port = serr->port;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000468 if (skb->protocol == htons(ETH_P_IPV6)) {
YOSHIFUJI Hideaki / 吉藤英明6c40d102013-01-08 06:44:23 +0000469 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
470 struct ipv6hdr, daddr);
471 sin->sin6_addr = ip6h->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (np->sndflow)
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000473 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
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(*(__be32 *)(nh + serr->addr_offset),
479 &sin->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000480 sin->sin6_scope_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100482 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
485 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
486 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500487 memset(sin, 0, sizeof(*sin));
Willem de Bruijnc247f052015-03-07 20:33:22 -0500488
489 if (ip6_datagram_support_cmsg(skb, serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 sin->sin6_family = AF_INET6;
Willem de Bruijnc247f052015-03-07 20:33:22 -0500491 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100492 ip6_datagram_recv_common_ctl(sk, msg, skb);
Brian Haleyd40a4de2010-05-03 15:44:27 +0000493 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000494 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100496 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000497 sin->sin6_scope_id =
498 ipv6_iface_scope_id(&sin->sin6_addr,
499 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700501 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
502 &sin->sin6_addr);
Willem de Bruijnf8121162015-01-15 13:18:40 -0500503 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 ip_cmsg_recv(msg, skb);
505 }
506 }
507
508 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
509
510 /* Now we could try to dump offended packet options */
511
512 msg->msg_flags |= MSG_ERRQUEUE;
513 err = copied;
514
Eric Dumazet960a2622016-04-21 22:27:32 -0700515 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516out:
517 return err;
518}
Chris Elstona495f832012-04-29 21:48:53 +0000519EXPORT_SYMBOL_GPL(ipv6_recv_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Brian Haley4b340ae2010-04-23 11:26:09 +0000521/*
522 * Handle IPV6_RECVPATHMTU
523 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100524int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
525 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000526{
527 struct ipv6_pinfo *np = inet6_sk(sk);
528 struct sk_buff *skb;
Brian Haley4b340ae2010-04-23 11:26:09 +0000529 struct ip6_mtuinfo mtu_info;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100530 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Brian Haley4b340ae2010-04-23 11:26:09 +0000531 int err;
532 int copied;
533
534 err = -EAGAIN;
535 skb = xchg(&np->rxpmtu, NULL);
Ian Morris63159f22015-03-29 14:00:04 +0100536 if (!skb)
Brian Haley4b340ae2010-04-23 11:26:09 +0000537 goto out;
538
539 copied = skb->len;
540 if (copied > len) {
541 msg->msg_flags |= MSG_TRUNC;
542 copied = len;
543 }
David S. Miller51f3d022014-11-05 16:46:40 -0500544 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Brian Haley4b340ae2010-04-23 11:26:09 +0000545 if (err)
546 goto out_free_skb;
547
548 sock_recv_timestamp(msg, sk, skb);
549
550 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
551
Brian Haley4b340ae2010-04-23 11:26:09 +0000552 if (sin) {
553 sin->sin6_family = AF_INET6;
554 sin->sin6_flowinfo = 0;
555 sin->sin6_port = 0;
556 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000557 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100558 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000559 }
560
561 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
562
563 err = copied;
564
565out_free_skb:
566 kfree_skb(skb);
567out:
568 return err;
569}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100572void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
573 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct ipv6_pinfo *np = inet6_sk(sk);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100576 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (np->rxopt.bits.rxinfo) {
579 struct in6_pktinfo src_info;
580
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100581 if (is_ipv6) {
582 src_info.ipi6_ifindex = IP6CB(skb)->iif;
583 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
584 } else {
585 src_info.ipi6_ifindex =
586 PKTINFO_SKB_CB(skb)->ipi_ifindex;
587 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
588 &src_info.ipi6_addr);
589 }
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500590
591 if (src_info.ipi6_ifindex >= 0)
592 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
593 sizeof(src_info), &src_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100595}
596
597void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
598 struct sk_buff *skb)
599{
600 struct ipv6_pinfo *np = inet6_sk(sk);
601 struct inet6_skb_parm *opt = IP6CB(skb);
602 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700605 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
607 }
608
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900609 if (np->rxopt.bits.rxtclass) {
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +0000610 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900611 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
612 }
613
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000614 if (np->rxopt.bits.rxflow) {
615 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
616 if (flowinfo)
617 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900619
620 /* HbH is allowed only once */
Florian Westphal8b58a392015-07-08 23:32:12 +0200621 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
622 u8 *ptr = nh + sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
624 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900625
626 if (opt->lastopt &&
627 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
628 /*
629 * Silly enough, but we need to reparse in order to
630 * report extension headers (except for HbH)
631 * in order.
632 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900633 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900634 * (and WILL NOT be) defined because
635 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
636 */
637 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700638 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900639
640 while (off <= opt->lastopt) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000641 unsigned int len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700642 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900643
Eldad Zackb5a42572012-04-01 07:49:03 +0000644 switch (nexthdr) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900645 case IPPROTO_DSTOPTS:
646 nexthdr = ptr[0];
647 len = (ptr[1] + 1) << 3;
648 if (np->rxopt.bits.dstopts)
649 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
650 break;
651 case IPPROTO_ROUTING:
652 nexthdr = ptr[0];
653 len = (ptr[1] + 1) << 3;
654 if (np->rxopt.bits.srcrt)
655 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
656 break;
657 case IPPROTO_AH:
658 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900659 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900660 break;
661 default:
662 nexthdr = ptr[0];
663 len = (ptr[1] + 1) << 3;
664 break;
665 }
666
667 off += len;
668 }
669 }
670
671 /* socket options in old style */
672 if (np->rxopt.bits.rxoinfo) {
673 struct in6_pktinfo src_info;
674
675 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000676 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900677 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
678 }
679 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700680 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900681 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
682 }
Florian Westphal8b58a392015-07-08 23:32:12 +0200683 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
684 u8 *ptr = nh + sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900685 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
686 }
687 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700688 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900689 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900691 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700692 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900693 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900695 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700696 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900697 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200699 if (np->rxopt.bits.rxorigdstaddr) {
700 struct sockaddr_in6 sin6;
Eric Dumazet747465e2012-01-16 19:27:39 +0000701 __be16 *ports = (__be16 *) skb_transport_header(skb);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200702
703 if (skb_transport_offset(skb) + 4 <= skb->len) {
704 /* All current transport protocols have the port numbers in the
705 * first four bytes of the transport header and this function is
706 * written with this assumption in mind.
707 */
708
709 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000710 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200711 sin6.sin6_port = ports[1];
712 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000713 sin6.sin6_scope_id =
714 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
715 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200716
717 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
718 }
719 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100720}
721
722void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
723 struct sk_buff *skb)
724{
725 ip6_datagram_recv_common_ctl(sk, msg, skb);
726 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
Tom Parkin8e72d372013-01-31 01:02:25 +0000728EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Tom Parkin73df66f2013-01-31 01:02:24 +0000730int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
731 struct msghdr *msg, struct flowi6 *fl6,
Wei Wang26879da2016-05-02 21:40:07 -0700732 struct ipcm6_cookie *ipc6, 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;
Wei Wang26879da2016-05-02 21:40:07 -0700738 struct ipv6_txoptions *opt = ipc6->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int len;
740 int err = 0;
741
Gu Zhengf95b4142014-12-11 11:22:04 +0800742 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (!CMSG_OK(msg, cmsg)) {
746 err = -EINVAL;
747 goto exit_f;
748 }
749
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400750 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700751 err = __sock_cmsg_send(sk, msg, cmsg, sockc);
752 if (err)
753 return err;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400754 continue;
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (cmsg->cmsg_level != SOL_IPV6)
758 continue;
759
760 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900761 case IPV6_PKTINFO:
762 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900763 {
764 struct net_device *dev = NULL;
765
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900766 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 err = -EINVAL;
768 goto exit_f;
769 }
770
771 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500774 if (fl6->flowi6_oif &&
775 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500777 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900780 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Eric Dumazet536b2e92009-11-02 12:21:06 +0100782 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500783 if (fl6->flowi6_oif) {
784 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100785 if (!dev) {
786 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900787 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100788 }
789 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
790 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900791 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100792 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900793
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900794 if (addr_type != IPV6_ADDR_ANY) {
795 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000796 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000797 !ipv6_chk_addr(net, &src_info->ipi6_addr,
FX Le Bail7c90cc22014-01-22 07:42:37 +0100798 strict ? dev : NULL, 0) &&
799 !ipv6_chk_acast_addr_src(net, dev,
800 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900801 err = -EINVAL;
802 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000803 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900805
Eric Dumazet536b2e92009-11-02 12:21:06 +0100806 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900808 if (err)
809 goto exit_f;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900815 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 err = -EINVAL;
817 goto exit_f;
818 }
819
David S. Miller4c9483b2011-03-12 16:22:43 -0500820 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
821 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 err = -EINVAL;
823 goto exit_f;
824 }
825 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500826 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 break;
828
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900829 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900831 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 err = -EINVAL;
833 goto exit_f;
834 }
835
836 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
837 len = ((hdr->hdrlen + 1) << 3);
838 if (cmsg->cmsg_len < CMSG_LEN(len)) {
839 err = -EINVAL;
840 goto exit_f;
841 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000842 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 err = -EPERM;
844 goto exit_f;
845 }
846 opt->opt_nflen += len;
847 opt->hopopt = hdr;
848 break;
849
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900850 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900851 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 err = -EINVAL;
853 goto exit_f;
854 }
855
856 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
857 len = ((hdr->hdrlen + 1) << 3);
858 if (cmsg->cmsg_len < CMSG_LEN(len)) {
859 err = -EINVAL;
860 goto exit_f;
861 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000862 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 err = -EPERM;
864 goto exit_f;
865 }
866 if (opt->dst1opt) {
867 err = -EINVAL;
868 goto exit_f;
869 }
870 opt->opt_flen += len;
871 opt->dst1opt = hdr;
872 break;
873
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900874 case IPV6_DSTOPTS:
875 case IPV6_RTHDRDSTOPTS:
876 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
877 err = -EINVAL;
878 goto exit_f;
879 }
880
881 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
882 len = ((hdr->hdrlen + 1) << 3);
883 if (cmsg->cmsg_len < CMSG_LEN(len)) {
884 err = -EINVAL;
885 goto exit_f;
886 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000887 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900888 err = -EPERM;
889 goto exit_f;
890 }
891 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
892 opt->opt_flen += len;
893 opt->dst1opt = hdr;
894 } else {
895 opt->opt_nflen += len;
896 opt->dst0opt = hdr;
897 }
898 break;
899
900 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900902 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 err = -EINVAL;
904 goto exit_f;
905 }
906
907 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
908
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700909 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000910#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700911 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800912 if (rthdr->hdrlen != 2 ||
913 rthdr->segments_left != 1) {
914 err = -EINVAL;
915 goto exit_f;
916 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700917 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700918#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700919 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 err = -EINVAL;
921 goto exit_f;
922 }
923
924 len = ((rthdr->hdrlen + 1) << 3);
925
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900926 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 err = -EINVAL;
928 goto exit_f;
929 }
930
931 /* segments left must also match */
932 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
933 err = -EINVAL;
934 goto exit_f;
935 }
936
937 opt->opt_nflen += len;
938 opt->srcrt = rthdr;
939
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900940 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
942
943 opt->opt_nflen += dsthdrlen;
944 opt->dst0opt = opt->dst1opt;
945 opt->dst1opt = NULL;
946 opt->opt_flen -= dsthdrlen;
947 }
948
949 break;
950
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900951 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 case IPV6_HOPLIMIT:
953 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
954 err = -EINVAL;
955 goto exit_f;
956 }
957
Wei Wang26879da2016-05-02 21:40:07 -0700958 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
959 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
Shan Weie8766fc2008-06-10 15:50:55 +0800960 err = -EINVAL;
961 goto exit_f;
962 }
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
965
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900966 case IPV6_TCLASS:
967 {
968 int tc;
969
970 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000971 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900972 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900973
974 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700975 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900976 goto exit_f;
977
978 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700979 ipc6->tclass = tc;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900980
981 break;
982 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000983
984 case IPV6_DONTFRAG:
985 {
986 int df;
987
988 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000989 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +0000990 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +0000991
992 df = *(int *)CMSG_DATA(cmsg);
993 if (df < 0 || df > 1)
994 goto exit_f;
995
996 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700997 ipc6->dontfrag = df;
Brian Haley13b52cd2010-04-23 11:26:08 +0000998
999 break;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -08001002 net_dbg_ratelimited("invalid cmsg type: %d\n",
1003 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -07001005 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
1009exit_f:
1010 return err;
1011}
Tom Parkin73df66f2013-01-31 01:02:24 +00001012EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001013
1014void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1015 __u16 srcp, __u16 destp, int bucket)
1016{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001017 const struct in6_addr *dest, *src;
1018
Eric Dumazetefe42082013-10-03 15:42:29 -07001019 dest = &sp->sk_v6_daddr;
1020 src = &sp->sk_v6_rcv_saddr;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001021 seq_printf(seq,
1022 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001023 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001024 bucket,
1025 src->s6_addr32[0], src->s6_addr32[1],
1026 src->s6_addr32[2], src->s6_addr32[3], srcp,
1027 dest->s6_addr32[0], dest->s6_addr32[1],
1028 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1029 sp->sk_state,
1030 sk_wmem_alloc_get(sp),
1031 sk_rmem_alloc_get(sp),
1032 0, 0L, 0,
1033 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1034 0,
1035 sock_i_ino(sp),
1036 atomic_read(&sp->sk_refcnt), sp,
1037 atomic_read(&sp->sk_drops));
1038}