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