blob: 442ec1f39ed19bc1b2b96301c84e504685f9f0a7 [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.
Willem de Bruijnc247f052015-03-07 20:33:22 -0500403 */
404static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
405 struct sock_exterr_skb *serr)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500406{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500407 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
408 serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
409 return true;
410
411 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
412 return false;
413
Willem de Bruijn8c04e2a2017-04-12 19:24:35 -0400414 if (!IP6CB(skb)->iif)
Willem de Bruijnc247f052015-03-07 20:33:22 -0500415 return false;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500416
Willem de Bruijnc247f052015-03-07 20:33:22 -0500417 return true;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500418}
419
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900420/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * Handle MSG_ERRQUEUE
422 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100423int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct ipv6_pinfo *np = inet6_sk(sk);
426 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400427 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100428 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct {
430 struct sock_extended_err ee;
431 struct sockaddr_in6 offender;
432 } errhdr;
433 int err;
434 int copied;
435
436 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400437 skb = sock_dequeue_err_skb(sk);
Ian Morris63159f22015-03-29 14:00:04 +0100438 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto out;
440
441 copied = skb->len;
442 if (copied > len) {
443 msg->msg_flags |= MSG_TRUNC;
444 copied = len;
445 }
David S. Miller51f3d022014-11-05 16:46:40 -0500446 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700447 if (unlikely(err)) {
448 kfree_skb(skb);
449 return err;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 sock_recv_timestamp(msg, sk, skb);
452
453 serr = SKB_EXT_ERR(skb);
454
Julian Anastasov34b99df2015-06-23 08:34:39 +0300455 if (sin && ipv6_datagram_support_addr(serr)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700456 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 sin->sin6_family = AF_INET6;
458 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900459 sin->sin6_port = serr->port;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000460 if (skb->protocol == htons(ETH_P_IPV6)) {
YOSHIFUJI Hideaki / 吉藤英明6c40d102013-01-08 06:44:23 +0000461 const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
462 struct ipv6hdr, daddr);
463 sin->sin6_addr = ip6h->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (np->sndflow)
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000465 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000466 sin->sin6_scope_id =
467 ipv6_iface_scope_id(&sin->sin6_addr,
468 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700470 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
471 &sin->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000472 sin->sin6_scope_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100474 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
477 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
478 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500479 memset(sin, 0, sizeof(*sin));
Willem de Bruijnc247f052015-03-07 20:33:22 -0500480
481 if (ip6_datagram_support_cmsg(skb, serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 sin->sin6_family = AF_INET6;
Willem de Bruijnc247f052015-03-07 20:33:22 -0500483 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100484 ip6_datagram_recv_common_ctl(sk, msg, skb);
Brian Haleyd40a4de2010-05-03 15:44:27 +0000485 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000486 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100488 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000489 sin->sin6_scope_id =
490 ipv6_iface_scope_id(&sin->sin6_addr,
491 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700493 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
494 &sin->sin6_addr);
Willem de Bruijnf8121162015-01-15 13:18:40 -0500495 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 ip_cmsg_recv(msg, skb);
497 }
498 }
499
500 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
501
502 /* Now we could try to dump offended packet options */
503
504 msg->msg_flags |= MSG_ERRQUEUE;
505 err = copied;
506
Eric Dumazet960a2622016-04-21 22:27:32 -0700507 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508out:
509 return err;
510}
Chris Elstona495f832012-04-29 21:48:53 +0000511EXPORT_SYMBOL_GPL(ipv6_recv_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Brian Haley4b340ae2010-04-23 11:26:09 +0000513/*
514 * Handle IPV6_RECVPATHMTU
515 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100516int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
517 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000518{
519 struct ipv6_pinfo *np = inet6_sk(sk);
520 struct sk_buff *skb;
Brian Haley4b340ae2010-04-23 11:26:09 +0000521 struct ip6_mtuinfo mtu_info;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100522 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
Brian Haley4b340ae2010-04-23 11:26:09 +0000523 int err;
524 int copied;
525
526 err = -EAGAIN;
527 skb = xchg(&np->rxpmtu, NULL);
Ian Morris63159f22015-03-29 14:00:04 +0100528 if (!skb)
Brian Haley4b340ae2010-04-23 11:26:09 +0000529 goto out;
530
531 copied = skb->len;
532 if (copied > len) {
533 msg->msg_flags |= MSG_TRUNC;
534 copied = len;
535 }
David S. Miller51f3d022014-11-05 16:46:40 -0500536 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Brian Haley4b340ae2010-04-23 11:26:09 +0000537 if (err)
538 goto out_free_skb;
539
540 sock_recv_timestamp(msg, sk, skb);
541
542 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
543
Brian Haley4b340ae2010-04-23 11:26:09 +0000544 if (sin) {
545 sin->sin6_family = AF_INET6;
546 sin->sin6_flowinfo = 0;
547 sin->sin6_port = 0;
548 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000549 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100550 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000551 }
552
553 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
554
555 err = copied;
556
557out_free_skb:
558 kfree_skb(skb);
559out:
560 return err;
561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100564void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
565 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct ipv6_pinfo *np = inet6_sk(sk);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100568 bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (np->rxopt.bits.rxinfo) {
571 struct in6_pktinfo src_info;
572
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100573 if (is_ipv6) {
574 src_info.ipi6_ifindex = IP6CB(skb)->iif;
575 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
576 } else {
577 src_info.ipi6_ifindex =
578 PKTINFO_SKB_CB(skb)->ipi_ifindex;
579 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
580 &src_info.ipi6_addr);
581 }
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500582
583 if (src_info.ipi6_ifindex >= 0)
584 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
585 sizeof(src_info), &src_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100587}
588
589void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
590 struct sk_buff *skb)
591{
592 struct ipv6_pinfo *np = inet6_sk(sk);
593 struct inet6_skb_parm *opt = IP6CB(skb);
594 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700597 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
599 }
600
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900601 if (np->rxopt.bits.rxtclass) {
YOSHIFUJI Hideaki / 吉藤英明e7219852013-01-13 05:02:01 +0000602 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900603 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
604 }
605
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000606 if (np->rxopt.bits.rxflow) {
607 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
608 if (flowinfo)
609 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900611
612 /* HbH is allowed only once */
Florian Westphal8b58a392015-07-08 23:32:12 +0200613 if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
614 u8 *ptr = nh + sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
616 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900617
618 if (opt->lastopt &&
619 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
620 /*
621 * Silly enough, but we need to reparse in order to
622 * report extension headers (except for HbH)
623 * in order.
624 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900625 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900626 * (and WILL NOT be) defined because
627 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
628 */
629 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700630 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900631
632 while (off <= opt->lastopt) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000633 unsigned int len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700634 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900635
Eldad Zackb5a42572012-04-01 07:49:03 +0000636 switch (nexthdr) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900637 case IPPROTO_DSTOPTS:
638 nexthdr = ptr[0];
639 len = (ptr[1] + 1) << 3;
640 if (np->rxopt.bits.dstopts)
641 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
642 break;
643 case IPPROTO_ROUTING:
644 nexthdr = ptr[0];
645 len = (ptr[1] + 1) << 3;
646 if (np->rxopt.bits.srcrt)
647 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
648 break;
649 case IPPROTO_AH:
650 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900651 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900652 break;
653 default:
654 nexthdr = ptr[0];
655 len = (ptr[1] + 1) << 3;
656 break;
657 }
658
659 off += len;
660 }
661 }
662
663 /* socket options in old style */
664 if (np->rxopt.bits.rxoinfo) {
665 struct in6_pktinfo src_info;
666
667 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000668 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900669 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
670 }
671 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700672 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900673 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
674 }
Florian Westphal8b58a392015-07-08 23:32:12 +0200675 if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
676 u8 *ptr = nh + sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900677 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
678 }
679 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700680 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900681 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900683 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700684 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900685 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900687 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700688 u8 *ptr = nh + opt->dst1;
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 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200691 if (np->rxopt.bits.rxorigdstaddr) {
692 struct sockaddr_in6 sin6;
Eric Dumazet747465e2012-01-16 19:27:39 +0000693 __be16 *ports = (__be16 *) skb_transport_header(skb);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200694
Willem de Bruijnd36a1cb2016-12-22 18:19:16 -0500695 if (skb_transport_offset(skb) + 4 <= (int)skb->len) {
Balazs Scheidler6c468622010-10-21 16:08:28 +0200696 /* All current transport protocols have the port numbers in the
697 * first four bytes of the transport header and this function is
698 * written with this assumption in mind.
699 */
700
701 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000702 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200703 sin6.sin6_port = ports[1];
704 sin6.sin6_flowinfo = 0;
Hannes Frederic Sowa3868b7a2013-03-08 02:07:26 +0000705 sin6.sin6_scope_id =
706 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
707 opt->iif);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200708
709 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
710 }
711 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100712}
713
714void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
715 struct sk_buff *skb)
716{
717 ip6_datagram_recv_common_ctl(sk, msg, skb);
718 ip6_datagram_recv_specific_ctl(sk, msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
Tom Parkin8e72d372013-01-31 01:02:25 +0000720EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Tom Parkin73df66f2013-01-31 01:02:24 +0000722int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
723 struct msghdr *msg, struct flowi6 *fl6,
Wei Wang26879da2016-05-02 21:40:07 -0700724 struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct in6_pktinfo *src_info;
727 struct cmsghdr *cmsg;
728 struct ipv6_rt_hdr *rthdr;
729 struct ipv6_opt_hdr *hdr;
Wei Wang26879da2016-05-02 21:40:07 -0700730 struct ipv6_txoptions *opt = ipc6->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int len;
732 int err = 0;
733
Gu Zhengf95b4142014-12-11 11:22:04 +0800734 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (!CMSG_OK(msg, cmsg)) {
738 err = -EINVAL;
739 goto exit_f;
740 }
741
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400742 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700743 err = __sock_cmsg_send(sk, msg, cmsg, sockc);
744 if (err)
745 return err;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400746 continue;
747 }
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (cmsg->cmsg_level != SOL_IPV6)
750 continue;
751
752 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900753 case IPV6_PKTINFO:
754 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900755 {
756 struct net_device *dev = NULL;
757
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900758 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 err = -EINVAL;
760 goto exit_f;
761 }
762
763 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500766 if (fl6->flowi6_oif &&
767 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500769 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900772 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Eric Dumazet536b2e92009-11-02 12:21:06 +0100774 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500775 if (fl6->flowi6_oif) {
776 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100777 if (!dev) {
778 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900779 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100780 }
781 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
782 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900783 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100784 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900785
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900786 if (addr_type != IPV6_ADDR_ANY) {
787 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000788 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000789 !ipv6_chk_addr(net, &src_info->ipi6_addr,
FX Le Bail7c90cc22014-01-22 07:42:37 +0100790 strict ? dev : NULL, 0) &&
791 !ipv6_chk_acast_addr_src(net, dev,
792 &src_info->ipi6_addr))
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900793 err = -EINVAL;
794 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000795 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900797
Eric Dumazet536b2e92009-11-02 12:21:06 +0100798 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900800 if (err)
801 goto exit_f;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900807 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 err = -EINVAL;
809 goto exit_f;
810 }
811
David S. Miller4c9483b2011-03-12 16:22:43 -0500812 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
813 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
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 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 break;
820
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900821 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900823 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 err = -EINVAL;
825 goto exit_f;
826 }
827
828 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
829 len = ((hdr->hdrlen + 1) << 3);
830 if (cmsg->cmsg_len < CMSG_LEN(len)) {
831 err = -EINVAL;
832 goto exit_f;
833 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000834 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 err = -EPERM;
836 goto exit_f;
837 }
838 opt->opt_nflen += len;
839 opt->hopopt = hdr;
840 break;
841
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900842 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900843 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 err = -EINVAL;
845 goto exit_f;
846 }
847
848 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
849 len = ((hdr->hdrlen + 1) << 3);
850 if (cmsg->cmsg_len < CMSG_LEN(len)) {
851 err = -EINVAL;
852 goto exit_f;
853 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000854 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 err = -EPERM;
856 goto exit_f;
857 }
858 if (opt->dst1opt) {
859 err = -EINVAL;
860 goto exit_f;
861 }
862 opt->opt_flen += len;
863 opt->dst1opt = hdr;
864 break;
865
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900866 case IPV6_DSTOPTS:
867 case IPV6_RTHDRDSTOPTS:
868 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
869 err = -EINVAL;
870 goto exit_f;
871 }
872
873 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
874 len = ((hdr->hdrlen + 1) << 3);
875 if (cmsg->cmsg_len < CMSG_LEN(len)) {
876 err = -EINVAL;
877 goto exit_f;
878 }
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000879 if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900880 err = -EPERM;
881 goto exit_f;
882 }
883 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
884 opt->opt_flen += len;
885 opt->dst1opt = hdr;
886 } else {
887 opt->opt_nflen += len;
888 opt->dst0opt = hdr;
889 }
890 break;
891
892 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900894 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 err = -EINVAL;
896 goto exit_f;
897 }
898
899 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
900
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700901 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000902#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700903 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800904 if (rthdr->hdrlen != 2 ||
905 rthdr->segments_left != 1) {
906 err = -EINVAL;
907 goto exit_f;
908 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700909 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700910#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700911 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 err = -EINVAL;
913 goto exit_f;
914 }
915
916 len = ((rthdr->hdrlen + 1) << 3);
917
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900918 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 err = -EINVAL;
920 goto exit_f;
921 }
922
923 /* segments left must also match */
924 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
925 err = -EINVAL;
926 goto exit_f;
927 }
928
929 opt->opt_nflen += len;
930 opt->srcrt = rthdr;
931
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900932 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
934
935 opt->opt_nflen += dsthdrlen;
936 opt->dst0opt = opt->dst1opt;
937 opt->dst1opt = NULL;
938 opt->opt_flen -= dsthdrlen;
939 }
940
941 break;
942
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900943 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 case IPV6_HOPLIMIT:
945 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
946 err = -EINVAL;
947 goto exit_f;
948 }
949
Wei Wang26879da2016-05-02 21:40:07 -0700950 ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
951 if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
Shan Weie8766fc2008-06-10 15:50:55 +0800952 err = -EINVAL;
953 goto exit_f;
954 }
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900958 case IPV6_TCLASS:
959 {
960 int tc;
961
962 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000963 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900964 goto exit_f;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900965
966 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700967 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900968 goto exit_f;
969
970 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700971 ipc6->tclass = tc;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900972
973 break;
974 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000975
976 case IPV6_DONTFRAG:
977 {
978 int df;
979
980 err = -EINVAL;
Eldad Zackb5a42572012-04-01 07:49:03 +0000981 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
Brian Haley13b52cd2010-04-23 11:26:08 +0000982 goto exit_f;
Brian Haley13b52cd2010-04-23 11:26:08 +0000983
984 df = *(int *)CMSG_DATA(cmsg);
985 if (df < 0 || df > 1)
986 goto exit_f;
987
988 err = 0;
Wei Wang26879da2016-05-02 21:40:07 -0700989 ipc6->dontfrag = df;
Brian Haley13b52cd2010-04-23 11:26:08 +0000990
991 break;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 default:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800994 net_dbg_ratelimited("invalid cmsg type: %d\n",
995 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -0700997 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
1001exit_f:
1002 return err;
1003}
Tom Parkin73df66f2013-01-31 01:02:24 +00001004EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001005
1006void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1007 __u16 srcp, __u16 destp, int bucket)
1008{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001009 const struct in6_addr *dest, *src;
1010
Eric Dumazetefe42082013-10-03 15:42:29 -07001011 dest = &sp->sk_v6_daddr;
1012 src = &sp->sk_v6_rcv_saddr;
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001013 seq_printf(seq,
1014 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001015 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001016 bucket,
1017 src->s6_addr32[0], src->s6_addr32[1],
1018 src->s6_addr32[2], src->s6_addr32[3], srcp,
1019 dest->s6_addr32[0], dest->s6_addr32[1],
1020 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1021 sp->sk_state,
1022 sk_wmem_alloc_get(sp),
1023 sk_rmem_alloc_get(sp),
1024 0, 0L, 0,
1025 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1026 0,
1027 sock_i_ino(sp),
1028 atomic_read(&sp->sk_refcnt), sp,
1029 atomic_read(&sp->sk_drops));
1030}