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