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