1 /* $OpenBSD: ip6_output.c,v 1.216 2016/09/19 18:09:09 tedu Exp $ */ 2 /* $KAME: ip6_output.c,v 1.172 2001/03/25 09:55:56 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988, 1990, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 62 */ 63 64 #include "pf.h" 65 66 #include <sys/param.h> 67 #include <sys/malloc.h> 68 #include <sys/mbuf.h> 69 #include <sys/errno.h> 70 #include <sys/protosw.h> 71 #include <sys/socket.h> 72 #include <sys/socketvar.h> 73 #include <sys/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 *, int *); 130 int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int); 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 alwaysfrag, 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(lo0ifidx); 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, &alwaysfrag)) != 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, alwaysfrag == 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 * 3: if we always need to attach fragment header (alwaysfrag == 1) 666 * always fragment 667 * 668 * 4: if dontfrag == 1 && alwaysfrag == 1 669 * error, as we cannot handle this conflicting request 670 */ 671 tlen = m->m_pkthdr.len; 672 673 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) 674 dontfrag = 1; 675 else 676 dontfrag = 0; 677 if (dontfrag && alwaysfrag) { /* case 4 */ 678 /* conflicting request - can't transmit */ 679 error = EMSGSIZE; 680 goto bad; 681 } 682 if (dontfrag && tlen > ifp->if_mtu) { /* case 2-b */ 683 error = EMSGSIZE; 684 goto bad; 685 } 686 687 /* 688 * transmit packet without fragmentation 689 */ 690 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */ 691 error = ifp->if_output(ifp, m, sin6tosa(dst), ro->ro_rt); 692 goto done; 693 } 694 695 /* 696 * try to fragment the packet. case 1-b and 3 697 */ 698 if (mtu < IPV6_MMTU) { 699 /* path MTU cannot be less than IPV6_MMTU */ 700 error = EMSGSIZE; 701 goto bad; 702 } else if (ip6->ip6_plen == 0) { 703 /* jumbo payload cannot be fragmented */ 704 error = EMSGSIZE; 705 goto bad; 706 } else { 707 u_char nextproto; 708 #if 0 709 struct ip6ctlparam ip6cp; 710 u_int32_t mtu32; 711 #endif 712 713 /* 714 * Too large for the destination or interface; 715 * fragment if possible. 716 * Must be able to put at least 8 bytes per fragment. 717 */ 718 hlen = unfragpartlen; 719 if (mtu > IPV6_MAXPACKET) 720 mtu = IPV6_MAXPACKET; 721 722 #if 0 723 /* Notify a proper path MTU to applications. */ 724 mtu32 = (u_int32_t)mtu; 725 bzero(&ip6cp, sizeof(ip6cp)); 726 ip6cp.ip6c_cmdarg = (void *)&mtu32; 727 pfctlinput2(PRC_MSGSIZE, sin6tosa(&ro_pmtu->ro_dst), 728 (void *)&ip6cp); 729 #endif 730 731 /* 732 * Change the next header field of the last header in the 733 * unfragmentable part. 734 */ 735 if (exthdrs.ip6e_rthdr) { 736 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 737 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 738 } else if (exthdrs.ip6e_dest1) { 739 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 740 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 741 } else if (exthdrs.ip6e_hbh) { 742 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 743 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 744 } else { 745 nextproto = ip6->ip6_nxt; 746 ip6->ip6_nxt = IPPROTO_FRAGMENT; 747 } 748 749 m0 = m; 750 error = ip6_fragment(m0, hlen, nextproto, mtu); 751 if (error) 752 ip6stat.ip6s_odropped++; 753 } 754 755 /* 756 * Remove leading garbages. 757 */ 758 m = m0->m_nextpkt; 759 m0->m_nextpkt = 0; 760 m_freem(m0); 761 for (m0 = m; m; m = m0) { 762 m0 = m->m_nextpkt; 763 m->m_nextpkt = 0; 764 if (error == 0) { 765 ip6stat.ip6s_ofragments++; 766 error = ifp->if_output(ifp, m, sin6tosa(dst), 767 ro->ro_rt); 768 } else 769 m_freem(m); 770 } 771 772 if (error == 0) 773 ip6stat.ip6s_fragmented++; 774 775 done: 776 if_put(ifp); 777 if (ro == &ip6route && ro->ro_rt) { 778 rtfree(ro->ro_rt); 779 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) { 780 rtfree(ro_pmtu->ro_rt); 781 } 782 783 return (error); 784 785 freehdrs: 786 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 787 m_freem(exthdrs.ip6e_dest1); 788 m_freem(exthdrs.ip6e_rthdr); 789 m_freem(exthdrs.ip6e_dest2); 790 /* FALLTHROUGH */ 791 bad: 792 m_freem(m); 793 goto done; 794 } 795 796 int 797 ip6_fragment(struct mbuf *m0, int hlen, u_char nextproto, u_long mtu) 798 { 799 struct mbuf *m, **mnext, *m_frgpart; 800 struct ip6_hdr *mhip6; 801 struct ip6_frag *ip6f; 802 u_int32_t id; 803 int tlen, len, off; 804 int error; 805 806 id = htonl(ip6_randomid()); 807 808 mnext = &m0->m_nextpkt; 809 *mnext = NULL; 810 811 tlen = m0->m_pkthdr.len; 812 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 813 if (len < 8) 814 return (EMSGSIZE); 815 816 /* 817 * Loop through length of segment after first fragment, 818 * make new header and copy data of each part and link onto 819 * chain. 820 */ 821 for (off = hlen; off < tlen; off += len) { 822 struct mbuf *mlast; 823 824 if ((m = m_gethdr(M_DONTWAIT, MT_HEADER)) == NULL) 825 return (ENOBUFS); 826 *mnext = m; 827 mnext = &m->m_nextpkt; 828 if ((error = m_dup_pkthdr(m, m0, M_DONTWAIT)) != 0) 829 return (error); 830 m->m_data += max_linkhdr; 831 mhip6 = mtod(m, struct ip6_hdr *); 832 *mhip6 = *mtod(m0, struct ip6_hdr *); 833 m->m_len = sizeof(*mhip6); 834 if ((error = ip6_insertfraghdr(m0, m, hlen, &ip6f)) != 0) 835 return (error); 836 ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7)); 837 if (off + len >= tlen) 838 len = tlen - off; 839 else 840 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 841 mhip6->ip6_plen = htons((u_int16_t)(len + hlen + 842 sizeof(*ip6f) - sizeof(struct ip6_hdr))); 843 if ((m_frgpart = m_copym(m0, off, len, M_DONTWAIT)) == NULL) 844 return (ENOBUFS); 845 for (mlast = m; mlast->m_next; mlast = mlast->m_next) 846 ; 847 mlast->m_next = m_frgpart; 848 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 849 ip6f->ip6f_reserved = 0; 850 ip6f->ip6f_ident = id; 851 ip6f->ip6f_nxt = nextproto; 852 } 853 854 return (0); 855 } 856 857 int 858 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen) 859 { 860 struct mbuf *m; 861 862 if (hlen > MCLBYTES) 863 return (ENOBUFS); /* XXX */ 864 865 MGET(m, M_DONTWAIT, MT_DATA); 866 if (!m) 867 return (ENOBUFS); 868 869 if (hlen > MLEN) { 870 MCLGET(m, M_DONTWAIT); 871 if ((m->m_flags & M_EXT) == 0) { 872 m_free(m); 873 return (ENOBUFS); 874 } 875 } 876 m->m_len = hlen; 877 if (hdr) 878 bcopy(hdr, mtod(m, caddr_t), hlen); 879 880 *mp = m; 881 return (0); 882 } 883 884 /* 885 * Insert jumbo payload option. 886 */ 887 int 888 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) 889 { 890 struct mbuf *mopt; 891 u_int8_t *optbuf; 892 u_int32_t v; 893 894 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 895 896 /* 897 * If there is no hop-by-hop options header, allocate new one. 898 * If there is one but it doesn't have enough space to store the 899 * jumbo payload option, allocate a cluster to store the whole options. 900 * Otherwise, use it to store the options. 901 */ 902 if (exthdrs->ip6e_hbh == 0) { 903 MGET(mopt, M_DONTWAIT, MT_DATA); 904 if (mopt == NULL) 905 return (ENOBUFS); 906 mopt->m_len = JUMBOOPTLEN; 907 optbuf = mtod(mopt, u_int8_t *); 908 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 909 exthdrs->ip6e_hbh = mopt; 910 } else { 911 struct ip6_hbh *hbh; 912 913 mopt = exthdrs->ip6e_hbh; 914 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 915 /* 916 * XXX assumption: 917 * - exthdrs->ip6e_hbh is not referenced from places 918 * other than exthdrs. 919 * - exthdrs->ip6e_hbh is not an mbuf chain. 920 */ 921 int oldoptlen = mopt->m_len; 922 struct mbuf *n; 923 924 /* 925 * XXX: give up if the whole (new) hbh header does 926 * not fit even in an mbuf cluster. 927 */ 928 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 929 return (ENOBUFS); 930 931 /* 932 * As a consequence, we must always prepare a cluster 933 * at this point. 934 */ 935 MGET(n, M_DONTWAIT, MT_DATA); 936 if (n) { 937 MCLGET(n, M_DONTWAIT); 938 if ((n->m_flags & M_EXT) == 0) { 939 m_freem(n); 940 n = NULL; 941 } 942 } 943 if (!n) 944 return (ENOBUFS); 945 n->m_len = oldoptlen + JUMBOOPTLEN; 946 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 947 oldoptlen); 948 optbuf = mtod(n, u_int8_t *) + oldoptlen; 949 m_freem(mopt); 950 mopt = exthdrs->ip6e_hbh = n; 951 } else { 952 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len; 953 mopt->m_len += JUMBOOPTLEN; 954 } 955 optbuf[0] = IP6OPT_PADN; 956 optbuf[1] = 0; 957 958 /* 959 * Adjust the header length according to the pad and 960 * the jumbo payload option. 961 */ 962 hbh = mtod(mopt, struct ip6_hbh *); 963 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 964 } 965 966 /* fill in the option. */ 967 optbuf[2] = IP6OPT_JUMBO; 968 optbuf[3] = 4; 969 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 970 memcpy(&optbuf[4], &v, sizeof(u_int32_t)); 971 972 /* finally, adjust the packet header length */ 973 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 974 975 return (0); 976 #undef JUMBOOPTLEN 977 } 978 979 /* 980 * Insert fragment header and copy unfragmentable header portions. 981 */ 982 int 983 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, 984 struct ip6_frag **frghdrp) 985 { 986 struct mbuf *n, *mlast; 987 988 if (hlen > sizeof(struct ip6_hdr)) { 989 n = m_copym(m0, sizeof(struct ip6_hdr), 990 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 991 if (n == NULL) 992 return (ENOBUFS); 993 m->m_next = n; 994 } else 995 n = m; 996 997 /* Search for the last mbuf of unfragmentable part. */ 998 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 999 ; 1000 1001 if ((mlast->m_flags & M_EXT) == 0 && 1002 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1003 /* use the trailing space of the last mbuf for the fragment hdr */ 1004 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) + 1005 mlast->m_len); 1006 mlast->m_len += sizeof(struct ip6_frag); 1007 m->m_pkthdr.len += sizeof(struct ip6_frag); 1008 } else { 1009 /* allocate a new mbuf for the fragment header */ 1010 struct mbuf *mfrg; 1011 1012 MGET(mfrg, M_DONTWAIT, MT_DATA); 1013 if (mfrg == NULL) 1014 return (ENOBUFS); 1015 mfrg->m_len = sizeof(struct ip6_frag); 1016 *frghdrp = mtod(mfrg, struct ip6_frag *); 1017 mlast->m_next = mfrg; 1018 } 1019 1020 return (0); 1021 } 1022 1023 int 1024 ip6_getpmtu(struct rtentry *rt, struct ifnet *ifp, u_long *mtup, 1025 int *alwaysfragp) 1026 { 1027 u_int32_t mtu = 0; 1028 int alwaysfrag = 0; 1029 int error = 0; 1030 1031 if (rt != NULL) { 1032 mtu = rt->rt_rmx.rmx_mtu; 1033 if (mtu == 0) 1034 mtu = ifp->if_mtu; 1035 else if (mtu < IPV6_MMTU) { 1036 /* 1037 * RFC2460 section 5, last paragraph: 1038 * if we record ICMPv6 too big message with 1039 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU 1040 * or smaller, with fragment header attached. 1041 * (fragment header is needed regardless from the 1042 * packet size, for translators to identify packets) 1043 */ 1044 alwaysfrag = 1; 1045 mtu = IPV6_MMTU; 1046 } else if (mtu > ifp->if_mtu) { 1047 /* 1048 * The MTU on the route is larger than the MTU on 1049 * the interface! This shouldn't happen, unless the 1050 * MTU of the interface has been changed after the 1051 * interface was brought up. Change the MTU in the 1052 * route to match the interface MTU (as long as the 1053 * field isn't locked). 1054 */ 1055 mtu = ifp->if_mtu; 1056 if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) 1057 rt->rt_rmx.rmx_mtu = mtu; 1058 } 1059 } else { 1060 mtu = ifp->if_mtu; 1061 } 1062 1063 *mtup = mtu; 1064 if (alwaysfragp) 1065 *alwaysfragp = alwaysfrag; 1066 return (error); 1067 } 1068 1069 /* 1070 * IP6 socket option processing. 1071 */ 1072 int 1073 ip6_ctloutput(int op, struct socket *so, int level, int optname, 1074 struct mbuf **mp) 1075 { 1076 int privileged, optdatalen, uproto; 1077 void *optdata; 1078 struct inpcb *inp = sotoinpcb(so); 1079 struct mbuf *m = *mp; 1080 int error, optval; 1081 struct proc *p = curproc; /* For IPSec and rdomain */ 1082 u_int rtid = 0; 1083 1084 error = optval = 0; 1085 1086 privileged = (inp->inp_socket->so_state & SS_PRIV); 1087 uproto = (int)so->so_proto->pr_protocol; 1088 1089 if (level == IPPROTO_IPV6) { 1090 switch (op) { 1091 case PRCO_SETOPT: 1092 switch (optname) { 1093 /* 1094 * Use of some Hop-by-Hop options or some 1095 * Destination options, might require special 1096 * privilege. That is, normal applications 1097 * (without special privilege) might be forbidden 1098 * from setting certain options in outgoing packets, 1099 * and might never see certain options in received 1100 * packets. [RFC 2292 Section 6] 1101 * KAME specific note: 1102 * KAME prevents non-privileged users from sending or 1103 * receiving ANY hbh/dst options in order to avoid 1104 * overhead of parsing options in the kernel. 1105 */ 1106 case IPV6_RECVHOPOPTS: 1107 case IPV6_RECVDSTOPTS: 1108 if (!privileged) { 1109 error = EPERM; 1110 break; 1111 } 1112 /* FALLTHROUGH */ 1113 case IPV6_UNICAST_HOPS: 1114 case IPV6_MINHOPCOUNT: 1115 case IPV6_HOPLIMIT: 1116 1117 case IPV6_RECVPKTINFO: 1118 case IPV6_RECVHOPLIMIT: 1119 case IPV6_RECVRTHDR: 1120 case IPV6_RECVPATHMTU: 1121 case IPV6_RECVTCLASS: 1122 case IPV6_V6ONLY: 1123 case IPV6_AUTOFLOWLABEL: 1124 case IPV6_RECVDSTPORT: 1125 if (m == NULL || m->m_len != sizeof(int)) { 1126 error = EINVAL; 1127 break; 1128 } 1129 optval = *mtod(m, int *); 1130 switch (optname) { 1131 1132 case IPV6_UNICAST_HOPS: 1133 if (optval < -1 || optval >= 256) 1134 error = EINVAL; 1135 else { 1136 /* -1 = kernel default */ 1137 inp->inp_hops = optval; 1138 } 1139 break; 1140 1141 case IPV6_MINHOPCOUNT: 1142 if (optval < 0 || optval > 255) 1143 error = EINVAL; 1144 else 1145 inp->inp_ip6_minhlim = optval; 1146 break; 1147 1148 #define OPTSET(bit) \ 1149 do { \ 1150 if (optval) \ 1151 inp->inp_flags |= (bit); \ 1152 else \ 1153 inp->inp_flags &= ~(bit); \ 1154 } while (/*CONSTCOND*/ 0) 1155 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0) 1156 1157 case IPV6_RECVPKTINFO: 1158 OPTSET(IN6P_PKTINFO); 1159 break; 1160 1161 case IPV6_HOPLIMIT: 1162 { 1163 struct ip6_pktopts **optp; 1164 1165 optp = &inp->inp_outputopts6; 1166 error = ip6_pcbopt(IPV6_HOPLIMIT, 1167 (u_char *)&optval, 1168 sizeof(optval), 1169 optp, 1170 privileged, uproto); 1171 break; 1172 } 1173 1174 case IPV6_RECVHOPLIMIT: 1175 OPTSET(IN6P_HOPLIMIT); 1176 break; 1177 1178 case IPV6_RECVHOPOPTS: 1179 OPTSET(IN6P_HOPOPTS); 1180 break; 1181 1182 case IPV6_RECVDSTOPTS: 1183 OPTSET(IN6P_DSTOPTS); 1184 break; 1185 1186 case IPV6_RECVRTHDR: 1187 OPTSET(IN6P_RTHDR); 1188 break; 1189 1190 case IPV6_RECVPATHMTU: 1191 /* 1192 * We ignore this option for TCP 1193 * sockets. 1194 * (RFC3542 leaves this case 1195 * unspecified.) 1196 */ 1197 if (uproto != IPPROTO_TCP) 1198 OPTSET(IN6P_MTU); 1199 break; 1200 1201 case IPV6_V6ONLY: 1202 /* 1203 * make setsockopt(IPV6_V6ONLY) 1204 * available only prior to bind(2). 1205 * see ipng mailing list, Jun 22 2001. 1206 */ 1207 if (inp->inp_lport || 1208 !IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) { 1209 error = EINVAL; 1210 break; 1211 } 1212 /* No support for IPv4-mapped addresses. */ 1213 if (!optval) 1214 error = EINVAL; 1215 else 1216 error = 0; 1217 break; 1218 case IPV6_RECVTCLASS: 1219 OPTSET(IN6P_TCLASS); 1220 break; 1221 case IPV6_AUTOFLOWLABEL: 1222 OPTSET(IN6P_AUTOFLOWLABEL); 1223 break; 1224 1225 case IPV6_RECVDSTPORT: 1226 OPTSET(IN6P_RECVDSTPORT); 1227 break; 1228 } 1229 break; 1230 1231 case IPV6_TCLASS: 1232 case IPV6_DONTFRAG: 1233 case IPV6_USE_MIN_MTU: 1234 if (m == NULL || m->m_len != sizeof(optval)) { 1235 error = EINVAL; 1236 break; 1237 } 1238 optval = *mtod(m, int *); 1239 { 1240 struct ip6_pktopts **optp; 1241 optp = &inp->inp_outputopts6; 1242 error = ip6_pcbopt(optname, 1243 (u_char *)&optval, 1244 sizeof(optval), 1245 optp, 1246 privileged, uproto); 1247 break; 1248 } 1249 1250 case IPV6_PKTINFO: 1251 case IPV6_HOPOPTS: 1252 case IPV6_RTHDR: 1253 case IPV6_DSTOPTS: 1254 case IPV6_RTHDRDSTOPTS: 1255 { 1256 /* new advanced API (RFC3542) */ 1257 u_char *optbuf; 1258 int optbuflen; 1259 struct ip6_pktopts **optp; 1260 1261 if (m && m->m_next) { 1262 error = EINVAL; /* XXX */ 1263 break; 1264 } 1265 if (m) { 1266 optbuf = mtod(m, u_char *); 1267 optbuflen = m->m_len; 1268 } else { 1269 optbuf = NULL; 1270 optbuflen = 0; 1271 } 1272 optp = &inp->inp_outputopts6; 1273 error = ip6_pcbopt(optname, 1274 optbuf, optbuflen, 1275 optp, privileged, uproto); 1276 break; 1277 } 1278 #undef OPTSET 1279 1280 case IPV6_MULTICAST_IF: 1281 case IPV6_MULTICAST_HOPS: 1282 case IPV6_MULTICAST_LOOP: 1283 case IPV6_JOIN_GROUP: 1284 case IPV6_LEAVE_GROUP: 1285 error = ip6_setmoptions(optname, 1286 &inp->inp_moptions6, 1287 m); 1288 break; 1289 1290 case IPV6_PORTRANGE: 1291 if (m == NULL || m->m_len != sizeof(int)) { 1292 error = EINVAL; 1293 break; 1294 } 1295 optval = *mtod(m, int *); 1296 1297 switch (optval) { 1298 case IPV6_PORTRANGE_DEFAULT: 1299 inp->inp_flags &= ~(IN6P_LOWPORT); 1300 inp->inp_flags &= ~(IN6P_HIGHPORT); 1301 break; 1302 1303 case IPV6_PORTRANGE_HIGH: 1304 inp->inp_flags &= ~(IN6P_LOWPORT); 1305 inp->inp_flags |= IN6P_HIGHPORT; 1306 break; 1307 1308 case IPV6_PORTRANGE_LOW: 1309 inp->inp_flags &= ~(IN6P_HIGHPORT); 1310 inp->inp_flags |= IN6P_LOWPORT; 1311 break; 1312 1313 default: 1314 error = EINVAL; 1315 break; 1316 } 1317 break; 1318 1319 case IPSEC6_OUTSA: 1320 error = EINVAL; 1321 break; 1322 1323 case IPV6_AUTH_LEVEL: 1324 case IPV6_ESP_TRANS_LEVEL: 1325 case IPV6_ESP_NETWORK_LEVEL: 1326 case IPV6_IPCOMP_LEVEL: 1327 #ifndef IPSEC 1328 error = EINVAL; 1329 #else 1330 if (m == NULL || m->m_len != sizeof(int)) { 1331 error = EINVAL; 1332 break; 1333 } 1334 optval = *mtod(m, int *); 1335 1336 if (optval < IPSEC_LEVEL_BYPASS || 1337 optval > IPSEC_LEVEL_UNIQUE) { 1338 error = EINVAL; 1339 break; 1340 } 1341 1342 switch (optname) { 1343 case IPV6_AUTH_LEVEL: 1344 if (optval < IPSEC_AUTH_LEVEL_DEFAULT && 1345 suser(p, 0)) { 1346 error = EACCES; 1347 break; 1348 } 1349 inp->inp_seclevel[SL_AUTH] = optval; 1350 break; 1351 1352 case IPV6_ESP_TRANS_LEVEL: 1353 if (optval < IPSEC_ESP_TRANS_LEVEL_DEFAULT && 1354 suser(p, 0)) { 1355 error = EACCES; 1356 break; 1357 } 1358 inp->inp_seclevel[SL_ESP_TRANS] = optval; 1359 break; 1360 1361 case IPV6_ESP_NETWORK_LEVEL: 1362 if (optval < IPSEC_ESP_NETWORK_LEVEL_DEFAULT && 1363 suser(p, 0)) { 1364 error = EACCES; 1365 break; 1366 } 1367 inp->inp_seclevel[SL_ESP_NETWORK] = optval; 1368 break; 1369 1370 case IPV6_IPCOMP_LEVEL: 1371 if (optval < IPSEC_IPCOMP_LEVEL_DEFAULT && 1372 suser(p, 0)) { 1373 error = EACCES; 1374 break; 1375 } 1376 inp->inp_seclevel[SL_IPCOMP] = optval; 1377 break; 1378 } 1379 #endif 1380 break; 1381 case SO_RTABLE: 1382 if (m == NULL || m->m_len < sizeof(u_int)) { 1383 error = EINVAL; 1384 break; 1385 } 1386 rtid = *mtod(m, u_int *); 1387 if (inp->inp_rtableid == rtid) 1388 break; 1389 /* needs privileges to switch when already set */ 1390 if (p->p_p->ps_rtableid != rtid && 1391 p->p_p->ps_rtableid != 0 && 1392 (error = suser(p, 0)) != 0) 1393 break; 1394 /* table must exist */ 1395 if (!rtable_exists(rtid)) { 1396 error = EINVAL; 1397 break; 1398 } 1399 if (inp->inp_lport) { 1400 error = EBUSY; 1401 break; 1402 } 1403 inp->inp_rtableid = rtid; 1404 in_pcbrehash(inp); 1405 break; 1406 case IPV6_PIPEX: 1407 if (m != NULL && m->m_len == sizeof(int)) 1408 inp->inp_pipex = *mtod(m, int *); 1409 else 1410 error = EINVAL; 1411 break; 1412 1413 default: 1414 error = ENOPROTOOPT; 1415 break; 1416 } 1417 if (m) 1418 (void)m_free(m); 1419 break; 1420 1421 case PRCO_GETOPT: 1422 switch (optname) { 1423 1424 case IPV6_RECVHOPOPTS: 1425 case IPV6_RECVDSTOPTS: 1426 case IPV6_UNICAST_HOPS: 1427 case IPV6_MINHOPCOUNT: 1428 case IPV6_RECVPKTINFO: 1429 case IPV6_RECVHOPLIMIT: 1430 case IPV6_RECVRTHDR: 1431 case IPV6_RECVPATHMTU: 1432 1433 case IPV6_V6ONLY: 1434 case IPV6_PORTRANGE: 1435 case IPV6_RECVTCLASS: 1436 case IPV6_AUTOFLOWLABEL: 1437 case IPV6_RECVDSTPORT: 1438 switch (optname) { 1439 1440 case IPV6_RECVHOPOPTS: 1441 optval = OPTBIT(IN6P_HOPOPTS); 1442 break; 1443 1444 case IPV6_RECVDSTOPTS: 1445 optval = OPTBIT(IN6P_DSTOPTS); 1446 break; 1447 1448 case IPV6_UNICAST_HOPS: 1449 optval = inp->inp_hops; 1450 break; 1451 1452 case IPV6_MINHOPCOUNT: 1453 optval = inp->inp_ip6_minhlim; 1454 break; 1455 1456 case IPV6_RECVPKTINFO: 1457 optval = OPTBIT(IN6P_PKTINFO); 1458 break; 1459 1460 case IPV6_RECVHOPLIMIT: 1461 optval = OPTBIT(IN6P_HOPLIMIT); 1462 break; 1463 1464 case IPV6_RECVRTHDR: 1465 optval = OPTBIT(IN6P_RTHDR); 1466 break; 1467 1468 case IPV6_RECVPATHMTU: 1469 optval = OPTBIT(IN6P_MTU); 1470 break; 1471 1472 case IPV6_V6ONLY: 1473 optval = 1; 1474 break; 1475 1476 case IPV6_PORTRANGE: 1477 { 1478 int flags; 1479 flags = inp->inp_flags; 1480 if (flags & IN6P_HIGHPORT) 1481 optval = IPV6_PORTRANGE_HIGH; 1482 else if (flags & IN6P_LOWPORT) 1483 optval = IPV6_PORTRANGE_LOW; 1484 else 1485 optval = 0; 1486 break; 1487 } 1488 case IPV6_RECVTCLASS: 1489 optval = OPTBIT(IN6P_TCLASS); 1490 break; 1491 1492 case IPV6_AUTOFLOWLABEL: 1493 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 1494 break; 1495 1496 case IPV6_RECVDSTPORT: 1497 optval = OPTBIT(IN6P_RECVDSTPORT); 1498 break; 1499 } 1500 if (error) 1501 break; 1502 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1503 m->m_len = sizeof(int); 1504 *mtod(m, int *) = optval; 1505 break; 1506 1507 case IPV6_PATHMTU: 1508 { 1509 u_long pmtu = 0; 1510 struct ip6_mtuinfo mtuinfo; 1511 struct ifnet *ifp; 1512 struct rtentry *rt; 1513 1514 if (!(so->so_state & SS_ISCONNECTED)) 1515 return (ENOTCONN); 1516 1517 rt = in_pcbrtentry(inp); 1518 if (!rtisvalid(rt)) 1519 return (EHOSTUNREACH); 1520 1521 ifp = if_get(rt->rt_ifidx); 1522 if (ifp == NULL) 1523 return (EHOSTUNREACH); 1524 /* 1525 * XXX: we dot not consider the case of source 1526 * routing, or optional information to specify 1527 * the outgoing interface. 1528 */ 1529 error = ip6_getpmtu(rt, ifp, &pmtu, NULL); 1530 if_put(ifp); 1531 if (error) 1532 break; 1533 if (pmtu > IPV6_MAXPACKET) 1534 pmtu = IPV6_MAXPACKET; 1535 1536 bzero(&mtuinfo, sizeof(mtuinfo)); 1537 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 1538 optdata = (void *)&mtuinfo; 1539 optdatalen = sizeof(mtuinfo); 1540 if (optdatalen > MCLBYTES) 1541 return (EMSGSIZE); /* XXX */ 1542 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1543 if (optdatalen > MLEN) 1544 MCLGET(m, M_WAIT); 1545 m->m_len = optdatalen; 1546 bcopy(optdata, mtod(m, void *), optdatalen); 1547 break; 1548 } 1549 1550 case IPV6_PKTINFO: 1551 case IPV6_HOPOPTS: 1552 case IPV6_RTHDR: 1553 case IPV6_DSTOPTS: 1554 case IPV6_RTHDRDSTOPTS: 1555 case IPV6_TCLASS: 1556 case IPV6_DONTFRAG: 1557 case IPV6_USE_MIN_MTU: 1558 error = ip6_getpcbopt(inp->inp_outputopts6, 1559 optname, mp); 1560 break; 1561 1562 case IPV6_MULTICAST_IF: 1563 case IPV6_MULTICAST_HOPS: 1564 case IPV6_MULTICAST_LOOP: 1565 case IPV6_JOIN_GROUP: 1566 case IPV6_LEAVE_GROUP: 1567 error = ip6_getmoptions(optname, 1568 inp->inp_moptions6, mp); 1569 break; 1570 1571 case IPSEC6_OUTSA: 1572 error = EINVAL; 1573 break; 1574 1575 case IPV6_AUTH_LEVEL: 1576 case IPV6_ESP_TRANS_LEVEL: 1577 case IPV6_ESP_NETWORK_LEVEL: 1578 case IPV6_IPCOMP_LEVEL: 1579 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1580 #ifndef IPSEC 1581 m->m_len = sizeof(int); 1582 *mtod(m, int *) = IPSEC_LEVEL_NONE; 1583 #else 1584 m->m_len = sizeof(int); 1585 switch (optname) { 1586 case IPV6_AUTH_LEVEL: 1587 optval = inp->inp_seclevel[SL_AUTH]; 1588 break; 1589 1590 case IPV6_ESP_TRANS_LEVEL: 1591 optval = 1592 inp->inp_seclevel[SL_ESP_TRANS]; 1593 break; 1594 1595 case IPV6_ESP_NETWORK_LEVEL: 1596 optval = 1597 inp->inp_seclevel[SL_ESP_NETWORK]; 1598 break; 1599 1600 case IPV6_IPCOMP_LEVEL: 1601 optval = inp->inp_seclevel[SL_IPCOMP]; 1602 break; 1603 } 1604 *mtod(m, int *) = optval; 1605 #endif 1606 break; 1607 case SO_RTABLE: 1608 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1609 m->m_len = sizeof(u_int); 1610 *mtod(m, u_int *) = optval; 1611 break; 1612 case IPV6_PIPEX: 1613 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1614 m->m_len = sizeof(int); 1615 *mtod(m, int *) = optval; 1616 break; 1617 1618 default: 1619 error = ENOPROTOOPT; 1620 break; 1621 } 1622 break; 1623 } 1624 } else { 1625 error = EINVAL; 1626 if (op == PRCO_SETOPT) 1627 (void)m_free(*mp); 1628 } 1629 return (error); 1630 } 1631 1632 int 1633 ip6_raw_ctloutput(int op, struct socket *so, int level, int optname, 1634 struct mbuf **mp) 1635 { 1636 int error = 0, optval; 1637 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 1638 struct inpcb *inp = sotoinpcb(so); 1639 struct mbuf *m = *mp; 1640 1641 if (level != IPPROTO_IPV6) { 1642 if (op == PRCO_SETOPT) 1643 (void)m_free(*mp); 1644 return (EINVAL); 1645 } 1646 1647 switch (optname) { 1648 case IPV6_CHECKSUM: 1649 /* 1650 * For ICMPv6 sockets, no modification allowed for checksum 1651 * offset, permit "no change" values to help existing apps. 1652 * 1653 * RFC3542 says: "An attempt to set IPV6_CHECKSUM 1654 * for an ICMPv6 socket will fail." 1655 * The current behavior does not meet RFC3542. 1656 */ 1657 switch (op) { 1658 case PRCO_SETOPT: 1659 if (m == NULL || m->m_len != sizeof(int)) { 1660 error = EINVAL; 1661 break; 1662 } 1663 optval = *mtod(m, int *); 1664 if ((optval % 2) != 0) { 1665 /* the API assumes even offset values */ 1666 error = EINVAL; 1667 } else if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 1668 if (optval != icmp6off) 1669 error = EINVAL; 1670 } else 1671 inp->inp_cksum6 = optval; 1672 break; 1673 1674 case PRCO_GETOPT: 1675 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 1676 optval = icmp6off; 1677 else 1678 optval = inp->inp_cksum6; 1679 1680 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1681 m->m_len = sizeof(int); 1682 *mtod(m, int *) = optval; 1683 break; 1684 1685 default: 1686 error = EINVAL; 1687 break; 1688 } 1689 break; 1690 1691 default: 1692 error = ENOPROTOOPT; 1693 break; 1694 } 1695 1696 if (op == PRCO_SETOPT) 1697 (void)m_free(m); 1698 1699 return (error); 1700 } 1701 1702 /* 1703 * initialize ip6_pktopts. beware that there are non-zero default values in 1704 * the struct. 1705 */ 1706 void 1707 ip6_initpktopts(struct ip6_pktopts *opt) 1708 { 1709 1710 bzero(opt, sizeof(*opt)); 1711 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 1712 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 1713 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 1714 } 1715 1716 int 1717 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, 1718 int priv, int uproto) 1719 { 1720 struct ip6_pktopts *opt; 1721 1722 if (*pktopt == NULL) { 1723 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 1724 M_WAITOK); 1725 ip6_initpktopts(*pktopt); 1726 } 1727 opt = *pktopt; 1728 1729 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, uproto)); 1730 } 1731 1732 int 1733 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct mbuf **mp) 1734 { 1735 void *optdata = NULL; 1736 int optdatalen = 0; 1737 struct ip6_ext *ip6e; 1738 int error = 0; 1739 struct in6_pktinfo null_pktinfo; 1740 int deftclass = 0, on; 1741 int defminmtu = IP6PO_MINMTU_MCASTONLY; 1742 struct mbuf *m; 1743 1744 switch (optname) { 1745 case IPV6_PKTINFO: 1746 if (pktopt && pktopt->ip6po_pktinfo) 1747 optdata = (void *)pktopt->ip6po_pktinfo; 1748 else { 1749 /* XXX: we don't have to do this every time... */ 1750 bzero(&null_pktinfo, sizeof(null_pktinfo)); 1751 optdata = (void *)&null_pktinfo; 1752 } 1753 optdatalen = sizeof(struct in6_pktinfo); 1754 break; 1755 case IPV6_TCLASS: 1756 if (pktopt && pktopt->ip6po_tclass >= 0) 1757 optdata = (void *)&pktopt->ip6po_tclass; 1758 else 1759 optdata = (void *)&deftclass; 1760 optdatalen = sizeof(int); 1761 break; 1762 case IPV6_HOPOPTS: 1763 if (pktopt && pktopt->ip6po_hbh) { 1764 optdata = (void *)pktopt->ip6po_hbh; 1765 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; 1766 optdatalen = (ip6e->ip6e_len + 1) << 3; 1767 } 1768 break; 1769 case IPV6_RTHDR: 1770 if (pktopt && pktopt->ip6po_rthdr) { 1771 optdata = (void *)pktopt->ip6po_rthdr; 1772 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; 1773 optdatalen = (ip6e->ip6e_len + 1) << 3; 1774 } 1775 break; 1776 case IPV6_RTHDRDSTOPTS: 1777 if (pktopt && pktopt->ip6po_dest1) { 1778 optdata = (void *)pktopt->ip6po_dest1; 1779 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; 1780 optdatalen = (ip6e->ip6e_len + 1) << 3; 1781 } 1782 break; 1783 case IPV6_DSTOPTS: 1784 if (pktopt && pktopt->ip6po_dest2) { 1785 optdata = (void *)pktopt->ip6po_dest2; 1786 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; 1787 optdatalen = (ip6e->ip6e_len + 1) << 3; 1788 } 1789 break; 1790 case IPV6_USE_MIN_MTU: 1791 if (pktopt) 1792 optdata = (void *)&pktopt->ip6po_minmtu; 1793 else 1794 optdata = (void *)&defminmtu; 1795 optdatalen = sizeof(int); 1796 break; 1797 case IPV6_DONTFRAG: 1798 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 1799 on = 1; 1800 else 1801 on = 0; 1802 optdata = (void *)&on; 1803 optdatalen = sizeof(on); 1804 break; 1805 default: /* should not happen */ 1806 #ifdef DIAGNOSTIC 1807 panic("ip6_getpcbopt: unexpected option"); 1808 #endif 1809 return (ENOPROTOOPT); 1810 } 1811 1812 if (optdatalen > MCLBYTES) 1813 return (EMSGSIZE); /* XXX */ 1814 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1815 if (optdatalen > MLEN) 1816 MCLGET(m, M_WAIT); 1817 m->m_len = optdatalen; 1818 if (optdatalen) 1819 bcopy(optdata, mtod(m, void *), optdatalen); 1820 1821 return (error); 1822 } 1823 1824 void 1825 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) 1826 { 1827 if (optname == -1 || optname == IPV6_PKTINFO) { 1828 if (pktopt->ip6po_pktinfo) 1829 free(pktopt->ip6po_pktinfo, M_IP6OPT, 0); 1830 pktopt->ip6po_pktinfo = NULL; 1831 } 1832 if (optname == -1 || optname == IPV6_HOPLIMIT) 1833 pktopt->ip6po_hlim = -1; 1834 if (optname == -1 || optname == IPV6_TCLASS) 1835 pktopt->ip6po_tclass = -1; 1836 if (optname == -1 || optname == IPV6_HOPOPTS) { 1837 if (pktopt->ip6po_hbh) 1838 free(pktopt->ip6po_hbh, M_IP6OPT, 0); 1839 pktopt->ip6po_hbh = NULL; 1840 } 1841 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 1842 if (pktopt->ip6po_dest1) 1843 free(pktopt->ip6po_dest1, M_IP6OPT, 0); 1844 pktopt->ip6po_dest1 = NULL; 1845 } 1846 if (optname == -1 || optname == IPV6_RTHDR) { 1847 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 1848 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT, 0); 1849 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 1850 if (pktopt->ip6po_route.ro_rt) { 1851 rtfree(pktopt->ip6po_route.ro_rt); 1852 pktopt->ip6po_route.ro_rt = NULL; 1853 } 1854 } 1855 if (optname == -1 || optname == IPV6_DSTOPTS) { 1856 if (pktopt->ip6po_dest2) 1857 free(pktopt->ip6po_dest2, M_IP6OPT, 0); 1858 pktopt->ip6po_dest2 = NULL; 1859 } 1860 } 1861 1862 #define PKTOPT_EXTHDRCPY(type) \ 1863 do {\ 1864 if (src->type) {\ 1865 size_t hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 1866 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 1867 if (dst->type == NULL && canwait == M_NOWAIT)\ 1868 goto bad;\ 1869 memcpy(dst->type, src->type, hlen);\ 1870 }\ 1871 } while (/*CONSTCOND*/ 0) 1872 1873 int 1874 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) 1875 { 1876 dst->ip6po_hlim = src->ip6po_hlim; 1877 dst->ip6po_tclass = src->ip6po_tclass; 1878 dst->ip6po_flags = src->ip6po_flags; 1879 if (src->ip6po_pktinfo) { 1880 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 1881 M_IP6OPT, canwait); 1882 if (dst->ip6po_pktinfo == NULL) 1883 goto bad; 1884 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 1885 } 1886 PKTOPT_EXTHDRCPY(ip6po_hbh); 1887 PKTOPT_EXTHDRCPY(ip6po_dest1); 1888 PKTOPT_EXTHDRCPY(ip6po_dest2); 1889 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 1890 return (0); 1891 1892 bad: 1893 ip6_clearpktopts(dst, -1); 1894 return (ENOBUFS); 1895 } 1896 #undef PKTOPT_EXTHDRCPY 1897 1898 void 1899 ip6_freepcbopts(struct ip6_pktopts *pktopt) 1900 { 1901 if (pktopt == NULL) 1902 return; 1903 1904 ip6_clearpktopts(pktopt, -1); 1905 1906 free(pktopt, M_IP6OPT, 0); 1907 } 1908 1909 /* 1910 * Set the IP6 multicast options in response to user setsockopt(). 1911 */ 1912 int 1913 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m) 1914 { 1915 int error = 0; 1916 u_int loop, ifindex; 1917 struct ipv6_mreq *mreq; 1918 struct ifnet *ifp; 1919 struct ip6_moptions *im6o = *im6op; 1920 struct route_in6 ro; 1921 struct sockaddr_in6 *dst; 1922 struct in6_multi_mship *imm; 1923 struct proc *p = curproc; /* XXX */ 1924 1925 if (im6o == NULL) { 1926 /* 1927 * No multicast option buffer attached to the pcb; 1928 * allocate one and initialize to default values. 1929 */ 1930 im6o = (struct ip6_moptions *) 1931 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK); 1932 1933 if (im6o == NULL) 1934 return (ENOBUFS); 1935 *im6op = im6o; 1936 im6o->im6o_ifidx = 0; 1937 im6o->im6o_hlim = ip6_defmcasthlim; 1938 im6o->im6o_loop = IPV6_DEFAULT_MULTICAST_LOOP; 1939 LIST_INIT(&im6o->im6o_memberships); 1940 } 1941 1942 switch (optname) { 1943 1944 case IPV6_MULTICAST_IF: 1945 /* 1946 * Select the interface for outgoing multicast packets. 1947 */ 1948 if (m == NULL || m->m_len != sizeof(u_int)) { 1949 error = EINVAL; 1950 break; 1951 } 1952 memcpy(&ifindex, mtod(m, u_int *), sizeof(ifindex)); 1953 if (ifindex != 0) { 1954 ifp = if_get(ifindex); 1955 if (ifp == NULL) { 1956 error = ENXIO; /* XXX EINVAL? */ 1957 break; 1958 } 1959 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 1960 error = EADDRNOTAVAIL; 1961 if_put(ifp); 1962 break; 1963 } 1964 if_put(ifp); 1965 } 1966 im6o->im6o_ifidx = ifindex; 1967 break; 1968 1969 case IPV6_MULTICAST_HOPS: 1970 { 1971 /* 1972 * Set the IP6 hoplimit for outgoing multicast packets. 1973 */ 1974 int optval; 1975 if (m == NULL || m->m_len != sizeof(int)) { 1976 error = EINVAL; 1977 break; 1978 } 1979 memcpy(&optval, mtod(m, u_int *), sizeof(optval)); 1980 if (optval < -1 || optval >= 256) 1981 error = EINVAL; 1982 else if (optval == -1) 1983 im6o->im6o_hlim = ip6_defmcasthlim; 1984 else 1985 im6o->im6o_hlim = optval; 1986 break; 1987 } 1988 1989 case IPV6_MULTICAST_LOOP: 1990 /* 1991 * Set the loopback flag for outgoing multicast packets. 1992 * Must be zero or one. 1993 */ 1994 if (m == NULL || m->m_len != sizeof(u_int)) { 1995 error = EINVAL; 1996 break; 1997 } 1998 memcpy(&loop, mtod(m, u_int *), sizeof(loop)); 1999 if (loop > 1) { 2000 error = EINVAL; 2001 break; 2002 } 2003 im6o->im6o_loop = loop; 2004 break; 2005 2006 case IPV6_JOIN_GROUP: 2007 /* 2008 * Add a multicast group membership. 2009 * Group must be a valid IP6 multicast address. 2010 */ 2011 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2012 error = EINVAL; 2013 break; 2014 } 2015 mreq = mtod(m, struct ipv6_mreq *); 2016 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2017 /* 2018 * We use the unspecified address to specify to accept 2019 * all multicast addresses. Only super user is allowed 2020 * to do this. 2021 */ 2022 if (suser(p, 0)) 2023 { 2024 error = EACCES; 2025 break; 2026 } 2027 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2028 error = EINVAL; 2029 break; 2030 } 2031 2032 /* 2033 * If no interface was explicitly specified, choose an 2034 * appropriate one according to the given multicast address. 2035 */ 2036 if (mreq->ipv6mr_interface == 0) { 2037 /* 2038 * Look up the routing table for the 2039 * address, and choose the outgoing interface. 2040 * XXX: is it a good approach? 2041 */ 2042 bzero(&ro, sizeof(ro)); 2043 ro.ro_tableid = m->m_pkthdr.ph_rtableid; 2044 dst = &ro.ro_dst; 2045 dst->sin6_len = sizeof(struct sockaddr_in6); 2046 dst->sin6_family = AF_INET6; 2047 dst->sin6_addr = mreq->ipv6mr_multiaddr; 2048 ro.ro_rt = rtalloc(sin6tosa(&ro.ro_dst), 2049 RT_RESOLVE, ro.ro_tableid); 2050 if (ro.ro_rt == NULL) { 2051 error = EADDRNOTAVAIL; 2052 break; 2053 } 2054 ifp = if_get(ro.ro_rt->rt_ifidx); 2055 rtfree(ro.ro_rt); 2056 } else { 2057 /* 2058 * If the interface is specified, validate it. 2059 */ 2060 ifp = if_get(mreq->ipv6mr_interface); 2061 if (ifp == NULL) { 2062 error = ENXIO; /* XXX EINVAL? */ 2063 break; 2064 } 2065 } 2066 2067 /* 2068 * See if we found an interface, and confirm that it 2069 * supports multicast 2070 */ 2071 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2072 if_put(ifp); 2073 error = EADDRNOTAVAIL; 2074 break; 2075 } 2076 /* 2077 * Put interface index into the multicast address, 2078 * if the address has link/interface-local scope. 2079 */ 2080 if (IN6_IS_SCOPE_EMBED(&mreq->ipv6mr_multiaddr)) { 2081 mreq->ipv6mr_multiaddr.s6_addr16[1] = 2082 htons(ifp->if_index); 2083 } 2084 /* 2085 * See if the membership already exists. 2086 */ 2087 LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) 2088 if (imm->i6mm_maddr->in6m_ifidx == ifp->if_index && 2089 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2090 &mreq->ipv6mr_multiaddr)) 2091 break; 2092 if (imm != NULL) { 2093 if_put(ifp); 2094 error = EADDRINUSE; 2095 break; 2096 } 2097 /* 2098 * Everything looks good; add a new record to the multicast 2099 * address list for the given interface. 2100 */ 2101 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error); 2102 if_put(ifp); 2103 if (!imm) 2104 break; 2105 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2106 break; 2107 2108 case IPV6_LEAVE_GROUP: 2109 /* 2110 * Drop a multicast group membership. 2111 * Group must be a valid IP6 multicast address. 2112 */ 2113 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2114 error = EINVAL; 2115 break; 2116 } 2117 mreq = mtod(m, struct ipv6_mreq *); 2118 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2119 if (suser(p, 0)) 2120 { 2121 error = EACCES; 2122 break; 2123 } 2124 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2125 error = EINVAL; 2126 break; 2127 } 2128 2129 /* 2130 * Put interface index into the multicast address, 2131 * if the address has link-local scope. 2132 */ 2133 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2134 mreq->ipv6mr_multiaddr.s6_addr16[1] = 2135 htons(mreq->ipv6mr_interface); 2136 } 2137 2138 /* 2139 * If an interface address was specified, get a pointer 2140 * to its ifnet structure. 2141 */ 2142 if (mreq->ipv6mr_interface == 0) 2143 ifp = NULL; 2144 else { 2145 ifp = if_get(mreq->ipv6mr_interface); 2146 if (ifp == NULL) { 2147 error = ENXIO; /* XXX EINVAL? */ 2148 break; 2149 } 2150 } 2151 2152 /* 2153 * Find the membership in the membership list. 2154 */ 2155 LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) { 2156 if ((ifp == NULL || 2157 imm->i6mm_maddr->in6m_ifidx == ifp->if_index) && 2158 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2159 &mreq->ipv6mr_multiaddr)) 2160 break; 2161 } 2162 2163 if_put(ifp); 2164 2165 if (imm == NULL) { 2166 /* Unable to resolve interface */ 2167 error = EADDRNOTAVAIL; 2168 break; 2169 } 2170 /* 2171 * Give up the multicast address record to which the 2172 * membership points. 2173 */ 2174 LIST_REMOVE(imm, i6mm_chain); 2175 in6_leavegroup(imm); 2176 break; 2177 2178 default: 2179 error = EOPNOTSUPP; 2180 break; 2181 } 2182 2183 /* 2184 * If all options have default values, no need to keep the option 2185 * structure. 2186 */ 2187 if (im6o->im6o_ifidx == 0 && 2188 im6o->im6o_hlim == ip6_defmcasthlim && 2189 im6o->im6o_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2190 LIST_EMPTY(&im6o->im6o_memberships)) { 2191 free(*im6op, M_IPMOPTS, 0); 2192 *im6op = NULL; 2193 } 2194 2195 return (error); 2196 } 2197 2198 /* 2199 * Return the IP6 multicast options in response to user getsockopt(). 2200 */ 2201 int 2202 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp) 2203 { 2204 u_int *hlim, *loop, *ifindex; 2205 2206 *mp = m_get(M_WAIT, MT_SOOPTS); 2207 2208 switch (optname) { 2209 2210 case IPV6_MULTICAST_IF: 2211 ifindex = mtod(*mp, u_int *); 2212 (*mp)->m_len = sizeof(u_int); 2213 if (im6o == NULL || im6o->im6o_ifidx == 0) 2214 *ifindex = 0; 2215 else 2216 *ifindex = im6o->im6o_ifidx; 2217 return (0); 2218 2219 case IPV6_MULTICAST_HOPS: 2220 hlim = mtod(*mp, u_int *); 2221 (*mp)->m_len = sizeof(u_int); 2222 if (im6o == NULL) 2223 *hlim = ip6_defmcasthlim; 2224 else 2225 *hlim = im6o->im6o_hlim; 2226 return (0); 2227 2228 case IPV6_MULTICAST_LOOP: 2229 loop = mtod(*mp, u_int *); 2230 (*mp)->m_len = sizeof(u_int); 2231 if (im6o == NULL) 2232 *loop = ip6_defmcasthlim; 2233 else 2234 *loop = im6o->im6o_loop; 2235 return (0); 2236 2237 default: 2238 return (EOPNOTSUPP); 2239 } 2240 } 2241 2242 /* 2243 * Discard the IP6 multicast options. 2244 */ 2245 void 2246 ip6_freemoptions(struct ip6_moptions *im6o) 2247 { 2248 struct in6_multi_mship *imm; 2249 2250 if (im6o == NULL) 2251 return; 2252 2253 while (!LIST_EMPTY(&im6o->im6o_memberships)) { 2254 imm = LIST_FIRST(&im6o->im6o_memberships); 2255 LIST_REMOVE(imm, i6mm_chain); 2256 in6_leavegroup(imm); 2257 } 2258 free(im6o, M_IPMOPTS, 0); 2259 } 2260 2261 /* 2262 * Set IPv6 outgoing packet options based on advanced API. 2263 */ 2264 int 2265 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2266 struct ip6_pktopts *stickyopt, int priv, int uproto) 2267 { 2268 u_int clen; 2269 struct cmsghdr *cm = 0; 2270 caddr_t cmsgs; 2271 int error; 2272 2273 if (control == NULL || opt == NULL) 2274 return (EINVAL); 2275 2276 ip6_initpktopts(opt); 2277 if (stickyopt) { 2278 int error; 2279 2280 /* 2281 * If stickyopt is provided, make a local copy of the options 2282 * for this particular packet, then override them by ancillary 2283 * objects. 2284 * XXX: copypktopts() does not copy the cached route to a next 2285 * hop (if any). This is not very good in terms of efficiency, 2286 * but we can allow this since this option should be rarely 2287 * used. 2288 */ 2289 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2290 return (error); 2291 } 2292 2293 /* 2294 * XXX: Currently, we assume all the optional information is stored 2295 * in a single mbuf. 2296 */ 2297 if (control->m_next) 2298 return (EINVAL); 2299 2300 clen = control->m_len; 2301 cmsgs = mtod(control, caddr_t); 2302 do { 2303 if (clen < CMSG_LEN(0)) 2304 return (EINVAL); 2305 cm = (struct cmsghdr *)cmsgs; 2306 if (cm->cmsg_len < CMSG_LEN(0) || cm->cmsg_len > clen || 2307 CMSG_ALIGN(cm->cmsg_len) > clen) 2308 return (EINVAL); 2309 if (cm->cmsg_level == IPPROTO_IPV6) { 2310 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2311 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, uproto); 2312 if (error) 2313 return (error); 2314 } 2315 2316 clen -= CMSG_ALIGN(cm->cmsg_len); 2317 cmsgs += CMSG_ALIGN(cm->cmsg_len); 2318 } while (clen); 2319 2320 return (0); 2321 } 2322 2323 /* 2324 * Set a particular packet option, as a sticky option or an ancillary data 2325 * item. "len" can be 0 only when it's a sticky option. 2326 */ 2327 int 2328 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2329 int priv, int sticky, int uproto) 2330 { 2331 int minmtupolicy; 2332 2333 switch (optname) { 2334 case IPV6_PKTINFO: 2335 { 2336 struct ifnet *ifp = NULL; 2337 struct in6_pktinfo *pktinfo; 2338 2339 if (len != sizeof(struct in6_pktinfo)) 2340 return (EINVAL); 2341 2342 pktinfo = (struct in6_pktinfo *)buf; 2343 2344 /* 2345 * An application can clear any sticky IPV6_PKTINFO option by 2346 * doing a "regular" setsockopt with ipi6_addr being 2347 * in6addr_any and ipi6_ifindex being zero. 2348 * [RFC 3542, Section 6] 2349 */ 2350 if (opt->ip6po_pktinfo && 2351 pktinfo->ipi6_ifindex == 0 && 2352 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2353 ip6_clearpktopts(opt, optname); 2354 break; 2355 } 2356 2357 if (uproto == IPPROTO_TCP && 2358 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2359 return (EINVAL); 2360 } 2361 2362 if (pktinfo->ipi6_ifindex) { 2363 ifp = if_get(pktinfo->ipi6_ifindex); 2364 if (ifp == NULL) 2365 return (ENXIO); 2366 if_put(ifp); 2367 } 2368 2369 /* 2370 * We store the address anyway, and let in6_selectsrc() 2371 * validate the specified address. This is because ipi6_addr 2372 * may not have enough information about its scope zone, and 2373 * we may need additional information (such as outgoing 2374 * interface or the scope zone of a destination address) to 2375 * disambiguate the scope. 2376 * XXX: the delay of the validation may confuse the 2377 * application when it is used as a sticky option. 2378 */ 2379 if (opt->ip6po_pktinfo == NULL) { 2380 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2381 M_IP6OPT, M_NOWAIT); 2382 if (opt->ip6po_pktinfo == NULL) 2383 return (ENOBUFS); 2384 } 2385 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 2386 break; 2387 } 2388 2389 case IPV6_HOPLIMIT: 2390 { 2391 int *hlimp; 2392 2393 /* 2394 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2395 * to simplify the ordering among hoplimit options. 2396 */ 2397 if (sticky) 2398 return (ENOPROTOOPT); 2399 2400 if (len != sizeof(int)) 2401 return (EINVAL); 2402 hlimp = (int *)buf; 2403 if (*hlimp < -1 || *hlimp > 255) 2404 return (EINVAL); 2405 2406 opt->ip6po_hlim = *hlimp; 2407 break; 2408 } 2409 2410 case IPV6_TCLASS: 2411 { 2412 int tclass; 2413 2414 if (len != sizeof(int)) 2415 return (EINVAL); 2416 tclass = *(int *)buf; 2417 if (tclass < -1 || tclass > 255) 2418 return (EINVAL); 2419 2420 opt->ip6po_tclass = tclass; 2421 break; 2422 } 2423 case IPV6_HOPOPTS: 2424 { 2425 struct ip6_hbh *hbh; 2426 int hbhlen; 2427 2428 /* 2429 * XXX: We don't allow a non-privileged user to set ANY HbH 2430 * options, since per-option restriction has too much 2431 * overhead. 2432 */ 2433 if (!priv) 2434 return (EPERM); 2435 2436 if (len == 0) { 2437 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2438 break; /* just remove the option */ 2439 } 2440 2441 /* message length validation */ 2442 if (len < sizeof(struct ip6_hbh)) 2443 return (EINVAL); 2444 hbh = (struct ip6_hbh *)buf; 2445 hbhlen = (hbh->ip6h_len + 1) << 3; 2446 if (len != hbhlen) 2447 return (EINVAL); 2448 2449 /* turn off the previous option, then set the new option. */ 2450 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2451 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 2452 if (opt->ip6po_hbh == NULL) 2453 return (ENOBUFS); 2454 memcpy(opt->ip6po_hbh, hbh, hbhlen); 2455 2456 break; 2457 } 2458 2459 case IPV6_DSTOPTS: 2460 case IPV6_RTHDRDSTOPTS: 2461 { 2462 struct ip6_dest *dest, **newdest = NULL; 2463 int destlen; 2464 2465 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */ 2466 return (EPERM); 2467 2468 if (len == 0) { 2469 ip6_clearpktopts(opt, optname); 2470 break; /* just remove the option */ 2471 } 2472 2473 /* message length validation */ 2474 if (len < sizeof(struct ip6_dest)) 2475 return (EINVAL); 2476 dest = (struct ip6_dest *)buf; 2477 destlen = (dest->ip6d_len + 1) << 3; 2478 if (len != destlen) 2479 return (EINVAL); 2480 /* 2481 * Determine the position that the destination options header 2482 * should be inserted; before or after the routing header. 2483 */ 2484 switch (optname) { 2485 case IPV6_RTHDRDSTOPTS: 2486 newdest = &opt->ip6po_dest1; 2487 break; 2488 case IPV6_DSTOPTS: 2489 newdest = &opt->ip6po_dest2; 2490 break; 2491 } 2492 2493 /* turn off the previous option, then set the new option. */ 2494 ip6_clearpktopts(opt, optname); 2495 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 2496 if (*newdest == NULL) 2497 return (ENOBUFS); 2498 memcpy(*newdest, dest, destlen); 2499 2500 break; 2501 } 2502 2503 case IPV6_RTHDR: 2504 { 2505 struct ip6_rthdr *rth; 2506 int rthlen; 2507 2508 if (len == 0) { 2509 ip6_clearpktopts(opt, IPV6_RTHDR); 2510 break; /* just remove the option */ 2511 } 2512 2513 /* message length validation */ 2514 if (len < sizeof(struct ip6_rthdr)) 2515 return (EINVAL); 2516 rth = (struct ip6_rthdr *)buf; 2517 rthlen = (rth->ip6r_len + 1) << 3; 2518 if (len != rthlen) 2519 return (EINVAL); 2520 2521 switch (rth->ip6r_type) { 2522 case IPV6_RTHDR_TYPE_0: 2523 if (rth->ip6r_len == 0) /* must contain one addr */ 2524 return (EINVAL); 2525 if (rth->ip6r_len % 2) /* length must be even */ 2526 return (EINVAL); 2527 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 2528 return (EINVAL); 2529 break; 2530 default: 2531 return (EINVAL); /* not supported */ 2532 } 2533 /* turn off the previous option */ 2534 ip6_clearpktopts(opt, IPV6_RTHDR); 2535 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 2536 if (opt->ip6po_rthdr == NULL) 2537 return (ENOBUFS); 2538 memcpy(opt->ip6po_rthdr, rth, rthlen); 2539 break; 2540 } 2541 2542 case IPV6_USE_MIN_MTU: 2543 if (len != sizeof(int)) 2544 return (EINVAL); 2545 minmtupolicy = *(int *)buf; 2546 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 2547 minmtupolicy != IP6PO_MINMTU_DISABLE && 2548 minmtupolicy != IP6PO_MINMTU_ALL) { 2549 return (EINVAL); 2550 } 2551 opt->ip6po_minmtu = minmtupolicy; 2552 break; 2553 2554 case IPV6_DONTFRAG: 2555 if (len != sizeof(int)) 2556 return (EINVAL); 2557 2558 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 2559 /* 2560 * we ignore this option for TCP sockets. 2561 * (RFC3542 leaves this case unspecified.) 2562 */ 2563 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 2564 } else 2565 opt->ip6po_flags |= IP6PO_DONTFRAG; 2566 break; 2567 2568 default: 2569 return (ENOPROTOOPT); 2570 } /* end of switch */ 2571 2572 return (0); 2573 } 2574 2575 /* 2576 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 2577 * packet to the input queue of a specified interface. 2578 */ 2579 void 2580 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst) 2581 { 2582 struct mbuf *copym; 2583 struct ip6_hdr *ip6; 2584 2585 /* 2586 * Duplicate the packet. 2587 */ 2588 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT); 2589 if (copym == NULL) 2590 return; 2591 2592 /* 2593 * Make sure to deep-copy IPv6 header portion in case the data 2594 * is in an mbuf cluster, so that we can safely override the IPv6 2595 * header portion later. 2596 */ 2597 if ((copym->m_flags & M_EXT) != 0 || 2598 copym->m_len < sizeof(struct ip6_hdr)) { 2599 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 2600 if (copym == NULL) 2601 return; 2602 } 2603 2604 #ifdef DIAGNOSTIC 2605 if (copym->m_len < sizeof(*ip6)) { 2606 m_freem(copym); 2607 return; 2608 } 2609 #endif 2610 2611 ip6 = mtod(copym, struct ip6_hdr *); 2612 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) 2613 ip6->ip6_src.s6_addr16[1] = 0; 2614 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) 2615 ip6->ip6_dst.s6_addr16[1] = 0; 2616 2617 if_input_local(ifp, copym, dst->sin6_family); 2618 } 2619 2620 /* 2621 * Chop IPv6 header off from the payload. 2622 */ 2623 int 2624 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 2625 { 2626 struct mbuf *mh; 2627 struct ip6_hdr *ip6; 2628 2629 ip6 = mtod(m, struct ip6_hdr *); 2630 if (m->m_len > sizeof(*ip6)) { 2631 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 2632 if (mh == NULL) { 2633 m_freem(m); 2634 return ENOBUFS; 2635 } 2636 M_MOVE_PKTHDR(mh, m); 2637 MH_ALIGN(mh, sizeof(*ip6)); 2638 m->m_len -= sizeof(*ip6); 2639 m->m_data += sizeof(*ip6); 2640 mh->m_next = m; 2641 m = mh; 2642 m->m_len = sizeof(*ip6); 2643 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 2644 } 2645 exthdrs->ip6e_ip6 = m; 2646 return 0; 2647 } 2648 2649 u_int32_t 2650 ip6_randomid(void) 2651 { 2652 return idgen32(&ip6_id_ctx); 2653 } 2654 2655 void 2656 ip6_randomid_init(void) 2657 { 2658 idgen32_init(&ip6_id_ctx); 2659 } 2660 2661 /* 2662 * Compute significant parts of the IPv6 checksum pseudo-header 2663 * for use in a delayed TCP/UDP checksum calculation. 2664 */ 2665 static __inline u_int16_t __attribute__((__unused__)) 2666 in6_cksum_phdr(const struct in6_addr *src, const struct in6_addr *dst, 2667 u_int32_t len, u_int32_t nxt) 2668 { 2669 u_int32_t sum = 0; 2670 const u_int16_t *w; 2671 2672 w = (const u_int16_t *) src; 2673 sum += w[0]; 2674 if (!IN6_IS_SCOPE_EMBED(src)) 2675 sum += w[1]; 2676 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5]; 2677 sum += w[6]; sum += w[7]; 2678 2679 w = (const u_int16_t *) dst; 2680 sum += w[0]; 2681 if (!IN6_IS_SCOPE_EMBED(dst)) 2682 sum += w[1]; 2683 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5]; 2684 sum += w[6]; sum += w[7]; 2685 2686 sum += (u_int16_t)(len >> 16) + (u_int16_t)(len /*& 0xffff*/); 2687 2688 sum += (u_int16_t)(nxt >> 16) + (u_int16_t)(nxt /*& 0xffff*/); 2689 2690 sum = (u_int16_t)(sum >> 16) + (u_int16_t)(sum /*& 0xffff*/); 2691 2692 if (sum > 0xffff) 2693 sum -= 0xffff; 2694 2695 return (sum); 2696 } 2697 2698 /* 2699 * Process a delayed payload checksum calculation. 2700 */ 2701 void 2702 in6_delayed_cksum(struct mbuf *m, u_int8_t nxt) 2703 { 2704 int nxtp, offset; 2705 u_int16_t csum; 2706 2707 offset = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxtp); 2708 if (offset <= 0 || nxtp != nxt) 2709 /* If the desired next protocol isn't found, punt. */ 2710 return; 2711 csum = (u_int16_t)(in6_cksum(m, 0, offset, m->m_pkthdr.len - offset)); 2712 2713 switch (nxt) { 2714 case IPPROTO_TCP: 2715 offset += offsetof(struct tcphdr, th_sum); 2716 break; 2717 2718 case IPPROTO_UDP: 2719 offset += offsetof(struct udphdr, uh_sum); 2720 if (csum == 0) 2721 csum = 0xffff; 2722 break; 2723 2724 case IPPROTO_ICMPV6: 2725 offset += offsetof(struct icmp6_hdr, icmp6_cksum); 2726 break; 2727 } 2728 2729 if ((offset + sizeof(u_int16_t)) > m->m_len) 2730 m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT); 2731 else 2732 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum; 2733 } 2734 2735 void 2736 in6_proto_cksum_out(struct mbuf *m, struct ifnet *ifp) 2737 { 2738 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 2739 2740 /* some hw and in6_delayed_cksum need the pseudo header cksum */ 2741 if (m->m_pkthdr.csum_flags & 2742 (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT|M_ICMP_CSUM_OUT)) { 2743 int nxt, offset; 2744 u_int16_t csum; 2745 2746 offset = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 2747 csum = in6_cksum_phdr(&ip6->ip6_src, &ip6->ip6_dst, 2748 htonl(m->m_pkthdr.len - offset), htonl(nxt)); 2749 if (nxt == IPPROTO_TCP) 2750 offset += offsetof(struct tcphdr, th_sum); 2751 else if (nxt == IPPROTO_UDP) 2752 offset += offsetof(struct udphdr, uh_sum); 2753 else if (nxt == IPPROTO_ICMPV6) 2754 offset += offsetof(struct icmp6_hdr, icmp6_cksum); 2755 if ((offset + sizeof(u_int16_t)) > m->m_len) 2756 m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT); 2757 else 2758 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum; 2759 } 2760 2761 if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) { 2762 if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_TCPv6) || 2763 ip6->ip6_nxt != IPPROTO_TCP || 2764 ifp->if_bridgeport != NULL) { 2765 tcpstat.tcps_outswcsum++; 2766 in6_delayed_cksum(m, IPPROTO_TCP); 2767 m->m_pkthdr.csum_flags &= ~M_TCP_CSUM_OUT; /* Clear */ 2768 } 2769 } else if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) { 2770 if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_UDPv6) || 2771 ip6->ip6_nxt != IPPROTO_UDP || 2772 ifp->if_bridgeport != NULL) { 2773 udpstat.udps_outswcsum++; 2774 in6_delayed_cksum(m, IPPROTO_UDP); 2775 m->m_pkthdr.csum_flags &= ~M_UDP_CSUM_OUT; /* Clear */ 2776 } 2777 } else if (m->m_pkthdr.csum_flags & M_ICMP_CSUM_OUT) { 2778 in6_delayed_cksum(m, IPPROTO_ICMPV6); 2779 m->m_pkthdr.csum_flags &= ~M_ICMP_CSUM_OUT; /* Clear */ 2780 } 2781 } 2782 2783 #ifdef IPSEC 2784 struct tdb * 2785 ip6_output_ipsec_lookup(struct mbuf *m, int *error, struct inpcb *inp) 2786 { 2787 struct tdb *tdb; 2788 struct m_tag *mtag; 2789 struct tdb_ident *tdbi; 2790 2791 /* 2792 * Check if there was an outgoing SA bound to the flow 2793 * from a transport protocol. 2794 */ 2795 2796 /* Do we have any pending SAs to apply ? */ 2797 tdb = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr), 2798 error, IPSP_DIRECTION_OUT, NULL, inp, 0); 2799 2800 if (tdb == NULL) 2801 return NULL; 2802 /* Loop detection */ 2803 for (mtag = m_tag_first(m); mtag != NULL; mtag = m_tag_next(m, mtag)) { 2804 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE) 2805 continue; 2806 tdbi = (struct tdb_ident *)(mtag + 1); 2807 if (tdbi->spi == tdb->tdb_spi && 2808 tdbi->proto == tdb->tdb_sproto && 2809 tdbi->rdomain == tdb->tdb_rdomain && 2810 !memcmp(&tdbi->dst, &tdb->tdb_dst, 2811 sizeof(union sockaddr_union))) { 2812 /* no IPsec needed */ 2813 return NULL; 2814 } 2815 } 2816 return tdb; 2817 } 2818 2819 int 2820 ip6_output_ipsec_send(struct tdb *tdb, struct mbuf *m, int tunalready, int fwd) 2821 { 2822 #if NPF > 0 2823 struct ifnet *encif; 2824 #endif 2825 2826 #if NPF > 0 2827 if ((encif = enc_getif(tdb->tdb_rdomain, tdb->tdb_tap)) == NULL || 2828 pf_test(AF_INET6, fwd ? PF_FWD : PF_OUT, encif, &m) != PF_PASS) { 2829 m_freem(m); 2830 return EHOSTUNREACH; 2831 } 2832 if (m == NULL) 2833 return 0; 2834 /* 2835 * PF_TAG_REROUTE handling or not... 2836 * Packet is entering IPsec so the routing is 2837 * already overruled by the IPsec policy. 2838 * Until now the change was not reconsidered. 2839 * What's the behaviour? 2840 */ 2841 in6_proto_cksum_out(m, encif); 2842 #endif 2843 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 2844 2845 /* Callee frees mbuf */ 2846 return ipsp_process_packet(m, tdb, AF_INET6, tunalready); 2847 } 2848 #endif /* IPSEC */ 2849