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