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