ip6_input.c revision 262256
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 *	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
30 */
31
32/*-
33 * Copyright (c) 1982, 1986, 1988, 1993
34 *	The Regents of the University of California.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
61 */
62
63#include <sys/cdefs.h>
64__FBSDID("$FreeBSD: stable/10/sys/netinet6/ip6_input.c 262256 2014-02-20 21:01:59Z ae $");
65
66#include "opt_inet.h"
67#include "opt_inet6.h"
68#include "opt_ipfw.h"
69#include "opt_ipsec.h"
70#include "opt_kdtrace.h"
71#include "opt_route.h"
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/malloc.h>
76#include <sys/mbuf.h>
77#include <sys/proc.h>
78#include <sys/domain.h>
79#include <sys/protosw.h>
80#include <sys/sdt.h>
81#include <sys/socket.h>
82#include <sys/socketvar.h>
83#include <sys/errno.h>
84#include <sys/time.h>
85#include <sys/kernel.h>
86#include <sys/syslog.h>
87
88#include <net/if.h>
89#include <net/if_types.h>
90#include <net/if_dl.h>
91#include <net/route.h>
92#include <net/netisr.h>
93#include <net/pfil.h>
94#include <net/vnet.h>
95
96#include <netinet/in.h>
97#include <netinet/in_kdtrace.h>
98#include <netinet/ip_var.h>
99#include <netinet/in_systm.h>
100#include <net/if_llatbl.h>
101#ifdef INET
102#include <netinet/ip.h>
103#include <netinet/ip_icmp.h>
104#endif /* INET */
105#include <netinet/ip6.h>
106#include <netinet6/in6_var.h>
107#include <netinet6/ip6_var.h>
108#include <netinet/in_pcb.h>
109#include <netinet/icmp6.h>
110#include <netinet6/scope6_var.h>
111#include <netinet6/in6_ifattach.h>
112#include <netinet6/nd6.h>
113
114#ifdef IPSEC
115#include <netipsec/ipsec.h>
116#include <netinet6/ip6_ipsec.h>
117#include <netipsec/ipsec6.h>
118#endif /* IPSEC */
119
120#include <netinet6/ip6protosw.h>
121
122#ifdef FLOWTABLE
123#include <net/flowtable.h>
124VNET_DECLARE(int, ip6_output_flowtable_size);
125#define	V_ip6_output_flowtable_size	VNET(ip6_output_flowtable_size)
126#endif
127
128extern struct domain inet6domain;
129
130u_char ip6_protox[IPPROTO_MAX];
131VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
132VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
133VNET_DEFINE(u_long, in6_ifaddrhmask);
134
135static struct netisr_handler ip6_nh = {
136	.nh_name = "ip6",
137	.nh_handler = ip6_input,
138	.nh_proto = NETISR_IPV6,
139	.nh_policy = NETISR_POLICY_FLOW,
140};
141
142VNET_DECLARE(struct callout, in6_tmpaddrtimer_ch);
143#define	V_in6_tmpaddrtimer_ch		VNET(in6_tmpaddrtimer_ch)
144
145VNET_DEFINE(struct pfil_head, inet6_pfil_hook);
146
147VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
148VNET_PCPUSTAT_SYSINIT(ip6stat);
149#ifdef VIMAGE
150VNET_PCPUSTAT_SYSUNINIT(ip6stat);
151#endif /* VIMAGE */
152
153struct rwlock in6_ifaddr_lock;
154RW_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
155
156static void ip6_init2(void *);
157static struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *);
158static struct ip6aux *ip6_addaux(struct mbuf *);
159static struct ip6aux *ip6_findaux(struct mbuf *m);
160static void ip6_delaux (struct mbuf *);
161static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
162#ifdef PULLDOWN_TEST
163static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
164#endif
165
166/*
167 * IP6 initialization: fill in IP6 protocol switch table.
168 * All protocols not implemented in kernel go to raw IP6 protocol handler.
169 */
170void
171ip6_init(void)
172{
173	struct ip6protosw *pr;
174	int i;
175
176	TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
177	    &V_ip6_auto_linklocal);
178	TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
179	TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
180
181	TAILQ_INIT(&V_in6_ifaddrhead);
182	V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
183	    &V_in6_ifaddrhmask);
184
185	/* Initialize packet filter hooks. */
186	V_inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
187	V_inet6_pfil_hook.ph_af = AF_INET6;
188	if ((i = pfil_head_register(&V_inet6_pfil_hook)) != 0)
189		printf("%s: WARNING: unable to register pfil hook, "
190			"error %d\n", __func__, i);
191
192	scope6_init();
193	addrsel_policy_init();
194	nd6_init();
195	frag6_init();
196
197#ifdef FLOWTABLE
198	if (TUNABLE_INT_FETCH("net.inet6.ip6.output_flowtable_size",
199		&V_ip6_output_flowtable_size)) {
200		if (V_ip6_output_flowtable_size < 256)
201			V_ip6_output_flowtable_size = 256;
202		if (!powerof2(V_ip6_output_flowtable_size)) {
203			printf("flowtable must be power of 2 size\n");
204			V_ip6_output_flowtable_size = 2048;
205		}
206	} else {
207		/*
208		 * round up to the next power of 2
209		 */
210		V_ip6_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1);
211	}
212	V_ip6_ft = flowtable_alloc("ipv6", V_ip6_output_flowtable_size, FL_IPV6|FL_PCPU);
213#endif
214
215	V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
216
217	/* Skip global initialization stuff for non-default instances. */
218	if (!IS_DEFAULT_VNET(curvnet))
219		return;
220
221#ifdef DIAGNOSTIC
222	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
223		panic("sizeof(protosw) != sizeof(ip6protosw)");
224#endif
225	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
226	if (pr == NULL)
227		panic("ip6_init");
228
229	/* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
230	for (i = 0; i < IPPROTO_MAX; i++)
231		ip6_protox[i] = pr - inet6sw;
232	/*
233	 * Cycle through IP protocols and put them into the appropriate place
234	 * in ip6_protox[].
235	 */
236	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
237	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
238		if (pr->pr_domain->dom_family == PF_INET6 &&
239		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
240			/* Be careful to only index valid IP protocols. */
241			if (pr->pr_protocol < IPPROTO_MAX)
242				ip6_protox[pr->pr_protocol] = pr - inet6sw;
243		}
244
245	netisr_register(&ip6_nh);
246}
247
248/*
249 * The protocol to be inserted into ip6_protox[] must be already registered
250 * in inet6sw[], either statically or through pf_proto_register().
251 */
252int
253ip6proto_register(short ip6proto)
254{
255	struct ip6protosw *pr;
256
257	/* Sanity checks. */
258	if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
259		return (EPROTONOSUPPORT);
260
261	/*
262	 * The protocol slot must not be occupied by another protocol
263	 * already.  An index pointing to IPPROTO_RAW is unused.
264	 */
265	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
266	if (pr == NULL)
267		return (EPFNOSUPPORT);
268	if (ip6_protox[ip6proto] != pr - inet6sw)	/* IPPROTO_RAW */
269		return (EEXIST);
270
271	/*
272	 * Find the protocol position in inet6sw[] and set the index.
273	 */
274	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
275	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) {
276		if (pr->pr_domain->dom_family == PF_INET6 &&
277		    pr->pr_protocol && pr->pr_protocol == ip6proto) {
278			ip6_protox[pr->pr_protocol] = pr - inet6sw;
279			return (0);
280		}
281	}
282	return (EPROTONOSUPPORT);
283}
284
285int
286ip6proto_unregister(short ip6proto)
287{
288	struct ip6protosw *pr;
289
290	/* Sanity checks. */
291	if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
292		return (EPROTONOSUPPORT);
293
294	/* Check if the protocol was indeed registered. */
295	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
296	if (pr == NULL)
297		return (EPFNOSUPPORT);
298	if (ip6_protox[ip6proto] == pr - inet6sw)	/* IPPROTO_RAW */
299		return (ENOENT);
300
301	/* Reset the protocol slot to IPPROTO_RAW. */
302	ip6_protox[ip6proto] = pr - inet6sw;
303	return (0);
304}
305
306#ifdef VIMAGE
307void
308ip6_destroy()
309{
310	int i;
311
312	if ((i = pfil_head_unregister(&V_inet6_pfil_hook)) != 0)
313		printf("%s: WARNING: unable to unregister pfil hook, "
314		    "error %d\n", __func__, i);
315	hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
316	nd6_destroy();
317	callout_drain(&V_in6_tmpaddrtimer_ch);
318}
319#endif
320
321static int
322ip6_init2_vnet(const void *unused __unused)
323{
324
325	/* nd6_timer_init */
326	callout_init(&V_nd6_timer_ch, 0);
327	callout_reset(&V_nd6_timer_ch, hz, nd6_timer, curvnet);
328
329	/* timer for regeneranation of temporary addresses randomize ID */
330	callout_init(&V_in6_tmpaddrtimer_ch, 0);
331	callout_reset(&V_in6_tmpaddrtimer_ch,
332		      (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
333		       V_ip6_temp_regen_advance) * hz,
334		      in6_tmpaddrtimer, curvnet);
335
336	return (0);
337}
338
339static void
340ip6_init2(void *dummy)
341{
342
343	ip6_init2_vnet(NULL);
344}
345
346/* cheat */
347/* This must be after route_init(), which is now SI_ORDER_THIRD */
348SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
349
350static int
351ip6_input_hbh(struct mbuf *m, uint32_t *plen, uint32_t *rtalert, int *off,
352    int *nxt, int *ours)
353{
354	struct ip6_hdr *ip6;
355	struct ip6_hbh *hbh;
356
357	if (ip6_hopopts_input(plen, rtalert, &m, off)) {
358#if 0	/*touches NULL pointer*/
359		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
360#endif
361		goto out;	/* m have already been freed */
362	}
363
364	/* adjust pointer */
365	ip6 = mtod(m, struct ip6_hdr *);
366
367	/*
368	 * if the payload length field is 0 and the next header field
369	 * indicates Hop-by-Hop Options header, then a Jumbo Payload
370	 * option MUST be included.
371	 */
372	if (ip6->ip6_plen == 0 && *plen == 0) {
373		/*
374		 * Note that if a valid jumbo payload option is
375		 * contained, ip6_hopopts_input() must set a valid
376		 * (non-zero) payload length to the variable plen.
377		 */
378		IP6STAT_INC(ip6s_badoptions);
379		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
380		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
381		icmp6_error(m, ICMP6_PARAM_PROB,
382			    ICMP6_PARAMPROB_HEADER,
383			    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
384		goto out;
385	}
386#ifndef PULLDOWN_TEST
387	/* ip6_hopopts_input() ensures that mbuf is contiguous */
388	hbh = (struct ip6_hbh *)(ip6 + 1);
389#else
390	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
391		sizeof(struct ip6_hbh));
392	if (hbh == NULL) {
393		IP6STAT_INC(ip6s_tooshort);
394		goto out;
395	}
396#endif
397	*nxt = hbh->ip6h_nxt;
398
399	/*
400	 * If we are acting as a router and the packet contains a
401	 * router alert option, see if we know the option value.
402	 * Currently, we only support the option value for MLD, in which
403	 * case we should pass the packet to the multicast routing
404	 * daemon.
405	 */
406	if (*rtalert != ~0) {
407		switch (*rtalert) {
408		case IP6OPT_RTALERT_MLD:
409			if (V_ip6_forwarding)
410				*ours = 1;
411			break;
412		default:
413			/*
414			 * RFC2711 requires unrecognized values must be
415			 * silently ignored.
416			 */
417			break;
418		}
419	}
420
421	return (0);
422
423out:
424	return (1);
425}
426
427void
428ip6_input(struct mbuf *m)
429{
430	struct ip6_hdr *ip6;
431	int off = sizeof(struct ip6_hdr), nest;
432	u_int32_t plen;
433	u_int32_t rtalert = ~0;
434	int nxt, ours = 0;
435	struct ifnet *deliverifp = NULL, *ifp = NULL;
436	struct in6_addr odst;
437	struct route_in6 rin6;
438	int srcrt = 0;
439	struct llentry *lle = NULL;
440	struct sockaddr_in6 dst6, *dst;
441
442	bzero(&rin6, sizeof(struct route_in6));
443#ifdef IPSEC
444	/*
445	 * should the inner packet be considered authentic?
446	 * see comment in ah4_input().
447	 * NB: m cannot be NULL when passed to the input routine
448	 */
449
450	m->m_flags &= ~M_AUTHIPHDR;
451	m->m_flags &= ~M_AUTHIPDGM;
452
453#endif /* IPSEC */
454
455	/*
456	 * make sure we don't have onion peering information into m_tag.
457	 */
458	ip6_delaux(m);
459
460	if (m->m_flags & M_FASTFWD_OURS) {
461		/*
462		 * Firewall changed destination to local.
463		 */
464		m->m_flags &= ~M_FASTFWD_OURS;
465		ours = 1;
466		deliverifp = m->m_pkthdr.rcvif;
467		ip6 = mtod(m, struct ip6_hdr *);
468		goto hbhcheck;
469	}
470
471	/*
472	 * mbuf statistics
473	 */
474	if (m->m_flags & M_EXT) {
475		if (m->m_next)
476			IP6STAT_INC(ip6s_mext2m);
477		else
478			IP6STAT_INC(ip6s_mext1);
479	} else {
480		if (m->m_next) {
481			if (m->m_flags & M_LOOP) {
482				IP6STAT_INC(ip6s_m2m[V_loif->if_index]);
483			} else if (m->m_pkthdr.rcvif->if_index < IP6S_M2MMAX)
484				IP6STAT_INC(
485				    ip6s_m2m[m->m_pkthdr.rcvif->if_index]);
486			else
487				IP6STAT_INC(ip6s_m2m[0]);
488		} else
489			IP6STAT_INC(ip6s_m1);
490	}
491
492	/* drop the packet if IPv6 operation is disabled on the IF */
493	if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
494		m_freem(m);
495		return;
496	}
497
498	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
499	IP6STAT_INC(ip6s_total);
500
501#ifndef PULLDOWN_TEST
502	/*
503	 * L2 bridge code and some other code can return mbuf chain
504	 * that does not conform to KAME requirement.  too bad.
505	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
506	 */
507	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
508		struct mbuf *n;
509
510		if (m->m_pkthdr.len > MHLEN)
511			n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
512		else
513			n = m_gethdr(M_NOWAIT, MT_DATA);
514		if (n == NULL) {
515			m_freem(m);
516			return;	/* ENOBUFS */
517		}
518
519		m_move_pkthdr(n, m);
520		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
521		n->m_len = n->m_pkthdr.len;
522		m_freem(m);
523		m = n;
524	}
525	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
526#endif
527
528	if (m->m_len < sizeof(struct ip6_hdr)) {
529		struct ifnet *inifp;
530		inifp = m->m_pkthdr.rcvif;
531		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
532			IP6STAT_INC(ip6s_toosmall);
533			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
534			return;
535		}
536	}
537
538	ip6 = mtod(m, struct ip6_hdr *);
539
540	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
541		IP6STAT_INC(ip6s_badvers);
542		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
543		goto bad;
544	}
545
546	IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
547
548	IP_PROBE(receive, NULL, NULL, ip6, m->m_pkthdr.rcvif, NULL, ip6);
549
550	/*
551	 * Check against address spoofing/corruption.
552	 */
553	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
554	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
555		/*
556		 * XXX: "badscope" is not very suitable for a multicast source.
557		 */
558		IP6STAT_INC(ip6s_badscope);
559		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
560		goto bad;
561	}
562	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
563	    !(m->m_flags & M_LOOP)) {
564		/*
565		 * In this case, the packet should come from the loopback
566		 * interface.  However, we cannot just check the if_flags,
567		 * because ip6_mloopback() passes the "actual" interface
568		 * as the outgoing/incoming interface.
569		 */
570		IP6STAT_INC(ip6s_badscope);
571		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
572		goto bad;
573	}
574	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
575	    IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
576		/*
577		 * RFC4291 2.7:
578		 * Nodes must not originate a packet to a multicast address
579		 * whose scop field contains the reserved value 0; if such
580		 * a packet is received, it must be silently dropped.
581		 */
582		IP6STAT_INC(ip6s_badscope);
583		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
584		goto bad;
585	}
586#ifdef ALTQ
587	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
588		/* packet is dropped by traffic conditioner */
589		return;
590	}
591#endif
592	/*
593	 * The following check is not documented in specs.  A malicious
594	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
595	 * and bypass security checks (act as if it was from 127.0.0.1 by using
596	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
597	 *
598	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
599	 * support IPv4-less kernel compilation, we cannot support SIIT
600	 * environment at all.  So, it makes more sense for us to reject any
601	 * malicious packets for non-SIIT environment, than try to do a
602	 * partial support for SIIT environment.
603	 */
604	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
605	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
606		IP6STAT_INC(ip6s_badscope);
607		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
608		goto bad;
609	}
610#if 0
611	/*
612	 * Reject packets with IPv4 compatible addresses (auto tunnel).
613	 *
614	 * The code forbids auto tunnel relay case in RFC1933 (the check is
615	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
616	 * is revised to forbid relaying case.
617	 */
618	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
619	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
620		IP6STAT_INC(ip6s_badscope);
621		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
622		goto bad;
623	}
624#endif
625#ifdef IPSEC
626	/*
627	 * Bypass packet filtering for packets previously handled by IPsec.
628	 */
629	if (ip6_ipsec_filtertunnel(m))
630		goto passin;
631#endif /* IPSEC */
632
633	/*
634	 * Run through list of hooks for input packets.
635	 *
636	 * NB: Beware of the destination address changing
637	 *     (e.g. by NAT rewriting).  When this happens,
638	 *     tell ip6_forward to do the right thing.
639	 */
640	odst = ip6->ip6_dst;
641
642	/* Jump over all PFIL processing if hooks are not active. */
643	if (!PFIL_HOOKED(&V_inet6_pfil_hook))
644		goto passin;
645
646	if (pfil_run_hooks(&V_inet6_pfil_hook, &m,
647	    m->m_pkthdr.rcvif, PFIL_IN, NULL))
648		return;
649	if (m == NULL)			/* consumed by filter */
650		return;
651	ip6 = mtod(m, struct ip6_hdr *);
652	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
653
654	if (m->m_flags & M_FASTFWD_OURS) {
655		m->m_flags &= ~M_FASTFWD_OURS;
656		ours = 1;
657		deliverifp = m->m_pkthdr.rcvif;
658		goto hbhcheck;
659	}
660	if ((m->m_flags & M_IP6_NEXTHOP) &&
661	    m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
662		/*
663		 * Directly ship the packet on.  This allows forwarding
664		 * packets originally destined to us to some other directly
665		 * connected host.
666		 */
667		ip6_forward(m, 1);
668		goto out;
669	}
670
671passin:
672	/*
673	 * Disambiguate address scope zones (if there is ambiguity).
674	 * We first make sure that the original source or destination address
675	 * is not in our internal form for scoped addresses.  Such addresses
676	 * are not necessarily invalid spec-wise, but we cannot accept them due
677	 * to the usage conflict.
678	 * in6_setscope() then also checks and rejects the cases where src or
679	 * dst are the loopback address and the receiving interface
680	 * is not loopback.
681	 */
682	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
683		IP6STAT_INC(ip6s_badscope); /* XXX */
684		goto bad;
685	}
686	if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
687	    in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
688		IP6STAT_INC(ip6s_badscope);
689		goto bad;
690	}
691
692	/*
693	 * Multicast check. Assume packet is for us to avoid
694	 * prematurely taking locks.
695	 */
696	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
697		ours = 1;
698		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
699		deliverifp = m->m_pkthdr.rcvif;
700		goto hbhcheck;
701	}
702
703	/*
704	 *  Unicast check
705	 */
706
707	bzero(&dst6, sizeof(dst6));
708	dst6.sin6_family = AF_INET6;
709	dst6.sin6_len = sizeof(struct sockaddr_in6);
710	dst6.sin6_addr = ip6->ip6_dst;
711	ifp = m->m_pkthdr.rcvif;
712	IF_AFDATA_RLOCK(ifp);
713	lle = lla_lookup(LLTABLE6(ifp), 0,
714	     (struct sockaddr *)&dst6);
715	IF_AFDATA_RUNLOCK(ifp);
716	if ((lle != NULL) && (lle->la_flags & LLE_IFADDR)) {
717		struct ifaddr *ifa;
718		struct in6_ifaddr *ia6;
719		int bad;
720
721		bad = 1;
722#define	sa_equal(a1, a2)						\
723	(bcmp((a1), (a2), ((a1))->sin6_len) == 0)
724		IF_ADDR_RLOCK(ifp);
725		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
726			if (ifa->ifa_addr->sa_family != dst6.sin6_family)
727				continue;
728			if (sa_equal(&dst6, ifa->ifa_addr))
729				break;
730		}
731		KASSERT(ifa != NULL, ("%s: ifa not found for lle %p",
732		    __func__, lle));
733#undef sa_equal
734
735		ia6 = (struct in6_ifaddr *)ifa;
736		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
737			/* Count the packet in the ip address stats */
738			ia6->ia_ifa.if_ipackets++;
739			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
740
741			/*
742			 * record address information into m_tag.
743			 */
744			(void)ip6_setdstifaddr(m, ia6);
745
746			bad = 0;
747		} else {
748			char ip6bufs[INET6_ADDRSTRLEN];
749			char ip6bufd[INET6_ADDRSTRLEN];
750			/* address is not ready, so discard the packet. */
751			nd6log((LOG_INFO,
752			    "ip6_input: packet to an unready address %s->%s\n",
753			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
754			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
755		}
756		IF_ADDR_RUNLOCK(ifp);
757		LLE_RUNLOCK(lle);
758		if (bad)
759			goto bad;
760		else {
761			ours = 1;
762			deliverifp = ifp;
763			goto hbhcheck;
764		}
765	}
766	if (lle != NULL)
767		LLE_RUNLOCK(lle);
768
769	dst = &rin6.ro_dst;
770	dst->sin6_len = sizeof(struct sockaddr_in6);
771	dst->sin6_family = AF_INET6;
772	dst->sin6_addr = ip6->ip6_dst;
773	rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m));
774	if (rin6.ro_rt)
775		RT_UNLOCK(rin6.ro_rt);
776
777#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
778
779	/*
780	 * Accept the packet if the forwarding interface to the destination
781	 * according to the routing table is the loopback interface,
782	 * unless the associated route has a gateway.
783	 * Note that this approach causes to accept a packet if there is a
784	 * route to the loopback interface for the destination of the packet.
785	 * But we think it's even useful in some situations, e.g. when using
786	 * a special daemon which wants to intercept the packet.
787	 *
788	 * XXX: some OSes automatically make a cloned route for the destination
789	 * of an outgoing packet.  If the outgoing interface of the packet
790	 * is a loopback one, the kernel would consider the packet to be
791	 * accepted, even if we have no such address assinged on the interface.
792	 * We check the cloned flag of the route entry to reject such cases,
793	 * assuming that route entries for our own addresses are not made by
794	 * cloning (it should be true because in6_addloop explicitly installs
795	 * the host route).  However, we might have to do an explicit check
796	 * while it would be less efficient.  Or, should we rather install a
797	 * reject route for such a case?
798	 */
799	if (rin6.ro_rt &&
800	    (rin6.ro_rt->rt_flags &
801	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
802#ifdef RTF_WASCLONED
803	    !(rin6.ro_rt->rt_flags & RTF_WASCLONED) &&
804#endif
805#ifdef RTF_CLONED
806	    !(rin6.ro_rt->rt_flags & RTF_CLONED) &&
807#endif
808#if 0
809	    /*
810	     * The check below is redundant since the comparison of
811	     * the destination and the key of the rtentry has
812	     * already done through looking up the routing table.
813	     */
814	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
815	    &rt6_key(rin6.ro_rt)->sin6_addr)
816#endif
817	    rin6.ro_rt->rt_ifp->if_type == IFT_LOOP) {
818		int free_ia6 = 0;
819		struct in6_ifaddr *ia6;
820
821		/*
822		 * found the loopback route to the interface address
823		 */
824		if (rin6.ro_rt->rt_gateway->sa_family == AF_LINK) {
825			struct sockaddr_in6 dest6;
826
827			bzero(&dest6, sizeof(dest6));
828			dest6.sin6_family = AF_INET6;
829			dest6.sin6_len = sizeof(dest6);
830			dest6.sin6_addr = ip6->ip6_dst;
831			ia6 = (struct in6_ifaddr *)
832			    ifa_ifwithaddr((struct sockaddr *)&dest6);
833			if (ia6 == NULL)
834				goto bad;
835			free_ia6 = 1;
836		}
837		else
838			ia6 = (struct in6_ifaddr *)rin6.ro_rt->rt_ifa;
839
840		/*
841		 * record address information into m_tag.
842		 */
843		(void)ip6_setdstifaddr(m, ia6);
844
845		/*
846		 * packets to a tentative, duplicated, or somehow invalid
847		 * address must not be accepted.
848		 */
849		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
850			/* this address is ready */
851			ours = 1;
852			deliverifp = ia6->ia_ifp;	/* correct? */
853			/* Count the packet in the ip address stats */
854			ia6->ia_ifa.if_ipackets++;
855			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
856			if (ia6 != NULL && free_ia6 != 0)
857				ifa_free(&ia6->ia_ifa);
858			goto hbhcheck;
859		} else {
860			char ip6bufs[INET6_ADDRSTRLEN];
861			char ip6bufd[INET6_ADDRSTRLEN];
862			/* address is not ready, so discard the packet. */
863			nd6log((LOG_INFO,
864			    "ip6_input: packet to an unready address %s->%s\n",
865			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
866			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
867
868			if (ia6 != NULL && free_ia6 != 0)
869				ifa_free(&ia6->ia_ifa);
870			goto bad;
871		}
872	}
873
874	/*
875	 * FAITH (Firewall Aided Internet Translator)
876	 */
877	if (V_ip6_keepfaith) {
878		if (rin6.ro_rt && rin6.ro_rt->rt_ifp &&
879		    rin6.ro_rt->rt_ifp->if_type == IFT_FAITH) {
880			/* XXX do we need more sanity checks? */
881			ours = 1;
882			deliverifp = rin6.ro_rt->rt_ifp; /* faith */
883			goto hbhcheck;
884		}
885	}
886
887	/*
888	 * Now there is no reason to process the packet if it's not our own
889	 * and we're not a router.
890	 */
891	if (!V_ip6_forwarding) {
892		IP6STAT_INC(ip6s_cantforward);
893		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
894		goto bad;
895	}
896
897  hbhcheck:
898	/*
899	 * record address information into m_tag, if we don't have one yet.
900	 * note that we are unable to record it, if the address is not listed
901	 * as our interface address (e.g. multicast addresses, addresses
902	 * within FAITH prefixes and such).
903	 */
904	if (deliverifp) {
905		struct in6_ifaddr *ia6;
906
907 		if ((ia6 = ip6_getdstifaddr(m)) != NULL) {
908			ifa_free(&ia6->ia_ifa);
909		} else {
910			ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
911			if (ia6) {
912				if (!ip6_setdstifaddr(m, ia6)) {
913					/*
914					 * XXX maybe we should drop the packet here,
915					 * as we could not provide enough information
916					 * to the upper layers.
917					 */
918				}
919				ifa_free(&ia6->ia_ifa);
920			}
921		}
922	}
923
924	/*
925	 * Process Hop-by-Hop options header if it's contained.
926	 * m may be modified in ip6_hopopts_input().
927	 * If a JumboPayload option is included, plen will also be modified.
928	 */
929	plen = (u_int32_t)ntohs(ip6->ip6_plen);
930	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
931		int error;
932
933		error = ip6_input_hbh(m, &plen, &rtalert, &off, &nxt, &ours);
934		if (error != 0)
935			goto out;
936	} else
937		nxt = ip6->ip6_nxt;
938
939	/*
940	 * Check that the amount of data in the buffers
941	 * is as at least much as the IPv6 header would have us expect.
942	 * Trim mbufs if longer than we expect.
943	 * Drop packet if shorter than we expect.
944	 */
945	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
946		IP6STAT_INC(ip6s_tooshort);
947		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
948		goto bad;
949	}
950	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
951		if (m->m_len == m->m_pkthdr.len) {
952			m->m_len = sizeof(struct ip6_hdr) + plen;
953			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
954		} else
955			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
956	}
957
958	/*
959	 * Forward if desirable.
960	 */
961	if (V_ip6_mrouter &&
962	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
963		/*
964		 * If we are acting as a multicast router, all
965		 * incoming multicast packets are passed to the
966		 * kernel-level multicast forwarding function.
967		 * The packet is returned (relatively) intact; if
968		 * ip6_mforward() returns a non-zero value, the packet
969		 * must be discarded, else it may be accepted below.
970		 *
971		 * XXX TODO: Check hlim and multicast scope here to avoid
972		 * unnecessarily calling into ip6_mforward().
973		 */
974		if (ip6_mforward &&
975		    ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
976			IP6STAT_INC(ip6s_cantforward);
977			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
978			goto bad;
979		}
980	} else if (!ours) {
981		ip6_forward(m, srcrt);
982		goto out;
983	}
984
985	ip6 = mtod(m, struct ip6_hdr *);
986
987	/*
988	 * Malicious party may be able to use IPv4 mapped addr to confuse
989	 * tcp/udp stack and bypass security checks (act as if it was from
990	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
991	 *
992	 * For SIIT end node behavior, you may want to disable the check.
993	 * However, you will  become vulnerable to attacks using IPv4 mapped
994	 * source.
995	 */
996	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
997	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
998		IP6STAT_INC(ip6s_badscope);
999		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1000		goto bad;
1001	}
1002
1003	/*
1004	 * Tell launch routine the next header
1005	 */
1006	IP6STAT_INC(ip6s_delivered);
1007	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1008	nest = 0;
1009
1010	while (nxt != IPPROTO_DONE) {
1011		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
1012			IP6STAT_INC(ip6s_toomanyhdr);
1013			goto bad;
1014		}
1015
1016		/*
1017		 * protection against faulty packet - there should be
1018		 * more sanity checks in header chain processing.
1019		 */
1020		if (m->m_pkthdr.len < off) {
1021			IP6STAT_INC(ip6s_tooshort);
1022			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1023			goto bad;
1024		}
1025
1026#ifdef IPSEC
1027		/*
1028		 * enforce IPsec policy checking if we are seeing last header.
1029		 * note that we do not visit this with protocols with pcb layer
1030		 * code - like udp/tcp/raw ip.
1031		 */
1032		if (ip6_ipsec_input(m, nxt))
1033			goto bad;
1034#endif /* IPSEC */
1035
1036		/*
1037		 * Use mbuf flags to propagate Router Alert option to
1038		 * ICMPv6 layer, as hop-by-hop options have been stripped.
1039		 */
1040		if (nxt == IPPROTO_ICMPV6 && rtalert != ~0)
1041			m->m_flags |= M_RTALERT_MLD;
1042
1043		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
1044	}
1045	goto out;
1046bad:
1047	m_freem(m);
1048out:
1049	if (rin6.ro_rt)
1050		RTFREE(rin6.ro_rt);
1051}
1052
1053/*
1054 * set/grab in6_ifaddr correspond to IPv6 destination address.
1055 * XXX backward compatibility wrapper
1056 *
1057 * XXXRW: We should bump the refcount on ia6 before sticking it in the m_tag,
1058 * and then bump it when the tag is copied, and release it when the tag is
1059 * freed.  Unfortunately, m_tags don't support deep copies (yet), so instead
1060 * we just bump the ia refcount when we receive it.  This should be fixed.
1061 */
1062static struct ip6aux *
1063ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
1064{
1065	struct ip6aux *ip6a;
1066
1067	ip6a = ip6_addaux(m);
1068	if (ip6a)
1069		ip6a->ip6a_dstia6 = ia6;
1070	return ip6a;	/* NULL if failed to set */
1071}
1072
1073struct in6_ifaddr *
1074ip6_getdstifaddr(struct mbuf *m)
1075{
1076	struct ip6aux *ip6a;
1077	struct in6_ifaddr *ia;
1078
1079	ip6a = ip6_findaux(m);
1080	if (ip6a) {
1081		ia = ip6a->ip6a_dstia6;
1082		ifa_ref(&ia->ia_ifa);
1083		return ia;
1084	} else
1085		return NULL;
1086}
1087
1088/*
1089 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1090 * included, the real payload length will be stored in plenp.
1091 *
1092 * rtalertp - XXX: should be stored more smart way
1093 */
1094static int
1095ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
1096    struct mbuf **mp, int *offp)
1097{
1098	struct mbuf *m = *mp;
1099	int off = *offp, hbhlen;
1100	struct ip6_hbh *hbh;
1101	u_int8_t *opt;
1102
1103	/* validation of the length of the header */
1104#ifndef PULLDOWN_TEST
1105	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
1106	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1107	hbhlen = (hbh->ip6h_len + 1) << 3;
1108
1109	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
1110	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1111#else
1112	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1113		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
1114	if (hbh == NULL) {
1115		IP6STAT_INC(ip6s_tooshort);
1116		return -1;
1117	}
1118	hbhlen = (hbh->ip6h_len + 1) << 3;
1119	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
1120		hbhlen);
1121	if (hbh == NULL) {
1122		IP6STAT_INC(ip6s_tooshort);
1123		return -1;
1124	}
1125#endif
1126	off += hbhlen;
1127	hbhlen -= sizeof(struct ip6_hbh);
1128	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
1129
1130	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
1131				hbhlen, rtalertp, plenp) < 0)
1132		return (-1);
1133
1134	*offp = off;
1135	*mp = m;
1136	return (0);
1137}
1138
1139/*
1140 * Search header for all Hop-by-hop options and process each option.
1141 * This function is separate from ip6_hopopts_input() in order to
1142 * handle a case where the sending node itself process its hop-by-hop
1143 * options header. In such a case, the function is called from ip6_output().
1144 *
1145 * The function assumes that hbh header is located right after the IPv6 header
1146 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1147 * opthead + hbhlen is located in contiguous memory region.
1148 */
1149int
1150ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
1151    u_int32_t *rtalertp, u_int32_t *plenp)
1152{
1153	struct ip6_hdr *ip6;
1154	int optlen = 0;
1155	u_int8_t *opt = opthead;
1156	u_int16_t rtalert_val;
1157	u_int32_t jumboplen;
1158	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1159
1160	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1161		switch (*opt) {
1162		case IP6OPT_PAD1:
1163			optlen = 1;
1164			break;
1165		case IP6OPT_PADN:
1166			if (hbhlen < IP6OPT_MINLEN) {
1167				IP6STAT_INC(ip6s_toosmall);
1168				goto bad;
1169			}
1170			optlen = *(opt + 1) + 2;
1171			break;
1172		case IP6OPT_ROUTER_ALERT:
1173			/* XXX may need check for alignment */
1174			if (hbhlen < IP6OPT_RTALERT_LEN) {
1175				IP6STAT_INC(ip6s_toosmall);
1176				goto bad;
1177			}
1178			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1179				/* XXX stat */
1180				icmp6_error(m, ICMP6_PARAM_PROB,
1181				    ICMP6_PARAMPROB_HEADER,
1182				    erroff + opt + 1 - opthead);
1183				return (-1);
1184			}
1185			optlen = IP6OPT_RTALERT_LEN;
1186			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1187			*rtalertp = ntohs(rtalert_val);
1188			break;
1189		case IP6OPT_JUMBO:
1190			/* XXX may need check for alignment */
1191			if (hbhlen < IP6OPT_JUMBO_LEN) {
1192				IP6STAT_INC(ip6s_toosmall);
1193				goto bad;
1194			}
1195			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1196				/* XXX stat */
1197				icmp6_error(m, ICMP6_PARAM_PROB,
1198				    ICMP6_PARAMPROB_HEADER,
1199				    erroff + opt + 1 - opthead);
1200				return (-1);
1201			}
1202			optlen = IP6OPT_JUMBO_LEN;
1203
1204			/*
1205			 * IPv6 packets that have non 0 payload length
1206			 * must not contain a jumbo payload option.
1207			 */
1208			ip6 = mtod(m, struct ip6_hdr *);
1209			if (ip6->ip6_plen) {
1210				IP6STAT_INC(ip6s_badoptions);
1211				icmp6_error(m, ICMP6_PARAM_PROB,
1212				    ICMP6_PARAMPROB_HEADER,
1213				    erroff + opt - opthead);
1214				return (-1);
1215			}
1216
1217			/*
1218			 * We may see jumbolen in unaligned location, so
1219			 * we'd need to perform bcopy().
1220			 */
1221			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1222			jumboplen = (u_int32_t)htonl(jumboplen);
1223
1224#if 1
1225			/*
1226			 * if there are multiple jumbo payload options,
1227			 * *plenp will be non-zero and the packet will be
1228			 * rejected.
1229			 * the behavior may need some debate in ipngwg -
1230			 * multiple options does not make sense, however,
1231			 * there's no explicit mention in specification.
1232			 */
1233			if (*plenp != 0) {
1234				IP6STAT_INC(ip6s_badoptions);
1235				icmp6_error(m, ICMP6_PARAM_PROB,
1236				    ICMP6_PARAMPROB_HEADER,
1237				    erroff + opt + 2 - opthead);
1238				return (-1);
1239			}
1240#endif
1241
1242			/*
1243			 * jumbo payload length must be larger than 65535.
1244			 */
1245			if (jumboplen <= IPV6_MAXPACKET) {
1246				IP6STAT_INC(ip6s_badoptions);
1247				icmp6_error(m, ICMP6_PARAM_PROB,
1248				    ICMP6_PARAMPROB_HEADER,
1249				    erroff + opt + 2 - opthead);
1250				return (-1);
1251			}
1252			*plenp = jumboplen;
1253
1254			break;
1255		default:		/* unknown option */
1256			if (hbhlen < IP6OPT_MINLEN) {
1257				IP6STAT_INC(ip6s_toosmall);
1258				goto bad;
1259			}
1260			optlen = ip6_unknown_opt(opt, m,
1261			    erroff + opt - opthead);
1262			if (optlen == -1)
1263				return (-1);
1264			optlen += 2;
1265			break;
1266		}
1267	}
1268
1269	return (0);
1270
1271  bad:
1272	m_freem(m);
1273	return (-1);
1274}
1275
1276/*
1277 * Unknown option processing.
1278 * The third argument `off' is the offset from the IPv6 header to the option,
1279 * which is necessary if the IPv6 header the and option header and IPv6 header
1280 * is not contiguous in order to return an ICMPv6 error.
1281 */
1282int
1283ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1284{
1285	struct ip6_hdr *ip6;
1286
1287	switch (IP6OPT_TYPE(*optp)) {
1288	case IP6OPT_TYPE_SKIP: /* ignore the option */
1289		return ((int)*(optp + 1));
1290	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1291		m_freem(m);
1292		return (-1);
1293	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1294		IP6STAT_INC(ip6s_badoptions);
1295		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1296		return (-1);
1297	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1298		IP6STAT_INC(ip6s_badoptions);
1299		ip6 = mtod(m, struct ip6_hdr *);
1300		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1301		    (m->m_flags & (M_BCAST|M_MCAST)))
1302			m_freem(m);
1303		else
1304			icmp6_error(m, ICMP6_PARAM_PROB,
1305				    ICMP6_PARAMPROB_OPTION, off);
1306		return (-1);
1307	}
1308
1309	m_freem(m);		/* XXX: NOTREACHED */
1310	return (-1);
1311}
1312
1313/*
1314 * Create the "control" list for this pcb.
1315 * These functions will not modify mbuf chain at all.
1316 *
1317 * With KAME mbuf chain restriction:
1318 * The routine will be called from upper layer handlers like tcp6_input().
1319 * Thus the routine assumes that the caller (tcp6_input) have already
1320 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1321 * very first mbuf on the mbuf chain.
1322 *
1323 * ip6_savecontrol_v4 will handle those options that are possible to be
1324 * set on a v4-mapped socket.
1325 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1326 * options and handle the v6-only ones itself.
1327 */
1328struct mbuf **
1329ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1330    int *v4only)
1331{
1332	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1333
1334#ifdef SO_TIMESTAMP
1335	if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1336		struct timeval tv;
1337
1338		microtime(&tv);
1339		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1340		    SCM_TIMESTAMP, SOL_SOCKET);
1341		if (*mp)
1342			mp = &(*mp)->m_next;
1343	}
1344#endif
1345
1346#define IS2292(inp, x, y)	(((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1347	/* RFC 2292 sec. 5 */
1348	if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1349		struct in6_pktinfo pi6;
1350
1351		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1352#ifdef INET
1353			struct ip *ip;
1354
1355			ip = mtod(m, struct ip *);
1356			pi6.ipi6_addr.s6_addr32[0] = 0;
1357			pi6.ipi6_addr.s6_addr32[1] = 0;
1358			pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1359			pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1360#else
1361			/* We won't hit this code */
1362			bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1363#endif
1364		} else {
1365			bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1366			in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1367		}
1368		pi6.ipi6_ifindex =
1369		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1370
1371		*mp = sbcreatecontrol((caddr_t) &pi6,
1372		    sizeof(struct in6_pktinfo),
1373		    IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1374		if (*mp)
1375			mp = &(*mp)->m_next;
1376	}
1377
1378	if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1379		int hlim;
1380
1381		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1382#ifdef INET
1383			struct ip *ip;
1384
1385			ip = mtod(m, struct ip *);
1386			hlim = ip->ip_ttl;
1387#else
1388			/* We won't hit this code */
1389			hlim = 0;
1390#endif
1391		} else {
1392			hlim = ip6->ip6_hlim & 0xff;
1393		}
1394		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1395		    IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1396		    IPPROTO_IPV6);
1397		if (*mp)
1398			mp = &(*mp)->m_next;
1399	}
1400
1401	if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1402		int tclass;
1403
1404		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1405#ifdef INET
1406			struct ip *ip;
1407
1408			ip = mtod(m, struct ip *);
1409			tclass = ip->ip_tos;
1410#else
1411			/* We won't hit this code */
1412			tclass = 0;
1413#endif
1414		} else {
1415			u_int32_t flowinfo;
1416
1417			flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1418			flowinfo >>= 20;
1419			tclass = flowinfo & 0xff;
1420		}
1421		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
1422		    IPV6_TCLASS, IPPROTO_IPV6);
1423		if (*mp)
1424			mp = &(*mp)->m_next;
1425	}
1426
1427	if (v4only != NULL) {
1428		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1429			*v4only = 1;
1430		} else {
1431			*v4only = 0;
1432		}
1433	}
1434
1435	return (mp);
1436}
1437
1438void
1439ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1440{
1441	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1442	int v4only = 0;
1443
1444	mp = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1445	if (v4only)
1446		return;
1447
1448	/*
1449	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1450	 * privilege for the option (see ip6_ctloutput), but it might be too
1451	 * strict, since there might be some hop-by-hop options which can be
1452	 * returned to normal user.
1453	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1454	 */
1455	if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1456		/*
1457		 * Check if a hop-by-hop options header is contatined in the
1458		 * received packet, and if so, store the options as ancillary
1459		 * data. Note that a hop-by-hop options header must be
1460		 * just after the IPv6 header, which is assured through the
1461		 * IPv6 input processing.
1462		 */
1463		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1464			struct ip6_hbh *hbh;
1465			int hbhlen = 0;
1466#ifdef PULLDOWN_TEST
1467			struct mbuf *ext;
1468#endif
1469
1470#ifndef PULLDOWN_TEST
1471			hbh = (struct ip6_hbh *)(ip6 + 1);
1472			hbhlen = (hbh->ip6h_len + 1) << 3;
1473#else
1474			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1475			    ip6->ip6_nxt);
1476			if (ext == NULL) {
1477				IP6STAT_INC(ip6s_tooshort);
1478				return;
1479			}
1480			hbh = mtod(ext, struct ip6_hbh *);
1481			hbhlen = (hbh->ip6h_len + 1) << 3;
1482			if (hbhlen != ext->m_len) {
1483				m_freem(ext);
1484				IP6STAT_INC(ip6s_tooshort);
1485				return;
1486			}
1487#endif
1488
1489			/*
1490			 * XXX: We copy the whole header even if a
1491			 * jumbo payload option is included, the option which
1492			 * is to be removed before returning according to
1493			 * RFC2292.
1494			 * Note: this constraint is removed in RFC3542
1495			 */
1496			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1497			    IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1498			    IPPROTO_IPV6);
1499			if (*mp)
1500				mp = &(*mp)->m_next;
1501#ifdef PULLDOWN_TEST
1502			m_freem(ext);
1503#endif
1504		}
1505	}
1506
1507	if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1508		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1509
1510		/*
1511		 * Search for destination options headers or routing
1512		 * header(s) through the header chain, and stores each
1513		 * header as ancillary data.
1514		 * Note that the order of the headers remains in
1515		 * the chain of ancillary data.
1516		 */
1517		while (1) {	/* is explicit loop prevention necessary? */
1518			struct ip6_ext *ip6e = NULL;
1519			int elen;
1520#ifdef PULLDOWN_TEST
1521			struct mbuf *ext = NULL;
1522#endif
1523
1524			/*
1525			 * if it is not an extension header, don't try to
1526			 * pull it from the chain.
1527			 */
1528			switch (nxt) {
1529			case IPPROTO_DSTOPTS:
1530			case IPPROTO_ROUTING:
1531			case IPPROTO_HOPOPTS:
1532			case IPPROTO_AH: /* is it possible? */
1533				break;
1534			default:
1535				goto loopend;
1536			}
1537
1538#ifndef PULLDOWN_TEST
1539			if (off + sizeof(*ip6e) > m->m_len)
1540				goto loopend;
1541			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1542			if (nxt == IPPROTO_AH)
1543				elen = (ip6e->ip6e_len + 2) << 2;
1544			else
1545				elen = (ip6e->ip6e_len + 1) << 3;
1546			if (off + elen > m->m_len)
1547				goto loopend;
1548#else
1549			ext = ip6_pullexthdr(m, off, nxt);
1550			if (ext == NULL) {
1551				IP6STAT_INC(ip6s_tooshort);
1552				return;
1553			}
1554			ip6e = mtod(ext, struct ip6_ext *);
1555			if (nxt == IPPROTO_AH)
1556				elen = (ip6e->ip6e_len + 2) << 2;
1557			else
1558				elen = (ip6e->ip6e_len + 1) << 3;
1559			if (elen != ext->m_len) {
1560				m_freem(ext);
1561				IP6STAT_INC(ip6s_tooshort);
1562				return;
1563			}
1564#endif
1565
1566			switch (nxt) {
1567			case IPPROTO_DSTOPTS:
1568				if (!(in6p->inp_flags & IN6P_DSTOPTS))
1569					break;
1570
1571				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1572				    IS2292(in6p,
1573					IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1574				    IPPROTO_IPV6);
1575				if (*mp)
1576					mp = &(*mp)->m_next;
1577				break;
1578			case IPPROTO_ROUTING:
1579				if (!(in6p->inp_flags & IN6P_RTHDR))
1580					break;
1581
1582				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1583				    IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
1584				    IPPROTO_IPV6);
1585				if (*mp)
1586					mp = &(*mp)->m_next;
1587				break;
1588			case IPPROTO_HOPOPTS:
1589			case IPPROTO_AH: /* is it possible? */
1590				break;
1591
1592			default:
1593				/*
1594				 * other cases have been filtered in the above.
1595				 * none will visit this case.  here we supply
1596				 * the code just in case (nxt overwritten or
1597				 * other cases).
1598				 */
1599#ifdef PULLDOWN_TEST
1600				m_freem(ext);
1601#endif
1602				goto loopend;
1603
1604			}
1605
1606			/* proceed with the next header. */
1607			off += elen;
1608			nxt = ip6e->ip6e_nxt;
1609			ip6e = NULL;
1610#ifdef PULLDOWN_TEST
1611			m_freem(ext);
1612			ext = NULL;
1613#endif
1614		}
1615	  loopend:
1616		;
1617	}
1618}
1619#undef IS2292
1620
1621void
1622ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu)
1623{
1624	struct socket *so;
1625	struct mbuf *m_mtu;
1626	struct ip6_mtuinfo mtuctl;
1627
1628	so =  in6p->inp_socket;
1629
1630	if (mtu == NULL)
1631		return;
1632
1633#ifdef DIAGNOSTIC
1634	if (so == NULL)		/* I believe this is impossible */
1635		panic("ip6_notify_pmtu: socket is NULL");
1636#endif
1637
1638	bzero(&mtuctl, sizeof(mtuctl));	/* zero-clear for safety */
1639	mtuctl.ip6m_mtu = *mtu;
1640	mtuctl.ip6m_addr = *dst;
1641	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1642		return;
1643
1644	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1645	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1646		return;
1647
1648	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1649	    == 0) {
1650		m_freem(m_mtu);
1651		/* XXX: should count statistics */
1652	} else
1653		sorwakeup(so);
1654
1655	return;
1656}
1657
1658#ifdef PULLDOWN_TEST
1659/*
1660 * pull single extension header from mbuf chain.  returns single mbuf that
1661 * contains the result, or NULL on error.
1662 */
1663static struct mbuf *
1664ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1665{
1666	struct ip6_ext ip6e;
1667	size_t elen;
1668	struct mbuf *n;
1669
1670#ifdef DIAGNOSTIC
1671	switch (nxt) {
1672	case IPPROTO_DSTOPTS:
1673	case IPPROTO_ROUTING:
1674	case IPPROTO_HOPOPTS:
1675	case IPPROTO_AH: /* is it possible? */
1676		break;
1677	default:
1678		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1679	}
1680#endif
1681
1682	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1683	if (nxt == IPPROTO_AH)
1684		elen = (ip6e.ip6e_len + 2) << 2;
1685	else
1686		elen = (ip6e.ip6e_len + 1) << 3;
1687
1688	if (elen > MLEN)
1689		n = m_getcl(M_NOWAIT, MT_DATA, 0);
1690	else
1691		n = m_get(M_NOWAIT, MT_DATA);
1692	if (n == NULL)
1693		return NULL;
1694
1695	m_copydata(m, off, elen, mtod(n, caddr_t));
1696	n->m_len = elen;
1697	return n;
1698}
1699#endif
1700
1701/*
1702 * Get pointer to the previous header followed by the header
1703 * currently processed.
1704 * XXX: This function supposes that
1705 *	M includes all headers,
1706 *	the next header field and the header length field of each header
1707 *	are valid, and
1708 *	the sum of each header length equals to OFF.
1709 * Because of these assumptions, this function must be called very
1710 * carefully. Moreover, it will not be used in the near future when
1711 * we develop `neater' mechanism to process extension headers.
1712 */
1713char *
1714ip6_get_prevhdr(struct mbuf *m, int off)
1715{
1716	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1717
1718	if (off == sizeof(struct ip6_hdr))
1719		return (&ip6->ip6_nxt);
1720	else {
1721		int len, nxt;
1722		struct ip6_ext *ip6e = NULL;
1723
1724		nxt = ip6->ip6_nxt;
1725		len = sizeof(struct ip6_hdr);
1726		while (len < off) {
1727			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1728
1729			switch (nxt) {
1730			case IPPROTO_FRAGMENT:
1731				len += sizeof(struct ip6_frag);
1732				break;
1733			case IPPROTO_AH:
1734				len += (ip6e->ip6e_len + 2) << 2;
1735				break;
1736			default:
1737				len += (ip6e->ip6e_len + 1) << 3;
1738				break;
1739			}
1740			nxt = ip6e->ip6e_nxt;
1741		}
1742		if (ip6e)
1743			return (&ip6e->ip6e_nxt);
1744		else
1745			return NULL;
1746	}
1747}
1748
1749/*
1750 * get next header offset.  m will be retained.
1751 */
1752int
1753ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1754{
1755	struct ip6_hdr ip6;
1756	struct ip6_ext ip6e;
1757	struct ip6_frag fh;
1758
1759	/* just in case */
1760	if (m == NULL)
1761		panic("ip6_nexthdr: m == NULL");
1762	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1763		return -1;
1764
1765	switch (proto) {
1766	case IPPROTO_IPV6:
1767		if (m->m_pkthdr.len < off + sizeof(ip6))
1768			return -1;
1769		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1770		if (nxtp)
1771			*nxtp = ip6.ip6_nxt;
1772		off += sizeof(ip6);
1773		return off;
1774
1775	case IPPROTO_FRAGMENT:
1776		/*
1777		 * terminate parsing if it is not the first fragment,
1778		 * it does not make sense to parse through it.
1779		 */
1780		if (m->m_pkthdr.len < off + sizeof(fh))
1781			return -1;
1782		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1783		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1784		if (fh.ip6f_offlg & IP6F_OFF_MASK)
1785			return -1;
1786		if (nxtp)
1787			*nxtp = fh.ip6f_nxt;
1788		off += sizeof(struct ip6_frag);
1789		return off;
1790
1791	case IPPROTO_AH:
1792		if (m->m_pkthdr.len < off + sizeof(ip6e))
1793			return -1;
1794		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1795		if (nxtp)
1796			*nxtp = ip6e.ip6e_nxt;
1797		off += (ip6e.ip6e_len + 2) << 2;
1798		return off;
1799
1800	case IPPROTO_HOPOPTS:
1801	case IPPROTO_ROUTING:
1802	case IPPROTO_DSTOPTS:
1803		if (m->m_pkthdr.len < off + sizeof(ip6e))
1804			return -1;
1805		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1806		if (nxtp)
1807			*nxtp = ip6e.ip6e_nxt;
1808		off += (ip6e.ip6e_len + 1) << 3;
1809		return off;
1810
1811	case IPPROTO_NONE:
1812	case IPPROTO_ESP:
1813	case IPPROTO_IPCOMP:
1814		/* give up */
1815		return -1;
1816
1817	default:
1818		return -1;
1819	}
1820
1821	return -1;
1822}
1823
1824/*
1825 * get offset for the last header in the chain.  m will be kept untainted.
1826 */
1827int
1828ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1829{
1830	int newoff;
1831	int nxt;
1832
1833	if (!nxtp) {
1834		nxt = -1;
1835		nxtp = &nxt;
1836	}
1837	while (1) {
1838		newoff = ip6_nexthdr(m, off, proto, nxtp);
1839		if (newoff < 0)
1840			return off;
1841		else if (newoff < off)
1842			return -1;	/* invalid */
1843		else if (newoff == off)
1844			return newoff;
1845
1846		off = newoff;
1847		proto = *nxtp;
1848	}
1849}
1850
1851static struct ip6aux *
1852ip6_addaux(struct mbuf *m)
1853{
1854	struct m_tag *mtag;
1855
1856	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1857	if (!mtag) {
1858		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1859		    M_NOWAIT);
1860		if (mtag) {
1861			m_tag_prepend(m, mtag);
1862			bzero(mtag + 1, sizeof(struct ip6aux));
1863		}
1864	}
1865	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1866}
1867
1868static struct ip6aux *
1869ip6_findaux(struct mbuf *m)
1870{
1871	struct m_tag *mtag;
1872
1873	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1874	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1875}
1876
1877static void
1878ip6_delaux(struct mbuf *m)
1879{
1880	struct m_tag *mtag;
1881
1882	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1883	if (mtag)
1884		m_tag_delete(m, mtag);
1885}
1886
1887/*
1888 * System control for IP6
1889 */
1890
1891u_char	inet6ctlerrmap[PRC_NCMDS] = {
1892	0,		0,		0,		0,
1893	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1894	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1895	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1896	0,		0,		0,		0,
1897	ENOPROTOOPT
1898};
1899