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