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 int needfiblookup; 259 uint32_t fibnum; 260 struct m_tag *fwd_tag = NULL; 261 262 ip6 = mtod(m, struct ip6_hdr *); 263 if (ip6 == NULL) { 264 printf ("ip6 is NULL"); 265 goto bad; 266 } 267 268 if (inp != NULL) { 269 M_SETFIB(m, inp->inp_inc.inc_fibnum); 270 if ((flags & IP_NODEFAULTFLOWID) == 0) { 271 /* unconditionally set flowid */ 272 m->m_pkthdr.flowid = inp->inp_flowid; 273 M_HASHTYPE_SET(m, inp->inp_flowtype); 274 } 275 } 276 277 finaldst = ip6->ip6_dst; 278 bzero(&exthdrs, sizeof(exthdrs)); 279 if (opt) { 280 /* Hop-by-Hop options header */ 281 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 282 /* Destination options header(1st part) */ 283 if (opt->ip6po_rthdr) { 284 /* 285 * Destination options header(1st part) 286 * This only makes sense with a routing header. 287 * See Section 9.2 of RFC 3542. 288 * Disabling this part just for MIP6 convenience is 289 * a bad idea. We need to think carefully about a 290 * way to make the advanced API coexist with MIP6 291 * options, which might automatically be inserted in 292 * the kernel. 293 */ 294 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 295 } 296 /* Routing header */ 297 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 298 /* Destination options header(2nd part) */ 299 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 300 } 301 302 #ifdef IPSEC 303 /* 304 * IPSec checking which handles several cases. 305 * FAST IPSEC: We re-injected the packet. 306 * XXX: need scope argument. 307 */ 308 switch(ip6_ipsec_output(&m, inp, &error)) 309 { 310 case 1: /* Bad packet */ 311 goto freehdrs; 312 case -1: /* IPSec done */ 313 goto done; 314 case 0: /* No IPSec */ 315 default: 316 break; 317 } 318 #endif /* IPSEC */ 319 320 /* 321 * Calculate the total length of the extension header chain. 322 * Keep the length of the unfragmentable part for fragmentation. 323 */ 324 optlen = 0; 325 if (exthdrs.ip6e_hbh) 326 optlen += exthdrs.ip6e_hbh->m_len; 327 if (exthdrs.ip6e_dest1) 328 optlen += exthdrs.ip6e_dest1->m_len; 329 if (exthdrs.ip6e_rthdr) 330 optlen += exthdrs.ip6e_rthdr->m_len; 331 unfragpartlen = optlen + sizeof(struct ip6_hdr); 332 333 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */ 334 if (exthdrs.ip6e_dest2) 335 optlen += exthdrs.ip6e_dest2->m_len; 336 337 /* 338 * If there is at least one extension header, 339 * separate IP6 header from the payload. 340 */ 341 if (optlen && !hdrsplit) { 342 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 343 m = NULL; 344 goto freehdrs; 345 } 346 m = exthdrs.ip6e_ip6; 347 hdrsplit++; 348 } 349 350 /* adjust pointer */ 351 ip6 = mtod(m, struct ip6_hdr *); 352 353 /* adjust mbuf packet header length */ 354 m->m_pkthdr.len += optlen; 355 plen = m->m_pkthdr.len - sizeof(*ip6); 356 357 /* If this is a jumbo payload, insert a jumbo payload option. */ 358 if (plen > IPV6_MAXPACKET) { 359 if (!hdrsplit) { 360 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 361 m = NULL; 362 goto freehdrs; 363 } 364 m = exthdrs.ip6e_ip6; 365 hdrsplit++; 366 } 367 /* adjust pointer */ 368 ip6 = mtod(m, struct ip6_hdr *); 369 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 370 goto freehdrs; 371 ip6->ip6_plen = 0; 372 } else 373 ip6->ip6_plen = htons(plen); 374 375 /* 376 * Concatenate headers and fill in next header fields. 377 * Here we have, on "m" 378 * IPv6 payload 379 * and we insert headers accordingly. Finally, we should be getting: 380 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 381 * 382 * during the header composing process, "m" points to IPv6 header. 383 * "mprev" points to an extension header prior to esp. 384 */ 385 u_char *nexthdrp = &ip6->ip6_nxt; 386 mprev = m; 387 388 /* 389 * we treat dest2 specially. this makes IPsec processing 390 * much easier. the goal here is to make mprev point the 391 * mbuf prior to dest2. 392 * 393 * result: IPv6 dest2 payload 394 * m and mprev will point to IPv6 header. 395 */ 396 if (exthdrs.ip6e_dest2) { 397 if (!hdrsplit) 398 panic("assumption failed: hdr not split"); 399 exthdrs.ip6e_dest2->m_next = m->m_next; 400 m->m_next = exthdrs.ip6e_dest2; 401 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 402 ip6->ip6_nxt = IPPROTO_DSTOPTS; 403 } 404 405 /* 406 * result: IPv6 hbh dest1 rthdr dest2 payload 407 * m will point to IPv6 header. mprev will point to the 408 * extension header prior to dest2 (rthdr in the above case). 409 */ 410 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 411 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 412 IPPROTO_DSTOPTS); 413 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 414 IPPROTO_ROUTING); 415 416 /* 417 * If there is a routing header, discard the packet. 418 */ 419 if (exthdrs.ip6e_rthdr) { 420 error = EINVAL; 421 goto bad; 422 } 423 424 /* Source address validation */ 425 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 426 (flags & IPV6_UNSPECSRC) == 0) { 427 error = EOPNOTSUPP; 428 IP6STAT_INC(ip6s_badscope); 429 goto bad; 430 } 431 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 432 error = EOPNOTSUPP; 433 IP6STAT_INC(ip6s_badscope); 434 goto bad; 435 } 436 437 IP6STAT_INC(ip6s_localout); 438 439 /* 440 * Route packet. 441 */ 442 if (ro == 0) { 443 ro = &ip6route; 444 bzero((caddr_t)ro, sizeof(*ro)); 445 } 446 ro_pmtu = ro; 447 if (opt && opt->ip6po_rthdr) 448 ro = &opt->ip6po_route; 449 dst = (struct sockaddr_in6 *)&ro->ro_dst; 450 #ifdef FLOWTABLE 451 if (ro->ro_rt == NULL) 452 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro); 453 #endif 454 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m); 455 again: 456 /* 457 * if specified, try to fill in the traffic class field. 458 * do not override if a non-zero value is already set. 459 * we check the diffserv field and the ecn field separately. 460 */ 461 if (opt && opt->ip6po_tclass >= 0) { 462 int mask = 0; 463 464 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0) 465 mask |= 0xfc; 466 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0) 467 mask |= 0x03; 468 if (mask != 0) 469 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20); 470 } 471 472 /* fill in or override the hop limit field, if necessary. */ 473 if (opt && opt->ip6po_hlim != -1) 474 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 475 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 476 if (im6o != NULL) 477 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 478 else 479 ip6->ip6_hlim = V_ip6_defmcasthlim; 480 } 481 482 /* adjust pointer */ 483 ip6 = mtod(m, struct ip6_hdr *); 484 485 if (ro->ro_rt && fwd_tag == NULL) { 486 rt = ro->ro_rt; 487 ifp = ro->ro_rt->rt_ifp; 488 } else { 489 if (fwd_tag == NULL) { 490 bzero(&dst_sa, sizeof(dst_sa)); 491 dst_sa.sin6_family = AF_INET6; 492 dst_sa.sin6_len = sizeof(dst_sa); 493 dst_sa.sin6_addr = ip6->ip6_dst; 494 } 495 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp, 496 &rt, fibnum); 497 if (error != 0) { 498 if (ifp != NULL) 499 in6_ifstat_inc(ifp, ifs6_out_discard); 500 goto bad; 501 } 502 } 503 if (rt == NULL) { 504 /* 505 * If in6_selectroute() does not return a route entry, 506 * dst may not have been updated. 507 */ 508 *dst = dst_sa; /* XXX */ 509 } 510 511 /* 512 * then rt (for unicast) and ifp must be non-NULL valid values. 513 */ 514 if ((flags & IPV6_FORWARDING) == 0) { 515 /* XXX: the FORWARDING flag can be set for mrouting. */ 516 in6_ifstat_inc(ifp, ifs6_out_request); 517 } 518 if (rt != NULL) { 519 ia = (struct in6_ifaddr *)(rt->rt_ifa); 520 counter_u64_add(rt->rt_pksent, 1); 521 } 522 523 524 /* 525 * The outgoing interface must be in the zone of source and 526 * destination addresses. 527 */ 528 origifp = ifp; 529 530 src0 = ip6->ip6_src; 531 if (in6_setscope(&src0, origifp, &zone)) 532 goto badscope; 533 bzero(&src_sa, sizeof(src_sa)); 534 src_sa.sin6_family = AF_INET6; 535 src_sa.sin6_len = sizeof(src_sa); 536 src_sa.sin6_addr = ip6->ip6_src; 537 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id) 538 goto badscope; 539 540 dst0 = ip6->ip6_dst; 541 if (in6_setscope(&dst0, origifp, &zone)) 542 goto badscope; 543 /* re-initialize to be sure */ 544 bzero(&dst_sa, sizeof(dst_sa)); 545 dst_sa.sin6_family = AF_INET6; 546 dst_sa.sin6_len = sizeof(dst_sa); 547 dst_sa.sin6_addr = ip6->ip6_dst; 548 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) { 549 goto badscope; 550 } 551 552 /* We should use ia_ifp to support the case of 553 * sending packets to an address of our own. 554 */ 555 if (ia != NULL && ia->ia_ifp) 556 ifp = ia->ia_ifp; 557 558 /* scope check is done. */ 559 goto routefound; 560 561 badscope: 562 IP6STAT_INC(ip6s_badscope); 563 in6_ifstat_inc(origifp, ifs6_out_discard); 564 if (error == 0) 565 error = EHOSTUNREACH; /* XXX */ 566 goto bad; 567 568 routefound: 569 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 570 if (opt && opt->ip6po_nextroute.ro_rt) { 571 /* 572 * The nexthop is explicitly specified by the 573 * application. We assume the next hop is an IPv6 574 * address. 575 */ 576 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop; 577 } 578 else if ((rt->rt_flags & RTF_GATEWAY)) 579 dst = (struct sockaddr_in6 *)rt->rt_gateway; 580 } 581 582 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 583 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 584 } else { 585 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 586 in6_ifstat_inc(ifp, ifs6_out_mcast); 587 /* 588 * Confirm that the outgoing interface supports multicast. 589 */ 590 if (!(ifp->if_flags & IFF_MULTICAST)) { 591 IP6STAT_INC(ip6s_noroute); 592 in6_ifstat_inc(ifp, ifs6_out_discard); 593 error = ENETUNREACH; 594 goto bad; 595 } 596 if ((im6o == NULL && in6_mcast_loop) || 597 (im6o && im6o->im6o_multicast_loop)) { 598 /* 599 * Loop back multicast datagram if not expressly 600 * forbidden to do so, even if we have not joined 601 * the address; protocols will filter it later, 602 * thus deferring a hash lookup and lock acquisition 603 * at the expense of an m_copym(). 604 */ 605 ip6_mloopback(ifp, m, dst); 606 } else { 607 /* 608 * If we are acting as a multicast router, perform 609 * multicast forwarding as if the packet had just 610 * arrived on the interface to which we are about 611 * to send. The multicast forwarding function 612 * recursively calls this function, using the 613 * IPV6_FORWARDING flag to prevent infinite recursion. 614 * 615 * Multicasts that are looped back by ip6_mloopback(), 616 * above, will be forwarded by the ip6_input() routine, 617 * if necessary. 618 */ 619 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 620 /* 621 * XXX: ip6_mforward expects that rcvif is NULL 622 * when it is called from the originating path. 623 * However, it may not always be the case. 624 */ 625 m->m_pkthdr.rcvif = NULL; 626 if (ip6_mforward(ip6, ifp, m) != 0) { 627 m_freem(m); 628 goto done; 629 } 630 } 631 } 632 /* 633 * Multicasts with a hoplimit of zero may be looped back, 634 * above, but must not be transmitted on a network. 635 * Also, multicasts addressed to the loopback interface 636 * are not sent -- the above call to ip6_mloopback() will 637 * loop back a copy if this host actually belongs to the 638 * destination group on the loopback interface. 639 */ 640 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) || 641 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) { 642 m_freem(m); 643 goto done; 644 } 645 } 646 647 /* 648 * Fill the outgoing inteface to tell the upper layer 649 * to increment per-interface statistics. 650 */ 651 if (ifpp) 652 *ifpp = ifp; 653 654 /* Determine path MTU. */ 655 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu, 656 &alwaysfrag, fibnum)) != 0) 657 goto bad; 658 659 /* 660 * The caller of this function may specify to use the minimum MTU 661 * in some cases. 662 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU 663 * setting. The logic is a bit complicated; by default, unicast 664 * packets will follow path MTU while multicast packets will be sent at 665 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets 666 * including unicast ones will be sent at the minimum MTU. Multicast 667 * packets will always be sent at the minimum MTU unless 668 * IP6PO_MINMTU_DISABLE is explicitly specified. 669 * See RFC 3542 for more details. 670 */ 671 if (mtu > IPV6_MMTU) { 672 if ((flags & IPV6_MINMTU)) 673 mtu = IPV6_MMTU; 674 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) 675 mtu = IPV6_MMTU; 676 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 677 (opt == NULL || 678 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) { 679 mtu = IPV6_MMTU; 680 } 681 } 682 683 /* 684 * clear embedded scope identifiers if necessary. 685 * in6_clearscope will touch the addresses only when necessary. 686 */ 687 in6_clearscope(&ip6->ip6_src); 688 in6_clearscope(&ip6->ip6_dst); 689 690 /* 691 * If the outgoing packet contains a hop-by-hop options header, 692 * it must be examined and processed even by the source node. 693 * (RFC 2460, section 4.) 694 */ 695 if (exthdrs.ip6e_hbh) { 696 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 697 u_int32_t dummy; /* XXX unused */ 698 u_int32_t plen = 0; /* XXX: ip6_process will check the value */ 699 700 #ifdef DIAGNOSTIC 701 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) 702 panic("ip6e_hbh is not contiguous"); 703 #endif 704 /* 705 * XXX: if we have to send an ICMPv6 error to the sender, 706 * we need the M_LOOP flag since icmp6_error() expects 707 * the IPv6 and the hop-by-hop options header are 708 * contiguous unless the flag is set. 709 */ 710 m->m_flags |= M_LOOP; 711 m->m_pkthdr.rcvif = ifp; 712 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1), 713 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh), 714 &dummy, &plen) < 0) { 715 /* m was already freed at this point */ 716 error = EINVAL;/* better error? */ 717 goto done; 718 } 719 m->m_flags &= ~M_LOOP; /* XXX */ 720 m->m_pkthdr.rcvif = NULL; 721 } 722 723 /* Jump over all PFIL processing if hooks are not active. */ 724 if (!PFIL_HOOKED(&V_inet6_pfil_hook)) 725 goto passout; 726 727 odst = ip6->ip6_dst; 728 /* Run through list of hooks for output packets. */ 729 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp); 730 if (error != 0 || m == NULL) 731 goto done; 732 ip6 = mtod(m, struct ip6_hdr *); 733 734 needfiblookup = 0; 735 /* See if destination IP address was changed by packet filter. */ 736 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) { 737 m->m_flags |= M_SKIP_FIREWALL; 738 /* If destination is now ourself drop to ip6_input(). */ 739 if (in6_localip(&ip6->ip6_dst)) { 740 m->m_flags |= M_FASTFWD_OURS; 741 if (m->m_pkthdr.rcvif == NULL) 742 m->m_pkthdr.rcvif = V_loif; 743 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 744 m->m_pkthdr.csum_flags |= 745 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 746 m->m_pkthdr.csum_data = 0xffff; 747 } 748 #ifdef SCTP 749 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 750 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 751 #endif 752 error = netisr_queue(NETISR_IPV6, m); 753 goto done; 754 } else 755 needfiblookup = 1; /* Redo the routing table lookup. */ 756 } 757 /* See if fib was changed by packet filter. */ 758 if (fibnum != M_GETFIB(m)) { 759 m->m_flags |= M_SKIP_FIREWALL; 760 fibnum = M_GETFIB(m); 761 RO_RTFREE(ro); 762 needfiblookup = 1; 763 } 764 if (needfiblookup) 765 goto again; 766 767 /* See if local, if yes, send it to netisr. */ 768 if (m->m_flags & M_FASTFWD_OURS) { 769 if (m->m_pkthdr.rcvif == NULL) 770 m->m_pkthdr.rcvif = V_loif; 771 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 772 m->m_pkthdr.csum_flags |= 773 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 774 m->m_pkthdr.csum_data = 0xffff; 775 } 776 #ifdef SCTP 777 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 778 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 779 #endif 780 error = netisr_queue(NETISR_IPV6, m); 781 goto done; 782 } 783 /* Or forward to some other address? */ 784 if ((m->m_flags & M_IP6_NEXTHOP) && 785 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 786 dst = (struct sockaddr_in6 *)&ro->ro_dst; 787 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6)); 788 m->m_flags |= M_SKIP_FIREWALL; 789 m->m_flags &= ~M_IP6_NEXTHOP; 790 m_tag_delete(m, fwd_tag); 791 goto again; 792 } 793 794 passout: 795 /* 796 * Send the packet to the outgoing interface. 797 * If necessary, do IPv6 fragmentation before sending. 798 * 799 * the logic here is rather complex: 800 * 1: normal case (dontfrag == 0, alwaysfrag == 0) 801 * 1-a: send as is if tlen <= path mtu 802 * 1-b: fragment if tlen > path mtu 803 * 804 * 2: if user asks us not to fragment (dontfrag == 1) 805 * 2-a: send as is if tlen <= interface mtu 806 * 2-b: error if tlen > interface mtu 807 * 808 * 3: if we always need to attach fragment header (alwaysfrag == 1) 809 * always fragment 810 * 811 * 4: if dontfrag == 1 && alwaysfrag == 1 812 * error, as we cannot handle this conflicting request 813 */ 814 sw_csum = m->m_pkthdr.csum_flags; 815 if (!hdrsplit) { 816 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0; 817 sw_csum &= ~ifp->if_hwassist; 818 } else 819 tso = 0; 820 /* 821 * If we added extension headers, we will not do TSO and calculate the 822 * checksums ourselves for now. 823 * XXX-BZ Need a framework to know when the NIC can handle it, even 824 * with ext. hdrs. 825 */ 826 if (sw_csum & CSUM_DELAY_DATA_IPV6) { 827 sw_csum &= ~CSUM_DELAY_DATA_IPV6; 828 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr)); 829 } 830 #ifdef SCTP 831 if (sw_csum & CSUM_SCTP_IPV6) { 832 sw_csum &= ~CSUM_SCTP_IPV6; 833 sctp_delayed_cksum(m, sizeof(struct ip6_hdr)); 834 } 835 #endif 836 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 837 tlen = m->m_pkthdr.len; 838 839 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso) 840 dontfrag = 1; 841 else 842 dontfrag = 0; 843 if (dontfrag && alwaysfrag) { /* case 4 */ 844 /* conflicting request - can't transmit */ 845 error = EMSGSIZE; 846 goto bad; 847 } 848 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */ 849 /* 850 * Even if the DONTFRAG option is specified, we cannot send the 851 * packet when the data length is larger than the MTU of the 852 * outgoing interface. 853 * Notify the error by sending IPV6_PATHMTU ancillary data as 854 * well as returning an error code (the latter is not described 855 * in the API spec.) 856 */ 857 u_int32_t mtu32; 858 struct ip6ctlparam ip6cp; 859 860 mtu32 = (u_int32_t)mtu; 861 bzero(&ip6cp, sizeof(ip6cp)); 862 ip6cp.ip6c_cmdarg = (void *)&mtu32; 863 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst, 864 (void *)&ip6cp); 865 866 error = EMSGSIZE; 867 goto bad; 868 } 869 870 /* 871 * transmit packet without fragmentation 872 */ 873 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */ 874 struct in6_ifaddr *ia6; 875 876 ip6 = mtod(m, struct ip6_hdr *); 877 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); 878 if (ia6) { 879 /* Record statistics for this interface address. */ 880 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1); 881 counter_u64_add(ia6->ia_ifa.ifa_obytes, 882 m->m_pkthdr.len); 883 ifa_free(&ia6->ia_ifa); 884 } 885 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 886 goto done; 887 } 888 889 /* 890 * try to fragment the packet. case 1-b and 3 891 */ 892 if (mtu < IPV6_MMTU) { 893 /* path MTU cannot be less than IPV6_MMTU */ 894 error = EMSGSIZE; 895 in6_ifstat_inc(ifp, ifs6_out_fragfail); 896 goto bad; 897 } else if (ip6->ip6_plen == 0) { 898 /* jumbo payload cannot be fragmented */ 899 error = EMSGSIZE; 900 in6_ifstat_inc(ifp, ifs6_out_fragfail); 901 goto bad; 902 } else { 903 struct mbuf **mnext, *m_frgpart; 904 struct ip6_frag *ip6f; 905 u_int32_t id = htonl(ip6_randomid()); 906 u_char nextproto; 907 908 /* 909 * Too large for the destination or interface; 910 * fragment if possible. 911 * Must be able to put at least 8 bytes per fragment. 912 */ 913 hlen = unfragpartlen; 914 if (mtu > IPV6_MAXPACKET) 915 mtu = IPV6_MAXPACKET; 916 917 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 918 if (len < 8) { 919 error = EMSGSIZE; 920 in6_ifstat_inc(ifp, ifs6_out_fragfail); 921 goto bad; 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 (M_WRITABLE(mlast) && 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 } 1266 } else if (ifp) { 1267 mtu = IN6_LINKMTU(ifp); 1268 } else 1269 error = EHOSTUNREACH; /* XXX */ 1270 1271 *mtup = mtu; 1272 if (alwaysfragp) 1273 *alwaysfragp = alwaysfrag; 1274 return (error); 1275 } 1276 1277 /* 1278 * IP6 socket option processing. 1279 */ 1280 int 1281 ip6_ctloutput(struct socket *so, struct sockopt *sopt) 1282 { 1283 int optdatalen, uproto; 1284 void *optdata; 1285 struct inpcb *in6p = sotoinpcb(so); 1286 int error, optval; 1287 int level, op, optname; 1288 int optlen; 1289 struct thread *td; 1290 #ifdef RSS 1291 uint32_t rss_bucket; 1292 int retval; 1293 #endif 1294 1295 level = sopt->sopt_level; 1296 op = sopt->sopt_dir; 1297 optname = sopt->sopt_name; 1298 optlen = sopt->sopt_valsize; 1299 td = sopt->sopt_td; 1300 error = 0; 1301 optval = 0; 1302 uproto = (int)so->so_proto->pr_protocol; 1303 1304 if (level != IPPROTO_IPV6) { 1305 error = EINVAL; 1306 1307 if (sopt->sopt_level == SOL_SOCKET && 1308 sopt->sopt_dir == SOPT_SET) { 1309 switch (sopt->sopt_name) { 1310 case SO_REUSEADDR: 1311 INP_WLOCK(in6p); 1312 if ((so->so_options & SO_REUSEADDR) != 0) 1313 in6p->inp_flags2 |= INP_REUSEADDR; 1314 else 1315 in6p->inp_flags2 &= ~INP_REUSEADDR; 1316 INP_WUNLOCK(in6p); 1317 error = 0; 1318 break; 1319 case SO_REUSEPORT: 1320 INP_WLOCK(in6p); 1321 if ((so->so_options & SO_REUSEPORT) != 0) 1322 in6p->inp_flags2 |= INP_REUSEPORT; 1323 else 1324 in6p->inp_flags2 &= ~INP_REUSEPORT; 1325 INP_WUNLOCK(in6p); 1326 error = 0; 1327 break; 1328 case SO_SETFIB: 1329 INP_WLOCK(in6p); 1330 in6p->inp_inc.inc_fibnum = so->so_fibnum; 1331 INP_WUNLOCK(in6p); 1332 error = 0; 1333 break; 1334 default: 1335 break; 1336 } 1337 } 1338 } else { /* level == IPPROTO_IPV6 */ 1339 switch (op) { 1340 1341 case SOPT_SET: 1342 switch (optname) { 1343 case IPV6_2292PKTOPTIONS: 1344 #ifdef IPV6_PKTOPTIONS 1345 case IPV6_PKTOPTIONS: 1346 #endif 1347 { 1348 struct mbuf *m; 1349 1350 error = soopt_getm(sopt, &m); /* XXX */ 1351 if (error != 0) 1352 break; 1353 error = soopt_mcopyin(sopt, m); /* XXX */ 1354 if (error != 0) 1355 break; 1356 error = ip6_pcbopts(&in6p->in6p_outputopts, 1357 m, so, sopt); 1358 m_freem(m); /* XXX */ 1359 break; 1360 } 1361 1362 /* 1363 * Use of some Hop-by-Hop options or some 1364 * Destination options, might require special 1365 * privilege. That is, normal applications 1366 * (without special privilege) might be forbidden 1367 * from setting certain options in outgoing packets, 1368 * and might never see certain options in received 1369 * packets. [RFC 2292 Section 6] 1370 * KAME specific note: 1371 * KAME prevents non-privileged users from sending or 1372 * receiving ANY hbh/dst options in order to avoid 1373 * overhead of parsing options in the kernel. 1374 */ 1375 case IPV6_RECVHOPOPTS: 1376 case IPV6_RECVDSTOPTS: 1377 case IPV6_RECVRTHDRDSTOPTS: 1378 if (td != NULL) { 1379 error = priv_check(td, 1380 PRIV_NETINET_SETHDROPTS); 1381 if (error) 1382 break; 1383 } 1384 /* FALLTHROUGH */ 1385 case IPV6_UNICAST_HOPS: 1386 case IPV6_HOPLIMIT: 1387 1388 case IPV6_RECVPKTINFO: 1389 case IPV6_RECVHOPLIMIT: 1390 case IPV6_RECVRTHDR: 1391 case IPV6_RECVPATHMTU: 1392 case IPV6_RECVTCLASS: 1393 case IPV6_V6ONLY: 1394 case IPV6_AUTOFLOWLABEL: 1395 case IPV6_BINDANY: 1396 case IPV6_BINDMULTI: 1397 #ifdef RSS 1398 case IPV6_RSS_LISTEN_BUCKET: 1399 #endif 1400 if (optname == IPV6_BINDANY && td != NULL) { 1401 error = priv_check(td, 1402 PRIV_NETINET_BINDANY); 1403 if (error) 1404 break; 1405 } 1406 1407 if (optlen != sizeof(int)) { 1408 error = EINVAL; 1409 break; 1410 } 1411 error = sooptcopyin(sopt, &optval, 1412 sizeof optval, sizeof optval); 1413 if (error) 1414 break; 1415 switch (optname) { 1416 1417 case IPV6_UNICAST_HOPS: 1418 if (optval < -1 || optval >= 256) 1419 error = EINVAL; 1420 else { 1421 /* -1 = kernel default */ 1422 in6p->in6p_hops = optval; 1423 if ((in6p->inp_vflag & 1424 INP_IPV4) != 0) 1425 in6p->inp_ip_ttl = optval; 1426 } 1427 break; 1428 #define OPTSET(bit) \ 1429 do { \ 1430 INP_WLOCK(in6p); \ 1431 if (optval) \ 1432 in6p->inp_flags |= (bit); \ 1433 else \ 1434 in6p->inp_flags &= ~(bit); \ 1435 INP_WUNLOCK(in6p); \ 1436 } while (/*CONSTCOND*/ 0) 1437 #define OPTSET2292(bit) \ 1438 do { \ 1439 INP_WLOCK(in6p); \ 1440 in6p->inp_flags |= IN6P_RFC2292; \ 1441 if (optval) \ 1442 in6p->inp_flags |= (bit); \ 1443 else \ 1444 in6p->inp_flags &= ~(bit); \ 1445 INP_WUNLOCK(in6p); \ 1446 } while (/*CONSTCOND*/ 0) 1447 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0) 1448 1449 #define OPTSET2(bit, val) do { \ 1450 INP_WLOCK(in6p); \ 1451 if (val) \ 1452 in6p->inp_flags2 |= bit; \ 1453 else \ 1454 in6p->inp_flags2 &= ~bit; \ 1455 INP_WUNLOCK(in6p); \ 1456 } while (0) 1457 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0) 1458 1459 case IPV6_RECVPKTINFO: 1460 /* cannot mix with RFC2292 */ 1461 if (OPTBIT(IN6P_RFC2292)) { 1462 error = EINVAL; 1463 break; 1464 } 1465 OPTSET(IN6P_PKTINFO); 1466 break; 1467 1468 case IPV6_HOPLIMIT: 1469 { 1470 struct ip6_pktopts **optp; 1471 1472 /* cannot mix with RFC2292 */ 1473 if (OPTBIT(IN6P_RFC2292)) { 1474 error = EINVAL; 1475 break; 1476 } 1477 optp = &in6p->in6p_outputopts; 1478 error = ip6_pcbopt(IPV6_HOPLIMIT, 1479 (u_char *)&optval, sizeof(optval), 1480 optp, (td != NULL) ? td->td_ucred : 1481 NULL, uproto); 1482 break; 1483 } 1484 1485 case IPV6_RECVHOPLIMIT: 1486 /* cannot mix with RFC2292 */ 1487 if (OPTBIT(IN6P_RFC2292)) { 1488 error = EINVAL; 1489 break; 1490 } 1491 OPTSET(IN6P_HOPLIMIT); 1492 break; 1493 1494 case IPV6_RECVHOPOPTS: 1495 /* cannot mix with RFC2292 */ 1496 if (OPTBIT(IN6P_RFC2292)) { 1497 error = EINVAL; 1498 break; 1499 } 1500 OPTSET(IN6P_HOPOPTS); 1501 break; 1502 1503 case IPV6_RECVDSTOPTS: 1504 /* cannot mix with RFC2292 */ 1505 if (OPTBIT(IN6P_RFC2292)) { 1506 error = EINVAL; 1507 break; 1508 } 1509 OPTSET(IN6P_DSTOPTS); 1510 break; 1511 1512 case IPV6_RECVRTHDRDSTOPTS: 1513 /* cannot mix with RFC2292 */ 1514 if (OPTBIT(IN6P_RFC2292)) { 1515 error = EINVAL; 1516 break; 1517 } 1518 OPTSET(IN6P_RTHDRDSTOPTS); 1519 break; 1520 1521 case IPV6_RECVRTHDR: 1522 /* cannot mix with RFC2292 */ 1523 if (OPTBIT(IN6P_RFC2292)) { 1524 error = EINVAL; 1525 break; 1526 } 1527 OPTSET(IN6P_RTHDR); 1528 break; 1529 1530 case IPV6_RECVPATHMTU: 1531 /* 1532 * We ignore this option for TCP 1533 * sockets. 1534 * (RFC3542 leaves this case 1535 * unspecified.) 1536 */ 1537 if (uproto != IPPROTO_TCP) 1538 OPTSET(IN6P_MTU); 1539 break; 1540 1541 case IPV6_V6ONLY: 1542 /* 1543 * make setsockopt(IPV6_V6ONLY) 1544 * available only prior to bind(2). 1545 * see ipng mailing list, Jun 22 2001. 1546 */ 1547 if (in6p->inp_lport || 1548 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1549 error = EINVAL; 1550 break; 1551 } 1552 OPTSET(IN6P_IPV6_V6ONLY); 1553 if (optval) 1554 in6p->inp_vflag &= ~INP_IPV4; 1555 else 1556 in6p->inp_vflag |= INP_IPV4; 1557 break; 1558 case IPV6_RECVTCLASS: 1559 /* cannot mix with RFC2292 XXX */ 1560 if (OPTBIT(IN6P_RFC2292)) { 1561 error = EINVAL; 1562 break; 1563 } 1564 OPTSET(IN6P_TCLASS); 1565 break; 1566 case IPV6_AUTOFLOWLABEL: 1567 OPTSET(IN6P_AUTOFLOWLABEL); 1568 break; 1569 1570 case IPV6_BINDANY: 1571 OPTSET(INP_BINDANY); 1572 break; 1573 1574 case IPV6_BINDMULTI: 1575 OPTSET2(INP_BINDMULTI, optval); 1576 break; 1577 #ifdef RSS 1578 case IPV6_RSS_LISTEN_BUCKET: 1579 if ((optval >= 0) && 1580 (optval < rss_getnumbuckets())) { 1581 in6p->inp_rss_listen_bucket = optval; 1582 OPTSET2(INP_RSS_BUCKET_SET, 1); 1583 } else { 1584 error = EINVAL; 1585 } 1586 break; 1587 #endif 1588 } 1589 break; 1590 1591 case IPV6_TCLASS: 1592 case IPV6_DONTFRAG: 1593 case IPV6_USE_MIN_MTU: 1594 case IPV6_PREFER_TEMPADDR: 1595 if (optlen != sizeof(optval)) { 1596 error = EINVAL; 1597 break; 1598 } 1599 error = sooptcopyin(sopt, &optval, 1600 sizeof optval, sizeof optval); 1601 if (error) 1602 break; 1603 { 1604 struct ip6_pktopts **optp; 1605 optp = &in6p->in6p_outputopts; 1606 error = ip6_pcbopt(optname, 1607 (u_char *)&optval, sizeof(optval), 1608 optp, (td != NULL) ? td->td_ucred : 1609 NULL, uproto); 1610 break; 1611 } 1612 1613 case IPV6_2292PKTINFO: 1614 case IPV6_2292HOPLIMIT: 1615 case IPV6_2292HOPOPTS: 1616 case IPV6_2292DSTOPTS: 1617 case IPV6_2292RTHDR: 1618 /* RFC 2292 */ 1619 if (optlen != sizeof(int)) { 1620 error = EINVAL; 1621 break; 1622 } 1623 error = sooptcopyin(sopt, &optval, 1624 sizeof optval, sizeof optval); 1625 if (error) 1626 break; 1627 switch (optname) { 1628 case IPV6_2292PKTINFO: 1629 OPTSET2292(IN6P_PKTINFO); 1630 break; 1631 case IPV6_2292HOPLIMIT: 1632 OPTSET2292(IN6P_HOPLIMIT); 1633 break; 1634 case IPV6_2292HOPOPTS: 1635 /* 1636 * Check super-user privilege. 1637 * See comments for IPV6_RECVHOPOPTS. 1638 */ 1639 if (td != NULL) { 1640 error = priv_check(td, 1641 PRIV_NETINET_SETHDROPTS); 1642 if (error) 1643 return (error); 1644 } 1645 OPTSET2292(IN6P_HOPOPTS); 1646 break; 1647 case IPV6_2292DSTOPTS: 1648 if (td != NULL) { 1649 error = priv_check(td, 1650 PRIV_NETINET_SETHDROPTS); 1651 if (error) 1652 return (error); 1653 } 1654 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1655 break; 1656 case IPV6_2292RTHDR: 1657 OPTSET2292(IN6P_RTHDR); 1658 break; 1659 } 1660 break; 1661 case IPV6_PKTINFO: 1662 case IPV6_HOPOPTS: 1663 case IPV6_RTHDR: 1664 case IPV6_DSTOPTS: 1665 case IPV6_RTHDRDSTOPTS: 1666 case IPV6_NEXTHOP: 1667 { 1668 /* new advanced API (RFC3542) */ 1669 u_char *optbuf; 1670 u_char optbuf_storage[MCLBYTES]; 1671 int optlen; 1672 struct ip6_pktopts **optp; 1673 1674 /* cannot mix with RFC2292 */ 1675 if (OPTBIT(IN6P_RFC2292)) { 1676 error = EINVAL; 1677 break; 1678 } 1679 1680 /* 1681 * We only ensure valsize is not too large 1682 * here. Further validation will be done 1683 * later. 1684 */ 1685 error = sooptcopyin(sopt, optbuf_storage, 1686 sizeof(optbuf_storage), 0); 1687 if (error) 1688 break; 1689 optlen = sopt->sopt_valsize; 1690 optbuf = optbuf_storage; 1691 optp = &in6p->in6p_outputopts; 1692 error = ip6_pcbopt(optname, optbuf, optlen, 1693 optp, (td != NULL) ? td->td_ucred : NULL, 1694 uproto); 1695 break; 1696 } 1697 #undef OPTSET 1698 1699 case IPV6_MULTICAST_IF: 1700 case IPV6_MULTICAST_HOPS: 1701 case IPV6_MULTICAST_LOOP: 1702 case IPV6_JOIN_GROUP: 1703 case IPV6_LEAVE_GROUP: 1704 case IPV6_MSFILTER: 1705 case MCAST_BLOCK_SOURCE: 1706 case MCAST_UNBLOCK_SOURCE: 1707 case MCAST_JOIN_GROUP: 1708 case MCAST_LEAVE_GROUP: 1709 case MCAST_JOIN_SOURCE_GROUP: 1710 case MCAST_LEAVE_SOURCE_GROUP: 1711 error = ip6_setmoptions(in6p, sopt); 1712 break; 1713 1714 case IPV6_PORTRANGE: 1715 error = sooptcopyin(sopt, &optval, 1716 sizeof optval, sizeof optval); 1717 if (error) 1718 break; 1719 1720 INP_WLOCK(in6p); 1721 switch (optval) { 1722 case IPV6_PORTRANGE_DEFAULT: 1723 in6p->inp_flags &= ~(INP_LOWPORT); 1724 in6p->inp_flags &= ~(INP_HIGHPORT); 1725 break; 1726 1727 case IPV6_PORTRANGE_HIGH: 1728 in6p->inp_flags &= ~(INP_LOWPORT); 1729 in6p->inp_flags |= INP_HIGHPORT; 1730 break; 1731 1732 case IPV6_PORTRANGE_LOW: 1733 in6p->inp_flags &= ~(INP_HIGHPORT); 1734 in6p->inp_flags |= INP_LOWPORT; 1735 break; 1736 1737 default: 1738 error = EINVAL; 1739 break; 1740 } 1741 INP_WUNLOCK(in6p); 1742 break; 1743 1744 #ifdef IPSEC 1745 case IPV6_IPSEC_POLICY: 1746 { 1747 caddr_t req; 1748 struct mbuf *m; 1749 1750 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1751 break; 1752 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1753 break; 1754 req = mtod(m, caddr_t); 1755 error = ipsec_set_policy(in6p, optname, req, 1756 m->m_len, (sopt->sopt_td != NULL) ? 1757 sopt->sopt_td->td_ucred : NULL); 1758 m_freem(m); 1759 break; 1760 } 1761 #endif /* IPSEC */ 1762 1763 default: 1764 error = ENOPROTOOPT; 1765 break; 1766 } 1767 break; 1768 1769 case SOPT_GET: 1770 switch (optname) { 1771 1772 case IPV6_2292PKTOPTIONS: 1773 #ifdef IPV6_PKTOPTIONS 1774 case IPV6_PKTOPTIONS: 1775 #endif 1776 /* 1777 * RFC3542 (effectively) deprecated the 1778 * semantics of the 2292-style pktoptions. 1779 * Since it was not reliable in nature (i.e., 1780 * applications had to expect the lack of some 1781 * information after all), it would make sense 1782 * to simplify this part by always returning 1783 * empty data. 1784 */ 1785 sopt->sopt_valsize = 0; 1786 break; 1787 1788 case IPV6_RECVHOPOPTS: 1789 case IPV6_RECVDSTOPTS: 1790 case IPV6_RECVRTHDRDSTOPTS: 1791 case IPV6_UNICAST_HOPS: 1792 case IPV6_RECVPKTINFO: 1793 case IPV6_RECVHOPLIMIT: 1794 case IPV6_RECVRTHDR: 1795 case IPV6_RECVPATHMTU: 1796 1797 case IPV6_V6ONLY: 1798 case IPV6_PORTRANGE: 1799 case IPV6_RECVTCLASS: 1800 case IPV6_AUTOFLOWLABEL: 1801 case IPV6_BINDANY: 1802 case IPV6_FLOWID: 1803 case IPV6_FLOWTYPE: 1804 #ifdef RSS 1805 case IPV6_RSSBUCKETID: 1806 #endif 1807 switch (optname) { 1808 1809 case IPV6_RECVHOPOPTS: 1810 optval = OPTBIT(IN6P_HOPOPTS); 1811 break; 1812 1813 case IPV6_RECVDSTOPTS: 1814 optval = OPTBIT(IN6P_DSTOPTS); 1815 break; 1816 1817 case IPV6_RECVRTHDRDSTOPTS: 1818 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 1819 break; 1820 1821 case IPV6_UNICAST_HOPS: 1822 optval = in6p->in6p_hops; 1823 break; 1824 1825 case IPV6_RECVPKTINFO: 1826 optval = OPTBIT(IN6P_PKTINFO); 1827 break; 1828 1829 case IPV6_RECVHOPLIMIT: 1830 optval = OPTBIT(IN6P_HOPLIMIT); 1831 break; 1832 1833 case IPV6_RECVRTHDR: 1834 optval = OPTBIT(IN6P_RTHDR); 1835 break; 1836 1837 case IPV6_RECVPATHMTU: 1838 optval = OPTBIT(IN6P_MTU); 1839 break; 1840 1841 case IPV6_V6ONLY: 1842 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1843 break; 1844 1845 case IPV6_PORTRANGE: 1846 { 1847 int flags; 1848 flags = in6p->inp_flags; 1849 if (flags & INP_HIGHPORT) 1850 optval = IPV6_PORTRANGE_HIGH; 1851 else if (flags & INP_LOWPORT) 1852 optval = IPV6_PORTRANGE_LOW; 1853 else 1854 optval = 0; 1855 break; 1856 } 1857 case IPV6_RECVTCLASS: 1858 optval = OPTBIT(IN6P_TCLASS); 1859 break; 1860 1861 case IPV6_AUTOFLOWLABEL: 1862 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 1863 break; 1864 1865 case IPV6_BINDANY: 1866 optval = OPTBIT(INP_BINDANY); 1867 break; 1868 1869 case IPV6_FLOWID: 1870 optval = in6p->inp_flowid; 1871 break; 1872 1873 case IPV6_FLOWTYPE: 1874 optval = in6p->inp_flowtype; 1875 break; 1876 #ifdef RSS 1877 case IPV6_RSSBUCKETID: 1878 retval = 1879 rss_hash2bucket(in6p->inp_flowid, 1880 in6p->inp_flowtype, 1881 &rss_bucket); 1882 if (retval == 0) 1883 optval = rss_bucket; 1884 else 1885 error = EINVAL; 1886 break; 1887 #endif 1888 1889 case IPV6_BINDMULTI: 1890 optval = OPTBIT2(INP_BINDMULTI); 1891 break; 1892 1893 } 1894 if (error) 1895 break; 1896 error = sooptcopyout(sopt, &optval, 1897 sizeof optval); 1898 break; 1899 1900 case IPV6_PATHMTU: 1901 { 1902 u_long pmtu = 0; 1903 struct ip6_mtuinfo mtuinfo; 1904 struct route_in6 sro; 1905 1906 bzero(&sro, sizeof(sro)); 1907 1908 if (!(so->so_state & SS_ISCONNECTED)) 1909 return (ENOTCONN); 1910 /* 1911 * XXX: we dot not consider the case of source 1912 * routing, or optional information to specify 1913 * the outgoing interface. 1914 */ 1915 error = ip6_getpmtu(&sro, NULL, NULL, 1916 &in6p->in6p_faddr, &pmtu, NULL, 1917 so->so_fibnum); 1918 if (sro.ro_rt) 1919 RTFREE(sro.ro_rt); 1920 if (error) 1921 break; 1922 if (pmtu > IPV6_MAXPACKET) 1923 pmtu = IPV6_MAXPACKET; 1924 1925 bzero(&mtuinfo, sizeof(mtuinfo)); 1926 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 1927 optdata = (void *)&mtuinfo; 1928 optdatalen = sizeof(mtuinfo); 1929 error = sooptcopyout(sopt, optdata, 1930 optdatalen); 1931 break; 1932 } 1933 1934 case IPV6_2292PKTINFO: 1935 case IPV6_2292HOPLIMIT: 1936 case IPV6_2292HOPOPTS: 1937 case IPV6_2292RTHDR: 1938 case IPV6_2292DSTOPTS: 1939 switch (optname) { 1940 case IPV6_2292PKTINFO: 1941 optval = OPTBIT(IN6P_PKTINFO); 1942 break; 1943 case IPV6_2292HOPLIMIT: 1944 optval = OPTBIT(IN6P_HOPLIMIT); 1945 break; 1946 case IPV6_2292HOPOPTS: 1947 optval = OPTBIT(IN6P_HOPOPTS); 1948 break; 1949 case IPV6_2292RTHDR: 1950 optval = OPTBIT(IN6P_RTHDR); 1951 break; 1952 case IPV6_2292DSTOPTS: 1953 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1954 break; 1955 } 1956 error = sooptcopyout(sopt, &optval, 1957 sizeof optval); 1958 break; 1959 case IPV6_PKTINFO: 1960 case IPV6_HOPOPTS: 1961 case IPV6_RTHDR: 1962 case IPV6_DSTOPTS: 1963 case IPV6_RTHDRDSTOPTS: 1964 case IPV6_NEXTHOP: 1965 case IPV6_TCLASS: 1966 case IPV6_DONTFRAG: 1967 case IPV6_USE_MIN_MTU: 1968 case IPV6_PREFER_TEMPADDR: 1969 error = ip6_getpcbopt(in6p->in6p_outputopts, 1970 optname, sopt); 1971 break; 1972 1973 case IPV6_MULTICAST_IF: 1974 case IPV6_MULTICAST_HOPS: 1975 case IPV6_MULTICAST_LOOP: 1976 case IPV6_MSFILTER: 1977 error = ip6_getmoptions(in6p, sopt); 1978 break; 1979 1980 #ifdef IPSEC 1981 case IPV6_IPSEC_POLICY: 1982 { 1983 caddr_t req = NULL; 1984 size_t len = 0; 1985 struct mbuf *m = NULL; 1986 struct mbuf **mp = &m; 1987 size_t ovalsize = sopt->sopt_valsize; 1988 caddr_t oval = (caddr_t)sopt->sopt_val; 1989 1990 error = soopt_getm(sopt, &m); /* XXX */ 1991 if (error != 0) 1992 break; 1993 error = soopt_mcopyin(sopt, m); /* XXX */ 1994 if (error != 0) 1995 break; 1996 sopt->sopt_valsize = ovalsize; 1997 sopt->sopt_val = oval; 1998 if (m) { 1999 req = mtod(m, caddr_t); 2000 len = m->m_len; 2001 } 2002 error = ipsec_get_policy(in6p, req, len, mp); 2003 if (error == 0) 2004 error = soopt_mcopyout(sopt, m); /* XXX */ 2005 if (error == 0 && m) 2006 m_freem(m); 2007 break; 2008 } 2009 #endif /* IPSEC */ 2010 2011 default: 2012 error = ENOPROTOOPT; 2013 break; 2014 } 2015 break; 2016 } 2017 } 2018 return (error); 2019 } 2020 2021 int 2022 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt) 2023 { 2024 int error = 0, optval, optlen; 2025 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 2026 struct inpcb *in6p = sotoinpcb(so); 2027 int level, op, optname; 2028 2029 level = sopt->sopt_level; 2030 op = sopt->sopt_dir; 2031 optname = sopt->sopt_name; 2032 optlen = sopt->sopt_valsize; 2033 2034 if (level != IPPROTO_IPV6) { 2035 return (EINVAL); 2036 } 2037 2038 switch (optname) { 2039 case IPV6_CHECKSUM: 2040 /* 2041 * For ICMPv6 sockets, no modification allowed for checksum 2042 * offset, permit "no change" values to help existing apps. 2043 * 2044 * RFC3542 says: "An attempt to set IPV6_CHECKSUM 2045 * for an ICMPv6 socket will fail." 2046 * The current behavior does not meet RFC3542. 2047 */ 2048 switch (op) { 2049 case SOPT_SET: 2050 if (optlen != sizeof(int)) { 2051 error = EINVAL; 2052 break; 2053 } 2054 error = sooptcopyin(sopt, &optval, sizeof(optval), 2055 sizeof(optval)); 2056 if (error) 2057 break; 2058 if ((optval % 2) != 0) { 2059 /* the API assumes even offset values */ 2060 error = EINVAL; 2061 } else if (so->so_proto->pr_protocol == 2062 IPPROTO_ICMPV6) { 2063 if (optval != icmp6off) 2064 error = EINVAL; 2065 } else 2066 in6p->in6p_cksum = optval; 2067 break; 2068 2069 case SOPT_GET: 2070 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 2071 optval = icmp6off; 2072 else 2073 optval = in6p->in6p_cksum; 2074 2075 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2076 break; 2077 2078 default: 2079 error = EINVAL; 2080 break; 2081 } 2082 break; 2083 2084 default: 2085 error = ENOPROTOOPT; 2086 break; 2087 } 2088 2089 return (error); 2090 } 2091 2092 /* 2093 * Set up IP6 options in pcb for insertion in output packets or 2094 * specifying behavior of outgoing packets. 2095 */ 2096 static int 2097 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, 2098 struct socket *so, struct sockopt *sopt) 2099 { 2100 struct ip6_pktopts *opt = *pktopt; 2101 int error = 0; 2102 struct thread *td = sopt->sopt_td; 2103 2104 /* turn off any old options. */ 2105 if (opt) { 2106 #ifdef DIAGNOSTIC 2107 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 2108 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 2109 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 2110 printf("ip6_pcbopts: all specified options are cleared.\n"); 2111 #endif 2112 ip6_clearpktopts(opt, -1); 2113 } else 2114 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 2115 *pktopt = NULL; 2116 2117 if (!m || m->m_len == 0) { 2118 /* 2119 * Only turning off any previous options, regardless of 2120 * whether the opt is just created or given. 2121 */ 2122 free(opt, M_IP6OPT); 2123 return (0); 2124 } 2125 2126 /* set options specified by user. */ 2127 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ? 2128 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) { 2129 ip6_clearpktopts(opt, -1); /* XXX: discard all options */ 2130 free(opt, M_IP6OPT); 2131 return (error); 2132 } 2133 *pktopt = opt; 2134 return (0); 2135 } 2136 2137 /* 2138 * initialize ip6_pktopts. beware that there are non-zero default values in 2139 * the struct. 2140 */ 2141 void 2142 ip6_initpktopts(struct ip6_pktopts *opt) 2143 { 2144 2145 bzero(opt, sizeof(*opt)); 2146 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 2147 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 2148 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 2149 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; 2150 } 2151 2152 static int 2153 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, 2154 struct ucred *cred, int uproto) 2155 { 2156 struct ip6_pktopts *opt; 2157 2158 if (*pktopt == NULL) { 2159 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 2160 M_WAITOK); 2161 ip6_initpktopts(*pktopt); 2162 } 2163 opt = *pktopt; 2164 2165 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto)); 2166 } 2167 2168 static int 2169 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt) 2170 { 2171 void *optdata = NULL; 2172 int optdatalen = 0; 2173 struct ip6_ext *ip6e; 2174 int error = 0; 2175 struct in6_pktinfo null_pktinfo; 2176 int deftclass = 0, on; 2177 int defminmtu = IP6PO_MINMTU_MCASTONLY; 2178 int defpreftemp = IP6PO_TEMPADDR_SYSTEM; 2179 2180 switch (optname) { 2181 case IPV6_PKTINFO: 2182 if (pktopt && pktopt->ip6po_pktinfo) 2183 optdata = (void *)pktopt->ip6po_pktinfo; 2184 else { 2185 /* XXX: we don't have to do this every time... */ 2186 bzero(&null_pktinfo, sizeof(null_pktinfo)); 2187 optdata = (void *)&null_pktinfo; 2188 } 2189 optdatalen = sizeof(struct in6_pktinfo); 2190 break; 2191 case IPV6_TCLASS: 2192 if (pktopt && pktopt->ip6po_tclass >= 0) 2193 optdata = (void *)&pktopt->ip6po_tclass; 2194 else 2195 optdata = (void *)&deftclass; 2196 optdatalen = sizeof(int); 2197 break; 2198 case IPV6_HOPOPTS: 2199 if (pktopt && pktopt->ip6po_hbh) { 2200 optdata = (void *)pktopt->ip6po_hbh; 2201 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; 2202 optdatalen = (ip6e->ip6e_len + 1) << 3; 2203 } 2204 break; 2205 case IPV6_RTHDR: 2206 if (pktopt && pktopt->ip6po_rthdr) { 2207 optdata = (void *)pktopt->ip6po_rthdr; 2208 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; 2209 optdatalen = (ip6e->ip6e_len + 1) << 3; 2210 } 2211 break; 2212 case IPV6_RTHDRDSTOPTS: 2213 if (pktopt && pktopt->ip6po_dest1) { 2214 optdata = (void *)pktopt->ip6po_dest1; 2215 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; 2216 optdatalen = (ip6e->ip6e_len + 1) << 3; 2217 } 2218 break; 2219 case IPV6_DSTOPTS: 2220 if (pktopt && pktopt->ip6po_dest2) { 2221 optdata = (void *)pktopt->ip6po_dest2; 2222 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; 2223 optdatalen = (ip6e->ip6e_len + 1) << 3; 2224 } 2225 break; 2226 case IPV6_NEXTHOP: 2227 if (pktopt && pktopt->ip6po_nexthop) { 2228 optdata = (void *)pktopt->ip6po_nexthop; 2229 optdatalen = pktopt->ip6po_nexthop->sa_len; 2230 } 2231 break; 2232 case IPV6_USE_MIN_MTU: 2233 if (pktopt) 2234 optdata = (void *)&pktopt->ip6po_minmtu; 2235 else 2236 optdata = (void *)&defminmtu; 2237 optdatalen = sizeof(int); 2238 break; 2239 case IPV6_DONTFRAG: 2240 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 2241 on = 1; 2242 else 2243 on = 0; 2244 optdata = (void *)&on; 2245 optdatalen = sizeof(on); 2246 break; 2247 case IPV6_PREFER_TEMPADDR: 2248 if (pktopt) 2249 optdata = (void *)&pktopt->ip6po_prefer_tempaddr; 2250 else 2251 optdata = (void *)&defpreftemp; 2252 optdatalen = sizeof(int); 2253 break; 2254 default: /* should not happen */ 2255 #ifdef DIAGNOSTIC 2256 panic("ip6_getpcbopt: unexpected option\n"); 2257 #endif 2258 return (ENOPROTOOPT); 2259 } 2260 2261 error = sooptcopyout(sopt, optdata, optdatalen); 2262 2263 return (error); 2264 } 2265 2266 void 2267 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) 2268 { 2269 if (pktopt == NULL) 2270 return; 2271 2272 if (optname == -1 || optname == IPV6_PKTINFO) { 2273 if (pktopt->ip6po_pktinfo) 2274 free(pktopt->ip6po_pktinfo, M_IP6OPT); 2275 pktopt->ip6po_pktinfo = NULL; 2276 } 2277 if (optname == -1 || optname == IPV6_HOPLIMIT) 2278 pktopt->ip6po_hlim = -1; 2279 if (optname == -1 || optname == IPV6_TCLASS) 2280 pktopt->ip6po_tclass = -1; 2281 if (optname == -1 || optname == IPV6_NEXTHOP) { 2282 if (pktopt->ip6po_nextroute.ro_rt) { 2283 RTFREE(pktopt->ip6po_nextroute.ro_rt); 2284 pktopt->ip6po_nextroute.ro_rt = NULL; 2285 } 2286 if (pktopt->ip6po_nexthop) 2287 free(pktopt->ip6po_nexthop, M_IP6OPT); 2288 pktopt->ip6po_nexthop = NULL; 2289 } 2290 if (optname == -1 || optname == IPV6_HOPOPTS) { 2291 if (pktopt->ip6po_hbh) 2292 free(pktopt->ip6po_hbh, M_IP6OPT); 2293 pktopt->ip6po_hbh = NULL; 2294 } 2295 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 2296 if (pktopt->ip6po_dest1) 2297 free(pktopt->ip6po_dest1, M_IP6OPT); 2298 pktopt->ip6po_dest1 = NULL; 2299 } 2300 if (optname == -1 || optname == IPV6_RTHDR) { 2301 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 2302 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 2303 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 2304 if (pktopt->ip6po_route.ro_rt) { 2305 RTFREE(pktopt->ip6po_route.ro_rt); 2306 pktopt->ip6po_route.ro_rt = NULL; 2307 } 2308 } 2309 if (optname == -1 || optname == IPV6_DSTOPTS) { 2310 if (pktopt->ip6po_dest2) 2311 free(pktopt->ip6po_dest2, M_IP6OPT); 2312 pktopt->ip6po_dest2 = NULL; 2313 } 2314 } 2315 2316 #define PKTOPT_EXTHDRCPY(type) \ 2317 do {\ 2318 if (src->type) {\ 2319 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 2320 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 2321 if (dst->type == NULL && canwait == M_NOWAIT)\ 2322 goto bad;\ 2323 bcopy(src->type, dst->type, hlen);\ 2324 }\ 2325 } while (/*CONSTCOND*/ 0) 2326 2327 static int 2328 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) 2329 { 2330 if (dst == NULL || src == NULL) { 2331 printf("ip6_clearpktopts: invalid argument\n"); 2332 return (EINVAL); 2333 } 2334 2335 dst->ip6po_hlim = src->ip6po_hlim; 2336 dst->ip6po_tclass = src->ip6po_tclass; 2337 dst->ip6po_flags = src->ip6po_flags; 2338 dst->ip6po_minmtu = src->ip6po_minmtu; 2339 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr; 2340 if (src->ip6po_pktinfo) { 2341 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2342 M_IP6OPT, canwait); 2343 if (dst->ip6po_pktinfo == NULL) 2344 goto bad; 2345 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2346 } 2347 if (src->ip6po_nexthop) { 2348 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2349 M_IP6OPT, canwait); 2350 if (dst->ip6po_nexthop == NULL) 2351 goto bad; 2352 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 2353 src->ip6po_nexthop->sa_len); 2354 } 2355 PKTOPT_EXTHDRCPY(ip6po_hbh); 2356 PKTOPT_EXTHDRCPY(ip6po_dest1); 2357 PKTOPT_EXTHDRCPY(ip6po_dest2); 2358 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2359 return (0); 2360 2361 bad: 2362 ip6_clearpktopts(dst, -1); 2363 return (ENOBUFS); 2364 } 2365 #undef PKTOPT_EXTHDRCPY 2366 2367 struct ip6_pktopts * 2368 ip6_copypktopts(struct ip6_pktopts *src, int canwait) 2369 { 2370 int error; 2371 struct ip6_pktopts *dst; 2372 2373 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2374 if (dst == NULL) 2375 return (NULL); 2376 ip6_initpktopts(dst); 2377 2378 if ((error = copypktopts(dst, src, canwait)) != 0) { 2379 free(dst, M_IP6OPT); 2380 return (NULL); 2381 } 2382 2383 return (dst); 2384 } 2385 2386 void 2387 ip6_freepcbopts(struct ip6_pktopts *pktopt) 2388 { 2389 if (pktopt == NULL) 2390 return; 2391 2392 ip6_clearpktopts(pktopt, -1); 2393 2394 free(pktopt, M_IP6OPT); 2395 } 2396 2397 /* 2398 * Set IPv6 outgoing packet options based on advanced API. 2399 */ 2400 int 2401 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2402 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto) 2403 { 2404 struct cmsghdr *cm = 0; 2405 2406 if (control == NULL || opt == NULL) 2407 return (EINVAL); 2408 2409 ip6_initpktopts(opt); 2410 if (stickyopt) { 2411 int error; 2412 2413 /* 2414 * If stickyopt is provided, make a local copy of the options 2415 * for this particular packet, then override them by ancillary 2416 * objects. 2417 * XXX: copypktopts() does not copy the cached route to a next 2418 * hop (if any). This is not very good in terms of efficiency, 2419 * but we can allow this since this option should be rarely 2420 * used. 2421 */ 2422 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2423 return (error); 2424 } 2425 2426 /* 2427 * XXX: Currently, we assume all the optional information is stored 2428 * in a single mbuf. 2429 */ 2430 if (control->m_next) 2431 return (EINVAL); 2432 2433 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2434 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2435 int error; 2436 2437 if (control->m_len < CMSG_LEN(0)) 2438 return (EINVAL); 2439 2440 cm = mtod(control, struct cmsghdr *); 2441 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2442 return (EINVAL); 2443 if (cm->cmsg_level != IPPROTO_IPV6) 2444 continue; 2445 2446 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2447 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); 2448 if (error) 2449 return (error); 2450 } 2451 2452 return (0); 2453 } 2454 2455 /* 2456 * Set a particular packet option, as a sticky option or an ancillary data 2457 * item. "len" can be 0 only when it's a sticky option. 2458 * We have 4 cases of combination of "sticky" and "cmsg": 2459 * "sticky=0, cmsg=0": impossible 2460 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2461 * "sticky=1, cmsg=0": RFC3542 socket option 2462 * "sticky=1, cmsg=1": RFC2292 socket option 2463 */ 2464 static int 2465 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2466 struct ucred *cred, int sticky, int cmsg, int uproto) 2467 { 2468 int minmtupolicy, preftemp; 2469 int error; 2470 2471 if (!sticky && !cmsg) { 2472 #ifdef DIAGNOSTIC 2473 printf("ip6_setpktopt: impossible case\n"); 2474 #endif 2475 return (EINVAL); 2476 } 2477 2478 /* 2479 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2480 * not be specified in the context of RFC3542. Conversely, 2481 * RFC3542 types should not be specified in the context of RFC2292. 2482 */ 2483 if (!cmsg) { 2484 switch (optname) { 2485 case IPV6_2292PKTINFO: 2486 case IPV6_2292HOPLIMIT: 2487 case IPV6_2292NEXTHOP: 2488 case IPV6_2292HOPOPTS: 2489 case IPV6_2292DSTOPTS: 2490 case IPV6_2292RTHDR: 2491 case IPV6_2292PKTOPTIONS: 2492 return (ENOPROTOOPT); 2493 } 2494 } 2495 if (sticky && cmsg) { 2496 switch (optname) { 2497 case IPV6_PKTINFO: 2498 case IPV6_HOPLIMIT: 2499 case IPV6_NEXTHOP: 2500 case IPV6_HOPOPTS: 2501 case IPV6_DSTOPTS: 2502 case IPV6_RTHDRDSTOPTS: 2503 case IPV6_RTHDR: 2504 case IPV6_USE_MIN_MTU: 2505 case IPV6_DONTFRAG: 2506 case IPV6_TCLASS: 2507 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */ 2508 return (ENOPROTOOPT); 2509 } 2510 } 2511 2512 switch (optname) { 2513 case IPV6_2292PKTINFO: 2514 case IPV6_PKTINFO: 2515 { 2516 struct ifnet *ifp = NULL; 2517 struct in6_pktinfo *pktinfo; 2518 2519 if (len != sizeof(struct in6_pktinfo)) 2520 return (EINVAL); 2521 2522 pktinfo = (struct in6_pktinfo *)buf; 2523 2524 /* 2525 * An application can clear any sticky IPV6_PKTINFO option by 2526 * doing a "regular" setsockopt with ipi6_addr being 2527 * in6addr_any and ipi6_ifindex being zero. 2528 * [RFC 3542, Section 6] 2529 */ 2530 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2531 pktinfo->ipi6_ifindex == 0 && 2532 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2533 ip6_clearpktopts(opt, optname); 2534 break; 2535 } 2536 2537 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2538 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2539 return (EINVAL); 2540 } 2541 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr)) 2542 return (EINVAL); 2543 /* validate the interface index if specified. */ 2544 if (pktinfo->ipi6_ifindex > V_if_index) 2545 return (ENXIO); 2546 if (pktinfo->ipi6_ifindex) { 2547 ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 2548 if (ifp == NULL) 2549 return (ENXIO); 2550 } 2551 if (ifp != NULL && ( 2552 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) 2553 return (ENETDOWN); 2554 2555 if (ifp != NULL && 2556 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2557 struct in6_ifaddr *ia; 2558 2559 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr); 2560 if (ia == NULL) 2561 return (EADDRNOTAVAIL); 2562 ifa_free(&ia->ia_ifa); 2563 } 2564 /* 2565 * We store the address anyway, and let in6_selectsrc() 2566 * validate the specified address. This is because ipi6_addr 2567 * may not have enough information about its scope zone, and 2568 * we may need additional information (such as outgoing 2569 * interface or the scope zone of a destination address) to 2570 * disambiguate the scope. 2571 * XXX: the delay of the validation may confuse the 2572 * application when it is used as a sticky option. 2573 */ 2574 if (opt->ip6po_pktinfo == NULL) { 2575 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2576 M_IP6OPT, M_NOWAIT); 2577 if (opt->ip6po_pktinfo == NULL) 2578 return (ENOBUFS); 2579 } 2580 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 2581 break; 2582 } 2583 2584 case IPV6_2292HOPLIMIT: 2585 case IPV6_HOPLIMIT: 2586 { 2587 int *hlimp; 2588 2589 /* 2590 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2591 * to simplify the ordering among hoplimit options. 2592 */ 2593 if (optname == IPV6_HOPLIMIT && sticky) 2594 return (ENOPROTOOPT); 2595 2596 if (len != sizeof(int)) 2597 return (EINVAL); 2598 hlimp = (int *)buf; 2599 if (*hlimp < -1 || *hlimp > 255) 2600 return (EINVAL); 2601 2602 opt->ip6po_hlim = *hlimp; 2603 break; 2604 } 2605 2606 case IPV6_TCLASS: 2607 { 2608 int tclass; 2609 2610 if (len != sizeof(int)) 2611 return (EINVAL); 2612 tclass = *(int *)buf; 2613 if (tclass < -1 || tclass > 255) 2614 return (EINVAL); 2615 2616 opt->ip6po_tclass = tclass; 2617 break; 2618 } 2619 2620 case IPV6_2292NEXTHOP: 2621 case IPV6_NEXTHOP: 2622 if (cred != NULL) { 2623 error = priv_check_cred(cred, 2624 PRIV_NETINET_SETHDROPTS, 0); 2625 if (error) 2626 return (error); 2627 } 2628 2629 if (len == 0) { /* just remove the option */ 2630 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2631 break; 2632 } 2633 2634 /* check if cmsg_len is large enough for sa_len */ 2635 if (len < sizeof(struct sockaddr) || len < *buf) 2636 return (EINVAL); 2637 2638 switch (((struct sockaddr *)buf)->sa_family) { 2639 case AF_INET6: 2640 { 2641 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 2642 int error; 2643 2644 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 2645 return (EINVAL); 2646 2647 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 2648 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 2649 return (EINVAL); 2650 } 2651 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone)) 2652 != 0) { 2653 return (error); 2654 } 2655 break; 2656 } 2657 case AF_LINK: /* should eventually be supported */ 2658 default: 2659 return (EAFNOSUPPORT); 2660 } 2661 2662 /* turn off the previous option, then set the new option. */ 2663 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2664 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 2665 if (opt->ip6po_nexthop == NULL) 2666 return (ENOBUFS); 2667 bcopy(buf, opt->ip6po_nexthop, *buf); 2668 break; 2669 2670 case IPV6_2292HOPOPTS: 2671 case IPV6_HOPOPTS: 2672 { 2673 struct ip6_hbh *hbh; 2674 int hbhlen; 2675 2676 /* 2677 * XXX: We don't allow a non-privileged user to set ANY HbH 2678 * options, since per-option restriction has too much 2679 * overhead. 2680 */ 2681 if (cred != NULL) { 2682 error = priv_check_cred(cred, 2683 PRIV_NETINET_SETHDROPTS, 0); 2684 if (error) 2685 return (error); 2686 } 2687 2688 if (len == 0) { 2689 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2690 break; /* just remove the option */ 2691 } 2692 2693 /* message length validation */ 2694 if (len < sizeof(struct ip6_hbh)) 2695 return (EINVAL); 2696 hbh = (struct ip6_hbh *)buf; 2697 hbhlen = (hbh->ip6h_len + 1) << 3; 2698 if (len != hbhlen) 2699 return (EINVAL); 2700 2701 /* turn off the previous option, then set the new option. */ 2702 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2703 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 2704 if (opt->ip6po_hbh == NULL) 2705 return (ENOBUFS); 2706 bcopy(hbh, opt->ip6po_hbh, hbhlen); 2707 2708 break; 2709 } 2710 2711 case IPV6_2292DSTOPTS: 2712 case IPV6_DSTOPTS: 2713 case IPV6_RTHDRDSTOPTS: 2714 { 2715 struct ip6_dest *dest, **newdest = NULL; 2716 int destlen; 2717 2718 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */ 2719 error = priv_check_cred(cred, 2720 PRIV_NETINET_SETHDROPTS, 0); 2721 if (error) 2722 return (error); 2723 } 2724 2725 if (len == 0) { 2726 ip6_clearpktopts(opt, optname); 2727 break; /* just remove the option */ 2728 } 2729 2730 /* message length validation */ 2731 if (len < sizeof(struct ip6_dest)) 2732 return (EINVAL); 2733 dest = (struct ip6_dest *)buf; 2734 destlen = (dest->ip6d_len + 1) << 3; 2735 if (len != destlen) 2736 return (EINVAL); 2737 2738 /* 2739 * Determine the position that the destination options header 2740 * should be inserted; before or after the routing header. 2741 */ 2742 switch (optname) { 2743 case IPV6_2292DSTOPTS: 2744 /* 2745 * The old advacned API is ambiguous on this point. 2746 * Our approach is to determine the position based 2747 * according to the existence of a routing header. 2748 * Note, however, that this depends on the order of the 2749 * extension headers in the ancillary data; the 1st 2750 * part of the destination options header must appear 2751 * before the routing header in the ancillary data, 2752 * too. 2753 * RFC3542 solved the ambiguity by introducing 2754 * separate ancillary data or option types. 2755 */ 2756 if (opt->ip6po_rthdr == NULL) 2757 newdest = &opt->ip6po_dest1; 2758 else 2759 newdest = &opt->ip6po_dest2; 2760 break; 2761 case IPV6_RTHDRDSTOPTS: 2762 newdest = &opt->ip6po_dest1; 2763 break; 2764 case IPV6_DSTOPTS: 2765 newdest = &opt->ip6po_dest2; 2766 break; 2767 } 2768 2769 /* turn off the previous option, then set the new option. */ 2770 ip6_clearpktopts(opt, optname); 2771 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 2772 if (*newdest == NULL) 2773 return (ENOBUFS); 2774 bcopy(dest, *newdest, destlen); 2775 2776 break; 2777 } 2778 2779 case IPV6_2292RTHDR: 2780 case IPV6_RTHDR: 2781 { 2782 struct ip6_rthdr *rth; 2783 int rthlen; 2784 2785 if (len == 0) { 2786 ip6_clearpktopts(opt, IPV6_RTHDR); 2787 break; /* just remove the option */ 2788 } 2789 2790 /* message length validation */ 2791 if (len < sizeof(struct ip6_rthdr)) 2792 return (EINVAL); 2793 rth = (struct ip6_rthdr *)buf; 2794 rthlen = (rth->ip6r_len + 1) << 3; 2795 if (len != rthlen) 2796 return (EINVAL); 2797 2798 switch (rth->ip6r_type) { 2799 case IPV6_RTHDR_TYPE_0: 2800 if (rth->ip6r_len == 0) /* must contain one addr */ 2801 return (EINVAL); 2802 if (rth->ip6r_len % 2) /* length must be even */ 2803 return (EINVAL); 2804 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 2805 return (EINVAL); 2806 break; 2807 default: 2808 return (EINVAL); /* not supported */ 2809 } 2810 2811 /* turn off the previous option */ 2812 ip6_clearpktopts(opt, IPV6_RTHDR); 2813 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 2814 if (opt->ip6po_rthdr == NULL) 2815 return (ENOBUFS); 2816 bcopy(rth, opt->ip6po_rthdr, rthlen); 2817 2818 break; 2819 } 2820 2821 case IPV6_USE_MIN_MTU: 2822 if (len != sizeof(int)) 2823 return (EINVAL); 2824 minmtupolicy = *(int *)buf; 2825 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 2826 minmtupolicy != IP6PO_MINMTU_DISABLE && 2827 minmtupolicy != IP6PO_MINMTU_ALL) { 2828 return (EINVAL); 2829 } 2830 opt->ip6po_minmtu = minmtupolicy; 2831 break; 2832 2833 case IPV6_DONTFRAG: 2834 if (len != sizeof(int)) 2835 return (EINVAL); 2836 2837 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 2838 /* 2839 * we ignore this option for TCP sockets. 2840 * (RFC3542 leaves this case unspecified.) 2841 */ 2842 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 2843 } else 2844 opt->ip6po_flags |= IP6PO_DONTFRAG; 2845 break; 2846 2847 case IPV6_PREFER_TEMPADDR: 2848 if (len != sizeof(int)) 2849 return (EINVAL); 2850 preftemp = *(int *)buf; 2851 if (preftemp != IP6PO_TEMPADDR_SYSTEM && 2852 preftemp != IP6PO_TEMPADDR_NOTPREFER && 2853 preftemp != IP6PO_TEMPADDR_PREFER) { 2854 return (EINVAL); 2855 } 2856 opt->ip6po_prefer_tempaddr = preftemp; 2857 break; 2858 2859 default: 2860 return (ENOPROTOOPT); 2861 } /* end of switch */ 2862 2863 return (0); 2864 } 2865 2866 /* 2867 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 2868 * packet to the input queue of a specified interface. Note that this 2869 * calls the output routine of the loopback "driver", but with an interface 2870 * pointer that might NOT be &loif -- easier than replicating that code here. 2871 */ 2872 void 2873 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst) 2874 { 2875 struct mbuf *copym; 2876 struct ip6_hdr *ip6; 2877 2878 copym = m_copy(m, 0, M_COPYALL); 2879 if (copym == NULL) 2880 return; 2881 2882 /* 2883 * Make sure to deep-copy IPv6 header portion in case the data 2884 * is in an mbuf cluster, so that we can safely override the IPv6 2885 * header portion later. 2886 */ 2887 if (!M_WRITABLE(copym) || 2888 copym->m_len < sizeof(struct ip6_hdr)) { 2889 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 2890 if (copym == NULL) 2891 return; 2892 } 2893 2894 #ifdef DIAGNOSTIC 2895 if (copym->m_len < sizeof(*ip6)) { 2896 m_freem(copym); 2897 return; 2898 } 2899 #endif 2900 2901 ip6 = mtod(copym, struct ip6_hdr *); 2902 /* 2903 * clear embedded scope identifiers if necessary. 2904 * in6_clearscope will touch the addresses only when necessary. 2905 */ 2906 in6_clearscope(&ip6->ip6_src); 2907 in6_clearscope(&ip6->ip6_dst); 2908 2909 (void)if_simloop(ifp, copym, dst->sin6_family, 0); 2910 } 2911 2912 /* 2913 * Chop IPv6 header off from the payload. 2914 */ 2915 static int 2916 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 2917 { 2918 struct mbuf *mh; 2919 struct ip6_hdr *ip6; 2920 2921 ip6 = mtod(m, struct ip6_hdr *); 2922 if (m->m_len > sizeof(*ip6)) { 2923 mh = m_gethdr(M_NOWAIT, MT_DATA); 2924 if (mh == NULL) { 2925 m_freem(m); 2926 return ENOBUFS; 2927 } 2928 m_move_pkthdr(mh, m); 2929 M_ALIGN(mh, sizeof(*ip6)); 2930 m->m_len -= sizeof(*ip6); 2931 m->m_data += sizeof(*ip6); 2932 mh->m_next = m; 2933 m = mh; 2934 m->m_len = sizeof(*ip6); 2935 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 2936 } 2937 exthdrs->ip6e_ip6 = m; 2938 return 0; 2939 } 2940 2941 /* 2942 * Compute IPv6 extension header length. 2943 */ 2944 int 2945 ip6_optlen(struct inpcb *in6p) 2946 { 2947 int len; 2948 2949 if (!in6p->in6p_outputopts) 2950 return 0; 2951 2952 len = 0; 2953 #define elen(x) \ 2954 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 2955 2956 len += elen(in6p->in6p_outputopts->ip6po_hbh); 2957 if (in6p->in6p_outputopts->ip6po_rthdr) 2958 /* dest1 is valid with rthdr only */ 2959 len += elen(in6p->in6p_outputopts->ip6po_dest1); 2960 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 2961 len += elen(in6p->in6p_outputopts->ip6po_dest2); 2962 return len; 2963 #undef elen 2964 } 2965