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