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