1 /* $OpenBSD: ip6_output.c,v 1.76 2003/08/15 20:32:20 tedu 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 "pf.h" 65 66 #include <sys/param.h> 67 #include <sys/malloc.h> 68 #include <sys/mbuf.h> 69 #include <sys/errno.h> 70 #include <sys/protosw.h> 71 #include <sys/socket.h> 72 #include <sys/socketvar.h> 73 #include <sys/systm.h> 74 #include <sys/proc.h> 75 76 #include <net/if.h> 77 #include <net/route.h> 78 79 #include <netinet/in.h> 80 #include <netinet/in_var.h> 81 #include <netinet/in_systm.h> 82 #include <netinet/ip.h> 83 #include <netinet/in_pcb.h> 84 85 #include <netinet/ip6.h> 86 #include <netinet/icmp6.h> 87 #include <netinet6/ip6_var.h> 88 #include <netinet6/nd6.h> 89 90 #if NPF > 0 91 #include <net/pfvar.h> 92 #endif 93 94 #ifdef IPSEC 95 #include <netinet/ip_ah.h> 96 #include <netinet/ip_esp.h> 97 #include <netinet/udp.h> 98 #include <netinet/tcp.h> 99 #include <net/pfkeyv2.h> 100 101 extern u_int8_t get_sa_require(struct inpcb *); 102 103 extern int ipsec_auth_default_level; 104 extern int ipsec_esp_trans_default_level; 105 extern int ipsec_esp_network_default_level; 106 extern int ipsec_ipcomp_default_level; 107 #endif /* IPSEC */ 108 109 struct ip6_exthdrs { 110 struct mbuf *ip6e_ip6; 111 struct mbuf *ip6e_hbh; 112 struct mbuf *ip6e_dest1; 113 struct mbuf *ip6e_rthdr; 114 struct mbuf *ip6e_dest2; 115 }; 116 117 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, struct socket *); 118 static int ip6_setmoptions(int, struct ip6_moptions **, struct mbuf *); 119 static int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **); 120 static int ip6_copyexthdr(struct mbuf **, caddr_t, int); 121 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int, 122 struct ip6_frag **); 123 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t); 124 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *); 125 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *, 126 struct ifnet *, struct in6_addr *, u_long *); 127 128 /* 129 * IP6 output. The packet in mbuf chain m contains a skeletal IP6 130 * header (with pri, len, nxt, hlim, src, dst). 131 * This function may modify ver and hlim only. 132 * The mbuf chain containing the packet will be freed. 133 * The mbuf opt, if present, will not be freed. 134 * 135 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and 136 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one, 137 * which is rt_rmx.rmx_mtu. 138 */ 139 int 140 ip6_output(m0, opt, ro, flags, im6o, ifpp) 141 struct mbuf *m0; 142 struct ip6_pktopts *opt; 143 struct route_in6 *ro; 144 int flags; 145 struct ip6_moptions *im6o; 146 struct ifnet **ifpp; /* XXX: just for statistics */ 147 { 148 struct ip6_hdr *ip6, *mhip6; 149 struct ifnet *ifp, *origifp; 150 struct mbuf *m = m0; 151 int hlen, tlen, len, off; 152 struct route_in6 ip6route; 153 struct sockaddr_in6 *dst; 154 int error = 0; 155 struct in6_ifaddr *ia; 156 u_long mtu; 157 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; 158 struct ip6_exthdrs exthdrs; 159 struct in6_addr finaldst; 160 struct route_in6 *ro_pmtu = NULL; 161 int hdrsplit = 0; 162 u_int8_t sproto = 0; 163 #ifdef IPSEC 164 struct m_tag *mtag; 165 union sockaddr_union sdst; 166 struct tdb_ident *tdbi; 167 u_int32_t sspi; 168 struct inpcb *inp; 169 struct tdb *tdb; 170 int s; 171 #endif /* IPSEC */ 172 173 #ifdef IPSEC 174 inp = NULL; /*XXX*/ 175 if (inp && (inp->inp_flags & INP_IPV6) == 0) 176 panic("ip6_output: IPv4 pcb is passed"); 177 #endif /* IPSEC */ 178 179 #define MAKE_EXTHDR(hp, mp) \ 180 do { \ 181 if (hp) { \ 182 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 183 error = ip6_copyexthdr((mp), (caddr_t)(hp), \ 184 ((eh)->ip6e_len + 1) << 3); \ 185 if (error) \ 186 goto freehdrs; \ 187 } \ 188 } while (0) 189 190 bzero(&exthdrs, sizeof(exthdrs)); 191 if (opt) { 192 /* Hop-by-Hop options header */ 193 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 194 /* Destination options header(1st part) */ 195 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 196 /* Routing header */ 197 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 198 /* Destination options header(2nd part) */ 199 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 200 } 201 202 #ifdef IPSEC 203 /* 204 * splnet is chosen over spltdb because we are not allowed to 205 * lower the level, and udp6_output calls us in splnet(). XXX check 206 */ 207 s = splnet(); 208 209 /* 210 * Check if there was an outgoing SA bound to the flow 211 * from a transport protocol. 212 */ 213 ip6 = mtod(m, struct ip6_hdr *); 214 215 /* Do we have any pending SAs to apply ? */ 216 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 217 if (mtag != NULL) { 218 #ifdef DIAGNOSTIC 219 if (mtag->m_tag_len != sizeof (struct tdb_ident)) 220 panic("ip6_output: tag of length %d (should be %d", 221 mtag->m_tag_len, sizeof (struct tdb_ident)); 222 #endif 223 tdbi = (struct tdb_ident *)(mtag + 1); 224 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto); 225 if (tdb == NULL) 226 error = -EINVAL; 227 m_tag_delete(m, mtag); 228 } else 229 tdb = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr), 230 &error, IPSP_DIRECTION_OUT, NULL, inp); 231 232 if (tdb == NULL) { 233 splx(s); 234 235 if (error == 0) { 236 /* 237 * No IPsec processing required, we'll just send the 238 * packet out. 239 */ 240 sproto = 0; 241 242 /* Fall through to routing/multicast handling */ 243 } else { 244 /* 245 * -EINVAL is used to indicate that the packet should 246 * be silently dropped, typically because we've asked 247 * key management for an SA. 248 */ 249 if (error == -EINVAL) /* Should silently drop packet */ 250 error = 0; 251 252 goto freehdrs; 253 } 254 } else { 255 /* Loop detection */ 256 for (mtag = m_tag_first(m); mtag != NULL; 257 mtag = m_tag_next(m, mtag)) { 258 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 259 mtag->m_tag_id != 260 PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 261 continue; 262 tdbi = (struct tdb_ident *)(mtag + 1); 263 if (tdbi->spi == tdb->tdb_spi && 264 tdbi->proto == tdb->tdb_sproto && 265 !bcmp(&tdbi->dst, &tdb->tdb_dst, 266 sizeof(union sockaddr_union))) { 267 splx(s); 268 sproto = 0; /* mark as no-IPsec-needed */ 269 goto done_spd; 270 } 271 } 272 273 /* We need to do IPsec */ 274 bcopy(&tdb->tdb_dst, &sdst, sizeof(sdst)); 275 sspi = tdb->tdb_spi; 276 sproto = tdb->tdb_sproto; 277 splx(s); 278 279 #if 1 /* XXX */ 280 /* if we have any extension header, we cannot perform IPsec */ 281 if (exthdrs.ip6e_hbh || exthdrs.ip6e_dest1 || 282 exthdrs.ip6e_rthdr || exthdrs.ip6e_dest2) { 283 error = EHOSTUNREACH; 284 goto freehdrs; 285 } 286 #endif 287 } 288 289 /* Fall through to the routing/multicast handling code */ 290 done_spd: 291 #endif /* IPSEC */ 292 293 /* 294 * Calculate the total length of the extension header chain. 295 * Keep the length of the unfragmentable part for fragmentation. 296 */ 297 optlen = 0; 298 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len; 299 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len; 300 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len; 301 unfragpartlen = optlen + sizeof(struct ip6_hdr); 302 /* NOTE: we don't add AH/ESP length here. do that later. */ 303 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len; 304 305 /* 306 * If we need IPsec, or there is at least one extension header, 307 * separate IP6 header from the payload. 308 */ 309 if ((sproto || optlen) && !hdrsplit) { 310 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 311 m = NULL; 312 goto freehdrs; 313 } 314 m = exthdrs.ip6e_ip6; 315 hdrsplit++; 316 } 317 318 /* adjust pointer */ 319 ip6 = mtod(m, struct ip6_hdr *); 320 321 /* adjust mbuf packet header length */ 322 m->m_pkthdr.len += optlen; 323 plen = m->m_pkthdr.len - sizeof(*ip6); 324 325 /* If this is a jumbo payload, insert a jumbo payload option. */ 326 if (plen > IPV6_MAXPACKET) { 327 if (!hdrsplit) { 328 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 329 m = NULL; 330 goto freehdrs; 331 } 332 m = exthdrs.ip6e_ip6; 333 hdrsplit++; 334 } 335 /* adjust pointer */ 336 ip6 = mtod(m, struct ip6_hdr *); 337 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 338 goto freehdrs; 339 ip6->ip6_plen = 0; 340 } else 341 ip6->ip6_plen = htons(plen); 342 343 /* 344 * Concatenate headers and fill in next header fields. 345 * Here we have, on "m" 346 * IPv6 payload 347 * and we insert headers accordingly. Finally, we should be getting: 348 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 349 * 350 * during the header composing process, "m" points to IPv6 header. 351 * "mprev" points to an extension header prior to esp. 352 */ 353 { 354 u_char *nexthdrp = &ip6->ip6_nxt; 355 struct mbuf *mprev = m; 356 357 /* 358 * we treat dest2 specially. this makes IPsec processing 359 * much easier. 360 * 361 * result: IPv6 dest2 payload 362 * m and mprev will point to IPv6 header. 363 */ 364 if (exthdrs.ip6e_dest2) { 365 if (!hdrsplit) 366 panic("assumption failed: hdr not split"); 367 exthdrs.ip6e_dest2->m_next = m->m_next; 368 m->m_next = exthdrs.ip6e_dest2; 369 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 370 ip6->ip6_nxt = IPPROTO_DSTOPTS; 371 } 372 373 #define MAKE_CHAIN(m, mp, p, i)\ 374 do {\ 375 if (m) {\ 376 if (!hdrsplit) \ 377 panic("assumption failed: hdr not split"); \ 378 *mtod((m), u_char *) = *(p);\ 379 *(p) = (i);\ 380 p = mtod((m), u_char *);\ 381 (m)->m_next = (mp)->m_next;\ 382 (mp)->m_next = (m);\ 383 (mp) = (m);\ 384 }\ 385 } while (0) 386 /* 387 * result: IPv6 hbh dest1 rthdr dest2 payload 388 * m will point to IPv6 header. mprev will point to the 389 * extension header prior to dest2 (rthdr in the above case). 390 */ 391 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 392 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 393 IPPROTO_DSTOPTS); 394 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 395 IPPROTO_ROUTING); 396 } 397 398 /* 399 * If there is a routing header, replace destination address field 400 * with the first hop of the routing header. 401 */ 402 if (exthdrs.ip6e_rthdr) { 403 struct ip6_rthdr *rh; 404 struct ip6_rthdr0 *rh0; 405 struct in6_addr *addr; 406 407 rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, 408 struct ip6_rthdr *)); 409 finaldst = ip6->ip6_dst; 410 switch (rh->ip6r_type) { 411 case IPV6_RTHDR_TYPE_0: 412 rh0 = (struct ip6_rthdr0 *)rh; 413 addr = (struct in6_addr *)(rh0 + 1); 414 ip6->ip6_dst = addr[0]; 415 bcopy(&addr[1], &addr[0], 416 sizeof(struct in6_addr) * (rh0->ip6r0_segleft - 1)); 417 addr[rh0->ip6r0_segleft - 1] = finaldst; 418 break; 419 default: /* is it possible? */ 420 error = EINVAL; 421 goto bad; 422 } 423 } 424 425 /* Source address validation */ 426 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 427 (flags & IPV6_UNSPECSRC) == 0) { 428 error = EOPNOTSUPP; 429 ip6stat.ip6s_badscope++; 430 goto bad; 431 } 432 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 433 error = EOPNOTSUPP; 434 ip6stat.ip6s_badscope++; 435 goto bad; 436 } 437 438 ip6stat.ip6s_localout++; 439 440 /* 441 * Route packet. 442 */ 443 if (ro == 0) { 444 ro = &ip6route; 445 bzero((caddr_t)ro, sizeof(*ro)); 446 } 447 ro_pmtu = ro; 448 if (opt && opt->ip6po_rthdr) 449 ro = &opt->ip6po_route; 450 dst = (struct sockaddr_in6 *)&ro->ro_dst; 451 /* 452 * If there is a cached route, 453 * check that it is to the same destination 454 * and is still up. If not, free it and try again. 455 */ 456 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 457 dst->sin6_family != AF_INET6 || 458 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) { 459 RTFREE(ro->ro_rt); 460 ro->ro_rt = (struct rtentry *)0; 461 } 462 if (ro->ro_rt == 0) { 463 bzero(dst, sizeof(*dst)); 464 dst->sin6_family = AF_INET6; 465 dst->sin6_len = sizeof(struct sockaddr_in6); 466 dst->sin6_addr = ip6->ip6_dst; 467 } 468 #ifdef IPSEC 469 /* 470 * Check if the packet needs encapsulation. 471 * ipsp_process_packet will never come back to here. 472 */ 473 if (sproto != 0) { 474 s = splnet(); 475 476 /* fill in IPv6 header which would be filled later */ 477 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 478 if (opt && opt->ip6po_hlim != -1) 479 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 480 } else { 481 if (im6o != NULL) 482 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 483 else 484 ip6->ip6_hlim = ip6_defmcasthlim; 485 if (opt && opt->ip6po_hlim != -1) 486 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 487 488 /* 489 * XXX what should we do if ip6_hlim == 0 and the 490 * packet gets tunnelled? 491 */ 492 } 493 494 tdb = gettdb(sspi, &sdst, sproto); 495 if (tdb == NULL) { 496 splx(s); 497 error = EHOSTUNREACH; 498 m_freem(m); 499 goto done; 500 } 501 502 /* Latch to PCB */ 503 if (inp) 504 tdb_add_inp(tdb, inp, 0); 505 506 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 507 508 /* Callee frees mbuf */ 509 error = ipsp_process_packet(m, tdb, AF_INET6, 0); 510 splx(s); 511 return error; /* Nothing more to be done */ 512 } 513 #endif /* IPSEC */ 514 515 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 516 /* Unicast */ 517 518 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 519 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) 520 /* xxx 521 * interface selection comes here 522 * if an interface is specified from an upper layer, 523 * ifp must point it. 524 */ 525 if (ro->ro_rt == 0) { 526 /* 527 * non-bsdi always clone routes, if parent is 528 * PRF_CLONING. 529 */ 530 rtalloc((struct route *)ro); 531 } 532 if (ro->ro_rt == 0) { 533 ip6stat.ip6s_noroute++; 534 error = EHOSTUNREACH; 535 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */ 536 goto bad; 537 } 538 ia = ifatoia6(ro->ro_rt->rt_ifa); 539 ifp = ro->ro_rt->rt_ifp; 540 ro->ro_rt->rt_use++; 541 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 542 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway; 543 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 544 545 in6_ifstat_inc(ifp, ifs6_out_request); 546 547 /* 548 * Check if the outgoing interface conflicts with 549 * the interface specified by ifi6_ifindex (if specified). 550 * Note that loopback interface is always okay. 551 * (this may happen when we are sending a packet to one of 552 * our own addresses.) 553 */ 554 if (opt && opt->ip6po_pktinfo 555 && opt->ip6po_pktinfo->ipi6_ifindex) { 556 if (!(ifp->if_flags & IFF_LOOPBACK) 557 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) { 558 ip6stat.ip6s_noroute++; 559 in6_ifstat_inc(ifp, ifs6_out_discard); 560 error = EHOSTUNREACH; 561 goto bad; 562 } 563 } 564 565 if (opt && opt->ip6po_hlim != -1) 566 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 567 } else { 568 /* Multicast */ 569 struct in6_multi *in6m; 570 571 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 572 573 /* 574 * See if the caller provided any multicast options 575 */ 576 ifp = NULL; 577 if (im6o != NULL) { 578 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 579 if (im6o->im6o_multicast_ifp != NULL) 580 ifp = im6o->im6o_multicast_ifp; 581 } else 582 ip6->ip6_hlim = ip6_defmcasthlim; 583 584 /* 585 * See if the caller provided the outgoing interface 586 * as an ancillary data. 587 * Boundary check for ifindex is assumed to be already done. 588 */ 589 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex) 590 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex]; 591 592 /* 593 * If the destination is a node-local scope multicast, 594 * the packet should be loop-backed only. 595 */ 596 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) { 597 /* 598 * If the outgoing interface is already specified, 599 * it should be a loopback interface. 600 */ 601 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) { 602 ip6stat.ip6s_badscope++; 603 error = ENETUNREACH; /* XXX: better error? */ 604 /* XXX correct ifp? */ 605 in6_ifstat_inc(ifp, ifs6_out_discard); 606 goto bad; 607 } else { 608 ifp = lo0ifp; 609 } 610 } 611 612 if (opt && opt->ip6po_hlim != -1) 613 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 614 615 /* 616 * If caller did not provide an interface lookup a 617 * default in the routing table. This is either a 618 * default for the speicfied group (i.e. a host 619 * route), or a multicast default (a route for the 620 * ``net'' ff00::/8). 621 */ 622 if (ifp == NULL) { 623 if (ro->ro_rt == 0) { 624 ro->ro_rt = rtalloc1((struct sockaddr *) 625 &ro->ro_dst, 0); 626 } 627 if (ro->ro_rt == 0) { 628 ip6stat.ip6s_noroute++; 629 error = EHOSTUNREACH; 630 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */ 631 goto bad; 632 } 633 ia = ifatoia6(ro->ro_rt->rt_ifa); 634 ifp = ro->ro_rt->rt_ifp; 635 ro->ro_rt->rt_use++; 636 } 637 638 if ((flags & IPV6_FORWARDING) == 0) 639 in6_ifstat_inc(ifp, ifs6_out_request); 640 in6_ifstat_inc(ifp, ifs6_out_mcast); 641 642 /* 643 * Confirm that the outgoing interface supports multicast. 644 */ 645 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 646 ip6stat.ip6s_noroute++; 647 in6_ifstat_inc(ifp, ifs6_out_discard); 648 error = ENETUNREACH; 649 goto bad; 650 } 651 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); 652 if (in6m != NULL && 653 (im6o == NULL || im6o->im6o_multicast_loop)) { 654 /* 655 * If we belong to the destination multicast group 656 * on the outgoing interface, and the caller did not 657 * forbid loopback, loop back a copy. 658 */ 659 ip6_mloopback(ifp, m, dst); 660 } else { 661 /* 662 * If we are acting as a multicast router, perform 663 * multicast forwarding as if the packet had just 664 * arrived on the interface to which we are about 665 * to send. The multicast forwarding function 666 * recursively calls this function, using the 667 * IPV6_FORWARDING flag to prevent infinite recursion. 668 * 669 * Multicasts that are looped back by ip6_mloopback(), 670 * above, will be forwarded by the ip6_input() routine, 671 * if necessary. 672 */ 673 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 674 if (ip6_mforward(ip6, ifp, m) != 0) { 675 m_freem(m); 676 goto done; 677 } 678 } 679 } 680 /* 681 * Multicasts with a hoplimit of zero may be looped back, 682 * above, but must not be transmitted on a network. 683 * Also, multicasts addressed to the loopback interface 684 * are not sent -- the above call to ip6_mloopback() will 685 * loop back a copy if this host actually belongs to the 686 * destination group on the loopback interface. 687 */ 688 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) { 689 m_freem(m); 690 goto done; 691 } 692 } 693 694 /* 695 * Fill the outgoing inteface to tell the upper layer 696 * to increment per-interface statistics. 697 */ 698 if (ifpp) 699 *ifpp = ifp; 700 701 /* Determine path MTU. */ 702 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu)) != 0) 703 goto bad; 704 705 /* 706 * The caller of this function may specify to use the minimum MTU 707 * in some cases. 708 */ 709 if (mtu > IPV6_MMTU) { 710 if ((flags & IPV6_MINMTU)) 711 mtu = IPV6_MMTU; 712 } 713 714 /* Fake scoped addresses */ 715 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 716 /* 717 * If source or destination address is a scoped address, and 718 * the packet is going to be sent to a loopback interface, 719 * we should keep the original interface. 720 */ 721 722 /* 723 * XXX: this is a very experimental and temporary solution. 724 * We eventually have sockaddr_in6 and use the sin6_scope_id 725 * field of the structure here. 726 * We rely on the consistency between two scope zone ids 727 * of source add destination, which should already be assured 728 * Larger scopes than link will be supported in the near 729 * future. 730 */ 731 origifp = NULL; 732 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 733 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])]; 734 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 735 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])]; 736 /* 737 * XXX: origifp can be NULL even in those two cases above. 738 * For example, if we remove the (only) link-local address 739 * from the loopback interface, and try to send a link-local 740 * address without link-id information. Then the source 741 * address is ::1, and the destination address is the 742 * link-local address with its s6_addr16[1] being zero. 743 * What is worse, if the packet goes to the loopback interface 744 * by a default rejected route, the null pointer would be 745 * passed to looutput, and the kernel would hang. 746 * The following last resort would prevent such disaster. 747 */ 748 if (origifp == NULL) 749 origifp = ifp; 750 } else 751 origifp = ifp; 752 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 753 ip6->ip6_src.s6_addr16[1] = 0; 754 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 755 ip6->ip6_dst.s6_addr16[1] = 0; 756 757 /* 758 * If the outgoing packet contains a hop-by-hop options header, 759 * it must be examined and processed even by the source node. 760 * (RFC 2460, section 4.) 761 */ 762 if (exthdrs.ip6e_hbh) { 763 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 764 u_int32_t dummy1; /* XXX unused */ 765 u_int32_t dummy2; /* XXX unused */ 766 767 /* 768 * XXX: if we have to send an ICMPv6 error to the sender, 769 * we need the M_LOOP flag since icmp6_error() expects 770 * the IPv6 and the hop-by-hop options header are 771 * continuous unless the flag is set. 772 */ 773 m->m_flags |= M_LOOP; 774 m->m_pkthdr.rcvif = ifp; 775 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1), 776 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh), 777 &dummy1, &dummy2) < 0) { 778 /* m was already freed at this point */ 779 error = EINVAL;/* better error? */ 780 goto done; 781 } 782 m->m_flags &= ~M_LOOP; /* XXX */ 783 m->m_pkthdr.rcvif = NULL; 784 } 785 786 #if NPF > 0 787 if (pf_test6(PF_OUT, ifp, &m) != PF_PASS) { 788 error = EHOSTUNREACH; 789 m_freem(m); 790 goto done; 791 } 792 if (m == NULL) 793 goto done; 794 ip6 = mtod(m, struct ip6_hdr *); 795 #endif 796 797 /* 798 * Send the packet to the outgoing interface. 799 * If necessary, do IPv6 fragmentation before sending. 800 */ 801 tlen = m->m_pkthdr.len; 802 if (tlen <= mtu) { 803 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 804 goto done; 805 } else if (mtu < IPV6_MMTU) { 806 /* 807 * note that path MTU is never less than IPV6_MMTU 808 * (see icmp6_input). 809 */ 810 error = EMSGSIZE; 811 in6_ifstat_inc(ifp, ifs6_out_fragfail); 812 goto bad; 813 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */ 814 error = EMSGSIZE; 815 in6_ifstat_inc(ifp, ifs6_out_fragfail); 816 goto bad; 817 } else { 818 struct mbuf **mnext, *m_frgpart; 819 struct ip6_frag *ip6f; 820 u_int32_t id = htonl(ip6_id++); 821 u_char nextproto; 822 823 /* 824 * Too large for the destination or interface; 825 * fragment if possible. 826 * Must be able to put at least 8 bytes per fragment. 827 */ 828 hlen = unfragpartlen; 829 if (mtu > IPV6_MAXPACKET) 830 mtu = IPV6_MAXPACKET; 831 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 832 if (len < 8) { 833 error = EMSGSIZE; 834 in6_ifstat_inc(ifp, ifs6_out_fragfail); 835 goto bad; 836 } 837 838 mnext = &m->m_nextpkt; 839 840 /* 841 * Change the next header field of the last header in the 842 * unfragmentable part. 843 */ 844 if (exthdrs.ip6e_rthdr) { 845 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 846 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 847 } else if (exthdrs.ip6e_dest1) { 848 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 849 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 850 } else if (exthdrs.ip6e_hbh) { 851 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 852 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 853 } else { 854 nextproto = ip6->ip6_nxt; 855 ip6->ip6_nxt = IPPROTO_FRAGMENT; 856 } 857 858 /* 859 * Loop through length of segment after first fragment, 860 * make new header and copy data of each part and link onto 861 * chain. 862 */ 863 m0 = m; 864 for (off = hlen; off < tlen; off += len) { 865 MGETHDR(m, M_DONTWAIT, MT_HEADER); 866 if (!m) { 867 error = ENOBUFS; 868 ip6stat.ip6s_odropped++; 869 goto sendorfree; 870 } 871 m->m_flags = m0->m_flags & M_COPYFLAGS; 872 *mnext = m; 873 mnext = &m->m_nextpkt; 874 m->m_data += max_linkhdr; 875 mhip6 = mtod(m, struct ip6_hdr *); 876 *mhip6 = *ip6; 877 m->m_len = sizeof(*mhip6); 878 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 879 if (error) { 880 ip6stat.ip6s_odropped++; 881 goto sendorfree; 882 } 883 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7)); 884 if (off + len >= tlen) 885 len = tlen - off; 886 else 887 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 888 mhip6->ip6_plen = htons((u_short)(len + hlen + 889 sizeof(*ip6f) - sizeof(struct ip6_hdr))); 890 if ((m_frgpart = m_copy(m0, off, len)) == 0) { 891 error = ENOBUFS; 892 ip6stat.ip6s_odropped++; 893 goto sendorfree; 894 } 895 m_cat(m, m_frgpart); 896 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 897 m->m_pkthdr.rcvif = (struct ifnet *)0; 898 ip6f->ip6f_reserved = 0; 899 ip6f->ip6f_ident = id; 900 ip6f->ip6f_nxt = nextproto; 901 ip6stat.ip6s_ofragments++; 902 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 903 } 904 905 in6_ifstat_inc(ifp, ifs6_out_fragok); 906 } 907 908 /* 909 * Remove leading garbages. 910 */ 911 sendorfree: 912 m = m0->m_nextpkt; 913 m0->m_nextpkt = 0; 914 m_freem(m0); 915 for (m0 = m; m; m = m0) { 916 m0 = m->m_nextpkt; 917 m->m_nextpkt = 0; 918 if (error == 0) { 919 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 920 } else 921 m_freem(m); 922 } 923 924 if (error == 0) 925 ip6stat.ip6s_fragmented++; 926 927 done: 928 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */ 929 RTFREE(ro->ro_rt); 930 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) { 931 RTFREE(ro_pmtu->ro_rt); 932 } 933 934 return (error); 935 936 freehdrs: 937 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 938 m_freem(exthdrs.ip6e_dest1); 939 m_freem(exthdrs.ip6e_rthdr); 940 m_freem(exthdrs.ip6e_dest2); 941 /* FALLTHROUGH */ 942 bad: 943 m_freem(m); 944 goto done; 945 } 946 947 static int 948 ip6_copyexthdr(mp, hdr, hlen) 949 struct mbuf **mp; 950 caddr_t hdr; 951 int hlen; 952 { 953 struct mbuf *m; 954 955 if (hlen > MCLBYTES) 956 return (ENOBUFS); /* XXX */ 957 958 MGET(m, M_DONTWAIT, MT_DATA); 959 if (!m) 960 return (ENOBUFS); 961 962 if (hlen > MLEN) { 963 MCLGET(m, M_DONTWAIT); 964 if ((m->m_flags & M_EXT) == 0) { 965 m_free(m); 966 return (ENOBUFS); 967 } 968 } 969 m->m_len = hlen; 970 if (hdr) 971 bcopy(hdr, mtod(m, caddr_t), hlen); 972 973 *mp = m; 974 return (0); 975 } 976 977 /* 978 * Insert jumbo payload option. 979 */ 980 static int 981 ip6_insert_jumboopt(exthdrs, plen) 982 struct ip6_exthdrs *exthdrs; 983 u_int32_t plen; 984 { 985 struct mbuf *mopt; 986 u_int8_t *optbuf; 987 u_int32_t v; 988 989 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 990 991 /* 992 * If there is no hop-by-hop options header, allocate new one. 993 * If there is one but it doesn't have enough space to store the 994 * jumbo payload option, allocate a cluster to store the whole options. 995 * Otherwise, use it to store the options. 996 */ 997 if (exthdrs->ip6e_hbh == 0) { 998 MGET(mopt, M_DONTWAIT, MT_DATA); 999 if (mopt == 0) 1000 return (ENOBUFS); 1001 mopt->m_len = JUMBOOPTLEN; 1002 optbuf = mtod(mopt, u_int8_t *); 1003 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1004 exthdrs->ip6e_hbh = mopt; 1005 } else { 1006 struct ip6_hbh *hbh; 1007 1008 mopt = exthdrs->ip6e_hbh; 1009 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1010 /* 1011 * XXX assumption: 1012 * - exthdrs->ip6e_hbh is not referenced from places 1013 * other than exthdrs. 1014 * - exthdrs->ip6e_hbh is not an mbuf chain. 1015 */ 1016 int oldoptlen = mopt->m_len; 1017 struct mbuf *n; 1018 1019 /* 1020 * XXX: give up if the whole (new) hbh header does 1021 * not fit even in an mbuf cluster. 1022 */ 1023 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1024 return (ENOBUFS); 1025 1026 /* 1027 * As a consequence, we must always prepare a cluster 1028 * at this point. 1029 */ 1030 MGET(n, M_DONTWAIT, MT_DATA); 1031 if (n) { 1032 MCLGET(n, M_DONTWAIT); 1033 if ((n->m_flags & M_EXT) == 0) { 1034 m_freem(n); 1035 n = NULL; 1036 } 1037 } 1038 if (!n) 1039 return (ENOBUFS); 1040 n->m_len = oldoptlen + JUMBOOPTLEN; 1041 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1042 oldoptlen); 1043 optbuf = mtod(n, u_int8_t *) + oldoptlen; 1044 m_freem(mopt); 1045 mopt = exthdrs->ip6e_hbh = n; 1046 } else { 1047 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len; 1048 mopt->m_len += JUMBOOPTLEN; 1049 } 1050 optbuf[0] = IP6OPT_PADN; 1051 optbuf[1] = 0; 1052 1053 /* 1054 * Adjust the header length according to the pad and 1055 * the jumbo payload option. 1056 */ 1057 hbh = mtod(mopt, struct ip6_hbh *); 1058 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1059 } 1060 1061 /* fill in the option. */ 1062 optbuf[2] = IP6OPT_JUMBO; 1063 optbuf[3] = 4; 1064 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1065 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1066 1067 /* finally, adjust the packet header length */ 1068 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1069 1070 return (0); 1071 #undef JUMBOOPTLEN 1072 } 1073 1074 /* 1075 * Insert fragment header and copy unfragmentable header portions. 1076 */ 1077 static int 1078 ip6_insertfraghdr(m0, m, hlen, frghdrp) 1079 struct mbuf *m0, *m; 1080 int hlen; 1081 struct ip6_frag **frghdrp; 1082 { 1083 struct mbuf *n, *mlast; 1084 1085 if (hlen > sizeof(struct ip6_hdr)) { 1086 n = m_copym(m0, sizeof(struct ip6_hdr), 1087 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1088 if (n == 0) 1089 return (ENOBUFS); 1090 m->m_next = n; 1091 } else 1092 n = m; 1093 1094 /* Search for the last mbuf of unfragmentable part. */ 1095 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1096 ; 1097 1098 if ((mlast->m_flags & M_EXT) == 0 && 1099 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1100 /* use the trailing space of the last mbuf for the fragment hdr */ 1101 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) + 1102 mlast->m_len); 1103 mlast->m_len += sizeof(struct ip6_frag); 1104 m->m_pkthdr.len += sizeof(struct ip6_frag); 1105 } else { 1106 /* allocate a new mbuf for the fragment header */ 1107 struct mbuf *mfrg; 1108 1109 MGET(mfrg, M_DONTWAIT, MT_DATA); 1110 if (mfrg == 0) 1111 return (ENOBUFS); 1112 mfrg->m_len = sizeof(struct ip6_frag); 1113 *frghdrp = mtod(mfrg, struct ip6_frag *); 1114 mlast->m_next = mfrg; 1115 } 1116 1117 return (0); 1118 } 1119 1120 static int 1121 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup) 1122 struct route_in6 *ro_pmtu, *ro; 1123 struct ifnet *ifp; 1124 struct in6_addr *dst; 1125 u_long *mtup; 1126 { 1127 u_int32_t mtu = 0; 1128 int error = 0; 1129 1130 if (ro_pmtu != ro) { 1131 /* The first hop and the final destination may differ. */ 1132 struct sockaddr_in6 *sa6_dst = 1133 (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 1134 if (ro_pmtu->ro_rt && 1135 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 || 1136 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) { 1137 RTFREE(ro_pmtu->ro_rt); 1138 ro_pmtu->ro_rt = (struct rtentry *)0; 1139 } 1140 if (ro_pmtu->ro_rt == 0) { 1141 bzero(sa6_dst, sizeof(*sa6_dst)); 1142 sa6_dst->sin6_family = AF_INET6; 1143 sa6_dst->sin6_len = sizeof(struct sockaddr_in6); 1144 sa6_dst->sin6_addr = *dst; 1145 1146 rtalloc((struct route *)ro_pmtu); 1147 } 1148 } 1149 if (ro_pmtu->ro_rt) { 1150 u_int32_t ifmtu; 1151 1152 if (ifp == NULL) 1153 ifp = ro_pmtu->ro_rt->rt_ifp; 1154 ifmtu = IN6_LINKMTU(ifp); 1155 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu; 1156 if (mtu == 0) 1157 mtu = ifmtu; 1158 else if (mtu > ifmtu) { 1159 /* 1160 * The MTU on the route is larger than the MTU on 1161 * the interface! This shouldn't happen, unless the 1162 * MTU of the interface has been changed after the 1163 * interface was brought up. Change the MTU in the 1164 * route to match the interface MTU (as long as the 1165 * field isn't locked). 1166 * 1167 * if MTU on the route is 0, we need to fix the MTU. 1168 * this case happens with path MTU discovery timeouts. 1169 */ 1170 mtu = ifmtu; 1171 if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU)) 1172 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; 1173 } 1174 } else if (ifp) { 1175 mtu = IN6_LINKMTU(ifp); 1176 } else 1177 error = EHOSTUNREACH; /* XXX */ 1178 1179 *mtup = mtu; 1180 return (error); 1181 } 1182 1183 /* 1184 * IP6 socket option processing. 1185 */ 1186 int 1187 ip6_ctloutput(op, so, level, optname, mp) 1188 int op; 1189 struct socket *so; 1190 int level, optname; 1191 struct mbuf **mp; 1192 { 1193 int privileged; 1194 struct inpcb *inp = sotoinpcb(so); 1195 struct mbuf *m = *mp; 1196 int error, optval; 1197 int optlen; 1198 #ifdef IPSEC 1199 struct proc *p = curproc; /* XXX */ 1200 struct tdb *tdb; 1201 struct tdb_ident *tdbip, tdbi; 1202 int s; 1203 #endif 1204 1205 optlen = m ? m->m_len : 0; 1206 error = optval = 0; 1207 1208 privileged = (inp->inp_socket->so_state & SS_PRIV); 1209 1210 if (level == IPPROTO_IPV6) { 1211 switch (op) { 1212 case PRCO_SETOPT: 1213 switch (optname) { 1214 case IPV6_PKTOPTIONS: 1215 /* m is freed in ip6_pcbopts */ 1216 return (ip6_pcbopts(&inp->inp_outputopts6, 1217 m, so)); 1218 case IPV6_HOPOPTS: 1219 case IPV6_DSTOPTS: 1220 if (!privileged) { 1221 error = EPERM; 1222 break; 1223 } 1224 /* FALLTHROUGH */ 1225 case IPV6_UNICAST_HOPS: 1226 case IPV6_RECVOPTS: 1227 case IPV6_RECVRETOPTS: 1228 case IPV6_RECVDSTADDR: 1229 case IPV6_PKTINFO: 1230 case IPV6_HOPLIMIT: 1231 case IPV6_RTHDR: 1232 case IPV6_FAITH: 1233 case IPV6_V6ONLY: 1234 if (optlen != sizeof(int)) { 1235 error = EINVAL; 1236 break; 1237 } 1238 optval = *mtod(m, int *); 1239 switch (optname) { 1240 1241 case IPV6_UNICAST_HOPS: 1242 if (optval < -1 || optval >= 256) 1243 error = EINVAL; 1244 else { 1245 /* -1 = kernel default */ 1246 inp->inp_hops = optval; 1247 } 1248 break; 1249 #define OPTSET(bit) \ 1250 do { \ 1251 if (optval) \ 1252 inp->inp_flags |= (bit); \ 1253 else \ 1254 inp->inp_flags &= ~(bit); \ 1255 } while (0) 1256 case IPV6_RECVOPTS: 1257 OPTSET(IN6P_RECVOPTS); 1258 break; 1259 1260 case IPV6_RECVRETOPTS: 1261 OPTSET(IN6P_RECVRETOPTS); 1262 break; 1263 1264 case IPV6_RECVDSTADDR: 1265 OPTSET(IN6P_RECVDSTADDR); 1266 break; 1267 1268 case IPV6_PKTINFO: 1269 OPTSET(IN6P_PKTINFO); 1270 break; 1271 1272 case IPV6_HOPLIMIT: 1273 OPTSET(IN6P_HOPLIMIT); 1274 break; 1275 1276 case IPV6_HOPOPTS: 1277 OPTSET(IN6P_HOPOPTS); 1278 break; 1279 1280 case IPV6_DSTOPTS: 1281 OPTSET(IN6P_DSTOPTS); 1282 break; 1283 1284 case IPV6_RTHDR: 1285 OPTSET(IN6P_RTHDR); 1286 break; 1287 1288 case IPV6_FAITH: 1289 OPTSET(IN6P_FAITH); 1290 break; 1291 1292 case IPV6_V6ONLY: 1293 if (!optval) 1294 error = EINVAL; 1295 break; 1296 } 1297 break; 1298 #undef OPTSET 1299 1300 case IPV6_MULTICAST_IF: 1301 case IPV6_MULTICAST_HOPS: 1302 case IPV6_MULTICAST_LOOP: 1303 case IPV6_JOIN_GROUP: 1304 case IPV6_LEAVE_GROUP: 1305 error = ip6_setmoptions(optname, 1306 &inp->inp_moptions6, m); 1307 break; 1308 1309 case IPV6_PORTRANGE: 1310 optval = *mtod(m, int *); 1311 1312 switch (optval) { 1313 case IPV6_PORTRANGE_DEFAULT: 1314 inp->inp_flags &= ~(IN6P_LOWPORT); 1315 inp->inp_flags &= ~(IN6P_HIGHPORT); 1316 break; 1317 1318 case IPV6_PORTRANGE_HIGH: 1319 inp->inp_flags &= ~(IN6P_LOWPORT); 1320 inp->inp_flags |= IN6P_HIGHPORT; 1321 break; 1322 1323 case IPV6_PORTRANGE_LOW: 1324 inp->inp_flags &= ~(IN6P_HIGHPORT); 1325 inp->inp_flags |= IN6P_LOWPORT; 1326 break; 1327 1328 default: 1329 error = EINVAL; 1330 break; 1331 } 1332 break; 1333 1334 case IPSEC6_OUTSA: 1335 #ifndef IPSEC 1336 error = EINVAL; 1337 #else 1338 s = spltdb(); 1339 if (m == 0 || m->m_len != sizeof(struct tdb_ident)) { 1340 error = EINVAL; 1341 } else { 1342 tdbip = mtod(m, struct tdb_ident *); 1343 tdb = gettdb(tdbip->spi, &tdbip->dst, 1344 tdbip->proto); 1345 if (tdb == NULL) 1346 error = ESRCH; 1347 else 1348 tdb_add_inp(tdb, inp, 0); 1349 } 1350 splx(s); 1351 #endif /* IPSEC */ 1352 break; 1353 1354 case IPV6_AUTH_LEVEL: 1355 case IPV6_ESP_TRANS_LEVEL: 1356 case IPV6_ESP_NETWORK_LEVEL: 1357 case IPV6_IPCOMP_LEVEL: 1358 #ifndef IPSEC 1359 error = EINVAL; 1360 #else 1361 if (m == 0 || m->m_len != sizeof(int)) { 1362 error = EINVAL; 1363 break; 1364 } 1365 optval = *mtod(m, int *); 1366 1367 if (optval < IPSEC_LEVEL_BYPASS || 1368 optval > IPSEC_LEVEL_UNIQUE) { 1369 error = EINVAL; 1370 break; 1371 } 1372 1373 switch (optname) { 1374 case IPV6_AUTH_LEVEL: 1375 if (optval < ipsec_auth_default_level && 1376 suser(p, 0)) { 1377 error = EACCES; 1378 break; 1379 } 1380 inp->inp_seclevel[SL_AUTH] = optval; 1381 break; 1382 1383 case IPV6_ESP_TRANS_LEVEL: 1384 if (optval < ipsec_esp_trans_default_level && 1385 suser(p, 0)) { 1386 error = EACCES; 1387 break; 1388 } 1389 inp->inp_seclevel[SL_ESP_TRANS] = optval; 1390 break; 1391 1392 case IPV6_ESP_NETWORK_LEVEL: 1393 if (optval < ipsec_esp_network_default_level && 1394 suser(p, 0)) { 1395 error = EACCES; 1396 break; 1397 } 1398 inp->inp_seclevel[SL_ESP_NETWORK] = optval; 1399 break; 1400 1401 case IPV6_IPCOMP_LEVEL: 1402 if (optval < ipsec_ipcomp_default_level && 1403 suser(p, 0)) { 1404 error = EACCES; 1405 break; 1406 } 1407 inp->inp_seclevel[SL_IPCOMP] = optval; 1408 break; 1409 } 1410 if (!error) 1411 inp->inp_secrequire = get_sa_require(inp); 1412 #endif 1413 break; 1414 1415 1416 default: 1417 error = ENOPROTOOPT; 1418 break; 1419 } 1420 if (m) 1421 (void)m_free(m); 1422 break; 1423 1424 case PRCO_GETOPT: 1425 switch (optname) { 1426 1427 case IPV6_OPTIONS: 1428 case IPV6_RETOPTS: 1429 error = ENOPROTOOPT; 1430 break; 1431 1432 case IPV6_PKTOPTIONS: 1433 if (inp->inp_options) { 1434 *mp = m_copym(inp->inp_options, 0, 1435 M_COPYALL, M_WAIT); 1436 } else { 1437 *mp = m_get(M_WAIT, MT_SOOPTS); 1438 (*mp)->m_len = 0; 1439 } 1440 break; 1441 1442 case IPV6_HOPOPTS: 1443 case IPV6_DSTOPTS: 1444 if (!privileged) { 1445 error = EPERM; 1446 break; 1447 } 1448 /* FALLTHROUGH */ 1449 case IPV6_UNICAST_HOPS: 1450 case IPV6_RECVOPTS: 1451 case IPV6_RECVRETOPTS: 1452 case IPV6_RECVDSTADDR: 1453 case IPV6_PKTINFO: 1454 case IPV6_HOPLIMIT: 1455 case IPV6_RTHDR: 1456 case IPV6_FAITH: 1457 case IPV6_V6ONLY: 1458 case IPV6_PORTRANGE: 1459 switch (optname) { 1460 1461 case IPV6_UNICAST_HOPS: 1462 optval = inp->inp_hops; 1463 break; 1464 1465 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1466 1467 case IPV6_RECVOPTS: 1468 optval = OPTBIT(IN6P_RECVOPTS); 1469 break; 1470 1471 case IPV6_RECVRETOPTS: 1472 optval = OPTBIT(IN6P_RECVRETOPTS); 1473 break; 1474 1475 case IPV6_RECVDSTADDR: 1476 optval = OPTBIT(IN6P_RECVDSTADDR); 1477 break; 1478 1479 case IPV6_PKTINFO: 1480 optval = OPTBIT(IN6P_PKTINFO); 1481 break; 1482 1483 case IPV6_HOPLIMIT: 1484 optval = OPTBIT(IN6P_HOPLIMIT); 1485 break; 1486 1487 case IPV6_HOPOPTS: 1488 optval = OPTBIT(IN6P_HOPOPTS); 1489 break; 1490 1491 case IPV6_DSTOPTS: 1492 optval = OPTBIT(IN6P_DSTOPTS); 1493 break; 1494 1495 case IPV6_RTHDR: 1496 optval = OPTBIT(IN6P_RTHDR); 1497 break; 1498 1499 case IPV6_FAITH: 1500 optval = OPTBIT(IN6P_FAITH); 1501 break; 1502 1503 case IPV6_V6ONLY: 1504 optval = (ip6_v6only != 0); /* XXX */ 1505 break; 1506 1507 case IPV6_PORTRANGE: 1508 { 1509 int flags; 1510 1511 flags = inp->inp_flags; 1512 if (flags & IN6P_HIGHPORT) 1513 optval = IPV6_PORTRANGE_HIGH; 1514 else if (flags & IN6P_LOWPORT) 1515 optval = IPV6_PORTRANGE_LOW; 1516 else 1517 optval = 0; 1518 break; 1519 } 1520 } 1521 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1522 m->m_len = sizeof(int); 1523 *mtod(m, int *) = optval; 1524 break; 1525 1526 case IPV6_MULTICAST_IF: 1527 case IPV6_MULTICAST_HOPS: 1528 case IPV6_MULTICAST_LOOP: 1529 case IPV6_JOIN_GROUP: 1530 case IPV6_LEAVE_GROUP: 1531 error = ip6_getmoptions(optname, inp->inp_moptions6, mp); 1532 break; 1533 1534 case IPSEC6_OUTSA: 1535 #ifndef IPSEC 1536 error = EINVAL; 1537 #else 1538 s = spltdb(); 1539 if (inp->inp_tdb_out == NULL) { 1540 error = ENOENT; 1541 } else { 1542 tdbi.spi = inp->inp_tdb_out->tdb_spi; 1543 tdbi.dst = inp->inp_tdb_out->tdb_dst; 1544 tdbi.proto = inp->inp_tdb_out->tdb_sproto; 1545 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1546 m->m_len = sizeof(tdbi); 1547 bcopy((caddr_t)&tdbi, mtod(m, caddr_t), 1548 (unsigned)m->m_len); 1549 } 1550 splx(s); 1551 #endif /* IPSEC */ 1552 break; 1553 1554 case IPV6_AUTH_LEVEL: 1555 case IPV6_ESP_TRANS_LEVEL: 1556 case IPV6_ESP_NETWORK_LEVEL: 1557 case IPV6_IPCOMP_LEVEL: 1558 #ifndef IPSEC 1559 m->m_len = sizeof(int); 1560 *mtod(m, int *) = IPSEC_LEVEL_NONE; 1561 #else 1562 m->m_len = sizeof(int); 1563 switch (optname) { 1564 case IPV6_AUTH_LEVEL: 1565 optval = inp->inp_seclevel[SL_AUTH]; 1566 break; 1567 1568 case IPV6_ESP_TRANS_LEVEL: 1569 optval = 1570 inp->inp_seclevel[SL_ESP_TRANS]; 1571 break; 1572 1573 case IPV6_ESP_NETWORK_LEVEL: 1574 optval = 1575 inp->inp_seclevel[SL_ESP_NETWORK]; 1576 break; 1577 1578 case IPV6_IPCOMP_LEVEL: 1579 optval = inp->inp_seclevel[SL_IPCOMP]; 1580 break; 1581 } 1582 *mtod(m, int *) = optval; 1583 #endif 1584 break; 1585 1586 default: 1587 error = ENOPROTOOPT; 1588 break; 1589 } 1590 break; 1591 } 1592 } else { 1593 error = EINVAL; 1594 if (op == PRCO_SETOPT && *mp) 1595 (void)m_free(*mp); 1596 } 1597 return (error); 1598 } 1599 1600 int 1601 ip6_raw_ctloutput(op, so, level, optname, mp) 1602 int op; 1603 struct socket *so; 1604 int level, optname; 1605 struct mbuf **mp; 1606 { 1607 int error = 0, optval, optlen; 1608 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 1609 struct inpcb *inp = sotoinpcb(so); 1610 struct mbuf *m = *mp; 1611 1612 optlen = m ? m->m_len : 0; 1613 1614 if (level != IPPROTO_IPV6) { 1615 if (op == PRCO_SETOPT && *mp) 1616 (void)m_free(*mp); 1617 return (EINVAL); 1618 } 1619 1620 switch (optname) { 1621 case IPV6_CHECKSUM: 1622 /* 1623 * For ICMPv6 sockets, no modification allowed for checksum 1624 * offset, permit "no change" values to help existing apps. 1625 * 1626 * XXX 2292bis says: "An attempt to set IPV6_CHECKSUM 1627 * for an ICMPv6 socket will fail." 1628 * The current behavior does not meet 2292bis. 1629 */ 1630 switch (op) { 1631 case PRCO_SETOPT: 1632 if (optlen != sizeof(int)) { 1633 error = EINVAL; 1634 break; 1635 } 1636 optval = *mtod(m, int *); 1637 if ((optval % 2) != 0) { 1638 /* the API assumes even offset values */ 1639 error = EINVAL; 1640 } else if (so->so_proto->pr_protocol == 1641 IPPROTO_ICMPV6) { 1642 if (optval != icmp6off) 1643 error = EINVAL; 1644 } else 1645 inp->in6p_cksum = optval; 1646 break; 1647 1648 case PRCO_GETOPT: 1649 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 1650 optval = icmp6off; 1651 else 1652 optval = inp->in6p_cksum; 1653 1654 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1655 m->m_len = sizeof(int); 1656 *mtod(m, int *) = optval; 1657 break; 1658 1659 default: 1660 error = EINVAL; 1661 break; 1662 } 1663 break; 1664 1665 default: 1666 error = ENOPROTOOPT; 1667 break; 1668 } 1669 1670 if (op == PRCO_SETOPT && m) 1671 (void)m_free(m); 1672 1673 return (error); 1674 } 1675 1676 /* 1677 * Set up IP6 options in pcb for insertion in output packets. 1678 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1679 * with destination address if source routed. 1680 */ 1681 static int 1682 ip6_pcbopts(pktopt, m, so) 1683 struct ip6_pktopts **pktopt; 1684 struct mbuf *m; 1685 struct socket *so; 1686 { 1687 struct ip6_pktopts *opt = *pktopt; 1688 int error = 0; 1689 struct proc *p = curproc; /* XXX */ 1690 int priv = 0; 1691 1692 /* turn off any old options. */ 1693 if (opt) { 1694 if (opt->ip6po_m) 1695 (void)m_free(opt->ip6po_m); 1696 } else 1697 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 1698 *pktopt = 0; 1699 1700 if (!m || m->m_len == 0) { 1701 /* 1702 * Only turning off any previous options. 1703 */ 1704 free(opt, M_IP6OPT); 1705 if (m) 1706 (void)m_free(m); 1707 return (0); 1708 } 1709 1710 /* set options specified by user. */ 1711 if (p && !suser(p, 0)) 1712 priv = 1; 1713 if ((error = ip6_setpktoptions(m, opt, priv)) != 0) { 1714 (void)m_free(m); 1715 free(opt, M_IP6OPT); 1716 return (error); 1717 } 1718 *pktopt = opt; 1719 return (0); 1720 } 1721 1722 /* 1723 * Set the IP6 multicast options in response to user setsockopt(). 1724 */ 1725 static int 1726 ip6_setmoptions(optname, im6op, m) 1727 int optname; 1728 struct ip6_moptions **im6op; 1729 struct mbuf *m; 1730 { 1731 int error = 0; 1732 u_int loop, ifindex; 1733 struct ipv6_mreq *mreq; 1734 struct ifnet *ifp; 1735 struct ip6_moptions *im6o = *im6op; 1736 struct route_in6 ro; 1737 struct sockaddr_in6 *dst; 1738 struct in6_multi_mship *imm; 1739 struct proc *p = curproc; /* XXX */ 1740 1741 if (im6o == NULL) { 1742 /* 1743 * No multicast option buffer attached to the pcb; 1744 * allocate one and initialize to default values. 1745 */ 1746 im6o = (struct ip6_moptions *) 1747 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK); 1748 1749 if (im6o == NULL) 1750 return (ENOBUFS); 1751 *im6op = im6o; 1752 im6o->im6o_multicast_ifp = NULL; 1753 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 1754 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 1755 LIST_INIT(&im6o->im6o_memberships); 1756 } 1757 1758 switch (optname) { 1759 1760 case IPV6_MULTICAST_IF: 1761 /* 1762 * Select the interface for outgoing multicast packets. 1763 */ 1764 if (m == NULL || m->m_len != sizeof(u_int)) { 1765 error = EINVAL; 1766 break; 1767 } 1768 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex)); 1769 if (ifindex < 0 || if_index < ifindex) { 1770 error = ENXIO; /* XXX EINVAL? */ 1771 break; 1772 } 1773 ifp = ifindex2ifnet[ifindex]; 1774 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1775 error = EADDRNOTAVAIL; 1776 break; 1777 } 1778 im6o->im6o_multicast_ifp = ifp; 1779 break; 1780 1781 case IPV6_MULTICAST_HOPS: 1782 { 1783 /* 1784 * Set the IP6 hoplimit for outgoing multicast packets. 1785 */ 1786 int optval; 1787 if (m == NULL || m->m_len != sizeof(int)) { 1788 error = EINVAL; 1789 break; 1790 } 1791 bcopy(mtod(m, u_int *), &optval, sizeof(optval)); 1792 if (optval < -1 || optval >= 256) 1793 error = EINVAL; 1794 else if (optval == -1) 1795 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 1796 else 1797 im6o->im6o_multicast_hlim = optval; 1798 break; 1799 } 1800 1801 case IPV6_MULTICAST_LOOP: 1802 /* 1803 * Set the loopback flag for outgoing multicast packets. 1804 * Must be zero or one. 1805 */ 1806 if (m == NULL || m->m_len != sizeof(u_int)) { 1807 error = EINVAL; 1808 break; 1809 } 1810 bcopy(mtod(m, u_int *), &loop, sizeof(loop)); 1811 if (loop > 1) { 1812 error = EINVAL; 1813 break; 1814 } 1815 im6o->im6o_multicast_loop = loop; 1816 break; 1817 1818 case IPV6_JOIN_GROUP: 1819 /* 1820 * Add a multicast group membership. 1821 * Group must be a valid IP6 multicast address. 1822 */ 1823 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 1824 error = EINVAL; 1825 break; 1826 } 1827 mreq = mtod(m, struct ipv6_mreq *); 1828 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 1829 /* 1830 * We use the unspecified address to specify to accept 1831 * all multicast addresses. Only super user is allowed 1832 * to do this. 1833 */ 1834 if (suser(p, 0)) 1835 { 1836 error = EACCES; 1837 break; 1838 } 1839 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 1840 error = EINVAL; 1841 break; 1842 } 1843 1844 /* 1845 * If the interface is specified, validate it. 1846 */ 1847 if (mreq->ipv6mr_interface < 0 1848 || if_index < mreq->ipv6mr_interface) { 1849 error = ENXIO; /* XXX EINVAL? */ 1850 break; 1851 } 1852 /* 1853 * If no interface was explicitly specified, choose an 1854 * appropriate one according to the given multicast address. 1855 */ 1856 if (mreq->ipv6mr_interface == 0) { 1857 /* 1858 * If the multicast address is in node-local scope, 1859 * the interface should be a loopback interface. 1860 * Otherwise, look up the routing table for the 1861 * address, and choose the outgoing interface. 1862 * XXX: is it a good approach? 1863 */ 1864 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) { 1865 ifp = lo0ifp; 1866 } else { 1867 ro.ro_rt = NULL; 1868 dst = (struct sockaddr_in6 *)&ro.ro_dst; 1869 bzero(dst, sizeof(*dst)); 1870 dst->sin6_len = sizeof(struct sockaddr_in6); 1871 dst->sin6_family = AF_INET6; 1872 dst->sin6_addr = mreq->ipv6mr_multiaddr; 1873 rtalloc((struct route *)&ro); 1874 if (ro.ro_rt == NULL) { 1875 error = EADDRNOTAVAIL; 1876 break; 1877 } 1878 ifp = ro.ro_rt->rt_ifp; 1879 rtfree(ro.ro_rt); 1880 } 1881 } else 1882 ifp = ifindex2ifnet[mreq->ipv6mr_interface]; 1883 1884 /* 1885 * See if we found an interface, and confirm that it 1886 * supports multicast 1887 */ 1888 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1889 error = EADDRNOTAVAIL; 1890 break; 1891 } 1892 /* 1893 * Put interface index into the multicast address, 1894 * if the address has link-local scope. 1895 */ 1896 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 1897 mreq->ipv6mr_multiaddr.s6_addr16[1] = 1898 htons(mreq->ipv6mr_interface); 1899 } 1900 /* 1901 * See if the membership already exists. 1902 */ 1903 for (imm = im6o->im6o_memberships.lh_first; 1904 imm != NULL; imm = imm->i6mm_chain.le_next) 1905 if (imm->i6mm_maddr->in6m_ifp == ifp && 1906 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 1907 &mreq->ipv6mr_multiaddr)) 1908 break; 1909 if (imm != NULL) { 1910 error = EADDRINUSE; 1911 break; 1912 } 1913 /* 1914 * Everything looks good; add a new record to the multicast 1915 * address list for the given interface. 1916 */ 1917 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error); 1918 if (!imm) 1919 break; 1920 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 1921 break; 1922 1923 case IPV6_LEAVE_GROUP: 1924 /* 1925 * Drop a multicast group membership. 1926 * Group must be a valid IP6 multicast address. 1927 */ 1928 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 1929 error = EINVAL; 1930 break; 1931 } 1932 mreq = mtod(m, struct ipv6_mreq *); 1933 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 1934 if (suser(p, 0)) 1935 { 1936 error = EACCES; 1937 break; 1938 } 1939 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 1940 error = EINVAL; 1941 break; 1942 } 1943 /* 1944 * If an interface address was specified, get a pointer 1945 * to its ifnet structure. 1946 */ 1947 if (mreq->ipv6mr_interface < 0 1948 || if_index < mreq->ipv6mr_interface) { 1949 error = ENXIO; /* XXX EINVAL? */ 1950 break; 1951 } 1952 ifp = ifindex2ifnet[mreq->ipv6mr_interface]; 1953 /* 1954 * Put interface index into the multicast address, 1955 * if the address has link-local scope. 1956 */ 1957 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 1958 mreq->ipv6mr_multiaddr.s6_addr16[1] = 1959 htons(mreq->ipv6mr_interface); 1960 } 1961 /* 1962 * Find the membership in the membership list. 1963 */ 1964 for (imm = im6o->im6o_memberships.lh_first; 1965 imm != NULL; imm = imm->i6mm_chain.le_next) { 1966 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) && 1967 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 1968 &mreq->ipv6mr_multiaddr)) 1969 break; 1970 } 1971 if (imm == NULL) { 1972 /* Unable to resolve interface */ 1973 error = EADDRNOTAVAIL; 1974 break; 1975 } 1976 /* 1977 * Give up the multicast address record to which the 1978 * membership points. 1979 */ 1980 LIST_REMOVE(imm, i6mm_chain); 1981 in6_leavegroup(imm); 1982 break; 1983 1984 default: 1985 error = EOPNOTSUPP; 1986 break; 1987 } 1988 1989 /* 1990 * If all options have default values, no need to keep the mbuf. 1991 */ 1992 if (im6o->im6o_multicast_ifp == NULL && 1993 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 1994 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 1995 im6o->im6o_memberships.lh_first == NULL) { 1996 free(*im6op, M_IPMOPTS); 1997 *im6op = NULL; 1998 } 1999 2000 return (error); 2001 } 2002 2003 /* 2004 * Return the IP6 multicast options in response to user getsockopt(). 2005 */ 2006 static int 2007 ip6_getmoptions(optname, im6o, mp) 2008 int optname; 2009 struct ip6_moptions *im6o; 2010 struct mbuf **mp; 2011 { 2012 u_int *hlim, *loop, *ifindex; 2013 2014 *mp = m_get(M_WAIT, MT_SOOPTS); 2015 2016 switch (optname) { 2017 2018 case IPV6_MULTICAST_IF: 2019 ifindex = mtod(*mp, u_int *); 2020 (*mp)->m_len = sizeof(u_int); 2021 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2022 *ifindex = 0; 2023 else 2024 *ifindex = im6o->im6o_multicast_ifp->if_index; 2025 return (0); 2026 2027 case IPV6_MULTICAST_HOPS: 2028 hlim = mtod(*mp, u_int *); 2029 (*mp)->m_len = sizeof(u_int); 2030 if (im6o == NULL) 2031 *hlim = ip6_defmcasthlim; 2032 else 2033 *hlim = im6o->im6o_multicast_hlim; 2034 return (0); 2035 2036 case IPV6_MULTICAST_LOOP: 2037 loop = mtod(*mp, u_int *); 2038 (*mp)->m_len = sizeof(u_int); 2039 if (im6o == NULL) 2040 *loop = ip6_defmcasthlim; 2041 else 2042 *loop = im6o->im6o_multicast_loop; 2043 return (0); 2044 2045 default: 2046 return (EOPNOTSUPP); 2047 } 2048 } 2049 2050 /* 2051 * Discard the IP6 multicast options. 2052 */ 2053 void 2054 ip6_freemoptions(im6o) 2055 struct ip6_moptions *im6o; 2056 { 2057 struct in6_multi_mship *imm; 2058 2059 if (im6o == NULL) 2060 return; 2061 2062 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2063 LIST_REMOVE(imm, i6mm_chain); 2064 in6_leavegroup(imm); 2065 } 2066 free(im6o, M_IPMOPTS); 2067 } 2068 2069 /* 2070 * Set IPv6 outgoing packet options based on advanced API. 2071 */ 2072 int 2073 ip6_setpktoptions(control, opt, priv) 2074 struct mbuf *control; 2075 struct ip6_pktopts *opt; 2076 int priv; 2077 { 2078 struct cmsghdr *cm = 0; 2079 2080 if (control == 0 || opt == 0) 2081 return (EINVAL); 2082 2083 bzero(opt, sizeof(*opt)); 2084 opt->ip6po_hlim = -1; /* -1 means to use default hop limit */ 2085 2086 /* 2087 * XXX: Currently, we assume all the optional information is stored 2088 * in a single mbuf. 2089 */ 2090 if (control->m_next) 2091 return (EINVAL); 2092 2093 opt->ip6po_m = control; 2094 2095 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2096 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2097 cm = mtod(control, struct cmsghdr *); 2098 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2099 return (EINVAL); 2100 if (cm->cmsg_level != IPPROTO_IPV6) 2101 continue; 2102 2103 switch (cm->cmsg_type) { 2104 case IPV6_PKTINFO: 2105 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo))) 2106 return (EINVAL); 2107 opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm); 2108 if (opt->ip6po_pktinfo->ipi6_ifindex && 2109 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr)) 2110 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] = 2111 htons(opt->ip6po_pktinfo->ipi6_ifindex); 2112 2113 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index || 2114 opt->ip6po_pktinfo->ipi6_ifindex < 0) { 2115 return (ENXIO); 2116 } 2117 2118 /* 2119 * Check if the requested source address is indeed a 2120 * unicast address assigned to the node, and can be 2121 * used as the packet's source address. 2122 */ 2123 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) { 2124 struct ifaddr *ia; 2125 struct in6_ifaddr *ia6; 2126 struct sockaddr_in6 sin6; 2127 2128 bzero(&sin6, sizeof(sin6)); 2129 sin6.sin6_len = sizeof(sin6); 2130 sin6.sin6_family = AF_INET6; 2131 sin6.sin6_addr = 2132 opt->ip6po_pktinfo->ipi6_addr; 2133 ia = ifa_ifwithaddr(sin6tosa(&sin6)); 2134 if (ia == NULL || 2135 (opt->ip6po_pktinfo->ipi6_ifindex && 2136 (ia->ifa_ifp->if_index != 2137 opt->ip6po_pktinfo->ipi6_ifindex))) { 2138 return (EADDRNOTAVAIL); 2139 } 2140 ia6 = (struct in6_ifaddr *)ia; 2141 if ((ia6->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) != 0) { 2142 return (EADDRNOTAVAIL); 2143 } 2144 2145 /* 2146 * Check if the requested source address is 2147 * indeed a unicast address assigned to the 2148 * node. 2149 */ 2150 if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr)) 2151 return (EADDRNOTAVAIL); 2152 } 2153 break; 2154 2155 case IPV6_HOPLIMIT: 2156 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) 2157 return (EINVAL); 2158 2159 bcopy(CMSG_DATA(cm), &opt->ip6po_hlim, 2160 sizeof(opt->ip6po_hlim)); 2161 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255) 2162 return (EINVAL); 2163 break; 2164 2165 case IPV6_NEXTHOP: 2166 if (!priv) 2167 return (EPERM); 2168 2169 /* check if cmsg_len is large enough for sa_len */ 2170 if (cm->cmsg_len < sizeof(u_char) || 2171 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm))) 2172 return (EINVAL); 2173 2174 opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm); 2175 2176 break; 2177 2178 case IPV6_HOPOPTS: 2179 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh))) 2180 return (EINVAL); 2181 opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm); 2182 if (cm->cmsg_len != 2183 CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3)) 2184 return (EINVAL); 2185 break; 2186 2187 case IPV6_DSTOPTS: 2188 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest))) 2189 return (EINVAL); 2190 2191 /* 2192 * If there is no routing header yet, the destination 2193 * options header should be put on the 1st part. 2194 * Otherwise, the header should be on the 2nd part. 2195 * (See RFC 2460, section 4.1) 2196 */ 2197 if (opt->ip6po_rthdr == NULL) { 2198 opt->ip6po_dest1 = 2199 (struct ip6_dest *)CMSG_DATA(cm); 2200 if (cm->cmsg_len != 2201 CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1) << 3)); 2202 return (EINVAL); 2203 } 2204 else { 2205 opt->ip6po_dest2 = 2206 (struct ip6_dest *)CMSG_DATA(cm); 2207 if (cm->cmsg_len != 2208 CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1) << 3)) 2209 return (EINVAL); 2210 } 2211 break; 2212 2213 case IPV6_RTHDR: 2214 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr))) 2215 return (EINVAL); 2216 opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm); 2217 if (cm->cmsg_len != 2218 CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3)) 2219 return (EINVAL); 2220 switch (opt->ip6po_rthdr->ip6r_type) { 2221 case IPV6_RTHDR_TYPE_0: 2222 if (opt->ip6po_rthdr->ip6r_segleft == 0) 2223 return (EINVAL); 2224 break; 2225 default: 2226 return (EINVAL); 2227 } 2228 break; 2229 2230 default: 2231 return (ENOPROTOOPT); 2232 } 2233 } 2234 2235 return (0); 2236 } 2237 2238 /* 2239 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 2240 * packet to the input queue of a specified interface. Note that this 2241 * calls the output routine of the loopback "driver", but with an interface 2242 * pointer that might NOT be lo0ifp -- easier than replicating that code here. 2243 */ 2244 void 2245 ip6_mloopback(ifp, m, dst) 2246 struct ifnet *ifp; 2247 struct mbuf *m; 2248 struct sockaddr_in6 *dst; 2249 { 2250 struct mbuf *copym; 2251 struct ip6_hdr *ip6; 2252 2253 copym = m_copy(m, 0, M_COPYALL); 2254 if (copym == NULL) 2255 return; 2256 2257 /* 2258 * Make sure to deep-copy IPv6 header portion in case the data 2259 * is in an mbuf cluster, so that we can safely override the IPv6 2260 * header portion later. 2261 */ 2262 if ((copym->m_flags & M_EXT) != 0 || 2263 copym->m_len < sizeof(struct ip6_hdr)) { 2264 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 2265 if (copym == NULL) 2266 return; 2267 } 2268 2269 #ifdef DIAGNOSTIC 2270 if (copym->m_len < sizeof(*ip6)) { 2271 m_freem(copym); 2272 return; 2273 } 2274 #endif 2275 2276 ip6 = mtod(copym, struct ip6_hdr *); 2277 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 2278 ip6->ip6_src.s6_addr16[1] = 0; 2279 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 2280 ip6->ip6_dst.s6_addr16[1] = 0; 2281 2282 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL); 2283 } 2284 2285 /* 2286 * Chop IPv6 header off from the payload. 2287 */ 2288 static int 2289 ip6_splithdr(m, exthdrs) 2290 struct mbuf *m; 2291 struct ip6_exthdrs *exthdrs; 2292 { 2293 struct mbuf *mh; 2294 struct ip6_hdr *ip6; 2295 2296 ip6 = mtod(m, struct ip6_hdr *); 2297 if (m->m_len > sizeof(*ip6)) { 2298 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 2299 if (mh == 0) { 2300 m_freem(m); 2301 return ENOBUFS; 2302 } 2303 M_MOVE_PKTHDR(mh, m); 2304 MH_ALIGN(mh, sizeof(*ip6)); 2305 m->m_len -= sizeof(*ip6); 2306 m->m_data += sizeof(*ip6); 2307 mh->m_next = m; 2308 m = mh; 2309 m->m_len = sizeof(*ip6); 2310 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 2311 } 2312 exthdrs->ip6e_ip6 = m; 2313 return 0; 2314 } 2315 2316 /* 2317 * Compute IPv6 extension header length. 2318 */ 2319 int 2320 ip6_optlen(inp) 2321 struct inpcb *inp; 2322 { 2323 int len; 2324 2325 if (!inp->inp_outputopts6) 2326 return 0; 2327 2328 len = 0; 2329 #define elen(x) \ 2330 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 2331 2332 len += elen(inp->inp_outputopts6->ip6po_hbh); 2333 len += elen(inp->inp_outputopts6->ip6po_dest1); 2334 len += elen(inp->inp_outputopts6->ip6po_rthdr); 2335 len += elen(inp->inp_outputopts6->ip6po_dest2); 2336 return len; 2337 #undef elen 2338 } 2339