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