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