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