2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
69 #include "opt_route.h"
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
76 #include <sys/domain.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/errno.h>
82 #include <sys/kernel.h>
83 #include <sys/syslog.h>
84 #include <sys/vimage.h>
87 #include <net/if_types.h>
88 #include <net/if_dl.h>
89 #include <net/route.h>
90 #include <net/netisr.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <net/if_llatbl.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_icmp.h>
100 #include <netinet/vinet.h>
102 #include <netinet/ip6.h>
103 #include <netinet6/in6_var.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/icmp6.h>
107 #include <netinet6/scope6_var.h>
108 #include <netinet6/in6_ifattach.h>
109 #include <netinet6/nd6.h>
110 #include <netinet6/vinet6.h>
113 #include <netipsec/ipsec.h>
114 #include <netinet6/ip6_ipsec.h>
115 #include <netipsec/ipsec6.h>
118 #include <netinet6/ip6protosw.h>
120 extern struct domain inet6domain;
122 u_char ip6_protox[IPPROTO_MAX];
123 static struct ifqueue ip6intrq;
126 #ifndef VIMAGE_GLOBALS
127 struct vnet_inet6 vnet_inet6_0;
131 #ifdef VIMAGE_GLOBALS
132 static int ip6qmaxlen;
133 struct in6_ifaddr *in6_ifaddr;
134 struct ip6stat ip6stat;
136 extern struct callout in6_tmpaddrtimer_ch;
139 extern int pmtu_expire;
140 extern int pmtu_probe;
141 extern u_long rip6_sendspace;
142 extern u_long rip6_recvspace;
143 extern int icmp6errppslim;
144 extern int icmp6_nodeinfo;
145 extern int udp6_sendspace;
146 extern int udp6_recvspace;
149 struct pfil_head inet6_pfil_hook;
151 static void ip6_init2(void *);
152 static struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *);
153 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
155 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
159 * IP6 initialization: fill in IP6 protocol switch table.
160 * All protocols not implemented in kernel go to raw IP6 protocol handler.
165 INIT_VNET_INET6(curvnet);
166 struct ip6protosw *pr;
169 V_ip6qmaxlen = IFQ_MAXLEN;
171 #ifdef IP6_AUTO_LINKLOCAL
172 V_ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
174 V_ip6_auto_linklocal = 1; /* enable by default */
176 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
177 &V_ip6_auto_linklocal);
179 #ifndef IPV6FORWARDING
181 #define IPV6FORWARDING 1 /* forward IP6 packets not for us */
183 #define IPV6FORWARDING 0 /* don't forward IP6 packets not for us */
184 #endif /* GATEWAY6 */
185 #endif /* !IPV6FORWARDING */
187 #ifndef IPV6_SENDREDIRECTS
188 #define IPV6_SENDREDIRECTS 1
191 V_ip6_forwarding = IPV6FORWARDING; /* act as router? */
192 V_ip6_sendredirects = IPV6_SENDREDIRECTS;
193 V_ip6_defhlim = IPV6_DEFHLIM;
194 V_ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
195 V_ip6_accept_rtadv = 0; /* "IPV6FORWARDING ? 0 : 1" is dangerous */
196 V_ip6_log_interval = 5;
197 V_ip6_hdrnestlimit = 15; /* How many header options will we process? */
198 V_ip6_dad_count = 1; /* DupAddrDetectionTransmits */
199 V_ip6_auto_flowlabel = 1;
200 V_ip6_use_deprecated = 1;/* allow deprecated addr (RFC2462 5.5.4) */
201 V_ip6_rr_prune = 5; /* router renumbering prefix
202 * walk list every 5 sec. */
203 V_ip6_mcast_pmtu = 0; /* enable pMTU discovery for multicast? */
206 V_ip6_log_time = (time_t)0L;
210 V_nd6_onlink_ns_rfc4861 = 0; /* allow 'on-link' nd6 NS (RFC 4861) */
212 V_pmtu_expire = 60*10;
215 /* raw IP6 parameters */
217 * Nominal space allocated to a raw ip socket.
219 #define RIPV6SNDQ 8192
220 #define RIPV6RCVQ 8192
221 V_rip6_sendspace = RIPV6SNDQ;
222 V_rip6_recvspace = RIPV6RCVQ;
224 /* ICMPV6 parameters */
225 V_icmp6_rediraccept = 1; /* accept and process redirects */
226 V_icmp6_redirtimeout = 10 * 60; /* 10 minutes */
227 V_icmp6errppslim = 100; /* 100pps */
228 /* control how to respond to NI queries */
229 V_icmp6_nodeinfo = (ICMP6_NODEINFO_FQDNOK|ICMP6_NODEINFO_NODEADDROK);
231 /* UDP on IP6 parameters */
232 V_udp6_sendspace = 9216; /* really max datagram size */
233 V_udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
234 /* 40 1K datagrams */
238 if (sizeof(struct protosw) != sizeof(struct ip6protosw))
239 panic("sizeof(protosw) != sizeof(ip6protosw)");
241 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
245 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
246 for (i = 0; i < IPPROTO_MAX; i++)
247 ip6_protox[i] = pr - inet6sw;
249 * Cycle through IP protocols and put them into the appropriate place
252 for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
253 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
254 if (pr->pr_domain->dom_family == PF_INET6 &&
255 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
256 /* Be careful to only index valid IP protocols. */
257 if (pr->pr_protocol < IPPROTO_MAX)
258 ip6_protox[pr->pr_protocol] = pr - inet6sw;
261 /* Initialize packet filter hooks. */
262 inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
263 inet6_pfil_hook.ph_af = AF_INET6;
264 if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
265 printf("%s: WARNING: unable to register pfil hook, "
266 "error %d\n", __func__, i);
268 ip6intrq.ifq_maxlen = V_ip6qmaxlen;
269 mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
270 netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
272 addrsel_policy_init();
275 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
279 ip6_init2(void *dummy)
281 INIT_VNET_INET6(curvnet);
284 callout_init(&V_nd6_timer_ch, 0);
285 callout_reset(&V_nd6_timer_ch, hz, nd6_timer, NULL);
287 /* timer for regeneranation of temporary addresses randomize ID */
288 callout_init(&V_in6_tmpaddrtimer_ch, 0);
289 callout_reset(&V_in6_tmpaddrtimer_ch,
290 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
291 V_ip6_temp_regen_advance) * hz,
292 in6_tmpaddrtimer, NULL);
296 /* This must be after route_init(), which is now SI_ORDER_THIRD */
297 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
300 ip6_input(struct mbuf *m)
302 INIT_VNET_NET(curvnet);
303 INIT_VNET_INET6(curvnet);
305 int off = sizeof(struct ip6_hdr), nest;
307 u_int32_t rtalert = ~0;
309 struct ifnet *deliverifp = NULL, *ifp = NULL;
310 struct in6_addr odst;
311 struct route_in6 rin6;
313 struct llentry *lle = NULL;
314 struct sockaddr_in6 dst6, *dst;
316 bzero(&rin6, sizeof(struct route_in6));
319 * should the inner packet be considered authentic?
320 * see comment in ah4_input().
321 * NB: m cannot be NULL when passed to the input routine
324 m->m_flags &= ~M_AUTHIPHDR;
325 m->m_flags &= ~M_AUTHIPDGM;
330 * make sure we don't have onion peering information into m_tag.
337 if (m->m_flags & M_EXT) {
339 V_ip6stat.ip6s_mext2m++;
341 V_ip6stat.ip6s_mext1++;
343 #define M2MMAX (sizeof(V_ip6stat.ip6s_m2m)/sizeof(V_ip6stat.ip6s_m2m[0]))
345 if (m->m_flags & M_LOOP) {
346 V_ip6stat.ip6s_m2m[V_loif[0].if_index]++; /* XXX */
347 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
348 V_ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
350 V_ip6stat.ip6s_m2m[0]++;
356 /* drop the packet if IPv6 operation is disabled on the IF */
357 if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
362 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
363 V_ip6stat.ip6s_total++;
365 #ifndef PULLDOWN_TEST
367 * L2 bridge code and some other code can return mbuf chain
368 * that does not conform to KAME requirement. too bad.
369 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
371 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
374 MGETHDR(n, M_DONTWAIT, MT_HEADER);
377 if (n && n->m_pkthdr.len > MHLEN) {
378 MCLGET(n, M_DONTWAIT);
379 if ((n->m_flags & M_EXT) == 0) {
386 return; /* ENOBUFS */
389 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
390 n->m_len = n->m_pkthdr.len;
394 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
397 if (m->m_len < sizeof(struct ip6_hdr)) {
399 inifp = m->m_pkthdr.rcvif;
400 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
401 V_ip6stat.ip6s_toosmall++;
402 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
407 ip6 = mtod(m, struct ip6_hdr *);
409 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
410 V_ip6stat.ip6s_badvers++;
411 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
415 V_ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
418 * Check against address spoofing/corruption.
420 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
421 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
423 * XXX: "badscope" is not very suitable for a multicast source.
425 V_ip6stat.ip6s_badscope++;
426 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
429 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
430 !(m->m_flags & M_LOOP)) {
432 * In this case, the packet should come from the loopback
433 * interface. However, we cannot just check the if_flags,
434 * because ip6_mloopback() passes the "actual" interface
435 * as the outgoing/incoming interface.
437 V_ip6stat.ip6s_badscope++;
438 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
443 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
444 /* packet is dropped by traffic conditioner */
449 * The following check is not documented in specs. A malicious
450 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
451 * and bypass security checks (act as if it was from 127.0.0.1 by using
452 * IPv6 src ::ffff:127.0.0.1). Be cautious.
454 * This check chokes if we are in an SIIT cloud. As none of BSDs
455 * support IPv4-less kernel compilation, we cannot support SIIT
456 * environment at all. So, it makes more sense for us to reject any
457 * malicious packets for non-SIIT environment, than try to do a
458 * partial support for SIIT environment.
460 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
461 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
462 V_ip6stat.ip6s_badscope++;
463 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
468 * Reject packets with IPv4 compatible addresses (auto tunnel).
470 * The code forbids auto tunnel relay case in RFC1933 (the check is
471 * stronger than RFC1933). We may want to re-enable it if mech-xx
472 * is revised to forbid relaying case.
474 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
475 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
476 V_ip6stat.ip6s_badscope++;
477 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
483 * Run through list of hooks for input packets.
485 * NB: Beware of the destination address changing
486 * (e.g. by NAT rewriting). When this happens,
487 * tell ip6_forward to do the right thing.
491 /* Jump over all PFIL processing if hooks are not active. */
492 if (!PFIL_HOOKED(&inet6_pfil_hook))
495 if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL))
497 if (m == NULL) /* consumed by filter */
499 ip6 = mtod(m, struct ip6_hdr *);
500 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
504 * Disambiguate address scope zones (if there is ambiguity).
505 * We first make sure that the original source or destination address
506 * is not in our internal form for scoped addresses. Such addresses
507 * are not necessarily invalid spec-wise, but we cannot accept them due
508 * to the usage conflict.
509 * in6_setscope() then also checks and rejects the cases where src or
510 * dst are the loopback address and the receiving interface
513 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
514 V_ip6stat.ip6s_badscope++; /* XXX */
517 if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
518 in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
519 V_ip6stat.ip6s_badscope++;
526 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
527 struct in6_multi *in6m = 0;
529 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
531 * See if we belong to the destination multicast group on the
534 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
537 else if (!ip6_mrouter) {
538 V_ip6stat.ip6s_notmember++;
539 V_ip6stat.ip6s_cantforward++;
540 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
543 deliverifp = m->m_pkthdr.rcvif;
551 bzero(&dst6, sizeof(dst6));
552 dst6.sin6_family = AF_INET6;
553 dst6.sin6_len = sizeof(struct sockaddr_in6);
554 dst6.sin6_addr = ip6->ip6_dst;
555 ifp = m->m_pkthdr.rcvif;
557 lle = lla_lookup(LLTABLE6(ifp), 0,
558 (struct sockaddr *)&dst6);
559 IF_AFDATA_UNLOCK(ifp);
560 if ((lle != NULL) && (lle->la_flags & LLE_IFADDR)) {
570 dst->sin6_len = sizeof(struct sockaddr_in6);
571 dst->sin6_family = AF_INET6;
572 dst->sin6_addr = ip6->ip6_dst;
573 rin6.ro_rt = rtalloc1((struct sockaddr *)dst, 0, 0);
575 RT_UNLOCK(rin6.ro_rt);
577 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
580 * Accept the packet if the forwarding interface to the destination
581 * according to the routing table is the loopback interface,
582 * unless the associated route has a gateway.
583 * Note that this approach causes to accept a packet if there is a
584 * route to the loopback interface for the destination of the packet.
585 * But we think it's even useful in some situations, e.g. when using
586 * a special daemon which wants to intercept the packet.
588 * XXX: some OSes automatically make a cloned route for the destination
589 * of an outgoing packet. If the outgoing interface of the packet
590 * is a loopback one, the kernel would consider the packet to be
591 * accepted, even if we have no such address assinged on the interface.
592 * We check the cloned flag of the route entry to reject such cases,
593 * assuming that route entries for our own addresses are not made by
594 * cloning (it should be true because in6_addloop explicitly installs
595 * the host route). However, we might have to do an explicit check
596 * while it would be less efficient. Or, should we rather install a
597 * reject route for such a case?
600 (rin6.ro_rt->rt_flags &
601 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
603 !(rin6.ro_rt->rt_flags & RTF_WASCLONED) &&
606 !(rin6.ro_rt->rt_flags & RTF_CLONED) &&
610 * The check below is redundant since the comparison of
611 * the destination and the key of the rtentry has
612 * already done through looking up the routing table.
614 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
615 &rt6_key(rin6.ro_rt)->sin6_addr)
617 rin6.ro_rt->rt_ifp->if_type == IFT_LOOP) {
618 struct in6_ifaddr *ia6 =
619 (struct in6_ifaddr *)rin6.ro_rt->rt_ifa;
622 * record address information into m_tag.
624 (void)ip6_setdstifaddr(m, ia6);
627 * packets to a tentative, duplicated, or somehow invalid
628 * address must not be accepted.
630 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
631 /* this address is ready */
633 deliverifp = ia6->ia_ifp; /* correct? */
634 /* Count the packet in the ip address stats */
635 ia6->ia_ifa.if_ipackets++;
636 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
639 char ip6bufs[INET6_ADDRSTRLEN];
640 char ip6bufd[INET6_ADDRSTRLEN];
641 /* address is not ready, so discard the packet. */
643 "ip6_input: packet to an unready address %s->%s\n",
644 ip6_sprintf(ip6bufs, &ip6->ip6_src),
645 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
652 * FAITH (Firewall Aided Internet Translator)
654 if (V_ip6_keepfaith) {
655 if (rin6.ro_rt && rin6.ro_rt->rt_ifp &&
656 rin6.ro_rt->rt_ifp->if_type == IFT_FAITH) {
657 /* XXX do we need more sanity checks? */
659 deliverifp = rin6.ro_rt->rt_ifp; /* faith */
665 * Now there is no reason to process the packet if it's not our own
666 * and we're not a router.
668 if (!V_ip6_forwarding) {
669 V_ip6stat.ip6s_cantforward++;
670 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
676 * record address information into m_tag, if we don't have one yet.
677 * note that we are unable to record it, if the address is not listed
678 * as our interface address (e.g. multicast addresses, addresses
679 * within FAITH prefixes and such).
681 if (deliverifp && !ip6_getdstifaddr(m)) {
682 struct in6_ifaddr *ia6;
684 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
686 if (!ip6_setdstifaddr(m, ia6)) {
688 * XXX maybe we should drop the packet here,
689 * as we could not provide enough information
690 * to the upper layers.
697 * Process Hop-by-Hop options header if it's contained.
698 * m may be modified in ip6_hopopts_input().
699 * If a JumboPayload option is included, plen will also be modified.
701 plen = (u_int32_t)ntohs(ip6->ip6_plen);
702 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
705 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
706 #if 0 /*touches NULL pointer*/
707 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
709 goto out; /* m have already been freed */
713 ip6 = mtod(m, struct ip6_hdr *);
716 * if the payload length field is 0 and the next header field
717 * indicates Hop-by-Hop Options header, then a Jumbo Payload
718 * option MUST be included.
720 if (ip6->ip6_plen == 0 && plen == 0) {
722 * Note that if a valid jumbo payload option is
723 * contained, ip6_hopopts_input() must set a valid
724 * (non-zero) payload length to the variable plen.
726 V_ip6stat.ip6s_badoptions++;
727 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
728 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
729 icmp6_error(m, ICMP6_PARAM_PROB,
730 ICMP6_PARAMPROB_HEADER,
731 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
734 #ifndef PULLDOWN_TEST
735 /* ip6_hopopts_input() ensures that mbuf is contiguous */
736 hbh = (struct ip6_hbh *)(ip6 + 1);
738 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
739 sizeof(struct ip6_hbh));
741 V_ip6stat.ip6s_tooshort++;
748 * If we are acting as a router and the packet contains a
749 * router alert option, see if we know the option value.
750 * Currently, we only support the option value for MLD, in which
751 * case we should pass the packet to the multicast routing
754 if (rtalert != ~0 && V_ip6_forwarding) {
756 case IP6OPT_RTALERT_MLD:
761 * RFC2711 requires unrecognized values must be
771 * Check that the amount of data in the buffers
772 * is as at least much as the IPv6 header would have us expect.
773 * Trim mbufs if longer than we expect.
774 * Drop packet if shorter than we expect.
776 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
777 V_ip6stat.ip6s_tooshort++;
778 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
781 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
782 if (m->m_len == m->m_pkthdr.len) {
783 m->m_len = sizeof(struct ip6_hdr) + plen;
784 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
786 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
790 * Forward if desirable.
792 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
794 * If we are acting as a multicast router, all
795 * incoming multicast packets are passed to the
796 * kernel-level multicast forwarding function.
797 * The packet is returned (relatively) intact; if
798 * ip6_mforward() returns a non-zero value, the packet
799 * must be discarded, else it may be accepted below.
801 if (ip6_mrouter && ip6_mforward &&
802 ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
803 V_ip6stat.ip6s_cantforward++;
809 ip6_forward(m, srcrt);
813 ip6 = mtod(m, struct ip6_hdr *);
816 * Malicious party may be able to use IPv4 mapped addr to confuse
817 * tcp/udp stack and bypass security checks (act as if it was from
818 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
820 * For SIIT end node behavior, you may want to disable the check.
821 * However, you will become vulnerable to attacks using IPv4 mapped
824 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
825 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
826 V_ip6stat.ip6s_badscope++;
827 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
832 * Tell launch routine the next header
834 V_ip6stat.ip6s_delivered++;
835 in6_ifstat_inc(deliverifp, ifs6_in_deliver);
838 while (nxt != IPPROTO_DONE) {
839 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
840 V_ip6stat.ip6s_toomanyhdr++;
845 * protection against faulty packet - there should be
846 * more sanity checks in header chain processing.
848 if (m->m_pkthdr.len < off) {
849 V_ip6stat.ip6s_tooshort++;
850 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
856 * enforce IPsec policy checking if we are seeing last header.
857 * note that we do not visit this with protocols with pcb layer
858 * code - like udp/tcp/raw ip.
860 if (ip6_ipsec_input(m, nxt))
863 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
874 * set/grab in6_ifaddr correspond to IPv6 destination address.
875 * XXX backward compatibility wrapper
877 static struct ip6aux *
878 ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
882 ip6a = ip6_addaux(m);
884 ip6a->ip6a_dstia6 = ia6;
885 return ip6a; /* NULL if failed to set */
889 ip6_getdstifaddr(struct mbuf *m)
893 ip6a = ip6_findaux(m);
895 return ip6a->ip6a_dstia6;
901 * Hop-by-Hop options header processing. If a valid jumbo payload option is
902 * included, the real payload length will be stored in plenp.
904 * rtalertp - XXX: should be stored more smart way
907 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
908 struct mbuf **mp, int *offp)
910 INIT_VNET_INET6(curvnet);
911 struct mbuf *m = *mp;
912 int off = *offp, hbhlen;
916 /* validation of the length of the header */
917 #ifndef PULLDOWN_TEST
918 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
919 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
920 hbhlen = (hbh->ip6h_len + 1) << 3;
922 IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
923 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
925 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
926 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
928 V_ip6stat.ip6s_tooshort++;
931 hbhlen = (hbh->ip6h_len + 1) << 3;
932 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
935 V_ip6stat.ip6s_tooshort++;
940 hbhlen -= sizeof(struct ip6_hbh);
941 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
943 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
944 hbhlen, rtalertp, plenp) < 0)
953 * Search header for all Hop-by-hop options and process each option.
954 * This function is separate from ip6_hopopts_input() in order to
955 * handle a case where the sending node itself process its hop-by-hop
956 * options header. In such a case, the function is called from ip6_output().
958 * The function assumes that hbh header is located right after the IPv6 header
959 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
960 * opthead + hbhlen is located in continuous memory region.
963 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
964 u_int32_t *rtalertp, u_int32_t *plenp)
966 INIT_VNET_INET6(curvnet);
969 u_int8_t *opt = opthead;
970 u_int16_t rtalert_val;
972 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
974 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
980 if (hbhlen < IP6OPT_MINLEN) {
981 V_ip6stat.ip6s_toosmall++;
984 optlen = *(opt + 1) + 2;
986 case IP6OPT_ROUTER_ALERT:
987 /* XXX may need check for alignment */
988 if (hbhlen < IP6OPT_RTALERT_LEN) {
989 V_ip6stat.ip6s_toosmall++;
992 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
994 icmp6_error(m, ICMP6_PARAM_PROB,
995 ICMP6_PARAMPROB_HEADER,
996 erroff + opt + 1 - opthead);
999 optlen = IP6OPT_RTALERT_LEN;
1000 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1001 *rtalertp = ntohs(rtalert_val);
1004 /* XXX may need check for alignment */
1005 if (hbhlen < IP6OPT_JUMBO_LEN) {
1006 V_ip6stat.ip6s_toosmall++;
1009 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1011 icmp6_error(m, ICMP6_PARAM_PROB,
1012 ICMP6_PARAMPROB_HEADER,
1013 erroff + opt + 1 - opthead);
1016 optlen = IP6OPT_JUMBO_LEN;
1019 * IPv6 packets that have non 0 payload length
1020 * must not contain a jumbo payload option.
1022 ip6 = mtod(m, struct ip6_hdr *);
1023 if (ip6->ip6_plen) {
1024 V_ip6stat.ip6s_badoptions++;
1025 icmp6_error(m, ICMP6_PARAM_PROB,
1026 ICMP6_PARAMPROB_HEADER,
1027 erroff + opt - opthead);
1032 * We may see jumbolen in unaligned location, so
1033 * we'd need to perform bcopy().
1035 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1036 jumboplen = (u_int32_t)htonl(jumboplen);
1040 * if there are multiple jumbo payload options,
1041 * *plenp will be non-zero and the packet will be
1043 * the behavior may need some debate in ipngwg -
1044 * multiple options does not make sense, however,
1045 * there's no explicit mention in specification.
1048 V_ip6stat.ip6s_badoptions++;
1049 icmp6_error(m, ICMP6_PARAM_PROB,
1050 ICMP6_PARAMPROB_HEADER,
1051 erroff + opt + 2 - opthead);
1057 * jumbo payload length must be larger than 65535.
1059 if (jumboplen <= IPV6_MAXPACKET) {
1060 V_ip6stat.ip6s_badoptions++;
1061 icmp6_error(m, ICMP6_PARAM_PROB,
1062 ICMP6_PARAMPROB_HEADER,
1063 erroff + opt + 2 - opthead);
1069 default: /* unknown option */
1070 if (hbhlen < IP6OPT_MINLEN) {
1071 V_ip6stat.ip6s_toosmall++;
1074 optlen = ip6_unknown_opt(opt, m,
1075 erroff + opt - opthead);
1091 * Unknown option processing.
1092 * The third argument `off' is the offset from the IPv6 header to the option,
1093 * which is necessary if the IPv6 header the and option header and IPv6 header
1094 * is not continuous in order to return an ICMPv6 error.
1097 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1099 INIT_VNET_INET6(curvnet);
1100 struct ip6_hdr *ip6;
1102 switch (IP6OPT_TYPE(*optp)) {
1103 case IP6OPT_TYPE_SKIP: /* ignore the option */
1104 return ((int)*(optp + 1));
1105 case IP6OPT_TYPE_DISCARD: /* silently discard */
1108 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1109 V_ip6stat.ip6s_badoptions++;
1110 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1112 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1113 V_ip6stat.ip6s_badoptions++;
1114 ip6 = mtod(m, struct ip6_hdr *);
1115 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1116 (m->m_flags & (M_BCAST|M_MCAST)))
1119 icmp6_error(m, ICMP6_PARAM_PROB,
1120 ICMP6_PARAMPROB_OPTION, off);
1124 m_freem(m); /* XXX: NOTREACHED */
1129 * Create the "control" list for this pcb.
1130 * These functions will not modify mbuf chain at all.
1132 * With KAME mbuf chain restriction:
1133 * The routine will be called from upper layer handlers like tcp6_input().
1134 * Thus the routine assumes that the caller (tcp6_input) have already
1135 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1136 * very first mbuf on the mbuf chain.
1138 * ip6_savecontrol_v4 will handle those options that are possible to be
1139 * set on a v4-mapped socket.
1140 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1141 * options and handle the v6-only ones itself.
1144 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1147 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1150 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1154 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1155 SCM_TIMESTAMP, SOL_SOCKET);
1157 mp = &(*mp)->m_next;
1161 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1167 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1168 /* RFC 2292 sec. 5 */
1169 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1170 struct in6_pktinfo pi6;
1172 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1173 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1175 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1177 *mp = sbcreatecontrol((caddr_t) &pi6,
1178 sizeof(struct in6_pktinfo),
1179 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1181 mp = &(*mp)->m_next;
1184 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1185 int hlim = ip6->ip6_hlim & 0xff;
1187 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1188 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1191 mp = &(*mp)->m_next;
1200 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1202 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1205 mp = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1209 if ((in6p->inp_flags & IN6P_TCLASS) != 0) {
1213 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1216 tclass = flowinfo & 0xff;
1217 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1218 IPV6_TCLASS, IPPROTO_IPV6);
1220 mp = &(*mp)->m_next;
1224 * IPV6_HOPOPTS socket option. Recall that we required super-user
1225 * privilege for the option (see ip6_ctloutput), but it might be too
1226 * strict, since there might be some hop-by-hop options which can be
1227 * returned to normal user.
1228 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1230 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1232 * Check if a hop-by-hop options header is contatined in the
1233 * received packet, and if so, store the options as ancillary
1234 * data. Note that a hop-by-hop options header must be
1235 * just after the IPv6 header, which is assured through the
1236 * IPv6 input processing.
1238 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1239 struct ip6_hbh *hbh;
1241 #ifdef PULLDOWN_TEST
1245 #ifndef PULLDOWN_TEST
1246 hbh = (struct ip6_hbh *)(ip6 + 1);
1247 hbhlen = (hbh->ip6h_len + 1) << 3;
1249 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1252 V_ip6stat.ip6s_tooshort++;
1255 hbh = mtod(ext, struct ip6_hbh *);
1256 hbhlen = (hbh->ip6h_len + 1) << 3;
1257 if (hbhlen != ext->m_len) {
1259 V_ip6stat.ip6s_tooshort++;
1265 * XXX: We copy the whole header even if a
1266 * jumbo payload option is included, the option which
1267 * is to be removed before returning according to
1269 * Note: this constraint is removed in RFC3542
1271 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1272 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1275 mp = &(*mp)->m_next;
1276 #ifdef PULLDOWN_TEST
1282 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1283 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1286 * Search for destination options headers or routing
1287 * header(s) through the header chain, and stores each
1288 * header as ancillary data.
1289 * Note that the order of the headers remains in
1290 * the chain of ancillary data.
1292 while (1) { /* is explicit loop prevention necessary? */
1293 struct ip6_ext *ip6e = NULL;
1295 #ifdef PULLDOWN_TEST
1296 struct mbuf *ext = NULL;
1300 * if it is not an extension header, don't try to
1301 * pull it from the chain.
1304 case IPPROTO_DSTOPTS:
1305 case IPPROTO_ROUTING:
1306 case IPPROTO_HOPOPTS:
1307 case IPPROTO_AH: /* is it possible? */
1313 #ifndef PULLDOWN_TEST
1314 if (off + sizeof(*ip6e) > m->m_len)
1316 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1317 if (nxt == IPPROTO_AH)
1318 elen = (ip6e->ip6e_len + 2) << 2;
1320 elen = (ip6e->ip6e_len + 1) << 3;
1321 if (off + elen > m->m_len)
1324 ext = ip6_pullexthdr(m, off, nxt);
1326 V_ip6stat.ip6s_tooshort++;
1329 ip6e = mtod(ext, struct ip6_ext *);
1330 if (nxt == IPPROTO_AH)
1331 elen = (ip6e->ip6e_len + 2) << 2;
1333 elen = (ip6e->ip6e_len + 1) << 3;
1334 if (elen != ext->m_len) {
1336 V_ip6stat.ip6s_tooshort++;
1342 case IPPROTO_DSTOPTS:
1343 if (!(in6p->inp_flags & IN6P_DSTOPTS))
1346 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1348 IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1351 mp = &(*mp)->m_next;
1353 case IPPROTO_ROUTING:
1354 if (!in6p->inp_flags & IN6P_RTHDR)
1357 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1358 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
1361 mp = &(*mp)->m_next;
1363 case IPPROTO_HOPOPTS:
1364 case IPPROTO_AH: /* is it possible? */
1369 * other cases have been filtered in the above.
1370 * none will visit this case. here we supply
1371 * the code just in case (nxt overwritten or
1374 #ifdef PULLDOWN_TEST
1381 /* proceed with the next header. */
1383 nxt = ip6e->ip6e_nxt;
1385 #ifdef PULLDOWN_TEST
1397 ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu)
1401 struct ip6_mtuinfo mtuctl;
1403 so = in6p->inp_socket;
1409 if (so == NULL) /* I believe this is impossible */
1410 panic("ip6_notify_pmtu: socket is NULL");
1413 bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */
1414 mtuctl.ip6m_mtu = *mtu;
1415 mtuctl.ip6m_addr = *dst;
1416 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1419 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1420 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1423 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1426 /* XXX: should count statistics */
1433 #ifdef PULLDOWN_TEST
1435 * pull single extension header from mbuf chain. returns single mbuf that
1436 * contains the result, or NULL on error.
1438 static struct mbuf *
1439 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1441 struct ip6_ext ip6e;
1447 case IPPROTO_DSTOPTS:
1448 case IPPROTO_ROUTING:
1449 case IPPROTO_HOPOPTS:
1450 case IPPROTO_AH: /* is it possible? */
1453 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1457 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1458 if (nxt == IPPROTO_AH)
1459 elen = (ip6e.ip6e_len + 2) << 2;
1461 elen = (ip6e.ip6e_len + 1) << 3;
1463 MGET(n, M_DONTWAIT, MT_DATA);
1464 if (n && elen >= MLEN) {
1465 MCLGET(n, M_DONTWAIT);
1466 if ((n->m_flags & M_EXT) == 0) {
1475 if (elen >= M_TRAILINGSPACE(n)) {
1480 m_copydata(m, off, elen, mtod(n, caddr_t));
1487 * Get pointer to the previous header followed by the header
1488 * currently processed.
1489 * XXX: This function supposes that
1490 * M includes all headers,
1491 * the next header field and the header length field of each header
1493 * the sum of each header length equals to OFF.
1494 * Because of these assumptions, this function must be called very
1495 * carefully. Moreover, it will not be used in the near future when
1496 * we develop `neater' mechanism to process extension headers.
1499 ip6_get_prevhdr(struct mbuf *m, int off)
1501 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1503 if (off == sizeof(struct ip6_hdr))
1504 return (&ip6->ip6_nxt);
1507 struct ip6_ext *ip6e = NULL;
1510 len = sizeof(struct ip6_hdr);
1512 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1515 case IPPROTO_FRAGMENT:
1516 len += sizeof(struct ip6_frag);
1519 len += (ip6e->ip6e_len + 2) << 2;
1522 len += (ip6e->ip6e_len + 1) << 3;
1525 nxt = ip6e->ip6e_nxt;
1528 return (&ip6e->ip6e_nxt);
1535 * get next header offset. m will be retained.
1538 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1541 struct ip6_ext ip6e;
1546 panic("ip6_nexthdr: m == NULL");
1547 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1552 if (m->m_pkthdr.len < off + sizeof(ip6))
1554 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1556 *nxtp = ip6.ip6_nxt;
1560 case IPPROTO_FRAGMENT:
1562 * terminate parsing if it is not the first fragment,
1563 * it does not make sense to parse through it.
1565 if (m->m_pkthdr.len < off + sizeof(fh))
1567 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1568 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1569 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1572 *nxtp = fh.ip6f_nxt;
1573 off += sizeof(struct ip6_frag);
1577 if (m->m_pkthdr.len < off + sizeof(ip6e))
1579 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1581 *nxtp = ip6e.ip6e_nxt;
1582 off += (ip6e.ip6e_len + 2) << 2;
1585 case IPPROTO_HOPOPTS:
1586 case IPPROTO_ROUTING:
1587 case IPPROTO_DSTOPTS:
1588 if (m->m_pkthdr.len < off + sizeof(ip6e))
1590 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1592 *nxtp = ip6e.ip6e_nxt;
1593 off += (ip6e.ip6e_len + 1) << 3;
1598 case IPPROTO_IPCOMP:
1610 * get offset for the last header in the chain. m will be kept untainted.
1613 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1623 newoff = ip6_nexthdr(m, off, proto, nxtp);
1626 else if (newoff < off)
1627 return -1; /* invalid */
1628 else if (newoff == off)
1637 ip6_addaux(struct mbuf *m)
1641 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1643 mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1646 m_tag_prepend(m, mtag);
1647 bzero(mtag + 1, sizeof(struct ip6aux));
1650 return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1654 ip6_findaux(struct mbuf *m)
1658 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1659 return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1663 ip6_delaux(struct mbuf *m)
1667 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1669 m_tag_delete(m, mtag);
1673 * System control for IP6
1676 u_char inet6ctlerrmap[PRC_NCMDS] = {
1678 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1679 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1680 EMSGSIZE, EHOSTUNREACH, 0, 0,