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