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