1 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.13.2.18 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/ip6_output.c,v 1.11 2004/06/01 20:49:08 dillon Exp $ */
3 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
69 #include "opt_ip6fw.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73 #include "opt_pfil_hooks.h"
75 #include <sys/param.h>
76 #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>
87 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet6/in6_var.h>
95 #include <netinet/ip6.h>
96 #include <netinet/icmp6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet6/nd6.h>
102 #include <netinet6/ipsec.h>
104 #include <netinet6/ipsec6.h>
106 #include <netproto/key/key.h>
112 #include <netipsec/key.h>
113 #endif /* FAST_IPSEC */
115 #include <net/ip6fw/ip6_fw.h>
117 #include <net/net_osdep.h>
119 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
122 struct mbuf *ip6e_ip6;
123 struct mbuf *ip6e_hbh;
124 struct mbuf *ip6e_dest1;
125 struct mbuf *ip6e_rthdr;
126 struct mbuf *ip6e_dest2;
129 static int ip6_pcbopts (struct ip6_pktopts **, struct mbuf *,
130 struct socket *, struct sockopt *sopt);
131 static int ip6_setmoptions (int, struct ip6_moptions **, struct mbuf *);
132 static int ip6_getmoptions (int, struct ip6_moptions *, struct mbuf **);
133 static int ip6_copyexthdr (struct mbuf **, caddr_t, int);
134 static int ip6_insertfraghdr (struct mbuf *, struct mbuf *, int,
136 static int ip6_insert_jumboopt (struct ip6_exthdrs *, u_int32_t);
137 static int ip6_splithdr (struct mbuf *, struct ip6_exthdrs *);
140 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
141 * header (with pri, len, nxt, hlim, src, dst).
142 * This function may modify ver and hlim only.
143 * The mbuf chain containing the packet will be freed.
144 * The mbuf opt, if present, will not be freed.
146 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
147 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
148 * which is rt_rmx.rmx_mtu.
151 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro,
152 int flags, struct ip6_moptions *im6o,
153 struct ifnet **ifpp, /* XXX: just for statistics */
156 struct ip6_hdr *ip6, *mhip6;
157 struct ifnet *ifp, *origifp;
159 int hlen, tlen, len, off;
160 struct route_in6 ip6route;
161 struct sockaddr_in6 *dst;
163 struct in6_ifaddr *ia = NULL;
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;
172 int needipsectun = 0;
173 struct secpolicy *sp = NULL;
174 struct socket *so = inp ? inp->inp_socket : NULL;
176 ip6 = mtod(m, struct ip6_hdr *);
179 int needipsectun = 0;
180 struct secpolicy *sp = NULL;
182 ip6 = mtod(m, struct ip6_hdr *);
183 #endif /* FAST_IPSEC */
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);
253 /* get a security policy for this packet */
255 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
257 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
260 newipsecstat.ips_out_inval++;
267 switch (sp->policy) {
268 case IPSEC_POLICY_DISCARD:
270 * This packet is just discarded.
272 newipsecstat.ips_out_polvio++;
275 case IPSEC_POLICY_BYPASS:
276 case IPSEC_POLICY_NONE:
277 /* no need to do IPsec. */
281 case IPSEC_POLICY_IPSEC:
282 if (sp->req == NULL) {
283 /* acquire a policy */
284 error = key_spdacquire(sp);
290 case IPSEC_POLICY_ENTRUST:
292 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
294 #endif /* FAST_IPSEC */
297 * Calculate the total length of the extension header chain.
298 * Keep the length of the unfragmentable part for fragmentation.
301 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
302 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
303 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
304 unfragpartlen = optlen + sizeof(struct ip6_hdr);
305 /* NOTE: we don't add AH/ESP length here. do that later. */
306 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
309 * If we need IPsec, or there is at least one extension header,
310 * separate IP6 header from the payload.
312 if ((needipsec || optlen) && !hdrsplit) {
313 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
317 m = exthdrs.ip6e_ip6;
322 ip6 = mtod(m, struct ip6_hdr *);
324 /* adjust mbuf packet header length */
325 m->m_pkthdr.len += optlen;
326 plen = m->m_pkthdr.len - sizeof(*ip6);
328 /* If this is a jumbo payload, insert a jumbo payload option. */
329 if (plen > IPV6_MAXPACKET) {
331 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
335 m = exthdrs.ip6e_ip6;
339 ip6 = mtod(m, struct ip6_hdr *);
340 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
344 ip6->ip6_plen = htons(plen);
347 * Concatenate headers and fill in next header fields.
348 * Here we have, on "m"
350 * and we insert headers accordingly. Finally, we should be getting:
351 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
353 * during the header composing process, "m" points to IPv6 header.
354 * "mprev" points to an extension header prior to esp.
357 u_char *nexthdrp = &ip6->ip6_nxt;
358 struct mbuf *mprev = m;
361 * we treat dest2 specially. this makes IPsec processing
362 * much easier. the goal here is to make mprev point the
363 * mbuf prior to dest2.
365 * result: IPv6 dest2 payload
366 * m and mprev will point to IPv6 header.
368 if (exthdrs.ip6e_dest2) {
370 panic("assumption failed: hdr not split");
371 exthdrs.ip6e_dest2->m_next = m->m_next;
372 m->m_next = exthdrs.ip6e_dest2;
373 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
374 ip6->ip6_nxt = IPPROTO_DSTOPTS;
377 #define MAKE_CHAIN(m, mp, p, i)\
381 panic("assumption failed: hdr not split"); \
382 *mtod((m), u_char *) = *(p);\
384 p = mtod((m), u_char *);\
385 (m)->m_next = (mp)->m_next;\
391 * result: IPv6 hbh dest1 rthdr dest2 payload
392 * m will point to IPv6 header. mprev will point to the
393 * extension header prior to dest2 (rthdr in the above case).
395 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
396 nexthdrp, IPPROTO_HOPOPTS);
397 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
398 nexthdrp, IPPROTO_DSTOPTS);
399 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
400 nexthdrp, IPPROTO_ROUTING);
402 #if defined(IPSEC) || defined(FAST_IPSEC)
407 * pointers after IPsec headers are not valid any more.
408 * other pointers need a great care too.
409 * (IPsec routines should not mangle mbufs prior to AH/ESP)
411 exthdrs.ip6e_dest2 = NULL;
414 struct ip6_rthdr *rh = NULL;
416 struct ipsec_output_state state;
418 if (exthdrs.ip6e_rthdr) {
419 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
420 segleft_org = rh->ip6r_segleft;
421 rh->ip6r_segleft = 0;
424 bzero(&state, sizeof(state));
426 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
430 /* mbuf is already reclaimed in ipsec6_output_trans. */
440 printf("ip6_output (ipsec): error code %d\n", error);
443 /* don't show these error codes to the user */
449 if (exthdrs.ip6e_rthdr) {
450 /* ah6_output doesn't modify mbuf chain */
451 rh->ip6r_segleft = segleft_org;
459 * If there is a routing header, replace destination address field
460 * with the first hop of the routing header.
462 if (exthdrs.ip6e_rthdr) {
463 struct ip6_rthdr *rh =
464 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
465 struct ip6_rthdr *));
466 struct ip6_rthdr0 *rh0;
468 finaldst = ip6->ip6_dst;
469 switch (rh->ip6r_type) {
470 case IPV6_RTHDR_TYPE_0:
471 rh0 = (struct ip6_rthdr0 *)rh;
472 ip6->ip6_dst = rh0->ip6r0_addr[0];
473 bcopy((caddr_t)&rh0->ip6r0_addr[1],
474 (caddr_t)&rh0->ip6r0_addr[0],
475 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
477 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
479 default: /* is it possible? */
485 /* Source address validation */
486 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
487 (flags & IPV6_DADOUTPUT) == 0) {
489 ip6stat.ip6s_badscope++;
492 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
494 ip6stat.ip6s_badscope++;
498 ip6stat.ip6s_localout++;
505 bzero((caddr_t)ro, sizeof(*ro));
508 if (opt && opt->ip6po_rthdr)
509 ro = &opt->ip6po_route;
510 dst = (struct sockaddr_in6 *)&ro->ro_dst;
512 * If there is a cached route,
513 * check that it is to the same destination
514 * and is still up. If not, free it and try again.
516 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
517 dst->sin6_family != AF_INET6 ||
518 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
520 ro->ro_rt = (struct rtentry *)0;
522 if (ro->ro_rt == 0) {
523 bzero(dst, sizeof(*dst));
524 dst->sin6_family = AF_INET6;
525 dst->sin6_len = sizeof(struct sockaddr_in6);
526 dst->sin6_addr = ip6->ip6_dst;
528 /* XXX: sin6_scope_id should already be fixed at this point */
529 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
530 dst->sin6_scope_id = ntohs(dst->sin6_addr.s6_addr16[1]);
533 #if defined(IPSEC) || defined(FAST_IPSEC)
534 if (needipsec && needipsectun) {
535 struct ipsec_output_state state;
538 * All the extension headers will become inaccessible
539 * (since they can be encrypted).
540 * Don't panic, we need no more updates to extension headers
541 * on inner IPv6 packet (since they are now encapsulated).
543 * IPv6 [ESP|AH] IPv6 [extension headers] payload
545 bzero(&exthdrs, sizeof(exthdrs));
546 exthdrs.ip6e_ip6 = m;
548 bzero(&state, sizeof(state));
550 state.ro = (struct route *)ro;
551 state.dst = (struct sockaddr *)dst;
553 error = ipsec6_output_tunnel(&state, sp, flags);
556 ro = (struct route_in6 *)state.ro;
557 dst = (struct sockaddr_in6 *)state.dst;
559 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
570 printf("ip6_output (ipsec): error code %d\n", error);
573 /* don't show these error codes to the user */
580 exthdrs.ip6e_ip6 = m;
584 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
587 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
588 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
590 * interface selection comes here
591 * if an interface is specified from an upper layer,
594 if (ro->ro_rt == 0) {
596 * non-bsdi always clone routes, if parent is
599 rtalloc((struct route *)ro);
601 if (ro->ro_rt == 0) {
602 ip6stat.ip6s_noroute++;
603 error = EHOSTUNREACH;
604 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
607 ia = ifatoia6(ro->ro_rt->rt_ifa);
608 ifp = ro->ro_rt->rt_ifp;
610 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
611 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
612 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
614 in6_ifstat_inc(ifp, ifs6_out_request);
617 * Check if the outgoing interface conflicts with
618 * the interface specified by ifi6_ifindex (if specified).
619 * Note that loopback interface is always okay.
620 * (this may happen when we are sending a packet to one of
621 * our own addresses.)
623 if (opt && opt->ip6po_pktinfo
624 && opt->ip6po_pktinfo->ipi6_ifindex) {
625 if (!(ifp->if_flags & IFF_LOOPBACK)
626 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
627 ip6stat.ip6s_noroute++;
628 in6_ifstat_inc(ifp, ifs6_out_discard);
629 error = EHOSTUNREACH;
634 if (opt && opt->ip6po_hlim != -1)
635 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
638 struct in6_multi *in6m;
640 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
643 * See if the caller provided any multicast options
647 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
648 if (im6o->im6o_multicast_ifp != NULL)
649 ifp = im6o->im6o_multicast_ifp;
651 ip6->ip6_hlim = ip6_defmcasthlim;
654 * See if the caller provided the outgoing interface
655 * as an ancillary data.
656 * Boundary check for ifindex is assumed to be already done.
658 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
659 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
662 * If the destination is a node-local scope multicast,
663 * the packet should be loop-backed only.
665 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
667 * If the outgoing interface is already specified,
668 * it should be a loopback interface.
670 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
671 ip6stat.ip6s_badscope++;
672 error = ENETUNREACH; /* XXX: better error? */
673 /* XXX correct ifp? */
674 in6_ifstat_inc(ifp, ifs6_out_discard);
681 if (opt && opt->ip6po_hlim != -1)
682 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
685 * If caller did not provide an interface lookup a
686 * default in the routing table. This is either a
687 * default for the speicfied group (i.e. a host
688 * route), or a multicast default (a route for the
692 if (ro->ro_rt == 0) {
693 ro->ro_rt = rtalloc1((struct sockaddr *)
694 &ro->ro_dst, 0, 0UL);
696 if (ro->ro_rt == 0) {
697 ip6stat.ip6s_noroute++;
698 error = EHOSTUNREACH;
699 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
702 ia = ifatoia6(ro->ro_rt->rt_ifa);
703 ifp = ro->ro_rt->rt_ifp;
707 if ((flags & IPV6_FORWARDING) == 0)
708 in6_ifstat_inc(ifp, ifs6_out_request);
709 in6_ifstat_inc(ifp, ifs6_out_mcast);
712 * Confirm that the outgoing interface supports multicast.
714 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
715 ip6stat.ip6s_noroute++;
716 in6_ifstat_inc(ifp, ifs6_out_discard);
720 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
722 (im6o == NULL || im6o->im6o_multicast_loop)) {
724 * If we belong to the destination multicast group
725 * on the outgoing interface, and the caller did not
726 * forbid loopback, loop back a copy.
728 ip6_mloopback(ifp, m, dst);
731 * If we are acting as a multicast router, perform
732 * multicast forwarding as if the packet had just
733 * arrived on the interface to which we are about
734 * to send. The multicast forwarding function
735 * recursively calls this function, using the
736 * IPV6_FORWARDING flag to prevent infinite recursion.
738 * Multicasts that are looped back by ip6_mloopback(),
739 * above, will be forwarded by the ip6_input() routine,
742 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
743 if (ip6_mforward(ip6, ifp, m) != 0) {
750 * Multicasts with a hoplimit of zero may be looped back,
751 * above, but must not be transmitted on a network.
752 * Also, multicasts addressed to the loopback interface
753 * are not sent -- the above call to ip6_mloopback() will
754 * loop back a copy if this host actually belongs to the
755 * destination group on the loopback interface.
757 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
764 * Fill the outgoing inteface to tell the upper layer
765 * to increment per-interface statistics.
771 * Determine path MTU.
774 /* The first hop and the final destination may differ. */
775 struct sockaddr_in6 *sin6_fin =
776 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
777 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
778 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
780 RTFREE(ro_pmtu->ro_rt);
781 ro_pmtu->ro_rt = (struct rtentry *)0;
783 if (ro_pmtu->ro_rt == 0) {
784 bzero(sin6_fin, sizeof(*sin6_fin));
785 sin6_fin->sin6_family = AF_INET6;
786 sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
787 sin6_fin->sin6_addr = finaldst;
789 rtalloc((struct route *)ro_pmtu);
792 if (ro_pmtu->ro_rt != NULL) {
793 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu;
795 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
796 if (mtu > ifmtu || mtu == 0) {
798 * The MTU on the route is larger than the MTU on
799 * the interface! This shouldn't happen, unless the
800 * MTU of the interface has been changed after the
801 * interface was brought up. Change the MTU in the
802 * route to match the interface MTU (as long as the
803 * field isn't locked).
805 * if MTU on the route is 0, we need to fix the MTU.
806 * this case happens with path MTU discovery timeouts.
809 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
810 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
813 mtu = nd_ifinfo[ifp->if_index].linkmtu;
817 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
819 if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU)
822 /* Fake scoped addresses */
823 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
825 * If source or destination address is a scoped address, and
826 * the packet is going to be sent to a loopback interface,
827 * we should keep the original interface.
831 * XXX: this is a very experimental and temporary solution.
832 * We eventually have sockaddr_in6 and use the sin6_scope_id
833 * field of the structure here.
834 * We rely on the consistency between two scope zone ids
835 * of source and destination, which should already be assured.
836 * Larger scopes than link will be supported in the future.
839 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
840 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
841 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
842 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
844 * XXX: origifp can be NULL even in those two cases above.
845 * For example, if we remove the (only) link-local address
846 * from the loopback interface, and try to send a link-local
847 * address without link-id information. Then the source
848 * address is ::1, and the destination address is the
849 * link-local address with its s6_addr16[1] being zero.
850 * What is worse, if the packet goes to the loopback interface
851 * by a default rejected route, the null pointer would be
852 * passed to looutput, and the kernel would hang.
853 * The following last resort would prevent such disaster.
860 #ifndef SCOPEDROUTING
862 * clear embedded scope identifiers if necessary.
863 * in6_clearscope will touch the addresses only when necessary.
865 in6_clearscope(&ip6->ip6_src);
866 in6_clearscope(&ip6->ip6_dst);
870 * Check with the firewall...
872 if (ip6_fw_enable && ip6_fw_chk_ptr) {
874 m->m_pkthdr.rcvif = NULL; /* XXX */
875 /* If ipfw says divert, we have to just drop packet */
876 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
887 * If the outgoing packet contains a hop-by-hop options header,
888 * it must be examined and processed even by the source node.
889 * (RFC 2460, section 4.)
891 if (exthdrs.ip6e_hbh) {
892 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
893 u_int32_t dummy1; /* XXX unused */
894 u_int32_t dummy2; /* XXX unused */
897 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
898 panic("ip6e_hbh is not continuous");
901 * XXX: if we have to send an ICMPv6 error to the sender,
902 * we need the M_LOOP flag since icmp6_error() expects
903 * the IPv6 and the hop-by-hop options header are
904 * continuous unless the flag is set.
906 m->m_flags |= M_LOOP;
907 m->m_pkthdr.rcvif = ifp;
908 if (ip6_process_hopopts(m,
909 (u_int8_t *)(hbh + 1),
910 ((hbh->ip6h_len + 1) << 3) -
911 sizeof(struct ip6_hbh),
912 &dummy1, &dummy2) < 0) {
913 /* m was already freed at this point */
914 error = EINVAL;/* better error? */
917 m->m_flags &= ~M_LOOP; /* XXX */
918 m->m_pkthdr.rcvif = NULL;
923 * Run through list of hooks for output packets.
925 if (pfil_has_hooks(&inet6_pfil_hook)) {
926 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT);
927 if (error != 0 || m == NULL)
929 ip6 = mtod(m, struct ip6_hdr *);
931 #endif /* PFIL_HOOKS */
933 * Send the packet to the outgoing interface.
934 * If necessary, do IPv6 fragmentation before sending.
936 tlen = m->m_pkthdr.len;
940 * On any link that cannot convey a 1280-octet packet in one piece,
941 * link-specific fragmentation and reassembly must be provided at
942 * a layer below IPv6. [RFC 2460, sec.5]
943 * Thus if the interface has ability of link-level fragmentation,
944 * we can just send the packet even if the packet size is
945 * larger than the link's MTU.
946 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
949 || ifp->if_flags & IFF_FRAGMENTABLE
953 /* Record statistics for this interface address. */
954 if (ia && !(flags & IPV6_FORWARDING)) {
955 ia->ia_ifa.if_opackets++;
956 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
959 /* clean ipsec history once it goes out of the node */
962 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
964 } else if (mtu < IPV6_MMTU) {
966 * note that path MTU is never less than IPV6_MMTU
970 in6_ifstat_inc(ifp, ifs6_out_fragfail);
972 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
974 in6_ifstat_inc(ifp, ifs6_out_fragfail);
977 struct mbuf **mnext, *m_frgpart;
978 struct ip6_frag *ip6f;
979 u_int32_t id = htonl(ip6_id++);
983 * Too large for the destination or interface;
984 * fragment if possible.
985 * Must be able to put at least 8 bytes per fragment.
987 hlen = unfragpartlen;
988 if (mtu > IPV6_MAXPACKET)
989 mtu = IPV6_MAXPACKET;
991 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
994 in6_ifstat_inc(ifp, ifs6_out_fragfail);
998 mnext = &m->m_nextpkt;
1001 * Change the next header field of the last header in the
1002 * unfragmentable part.
1004 if (exthdrs.ip6e_rthdr) {
1005 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1006 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1007 } else if (exthdrs.ip6e_dest1) {
1008 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1009 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1010 } else if (exthdrs.ip6e_hbh) {
1011 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1012 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1014 nextproto = ip6->ip6_nxt;
1015 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1019 * Loop through length of segment after first fragment,
1020 * make new header and copy data of each part and link onto
1024 for (off = hlen; off < tlen; off += len) {
1025 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1028 ip6stat.ip6s_odropped++;
1031 m->m_pkthdr.rcvif = NULL;
1032 m->m_flags = m0->m_flags & M_COPYFLAGS;
1034 mnext = &m->m_nextpkt;
1035 m->m_data += max_linkhdr;
1036 mhip6 = mtod(m, struct ip6_hdr *);
1038 m->m_len = sizeof(*mhip6);
1039 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1041 ip6stat.ip6s_odropped++;
1044 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1045 if (off + len >= tlen)
1048 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1049 mhip6->ip6_plen = htons((u_short)(len + hlen +
1051 sizeof(struct ip6_hdr)));
1052 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1054 ip6stat.ip6s_odropped++;
1057 m_cat(m, m_frgpart);
1058 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1059 m->m_pkthdr.rcvif = (struct ifnet *)0;
1060 ip6f->ip6f_reserved = 0;
1061 ip6f->ip6f_ident = id;
1062 ip6f->ip6f_nxt = nextproto;
1063 ip6stat.ip6s_ofragments++;
1064 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1067 in6_ifstat_inc(ifp, ifs6_out_fragok);
1071 * Remove leading garbages.
1077 for (m0 = m; m; m = m0) {
1081 /* Record statistics for this interface address. */
1083 ia->ia_ifa.if_opackets++;
1084 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1087 /* clean ipsec history once it goes out of the node */
1090 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1096 ip6stat.ip6s_fragmented++;
1099 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1101 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1102 RTFREE(ro_pmtu->ro_rt);
1112 #endif /* FAST_IPSEC */
1117 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1118 m_freem(exthdrs.ip6e_dest1);
1119 m_freem(exthdrs.ip6e_rthdr);
1120 m_freem(exthdrs.ip6e_dest2);
1128 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1132 if (hlen > MCLBYTES)
1133 return(ENOBUFS); /* XXX */
1135 MGET(m, M_DONTWAIT, MT_DATA);
1140 MCLGET(m, M_DONTWAIT);
1141 if ((m->m_flags & M_EXT) == 0) {
1148 bcopy(hdr, mtod(m, caddr_t), hlen);
1155 * Insert jumbo payload option.
1158 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1164 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1167 * If there is no hop-by-hop options header, allocate new one.
1168 * If there is one but it doesn't have enough space to store the
1169 * jumbo payload option, allocate a cluster to store the whole options.
1170 * Otherwise, use it to store the options.
1172 if (exthdrs->ip6e_hbh == 0) {
1173 MGET(mopt, M_DONTWAIT, MT_DATA);
1176 mopt->m_len = JUMBOOPTLEN;
1177 optbuf = mtod(mopt, u_char *);
1178 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1179 exthdrs->ip6e_hbh = mopt;
1181 struct ip6_hbh *hbh;
1183 mopt = exthdrs->ip6e_hbh;
1184 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1187 * - exthdrs->ip6e_hbh is not referenced from places
1188 * other than exthdrs.
1189 * - exthdrs->ip6e_hbh is not an mbuf chain.
1191 int oldoptlen = mopt->m_len;
1195 * XXX: give up if the whole (new) hbh header does
1196 * not fit even in an mbuf cluster.
1198 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1202 * As a consequence, we must always prepare a cluster
1205 MGET(n, M_DONTWAIT, MT_DATA);
1207 MCLGET(n, M_DONTWAIT);
1208 if ((n->m_flags & M_EXT) == 0) {
1215 n->m_len = oldoptlen + JUMBOOPTLEN;
1216 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1218 optbuf = mtod(n, caddr_t) + oldoptlen;
1220 mopt = exthdrs->ip6e_hbh = n;
1222 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1223 mopt->m_len += JUMBOOPTLEN;
1225 optbuf[0] = IP6OPT_PADN;
1229 * Adjust the header length according to the pad and
1230 * the jumbo payload option.
1232 hbh = mtod(mopt, struct ip6_hbh *);
1233 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1236 /* fill in the option. */
1237 optbuf[2] = IP6OPT_JUMBO;
1239 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1240 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1242 /* finally, adjust the packet header length */
1243 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1250 * Insert fragment header and copy unfragmentable header portions.
1253 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1254 struct ip6_frag **frghdrp)
1256 struct mbuf *n, *mlast;
1258 if (hlen > sizeof(struct ip6_hdr)) {
1259 n = m_copym(m0, sizeof(struct ip6_hdr),
1260 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1267 /* Search for the last mbuf of unfragmentable part. */
1268 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1271 if ((mlast->m_flags & M_EXT) == 0 &&
1272 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1273 /* use the trailing space of the last mbuf for the fragment hdr */
1275 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1276 mlast->m_len += sizeof(struct ip6_frag);
1277 m->m_pkthdr.len += sizeof(struct ip6_frag);
1279 /* allocate a new mbuf for the fragment header */
1282 MGET(mfrg, M_DONTWAIT, MT_DATA);
1285 mfrg->m_len = sizeof(struct ip6_frag);
1286 *frghdrp = mtod(mfrg, struct ip6_frag *);
1287 mlast->m_next = mfrg;
1294 * IP6 socket option processing.
1297 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1300 struct inpcb *in6p = sotoinpcb(so);
1302 int level, op, optname;
1307 level = sopt->sopt_level;
1308 op = sopt->sopt_dir;
1309 optname = sopt->sopt_name;
1310 optlen = sopt->sopt_valsize;
1313 panic("ip6_ctloutput: arg soopt is NULL");
1319 privileged = (td == NULL || suser(td)) ? 0 : 1;
1321 if (level == IPPROTO_IPV6) {
1326 case IPV6_PKTOPTIONS:
1330 error = soopt_getm(sopt, &m); /* XXX */
1333 error = soopt_mcopyin(sopt, m); /* XXX */
1336 error = ip6_pcbopts(&in6p->in6p_outputopts,
1338 m_freem(m); /* XXX */
1343 * Use of some Hop-by-Hop options or some
1344 * Destination options, might require special
1345 * privilege. That is, normal applications
1346 * (without special privilege) might be forbidden
1347 * from setting certain options in outgoing packets,
1348 * and might never see certain options in received
1349 * packets. [RFC 2292 Section 6]
1350 * KAME specific note:
1351 * KAME prevents non-privileged users from sending or
1352 * receiving ANY hbh/dst options in order to avoid
1353 * overhead of parsing options in the kernel.
1355 case IPV6_UNICAST_HOPS:
1360 if (optlen != sizeof(int)) {
1364 error = sooptcopyin(sopt, &optval,
1365 sizeof optval, sizeof optval);
1370 case IPV6_UNICAST_HOPS:
1371 if (optval < -1 || optval >= 256)
1374 /* -1 = kernel default */
1375 in6p->in6p_hops = optval;
1377 if ((in6p->in6p_vflag &
1379 in6p->inp_ip_ttl = optval;
1382 #define OPTSET(bit) \
1385 in6p->in6p_flags |= (bit); \
1387 in6p->in6p_flags &= ~(bit); \
1389 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1392 in6p->in6p_cksum = optval;
1401 * make setsockopt(IPV6_V6ONLY)
1402 * available only prior to bind(2).
1403 * see ipng mailing list, Jun 22 2001.
1405 if (in6p->in6p_lport ||
1406 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1411 OPTSET(IN6P_IPV6_V6ONLY);
1413 in6p->in6p_vflag &= ~INP_IPV4;
1415 in6p->in6p_vflag |= INP_IPV4;
1426 if (optlen != sizeof(int)) {
1430 error = sooptcopyin(sopt, &optval,
1431 sizeof optval, sizeof optval);
1436 OPTSET(IN6P_PKTINFO);
1439 OPTSET(IN6P_HOPLIMIT);
1443 * Check super-user privilege.
1444 * See comments for IPV6_RECVHOPOPTS.
1448 OPTSET(IN6P_HOPOPTS);
1453 OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1462 case IPV6_MULTICAST_IF:
1463 case IPV6_MULTICAST_HOPS:
1464 case IPV6_MULTICAST_LOOP:
1465 case IPV6_JOIN_GROUP:
1466 case IPV6_LEAVE_GROUP:
1469 if (sopt->sopt_valsize > MLEN) {
1474 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_HEADER);
1479 m->m_len = sopt->sopt_valsize;
1480 error = sooptcopyin(sopt, mtod(m, char *),
1481 m->m_len, m->m_len);
1482 error = ip6_setmoptions(sopt->sopt_name,
1483 &in6p->in6p_moptions,
1489 case IPV6_PORTRANGE:
1490 error = sooptcopyin(sopt, &optval,
1491 sizeof optval, sizeof optval);
1496 case IPV6_PORTRANGE_DEFAULT:
1497 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1498 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1501 case IPV6_PORTRANGE_HIGH:
1502 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1503 in6p->in6p_flags |= IN6P_HIGHPORT;
1506 case IPV6_PORTRANGE_LOW:
1507 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1508 in6p->in6p_flags |= IN6P_LOWPORT;
1517 #if defined(IPSEC) || defined(FAST_IPSEC)
1518 case IPV6_IPSEC_POLICY:
1524 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1526 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1529 req = mtod(m, caddr_t);
1532 error = ipsec6_set_policy(in6p, optname, req,
1537 #endif /* KAME IPSEC */
1545 struct mbuf **mp = &m;
1547 if (ip6_fw_ctl_ptr == NULL)
1550 if ((error = soopt_getm(sopt, &m)) != 0)
1553 if ((error = soopt_mcopyin(sopt, m)) != 0)
1555 error = (*ip6_fw_ctl_ptr)(optname, mp);
1561 error = ENOPROTOOPT;
1569 case IPV6_PKTOPTIONS:
1570 if (in6p->in6p_options) {
1572 m = m_copym(in6p->in6p_options,
1573 0, M_COPYALL, M_WAIT);
1574 error = soopt_mcopyout(sopt, m);
1578 sopt->sopt_valsize = 0;
1581 case IPV6_UNICAST_HOPS:
1586 case IPV6_PORTRANGE:
1589 case IPV6_UNICAST_HOPS:
1590 optval = in6p->in6p_hops;
1594 optval = in6p->in6p_cksum;
1598 optval = OPTBIT(IN6P_FAITH);
1602 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1605 case IPV6_PORTRANGE:
1608 flags = in6p->in6p_flags;
1609 if (flags & IN6P_HIGHPORT)
1610 optval = IPV6_PORTRANGE_HIGH;
1611 else if (flags & IN6P_LOWPORT)
1612 optval = IPV6_PORTRANGE_LOW;
1618 error = sooptcopyout(sopt, &optval,
1627 if (optname == IPV6_HOPOPTS ||
1628 optname == IPV6_DSTOPTS ||
1633 optval = OPTBIT(IN6P_PKTINFO);
1636 optval = OPTBIT(IN6P_HOPLIMIT);
1641 optval = OPTBIT(IN6P_HOPOPTS);
1644 optval = OPTBIT(IN6P_RTHDR);
1649 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1652 error = sooptcopyout(sopt, &optval,
1656 case IPV6_MULTICAST_IF:
1657 case IPV6_MULTICAST_HOPS:
1658 case IPV6_MULTICAST_LOOP:
1659 case IPV6_JOIN_GROUP:
1660 case IPV6_LEAVE_GROUP:
1663 error = ip6_getmoptions(sopt->sopt_name,
1664 in6p->in6p_moptions, &m);
1666 error = sooptcopyout(sopt,
1667 mtod(m, char *), m->m_len);
1672 #if defined(IPSEC) || defined(FAST_IPSEC)
1673 case IPV6_IPSEC_POLICY:
1677 struct mbuf *m = NULL;
1678 struct mbuf **mp = &m;
1680 error = soopt_getm(sopt, &m); /* XXX */
1683 error = soopt_mcopyin(sopt, m); /* XXX */
1687 req = mtod(m, caddr_t);
1690 error = ipsec6_get_policy(in6p, req, len, mp);
1692 error = soopt_mcopyout(sopt, m); /*XXX*/
1693 if (error == 0 && m)
1697 #endif /* KAME IPSEC */
1702 struct mbuf **mp = &m;
1704 if (ip6_fw_ctl_ptr == NULL)
1708 error = (*ip6_fw_ctl_ptr)(optname, mp);
1710 error = soopt_mcopyout(sopt, m); /* XXX */
1711 if (error == 0 && m)
1717 error = ENOPROTOOPT;
1729 * Set up IP6 options in pcb for insertion in output packets or
1730 * specifying behavior of outgoing packets.
1733 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so,
1734 struct sockopt *sopt)
1736 struct ip6_pktopts *opt = *pktopt;
1738 struct thread *td = sopt->sopt_td;
1741 /* turn off any old options. */
1744 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1745 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1746 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1747 printf("ip6_pcbopts: all specified options are cleared.\n");
1749 ip6_clearpktopts(opt, 1, -1);
1751 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1754 if (!m || m->m_len == 0) {
1756 * Only turning off any previous options, regardless of
1757 * whether the opt is just created or given.
1759 free(opt, M_IP6OPT);
1763 /* set options specified by user. */
1766 if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) {
1767 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */
1768 free(opt, M_IP6OPT);
1776 * initialize ip6_pktopts. beware that there are non-zero default values in
1780 init_ip6pktopts(struct ip6_pktopts *opt)
1783 bzero(opt, sizeof(*opt));
1784 opt->ip6po_hlim = -1; /* -1 means default hop limit */
1788 ip6_clearpktopts(struct ip6_pktopts *pktopt, int needfree, int optname)
1793 if (optname == -1) {
1794 if (needfree && pktopt->ip6po_pktinfo)
1795 free(pktopt->ip6po_pktinfo, M_IP6OPT);
1796 pktopt->ip6po_pktinfo = NULL;
1799 pktopt->ip6po_hlim = -1;
1800 if (optname == -1) {
1801 if (needfree && pktopt->ip6po_nexthop)
1802 free(pktopt->ip6po_nexthop, M_IP6OPT);
1803 pktopt->ip6po_nexthop = NULL;
1805 if (optname == -1) {
1806 if (needfree && pktopt->ip6po_hbh)
1807 free(pktopt->ip6po_hbh, M_IP6OPT);
1808 pktopt->ip6po_hbh = NULL;
1810 if (optname == -1) {
1811 if (needfree && pktopt->ip6po_dest1)
1812 free(pktopt->ip6po_dest1, M_IP6OPT);
1813 pktopt->ip6po_dest1 = NULL;
1815 if (optname == -1) {
1816 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
1817 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
1818 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
1819 if (pktopt->ip6po_route.ro_rt) {
1820 RTFREE(pktopt->ip6po_route.ro_rt);
1821 pktopt->ip6po_route.ro_rt = NULL;
1824 if (optname == -1) {
1825 if (needfree && pktopt->ip6po_dest2)
1826 free(pktopt->ip6po_dest2, M_IP6OPT);
1827 pktopt->ip6po_dest2 = NULL;
1831 #define PKTOPT_EXTHDRCPY(type) \
1835 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1836 dst->type = malloc(hlen, M_IP6OPT, canwait);\
1837 if (dst->type == NULL && canwait == M_NOWAIT)\
1839 bcopy(src->type, dst->type, hlen);\
1843 struct ip6_pktopts *
1844 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
1846 struct ip6_pktopts *dst;
1849 printf("ip6_clearpktopts: invalid argument\n");
1853 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
1854 if (dst == NULL && canwait == M_NOWAIT)
1856 bzero(dst, sizeof(*dst));
1858 dst->ip6po_hlim = src->ip6po_hlim;
1859 if (src->ip6po_pktinfo) {
1860 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
1862 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
1864 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
1866 if (src->ip6po_nexthop) {
1867 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
1869 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
1871 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
1872 src->ip6po_nexthop->sa_len);
1874 PKTOPT_EXTHDRCPY(ip6po_hbh);
1875 PKTOPT_EXTHDRCPY(ip6po_dest1);
1876 PKTOPT_EXTHDRCPY(ip6po_dest2);
1877 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
1881 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
1882 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
1883 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
1884 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
1885 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
1886 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
1887 free(dst, M_IP6OPT);
1890 #undef PKTOPT_EXTHDRCPY
1893 ip6_freepcbopts(struct ip6_pktopts *pktopt)
1898 ip6_clearpktopts(pktopt, 1, -1);
1900 free(pktopt, M_IP6OPT);
1904 * Set the IP6 multicast options in response to user setsockopt().
1907 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
1910 u_int loop, ifindex;
1911 struct ipv6_mreq *mreq;
1913 struct ip6_moptions *im6o = *im6op;
1914 struct route_in6 ro;
1915 struct sockaddr_in6 *dst;
1916 struct in6_multi_mship *imm;
1917 struct thread *td = curthread; /* XXX */
1921 * No multicast option buffer attached to the pcb;
1922 * allocate one and initialize to default values.
1924 im6o = (struct ip6_moptions *)
1925 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1930 im6o->im6o_multicast_ifp = NULL;
1931 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1932 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1933 LIST_INIT(&im6o->im6o_memberships);
1938 case IPV6_MULTICAST_IF:
1940 * Select the interface for outgoing multicast packets.
1942 if (m == NULL || m->m_len != sizeof(u_int)) {
1946 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1947 if (ifindex < 0 || if_index < ifindex) {
1948 error = ENXIO; /* XXX EINVAL? */
1951 ifp = ifindex2ifnet[ifindex];
1952 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1953 error = EADDRNOTAVAIL;
1956 im6o->im6o_multicast_ifp = ifp;
1959 case IPV6_MULTICAST_HOPS:
1962 * Set the IP6 hoplimit for outgoing multicast packets.
1965 if (m == NULL || m->m_len != sizeof(int)) {
1969 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1970 if (optval < -1 || optval >= 256)
1972 else if (optval == -1)
1973 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1975 im6o->im6o_multicast_hlim = optval;
1979 case IPV6_MULTICAST_LOOP:
1981 * Set the loopback flag for outgoing multicast packets.
1982 * Must be zero or one.
1984 if (m == NULL || m->m_len != sizeof(u_int)) {
1988 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
1993 im6o->im6o_multicast_loop = loop;
1996 case IPV6_JOIN_GROUP:
1998 * Add a multicast group membership.
1999 * Group must be a valid IP6 multicast address.
2001 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2005 mreq = mtod(m, struct ipv6_mreq *);
2006 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2008 * We use the unspecified address to specify to accept
2009 * all multicast addresses. Only super user is allowed
2017 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2023 * If the interface is specified, validate it.
2025 if (mreq->ipv6mr_interface < 0
2026 || if_index < mreq->ipv6mr_interface) {
2027 error = ENXIO; /* XXX EINVAL? */
2031 * If no interface was explicitly specified, choose an
2032 * appropriate one according to the given multicast address.
2034 if (mreq->ipv6mr_interface == 0) {
2036 * If the multicast address is in node-local scope,
2037 * the interface should be a loopback interface.
2038 * Otherwise, look up the routing table for the
2039 * address, and choose the outgoing interface.
2040 * XXX: is it a good approach?
2042 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
2046 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2047 bzero(dst, sizeof(*dst));
2048 dst->sin6_len = sizeof(struct sockaddr_in6);
2049 dst->sin6_family = AF_INET6;
2050 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2051 rtalloc((struct route *)&ro);
2052 if (ro.ro_rt == NULL) {
2053 error = EADDRNOTAVAIL;
2056 ifp = ro.ro_rt->rt_ifp;
2060 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2063 * See if we found an interface, and confirm that it
2064 * supports multicast
2066 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2067 error = EADDRNOTAVAIL;
2071 * Put interface index into the multicast address,
2072 * if the address has link-local scope.
2074 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2075 mreq->ipv6mr_multiaddr.s6_addr16[1]
2076 = htons(mreq->ipv6mr_interface);
2079 * See if the membership already exists.
2081 for (imm = im6o->im6o_memberships.lh_first;
2082 imm != NULL; imm = imm->i6mm_chain.le_next)
2083 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2084 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2085 &mreq->ipv6mr_multiaddr))
2092 * Everything looks good; add a new record to the multicast
2093 * address list for the given interface.
2095 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2100 if ((imm->i6mm_maddr =
2101 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2102 free(imm, M_IPMADDR);
2105 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2108 case IPV6_LEAVE_GROUP:
2110 * Drop a multicast group membership.
2111 * Group must be a valid IP6 multicast address.
2113 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2117 mreq = mtod(m, struct ipv6_mreq *);
2118 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2123 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2128 * If an interface address was specified, get a pointer
2129 * to its ifnet structure.
2131 if (mreq->ipv6mr_interface < 0
2132 || if_index < mreq->ipv6mr_interface) {
2133 error = ENXIO; /* XXX EINVAL? */
2136 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2138 * Put interface index into the multicast address,
2139 * if the address has link-local scope.
2141 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2142 mreq->ipv6mr_multiaddr.s6_addr16[1]
2143 = htons(mreq->ipv6mr_interface);
2146 * Find the membership in the membership list.
2148 for (imm = im6o->im6o_memberships.lh_first;
2149 imm != NULL; imm = imm->i6mm_chain.le_next) {
2151 imm->i6mm_maddr->in6m_ifp == ifp) &&
2152 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2153 &mreq->ipv6mr_multiaddr))
2157 /* Unable to resolve interface */
2158 error = EADDRNOTAVAIL;
2162 * Give up the multicast address record to which the
2163 * membership points.
2165 LIST_REMOVE(imm, i6mm_chain);
2166 in6_delmulti(imm->i6mm_maddr);
2167 free(imm, M_IPMADDR);
2176 * If all options have default values, no need to keep the mbuf.
2178 if (im6o->im6o_multicast_ifp == NULL &&
2179 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2180 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2181 im6o->im6o_memberships.lh_first == NULL) {
2182 free(*im6op, M_IPMOPTS);
2190 * Return the IP6 multicast options in response to user getsockopt().
2193 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2195 u_int *hlim, *loop, *ifindex;
2197 *mp = m_get(M_WAIT, MT_HEADER); /* XXX */
2201 case IPV6_MULTICAST_IF:
2202 ifindex = mtod(*mp, u_int *);
2203 (*mp)->m_len = sizeof(u_int);
2204 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2207 *ifindex = im6o->im6o_multicast_ifp->if_index;
2210 case IPV6_MULTICAST_HOPS:
2211 hlim = mtod(*mp, u_int *);
2212 (*mp)->m_len = sizeof(u_int);
2214 *hlim = ip6_defmcasthlim;
2216 *hlim = im6o->im6o_multicast_hlim;
2219 case IPV6_MULTICAST_LOOP:
2220 loop = mtod(*mp, u_int *);
2221 (*mp)->m_len = sizeof(u_int);
2223 *loop = ip6_defmcasthlim;
2225 *loop = im6o->im6o_multicast_loop;
2234 * Discard the IP6 multicast options.
2237 ip6_freemoptions(struct ip6_moptions *im6o)
2239 struct in6_multi_mship *imm;
2244 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2245 LIST_REMOVE(imm, i6mm_chain);
2246 if (imm->i6mm_maddr)
2247 in6_delmulti(imm->i6mm_maddr);
2248 free(imm, M_IPMADDR);
2250 free(im6o, M_IPMOPTS);
2254 * Set IPv6 outgoing packet options based on advanced API.
2257 ip6_setpktoptions(struct mbuf *control, struct ip6_pktopts *opt, int priv,
2260 struct cmsghdr *cm = 0;
2262 if (control == 0 || opt == 0)
2265 init_ip6pktopts(opt);
2268 * XXX: Currently, we assume all the optional information is stored
2271 if (control->m_next)
2274 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2275 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2276 cm = mtod(control, struct cmsghdr *);
2277 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2279 if (cm->cmsg_level != IPPROTO_IPV6)
2283 * XXX should check if RFC2292 API is mixed with 2292bis API
2285 switch (cm->cmsg_type) {
2287 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
2290 /* XXX: Is it really WAITOK? */
2291 opt->ip6po_pktinfo =
2292 malloc(sizeof(struct in6_pktinfo),
2293 M_IP6OPT, M_WAITOK);
2294 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo,
2295 sizeof(struct in6_pktinfo));
2297 opt->ip6po_pktinfo =
2298 (struct in6_pktinfo *)CMSG_DATA(cm);
2299 if (opt->ip6po_pktinfo->ipi6_ifindex &&
2300 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
2301 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
2302 htons(opt->ip6po_pktinfo->ipi6_ifindex);
2304 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
2305 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
2310 * Check if the requested source address is indeed a
2311 * unicast address assigned to the node, and can be
2312 * used as the packet's source address.
2314 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2315 struct in6_ifaddr *ia6;
2316 struct sockaddr_in6 sin6;
2318 bzero(&sin6, sizeof(sin6));
2319 sin6.sin6_len = sizeof(sin6);
2320 sin6.sin6_family = AF_INET6;
2322 opt->ip6po_pktinfo->ipi6_addr;
2323 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2325 (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2326 IN6_IFF_NOTREADY)) != 0)
2327 return(EADDRNOTAVAIL);
2332 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2335 opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
2336 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2344 if (cm->cmsg_len < sizeof(u_char) ||
2345 /* check if cmsg_len is large enough for sa_len */
2346 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2350 opt->ip6po_nexthop =
2351 malloc(*CMSG_DATA(cm),
2352 M_IP6OPT, M_WAITOK);
2353 bcopy(CMSG_DATA(cm),
2357 opt->ip6po_nexthop =
2358 (struct sockaddr *)CMSG_DATA(cm);
2363 struct ip6_hbh *hbh;
2366 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2368 hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2369 hbhlen = (hbh->ip6h_len + 1) << 3;
2370 if (cm->cmsg_len != CMSG_LEN(hbhlen))
2375 malloc(hbhlen, M_IP6OPT, M_WAITOK);
2376 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2378 opt->ip6po_hbh = hbh;
2384 struct ip6_dest *dest, **newdest;
2387 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2389 dest = (struct ip6_dest *)CMSG_DATA(cm);
2390 destlen = (dest->ip6d_len + 1) << 3;
2391 if (cm->cmsg_len != CMSG_LEN(destlen))
2395 * The old advacned API is ambiguous on this
2396 * point. Our approach is to determine the
2397 * position based according to the existence
2398 * of a routing header. Note, however, that
2399 * this depends on the order of the extension
2400 * headers in the ancillary data; the 1st part
2401 * of the destination options header must
2402 * appear before the routing header in the
2403 * ancillary data, too.
2404 * RFC2292bis solved the ambiguity by
2405 * introducing separate cmsg types.
2407 if (opt->ip6po_rthdr == NULL)
2408 newdest = &opt->ip6po_dest1;
2410 newdest = &opt->ip6po_dest2;
2413 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK);
2414 bcopy(dest, *newdest, destlen);
2423 struct ip6_rthdr *rth;
2426 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2428 rth = (struct ip6_rthdr *)CMSG_DATA(cm);
2429 rthlen = (rth->ip6r_len + 1) << 3;
2430 if (cm->cmsg_len != CMSG_LEN(rthlen))
2433 switch (rth->ip6r_type) {
2434 case IPV6_RTHDR_TYPE_0:
2435 /* must contain one addr */
2436 if (rth->ip6r_len == 0)
2438 /* length must be even */
2439 if (rth->ip6r_len % 2)
2441 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2445 return(EINVAL); /* not supported */
2449 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT,
2451 bcopy(rth, opt->ip6po_rthdr, rthlen);
2453 opt->ip6po_rthdr = rth;
2459 return(ENOPROTOOPT);
2467 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2468 * packet to the input queue of a specified interface. Note that this
2469 * calls the output routine of the loopback "driver", but with an interface
2470 * pointer that might NOT be &loif -- easier than replicating that code here.
2473 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2476 struct ip6_hdr *ip6;
2478 copym = m_copy(m, 0, M_COPYALL);
2483 * Make sure to deep-copy IPv6 header portion in case the data
2484 * is in an mbuf cluster, so that we can safely override the IPv6
2485 * header portion later.
2487 if ((copym->m_flags & M_EXT) != 0 ||
2488 copym->m_len < sizeof(struct ip6_hdr)) {
2489 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2495 if (copym->m_len < sizeof(*ip6)) {
2501 ip6 = mtod(copym, struct ip6_hdr *);
2502 #ifndef SCOPEDROUTING
2504 * clear embedded scope identifiers if necessary.
2505 * in6_clearscope will touch the addresses only when necessary.
2507 in6_clearscope(&ip6->ip6_src);
2508 in6_clearscope(&ip6->ip6_dst);
2511 (void)if_simloop(ifp, copym, dst->sin6_family, NULL);
2515 * Chop IPv6 header off from the payload.
2518 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2521 struct ip6_hdr *ip6;
2523 ip6 = mtod(m, struct ip6_hdr *);
2524 if (m->m_len > sizeof(*ip6)) {
2525 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2530 M_MOVE_PKTHDR(mh, m);
2531 MH_ALIGN(mh, sizeof(*ip6));
2532 m->m_len -= sizeof(*ip6);
2533 m->m_data += sizeof(*ip6);
2536 m->m_len = sizeof(*ip6);
2537 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2539 exthdrs->ip6e_ip6 = m;
2544 * Compute IPv6 extension header length.
2547 ip6_optlen(struct in6pcb *in6p)
2551 if (!in6p->in6p_outputopts)
2556 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2558 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2559 if (in6p->in6p_outputopts->ip6po_rthdr)
2560 /* dest1 is valid with rthdr only */
2561 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2562 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2563 len += elen(in6p->in6p_outputopts->ip6po_dest2);