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