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