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