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