Annotation of src/sys/netinet6/ip6_input.c, Revision 1.8.2.2
1.8.2.2 ! bouyer 1: /* $NetBSD$ */
1.8.2.1 bouyer 2: /* $KAME: ip6_input.c,v 1.121 2000/08/31 06:07:29 itojun Exp $ */
1.3 thorpej 3:
1.2 itojun 4: /*
5: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6: * All rights reserved.
1.8.2.1 bouyer 7: *
1.2 itojun 8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. Neither the name of the project nor the names of its contributors
17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
1.8.2.1 bouyer 19: *
1.2 itojun 20: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: /*
34: * Copyright (c) 1982, 1986, 1988, 1993
35: * The Regents of the University of California. All rights reserved.
36: *
37: * Redistribution and use in source and binary forms, with or without
38: * modification, are permitted provided that the following conditions
39: * are met:
40: * 1. Redistributions of source code must retain the above copyright
41: * notice, this list of conditions and the following disclaimer.
42: * 2. Redistributions in binary form must reproduce the above copyright
43: * notice, this list of conditions and the following disclaimer in the
44: * documentation and/or other materials provided with the distribution.
45: * 3. All advertising materials mentioning features or use of this software
46: * must display the following acknowledgement:
47: * This product includes software developed by the University of
48: * California, Berkeley and its contributors.
49: * 4. Neither the name of the University nor the names of its contributors
50: * may be used to endorse or promote products derived from this software
51: * without specific prior written permission.
52: *
53: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63: * SUCH DAMAGE.
64: *
65: * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
66: */
67:
68: #include "opt_inet.h"
1.4 thorpej 69: #include "opt_ipsec.h"
1.8.2.1 bouyer 70: #include "opt_pfil_hooks.h"
1.2 itojun 71:
72: #include <sys/param.h>
73: #include <sys/systm.h>
74: #include <sys/malloc.h>
75: #include <sys/mbuf.h>
76: #include <sys/domain.h>
77: #include <sys/protosw.h>
78: #include <sys/socket.h>
79: #include <sys/socketvar.h>
80: #include <sys/errno.h>
81: #include <sys/time.h>
82: #include <sys/kernel.h>
83: #include <sys/syslog.h>
84: #include <sys/proc.h>
85:
86: #include <net/if.h>
87: #include <net/if_types.h>
88: #include <net/if_dl.h>
89: #include <net/route.h>
90: #include <net/netisr.h>
1.8.2.1 bouyer 91: #ifdef PFIL_HOOKS
92: #include <net/pfil.h>
93: #endif
1.2 itojun 94:
95: #include <netinet/in.h>
96: #include <netinet/in_systm.h>
97: #ifdef INET
98: #include <netinet/ip.h>
99: #include <netinet/ip_icmp.h>
100: #endif /*INET*/
1.8.2.1 bouyer 101: #include <netinet/ip6.h>
102: #include <netinet6/in6_var.h>
103: #include <netinet6/ip6_var.h>
104: #include <netinet6/in6_pcb.h>
105: #include <netinet/icmp6.h>
106: #include <netinet6/in6_ifattach.h>
107: #include <netinet6/nd6.h>
108: #include <netinet6/in6_prefix.h>
109:
110: #ifdef IPV6FIREWALL
111: #include <netinet6/ip6_fw.h>
112: #endif
1.2 itojun 113:
114: #include <netinet6/ip6protosw.h>
115:
116: /* we need it for NLOOP. */
117: #include "loop.h"
118: #include "faith.h"
119:
120: #include "gif.h"
121: #include "bpfilter.h"
122:
1.8.2.1 bouyer 123: #include <net/net_osdep.h>
124:
1.2 itojun 125: extern struct domain inet6domain;
126:
127: u_char ip6_protox[IPPROTO_MAX];
128: static int ip6qmaxlen = IFQ_MAXLEN;
129: struct in6_ifaddr *in6_ifaddr;
130: struct ifqueue ip6intrq;
131:
132: extern struct ifnet loif[NLOOP];
133: int ip6_forward_srcrt; /* XXX */
134: int ip6_sourcecheck; /* XXX */
135: int ip6_sourcecheck_interval; /* XXX */
1.8.2.1 bouyer 136:
137: #ifdef IPV6FIREWALL
138: /* firewall hooks */
139: ip6_fw_chk_t *ip6_fw_chk_ptr;
140: ip6_fw_ctl_t *ip6_fw_ctl_ptr;
1.2 itojun 141: #endif
142:
1.8.2.2 ! bouyer 143: #ifdef PFIL_HOOKS
! 144: struct pfil_head inet6_pfil_hook;
! 145: #endif
! 146:
1.2 itojun 147: struct ip6stat ip6stat;
148:
149: static void ip6_init2 __P((void *));
150:
1.5 itojun 151: static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
1.2 itojun 152:
153: /*
154: * IP6 initialization: fill in IP6 protocol switch table.
155: * All protocols not implemented in kernel go to raw IP6 protocol handler.
156: */
157: void
158: ip6_init()
159: {
160: register struct ip6protosw *pr;
161: register int i;
162: struct timeval tv;
163:
164: pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
165: if (pr == 0)
166: panic("ip6_init");
167: for (i = 0; i < IPPROTO_MAX; i++)
168: ip6_protox[i] = pr - inet6sw;
169: for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
170: pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
171: if (pr->pr_domain->dom_family == PF_INET6 &&
172: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
173: ip6_protox[pr->pr_protocol] = pr - inet6sw;
174: ip6intrq.ifq_maxlen = ip6qmaxlen;
175: nd6_init();
176: frag6_init();
1.8.2.1 bouyer 177: #ifdef IPV6FIREWALL
178: ip6_fw_init();
179: #endif
1.2 itojun 180: /*
181: * in many cases, random() here does NOT return random number
182: * as initialization during bootstrap time occur in fixed order.
183: */
184: microtime(&tv);
185: ip6_flow_seq = random() ^ tv.tv_usec;
186:
187: ip6_init2((void *)0);
1.8.2.2 ! bouyer 188:
! 189: #ifdef PFIL_HOOKS
! 190: /* Register our Packet Filter hook. */
! 191: inet6_pfil_hook.ph_key = (void *)(u_long) AF_INET6;
! 192: inet6_pfil_hook.ph_dlt = DLT_RAW;
! 193: i = pfil_head_register(&inet6_pfil_hook);
! 194: if (i != 0)
! 195: printf("ip6_init: WARNING: unable to register pfil hook, "
! 196: "error %d\n", i);
! 197: #endif /* PFIL_HOOKS */
1.2 itojun 198: }
199:
200: static void
201: ip6_init2(dummy)
202: void *dummy;
203: {
204: /*
205: * to route local address of p2p link to loopback,
1.8.2.1 bouyer 206: * assign loopback address first.
1.2 itojun 207: */
1.8.2.1 bouyer 208: in6_ifattach(&loif[0], NULL);
1.2 itojun 209:
210: /* nd6_timer_init */
1.8.2.1 bouyer 211: callout_init(&nd6_timer_ch);
212: callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
213: /* router renumbering prefix list maintenance */
214: callout_init(&in6_rr_timer_ch);
215: callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
1.2 itojun 216: }
217:
218: /*
219: * IP6 input interrupt handling. Just pass the packet to ip6_input.
220: */
221: void
222: ip6intr()
223: {
224: int s;
225: struct mbuf *m;
226:
227: for (;;) {
228: s = splimp();
229: IF_DEQUEUE(&ip6intrq, m);
230: splx(s);
231: if (m == 0)
232: return;
233: ip6_input(m);
234: }
235: }
236:
237: extern struct route_in6 ip6_forward_rt;
238:
239: void
240: ip6_input(m)
241: struct mbuf *m;
242: {
1.8.2.1 bouyer 243: struct ip6_hdr *ip6;
1.2 itojun 244: int off = sizeof(struct ip6_hdr), nest;
245: u_int32_t plen;
1.5 itojun 246: u_int32_t rtalert = ~0;
1.2 itojun 247: int nxt, ours = 0;
1.8.2.1 bouyer 248: struct ifnet *deliverifp = NULL;
1.2 itojun 249:
250: #ifdef IPSEC
251: /*
252: * should the inner packet be considered authentic?
253: * see comment in ah4_input().
254: */
255: if (m) {
256: m->m_flags &= ~M_AUTHIPHDR;
257: m->m_flags &= ~M_AUTHIPDGM;
258: }
259: #endif
1.8.2.1 bouyer 260:
1.2 itojun 261: /*
262: * mbuf statistics by kazu
263: */
264: if (m->m_flags & M_EXT) {
265: if (m->m_next)
266: ip6stat.ip6s_mext2m++;
267: else
268: ip6stat.ip6s_mext1++;
269: } else {
270: if (m->m_next) {
1.8.2.1 bouyer 271: if (m->m_flags & M_LOOP) {
1.2 itojun 272: ip6stat.ip6s_m2m[loif[0].if_index]++; /*XXX*/
1.8.2.1 bouyer 273: } else if (m->m_pkthdr.rcvif->if_index <= 31)
1.2 itojun 274: ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
275: else
276: ip6stat.ip6s_m2m[0]++;
277: } else
278: ip6stat.ip6s_m1++;
279: }
280:
1.8.2.1 bouyer 281: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
1.2 itojun 282: ip6stat.ip6s_total++;
283:
1.8.2.1 bouyer 284: #ifndef PULLDOWN_TEST
285: /* XXX is the line really necessary? */
286: IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
287: #endif
288:
289: if (m->m_len < sizeof(struct ip6_hdr)) {
290: struct ifnet *inifp;
291: inifp = m->m_pkthdr.rcvif;
292: if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
293: ip6stat.ip6s_toosmall++;
294: in6_ifstat_inc(inifp, ifs6_in_hdrerr);
295: return;
296: }
1.2 itojun 297: }
298:
299: ip6 = mtod(m, struct ip6_hdr *);
300:
301: if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
302: ip6stat.ip6s_badvers++;
1.8.2.1 bouyer 303: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2 itojun 304: goto bad;
305: }
306:
1.8.2.1 bouyer 307: #ifdef PFIL_HOOKS
308: /*
309: * Run through list of hooks for input packets. If there are any
310: * filters which require that additional packets in the flow are
311: * not fast-forwarded, they must clear the M_CANFASTFWD flag.
312: * Note that filters must _never_ set this flag, as another filter
313: * in the list may have previously cleared it.
314: */
1.8.2.2 ! bouyer 315: if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
! 316: PFIL_IN) != 0)
! 317: return;
! 318: if (m == NULL)
! 319: return;
! 320: ip6 = mtod(m, struct ip6_hdr *);
1.8.2.1 bouyer 321: #endif /* PFIL_HOOKS */
322:
323:
1.2 itojun 324: ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
325:
1.8.2.1 bouyer 326: #ifdef IPV6FIREWALL
327: /*
328: * Check with the firewall...
329: */
330: if (ip6_fw_chk_ptr) {
331: u_short port = 0;
332: /* If ipfw says divert, we have to just drop packet */
333: /* use port as a dummy argument */
334: if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
335: m_freem(m);
336: m = NULL;
337: }
338: if (!m)
339: return;
340: }
341: #endif
342:
1.2 itojun 343: /*
344: * Scope check
345: */
346: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
347: IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
348: ip6stat.ip6s_badscope++;
1.8.2.1 bouyer 349: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2 itojun 350: goto bad;
351: }
1.8.2.1 bouyer 352: /*
353: * The following check is not documented in the spec. Malicious party
354: * may be able to use IPv4 mapped addr to confuse tcp/udp stack and
355: * bypass security checks (act as if it was from 127.0.0.1 by using
356: * IPv6 src ::ffff:127.0.0.1). Be cautious.
357: */
358: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
359: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
360: ip6stat.ip6s_badscope++;
361: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
362: goto bad;
363: }
364: #if 0
365: /*
366: * Reject packets with IPv4 compatible addresses (auto tunnel).
367: *
368: * The code forbids auto tunnel relay case in RFC1933 (the check is
369: * stronger than RFC1933). We may want to re-enable it if mech-xx
370: * is revised to forbid relaying case.
371: */
372: if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
373: IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
374: ip6stat.ip6s_badscope++;
375: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
376: goto bad;
377: }
378: #endif
1.2 itojun 379: if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
380: IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
381: if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
382: ours = 1;
1.8.2.1 bouyer 383: deliverifp = m->m_pkthdr.rcvif;
1.2 itojun 384: goto hbhcheck;
385: } else {
386: ip6stat.ip6s_badscope++;
1.8.2.1 bouyer 387: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2 itojun 388: goto bad;
389: }
390: }
391:
1.8.2.1 bouyer 392: #ifndef FAKE_LOOPBACK_IF
393: if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0)
394: #else
395: if (1)
396: #endif
397: {
1.2 itojun 398: if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
399: ip6->ip6_src.s6_addr16[1]
400: = htons(m->m_pkthdr.rcvif->if_index);
401: if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
402: ip6->ip6_dst.s6_addr16[1]
403: = htons(m->m_pkthdr.rcvif->if_index);
404: }
405:
406: /*
1.8.2.1 bouyer 407: * XXX we need this since we do not have "goto ours" hack route
408: * for some of our ifaddrs on loopback interface.
409: * we should correct it by changing in6_ifattach to install
410: * "goto ours" hack route.
411: */
412: if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0) {
413: if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
414: ours = 1;
415: deliverifp = m->m_pkthdr.rcvif;
416: goto hbhcheck;
417: }
418: }
419:
420: /*
1.2 itojun 421: * Multicast check
422: */
423: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
424: struct in6_multi *in6m = 0;
1.8.2.1 bouyer 425:
426: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
1.2 itojun 427: /*
428: * See if we belong to the destination multicast group on the
429: * arrival interface.
430: */
431: IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
432: if (in6m)
433: ours = 1;
434: else if (!ip6_mrouter) {
435: ip6stat.ip6s_notmember++;
436: ip6stat.ip6s_cantforward++;
1.8.2.1 bouyer 437: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2 itojun 438: goto bad;
439: }
1.8.2.1 bouyer 440: deliverifp = m->m_pkthdr.rcvif;
1.2 itojun 441: goto hbhcheck;
442: }
443:
444: /*
445: * Unicast check
446: */
1.8.2.1 bouyer 447: if (ip6_forward_rt.ro_rt != NULL &&
448: (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
449: IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
450: &ip6_forward_rt.ro_dst.sin6_addr))
451: ip6stat.ip6s_forward_cachehit++;
452: else {
1.2 itojun 453: if (ip6_forward_rt.ro_rt) {
1.8.2.1 bouyer 454: /* route is down or destination is different */
455: ip6stat.ip6s_forward_cachemiss++;
1.2 itojun 456: RTFREE(ip6_forward_rt.ro_rt);
457: ip6_forward_rt.ro_rt = 0;
458: }
1.8.2.1 bouyer 459:
1.2 itojun 460: bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
461: ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
462: ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
463: ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
464:
465: rtalloc((struct route *)&ip6_forward_rt);
466: }
467:
468: #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
469:
470: /*
471: * Accept the packet if the forwarding interface to the destination
472: * according to the routing table is the loopback interface,
473: * unless the associated route has a gateway.
474: * Note that this approach causes to accept a packet if there is a
475: * route to the loopback interface for the destination of the packet.
476: * But we think it's even useful in some situations, e.g. when using
477: * a special daemon which wants to intercept the packet.
478: */
479: if (ip6_forward_rt.ro_rt &&
480: (ip6_forward_rt.ro_rt->rt_flags &
481: (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
482: #if 0
483: /*
484: * The check below is redundant since the comparison of
485: * the destination and the key of the rtentry has
486: * already done through looking up the routing table.
487: */
488: IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
489: &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
490: #endif
1.8.2.1 bouyer 491: ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
1.2 itojun 492: struct in6_ifaddr *ia6 =
493: (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
494: if (ia6->ia6_flags & IN6_IFF_ANYCAST)
495: m->m_flags |= M_ANYCAST6;
1.8.2.1 bouyer 496: /*
497: * packets to a tentative, duplicated, or somehow invalid
498: * address must not be accepted.
499: */
1.2 itojun 500: if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
1.8.2.1 bouyer 501: /* this address is ready */
1.2 itojun 502: ours = 1;
1.8.2.1 bouyer 503: deliverifp = ia6->ia_ifp; /* correct? */
1.2 itojun 504: goto hbhcheck;
505: } else {
1.8.2.1 bouyer 506: /* address is not ready, so discard the packet. */
507: log(LOG_INFO,
508: "ip6_input: packet to an unready address %s->%s\n",
509: ip6_sprintf(&ip6->ip6_src),
510: ip6_sprintf(&ip6->ip6_dst));
511:
512: goto bad;
1.2 itojun 513: }
514: }
515:
516: /*
517: * FAITH(Firewall Aided Internet Translator)
518: */
519: #if defined(NFAITH) && 0 < NFAITH
520: if (ip6_keepfaith) {
521: if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
522: && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
523: /* XXX do we need more sanity checks? */
524: ours = 1;
1.8.2.1 bouyer 525: deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
526: goto hbhcheck;
527: }
528: }
529: #endif
530:
531: #if 0
532: {
533: /*
534: * Last resort: check in6_ifaddr for incoming interface.
535: * The code is here until I update the "goto ours hack" code above
536: * working right.
537: */
538: struct ifaddr *ifa;
539: for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
540: ifa;
541: ifa = ifa->ifa_list.tqe_next) {
542: if (ifa->ifa_addr == NULL)
543: continue; /* just for safety */
544: if (ifa->ifa_addr->sa_family != AF_INET6)
545: continue;
546: if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
547: ours = 1;
548: deliverifp = ifa->ifa_ifp;
1.2 itojun 549: goto hbhcheck;
550: }
551: }
1.8.2.1 bouyer 552: }
1.2 itojun 553: #endif
554:
555: /*
556: * Now there is no reason to process the packet if it's not our own
557: * and we're not a router.
558: */
559: if (!ip6_forwarding) {
560: ip6stat.ip6s_cantforward++;
1.8.2.1 bouyer 561: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2 itojun 562: goto bad;
563: }
564:
565: hbhcheck:
566: /*
567: * Process Hop-by-Hop options header if it's contained.
568: * m may be modified in ip6_hopopts_input().
569: * If a JumboPayload option is included, plen will also be modified.
570: */
571: plen = (u_int32_t)ntohs(ip6->ip6_plen);
572: if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.8.2.1 bouyer 573: struct ip6_hbh *hbh;
574:
575: if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
576: #if 0 /*touches NULL pointer*/
577: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
578: #endif
1.2 itojun 579: return; /* m have already been freed */
1.8.2.1 bouyer 580: }
581:
1.2 itojun 582: /* adjust pointer */
583: ip6 = mtod(m, struct ip6_hdr *);
1.8.2.1 bouyer 584:
585: /*
586: * if the payload length field is 0 and the next header field
587: * indicates Hop-by-Hop Options header, then a Jumbo Payload
588: * option MUST be included.
589: */
590: if (ip6->ip6_plen == 0 && plen == 0) {
591: /*
592: * Note that if a valid jumbo payload option is
593: * contained, ip6_hoptops_input() must set a valid
594: * (non-zero) payload length to the variable plen.
595: */
596: ip6stat.ip6s_badoptions++;
597: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
598: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
599: icmp6_error(m, ICMP6_PARAM_PROB,
600: ICMP6_PARAMPROB_HEADER,
601: (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
602: return;
603: }
604: #ifndef PULLDOWN_TEST
605: /* ip6_hopopts_input() ensures that mbuf is contiguous */
606: hbh = (struct ip6_hbh *)(ip6 + 1);
607: #else
608: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
609: sizeof(struct ip6_hbh));
610: if (hbh == NULL) {
611: ip6stat.ip6s_tooshort++;
612: return;
613: }
614: #endif
615: nxt = hbh->ip6h_nxt;
1.2 itojun 616:
617: /*
618: * accept the packet if a router alert option is included
619: * and we act as an IPv6 router.
620: */
1.5 itojun 621: if (rtalert != ~0 && ip6_forwarding)
1.2 itojun 622: ours = 1;
623: } else
624: nxt = ip6->ip6_nxt;
625:
626: /*
627: * Check that the amount of data in the buffers
628: * is as at least much as the IPv6 header would have us expect.
629: * Trim mbufs if longer than we expect.
630: * Drop packet if shorter than we expect.
631: */
632: if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
633: ip6stat.ip6s_tooshort++;
1.8.2.1 bouyer 634: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.2 itojun 635: goto bad;
636: }
637: if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
638: if (m->m_len == m->m_pkthdr.len) {
639: m->m_len = sizeof(struct ip6_hdr) + plen;
640: m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
641: } else
642: m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
643: }
644:
645: /*
646: * Forward if desirable.
647: */
648: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
649: /*
650: * If we are acting as a multicast router, all
651: * incoming multicast packets are passed to the
652: * kernel-level multicast forwarding function.
653: * The packet is returned (relatively) intact; if
654: * ip6_mforward() returns a non-zero value, the packet
655: * must be discarded, else it may be accepted below.
656: */
657: if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
658: ip6stat.ip6s_cantforward++;
659: m_freem(m);
660: return;
661: }
662: if (!ours) {
663: m_freem(m);
664: return;
665: }
1.8.2.1 bouyer 666: } else if (!ours) {
1.2 itojun 667: ip6_forward(m, 0);
668: return;
669: }
670:
1.8.2.1 bouyer 671: ip6 = mtod(m, struct ip6_hdr *);
672:
673: /*
674: * Malicious party may be able to use IPv4 mapped addr to confuse
675: * tcp/udp stack and bypass security checks (act as if it was from
676: * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
677: *
678: * For SIIT end node behavior, you may want to disable the check.
679: * However, you will become vulnerable to attacks using IPv4 mapped
680: * source.
681: */
682: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
683: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
684: ip6stat.ip6s_badscope++;
685: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
686: goto bad;
687: }
688:
1.2 itojun 689: /*
690: * Tell launch routine the next header
691: */
1.8.2.1 bouyer 692: #ifdef IFA_STATS
693: if (deliverifp != NULL) {
694: struct in6_ifaddr *ia6;
695: ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
696: if (ia6)
697: ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
698: }
699: #endif
1.2 itojun 700: ip6stat.ip6s_delivered++;
1.8.2.1 bouyer 701: in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1.2 itojun 702: nest = 0;
703: while (nxt != IPPROTO_DONE) {
704: if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
705: ip6stat.ip6s_toomanyhdr++;
706: goto bad;
707: }
1.8 itojun 708:
709: /*
710: * protection against faulty packet - there should be
711: * more sanity checks in header chain processing.
712: */
713: if (m->m_pkthdr.len < off) {
714: ip6stat.ip6s_tooshort++;
1.8.2.1 bouyer 715: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.8 itojun 716: goto bad;
717: }
718:
1.2 itojun 719: nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
720: }
721: return;
722: bad:
723: m_freem(m);
724: }
725:
726: /*
727: * Hop-by-Hop options header processing. If a valid jumbo payload option is
728: * included, the real payload length will be stored in plenp.
729: */
730: static int
731: ip6_hopopts_input(plenp, rtalertp, mp, offp)
732: u_int32_t *plenp;
1.5 itojun 733: u_int32_t *rtalertp; /* XXX: should be stored more smart way */
1.2 itojun 734: struct mbuf **mp;
735: int *offp;
736: {
737: register struct mbuf *m = *mp;
738: int off = *offp, hbhlen;
739: struct ip6_hbh *hbh;
740: u_int8_t *opt;
741:
742: /* validation of the length of the header */
1.8.2.1 bouyer 743: #ifndef PULLDOWN_TEST
1.2 itojun 744: IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
745: hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
746: hbhlen = (hbh->ip6h_len + 1) << 3;
747:
748: IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
749: hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1.8.2.1 bouyer 750: #else
751: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
752: sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
753: if (hbh == NULL) {
754: ip6stat.ip6s_tooshort++;
755: return -1;
756: }
757: hbhlen = (hbh->ip6h_len + 1) << 3;
758: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
759: hbhlen);
760: if (hbh == NULL) {
761: ip6stat.ip6s_tooshort++;
762: return -1;
763: }
764: #endif
1.2 itojun 765: off += hbhlen;
766: hbhlen -= sizeof(struct ip6_hbh);
767: opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
768:
769: if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
770: hbhlen, rtalertp, plenp) < 0)
771: return(-1);
772:
773: *offp = off;
774: *mp = m;
775: return(0);
776: }
777:
778: /*
779: * Search header for all Hop-by-hop options and process each option.
780: * This function is separate from ip6_hopopts_input() in order to
781: * handle a case where the sending node itself process its hop-by-hop
782: * options header. In such a case, the function is called from ip6_output().
783: */
784: int
785: ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
786: struct mbuf *m;
787: u_int8_t *opthead;
788: int hbhlen;
1.5 itojun 789: u_int32_t *rtalertp;
1.2 itojun 790: u_int32_t *plenp;
791: {
792: struct ip6_hdr *ip6;
793: int optlen = 0;
794: u_int8_t *opt = opthead;
795: u_int16_t rtalert_val;
1.8.2.1 bouyer 796: u_int32_t jumboplen;
1.2 itojun 797:
798: for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
799: switch(*opt) {
800: case IP6OPT_PAD1:
801: optlen = 1;
802: break;
803: case IP6OPT_PADN:
804: if (hbhlen < IP6OPT_MINLEN) {
805: ip6stat.ip6s_toosmall++;
806: goto bad;
807: }
808: optlen = *(opt + 1) + 2;
809: break;
810: case IP6OPT_RTALERT:
1.8 itojun 811: /* XXX may need check for alignment */
1.2 itojun 812: if (hbhlen < IP6OPT_RTALERT_LEN) {
813: ip6stat.ip6s_toosmall++;
814: goto bad;
815: }
816: if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2)
817: /* XXX: should we discard the packet? */
818: log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
819: *(opt + 1));
820: optlen = IP6OPT_RTALERT_LEN;
821: bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
822: *rtalertp = ntohs(rtalert_val);
823: break;
824: case IP6OPT_JUMBO:
1.8.2.1 bouyer 825: /* XXX may need check for alignment */
826: if (hbhlen < IP6OPT_JUMBO_LEN) {
827: ip6stat.ip6s_toosmall++;
828: goto bad;
829: }
830: if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2)
831: /* XXX: should we discard the packet? */
832: log(LOG_ERR, "length of jumbopayload opt "
833: "is inconsistent(%d)\n",
834: *(opt + 1));
835: optlen = IP6OPT_JUMBO_LEN;
1.2 itojun 836:
1.8.2.1 bouyer 837: /*
838: * IPv6 packets that have non 0 payload length
839: * must not contain a jumbo paylod option.
840: */
841: ip6 = mtod(m, struct ip6_hdr *);
842: if (ip6->ip6_plen) {
843: ip6stat.ip6s_badoptions++;
844: icmp6_error(m, ICMP6_PARAM_PROB,
845: ICMP6_PARAMPROB_HEADER,
846: sizeof(struct ip6_hdr) +
847: sizeof(struct ip6_hbh) +
848: opt - opthead);
849: return(-1);
850: }
851:
852: /*
853: * We may see jumbolen in unaligned location, so
854: * we'd need to perform bcopy().
855: */
856: bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
857: jumboplen = (u_int32_t)htonl(jumboplen);
858:
859: #if 1
860: /*
861: * if there are multiple jumbo payload options,
862: * *plenp will be non-zero and the packet will be
863: * rejected.
864: * the behavior may need some debate in ipngwg -
865: * multiple options does not make sense, however,
866: * there's no explicit mention in specification.
867: */
868: if (*plenp != 0) {
869: ip6stat.ip6s_badoptions++;
870: icmp6_error(m, ICMP6_PARAM_PROB,
871: ICMP6_PARAMPROB_HEADER,
872: sizeof(struct ip6_hdr) +
873: sizeof(struct ip6_hbh) +
874: opt + 2 - opthead);
875: return(-1);
876: }
1.8 itojun 877: #endif
1.2 itojun 878:
1.8.2.1 bouyer 879: /*
880: * jumbo payload length must be larger than 65535.
881: */
882: if (jumboplen <= IPV6_MAXPACKET) {
883: ip6stat.ip6s_badoptions++;
884: icmp6_error(m, ICMP6_PARAM_PROB,
885: ICMP6_PARAMPROB_HEADER,
886: sizeof(struct ip6_hdr) +
887: sizeof(struct ip6_hbh) +
888: opt + 2 - opthead);
889: return(-1);
890: }
891: *plenp = jumboplen;
892:
893: break;
1.2 itojun 894: default: /* unknown option */
895: if (hbhlen < IP6OPT_MINLEN) {
896: ip6stat.ip6s_toosmall++;
897: goto bad;
898: }
899: if ((optlen = ip6_unknown_opt(opt, m,
900: sizeof(struct ip6_hdr) +
901: sizeof(struct ip6_hbh) +
902: opt - opthead)) == -1)
903: return(-1);
904: optlen += 2;
905: break;
906: }
907: }
908:
909: return(0);
910:
911: bad:
912: m_freem(m);
913: return(-1);
914: }
915:
916: /*
917: * Unknown option processing.
918: * The third argument `off' is the offset from the IPv6 header to the option,
919: * which is necessary if the IPv6 header the and option header and IPv6 header
920: * is not continuous in order to return an ICMPv6 error.
921: */
922: int
923: ip6_unknown_opt(optp, m, off)
924: u_int8_t *optp;
925: struct mbuf *m;
926: int off;
927: {
928: struct ip6_hdr *ip6;
929:
930: switch(IP6OPT_TYPE(*optp)) {
931: case IP6OPT_TYPE_SKIP: /* ignore the option */
932: return((int)*(optp + 1));
933: case IP6OPT_TYPE_DISCARD: /* silently discard */
934: m_freem(m);
935: return(-1);
936: case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
937: ip6stat.ip6s_badoptions++;
938: icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
939: return(-1);
940: case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
941: ip6stat.ip6s_badoptions++;
942: ip6 = mtod(m, struct ip6_hdr *);
943: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
944: (m->m_flags & (M_BCAST|M_MCAST)))
945: m_freem(m);
946: else
947: icmp6_error(m, ICMP6_PARAM_PROB,
948: ICMP6_PARAMPROB_OPTION, off);
949: return(-1);
950: }
951:
952: m_freem(m); /* XXX: NOTREACHED */
953: return(-1);
954: }
955:
956: /*
1.8.2.1 bouyer 957: * Create the "control" list for this pcb.
958: *
959: * The routine will be called from upper layer handlers like tcp6_input().
960: * Thus the routine assumes that the caller (tcp6_input) have already
961: * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
962: * very first mbuf on the mbuf chain.
963: * We may want to add some infinite loop prevention or sanity checks for safety.
964: * (This applies only when you are using KAME mbuf chain restriction, i.e.
965: * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2 itojun 966: */
967: void
968: ip6_savecontrol(in6p, mp, ip6, m)
969: register struct in6pcb *in6p;
970: register struct mbuf **mp;
971: register struct ip6_hdr *ip6;
972: register struct mbuf *m;
973: {
974: struct proc *p = curproc; /* XXX */
1.8.2.1 bouyer 975: int privileged;
976:
977: privileged = 0;
978: if (p && !suser(p->p_ucred, &p->p_acflag))
979: privileged++;
1.2 itojun 980:
1.8.2.1 bouyer 981: #ifdef SO_TIMESTAMP
1.2 itojun 982: if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
983: struct timeval tv;
984:
985: microtime(&tv);
986: *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
987: SCM_TIMESTAMP, SOL_SOCKET);
988: if (*mp)
989: mp = &(*mp)->m_next;
990: }
1.8.2.1 bouyer 991: #endif
1.2 itojun 992: if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
993: *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
994: sizeof(struct in6_addr), IPV6_RECVDSTADDR,
995: IPPROTO_IPV6);
996: if (*mp)
997: mp = &(*mp)->m_next;
998: }
999:
1000: #ifdef noyet
1001: /* options were tossed above */
1002: if (in6p->in6p_flags & IN6P_RECVOPTS)
1003: /* broken */
1004: /* ip6_srcroute doesn't do what we want here, need to fix */
1005: if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
1006: /* broken */
1007: #endif
1008:
1009: /* RFC 2292 sec. 5 */
1010: if (in6p->in6p_flags & IN6P_PKTINFO) {
1011: struct in6_pktinfo pi6;
1012: bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1013: if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
1014: pi6.ipi6_addr.s6_addr16[1] = 0;
1015: pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
1016: ? m->m_pkthdr.rcvif->if_index
1017: : 0;
1018: *mp = sbcreatecontrol((caddr_t) &pi6,
1019: sizeof(struct in6_pktinfo), IPV6_PKTINFO,
1020: IPPROTO_IPV6);
1021: if (*mp)
1022: mp = &(*mp)->m_next;
1023: }
1024: if (in6p->in6p_flags & IN6P_HOPLIMIT) {
1025: int hlim = ip6->ip6_hlim & 0xff;
1026: *mp = sbcreatecontrol((caddr_t) &hlim,
1027: sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
1028: if (*mp)
1029: mp = &(*mp)->m_next;
1030: }
1031: /* IN6P_NEXTHOP - for outgoing packet only */
1032:
1033: /*
1034: * IPV6_HOPOPTS socket option. We require super-user privilege
1035: * for the option, but it might be too strict, since there might
1036: * be some hop-by-hop options which can be returned to normal user.
1037: * See RFC 2292 section 6.
1038: */
1.8.2.1 bouyer 1039: if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2 itojun 1040: /*
1041: * Check if a hop-by-hop options header is contatined in the
1042: * received packet, and if so, store the options as ancillary
1043: * data. Note that a hop-by-hop options header must be
1044: * just after the IPv6 header, which fact is assured through
1045: * the IPv6 input processing.
1046: */
1047: struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1048: if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.8.2.1 bouyer 1049: struct ip6_hbh *hbh;
1050: int hbhlen;
1051:
1052: #ifndef PULLDOWN_TEST
1053: hbh = (struct ip6_hbh *)(ip6 + 1);
1054: hbhlen = (hbh->ip6h_len + 1) << 3;
1055: #else
1056: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1057: sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
1058: if (hbh == NULL) {
1059: ip6stat.ip6s_tooshort++;
1060: return;
1061: }
1062: hbhlen = (hbh->ip6h_len + 1) << 3;
1063: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1064: sizeof(struct ip6_hdr), hbhlen);
1065: if (hbh == NULL) {
1066: ip6stat.ip6s_tooshort++;
1067: return;
1068: }
1069: #endif
1.2 itojun 1070:
1071: /*
1072: * XXX: We copy whole the header even if a jumbo
1073: * payload option is included, which option is to
1074: * be removed before returning in the RFC 2292.
1075: * But it's too painful operation...
1076: */
1.8.2.1 bouyer 1077: *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1.2 itojun 1078: IPV6_HOPOPTS, IPPROTO_IPV6);
1079: if (*mp)
1080: mp = &(*mp)->m_next;
1081: }
1082: }
1083:
1084: /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1085: if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1086: struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1087: int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
1088:
1089: /*
1090: * Search for destination options headers or routing
1091: * header(s) through the header chain, and stores each
1092: * header as ancillary data.
1093: * Note that the order of the headers remains in
1094: * the chain of ancillary data.
1095: */
1096: while(1) { /* is explicit loop prevention necessary? */
1.8.2.1 bouyer 1097: struct ip6_ext *ip6e;
1098: int elen;
1099:
1100: #ifndef PULLDOWN_TEST
1101: ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1102: if (nxt == IPPROTO_AH)
1103: elen = (ip6e->ip6e_len + 2) << 2;
1104: else
1105: elen = (ip6e->ip6e_len + 1) << 3;
1106: #else
1107: IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
1108: sizeof(struct ip6_ext));
1109: if (ip6e == NULL) {
1110: ip6stat.ip6s_tooshort++;
1111: return;
1112: }
1113: if (nxt == IPPROTO_AH)
1114: elen = (ip6e->ip6e_len + 2) << 2;
1115: else
1116: elen = (ip6e->ip6e_len + 1) << 3;
1117: IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
1118: if (ip6e == NULL) {
1119: ip6stat.ip6s_tooshort++;
1120: return;
1121: }
1122: #endif
1.2 itojun 1123:
1124: switch(nxt) {
1125: case IPPROTO_DSTOPTS:
1126: if (!in6p->in6p_flags & IN6P_DSTOPTS)
1127: break;
1128:
1129: /*
1130: * We also require super-user privilege for
1131: * the option.
1132: * See the comments on IN6_HOPOPTS.
1133: */
1.8.2.1 bouyer 1134: if (!privileged)
1.2 itojun 1135: break;
1136:
1.8.2.1 bouyer 1137: *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1.2 itojun 1138: IPV6_DSTOPTS,
1139: IPPROTO_IPV6);
1140: if (*mp)
1141: mp = &(*mp)->m_next;
1142: break;
1143:
1144: case IPPROTO_ROUTING:
1145: if (!in6p->in6p_flags & IN6P_RTHDR)
1146: break;
1147:
1.8.2.1 bouyer 1148: *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1.2 itojun 1149: IPV6_RTHDR,
1150: IPPROTO_IPV6);
1151: if (*mp)
1152: mp = &(*mp)->m_next;
1153: break;
1154:
1155: case IPPROTO_UDP:
1156: case IPPROTO_TCP:
1157: case IPPROTO_ICMPV6:
1158: default:
1159: /*
1160: * stop search if we encounter an upper
1161: * layer protocol headers.
1162: */
1163: goto loopend;
1164:
1165: case IPPROTO_HOPOPTS:
1166: case IPPROTO_AH: /* is it possible? */
1167: break;
1168: }
1169:
1170: /* proceed with the next header. */
1.8.2.1 bouyer 1171: off += elen;
1.2 itojun 1172: nxt = ip6e->ip6e_nxt;
1173: }
1174: loopend:
1175: }
1.8.2.1 bouyer 1176: if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2 itojun 1177: /* to be done */
1178: }
1.8.2.1 bouyer 1179: if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1.2 itojun 1180: /* to be done */
1181: }
1182: /* IN6P_RTHDR - to be done */
1183:
1184: }
1185:
1186: /*
1187: * Get pointer to the previous header followed by the header
1188: * currently processed.
1189: * XXX: This function supposes that
1190: * M includes all headers,
1191: * the next header field and the header length field of each header
1192: * are valid, and
1193: * the sum of each header length equals to OFF.
1194: * Because of these assumptions, this function must be called very
1195: * carefully. Moreover, it will not be used in the near future when
1196: * we develop `neater' mechanism to process extension headers.
1197: */
1198: char *
1199: ip6_get_prevhdr(m, off)
1200: struct mbuf *m;
1201: int off;
1202: {
1203: struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1204:
1205: if (off == sizeof(struct ip6_hdr))
1206: return(&ip6->ip6_nxt);
1207: else {
1208: int len, nxt;
1209: struct ip6_ext *ip6e = NULL;
1210:
1211: nxt = ip6->ip6_nxt;
1212: len = sizeof(struct ip6_hdr);
1213: while (len < off) {
1214: ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1215:
1216: switch(nxt) {
1217: case IPPROTO_FRAGMENT:
1218: len += sizeof(struct ip6_frag);
1219: break;
1220: case IPPROTO_AH:
1221: len += (ip6e->ip6e_len + 2) << 2;
1222: break;
1223: default:
1224: len += (ip6e->ip6e_len + 1) << 3;
1225: break;
1226: }
1227: nxt = ip6e->ip6e_nxt;
1228: }
1229: if (ip6e)
1230: return(&ip6e->ip6e_nxt);
1231: else
1232: return NULL;
1233: }
1234: }
1235:
1236: /*
1.8.2.1 bouyer 1237: * get next header offset. m will be retained.
1238: */
1239: int
1240: ip6_nexthdr(m, off, proto, nxtp)
1241: struct mbuf *m;
1242: int off;
1243: int proto;
1244: int *nxtp;
1245: {
1246: struct ip6_hdr ip6;
1247: struct ip6_ext ip6e;
1248: struct ip6_frag fh;
1249:
1250: /* just in case */
1251: if (m == NULL)
1252: panic("ip6_nexthdr: m == NULL");
1253: if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1254: return -1;
1255:
1256: switch (proto) {
1257: case IPPROTO_IPV6:
1258: if (m->m_pkthdr.len < off + sizeof(ip6))
1259: return -1;
1260: m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1261: if (nxtp)
1262: *nxtp = ip6.ip6_nxt;
1263: off += sizeof(ip6);
1264: return off;
1265:
1266: case IPPROTO_FRAGMENT:
1267: /*
1268: * terminate parsing if it is not the first fragment,
1269: * it does not make sense to parse through it.
1270: */
1271: if (m->m_pkthdr.len < off + sizeof(fh))
1272: return -1;
1273: m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1274: if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
1275: return -1;
1276: if (nxtp)
1277: *nxtp = fh.ip6f_nxt;
1278: off += sizeof(struct ip6_frag);
1279: return off;
1280:
1281: case IPPROTO_AH:
1282: if (m->m_pkthdr.len < off + sizeof(ip6e))
1283: return -1;
1284: m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1285: if (nxtp)
1286: *nxtp = ip6e.ip6e_nxt;
1287: off += (ip6e.ip6e_len + 2) << 2;
1288: return off;
1289:
1290: case IPPROTO_HOPOPTS:
1291: case IPPROTO_ROUTING:
1292: case IPPROTO_DSTOPTS:
1293: if (m->m_pkthdr.len < off + sizeof(ip6e))
1294: return -1;
1295: m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1296: if (nxtp)
1297: *nxtp = ip6e.ip6e_nxt;
1298: off += (ip6e.ip6e_len + 1) << 3;
1299: return off;
1300:
1301: case IPPROTO_NONE:
1302: case IPPROTO_ESP:
1303: case IPPROTO_IPCOMP:
1304: /* give up */
1305: return -1;
1306:
1307: default:
1308: return -1;
1309: }
1310:
1311: return -1;
1312: }
1313:
1314: /*
1315: * get offset for the last header in the chain. m will be kept untainted.
1316: */
1317: int
1318: ip6_lasthdr(m, off, proto, nxtp)
1319: struct mbuf *m;
1320: int off;
1321: int proto;
1322: int *nxtp;
1323: {
1324: int newoff;
1325: int nxt;
1326:
1327: if (!nxtp) {
1328: nxt = -1;
1329: nxtp = &nxt;
1330: }
1331: while (1) {
1332: newoff = ip6_nexthdr(m, off, proto, nxtp);
1333: if (newoff < 0)
1334: return off;
1335: else if (newoff < off)
1336: return -1; /* invalid */
1337: else if (newoff == off)
1338: return newoff;
1339:
1340: off = newoff;
1341: proto = *nxtp;
1342: }
1343: }
1344:
1345: /*
1.2 itojun 1346: * System control for IP6
1347: */
1348:
1349: u_char inet6ctlerrmap[PRC_NCMDS] = {
1350: 0, 0, 0, 0,
1351: 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1352: EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1353: EMSGSIZE, EHOSTUNREACH, 0, 0,
1354: 0, 0, 0, 0,
1355: ENOPROTOOPT
1356: };
1357:
1.8.2.1 bouyer 1358: #include <uvm/uvm_extern.h>
1.2 itojun 1359: #include <sys/sysctl.h>
1360:
1361: int
1362: ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1363: int *name;
1364: u_int namelen;
1365: void *oldp;
1366: size_t *oldlenp;
1367: void *newp;
1368: size_t newlen;
1369: {
1.8.2.1 bouyer 1370: int old, error;
1371:
1.2 itojun 1372: /* All sysctl names at this level are terminal. */
1373: if (namelen != 1)
1374: return ENOTDIR;
1375:
1376: switch (name[0]) {
1377:
1378: case IPV6CTL_FORWARDING:
1379: return sysctl_int(oldp, oldlenp, newp, newlen,
1380: &ip6_forwarding);
1381: case IPV6CTL_SENDREDIRECTS:
1382: return sysctl_int(oldp, oldlenp, newp, newlen,
1383: &ip6_sendredirects);
1384: case IPV6CTL_DEFHLIM:
1385: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
1386: case IPV6CTL_MAXFRAGPACKETS:
1387: return sysctl_int(oldp, oldlenp, newp, newlen,
1388: &ip6_maxfragpackets);
1389: case IPV6CTL_ACCEPT_RTADV:
1390: return sysctl_int(oldp, oldlenp, newp, newlen,
1391: &ip6_accept_rtadv);
1392: case IPV6CTL_KEEPFAITH:
1393: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
1394: case IPV6CTL_LOG_INTERVAL:
1395: return sysctl_int(oldp, oldlenp, newp, newlen,
1396: &ip6_log_interval);
1397: case IPV6CTL_HDRNESTLIMIT:
1398: return sysctl_int(oldp, oldlenp, newp, newlen,
1399: &ip6_hdrnestlimit);
1400: case IPV6CTL_DAD_COUNT:
1401: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
1402: case IPV6CTL_AUTO_FLOWLABEL:
1403: return sysctl_int(oldp, oldlenp, newp, newlen,
1404: &ip6_auto_flowlabel);
1405: case IPV6CTL_DEFMCASTHLIM:
1406: return sysctl_int(oldp, oldlenp, newp, newlen,
1407: &ip6_defmcasthlim);
1408: case IPV6CTL_GIF_HLIM:
1409: return sysctl_int(oldp, oldlenp, newp, newlen,
1410: &ip6_gif_hlim);
1411: case IPV6CTL_KAME_VERSION:
1412: return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1.8.2.1 bouyer 1413: case IPV6CTL_USE_DEPRECATED:
1414: return sysctl_int(oldp, oldlenp, newp, newlen,
1415: &ip6_use_deprecated);
1416: case IPV6CTL_RR_PRUNE:
1417: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
1418: #ifndef INET6_BINDV6ONLY
1419: case IPV6CTL_BINDV6ONLY:
1420: return sysctl_int(oldp, oldlenp, newp, newlen,
1421: &ip6_bindv6only);
1422: #endif
1423: case IPV6CTL_ANONPORTMIN:
1424: old = ip6_anonportmin;
1425: error = sysctl_int(oldp, oldlenp, newp, newlen,
1426: &ip6_anonportmin);
1427: if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmin < 0 ||
1428: ip6_anonportmin > 65535
1429: #ifndef IPNOPRIVPORTS
1430: || ip6_anonportmin < IPV6PORT_RESERVED
1431: #endif
1432: ) {
1433: ip6_anonportmin = old;
1434: return (EINVAL);
1435: }
1436: return (error);
1437: case IPV6CTL_ANONPORTMAX:
1438: old = ip6_anonportmax;
1439: error = sysctl_int(oldp, oldlenp, newp, newlen,
1440: &ip6_anonportmax);
1441: if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmax < 0 ||
1442: ip6_anonportmax > 65535
1443: #ifndef IPNOPRIVPORTS
1444: || ip6_anonportmax < IPV6PORT_RESERVED
1445: #endif
1446: ) {
1447: ip6_anonportmax = old;
1448: return (EINVAL);
1449: }
1450: return (error);
1451: #ifndef IPNOPRIVPORTS
1452: case IPV6CTL_LOWPORTMIN:
1453: old = ip6_lowportmin;
1454: error = sysctl_int(oldp, oldlenp, newp, newlen,
1455: &ip6_lowportmin);
1456: if (ip6_lowportmin >= ip6_lowportmax ||
1457: ip6_lowportmin > IPV6PORT_RESERVEDMAX ||
1458: ip6_lowportmin < IPV6PORT_RESERVEDMIN) {
1459: ip6_lowportmin = old;
1460: return (EINVAL);
1461: }
1462: return (error);
1463: case IPV6CTL_LOWPORTMAX:
1464: old = ip6_lowportmax;
1465: error = sysctl_int(oldp, oldlenp, newp, newlen,
1466: &ip6_lowportmax);
1467: if (ip6_lowportmin >= ip6_lowportmax ||
1468: ip6_lowportmax > IPV6PORT_RESERVEDMAX ||
1469: ip6_lowportmax < IPV6PORT_RESERVEDMIN) {
1470: ip6_lowportmax = old;
1471: return (EINVAL);
1472: }
1473: return (error);
1474: #endif
1.2 itojun 1475: default:
1476: return EOPNOTSUPP;
1477: }
1478: /* NOTREACHED */
1479: }
CVSweb <webmaster@jp.NetBSD.org>