1 /* 2 * common UDP/RAW code 3 * Linux INET6 implementation 4 * 5 * Authors: 6 * Pedro Roque <roque@di.fc.ul.pt> 7 * 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 14 #include <linux/capability.h> 15 #include <linux/errno.h> 16 #include <linux/types.h> 17 #include <linux/kernel.h> 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> 24 #include <linux/slab.h> 25 26 #include <net/ipv6.h> 27 #include <net/ndisc.h> 28 #include <net/addrconf.h> 29 #include <net/transp_v6.h> 30 #include <net/ip6_route.h> 31 #include <net/tcp_states.h> 32 33 #include <linux/errqueue.h> 34 #include <asm/uaccess.h> 35 36 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 37 { 38 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr; 39 struct inet_sock *inet = inet_sk(sk); 40 struct ipv6_pinfo *np = inet6_sk(sk); 41 struct in6_addr *daddr, *final_p = NULL, final; 42 struct dst_entry *dst; 43 struct flowi fl; 44 struct ip6_flowlabel *flowlabel = NULL; 45 int addr_type; 46 int err; 47 48 if (usin->sin6_family == AF_INET) { 49 if (__ipv6_only_sock(sk)) 50 return -EAFNOSUPPORT; 51 err = ip4_datagram_connect(sk, uaddr, addr_len); 52 goto ipv4_connected; 53 } 54 55 if (addr_len < SIN6_LEN_RFC2133) 56 return -EINVAL; 57 58 if (usin->sin6_family != AF_INET6) 59 return -EAFNOSUPPORT; 60 61 memset(&fl, 0, sizeof(fl)); 62 if (np->sndflow) { 63 fl.fl6_flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK; 64 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) { 65 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); 66 if (flowlabel == NULL) 67 return -EINVAL; 68 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst); 69 } 70 } 71 72 addr_type = ipv6_addr_type(&usin->sin6_addr); 73 74 if (addr_type == IPV6_ADDR_ANY) { 75 /* 76 * connect to self 77 */ 78 usin->sin6_addr.s6_addr[15] = 0x01; 79 } 80 81 daddr = &usin->sin6_addr; 82 83 if (addr_type == IPV6_ADDR_MAPPED) { 84 struct sockaddr_in sin; 85 86 if (__ipv6_only_sock(sk)) { 87 err = -ENETUNREACH; 88 goto out; 89 } 90 sin.sin_family = AF_INET; 91 sin.sin_addr.s_addr = daddr->s6_addr32[3]; 92 sin.sin_port = usin->sin6_port; 93 94 err = ip4_datagram_connect(sk, 95 (struct sockaddr*) &sin, 96 sizeof(sin)); 97 98 ipv4_connected: 99 if (err) 100 goto out; 101 102 ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr); 103 104 if (ipv6_addr_any(&np->saddr)) 105 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 106 107 if (ipv6_addr_any(&np->rcv_saddr)) 108 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, 109 &np->rcv_saddr); 110 111 goto out; 112 } 113 114 if (addr_type&IPV6_ADDR_LINKLOCAL) { 115 if (addr_len >= sizeof(struct sockaddr_in6) && 116 usin->sin6_scope_id) { 117 if (sk->sk_bound_dev_if && 118 sk->sk_bound_dev_if != usin->sin6_scope_id) { 119 err = -EINVAL; 120 goto out; 121 } 122 sk->sk_bound_dev_if = usin->sin6_scope_id; 123 } 124 125 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST)) 126 sk->sk_bound_dev_if = np->mcast_oif; 127 128 /* Connect to link-local address requires an interface */ 129 if (!sk->sk_bound_dev_if) { 130 err = -EINVAL; 131 goto out; 132 } 133 } 134 135 ipv6_addr_copy(&np->daddr, daddr); 136 np->flow_label = fl.fl6_flowlabel; 137 138 inet->inet_dport = usin->sin6_port; 139 140 /* 141 * Check for a route to destination an obtain the 142 * destination cache for it. 143 */ 144 145 fl.proto = sk->sk_protocol; 146 ipv6_addr_copy(&fl.fl6_dst, &np->daddr); 147 ipv6_addr_copy(&fl.fl6_src, &np->saddr); 148 fl.oif = sk->sk_bound_dev_if; 149 fl.mark = sk->sk_mark; 150 fl.fl_ip_dport = inet->inet_dport; 151 fl.fl_ip_sport = inet->inet_sport; 152 153 if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST)) 154 fl.oif = np->mcast_oif; 155 156 security_sk_classify_flow(sk, &fl); 157 158 if (flowlabel) { 159 if (flowlabel->opt && flowlabel->opt->srcrt) { 160 struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt; 161 ipv6_addr_copy(&final, &fl.fl6_dst); 162 ipv6_addr_copy(&fl.fl6_dst, rt0->addr); 163 final_p = &final; 164 } 165 } else if (np->opt && np->opt->srcrt) { 166 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt; 167 ipv6_addr_copy(&final, &fl.fl6_dst); 168 ipv6_addr_copy(&fl.fl6_dst, rt0->addr); 169 final_p = &final; 170 } 171 172 err = ip6_dst_lookup(sk, &dst, &fl); 173 if (err) 174 goto out; 175 if (final_p) 176 ipv6_addr_copy(&fl.fl6_dst, final_p); 177 178 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT); 179 if (err < 0) { 180 if (err == -EREMOTE) 181 err = ip6_dst_blackhole(sk, &dst, &fl); 182 if (err < 0) 183 goto out; 184 } 185 186 /* source address lookup done in ip6_dst_lookup */ 187 188 if (ipv6_addr_any(&np->saddr)) 189 ipv6_addr_copy(&np->saddr, &fl.fl6_src); 190 191 if (ipv6_addr_any(&np->rcv_saddr)) { 192 ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src); 193 inet->inet_rcv_saddr = LOOPBACK4_IPV6; 194 } 195 196 ip6_dst_store(sk, dst, 197 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ? 198 &np->daddr : NULL, 199 #ifdef CONFIG_IPV6_SUBTREES 200 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ? 201 &np->saddr : 202 #endif 203 NULL); 204 205 sk->sk_state = TCP_ESTABLISHED; 206 out: 207 fl6_sock_release(flowlabel); 208 return err; 209 } 210 211 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, 212 __be16 port, u32 info, u8 *payload) 213 { 214 struct ipv6_pinfo *np = inet6_sk(sk); 215 struct icmp6hdr *icmph = icmp6_hdr(skb); 216 struct sock_exterr_skb *serr; 217 218 if (!np->recverr) 219 return; 220 221 skb = skb_clone(skb, GFP_ATOMIC); 222 if (!skb) 223 return; 224 225 skb->protocol = htons(ETH_P_IPV6); 226 227 serr = SKB_EXT_ERR(skb); 228 serr->ee.ee_errno = err; 229 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6; 230 serr->ee.ee_type = icmph->icmp6_type; 231 serr->ee.ee_code = icmph->icmp6_code; 232 serr->ee.ee_pad = 0; 233 serr->ee.ee_info = info; 234 serr->ee.ee_data = 0; 235 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) - 236 skb_network_header(skb); 237 serr->port = port; 238 239 __skb_pull(skb, payload - skb->data); 240 skb_reset_transport_header(skb); 241 242 if (sock_queue_err_skb(sk, skb)) 243 kfree_skb(skb); 244 } 245 246 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info) 247 { 248 struct ipv6_pinfo *np = inet6_sk(sk); 249 struct sock_exterr_skb *serr; 250 struct ipv6hdr *iph; 251 struct sk_buff *skb; 252 253 if (!np->recverr) 254 return; 255 256 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); 257 if (!skb) 258 return; 259 260 skb->protocol = htons(ETH_P_IPV6); 261 262 skb_put(skb, sizeof(struct ipv6hdr)); 263 skb_reset_network_header(skb); 264 iph = ipv6_hdr(skb); 265 ipv6_addr_copy(&iph->daddr, &fl->fl6_dst); 266 267 serr = SKB_EXT_ERR(skb); 268 serr->ee.ee_errno = err; 269 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; 270 serr->ee.ee_type = 0; 271 serr->ee.ee_code = 0; 272 serr->ee.ee_pad = 0; 273 serr->ee.ee_info = info; 274 serr->ee.ee_data = 0; 275 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); 276 serr->port = fl->fl_ip_dport; 277 278 __skb_pull(skb, skb_tail_pointer(skb) - skb->data); 279 skb_reset_transport_header(skb); 280 281 if (sock_queue_err_skb(sk, skb)) 282 kfree_skb(skb); 283 } 284 285 void ipv6_local_rxpmtu(struct sock *sk, struct flowi *fl, u32 mtu) 286 { 287 struct ipv6_pinfo *np = inet6_sk(sk); 288 struct ipv6hdr *iph; 289 struct sk_buff *skb; 290 struct ip6_mtuinfo *mtu_info; 291 292 if (!np->rxopt.bits.rxpmtu) 293 return; 294 295 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); 296 if (!skb) 297 return; 298 299 skb_put(skb, sizeof(struct ipv6hdr)); 300 skb_reset_network_header(skb); 301 iph = ipv6_hdr(skb); 302 ipv6_addr_copy(&iph->daddr, &fl->fl6_dst); 303 304 mtu_info = IP6CBMTU(skb); 305 if (!mtu_info) { 306 kfree_skb(skb); 307 return; 308 } 309 310 mtu_info->ip6m_mtu = mtu; 311 mtu_info->ip6m_addr.sin6_family = AF_INET6; 312 mtu_info->ip6m_addr.sin6_port = 0; 313 mtu_info->ip6m_addr.sin6_flowinfo = 0; 314 mtu_info->ip6m_addr.sin6_scope_id = fl->oif; 315 ipv6_addr_copy(&mtu_info->ip6m_addr.sin6_addr, &ipv6_hdr(skb)->daddr); 316 317 __skb_pull(skb, skb_tail_pointer(skb) - skb->data); 318 skb_reset_transport_header(skb); 319 320 skb = xchg(&np->rxpmtu, skb); 321 kfree_skb(skb); 322 } 323 324 /* 325 * Handle MSG_ERRQUEUE 326 */ 327 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len) 328 { 329 struct ipv6_pinfo *np = inet6_sk(sk); 330 struct sock_exterr_skb *serr; 331 struct sk_buff *skb, *skb2; 332 struct sockaddr_in6 *sin; 333 struct { 334 struct sock_extended_err ee; 335 struct sockaddr_in6 offender; 336 } errhdr; 337 int err; 338 int copied; 339 340 err = -EAGAIN; 341 skb = skb_dequeue(&sk->sk_error_queue); 342 if (skb == NULL) 343 goto out; 344 345 copied = skb->len; 346 if (copied > len) { 347 msg->msg_flags |= MSG_TRUNC; 348 copied = len; 349 } 350 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 351 if (err) 352 goto out_free_skb; 353 354 sock_recv_timestamp(msg, sk, skb); 355 356 serr = SKB_EXT_ERR(skb); 357 358 sin = (struct sockaddr_in6 *)msg->msg_name; 359 if (sin) { 360 const unsigned char *nh = skb_network_header(skb); 361 sin->sin6_family = AF_INET6; 362 sin->sin6_flowinfo = 0; 363 sin->sin6_port = serr->port; 364 sin->sin6_scope_id = 0; 365 if (skb->protocol == htons(ETH_P_IPV6)) { 366 ipv6_addr_copy(&sin->sin6_addr, 367 (struct in6_addr *)(nh + serr->addr_offset)); 368 if (np->sndflow) 369 sin->sin6_flowinfo = 370 (*(__be32 *)(nh + serr->addr_offset - 24) & 371 IPV6_FLOWINFO_MASK); 372 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) 373 sin->sin6_scope_id = IP6CB(skb)->iif; 374 } else { 375 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset), 376 &sin->sin6_addr); 377 } 378 } 379 380 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); 381 sin = &errhdr.offender; 382 sin->sin6_family = AF_UNSPEC; 383 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) { 384 sin->sin6_family = AF_INET6; 385 sin->sin6_flowinfo = 0; 386 sin->sin6_scope_id = 0; 387 if (skb->protocol == htons(ETH_P_IPV6)) { 388 ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr); 389 if (np->rxopt.all) 390 datagram_recv_ctl(sk, msg, skb); 391 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) 392 sin->sin6_scope_id = IP6CB(skb)->iif; 393 } else { 394 struct inet_sock *inet = inet_sk(sk); 395 396 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 397 &sin->sin6_addr); 398 if (inet->cmsg_flags) 399 ip_cmsg_recv(msg, skb); 400 } 401 } 402 403 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr); 404 405 /* Now we could try to dump offended packet options */ 406 407 msg->msg_flags |= MSG_ERRQUEUE; 408 err = copied; 409 410 /* Reset and regenerate socket error */ 411 spin_lock_bh(&sk->sk_error_queue.lock); 412 sk->sk_err = 0; 413 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) { 414 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; 415 spin_unlock_bh(&sk->sk_error_queue.lock); 416 sk->sk_error_report(sk); 417 } else { 418 spin_unlock_bh(&sk->sk_error_queue.lock); 419 } 420 421 out_free_skb: 422 kfree_skb(skb); 423 out: 424 return err; 425 } 426 427 /* 428 * Handle IPV6_RECVPATHMTU 429 */ 430 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len) 431 { 432 struct ipv6_pinfo *np = inet6_sk(sk); 433 struct sk_buff *skb; 434 struct sockaddr_in6 *sin; 435 struct ip6_mtuinfo mtu_info; 436 int err; 437 int copied; 438 439 err = -EAGAIN; 440 skb = xchg(&np->rxpmtu, NULL); 441 if (skb == NULL) 442 goto out; 443 444 copied = skb->len; 445 if (copied > len) { 446 msg->msg_flags |= MSG_TRUNC; 447 copied = len; 448 } 449 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 450 if (err) 451 goto out_free_skb; 452 453 sock_recv_timestamp(msg, sk, skb); 454 455 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info)); 456 457 sin = (struct sockaddr_in6 *)msg->msg_name; 458 if (sin) { 459 sin->sin6_family = AF_INET6; 460 sin->sin6_flowinfo = 0; 461 sin->sin6_port = 0; 462 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id; 463 ipv6_addr_copy(&sin->sin6_addr, &mtu_info.ip6m_addr.sin6_addr); 464 } 465 466 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info); 467 468 err = copied; 469 470 out_free_skb: 471 kfree_skb(skb); 472 out: 473 return err; 474 } 475 476 477 int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb) 478 { 479 struct ipv6_pinfo *np = inet6_sk(sk); 480 struct inet6_skb_parm *opt = IP6CB(skb); 481 unsigned char *nh = skb_network_header(skb); 482 483 if (np->rxopt.bits.rxinfo) { 484 struct in6_pktinfo src_info; 485 486 src_info.ipi6_ifindex = opt->iif; 487 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr); 488 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); 489 } 490 491 if (np->rxopt.bits.rxhlim) { 492 int hlim = ipv6_hdr(skb)->hop_limit; 493 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); 494 } 495 496 if (np->rxopt.bits.rxtclass) { 497 int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff; 498 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); 499 } 500 501 if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) { 502 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK; 503 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); 504 } 505 506 /* HbH is allowed only once */ 507 if (np->rxopt.bits.hopopts && opt->hop) { 508 u8 *ptr = nh + opt->hop; 509 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr); 510 } 511 512 if (opt->lastopt && 513 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) { 514 /* 515 * Silly enough, but we need to reparse in order to 516 * report extension headers (except for HbH) 517 * in order. 518 * 519 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT 520 * (and WILL NOT be) defined because 521 * IPV6_RECVDSTOPTS is more generic. --yoshfuji 522 */ 523 unsigned int off = sizeof(struct ipv6hdr); 524 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 525 526 while (off <= opt->lastopt) { 527 unsigned len; 528 u8 *ptr = nh + off; 529 530 switch(nexthdr) { 531 case IPPROTO_DSTOPTS: 532 nexthdr = ptr[0]; 533 len = (ptr[1] + 1) << 3; 534 if (np->rxopt.bits.dstopts) 535 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr); 536 break; 537 case IPPROTO_ROUTING: 538 nexthdr = ptr[0]; 539 len = (ptr[1] + 1) << 3; 540 if (np->rxopt.bits.srcrt) 541 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr); 542 break; 543 case IPPROTO_AH: 544 nexthdr = ptr[0]; 545 len = (ptr[1] + 2) << 2; 546 break; 547 default: 548 nexthdr = ptr[0]; 549 len = (ptr[1] + 1) << 3; 550 break; 551 } 552 553 off += len; 554 } 555 } 556 557 /* socket options in old style */ 558 if (np->rxopt.bits.rxoinfo) { 559 struct in6_pktinfo src_info; 560 561 src_info.ipi6_ifindex = opt->iif; 562 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr); 563 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); 564 } 565 if (np->rxopt.bits.rxohlim) { 566 int hlim = ipv6_hdr(skb)->hop_limit; 567 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); 568 } 569 if (np->rxopt.bits.ohopopts && opt->hop) { 570 u8 *ptr = nh + opt->hop; 571 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr); 572 } 573 if (np->rxopt.bits.odstopts && opt->dst0) { 574 u8 *ptr = nh + opt->dst0; 575 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); 576 } 577 if (np->rxopt.bits.osrcrt && opt->srcrt) { 578 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt); 579 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr); 580 } 581 if (np->rxopt.bits.odstopts && opt->dst1) { 582 u8 *ptr = nh + opt->dst1; 583 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); 584 } 585 return 0; 586 } 587 588 int datagram_send_ctl(struct net *net, 589 struct msghdr *msg, struct flowi *fl, 590 struct ipv6_txoptions *opt, 591 int *hlimit, int *tclass, int *dontfrag) 592 { 593 struct in6_pktinfo *src_info; 594 struct cmsghdr *cmsg; 595 struct ipv6_rt_hdr *rthdr; 596 struct ipv6_opt_hdr *hdr; 597 int len; 598 int err = 0; 599 600 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { 601 int addr_type; 602 603 if (!CMSG_OK(msg, cmsg)) { 604 err = -EINVAL; 605 goto exit_f; 606 } 607 608 if (cmsg->cmsg_level != SOL_IPV6) 609 continue; 610 611 switch (cmsg->cmsg_type) { 612 case IPV6_PKTINFO: 613 case IPV6_2292PKTINFO: 614 { 615 struct net_device *dev = NULL; 616 617 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) { 618 err = -EINVAL; 619 goto exit_f; 620 } 621 622 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); 623 624 if (src_info->ipi6_ifindex) { 625 if (fl->oif && src_info->ipi6_ifindex != fl->oif) 626 return -EINVAL; 627 fl->oif = src_info->ipi6_ifindex; 628 } 629 630 addr_type = __ipv6_addr_type(&src_info->ipi6_addr); 631 632 rcu_read_lock(); 633 if (fl->oif) { 634 dev = dev_get_by_index_rcu(net, fl->oif); 635 if (!dev) { 636 rcu_read_unlock(); 637 return -ENODEV; 638 } 639 } else if (addr_type & IPV6_ADDR_LINKLOCAL) { 640 rcu_read_unlock(); 641 return -EINVAL; 642 } 643 644 if (addr_type != IPV6_ADDR_ANY) { 645 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL; 646 if (!ipv6_chk_addr(net, &src_info->ipi6_addr, 647 strict ? dev : NULL, 0)) 648 err = -EINVAL; 649 else 650 ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr); 651 } 652 653 rcu_read_unlock(); 654 655 if (err) 656 goto exit_f; 657 658 break; 659 } 660 661 case IPV6_FLOWINFO: 662 if (cmsg->cmsg_len < CMSG_LEN(4)) { 663 err = -EINVAL; 664 goto exit_f; 665 } 666 667 if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) { 668 if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) { 669 err = -EINVAL; 670 goto exit_f; 671 } 672 } 673 fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg); 674 break; 675 676 case IPV6_2292HOPOPTS: 677 case IPV6_HOPOPTS: 678 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { 679 err = -EINVAL; 680 goto exit_f; 681 } 682 683 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); 684 len = ((hdr->hdrlen + 1) << 3); 685 if (cmsg->cmsg_len < CMSG_LEN(len)) { 686 err = -EINVAL; 687 goto exit_f; 688 } 689 if (!capable(CAP_NET_RAW)) { 690 err = -EPERM; 691 goto exit_f; 692 } 693 opt->opt_nflen += len; 694 opt->hopopt = hdr; 695 break; 696 697 case IPV6_2292DSTOPTS: 698 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { 699 err = -EINVAL; 700 goto exit_f; 701 } 702 703 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); 704 len = ((hdr->hdrlen + 1) << 3); 705 if (cmsg->cmsg_len < CMSG_LEN(len)) { 706 err = -EINVAL; 707 goto exit_f; 708 } 709 if (!capable(CAP_NET_RAW)) { 710 err = -EPERM; 711 goto exit_f; 712 } 713 if (opt->dst1opt) { 714 err = -EINVAL; 715 goto exit_f; 716 } 717 opt->opt_flen += len; 718 opt->dst1opt = hdr; 719 break; 720 721 case IPV6_DSTOPTS: 722 case IPV6_RTHDRDSTOPTS: 723 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { 724 err = -EINVAL; 725 goto exit_f; 726 } 727 728 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); 729 len = ((hdr->hdrlen + 1) << 3); 730 if (cmsg->cmsg_len < CMSG_LEN(len)) { 731 err = -EINVAL; 732 goto exit_f; 733 } 734 if (!capable(CAP_NET_RAW)) { 735 err = -EPERM; 736 goto exit_f; 737 } 738 if (cmsg->cmsg_type == IPV6_DSTOPTS) { 739 opt->opt_flen += len; 740 opt->dst1opt = hdr; 741 } else { 742 opt->opt_nflen += len; 743 opt->dst0opt = hdr; 744 } 745 break; 746 747 case IPV6_2292RTHDR: 748 case IPV6_RTHDR: 749 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { 750 err = -EINVAL; 751 goto exit_f; 752 } 753 754 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg); 755 756 switch (rthdr->type) { 757 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 758 case IPV6_SRCRT_TYPE_2: 759 if (rthdr->hdrlen != 2 || 760 rthdr->segments_left != 1) { 761 err = -EINVAL; 762 goto exit_f; 763 } 764 break; 765 #endif 766 default: 767 err = -EINVAL; 768 goto exit_f; 769 } 770 771 len = ((rthdr->hdrlen + 1) << 3); 772 773 if (cmsg->cmsg_len < CMSG_LEN(len)) { 774 err = -EINVAL; 775 goto exit_f; 776 } 777 778 /* segments left must also match */ 779 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) { 780 err = -EINVAL; 781 goto exit_f; 782 } 783 784 opt->opt_nflen += len; 785 opt->srcrt = rthdr; 786 787 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) { 788 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3); 789 790 opt->opt_nflen += dsthdrlen; 791 opt->dst0opt = opt->dst1opt; 792 opt->dst1opt = NULL; 793 opt->opt_flen -= dsthdrlen; 794 } 795 796 break; 797 798 case IPV6_2292HOPLIMIT: 799 case IPV6_HOPLIMIT: 800 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { 801 err = -EINVAL; 802 goto exit_f; 803 } 804 805 *hlimit = *(int *)CMSG_DATA(cmsg); 806 if (*hlimit < -1 || *hlimit > 0xff) { 807 err = -EINVAL; 808 goto exit_f; 809 } 810 811 break; 812 813 case IPV6_TCLASS: 814 { 815 int tc; 816 817 err = -EINVAL; 818 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { 819 goto exit_f; 820 } 821 822 tc = *(int *)CMSG_DATA(cmsg); 823 if (tc < -1 || tc > 0xff) 824 goto exit_f; 825 826 err = 0; 827 *tclass = tc; 828 829 break; 830 } 831 832 case IPV6_DONTFRAG: 833 { 834 int df; 835 836 err = -EINVAL; 837 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { 838 goto exit_f; 839 } 840 841 df = *(int *)CMSG_DATA(cmsg); 842 if (df < 0 || df > 1) 843 goto exit_f; 844 845 err = 0; 846 *dontfrag = df; 847 848 break; 849 } 850 default: 851 LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n", 852 cmsg->cmsg_type); 853 err = -EINVAL; 854 goto exit_f; 855 } 856 } 857 858 exit_f: 859 return err; 860 } 861