2 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
65 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
68 #include "opt_ip6fw.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
72 #include "opt_pfil_hooks.h"
74 #include <sys/param.h>
75 #include <sys/malloc.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>
86 #include <net/route.h>
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>
101 #include <netinet6/ipsec.h>
103 #include <netinet6/ipsec6.h>
105 #include <netkey/key.h>
108 #include <netinet6/ip6_fw.h>
110 #include <net/net_osdep.h>
112 #include <netinet6/ip6protosw.h>
114 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
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;
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 *));
135 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
136 * header (with pri, len, nxt, hlim, src, dst).
137 * This function may modify ver and hlim only.
138 * The mbuf chain containing the packet will be freed.
139 * The mbuf opt, if present, will not be freed.
141 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
142 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
143 * which is rt_rmx.rmx_mtu.
146 ip6_output(m0, opt, ro, flags, im6o, ifpp)
148 struct ip6_pktopts *opt;
149 struct route_in6 *ro;
151 struct ip6_moptions *im6o;
152 struct ifnet **ifpp; /* XXX: just for statistics */
154 struct ip6_hdr *ip6, *mhip6;
155 struct ifnet *ifp, *origifp;
157 int hlen, tlen, len, off;
158 struct route_in6 ip6route;
159 struct sockaddr_in6 *dst;
161 struct in6_ifaddr *ia = NULL;
163 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
164 struct ip6_exthdrs exthdrs;
165 struct in6_addr finaldst;
166 struct route_in6 *ro_pmtu = NULL;
170 struct packet_filter_hook *pfh;
173 #endif /* PFIL_HOOKS */
175 int needipsectun = 0;
177 struct secpolicy *sp = NULL;
179 /* for AH processing. stupid to have "socket" variable in IP layer... */
180 so = ipsec_getsocket(m);
181 (void)ipsec_setsocket(m, NULL);
182 ip6 = mtod(m, struct ip6_hdr *);
185 #define MAKE_EXTHDR(hp, mp) \
188 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
189 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
190 ((eh)->ip6e_len + 1) << 3); \
196 bzero(&exthdrs, sizeof(exthdrs));
199 /* Hop-by-Hop options header */
200 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
201 /* Destination options header(1st part) */
202 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
204 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
205 /* Destination options header(2nd part) */
206 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
210 /* get a security policy for this packet */
212 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
214 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
217 ipsec6stat.out_inval++;
224 switch (sp->policy) {
225 case IPSEC_POLICY_DISCARD:
227 * This packet is just discarded.
229 ipsec6stat.out_polvio++;
232 case IPSEC_POLICY_BYPASS:
233 case IPSEC_POLICY_NONE:
234 /* no need to do IPsec. */
238 case IPSEC_POLICY_IPSEC:
239 if (sp->req == NULL) {
240 /* acquire a policy */
241 error = key_spdacquire(sp);
247 case IPSEC_POLICY_ENTRUST:
249 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
254 * Calculate the total length of the extension header chain.
255 * Keep the length of the unfragmentable part for fragmentation.
258 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
259 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
260 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
261 unfragpartlen = optlen + sizeof(struct ip6_hdr);
262 /* NOTE: we don't add AH/ESP length here. do that later. */
263 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
266 * If we need IPsec, or there is at least one extension header,
267 * separate IP6 header from the payload.
269 if ((needipsec || optlen) && !hdrsplit) {
270 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
274 m = exthdrs.ip6e_ip6;
279 ip6 = mtod(m, struct ip6_hdr *);
281 /* adjust mbuf packet header length */
282 m->m_pkthdr.len += optlen;
283 plen = m->m_pkthdr.len - sizeof(*ip6);
285 /* If this is a jumbo payload, insert a jumbo payload option. */
286 if (plen > IPV6_MAXPACKET) {
288 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
292 m = exthdrs.ip6e_ip6;
296 ip6 = mtod(m, struct ip6_hdr *);
297 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
301 ip6->ip6_plen = htons(plen);
304 * Concatenate headers and fill in next header fields.
305 * Here we have, on "m"
307 * and we insert headers accordingly. Finally, we should be getting:
308 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
310 * during the header composing process, "m" points to IPv6 header.
311 * "mprev" points to an extension header prior to esp.
314 u_char *nexthdrp = &ip6->ip6_nxt;
315 struct mbuf *mprev = m;
318 * we treat dest2 specially. this makes IPsec processing
319 * much easier. the goal here is to make mprev point the
320 * mbuf prior to dest2.
322 * result: IPv6 dest2 payload
323 * m and mprev will point to IPv6 header.
325 if (exthdrs.ip6e_dest2) {
327 panic("assumption failed: hdr not split");
328 exthdrs.ip6e_dest2->m_next = m->m_next;
329 m->m_next = exthdrs.ip6e_dest2;
330 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
331 ip6->ip6_nxt = IPPROTO_DSTOPTS;
334 #define MAKE_CHAIN(m, mp, p, i)\
338 panic("assumption failed: hdr not split"); \
339 *mtod((m), u_char *) = *(p);\
341 p = mtod((m), u_char *);\
342 (m)->m_next = (mp)->m_next;\
348 * result: IPv6 hbh dest1 rthdr dest2 payload
349 * m will point to IPv6 header. mprev will point to the
350 * extension header prior to dest2 (rthdr in the above case).
352 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
353 nexthdrp, IPPROTO_HOPOPTS);
354 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
355 nexthdrp, IPPROTO_DSTOPTS);
356 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
357 nexthdrp, IPPROTO_ROUTING);
364 * pointers after IPsec headers are not valid any more.
365 * other pointers need a great care too.
366 * (IPsec routines should not mangle mbufs prior to AH/ESP)
368 exthdrs.ip6e_dest2 = NULL;
371 struct ip6_rthdr *rh = NULL;
373 struct ipsec_output_state state;
375 if (exthdrs.ip6e_rthdr) {
376 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
377 segleft_org = rh->ip6r_segleft;
378 rh->ip6r_segleft = 0;
381 bzero(&state, sizeof(state));
383 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
387 /* mbuf is already reclaimed in ipsec6_output_trans. */
397 printf("ip6_output (ipsec): error code %d\n", error);
400 /* don't show these error codes to the user */
406 if (exthdrs.ip6e_rthdr) {
407 /* ah6_output doesn't modify mbuf chain */
408 rh->ip6r_segleft = segleft_org;
416 * If there is a routing header, replace destination address field
417 * with the first hop of the routing header.
419 if (exthdrs.ip6e_rthdr) {
420 struct ip6_rthdr *rh =
421 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
422 struct ip6_rthdr *));
423 struct ip6_rthdr0 *rh0;
425 finaldst = ip6->ip6_dst;
426 switch (rh->ip6r_type) {
427 case IPV6_RTHDR_TYPE_0:
428 rh0 = (struct ip6_rthdr0 *)rh;
429 ip6->ip6_dst = rh0->ip6r0_addr[0];
430 bcopy((caddr_t)&rh0->ip6r0_addr[1],
431 (caddr_t)&rh0->ip6r0_addr[0],
432 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
434 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
436 default: /* is it possible? */
442 /* Source address validation */
443 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
444 (flags & IPV6_DADOUTPUT) == 0) {
446 ip6stat.ip6s_badscope++;
449 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
451 ip6stat.ip6s_badscope++;
455 ip6stat.ip6s_localout++;
462 bzero((caddr_t)ro, sizeof(*ro));
465 if (opt && opt->ip6po_rthdr)
466 ro = &opt->ip6po_route;
467 dst = (struct sockaddr_in6 *)&ro->ro_dst;
469 * If there is a cached route,
470 * check that it is to the same destination
471 * and is still up. If not, free it and try again.
473 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
474 dst->sin6_family != AF_INET6 ||
475 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
477 ro->ro_rt = (struct rtentry *)0;
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;
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]);
491 if (needipsec && needipsectun) {
492 struct ipsec_output_state state;
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).
500 * IPv6 [ESP|AH] IPv6 [extension headers] payload
502 bzero(&exthdrs, sizeof(exthdrs));
503 exthdrs.ip6e_ip6 = m;
505 bzero(&state, sizeof(state));
507 state.ro = (struct route *)ro;
508 state.dst = (struct sockaddr *)dst;
510 error = ipsec6_output_tunnel(&state, sp, flags);
513 ro = (struct route_in6 *)state.ro;
514 dst = (struct sockaddr_in6 *)state.dst;
516 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
527 printf("ip6_output (ipsec): error code %d\n", error);
530 /* don't show these error codes to the user */
537 exthdrs.ip6e_ip6 = m;
541 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
544 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
545 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
547 * interface selection comes here
548 * if an interface is specified from an upper layer,
551 if (ro->ro_rt == 0) {
553 * non-bsdi always clone routes, if parent is
556 rtalloc((struct route *)ro);
558 if (ro->ro_rt == 0) {
559 ip6stat.ip6s_noroute++;
560 error = EHOSTUNREACH;
561 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
564 ia = ifatoia6(ro->ro_rt->rt_ifa);
565 ifp = ro->ro_rt->rt_ifp;
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 */
571 in6_ifstat_inc(ifp, ifs6_out_request);
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.)
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;
591 if (opt && opt->ip6po_hlim != -1)
592 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
595 struct in6_multi *in6m;
597 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
600 * See if the caller provided any multicast options
604 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
605 if (im6o->im6o_multicast_ifp != NULL)
606 ifp = im6o->im6o_multicast_ifp;
608 ip6->ip6_hlim = ip6_defmcasthlim;
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.
615 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
616 ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex);
619 * If the destination is a node-local scope multicast,
620 * the packet should be loop-backed only.
622 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
624 * If the outgoing interface is already specified,
625 * it should be a loopback interface.
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);
638 if (opt && opt->ip6po_hlim != -1)
639 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
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
649 if (ro->ro_rt == 0) {
650 ro->ro_rt = rtalloc1((struct sockaddr *)
651 &ro->ro_dst, 0, 0UL);
653 if (ro->ro_rt == 0) {
654 ip6stat.ip6s_noroute++;
655 error = EHOSTUNREACH;
656 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
659 ia = ifatoia6(ro->ro_rt->rt_ifa);
660 ifp = ro->ro_rt->rt_ifp;
664 if ((flags & IPV6_FORWARDING) == 0)
665 in6_ifstat_inc(ifp, ifs6_out_request);
666 in6_ifstat_inc(ifp, ifs6_out_mcast);
669 * Confirm that the outgoing interface supports multicast.
671 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
672 ip6stat.ip6s_noroute++;
673 in6_ifstat_inc(ifp, ifs6_out_discard);
677 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
679 (im6o == NULL || im6o->im6o_multicast_loop)) {
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.
685 ip6_mloopback(ifp, m, dst);
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.
695 * Multicasts that are looped back by ip6_mloopback(),
696 * above, will be forwarded by the ip6_input() routine,
699 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
700 if (ip6_mforward(ip6, ifp, m) != 0) {
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.
714 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
721 * Fill the outgoing inteface to tell the upper layer
722 * to increment per-interface statistics.
728 * Determine path MTU.
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,
737 RTFREE(ro_pmtu->ro_rt);
738 ro_pmtu->ro_rt = (struct rtentry *)0;
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;
746 rtalloc((struct route *)ro_pmtu);
749 if (ro_pmtu->ro_rt != NULL) {
750 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu;
752 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
753 if (mtu > ifmtu || mtu == 0) {
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).
762 * if MTU on the route is 0, we need to fix the MTU.
763 * this case happens with path MTU discovery timeouts.
766 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
767 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
770 mtu = nd_ifinfo[ifp->if_index].linkmtu;
774 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
776 if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU)
779 /* Fake scoped addresses */
780 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
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.
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 and destination, which should already be assured.
793 * Larger scopes than link will be supported in the future.
796 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
797 origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1]));
798 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
799 origifp = ifnet_byindex(ntohs(ip6->ip6_dst.s6_addr16[1]));
801 * XXX: origifp can be NULL even in those two cases above.
802 * For example, if we remove the (only) link-local address
803 * from the loopback interface, and try to send a link-local
804 * address without link-id information. Then the source
805 * address is ::1, and the destination address is the
806 * link-local address with its s6_addr16[1] being zero.
807 * What is worse, if the packet goes to the loopback interface
808 * by a default rejected route, the null pointer would be
809 * passed to looutput, and the kernel would hang.
810 * The following last resort would prevent such disaster.
817 #ifndef SCOPEDROUTING
819 * clear embedded scope identifiers if necessary.
820 * in6_clearscope will touch the addresses only when necessary.
822 in6_clearscope(&ip6->ip6_src);
823 in6_clearscope(&ip6->ip6_dst);
827 * Check with the firewall...
829 if (ip6_fw_enable && ip6_fw_chk_ptr) {
831 m->m_pkthdr.rcvif = NULL; /* XXX */
832 /* If ipfw says divert, we have to just drop packet */
833 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
844 * If the outgoing packet contains a hop-by-hop options header,
845 * it must be examined and processed even by the source node.
846 * (RFC 2460, section 4.)
848 if (exthdrs.ip6e_hbh) {
849 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
850 u_int32_t dummy1; /* XXX unused */
851 u_int32_t dummy2; /* XXX unused */
854 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
855 panic("ip6e_hbh is not continuous");
858 * XXX: if we have to send an ICMPv6 error to the sender,
859 * we need the M_LOOP flag since icmp6_error() expects
860 * the IPv6 and the hop-by-hop options header are
861 * continuous unless the flag is set.
863 m->m_flags |= M_LOOP;
864 m->m_pkthdr.rcvif = ifp;
865 if (ip6_process_hopopts(m,
866 (u_int8_t *)(hbh + 1),
867 ((hbh->ip6h_len + 1) << 3) -
868 sizeof(struct ip6_hbh),
869 &dummy1, &dummy2) < 0) {
870 /* m was already freed at this point */
871 error = EINVAL;/* better error? */
874 m->m_flags &= ~M_LOOP; /* XXX */
875 m->m_pkthdr.rcvif = NULL;
880 * Run through list of hooks for output packets.
883 pfh = pfil_hook_get(PFIL_OUT, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
884 for (; pfh; pfh = pfh->pfil_link.tqe_next)
885 if (pfh->pfil_func) {
886 rv = pfh->pfil_func(ip6, sizeof(*ip6), ifp, 1, &m1);
888 error = EHOSTUNREACH;
894 ip6 = mtod(m, struct ip6_hdr *);
896 #endif /* PFIL_HOOKS */
898 * Send the packet to the outgoing interface.
899 * If necessary, do IPv6 fragmentation before sending.
901 tlen = m->m_pkthdr.len;
905 * On any link that cannot convey a 1280-octet packet in one piece,
906 * link-specific fragmentation and reassembly must be provided at
907 * a layer below IPv6. [RFC 2460, sec.5]
908 * Thus if the interface has ability of link-level fragmentation,
909 * we can just send the packet even if the packet size is
910 * larger than the link's MTU.
911 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
914 || ifp->if_flags & IFF_FRAGMENTABLE
918 /* Record statistics for this interface address. */
919 if (ia && !(flags & IPV6_FORWARDING)) {
920 ia->ia_ifa.if_opackets++;
921 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
924 /* clean ipsec history once it goes out of the node */
927 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
929 } else if (mtu < IPV6_MMTU) {
931 * note that path MTU is never less than IPV6_MMTU
935 in6_ifstat_inc(ifp, ifs6_out_fragfail);
937 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
939 in6_ifstat_inc(ifp, ifs6_out_fragfail);
942 struct mbuf **mnext, *m_frgpart;
943 struct ip6_frag *ip6f;
944 u_int32_t id = htonl(ip6_id++);
948 * Too large for the destination or interface;
949 * fragment if possible.
950 * Must be able to put at least 8 bytes per fragment.
952 hlen = unfragpartlen;
953 if (mtu > IPV6_MAXPACKET)
954 mtu = IPV6_MAXPACKET;
956 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
959 in6_ifstat_inc(ifp, ifs6_out_fragfail);
963 mnext = &m->m_nextpkt;
966 * Change the next header field of the last header in the
967 * unfragmentable part.
969 if (exthdrs.ip6e_rthdr) {
970 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
971 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
972 } else if (exthdrs.ip6e_dest1) {
973 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
974 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
975 } else if (exthdrs.ip6e_hbh) {
976 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
977 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
979 nextproto = ip6->ip6_nxt;
980 ip6->ip6_nxt = IPPROTO_FRAGMENT;
984 * Loop through length of segment after first fragment,
985 * make new header and copy data of each part and link onto
989 for (off = hlen; off < tlen; off += len) {
990 MGETHDR(m, M_DONTWAIT, MT_HEADER);
993 ip6stat.ip6s_odropped++;
996 m->m_pkthdr.rcvif = NULL;
997 m->m_flags = m0->m_flags & M_COPYFLAGS;
999 mnext = &m->m_nextpkt;
1000 m->m_data += max_linkhdr;
1001 mhip6 = mtod(m, struct ip6_hdr *);
1003 m->m_len = sizeof(*mhip6);
1004 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1006 ip6stat.ip6s_odropped++;
1009 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1010 if (off + len >= tlen)
1013 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1014 mhip6->ip6_plen = htons((u_short)(len + hlen +
1016 sizeof(struct ip6_hdr)));
1017 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1019 ip6stat.ip6s_odropped++;
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);
1032 in6_ifstat_inc(ifp, ifs6_out_fragok);
1036 * Remove leading garbages.
1042 for (m0 = m; m; m = m0) {
1046 /* Record statistics for this interface address. */
1048 ia->ia_ifa.if_opackets++;
1049 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1052 /* clean ipsec history once it goes out of the node */
1055 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1061 ip6stat.ip6s_fragmented++;
1064 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1066 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1067 RTFREE(ro_pmtu->ro_rt);
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);
1089 ip6_copyexthdr(mp, hdr, hlen)
1096 if (hlen > MCLBYTES)
1097 return(ENOBUFS); /* XXX */
1099 MGET(m, M_DONTWAIT, MT_DATA);
1104 MCLGET(m, M_DONTWAIT);
1105 if ((m->m_flags & M_EXT) == 0) {
1112 bcopy(hdr, mtod(m, caddr_t), hlen);
1119 * Insert jumbo payload option.
1122 ip6_insert_jumboopt(exthdrs, plen)
1123 struct ip6_exthdrs *exthdrs;
1130 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
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.
1138 if (exthdrs->ip6e_hbh == 0) {
1139 MGET(mopt, M_DONTWAIT, MT_DATA);
1142 mopt->m_len = JUMBOOPTLEN;
1143 optbuf = mtod(mopt, u_char *);
1144 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1145 exthdrs->ip6e_hbh = mopt;
1147 struct ip6_hbh *hbh;
1149 mopt = exthdrs->ip6e_hbh;
1150 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1153 * - exthdrs->ip6e_hbh is not referenced from places
1154 * other than exthdrs.
1155 * - exthdrs->ip6e_hbh is not an mbuf chain.
1157 int oldoptlen = mopt->m_len;
1161 * XXX: give up if the whole (new) hbh header does
1162 * not fit even in an mbuf cluster.
1164 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1168 * As a consequence, we must always prepare a cluster
1171 MGET(n, M_DONTWAIT, MT_DATA);
1173 MCLGET(n, M_DONTWAIT);
1174 if ((n->m_flags & M_EXT) == 0) {
1181 n->m_len = oldoptlen + JUMBOOPTLEN;
1182 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1184 optbuf = mtod(n, caddr_t) + oldoptlen;
1186 mopt = exthdrs->ip6e_hbh = n;
1188 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1189 mopt->m_len += JUMBOOPTLEN;
1191 optbuf[0] = IP6OPT_PADN;
1195 * Adjust the header length according to the pad and
1196 * the jumbo payload option.
1198 hbh = mtod(mopt, struct ip6_hbh *);
1199 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1202 /* fill in the option. */
1203 optbuf[2] = IP6OPT_JUMBO;
1205 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1206 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1208 /* finally, adjust the packet header length */
1209 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1216 * Insert fragment header and copy unfragmentable header portions.
1219 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1220 struct mbuf *m0, *m;
1222 struct ip6_frag **frghdrp;
1224 struct mbuf *n, *mlast;
1226 if (hlen > sizeof(struct ip6_hdr)) {
1227 n = m_copym(m0, sizeof(struct ip6_hdr),
1228 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1235 /* Search for the last mbuf of unfragmentable part. */
1236 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
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 */
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);
1247 /* allocate a new mbuf for the fragment header */
1250 MGET(mfrg, M_DONTWAIT, MT_DATA);
1253 mfrg->m_len = sizeof(struct ip6_frag);
1254 *frghdrp = mtod(mfrg, struct ip6_frag *);
1255 mlast->m_next = mfrg;
1262 * IP6 socket option processing.
1265 ip6_ctloutput(so, sopt)
1267 struct sockopt *sopt;
1270 struct inpcb *in6p = sotoinpcb(so);
1272 int level, op, optname;
1277 level = sopt->sopt_level;
1278 op = sopt->sopt_dir;
1279 optname = sopt->sopt_name;
1280 optlen = sopt->sopt_valsize;
1283 panic("ip6_ctloutput: arg soopt is NULL");
1287 privileged = (td == 0 || suser(td)) ? 0 : 1;
1289 if (level == IPPROTO_IPV6) {
1294 case IPV6_PKTOPTIONS:
1298 error = soopt_getm(sopt, &m); /* XXX */
1301 error = soopt_mcopyin(sopt, m); /* XXX */
1304 error = ip6_pcbopts(&in6p->in6p_outputopts,
1306 m_freem(m); /* XXX */
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.
1323 case IPV6_UNICAST_HOPS:
1328 if (optlen != sizeof(int)) {
1332 error = sooptcopyin(sopt, &optval,
1333 sizeof optval, sizeof optval);
1338 case IPV6_UNICAST_HOPS:
1339 if (optval < -1 || optval >= 256)
1342 /* -1 = kernel default */
1343 in6p->in6p_hops = optval;
1345 if ((in6p->in6p_vflag &
1347 in6p->inp_ip_ttl = optval;
1350 #define OPTSET(bit) \
1353 in6p->in6p_flags |= (bit); \
1355 in6p->in6p_flags &= ~(bit); \
1357 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1360 in6p->in6p_cksum = optval;
1369 * make setsockopt(IPV6_V6ONLY)
1370 * available only prior to bind(2).
1371 * see ipng mailing list, Jun 22 2001.
1373 if (in6p->in6p_lport ||
1374 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1380 * XXX: BINDV6ONLY should be integrated
1383 OPTSET(IN6P_BINDV6ONLY);
1384 OPTSET(IN6P_IPV6_V6ONLY);
1395 if (optlen != sizeof(int)) {
1399 error = sooptcopyin(sopt, &optval,
1400 sizeof optval, sizeof optval);
1405 OPTSET(IN6P_PKTINFO);
1408 OPTSET(IN6P_HOPLIMIT);
1412 * Check super-user privilege.
1413 * See comments for IPV6_RECVHOPOPTS.
1417 OPTSET(IN6P_HOPOPTS);
1422 OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1431 case IPV6_MULTICAST_IF:
1432 case IPV6_MULTICAST_HOPS:
1433 case IPV6_MULTICAST_LOOP:
1434 case IPV6_JOIN_GROUP:
1435 case IPV6_LEAVE_GROUP:
1438 if (sopt->sopt_valsize > MLEN) {
1443 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1448 m->m_len = sopt->sopt_valsize;
1449 error = sooptcopyin(sopt, mtod(m, char *),
1450 m->m_len, m->m_len);
1451 error = ip6_setmoptions(sopt->sopt_name,
1452 &in6p->in6p_moptions,
1458 case IPV6_PORTRANGE:
1459 error = sooptcopyin(sopt, &optval,
1460 sizeof optval, sizeof optval);
1465 case IPV6_PORTRANGE_DEFAULT:
1466 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1467 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1470 case IPV6_PORTRANGE_HIGH:
1471 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1472 in6p->in6p_flags |= IN6P_HIGHPORT;
1475 case IPV6_PORTRANGE_LOW:
1476 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1477 in6p->in6p_flags |= IN6P_LOWPORT;
1487 case IPV6_IPSEC_POLICY:
1493 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1495 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1498 req = mtod(m, caddr_t);
1501 error = ipsec6_set_policy(in6p, optname, req,
1506 #endif /* KAME IPSEC */
1514 struct mbuf **mp = &m;
1516 if (ip6_fw_ctl_ptr == NULL)
1519 if ((error = soopt_getm(sopt, &m)) != 0)
1522 if ((error = soopt_mcopyin(sopt, m)) != 0)
1524 error = (*ip6_fw_ctl_ptr)(optname, mp);
1530 error = ENOPROTOOPT;
1538 case IPV6_PKTOPTIONS:
1539 if (in6p->in6p_options) {
1541 m = m_copym(in6p->in6p_options,
1542 0, M_COPYALL, M_TRYWAIT);
1543 error = soopt_mcopyout(sopt, m);
1547 sopt->sopt_valsize = 0;
1550 case IPV6_UNICAST_HOPS:
1555 case IPV6_PORTRANGE:
1558 case IPV6_UNICAST_HOPS:
1559 optval = in6p->in6p_hops;
1563 optval = in6p->in6p_cksum;
1567 optval = OPTBIT(IN6P_FAITH);
1571 /* XXX: see the setopt case. */
1572 optval = OPTBIT(IN6P_BINDV6ONLY);
1575 case IPV6_PORTRANGE:
1578 flags = in6p->in6p_flags;
1579 if (flags & IN6P_HIGHPORT)
1580 optval = IPV6_PORTRANGE_HIGH;
1581 else if (flags & IN6P_LOWPORT)
1582 optval = IPV6_PORTRANGE_LOW;
1588 error = sooptcopyout(sopt, &optval,
1597 if (optname == IPV6_HOPOPTS ||
1598 optname == IPV6_DSTOPTS ||
1603 optval = OPTBIT(IN6P_PKTINFO);
1606 optval = OPTBIT(IN6P_HOPLIMIT);
1611 optval = OPTBIT(IN6P_HOPOPTS);
1614 optval = OPTBIT(IN6P_RTHDR);
1619 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1622 error = sooptcopyout(sopt, &optval,
1626 case IPV6_MULTICAST_IF:
1627 case IPV6_MULTICAST_HOPS:
1628 case IPV6_MULTICAST_LOOP:
1629 case IPV6_JOIN_GROUP:
1630 case IPV6_LEAVE_GROUP:
1633 error = ip6_getmoptions(sopt->sopt_name,
1634 in6p->in6p_moptions, &m);
1636 error = sooptcopyout(sopt,
1637 mtod(m, char *), m->m_len);
1643 case IPV6_IPSEC_POLICY:
1647 struct mbuf *m = NULL;
1648 struct mbuf **mp = &m;
1650 error = soopt_getm(sopt, &m); /* XXX */
1653 error = soopt_mcopyin(sopt, m); /* XXX */
1657 req = mtod(m, caddr_t);
1660 error = ipsec6_get_policy(in6p, req, len, mp);
1662 error = soopt_mcopyout(sopt, m); /*XXX*/
1663 if (error == 0 && m)
1667 #endif /* KAME IPSEC */
1672 struct mbuf **mp = &m;
1674 if (ip6_fw_ctl_ptr == NULL)
1678 error = (*ip6_fw_ctl_ptr)(optname, mp);
1680 error = soopt_mcopyout(sopt, m); /* XXX */
1681 if (error == 0 && m)
1687 error = ENOPROTOOPT;
1699 * Set up IP6 options in pcb for insertion in output packets or
1700 * specifying behavior of outgoing packets.
1703 ip6_pcbopts(pktopt, m, so, sopt)
1704 struct ip6_pktopts **pktopt;
1707 struct sockopt *sopt;
1709 struct ip6_pktopts *opt = *pktopt;
1711 struct thread *td = sopt->sopt_td;
1714 /* turn off any old options. */
1717 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1718 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1719 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1720 printf("ip6_pcbopts: all specified options are cleared.\n");
1722 ip6_clearpktopts(opt, 1, -1);
1724 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1727 if (!m || m->m_len == 0) {
1729 * Only turning off any previous options.
1732 free(opt, M_IP6OPT);
1736 /* set options specified by user. */
1737 if (td && !suser(td))
1739 if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) {
1740 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */
1748 * initialize ip6_pktopts. beware that there are non-zero default values in
1752 init_ip6pktopts(opt)
1753 struct ip6_pktopts *opt;
1756 bzero(opt, sizeof(*opt));
1757 opt->ip6po_hlim = -1; /* -1 means default hop limit */
1761 ip6_clearpktopts(pktopt, needfree, optname)
1762 struct ip6_pktopts *pktopt;
1763 int needfree, optname;
1768 if (optname == -1) {
1769 if (needfree && pktopt->ip6po_pktinfo)
1770 free(pktopt->ip6po_pktinfo, M_IP6OPT);
1771 pktopt->ip6po_pktinfo = NULL;
1774 pktopt->ip6po_hlim = -1;
1775 if (optname == -1) {
1776 if (needfree && pktopt->ip6po_nexthop)
1777 free(pktopt->ip6po_nexthop, M_IP6OPT);
1778 pktopt->ip6po_nexthop = NULL;
1780 if (optname == -1) {
1781 if (needfree && pktopt->ip6po_hbh)
1782 free(pktopt->ip6po_hbh, M_IP6OPT);
1783 pktopt->ip6po_hbh = NULL;
1785 if (optname == -1) {
1786 if (needfree && pktopt->ip6po_dest1)
1787 free(pktopt->ip6po_dest1, M_IP6OPT);
1788 pktopt->ip6po_dest1 = NULL;
1790 if (optname == -1) {
1791 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
1792 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
1793 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
1794 if (pktopt->ip6po_route.ro_rt) {
1795 RTFREE(pktopt->ip6po_route.ro_rt);
1796 pktopt->ip6po_route.ro_rt = NULL;
1799 if (optname == -1) {
1800 if (needfree && pktopt->ip6po_dest2)
1801 free(pktopt->ip6po_dest2, M_IP6OPT);
1802 pktopt->ip6po_dest2 = NULL;
1806 #define PKTOPT_EXTHDRCPY(type) \
1810 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1811 dst->type = malloc(hlen, M_IP6OPT, canwait);\
1812 if (dst->type == NULL && canwait == M_NOWAIT)\
1814 bcopy(src->type, dst->type, hlen);\
1818 struct ip6_pktopts *
1819 ip6_copypktopts(src, canwait)
1820 struct ip6_pktopts *src;
1823 struct ip6_pktopts *dst;
1826 printf("ip6_clearpktopts: invalid argument\n");
1830 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
1831 if (dst == NULL && canwait == M_NOWAIT)
1833 bzero(dst, sizeof(*dst));
1835 dst->ip6po_hlim = src->ip6po_hlim;
1836 if (src->ip6po_pktinfo) {
1837 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
1839 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
1841 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
1843 if (src->ip6po_nexthop) {
1844 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
1846 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
1848 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
1849 src->ip6po_nexthop->sa_len);
1851 PKTOPT_EXTHDRCPY(ip6po_hbh);
1852 PKTOPT_EXTHDRCPY(ip6po_dest1);
1853 PKTOPT_EXTHDRCPY(ip6po_dest2);
1854 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
1858 printf("ip6_copypktopts: copy failed");
1859 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
1860 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
1861 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
1862 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
1863 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
1864 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
1867 #undef PKTOPT_EXTHDRCPY
1870 ip6_freepcbopts(pktopt)
1871 struct ip6_pktopts *pktopt;
1876 ip6_clearpktopts(pktopt, 1, -1);
1878 free(pktopt, M_IP6OPT);
1882 * Set the IP6 multicast options in response to user setsockopt().
1885 ip6_setmoptions(optname, im6op, m)
1887 struct ip6_moptions **im6op;
1891 u_int loop, ifindex;
1892 struct ipv6_mreq *mreq;
1894 struct ip6_moptions *im6o = *im6op;
1895 struct route_in6 ro;
1896 struct sockaddr_in6 *dst;
1897 struct in6_multi_mship *imm;
1898 struct thread *td = curthread; /* XXX */
1902 * No multicast option buffer attached to the pcb;
1903 * allocate one and initialize to default values.
1905 im6o = (struct ip6_moptions *)
1906 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1911 im6o->im6o_multicast_ifp = NULL;
1912 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1913 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1914 LIST_INIT(&im6o->im6o_memberships);
1919 case IPV6_MULTICAST_IF:
1921 * Select the interface for outgoing multicast packets.
1923 if (m == NULL || m->m_len != sizeof(u_int)) {
1927 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1928 if (ifindex < 0 || if_index < ifindex) {
1929 error = ENXIO; /* XXX EINVAL? */
1932 ifp = ifnet_byindex(ifindex);
1933 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1934 error = EADDRNOTAVAIL;
1937 im6o->im6o_multicast_ifp = ifp;
1940 case IPV6_MULTICAST_HOPS:
1943 * Set the IP6 hoplimit for outgoing multicast packets.
1946 if (m == NULL || m->m_len != sizeof(int)) {
1950 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1951 if (optval < -1 || optval >= 256)
1953 else if (optval == -1)
1954 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1956 im6o->im6o_multicast_hlim = optval;
1960 case IPV6_MULTICAST_LOOP:
1962 * Set the loopback flag for outgoing multicast packets.
1963 * Must be zero or one.
1965 if (m == NULL || m->m_len != sizeof(u_int)) {
1969 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
1974 im6o->im6o_multicast_loop = loop;
1977 case IPV6_JOIN_GROUP:
1979 * Add a multicast group membership.
1980 * Group must be a valid IP6 multicast address.
1982 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1986 mreq = mtod(m, struct ipv6_mreq *);
1987 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1989 * We use the unspecified address to specify to accept
1990 * all multicast addresses. Only super user is allowed
1998 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2004 * If the interface is specified, validate it.
2006 if (mreq->ipv6mr_interface < 0
2007 || if_index < mreq->ipv6mr_interface) {
2008 error = ENXIO; /* XXX EINVAL? */
2012 * If no interface was explicitly specified, choose an
2013 * appropriate one according to the given multicast address.
2015 if (mreq->ipv6mr_interface == 0) {
2017 * If the multicast address is in node-local scope,
2018 * the interface should be a loopback interface.
2019 * Otherwise, look up the routing table for the
2020 * address, and choose the outgoing interface.
2021 * XXX: is it a good approach?
2023 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
2027 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2028 bzero(dst, sizeof(*dst));
2029 dst->sin6_len = sizeof(struct sockaddr_in6);
2030 dst->sin6_family = AF_INET6;
2031 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2032 rtalloc((struct route *)&ro);
2033 if (ro.ro_rt == NULL) {
2034 error = EADDRNOTAVAIL;
2037 ifp = ro.ro_rt->rt_ifp;
2041 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2044 * See if we found an interface, and confirm that it
2045 * supports multicast
2047 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2048 error = EADDRNOTAVAIL;
2052 * Put interface index into the multicast address,
2053 * if the address has link-local scope.
2055 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2056 mreq->ipv6mr_multiaddr.s6_addr16[1]
2057 = htons(mreq->ipv6mr_interface);
2060 * See if the membership already exists.
2062 for (imm = im6o->im6o_memberships.lh_first;
2063 imm != NULL; imm = imm->i6mm_chain.le_next)
2064 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2065 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2066 &mreq->ipv6mr_multiaddr))
2073 * Everything looks good; add a new record to the multicast
2074 * address list for the given interface.
2076 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2081 if ((imm->i6mm_maddr =
2082 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2083 free(imm, M_IPMADDR);
2086 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2089 case IPV6_LEAVE_GROUP:
2091 * Drop a multicast group membership.
2092 * Group must be a valid IP6 multicast address.
2094 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2098 mreq = mtod(m, struct ipv6_mreq *);
2099 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2104 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2109 * If an interface address was specified, get a pointer
2110 * to its ifnet structure.
2112 if (mreq->ipv6mr_interface < 0
2113 || if_index < mreq->ipv6mr_interface) {
2114 error = ENXIO; /* XXX EINVAL? */
2117 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2119 * Put interface index into the multicast address,
2120 * if the address has link-local scope.
2122 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2123 mreq->ipv6mr_multiaddr.s6_addr16[1]
2124 = htons(mreq->ipv6mr_interface);
2127 * Find the membership in the membership list.
2129 for (imm = im6o->im6o_memberships.lh_first;
2130 imm != NULL; imm = imm->i6mm_chain.le_next) {
2132 imm->i6mm_maddr->in6m_ifp == ifp) &&
2133 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2134 &mreq->ipv6mr_multiaddr))
2138 /* Unable to resolve interface */
2139 error = EADDRNOTAVAIL;
2143 * Give up the multicast address record to which the
2144 * membership points.
2146 LIST_REMOVE(imm, i6mm_chain);
2147 in6_delmulti(imm->i6mm_maddr);
2148 free(imm, M_IPMADDR);
2157 * If all options have default values, no need to keep the mbuf.
2159 if (im6o->im6o_multicast_ifp == NULL &&
2160 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2161 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2162 im6o->im6o_memberships.lh_first == NULL) {
2163 free(*im6op, M_IPMOPTS);
2171 * Return the IP6 multicast options in response to user getsockopt().
2174 ip6_getmoptions(optname, im6o, mp)
2176 struct ip6_moptions *im6o;
2179 u_int *hlim, *loop, *ifindex;
2181 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2185 case IPV6_MULTICAST_IF:
2186 ifindex = mtod(*mp, u_int *);
2187 (*mp)->m_len = sizeof(u_int);
2188 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2191 *ifindex = im6o->im6o_multicast_ifp->if_index;
2194 case IPV6_MULTICAST_HOPS:
2195 hlim = mtod(*mp, u_int *);
2196 (*mp)->m_len = sizeof(u_int);
2198 *hlim = ip6_defmcasthlim;
2200 *hlim = im6o->im6o_multicast_hlim;
2203 case IPV6_MULTICAST_LOOP:
2204 loop = mtod(*mp, u_int *);
2205 (*mp)->m_len = sizeof(u_int);
2207 *loop = ip6_defmcasthlim;
2209 *loop = im6o->im6o_multicast_loop;
2218 * Discard the IP6 multicast options.
2221 ip6_freemoptions(im6o)
2222 struct ip6_moptions *im6o;
2224 struct in6_multi_mship *imm;
2229 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2230 LIST_REMOVE(imm, i6mm_chain);
2231 if (imm->i6mm_maddr)
2232 in6_delmulti(imm->i6mm_maddr);
2233 free(imm, M_IPMADDR);
2235 free(im6o, M_IPMOPTS);
2239 * Set IPv6 outgoing packet options based on advanced API.
2242 ip6_setpktoptions(control, opt, priv, needcopy)
2243 struct mbuf *control;
2244 struct ip6_pktopts *opt;
2247 struct cmsghdr *cm = 0;
2249 if (control == 0 || opt == 0)
2252 init_ip6pktopts(opt);
2255 * XXX: Currently, we assume all the optional information is stored
2258 if (control->m_next)
2261 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2262 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2263 cm = mtod(control, struct cmsghdr *);
2264 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2266 if (cm->cmsg_level != IPPROTO_IPV6)
2270 * XXX should check if RFC2292 API is mixed with 2292bis API
2272 switch (cm->cmsg_type) {
2274 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
2277 /* XXX: Is it really WAITOK? */
2278 opt->ip6po_pktinfo =
2279 malloc(sizeof(struct in6_pktinfo),
2280 M_IP6OPT, M_WAITOK);
2281 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo,
2282 sizeof(struct in6_pktinfo));
2284 opt->ip6po_pktinfo =
2285 (struct in6_pktinfo *)CMSG_DATA(cm);
2286 if (opt->ip6po_pktinfo->ipi6_ifindex &&
2287 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
2288 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
2289 htons(opt->ip6po_pktinfo->ipi6_ifindex);
2291 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
2292 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
2297 * Check if the requested source address is indeed a
2298 * unicast address assigned to the node, and can be
2299 * used as the packet's source address.
2301 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2302 struct in6_ifaddr *ia6;
2303 struct sockaddr_in6 sin6;
2305 bzero(&sin6, sizeof(sin6));
2306 sin6.sin6_len = sizeof(sin6);
2307 sin6.sin6_family = AF_INET6;
2309 opt->ip6po_pktinfo->ipi6_addr;
2310 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2312 (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2313 IN6_IFF_NOTREADY)) != 0)
2314 return(EADDRNOTAVAIL);
2319 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2322 opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
2323 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2331 if (cm->cmsg_len < sizeof(u_char) ||
2332 /* check if cmsg_len is large enough for sa_len */
2333 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2337 opt->ip6po_nexthop =
2338 malloc(*CMSG_DATA(cm),
2339 M_IP6OPT, M_WAITOK);
2340 bcopy(CMSG_DATA(cm),
2344 opt->ip6po_nexthop =
2345 (struct sockaddr *)CMSG_DATA(cm);
2350 struct ip6_hbh *hbh;
2353 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2355 hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2356 hbhlen = (hbh->ip6h_len + 1) << 3;
2357 if (cm->cmsg_len != CMSG_LEN(hbhlen))
2362 malloc(hbhlen, M_IP6OPT, M_WAITOK);
2363 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2365 opt->ip6po_hbh = hbh;
2371 struct ip6_dest *dest, **newdest;
2374 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2376 dest = (struct ip6_dest *)CMSG_DATA(cm);
2377 destlen = (dest->ip6d_len + 1) << 3;
2378 if (cm->cmsg_len != CMSG_LEN(destlen))
2382 * The old advacned API is ambiguous on this
2383 * point. Our approach is to determine the
2384 * position based according to the existence
2385 * of a routing header. Note, however, that
2386 * this depends on the order of the extension
2387 * headers in the ancillary data; the 1st part
2388 * of the destination options header must
2389 * appear before the routing header in the
2390 * ancillary data, too.
2391 * RFC2292bis solved the ambiguity by
2392 * introducing separate cmsg types.
2394 if (opt->ip6po_rthdr == NULL)
2395 newdest = &opt->ip6po_dest1;
2397 newdest = &opt->ip6po_dest2;
2400 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK);
2401 bcopy(dest, *newdest, destlen);
2410 struct ip6_rthdr *rth;
2413 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2415 rth = (struct ip6_rthdr *)CMSG_DATA(cm);
2416 rthlen = (rth->ip6r_len + 1) << 3;
2417 if (cm->cmsg_len != CMSG_LEN(rthlen))
2420 switch (rth->ip6r_type) {
2421 case IPV6_RTHDR_TYPE_0:
2422 /* must contain one addr */
2423 if (rth->ip6r_len == 0)
2425 /* length must be even */
2426 if (rth->ip6r_len % 2)
2428 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2432 return(EINVAL); /* not supported */
2436 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT,
2438 bcopy(rth, opt->ip6po_rthdr, rthlen);
2440 opt->ip6po_rthdr = rth;
2446 return(ENOPROTOOPT);
2454 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2455 * packet to the input queue of a specified interface. Note that this
2456 * calls the output routine of the loopback "driver", but with an interface
2457 * pointer that might NOT be &loif -- easier than replicating that code here.
2460 ip6_mloopback(ifp, m, dst)
2463 struct sockaddr_in6 *dst;
2466 struct ip6_hdr *ip6;
2468 copym = m_copy(m, 0, M_COPYALL);
2473 * Make sure to deep-copy IPv6 header portion in case the data
2474 * is in an mbuf cluster, so that we can safely override the IPv6
2475 * header portion later.
2477 if ((copym->m_flags & M_EXT) != 0 ||
2478 copym->m_len < sizeof(struct ip6_hdr)) {
2479 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2485 if (copym->m_len < sizeof(*ip6)) {
2491 ip6 = mtod(copym, struct ip6_hdr *);
2492 #ifndef SCOPEDROUTING
2494 * clear embedded scope identifiers if necessary.
2495 * in6_clearscope will touch the addresses only when necessary.
2497 in6_clearscope(&ip6->ip6_src);
2498 in6_clearscope(&ip6->ip6_dst);
2501 (void)if_simloop(ifp, copym, dst->sin6_family, NULL);
2505 * Chop IPv6 header off from the payload.
2508 ip6_splithdr(m, exthdrs)
2510 struct ip6_exthdrs *exthdrs;
2513 struct ip6_hdr *ip6;
2515 ip6 = mtod(m, struct ip6_hdr *);
2516 if (m->m_len > sizeof(*ip6)) {
2517 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2522 M_COPY_PKTHDR(mh, m);
2523 MH_ALIGN(mh, sizeof(*ip6));
2524 m->m_flags &= ~M_PKTHDR;
2525 m->m_len -= sizeof(*ip6);
2526 m->m_data += sizeof(*ip6);
2529 m->m_len = sizeof(*ip6);
2530 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2532 exthdrs->ip6e_ip6 = m;
2537 * Compute IPv6 extension header length.
2541 struct in6pcb *in6p;
2545 if (!in6p->in6p_outputopts)
2550 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2552 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2553 if (in6p->in6p_outputopts->ip6po_rthdr)
2554 /* dest1 is valid with rthdr only */
2555 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2556 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2557 len += elen(in6p->in6p_outputopts->ip6po_dest2);