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