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