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