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