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