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