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