1 /* $NetBSD: ip6_output.c,v 1.123 2007/11/06 23:48:24 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.123 2007/11/06 23:48:24 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 208 #ifdef DIAGNOSTIC 209 if ((m->m_flags & M_PKTHDR) == 0) 210 panic("ip6_output: no HDR"); 211 212 if ((m->m_pkthdr.csum_flags & 213 (M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_TSOv4)) != 0) { 214 panic("ip6_output: IPv4 checksum offload flags: %d", 215 m->m_pkthdr.csum_flags); 216 } 217 218 if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 219 (M_CSUM_TCPv6|M_CSUM_UDPv6)) { 220 panic("ip6_output: conflicting checksum offload flags: %d", 221 m->m_pkthdr.csum_flags); 222 } 223 #endif 224 225 M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data, sizeof(struct ip6_hdr)); 226 227 #define MAKE_EXTHDR(hp, mp) \ 228 do { \ 229 if (hp) { \ 230 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 231 error = ip6_copyexthdr((mp), (void *)(hp), \ 232 ((eh)->ip6e_len + 1) << 3); \ 233 if (error) \ 234 goto freehdrs; \ 235 } \ 236 } while (/*CONSTCOND*/ 0) 237 238 bzero(&exthdrs, sizeof(exthdrs)); 239 if (opt) { 240 /* Hop-by-Hop options header */ 241 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 242 /* Destination options header(1st part) */ 243 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 244 /* Routing header */ 245 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 246 /* Destination options header(2nd part) */ 247 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 248 } 249 250 #ifdef IPSEC 251 if ((flags & IPV6_FORWARDING) != 0) { 252 needipsec = 0; 253 goto skippolicycheck; 254 } 255 256 /* get a security policy for this packet */ 257 if (so == NULL) 258 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error); 259 else { 260 if (IPSEC_PCB_SKIP_IPSEC(sotoinpcb_hdr(so)->inph_sp, 261 IPSEC_DIR_OUTBOUND)) { 262 needipsec = 0; 263 goto skippolicycheck; 264 } 265 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 266 } 267 268 if (sp == NULL) { 269 ipsec6stat.out_inval++; 270 goto freehdrs; 271 } 272 273 error = 0; 274 275 /* check policy */ 276 switch (sp->policy) { 277 case IPSEC_POLICY_DISCARD: 278 /* 279 * This packet is just discarded. 280 */ 281 ipsec6stat.out_polvio++; 282 goto freehdrs; 283 284 case IPSEC_POLICY_BYPASS: 285 case IPSEC_POLICY_NONE: 286 /* no need to do IPsec. */ 287 needipsec = 0; 288 break; 289 290 case IPSEC_POLICY_IPSEC: 291 if (sp->req == NULL) { 292 /* XXX should be panic ? */ 293 printf("ip6_output: No IPsec request specified.\n"); 294 error = EINVAL; 295 goto freehdrs; 296 } 297 needipsec = 1; 298 break; 299 300 case IPSEC_POLICY_ENTRUST: 301 default: 302 printf("ip6_output: Invalid policy found. %d\n", sp->policy); 303 } 304 305 skippolicycheck:; 306 #endif /* IPSEC */ 307 308 /* 309 * Calculate the total length of the extension header chain. 310 * Keep the length of the unfragmentable part for fragmentation. 311 */ 312 optlen = 0; 313 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len; 314 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len; 315 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len; 316 unfragpartlen = optlen + sizeof(struct ip6_hdr); 317 /* NOTE: we don't add AH/ESP length here. do that later. */ 318 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len; 319 320 #ifdef FAST_IPSEC 321 /* Check the security policy (SP) for the packet */ 322 323 /* XXX For moment, we doesn't support packet with extented action */ 324 if (optlen !=0) 325 goto freehdrs; 326 327 sp = ipsec6_check_policy(m,so,flags,&needipsec,&error); 328 if (error != 0) { 329 /* 330 * Hack: -EINVAL is used to signal that a packet 331 * should be silently discarded. This is typically 332 * because we asked key management for an SA and 333 * it was delayed (e.g. kicked up to IKE). 334 */ 335 if (error == -EINVAL) 336 error = 0; 337 goto freehdrs; 338 } 339 #endif /* FAST_IPSEC */ 340 341 342 if (needipsec && 343 (m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) { 344 in6_delayed_cksum(m); 345 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6); 346 } 347 348 349 /* 350 * If we need IPsec, or there is at least one extension header, 351 * separate IP6 header from the payload. 352 */ 353 if ((needipsec || optlen) && !hdrsplit) { 354 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 355 m = NULL; 356 goto freehdrs; 357 } 358 m = exthdrs.ip6e_ip6; 359 hdrsplit++; 360 } 361 362 /* adjust pointer */ 363 ip6 = mtod(m, struct ip6_hdr *); 364 365 /* adjust mbuf packet header length */ 366 m->m_pkthdr.len += optlen; 367 plen = m->m_pkthdr.len - sizeof(*ip6); 368 369 /* If this is a jumbo payload, insert a jumbo payload option. */ 370 if (plen > IPV6_MAXPACKET) { 371 if (!hdrsplit) { 372 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 373 m = NULL; 374 goto freehdrs; 375 } 376 m = exthdrs.ip6e_ip6; 377 hdrsplit++; 378 } 379 /* adjust pointer */ 380 ip6 = mtod(m, struct ip6_hdr *); 381 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 382 goto freehdrs; 383 optlen += 8; /* XXX JUMBOOPTLEN */ 384 ip6->ip6_plen = 0; 385 } else 386 ip6->ip6_plen = htons(plen); 387 388 /* 389 * Concatenate headers and fill in next header fields. 390 * Here we have, on "m" 391 * IPv6 payload 392 * and we insert headers accordingly. Finally, we should be getting: 393 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 394 * 395 * during the header composing process, "m" points to IPv6 header. 396 * "mprev" points to an extension header prior to esp. 397 */ 398 { 399 u_char *nexthdrp = &ip6->ip6_nxt; 400 struct mbuf *mprev = m; 401 402 /* 403 * we treat dest2 specially. this makes IPsec processing 404 * much easier. the goal here is to make mprev point the 405 * mbuf prior to dest2. 406 * 407 * result: IPv6 dest2 payload 408 * m and mprev will point to IPv6 header. 409 */ 410 if (exthdrs.ip6e_dest2) { 411 if (!hdrsplit) 412 panic("assumption failed: hdr not split"); 413 exthdrs.ip6e_dest2->m_next = m->m_next; 414 m->m_next = exthdrs.ip6e_dest2; 415 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 416 ip6->ip6_nxt = IPPROTO_DSTOPTS; 417 } 418 419 #define MAKE_CHAIN(m, mp, p, i)\ 420 do {\ 421 if (m) {\ 422 if (!hdrsplit) \ 423 panic("assumption failed: hdr not split"); \ 424 *mtod((m), u_char *) = *(p);\ 425 *(p) = (i);\ 426 p = mtod((m), u_char *);\ 427 (m)->m_next = (mp)->m_next;\ 428 (mp)->m_next = (m);\ 429 (mp) = (m);\ 430 }\ 431 } while (/*CONSTCOND*/ 0) 432 /* 433 * result: IPv6 hbh dest1 rthdr dest2 payload 434 * m will point to IPv6 header. mprev will point to the 435 * extension header prior to dest2 (rthdr in the above case). 436 */ 437 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 438 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 439 IPPROTO_DSTOPTS); 440 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 441 IPPROTO_ROUTING); 442 443 M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data, 444 sizeof(struct ip6_hdr) + optlen); 445 446 #ifdef IPSEC 447 if (!needipsec) 448 goto skip_ipsec2; 449 450 /* 451 * pointers after IPsec headers are not valid any more. 452 * other pointers need a great care too. 453 * (IPsec routines should not mangle mbufs prior to AH/ESP) 454 */ 455 exthdrs.ip6e_dest2 = NULL; 456 457 { 458 struct ip6_rthdr *rh = NULL; 459 int segleft_org = 0; 460 struct ipsec_output_state state; 461 462 if (exthdrs.ip6e_rthdr) { 463 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 464 segleft_org = rh->ip6r_segleft; 465 rh->ip6r_segleft = 0; 466 } 467 468 bzero(&state, sizeof(state)); 469 state.m = m; 470 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags, 471 &needipsectun); 472 m = state.m; 473 if (error) { 474 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 475 /* mbuf is already reclaimed in ipsec6_output_trans. */ 476 m = NULL; 477 switch (error) { 478 case EHOSTUNREACH: 479 case ENETUNREACH: 480 case EMSGSIZE: 481 case ENOBUFS: 482 case ENOMEM: 483 break; 484 default: 485 printf("ip6_output (ipsec): error code %d\n", error); 486 /* FALLTHROUGH */ 487 case ENOENT: 488 /* don't show these error codes to the user */ 489 error = 0; 490 break; 491 } 492 goto bad; 493 } 494 if (exthdrs.ip6e_rthdr) { 495 /* ah6_output doesn't modify mbuf chain */ 496 rh->ip6r_segleft = segleft_org; 497 } 498 } 499 skip_ipsec2:; 500 #endif 501 } 502 503 /* 504 * If there is a routing header, replace destination address field 505 * with the first hop of the routing header. 506 */ 507 if (exthdrs.ip6e_rthdr) { 508 struct ip6_rthdr *rh; 509 struct ip6_rthdr0 *rh0; 510 struct in6_addr *addr; 511 struct sockaddr_in6 sa; 512 513 rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, 514 struct ip6_rthdr *)); 515 finaldst = ip6->ip6_dst; 516 switch (rh->ip6r_type) { 517 case IPV6_RTHDR_TYPE_0: 518 rh0 = (struct ip6_rthdr0 *)rh; 519 addr = (struct in6_addr *)(rh0 + 1); 520 521 /* 522 * construct a sockaddr_in6 form of 523 * the first hop. 524 * 525 * XXX: we may not have enough 526 * information about its scope zone; 527 * there is no standard API to pass 528 * the information from the 529 * application. 530 */ 531 sockaddr_in6_init(&sa, addr, 0, 0, 0); 532 if ((error = sa6_embedscope(&sa, 533 ip6_use_defzone)) != 0) { 534 goto bad; 535 } 536 ip6->ip6_dst = sa.sin6_addr; 537 (void)memmove(&addr[0], &addr[1], 538 sizeof(struct in6_addr) * 539 (rh0->ip6r0_segleft - 1)); 540 addr[rh0->ip6r0_segleft - 1] = finaldst; 541 /* XXX */ 542 in6_clearscope(addr + rh0->ip6r0_segleft - 1); 543 break; 544 default: /* is it possible? */ 545 error = EINVAL; 546 goto bad; 547 } 548 } 549 550 /* Source address validation */ 551 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 552 (flags & IPV6_UNSPECSRC) == 0) { 553 error = EOPNOTSUPP; 554 ip6stat.ip6s_badscope++; 555 goto bad; 556 } 557 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 558 error = EOPNOTSUPP; 559 ip6stat.ip6s_badscope++; 560 goto bad; 561 } 562 563 ip6stat.ip6s_localout++; 564 565 /* 566 * Route packet. 567 */ 568 /* initialize cached route */ 569 if (ro == NULL) { 570 memset(&ip6route, 0, sizeof(ip6route)); 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 && opt->ip6po_nextroute.ro_rt != 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 /* XXX Second if is invariant? */ 1175 if (ro == &ip6route) 1176 rtcache_free(ro); 1177 else if (ro_pmtu == &ip6route) 1178 rtcache_free(ro_pmtu); 1179 1180 #ifdef IPSEC 1181 if (sp != NULL) 1182 key_freesp(sp); 1183 #endif /* IPSEC */ 1184 #ifdef FAST_IPSEC 1185 if (sp != NULL) 1186 KEY_FREESP(&sp); 1187 #endif /* FAST_IPSEC */ 1188 1189 1190 return (error); 1191 1192 freehdrs: 1193 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1194 m_freem(exthdrs.ip6e_dest1); 1195 m_freem(exthdrs.ip6e_rthdr); 1196 m_freem(exthdrs.ip6e_dest2); 1197 /* FALLTHROUGH */ 1198 bad: 1199 m_freem(m); 1200 goto done; 1201 badscope: 1202 ip6stat.ip6s_badscope++; 1203 in6_ifstat_inc(origifp, ifs6_out_discard); 1204 if (error == 0) 1205 error = EHOSTUNREACH; /* XXX */ 1206 goto bad; 1207 } 1208 1209 static int 1210 ip6_copyexthdr(struct mbuf **mp, void *hdr, int hlen) 1211 { 1212 struct mbuf *m; 1213 1214 if (hlen > MCLBYTES) 1215 return (ENOBUFS); /* XXX */ 1216 1217 MGET(m, M_DONTWAIT, MT_DATA); 1218 if (!m) 1219 return (ENOBUFS); 1220 1221 if (hlen > MLEN) { 1222 MCLGET(m, M_DONTWAIT); 1223 if ((m->m_flags & M_EXT) == 0) { 1224 m_free(m); 1225 return (ENOBUFS); 1226 } 1227 } 1228 m->m_len = hlen; 1229 if (hdr) 1230 bcopy(hdr, mtod(m, void *), hlen); 1231 1232 *mp = m; 1233 return (0); 1234 } 1235 1236 /* 1237 * Process a delayed payload checksum calculation. 1238 */ 1239 void 1240 in6_delayed_cksum(struct mbuf *m) 1241 { 1242 uint16_t csum, offset; 1243 1244 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0); 1245 KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0); 1246 KASSERT((m->m_pkthdr.csum_flags 1247 & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0); 1248 1249 offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data); 1250 csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset); 1251 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) { 1252 csum = 0xffff; 1253 } 1254 1255 offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data); 1256 if ((offset + sizeof(csum)) > m->m_len) { 1257 m_copyback(m, offset, sizeof(csum), &csum); 1258 } else { 1259 *(uint16_t *)(mtod(m, char *) + offset) = csum; 1260 } 1261 } 1262 1263 /* 1264 * Insert jumbo payload option. 1265 */ 1266 static int 1267 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) 1268 { 1269 struct mbuf *mopt; 1270 u_int8_t *optbuf; 1271 u_int32_t v; 1272 1273 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1274 1275 /* 1276 * If there is no hop-by-hop options header, allocate new one. 1277 * If there is one but it doesn't have enough space to store the 1278 * jumbo payload option, allocate a cluster to store the whole options. 1279 * Otherwise, use it to store the options. 1280 */ 1281 if (exthdrs->ip6e_hbh == 0) { 1282 MGET(mopt, M_DONTWAIT, MT_DATA); 1283 if (mopt == 0) 1284 return (ENOBUFS); 1285 mopt->m_len = JUMBOOPTLEN; 1286 optbuf = mtod(mopt, u_int8_t *); 1287 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1288 exthdrs->ip6e_hbh = mopt; 1289 } else { 1290 struct ip6_hbh *hbh; 1291 1292 mopt = exthdrs->ip6e_hbh; 1293 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1294 /* 1295 * XXX assumption: 1296 * - exthdrs->ip6e_hbh is not referenced from places 1297 * other than exthdrs. 1298 * - exthdrs->ip6e_hbh is not an mbuf chain. 1299 */ 1300 int oldoptlen = mopt->m_len; 1301 struct mbuf *n; 1302 1303 /* 1304 * XXX: give up if the whole (new) hbh header does 1305 * not fit even in an mbuf cluster. 1306 */ 1307 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1308 return (ENOBUFS); 1309 1310 /* 1311 * As a consequence, we must always prepare a cluster 1312 * at this point. 1313 */ 1314 MGET(n, M_DONTWAIT, MT_DATA); 1315 if (n) { 1316 MCLGET(n, M_DONTWAIT); 1317 if ((n->m_flags & M_EXT) == 0) { 1318 m_freem(n); 1319 n = NULL; 1320 } 1321 } 1322 if (!n) 1323 return (ENOBUFS); 1324 n->m_len = oldoptlen + JUMBOOPTLEN; 1325 bcopy(mtod(mopt, void *), mtod(n, void *), 1326 oldoptlen); 1327 optbuf = mtod(n, u_int8_t *) + oldoptlen; 1328 m_freem(mopt); 1329 mopt = exthdrs->ip6e_hbh = n; 1330 } else { 1331 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len; 1332 mopt->m_len += JUMBOOPTLEN; 1333 } 1334 optbuf[0] = IP6OPT_PADN; 1335 optbuf[1] = 0; 1336 1337 /* 1338 * Adjust the header length according to the pad and 1339 * the jumbo payload option. 1340 */ 1341 hbh = mtod(mopt, struct ip6_hbh *); 1342 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1343 } 1344 1345 /* fill in the option. */ 1346 optbuf[2] = IP6OPT_JUMBO; 1347 optbuf[3] = 4; 1348 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1349 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1350 1351 /* finally, adjust the packet header length */ 1352 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1353 1354 return (0); 1355 #undef JUMBOOPTLEN 1356 } 1357 1358 /* 1359 * Insert fragment header and copy unfragmentable header portions. 1360 */ 1361 static int 1362 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, 1363 struct ip6_frag **frghdrp) 1364 { 1365 struct mbuf *n, *mlast; 1366 1367 if (hlen > sizeof(struct ip6_hdr)) { 1368 n = m_copym(m0, sizeof(struct ip6_hdr), 1369 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1370 if (n == 0) 1371 return (ENOBUFS); 1372 m->m_next = n; 1373 } else 1374 n = m; 1375 1376 /* Search for the last mbuf of unfragmentable part. */ 1377 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1378 ; 1379 1380 if ((mlast->m_flags & M_EXT) == 0 && 1381 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1382 /* use the trailing space of the last mbuf for the fragment hdr */ 1383 *frghdrp = (struct ip6_frag *)(mtod(mlast, char *) + 1384 mlast->m_len); 1385 mlast->m_len += sizeof(struct ip6_frag); 1386 m->m_pkthdr.len += sizeof(struct ip6_frag); 1387 } else { 1388 /* allocate a new mbuf for the fragment header */ 1389 struct mbuf *mfrg; 1390 1391 MGET(mfrg, M_DONTWAIT, MT_DATA); 1392 if (mfrg == 0) 1393 return (ENOBUFS); 1394 mfrg->m_len = sizeof(struct ip6_frag); 1395 *frghdrp = mtod(mfrg, struct ip6_frag *); 1396 mlast->m_next = mfrg; 1397 } 1398 1399 return (0); 1400 } 1401 1402 static int 1403 ip6_getpmtu(struct route *ro_pmtu, struct route *ro, struct ifnet *ifp, 1404 const struct in6_addr *dst, u_long *mtup, int *alwaysfragp) 1405 { 1406 u_int32_t mtu = 0; 1407 int alwaysfrag = 0; 1408 int error = 0; 1409 1410 if (ro_pmtu != ro) { 1411 union { 1412 struct sockaddr dst; 1413 struct sockaddr_in6 dst6; 1414 } u; 1415 1416 /* The first hop and the final destination may differ. */ 1417 sockaddr_in6_init(&u.dst6, dst, 0, 0, 0); 1418 rtcache_lookup(ro_pmtu, &u.dst); 1419 } 1420 if (ro_pmtu->ro_rt != NULL) { 1421 u_int32_t ifmtu; 1422 1423 if (ifp == NULL) 1424 ifp = ro_pmtu->ro_rt->rt_ifp; 1425 ifmtu = IN6_LINKMTU(ifp); 1426 mtu = ro_pmtu->ro_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 (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU)) 1451 ro_pmtu->ro_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 union { 2590 struct sockaddr dst; 2591 struct sockaddr_in6 dst6; 2592 } u; 2593 2594 /* 2595 * Look up the routing table for the 2596 * address, and choose the outgoing interface. 2597 * XXX: is it a good approach? 2598 */ 2599 memset(&ro, 0, sizeof(ro)); 2600 sockaddr_in6_init(&u.dst6, &mreq->ipv6mr_multiaddr, 0, 2601 0, 0); 2602 rtcache_setdst(&ro, &u.dst); 2603 rtcache_init(&ro); 2604 ifp = (ro.ro_rt != NULL) ? ro.ro_rt->rt_ifp : NULL; 2605 rtcache_free(&ro); 2606 } else { 2607 /* 2608 * If the interface is specified, validate it. 2609 */ 2610 if (if_indexlim <= mreq->ipv6mr_interface || 2611 !ifindex2ifnet[mreq->ipv6mr_interface]) { 2612 error = ENXIO; /* XXX EINVAL? */ 2613 break; 2614 } 2615 ifp = ifindex2ifnet[mreq->ipv6mr_interface]; 2616 } 2617 2618 /* 2619 * See if we found an interface, and confirm that it 2620 * supports multicast 2621 */ 2622 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2623 error = EADDRNOTAVAIL; 2624 break; 2625 } 2626 2627 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) { 2628 error = EADDRNOTAVAIL; /* XXX: should not happen */ 2629 break; 2630 } 2631 2632 /* 2633 * See if the membership already exists. 2634 */ 2635 for (imm = im6o->im6o_memberships.lh_first; 2636 imm != NULL; imm = imm->i6mm_chain.le_next) 2637 if (imm->i6mm_maddr->in6m_ifp == ifp && 2638 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2639 &mreq->ipv6mr_multiaddr)) 2640 break; 2641 if (imm != NULL) { 2642 error = EADDRINUSE; 2643 break; 2644 } 2645 /* 2646 * Everything looks good; add a new record to the multicast 2647 * address list for the given interface. 2648 */ 2649 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0); 2650 if (imm == NULL) 2651 break; 2652 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2653 break; 2654 2655 case IPV6_LEAVE_GROUP: 2656 /* 2657 * Drop a multicast group membership. 2658 * Group must be a valid IP6 multicast address. 2659 */ 2660 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2661 error = EINVAL; 2662 break; 2663 } 2664 mreq = mtod(m, struct ipv6_mreq *); 2665 2666 /* 2667 * If an interface address was specified, get a pointer 2668 * to its ifnet structure. 2669 */ 2670 if (mreq->ipv6mr_interface != 0) { 2671 if (if_indexlim <= mreq->ipv6mr_interface || 2672 !ifindex2ifnet[mreq->ipv6mr_interface]) { 2673 error = ENXIO; /* XXX EINVAL? */ 2674 break; 2675 } 2676 ifp = ifindex2ifnet[mreq->ipv6mr_interface]; 2677 } else 2678 ifp = NULL; 2679 2680 /* Fill in the scope zone ID */ 2681 if (ifp) { 2682 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) { 2683 /* XXX: should not happen */ 2684 error = EADDRNOTAVAIL; 2685 break; 2686 } 2687 } else if (mreq->ipv6mr_interface != 0) { 2688 /* 2689 * XXX: This case would happens when the (positive) 2690 * index is in the valid range, but the corresponding 2691 * interface has been detached dynamically. The above 2692 * check probably avoids such case to happen here, but 2693 * we check it explicitly for safety. 2694 */ 2695 error = EADDRNOTAVAIL; 2696 break; 2697 } else { /* ipv6mr_interface == 0 */ 2698 struct sockaddr_in6 sa6_mc; 2699 2700 /* 2701 * The API spec says as follows: 2702 * If the interface index is specified as 0, the 2703 * system may choose a multicast group membership to 2704 * drop by matching the multicast address only. 2705 * On the other hand, we cannot disambiguate the scope 2706 * zone unless an interface is provided. Thus, we 2707 * check if there's ambiguity with the default scope 2708 * zone as the last resort. 2709 */ 2710 sockaddr_in6_init(&sa6_mc, &mreq->ipv6mr_multiaddr, 2711 0, 0, 0); 2712 error = sa6_embedscope(&sa6_mc, ip6_use_defzone); 2713 if (error != 0) 2714 break; 2715 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr; 2716 } 2717 2718 /* 2719 * Find the membership in the membership list. 2720 */ 2721 for (imm = im6o->im6o_memberships.lh_first; 2722 imm != NULL; imm = imm->i6mm_chain.le_next) { 2723 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) && 2724 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2725 &mreq->ipv6mr_multiaddr)) 2726 break; 2727 } 2728 if (imm == NULL) { 2729 /* Unable to resolve interface */ 2730 error = EADDRNOTAVAIL; 2731 break; 2732 } 2733 /* 2734 * Give up the multicast address record to which the 2735 * membership points. 2736 */ 2737 LIST_REMOVE(imm, i6mm_chain); 2738 in6_leavegroup(imm); 2739 break; 2740 2741 default: 2742 error = EOPNOTSUPP; 2743 break; 2744 } 2745 2746 /* 2747 * If all options have default values, no need to keep the mbuf. 2748 */ 2749 if (im6o->im6o_multicast_ifp == NULL && 2750 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2751 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2752 im6o->im6o_memberships.lh_first == NULL) { 2753 free(*im6op, M_IPMOPTS); 2754 *im6op = NULL; 2755 } 2756 2757 return (error); 2758 } 2759 2760 /* 2761 * Return the IP6 multicast options in response to user getsockopt(). 2762 */ 2763 static int 2764 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp) 2765 { 2766 u_int *hlim, *loop, *ifindex; 2767 2768 *mp = m_get(M_WAIT, MT_SOOPTS); 2769 2770 switch (optname) { 2771 2772 case IPV6_MULTICAST_IF: 2773 ifindex = mtod(*mp, u_int *); 2774 (*mp)->m_len = sizeof(u_int); 2775 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2776 *ifindex = 0; 2777 else 2778 *ifindex = im6o->im6o_multicast_ifp->if_index; 2779 return (0); 2780 2781 case IPV6_MULTICAST_HOPS: 2782 hlim = mtod(*mp, u_int *); 2783 (*mp)->m_len = sizeof(u_int); 2784 if (im6o == NULL) 2785 *hlim = ip6_defmcasthlim; 2786 else 2787 *hlim = im6o->im6o_multicast_hlim; 2788 return (0); 2789 2790 case IPV6_MULTICAST_LOOP: 2791 loop = mtod(*mp, u_int *); 2792 (*mp)->m_len = sizeof(u_int); 2793 if (im6o == NULL) 2794 *loop = ip6_defmcasthlim; 2795 else 2796 *loop = im6o->im6o_multicast_loop; 2797 return (0); 2798 2799 default: 2800 return (EOPNOTSUPP); 2801 } 2802 } 2803 2804 /* 2805 * Discard the IP6 multicast options. 2806 */ 2807 void 2808 ip6_freemoptions(struct ip6_moptions *im6o) 2809 { 2810 struct in6_multi_mship *imm; 2811 2812 if (im6o == NULL) 2813 return; 2814 2815 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2816 LIST_REMOVE(imm, i6mm_chain); 2817 in6_leavegroup(imm); 2818 } 2819 free(im6o, M_IPMOPTS); 2820 } 2821 2822 /* 2823 * Set IPv6 outgoing packet options based on advanced API. 2824 */ 2825 int 2826 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2827 struct ip6_pktopts *stickyopt, int priv, int uproto) 2828 { 2829 struct cmsghdr *cm = 0; 2830 2831 if (control == NULL || opt == NULL) 2832 return (EINVAL); 2833 2834 ip6_initpktopts(opt); 2835 if (stickyopt) { 2836 int error; 2837 2838 /* 2839 * If stickyopt is provided, make a local copy of the options 2840 * for this particular packet, then override them by ancillary 2841 * objects. 2842 * XXX: copypktopts() does not copy the cached route to a next 2843 * hop (if any). This is not very good in terms of efficiency, 2844 * but we can allow this since this option should be rarely 2845 * used. 2846 */ 2847 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2848 return (error); 2849 } 2850 2851 /* 2852 * XXX: Currently, we assume all the optional information is stored 2853 * in a single mbuf. 2854 */ 2855 if (control->m_next) 2856 return (EINVAL); 2857 2858 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2859 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2860 int error; 2861 2862 if (control->m_len < CMSG_LEN(0)) 2863 return (EINVAL); 2864 2865 cm = mtod(control, struct cmsghdr *); 2866 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2867 return (EINVAL); 2868 if (cm->cmsg_level != IPPROTO_IPV6) 2869 continue; 2870 2871 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2872 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto); 2873 if (error) 2874 return (error); 2875 } 2876 2877 return (0); 2878 } 2879 2880 /* 2881 * Set a particular packet option, as a sticky option or an ancillary data 2882 * item. "len" can be 0 only when it's a sticky option. 2883 * We have 4 cases of combination of "sticky" and "cmsg": 2884 * "sticky=0, cmsg=0": impossible 2885 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2886 * "sticky=1, cmsg=0": RFC3542 socket option 2887 * "sticky=1, cmsg=1": RFC2292 socket option 2888 */ 2889 static int 2890 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2891 int priv, int sticky, int cmsg, int uproto) 2892 { 2893 int minmtupolicy; 2894 2895 if (!sticky && !cmsg) { 2896 #ifdef DIAGNOSTIC 2897 printf("ip6_setpktopt: impossible case\n"); 2898 #endif 2899 return (EINVAL); 2900 } 2901 2902 /* 2903 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2904 * not be specified in the context of RFC3542. Conversely, 2905 * RFC3542 types should not be specified in the context of RFC2292. 2906 */ 2907 if (!cmsg) { 2908 switch (optname) { 2909 case IPV6_2292PKTINFO: 2910 case IPV6_2292HOPLIMIT: 2911 case IPV6_2292NEXTHOP: 2912 case IPV6_2292HOPOPTS: 2913 case IPV6_2292DSTOPTS: 2914 case IPV6_2292RTHDR: 2915 case IPV6_2292PKTOPTIONS: 2916 return (ENOPROTOOPT); 2917 } 2918 } 2919 if (sticky && cmsg) { 2920 switch (optname) { 2921 case IPV6_PKTINFO: 2922 case IPV6_HOPLIMIT: 2923 case IPV6_NEXTHOP: 2924 case IPV6_HOPOPTS: 2925 case IPV6_DSTOPTS: 2926 case IPV6_RTHDRDSTOPTS: 2927 case IPV6_RTHDR: 2928 case IPV6_USE_MIN_MTU: 2929 case IPV6_DONTFRAG: 2930 case IPV6_OTCLASS: 2931 case IPV6_TCLASS: 2932 return (ENOPROTOOPT); 2933 } 2934 } 2935 2936 switch (optname) { 2937 #ifdef RFC2292 2938 case IPV6_2292PKTINFO: 2939 #endif 2940 case IPV6_PKTINFO: 2941 { 2942 struct ifnet *ifp = NULL; 2943 struct in6_pktinfo *pktinfo; 2944 2945 if (len != sizeof(struct in6_pktinfo)) 2946 return (EINVAL); 2947 2948 pktinfo = (struct in6_pktinfo *)buf; 2949 2950 /* 2951 * An application can clear any sticky IPV6_PKTINFO option by 2952 * doing a "regular" setsockopt with ipi6_addr being 2953 * in6addr_any and ipi6_ifindex being zero. 2954 * [RFC 3542, Section 6] 2955 */ 2956 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2957 pktinfo->ipi6_ifindex == 0 && 2958 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2959 ip6_clearpktopts(opt, optname); 2960 break; 2961 } 2962 2963 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2964 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2965 return (EINVAL); 2966 } 2967 2968 /* validate the interface index if specified. */ 2969 if (pktinfo->ipi6_ifindex >= if_indexlim) { 2970 return (ENXIO); 2971 } 2972 if (pktinfo->ipi6_ifindex) { 2973 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex]; 2974 if (ifp == NULL) 2975 return (ENXIO); 2976 } 2977 2978 /* 2979 * We store the address anyway, and let in6_selectsrc() 2980 * validate the specified address. This is because ipi6_addr 2981 * may not have enough information about its scope zone, and 2982 * we may need additional information (such as outgoing 2983 * interface or the scope zone of a destination address) to 2984 * disambiguate the scope. 2985 * XXX: the delay of the validation may confuse the 2986 * application when it is used as a sticky option. 2987 */ 2988 if (opt->ip6po_pktinfo == NULL) { 2989 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2990 M_IP6OPT, M_NOWAIT); 2991 if (opt->ip6po_pktinfo == NULL) 2992 return (ENOBUFS); 2993 } 2994 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo)); 2995 break; 2996 } 2997 2998 #ifdef RFC2292 2999 case IPV6_2292HOPLIMIT: 3000 #endif 3001 case IPV6_HOPLIMIT: 3002 { 3003 int *hlimp; 3004 3005 /* 3006 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 3007 * to simplify the ordering among hoplimit options. 3008 */ 3009 if (optname == IPV6_HOPLIMIT && sticky) 3010 return (ENOPROTOOPT); 3011 3012 if (len != sizeof(int)) 3013 return (EINVAL); 3014 hlimp = (int *)buf; 3015 if (*hlimp < -1 || *hlimp > 255) 3016 return (EINVAL); 3017 3018 opt->ip6po_hlim = *hlimp; 3019 break; 3020 } 3021 3022 case IPV6_OTCLASS: 3023 if (len != sizeof(u_int8_t)) 3024 return (EINVAL); 3025 3026 opt->ip6po_tclass = *(u_int8_t *)buf; 3027 break; 3028 3029 case IPV6_TCLASS: 3030 { 3031 int tclass; 3032 3033 if (len != sizeof(int)) 3034 return (EINVAL); 3035 tclass = *(int *)buf; 3036 if (tclass < -1 || tclass > 255) 3037 return (EINVAL); 3038 3039 opt->ip6po_tclass = tclass; 3040 break; 3041 } 3042 3043 #ifdef RFC2292 3044 case IPV6_2292NEXTHOP: 3045 #endif 3046 case IPV6_NEXTHOP: 3047 if (!priv) 3048 return (EPERM); 3049 3050 if (len == 0) { /* just remove the option */ 3051 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3052 break; 3053 } 3054 3055 /* check if cmsg_len is large enough for sa_len */ 3056 if (len < sizeof(struct sockaddr) || len < *buf) 3057 return (EINVAL); 3058 3059 switch (((struct sockaddr *)buf)->sa_family) { 3060 case AF_INET6: 3061 { 3062 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 3063 int error; 3064 3065 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 3066 return (EINVAL); 3067 3068 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 3069 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 3070 return (EINVAL); 3071 } 3072 if ((error = sa6_embedscope(sa6, ip6_use_defzone)) 3073 != 0) { 3074 return (error); 3075 } 3076 break; 3077 } 3078 case AF_LINK: /* eventually be supported? */ 3079 default: 3080 return (EAFNOSUPPORT); 3081 } 3082 3083 /* turn off the previous option, then set the new option. */ 3084 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3085 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 3086 if (opt->ip6po_nexthop == NULL) 3087 return (ENOBUFS); 3088 memcpy(opt->ip6po_nexthop, buf, *buf); 3089 break; 3090 3091 #ifdef RFC2292 3092 case IPV6_2292HOPOPTS: 3093 #endif 3094 case IPV6_HOPOPTS: 3095 { 3096 struct ip6_hbh *hbh; 3097 int hbhlen; 3098 3099 /* 3100 * XXX: We don't allow a non-privileged user to set ANY HbH 3101 * options, since per-option restriction has too much 3102 * overhead. 3103 */ 3104 if (!priv) 3105 return (EPERM); 3106 3107 if (len == 0) { 3108 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3109 break; /* just remove the option */ 3110 } 3111 3112 /* message length validation */ 3113 if (len < sizeof(struct ip6_hbh)) 3114 return (EINVAL); 3115 hbh = (struct ip6_hbh *)buf; 3116 hbhlen = (hbh->ip6h_len + 1) << 3; 3117 if (len != hbhlen) 3118 return (EINVAL); 3119 3120 /* turn off the previous option, then set the new option. */ 3121 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3122 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 3123 if (opt->ip6po_hbh == NULL) 3124 return (ENOBUFS); 3125 memcpy(opt->ip6po_hbh, hbh, hbhlen); 3126 3127 break; 3128 } 3129 3130 #ifdef RFC2292 3131 case IPV6_2292DSTOPTS: 3132 #endif 3133 case IPV6_DSTOPTS: 3134 case IPV6_RTHDRDSTOPTS: 3135 { 3136 struct ip6_dest *dest, **newdest = NULL; 3137 int destlen; 3138 3139 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */ 3140 return (EPERM); 3141 3142 if (len == 0) { 3143 ip6_clearpktopts(opt, optname); 3144 break; /* just remove the option */ 3145 } 3146 3147 /* message length validation */ 3148 if (len < sizeof(struct ip6_dest)) 3149 return (EINVAL); 3150 dest = (struct ip6_dest *)buf; 3151 destlen = (dest->ip6d_len + 1) << 3; 3152 if (len != destlen) 3153 return (EINVAL); 3154 /* 3155 * Determine the position that the destination options header 3156 * should be inserted; before or after the routing header. 3157 */ 3158 switch (optname) { 3159 case IPV6_2292DSTOPTS: 3160 /* 3161 * The old advanced API is ambiguous on this point. 3162 * Our approach is to determine the position based 3163 * according to the existence of a routing header. 3164 * Note, however, that this depends on the order of the 3165 * extension headers in the ancillary data; the 1st 3166 * part of the destination options header must appear 3167 * before the routing header in the ancillary data, 3168 * too. 3169 * RFC3542 solved the ambiguity by introducing 3170 * separate ancillary data or option types. 3171 */ 3172 if (opt->ip6po_rthdr == NULL) 3173 newdest = &opt->ip6po_dest1; 3174 else 3175 newdest = &opt->ip6po_dest2; 3176 break; 3177 case IPV6_RTHDRDSTOPTS: 3178 newdest = &opt->ip6po_dest1; 3179 break; 3180 case IPV6_DSTOPTS: 3181 newdest = &opt->ip6po_dest2; 3182 break; 3183 } 3184 3185 /* turn off the previous option, then set the new option. */ 3186 ip6_clearpktopts(opt, optname); 3187 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 3188 if (*newdest == NULL) 3189 return (ENOBUFS); 3190 memcpy(*newdest, dest, destlen); 3191 3192 break; 3193 } 3194 3195 #ifdef RFC2292 3196 case IPV6_2292RTHDR: 3197 #endif 3198 case IPV6_RTHDR: 3199 { 3200 struct ip6_rthdr *rth; 3201 int rthlen; 3202 3203 if (len == 0) { 3204 ip6_clearpktopts(opt, IPV6_RTHDR); 3205 break; /* just remove the option */ 3206 } 3207 3208 /* message length validation */ 3209 if (len < sizeof(struct ip6_rthdr)) 3210 return (EINVAL); 3211 rth = (struct ip6_rthdr *)buf; 3212 rthlen = (rth->ip6r_len + 1) << 3; 3213 if (len != rthlen) 3214 return (EINVAL); 3215 switch (rth->ip6r_type) { 3216 case IPV6_RTHDR_TYPE_0: 3217 if (rth->ip6r_len == 0) /* must contain one addr */ 3218 return (EINVAL); 3219 if (rth->ip6r_len % 2) /* length must be even */ 3220 return (EINVAL); 3221 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 3222 return (EINVAL); 3223 break; 3224 default: 3225 return (EINVAL); /* not supported */ 3226 } 3227 /* turn off the previous option */ 3228 ip6_clearpktopts(opt, IPV6_RTHDR); 3229 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 3230 if (opt->ip6po_rthdr == NULL) 3231 return (ENOBUFS); 3232 memcpy(opt->ip6po_rthdr, rth, rthlen); 3233 break; 3234 } 3235 3236 case IPV6_USE_MIN_MTU: 3237 if (len != sizeof(int)) 3238 return (EINVAL); 3239 minmtupolicy = *(int *)buf; 3240 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 3241 minmtupolicy != IP6PO_MINMTU_DISABLE && 3242 minmtupolicy != IP6PO_MINMTU_ALL) { 3243 return (EINVAL); 3244 } 3245 opt->ip6po_minmtu = minmtupolicy; 3246 break; 3247 3248 case IPV6_DONTFRAG: 3249 if (len != sizeof(int)) 3250 return (EINVAL); 3251 3252 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 3253 /* 3254 * we ignore this option for TCP sockets. 3255 * (RFC3542 leaves this case unspecified.) 3256 */ 3257 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 3258 } else 3259 opt->ip6po_flags |= IP6PO_DONTFRAG; 3260 break; 3261 3262 default: 3263 return (ENOPROTOOPT); 3264 } /* end of switch */ 3265 3266 return (0); 3267 } 3268 3269 /* 3270 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 3271 * packet to the input queue of a specified interface. Note that this 3272 * calls the output routine of the loopback "driver", but with an interface 3273 * pointer that might NOT be lo0ifp -- easier than replicating that code here. 3274 */ 3275 void 3276 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, 3277 const struct sockaddr_in6 *dst) 3278 { 3279 struct mbuf *copym; 3280 struct ip6_hdr *ip6; 3281 3282 copym = m_copy(m, 0, M_COPYALL); 3283 if (copym == NULL) 3284 return; 3285 3286 /* 3287 * Make sure to deep-copy IPv6 header portion in case the data 3288 * is in an mbuf cluster, so that we can safely override the IPv6 3289 * header portion later. 3290 */ 3291 if ((copym->m_flags & M_EXT) != 0 || 3292 copym->m_len < sizeof(struct ip6_hdr)) { 3293 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 3294 if (copym == NULL) 3295 return; 3296 } 3297 3298 #ifdef DIAGNOSTIC 3299 if (copym->m_len < sizeof(*ip6)) { 3300 m_freem(copym); 3301 return; 3302 } 3303 #endif 3304 3305 ip6 = mtod(copym, struct ip6_hdr *); 3306 /* 3307 * clear embedded scope identifiers if necessary. 3308 * in6_clearscope will touch the addresses only when necessary. 3309 */ 3310 in6_clearscope(&ip6->ip6_src); 3311 in6_clearscope(&ip6->ip6_dst); 3312 3313 (void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL); 3314 } 3315 3316 /* 3317 * Chop IPv6 header off from the payload. 3318 */ 3319 static int 3320 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 3321 { 3322 struct mbuf *mh; 3323 struct ip6_hdr *ip6; 3324 3325 ip6 = mtod(m, struct ip6_hdr *); 3326 if (m->m_len > sizeof(*ip6)) { 3327 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 3328 if (mh == 0) { 3329 m_freem(m); 3330 return ENOBUFS; 3331 } 3332 M_MOVE_PKTHDR(mh, m); 3333 MH_ALIGN(mh, sizeof(*ip6)); 3334 m->m_len -= sizeof(*ip6); 3335 m->m_data += sizeof(*ip6); 3336 mh->m_next = m; 3337 m = mh; 3338 m->m_len = sizeof(*ip6); 3339 bcopy((void *)ip6, mtod(m, void *), sizeof(*ip6)); 3340 } 3341 exthdrs->ip6e_ip6 = m; 3342 return 0; 3343 } 3344 3345 /* 3346 * Compute IPv6 extension header length. 3347 */ 3348 int 3349 ip6_optlen(struct in6pcb *in6p) 3350 { 3351 int len; 3352 3353 if (!in6p->in6p_outputopts) 3354 return 0; 3355 3356 len = 0; 3357 #define elen(x) \ 3358 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 3359 3360 len += elen(in6p->in6p_outputopts->ip6po_hbh); 3361 len += elen(in6p->in6p_outputopts->ip6po_dest1); 3362 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 3363 len += elen(in6p->in6p_outputopts->ip6po_dest2); 3364 return len; 3365 #undef elen 3366 } 3367