182 ip6 = mtod(m, struct ip6_hdr *); 183#endif /* IPSEC */ 184 185#define MAKE_EXTHDR(hp, mp) \ 186 do { \ 187 if (hp) { \ 188 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 189 error = ip6_copyexthdr((mp), (caddr_t)(hp), \ 190 ((eh)->ip6e_len + 1) << 3); \ 191 if (error) \ 192 goto freehdrs; \ 193 } \ 194 } while (0) 195 196 bzero(&exthdrs, sizeof(exthdrs)); 197 198 if (opt) { 199 /* Hop-by-Hop options header */ 200 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 201 /* Destination options header(1st part) */ 202 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 203 /* Routing header */ 204 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 205 /* Destination options header(2nd part) */ 206 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 207 } 208 209#ifdef IPSEC 210 /* get a security policy for this packet */ 211 if (so == NULL) 212 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error); 213 else 214 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 215 216 if (sp == NULL) { 217 ipsec6stat.out_inval++; 218 goto freehdrs; 219 } 220 221 error = 0; 222 223 /* check policy */ 224 switch (sp->policy) { 225 case IPSEC_POLICY_DISCARD: 226 /* 227 * This packet is just discarded. 228 */ 229 ipsec6stat.out_polvio++; 230 goto freehdrs; 231 232 case IPSEC_POLICY_BYPASS: 233 case IPSEC_POLICY_NONE: 234 /* no need to do IPsec. */ 235 needipsec = 0; 236 break; 237 238 case IPSEC_POLICY_IPSEC: 239 if (sp->req == NULL) { 240 /* acquire a policy */ 241 error = key_spdacquire(sp); 242 goto freehdrs; 243 } 244 needipsec = 1; 245 break; 246 247 case IPSEC_POLICY_ENTRUST: 248 default: 249 printf("ip6_output: Invalid policy found. %d\n", sp->policy); 250 } 251#endif /* IPSEC */ 252 253 /* 254 * Calculate the total length of the extension header chain. 255 * Keep the length of the unfragmentable part for fragmentation. 256 */ 257 optlen = 0; 258 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len; 259 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len; 260 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len; 261 unfragpartlen = optlen + sizeof(struct ip6_hdr); 262 /* NOTE: we don't add AH/ESP length here. do that later. */ 263 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len; 264 265 /* 266 * If we need IPsec, or there is at least one extension header, 267 * separate IP6 header from the payload. 268 */ 269 if ((needipsec || optlen) && !hdrsplit) { 270 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 271 m = NULL; 272 goto freehdrs; 273 } 274 m = exthdrs.ip6e_ip6; 275 hdrsplit++; 276 } 277 278 /* adjust pointer */ 279 ip6 = mtod(m, struct ip6_hdr *); 280 281 /* adjust mbuf packet header length */ 282 m->m_pkthdr.len += optlen; 283 plen = m->m_pkthdr.len - sizeof(*ip6); 284 285 /* If this is a jumbo payload, insert a jumbo payload option. */ 286 if (plen > IPV6_MAXPACKET) { 287 if (!hdrsplit) { 288 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 289 m = NULL; 290 goto freehdrs; 291 } 292 m = exthdrs.ip6e_ip6; 293 hdrsplit++; 294 } 295 /* adjust pointer */ 296 ip6 = mtod(m, struct ip6_hdr *); 297 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 298 goto freehdrs; 299 ip6->ip6_plen = 0; 300 } else 301 ip6->ip6_plen = htons(plen); 302 303 /* 304 * Concatenate headers and fill in next header fields. 305 * Here we have, on "m" 306 * IPv6 payload 307 * and we insert headers accordingly. Finally, we should be getting: 308 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 309 * 310 * during the header composing process, "m" points to IPv6 header. 311 * "mprev" points to an extension header prior to esp. 312 */ 313 { 314 u_char *nexthdrp = &ip6->ip6_nxt; 315 struct mbuf *mprev = m; 316 317 /* 318 * we treat dest2 specially. this makes IPsec processing 319 * much easier. the goal here is to make mprev point the 320 * mbuf prior to dest2. 321 * 322 * result: IPv6 dest2 payload 323 * m and mprev will point to IPv6 header. 324 */ 325 if (exthdrs.ip6e_dest2) { 326 if (!hdrsplit) 327 panic("assumption failed: hdr not split"); 328 exthdrs.ip6e_dest2->m_next = m->m_next; 329 m->m_next = exthdrs.ip6e_dest2; 330 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 331 ip6->ip6_nxt = IPPROTO_DSTOPTS; 332 } 333 334#define MAKE_CHAIN(m, mp, p, i)\ 335 do {\ 336 if (m) {\ 337 if (!hdrsplit) \ 338 panic("assumption failed: hdr not split"); \ 339 *mtod((m), u_char *) = *(p);\ 340 *(p) = (i);\ 341 p = mtod((m), u_char *);\ 342 (m)->m_next = (mp)->m_next;\ 343 (mp)->m_next = (m);\ 344 (mp) = (m);\ 345 }\ 346 } while (0) 347 /* 348 * result: IPv6 hbh dest1 rthdr dest2 payload 349 * m will point to IPv6 header. mprev will point to the 350 * extension header prior to dest2 (rthdr in the above case). 351 */ 352 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, 353 nexthdrp, IPPROTO_HOPOPTS); 354 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, 355 nexthdrp, IPPROTO_DSTOPTS); 356 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, 357 nexthdrp, IPPROTO_ROUTING); 358 359#ifdef IPSEC 360 if (!needipsec) 361 goto skip_ipsec2; 362 363 /* 364 * pointers after IPsec headers are not valid any more. 365 * other pointers need a great care too. 366 * (IPsec routines should not mangle mbufs prior to AH/ESP) 367 */ 368 exthdrs.ip6e_dest2 = NULL; 369 370 { 371 struct ip6_rthdr *rh = NULL; 372 int segleft_org = 0; 373 struct ipsec_output_state state; 374 375 if (exthdrs.ip6e_rthdr) { 376 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 377 segleft_org = rh->ip6r_segleft; 378 rh->ip6r_segleft = 0; 379 } 380 381 bzero(&state, sizeof(state)); 382 state.m = m; 383 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags, 384 &needipsectun); 385 m = state.m; 386 if (error) { 387 /* mbuf is already reclaimed in ipsec6_output_trans. */ 388 m = NULL; 389 switch (error) { 390 case EHOSTUNREACH: 391 case ENETUNREACH: 392 case EMSGSIZE: 393 case ENOBUFS: 394 case ENOMEM: 395 break; 396 default: 397 printf("ip6_output (ipsec): error code %d\n", error); 398 /* fall through */ 399 case ENOENT: 400 /* don't show these error codes to the user */ 401 error = 0; 402 break; 403 } 404 goto bad; 405 } 406 if (exthdrs.ip6e_rthdr) { 407 /* ah6_output doesn't modify mbuf chain */ 408 rh->ip6r_segleft = segleft_org; 409 } 410 } 411skip_ipsec2:; 412#endif 413 } 414 415 /* 416 * If there is a routing header, replace destination address field 417 * with the first hop of the routing header. 418 */ 419 if (exthdrs.ip6e_rthdr) { 420 struct ip6_rthdr *rh = 421 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, 422 struct ip6_rthdr *)); 423 struct ip6_rthdr0 *rh0; 424 425 finaldst = ip6->ip6_dst; 426 switch (rh->ip6r_type) { 427 case IPV6_RTHDR_TYPE_0: 428 rh0 = (struct ip6_rthdr0 *)rh; 429 ip6->ip6_dst = rh0->ip6r0_addr[0]; 430 bcopy((caddr_t)&rh0->ip6r0_addr[1], 431 (caddr_t)&rh0->ip6r0_addr[0], 432 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1) 433 ); 434 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst; 435 break; 436 default: /* is it possible? */ 437 error = EINVAL; 438 goto bad; 439 } 440 } 441 442 /* Source address validation */ 443 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 444 (flags & IPV6_DADOUTPUT) == 0) { 445 error = EOPNOTSUPP; 446 ip6stat.ip6s_badscope++; 447 goto bad; 448 } 449 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 450 error = EOPNOTSUPP; 451 ip6stat.ip6s_badscope++; 452 goto bad; 453 } 454 455 ip6stat.ip6s_localout++; 456 457 /* 458 * Route packet. 459 */ 460 if (ro == 0) { 461 ro = &ip6route; 462 bzero((caddr_t)ro, sizeof(*ro)); 463 } 464 ro_pmtu = ro; 465 if (opt && opt->ip6po_rthdr) 466 ro = &opt->ip6po_route; 467 dst = (struct sockaddr_in6 *)&ro->ro_dst; 468 /* 469 * If there is a cached route, 470 * check that it is to the same destination 471 * and is still up. If not, free it and try again. 472 */ 473 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 474 dst->sin6_family != AF_INET6 || 475 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) { 476 RTFREE(ro->ro_rt); 477 ro->ro_rt = (struct rtentry *)0; 478 } 479 if (ro->ro_rt == 0) { 480 bzero(dst, sizeof(*dst)); 481 dst->sin6_family = AF_INET6; 482 dst->sin6_len = sizeof(struct sockaddr_in6); 483 dst->sin6_addr = ip6->ip6_dst; 484#ifdef SCOPEDROUTING 485 /* XXX: sin6_scope_id should already be fixed at this point */ 486 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr)) 487 dst->sin6_scope_id = ntohs(dst->sin6_addr.s6_addr16[1]); 488#endif 489 } 490#ifdef IPSEC 491 if (needipsec && needipsectun) { 492 struct ipsec_output_state state; 493 494 /* 495 * All the extension headers will become inaccessible 496 * (since they can be encrypted). 497 * Don't panic, we need no more updates to extension headers 498 * on inner IPv6 packet (since they are now encapsulated). 499 * 500 * IPv6 [ESP|AH] IPv6 [extension headers] payload 501 */ 502 bzero(&exthdrs, sizeof(exthdrs)); 503 exthdrs.ip6e_ip6 = m; 504 505 bzero(&state, sizeof(state)); 506 state.m = m; 507 state.ro = (struct route *)ro; 508 state.dst = (struct sockaddr *)dst; 509 510 error = ipsec6_output_tunnel(&state, sp, flags); 511 512 m = state.m; 513 ro = (struct route_in6 *)state.ro; 514 dst = (struct sockaddr_in6 *)state.dst; 515 if (error) { 516 /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 517 m0 = m = NULL; 518 m = NULL; 519 switch (error) { 520 case EHOSTUNREACH: 521 case ENETUNREACH: 522 case EMSGSIZE: 523 case ENOBUFS: 524 case ENOMEM: 525 break; 526 default: 527 printf("ip6_output (ipsec): error code %d\n", error); 528 /* fall through */ 529 case ENOENT: 530 /* don't show these error codes to the user */ 531 error = 0; 532 break; 533 } 534 goto bad; 535 } 536 537 exthdrs.ip6e_ip6 = m; 538 } 539#endif /* IPSEC */ 540 541 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 542 /* Unicast */ 543 544#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 545#define sin6tosa(sin6) ((struct sockaddr *)(sin6)) 546 /* xxx 547 * interface selection comes here 548 * if an interface is specified from an upper layer, 549 * ifp must point it. 550 */ 551 if (ro->ro_rt == 0) { 552 /* 553 * non-bsdi always clone routes, if parent is 554 * PRF_CLONING. 555 */ 556 rtalloc((struct route *)ro); 557 } 558 if (ro->ro_rt == 0) { 559 ip6stat.ip6s_noroute++; 560 error = EHOSTUNREACH; 561 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */ 562 goto bad; 563 } 564 ia = ifatoia6(ro->ro_rt->rt_ifa); 565 ifp = ro->ro_rt->rt_ifp; 566 ro->ro_rt->rt_use++; 567 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 568 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway; 569 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 570 571 in6_ifstat_inc(ifp, ifs6_out_request); 572 573 /* 574 * Check if the outgoing interface conflicts with 575 * the interface specified by ifi6_ifindex (if specified). 576 * Note that loopback interface is always okay. 577 * (this may happen when we are sending a packet to one of 578 * our own addresses.) 579 */ 580 if (opt && opt->ip6po_pktinfo 581 && opt->ip6po_pktinfo->ipi6_ifindex) { 582 if (!(ifp->if_flags & IFF_LOOPBACK) 583 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) { 584 ip6stat.ip6s_noroute++; 585 in6_ifstat_inc(ifp, ifs6_out_discard); 586 error = EHOSTUNREACH; 587 goto bad; 588 } 589 } 590 591 if (opt && opt->ip6po_hlim != -1) 592 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 593 } else { 594 /* Multicast */ 595 struct in6_multi *in6m; 596 597 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 598 599 /* 600 * See if the caller provided any multicast options 601 */ 602 ifp = NULL; 603 if (im6o != NULL) { 604 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 605 if (im6o->im6o_multicast_ifp != NULL) 606 ifp = im6o->im6o_multicast_ifp; 607 } else 608 ip6->ip6_hlim = ip6_defmcasthlim; 609 610 /* 611 * See if the caller provided the outgoing interface 612 * as an ancillary data. 613 * Boundary check for ifindex is assumed to be already done. 614 */ 615 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex) 616 ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex); 617 618 /* 619 * If the destination is a node-local scope multicast, 620 * the packet should be loop-backed only. 621 */ 622 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) { 623 /* 624 * If the outgoing interface is already specified, 625 * it should be a loopback interface. 626 */ 627 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) { 628 ip6stat.ip6s_badscope++; 629 error = ENETUNREACH; /* XXX: better error? */ 630 /* XXX correct ifp? */ 631 in6_ifstat_inc(ifp, ifs6_out_discard); 632 goto bad; 633 } else { 634 ifp = &loif[0]; 635 } 636 } 637 638 if (opt && opt->ip6po_hlim != -1) 639 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 640 641 /* 642 * If caller did not provide an interface lookup a 643 * default in the routing table. This is either a 644 * default for the speicfied group (i.e. a host 645 * route), or a multicast default (a route for the 646 * ``net'' ff00::/8). 647 */ 648 if (ifp == NULL) { 649 if (ro->ro_rt == 0) { 650 ro->ro_rt = rtalloc1((struct sockaddr *) 651 &ro->ro_dst, 0, 0UL); 652 } 653 if (ro->ro_rt == 0) { 654 ip6stat.ip6s_noroute++; 655 error = EHOSTUNREACH; 656 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */ 657 goto bad; 658 } 659 ia = ifatoia6(ro->ro_rt->rt_ifa); 660 ifp = ro->ro_rt->rt_ifp; 661 ro->ro_rt->rt_use++; 662 } 663 664 if ((flags & IPV6_FORWARDING) == 0) 665 in6_ifstat_inc(ifp, ifs6_out_request); 666 in6_ifstat_inc(ifp, ifs6_out_mcast); 667 668 /* 669 * Confirm that the outgoing interface supports multicast. 670 */ 671 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 672 ip6stat.ip6s_noroute++; 673 in6_ifstat_inc(ifp, ifs6_out_discard); 674 error = ENETUNREACH; 675 goto bad; 676 } 677 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); 678 if (in6m != NULL && 679 (im6o == NULL || im6o->im6o_multicast_loop)) { 680 /* 681 * If we belong to the destination multicast group 682 * on the outgoing interface, and the caller did not 683 * forbid loopback, loop back a copy. 684 */ 685 ip6_mloopback(ifp, m, dst); 686 } else { 687 /* 688 * If we are acting as a multicast router, perform 689 * multicast forwarding as if the packet had just 690 * arrived on the interface to which we are about 691 * to send. The multicast forwarding function 692 * recursively calls this function, using the 693 * IPV6_FORWARDING flag to prevent infinite recursion. 694 * 695 * Multicasts that are looped back by ip6_mloopback(), 696 * above, will be forwarded by the ip6_input() routine, 697 * if necessary. 698 */ 699 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 700 if (ip6_mforward(ip6, ifp, m) != 0) { 701 m_freem(m); 702 goto done; 703 } 704 } 705 } 706 /* 707 * Multicasts with a hoplimit of zero may be looped back, 708 * above, but must not be transmitted on a network. 709 * Also, multicasts addressed to the loopback interface 710 * are not sent -- the above call to ip6_mloopback() will 711 * loop back a copy if this host actually belongs to the 712 * destination group on the loopback interface. 713 */ 714 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) { 715 m_freem(m); 716 goto done; 717 } 718 } 719 720 /* 721 * Fill the outgoing inteface to tell the upper layer 722 * to increment per-interface statistics. 723 */ 724 if (ifpp) 725 *ifpp = ifp; 726 727 /* 728 * Determine path MTU. 729 */ 730 if (ro_pmtu != ro) { 731 /* The first hop and the final destination may differ. */ 732 struct sockaddr_in6 *sin6_fin = 733 (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 734 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 735 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr, 736 &finaldst))) { 737 RTFREE(ro_pmtu->ro_rt); 738 ro_pmtu->ro_rt = (struct rtentry *)0; 739 } 740 if (ro_pmtu->ro_rt == 0) { 741 bzero(sin6_fin, sizeof(*sin6_fin)); 742 sin6_fin->sin6_family = AF_INET6; 743 sin6_fin->sin6_len = sizeof(struct sockaddr_in6); 744 sin6_fin->sin6_addr = finaldst; 745 746 rtalloc((struct route *)ro_pmtu); 747 } 748 } 749 if (ro_pmtu->ro_rt != NULL) { 750 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu; 751 752 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu; 753 if (mtu > ifmtu || mtu == 0) { 754 /* 755 * The MTU on the route is larger than the MTU on 756 * the interface! This shouldn't happen, unless the 757 * MTU of the interface has been changed after the 758 * interface was brought up. Change the MTU in the 759 * route to match the interface MTU (as long as the 760 * field isn't locked). 761 * 762 * if MTU on the route is 0, we need to fix the MTU. 763 * this case happens with path MTU discovery timeouts. 764 */ 765 mtu = ifmtu; 766 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 767 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */ 768 } 769 } else { 770 mtu = nd_ifinfo[ifp->if_index].linkmtu; 771 } 772 773 /* 774 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting 775 */ 776 if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU) 777 mtu = IPV6_MMTU; 778 779 /* Fake scoped addresses */ 780 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 781 /* 782 * If source or destination address is a scoped address, and 783 * the packet is going to be sent to a loopback interface, 784 * we should keep the original interface. 785 */ 786 787 /* 788 * XXX: this is a very experimental and temporary solution. 789 * We eventually have sockaddr_in6 and use the sin6_scope_id 790 * field of the structure here. 791 * We rely on the consistency between two scope zone ids 792 * of source and destination, which should already be assured. 793 * Larger scopes than link will be supported in the future. 794 */ 795 origifp = NULL; 796 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 797 origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1])); 798 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 799 origifp = ifnet_byindex(ntohs(ip6->ip6_dst.s6_addr16[1])); 800 /* 801 * XXX: origifp can be NULL even in those two cases above. 802 * For example, if we remove the (only) link-local address 803 * from the loopback interface, and try to send a link-local 804 * address without link-id information. Then the source 805 * address is ::1, and the destination address is the 806 * link-local address with its s6_addr16[1] being zero. 807 * What is worse, if the packet goes to the loopback interface 808 * by a default rejected route, the null pointer would be 809 * passed to looutput, and the kernel would hang. 810 * The following last resort would prevent such disaster. 811 */ 812 if (origifp == NULL) 813 origifp = ifp; 814 } 815 else 816 origifp = ifp; 817#ifndef SCOPEDROUTING 818 /* 819 * clear embedded scope identifiers if necessary. 820 * in6_clearscope will touch the addresses only when necessary. 821 */ 822 in6_clearscope(&ip6->ip6_src); 823 in6_clearscope(&ip6->ip6_dst); 824#endif 825 826 /* 827 * Check with the firewall... 828 */ 829 if (ip6_fw_enable && ip6_fw_chk_ptr) { 830 u_short port = 0; 831 m->m_pkthdr.rcvif = NULL; /* XXX */ 832 /* If ipfw says divert, we have to just drop packet */ 833 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) { 834 m_freem(m); 835 goto done; 836 } 837 if (!m) { 838 error = EACCES; 839 goto done; 840 } 841 } 842 843 /* 844 * If the outgoing packet contains a hop-by-hop options header, 845 * it must be examined and processed even by the source node. 846 * (RFC 2460, section 4.) 847 */ 848 if (exthdrs.ip6e_hbh) { 849 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 850 u_int32_t dummy1; /* XXX unused */ 851 u_int32_t dummy2; /* XXX unused */ 852 853#ifdef DIAGNOSTIC 854 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) 855 panic("ip6e_hbh is not continuous"); 856#endif 857 /* 858 * XXX: if we have to send an ICMPv6 error to the sender, 859 * we need the M_LOOP flag since icmp6_error() expects 860 * the IPv6 and the hop-by-hop options header are 861 * continuous unless the flag is set. 862 */ 863 m->m_flags |= M_LOOP; 864 m->m_pkthdr.rcvif = ifp; 865 if (ip6_process_hopopts(m, 866 (u_int8_t *)(hbh + 1), 867 ((hbh->ip6h_len + 1) << 3) - 868 sizeof(struct ip6_hbh), 869 &dummy1, &dummy2) < 0) { 870 /* m was already freed at this point */ 871 error = EINVAL;/* better error? */ 872 goto done; 873 } 874 m->m_flags &= ~M_LOOP; /* XXX */ 875 m->m_pkthdr.rcvif = NULL; 876 } 877 878#ifdef PFIL_HOOKS 879 /* 880 * Run through list of hooks for output packets. 881 */ 882 m1 = m; 883 pfh = pfil_hook_get(PFIL_OUT, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh); 884 for (; pfh; pfh = pfh->pfil_link.tqe_next) 885 if (pfh->pfil_func) { 886 rv = pfh->pfil_func(ip6, sizeof(*ip6), ifp, 1, &m1); 887 if (rv) { 888 error = EHOSTUNREACH; 889 goto done; 890 } 891 m = m1; 892 if (m == NULL) 893 goto done; 894 ip6 = mtod(m, struct ip6_hdr *); 895 } 896#endif /* PFIL_HOOKS */ 897 /* 898 * Send the packet to the outgoing interface. 899 * If necessary, do IPv6 fragmentation before sending. 900 */ 901 tlen = m->m_pkthdr.len; 902 if (tlen <= mtu 903#ifdef notyet 904 /* 905 * On any link that cannot convey a 1280-octet packet in one piece, 906 * link-specific fragmentation and reassembly must be provided at 907 * a layer below IPv6. [RFC 2460, sec.5] 908 * Thus if the interface has ability of link-level fragmentation, 909 * we can just send the packet even if the packet size is 910 * larger than the link's MTU. 911 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet... 912 */ 913 914 || ifp->if_flags & IFF_FRAGMENTABLE 915#endif 916 ) 917 { 918 /* Record statistics for this interface address. */ 919 if (ia && !(flags & IPV6_FORWARDING)) { 920 ia->ia_ifa.if_opackets++; 921 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 922 } 923#ifdef IPSEC 924 /* clean ipsec history once it goes out of the node */ 925 ipsec_delaux(m); 926#endif 927 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 928 goto done; 929 } else if (mtu < IPV6_MMTU) { 930 /* 931 * note that path MTU is never less than IPV6_MMTU 932 * (see icmp6_input). 933 */ 934 error = EMSGSIZE; 935 in6_ifstat_inc(ifp, ifs6_out_fragfail); 936 goto bad; 937 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */ 938 error = EMSGSIZE; 939 in6_ifstat_inc(ifp, ifs6_out_fragfail); 940 goto bad; 941 } else { 942 struct mbuf **mnext, *m_frgpart; 943 struct ip6_frag *ip6f; 944 u_int32_t id = htonl(ip6_id++); 945 u_char nextproto; 946 947 /* 948 * Too large for the destination or interface; 949 * fragment if possible. 950 * Must be able to put at least 8 bytes per fragment. 951 */ 952 hlen = unfragpartlen; 953 if (mtu > IPV6_MAXPACKET) 954 mtu = IPV6_MAXPACKET; 955 956 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 957 if (len < 8) { 958 error = EMSGSIZE; 959 in6_ifstat_inc(ifp, ifs6_out_fragfail); 960 goto bad; 961 } 962 963 mnext = &m->m_nextpkt; 964 965 /* 966 * Change the next header field of the last header in the 967 * unfragmentable part. 968 */ 969 if (exthdrs.ip6e_rthdr) { 970 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 971 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 972 } else if (exthdrs.ip6e_dest1) { 973 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 974 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 975 } else if (exthdrs.ip6e_hbh) { 976 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 977 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 978 } else { 979 nextproto = ip6->ip6_nxt; 980 ip6->ip6_nxt = IPPROTO_FRAGMENT; 981 } 982 983 /* 984 * Loop through length of segment after first fragment, 985 * make new header and copy data of each part and link onto 986 * chain. 987 */ 988 m0 = m; 989 for (off = hlen; off < tlen; off += len) { 990 MGETHDR(m, M_DONTWAIT, MT_HEADER); 991 if (!m) { 992 error = ENOBUFS; 993 ip6stat.ip6s_odropped++; 994 goto sendorfree; 995 } 996 m->m_pkthdr.rcvif = NULL; 997 m->m_flags = m0->m_flags & M_COPYFLAGS; 998 *mnext = m; 999 mnext = &m->m_nextpkt; 1000 m->m_data += max_linkhdr; 1001 mhip6 = mtod(m, struct ip6_hdr *); 1002 *mhip6 = *ip6; 1003 m->m_len = sizeof(*mhip6); 1004 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 1005 if (error) { 1006 ip6stat.ip6s_odropped++; 1007 goto sendorfree; 1008 } 1009 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7)); 1010 if (off + len >= tlen) 1011 len = tlen - off; 1012 else 1013 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 1014 mhip6->ip6_plen = htons((u_short)(len + hlen + 1015 sizeof(*ip6f) - 1016 sizeof(struct ip6_hdr))); 1017 if ((m_frgpart = m_copy(m0, off, len)) == 0) { 1018 error = ENOBUFS; 1019 ip6stat.ip6s_odropped++; 1020 goto sendorfree; 1021 } 1022 m_cat(m, m_frgpart); 1023 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 1024 m->m_pkthdr.rcvif = (struct ifnet *)0; 1025 ip6f->ip6f_reserved = 0; 1026 ip6f->ip6f_ident = id; 1027 ip6f->ip6f_nxt = nextproto; 1028 ip6stat.ip6s_ofragments++; 1029 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 1030 } 1031 1032 in6_ifstat_inc(ifp, ifs6_out_fragok); 1033 } 1034 1035 /* 1036 * Remove leading garbages. 1037 */ 1038sendorfree: 1039 m = m0->m_nextpkt; 1040 m0->m_nextpkt = 0; 1041 m_freem(m0); 1042 for (m0 = m; m; m = m0) { 1043 m0 = m->m_nextpkt; 1044 m->m_nextpkt = 0; 1045 if (error == 0) { 1046 /* Record statistics for this interface address. */ 1047 if (ia) { 1048 ia->ia_ifa.if_opackets++; 1049 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1050 } 1051#ifdef IPSEC 1052 /* clean ipsec history once it goes out of the node */ 1053 ipsec_delaux(m); 1054#endif 1055 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 1056 } else 1057 m_freem(m); 1058 } 1059 1060 if (error == 0) 1061 ip6stat.ip6s_fragmented++; 1062 1063done: 1064 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */ 1065 RTFREE(ro->ro_rt); 1066 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) { 1067 RTFREE(ro_pmtu->ro_rt); 1068 } 1069 1070#ifdef IPSEC 1071 if (sp != NULL) 1072 key_freesp(sp); 1073#endif /* IPSEC */ 1074 1075 return(error); 1076 1077freehdrs: 1078 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1079 m_freem(exthdrs.ip6e_dest1); 1080 m_freem(exthdrs.ip6e_rthdr); 1081 m_freem(exthdrs.ip6e_dest2); 1082 /* fall through */ 1083bad: 1084 m_freem(m); 1085 goto done; 1086} 1087 1088static int 1089ip6_copyexthdr(mp, hdr, hlen) 1090 struct mbuf **mp; 1091 caddr_t hdr; 1092 int hlen; 1093{ 1094 struct mbuf *m; 1095 1096 if (hlen > MCLBYTES) 1097 return(ENOBUFS); /* XXX */ 1098 1099 MGET(m, M_DONTWAIT, MT_DATA); 1100 if (!m) 1101 return(ENOBUFS); 1102 1103 if (hlen > MLEN) { 1104 MCLGET(m, M_DONTWAIT); 1105 if ((m->m_flags & M_EXT) == 0) { 1106 m_free(m); 1107 return(ENOBUFS); 1108 } 1109 } 1110 m->m_len = hlen; 1111 if (hdr) 1112 bcopy(hdr, mtod(m, caddr_t), hlen); 1113 1114 *mp = m; 1115 return(0); 1116} 1117 1118/* 1119 * Insert jumbo payload option. 1120 */ 1121static int 1122ip6_insert_jumboopt(exthdrs, plen) 1123 struct ip6_exthdrs *exthdrs; 1124 u_int32_t plen; 1125{ 1126 struct mbuf *mopt; 1127 u_char *optbuf; 1128 u_int32_t v; 1129 1130#define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1131 1132 /* 1133 * If there is no hop-by-hop options header, allocate new one. 1134 * If there is one but it doesn't have enough space to store the 1135 * jumbo payload option, allocate a cluster to store the whole options. 1136 * Otherwise, use it to store the options. 1137 */ 1138 if (exthdrs->ip6e_hbh == 0) { 1139 MGET(mopt, M_DONTWAIT, MT_DATA); 1140 if (mopt == 0) 1141 return(ENOBUFS); 1142 mopt->m_len = JUMBOOPTLEN; 1143 optbuf = mtod(mopt, u_char *); 1144 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1145 exthdrs->ip6e_hbh = mopt; 1146 } else { 1147 struct ip6_hbh *hbh; 1148 1149 mopt = exthdrs->ip6e_hbh; 1150 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1151 /* 1152 * XXX assumption: 1153 * - exthdrs->ip6e_hbh is not referenced from places 1154 * other than exthdrs. 1155 * - exthdrs->ip6e_hbh is not an mbuf chain. 1156 */ 1157 int oldoptlen = mopt->m_len; 1158 struct mbuf *n; 1159 1160 /* 1161 * XXX: give up if the whole (new) hbh header does 1162 * not fit even in an mbuf cluster. 1163 */ 1164 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1165 return(ENOBUFS); 1166 1167 /* 1168 * As a consequence, we must always prepare a cluster 1169 * at this point. 1170 */ 1171 MGET(n, M_DONTWAIT, MT_DATA); 1172 if (n) { 1173 MCLGET(n, M_DONTWAIT); 1174 if ((n->m_flags & M_EXT) == 0) { 1175 m_freem(n); 1176 n = NULL; 1177 } 1178 } 1179 if (!n) 1180 return(ENOBUFS); 1181 n->m_len = oldoptlen + JUMBOOPTLEN; 1182 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1183 oldoptlen); 1184 optbuf = mtod(n, caddr_t) + oldoptlen; 1185 m_freem(mopt); 1186 mopt = exthdrs->ip6e_hbh = n; 1187 } else { 1188 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1189 mopt->m_len += JUMBOOPTLEN; 1190 } 1191 optbuf[0] = IP6OPT_PADN; 1192 optbuf[1] = 1; 1193 1194 /* 1195 * Adjust the header length according to the pad and 1196 * the jumbo payload option. 1197 */ 1198 hbh = mtod(mopt, struct ip6_hbh *); 1199 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1200 } 1201 1202 /* fill in the option. */ 1203 optbuf[2] = IP6OPT_JUMBO; 1204 optbuf[3] = 4; 1205 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1206 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1207 1208 /* finally, adjust the packet header length */ 1209 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1210 1211 return(0); 1212#undef JUMBOOPTLEN 1213} 1214 1215/* 1216 * Insert fragment header and copy unfragmentable header portions. 1217 */ 1218static int 1219ip6_insertfraghdr(m0, m, hlen, frghdrp) 1220 struct mbuf *m0, *m; 1221 int hlen; 1222 struct ip6_frag **frghdrp; 1223{ 1224 struct mbuf *n, *mlast; 1225 1226 if (hlen > sizeof(struct ip6_hdr)) { 1227 n = m_copym(m0, sizeof(struct ip6_hdr), 1228 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1229 if (n == 0) 1230 return(ENOBUFS); 1231 m->m_next = n; 1232 } else 1233 n = m; 1234 1235 /* Search for the last mbuf of unfragmentable part. */ 1236 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1237 ; 1238 1239 if ((mlast->m_flags & M_EXT) == 0 && 1240 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1241 /* use the trailing space of the last mbuf for the fragment hdr */ 1242 *frghdrp = 1243 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len); 1244 mlast->m_len += sizeof(struct ip6_frag); 1245 m->m_pkthdr.len += sizeof(struct ip6_frag); 1246 } else { 1247 /* allocate a new mbuf for the fragment header */ 1248 struct mbuf *mfrg; 1249 1250 MGET(mfrg, M_DONTWAIT, MT_DATA); 1251 if (mfrg == 0) 1252 return(ENOBUFS); 1253 mfrg->m_len = sizeof(struct ip6_frag); 1254 *frghdrp = mtod(mfrg, struct ip6_frag *); 1255 mlast->m_next = mfrg; 1256 } 1257 1258 return(0); 1259} 1260 1261/* 1262 * IP6 socket option processing. 1263 */ 1264int 1265ip6_ctloutput(so, sopt) 1266 struct socket *so; 1267 struct sockopt *sopt; 1268{ 1269 int privileged; 1270 struct inpcb *in6p = sotoinpcb(so); 1271 int error, optval; 1272 int level, op, optname; 1273 int optlen; 1274 struct thread *td; 1275 1276 if (sopt) { 1277 level = sopt->sopt_level; 1278 op = sopt->sopt_dir; 1279 optname = sopt->sopt_name; 1280 optlen = sopt->sopt_valsize; 1281 td = sopt->sopt_td; 1282 } else { 1283 panic("ip6_ctloutput: arg soopt is NULL"); 1284 } 1285 error = optval = 0; 1286 1287 privileged = (td == 0 || suser(td)) ? 0 : 1; 1288 1289 if (level == IPPROTO_IPV6) { 1290 switch (op) { 1291 1292 case SOPT_SET: 1293 switch (optname) { 1294 case IPV6_PKTOPTIONS: 1295 { 1296 struct mbuf *m; 1297 1298 error = soopt_getm(sopt, &m); /* XXX */ 1299 if (error != 0) 1300 break; 1301 error = soopt_mcopyin(sopt, m); /* XXX */ 1302 if (error != 0) 1303 break; 1304 error = ip6_pcbopts(&in6p->in6p_outputopts, 1305 m, so, sopt); 1306 m_freem(m); /* XXX */ 1307 break; 1308 } 1309 1310 /* 1311 * Use of some Hop-by-Hop options or some 1312 * Destination options, might require special 1313 * privilege. That is, normal applications 1314 * (without special privilege) might be forbidden 1315 * from setting certain options in outgoing packets, 1316 * and might never see certain options in received 1317 * packets. [RFC 2292 Section 6] 1318 * KAME specific note: 1319 * KAME prevents non-privileged users from sending or 1320 * receiving ANY hbh/dst options in order to avoid 1321 * overhead of parsing options in the kernel. 1322 */ 1323 case IPV6_UNICAST_HOPS: 1324 case IPV6_CHECKSUM: 1325 case IPV6_FAITH: 1326 1327 case IPV6_V6ONLY: 1328 if (optlen != sizeof(int)) { 1329 error = EINVAL; 1330 break; 1331 } 1332 error = sooptcopyin(sopt, &optval, 1333 sizeof optval, sizeof optval); 1334 if (error) 1335 break; 1336 switch (optname) { 1337 1338 case IPV6_UNICAST_HOPS: 1339 if (optval < -1 || optval >= 256) 1340 error = EINVAL; 1341 else { 1342 /* -1 = kernel default */ 1343 in6p->in6p_hops = optval; 1344 1345 if ((in6p->in6p_vflag & 1346 INP_IPV4) != 0) 1347 in6p->inp_ip_ttl = optval; 1348 } 1349 break; 1350#define OPTSET(bit) \ 1351do { \ 1352 if (optval) \ 1353 in6p->in6p_flags |= (bit); \ 1354 else \ 1355 in6p->in6p_flags &= ~(bit); \ 1356} while (0) 1357#define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) 1358 1359 case IPV6_CHECKSUM: 1360 in6p->in6p_cksum = optval; 1361 break; 1362 1363 case IPV6_FAITH: 1364 OPTSET(IN6P_FAITH); 1365 break; 1366 1367 case IPV6_V6ONLY: 1368 /* 1369 * make setsockopt(IPV6_V6ONLY) 1370 * available only prior to bind(2). 1371 * see ipng mailing list, Jun 22 2001. 1372 */ 1373 if (in6p->in6p_lport || 1374 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 1375 { 1376 error = EINVAL; 1377 break; 1378 } 1379 OPTSET(IN6P_IPV6_V6ONLY); 1380 if (optval) 1381 in6p->in6p_vflag &= ~INP_IPV4; 1382 else 1383 in6p->in6p_vflag |= INP_IPV4; 1384 break; 1385 } 1386 break; 1387 1388 case IPV6_PKTINFO: 1389 case IPV6_HOPLIMIT: 1390 case IPV6_HOPOPTS: 1391 case IPV6_DSTOPTS: 1392 case IPV6_RTHDR: 1393 /* RFC 2292 */ 1394 if (optlen != sizeof(int)) { 1395 error = EINVAL; 1396 break; 1397 } 1398 error = sooptcopyin(sopt, &optval, 1399 sizeof optval, sizeof optval); 1400 if (error) 1401 break; 1402 switch (optname) { 1403 case IPV6_PKTINFO: 1404 OPTSET(IN6P_PKTINFO); 1405 break; 1406 case IPV6_HOPLIMIT: 1407 OPTSET(IN6P_HOPLIMIT); 1408 break; 1409 case IPV6_HOPOPTS: 1410 /* 1411 * Check super-user privilege. 1412 * See comments for IPV6_RECVHOPOPTS. 1413 */ 1414 if (!privileged) 1415 return(EPERM); 1416 OPTSET(IN6P_HOPOPTS); 1417 break; 1418 case IPV6_DSTOPTS: 1419 if (!privileged) 1420 return(EPERM); 1421 OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1422 break; 1423 case IPV6_RTHDR: 1424 OPTSET(IN6P_RTHDR); 1425 break; 1426 } 1427 break; 1428#undef OPTSET 1429 1430 case IPV6_MULTICAST_IF: 1431 case IPV6_MULTICAST_HOPS: 1432 case IPV6_MULTICAST_LOOP: 1433 case IPV6_JOIN_GROUP: 1434 case IPV6_LEAVE_GROUP: 1435 { 1436 struct mbuf *m; 1437 if (sopt->sopt_valsize > MLEN) { 1438 error = EMSGSIZE; 1439 break; 1440 } 1441 /* XXX */ 1442 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1443 if (m == 0) { 1444 error = ENOBUFS; 1445 break; 1446 } 1447 m->m_len = sopt->sopt_valsize; 1448 error = sooptcopyin(sopt, mtod(m, char *), 1449 m->m_len, m->m_len); 1450 error = ip6_setmoptions(sopt->sopt_name, 1451 &in6p->in6p_moptions, 1452 m); 1453 (void)m_free(m); 1454 } 1455 break; 1456 1457 case IPV6_PORTRANGE: 1458 error = sooptcopyin(sopt, &optval, 1459 sizeof optval, sizeof optval); 1460 if (error) 1461 break; 1462 1463 switch (optval) { 1464 case IPV6_PORTRANGE_DEFAULT: 1465 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1466 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1467 break; 1468 1469 case IPV6_PORTRANGE_HIGH: 1470 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1471 in6p->in6p_flags |= IN6P_HIGHPORT; 1472 break; 1473 1474 case IPV6_PORTRANGE_LOW: 1475 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1476 in6p->in6p_flags |= IN6P_LOWPORT; 1477 break; 1478 1479 default: 1480 error = EINVAL; 1481 break; 1482 } 1483 break; 1484 1485#ifdef IPSEC 1486 case IPV6_IPSEC_POLICY: 1487 { 1488 caddr_t req = NULL; 1489 size_t len = 0; 1490 struct mbuf *m; 1491 1492 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1493 break; 1494 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1495 break; 1496 if (m) { 1497 req = mtod(m, caddr_t); 1498 len = m->m_len; 1499 } 1500 error = ipsec6_set_policy(in6p, optname, req, 1501 len, privileged); 1502 m_freem(m); 1503 } 1504 break; 1505#endif /* KAME IPSEC */ 1506 1507 case IPV6_FW_ADD: 1508 case IPV6_FW_DEL: 1509 case IPV6_FW_FLUSH: 1510 case IPV6_FW_ZERO: 1511 { 1512 struct mbuf *m; 1513 struct mbuf **mp = &m; 1514 1515 if (ip6_fw_ctl_ptr == NULL) 1516 return EINVAL; 1517 /* XXX */ 1518 if ((error = soopt_getm(sopt, &m)) != 0) 1519 break; 1520 /* XXX */ 1521 if ((error = soopt_mcopyin(sopt, m)) != 0) 1522 break; 1523 error = (*ip6_fw_ctl_ptr)(optname, mp); 1524 m = *mp; 1525 } 1526 break; 1527 1528 default: 1529 error = ENOPROTOOPT; 1530 break; 1531 } 1532 break; 1533 1534 case SOPT_GET: 1535 switch (optname) { 1536 1537 case IPV6_PKTOPTIONS: 1538 if (in6p->in6p_options) { 1539 struct mbuf *m; 1540 m = m_copym(in6p->in6p_options, 1541 0, M_COPYALL, M_TRYWAIT); 1542 error = soopt_mcopyout(sopt, m); 1543 if (error == 0) 1544 m_freem(m); 1545 } else 1546 sopt->sopt_valsize = 0; 1547 break; 1548 1549 case IPV6_UNICAST_HOPS: 1550 case IPV6_CHECKSUM: 1551 1552 case IPV6_FAITH: 1553 case IPV6_V6ONLY: 1554 case IPV6_PORTRANGE: 1555 switch (optname) { 1556 1557 case IPV6_UNICAST_HOPS: 1558 optval = in6p->in6p_hops; 1559 break; 1560 1561 case IPV6_CHECKSUM: 1562 optval = in6p->in6p_cksum; 1563 break; 1564 1565 case IPV6_FAITH: 1566 optval = OPTBIT(IN6P_FAITH); 1567 break; 1568 1569 case IPV6_V6ONLY: 1570 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1571 break; 1572 1573 case IPV6_PORTRANGE: 1574 { 1575 int flags; 1576 flags = in6p->in6p_flags; 1577 if (flags & IN6P_HIGHPORT) 1578 optval = IPV6_PORTRANGE_HIGH; 1579 else if (flags & IN6P_LOWPORT) 1580 optval = IPV6_PORTRANGE_LOW; 1581 else 1582 optval = 0; 1583 break; 1584 } 1585 } 1586 error = sooptcopyout(sopt, &optval, 1587 sizeof optval); 1588 break; 1589 1590 case IPV6_PKTINFO: 1591 case IPV6_HOPLIMIT: 1592 case IPV6_HOPOPTS: 1593 case IPV6_RTHDR: 1594 case IPV6_DSTOPTS: 1595 if (optname == IPV6_HOPOPTS || 1596 optname == IPV6_DSTOPTS || 1597 !privileged) 1598 return(EPERM); 1599 switch (optname) { 1600 case IPV6_PKTINFO: 1601 optval = OPTBIT(IN6P_PKTINFO); 1602 break; 1603 case IPV6_HOPLIMIT: 1604 optval = OPTBIT(IN6P_HOPLIMIT); 1605 break; 1606 case IPV6_HOPOPTS: 1607 if (!privileged) 1608 return(EPERM); 1609 optval = OPTBIT(IN6P_HOPOPTS); 1610 break; 1611 case IPV6_RTHDR: 1612 optval = OPTBIT(IN6P_RTHDR); 1613 break; 1614 case IPV6_DSTOPTS: 1615 if (!privileged) 1616 return(EPERM); 1617 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1618 break; 1619 } 1620 error = sooptcopyout(sopt, &optval, 1621 sizeof optval); 1622 break; 1623 1624 case IPV6_MULTICAST_IF: 1625 case IPV6_MULTICAST_HOPS: 1626 case IPV6_MULTICAST_LOOP: 1627 case IPV6_JOIN_GROUP: 1628 case IPV6_LEAVE_GROUP: 1629 { 1630 struct mbuf *m; 1631 error = ip6_getmoptions(sopt->sopt_name, 1632 in6p->in6p_moptions, &m); 1633 if (error == 0) 1634 error = sooptcopyout(sopt, 1635 mtod(m, char *), m->m_len); 1636 m_freem(m); 1637 } 1638 break; 1639 1640#ifdef IPSEC 1641 case IPV6_IPSEC_POLICY: 1642 { 1643 caddr_t req = NULL; 1644 size_t len = 0; 1645 struct mbuf *m = NULL; 1646 struct mbuf **mp = &m; 1647 1648 error = soopt_getm(sopt, &m); /* XXX */ 1649 if (error != 0) 1650 break; 1651 error = soopt_mcopyin(sopt, m); /* XXX */ 1652 if (error != 0) 1653 break; 1654 if (m) { 1655 req = mtod(m, caddr_t); 1656 len = m->m_len; 1657 } 1658 error = ipsec6_get_policy(in6p, req, len, mp); 1659 if (error == 0) 1660 error = soopt_mcopyout(sopt, m); /*XXX*/ 1661 if (error == 0 && m) 1662 m_freem(m); 1663 break; 1664 } 1665#endif /* KAME IPSEC */ 1666 1667 case IPV6_FW_GET: 1668 { 1669 struct mbuf *m; 1670 struct mbuf **mp = &m; 1671 1672 if (ip6_fw_ctl_ptr == NULL) 1673 { 1674 return EINVAL; 1675 } 1676 error = (*ip6_fw_ctl_ptr)(optname, mp); 1677 if (error == 0) 1678 error = soopt_mcopyout(sopt, m); /* XXX */ 1679 if (error == 0 && m) 1680 m_freem(m); 1681 } 1682 break; 1683 1684 default: 1685 error = ENOPROTOOPT; 1686 break; 1687 } 1688 break; 1689 } 1690 } else { 1691 error = EINVAL; 1692 } 1693 return(error); 1694} 1695 1696/* 1697 * Set up IP6 options in pcb for insertion in output packets or 1698 * specifying behavior of outgoing packets. 1699 */ 1700static int 1701ip6_pcbopts(pktopt, m, so, sopt) 1702 struct ip6_pktopts **pktopt; 1703 struct mbuf *m; 1704 struct socket *so; 1705 struct sockopt *sopt; 1706{ 1707 struct ip6_pktopts *opt = *pktopt; 1708 int error = 0; 1709 struct thread *td = sopt->sopt_td; 1710 int priv = 0; 1711 1712 /* turn off any old options. */ 1713 if (opt) { 1714#ifdef DIAGNOSTIC 1715 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 1716 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 1717 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 1718 printf("ip6_pcbopts: all specified options are cleared.\n"); 1719#endif 1720 ip6_clearpktopts(opt, 1, -1); 1721 } else 1722 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 1723 *pktopt = NULL; 1724 1725 if (!m || m->m_len == 0) { 1726 /* 1727 * Only turning off any previous options. 1728 */ 1729 if (opt) 1730 free(opt, M_IP6OPT); 1731 return(0); 1732 } 1733 1734 /* set options specified by user. */ 1735 if (td && !suser(td)) 1736 priv = 1; 1737 if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) { 1738 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */ 1739 return(error); 1740 } 1741 *pktopt = opt; 1742 return(0); 1743} 1744 1745/* 1746 * initialize ip6_pktopts. beware that there are non-zero default values in 1747 * the struct. 1748 */ 1749void 1750init_ip6pktopts(opt) 1751 struct ip6_pktopts *opt; 1752{ 1753 1754 bzero(opt, sizeof(*opt)); 1755 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 1756} 1757 1758void 1759ip6_clearpktopts(pktopt, needfree, optname) 1760 struct ip6_pktopts *pktopt; 1761 int needfree, optname; 1762{ 1763 if (pktopt == NULL) 1764 return; 1765 1766 if (optname == -1) { 1767 if (needfree && pktopt->ip6po_pktinfo) 1768 free(pktopt->ip6po_pktinfo, M_IP6OPT); 1769 pktopt->ip6po_pktinfo = NULL; 1770 } 1771 if (optname == -1) 1772 pktopt->ip6po_hlim = -1; 1773 if (optname == -1) { 1774 if (needfree && pktopt->ip6po_nexthop) 1775 free(pktopt->ip6po_nexthop, M_IP6OPT); 1776 pktopt->ip6po_nexthop = NULL; 1777 } 1778 if (optname == -1) { 1779 if (needfree && pktopt->ip6po_hbh) 1780 free(pktopt->ip6po_hbh, M_IP6OPT); 1781 pktopt->ip6po_hbh = NULL; 1782 } 1783 if (optname == -1) { 1784 if (needfree && pktopt->ip6po_dest1) 1785 free(pktopt->ip6po_dest1, M_IP6OPT); 1786 pktopt->ip6po_dest1 = NULL; 1787 } 1788 if (optname == -1) { 1789 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 1790 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 1791 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 1792 if (pktopt->ip6po_route.ro_rt) { 1793 RTFREE(pktopt->ip6po_route.ro_rt); 1794 pktopt->ip6po_route.ro_rt = NULL; 1795 } 1796 } 1797 if (optname == -1) { 1798 if (needfree && pktopt->ip6po_dest2) 1799 free(pktopt->ip6po_dest2, M_IP6OPT); 1800 pktopt->ip6po_dest2 = NULL; 1801 } 1802} 1803 1804#define PKTOPT_EXTHDRCPY(type) \ 1805do {\ 1806 if (src->type) {\ 1807 int hlen =\ 1808 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 1809 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 1810 if (dst->type == NULL && canwait == M_NOWAIT)\ 1811 goto bad;\ 1812 bcopy(src->type, dst->type, hlen);\ 1813 }\ 1814} while (0) 1815 1816struct ip6_pktopts * 1817ip6_copypktopts(src, canwait) 1818 struct ip6_pktopts *src; 1819 int canwait; 1820{ 1821 struct ip6_pktopts *dst; 1822 1823 if (src == NULL) { 1824 printf("ip6_clearpktopts: invalid argument\n"); 1825 return(NULL); 1826 } 1827 1828 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 1829 if (dst == NULL && canwait == M_NOWAIT) 1830 goto bad; 1831 bzero(dst, sizeof(*dst)); 1832 1833 dst->ip6po_hlim = src->ip6po_hlim; 1834 if (src->ip6po_pktinfo) { 1835 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 1836 M_IP6OPT, canwait); 1837 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT) 1838 goto bad; 1839 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 1840 } 1841 if (src->ip6po_nexthop) { 1842 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 1843 M_IP6OPT, canwait); 1844 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT) 1845 goto bad; 1846 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 1847 src->ip6po_nexthop->sa_len); 1848 } 1849 PKTOPT_EXTHDRCPY(ip6po_hbh); 1850 PKTOPT_EXTHDRCPY(ip6po_dest1); 1851 PKTOPT_EXTHDRCPY(ip6po_dest2); 1852 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 1853 return(dst); 1854 1855 bad: 1856 printf("ip6_copypktopts: copy failed"); 1857 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT); 1858 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT); 1859 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT); 1860 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT); 1861 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT); 1862 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT); 1863 return(NULL); 1864} 1865#undef PKTOPT_EXTHDRCPY 1866 1867void 1868ip6_freepcbopts(pktopt) 1869 struct ip6_pktopts *pktopt; 1870{ 1871 if (pktopt == NULL) 1872 return; 1873 1874 ip6_clearpktopts(pktopt, 1, -1); 1875 1876 free(pktopt, M_IP6OPT); 1877} 1878 1879/* 1880 * Set the IP6 multicast options in response to user setsockopt(). 1881 */ 1882static int 1883ip6_setmoptions(optname, im6op, m) 1884 int optname; 1885 struct ip6_moptions **im6op; 1886 struct mbuf *m; 1887{ 1888 int error = 0; 1889 u_int loop, ifindex; 1890 struct ipv6_mreq *mreq; 1891 struct ifnet *ifp; 1892 struct ip6_moptions *im6o = *im6op; 1893 struct route_in6 ro; 1894 struct sockaddr_in6 *dst; 1895 struct in6_multi_mship *imm; 1896 struct thread *td = curthread; /* XXX */ 1897 1898 if (im6o == NULL) { 1899 /* 1900 * No multicast option buffer attached to the pcb; 1901 * allocate one and initialize to default values. 1902 */ 1903 im6o = (struct ip6_moptions *) 1904 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK); 1905 1906 if (im6o == NULL) 1907 return(ENOBUFS); 1908 *im6op = im6o; 1909 im6o->im6o_multicast_ifp = NULL; 1910 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 1911 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 1912 LIST_INIT(&im6o->im6o_memberships); 1913 } 1914 1915 switch (optname) { 1916 1917 case IPV6_MULTICAST_IF: 1918 /* 1919 * Select the interface for outgoing multicast packets. 1920 */ 1921 if (m == NULL || m->m_len != sizeof(u_int)) { 1922 error = EINVAL; 1923 break; 1924 } 1925 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex)); 1926 if (ifindex < 0 || if_index < ifindex) { 1927 error = ENXIO; /* XXX EINVAL? */ 1928 break; 1929 } 1930 ifp = ifnet_byindex(ifindex); 1931 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1932 error = EADDRNOTAVAIL; 1933 break; 1934 } 1935 im6o->im6o_multicast_ifp = ifp; 1936 break; 1937 1938 case IPV6_MULTICAST_HOPS: 1939 { 1940 /* 1941 * Set the IP6 hoplimit for outgoing multicast packets. 1942 */ 1943 int optval; 1944 if (m == NULL || m->m_len != sizeof(int)) { 1945 error = EINVAL; 1946 break; 1947 } 1948 bcopy(mtod(m, u_int *), &optval, sizeof(optval)); 1949 if (optval < -1 || optval >= 256) 1950 error = EINVAL; 1951 else if (optval == -1) 1952 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 1953 else 1954 im6o->im6o_multicast_hlim = optval; 1955 break; 1956 } 1957 1958 case IPV6_MULTICAST_LOOP: 1959 /* 1960 * Set the loopback flag for outgoing multicast packets. 1961 * Must be zero or one. 1962 */ 1963 if (m == NULL || m->m_len != sizeof(u_int)) { 1964 error = EINVAL; 1965 break; 1966 } 1967 bcopy(mtod(m, u_int *), &loop, sizeof(loop)); 1968 if (loop > 1) { 1969 error = EINVAL; 1970 break; 1971 } 1972 im6o->im6o_multicast_loop = loop; 1973 break; 1974 1975 case IPV6_JOIN_GROUP: 1976 /* 1977 * Add a multicast group membership. 1978 * Group must be a valid IP6 multicast address. 1979 */ 1980 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 1981 error = EINVAL; 1982 break; 1983 } 1984 mreq = mtod(m, struct ipv6_mreq *); 1985 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 1986 /* 1987 * We use the unspecified address to specify to accept 1988 * all multicast addresses. Only super user is allowed 1989 * to do this. 1990 */ 1991 if (suser(td)) 1992 { 1993 error = EACCES; 1994 break; 1995 } 1996 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 1997 error = EINVAL; 1998 break; 1999 } 2000 2001 /* 2002 * If the interface is specified, validate it. 2003 */ 2004 if (mreq->ipv6mr_interface < 0 2005 || if_index < mreq->ipv6mr_interface) { 2006 error = ENXIO; /* XXX EINVAL? */ 2007 break; 2008 } 2009 /* 2010 * If no interface was explicitly specified, choose an 2011 * appropriate one according to the given multicast address. 2012 */ 2013 if (mreq->ipv6mr_interface == 0) { 2014 /* 2015 * If the multicast address is in node-local scope, 2016 * the interface should be a loopback interface. 2017 * Otherwise, look up the routing table for the 2018 * address, and choose the outgoing interface. 2019 * XXX: is it a good approach? 2020 */ 2021 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) { 2022 ifp = &loif[0]; 2023 } else { 2024 ro.ro_rt = NULL; 2025 dst = (struct sockaddr_in6 *)&ro.ro_dst; 2026 bzero(dst, sizeof(*dst)); 2027 dst->sin6_len = sizeof(struct sockaddr_in6); 2028 dst->sin6_family = AF_INET6; 2029 dst->sin6_addr = mreq->ipv6mr_multiaddr; 2030 rtalloc((struct route *)&ro); 2031 if (ro.ro_rt == NULL) { 2032 error = EADDRNOTAVAIL; 2033 break; 2034 } 2035 ifp = ro.ro_rt->rt_ifp; 2036 rtfree(ro.ro_rt); 2037 } 2038 } else 2039 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2040 2041 /* 2042 * See if we found an interface, and confirm that it 2043 * supports multicast 2044 */ 2045 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2046 error = EADDRNOTAVAIL; 2047 break; 2048 } 2049 /* 2050 * Put interface index into the multicast address, 2051 * if the address has link-local scope. 2052 */ 2053 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2054 mreq->ipv6mr_multiaddr.s6_addr16[1] 2055 = htons(mreq->ipv6mr_interface); 2056 } 2057 /* 2058 * See if the membership already exists. 2059 */ 2060 for (imm = im6o->im6o_memberships.lh_first; 2061 imm != NULL; imm = imm->i6mm_chain.le_next) 2062 if (imm->i6mm_maddr->in6m_ifp == ifp && 2063 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2064 &mreq->ipv6mr_multiaddr)) 2065 break; 2066 if (imm != NULL) { 2067 error = EADDRINUSE; 2068 break; 2069 } 2070 /* 2071 * Everything looks good; add a new record to the multicast 2072 * address list for the given interface. 2073 */ 2074 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK); 2075 if (imm == NULL) { 2076 error = ENOBUFS; 2077 break; 2078 } 2079 if ((imm->i6mm_maddr = 2080 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) { 2081 free(imm, M_IPMADDR); 2082 break; 2083 } 2084 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2085 break; 2086 2087 case IPV6_LEAVE_GROUP: 2088 /* 2089 * Drop a multicast group membership. 2090 * Group must be a valid IP6 multicast address. 2091 */ 2092 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2093 error = EINVAL; 2094 break; 2095 } 2096 mreq = mtod(m, struct ipv6_mreq *); 2097 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2098 if (suser(td)) { 2099 error = EACCES; 2100 break; 2101 } 2102 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2103 error = EINVAL; 2104 break; 2105 } 2106 /* 2107 * If an interface address was specified, get a pointer 2108 * to its ifnet structure. 2109 */ 2110 if (mreq->ipv6mr_interface < 0 2111 || if_index < mreq->ipv6mr_interface) { 2112 error = ENXIO; /* XXX EINVAL? */ 2113 break; 2114 } 2115 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2116 /* 2117 * Put interface index into the multicast address, 2118 * if the address has link-local scope. 2119 */ 2120 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2121 mreq->ipv6mr_multiaddr.s6_addr16[1] 2122 = htons(mreq->ipv6mr_interface); 2123 } 2124 /* 2125 * Find the membership in the membership list. 2126 */ 2127 for (imm = im6o->im6o_memberships.lh_first; 2128 imm != NULL; imm = imm->i6mm_chain.le_next) { 2129 if ((ifp == NULL || 2130 imm->i6mm_maddr->in6m_ifp == ifp) && 2131 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2132 &mreq->ipv6mr_multiaddr)) 2133 break; 2134 } 2135 if (imm == NULL) { 2136 /* Unable to resolve interface */ 2137 error = EADDRNOTAVAIL; 2138 break; 2139 } 2140 /* 2141 * Give up the multicast address record to which the 2142 * membership points. 2143 */ 2144 LIST_REMOVE(imm, i6mm_chain); 2145 in6_delmulti(imm->i6mm_maddr); 2146 free(imm, M_IPMADDR); 2147 break; 2148 2149 default: 2150 error = EOPNOTSUPP; 2151 break; 2152 } 2153 2154 /* 2155 * If all options have default values, no need to keep the mbuf. 2156 */ 2157 if (im6o->im6o_multicast_ifp == NULL && 2158 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2159 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2160 im6o->im6o_memberships.lh_first == NULL) { 2161 free(*im6op, M_IPMOPTS); 2162 *im6op = NULL; 2163 } 2164 2165 return(error); 2166} 2167 2168/* 2169 * Return the IP6 multicast options in response to user getsockopt(). 2170 */ 2171static int 2172ip6_getmoptions(optname, im6o, mp) 2173 int optname; 2174 struct ip6_moptions *im6o; 2175 struct mbuf **mp; 2176{ 2177 u_int *hlim, *loop, *ifindex; 2178 2179 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */ 2180 2181 switch (optname) { 2182 2183 case IPV6_MULTICAST_IF: 2184 ifindex = mtod(*mp, u_int *); 2185 (*mp)->m_len = sizeof(u_int); 2186 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2187 *ifindex = 0; 2188 else 2189 *ifindex = im6o->im6o_multicast_ifp->if_index; 2190 return(0); 2191 2192 case IPV6_MULTICAST_HOPS: 2193 hlim = mtod(*mp, u_int *); 2194 (*mp)->m_len = sizeof(u_int); 2195 if (im6o == NULL) 2196 *hlim = ip6_defmcasthlim; 2197 else 2198 *hlim = im6o->im6o_multicast_hlim; 2199 return(0); 2200 2201 case IPV6_MULTICAST_LOOP: 2202 loop = mtod(*mp, u_int *); 2203 (*mp)->m_len = sizeof(u_int); 2204 if (im6o == NULL) 2205 *loop = ip6_defmcasthlim; 2206 else 2207 *loop = im6o->im6o_multicast_loop; 2208 return(0); 2209 2210 default: 2211 return(EOPNOTSUPP); 2212 } 2213} 2214 2215/* 2216 * Discard the IP6 multicast options. 2217 */ 2218void 2219ip6_freemoptions(im6o) 2220 struct ip6_moptions *im6o; 2221{ 2222 struct in6_multi_mship *imm; 2223 2224 if (im6o == NULL) 2225 return; 2226 2227 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2228 LIST_REMOVE(imm, i6mm_chain); 2229 if (imm->i6mm_maddr) 2230 in6_delmulti(imm->i6mm_maddr); 2231 free(imm, M_IPMADDR); 2232 } 2233 free(im6o, M_IPMOPTS); 2234} 2235 2236/* 2237 * Set IPv6 outgoing packet options based on advanced API. 2238 */ 2239int 2240ip6_setpktoptions(control, opt, priv, needcopy) 2241 struct mbuf *control; 2242 struct ip6_pktopts *opt; 2243 int priv, needcopy; 2244{ 2245 struct cmsghdr *cm = 0; 2246 2247 if (control == 0 || opt == 0) 2248 return(EINVAL); 2249 2250 init_ip6pktopts(opt); 2251 2252 /* 2253 * XXX: Currently, we assume all the optional information is stored 2254 * in a single mbuf. 2255 */ 2256 if (control->m_next) 2257 return(EINVAL); 2258 2259 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2260 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2261 cm = mtod(control, struct cmsghdr *); 2262 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2263 return(EINVAL); 2264 if (cm->cmsg_level != IPPROTO_IPV6) 2265 continue; 2266 2267 /* 2268 * XXX should check if RFC2292 API is mixed with 2292bis API 2269 */ 2270 switch (cm->cmsg_type) { 2271 case IPV6_PKTINFO: 2272 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo))) 2273 return(EINVAL); 2274 if (needcopy) { 2275 /* XXX: Is it really WAITOK? */ 2276 opt->ip6po_pktinfo = 2277 malloc(sizeof(struct in6_pktinfo), 2278 M_IP6OPT, M_WAITOK); 2279 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo, 2280 sizeof(struct in6_pktinfo)); 2281 } else 2282 opt->ip6po_pktinfo = 2283 (struct in6_pktinfo *)CMSG_DATA(cm); 2284 if (opt->ip6po_pktinfo->ipi6_ifindex && 2285 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr)) 2286 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] = 2287 htons(opt->ip6po_pktinfo->ipi6_ifindex); 2288 2289 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index 2290 || opt->ip6po_pktinfo->ipi6_ifindex < 0) { 2291 return(ENXIO); 2292 } 2293 2294 /* 2295 * Check if the requested source address is indeed a 2296 * unicast address assigned to the node, and can be 2297 * used as the packet's source address. 2298 */ 2299 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) { 2300 struct in6_ifaddr *ia6; 2301 struct sockaddr_in6 sin6; 2302 2303 bzero(&sin6, sizeof(sin6)); 2304 sin6.sin6_len = sizeof(sin6); 2305 sin6.sin6_family = AF_INET6; 2306 sin6.sin6_addr = 2307 opt->ip6po_pktinfo->ipi6_addr; 2308 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6)); 2309 if (ia6 == NULL || 2310 (ia6->ia6_flags & (IN6_IFF_ANYCAST | 2311 IN6_IFF_NOTREADY)) != 0) 2312 return(EADDRNOTAVAIL); 2313 } 2314 break; 2315 2316 case IPV6_HOPLIMIT: 2317 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) 2318 return(EINVAL); 2319 2320 opt->ip6po_hlim = *(int *)CMSG_DATA(cm); 2321 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255) 2322 return(EINVAL); 2323 break; 2324 2325 case IPV6_NEXTHOP: 2326 if (!priv) 2327 return(EPERM); 2328 2329 if (cm->cmsg_len < sizeof(u_char) || 2330 /* check if cmsg_len is large enough for sa_len */ 2331 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm))) 2332 return(EINVAL); 2333 2334 if (needcopy) { 2335 opt->ip6po_nexthop = 2336 malloc(*CMSG_DATA(cm), 2337 M_IP6OPT, M_WAITOK); 2338 bcopy(CMSG_DATA(cm), 2339 opt->ip6po_nexthop, 2340 *CMSG_DATA(cm)); 2341 } else 2342 opt->ip6po_nexthop = 2343 (struct sockaddr *)CMSG_DATA(cm); 2344 break; 2345 2346 case IPV6_HOPOPTS: 2347 { 2348 struct ip6_hbh *hbh; 2349 int hbhlen; 2350 2351 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh))) 2352 return(EINVAL); 2353 hbh = (struct ip6_hbh *)CMSG_DATA(cm); 2354 hbhlen = (hbh->ip6h_len + 1) << 3; 2355 if (cm->cmsg_len != CMSG_LEN(hbhlen)) 2356 return(EINVAL); 2357 2358 if (needcopy) { 2359 opt->ip6po_hbh = 2360 malloc(hbhlen, M_IP6OPT, M_WAITOK); 2361 bcopy(hbh, opt->ip6po_hbh, hbhlen); 2362 } else 2363 opt->ip6po_hbh = hbh; 2364 break; 2365 } 2366 2367 case IPV6_DSTOPTS: 2368 { 2369 struct ip6_dest *dest, **newdest; 2370 int destlen; 2371 2372 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest))) 2373 return(EINVAL); 2374 dest = (struct ip6_dest *)CMSG_DATA(cm); 2375 destlen = (dest->ip6d_len + 1) << 3; 2376 if (cm->cmsg_len != CMSG_LEN(destlen)) 2377 return(EINVAL); 2378 2379 /* 2380 * The old advacned API is ambiguous on this 2381 * point. Our approach is to determine the 2382 * position based according to the existence 2383 * of a routing header. Note, however, that 2384 * this depends on the order of the extension 2385 * headers in the ancillary data; the 1st part 2386 * of the destination options header must 2387 * appear before the routing header in the 2388 * ancillary data, too. 2389 * RFC2292bis solved the ambiguity by 2390 * introducing separate cmsg types. 2391 */ 2392 if (opt->ip6po_rthdr == NULL) 2393 newdest = &opt->ip6po_dest1; 2394 else 2395 newdest = &opt->ip6po_dest2; 2396 2397 if (needcopy) { 2398 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK); 2399 bcopy(dest, *newdest, destlen); 2400 } else 2401 *newdest = dest; 2402 2403 break; 2404 } 2405 2406 case IPV6_RTHDR: 2407 { 2408 struct ip6_rthdr *rth; 2409 int rthlen; 2410 2411 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr))) 2412 return(EINVAL); 2413 rth = (struct ip6_rthdr *)CMSG_DATA(cm); 2414 rthlen = (rth->ip6r_len + 1) << 3; 2415 if (cm->cmsg_len != CMSG_LEN(rthlen)) 2416 return(EINVAL); 2417 2418 switch (rth->ip6r_type) { 2419 case IPV6_RTHDR_TYPE_0: 2420 /* must contain one addr */ 2421 if (rth->ip6r_len == 0) 2422 return(EINVAL); 2423 /* length must be even */ 2424 if (rth->ip6r_len % 2) 2425 return(EINVAL); 2426 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 2427 return(EINVAL); 2428 break; 2429 default: 2430 return(EINVAL); /* not supported */ 2431 } 2432 2433 if (needcopy) { 2434 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, 2435 M_WAITOK); 2436 bcopy(rth, opt->ip6po_rthdr, rthlen); 2437 } else 2438 opt->ip6po_rthdr = rth; 2439 2440 break; 2441 } 2442 2443 default: 2444 return(ENOPROTOOPT); 2445 } 2446 } 2447 2448 return(0); 2449} 2450 2451/* 2452 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 2453 * packet to the input queue of a specified interface. Note that this 2454 * calls the output routine of the loopback "driver", but with an interface 2455 * pointer that might NOT be &loif -- easier than replicating that code here. 2456 */ 2457void 2458ip6_mloopback(ifp, m, dst) 2459 struct ifnet *ifp; 2460 struct mbuf *m; 2461 struct sockaddr_in6 *dst; 2462{ 2463 struct mbuf *copym; 2464 struct ip6_hdr *ip6; 2465 2466 copym = m_copy(m, 0, M_COPYALL); 2467 if (copym == NULL) 2468 return; 2469 2470 /* 2471 * Make sure to deep-copy IPv6 header portion in case the data 2472 * is in an mbuf cluster, so that we can safely override the IPv6 2473 * header portion later. 2474 */ 2475 if ((copym->m_flags & M_EXT) != 0 || 2476 copym->m_len < sizeof(struct ip6_hdr)) { 2477 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 2478 if (copym == NULL) 2479 return; 2480 } 2481 2482#ifdef DIAGNOSTIC 2483 if (copym->m_len < sizeof(*ip6)) { 2484 m_freem(copym); 2485 return; 2486 } 2487#endif 2488 2489 ip6 = mtod(copym, struct ip6_hdr *); 2490#ifndef SCOPEDROUTING 2491 /* 2492 * clear embedded scope identifiers if necessary. 2493 * in6_clearscope will touch the addresses only when necessary. 2494 */ 2495 in6_clearscope(&ip6->ip6_src); 2496 in6_clearscope(&ip6->ip6_dst); 2497#endif 2498 2499 (void)if_simloop(ifp, copym, dst->sin6_family, 0); 2500} 2501 2502/* 2503 * Chop IPv6 header off from the payload. 2504 */ 2505static int 2506ip6_splithdr(m, exthdrs) 2507 struct mbuf *m; 2508 struct ip6_exthdrs *exthdrs; 2509{ 2510 struct mbuf *mh; 2511 struct ip6_hdr *ip6; 2512 2513 ip6 = mtod(m, struct ip6_hdr *); 2514 if (m->m_len > sizeof(*ip6)) { 2515 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 2516 if (mh == 0) { 2517 m_freem(m); 2518 return ENOBUFS; 2519 } 2520 M_COPY_PKTHDR(mh, m); 2521 MH_ALIGN(mh, sizeof(*ip6)); 2522 m->m_flags &= ~M_PKTHDR; 2523 m->m_len -= sizeof(*ip6); 2524 m->m_data += sizeof(*ip6); 2525 mh->m_next = m; 2526 m = mh; 2527 m->m_len = sizeof(*ip6); 2528 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 2529 } 2530 exthdrs->ip6e_ip6 = m; 2531 return 0; 2532} 2533 2534/* 2535 * Compute IPv6 extension header length. 2536 */ 2537int 2538ip6_optlen(in6p) 2539 struct in6pcb *in6p; 2540{ 2541 int len; 2542 2543 if (!in6p->in6p_outputopts) 2544 return 0; 2545 2546 len = 0; 2547#define elen(x) \ 2548 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 2549 2550 len += elen(in6p->in6p_outputopts->ip6po_hbh); 2551 if (in6p->in6p_outputopts->ip6po_rthdr) 2552 /* dest1 is valid with rthdr only */ 2553 len += elen(in6p->in6p_outputopts->ip6po_dest1); 2554 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 2555 len += elen(in6p->in6p_outputopts->ip6po_dest2); 2556 return len; 2557#undef elen 2558}
| 180 ip6 = mtod(m, struct ip6_hdr *); 181#endif /* IPSEC */ 182 183#define MAKE_EXTHDR(hp, mp) \ 184 do { \ 185 if (hp) { \ 186 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 187 error = ip6_copyexthdr((mp), (caddr_t)(hp), \ 188 ((eh)->ip6e_len + 1) << 3); \ 189 if (error) \ 190 goto freehdrs; \ 191 } \ 192 } while (0) 193 194 bzero(&exthdrs, sizeof(exthdrs)); 195 196 if (opt) { 197 /* Hop-by-Hop options header */ 198 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 199 /* Destination options header(1st part) */ 200 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 201 /* Routing header */ 202 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 203 /* Destination options header(2nd part) */ 204 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 205 } 206 207#ifdef IPSEC 208 /* get a security policy for this packet */ 209 if (so == NULL) 210 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error); 211 else 212 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 213 214 if (sp == NULL) { 215 ipsec6stat.out_inval++; 216 goto freehdrs; 217 } 218 219 error = 0; 220 221 /* check policy */ 222 switch (sp->policy) { 223 case IPSEC_POLICY_DISCARD: 224 /* 225 * This packet is just discarded. 226 */ 227 ipsec6stat.out_polvio++; 228 goto freehdrs; 229 230 case IPSEC_POLICY_BYPASS: 231 case IPSEC_POLICY_NONE: 232 /* no need to do IPsec. */ 233 needipsec = 0; 234 break; 235 236 case IPSEC_POLICY_IPSEC: 237 if (sp->req == NULL) { 238 /* acquire a policy */ 239 error = key_spdacquire(sp); 240 goto freehdrs; 241 } 242 needipsec = 1; 243 break; 244 245 case IPSEC_POLICY_ENTRUST: 246 default: 247 printf("ip6_output: Invalid policy found. %d\n", sp->policy); 248 } 249#endif /* IPSEC */ 250 251 /* 252 * Calculate the total length of the extension header chain. 253 * Keep the length of the unfragmentable part for fragmentation. 254 */ 255 optlen = 0; 256 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len; 257 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len; 258 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len; 259 unfragpartlen = optlen + sizeof(struct ip6_hdr); 260 /* NOTE: we don't add AH/ESP length here. do that later. */ 261 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len; 262 263 /* 264 * If we need IPsec, or there is at least one extension header, 265 * separate IP6 header from the payload. 266 */ 267 if ((needipsec || optlen) && !hdrsplit) { 268 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 269 m = NULL; 270 goto freehdrs; 271 } 272 m = exthdrs.ip6e_ip6; 273 hdrsplit++; 274 } 275 276 /* adjust pointer */ 277 ip6 = mtod(m, struct ip6_hdr *); 278 279 /* adjust mbuf packet header length */ 280 m->m_pkthdr.len += optlen; 281 plen = m->m_pkthdr.len - sizeof(*ip6); 282 283 /* If this is a jumbo payload, insert a jumbo payload option. */ 284 if (plen > IPV6_MAXPACKET) { 285 if (!hdrsplit) { 286 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 287 m = NULL; 288 goto freehdrs; 289 } 290 m = exthdrs.ip6e_ip6; 291 hdrsplit++; 292 } 293 /* adjust pointer */ 294 ip6 = mtod(m, struct ip6_hdr *); 295 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 296 goto freehdrs; 297 ip6->ip6_plen = 0; 298 } else 299 ip6->ip6_plen = htons(plen); 300 301 /* 302 * Concatenate headers and fill in next header fields. 303 * Here we have, on "m" 304 * IPv6 payload 305 * and we insert headers accordingly. Finally, we should be getting: 306 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 307 * 308 * during the header composing process, "m" points to IPv6 header. 309 * "mprev" points to an extension header prior to esp. 310 */ 311 { 312 u_char *nexthdrp = &ip6->ip6_nxt; 313 struct mbuf *mprev = m; 314 315 /* 316 * we treat dest2 specially. this makes IPsec processing 317 * much easier. the goal here is to make mprev point the 318 * mbuf prior to dest2. 319 * 320 * result: IPv6 dest2 payload 321 * m and mprev will point to IPv6 header. 322 */ 323 if (exthdrs.ip6e_dest2) { 324 if (!hdrsplit) 325 panic("assumption failed: hdr not split"); 326 exthdrs.ip6e_dest2->m_next = m->m_next; 327 m->m_next = exthdrs.ip6e_dest2; 328 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 329 ip6->ip6_nxt = IPPROTO_DSTOPTS; 330 } 331 332#define MAKE_CHAIN(m, mp, p, i)\ 333 do {\ 334 if (m) {\ 335 if (!hdrsplit) \ 336 panic("assumption failed: hdr not split"); \ 337 *mtod((m), u_char *) = *(p);\ 338 *(p) = (i);\ 339 p = mtod((m), u_char *);\ 340 (m)->m_next = (mp)->m_next;\ 341 (mp)->m_next = (m);\ 342 (mp) = (m);\ 343 }\ 344 } while (0) 345 /* 346 * result: IPv6 hbh dest1 rthdr dest2 payload 347 * m will point to IPv6 header. mprev will point to the 348 * extension header prior to dest2 (rthdr in the above case). 349 */ 350 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, 351 nexthdrp, IPPROTO_HOPOPTS); 352 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, 353 nexthdrp, IPPROTO_DSTOPTS); 354 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, 355 nexthdrp, IPPROTO_ROUTING); 356 357#ifdef IPSEC 358 if (!needipsec) 359 goto skip_ipsec2; 360 361 /* 362 * pointers after IPsec headers are not valid any more. 363 * other pointers need a great care too. 364 * (IPsec routines should not mangle mbufs prior to AH/ESP) 365 */ 366 exthdrs.ip6e_dest2 = NULL; 367 368 { 369 struct ip6_rthdr *rh = NULL; 370 int segleft_org = 0; 371 struct ipsec_output_state state; 372 373 if (exthdrs.ip6e_rthdr) { 374 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 375 segleft_org = rh->ip6r_segleft; 376 rh->ip6r_segleft = 0; 377 } 378 379 bzero(&state, sizeof(state)); 380 state.m = m; 381 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags, 382 &needipsectun); 383 m = state.m; 384 if (error) { 385 /* mbuf is already reclaimed in ipsec6_output_trans. */ 386 m = NULL; 387 switch (error) { 388 case EHOSTUNREACH: 389 case ENETUNREACH: 390 case EMSGSIZE: 391 case ENOBUFS: 392 case ENOMEM: 393 break; 394 default: 395 printf("ip6_output (ipsec): error code %d\n", error); 396 /* fall through */ 397 case ENOENT: 398 /* don't show these error codes to the user */ 399 error = 0; 400 break; 401 } 402 goto bad; 403 } 404 if (exthdrs.ip6e_rthdr) { 405 /* ah6_output doesn't modify mbuf chain */ 406 rh->ip6r_segleft = segleft_org; 407 } 408 } 409skip_ipsec2:; 410#endif 411 } 412 413 /* 414 * If there is a routing header, replace destination address field 415 * with the first hop of the routing header. 416 */ 417 if (exthdrs.ip6e_rthdr) { 418 struct ip6_rthdr *rh = 419 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, 420 struct ip6_rthdr *)); 421 struct ip6_rthdr0 *rh0; 422 423 finaldst = ip6->ip6_dst; 424 switch (rh->ip6r_type) { 425 case IPV6_RTHDR_TYPE_0: 426 rh0 = (struct ip6_rthdr0 *)rh; 427 ip6->ip6_dst = rh0->ip6r0_addr[0]; 428 bcopy((caddr_t)&rh0->ip6r0_addr[1], 429 (caddr_t)&rh0->ip6r0_addr[0], 430 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1) 431 ); 432 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst; 433 break; 434 default: /* is it possible? */ 435 error = EINVAL; 436 goto bad; 437 } 438 } 439 440 /* Source address validation */ 441 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 442 (flags & IPV6_DADOUTPUT) == 0) { 443 error = EOPNOTSUPP; 444 ip6stat.ip6s_badscope++; 445 goto bad; 446 } 447 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 448 error = EOPNOTSUPP; 449 ip6stat.ip6s_badscope++; 450 goto bad; 451 } 452 453 ip6stat.ip6s_localout++; 454 455 /* 456 * Route packet. 457 */ 458 if (ro == 0) { 459 ro = &ip6route; 460 bzero((caddr_t)ro, sizeof(*ro)); 461 } 462 ro_pmtu = ro; 463 if (opt && opt->ip6po_rthdr) 464 ro = &opt->ip6po_route; 465 dst = (struct sockaddr_in6 *)&ro->ro_dst; 466 /* 467 * If there is a cached route, 468 * check that it is to the same destination 469 * and is still up. If not, free it and try again. 470 */ 471 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 472 dst->sin6_family != AF_INET6 || 473 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) { 474 RTFREE(ro->ro_rt); 475 ro->ro_rt = (struct rtentry *)0; 476 } 477 if (ro->ro_rt == 0) { 478 bzero(dst, sizeof(*dst)); 479 dst->sin6_family = AF_INET6; 480 dst->sin6_len = sizeof(struct sockaddr_in6); 481 dst->sin6_addr = ip6->ip6_dst; 482#ifdef SCOPEDROUTING 483 /* XXX: sin6_scope_id should already be fixed at this point */ 484 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr)) 485 dst->sin6_scope_id = ntohs(dst->sin6_addr.s6_addr16[1]); 486#endif 487 } 488#ifdef IPSEC 489 if (needipsec && needipsectun) { 490 struct ipsec_output_state state; 491 492 /* 493 * All the extension headers will become inaccessible 494 * (since they can be encrypted). 495 * Don't panic, we need no more updates to extension headers 496 * on inner IPv6 packet (since they are now encapsulated). 497 * 498 * IPv6 [ESP|AH] IPv6 [extension headers] payload 499 */ 500 bzero(&exthdrs, sizeof(exthdrs)); 501 exthdrs.ip6e_ip6 = m; 502 503 bzero(&state, sizeof(state)); 504 state.m = m; 505 state.ro = (struct route *)ro; 506 state.dst = (struct sockaddr *)dst; 507 508 error = ipsec6_output_tunnel(&state, sp, flags); 509 510 m = state.m; 511 ro = (struct route_in6 *)state.ro; 512 dst = (struct sockaddr_in6 *)state.dst; 513 if (error) { 514 /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 515 m0 = m = NULL; 516 m = NULL; 517 switch (error) { 518 case EHOSTUNREACH: 519 case ENETUNREACH: 520 case EMSGSIZE: 521 case ENOBUFS: 522 case ENOMEM: 523 break; 524 default: 525 printf("ip6_output (ipsec): error code %d\n", error); 526 /* fall through */ 527 case ENOENT: 528 /* don't show these error codes to the user */ 529 error = 0; 530 break; 531 } 532 goto bad; 533 } 534 535 exthdrs.ip6e_ip6 = m; 536 } 537#endif /* IPSEC */ 538 539 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 540 /* Unicast */ 541 542#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 543#define sin6tosa(sin6) ((struct sockaddr *)(sin6)) 544 /* xxx 545 * interface selection comes here 546 * if an interface is specified from an upper layer, 547 * ifp must point it. 548 */ 549 if (ro->ro_rt == 0) { 550 /* 551 * non-bsdi always clone routes, if parent is 552 * PRF_CLONING. 553 */ 554 rtalloc((struct route *)ro); 555 } 556 if (ro->ro_rt == 0) { 557 ip6stat.ip6s_noroute++; 558 error = EHOSTUNREACH; 559 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */ 560 goto bad; 561 } 562 ia = ifatoia6(ro->ro_rt->rt_ifa); 563 ifp = ro->ro_rt->rt_ifp; 564 ro->ro_rt->rt_use++; 565 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 566 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway; 567 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 568 569 in6_ifstat_inc(ifp, ifs6_out_request); 570 571 /* 572 * Check if the outgoing interface conflicts with 573 * the interface specified by ifi6_ifindex (if specified). 574 * Note that loopback interface is always okay. 575 * (this may happen when we are sending a packet to one of 576 * our own addresses.) 577 */ 578 if (opt && opt->ip6po_pktinfo 579 && opt->ip6po_pktinfo->ipi6_ifindex) { 580 if (!(ifp->if_flags & IFF_LOOPBACK) 581 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) { 582 ip6stat.ip6s_noroute++; 583 in6_ifstat_inc(ifp, ifs6_out_discard); 584 error = EHOSTUNREACH; 585 goto bad; 586 } 587 } 588 589 if (opt && opt->ip6po_hlim != -1) 590 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 591 } else { 592 /* Multicast */ 593 struct in6_multi *in6m; 594 595 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 596 597 /* 598 * See if the caller provided any multicast options 599 */ 600 ifp = NULL; 601 if (im6o != NULL) { 602 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 603 if (im6o->im6o_multicast_ifp != NULL) 604 ifp = im6o->im6o_multicast_ifp; 605 } else 606 ip6->ip6_hlim = ip6_defmcasthlim; 607 608 /* 609 * See if the caller provided the outgoing interface 610 * as an ancillary data. 611 * Boundary check for ifindex is assumed to be already done. 612 */ 613 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex) 614 ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex); 615 616 /* 617 * If the destination is a node-local scope multicast, 618 * the packet should be loop-backed only. 619 */ 620 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) { 621 /* 622 * If the outgoing interface is already specified, 623 * it should be a loopback interface. 624 */ 625 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) { 626 ip6stat.ip6s_badscope++; 627 error = ENETUNREACH; /* XXX: better error? */ 628 /* XXX correct ifp? */ 629 in6_ifstat_inc(ifp, ifs6_out_discard); 630 goto bad; 631 } else { 632 ifp = &loif[0]; 633 } 634 } 635 636 if (opt && opt->ip6po_hlim != -1) 637 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 638 639 /* 640 * If caller did not provide an interface lookup a 641 * default in the routing table. This is either a 642 * default for the speicfied group (i.e. a host 643 * route), or a multicast default (a route for the 644 * ``net'' ff00::/8). 645 */ 646 if (ifp == NULL) { 647 if (ro->ro_rt == 0) { 648 ro->ro_rt = rtalloc1((struct sockaddr *) 649 &ro->ro_dst, 0, 0UL); 650 } 651 if (ro->ro_rt == 0) { 652 ip6stat.ip6s_noroute++; 653 error = EHOSTUNREACH; 654 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */ 655 goto bad; 656 } 657 ia = ifatoia6(ro->ro_rt->rt_ifa); 658 ifp = ro->ro_rt->rt_ifp; 659 ro->ro_rt->rt_use++; 660 } 661 662 if ((flags & IPV6_FORWARDING) == 0) 663 in6_ifstat_inc(ifp, ifs6_out_request); 664 in6_ifstat_inc(ifp, ifs6_out_mcast); 665 666 /* 667 * Confirm that the outgoing interface supports multicast. 668 */ 669 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 670 ip6stat.ip6s_noroute++; 671 in6_ifstat_inc(ifp, ifs6_out_discard); 672 error = ENETUNREACH; 673 goto bad; 674 } 675 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); 676 if (in6m != NULL && 677 (im6o == NULL || im6o->im6o_multicast_loop)) { 678 /* 679 * If we belong to the destination multicast group 680 * on the outgoing interface, and the caller did not 681 * forbid loopback, loop back a copy. 682 */ 683 ip6_mloopback(ifp, m, dst); 684 } else { 685 /* 686 * If we are acting as a multicast router, perform 687 * multicast forwarding as if the packet had just 688 * arrived on the interface to which we are about 689 * to send. The multicast forwarding function 690 * recursively calls this function, using the 691 * IPV6_FORWARDING flag to prevent infinite recursion. 692 * 693 * Multicasts that are looped back by ip6_mloopback(), 694 * above, will be forwarded by the ip6_input() routine, 695 * if necessary. 696 */ 697 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 698 if (ip6_mforward(ip6, ifp, m) != 0) { 699 m_freem(m); 700 goto done; 701 } 702 } 703 } 704 /* 705 * Multicasts with a hoplimit of zero may be looped back, 706 * above, but must not be transmitted on a network. 707 * Also, multicasts addressed to the loopback interface 708 * are not sent -- the above call to ip6_mloopback() will 709 * loop back a copy if this host actually belongs to the 710 * destination group on the loopback interface. 711 */ 712 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) { 713 m_freem(m); 714 goto done; 715 } 716 } 717 718 /* 719 * Fill the outgoing inteface to tell the upper layer 720 * to increment per-interface statistics. 721 */ 722 if (ifpp) 723 *ifpp = ifp; 724 725 /* 726 * Determine path MTU. 727 */ 728 if (ro_pmtu != ro) { 729 /* The first hop and the final destination may differ. */ 730 struct sockaddr_in6 *sin6_fin = 731 (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 732 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 733 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr, 734 &finaldst))) { 735 RTFREE(ro_pmtu->ro_rt); 736 ro_pmtu->ro_rt = (struct rtentry *)0; 737 } 738 if (ro_pmtu->ro_rt == 0) { 739 bzero(sin6_fin, sizeof(*sin6_fin)); 740 sin6_fin->sin6_family = AF_INET6; 741 sin6_fin->sin6_len = sizeof(struct sockaddr_in6); 742 sin6_fin->sin6_addr = finaldst; 743 744 rtalloc((struct route *)ro_pmtu); 745 } 746 } 747 if (ro_pmtu->ro_rt != NULL) { 748 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu; 749 750 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu; 751 if (mtu > ifmtu || mtu == 0) { 752 /* 753 * The MTU on the route is larger than the MTU on 754 * the interface! This shouldn't happen, unless the 755 * MTU of the interface has been changed after the 756 * interface was brought up. Change the MTU in the 757 * route to match the interface MTU (as long as the 758 * field isn't locked). 759 * 760 * if MTU on the route is 0, we need to fix the MTU. 761 * this case happens with path MTU discovery timeouts. 762 */ 763 mtu = ifmtu; 764 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 765 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */ 766 } 767 } else { 768 mtu = nd_ifinfo[ifp->if_index].linkmtu; 769 } 770 771 /* 772 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting 773 */ 774 if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU) 775 mtu = IPV6_MMTU; 776 777 /* Fake scoped addresses */ 778 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 779 /* 780 * If source or destination address is a scoped address, and 781 * the packet is going to be sent to a loopback interface, 782 * we should keep the original interface. 783 */ 784 785 /* 786 * XXX: this is a very experimental and temporary solution. 787 * We eventually have sockaddr_in6 and use the sin6_scope_id 788 * field of the structure here. 789 * We rely on the consistency between two scope zone ids 790 * of source and destination, which should already be assured. 791 * Larger scopes than link will be supported in the future. 792 */ 793 origifp = NULL; 794 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 795 origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1])); 796 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 797 origifp = ifnet_byindex(ntohs(ip6->ip6_dst.s6_addr16[1])); 798 /* 799 * XXX: origifp can be NULL even in those two cases above. 800 * For example, if we remove the (only) link-local address 801 * from the loopback interface, and try to send a link-local 802 * address without link-id information. Then the source 803 * address is ::1, and the destination address is the 804 * link-local address with its s6_addr16[1] being zero. 805 * What is worse, if the packet goes to the loopback interface 806 * by a default rejected route, the null pointer would be 807 * passed to looutput, and the kernel would hang. 808 * The following last resort would prevent such disaster. 809 */ 810 if (origifp == NULL) 811 origifp = ifp; 812 } 813 else 814 origifp = ifp; 815#ifndef SCOPEDROUTING 816 /* 817 * clear embedded scope identifiers if necessary. 818 * in6_clearscope will touch the addresses only when necessary. 819 */ 820 in6_clearscope(&ip6->ip6_src); 821 in6_clearscope(&ip6->ip6_dst); 822#endif 823 824 /* 825 * Check with the firewall... 826 */ 827 if (ip6_fw_enable && ip6_fw_chk_ptr) { 828 u_short port = 0; 829 m->m_pkthdr.rcvif = NULL; /* XXX */ 830 /* If ipfw says divert, we have to just drop packet */ 831 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) { 832 m_freem(m); 833 goto done; 834 } 835 if (!m) { 836 error = EACCES; 837 goto done; 838 } 839 } 840 841 /* 842 * If the outgoing packet contains a hop-by-hop options header, 843 * it must be examined and processed even by the source node. 844 * (RFC 2460, section 4.) 845 */ 846 if (exthdrs.ip6e_hbh) { 847 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 848 u_int32_t dummy1; /* XXX unused */ 849 u_int32_t dummy2; /* XXX unused */ 850 851#ifdef DIAGNOSTIC 852 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) 853 panic("ip6e_hbh is not continuous"); 854#endif 855 /* 856 * XXX: if we have to send an ICMPv6 error to the sender, 857 * we need the M_LOOP flag since icmp6_error() expects 858 * the IPv6 and the hop-by-hop options header are 859 * continuous unless the flag is set. 860 */ 861 m->m_flags |= M_LOOP; 862 m->m_pkthdr.rcvif = ifp; 863 if (ip6_process_hopopts(m, 864 (u_int8_t *)(hbh + 1), 865 ((hbh->ip6h_len + 1) << 3) - 866 sizeof(struct ip6_hbh), 867 &dummy1, &dummy2) < 0) { 868 /* m was already freed at this point */ 869 error = EINVAL;/* better error? */ 870 goto done; 871 } 872 m->m_flags &= ~M_LOOP; /* XXX */ 873 m->m_pkthdr.rcvif = NULL; 874 } 875 876#ifdef PFIL_HOOKS 877 /* 878 * Run through list of hooks for output packets. 879 */ 880 m1 = m; 881 pfh = pfil_hook_get(PFIL_OUT, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh); 882 for (; pfh; pfh = pfh->pfil_link.tqe_next) 883 if (pfh->pfil_func) { 884 rv = pfh->pfil_func(ip6, sizeof(*ip6), ifp, 1, &m1); 885 if (rv) { 886 error = EHOSTUNREACH; 887 goto done; 888 } 889 m = m1; 890 if (m == NULL) 891 goto done; 892 ip6 = mtod(m, struct ip6_hdr *); 893 } 894#endif /* PFIL_HOOKS */ 895 /* 896 * Send the packet to the outgoing interface. 897 * If necessary, do IPv6 fragmentation before sending. 898 */ 899 tlen = m->m_pkthdr.len; 900 if (tlen <= mtu 901#ifdef notyet 902 /* 903 * On any link that cannot convey a 1280-octet packet in one piece, 904 * link-specific fragmentation and reassembly must be provided at 905 * a layer below IPv6. [RFC 2460, sec.5] 906 * Thus if the interface has ability of link-level fragmentation, 907 * we can just send the packet even if the packet size is 908 * larger than the link's MTU. 909 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet... 910 */ 911 912 || ifp->if_flags & IFF_FRAGMENTABLE 913#endif 914 ) 915 { 916 /* Record statistics for this interface address. */ 917 if (ia && !(flags & IPV6_FORWARDING)) { 918 ia->ia_ifa.if_opackets++; 919 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 920 } 921#ifdef IPSEC 922 /* clean ipsec history once it goes out of the node */ 923 ipsec_delaux(m); 924#endif 925 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 926 goto done; 927 } else if (mtu < IPV6_MMTU) { 928 /* 929 * note that path MTU is never less than IPV6_MMTU 930 * (see icmp6_input). 931 */ 932 error = EMSGSIZE; 933 in6_ifstat_inc(ifp, ifs6_out_fragfail); 934 goto bad; 935 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */ 936 error = EMSGSIZE; 937 in6_ifstat_inc(ifp, ifs6_out_fragfail); 938 goto bad; 939 } else { 940 struct mbuf **mnext, *m_frgpart; 941 struct ip6_frag *ip6f; 942 u_int32_t id = htonl(ip6_id++); 943 u_char nextproto; 944 945 /* 946 * Too large for the destination or interface; 947 * fragment if possible. 948 * Must be able to put at least 8 bytes per fragment. 949 */ 950 hlen = unfragpartlen; 951 if (mtu > IPV6_MAXPACKET) 952 mtu = IPV6_MAXPACKET; 953 954 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 955 if (len < 8) { 956 error = EMSGSIZE; 957 in6_ifstat_inc(ifp, ifs6_out_fragfail); 958 goto bad; 959 } 960 961 mnext = &m->m_nextpkt; 962 963 /* 964 * Change the next header field of the last header in the 965 * unfragmentable part. 966 */ 967 if (exthdrs.ip6e_rthdr) { 968 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 969 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 970 } else if (exthdrs.ip6e_dest1) { 971 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 972 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 973 } else if (exthdrs.ip6e_hbh) { 974 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 975 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 976 } else { 977 nextproto = ip6->ip6_nxt; 978 ip6->ip6_nxt = IPPROTO_FRAGMENT; 979 } 980 981 /* 982 * Loop through length of segment after first fragment, 983 * make new header and copy data of each part and link onto 984 * chain. 985 */ 986 m0 = m; 987 for (off = hlen; off < tlen; off += len) { 988 MGETHDR(m, M_DONTWAIT, MT_HEADER); 989 if (!m) { 990 error = ENOBUFS; 991 ip6stat.ip6s_odropped++; 992 goto sendorfree; 993 } 994 m->m_pkthdr.rcvif = NULL; 995 m->m_flags = m0->m_flags & M_COPYFLAGS; 996 *mnext = m; 997 mnext = &m->m_nextpkt; 998 m->m_data += max_linkhdr; 999 mhip6 = mtod(m, struct ip6_hdr *); 1000 *mhip6 = *ip6; 1001 m->m_len = sizeof(*mhip6); 1002 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 1003 if (error) { 1004 ip6stat.ip6s_odropped++; 1005 goto sendorfree; 1006 } 1007 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7)); 1008 if (off + len >= tlen) 1009 len = tlen - off; 1010 else 1011 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 1012 mhip6->ip6_plen = htons((u_short)(len + hlen + 1013 sizeof(*ip6f) - 1014 sizeof(struct ip6_hdr))); 1015 if ((m_frgpart = m_copy(m0, off, len)) == 0) { 1016 error = ENOBUFS; 1017 ip6stat.ip6s_odropped++; 1018 goto sendorfree; 1019 } 1020 m_cat(m, m_frgpart); 1021 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 1022 m->m_pkthdr.rcvif = (struct ifnet *)0; 1023 ip6f->ip6f_reserved = 0; 1024 ip6f->ip6f_ident = id; 1025 ip6f->ip6f_nxt = nextproto; 1026 ip6stat.ip6s_ofragments++; 1027 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 1028 } 1029 1030 in6_ifstat_inc(ifp, ifs6_out_fragok); 1031 } 1032 1033 /* 1034 * Remove leading garbages. 1035 */ 1036sendorfree: 1037 m = m0->m_nextpkt; 1038 m0->m_nextpkt = 0; 1039 m_freem(m0); 1040 for (m0 = m; m; m = m0) { 1041 m0 = m->m_nextpkt; 1042 m->m_nextpkt = 0; 1043 if (error == 0) { 1044 /* Record statistics for this interface address. */ 1045 if (ia) { 1046 ia->ia_ifa.if_opackets++; 1047 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1048 } 1049#ifdef IPSEC 1050 /* clean ipsec history once it goes out of the node */ 1051 ipsec_delaux(m); 1052#endif 1053 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 1054 } else 1055 m_freem(m); 1056 } 1057 1058 if (error == 0) 1059 ip6stat.ip6s_fragmented++; 1060 1061done: 1062 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */ 1063 RTFREE(ro->ro_rt); 1064 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) { 1065 RTFREE(ro_pmtu->ro_rt); 1066 } 1067 1068#ifdef IPSEC 1069 if (sp != NULL) 1070 key_freesp(sp); 1071#endif /* IPSEC */ 1072 1073 return(error); 1074 1075freehdrs: 1076 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1077 m_freem(exthdrs.ip6e_dest1); 1078 m_freem(exthdrs.ip6e_rthdr); 1079 m_freem(exthdrs.ip6e_dest2); 1080 /* fall through */ 1081bad: 1082 m_freem(m); 1083 goto done; 1084} 1085 1086static int 1087ip6_copyexthdr(mp, hdr, hlen) 1088 struct mbuf **mp; 1089 caddr_t hdr; 1090 int hlen; 1091{ 1092 struct mbuf *m; 1093 1094 if (hlen > MCLBYTES) 1095 return(ENOBUFS); /* XXX */ 1096 1097 MGET(m, M_DONTWAIT, MT_DATA); 1098 if (!m) 1099 return(ENOBUFS); 1100 1101 if (hlen > MLEN) { 1102 MCLGET(m, M_DONTWAIT); 1103 if ((m->m_flags & M_EXT) == 0) { 1104 m_free(m); 1105 return(ENOBUFS); 1106 } 1107 } 1108 m->m_len = hlen; 1109 if (hdr) 1110 bcopy(hdr, mtod(m, caddr_t), hlen); 1111 1112 *mp = m; 1113 return(0); 1114} 1115 1116/* 1117 * Insert jumbo payload option. 1118 */ 1119static int 1120ip6_insert_jumboopt(exthdrs, plen) 1121 struct ip6_exthdrs *exthdrs; 1122 u_int32_t plen; 1123{ 1124 struct mbuf *mopt; 1125 u_char *optbuf; 1126 u_int32_t v; 1127 1128#define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1129 1130 /* 1131 * If there is no hop-by-hop options header, allocate new one. 1132 * If there is one but it doesn't have enough space to store the 1133 * jumbo payload option, allocate a cluster to store the whole options. 1134 * Otherwise, use it to store the options. 1135 */ 1136 if (exthdrs->ip6e_hbh == 0) { 1137 MGET(mopt, M_DONTWAIT, MT_DATA); 1138 if (mopt == 0) 1139 return(ENOBUFS); 1140 mopt->m_len = JUMBOOPTLEN; 1141 optbuf = mtod(mopt, u_char *); 1142 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1143 exthdrs->ip6e_hbh = mopt; 1144 } else { 1145 struct ip6_hbh *hbh; 1146 1147 mopt = exthdrs->ip6e_hbh; 1148 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1149 /* 1150 * XXX assumption: 1151 * - exthdrs->ip6e_hbh is not referenced from places 1152 * other than exthdrs. 1153 * - exthdrs->ip6e_hbh is not an mbuf chain. 1154 */ 1155 int oldoptlen = mopt->m_len; 1156 struct mbuf *n; 1157 1158 /* 1159 * XXX: give up if the whole (new) hbh header does 1160 * not fit even in an mbuf cluster. 1161 */ 1162 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1163 return(ENOBUFS); 1164 1165 /* 1166 * As a consequence, we must always prepare a cluster 1167 * at this point. 1168 */ 1169 MGET(n, M_DONTWAIT, MT_DATA); 1170 if (n) { 1171 MCLGET(n, M_DONTWAIT); 1172 if ((n->m_flags & M_EXT) == 0) { 1173 m_freem(n); 1174 n = NULL; 1175 } 1176 } 1177 if (!n) 1178 return(ENOBUFS); 1179 n->m_len = oldoptlen + JUMBOOPTLEN; 1180 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1181 oldoptlen); 1182 optbuf = mtod(n, caddr_t) + oldoptlen; 1183 m_freem(mopt); 1184 mopt = exthdrs->ip6e_hbh = n; 1185 } else { 1186 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1187 mopt->m_len += JUMBOOPTLEN; 1188 } 1189 optbuf[0] = IP6OPT_PADN; 1190 optbuf[1] = 1; 1191 1192 /* 1193 * Adjust the header length according to the pad and 1194 * the jumbo payload option. 1195 */ 1196 hbh = mtod(mopt, struct ip6_hbh *); 1197 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1198 } 1199 1200 /* fill in the option. */ 1201 optbuf[2] = IP6OPT_JUMBO; 1202 optbuf[3] = 4; 1203 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1204 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1205 1206 /* finally, adjust the packet header length */ 1207 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1208 1209 return(0); 1210#undef JUMBOOPTLEN 1211} 1212 1213/* 1214 * Insert fragment header and copy unfragmentable header portions. 1215 */ 1216static int 1217ip6_insertfraghdr(m0, m, hlen, frghdrp) 1218 struct mbuf *m0, *m; 1219 int hlen; 1220 struct ip6_frag **frghdrp; 1221{ 1222 struct mbuf *n, *mlast; 1223 1224 if (hlen > sizeof(struct ip6_hdr)) { 1225 n = m_copym(m0, sizeof(struct ip6_hdr), 1226 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1227 if (n == 0) 1228 return(ENOBUFS); 1229 m->m_next = n; 1230 } else 1231 n = m; 1232 1233 /* Search for the last mbuf of unfragmentable part. */ 1234 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1235 ; 1236 1237 if ((mlast->m_flags & M_EXT) == 0 && 1238 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1239 /* use the trailing space of the last mbuf for the fragment hdr */ 1240 *frghdrp = 1241 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len); 1242 mlast->m_len += sizeof(struct ip6_frag); 1243 m->m_pkthdr.len += sizeof(struct ip6_frag); 1244 } else { 1245 /* allocate a new mbuf for the fragment header */ 1246 struct mbuf *mfrg; 1247 1248 MGET(mfrg, M_DONTWAIT, MT_DATA); 1249 if (mfrg == 0) 1250 return(ENOBUFS); 1251 mfrg->m_len = sizeof(struct ip6_frag); 1252 *frghdrp = mtod(mfrg, struct ip6_frag *); 1253 mlast->m_next = mfrg; 1254 } 1255 1256 return(0); 1257} 1258 1259/* 1260 * IP6 socket option processing. 1261 */ 1262int 1263ip6_ctloutput(so, sopt) 1264 struct socket *so; 1265 struct sockopt *sopt; 1266{ 1267 int privileged; 1268 struct inpcb *in6p = sotoinpcb(so); 1269 int error, optval; 1270 int level, op, optname; 1271 int optlen; 1272 struct thread *td; 1273 1274 if (sopt) { 1275 level = sopt->sopt_level; 1276 op = sopt->sopt_dir; 1277 optname = sopt->sopt_name; 1278 optlen = sopt->sopt_valsize; 1279 td = sopt->sopt_td; 1280 } else { 1281 panic("ip6_ctloutput: arg soopt is NULL"); 1282 } 1283 error = optval = 0; 1284 1285 privileged = (td == 0 || suser(td)) ? 0 : 1; 1286 1287 if (level == IPPROTO_IPV6) { 1288 switch (op) { 1289 1290 case SOPT_SET: 1291 switch (optname) { 1292 case IPV6_PKTOPTIONS: 1293 { 1294 struct mbuf *m; 1295 1296 error = soopt_getm(sopt, &m); /* XXX */ 1297 if (error != 0) 1298 break; 1299 error = soopt_mcopyin(sopt, m); /* XXX */ 1300 if (error != 0) 1301 break; 1302 error = ip6_pcbopts(&in6p->in6p_outputopts, 1303 m, so, sopt); 1304 m_freem(m); /* XXX */ 1305 break; 1306 } 1307 1308 /* 1309 * Use of some Hop-by-Hop options or some 1310 * Destination options, might require special 1311 * privilege. That is, normal applications 1312 * (without special privilege) might be forbidden 1313 * from setting certain options in outgoing packets, 1314 * and might never see certain options in received 1315 * packets. [RFC 2292 Section 6] 1316 * KAME specific note: 1317 * KAME prevents non-privileged users from sending or 1318 * receiving ANY hbh/dst options in order to avoid 1319 * overhead of parsing options in the kernel. 1320 */ 1321 case IPV6_UNICAST_HOPS: 1322 case IPV6_CHECKSUM: 1323 case IPV6_FAITH: 1324 1325 case IPV6_V6ONLY: 1326 if (optlen != sizeof(int)) { 1327 error = EINVAL; 1328 break; 1329 } 1330 error = sooptcopyin(sopt, &optval, 1331 sizeof optval, sizeof optval); 1332 if (error) 1333 break; 1334 switch (optname) { 1335 1336 case IPV6_UNICAST_HOPS: 1337 if (optval < -1 || optval >= 256) 1338 error = EINVAL; 1339 else { 1340 /* -1 = kernel default */ 1341 in6p->in6p_hops = optval; 1342 1343 if ((in6p->in6p_vflag & 1344 INP_IPV4) != 0) 1345 in6p->inp_ip_ttl = optval; 1346 } 1347 break; 1348#define OPTSET(bit) \ 1349do { \ 1350 if (optval) \ 1351 in6p->in6p_flags |= (bit); \ 1352 else \ 1353 in6p->in6p_flags &= ~(bit); \ 1354} while (0) 1355#define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) 1356 1357 case IPV6_CHECKSUM: 1358 in6p->in6p_cksum = optval; 1359 break; 1360 1361 case IPV6_FAITH: 1362 OPTSET(IN6P_FAITH); 1363 break; 1364 1365 case IPV6_V6ONLY: 1366 /* 1367 * make setsockopt(IPV6_V6ONLY) 1368 * available only prior to bind(2). 1369 * see ipng mailing list, Jun 22 2001. 1370 */ 1371 if (in6p->in6p_lport || 1372 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 1373 { 1374 error = EINVAL; 1375 break; 1376 } 1377 OPTSET(IN6P_IPV6_V6ONLY); 1378 if (optval) 1379 in6p->in6p_vflag &= ~INP_IPV4; 1380 else 1381 in6p->in6p_vflag |= INP_IPV4; 1382 break; 1383 } 1384 break; 1385 1386 case IPV6_PKTINFO: 1387 case IPV6_HOPLIMIT: 1388 case IPV6_HOPOPTS: 1389 case IPV6_DSTOPTS: 1390 case IPV6_RTHDR: 1391 /* RFC 2292 */ 1392 if (optlen != sizeof(int)) { 1393 error = EINVAL; 1394 break; 1395 } 1396 error = sooptcopyin(sopt, &optval, 1397 sizeof optval, sizeof optval); 1398 if (error) 1399 break; 1400 switch (optname) { 1401 case IPV6_PKTINFO: 1402 OPTSET(IN6P_PKTINFO); 1403 break; 1404 case IPV6_HOPLIMIT: 1405 OPTSET(IN6P_HOPLIMIT); 1406 break; 1407 case IPV6_HOPOPTS: 1408 /* 1409 * Check super-user privilege. 1410 * See comments for IPV6_RECVHOPOPTS. 1411 */ 1412 if (!privileged) 1413 return(EPERM); 1414 OPTSET(IN6P_HOPOPTS); 1415 break; 1416 case IPV6_DSTOPTS: 1417 if (!privileged) 1418 return(EPERM); 1419 OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1420 break; 1421 case IPV6_RTHDR: 1422 OPTSET(IN6P_RTHDR); 1423 break; 1424 } 1425 break; 1426#undef OPTSET 1427 1428 case IPV6_MULTICAST_IF: 1429 case IPV6_MULTICAST_HOPS: 1430 case IPV6_MULTICAST_LOOP: 1431 case IPV6_JOIN_GROUP: 1432 case IPV6_LEAVE_GROUP: 1433 { 1434 struct mbuf *m; 1435 if (sopt->sopt_valsize > MLEN) { 1436 error = EMSGSIZE; 1437 break; 1438 } 1439 /* XXX */ 1440 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1441 if (m == 0) { 1442 error = ENOBUFS; 1443 break; 1444 } 1445 m->m_len = sopt->sopt_valsize; 1446 error = sooptcopyin(sopt, mtod(m, char *), 1447 m->m_len, m->m_len); 1448 error = ip6_setmoptions(sopt->sopt_name, 1449 &in6p->in6p_moptions, 1450 m); 1451 (void)m_free(m); 1452 } 1453 break; 1454 1455 case IPV6_PORTRANGE: 1456 error = sooptcopyin(sopt, &optval, 1457 sizeof optval, sizeof optval); 1458 if (error) 1459 break; 1460 1461 switch (optval) { 1462 case IPV6_PORTRANGE_DEFAULT: 1463 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1464 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1465 break; 1466 1467 case IPV6_PORTRANGE_HIGH: 1468 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1469 in6p->in6p_flags |= IN6P_HIGHPORT; 1470 break; 1471 1472 case IPV6_PORTRANGE_LOW: 1473 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1474 in6p->in6p_flags |= IN6P_LOWPORT; 1475 break; 1476 1477 default: 1478 error = EINVAL; 1479 break; 1480 } 1481 break; 1482 1483#ifdef IPSEC 1484 case IPV6_IPSEC_POLICY: 1485 { 1486 caddr_t req = NULL; 1487 size_t len = 0; 1488 struct mbuf *m; 1489 1490 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1491 break; 1492 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1493 break; 1494 if (m) { 1495 req = mtod(m, caddr_t); 1496 len = m->m_len; 1497 } 1498 error = ipsec6_set_policy(in6p, optname, req, 1499 len, privileged); 1500 m_freem(m); 1501 } 1502 break; 1503#endif /* KAME IPSEC */ 1504 1505 case IPV6_FW_ADD: 1506 case IPV6_FW_DEL: 1507 case IPV6_FW_FLUSH: 1508 case IPV6_FW_ZERO: 1509 { 1510 struct mbuf *m; 1511 struct mbuf **mp = &m; 1512 1513 if (ip6_fw_ctl_ptr == NULL) 1514 return EINVAL; 1515 /* XXX */ 1516 if ((error = soopt_getm(sopt, &m)) != 0) 1517 break; 1518 /* XXX */ 1519 if ((error = soopt_mcopyin(sopt, m)) != 0) 1520 break; 1521 error = (*ip6_fw_ctl_ptr)(optname, mp); 1522 m = *mp; 1523 } 1524 break; 1525 1526 default: 1527 error = ENOPROTOOPT; 1528 break; 1529 } 1530 break; 1531 1532 case SOPT_GET: 1533 switch (optname) { 1534 1535 case IPV6_PKTOPTIONS: 1536 if (in6p->in6p_options) { 1537 struct mbuf *m; 1538 m = m_copym(in6p->in6p_options, 1539 0, M_COPYALL, M_TRYWAIT); 1540 error = soopt_mcopyout(sopt, m); 1541 if (error == 0) 1542 m_freem(m); 1543 } else 1544 sopt->sopt_valsize = 0; 1545 break; 1546 1547 case IPV6_UNICAST_HOPS: 1548 case IPV6_CHECKSUM: 1549 1550 case IPV6_FAITH: 1551 case IPV6_V6ONLY: 1552 case IPV6_PORTRANGE: 1553 switch (optname) { 1554 1555 case IPV6_UNICAST_HOPS: 1556 optval = in6p->in6p_hops; 1557 break; 1558 1559 case IPV6_CHECKSUM: 1560 optval = in6p->in6p_cksum; 1561 break; 1562 1563 case IPV6_FAITH: 1564 optval = OPTBIT(IN6P_FAITH); 1565 break; 1566 1567 case IPV6_V6ONLY: 1568 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1569 break; 1570 1571 case IPV6_PORTRANGE: 1572 { 1573 int flags; 1574 flags = in6p->in6p_flags; 1575 if (flags & IN6P_HIGHPORT) 1576 optval = IPV6_PORTRANGE_HIGH; 1577 else if (flags & IN6P_LOWPORT) 1578 optval = IPV6_PORTRANGE_LOW; 1579 else 1580 optval = 0; 1581 break; 1582 } 1583 } 1584 error = sooptcopyout(sopt, &optval, 1585 sizeof optval); 1586 break; 1587 1588 case IPV6_PKTINFO: 1589 case IPV6_HOPLIMIT: 1590 case IPV6_HOPOPTS: 1591 case IPV6_RTHDR: 1592 case IPV6_DSTOPTS: 1593 if (optname == IPV6_HOPOPTS || 1594 optname == IPV6_DSTOPTS || 1595 !privileged) 1596 return(EPERM); 1597 switch (optname) { 1598 case IPV6_PKTINFO: 1599 optval = OPTBIT(IN6P_PKTINFO); 1600 break; 1601 case IPV6_HOPLIMIT: 1602 optval = OPTBIT(IN6P_HOPLIMIT); 1603 break; 1604 case IPV6_HOPOPTS: 1605 if (!privileged) 1606 return(EPERM); 1607 optval = OPTBIT(IN6P_HOPOPTS); 1608 break; 1609 case IPV6_RTHDR: 1610 optval = OPTBIT(IN6P_RTHDR); 1611 break; 1612 case IPV6_DSTOPTS: 1613 if (!privileged) 1614 return(EPERM); 1615 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1616 break; 1617 } 1618 error = sooptcopyout(sopt, &optval, 1619 sizeof optval); 1620 break; 1621 1622 case IPV6_MULTICAST_IF: 1623 case IPV6_MULTICAST_HOPS: 1624 case IPV6_MULTICAST_LOOP: 1625 case IPV6_JOIN_GROUP: 1626 case IPV6_LEAVE_GROUP: 1627 { 1628 struct mbuf *m; 1629 error = ip6_getmoptions(sopt->sopt_name, 1630 in6p->in6p_moptions, &m); 1631 if (error == 0) 1632 error = sooptcopyout(sopt, 1633 mtod(m, char *), m->m_len); 1634 m_freem(m); 1635 } 1636 break; 1637 1638#ifdef IPSEC 1639 case IPV6_IPSEC_POLICY: 1640 { 1641 caddr_t req = NULL; 1642 size_t len = 0; 1643 struct mbuf *m = NULL; 1644 struct mbuf **mp = &m; 1645 1646 error = soopt_getm(sopt, &m); /* XXX */ 1647 if (error != 0) 1648 break; 1649 error = soopt_mcopyin(sopt, m); /* XXX */ 1650 if (error != 0) 1651 break; 1652 if (m) { 1653 req = mtod(m, caddr_t); 1654 len = m->m_len; 1655 } 1656 error = ipsec6_get_policy(in6p, req, len, mp); 1657 if (error == 0) 1658 error = soopt_mcopyout(sopt, m); /*XXX*/ 1659 if (error == 0 && m) 1660 m_freem(m); 1661 break; 1662 } 1663#endif /* KAME IPSEC */ 1664 1665 case IPV6_FW_GET: 1666 { 1667 struct mbuf *m; 1668 struct mbuf **mp = &m; 1669 1670 if (ip6_fw_ctl_ptr == NULL) 1671 { 1672 return EINVAL; 1673 } 1674 error = (*ip6_fw_ctl_ptr)(optname, mp); 1675 if (error == 0) 1676 error = soopt_mcopyout(sopt, m); /* XXX */ 1677 if (error == 0 && m) 1678 m_freem(m); 1679 } 1680 break; 1681 1682 default: 1683 error = ENOPROTOOPT; 1684 break; 1685 } 1686 break; 1687 } 1688 } else { 1689 error = EINVAL; 1690 } 1691 return(error); 1692} 1693 1694/* 1695 * Set up IP6 options in pcb for insertion in output packets or 1696 * specifying behavior of outgoing packets. 1697 */ 1698static int 1699ip6_pcbopts(pktopt, m, so, sopt) 1700 struct ip6_pktopts **pktopt; 1701 struct mbuf *m; 1702 struct socket *so; 1703 struct sockopt *sopt; 1704{ 1705 struct ip6_pktopts *opt = *pktopt; 1706 int error = 0; 1707 struct thread *td = sopt->sopt_td; 1708 int priv = 0; 1709 1710 /* turn off any old options. */ 1711 if (opt) { 1712#ifdef DIAGNOSTIC 1713 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 1714 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 1715 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 1716 printf("ip6_pcbopts: all specified options are cleared.\n"); 1717#endif 1718 ip6_clearpktopts(opt, 1, -1); 1719 } else 1720 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 1721 *pktopt = NULL; 1722 1723 if (!m || m->m_len == 0) { 1724 /* 1725 * Only turning off any previous options. 1726 */ 1727 if (opt) 1728 free(opt, M_IP6OPT); 1729 return(0); 1730 } 1731 1732 /* set options specified by user. */ 1733 if (td && !suser(td)) 1734 priv = 1; 1735 if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) { 1736 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */ 1737 return(error); 1738 } 1739 *pktopt = opt; 1740 return(0); 1741} 1742 1743/* 1744 * initialize ip6_pktopts. beware that there are non-zero default values in 1745 * the struct. 1746 */ 1747void 1748init_ip6pktopts(opt) 1749 struct ip6_pktopts *opt; 1750{ 1751 1752 bzero(opt, sizeof(*opt)); 1753 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 1754} 1755 1756void 1757ip6_clearpktopts(pktopt, needfree, optname) 1758 struct ip6_pktopts *pktopt; 1759 int needfree, optname; 1760{ 1761 if (pktopt == NULL) 1762 return; 1763 1764 if (optname == -1) { 1765 if (needfree && pktopt->ip6po_pktinfo) 1766 free(pktopt->ip6po_pktinfo, M_IP6OPT); 1767 pktopt->ip6po_pktinfo = NULL; 1768 } 1769 if (optname == -1) 1770 pktopt->ip6po_hlim = -1; 1771 if (optname == -1) { 1772 if (needfree && pktopt->ip6po_nexthop) 1773 free(pktopt->ip6po_nexthop, M_IP6OPT); 1774 pktopt->ip6po_nexthop = NULL; 1775 } 1776 if (optname == -1) { 1777 if (needfree && pktopt->ip6po_hbh) 1778 free(pktopt->ip6po_hbh, M_IP6OPT); 1779 pktopt->ip6po_hbh = NULL; 1780 } 1781 if (optname == -1) { 1782 if (needfree && pktopt->ip6po_dest1) 1783 free(pktopt->ip6po_dest1, M_IP6OPT); 1784 pktopt->ip6po_dest1 = NULL; 1785 } 1786 if (optname == -1) { 1787 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 1788 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 1789 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 1790 if (pktopt->ip6po_route.ro_rt) { 1791 RTFREE(pktopt->ip6po_route.ro_rt); 1792 pktopt->ip6po_route.ro_rt = NULL; 1793 } 1794 } 1795 if (optname == -1) { 1796 if (needfree && pktopt->ip6po_dest2) 1797 free(pktopt->ip6po_dest2, M_IP6OPT); 1798 pktopt->ip6po_dest2 = NULL; 1799 } 1800} 1801 1802#define PKTOPT_EXTHDRCPY(type) \ 1803do {\ 1804 if (src->type) {\ 1805 int hlen =\ 1806 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 1807 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 1808 if (dst->type == NULL && canwait == M_NOWAIT)\ 1809 goto bad;\ 1810 bcopy(src->type, dst->type, hlen);\ 1811 }\ 1812} while (0) 1813 1814struct ip6_pktopts * 1815ip6_copypktopts(src, canwait) 1816 struct ip6_pktopts *src; 1817 int canwait; 1818{ 1819 struct ip6_pktopts *dst; 1820 1821 if (src == NULL) { 1822 printf("ip6_clearpktopts: invalid argument\n"); 1823 return(NULL); 1824 } 1825 1826 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 1827 if (dst == NULL && canwait == M_NOWAIT) 1828 goto bad; 1829 bzero(dst, sizeof(*dst)); 1830 1831 dst->ip6po_hlim = src->ip6po_hlim; 1832 if (src->ip6po_pktinfo) { 1833 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 1834 M_IP6OPT, canwait); 1835 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT) 1836 goto bad; 1837 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 1838 } 1839 if (src->ip6po_nexthop) { 1840 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 1841 M_IP6OPT, canwait); 1842 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT) 1843 goto bad; 1844 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 1845 src->ip6po_nexthop->sa_len); 1846 } 1847 PKTOPT_EXTHDRCPY(ip6po_hbh); 1848 PKTOPT_EXTHDRCPY(ip6po_dest1); 1849 PKTOPT_EXTHDRCPY(ip6po_dest2); 1850 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 1851 return(dst); 1852 1853 bad: 1854 printf("ip6_copypktopts: copy failed"); 1855 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT); 1856 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT); 1857 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT); 1858 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT); 1859 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT); 1860 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT); 1861 return(NULL); 1862} 1863#undef PKTOPT_EXTHDRCPY 1864 1865void 1866ip6_freepcbopts(pktopt) 1867 struct ip6_pktopts *pktopt; 1868{ 1869 if (pktopt == NULL) 1870 return; 1871 1872 ip6_clearpktopts(pktopt, 1, -1); 1873 1874 free(pktopt, M_IP6OPT); 1875} 1876 1877/* 1878 * Set the IP6 multicast options in response to user setsockopt(). 1879 */ 1880static int 1881ip6_setmoptions(optname, im6op, m) 1882 int optname; 1883 struct ip6_moptions **im6op; 1884 struct mbuf *m; 1885{ 1886 int error = 0; 1887 u_int loop, ifindex; 1888 struct ipv6_mreq *mreq; 1889 struct ifnet *ifp; 1890 struct ip6_moptions *im6o = *im6op; 1891 struct route_in6 ro; 1892 struct sockaddr_in6 *dst; 1893 struct in6_multi_mship *imm; 1894 struct thread *td = curthread; /* XXX */ 1895 1896 if (im6o == NULL) { 1897 /* 1898 * No multicast option buffer attached to the pcb; 1899 * allocate one and initialize to default values. 1900 */ 1901 im6o = (struct ip6_moptions *) 1902 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK); 1903 1904 if (im6o == NULL) 1905 return(ENOBUFS); 1906 *im6op = im6o; 1907 im6o->im6o_multicast_ifp = NULL; 1908 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 1909 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 1910 LIST_INIT(&im6o->im6o_memberships); 1911 } 1912 1913 switch (optname) { 1914 1915 case IPV6_MULTICAST_IF: 1916 /* 1917 * Select the interface for outgoing multicast packets. 1918 */ 1919 if (m == NULL || m->m_len != sizeof(u_int)) { 1920 error = EINVAL; 1921 break; 1922 } 1923 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex)); 1924 if (ifindex < 0 || if_index < ifindex) { 1925 error = ENXIO; /* XXX EINVAL? */ 1926 break; 1927 } 1928 ifp = ifnet_byindex(ifindex); 1929 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1930 error = EADDRNOTAVAIL; 1931 break; 1932 } 1933 im6o->im6o_multicast_ifp = ifp; 1934 break; 1935 1936 case IPV6_MULTICAST_HOPS: 1937 { 1938 /* 1939 * Set the IP6 hoplimit for outgoing multicast packets. 1940 */ 1941 int optval; 1942 if (m == NULL || m->m_len != sizeof(int)) { 1943 error = EINVAL; 1944 break; 1945 } 1946 bcopy(mtod(m, u_int *), &optval, sizeof(optval)); 1947 if (optval < -1 || optval >= 256) 1948 error = EINVAL; 1949 else if (optval == -1) 1950 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 1951 else 1952 im6o->im6o_multicast_hlim = optval; 1953 break; 1954 } 1955 1956 case IPV6_MULTICAST_LOOP: 1957 /* 1958 * Set the loopback flag for outgoing multicast packets. 1959 * Must be zero or one. 1960 */ 1961 if (m == NULL || m->m_len != sizeof(u_int)) { 1962 error = EINVAL; 1963 break; 1964 } 1965 bcopy(mtod(m, u_int *), &loop, sizeof(loop)); 1966 if (loop > 1) { 1967 error = EINVAL; 1968 break; 1969 } 1970 im6o->im6o_multicast_loop = loop; 1971 break; 1972 1973 case IPV6_JOIN_GROUP: 1974 /* 1975 * Add a multicast group membership. 1976 * Group must be a valid IP6 multicast address. 1977 */ 1978 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 1979 error = EINVAL; 1980 break; 1981 } 1982 mreq = mtod(m, struct ipv6_mreq *); 1983 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 1984 /* 1985 * We use the unspecified address to specify to accept 1986 * all multicast addresses. Only super user is allowed 1987 * to do this. 1988 */ 1989 if (suser(td)) 1990 { 1991 error = EACCES; 1992 break; 1993 } 1994 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 1995 error = EINVAL; 1996 break; 1997 } 1998 1999 /* 2000 * If the interface is specified, validate it. 2001 */ 2002 if (mreq->ipv6mr_interface < 0 2003 || if_index < mreq->ipv6mr_interface) { 2004 error = ENXIO; /* XXX EINVAL? */ 2005 break; 2006 } 2007 /* 2008 * If no interface was explicitly specified, choose an 2009 * appropriate one according to the given multicast address. 2010 */ 2011 if (mreq->ipv6mr_interface == 0) { 2012 /* 2013 * If the multicast address is in node-local scope, 2014 * the interface should be a loopback interface. 2015 * Otherwise, look up the routing table for the 2016 * address, and choose the outgoing interface. 2017 * XXX: is it a good approach? 2018 */ 2019 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) { 2020 ifp = &loif[0]; 2021 } else { 2022 ro.ro_rt = NULL; 2023 dst = (struct sockaddr_in6 *)&ro.ro_dst; 2024 bzero(dst, sizeof(*dst)); 2025 dst->sin6_len = sizeof(struct sockaddr_in6); 2026 dst->sin6_family = AF_INET6; 2027 dst->sin6_addr = mreq->ipv6mr_multiaddr; 2028 rtalloc((struct route *)&ro); 2029 if (ro.ro_rt == NULL) { 2030 error = EADDRNOTAVAIL; 2031 break; 2032 } 2033 ifp = ro.ro_rt->rt_ifp; 2034 rtfree(ro.ro_rt); 2035 } 2036 } else 2037 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2038 2039 /* 2040 * See if we found an interface, and confirm that it 2041 * supports multicast 2042 */ 2043 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2044 error = EADDRNOTAVAIL; 2045 break; 2046 } 2047 /* 2048 * Put interface index into the multicast address, 2049 * if the address has link-local scope. 2050 */ 2051 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2052 mreq->ipv6mr_multiaddr.s6_addr16[1] 2053 = htons(mreq->ipv6mr_interface); 2054 } 2055 /* 2056 * See if the membership already exists. 2057 */ 2058 for (imm = im6o->im6o_memberships.lh_first; 2059 imm != NULL; imm = imm->i6mm_chain.le_next) 2060 if (imm->i6mm_maddr->in6m_ifp == ifp && 2061 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2062 &mreq->ipv6mr_multiaddr)) 2063 break; 2064 if (imm != NULL) { 2065 error = EADDRINUSE; 2066 break; 2067 } 2068 /* 2069 * Everything looks good; add a new record to the multicast 2070 * address list for the given interface. 2071 */ 2072 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK); 2073 if (imm == NULL) { 2074 error = ENOBUFS; 2075 break; 2076 } 2077 if ((imm->i6mm_maddr = 2078 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) { 2079 free(imm, M_IPMADDR); 2080 break; 2081 } 2082 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2083 break; 2084 2085 case IPV6_LEAVE_GROUP: 2086 /* 2087 * Drop a multicast group membership. 2088 * Group must be a valid IP6 multicast address. 2089 */ 2090 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2091 error = EINVAL; 2092 break; 2093 } 2094 mreq = mtod(m, struct ipv6_mreq *); 2095 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2096 if (suser(td)) { 2097 error = EACCES; 2098 break; 2099 } 2100 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2101 error = EINVAL; 2102 break; 2103 } 2104 /* 2105 * If an interface address was specified, get a pointer 2106 * to its ifnet structure. 2107 */ 2108 if (mreq->ipv6mr_interface < 0 2109 || if_index < mreq->ipv6mr_interface) { 2110 error = ENXIO; /* XXX EINVAL? */ 2111 break; 2112 } 2113 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2114 /* 2115 * Put interface index into the multicast address, 2116 * if the address has link-local scope. 2117 */ 2118 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2119 mreq->ipv6mr_multiaddr.s6_addr16[1] 2120 = htons(mreq->ipv6mr_interface); 2121 } 2122 /* 2123 * Find the membership in the membership list. 2124 */ 2125 for (imm = im6o->im6o_memberships.lh_first; 2126 imm != NULL; imm = imm->i6mm_chain.le_next) { 2127 if ((ifp == NULL || 2128 imm->i6mm_maddr->in6m_ifp == ifp) && 2129 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2130 &mreq->ipv6mr_multiaddr)) 2131 break; 2132 } 2133 if (imm == NULL) { 2134 /* Unable to resolve interface */ 2135 error = EADDRNOTAVAIL; 2136 break; 2137 } 2138 /* 2139 * Give up the multicast address record to which the 2140 * membership points. 2141 */ 2142 LIST_REMOVE(imm, i6mm_chain); 2143 in6_delmulti(imm->i6mm_maddr); 2144 free(imm, M_IPMADDR); 2145 break; 2146 2147 default: 2148 error = EOPNOTSUPP; 2149 break; 2150 } 2151 2152 /* 2153 * If all options have default values, no need to keep the mbuf. 2154 */ 2155 if (im6o->im6o_multicast_ifp == NULL && 2156 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2157 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2158 im6o->im6o_memberships.lh_first == NULL) { 2159 free(*im6op, M_IPMOPTS); 2160 *im6op = NULL; 2161 } 2162 2163 return(error); 2164} 2165 2166/* 2167 * Return the IP6 multicast options in response to user getsockopt(). 2168 */ 2169static int 2170ip6_getmoptions(optname, im6o, mp) 2171 int optname; 2172 struct ip6_moptions *im6o; 2173 struct mbuf **mp; 2174{ 2175 u_int *hlim, *loop, *ifindex; 2176 2177 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */ 2178 2179 switch (optname) { 2180 2181 case IPV6_MULTICAST_IF: 2182 ifindex = mtod(*mp, u_int *); 2183 (*mp)->m_len = sizeof(u_int); 2184 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2185 *ifindex = 0; 2186 else 2187 *ifindex = im6o->im6o_multicast_ifp->if_index; 2188 return(0); 2189 2190 case IPV6_MULTICAST_HOPS: 2191 hlim = mtod(*mp, u_int *); 2192 (*mp)->m_len = sizeof(u_int); 2193 if (im6o == NULL) 2194 *hlim = ip6_defmcasthlim; 2195 else 2196 *hlim = im6o->im6o_multicast_hlim; 2197 return(0); 2198 2199 case IPV6_MULTICAST_LOOP: 2200 loop = mtod(*mp, u_int *); 2201 (*mp)->m_len = sizeof(u_int); 2202 if (im6o == NULL) 2203 *loop = ip6_defmcasthlim; 2204 else 2205 *loop = im6o->im6o_multicast_loop; 2206 return(0); 2207 2208 default: 2209 return(EOPNOTSUPP); 2210 } 2211} 2212 2213/* 2214 * Discard the IP6 multicast options. 2215 */ 2216void 2217ip6_freemoptions(im6o) 2218 struct ip6_moptions *im6o; 2219{ 2220 struct in6_multi_mship *imm; 2221 2222 if (im6o == NULL) 2223 return; 2224 2225 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2226 LIST_REMOVE(imm, i6mm_chain); 2227 if (imm->i6mm_maddr) 2228 in6_delmulti(imm->i6mm_maddr); 2229 free(imm, M_IPMADDR); 2230 } 2231 free(im6o, M_IPMOPTS); 2232} 2233 2234/* 2235 * Set IPv6 outgoing packet options based on advanced API. 2236 */ 2237int 2238ip6_setpktoptions(control, opt, priv, needcopy) 2239 struct mbuf *control; 2240 struct ip6_pktopts *opt; 2241 int priv, needcopy; 2242{ 2243 struct cmsghdr *cm = 0; 2244 2245 if (control == 0 || opt == 0) 2246 return(EINVAL); 2247 2248 init_ip6pktopts(opt); 2249 2250 /* 2251 * XXX: Currently, we assume all the optional information is stored 2252 * in a single mbuf. 2253 */ 2254 if (control->m_next) 2255 return(EINVAL); 2256 2257 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2258 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2259 cm = mtod(control, struct cmsghdr *); 2260 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2261 return(EINVAL); 2262 if (cm->cmsg_level != IPPROTO_IPV6) 2263 continue; 2264 2265 /* 2266 * XXX should check if RFC2292 API is mixed with 2292bis API 2267 */ 2268 switch (cm->cmsg_type) { 2269 case IPV6_PKTINFO: 2270 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo))) 2271 return(EINVAL); 2272 if (needcopy) { 2273 /* XXX: Is it really WAITOK? */ 2274 opt->ip6po_pktinfo = 2275 malloc(sizeof(struct in6_pktinfo), 2276 M_IP6OPT, M_WAITOK); 2277 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo, 2278 sizeof(struct in6_pktinfo)); 2279 } else 2280 opt->ip6po_pktinfo = 2281 (struct in6_pktinfo *)CMSG_DATA(cm); 2282 if (opt->ip6po_pktinfo->ipi6_ifindex && 2283 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr)) 2284 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] = 2285 htons(opt->ip6po_pktinfo->ipi6_ifindex); 2286 2287 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index 2288 || opt->ip6po_pktinfo->ipi6_ifindex < 0) { 2289 return(ENXIO); 2290 } 2291 2292 /* 2293 * Check if the requested source address is indeed a 2294 * unicast address assigned to the node, and can be 2295 * used as the packet's source address. 2296 */ 2297 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) { 2298 struct in6_ifaddr *ia6; 2299 struct sockaddr_in6 sin6; 2300 2301 bzero(&sin6, sizeof(sin6)); 2302 sin6.sin6_len = sizeof(sin6); 2303 sin6.sin6_family = AF_INET6; 2304 sin6.sin6_addr = 2305 opt->ip6po_pktinfo->ipi6_addr; 2306 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6)); 2307 if (ia6 == NULL || 2308 (ia6->ia6_flags & (IN6_IFF_ANYCAST | 2309 IN6_IFF_NOTREADY)) != 0) 2310 return(EADDRNOTAVAIL); 2311 } 2312 break; 2313 2314 case IPV6_HOPLIMIT: 2315 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) 2316 return(EINVAL); 2317 2318 opt->ip6po_hlim = *(int *)CMSG_DATA(cm); 2319 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255) 2320 return(EINVAL); 2321 break; 2322 2323 case IPV6_NEXTHOP: 2324 if (!priv) 2325 return(EPERM); 2326 2327 if (cm->cmsg_len < sizeof(u_char) || 2328 /* check if cmsg_len is large enough for sa_len */ 2329 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm))) 2330 return(EINVAL); 2331 2332 if (needcopy) { 2333 opt->ip6po_nexthop = 2334 malloc(*CMSG_DATA(cm), 2335 M_IP6OPT, M_WAITOK); 2336 bcopy(CMSG_DATA(cm), 2337 opt->ip6po_nexthop, 2338 *CMSG_DATA(cm)); 2339 } else 2340 opt->ip6po_nexthop = 2341 (struct sockaddr *)CMSG_DATA(cm); 2342 break; 2343 2344 case IPV6_HOPOPTS: 2345 { 2346 struct ip6_hbh *hbh; 2347 int hbhlen; 2348 2349 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh))) 2350 return(EINVAL); 2351 hbh = (struct ip6_hbh *)CMSG_DATA(cm); 2352 hbhlen = (hbh->ip6h_len + 1) << 3; 2353 if (cm->cmsg_len != CMSG_LEN(hbhlen)) 2354 return(EINVAL); 2355 2356 if (needcopy) { 2357 opt->ip6po_hbh = 2358 malloc(hbhlen, M_IP6OPT, M_WAITOK); 2359 bcopy(hbh, opt->ip6po_hbh, hbhlen); 2360 } else 2361 opt->ip6po_hbh = hbh; 2362 break; 2363 } 2364 2365 case IPV6_DSTOPTS: 2366 { 2367 struct ip6_dest *dest, **newdest; 2368 int destlen; 2369 2370 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest))) 2371 return(EINVAL); 2372 dest = (struct ip6_dest *)CMSG_DATA(cm); 2373 destlen = (dest->ip6d_len + 1) << 3; 2374 if (cm->cmsg_len != CMSG_LEN(destlen)) 2375 return(EINVAL); 2376 2377 /* 2378 * The old advacned API is ambiguous on this 2379 * point. Our approach is to determine the 2380 * position based according to the existence 2381 * of a routing header. Note, however, that 2382 * this depends on the order of the extension 2383 * headers in the ancillary data; the 1st part 2384 * of the destination options header must 2385 * appear before the routing header in the 2386 * ancillary data, too. 2387 * RFC2292bis solved the ambiguity by 2388 * introducing separate cmsg types. 2389 */ 2390 if (opt->ip6po_rthdr == NULL) 2391 newdest = &opt->ip6po_dest1; 2392 else 2393 newdest = &opt->ip6po_dest2; 2394 2395 if (needcopy) { 2396 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK); 2397 bcopy(dest, *newdest, destlen); 2398 } else 2399 *newdest = dest; 2400 2401 break; 2402 } 2403 2404 case IPV6_RTHDR: 2405 { 2406 struct ip6_rthdr *rth; 2407 int rthlen; 2408 2409 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr))) 2410 return(EINVAL); 2411 rth = (struct ip6_rthdr *)CMSG_DATA(cm); 2412 rthlen = (rth->ip6r_len + 1) << 3; 2413 if (cm->cmsg_len != CMSG_LEN(rthlen)) 2414 return(EINVAL); 2415 2416 switch (rth->ip6r_type) { 2417 case IPV6_RTHDR_TYPE_0: 2418 /* must contain one addr */ 2419 if (rth->ip6r_len == 0) 2420 return(EINVAL); 2421 /* length must be even */ 2422 if (rth->ip6r_len % 2) 2423 return(EINVAL); 2424 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 2425 return(EINVAL); 2426 break; 2427 default: 2428 return(EINVAL); /* not supported */ 2429 } 2430 2431 if (needcopy) { 2432 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, 2433 M_WAITOK); 2434 bcopy(rth, opt->ip6po_rthdr, rthlen); 2435 } else 2436 opt->ip6po_rthdr = rth; 2437 2438 break; 2439 } 2440 2441 default: 2442 return(ENOPROTOOPT); 2443 } 2444 } 2445 2446 return(0); 2447} 2448 2449/* 2450 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 2451 * packet to the input queue of a specified interface. Note that this 2452 * calls the output routine of the loopback "driver", but with an interface 2453 * pointer that might NOT be &loif -- easier than replicating that code here. 2454 */ 2455void 2456ip6_mloopback(ifp, m, dst) 2457 struct ifnet *ifp; 2458 struct mbuf *m; 2459 struct sockaddr_in6 *dst; 2460{ 2461 struct mbuf *copym; 2462 struct ip6_hdr *ip6; 2463 2464 copym = m_copy(m, 0, M_COPYALL); 2465 if (copym == NULL) 2466 return; 2467 2468 /* 2469 * Make sure to deep-copy IPv6 header portion in case the data 2470 * is in an mbuf cluster, so that we can safely override the IPv6 2471 * header portion later. 2472 */ 2473 if ((copym->m_flags & M_EXT) != 0 || 2474 copym->m_len < sizeof(struct ip6_hdr)) { 2475 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 2476 if (copym == NULL) 2477 return; 2478 } 2479 2480#ifdef DIAGNOSTIC 2481 if (copym->m_len < sizeof(*ip6)) { 2482 m_freem(copym); 2483 return; 2484 } 2485#endif 2486 2487 ip6 = mtod(copym, struct ip6_hdr *); 2488#ifndef SCOPEDROUTING 2489 /* 2490 * clear embedded scope identifiers if necessary. 2491 * in6_clearscope will touch the addresses only when necessary. 2492 */ 2493 in6_clearscope(&ip6->ip6_src); 2494 in6_clearscope(&ip6->ip6_dst); 2495#endif 2496 2497 (void)if_simloop(ifp, copym, dst->sin6_family, 0); 2498} 2499 2500/* 2501 * Chop IPv6 header off from the payload. 2502 */ 2503static int 2504ip6_splithdr(m, exthdrs) 2505 struct mbuf *m; 2506 struct ip6_exthdrs *exthdrs; 2507{ 2508 struct mbuf *mh; 2509 struct ip6_hdr *ip6; 2510 2511 ip6 = mtod(m, struct ip6_hdr *); 2512 if (m->m_len > sizeof(*ip6)) { 2513 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 2514 if (mh == 0) { 2515 m_freem(m); 2516 return ENOBUFS; 2517 } 2518 M_COPY_PKTHDR(mh, m); 2519 MH_ALIGN(mh, sizeof(*ip6)); 2520 m->m_flags &= ~M_PKTHDR; 2521 m->m_len -= sizeof(*ip6); 2522 m->m_data += sizeof(*ip6); 2523 mh->m_next = m; 2524 m = mh; 2525 m->m_len = sizeof(*ip6); 2526 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 2527 } 2528 exthdrs->ip6e_ip6 = m; 2529 return 0; 2530} 2531 2532/* 2533 * Compute IPv6 extension header length. 2534 */ 2535int 2536ip6_optlen(in6p) 2537 struct in6pcb *in6p; 2538{ 2539 int len; 2540 2541 if (!in6p->in6p_outputopts) 2542 return 0; 2543 2544 len = 0; 2545#define elen(x) \ 2546 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 2547 2548 len += elen(in6p->in6p_outputopts->ip6po_hbh); 2549 if (in6p->in6p_outputopts->ip6po_rthdr) 2550 /* dest1 is valid with rthdr only */ 2551 len += elen(in6p->in6p_outputopts->ip6po_dest1); 2552 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 2553 len += elen(in6p->in6p_outputopts->ip6po_dest2); 2554 return len; 2555#undef elen 2556}
|