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