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