]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ip6_input.c
390be10d1703e311491a2dcc8a1d53197e39e979
[apple/xnu.git] / bsd / netinet6 / ip6_input.c
1 /*
2 * Copyright (c) 2003-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.10 2001/07/24 19:10:18 brooks Exp $ */
29 /* $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $ */
30
31 /*
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60 /*
61 * Copyright (c) 1982, 1986, 1988, 1993
62 * The Regents of the University of California. All rights reserved.
63 *
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
66 * are met:
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
72 * 3. All advertising materials mentioning features or use of this software
73 * must display the following acknowledgement:
74 * This product includes software developed by the University of
75 * California, Berkeley and its contributors.
76 * 4. Neither the name of the University nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
79 *
80 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * SUCH DAMAGE.
91 *
92 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
93 */
94
95
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/malloc.h>
99 #include <sys/mbuf.h>
100 #include <sys/domain.h>
101 #include <sys/protosw.h>
102 #include <sys/socket.h>
103 #include <sys/socketvar.h>
104 #include <sys/errno.h>
105 #include <sys/time.h>
106 #include <sys/kernel.h>
107 #include <sys/syslog.h>
108 #include <sys/proc.h>
109 #include <sys/kauth.h>
110
111 #include <net/if.h>
112 #include <net/if_var.h>
113 #include <net/if_types.h>
114 #include <net/if_dl.h>
115 #include <net/route.h>
116 #include <net/kpi_protocol.h>
117
118 #include <netinet/in.h>
119 #include <netinet/in_systm.h>
120 #if INET
121 #include <netinet/ip.h>
122 #include <netinet/ip_icmp.h>
123 #endif /*INET*/
124 #include <netinet/ip6.h>
125 #include <netinet6/in6_var.h>
126 #include <netinet6/ip6_var.h>
127 #include <netinet/in_pcb.h>
128 #include <netinet/icmp6.h>
129 #include <netinet6/in6_ifattach.h>
130 #include <netinet6/nd6.h>
131 #include <netinet6/in6_prefix.h>
132
133 #if IPSEC
134 #include <netinet6/ipsec.h>
135 #if INET6
136 #include <netinet6/ipsec6.h>
137 #endif
138 extern int ipsec_bypass;
139 #endif
140
141 #include <netinet6/ip6_fw.h>
142
143 #include <netinet/kpi_ipfilter_var.h>
144
145 #include <netinet6/ip6protosw.h>
146
147 /* we need it for NLOOP. */
148 #include "loop.h"
149 #include "faith.h"
150
151 #include <net/net_osdep.h>
152
153 #if PF
154 #include <net/pfvar.h>
155 #endif /* PF */
156
157 extern struct domain inet6domain;
158 extern struct ip6protosw inet6sw[];
159
160 struct ip6protosw * ip6_protox[IPPROTO_MAX];
161 static int ip6qmaxlen = IFQ_MAXLEN;
162 struct in6_ifaddr *in6_ifaddrs;
163
164 int ip6_forward_srcrt; /* XXX */
165 int ip6_sourcecheck; /* XXX */
166 int ip6_sourcecheck_interval; /* XXX */
167 const int int6intrq_present = 1;
168
169 int ip6_ours_check_algorithm;
170 int in6_init2done = 0;
171
172
173 /* firewall hooks */
174 ip6_fw_chk_t *ip6_fw_chk_ptr;
175 ip6_fw_ctl_t *ip6_fw_ctl_ptr;
176 int ip6_fw_enable = 1;
177
178 struct ip6stat ip6stat;
179
180 #ifdef __APPLE__
181 struct ifqueue ip6intrq;
182 lck_mtx_t *ip6_mutex;
183 lck_mtx_t *dad6_mutex;
184 lck_mtx_t *nd6_mutex;
185 lck_mtx_t *prefix6_mutex;
186 lck_mtx_t *scope6_mutex;
187 lck_attr_t *ip6_mutex_attr;
188 lck_grp_t *ip6_mutex_grp;
189 lck_grp_attr_t *ip6_mutex_grp_attr;
190 extern lck_mtx_t *inet6_domain_mutex;
191 #endif
192 extern int loopattach_done;
193
194 static void ip6_init2(void *);
195 static struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *);
196
197 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
198 #if PULLDOWN_TEST
199 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
200 #endif
201
202 #ifdef __APPLE__
203 void gifattach(void);
204 void faithattach(void);
205 void stfattach(void);
206 #endif
207
208 extern lck_mtx_t *domain_proto_mtx;
209
210
211 static void
212 ip6_proto_input(
213 __unused protocol_family_t protocol,
214 mbuf_t packet)
215 {
216 ip6_input(packet);
217 }
218
219 /*
220 * IP6 initialization: fill in IP6 protocol switch table.
221 * All protocols not implemented in kernel go to raw IP6 protocol handler.
222 */
223 void
224 ip6_init()
225 {
226 struct ip6protosw *pr;
227 int i;
228 struct timeval tv;
229
230 #if DIAGNOSTIC
231 if (sizeof(struct protosw) != sizeof(struct ip6protosw))
232 panic("sizeof(protosw) != sizeof(ip6protosw)");
233 #endif
234 pr = (struct ip6protosw *)pffindproto_locked(PF_INET6, IPPROTO_RAW, SOCK_RAW);
235 if (pr == 0)
236 panic("ip6_init");
237 for (i = 0; i < IPPROTO_MAX; i++)
238 ip6_protox[i] = pr;
239 for (pr = (struct ip6protosw*)inet6domain.dom_protosw; pr; pr = pr->pr_next) {
240 if(!(pr->pr_domain)) continue; /* If uninitialized, skip */
241 if (pr->pr_domain->dom_family == PF_INET6 &&
242 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
243 ip6_protox[pr->pr_protocol] = pr;
244 }
245 }
246
247 ip6_mutex_grp_attr = lck_grp_attr_alloc_init();
248
249 ip6_mutex_grp = lck_grp_alloc_init("ip6", ip6_mutex_grp_attr);
250 ip6_mutex_attr = lck_attr_alloc_init();
251
252 if ((ip6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
253 panic("ip6_init: can't alloc ip6_mutex\n");
254 }
255 if ((dad6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
256 panic("ip6_init: can't alloc dad6_mutex\n");
257 }
258 if ((nd6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
259 panic("ip6_init: can't alloc nd6_mutex\n");
260 }
261
262 if ((prefix6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
263 panic("ip6_init: can't alloc prefix6_mutex\n");
264 }
265
266 if ((scope6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
267 panic("ip6_init: can't alloc scope6_mutex\n");
268 }
269
270
271 inet6domain.dom_flags = DOM_REENTRANT;
272
273 ip6intrq.ifq_maxlen = ip6qmaxlen;
274 in6_ifaddr_init();
275 nd6_init();
276 frag6_init();
277 icmp6_init();
278 /*
279 * in many cases, random() here does NOT return random number
280 * as initialization during bootstrap time occur in fixed order.
281 */
282 microtime(&tv);
283 ip6_flow_seq = random() ^ tv.tv_usec;
284 microtime(&tv);
285 ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR;
286 timeout(ip6_init2, (caddr_t)0, 1 * hz);
287
288 lck_mtx_unlock(domain_proto_mtx);
289 proto_register_input(PF_INET6, ip6_proto_input, NULL, 0);
290 lck_mtx_lock(domain_proto_mtx);
291 }
292
293 static void
294 ip6_init2(
295 __unused void *dummy)
296 {
297 /*
298 * to route local address of p2p link to loopback,
299 * assign loopback address first.
300 */
301 if (loopattach_done == 0) {
302 timeout(ip6_init2, (caddr_t)0, 1 * hz);
303 return;
304 }
305 (void) in6_ifattach(lo_ifp, NULL, NULL);
306
307 #ifdef __APPLE__
308 /* nd6_timer_init */
309 timeout(nd6_timer, (caddr_t)0, hz);
310
311 /* router renumbering prefix list maintenance */
312 timeout(in6_rr_timer, (caddr_t)0, hz);
313
314 /* timer for regeneranation of temporary addresses randomize ID */
315 timeout(in6_tmpaddrtimer, (caddr_t)0,
316 (ip6_temp_preferred_lifetime - ip6_desync_factor -
317 ip6_temp_regen_advance) * hz);
318
319 #if NGIF
320 gifattach();
321 #endif
322 #if NFAITH
323 faithattach();
324 #endif
325 #if NSTF
326 stfattach();
327 #endif
328 #else
329 /* nd6_timer_init */
330
331 callout_init(&nd6_timer_ch);
332 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
333
334 /* router renumbering prefix list maintenance */
335 callout_init(&in6_rr_timer_ch);
336 callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
337
338 /* timer for regeneranation of temporary addresses randomize ID */
339 callout_reset(&in6_tmpaddrtimer_ch,
340 (ip6_temp_preferred_lifetime - ip6_desync_factor -
341 ip6_temp_regen_advance) * hz,
342 in6_tmpaddrtimer, NULL);
343 #endif
344
345 in6_init2done = 1;
346 }
347
348 #if __FreeBSD__
349 /* cheat */
350 /* This must be after route_init(), which is now SI_ORDER_THIRD */
351 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
352 #endif
353
354 /*
355 * ip6_forward_rt contains the route entry that was recently used during
356 * the forwarding of an IPv6 packet and thus acts as a route cache. Access
357 * to this variable is protected by the global lock ip6_mutex.
358 */
359 static struct route_in6 ip6_forward_rt;
360
361 void
362 ip6_input(m)
363 struct mbuf *m;
364 {
365 struct ip6_hdr *ip6;
366 int off = sizeof(struct ip6_hdr), nest;
367 u_int32_t plen;
368 u_int32_t rtalert = ~0;
369 int nxt = 0, ours = 0;
370 struct ifnet *deliverifp = NULL;
371 ipfilter_t inject_ipfref = 0;
372 int seen;
373
374 /*
375 * No need to proccess packet twice if we've
376 * already seen it
377 */
378 inject_ipfref = ipf_get_inject_filter(m);
379 if (inject_ipfref != 0) {
380 ip6 = mtod(m, struct ip6_hdr *);
381 nxt = ip6->ip6_nxt;
382 seen = 0;
383 goto injectit;
384 } else
385 seen = 1;
386
387 #if IPSEC
388 /*
389 * should the inner packet be considered authentic?
390 * see comment in ah4_input().
391 */
392 if (m) {
393 m->m_flags &= ~M_AUTHIPHDR;
394 m->m_flags &= ~M_AUTHIPDGM;
395 }
396 #endif
397
398 /*
399 * make sure we don't have onion peering information into m_aux.
400 */
401 ip6_delaux(m);
402
403 lck_mtx_lock(ip6_mutex);
404 /*
405 * mbuf statistics
406 */
407 if (m->m_flags & M_EXT) {
408 if (m->m_next)
409 ip6stat.ip6s_mext2m++;
410 else
411 ip6stat.ip6s_mext1++;
412 } else {
413 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
414 if (m->m_next) {
415 if (m->m_flags & M_LOOP) {
416 ip6stat.ip6s_m2m[ifnet_index(lo_ifp)]++; /* XXX */
417 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
418 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
419 else
420 ip6stat.ip6s_m2m[0]++;
421 } else
422 ip6stat.ip6s_m1++;
423 #undef M2MMAX
424 }
425
426 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
427 ip6stat.ip6s_total++;
428
429 #ifndef PULLDOWN_TEST
430 /*
431 * L2 bridge code and some other code can return mbuf chain
432 * that does not conform to KAME requirement. too bad.
433 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
434 */
435 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
436 struct mbuf *n;
437
438 MGETHDR(n, M_DONTWAIT, MT_HEADER); /* MAC-OK */
439 if (n)
440 M_COPY_PKTHDR(n, m);
441 if (n && m->m_pkthdr.len > MHLEN) {
442 MCLGET(n, M_DONTWAIT);
443 if ((n->m_flags & M_EXT) == 0) {
444 m_freem(n);
445 n = NULL;
446 }
447 }
448 if (n == NULL) {
449 m_freem(m);
450 lck_mtx_unlock(ip6_mutex);
451 return; /*ENOBUFS*/
452 }
453
454 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
455 n->m_len = m->m_pkthdr.len;
456 m_freem(m);
457 m = n;
458 }
459 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr),
460 {lck_mtx_unlock(ip6_mutex); return;});
461 #endif
462
463 if (m->m_len < sizeof(struct ip6_hdr)) {
464 struct ifnet *inifp;
465 inifp = m->m_pkthdr.rcvif;
466 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
467 ip6stat.ip6s_toosmall++;
468 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
469 lck_mtx_unlock(ip6_mutex);
470 return;
471 }
472 }
473
474 ip6 = mtod(m, struct ip6_hdr *);
475
476 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
477 ip6stat.ip6s_badvers++;
478 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
479 goto bad;
480 }
481
482 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
483
484 /*
485 * Check with the firewall...
486 */
487 if (ip6_fw_enable && ip6_fw_chk_ptr) {
488 u_short port = 0;
489 /* If ipfw says divert, we have to just drop packet */
490 /* use port as a dummy argument */
491 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
492 m_freem(m);
493 m = NULL;
494 }
495 if (!m) {
496 lck_mtx_unlock(ip6_mutex);
497 return;
498 }
499 }
500
501 /*
502 * Check against address spoofing/corruption.
503 */
504 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
505 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
506 /*
507 * XXX: "badscope" is not very suitable for a multicast source.
508 */
509 ip6stat.ip6s_badscope++;
510 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
511 goto bad;
512 }
513 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
514 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
515 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
516 ip6stat.ip6s_badscope++;
517 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
518 goto bad;
519 }
520
521 /*
522 * The following check is not documented in specs. A malicious
523 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
524 * and bypass security checks (act as if it was from 127.0.0.1 by using
525 * IPv6 src ::ffff:127.0.0.1). Be cautious.
526 *
527 * This check chokes if we are in an SIIT cloud. As none of BSDs
528 * support IPv4-less kernel compilation, we cannot support SIIT
529 * environment at all. So, it makes more sense for us to reject any
530 * malicious packets for non-SIIT environment, than try to do a
531 * partical support for SIIT environment.
532 */
533 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
534 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
535 ip6stat.ip6s_badscope++;
536 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
537 goto bad;
538 }
539 #if 0
540 /*
541 * Reject packets with IPv4 compatible addresses (auto tunnel).
542 *
543 * The code forbids auto tunnel relay case in RFC1933 (the check is
544 * stronger than RFC1933). We may want to re-enable it if mech-xx
545 * is revised to forbid relaying case.
546 */
547 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
548 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
549 ip6stat.ip6s_badscope++;
550 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
551 goto bad;
552 }
553 #endif
554
555 #if PF
556 /* Invoke inbound packet filter */
557 lck_mtx_unlock(ip6_mutex);
558 if (pf_af_hook(m->m_pkthdr.rcvif, NULL, &m, AF_INET6, TRUE) != 0) {
559 if (m != NULL) {
560 panic("%s: unexpected packet %p\n", __func__, m);
561 /* NOTREACHED */
562 }
563 /* Already freed by callee */
564 return;
565 }
566 ip6 = mtod(m, struct ip6_hdr *);
567 lck_mtx_lock(ip6_mutex);
568 #endif /* PF */
569
570 /* drop packets if interface ID portion is already filled */
571 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
572 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
573 ip6->ip6_src.s6_addr16[1]) {
574 ip6stat.ip6s_badscope++;
575 goto bad;
576 }
577 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
578 ip6->ip6_dst.s6_addr16[1]) {
579 ip6stat.ip6s_badscope++;
580 goto bad;
581 }
582 }
583
584 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
585 ip6->ip6_src.s6_addr16[1]
586 = htons(m->m_pkthdr.rcvif->if_index);
587 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
588 ip6->ip6_dst.s6_addr16[1]
589 = htons(m->m_pkthdr.rcvif->if_index);
590
591 #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */
592 /*
593 * We use rt->rt_ifp to determine if the address is ours or not.
594 * If rt_ifp is lo0, the address is ours.
595 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
596 * so any address under fe80::%lo0/64 will be mistakenly considered
597 * local. The special case is supplied to handle the case properly
598 * by actually looking at interface addresses
599 * (using in6ifa_ifpwithaddr).
600 */
601 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
602 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
603 struct in6_ifaddr *ia6;
604 if (!(ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst))) {
605 lck_mtx_unlock(ip6_mutex);
606 icmp6_error(m, ICMP6_DST_UNREACH,
607 ICMP6_DST_UNREACH_ADDR, 0);
608 /* m is already freed */
609 return;
610 }
611 ifafree(&ia6->ia_ifa);
612
613 ours = 1;
614 deliverifp = m->m_pkthdr.rcvif;
615 goto hbhcheck;
616 }
617 #endif
618
619 /*
620 * Multicast check
621 */
622 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
623 struct in6_multi *in6m = 0;
624 struct ifnet *ifp = m->m_pkthdr.rcvif;
625
626 in6_ifstat_inc(ifp, ifs6_in_mcast);
627 /*
628 * See if we belong to the destination multicast group on the
629 * arrival interface.
630 */
631 ifnet_lock_shared(ifp);
632 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
633 ifnet_lock_done(ifp);
634 if (in6m)
635 ours = 1;
636 else if (!ip6_mrouter) {
637 ip6stat.ip6s_notmember++;
638 ip6stat.ip6s_cantforward++;
639 in6_ifstat_inc(ifp, ifs6_in_discard);
640 goto bad;
641 }
642 deliverifp = ifp;
643 goto hbhcheck;
644 }
645
646 if (ip6_forward_rt.ro_rt != NULL)
647 RT_LOCK(ip6_forward_rt.ro_rt);
648 /*
649 * Unicast check
650 */
651 if (ip6_forward_rt.ro_rt != NULL &&
652 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) &&
653 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
654 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr) &&
655 ip6_forward_rt.ro_rt->generation_id == route_generation) {
656 ip6stat.ip6s_forward_cachehit++;
657 } else {
658 struct sockaddr_in6 *dst6;
659
660 if (ip6_forward_rt.ro_rt != NULL) {
661 /* route is down/stale or destination is different */
662 ip6stat.ip6s_forward_cachemiss++;
663 RT_UNLOCK(ip6_forward_rt.ro_rt);
664 rtfree(ip6_forward_rt.ro_rt);
665 ip6_forward_rt.ro_rt = NULL;
666 }
667
668 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
669 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
670 dst6->sin6_len = sizeof(struct sockaddr_in6);
671 dst6->sin6_family = AF_INET6;
672 dst6->sin6_addr = ip6->ip6_dst;
673 #if SCOPEDROUTING
674 ip6_forward_rt.ro_dst.sin6_scope_id =
675 in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst);
676 #endif
677
678 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
679 if (ip6_forward_rt.ro_rt != NULL)
680 RT_LOCK(ip6_forward_rt.ro_rt);
681 }
682
683 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
684
685 /*
686 * Accept the packet if the forwarding interface to the destination
687 * according to the routing table is the loopback interface,
688 * unless the associated route has a gateway.
689 * Note that this approach causes to accept a packet if there is a
690 * route to the loopback interface for the destination of the packet.
691 * But we think it's even useful in some situations, e.g. when using
692 * a special daemon which wants to intercept the packet.
693 *
694 * XXX: some OSes automatically make a cloned route for the destination
695 * of an outgoing packet. If the outgoing interface of the packet
696 * is a loopback one, the kernel would consider the packet to be
697 * accepted, even if we have no such address assinged on the interface.
698 * We check the cloned flag of the route entry to reject such cases,
699 * assuming that route entries for our own addresses are not made by
700 * cloning (it should be true because in6_addloop explicitly installs
701 * the host route). However, we might have to do an explicit check
702 * while it would be less efficient. Or, should we rather install a
703 * reject route for such a case?
704 */
705 if (ip6_forward_rt.ro_rt != NULL &&
706 (ip6_forward_rt.ro_rt->rt_flags &
707 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
708 #if RTF_WASCLONED
709 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
710 #endif
711 #if 0
712 /*
713 * The check below is redundant since the comparison of
714 * the destination and the key of the rtentry has
715 * already done through looking up the routing table.
716 */
717 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
718 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
719 #endif
720 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
721 struct in6_ifaddr *ia6 =
722 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
723
724 /*
725 * record address information into m_aux.
726 */
727 (void)ip6_setdstifaddr(m, ia6);
728
729 /*
730 * packets to a tentative, duplicated, or somehow invalid
731 * address must not be accepted.
732 */
733 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
734 /* this address is ready */
735 ours = 1;
736 deliverifp = ia6->ia_ifp; /* correct? */
737 /* Count the packet in the ip address stats */
738 #ifndef __APPLE__
739
740 ia6->ia_ifa.if_ipackets++;
741 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
742 #endif
743 RT_UNLOCK(ip6_forward_rt.ro_rt);
744 goto hbhcheck;
745 } else {
746 RT_UNLOCK(ip6_forward_rt.ro_rt);
747 /* address is not ready, so discard the packet. */
748 nd6log((LOG_INFO,
749 "ip6_input: packet to an unready address %s->%s\n",
750 ip6_sprintf(&ip6->ip6_src),
751 ip6_sprintf(&ip6->ip6_dst)));
752 goto bad;
753 }
754 }
755
756 /*
757 * FAITH(Firewall Aided Internet Translator)
758 */
759 #if defined(NFAITH) && 0 < NFAITH
760 if (ip6_keepfaith) {
761 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
762 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
763 /* XXX do we need more sanity checks? */
764 ours = 1;
765 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
766 RT_UNLOCK(ip6_forward_rt.ro_rt);
767 goto hbhcheck;
768 }
769 }
770 #endif
771 if (ip6_forward_rt.ro_rt != NULL)
772 RT_UNLOCK(ip6_forward_rt.ro_rt);
773
774 /*
775 * Now there is no reason to process the packet if it's not our own
776 * and we're not a router.
777 */
778 if (!ip6_forwarding) {
779 ip6stat.ip6s_cantforward++;
780 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
781 goto bad;
782 }
783
784 hbhcheck:
785 /*
786 * record address information into m_aux, if we don't have one yet.
787 * note that we are unable to record it, if the address is not listed
788 * as our interface address (e.g. multicast addresses, addresses
789 * within FAITH prefixes and such).
790 */
791 if (deliverifp && !ip6_getdstifaddr(m)) {
792 struct in6_ifaddr *ia6;
793
794 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
795 if (ia6) {
796 if (!ip6_setdstifaddr(m, ia6)) {
797 /*
798 * XXX maybe we should drop the packet here,
799 * as we could not provide enough information
800 * to the upper layers.
801 */
802 }
803 ifafree(&ia6->ia_ifa);
804 }
805 }
806
807 /*
808 * Process Hop-by-Hop options header if it's contained.
809 * m may be modified in ip6_hopopts_input().
810 * If a JumboPayload option is included, plen will also be modified.
811 */
812 plen = (u_int32_t)ntohs(ip6->ip6_plen);
813 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
814 struct ip6_hbh *hbh;
815
816 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
817 #if 0 /*touches NULL pointer*/
818 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
819 #endif
820 lck_mtx_unlock(ip6_mutex);
821 return; /* m have already been freed */
822 }
823
824 /* adjust pointer */
825 ip6 = mtod(m, struct ip6_hdr *);
826
827 /*
828 * if the payload length field is 0 and the next header field
829 * indicates Hop-by-Hop Options header, then a Jumbo Payload
830 * option MUST be included.
831 */
832 if (ip6->ip6_plen == 0 && plen == 0) {
833 /*
834 * Note that if a valid jumbo payload option is
835 * contained, ip6_hoptops_input() must set a valid
836 * (non-zero) payload length to the variable plen.
837 */
838 ip6stat.ip6s_badoptions++;
839 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
840 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
841 lck_mtx_unlock(ip6_mutex);
842 icmp6_error(m, ICMP6_PARAM_PROB,
843 ICMP6_PARAMPROB_HEADER,
844 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
845 return;
846 }
847 #ifndef PULLDOWN_TEST
848 /* ip6_hopopts_input() ensures that mbuf is contiguous */
849 hbh = (struct ip6_hbh *)(ip6 + 1);
850 #else
851 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
852 sizeof(struct ip6_hbh));
853 if (hbh == NULL) {
854 ip6stat.ip6s_tooshort++;
855 lck_mtx_unlock(ip6_mutex);
856 return;
857 }
858 #endif
859 nxt = hbh->ip6h_nxt;
860
861 /*
862 * accept the packet if a router alert option is included
863 * and we act as an IPv6 router.
864 */
865 if (rtalert != ~0 && ip6_forwarding)
866 ours = 1;
867 } else
868 nxt = ip6->ip6_nxt;
869
870 /*
871 * Check that the amount of data in the buffers
872 * is as at least much as the IPv6 header would have us expect.
873 * Trim mbufs if longer than we expect.
874 * Drop packet if shorter than we expect.
875 */
876 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
877 ip6stat.ip6s_tooshort++;
878 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
879 goto bad;
880 }
881 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
882 if (m->m_len == m->m_pkthdr.len) {
883 m->m_len = sizeof(struct ip6_hdr) + plen;
884 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
885 } else
886 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
887 }
888
889 /*
890 * Forward if desirable.
891 */
892 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
893 /*
894 * If we are acting as a multicast router, all
895 * incoming multicast packets are passed to the
896 * kernel-level multicast forwarding function.
897 * The packet is returned (relatively) intact; if
898 * ip6_mforward() returns a non-zero value, the packet
899 * must be discarded, else it may be accepted below.
900 */
901 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
902 ip6stat.ip6s_cantforward++;
903 m_freem(m);
904 lck_mtx_unlock(ip6_mutex);
905 return;
906 }
907 if (!ours) {
908 m_freem(m);
909 lck_mtx_unlock(ip6_mutex);
910 return;
911 }
912 } else if (!ours) {
913 ip6_forward(m, &ip6_forward_rt, 0, 1);
914 lck_mtx_unlock(ip6_mutex);
915 return;
916 }
917
918 ip6 = mtod(m, struct ip6_hdr *);
919
920 /*
921 * Malicious party may be able to use IPv4 mapped addr to confuse
922 * tcp/udp stack and bypass security checks (act as if it was from
923 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
924 *
925 * For SIIT end node behavior, you may want to disable the check.
926 * However, you will become vulnerable to attacks using IPv4 mapped
927 * source.
928 */
929 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
930 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
931 ip6stat.ip6s_badscope++;
932 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
933 goto bad;
934 }
935
936 /*
937 * Tell launch routine the next header
938 */
939 ip6stat.ip6s_delivered++;
940 in6_ifstat_inc(deliverifp, ifs6_in_deliver);
941
942 lck_mtx_unlock(ip6_mutex);
943 injectit:
944 nest = 0;
945
946 while (nxt != IPPROTO_DONE) {
947 struct ipfilter *filter;
948
949 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
950 ip6stat.ip6s_toomanyhdr++;
951 goto badunlocked;
952 }
953
954 /*
955 * protection against faulty packet - there should be
956 * more sanity checks in header chain processing.
957 */
958 if (m->m_pkthdr.len < off) {
959 ip6stat.ip6s_tooshort++;
960 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
961 goto badunlocked;
962 }
963
964 #if 0
965 /*
966 * do we need to do it for every header? yeah, other
967 * functions can play with it (like re-allocate and copy).
968 */
969 mhist = ip6_addaux(m);
970 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) {
971 hist = mtod(mhist, caddr_t) + mhist->m_len;
972 bcopy(&nxt, hist, sizeof(nxt));
973 mhist->m_len += sizeof(nxt);
974 } else {
975 ip6stat.ip6s_toomanyhdr++;
976 goto bad;
977 }
978 #endif
979
980 #if IPSEC
981 /*
982 * enforce IPsec policy checking if we are seeing last header.
983 * note that we do not visit this with protocols with pcb layer
984 * code - like udp/tcp/raw ip.
985 */
986 if ((ipsec_bypass == 0) && (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0) {
987 if (ipsec6_in_reject(m, NULL)) {
988 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
989 goto badunlocked;
990 }
991 }
992 #endif
993
994 /*
995 * Call IP filter
996 */
997 if (!TAILQ_EMPTY(&ipv6_filters)) {
998 ipf_ref();
999 TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
1000 if (seen == 0) {
1001 if ((struct ipfilter *)inject_ipfref == filter)
1002 seen = 1;
1003 } else if (filter->ipf_filter.ipf_input) {
1004 errno_t result;
1005
1006 result = filter->ipf_filter.ipf_input(
1007 filter->ipf_filter.cookie, (mbuf_t*)&m, off, nxt);
1008 if (result == EJUSTRETURN) {
1009 ipf_unref();
1010 return;
1011 }
1012 if (result != 0) {
1013 ipf_unref();
1014 m_freem(m);
1015 return;
1016 }
1017 }
1018 }
1019 ipf_unref();
1020 }
1021 if (!(ip6_protox[nxt]->pr_flags & PR_PROTOLOCK)) {
1022 lck_mtx_lock(inet6_domain_mutex);
1023 nxt = (*ip6_protox[nxt]->pr_input)(&m, &off);
1024 lck_mtx_unlock(inet6_domain_mutex);
1025 }
1026 else
1027 nxt = (*ip6_protox[nxt]->pr_input)(&m, &off);
1028 }
1029 return;
1030 bad:
1031 lck_mtx_unlock(ip6_mutex);
1032 badunlocked:
1033 m_freem(m);
1034 return;
1035 }
1036
1037 /*
1038 * set/grab in6_ifaddr correspond to IPv6 destination address.
1039 * XXX backward compatibility wrapper
1040 */
1041 static struct ip6aux *
1042 ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
1043 {
1044 struct ip6aux *n;
1045
1046 n = ip6_addaux(m);
1047 if (n)
1048 n->ip6a_dstia6 = ia6;
1049 return (struct ip6aux *)n; /* NULL if failed to set */
1050 }
1051
1052 struct in6_ifaddr *
1053 ip6_getdstifaddr(m)
1054 struct mbuf *m;
1055 {
1056 struct ip6aux *n;
1057
1058 n = ip6_findaux(m);
1059 if (n)
1060 return n->ip6a_dstia6;
1061 else
1062 return NULL;
1063 }
1064
1065 /*
1066 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1067 * included, the real payload length will be stored in plenp.
1068 */
1069 static int
1070 ip6_hopopts_input(plenp, rtalertp, mp, offp)
1071 u_int32_t *plenp;
1072 u_int32_t *rtalertp; /* XXX: should be stored more smart way */
1073 struct mbuf **mp;
1074 int *offp;
1075 {
1076 struct mbuf *m = *mp;
1077 int off = *offp, hbhlen;
1078 struct ip6_hbh *hbh;
1079 u_int8_t *opt;
1080
1081 /* validation of the length of the header */
1082 #ifndef PULLDOWN_TEST
1083 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), return -1);
1084 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1085 hbhlen = (hbh->ip6h_len + 1) << 3;
1086
1087 IP6_EXTHDR_CHECK(m, off, hbhlen, return -1);
1088 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1089 #else
1090 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1091 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
1092 if (hbh == NULL) {
1093 ip6stat.ip6s_tooshort++;
1094 return -1;
1095 }
1096 hbhlen = (hbh->ip6h_len + 1) << 3;
1097 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
1098 hbhlen);
1099 if (hbh == NULL) {
1100 ip6stat.ip6s_tooshort++;
1101 return -1;
1102 }
1103 #endif
1104 off += hbhlen;
1105 hbhlen -= sizeof(struct ip6_hbh);
1106 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
1107
1108 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
1109 hbhlen, rtalertp, plenp) < 0)
1110 return(-1);
1111
1112 *offp = off;
1113 *mp = m;
1114 return(0);
1115 }
1116
1117 /*
1118 * Search header for all Hop-by-hop options and process each option.
1119 * This function is separate from ip6_hopopts_input() in order to
1120 * handle a case where the sending node itself process its hop-by-hop
1121 * options header. In such a case, the function is called from ip6_output().
1122 *
1123 * The function assumes that hbh header is located right after the IPv6 header
1124 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1125 * opthead + hbhlen is located in continuous memory region.
1126 */
1127 int
1128 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
1129 struct mbuf *m;
1130 u_int8_t *opthead;
1131 int hbhlen;
1132 u_int32_t *rtalertp;
1133 u_int32_t *plenp;
1134 {
1135 struct ip6_hdr *ip6;
1136 int optlen = 0;
1137 u_int8_t *opt = opthead;
1138 u_int16_t rtalert_val;
1139 u_int32_t jumboplen;
1140 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1141
1142 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1143 switch (*opt) {
1144 case IP6OPT_PAD1:
1145 optlen = 1;
1146 break;
1147 case IP6OPT_PADN:
1148 if (hbhlen < IP6OPT_MINLEN) {
1149 ip6stat.ip6s_toosmall++;
1150 goto bad;
1151 }
1152 optlen = *(opt + 1) + 2;
1153 break;
1154 case IP6OPT_RTALERT:
1155 /* XXX may need check for alignment */
1156 if (hbhlen < IP6OPT_RTALERT_LEN) {
1157 ip6stat.ip6s_toosmall++;
1158 goto bad;
1159 }
1160 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1161 /* XXX stat */
1162 lck_mtx_unlock(ip6_mutex);
1163 icmp6_error(m, ICMP6_PARAM_PROB,
1164 ICMP6_PARAMPROB_HEADER,
1165 erroff + opt + 1 - opthead);
1166 lck_mtx_lock(ip6_mutex);
1167 return(-1);
1168 }
1169 optlen = IP6OPT_RTALERT_LEN;
1170 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1171 *rtalertp = ntohs(rtalert_val);
1172 break;
1173 case IP6OPT_JUMBO:
1174 /* XXX may need check for alignment */
1175 if (hbhlen < IP6OPT_JUMBO_LEN) {
1176 ip6stat.ip6s_toosmall++;
1177 goto bad;
1178 }
1179 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1180 /* XXX stat */
1181 lck_mtx_unlock(ip6_mutex);
1182 icmp6_error(m, ICMP6_PARAM_PROB,
1183 ICMP6_PARAMPROB_HEADER,
1184 erroff + opt + 1 - opthead);
1185 lck_mtx_lock(ip6_mutex);
1186 return(-1);
1187 }
1188 optlen = IP6OPT_JUMBO_LEN;
1189
1190 /*
1191 * IPv6 packets that have non 0 payload length
1192 * must not contain a jumbo payload option.
1193 */
1194 ip6 = mtod(m, struct ip6_hdr *);
1195 if (ip6->ip6_plen) {
1196 ip6stat.ip6s_badoptions++;
1197 lck_mtx_unlock(ip6_mutex);
1198 icmp6_error(m, ICMP6_PARAM_PROB,
1199 ICMP6_PARAMPROB_HEADER,
1200 erroff + opt - opthead);
1201 lck_mtx_lock(ip6_mutex);
1202 return(-1);
1203 }
1204
1205 /*
1206 * We may see jumbolen in unaligned location, so
1207 * we'd need to perform bcopy().
1208 */
1209 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1210 jumboplen = (u_int32_t)htonl(jumboplen);
1211
1212 #if 1
1213 /*
1214 * if there are multiple jumbo payload options,
1215 * *plenp will be non-zero and the packet will be
1216 * rejected.
1217 * the behavior may need some debate in ipngwg -
1218 * multiple options does not make sense, however,
1219 * there's no explicit mention in specification.
1220 */
1221 if (*plenp != 0) {
1222 ip6stat.ip6s_badoptions++;
1223 lck_mtx_unlock(ip6_mutex);
1224 icmp6_error(m, ICMP6_PARAM_PROB,
1225 ICMP6_PARAMPROB_HEADER,
1226 erroff + opt + 2 - opthead);
1227 lck_mtx_lock(ip6_mutex);
1228 return(-1);
1229 }
1230 #endif
1231
1232 /*
1233 * jumbo payload length must be larger than 65535.
1234 */
1235 if (jumboplen <= IPV6_MAXPACKET) {
1236 ip6stat.ip6s_badoptions++;
1237 lck_mtx_unlock(ip6_mutex);
1238 icmp6_error(m, ICMP6_PARAM_PROB,
1239 ICMP6_PARAMPROB_HEADER,
1240 erroff + opt + 2 - opthead);
1241 lck_mtx_lock(ip6_mutex);
1242 return(-1);
1243 }
1244 *plenp = jumboplen;
1245
1246 break;
1247 default: /* unknown option */
1248 if (hbhlen < IP6OPT_MINLEN) {
1249 ip6stat.ip6s_toosmall++;
1250 goto bad;
1251 }
1252 optlen = ip6_unknown_opt(opt, m,
1253 erroff + opt - opthead, 1);
1254 if (optlen == -1) {
1255 /* ip6_unknown opt unlocked ip6_mutex */
1256 return(-1);
1257 }
1258 optlen += 2;
1259 break;
1260 }
1261 }
1262
1263 return(0);
1264
1265 bad:
1266 m_freem(m);
1267 return(-1);
1268 }
1269
1270 /*
1271 * Unknown option processing.
1272 * The third argument `off' is the offset from the IPv6 header to the option,
1273 * which is necessary if the IPv6 header the and option header and IPv6 header
1274 * is not continuous in order to return an ICMPv6 error.
1275 */
1276 int
1277 ip6_unknown_opt(optp, m, off, locked)
1278 u_int8_t *optp;
1279 struct mbuf *m;
1280 int off;
1281 int locked;
1282 {
1283 struct ip6_hdr *ip6;
1284
1285 switch (IP6OPT_TYPE(*optp)) {
1286 case IP6OPT_TYPE_SKIP: /* ignore the option */
1287 return((int)*(optp + 1));
1288 case IP6OPT_TYPE_DISCARD: /* silently discard */
1289 m_freem(m);
1290 return(-1);
1291 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1292 ip6stat.ip6s_badoptions++;
1293 if (locked)
1294 lck_mtx_unlock(ip6_mutex);
1295 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1296 if (locked)
1297 lck_mtx_lock(ip6_mutex);
1298 return(-1);
1299 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1300 ip6stat.ip6s_badoptions++;
1301 ip6 = mtod(m, struct ip6_hdr *);
1302 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1303 (m->m_flags & (M_BCAST|M_MCAST)))
1304 m_freem(m);
1305 else {
1306 if (locked)
1307 lck_mtx_unlock(ip6_mutex);
1308 icmp6_error(m, ICMP6_PARAM_PROB,
1309 ICMP6_PARAMPROB_OPTION, off);
1310 if (locked)
1311 lck_mtx_lock(ip6_mutex);
1312 }
1313 return(-1);
1314 }
1315
1316 m_freem(m); /* XXX: NOTREACHED */
1317 return(-1);
1318 }
1319
1320 /*
1321 * Create the "control" list for this pcb.
1322 * The function will not modify mbuf chain at all.
1323 *
1324 * with KAME mbuf chain restriction:
1325 * The routine will be called from upper layer handlers like tcp6_input().
1326 * Thus the routine assumes that the caller (tcp6_input) have already
1327 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1328 * very first mbuf on the mbuf chain.
1329 */
1330 void
1331 ip6_savecontrol(in6p, mp, ip6, m)
1332 struct inpcb *in6p;
1333 struct mbuf **mp;
1334 struct ip6_hdr *ip6;
1335 struct mbuf *m;
1336 {
1337 int rthdr_exist = 0;
1338
1339 #if SO_TIMESTAMP
1340 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
1341 struct timeval tv;
1342
1343 microtime(&tv);
1344 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1345 SCM_TIMESTAMP, SOL_SOCKET);
1346 if (*mp) {
1347 mp = &(*mp)->m_next;
1348 }
1349 }
1350 #endif
1351
1352 /* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
1353 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1354 return;
1355
1356 /* RFC 2292 sec. 5 */
1357 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1358 struct in6_pktinfo pi6;
1359 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1360 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
1361 pi6.ipi6_addr.s6_addr16[1] = 0;
1362 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
1363 ? m->m_pkthdr.rcvif->if_index
1364 : 0;
1365 *mp = sbcreatecontrol((caddr_t) &pi6,
1366 sizeof(struct in6_pktinfo), IPV6_PKTINFO,
1367 IPPROTO_IPV6);
1368 if (*mp)
1369 mp = &(*mp)->m_next;
1370 }
1371
1372 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1373 int hlim = ip6->ip6_hlim & 0xff;
1374 *mp = sbcreatecontrol((caddr_t) &hlim,
1375 sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
1376 if (*mp)
1377 mp = &(*mp)->m_next;
1378 }
1379
1380 if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1381 u_int32_t flowinfo;
1382 int tclass;
1383
1384 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1385 flowinfo >>= 20;
1386
1387 tclass = flowinfo & 0xff;
1388 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1389 IPV6_TCLASS, IPPROTO_IPV6);
1390 if (*mp)
1391 mp = &(*mp)->m_next;
1392 }
1393
1394 /*
1395 * IPV6_HOPOPTS socket option. Recall that we required super-user
1396 * privilege for the option (see ip6_ctloutput), but it might be too
1397 * strict, since there might be some hop-by-hop options which can be
1398 * returned to normal user.
1399 * See RFC 2292 section 6.
1400 */
1401 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1402 /*
1403 * Check if a hop-by-hop options header is contatined in the
1404 * received packet, and if so, store the options as ancillary
1405 * data. Note that a hop-by-hop options header must be
1406 * just after the IPv6 header, which fact is assured through
1407 * the IPv6 input processing.
1408 */
1409 ip6 = mtod(m, struct ip6_hdr *);
1410 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1411 struct ip6_hbh *hbh;
1412 int hbhlen = 0;
1413 #if PULLDOWN_TEST
1414 struct mbuf *ext;
1415 #endif
1416
1417 #ifndef PULLDOWN_TEST
1418 hbh = (struct ip6_hbh *)(ip6 + 1);
1419 hbhlen = (hbh->ip6h_len + 1) << 3;
1420 #else
1421 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1422 ip6->ip6_nxt);
1423 if (ext == NULL) {
1424 ip6stat.ip6s_tooshort++;
1425 return;
1426 }
1427 hbh = mtod(ext, struct ip6_hbh *);
1428 hbhlen = (hbh->ip6h_len + 1) << 3;
1429 if (hbhlen != ext->m_len) {
1430 m_freem(ext);
1431 ip6stat.ip6s_tooshort++;
1432 return;
1433 }
1434 #endif
1435
1436 /*
1437 * XXX: We copy whole the header even if a jumbo
1438 * payload option is included, which option is to
1439 * be removed before returning in the RFC 2292.
1440 * Note: this constraint is removed in 2292bis.
1441 */
1442 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1443 IPV6_HOPOPTS, IPPROTO_IPV6);
1444 if (*mp)
1445 mp = &(*mp)->m_next;
1446 #if PULLDOWN_TEST
1447 m_freem(ext);
1448 #endif
1449 }
1450 }
1451
1452 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1453 if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
1454 int proto, off, nxt;
1455
1456 /*
1457 * go through the header chain to see if a routing header is
1458 * contained in the packet. We need this information to store
1459 * destination options headers (if any) properly.
1460 * XXX: performance issue. We should record this info when
1461 * processing extension headers in incoming routine.
1462 * (todo) use m_aux?
1463 */
1464 proto = IPPROTO_IPV6;
1465 off = 0;
1466 nxt = -1;
1467 while (1) {
1468 int newoff;
1469
1470 newoff = ip6_nexthdr(m, off, proto, &nxt);
1471 if (newoff < 0)
1472 break;
1473 if (newoff < off) /* invalid, check for safety */
1474 break;
1475 if ((proto = nxt) == IPPROTO_ROUTING) {
1476 rthdr_exist = 1;
1477 break;
1478 }
1479 off = newoff;
1480 }
1481 }
1482
1483 if ((in6p->in6p_flags &
1484 (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
1485 ip6 = mtod(m, struct ip6_hdr *);
1486 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1487
1488 /*
1489 * Search for destination options headers or routing
1490 * header(s) through the header chain, and stores each
1491 * header as ancillary data.
1492 * Note that the order of the headers remains in
1493 * the chain of ancillary data.
1494 */
1495 while (1) { /* is explicit loop prevention necessary? */
1496 struct ip6_ext *ip6e = NULL;
1497 int elen;
1498 #if PULLDOWN_TEST
1499 struct mbuf *ext = NULL;
1500 #endif
1501
1502 /*
1503 * if it is not an extension header, don't try to
1504 * pull it from the chain.
1505 */
1506 switch (nxt) {
1507 case IPPROTO_DSTOPTS:
1508 case IPPROTO_ROUTING:
1509 case IPPROTO_HOPOPTS:
1510 case IPPROTO_AH: /* is it possible? */
1511 break;
1512 default:
1513 goto loopend;
1514 }
1515
1516 #ifndef PULLDOWN_TEST
1517 if (off + sizeof(*ip6e) > m->m_len)
1518 goto loopend;
1519 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1520 if (nxt == IPPROTO_AH)
1521 elen = (ip6e->ip6e_len + 2) << 2;
1522 else
1523 elen = (ip6e->ip6e_len + 1) << 3;
1524 if (off + elen > m->m_len)
1525 goto loopend;
1526 #else
1527 ext = ip6_pullexthdr(m, off, nxt);
1528 if (ext == NULL) {
1529 ip6stat.ip6s_tooshort++;
1530 return;
1531 }
1532 ip6e = mtod(ext, struct ip6_ext *);
1533 if (nxt == IPPROTO_AH)
1534 elen = (ip6e->ip6e_len + 2) << 2;
1535 else
1536 elen = (ip6e->ip6e_len + 1) << 3;
1537 if (elen != ext->m_len) {
1538 m_freem(ext);
1539 ip6stat.ip6s_tooshort++;
1540 return;
1541 }
1542 #endif
1543
1544 switch (nxt) {
1545 case IPPROTO_DSTOPTS:
1546 if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0)
1547 break;
1548
1549 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1550 IPV6_DSTOPTS,
1551 IPPROTO_IPV6);
1552 if (*mp)
1553 mp = &(*mp)->m_next;
1554 break;
1555 case IPPROTO_ROUTING:
1556 if (!in6p->in6p_flags & IN6P_RTHDR)
1557 break;
1558
1559 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1560 IPV6_RTHDR,
1561 IPPROTO_IPV6);
1562 if (*mp)
1563 mp = &(*mp)->m_next;
1564 break;
1565 case IPPROTO_HOPOPTS:
1566 case IPPROTO_AH: /* is it possible? */
1567 break;
1568
1569 default:
1570 /*
1571 * other cases have been filtered in the above.
1572 * none will visit this case. here we supply
1573 * the code just in case (nxt overwritten or
1574 * other cases).
1575 */
1576 #if PULLDOWN_TEST
1577 m_freem(ext);
1578 #endif
1579 goto loopend;
1580
1581 }
1582
1583 /* proceed with the next header. */
1584 off += elen;
1585 nxt = ip6e->ip6e_nxt;
1586 ip6e = NULL;
1587 #if PULLDOWN_TEST
1588 m_freem(ext);
1589 ext = NULL;
1590 #endif
1591 }
1592 loopend:
1593 ;
1594 }
1595
1596 }
1597
1598 #if PULLDOWN_TEST
1599 /*
1600 * pull single extension header from mbuf chain. returns single mbuf that
1601 * contains the result, or NULL on error.
1602 */
1603 static struct mbuf *
1604 ip6_pullexthdr(m, off, nxt)
1605 struct mbuf *m;
1606 size_t off;
1607 int nxt;
1608 {
1609 struct ip6_ext ip6e;
1610 size_t elen;
1611 struct mbuf *n;
1612
1613 #if DIAGNOSTIC
1614 switch (nxt) {
1615 case IPPROTO_DSTOPTS:
1616 case IPPROTO_ROUTING:
1617 case IPPROTO_HOPOPTS:
1618 case IPPROTO_AH: /* is it possible? */
1619 break;
1620 default:
1621 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1622 }
1623 #endif
1624
1625 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1626 if (nxt == IPPROTO_AH)
1627 elen = (ip6e.ip6e_len + 2) << 2;
1628 else
1629 elen = (ip6e.ip6e_len + 1) << 3;
1630
1631 MGET(n, M_DONTWAIT, MT_DATA);
1632 if (n && elen >= MLEN) {
1633 MCLGET(n, M_DONTWAIT);
1634 if ((n->m_flags & M_EXT) == 0) {
1635 m_free(n);
1636 n = NULL;
1637 }
1638 }
1639 if (!n)
1640 return NULL;
1641
1642 n->m_len = 0;
1643 if (elen >= M_TRAILINGSPACE(n)) {
1644 m_free(n);
1645 return NULL;
1646 }
1647
1648 m_copydata(m, off, elen, mtod(n, caddr_t));
1649 n->m_len = elen;
1650 return n;
1651 }
1652 #endif
1653
1654 /*
1655 * Get pointer to the previous header followed by the header
1656 * currently processed.
1657 * XXX: This function supposes that
1658 * M includes all headers,
1659 * the next header field and the header length field of each header
1660 * are valid, and
1661 * the sum of each header length equals to OFF.
1662 * Because of these assumptions, this function must be called very
1663 * carefully. Moreover, it will not be used in the near future when
1664 * we develop `neater' mechanism to process extension headers.
1665 */
1666 char *
1667 ip6_get_prevhdr(m, off)
1668 struct mbuf *m;
1669 int off;
1670 {
1671 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1672
1673 if (off == sizeof(struct ip6_hdr))
1674 return((char *) &ip6->ip6_nxt);
1675 else {
1676 int len, nxt;
1677 struct ip6_ext *ip6e = NULL;
1678
1679 nxt = ip6->ip6_nxt;
1680 len = sizeof(struct ip6_hdr);
1681 while (len < off) {
1682 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1683
1684 switch (nxt) {
1685 case IPPROTO_FRAGMENT:
1686 len += sizeof(struct ip6_frag);
1687 break;
1688 case IPPROTO_AH:
1689 len += (ip6e->ip6e_len + 2) << 2;
1690 break;
1691 default:
1692 len += (ip6e->ip6e_len + 1) << 3;
1693 break;
1694 }
1695 nxt = ip6e->ip6e_nxt;
1696 }
1697 if (ip6e)
1698 return((char *) &ip6e->ip6e_nxt);
1699 else
1700 return NULL;
1701 }
1702 }
1703
1704 /*
1705 * get next header offset. m will be retained.
1706 */
1707 int
1708 ip6_nexthdr(m, off, proto, nxtp)
1709 struct mbuf *m;
1710 int off;
1711 int proto;
1712 int *nxtp;
1713 {
1714 struct ip6_hdr ip6;
1715 struct ip6_ext ip6e;
1716 struct ip6_frag fh;
1717
1718 /* just in case */
1719 if (m == NULL)
1720 panic("ip6_nexthdr: m == NULL");
1721 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1722 return -1;
1723
1724 switch (proto) {
1725 case IPPROTO_IPV6:
1726 if (m->m_pkthdr.len < off + sizeof(ip6))
1727 return -1;
1728 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1729 if (nxtp)
1730 *nxtp = ip6.ip6_nxt;
1731 off += sizeof(ip6);
1732 return off;
1733
1734 case IPPROTO_FRAGMENT:
1735 /*
1736 * terminate parsing if it is not the first fragment,
1737 * it does not make sense to parse through it.
1738 */
1739 if (m->m_pkthdr.len < off + sizeof(fh))
1740 return -1;
1741 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1742 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1743 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1744 return -1;
1745 if (nxtp)
1746 *nxtp = fh.ip6f_nxt;
1747 off += sizeof(struct ip6_frag);
1748 return off;
1749
1750 case IPPROTO_AH:
1751 if (m->m_pkthdr.len < off + sizeof(ip6e))
1752 return -1;
1753 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1754 if (nxtp)
1755 *nxtp = ip6e.ip6e_nxt;
1756 off += (ip6e.ip6e_len + 2) << 2;
1757 return off;
1758
1759 case IPPROTO_HOPOPTS:
1760 case IPPROTO_ROUTING:
1761 case IPPROTO_DSTOPTS:
1762 if (m->m_pkthdr.len < off + sizeof(ip6e))
1763 return -1;
1764 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1765 if (nxtp)
1766 *nxtp = ip6e.ip6e_nxt;
1767 off += (ip6e.ip6e_len + 1) << 3;
1768 return off;
1769
1770 case IPPROTO_NONE:
1771 case IPPROTO_ESP:
1772 case IPPROTO_IPCOMP:
1773 /* give up */
1774 return -1;
1775
1776 default:
1777 return -1;
1778 }
1779
1780 return -1;
1781 }
1782
1783 /*
1784 * get offset for the last header in the chain. m will be kept untainted.
1785 */
1786 int
1787 ip6_lasthdr(m, off, proto, nxtp)
1788 struct mbuf *m;
1789 int off;
1790 int proto;
1791 int *nxtp;
1792 {
1793 int newoff;
1794 int nxt;
1795
1796 if (!nxtp) {
1797 nxt = -1;
1798 nxtp = &nxt;
1799 }
1800 while (1) {
1801 newoff = ip6_nexthdr(m, off, proto, nxtp);
1802 if (newoff < 0)
1803 return off;
1804 else if (newoff < off)
1805 return -1; /* invalid */
1806 else if (newoff == off)
1807 return newoff;
1808
1809 off = newoff;
1810 proto = *nxtp;
1811 }
1812 }
1813
1814 struct ip6aux *
1815 ip6_addaux(
1816 struct mbuf *m)
1817 {
1818 struct m_tag *tag;
1819
1820 /* Check if one is already allocated */
1821 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6, NULL);
1822 if (tag == NULL) {
1823 /* Allocate a tag */
1824 tag = m_tag_alloc(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6,
1825 sizeof (struct ip6aux), M_DONTWAIT);
1826
1827 /* Attach it to the mbuf */
1828 if (tag) {
1829 m_tag_prepend(m, tag);
1830 }
1831 }
1832
1833 return tag ? (struct ip6aux*)(tag + 1) : NULL;
1834 }
1835
1836 struct ip6aux *
1837 ip6_findaux(
1838 struct mbuf *m)
1839 {
1840 struct m_tag *tag;
1841
1842 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, NULL);
1843
1844 return tag ? (struct ip6aux*)(tag + 1) : NULL;
1845 }
1846
1847 void
1848 ip6_delaux(
1849 struct mbuf *m)
1850 {
1851 struct m_tag *tag;
1852
1853 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, NULL);
1854 if (tag) {
1855 m_tag_delete(m, tag);
1856 }
1857 }
1858
1859 /*
1860 * System control for IP6
1861 */
1862
1863 u_char inet6ctlerrmap[PRC_NCMDS] = {
1864 0, 0, 0, 0,
1865 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1866 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1867 EMSGSIZE, EHOSTUNREACH, 0, 0,
1868 0, 0, 0, 0,
1869 ENOPROTOOPT
1870 };