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