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