1 /* $NetBSD: ip6_output.c,v 1.229 2021/09/21 15:07:43 christos Exp $ */ 2 /* $KAME: ip6_output.c,v 1.172 2001/03/25 09:55:56 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988, 1990, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: ip6_output.c,v 1.229 2021/09/21 15:07:43 christos Exp $"); 66 67 #ifdef _KERNEL_OPT 68 #include "opt_inet.h" 69 #include "opt_inet6.h" 70 #include "opt_ipsec.h" 71 #endif 72 73 #include <sys/param.h> 74 #include <sys/malloc.h> 75 #include <sys/mbuf.h> 76 #include <sys/errno.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/syslog.h> 80 #include <sys/systm.h> 81 #include <sys/proc.h> 82 #include <sys/kauth.h> 83 84 #include <net/if.h> 85 #include <net/route.h> 86 #include <net/pfil.h> 87 88 #include <netinet/in.h> 89 #include <netinet/in_var.h> 90 #include <netinet/ip6.h> 91 #include <netinet/ip_var.h> 92 #include <netinet/icmp6.h> 93 #include <netinet/in_offload.h> 94 #include <netinet/portalgo.h> 95 #include <netinet6/in6_offload.h> 96 #include <netinet6/ip6_var.h> 97 #include <netinet6/ip6_private.h> 98 #include <netinet6/in6_pcb.h> 99 #include <netinet6/nd6.h> 100 #include <netinet6/ip6protosw.h> 101 #include <netinet6/scope6_var.h> 102 103 #ifdef IPSEC 104 #include <netipsec/ipsec.h> 105 #include <netipsec/ipsec6.h> 106 #include <netipsec/key.h> 107 #endif 108 109 extern pfil_head_t *inet6_pfil_hook; /* XXX */ 110 111 struct ip6_exthdrs { 112 struct mbuf *ip6e_ip6; 113 struct mbuf *ip6e_hbh; 114 struct mbuf *ip6e_dest1; 115 struct mbuf *ip6e_rthdr; 116 struct mbuf *ip6e_dest2; 117 }; 118 119 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **, 120 kauth_cred_t, int); 121 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *); 122 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, kauth_cred_t, 123 int, int, int); 124 static int ip6_setmoptions(const struct sockopt *, struct in6pcb *); 125 static int ip6_getmoptions(struct sockopt *, struct in6pcb *); 126 static int ip6_copyexthdr(struct mbuf **, void *, int); 127 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int, 128 struct ip6_frag **); 129 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t); 130 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *); 131 static int ip6_getpmtu(struct rtentry *, struct ifnet *, u_long *, int *); 132 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int); 133 static int ip6_ifaddrvalid(const struct in6_addr *, const struct in6_addr *); 134 static int ip6_handle_rthdr(struct ip6_rthdr *, struct ip6_hdr *); 135 136 #ifdef RFC2292 137 static int ip6_pcbopts(struct ip6_pktopts **, struct socket *, struct sockopt *); 138 #endif 139 140 static int 141 ip6_handle_rthdr(struct ip6_rthdr *rh, struct ip6_hdr *ip6) 142 { 143 int error = 0; 144 145 switch (rh->ip6r_type) { 146 case IPV6_RTHDR_TYPE_0: 147 /* Dropped, RFC5095. */ 148 default: /* is it possible? */ 149 error = EINVAL; 150 } 151 152 return error; 153 } 154 155 /* 156 * Send an IP packet to a host. 157 */ 158 int 159 ip6_if_output(struct ifnet * const ifp, struct ifnet * const origifp, 160 struct mbuf * const m, const struct sockaddr_in6 * const dst, 161 const struct rtentry *rt) 162 { 163 int error = 0; 164 165 if (rt != NULL) { 166 error = rt_check_reject_route(rt, ifp); 167 if (error != 0) { 168 IP6_STATINC(IP6_STAT_RTREJECT); 169 m_freem(m); 170 return error; 171 } 172 } 173 174 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 175 error = if_output_lock(ifp, origifp, m, sin6tocsa(dst), rt); 176 else 177 error = if_output_lock(ifp, ifp, m, sin6tocsa(dst), rt); 178 return error; 179 } 180 181 /* 182 * IP6 output. The packet in mbuf chain m contains a skeletal IP6 183 * header (with pri, len, nxt, hlim, src, dst). 184 * 185 * This function may modify ver and hlim only. The mbuf chain containing the 186 * packet will be freed. The mbuf opt, if present, will not be freed. 187 * 188 * Type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and 189 * nd_ifinfo.linkmtu is u_int32_t. So we use u_long to hold largest one, 190 * which is rt_rmx.rmx_mtu. 191 */ 192 int 193 ip6_output( 194 struct mbuf *m0, 195 struct ip6_pktopts *opt, 196 struct route *ro, 197 int flags, 198 struct ip6_moptions *im6o, 199 struct in6pcb *in6p, 200 struct ifnet **ifpp /* XXX: just for statistics */ 201 ) 202 { 203 struct ip6_hdr *ip6, *mhip6; 204 struct ifnet *ifp = NULL, *origifp = NULL; 205 struct mbuf *m = m0; 206 int tlen, len, off; 207 bool tso; 208 struct route ip6route; 209 struct rtentry *rt = NULL, *rt_pmtu; 210 const struct sockaddr_in6 *dst; 211 struct sockaddr_in6 src_sa, dst_sa; 212 int error = 0; 213 struct in6_ifaddr *ia = NULL; 214 u_long mtu; 215 int alwaysfrag, dontfrag; 216 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; 217 struct ip6_exthdrs exthdrs; 218 struct in6_addr finaldst, src0, dst0; 219 u_int32_t zone; 220 struct route *ro_pmtu = NULL; 221 int hdrsplit = 0; 222 int needipsec = 0; 223 #ifdef IPSEC 224 struct secpolicy *sp = NULL; 225 #endif 226 struct psref psref, psref_ia; 227 int bound = curlwp_bind(); 228 bool release_psref_ia = false; 229 230 #ifdef DIAGNOSTIC 231 if ((m->m_flags & M_PKTHDR) == 0) 232 panic("ip6_output: no HDR"); 233 if ((m->m_pkthdr.csum_flags & 234 (M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_TSOv4)) != 0) { 235 panic("ip6_output: IPv4 checksum offload flags: %d", 236 m->m_pkthdr.csum_flags); 237 } 238 if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 239 (M_CSUM_TCPv6|M_CSUM_UDPv6)) { 240 panic("ip6_output: conflicting checksum offload flags: %d", 241 m->m_pkthdr.csum_flags); 242 } 243 #endif 244 245 M_CSUM_DATA_IPv6_SET(m->m_pkthdr.csum_data, sizeof(struct ip6_hdr)); 246 247 #define MAKE_EXTHDR(hp, mp) \ 248 do { \ 249 if (hp) { \ 250 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 251 error = ip6_copyexthdr((mp), (void *)(hp), \ 252 ((eh)->ip6e_len + 1) << 3); \ 253 if (error) \ 254 goto freehdrs; \ 255 } \ 256 } while (/*CONSTCOND*/ 0) 257 258 memset(&exthdrs, 0, sizeof(exthdrs)); 259 if (opt) { 260 /* Hop-by-Hop options header */ 261 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 262 /* Destination options header (1st part) */ 263 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 264 /* Routing header */ 265 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 266 /* Destination options header (2nd part) */ 267 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 268 } 269 270 /* 271 * Calculate the total length of the extension header chain. 272 * Keep the length of the unfragmentable part for fragmentation. 273 */ 274 optlen = 0; 275 if (exthdrs.ip6e_hbh) 276 optlen += exthdrs.ip6e_hbh->m_len; 277 if (exthdrs.ip6e_dest1) 278 optlen += exthdrs.ip6e_dest1->m_len; 279 if (exthdrs.ip6e_rthdr) 280 optlen += exthdrs.ip6e_rthdr->m_len; 281 unfragpartlen = optlen + sizeof(struct ip6_hdr); 282 /* NOTE: we don't add AH/ESP length here. do that later. */ 283 if (exthdrs.ip6e_dest2) 284 optlen += exthdrs.ip6e_dest2->m_len; 285 286 #ifdef IPSEC 287 if (ipsec_used) { 288 /* Check the security policy (SP) for the packet */ 289 sp = ipsec6_check_policy(m, in6p, flags, &needipsec, &error); 290 if (error != 0) { 291 /* 292 * Hack: -EINVAL is used to signal that a packet 293 * should be silently discarded. This is typically 294 * because we asked key management for an SA and 295 * it was delayed (e.g. kicked up to IKE). 296 */ 297 if (error == -EINVAL) 298 error = 0; 299 IP6_STATINC(IP6_STAT_IPSECDROP_OUT); 300 goto freehdrs; 301 } 302 } 303 #endif 304 305 if (needipsec && 306 (m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) { 307 in6_undefer_cksum_tcpudp(m); 308 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6); 309 } 310 311 /* 312 * If we need IPsec, or there is at least one extension header, 313 * separate IP6 header from the payload. 314 */ 315 if ((needipsec || optlen) && !hdrsplit) { 316 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 317 IP6_STATINC(IP6_STAT_ODROPPED); 318 m = NULL; 319 goto freehdrs; 320 } 321 m = exthdrs.ip6e_ip6; 322 hdrsplit++; 323 } 324 325 /* adjust pointer */ 326 ip6 = mtod(m, struct ip6_hdr *); 327 328 /* adjust mbuf packet header length */ 329 m->m_pkthdr.len += optlen; 330 plen = m->m_pkthdr.len - sizeof(*ip6); 331 332 /* If this is a jumbo payload, insert a jumbo payload option. */ 333 if (plen > IPV6_MAXPACKET) { 334 if (!hdrsplit) { 335 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 336 IP6_STATINC(IP6_STAT_ODROPPED); 337 m = NULL; 338 goto freehdrs; 339 } 340 m = exthdrs.ip6e_ip6; 341 hdrsplit++; 342 } 343 /* adjust pointer */ 344 ip6 = mtod(m, struct ip6_hdr *); 345 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) { 346 IP6_STATINC(IP6_STAT_ODROPPED); 347 goto freehdrs; 348 } 349 optlen += 8; /* XXX JUMBOOPTLEN */ 350 ip6->ip6_plen = 0; 351 } else 352 ip6->ip6_plen = htons(plen); 353 354 /* 355 * Concatenate headers and fill in next header fields. 356 * Here we have, on "m" 357 * IPv6 payload 358 * and we insert headers accordingly. Finally, we should be getting: 359 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 360 * 361 * during the header composing process, "m" points to IPv6 header. 362 * "mprev" points to an extension header prior to esp. 363 */ 364 { 365 u_char *nexthdrp = &ip6->ip6_nxt; 366 struct mbuf *mprev = m; 367 368 /* 369 * we treat dest2 specially. this makes IPsec processing 370 * much easier. the goal here is to make mprev point the 371 * mbuf prior to dest2. 372 * 373 * result: IPv6 dest2 payload 374 * m and mprev will point to IPv6 header. 375 */ 376 if (exthdrs.ip6e_dest2) { 377 if (!hdrsplit) 378 panic("assumption failed: hdr not split"); 379 exthdrs.ip6e_dest2->m_next = m->m_next; 380 m->m_next = exthdrs.ip6e_dest2; 381 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 382 ip6->ip6_nxt = IPPROTO_DSTOPTS; 383 } 384 385 #define MAKE_CHAIN(m, mp, p, i)\ 386 do {\ 387 if (m) {\ 388 if (!hdrsplit) \ 389 panic("assumption failed: hdr not split"); \ 390 *mtod((m), u_char *) = *(p);\ 391 *(p) = (i);\ 392 p = mtod((m), u_char *);\ 393 (m)->m_next = (mp)->m_next;\ 394 (mp)->m_next = (m);\ 395 (mp) = (m);\ 396 }\ 397 } while (/*CONSTCOND*/ 0) 398 /* 399 * result: IPv6 hbh dest1 rthdr dest2 payload 400 * m will point to IPv6 header. mprev will point to the 401 * extension header prior to dest2 (rthdr in the above case). 402 */ 403 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 404 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 405 IPPROTO_DSTOPTS); 406 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 407 IPPROTO_ROUTING); 408 409 M_CSUM_DATA_IPv6_SET(m->m_pkthdr.csum_data, 410 sizeof(struct ip6_hdr) + optlen); 411 } 412 413 /* Need to save for pmtu */ 414 finaldst = ip6->ip6_dst; 415 416 /* 417 * If there is a routing header, replace destination address field 418 * with the first hop of the routing header. 419 */ 420 if (exthdrs.ip6e_rthdr) { 421 struct ip6_rthdr *rh; 422 423 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 424 425 error = ip6_handle_rthdr(rh, ip6); 426 if (error != 0) { 427 IP6_STATINC(IP6_STAT_ODROPPED); 428 goto bad; 429 } 430 } 431 432 /* Source address validation */ 433 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 434 (flags & IPV6_UNSPECSRC) == 0) { 435 error = EOPNOTSUPP; 436 IP6_STATINC(IP6_STAT_BADSCOPE); 437 goto bad; 438 } 439 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 440 error = EOPNOTSUPP; 441 IP6_STATINC(IP6_STAT_BADSCOPE); 442 goto bad; 443 } 444 445 IP6_STATINC(IP6_STAT_LOCALOUT); 446 447 /* 448 * Route packet. 449 */ 450 /* initialize cached route */ 451 if (ro == NULL) { 452 memset(&ip6route, 0, sizeof(ip6route)); 453 ro = &ip6route; 454 } 455 ro_pmtu = ro; 456 if (opt && opt->ip6po_rthdr) 457 ro = &opt->ip6po_route; 458 459 /* 460 * if specified, try to fill in the traffic class field. 461 * do not override if a non-zero value is already set. 462 * we check the diffserv field and the ecn field separately. 463 */ 464 if (opt && opt->ip6po_tclass >= 0) { 465 int mask = 0; 466 467 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0) 468 mask |= 0xfc; 469 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0) 470 mask |= 0x03; 471 if (mask != 0) 472 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20); 473 } 474 475 /* fill in or override the hop limit field, if necessary. */ 476 if (opt && opt->ip6po_hlim != -1) 477 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 478 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 479 if (im6o != NULL) 480 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 481 else 482 ip6->ip6_hlim = ip6_defmcasthlim; 483 } 484 485 #ifdef IPSEC 486 if (needipsec) { 487 int s = splsoftnet(); 488 error = ipsec6_process_packet(m, sp->req, flags); 489 splx(s); 490 491 /* 492 * Preserve KAME behaviour: ENOENT can be returned 493 * when an SA acquire is in progress. Don't propagate 494 * this to user-level; it confuses applications. 495 * XXX this will go away when the SADB is redone. 496 */ 497 if (error == ENOENT) 498 error = 0; 499 500 goto done; 501 } 502 #endif 503 504 /* adjust pointer */ 505 ip6 = mtod(m, struct ip6_hdr *); 506 507 sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0); 508 509 /* We do not need a route for multicast */ 510 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 511 struct in6_pktinfo *pi = NULL; 512 513 /* 514 * If the outgoing interface for the address is specified by 515 * the caller, use it. 516 */ 517 if (opt && (pi = opt->ip6po_pktinfo) != NULL) { 518 /* XXX boundary check is assumed to be already done. */ 519 ifp = if_get_byindex(pi->ipi6_ifindex, &psref); 520 } else if (im6o != NULL) { 521 ifp = if_get_byindex(im6o->im6o_multicast_if_index, 522 &psref); 523 } 524 } 525 526 if (ifp == NULL) { 527 error = in6_selectroute(&dst_sa, opt, &ro, &rt, true); 528 if (error != 0) 529 goto bad; 530 ifp = if_get_byindex(rt->rt_ifp->if_index, &psref); 531 } 532 533 if (rt == NULL) { 534 /* 535 * If in6_selectroute() does not return a route entry, 536 * dst may not have been updated. 537 */ 538 error = rtcache_setdst(ro, sin6tosa(&dst_sa)); 539 if (error) { 540 IP6_STATINC(IP6_STAT_ODROPPED); 541 goto bad; 542 } 543 } 544 545 /* 546 * then rt (for unicast) and ifp must be non-NULL valid values. 547 */ 548 if ((flags & IPV6_FORWARDING) == 0) { 549 /* XXX: the FORWARDING flag can be set for mrouting. */ 550 in6_ifstat_inc(ifp, ifs6_out_request); 551 } 552 if (rt != NULL) { 553 ia = (struct in6_ifaddr *)(rt->rt_ifa); 554 rt->rt_use++; 555 } 556 557 /* 558 * The outgoing interface must be in the zone of source and 559 * destination addresses. We should use ia_ifp to support the 560 * case of sending packets to an address of our own. 561 */ 562 if (ia != NULL) { 563 origifp = ia->ia_ifp; 564 if (if_is_deactivated(origifp)) { 565 IP6_STATINC(IP6_STAT_ODROPPED); 566 goto bad; 567 } 568 if_acquire(origifp, &psref_ia); 569 release_psref_ia = true; 570 } else 571 origifp = ifp; 572 573 src0 = ip6->ip6_src; 574 if (in6_setscope(&src0, origifp, &zone)) 575 goto badscope; 576 sockaddr_in6_init(&src_sa, &ip6->ip6_src, 0, 0, 0); 577 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id) 578 goto badscope; 579 580 dst0 = ip6->ip6_dst; 581 if (in6_setscope(&dst0, origifp, &zone)) 582 goto badscope; 583 /* re-initialize to be sure */ 584 sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0); 585 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) 586 goto badscope; 587 588 /* scope check is done. */ 589 590 /* Ensure we only send from a valid address. */ 591 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 592 (flags & IPV6_FORWARDING) == 0 && 593 (error = ip6_ifaddrvalid(&src0, &dst0)) != 0) 594 { 595 char ip6buf[INET6_ADDRSTRLEN]; 596 nd6log(LOG_ERR, 597 "refusing to send from invalid address %s (pid %d)\n", 598 IN6_PRINT(ip6buf, &src0), curproc->p_pid); 599 IP6_STATINC(IP6_STAT_ODROPPED); 600 in6_ifstat_inc(origifp, ifs6_out_discard); 601 if (error == 1) 602 /* 603 * Address exists, but is tentative or detached. 604 * We can't send from it because it's invalid, 605 * so we drop the packet. 606 */ 607 error = 0; 608 else 609 error = EADDRNOTAVAIL; 610 goto bad; 611 } 612 613 if (rt != NULL && (rt->rt_flags & RTF_GATEWAY) && 614 !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 615 dst = satocsin6(rt->rt_gateway); 616 else 617 dst = satocsin6(rtcache_getdst(ro)); 618 619 /* 620 * XXXXXX: original code follows: 621 */ 622 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 623 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 624 else { 625 bool ingroup; 626 627 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 628 629 in6_ifstat_inc(ifp, ifs6_out_mcast); 630 631 /* 632 * Confirm that the outgoing interface supports multicast. 633 */ 634 if (!(ifp->if_flags & IFF_MULTICAST)) { 635 IP6_STATINC(IP6_STAT_NOROUTE); 636 in6_ifstat_inc(ifp, ifs6_out_discard); 637 error = ENETUNREACH; 638 goto bad; 639 } 640 641 ingroup = in6_multi_group(&ip6->ip6_dst, ifp); 642 if (ingroup && (im6o == NULL || im6o->im6o_multicast_loop)) { 643 /* 644 * If we belong to the destination multicast group 645 * on the outgoing interface, and the caller did not 646 * forbid loopback, loop back a copy. 647 */ 648 KASSERT(dst != NULL); 649 ip6_mloopback(ifp, m, dst); 650 } else { 651 /* 652 * If we are acting as a multicast router, perform 653 * multicast forwarding as if the packet had just 654 * arrived on the interface to which we are about 655 * to send. The multicast forwarding function 656 * recursively calls this function, using the 657 * IPV6_FORWARDING flag to prevent infinite recursion. 658 * 659 * Multicasts that are looped back by ip6_mloopback(), 660 * above, will be forwarded by the ip6_input() routine, 661 * if necessary. 662 */ 663 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 664 if (ip6_mforward(ip6, ifp, m) != 0) { 665 m_freem(m); 666 goto done; 667 } 668 } 669 } 670 /* 671 * Multicasts with a hoplimit of zero may be looped back, 672 * above, but must not be transmitted on a network. 673 * Also, multicasts addressed to the loopback interface 674 * are not sent -- the above call to ip6_mloopback() will 675 * loop back a copy if this host actually belongs to the 676 * destination group on the loopback interface. 677 */ 678 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) || 679 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) { 680 m_freem(m); 681 goto done; 682 } 683 } 684 685 /* 686 * Fill the outgoing interface to tell the upper layer 687 * to increment per-interface statistics. 688 */ 689 if (ifpp) 690 *ifpp = ifp; 691 692 /* Determine path MTU. */ 693 /* 694 * ro_pmtu represent final destination while 695 * ro might represent immediate destination. 696 * Use ro_pmtu destination since MTU might differ. 697 */ 698 if (ro_pmtu != ro) { 699 union { 700 struct sockaddr dst; 701 struct sockaddr_in6 dst6; 702 } u; 703 704 /* ro_pmtu may not have a cache */ 705 sockaddr_in6_init(&u.dst6, &finaldst, 0, 0, 0); 706 rt_pmtu = rtcache_lookup(ro_pmtu, &u.dst); 707 } else 708 rt_pmtu = rt; 709 error = ip6_getpmtu(rt_pmtu, ifp, &mtu, &alwaysfrag); 710 if (rt_pmtu != NULL && rt_pmtu != rt) 711 rtcache_unref(rt_pmtu, ro_pmtu); 712 KASSERT(error == 0); /* ip6_getpmtu never fail if ifp is passed */ 713 714 /* 715 * The caller of this function may specify to use the minimum MTU 716 * in some cases. 717 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU 718 * setting. The logic is a bit complicated; by default, unicast 719 * packets will follow path MTU while multicast packets will be sent at 720 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets 721 * including unicast ones will be sent at the minimum MTU. Multicast 722 * packets will always be sent at the minimum MTU unless 723 * IP6PO_MINMTU_DISABLE is explicitly specified. 724 * See RFC 3542 for more details. 725 */ 726 if (mtu > IPV6_MMTU) { 727 if ((flags & IPV6_MINMTU)) 728 mtu = IPV6_MMTU; 729 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) 730 mtu = IPV6_MMTU; 731 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 732 (opt == NULL || 733 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) { 734 mtu = IPV6_MMTU; 735 } 736 } 737 738 /* 739 * clear embedded scope identifiers if necessary. 740 * in6_clearscope will touch the addresses only when necessary. 741 */ 742 in6_clearscope(&ip6->ip6_src); 743 in6_clearscope(&ip6->ip6_dst); 744 745 /* 746 * If the outgoing packet contains a hop-by-hop options header, 747 * it must be examined and processed even by the source node. 748 * (RFC 2460, section 4.) 749 * 750 * XXX Is this really necessary? 751 */ 752 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 753 u_int32_t dummy1; /* XXX unused */ 754 u_int32_t dummy2; /* XXX unused */ 755 int hoff = sizeof(struct ip6_hdr); 756 757 if (ip6_hopopts_input(&dummy1, &dummy2, &m, &hoff)) { 758 /* m was already freed at this point */ 759 error = EINVAL; 760 goto done; 761 } 762 763 ip6 = mtod(m, struct ip6_hdr *); 764 } 765 766 /* 767 * Run through list of hooks for output packets. 768 */ 769 error = pfil_run_hooks(inet6_pfil_hook, &m, ifp, PFIL_OUT); 770 if (error != 0 || m == NULL) { 771 IP6_STATINC(IP6_STAT_PFILDROP_OUT); 772 goto done; 773 } 774 ip6 = mtod(m, struct ip6_hdr *); 775 776 /* 777 * Send the packet to the outgoing interface. 778 * If necessary, do IPv6 fragmentation before sending. 779 * 780 * the logic here is rather complex: 781 * 1: normal case (dontfrag == 0, alwaysfrag == 0) 782 * 1-a: send as is if tlen <= path mtu 783 * 1-b: fragment if tlen > path mtu 784 * 785 * 2: if user asks us not to fragment (dontfrag == 1) 786 * 2-a: send as is if tlen <= interface mtu 787 * 2-b: error if tlen > interface mtu 788 * 789 * 3: if we always need to attach fragment header (alwaysfrag == 1) 790 * always fragment 791 * 792 * 4: if dontfrag == 1 && alwaysfrag == 1 793 * error, as we cannot handle this conflicting request 794 */ 795 tlen = m->m_pkthdr.len; 796 tso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0; 797 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) 798 dontfrag = 1; 799 else 800 dontfrag = 0; 801 802 if (dontfrag && alwaysfrag) { /* case 4 */ 803 /* conflicting request - can't transmit */ 804 IP6_STATINC(IP6_STAT_CANTFRAG); 805 error = EMSGSIZE; 806 goto bad; 807 } 808 if (dontfrag && (!tso && tlen > ifp->if_mtu)) { /* case 2-b */ 809 /* 810 * Even if the DONTFRAG option is specified, we cannot send the 811 * packet when the data length is larger than the MTU of the 812 * outgoing interface. 813 * Notify the error by sending IPV6_PATHMTU ancillary data as 814 * well as returning an error code (the latter is not described 815 * in the API spec.) 816 */ 817 u_int32_t mtu32; 818 struct ip6ctlparam ip6cp; 819 820 mtu32 = (u_int32_t)mtu; 821 memset(&ip6cp, 0, sizeof(ip6cp)); 822 ip6cp.ip6c_cmdarg = (void *)&mtu32; 823 pfctlinput2(PRC_MSGSIZE, 824 rtcache_getdst(ro_pmtu), &ip6cp); 825 826 IP6_STATINC(IP6_STAT_CANTFRAG); 827 error = EMSGSIZE; 828 goto bad; 829 } 830 831 /* 832 * transmit packet without fragmentation 833 */ 834 if (dontfrag || (!alwaysfrag && (tlen <= mtu || tso))) { 835 /* case 1-a and 2-a */ 836 struct in6_ifaddr *ia6; 837 int sw_csum; 838 int s; 839 840 ip6 = mtod(m, struct ip6_hdr *); 841 s = pserialize_read_enter(); 842 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); 843 if (ia6) { 844 /* Record statistics for this interface address. */ 845 ia6->ia_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len; 846 } 847 pserialize_read_exit(s); 848 849 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx; 850 if ((sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) { 851 if (IN6_NEED_CHECKSUM(ifp, 852 sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6))) { 853 in6_undefer_cksum_tcpudp(m); 854 } 855 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6); 856 } 857 858 KASSERT(dst != NULL); 859 if (__predict_false(sw_csum & M_CSUM_TSOv6)) { 860 /* 861 * TSO6 is required by a packet, but disabled for 862 * the interface. 863 */ 864 error = ip6_tso_output(ifp, origifp, m, dst, rt); 865 } else 866 error = ip6_if_output(ifp, origifp, m, dst, rt); 867 goto done; 868 } 869 870 if (tso) { 871 IP6_STATINC(IP6_STAT_CANTFRAG); /* XXX */ 872 error = EINVAL; /* XXX */ 873 goto bad; 874 } 875 876 /* 877 * try to fragment the packet. case 1-b and 3 878 */ 879 if (mtu < IPV6_MMTU) { 880 /* path MTU cannot be less than IPV6_MMTU */ 881 IP6_STATINC(IP6_STAT_CANTFRAG); 882 error = EMSGSIZE; 883 in6_ifstat_inc(ifp, ifs6_out_fragfail); 884 goto bad; 885 } else if (ip6->ip6_plen == 0) { 886 /* jumbo payload cannot be fragmented */ 887 IP6_STATINC(IP6_STAT_CANTFRAG); 888 error = EMSGSIZE; 889 in6_ifstat_inc(ifp, ifs6_out_fragfail); 890 goto bad; 891 } else { 892 const uint32_t id = ip6_randomid(); 893 struct mbuf **mnext, *m_frgpart; 894 const int hlen = unfragpartlen; 895 struct ip6_frag *ip6f; 896 u_char nextproto; 897 898 if (mtu > IPV6_MAXPACKET) 899 mtu = IPV6_MAXPACKET; 900 901 /* 902 * Must be able to put at least 8 bytes per fragment. 903 */ 904 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 905 if (len < 8) { 906 IP6_STATINC(IP6_STAT_CANTFRAG); 907 error = EMSGSIZE; 908 in6_ifstat_inc(ifp, ifs6_out_fragfail); 909 goto bad; 910 } 911 912 mnext = &m->m_nextpkt; 913 914 /* 915 * Change the next header field of the last header in the 916 * unfragmentable part. 917 */ 918 if (exthdrs.ip6e_rthdr) { 919 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 920 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 921 } else if (exthdrs.ip6e_dest1) { 922 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 923 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 924 } else if (exthdrs.ip6e_hbh) { 925 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 926 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 927 } else { 928 nextproto = ip6->ip6_nxt; 929 ip6->ip6_nxt = IPPROTO_FRAGMENT; 930 } 931 932 if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) 933 != 0) { 934 if (IN6_NEED_CHECKSUM(ifp, 935 m->m_pkthdr.csum_flags & 936 (M_CSUM_UDPv6|M_CSUM_TCPv6))) { 937 in6_undefer_cksum_tcpudp(m); 938 } 939 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6); 940 } 941 942 /* 943 * Loop through length of segment after first fragment, 944 * make new header and copy data of each part and link onto 945 * chain. 946 */ 947 m0 = m; 948 for (off = hlen; off < tlen; off += len) { 949 struct mbuf *mlast; 950 951 MGETHDR(m, M_DONTWAIT, MT_HEADER); 952 if (!m) { 953 error = ENOBUFS; 954 IP6_STATINC(IP6_STAT_ODROPPED); 955 goto sendorfree; 956 } 957 m_reset_rcvif(m); 958 m->m_flags = m0->m_flags & M_COPYFLAGS; 959 *mnext = m; 960 mnext = &m->m_nextpkt; 961 m->m_data += max_linkhdr; 962 mhip6 = mtod(m, struct ip6_hdr *); 963 *mhip6 = *ip6; 964 m->m_len = sizeof(*mhip6); 965 966 ip6f = NULL; 967 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 968 if (error) { 969 IP6_STATINC(IP6_STAT_ODROPPED); 970 goto sendorfree; 971 } 972 973 /* Fill in the Frag6 Header */ 974 ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7)); 975 if (off + len >= tlen) 976 len = tlen - off; 977 else 978 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 979 ip6f->ip6f_reserved = 0; 980 ip6f->ip6f_ident = id; 981 ip6f->ip6f_nxt = nextproto; 982 983 mhip6->ip6_plen = htons((u_int16_t)(len + hlen + 984 sizeof(*ip6f) - sizeof(struct ip6_hdr))); 985 if ((m_frgpart = m_copym(m0, off, len, M_DONTWAIT)) == NULL) { 986 error = ENOBUFS; 987 IP6_STATINC(IP6_STAT_ODROPPED); 988 goto sendorfree; 989 } 990 for (mlast = m; mlast->m_next; mlast = mlast->m_next) 991 ; 992 mlast->m_next = m_frgpart; 993 994 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 995 m_reset_rcvif(m); 996 IP6_STATINC(IP6_STAT_OFRAGMENTS); 997 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 998 } 999 1000 in6_ifstat_inc(ifp, ifs6_out_fragok); 1001 } 1002 1003 sendorfree: 1004 m = m0->m_nextpkt; 1005 m0->m_nextpkt = 0; 1006 m_freem(m0); 1007 for (m0 = m; m; m = m0) { 1008 m0 = m->m_nextpkt; 1009 m->m_nextpkt = 0; 1010 if (error == 0) { 1011 struct in6_ifaddr *ia6; 1012 int s; 1013 ip6 = mtod(m, struct ip6_hdr *); 1014 s = pserialize_read_enter(); 1015 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); 1016 if (ia6) { 1017 /* 1018 * Record statistics for this interface 1019 * address. 1020 */ 1021 ia6->ia_ifa.ifa_data.ifad_outbytes += 1022 m->m_pkthdr.len; 1023 } 1024 pserialize_read_exit(s); 1025 KASSERT(dst != NULL); 1026 error = ip6_if_output(ifp, origifp, m, dst, rt); 1027 } else 1028 m_freem(m); 1029 } 1030 1031 if (error == 0) 1032 IP6_STATINC(IP6_STAT_FRAGMENTED); 1033 1034 done: 1035 rtcache_unref(rt, ro); 1036 if (ro == &ip6route) 1037 rtcache_free(&ip6route); 1038 #ifdef IPSEC 1039 if (sp != NULL) 1040 KEY_SP_UNREF(&sp); 1041 #endif 1042 if_put(ifp, &psref); 1043 if (release_psref_ia) 1044 if_put(origifp, &psref_ia); 1045 curlwp_bindx(bound); 1046 1047 return error; 1048 1049 freehdrs: 1050 m_freem(exthdrs.ip6e_hbh); 1051 m_freem(exthdrs.ip6e_dest1); 1052 m_freem(exthdrs.ip6e_rthdr); 1053 m_freem(exthdrs.ip6e_dest2); 1054 /* FALLTHROUGH */ 1055 bad: 1056 m_freem(m); 1057 goto done; 1058 1059 badscope: 1060 IP6_STATINC(IP6_STAT_BADSCOPE); 1061 in6_ifstat_inc(origifp, ifs6_out_discard); 1062 if (error == 0) 1063 error = EHOSTUNREACH; /* XXX */ 1064 goto bad; 1065 } 1066 1067 static int 1068 ip6_copyexthdr(struct mbuf **mp, void *hdr, int hlen) 1069 { 1070 struct mbuf *m; 1071 1072 if (hlen > MCLBYTES) 1073 return ENOBUFS; /* XXX */ 1074 1075 MGET(m, M_DONTWAIT, MT_DATA); 1076 if (!m) 1077 return ENOBUFS; 1078 1079 if (hlen > MLEN) { 1080 MCLGET(m, M_DONTWAIT); 1081 if ((m->m_flags & M_EXT) == 0) { 1082 m_free(m); 1083 return ENOBUFS; 1084 } 1085 } 1086 m->m_len = hlen; 1087 if (hdr) 1088 memcpy(mtod(m, void *), hdr, hlen); 1089 1090 *mp = m; 1091 return 0; 1092 } 1093 1094 /* 1095 * Insert jumbo payload option. 1096 */ 1097 static int 1098 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) 1099 { 1100 struct mbuf *mopt; 1101 u_int8_t *optbuf; 1102 u_int32_t v; 1103 1104 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1105 1106 /* 1107 * If there is no hop-by-hop options header, allocate new one. 1108 * If there is one but it doesn't have enough space to store the 1109 * jumbo payload option, allocate a cluster to store the whole options. 1110 * Otherwise, use it to store the options. 1111 */ 1112 if (exthdrs->ip6e_hbh == NULL) { 1113 MGET(mopt, M_DONTWAIT, MT_DATA); 1114 if (mopt == 0) 1115 return (ENOBUFS); 1116 mopt->m_len = JUMBOOPTLEN; 1117 optbuf = mtod(mopt, u_int8_t *); 1118 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1119 exthdrs->ip6e_hbh = mopt; 1120 } else { 1121 struct ip6_hbh *hbh; 1122 1123 mopt = exthdrs->ip6e_hbh; 1124 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1125 const int oldoptlen = mopt->m_len; 1126 struct mbuf *n; 1127 1128 /* 1129 * Assumptions: 1130 * - exthdrs->ip6e_hbh is not referenced from places 1131 * other than exthdrs. 1132 * - exthdrs->ip6e_hbh is not an mbuf chain. 1133 */ 1134 KASSERT(mopt->m_next == NULL); 1135 1136 /* 1137 * Give up if the whole (new) hbh header does not fit 1138 * even in an mbuf cluster. 1139 */ 1140 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1141 return ENOBUFS; 1142 1143 /* 1144 * At this point, we must always prepare a cluster. 1145 */ 1146 MGET(n, M_DONTWAIT, MT_DATA); 1147 if (n) { 1148 MCLGET(n, M_DONTWAIT); 1149 if ((n->m_flags & M_EXT) == 0) { 1150 m_freem(n); 1151 n = NULL; 1152 } 1153 } 1154 if (!n) 1155 return ENOBUFS; 1156 1157 n->m_len = oldoptlen + JUMBOOPTLEN; 1158 bcopy(mtod(mopt, void *), mtod(n, void *), 1159 oldoptlen); 1160 optbuf = mtod(n, u_int8_t *) + oldoptlen; 1161 m_freem(mopt); 1162 mopt = exthdrs->ip6e_hbh = n; 1163 } else { 1164 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len; 1165 mopt->m_len += JUMBOOPTLEN; 1166 } 1167 optbuf[0] = IP6OPT_PADN; 1168 optbuf[1] = 0; 1169 1170 /* 1171 * Adjust the header length according to the pad and 1172 * the jumbo payload option. 1173 */ 1174 hbh = mtod(mopt, struct ip6_hbh *); 1175 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1176 } 1177 1178 /* fill in the option. */ 1179 optbuf[2] = IP6OPT_JUMBO; 1180 optbuf[3] = 4; 1181 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1182 memcpy(&optbuf[4], &v, sizeof(u_int32_t)); 1183 1184 /* finally, adjust the packet header length */ 1185 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1186 1187 return 0; 1188 #undef JUMBOOPTLEN 1189 } 1190 1191 /* 1192 * Insert fragment header and copy unfragmentable header portions. 1193 * 1194 * *frghdrp will not be read, and it is guaranteed that either an 1195 * error is returned or that *frghdrp will point to space allocated 1196 * for the fragment header. 1197 * 1198 * On entry, m contains: 1199 * IPv6 Header 1200 * On exit, it contains: 1201 * IPv6 Header -> Unfragmentable Part -> Frag6 Header 1202 */ 1203 static int 1204 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, 1205 struct ip6_frag **frghdrp) 1206 { 1207 struct mbuf *n, *mlast; 1208 1209 if (hlen > sizeof(struct ip6_hdr)) { 1210 n = m_copym(m0, sizeof(struct ip6_hdr), 1211 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1212 if (n == NULL) 1213 return ENOBUFS; 1214 m->m_next = n; 1215 } else 1216 n = m; 1217 1218 /* Search for the last mbuf of unfragmentable part. */ 1219 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1220 ; 1221 1222 if ((mlast->m_flags & M_EXT) == 0 && 1223 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1224 /* use the trailing space of the last mbuf for the fragment hdr */ 1225 *frghdrp = (struct ip6_frag *)(mtod(mlast, char *) + 1226 mlast->m_len); 1227 mlast->m_len += sizeof(struct ip6_frag); 1228 } else { 1229 /* allocate a new mbuf for the fragment header */ 1230 struct mbuf *mfrg; 1231 1232 MGET(mfrg, M_DONTWAIT, MT_DATA); 1233 if (mfrg == NULL) 1234 return ENOBUFS; 1235 mfrg->m_len = sizeof(struct ip6_frag); 1236 *frghdrp = mtod(mfrg, struct ip6_frag *); 1237 mlast->m_next = mfrg; 1238 } 1239 1240 return 0; 1241 } 1242 1243 static int 1244 ip6_getpmtu(struct rtentry *rt, struct ifnet *ifp, u_long *mtup, 1245 int *alwaysfragp) 1246 { 1247 u_int32_t mtu = 0; 1248 int alwaysfrag = 0; 1249 int error = 0; 1250 1251 if (rt != NULL) { 1252 if (ifp == NULL) 1253 ifp = rt->rt_ifp; 1254 mtu = rt->rt_rmx.rmx_mtu; 1255 if (mtu == 0) 1256 mtu = ifp->if_mtu; 1257 else if (mtu < IPV6_MMTU) { 1258 /* 1259 * RFC2460 section 5, last paragraph: 1260 * if we record ICMPv6 too big message with 1261 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU 1262 * or smaller, with fragment header attached. 1263 * (fragment header is needed regardless from the 1264 * packet size, for translators to identify packets) 1265 */ 1266 alwaysfrag = 1; 1267 mtu = IPV6_MMTU; 1268 } else if (mtu > ifp->if_mtu) { 1269 /* 1270 * The MTU on the route is larger than the MTU on 1271 * the interface! This shouldn't happen, unless the 1272 * MTU of the interface has been changed after the 1273 * interface was brought up. Change the MTU in the 1274 * route to match the interface MTU (as long as the 1275 * field isn't locked). 1276 */ 1277 mtu = ifp->if_mtu; 1278 if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) 1279 rt->rt_rmx.rmx_mtu = mtu; 1280 } 1281 } else if (ifp) { 1282 mtu = ifp->if_mtu; 1283 } else 1284 error = EHOSTUNREACH; /* XXX */ 1285 1286 *mtup = mtu; 1287 if (alwaysfragp) 1288 *alwaysfragp = alwaysfrag; 1289 return (error); 1290 } 1291 1292 /* 1293 * IP6 socket option processing. 1294 */ 1295 int 1296 ip6_ctloutput(int op, struct socket *so, struct sockopt *sopt) 1297 { 1298 int optdatalen, uproto; 1299 void *optdata; 1300 struct in6pcb *in6p = sotoin6pcb(so); 1301 struct ip_moptions **mopts; 1302 int error, optval; 1303 int level, optname; 1304 1305 KASSERT(solocked(so)); 1306 KASSERT(sopt != NULL); 1307 1308 level = sopt->sopt_level; 1309 optname = sopt->sopt_name; 1310 1311 error = optval = 0; 1312 uproto = (int)so->so_proto->pr_protocol; 1313 1314 switch (level) { 1315 case IPPROTO_IP: 1316 switch (optname) { 1317 case IP_ADD_MEMBERSHIP: 1318 case IP_DROP_MEMBERSHIP: 1319 case IP_MULTICAST_IF: 1320 case IP_MULTICAST_LOOP: 1321 case IP_MULTICAST_TTL: 1322 mopts = &in6p->in6p_v4moptions; 1323 switch (op) { 1324 case PRCO_GETOPT: 1325 return ip_getmoptions(*mopts, sopt); 1326 case PRCO_SETOPT: 1327 return ip_setmoptions(mopts, sopt); 1328 default: 1329 return EINVAL; 1330 } 1331 default: 1332 return ENOPROTOOPT; 1333 } 1334 case IPPROTO_IPV6: 1335 break; 1336 default: 1337 return ENOPROTOOPT; 1338 } 1339 switch (op) { 1340 case PRCO_SETOPT: 1341 switch (optname) { 1342 #ifdef RFC2292 1343 case IPV6_2292PKTOPTIONS: 1344 error = ip6_pcbopts(&in6p->in6p_outputopts, so, sopt); 1345 break; 1346 #endif 1347 1348 /* 1349 * Use of some Hop-by-Hop options or some 1350 * Destination options, might require special 1351 * privilege. That is, normal applications 1352 * (without special privilege) might be forbidden 1353 * from setting certain options in outgoing packets, 1354 * and might never see certain options in received 1355 * packets. [RFC 2292 Section 6] 1356 * KAME specific note: 1357 * KAME prevents non-privileged users from sending or 1358 * receiving ANY hbh/dst options in order to avoid 1359 * overhead of parsing options in the kernel. 1360 */ 1361 case IPV6_RECVHOPOPTS: 1362 case IPV6_RECVDSTOPTS: 1363 case IPV6_RECVRTHDRDSTOPTS: 1364 error = kauth_authorize_network( 1365 kauth_cred_get(), 1366 KAUTH_NETWORK_IPV6, KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, 1367 NULL, NULL, NULL); 1368 if (error) 1369 break; 1370 /* FALLTHROUGH */ 1371 case IPV6_UNICAST_HOPS: 1372 case IPV6_HOPLIMIT: 1373 case IPV6_FAITH: 1374 1375 case IPV6_RECVPKTINFO: 1376 case IPV6_RECVHOPLIMIT: 1377 case IPV6_RECVRTHDR: 1378 case IPV6_RECVPATHMTU: 1379 case IPV6_RECVTCLASS: 1380 case IPV6_V6ONLY: 1381 case IPV6_BINDANY: 1382 error = sockopt_getint(sopt, &optval); 1383 if (error) 1384 break; 1385 switch (optname) { 1386 case IPV6_UNICAST_HOPS: 1387 if (optval < -1 || optval >= 256) 1388 error = EINVAL; 1389 else { 1390 /* -1 = kernel default */ 1391 in6p->in6p_hops = optval; 1392 } 1393 break; 1394 #define OPTSET(bit) \ 1395 do { \ 1396 if (optval) \ 1397 in6p->in6p_flags |= (bit); \ 1398 else \ 1399 in6p->in6p_flags &= ~(bit); \ 1400 } while (/*CONSTCOND*/ 0) 1401 1402 #ifdef RFC2292 1403 #define OPTSET2292(bit) \ 1404 do { \ 1405 in6p->in6p_flags |= IN6P_RFC2292; \ 1406 if (optval) \ 1407 in6p->in6p_flags |= (bit); \ 1408 else \ 1409 in6p->in6p_flags &= ~(bit); \ 1410 } while (/*CONSTCOND*/ 0) 1411 #endif 1412 1413 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) 1414 1415 case IPV6_RECVPKTINFO: 1416 #ifdef RFC2292 1417 /* cannot mix with RFC2292 */ 1418 if (OPTBIT(IN6P_RFC2292)) { 1419 error = EINVAL; 1420 break; 1421 } 1422 #endif 1423 OPTSET(IN6P_PKTINFO); 1424 break; 1425 1426 case IPV6_HOPLIMIT: 1427 { 1428 struct ip6_pktopts **optp; 1429 1430 #ifdef RFC2292 1431 /* cannot mix with RFC2292 */ 1432 if (OPTBIT(IN6P_RFC2292)) { 1433 error = EINVAL; 1434 break; 1435 } 1436 #endif 1437 optp = &in6p->in6p_outputopts; 1438 error = ip6_pcbopt(IPV6_HOPLIMIT, 1439 (u_char *)&optval, 1440 sizeof(optval), 1441 optp, 1442 kauth_cred_get(), uproto); 1443 break; 1444 } 1445 1446 case IPV6_RECVHOPLIMIT: 1447 #ifdef RFC2292 1448 /* cannot mix with RFC2292 */ 1449 if (OPTBIT(IN6P_RFC2292)) { 1450 error = EINVAL; 1451 break; 1452 } 1453 #endif 1454 OPTSET(IN6P_HOPLIMIT); 1455 break; 1456 1457 case IPV6_RECVHOPOPTS: 1458 #ifdef RFC2292 1459 /* cannot mix with RFC2292 */ 1460 if (OPTBIT(IN6P_RFC2292)) { 1461 error = EINVAL; 1462 break; 1463 } 1464 #endif 1465 OPTSET(IN6P_HOPOPTS); 1466 break; 1467 1468 case IPV6_RECVDSTOPTS: 1469 #ifdef RFC2292 1470 /* cannot mix with RFC2292 */ 1471 if (OPTBIT(IN6P_RFC2292)) { 1472 error = EINVAL; 1473 break; 1474 } 1475 #endif 1476 OPTSET(IN6P_DSTOPTS); 1477 break; 1478 1479 case IPV6_RECVRTHDRDSTOPTS: 1480 #ifdef RFC2292 1481 /* cannot mix with RFC2292 */ 1482 if (OPTBIT(IN6P_RFC2292)) { 1483 error = EINVAL; 1484 break; 1485 } 1486 #endif 1487 OPTSET(IN6P_RTHDRDSTOPTS); 1488 break; 1489 1490 case IPV6_RECVRTHDR: 1491 #ifdef RFC2292 1492 /* cannot mix with RFC2292 */ 1493 if (OPTBIT(IN6P_RFC2292)) { 1494 error = EINVAL; 1495 break; 1496 } 1497 #endif 1498 OPTSET(IN6P_RTHDR); 1499 break; 1500 1501 case IPV6_FAITH: 1502 OPTSET(IN6P_FAITH); 1503 break; 1504 1505 case IPV6_RECVPATHMTU: 1506 /* 1507 * We ignore this option for TCP 1508 * sockets. 1509 * (RFC3542 leaves this case 1510 * unspecified.) 1511 */ 1512 if (uproto != IPPROTO_TCP) 1513 OPTSET(IN6P_MTU); 1514 break; 1515 1516 case IPV6_V6ONLY: 1517 /* 1518 * make setsockopt(IPV6_V6ONLY) 1519 * available only prior to bind(2). 1520 * see ipng mailing list, Jun 22 2001. 1521 */ 1522 if (in6p->in6p_lport || 1523 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1524 error = EINVAL; 1525 break; 1526 } 1527 #ifdef INET6_BINDV6ONLY 1528 if (!optval) 1529 error = EINVAL; 1530 #else 1531 OPTSET(IN6P_IPV6_V6ONLY); 1532 #endif 1533 break; 1534 1535 case IPV6_RECVTCLASS: 1536 #ifdef RFC2292 1537 /* cannot mix with RFC2292 XXX */ 1538 if (OPTBIT(IN6P_RFC2292)) { 1539 error = EINVAL; 1540 break; 1541 } 1542 #endif 1543 OPTSET(IN6P_TCLASS); 1544 break; 1545 1546 case IPV6_BINDANY: 1547 error = kauth_authorize_network( 1548 kauth_cred_get(), KAUTH_NETWORK_BIND, 1549 KAUTH_REQ_NETWORK_BIND_ANYADDR, so, NULL, 1550 NULL); 1551 if (error) 1552 break; 1553 OPTSET(IN6P_BINDANY); 1554 break; 1555 } 1556 break; 1557 1558 case IPV6_OTCLASS: 1559 { 1560 struct ip6_pktopts **optp; 1561 u_int8_t tclass; 1562 1563 error = sockopt_get(sopt, &tclass, sizeof(tclass)); 1564 if (error) 1565 break; 1566 optp = &in6p->in6p_outputopts; 1567 error = ip6_pcbopt(optname, 1568 (u_char *)&tclass, 1569 sizeof(tclass), 1570 optp, 1571 kauth_cred_get(), uproto); 1572 break; 1573 } 1574 1575 case IPV6_TCLASS: 1576 case IPV6_DONTFRAG: 1577 case IPV6_USE_MIN_MTU: 1578 case IPV6_PREFER_TEMPADDR: 1579 error = sockopt_getint(sopt, &optval); 1580 if (error) 1581 break; 1582 { 1583 struct ip6_pktopts **optp; 1584 optp = &in6p->in6p_outputopts; 1585 error = ip6_pcbopt(optname, 1586 (u_char *)&optval, 1587 sizeof(optval), 1588 optp, 1589 kauth_cred_get(), uproto); 1590 break; 1591 } 1592 1593 #ifdef RFC2292 1594 case IPV6_2292PKTINFO: 1595 case IPV6_2292HOPLIMIT: 1596 case IPV6_2292HOPOPTS: 1597 case IPV6_2292DSTOPTS: 1598 case IPV6_2292RTHDR: 1599 /* RFC 2292 */ 1600 error = sockopt_getint(sopt, &optval); 1601 if (error) 1602 break; 1603 1604 switch (optname) { 1605 case IPV6_2292PKTINFO: 1606 OPTSET2292(IN6P_PKTINFO); 1607 break; 1608 case IPV6_2292HOPLIMIT: 1609 OPTSET2292(IN6P_HOPLIMIT); 1610 break; 1611 case IPV6_2292HOPOPTS: 1612 /* 1613 * Check super-user privilege. 1614 * See comments for IPV6_RECVHOPOPTS. 1615 */ 1616 error = kauth_authorize_network( 1617 kauth_cred_get(), 1618 KAUTH_NETWORK_IPV6, 1619 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, 1620 NULL, NULL); 1621 if (error) 1622 return (error); 1623 OPTSET2292(IN6P_HOPOPTS); 1624 break; 1625 case IPV6_2292DSTOPTS: 1626 error = kauth_authorize_network( 1627 kauth_cred_get(), 1628 KAUTH_NETWORK_IPV6, 1629 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, 1630 NULL, NULL); 1631 if (error) 1632 return (error); 1633 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1634 break; 1635 case IPV6_2292RTHDR: 1636 OPTSET2292(IN6P_RTHDR); 1637 break; 1638 } 1639 break; 1640 #endif 1641 case IPV6_PKTINFO: 1642 case IPV6_HOPOPTS: 1643 case IPV6_RTHDR: 1644 case IPV6_DSTOPTS: 1645 case IPV6_RTHDRDSTOPTS: 1646 case IPV6_NEXTHOP: { 1647 /* new advanced API (RFC3542) */ 1648 void *optbuf; 1649 int optbuflen; 1650 struct ip6_pktopts **optp; 1651 1652 #ifdef RFC2292 1653 /* cannot mix with RFC2292 */ 1654 if (OPTBIT(IN6P_RFC2292)) { 1655 error = EINVAL; 1656 break; 1657 } 1658 #endif 1659 1660 optbuflen = sopt->sopt_size; 1661 optbuf = malloc(optbuflen, M_IP6OPT, M_NOWAIT); 1662 if (optbuf == NULL) { 1663 error = ENOBUFS; 1664 break; 1665 } 1666 1667 error = sockopt_get(sopt, optbuf, optbuflen); 1668 if (error) { 1669 free(optbuf, M_IP6OPT); 1670 break; 1671 } 1672 optp = &in6p->in6p_outputopts; 1673 error = ip6_pcbopt(optname, optbuf, optbuflen, 1674 optp, kauth_cred_get(), uproto); 1675 1676 free(optbuf, M_IP6OPT); 1677 break; 1678 } 1679 #undef OPTSET 1680 1681 case IPV6_MULTICAST_IF: 1682 case IPV6_MULTICAST_HOPS: 1683 case IPV6_MULTICAST_LOOP: 1684 case IPV6_JOIN_GROUP: 1685 case IPV6_LEAVE_GROUP: 1686 error = ip6_setmoptions(sopt, in6p); 1687 break; 1688 1689 case IPV6_PORTRANGE: 1690 error = sockopt_getint(sopt, &optval); 1691 if (error) 1692 break; 1693 1694 switch (optval) { 1695 case IPV6_PORTRANGE_DEFAULT: 1696 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1697 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1698 break; 1699 1700 case IPV6_PORTRANGE_HIGH: 1701 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1702 in6p->in6p_flags |= IN6P_HIGHPORT; 1703 break; 1704 1705 case IPV6_PORTRANGE_LOW: 1706 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1707 in6p->in6p_flags |= IN6P_LOWPORT; 1708 break; 1709 1710 default: 1711 error = EINVAL; 1712 break; 1713 } 1714 break; 1715 1716 case IPV6_PORTALGO: 1717 error = sockopt_getint(sopt, &optval); 1718 if (error) 1719 break; 1720 1721 error = portalgo_algo_index_select( 1722 (struct inpcb_hdr *)in6p, optval); 1723 break; 1724 1725 #if defined(IPSEC) 1726 case IPV6_IPSEC_POLICY: 1727 if (ipsec_enabled) { 1728 error = ipsec_set_policy(in6p, 1729 sopt->sopt_data, sopt->sopt_size, 1730 kauth_cred_get()); 1731 } else 1732 error = ENOPROTOOPT; 1733 break; 1734 #endif /* IPSEC */ 1735 1736 default: 1737 error = ENOPROTOOPT; 1738 break; 1739 } 1740 break; 1741 1742 case PRCO_GETOPT: 1743 switch (optname) { 1744 #ifdef RFC2292 1745 case IPV6_2292PKTOPTIONS: 1746 /* 1747 * RFC3542 (effectively) deprecated the 1748 * semantics of the 2292-style pktoptions. 1749 * Since it was not reliable in nature (i.e., 1750 * applications had to expect the lack of some 1751 * information after all), it would make sense 1752 * to simplify this part by always returning 1753 * empty data. 1754 */ 1755 break; 1756 #endif 1757 1758 case IPV6_RECVHOPOPTS: 1759 case IPV6_RECVDSTOPTS: 1760 case IPV6_RECVRTHDRDSTOPTS: 1761 case IPV6_UNICAST_HOPS: 1762 case IPV6_RECVPKTINFO: 1763 case IPV6_RECVHOPLIMIT: 1764 case IPV6_RECVRTHDR: 1765 case IPV6_RECVPATHMTU: 1766 1767 case IPV6_FAITH: 1768 case IPV6_V6ONLY: 1769 case IPV6_PORTRANGE: 1770 case IPV6_RECVTCLASS: 1771 case IPV6_BINDANY: 1772 switch (optname) { 1773 1774 case IPV6_RECVHOPOPTS: 1775 optval = OPTBIT(IN6P_HOPOPTS); 1776 break; 1777 1778 case IPV6_RECVDSTOPTS: 1779 optval = OPTBIT(IN6P_DSTOPTS); 1780 break; 1781 1782 case IPV6_RECVRTHDRDSTOPTS: 1783 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 1784 break; 1785 1786 case IPV6_UNICAST_HOPS: 1787 optval = in6p->in6p_hops; 1788 break; 1789 1790 case IPV6_RECVPKTINFO: 1791 optval = OPTBIT(IN6P_PKTINFO); 1792 break; 1793 1794 case IPV6_RECVHOPLIMIT: 1795 optval = OPTBIT(IN6P_HOPLIMIT); 1796 break; 1797 1798 case IPV6_RECVRTHDR: 1799 optval = OPTBIT(IN6P_RTHDR); 1800 break; 1801 1802 case IPV6_RECVPATHMTU: 1803 optval = OPTBIT(IN6P_MTU); 1804 break; 1805 1806 case IPV6_FAITH: 1807 optval = OPTBIT(IN6P_FAITH); 1808 break; 1809 1810 case IPV6_V6ONLY: 1811 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1812 break; 1813 1814 case IPV6_PORTRANGE: 1815 { 1816 int flags; 1817 flags = in6p->in6p_flags; 1818 if (flags & IN6P_HIGHPORT) 1819 optval = IPV6_PORTRANGE_HIGH; 1820 else if (flags & IN6P_LOWPORT) 1821 optval = IPV6_PORTRANGE_LOW; 1822 else 1823 optval = 0; 1824 break; 1825 } 1826 case IPV6_RECVTCLASS: 1827 optval = OPTBIT(IN6P_TCLASS); 1828 break; 1829 1830 case IPV6_BINDANY: 1831 optval = OPTBIT(IN6P_BINDANY); 1832 break; 1833 } 1834 1835 if (error) 1836 break; 1837 error = sockopt_setint(sopt, optval); 1838 break; 1839 1840 case IPV6_PATHMTU: 1841 { 1842 u_long pmtu = 0; 1843 struct ip6_mtuinfo mtuinfo; 1844 struct route *ro = &in6p->in6p_route; 1845 struct rtentry *rt; 1846 union { 1847 struct sockaddr dst; 1848 struct sockaddr_in6 dst6; 1849 } u; 1850 1851 if (!(so->so_state & SS_ISCONNECTED)) 1852 return (ENOTCONN); 1853 /* 1854 * XXX: we dot not consider the case of source 1855 * routing, or optional information to specify 1856 * the outgoing interface. 1857 */ 1858 sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0); 1859 rt = rtcache_lookup(ro, &u.dst); 1860 error = ip6_getpmtu(rt, NULL, &pmtu, NULL); 1861 rtcache_unref(rt, ro); 1862 if (error) 1863 break; 1864 if (pmtu > IPV6_MAXPACKET) 1865 pmtu = IPV6_MAXPACKET; 1866 1867 memset(&mtuinfo, 0, sizeof(mtuinfo)); 1868 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 1869 optdata = (void *)&mtuinfo; 1870 optdatalen = sizeof(mtuinfo); 1871 if (optdatalen > MCLBYTES) 1872 return (EMSGSIZE); /* XXX */ 1873 error = sockopt_set(sopt, optdata, optdatalen); 1874 break; 1875 } 1876 1877 #ifdef RFC2292 1878 case IPV6_2292PKTINFO: 1879 case IPV6_2292HOPLIMIT: 1880 case IPV6_2292HOPOPTS: 1881 case IPV6_2292RTHDR: 1882 case IPV6_2292DSTOPTS: 1883 switch (optname) { 1884 case IPV6_2292PKTINFO: 1885 optval = OPTBIT(IN6P_PKTINFO); 1886 break; 1887 case IPV6_2292HOPLIMIT: 1888 optval = OPTBIT(IN6P_HOPLIMIT); 1889 break; 1890 case IPV6_2292HOPOPTS: 1891 optval = OPTBIT(IN6P_HOPOPTS); 1892 break; 1893 case IPV6_2292RTHDR: 1894 optval = OPTBIT(IN6P_RTHDR); 1895 break; 1896 case IPV6_2292DSTOPTS: 1897 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1898 break; 1899 } 1900 error = sockopt_setint(sopt, optval); 1901 break; 1902 #endif 1903 case IPV6_PKTINFO: 1904 case IPV6_HOPOPTS: 1905 case IPV6_RTHDR: 1906 case IPV6_DSTOPTS: 1907 case IPV6_RTHDRDSTOPTS: 1908 case IPV6_NEXTHOP: 1909 case IPV6_OTCLASS: 1910 case IPV6_TCLASS: 1911 case IPV6_DONTFRAG: 1912 case IPV6_USE_MIN_MTU: 1913 case IPV6_PREFER_TEMPADDR: 1914 error = ip6_getpcbopt(in6p->in6p_outputopts, 1915 optname, sopt); 1916 break; 1917 1918 case IPV6_MULTICAST_IF: 1919 case IPV6_MULTICAST_HOPS: 1920 case IPV6_MULTICAST_LOOP: 1921 case IPV6_JOIN_GROUP: 1922 case IPV6_LEAVE_GROUP: 1923 error = ip6_getmoptions(sopt, in6p); 1924 break; 1925 1926 case IPV6_PORTALGO: 1927 optval = ((struct inpcb_hdr *)in6p)->inph_portalgo; 1928 error = sockopt_setint(sopt, optval); 1929 break; 1930 1931 #if defined(IPSEC) 1932 case IPV6_IPSEC_POLICY: 1933 if (ipsec_used) { 1934 struct mbuf *m = NULL; 1935 1936 /* 1937 * XXX: this will return EINVAL as sopt is 1938 * empty 1939 */ 1940 error = ipsec_get_policy(in6p, sopt->sopt_data, 1941 sopt->sopt_size, &m); 1942 if (!error) 1943 error = sockopt_setmbuf(sopt, m); 1944 } else 1945 error = ENOPROTOOPT; 1946 break; 1947 #endif /* IPSEC */ 1948 1949 default: 1950 error = ENOPROTOOPT; 1951 break; 1952 } 1953 break; 1954 } 1955 return (error); 1956 } 1957 1958 int 1959 ip6_raw_ctloutput(int op, struct socket *so, struct sockopt *sopt) 1960 { 1961 int error = 0, optval; 1962 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 1963 struct in6pcb *in6p = sotoin6pcb(so); 1964 int level, optname; 1965 1966 KASSERT(sopt != NULL); 1967 1968 level = sopt->sopt_level; 1969 optname = sopt->sopt_name; 1970 1971 if (level != IPPROTO_IPV6) { 1972 return ENOPROTOOPT; 1973 } 1974 1975 switch (optname) { 1976 case IPV6_CHECKSUM: 1977 /* 1978 * For ICMPv6 sockets, no modification allowed for checksum 1979 * offset, permit "no change" values to help existing apps. 1980 * 1981 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM 1982 * for an ICMPv6 socket will fail." The current 1983 * behavior does not meet RFC3542. 1984 */ 1985 switch (op) { 1986 case PRCO_SETOPT: 1987 error = sockopt_getint(sopt, &optval); 1988 if (error) 1989 break; 1990 if ((optval % 2) != 0) { 1991 /* the API assumes even offset values */ 1992 error = EINVAL; 1993 } else if (so->so_proto->pr_protocol == 1994 IPPROTO_ICMPV6) { 1995 if (optval != icmp6off) 1996 error = EINVAL; 1997 } else 1998 in6p->in6p_cksum = optval; 1999 break; 2000 2001 case PRCO_GETOPT: 2002 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 2003 optval = icmp6off; 2004 else 2005 optval = in6p->in6p_cksum; 2006 2007 error = sockopt_setint(sopt, optval); 2008 break; 2009 2010 default: 2011 error = EINVAL; 2012 break; 2013 } 2014 break; 2015 2016 default: 2017 error = ENOPROTOOPT; 2018 break; 2019 } 2020 2021 return (error); 2022 } 2023 2024 #ifdef RFC2292 2025 /* 2026 * Set up IP6 options in pcb for insertion in output packets or 2027 * specifying behavior of outgoing packets. 2028 */ 2029 static int 2030 ip6_pcbopts(struct ip6_pktopts **pktopt, struct socket *so, 2031 struct sockopt *sopt) 2032 { 2033 struct ip6_pktopts *opt = *pktopt; 2034 struct mbuf *m; 2035 int error = 0; 2036 2037 KASSERT(solocked(so)); 2038 2039 /* turn off any old options. */ 2040 if (opt) { 2041 #ifdef DIAGNOSTIC 2042 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 2043 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 2044 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 2045 printf("ip6_pcbopts: all specified options are cleared.\n"); 2046 #endif 2047 ip6_clearpktopts(opt, -1); 2048 } else { 2049 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT); 2050 if (opt == NULL) 2051 return (ENOBUFS); 2052 } 2053 *pktopt = NULL; 2054 2055 if (sopt == NULL || sopt->sopt_size == 0) { 2056 /* 2057 * Only turning off any previous options, regardless of 2058 * whether the opt is just created or given. 2059 */ 2060 free(opt, M_IP6OPT); 2061 return (0); 2062 } 2063 2064 /* set options specified by user. */ 2065 m = sockopt_getmbuf(sopt); 2066 if (m == NULL) { 2067 free(opt, M_IP6OPT); 2068 return (ENOBUFS); 2069 } 2070 2071 error = ip6_setpktopts(m, opt, NULL, kauth_cred_get(), 2072 so->so_proto->pr_protocol); 2073 m_freem(m); 2074 if (error != 0) { 2075 ip6_clearpktopts(opt, -1); /* XXX: discard all options */ 2076 free(opt, M_IP6OPT); 2077 return (error); 2078 } 2079 *pktopt = opt; 2080 return (0); 2081 } 2082 #endif 2083 2084 /* 2085 * initialize ip6_pktopts. beware that there are non-zero default values in 2086 * the struct. 2087 */ 2088 void 2089 ip6_initpktopts(struct ip6_pktopts *opt) 2090 { 2091 2092 memset(opt, 0, sizeof(*opt)); 2093 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 2094 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 2095 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 2096 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; 2097 } 2098 2099 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */ 2100 static int 2101 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, 2102 kauth_cred_t cred, int uproto) 2103 { 2104 struct ip6_pktopts *opt; 2105 2106 if (*pktopt == NULL) { 2107 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 2108 M_NOWAIT); 2109 if (*pktopt == NULL) 2110 return (ENOBUFS); 2111 2112 ip6_initpktopts(*pktopt); 2113 } 2114 opt = *pktopt; 2115 2116 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto)); 2117 } 2118 2119 static int 2120 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt) 2121 { 2122 void *optdata = NULL; 2123 int optdatalen = 0; 2124 struct ip6_ext *ip6e; 2125 int error = 0; 2126 struct in6_pktinfo null_pktinfo; 2127 int deftclass = 0, on; 2128 int defminmtu = IP6PO_MINMTU_MCASTONLY; 2129 int defpreftemp = IP6PO_TEMPADDR_SYSTEM; 2130 2131 switch (optname) { 2132 case IPV6_PKTINFO: 2133 if (pktopt && pktopt->ip6po_pktinfo) 2134 optdata = (void *)pktopt->ip6po_pktinfo; 2135 else { 2136 /* XXX: we don't have to do this every time... */ 2137 memset(&null_pktinfo, 0, sizeof(null_pktinfo)); 2138 optdata = (void *)&null_pktinfo; 2139 } 2140 optdatalen = sizeof(struct in6_pktinfo); 2141 break; 2142 case IPV6_OTCLASS: 2143 /* XXX */ 2144 return (EINVAL); 2145 case IPV6_TCLASS: 2146 if (pktopt && pktopt->ip6po_tclass >= 0) 2147 optdata = (void *)&pktopt->ip6po_tclass; 2148 else 2149 optdata = (void *)&deftclass; 2150 optdatalen = sizeof(int); 2151 break; 2152 case IPV6_HOPOPTS: 2153 if (pktopt && pktopt->ip6po_hbh) { 2154 optdata = (void *)pktopt->ip6po_hbh; 2155 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; 2156 optdatalen = (ip6e->ip6e_len + 1) << 3; 2157 } 2158 break; 2159 case IPV6_RTHDR: 2160 if (pktopt && pktopt->ip6po_rthdr) { 2161 optdata = (void *)pktopt->ip6po_rthdr; 2162 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; 2163 optdatalen = (ip6e->ip6e_len + 1) << 3; 2164 } 2165 break; 2166 case IPV6_RTHDRDSTOPTS: 2167 if (pktopt && pktopt->ip6po_dest1) { 2168 optdata = (void *)pktopt->ip6po_dest1; 2169 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; 2170 optdatalen = (ip6e->ip6e_len + 1) << 3; 2171 } 2172 break; 2173 case IPV6_DSTOPTS: 2174 if (pktopt && pktopt->ip6po_dest2) { 2175 optdata = (void *)pktopt->ip6po_dest2; 2176 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; 2177 optdatalen = (ip6e->ip6e_len + 1) << 3; 2178 } 2179 break; 2180 case IPV6_NEXTHOP: 2181 if (pktopt && pktopt->ip6po_nexthop) { 2182 optdata = (void *)pktopt->ip6po_nexthop; 2183 optdatalen = pktopt->ip6po_nexthop->sa_len; 2184 } 2185 break; 2186 case IPV6_USE_MIN_MTU: 2187 if (pktopt) 2188 optdata = (void *)&pktopt->ip6po_minmtu; 2189 else 2190 optdata = (void *)&defminmtu; 2191 optdatalen = sizeof(int); 2192 break; 2193 case IPV6_DONTFRAG: 2194 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 2195 on = 1; 2196 else 2197 on = 0; 2198 optdata = (void *)&on; 2199 optdatalen = sizeof(on); 2200 break; 2201 case IPV6_PREFER_TEMPADDR: 2202 if (pktopt) 2203 optdata = (void *)&pktopt->ip6po_prefer_tempaddr; 2204 else 2205 optdata = (void *)&defpreftemp; 2206 optdatalen = sizeof(int); 2207 break; 2208 default: /* should not happen */ 2209 #ifdef DIAGNOSTIC 2210 panic("ip6_getpcbopt: unexpected option\n"); 2211 #endif 2212 return (ENOPROTOOPT); 2213 } 2214 2215 error = sockopt_set(sopt, optdata, optdatalen); 2216 2217 return (error); 2218 } 2219 2220 void 2221 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) 2222 { 2223 if (optname == -1 || optname == IPV6_PKTINFO) { 2224 if (pktopt->ip6po_pktinfo) 2225 free(pktopt->ip6po_pktinfo, M_IP6OPT); 2226 pktopt->ip6po_pktinfo = NULL; 2227 } 2228 if (optname == -1 || optname == IPV6_HOPLIMIT) 2229 pktopt->ip6po_hlim = -1; 2230 if (optname == -1 || optname == IPV6_TCLASS) 2231 pktopt->ip6po_tclass = -1; 2232 if (optname == -1 || optname == IPV6_NEXTHOP) { 2233 rtcache_free(&pktopt->ip6po_nextroute); 2234 if (pktopt->ip6po_nexthop) 2235 free(pktopt->ip6po_nexthop, M_IP6OPT); 2236 pktopt->ip6po_nexthop = NULL; 2237 } 2238 if (optname == -1 || optname == IPV6_HOPOPTS) { 2239 if (pktopt->ip6po_hbh) 2240 free(pktopt->ip6po_hbh, M_IP6OPT); 2241 pktopt->ip6po_hbh = NULL; 2242 } 2243 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 2244 if (pktopt->ip6po_dest1) 2245 free(pktopt->ip6po_dest1, M_IP6OPT); 2246 pktopt->ip6po_dest1 = NULL; 2247 } 2248 if (optname == -1 || optname == IPV6_RTHDR) { 2249 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 2250 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 2251 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 2252 rtcache_free(&pktopt->ip6po_route); 2253 } 2254 if (optname == -1 || optname == IPV6_DSTOPTS) { 2255 if (pktopt->ip6po_dest2) 2256 free(pktopt->ip6po_dest2, M_IP6OPT); 2257 pktopt->ip6po_dest2 = NULL; 2258 } 2259 } 2260 2261 #define PKTOPT_EXTHDRCPY(type) \ 2262 do { \ 2263 if (src->type) { \ 2264 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 2265 dst->type = malloc(hlen, M_IP6OPT, canwait); \ 2266 if (dst->type == NULL) \ 2267 goto bad; \ 2268 memcpy(dst->type, src->type, hlen); \ 2269 } \ 2270 } while (/*CONSTCOND*/ 0) 2271 2272 static int 2273 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) 2274 { 2275 dst->ip6po_hlim = src->ip6po_hlim; 2276 dst->ip6po_tclass = src->ip6po_tclass; 2277 dst->ip6po_flags = src->ip6po_flags; 2278 dst->ip6po_minmtu = src->ip6po_minmtu; 2279 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr; 2280 if (src->ip6po_pktinfo) { 2281 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2282 M_IP6OPT, canwait); 2283 if (dst->ip6po_pktinfo == NULL) 2284 goto bad; 2285 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2286 } 2287 if (src->ip6po_nexthop) { 2288 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2289 M_IP6OPT, canwait); 2290 if (dst->ip6po_nexthop == NULL) 2291 goto bad; 2292 memcpy(dst->ip6po_nexthop, src->ip6po_nexthop, 2293 src->ip6po_nexthop->sa_len); 2294 } 2295 PKTOPT_EXTHDRCPY(ip6po_hbh); 2296 PKTOPT_EXTHDRCPY(ip6po_dest1); 2297 PKTOPT_EXTHDRCPY(ip6po_dest2); 2298 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2299 return (0); 2300 2301 bad: 2302 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT); 2303 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT); 2304 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT); 2305 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT); 2306 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT); 2307 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT); 2308 2309 return (ENOBUFS); 2310 } 2311 #undef PKTOPT_EXTHDRCPY 2312 2313 struct ip6_pktopts * 2314 ip6_copypktopts(struct ip6_pktopts *src, int canwait) 2315 { 2316 int error; 2317 struct ip6_pktopts *dst; 2318 2319 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2320 if (dst == NULL) 2321 return (NULL); 2322 ip6_initpktopts(dst); 2323 2324 if ((error = copypktopts(dst, src, canwait)) != 0) { 2325 free(dst, M_IP6OPT); 2326 return (NULL); 2327 } 2328 2329 return (dst); 2330 } 2331 2332 void 2333 ip6_freepcbopts(struct ip6_pktopts *pktopt) 2334 { 2335 if (pktopt == NULL) 2336 return; 2337 2338 ip6_clearpktopts(pktopt, -1); 2339 2340 free(pktopt, M_IP6OPT); 2341 } 2342 2343 int 2344 ip6_get_membership(const struct sockopt *sopt, struct ifnet **ifp, 2345 struct psref *psref, void *v, size_t l) 2346 { 2347 struct ipv6_mreq mreq; 2348 int error; 2349 struct in6_addr *ia = &mreq.ipv6mr_multiaddr; 2350 struct in_addr *ia4 = (void *)&ia->s6_addr32[3]; 2351 2352 error = sockopt_get(sopt, &mreq, sizeof(mreq)); 2353 if (error != 0) 2354 return error; 2355 2356 if (IN6_IS_ADDR_UNSPECIFIED(ia)) { 2357 /* 2358 * We use the unspecified address to specify to accept 2359 * all multicast addresses. Only super user is allowed 2360 * to do this. 2361 */ 2362 if (kauth_authorize_network(kauth_cred_get(), 2363 KAUTH_NETWORK_IPV6, 2364 KAUTH_REQ_NETWORK_IPV6_JOIN_MULTICAST, NULL, NULL, NULL)) 2365 return EACCES; 2366 } else if (IN6_IS_ADDR_V4MAPPED(ia)) { 2367 // Don't bother if we are not going to use ifp. 2368 if (l == sizeof(*ia)) { 2369 memcpy(v, ia, l); 2370 return 0; 2371 } 2372 } else if (!IN6_IS_ADDR_MULTICAST(ia)) { 2373 return EINVAL; 2374 } 2375 2376 /* 2377 * If no interface was explicitly specified, choose an 2378 * appropriate one according to the given multicast address. 2379 */ 2380 if (mreq.ipv6mr_interface == 0) { 2381 struct rtentry *rt; 2382 union { 2383 struct sockaddr dst; 2384 struct sockaddr_in dst4; 2385 struct sockaddr_in6 dst6; 2386 } u; 2387 struct route ro; 2388 2389 /* 2390 * Look up the routing table for the 2391 * address, and choose the outgoing interface. 2392 * XXX: is it a good approach? 2393 */ 2394 memset(&ro, 0, sizeof(ro)); 2395 if (IN6_IS_ADDR_V4MAPPED(ia)) 2396 sockaddr_in_init(&u.dst4, ia4, 0); 2397 else 2398 sockaddr_in6_init(&u.dst6, ia, 0, 0, 0); 2399 error = rtcache_setdst(&ro, &u.dst); 2400 if (error != 0) 2401 return error; 2402 rt = rtcache_init(&ro); 2403 *ifp = rt != NULL ? 2404 if_get_byindex(rt->rt_ifp->if_index, psref) : NULL; 2405 rtcache_unref(rt, &ro); 2406 rtcache_free(&ro); 2407 } else { 2408 /* 2409 * If the interface is specified, validate it. 2410 */ 2411 *ifp = if_get_byindex(mreq.ipv6mr_interface, psref); 2412 if (*ifp == NULL) 2413 return ENXIO; /* XXX EINVAL? */ 2414 } 2415 if (sizeof(*ia) == l) 2416 memcpy(v, ia, l); 2417 else 2418 memcpy(v, ia4, l); 2419 return 0; 2420 } 2421 2422 /* 2423 * Set the IP6 multicast options in response to user setsockopt(). 2424 */ 2425 static int 2426 ip6_setmoptions(const struct sockopt *sopt, struct in6pcb *in6p) 2427 { 2428 int error = 0; 2429 u_int loop, ifindex; 2430 struct ipv6_mreq mreq; 2431 struct in6_addr ia; 2432 struct ifnet *ifp; 2433 struct ip6_moptions *im6o = in6p->in6p_moptions; 2434 struct in6_multi_mship *imm; 2435 2436 KASSERT(in6p_locked(in6p)); 2437 2438 if (im6o == NULL) { 2439 /* 2440 * No multicast option buffer attached to the pcb; 2441 * allocate one and initialize to default values. 2442 */ 2443 im6o = malloc(sizeof(*im6o), M_IPMOPTS, M_NOWAIT); 2444 if (im6o == NULL) 2445 return (ENOBUFS); 2446 in6p->in6p_moptions = im6o; 2447 im6o->im6o_multicast_if_index = 0; 2448 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2449 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 2450 LIST_INIT(&im6o->im6o_memberships); 2451 } 2452 2453 switch (sopt->sopt_name) { 2454 2455 case IPV6_MULTICAST_IF: { 2456 int s; 2457 /* 2458 * Select the interface for outgoing multicast packets. 2459 */ 2460 error = sockopt_get(sopt, &ifindex, sizeof(ifindex)); 2461 if (error != 0) 2462 break; 2463 2464 s = pserialize_read_enter(); 2465 if (ifindex != 0) { 2466 if ((ifp = if_byindex(ifindex)) == NULL) { 2467 pserialize_read_exit(s); 2468 error = ENXIO; /* XXX EINVAL? */ 2469 break; 2470 } 2471 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 2472 pserialize_read_exit(s); 2473 error = EADDRNOTAVAIL; 2474 break; 2475 } 2476 } else 2477 ifp = NULL; 2478 im6o->im6o_multicast_if_index = if_get_index(ifp); 2479 pserialize_read_exit(s); 2480 break; 2481 } 2482 2483 case IPV6_MULTICAST_HOPS: 2484 { 2485 /* 2486 * Set the IP6 hoplimit for outgoing multicast packets. 2487 */ 2488 int optval; 2489 2490 error = sockopt_getint(sopt, &optval); 2491 if (error != 0) 2492 break; 2493 2494 if (optval < -1 || optval >= 256) 2495 error = EINVAL; 2496 else if (optval == -1) 2497 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2498 else 2499 im6o->im6o_multicast_hlim = optval; 2500 break; 2501 } 2502 2503 case IPV6_MULTICAST_LOOP: 2504 /* 2505 * Set the loopback flag for outgoing multicast packets. 2506 * Must be zero or one. 2507 */ 2508 error = sockopt_get(sopt, &loop, sizeof(loop)); 2509 if (error != 0) 2510 break; 2511 if (loop > 1) { 2512 error = EINVAL; 2513 break; 2514 } 2515 im6o->im6o_multicast_loop = loop; 2516 break; 2517 2518 case IPV6_JOIN_GROUP: { 2519 int bound; 2520 struct psref psref; 2521 /* 2522 * Add a multicast group membership. 2523 * Group must be a valid IP6 multicast address. 2524 */ 2525 bound = curlwp_bind(); 2526 ifp = NULL; 2527 error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia)); 2528 if (error != 0) { 2529 KASSERT(ifp == NULL); 2530 curlwp_bindx(bound); 2531 return error; 2532 } 2533 2534 if (IN6_IS_ADDR_V4MAPPED(&ia)) { 2535 error = ip_setmoptions(&in6p->in6p_v4moptions, sopt); 2536 goto put_break; 2537 } 2538 /* 2539 * See if we found an interface, and confirm that it 2540 * supports multicast 2541 */ 2542 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2543 error = EADDRNOTAVAIL; 2544 goto put_break; 2545 } 2546 2547 if (in6_setscope(&ia, ifp, NULL)) { 2548 error = EADDRNOTAVAIL; /* XXX: should not happen */ 2549 goto put_break; 2550 } 2551 2552 /* 2553 * See if the membership already exists. 2554 */ 2555 LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) { 2556 if (imm->i6mm_maddr->in6m_ifp == ifp && 2557 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2558 &ia)) 2559 goto put_break; 2560 } 2561 if (imm != NULL) { 2562 error = EADDRINUSE; 2563 goto put_break; 2564 } 2565 /* 2566 * Everything looks good; add a new record to the multicast 2567 * address list for the given interface. 2568 */ 2569 imm = in6_joingroup(ifp, &ia, &error, 0); 2570 if (imm == NULL) 2571 goto put_break; 2572 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2573 put_break: 2574 if_put(ifp, &psref); 2575 curlwp_bindx(bound); 2576 break; 2577 } 2578 2579 case IPV6_LEAVE_GROUP: { 2580 /* 2581 * Drop a multicast group membership. 2582 * Group must be a valid IP6 multicast address. 2583 */ 2584 error = sockopt_get(sopt, &mreq, sizeof(mreq)); 2585 if (error != 0) 2586 break; 2587 2588 if (IN6_IS_ADDR_V4MAPPED(&mreq.ipv6mr_multiaddr)) { 2589 error = ip_setmoptions(&in6p->in6p_v4moptions, sopt); 2590 break; 2591 } 2592 /* 2593 * If an interface address was specified, get a pointer 2594 * to its ifnet structure. 2595 */ 2596 if (mreq.ipv6mr_interface != 0) { 2597 if ((ifp = if_byindex(mreq.ipv6mr_interface)) == NULL) { 2598 error = ENXIO; /* XXX EINVAL? */ 2599 break; 2600 } 2601 } else 2602 ifp = NULL; 2603 2604 /* Fill in the scope zone ID */ 2605 if (ifp) { 2606 if (in6_setscope(&mreq.ipv6mr_multiaddr, ifp, NULL)) { 2607 /* XXX: should not happen */ 2608 error = EADDRNOTAVAIL; 2609 break; 2610 } 2611 } else if (mreq.ipv6mr_interface != 0) { 2612 /* 2613 * XXX: This case would happens when the (positive) 2614 * index is in the valid range, but the corresponding 2615 * interface has been detached dynamically. The above 2616 * check probably avoids such case to happen here, but 2617 * we check it explicitly for safety. 2618 */ 2619 error = EADDRNOTAVAIL; 2620 break; 2621 } else { /* ipv6mr_interface == 0 */ 2622 struct sockaddr_in6 sa6_mc; 2623 2624 /* 2625 * The API spec says as follows: 2626 * If the interface index is specified as 0, the 2627 * system may choose a multicast group membership to 2628 * drop by matching the multicast address only. 2629 * On the other hand, we cannot disambiguate the scope 2630 * zone unless an interface is provided. Thus, we 2631 * check if there's ambiguity with the default scope 2632 * zone as the last resort. 2633 */ 2634 sockaddr_in6_init(&sa6_mc, &mreq.ipv6mr_multiaddr, 2635 0, 0, 0); 2636 error = sa6_embedscope(&sa6_mc, ip6_use_defzone); 2637 if (error != 0) 2638 break; 2639 mreq.ipv6mr_multiaddr = sa6_mc.sin6_addr; 2640 } 2641 2642 /* 2643 * Find the membership in the membership list. 2644 */ 2645 LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) { 2646 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) && 2647 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2648 &mreq.ipv6mr_multiaddr)) 2649 break; 2650 } 2651 if (imm == NULL) { 2652 /* Unable to resolve interface */ 2653 error = EADDRNOTAVAIL; 2654 break; 2655 } 2656 /* 2657 * Give up the multicast address record to which the 2658 * membership points. 2659 */ 2660 LIST_REMOVE(imm, i6mm_chain); 2661 in6_leavegroup(imm); 2662 /* in6m_ifp should not leave thanks to in6p_lock */ 2663 break; 2664 } 2665 2666 default: 2667 error = EOPNOTSUPP; 2668 break; 2669 } 2670 2671 /* 2672 * If all options have default values, no need to keep the mbuf. 2673 */ 2674 if (im6o->im6o_multicast_if_index == 0 && 2675 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2676 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2677 LIST_EMPTY(&im6o->im6o_memberships)) { 2678 free(in6p->in6p_moptions, M_IPMOPTS); 2679 in6p->in6p_moptions = NULL; 2680 } 2681 2682 return (error); 2683 } 2684 2685 /* 2686 * Return the IP6 multicast options in response to user getsockopt(). 2687 */ 2688 static int 2689 ip6_getmoptions(struct sockopt *sopt, struct in6pcb *in6p) 2690 { 2691 u_int optval; 2692 int error; 2693 struct ip6_moptions *im6o = in6p->in6p_moptions; 2694 2695 switch (sopt->sopt_name) { 2696 case IPV6_MULTICAST_IF: 2697 if (im6o == NULL || im6o->im6o_multicast_if_index == 0) 2698 optval = 0; 2699 else 2700 optval = im6o->im6o_multicast_if_index; 2701 2702 error = sockopt_set(sopt, &optval, sizeof(optval)); 2703 break; 2704 2705 case IPV6_MULTICAST_HOPS: 2706 if (im6o == NULL) 2707 optval = ip6_defmcasthlim; 2708 else 2709 optval = im6o->im6o_multicast_hlim; 2710 2711 error = sockopt_set(sopt, &optval, sizeof(optval)); 2712 break; 2713 2714 case IPV6_MULTICAST_LOOP: 2715 if (im6o == NULL) 2716 optval = IPV6_DEFAULT_MULTICAST_LOOP; 2717 else 2718 optval = im6o->im6o_multicast_loop; 2719 2720 error = sockopt_set(sopt, &optval, sizeof(optval)); 2721 break; 2722 2723 default: 2724 error = EOPNOTSUPP; 2725 } 2726 2727 return (error); 2728 } 2729 2730 /* 2731 * Discard the IP6 multicast options. 2732 */ 2733 void 2734 ip6_freemoptions(struct ip6_moptions *im6o) 2735 { 2736 struct in6_multi_mship *imm, *nimm; 2737 2738 if (im6o == NULL) 2739 return; 2740 2741 /* The owner of im6o (in6p) should be protected by solock */ 2742 LIST_FOREACH_SAFE(imm, &im6o->im6o_memberships, i6mm_chain, nimm) { 2743 LIST_REMOVE(imm, i6mm_chain); 2744 in6_leavegroup(imm); 2745 } 2746 free(im6o, M_IPMOPTS); 2747 } 2748 2749 /* 2750 * Set IPv6 outgoing packet options based on advanced API. 2751 */ 2752 int 2753 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2754 struct ip6_pktopts *stickyopt, kauth_cred_t cred, int uproto) 2755 { 2756 struct cmsghdr *cm = 0; 2757 2758 if (control == NULL || opt == NULL) 2759 return (EINVAL); 2760 2761 ip6_initpktopts(opt); 2762 if (stickyopt) { 2763 int error; 2764 2765 /* 2766 * If stickyopt is provided, make a local copy of the options 2767 * for this particular packet, then override them by ancillary 2768 * objects. 2769 * XXX: copypktopts() does not copy the cached route to a next 2770 * hop (if any). This is not very good in terms of efficiency, 2771 * but we can allow this since this option should be rarely 2772 * used. 2773 */ 2774 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2775 return (error); 2776 } 2777 2778 /* 2779 * XXX: Currently, we assume all the optional information is stored 2780 * in a single mbuf. 2781 */ 2782 if (control->m_next) 2783 return (EINVAL); 2784 2785 /* XXX if cm->cmsg_len is not aligned, control->m_len can become <0 */ 2786 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2787 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2788 int error; 2789 2790 if (control->m_len < CMSG_LEN(0)) 2791 return (EINVAL); 2792 2793 cm = mtod(control, struct cmsghdr *); 2794 if (cm->cmsg_len < CMSG_LEN(0) || cm->cmsg_len > control->m_len) 2795 return (EINVAL); 2796 if (cm->cmsg_level != IPPROTO_IPV6) 2797 continue; 2798 2799 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2800 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); 2801 if (error) 2802 return (error); 2803 } 2804 2805 return (0); 2806 } 2807 2808 /* 2809 * Set a particular packet option, as a sticky option or an ancillary data 2810 * item. "len" can be 0 only when it's a sticky option. 2811 * We have 4 cases of combination of "sticky" and "cmsg": 2812 * "sticky=0, cmsg=0": impossible 2813 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2814 * "sticky=1, cmsg=0": RFC3542 socket option 2815 * "sticky=1, cmsg=1": RFC2292 socket option 2816 */ 2817 static int 2818 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2819 kauth_cred_t cred, int sticky, int cmsg, int uproto) 2820 { 2821 int minmtupolicy; 2822 int error; 2823 2824 if (!sticky && !cmsg) { 2825 #ifdef DIAGNOSTIC 2826 printf("ip6_setpktopt: impossible case\n"); 2827 #endif 2828 return (EINVAL); 2829 } 2830 2831 /* 2832 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2833 * not be specified in the context of RFC3542. Conversely, 2834 * RFC3542 types should not be specified in the context of RFC2292. 2835 */ 2836 if (!cmsg) { 2837 switch (optname) { 2838 case IPV6_2292PKTINFO: 2839 case IPV6_2292HOPLIMIT: 2840 case IPV6_2292NEXTHOP: 2841 case IPV6_2292HOPOPTS: 2842 case IPV6_2292DSTOPTS: 2843 case IPV6_2292RTHDR: 2844 case IPV6_2292PKTOPTIONS: 2845 return (ENOPROTOOPT); 2846 } 2847 } 2848 if (sticky && cmsg) { 2849 switch (optname) { 2850 case IPV6_PKTINFO: 2851 case IPV6_HOPLIMIT: 2852 case IPV6_NEXTHOP: 2853 case IPV6_HOPOPTS: 2854 case IPV6_DSTOPTS: 2855 case IPV6_RTHDRDSTOPTS: 2856 case IPV6_RTHDR: 2857 case IPV6_USE_MIN_MTU: 2858 case IPV6_DONTFRAG: 2859 case IPV6_OTCLASS: 2860 case IPV6_TCLASS: 2861 case IPV6_PREFER_TEMPADDR: /* XXX not an RFC3542 option */ 2862 return (ENOPROTOOPT); 2863 } 2864 } 2865 2866 switch (optname) { 2867 #ifdef RFC2292 2868 case IPV6_2292PKTINFO: 2869 #endif 2870 case IPV6_PKTINFO: 2871 { 2872 struct in6_pktinfo *pktinfo; 2873 2874 if (len != sizeof(struct in6_pktinfo)) 2875 return (EINVAL); 2876 2877 pktinfo = (struct in6_pktinfo *)buf; 2878 2879 /* 2880 * An application can clear any sticky IPV6_PKTINFO option by 2881 * doing a "regular" setsockopt with ipi6_addr being 2882 * in6addr_any and ipi6_ifindex being zero. 2883 * [RFC 3542, Section 6] 2884 */ 2885 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2886 pktinfo->ipi6_ifindex == 0 && 2887 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2888 ip6_clearpktopts(opt, optname); 2889 break; 2890 } 2891 2892 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2893 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2894 return (EINVAL); 2895 } 2896 2897 /* Validate the interface index if specified. */ 2898 if (pktinfo->ipi6_ifindex) { 2899 struct ifnet *ifp; 2900 int s = pserialize_read_enter(); 2901 ifp = if_byindex(pktinfo->ipi6_ifindex); 2902 if (ifp == NULL) { 2903 pserialize_read_exit(s); 2904 return ENXIO; 2905 } 2906 pserialize_read_exit(s); 2907 } 2908 2909 /* 2910 * We store the address anyway, and let in6_selectsrc() 2911 * validate the specified address. This is because ipi6_addr 2912 * may not have enough information about its scope zone, and 2913 * we may need additional information (such as outgoing 2914 * interface or the scope zone of a destination address) to 2915 * disambiguate the scope. 2916 * XXX: the delay of the validation may confuse the 2917 * application when it is used as a sticky option. 2918 */ 2919 if (opt->ip6po_pktinfo == NULL) { 2920 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2921 M_IP6OPT, M_NOWAIT); 2922 if (opt->ip6po_pktinfo == NULL) 2923 return (ENOBUFS); 2924 } 2925 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo)); 2926 break; 2927 } 2928 2929 #ifdef RFC2292 2930 case IPV6_2292HOPLIMIT: 2931 #endif 2932 case IPV6_HOPLIMIT: 2933 { 2934 int *hlimp; 2935 2936 /* 2937 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2938 * to simplify the ordering among hoplimit options. 2939 */ 2940 if (optname == IPV6_HOPLIMIT && sticky) 2941 return (ENOPROTOOPT); 2942 2943 if (len != sizeof(int)) 2944 return (EINVAL); 2945 hlimp = (int *)buf; 2946 if (*hlimp < -1 || *hlimp > 255) 2947 return (EINVAL); 2948 2949 opt->ip6po_hlim = *hlimp; 2950 break; 2951 } 2952 2953 case IPV6_OTCLASS: 2954 if (len != sizeof(u_int8_t)) 2955 return (EINVAL); 2956 2957 opt->ip6po_tclass = *(u_int8_t *)buf; 2958 break; 2959 2960 case IPV6_TCLASS: 2961 { 2962 int tclass; 2963 2964 if (len != sizeof(int)) 2965 return (EINVAL); 2966 tclass = *(int *)buf; 2967 if (tclass < -1 || tclass > 255) 2968 return (EINVAL); 2969 2970 opt->ip6po_tclass = tclass; 2971 break; 2972 } 2973 2974 #ifdef RFC2292 2975 case IPV6_2292NEXTHOP: 2976 #endif 2977 case IPV6_NEXTHOP: 2978 error = kauth_authorize_network(cred, 2979 KAUTH_NETWORK_IPV6, 2980 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL); 2981 if (error) 2982 return (error); 2983 2984 if (len == 0) { /* just remove the option */ 2985 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2986 break; 2987 } 2988 2989 /* check if cmsg_len is large enough for sa_len */ 2990 if (len < sizeof(struct sockaddr) || len < *buf) 2991 return (EINVAL); 2992 2993 switch (((struct sockaddr *)buf)->sa_family) { 2994 case AF_INET6: 2995 { 2996 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 2997 2998 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 2999 return (EINVAL); 3000 3001 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 3002 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 3003 return (EINVAL); 3004 } 3005 if ((error = sa6_embedscope(sa6, ip6_use_defzone)) 3006 != 0) { 3007 return (error); 3008 } 3009 break; 3010 } 3011 case AF_LINK: /* eventually be supported? */ 3012 default: 3013 return (EAFNOSUPPORT); 3014 } 3015 3016 /* turn off the previous option, then set the new option. */ 3017 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3018 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 3019 if (opt->ip6po_nexthop == NULL) 3020 return (ENOBUFS); 3021 memcpy(opt->ip6po_nexthop, buf, *buf); 3022 break; 3023 3024 #ifdef RFC2292 3025 case IPV6_2292HOPOPTS: 3026 #endif 3027 case IPV6_HOPOPTS: 3028 { 3029 struct ip6_hbh *hbh; 3030 int hbhlen; 3031 3032 /* 3033 * XXX: We don't allow a non-privileged user to set ANY HbH 3034 * options, since per-option restriction has too much 3035 * overhead. 3036 */ 3037 error = kauth_authorize_network(cred, 3038 KAUTH_NETWORK_IPV6, 3039 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL); 3040 if (error) 3041 return (error); 3042 3043 if (len == 0) { 3044 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3045 break; /* just remove the option */ 3046 } 3047 3048 /* message length validation */ 3049 if (len < sizeof(struct ip6_hbh)) 3050 return (EINVAL); 3051 hbh = (struct ip6_hbh *)buf; 3052 hbhlen = (hbh->ip6h_len + 1) << 3; 3053 if (len != hbhlen) 3054 return (EINVAL); 3055 3056 /* turn off the previous option, then set the new option. */ 3057 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3058 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 3059 if (opt->ip6po_hbh == NULL) 3060 return (ENOBUFS); 3061 memcpy(opt->ip6po_hbh, hbh, hbhlen); 3062 3063 break; 3064 } 3065 3066 #ifdef RFC2292 3067 case IPV6_2292DSTOPTS: 3068 #endif 3069 case IPV6_DSTOPTS: 3070 case IPV6_RTHDRDSTOPTS: 3071 { 3072 struct ip6_dest *dest, **newdest = NULL; 3073 int destlen; 3074 3075 /* XXX: see the comment for IPV6_HOPOPTS */ 3076 error = kauth_authorize_network(cred, 3077 KAUTH_NETWORK_IPV6, 3078 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL); 3079 if (error) 3080 return (error); 3081 3082 if (len == 0) { 3083 ip6_clearpktopts(opt, optname); 3084 break; /* just remove the option */ 3085 } 3086 3087 /* message length validation */ 3088 if (len < sizeof(struct ip6_dest)) 3089 return (EINVAL); 3090 dest = (struct ip6_dest *)buf; 3091 destlen = (dest->ip6d_len + 1) << 3; 3092 if (len != destlen) 3093 return (EINVAL); 3094 /* 3095 * Determine the position that the destination options header 3096 * should be inserted; before or after the routing header. 3097 */ 3098 switch (optname) { 3099 case IPV6_2292DSTOPTS: 3100 /* 3101 * The old advanced API is ambiguous on this point. 3102 * Our approach is to determine the position based 3103 * according to the existence of a routing header. 3104 * Note, however, that this depends on the order of the 3105 * extension headers in the ancillary data; the 1st 3106 * part of the destination options header must appear 3107 * before the routing header in the ancillary data, 3108 * too. 3109 * RFC3542 solved the ambiguity by introducing 3110 * separate ancillary data or option types. 3111 */ 3112 if (opt->ip6po_rthdr == NULL) 3113 newdest = &opt->ip6po_dest1; 3114 else 3115 newdest = &opt->ip6po_dest2; 3116 break; 3117 case IPV6_RTHDRDSTOPTS: 3118 newdest = &opt->ip6po_dest1; 3119 break; 3120 case IPV6_DSTOPTS: 3121 newdest = &opt->ip6po_dest2; 3122 break; 3123 } 3124 3125 /* turn off the previous option, then set the new option. */ 3126 ip6_clearpktopts(opt, optname); 3127 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 3128 if (*newdest == NULL) 3129 return (ENOBUFS); 3130 memcpy(*newdest, dest, destlen); 3131 3132 break; 3133 } 3134 3135 #ifdef RFC2292 3136 case IPV6_2292RTHDR: 3137 #endif 3138 case IPV6_RTHDR: 3139 { 3140 struct ip6_rthdr *rth; 3141 int rthlen; 3142 3143 if (len == 0) { 3144 ip6_clearpktopts(opt, IPV6_RTHDR); 3145 break; /* just remove the option */ 3146 } 3147 3148 /* message length validation */ 3149 if (len < sizeof(struct ip6_rthdr)) 3150 return (EINVAL); 3151 rth = (struct ip6_rthdr *)buf; 3152 rthlen = (rth->ip6r_len + 1) << 3; 3153 if (len != rthlen) 3154 return (EINVAL); 3155 switch (rth->ip6r_type) { 3156 case IPV6_RTHDR_TYPE_0: 3157 /* Dropped, RFC5095. */ 3158 default: 3159 return (EINVAL); /* not supported */ 3160 } 3161 /* turn off the previous option */ 3162 ip6_clearpktopts(opt, IPV6_RTHDR); 3163 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 3164 if (opt->ip6po_rthdr == NULL) 3165 return (ENOBUFS); 3166 memcpy(opt->ip6po_rthdr, rth, rthlen); 3167 break; 3168 } 3169 3170 case IPV6_USE_MIN_MTU: 3171 if (len != sizeof(int)) 3172 return (EINVAL); 3173 minmtupolicy = *(int *)buf; 3174 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 3175 minmtupolicy != IP6PO_MINMTU_DISABLE && 3176 minmtupolicy != IP6PO_MINMTU_ALL) { 3177 return (EINVAL); 3178 } 3179 opt->ip6po_minmtu = minmtupolicy; 3180 break; 3181 3182 case IPV6_DONTFRAG: 3183 if (len != sizeof(int)) 3184 return (EINVAL); 3185 3186 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 3187 /* 3188 * we ignore this option for TCP sockets. 3189 * (RFC3542 leaves this case unspecified.) 3190 */ 3191 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 3192 } else 3193 opt->ip6po_flags |= IP6PO_DONTFRAG; 3194 break; 3195 3196 case IPV6_PREFER_TEMPADDR: 3197 { 3198 int preftemp; 3199 3200 if (len != sizeof(int)) 3201 return (EINVAL); 3202 preftemp = *(int *)buf; 3203 switch (preftemp) { 3204 case IP6PO_TEMPADDR_SYSTEM: 3205 case IP6PO_TEMPADDR_NOTPREFER: 3206 case IP6PO_TEMPADDR_PREFER: 3207 break; 3208 default: 3209 return (EINVAL); 3210 } 3211 opt->ip6po_prefer_tempaddr = preftemp; 3212 break; 3213 } 3214 3215 default: 3216 return (ENOPROTOOPT); 3217 } /* end of switch */ 3218 3219 return (0); 3220 } 3221 3222 /* 3223 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 3224 * packet to the input queue of a specified interface. Note that this 3225 * calls the output routine of the loopback "driver", but with an interface 3226 * pointer that might NOT be lo0ifp -- easier than replicating that code here. 3227 */ 3228 void 3229 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, 3230 const struct sockaddr_in6 *dst) 3231 { 3232 struct mbuf *copym; 3233 struct ip6_hdr *ip6; 3234 3235 copym = m_copypacket(m, M_DONTWAIT); 3236 if (copym == NULL) 3237 return; 3238 3239 /* 3240 * Make sure to deep-copy IPv6 header portion in case the data 3241 * is in an mbuf cluster, so that we can safely override the IPv6 3242 * header portion later. 3243 */ 3244 if ((copym->m_flags & M_EXT) != 0 || 3245 copym->m_len < sizeof(struct ip6_hdr)) { 3246 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 3247 if (copym == NULL) 3248 return; 3249 } 3250 3251 #ifdef DIAGNOSTIC 3252 if (copym->m_len < sizeof(*ip6)) { 3253 m_freem(copym); 3254 return; 3255 } 3256 #endif 3257 3258 ip6 = mtod(copym, struct ip6_hdr *); 3259 /* 3260 * clear embedded scope identifiers if necessary. 3261 * in6_clearscope will touch the addresses only when necessary. 3262 */ 3263 in6_clearscope(&ip6->ip6_src); 3264 in6_clearscope(&ip6->ip6_dst); 3265 3266 (void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL); 3267 } 3268 3269 /* 3270 * Chop IPv6 header off from the payload. 3271 */ 3272 static int 3273 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 3274 { 3275 struct mbuf *mh; 3276 struct ip6_hdr *ip6; 3277 3278 ip6 = mtod(m, struct ip6_hdr *); 3279 if (m->m_len > sizeof(*ip6)) { 3280 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 3281 if (mh == NULL) { 3282 m_freem(m); 3283 return ENOBUFS; 3284 } 3285 m_move_pkthdr(mh, m); 3286 m_align(mh, sizeof(*ip6)); 3287 m->m_len -= sizeof(*ip6); 3288 m->m_data += sizeof(*ip6); 3289 mh->m_next = m; 3290 mh->m_len = sizeof(*ip6); 3291 memcpy(mtod(mh, void *), (void *)ip6, sizeof(*ip6)); 3292 m = mh; 3293 } 3294 exthdrs->ip6e_ip6 = m; 3295 return 0; 3296 } 3297 3298 /* 3299 * Compute IPv6 extension header length. 3300 */ 3301 int 3302 ip6_optlen(struct in6pcb *in6p) 3303 { 3304 int len; 3305 3306 if (!in6p->in6p_outputopts) 3307 return 0; 3308 3309 len = 0; 3310 #define elen(x) \ 3311 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 3312 3313 len += elen(in6p->in6p_outputopts->ip6po_hbh); 3314 len += elen(in6p->in6p_outputopts->ip6po_dest1); 3315 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 3316 len += elen(in6p->in6p_outputopts->ip6po_dest2); 3317 return len; 3318 #undef elen 3319 } 3320 3321 /* 3322 * Ensure sending address is valid. 3323 * Returns 0 on success, -1 if an error should be sent back or 1 3324 * if the packet could be dropped without error (protocol dependent). 3325 */ 3326 static int 3327 ip6_ifaddrvalid(const struct in6_addr *src, const struct in6_addr *dst) 3328 { 3329 struct sockaddr_in6 sin6; 3330 int s, error; 3331 struct ifaddr *ifa; 3332 struct in6_ifaddr *ia6; 3333 3334 if (IN6_IS_ADDR_UNSPECIFIED(src)) 3335 return 0; 3336 3337 memset(&sin6, 0, sizeof(sin6)); 3338 sin6.sin6_family = AF_INET6; 3339 sin6.sin6_len = sizeof(sin6); 3340 sin6.sin6_addr = *src; 3341 3342 s = pserialize_read_enter(); 3343 ifa = ifa_ifwithaddr(sin6tosa(&sin6)); 3344 if ((ia6 = ifatoia6(ifa)) == NULL || 3345 ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) 3346 error = -1; 3347 else if (ia6->ia6_flags & IN6_IFF_TENTATIVE) 3348 error = 1; 3349 else if (ia6->ia6_flags & IN6_IFF_DETACHED && 3350 (sin6.sin6_addr = *dst, ifa_ifwithaddr(sin6tosa(&sin6)) == NULL)) 3351 /* Allow internal traffic to DETACHED addresses */ 3352 error = 1; 3353 else 3354 error = 0; 3355 pserialize_read_exit(s); 3356 3357 return error; 3358 } 3359