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