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