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