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 /* 1068 * Release the route if using our private route, or if 1069 * (with flowtable) we don't have our own reference. 1070 */ 1071 if (ro == &ip6route || 1072 (ro != NULL && ro->ro_flags & RT_NORTREF)) 1073 RO_RTFREE(ro); 1074 return (error); 1075 1076 freehdrs: 1077 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1078 m_freem(exthdrs.ip6e_dest1); 1079 m_freem(exthdrs.ip6e_rthdr); 1080 m_freem(exthdrs.ip6e_dest2); 1081 /* FALLTHROUGH */ 1082 bad: 1083 if (m) 1084 m_freem(m); 1085 goto done; 1086 } 1087 1088 static int 1089 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen) 1090 { 1091 struct mbuf *m; 1092 1093 if (hlen > MCLBYTES) 1094 return (ENOBUFS); /* XXX */ 1095 1096 if (hlen > MLEN) 1097 m = m_getcl(M_NOWAIT, MT_DATA, 0); 1098 else 1099 m = m_get(M_NOWAIT, MT_DATA); 1100 if (m == NULL) 1101 return (ENOBUFS); 1102 m->m_len = hlen; 1103 if (hdr) 1104 bcopy(hdr, mtod(m, caddr_t), hlen); 1105 1106 *mp = m; 1107 return (0); 1108 } 1109 1110 /* 1111 * Insert jumbo payload option. 1112 */ 1113 static int 1114 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) 1115 { 1116 struct mbuf *mopt; 1117 u_char *optbuf; 1118 u_int32_t v; 1119 1120 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1121 1122 /* 1123 * If there is no hop-by-hop options header, allocate new one. 1124 * If there is one but it doesn't have enough space to store the 1125 * jumbo payload option, allocate a cluster to store the whole options. 1126 * Otherwise, use it to store the options. 1127 */ 1128 if (exthdrs->ip6e_hbh == NULL) { 1129 mopt = m_get(M_NOWAIT, MT_DATA); 1130 if (mopt == NULL) 1131 return (ENOBUFS); 1132 mopt->m_len = JUMBOOPTLEN; 1133 optbuf = mtod(mopt, u_char *); 1134 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1135 exthdrs->ip6e_hbh = mopt; 1136 } else { 1137 struct ip6_hbh *hbh; 1138 1139 mopt = exthdrs->ip6e_hbh; 1140 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1141 /* 1142 * XXX assumption: 1143 * - exthdrs->ip6e_hbh is not referenced from places 1144 * other than exthdrs. 1145 * - exthdrs->ip6e_hbh is not an mbuf chain. 1146 */ 1147 int oldoptlen = mopt->m_len; 1148 struct mbuf *n; 1149 1150 /* 1151 * XXX: give up if the whole (new) hbh header does 1152 * not fit even in an mbuf cluster. 1153 */ 1154 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1155 return (ENOBUFS); 1156 1157 /* 1158 * As a consequence, we must always prepare a cluster 1159 * at this point. 1160 */ 1161 n = m_getcl(M_NOWAIT, MT_DATA, 0); 1162 if (n == NULL) 1163 return (ENOBUFS); 1164 n->m_len = oldoptlen + JUMBOOPTLEN; 1165 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1166 oldoptlen); 1167 optbuf = mtod(n, caddr_t) + oldoptlen; 1168 m_freem(mopt); 1169 mopt = exthdrs->ip6e_hbh = n; 1170 } else { 1171 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1172 mopt->m_len += JUMBOOPTLEN; 1173 } 1174 optbuf[0] = IP6OPT_PADN; 1175 optbuf[1] = 1; 1176 1177 /* 1178 * Adjust the header length according to the pad and 1179 * the jumbo payload option. 1180 */ 1181 hbh = mtod(mopt, struct ip6_hbh *); 1182 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1183 } 1184 1185 /* fill in the option. */ 1186 optbuf[2] = IP6OPT_JUMBO; 1187 optbuf[3] = 4; 1188 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1189 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1190 1191 /* finally, adjust the packet header length */ 1192 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1193 1194 return (0); 1195 #undef JUMBOOPTLEN 1196 } 1197 1198 /* 1199 * Insert fragment header and copy unfragmentable header portions. 1200 */ 1201 static int 1202 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, 1203 struct ip6_frag **frghdrp) 1204 { 1205 struct mbuf *n, *mlast; 1206 1207 if (hlen > sizeof(struct ip6_hdr)) { 1208 n = m_copym(m0, sizeof(struct ip6_hdr), 1209 hlen - sizeof(struct ip6_hdr), M_NOWAIT); 1210 if (n == NULL) 1211 return (ENOBUFS); 1212 m->m_next = n; 1213 } else 1214 n = m; 1215 1216 /* Search for the last mbuf of unfragmentable part. */ 1217 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1218 ; 1219 1220 if (M_WRITABLE(mlast) && 1221 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1222 /* use the trailing space of the last mbuf for the fragment hdr */ 1223 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) + 1224 mlast->m_len); 1225 mlast->m_len += sizeof(struct ip6_frag); 1226 m->m_pkthdr.len += sizeof(struct ip6_frag); 1227 } else { 1228 /* allocate a new mbuf for the fragment header */ 1229 struct mbuf *mfrg; 1230 1231 mfrg = m_get(M_NOWAIT, MT_DATA); 1232 if (mfrg == NULL) 1233 return (ENOBUFS); 1234 mfrg->m_len = sizeof(struct ip6_frag); 1235 *frghdrp = mtod(mfrg, struct ip6_frag *); 1236 mlast->m_next = mfrg; 1237 } 1238 1239 return (0); 1240 } 1241 1242 /* 1243 * Calculates IPv6 path mtu for destination @dst. 1244 * Resulting MTU is stored in @mtup. 1245 * 1246 * Returns 0 on success. 1247 */ 1248 static int 1249 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup) 1250 { 1251 struct nhop6_extended nh6; 1252 struct in6_addr kdst; 1253 uint32_t scopeid; 1254 struct ifnet *ifp; 1255 u_long mtu; 1256 int error; 1257 1258 in6_splitscope(dst, &kdst, &scopeid); 1259 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0) 1260 return (EHOSTUNREACH); 1261 1262 ifp = nh6.nh_ifp; 1263 mtu = nh6.nh_mtu; 1264 1265 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0); 1266 fib6_free_nh_ext(fibnum, &nh6); 1267 1268 return (error); 1269 } 1270 1271 /* 1272 * Calculates IPv6 path MTU for @dst based on transmit @ifp, 1273 * and cached data in @ro_pmtu. 1274 * MTU from (successful) route lookup is saved (along with dst) 1275 * inside @ro_pmtu to avoid subsequent route lookups after packet 1276 * filter processing. 1277 * 1278 * Stores mtu and always-frag value into @mtup and @alwaysfragp. 1279 * Returns 0 on success. 1280 */ 1281 static int 1282 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup, 1283 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup, 1284 int *alwaysfragp, u_int fibnum, u_int proto) 1285 { 1286 struct nhop6_basic nh6; 1287 struct in6_addr kdst; 1288 uint32_t scopeid; 1289 struct sockaddr_in6 *sa6_dst; 1290 u_long mtu; 1291 1292 mtu = 0; 1293 if (do_lookup) { 1294 1295 /* 1296 * Here ro_pmtu has final destination address, while 1297 * ro might represent immediate destination. 1298 * Use ro_pmtu destination since mtu might differ. 1299 */ 1300 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 1301 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst)) 1302 ro_pmtu->ro_mtu = 0; 1303 1304 if (ro_pmtu->ro_mtu == 0) { 1305 bzero(sa6_dst, sizeof(*sa6_dst)); 1306 sa6_dst->sin6_family = AF_INET6; 1307 sa6_dst->sin6_len = sizeof(struct sockaddr_in6); 1308 sa6_dst->sin6_addr = *dst; 1309 1310 in6_splitscope(dst, &kdst, &scopeid); 1311 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0, 1312 &nh6) == 0) 1313 ro_pmtu->ro_mtu = nh6.nh_mtu; 1314 } 1315 1316 mtu = ro_pmtu->ro_mtu; 1317 } 1318 1319 if (ro_pmtu->ro_rt) 1320 mtu = ro_pmtu->ro_rt->rt_mtu; 1321 1322 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto)); 1323 } 1324 1325 /* 1326 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and 1327 * hostcache data for @dst. 1328 * Stores mtu and always-frag value into @mtup and @alwaysfragp. 1329 * 1330 * Returns 0 on success. 1331 */ 1332 static int 1333 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu, 1334 u_long *mtup, int *alwaysfragp, u_int proto) 1335 { 1336 u_long mtu = 0; 1337 int alwaysfrag = 0; 1338 int error = 0; 1339 1340 if (rt_mtu > 0) { 1341 u_int32_t ifmtu; 1342 struct in_conninfo inc; 1343 1344 bzero(&inc, sizeof(inc)); 1345 inc.inc_flags |= INC_ISIPV6; 1346 inc.inc6_faddr = *dst; 1347 1348 ifmtu = IN6_LINKMTU(ifp); 1349 1350 /* TCP is known to react to pmtu changes so skip hc */ 1351 if (proto != IPPROTO_TCP) 1352 mtu = tcp_hc_getmtu(&inc); 1353 1354 if (mtu) 1355 mtu = min(mtu, rt_mtu); 1356 else 1357 mtu = rt_mtu; 1358 if (mtu == 0) 1359 mtu = ifmtu; 1360 else if (mtu < IPV6_MMTU) { 1361 /* 1362 * RFC2460 section 5, last paragraph: 1363 * if we record ICMPv6 too big message with 1364 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU 1365 * or smaller, with framgent header attached. 1366 * (fragment header is needed regardless from the 1367 * packet size, for translators to identify packets) 1368 */ 1369 alwaysfrag = 1; 1370 mtu = IPV6_MMTU; 1371 } 1372 } else if (ifp) { 1373 mtu = IN6_LINKMTU(ifp); 1374 } else 1375 error = EHOSTUNREACH; /* XXX */ 1376 1377 *mtup = mtu; 1378 if (alwaysfragp) 1379 *alwaysfragp = alwaysfrag; 1380 return (error); 1381 } 1382 1383 /* 1384 * IP6 socket option processing. 1385 */ 1386 int 1387 ip6_ctloutput(struct socket *so, struct sockopt *sopt) 1388 { 1389 int optdatalen, uproto; 1390 void *optdata; 1391 struct inpcb *in6p = sotoinpcb(so); 1392 int error, optval; 1393 int level, op, optname; 1394 int optlen; 1395 struct thread *td; 1396 #ifdef RSS 1397 uint32_t rss_bucket; 1398 int retval; 1399 #endif 1400 1401 level = sopt->sopt_level; 1402 op = sopt->sopt_dir; 1403 optname = sopt->sopt_name; 1404 optlen = sopt->sopt_valsize; 1405 td = sopt->sopt_td; 1406 error = 0; 1407 optval = 0; 1408 uproto = (int)so->so_proto->pr_protocol; 1409 1410 if (level != IPPROTO_IPV6) { 1411 error = EINVAL; 1412 1413 if (sopt->sopt_level == SOL_SOCKET && 1414 sopt->sopt_dir == SOPT_SET) { 1415 switch (sopt->sopt_name) { 1416 case SO_REUSEADDR: 1417 INP_WLOCK(in6p); 1418 if ((so->so_options & SO_REUSEADDR) != 0) 1419 in6p->inp_flags2 |= INP_REUSEADDR; 1420 else 1421 in6p->inp_flags2 &= ~INP_REUSEADDR; 1422 INP_WUNLOCK(in6p); 1423 error = 0; 1424 break; 1425 case SO_REUSEPORT: 1426 INP_WLOCK(in6p); 1427 if ((so->so_options & SO_REUSEPORT) != 0) 1428 in6p->inp_flags2 |= INP_REUSEPORT; 1429 else 1430 in6p->inp_flags2 &= ~INP_REUSEPORT; 1431 INP_WUNLOCK(in6p); 1432 error = 0; 1433 break; 1434 case SO_SETFIB: 1435 INP_WLOCK(in6p); 1436 in6p->inp_inc.inc_fibnum = so->so_fibnum; 1437 INP_WUNLOCK(in6p); 1438 error = 0; 1439 break; 1440 default: 1441 break; 1442 } 1443 } 1444 } else { /* level == IPPROTO_IPV6 */ 1445 switch (op) { 1446 1447 case SOPT_SET: 1448 switch (optname) { 1449 case IPV6_2292PKTOPTIONS: 1450 #ifdef IPV6_PKTOPTIONS 1451 case IPV6_PKTOPTIONS: 1452 #endif 1453 { 1454 struct mbuf *m; 1455 1456 error = soopt_getm(sopt, &m); /* XXX */ 1457 if (error != 0) 1458 break; 1459 error = soopt_mcopyin(sopt, m); /* XXX */ 1460 if (error != 0) 1461 break; 1462 error = ip6_pcbopts(&in6p->in6p_outputopts, 1463 m, so, sopt); 1464 m_freem(m); /* XXX */ 1465 break; 1466 } 1467 1468 /* 1469 * Use of some Hop-by-Hop options or some 1470 * Destination options, might require special 1471 * privilege. That is, normal applications 1472 * (without special privilege) might be forbidden 1473 * from setting certain options in outgoing packets, 1474 * and might never see certain options in received 1475 * packets. [RFC 2292 Section 6] 1476 * KAME specific note: 1477 * KAME prevents non-privileged users from sending or 1478 * receiving ANY hbh/dst options in order to avoid 1479 * overhead of parsing options in the kernel. 1480 */ 1481 case IPV6_RECVHOPOPTS: 1482 case IPV6_RECVDSTOPTS: 1483 case IPV6_RECVRTHDRDSTOPTS: 1484 if (td != NULL) { 1485 error = priv_check(td, 1486 PRIV_NETINET_SETHDROPTS); 1487 if (error) 1488 break; 1489 } 1490 /* FALLTHROUGH */ 1491 case IPV6_UNICAST_HOPS: 1492 case IPV6_HOPLIMIT: 1493 1494 case IPV6_RECVPKTINFO: 1495 case IPV6_RECVHOPLIMIT: 1496 case IPV6_RECVRTHDR: 1497 case IPV6_RECVPATHMTU: 1498 case IPV6_RECVTCLASS: 1499 case IPV6_RECVFLOWID: 1500 #ifdef RSS 1501 case IPV6_RECVRSSBUCKETID: 1502 #endif 1503 case IPV6_V6ONLY: 1504 case IPV6_AUTOFLOWLABEL: 1505 case IPV6_BINDANY: 1506 case IPV6_BINDMULTI: 1507 #ifdef RSS 1508 case IPV6_RSS_LISTEN_BUCKET: 1509 #endif 1510 if (optname == IPV6_BINDANY && td != NULL) { 1511 error = priv_check(td, 1512 PRIV_NETINET_BINDANY); 1513 if (error) 1514 break; 1515 } 1516 1517 if (optlen != sizeof(int)) { 1518 error = EINVAL; 1519 break; 1520 } 1521 error = sooptcopyin(sopt, &optval, 1522 sizeof optval, sizeof optval); 1523 if (error) 1524 break; 1525 switch (optname) { 1526 1527 case IPV6_UNICAST_HOPS: 1528 if (optval < -1 || optval >= 256) 1529 error = EINVAL; 1530 else { 1531 /* -1 = kernel default */ 1532 in6p->in6p_hops = optval; 1533 if ((in6p->inp_vflag & 1534 INP_IPV4) != 0) 1535 in6p->inp_ip_ttl = optval; 1536 } 1537 break; 1538 #define OPTSET(bit) \ 1539 do { \ 1540 INP_WLOCK(in6p); \ 1541 if (optval) \ 1542 in6p->inp_flags |= (bit); \ 1543 else \ 1544 in6p->inp_flags &= ~(bit); \ 1545 INP_WUNLOCK(in6p); \ 1546 } while (/*CONSTCOND*/ 0) 1547 #define OPTSET2292(bit) \ 1548 do { \ 1549 INP_WLOCK(in6p); \ 1550 in6p->inp_flags |= IN6P_RFC2292; \ 1551 if (optval) \ 1552 in6p->inp_flags |= (bit); \ 1553 else \ 1554 in6p->inp_flags &= ~(bit); \ 1555 INP_WUNLOCK(in6p); \ 1556 } while (/*CONSTCOND*/ 0) 1557 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0) 1558 1559 #define OPTSET2(bit, val) do { \ 1560 INP_WLOCK(in6p); \ 1561 if (val) \ 1562 in6p->inp_flags2 |= bit; \ 1563 else \ 1564 in6p->inp_flags2 &= ~bit; \ 1565 INP_WUNLOCK(in6p); \ 1566 } while (0) 1567 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0) 1568 1569 case IPV6_RECVPKTINFO: 1570 /* cannot mix with RFC2292 */ 1571 if (OPTBIT(IN6P_RFC2292)) { 1572 error = EINVAL; 1573 break; 1574 } 1575 OPTSET(IN6P_PKTINFO); 1576 break; 1577 1578 case IPV6_HOPLIMIT: 1579 { 1580 struct ip6_pktopts **optp; 1581 1582 /* cannot mix with RFC2292 */ 1583 if (OPTBIT(IN6P_RFC2292)) { 1584 error = EINVAL; 1585 break; 1586 } 1587 optp = &in6p->in6p_outputopts; 1588 error = ip6_pcbopt(IPV6_HOPLIMIT, 1589 (u_char *)&optval, sizeof(optval), 1590 optp, (td != NULL) ? td->td_ucred : 1591 NULL, uproto); 1592 break; 1593 } 1594 1595 case IPV6_RECVHOPLIMIT: 1596 /* cannot mix with RFC2292 */ 1597 if (OPTBIT(IN6P_RFC2292)) { 1598 error = EINVAL; 1599 break; 1600 } 1601 OPTSET(IN6P_HOPLIMIT); 1602 break; 1603 1604 case IPV6_RECVHOPOPTS: 1605 /* cannot mix with RFC2292 */ 1606 if (OPTBIT(IN6P_RFC2292)) { 1607 error = EINVAL; 1608 break; 1609 } 1610 OPTSET(IN6P_HOPOPTS); 1611 break; 1612 1613 case IPV6_RECVDSTOPTS: 1614 /* cannot mix with RFC2292 */ 1615 if (OPTBIT(IN6P_RFC2292)) { 1616 error = EINVAL; 1617 break; 1618 } 1619 OPTSET(IN6P_DSTOPTS); 1620 break; 1621 1622 case IPV6_RECVRTHDRDSTOPTS: 1623 /* cannot mix with RFC2292 */ 1624 if (OPTBIT(IN6P_RFC2292)) { 1625 error = EINVAL; 1626 break; 1627 } 1628 OPTSET(IN6P_RTHDRDSTOPTS); 1629 break; 1630 1631 case IPV6_RECVRTHDR: 1632 /* cannot mix with RFC2292 */ 1633 if (OPTBIT(IN6P_RFC2292)) { 1634 error = EINVAL; 1635 break; 1636 } 1637 OPTSET(IN6P_RTHDR); 1638 break; 1639 1640 case IPV6_RECVPATHMTU: 1641 /* 1642 * We ignore this option for TCP 1643 * sockets. 1644 * (RFC3542 leaves this case 1645 * unspecified.) 1646 */ 1647 if (uproto != IPPROTO_TCP) 1648 OPTSET(IN6P_MTU); 1649 break; 1650 1651 case IPV6_RECVFLOWID: 1652 OPTSET2(INP_RECVFLOWID, optval); 1653 break; 1654 1655 #ifdef RSS 1656 case IPV6_RECVRSSBUCKETID: 1657 OPTSET2(INP_RECVRSSBUCKETID, optval); 1658 break; 1659 #endif 1660 1661 case IPV6_V6ONLY: 1662 /* 1663 * make setsockopt(IPV6_V6ONLY) 1664 * available only prior to bind(2). 1665 * see ipng mailing list, Jun 22 2001. 1666 */ 1667 if (in6p->inp_lport || 1668 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1669 error = EINVAL; 1670 break; 1671 } 1672 OPTSET(IN6P_IPV6_V6ONLY); 1673 if (optval) 1674 in6p->inp_vflag &= ~INP_IPV4; 1675 else 1676 in6p->inp_vflag |= INP_IPV4; 1677 break; 1678 case IPV6_RECVTCLASS: 1679 /* cannot mix with RFC2292 XXX */ 1680 if (OPTBIT(IN6P_RFC2292)) { 1681 error = EINVAL; 1682 break; 1683 } 1684 OPTSET(IN6P_TCLASS); 1685 break; 1686 case IPV6_AUTOFLOWLABEL: 1687 OPTSET(IN6P_AUTOFLOWLABEL); 1688 break; 1689 1690 case IPV6_BINDANY: 1691 OPTSET(INP_BINDANY); 1692 break; 1693 1694 case IPV6_BINDMULTI: 1695 OPTSET2(INP_BINDMULTI, optval); 1696 break; 1697 #ifdef RSS 1698 case IPV6_RSS_LISTEN_BUCKET: 1699 if ((optval >= 0) && 1700 (optval < rss_getnumbuckets())) { 1701 in6p->inp_rss_listen_bucket = optval; 1702 OPTSET2(INP_RSS_BUCKET_SET, 1); 1703 } else { 1704 error = EINVAL; 1705 } 1706 break; 1707 #endif 1708 } 1709 break; 1710 1711 case IPV6_TCLASS: 1712 case IPV6_DONTFRAG: 1713 case IPV6_USE_MIN_MTU: 1714 case IPV6_PREFER_TEMPADDR: 1715 if (optlen != sizeof(optval)) { 1716 error = EINVAL; 1717 break; 1718 } 1719 error = sooptcopyin(sopt, &optval, 1720 sizeof optval, sizeof optval); 1721 if (error) 1722 break; 1723 { 1724 struct ip6_pktopts **optp; 1725 optp = &in6p->in6p_outputopts; 1726 error = ip6_pcbopt(optname, 1727 (u_char *)&optval, sizeof(optval), 1728 optp, (td != NULL) ? td->td_ucred : 1729 NULL, uproto); 1730 break; 1731 } 1732 1733 case IPV6_2292PKTINFO: 1734 case IPV6_2292HOPLIMIT: 1735 case IPV6_2292HOPOPTS: 1736 case IPV6_2292DSTOPTS: 1737 case IPV6_2292RTHDR: 1738 /* RFC 2292 */ 1739 if (optlen != sizeof(int)) { 1740 error = EINVAL; 1741 break; 1742 } 1743 error = sooptcopyin(sopt, &optval, 1744 sizeof optval, sizeof optval); 1745 if (error) 1746 break; 1747 switch (optname) { 1748 case IPV6_2292PKTINFO: 1749 OPTSET2292(IN6P_PKTINFO); 1750 break; 1751 case IPV6_2292HOPLIMIT: 1752 OPTSET2292(IN6P_HOPLIMIT); 1753 break; 1754 case IPV6_2292HOPOPTS: 1755 /* 1756 * Check super-user privilege. 1757 * See comments for IPV6_RECVHOPOPTS. 1758 */ 1759 if (td != NULL) { 1760 error = priv_check(td, 1761 PRIV_NETINET_SETHDROPTS); 1762 if (error) 1763 return (error); 1764 } 1765 OPTSET2292(IN6P_HOPOPTS); 1766 break; 1767 case IPV6_2292DSTOPTS: 1768 if (td != NULL) { 1769 error = priv_check(td, 1770 PRIV_NETINET_SETHDROPTS); 1771 if (error) 1772 return (error); 1773 } 1774 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1775 break; 1776 case IPV6_2292RTHDR: 1777 OPTSET2292(IN6P_RTHDR); 1778 break; 1779 } 1780 break; 1781 case IPV6_PKTINFO: 1782 case IPV6_HOPOPTS: 1783 case IPV6_RTHDR: 1784 case IPV6_DSTOPTS: 1785 case IPV6_RTHDRDSTOPTS: 1786 case IPV6_NEXTHOP: 1787 { 1788 /* new advanced API (RFC3542) */ 1789 u_char *optbuf; 1790 u_char optbuf_storage[MCLBYTES]; 1791 int optlen; 1792 struct ip6_pktopts **optp; 1793 1794 /* cannot mix with RFC2292 */ 1795 if (OPTBIT(IN6P_RFC2292)) { 1796 error = EINVAL; 1797 break; 1798 } 1799 1800 /* 1801 * We only ensure valsize is not too large 1802 * here. Further validation will be done 1803 * later. 1804 */ 1805 error = sooptcopyin(sopt, optbuf_storage, 1806 sizeof(optbuf_storage), 0); 1807 if (error) 1808 break; 1809 optlen = sopt->sopt_valsize; 1810 optbuf = optbuf_storage; 1811 optp = &in6p->in6p_outputopts; 1812 error = ip6_pcbopt(optname, optbuf, optlen, 1813 optp, (td != NULL) ? td->td_ucred : NULL, 1814 uproto); 1815 break; 1816 } 1817 #undef OPTSET 1818 1819 case IPV6_MULTICAST_IF: 1820 case IPV6_MULTICAST_HOPS: 1821 case IPV6_MULTICAST_LOOP: 1822 case IPV6_JOIN_GROUP: 1823 case IPV6_LEAVE_GROUP: 1824 case IPV6_MSFILTER: 1825 case MCAST_BLOCK_SOURCE: 1826 case MCAST_UNBLOCK_SOURCE: 1827 case MCAST_JOIN_GROUP: 1828 case MCAST_LEAVE_GROUP: 1829 case MCAST_JOIN_SOURCE_GROUP: 1830 case MCAST_LEAVE_SOURCE_GROUP: 1831 error = ip6_setmoptions(in6p, sopt); 1832 break; 1833 1834 case IPV6_PORTRANGE: 1835 error = sooptcopyin(sopt, &optval, 1836 sizeof optval, sizeof optval); 1837 if (error) 1838 break; 1839 1840 INP_WLOCK(in6p); 1841 switch (optval) { 1842 case IPV6_PORTRANGE_DEFAULT: 1843 in6p->inp_flags &= ~(INP_LOWPORT); 1844 in6p->inp_flags &= ~(INP_HIGHPORT); 1845 break; 1846 1847 case IPV6_PORTRANGE_HIGH: 1848 in6p->inp_flags &= ~(INP_LOWPORT); 1849 in6p->inp_flags |= INP_HIGHPORT; 1850 break; 1851 1852 case IPV6_PORTRANGE_LOW: 1853 in6p->inp_flags &= ~(INP_HIGHPORT); 1854 in6p->inp_flags |= INP_LOWPORT; 1855 break; 1856 1857 default: 1858 error = EINVAL; 1859 break; 1860 } 1861 INP_WUNLOCK(in6p); 1862 break; 1863 1864 #ifdef IPSEC 1865 case IPV6_IPSEC_POLICY: 1866 { 1867 caddr_t req; 1868 struct mbuf *m; 1869 1870 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1871 break; 1872 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1873 break; 1874 req = mtod(m, caddr_t); 1875 error = ipsec_set_policy(in6p, optname, req, 1876 m->m_len, (sopt->sopt_td != NULL) ? 1877 sopt->sopt_td->td_ucred : NULL); 1878 m_freem(m); 1879 break; 1880 } 1881 #endif /* IPSEC */ 1882 1883 default: 1884 error = ENOPROTOOPT; 1885 break; 1886 } 1887 break; 1888 1889 case SOPT_GET: 1890 switch (optname) { 1891 1892 case IPV6_2292PKTOPTIONS: 1893 #ifdef IPV6_PKTOPTIONS 1894 case IPV6_PKTOPTIONS: 1895 #endif 1896 /* 1897 * RFC3542 (effectively) deprecated the 1898 * semantics of the 2292-style pktoptions. 1899 * Since it was not reliable in nature (i.e., 1900 * applications had to expect the lack of some 1901 * information after all), it would make sense 1902 * to simplify this part by always returning 1903 * empty data. 1904 */ 1905 sopt->sopt_valsize = 0; 1906 break; 1907 1908 case IPV6_RECVHOPOPTS: 1909 case IPV6_RECVDSTOPTS: 1910 case IPV6_RECVRTHDRDSTOPTS: 1911 case IPV6_UNICAST_HOPS: 1912 case IPV6_RECVPKTINFO: 1913 case IPV6_RECVHOPLIMIT: 1914 case IPV6_RECVRTHDR: 1915 case IPV6_RECVPATHMTU: 1916 1917 case IPV6_V6ONLY: 1918 case IPV6_PORTRANGE: 1919 case IPV6_RECVTCLASS: 1920 case IPV6_AUTOFLOWLABEL: 1921 case IPV6_BINDANY: 1922 case IPV6_FLOWID: 1923 case IPV6_FLOWTYPE: 1924 case IPV6_RECVFLOWID: 1925 #ifdef RSS 1926 case IPV6_RSSBUCKETID: 1927 case IPV6_RECVRSSBUCKETID: 1928 #endif 1929 case IPV6_BINDMULTI: 1930 switch (optname) { 1931 1932 case IPV6_RECVHOPOPTS: 1933 optval = OPTBIT(IN6P_HOPOPTS); 1934 break; 1935 1936 case IPV6_RECVDSTOPTS: 1937 optval = OPTBIT(IN6P_DSTOPTS); 1938 break; 1939 1940 case IPV6_RECVRTHDRDSTOPTS: 1941 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 1942 break; 1943 1944 case IPV6_UNICAST_HOPS: 1945 optval = in6p->in6p_hops; 1946 break; 1947 1948 case IPV6_RECVPKTINFO: 1949 optval = OPTBIT(IN6P_PKTINFO); 1950 break; 1951 1952 case IPV6_RECVHOPLIMIT: 1953 optval = OPTBIT(IN6P_HOPLIMIT); 1954 break; 1955 1956 case IPV6_RECVRTHDR: 1957 optval = OPTBIT(IN6P_RTHDR); 1958 break; 1959 1960 case IPV6_RECVPATHMTU: 1961 optval = OPTBIT(IN6P_MTU); 1962 break; 1963 1964 case IPV6_V6ONLY: 1965 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1966 break; 1967 1968 case IPV6_PORTRANGE: 1969 { 1970 int flags; 1971 flags = in6p->inp_flags; 1972 if (flags & INP_HIGHPORT) 1973 optval = IPV6_PORTRANGE_HIGH; 1974 else if (flags & INP_LOWPORT) 1975 optval = IPV6_PORTRANGE_LOW; 1976 else 1977 optval = 0; 1978 break; 1979 } 1980 case IPV6_RECVTCLASS: 1981 optval = OPTBIT(IN6P_TCLASS); 1982 break; 1983 1984 case IPV6_AUTOFLOWLABEL: 1985 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 1986 break; 1987 1988 case IPV6_BINDANY: 1989 optval = OPTBIT(INP_BINDANY); 1990 break; 1991 1992 case IPV6_FLOWID: 1993 optval = in6p->inp_flowid; 1994 break; 1995 1996 case IPV6_FLOWTYPE: 1997 optval = in6p->inp_flowtype; 1998 break; 1999 2000 case IPV6_RECVFLOWID: 2001 optval = OPTBIT2(INP_RECVFLOWID); 2002 break; 2003 #ifdef RSS 2004 case IPV6_RSSBUCKETID: 2005 retval = 2006 rss_hash2bucket(in6p->inp_flowid, 2007 in6p->inp_flowtype, 2008 &rss_bucket); 2009 if (retval == 0) 2010 optval = rss_bucket; 2011 else 2012 error = EINVAL; 2013 break; 2014 2015 case IPV6_RECVRSSBUCKETID: 2016 optval = OPTBIT2(INP_RECVRSSBUCKETID); 2017 break; 2018 #endif 2019 2020 case IPV6_BINDMULTI: 2021 optval = OPTBIT2(INP_BINDMULTI); 2022 break; 2023 2024 } 2025 if (error) 2026 break; 2027 error = sooptcopyout(sopt, &optval, 2028 sizeof optval); 2029 break; 2030 2031 case IPV6_PATHMTU: 2032 { 2033 u_long pmtu = 0; 2034 struct ip6_mtuinfo mtuinfo; 2035 2036 if (!(so->so_state & SS_ISCONNECTED)) 2037 return (ENOTCONN); 2038 /* 2039 * XXX: we dot not consider the case of source 2040 * routing, or optional information to specify 2041 * the outgoing interface. 2042 */ 2043 error = ip6_getpmtu_ctl(so->so_fibnum, 2044 &in6p->in6p_faddr, &pmtu); 2045 if (error) 2046 break; 2047 if (pmtu > IPV6_MAXPACKET) 2048 pmtu = IPV6_MAXPACKET; 2049 2050 bzero(&mtuinfo, sizeof(mtuinfo)); 2051 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 2052 optdata = (void *)&mtuinfo; 2053 optdatalen = sizeof(mtuinfo); 2054 error = sooptcopyout(sopt, optdata, 2055 optdatalen); 2056 break; 2057 } 2058 2059 case IPV6_2292PKTINFO: 2060 case IPV6_2292HOPLIMIT: 2061 case IPV6_2292HOPOPTS: 2062 case IPV6_2292RTHDR: 2063 case IPV6_2292DSTOPTS: 2064 switch (optname) { 2065 case IPV6_2292PKTINFO: 2066 optval = OPTBIT(IN6P_PKTINFO); 2067 break; 2068 case IPV6_2292HOPLIMIT: 2069 optval = OPTBIT(IN6P_HOPLIMIT); 2070 break; 2071 case IPV6_2292HOPOPTS: 2072 optval = OPTBIT(IN6P_HOPOPTS); 2073 break; 2074 case IPV6_2292RTHDR: 2075 optval = OPTBIT(IN6P_RTHDR); 2076 break; 2077 case IPV6_2292DSTOPTS: 2078 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 2079 break; 2080 } 2081 error = sooptcopyout(sopt, &optval, 2082 sizeof optval); 2083 break; 2084 case IPV6_PKTINFO: 2085 case IPV6_HOPOPTS: 2086 case IPV6_RTHDR: 2087 case IPV6_DSTOPTS: 2088 case IPV6_RTHDRDSTOPTS: 2089 case IPV6_NEXTHOP: 2090 case IPV6_TCLASS: 2091 case IPV6_DONTFRAG: 2092 case IPV6_USE_MIN_MTU: 2093 case IPV6_PREFER_TEMPADDR: 2094 error = ip6_getpcbopt(in6p->in6p_outputopts, 2095 optname, sopt); 2096 break; 2097 2098 case IPV6_MULTICAST_IF: 2099 case IPV6_MULTICAST_HOPS: 2100 case IPV6_MULTICAST_LOOP: 2101 case IPV6_MSFILTER: 2102 error = ip6_getmoptions(in6p, sopt); 2103 break; 2104 2105 #ifdef IPSEC 2106 case IPV6_IPSEC_POLICY: 2107 { 2108 caddr_t req = NULL; 2109 size_t len = 0; 2110 struct mbuf *m = NULL; 2111 struct mbuf **mp = &m; 2112 size_t ovalsize = sopt->sopt_valsize; 2113 caddr_t oval = (caddr_t)sopt->sopt_val; 2114 2115 error = soopt_getm(sopt, &m); /* XXX */ 2116 if (error != 0) 2117 break; 2118 error = soopt_mcopyin(sopt, m); /* XXX */ 2119 if (error != 0) 2120 break; 2121 sopt->sopt_valsize = ovalsize; 2122 sopt->sopt_val = oval; 2123 if (m) { 2124 req = mtod(m, caddr_t); 2125 len = m->m_len; 2126 } 2127 error = ipsec_get_policy(in6p, req, len, mp); 2128 if (error == 0) 2129 error = soopt_mcopyout(sopt, m); /* XXX */ 2130 if (error == 0 && m) 2131 m_freem(m); 2132 break; 2133 } 2134 #endif /* IPSEC */ 2135 2136 default: 2137 error = ENOPROTOOPT; 2138 break; 2139 } 2140 break; 2141 } 2142 } 2143 return (error); 2144 } 2145 2146 int 2147 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt) 2148 { 2149 int error = 0, optval, optlen; 2150 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 2151 struct inpcb *in6p = sotoinpcb(so); 2152 int level, op, optname; 2153 2154 level = sopt->sopt_level; 2155 op = sopt->sopt_dir; 2156 optname = sopt->sopt_name; 2157 optlen = sopt->sopt_valsize; 2158 2159 if (level != IPPROTO_IPV6) { 2160 return (EINVAL); 2161 } 2162 2163 switch (optname) { 2164 case IPV6_CHECKSUM: 2165 /* 2166 * For ICMPv6 sockets, no modification allowed for checksum 2167 * offset, permit "no change" values to help existing apps. 2168 * 2169 * RFC3542 says: "An attempt to set IPV6_CHECKSUM 2170 * for an ICMPv6 socket will fail." 2171 * The current behavior does not meet RFC3542. 2172 */ 2173 switch (op) { 2174 case SOPT_SET: 2175 if (optlen != sizeof(int)) { 2176 error = EINVAL; 2177 break; 2178 } 2179 error = sooptcopyin(sopt, &optval, sizeof(optval), 2180 sizeof(optval)); 2181 if (error) 2182 break; 2183 if ((optval % 2) != 0) { 2184 /* the API assumes even offset values */ 2185 error = EINVAL; 2186 } else if (so->so_proto->pr_protocol == 2187 IPPROTO_ICMPV6) { 2188 if (optval != icmp6off) 2189 error = EINVAL; 2190 } else 2191 in6p->in6p_cksum = optval; 2192 break; 2193 2194 case SOPT_GET: 2195 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 2196 optval = icmp6off; 2197 else 2198 optval = in6p->in6p_cksum; 2199 2200 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2201 break; 2202 2203 default: 2204 error = EINVAL; 2205 break; 2206 } 2207 break; 2208 2209 default: 2210 error = ENOPROTOOPT; 2211 break; 2212 } 2213 2214 return (error); 2215 } 2216 2217 /* 2218 * Set up IP6 options in pcb for insertion in output packets or 2219 * specifying behavior of outgoing packets. 2220 */ 2221 static int 2222 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, 2223 struct socket *so, struct sockopt *sopt) 2224 { 2225 struct ip6_pktopts *opt = *pktopt; 2226 int error = 0; 2227 struct thread *td = sopt->sopt_td; 2228 2229 /* turn off any old options. */ 2230 if (opt) { 2231 #ifdef DIAGNOSTIC 2232 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 2233 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 2234 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 2235 printf("ip6_pcbopts: all specified options are cleared.\n"); 2236 #endif 2237 ip6_clearpktopts(opt, -1); 2238 } else 2239 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 2240 *pktopt = NULL; 2241 2242 if (!m || m->m_len == 0) { 2243 /* 2244 * Only turning off any previous options, regardless of 2245 * whether the opt is just created or given. 2246 */ 2247 free(opt, M_IP6OPT); 2248 return (0); 2249 } 2250 2251 /* set options specified by user. */ 2252 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ? 2253 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) { 2254 ip6_clearpktopts(opt, -1); /* XXX: discard all options */ 2255 free(opt, M_IP6OPT); 2256 return (error); 2257 } 2258 *pktopt = opt; 2259 return (0); 2260 } 2261 2262 /* 2263 * initialize ip6_pktopts. beware that there are non-zero default values in 2264 * the struct. 2265 */ 2266 void 2267 ip6_initpktopts(struct ip6_pktopts *opt) 2268 { 2269 2270 bzero(opt, sizeof(*opt)); 2271 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 2272 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 2273 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 2274 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; 2275 } 2276 2277 static int 2278 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, 2279 struct ucred *cred, int uproto) 2280 { 2281 struct ip6_pktopts *opt; 2282 2283 if (*pktopt == NULL) { 2284 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 2285 M_WAITOK); 2286 ip6_initpktopts(*pktopt); 2287 } 2288 opt = *pktopt; 2289 2290 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto)); 2291 } 2292 2293 static int 2294 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt) 2295 { 2296 void *optdata = NULL; 2297 int optdatalen = 0; 2298 struct ip6_ext *ip6e; 2299 int error = 0; 2300 struct in6_pktinfo null_pktinfo; 2301 int deftclass = 0, on; 2302 int defminmtu = IP6PO_MINMTU_MCASTONLY; 2303 int defpreftemp = IP6PO_TEMPADDR_SYSTEM; 2304 2305 switch (optname) { 2306 case IPV6_PKTINFO: 2307 optdata = (void *)&null_pktinfo; 2308 if (pktopt && pktopt->ip6po_pktinfo) { 2309 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo, 2310 sizeof(null_pktinfo)); 2311 in6_clearscope(&null_pktinfo.ipi6_addr); 2312 } else { 2313 /* XXX: we don't have to do this every time... */ 2314 bzero(&null_pktinfo, sizeof(null_pktinfo)); 2315 } 2316 optdatalen = sizeof(struct in6_pktinfo); 2317 break; 2318 case IPV6_TCLASS: 2319 if (pktopt && pktopt->ip6po_tclass >= 0) 2320 optdata = (void *)&pktopt->ip6po_tclass; 2321 else 2322 optdata = (void *)&deftclass; 2323 optdatalen = sizeof(int); 2324 break; 2325 case IPV6_HOPOPTS: 2326 if (pktopt && pktopt->ip6po_hbh) { 2327 optdata = (void *)pktopt->ip6po_hbh; 2328 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; 2329 optdatalen = (ip6e->ip6e_len + 1) << 3; 2330 } 2331 break; 2332 case IPV6_RTHDR: 2333 if (pktopt && pktopt->ip6po_rthdr) { 2334 optdata = (void *)pktopt->ip6po_rthdr; 2335 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; 2336 optdatalen = (ip6e->ip6e_len + 1) << 3; 2337 } 2338 break; 2339 case IPV6_RTHDRDSTOPTS: 2340 if (pktopt && pktopt->ip6po_dest1) { 2341 optdata = (void *)pktopt->ip6po_dest1; 2342 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; 2343 optdatalen = (ip6e->ip6e_len + 1) << 3; 2344 } 2345 break; 2346 case IPV6_DSTOPTS: 2347 if (pktopt && pktopt->ip6po_dest2) { 2348 optdata = (void *)pktopt->ip6po_dest2; 2349 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; 2350 optdatalen = (ip6e->ip6e_len + 1) << 3; 2351 } 2352 break; 2353 case IPV6_NEXTHOP: 2354 if (pktopt && pktopt->ip6po_nexthop) { 2355 optdata = (void *)pktopt->ip6po_nexthop; 2356 optdatalen = pktopt->ip6po_nexthop->sa_len; 2357 } 2358 break; 2359 case IPV6_USE_MIN_MTU: 2360 if (pktopt) 2361 optdata = (void *)&pktopt->ip6po_minmtu; 2362 else 2363 optdata = (void *)&defminmtu; 2364 optdatalen = sizeof(int); 2365 break; 2366 case IPV6_DONTFRAG: 2367 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 2368 on = 1; 2369 else 2370 on = 0; 2371 optdata = (void *)&on; 2372 optdatalen = sizeof(on); 2373 break; 2374 case IPV6_PREFER_TEMPADDR: 2375 if (pktopt) 2376 optdata = (void *)&pktopt->ip6po_prefer_tempaddr; 2377 else 2378 optdata = (void *)&defpreftemp; 2379 optdatalen = sizeof(int); 2380 break; 2381 default: /* should not happen */ 2382 #ifdef DIAGNOSTIC 2383 panic("ip6_getpcbopt: unexpected option\n"); 2384 #endif 2385 return (ENOPROTOOPT); 2386 } 2387 2388 error = sooptcopyout(sopt, optdata, optdatalen); 2389 2390 return (error); 2391 } 2392 2393 void 2394 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) 2395 { 2396 if (pktopt == NULL) 2397 return; 2398 2399 if (optname == -1 || optname == IPV6_PKTINFO) { 2400 if (pktopt->ip6po_pktinfo) 2401 free(pktopt->ip6po_pktinfo, M_IP6OPT); 2402 pktopt->ip6po_pktinfo = NULL; 2403 } 2404 if (optname == -1 || optname == IPV6_HOPLIMIT) 2405 pktopt->ip6po_hlim = -1; 2406 if (optname == -1 || optname == IPV6_TCLASS) 2407 pktopt->ip6po_tclass = -1; 2408 if (optname == -1 || optname == IPV6_NEXTHOP) { 2409 if (pktopt->ip6po_nextroute.ro_rt) { 2410 RTFREE(pktopt->ip6po_nextroute.ro_rt); 2411 pktopt->ip6po_nextroute.ro_rt = NULL; 2412 } 2413 if (pktopt->ip6po_nexthop) 2414 free(pktopt->ip6po_nexthop, M_IP6OPT); 2415 pktopt->ip6po_nexthop = NULL; 2416 } 2417 if (optname == -1 || optname == IPV6_HOPOPTS) { 2418 if (pktopt->ip6po_hbh) 2419 free(pktopt->ip6po_hbh, M_IP6OPT); 2420 pktopt->ip6po_hbh = NULL; 2421 } 2422 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 2423 if (pktopt->ip6po_dest1) 2424 free(pktopt->ip6po_dest1, M_IP6OPT); 2425 pktopt->ip6po_dest1 = NULL; 2426 } 2427 if (optname == -1 || optname == IPV6_RTHDR) { 2428 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 2429 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 2430 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 2431 if (pktopt->ip6po_route.ro_rt) { 2432 RTFREE(pktopt->ip6po_route.ro_rt); 2433 pktopt->ip6po_route.ro_rt = NULL; 2434 } 2435 } 2436 if (optname == -1 || optname == IPV6_DSTOPTS) { 2437 if (pktopt->ip6po_dest2) 2438 free(pktopt->ip6po_dest2, M_IP6OPT); 2439 pktopt->ip6po_dest2 = NULL; 2440 } 2441 } 2442 2443 #define PKTOPT_EXTHDRCPY(type) \ 2444 do {\ 2445 if (src->type) {\ 2446 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 2447 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 2448 if (dst->type == NULL && canwait == M_NOWAIT)\ 2449 goto bad;\ 2450 bcopy(src->type, dst->type, hlen);\ 2451 }\ 2452 } while (/*CONSTCOND*/ 0) 2453 2454 static int 2455 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) 2456 { 2457 if (dst == NULL || src == NULL) { 2458 printf("ip6_clearpktopts: invalid argument\n"); 2459 return (EINVAL); 2460 } 2461 2462 dst->ip6po_hlim = src->ip6po_hlim; 2463 dst->ip6po_tclass = src->ip6po_tclass; 2464 dst->ip6po_flags = src->ip6po_flags; 2465 dst->ip6po_minmtu = src->ip6po_minmtu; 2466 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr; 2467 if (src->ip6po_pktinfo) { 2468 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2469 M_IP6OPT, canwait); 2470 if (dst->ip6po_pktinfo == NULL) 2471 goto bad; 2472 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2473 } 2474 if (src->ip6po_nexthop) { 2475 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2476 M_IP6OPT, canwait); 2477 if (dst->ip6po_nexthop == NULL) 2478 goto bad; 2479 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 2480 src->ip6po_nexthop->sa_len); 2481 } 2482 PKTOPT_EXTHDRCPY(ip6po_hbh); 2483 PKTOPT_EXTHDRCPY(ip6po_dest1); 2484 PKTOPT_EXTHDRCPY(ip6po_dest2); 2485 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2486 return (0); 2487 2488 bad: 2489 ip6_clearpktopts(dst, -1); 2490 return (ENOBUFS); 2491 } 2492 #undef PKTOPT_EXTHDRCPY 2493 2494 struct ip6_pktopts * 2495 ip6_copypktopts(struct ip6_pktopts *src, int canwait) 2496 { 2497 int error; 2498 struct ip6_pktopts *dst; 2499 2500 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2501 if (dst == NULL) 2502 return (NULL); 2503 ip6_initpktopts(dst); 2504 2505 if ((error = copypktopts(dst, src, canwait)) != 0) { 2506 free(dst, M_IP6OPT); 2507 return (NULL); 2508 } 2509 2510 return (dst); 2511 } 2512 2513 void 2514 ip6_freepcbopts(struct ip6_pktopts *pktopt) 2515 { 2516 if (pktopt == NULL) 2517 return; 2518 2519 ip6_clearpktopts(pktopt, -1); 2520 2521 free(pktopt, M_IP6OPT); 2522 } 2523 2524 /* 2525 * Set IPv6 outgoing packet options based on advanced API. 2526 */ 2527 int 2528 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2529 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto) 2530 { 2531 struct cmsghdr *cm = NULL; 2532 2533 if (control == NULL || opt == NULL) 2534 return (EINVAL); 2535 2536 ip6_initpktopts(opt); 2537 if (stickyopt) { 2538 int error; 2539 2540 /* 2541 * If stickyopt is provided, make a local copy of the options 2542 * for this particular packet, then override them by ancillary 2543 * objects. 2544 * XXX: copypktopts() does not copy the cached route to a next 2545 * hop (if any). This is not very good in terms of efficiency, 2546 * but we can allow this since this option should be rarely 2547 * used. 2548 */ 2549 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2550 return (error); 2551 } 2552 2553 /* 2554 * XXX: Currently, we assume all the optional information is stored 2555 * in a single mbuf. 2556 */ 2557 if (control->m_next) 2558 return (EINVAL); 2559 2560 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2561 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2562 int error; 2563 2564 if (control->m_len < CMSG_LEN(0)) 2565 return (EINVAL); 2566 2567 cm = mtod(control, struct cmsghdr *); 2568 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2569 return (EINVAL); 2570 if (cm->cmsg_level != IPPROTO_IPV6) 2571 continue; 2572 2573 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2574 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); 2575 if (error) 2576 return (error); 2577 } 2578 2579 return (0); 2580 } 2581 2582 /* 2583 * Set a particular packet option, as a sticky option or an ancillary data 2584 * item. "len" can be 0 only when it's a sticky option. 2585 * We have 4 cases of combination of "sticky" and "cmsg": 2586 * "sticky=0, cmsg=0": impossible 2587 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2588 * "sticky=1, cmsg=0": RFC3542 socket option 2589 * "sticky=1, cmsg=1": RFC2292 socket option 2590 */ 2591 static int 2592 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2593 struct ucred *cred, int sticky, int cmsg, int uproto) 2594 { 2595 int minmtupolicy, preftemp; 2596 int error; 2597 2598 if (!sticky && !cmsg) { 2599 #ifdef DIAGNOSTIC 2600 printf("ip6_setpktopt: impossible case\n"); 2601 #endif 2602 return (EINVAL); 2603 } 2604 2605 /* 2606 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2607 * not be specified in the context of RFC3542. Conversely, 2608 * RFC3542 types should not be specified in the context of RFC2292. 2609 */ 2610 if (!cmsg) { 2611 switch (optname) { 2612 case IPV6_2292PKTINFO: 2613 case IPV6_2292HOPLIMIT: 2614 case IPV6_2292NEXTHOP: 2615 case IPV6_2292HOPOPTS: 2616 case IPV6_2292DSTOPTS: 2617 case IPV6_2292RTHDR: 2618 case IPV6_2292PKTOPTIONS: 2619 return (ENOPROTOOPT); 2620 } 2621 } 2622 if (sticky && cmsg) { 2623 switch (optname) { 2624 case IPV6_PKTINFO: 2625 case IPV6_HOPLIMIT: 2626 case IPV6_NEXTHOP: 2627 case IPV6_HOPOPTS: 2628 case IPV6_DSTOPTS: 2629 case IPV6_RTHDRDSTOPTS: 2630 case IPV6_RTHDR: 2631 case IPV6_USE_MIN_MTU: 2632 case IPV6_DONTFRAG: 2633 case IPV6_TCLASS: 2634 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */ 2635 return (ENOPROTOOPT); 2636 } 2637 } 2638 2639 switch (optname) { 2640 case IPV6_2292PKTINFO: 2641 case IPV6_PKTINFO: 2642 { 2643 struct ifnet *ifp = NULL; 2644 struct in6_pktinfo *pktinfo; 2645 2646 if (len != sizeof(struct in6_pktinfo)) 2647 return (EINVAL); 2648 2649 pktinfo = (struct in6_pktinfo *)buf; 2650 2651 /* 2652 * An application can clear any sticky IPV6_PKTINFO option by 2653 * doing a "regular" setsockopt with ipi6_addr being 2654 * in6addr_any and ipi6_ifindex being zero. 2655 * [RFC 3542, Section 6] 2656 */ 2657 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2658 pktinfo->ipi6_ifindex == 0 && 2659 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2660 ip6_clearpktopts(opt, optname); 2661 break; 2662 } 2663 2664 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2665 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2666 return (EINVAL); 2667 } 2668 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr)) 2669 return (EINVAL); 2670 /* validate the interface index if specified. */ 2671 if (pktinfo->ipi6_ifindex > V_if_index) 2672 return (ENXIO); 2673 if (pktinfo->ipi6_ifindex) { 2674 ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 2675 if (ifp == NULL) 2676 return (ENXIO); 2677 } 2678 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL || 2679 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0)) 2680 return (ENETDOWN); 2681 2682 if (ifp != NULL && 2683 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2684 struct in6_ifaddr *ia; 2685 2686 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL); 2687 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr); 2688 if (ia == NULL) 2689 return (EADDRNOTAVAIL); 2690 ifa_free(&ia->ia_ifa); 2691 } 2692 /* 2693 * We store the address anyway, and let in6_selectsrc() 2694 * validate the specified address. This is because ipi6_addr 2695 * may not have enough information about its scope zone, and 2696 * we may need additional information (such as outgoing 2697 * interface or the scope zone of a destination address) to 2698 * disambiguate the scope. 2699 * XXX: the delay of the validation may confuse the 2700 * application when it is used as a sticky option. 2701 */ 2702 if (opt->ip6po_pktinfo == NULL) { 2703 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2704 M_IP6OPT, M_NOWAIT); 2705 if (opt->ip6po_pktinfo == NULL) 2706 return (ENOBUFS); 2707 } 2708 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 2709 break; 2710 } 2711 2712 case IPV6_2292HOPLIMIT: 2713 case IPV6_HOPLIMIT: 2714 { 2715 int *hlimp; 2716 2717 /* 2718 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2719 * to simplify the ordering among hoplimit options. 2720 */ 2721 if (optname == IPV6_HOPLIMIT && sticky) 2722 return (ENOPROTOOPT); 2723 2724 if (len != sizeof(int)) 2725 return (EINVAL); 2726 hlimp = (int *)buf; 2727 if (*hlimp < -1 || *hlimp > 255) 2728 return (EINVAL); 2729 2730 opt->ip6po_hlim = *hlimp; 2731 break; 2732 } 2733 2734 case IPV6_TCLASS: 2735 { 2736 int tclass; 2737 2738 if (len != sizeof(int)) 2739 return (EINVAL); 2740 tclass = *(int *)buf; 2741 if (tclass < -1 || tclass > 255) 2742 return (EINVAL); 2743 2744 opt->ip6po_tclass = tclass; 2745 break; 2746 } 2747 2748 case IPV6_2292NEXTHOP: 2749 case IPV6_NEXTHOP: 2750 if (cred != NULL) { 2751 error = priv_check_cred(cred, 2752 PRIV_NETINET_SETHDROPTS, 0); 2753 if (error) 2754 return (error); 2755 } 2756 2757 if (len == 0) { /* just remove the option */ 2758 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2759 break; 2760 } 2761 2762 /* check if cmsg_len is large enough for sa_len */ 2763 if (len < sizeof(struct sockaddr) || len < *buf) 2764 return (EINVAL); 2765 2766 switch (((struct sockaddr *)buf)->sa_family) { 2767 case AF_INET6: 2768 { 2769 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 2770 int error; 2771 2772 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 2773 return (EINVAL); 2774 2775 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 2776 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 2777 return (EINVAL); 2778 } 2779 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone)) 2780 != 0) { 2781 return (error); 2782 } 2783 break; 2784 } 2785 case AF_LINK: /* should eventually be supported */ 2786 default: 2787 return (EAFNOSUPPORT); 2788 } 2789 2790 /* turn off the previous option, then set the new option. */ 2791 ip6_clearpktopts(opt, IPV6_NEXTHOP); 2792 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 2793 if (opt->ip6po_nexthop == NULL) 2794 return (ENOBUFS); 2795 bcopy(buf, opt->ip6po_nexthop, *buf); 2796 break; 2797 2798 case IPV6_2292HOPOPTS: 2799 case IPV6_HOPOPTS: 2800 { 2801 struct ip6_hbh *hbh; 2802 int hbhlen; 2803 2804 /* 2805 * XXX: We don't allow a non-privileged user to set ANY HbH 2806 * options, since per-option restriction has too much 2807 * overhead. 2808 */ 2809 if (cred != NULL) { 2810 error = priv_check_cred(cred, 2811 PRIV_NETINET_SETHDROPTS, 0); 2812 if (error) 2813 return (error); 2814 } 2815 2816 if (len == 0) { 2817 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2818 break; /* just remove the option */ 2819 } 2820 2821 /* message length validation */ 2822 if (len < sizeof(struct ip6_hbh)) 2823 return (EINVAL); 2824 hbh = (struct ip6_hbh *)buf; 2825 hbhlen = (hbh->ip6h_len + 1) << 3; 2826 if (len != hbhlen) 2827 return (EINVAL); 2828 2829 /* turn off the previous option, then set the new option. */ 2830 ip6_clearpktopts(opt, IPV6_HOPOPTS); 2831 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 2832 if (opt->ip6po_hbh == NULL) 2833 return (ENOBUFS); 2834 bcopy(hbh, opt->ip6po_hbh, hbhlen); 2835 2836 break; 2837 } 2838 2839 case IPV6_2292DSTOPTS: 2840 case IPV6_DSTOPTS: 2841 case IPV6_RTHDRDSTOPTS: 2842 { 2843 struct ip6_dest *dest, **newdest = NULL; 2844 int destlen; 2845 2846 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */ 2847 error = priv_check_cred(cred, 2848 PRIV_NETINET_SETHDROPTS, 0); 2849 if (error) 2850 return (error); 2851 } 2852 2853 if (len == 0) { 2854 ip6_clearpktopts(opt, optname); 2855 break; /* just remove the option */ 2856 } 2857 2858 /* message length validation */ 2859 if (len < sizeof(struct ip6_dest)) 2860 return (EINVAL); 2861 dest = (struct ip6_dest *)buf; 2862 destlen = (dest->ip6d_len + 1) << 3; 2863 if (len != destlen) 2864 return (EINVAL); 2865 2866 /* 2867 * Determine the position that the destination options header 2868 * should be inserted; before or after the routing header. 2869 */ 2870 switch (optname) { 2871 case IPV6_2292DSTOPTS: 2872 /* 2873 * The old advacned API is ambiguous on this point. 2874 * Our approach is to determine the position based 2875 * according to the existence of a routing header. 2876 * Note, however, that this depends on the order of the 2877 * extension headers in the ancillary data; the 1st 2878 * part of the destination options header must appear 2879 * before the routing header in the ancillary data, 2880 * too. 2881 * RFC3542 solved the ambiguity by introducing 2882 * separate ancillary data or option types. 2883 */ 2884 if (opt->ip6po_rthdr == NULL) 2885 newdest = &opt->ip6po_dest1; 2886 else 2887 newdest = &opt->ip6po_dest2; 2888 break; 2889 case IPV6_RTHDRDSTOPTS: 2890 newdest = &opt->ip6po_dest1; 2891 break; 2892 case IPV6_DSTOPTS: 2893 newdest = &opt->ip6po_dest2; 2894 break; 2895 } 2896 2897 /* turn off the previous option, then set the new option. */ 2898 ip6_clearpktopts(opt, optname); 2899 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 2900 if (*newdest == NULL) 2901 return (ENOBUFS); 2902 bcopy(dest, *newdest, destlen); 2903 2904 break; 2905 } 2906 2907 case IPV6_2292RTHDR: 2908 case IPV6_RTHDR: 2909 { 2910 struct ip6_rthdr *rth; 2911 int rthlen; 2912 2913 if (len == 0) { 2914 ip6_clearpktopts(opt, IPV6_RTHDR); 2915 break; /* just remove the option */ 2916 } 2917 2918 /* message length validation */ 2919 if (len < sizeof(struct ip6_rthdr)) 2920 return (EINVAL); 2921 rth = (struct ip6_rthdr *)buf; 2922 rthlen = (rth->ip6r_len + 1) << 3; 2923 if (len != rthlen) 2924 return (EINVAL); 2925 2926 switch (rth->ip6r_type) { 2927 case IPV6_RTHDR_TYPE_0: 2928 if (rth->ip6r_len == 0) /* must contain one addr */ 2929 return (EINVAL); 2930 if (rth->ip6r_len % 2) /* length must be even */ 2931 return (EINVAL); 2932 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 2933 return (EINVAL); 2934 break; 2935 default: 2936 return (EINVAL); /* not supported */ 2937 } 2938 2939 /* turn off the previous option */ 2940 ip6_clearpktopts(opt, IPV6_RTHDR); 2941 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 2942 if (opt->ip6po_rthdr == NULL) 2943 return (ENOBUFS); 2944 bcopy(rth, opt->ip6po_rthdr, rthlen); 2945 2946 break; 2947 } 2948 2949 case IPV6_USE_MIN_MTU: 2950 if (len != sizeof(int)) 2951 return (EINVAL); 2952 minmtupolicy = *(int *)buf; 2953 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 2954 minmtupolicy != IP6PO_MINMTU_DISABLE && 2955 minmtupolicy != IP6PO_MINMTU_ALL) { 2956 return (EINVAL); 2957 } 2958 opt->ip6po_minmtu = minmtupolicy; 2959 break; 2960 2961 case IPV6_DONTFRAG: 2962 if (len != sizeof(int)) 2963 return (EINVAL); 2964 2965 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 2966 /* 2967 * we ignore this option for TCP sockets. 2968 * (RFC3542 leaves this case unspecified.) 2969 */ 2970 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 2971 } else 2972 opt->ip6po_flags |= IP6PO_DONTFRAG; 2973 break; 2974 2975 case IPV6_PREFER_TEMPADDR: 2976 if (len != sizeof(int)) 2977 return (EINVAL); 2978 preftemp = *(int *)buf; 2979 if (preftemp != IP6PO_TEMPADDR_SYSTEM && 2980 preftemp != IP6PO_TEMPADDR_NOTPREFER && 2981 preftemp != IP6PO_TEMPADDR_PREFER) { 2982 return (EINVAL); 2983 } 2984 opt->ip6po_prefer_tempaddr = preftemp; 2985 break; 2986 2987 default: 2988 return (ENOPROTOOPT); 2989 } /* end of switch */ 2990 2991 return (0); 2992 } 2993 2994 /* 2995 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 2996 * packet to the input queue of a specified interface. Note that this 2997 * calls the output routine of the loopback "driver", but with an interface 2998 * pointer that might NOT be &loif -- easier than replicating that code here. 2999 */ 3000 void 3001 ip6_mloopback(struct ifnet *ifp, struct mbuf *m) 3002 { 3003 struct mbuf *copym; 3004 struct ip6_hdr *ip6; 3005 3006 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT); 3007 if (copym == NULL) 3008 return; 3009 3010 /* 3011 * Make sure to deep-copy IPv6 header portion in case the data 3012 * is in an mbuf cluster, so that we can safely override the IPv6 3013 * header portion later. 3014 */ 3015 if (!M_WRITABLE(copym) || 3016 copym->m_len < sizeof(struct ip6_hdr)) { 3017 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 3018 if (copym == NULL) 3019 return; 3020 } 3021 ip6 = mtod(copym, struct ip6_hdr *); 3022 /* 3023 * clear embedded scope identifiers if necessary. 3024 * in6_clearscope will touch the addresses only when necessary. 3025 */ 3026 in6_clearscope(&ip6->ip6_src); 3027 in6_clearscope(&ip6->ip6_dst); 3028 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 3029 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 | 3030 CSUM_PSEUDO_HDR; 3031 copym->m_pkthdr.csum_data = 0xffff; 3032 } 3033 if_simloop(ifp, copym, AF_INET6, 0); 3034 } 3035 3036 /* 3037 * Chop IPv6 header off from the payload. 3038 */ 3039 static int 3040 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 3041 { 3042 struct mbuf *mh; 3043 struct ip6_hdr *ip6; 3044 3045 ip6 = mtod(m, struct ip6_hdr *); 3046 if (m->m_len > sizeof(*ip6)) { 3047 mh = m_gethdr(M_NOWAIT, MT_DATA); 3048 if (mh == NULL) { 3049 m_freem(m); 3050 return ENOBUFS; 3051 } 3052 m_move_pkthdr(mh, m); 3053 M_ALIGN(mh, sizeof(*ip6)); 3054 m->m_len -= sizeof(*ip6); 3055 m->m_data += sizeof(*ip6); 3056 mh->m_next = m; 3057 m = mh; 3058 m->m_len = sizeof(*ip6); 3059 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 3060 } 3061 exthdrs->ip6e_ip6 = m; 3062 return 0; 3063 } 3064 3065 /* 3066 * Compute IPv6 extension header length. 3067 */ 3068 int 3069 ip6_optlen(struct inpcb *in6p) 3070 { 3071 int len; 3072 3073 if (!in6p->in6p_outputopts) 3074 return 0; 3075 3076 len = 0; 3077 #define elen(x) \ 3078 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 3079 3080 len += elen(in6p->in6p_outputopts->ip6po_hbh); 3081 if (in6p->in6p_outputopts->ip6po_rthdr) 3082 /* dest1 is valid with rthdr only */ 3083 len += elen(in6p->in6p_outputopts->ip6po_dest1); 3084 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 3085 len += elen(in6p->in6p_outputopts->ip6po_dest2); 3086 return len; 3087 #undef elen 3088 } 3089