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