Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * common UDP/RAW code |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 7 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/errno.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/socket.h> |
| 16 | #include <linux/sockios.h> |
| 17 | #include <linux/in6.h> |
| 18 | #include <linux/ipv6.h> |
| 19 | #include <linux/route.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Chris Elston | a495f83 | 2012-04-29 21:48:53 +0000 | [diff] [blame] | 21 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <net/ipv6.h> |
| 24 | #include <net/ndisc.h> |
| 25 | #include <net/addrconf.h> |
| 26 | #include <net/transp_v6.h> |
| 27 | #include <net/ip6_route.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 28 | #include <net/tcp_states.h> |
YOSHIFUJI Hideaki / 吉藤英明 | e721985 | 2013-01-13 05:02:01 +0000 | [diff] [blame] | 29 | #include <net/dsfield.h> |
Willem de Bruijn | acdcecc | 2019-09-12 21:16:39 -0400 | [diff] [blame] | 30 | #include <net/sock_reuseport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <linux/errqueue.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 33 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 35 | static bool ipv6_mapped_addr_any(const struct in6_addr *a) |
Max Matveev | c15fea2 | 2011-08-05 03:56:30 -0700 | [diff] [blame] | 36 | { |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 37 | return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0); |
Max Matveev | c15fea2 | 2011-08-05 03:56:30 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Martin KaFai Lau | 80fbdb2 | 2016-04-11 15:29:34 -0700 | [diff] [blame] | 40 | static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk) |
| 41 | { |
| 42 | struct inet_sock *inet = inet_sk(sk); |
| 43 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 44 | |
| 45 | memset(fl6, 0, sizeof(*fl6)); |
| 46 | fl6->flowi6_proto = sk->sk_protocol; |
| 47 | fl6->daddr = sk->sk_v6_daddr; |
| 48 | fl6->saddr = np->saddr; |
| 49 | fl6->flowi6_oif = sk->sk_bound_dev_if; |
| 50 | fl6->flowi6_mark = sk->sk_mark; |
| 51 | fl6->fl6_dport = inet->inet_dport; |
| 52 | fl6->fl6_sport = inet->inet_sport; |
| 53 | fl6->flowlabel = np->flow_label; |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 54 | fl6->flowi6_uid = sk->sk_uid; |
Martin KaFai Lau | 80fbdb2 | 2016-04-11 15:29:34 -0700 | [diff] [blame] | 55 | |
| 56 | if (!fl6->flowi6_oif) |
| 57 | fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; |
| 58 | |
| 59 | if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) |
| 60 | fl6->flowi6_oif = np->mcast_oif; |
| 61 | |
| 62 | security_sk_classify_flow(sk, flowi6_to_flowi(fl6)); |
| 63 | } |
| 64 | |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 65 | int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr) |
Martin KaFai Lau | 7e2040d | 2016-04-11 15:29:35 -0700 | [diff] [blame] | 66 | { |
| 67 | struct ip6_flowlabel *flowlabel = NULL; |
| 68 | struct in6_addr *final_p, final; |
| 69 | struct ipv6_txoptions *opt; |
| 70 | struct dst_entry *dst; |
| 71 | struct inet_sock *inet = inet_sk(sk); |
| 72 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 73 | struct flowi6 fl6; |
| 74 | int err = 0; |
| 75 | |
| 76 | if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) { |
| 77 | flowlabel = fl6_sock_lookup(sk, np->flow_label); |
Eric Dumazet | 8975a3a | 2019-07-10 06:40:10 -0700 | [diff] [blame] | 78 | if (IS_ERR(flowlabel)) |
Martin KaFai Lau | 7e2040d | 2016-04-11 15:29:35 -0700 | [diff] [blame] | 79 | return -EINVAL; |
| 80 | } |
| 81 | ip6_datagram_flow_key_init(&fl6, sk); |
| 82 | |
| 83 | rcu_read_lock(); |
| 84 | opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt); |
| 85 | final_p = fl6_update_dst(&fl6, opt, &final); |
| 86 | rcu_read_unlock(); |
| 87 | |
Sabrina Dubroca | c4e85f7 | 2019-12-04 15:35:52 +0100 | [diff] [blame] | 88 | dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); |
Martin KaFai Lau | 7e2040d | 2016-04-11 15:29:35 -0700 | [diff] [blame] | 89 | if (IS_ERR(dst)) { |
| 90 | err = PTR_ERR(dst); |
| 91 | goto out; |
| 92 | } |
| 93 | |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 94 | if (fix_sk_saddr) { |
| 95 | if (ipv6_addr_any(&np->saddr)) |
| 96 | np->saddr = fl6.saddr; |
Martin KaFai Lau | 7e2040d | 2016-04-11 15:29:35 -0700 | [diff] [blame] | 97 | |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 98 | if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) { |
| 99 | sk->sk_v6_rcv_saddr = fl6.saddr; |
| 100 | inet->inet_rcv_saddr = LOOPBACK4_IPV6; |
| 101 | if (sk->sk_prot->rehash) |
| 102 | sk->sk_prot->rehash(sk); |
| 103 | } |
Martin KaFai Lau | 7e2040d | 2016-04-11 15:29:35 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Alexey Kodanev | 7d6850f | 2018-04-03 15:00:07 +0300 | [diff] [blame] | 106 | ip6_sk_dst_store_flow(sk, dst, &fl6); |
Martin KaFai Lau | 7e2040d | 2016-04-11 15:29:35 -0700 | [diff] [blame] | 107 | |
| 108 | out: |
| 109 | fl6_sock_release(flowlabel); |
| 110 | return err; |
| 111 | } |
| 112 | |
Martin KaFai Lau | e646b65 | 2016-04-11 15:29:37 -0700 | [diff] [blame] | 113 | void ip6_datagram_release_cb(struct sock *sk) |
| 114 | { |
| 115 | struct dst_entry *dst; |
| 116 | |
| 117 | if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) |
| 118 | return; |
| 119 | |
| 120 | rcu_read_lock(); |
| 121 | dst = __sk_dst_get(sk); |
| 122 | if (!dst || !dst->obsolete || |
| 123 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) { |
| 124 | rcu_read_unlock(); |
| 125 | return; |
| 126 | } |
| 127 | rcu_read_unlock(); |
| 128 | |
| 129 | ip6_datagram_dst_update(sk, false); |
| 130 | } |
| 131 | EXPORT_SYMBOL_GPL(ip6_datagram_release_cb); |
| 132 | |
Guillaume Nault | 0382a25 | 2016-11-29 13:09:44 +0100 | [diff] [blame] | 133 | int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, |
| 134 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 137 | struct inet_sock *inet = inet_sk(sk); |
| 138 | struct ipv6_pinfo *np = inet6_sk(sk); |
Paolo Abeni | 2f987a7 | 2018-03-12 14:54:23 +0100 | [diff] [blame] | 139 | struct in6_addr *daddr, old_daddr; |
| 140 | __be32 fl6_flowlabel = 0; |
| 141 | __be32 old_fl6_flowlabel; |
Stefano Brivio | 5f2fb80 | 2018-03-19 11:24:58 +0100 | [diff] [blame] | 142 | __be16 old_dport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | int addr_type; |
| 144 | int err; |
| 145 | |
| 146 | if (usin->sin6_family == AF_INET) { |
| 147 | if (__ipv6_only_sock(sk)) |
| 148 | return -EAFNOSUPPORT; |
Eric Dumazet | 03645a1 | 2015-07-14 08:10:22 +0200 | [diff] [blame] | 149 | err = __ip4_datagram_connect(sk, uaddr, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | goto ipv4_connected; |
| 151 | } |
| 152 | |
| 153 | if (addr_len < SIN6_LEN_RFC2133) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 154 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 156 | if (usin->sin6_family != AF_INET6) |
| 157 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Martin KaFai Lau | 7e2040d | 2016-04-11 15:29:35 -0700 | [diff] [blame] | 159 | if (np->sndflow) |
Martin KaFai Lau | 80fbdb2 | 2016-04-11 15:29:34 -0700 | [diff] [blame] | 160 | fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Jonathan T. Leighton | 052d236 | 2017-02-12 17:26:07 -0500 | [diff] [blame] | 162 | if (ipv6_addr_any(&usin->sin6_addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* |
| 164 | * connect to self |
| 165 | */ |
Jonathan T. Leighton | 052d236 | 2017-02-12 17:26:07 -0500 | [diff] [blame] | 166 | if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) |
| 167 | ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), |
| 168 | &usin->sin6_addr); |
| 169 | else |
| 170 | usin->sin6_addr = in6addr_loopback; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jonathan T. Leighton | 052d236 | 2017-02-12 17:26:07 -0500 | [diff] [blame] | 173 | addr_type = ipv6_addr_type(&usin->sin6_addr); |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | daddr = &usin->sin6_addr; |
| 176 | |
Jonathan T. Leighton | 052d236 | 2017-02-12 17:26:07 -0500 | [diff] [blame] | 177 | if (addr_type & IPV6_ADDR_MAPPED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | struct sockaddr_in sin; |
| 179 | |
| 180 | if (__ipv6_only_sock(sk)) { |
| 181 | err = -ENETUNREACH; |
| 182 | goto out; |
| 183 | } |
| 184 | sin.sin_family = AF_INET; |
| 185 | sin.sin_addr.s_addr = daddr->s6_addr32[3]; |
| 186 | sin.sin_port = usin->sin6_port; |
| 187 | |
Eric Dumazet | 03645a1 | 2015-07-14 08:10:22 +0200 | [diff] [blame] | 188 | err = __ip4_datagram_connect(sk, |
| 189 | (struct sockaddr *) &sin, |
| 190 | sizeof(sin)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | ipv4_connected: |
| 193 | if (err) |
| 194 | goto out; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 195 | |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 196 | ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Max Matveev | c15fea2 | 2011-08-05 03:56:30 -0700 | [diff] [blame] | 198 | if (ipv6_addr_any(&np->saddr) || |
| 199 | ipv6_mapped_addr_any(&np->saddr)) |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 200 | ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 202 | if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) || |
| 203 | ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 204 | ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 205 | &sk->sk_v6_rcv_saddr); |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 206 | if (sk->sk_prot->rehash) |
| 207 | sk->sk_prot->rehash(sk); |
| 208 | } |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | goto out; |
| 211 | } |
| 212 | |
Hannes Frederic Sowa | 842df07 | 2013-03-08 02:07:19 +0000 | [diff] [blame] | 213 | if (__ipv6_addr_needs_scope_id(addr_type)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (addr_len >= sizeof(struct sockaddr_in6) && |
| 215 | usin->sin6_scope_id) { |
David Ahern | 54dc3e3 | 2018-01-04 14:03:54 -0800 | [diff] [blame] | 216 | if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | err = -EINVAL; |
| 218 | goto out; |
| 219 | } |
| 220 | sk->sk_bound_dev_if = usin->sin6_scope_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Brian Haley | 1ac4f00 | 2008-01-08 23:52:21 -0800 | [diff] [blame] | 223 | if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST)) |
| 224 | sk->sk_bound_dev_if = np->mcast_oif; |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | /* Connect to link-local address requires an interface */ |
| 227 | if (!sk->sk_bound_dev_if) { |
| 228 | err = -EINVAL; |
| 229 | goto out; |
| 230 | } |
| 231 | } |
| 232 | |
Paolo Abeni | 2f987a7 | 2018-03-12 14:54:23 +0100 | [diff] [blame] | 233 | /* save the current peer information before updating it */ |
| 234 | old_daddr = sk->sk_v6_daddr; |
| 235 | old_fl6_flowlabel = np->flow_label; |
| 236 | old_dport = inet->inet_dport; |
| 237 | |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 238 | sk->sk_v6_daddr = *daddr; |
Martin KaFai Lau | 80fbdb2 | 2016-04-11 15:29:34 -0700 | [diff] [blame] | 239 | np->flow_label = fl6_flowlabel; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 240 | inet->inet_dport = usin->sin6_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * Check for a route to destination an obtain the |
| 244 | * destination cache for it. |
| 245 | */ |
| 246 | |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 247 | err = ip6_datagram_dst_update(sk, true); |
Wei Wang | 85cb73f | 2017-06-23 15:25:37 -0700 | [diff] [blame] | 248 | if (err) { |
Paolo Abeni | 2f987a7 | 2018-03-12 14:54:23 +0100 | [diff] [blame] | 249 | /* Restore the socket peer info, to keep it consistent with |
| 250 | * the old socket state |
Wei Wang | 85cb73f | 2017-06-23 15:25:37 -0700 | [diff] [blame] | 251 | */ |
Paolo Abeni | 2f987a7 | 2018-03-12 14:54:23 +0100 | [diff] [blame] | 252 | sk->sk_v6_daddr = old_daddr; |
| 253 | np->flow_label = old_fl6_flowlabel; |
| 254 | inet->inet_dport = old_dport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | goto out; |
Wei Wang | 85cb73f | 2017-06-23 15:25:37 -0700 | [diff] [blame] | 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Willem de Bruijn | acdcecc | 2019-09-12 21:16:39 -0400 | [diff] [blame] | 258 | reuseport_has_conns(sk, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | sk->sk_state = TCP_ESTABLISHED; |
Tom Herbert | 877d1f6 | 2015-07-28 16:02:05 -0700 | [diff] [blame] | 260 | sk_set_txhash(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return err; |
| 263 | } |
Guillaume Nault | 0382a25 | 2016-11-29 13:09:44 +0100 | [diff] [blame] | 264 | EXPORT_SYMBOL_GPL(__ip6_datagram_connect); |
Eric Dumazet | 03645a1 | 2015-07-14 08:10:22 +0200 | [diff] [blame] | 265 | |
| 266 | int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 267 | { |
| 268 | int res; |
| 269 | |
| 270 | lock_sock(sk); |
| 271 | res = __ip6_datagram_connect(sk, uaddr, addr_len); |
| 272 | release_sock(sk); |
| 273 | return res; |
| 274 | } |
Chris Elston | a495f83 | 2012-04-29 21:48:53 +0000 | [diff] [blame] | 275 | EXPORT_SYMBOL_GPL(ip6_datagram_connect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Hannes Frederic Sowa | 82b276c | 2014-01-20 05:16:39 +0100 | [diff] [blame] | 277 | int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr, |
| 278 | int addr_len) |
| 279 | { |
| 280 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr); |
| 281 | if (sin6->sin6_family != AF_INET6) |
| 282 | return -EAFNOSUPPORT; |
| 283 | return ip6_datagram_connect(sk, uaddr, addr_len); |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only); |
| 286 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 287 | void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 288 | __be16 port, u32 info, u8 *payload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | struct ipv6_pinfo *np = inet6_sk(sk); |
Arnaldo Carvalho de Melo | cc70ab2 | 2007-03-13 14:03:22 -0300 | [diff] [blame] | 291 | struct icmp6hdr *icmph = icmp6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | struct sock_exterr_skb *serr; |
| 293 | |
| 294 | if (!np->recverr) |
| 295 | return; |
| 296 | |
| 297 | skb = skb_clone(skb, GFP_ATOMIC); |
| 298 | if (!skb) |
| 299 | return; |
| 300 | |
Brian Haley | d40a4de | 2010-05-03 15:44:27 +0000 | [diff] [blame] | 301 | skb->protocol = htons(ETH_P_IPV6); |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | serr = SKB_EXT_ERR(skb); |
| 304 | serr->ee.ee_errno = err; |
| 305 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 306 | serr->ee.ee_type = icmph->icmp6_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | serr->ee.ee_code = icmph->icmp6_code; |
| 308 | serr->ee.ee_pad = 0; |
| 309 | serr->ee.ee_info = info; |
| 310 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 311 | serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) - |
| 312 | skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | serr->port = port; |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | __skb_pull(skb, payload - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 316 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | if (sock_queue_err_skb(sk, skb)) |
| 319 | kfree_skb(skb); |
| 320 | } |
| 321 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 322 | void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
Eric Dumazet | 1c1e9d2 | 2015-09-25 07:39:20 -0700 | [diff] [blame] | 324 | const struct ipv6_pinfo *np = inet6_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | struct sock_exterr_skb *serr; |
| 326 | struct ipv6hdr *iph; |
| 327 | struct sk_buff *skb; |
| 328 | |
| 329 | if (!np->recverr) |
| 330 | return; |
| 331 | |
| 332 | skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); |
| 333 | if (!skb) |
| 334 | return; |
| 335 | |
Brian Haley | d40a4de | 2010-05-03 15:44:27 +0000 | [diff] [blame] | 336 | skb->protocol = htons(ETH_P_IPV6); |
| 337 | |
Arnaldo Carvalho de Melo | 1ced98e | 2007-03-10 19:57:15 -0300 | [diff] [blame] | 338 | skb_put(skb, sizeof(struct ipv6hdr)); |
| 339 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 340 | iph = ipv6_hdr(skb); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 341 | iph->daddr = fl6->daddr; |
Eric Dumazet | 7d033c9 | 2019-01-08 04:06:14 -0800 | [diff] [blame] | 342 | ip6_flow_hdr(iph, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | serr = SKB_EXT_ERR(skb); |
| 345 | serr->ee.ee_errno = err; |
| 346 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 347 | serr->ee.ee_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | serr->ee.ee_code = 0; |
| 349 | serr->ee.ee_pad = 0; |
| 350 | serr->ee.ee_info = info; |
| 351 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 352 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
David S. Miller | 1958b85 | 2011-03-12 16:36:19 -0500 | [diff] [blame] | 353 | serr->port = fl6->fl6_dport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 355 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 356 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | if (sock_queue_err_skb(sk, skb)) |
| 359 | kfree_skb(skb); |
| 360 | } |
| 361 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 362 | void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu) |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 363 | { |
| 364 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 365 | struct ipv6hdr *iph; |
| 366 | struct sk_buff *skb; |
| 367 | struct ip6_mtuinfo *mtu_info; |
| 368 | |
| 369 | if (!np->rxopt.bits.rxpmtu) |
| 370 | return; |
| 371 | |
| 372 | skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); |
| 373 | if (!skb) |
| 374 | return; |
| 375 | |
| 376 | skb_put(skb, sizeof(struct ipv6hdr)); |
| 377 | skb_reset_network_header(skb); |
| 378 | iph = ipv6_hdr(skb); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 379 | iph->daddr = fl6->daddr; |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 380 | |
| 381 | mtu_info = IP6CBMTU(skb); |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 382 | |
| 383 | mtu_info->ip6m_mtu = mtu; |
| 384 | mtu_info->ip6m_addr.sin6_family = AF_INET6; |
| 385 | mtu_info->ip6m_addr.sin6_port = 0; |
| 386 | mtu_info->ip6m_addr.sin6_flowinfo = 0; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 387 | mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 388 | mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr; |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 389 | |
| 390 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
| 391 | skb_reset_transport_header(skb); |
| 392 | |
| 393 | skb = xchg(&np->rxpmtu, skb); |
| 394 | kfree_skb(skb); |
| 395 | } |
| 396 | |
Julian Anastasov | 34b99df | 2015-06-23 08:34:39 +0300 | [diff] [blame] | 397 | /* For some errors we have valid addr_offset even with zero payload and |
| 398 | * zero port. Also, addr_offset should be supported if port is set. |
| 399 | */ |
| 400 | static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr) |
| 401 | { |
| 402 | return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 || |
| 403 | serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
| 404 | serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port; |
| 405 | } |
| 406 | |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 407 | /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL. |
| 408 | * |
| 409 | * At one point, excluding local errors was a quick test to identify icmp/icmp6 |
| 410 | * errors. This is no longer true, but the test remained, so the v6 stack, |
| 411 | * unlike v4, also honors cmsg requests on all wifi and timestamp errors. |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 412 | */ |
| 413 | static bool ip6_datagram_support_cmsg(struct sk_buff *skb, |
| 414 | struct sock_exterr_skb *serr) |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 415 | { |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 416 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
| 417 | serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) |
| 418 | return true; |
| 419 | |
| 420 | if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL) |
| 421 | return false; |
| 422 | |
Willem de Bruijn | 1862d62 | 2017-04-12 19:24:35 -0400 | [diff] [blame] | 423 | if (!IP6CB(skb)->iif) |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 424 | return false; |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 425 | |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 426 | return true; |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 427 | } |
| 428 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 429 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | * Handle MSG_ERRQUEUE |
| 431 | */ |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 432 | int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
| 434 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 435 | struct sock_exterr_skb *serr; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 436 | struct sk_buff *skb; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 437 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | struct { |
| 439 | struct sock_extended_err ee; |
| 440 | struct sockaddr_in6 offender; |
| 441 | } errhdr; |
| 442 | int err; |
| 443 | int copied; |
| 444 | |
| 445 | err = -EAGAIN; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 446 | skb = sock_dequeue_err_skb(sk); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 447 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | goto out; |
| 449 | |
| 450 | copied = skb->len; |
| 451 | if (copied > len) { |
| 452 | msg->msg_flags |= MSG_TRUNC; |
| 453 | copied = len; |
| 454 | } |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 455 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Eric Dumazet | 960a262 | 2016-04-21 22:27:32 -0700 | [diff] [blame] | 456 | if (unlikely(err)) { |
| 457 | kfree_skb(skb); |
| 458 | return err; |
| 459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | sock_recv_timestamp(msg, sk, skb); |
| 461 | |
| 462 | serr = SKB_EXT_ERR(skb); |
| 463 | |
Julian Anastasov | 34b99df | 2015-06-23 08:34:39 +0300 | [diff] [blame] | 464 | if (sin && ipv6_datagram_support_addr(serr)) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 465 | const unsigned char *nh = skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | sin->sin6_family = AF_INET6; |
| 467 | sin->sin6_flowinfo = 0; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 468 | sin->sin6_port = serr->port; |
Brian Haley | d40a4de | 2010-05-03 15:44:27 +0000 | [diff] [blame] | 469 | if (skb->protocol == htons(ETH_P_IPV6)) { |
YOSHIFUJI Hideaki / 吉藤英明 | 6c40d10 | 2013-01-08 06:44:23 +0000 | [diff] [blame] | 470 | const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset), |
| 471 | struct ipv6hdr, daddr); |
| 472 | sin->sin6_addr = ip6h->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | if (np->sndflow) |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 474 | sin->sin6_flowinfo = ip6_flowinfo(ip6h); |
Hannes Frederic Sowa | 842df07 | 2013-03-08 02:07:19 +0000 | [diff] [blame] | 475 | sin->sin6_scope_id = |
| 476 | ipv6_iface_scope_id(&sin->sin6_addr, |
| 477 | IP6CB(skb)->iif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } else { |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 479 | ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset), |
| 480 | &sin->sin6_addr); |
Hannes Frederic Sowa | 842df07 | 2013-03-08 02:07:19 +0000 | [diff] [blame] | 481 | sin->sin6_scope_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 483 | *addr_len = sizeof(*sin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); |
| 487 | sin = &errhdr.offender; |
Willem de Bruijn | f812116 | 2015-01-15 13:18:40 -0500 | [diff] [blame] | 488 | memset(sin, 0, sizeof(*sin)); |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 489 | |
| 490 | if (ip6_datagram_support_cmsg(skb, serr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | sin->sin6_family = AF_INET6; |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 492 | if (np->rxopt.all) |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 493 | ip6_datagram_recv_common_ctl(sk, msg, skb); |
Brian Haley | d40a4de | 2010-05-03 15:44:27 +0000 | [diff] [blame] | 494 | if (skb->protocol == htons(ETH_P_IPV6)) { |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 495 | sin->sin6_addr = ipv6_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (np->rxopt.all) |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 497 | ip6_datagram_recv_specific_ctl(sk, msg, skb); |
Hannes Frederic Sowa | 842df07 | 2013-03-08 02:07:19 +0000 | [diff] [blame] | 498 | sin->sin6_scope_id = |
| 499 | ipv6_iface_scope_id(&sin->sin6_addr, |
| 500 | IP6CB(skb)->iif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } else { |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 502 | ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, |
| 503 | &sin->sin6_addr); |
Willem de Bruijn | f812116 | 2015-01-15 13:18:40 -0500 | [diff] [blame] | 504 | if (inet_sk(sk)->cmsg_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | ip_cmsg_recv(msg, skb); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr); |
| 510 | |
| 511 | /* Now we could try to dump offended packet options */ |
| 512 | |
| 513 | msg->msg_flags |= MSG_ERRQUEUE; |
| 514 | err = copied; |
| 515 | |
Eric Dumazet | 960a262 | 2016-04-21 22:27:32 -0700 | [diff] [blame] | 516 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | out: |
| 518 | return err; |
| 519 | } |
Chris Elston | a495f83 | 2012-04-29 21:48:53 +0000 | [diff] [blame] | 520 | EXPORT_SYMBOL_GPL(ipv6_recv_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 522 | /* |
| 523 | * Handle IPV6_RECVPATHMTU |
| 524 | */ |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 525 | int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len, |
| 526 | int *addr_len) |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 527 | { |
| 528 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 529 | struct sk_buff *skb; |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 530 | struct ip6_mtuinfo mtu_info; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 531 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name); |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 532 | int err; |
| 533 | int copied; |
| 534 | |
| 535 | err = -EAGAIN; |
| 536 | skb = xchg(&np->rxpmtu, NULL); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 537 | if (!skb) |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 538 | goto out; |
| 539 | |
| 540 | copied = skb->len; |
| 541 | if (copied > len) { |
| 542 | msg->msg_flags |= MSG_TRUNC; |
| 543 | copied = len; |
| 544 | } |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 545 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 546 | if (err) |
| 547 | goto out_free_skb; |
| 548 | |
| 549 | sock_recv_timestamp(msg, sk, skb); |
| 550 | |
| 551 | memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info)); |
| 552 | |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 553 | if (sin) { |
| 554 | sin->sin6_family = AF_INET6; |
| 555 | sin->sin6_flowinfo = 0; |
| 556 | sin->sin6_port = 0; |
| 557 | sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 558 | sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr; |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 559 | *addr_len = sizeof(*sin); |
Brian Haley | 4b340ae | 2010-04-23 11:26:09 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info); |
| 563 | |
| 564 | err = copied; |
| 565 | |
| 566 | out_free_skb: |
| 567 | kfree_skb(skb); |
| 568 | out: |
| 569 | return err; |
| 570 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
| 572 | |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 573 | void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg, |
| 574 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
| 576 | struct ipv6_pinfo *np = inet6_sk(sk); |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 577 | bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | if (np->rxopt.bits.rxinfo) { |
| 580 | struct in6_pktinfo src_info; |
| 581 | |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 582 | if (is_ipv6) { |
| 583 | src_info.ipi6_ifindex = IP6CB(skb)->iif; |
| 584 | src_info.ipi6_addr = ipv6_hdr(skb)->daddr; |
| 585 | } else { |
| 586 | src_info.ipi6_ifindex = |
| 587 | PKTINFO_SKB_CB(skb)->ipi_ifindex; |
| 588 | ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr, |
| 589 | &src_info.ipi6_addr); |
| 590 | } |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 591 | |
| 592 | if (src_info.ipi6_ifindex >= 0) |
| 593 | put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, |
| 594 | sizeof(src_info), &src_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg, |
| 599 | struct sk_buff *skb) |
| 600 | { |
| 601 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 602 | struct inet6_skb_parm *opt = IP6CB(skb); |
| 603 | unsigned char *nh = skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | if (np->rxopt.bits.rxhlim) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 606 | int hlim = ipv6_hdr(skb)->hop_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
| 608 | } |
| 609 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 610 | if (np->rxopt.bits.rxtclass) { |
YOSHIFUJI Hideaki / 吉藤英明 | e721985 | 2013-01-13 05:02:01 +0000 | [diff] [blame] | 611 | int tclass = ipv6_get_dsfield(ipv6_hdr(skb)); |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 612 | put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
| 613 | } |
| 614 | |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 615 | if (np->rxopt.bits.rxflow) { |
| 616 | __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh); |
| 617 | if (flowinfo) |
| 618 | put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 620 | |
| 621 | /* HbH is allowed only once */ |
Florian Westphal | 8b58a39 | 2015-07-08 23:32:12 +0200 | [diff] [blame] | 622 | if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) { |
| 623 | u8 *ptr = nh + sizeof(struct ipv6hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr); |
| 625 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 626 | |
| 627 | if (opt->lastopt && |
| 628 | (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) { |
| 629 | /* |
| 630 | * Silly enough, but we need to reparse in order to |
| 631 | * report extension headers (except for HbH) |
| 632 | * in order. |
| 633 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 634 | * Also note that IPV6_RECVRTHDRDSTOPTS is NOT |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 635 | * (and WILL NOT be) defined because |
| 636 | * IPV6_RECVDSTOPTS is more generic. --yoshfuji |
| 637 | */ |
| 638 | unsigned int off = sizeof(struct ipv6hdr); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 639 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 640 | |
| 641 | while (off <= opt->lastopt) { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 642 | unsigned int len; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 643 | u8 *ptr = nh + off; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 644 | |
Eldad Zack | b5a4257 | 2012-04-01 07:49:03 +0000 | [diff] [blame] | 645 | switch (nexthdr) { |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 646 | case IPPROTO_DSTOPTS: |
| 647 | nexthdr = ptr[0]; |
| 648 | len = (ptr[1] + 1) << 3; |
| 649 | if (np->rxopt.bits.dstopts) |
| 650 | put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr); |
| 651 | break; |
| 652 | case IPPROTO_ROUTING: |
| 653 | nexthdr = ptr[0]; |
| 654 | len = (ptr[1] + 1) << 3; |
| 655 | if (np->rxopt.bits.srcrt) |
| 656 | put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr); |
| 657 | break; |
| 658 | case IPPROTO_AH: |
| 659 | nexthdr = ptr[0]; |
Ville Nuorvala | a305989 | 2005-11-20 12:21:59 +0900 | [diff] [blame] | 660 | len = (ptr[1] + 2) << 2; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 661 | break; |
| 662 | default: |
| 663 | nexthdr = ptr[0]; |
| 664 | len = (ptr[1] + 1) << 3; |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | off += len; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* socket options in old style */ |
| 673 | if (np->rxopt.bits.rxoinfo) { |
| 674 | struct in6_pktinfo src_info; |
| 675 | |
| 676 | src_info.ipi6_ifindex = opt->iif; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 677 | src_info.ipi6_addr = ipv6_hdr(skb)->daddr; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 678 | put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
| 679 | } |
| 680 | if (np->rxopt.bits.rxohlim) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 681 | int hlim = ipv6_hdr(skb)->hop_limit; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 682 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
| 683 | } |
Florian Westphal | 8b58a39 | 2015-07-08 23:32:12 +0200 | [diff] [blame] | 684 | if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) { |
| 685 | u8 *ptr = nh + sizeof(struct ipv6hdr); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 686 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr); |
| 687 | } |
| 688 | if (np->rxopt.bits.odstopts && opt->dst0) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 689 | u8 *ptr = nh + opt->dst0; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 690 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 692 | if (np->rxopt.bits.osrcrt && opt->srcrt) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 693 | struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 694 | put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 696 | if (np->rxopt.bits.odstopts && opt->dst1) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 697 | u8 *ptr = nh + opt->dst1; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 698 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 700 | if (np->rxopt.bits.rxorigdstaddr) { |
| 701 | struct sockaddr_in6 sin6; |
Willem de Bruijn | 4a06fa6 | 2019-01-07 16:47:33 -0500 | [diff] [blame] | 702 | __be16 _ports[2], *ports; |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 703 | |
Willem de Bruijn | 4a06fa6 | 2019-01-07 16:47:33 -0500 | [diff] [blame] | 704 | ports = skb_header_pointer(skb, skb_transport_offset(skb), |
| 705 | sizeof(_ports), &_ports); |
| 706 | if (ports) { |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 707 | /* All current transport protocols have the port numbers in the |
| 708 | * first four bytes of the transport header and this function is |
| 709 | * written with this assumption in mind. |
| 710 | */ |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 711 | sin6.sin6_family = AF_INET6; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 712 | sin6.sin6_addr = ipv6_hdr(skb)->daddr; |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 713 | sin6.sin6_port = ports[1]; |
| 714 | sin6.sin6_flowinfo = 0; |
Hannes Frederic Sowa | 3868b7a | 2013-03-08 02:07:26 +0000 | [diff] [blame] | 715 | sin6.sin6_scope_id = |
| 716 | ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr, |
| 717 | opt->iif); |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 718 | |
| 719 | put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6); |
| 720 | } |
| 721 | } |
Willem de Bruijn | 0cc0aa61 | 2016-11-02 11:02:17 -0400 | [diff] [blame] | 722 | if (np->rxopt.bits.recvfragsize && opt->frag_max_size) { |
| 723 | int val = opt->frag_max_size; |
| 724 | |
| 725 | put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val); |
| 726 | } |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg, |
| 730 | struct sk_buff *skb) |
| 731 | { |
| 732 | ip6_datagram_recv_common_ctl(sk, msg, skb); |
| 733 | ip6_datagram_recv_specific_ctl(sk, msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
Tom Parkin | 8e72d37 | 2013-01-31 01:02:25 +0000 | [diff] [blame] | 735 | EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Tom Parkin | 73df66f | 2013-01-31 01:02:24 +0000 | [diff] [blame] | 737 | int ip6_datagram_send_ctl(struct net *net, struct sock *sk, |
| 738 | struct msghdr *msg, struct flowi6 *fl6, |
Willem de Bruijn | 5fdaa88 | 2018-07-06 10:12:57 -0400 | [diff] [blame] | 739 | struct ipcm6_cookie *ipc6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | { |
| 741 | struct in6_pktinfo *src_info; |
| 742 | struct cmsghdr *cmsg; |
| 743 | struct ipv6_rt_hdr *rthdr; |
| 744 | struct ipv6_opt_hdr *hdr; |
Wei Wang | 26879da | 2016-05-02 21:40:07 -0700 | [diff] [blame] | 745 | struct ipv6_txoptions *opt = ipc6->opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | int len; |
| 747 | int err = 0; |
| 748 | |
Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 749 | for_each_cmsghdr(cmsg, msg) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | int addr_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | if (!CMSG_OK(msg, cmsg)) { |
| 753 | err = -EINVAL; |
| 754 | goto exit_f; |
| 755 | } |
| 756 | |
Soheil Hassas Yeganeh | ad1e46a | 2016-04-02 23:08:11 -0400 | [diff] [blame] | 757 | if (cmsg->cmsg_level == SOL_SOCKET) { |
Willem de Bruijn | 5fdaa88 | 2018-07-06 10:12:57 -0400 | [diff] [blame] | 758 | err = __sock_cmsg_send(sk, msg, cmsg, &ipc6->sockc); |
Eric Dumazet | 2632616 | 2016-05-13 06:14:37 -0700 | [diff] [blame] | 759 | if (err) |
| 760 | return err; |
Soheil Hassas Yeganeh | ad1e46a | 2016-04-02 23:08:11 -0400 | [diff] [blame] | 761 | continue; |
| 762 | } |
| 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | if (cmsg->cmsg_level != SOL_IPV6) |
| 765 | continue; |
| 766 | |
| 767 | switch (cmsg->cmsg_type) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 768 | case IPV6_PKTINFO: |
| 769 | case IPV6_2292PKTINFO: |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 770 | { |
| 771 | struct net_device *dev = NULL; |
Mike Manning | 6da5b0f | 2018-11-07 15:36:04 +0000 | [diff] [blame] | 772 | int src_idx; |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 773 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 774 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | err = -EINVAL; |
| 776 | goto exit_f; |
| 777 | } |
| 778 | |
| 779 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); |
Mike Manning | 6da5b0f | 2018-11-07 15:36:04 +0000 | [diff] [blame] | 780 | src_idx = src_info->ipi6_ifindex; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 781 | |
Mike Manning | 6da5b0f | 2018-11-07 15:36:04 +0000 | [diff] [blame] | 782 | if (src_idx) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 783 | if (fl6->flowi6_oif && |
Mike Manning | 6da5b0f | 2018-11-07 15:36:04 +0000 | [diff] [blame] | 784 | src_idx != fl6->flowi6_oif && |
| 785 | (sk->sk_bound_dev_if != fl6->flowi6_oif || |
| 786 | !sk_dev_equal_l3scope(sk, src_idx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return -EINVAL; |
Mike Manning | 6da5b0f | 2018-11-07 15:36:04 +0000 | [diff] [blame] | 788 | fl6->flowi6_oif = src_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 791 | addr_type = __ipv6_addr_type(&src_info->ipi6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 793 | rcu_read_lock(); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 794 | if (fl6->flowi6_oif) { |
| 795 | dev = dev_get_by_index_rcu(net, fl6->flowi6_oif); |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 796 | if (!dev) { |
| 797 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 798 | return -ENODEV; |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 799 | } |
| 800 | } else if (addr_type & IPV6_ADDR_LINKLOCAL) { |
| 801 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 802 | return -EINVAL; |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 803 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 804 | |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 805 | if (addr_type != IPV6_ADDR_ANY) { |
| 806 | int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL; |
Vincent Bernat | 83ba464 | 2018-07-31 21:18:11 +0200 | [diff] [blame] | 807 | if (!ipv6_can_nonlocal_bind(net, inet_sk(sk)) && |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 808 | !ipv6_chk_addr_and_flags(net, &src_info->ipi6_addr, |
| 809 | dev, !strict, 0, |
| 810 | IFA_F_TENTATIVE) && |
FX Le Bail | 7c90cc2 | 2014-01-22 07:42:37 +0100 | [diff] [blame] | 811 | !ipv6_chk_acast_addr_src(net, dev, |
| 812 | &src_info->ipi6_addr)) |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 813 | err = -EINVAL; |
| 814 | else |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 815 | fl6->saddr = src_info->ipi6_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 817 | |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 818 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 820 | if (err) |
| 821 | goto exit_f; |
| 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | break; |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
| 826 | case IPV6_FLOWINFO: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 827 | if (cmsg->cmsg_len < CMSG_LEN(4)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | err = -EINVAL; |
| 829 | goto exit_f; |
| 830 | } |
| 831 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 832 | if (fl6->flowlabel&IPV6_FLOWINFO_MASK) { |
| 833 | if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | err = -EINVAL; |
| 835 | goto exit_f; |
| 836 | } |
| 837 | } |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 838 | fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | break; |
| 840 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 841 | case IPV6_2292HOPOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | case IPV6_HOPOPTS: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 843 | if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | 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. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 854 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | err = -EPERM; |
| 856 | goto exit_f; |
| 857 | } |
| 858 | opt->opt_nflen += len; |
| 859 | opt->hopopt = hdr; |
| 860 | break; |
| 861 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 862 | case IPV6_2292DSTOPTS: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 863 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | err = -EINVAL; |
| 865 | goto exit_f; |
| 866 | } |
| 867 | |
| 868 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); |
| 869 | len = ((hdr->hdrlen + 1) << 3); |
| 870 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
| 871 | err = -EINVAL; |
| 872 | goto exit_f; |
| 873 | } |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 874 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | err = -EPERM; |
| 876 | goto exit_f; |
| 877 | } |
| 878 | if (opt->dst1opt) { |
| 879 | err = -EINVAL; |
| 880 | goto exit_f; |
| 881 | } |
| 882 | opt->opt_flen += len; |
| 883 | opt->dst1opt = hdr; |
| 884 | break; |
| 885 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 886 | case IPV6_DSTOPTS: |
| 887 | case IPV6_RTHDRDSTOPTS: |
| 888 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
| 889 | err = -EINVAL; |
| 890 | goto exit_f; |
| 891 | } |
| 892 | |
| 893 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); |
| 894 | len = ((hdr->hdrlen + 1) << 3); |
| 895 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
| 896 | err = -EINVAL; |
| 897 | goto exit_f; |
| 898 | } |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 899 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) { |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 900 | err = -EPERM; |
| 901 | goto exit_f; |
| 902 | } |
| 903 | if (cmsg->cmsg_type == IPV6_DSTOPTS) { |
| 904 | opt->opt_flen += len; |
| 905 | opt->dst1opt = hdr; |
| 906 | } else { |
| 907 | opt->opt_nflen += len; |
| 908 | opt->dst0opt = hdr; |
| 909 | } |
| 910 | break; |
| 911 | |
| 912 | case IPV6_2292RTHDR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | case IPV6_RTHDR: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 914 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | err = -EINVAL; |
| 916 | goto exit_f; |
| 917 | } |
| 918 | |
| 919 | rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg); |
| 920 | |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 921 | switch (rthdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 922 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 923 | case IPV6_SRCRT_TYPE_2: |
Brian Haley | 6e093d9 | 2008-11-12 22:59:21 -0800 | [diff] [blame] | 924 | if (rthdr->hdrlen != 2 || |
| 925 | rthdr->segments_left != 1) { |
| 926 | err = -EINVAL; |
| 927 | goto exit_f; |
| 928 | } |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 929 | break; |
YOSHIFUJI Hideaki | bb4dbf9 | 2007-07-10 22:55:49 -0700 | [diff] [blame] | 930 | #endif |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 931 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | err = -EINVAL; |
| 933 | goto exit_f; |
| 934 | } |
| 935 | |
| 936 | len = ((rthdr->hdrlen + 1) << 3); |
| 937 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 938 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | err = -EINVAL; |
| 940 | goto exit_f; |
| 941 | } |
| 942 | |
| 943 | /* segments left must also match */ |
| 944 | if ((rthdr->hdrlen >> 1) != rthdr->segments_left) { |
| 945 | err = -EINVAL; |
| 946 | goto exit_f; |
| 947 | } |
| 948 | |
| 949 | opt->opt_nflen += len; |
| 950 | opt->srcrt = rthdr; |
| 951 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 952 | if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3); |
| 954 | |
| 955 | opt->opt_nflen += dsthdrlen; |
| 956 | opt->dst0opt = opt->dst1opt; |
| 957 | opt->dst1opt = NULL; |
| 958 | opt->opt_flen -= dsthdrlen; |
| 959 | } |
| 960 | |
| 961 | break; |
| 962 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 963 | case IPV6_2292HOPLIMIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | case IPV6_HOPLIMIT: |
| 965 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { |
| 966 | err = -EINVAL; |
| 967 | goto exit_f; |
| 968 | } |
| 969 | |
Wei Wang | 26879da | 2016-05-02 21:40:07 -0700 | [diff] [blame] | 970 | ipc6->hlimit = *(int *)CMSG_DATA(cmsg); |
| 971 | if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) { |
Shan Wei | e8766fc | 2008-06-10 15:50:55 +0800 | [diff] [blame] | 972 | err = -EINVAL; |
| 973 | goto exit_f; |
| 974 | } |
| 975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | break; |
| 977 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 978 | case IPV6_TCLASS: |
| 979 | { |
| 980 | int tc; |
| 981 | |
| 982 | err = -EINVAL; |
Eldad Zack | b5a4257 | 2012-04-01 07:49:03 +0000 | [diff] [blame] | 983 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 984 | goto exit_f; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 985 | |
| 986 | tc = *(int *)CMSG_DATA(cmsg); |
Remi Denis-Courmont | d0ee011 | 2006-09-13 20:08:07 -0700 | [diff] [blame] | 987 | if (tc < -1 || tc > 0xff) |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 988 | goto exit_f; |
| 989 | |
| 990 | err = 0; |
Wei Wang | 26879da | 2016-05-02 21:40:07 -0700 | [diff] [blame] | 991 | ipc6->tclass = tc; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 992 | |
| 993 | break; |
| 994 | } |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame] | 995 | |
| 996 | case IPV6_DONTFRAG: |
| 997 | { |
| 998 | int df; |
| 999 | |
| 1000 | err = -EINVAL; |
Eldad Zack | b5a4257 | 2012-04-01 07:49:03 +0000 | [diff] [blame] | 1001 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame] | 1002 | goto exit_f; |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame] | 1003 | |
| 1004 | df = *(int *)CMSG_DATA(cmsg); |
| 1005 | if (df < 0 || df > 1) |
| 1006 | goto exit_f; |
| 1007 | |
| 1008 | err = 0; |
Wei Wang | 26879da | 2016-05-02 21:40:07 -0700 | [diff] [blame] | 1009 | ipc6->dontfrag = df; |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame] | 1010 | |
| 1011 | break; |
| 1012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | default: |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 1014 | net_dbg_ratelimited("invalid cmsg type: %d\n", |
| 1015 | cmsg->cmsg_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | err = -EINVAL; |
Miao Xie | 4a36702 | 2008-07-29 23:57:58 -0700 | [diff] [blame] | 1017 | goto exit_f; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | exit_f: |
| 1022 | return err; |
| 1023 | } |
Tom Parkin | 73df66f | 2013-01-31 01:02:24 +0000 | [diff] [blame] | 1024 | EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl); |
Lorenzo Colitti | 17ef66af | 2013-05-31 15:05:48 +0000 | [diff] [blame] | 1025 | |
Paolo Abeni | 6c206b2 | 2018-06-08 11:35:40 +0200 | [diff] [blame] | 1026 | void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp, |
| 1027 | __u16 srcp, __u16 destp, int rqueue, int bucket) |
Lorenzo Colitti | 17ef66af | 2013-05-31 15:05:48 +0000 | [diff] [blame] | 1028 | { |
Lorenzo Colitti | 17ef66af | 2013-05-31 15:05:48 +0000 | [diff] [blame] | 1029 | const struct in6_addr *dest, *src; |
| 1030 | |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 1031 | dest = &sp->sk_v6_daddr; |
| 1032 | src = &sp->sk_v6_rcv_saddr; |
Lorenzo Colitti | 17ef66af | 2013-05-31 15:05:48 +0000 | [diff] [blame] | 1033 | seq_printf(seq, |
| 1034 | "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " |
Patrick Talbert | ea9a037 | 2019-05-17 17:11:28 +0200 | [diff] [blame] | 1035 | "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n", |
Lorenzo Colitti | 17ef66af | 2013-05-31 15:05:48 +0000 | [diff] [blame] | 1036 | bucket, |
| 1037 | src->s6_addr32[0], src->s6_addr32[1], |
| 1038 | src->s6_addr32[2], src->s6_addr32[3], srcp, |
| 1039 | dest->s6_addr32[0], dest->s6_addr32[1], |
| 1040 | dest->s6_addr32[2], dest->s6_addr32[3], destp, |
| 1041 | sp->sk_state, |
| 1042 | sk_wmem_alloc_get(sp), |
Paolo Abeni | 6c206b2 | 2018-06-08 11:35:40 +0200 | [diff] [blame] | 1043 | rqueue, |
Lorenzo Colitti | 17ef66af | 2013-05-31 15:05:48 +0000 | [diff] [blame] | 1044 | 0, 0L, 0, |
| 1045 | from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)), |
| 1046 | 0, |
| 1047 | sock_i_ino(sp), |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 1048 | refcount_read(&sp->sk_refcnt), sp, |
Lorenzo Colitti | 17ef66af | 2013-05-31 15:05:48 +0000 | [diff] [blame] | 1049 | atomic_read(&sp->sk_drops)); |
| 1050 | } |