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 epoch_tracker et; 388 struct route_in6 ip6route; 389 struct rtentry *rt = NULL; 390 struct sockaddr_in6 *dst, src_sa, dst_sa; 391 struct in6_addr odst; 392 int error = 0; 393 struct in6_ifaddr *ia = NULL; 394 u_long mtu; 395 int alwaysfrag, dontfrag; 396 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; 397 struct ip6_exthdrs exthdrs; 398 struct in6_addr src0, dst0; 399 u_int32_t zone; 400 struct route_in6 *ro_pmtu = NULL; 401 int hdrsplit = 0; 402 int sw_csum, tso; 403 int needfiblookup; 404 uint32_t fibnum; 405 struct m_tag *fwd_tag = NULL; 406 uint32_t id; 407 408 if (inp != NULL) { 409 INP_LOCK_ASSERT(inp); 410 M_SETFIB(m, inp->inp_inc.inc_fibnum); 411 if ((flags & IP_NODEFAULTFLOWID) == 0) { 412 /* unconditionally set flowid */ 413 m->m_pkthdr.flowid = inp->inp_flowid; 414 M_HASHTYPE_SET(m, inp->inp_flowtype); 415 } 416 #ifdef NUMA 417 m->m_pkthdr.numa_domain = inp->inp_numa_domain; 418 #endif 419 } 420 421 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 422 /* 423 * IPSec checking which handles several cases. 424 * FAST IPSEC: We re-injected the packet. 425 * XXX: need scope argument. 426 */ 427 if (IPSEC_ENABLED(ipv6)) { 428 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) { 429 if (error == EINPROGRESS) 430 error = 0; 431 goto done; 432 } 433 } 434 #endif /* IPSEC */ 435 436 bzero(&exthdrs, sizeof(exthdrs)); 437 if (opt) { 438 /* Hop-by-Hop options header */ 439 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 440 /* Destination options header(1st part) */ 441 if (opt->ip6po_rthdr) { 442 /* 443 * Destination options header(1st part) 444 * This only makes sense with a routing header. 445 * See Section 9.2 of RFC 3542. 446 * Disabling this part just for MIP6 convenience is 447 * a bad idea. We need to think carefully about a 448 * way to make the advanced API coexist with MIP6 449 * options, which might automatically be inserted in 450 * the kernel. 451 */ 452 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 453 } 454 /* Routing header */ 455 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 456 /* Destination options header(2nd part) */ 457 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 458 } 459 460 /* 461 * Calculate the total length of the extension header chain. 462 * Keep the length of the unfragmentable part for fragmentation. 463 */ 464 optlen = 0; 465 if (exthdrs.ip6e_hbh) 466 optlen += exthdrs.ip6e_hbh->m_len; 467 if (exthdrs.ip6e_dest1) 468 optlen += exthdrs.ip6e_dest1->m_len; 469 if (exthdrs.ip6e_rthdr) 470 optlen += exthdrs.ip6e_rthdr->m_len; 471 unfragpartlen = optlen + sizeof(struct ip6_hdr); 472 473 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */ 474 if (exthdrs.ip6e_dest2) 475 optlen += exthdrs.ip6e_dest2->m_len; 476 477 /* 478 * If there is at least one extension header, 479 * separate IP6 header from the payload. 480 */ 481 if (optlen && !hdrsplit) { 482 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 483 m = NULL; 484 goto freehdrs; 485 } 486 m = exthdrs.ip6e_ip6; 487 hdrsplit++; 488 } 489 490 ip6 = mtod(m, struct ip6_hdr *); 491 492 /* adjust mbuf packet header length */ 493 m->m_pkthdr.len += optlen; 494 plen = m->m_pkthdr.len - sizeof(*ip6); 495 496 /* If this is a jumbo payload, insert a jumbo payload option. */ 497 if (plen > IPV6_MAXPACKET) { 498 if (!hdrsplit) { 499 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 500 m = NULL; 501 goto freehdrs; 502 } 503 m = exthdrs.ip6e_ip6; 504 hdrsplit++; 505 } 506 /* adjust pointer */ 507 ip6 = mtod(m, struct ip6_hdr *); 508 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 509 goto freehdrs; 510 ip6->ip6_plen = 0; 511 } else 512 ip6->ip6_plen = htons(plen); 513 514 /* 515 * Concatenate headers and fill in next header fields. 516 * Here we have, on "m" 517 * IPv6 payload 518 * and we insert headers accordingly. Finally, we should be getting: 519 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 520 * 521 * during the header composing process, "m" points to IPv6 header. 522 * "mprev" points to an extension header prior to esp. 523 */ 524 u_char *nexthdrp = &ip6->ip6_nxt; 525 mprev = m; 526 527 /* 528 * we treat dest2 specially. this makes IPsec processing 529 * much easier. the goal here is to make mprev point the 530 * mbuf prior to dest2. 531 * 532 * result: IPv6 dest2 payload 533 * m and mprev will point to IPv6 header. 534 */ 535 if (exthdrs.ip6e_dest2) { 536 if (!hdrsplit) 537 panic("assumption failed: hdr not split"); 538 exthdrs.ip6e_dest2->m_next = m->m_next; 539 m->m_next = exthdrs.ip6e_dest2; 540 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 541 ip6->ip6_nxt = IPPROTO_DSTOPTS; 542 } 543 544 /* 545 * result: IPv6 hbh dest1 rthdr dest2 payload 546 * m will point to IPv6 header. mprev will point to the 547 * extension header prior to dest2 (rthdr in the above case). 548 */ 549 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 550 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 551 IPPROTO_DSTOPTS); 552 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 553 IPPROTO_ROUTING); 554 555 /* 556 * If there is a routing header, discard the packet. 557 */ 558 if (exthdrs.ip6e_rthdr) { 559 error = EINVAL; 560 goto bad; 561 } 562 563 /* Source address validation */ 564 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 565 (flags & IPV6_UNSPECSRC) == 0) { 566 error = EOPNOTSUPP; 567 IP6STAT_INC(ip6s_badscope); 568 goto bad; 569 } 570 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 571 error = EOPNOTSUPP; 572 IP6STAT_INC(ip6s_badscope); 573 goto bad; 574 } 575 576 IP6STAT_INC(ip6s_localout); 577 578 /* 579 * Route packet. 580 */ 581 if (ro == NULL) { 582 ro = &ip6route; 583 bzero((caddr_t)ro, sizeof(*ro)); 584 } 585 ro_pmtu = ro; 586 if (opt && opt->ip6po_rthdr) 587 ro = &opt->ip6po_route; 588 dst = (struct sockaddr_in6 *)&ro->ro_dst; 589 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m); 590 NET_EPOCH_ENTER(et); 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 = EPERM; 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 NET_EPOCH_EXIT(et); 1192 if (ro == &ip6route) 1193 RO_RTFREE(ro); 1194 return (error); 1195 1196 freehdrs: 1197 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1198 m_freem(exthdrs.ip6e_dest1); 1199 m_freem(exthdrs.ip6e_rthdr); 1200 m_freem(exthdrs.ip6e_dest2); 1201 /* FALLTHROUGH */ 1202 bad: 1203 if (m) 1204 m_freem(m); 1205 goto done; 1206 } 1207 1208 static int 1209 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen) 1210 { 1211 struct mbuf *m; 1212 1213 if (hlen > MCLBYTES) 1214 return (ENOBUFS); /* XXX */ 1215 1216 if (hlen > MLEN) 1217 m = m_getcl(M_NOWAIT, MT_DATA, 0); 1218 else 1219 m = m_get(M_NOWAIT, MT_DATA); 1220 if (m == NULL) 1221 return (ENOBUFS); 1222 m->m_len = hlen; 1223 if (hdr) 1224 bcopy(hdr, mtod(m, caddr_t), hlen); 1225 1226 *mp = m; 1227 return (0); 1228 } 1229 1230 /* 1231 * Insert jumbo payload option. 1232 */ 1233 static int 1234 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) 1235 { 1236 struct mbuf *mopt; 1237 u_char *optbuf; 1238 u_int32_t v; 1239 1240 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1241 1242 /* 1243 * If there is no hop-by-hop options header, allocate new one. 1244 * If there is one but it doesn't have enough space to store the 1245 * jumbo payload option, allocate a cluster to store the whole options. 1246 * Otherwise, use it to store the options. 1247 */ 1248 if (exthdrs->ip6e_hbh == NULL) { 1249 mopt = m_get(M_NOWAIT, MT_DATA); 1250 if (mopt == NULL) 1251 return (ENOBUFS); 1252 mopt->m_len = JUMBOOPTLEN; 1253 optbuf = mtod(mopt, u_char *); 1254 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1255 exthdrs->ip6e_hbh = mopt; 1256 } else { 1257 struct ip6_hbh *hbh; 1258 1259 mopt = exthdrs->ip6e_hbh; 1260 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1261 /* 1262 * XXX assumption: 1263 * - exthdrs->ip6e_hbh is not referenced from places 1264 * other than exthdrs. 1265 * - exthdrs->ip6e_hbh is not an mbuf chain. 1266 */ 1267 int oldoptlen = mopt->m_len; 1268 struct mbuf *n; 1269 1270 /* 1271 * XXX: give up if the whole (new) hbh header does 1272 * not fit even in an mbuf cluster. 1273 */ 1274 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1275 return (ENOBUFS); 1276 1277 /* 1278 * As a consequence, we must always prepare a cluster 1279 * at this point. 1280 */ 1281 n = m_getcl(M_NOWAIT, MT_DATA, 0); 1282 if (n == NULL) 1283 return (ENOBUFS); 1284 n->m_len = oldoptlen + JUMBOOPTLEN; 1285 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1286 oldoptlen); 1287 optbuf = mtod(n, caddr_t) + oldoptlen; 1288 m_freem(mopt); 1289 mopt = exthdrs->ip6e_hbh = n; 1290 } else { 1291 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1292 mopt->m_len += JUMBOOPTLEN; 1293 } 1294 optbuf[0] = IP6OPT_PADN; 1295 optbuf[1] = 1; 1296 1297 /* 1298 * Adjust the header length according to the pad and 1299 * the jumbo payload option. 1300 */ 1301 hbh = mtod(mopt, struct ip6_hbh *); 1302 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1303 } 1304 1305 /* fill in the option. */ 1306 optbuf[2] = IP6OPT_JUMBO; 1307 optbuf[3] = 4; 1308 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1309 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1310 1311 /* finally, adjust the packet header length */ 1312 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1313 1314 return (0); 1315 #undef JUMBOOPTLEN 1316 } 1317 1318 /* 1319 * Insert fragment header and copy unfragmentable header portions. 1320 */ 1321 static int 1322 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, 1323 struct ip6_frag **frghdrp) 1324 { 1325 struct mbuf *n, *mlast; 1326 1327 if (hlen > sizeof(struct ip6_hdr)) { 1328 n = m_copym(m0, sizeof(struct ip6_hdr), 1329 hlen - sizeof(struct ip6_hdr), M_NOWAIT); 1330 if (n == NULL) 1331 return (ENOBUFS); 1332 m->m_next = n; 1333 } else 1334 n = m; 1335 1336 /* Search for the last mbuf of unfragmentable part. */ 1337 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1338 ; 1339 1340 if (M_WRITABLE(mlast) && 1341 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1342 /* use the trailing space of the last mbuf for the fragment hdr */ 1343 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) + 1344 mlast->m_len); 1345 mlast->m_len += sizeof(struct ip6_frag); 1346 m->m_pkthdr.len += sizeof(struct ip6_frag); 1347 } else { 1348 /* allocate a new mbuf for the fragment header */ 1349 struct mbuf *mfrg; 1350 1351 mfrg = m_get(M_NOWAIT, MT_DATA); 1352 if (mfrg == NULL) 1353 return (ENOBUFS); 1354 mfrg->m_len = sizeof(struct ip6_frag); 1355 *frghdrp = mtod(mfrg, struct ip6_frag *); 1356 mlast->m_next = mfrg; 1357 } 1358 1359 return (0); 1360 } 1361 1362 /* 1363 * Calculates IPv6 path mtu for destination @dst. 1364 * Resulting MTU is stored in @mtup. 1365 * 1366 * Returns 0 on success. 1367 */ 1368 static int 1369 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup) 1370 { 1371 struct nhop6_extended nh6; 1372 struct in6_addr kdst; 1373 uint32_t scopeid; 1374 struct ifnet *ifp; 1375 u_long mtu; 1376 int error; 1377 1378 in6_splitscope(dst, &kdst, &scopeid); 1379 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0) 1380 return (EHOSTUNREACH); 1381 1382 ifp = nh6.nh_ifp; 1383 mtu = nh6.nh_mtu; 1384 1385 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0); 1386 fib6_free_nh_ext(fibnum, &nh6); 1387 1388 return (error); 1389 } 1390 1391 /* 1392 * Calculates IPv6 path MTU for @dst based on transmit @ifp, 1393 * and cached data in @ro_pmtu. 1394 * MTU from (successful) route lookup is saved (along with dst) 1395 * inside @ro_pmtu to avoid subsequent route lookups after packet 1396 * filter processing. 1397 * 1398 * Stores mtu and always-frag value into @mtup and @alwaysfragp. 1399 * Returns 0 on success. 1400 */ 1401 static int 1402 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup, 1403 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup, 1404 int *alwaysfragp, u_int fibnum, u_int proto) 1405 { 1406 struct nhop6_basic nh6; 1407 struct in6_addr kdst; 1408 uint32_t scopeid; 1409 struct sockaddr_in6 *sa6_dst; 1410 u_long mtu; 1411 1412 mtu = 0; 1413 if (do_lookup) { 1414 1415 /* 1416 * Here ro_pmtu has final destination address, while 1417 * ro might represent immediate destination. 1418 * Use ro_pmtu destination since mtu might differ. 1419 */ 1420 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 1421 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst)) 1422 ro_pmtu->ro_mtu = 0; 1423 1424 if (ro_pmtu->ro_mtu == 0) { 1425 bzero(sa6_dst, sizeof(*sa6_dst)); 1426 sa6_dst->sin6_family = AF_INET6; 1427 sa6_dst->sin6_len = sizeof(struct sockaddr_in6); 1428 sa6_dst->sin6_addr = *dst; 1429 1430 in6_splitscope(dst, &kdst, &scopeid); 1431 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0, 1432 &nh6) == 0) 1433 ro_pmtu->ro_mtu = nh6.nh_mtu; 1434 } 1435 1436 mtu = ro_pmtu->ro_mtu; 1437 } 1438 1439 if (ro_pmtu->ro_rt) 1440 mtu = ro_pmtu->ro_rt->rt_mtu; 1441 1442 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto)); 1443 } 1444 1445 /* 1446 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and 1447 * hostcache data for @dst. 1448 * Stores mtu and always-frag value into @mtup and @alwaysfragp. 1449 * 1450 * Returns 0 on success. 1451 */ 1452 static int 1453 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu, 1454 u_long *mtup, int *alwaysfragp, u_int proto) 1455 { 1456 u_long mtu = 0; 1457 int alwaysfrag = 0; 1458 int error = 0; 1459 1460 if (rt_mtu > 0) { 1461 u_int32_t ifmtu; 1462 struct in_conninfo inc; 1463 1464 bzero(&inc, sizeof(inc)); 1465 inc.inc_flags |= INC_ISIPV6; 1466 inc.inc6_faddr = *dst; 1467 1468 ifmtu = IN6_LINKMTU(ifp); 1469 1470 /* TCP is known to react to pmtu changes so skip hc */ 1471 if (proto != IPPROTO_TCP) 1472 mtu = tcp_hc_getmtu(&inc); 1473 1474 if (mtu) 1475 mtu = min(mtu, rt_mtu); 1476 else 1477 mtu = rt_mtu; 1478 if (mtu == 0) 1479 mtu = ifmtu; 1480 else if (mtu < IPV6_MMTU) { 1481 /* 1482 * RFC2460 section 5, last paragraph: 1483 * if we record ICMPv6 too big message with 1484 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU 1485 * or smaller, with framgent header attached. 1486 * (fragment header is needed regardless from the 1487 * packet size, for translators to identify packets) 1488 */ 1489 alwaysfrag = 1; 1490 mtu = IPV6_MMTU; 1491 } 1492 } else if (ifp) { 1493 mtu = IN6_LINKMTU(ifp); 1494 } else 1495 error = EHOSTUNREACH; /* XXX */ 1496 1497 *mtup = mtu; 1498 if (alwaysfragp) 1499 *alwaysfragp = alwaysfrag; 1500 return (error); 1501 } 1502 1503 /* 1504 * IP6 socket option processing. 1505 */ 1506 int 1507 ip6_ctloutput(struct socket *so, struct sockopt *sopt) 1508 { 1509 int optdatalen, uproto; 1510 void *optdata; 1511 struct inpcb *inp = sotoinpcb(so); 1512 int error, optval; 1513 int level, op, optname; 1514 int optlen; 1515 struct thread *td; 1516 #ifdef RSS 1517 uint32_t rss_bucket; 1518 int retval; 1519 #endif 1520 1521 /* 1522 * Don't use more than a quarter of mbuf clusters. N.B.: 1523 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow 1524 * on LP64 architectures, so cast to u_long to avoid undefined 1525 * behavior. ILP32 architectures cannot have nmbclusters 1526 * large enough to overflow for other reasons. 1527 */ 1528 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4) 1529 1530 level = sopt->sopt_level; 1531 op = sopt->sopt_dir; 1532 optname = sopt->sopt_name; 1533 optlen = sopt->sopt_valsize; 1534 td = sopt->sopt_td; 1535 error = 0; 1536 optval = 0; 1537 uproto = (int)so->so_proto->pr_protocol; 1538 1539 if (level != IPPROTO_IPV6) { 1540 error = EINVAL; 1541 1542 if (sopt->sopt_level == SOL_SOCKET && 1543 sopt->sopt_dir == SOPT_SET) { 1544 switch (sopt->sopt_name) { 1545 case SO_REUSEADDR: 1546 INP_WLOCK(inp); 1547 if ((so->so_options & SO_REUSEADDR) != 0) 1548 inp->inp_flags2 |= INP_REUSEADDR; 1549 else 1550 inp->inp_flags2 &= ~INP_REUSEADDR; 1551 INP_WUNLOCK(inp); 1552 error = 0; 1553 break; 1554 case SO_REUSEPORT: 1555 INP_WLOCK(inp); 1556 if ((so->so_options & SO_REUSEPORT) != 0) 1557 inp->inp_flags2 |= INP_REUSEPORT; 1558 else 1559 inp->inp_flags2 &= ~INP_REUSEPORT; 1560 INP_WUNLOCK(inp); 1561 error = 0; 1562 break; 1563 case SO_REUSEPORT_LB: 1564 INP_WLOCK(inp); 1565 if ((so->so_options & SO_REUSEPORT_LB) != 0) 1566 inp->inp_flags2 |= INP_REUSEPORT_LB; 1567 else 1568 inp->inp_flags2 &= ~INP_REUSEPORT_LB; 1569 INP_WUNLOCK(inp); 1570 error = 0; 1571 break; 1572 case SO_SETFIB: 1573 INP_WLOCK(inp); 1574 inp->inp_inc.inc_fibnum = so->so_fibnum; 1575 INP_WUNLOCK(inp); 1576 error = 0; 1577 break; 1578 case SO_MAX_PACING_RATE: 1579 #ifdef RATELIMIT 1580 INP_WLOCK(inp); 1581 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 1582 INP_WUNLOCK(inp); 1583 error = 0; 1584 #else 1585 error = EOPNOTSUPP; 1586 #endif 1587 break; 1588 default: 1589 break; 1590 } 1591 } 1592 } else { /* level == IPPROTO_IPV6 */ 1593 switch (op) { 1594 1595 case SOPT_SET: 1596 switch (optname) { 1597 case IPV6_2292PKTOPTIONS: 1598 #ifdef IPV6_PKTOPTIONS 1599 case IPV6_PKTOPTIONS: 1600 #endif 1601 { 1602 struct mbuf *m; 1603 1604 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) { 1605 printf("ip6_ctloutput: mbuf limit hit\n"); 1606 error = ENOBUFS; 1607 break; 1608 } 1609 1610 error = soopt_getm(sopt, &m); /* XXX */ 1611 if (error != 0) 1612 break; 1613 error = soopt_mcopyin(sopt, m); /* XXX */ 1614 if (error != 0) 1615 break; 1616 error = ip6_pcbopts(&inp->in6p_outputopts, 1617 m, so, sopt); 1618 m_freem(m); /* XXX */ 1619 break; 1620 } 1621 1622 /* 1623 * Use of some Hop-by-Hop options or some 1624 * Destination options, might require special 1625 * privilege. That is, normal applications 1626 * (without special privilege) might be forbidden 1627 * from setting certain options in outgoing packets, 1628 * and might never see certain options in received 1629 * packets. [RFC 2292 Section 6] 1630 * KAME specific note: 1631 * KAME prevents non-privileged users from sending or 1632 * receiving ANY hbh/dst options in order to avoid 1633 * overhead of parsing options in the kernel. 1634 */ 1635 case IPV6_RECVHOPOPTS: 1636 case IPV6_RECVDSTOPTS: 1637 case IPV6_RECVRTHDRDSTOPTS: 1638 if (td != NULL) { 1639 error = priv_check(td, 1640 PRIV_NETINET_SETHDROPTS); 1641 if (error) 1642 break; 1643 } 1644 /* FALLTHROUGH */ 1645 case IPV6_UNICAST_HOPS: 1646 case IPV6_HOPLIMIT: 1647 1648 case IPV6_RECVPKTINFO: 1649 case IPV6_RECVHOPLIMIT: 1650 case IPV6_RECVRTHDR: 1651 case IPV6_RECVPATHMTU: 1652 case IPV6_RECVTCLASS: 1653 case IPV6_RECVFLOWID: 1654 #ifdef RSS 1655 case IPV6_RECVRSSBUCKETID: 1656 #endif 1657 case IPV6_V6ONLY: 1658 case IPV6_AUTOFLOWLABEL: 1659 case IPV6_ORIGDSTADDR: 1660 case IPV6_BINDANY: 1661 case IPV6_BINDMULTI: 1662 #ifdef RSS 1663 case IPV6_RSS_LISTEN_BUCKET: 1664 #endif 1665 if (optname == IPV6_BINDANY && td != NULL) { 1666 error = priv_check(td, 1667 PRIV_NETINET_BINDANY); 1668 if (error) 1669 break; 1670 } 1671 1672 if (optlen != sizeof(int)) { 1673 error = EINVAL; 1674 break; 1675 } 1676 error = sooptcopyin(sopt, &optval, 1677 sizeof optval, sizeof optval); 1678 if (error) 1679 break; 1680 switch (optname) { 1681 1682 case IPV6_UNICAST_HOPS: 1683 if (optval < -1 || optval >= 256) 1684 error = EINVAL; 1685 else { 1686 /* -1 = kernel default */ 1687 inp->in6p_hops = optval; 1688 if ((inp->inp_vflag & 1689 INP_IPV4) != 0) 1690 inp->inp_ip_ttl = optval; 1691 } 1692 break; 1693 #define OPTSET(bit) \ 1694 do { \ 1695 INP_WLOCK(inp); \ 1696 if (optval) \ 1697 inp->inp_flags |= (bit); \ 1698 else \ 1699 inp->inp_flags &= ~(bit); \ 1700 INP_WUNLOCK(inp); \ 1701 } while (/*CONSTCOND*/ 0) 1702 #define OPTSET2292(bit) \ 1703 do { \ 1704 INP_WLOCK(inp); \ 1705 inp->inp_flags |= IN6P_RFC2292; \ 1706 if (optval) \ 1707 inp->inp_flags |= (bit); \ 1708 else \ 1709 inp->inp_flags &= ~(bit); \ 1710 INP_WUNLOCK(inp); \ 1711 } while (/*CONSTCOND*/ 0) 1712 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0) 1713 1714 #define OPTSET2_N(bit, val) do { \ 1715 if (val) \ 1716 inp->inp_flags2 |= bit; \ 1717 else \ 1718 inp->inp_flags2 &= ~bit; \ 1719 } while (0) 1720 #define OPTSET2(bit, val) do { \ 1721 INP_WLOCK(inp); \ 1722 OPTSET2_N(bit, val); \ 1723 INP_WUNLOCK(inp); \ 1724 } while (0) 1725 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0) 1726 #define OPTSET2292_EXCLUSIVE(bit) \ 1727 do { \ 1728 INP_WLOCK(inp); \ 1729 if (OPTBIT(IN6P_RFC2292)) { \ 1730 error = EINVAL; \ 1731 } else { \ 1732 if (optval) \ 1733 inp->inp_flags |= (bit); \ 1734 else \ 1735 inp->inp_flags &= ~(bit); \ 1736 } \ 1737 INP_WUNLOCK(inp); \ 1738 } while (/*CONSTCOND*/ 0) 1739 1740 case IPV6_RECVPKTINFO: 1741 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO); 1742 break; 1743 1744 case IPV6_HOPLIMIT: 1745 { 1746 struct ip6_pktopts **optp; 1747 1748 /* cannot mix with RFC2292 */ 1749 if (OPTBIT(IN6P_RFC2292)) { 1750 error = EINVAL; 1751 break; 1752 } 1753 INP_WLOCK(inp); 1754 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1755 INP_WUNLOCK(inp); 1756 return (ECONNRESET); 1757 } 1758 optp = &inp->in6p_outputopts; 1759 error = ip6_pcbopt(IPV6_HOPLIMIT, 1760 (u_char *)&optval, sizeof(optval), 1761 optp, (td != NULL) ? td->td_ucred : 1762 NULL, uproto); 1763 INP_WUNLOCK(inp); 1764 break; 1765 } 1766 1767 case IPV6_RECVHOPLIMIT: 1768 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT); 1769 break; 1770 1771 case IPV6_RECVHOPOPTS: 1772 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS); 1773 break; 1774 1775 case IPV6_RECVDSTOPTS: 1776 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS); 1777 break; 1778 1779 case IPV6_RECVRTHDRDSTOPTS: 1780 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS); 1781 break; 1782 1783 case IPV6_RECVRTHDR: 1784 OPTSET2292_EXCLUSIVE(IN6P_RTHDR); 1785 break; 1786 1787 case IPV6_RECVPATHMTU: 1788 /* 1789 * We ignore this option for TCP 1790 * sockets. 1791 * (RFC3542 leaves this case 1792 * unspecified.) 1793 */ 1794 if (uproto != IPPROTO_TCP) 1795 OPTSET(IN6P_MTU); 1796 break; 1797 1798 case IPV6_RECVFLOWID: 1799 OPTSET2(INP_RECVFLOWID, optval); 1800 break; 1801 1802 #ifdef RSS 1803 case IPV6_RECVRSSBUCKETID: 1804 OPTSET2(INP_RECVRSSBUCKETID, optval); 1805 break; 1806 #endif 1807 1808 case IPV6_V6ONLY: 1809 INP_WLOCK(inp); 1810 if (inp->inp_lport || 1811 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 1812 /* 1813 * The socket is already bound. 1814 */ 1815 INP_WUNLOCK(inp); 1816 error = EINVAL; 1817 break; 1818 } 1819 if (optval) { 1820 inp->inp_flags |= IN6P_IPV6_V6ONLY; 1821 inp->inp_vflag &= ~INP_IPV4; 1822 } else { 1823 inp->inp_flags &= ~IN6P_IPV6_V6ONLY; 1824 inp->inp_vflag |= INP_IPV4; 1825 } 1826 INP_WUNLOCK(inp); 1827 break; 1828 case IPV6_RECVTCLASS: 1829 /* cannot mix with RFC2292 XXX */ 1830 OPTSET2292_EXCLUSIVE(IN6P_TCLASS); 1831 break; 1832 case IPV6_AUTOFLOWLABEL: 1833 OPTSET(IN6P_AUTOFLOWLABEL); 1834 break; 1835 1836 case IPV6_ORIGDSTADDR: 1837 OPTSET2(INP_ORIGDSTADDR, optval); 1838 break; 1839 case IPV6_BINDANY: 1840 OPTSET(INP_BINDANY); 1841 break; 1842 1843 case IPV6_BINDMULTI: 1844 OPTSET2(INP_BINDMULTI, optval); 1845 break; 1846 #ifdef RSS 1847 case IPV6_RSS_LISTEN_BUCKET: 1848 if ((optval >= 0) && 1849 (optval < rss_getnumbuckets())) { 1850 INP_WLOCK(inp); 1851 inp->inp_rss_listen_bucket = optval; 1852 OPTSET2_N(INP_RSS_BUCKET_SET, 1); 1853 INP_WUNLOCK(inp); 1854 } else { 1855 error = EINVAL; 1856 } 1857 break; 1858 #endif 1859 } 1860 break; 1861 1862 case IPV6_TCLASS: 1863 case IPV6_DONTFRAG: 1864 case IPV6_USE_MIN_MTU: 1865 case IPV6_PREFER_TEMPADDR: 1866 if (optlen != sizeof(optval)) { 1867 error = EINVAL; 1868 break; 1869 } 1870 error = sooptcopyin(sopt, &optval, 1871 sizeof optval, sizeof optval); 1872 if (error) 1873 break; 1874 { 1875 struct ip6_pktopts **optp; 1876 INP_WLOCK(inp); 1877 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1878 INP_WUNLOCK(inp); 1879 return (ECONNRESET); 1880 } 1881 optp = &inp->in6p_outputopts; 1882 error = ip6_pcbopt(optname, 1883 (u_char *)&optval, sizeof(optval), 1884 optp, (td != NULL) ? td->td_ucred : 1885 NULL, uproto); 1886 INP_WUNLOCK(inp); 1887 break; 1888 } 1889 1890 case IPV6_2292PKTINFO: 1891 case IPV6_2292HOPLIMIT: 1892 case IPV6_2292HOPOPTS: 1893 case IPV6_2292DSTOPTS: 1894 case IPV6_2292RTHDR: 1895 /* RFC 2292 */ 1896 if (optlen != sizeof(int)) { 1897 error = EINVAL; 1898 break; 1899 } 1900 error = sooptcopyin(sopt, &optval, 1901 sizeof optval, sizeof optval); 1902 if (error) 1903 break; 1904 switch (optname) { 1905 case IPV6_2292PKTINFO: 1906 OPTSET2292(IN6P_PKTINFO); 1907 break; 1908 case IPV6_2292HOPLIMIT: 1909 OPTSET2292(IN6P_HOPLIMIT); 1910 break; 1911 case IPV6_2292HOPOPTS: 1912 /* 1913 * Check super-user privilege. 1914 * See comments for IPV6_RECVHOPOPTS. 1915 */ 1916 if (td != NULL) { 1917 error = priv_check(td, 1918 PRIV_NETINET_SETHDROPTS); 1919 if (error) 1920 return (error); 1921 } 1922 OPTSET2292(IN6P_HOPOPTS); 1923 break; 1924 case IPV6_2292DSTOPTS: 1925 if (td != NULL) { 1926 error = priv_check(td, 1927 PRIV_NETINET_SETHDROPTS); 1928 if (error) 1929 return (error); 1930 } 1931 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1932 break; 1933 case IPV6_2292RTHDR: 1934 OPTSET2292(IN6P_RTHDR); 1935 break; 1936 } 1937 break; 1938 case IPV6_PKTINFO: 1939 case IPV6_HOPOPTS: 1940 case IPV6_RTHDR: 1941 case IPV6_DSTOPTS: 1942 case IPV6_RTHDRDSTOPTS: 1943 case IPV6_NEXTHOP: 1944 { 1945 /* new advanced API (RFC3542) */ 1946 u_char *optbuf; 1947 u_char optbuf_storage[MCLBYTES]; 1948 int optlen; 1949 struct ip6_pktopts **optp; 1950 1951 /* cannot mix with RFC2292 */ 1952 if (OPTBIT(IN6P_RFC2292)) { 1953 error = EINVAL; 1954 break; 1955 } 1956 1957 /* 1958 * We only ensure valsize is not too large 1959 * here. Further validation will be done 1960 * later. 1961 */ 1962 error = sooptcopyin(sopt, optbuf_storage, 1963 sizeof(optbuf_storage), 0); 1964 if (error) 1965 break; 1966 optlen = sopt->sopt_valsize; 1967 optbuf = optbuf_storage; 1968 INP_WLOCK(inp); 1969 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1970 INP_WUNLOCK(inp); 1971 return (ECONNRESET); 1972 } 1973 optp = &inp->in6p_outputopts; 1974 error = ip6_pcbopt(optname, optbuf, optlen, 1975 optp, (td != NULL) ? td->td_ucred : NULL, 1976 uproto); 1977 INP_WUNLOCK(inp); 1978 break; 1979 } 1980 #undef OPTSET 1981 1982 case IPV6_MULTICAST_IF: 1983 case IPV6_MULTICAST_HOPS: 1984 case IPV6_MULTICAST_LOOP: 1985 case IPV6_JOIN_GROUP: 1986 case IPV6_LEAVE_GROUP: 1987 case IPV6_MSFILTER: 1988 case MCAST_BLOCK_SOURCE: 1989 case MCAST_UNBLOCK_SOURCE: 1990 case MCAST_JOIN_GROUP: 1991 case MCAST_LEAVE_GROUP: 1992 case MCAST_JOIN_SOURCE_GROUP: 1993 case MCAST_LEAVE_SOURCE_GROUP: 1994 error = ip6_setmoptions(inp, sopt); 1995 break; 1996 1997 case IPV6_PORTRANGE: 1998 error = sooptcopyin(sopt, &optval, 1999 sizeof optval, sizeof optval); 2000 if (error) 2001 break; 2002 2003 INP_WLOCK(inp); 2004 switch (optval) { 2005 case IPV6_PORTRANGE_DEFAULT: 2006 inp->inp_flags &= ~(INP_LOWPORT); 2007 inp->inp_flags &= ~(INP_HIGHPORT); 2008 break; 2009 2010 case IPV6_PORTRANGE_HIGH: 2011 inp->inp_flags &= ~(INP_LOWPORT); 2012 inp->inp_flags |= INP_HIGHPORT; 2013 break; 2014 2015 case IPV6_PORTRANGE_LOW: 2016 inp->inp_flags &= ~(INP_HIGHPORT); 2017 inp->inp_flags |= INP_LOWPORT; 2018 break; 2019 2020 default: 2021 error = EINVAL; 2022 break; 2023 } 2024 INP_WUNLOCK(inp); 2025 break; 2026 2027 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 2028 case IPV6_IPSEC_POLICY: 2029 if (IPSEC_ENABLED(ipv6)) { 2030 error = IPSEC_PCBCTL(ipv6, inp, sopt); 2031 break; 2032 } 2033 /* FALLTHROUGH */ 2034 #endif /* IPSEC */ 2035 2036 default: 2037 error = ENOPROTOOPT; 2038 break; 2039 } 2040 break; 2041 2042 case SOPT_GET: 2043 switch (optname) { 2044 2045 case IPV6_2292PKTOPTIONS: 2046 #ifdef IPV6_PKTOPTIONS 2047 case IPV6_PKTOPTIONS: 2048 #endif 2049 /* 2050 * RFC3542 (effectively) deprecated the 2051 * semantics of the 2292-style pktoptions. 2052 * Since it was not reliable in nature (i.e., 2053 * applications had to expect the lack of some 2054 * information after all), it would make sense 2055 * to simplify this part by always returning 2056 * empty data. 2057 */ 2058 sopt->sopt_valsize = 0; 2059 break; 2060 2061 case IPV6_RECVHOPOPTS: 2062 case IPV6_RECVDSTOPTS: 2063 case IPV6_RECVRTHDRDSTOPTS: 2064 case IPV6_UNICAST_HOPS: 2065 case IPV6_RECVPKTINFO: 2066 case IPV6_RECVHOPLIMIT: 2067 case IPV6_RECVRTHDR: 2068 case IPV6_RECVPATHMTU: 2069 2070 case IPV6_V6ONLY: 2071 case IPV6_PORTRANGE: 2072 case IPV6_RECVTCLASS: 2073 case IPV6_AUTOFLOWLABEL: 2074 case IPV6_BINDANY: 2075 case IPV6_FLOWID: 2076 case IPV6_FLOWTYPE: 2077 case IPV6_RECVFLOWID: 2078 #ifdef RSS 2079 case IPV6_RSSBUCKETID: 2080 case IPV6_RECVRSSBUCKETID: 2081 #endif 2082 case IPV6_BINDMULTI: 2083 switch (optname) { 2084 2085 case IPV6_RECVHOPOPTS: 2086 optval = OPTBIT(IN6P_HOPOPTS); 2087 break; 2088 2089 case IPV6_RECVDSTOPTS: 2090 optval = OPTBIT(IN6P_DSTOPTS); 2091 break; 2092 2093 case IPV6_RECVRTHDRDSTOPTS: 2094 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 2095 break; 2096 2097 case IPV6_UNICAST_HOPS: 2098 optval = inp->in6p_hops; 2099 break; 2100 2101 case IPV6_RECVPKTINFO: 2102 optval = OPTBIT(IN6P_PKTINFO); 2103 break; 2104 2105 case IPV6_RECVHOPLIMIT: 2106 optval = OPTBIT(IN6P_HOPLIMIT); 2107 break; 2108 2109 case IPV6_RECVRTHDR: 2110 optval = OPTBIT(IN6P_RTHDR); 2111 break; 2112 2113 case IPV6_RECVPATHMTU: 2114 optval = OPTBIT(IN6P_MTU); 2115 break; 2116 2117 case IPV6_V6ONLY: 2118 optval = OPTBIT(IN6P_IPV6_V6ONLY); 2119 break; 2120 2121 case IPV6_PORTRANGE: 2122 { 2123 int flags; 2124 flags = inp->inp_flags; 2125 if (flags & INP_HIGHPORT) 2126 optval = IPV6_PORTRANGE_HIGH; 2127 else if (flags & INP_LOWPORT) 2128 optval = IPV6_PORTRANGE_LOW; 2129 else 2130 optval = 0; 2131 break; 2132 } 2133 case IPV6_RECVTCLASS: 2134 optval = OPTBIT(IN6P_TCLASS); 2135 break; 2136 2137 case IPV6_AUTOFLOWLABEL: 2138 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 2139 break; 2140 2141 case IPV6_ORIGDSTADDR: 2142 optval = OPTBIT2(INP_ORIGDSTADDR); 2143 break; 2144 2145 case IPV6_BINDANY: 2146 optval = OPTBIT(INP_BINDANY); 2147 break; 2148 2149 case IPV6_FLOWID: 2150 optval = inp->inp_flowid; 2151 break; 2152 2153 case IPV6_FLOWTYPE: 2154 optval = inp->inp_flowtype; 2155 break; 2156 2157 case IPV6_RECVFLOWID: 2158 optval = OPTBIT2(INP_RECVFLOWID); 2159 break; 2160 #ifdef RSS 2161 case IPV6_RSSBUCKETID: 2162 retval = 2163 rss_hash2bucket(inp->inp_flowid, 2164 inp->inp_flowtype, 2165 &rss_bucket); 2166 if (retval == 0) 2167 optval = rss_bucket; 2168 else 2169 error = EINVAL; 2170 break; 2171 2172 case IPV6_RECVRSSBUCKETID: 2173 optval = OPTBIT2(INP_RECVRSSBUCKETID); 2174 break; 2175 #endif 2176 2177 case IPV6_BINDMULTI: 2178 optval = OPTBIT2(INP_BINDMULTI); 2179 break; 2180 2181 } 2182 if (error) 2183 break; 2184 error = sooptcopyout(sopt, &optval, 2185 sizeof optval); 2186 break; 2187 2188 case IPV6_PATHMTU: 2189 { 2190 u_long pmtu = 0; 2191 struct ip6_mtuinfo mtuinfo; 2192 struct in6_addr addr; 2193 2194 if (!(so->so_state & SS_ISCONNECTED)) 2195 return (ENOTCONN); 2196 /* 2197 * XXX: we dot not consider the case of source 2198 * routing, or optional information to specify 2199 * the outgoing interface. 2200 * Copy faddr out of inp to avoid holding lock 2201 * on inp during route lookup. 2202 */ 2203 INP_RLOCK(inp); 2204 bcopy(&inp->in6p_faddr, &addr, sizeof(addr)); 2205 INP_RUNLOCK(inp); 2206 error = ip6_getpmtu_ctl(so->so_fibnum, 2207 &addr, &pmtu); 2208 if (error) 2209 break; 2210 if (pmtu > IPV6_MAXPACKET) 2211 pmtu = IPV6_MAXPACKET; 2212 2213 bzero(&mtuinfo, sizeof(mtuinfo)); 2214 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 2215 optdata = (void *)&mtuinfo; 2216 optdatalen = sizeof(mtuinfo); 2217 error = sooptcopyout(sopt, optdata, 2218 optdatalen); 2219 break; 2220 } 2221 2222 case IPV6_2292PKTINFO: 2223 case IPV6_2292HOPLIMIT: 2224 case IPV6_2292HOPOPTS: 2225 case IPV6_2292RTHDR: 2226 case IPV6_2292DSTOPTS: 2227 switch (optname) { 2228 case IPV6_2292PKTINFO: 2229 optval = OPTBIT(IN6P_PKTINFO); 2230 break; 2231 case IPV6_2292HOPLIMIT: 2232 optval = OPTBIT(IN6P_HOPLIMIT); 2233 break; 2234 case IPV6_2292HOPOPTS: 2235 optval = OPTBIT(IN6P_HOPOPTS); 2236 break; 2237 case IPV6_2292RTHDR: 2238 optval = OPTBIT(IN6P_RTHDR); 2239 break; 2240 case IPV6_2292DSTOPTS: 2241 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 2242 break; 2243 } 2244 error = sooptcopyout(sopt, &optval, 2245 sizeof optval); 2246 break; 2247 case IPV6_PKTINFO: 2248 case IPV6_HOPOPTS: 2249 case IPV6_RTHDR: 2250 case IPV6_DSTOPTS: 2251 case IPV6_RTHDRDSTOPTS: 2252 case IPV6_NEXTHOP: 2253 case IPV6_TCLASS: 2254 case IPV6_DONTFRAG: 2255 case IPV6_USE_MIN_MTU: 2256 case IPV6_PREFER_TEMPADDR: 2257 error = ip6_getpcbopt(inp, optname, sopt); 2258 break; 2259 2260 case IPV6_MULTICAST_IF: 2261 case IPV6_MULTICAST_HOPS: 2262 case IPV6_MULTICAST_LOOP: 2263 case IPV6_MSFILTER: 2264 error = ip6_getmoptions(inp, sopt); 2265 break; 2266 2267 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 2268 case IPV6_IPSEC_POLICY: 2269 if (IPSEC_ENABLED(ipv6)) { 2270 error = IPSEC_PCBCTL(ipv6, inp, sopt); 2271 break; 2272 } 2273 /* FALLTHROUGH */ 2274 #endif /* IPSEC */ 2275 default: 2276 error = ENOPROTOOPT; 2277 break; 2278 } 2279 break; 2280 } 2281 } 2282 return (error); 2283 } 2284 2285 int 2286 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt) 2287 { 2288 int error = 0, optval, optlen; 2289 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 2290 struct inpcb *inp = sotoinpcb(so); 2291 int level, op, optname; 2292 2293 level = sopt->sopt_level; 2294 op = sopt->sopt_dir; 2295 optname = sopt->sopt_name; 2296 optlen = sopt->sopt_valsize; 2297 2298 if (level != IPPROTO_IPV6) { 2299 return (EINVAL); 2300 } 2301 2302 switch (optname) { 2303 case IPV6_CHECKSUM: 2304 /* 2305 * For ICMPv6 sockets, no modification allowed for checksum 2306 * offset, permit "no change" values to help existing apps. 2307 * 2308 * RFC3542 says: "An attempt to set IPV6_CHECKSUM 2309 * for an ICMPv6 socket will fail." 2310 * The current behavior does not meet RFC3542. 2311 */ 2312 switch (op) { 2313 case SOPT_SET: 2314 if (optlen != sizeof(int)) { 2315 error = EINVAL; 2316 break; 2317 } 2318 error = sooptcopyin(sopt, &optval, sizeof(optval), 2319 sizeof(optval)); 2320 if (error) 2321 break; 2322 if (optval < -1 || (optval % 2) != 0) { 2323 /* 2324 * The API assumes non-negative even offset 2325 * values or -1 as a special value. 2326 */ 2327 error = EINVAL; 2328 } else if (so->so_proto->pr_protocol == 2329 IPPROTO_ICMPV6) { 2330 if (optval != icmp6off) 2331 error = EINVAL; 2332 } else 2333 inp->in6p_cksum = optval; 2334 break; 2335 2336 case SOPT_GET: 2337 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 2338 optval = icmp6off; 2339 else 2340 optval = inp->in6p_cksum; 2341 2342 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2343 break; 2344 2345 default: 2346 error = EINVAL; 2347 break; 2348 } 2349 break; 2350 2351 default: 2352 error = ENOPROTOOPT; 2353 break; 2354 } 2355 2356 return (error); 2357 } 2358 2359 /* 2360 * Set up IP6 options in pcb for insertion in output packets or 2361 * specifying behavior of outgoing packets. 2362 */ 2363 static int 2364 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, 2365 struct socket *so, struct sockopt *sopt) 2366 { 2367 struct ip6_pktopts *opt = *pktopt; 2368 int error = 0; 2369 struct thread *td = sopt->sopt_td; 2370 2371 /* turn off any old options. */ 2372 if (opt) { 2373 #ifdef DIAGNOSTIC 2374 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 2375 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 2376 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 2377 printf("ip6_pcbopts: all specified options are cleared.\n"); 2378 #endif 2379 ip6_clearpktopts(opt, -1); 2380 } else 2381 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 2382 *pktopt = NULL; 2383 2384 if (!m || m->m_len == 0) { 2385 /* 2386 * Only turning off any previous options, regardless of 2387 * whether the opt is just created or given. 2388 */ 2389 free(opt, M_IP6OPT); 2390 return (0); 2391 } 2392 2393 /* set options specified by user. */ 2394 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ? 2395 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) { 2396 ip6_clearpktopts(opt, -1); /* XXX: discard all options */ 2397 free(opt, M_IP6OPT); 2398 return (error); 2399 } 2400 *pktopt = opt; 2401 return (0); 2402 } 2403 2404 /* 2405 * initialize ip6_pktopts. beware that there are non-zero default values in 2406 * the struct. 2407 */ 2408 void 2409 ip6_initpktopts(struct ip6_pktopts *opt) 2410 { 2411 2412 bzero(opt, sizeof(*opt)); 2413 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 2414 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 2415 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 2416 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; 2417 } 2418 2419 static int 2420 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, 2421 struct ucred *cred, int uproto) 2422 { 2423 struct ip6_pktopts *opt; 2424 2425 if (*pktopt == NULL) { 2426 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 2427 M_NOWAIT); 2428 if (*pktopt == NULL) 2429 return (ENOBUFS); 2430 ip6_initpktopts(*pktopt); 2431 } 2432 opt = *pktopt; 2433 2434 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto)); 2435 } 2436 2437 #define GET_PKTOPT_VAR(field, lenexpr) do { \ 2438 if (pktopt && pktopt->field) { \ 2439 INP_RUNLOCK(inp); \ 2440 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \ 2441 malloc_optdata = true; \ 2442 INP_RLOCK(inp); \ 2443 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 2444 INP_RUNLOCK(inp); \ 2445 free(optdata, M_TEMP); \ 2446 return (ECONNRESET); \ 2447 } \ 2448 pktopt = inp->in6p_outputopts; \ 2449 if (pktopt && pktopt->field) { \ 2450 optdatalen = min(lenexpr, sopt->sopt_valsize); \ 2451 bcopy(&pktopt->field, optdata, optdatalen); \ 2452 } else { \ 2453 free(optdata, M_TEMP); \ 2454 optdata = NULL; \ 2455 malloc_optdata = false; \ 2456 } \ 2457 } \ 2458 } while(0) 2459 2460 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \ 2461 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3) 2462 2463 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \ 2464 pktopt->field->sa_len) 2465 2466 static int 2467 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt) 2468 { 2469 void *optdata = NULL; 2470 bool malloc_optdata = false; 2471 int optdatalen = 0; 2472 int error = 0; 2473 struct in6_pktinfo null_pktinfo; 2474 int deftclass = 0, on; 2475 int defminmtu = IP6PO_MINMTU_MCASTONLY; 2476 int defpreftemp = IP6PO_TEMPADDR_SYSTEM; 2477 struct ip6_pktopts *pktopt; 2478 2479 INP_RLOCK(inp); 2480 pktopt = inp->in6p_outputopts; 2481 2482 switch (optname) { 2483 case IPV6_PKTINFO: 2484 optdata = (void *)&null_pktinfo; 2485 if (pktopt && pktopt->ip6po_pktinfo) { 2486 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo, 2487 sizeof(null_pktinfo)); 2488 in6_clearscope(&null_pktinfo.ipi6_addr); 2489 } else { 2490 /* XXX: we don't have to do this every time... */ 2491 bzero(&null_pktinfo, sizeof(null_pktinfo)); 2492 } 2493 optdatalen = sizeof(struct in6_pktinfo); 2494 break; 2495 case IPV6_TCLASS: 2496 if (pktopt && pktopt->ip6po_tclass >= 0) 2497 deftclass = pktopt->ip6po_tclass; 2498 optdata = (void *)&deftclass; 2499 optdatalen = sizeof(int); 2500 break; 2501 case IPV6_HOPOPTS: 2502 GET_PKTOPT_EXT_HDR(ip6po_hbh); 2503 break; 2504 case IPV6_RTHDR: 2505 GET_PKTOPT_EXT_HDR(ip6po_rthdr); 2506 break; 2507 case IPV6_RTHDRDSTOPTS: 2508 GET_PKTOPT_EXT_HDR(ip6po_dest1); 2509 break; 2510 case IPV6_DSTOPTS: 2511 GET_PKTOPT_EXT_HDR(ip6po_dest2); 2512 break; 2513 case IPV6_NEXTHOP: 2514 GET_PKTOPT_SOCKADDR(ip6po_nexthop); 2515 break; 2516 case IPV6_USE_MIN_MTU: 2517 if (pktopt) 2518 defminmtu = pktopt->ip6po_minmtu; 2519 optdata = (void *)&defminmtu; 2520 optdatalen = sizeof(int); 2521 break; 2522 case IPV6_DONTFRAG: 2523 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 2524 on = 1; 2525 else 2526 on = 0; 2527 optdata = (void *)&on; 2528 optdatalen = sizeof(on); 2529 break; 2530 case IPV6_PREFER_TEMPADDR: 2531 if (pktopt) 2532 defpreftemp = pktopt->ip6po_prefer_tempaddr; 2533 optdata = (void *)&defpreftemp; 2534 optdatalen = sizeof(int); 2535 break; 2536 default: /* should not happen */ 2537 #ifdef DIAGNOSTIC 2538 panic("ip6_getpcbopt: unexpected option\n"); 2539 #endif 2540 INP_RUNLOCK(inp); 2541 return (ENOPROTOOPT); 2542 } 2543 INP_RUNLOCK(inp); 2544 2545 error = sooptcopyout(sopt, optdata, optdatalen); 2546 if (malloc_optdata) 2547 free(optdata, M_TEMP); 2548 2549 return (error); 2550 } 2551 2552 void 2553 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) 2554 { 2555 if (pktopt == NULL) 2556 return; 2557 2558 if (optname == -1 || optname == IPV6_PKTINFO) { 2559 if (pktopt->ip6po_pktinfo) 2560 free(pktopt->ip6po_pktinfo, M_IP6OPT); 2561 pktopt->ip6po_pktinfo = NULL; 2562 } 2563 if (optname == -1 || optname == IPV6_HOPLIMIT) 2564 pktopt->ip6po_hlim = -1; 2565 if (optname == -1 || optname == IPV6_TCLASS) 2566 pktopt->ip6po_tclass = -1; 2567 if (optname == -1 || optname == IPV6_NEXTHOP) { 2568 if (pktopt->ip6po_nextroute.ro_rt) { 2569 RTFREE(pktopt->ip6po_nextroute.ro_rt); 2570 pktopt->ip6po_nextroute.ro_rt = NULL; 2571 } 2572 if (pktopt->ip6po_nexthop) 2573 free(pktopt->ip6po_nexthop, M_IP6OPT); 2574 pktopt->ip6po_nexthop = NULL; 2575 } 2576 if (optname == -1 || optname == IPV6_HOPOPTS) { 2577 if (pktopt->ip6po_hbh) 2578 free(pktopt->ip6po_hbh, M_IP6OPT); 2579 pktopt->ip6po_hbh = NULL; 2580 } 2581 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 2582 if (pktopt->ip6po_dest1) 2583 free(pktopt->ip6po_dest1, M_IP6OPT); 2584 pktopt->ip6po_dest1 = NULL; 2585 } 2586 if (optname == -1 || optname == IPV6_RTHDR) { 2587 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 2588 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 2589 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 2590 if (pktopt->ip6po_route.ro_rt) { 2591 RTFREE(pktopt->ip6po_route.ro_rt); 2592 pktopt->ip6po_route.ro_rt = NULL; 2593 } 2594 } 2595 if (optname == -1 || optname == IPV6_DSTOPTS) { 2596 if (pktopt->ip6po_dest2) 2597 free(pktopt->ip6po_dest2, M_IP6OPT); 2598 pktopt->ip6po_dest2 = NULL; 2599 } 2600 } 2601 2602 #define PKTOPT_EXTHDRCPY(type) \ 2603 do {\ 2604 if (src->type) {\ 2605 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 2606 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 2607 if (dst->type == NULL)\ 2608 goto bad;\ 2609 bcopy(src->type, dst->type, hlen);\ 2610 }\ 2611 } while (/*CONSTCOND*/ 0) 2612 2613 static int 2614 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) 2615 { 2616 if (dst == NULL || src == NULL) { 2617 printf("ip6_clearpktopts: invalid argument\n"); 2618 return (EINVAL); 2619 } 2620 2621 dst->ip6po_hlim = src->ip6po_hlim; 2622 dst->ip6po_tclass = src->ip6po_tclass; 2623 dst->ip6po_flags = src->ip6po_flags; 2624 dst->ip6po_minmtu = src->ip6po_minmtu; 2625 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr; 2626 if (src->ip6po_pktinfo) { 2627 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2628 M_IP6OPT, canwait); 2629 if (dst->ip6po_pktinfo == NULL) 2630 goto bad; 2631 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2632 } 2633 if (src->ip6po_nexthop) { 2634 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2635 M_IP6OPT, canwait); 2636 if (dst->ip6po_nexthop == NULL) 2637 goto bad; 2638 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 2639 src->ip6po_nexthop->sa_len); 2640 } 2641 PKTOPT_EXTHDRCPY(ip6po_hbh); 2642 PKTOPT_EXTHDRCPY(ip6po_dest1); 2643 PKTOPT_EXTHDRCPY(ip6po_dest2); 2644 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2645 return (0); 2646 2647 bad: 2648 ip6_clearpktopts(dst, -1); 2649 return (ENOBUFS); 2650 } 2651 #undef PKTOPT_EXTHDRCPY 2652 2653 struct ip6_pktopts * 2654 ip6_copypktopts(struct ip6_pktopts *src, int canwait) 2655 { 2656 int error; 2657 struct ip6_pktopts *dst; 2658 2659 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2660 if (dst == NULL) 2661 return (NULL); 2662 ip6_initpktopts(dst); 2663 2664 if ((error = copypktopts(dst, src, canwait)) != 0) { 2665 free(dst, M_IP6OPT); 2666 return (NULL); 2667 } 2668 2669 return (dst); 2670 } 2671 2672 void 2673 ip6_freepcbopts(struct ip6_pktopts *pktopt) 2674 { 2675 if (pktopt == NULL) 2676 return; 2677 2678 ip6_clearpktopts(pktopt, -1); 2679 2680 free(pktopt, M_IP6OPT); 2681 } 2682 2683 /* 2684 * Set IPv6 outgoing packet options based on advanced API. 2685 */ 2686 int 2687 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2688 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto) 2689 { 2690 struct cmsghdr *cm = NULL; 2691 2692 if (control == NULL || opt == NULL) 2693 return (EINVAL); 2694 2695 ip6_initpktopts(opt); 2696 if (stickyopt) { 2697 int error; 2698 2699 /* 2700 * If stickyopt is provided, make a local copy of the options 2701 * for this particular packet, then override them by ancillary 2702 * objects. 2703 * XXX: copypktopts() does not copy the cached route to a next 2704 * hop (if any). This is not very good in terms of efficiency, 2705 * but we can allow this since this option should be rarely 2706 * used. 2707 */ 2708 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2709 return (error); 2710 } 2711 2712 /* 2713 * XXX: Currently, we assume all the optional information is stored 2714 * in a single mbuf. 2715 */ 2716 if (control->m_next) 2717 return (EINVAL); 2718 2719 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2720 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2721 int error; 2722 2723 if (control->m_len < CMSG_LEN(0)) 2724 return (EINVAL); 2725 2726 cm = mtod(control, struct cmsghdr *); 2727 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2728 return (EINVAL); 2729 if (cm->cmsg_level != IPPROTO_IPV6) 2730 continue; 2731 2732 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2733 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); 2734 if (error) 2735 return (error); 2736 } 2737 2738 return (0); 2739 } 2740 2741 /* 2742 * Set a particular packet option, as a sticky option or an ancillary data 2743 * item. "len" can be 0 only when it's a sticky option. 2744 * We have 4 cases of combination of "sticky" and "cmsg": 2745 * "sticky=0, cmsg=0": impossible 2746 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2747 * "sticky=1, cmsg=0": RFC3542 socket option 2748 * "sticky=1, cmsg=1": RFC2292 socket option 2749 */ 2750 static int 2751 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2752 struct ucred *cred, int sticky, int cmsg, int uproto) 2753 { 2754 int minmtupolicy, preftemp; 2755 int error; 2756 2757 if (!sticky && !cmsg) { 2758 #ifdef DIAGNOSTIC 2759 printf("ip6_setpktopt: impossible case\n"); 2760 #endif 2761 return (EINVAL); 2762 } 2763 2764 /* 2765 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2766 * not be specified in the context of RFC3542. Conversely, 2767 * RFC3542 types should not be specified in the context of RFC2292. 2768 */ 2769 if (!cmsg) { 2770 switch (optname) { 2771 case IPV6_2292PKTINFO: 2772 case IPV6_2292HOPLIMIT: 2773 case IPV6_2292NEXTHOP: 2774 case IPV6_2292HOPOPTS: 2775 case IPV6_2292DSTOPTS: 2776 case IPV6_2292RTHDR: 2777 case IPV6_2292PKTOPTIONS: 2778 return (ENOPROTOOPT); 2779 } 2780 } 2781 if (sticky && cmsg) { 2782 switch (optname) { 2783 case IPV6_PKTINFO: 2784 case IPV6_HOPLIMIT: 2785 case IPV6_NEXTHOP: 2786 case IPV6_HOPOPTS: 2787 case IPV6_DSTOPTS: 2788 case IPV6_RTHDRDSTOPTS: 2789 case IPV6_RTHDR: 2790 case IPV6_USE_MIN_MTU: 2791 case IPV6_DONTFRAG: 2792 case IPV6_TCLASS: 2793 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */ 2794 return (ENOPROTOOPT); 2795 } 2796 } 2797 2798 switch (optname) { 2799 case IPV6_2292PKTINFO: 2800 case IPV6_PKTINFO: 2801 { 2802 struct ifnet *ifp = NULL; 2803 struct in6_pktinfo *pktinfo; 2804 2805 if (len != sizeof(struct in6_pktinfo)) 2806 return (EINVAL); 2807 2808 pktinfo = (struct in6_pktinfo *)buf; 2809 2810 /* 2811 * An application can clear any sticky IPV6_PKTINFO option by 2812 * doing a "regular" setsockopt with ipi6_addr being 2813 * in6addr_any and ipi6_ifindex being zero. 2814 * [RFC 3542, Section 6] 2815 */ 2816 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2817 pktinfo->ipi6_ifindex == 0 && 2818 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2819 ip6_clearpktopts(opt, optname); 2820 break; 2821 } 2822 2823 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2824 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2825 return (EINVAL); 2826 } 2827 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr)) 2828 return (EINVAL); 2829 /* validate the interface index if specified. */ 2830 if (pktinfo->ipi6_ifindex > V_if_index) 2831 return (ENXIO); 2832 if (pktinfo->ipi6_ifindex) { 2833 ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 2834 if (ifp == NULL) 2835 return (ENXIO); 2836 } 2837 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL || 2838 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0)) 2839 return (ENETDOWN); 2840 2841 if (ifp != NULL && 2842 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2843 struct in6_ifaddr *ia; 2844 2845 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL); 2846 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr); 2847 if (ia == NULL) 2848 return (EADDRNOTAVAIL); 2849 ifa_free(&ia->ia_ifa); 2850 } 2851 /* 2852 * We store the address anyway, and let in6_selectsrc() 2853 * validate the specified address. This is because ipi6_addr 2854 * may not have enough information about its scope zone, and 2855 * we may need additional information (such as outgoing 2856 * interface or the scope zone of a destination address) to 2857 * disambiguate the scope. 2858 * XXX: the delay of the validation may confuse the 2859 * application when it is used as a sticky option. 2860 */ 2861 if (opt->ip6po_pktinfo == NULL) { 2862 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2863 M_IP6OPT, M_NOWAIT); 2864 if (opt->ip6po_pktinfo == NULL) 2865 return (ENOBUFS); 2866 } 2867 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 2868 break; 2869 } 2870 2871 case IPV6_2292HOPLIMIT: 2872 case IPV6_HOPLIMIT: 2873 { 2874 int *hlimp; 2875 2876 /* 2877 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2878 * to simplify the ordering among hoplimit options. 2879 */ 2880 if (optname == IPV6_HOPLIMIT && sticky) 2881 return (ENOPROTOOPT); 2882 2883 if (len != sizeof(int)) 2884 return (EINVAL); 2885 hlimp = (int *)buf; 2886 if (*hlimp < -1 || *hlimp > 255) 2887 return (EINVAL); 2888 2889 opt->ip6po_hlim = *hlimp; 2890 break; 2891 } 2892 2893 case IPV6_TCLASS: 2894 { 2895 int tclass; 2896 2897 if (len != sizeof(int)) 2898 return (EINVAL); 2899 tclass = *(int *)buf; 2900 if (tclass < -1 || tclass > 255) 2901 return (EINVAL); 2902 2903 opt->ip6po_tclass = tclass; 2904 break; 2905 } 2906 2907 case IPV6_2292NEXTHOP: 2908 case IPV6_NEXTHOP: 2909 if (cred != NULL) { 2910 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS); 2911 if (error) 2912 return (error); 2913 } 2914 2915 if (len == 0) { /* just remove the option */ 2916 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2917 break; 2918 } 2919 2920 /* check if cmsg_len is large enough for sa_len */ 2921 if (len < sizeof(struct sockaddr) || len < *buf) 2922 return (EINVAL); 2923 2924 switch (((struct sockaddr *)buf)->sa_family) { 2925 case AF_INET6: 2926 { 2927 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 2928 int error; 2929 2930 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 2931 return (EINVAL); 2932 2933 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 2934 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 2935 return (EINVAL); 2936 } 2937 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone)) 2938 != 0) { 2939 return (error); 2940 } 2941 break; 2942 } 2943 case AF_LINK: /* should eventually be supported */ 2944 default: 2945 return (EAFNOSUPPORT); 2946 } 2947 2948 /* turn off the previous option, then set the new option. */ 2949 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2950 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 2951 if (opt->ip6po_nexthop == NULL) 2952 return (ENOBUFS); 2953 bcopy(buf, opt->ip6po_nexthop, *buf); 2954 break; 2955 2956 case IPV6_2292HOPOPTS: 2957 case IPV6_HOPOPTS: 2958 { 2959 struct ip6_hbh *hbh; 2960 int hbhlen; 2961 2962 /* 2963 * XXX: We don't allow a non-privileged user to set ANY HbH 2964 * options, since per-option restriction has too much 2965 * overhead. 2966 */ 2967 if (cred != NULL) { 2968 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS); 2969 if (error) 2970 return (error); 2971 } 2972 2973 if (len == 0) { 2974 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2975 break; /* just remove the option */ 2976 } 2977 2978 /* message length validation */ 2979 if (len < sizeof(struct ip6_hbh)) 2980 return (EINVAL); 2981 hbh = (struct ip6_hbh *)buf; 2982 hbhlen = (hbh->ip6h_len + 1) << 3; 2983 if (len != hbhlen) 2984 return (EINVAL); 2985 2986 /* turn off the previous option, then set the new option. */ 2987 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2988 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 2989 if (opt->ip6po_hbh == NULL) 2990 return (ENOBUFS); 2991 bcopy(hbh, opt->ip6po_hbh, hbhlen); 2992 2993 break; 2994 } 2995 2996 case IPV6_2292DSTOPTS: 2997 case IPV6_DSTOPTS: 2998 case IPV6_RTHDRDSTOPTS: 2999 { 3000 struct ip6_dest *dest, **newdest = NULL; 3001 int destlen; 3002 3003 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */ 3004 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS); 3005 if (error) 3006 return (error); 3007 } 3008 3009 if (len == 0) { 3010 ip6_clearpktopts(opt, optname); 3011 break; /* just remove the option */ 3012 } 3013 3014 /* message length validation */ 3015 if (len < sizeof(struct ip6_dest)) 3016 return (EINVAL); 3017 dest = (struct ip6_dest *)buf; 3018 destlen = (dest->ip6d_len + 1) << 3; 3019 if (len != destlen) 3020 return (EINVAL); 3021 3022 /* 3023 * Determine the position that the destination options header 3024 * should be inserted; before or after the routing header. 3025 */ 3026 switch (optname) { 3027 case IPV6_2292DSTOPTS: 3028 /* 3029 * The old advacned API is ambiguous on this point. 3030 * Our approach is to determine the position based 3031 * according to the existence of a routing header. 3032 * Note, however, that this depends on the order of the 3033 * extension headers in the ancillary data; the 1st 3034 * part of the destination options header must appear 3035 * before the routing header in the ancillary data, 3036 * too. 3037 * RFC3542 solved the ambiguity by introducing 3038 * separate ancillary data or option types. 3039 */ 3040 if (opt->ip6po_rthdr == NULL) 3041 newdest = &opt->ip6po_dest1; 3042 else 3043 newdest = &opt->ip6po_dest2; 3044 break; 3045 case IPV6_RTHDRDSTOPTS: 3046 newdest = &opt->ip6po_dest1; 3047 break; 3048 case IPV6_DSTOPTS: 3049 newdest = &opt->ip6po_dest2; 3050 break; 3051 } 3052 3053 /* turn off the previous option, then set the new option. */ 3054 ip6_clearpktopts(opt, optname); 3055 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 3056 if (*newdest == NULL) 3057 return (ENOBUFS); 3058 bcopy(dest, *newdest, destlen); 3059 3060 break; 3061 } 3062 3063 case IPV6_2292RTHDR: 3064 case IPV6_RTHDR: 3065 { 3066 struct ip6_rthdr *rth; 3067 int rthlen; 3068 3069 if (len == 0) { 3070 ip6_clearpktopts(opt, IPV6_RTHDR); 3071 break; /* just remove the option */ 3072 } 3073 3074 /* message length validation */ 3075 if (len < sizeof(struct ip6_rthdr)) 3076 return (EINVAL); 3077 rth = (struct ip6_rthdr *)buf; 3078 rthlen = (rth->ip6r_len + 1) << 3; 3079 if (len != rthlen) 3080 return (EINVAL); 3081 3082 switch (rth->ip6r_type) { 3083 case IPV6_RTHDR_TYPE_0: 3084 if (rth->ip6r_len == 0) /* must contain one addr */ 3085 return (EINVAL); 3086 if (rth->ip6r_len % 2) /* length must be even */ 3087 return (EINVAL); 3088 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 3089 return (EINVAL); 3090 break; 3091 default: 3092 return (EINVAL); /* not supported */ 3093 } 3094 3095 /* turn off the previous option */ 3096 ip6_clearpktopts(opt, IPV6_RTHDR); 3097 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 3098 if (opt->ip6po_rthdr == NULL) 3099 return (ENOBUFS); 3100 bcopy(rth, opt->ip6po_rthdr, rthlen); 3101 3102 break; 3103 } 3104 3105 case IPV6_USE_MIN_MTU: 3106 if (len != sizeof(int)) 3107 return (EINVAL); 3108 minmtupolicy = *(int *)buf; 3109 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 3110 minmtupolicy != IP6PO_MINMTU_DISABLE && 3111 minmtupolicy != IP6PO_MINMTU_ALL) { 3112 return (EINVAL); 3113 } 3114 opt->ip6po_minmtu = minmtupolicy; 3115 break; 3116 3117 case IPV6_DONTFRAG: 3118 if (len != sizeof(int)) 3119 return (EINVAL); 3120 3121 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 3122 /* 3123 * we ignore this option for TCP sockets. 3124 * (RFC3542 leaves this case unspecified.) 3125 */ 3126 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 3127 } else 3128 opt->ip6po_flags |= IP6PO_DONTFRAG; 3129 break; 3130 3131 case IPV6_PREFER_TEMPADDR: 3132 if (len != sizeof(int)) 3133 return (EINVAL); 3134 preftemp = *(int *)buf; 3135 if (preftemp != IP6PO_TEMPADDR_SYSTEM && 3136 preftemp != IP6PO_TEMPADDR_NOTPREFER && 3137 preftemp != IP6PO_TEMPADDR_PREFER) { 3138 return (EINVAL); 3139 } 3140 opt->ip6po_prefer_tempaddr = preftemp; 3141 break; 3142 3143 default: 3144 return (ENOPROTOOPT); 3145 } /* end of switch */ 3146 3147 return (0); 3148 } 3149 3150 /* 3151 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 3152 * packet to the input queue of a specified interface. Note that this 3153 * calls the output routine of the loopback "driver", but with an interface 3154 * pointer that might NOT be &loif -- easier than replicating that code here. 3155 */ 3156 void 3157 ip6_mloopback(struct ifnet *ifp, struct mbuf *m) 3158 { 3159 struct mbuf *copym; 3160 struct ip6_hdr *ip6; 3161 3162 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT); 3163 if (copym == NULL) 3164 return; 3165 3166 /* 3167 * Make sure to deep-copy IPv6 header portion in case the data 3168 * is in an mbuf cluster, so that we can safely override the IPv6 3169 * header portion later. 3170 */ 3171 if (!M_WRITABLE(copym) || 3172 copym->m_len < sizeof(struct ip6_hdr)) { 3173 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 3174 if (copym == NULL) 3175 return; 3176 } 3177 ip6 = mtod(copym, struct ip6_hdr *); 3178 /* 3179 * clear embedded scope identifiers if necessary. 3180 * in6_clearscope will touch the addresses only when necessary. 3181 */ 3182 in6_clearscope(&ip6->ip6_src); 3183 in6_clearscope(&ip6->ip6_dst); 3184 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 3185 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 | 3186 CSUM_PSEUDO_HDR; 3187 copym->m_pkthdr.csum_data = 0xffff; 3188 } 3189 if_simloop(ifp, copym, AF_INET6, 0); 3190 } 3191 3192 /* 3193 * Chop IPv6 header off from the payload. 3194 */ 3195 static int 3196 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 3197 { 3198 struct mbuf *mh; 3199 struct ip6_hdr *ip6; 3200 3201 ip6 = mtod(m, struct ip6_hdr *); 3202 if (m->m_len > sizeof(*ip6)) { 3203 mh = m_gethdr(M_NOWAIT, MT_DATA); 3204 if (mh == NULL) { 3205 m_freem(m); 3206 return ENOBUFS; 3207 } 3208 m_move_pkthdr(mh, m); 3209 M_ALIGN(mh, sizeof(*ip6)); 3210 m->m_len -= sizeof(*ip6); 3211 m->m_data += sizeof(*ip6); 3212 mh->m_next = m; 3213 m = mh; 3214 m->m_len = sizeof(*ip6); 3215 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 3216 } 3217 exthdrs->ip6e_ip6 = m; 3218 return 0; 3219 } 3220 3221 /* 3222 * Compute IPv6 extension header length. 3223 */ 3224 int 3225 ip6_optlen(struct inpcb *inp) 3226 { 3227 int len; 3228 3229 if (!inp->in6p_outputopts) 3230 return 0; 3231 3232 len = 0; 3233 #define elen(x) \ 3234 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 3235 3236 len += elen(inp->in6p_outputopts->ip6po_hbh); 3237 if (inp->in6p_outputopts->ip6po_rthdr) 3238 /* dest1 is valid with rthdr only */ 3239 len += elen(inp->in6p_outputopts->ip6po_dest1); 3240 len += elen(inp->in6p_outputopts->ip6po_rthdr); 3241 len += elen(inp->in6p_outputopts->ip6po_dest2); 3242 return len; 3243 #undef elen 3244 } 3245