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