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