| 1 | |
| 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 |
|
| 236 | if (!sk->sk_bound_dev_if) {
|
| 237 | err = -EINVAL;
|
| 238 | goto out;
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 |
|
| 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 | |
| 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 | |
| 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 | |
| 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)
|
| 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 | msg->msg_namelen = 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 |
|
| 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 | {
|
| 550 | struct ipv6_pinfo *np = inet6_sk(sk);
|
| 551 | struct sk_buff *skb;
|
| 552 | struct ip6_mtuinfo mtu_info;
|
| 553 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
|
| 554 | int err;
|
| 555 | int copied;
|
| 556 |
|
| 557 | err = -EAGAIN;
|
| 558 | skb = xchg(&np->rxpmtu, NULL);
|
| 559 | if (!skb)
|
| 560 | goto out;
|
| 561 |
|
| 562 | copied = skb->len;
|
| 563 | if (copied > len) {
|
| 564 | msg->msg_flags |= MSG_TRUNC;
|
| 565 | copied = len;
|
| 566 | }
|
| 567 | err = skb_copy_datagram_msg(skb, 0, msg, copied);
|
| 568 | if (err)
|
| 569 | goto out_free_skb;
|
| 570 |
|
| 571 | sock_recv_timestamp(msg, sk, skb);
|
| 572 |
|
| 573 | memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
|
| 574 |
|
| 575 | if (sin) {
|
| 576 | sin->sin6_family = AF_INET6;
|
| 577 | sin->sin6_flowinfo = 0;
|
| 578 | sin->sin6_port = 0;
|
| 579 | sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
|
| 580 | sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
|
| 581 | msg->msg_namelen = sizeof(*sin);
|
| 582 | }
|
| 583 |
|
| 584 | put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
|
| 585 |
|
| 586 | err = copied;
|
| 587 |
|
| 588 | out_free_skb:
|
| 589 | kfree_skb(skb);
|
| 590 | out:
|
| 591 | return err;
|
| 592 | }
|
| 593 |
|
| 594 |
|
| 595 | void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
|
| 596 | struct sk_buff *skb)
|
| 597 | {
|
| 598 | struct ipv6_pinfo *np = inet6_sk(sk);
|
| 599 | bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
|
| 600 |
|
| 601 | if (np->rxopt.bits.rxinfo) {
|
| 602 | struct in6_pktinfo src_info;
|
| 603 |
|
| 604 | if (is_ipv6) {
|
| 605 | src_info.ipi6_ifindex = IP6CB(skb)->iif;
|
| 606 | src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
|
| 607 | } else {
|
| 608 | src_info.ipi6_ifindex =
|
| 609 | PKTINFO_SKB_CB(skb)->ipi_ifindex;
|
| 610 | ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
|
| 611 | &src_info.ipi6_addr);
|
| 612 | }
|
| 613 |
|
| 614 | if (src_info.ipi6_ifindex >= 0)
|
| 615 | put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
|
| 616 | sizeof(src_info), &src_info);
|
| 617 | }
|
| 618 | }
|
| 619 |
|
| 620 | static u16 ipv6_get_exthdr_len(const struct sk_buff *skb, const u8 *ptr)
|
| 621 | {
|
| 622 | u16 len;
|
| 623 |
|
| 624 | if (ptr + 2 > skb_tail_pointer(skb))
|
| 625 | return 0;
|
| 626 |
|
| 627 | len = (ptr[1] + 1) << 3;
|
| 628 |
|
| 629 | return (len <= skb_tail_pointer(skb) - ptr) ? len : 0;
|
| 630 | }
|
| 631 |
|
| 632 | void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
|
| 633 | struct sk_buff *skb)
|
| 634 | {
|
| 635 | struct ipv6_pinfo *np = inet6_sk(sk);
|
| 636 | struct inet6_skb_parm *opt = IP6CB(skb);
|
| 637 | unsigned char *nh = skb_network_header(skb);
|
| 638 |
|
| 639 | if (np->rxopt.bits.rxhlim) {
|
| 640 | int hlim = ipv6_hdr(skb)->hop_limit;
|
| 641 | put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
|
| 642 | }
|
| 643 |
|
| 644 | if (np->rxopt.bits.rxtclass) {
|
| 645 | int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
|
| 646 | put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
|
| 647 | }
|
| 648 |
|
| 649 | if (np->rxopt.bits.rxflow) {
|
| 650 | __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
|
| 651 | if (flowinfo)
|
| 652 | put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
|
| 653 | }
|
| 654 |
|
| 655 |
|
| 656 | if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
|
| 657 | u8 *ptr = nh + sizeof(struct ipv6hdr);
|
| 658 | u16 len = ipv6_get_exthdr_len(skb, ptr);
|
| 659 |
|
| 660 | if (len)
|
| 661 | put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, len, ptr);
|
| 662 | }
|
| 663 |
|
| 664 | if (opt->lastopt &&
|
| 665 | (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
|
| 666 | |
| 667 | * Silly enough, but we need to reparse in order to
|
| 668 | * report extension headers (except for HbH)
|
| 669 | * in order.
|
| 670 | *
|
| 671 | * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
|
| 672 | * (and WILL NOT be) defined because
|
| 673 | * IPV6_RECVDSTOPTS is more generic. --yoshfuji
|
| 674 |
|
| 675 | unsigned int off = sizeof(struct ipv6hdr);
|
| 676 | u8 nexthdr = ipv6_hdr(skb)->nexthdr;
|
| 677 |
|
| 678 | while (off <= opt->lastopt) {
|
| 679 | unsigned int len;
|
| 680 | u8 *ptr = nh + off;
|
| 681 |
|
| 682 | if (ptr + 2 > skb_tail_pointer(skb))
|
| 683 | return;
|
| 684 |
|
| 685 | switch (nexthdr) {
|
| 686 | case IPPROTO_DSTOPTS:
|
| 687 | nexthdr = ptr[0];
|
| 688 | len = ipv6_get_exthdr_len(skb, ptr);
|
| 689 | if (!len)
|
| 690 | return;
|
| 691 | if (np->rxopt.bits.dstopts)
|
| 692 | put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
|
| 693 | break;
|
| 694 | case IPPROTO_ROUTING:
|
| 695 | nexthdr = ptr[0];
|
| 696 | len = ipv6_get_exthdr_len(skb, ptr);
|
| 697 | if (!len)
|
| 698 | return;
|
| 699 | if (np->rxopt.bits.srcrt)
|
| 700 | put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
|
| 701 | break;
|
| 702 | case IPPROTO_AH:
|
| 703 | nexthdr = ptr[0];
|
| 704 | len = (ptr[1] + 2) << 2;
|
| 705 | if (ptr + len > skb_tail_pointer(skb))
|
| 706 | return;
|
| 707 | break;
|
| 708 | default:
|
| 709 | nexthdr = ptr[0];
|
| 710 | len = ipv6_get_exthdr_len(skb, ptr);
|
| 711 | if (!len)
|
| 712 | return;
|
| 713 | break;
|
| 714 | }
|
| 715 |
|
| 716 | off += len;
|
| 717 | }
|
| 718 | }
|
| 719 |
|
| 720 |
|
| 721 | if (np->rxopt.bits.rxoinfo) {
|
| 722 | struct in6_pktinfo src_info;
|
| 723 |
|
| 724 | src_info.ipi6_ifindex = opt->iif;
|
| 725 | src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
|
| 726 | put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
|
| 727 | }
|
| 728 | if (np->rxopt.bits.rxohlim) {
|
| 729 | int hlim = ipv6_hdr(skb)->hop_limit;
|
| 730 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
|
| 731 | }
|
| 732 | if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
|
| 733 | u8 *ptr = nh + sizeof(struct ipv6hdr);
|
| 734 | u16 len = ipv6_get_exthdr_len(skb, ptr);
|
| 735 |
|
| 736 | if (len)
|
| 737 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, len, ptr);
|
| 738 | }
|
| 739 | if (np->rxopt.bits.odstopts && opt->dst0) {
|
| 740 | u8 *ptr = nh + opt->dst0;
|
| 741 | u16 len = ipv6_get_exthdr_len(skb, ptr);
|
| 742 |
|
| 743 | if (len)
|
| 744 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, len, ptr);
|
| 745 | }
|
| 746 | if (np->rxopt.bits.osrcrt && opt->srcrt) {
|
| 747 | struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
|
| 748 | u16 len = ipv6_get_exthdr_len(skb, (u8 *)rthdr);
|
| 749 |
|
| 750 | if (len)
|
| 751 | put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, len, rthdr);
|
| 752 | }
|
| 753 | if (np->rxopt.bits.odstopts && opt->dst1) {
|
| 754 | u8 *ptr = nh + opt->dst1;
|
| 755 | u16 len = ipv6_get_exthdr_len(skb, ptr);
|
| 756 |
|
| 757 | if (len)
|
| 758 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, len, ptr);
|
| 759 | }
|
| 760 | if (np->rxopt.bits.rxorigdstaddr) {
|
| 761 | struct sockaddr_in6 sin6;
|
| 762 | __be16 _ports[2], *ports;
|
| 763 |
|
| 764 | ports = skb_header_pointer(skb, skb_transport_offset(skb),
|
| 765 | sizeof(_ports), &_ports);
|
| 766 | if (ports) {
|
| 767 | |
| 768 | * first four bytes of the transport header and this function is
|
| 769 | * written with this assumption in mind.
|
| 770 |
|
| 771 | sin6.sin6_family = AF_INET6;
|
| 772 | sin6.sin6_addr = ipv6_hdr(skb)->daddr;
|
| 773 | sin6.sin6_port = ports[1];
|
| 774 | sin6.sin6_flowinfo = 0;
|
| 775 | sin6.sin6_scope_id =
|
| 776 | ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
|
| 777 | opt->iif);
|
| 778 |
|
| 779 | put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
|
| 780 | }
|
| 781 | }
|
| 782 | if (np->rxopt.bits.recvfragsize && opt->frag_max_size) {
|
| 783 | int val = opt->frag_max_size;
|
| 784 |
|
| 785 | put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val);
|
| 786 | }
|
| 787 | }
|
| 788 |
|
| 789 | void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
|
| 790 | struct sk_buff *skb)
|
| 791 | {
|
| 792 | ip6_datagram_recv_common_ctl(sk, msg, skb);
|
| 793 | ip6_datagram_recv_specific_ctl(sk, msg, skb);
|
| 794 | }
|
| 795 | EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
|
| 796 |
|
| 797 | int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
|
| 798 | struct msghdr *msg, struct flowi6 *fl6,
|
| 799 | struct ipcm6_cookie *ipc6)
|
| 800 | {
|
| 801 | struct in6_pktinfo *src_info;
|
| 802 | struct cmsghdr *cmsg;
|
| 803 | struct ipv6_rt_hdr *orthdr;
|
| 804 | struct ipv6_rt_hdr *rthdr;
|
| 805 | struct ipv6_opt_hdr *hdr;
|
| 806 | struct ipv6_txoptions *opt = ipc6->opt;
|
| 807 | int len;
|
| 808 | int err = 0;
|
| 809 |
|
| 810 | for_each_cmsghdr(cmsg, msg) {
|
| 811 | int addr_type;
|
| 812 |
|
| 813 | if (!CMSG_OK(msg, cmsg)) {
|
| 814 | err = -EINVAL;
|
| 815 | goto exit_f;
|
| 816 | }
|
| 817 |
|
| 818 | if (cmsg->cmsg_level == SOL_SOCKET) {
|
| 819 | err = __sock_cmsg_send(sk, cmsg, &ipc6->sockc);
|
| 820 | if (err)
|
| 821 | return err;
|
| 822 | continue;
|
| 823 | }
|
| 824 |
|
| 825 | if (cmsg->cmsg_level != SOL_IPV6)
|
| 826 | continue;
|
| 827 |
|
| 828 | switch (cmsg->cmsg_type) {
|
| 829 | case IPV6_PKTINFO:
|
| 830 | case IPV6_2292PKTINFO:
|
| 831 | {
|
| 832 | struct net_device *dev = NULL;
|
| 833 | int src_idx;
|
| 834 |
|
| 835 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
|
| 836 | err = -EINVAL;
|
| 837 | goto exit_f;
|
| 838 | }
|
| 839 |
|
| 840 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
|
| 841 | src_idx = src_info->ipi6_ifindex;
|
| 842 |
|
| 843 | if (src_idx) {
|
| 844 | if (fl6->flowi6_oif &&
|
| 845 | src_idx != fl6->flowi6_oif &&
|
| 846 | (READ_ONCE(sk->sk_bound_dev_if) != fl6->flowi6_oif ||
|
| 847 | !sk_dev_equal_l3scope(sk, src_idx)))
|
| 848 | return -EINVAL;
|
| 849 | fl6->flowi6_oif = src_idx;
|
| 850 | }
|
| 851 |
|
| 852 | addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
|
| 853 |
|
| 854 | rcu_read_lock();
|
| 855 | if (fl6->flowi6_oif) {
|
| 856 | dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
|
| 857 | if (!dev) {
|
| 858 | rcu_read_unlock();
|
| 859 | return -ENODEV;
|
| 860 | }
|
| 861 | } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
|
| 862 | rcu_read_unlock();
|
| 863 | return -EINVAL;
|
| 864 | }
|
| 865 |
|
| 866 | if (addr_type != IPV6_ADDR_ANY) {
|
| 867 | int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
|
| 868 | if (!ipv6_can_nonlocal_bind(net, inet_sk(sk)) &&
|
| 869 | !ipv6_chk_addr_and_flags(net, &src_info->ipi6_addr,
|
| 870 | dev, !strict, 0,
|
| 871 | IFA_F_TENTATIVE) &&
|
| 872 | !ipv6_chk_acast_addr_src(net, dev,
|
| 873 | &src_info->ipi6_addr))
|
| 874 | err = -EINVAL;
|
| 875 | else
|
| 876 | fl6->saddr = src_info->ipi6_addr;
|
| 877 | }
|
| 878 |
|
| 879 | rcu_read_unlock();
|
| 880 |
|
| 881 | if (err)
|
| 882 | goto exit_f;
|
| 883 |
|
| 884 | break;
|
| 885 | }
|
| 886 |
|
| 887 | case IPV6_FLOWINFO:
|
| 888 | if (cmsg->cmsg_len < CMSG_LEN(4)) {
|
| 889 | err = -EINVAL;
|
| 890 | goto exit_f;
|
| 891 | }
|
| 892 |
|
| 893 | if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
|
| 894 | if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
|
| 895 | err = -EINVAL;
|
| 896 | goto exit_f;
|
| 897 | }
|
| 898 | }
|
| 899 | fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
|
| 900 | break;
|
| 901 |
|
| 902 | case IPV6_2292HOPOPTS:
|
| 903 | case IPV6_HOPOPTS:
|
| 904 | if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
|
| 905 | err = -EINVAL;
|
| 906 | goto exit_f;
|
| 907 | }
|
| 908 |
|
| 909 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
|
| 910 | len = ((hdr->hdrlen + 1) << 3);
|
| 911 | if (cmsg->cmsg_len < CMSG_LEN(len)) {
|
| 912 | err = -EINVAL;
|
| 913 | goto exit_f;
|
| 914 | }
|
| 915 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
|
| 916 | err = -EPERM;
|
| 917 | goto exit_f;
|
| 918 | }
|
| 919 | opt->opt_nflen += len;
|
| 920 | opt->hopopt = hdr;
|
| 921 | break;
|
| 922 |
|
| 923 | case IPV6_2292DSTOPTS:
|
| 924 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
|
| 925 | err = -EINVAL;
|
| 926 | goto exit_f;
|
| 927 | }
|
| 928 |
|
| 929 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
|
| 930 | len = ((hdr->hdrlen + 1) << 3);
|
| 931 | if (cmsg->cmsg_len < CMSG_LEN(len)) {
|
| 932 | err = -EINVAL;
|
| 933 | goto exit_f;
|
| 934 | }
|
| 935 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
|
| 936 | err = -EPERM;
|
| 937 | goto exit_f;
|
| 938 | }
|
| 939 | if (opt->dst1opt) {
|
| 940 | err = -EINVAL;
|
| 941 | goto exit_f;
|
| 942 | }
|
| 943 | opt->opt_flen += len;
|
| 944 | opt->dst1opt = hdr;
|
| 945 | break;
|
| 946 |
|
| 947 | case IPV6_DSTOPTS:
|
| 948 | case IPV6_RTHDRDSTOPTS:
|
| 949 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
|
| 950 | err = -EINVAL;
|
| 951 | goto exit_f;
|
| 952 | }
|
| 953 |
|
| 954 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
|
| 955 | len = ((hdr->hdrlen + 1) << 3);
|
| 956 | if (cmsg->cmsg_len < CMSG_LEN(len)) {
|
| 957 | err = -EINVAL;
|
| 958 | goto exit_f;
|
| 959 | }
|
| 960 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
|
| 961 | err = -EPERM;
|
| 962 | goto exit_f;
|
| 963 | }
|
| 964 | if (cmsg->cmsg_type == IPV6_DSTOPTS) {
|
| 965 | if (opt->dst1opt)
|
| 966 | opt->opt_flen -= ipv6_optlen(opt->dst1opt);
|
| 967 | opt->opt_flen += len;
|
| 968 | opt->dst1opt = hdr;
|
| 969 | } else {
|
| 970 | if (opt->dst0opt)
|
| 971 | opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
|
| 972 | opt->opt_nflen += len;
|
| 973 | opt->dst0opt = hdr;
|
| 974 | }
|
| 975 | break;
|
| 976 |
|
| 977 | case IPV6_2292RTHDR:
|
| 978 | case IPV6_RTHDR:
|
| 979 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
|
| 980 | err = -EINVAL;
|
| 981 | goto exit_f;
|
| 982 | }
|
| 983 |
|
| 984 | rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
|
| 985 |
|
| 986 | switch (rthdr->type) {
|
| 987 | #if IS_ENABLED(CONFIG_IPV6_MIP6)
|
| 988 | case IPV6_SRCRT_TYPE_2:
|
| 989 | if (rthdr->hdrlen != 2 ||
|
| 990 | rthdr->segments_left != 1) {
|
| 991 | err = -EINVAL;
|
| 992 | goto exit_f;
|
| 993 | }
|
| 994 | break;
|
| 995 | #endif
|
| 996 | default:
|
| 997 | err = -EINVAL;
|
| 998 | goto exit_f;
|
| 999 | }
|
| 1000 |
|
| 1001 | len = ((rthdr->hdrlen + 1) << 3);
|
| 1002 |
|
| 1003 | if (cmsg->cmsg_len < CMSG_LEN(len)) {
|
| 1004 | err = -EINVAL;
|
| 1005 | goto exit_f;
|
| 1006 | }
|
| 1007 |
|
| 1008 |
|
| 1009 | if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
|
| 1010 | err = -EINVAL;
|
| 1011 | goto exit_f;
|
| 1012 | }
|
| 1013 |
|
| 1014 | orthdr = opt->srcrt;
|
| 1015 | if (orthdr)
|
| 1016 | opt->opt_nflen -= ((orthdr->hdrlen + 1) << 3);
|
| 1017 | opt->opt_nflen += len;
|
| 1018 | opt->srcrt = rthdr;
|
| 1019 |
|
| 1020 | if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
|
| 1021 | int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
|
| 1022 |
|
| 1023 | if (opt->dst0opt)
|
| 1024 | opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
|
| 1025 | opt->opt_nflen += dsthdrlen;
|
| 1026 | opt->dst0opt = opt->dst1opt;
|
| 1027 | opt->dst1opt = NULL;
|
| 1028 | opt->opt_flen -= dsthdrlen;
|
| 1029 | }
|
| 1030 |
|
| 1031 | break;
|
| 1032 |
|
| 1033 | case IPV6_2292HOPLIMIT:
|
| 1034 | case IPV6_HOPLIMIT:
|
| 1035 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
|
| 1036 | err = -EINVAL;
|
| 1037 | goto exit_f;
|
| 1038 | }
|
| 1039 |
|
| 1040 | ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
|
| 1041 | if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
|
| 1042 | err = -EINVAL;
|
| 1043 | goto exit_f;
|
| 1044 | }
|
| 1045 |
|
| 1046 | break;
|
| 1047 |
|
| 1048 | case IPV6_TCLASS:
|
| 1049 | {
|
| 1050 | int tc;
|
| 1051 |
|
| 1052 | err = -EINVAL;
|
| 1053 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
|
| 1054 | goto exit_f;
|
| 1055 |
|
| 1056 | tc = *(int *)CMSG_DATA(cmsg);
|
| 1057 | if (tc < -1 || tc > 0xff)
|
| 1058 | goto exit_f;
|
| 1059 |
|
| 1060 | err = 0;
|
| 1061 | ipc6->tclass = tc;
|
| 1062 |
|
| 1063 | break;
|
| 1064 | }
|
| 1065 |
|
| 1066 | case IPV6_DONTFRAG:
|
| 1067 | {
|
| 1068 | int df;
|
| 1069 |
|
| 1070 | err = -EINVAL;
|
| 1071 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
|
| 1072 | goto exit_f;
|
| 1073 |
|
| 1074 | df = *(int *)CMSG_DATA(cmsg);
|
| 1075 | if (df < 0 || df > 1)
|
| 1076 | goto exit_f;
|
| 1077 |
|
| 1078 | err = 0;
|
| 1079 | ipc6->dontfrag = df;
|
| 1080 |
|
| 1081 | break;
|
| 1082 | }
|
| 1083 | default:
|
| 1084 | net_dbg_ratelimited("invalid cmsg type: %d\n",
|
| 1085 | cmsg->cmsg_type);
|
| 1086 | err = -EINVAL;
|
| 1087 | goto exit_f;
|
| 1088 | }
|
| 1089 | }
|
| 1090 |
|
| 1091 | exit_f:
|
| 1092 | return err;
|
| 1093 | }
|
| 1094 | EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
|
| 1095 |
|
| 1096 | void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
|
| 1097 | __u16 srcp, __u16 destp, int rqueue, int bucket)
|
| 1098 | {
|
| 1099 | const struct in6_addr *dest, *src;
|
| 1100 |
|
| 1101 | dest = &sp->sk_v6_daddr;
|
| 1102 | src = &sp->sk_v6_rcv_saddr;
|
| 1103 | seq_printf(seq,
|
| 1104 | "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
|
| 1105 | "%02X %08X:%08X %02X:%08lX %08X %5u %8d %llu %d %pK %u\n",
|
| 1106 | bucket,
|
| 1107 | src->s6_addr32[0], src->s6_addr32[1],
|
| 1108 | src->s6_addr32[2], src->s6_addr32[3], srcp,
|
| 1109 | dest->s6_addr32[0], dest->s6_addr32[1],
|
| 1110 | dest->s6_addr32[2], dest->s6_addr32[3], destp,
|
| 1111 | sp->sk_state,
|
| 1112 | sk_wmem_alloc_get(sp),
|
| 1113 | rqueue,
|
| 1114 | 0, 0L, 0,
|
| 1115 | from_kuid_munged(seq_user_ns(seq), sk_uid(sp)),
|
| 1116 | 0,
|
| 1117 | sock_i_ino(sp),
|
| 1118 | refcount_read(&sp->sk_refcnt), sp,
|
| 1119 | sk_drops_read(sp));
|
| 1120 | }
|