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 }
|
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}
|