1 /*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ 30 */ 31 32 /*- 33 * Copyright (c) 1982, 1986, 1988, 1990, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 4. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 61 */ 62 63 #include <sys/cdefs.h> 64 __FBSDID("$FreeBSD$"); 65 66 #include "opt_inet.h" 67 #include "opt_inet6.h" 68 #include "opt_ipfw.h" 69 #include "opt_ipsec.h" 70 #include "opt_sctp.h" 71 #include "opt_route.h" 72 #include "opt_rss.h" 73 74 #include <sys/param.h> 75 #include <sys/kernel.h> 76 #include <sys/malloc.h> 77 #include <sys/mbuf.h> 78 #include <sys/errno.h> 79 #include <sys/priv.h> 80 #include <sys/proc.h> 81 #include <sys/protosw.h> 82 #include <sys/socket.h> 83 #include <sys/socketvar.h> 84 #include <sys/syslog.h> 85 #include <sys/ucred.h> 86 87 #include <machine/in_cksum.h> 88 89 #include <net/if.h> 90 #include <net/if_var.h> 91 #include <net/netisr.h> 92 #include <net/route.h> 93 #include <net/pfil.h> 94 #include <net/vnet.h> 95 96 #include <netinet/in.h> 97 #include <netinet/in_var.h> 98 #include <netinet/ip_var.h> 99 #include <netinet6/in6_var.h> 100 #include <netinet/ip6.h> 101 #include <netinet/icmp6.h> 102 #include <netinet6/ip6_var.h> 103 #include <netinet/in_pcb.h> 104 #include <netinet/tcp_var.h> 105 #include <netinet6/nd6.h> 106 #include <netinet/in_rss.h> 107 108 #ifdef IPSEC 109 #include <netipsec/ipsec.h> 110 #include <netipsec/ipsec6.h> 111 #include <netipsec/key.h> 112 #include <netinet6/ip6_ipsec.h> 113 #endif /* IPSEC */ 114 #ifdef SCTP 115 #include <netinet/sctp.h> 116 #include <netinet/sctp_crc32.h> 117 #endif 118 119 #include <netinet6/ip6protosw.h> 120 #include <netinet6/scope6_var.h> 121 122 #ifdef FLOWTABLE 123 #include <net/flowtable.h> 124 #endif 125 126 extern int in6_mcast_loop; 127 128 struct ip6_exthdrs { 129 struct mbuf *ip6e_ip6; 130 struct mbuf *ip6e_hbh; 131 struct mbuf *ip6e_dest1; 132 struct mbuf *ip6e_rthdr; 133 struct mbuf *ip6e_dest2; 134 }; 135 136 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **, 137 struct ucred *, int); 138 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, 139 struct socket *, struct sockopt *); 140 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *); 141 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, 142 struct ucred *, int, int, int); 143 144 static int ip6_copyexthdr(struct mbuf **, caddr_t, int); 145 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int, 146 struct ip6_frag **); 147 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t); 148 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *); 149 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *, 150 struct ifnet *, struct in6_addr *, u_long *, int *, u_int); 151 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int); 152 153 154 /* 155 * Make an extension header from option data. hp is the source, and 156 * mp is the destination. 157 */ 158 #define MAKE_EXTHDR(hp, mp) \ 159 do { \ 160 if (hp) { \ 161 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 162 error = ip6_copyexthdr((mp), (caddr_t)(hp), \ 163 ((eh)->ip6e_len + 1) << 3); \ 164 if (error) \ 165 goto freehdrs; \ 166 } \ 167 } while (/*CONSTCOND*/ 0) 168 169 /* 170 * Form a chain of extension headers. 171 * m is the extension header mbuf 172 * mp is the previous mbuf in the chain 173 * p is the next header 174 * i is the type of option. 175 */ 176 #define MAKE_CHAIN(m, mp, p, i)\ 177 do {\ 178 if (m) {\ 179 if (!hdrsplit) \ 180 panic("assumption failed: hdr not split"); \ 181 *mtod((m), u_char *) = *(p);\ 182 *(p) = (i);\ 183 p = mtod((m), u_char *);\ 184 (m)->m_next = (mp)->m_next;\ 185 (mp)->m_next = (m);\ 186 (mp) = (m);\ 187 }\ 188 } while (/*CONSTCOND*/ 0) 189 190 void 191 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset) 192 { 193 u_short csum; 194 195 csum = in_cksum_skip(m, offset + plen, offset); 196 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0) 197 csum = 0xffff; 198 offset += m->m_pkthdr.csum_data; /* checksum offset */ 199 200 if (offset + sizeof(u_short) > m->m_len) { 201 printf("%s: delayed m_pullup, m->len: %d plen %u off %u " 202 "csum_flags=%b\n", __func__, m->m_len, plen, offset, 203 (int)m->m_pkthdr.csum_flags, CSUM_BITS); 204 /* 205 * XXX this should not happen, but if it does, the correct 206 * behavior may be to insert the checksum in the appropriate 207 * next mbuf in the chain. 208 */ 209 return; 210 } 211 *(u_short *)(m->m_data + offset) = csum; 212 } 213 214 /* 215 * IP6 output. The packet in mbuf chain m contains a skeletal IP6 216 * header (with pri, len, nxt, hlim, src, dst). 217 * This function may modify ver and hlim only. 218 * The mbuf chain containing the packet will be freed. 219 * The mbuf opt, if present, will not be freed. 220 * If route_in6 ro is present and has ro_rt initialized, route lookup would be 221 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL, 222 * then result of route lookup is stored in ro->ro_rt. 223 * 224 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and 225 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one, 226 * which is rt_mtu. 227 * 228 * ifpp - XXX: just for statistics 229 */ 230 /* 231 * XXX TODO: no flowid is assigned for outbound flows? 232 */ 233 int 234 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, 235 struct route_in6 *ro, int flags, struct ip6_moptions *im6o, 236 struct ifnet **ifpp, struct inpcb *inp) 237 { 238 struct ip6_hdr *ip6, *mhip6; 239 struct ifnet *ifp, *origifp; 240 struct mbuf *m = m0; 241 struct mbuf *mprev = NULL; 242 int hlen, tlen, len, off; 243 struct route_in6 ip6route; 244 struct rtentry *rt = NULL; 245 struct sockaddr_in6 *dst, src_sa, dst_sa; 246 struct in6_addr odst; 247 int error = 0; 248 struct in6_ifaddr *ia = NULL; 249 u_long mtu; 250 int alwaysfrag, dontfrag; 251 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; 252 struct ip6_exthdrs exthdrs; 253 struct in6_addr finaldst, src0, dst0; 254 u_int32_t zone; 255 struct route_in6 *ro_pmtu = NULL; 256 int hdrsplit = 0; 257 int sw_csum, tso; 258 int needfiblookup; 259 uint32_t fibnum; 260 struct m_tag *fwd_tag = NULL; 261 262 ip6 = mtod(m, struct ip6_hdr *); 263 if (ip6 == NULL) { 264 printf ("ip6 is NULL"); 265 goto bad; 266 } 267 268 if (inp != NULL) { 269 M_SETFIB(m, inp->inp_inc.inc_fibnum); 270 if ((flags & IP_NODEFAULTFLOWID) == 0) { 271 /* unconditionally set flowid */ 272 m->m_pkthdr.flowid = inp->inp_flowid; 273 M_HASHTYPE_SET(m, inp->inp_flowtype); 274 } 275 } 276 277 finaldst = ip6->ip6_dst; 278 bzero(&exthdrs, sizeof(exthdrs)); 279 if (opt) { 280 /* Hop-by-Hop options header */ 281 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 282 /* Destination options header(1st part) */ 283 if (opt->ip6po_rthdr) { 284 /* 285 * Destination options header(1st part) 286 * This only makes sense with a routing header. 287 * See Section 9.2 of RFC 3542. 288 * Disabling this part just for MIP6 convenience is 289 * a bad idea. We need to think carefully about a 290 * way to make the advanced API coexist with MIP6 291 * options, which might automatically be inserted in 292 * the kernel. 293 */ 294 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 295 } 296 /* Routing header */ 297 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 298 /* Destination options header(2nd part) */ 299 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 300 } 301 302 #ifdef IPSEC 303 /* 304 * IPSec checking which handles several cases. 305 * FAST IPSEC: We re-injected the packet. 306 * XXX: need scope argument. 307 */ 308 switch(ip6_ipsec_output(&m, inp, &error)) 309 { 310 case 1: /* Bad packet */ 311 goto freehdrs; 312 case -1: /* IPSec done */ 313 goto done; 314 case 0: /* No IPSec */ 315 default: 316 break; 317 } 318 #endif /* IPSEC */ 319 320 /* 321 * Calculate the total length of the extension header chain. 322 * Keep the length of the unfragmentable part for fragmentation. 323 */ 324 optlen = 0; 325 if (exthdrs.ip6e_hbh) 326 optlen += exthdrs.ip6e_hbh->m_len; 327 if (exthdrs.ip6e_dest1) 328 optlen += exthdrs.ip6e_dest1->m_len; 329 if (exthdrs.ip6e_rthdr) 330 optlen += exthdrs.ip6e_rthdr->m_len; 331 unfragpartlen = optlen + sizeof(struct ip6_hdr); 332 333 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */ 334 if (exthdrs.ip6e_dest2) 335 optlen += exthdrs.ip6e_dest2->m_len; 336 337 /* 338 * If there is at least one extension header, 339 * separate IP6 header from the payload. 340 */ 341 if (optlen && !hdrsplit) { 342 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 343 m = NULL; 344 goto freehdrs; 345 } 346 m = exthdrs.ip6e_ip6; 347 hdrsplit++; 348 } 349 350 /* adjust pointer */ 351 ip6 = mtod(m, struct ip6_hdr *); 352 353 /* adjust mbuf packet header length */ 354 m->m_pkthdr.len += optlen; 355 plen = m->m_pkthdr.len - sizeof(*ip6); 356 357 /* If this is a jumbo payload, insert a jumbo payload option. */ 358 if (plen > IPV6_MAXPACKET) { 359 if (!hdrsplit) { 360 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 361 m = NULL; 362 goto freehdrs; 363 } 364 m = exthdrs.ip6e_ip6; 365 hdrsplit++; 366 } 367 /* adjust pointer */ 368 ip6 = mtod(m, struct ip6_hdr *); 369 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 370 goto freehdrs; 371 ip6->ip6_plen = 0; 372 } else 373 ip6->ip6_plen = htons(plen); 374 375 /* 376 * Concatenate headers and fill in next header fields. 377 * Here we have, on "m" 378 * IPv6 payload 379 * and we insert headers accordingly. Finally, we should be getting: 380 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 381 * 382 * during the header composing process, "m" points to IPv6 header. 383 * "mprev" points to an extension header prior to esp. 384 */ 385 u_char *nexthdrp = &ip6->ip6_nxt; 386 mprev = m; 387 388 /* 389 * we treat dest2 specially. this makes IPsec processing 390 * much easier. the goal here is to make mprev point the 391 * mbuf prior to dest2. 392 * 393 * result: IPv6 dest2 payload 394 * m and mprev will point to IPv6 header. 395 */ 396 if (exthdrs.ip6e_dest2) { 397 if (!hdrsplit) 398 panic("assumption failed: hdr not split"); 399 exthdrs.ip6e_dest2->m_next = m->m_next; 400 m->m_next = exthdrs.ip6e_dest2; 401 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 402 ip6->ip6_nxt = IPPROTO_DSTOPTS; 403 } 404 405 /* 406 * result: IPv6 hbh dest1 rthdr dest2 payload 407 * m will point to IPv6 header. mprev will point to the 408 * extension header prior to dest2 (rthdr in the above case). 409 */ 410 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 411 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 412 IPPROTO_DSTOPTS); 413 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 414 IPPROTO_ROUTING); 415 416 /* 417 * If there is a routing header, discard the packet. 418 */ 419 if (exthdrs.ip6e_rthdr) { 420 error = EINVAL; 421 goto bad; 422 } 423 424 /* Source address validation */ 425 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 426 (flags & IPV6_UNSPECSRC) == 0) { 427 error = EOPNOTSUPP; 428 IP6STAT_INC(ip6s_badscope); 429 goto bad; 430 } 431 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 432 error = EOPNOTSUPP; 433 IP6STAT_INC(ip6s_badscope); 434 goto bad; 435 } 436 437 IP6STAT_INC(ip6s_localout); 438 439 /* 440 * Route packet. 441 */ 442 if (ro == 0) { 443 ro = &ip6route; 444 bzero((caddr_t)ro, sizeof(*ro)); 445 } 446 ro_pmtu = ro; 447 if (opt && opt->ip6po_rthdr) 448 ro = &opt->ip6po_route; 449 dst = (struct sockaddr_in6 *)&ro->ro_dst; 450 #ifdef FLOWTABLE 451 if (ro->ro_rt == NULL) 452 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro); 453 #endif 454 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m); 455 again: 456 /* 457 * if specified, try to fill in the traffic class field. 458 * do not override if a non-zero value is already set. 459 * we check the diffserv field and the ecn field separately. 460 */ 461 if (opt && opt->ip6po_tclass >= 0) { 462 int mask = 0; 463 464 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0) 465 mask |= 0xfc; 466 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0) 467 mask |= 0x03; 468 if (mask != 0) 469 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20); 470 } 471 472 /* fill in or override the hop limit field, if necessary. */ 473 if (opt && opt->ip6po_hlim != -1) 474 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 475 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 476 if (im6o != NULL) 477 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 478 else 479 ip6->ip6_hlim = V_ip6_defmcasthlim; 480 } 481 482 /* adjust pointer */ 483 ip6 = mtod(m, struct ip6_hdr *); 484 485 if (ro->ro_rt && fwd_tag == NULL) { 486 rt = ro->ro_rt; 487 ifp = ro->ro_rt->rt_ifp; 488 } else { 489 if (fwd_tag == NULL) { 490 bzero(&dst_sa, sizeof(dst_sa)); 491 dst_sa.sin6_family = AF_INET6; 492 dst_sa.sin6_len = sizeof(dst_sa); 493 dst_sa.sin6_addr = ip6->ip6_dst; 494 } 495 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp, 496 &rt, fibnum); 497 if (error != 0) { 498 if (ifp != NULL) 499 in6_ifstat_inc(ifp, ifs6_out_discard); 500 goto bad; 501 } 502 } 503 if (rt == NULL) { 504 /* 505 * If in6_selectroute() does not return a route entry, 506 * dst may not have been updated. 507 */ 508 *dst = dst_sa; /* XXX */ 509 } 510 511 /* 512 * then rt (for unicast) and ifp must be non-NULL valid values. 513 */ 514 if ((flags & IPV6_FORWARDING) == 0) { 515 /* XXX: the FORWARDING flag can be set for mrouting. */ 516 in6_ifstat_inc(ifp, ifs6_out_request); 517 } 518 if (rt != NULL) { 519 ia = (struct in6_ifaddr *)(rt->rt_ifa); 520 counter_u64_add(rt->rt_pksent, 1); 521 } 522 523 524 /* 525 * The outgoing interface must be in the zone of source and 526 * destination addresses. 527 */ 528 origifp = ifp; 529 530 src0 = ip6->ip6_src; 531 if (in6_setscope(&src0, origifp, &zone)) 532 goto badscope; 533 bzero(&src_sa, sizeof(src_sa)); 534 src_sa.sin6_family = AF_INET6; 535 src_sa.sin6_len = sizeof(src_sa); 536 src_sa.sin6_addr = ip6->ip6_src; 537 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id) 538 goto badscope; 539 540 dst0 = ip6->ip6_dst; 541 if (in6_setscope(&dst0, origifp, &zone)) 542 goto badscope; 543 /* re-initialize to be sure */ 544 bzero(&dst_sa, sizeof(dst_sa)); 545 dst_sa.sin6_family = AF_INET6; 546 dst_sa.sin6_len = sizeof(dst_sa); 547 dst_sa.sin6_addr = ip6->ip6_dst; 548 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) { 549 goto badscope; 550 } 551 552 /* We should use ia_ifp to support the case of 553 * sending packets to an address of our own. 554 */ 555 if (ia != NULL && ia->ia_ifp) 556 ifp = ia->ia_ifp; 557 558 /* scope check is done. */ 559 goto routefound; 560 561 badscope: 562 IP6STAT_INC(ip6s_badscope); 563 in6_ifstat_inc(origifp, ifs6_out_discard); 564 if (error == 0) 565 error = EHOSTUNREACH; /* XXX */ 566 goto bad; 567 568 routefound: 569 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 570 if (opt && opt->ip6po_nextroute.ro_rt) { 571 /* 572 * The nexthop is explicitly specified by the 573 * application. We assume the next hop is an IPv6 574 * address. 575 */ 576 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop; 577 } 578 else if ((rt->rt_flags & RTF_GATEWAY)) 579 dst = (struct sockaddr_in6 *)rt->rt_gateway; 580 } 581 582 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 583 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 584 } else { 585 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 586 in6_ifstat_inc(ifp, ifs6_out_mcast); 587 /* 588 * Confirm that the outgoing interface supports multicast. 589 */ 590 if (!(ifp->if_flags & IFF_MULTICAST)) { 591 IP6STAT_INC(ip6s_noroute); 592 in6_ifstat_inc(ifp, ifs6_out_discard); 593 error = ENETUNREACH; 594 goto bad; 595 } 596 if ((im6o == NULL && in6_mcast_loop) || 597 (im6o && im6o->im6o_multicast_loop)) { 598 /* 599 * Loop back multicast datagram if not expressly 600 * forbidden to do so, even if we have not joined 601 * the address; protocols will filter it later, 602 * thus deferring a hash lookup and lock acquisition 603 * at the expense of an m_copym(). 604 */ 605 ip6_mloopback(ifp, m, dst); 606 } else { 607 /* 608 * If we are acting as a multicast router, perform 609 * multicast forwarding as if the packet had just 610 * arrived on the interface to which we are about 611 * to send. The multicast forwarding function 612 * recursively calls this function, using the 613 * IPV6_FORWARDING flag to prevent infinite recursion. 614 * 615 * Multicasts that are looped back by ip6_mloopback(), 616 * above, will be forwarded by the ip6_input() routine, 617 * if necessary. 618 */ 619 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 620 /* 621 * XXX: ip6_mforward expects that rcvif is NULL 622 * when it is called from the originating path. 623 * However, it may not always be the case. 624 */ 625 m->m_pkthdr.rcvif = NULL; 626 if (ip6_mforward(ip6, ifp, m) != 0) { 627 m_freem(m); 628 goto done; 629 } 630 } 631 } 632 /* 633 * Multicasts with a hoplimit of zero may be looped back, 634 * above, but must not be transmitted on a network. 635 * Also, multicasts addressed to the loopback interface 636 * are not sent -- the above call to ip6_mloopback() will 637 * loop back a copy if this host actually belongs to the 638 * destination group on the loopback interface. 639 */ 640 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) || 641 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) { 642 m_freem(m); 643 goto done; 644 } 645 } 646 647 /* 648 * Fill the outgoing inteface to tell the upper layer 649 * to increment per-interface statistics. 650 */ 651 if (ifpp) 652 *ifpp = ifp; 653 654 /* Determine path MTU. */ 655 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu, 656 &alwaysfrag, fibnum)) != 0) 657 goto bad; 658 659 /* 660 * The caller of this function may specify to use the minimum MTU 661 * in some cases. 662 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU 663 * setting. The logic is a bit complicated; by default, unicast 664 * packets will follow path MTU while multicast packets will be sent at 665 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets 666 * including unicast ones will be sent at the minimum MTU. Multicast 667 * packets will always be sent at the minimum MTU unless 668 * IP6PO_MINMTU_DISABLE is explicitly specified. 669 * See RFC 3542 for more details. 670 */ 671 if (mtu > IPV6_MMTU) { 672 if ((flags & IPV6_MINMTU)) 673 mtu = IPV6_MMTU; 674 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) 675 mtu = IPV6_MMTU; 676 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 677 (opt == NULL || 678 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) { 679 mtu = IPV6_MMTU; 680 } 681 } 682 683 /* 684 * clear embedded scope identifiers if necessary. 685 * in6_clearscope will touch the addresses only when necessary. 686 */ 687 in6_clearscope(&ip6->ip6_src); 688 in6_clearscope(&ip6->ip6_dst); 689 690 /* 691 * If the outgoing packet contains a hop-by-hop options header, 692 * it must be examined and processed even by the source node. 693 * (RFC 2460, section 4.) 694 */ 695 if (exthdrs.ip6e_hbh) { 696 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 697 u_int32_t dummy; /* XXX unused */ 698 u_int32_t plen = 0; /* XXX: ip6_process will check the value */ 699 700 #ifdef DIAGNOSTIC 701 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) 702 panic("ip6e_hbh is not contiguous"); 703 #endif 704 /* 705 * XXX: if we have to send an ICMPv6 error to the sender, 706 * we need the M_LOOP flag since icmp6_error() expects 707 * the IPv6 and the hop-by-hop options header are 708 * contiguous unless the flag is set. 709 */ 710 m->m_flags |= M_LOOP; 711 m->m_pkthdr.rcvif = ifp; 712 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1), 713 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh), 714 &dummy, &plen) < 0) { 715 /* m was already freed at this point */ 716 error = EINVAL;/* better error? */ 717 goto done; 718 } 719 m->m_flags &= ~M_LOOP; /* XXX */ 720 m->m_pkthdr.rcvif = NULL; 721 } 722 723 /* Jump over all PFIL processing if hooks are not active. */ 724 if (!PFIL_HOOKED(&V_inet6_pfil_hook)) 725 goto passout; 726 727 odst = ip6->ip6_dst; 728 /* Run through list of hooks for output packets. */ 729 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp); 730 if (error != 0 || m == NULL) 731 goto done; 732 ip6 = mtod(m, struct ip6_hdr *); 733 734 needfiblookup = 0; 735 /* See if destination IP address was changed by packet filter. */ 736 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) { 737 m->m_flags |= M_SKIP_FIREWALL; 738 /* If destination is now ourself drop to ip6_input(). */ 739 if (in6_localip(&ip6->ip6_dst)) { 740 m->m_flags |= M_FASTFWD_OURS; 741 if (m->m_pkthdr.rcvif == NULL) 742 m->m_pkthdr.rcvif = V_loif; 743 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 744 m->m_pkthdr.csum_flags |= 745 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 746 m->m_pkthdr.csum_data = 0xffff; 747 } 748 #ifdef SCTP 749 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 750 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 751 #endif 752 error = netisr_queue(NETISR_IPV6, m); 753 goto done; 754 } else 755 needfiblookup = 1; /* Redo the routing table lookup. */ 756 } 757 /* See if fib was changed by packet filter. */ 758 if (fibnum != M_GETFIB(m)) { 759 m->m_flags |= M_SKIP_FIREWALL; 760 fibnum = M_GETFIB(m); 761 RO_RTFREE(ro); 762 needfiblookup = 1; 763 } 764 if (needfiblookup) 765 goto again; 766 767 /* See if local, if yes, send it to netisr. */ 768 if (m->m_flags & M_FASTFWD_OURS) { 769 if (m->m_pkthdr.rcvif == NULL) 770 m->m_pkthdr.rcvif = V_loif; 771 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 772 m->m_pkthdr.csum_flags |= 773 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 774 m->m_pkthdr.csum_data = 0xffff; 775 } 776 #ifdef SCTP 777 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 778 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 779 #endif 780 error = netisr_queue(NETISR_IPV6, m); 781 goto done; 782 } 783 /* Or forward to some other address? */ 784 if ((m->m_flags & M_IP6_NEXTHOP) && 785 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 786 dst = (struct sockaddr_in6 *)&ro->ro_dst; 787 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6)); 788 m->m_flags |= M_SKIP_FIREWALL; 789 m->m_flags &= ~M_IP6_NEXTHOP; 790 m_tag_delete(m, fwd_tag); 791 goto again; 792 } 793 794 passout: 795 /* 796 * Send the packet to the outgoing interface. 797 * If necessary, do IPv6 fragmentation before sending. 798 * 799 * the logic here is rather complex: 800 * 1: normal case (dontfrag == 0, alwaysfrag == 0) 801 * 1-a: send as is if tlen <= path mtu 802 * 1-b: fragment if tlen > path mtu 803 * 804 * 2: if user asks us not to fragment (dontfrag == 1) 805 * 2-a: send as is if tlen <= interface mtu 806 * 2-b: error if tlen > interface mtu 807 * 808 * 3: if we always need to attach fragment header (alwaysfrag == 1) 809 * always fragment 810 * 811 * 4: if dontfrag == 1 && alwaysfrag == 1 812 * error, as we cannot handle this conflicting request 813 */ 814 sw_csum = m->m_pkthdr.csum_flags; 815 if (!hdrsplit) { 816 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0; 817 sw_csum &= ~ifp->if_hwassist; 818 } else 819 tso = 0; 820 /* 821 * If we added extension headers, we will not do TSO and calculate the 822 * checksums ourselves for now. 823 * XXX-BZ Need a framework to know when the NIC can handle it, even 824 * with ext. hdrs. 825 */ 826 if (sw_csum & CSUM_DELAY_DATA_IPV6) { 827 sw_csum &= ~CSUM_DELAY_DATA_IPV6; 828 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr)); 829 } 830 #ifdef SCTP 831 if (sw_csum & CSUM_SCTP_IPV6) { 832 sw_csum &= ~CSUM_SCTP_IPV6; 833 sctp_delayed_cksum(m, sizeof(struct ip6_hdr)); 834 } 835 #endif 836 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 837 tlen = m->m_pkthdr.len; 838 839 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso) 840 dontfrag = 1; 841 else 842 dontfrag = 0; 843 if (dontfrag && alwaysfrag) { /* case 4 */ 844 /* conflicting request - can't transmit */ 845 error = EMSGSIZE; 846 goto bad; 847 } 848 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */ 849 /* 850 * Even if the DONTFRAG option is specified, we cannot send the 851 * packet when the data length is larger than the MTU of the 852 * outgoing interface. 853 * Notify the error by sending IPV6_PATHMTU ancillary data as 854 * well as returning an error code (the latter is not described 855 * in the API spec.) 856 */ 857 u_int32_t mtu32; 858 struct ip6ctlparam ip6cp; 859 860 mtu32 = (u_int32_t)mtu; 861 bzero(&ip6cp, sizeof(ip6cp)); 862 ip6cp.ip6c_cmdarg = (void *)&mtu32; 863 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst, 864 (void *)&ip6cp); 865 866 error = EMSGSIZE; 867 goto bad; 868 } 869 870 /* 871 * transmit packet without fragmentation 872 */ 873 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */ 874 struct in6_ifaddr *ia6; 875 876 ip6 = mtod(m, struct ip6_hdr *); 877 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); 878 if (ia6) { 879 /* Record statistics for this interface address. */ 880 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1); 881 counter_u64_add(ia6->ia_ifa.ifa_obytes, 882 m->m_pkthdr.len); 883 ifa_free(&ia6->ia_ifa); 884 } 885 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 886 goto done; 887 } 888 889 /* 890 * try to fragment the packet. case 1-b and 3 891 */ 892 if (mtu < IPV6_MMTU) { 893 /* path MTU cannot be less than IPV6_MMTU */ 894 error = EMSGSIZE; 895 in6_ifstat_inc(ifp, ifs6_out_fragfail); 896 goto bad; 897 } else if (ip6->ip6_plen == 0) { 898 /* jumbo payload cannot be fragmented */ 899 error = EMSGSIZE; 900 in6_ifstat_inc(ifp, ifs6_out_fragfail); 901 goto bad; 902 } else { 903 struct mbuf **mnext, *m_frgpart; 904 struct ip6_frag *ip6f; 905 u_int32_t id = htonl(ip6_randomid()); 906 u_char nextproto; 907 908 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len; 909 910 /* 911 * Too large for the destination or interface; 912 * fragment if possible. 913 * Must be able to put at least 8 bytes per fragment. 914 */ 915 hlen = unfragpartlen; 916 if (mtu > IPV6_MAXPACKET) 917 mtu = IPV6_MAXPACKET; 918 919 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 920 if (len < 8) { 921 error = EMSGSIZE; 922 in6_ifstat_inc(ifp, ifs6_out_fragfail); 923 goto bad; 924 } 925 926 /* 927 * Verify that we have any chance at all of being able to queue 928 * the packet or packet fragments 929 */ 930 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen) 931 < tlen /* - hlen */)) { 932 error = ENOBUFS; 933 IP6STAT_INC(ip6s_odropped); 934 goto bad; 935 } 936 937 938 /* 939 * If the interface will not calculate checksums on 940 * fragmented packets, then do it here. 941 * XXX-BZ handle the hw offloading case. Need flags. 942 */ 943 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 944 in6_delayed_cksum(m, plen, hlen); 945 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 946 } 947 #ifdef SCTP 948 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) { 949 sctp_delayed_cksum(m, hlen); 950 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6; 951 } 952 #endif 953 mnext = &m->m_nextpkt; 954 955 /* 956 * Change the next header field of the last header in the 957 * unfragmentable part. 958 */ 959 if (exthdrs.ip6e_rthdr) { 960 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 961 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 962 } else if (exthdrs.ip6e_dest1) { 963 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 964 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 965 } else if (exthdrs.ip6e_hbh) { 966 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 967 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 968 } else { 969 nextproto = ip6->ip6_nxt; 970 ip6->ip6_nxt = IPPROTO_FRAGMENT; 971 } 972 973 /* 974 * Loop through length of segment after first fragment, 975 * make new header and copy data of each part and link onto 976 * chain. 977 */ 978 m0 = m; 979 for (off = hlen; off < tlen; off += len) { 980 m = m_gethdr(M_NOWAIT, MT_DATA); 981 if (!m) { 982 error = ENOBUFS; 983 IP6STAT_INC(ip6s_odropped); 984 goto sendorfree; 985 } 986 m->m_flags = m0->m_flags & M_COPYFLAGS; 987 *mnext = m; 988 mnext = &m->m_nextpkt; 989 m->m_data += max_linkhdr; 990 mhip6 = mtod(m, struct ip6_hdr *); 991 *mhip6 = *ip6; 992 m->m_len = sizeof(*mhip6); 993 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 994 if (error) { 995 IP6STAT_INC(ip6s_odropped); 996 goto sendorfree; 997 } 998 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7)); 999 if (off + len >= tlen) 1000 len = tlen - off; 1001 else 1002 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 1003 mhip6->ip6_plen = htons((u_short)(len + hlen + 1004 sizeof(*ip6f) - sizeof(struct ip6_hdr))); 1005 if ((m_frgpart = m_copy(m0, off, len)) == 0) { 1006 error = ENOBUFS; 1007 IP6STAT_INC(ip6s_odropped); 1008 goto sendorfree; 1009 } 1010 m_cat(m, m_frgpart); 1011 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 1012 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum; 1013 m->m_pkthdr.rcvif = NULL; 1014 ip6f->ip6f_reserved = 0; 1015 ip6f->ip6f_ident = id; 1016 ip6f->ip6f_nxt = nextproto; 1017 IP6STAT_INC(ip6s_ofragments); 1018 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 1019 } 1020 1021 in6_ifstat_inc(ifp, ifs6_out_fragok); 1022 } 1023 1024 /* 1025 * Remove leading garbages. 1026 */ 1027 sendorfree: 1028 m = m0->m_nextpkt; 1029 m0->m_nextpkt = 0; 1030 m_freem(m0); 1031 for (m0 = m; m; m = m0) { 1032 m0 = m->m_nextpkt; 1033 m->m_nextpkt = 0; 1034 if (error == 0) { 1035 /* Record statistics for this interface address. */ 1036 if (ia) { 1037 counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 1038 counter_u64_add(ia->ia_ifa.ifa_obytes, 1039 m->m_pkthdr.len); 1040 } 1041 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 1042 } else 1043 m_freem(m); 1044 } 1045 1046 if (error == 0) 1047 IP6STAT_INC(ip6s_fragmented); 1048 1049 done: 1050 if (ro == &ip6route) 1051 RO_RTFREE(ro); 1052 if (ro_pmtu == &ip6route) 1053 RO_RTFREE(ro_pmtu); 1054 return (error); 1055 1056 freehdrs: 1057 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1058 m_freem(exthdrs.ip6e_dest1); 1059 m_freem(exthdrs.ip6e_rthdr); 1060 m_freem(exthdrs.ip6e_dest2); 1061 /* FALLTHROUGH */ 1062 bad: 1063 if (m) 1064 m_freem(m); 1065 goto done; 1066 } 1067 1068 static int 1069 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen) 1070 { 1071 struct mbuf *m; 1072 1073 if (hlen > MCLBYTES) 1074 return (ENOBUFS); /* XXX */ 1075 1076 if (hlen > MLEN) 1077 m = m_getcl(M_NOWAIT, MT_DATA, 0); 1078 else 1079 m = m_get(M_NOWAIT, MT_DATA); 1080 if (m == NULL) 1081 return (ENOBUFS); 1082 m->m_len = hlen; 1083 if (hdr) 1084 bcopy(hdr, mtod(m, caddr_t), hlen); 1085 1086 *mp = m; 1087 return (0); 1088 } 1089 1090 /* 1091 * Insert jumbo payload option. 1092 */ 1093 static int 1094 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) 1095 { 1096 struct mbuf *mopt; 1097 u_char *optbuf; 1098 u_int32_t v; 1099 1100 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1101 1102 /* 1103 * If there is no hop-by-hop options header, allocate new one. 1104 * If there is one but it doesn't have enough space to store the 1105 * jumbo payload option, allocate a cluster to store the whole options. 1106 * Otherwise, use it to store the options. 1107 */ 1108 if (exthdrs->ip6e_hbh == 0) { 1109 mopt = m_get(M_NOWAIT, MT_DATA); 1110 if (mopt == NULL) 1111 return (ENOBUFS); 1112 mopt->m_len = JUMBOOPTLEN; 1113 optbuf = mtod(mopt, u_char *); 1114 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1115 exthdrs->ip6e_hbh = mopt; 1116 } else { 1117 struct ip6_hbh *hbh; 1118 1119 mopt = exthdrs->ip6e_hbh; 1120 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1121 /* 1122 * XXX assumption: 1123 * - exthdrs->ip6e_hbh is not referenced from places 1124 * other than exthdrs. 1125 * - exthdrs->ip6e_hbh is not an mbuf chain. 1126 */ 1127 int oldoptlen = mopt->m_len; 1128 struct mbuf *n; 1129 1130 /* 1131 * XXX: give up if the whole (new) hbh header does 1132 * not fit even in an mbuf cluster. 1133 */ 1134 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1135 return (ENOBUFS); 1136 1137 /* 1138 * As a consequence, we must always prepare a cluster 1139 * at this point. 1140 */ 1141 n = m_getcl(M_NOWAIT, MT_DATA, 0); 1142 if (n == NULL) 1143 return (ENOBUFS); 1144 n->m_len = oldoptlen + JUMBOOPTLEN; 1145 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1146 oldoptlen); 1147 optbuf = mtod(n, caddr_t) + oldoptlen; 1148 m_freem(mopt); 1149 mopt = exthdrs->ip6e_hbh = n; 1150 } else { 1151 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1152 mopt->m_len += JUMBOOPTLEN; 1153 } 1154 optbuf[0] = IP6OPT_PADN; 1155 optbuf[1] = 1; 1156 1157 /* 1158 * Adjust the header length according to the pad and 1159 * the jumbo payload option. 1160 */ 1161 hbh = mtod(mopt, struct ip6_hbh *); 1162 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1163 } 1164 1165 /* fill in the option. */ 1166 optbuf[2] = IP6OPT_JUMBO; 1167 optbuf[3] = 4; 1168 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1169 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1170 1171 /* finally, adjust the packet header length */ 1172 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1173 1174 return (0); 1175 #undef JUMBOOPTLEN 1176 } 1177 1178 /* 1179 * Insert fragment header and copy unfragmentable header portions. 1180 */ 1181 static int 1182 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, 1183 struct ip6_frag **frghdrp) 1184 { 1185 struct mbuf *n, *mlast; 1186 1187 if (hlen > sizeof(struct ip6_hdr)) { 1188 n = m_copym(m0, sizeof(struct ip6_hdr), 1189 hlen - sizeof(struct ip6_hdr), M_NOWAIT); 1190 if (n == 0) 1191 return (ENOBUFS); 1192 m->m_next = n; 1193 } else 1194 n = m; 1195 1196 /* Search for the last mbuf of unfragmentable part. */ 1197 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1198 ; 1199 1200 if (M_WRITABLE(mlast) && 1201 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1202 /* use the trailing space of the last mbuf for the fragment hdr */ 1203 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) + 1204 mlast->m_len); 1205 mlast->m_len += sizeof(struct ip6_frag); 1206 m->m_pkthdr.len += sizeof(struct ip6_frag); 1207 } else { 1208 /* allocate a new mbuf for the fragment header */ 1209 struct mbuf *mfrg; 1210 1211 mfrg = m_get(M_NOWAIT, MT_DATA); 1212 if (mfrg == NULL) 1213 return (ENOBUFS); 1214 mfrg->m_len = sizeof(struct ip6_frag); 1215 *frghdrp = mtod(mfrg, struct ip6_frag *); 1216 mlast->m_next = mfrg; 1217 } 1218 1219 return (0); 1220 } 1221 1222 static int 1223 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro, 1224 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup, 1225 int *alwaysfragp, u_int fibnum) 1226 { 1227 u_int32_t mtu = 0; 1228 int alwaysfrag = 0; 1229 int error = 0; 1230 1231 if (ro_pmtu != ro) { 1232 /* The first hop and the final destination may differ. */ 1233 struct sockaddr_in6 *sa6_dst = 1234 (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 1235 if (ro_pmtu->ro_rt && 1236 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 || 1237 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) { 1238 RTFREE(ro_pmtu->ro_rt); 1239 ro_pmtu->ro_rt = (struct rtentry *)NULL; 1240 } 1241 if (ro_pmtu->ro_rt == NULL) { 1242 bzero(sa6_dst, sizeof(*sa6_dst)); 1243 sa6_dst->sin6_family = AF_INET6; 1244 sa6_dst->sin6_len = sizeof(struct sockaddr_in6); 1245 sa6_dst->sin6_addr = *dst; 1246 1247 in6_rtalloc(ro_pmtu, fibnum); 1248 } 1249 } 1250 if (ro_pmtu->ro_rt) { 1251 u_int32_t ifmtu; 1252 struct in_conninfo inc; 1253 1254 bzero(&inc, sizeof(inc)); 1255 inc.inc_flags |= INC_ISIPV6; 1256 inc.inc6_faddr = *dst; 1257 1258 if (ifp == NULL) 1259 ifp = ro_pmtu->ro_rt->rt_ifp; 1260 ifmtu = IN6_LINKMTU(ifp); 1261 mtu = tcp_hc_getmtu(&inc); 1262 if (mtu) 1263 mtu = min(mtu, ro_pmtu->ro_rt->rt_mtu); 1264 else 1265 mtu = ro_pmtu->ro_rt->rt_mtu; 1266 if (mtu == 0) 1267 mtu = ifmtu; 1268 else if (mtu < IPV6_MMTU) { 1269 /* 1270 * RFC2460 section 5, last paragraph: 1271 * if we record ICMPv6 too big message with 1272 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU 1273 * or smaller, with framgent header attached. 1274 * (fragment header is needed regardless from the 1275 * packet size, for translators to identify packets) 1276 */ 1277 alwaysfrag = 1; 1278 mtu = IPV6_MMTU; 1279 } 1280 } else if (ifp) { 1281 mtu = IN6_LINKMTU(ifp); 1282 } else 1283 error = EHOSTUNREACH; /* XXX */ 1284 1285 *mtup = mtu; 1286 if (alwaysfragp) 1287 *alwaysfragp = alwaysfrag; 1288 return (error); 1289 } 1290 1291 /* 1292 * IP6 socket option processing. 1293 */ 1294 int 1295 ip6_ctloutput(struct socket *so, struct sockopt *sopt) 1296 { 1297 int optdatalen, uproto; 1298 void *optdata; 1299 struct inpcb *in6p = sotoinpcb(so); 1300 int error, optval; 1301 int level, op, optname; 1302 int optlen; 1303 struct thread *td; 1304 #ifdef RSS 1305 uint32_t rss_bucket; 1306 int retval; 1307 #endif 1308 1309 level = sopt->sopt_level; 1310 op = sopt->sopt_dir; 1311 optname = sopt->sopt_name; 1312 optlen = sopt->sopt_valsize; 1313 td = sopt->sopt_td; 1314 error = 0; 1315 optval = 0; 1316 uproto = (int)so->so_proto->pr_protocol; 1317 1318 if (level != IPPROTO_IPV6) { 1319 error = EINVAL; 1320 1321 if (sopt->sopt_level == SOL_SOCKET && 1322 sopt->sopt_dir == SOPT_SET) { 1323 switch (sopt->sopt_name) { 1324 case SO_REUSEADDR: 1325 INP_WLOCK(in6p); 1326 if ((so->so_options & SO_REUSEADDR) != 0) 1327 in6p->inp_flags2 |= INP_REUSEADDR; 1328 else 1329 in6p->inp_flags2 &= ~INP_REUSEADDR; 1330 INP_WUNLOCK(in6p); 1331 error = 0; 1332 break; 1333 case SO_REUSEPORT: 1334 INP_WLOCK(in6p); 1335 if ((so->so_options & SO_REUSEPORT) != 0) 1336 in6p->inp_flags2 |= INP_REUSEPORT; 1337 else 1338 in6p->inp_flags2 &= ~INP_REUSEPORT; 1339 INP_WUNLOCK(in6p); 1340 error = 0; 1341 break; 1342 case SO_SETFIB: 1343 INP_WLOCK(in6p); 1344 in6p->inp_inc.inc_fibnum = so->so_fibnum; 1345 INP_WUNLOCK(in6p); 1346 error = 0; 1347 break; 1348 default: 1349 break; 1350 } 1351 } 1352 } else { /* level == IPPROTO_IPV6 */ 1353 switch (op) { 1354 1355 case SOPT_SET: 1356 switch (optname) { 1357 case IPV6_2292PKTOPTIONS: 1358 #ifdef IPV6_PKTOPTIONS 1359 case IPV6_PKTOPTIONS: 1360 #endif 1361 { 1362 struct mbuf *m; 1363 1364 error = soopt_getm(sopt, &m); /* XXX */ 1365 if (error != 0) 1366 break; 1367 error = soopt_mcopyin(sopt, m); /* XXX */ 1368 if (error != 0) 1369 break; 1370 error = ip6_pcbopts(&in6p->in6p_outputopts, 1371 m, so, sopt); 1372 m_freem(m); /* XXX */ 1373 break; 1374 } 1375 1376 /* 1377 * Use of some Hop-by-Hop options or some 1378 * Destination options, might require special 1379 * privilege. That is, normal applications 1380 * (without special privilege) might be forbidden 1381 * from setting certain options in outgoing packets, 1382 * and might never see certain options in received 1383 * packets. [RFC 2292 Section 6] 1384 * KAME specific note: 1385 * KAME prevents non-privileged users from sending or 1386 * receiving ANY hbh/dst options in order to avoid 1387 * overhead of parsing options in the kernel. 1388 */ 1389 case IPV6_RECVHOPOPTS: 1390 case IPV6_RECVDSTOPTS: 1391 case IPV6_RECVRTHDRDSTOPTS: 1392 if (td != NULL) { 1393 error = priv_check(td, 1394 PRIV_NETINET_SETHDROPTS); 1395 if (error) 1396 break; 1397 } 1398 /* FALLTHROUGH */ 1399 case IPV6_UNICAST_HOPS: 1400 case IPV6_HOPLIMIT: 1401 1402 case IPV6_RECVPKTINFO: 1403 case IPV6_RECVHOPLIMIT: 1404 case IPV6_RECVRTHDR: 1405 case IPV6_RECVPATHMTU: 1406 case IPV6_RECVTCLASS: 1407 case IPV6_V6ONLY: 1408 case IPV6_AUTOFLOWLABEL: 1409 case IPV6_BINDANY: 1410 case IPV6_BINDMULTI: 1411 #ifdef RSS 1412 case IPV6_RSS_LISTEN_BUCKET: 1413 #endif 1414 if (optname == IPV6_BINDANY && td != NULL) { 1415 error = priv_check(td, 1416 PRIV_NETINET_BINDANY); 1417 if (error) 1418 break; 1419 } 1420 1421 if (optlen != sizeof(int)) { 1422 error = EINVAL; 1423 break; 1424 } 1425 error = sooptcopyin(sopt, &optval, 1426 sizeof optval, sizeof optval); 1427 if (error) 1428 break; 1429 switch (optname) { 1430 1431 case IPV6_UNICAST_HOPS: 1432 if (optval < -1 || optval >= 256) 1433 error = EINVAL; 1434 else { 1435 /* -1 = kernel default */ 1436 in6p->in6p_hops = optval; 1437 if ((in6p->inp_vflag & 1438 INP_IPV4) != 0) 1439 in6p->inp_ip_ttl = optval; 1440 } 1441 break; 1442 #define OPTSET(bit) \ 1443 do { \ 1444 INP_WLOCK(in6p); \ 1445 if (optval) \ 1446 in6p->inp_flags |= (bit); \ 1447 else \ 1448 in6p->inp_flags &= ~(bit); \ 1449 INP_WUNLOCK(in6p); \ 1450 } while (/*CONSTCOND*/ 0) 1451 #define OPTSET2292(bit) \ 1452 do { \ 1453 INP_WLOCK(in6p); \ 1454 in6p->inp_flags |= IN6P_RFC2292; \ 1455 if (optval) \ 1456 in6p->inp_flags |= (bit); \ 1457 else \ 1458 in6p->inp_flags &= ~(bit); \ 1459 INP_WUNLOCK(in6p); \ 1460 } while (/*CONSTCOND*/ 0) 1461 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0) 1462 1463 #define OPTSET2(bit, val) do { \ 1464 INP_WLOCK(in6p); \ 1465 if (val) \ 1466 in6p->inp_flags2 |= bit; \ 1467 else \ 1468 in6p->inp_flags2 &= ~bit; \ 1469 INP_WUNLOCK(in6p); \ 1470 } while (0) 1471 #define OPTBIT2(bit) (in6p->inp_flags2 & (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_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->inp_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->inp_vflag &= ~INP_IPV4; 1569 else 1570 in6p->inp_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 case IPV6_BINDANY: 1585 OPTSET(INP_BINDANY); 1586 break; 1587 1588 case IPV6_BINDMULTI: 1589 OPTSET2(INP_BINDMULTI, optval); 1590 break; 1591 #ifdef RSS 1592 case IPV6_RSS_LISTEN_BUCKET: 1593 if ((optval >= 0) && 1594 (optval < rss_getnumbuckets())) { 1595 in6p->inp_rss_listen_bucket = optval; 1596 OPTSET2(INP_RSS_BUCKET_SET, 1); 1597 } else { 1598 error = EINVAL; 1599 } 1600 break; 1601 #endif 1602 } 1603 break; 1604 1605 case IPV6_TCLASS: 1606 case IPV6_DONTFRAG: 1607 case IPV6_USE_MIN_MTU: 1608 case IPV6_PREFER_TEMPADDR: 1609 if (optlen != sizeof(optval)) { 1610 error = EINVAL; 1611 break; 1612 } 1613 error = sooptcopyin(sopt, &optval, 1614 sizeof optval, sizeof optval); 1615 if (error) 1616 break; 1617 { 1618 struct ip6_pktopts **optp; 1619 optp = &in6p->in6p_outputopts; 1620 error = ip6_pcbopt(optname, 1621 (u_char *)&optval, sizeof(optval), 1622 optp, (td != NULL) ? td->td_ucred : 1623 NULL, uproto); 1624 break; 1625 } 1626 1627 case IPV6_2292PKTINFO: 1628 case IPV6_2292HOPLIMIT: 1629 case IPV6_2292HOPOPTS: 1630 case IPV6_2292DSTOPTS: 1631 case IPV6_2292RTHDR: 1632 /* RFC 2292 */ 1633 if (optlen != sizeof(int)) { 1634 error = EINVAL; 1635 break; 1636 } 1637 error = sooptcopyin(sopt, &optval, 1638 sizeof optval, sizeof optval); 1639 if (error) 1640 break; 1641 switch (optname) { 1642 case IPV6_2292PKTINFO: 1643 OPTSET2292(IN6P_PKTINFO); 1644 break; 1645 case IPV6_2292HOPLIMIT: 1646 OPTSET2292(IN6P_HOPLIMIT); 1647 break; 1648 case IPV6_2292HOPOPTS: 1649 /* 1650 * Check super-user privilege. 1651 * See comments for IPV6_RECVHOPOPTS. 1652 */ 1653 if (td != NULL) { 1654 error = priv_check(td, 1655 PRIV_NETINET_SETHDROPTS); 1656 if (error) 1657 return (error); 1658 } 1659 OPTSET2292(IN6P_HOPOPTS); 1660 break; 1661 case IPV6_2292DSTOPTS: 1662 if (td != NULL) { 1663 error = priv_check(td, 1664 PRIV_NETINET_SETHDROPTS); 1665 if (error) 1666 return (error); 1667 } 1668 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1669 break; 1670 case IPV6_2292RTHDR: 1671 OPTSET2292(IN6P_RTHDR); 1672 break; 1673 } 1674 break; 1675 case IPV6_PKTINFO: 1676 case IPV6_HOPOPTS: 1677 case IPV6_RTHDR: 1678 case IPV6_DSTOPTS: 1679 case IPV6_RTHDRDSTOPTS: 1680 case IPV6_NEXTHOP: 1681 { 1682 /* new advanced API (RFC3542) */ 1683 u_char *optbuf; 1684 u_char optbuf_storage[MCLBYTES]; 1685 int optlen; 1686 struct ip6_pktopts **optp; 1687 1688 /* cannot mix with RFC2292 */ 1689 if (OPTBIT(IN6P_RFC2292)) { 1690 error = EINVAL; 1691 break; 1692 } 1693 1694 /* 1695 * We only ensure valsize is not too large 1696 * here. Further validation will be done 1697 * later. 1698 */ 1699 error = sooptcopyin(sopt, optbuf_storage, 1700 sizeof(optbuf_storage), 0); 1701 if (error) 1702 break; 1703 optlen = sopt->sopt_valsize; 1704 optbuf = optbuf_storage; 1705 optp = &in6p->in6p_outputopts; 1706 error = ip6_pcbopt(optname, optbuf, optlen, 1707 optp, (td != NULL) ? td->td_ucred : NULL, 1708 uproto); 1709 break; 1710 } 1711 #undef OPTSET 1712 1713 case IPV6_MULTICAST_IF: 1714 case IPV6_MULTICAST_HOPS: 1715 case IPV6_MULTICAST_LOOP: 1716 case IPV6_JOIN_GROUP: 1717 case IPV6_LEAVE_GROUP: 1718 case IPV6_MSFILTER: 1719 case MCAST_BLOCK_SOURCE: 1720 case MCAST_UNBLOCK_SOURCE: 1721 case MCAST_JOIN_GROUP: 1722 case MCAST_LEAVE_GROUP: 1723 case MCAST_JOIN_SOURCE_GROUP: 1724 case MCAST_LEAVE_SOURCE_GROUP: 1725 error = ip6_setmoptions(in6p, sopt); 1726 break; 1727 1728 case IPV6_PORTRANGE: 1729 error = sooptcopyin(sopt, &optval, 1730 sizeof optval, sizeof optval); 1731 if (error) 1732 break; 1733 1734 INP_WLOCK(in6p); 1735 switch (optval) { 1736 case IPV6_PORTRANGE_DEFAULT: 1737 in6p->inp_flags &= ~(INP_LOWPORT); 1738 in6p->inp_flags &= ~(INP_HIGHPORT); 1739 break; 1740 1741 case IPV6_PORTRANGE_HIGH: 1742 in6p->inp_flags &= ~(INP_LOWPORT); 1743 in6p->inp_flags |= INP_HIGHPORT; 1744 break; 1745 1746 case IPV6_PORTRANGE_LOW: 1747 in6p->inp_flags &= ~(INP_HIGHPORT); 1748 in6p->inp_flags |= INP_LOWPORT; 1749 break; 1750 1751 default: 1752 error = EINVAL; 1753 break; 1754 } 1755 INP_WUNLOCK(in6p); 1756 break; 1757 1758 #ifdef IPSEC 1759 case IPV6_IPSEC_POLICY: 1760 { 1761 caddr_t req; 1762 struct mbuf *m; 1763 1764 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1765 break; 1766 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1767 break; 1768 req = mtod(m, caddr_t); 1769 error = ipsec_set_policy(in6p, optname, req, 1770 m->m_len, (sopt->sopt_td != NULL) ? 1771 sopt->sopt_td->td_ucred : NULL); 1772 m_freem(m); 1773 break; 1774 } 1775 #endif /* IPSEC */ 1776 1777 default: 1778 error = ENOPROTOOPT; 1779 break; 1780 } 1781 break; 1782 1783 case SOPT_GET: 1784 switch (optname) { 1785 1786 case IPV6_2292PKTOPTIONS: 1787 #ifdef IPV6_PKTOPTIONS 1788 case IPV6_PKTOPTIONS: 1789 #endif 1790 /* 1791 * RFC3542 (effectively) deprecated the 1792 * semantics of the 2292-style pktoptions. 1793 * Since it was not reliable in nature (i.e., 1794 * applications had to expect the lack of some 1795 * information after all), it would make sense 1796 * to simplify this part by always returning 1797 * empty data. 1798 */ 1799 sopt->sopt_valsize = 0; 1800 break; 1801 1802 case IPV6_RECVHOPOPTS: 1803 case IPV6_RECVDSTOPTS: 1804 case IPV6_RECVRTHDRDSTOPTS: 1805 case IPV6_UNICAST_HOPS: 1806 case IPV6_RECVPKTINFO: 1807 case IPV6_RECVHOPLIMIT: 1808 case IPV6_RECVRTHDR: 1809 case IPV6_RECVPATHMTU: 1810 1811 case IPV6_V6ONLY: 1812 case IPV6_PORTRANGE: 1813 case IPV6_RECVTCLASS: 1814 case IPV6_AUTOFLOWLABEL: 1815 case IPV6_BINDANY: 1816 case IPV6_FLOWID: 1817 case IPV6_FLOWTYPE: 1818 #ifdef RSS 1819 case IPV6_RSSBUCKETID: 1820 #endif 1821 switch (optname) { 1822 1823 case IPV6_RECVHOPOPTS: 1824 optval = OPTBIT(IN6P_HOPOPTS); 1825 break; 1826 1827 case IPV6_RECVDSTOPTS: 1828 optval = OPTBIT(IN6P_DSTOPTS); 1829 break; 1830 1831 case IPV6_RECVRTHDRDSTOPTS: 1832 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 1833 break; 1834 1835 case IPV6_UNICAST_HOPS: 1836 optval = in6p->in6p_hops; 1837 break; 1838 1839 case IPV6_RECVPKTINFO: 1840 optval = OPTBIT(IN6P_PKTINFO); 1841 break; 1842 1843 case IPV6_RECVHOPLIMIT: 1844 optval = OPTBIT(IN6P_HOPLIMIT); 1845 break; 1846 1847 case IPV6_RECVRTHDR: 1848 optval = OPTBIT(IN6P_RTHDR); 1849 break; 1850 1851 case IPV6_RECVPATHMTU: 1852 optval = OPTBIT(IN6P_MTU); 1853 break; 1854 1855 case IPV6_V6ONLY: 1856 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1857 break; 1858 1859 case IPV6_PORTRANGE: 1860 { 1861 int flags; 1862 flags = in6p->inp_flags; 1863 if (flags & INP_HIGHPORT) 1864 optval = IPV6_PORTRANGE_HIGH; 1865 else if (flags & INP_LOWPORT) 1866 optval = IPV6_PORTRANGE_LOW; 1867 else 1868 optval = 0; 1869 break; 1870 } 1871 case IPV6_RECVTCLASS: 1872 optval = OPTBIT(IN6P_TCLASS); 1873 break; 1874 1875 case IPV6_AUTOFLOWLABEL: 1876 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 1877 break; 1878 1879 case IPV6_BINDANY: 1880 optval = OPTBIT(INP_BINDANY); 1881 break; 1882 1883 case IPV6_FLOWID: 1884 optval = in6p->inp_flowid; 1885 break; 1886 1887 case IPV6_FLOWTYPE: 1888 optval = in6p->inp_flowtype; 1889 break; 1890 #ifdef RSS 1891 case IPV6_RSSBUCKETID: 1892 retval = 1893 rss_hash2bucket(in6p->inp_flowid, 1894 in6p->inp_flowtype, 1895 &rss_bucket); 1896 if (retval == 0) 1897 optval = rss_bucket; 1898 else 1899 error = EINVAL; 1900 break; 1901 #endif 1902 1903 case IPV6_BINDMULTI: 1904 optval = OPTBIT2(INP_BINDMULTI); 1905 break; 1906 1907 } 1908 if (error) 1909 break; 1910 error = sooptcopyout(sopt, &optval, 1911 sizeof optval); 1912 break; 1913 1914 case IPV6_PATHMTU: 1915 { 1916 u_long pmtu = 0; 1917 struct ip6_mtuinfo mtuinfo; 1918 struct route_in6 sro; 1919 1920 bzero(&sro, sizeof(sro)); 1921 1922 if (!(so->so_state & SS_ISCONNECTED)) 1923 return (ENOTCONN); 1924 /* 1925 * XXX: we dot not consider the case of source 1926 * routing, or optional information to specify 1927 * the outgoing interface. 1928 */ 1929 error = ip6_getpmtu(&sro, NULL, NULL, 1930 &in6p->in6p_faddr, &pmtu, NULL, 1931 so->so_fibnum); 1932 if (sro.ro_rt) 1933 RTFREE(sro.ro_rt); 1934 if (error) 1935 break; 1936 if (pmtu > IPV6_MAXPACKET) 1937 pmtu = IPV6_MAXPACKET; 1938 1939 bzero(&mtuinfo, sizeof(mtuinfo)); 1940 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 1941 optdata = (void *)&mtuinfo; 1942 optdatalen = sizeof(mtuinfo); 1943 error = sooptcopyout(sopt, optdata, 1944 optdatalen); 1945 break; 1946 } 1947 1948 case IPV6_2292PKTINFO: 1949 case IPV6_2292HOPLIMIT: 1950 case IPV6_2292HOPOPTS: 1951 case IPV6_2292RTHDR: 1952 case IPV6_2292DSTOPTS: 1953 switch (optname) { 1954 case IPV6_2292PKTINFO: 1955 optval = OPTBIT(IN6P_PKTINFO); 1956 break; 1957 case IPV6_2292HOPLIMIT: 1958 optval = OPTBIT(IN6P_HOPLIMIT); 1959 break; 1960 case IPV6_2292HOPOPTS: 1961 optval = OPTBIT(IN6P_HOPOPTS); 1962 break; 1963 case IPV6_2292RTHDR: 1964 optval = OPTBIT(IN6P_RTHDR); 1965 break; 1966 case IPV6_2292DSTOPTS: 1967 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1968 break; 1969 } 1970 error = sooptcopyout(sopt, &optval, 1971 sizeof optval); 1972 break; 1973 case IPV6_PKTINFO: 1974 case IPV6_HOPOPTS: 1975 case IPV6_RTHDR: 1976 case IPV6_DSTOPTS: 1977 case IPV6_RTHDRDSTOPTS: 1978 case IPV6_NEXTHOP: 1979 case IPV6_TCLASS: 1980 case IPV6_DONTFRAG: 1981 case IPV6_USE_MIN_MTU: 1982 case IPV6_PREFER_TEMPADDR: 1983 error = ip6_getpcbopt(in6p->in6p_outputopts, 1984 optname, sopt); 1985 break; 1986 1987 case IPV6_MULTICAST_IF: 1988 case IPV6_MULTICAST_HOPS: 1989 case IPV6_MULTICAST_LOOP: 1990 case IPV6_MSFILTER: 1991 error = ip6_getmoptions(in6p, sopt); 1992 break; 1993 1994 #ifdef IPSEC 1995 case IPV6_IPSEC_POLICY: 1996 { 1997 caddr_t req = NULL; 1998 size_t len = 0; 1999 struct mbuf *m = NULL; 2000 struct mbuf **mp = &m; 2001 size_t ovalsize = sopt->sopt_valsize; 2002 caddr_t oval = (caddr_t)sopt->sopt_val; 2003 2004 error = soopt_getm(sopt, &m); /* XXX */ 2005 if (error != 0) 2006 break; 2007 error = soopt_mcopyin(sopt, m); /* XXX */ 2008 if (error != 0) 2009 break; 2010 sopt->sopt_valsize = ovalsize; 2011 sopt->sopt_val = oval; 2012 if (m) { 2013 req = mtod(m, caddr_t); 2014 len = m->m_len; 2015 } 2016 error = ipsec_get_policy(in6p, req, len, mp); 2017 if (error == 0) 2018 error = soopt_mcopyout(sopt, m); /* XXX */ 2019 if (error == 0 && m) 2020 m_freem(m); 2021 break; 2022 } 2023 #endif /* IPSEC */ 2024 2025 default: 2026 error = ENOPROTOOPT; 2027 break; 2028 } 2029 break; 2030 } 2031 } 2032 return (error); 2033 } 2034 2035 int 2036 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt) 2037 { 2038 int error = 0, optval, optlen; 2039 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 2040 struct inpcb *in6p = sotoinpcb(so); 2041 int level, op, optname; 2042 2043 level = sopt->sopt_level; 2044 op = sopt->sopt_dir; 2045 optname = sopt->sopt_name; 2046 optlen = sopt->sopt_valsize; 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 */ 2110 static int 2111 ip6_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 */ 2155 void 2156 ip6_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 2166 static int 2167 ip6_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 2182 static int 2183 ip6_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 2280 void 2281 ip6_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) \ 2331 do {\ 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 2341 static int 2342 copypktopts(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 dst->ip6po_minmtu = src->ip6po_minmtu; 2353 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr; 2354 if (src->ip6po_pktinfo) { 2355 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2356 M_IP6OPT, canwait); 2357 if (dst->ip6po_pktinfo == NULL) 2358 goto bad; 2359 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2360 } 2361 if (src->ip6po_nexthop) { 2362 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2363 M_IP6OPT, canwait); 2364 if (dst->ip6po_nexthop == NULL) 2365 goto bad; 2366 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 2367 src->ip6po_nexthop->sa_len); 2368 } 2369 PKTOPT_EXTHDRCPY(ip6po_hbh); 2370 PKTOPT_EXTHDRCPY(ip6po_dest1); 2371 PKTOPT_EXTHDRCPY(ip6po_dest2); 2372 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2373 return (0); 2374 2375 bad: 2376 ip6_clearpktopts(dst, -1); 2377 return (ENOBUFS); 2378 } 2379 #undef PKTOPT_EXTHDRCPY 2380 2381 struct ip6_pktopts * 2382 ip6_copypktopts(struct ip6_pktopts *src, int canwait) 2383 { 2384 int error; 2385 struct ip6_pktopts *dst; 2386 2387 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2388 if (dst == NULL) 2389 return (NULL); 2390 ip6_initpktopts(dst); 2391 2392 if ((error = copypktopts(dst, src, canwait)) != 0) { 2393 free(dst, M_IP6OPT); 2394 return (NULL); 2395 } 2396 2397 return (dst); 2398 } 2399 2400 void 2401 ip6_freepcbopts(struct ip6_pktopts *pktopt) 2402 { 2403 if (pktopt == NULL) 2404 return; 2405 2406 ip6_clearpktopts(pktopt, -1); 2407 2408 free(pktopt, M_IP6OPT); 2409 } 2410 2411 /* 2412 * Set IPv6 outgoing packet options based on advanced API. 2413 */ 2414 int 2415 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2416 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto) 2417 { 2418 struct cmsghdr *cm = 0; 2419 2420 if (control == NULL || opt == NULL) 2421 return (EINVAL); 2422 2423 ip6_initpktopts(opt); 2424 if (stickyopt) { 2425 int error; 2426 2427 /* 2428 * If stickyopt is provided, make a local copy of the options 2429 * for this particular packet, then override them by ancillary 2430 * objects. 2431 * XXX: copypktopts() does not copy the cached route to a next 2432 * hop (if any). This is not very good in terms of efficiency, 2433 * but we can allow this since this option should be rarely 2434 * used. 2435 */ 2436 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2437 return (error); 2438 } 2439 2440 /* 2441 * XXX: Currently, we assume all the optional information is stored 2442 * in a single mbuf. 2443 */ 2444 if (control->m_next) 2445 return (EINVAL); 2446 2447 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2448 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2449 int error; 2450 2451 if (control->m_len < CMSG_LEN(0)) 2452 return (EINVAL); 2453 2454 cm = mtod(control, struct cmsghdr *); 2455 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2456 return (EINVAL); 2457 if (cm->cmsg_level != IPPROTO_IPV6) 2458 continue; 2459 2460 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2461 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); 2462 if (error) 2463 return (error); 2464 } 2465 2466 return (0); 2467 } 2468 2469 /* 2470 * Set a particular packet option, as a sticky option or an ancillary data 2471 * item. "len" can be 0 only when it's a sticky option. 2472 * We have 4 cases of combination of "sticky" and "cmsg": 2473 * "sticky=0, cmsg=0": impossible 2474 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2475 * "sticky=1, cmsg=0": RFC3542 socket option 2476 * "sticky=1, cmsg=1": RFC2292 socket option 2477 */ 2478 static int 2479 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2480 struct ucred *cred, int sticky, int cmsg, int uproto) 2481 { 2482 int minmtupolicy, preftemp; 2483 int error; 2484 2485 if (!sticky && !cmsg) { 2486 #ifdef DIAGNOSTIC 2487 printf("ip6_setpktopt: impossible case\n"); 2488 #endif 2489 return (EINVAL); 2490 } 2491 2492 /* 2493 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2494 * not be specified in the context of RFC3542. Conversely, 2495 * RFC3542 types should not be specified in the context of RFC2292. 2496 */ 2497 if (!cmsg) { 2498 switch (optname) { 2499 case IPV6_2292PKTINFO: 2500 case IPV6_2292HOPLIMIT: 2501 case IPV6_2292NEXTHOP: 2502 case IPV6_2292HOPOPTS: 2503 case IPV6_2292DSTOPTS: 2504 case IPV6_2292RTHDR: 2505 case IPV6_2292PKTOPTIONS: 2506 return (ENOPROTOOPT); 2507 } 2508 } 2509 if (sticky && cmsg) { 2510 switch (optname) { 2511 case IPV6_PKTINFO: 2512 case IPV6_HOPLIMIT: 2513 case IPV6_NEXTHOP: 2514 case IPV6_HOPOPTS: 2515 case IPV6_DSTOPTS: 2516 case IPV6_RTHDRDSTOPTS: 2517 case IPV6_RTHDR: 2518 case IPV6_USE_MIN_MTU: 2519 case IPV6_DONTFRAG: 2520 case IPV6_TCLASS: 2521 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */ 2522 return (ENOPROTOOPT); 2523 } 2524 } 2525 2526 switch (optname) { 2527 case IPV6_2292PKTINFO: 2528 case IPV6_PKTINFO: 2529 { 2530 struct ifnet *ifp = NULL; 2531 struct in6_pktinfo *pktinfo; 2532 2533 if (len != sizeof(struct in6_pktinfo)) 2534 return (EINVAL); 2535 2536 pktinfo = (struct in6_pktinfo *)buf; 2537 2538 /* 2539 * An application can clear any sticky IPV6_PKTINFO option by 2540 * doing a "regular" setsockopt with ipi6_addr being 2541 * in6addr_any and ipi6_ifindex being zero. 2542 * [RFC 3542, Section 6] 2543 */ 2544 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2545 pktinfo->ipi6_ifindex == 0 && 2546 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2547 ip6_clearpktopts(opt, optname); 2548 break; 2549 } 2550 2551 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2552 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2553 return (EINVAL); 2554 } 2555 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr)) 2556 return (EINVAL); 2557 /* validate the interface index if specified. */ 2558 if (pktinfo->ipi6_ifindex > V_if_index) 2559 return (ENXIO); 2560 if (pktinfo->ipi6_ifindex) { 2561 ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 2562 if (ifp == NULL) 2563 return (ENXIO); 2564 } 2565 if (ifp != NULL && ( 2566 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) 2567 return (ENETDOWN); 2568 2569 if (ifp != NULL && 2570 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2571 struct in6_ifaddr *ia; 2572 2573 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr); 2574 if (ia == NULL) 2575 return (EADDRNOTAVAIL); 2576 ifa_free(&ia->ia_ifa); 2577 } 2578 /* 2579 * We store the address anyway, and let in6_selectsrc() 2580 * validate the specified address. This is because ipi6_addr 2581 * may not have enough information about its scope zone, and 2582 * we may need additional information (such as outgoing 2583 * interface or the scope zone of a destination address) to 2584 * disambiguate the scope. 2585 * XXX: the delay of the validation may confuse the 2586 * application when it is used as a sticky option. 2587 */ 2588 if (opt->ip6po_pktinfo == NULL) { 2589 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2590 M_IP6OPT, M_NOWAIT); 2591 if (opt->ip6po_pktinfo == NULL) 2592 return (ENOBUFS); 2593 } 2594 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 2595 break; 2596 } 2597 2598 case IPV6_2292HOPLIMIT: 2599 case IPV6_HOPLIMIT: 2600 { 2601 int *hlimp; 2602 2603 /* 2604 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2605 * to simplify the ordering among hoplimit options. 2606 */ 2607 if (optname == IPV6_HOPLIMIT && sticky) 2608 return (ENOPROTOOPT); 2609 2610 if (len != sizeof(int)) 2611 return (EINVAL); 2612 hlimp = (int *)buf; 2613 if (*hlimp < -1 || *hlimp > 255) 2614 return (EINVAL); 2615 2616 opt->ip6po_hlim = *hlimp; 2617 break; 2618 } 2619 2620 case IPV6_TCLASS: 2621 { 2622 int tclass; 2623 2624 if (len != sizeof(int)) 2625 return (EINVAL); 2626 tclass = *(int *)buf; 2627 if (tclass < -1 || tclass > 255) 2628 return (EINVAL); 2629 2630 opt->ip6po_tclass = tclass; 2631 break; 2632 } 2633 2634 case IPV6_2292NEXTHOP: 2635 case IPV6_NEXTHOP: 2636 if (cred != NULL) { 2637 error = priv_check_cred(cred, 2638 PRIV_NETINET_SETHDROPTS, 0); 2639 if (error) 2640 return (error); 2641 } 2642 2643 if (len == 0) { /* just remove the option */ 2644 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2645 break; 2646 } 2647 2648 /* check if cmsg_len is large enough for sa_len */ 2649 if (len < sizeof(struct sockaddr) || len < *buf) 2650 return (EINVAL); 2651 2652 switch (((struct sockaddr *)buf)->sa_family) { 2653 case AF_INET6: 2654 { 2655 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 2656 int error; 2657 2658 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 2659 return (EINVAL); 2660 2661 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 2662 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 2663 return (EINVAL); 2664 } 2665 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone)) 2666 != 0) { 2667 return (error); 2668 } 2669 break; 2670 } 2671 case AF_LINK: /* should eventually be supported */ 2672 default: 2673 return (EAFNOSUPPORT); 2674 } 2675 2676 /* turn off the previous option, then set the new option. */ 2677 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2678 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 2679 if (opt->ip6po_nexthop == NULL) 2680 return (ENOBUFS); 2681 bcopy(buf, opt->ip6po_nexthop, *buf); 2682 break; 2683 2684 case IPV6_2292HOPOPTS: 2685 case IPV6_HOPOPTS: 2686 { 2687 struct ip6_hbh *hbh; 2688 int hbhlen; 2689 2690 /* 2691 * XXX: We don't allow a non-privileged user to set ANY HbH 2692 * options, since per-option restriction has too much 2693 * overhead. 2694 */ 2695 if (cred != NULL) { 2696 error = priv_check_cred(cred, 2697 PRIV_NETINET_SETHDROPTS, 0); 2698 if (error) 2699 return (error); 2700 } 2701 2702 if (len == 0) { 2703 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2704 break; /* just remove the option */ 2705 } 2706 2707 /* message length validation */ 2708 if (len < sizeof(struct ip6_hbh)) 2709 return (EINVAL); 2710 hbh = (struct ip6_hbh *)buf; 2711 hbhlen = (hbh->ip6h_len + 1) << 3; 2712 if (len != hbhlen) 2713 return (EINVAL); 2714 2715 /* turn off the previous option, then set the new option. */ 2716 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2717 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 2718 if (opt->ip6po_hbh == NULL) 2719 return (ENOBUFS); 2720 bcopy(hbh, opt->ip6po_hbh, hbhlen); 2721 2722 break; 2723 } 2724 2725 case IPV6_2292DSTOPTS: 2726 case IPV6_DSTOPTS: 2727 case IPV6_RTHDRDSTOPTS: 2728 { 2729 struct ip6_dest *dest, **newdest = NULL; 2730 int destlen; 2731 2732 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */ 2733 error = priv_check_cred(cred, 2734 PRIV_NETINET_SETHDROPTS, 0); 2735 if (error) 2736 return (error); 2737 } 2738 2739 if (len == 0) { 2740 ip6_clearpktopts(opt, optname); 2741 break; /* just remove the option */ 2742 } 2743 2744 /* message length validation */ 2745 if (len < sizeof(struct ip6_dest)) 2746 return (EINVAL); 2747 dest = (struct ip6_dest *)buf; 2748 destlen = (dest->ip6d_len + 1) << 3; 2749 if (len != destlen) 2750 return (EINVAL); 2751 2752 /* 2753 * Determine the position that the destination options header 2754 * should be inserted; before or after the routing header. 2755 */ 2756 switch (optname) { 2757 case IPV6_2292DSTOPTS: 2758 /* 2759 * The old advacned API is ambiguous on this point. 2760 * Our approach is to determine the position based 2761 * according to the existence of a routing header. 2762 * Note, however, that this depends on the order of the 2763 * extension headers in the ancillary data; the 1st 2764 * part of the destination options header must appear 2765 * before the routing header in the ancillary data, 2766 * too. 2767 * RFC3542 solved the ambiguity by introducing 2768 * separate ancillary data or option types. 2769 */ 2770 if (opt->ip6po_rthdr == NULL) 2771 newdest = &opt->ip6po_dest1; 2772 else 2773 newdest = &opt->ip6po_dest2; 2774 break; 2775 case IPV6_RTHDRDSTOPTS: 2776 newdest = &opt->ip6po_dest1; 2777 break; 2778 case IPV6_DSTOPTS: 2779 newdest = &opt->ip6po_dest2; 2780 break; 2781 } 2782 2783 /* turn off the previous option, then set the new option. */ 2784 ip6_clearpktopts(opt, optname); 2785 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 2786 if (*newdest == NULL) 2787 return (ENOBUFS); 2788 bcopy(dest, *newdest, destlen); 2789 2790 break; 2791 } 2792 2793 case IPV6_2292RTHDR: 2794 case IPV6_RTHDR: 2795 { 2796 struct ip6_rthdr *rth; 2797 int rthlen; 2798 2799 if (len == 0) { 2800 ip6_clearpktopts(opt, IPV6_RTHDR); 2801 break; /* just remove the option */ 2802 } 2803 2804 /* message length validation */ 2805 if (len < sizeof(struct ip6_rthdr)) 2806 return (EINVAL); 2807 rth = (struct ip6_rthdr *)buf; 2808 rthlen = (rth->ip6r_len + 1) << 3; 2809 if (len != rthlen) 2810 return (EINVAL); 2811 2812 switch (rth->ip6r_type) { 2813 case IPV6_RTHDR_TYPE_0: 2814 if (rth->ip6r_len == 0) /* must contain one addr */ 2815 return (EINVAL); 2816 if (rth->ip6r_len % 2) /* length must be even */ 2817 return (EINVAL); 2818 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 2819 return (EINVAL); 2820 break; 2821 default: 2822 return (EINVAL); /* not supported */ 2823 } 2824 2825 /* turn off the previous option */ 2826 ip6_clearpktopts(opt, IPV6_RTHDR); 2827 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 2828 if (opt->ip6po_rthdr == NULL) 2829 return (ENOBUFS); 2830 bcopy(rth, opt->ip6po_rthdr, rthlen); 2831 2832 break; 2833 } 2834 2835 case IPV6_USE_MIN_MTU: 2836 if (len != sizeof(int)) 2837 return (EINVAL); 2838 minmtupolicy = *(int *)buf; 2839 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 2840 minmtupolicy != IP6PO_MINMTU_DISABLE && 2841 minmtupolicy != IP6PO_MINMTU_ALL) { 2842 return (EINVAL); 2843 } 2844 opt->ip6po_minmtu = minmtupolicy; 2845 break; 2846 2847 case IPV6_DONTFRAG: 2848 if (len != sizeof(int)) 2849 return (EINVAL); 2850 2851 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 2852 /* 2853 * we ignore this option for TCP sockets. 2854 * (RFC3542 leaves this case unspecified.) 2855 */ 2856 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 2857 } else 2858 opt->ip6po_flags |= IP6PO_DONTFRAG; 2859 break; 2860 2861 case IPV6_PREFER_TEMPADDR: 2862 if (len != sizeof(int)) 2863 return (EINVAL); 2864 preftemp = *(int *)buf; 2865 if (preftemp != IP6PO_TEMPADDR_SYSTEM && 2866 preftemp != IP6PO_TEMPADDR_NOTPREFER && 2867 preftemp != IP6PO_TEMPADDR_PREFER) { 2868 return (EINVAL); 2869 } 2870 opt->ip6po_prefer_tempaddr = preftemp; 2871 break; 2872 2873 default: 2874 return (ENOPROTOOPT); 2875 } /* end of switch */ 2876 2877 return (0); 2878 } 2879 2880 /* 2881 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 2882 * packet to the input queue of a specified interface. Note that this 2883 * calls the output routine of the loopback "driver", but with an interface 2884 * pointer that might NOT be &loif -- easier than replicating that code here. 2885 */ 2886 void 2887 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst) 2888 { 2889 struct mbuf *copym; 2890 struct ip6_hdr *ip6; 2891 2892 copym = m_copy(m, 0, M_COPYALL); 2893 if (copym == NULL) 2894 return; 2895 2896 /* 2897 * Make sure to deep-copy IPv6 header portion in case the data 2898 * is in an mbuf cluster, so that we can safely override the IPv6 2899 * header portion later. 2900 */ 2901 if (!M_WRITABLE(copym) || 2902 copym->m_len < sizeof(struct ip6_hdr)) { 2903 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 2904 if (copym == NULL) 2905 return; 2906 } 2907 2908 #ifdef DIAGNOSTIC 2909 if (copym->m_len < sizeof(*ip6)) { 2910 m_freem(copym); 2911 return; 2912 } 2913 #endif 2914 2915 ip6 = mtod(copym, struct ip6_hdr *); 2916 /* 2917 * clear embedded scope identifiers if necessary. 2918 * in6_clearscope will touch the addresses only when necessary. 2919 */ 2920 in6_clearscope(&ip6->ip6_src); 2921 in6_clearscope(&ip6->ip6_dst); 2922 2923 (void)if_simloop(ifp, copym, dst->sin6_family, 0); 2924 } 2925 2926 /* 2927 * Chop IPv6 header off from the payload. 2928 */ 2929 static int 2930 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 2931 { 2932 struct mbuf *mh; 2933 struct ip6_hdr *ip6; 2934 2935 ip6 = mtod(m, struct ip6_hdr *); 2936 if (m->m_len > sizeof(*ip6)) { 2937 mh = m_gethdr(M_NOWAIT, MT_DATA); 2938 if (mh == NULL) { 2939 m_freem(m); 2940 return ENOBUFS; 2941 } 2942 m_move_pkthdr(mh, m); 2943 MH_ALIGN(mh, sizeof(*ip6)); 2944 m->m_len -= sizeof(*ip6); 2945 m->m_data += sizeof(*ip6); 2946 mh->m_next = m; 2947 m = mh; 2948 m->m_len = sizeof(*ip6); 2949 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 2950 } 2951 exthdrs->ip6e_ip6 = m; 2952 return 0; 2953 } 2954 2955 /* 2956 * Compute IPv6 extension header length. 2957 */ 2958 int 2959 ip6_optlen(struct inpcb *in6p) 2960 { 2961 int len; 2962 2963 if (!in6p->in6p_outputopts) 2964 return 0; 2965 2966 len = 0; 2967 #define elen(x) \ 2968 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 2969 2970 len += elen(in6p->in6p_outputopts->ip6po_hbh); 2971 if (in6p->in6p_outputopts->ip6po_rthdr) 2972 /* dest1 is valid with rthdr only */ 2973 len += elen(in6p->in6p_outputopts->ip6po_dest1); 2974 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 2975 len += elen(in6p->in6p_outputopts->ip6po_dest2); 2976 return len; 2977 #undef elen 2978 } 2979