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