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