1 /* $FreeBSD: releng/5.2/sys/netinet6/ip6_input.c 122921 2003-11-20 19:47:31Z andre $ */
2 /* $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
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.
19 *
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_ip6fw.h"
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
72 #include "opt_pfil_hooks.h"
73 #include "opt_random_ip_id.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/proc.h>
80 #include <sys/domain.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/errno.h>
85 #include <sys/time.h>
86 #include <sys/kernel.h>
87 #include <sys/syslog.h>
88
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/if_dl.h>
92 #include <net/route.h>
93 #include <net/netisr.h>
94 #ifdef PFIL_HOOKS
95 #include <net/pfil.h>
96 #endif
97
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #ifdef INET
101 #include <netinet/ip.h>
102 #include <netinet/ip_icmp.h>
103 #endif /* INET */
104 #include <netinet/ip6.h>
105 #include <netinet6/in6_var.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet/in_pcb.h>
108 #include <netinet/icmp6.h>
109 #include <netinet6/scope6_var.h>
110 #include <netinet6/in6_ifattach.h>
111 #include <netinet6/nd6.h>
112 #include <netinet6/in6_prefix.h>
113
114 #ifdef IPSEC
115 #include <netinet6/ipsec.h>
116 #ifdef INET6
117 #include <netinet6/ipsec6.h>
118 #endif
119 #endif
120
121 #ifdef FAST_IPSEC
122 #include <netipsec/ipsec.h>
123 #include <netipsec/ipsec6.h>
124 #define IPSEC
125 #endif /* FAST_IPSEC */
126
127 #include <netinet6/ip6_fw.h>
128
129 #include <netinet6/ip6protosw.h>
130
131 #include <net/net_osdep.h>
132
133 extern struct domain inet6domain;
134
135 u_char ip6_protox[IPPROTO_MAX];
136 static struct ifqueue ip6intrq;
137 static int ip6qmaxlen = IFQ_MAXLEN;
138 struct in6_ifaddr *in6_ifaddr;
139
140 extern struct callout in6_tmpaddrtimer_ch;
141
142 int ip6_forward_srcrt; /* XXX */
143 int ip6_sourcecheck; /* XXX */
144 int ip6_sourcecheck_interval; /* XXX */
145
146 int ip6_ours_check_algorithm;
147
148 #ifdef PFIL_HOOKS
149 struct pfil_head inet6_pfil_hook;
150 #endif
151
152 /* firewall hooks */
153 ip6_fw_chk_t *ip6_fw_chk_ptr;
154 ip6_fw_ctl_t *ip6_fw_ctl_ptr;
155 int ip6_fw_enable = 1;
156
157 struct ip6stat ip6stat;
158
159 static void ip6_init2 __P((void *));
160 static struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
161 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
162 #ifdef PULLDOWN_TEST
163 static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
164 #endif
165
166 /*
167 * IP6 initialization: fill in IP6 protocol switch table.
168 * All protocols not implemented in kernel go to raw IP6 protocol handler.
169 */
170 void
171 ip6_init()
172 {
173 struct ip6protosw *pr;
174 int i;
175
176 #ifdef DIAGNOSTIC
177 if (sizeof(struct protosw) != sizeof(struct ip6protosw))
178 panic("sizeof(protosw) != sizeof(ip6protosw)");
179 #endif
180 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
181 if (pr == 0)
182 panic("ip6_init");
183 for (i = 0; i < IPPROTO_MAX; i++)
184 ip6_protox[i] = pr - inet6sw;
185 for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
186 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
187 if (pr->pr_domain->dom_family == PF_INET6 &&
188 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
189 ip6_protox[pr->pr_protocol] = pr - inet6sw;
190 #ifdef PFIL_HOOKS
191 inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
192 inet6_pfil_hook.ph_af = AF_INET6;
193 if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
194 printf("%s: WARNING: unable to register pfil hook, "
195 "error %d\n", __func__, i);
196 #endif /* PFIL_HOOKS */
197 ip6intrq.ifq_maxlen = ip6qmaxlen;
198 mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
199 netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
200 scope6_init();
201 addrsel_policy_init();
202 nd6_init();
203 frag6_init();
204 #ifndef RANDOM_IP_ID
205 ip6_flow_seq = arc4random();
206 #endif
207 ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
208 }
209
210 static void
211 ip6_init2(dummy)
212 void *dummy;
213 {
214
215 /* nd6_timer_init */
216 callout_init(&nd6_timer_ch, 0);
217 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
218
219 /* router renumbering prefix list maintenance */
220 callout_init(&in6_rr_timer_ch, 0);
221 callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
222
223 /* timer for regeneranation of temporary addresses randomize ID */
224 callout_init(&in6_tmpaddrtimer_ch, 0);
225 callout_reset(&in6_tmpaddrtimer_ch,
226 (ip6_temp_preferred_lifetime - ip6_desync_factor -
227 ip6_temp_regen_advance) * hz,
228 in6_tmpaddrtimer, NULL);
229 }
230
231 /* cheat */
232 /* This must be after route_init(), which is now SI_ORDER_THIRD */
233 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
234
235 extern struct route_in6 ip6_forward_rt;
236
237 void
238 ip6_input(m)
239 struct mbuf *m;
240 {
241 struct ip6_hdr *ip6;
242 int off = sizeof(struct ip6_hdr), nest;
243 u_int32_t plen;
244 u_int32_t rtalert = ~0;
245 int nxt, ours = 0;
246 struct ifnet *deliverifp = NULL;
247 #ifdef PFIL_HOOKS
248 struct in6_addr odst;
249 #endif
250 int srcrt = 0;
251
252 GIANT_REQUIRED; /* XXX for now */
253 #ifdef IPSEC
254 /*
255 * should the inner packet be considered authentic?
256 * see comment in ah4_input().
257 */
258 if (m) {
259 m->m_flags &= ~M_AUTHIPHDR;
260 m->m_flags &= ~M_AUTHIPDGM;
261 }
262 #endif
263
264 /*
265 * make sure we don't have onion peering information into m_tag.
266 */
267 ip6_delaux(m);
268
269 /*
270 * mbuf statistics
271 */
272 if (m->m_flags & M_EXT) {
273 if (m->m_next)
274 ip6stat.ip6s_mext2m++;
275 else
276 ip6stat.ip6s_mext1++;
277 } else {
278 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
279 if (m->m_next) {
280 if (m->m_flags & M_LOOP) {
281 ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
282 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
283 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
284 else
285 ip6stat.ip6s_m2m[0]++;
286 } else
287 ip6stat.ip6s_m1++;
288 #undef M2MMAX
289 }
290
291 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
292 ip6stat.ip6s_total++;
293
294 #ifndef PULLDOWN_TEST
295 /*
296 * L2 bridge code and some other code can return mbuf chain
297 * that does not conform to KAME requirement. too bad.
298 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
299 */
300 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
301 struct mbuf *n;
302
303 MGETHDR(n, M_DONTWAIT, MT_HEADER);
304 if (n)
305 M_MOVE_PKTHDR(n, m);
306 if (n && n->m_pkthdr.len > MHLEN) {
307 MCLGET(n, M_DONTWAIT);
308 if ((n->m_flags & M_EXT) == 0) {
309 m_freem(n);
310 n = NULL;
311 }
312 }
313 if (n == NULL) {
314 m_freem(m);
315 return; /* ENOBUFS */
316 }
317
318 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
319 n->m_len = n->m_pkthdr.len;
320 m_freem(m);
321 m = n;
322 }
323 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
324 #endif
325
326 if (m->m_len < sizeof(struct ip6_hdr)) {
327 struct ifnet *inifp;
328 inifp = m->m_pkthdr.rcvif;
329 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
330 ip6stat.ip6s_toosmall++;
331 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
332 return;
333 }
334 }
335
336 ip6 = mtod(m, struct ip6_hdr *);
337
338 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
339 ip6stat.ip6s_badvers++;
340 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
341 goto bad;
342 }
343
344 #ifdef PFIL_HOOKS
345 /*
346 * Run through list of hooks for input packets.
347 *
348 * NB: Beware of the destination address changing
349 * (e.g. by NAT rewriting). When this happens,
350 * tell ip6_forward to do the right thing.
351 */
352 odst = ip6->ip6_dst;
353 if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN))
354 return;
355 if (m == NULL) /* consumed by filter */
356 return;
357 ip6 = mtod(m, struct ip6_hdr *);
358 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
359 #endif /* PFIL_HOOKS */
360
361 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
362
363 /*
364 * Check with the firewall...
365 */
366 if (ip6_fw_enable && ip6_fw_chk_ptr) {
367 u_short port = 0;
368 /* If ipfw says divert, we have to just drop packet */
369 /* use port as a dummy argument */
370 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
371 m_freem(m);
372 m = NULL;
373 }
374 if (!m)
375 return;
376 }
377
378 /*
379 * Check against address spoofing/corruption.
380 */
381 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
382 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
383 /*
384 * XXX: "badscope" is not very suitable for a multicast source.
385 */
386 ip6stat.ip6s_badscope++;
387 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
388 goto bad;
389 }
390 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
391 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
392 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
393 ip6stat.ip6s_badscope++;
394 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
395 goto bad;
396 }
397
398 /*
399 * The following check is not documented in specs. A malicious
400 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
401 * and bypass security checks (act as if it was from 127.0.0.1 by using
402 * IPv6 src ::ffff:127.0.0.1). Be cautious.
403 *
404 * This check chokes if we are in an SIIT cloud. As none of BSDs
405 * support IPv4-less kernel compilation, we cannot support SIIT
406 * environment at all. So, it makes more sense for us to reject any
407 * malicious packets for non-SIIT environment, than try to do a
408 * partial support for SIIT environment.
409 */
410 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
411 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
412 ip6stat.ip6s_badscope++;
413 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
414 goto bad;
415 }
416 #if 0
417 /*
418 * Reject packets with IPv4 compatible addresses (auto tunnel).
419 *
420 * The code forbids auto tunnel relay case in RFC1933 (the check is
421 * stronger than RFC1933). We may want to re-enable it if mech-xx
422 * is revised to forbid relaying case.
423 */
424 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
425 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
426 ip6stat.ip6s_badscope++;
427 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
428 goto bad;
429 }
430 #endif
431
432 /* drop packets if interface ID portion is already filled */
433 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
434 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
435 ip6->ip6_src.s6_addr16[1]) {
436 ip6stat.ip6s_badscope++;
437 goto bad;
438 }
439 if ((IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
440 IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) &&
441 ip6->ip6_dst.s6_addr16[1]) {
442 ip6stat.ip6s_badscope++;
443 goto bad;
444 }
445 }
446
447 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
448 ip6->ip6_src.s6_addr16[1]
449 = htons(m->m_pkthdr.rcvif->if_index);
450 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
451 ip6->ip6_dst.s6_addr16[1]
452 = htons(m->m_pkthdr.rcvif->if_index);
453
454 /*
455 * Multicast check
456 */
457 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
458 struct in6_multi *in6m = 0;
459
460 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
461 /*
462 * See if we belong to the destination multicast group on the
463 * arrival interface.
464 */
465 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
466 if (in6m)
467 ours = 1;
468 else if (!ip6_mrouter) {
469 ip6stat.ip6s_notmember++;
470 ip6stat.ip6s_cantforward++;
471 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
472 goto bad;
473 }
474 deliverifp = m->m_pkthdr.rcvif;
475 goto hbhcheck;
476 }
477
478 /*
479 * Unicast check
480 */
481 if (ip6_forward_rt.ro_rt != NULL &&
482 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
483 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
484 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
485 ip6stat.ip6s_forward_cachehit++;
486 else {
487 struct sockaddr_in6 *dst6;
488
489 if (ip6_forward_rt.ro_rt) {
490 /* route is down or destination is different */
491 ip6stat.ip6s_forward_cachemiss++;
492 RTFREE(ip6_forward_rt.ro_rt);
493 ip6_forward_rt.ro_rt = 0;
494 }
495
496 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
497 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
498 dst6->sin6_len = sizeof(struct sockaddr_in6);
499 dst6->sin6_family = AF_INET6;
500 dst6->sin6_addr = ip6->ip6_dst;
501
502 rtalloc((struct route *)&ip6_forward_rt);
503 }
504
505 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
506
507 /*
508 * Accept the packet if the forwarding interface to the destination
509 * according to the routing table is the loopback interface,
510 * unless the associated route has a gateway.
511 * Note that this approach causes to accept a packet if there is a
512 * route to the loopback interface for the destination of the packet.
513 * But we think it's even useful in some situations, e.g. when using
514 * a special daemon which wants to intercept the packet.
515 *
516 * XXX: some OSes automatically make a cloned route for the destination
517 * of an outgoing packet. If the outgoing interface of the packet
518 * is a loopback one, the kernel would consider the packet to be
519 * accepted, even if we have no such address assinged on the interface.
520 * We check the cloned flag of the route entry to reject such cases,
521 * assuming that route entries for our own addresses are not made by
522 * cloning (it should be true because in6_addloop explicitly installs
523 * the host route). However, we might have to do an explicit check
524 * while it would be less efficient. Or, should we rather install a
525 * reject route for such a case?
526 */
527 if (ip6_forward_rt.ro_rt &&
528 (ip6_forward_rt.ro_rt->rt_flags &
529 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
530 #ifdef RTF_WASCLONED
531 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
532 #endif
533 #ifdef RTF_CLONED
534 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
535 #endif
536 #if 0
537 /*
538 * The check below is redundant since the comparison of
539 * the destination and the key of the rtentry has
540 * already done through looking up the routing table.
541 */
542 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
543 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
544 #endif
545 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
546 struct in6_ifaddr *ia6 =
547 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
548
549 /*
550 * record address information into m_tag.
551 */
552 (void)ip6_setdstifaddr(m, ia6);
553
554 /*
555 * packets to a tentative, duplicated, or somehow invalid
556 * address must not be accepted.
557 */
558 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
559 /* this address is ready */
560 ours = 1;
561 deliverifp = ia6->ia_ifp; /* correct? */
562 /* Count the packet in the ip address stats */
563 ia6->ia_ifa.if_ipackets++;
564 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
565 goto hbhcheck;
566 } else {
567 /* address is not ready, so discard the packet. */
568 nd6log((LOG_INFO,
569 "ip6_input: packet to an unready address %s->%s\n",
570 ip6_sprintf(&ip6->ip6_src),
571 ip6_sprintf(&ip6->ip6_dst)));
572
573 goto bad;
574 }
575 }
576
577 /*
578 * FAITH (Firewall Aided Internet Translator)
579 */
580 if (ip6_keepfaith) {
581 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
582 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
583 /* XXX do we need more sanity checks? */
584 ours = 1;
585 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
586 goto hbhcheck;
587 }
588 }
589
590 /*
591 * Now there is no reason to process the packet if it's not our own
592 * and we're not a router.
593 */
594 if (!ip6_forwarding) {
595 ip6stat.ip6s_cantforward++;
596 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
597 goto bad;
598 }
599
600 hbhcheck:
601 /*
602 * record address information into m_tag, if we don't have one yet.
603 * note that we are unable to record it, if the address is not listed
604 * as our interface address (e.g. multicast addresses, addresses
605 * within FAITH prefixes and such).
606 */
607 if (deliverifp && !ip6_getdstifaddr(m)) {
608 struct in6_ifaddr *ia6;
609
610 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
611 if (ia6) {
612 if (!ip6_setdstifaddr(m, ia6)) {
613 /*
614 * XXX maybe we should drop the packet here,
615 * as we could not provide enough information
616 * to the upper layers.
617 */
618 }
619 }
620 }
621
622 /*
623 * Process Hop-by-Hop options header if it's contained.
624 * m may be modified in ip6_hopopts_input().
625 * If a JumboPayload option is included, plen will also be modified.
626 */
627 plen = (u_int32_t)ntohs(ip6->ip6_plen);
628 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
629 struct ip6_hbh *hbh;
630
631 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
632 #if 0 /*touches NULL pointer*/
633 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
634 #endif
635 return; /* m have already been freed */
636 }
637
638 /* adjust pointer */
639 ip6 = mtod(m, struct ip6_hdr *);
640
641 /*
642 * if the payload length field is 0 and the next header field
643 * indicates Hop-by-Hop Options header, then a Jumbo Payload
644 * option MUST be included.
645 */
646 if (ip6->ip6_plen == 0 && plen == 0) {
647 /*
648 * Note that if a valid jumbo payload option is
649 * contained, ip6_hopopts_input() must set a valid
650 * (non-zero) payload length to the variable plen.
651 */
652 ip6stat.ip6s_badoptions++;
653 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
654 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
655 icmp6_error(m, ICMP6_PARAM_PROB,
656 ICMP6_PARAMPROB_HEADER,
657 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
658 return;
659 }
660 #ifndef PULLDOWN_TEST
661 /* ip6_hopopts_input() ensures that mbuf is contiguous */
662 hbh = (struct ip6_hbh *)(ip6 + 1);
663 #else
664 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
665 sizeof(struct ip6_hbh));
666 if (hbh == NULL) {
667 ip6stat.ip6s_tooshort++;
668 return;
669 }
670 #endif
671 nxt = hbh->ip6h_nxt;
672
673 /*
674 * accept the packet if a router alert option is included
675 * and we act as an IPv6 router.
676 */
677 if (rtalert != ~0 && ip6_forwarding)
678 ours = 1;
679 } else
680 nxt = ip6->ip6_nxt;
681
682 /*
683 * Check that the amount of data in the buffers
684 * is as at least much as the IPv6 header would have us expect.
685 * Trim mbufs if longer than we expect.
686 * Drop packet if shorter than we expect.
687 */
688 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
689 ip6stat.ip6s_tooshort++;
690 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
691 goto bad;
692 }
693 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
694 if (m->m_len == m->m_pkthdr.len) {
695 m->m_len = sizeof(struct ip6_hdr) + plen;
696 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
697 } else
698 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
699 }
700
701 /*
702 * Forward if desirable.
703 */
704 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
705 /*
706 * If we are acting as a multicast router, all
707 * incoming multicast packets are passed to the
708 * kernel-level multicast forwarding function.
709 * The packet is returned (relatively) intact; if
710 * ip6_mforward() returns a non-zero value, the packet
711 * must be discarded, else it may be accepted below.
712 */
713 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
714 ip6stat.ip6s_cantforward++;
715 m_freem(m);
716 return;
717 }
718 if (!ours) {
719 m_freem(m);
720 return;
721 }
722 } else if (!ours) {
723 ip6_forward(m, srcrt);
724 return;
725 }
726
727 ip6 = mtod(m, struct ip6_hdr *);
728
729 /*
730 * Malicious party may be able to use IPv4 mapped addr to confuse
731 * tcp/udp stack and bypass security checks (act as if it was from
732 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
733 *
734 * For SIIT end node behavior, you may want to disable the check.
735 * However, you will become vulnerable to attacks using IPv4 mapped
736 * source.
737 */
738 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
739 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
740 ip6stat.ip6s_badscope++;
741 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
742 goto bad;
743 }
744
745 /*
746 * Tell launch routine the next header
747 */
748 ip6stat.ip6s_delivered++;
749 in6_ifstat_inc(deliverifp, ifs6_in_deliver);
750 nest = 0;
751
752 while (nxt != IPPROTO_DONE) {
753 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
754 ip6stat.ip6s_toomanyhdr++;
755 goto bad;
756 }
757
758 /*
759 * protection against faulty packet - there should be
760 * more sanity checks in header chain processing.
761 */
762 if (m->m_pkthdr.len < off) {
763 ip6stat.ip6s_tooshort++;
764 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
765 goto bad;
766 }
767
768 #ifdef IPSEC
769 /*
770 * enforce IPsec policy checking if we are seeing last header.
771 * note that we do not visit this with protocols with pcb layer
772 * code - like udp/tcp/raw ip.
773 */
774 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
775 ipsec6_in_reject(m, NULL)) {
776 ipsec6stat.in_polvio++;
777 goto bad;
778 }
779 #endif
780 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
781 }
782 return;
783 bad:
784 m_freem(m);
785 }
786
787 /*
788 * set/grab in6_ifaddr correspond to IPv6 destination address.
789 * XXX backward compatibility wrapper
790 */
791 static struct ip6aux *
792 ip6_setdstifaddr(m, ia6)
793 struct mbuf *m;
794 struct in6_ifaddr *ia6;
795 {
796 struct ip6aux *ip6a;
797
798 ip6a = ip6_addaux(m);
799 if (ip6a)
800 ip6a->ip6a_dstia6 = ia6;
801 return ip6a; /* NULL if failed to set */
802 }
803
804 struct in6_ifaddr *
805 ip6_getdstifaddr(m)
806 struct mbuf *m;
807 {
808 struct ip6aux *ip6a;
809
810 ip6a = ip6_findaux(m);
811 if (ip6a)
812 return ip6a->ip6a_dstia6;
813 else
814 return NULL;
815 }
816
817 /*
818 * Hop-by-Hop options header processing. If a valid jumbo payload option is
819 * included, the real payload length will be stored in plenp.
820 */
821 static int
822 ip6_hopopts_input(plenp, rtalertp, mp, offp)
823 u_int32_t *plenp;
824 u_int32_t *rtalertp; /* XXX: should be stored more smart way */
825 struct mbuf **mp;
826 int *offp;
827 {
828 struct mbuf *m = *mp;
829 int off = *offp, hbhlen;
830 struct ip6_hbh *hbh;
831 u_int8_t *opt;
832
833 /* validation of the length of the header */
834 #ifndef PULLDOWN_TEST
835 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
836 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
837 hbhlen = (hbh->ip6h_len + 1) << 3;
838
839 IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
840 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
841 #else
842 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
843 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
844 if (hbh == NULL) {
845 ip6stat.ip6s_tooshort++;
846 return -1;
847 }
848 hbhlen = (hbh->ip6h_len + 1) << 3;
849 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
850 hbhlen);
851 if (hbh == NULL) {
852 ip6stat.ip6s_tooshort++;
853 return -1;
854 }
855 #endif
856 off += hbhlen;
857 hbhlen -= sizeof(struct ip6_hbh);
858 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
859
860 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
861 hbhlen, rtalertp, plenp) < 0)
862 return (-1);
863
864 *offp = off;
865 *mp = m;
866 return (0);
867 }
868
869 /*
870 * Search header for all Hop-by-hop options and process each option.
871 * This function is separate from ip6_hopopts_input() in order to
872 * handle a case where the sending node itself process its hop-by-hop
873 * options header. In such a case, the function is called from ip6_output().
874 *
875 * The function assumes that hbh header is located right after the IPv6 header
876 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
877 * opthead + hbhlen is located in continuous memory region.
878 */
879 int
880 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
881 struct mbuf *m;
882 u_int8_t *opthead;
883 int hbhlen;
884 u_int32_t *rtalertp;
885 u_int32_t *plenp;
886 {
887 struct ip6_hdr *ip6;
888 int optlen = 0;
889 u_int8_t *opt = opthead;
890 u_int16_t rtalert_val;
891 u_int32_t jumboplen;
892 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
893
894 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
895 switch (*opt) {
896 case IP6OPT_PAD1:
897 optlen = 1;
898 break;
899 case IP6OPT_PADN:
900 if (hbhlen < IP6OPT_MINLEN) {
901 ip6stat.ip6s_toosmall++;
902 goto bad;
903 }
904 optlen = *(opt + 1) + 2;
905 break;
906 case IP6OPT_ROUTER_ALERT:
907 /* XXX may need check for alignment */
908 if (hbhlen < IP6OPT_RTALERT_LEN) {
909 ip6stat.ip6s_toosmall++;
910 goto bad;
911 }
912 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
913 /* XXX stat */
914 icmp6_error(m, ICMP6_PARAM_PROB,
915 ICMP6_PARAMPROB_HEADER,
916 erroff + opt + 1 - opthead);
917 return (-1);
918 }
919 optlen = IP6OPT_RTALERT_LEN;
920 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
921 *rtalertp = ntohs(rtalert_val);
922 break;
923 case IP6OPT_JUMBO:
924 /* XXX may need check for alignment */
925 if (hbhlen < IP6OPT_JUMBO_LEN) {
926 ip6stat.ip6s_toosmall++;
927 goto bad;
928 }
929 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
930 /* XXX stat */
931 icmp6_error(m, ICMP6_PARAM_PROB,
932 ICMP6_PARAMPROB_HEADER,
933 erroff + opt + 1 - opthead);
934 return (-1);
935 }
936 optlen = IP6OPT_JUMBO_LEN;
937
938 /*
939 * IPv6 packets that have non 0 payload length
940 * must not contain a jumbo payload option.
941 */
942 ip6 = mtod(m, struct ip6_hdr *);
943 if (ip6->ip6_plen) {
944 ip6stat.ip6s_badoptions++;
945 icmp6_error(m, ICMP6_PARAM_PROB,
946 ICMP6_PARAMPROB_HEADER,
947 erroff + opt - opthead);
948 return (-1);
949 }
950
951 /*
952 * We may see jumbolen in unaligned location, so
953 * we'd need to perform bcopy().
954 */
955 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
956 jumboplen = (u_int32_t)htonl(jumboplen);
957
958 #if 1
959 /*
960 * if there are multiple jumbo payload options,
961 * *plenp will be non-zero and the packet will be
962 * rejected.
963 * the behavior may need some debate in ipngwg -
964 * multiple options does not make sense, however,
965 * there's no explicit mention in specification.
966 */
967 if (*plenp != 0) {
968 ip6stat.ip6s_badoptions++;
969 icmp6_error(m, ICMP6_PARAM_PROB,
970 ICMP6_PARAMPROB_HEADER,
971 erroff + opt + 2 - opthead);
972 return (-1);
973 }
974 #endif
975
976 /*
977 * jumbo payload length must be larger than 65535.
978 */
979 if (jumboplen <= IPV6_MAXPACKET) {
980 ip6stat.ip6s_badoptions++;
981 icmp6_error(m, ICMP6_PARAM_PROB,
982 ICMP6_PARAMPROB_HEADER,
983 erroff + opt + 2 - opthead);
984 return (-1);
985 }
986 *plenp = jumboplen;
987
988 break;
989 default: /* unknown option */
990 if (hbhlen < IP6OPT_MINLEN) {
991 ip6stat.ip6s_toosmall++;
992 goto bad;
993 }
994 optlen = ip6_unknown_opt(opt, m,
995 erroff + opt - opthead);
996 if (optlen == -1)
997 return (-1);
998 optlen += 2;
999 break;
1000 }
1001 }
1002
1003 return (0);
1004
1005 bad:
1006 m_freem(m);
1007 return (-1);
1008 }
1009
1010 /*
1011 * Unknown option processing.
1012 * The third argument `off' is the offset from the IPv6 header to the option,
1013 * which is necessary if the IPv6 header the and option header and IPv6 header
1014 * is not continuous in order to return an ICMPv6 error.
1015 */
1016 int
1017 ip6_unknown_opt(optp, m, off)
1018 u_int8_t *optp;
1019 struct mbuf *m;
1020 int off;
1021 {
1022 struct ip6_hdr *ip6;
1023
1024 switch (IP6OPT_TYPE(*optp)) {
1025 case IP6OPT_TYPE_SKIP: /* ignore the option */
1026 return ((int)*(optp + 1));
1027 case IP6OPT_TYPE_DISCARD: /* silently discard */
1028 m_freem(m);
1029 return (-1);
1030 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1031 ip6stat.ip6s_badoptions++;
1032 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1033 return (-1);
1034 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1035 ip6stat.ip6s_badoptions++;
1036 ip6 = mtod(m, struct ip6_hdr *);
1037 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1038 (m->m_flags & (M_BCAST|M_MCAST)))
1039 m_freem(m);
1040 else
1041 icmp6_error(m, ICMP6_PARAM_PROB,
1042 ICMP6_PARAMPROB_OPTION, off);
1043 return (-1);
1044 }
1045
1046 m_freem(m); /* XXX: NOTREACHED */
1047 return (-1);
1048 }
1049
1050 /*
1051 * Create the "control" list for this pcb.
1052 * The function will not modify mbuf chain at all.
1053 *
1054 * with KAME mbuf chain restriction:
1055 * The routine will be called from upper layer handlers like tcp6_input().
1056 * Thus the routine assumes that the caller (tcp6_input) have already
1057 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1058 * very first mbuf on the mbuf chain.
1059 */
1060 void
1061 ip6_savecontrol(in6p, m, mp)
1062 struct inpcb *in6p;
1063 struct mbuf *m, **mp;
1064 {
1065 #define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1066 struct thread *td = curthread; /* XXX */
1067 int privileged = 0;
1068 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1069
1070 if (td && !suser(td))
1071 privileged++;
1072
1073 #ifdef SO_TIMESTAMP
1074 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
1075 struct timeval tv;
1076
1077 microtime(&tv);
1078 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1079 SCM_TIMESTAMP, SOL_SOCKET);
1080 if (*mp)
1081 mp = &(*mp)->m_next;
1082 }
1083 #endif
1084
1085 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1086 return;
1087
1088 /* RFC 2292 sec. 5 */
1089 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1090 struct in6_pktinfo pi6;
1091
1092 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1093 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1094 pi6.ipi6_ifindex =
1095 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1096
1097 *mp = sbcreatecontrol((caddr_t) &pi6,
1098 sizeof(struct in6_pktinfo),
1099 IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1100 if (*mp)
1101 mp = &(*mp)->m_next;
1102 }
1103
1104 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1105 int hlim = ip6->ip6_hlim & 0xff;
1106
1107 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1108 IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1109 if (*mp)
1110 mp = &(*mp)->m_next;
1111 }
1112
1113 if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1114 u_int32_t flowinfo;
1115 int tclass;
1116
1117 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1118 flowinfo >>= 20;
1119
1120 tclass = flowinfo & 0xff;
1121 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1122 IPV6_TCLASS, IPPROTO_IPV6);
1123 if (*mp)
1124 mp = &(*mp)->m_next;
1125 }
1126
1127 /*
1128 * IPV6_HOPOPTS socket option. We require super-user privilege
1129 * for the option, but it might be too strict, since there might
1130 * be some hop-by-hop options which can be returned to normal user.
1131 * See RFC 2292 section 6.
1132 */
1133 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1134 #ifdef DIAGNOSTIC
1135 if (!privileged)
1136 panic("IN6P_HOPOPTS is set for unprivileged socket");
1137 #endif
1138 /*
1139 * Check if a hop-by-hop options header is contatined in the
1140 * received packet, and if so, store the options as ancillary
1141 * data. Note that a hop-by-hop options header must be
1142 * just after the IPv6 header, which is assured through the
1143 * IPv6 input processing.
1144 */
1145 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1146 struct ip6_hbh *hbh;
1147 int hbhlen = 0;
1148 #ifdef PULLDOWN_TEST
1149 struct mbuf *ext;
1150 #endif
1151
1152 #ifndef PULLDOWN_TEST
1153 hbh = (struct ip6_hbh *)(ip6 + 1);
1154 hbhlen = (hbh->ip6h_len + 1) << 3;
1155 #else
1156 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1157 ip6->ip6_nxt);
1158 if (ext == NULL) {
1159 ip6stat.ip6s_tooshort++;
1160 return;
1161 }
1162 hbh = mtod(ext, struct ip6_hbh *);
1163 hbhlen = (hbh->ip6h_len + 1) << 3;
1164 if (hbhlen != ext->m_len) {
1165 m_freem(ext);
1166 ip6stat.ip6s_tooshort++;
1167 return;
1168 }
1169 #endif
1170
1171 /*
1172 * XXX: We copy the whole header even if a
1173 * jumbo payload option is included, the option which
1174 * is to be removed before returning according to
1175 * RFC2292.
1176 * Note: this constraint is removed in 2292bis.
1177 */
1178 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1179 IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1180 IPPROTO_IPV6);
1181 if (*mp)
1182 mp = &(*mp)->m_next;
1183 #ifdef PULLDOWN_TEST
1184 m_freem(ext);
1185 #endif
1186 }
1187 }
1188
1189 if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1190 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1191
1192 /*
1193 * Search for destination options headers or routing
1194 * header(s) through the header chain, and stores each
1195 * header as ancillary data.
1196 * Note that the order of the headers remains in
1197 * the chain of ancillary data.
1198 */
1199 while (1) { /* is explicit loop prevention necessary? */
1200 struct ip6_ext *ip6e = NULL;
1201 int elen;
1202 #ifdef PULLDOWN_TEST
1203 struct mbuf *ext = NULL;
1204 #endif
1205
1206 /*
1207 * if it is not an extension header, don't try to
1208 * pull it from the chain.
1209 */
1210 switch (nxt) {
1211 case IPPROTO_DSTOPTS:
1212 case IPPROTO_ROUTING:
1213 case IPPROTO_HOPOPTS:
1214 case IPPROTO_AH: /* is it possible? */
1215 break;
1216 default:
1217 goto loopend;
1218 }
1219
1220 #ifndef PULLDOWN_TEST
1221 if (off + sizeof(*ip6e) > m->m_len)
1222 goto loopend;
1223 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1224 if (nxt == IPPROTO_AH)
1225 elen = (ip6e->ip6e_len + 2) << 2;
1226 else
1227 elen = (ip6e->ip6e_len + 1) << 3;
1228 if (off + elen > m->m_len)
1229 goto loopend;
1230 #else
1231 ext = ip6_pullexthdr(m, off, nxt);
1232 if (ext == NULL) {
1233 ip6stat.ip6s_tooshort++;
1234 return;
1235 }
1236 ip6e = mtod(ext, struct ip6_ext *);
1237 if (nxt == IPPROTO_AH)
1238 elen = (ip6e->ip6e_len + 2) << 2;
1239 else
1240 elen = (ip6e->ip6e_len + 1) << 3;
1241 if (elen != ext->m_len) {
1242 m_freem(ext);
1243 ip6stat.ip6s_tooshort++;
1244 return;
1245 }
1246 #endif
1247
1248 switch (nxt) {
1249 case IPPROTO_DSTOPTS:
1250 if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1251 break;
1252
1253 /*
1254 * We also require super-user privilege for
1255 * the option. See comments on IN6_HOPOPTS.
1256 */
1257 if (!privileged)
1258 break;
1259
1260 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1261 IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1262 IPPROTO_IPV6);
1263 if (*mp)
1264 mp = &(*mp)->m_next;
1265 break;
1266 case IPPROTO_ROUTING:
1267 if (!in6p->in6p_flags & IN6P_RTHDR)
1268 break;
1269
1270 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1271 IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1272 IPPROTO_IPV6);
1273 if (*mp)
1274 mp = &(*mp)->m_next;
1275 break;
1276 case IPPROTO_HOPOPTS:
1277 case IPPROTO_AH: /* is it possible? */
1278 break;
1279
1280 default:
1281 /*
1282 * other cases have been filtered in the above.
1283 * none will visit this case. here we supply
1284 * the code just in case (nxt overwritten or
1285 * other cases).
1286 */
1287 #ifdef PULLDOWN_TEST
1288 m_freem(ext);
1289 #endif
1290 goto loopend;
1291
1292 }
1293
1294 /* proceed with the next header. */
1295 off += elen;
1296 nxt = ip6e->ip6e_nxt;
1297 ip6e = NULL;
1298 #ifdef PULLDOWN_TEST
1299 m_freem(ext);
1300 ext = NULL;
1301 #endif
1302 }
1303 loopend:
1304 ;
1305 }
1306
1307 #undef IS2292
1308 }
1309
1310 #ifdef PULLDOWN_TEST
1311 /*
1312 * pull single extension header from mbuf chain. returns single mbuf that
1313 * contains the result, or NULL on error.
1314 */
1315 static struct mbuf *
1316 ip6_pullexthdr(m, off, nxt)
1317 struct mbuf *m;
1318 size_t off;
1319 int nxt;
1320 {
1321 struct ip6_ext ip6e;
1322 size_t elen;
1323 struct mbuf *n;
1324
1325 #ifdef DIAGNOSTIC
1326 switch (nxt) {
1327 case IPPROTO_DSTOPTS:
1328 case IPPROTO_ROUTING:
1329 case IPPROTO_HOPOPTS:
1330 case IPPROTO_AH: /* is it possible? */
1331 break;
1332 default:
1333 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1334 }
1335 #endif
1336
1337 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1338 if (nxt == IPPROTO_AH)
1339 elen = (ip6e.ip6e_len + 2) << 2;
1340 else
1341 elen = (ip6e.ip6e_len + 1) << 3;
1342
1343 MGET(n, M_DONTWAIT, MT_DATA);
1344 if (n && elen >= MLEN) {
1345 MCLGET(n, M_DONTWAIT);
1346 if ((n->m_flags & M_EXT) == 0) {
1347 m_free(n);
1348 n = NULL;
1349 }
1350 }
1351 if (!n)
1352 return NULL;
1353
1354 n->m_len = 0;
1355 if (elen >= M_TRAILINGSPACE(n)) {
1356 m_free(n);
1357 return NULL;
1358 }
1359
1360 m_copydata(m, off, elen, mtod(n, caddr_t));
1361 n->m_len = elen;
1362 return n;
1363 }
1364 #endif
1365
1366 /*
1367 * Get pointer to the previous header followed by the header
1368 * currently processed.
1369 * XXX: This function supposes that
1370 * M includes all headers,
1371 * the next header field and the header length field of each header
1372 * are valid, and
1373 * the sum of each header length equals to OFF.
1374 * Because of these assumptions, this function must be called very
1375 * carefully. Moreover, it will not be used in the near future when
1376 * we develop `neater' mechanism to process extension headers.
1377 */
1378 char *
1379 ip6_get_prevhdr(m, off)
1380 struct mbuf *m;
1381 int off;
1382 {
1383 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1384
1385 if (off == sizeof(struct ip6_hdr))
1386 return (&ip6->ip6_nxt);
1387 else {
1388 int len, nxt;
1389 struct ip6_ext *ip6e = NULL;
1390
1391 nxt = ip6->ip6_nxt;
1392 len = sizeof(struct ip6_hdr);
1393 while (len < off) {
1394 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1395
1396 switch (nxt) {
1397 case IPPROTO_FRAGMENT:
1398 len += sizeof(struct ip6_frag);
1399 break;
1400 case IPPROTO_AH:
1401 len += (ip6e->ip6e_len + 2) << 2;
1402 break;
1403 default:
1404 len += (ip6e->ip6e_len + 1) << 3;
1405 break;
1406 }
1407 nxt = ip6e->ip6e_nxt;
1408 }
1409 if (ip6e)
1410 return (&ip6e->ip6e_nxt);
1411 else
1412 return NULL;
1413 }
1414 }
1415
1416 /*
1417 * get next header offset. m will be retained.
1418 */
1419 int
1420 ip6_nexthdr(m, off, proto, nxtp)
1421 struct mbuf *m;
1422 int off;
1423 int proto;
1424 int *nxtp;
1425 {
1426 struct ip6_hdr ip6;
1427 struct ip6_ext ip6e;
1428 struct ip6_frag fh;
1429
1430 /* just in case */
1431 if (m == NULL)
1432 panic("ip6_nexthdr: m == NULL");
1433 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1434 return -1;
1435
1436 switch (proto) {
1437 case IPPROTO_IPV6:
1438 if (m->m_pkthdr.len < off + sizeof(ip6))
1439 return -1;
1440 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1441 if (nxtp)
1442 *nxtp = ip6.ip6_nxt;
1443 off += sizeof(ip6);
1444 return off;
1445
1446 case IPPROTO_FRAGMENT:
1447 /*
1448 * terminate parsing if it is not the first fragment,
1449 * it does not make sense to parse through it.
1450 */
1451 if (m->m_pkthdr.len < off + sizeof(fh))
1452 return -1;
1453 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1454 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1455 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1456 return -1;
1457 if (nxtp)
1458 *nxtp = fh.ip6f_nxt;
1459 off += sizeof(struct ip6_frag);
1460 return off;
1461
1462 case IPPROTO_AH:
1463 if (m->m_pkthdr.len < off + sizeof(ip6e))
1464 return -1;
1465 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1466 if (nxtp)
1467 *nxtp = ip6e.ip6e_nxt;
1468 off += (ip6e.ip6e_len + 2) << 2;
1469 return off;
1470
1471 case IPPROTO_HOPOPTS:
1472 case IPPROTO_ROUTING:
1473 case IPPROTO_DSTOPTS:
1474 if (m->m_pkthdr.len < off + sizeof(ip6e))
1475 return -1;
1476 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1477 if (nxtp)
1478 *nxtp = ip6e.ip6e_nxt;
1479 off += (ip6e.ip6e_len + 1) << 3;
1480 return off;
1481
1482 case IPPROTO_NONE:
1483 case IPPROTO_ESP:
1484 case IPPROTO_IPCOMP:
1485 /* give up */
1486 return -1;
1487
1488 default:
1489 return -1;
1490 }
1491
1492 return -1;
1493 }
1494
1495 /*
1496 * get offset for the last header in the chain. m will be kept untainted.
1497 */
1498 int
1499 ip6_lasthdr(m, off, proto, nxtp)
1500 struct mbuf *m;
1501 int off;
1502 int proto;
1503 int *nxtp;
1504 {
1505 int newoff;
1506 int nxt;
1507
1508 if (!nxtp) {
1509 nxt = -1;
1510 nxtp = &nxt;
1511 }
1512 while (1) {
1513 newoff = ip6_nexthdr(m, off, proto, nxtp);
1514 if (newoff < 0)
1515 return off;
1516 else if (newoff < off)
1517 return -1; /* invalid */
1518 else if (newoff == off)
1519 return newoff;
1520
1521 off = newoff;
1522 proto = *nxtp;
1523 }
1524 }
1525
1526 struct ip6aux *
1527 ip6_addaux(m)
1528 struct mbuf *m;
1529 {
1530 struct m_tag *mtag;
1531
1532 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1533 if (!mtag) {
1534 mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1535 M_NOWAIT);
1536 if (mtag) {
1537 m_tag_prepend(m, mtag);
1538 bzero(mtag + 1, sizeof(struct ip6aux));
1539 }
1540 }
1541 return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1542 }
1543
1544 struct ip6aux *
1545 ip6_findaux(m)
1546 struct mbuf *m;
1547 {
1548 struct m_tag *mtag;
1549
1550 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1551 return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1552 }
1553
1554 void
1555 ip6_delaux(m)
1556 struct mbuf *m;
1557 {
1558 struct m_tag *mtag;
1559
1560 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1561 if (mtag)
1562 m_tag_delete(m, mtag);
1563 }
1564
1565 /*
1566 * System control for IP6
1567 */
1568
1569 u_char inet6ctlerrmap[PRC_NCMDS] = {
1570 0, 0, 0, 0,
1571 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1572 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1573 EMSGSIZE, EHOSTUNREACH, 0, 0,
1574 0, 0, 0, 0,
1575 ENOPROTOOPT
1576 };
Cache object: 220920857c6d72a30aa7ea8d54e97b6b
|