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