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