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