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