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