65 66#include "opt_inet.h" 67#include "opt_inet6.h" 68#include "opt_ipsec.h" 69 70#include <sys/param.h> 71#include <sys/kernel.h> 72#include <sys/malloc.h> 73#include <sys/mbuf.h> 74#include <sys/errno.h> 75#include <sys/priv.h> 76#include <sys/proc.h> 77#include <sys/protosw.h> 78#include <sys/socket.h> 79#include <sys/socketvar.h> 80#include <sys/ucred.h> 81 82#include <net/if.h> 83#include <net/netisr.h> 84#include <net/route.h> 85#include <net/pfil.h> 86 87#include <netinet/in.h> 88#include <netinet/in_var.h> 89#include <netinet6/in6_var.h> 90#include <netinet/ip6.h> 91#include <netinet/icmp6.h> 92#include <netinet6/ip6_var.h> 93#include <netinet/in_pcb.h> 94#include <netinet/tcp_var.h> 95#include <netinet6/nd6.h> 96 97#ifdef IPSEC 98#include <netipsec/ipsec.h> 99#include <netipsec/ipsec6.h> 100#include <netipsec/key.h> 101#include <netinet6/ip6_ipsec.h> 102#endif /* IPSEC */ 103 104#include <netinet6/ip6protosw.h> 105#include <netinet6/scope6_var.h> 106 107static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "internet multicast options"); 108 109struct ip6_exthdrs { 110 struct mbuf *ip6e_ip6; 111 struct mbuf *ip6e_hbh; 112 struct mbuf *ip6e_dest1; 113 struct mbuf *ip6e_rthdr; 114 struct mbuf *ip6e_dest2; 115}; 116 117static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **, 118 struct ucred *, int)); 119static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *, 120 struct socket *, struct sockopt *)); 121static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *); 122static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, 123 struct ucred *, int, int, int)); 124 125static int ip6_setmoptions(int, struct ip6_moptions **, struct mbuf *); 126static int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **); 127static int ip6_copyexthdr(struct mbuf **, caddr_t, int); 128static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int, 129 struct ip6_frag **)); 130static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t); 131static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *); 132static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *, 133 struct ifnet *, struct in6_addr *, u_long *, int *)); 134static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int); 135 136 137/* 138 * Make an extension header from option data. hp is the source, and 139 * mp is the destination. 140 */ 141#define MAKE_EXTHDR(hp, mp) \ 142 do { \ 143 if (hp) { \ 144 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 145 error = ip6_copyexthdr((mp), (caddr_t)(hp), \ 146 ((eh)->ip6e_len + 1) << 3); \ 147 if (error) \ 148 goto freehdrs; \ 149 } \ 150 } while (/*CONSTCOND*/ 0) 151 152/* 153 * Form a chain of extension headers. 154 * m is the extension header mbuf 155 * mp is the previous mbuf in the chain 156 * p is the next header 157 * i is the type of option. 158 */ 159#define MAKE_CHAIN(m, mp, p, i)\ 160 do {\ 161 if (m) {\ 162 if (!hdrsplit) \ 163 panic("assumption failed: hdr not split"); \ 164 *mtod((m), u_char *) = *(p);\ 165 *(p) = (i);\ 166 p = mtod((m), u_char *);\ 167 (m)->m_next = (mp)->m_next;\ 168 (mp)->m_next = (m);\ 169 (mp) = (m);\ 170 }\ 171 } while (/*CONSTCOND*/ 0) 172 173/* 174 * IP6 output. The packet in mbuf chain m contains a skeletal IP6 175 * header (with pri, len, nxt, hlim, src, dst). 176 * This function may modify ver and hlim only. 177 * The mbuf chain containing the packet will be freed. 178 * The mbuf opt, if present, will not be freed. 179 * 180 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and 181 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one, 182 * which is rt_rmx.rmx_mtu. 183 * 184 * ifpp - XXX: just for statistics 185 */ 186int 187ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, 188 struct route_in6 *ro, int flags, struct ip6_moptions *im6o, 189 struct ifnet **ifpp, struct inpcb *inp) 190{ 191 struct ip6_hdr *ip6, *mhip6; 192 struct ifnet *ifp, *origifp; 193 struct mbuf *m = m0; 194 struct mbuf *mprev = NULL; 195 int hlen, tlen, len, off; 196 struct route_in6 ip6route; 197 struct rtentry *rt = NULL; 198 struct sockaddr_in6 *dst, src_sa, dst_sa; 199 struct in6_addr odst; 200 int error = 0; 201 struct in6_ifaddr *ia = NULL; 202 u_long mtu; 203 int alwaysfrag, dontfrag; 204 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; 205 struct ip6_exthdrs exthdrs; 206 struct in6_addr finaldst, src0, dst0; 207 u_int32_t zone; 208 struct route_in6 *ro_pmtu = NULL; 209 int hdrsplit = 0; 210 int needipsec = 0; 211#ifdef IPSEC 212 struct ipsec_output_state state; 213 struct ip6_rthdr *rh = NULL; 214 int needipsectun = 0; 215 int segleft_org = 0; 216 struct secpolicy *sp = NULL; 217#endif /* IPSEC */ 218 219 ip6 = mtod(m, struct ip6_hdr *); 220 if (ip6 == NULL) { 221 printf ("ip6 is NULL"); 222 goto bad; 223 } 224 225 finaldst = ip6->ip6_dst; 226 227 bzero(&exthdrs, sizeof(exthdrs)); 228 229 if (opt) { 230 /* Hop-by-Hop options header */ 231 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 232 /* Destination options header(1st part) */ 233 if (opt->ip6po_rthdr) { 234 /* 235 * Destination options header(1st part) 236 * This only makes sense with a routing header. 237 * See Section 9.2 of RFC 3542. 238 * Disabling this part just for MIP6 convenience is 239 * a bad idea. We need to think carefully about a 240 * way to make the advanced API coexist with MIP6 241 * options, which might automatically be inserted in 242 * the kernel. 243 */ 244 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 245 } 246 /* Routing header */ 247 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 248 /* Destination options header(2nd part) */ 249 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 250 } 251 252 /* 253 * IPSec checking which handles several cases. 254 * FAST IPSEC: We re-injected the packet. 255 */ 256#ifdef IPSEC 257 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp)) 258 { 259 case 1: /* Bad packet */ 260 goto freehdrs; 261 case -1: /* Do IPSec */ 262 needipsec = 1; 263 case 0: /* No IPSec */ 264 default: 265 break; 266 } 267#endif /* IPSEC */ 268 269 /* 270 * Calculate the total length of the extension header chain. 271 * Keep the length of the unfragmentable part for fragmentation. 272 */ 273 optlen = 0; 274 if (exthdrs.ip6e_hbh) 275 optlen += exthdrs.ip6e_hbh->m_len; 276 if (exthdrs.ip6e_dest1) 277 optlen += exthdrs.ip6e_dest1->m_len; 278 if (exthdrs.ip6e_rthdr) 279 optlen += exthdrs.ip6e_rthdr->m_len; 280 unfragpartlen = optlen + sizeof(struct ip6_hdr); 281 282 /* NOTE: we don't add AH/ESP length here. do that later. */ 283 if (exthdrs.ip6e_dest2) 284 optlen += exthdrs.ip6e_dest2->m_len; 285 286 /* 287 * If we need IPsec, or there is at least one extension header, 288 * separate IP6 header from the payload. 289 */ 290 if ((needipsec || optlen) && !hdrsplit) { 291 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 292 m = NULL; 293 goto freehdrs; 294 } 295 m = exthdrs.ip6e_ip6; 296 hdrsplit++; 297 } 298 299 /* adjust pointer */ 300 ip6 = mtod(m, struct ip6_hdr *); 301 302 /* adjust mbuf packet header length */ 303 m->m_pkthdr.len += optlen; 304 plen = m->m_pkthdr.len - sizeof(*ip6); 305 306 /* If this is a jumbo payload, insert a jumbo payload option. */ 307 if (plen > IPV6_MAXPACKET) { 308 if (!hdrsplit) { 309 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 310 m = NULL; 311 goto freehdrs; 312 } 313 m = exthdrs.ip6e_ip6; 314 hdrsplit++; 315 } 316 /* adjust pointer */ 317 ip6 = mtod(m, struct ip6_hdr *); 318 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 319 goto freehdrs; 320 ip6->ip6_plen = 0; 321 } else 322 ip6->ip6_plen = htons(plen); 323 324 /* 325 * Concatenate headers and fill in next header fields. 326 * Here we have, on "m" 327 * IPv6 payload 328 * and we insert headers accordingly. Finally, we should be getting: 329 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 330 * 331 * during the header composing process, "m" points to IPv6 header. 332 * "mprev" points to an extension header prior to esp. 333 */ 334 u_char *nexthdrp = &ip6->ip6_nxt; 335 mprev = m; 336 337 /* 338 * we treat dest2 specially. this makes IPsec processing 339 * much easier. the goal here is to make mprev point the 340 * mbuf prior to dest2. 341 * 342 * result: IPv6 dest2 payload 343 * m and mprev will point to IPv6 header. 344 */ 345 if (exthdrs.ip6e_dest2) { 346 if (!hdrsplit) 347 panic("assumption failed: hdr not split"); 348 exthdrs.ip6e_dest2->m_next = m->m_next; 349 m->m_next = exthdrs.ip6e_dest2; 350 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 351 ip6->ip6_nxt = IPPROTO_DSTOPTS; 352 } 353 354 /* 355 * result: IPv6 hbh dest1 rthdr dest2 payload 356 * m will point to IPv6 header. mprev will point to the 357 * extension header prior to dest2 (rthdr in the above case). 358 */ 359 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 360 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 361 IPPROTO_DSTOPTS); 362 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 363 IPPROTO_ROUTING); 364 365#ifdef IPSEC 366 if (!needipsec) 367 goto skip_ipsec2; 368 369 /* 370 * pointers after IPsec headers are not valid any more. 371 * other pointers need a great care too. 372 * (IPsec routines should not mangle mbufs prior to AH/ESP) 373 */ 374 exthdrs.ip6e_dest2 = NULL; 375 376 if (exthdrs.ip6e_rthdr) { 377 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 378 segleft_org = rh->ip6r_segleft; 379 rh->ip6r_segleft = 0; 380 } 381 382 bzero(&state, sizeof(state)); 383 state.m = m; 384 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags, 385 &needipsectun); 386 m = state.m; 387 if (error) { 388 /* mbuf is already reclaimed in ipsec6_output_trans. */ 389 m = NULL; 390 switch (error) { 391 case EHOSTUNREACH: 392 case ENETUNREACH: 393 case EMSGSIZE: 394 case ENOBUFS: 395 case ENOMEM: 396 break; 397 default: 398 printf("[%s:%d] (ipsec): error code %d\n", 399 __func__, __LINE__, error); 400 /* FALLTHROUGH */ 401 case ENOENT: 402 /* don't show these error codes to the user */ 403 error = 0; 404 break; 405 } 406 goto bad; 407 } else if (!needipsectun) { 408 /* 409 * In the FAST IPSec case we have already 410 * re-injected the packet and it has been freed 411 * by the ipsec_done() function. So, just clean 412 * up after ourselves. 413 */ 414 m = NULL; 415 goto done; 416 } 417 if (exthdrs.ip6e_rthdr) { 418 /* ah6_output doesn't modify mbuf chain */ 419 rh->ip6r_segleft = segleft_org; 420 } 421skip_ipsec2:; 422#endif /* IPSEC */ 423 424 /* 425 * If there is a routing header, replace the destination address field 426 * with the first hop of the routing header. 427 */ 428 if (exthdrs.ip6e_rthdr) { 429 struct ip6_rthdr *rh = 430 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, 431 struct ip6_rthdr *)); 432 struct ip6_rthdr0 *rh0; 433 struct in6_addr *addr; 434 struct sockaddr_in6 sa; 435 436 switch (rh->ip6r_type) { 437 case IPV6_RTHDR_TYPE_0: 438 rh0 = (struct ip6_rthdr0 *)rh; 439 addr = (struct in6_addr *)(rh0 + 1); 440 441 /* 442 * construct a sockaddr_in6 form of 443 * the first hop. 444 * 445 * XXX: we may not have enough 446 * information about its scope zone; 447 * there is no standard API to pass 448 * the information from the 449 * application. 450 */ 451 bzero(&sa, sizeof(sa)); 452 sa.sin6_family = AF_INET6; 453 sa.sin6_len = sizeof(sa); 454 sa.sin6_addr = addr[0]; 455 if ((error = sa6_embedscope(&sa, 456 ip6_use_defzone)) != 0) { 457 goto bad; 458 } 459 ip6->ip6_dst = sa.sin6_addr; 460 bcopy(&addr[1], &addr[0], sizeof(struct in6_addr) 461 * (rh0->ip6r0_segleft - 1)); 462 addr[rh0->ip6r0_segleft - 1] = finaldst; 463 /* XXX */ 464 in6_clearscope(addr + rh0->ip6r0_segleft - 1); 465 break; 466 default: /* is it possible? */ 467 error = EINVAL; 468 goto bad; 469 } 470 } 471 472 /* Source address validation */ 473 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 474 (flags & IPV6_UNSPECSRC) == 0) { 475 error = EOPNOTSUPP; 476 ip6stat.ip6s_badscope++; 477 goto bad; 478 } 479 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 480 error = EOPNOTSUPP; 481 ip6stat.ip6s_badscope++; 482 goto bad; 483 } 484 485 ip6stat.ip6s_localout++; 486 487 /* 488 * Route packet. 489 */ 490 if (ro == 0) { 491 ro = &ip6route; 492 bzero((caddr_t)ro, sizeof(*ro)); 493 } 494 ro_pmtu = ro; 495 if (opt && opt->ip6po_rthdr) 496 ro = &opt->ip6po_route; 497 dst = (struct sockaddr_in6 *)&ro->ro_dst; 498 499again: 500 /* 501 * if specified, try to fill in the traffic class field. 502 * do not override if a non-zero value is already set. 503 * we check the diffserv field and the ecn field separately. 504 */ 505 if (opt && opt->ip6po_tclass >= 0) { 506 int mask = 0; 507 508 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0) 509 mask |= 0xfc; 510 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0) 511 mask |= 0x03; 512 if (mask != 0) 513 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20); 514 } 515 516 /* fill in or override the hop limit field, if necessary. */ 517 if (opt && opt->ip6po_hlim != -1) 518 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 519 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 520 if (im6o != NULL) 521 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 522 else 523 ip6->ip6_hlim = ip6_defmcasthlim; 524 } 525 526#ifdef IPSEC 527 /* 528 * We may re-inject packets into the stack here. 529 */ 530 if (needipsec && needipsectun) { 531 struct ipsec_output_state state; 532 533 /* 534 * All the extension headers will become inaccessible 535 * (since they can be encrypted). 536 * Don't panic, we need no more updates to extension headers 537 * on inner IPv6 packet (since they are now encapsulated). 538 * 539 * IPv6 [ESP|AH] IPv6 [extension headers] payload 540 */ 541 bzero(&exthdrs, sizeof(exthdrs)); 542 exthdrs.ip6e_ip6 = m; 543 544 bzero(&state, sizeof(state)); 545 state.m = m; 546 state.ro = (struct route *)ro; 547 state.dst = (struct sockaddr *)dst; 548 549 error = ipsec6_output_tunnel(&state, sp, flags); 550 551 m = state.m; 552 ro = (struct route_in6 *)state.ro; 553 dst = (struct sockaddr_in6 *)state.dst; 554 if (error) { 555 /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 556 m0 = m = NULL; 557 m = NULL; 558 switch (error) { 559 case EHOSTUNREACH: 560 case ENETUNREACH: 561 case EMSGSIZE: 562 case ENOBUFS: 563 case ENOMEM: 564 break; 565 default: 566 printf("[%s:%d] (ipsec): error code %d\n", 567 __func__, __LINE__, error); 568 /* FALLTHROUGH */ 569 case ENOENT: 570 /* don't show these error codes to the user */ 571 error = 0; 572 break; 573 } 574 goto bad; 575 } else { 576 /* 577 * In the FAST IPSec case we have already 578 * re-injected the packet and it has been freed 579 * by the ipsec_done() function. So, just clean 580 * up after ourselves. 581 */ 582 m = NULL; 583 goto done; 584 } 585 586 exthdrs.ip6e_ip6 = m; 587 } 588#endif /* IPSEC */ 589 590 /* adjust pointer */ 591 ip6 = mtod(m, struct ip6_hdr *); 592 593 bzero(&dst_sa, sizeof(dst_sa)); 594 dst_sa.sin6_family = AF_INET6; 595 dst_sa.sin6_len = sizeof(dst_sa); 596 dst_sa.sin6_addr = ip6->ip6_dst; 597 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro, 598 &ifp, &rt, 0)) != 0) { 599 switch (error) { 600 case EHOSTUNREACH: 601 ip6stat.ip6s_noroute++; 602 break; 603 case EADDRNOTAVAIL: 604 default: 605 break; /* XXX statistics? */ 606 } 607 if (ifp != NULL) 608 in6_ifstat_inc(ifp, ifs6_out_discard); 609 goto bad; 610 } 611 if (rt == NULL) { 612 /* 613 * If in6_selectroute() does not return a route entry, 614 * dst may not have been updated. 615 */ 616 *dst = dst_sa; /* XXX */ 617 } 618 619 /* 620 * then rt (for unicast) and ifp must be non-NULL valid values. 621 */ 622 if ((flags & IPV6_FORWARDING) == 0) { 623 /* XXX: the FORWARDING flag can be set for mrouting. */ 624 in6_ifstat_inc(ifp, ifs6_out_request); 625 } 626 if (rt != NULL) { 627 ia = (struct in6_ifaddr *)(rt->rt_ifa); 628 rt->rt_use++; 629 } 630 631 /* 632 * The outgoing interface must be in the zone of source and 633 * destination addresses. We should use ia_ifp to support the 634 * case of sending packets to an address of our own. 635 */ 636 if (ia != NULL && ia->ia_ifp) 637 origifp = ia->ia_ifp; 638 else 639 origifp = ifp; 640 641 src0 = ip6->ip6_src; 642 if (in6_setscope(&src0, origifp, &zone)) 643 goto badscope; 644 bzero(&src_sa, sizeof(src_sa)); 645 src_sa.sin6_family = AF_INET6; 646 src_sa.sin6_len = sizeof(src_sa); 647 src_sa.sin6_addr = ip6->ip6_src; 648 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id) 649 goto badscope; 650 651 dst0 = ip6->ip6_dst; 652 if (in6_setscope(&dst0, origifp, &zone)) 653 goto badscope; 654 /* re-initialize to be sure */ 655 bzero(&dst_sa, sizeof(dst_sa)); 656 dst_sa.sin6_family = AF_INET6; 657 dst_sa.sin6_len = sizeof(dst_sa); 658 dst_sa.sin6_addr = ip6->ip6_dst; 659 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) { 660 goto badscope; 661 } 662 663 /* scope check is done. */ 664 goto routefound; 665 666 badscope: 667 ip6stat.ip6s_badscope++; 668 in6_ifstat_inc(origifp, ifs6_out_discard); 669 if (error == 0) 670 error = EHOSTUNREACH; /* XXX */ 671 goto bad; 672 673 routefound: 674 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 675 if (opt && opt->ip6po_nextroute.ro_rt) { 676 /* 677 * The nexthop is explicitly specified by the 678 * application. We assume the next hop is an IPv6 679 * address. 680 */ 681 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop; 682 } 683 else if ((rt->rt_flags & RTF_GATEWAY)) 684 dst = (struct sockaddr_in6 *)rt->rt_gateway; 685 } 686 687 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 688 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 689 } else { 690 struct in6_multi *in6m; 691 692 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 693 694 in6_ifstat_inc(ifp, ifs6_out_mcast); 695 696 /* 697 * Confirm that the outgoing interface supports multicast. 698 */ 699 if (!(ifp->if_flags & IFF_MULTICAST)) { 700 ip6stat.ip6s_noroute++; 701 in6_ifstat_inc(ifp, ifs6_out_discard); 702 error = ENETUNREACH; 703 goto bad; 704 } 705 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); 706 if (in6m != NULL && 707 (im6o == NULL || im6o->im6o_multicast_loop)) { 708 /* 709 * If we belong to the destination multicast group 710 * on the outgoing interface, and the caller did not 711 * forbid loopback, loop back a copy. 712 */ 713 ip6_mloopback(ifp, m, dst); 714 } else { 715 /* 716 * If we are acting as a multicast router, perform 717 * multicast forwarding as if the packet had just 718 * arrived on the interface to which we are about 719 * to send. The multicast forwarding function 720 * recursively calls this function, using the 721 * IPV6_FORWARDING flag to prevent infinite recursion. 722 * 723 * Multicasts that are looped back by ip6_mloopback(), 724 * above, will be forwarded by the ip6_input() routine, 725 * if necessary. 726 */ 727 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 728 /* 729 * XXX: ip6_mforward expects that rcvif is NULL 730 * when it is called from the originating path. 731 * However, it is not always the case, since 732 * some versions of MGETHDR() does not 733 * initialize the field. 734 */ 735 m->m_pkthdr.rcvif = NULL; 736 if (ip6_mforward(ip6, ifp, m) != 0) { 737 m_freem(m); 738 goto done; 739 } 740 } 741 } 742 /* 743 * Multicasts with a hoplimit of zero may be looped back, 744 * above, but must not be transmitted on a network. 745 * Also, multicasts addressed to the loopback interface 746 * are not sent -- the above call to ip6_mloopback() will 747 * loop back a copy if this host actually belongs to the 748 * destination group on the loopback interface. 749 */ 750 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) || 751 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) { 752 m_freem(m); 753 goto done; 754 } 755 } 756 757 /* 758 * Fill the outgoing inteface to tell the upper layer 759 * to increment per-interface statistics. 760 */ 761 if (ifpp) 762 *ifpp = ifp; 763 764 /* Determine path MTU. */ 765 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu, 766 &alwaysfrag)) != 0) 767 goto bad; 768 769 /* 770 * The caller of this function may specify to use the minimum MTU 771 * in some cases. 772 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU 773 * setting. The logic is a bit complicated; by default, unicast 774 * packets will follow path MTU while multicast packets will be sent at 775 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets 776 * including unicast ones will be sent at the minimum MTU. Multicast 777 * packets will always be sent at the minimum MTU unless 778 * IP6PO_MINMTU_DISABLE is explicitly specified. 779 * See RFC 3542 for more details. 780 */ 781 if (mtu > IPV6_MMTU) { 782 if ((flags & IPV6_MINMTU)) 783 mtu = IPV6_MMTU; 784 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) 785 mtu = IPV6_MMTU; 786 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 787 (opt == NULL || 788 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) { 789 mtu = IPV6_MMTU; 790 } 791 } 792 793 /* 794 * clear embedded scope identifiers if necessary. 795 * in6_clearscope will touch the addresses only when necessary. 796 */ 797 in6_clearscope(&ip6->ip6_src); 798 in6_clearscope(&ip6->ip6_dst); 799 800 /* 801 * If the outgoing packet contains a hop-by-hop options header, 802 * it must be examined and processed even by the source node. 803 * (RFC 2460, section 4.) 804 */ 805 if (exthdrs.ip6e_hbh) { 806 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 807 u_int32_t dummy; /* XXX unused */ 808 u_int32_t plen = 0; /* XXX: ip6_process will check the value */ 809 810#ifdef DIAGNOSTIC 811 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) 812 panic("ip6e_hbh is not continuous"); 813#endif 814 /* 815 * XXX: if we have to send an ICMPv6 error to the sender, 816 * we need the M_LOOP flag since icmp6_error() expects 817 * the IPv6 and the hop-by-hop options header are 818 * continuous unless the flag is set. 819 */ 820 m->m_flags |= M_LOOP; 821 m->m_pkthdr.rcvif = ifp; 822 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1), 823 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh), 824 &dummy, &plen) < 0) { 825 /* m was already freed at this point */ 826 error = EINVAL;/* better error? */ 827 goto done; 828 } 829 m->m_flags &= ~M_LOOP; /* XXX */ 830 m->m_pkthdr.rcvif = NULL; 831 } 832 833 /* Jump over all PFIL processing if hooks are not active. */ 834 if (!PFIL_HOOKED(&inet6_pfil_hook)) 835 goto passout; 836 837 odst = ip6->ip6_dst; 838 /* Run through list of hooks for output packets. */ 839 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp); 840 if (error != 0 || m == NULL) 841 goto done; 842 ip6 = mtod(m, struct ip6_hdr *); 843 844 /* See if destination IP address was changed by packet filter. */ 845 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) { 846 m->m_flags |= M_SKIP_FIREWALL; 847 /* If destination is now ourself drop to ip6_input(). */ 848 if (in6_localaddr(&ip6->ip6_dst)) { 849 if (m->m_pkthdr.rcvif == NULL) 850 m->m_pkthdr.rcvif = loif; 851 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 852 m->m_pkthdr.csum_flags |= 853 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 854 m->m_pkthdr.csum_data = 0xffff; 855 } 856 m->m_pkthdr.csum_flags |= 857 CSUM_IP_CHECKED | CSUM_IP_VALID; 858 error = netisr_queue(NETISR_IPV6, m); 859 goto done; 860 } else 861 goto again; /* Redo the routing table lookup. */ 862 } 863 864 /* XXX: IPFIREWALL_FORWARD */ 865 866passout: 867 /* 868 * Send the packet to the outgoing interface. 869 * If necessary, do IPv6 fragmentation before sending. 870 * 871 * the logic here is rather complex: 872 * 1: normal case (dontfrag == 0, alwaysfrag == 0) 873 * 1-a: send as is if tlen <= path mtu 874 * 1-b: fragment if tlen > path mtu 875 * 876 * 2: if user asks us not to fragment (dontfrag == 1) 877 * 2-a: send as is if tlen <= interface mtu 878 * 2-b: error if tlen > interface mtu 879 * 880 * 3: if we always need to attach fragment header (alwaysfrag == 1) 881 * always fragment 882 * 883 * 4: if dontfrag == 1 && alwaysfrag == 1 884 * error, as we cannot handle this conflicting request 885 */ 886 tlen = m->m_pkthdr.len; 887 888 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) 889 dontfrag = 1; 890 else 891 dontfrag = 0; 892 if (dontfrag && alwaysfrag) { /* case 4 */ 893 /* conflicting request - can't transmit */ 894 error = EMSGSIZE; 895 goto bad; 896 } 897 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */ 898 /* 899 * Even if the DONTFRAG option is specified, we cannot send the 900 * packet when the data length is larger than the MTU of the 901 * outgoing interface. 902 * Notify the error by sending IPV6_PATHMTU ancillary data as 903 * well as returning an error code (the latter is not described 904 * in the API spec.) 905 */ 906 u_int32_t mtu32; 907 struct ip6ctlparam ip6cp; 908 909 mtu32 = (u_int32_t)mtu; 910 bzero(&ip6cp, sizeof(ip6cp)); 911 ip6cp.ip6c_cmdarg = (void *)&mtu32; 912 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst, 913 (void *)&ip6cp); 914 915 error = EMSGSIZE; 916 goto bad; 917 } 918 919 /* 920 * transmit packet without fragmentation 921 */ 922 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */ 923 struct in6_ifaddr *ia6; 924 925 ip6 = mtod(m, struct ip6_hdr *); 926 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); 927 if (ia6) { 928 /* Record statistics for this interface address. */ 929 ia6->ia_ifa.if_opackets++; 930 ia6->ia_ifa.if_obytes += m->m_pkthdr.len; 931 } 932 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 933 goto done; 934 } 935 936 /* 937 * try to fragment the packet. case 1-b and 3 938 */ 939 if (mtu < IPV6_MMTU) { 940 /* path MTU cannot be less than IPV6_MMTU */ 941 error = EMSGSIZE; 942 in6_ifstat_inc(ifp, ifs6_out_fragfail); 943 goto bad; 944 } else if (ip6->ip6_plen == 0) { 945 /* jumbo payload cannot be fragmented */ 946 error = EMSGSIZE; 947 in6_ifstat_inc(ifp, ifs6_out_fragfail); 948 goto bad; 949 } else { 950 struct mbuf **mnext, *m_frgpart; 951 struct ip6_frag *ip6f; 952 u_int32_t id = htonl(ip6_randomid()); 953 u_char nextproto; 954 955 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len; 956 957 /* 958 * Too large for the destination or interface; 959 * fragment if possible. 960 * Must be able to put at least 8 bytes per fragment. 961 */ 962 hlen = unfragpartlen; 963 if (mtu > IPV6_MAXPACKET) 964 mtu = IPV6_MAXPACKET; 965 966 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 967 if (len < 8) { 968 error = EMSGSIZE; 969 in6_ifstat_inc(ifp, ifs6_out_fragfail); 970 goto bad; 971 } 972 973 /* 974 * Verify that we have any chance at all of being able to queue 975 * the packet or packet fragments 976 */ 977 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen) 978 < tlen /* - hlen */)) { 979 error = ENOBUFS; 980 ip6stat.ip6s_odropped++; 981 goto bad; 982 } 983 984 mnext = &m->m_nextpkt; 985 986 /* 987 * Change the next header field of the last header in the 988 * unfragmentable part. 989 */ 990 if (exthdrs.ip6e_rthdr) { 991 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 992 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 993 } else if (exthdrs.ip6e_dest1) { 994 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 995 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 996 } else if (exthdrs.ip6e_hbh) { 997 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 998 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 999 } else { 1000 nextproto = ip6->ip6_nxt; 1001 ip6->ip6_nxt = IPPROTO_FRAGMENT; 1002 } 1003 1004 /* 1005 * Loop through length of segment after first fragment, 1006 * make new header and copy data of each part and link onto 1007 * chain. 1008 */ 1009 m0 = m; 1010 for (off = hlen; off < tlen; off += len) { 1011 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1012 if (!m) { 1013 error = ENOBUFS; 1014 ip6stat.ip6s_odropped++; 1015 goto sendorfree; 1016 } 1017 m->m_pkthdr.rcvif = NULL; 1018 m->m_flags = m0->m_flags & M_COPYFLAGS; 1019 *mnext = m; 1020 mnext = &m->m_nextpkt; 1021 m->m_data += max_linkhdr; 1022 mhip6 = mtod(m, struct ip6_hdr *); 1023 *mhip6 = *ip6; 1024 m->m_len = sizeof(*mhip6); 1025 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 1026 if (error) { 1027 ip6stat.ip6s_odropped++; 1028 goto sendorfree; 1029 } 1030 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7)); 1031 if (off + len >= tlen) 1032 len = tlen - off; 1033 else 1034 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 1035 mhip6->ip6_plen = htons((u_short)(len + hlen + 1036 sizeof(*ip6f) - sizeof(struct ip6_hdr))); 1037 if ((m_frgpart = m_copy(m0, off, len)) == 0) { 1038 error = ENOBUFS; 1039 ip6stat.ip6s_odropped++; 1040 goto sendorfree; 1041 } 1042 m_cat(m, m_frgpart); 1043 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 1044 m->m_pkthdr.rcvif = NULL; 1045 ip6f->ip6f_reserved = 0; 1046 ip6f->ip6f_ident = id; 1047 ip6f->ip6f_nxt = nextproto; 1048 ip6stat.ip6s_ofragments++; 1049 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 1050 } 1051 1052 in6_ifstat_inc(ifp, ifs6_out_fragok); 1053 } 1054 1055 /* 1056 * Remove leading garbages. 1057 */ 1058sendorfree: 1059 m = m0->m_nextpkt; 1060 m0->m_nextpkt = 0; 1061 m_freem(m0); 1062 for (m0 = m; m; m = m0) { 1063 m0 = m->m_nextpkt; 1064 m->m_nextpkt = 0; 1065 if (error == 0) { 1066 /* Record statistics for this interface address. */ 1067 if (ia) { 1068 ia->ia_ifa.if_opackets++; 1069 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1070 } 1071 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 1072 } else 1073 m_freem(m); 1074 } 1075 1076 if (error == 0) 1077 ip6stat.ip6s_fragmented++; 1078 1079done: 1080 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */ 1081 RTFREE(ro->ro_rt); 1082 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) { 1083 RTFREE(ro_pmtu->ro_rt); 1084 }
| 65 66#include "opt_inet.h" 67#include "opt_inet6.h" 68#include "opt_ipsec.h" 69 70#include <sys/param.h> 71#include <sys/kernel.h> 72#include <sys/malloc.h> 73#include <sys/mbuf.h> 74#include <sys/errno.h> 75#include <sys/priv.h> 76#include <sys/proc.h> 77#include <sys/protosw.h> 78#include <sys/socket.h> 79#include <sys/socketvar.h> 80#include <sys/ucred.h> 81 82#include <net/if.h> 83#include <net/netisr.h> 84#include <net/route.h> 85#include <net/pfil.h> 86 87#include <netinet/in.h> 88#include <netinet/in_var.h> 89#include <netinet6/in6_var.h> 90#include <netinet/ip6.h> 91#include <netinet/icmp6.h> 92#include <netinet6/ip6_var.h> 93#include <netinet/in_pcb.h> 94#include <netinet/tcp_var.h> 95#include <netinet6/nd6.h> 96 97#ifdef IPSEC 98#include <netipsec/ipsec.h> 99#include <netipsec/ipsec6.h> 100#include <netipsec/key.h> 101#include <netinet6/ip6_ipsec.h> 102#endif /* IPSEC */ 103 104#include <netinet6/ip6protosw.h> 105#include <netinet6/scope6_var.h> 106 107static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "internet multicast options"); 108 109struct ip6_exthdrs { 110 struct mbuf *ip6e_ip6; 111 struct mbuf *ip6e_hbh; 112 struct mbuf *ip6e_dest1; 113 struct mbuf *ip6e_rthdr; 114 struct mbuf *ip6e_dest2; 115}; 116 117static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **, 118 struct ucred *, int)); 119static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *, 120 struct socket *, struct sockopt *)); 121static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *); 122static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, 123 struct ucred *, int, int, int)); 124 125static int ip6_setmoptions(int, struct ip6_moptions **, struct mbuf *); 126static int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **); 127static int ip6_copyexthdr(struct mbuf **, caddr_t, int); 128static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int, 129 struct ip6_frag **)); 130static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t); 131static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *); 132static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *, 133 struct ifnet *, struct in6_addr *, u_long *, int *)); 134static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int); 135 136 137/* 138 * Make an extension header from option data. hp is the source, and 139 * mp is the destination. 140 */ 141#define MAKE_EXTHDR(hp, mp) \ 142 do { \ 143 if (hp) { \ 144 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 145 error = ip6_copyexthdr((mp), (caddr_t)(hp), \ 146 ((eh)->ip6e_len + 1) << 3); \ 147 if (error) \ 148 goto freehdrs; \ 149 } \ 150 } while (/*CONSTCOND*/ 0) 151 152/* 153 * Form a chain of extension headers. 154 * m is the extension header mbuf 155 * mp is the previous mbuf in the chain 156 * p is the next header 157 * i is the type of option. 158 */ 159#define MAKE_CHAIN(m, mp, p, i)\ 160 do {\ 161 if (m) {\ 162 if (!hdrsplit) \ 163 panic("assumption failed: hdr not split"); \ 164 *mtod((m), u_char *) = *(p);\ 165 *(p) = (i);\ 166 p = mtod((m), u_char *);\ 167 (m)->m_next = (mp)->m_next;\ 168 (mp)->m_next = (m);\ 169 (mp) = (m);\ 170 }\ 171 } while (/*CONSTCOND*/ 0) 172 173/* 174 * IP6 output. The packet in mbuf chain m contains a skeletal IP6 175 * header (with pri, len, nxt, hlim, src, dst). 176 * This function may modify ver and hlim only. 177 * The mbuf chain containing the packet will be freed. 178 * The mbuf opt, if present, will not be freed. 179 * 180 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and 181 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one, 182 * which is rt_rmx.rmx_mtu. 183 * 184 * ifpp - XXX: just for statistics 185 */ 186int 187ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, 188 struct route_in6 *ro, int flags, struct ip6_moptions *im6o, 189 struct ifnet **ifpp, struct inpcb *inp) 190{ 191 struct ip6_hdr *ip6, *mhip6; 192 struct ifnet *ifp, *origifp; 193 struct mbuf *m = m0; 194 struct mbuf *mprev = NULL; 195 int hlen, tlen, len, off; 196 struct route_in6 ip6route; 197 struct rtentry *rt = NULL; 198 struct sockaddr_in6 *dst, src_sa, dst_sa; 199 struct in6_addr odst; 200 int error = 0; 201 struct in6_ifaddr *ia = NULL; 202 u_long mtu; 203 int alwaysfrag, dontfrag; 204 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; 205 struct ip6_exthdrs exthdrs; 206 struct in6_addr finaldst, src0, dst0; 207 u_int32_t zone; 208 struct route_in6 *ro_pmtu = NULL; 209 int hdrsplit = 0; 210 int needipsec = 0; 211#ifdef IPSEC 212 struct ipsec_output_state state; 213 struct ip6_rthdr *rh = NULL; 214 int needipsectun = 0; 215 int segleft_org = 0; 216 struct secpolicy *sp = NULL; 217#endif /* IPSEC */ 218 219 ip6 = mtod(m, struct ip6_hdr *); 220 if (ip6 == NULL) { 221 printf ("ip6 is NULL"); 222 goto bad; 223 } 224 225 finaldst = ip6->ip6_dst; 226 227 bzero(&exthdrs, sizeof(exthdrs)); 228 229 if (opt) { 230 /* Hop-by-Hop options header */ 231 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 232 /* Destination options header(1st part) */ 233 if (opt->ip6po_rthdr) { 234 /* 235 * Destination options header(1st part) 236 * This only makes sense with a routing header. 237 * See Section 9.2 of RFC 3542. 238 * Disabling this part just for MIP6 convenience is 239 * a bad idea. We need to think carefully about a 240 * way to make the advanced API coexist with MIP6 241 * options, which might automatically be inserted in 242 * the kernel. 243 */ 244 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 245 } 246 /* Routing header */ 247 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 248 /* Destination options header(2nd part) */ 249 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 250 } 251 252 /* 253 * IPSec checking which handles several cases. 254 * FAST IPSEC: We re-injected the packet. 255 */ 256#ifdef IPSEC 257 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp)) 258 { 259 case 1: /* Bad packet */ 260 goto freehdrs; 261 case -1: /* Do IPSec */ 262 needipsec = 1; 263 case 0: /* No IPSec */ 264 default: 265 break; 266 } 267#endif /* IPSEC */ 268 269 /* 270 * Calculate the total length of the extension header chain. 271 * Keep the length of the unfragmentable part for fragmentation. 272 */ 273 optlen = 0; 274 if (exthdrs.ip6e_hbh) 275 optlen += exthdrs.ip6e_hbh->m_len; 276 if (exthdrs.ip6e_dest1) 277 optlen += exthdrs.ip6e_dest1->m_len; 278 if (exthdrs.ip6e_rthdr) 279 optlen += exthdrs.ip6e_rthdr->m_len; 280 unfragpartlen = optlen + sizeof(struct ip6_hdr); 281 282 /* NOTE: we don't add AH/ESP length here. do that later. */ 283 if (exthdrs.ip6e_dest2) 284 optlen += exthdrs.ip6e_dest2->m_len; 285 286 /* 287 * If we need IPsec, or there is at least one extension header, 288 * separate IP6 header from the payload. 289 */ 290 if ((needipsec || optlen) && !hdrsplit) { 291 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 292 m = NULL; 293 goto freehdrs; 294 } 295 m = exthdrs.ip6e_ip6; 296 hdrsplit++; 297 } 298 299 /* adjust pointer */ 300 ip6 = mtod(m, struct ip6_hdr *); 301 302 /* adjust mbuf packet header length */ 303 m->m_pkthdr.len += optlen; 304 plen = m->m_pkthdr.len - sizeof(*ip6); 305 306 /* If this is a jumbo payload, insert a jumbo payload option. */ 307 if (plen > IPV6_MAXPACKET) { 308 if (!hdrsplit) { 309 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 310 m = NULL; 311 goto freehdrs; 312 } 313 m = exthdrs.ip6e_ip6; 314 hdrsplit++; 315 } 316 /* adjust pointer */ 317 ip6 = mtod(m, struct ip6_hdr *); 318 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 319 goto freehdrs; 320 ip6->ip6_plen = 0; 321 } else 322 ip6->ip6_plen = htons(plen); 323 324 /* 325 * Concatenate headers and fill in next header fields. 326 * Here we have, on "m" 327 * IPv6 payload 328 * and we insert headers accordingly. Finally, we should be getting: 329 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 330 * 331 * during the header composing process, "m" points to IPv6 header. 332 * "mprev" points to an extension header prior to esp. 333 */ 334 u_char *nexthdrp = &ip6->ip6_nxt; 335 mprev = m; 336 337 /* 338 * we treat dest2 specially. this makes IPsec processing 339 * much easier. the goal here is to make mprev point the 340 * mbuf prior to dest2. 341 * 342 * result: IPv6 dest2 payload 343 * m and mprev will point to IPv6 header. 344 */ 345 if (exthdrs.ip6e_dest2) { 346 if (!hdrsplit) 347 panic("assumption failed: hdr not split"); 348 exthdrs.ip6e_dest2->m_next = m->m_next; 349 m->m_next = exthdrs.ip6e_dest2; 350 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 351 ip6->ip6_nxt = IPPROTO_DSTOPTS; 352 } 353 354 /* 355 * result: IPv6 hbh dest1 rthdr dest2 payload 356 * m will point to IPv6 header. mprev will point to the 357 * extension header prior to dest2 (rthdr in the above case). 358 */ 359 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 360 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 361 IPPROTO_DSTOPTS); 362 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 363 IPPROTO_ROUTING); 364 365#ifdef IPSEC 366 if (!needipsec) 367 goto skip_ipsec2; 368 369 /* 370 * pointers after IPsec headers are not valid any more. 371 * other pointers need a great care too. 372 * (IPsec routines should not mangle mbufs prior to AH/ESP) 373 */ 374 exthdrs.ip6e_dest2 = NULL; 375 376 if (exthdrs.ip6e_rthdr) { 377 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 378 segleft_org = rh->ip6r_segleft; 379 rh->ip6r_segleft = 0; 380 } 381 382 bzero(&state, sizeof(state)); 383 state.m = m; 384 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags, 385 &needipsectun); 386 m = state.m; 387 if (error) { 388 /* mbuf is already reclaimed in ipsec6_output_trans. */ 389 m = NULL; 390 switch (error) { 391 case EHOSTUNREACH: 392 case ENETUNREACH: 393 case EMSGSIZE: 394 case ENOBUFS: 395 case ENOMEM: 396 break; 397 default: 398 printf("[%s:%d] (ipsec): error code %d\n", 399 __func__, __LINE__, error); 400 /* FALLTHROUGH */ 401 case ENOENT: 402 /* don't show these error codes to the user */ 403 error = 0; 404 break; 405 } 406 goto bad; 407 } else if (!needipsectun) { 408 /* 409 * In the FAST IPSec case we have already 410 * re-injected the packet and it has been freed 411 * by the ipsec_done() function. So, just clean 412 * up after ourselves. 413 */ 414 m = NULL; 415 goto done; 416 } 417 if (exthdrs.ip6e_rthdr) { 418 /* ah6_output doesn't modify mbuf chain */ 419 rh->ip6r_segleft = segleft_org; 420 } 421skip_ipsec2:; 422#endif /* IPSEC */ 423 424 /* 425 * If there is a routing header, replace the destination address field 426 * with the first hop of the routing header. 427 */ 428 if (exthdrs.ip6e_rthdr) { 429 struct ip6_rthdr *rh = 430 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, 431 struct ip6_rthdr *)); 432 struct ip6_rthdr0 *rh0; 433 struct in6_addr *addr; 434 struct sockaddr_in6 sa; 435 436 switch (rh->ip6r_type) { 437 case IPV6_RTHDR_TYPE_0: 438 rh0 = (struct ip6_rthdr0 *)rh; 439 addr = (struct in6_addr *)(rh0 + 1); 440 441 /* 442 * construct a sockaddr_in6 form of 443 * the first hop. 444 * 445 * XXX: we may not have enough 446 * information about its scope zone; 447 * there is no standard API to pass 448 * the information from the 449 * application. 450 */ 451 bzero(&sa, sizeof(sa)); 452 sa.sin6_family = AF_INET6; 453 sa.sin6_len = sizeof(sa); 454 sa.sin6_addr = addr[0]; 455 if ((error = sa6_embedscope(&sa, 456 ip6_use_defzone)) != 0) { 457 goto bad; 458 } 459 ip6->ip6_dst = sa.sin6_addr; 460 bcopy(&addr[1], &addr[0], sizeof(struct in6_addr) 461 * (rh0->ip6r0_segleft - 1)); 462 addr[rh0->ip6r0_segleft - 1] = finaldst; 463 /* XXX */ 464 in6_clearscope(addr + rh0->ip6r0_segleft - 1); 465 break; 466 default: /* is it possible? */ 467 error = EINVAL; 468 goto bad; 469 } 470 } 471 472 /* Source address validation */ 473 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 474 (flags & IPV6_UNSPECSRC) == 0) { 475 error = EOPNOTSUPP; 476 ip6stat.ip6s_badscope++; 477 goto bad; 478 } 479 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 480 error = EOPNOTSUPP; 481 ip6stat.ip6s_badscope++; 482 goto bad; 483 } 484 485 ip6stat.ip6s_localout++; 486 487 /* 488 * Route packet. 489 */ 490 if (ro == 0) { 491 ro = &ip6route; 492 bzero((caddr_t)ro, sizeof(*ro)); 493 } 494 ro_pmtu = ro; 495 if (opt && opt->ip6po_rthdr) 496 ro = &opt->ip6po_route; 497 dst = (struct sockaddr_in6 *)&ro->ro_dst; 498 499again: 500 /* 501 * if specified, try to fill in the traffic class field. 502 * do not override if a non-zero value is already set. 503 * we check the diffserv field and the ecn field separately. 504 */ 505 if (opt && opt->ip6po_tclass >= 0) { 506 int mask = 0; 507 508 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0) 509 mask |= 0xfc; 510 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0) 511 mask |= 0x03; 512 if (mask != 0) 513 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20); 514 } 515 516 /* fill in or override the hop limit field, if necessary. */ 517 if (opt && opt->ip6po_hlim != -1) 518 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 519 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 520 if (im6o != NULL) 521 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 522 else 523 ip6->ip6_hlim = ip6_defmcasthlim; 524 } 525 526#ifdef IPSEC 527 /* 528 * We may re-inject packets into the stack here. 529 */ 530 if (needipsec && needipsectun) { 531 struct ipsec_output_state state; 532 533 /* 534 * All the extension headers will become inaccessible 535 * (since they can be encrypted). 536 * Don't panic, we need no more updates to extension headers 537 * on inner IPv6 packet (since they are now encapsulated). 538 * 539 * IPv6 [ESP|AH] IPv6 [extension headers] payload 540 */ 541 bzero(&exthdrs, sizeof(exthdrs)); 542 exthdrs.ip6e_ip6 = m; 543 544 bzero(&state, sizeof(state)); 545 state.m = m; 546 state.ro = (struct route *)ro; 547 state.dst = (struct sockaddr *)dst; 548 549 error = ipsec6_output_tunnel(&state, sp, flags); 550 551 m = state.m; 552 ro = (struct route_in6 *)state.ro; 553 dst = (struct sockaddr_in6 *)state.dst; 554 if (error) { 555 /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 556 m0 = m = NULL; 557 m = NULL; 558 switch (error) { 559 case EHOSTUNREACH: 560 case ENETUNREACH: 561 case EMSGSIZE: 562 case ENOBUFS: 563 case ENOMEM: 564 break; 565 default: 566 printf("[%s:%d] (ipsec): error code %d\n", 567 __func__, __LINE__, error); 568 /* FALLTHROUGH */ 569 case ENOENT: 570 /* don't show these error codes to the user */ 571 error = 0; 572 break; 573 } 574 goto bad; 575 } else { 576 /* 577 * In the FAST IPSec case we have already 578 * re-injected the packet and it has been freed 579 * by the ipsec_done() function. So, just clean 580 * up after ourselves. 581 */ 582 m = NULL; 583 goto done; 584 } 585 586 exthdrs.ip6e_ip6 = m; 587 } 588#endif /* IPSEC */ 589 590 /* adjust pointer */ 591 ip6 = mtod(m, struct ip6_hdr *); 592 593 bzero(&dst_sa, sizeof(dst_sa)); 594 dst_sa.sin6_family = AF_INET6; 595 dst_sa.sin6_len = sizeof(dst_sa); 596 dst_sa.sin6_addr = ip6->ip6_dst; 597 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro, 598 &ifp, &rt, 0)) != 0) { 599 switch (error) { 600 case EHOSTUNREACH: 601 ip6stat.ip6s_noroute++; 602 break; 603 case EADDRNOTAVAIL: 604 default: 605 break; /* XXX statistics? */ 606 } 607 if (ifp != NULL) 608 in6_ifstat_inc(ifp, ifs6_out_discard); 609 goto bad; 610 } 611 if (rt == NULL) { 612 /* 613 * If in6_selectroute() does not return a route entry, 614 * dst may not have been updated. 615 */ 616 *dst = dst_sa; /* XXX */ 617 } 618 619 /* 620 * then rt (for unicast) and ifp must be non-NULL valid values. 621 */ 622 if ((flags & IPV6_FORWARDING) == 0) { 623 /* XXX: the FORWARDING flag can be set for mrouting. */ 624 in6_ifstat_inc(ifp, ifs6_out_request); 625 } 626 if (rt != NULL) { 627 ia = (struct in6_ifaddr *)(rt->rt_ifa); 628 rt->rt_use++; 629 } 630 631 /* 632 * The outgoing interface must be in the zone of source and 633 * destination addresses. We should use ia_ifp to support the 634 * case of sending packets to an address of our own. 635 */ 636 if (ia != NULL && ia->ia_ifp) 637 origifp = ia->ia_ifp; 638 else 639 origifp = ifp; 640 641 src0 = ip6->ip6_src; 642 if (in6_setscope(&src0, origifp, &zone)) 643 goto badscope; 644 bzero(&src_sa, sizeof(src_sa)); 645 src_sa.sin6_family = AF_INET6; 646 src_sa.sin6_len = sizeof(src_sa); 647 src_sa.sin6_addr = ip6->ip6_src; 648 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id) 649 goto badscope; 650 651 dst0 = ip6->ip6_dst; 652 if (in6_setscope(&dst0, origifp, &zone)) 653 goto badscope; 654 /* re-initialize to be sure */ 655 bzero(&dst_sa, sizeof(dst_sa)); 656 dst_sa.sin6_family = AF_INET6; 657 dst_sa.sin6_len = sizeof(dst_sa); 658 dst_sa.sin6_addr = ip6->ip6_dst; 659 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) { 660 goto badscope; 661 } 662 663 /* scope check is done. */ 664 goto routefound; 665 666 badscope: 667 ip6stat.ip6s_badscope++; 668 in6_ifstat_inc(origifp, ifs6_out_discard); 669 if (error == 0) 670 error = EHOSTUNREACH; /* XXX */ 671 goto bad; 672 673 routefound: 674 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 675 if (opt && opt->ip6po_nextroute.ro_rt) { 676 /* 677 * The nexthop is explicitly specified by the 678 * application. We assume the next hop is an IPv6 679 * address. 680 */ 681 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop; 682 } 683 else if ((rt->rt_flags & RTF_GATEWAY)) 684 dst = (struct sockaddr_in6 *)rt->rt_gateway; 685 } 686 687 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 688 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 689 } else { 690 struct in6_multi *in6m; 691 692 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 693 694 in6_ifstat_inc(ifp, ifs6_out_mcast); 695 696 /* 697 * Confirm that the outgoing interface supports multicast. 698 */ 699 if (!(ifp->if_flags & IFF_MULTICAST)) { 700 ip6stat.ip6s_noroute++; 701 in6_ifstat_inc(ifp, ifs6_out_discard); 702 error = ENETUNREACH; 703 goto bad; 704 } 705 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); 706 if (in6m != NULL && 707 (im6o == NULL || im6o->im6o_multicast_loop)) { 708 /* 709 * If we belong to the destination multicast group 710 * on the outgoing interface, and the caller did not 711 * forbid loopback, loop back a copy. 712 */ 713 ip6_mloopback(ifp, m, dst); 714 } else { 715 /* 716 * If we are acting as a multicast router, perform 717 * multicast forwarding as if the packet had just 718 * arrived on the interface to which we are about 719 * to send. The multicast forwarding function 720 * recursively calls this function, using the 721 * IPV6_FORWARDING flag to prevent infinite recursion. 722 * 723 * Multicasts that are looped back by ip6_mloopback(), 724 * above, will be forwarded by the ip6_input() routine, 725 * if necessary. 726 */ 727 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 728 /* 729 * XXX: ip6_mforward expects that rcvif is NULL 730 * when it is called from the originating path. 731 * However, it is not always the case, since 732 * some versions of MGETHDR() does not 733 * initialize the field. 734 */ 735 m->m_pkthdr.rcvif = NULL; 736 if (ip6_mforward(ip6, ifp, m) != 0) { 737 m_freem(m); 738 goto done; 739 } 740 } 741 } 742 /* 743 * Multicasts with a hoplimit of zero may be looped back, 744 * above, but must not be transmitted on a network. 745 * Also, multicasts addressed to the loopback interface 746 * are not sent -- the above call to ip6_mloopback() will 747 * loop back a copy if this host actually belongs to the 748 * destination group on the loopback interface. 749 */ 750 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) || 751 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) { 752 m_freem(m); 753 goto done; 754 } 755 } 756 757 /* 758 * Fill the outgoing inteface to tell the upper layer 759 * to increment per-interface statistics. 760 */ 761 if (ifpp) 762 *ifpp = ifp; 763 764 /* Determine path MTU. */ 765 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu, 766 &alwaysfrag)) != 0) 767 goto bad; 768 769 /* 770 * The caller of this function may specify to use the minimum MTU 771 * in some cases. 772 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU 773 * setting. The logic is a bit complicated; by default, unicast 774 * packets will follow path MTU while multicast packets will be sent at 775 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets 776 * including unicast ones will be sent at the minimum MTU. Multicast 777 * packets will always be sent at the minimum MTU unless 778 * IP6PO_MINMTU_DISABLE is explicitly specified. 779 * See RFC 3542 for more details. 780 */ 781 if (mtu > IPV6_MMTU) { 782 if ((flags & IPV6_MINMTU)) 783 mtu = IPV6_MMTU; 784 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) 785 mtu = IPV6_MMTU; 786 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 787 (opt == NULL || 788 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) { 789 mtu = IPV6_MMTU; 790 } 791 } 792 793 /* 794 * clear embedded scope identifiers if necessary. 795 * in6_clearscope will touch the addresses only when necessary. 796 */ 797 in6_clearscope(&ip6->ip6_src); 798 in6_clearscope(&ip6->ip6_dst); 799 800 /* 801 * If the outgoing packet contains a hop-by-hop options header, 802 * it must be examined and processed even by the source node. 803 * (RFC 2460, section 4.) 804 */ 805 if (exthdrs.ip6e_hbh) { 806 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 807 u_int32_t dummy; /* XXX unused */ 808 u_int32_t plen = 0; /* XXX: ip6_process will check the value */ 809 810#ifdef DIAGNOSTIC 811 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) 812 panic("ip6e_hbh is not continuous"); 813#endif 814 /* 815 * XXX: if we have to send an ICMPv6 error to the sender, 816 * we need the M_LOOP flag since icmp6_error() expects 817 * the IPv6 and the hop-by-hop options header are 818 * continuous unless the flag is set. 819 */ 820 m->m_flags |= M_LOOP; 821 m->m_pkthdr.rcvif = ifp; 822 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1), 823 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh), 824 &dummy, &plen) < 0) { 825 /* m was already freed at this point */ 826 error = EINVAL;/* better error? */ 827 goto done; 828 } 829 m->m_flags &= ~M_LOOP; /* XXX */ 830 m->m_pkthdr.rcvif = NULL; 831 } 832 833 /* Jump over all PFIL processing if hooks are not active. */ 834 if (!PFIL_HOOKED(&inet6_pfil_hook)) 835 goto passout; 836 837 odst = ip6->ip6_dst; 838 /* Run through list of hooks for output packets. */ 839 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp); 840 if (error != 0 || m == NULL) 841 goto done; 842 ip6 = mtod(m, struct ip6_hdr *); 843 844 /* See if destination IP address was changed by packet filter. */ 845 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) { 846 m->m_flags |= M_SKIP_FIREWALL; 847 /* If destination is now ourself drop to ip6_input(). */ 848 if (in6_localaddr(&ip6->ip6_dst)) { 849 if (m->m_pkthdr.rcvif == NULL) 850 m->m_pkthdr.rcvif = loif; 851 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 852 m->m_pkthdr.csum_flags |= 853 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 854 m->m_pkthdr.csum_data = 0xffff; 855 } 856 m->m_pkthdr.csum_flags |= 857 CSUM_IP_CHECKED | CSUM_IP_VALID; 858 error = netisr_queue(NETISR_IPV6, m); 859 goto done; 860 } else 861 goto again; /* Redo the routing table lookup. */ 862 } 863 864 /* XXX: IPFIREWALL_FORWARD */ 865 866passout: 867 /* 868 * Send the packet to the outgoing interface. 869 * If necessary, do IPv6 fragmentation before sending. 870 * 871 * the logic here is rather complex: 872 * 1: normal case (dontfrag == 0, alwaysfrag == 0) 873 * 1-a: send as is if tlen <= path mtu 874 * 1-b: fragment if tlen > path mtu 875 * 876 * 2: if user asks us not to fragment (dontfrag == 1) 877 * 2-a: send as is if tlen <= interface mtu 878 * 2-b: error if tlen > interface mtu 879 * 880 * 3: if we always need to attach fragment header (alwaysfrag == 1) 881 * always fragment 882 * 883 * 4: if dontfrag == 1 && alwaysfrag == 1 884 * error, as we cannot handle this conflicting request 885 */ 886 tlen = m->m_pkthdr.len; 887 888 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) 889 dontfrag = 1; 890 else 891 dontfrag = 0; 892 if (dontfrag && alwaysfrag) { /* case 4 */ 893 /* conflicting request - can't transmit */ 894 error = EMSGSIZE; 895 goto bad; 896 } 897 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */ 898 /* 899 * Even if the DONTFRAG option is specified, we cannot send the 900 * packet when the data length is larger than the MTU of the 901 * outgoing interface. 902 * Notify the error by sending IPV6_PATHMTU ancillary data as 903 * well as returning an error code (the latter is not described 904 * in the API spec.) 905 */ 906 u_int32_t mtu32; 907 struct ip6ctlparam ip6cp; 908 909 mtu32 = (u_int32_t)mtu; 910 bzero(&ip6cp, sizeof(ip6cp)); 911 ip6cp.ip6c_cmdarg = (void *)&mtu32; 912 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst, 913 (void *)&ip6cp); 914 915 error = EMSGSIZE; 916 goto bad; 917 } 918 919 /* 920 * transmit packet without fragmentation 921 */ 922 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */ 923 struct in6_ifaddr *ia6; 924 925 ip6 = mtod(m, struct ip6_hdr *); 926 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); 927 if (ia6) { 928 /* Record statistics for this interface address. */ 929 ia6->ia_ifa.if_opackets++; 930 ia6->ia_ifa.if_obytes += m->m_pkthdr.len; 931 } 932 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 933 goto done; 934 } 935 936 /* 937 * try to fragment the packet. case 1-b and 3 938 */ 939 if (mtu < IPV6_MMTU) { 940 /* path MTU cannot be less than IPV6_MMTU */ 941 error = EMSGSIZE; 942 in6_ifstat_inc(ifp, ifs6_out_fragfail); 943 goto bad; 944 } else if (ip6->ip6_plen == 0) { 945 /* jumbo payload cannot be fragmented */ 946 error = EMSGSIZE; 947 in6_ifstat_inc(ifp, ifs6_out_fragfail); 948 goto bad; 949 } else { 950 struct mbuf **mnext, *m_frgpart; 951 struct ip6_frag *ip6f; 952 u_int32_t id = htonl(ip6_randomid()); 953 u_char nextproto; 954 955 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len; 956 957 /* 958 * Too large for the destination or interface; 959 * fragment if possible. 960 * Must be able to put at least 8 bytes per fragment. 961 */ 962 hlen = unfragpartlen; 963 if (mtu > IPV6_MAXPACKET) 964 mtu = IPV6_MAXPACKET; 965 966 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 967 if (len < 8) { 968 error = EMSGSIZE; 969 in6_ifstat_inc(ifp, ifs6_out_fragfail); 970 goto bad; 971 } 972 973 /* 974 * Verify that we have any chance at all of being able to queue 975 * the packet or packet fragments 976 */ 977 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen) 978 < tlen /* - hlen */)) { 979 error = ENOBUFS; 980 ip6stat.ip6s_odropped++; 981 goto bad; 982 } 983 984 mnext = &m->m_nextpkt; 985 986 /* 987 * Change the next header field of the last header in the 988 * unfragmentable part. 989 */ 990 if (exthdrs.ip6e_rthdr) { 991 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 992 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 993 } else if (exthdrs.ip6e_dest1) { 994 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 995 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 996 } else if (exthdrs.ip6e_hbh) { 997 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 998 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 999 } else { 1000 nextproto = ip6->ip6_nxt; 1001 ip6->ip6_nxt = IPPROTO_FRAGMENT; 1002 } 1003 1004 /* 1005 * Loop through length of segment after first fragment, 1006 * make new header and copy data of each part and link onto 1007 * chain. 1008 */ 1009 m0 = m; 1010 for (off = hlen; off < tlen; off += len) { 1011 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1012 if (!m) { 1013 error = ENOBUFS; 1014 ip6stat.ip6s_odropped++; 1015 goto sendorfree; 1016 } 1017 m->m_pkthdr.rcvif = NULL; 1018 m->m_flags = m0->m_flags & M_COPYFLAGS; 1019 *mnext = m; 1020 mnext = &m->m_nextpkt; 1021 m->m_data += max_linkhdr; 1022 mhip6 = mtod(m, struct ip6_hdr *); 1023 *mhip6 = *ip6; 1024 m->m_len = sizeof(*mhip6); 1025 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 1026 if (error) { 1027 ip6stat.ip6s_odropped++; 1028 goto sendorfree; 1029 } 1030 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7)); 1031 if (off + len >= tlen) 1032 len = tlen - off; 1033 else 1034 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 1035 mhip6->ip6_plen = htons((u_short)(len + hlen + 1036 sizeof(*ip6f) - sizeof(struct ip6_hdr))); 1037 if ((m_frgpart = m_copy(m0, off, len)) == 0) { 1038 error = ENOBUFS; 1039 ip6stat.ip6s_odropped++; 1040 goto sendorfree; 1041 } 1042 m_cat(m, m_frgpart); 1043 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 1044 m->m_pkthdr.rcvif = NULL; 1045 ip6f->ip6f_reserved = 0; 1046 ip6f->ip6f_ident = id; 1047 ip6f->ip6f_nxt = nextproto; 1048 ip6stat.ip6s_ofragments++; 1049 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 1050 } 1051 1052 in6_ifstat_inc(ifp, ifs6_out_fragok); 1053 } 1054 1055 /* 1056 * Remove leading garbages. 1057 */ 1058sendorfree: 1059 m = m0->m_nextpkt; 1060 m0->m_nextpkt = 0; 1061 m_freem(m0); 1062 for (m0 = m; m; m = m0) { 1063 m0 = m->m_nextpkt; 1064 m->m_nextpkt = 0; 1065 if (error == 0) { 1066 /* Record statistics for this interface address. */ 1067 if (ia) { 1068 ia->ia_ifa.if_opackets++; 1069 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1070 } 1071 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 1072 } else 1073 m_freem(m); 1074 } 1075 1076 if (error == 0) 1077 ip6stat.ip6s_fragmented++; 1078 1079done: 1080 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */ 1081 RTFREE(ro->ro_rt); 1082 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) { 1083 RTFREE(ro_pmtu->ro_rt); 1084 }
|
1085 1086 return (error); 1087 1088freehdrs: 1089 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1090 m_freem(exthdrs.ip6e_dest1); 1091 m_freem(exthdrs.ip6e_rthdr); 1092 m_freem(exthdrs.ip6e_dest2); 1093 /* FALLTHROUGH */ 1094bad: 1095 if (m) 1096 m_freem(m); 1097 goto done; 1098} 1099 1100static int 1101ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen) 1102{ 1103 struct mbuf *m; 1104 1105 if (hlen > MCLBYTES) 1106 return (ENOBUFS); /* XXX */ 1107 1108 MGET(m, M_DONTWAIT, MT_DATA); 1109 if (!m) 1110 return (ENOBUFS); 1111 1112 if (hlen > MLEN) { 1113 MCLGET(m, M_DONTWAIT); 1114 if ((m->m_flags & M_EXT) == 0) { 1115 m_free(m); 1116 return (ENOBUFS); 1117 } 1118 } 1119 m->m_len = hlen; 1120 if (hdr) 1121 bcopy(hdr, mtod(m, caddr_t), hlen); 1122 1123 *mp = m; 1124 return (0); 1125} 1126 1127/* 1128 * Insert jumbo payload option. 1129 */ 1130static int 1131ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) 1132{ 1133 struct mbuf *mopt; 1134 u_char *optbuf; 1135 u_int32_t v; 1136 1137#define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1138 1139 /* 1140 * If there is no hop-by-hop options header, allocate new one. 1141 * If there is one but it doesn't have enough space to store the 1142 * jumbo payload option, allocate a cluster to store the whole options. 1143 * Otherwise, use it to store the options. 1144 */ 1145 if (exthdrs->ip6e_hbh == 0) { 1146 MGET(mopt, M_DONTWAIT, MT_DATA); 1147 if (mopt == 0) 1148 return (ENOBUFS); 1149 mopt->m_len = JUMBOOPTLEN; 1150 optbuf = mtod(mopt, u_char *); 1151 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1152 exthdrs->ip6e_hbh = mopt; 1153 } else { 1154 struct ip6_hbh *hbh; 1155 1156 mopt = exthdrs->ip6e_hbh; 1157 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1158 /* 1159 * XXX assumption: 1160 * - exthdrs->ip6e_hbh is not referenced from places 1161 * other than exthdrs. 1162 * - exthdrs->ip6e_hbh is not an mbuf chain. 1163 */ 1164 int oldoptlen = mopt->m_len; 1165 struct mbuf *n; 1166 1167 /* 1168 * XXX: give up if the whole (new) hbh header does 1169 * not fit even in an mbuf cluster. 1170 */ 1171 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1172 return (ENOBUFS); 1173 1174 /* 1175 * As a consequence, we must always prepare a cluster 1176 * at this point. 1177 */ 1178 MGET(n, M_DONTWAIT, MT_DATA); 1179 if (n) { 1180 MCLGET(n, M_DONTWAIT); 1181 if ((n->m_flags & M_EXT) == 0) { 1182 m_freem(n); 1183 n = NULL; 1184 } 1185 } 1186 if (!n) 1187 return (ENOBUFS); 1188 n->m_len = oldoptlen + JUMBOOPTLEN; 1189 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1190 oldoptlen); 1191 optbuf = mtod(n, caddr_t) + oldoptlen; 1192 m_freem(mopt); 1193 mopt = exthdrs->ip6e_hbh = n; 1194 } else { 1195 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1196 mopt->m_len += JUMBOOPTLEN; 1197 } 1198 optbuf[0] = IP6OPT_PADN; 1199 optbuf[1] = 1; 1200 1201 /* 1202 * Adjust the header length according to the pad and 1203 * the jumbo payload option. 1204 */ 1205 hbh = mtod(mopt, struct ip6_hbh *); 1206 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1207 } 1208 1209 /* fill in the option. */ 1210 optbuf[2] = IP6OPT_JUMBO; 1211 optbuf[3] = 4; 1212 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1213 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1214 1215 /* finally, adjust the packet header length */ 1216 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1217 1218 return (0); 1219#undef JUMBOOPTLEN 1220} 1221 1222/* 1223 * Insert fragment header and copy unfragmentable header portions. 1224 */ 1225static int 1226ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, 1227 struct ip6_frag **frghdrp) 1228{ 1229 struct mbuf *n, *mlast; 1230 1231 if (hlen > sizeof(struct ip6_hdr)) { 1232 n = m_copym(m0, sizeof(struct ip6_hdr), 1233 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1234 if (n == 0) 1235 return (ENOBUFS); 1236 m->m_next = n; 1237 } else 1238 n = m; 1239 1240 /* Search for the last mbuf of unfragmentable part. */ 1241 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1242 ; 1243 1244 if ((mlast->m_flags & M_EXT) == 0 && 1245 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1246 /* use the trailing space of the last mbuf for the fragment hdr */ 1247 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) + 1248 mlast->m_len); 1249 mlast->m_len += sizeof(struct ip6_frag); 1250 m->m_pkthdr.len += sizeof(struct ip6_frag); 1251 } else { 1252 /* allocate a new mbuf for the fragment header */ 1253 struct mbuf *mfrg; 1254 1255 MGET(mfrg, M_DONTWAIT, MT_DATA); 1256 if (mfrg == 0) 1257 return (ENOBUFS); 1258 mfrg->m_len = sizeof(struct ip6_frag); 1259 *frghdrp = mtod(mfrg, struct ip6_frag *); 1260 mlast->m_next = mfrg; 1261 } 1262 1263 return (0); 1264} 1265 1266static int 1267ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro, 1268 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup, 1269 int *alwaysfragp) 1270{ 1271 u_int32_t mtu = 0; 1272 int alwaysfrag = 0; 1273 int error = 0; 1274 1275 if (ro_pmtu != ro) { 1276 /* The first hop and the final destination may differ. */ 1277 struct sockaddr_in6 *sa6_dst = 1278 (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 1279 if (ro_pmtu->ro_rt && 1280 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 || 1281 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) { 1282 RTFREE(ro_pmtu->ro_rt); 1283 ro_pmtu->ro_rt = (struct rtentry *)NULL; 1284 } 1285 if (ro_pmtu->ro_rt == NULL) { 1286 bzero(sa6_dst, sizeof(*sa6_dst)); 1287 sa6_dst->sin6_family = AF_INET6; 1288 sa6_dst->sin6_len = sizeof(struct sockaddr_in6); 1289 sa6_dst->sin6_addr = *dst; 1290 1291 rtalloc((struct route *)ro_pmtu); 1292 } 1293 } 1294 if (ro_pmtu->ro_rt) { 1295 u_int32_t ifmtu; 1296 struct in_conninfo inc; 1297 1298 bzero(&inc, sizeof(inc)); 1299 inc.inc_flags = 1; /* IPv6 */ 1300 inc.inc6_faddr = *dst; 1301 1302 if (ifp == NULL) 1303 ifp = ro_pmtu->ro_rt->rt_ifp; 1304 ifmtu = IN6_LINKMTU(ifp); 1305 mtu = tcp_hc_getmtu(&inc); 1306 if (mtu) 1307 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu); 1308 else 1309 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu; 1310 if (mtu == 0) 1311 mtu = ifmtu; 1312 else if (mtu < IPV6_MMTU) { 1313 /* 1314 * RFC2460 section 5, last paragraph: 1315 * if we record ICMPv6 too big message with 1316 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU 1317 * or smaller, with framgent header attached. 1318 * (fragment header is needed regardless from the 1319 * packet size, for translators to identify packets) 1320 */ 1321 alwaysfrag = 1; 1322 mtu = IPV6_MMTU; 1323 } else if (mtu > ifmtu) { 1324 /* 1325 * The MTU on the route is larger than the MTU on 1326 * the interface! This shouldn't happen, unless the 1327 * MTU of the interface has been changed after the 1328 * interface was brought up. Change the MTU in the 1329 * route to match the interface MTU (as long as the 1330 * field isn't locked). 1331 */ 1332 mtu = ifmtu; 1333 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; 1334 } 1335 } else if (ifp) { 1336 mtu = IN6_LINKMTU(ifp); 1337 } else 1338 error = EHOSTUNREACH; /* XXX */ 1339 1340 *mtup = mtu; 1341 if (alwaysfragp) 1342 *alwaysfragp = alwaysfrag; 1343 return (error); 1344} 1345 1346/* 1347 * IP6 socket option processing. 1348 */ 1349int 1350ip6_ctloutput(struct socket *so, struct sockopt *sopt) 1351{ 1352 int optdatalen, uproto; 1353 void *optdata; 1354 struct inpcb *in6p = sotoinpcb(so); 1355 int error, optval; 1356 int level, op, optname; 1357 int optlen; 1358 struct thread *td; 1359 1360 if (sopt) { 1361 level = sopt->sopt_level; 1362 op = sopt->sopt_dir; 1363 optname = sopt->sopt_name; 1364 optlen = sopt->sopt_valsize; 1365 td = sopt->sopt_td; 1366 } else { 1367 panic("ip6_ctloutput: arg soopt is NULL"); 1368 } 1369 error = optval = 0; 1370 1371 uproto = (int)so->so_proto->pr_protocol; 1372 1373 if (level == IPPROTO_IPV6) { 1374 switch (op) { 1375 1376 case SOPT_SET: 1377 switch (optname) { 1378 case IPV6_2292PKTOPTIONS: 1379#ifdef IPV6_PKTOPTIONS 1380 case IPV6_PKTOPTIONS: 1381#endif 1382 { 1383 struct mbuf *m; 1384 1385 error = soopt_getm(sopt, &m); /* XXX */ 1386 if (error != 0) 1387 break; 1388 error = soopt_mcopyin(sopt, m); /* XXX */ 1389 if (error != 0) 1390 break; 1391 error = ip6_pcbopts(&in6p->in6p_outputopts, 1392 m, so, sopt); 1393 m_freem(m); /* XXX */ 1394 break; 1395 } 1396 1397 /* 1398 * Use of some Hop-by-Hop options or some 1399 * Destination options, might require special 1400 * privilege. That is, normal applications 1401 * (without special privilege) might be forbidden 1402 * from setting certain options in outgoing packets, 1403 * and might never see certain options in received 1404 * packets. [RFC 2292 Section 6] 1405 * KAME specific note: 1406 * KAME prevents non-privileged users from sending or 1407 * receiving ANY hbh/dst options in order to avoid 1408 * overhead of parsing options in the kernel. 1409 */ 1410 case IPV6_RECVHOPOPTS: 1411 case IPV6_RECVDSTOPTS: 1412 case IPV6_RECVRTHDRDSTOPTS: 1413 if (td != NULL) { 1414 error = priv_check(td, 1415 PRIV_NETINET_SETHDROPTS); 1416 if (error) 1417 break; 1418 } 1419 /* FALLTHROUGH */ 1420 case IPV6_UNICAST_HOPS: 1421 case IPV6_HOPLIMIT: 1422 case IPV6_FAITH: 1423 1424 case IPV6_RECVPKTINFO: 1425 case IPV6_RECVHOPLIMIT: 1426 case IPV6_RECVRTHDR: 1427 case IPV6_RECVPATHMTU: 1428 case IPV6_RECVTCLASS: 1429 case IPV6_V6ONLY: 1430 case IPV6_AUTOFLOWLABEL: 1431 if (optlen != sizeof(int)) { 1432 error = EINVAL; 1433 break; 1434 } 1435 error = sooptcopyin(sopt, &optval, 1436 sizeof optval, sizeof optval); 1437 if (error) 1438 break; 1439 switch (optname) { 1440 1441 case IPV6_UNICAST_HOPS: 1442 if (optval < -1 || optval >= 256) 1443 error = EINVAL; 1444 else { 1445 /* -1 = kernel default */ 1446 in6p->in6p_hops = optval; 1447 if ((in6p->in6p_vflag & 1448 INP_IPV4) != 0) 1449 in6p->inp_ip_ttl = optval; 1450 } 1451 break; 1452#define OPTSET(bit) \ 1453do { \ 1454 if (optval) \ 1455 in6p->in6p_flags |= (bit); \ 1456 else \ 1457 in6p->in6p_flags &= ~(bit); \ 1458} while (/*CONSTCOND*/ 0) 1459#define OPTSET2292(bit) \ 1460do { \ 1461 in6p->in6p_flags |= IN6P_RFC2292; \ 1462 if (optval) \ 1463 in6p->in6p_flags |= (bit); \ 1464 else \ 1465 in6p->in6p_flags &= ~(bit); \ 1466} while (/*CONSTCOND*/ 0) 1467#define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) 1468 1469 case IPV6_RECVPKTINFO: 1470 /* cannot mix with RFC2292 */ 1471 if (OPTBIT(IN6P_RFC2292)) { 1472 error = EINVAL; 1473 break; 1474 } 1475 OPTSET(IN6P_PKTINFO); 1476 break; 1477 1478 case IPV6_HOPLIMIT: 1479 { 1480 struct ip6_pktopts **optp; 1481 1482 /* cannot mix with RFC2292 */ 1483 if (OPTBIT(IN6P_RFC2292)) { 1484 error = EINVAL; 1485 break; 1486 } 1487 optp = &in6p->in6p_outputopts; 1488 error = ip6_pcbopt(IPV6_HOPLIMIT, 1489 (u_char *)&optval, sizeof(optval), 1490 optp, (td != NULL) ? td->td_ucred : 1491 NULL, uproto); 1492 break; 1493 } 1494 1495 case IPV6_RECVHOPLIMIT: 1496 /* cannot mix with RFC2292 */ 1497 if (OPTBIT(IN6P_RFC2292)) { 1498 error = EINVAL; 1499 break; 1500 } 1501 OPTSET(IN6P_HOPLIMIT); 1502 break; 1503 1504 case IPV6_RECVHOPOPTS: 1505 /* cannot mix with RFC2292 */ 1506 if (OPTBIT(IN6P_RFC2292)) { 1507 error = EINVAL; 1508 break; 1509 } 1510 OPTSET(IN6P_HOPOPTS); 1511 break; 1512 1513 case IPV6_RECVDSTOPTS: 1514 /* cannot mix with RFC2292 */ 1515 if (OPTBIT(IN6P_RFC2292)) { 1516 error = EINVAL; 1517 break; 1518 } 1519 OPTSET(IN6P_DSTOPTS); 1520 break; 1521 1522 case IPV6_RECVRTHDRDSTOPTS: 1523 /* cannot mix with RFC2292 */ 1524 if (OPTBIT(IN6P_RFC2292)) { 1525 error = EINVAL; 1526 break; 1527 } 1528 OPTSET(IN6P_RTHDRDSTOPTS); 1529 break; 1530 1531 case IPV6_RECVRTHDR: 1532 /* cannot mix with RFC2292 */ 1533 if (OPTBIT(IN6P_RFC2292)) { 1534 error = EINVAL; 1535 break; 1536 } 1537 OPTSET(IN6P_RTHDR); 1538 break; 1539 1540 case IPV6_FAITH: 1541 OPTSET(IN6P_FAITH); 1542 break; 1543 1544 case IPV6_RECVPATHMTU: 1545 /* 1546 * We ignore this option for TCP 1547 * sockets. 1548 * (RFC3542 leaves this case 1549 * unspecified.) 1550 */ 1551 if (uproto != IPPROTO_TCP) 1552 OPTSET(IN6P_MTU); 1553 break; 1554 1555 case IPV6_V6ONLY: 1556 /* 1557 * make setsockopt(IPV6_V6ONLY) 1558 * available only prior to bind(2). 1559 * see ipng mailing list, Jun 22 2001. 1560 */ 1561 if (in6p->in6p_lport || 1562 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1563 error = EINVAL; 1564 break; 1565 } 1566 OPTSET(IN6P_IPV6_V6ONLY); 1567 if (optval) 1568 in6p->in6p_vflag &= ~INP_IPV4; 1569 else 1570 in6p->in6p_vflag |= INP_IPV4; 1571 break; 1572 case IPV6_RECVTCLASS: 1573 /* cannot mix with RFC2292 XXX */ 1574 if (OPTBIT(IN6P_RFC2292)) { 1575 error = EINVAL; 1576 break; 1577 } 1578 OPTSET(IN6P_TCLASS); 1579 break; 1580 case IPV6_AUTOFLOWLABEL: 1581 OPTSET(IN6P_AUTOFLOWLABEL); 1582 break; 1583 1584 } 1585 break; 1586 1587 case IPV6_TCLASS: 1588 case IPV6_DONTFRAG: 1589 case IPV6_USE_MIN_MTU: 1590 case IPV6_PREFER_TEMPADDR: 1591 if (optlen != sizeof(optval)) { 1592 error = EINVAL; 1593 break; 1594 } 1595 error = sooptcopyin(sopt, &optval, 1596 sizeof optval, sizeof optval); 1597 if (error) 1598 break; 1599 { 1600 struct ip6_pktopts **optp; 1601 optp = &in6p->in6p_outputopts; 1602 error = ip6_pcbopt(optname, 1603 (u_char *)&optval, sizeof(optval), 1604 optp, (td != NULL) ? td->td_ucred : 1605 NULL, uproto); 1606 break; 1607 } 1608 1609 case IPV6_2292PKTINFO: 1610 case IPV6_2292HOPLIMIT: 1611 case IPV6_2292HOPOPTS: 1612 case IPV6_2292DSTOPTS: 1613 case IPV6_2292RTHDR: 1614 /* RFC 2292 */ 1615 if (optlen != sizeof(int)) { 1616 error = EINVAL; 1617 break; 1618 } 1619 error = sooptcopyin(sopt, &optval, 1620 sizeof optval, sizeof optval); 1621 if (error) 1622 break; 1623 switch (optname) { 1624 case IPV6_2292PKTINFO: 1625 OPTSET2292(IN6P_PKTINFO); 1626 break; 1627 case IPV6_2292HOPLIMIT: 1628 OPTSET2292(IN6P_HOPLIMIT); 1629 break; 1630 case IPV6_2292HOPOPTS: 1631 /* 1632 * Check super-user privilege. 1633 * See comments for IPV6_RECVHOPOPTS. 1634 */ 1635 if (td != NULL) { 1636 error = priv_check(td, 1637 PRIV_NETINET_SETHDROPTS); 1638 if (error) 1639 return (error); 1640 } 1641 OPTSET2292(IN6P_HOPOPTS); 1642 break; 1643 case IPV6_2292DSTOPTS: 1644 if (td != NULL) { 1645 error = priv_check(td, 1646 PRIV_NETINET_SETHDROPTS); 1647 if (error) 1648 return (error); 1649 } 1650 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1651 break; 1652 case IPV6_2292RTHDR: 1653 OPTSET2292(IN6P_RTHDR); 1654 break; 1655 } 1656 break; 1657 case IPV6_PKTINFO: 1658 case IPV6_HOPOPTS: 1659 case IPV6_RTHDR: 1660 case IPV6_DSTOPTS: 1661 case IPV6_RTHDRDSTOPTS: 1662 case IPV6_NEXTHOP: 1663 { 1664 /* new advanced API (RFC3542) */ 1665 u_char *optbuf; 1666 u_char optbuf_storage[MCLBYTES]; 1667 int optlen; 1668 struct ip6_pktopts **optp; 1669 1670 /* cannot mix with RFC2292 */ 1671 if (OPTBIT(IN6P_RFC2292)) { 1672 error = EINVAL; 1673 break; 1674 } 1675 1676 /* 1677 * We only ensure valsize is not too large 1678 * here. Further validation will be done 1679 * later. 1680 */ 1681 error = sooptcopyin(sopt, optbuf_storage, 1682 sizeof(optbuf_storage), 0); 1683 if (error) 1684 break; 1685 optlen = sopt->sopt_valsize; 1686 optbuf = optbuf_storage; 1687 optp = &in6p->in6p_outputopts; 1688 error = ip6_pcbopt(optname, optbuf, optlen, 1689 optp, (td != NULL) ? td->td_ucred : NULL, 1690 uproto); 1691 break; 1692 } 1693#undef OPTSET 1694 1695 case IPV6_MULTICAST_IF: 1696 case IPV6_MULTICAST_HOPS: 1697 case IPV6_MULTICAST_LOOP: 1698 case IPV6_JOIN_GROUP: 1699 case IPV6_LEAVE_GROUP: 1700 { 1701 if (sopt->sopt_valsize > MLEN) { 1702 error = EMSGSIZE; 1703 break; 1704 } 1705 /* XXX */ 1706 } 1707 /* FALLTHROUGH */ 1708 { 1709 struct mbuf *m; 1710 1711 if (sopt->sopt_valsize > MCLBYTES) { 1712 error = EMSGSIZE; 1713 break; 1714 } 1715 /* XXX */ 1716 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA); 1717 if (m == 0) { 1718 error = ENOBUFS; 1719 break; 1720 } 1721 if (sopt->sopt_valsize > MLEN) { 1722 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT); 1723 if ((m->m_flags & M_EXT) == 0) { 1724 m_free(m); 1725 error = ENOBUFS; 1726 break; 1727 } 1728 } 1729 m->m_len = sopt->sopt_valsize; 1730 error = sooptcopyin(sopt, mtod(m, char *), 1731 m->m_len, m->m_len); 1732 if (error) { 1733 (void)m_free(m); 1734 break; 1735 } 1736 error = ip6_setmoptions(sopt->sopt_name, 1737 &in6p->in6p_moptions, 1738 m); 1739 (void)m_free(m); 1740 } 1741 break; 1742 1743 case IPV6_PORTRANGE: 1744 error = sooptcopyin(sopt, &optval, 1745 sizeof optval, sizeof optval); 1746 if (error) 1747 break; 1748 1749 switch (optval) { 1750 case IPV6_PORTRANGE_DEFAULT: 1751 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1752 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1753 break; 1754 1755 case IPV6_PORTRANGE_HIGH: 1756 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1757 in6p->in6p_flags |= IN6P_HIGHPORT; 1758 break; 1759 1760 case IPV6_PORTRANGE_LOW: 1761 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1762 in6p->in6p_flags |= IN6P_LOWPORT; 1763 break; 1764 1765 default: 1766 error = EINVAL; 1767 break; 1768 } 1769 break; 1770 1771#ifdef IPSEC 1772 case IPV6_IPSEC_POLICY: 1773 { 1774 caddr_t req; 1775 struct mbuf *m; 1776 1777 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1778 break; 1779 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1780 break; 1781 req = mtod(m, caddr_t); 1782 error = ipsec6_set_policy(in6p, optname, req, 1783 m->m_len, (sopt->sopt_td != NULL) ? 1784 sopt->sopt_td->td_ucred : NULL); 1785 m_freem(m); 1786 break; 1787 } 1788#endif /* IPSEC */ 1789 1790 default: 1791 error = ENOPROTOOPT; 1792 break; 1793 } 1794 break; 1795 1796 case SOPT_GET: 1797 switch (optname) { 1798 1799 case IPV6_2292PKTOPTIONS: 1800#ifdef IPV6_PKTOPTIONS 1801 case IPV6_PKTOPTIONS: 1802#endif 1803 /* 1804 * RFC3542 (effectively) deprecated the 1805 * semantics of the 2292-style pktoptions. 1806 * Since it was not reliable in nature (i.e., 1807 * applications had to expect the lack of some 1808 * information after all), it would make sense 1809 * to simplify this part by always returning 1810 * empty data. 1811 */ 1812 sopt->sopt_valsize = 0; 1813 break; 1814 1815 case IPV6_RECVHOPOPTS: 1816 case IPV6_RECVDSTOPTS: 1817 case IPV6_RECVRTHDRDSTOPTS: 1818 case IPV6_UNICAST_HOPS: 1819 case IPV6_RECVPKTINFO: 1820 case IPV6_RECVHOPLIMIT: 1821 case IPV6_RECVRTHDR: 1822 case IPV6_RECVPATHMTU: 1823 1824 case IPV6_FAITH: 1825 case IPV6_V6ONLY: 1826 case IPV6_PORTRANGE: 1827 case IPV6_RECVTCLASS: 1828 case IPV6_AUTOFLOWLABEL: 1829 switch (optname) { 1830 1831 case IPV6_RECVHOPOPTS: 1832 optval = OPTBIT(IN6P_HOPOPTS); 1833 break; 1834 1835 case IPV6_RECVDSTOPTS: 1836 optval = OPTBIT(IN6P_DSTOPTS); 1837 break; 1838 1839 case IPV6_RECVRTHDRDSTOPTS: 1840 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 1841 break; 1842 1843 case IPV6_UNICAST_HOPS: 1844 optval = in6p->in6p_hops; 1845 break; 1846 1847 case IPV6_RECVPKTINFO: 1848 optval = OPTBIT(IN6P_PKTINFO); 1849 break; 1850 1851 case IPV6_RECVHOPLIMIT: 1852 optval = OPTBIT(IN6P_HOPLIMIT); 1853 break; 1854 1855 case IPV6_RECVRTHDR: 1856 optval = OPTBIT(IN6P_RTHDR); 1857 break; 1858 1859 case IPV6_RECVPATHMTU: 1860 optval = OPTBIT(IN6P_MTU); 1861 break; 1862 1863 case IPV6_FAITH: 1864 optval = OPTBIT(IN6P_FAITH); 1865 break; 1866 1867 case IPV6_V6ONLY: 1868 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1869 break; 1870 1871 case IPV6_PORTRANGE: 1872 { 1873 int flags; 1874 flags = in6p->in6p_flags; 1875 if (flags & IN6P_HIGHPORT) 1876 optval = IPV6_PORTRANGE_HIGH; 1877 else if (flags & IN6P_LOWPORT) 1878 optval = IPV6_PORTRANGE_LOW; 1879 else 1880 optval = 0; 1881 break; 1882 } 1883 case IPV6_RECVTCLASS: 1884 optval = OPTBIT(IN6P_TCLASS); 1885 break; 1886 1887 case IPV6_AUTOFLOWLABEL: 1888 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 1889 break; 1890 } 1891 if (error) 1892 break; 1893 error = sooptcopyout(sopt, &optval, 1894 sizeof optval); 1895 break; 1896 1897 case IPV6_PATHMTU: 1898 { 1899 u_long pmtu = 0; 1900 struct ip6_mtuinfo mtuinfo; 1901 struct route_in6 sro; 1902 1903 bzero(&sro, sizeof(sro)); 1904 1905 if (!(so->so_state & SS_ISCONNECTED)) 1906 return (ENOTCONN); 1907 /* 1908 * XXX: we dot not consider the case of source 1909 * routing, or optional information to specify 1910 * the outgoing interface. 1911 */ 1912 error = ip6_getpmtu(&sro, NULL, NULL, 1913 &in6p->in6p_faddr, &pmtu, NULL); 1914 if (sro.ro_rt) 1915 RTFREE(sro.ro_rt); 1916 if (error) 1917 break; 1918 if (pmtu > IPV6_MAXPACKET) 1919 pmtu = IPV6_MAXPACKET; 1920 1921 bzero(&mtuinfo, sizeof(mtuinfo)); 1922 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 1923 optdata = (void *)&mtuinfo; 1924 optdatalen = sizeof(mtuinfo); 1925 error = sooptcopyout(sopt, optdata, 1926 optdatalen); 1927 break; 1928 } 1929 1930 case IPV6_2292PKTINFO: 1931 case IPV6_2292HOPLIMIT: 1932 case IPV6_2292HOPOPTS: 1933 case IPV6_2292RTHDR: 1934 case IPV6_2292DSTOPTS: 1935 switch (optname) { 1936 case IPV6_2292PKTINFO: 1937 optval = OPTBIT(IN6P_PKTINFO); 1938 break; 1939 case IPV6_2292HOPLIMIT: 1940 optval = OPTBIT(IN6P_HOPLIMIT); 1941 break; 1942 case IPV6_2292HOPOPTS: 1943 optval = OPTBIT(IN6P_HOPOPTS); 1944 break; 1945 case IPV6_2292RTHDR: 1946 optval = OPTBIT(IN6P_RTHDR); 1947 break; 1948 case IPV6_2292DSTOPTS: 1949 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1950 break; 1951 } 1952 error = sooptcopyout(sopt, &optval, 1953 sizeof optval); 1954 break; 1955 case IPV6_PKTINFO: 1956 case IPV6_HOPOPTS: 1957 case IPV6_RTHDR: 1958 case IPV6_DSTOPTS: 1959 case IPV6_RTHDRDSTOPTS: 1960 case IPV6_NEXTHOP: 1961 case IPV6_TCLASS: 1962 case IPV6_DONTFRAG: 1963 case IPV6_USE_MIN_MTU: 1964 case IPV6_PREFER_TEMPADDR: 1965 error = ip6_getpcbopt(in6p->in6p_outputopts, 1966 optname, sopt); 1967 break; 1968 1969 case IPV6_MULTICAST_IF: 1970 case IPV6_MULTICAST_HOPS: 1971 case IPV6_MULTICAST_LOOP: 1972 case IPV6_JOIN_GROUP: 1973 case IPV6_LEAVE_GROUP: 1974 { 1975 struct mbuf *m; 1976 error = ip6_getmoptions(sopt->sopt_name, 1977 in6p->in6p_moptions, &m); 1978 if (error == 0) 1979 error = sooptcopyout(sopt, 1980 mtod(m, char *), m->m_len); 1981 m_freem(m); 1982 } 1983 break; 1984 1985#ifdef IPSEC 1986 case IPV6_IPSEC_POLICY: 1987 { 1988 caddr_t req = NULL; 1989 size_t len = 0; 1990 struct mbuf *m = NULL; 1991 struct mbuf **mp = &m; 1992 size_t ovalsize = sopt->sopt_valsize; 1993 caddr_t oval = (caddr_t)sopt->sopt_val; 1994 1995 error = soopt_getm(sopt, &m); /* XXX */ 1996 if (error != 0) 1997 break; 1998 error = soopt_mcopyin(sopt, m); /* XXX */ 1999 if (error != 0) 2000 break; 2001 sopt->sopt_valsize = ovalsize; 2002 sopt->sopt_val = oval; 2003 if (m) { 2004 req = mtod(m, caddr_t); 2005 len = m->m_len; 2006 } 2007 error = ipsec6_get_policy(in6p, req, len, mp); 2008 if (error == 0) 2009 error = soopt_mcopyout(sopt, m); /* XXX */ 2010 if (error == 0 && m) 2011 m_freem(m); 2012 break; 2013 } 2014#endif /* IPSEC */ 2015 2016 default: 2017 error = ENOPROTOOPT; 2018 break; 2019 } 2020 break; 2021 } 2022 } else { /* level != IPPROTO_IPV6 */ 2023 error = EINVAL; 2024 } 2025 return (error); 2026} 2027 2028int 2029ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt) 2030{ 2031 int error = 0, optval, optlen; 2032 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 2033 struct in6pcb *in6p = sotoin6pcb(so); 2034 int level, op, optname; 2035 2036 if (sopt) { 2037 level = sopt->sopt_level; 2038 op = sopt->sopt_dir; 2039 optname = sopt->sopt_name; 2040 optlen = sopt->sopt_valsize; 2041 } else 2042 panic("ip6_raw_ctloutput: arg soopt is NULL"); 2043 2044 if (level != IPPROTO_IPV6) { 2045 return (EINVAL); 2046 } 2047 2048 switch (optname) { 2049 case IPV6_CHECKSUM: 2050 /* 2051 * For ICMPv6 sockets, no modification allowed for checksum 2052 * offset, permit "no change" values to help existing apps. 2053 * 2054 * RFC3542 says: "An attempt to set IPV6_CHECKSUM 2055 * for an ICMPv6 socket will fail." 2056 * The current behavior does not meet RFC3542. 2057 */ 2058 switch (op) { 2059 case SOPT_SET: 2060 if (optlen != sizeof(int)) { 2061 error = EINVAL; 2062 break; 2063 } 2064 error = sooptcopyin(sopt, &optval, sizeof(optval), 2065 sizeof(optval)); 2066 if (error) 2067 break; 2068 if ((optval % 2) != 0) { 2069 /* the API assumes even offset values */ 2070 error = EINVAL; 2071 } else if (so->so_proto->pr_protocol == 2072 IPPROTO_ICMPV6) { 2073 if (optval != icmp6off) 2074 error = EINVAL; 2075 } else 2076 in6p->in6p_cksum = optval; 2077 break; 2078 2079 case SOPT_GET: 2080 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 2081 optval = icmp6off; 2082 else 2083 optval = in6p->in6p_cksum; 2084 2085 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2086 break; 2087 2088 default: 2089 error = EINVAL; 2090 break; 2091 } 2092 break; 2093 2094 default: 2095 error = ENOPROTOOPT; 2096 break; 2097 } 2098 2099 return (error); 2100} 2101 2102/* 2103 * Set up IP6 options in pcb for insertion in output packets or 2104 * specifying behavior of outgoing packets. 2105 */ 2106static int 2107ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, 2108 struct socket *so, struct sockopt *sopt) 2109{ 2110 struct ip6_pktopts *opt = *pktopt; 2111 int error = 0; 2112 struct thread *td = sopt->sopt_td; 2113 2114 /* turn off any old options. */ 2115 if (opt) { 2116#ifdef DIAGNOSTIC 2117 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 2118 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 2119 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 2120 printf("ip6_pcbopts: all specified options are cleared.\n"); 2121#endif 2122 ip6_clearpktopts(opt, -1); 2123 } else 2124 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 2125 *pktopt = NULL; 2126 2127 if (!m || m->m_len == 0) { 2128 /* 2129 * Only turning off any previous options, regardless of 2130 * whether the opt is just created or given. 2131 */ 2132 free(opt, M_IP6OPT); 2133 return (0); 2134 } 2135 2136 /* set options specified by user. */ 2137 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ? 2138 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) { 2139 ip6_clearpktopts(opt, -1); /* XXX: discard all options */ 2140 free(opt, M_IP6OPT); 2141 return (error); 2142 } 2143 *pktopt = opt; 2144 return (0); 2145} 2146 2147/* 2148 * initialize ip6_pktopts. beware that there are non-zero default values in 2149 * the struct. 2150 */ 2151void 2152ip6_initpktopts(struct ip6_pktopts *opt) 2153{ 2154 2155 bzero(opt, sizeof(*opt)); 2156 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 2157 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 2158 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 2159 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; 2160} 2161 2162static int 2163ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, 2164 struct ucred *cred, int uproto) 2165{ 2166 struct ip6_pktopts *opt; 2167 2168 if (*pktopt == NULL) { 2169 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 2170 M_WAITOK); 2171 ip6_initpktopts(*pktopt); 2172 } 2173 opt = *pktopt; 2174 2175 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto)); 2176} 2177 2178static int 2179ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt) 2180{ 2181 void *optdata = NULL; 2182 int optdatalen = 0; 2183 struct ip6_ext *ip6e; 2184 int error = 0; 2185 struct in6_pktinfo null_pktinfo; 2186 int deftclass = 0, on; 2187 int defminmtu = IP6PO_MINMTU_MCASTONLY; 2188 int defpreftemp = IP6PO_TEMPADDR_SYSTEM; 2189 2190 switch (optname) { 2191 case IPV6_PKTINFO: 2192 if (pktopt && pktopt->ip6po_pktinfo) 2193 optdata = (void *)pktopt->ip6po_pktinfo; 2194 else { 2195 /* XXX: we don't have to do this every time... */ 2196 bzero(&null_pktinfo, sizeof(null_pktinfo)); 2197 optdata = (void *)&null_pktinfo; 2198 } 2199 optdatalen = sizeof(struct in6_pktinfo); 2200 break; 2201 case IPV6_TCLASS: 2202 if (pktopt && pktopt->ip6po_tclass >= 0) 2203 optdata = (void *)&pktopt->ip6po_tclass; 2204 else 2205 optdata = (void *)&deftclass; 2206 optdatalen = sizeof(int); 2207 break; 2208 case IPV6_HOPOPTS: 2209 if (pktopt && pktopt->ip6po_hbh) { 2210 optdata = (void *)pktopt->ip6po_hbh; 2211 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; 2212 optdatalen = (ip6e->ip6e_len + 1) << 3; 2213 } 2214 break; 2215 case IPV6_RTHDR: 2216 if (pktopt && pktopt->ip6po_rthdr) { 2217 optdata = (void *)pktopt->ip6po_rthdr; 2218 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; 2219 optdatalen = (ip6e->ip6e_len + 1) << 3; 2220 } 2221 break; 2222 case IPV6_RTHDRDSTOPTS: 2223 if (pktopt && pktopt->ip6po_dest1) { 2224 optdata = (void *)pktopt->ip6po_dest1; 2225 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; 2226 optdatalen = (ip6e->ip6e_len + 1) << 3; 2227 } 2228 break; 2229 case IPV6_DSTOPTS: 2230 if (pktopt && pktopt->ip6po_dest2) { 2231 optdata = (void *)pktopt->ip6po_dest2; 2232 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; 2233 optdatalen = (ip6e->ip6e_len + 1) << 3; 2234 } 2235 break; 2236 case IPV6_NEXTHOP: 2237 if (pktopt && pktopt->ip6po_nexthop) { 2238 optdata = (void *)pktopt->ip6po_nexthop; 2239 optdatalen = pktopt->ip6po_nexthop->sa_len; 2240 } 2241 break; 2242 case IPV6_USE_MIN_MTU: 2243 if (pktopt) 2244 optdata = (void *)&pktopt->ip6po_minmtu; 2245 else 2246 optdata = (void *)&defminmtu; 2247 optdatalen = sizeof(int); 2248 break; 2249 case IPV6_DONTFRAG: 2250 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 2251 on = 1; 2252 else 2253 on = 0; 2254 optdata = (void *)&on; 2255 optdatalen = sizeof(on); 2256 break; 2257 case IPV6_PREFER_TEMPADDR: 2258 if (pktopt) 2259 optdata = (void *)&pktopt->ip6po_prefer_tempaddr; 2260 else 2261 optdata = (void *)&defpreftemp; 2262 optdatalen = sizeof(int); 2263 break; 2264 default: /* should not happen */ 2265#ifdef DIAGNOSTIC 2266 panic("ip6_getpcbopt: unexpected option\n"); 2267#endif 2268 return (ENOPROTOOPT); 2269 } 2270 2271 error = sooptcopyout(sopt, optdata, optdatalen); 2272 2273 return (error); 2274} 2275 2276void 2277ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) 2278{ 2279 if (pktopt == NULL) 2280 return; 2281 2282 if (optname == -1 || optname == IPV6_PKTINFO) { 2283 if (pktopt->ip6po_pktinfo) 2284 free(pktopt->ip6po_pktinfo, M_IP6OPT); 2285 pktopt->ip6po_pktinfo = NULL; 2286 } 2287 if (optname == -1 || optname == IPV6_HOPLIMIT) 2288 pktopt->ip6po_hlim = -1; 2289 if (optname == -1 || optname == IPV6_TCLASS) 2290 pktopt->ip6po_tclass = -1; 2291 if (optname == -1 || optname == IPV6_NEXTHOP) { 2292 if (pktopt->ip6po_nextroute.ro_rt) { 2293 RTFREE(pktopt->ip6po_nextroute.ro_rt); 2294 pktopt->ip6po_nextroute.ro_rt = NULL; 2295 } 2296 if (pktopt->ip6po_nexthop) 2297 free(pktopt->ip6po_nexthop, M_IP6OPT); 2298 pktopt->ip6po_nexthop = NULL; 2299 } 2300 if (optname == -1 || optname == IPV6_HOPOPTS) { 2301 if (pktopt->ip6po_hbh) 2302 free(pktopt->ip6po_hbh, M_IP6OPT); 2303 pktopt->ip6po_hbh = NULL; 2304 } 2305 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 2306 if (pktopt->ip6po_dest1) 2307 free(pktopt->ip6po_dest1, M_IP6OPT); 2308 pktopt->ip6po_dest1 = NULL; 2309 } 2310 if (optname == -1 || optname == IPV6_RTHDR) { 2311 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 2312 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 2313 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 2314 if (pktopt->ip6po_route.ro_rt) { 2315 RTFREE(pktopt->ip6po_route.ro_rt); 2316 pktopt->ip6po_route.ro_rt = NULL; 2317 } 2318 } 2319 if (optname == -1 || optname == IPV6_DSTOPTS) { 2320 if (pktopt->ip6po_dest2) 2321 free(pktopt->ip6po_dest2, M_IP6OPT); 2322 pktopt->ip6po_dest2 = NULL; 2323 } 2324} 2325 2326#define PKTOPT_EXTHDRCPY(type) \ 2327do {\ 2328 if (src->type) {\ 2329 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 2330 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 2331 if (dst->type == NULL && canwait == M_NOWAIT)\ 2332 goto bad;\ 2333 bcopy(src->type, dst->type, hlen);\ 2334 }\ 2335} while (/*CONSTCOND*/ 0) 2336 2337static int 2338copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) 2339{ 2340 if (dst == NULL || src == NULL) { 2341 printf("ip6_clearpktopts: invalid argument\n"); 2342 return (EINVAL); 2343 } 2344 2345 dst->ip6po_hlim = src->ip6po_hlim; 2346 dst->ip6po_tclass = src->ip6po_tclass; 2347 dst->ip6po_flags = src->ip6po_flags; 2348 if (src->ip6po_pktinfo) { 2349 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2350 M_IP6OPT, canwait); 2351 if (dst->ip6po_pktinfo == NULL) 2352 goto bad; 2353 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2354 } 2355 if (src->ip6po_nexthop) { 2356 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2357 M_IP6OPT, canwait); 2358 if (dst->ip6po_nexthop == NULL) 2359 goto bad; 2360 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 2361 src->ip6po_nexthop->sa_len); 2362 } 2363 PKTOPT_EXTHDRCPY(ip6po_hbh); 2364 PKTOPT_EXTHDRCPY(ip6po_dest1); 2365 PKTOPT_EXTHDRCPY(ip6po_dest2); 2366 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2367 return (0); 2368 2369 bad: 2370 ip6_clearpktopts(dst, -1); 2371 return (ENOBUFS); 2372} 2373#undef PKTOPT_EXTHDRCPY 2374 2375struct ip6_pktopts * 2376ip6_copypktopts(struct ip6_pktopts *src, int canwait) 2377{ 2378 int error; 2379 struct ip6_pktopts *dst; 2380 2381 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2382 if (dst == NULL) 2383 return (NULL); 2384 ip6_initpktopts(dst); 2385 2386 if ((error = copypktopts(dst, src, canwait)) != 0) { 2387 free(dst, M_IP6OPT); 2388 return (NULL); 2389 } 2390 2391 return (dst); 2392} 2393 2394void 2395ip6_freepcbopts(struct ip6_pktopts *pktopt) 2396{ 2397 if (pktopt == NULL) 2398 return; 2399 2400 ip6_clearpktopts(pktopt, -1); 2401 2402 free(pktopt, M_IP6OPT); 2403} 2404 2405/* 2406 * Set the IP6 multicast options in response to user setsockopt(). 2407 */ 2408static int 2409ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m) 2410{ 2411 int error = 0; 2412 u_int loop, ifindex; 2413 struct ipv6_mreq *mreq; 2414 struct ifnet *ifp; 2415 struct ip6_moptions *im6o = *im6op; 2416 struct route_in6 ro; 2417 struct in6_multi_mship *imm; 2418 2419 if (im6o == NULL) { 2420 /* 2421 * No multicast option buffer attached to the pcb; 2422 * allocate one and initialize to default values. 2423 */ 2424 im6o = (struct ip6_moptions *) 2425 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK); 2426 2427 if (im6o == NULL) 2428 return (ENOBUFS); 2429 *im6op = im6o; 2430 im6o->im6o_multicast_ifp = NULL; 2431 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2432 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 2433 LIST_INIT(&im6o->im6o_memberships); 2434 } 2435 2436 switch (optname) { 2437 2438 case IPV6_MULTICAST_IF: 2439 /* 2440 * Select the interface for outgoing multicast packets. 2441 */ 2442 if (m == NULL || m->m_len != sizeof(u_int)) { 2443 error = EINVAL; 2444 break; 2445 } 2446 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex)); 2447 if (ifindex < 0 || if_index < ifindex) { 2448 error = ENXIO; /* XXX EINVAL? */ 2449 break; 2450 } 2451 ifp = ifnet_byindex(ifindex); 2452 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2453 error = EADDRNOTAVAIL; 2454 break; 2455 } 2456 im6o->im6o_multicast_ifp = ifp; 2457 break; 2458 2459 case IPV6_MULTICAST_HOPS: 2460 { 2461 /* 2462 * Set the IP6 hoplimit for outgoing multicast packets. 2463 */ 2464 int optval; 2465 if (m == NULL || m->m_len != sizeof(int)) { 2466 error = EINVAL; 2467 break; 2468 } 2469 bcopy(mtod(m, u_int *), &optval, sizeof(optval)); 2470 if (optval < -1 || optval >= 256) 2471 error = EINVAL; 2472 else if (optval == -1) 2473 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2474 else 2475 im6o->im6o_multicast_hlim = optval; 2476 break; 2477 } 2478 2479 case IPV6_MULTICAST_LOOP: 2480 /* 2481 * Set the loopback flag for outgoing multicast packets. 2482 * Must be zero or one. 2483 */ 2484 if (m == NULL || m->m_len != sizeof(u_int)) { 2485 error = EINVAL; 2486 break; 2487 } 2488 bcopy(mtod(m, u_int *), &loop, sizeof(loop)); 2489 if (loop > 1) { 2490 error = EINVAL; 2491 break; 2492 } 2493 im6o->im6o_multicast_loop = loop; 2494 break; 2495 2496 case IPV6_JOIN_GROUP: 2497 /* 2498 * Add a multicast group membership. 2499 * Group must be a valid IP6 multicast address. 2500 */ 2501 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2502 error = EINVAL; 2503 break; 2504 } 2505 mreq = mtod(m, struct ipv6_mreq *); 2506 2507 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2508 /* 2509 * We use the unspecified address to specify to accept 2510 * all multicast addresses. Only super user is allowed 2511 * to do this. 2512 */ 2513 /* XXX-BZ might need a better PRIV_NETINET_x for this */ 2514 error = priv_check(curthread, PRIV_NETINET_MROUTE); 2515 if (error) 2516 break; 2517 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2518 error = EINVAL; 2519 break; 2520 } 2521 2522 /* 2523 * If no interface was explicitly specified, choose an 2524 * appropriate one according to the given multicast address. 2525 */ 2526 if (mreq->ipv6mr_interface == 0) { 2527 struct sockaddr_in6 *dst; 2528 2529 /* 2530 * Look up the routing table for the 2531 * address, and choose the outgoing interface. 2532 * XXX: is it a good approach? 2533 */ 2534 ro.ro_rt = NULL; 2535 dst = (struct sockaddr_in6 *)&ro.ro_dst; 2536 bzero(dst, sizeof(*dst)); 2537 dst->sin6_family = AF_INET6; 2538 dst->sin6_len = sizeof(*dst); 2539 dst->sin6_addr = mreq->ipv6mr_multiaddr; 2540 rtalloc((struct route *)&ro); 2541 if (ro.ro_rt == NULL) { 2542 error = EADDRNOTAVAIL; 2543 break; 2544 } 2545 ifp = ro.ro_rt->rt_ifp; 2546 RTFREE(ro.ro_rt); 2547 } else { 2548 /* 2549 * If the interface is specified, validate it. 2550 */ 2551 if (mreq->ipv6mr_interface < 0 || 2552 if_index < mreq->ipv6mr_interface) { 2553 error = ENXIO; /* XXX EINVAL? */ 2554 break; 2555 } 2556 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2557 if (!ifp) { 2558 error = ENXIO; /* XXX EINVAL? */ 2559 break; 2560 } 2561 } 2562 2563 /* 2564 * See if we found an interface, and confirm that it 2565 * supports multicast 2566 */ 2567 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2568 error = EADDRNOTAVAIL; 2569 break; 2570 } 2571 2572 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) { 2573 error = EADDRNOTAVAIL; /* XXX: should not happen */ 2574 break; 2575 } 2576 2577 /* 2578 * See if the membership already exists. 2579 */ 2580 for (imm = im6o->im6o_memberships.lh_first; 2581 imm != NULL; imm = imm->i6mm_chain.le_next) 2582 if (imm->i6mm_maddr->in6m_ifp == ifp && 2583 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2584 &mreq->ipv6mr_multiaddr)) 2585 break; 2586 if (imm != NULL) { 2587 error = EADDRINUSE; 2588 break; 2589 } 2590 /* 2591 * Everything looks good; add a new record to the multicast 2592 * address list for the given interface. 2593 */ 2594 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0); 2595 if (imm == NULL) 2596 break; 2597 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2598 break; 2599 2600 case IPV6_LEAVE_GROUP: 2601 /* 2602 * Drop a multicast group membership. 2603 * Group must be a valid IP6 multicast address. 2604 */ 2605 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2606 error = EINVAL; 2607 break; 2608 } 2609 mreq = mtod(m, struct ipv6_mreq *); 2610 2611 /* 2612 * If an interface address was specified, get a pointer 2613 * to its ifnet structure. 2614 */ 2615 if (mreq->ipv6mr_interface < 0 || 2616 if_index < mreq->ipv6mr_interface) { 2617 error = ENXIO; /* XXX EINVAL? */ 2618 break; 2619 } 2620 if (mreq->ipv6mr_interface == 0) 2621 ifp = NULL; 2622 else 2623 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2624 2625 /* Fill in the scope zone ID */ 2626 if (ifp) { 2627 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) { 2628 /* XXX: should not happen */ 2629 error = EADDRNOTAVAIL; 2630 break; 2631 } 2632 } else if (mreq->ipv6mr_interface != 0) { 2633 /* 2634 * This case happens when the (positive) index is in 2635 * the valid range, but the corresponding interface has 2636 * been detached dynamically (XXX). 2637 */ 2638 error = EADDRNOTAVAIL; 2639 break; 2640 } else { /* ipv6mr_interface == 0 */ 2641 struct sockaddr_in6 sa6_mc; 2642 2643 /* 2644 * The API spec says as follows: 2645 * If the interface index is specified as 0, the 2646 * system may choose a multicast group membership to 2647 * drop by matching the multicast address only. 2648 * On the other hand, we cannot disambiguate the scope 2649 * zone unless an interface is provided. Thus, we 2650 * check if there's ambiguity with the default scope 2651 * zone as the last resort. 2652 */ 2653 bzero(&sa6_mc, sizeof(sa6_mc)); 2654 sa6_mc.sin6_family = AF_INET6; 2655 sa6_mc.sin6_len = sizeof(sa6_mc); 2656 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr; 2657 error = sa6_embedscope(&sa6_mc, ip6_use_defzone); 2658 if (error != 0) 2659 break; 2660 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr; 2661 } 2662 2663 /* 2664 * Find the membership in the membership list. 2665 */ 2666 for (imm = im6o->im6o_memberships.lh_first; 2667 imm != NULL; imm = imm->i6mm_chain.le_next) { 2668 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) && 2669 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2670 &mreq->ipv6mr_multiaddr)) 2671 break; 2672 } 2673 if (imm == NULL) { 2674 /* Unable to resolve interface */ 2675 error = EADDRNOTAVAIL; 2676 break; 2677 } 2678 /* 2679 * Give up the multicast address record to which the 2680 * membership points. 2681 */ 2682 LIST_REMOVE(imm, i6mm_chain); 2683 in6_delmulti(imm->i6mm_maddr); 2684 free(imm, M_IP6MADDR); 2685 break; 2686 2687 default: 2688 error = EOPNOTSUPP; 2689 break; 2690 } 2691 2692 /* 2693 * If all options have default values, no need to keep the mbuf. 2694 */ 2695 if (im6o->im6o_multicast_ifp == NULL && 2696 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2697 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2698 im6o->im6o_memberships.lh_first == NULL) { 2699 free(*im6op, M_IP6MOPTS); 2700 *im6op = NULL; 2701 } 2702 2703 return (error); 2704} 2705 2706/* 2707 * Return the IP6 multicast options in response to user getsockopt(). 2708 */ 2709static int 2710ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp) 2711{ 2712 u_int *hlim, *loop, *ifindex; 2713 2714 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */ 2715 2716 switch (optname) { 2717 2718 case IPV6_MULTICAST_IF: 2719 ifindex = mtod(*mp, u_int *); 2720 (*mp)->m_len = sizeof(u_int); 2721 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2722 *ifindex = 0; 2723 else 2724 *ifindex = im6o->im6o_multicast_ifp->if_index; 2725 return (0); 2726 2727 case IPV6_MULTICAST_HOPS: 2728 hlim = mtod(*mp, u_int *); 2729 (*mp)->m_len = sizeof(u_int); 2730 if (im6o == NULL) 2731 *hlim = ip6_defmcasthlim; 2732 else 2733 *hlim = im6o->im6o_multicast_hlim; 2734 return (0); 2735 2736 case IPV6_MULTICAST_LOOP: 2737 loop = mtod(*mp, u_int *); 2738 (*mp)->m_len = sizeof(u_int); 2739 if (im6o == NULL) 2740 *loop = ip6_defmcasthlim; 2741 else 2742 *loop = im6o->im6o_multicast_loop; 2743 return (0); 2744 2745 default: 2746 return (EOPNOTSUPP); 2747 } 2748} 2749 2750/* 2751 * Discard the IP6 multicast options. 2752 */ 2753void 2754ip6_freemoptions(struct ip6_moptions *im6o) 2755{ 2756 struct in6_multi_mship *imm; 2757 2758 if (im6o == NULL) 2759 return; 2760 2761 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2762 LIST_REMOVE(imm, i6mm_chain); 2763 if (imm->i6mm_maddr) 2764 in6_delmulti(imm->i6mm_maddr); 2765 free(imm, M_IP6MADDR); 2766 } 2767 free(im6o, M_IP6MOPTS); 2768} 2769 2770/* 2771 * Set IPv6 outgoing packet options based on advanced API. 2772 */ 2773int 2774ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2775 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto) 2776{ 2777 struct cmsghdr *cm = 0; 2778 2779 if (control == NULL || opt == NULL) 2780 return (EINVAL); 2781 2782 ip6_initpktopts(opt); 2783 if (stickyopt) { 2784 int error; 2785 2786 /* 2787 * If stickyopt is provided, make a local copy of the options 2788 * for this particular packet, then override them by ancillary 2789 * objects. 2790 * XXX: copypktopts() does not copy the cached route to a next 2791 * hop (if any). This is not very good in terms of efficiency, 2792 * but we can allow this since this option should be rarely 2793 * used. 2794 */ 2795 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2796 return (error); 2797 } 2798 2799 /* 2800 * XXX: Currently, we assume all the optional information is stored 2801 * in a single mbuf. 2802 */ 2803 if (control->m_next) 2804 return (EINVAL); 2805 2806 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2807 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2808 int error; 2809 2810 if (control->m_len < CMSG_LEN(0)) 2811 return (EINVAL); 2812 2813 cm = mtod(control, struct cmsghdr *); 2814 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2815 return (EINVAL); 2816 if (cm->cmsg_level != IPPROTO_IPV6) 2817 continue; 2818 2819 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2820 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); 2821 if (error) 2822 return (error); 2823 } 2824 2825 return (0); 2826} 2827 2828/* 2829 * Set a particular packet option, as a sticky option or an ancillary data 2830 * item. "len" can be 0 only when it's a sticky option. 2831 * We have 4 cases of combination of "sticky" and "cmsg": 2832 * "sticky=0, cmsg=0": impossible 2833 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2834 * "sticky=1, cmsg=0": RFC3542 socket option 2835 * "sticky=1, cmsg=1": RFC2292 socket option 2836 */ 2837static int 2838ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2839 struct ucred *cred, int sticky, int cmsg, int uproto) 2840{ 2841 int minmtupolicy, preftemp; 2842 int error; 2843 2844 if (!sticky && !cmsg) { 2845#ifdef DIAGNOSTIC 2846 printf("ip6_setpktopt: impossible case\n"); 2847#endif 2848 return (EINVAL); 2849 } 2850 2851 /* 2852 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2853 * not be specified in the context of RFC3542. Conversely, 2854 * RFC3542 types should not be specified in the context of RFC2292. 2855 */ 2856 if (!cmsg) { 2857 switch (optname) { 2858 case IPV6_2292PKTINFO: 2859 case IPV6_2292HOPLIMIT: 2860 case IPV6_2292NEXTHOP: 2861 case IPV6_2292HOPOPTS: 2862 case IPV6_2292DSTOPTS: 2863 case IPV6_2292RTHDR: 2864 case IPV6_2292PKTOPTIONS: 2865 return (ENOPROTOOPT); 2866 } 2867 } 2868 if (sticky && cmsg) { 2869 switch (optname) { 2870 case IPV6_PKTINFO: 2871 case IPV6_HOPLIMIT: 2872 case IPV6_NEXTHOP: 2873 case IPV6_HOPOPTS: 2874 case IPV6_DSTOPTS: 2875 case IPV6_RTHDRDSTOPTS: 2876 case IPV6_RTHDR: 2877 case IPV6_USE_MIN_MTU: 2878 case IPV6_DONTFRAG: 2879 case IPV6_TCLASS: 2880 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */ 2881 return (ENOPROTOOPT); 2882 } 2883 } 2884 2885 switch (optname) { 2886 case IPV6_2292PKTINFO: 2887 case IPV6_PKTINFO: 2888 { 2889 struct ifnet *ifp = NULL; 2890 struct in6_pktinfo *pktinfo; 2891 2892 if (len != sizeof(struct in6_pktinfo)) 2893 return (EINVAL); 2894 2895 pktinfo = (struct in6_pktinfo *)buf; 2896 2897 /* 2898 * An application can clear any sticky IPV6_PKTINFO option by 2899 * doing a "regular" setsockopt with ipi6_addr being 2900 * in6addr_any and ipi6_ifindex being zero. 2901 * [RFC 3542, Section 6] 2902 */ 2903 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2904 pktinfo->ipi6_ifindex == 0 && 2905 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2906 ip6_clearpktopts(opt, optname); 2907 break; 2908 } 2909 2910 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2911 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2912 return (EINVAL); 2913 } 2914 2915 /* validate the interface index if specified. */ 2916 if (pktinfo->ipi6_ifindex > if_index || 2917 pktinfo->ipi6_ifindex < 0) { 2918 return (ENXIO); 2919 } 2920 if (pktinfo->ipi6_ifindex) { 2921 ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 2922 if (ifp == NULL) 2923 return (ENXIO); 2924 } 2925 2926 /* 2927 * We store the address anyway, and let in6_selectsrc() 2928 * validate the specified address. This is because ipi6_addr 2929 * may not have enough information about its scope zone, and 2930 * we may need additional information (such as outgoing 2931 * interface or the scope zone of a destination address) to 2932 * disambiguate the scope. 2933 * XXX: the delay of the validation may confuse the 2934 * application when it is used as a sticky option. 2935 */ 2936 if (opt->ip6po_pktinfo == NULL) { 2937 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2938 M_IP6OPT, M_NOWAIT); 2939 if (opt->ip6po_pktinfo == NULL) 2940 return (ENOBUFS); 2941 } 2942 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 2943 break; 2944 } 2945 2946 case IPV6_2292HOPLIMIT: 2947 case IPV6_HOPLIMIT: 2948 { 2949 int *hlimp; 2950 2951 /* 2952 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2953 * to simplify the ordering among hoplimit options. 2954 */ 2955 if (optname == IPV6_HOPLIMIT && sticky) 2956 return (ENOPROTOOPT); 2957 2958 if (len != sizeof(int)) 2959 return (EINVAL); 2960 hlimp = (int *)buf; 2961 if (*hlimp < -1 || *hlimp > 255) 2962 return (EINVAL); 2963 2964 opt->ip6po_hlim = *hlimp; 2965 break; 2966 } 2967 2968 case IPV6_TCLASS: 2969 { 2970 int tclass; 2971 2972 if (len != sizeof(int)) 2973 return (EINVAL); 2974 tclass = *(int *)buf; 2975 if (tclass < -1 || tclass > 255) 2976 return (EINVAL); 2977 2978 opt->ip6po_tclass = tclass; 2979 break; 2980 } 2981 2982 case IPV6_2292NEXTHOP: 2983 case IPV6_NEXTHOP: 2984 if (cred != NULL) { 2985 error = priv_check_cred(cred, 2986 PRIV_NETINET_SETHDROPTS, 0); 2987 if (error) 2988 return (error); 2989 } 2990 2991 if (len == 0) { /* just remove the option */ 2992 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2993 break; 2994 } 2995 2996 /* check if cmsg_len is large enough for sa_len */ 2997 if (len < sizeof(struct sockaddr) || len < *buf) 2998 return (EINVAL); 2999 3000 switch (((struct sockaddr *)buf)->sa_family) { 3001 case AF_INET6: 3002 { 3003 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 3004 int error; 3005 3006 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 3007 return (EINVAL); 3008 3009 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 3010 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 3011 return (EINVAL); 3012 } 3013 if ((error = sa6_embedscope(sa6, ip6_use_defzone)) 3014 != 0) { 3015 return (error); 3016 } 3017 break; 3018 } 3019 case AF_LINK: /* should eventually be supported */ 3020 default: 3021 return (EAFNOSUPPORT); 3022 } 3023 3024 /* turn off the previous option, then set the new option. */ 3025 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3026 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 3027 if (opt->ip6po_nexthop == NULL) 3028 return (ENOBUFS); 3029 bcopy(buf, opt->ip6po_nexthop, *buf); 3030 break; 3031 3032 case IPV6_2292HOPOPTS: 3033 case IPV6_HOPOPTS: 3034 { 3035 struct ip6_hbh *hbh; 3036 int hbhlen; 3037 3038 /* 3039 * XXX: We don't allow a non-privileged user to set ANY HbH 3040 * options, since per-option restriction has too much 3041 * overhead. 3042 */ 3043 if (cred != NULL) { 3044 error = priv_check_cred(cred, 3045 PRIV_NETINET_SETHDROPTS, 0); 3046 if (error) 3047 return (error); 3048 } 3049 3050 if (len == 0) { 3051 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3052 break; /* just remove the option */ 3053 } 3054 3055 /* message length validation */ 3056 if (len < sizeof(struct ip6_hbh)) 3057 return (EINVAL); 3058 hbh = (struct ip6_hbh *)buf; 3059 hbhlen = (hbh->ip6h_len + 1) << 3; 3060 if (len != hbhlen) 3061 return (EINVAL); 3062 3063 /* turn off the previous option, then set the new option. */ 3064 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3065 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 3066 if (opt->ip6po_hbh == NULL) 3067 return (ENOBUFS); 3068 bcopy(hbh, opt->ip6po_hbh, hbhlen); 3069 3070 break; 3071 } 3072 3073 case IPV6_2292DSTOPTS: 3074 case IPV6_DSTOPTS: 3075 case IPV6_RTHDRDSTOPTS: 3076 { 3077 struct ip6_dest *dest, **newdest = NULL; 3078 int destlen; 3079 3080 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */ 3081 error = priv_check_cred(cred, 3082 PRIV_NETINET_SETHDROPTS, 0); 3083 if (error) 3084 return (error); 3085 } 3086 3087 if (len == 0) { 3088 ip6_clearpktopts(opt, optname); 3089 break; /* just remove the option */ 3090 } 3091 3092 /* message length validation */ 3093 if (len < sizeof(struct ip6_dest)) 3094 return (EINVAL); 3095 dest = (struct ip6_dest *)buf; 3096 destlen = (dest->ip6d_len + 1) << 3; 3097 if (len != destlen) 3098 return (EINVAL); 3099 3100 /* 3101 * Determine the position that the destination options header 3102 * should be inserted; before or after the routing header. 3103 */ 3104 switch (optname) { 3105 case IPV6_2292DSTOPTS: 3106 /* 3107 * The old advacned API is ambiguous on this point. 3108 * Our approach is to determine the position based 3109 * according to the existence of a routing header. 3110 * Note, however, that this depends on the order of the 3111 * extension headers in the ancillary data; the 1st 3112 * part of the destination options header must appear 3113 * before the routing header in the ancillary data, 3114 * too. 3115 * RFC3542 solved the ambiguity by introducing 3116 * separate ancillary data or option types. 3117 */ 3118 if (opt->ip6po_rthdr == NULL) 3119 newdest = &opt->ip6po_dest1; 3120 else 3121 newdest = &opt->ip6po_dest2; 3122 break; 3123 case IPV6_RTHDRDSTOPTS: 3124 newdest = &opt->ip6po_dest1; 3125 break; 3126 case IPV6_DSTOPTS: 3127 newdest = &opt->ip6po_dest2; 3128 break; 3129 } 3130 3131 /* turn off the previous option, then set the new option. */ 3132 ip6_clearpktopts(opt, optname); 3133 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 3134 if (*newdest == NULL) 3135 return (ENOBUFS); 3136 bcopy(dest, *newdest, destlen); 3137 3138 break; 3139 } 3140 3141 case IPV6_2292RTHDR: 3142 case IPV6_RTHDR: 3143 { 3144 struct ip6_rthdr *rth; 3145 int rthlen; 3146 3147 if (len == 0) { 3148 ip6_clearpktopts(opt, IPV6_RTHDR); 3149 break; /* just remove the option */ 3150 } 3151 3152 /* message length validation */ 3153 if (len < sizeof(struct ip6_rthdr)) 3154 return (EINVAL); 3155 rth = (struct ip6_rthdr *)buf; 3156 rthlen = (rth->ip6r_len + 1) << 3; 3157 if (len != rthlen) 3158 return (EINVAL); 3159 3160 switch (rth->ip6r_type) { 3161 case IPV6_RTHDR_TYPE_0: 3162 if (rth->ip6r_len == 0) /* must contain one addr */ 3163 return (EINVAL); 3164 if (rth->ip6r_len % 2) /* length must be even */ 3165 return (EINVAL); 3166 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 3167 return (EINVAL); 3168 break; 3169 default: 3170 return (EINVAL); /* not supported */ 3171 } 3172 3173 /* turn off the previous option */ 3174 ip6_clearpktopts(opt, IPV6_RTHDR); 3175 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 3176 if (opt->ip6po_rthdr == NULL) 3177 return (ENOBUFS); 3178 bcopy(rth, opt->ip6po_rthdr, rthlen); 3179 3180 break; 3181 } 3182 3183 case IPV6_USE_MIN_MTU: 3184 if (len != sizeof(int)) 3185 return (EINVAL); 3186 minmtupolicy = *(int *)buf; 3187 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 3188 minmtupolicy != IP6PO_MINMTU_DISABLE && 3189 minmtupolicy != IP6PO_MINMTU_ALL) { 3190 return (EINVAL); 3191 } 3192 opt->ip6po_minmtu = minmtupolicy; 3193 break; 3194 3195 case IPV6_DONTFRAG: 3196 if (len != sizeof(int)) 3197 return (EINVAL); 3198 3199 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 3200 /* 3201 * we ignore this option for TCP sockets. 3202 * (RFC3542 leaves this case unspecified.) 3203 */ 3204 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 3205 } else 3206 opt->ip6po_flags |= IP6PO_DONTFRAG; 3207 break; 3208 3209 case IPV6_PREFER_TEMPADDR: 3210 if (len != sizeof(int)) 3211 return (EINVAL); 3212 preftemp = *(int *)buf; 3213 if (preftemp != IP6PO_TEMPADDR_SYSTEM && 3214 preftemp != IP6PO_TEMPADDR_NOTPREFER && 3215 preftemp != IP6PO_TEMPADDR_PREFER) { 3216 return (EINVAL); 3217 } 3218 opt->ip6po_prefer_tempaddr = preftemp; 3219 break; 3220 3221 default: 3222 return (ENOPROTOOPT); 3223 } /* end of switch */ 3224 3225 return (0); 3226} 3227 3228/* 3229 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 3230 * packet to the input queue of a specified interface. Note that this 3231 * calls the output routine of the loopback "driver", but with an interface 3232 * pointer that might NOT be &loif -- easier than replicating that code here. 3233 */ 3234void 3235ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst) 3236{ 3237 struct mbuf *copym; 3238 struct ip6_hdr *ip6; 3239 3240 copym = m_copy(m, 0, M_COPYALL); 3241 if (copym == NULL) 3242 return; 3243 3244 /* 3245 * Make sure to deep-copy IPv6 header portion in case the data 3246 * is in an mbuf cluster, so that we can safely override the IPv6 3247 * header portion later. 3248 */ 3249 if ((copym->m_flags & M_EXT) != 0 || 3250 copym->m_len < sizeof(struct ip6_hdr)) { 3251 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 3252 if (copym == NULL) 3253 return; 3254 } 3255 3256#ifdef DIAGNOSTIC 3257 if (copym->m_len < sizeof(*ip6)) { 3258 m_freem(copym); 3259 return; 3260 } 3261#endif 3262 3263 ip6 = mtod(copym, struct ip6_hdr *); 3264 /* 3265 * clear embedded scope identifiers if necessary. 3266 * in6_clearscope will touch the addresses only when necessary. 3267 */ 3268 in6_clearscope(&ip6->ip6_src); 3269 in6_clearscope(&ip6->ip6_dst); 3270 3271 (void)if_simloop(ifp, copym, dst->sin6_family, 0); 3272} 3273 3274/* 3275 * Chop IPv6 header off from the payload. 3276 */ 3277static int 3278ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 3279{ 3280 struct mbuf *mh; 3281 struct ip6_hdr *ip6; 3282 3283 ip6 = mtod(m, struct ip6_hdr *); 3284 if (m->m_len > sizeof(*ip6)) { 3285 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 3286 if (mh == 0) { 3287 m_freem(m); 3288 return ENOBUFS; 3289 } 3290 M_MOVE_PKTHDR(mh, m); 3291 MH_ALIGN(mh, sizeof(*ip6)); 3292 m->m_len -= sizeof(*ip6); 3293 m->m_data += sizeof(*ip6); 3294 mh->m_next = m; 3295 m = mh; 3296 m->m_len = sizeof(*ip6); 3297 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 3298 } 3299 exthdrs->ip6e_ip6 = m; 3300 return 0; 3301} 3302 3303/* 3304 * Compute IPv6 extension header length. 3305 */ 3306int 3307ip6_optlen(struct in6pcb *in6p) 3308{ 3309 int len; 3310 3311 if (!in6p->in6p_outputopts) 3312 return 0; 3313 3314 len = 0; 3315#define elen(x) \ 3316 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 3317 3318 len += elen(in6p->in6p_outputopts->ip6po_hbh); 3319 if (in6p->in6p_outputopts->ip6po_rthdr) 3320 /* dest1 is valid with rthdr only */ 3321 len += elen(in6p->in6p_outputopts->ip6po_dest1); 3322 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 3323 len += elen(in6p->in6p_outputopts->ip6po_dest2); 3324 return len; 3325#undef elen 3326}
| 1089 1090 return (error); 1091 1092freehdrs: 1093 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1094 m_freem(exthdrs.ip6e_dest1); 1095 m_freem(exthdrs.ip6e_rthdr); 1096 m_freem(exthdrs.ip6e_dest2); 1097 /* FALLTHROUGH */ 1098bad: 1099 if (m) 1100 m_freem(m); 1101 goto done; 1102} 1103 1104static int 1105ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen) 1106{ 1107 struct mbuf *m; 1108 1109 if (hlen > MCLBYTES) 1110 return (ENOBUFS); /* XXX */ 1111 1112 MGET(m, M_DONTWAIT, MT_DATA); 1113 if (!m) 1114 return (ENOBUFS); 1115 1116 if (hlen > MLEN) { 1117 MCLGET(m, M_DONTWAIT); 1118 if ((m->m_flags & M_EXT) == 0) { 1119 m_free(m); 1120 return (ENOBUFS); 1121 } 1122 } 1123 m->m_len = hlen; 1124 if (hdr) 1125 bcopy(hdr, mtod(m, caddr_t), hlen); 1126 1127 *mp = m; 1128 return (0); 1129} 1130 1131/* 1132 * Insert jumbo payload option. 1133 */ 1134static int 1135ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) 1136{ 1137 struct mbuf *mopt; 1138 u_char *optbuf; 1139 u_int32_t v; 1140 1141#define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1142 1143 /* 1144 * If there is no hop-by-hop options header, allocate new one. 1145 * If there is one but it doesn't have enough space to store the 1146 * jumbo payload option, allocate a cluster to store the whole options. 1147 * Otherwise, use it to store the options. 1148 */ 1149 if (exthdrs->ip6e_hbh == 0) { 1150 MGET(mopt, M_DONTWAIT, MT_DATA); 1151 if (mopt == 0) 1152 return (ENOBUFS); 1153 mopt->m_len = JUMBOOPTLEN; 1154 optbuf = mtod(mopt, u_char *); 1155 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1156 exthdrs->ip6e_hbh = mopt; 1157 } else { 1158 struct ip6_hbh *hbh; 1159 1160 mopt = exthdrs->ip6e_hbh; 1161 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1162 /* 1163 * XXX assumption: 1164 * - exthdrs->ip6e_hbh is not referenced from places 1165 * other than exthdrs. 1166 * - exthdrs->ip6e_hbh is not an mbuf chain. 1167 */ 1168 int oldoptlen = mopt->m_len; 1169 struct mbuf *n; 1170 1171 /* 1172 * XXX: give up if the whole (new) hbh header does 1173 * not fit even in an mbuf cluster. 1174 */ 1175 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1176 return (ENOBUFS); 1177 1178 /* 1179 * As a consequence, we must always prepare a cluster 1180 * at this point. 1181 */ 1182 MGET(n, M_DONTWAIT, MT_DATA); 1183 if (n) { 1184 MCLGET(n, M_DONTWAIT); 1185 if ((n->m_flags & M_EXT) == 0) { 1186 m_freem(n); 1187 n = NULL; 1188 } 1189 } 1190 if (!n) 1191 return (ENOBUFS); 1192 n->m_len = oldoptlen + JUMBOOPTLEN; 1193 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1194 oldoptlen); 1195 optbuf = mtod(n, caddr_t) + oldoptlen; 1196 m_freem(mopt); 1197 mopt = exthdrs->ip6e_hbh = n; 1198 } else { 1199 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1200 mopt->m_len += JUMBOOPTLEN; 1201 } 1202 optbuf[0] = IP6OPT_PADN; 1203 optbuf[1] = 1; 1204 1205 /* 1206 * Adjust the header length according to the pad and 1207 * the jumbo payload option. 1208 */ 1209 hbh = mtod(mopt, struct ip6_hbh *); 1210 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1211 } 1212 1213 /* fill in the option. */ 1214 optbuf[2] = IP6OPT_JUMBO; 1215 optbuf[3] = 4; 1216 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1217 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1218 1219 /* finally, adjust the packet header length */ 1220 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1221 1222 return (0); 1223#undef JUMBOOPTLEN 1224} 1225 1226/* 1227 * Insert fragment header and copy unfragmentable header portions. 1228 */ 1229static int 1230ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, 1231 struct ip6_frag **frghdrp) 1232{ 1233 struct mbuf *n, *mlast; 1234 1235 if (hlen > sizeof(struct ip6_hdr)) { 1236 n = m_copym(m0, sizeof(struct ip6_hdr), 1237 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1238 if (n == 0) 1239 return (ENOBUFS); 1240 m->m_next = n; 1241 } else 1242 n = m; 1243 1244 /* Search for the last mbuf of unfragmentable part. */ 1245 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1246 ; 1247 1248 if ((mlast->m_flags & M_EXT) == 0 && 1249 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1250 /* use the trailing space of the last mbuf for the fragment hdr */ 1251 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) + 1252 mlast->m_len); 1253 mlast->m_len += sizeof(struct ip6_frag); 1254 m->m_pkthdr.len += sizeof(struct ip6_frag); 1255 } else { 1256 /* allocate a new mbuf for the fragment header */ 1257 struct mbuf *mfrg; 1258 1259 MGET(mfrg, M_DONTWAIT, MT_DATA); 1260 if (mfrg == 0) 1261 return (ENOBUFS); 1262 mfrg->m_len = sizeof(struct ip6_frag); 1263 *frghdrp = mtod(mfrg, struct ip6_frag *); 1264 mlast->m_next = mfrg; 1265 } 1266 1267 return (0); 1268} 1269 1270static int 1271ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro, 1272 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup, 1273 int *alwaysfragp) 1274{ 1275 u_int32_t mtu = 0; 1276 int alwaysfrag = 0; 1277 int error = 0; 1278 1279 if (ro_pmtu != ro) { 1280 /* The first hop and the final destination may differ. */ 1281 struct sockaddr_in6 *sa6_dst = 1282 (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 1283 if (ro_pmtu->ro_rt && 1284 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 || 1285 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) { 1286 RTFREE(ro_pmtu->ro_rt); 1287 ro_pmtu->ro_rt = (struct rtentry *)NULL; 1288 } 1289 if (ro_pmtu->ro_rt == NULL) { 1290 bzero(sa6_dst, sizeof(*sa6_dst)); 1291 sa6_dst->sin6_family = AF_INET6; 1292 sa6_dst->sin6_len = sizeof(struct sockaddr_in6); 1293 sa6_dst->sin6_addr = *dst; 1294 1295 rtalloc((struct route *)ro_pmtu); 1296 } 1297 } 1298 if (ro_pmtu->ro_rt) { 1299 u_int32_t ifmtu; 1300 struct in_conninfo inc; 1301 1302 bzero(&inc, sizeof(inc)); 1303 inc.inc_flags = 1; /* IPv6 */ 1304 inc.inc6_faddr = *dst; 1305 1306 if (ifp == NULL) 1307 ifp = ro_pmtu->ro_rt->rt_ifp; 1308 ifmtu = IN6_LINKMTU(ifp); 1309 mtu = tcp_hc_getmtu(&inc); 1310 if (mtu) 1311 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu); 1312 else 1313 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu; 1314 if (mtu == 0) 1315 mtu = ifmtu; 1316 else if (mtu < IPV6_MMTU) { 1317 /* 1318 * RFC2460 section 5, last paragraph: 1319 * if we record ICMPv6 too big message with 1320 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU 1321 * or smaller, with framgent header attached. 1322 * (fragment header is needed regardless from the 1323 * packet size, for translators to identify packets) 1324 */ 1325 alwaysfrag = 1; 1326 mtu = IPV6_MMTU; 1327 } else if (mtu > ifmtu) { 1328 /* 1329 * The MTU on the route is larger than the MTU on 1330 * the interface! This shouldn't happen, unless the 1331 * MTU of the interface has been changed after the 1332 * interface was brought up. Change the MTU in the 1333 * route to match the interface MTU (as long as the 1334 * field isn't locked). 1335 */ 1336 mtu = ifmtu; 1337 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; 1338 } 1339 } else if (ifp) { 1340 mtu = IN6_LINKMTU(ifp); 1341 } else 1342 error = EHOSTUNREACH; /* XXX */ 1343 1344 *mtup = mtu; 1345 if (alwaysfragp) 1346 *alwaysfragp = alwaysfrag; 1347 return (error); 1348} 1349 1350/* 1351 * IP6 socket option processing. 1352 */ 1353int 1354ip6_ctloutput(struct socket *so, struct sockopt *sopt) 1355{ 1356 int optdatalen, uproto; 1357 void *optdata; 1358 struct inpcb *in6p = sotoinpcb(so); 1359 int error, optval; 1360 int level, op, optname; 1361 int optlen; 1362 struct thread *td; 1363 1364 if (sopt) { 1365 level = sopt->sopt_level; 1366 op = sopt->sopt_dir; 1367 optname = sopt->sopt_name; 1368 optlen = sopt->sopt_valsize; 1369 td = sopt->sopt_td; 1370 } else { 1371 panic("ip6_ctloutput: arg soopt is NULL"); 1372 } 1373 error = optval = 0; 1374 1375 uproto = (int)so->so_proto->pr_protocol; 1376 1377 if (level == IPPROTO_IPV6) { 1378 switch (op) { 1379 1380 case SOPT_SET: 1381 switch (optname) { 1382 case IPV6_2292PKTOPTIONS: 1383#ifdef IPV6_PKTOPTIONS 1384 case IPV6_PKTOPTIONS: 1385#endif 1386 { 1387 struct mbuf *m; 1388 1389 error = soopt_getm(sopt, &m); /* XXX */ 1390 if (error != 0) 1391 break; 1392 error = soopt_mcopyin(sopt, m); /* XXX */ 1393 if (error != 0) 1394 break; 1395 error = ip6_pcbopts(&in6p->in6p_outputopts, 1396 m, so, sopt); 1397 m_freem(m); /* XXX */ 1398 break; 1399 } 1400 1401 /* 1402 * Use of some Hop-by-Hop options or some 1403 * Destination options, might require special 1404 * privilege. That is, normal applications 1405 * (without special privilege) might be forbidden 1406 * from setting certain options in outgoing packets, 1407 * and might never see certain options in received 1408 * packets. [RFC 2292 Section 6] 1409 * KAME specific note: 1410 * KAME prevents non-privileged users from sending or 1411 * receiving ANY hbh/dst options in order to avoid 1412 * overhead of parsing options in the kernel. 1413 */ 1414 case IPV6_RECVHOPOPTS: 1415 case IPV6_RECVDSTOPTS: 1416 case IPV6_RECVRTHDRDSTOPTS: 1417 if (td != NULL) { 1418 error = priv_check(td, 1419 PRIV_NETINET_SETHDROPTS); 1420 if (error) 1421 break; 1422 } 1423 /* FALLTHROUGH */ 1424 case IPV6_UNICAST_HOPS: 1425 case IPV6_HOPLIMIT: 1426 case IPV6_FAITH: 1427 1428 case IPV6_RECVPKTINFO: 1429 case IPV6_RECVHOPLIMIT: 1430 case IPV6_RECVRTHDR: 1431 case IPV6_RECVPATHMTU: 1432 case IPV6_RECVTCLASS: 1433 case IPV6_V6ONLY: 1434 case IPV6_AUTOFLOWLABEL: 1435 if (optlen != sizeof(int)) { 1436 error = EINVAL; 1437 break; 1438 } 1439 error = sooptcopyin(sopt, &optval, 1440 sizeof optval, sizeof optval); 1441 if (error) 1442 break; 1443 switch (optname) { 1444 1445 case IPV6_UNICAST_HOPS: 1446 if (optval < -1 || optval >= 256) 1447 error = EINVAL; 1448 else { 1449 /* -1 = kernel default */ 1450 in6p->in6p_hops = optval; 1451 if ((in6p->in6p_vflag & 1452 INP_IPV4) != 0) 1453 in6p->inp_ip_ttl = optval; 1454 } 1455 break; 1456#define OPTSET(bit) \ 1457do { \ 1458 if (optval) \ 1459 in6p->in6p_flags |= (bit); \ 1460 else \ 1461 in6p->in6p_flags &= ~(bit); \ 1462} while (/*CONSTCOND*/ 0) 1463#define OPTSET2292(bit) \ 1464do { \ 1465 in6p->in6p_flags |= IN6P_RFC2292; \ 1466 if (optval) \ 1467 in6p->in6p_flags |= (bit); \ 1468 else \ 1469 in6p->in6p_flags &= ~(bit); \ 1470} while (/*CONSTCOND*/ 0) 1471#define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) 1472 1473 case IPV6_RECVPKTINFO: 1474 /* cannot mix with RFC2292 */ 1475 if (OPTBIT(IN6P_RFC2292)) { 1476 error = EINVAL; 1477 break; 1478 } 1479 OPTSET(IN6P_PKTINFO); 1480 break; 1481 1482 case IPV6_HOPLIMIT: 1483 { 1484 struct ip6_pktopts **optp; 1485 1486 /* cannot mix with RFC2292 */ 1487 if (OPTBIT(IN6P_RFC2292)) { 1488 error = EINVAL; 1489 break; 1490 } 1491 optp = &in6p->in6p_outputopts; 1492 error = ip6_pcbopt(IPV6_HOPLIMIT, 1493 (u_char *)&optval, sizeof(optval), 1494 optp, (td != NULL) ? td->td_ucred : 1495 NULL, uproto); 1496 break; 1497 } 1498 1499 case IPV6_RECVHOPLIMIT: 1500 /* cannot mix with RFC2292 */ 1501 if (OPTBIT(IN6P_RFC2292)) { 1502 error = EINVAL; 1503 break; 1504 } 1505 OPTSET(IN6P_HOPLIMIT); 1506 break; 1507 1508 case IPV6_RECVHOPOPTS: 1509 /* cannot mix with RFC2292 */ 1510 if (OPTBIT(IN6P_RFC2292)) { 1511 error = EINVAL; 1512 break; 1513 } 1514 OPTSET(IN6P_HOPOPTS); 1515 break; 1516 1517 case IPV6_RECVDSTOPTS: 1518 /* cannot mix with RFC2292 */ 1519 if (OPTBIT(IN6P_RFC2292)) { 1520 error = EINVAL; 1521 break; 1522 } 1523 OPTSET(IN6P_DSTOPTS); 1524 break; 1525 1526 case IPV6_RECVRTHDRDSTOPTS: 1527 /* cannot mix with RFC2292 */ 1528 if (OPTBIT(IN6P_RFC2292)) { 1529 error = EINVAL; 1530 break; 1531 } 1532 OPTSET(IN6P_RTHDRDSTOPTS); 1533 break; 1534 1535 case IPV6_RECVRTHDR: 1536 /* cannot mix with RFC2292 */ 1537 if (OPTBIT(IN6P_RFC2292)) { 1538 error = EINVAL; 1539 break; 1540 } 1541 OPTSET(IN6P_RTHDR); 1542 break; 1543 1544 case IPV6_FAITH: 1545 OPTSET(IN6P_FAITH); 1546 break; 1547 1548 case IPV6_RECVPATHMTU: 1549 /* 1550 * We ignore this option for TCP 1551 * sockets. 1552 * (RFC3542 leaves this case 1553 * unspecified.) 1554 */ 1555 if (uproto != IPPROTO_TCP) 1556 OPTSET(IN6P_MTU); 1557 break; 1558 1559 case IPV6_V6ONLY: 1560 /* 1561 * make setsockopt(IPV6_V6ONLY) 1562 * available only prior to bind(2). 1563 * see ipng mailing list, Jun 22 2001. 1564 */ 1565 if (in6p->in6p_lport || 1566 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1567 error = EINVAL; 1568 break; 1569 } 1570 OPTSET(IN6P_IPV6_V6ONLY); 1571 if (optval) 1572 in6p->in6p_vflag &= ~INP_IPV4; 1573 else 1574 in6p->in6p_vflag |= INP_IPV4; 1575 break; 1576 case IPV6_RECVTCLASS: 1577 /* cannot mix with RFC2292 XXX */ 1578 if (OPTBIT(IN6P_RFC2292)) { 1579 error = EINVAL; 1580 break; 1581 } 1582 OPTSET(IN6P_TCLASS); 1583 break; 1584 case IPV6_AUTOFLOWLABEL: 1585 OPTSET(IN6P_AUTOFLOWLABEL); 1586 break; 1587 1588 } 1589 break; 1590 1591 case IPV6_TCLASS: 1592 case IPV6_DONTFRAG: 1593 case IPV6_USE_MIN_MTU: 1594 case IPV6_PREFER_TEMPADDR: 1595 if (optlen != sizeof(optval)) { 1596 error = EINVAL; 1597 break; 1598 } 1599 error = sooptcopyin(sopt, &optval, 1600 sizeof optval, sizeof optval); 1601 if (error) 1602 break; 1603 { 1604 struct ip6_pktopts **optp; 1605 optp = &in6p->in6p_outputopts; 1606 error = ip6_pcbopt(optname, 1607 (u_char *)&optval, sizeof(optval), 1608 optp, (td != NULL) ? td->td_ucred : 1609 NULL, uproto); 1610 break; 1611 } 1612 1613 case IPV6_2292PKTINFO: 1614 case IPV6_2292HOPLIMIT: 1615 case IPV6_2292HOPOPTS: 1616 case IPV6_2292DSTOPTS: 1617 case IPV6_2292RTHDR: 1618 /* RFC 2292 */ 1619 if (optlen != sizeof(int)) { 1620 error = EINVAL; 1621 break; 1622 } 1623 error = sooptcopyin(sopt, &optval, 1624 sizeof optval, sizeof optval); 1625 if (error) 1626 break; 1627 switch (optname) { 1628 case IPV6_2292PKTINFO: 1629 OPTSET2292(IN6P_PKTINFO); 1630 break; 1631 case IPV6_2292HOPLIMIT: 1632 OPTSET2292(IN6P_HOPLIMIT); 1633 break; 1634 case IPV6_2292HOPOPTS: 1635 /* 1636 * Check super-user privilege. 1637 * See comments for IPV6_RECVHOPOPTS. 1638 */ 1639 if (td != NULL) { 1640 error = priv_check(td, 1641 PRIV_NETINET_SETHDROPTS); 1642 if (error) 1643 return (error); 1644 } 1645 OPTSET2292(IN6P_HOPOPTS); 1646 break; 1647 case IPV6_2292DSTOPTS: 1648 if (td != NULL) { 1649 error = priv_check(td, 1650 PRIV_NETINET_SETHDROPTS); 1651 if (error) 1652 return (error); 1653 } 1654 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1655 break; 1656 case IPV6_2292RTHDR: 1657 OPTSET2292(IN6P_RTHDR); 1658 break; 1659 } 1660 break; 1661 case IPV6_PKTINFO: 1662 case IPV6_HOPOPTS: 1663 case IPV6_RTHDR: 1664 case IPV6_DSTOPTS: 1665 case IPV6_RTHDRDSTOPTS: 1666 case IPV6_NEXTHOP: 1667 { 1668 /* new advanced API (RFC3542) */ 1669 u_char *optbuf; 1670 u_char optbuf_storage[MCLBYTES]; 1671 int optlen; 1672 struct ip6_pktopts **optp; 1673 1674 /* cannot mix with RFC2292 */ 1675 if (OPTBIT(IN6P_RFC2292)) { 1676 error = EINVAL; 1677 break; 1678 } 1679 1680 /* 1681 * We only ensure valsize is not too large 1682 * here. Further validation will be done 1683 * later. 1684 */ 1685 error = sooptcopyin(sopt, optbuf_storage, 1686 sizeof(optbuf_storage), 0); 1687 if (error) 1688 break; 1689 optlen = sopt->sopt_valsize; 1690 optbuf = optbuf_storage; 1691 optp = &in6p->in6p_outputopts; 1692 error = ip6_pcbopt(optname, optbuf, optlen, 1693 optp, (td != NULL) ? td->td_ucred : NULL, 1694 uproto); 1695 break; 1696 } 1697#undef OPTSET 1698 1699 case IPV6_MULTICAST_IF: 1700 case IPV6_MULTICAST_HOPS: 1701 case IPV6_MULTICAST_LOOP: 1702 case IPV6_JOIN_GROUP: 1703 case IPV6_LEAVE_GROUP: 1704 { 1705 if (sopt->sopt_valsize > MLEN) { 1706 error = EMSGSIZE; 1707 break; 1708 } 1709 /* XXX */ 1710 } 1711 /* FALLTHROUGH */ 1712 { 1713 struct mbuf *m; 1714 1715 if (sopt->sopt_valsize > MCLBYTES) { 1716 error = EMSGSIZE; 1717 break; 1718 } 1719 /* XXX */ 1720 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA); 1721 if (m == 0) { 1722 error = ENOBUFS; 1723 break; 1724 } 1725 if (sopt->sopt_valsize > MLEN) { 1726 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT); 1727 if ((m->m_flags & M_EXT) == 0) { 1728 m_free(m); 1729 error = ENOBUFS; 1730 break; 1731 } 1732 } 1733 m->m_len = sopt->sopt_valsize; 1734 error = sooptcopyin(sopt, mtod(m, char *), 1735 m->m_len, m->m_len); 1736 if (error) { 1737 (void)m_free(m); 1738 break; 1739 } 1740 error = ip6_setmoptions(sopt->sopt_name, 1741 &in6p->in6p_moptions, 1742 m); 1743 (void)m_free(m); 1744 } 1745 break; 1746 1747 case IPV6_PORTRANGE: 1748 error = sooptcopyin(sopt, &optval, 1749 sizeof optval, sizeof optval); 1750 if (error) 1751 break; 1752 1753 switch (optval) { 1754 case IPV6_PORTRANGE_DEFAULT: 1755 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1756 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1757 break; 1758 1759 case IPV6_PORTRANGE_HIGH: 1760 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1761 in6p->in6p_flags |= IN6P_HIGHPORT; 1762 break; 1763 1764 case IPV6_PORTRANGE_LOW: 1765 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1766 in6p->in6p_flags |= IN6P_LOWPORT; 1767 break; 1768 1769 default: 1770 error = EINVAL; 1771 break; 1772 } 1773 break; 1774 1775#ifdef IPSEC 1776 case IPV6_IPSEC_POLICY: 1777 { 1778 caddr_t req; 1779 struct mbuf *m; 1780 1781 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1782 break; 1783 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1784 break; 1785 req = mtod(m, caddr_t); 1786 error = ipsec6_set_policy(in6p, optname, req, 1787 m->m_len, (sopt->sopt_td != NULL) ? 1788 sopt->sopt_td->td_ucred : NULL); 1789 m_freem(m); 1790 break; 1791 } 1792#endif /* IPSEC */ 1793 1794 default: 1795 error = ENOPROTOOPT; 1796 break; 1797 } 1798 break; 1799 1800 case SOPT_GET: 1801 switch (optname) { 1802 1803 case IPV6_2292PKTOPTIONS: 1804#ifdef IPV6_PKTOPTIONS 1805 case IPV6_PKTOPTIONS: 1806#endif 1807 /* 1808 * RFC3542 (effectively) deprecated the 1809 * semantics of the 2292-style pktoptions. 1810 * Since it was not reliable in nature (i.e., 1811 * applications had to expect the lack of some 1812 * information after all), it would make sense 1813 * to simplify this part by always returning 1814 * empty data. 1815 */ 1816 sopt->sopt_valsize = 0; 1817 break; 1818 1819 case IPV6_RECVHOPOPTS: 1820 case IPV6_RECVDSTOPTS: 1821 case IPV6_RECVRTHDRDSTOPTS: 1822 case IPV6_UNICAST_HOPS: 1823 case IPV6_RECVPKTINFO: 1824 case IPV6_RECVHOPLIMIT: 1825 case IPV6_RECVRTHDR: 1826 case IPV6_RECVPATHMTU: 1827 1828 case IPV6_FAITH: 1829 case IPV6_V6ONLY: 1830 case IPV6_PORTRANGE: 1831 case IPV6_RECVTCLASS: 1832 case IPV6_AUTOFLOWLABEL: 1833 switch (optname) { 1834 1835 case IPV6_RECVHOPOPTS: 1836 optval = OPTBIT(IN6P_HOPOPTS); 1837 break; 1838 1839 case IPV6_RECVDSTOPTS: 1840 optval = OPTBIT(IN6P_DSTOPTS); 1841 break; 1842 1843 case IPV6_RECVRTHDRDSTOPTS: 1844 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 1845 break; 1846 1847 case IPV6_UNICAST_HOPS: 1848 optval = in6p->in6p_hops; 1849 break; 1850 1851 case IPV6_RECVPKTINFO: 1852 optval = OPTBIT(IN6P_PKTINFO); 1853 break; 1854 1855 case IPV6_RECVHOPLIMIT: 1856 optval = OPTBIT(IN6P_HOPLIMIT); 1857 break; 1858 1859 case IPV6_RECVRTHDR: 1860 optval = OPTBIT(IN6P_RTHDR); 1861 break; 1862 1863 case IPV6_RECVPATHMTU: 1864 optval = OPTBIT(IN6P_MTU); 1865 break; 1866 1867 case IPV6_FAITH: 1868 optval = OPTBIT(IN6P_FAITH); 1869 break; 1870 1871 case IPV6_V6ONLY: 1872 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1873 break; 1874 1875 case IPV6_PORTRANGE: 1876 { 1877 int flags; 1878 flags = in6p->in6p_flags; 1879 if (flags & IN6P_HIGHPORT) 1880 optval = IPV6_PORTRANGE_HIGH; 1881 else if (flags & IN6P_LOWPORT) 1882 optval = IPV6_PORTRANGE_LOW; 1883 else 1884 optval = 0; 1885 break; 1886 } 1887 case IPV6_RECVTCLASS: 1888 optval = OPTBIT(IN6P_TCLASS); 1889 break; 1890 1891 case IPV6_AUTOFLOWLABEL: 1892 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 1893 break; 1894 } 1895 if (error) 1896 break; 1897 error = sooptcopyout(sopt, &optval, 1898 sizeof optval); 1899 break; 1900 1901 case IPV6_PATHMTU: 1902 { 1903 u_long pmtu = 0; 1904 struct ip6_mtuinfo mtuinfo; 1905 struct route_in6 sro; 1906 1907 bzero(&sro, sizeof(sro)); 1908 1909 if (!(so->so_state & SS_ISCONNECTED)) 1910 return (ENOTCONN); 1911 /* 1912 * XXX: we dot not consider the case of source 1913 * routing, or optional information to specify 1914 * the outgoing interface. 1915 */ 1916 error = ip6_getpmtu(&sro, NULL, NULL, 1917 &in6p->in6p_faddr, &pmtu, NULL); 1918 if (sro.ro_rt) 1919 RTFREE(sro.ro_rt); 1920 if (error) 1921 break; 1922 if (pmtu > IPV6_MAXPACKET) 1923 pmtu = IPV6_MAXPACKET; 1924 1925 bzero(&mtuinfo, sizeof(mtuinfo)); 1926 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 1927 optdata = (void *)&mtuinfo; 1928 optdatalen = sizeof(mtuinfo); 1929 error = sooptcopyout(sopt, optdata, 1930 optdatalen); 1931 break; 1932 } 1933 1934 case IPV6_2292PKTINFO: 1935 case IPV6_2292HOPLIMIT: 1936 case IPV6_2292HOPOPTS: 1937 case IPV6_2292RTHDR: 1938 case IPV6_2292DSTOPTS: 1939 switch (optname) { 1940 case IPV6_2292PKTINFO: 1941 optval = OPTBIT(IN6P_PKTINFO); 1942 break; 1943 case IPV6_2292HOPLIMIT: 1944 optval = OPTBIT(IN6P_HOPLIMIT); 1945 break; 1946 case IPV6_2292HOPOPTS: 1947 optval = OPTBIT(IN6P_HOPOPTS); 1948 break; 1949 case IPV6_2292RTHDR: 1950 optval = OPTBIT(IN6P_RTHDR); 1951 break; 1952 case IPV6_2292DSTOPTS: 1953 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1954 break; 1955 } 1956 error = sooptcopyout(sopt, &optval, 1957 sizeof optval); 1958 break; 1959 case IPV6_PKTINFO: 1960 case IPV6_HOPOPTS: 1961 case IPV6_RTHDR: 1962 case IPV6_DSTOPTS: 1963 case IPV6_RTHDRDSTOPTS: 1964 case IPV6_NEXTHOP: 1965 case IPV6_TCLASS: 1966 case IPV6_DONTFRAG: 1967 case IPV6_USE_MIN_MTU: 1968 case IPV6_PREFER_TEMPADDR: 1969 error = ip6_getpcbopt(in6p->in6p_outputopts, 1970 optname, sopt); 1971 break; 1972 1973 case IPV6_MULTICAST_IF: 1974 case IPV6_MULTICAST_HOPS: 1975 case IPV6_MULTICAST_LOOP: 1976 case IPV6_JOIN_GROUP: 1977 case IPV6_LEAVE_GROUP: 1978 { 1979 struct mbuf *m; 1980 error = ip6_getmoptions(sopt->sopt_name, 1981 in6p->in6p_moptions, &m); 1982 if (error == 0) 1983 error = sooptcopyout(sopt, 1984 mtod(m, char *), m->m_len); 1985 m_freem(m); 1986 } 1987 break; 1988 1989#ifdef IPSEC 1990 case IPV6_IPSEC_POLICY: 1991 { 1992 caddr_t req = NULL; 1993 size_t len = 0; 1994 struct mbuf *m = NULL; 1995 struct mbuf **mp = &m; 1996 size_t ovalsize = sopt->sopt_valsize; 1997 caddr_t oval = (caddr_t)sopt->sopt_val; 1998 1999 error = soopt_getm(sopt, &m); /* XXX */ 2000 if (error != 0) 2001 break; 2002 error = soopt_mcopyin(sopt, m); /* XXX */ 2003 if (error != 0) 2004 break; 2005 sopt->sopt_valsize = ovalsize; 2006 sopt->sopt_val = oval; 2007 if (m) { 2008 req = mtod(m, caddr_t); 2009 len = m->m_len; 2010 } 2011 error = ipsec6_get_policy(in6p, req, len, mp); 2012 if (error == 0) 2013 error = soopt_mcopyout(sopt, m); /* XXX */ 2014 if (error == 0 && m) 2015 m_freem(m); 2016 break; 2017 } 2018#endif /* IPSEC */ 2019 2020 default: 2021 error = ENOPROTOOPT; 2022 break; 2023 } 2024 break; 2025 } 2026 } else { /* level != IPPROTO_IPV6 */ 2027 error = EINVAL; 2028 } 2029 return (error); 2030} 2031 2032int 2033ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt) 2034{ 2035 int error = 0, optval, optlen; 2036 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 2037 struct in6pcb *in6p = sotoin6pcb(so); 2038 int level, op, optname; 2039 2040 if (sopt) { 2041 level = sopt->sopt_level; 2042 op = sopt->sopt_dir; 2043 optname = sopt->sopt_name; 2044 optlen = sopt->sopt_valsize; 2045 } else 2046 panic("ip6_raw_ctloutput: arg soopt is NULL"); 2047 2048 if (level != IPPROTO_IPV6) { 2049 return (EINVAL); 2050 } 2051 2052 switch (optname) { 2053 case IPV6_CHECKSUM: 2054 /* 2055 * For ICMPv6 sockets, no modification allowed for checksum 2056 * offset, permit "no change" values to help existing apps. 2057 * 2058 * RFC3542 says: "An attempt to set IPV6_CHECKSUM 2059 * for an ICMPv6 socket will fail." 2060 * The current behavior does not meet RFC3542. 2061 */ 2062 switch (op) { 2063 case SOPT_SET: 2064 if (optlen != sizeof(int)) { 2065 error = EINVAL; 2066 break; 2067 } 2068 error = sooptcopyin(sopt, &optval, sizeof(optval), 2069 sizeof(optval)); 2070 if (error) 2071 break; 2072 if ((optval % 2) != 0) { 2073 /* the API assumes even offset values */ 2074 error = EINVAL; 2075 } else if (so->so_proto->pr_protocol == 2076 IPPROTO_ICMPV6) { 2077 if (optval != icmp6off) 2078 error = EINVAL; 2079 } else 2080 in6p->in6p_cksum = optval; 2081 break; 2082 2083 case SOPT_GET: 2084 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 2085 optval = icmp6off; 2086 else 2087 optval = in6p->in6p_cksum; 2088 2089 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2090 break; 2091 2092 default: 2093 error = EINVAL; 2094 break; 2095 } 2096 break; 2097 2098 default: 2099 error = ENOPROTOOPT; 2100 break; 2101 } 2102 2103 return (error); 2104} 2105 2106/* 2107 * Set up IP6 options in pcb for insertion in output packets or 2108 * specifying behavior of outgoing packets. 2109 */ 2110static int 2111ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, 2112 struct socket *so, struct sockopt *sopt) 2113{ 2114 struct ip6_pktopts *opt = *pktopt; 2115 int error = 0; 2116 struct thread *td = sopt->sopt_td; 2117 2118 /* turn off any old options. */ 2119 if (opt) { 2120#ifdef DIAGNOSTIC 2121 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 2122 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 2123 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 2124 printf("ip6_pcbopts: all specified options are cleared.\n"); 2125#endif 2126 ip6_clearpktopts(opt, -1); 2127 } else 2128 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 2129 *pktopt = NULL; 2130 2131 if (!m || m->m_len == 0) { 2132 /* 2133 * Only turning off any previous options, regardless of 2134 * whether the opt is just created or given. 2135 */ 2136 free(opt, M_IP6OPT); 2137 return (0); 2138 } 2139 2140 /* set options specified by user. */ 2141 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ? 2142 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) { 2143 ip6_clearpktopts(opt, -1); /* XXX: discard all options */ 2144 free(opt, M_IP6OPT); 2145 return (error); 2146 } 2147 *pktopt = opt; 2148 return (0); 2149} 2150 2151/* 2152 * initialize ip6_pktopts. beware that there are non-zero default values in 2153 * the struct. 2154 */ 2155void 2156ip6_initpktopts(struct ip6_pktopts *opt) 2157{ 2158 2159 bzero(opt, sizeof(*opt)); 2160 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 2161 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 2162 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 2163 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; 2164} 2165 2166static int 2167ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, 2168 struct ucred *cred, int uproto) 2169{ 2170 struct ip6_pktopts *opt; 2171 2172 if (*pktopt == NULL) { 2173 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 2174 M_WAITOK); 2175 ip6_initpktopts(*pktopt); 2176 } 2177 opt = *pktopt; 2178 2179 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto)); 2180} 2181 2182static int 2183ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt) 2184{ 2185 void *optdata = NULL; 2186 int optdatalen = 0; 2187 struct ip6_ext *ip6e; 2188 int error = 0; 2189 struct in6_pktinfo null_pktinfo; 2190 int deftclass = 0, on; 2191 int defminmtu = IP6PO_MINMTU_MCASTONLY; 2192 int defpreftemp = IP6PO_TEMPADDR_SYSTEM; 2193 2194 switch (optname) { 2195 case IPV6_PKTINFO: 2196 if (pktopt && pktopt->ip6po_pktinfo) 2197 optdata = (void *)pktopt->ip6po_pktinfo; 2198 else { 2199 /* XXX: we don't have to do this every time... */ 2200 bzero(&null_pktinfo, sizeof(null_pktinfo)); 2201 optdata = (void *)&null_pktinfo; 2202 } 2203 optdatalen = sizeof(struct in6_pktinfo); 2204 break; 2205 case IPV6_TCLASS: 2206 if (pktopt && pktopt->ip6po_tclass >= 0) 2207 optdata = (void *)&pktopt->ip6po_tclass; 2208 else 2209 optdata = (void *)&deftclass; 2210 optdatalen = sizeof(int); 2211 break; 2212 case IPV6_HOPOPTS: 2213 if (pktopt && pktopt->ip6po_hbh) { 2214 optdata = (void *)pktopt->ip6po_hbh; 2215 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; 2216 optdatalen = (ip6e->ip6e_len + 1) << 3; 2217 } 2218 break; 2219 case IPV6_RTHDR: 2220 if (pktopt && pktopt->ip6po_rthdr) { 2221 optdata = (void *)pktopt->ip6po_rthdr; 2222 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; 2223 optdatalen = (ip6e->ip6e_len + 1) << 3; 2224 } 2225 break; 2226 case IPV6_RTHDRDSTOPTS: 2227 if (pktopt && pktopt->ip6po_dest1) { 2228 optdata = (void *)pktopt->ip6po_dest1; 2229 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; 2230 optdatalen = (ip6e->ip6e_len + 1) << 3; 2231 } 2232 break; 2233 case IPV6_DSTOPTS: 2234 if (pktopt && pktopt->ip6po_dest2) { 2235 optdata = (void *)pktopt->ip6po_dest2; 2236 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; 2237 optdatalen = (ip6e->ip6e_len + 1) << 3; 2238 } 2239 break; 2240 case IPV6_NEXTHOP: 2241 if (pktopt && pktopt->ip6po_nexthop) { 2242 optdata = (void *)pktopt->ip6po_nexthop; 2243 optdatalen = pktopt->ip6po_nexthop->sa_len; 2244 } 2245 break; 2246 case IPV6_USE_MIN_MTU: 2247 if (pktopt) 2248 optdata = (void *)&pktopt->ip6po_minmtu; 2249 else 2250 optdata = (void *)&defminmtu; 2251 optdatalen = sizeof(int); 2252 break; 2253 case IPV6_DONTFRAG: 2254 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 2255 on = 1; 2256 else 2257 on = 0; 2258 optdata = (void *)&on; 2259 optdatalen = sizeof(on); 2260 break; 2261 case IPV6_PREFER_TEMPADDR: 2262 if (pktopt) 2263 optdata = (void *)&pktopt->ip6po_prefer_tempaddr; 2264 else 2265 optdata = (void *)&defpreftemp; 2266 optdatalen = sizeof(int); 2267 break; 2268 default: /* should not happen */ 2269#ifdef DIAGNOSTIC 2270 panic("ip6_getpcbopt: unexpected option\n"); 2271#endif 2272 return (ENOPROTOOPT); 2273 } 2274 2275 error = sooptcopyout(sopt, optdata, optdatalen); 2276 2277 return (error); 2278} 2279 2280void 2281ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) 2282{ 2283 if (pktopt == NULL) 2284 return; 2285 2286 if (optname == -1 || optname == IPV6_PKTINFO) { 2287 if (pktopt->ip6po_pktinfo) 2288 free(pktopt->ip6po_pktinfo, M_IP6OPT); 2289 pktopt->ip6po_pktinfo = NULL; 2290 } 2291 if (optname == -1 || optname == IPV6_HOPLIMIT) 2292 pktopt->ip6po_hlim = -1; 2293 if (optname == -1 || optname == IPV6_TCLASS) 2294 pktopt->ip6po_tclass = -1; 2295 if (optname == -1 || optname == IPV6_NEXTHOP) { 2296 if (pktopt->ip6po_nextroute.ro_rt) { 2297 RTFREE(pktopt->ip6po_nextroute.ro_rt); 2298 pktopt->ip6po_nextroute.ro_rt = NULL; 2299 } 2300 if (pktopt->ip6po_nexthop) 2301 free(pktopt->ip6po_nexthop, M_IP6OPT); 2302 pktopt->ip6po_nexthop = NULL; 2303 } 2304 if (optname == -1 || optname == IPV6_HOPOPTS) { 2305 if (pktopt->ip6po_hbh) 2306 free(pktopt->ip6po_hbh, M_IP6OPT); 2307 pktopt->ip6po_hbh = NULL; 2308 } 2309 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 2310 if (pktopt->ip6po_dest1) 2311 free(pktopt->ip6po_dest1, M_IP6OPT); 2312 pktopt->ip6po_dest1 = NULL; 2313 } 2314 if (optname == -1 || optname == IPV6_RTHDR) { 2315 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 2316 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 2317 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 2318 if (pktopt->ip6po_route.ro_rt) { 2319 RTFREE(pktopt->ip6po_route.ro_rt); 2320 pktopt->ip6po_route.ro_rt = NULL; 2321 } 2322 } 2323 if (optname == -1 || optname == IPV6_DSTOPTS) { 2324 if (pktopt->ip6po_dest2) 2325 free(pktopt->ip6po_dest2, M_IP6OPT); 2326 pktopt->ip6po_dest2 = NULL; 2327 } 2328} 2329 2330#define PKTOPT_EXTHDRCPY(type) \ 2331do {\ 2332 if (src->type) {\ 2333 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 2334 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 2335 if (dst->type == NULL && canwait == M_NOWAIT)\ 2336 goto bad;\ 2337 bcopy(src->type, dst->type, hlen);\ 2338 }\ 2339} while (/*CONSTCOND*/ 0) 2340 2341static int 2342copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) 2343{ 2344 if (dst == NULL || src == NULL) { 2345 printf("ip6_clearpktopts: invalid argument\n"); 2346 return (EINVAL); 2347 } 2348 2349 dst->ip6po_hlim = src->ip6po_hlim; 2350 dst->ip6po_tclass = src->ip6po_tclass; 2351 dst->ip6po_flags = src->ip6po_flags; 2352 if (src->ip6po_pktinfo) { 2353 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2354 M_IP6OPT, canwait); 2355 if (dst->ip6po_pktinfo == NULL) 2356 goto bad; 2357 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2358 } 2359 if (src->ip6po_nexthop) { 2360 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2361 M_IP6OPT, canwait); 2362 if (dst->ip6po_nexthop == NULL) 2363 goto bad; 2364 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 2365 src->ip6po_nexthop->sa_len); 2366 } 2367 PKTOPT_EXTHDRCPY(ip6po_hbh); 2368 PKTOPT_EXTHDRCPY(ip6po_dest1); 2369 PKTOPT_EXTHDRCPY(ip6po_dest2); 2370 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2371 return (0); 2372 2373 bad: 2374 ip6_clearpktopts(dst, -1); 2375 return (ENOBUFS); 2376} 2377#undef PKTOPT_EXTHDRCPY 2378 2379struct ip6_pktopts * 2380ip6_copypktopts(struct ip6_pktopts *src, int canwait) 2381{ 2382 int error; 2383 struct ip6_pktopts *dst; 2384 2385 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2386 if (dst == NULL) 2387 return (NULL); 2388 ip6_initpktopts(dst); 2389 2390 if ((error = copypktopts(dst, src, canwait)) != 0) { 2391 free(dst, M_IP6OPT); 2392 return (NULL); 2393 } 2394 2395 return (dst); 2396} 2397 2398void 2399ip6_freepcbopts(struct ip6_pktopts *pktopt) 2400{ 2401 if (pktopt == NULL) 2402 return; 2403 2404 ip6_clearpktopts(pktopt, -1); 2405 2406 free(pktopt, M_IP6OPT); 2407} 2408 2409/* 2410 * Set the IP6 multicast options in response to user setsockopt(). 2411 */ 2412static int 2413ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m) 2414{ 2415 int error = 0; 2416 u_int loop, ifindex; 2417 struct ipv6_mreq *mreq; 2418 struct ifnet *ifp; 2419 struct ip6_moptions *im6o = *im6op; 2420 struct route_in6 ro; 2421 struct in6_multi_mship *imm; 2422 2423 if (im6o == NULL) { 2424 /* 2425 * No multicast option buffer attached to the pcb; 2426 * allocate one and initialize to default values. 2427 */ 2428 im6o = (struct ip6_moptions *) 2429 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK); 2430 2431 if (im6o == NULL) 2432 return (ENOBUFS); 2433 *im6op = im6o; 2434 im6o->im6o_multicast_ifp = NULL; 2435 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2436 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 2437 LIST_INIT(&im6o->im6o_memberships); 2438 } 2439 2440 switch (optname) { 2441 2442 case IPV6_MULTICAST_IF: 2443 /* 2444 * Select the interface for outgoing multicast packets. 2445 */ 2446 if (m == NULL || m->m_len != sizeof(u_int)) { 2447 error = EINVAL; 2448 break; 2449 } 2450 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex)); 2451 if (ifindex < 0 || if_index < ifindex) { 2452 error = ENXIO; /* XXX EINVAL? */ 2453 break; 2454 } 2455 ifp = ifnet_byindex(ifindex); 2456 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2457 error = EADDRNOTAVAIL; 2458 break; 2459 } 2460 im6o->im6o_multicast_ifp = ifp; 2461 break; 2462 2463 case IPV6_MULTICAST_HOPS: 2464 { 2465 /* 2466 * Set the IP6 hoplimit for outgoing multicast packets. 2467 */ 2468 int optval; 2469 if (m == NULL || m->m_len != sizeof(int)) { 2470 error = EINVAL; 2471 break; 2472 } 2473 bcopy(mtod(m, u_int *), &optval, sizeof(optval)); 2474 if (optval < -1 || optval >= 256) 2475 error = EINVAL; 2476 else if (optval == -1) 2477 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2478 else 2479 im6o->im6o_multicast_hlim = optval; 2480 break; 2481 } 2482 2483 case IPV6_MULTICAST_LOOP: 2484 /* 2485 * Set the loopback flag for outgoing multicast packets. 2486 * Must be zero or one. 2487 */ 2488 if (m == NULL || m->m_len != sizeof(u_int)) { 2489 error = EINVAL; 2490 break; 2491 } 2492 bcopy(mtod(m, u_int *), &loop, sizeof(loop)); 2493 if (loop > 1) { 2494 error = EINVAL; 2495 break; 2496 } 2497 im6o->im6o_multicast_loop = loop; 2498 break; 2499 2500 case IPV6_JOIN_GROUP: 2501 /* 2502 * Add a multicast group membership. 2503 * Group must be a valid IP6 multicast address. 2504 */ 2505 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2506 error = EINVAL; 2507 break; 2508 } 2509 mreq = mtod(m, struct ipv6_mreq *); 2510 2511 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2512 /* 2513 * We use the unspecified address to specify to accept 2514 * all multicast addresses. Only super user is allowed 2515 * to do this. 2516 */ 2517 /* XXX-BZ might need a better PRIV_NETINET_x for this */ 2518 error = priv_check(curthread, PRIV_NETINET_MROUTE); 2519 if (error) 2520 break; 2521 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2522 error = EINVAL; 2523 break; 2524 } 2525 2526 /* 2527 * If no interface was explicitly specified, choose an 2528 * appropriate one according to the given multicast address. 2529 */ 2530 if (mreq->ipv6mr_interface == 0) { 2531 struct sockaddr_in6 *dst; 2532 2533 /* 2534 * Look up the routing table for the 2535 * address, and choose the outgoing interface. 2536 * XXX: is it a good approach? 2537 */ 2538 ro.ro_rt = NULL; 2539 dst = (struct sockaddr_in6 *)&ro.ro_dst; 2540 bzero(dst, sizeof(*dst)); 2541 dst->sin6_family = AF_INET6; 2542 dst->sin6_len = sizeof(*dst); 2543 dst->sin6_addr = mreq->ipv6mr_multiaddr; 2544 rtalloc((struct route *)&ro); 2545 if (ro.ro_rt == NULL) { 2546 error = EADDRNOTAVAIL; 2547 break; 2548 } 2549 ifp = ro.ro_rt->rt_ifp; 2550 RTFREE(ro.ro_rt); 2551 } else { 2552 /* 2553 * If the interface is specified, validate it. 2554 */ 2555 if (mreq->ipv6mr_interface < 0 || 2556 if_index < mreq->ipv6mr_interface) { 2557 error = ENXIO; /* XXX EINVAL? */ 2558 break; 2559 } 2560 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2561 if (!ifp) { 2562 error = ENXIO; /* XXX EINVAL? */ 2563 break; 2564 } 2565 } 2566 2567 /* 2568 * See if we found an interface, and confirm that it 2569 * supports multicast 2570 */ 2571 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2572 error = EADDRNOTAVAIL; 2573 break; 2574 } 2575 2576 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) { 2577 error = EADDRNOTAVAIL; /* XXX: should not happen */ 2578 break; 2579 } 2580 2581 /* 2582 * See if the membership already exists. 2583 */ 2584 for (imm = im6o->im6o_memberships.lh_first; 2585 imm != NULL; imm = imm->i6mm_chain.le_next) 2586 if (imm->i6mm_maddr->in6m_ifp == ifp && 2587 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2588 &mreq->ipv6mr_multiaddr)) 2589 break; 2590 if (imm != NULL) { 2591 error = EADDRINUSE; 2592 break; 2593 } 2594 /* 2595 * Everything looks good; add a new record to the multicast 2596 * address list for the given interface. 2597 */ 2598 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0); 2599 if (imm == NULL) 2600 break; 2601 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2602 break; 2603 2604 case IPV6_LEAVE_GROUP: 2605 /* 2606 * Drop a multicast group membership. 2607 * Group must be a valid IP6 multicast address. 2608 */ 2609 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2610 error = EINVAL; 2611 break; 2612 } 2613 mreq = mtod(m, struct ipv6_mreq *); 2614 2615 /* 2616 * If an interface address was specified, get a pointer 2617 * to its ifnet structure. 2618 */ 2619 if (mreq->ipv6mr_interface < 0 || 2620 if_index < mreq->ipv6mr_interface) { 2621 error = ENXIO; /* XXX EINVAL? */ 2622 break; 2623 } 2624 if (mreq->ipv6mr_interface == 0) 2625 ifp = NULL; 2626 else 2627 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2628 2629 /* Fill in the scope zone ID */ 2630 if (ifp) { 2631 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) { 2632 /* XXX: should not happen */ 2633 error = EADDRNOTAVAIL; 2634 break; 2635 } 2636 } else if (mreq->ipv6mr_interface != 0) { 2637 /* 2638 * This case happens when the (positive) index is in 2639 * the valid range, but the corresponding interface has 2640 * been detached dynamically (XXX). 2641 */ 2642 error = EADDRNOTAVAIL; 2643 break; 2644 } else { /* ipv6mr_interface == 0 */ 2645 struct sockaddr_in6 sa6_mc; 2646 2647 /* 2648 * The API spec says as follows: 2649 * If the interface index is specified as 0, the 2650 * system may choose a multicast group membership to 2651 * drop by matching the multicast address only. 2652 * On the other hand, we cannot disambiguate the scope 2653 * zone unless an interface is provided. Thus, we 2654 * check if there's ambiguity with the default scope 2655 * zone as the last resort. 2656 */ 2657 bzero(&sa6_mc, sizeof(sa6_mc)); 2658 sa6_mc.sin6_family = AF_INET6; 2659 sa6_mc.sin6_len = sizeof(sa6_mc); 2660 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr; 2661 error = sa6_embedscope(&sa6_mc, ip6_use_defzone); 2662 if (error != 0) 2663 break; 2664 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr; 2665 } 2666 2667 /* 2668 * Find the membership in the membership list. 2669 */ 2670 for (imm = im6o->im6o_memberships.lh_first; 2671 imm != NULL; imm = imm->i6mm_chain.le_next) { 2672 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) && 2673 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2674 &mreq->ipv6mr_multiaddr)) 2675 break; 2676 } 2677 if (imm == NULL) { 2678 /* Unable to resolve interface */ 2679 error = EADDRNOTAVAIL; 2680 break; 2681 } 2682 /* 2683 * Give up the multicast address record to which the 2684 * membership points. 2685 */ 2686 LIST_REMOVE(imm, i6mm_chain); 2687 in6_delmulti(imm->i6mm_maddr); 2688 free(imm, M_IP6MADDR); 2689 break; 2690 2691 default: 2692 error = EOPNOTSUPP; 2693 break; 2694 } 2695 2696 /* 2697 * If all options have default values, no need to keep the mbuf. 2698 */ 2699 if (im6o->im6o_multicast_ifp == NULL && 2700 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2701 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2702 im6o->im6o_memberships.lh_first == NULL) { 2703 free(*im6op, M_IP6MOPTS); 2704 *im6op = NULL; 2705 } 2706 2707 return (error); 2708} 2709 2710/* 2711 * Return the IP6 multicast options in response to user getsockopt(). 2712 */ 2713static int 2714ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp) 2715{ 2716 u_int *hlim, *loop, *ifindex; 2717 2718 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */ 2719 2720 switch (optname) { 2721 2722 case IPV6_MULTICAST_IF: 2723 ifindex = mtod(*mp, u_int *); 2724 (*mp)->m_len = sizeof(u_int); 2725 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2726 *ifindex = 0; 2727 else 2728 *ifindex = im6o->im6o_multicast_ifp->if_index; 2729 return (0); 2730 2731 case IPV6_MULTICAST_HOPS: 2732 hlim = mtod(*mp, u_int *); 2733 (*mp)->m_len = sizeof(u_int); 2734 if (im6o == NULL) 2735 *hlim = ip6_defmcasthlim; 2736 else 2737 *hlim = im6o->im6o_multicast_hlim; 2738 return (0); 2739 2740 case IPV6_MULTICAST_LOOP: 2741 loop = mtod(*mp, u_int *); 2742 (*mp)->m_len = sizeof(u_int); 2743 if (im6o == NULL) 2744 *loop = ip6_defmcasthlim; 2745 else 2746 *loop = im6o->im6o_multicast_loop; 2747 return (0); 2748 2749 default: 2750 return (EOPNOTSUPP); 2751 } 2752} 2753 2754/* 2755 * Discard the IP6 multicast options. 2756 */ 2757void 2758ip6_freemoptions(struct ip6_moptions *im6o) 2759{ 2760 struct in6_multi_mship *imm; 2761 2762 if (im6o == NULL) 2763 return; 2764 2765 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2766 LIST_REMOVE(imm, i6mm_chain); 2767 if (imm->i6mm_maddr) 2768 in6_delmulti(imm->i6mm_maddr); 2769 free(imm, M_IP6MADDR); 2770 } 2771 free(im6o, M_IP6MOPTS); 2772} 2773 2774/* 2775 * Set IPv6 outgoing packet options based on advanced API. 2776 */ 2777int 2778ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2779 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto) 2780{ 2781 struct cmsghdr *cm = 0; 2782 2783 if (control == NULL || opt == NULL) 2784 return (EINVAL); 2785 2786 ip6_initpktopts(opt); 2787 if (stickyopt) { 2788 int error; 2789 2790 /* 2791 * If stickyopt is provided, make a local copy of the options 2792 * for this particular packet, then override them by ancillary 2793 * objects. 2794 * XXX: copypktopts() does not copy the cached route to a next 2795 * hop (if any). This is not very good in terms of efficiency, 2796 * but we can allow this since this option should be rarely 2797 * used. 2798 */ 2799 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2800 return (error); 2801 } 2802 2803 /* 2804 * XXX: Currently, we assume all the optional information is stored 2805 * in a single mbuf. 2806 */ 2807 if (control->m_next) 2808 return (EINVAL); 2809 2810 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2811 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2812 int error; 2813 2814 if (control->m_len < CMSG_LEN(0)) 2815 return (EINVAL); 2816 2817 cm = mtod(control, struct cmsghdr *); 2818 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2819 return (EINVAL); 2820 if (cm->cmsg_level != IPPROTO_IPV6) 2821 continue; 2822 2823 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2824 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); 2825 if (error) 2826 return (error); 2827 } 2828 2829 return (0); 2830} 2831 2832/* 2833 * Set a particular packet option, as a sticky option or an ancillary data 2834 * item. "len" can be 0 only when it's a sticky option. 2835 * We have 4 cases of combination of "sticky" and "cmsg": 2836 * "sticky=0, cmsg=0": impossible 2837 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2838 * "sticky=1, cmsg=0": RFC3542 socket option 2839 * "sticky=1, cmsg=1": RFC2292 socket option 2840 */ 2841static int 2842ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2843 struct ucred *cred, int sticky, int cmsg, int uproto) 2844{ 2845 int minmtupolicy, preftemp; 2846 int error; 2847 2848 if (!sticky && !cmsg) { 2849#ifdef DIAGNOSTIC 2850 printf("ip6_setpktopt: impossible case\n"); 2851#endif 2852 return (EINVAL); 2853 } 2854 2855 /* 2856 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2857 * not be specified in the context of RFC3542. Conversely, 2858 * RFC3542 types should not be specified in the context of RFC2292. 2859 */ 2860 if (!cmsg) { 2861 switch (optname) { 2862 case IPV6_2292PKTINFO: 2863 case IPV6_2292HOPLIMIT: 2864 case IPV6_2292NEXTHOP: 2865 case IPV6_2292HOPOPTS: 2866 case IPV6_2292DSTOPTS: 2867 case IPV6_2292RTHDR: 2868 case IPV6_2292PKTOPTIONS: 2869 return (ENOPROTOOPT); 2870 } 2871 } 2872 if (sticky && cmsg) { 2873 switch (optname) { 2874 case IPV6_PKTINFO: 2875 case IPV6_HOPLIMIT: 2876 case IPV6_NEXTHOP: 2877 case IPV6_HOPOPTS: 2878 case IPV6_DSTOPTS: 2879 case IPV6_RTHDRDSTOPTS: 2880 case IPV6_RTHDR: 2881 case IPV6_USE_MIN_MTU: 2882 case IPV6_DONTFRAG: 2883 case IPV6_TCLASS: 2884 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */ 2885 return (ENOPROTOOPT); 2886 } 2887 } 2888 2889 switch (optname) { 2890 case IPV6_2292PKTINFO: 2891 case IPV6_PKTINFO: 2892 { 2893 struct ifnet *ifp = NULL; 2894 struct in6_pktinfo *pktinfo; 2895 2896 if (len != sizeof(struct in6_pktinfo)) 2897 return (EINVAL); 2898 2899 pktinfo = (struct in6_pktinfo *)buf; 2900 2901 /* 2902 * An application can clear any sticky IPV6_PKTINFO option by 2903 * doing a "regular" setsockopt with ipi6_addr being 2904 * in6addr_any and ipi6_ifindex being zero. 2905 * [RFC 3542, Section 6] 2906 */ 2907 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2908 pktinfo->ipi6_ifindex == 0 && 2909 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2910 ip6_clearpktopts(opt, optname); 2911 break; 2912 } 2913 2914 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2915 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2916 return (EINVAL); 2917 } 2918 2919 /* validate the interface index if specified. */ 2920 if (pktinfo->ipi6_ifindex > if_index || 2921 pktinfo->ipi6_ifindex < 0) { 2922 return (ENXIO); 2923 } 2924 if (pktinfo->ipi6_ifindex) { 2925 ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 2926 if (ifp == NULL) 2927 return (ENXIO); 2928 } 2929 2930 /* 2931 * We store the address anyway, and let in6_selectsrc() 2932 * validate the specified address. This is because ipi6_addr 2933 * may not have enough information about its scope zone, and 2934 * we may need additional information (such as outgoing 2935 * interface or the scope zone of a destination address) to 2936 * disambiguate the scope. 2937 * XXX: the delay of the validation may confuse the 2938 * application when it is used as a sticky option. 2939 */ 2940 if (opt->ip6po_pktinfo == NULL) { 2941 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2942 M_IP6OPT, M_NOWAIT); 2943 if (opt->ip6po_pktinfo == NULL) 2944 return (ENOBUFS); 2945 } 2946 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 2947 break; 2948 } 2949 2950 case IPV6_2292HOPLIMIT: 2951 case IPV6_HOPLIMIT: 2952 { 2953 int *hlimp; 2954 2955 /* 2956 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2957 * to simplify the ordering among hoplimit options. 2958 */ 2959 if (optname == IPV6_HOPLIMIT && sticky) 2960 return (ENOPROTOOPT); 2961 2962 if (len != sizeof(int)) 2963 return (EINVAL); 2964 hlimp = (int *)buf; 2965 if (*hlimp < -1 || *hlimp > 255) 2966 return (EINVAL); 2967 2968 opt->ip6po_hlim = *hlimp; 2969 break; 2970 } 2971 2972 case IPV6_TCLASS: 2973 { 2974 int tclass; 2975 2976 if (len != sizeof(int)) 2977 return (EINVAL); 2978 tclass = *(int *)buf; 2979 if (tclass < -1 || tclass > 255) 2980 return (EINVAL); 2981 2982 opt->ip6po_tclass = tclass; 2983 break; 2984 } 2985 2986 case IPV6_2292NEXTHOP: 2987 case IPV6_NEXTHOP: 2988 if (cred != NULL) { 2989 error = priv_check_cred(cred, 2990 PRIV_NETINET_SETHDROPTS, 0); 2991 if (error) 2992 return (error); 2993 } 2994 2995 if (len == 0) { /* just remove the option */ 2996 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2997 break; 2998 } 2999 3000 /* check if cmsg_len is large enough for sa_len */ 3001 if (len < sizeof(struct sockaddr) || len < *buf) 3002 return (EINVAL); 3003 3004 switch (((struct sockaddr *)buf)->sa_family) { 3005 case AF_INET6: 3006 { 3007 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 3008 int error; 3009 3010 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 3011 return (EINVAL); 3012 3013 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 3014 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 3015 return (EINVAL); 3016 } 3017 if ((error = sa6_embedscope(sa6, ip6_use_defzone)) 3018 != 0) { 3019 return (error); 3020 } 3021 break; 3022 } 3023 case AF_LINK: /* should eventually be supported */ 3024 default: 3025 return (EAFNOSUPPORT); 3026 } 3027 3028 /* turn off the previous option, then set the new option. */ 3029 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3030 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 3031 if (opt->ip6po_nexthop == NULL) 3032 return (ENOBUFS); 3033 bcopy(buf, opt->ip6po_nexthop, *buf); 3034 break; 3035 3036 case IPV6_2292HOPOPTS: 3037 case IPV6_HOPOPTS: 3038 { 3039 struct ip6_hbh *hbh; 3040 int hbhlen; 3041 3042 /* 3043 * XXX: We don't allow a non-privileged user to set ANY HbH 3044 * options, since per-option restriction has too much 3045 * overhead. 3046 */ 3047 if (cred != NULL) { 3048 error = priv_check_cred(cred, 3049 PRIV_NETINET_SETHDROPTS, 0); 3050 if (error) 3051 return (error); 3052 } 3053 3054 if (len == 0) { 3055 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3056 break; /* just remove the option */ 3057 } 3058 3059 /* message length validation */ 3060 if (len < sizeof(struct ip6_hbh)) 3061 return (EINVAL); 3062 hbh = (struct ip6_hbh *)buf; 3063 hbhlen = (hbh->ip6h_len + 1) << 3; 3064 if (len != hbhlen) 3065 return (EINVAL); 3066 3067 /* turn off the previous option, then set the new option. */ 3068 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3069 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 3070 if (opt->ip6po_hbh == NULL) 3071 return (ENOBUFS); 3072 bcopy(hbh, opt->ip6po_hbh, hbhlen); 3073 3074 break; 3075 } 3076 3077 case IPV6_2292DSTOPTS: 3078 case IPV6_DSTOPTS: 3079 case IPV6_RTHDRDSTOPTS: 3080 { 3081 struct ip6_dest *dest, **newdest = NULL; 3082 int destlen; 3083 3084 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */ 3085 error = priv_check_cred(cred, 3086 PRIV_NETINET_SETHDROPTS, 0); 3087 if (error) 3088 return (error); 3089 } 3090 3091 if (len == 0) { 3092 ip6_clearpktopts(opt, optname); 3093 break; /* just remove the option */ 3094 } 3095 3096 /* message length validation */ 3097 if (len < sizeof(struct ip6_dest)) 3098 return (EINVAL); 3099 dest = (struct ip6_dest *)buf; 3100 destlen = (dest->ip6d_len + 1) << 3; 3101 if (len != destlen) 3102 return (EINVAL); 3103 3104 /* 3105 * Determine the position that the destination options header 3106 * should be inserted; before or after the routing header. 3107 */ 3108 switch (optname) { 3109 case IPV6_2292DSTOPTS: 3110 /* 3111 * The old advacned API is ambiguous on this point. 3112 * Our approach is to determine the position based 3113 * according to the existence of a routing header. 3114 * Note, however, that this depends on the order of the 3115 * extension headers in the ancillary data; the 1st 3116 * part of the destination options header must appear 3117 * before the routing header in the ancillary data, 3118 * too. 3119 * RFC3542 solved the ambiguity by introducing 3120 * separate ancillary data or option types. 3121 */ 3122 if (opt->ip6po_rthdr == NULL) 3123 newdest = &opt->ip6po_dest1; 3124 else 3125 newdest = &opt->ip6po_dest2; 3126 break; 3127 case IPV6_RTHDRDSTOPTS: 3128 newdest = &opt->ip6po_dest1; 3129 break; 3130 case IPV6_DSTOPTS: 3131 newdest = &opt->ip6po_dest2; 3132 break; 3133 } 3134 3135 /* turn off the previous option, then set the new option. */ 3136 ip6_clearpktopts(opt, optname); 3137 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 3138 if (*newdest == NULL) 3139 return (ENOBUFS); 3140 bcopy(dest, *newdest, destlen); 3141 3142 break; 3143 } 3144 3145 case IPV6_2292RTHDR: 3146 case IPV6_RTHDR: 3147 { 3148 struct ip6_rthdr *rth; 3149 int rthlen; 3150 3151 if (len == 0) { 3152 ip6_clearpktopts(opt, IPV6_RTHDR); 3153 break; /* just remove the option */ 3154 } 3155 3156 /* message length validation */ 3157 if (len < sizeof(struct ip6_rthdr)) 3158 return (EINVAL); 3159 rth = (struct ip6_rthdr *)buf; 3160 rthlen = (rth->ip6r_len + 1) << 3; 3161 if (len != rthlen) 3162 return (EINVAL); 3163 3164 switch (rth->ip6r_type) { 3165 case IPV6_RTHDR_TYPE_0: 3166 if (rth->ip6r_len == 0) /* must contain one addr */ 3167 return (EINVAL); 3168 if (rth->ip6r_len % 2) /* length must be even */ 3169 return (EINVAL); 3170 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 3171 return (EINVAL); 3172 break; 3173 default: 3174 return (EINVAL); /* not supported */ 3175 } 3176 3177 /* turn off the previous option */ 3178 ip6_clearpktopts(opt, IPV6_RTHDR); 3179 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 3180 if (opt->ip6po_rthdr == NULL) 3181 return (ENOBUFS); 3182 bcopy(rth, opt->ip6po_rthdr, rthlen); 3183 3184 break; 3185 } 3186 3187 case IPV6_USE_MIN_MTU: 3188 if (len != sizeof(int)) 3189 return (EINVAL); 3190 minmtupolicy = *(int *)buf; 3191 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 3192 minmtupolicy != IP6PO_MINMTU_DISABLE && 3193 minmtupolicy != IP6PO_MINMTU_ALL) { 3194 return (EINVAL); 3195 } 3196 opt->ip6po_minmtu = minmtupolicy; 3197 break; 3198 3199 case IPV6_DONTFRAG: 3200 if (len != sizeof(int)) 3201 return (EINVAL); 3202 3203 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 3204 /* 3205 * we ignore this option for TCP sockets. 3206 * (RFC3542 leaves this case unspecified.) 3207 */ 3208 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 3209 } else 3210 opt->ip6po_flags |= IP6PO_DONTFRAG; 3211 break; 3212 3213 case IPV6_PREFER_TEMPADDR: 3214 if (len != sizeof(int)) 3215 return (EINVAL); 3216 preftemp = *(int *)buf; 3217 if (preftemp != IP6PO_TEMPADDR_SYSTEM && 3218 preftemp != IP6PO_TEMPADDR_NOTPREFER && 3219 preftemp != IP6PO_TEMPADDR_PREFER) { 3220 return (EINVAL); 3221 } 3222 opt->ip6po_prefer_tempaddr = preftemp; 3223 break; 3224 3225 default: 3226 return (ENOPROTOOPT); 3227 } /* end of switch */ 3228 3229 return (0); 3230} 3231 3232/* 3233 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 3234 * packet to the input queue of a specified interface. Note that this 3235 * calls the output routine of the loopback "driver", but with an interface 3236 * pointer that might NOT be &loif -- easier than replicating that code here. 3237 */ 3238void 3239ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst) 3240{ 3241 struct mbuf *copym; 3242 struct ip6_hdr *ip6; 3243 3244 copym = m_copy(m, 0, M_COPYALL); 3245 if (copym == NULL) 3246 return; 3247 3248 /* 3249 * Make sure to deep-copy IPv6 header portion in case the data 3250 * is in an mbuf cluster, so that we can safely override the IPv6 3251 * header portion later. 3252 */ 3253 if ((copym->m_flags & M_EXT) != 0 || 3254 copym->m_len < sizeof(struct ip6_hdr)) { 3255 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 3256 if (copym == NULL) 3257 return; 3258 } 3259 3260#ifdef DIAGNOSTIC 3261 if (copym->m_len < sizeof(*ip6)) { 3262 m_freem(copym); 3263 return; 3264 } 3265#endif 3266 3267 ip6 = mtod(copym, struct ip6_hdr *); 3268 /* 3269 * clear embedded scope identifiers if necessary. 3270 * in6_clearscope will touch the addresses only when necessary. 3271 */ 3272 in6_clearscope(&ip6->ip6_src); 3273 in6_clearscope(&ip6->ip6_dst); 3274 3275 (void)if_simloop(ifp, copym, dst->sin6_family, 0); 3276} 3277 3278/* 3279 * Chop IPv6 header off from the payload. 3280 */ 3281static int 3282ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 3283{ 3284 struct mbuf *mh; 3285 struct ip6_hdr *ip6; 3286 3287 ip6 = mtod(m, struct ip6_hdr *); 3288 if (m->m_len > sizeof(*ip6)) { 3289 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 3290 if (mh == 0) { 3291 m_freem(m); 3292 return ENOBUFS; 3293 } 3294 M_MOVE_PKTHDR(mh, m); 3295 MH_ALIGN(mh, sizeof(*ip6)); 3296 m->m_len -= sizeof(*ip6); 3297 m->m_data += sizeof(*ip6); 3298 mh->m_next = m; 3299 m = mh; 3300 m->m_len = sizeof(*ip6); 3301 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 3302 } 3303 exthdrs->ip6e_ip6 = m; 3304 return 0; 3305} 3306 3307/* 3308 * Compute IPv6 extension header length. 3309 */ 3310int 3311ip6_optlen(struct in6pcb *in6p) 3312{ 3313 int len; 3314 3315 if (!in6p->in6p_outputopts) 3316 return 0; 3317 3318 len = 0; 3319#define elen(x) \ 3320 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 3321 3322 len += elen(in6p->in6p_outputopts->ip6po_hbh); 3323 if (in6p->in6p_outputopts->ip6po_rthdr) 3324 /* dest1 is valid with rthdr only */ 3325 len += elen(in6p->in6p_outputopts->ip6po_dest1); 3326 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 3327 len += elen(in6p->in6p_outputopts->ip6po_dest2); 3328 return len; 3329#undef elen 3330}
|