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