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