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 if (sopt) { 1379 level = sopt->sopt_level; 1380 op = sopt->sopt_dir; 1381 optname = sopt->sopt_name; 1382 optlen = sopt->sopt_valsize; 1383 td = sopt->sopt_td; 1384 } else { 1385 panic("ip6_ctloutput: arg soopt is NULL"); 1386 } 1387 error = optval = 0; 1388 1389 uproto = (int)so->so_proto->pr_protocol; 1390 1391 if (level == IPPROTO_IPV6) { 1392 switch (op) { 1393 1394 case SOPT_SET: 1395 switch (optname) { 1396 case IPV6_2292PKTOPTIONS: 1397 #ifdef IPV6_PKTOPTIONS 1398 case IPV6_PKTOPTIONS: 1399 #endif 1400 { 1401 struct mbuf *m; 1402 1403 error = soopt_getm(sopt, &m); /* XXX */ 1404 if (error != 0) 1405 break; 1406 error = soopt_mcopyin(sopt, m); /* XXX */ 1407 if (error != 0) 1408 break; 1409 error = ip6_pcbopts(&in6p->in6p_outputopts, 1410 m, so, sopt); 1411 m_freem(m); /* XXX */ 1412 break; 1413 } 1414 1415 /* 1416 * Use of some Hop-by-Hop options or some 1417 * Destination options, might require special 1418 * privilege. That is, normal applications 1419 * (without special privilege) might be forbidden 1420 * from setting certain options in outgoing packets, 1421 * and might never see certain options in received 1422 * packets. [RFC 2292 Section 6] 1423 * KAME specific note: 1424 * KAME prevents non-privileged users from sending or 1425 * receiving ANY hbh/dst options in order to avoid 1426 * overhead of parsing options in the kernel. 1427 */ 1428 case IPV6_RECVHOPOPTS: 1429 case IPV6_RECVDSTOPTS: 1430 case IPV6_RECVRTHDRDSTOPTS: 1431 if (td != NULL) { 1432 error = priv_check(td, 1433 PRIV_NETINET_SETHDROPTS); 1434 if (error) 1435 break; 1436 } 1437 /* FALLTHROUGH */ 1438 case IPV6_UNICAST_HOPS: 1439 case IPV6_HOPLIMIT: 1440 case IPV6_FAITH: 1441 1442 case IPV6_RECVPKTINFO: 1443 case IPV6_RECVHOPLIMIT: 1444 case IPV6_RECVRTHDR: 1445 case IPV6_RECVPATHMTU: 1446 case IPV6_RECVTCLASS: 1447 case IPV6_V6ONLY: 1448 case IPV6_AUTOFLOWLABEL: 1449 if (optlen != sizeof(int)) { 1450 error = EINVAL; 1451 break; 1452 } 1453 error = sooptcopyin(sopt, &optval, 1454 sizeof optval, sizeof optval); 1455 if (error) 1456 break; 1457 switch (optname) { 1458 1459 case IPV6_UNICAST_HOPS: 1460 if (optval < -1 || optval >= 256) 1461 error = EINVAL; 1462 else { 1463 /* -1 = kernel default */ 1464 in6p->in6p_hops = optval; 1465 if ((in6p->in6p_vflag & 1466 INP_IPV4) != 0) 1467 in6p->inp_ip_ttl = optval; 1468 } 1469 break; 1470 #define OPTSET(bit) \ 1471 do { \ 1472 if (optval) \ 1473 in6p->in6p_flags |= (bit); \ 1474 else \ 1475 in6p->in6p_flags &= ~(bit); \ 1476 } while (/*CONSTCOND*/ 0) 1477 #define OPTSET2292(bit) \ 1478 do { \ 1479 in6p->in6p_flags |= IN6P_RFC2292; \ 1480 if (optval) \ 1481 in6p->in6p_flags |= (bit); \ 1482 else \ 1483 in6p->in6p_flags &= ~(bit); \ 1484 } while (/*CONSTCOND*/ 0) 1485 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) 1486 1487 case IPV6_RECVPKTINFO: 1488 /* cannot mix with RFC2292 */ 1489 if (OPTBIT(IN6P_RFC2292)) { 1490 error = EINVAL; 1491 break; 1492 } 1493 OPTSET(IN6P_PKTINFO); 1494 break; 1495 1496 case IPV6_HOPLIMIT: 1497 { 1498 struct ip6_pktopts **optp; 1499 1500 /* cannot mix with RFC2292 */ 1501 if (OPTBIT(IN6P_RFC2292)) { 1502 error = EINVAL; 1503 break; 1504 } 1505 optp = &in6p->in6p_outputopts; 1506 error = ip6_pcbopt(IPV6_HOPLIMIT, 1507 (u_char *)&optval, sizeof(optval), 1508 optp, (td != NULL) ? td->td_ucred : 1509 NULL, uproto); 1510 break; 1511 } 1512 1513 case IPV6_RECVHOPLIMIT: 1514 /* cannot mix with RFC2292 */ 1515 if (OPTBIT(IN6P_RFC2292)) { 1516 error = EINVAL; 1517 break; 1518 } 1519 OPTSET(IN6P_HOPLIMIT); 1520 break; 1521 1522 case IPV6_RECVHOPOPTS: 1523 /* cannot mix with RFC2292 */ 1524 if (OPTBIT(IN6P_RFC2292)) { 1525 error = EINVAL; 1526 break; 1527 } 1528 OPTSET(IN6P_HOPOPTS); 1529 break; 1530 1531 case IPV6_RECVDSTOPTS: 1532 /* cannot mix with RFC2292 */ 1533 if (OPTBIT(IN6P_RFC2292)) { 1534 error = EINVAL; 1535 break; 1536 } 1537 OPTSET(IN6P_DSTOPTS); 1538 break; 1539 1540 case IPV6_RECVRTHDRDSTOPTS: 1541 /* cannot mix with RFC2292 */ 1542 if (OPTBIT(IN6P_RFC2292)) { 1543 error = EINVAL; 1544 break; 1545 } 1546 OPTSET(IN6P_RTHDRDSTOPTS); 1547 break; 1548 1549 case IPV6_RECVRTHDR: 1550 /* cannot mix with RFC2292 */ 1551 if (OPTBIT(IN6P_RFC2292)) { 1552 error = EINVAL; 1553 break; 1554 } 1555 OPTSET(IN6P_RTHDR); 1556 break; 1557 1558 case IPV6_FAITH: 1559 OPTSET(IN6P_FAITH); 1560 break; 1561 1562 case IPV6_RECVPATHMTU: 1563 /* 1564 * We ignore this option for TCP 1565 * sockets. 1566 * (RFC3542 leaves this case 1567 * unspecified.) 1568 */ 1569 if (uproto != IPPROTO_TCP) 1570 OPTSET(IN6P_MTU); 1571 break; 1572 1573 case IPV6_V6ONLY: 1574 /* 1575 * make setsockopt(IPV6_V6ONLY) 1576 * available only prior to bind(2). 1577 * see ipng mailing list, Jun 22 2001. 1578 */ 1579 if (in6p->in6p_lport || 1580 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1581 error = EINVAL; 1582 break; 1583 } 1584 OPTSET(IN6P_IPV6_V6ONLY); 1585 if (optval) 1586 in6p->in6p_vflag &= ~INP_IPV4; 1587 else 1588 in6p->in6p_vflag |= INP_IPV4; 1589 break; 1590 case IPV6_RECVTCLASS: 1591 /* cannot mix with RFC2292 XXX */ 1592 if (OPTBIT(IN6P_RFC2292)) { 1593 error = EINVAL; 1594 break; 1595 } 1596 OPTSET(IN6P_TCLASS); 1597 break; 1598 case IPV6_AUTOFLOWLABEL: 1599 OPTSET(IN6P_AUTOFLOWLABEL); 1600 break; 1601 1602 } 1603 break; 1604 1605 case IPV6_TCLASS: 1606 case IPV6_DONTFRAG: 1607 case IPV6_USE_MIN_MTU: 1608 case IPV6_PREFER_TEMPADDR: 1609 if (optlen != sizeof(optval)) { 1610 error = EINVAL; 1611 break; 1612 } 1613 error = sooptcopyin(sopt, &optval, 1614 sizeof optval, sizeof optval); 1615 if (error) 1616 break; 1617 { 1618 struct ip6_pktopts **optp; 1619 optp = &in6p->in6p_outputopts; 1620 error = ip6_pcbopt(optname, 1621 (u_char *)&optval, sizeof(optval), 1622 optp, (td != NULL) ? td->td_ucred : 1623 NULL, uproto); 1624 break; 1625 } 1626 1627 case IPV6_2292PKTINFO: 1628 case IPV6_2292HOPLIMIT: 1629 case IPV6_2292HOPOPTS: 1630 case IPV6_2292DSTOPTS: 1631 case IPV6_2292RTHDR: 1632 /* RFC 2292 */ 1633 if (optlen != sizeof(int)) { 1634 error = EINVAL; 1635 break; 1636 } 1637 error = sooptcopyin(sopt, &optval, 1638 sizeof optval, sizeof optval); 1639 if (error) 1640 break; 1641 switch (optname) { 1642 case IPV6_2292PKTINFO: 1643 OPTSET2292(IN6P_PKTINFO); 1644 break; 1645 case IPV6_2292HOPLIMIT: 1646 OPTSET2292(IN6P_HOPLIMIT); 1647 break; 1648 case IPV6_2292HOPOPTS: 1649 /* 1650 * Check super-user privilege. 1651 * See comments for IPV6_RECVHOPOPTS. 1652 */ 1653 if (td != NULL) { 1654 error = priv_check(td, 1655 PRIV_NETINET_SETHDROPTS); 1656 if (error) 1657 return (error); 1658 } 1659 OPTSET2292(IN6P_HOPOPTS); 1660 break; 1661 case IPV6_2292DSTOPTS: 1662 if (td != NULL) { 1663 error = priv_check(td, 1664 PRIV_NETINET_SETHDROPTS); 1665 if (error) 1666 return (error); 1667 } 1668 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1669 break; 1670 case IPV6_2292RTHDR: 1671 OPTSET2292(IN6P_RTHDR); 1672 break; 1673 } 1674 break; 1675 case IPV6_PKTINFO: 1676 case IPV6_HOPOPTS: 1677 case IPV6_RTHDR: 1678 case IPV6_DSTOPTS: 1679 case IPV6_RTHDRDSTOPTS: 1680 case IPV6_NEXTHOP: 1681 { 1682 /* new advanced API (RFC3542) */ 1683 u_char *optbuf; 1684 u_char optbuf_storage[MCLBYTES]; 1685 int optlen; 1686 struct ip6_pktopts **optp; 1687 1688 /* cannot mix with RFC2292 */ 1689 if (OPTBIT(IN6P_RFC2292)) { 1690 error = EINVAL; 1691 break; 1692 } 1693 1694 /* 1695 * We only ensure valsize is not too large 1696 * here. Further validation will be done 1697 * later. 1698 */ 1699 error = sooptcopyin(sopt, optbuf_storage, 1700 sizeof(optbuf_storage), 0); 1701 if (error) 1702 break; 1703 optlen = sopt->sopt_valsize; 1704 optbuf = optbuf_storage; 1705 optp = &in6p->in6p_outputopts; 1706 error = ip6_pcbopt(optname, optbuf, optlen, 1707 optp, (td != NULL) ? td->td_ucred : NULL, 1708 uproto); 1709 break; 1710 } 1711 #undef OPTSET 1712 1713 case IPV6_MULTICAST_IF: 1714 case IPV6_MULTICAST_HOPS: 1715 case IPV6_MULTICAST_LOOP: 1716 case IPV6_JOIN_GROUP: 1717 case IPV6_LEAVE_GROUP: 1718 { 1719 if (sopt->sopt_valsize > MLEN) { 1720 error = EMSGSIZE; 1721 break; 1722 } 1723 /* XXX */ 1724 } 1725 /* FALLTHROUGH */ 1726 { 1727 struct mbuf *m; 1728 1729 if (sopt->sopt_valsize > MCLBYTES) { 1730 error = EMSGSIZE; 1731 break; 1732 } 1733 /* XXX */ 1734 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA); 1735 if (m == 0) { 1736 error = ENOBUFS; 1737 break; 1738 } 1739 if (sopt->sopt_valsize > MLEN) { 1740 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT); 1741 if ((m->m_flags & M_EXT) == 0) { 1742 m_free(m); 1743 error = ENOBUFS; 1744 break; 1745 } 1746 } 1747 m->m_len = sopt->sopt_valsize; 1748 error = sooptcopyin(sopt, mtod(m, char *), 1749 m->m_len, m->m_len); 1750 if (error) { 1751 (void)m_free(m); 1752 break; 1753 } 1754 error = ip6_setmoptions(sopt->sopt_name, 1755 &in6p->in6p_moptions, 1756 m); 1757 (void)m_free(m); 1758 } 1759 break; 1760 1761 case IPV6_PORTRANGE: 1762 error = sooptcopyin(sopt, &optval, 1763 sizeof optval, sizeof optval); 1764 if (error) 1765 break; 1766 1767 switch (optval) { 1768 case IPV6_PORTRANGE_DEFAULT: 1769 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1770 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1771 break; 1772 1773 case IPV6_PORTRANGE_HIGH: 1774 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1775 in6p->in6p_flags |= IN6P_HIGHPORT; 1776 break; 1777 1778 case IPV6_PORTRANGE_LOW: 1779 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1780 in6p->in6p_flags |= IN6P_LOWPORT; 1781 break; 1782 1783 default: 1784 error = EINVAL; 1785 break; 1786 } 1787 break; 1788 1789 #ifdef IPSEC 1790 case IPV6_IPSEC_POLICY: 1791 { 1792 caddr_t req; 1793 struct mbuf *m; 1794 1795 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1796 break; 1797 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1798 break; 1799 req = mtod(m, caddr_t); 1800 error = ipsec6_set_policy(in6p, optname, req, 1801 m->m_len, (sopt->sopt_td != NULL) ? 1802 sopt->sopt_td->td_ucred : NULL); 1803 m_freem(m); 1804 break; 1805 } 1806 #endif /* IPSEC */ 1807 1808 default: 1809 error = ENOPROTOOPT; 1810 break; 1811 } 1812 break; 1813 1814 case SOPT_GET: 1815 switch (optname) { 1816 1817 case IPV6_2292PKTOPTIONS: 1818 #ifdef IPV6_PKTOPTIONS 1819 case IPV6_PKTOPTIONS: 1820 #endif 1821 /* 1822 * RFC3542 (effectively) deprecated the 1823 * semantics of the 2292-style pktoptions. 1824 * Since it was not reliable in nature (i.e., 1825 * applications had to expect the lack of some 1826 * information after all), it would make sense 1827 * to simplify this part by always returning 1828 * empty data. 1829 */ 1830 sopt->sopt_valsize = 0; 1831 break; 1832 1833 case IPV6_RECVHOPOPTS: 1834 case IPV6_RECVDSTOPTS: 1835 case IPV6_RECVRTHDRDSTOPTS: 1836 case IPV6_UNICAST_HOPS: 1837 case IPV6_RECVPKTINFO: 1838 case IPV6_RECVHOPLIMIT: 1839 case IPV6_RECVRTHDR: 1840 case IPV6_RECVPATHMTU: 1841 1842 case IPV6_FAITH: 1843 case IPV6_V6ONLY: 1844 case IPV6_PORTRANGE: 1845 case IPV6_RECVTCLASS: 1846 case IPV6_AUTOFLOWLABEL: 1847 switch (optname) { 1848 1849 case IPV6_RECVHOPOPTS: 1850 optval = OPTBIT(IN6P_HOPOPTS); 1851 break; 1852 1853 case IPV6_RECVDSTOPTS: 1854 optval = OPTBIT(IN6P_DSTOPTS); 1855 break; 1856 1857 case IPV6_RECVRTHDRDSTOPTS: 1858 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 1859 break; 1860 1861 case IPV6_UNICAST_HOPS: 1862 optval = in6p->in6p_hops; 1863 break; 1864 1865 case IPV6_RECVPKTINFO: 1866 optval = OPTBIT(IN6P_PKTINFO); 1867 break; 1868 1869 case IPV6_RECVHOPLIMIT: 1870 optval = OPTBIT(IN6P_HOPLIMIT); 1871 break; 1872 1873 case IPV6_RECVRTHDR: 1874 optval = OPTBIT(IN6P_RTHDR); 1875 break; 1876 1877 case IPV6_RECVPATHMTU: 1878 optval = OPTBIT(IN6P_MTU); 1879 break; 1880 1881 case IPV6_FAITH: 1882 optval = OPTBIT(IN6P_FAITH); 1883 break; 1884 1885 case IPV6_V6ONLY: 1886 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1887 break; 1888 1889 case IPV6_PORTRANGE: 1890 { 1891 int flags; 1892 flags = in6p->in6p_flags; 1893 if (flags & IN6P_HIGHPORT) 1894 optval = IPV6_PORTRANGE_HIGH; 1895 else if (flags & IN6P_LOWPORT) 1896 optval = IPV6_PORTRANGE_LOW; 1897 else 1898 optval = 0; 1899 break; 1900 } 1901 case IPV6_RECVTCLASS: 1902 optval = OPTBIT(IN6P_TCLASS); 1903 break; 1904 1905 case IPV6_AUTOFLOWLABEL: 1906 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 1907 break; 1908 } 1909 if (error) 1910 break; 1911 error = sooptcopyout(sopt, &optval, 1912 sizeof optval); 1913 break; 1914 1915 case IPV6_PATHMTU: 1916 { 1917 u_long pmtu = 0; 1918 struct ip6_mtuinfo mtuinfo; 1919 struct route_in6 sro; 1920 1921 bzero(&sro, sizeof(sro)); 1922 1923 if (!(so->so_state & SS_ISCONNECTED)) 1924 return (ENOTCONN); 1925 /* 1926 * XXX: we dot not consider the case of source 1927 * routing, or optional information to specify 1928 * the outgoing interface. 1929 */ 1930 error = ip6_getpmtu(&sro, NULL, NULL, 1931 &in6p->in6p_faddr, &pmtu, NULL); 1932 if (sro.ro_rt) 1933 RTFREE(sro.ro_rt); 1934 if (error) 1935 break; 1936 if (pmtu > IPV6_MAXPACKET) 1937 pmtu = IPV6_MAXPACKET; 1938 1939 bzero(&mtuinfo, sizeof(mtuinfo)); 1940 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 1941 optdata = (void *)&mtuinfo; 1942 optdatalen = sizeof(mtuinfo); 1943 error = sooptcopyout(sopt, optdata, 1944 optdatalen); 1945 break; 1946 } 1947 1948 case IPV6_2292PKTINFO: 1949 case IPV6_2292HOPLIMIT: 1950 case IPV6_2292HOPOPTS: 1951 case IPV6_2292RTHDR: 1952 case IPV6_2292DSTOPTS: 1953 switch (optname) { 1954 case IPV6_2292PKTINFO: 1955 optval = OPTBIT(IN6P_PKTINFO); 1956 break; 1957 case IPV6_2292HOPLIMIT: 1958 optval = OPTBIT(IN6P_HOPLIMIT); 1959 break; 1960 case IPV6_2292HOPOPTS: 1961 optval = OPTBIT(IN6P_HOPOPTS); 1962 break; 1963 case IPV6_2292RTHDR: 1964 optval = OPTBIT(IN6P_RTHDR); 1965 break; 1966 case IPV6_2292DSTOPTS: 1967 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1968 break; 1969 } 1970 error = sooptcopyout(sopt, &optval, 1971 sizeof optval); 1972 break; 1973 case IPV6_PKTINFO: 1974 case IPV6_HOPOPTS: 1975 case IPV6_RTHDR: 1976 case IPV6_DSTOPTS: 1977 case IPV6_RTHDRDSTOPTS: 1978 case IPV6_NEXTHOP: 1979 case IPV6_TCLASS: 1980 case IPV6_DONTFRAG: 1981 case IPV6_USE_MIN_MTU: 1982 case IPV6_PREFER_TEMPADDR: 1983 error = ip6_getpcbopt(in6p->in6p_outputopts, 1984 optname, sopt); 1985 break; 1986 1987 case IPV6_MULTICAST_IF: 1988 case IPV6_MULTICAST_HOPS: 1989 case IPV6_MULTICAST_LOOP: 1990 case IPV6_JOIN_GROUP: 1991 case IPV6_LEAVE_GROUP: 1992 { 1993 struct mbuf *m; 1994 error = ip6_getmoptions(sopt->sopt_name, 1995 in6p->in6p_moptions, &m); 1996 if (error == 0) 1997 error = sooptcopyout(sopt, 1998 mtod(m, char *), m->m_len); 1999 m_freem(m); 2000 } 2001 break; 2002 2003 #ifdef IPSEC 2004 case IPV6_IPSEC_POLICY: 2005 { 2006 caddr_t req = NULL; 2007 size_t len = 0; 2008 struct mbuf *m = NULL; 2009 struct mbuf **mp = &m; 2010 size_t ovalsize = sopt->sopt_valsize; 2011 caddr_t oval = (caddr_t)sopt->sopt_val; 2012 2013 error = soopt_getm(sopt, &m); /* XXX */ 2014 if (error != 0) 2015 break; 2016 error = soopt_mcopyin(sopt, m); /* XXX */ 2017 if (error != 0) 2018 break; 2019 sopt->sopt_valsize = ovalsize; 2020 sopt->sopt_val = oval; 2021 if (m) { 2022 req = mtod(m, caddr_t); 2023 len = m->m_len; 2024 } 2025 error = ipsec6_get_policy(in6p, req, len, mp); 2026 if (error == 0) 2027 error = soopt_mcopyout(sopt, m); /* XXX */ 2028 if (error == 0 && m) 2029 m_freem(m); 2030 break; 2031 } 2032 #endif /* IPSEC */ 2033 2034 default: 2035 error = ENOPROTOOPT; 2036 break; 2037 } 2038 break; 2039 } 2040 } else { /* level != IPPROTO_IPV6 */ 2041 error = EINVAL; 2042 } 2043 return (error); 2044 } 2045 2046 int 2047 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt) 2048 { 2049 int error = 0, optval, optlen; 2050 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 2051 struct in6pcb *in6p = sotoin6pcb(so); 2052 int level, op, optname; 2053 2054 if (sopt) { 2055 level = sopt->sopt_level; 2056 op = sopt->sopt_dir; 2057 optname = sopt->sopt_name; 2058 optlen = sopt->sopt_valsize; 2059 } else 2060 panic("ip6_raw_ctloutput: arg soopt is NULL"); 2061 2062 if (level != IPPROTO_IPV6) { 2063 return (EINVAL); 2064 } 2065 2066 switch (optname) { 2067 case IPV6_CHECKSUM: 2068 /* 2069 * For ICMPv6 sockets, no modification allowed for checksum 2070 * offset, permit "no change" values to help existing apps. 2071 * 2072 * RFC3542 says: "An attempt to set IPV6_CHECKSUM 2073 * for an ICMPv6 socket will fail." 2074 * The current behavior does not meet RFC3542. 2075 */ 2076 switch (op) { 2077 case SOPT_SET: 2078 if (optlen != sizeof(int)) { 2079 error = EINVAL; 2080 break; 2081 } 2082 error = sooptcopyin(sopt, &optval, sizeof(optval), 2083 sizeof(optval)); 2084 if (error) 2085 break; 2086 if ((optval % 2) != 0) { 2087 /* the API assumes even offset values */ 2088 error = EINVAL; 2089 } else if (so->so_proto->pr_protocol == 2090 IPPROTO_ICMPV6) { 2091 if (optval != icmp6off) 2092 error = EINVAL; 2093 } else 2094 in6p->in6p_cksum = optval; 2095 break; 2096 2097 case SOPT_GET: 2098 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 2099 optval = icmp6off; 2100 else 2101 optval = in6p->in6p_cksum; 2102 2103 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2104 break; 2105 2106 default: 2107 error = EINVAL; 2108 break; 2109 } 2110 break; 2111 2112 default: 2113 error = ENOPROTOOPT; 2114 break; 2115 } 2116 2117 return (error); 2118 } 2119 2120 /* 2121 * Set up IP6 options in pcb for insertion in output packets or 2122 * specifying behavior of outgoing packets. 2123 */ 2124 static int 2125 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, 2126 struct socket *so, struct sockopt *sopt) 2127 { 2128 struct ip6_pktopts *opt = *pktopt; 2129 int error = 0; 2130 struct thread *td = sopt->sopt_td; 2131 2132 /* turn off any old options. */ 2133 if (opt) { 2134 #ifdef DIAGNOSTIC 2135 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 2136 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 2137 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 2138 printf("ip6_pcbopts: all specified options are cleared.\n"); 2139 #endif 2140 ip6_clearpktopts(opt, -1); 2141 } else 2142 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 2143 *pktopt = NULL; 2144 2145 if (!m || m->m_len == 0) { 2146 /* 2147 * Only turning off any previous options, regardless of 2148 * whether the opt is just created or given. 2149 */ 2150 free(opt, M_IP6OPT); 2151 return (0); 2152 } 2153 2154 /* set options specified by user. */ 2155 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ? 2156 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) { 2157 ip6_clearpktopts(opt, -1); /* XXX: discard all options */ 2158 free(opt, M_IP6OPT); 2159 return (error); 2160 } 2161 *pktopt = opt; 2162 return (0); 2163 } 2164 2165 /* 2166 * initialize ip6_pktopts. beware that there are non-zero default values in 2167 * the struct. 2168 */ 2169 void 2170 ip6_initpktopts(struct ip6_pktopts *opt) 2171 { 2172 2173 bzero(opt, sizeof(*opt)); 2174 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 2175 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 2176 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 2177 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; 2178 } 2179 2180 static int 2181 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, 2182 struct ucred *cred, int uproto) 2183 { 2184 struct ip6_pktopts *opt; 2185 2186 if (*pktopt == NULL) { 2187 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 2188 M_WAITOK); 2189 ip6_initpktopts(*pktopt); 2190 } 2191 opt = *pktopt; 2192 2193 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto)); 2194 } 2195 2196 static int 2197 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt) 2198 { 2199 void *optdata = NULL; 2200 int optdatalen = 0; 2201 struct ip6_ext *ip6e; 2202 int error = 0; 2203 struct in6_pktinfo null_pktinfo; 2204 int deftclass = 0, on; 2205 int defminmtu = IP6PO_MINMTU_MCASTONLY; 2206 int defpreftemp = IP6PO_TEMPADDR_SYSTEM; 2207 2208 switch (optname) { 2209 case IPV6_PKTINFO: 2210 if (pktopt && pktopt->ip6po_pktinfo) 2211 optdata = (void *)pktopt->ip6po_pktinfo; 2212 else { 2213 /* XXX: we don't have to do this every time... */ 2214 bzero(&null_pktinfo, sizeof(null_pktinfo)); 2215 optdata = (void *)&null_pktinfo; 2216 } 2217 optdatalen = sizeof(struct in6_pktinfo); 2218 break; 2219 case IPV6_TCLASS: 2220 if (pktopt && pktopt->ip6po_tclass >= 0) 2221 optdata = (void *)&pktopt->ip6po_tclass; 2222 else 2223 optdata = (void *)&deftclass; 2224 optdatalen = sizeof(int); 2225 break; 2226 case IPV6_HOPOPTS: 2227 if (pktopt && pktopt->ip6po_hbh) { 2228 optdata = (void *)pktopt->ip6po_hbh; 2229 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; 2230 optdatalen = (ip6e->ip6e_len + 1) << 3; 2231 } 2232 break; 2233 case IPV6_RTHDR: 2234 if (pktopt && pktopt->ip6po_rthdr) { 2235 optdata = (void *)pktopt->ip6po_rthdr; 2236 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; 2237 optdatalen = (ip6e->ip6e_len + 1) << 3; 2238 } 2239 break; 2240 case IPV6_RTHDRDSTOPTS: 2241 if (pktopt && pktopt->ip6po_dest1) { 2242 optdata = (void *)pktopt->ip6po_dest1; 2243 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; 2244 optdatalen = (ip6e->ip6e_len + 1) << 3; 2245 } 2246 break; 2247 case IPV6_DSTOPTS: 2248 if (pktopt && pktopt->ip6po_dest2) { 2249 optdata = (void *)pktopt->ip6po_dest2; 2250 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; 2251 optdatalen = (ip6e->ip6e_len + 1) << 3; 2252 } 2253 break; 2254 case IPV6_NEXTHOP: 2255 if (pktopt && pktopt->ip6po_nexthop) { 2256 optdata = (void *)pktopt->ip6po_nexthop; 2257 optdatalen = pktopt->ip6po_nexthop->sa_len; 2258 } 2259 break; 2260 case IPV6_USE_MIN_MTU: 2261 if (pktopt) 2262 optdata = (void *)&pktopt->ip6po_minmtu; 2263 else 2264 optdata = (void *)&defminmtu; 2265 optdatalen = sizeof(int); 2266 break; 2267 case IPV6_DONTFRAG: 2268 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 2269 on = 1; 2270 else 2271 on = 0; 2272 optdata = (void *)&on; 2273 optdatalen = sizeof(on); 2274 break; 2275 case IPV6_PREFER_TEMPADDR: 2276 if (pktopt) 2277 optdata = (void *)&pktopt->ip6po_prefer_tempaddr; 2278 else 2279 optdata = (void *)&defpreftemp; 2280 optdatalen = sizeof(int); 2281 break; 2282 default: /* should not happen */ 2283 #ifdef DIAGNOSTIC 2284 panic("ip6_getpcbopt: unexpected option\n"); 2285 #endif 2286 return (ENOPROTOOPT); 2287 } 2288 2289 error = sooptcopyout(sopt, optdata, optdatalen); 2290 2291 return (error); 2292 } 2293 2294 void 2295 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) 2296 { 2297 if (pktopt == NULL) 2298 return; 2299 2300 if (optname == -1 || optname == IPV6_PKTINFO) { 2301 if (pktopt->ip6po_pktinfo) 2302 free(pktopt->ip6po_pktinfo, M_IP6OPT); 2303 pktopt->ip6po_pktinfo = NULL; 2304 } 2305 if (optname == -1 || optname == IPV6_HOPLIMIT) 2306 pktopt->ip6po_hlim = -1; 2307 if (optname == -1 || optname == IPV6_TCLASS) 2308 pktopt->ip6po_tclass = -1; 2309 if (optname == -1 || optname == IPV6_NEXTHOP) { 2310 if (pktopt->ip6po_nextroute.ro_rt) { 2311 RTFREE(pktopt->ip6po_nextroute.ro_rt); 2312 pktopt->ip6po_nextroute.ro_rt = NULL; 2313 } 2314 if (pktopt->ip6po_nexthop) 2315 free(pktopt->ip6po_nexthop, M_IP6OPT); 2316 pktopt->ip6po_nexthop = NULL; 2317 } 2318 if (optname == -1 || optname == IPV6_HOPOPTS) { 2319 if (pktopt->ip6po_hbh) 2320 free(pktopt->ip6po_hbh, M_IP6OPT); 2321 pktopt->ip6po_hbh = NULL; 2322 } 2323 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 2324 if (pktopt->ip6po_dest1) 2325 free(pktopt->ip6po_dest1, M_IP6OPT); 2326 pktopt->ip6po_dest1 = NULL; 2327 } 2328 if (optname == -1 || optname == IPV6_RTHDR) { 2329 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 2330 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 2331 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 2332 if (pktopt->ip6po_route.ro_rt) { 2333 RTFREE(pktopt->ip6po_route.ro_rt); 2334 pktopt->ip6po_route.ro_rt = NULL; 2335 } 2336 } 2337 if (optname == -1 || optname == IPV6_DSTOPTS) { 2338 if (pktopt->ip6po_dest2) 2339 free(pktopt->ip6po_dest2, M_IP6OPT); 2340 pktopt->ip6po_dest2 = NULL; 2341 } 2342 } 2343 2344 #define PKTOPT_EXTHDRCPY(type) \ 2345 do {\ 2346 if (src->type) {\ 2347 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 2348 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 2349 if (dst->type == NULL && canwait == M_NOWAIT)\ 2350 goto bad;\ 2351 bcopy(src->type, dst->type, hlen);\ 2352 }\ 2353 } while (/*CONSTCOND*/ 0) 2354 2355 static int 2356 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) 2357 { 2358 if (dst == NULL || src == NULL) { 2359 printf("ip6_clearpktopts: invalid argument\n"); 2360 return (EINVAL); 2361 } 2362 2363 dst->ip6po_hlim = src->ip6po_hlim; 2364 dst->ip6po_tclass = src->ip6po_tclass; 2365 dst->ip6po_flags = src->ip6po_flags; 2366 if (src->ip6po_pktinfo) { 2367 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2368 M_IP6OPT, canwait); 2369 if (dst->ip6po_pktinfo == NULL) 2370 goto bad; 2371 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2372 } 2373 if (src->ip6po_nexthop) { 2374 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2375 M_IP6OPT, canwait); 2376 if (dst->ip6po_nexthop == NULL) 2377 goto bad; 2378 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 2379 src->ip6po_nexthop->sa_len); 2380 } 2381 PKTOPT_EXTHDRCPY(ip6po_hbh); 2382 PKTOPT_EXTHDRCPY(ip6po_dest1); 2383 PKTOPT_EXTHDRCPY(ip6po_dest2); 2384 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2385 return (0); 2386 2387 bad: 2388 ip6_clearpktopts(dst, -1); 2389 return (ENOBUFS); 2390 } 2391 #undef PKTOPT_EXTHDRCPY 2392 2393 struct ip6_pktopts * 2394 ip6_copypktopts(struct ip6_pktopts *src, int canwait) 2395 { 2396 int error; 2397 struct ip6_pktopts *dst; 2398 2399 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2400 if (dst == NULL) 2401 return (NULL); 2402 ip6_initpktopts(dst); 2403 2404 if ((error = copypktopts(dst, src, canwait)) != 0) { 2405 free(dst, M_IP6OPT); 2406 return (NULL); 2407 } 2408 2409 return (dst); 2410 } 2411 2412 void 2413 ip6_freepcbopts(struct ip6_pktopts *pktopt) 2414 { 2415 if (pktopt == NULL) 2416 return; 2417 2418 ip6_clearpktopts(pktopt, -1); 2419 2420 free(pktopt, M_IP6OPT); 2421 } 2422 2423 /* 2424 * Set the IP6 multicast options in response to user setsockopt(). 2425 */ 2426 static int 2427 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m) 2428 { 2429 int error = 0; 2430 u_int loop, ifindex; 2431 struct ipv6_mreq *mreq; 2432 struct ifnet *ifp; 2433 struct ip6_moptions *im6o = *im6op; 2434 struct route_in6 ro; 2435 struct in6_multi_mship *imm; 2436 2437 if (im6o == NULL) { 2438 /* 2439 * No multicast option buffer attached to the pcb; 2440 * allocate one and initialize to default values. 2441 */ 2442 im6o = (struct ip6_moptions *) 2443 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK); 2444 2445 if (im6o == NULL) 2446 return (ENOBUFS); 2447 *im6op = im6o; 2448 im6o->im6o_multicast_ifp = NULL; 2449 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2450 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 2451 LIST_INIT(&im6o->im6o_memberships); 2452 } 2453 2454 switch (optname) { 2455 2456 case IPV6_MULTICAST_IF: 2457 /* 2458 * Select the interface for outgoing multicast packets. 2459 */ 2460 if (m == NULL || m->m_len != sizeof(u_int)) { 2461 error = EINVAL; 2462 break; 2463 } 2464 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex)); 2465 if (ifindex < 0 || if_index < ifindex) { 2466 error = ENXIO; /* XXX EINVAL? */ 2467 break; 2468 } 2469 ifp = ifnet_byindex(ifindex); 2470 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2471 error = EADDRNOTAVAIL; 2472 break; 2473 } 2474 im6o->im6o_multicast_ifp = ifp; 2475 break; 2476 2477 case IPV6_MULTICAST_HOPS: 2478 { 2479 /* 2480 * Set the IP6 hoplimit for outgoing multicast packets. 2481 */ 2482 int optval; 2483 if (m == NULL || m->m_len != sizeof(int)) { 2484 error = EINVAL; 2485 break; 2486 } 2487 bcopy(mtod(m, u_int *), &optval, sizeof(optval)); 2488 if (optval < -1 || optval >= 256) 2489 error = EINVAL; 2490 else if (optval == -1) 2491 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2492 else 2493 im6o->im6o_multicast_hlim = optval; 2494 break; 2495 } 2496 2497 case IPV6_MULTICAST_LOOP: 2498 /* 2499 * Set the loopback flag for outgoing multicast packets. 2500 * Must be zero or one. 2501 */ 2502 if (m == NULL || m->m_len != sizeof(u_int)) { 2503 error = EINVAL; 2504 break; 2505 } 2506 bcopy(mtod(m, u_int *), &loop, sizeof(loop)); 2507 if (loop > 1) { 2508 error = EINVAL; 2509 break; 2510 } 2511 im6o->im6o_multicast_loop = loop; 2512 break; 2513 2514 case IPV6_JOIN_GROUP: 2515 /* 2516 * Add a multicast group membership. 2517 * Group must be a valid IP6 multicast address. 2518 */ 2519 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2520 error = EINVAL; 2521 break; 2522 } 2523 mreq = mtod(m, struct ipv6_mreq *); 2524 2525 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2526 /* 2527 * We use the unspecified address to specify to accept 2528 * all multicast addresses. Only super user is allowed 2529 * to do this. 2530 */ 2531 /* XXX-BZ might need a better PRIV_NETINET_x for this */ 2532 error = priv_check(curthread, PRIV_NETINET_MROUTE); 2533 if (error) 2534 break; 2535 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2536 error = EINVAL; 2537 break; 2538 } 2539 2540 /* 2541 * If no interface was explicitly specified, choose an 2542 * appropriate one according to the given multicast address. 2543 */ 2544 if (mreq->ipv6mr_interface == 0) { 2545 struct sockaddr_in6 *dst; 2546 2547 /* 2548 * Look up the routing table for the 2549 * address, and choose the outgoing interface. 2550 * XXX: is it a good approach? 2551 */ 2552 ro.ro_rt = NULL; 2553 dst = (struct sockaddr_in6 *)&ro.ro_dst; 2554 bzero(dst, sizeof(*dst)); 2555 dst->sin6_family = AF_INET6; 2556 dst->sin6_len = sizeof(*dst); 2557 dst->sin6_addr = mreq->ipv6mr_multiaddr; 2558 rtalloc((struct route *)&ro); 2559 if (ro.ro_rt == NULL) { 2560 error = EADDRNOTAVAIL; 2561 break; 2562 } 2563 ifp = ro.ro_rt->rt_ifp; 2564 RTFREE(ro.ro_rt); 2565 } else { 2566 /* 2567 * If the interface is specified, validate it. 2568 */ 2569 if (mreq->ipv6mr_interface < 0 || 2570 if_index < mreq->ipv6mr_interface) { 2571 error = ENXIO; /* XXX EINVAL? */ 2572 break; 2573 } 2574 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2575 if (!ifp) { 2576 error = ENXIO; /* XXX EINVAL? */ 2577 break; 2578 } 2579 } 2580 2581 /* 2582 * See if we found an interface, and confirm that it 2583 * supports multicast 2584 */ 2585 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2586 error = EADDRNOTAVAIL; 2587 break; 2588 } 2589 2590 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) { 2591 error = EADDRNOTAVAIL; /* XXX: should not happen */ 2592 break; 2593 } 2594 2595 /* 2596 * See if the membership already exists. 2597 */ 2598 for (imm = im6o->im6o_memberships.lh_first; 2599 imm != NULL; imm = imm->i6mm_chain.le_next) 2600 if (imm->i6mm_maddr->in6m_ifp == ifp && 2601 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2602 &mreq->ipv6mr_multiaddr)) 2603 break; 2604 if (imm != NULL) { 2605 error = EADDRINUSE; 2606 break; 2607 } 2608 /* 2609 * Everything looks good; add a new record to the multicast 2610 * address list for the given interface. 2611 */ 2612 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0); 2613 if (imm == NULL) 2614 break; 2615 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2616 break; 2617 2618 case IPV6_LEAVE_GROUP: 2619 /* 2620 * Drop a multicast group membership. 2621 * Group must be a valid IP6 multicast address. 2622 */ 2623 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2624 error = EINVAL; 2625 break; 2626 } 2627 mreq = mtod(m, struct ipv6_mreq *); 2628 2629 /* 2630 * If an interface address was specified, get a pointer 2631 * to its ifnet structure. 2632 */ 2633 if (mreq->ipv6mr_interface < 0 || 2634 if_index < mreq->ipv6mr_interface) { 2635 error = ENXIO; /* XXX EINVAL? */ 2636 break; 2637 } 2638 if (mreq->ipv6mr_interface == 0) 2639 ifp = NULL; 2640 else 2641 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2642 2643 /* Fill in the scope zone ID */ 2644 if (ifp) { 2645 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) { 2646 /* XXX: should not happen */ 2647 error = EADDRNOTAVAIL; 2648 break; 2649 } 2650 } else if (mreq->ipv6mr_interface != 0) { 2651 /* 2652 * This case happens when the (positive) index is in 2653 * the valid range, but the corresponding interface has 2654 * been detached dynamically (XXX). 2655 */ 2656 error = EADDRNOTAVAIL; 2657 break; 2658 } else { /* ipv6mr_interface == 0 */ 2659 struct sockaddr_in6 sa6_mc; 2660 2661 /* 2662 * The API spec says as follows: 2663 * If the interface index is specified as 0, the 2664 * system may choose a multicast group membership to 2665 * drop by matching the multicast address only. 2666 * On the other hand, we cannot disambiguate the scope 2667 * zone unless an interface is provided. Thus, we 2668 * check if there's ambiguity with the default scope 2669 * zone as the last resort. 2670 */ 2671 bzero(&sa6_mc, sizeof(sa6_mc)); 2672 sa6_mc.sin6_family = AF_INET6; 2673 sa6_mc.sin6_len = sizeof(sa6_mc); 2674 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr; 2675 error = sa6_embedscope(&sa6_mc, ip6_use_defzone); 2676 if (error != 0) 2677 break; 2678 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr; 2679 } 2680 2681 /* 2682 * Find the membership in the membership list. 2683 */ 2684 for (imm = im6o->im6o_memberships.lh_first; 2685 imm != NULL; imm = imm->i6mm_chain.le_next) { 2686 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) && 2687 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2688 &mreq->ipv6mr_multiaddr)) 2689 break; 2690 } 2691 if (imm == NULL) { 2692 /* Unable to resolve interface */ 2693 error = EADDRNOTAVAIL; 2694 break; 2695 } 2696 /* 2697 * Give up the multicast address record to which the 2698 * membership points. 2699 */ 2700 LIST_REMOVE(imm, i6mm_chain); 2701 in6_delmulti(imm->i6mm_maddr); 2702 free(imm, M_IP6MADDR); 2703 break; 2704 2705 default: 2706 error = EOPNOTSUPP; 2707 break; 2708 } 2709 2710 /* 2711 * If all options have default values, no need to keep the mbuf. 2712 */ 2713 if (im6o->im6o_multicast_ifp == NULL && 2714 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2715 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2716 im6o->im6o_memberships.lh_first == NULL) { 2717 free(*im6op, M_IP6MOPTS); 2718 *im6op = NULL; 2719 } 2720 2721 return (error); 2722 } 2723 2724 /* 2725 * Return the IP6 multicast options in response to user getsockopt(). 2726 */ 2727 static int 2728 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp) 2729 { 2730 u_int *hlim, *loop, *ifindex; 2731 2732 *mp = m_get(M_WAIT, MT_HEADER); /* XXX */ 2733 2734 switch (optname) { 2735 2736 case IPV6_MULTICAST_IF: 2737 ifindex = mtod(*mp, u_int *); 2738 (*mp)->m_len = sizeof(u_int); 2739 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2740 *ifindex = 0; 2741 else 2742 *ifindex = im6o->im6o_multicast_ifp->if_index; 2743 return (0); 2744 2745 case IPV6_MULTICAST_HOPS: 2746 hlim = mtod(*mp, u_int *); 2747 (*mp)->m_len = sizeof(u_int); 2748 if (im6o == NULL) 2749 *hlim = ip6_defmcasthlim; 2750 else 2751 *hlim = im6o->im6o_multicast_hlim; 2752 return (0); 2753 2754 case IPV6_MULTICAST_LOOP: 2755 loop = mtod(*mp, u_int *); 2756 (*mp)->m_len = sizeof(u_int); 2757 if (im6o == NULL) 2758 *loop = ip6_defmcasthlim; 2759 else 2760 *loop = im6o->im6o_multicast_loop; 2761 return (0); 2762 2763 default: 2764 return (EOPNOTSUPP); 2765 } 2766 } 2767 2768 /* 2769 * Discard the IP6 multicast options. 2770 */ 2771 void 2772 ip6_freemoptions(struct ip6_moptions *im6o) 2773 { 2774 struct in6_multi_mship *imm; 2775 2776 if (im6o == NULL) 2777 return; 2778 2779 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2780 LIST_REMOVE(imm, i6mm_chain); 2781 if (imm->i6mm_maddr) 2782 in6_delmulti(imm->i6mm_maddr); 2783 free(imm, M_IP6MADDR); 2784 } 2785 free(im6o, M_IP6MOPTS); 2786 } 2787 2788 /* 2789 * Set IPv6 outgoing packet options based on advanced API. 2790 */ 2791 int 2792 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, 2793 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto) 2794 { 2795 struct cmsghdr *cm = 0; 2796 2797 if (control == NULL || opt == NULL) 2798 return (EINVAL); 2799 2800 ip6_initpktopts(opt); 2801 if (stickyopt) { 2802 int error; 2803 2804 /* 2805 * If stickyopt is provided, make a local copy of the options 2806 * for this particular packet, then override them by ancillary 2807 * objects. 2808 * XXX: copypktopts() does not copy the cached route to a next 2809 * hop (if any). This is not very good in terms of efficiency, 2810 * but we can allow this since this option should be rarely 2811 * used. 2812 */ 2813 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) 2814 return (error); 2815 } 2816 2817 /* 2818 * XXX: Currently, we assume all the optional information is stored 2819 * in a single mbuf. 2820 */ 2821 if (control->m_next) 2822 return (EINVAL); 2823 2824 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2825 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2826 int error; 2827 2828 if (control->m_len < CMSG_LEN(0)) 2829 return (EINVAL); 2830 2831 cm = mtod(control, struct cmsghdr *); 2832 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2833 return (EINVAL); 2834 if (cm->cmsg_level != IPPROTO_IPV6) 2835 continue; 2836 2837 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), 2838 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); 2839 if (error) 2840 return (error); 2841 } 2842 2843 return (0); 2844 } 2845 2846 /* 2847 * Set a particular packet option, as a sticky option or an ancillary data 2848 * item. "len" can be 0 only when it's a sticky option. 2849 * We have 4 cases of combination of "sticky" and "cmsg": 2850 * "sticky=0, cmsg=0": impossible 2851 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data 2852 * "sticky=1, cmsg=0": RFC3542 socket option 2853 * "sticky=1, cmsg=1": RFC2292 socket option 2854 */ 2855 static int 2856 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, 2857 struct ucred *cred, int sticky, int cmsg, int uproto) 2858 { 2859 int minmtupolicy, preftemp; 2860 int error; 2861 2862 if (!sticky && !cmsg) { 2863 #ifdef DIAGNOSTIC 2864 printf("ip6_setpktopt: impossible case\n"); 2865 #endif 2866 return (EINVAL); 2867 } 2868 2869 /* 2870 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2871 * not be specified in the context of RFC3542. Conversely, 2872 * RFC3542 types should not be specified in the context of RFC2292. 2873 */ 2874 if (!cmsg) { 2875 switch (optname) { 2876 case IPV6_2292PKTINFO: 2877 case IPV6_2292HOPLIMIT: 2878 case IPV6_2292NEXTHOP: 2879 case IPV6_2292HOPOPTS: 2880 case IPV6_2292DSTOPTS: 2881 case IPV6_2292RTHDR: 2882 case IPV6_2292PKTOPTIONS: 2883 return (ENOPROTOOPT); 2884 } 2885 } 2886 if (sticky && cmsg) { 2887 switch (optname) { 2888 case IPV6_PKTINFO: 2889 case IPV6_HOPLIMIT: 2890 case IPV6_NEXTHOP: 2891 case IPV6_HOPOPTS: 2892 case IPV6_DSTOPTS: 2893 case IPV6_RTHDRDSTOPTS: 2894 case IPV6_RTHDR: 2895 case IPV6_USE_MIN_MTU: 2896 case IPV6_DONTFRAG: 2897 case IPV6_TCLASS: 2898 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */ 2899 return (ENOPROTOOPT); 2900 } 2901 } 2902 2903 switch (optname) { 2904 case IPV6_2292PKTINFO: 2905 case IPV6_PKTINFO: 2906 { 2907 struct ifnet *ifp = NULL; 2908 struct in6_pktinfo *pktinfo; 2909 2910 if (len != sizeof(struct in6_pktinfo)) 2911 return (EINVAL); 2912 2913 pktinfo = (struct in6_pktinfo *)buf; 2914 2915 /* 2916 * An application can clear any sticky IPV6_PKTINFO option by 2917 * doing a "regular" setsockopt with ipi6_addr being 2918 * in6addr_any and ipi6_ifindex being zero. 2919 * [RFC 3542, Section 6] 2920 */ 2921 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 2922 pktinfo->ipi6_ifindex == 0 && 2923 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2924 ip6_clearpktopts(opt, optname); 2925 break; 2926 } 2927 2928 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 2929 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 2930 return (EINVAL); 2931 } 2932 2933 /* validate the interface index if specified. */ 2934 if (pktinfo->ipi6_ifindex > if_index || 2935 pktinfo->ipi6_ifindex < 0) { 2936 return (ENXIO); 2937 } 2938 if (pktinfo->ipi6_ifindex) { 2939 ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 2940 if (ifp == NULL) 2941 return (ENXIO); 2942 } 2943 2944 /* 2945 * We store the address anyway, and let in6_selectsrc() 2946 * validate the specified address. This is because ipi6_addr 2947 * may not have enough information about its scope zone, and 2948 * we may need additional information (such as outgoing 2949 * interface or the scope zone of a destination address) to 2950 * disambiguate the scope. 2951 * XXX: the delay of the validation may confuse the 2952 * application when it is used as a sticky option. 2953 */ 2954 if (opt->ip6po_pktinfo == NULL) { 2955 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 2956 M_IP6OPT, M_NOWAIT); 2957 if (opt->ip6po_pktinfo == NULL) 2958 return (ENOBUFS); 2959 } 2960 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 2961 break; 2962 } 2963 2964 case IPV6_2292HOPLIMIT: 2965 case IPV6_HOPLIMIT: 2966 { 2967 int *hlimp; 2968 2969 /* 2970 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 2971 * to simplify the ordering among hoplimit options. 2972 */ 2973 if (optname == IPV6_HOPLIMIT && sticky) 2974 return (ENOPROTOOPT); 2975 2976 if (len != sizeof(int)) 2977 return (EINVAL); 2978 hlimp = (int *)buf; 2979 if (*hlimp < -1 || *hlimp > 255) 2980 return (EINVAL); 2981 2982 opt->ip6po_hlim = *hlimp; 2983 break; 2984 } 2985 2986 case IPV6_TCLASS: 2987 { 2988 int tclass; 2989 2990 if (len != sizeof(int)) 2991 return (EINVAL); 2992 tclass = *(int *)buf; 2993 if (tclass < -1 || tclass > 255) 2994 return (EINVAL); 2995 2996 opt->ip6po_tclass = tclass; 2997 break; 2998 } 2999 3000 case IPV6_2292NEXTHOP: 3001 case IPV6_NEXTHOP: 3002 if (cred != NULL) { 3003 error = priv_check_cred(cred, 3004 PRIV_NETINET_SETHDROPTS, 0); 3005 if (error) 3006 return (error); 3007 } 3008 3009 if (len == 0) { /* just remove the option */ 3010 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3011 break; 3012 } 3013 3014 /* check if cmsg_len is large enough for sa_len */ 3015 if (len < sizeof(struct sockaddr) || len < *buf) 3016 return (EINVAL); 3017 3018 switch (((struct sockaddr *)buf)->sa_family) { 3019 case AF_INET6: 3020 { 3021 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 3022 int error; 3023 3024 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 3025 return (EINVAL); 3026 3027 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 3028 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 3029 return (EINVAL); 3030 } 3031 if ((error = sa6_embedscope(sa6, ip6_use_defzone)) 3032 != 0) { 3033 return (error); 3034 } 3035 break; 3036 } 3037 case AF_LINK: /* should eventually be supported */ 3038 default: 3039 return (EAFNOSUPPORT); 3040 } 3041 3042 /* turn off the previous option, then set the new option. */ 3043 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3044 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); 3045 if (opt->ip6po_nexthop == NULL) 3046 return (ENOBUFS); 3047 bcopy(buf, opt->ip6po_nexthop, *buf); 3048 break; 3049 3050 case IPV6_2292HOPOPTS: 3051 case IPV6_HOPOPTS: 3052 { 3053 struct ip6_hbh *hbh; 3054 int hbhlen; 3055 3056 /* 3057 * XXX: We don't allow a non-privileged user to set ANY HbH 3058 * options, since per-option restriction has too much 3059 * overhead. 3060 */ 3061 if (cred != NULL) { 3062 error = priv_check_cred(cred, 3063 PRIV_NETINET_SETHDROPTS, 0); 3064 if (error) 3065 return (error); 3066 } 3067 3068 if (len == 0) { 3069 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3070 break; /* just remove the option */ 3071 } 3072 3073 /* message length validation */ 3074 if (len < sizeof(struct ip6_hbh)) 3075 return (EINVAL); 3076 hbh = (struct ip6_hbh *)buf; 3077 hbhlen = (hbh->ip6h_len + 1) << 3; 3078 if (len != hbhlen) 3079 return (EINVAL); 3080 3081 /* turn off the previous option, then set the new option. */ 3082 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3083 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); 3084 if (opt->ip6po_hbh == NULL) 3085 return (ENOBUFS); 3086 bcopy(hbh, opt->ip6po_hbh, hbhlen); 3087 3088 break; 3089 } 3090 3091 case IPV6_2292DSTOPTS: 3092 case IPV6_DSTOPTS: 3093 case IPV6_RTHDRDSTOPTS: 3094 { 3095 struct ip6_dest *dest, **newdest = NULL; 3096 int destlen; 3097 3098 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */ 3099 error = priv_check_cred(cred, 3100 PRIV_NETINET_SETHDROPTS, 0); 3101 if (error) 3102 return (error); 3103 } 3104 3105 if (len == 0) { 3106 ip6_clearpktopts(opt, optname); 3107 break; /* just remove the option */ 3108 } 3109 3110 /* message length validation */ 3111 if (len < sizeof(struct ip6_dest)) 3112 return (EINVAL); 3113 dest = (struct ip6_dest *)buf; 3114 destlen = (dest->ip6d_len + 1) << 3; 3115 if (len != destlen) 3116 return (EINVAL); 3117 3118 /* 3119 * Determine the position that the destination options header 3120 * should be inserted; before or after the routing header. 3121 */ 3122 switch (optname) { 3123 case IPV6_2292DSTOPTS: 3124 /* 3125 * The old advacned API is ambiguous on this point. 3126 * Our approach is to determine the position based 3127 * according to the existence of a routing header. 3128 * Note, however, that this depends on the order of the 3129 * extension headers in the ancillary data; the 1st 3130 * part of the destination options header must appear 3131 * before the routing header in the ancillary data, 3132 * too. 3133 * RFC3542 solved the ambiguity by introducing 3134 * separate ancillary data or option types. 3135 */ 3136 if (opt->ip6po_rthdr == NULL) 3137 newdest = &opt->ip6po_dest1; 3138 else 3139 newdest = &opt->ip6po_dest2; 3140 break; 3141 case IPV6_RTHDRDSTOPTS: 3142 newdest = &opt->ip6po_dest1; 3143 break; 3144 case IPV6_DSTOPTS: 3145 newdest = &opt->ip6po_dest2; 3146 break; 3147 } 3148 3149 /* turn off the previous option, then set the new option. */ 3150 ip6_clearpktopts(opt, optname); 3151 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); 3152 if (*newdest == NULL) 3153 return (ENOBUFS); 3154 bcopy(dest, *newdest, destlen); 3155 3156 break; 3157 } 3158 3159 case IPV6_2292RTHDR: 3160 case IPV6_RTHDR: 3161 { 3162 struct ip6_rthdr *rth; 3163 int rthlen; 3164 3165 if (len == 0) { 3166 ip6_clearpktopts(opt, IPV6_RTHDR); 3167 break; /* just remove the option */ 3168 } 3169 3170 /* message length validation */ 3171 if (len < sizeof(struct ip6_rthdr)) 3172 return (EINVAL); 3173 rth = (struct ip6_rthdr *)buf; 3174 rthlen = (rth->ip6r_len + 1) << 3; 3175 if (len != rthlen) 3176 return (EINVAL); 3177 3178 switch (rth->ip6r_type) { 3179 case IPV6_RTHDR_TYPE_0: 3180 if (rth->ip6r_len == 0) /* must contain one addr */ 3181 return (EINVAL); 3182 if (rth->ip6r_len % 2) /* length must be even */ 3183 return (EINVAL); 3184 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 3185 return (EINVAL); 3186 break; 3187 default: 3188 return (EINVAL); /* not supported */ 3189 } 3190 3191 /* turn off the previous option */ 3192 ip6_clearpktopts(opt, IPV6_RTHDR); 3193 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); 3194 if (opt->ip6po_rthdr == NULL) 3195 return (ENOBUFS); 3196 bcopy(rth, opt->ip6po_rthdr, rthlen); 3197 3198 break; 3199 } 3200 3201 case IPV6_USE_MIN_MTU: 3202 if (len != sizeof(int)) 3203 return (EINVAL); 3204 minmtupolicy = *(int *)buf; 3205 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 3206 minmtupolicy != IP6PO_MINMTU_DISABLE && 3207 minmtupolicy != IP6PO_MINMTU_ALL) { 3208 return (EINVAL); 3209 } 3210 opt->ip6po_minmtu = minmtupolicy; 3211 break; 3212 3213 case IPV6_DONTFRAG: 3214 if (len != sizeof(int)) 3215 return (EINVAL); 3216 3217 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 3218 /* 3219 * we ignore this option for TCP sockets. 3220 * (RFC3542 leaves this case unspecified.) 3221 */ 3222 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 3223 } else 3224 opt->ip6po_flags |= IP6PO_DONTFRAG; 3225 break; 3226 3227 case IPV6_PREFER_TEMPADDR: 3228 if (len != sizeof(int)) 3229 return (EINVAL); 3230 preftemp = *(int *)buf; 3231 if (preftemp != IP6PO_TEMPADDR_SYSTEM && 3232 preftemp != IP6PO_TEMPADDR_NOTPREFER && 3233 preftemp != IP6PO_TEMPADDR_PREFER) { 3234 return (EINVAL); 3235 } 3236 opt->ip6po_prefer_tempaddr = preftemp; 3237 break; 3238 3239 default: 3240 return (ENOPROTOOPT); 3241 } /* end of switch */ 3242 3243 return (0); 3244 } 3245 3246 /* 3247 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 3248 * packet to the input queue of a specified interface. Note that this 3249 * calls the output routine of the loopback "driver", but with an interface 3250 * pointer that might NOT be &loif -- easier than replicating that code here. 3251 */ 3252 void 3253 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst) 3254 { 3255 struct mbuf *copym; 3256 struct ip6_hdr *ip6; 3257 3258 copym = m_copy(m, 0, M_COPYALL); 3259 if (copym == NULL) 3260 return; 3261 3262 /* 3263 * Make sure to deep-copy IPv6 header portion in case the data 3264 * is in an mbuf cluster, so that we can safely override the IPv6 3265 * header portion later. 3266 */ 3267 if ((copym->m_flags & M_EXT) != 0 || 3268 copym->m_len < sizeof(struct ip6_hdr)) { 3269 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 3270 if (copym == NULL) 3271 return; 3272 } 3273 3274 #ifdef DIAGNOSTIC 3275 if (copym->m_len < sizeof(*ip6)) { 3276 m_freem(copym); 3277 return; 3278 } 3279 #endif 3280 3281 ip6 = mtod(copym, struct ip6_hdr *); 3282 /* 3283 * clear embedded scope identifiers if necessary. 3284 * in6_clearscope will touch the addresses only when necessary. 3285 */ 3286 in6_clearscope(&ip6->ip6_src); 3287 in6_clearscope(&ip6->ip6_dst); 3288 3289 (void)if_simloop(ifp, copym, dst->sin6_family, 0); 3290 } 3291 3292 /* 3293 * Chop IPv6 header off from the payload. 3294 */ 3295 static int 3296 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) 3297 { 3298 struct mbuf *mh; 3299 struct ip6_hdr *ip6; 3300 3301 ip6 = mtod(m, struct ip6_hdr *); 3302 if (m->m_len > sizeof(*ip6)) { 3303 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 3304 if (mh == 0) { 3305 m_freem(m); 3306 return ENOBUFS; 3307 } 3308 M_MOVE_PKTHDR(mh, m); 3309 MH_ALIGN(mh, sizeof(*ip6)); 3310 m->m_len -= sizeof(*ip6); 3311 m->m_data += sizeof(*ip6); 3312 mh->m_next = m; 3313 m = mh; 3314 m->m_len = sizeof(*ip6); 3315 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 3316 } 3317 exthdrs->ip6e_ip6 = m; 3318 return 0; 3319 } 3320 3321 /* 3322 * Compute IPv6 extension header length. 3323 */ 3324 int 3325 ip6_optlen(struct in6pcb *in6p) 3326 { 3327 int len; 3328 3329 if (!in6p->in6p_outputopts) 3330 return 0; 3331 3332 len = 0; 3333 #define elen(x) \ 3334 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 3335 3336 len += elen(in6p->in6p_outputopts->ip6po_hbh); 3337 if (in6p->in6p_outputopts->ip6po_rthdr) 3338 /* dest1 is valid with rthdr only */ 3339 len += elen(in6p->in6p_outputopts->ip6po_dest1); 3340 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 3341 len += elen(in6p->in6p_outputopts->ip6po_dest2); 3342 return len; 3343 #undef elen 3344 } 3345