1 /* $FreeBSD$ */ 2 /* $KAME: ip6_output.c,v 1.180 2001/05/21 05:37:50 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 #include "opt_pfil_hooks.h" 73 74 #include <sys/param.h> 75 #include <sys/malloc.h> 76 #include <sys/mbuf.h> 77 #include <sys/proc.h> 78 #include <sys/errno.h> 79 #include <sys/protosw.h> 80 #include <sys/socket.h> 81 #include <sys/socketvar.h> 82 #include <sys/systm.h> 83 #include <sys/kernel.h> 84 85 #include <net/if.h> 86 #include <net/route.h> 87 #ifdef PFIL_HOOKS 88 #include <net/pfil.h> 89 #endif 90 91 #include <netinet/in.h> 92 #include <netinet/in_var.h> 93 #include <netinet6/in6_var.h> 94 #include <netinet/ip6.h> 95 #include <netinet/icmp6.h> 96 #include <netinet6/ip6_var.h> 97 #include <netinet/in_pcb.h> 98 #include <netinet6/nd6.h> 99 100 #ifdef IPSEC 101 #include <netinet6/ipsec.h> 102 #ifdef INET6 103 #include <netinet6/ipsec6.h> 104 #endif 105 #include <netkey/key.h> 106 #endif /* IPSEC */ 107 108 #include <netinet6/ip6_fw.h> 109 110 #include <net/net_osdep.h> 111 112 #include <netinet6/ip6protosw.h> 113 114 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options"); 115 116 struct ip6_exthdrs { 117 struct mbuf *ip6e_ip6; 118 struct mbuf *ip6e_hbh; 119 struct mbuf *ip6e_dest1; 120 struct mbuf *ip6e_rthdr; 121 struct mbuf *ip6e_dest2; 122 }; 123 124 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *, 125 struct socket *, struct sockopt *sopt)); 126 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *)); 127 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **)); 128 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int)); 129 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int, 130 struct ip6_frag **)); 131 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t)); 132 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *)); 133 134 extern struct ip6protosw inet6sw[]; 135 extern u_char ip6_protox[IPPROTO_MAX]; 136 137 /* 138 * IP6 output. The packet in mbuf chain m contains a skeletal IP6 139 * header (with pri, len, nxt, hlim, src, dst). 140 * This function may modify ver and hlim only. 141 * The mbuf chain containing the packet will be freed. 142 * The mbuf opt, if present, will not be freed. 143 * 144 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and 145 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one, 146 * which is rt_rmx.rmx_mtu. 147 */ 148 int 149 ip6_output(m0, opt, ro, flags, im6o, ifpp) 150 struct mbuf *m0; 151 struct ip6_pktopts *opt; 152 struct route_in6 *ro; 153 int flags; 154 struct ip6_moptions *im6o; 155 struct ifnet **ifpp; /* XXX: just for statistics */ 156 { 157 struct ip6_hdr *ip6, *mhip6; 158 struct ifnet *ifp, *origifp; 159 struct mbuf *m = m0; 160 int hlen, tlen, len, off; 161 struct route_in6 ip6route; 162 struct sockaddr_in6 *dst; 163 int error = 0; 164 struct in6_ifaddr *ia = NULL; 165 u_long mtu; 166 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; 167 struct ip6_exthdrs exthdrs; 168 struct in6_addr finaldst; 169 struct route_in6 *ro_pmtu = NULL; 170 int hdrsplit = 0; 171 int needipsec = 0; 172 #ifdef PFIL_HOOKS 173 struct packet_filter_hook *pfh; 174 struct mbuf *m1; 175 int rv; 176 #endif /* PFIL_HOOKS */ 177 #ifdef IPSEC 178 int needipsectun = 0; 179 struct socket *so; 180 struct secpolicy *sp = NULL; 181 182 /* for AH processing. stupid to have "socket" variable in IP layer... */ 183 so = ipsec_getsocket(m); 184 (void)ipsec_setsocket(m, NULL); 185 ip6 = mtod(m, struct ip6_hdr *); 186 #endif /* IPSEC */ 187 188 #define MAKE_EXTHDR(hp, mp) \ 189 do { \ 190 if (hp) { \ 191 struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 192 error = ip6_copyexthdr((mp), (caddr_t)(hp), \ 193 ((eh)->ip6e_len + 1) << 3); \ 194 if (error) \ 195 goto freehdrs; \ 196 } \ 197 } while (0) 198 199 bzero(&exthdrs, sizeof(exthdrs)); 200 201 if (opt) { 202 /* Hop-by-Hop options header */ 203 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 204 /* Destination options header(1st part) */ 205 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 206 /* Routing header */ 207 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 208 /* Destination options header(2nd part) */ 209 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 210 } 211 212 #ifdef IPSEC 213 /* get a security policy for this packet */ 214 if (so == NULL) 215 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error); 216 else 217 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 218 219 if (sp == NULL) { 220 ipsec6stat.out_inval++; 221 goto freehdrs; 222 } 223 224 error = 0; 225 226 /* check policy */ 227 switch (sp->policy) { 228 case IPSEC_POLICY_DISCARD: 229 /* 230 * This packet is just discarded. 231 */ 232 ipsec6stat.out_polvio++; 233 goto freehdrs; 234 235 case IPSEC_POLICY_BYPASS: 236 case IPSEC_POLICY_NONE: 237 /* no need to do IPsec. */ 238 needipsec = 0; 239 break; 240 241 case IPSEC_POLICY_IPSEC: 242 if (sp->req == NULL) { 243 /* acquire a policy */ 244 error = key_spdacquire(sp); 245 goto freehdrs; 246 } 247 needipsec = 1; 248 break; 249 250 case IPSEC_POLICY_ENTRUST: 251 default: 252 printf("ip6_output: Invalid policy found. %d\n", sp->policy); 253 } 254 #endif /* IPSEC */ 255 256 /* 257 * Calculate the total length of the extension header chain. 258 * Keep the length of the unfragmentable part for fragmentation. 259 */ 260 optlen = 0; 261 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len; 262 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len; 263 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len; 264 unfragpartlen = optlen + sizeof(struct ip6_hdr); 265 /* NOTE: we don't add AH/ESP length here. do that later. */ 266 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len; 267 268 /* 269 * If we need IPsec, or there is at least one extension header, 270 * separate IP6 header from the payload. 271 */ 272 if ((needipsec || optlen) && !hdrsplit) { 273 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 274 m = NULL; 275 goto freehdrs; 276 } 277 m = exthdrs.ip6e_ip6; 278 hdrsplit++; 279 } 280 281 /* adjust pointer */ 282 ip6 = mtod(m, struct ip6_hdr *); 283 284 /* adjust mbuf packet header length */ 285 m->m_pkthdr.len += optlen; 286 plen = m->m_pkthdr.len - sizeof(*ip6); 287 288 /* If this is a jumbo payload, insert a jumbo payload option. */ 289 if (plen > IPV6_MAXPACKET) { 290 if (!hdrsplit) { 291 if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 292 m = NULL; 293 goto freehdrs; 294 } 295 m = exthdrs.ip6e_ip6; 296 hdrsplit++; 297 } 298 /* adjust pointer */ 299 ip6 = mtod(m, struct ip6_hdr *); 300 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 301 goto freehdrs; 302 ip6->ip6_plen = 0; 303 } else 304 ip6->ip6_plen = htons(plen); 305 306 /* 307 * Concatenate headers and fill in next header fields. 308 * Here we have, on "m" 309 * IPv6 payload 310 * and we insert headers accordingly. Finally, we should be getting: 311 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 312 * 313 * during the header composing process, "m" points to IPv6 header. 314 * "mprev" points to an extension header prior to esp. 315 */ 316 { 317 u_char *nexthdrp = &ip6->ip6_nxt; 318 struct mbuf *mprev = m; 319 320 /* 321 * we treat dest2 specially. this makes IPsec processing 322 * much easier. 323 * 324 * result: IPv6 dest2 payload 325 * m and mprev will point to IPv6 header. 326 */ 327 if (exthdrs.ip6e_dest2) { 328 if (!hdrsplit) 329 panic("assumption failed: hdr not split"); 330 exthdrs.ip6e_dest2->m_next = m->m_next; 331 m->m_next = exthdrs.ip6e_dest2; 332 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 333 ip6->ip6_nxt = IPPROTO_DSTOPTS; 334 } 335 336 #define MAKE_CHAIN(m, mp, p, i)\ 337 do {\ 338 if (m) {\ 339 if (!hdrsplit) \ 340 panic("assumption failed: hdr not split"); \ 341 *mtod((m), u_char *) = *(p);\ 342 *(p) = (i);\ 343 p = mtod((m), u_char *);\ 344 (m)->m_next = (mp)->m_next;\ 345 (mp)->m_next = (m);\ 346 (mp) = (m);\ 347 }\ 348 } while (0) 349 /* 350 * result: IPv6 hbh dest1 rthdr dest2 payload 351 * m will point to IPv6 header. mprev will point to the 352 * extension header prior to dest2 (rthdr in the above case). 353 */ 354 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, 355 nexthdrp, IPPROTO_HOPOPTS); 356 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, 357 nexthdrp, IPPROTO_DSTOPTS); 358 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, 359 nexthdrp, IPPROTO_ROUTING); 360 361 #ifdef IPSEC 362 if (!needipsec) 363 goto skip_ipsec2; 364 365 /* 366 * pointers after IPsec headers are not valid any more. 367 * other pointers need a great care too. 368 * (IPsec routines should not mangle mbufs prior to AH/ESP) 369 */ 370 exthdrs.ip6e_dest2 = NULL; 371 372 { 373 struct ip6_rthdr *rh = NULL; 374 int segleft_org = 0; 375 struct ipsec_output_state state; 376 377 if (exthdrs.ip6e_rthdr) { 378 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 379 segleft_org = rh->ip6r_segleft; 380 rh->ip6r_segleft = 0; 381 } 382 383 bzero(&state, sizeof(state)); 384 state.m = m; 385 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags, 386 &needipsectun); 387 m = state.m; 388 if (error) { 389 /* mbuf is already reclaimed in ipsec6_output_trans. */ 390 m = NULL; 391 switch (error) { 392 case EHOSTUNREACH: 393 case ENETUNREACH: 394 case EMSGSIZE: 395 case ENOBUFS: 396 case ENOMEM: 397 break; 398 default: 399 printf("ip6_output (ipsec): error code %d\n", error); 400 /*fall through*/ 401 case ENOENT: 402 /* don't show these error codes to the user */ 403 error = 0; 404 break; 405 } 406 goto bad; 407 } 408 if (exthdrs.ip6e_rthdr) { 409 /* ah6_output doesn't modify mbuf chain */ 410 rh->ip6r_segleft = segleft_org; 411 } 412 } 413 skip_ipsec2:; 414 #endif 415 } 416 417 /* 418 * If there is a routing header, replace destination address field 419 * with the first hop of the routing header. 420 */ 421 if (exthdrs.ip6e_rthdr) { 422 struct ip6_rthdr *rh = 423 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, 424 struct ip6_rthdr *)); 425 struct ip6_rthdr0 *rh0; 426 427 finaldst = ip6->ip6_dst; 428 switch (rh->ip6r_type) { 429 case IPV6_RTHDR_TYPE_0: 430 rh0 = (struct ip6_rthdr0 *)rh; 431 ip6->ip6_dst = rh0->ip6r0_addr[0]; 432 bcopy((caddr_t)&rh0->ip6r0_addr[1], 433 (caddr_t)&rh0->ip6r0_addr[0], 434 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1) 435 ); 436 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst; 437 break; 438 default: /* is it possible? */ 439 error = EINVAL; 440 goto bad; 441 } 442 } 443 444 /* Source address validation */ 445 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 446 (flags & IPV6_DADOUTPUT) == 0) { 447 error = EOPNOTSUPP; 448 ip6stat.ip6s_badscope++; 449 goto bad; 450 } 451 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 452 error = EOPNOTSUPP; 453 ip6stat.ip6s_badscope++; 454 goto bad; 455 } 456 457 ip6stat.ip6s_localout++; 458 459 /* 460 * Route packet. 461 */ 462 if (ro == 0) { 463 ro = &ip6route; 464 bzero((caddr_t)ro, sizeof(*ro)); 465 } 466 ro_pmtu = ro; 467 if (opt && opt->ip6po_rthdr) 468 ro = &opt->ip6po_route; 469 dst = (struct sockaddr_in6 *)&ro->ro_dst; 470 /* 471 * If there is a cached route, 472 * check that it is to the same destination 473 * and is still up. If not, free it and try again. 474 */ 475 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 476 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) { 477 RTFREE(ro->ro_rt); 478 ro->ro_rt = (struct rtentry *)0; 479 } 480 if (ro->ro_rt == 0) { 481 bzero(dst, sizeof(*dst)); 482 dst->sin6_family = AF_INET6; 483 dst->sin6_len = sizeof(struct sockaddr_in6); 484 dst->sin6_addr = ip6->ip6_dst; 485 #ifdef SCOPEDROUTING 486 /* XXX: sin6_scope_id should already be fixed at this point */ 487 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr)) 488 dst->sin6_scope_id = ntohs(dst->sin6_addr.s6_addr16[1]); 489 #endif 490 } 491 #ifdef IPSEC 492 if (needipsec && needipsectun) { 493 struct ipsec_output_state state; 494 495 /* 496 * All the extension headers will become inaccessible 497 * (since they can be encrypted). 498 * Don't panic, we need no more updates to extension headers 499 * on inner IPv6 packet (since they are now encapsulated). 500 * 501 * IPv6 [ESP|AH] IPv6 [extension headers] payload 502 */ 503 bzero(&exthdrs, sizeof(exthdrs)); 504 exthdrs.ip6e_ip6 = m; 505 506 bzero(&state, sizeof(state)); 507 state.m = m; 508 state.ro = (struct route *)ro; 509 state.dst = (struct sockaddr *)dst; 510 511 error = ipsec6_output_tunnel(&state, sp, flags); 512 513 m = state.m; 514 ro = (struct route_in6 *)state.ro; 515 dst = (struct sockaddr_in6 *)state.dst; 516 if (error) { 517 /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 518 m0 = m = NULL; 519 m = NULL; 520 switch (error) { 521 case EHOSTUNREACH: 522 case ENETUNREACH: 523 case EMSGSIZE: 524 case ENOBUFS: 525 case ENOMEM: 526 break; 527 default: 528 printf("ip6_output (ipsec): error code %d\n", error); 529 /*fall through*/ 530 case ENOENT: 531 /* don't show these error codes to the user */ 532 error = 0; 533 break; 534 } 535 goto bad; 536 } 537 538 exthdrs.ip6e_ip6 = m; 539 } 540 #endif /*IPSEC*/ 541 542 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 543 /* Unicast */ 544 545 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 546 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) 547 /* xxx 548 * interface selection comes here 549 * if an interface is specified from an upper layer, 550 * ifp must point it. 551 */ 552 if (ro->ro_rt == 0) { 553 /* 554 * non-bsdi always clone routes, if parent is 555 * PRF_CLONING. 556 */ 557 rtalloc((struct route *)ro); 558 } 559 if (ro->ro_rt == 0) { 560 ip6stat.ip6s_noroute++; 561 error = EHOSTUNREACH; 562 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */ 563 goto bad; 564 } 565 ia = ifatoia6(ro->ro_rt->rt_ifa); 566 ifp = ro->ro_rt->rt_ifp; 567 ro->ro_rt->rt_use++; 568 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 569 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway; 570 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 571 572 in6_ifstat_inc(ifp, ifs6_out_request); 573 574 /* 575 * Check if the outgoing interface conflicts with 576 * the interface specified by ifi6_ifindex (if specified). 577 * Note that loopback interface is always okay. 578 * (this may happen when we are sending a packet to one of 579 * our own addresses.) 580 */ 581 if (opt && opt->ip6po_pktinfo 582 && opt->ip6po_pktinfo->ipi6_ifindex) { 583 if (!(ifp->if_flags & IFF_LOOPBACK) 584 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) { 585 ip6stat.ip6s_noroute++; 586 in6_ifstat_inc(ifp, ifs6_out_discard); 587 error = EHOSTUNREACH; 588 goto bad; 589 } 590 } 591 592 if (opt && opt->ip6po_hlim != -1) 593 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 594 } else { 595 /* Multicast */ 596 struct in6_multi *in6m; 597 598 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 599 600 /* 601 * See if the caller provided any multicast options 602 */ 603 ifp = NULL; 604 if (im6o != NULL) { 605 ip6->ip6_hlim = im6o->im6o_multicast_hlim; 606 if (im6o->im6o_multicast_ifp != NULL) 607 ifp = im6o->im6o_multicast_ifp; 608 } else 609 ip6->ip6_hlim = ip6_defmcasthlim; 610 611 /* 612 * See if the caller provided the outgoing interface 613 * as an ancillary data. 614 * Boundary check for ifindex is assumed to be already done. 615 */ 616 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex) 617 ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex); 618 619 /* 620 * If the destination is a node-local scope multicast, 621 * the packet should be loop-backed only. 622 */ 623 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) { 624 /* 625 * If the outgoing interface is already specified, 626 * it should be a loopback interface. 627 */ 628 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) { 629 ip6stat.ip6s_badscope++; 630 error = ENETUNREACH; /* XXX: better error? */ 631 /* XXX correct ifp? */ 632 in6_ifstat_inc(ifp, ifs6_out_discard); 633 goto bad; 634 } else { 635 ifp = &loif[0]; 636 } 637 } 638 639 if (opt && opt->ip6po_hlim != -1) 640 ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 641 642 /* 643 * If caller did not provide an interface lookup a 644 * default in the routing table. This is either a 645 * default for the speicfied group (i.e. a host 646 * route), or a multicast default (a route for the 647 * ``net'' ff00::/8). 648 */ 649 if (ifp == NULL) { 650 if (ro->ro_rt == 0) { 651 ro->ro_rt = rtalloc1((struct sockaddr *) 652 &ro->ro_dst, 0, 0UL); 653 } 654 if (ro->ro_rt == 0) { 655 ip6stat.ip6s_noroute++; 656 error = EHOSTUNREACH; 657 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */ 658 goto bad; 659 } 660 ia = ifatoia6(ro->ro_rt->rt_ifa); 661 ifp = ro->ro_rt->rt_ifp; 662 ro->ro_rt->rt_use++; 663 } 664 665 if ((flags & IPV6_FORWARDING) == 0) 666 in6_ifstat_inc(ifp, ifs6_out_request); 667 in6_ifstat_inc(ifp, ifs6_out_mcast); 668 669 /* 670 * Confirm that the outgoing interface supports multicast. 671 */ 672 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 673 ip6stat.ip6s_noroute++; 674 in6_ifstat_inc(ifp, ifs6_out_discard); 675 error = ENETUNREACH; 676 goto bad; 677 } 678 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); 679 if (in6m != NULL && 680 (im6o == NULL || im6o->im6o_multicast_loop)) { 681 /* 682 * If we belong to the destination multicast group 683 * on the outgoing interface, and the caller did not 684 * forbid loopback, loop back a copy. 685 */ 686 ip6_mloopback(ifp, m, dst); 687 } else { 688 /* 689 * If we are acting as a multicast router, perform 690 * multicast forwarding as if the packet had just 691 * arrived on the interface to which we are about 692 * to send. The multicast forwarding function 693 * recursively calls this function, using the 694 * IPV6_FORWARDING flag to prevent infinite recursion. 695 * 696 * Multicasts that are looped back by ip6_mloopback(), 697 * above, will be forwarded by the ip6_input() routine, 698 * if necessary. 699 */ 700 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 701 if (ip6_mforward(ip6, ifp, m) != 0) { 702 m_freem(m); 703 goto done; 704 } 705 } 706 } 707 /* 708 * Multicasts with a hoplimit of zero may be looped back, 709 * above, but must not be transmitted on a network. 710 * Also, multicasts addressed to the loopback interface 711 * are not sent -- the above call to ip6_mloopback() will 712 * loop back a copy if this host actually belongs to the 713 * destination group on the loopback interface. 714 */ 715 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) { 716 m_freem(m); 717 goto done; 718 } 719 } 720 721 /* 722 * Fill the outgoing inteface to tell the upper layer 723 * to increment per-interface statistics. 724 */ 725 if (ifpp) 726 *ifpp = ifp; 727 728 /* 729 * Determine path MTU. 730 */ 731 if (ro_pmtu != ro) { 732 /* The first hop and the final destination may differ. */ 733 struct sockaddr_in6 *sin6_fin = 734 (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 735 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 736 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr, 737 &finaldst))) { 738 RTFREE(ro_pmtu->ro_rt); 739 ro_pmtu->ro_rt = (struct rtentry *)0; 740 } 741 if (ro_pmtu->ro_rt == 0) { 742 bzero(sin6_fin, sizeof(*sin6_fin)); 743 sin6_fin->sin6_family = AF_INET6; 744 sin6_fin->sin6_len = sizeof(struct sockaddr_in6); 745 sin6_fin->sin6_addr = finaldst; 746 747 rtalloc((struct route *)ro_pmtu); 748 } 749 } 750 if (ro_pmtu->ro_rt != NULL) { 751 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu; 752 753 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu; 754 if (mtu > ifmtu || mtu == 0) { 755 /* 756 * The MTU on the route is larger than the MTU on 757 * the interface! This shouldn't happen, unless the 758 * MTU of the interface has been changed after the 759 * interface was brought up. Change the MTU in the 760 * route to match the interface MTU (as long as the 761 * field isn't locked). 762 * 763 * if MTU on the route is 0, we need to fix the MTU. 764 * this case happens with path MTU discovery timeouts. 765 */ 766 mtu = ifmtu; 767 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 768 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */ 769 } 770 } else { 771 mtu = nd_ifinfo[ifp->if_index].linkmtu; 772 } 773 774 /* 775 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting 776 */ 777 if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU) 778 mtu = IPV6_MMTU; 779 780 /* Fake scoped addresses */ 781 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 782 /* 783 * If source or destination address is a scoped address, and 784 * the packet is going to be sent to a loopback interface, 785 * we should keep the original interface. 786 */ 787 788 /* 789 * XXX: this is a very experimental and temporary solution. 790 * We eventually have sockaddr_in6 and use the sin6_scope_id 791 * field of the structure here. 792 * We rely on the consistency between two scope zone ids 793 * of source add destination, which should already be assured 794 * larger scopes than link will be supported in the near 795 * future. 796 */ 797 origifp = NULL; 798 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 799 origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1])); 800 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 801 origifp = ifnet_byindex(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 #ifndef SCOPEDROUTING 820 /* 821 * clear embedded scope identifiers if necessary. 822 * in6_clearscope will touch the addresses only when necessary. 823 */ 824 in6_clearscope(&ip6->ip6_src); 825 in6_clearscope(&ip6->ip6_dst); 826 #endif 827 828 /* 829 * Check with the firewall... 830 */ 831 if (ip6_fw_enable && ip6_fw_chk_ptr) { 832 u_short port = 0; 833 m->m_pkthdr.rcvif = NULL; /*XXX*/ 834 /* If ipfw says divert, we have to just drop packet */ 835 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) { 836 m_freem(m); 837 goto done; 838 } 839 if (!m) { 840 error = EACCES; 841 goto done; 842 } 843 } 844 845 /* 846 * If the outgoing packet contains a hop-by-hop options header, 847 * it must be examined and processed even by the source node. 848 * (RFC 2460, section 4.) 849 */ 850 if (exthdrs.ip6e_hbh) { 851 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 852 u_int32_t dummy1; /* XXX unused */ 853 u_int32_t dummy2; /* XXX unused */ 854 855 #ifdef DIAGNOSTIC 856 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) 857 panic("ip6e_hbh is not continuous"); 858 #endif 859 /* 860 * XXX: if we have to send an ICMPv6 error to the sender, 861 * we need the M_LOOP flag since icmp6_error() expects 862 * the IPv6 and the hop-by-hop options header are 863 * continuous unless the flag is set. 864 */ 865 m->m_flags |= M_LOOP; 866 m->m_pkthdr.rcvif = ifp; 867 if (ip6_process_hopopts(m, 868 (u_int8_t *)(hbh + 1), 869 ((hbh->ip6h_len + 1) << 3) - 870 sizeof(struct ip6_hbh), 871 &dummy1, &dummy2) < 0) { 872 /* m was already freed at this point */ 873 error = EINVAL;/* better error? */ 874 goto done; 875 } 876 m->m_flags &= ~M_LOOP; /* XXX */ 877 m->m_pkthdr.rcvif = NULL; 878 } 879 880 #ifdef PFIL_HOOKS 881 /* 882 * Run through list of hooks for output packets. 883 */ 884 m1 = m; 885 pfh = pfil_hook_get(PFIL_OUT, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh); 886 for (; pfh; pfh = pfh->pfil_link.tqe_next) 887 if (pfh->pfil_func) { 888 rv = pfh->pfil_func(ip6, sizeof(*ip6), ifp, 1, &m1); 889 if (rv) { 890 error = EHOSTUNREACH; 891 goto done; 892 } 893 m = m1; 894 if (m == NULL) 895 goto done; 896 ip6 = mtod(m, struct ip6_hdr *); 897 } 898 #endif /* PFIL_HOOKS */ 899 /* 900 * Send the packet to the outgoing interface. 901 * If necessary, do IPv6 fragmentation before sending. 902 */ 903 tlen = m->m_pkthdr.len; 904 if (tlen <= mtu 905 #ifdef notyet 906 /* 907 * On any link that cannot convey a 1280-octet packet in one piece, 908 * link-specific fragmentation and reassembly must be provided at 909 * a layer below IPv6. [RFC 2460, sec.5] 910 * Thus if the interface has ability of link-level fragmentation, 911 * we can just send the packet even if the packet size is 912 * larger than the link's MTU. 913 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet... 914 */ 915 916 || ifp->if_flags & IFF_FRAGMENTABLE 917 #endif 918 ) 919 { 920 /* Record statistics for this interface address. */ 921 if (ia && !(flags & IPV6_FORWARDING)) { 922 ia->ia_ifa.if_opackets++; 923 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 924 } 925 #ifdef IPSEC 926 /* clean ipsec history once it goes out of the node */ 927 ipsec_delaux(m); 928 #endif 929 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 930 goto done; 931 } else if (mtu < IPV6_MMTU) { 932 /* 933 * note that path MTU is never less than IPV6_MMTU 934 * (see icmp6_input). 935 */ 936 error = EMSGSIZE; 937 in6_ifstat_inc(ifp, ifs6_out_fragfail); 938 goto bad; 939 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */ 940 error = EMSGSIZE; 941 in6_ifstat_inc(ifp, ifs6_out_fragfail); 942 goto bad; 943 } else { 944 struct mbuf **mnext, *m_frgpart; 945 struct ip6_frag *ip6f; 946 u_int32_t id = htonl(ip6_id++); 947 u_char nextproto; 948 949 /* 950 * Too large for the destination or interface; 951 * fragment if possible. 952 * Must be able to put at least 8 bytes per fragment. 953 */ 954 hlen = unfragpartlen; 955 if (mtu > IPV6_MAXPACKET) 956 mtu = IPV6_MAXPACKET; 957 958 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 959 if (len < 8) { 960 error = EMSGSIZE; 961 in6_ifstat_inc(ifp, ifs6_out_fragfail); 962 goto bad; 963 } 964 965 mnext = &m->m_nextpkt; 966 967 /* 968 * Change the next header field of the last header in the 969 * unfragmentable part. 970 */ 971 if (exthdrs.ip6e_rthdr) { 972 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 973 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 974 } else if (exthdrs.ip6e_dest1) { 975 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 976 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 977 } else if (exthdrs.ip6e_hbh) { 978 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 979 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 980 } else { 981 nextproto = ip6->ip6_nxt; 982 ip6->ip6_nxt = IPPROTO_FRAGMENT; 983 } 984 985 /* 986 * Loop through length of segment after first fragment, 987 * make new header and copy data of each part and link onto chain. 988 */ 989 m0 = m; 990 for (off = hlen; off < tlen; off += len) { 991 MGETHDR(m, M_DONTWAIT, MT_HEADER); 992 if (!m) { 993 error = ENOBUFS; 994 ip6stat.ip6s_odropped++; 995 goto sendorfree; 996 } 997 m->m_pkthdr.rcvif = NULL; 998 m->m_flags = m0->m_flags & M_COPYFLAGS; 999 *mnext = m; 1000 mnext = &m->m_nextpkt; 1001 m->m_data += max_linkhdr; 1002 mhip6 = mtod(m, struct ip6_hdr *); 1003 *mhip6 = *ip6; 1004 m->m_len = sizeof(*mhip6); 1005 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 1006 if (error) { 1007 ip6stat.ip6s_odropped++; 1008 goto sendorfree; 1009 } 1010 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7)); 1011 if (off + len >= tlen) 1012 len = tlen - off; 1013 else 1014 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 1015 mhip6->ip6_plen = htons((u_short)(len + hlen + 1016 sizeof(*ip6f) - 1017 sizeof(struct ip6_hdr))); 1018 if ((m_frgpart = m_copy(m0, off, len)) == 0) { 1019 error = ENOBUFS; 1020 ip6stat.ip6s_odropped++; 1021 goto sendorfree; 1022 } 1023 m_cat(m, m_frgpart); 1024 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 1025 m->m_pkthdr.rcvif = (struct ifnet *)0; 1026 ip6f->ip6f_reserved = 0; 1027 ip6f->ip6f_ident = id; 1028 ip6f->ip6f_nxt = nextproto; 1029 ip6stat.ip6s_ofragments++; 1030 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 1031 } 1032 1033 in6_ifstat_inc(ifp, ifs6_out_fragok); 1034 } 1035 1036 /* 1037 * Remove leading garbages. 1038 */ 1039 sendorfree: 1040 m = m0->m_nextpkt; 1041 m0->m_nextpkt = 0; 1042 m_freem(m0); 1043 for (m0 = m; m; m = m0) { 1044 m0 = m->m_nextpkt; 1045 m->m_nextpkt = 0; 1046 if (error == 0) { 1047 /* Record statistics for this interface address. */ 1048 if (ia) { 1049 ia->ia_ifa.if_opackets++; 1050 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1051 } 1052 #ifdef IPSEC 1053 /* clean ipsec history once it goes out of the node */ 1054 ipsec_delaux(m); 1055 #endif 1056 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 1057 } else 1058 m_freem(m); 1059 } 1060 1061 if (error == 0) 1062 ip6stat.ip6s_fragmented++; 1063 1064 done: 1065 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */ 1066 RTFREE(ro->ro_rt); 1067 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) { 1068 RTFREE(ro_pmtu->ro_rt); 1069 } 1070 1071 #ifdef IPSEC 1072 if (sp != NULL) 1073 key_freesp(sp); 1074 #endif /* IPSEC */ 1075 1076 return(error); 1077 1078 freehdrs: 1079 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1080 m_freem(exthdrs.ip6e_dest1); 1081 m_freem(exthdrs.ip6e_rthdr); 1082 m_freem(exthdrs.ip6e_dest2); 1083 /* fall through */ 1084 bad: 1085 m_freem(m); 1086 goto done; 1087 } 1088 1089 static int 1090 ip6_copyexthdr(mp, hdr, hlen) 1091 struct mbuf **mp; 1092 caddr_t hdr; 1093 int hlen; 1094 { 1095 struct mbuf *m; 1096 1097 if (hlen > MCLBYTES) 1098 return(ENOBUFS); /* XXX */ 1099 1100 MGET(m, M_DONTWAIT, MT_DATA); 1101 if (!m) 1102 return(ENOBUFS); 1103 1104 if (hlen > MLEN) { 1105 MCLGET(m, M_DONTWAIT); 1106 if ((m->m_flags & M_EXT) == 0) { 1107 m_free(m); 1108 return(ENOBUFS); 1109 } 1110 } 1111 m->m_len = hlen; 1112 if (hdr) 1113 bcopy(hdr, mtod(m, caddr_t), hlen); 1114 1115 *mp = m; 1116 return(0); 1117 } 1118 1119 /* 1120 * Insert jumbo payload option. 1121 */ 1122 static int 1123 ip6_insert_jumboopt(exthdrs, plen) 1124 struct ip6_exthdrs *exthdrs; 1125 u_int32_t plen; 1126 { 1127 struct mbuf *mopt; 1128 u_char *optbuf; 1129 u_int32_t v; 1130 1131 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1132 1133 /* 1134 * If there is no hop-by-hop options header, allocate new one. 1135 * If there is one but it doesn't have enough space to store the 1136 * jumbo payload option, allocate a cluster to store the whole options. 1137 * Otherwise, use it to store the options. 1138 */ 1139 if (exthdrs->ip6e_hbh == 0) { 1140 MGET(mopt, M_DONTWAIT, MT_DATA); 1141 if (mopt == 0) 1142 return(ENOBUFS); 1143 mopt->m_len = JUMBOOPTLEN; 1144 optbuf = mtod(mopt, u_char *); 1145 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1146 exthdrs->ip6e_hbh = mopt; 1147 } else { 1148 struct ip6_hbh *hbh; 1149 1150 mopt = exthdrs->ip6e_hbh; 1151 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1152 /* 1153 * XXX assumption: 1154 * - exthdrs->ip6e_hbh is not referenced from places 1155 * other than exthdrs. 1156 * - exthdrs->ip6e_hbh is not an mbuf chain. 1157 */ 1158 int oldoptlen = mopt->m_len; 1159 struct mbuf *n; 1160 1161 /* 1162 * XXX: give up if the whole (new) hbh header does 1163 * not fit even in an mbuf cluster. 1164 */ 1165 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1166 return(ENOBUFS); 1167 1168 /* 1169 * As a consequence, we must always prepare a cluster 1170 * at this point. 1171 */ 1172 MGET(n, M_DONTWAIT, MT_DATA); 1173 if (n) { 1174 MCLGET(n, M_DONTWAIT); 1175 if ((n->m_flags & M_EXT) == 0) { 1176 m_freem(n); 1177 n = NULL; 1178 } 1179 } 1180 if (!n) 1181 return(ENOBUFS); 1182 n->m_len = oldoptlen + JUMBOOPTLEN; 1183 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1184 oldoptlen); 1185 optbuf = mtod(n, caddr_t) + oldoptlen; 1186 m_freem(mopt); 1187 mopt = exthdrs->ip6e_hbh = n; 1188 } else { 1189 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1190 mopt->m_len += JUMBOOPTLEN; 1191 } 1192 optbuf[0] = IP6OPT_PADN; 1193 optbuf[1] = 1; 1194 1195 /* 1196 * Adjust the header length according to the pad and 1197 * the jumbo payload option. 1198 */ 1199 hbh = mtod(mopt, struct ip6_hbh *); 1200 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1201 } 1202 1203 /* fill in the option. */ 1204 optbuf[2] = IP6OPT_JUMBO; 1205 optbuf[3] = 4; 1206 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1207 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1208 1209 /* finally, adjust the packet header length */ 1210 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1211 1212 return(0); 1213 #undef JUMBOOPTLEN 1214 } 1215 1216 /* 1217 * Insert fragment header and copy unfragmentable header portions. 1218 */ 1219 static int 1220 ip6_insertfraghdr(m0, m, hlen, frghdrp) 1221 struct mbuf *m0, *m; 1222 int hlen; 1223 struct ip6_frag **frghdrp; 1224 { 1225 struct mbuf *n, *mlast; 1226 1227 if (hlen > sizeof(struct ip6_hdr)) { 1228 n = m_copym(m0, sizeof(struct ip6_hdr), 1229 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1230 if (n == 0) 1231 return(ENOBUFS); 1232 m->m_next = n; 1233 } else 1234 n = m; 1235 1236 /* Search for the last mbuf of unfragmentable part. */ 1237 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1238 ; 1239 1240 if ((mlast->m_flags & M_EXT) == 0 && 1241 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1242 /* use the trailing space of the last mbuf for the fragment hdr */ 1243 *frghdrp = 1244 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len); 1245 mlast->m_len += sizeof(struct ip6_frag); 1246 m->m_pkthdr.len += sizeof(struct ip6_frag); 1247 } else { 1248 /* allocate a new mbuf for the fragment header */ 1249 struct mbuf *mfrg; 1250 1251 MGET(mfrg, M_DONTWAIT, MT_DATA); 1252 if (mfrg == 0) 1253 return(ENOBUFS); 1254 mfrg->m_len = sizeof(struct ip6_frag); 1255 *frghdrp = mtod(mfrg, struct ip6_frag *); 1256 mlast->m_next = mfrg; 1257 } 1258 1259 return(0); 1260 } 1261 1262 /* 1263 * IP6 socket option processing. 1264 */ 1265 int 1266 ip6_ctloutput(so, sopt) 1267 struct socket *so; 1268 struct sockopt *sopt; 1269 { 1270 int privileged; 1271 struct inpcb *in6p = sotoinpcb(so); 1272 int error, optval; 1273 int level, op, optname; 1274 int optlen; 1275 struct thread *td; 1276 1277 if (sopt) { 1278 level = sopt->sopt_level; 1279 op = sopt->sopt_dir; 1280 optname = sopt->sopt_name; 1281 optlen = sopt->sopt_valsize; 1282 td = sopt->sopt_td; 1283 } else { 1284 panic("ip6_ctloutput: arg soopt is NULL"); 1285 } 1286 error = optval = 0; 1287 1288 privileged = (td == 0 || suser_td(td)) ? 0 : 1; 1289 1290 if (level == IPPROTO_IPV6) { 1291 switch (op) { 1292 1293 case SOPT_SET: 1294 switch (optname) { 1295 case IPV6_PKTOPTIONS: 1296 { 1297 struct mbuf *m; 1298 1299 error = soopt_getm(sopt, &m); /* XXX */ 1300 if (error != NULL) 1301 break; 1302 error = soopt_mcopyin(sopt, m); /* XXX */ 1303 if (error != NULL) 1304 break; 1305 error = ip6_pcbopts(&in6p->in6p_outputopts, 1306 m, so, sopt); 1307 m_freem(m); /* XXX */ 1308 break; 1309 } 1310 1311 /* 1312 * Use of some Hop-by-Hop options or some 1313 * Destination options, might require special 1314 * privilege. That is, normal applications 1315 * (without special privilege) might be forbidden 1316 * from setting certain options in outgoing packets, 1317 * and might never see certain options in received 1318 * packets. [RFC 2292 Section 6] 1319 * KAME specific note: 1320 * KAME prevents non-privileged users from sending or 1321 * receiving ANY hbh/dst options in order to avoid 1322 * overhead of parsing options in the kernel. 1323 */ 1324 case IPV6_UNICAST_HOPS: 1325 case IPV6_CHECKSUM: 1326 case IPV6_FAITH: 1327 1328 case IPV6_V6ONLY: 1329 if (optlen != sizeof(int)) { 1330 error = EINVAL; 1331 break; 1332 } 1333 error = sooptcopyin(sopt, &optval, 1334 sizeof optval, sizeof optval); 1335 if (error) 1336 break; 1337 switch (optname) { 1338 1339 case IPV6_UNICAST_HOPS: 1340 if (optval < -1 || optval >= 256) 1341 error = EINVAL; 1342 else { 1343 /* -1 = kernel default */ 1344 in6p->in6p_hops = optval; 1345 1346 if ((in6p->in6p_vflag & 1347 INP_IPV4) != 0) 1348 in6p->inp_ip_ttl = optval; 1349 } 1350 break; 1351 #define OPTSET(bit) \ 1352 do { \ 1353 if (optval) \ 1354 in6p->in6p_flags |= (bit); \ 1355 else \ 1356 in6p->in6p_flags &= ~(bit); \ 1357 } while (0) 1358 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) 1359 1360 case IPV6_CHECKSUM: 1361 in6p->in6p_cksum = optval; 1362 break; 1363 1364 case IPV6_FAITH: 1365 OPTSET(IN6P_FAITH); 1366 break; 1367 1368 case IPV6_V6ONLY: 1369 /* 1370 * make setsockopt(IPV6_V6ONLY) 1371 * available only prior to bind(2). 1372 * see ipng mailing list, Jun 22 2001. 1373 */ 1374 if (in6p->in6p_lport || 1375 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 1376 { 1377 error = EINVAL; 1378 break; 1379 } 1380 /* 1381 * XXX: BINDV6ONLY should be integrated 1382 * into V6ONLY. 1383 */ 1384 OPTSET(IN6P_BINDV6ONLY); 1385 OPTSET(IN6P_IPV6_V6ONLY); 1386 break; 1387 } 1388 break; 1389 1390 case IPV6_PKTINFO: 1391 case IPV6_HOPLIMIT: 1392 case IPV6_HOPOPTS: 1393 case IPV6_DSTOPTS: 1394 case IPV6_RTHDR: 1395 /* RFC 2292 */ 1396 if (optlen != sizeof(int)) { 1397 error = EINVAL; 1398 break; 1399 } 1400 error = sooptcopyin(sopt, &optval, 1401 sizeof optval, sizeof optval); 1402 if (error) 1403 break; 1404 switch (optname) { 1405 case IPV6_PKTINFO: 1406 OPTSET(IN6P_PKTINFO); 1407 break; 1408 case IPV6_HOPLIMIT: 1409 OPTSET(IN6P_HOPLIMIT); 1410 break; 1411 case IPV6_HOPOPTS: 1412 /* 1413 * Check super-user privilege. 1414 * See comments for IPV6_RECVHOPOPTS. 1415 */ 1416 if (!privileged) 1417 return(EPERM); 1418 OPTSET(IN6P_HOPOPTS); 1419 break; 1420 case IPV6_DSTOPTS: 1421 if (!privileged) 1422 return(EPERM); 1423 OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1424 break; 1425 case IPV6_RTHDR: 1426 OPTSET(IN6P_RTHDR); 1427 break; 1428 } 1429 break; 1430 #undef OPTSET 1431 1432 case IPV6_MULTICAST_IF: 1433 case IPV6_MULTICAST_HOPS: 1434 case IPV6_MULTICAST_LOOP: 1435 case IPV6_JOIN_GROUP: 1436 case IPV6_LEAVE_GROUP: 1437 { 1438 struct mbuf *m; 1439 if (sopt->sopt_valsize > MLEN) { 1440 error = EMSGSIZE; 1441 break; 1442 } 1443 /* XXX */ 1444 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1445 if (m == 0) { 1446 error = ENOBUFS; 1447 break; 1448 } 1449 m->m_len = sopt->sopt_valsize; 1450 error = sooptcopyin(sopt, mtod(m, char *), 1451 m->m_len, m->m_len); 1452 error = ip6_setmoptions(sopt->sopt_name, 1453 &in6p->in6p_moptions, 1454 m); 1455 (void)m_free(m); 1456 } 1457 break; 1458 1459 case IPV6_PORTRANGE: 1460 error = sooptcopyin(sopt, &optval, 1461 sizeof optval, sizeof optval); 1462 if (error) 1463 break; 1464 1465 switch (optval) { 1466 case IPV6_PORTRANGE_DEFAULT: 1467 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1468 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1469 break; 1470 1471 case IPV6_PORTRANGE_HIGH: 1472 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1473 in6p->in6p_flags |= IN6P_HIGHPORT; 1474 break; 1475 1476 case IPV6_PORTRANGE_LOW: 1477 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1478 in6p->in6p_flags |= IN6P_LOWPORT; 1479 break; 1480 1481 default: 1482 error = EINVAL; 1483 break; 1484 } 1485 break; 1486 1487 #ifdef IPSEC 1488 case IPV6_IPSEC_POLICY: 1489 { 1490 caddr_t req = NULL; 1491 size_t len = 0; 1492 struct mbuf *m; 1493 1494 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1495 break; 1496 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1497 break; 1498 if (m) { 1499 req = mtod(m, caddr_t); 1500 len = m->m_len; 1501 } 1502 error = ipsec6_set_policy(in6p, optname, req, 1503 len, privileged); 1504 m_freem(m); 1505 } 1506 break; 1507 #endif /* KAME IPSEC */ 1508 1509 case IPV6_FW_ADD: 1510 case IPV6_FW_DEL: 1511 case IPV6_FW_FLUSH: 1512 case IPV6_FW_ZERO: 1513 { 1514 struct mbuf *m; 1515 struct mbuf **mp = &m; 1516 1517 if (ip6_fw_ctl_ptr == NULL) 1518 return EINVAL; 1519 /* XXX */ 1520 if ((error = soopt_getm(sopt, &m)) != 0) 1521 break; 1522 /* XXX */ 1523 if ((error = soopt_mcopyin(sopt, m)) != 0) 1524 break; 1525 error = (*ip6_fw_ctl_ptr)(optname, mp); 1526 m = *mp; 1527 } 1528 break; 1529 1530 default: 1531 error = ENOPROTOOPT; 1532 break; 1533 } 1534 break; 1535 1536 case SOPT_GET: 1537 switch (optname) { 1538 1539 case IPV6_PKTOPTIONS: 1540 if (in6p->in6p_options) { 1541 struct mbuf *m; 1542 m = m_copym(in6p->in6p_options, 1543 0, M_COPYALL, M_WAIT); 1544 error = soopt_mcopyout(sopt, m); 1545 if (error == 0) 1546 m_freem(m); 1547 } else 1548 sopt->sopt_valsize = 0; 1549 break; 1550 1551 case IPV6_UNICAST_HOPS: 1552 case IPV6_CHECKSUM: 1553 1554 case IPV6_FAITH: 1555 case IPV6_V6ONLY: 1556 case IPV6_PORTRANGE: 1557 switch (optname) { 1558 1559 case IPV6_UNICAST_HOPS: 1560 optval = in6p->in6p_hops; 1561 break; 1562 1563 case IPV6_CHECKSUM: 1564 optval = in6p->in6p_cksum; 1565 break; 1566 1567 case IPV6_FAITH: 1568 optval = OPTBIT(IN6P_FAITH); 1569 break; 1570 1571 case IPV6_V6ONLY: 1572 /* XXX: see the setopt case. */ 1573 optval = OPTBIT(IN6P_BINDV6ONLY); 1574 break; 1575 1576 case IPV6_PORTRANGE: 1577 { 1578 int flags; 1579 flags = in6p->in6p_flags; 1580 if (flags & IN6P_HIGHPORT) 1581 optval = IPV6_PORTRANGE_HIGH; 1582 else if (flags & IN6P_LOWPORT) 1583 optval = IPV6_PORTRANGE_LOW; 1584 else 1585 optval = 0; 1586 break; 1587 } 1588 } 1589 error = sooptcopyout(sopt, &optval, 1590 sizeof optval); 1591 break; 1592 1593 case IPV6_PKTINFO: 1594 case IPV6_HOPLIMIT: 1595 case IPV6_HOPOPTS: 1596 case IPV6_RTHDR: 1597 case IPV6_DSTOPTS: 1598 if (optname == IPV6_HOPOPTS || 1599 optname == IPV6_DSTOPTS || 1600 !privileged) 1601 return(EPERM); 1602 switch (optname) { 1603 case IPV6_PKTINFO: 1604 optval = OPTBIT(IN6P_PKTINFO); 1605 break; 1606 case IPV6_HOPLIMIT: 1607 optval = OPTBIT(IN6P_HOPLIMIT); 1608 break; 1609 case IPV6_HOPOPTS: 1610 if (!privileged) 1611 return(EPERM); 1612 optval = OPTBIT(IN6P_HOPOPTS); 1613 break; 1614 case IPV6_RTHDR: 1615 optval = OPTBIT(IN6P_RTHDR); 1616 break; 1617 case IPV6_DSTOPTS: 1618 if (!privileged) 1619 return(EPERM); 1620 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 1621 break; 1622 } 1623 error = sooptcopyout(sopt, &optval, 1624 sizeof optval); 1625 break; 1626 1627 case IPV6_MULTICAST_IF: 1628 case IPV6_MULTICAST_HOPS: 1629 case IPV6_MULTICAST_LOOP: 1630 case IPV6_JOIN_GROUP: 1631 case IPV6_LEAVE_GROUP: 1632 { 1633 struct mbuf *m; 1634 error = ip6_getmoptions(sopt->sopt_name, 1635 in6p->in6p_moptions, &m); 1636 if (error == 0) 1637 error = sooptcopyout(sopt, 1638 mtod(m, char *), m->m_len); 1639 m_freem(m); 1640 } 1641 break; 1642 1643 #ifdef IPSEC 1644 case IPV6_IPSEC_POLICY: 1645 { 1646 caddr_t req = NULL; 1647 size_t len = 0; 1648 struct mbuf *m = NULL; 1649 struct mbuf **mp = &m; 1650 1651 error = soopt_getm(sopt, &m); /* XXX */ 1652 if (error != NULL) 1653 break; 1654 error = soopt_mcopyin(sopt, m); /* XXX */ 1655 if (error != NULL) 1656 break; 1657 if (m) { 1658 req = mtod(m, caddr_t); 1659 len = m->m_len; 1660 } 1661 error = ipsec6_get_policy(in6p, req, len, mp); 1662 if (error == 0) 1663 error = soopt_mcopyout(sopt, m); /*XXX*/ 1664 if (error == 0 && m) 1665 m_freem(m); 1666 break; 1667 } 1668 #endif /* KAME IPSEC */ 1669 1670 case IPV6_FW_GET: 1671 { 1672 struct mbuf *m; 1673 struct mbuf **mp = &m; 1674 1675 if (ip6_fw_ctl_ptr == NULL) 1676 { 1677 return EINVAL; 1678 } 1679 error = (*ip6_fw_ctl_ptr)(optname, mp); 1680 if (error == 0) 1681 error = soopt_mcopyout(sopt, m); /* XXX */ 1682 if (error == 0 && m) 1683 m_freem(m); 1684 } 1685 break; 1686 1687 default: 1688 error = ENOPROTOOPT; 1689 break; 1690 } 1691 break; 1692 } 1693 } else { 1694 error = EINVAL; 1695 } 1696 return(error); 1697 } 1698 1699 /* 1700 * Set up IP6 options in pcb for insertion in output packets or 1701 * specifying behavior of outgoing packets. 1702 */ 1703 static int 1704 ip6_pcbopts(pktopt, m, so, sopt) 1705 struct ip6_pktopts **pktopt; 1706 struct mbuf *m; 1707 struct socket *so; 1708 struct sockopt *sopt; 1709 { 1710 struct ip6_pktopts *opt = *pktopt; 1711 int error = 0; 1712 struct thread *td = sopt->sopt_td; 1713 int priv = 0; 1714 1715 /* turn off any old options. */ 1716 if (opt) { 1717 #ifdef DIAGNOSTIC 1718 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 1719 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 1720 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 1721 printf("ip6_pcbopts: all specified options are cleared.\n"); 1722 #endif 1723 ip6_clearpktopts(opt, 1, -1); 1724 } else 1725 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 1726 *pktopt = NULL; 1727 1728 if (!m || m->m_len == 0) { 1729 /* 1730 * Only turning off any previous options. 1731 */ 1732 if (opt) 1733 free(opt, M_IP6OPT); 1734 return(0); 1735 } 1736 1737 /* set options specified by user. */ 1738 if (td && !suser_td(td)) 1739 priv = 1; 1740 if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) { 1741 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */ 1742 return(error); 1743 } 1744 *pktopt = opt; 1745 return(0); 1746 } 1747 1748 /* 1749 * initialize ip6_pktopts. beware that there are non-zero default values in 1750 * the struct. 1751 */ 1752 void 1753 init_ip6pktopts(opt) 1754 struct ip6_pktopts *opt; 1755 { 1756 1757 bzero(opt, sizeof(*opt)); 1758 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 1759 } 1760 1761 void 1762 ip6_clearpktopts(pktopt, needfree, optname) 1763 struct ip6_pktopts *pktopt; 1764 int needfree, optname; 1765 { 1766 if (pktopt == NULL) 1767 return; 1768 1769 if (optname == -1) { 1770 if (needfree && pktopt->ip6po_pktinfo) 1771 free(pktopt->ip6po_pktinfo, M_IP6OPT); 1772 pktopt->ip6po_pktinfo = NULL; 1773 } 1774 if (optname == -1) 1775 pktopt->ip6po_hlim = -1; 1776 if (optname == -1) { 1777 if (needfree && pktopt->ip6po_nexthop) 1778 free(pktopt->ip6po_nexthop, M_IP6OPT); 1779 pktopt->ip6po_nexthop = NULL; 1780 } 1781 if (optname == -1) { 1782 if (needfree && pktopt->ip6po_hbh) 1783 free(pktopt->ip6po_hbh, M_IP6OPT); 1784 pktopt->ip6po_hbh = NULL; 1785 } 1786 if (optname == -1) { 1787 if (needfree && pktopt->ip6po_dest1) 1788 free(pktopt->ip6po_dest1, M_IP6OPT); 1789 pktopt->ip6po_dest1 = NULL; 1790 } 1791 if (optname == -1) { 1792 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 1793 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 1794 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 1795 if (pktopt->ip6po_route.ro_rt) { 1796 RTFREE(pktopt->ip6po_route.ro_rt); 1797 pktopt->ip6po_route.ro_rt = NULL; 1798 } 1799 } 1800 if (optname == -1) { 1801 if (needfree && pktopt->ip6po_dest2) 1802 free(pktopt->ip6po_dest2, M_IP6OPT); 1803 pktopt->ip6po_dest2 = NULL; 1804 } 1805 } 1806 1807 #define PKTOPT_EXTHDRCPY(type) \ 1808 do {\ 1809 if (src->type) {\ 1810 int hlen =\ 1811 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 1812 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 1813 if (dst->type == NULL && canwait == M_NOWAIT)\ 1814 goto bad;\ 1815 bcopy(src->type, dst->type, hlen);\ 1816 }\ 1817 } while (0) 1818 1819 struct ip6_pktopts * 1820 ip6_copypktopts(src, canwait) 1821 struct ip6_pktopts *src; 1822 int canwait; 1823 { 1824 struct ip6_pktopts *dst; 1825 1826 if (src == NULL) { 1827 printf("ip6_clearpktopts: invalid argument\n"); 1828 return(NULL); 1829 } 1830 1831 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 1832 if (dst == NULL && canwait == M_NOWAIT) 1833 goto bad; 1834 bzero(dst, sizeof(*dst)); 1835 1836 dst->ip6po_hlim = src->ip6po_hlim; 1837 if (src->ip6po_pktinfo) { 1838 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 1839 M_IP6OPT, canwait); 1840 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT) 1841 goto bad; 1842 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 1843 } 1844 if (src->ip6po_nexthop) { 1845 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 1846 M_IP6OPT, canwait); 1847 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT) 1848 goto bad; 1849 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 1850 src->ip6po_nexthop->sa_len); 1851 } 1852 PKTOPT_EXTHDRCPY(ip6po_hbh); 1853 PKTOPT_EXTHDRCPY(ip6po_dest1); 1854 PKTOPT_EXTHDRCPY(ip6po_dest2); 1855 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 1856 return(dst); 1857 1858 bad: 1859 printf("ip6_copypktopts: copy failed"); 1860 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT); 1861 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT); 1862 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT); 1863 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT); 1864 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT); 1865 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT); 1866 return(NULL); 1867 } 1868 #undef PKTOPT_EXTHDRCPY 1869 1870 void 1871 ip6_freepcbopts(pktopt) 1872 struct ip6_pktopts *pktopt; 1873 { 1874 if (pktopt == NULL) 1875 return; 1876 1877 ip6_clearpktopts(pktopt, 1, -1); 1878 1879 free(pktopt, M_IP6OPT); 1880 } 1881 1882 /* 1883 * Set the IP6 multicast options in response to user setsockopt(). 1884 */ 1885 static int 1886 ip6_setmoptions(optname, im6op, m) 1887 int optname; 1888 struct ip6_moptions **im6op; 1889 struct mbuf *m; 1890 { 1891 int error = 0; 1892 u_int loop, ifindex; 1893 struct ipv6_mreq *mreq; 1894 struct ifnet *ifp; 1895 struct ip6_moptions *im6o = *im6op; 1896 struct route_in6 ro; 1897 struct sockaddr_in6 *dst; 1898 struct in6_multi_mship *imm; 1899 struct thread *td = curthread; /* XXX */ 1900 1901 if (im6o == NULL) { 1902 /* 1903 * No multicast option buffer attached to the pcb; 1904 * allocate one and initialize to default values. 1905 */ 1906 im6o = (struct ip6_moptions *) 1907 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK); 1908 1909 if (im6o == NULL) 1910 return(ENOBUFS); 1911 *im6op = im6o; 1912 im6o->im6o_multicast_ifp = NULL; 1913 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 1914 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 1915 LIST_INIT(&im6o->im6o_memberships); 1916 } 1917 1918 switch (optname) { 1919 1920 case IPV6_MULTICAST_IF: 1921 /* 1922 * Select the interface for outgoing multicast packets. 1923 */ 1924 if (m == NULL || m->m_len != sizeof(u_int)) { 1925 error = EINVAL; 1926 break; 1927 } 1928 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex)); 1929 if (ifindex < 0 || if_index < ifindex) { 1930 error = ENXIO; /* XXX EINVAL? */ 1931 break; 1932 } 1933 ifp = ifnet_byindex(ifindex); 1934 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1935 error = EADDRNOTAVAIL; 1936 break; 1937 } 1938 im6o->im6o_multicast_ifp = ifp; 1939 break; 1940 1941 case IPV6_MULTICAST_HOPS: 1942 { 1943 /* 1944 * Set the IP6 hoplimit for outgoing multicast packets. 1945 */ 1946 int optval; 1947 if (m == NULL || m->m_len != sizeof(int)) { 1948 error = EINVAL; 1949 break; 1950 } 1951 bcopy(mtod(m, u_int *), &optval, sizeof(optval)); 1952 if (optval < -1 || optval >= 256) 1953 error = EINVAL; 1954 else if (optval == -1) 1955 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 1956 else 1957 im6o->im6o_multicast_hlim = optval; 1958 break; 1959 } 1960 1961 case IPV6_MULTICAST_LOOP: 1962 /* 1963 * Set the loopback flag for outgoing multicast packets. 1964 * Must be zero or one. 1965 */ 1966 if (m == NULL || m->m_len != sizeof(u_int)) { 1967 error = EINVAL; 1968 break; 1969 } 1970 bcopy(mtod(m, u_int *), &loop, sizeof(loop)); 1971 if (loop > 1) { 1972 error = EINVAL; 1973 break; 1974 } 1975 im6o->im6o_multicast_loop = loop; 1976 break; 1977 1978 case IPV6_JOIN_GROUP: 1979 /* 1980 * Add a multicast group membership. 1981 * Group must be a valid IP6 multicast address. 1982 */ 1983 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 1984 error = EINVAL; 1985 break; 1986 } 1987 mreq = mtod(m, struct ipv6_mreq *); 1988 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 1989 /* 1990 * We use the unspecified address to specify to accept 1991 * all multicast addresses. Only super user is allowed 1992 * to do this. 1993 */ 1994 if (suser_td(td)) 1995 { 1996 error = EACCES; 1997 break; 1998 } 1999 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2000 error = EINVAL; 2001 break; 2002 } 2003 2004 /* 2005 * If the interface is specified, validate it. 2006 */ 2007 if (mreq->ipv6mr_interface < 0 2008 || if_index < mreq->ipv6mr_interface) { 2009 error = ENXIO; /* XXX EINVAL? */ 2010 break; 2011 } 2012 /* 2013 * If no interface was explicitly specified, choose an 2014 * appropriate one according to the given multicast address. 2015 */ 2016 if (mreq->ipv6mr_interface == 0) { 2017 /* 2018 * If the multicast address is in node-local scope, 2019 * the interface should be a loopback interface. 2020 * Otherwise, look up the routing table for the 2021 * address, and choose the outgoing interface. 2022 * XXX: is it a good approach? 2023 */ 2024 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) { 2025 ifp = &loif[0]; 2026 } else { 2027 ro.ro_rt = NULL; 2028 dst = (struct sockaddr_in6 *)&ro.ro_dst; 2029 bzero(dst, sizeof(*dst)); 2030 dst->sin6_len = sizeof(struct sockaddr_in6); 2031 dst->sin6_family = AF_INET6; 2032 dst->sin6_addr = mreq->ipv6mr_multiaddr; 2033 rtalloc((struct route *)&ro); 2034 if (ro.ro_rt == NULL) { 2035 error = EADDRNOTAVAIL; 2036 break; 2037 } 2038 ifp = ro.ro_rt->rt_ifp; 2039 rtfree(ro.ro_rt); 2040 } 2041 } else 2042 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2043 2044 /* 2045 * See if we found an interface, and confirm that it 2046 * supports multicast 2047 */ 2048 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2049 error = EADDRNOTAVAIL; 2050 break; 2051 } 2052 /* 2053 * Put interface index into the multicast address, 2054 * if the address has link-local scope. 2055 */ 2056 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2057 mreq->ipv6mr_multiaddr.s6_addr16[1] 2058 = htons(mreq->ipv6mr_interface); 2059 } 2060 /* 2061 * See if the membership already exists. 2062 */ 2063 for (imm = im6o->im6o_memberships.lh_first; 2064 imm != NULL; imm = imm->i6mm_chain.le_next) 2065 if (imm->i6mm_maddr->in6m_ifp == ifp && 2066 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2067 &mreq->ipv6mr_multiaddr)) 2068 break; 2069 if (imm != NULL) { 2070 error = EADDRINUSE; 2071 break; 2072 } 2073 /* 2074 * Everything looks good; add a new record to the multicast 2075 * address list for the given interface. 2076 */ 2077 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK); 2078 if (imm == NULL) { 2079 error = ENOBUFS; 2080 break; 2081 } 2082 if ((imm->i6mm_maddr = 2083 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) { 2084 free(imm, M_IPMADDR); 2085 break; 2086 } 2087 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2088 break; 2089 2090 case IPV6_LEAVE_GROUP: 2091 /* 2092 * Drop a multicast group membership. 2093 * Group must be a valid IP6 multicast address. 2094 */ 2095 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2096 error = EINVAL; 2097 break; 2098 } 2099 mreq = mtod(m, struct ipv6_mreq *); 2100 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2101 if (suser_td(td)) { 2102 error = EACCES; 2103 break; 2104 } 2105 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2106 error = EINVAL; 2107 break; 2108 } 2109 /* 2110 * If an interface address was specified, get a pointer 2111 * to its ifnet structure. 2112 */ 2113 if (mreq->ipv6mr_interface < 0 2114 || if_index < mreq->ipv6mr_interface) { 2115 error = ENXIO; /* XXX EINVAL? */ 2116 break; 2117 } 2118 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2119 /* 2120 * Put interface index into the multicast address, 2121 * if the address has link-local scope. 2122 */ 2123 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2124 mreq->ipv6mr_multiaddr.s6_addr16[1] 2125 = htons(mreq->ipv6mr_interface); 2126 } 2127 /* 2128 * Find the membership in the membership list. 2129 */ 2130 for (imm = im6o->im6o_memberships.lh_first; 2131 imm != NULL; imm = imm->i6mm_chain.le_next) { 2132 if ((ifp == NULL || 2133 imm->i6mm_maddr->in6m_ifp == ifp) && 2134 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2135 &mreq->ipv6mr_multiaddr)) 2136 break; 2137 } 2138 if (imm == NULL) { 2139 /* Unable to resolve interface */ 2140 error = EADDRNOTAVAIL; 2141 break; 2142 } 2143 /* 2144 * Give up the multicast address record to which the 2145 * membership points. 2146 */ 2147 LIST_REMOVE(imm, i6mm_chain); 2148 in6_delmulti(imm->i6mm_maddr); 2149 free(imm, M_IPMADDR); 2150 break; 2151 2152 default: 2153 error = EOPNOTSUPP; 2154 break; 2155 } 2156 2157 /* 2158 * If all options have default values, no need to keep the mbuf. 2159 */ 2160 if (im6o->im6o_multicast_ifp == NULL && 2161 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2162 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2163 im6o->im6o_memberships.lh_first == NULL) { 2164 free(*im6op, M_IPMOPTS); 2165 *im6op = NULL; 2166 } 2167 2168 return(error); 2169 } 2170 2171 /* 2172 * Return the IP6 multicast options in response to user getsockopt(). 2173 */ 2174 static int 2175 ip6_getmoptions(optname, im6o, mp) 2176 int optname; 2177 struct ip6_moptions *im6o; 2178 struct mbuf **mp; 2179 { 2180 u_int *hlim, *loop, *ifindex; 2181 2182 *mp = m_get(M_TRYWAIT, MT_HEADER); /*XXX*/ 2183 2184 switch (optname) { 2185 2186 case IPV6_MULTICAST_IF: 2187 ifindex = mtod(*mp, u_int *); 2188 (*mp)->m_len = sizeof(u_int); 2189 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2190 *ifindex = 0; 2191 else 2192 *ifindex = im6o->im6o_multicast_ifp->if_index; 2193 return(0); 2194 2195 case IPV6_MULTICAST_HOPS: 2196 hlim = mtod(*mp, u_int *); 2197 (*mp)->m_len = sizeof(u_int); 2198 if (im6o == NULL) 2199 *hlim = ip6_defmcasthlim; 2200 else 2201 *hlim = im6o->im6o_multicast_hlim; 2202 return(0); 2203 2204 case IPV6_MULTICAST_LOOP: 2205 loop = mtod(*mp, u_int *); 2206 (*mp)->m_len = sizeof(u_int); 2207 if (im6o == NULL) 2208 *loop = ip6_defmcasthlim; 2209 else 2210 *loop = im6o->im6o_multicast_loop; 2211 return(0); 2212 2213 default: 2214 return(EOPNOTSUPP); 2215 } 2216 } 2217 2218 /* 2219 * Discard the IP6 multicast options. 2220 */ 2221 void 2222 ip6_freemoptions(im6o) 2223 struct ip6_moptions *im6o; 2224 { 2225 struct in6_multi_mship *imm; 2226 2227 if (im6o == NULL) 2228 return; 2229 2230 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2231 LIST_REMOVE(imm, i6mm_chain); 2232 if (imm->i6mm_maddr) 2233 in6_delmulti(imm->i6mm_maddr); 2234 free(imm, M_IPMADDR); 2235 } 2236 free(im6o, M_IPMOPTS); 2237 } 2238 2239 /* 2240 * Set IPv6 outgoing packet options based on advanced API. 2241 */ 2242 int 2243 ip6_setpktoptions(control, opt, priv, needcopy) 2244 struct mbuf *control; 2245 struct ip6_pktopts *opt; 2246 int priv, needcopy; 2247 { 2248 struct cmsghdr *cm = 0; 2249 2250 if (control == 0 || opt == 0) 2251 return(EINVAL); 2252 2253 init_ip6pktopts(opt); 2254 2255 /* 2256 * XXX: Currently, we assume all the optional information is stored 2257 * in a single mbuf. 2258 */ 2259 if (control->m_next) 2260 return(EINVAL); 2261 2262 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2263 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2264 cm = mtod(control, struct cmsghdr *); 2265 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2266 return(EINVAL); 2267 if (cm->cmsg_level != IPPROTO_IPV6) 2268 continue; 2269 2270 /* 2271 * XXX should check if RFC2292 API is mixed with 2292bis API 2272 */ 2273 switch (cm->cmsg_type) { 2274 case IPV6_PKTINFO: 2275 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo))) 2276 return(EINVAL); 2277 if (needcopy) { 2278 /* XXX: Is it really WAITOK? */ 2279 opt->ip6po_pktinfo = 2280 malloc(sizeof(struct in6_pktinfo), 2281 M_IP6OPT, M_WAITOK); 2282 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo, 2283 sizeof(struct in6_pktinfo)); 2284 } else 2285 opt->ip6po_pktinfo = 2286 (struct in6_pktinfo *)CMSG_DATA(cm); 2287 if (opt->ip6po_pktinfo->ipi6_ifindex && 2288 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr)) 2289 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] = 2290 htons(opt->ip6po_pktinfo->ipi6_ifindex); 2291 2292 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index 2293 || opt->ip6po_pktinfo->ipi6_ifindex < 0) { 2294 return(ENXIO); 2295 } 2296 2297 /* 2298 * Check if the requested source address is indeed a 2299 * unicast address assigned to the node, and can be 2300 * used as the packet's source address. 2301 */ 2302 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) { 2303 struct in6_ifaddr *ia6; 2304 struct sockaddr_in6 sin6; 2305 2306 bzero(&sin6, sizeof(sin6)); 2307 sin6.sin6_len = sizeof(sin6); 2308 sin6.sin6_family = AF_INET6; 2309 sin6.sin6_addr = 2310 opt->ip6po_pktinfo->ipi6_addr; 2311 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6)); 2312 if (ia6 == NULL || 2313 (ia6->ia6_flags & (IN6_IFF_ANYCAST | 2314 IN6_IFF_NOTREADY)) != 0) 2315 return(EADDRNOTAVAIL); 2316 } 2317 break; 2318 2319 case IPV6_HOPLIMIT: 2320 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) 2321 return(EINVAL); 2322 2323 opt->ip6po_hlim = *(int *)CMSG_DATA(cm); 2324 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255) 2325 return(EINVAL); 2326 break; 2327 2328 case IPV6_NEXTHOP: 2329 if (!priv) 2330 return(EPERM); 2331 2332 if (cm->cmsg_len < sizeof(u_char) || 2333 /* check if cmsg_len is large enough for sa_len */ 2334 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm))) 2335 return(EINVAL); 2336 2337 if (needcopy) { 2338 opt->ip6po_nexthop = 2339 malloc(*CMSG_DATA(cm), 2340 M_IP6OPT, M_WAITOK); 2341 bcopy(CMSG_DATA(cm), 2342 opt->ip6po_nexthop, 2343 *CMSG_DATA(cm)); 2344 } else 2345 opt->ip6po_nexthop = 2346 (struct sockaddr *)CMSG_DATA(cm); 2347 break; 2348 2349 case IPV6_HOPOPTS: 2350 { 2351 struct ip6_hbh *hbh; 2352 int hbhlen; 2353 2354 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh))) 2355 return(EINVAL); 2356 hbh = (struct ip6_hbh *)CMSG_DATA(cm); 2357 hbhlen = (hbh->ip6h_len + 1) << 3; 2358 if (cm->cmsg_len != CMSG_LEN(hbhlen)) 2359 return(EINVAL); 2360 2361 if (needcopy) { 2362 opt->ip6po_hbh = 2363 malloc(hbhlen, M_IP6OPT, M_WAITOK); 2364 bcopy(hbh, opt->ip6po_hbh, hbhlen); 2365 } else 2366 opt->ip6po_hbh = hbh; 2367 break; 2368 } 2369 2370 case IPV6_DSTOPTS: 2371 { 2372 struct ip6_dest *dest, **newdest; 2373 int destlen; 2374 2375 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest))) 2376 return(EINVAL); 2377 dest = (struct ip6_dest *)CMSG_DATA(cm); 2378 destlen = (dest->ip6d_len + 1) << 3; 2379 if (cm->cmsg_len != CMSG_LEN(destlen)) 2380 return(EINVAL); 2381 2382 /* 2383 * The old advacned API is ambiguous on this 2384 * point. Our approach is to determine the 2385 * position based according to the existence 2386 * of a routing header. Note, however, that 2387 * this depends on the order of the extension 2388 * headers in the ancillary data; the 1st part 2389 * of the destination options header must 2390 * appear before the routing header in the 2391 * ancillary data, too. 2392 * RFC2292bis solved the ambiguity by 2393 * introducing separate cmsg types. 2394 */ 2395 if (opt->ip6po_rthdr == NULL) 2396 newdest = &opt->ip6po_dest1; 2397 else 2398 newdest = &opt->ip6po_dest2; 2399 2400 if (needcopy) { 2401 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK); 2402 bcopy(dest, *newdest, destlen); 2403 } else 2404 *newdest = dest; 2405 2406 break; 2407 } 2408 2409 case IPV6_RTHDR: 2410 { 2411 struct ip6_rthdr *rth; 2412 int rthlen; 2413 2414 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr))) 2415 return(EINVAL); 2416 rth = (struct ip6_rthdr *)CMSG_DATA(cm); 2417 rthlen = (rth->ip6r_len + 1) << 3; 2418 if (cm->cmsg_len != CMSG_LEN(rthlen)) 2419 return(EINVAL); 2420 2421 switch (rth->ip6r_type) { 2422 case IPV6_RTHDR_TYPE_0: 2423 /* must contain one addr */ 2424 if (rth->ip6r_len == 0) 2425 return(EINVAL); 2426 /* length must be even */ 2427 if (rth->ip6r_len % 2) 2428 return(EINVAL); 2429 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 2430 return(EINVAL); 2431 break; 2432 default: 2433 return(EINVAL); /* not supported */ 2434 } 2435 2436 if (needcopy) { 2437 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, 2438 M_WAITOK); 2439 bcopy(rth, opt->ip6po_rthdr, rthlen); 2440 } else 2441 opt->ip6po_rthdr = rth; 2442 2443 break; 2444 } 2445 2446 default: 2447 return(ENOPROTOOPT); 2448 } 2449 } 2450 2451 return(0); 2452 } 2453 2454 /* 2455 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 2456 * packet to the input queue of a specified interface. Note that this 2457 * calls the output routine of the loopback "driver", but with an interface 2458 * pointer that might NOT be &loif -- easier than replicating that code here. 2459 */ 2460 void 2461 ip6_mloopback(ifp, m, dst) 2462 struct ifnet *ifp; 2463 struct mbuf *m; 2464 struct sockaddr_in6 *dst; 2465 { 2466 struct mbuf *copym; 2467 struct ip6_hdr *ip6; 2468 2469 copym = m_copy(m, 0, M_COPYALL); 2470 if (copym == NULL) 2471 return; 2472 2473 /* 2474 * Make sure to deep-copy IPv6 header portion in case the data 2475 * is in an mbuf cluster, so that we can safely override the IPv6 2476 * header portion later. 2477 */ 2478 if ((copym->m_flags & M_EXT) != 0 || 2479 copym->m_len < sizeof(struct ip6_hdr)) { 2480 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 2481 if (copym == NULL) 2482 return; 2483 } 2484 2485 #ifdef DIAGNOSTIC 2486 if (copym->m_len < sizeof(*ip6)) { 2487 m_freem(copym); 2488 return; 2489 } 2490 #endif 2491 2492 ip6 = mtod(copym, struct ip6_hdr *); 2493 #ifndef SCOPEDROUTING 2494 /* 2495 * clear embedded scope identifiers if necessary. 2496 * in6_clearscope will touch the addresses only when necessary. 2497 */ 2498 in6_clearscope(&ip6->ip6_src); 2499 in6_clearscope(&ip6->ip6_dst); 2500 #endif 2501 2502 (void)if_simloop(ifp, copym, dst->sin6_family, NULL); 2503 } 2504 2505 /* 2506 * Chop IPv6 header off from the payload. 2507 */ 2508 static int 2509 ip6_splithdr(m, exthdrs) 2510 struct mbuf *m; 2511 struct ip6_exthdrs *exthdrs; 2512 { 2513 struct mbuf *mh; 2514 struct ip6_hdr *ip6; 2515 2516 ip6 = mtod(m, struct ip6_hdr *); 2517 if (m->m_len > sizeof(*ip6)) { 2518 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 2519 if (mh == 0) { 2520 m_freem(m); 2521 return ENOBUFS; 2522 } 2523 M_COPY_PKTHDR(mh, m); 2524 MH_ALIGN(mh, sizeof(*ip6)); 2525 m->m_flags &= ~M_PKTHDR; 2526 m->m_len -= sizeof(*ip6); 2527 m->m_data += sizeof(*ip6); 2528 mh->m_next = m; 2529 m = mh; 2530 m->m_len = sizeof(*ip6); 2531 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 2532 } 2533 exthdrs->ip6e_ip6 = m; 2534 return 0; 2535 } 2536 2537 /* 2538 * Compute IPv6 extension header length. 2539 */ 2540 int 2541 ip6_optlen(in6p) 2542 struct in6pcb *in6p; 2543 { 2544 int len; 2545 2546 if (!in6p->in6p_outputopts) 2547 return 0; 2548 2549 len = 0; 2550 #define elen(x) \ 2551 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 2552 2553 len += elen(in6p->in6p_outputopts->ip6po_hbh); 2554 if (in6p->in6p_outputopts->ip6po_rthdr) 2555 /* dest1 is valid with rthdr only */ 2556 len += elen(in6p->in6p_outputopts->ip6po_dest1); 2557 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 2558 len += elen(in6p->in6p_outputopts->ip6po_dest2); 2559 return len; 2560 #undef elen 2561 } 2562