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