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