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