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