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