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