xref: /netbsd-src/sys/netinet6/nd6_rtr.c (revision b05648aa26dffce5c4d6744406ae32a9c84eef47)
1 /*	$NetBSD: nd6_rtr.c,v 1.149 2020/06/12 11:04:45 roy Exp $	*/
2 /*	$KAME: nd6_rtr.c,v 1.95 2001/02/07 08:09:47 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: nd6_rtr.c,v 1.149 2020/06/12 11:04:45 roy Exp $");
35 
36 #ifdef _KERNEL_OPT
37 #include "opt_net_mpsafe.h"
38 #endif
39 
40 #include <sys/mbuf.h>
41 #include <sys/syslog.h>
42 
43 #include <net/if.h>
44 
45 #include <netinet/in.h>
46 #include <netinet6/in6_var.h>
47 #include <netinet/ip6.h>
48 #include <netinet6/nd6.h>
49 #include <netinet/icmp6.h>
50 #include <netinet6/icmp6_private.h>
51 
52 /*
53  * Cache the source link layer address of Router Advertisement
54  * and Solicition messages.
55  */
56 void
nd6_rtr_cache(struct mbuf * m,int off,int icmp6len,int icmp6_type)57 nd6_rtr_cache(struct mbuf *m, int off, int icmp6len, int icmp6_type)
58 {
59 	struct ifnet *ifp;
60 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
61 	struct nd_router_solicit *nd_rs;
62 	struct nd_router_advert *nd_ra;
63 	struct in6_addr saddr6 = ip6->ip6_src;
64 	char *lladdr = NULL;
65 	int lladdrlen = 0;
66 	union nd_opts ndopts;
67 	struct psref psref;
68 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
69 
70 	ifp = m_get_rcvif_psref(m, &psref);
71 	if (ifp == NULL)
72 		goto freeit;
73 
74 	/* Sanity checks */
75 	if (ip6->ip6_hlim != 255) {
76 		nd6log(LOG_ERR, "invalid hlim (%d) from %s to %s on %s\n",
77 		    ip6->ip6_hlim, IN6_PRINT(ip6bufs, &ip6->ip6_src),
78 		    IN6_PRINT(ip6bufd, &ip6->ip6_dst), if_name(ifp));
79 		goto bad;
80 	}
81 
82 	switch (icmp6_type) {
83 	case ND_ROUTER_SOLICIT:
84 		/*
85 		 * Don't update the neighbor cache, if src = ::.
86 		 * This indicates that the src has no IP address assigned yet.
87 		 */
88 		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
89 			goto freeit;
90 
91 		IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off,
92 		    icmp6len);
93 		if (nd_rs == NULL) {
94 			ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
95 			m_put_rcvif_psref(ifp, &psref);
96 			return;
97 		}
98 
99 		icmp6len -= sizeof(*nd_rs);
100 		nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
101 		break;
102 	case ND_ROUTER_ADVERT:
103 		if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
104 			nd6log(LOG_ERR, "src %s is not link-local\n",
105 			    IN6_PRINT(ip6bufs, &saddr6));
106 			goto bad;
107 		}
108 
109 		IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off,
110 		    icmp6len);
111 		if (nd_ra == NULL) {
112 			ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
113 			m_put_rcvif_psref(ifp, &psref);
114 			return;
115 		}
116 
117 		icmp6len -= sizeof(*nd_ra);
118 		nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
119 		break;
120 	}
121 
122 	if (nd6_options(&ndopts) < 0) {
123 		nd6log(LOG_INFO, "invalid ND option, ignored\n");
124 		/* nd6_options have incremented stats */
125 		goto freeit;
126 	}
127 
128 	if (ndopts.nd_opts_src_lladdr) {
129 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
130 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
131 	}
132 
133 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
134 		nd6log(LOG_INFO, "lladdrlen mismatch for %s "
135 		    "(if %d, %s packet %d)\n",
136 		    IN6_PRINT(ip6bufs, &saddr6),
137 		    ifp->if_addrlen,
138 		    icmp6_type == ND_ROUTER_SOLICIT ? "RS" : "RA",
139 		    lladdrlen - 2);
140 		goto bad;
141 	}
142 
143 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, icmp6_type, 0);
144 
145 freeit:
146 	m_put_rcvif_psref(ifp, &psref);
147 	m_freem(m);
148 	return;
149 
150 bad:
151 	ICMP6_STATINC(icmp6_type == ND_ROUTER_SOLICIT ?
152 	    ICMP6_STAT_BADRS : ICMP6_STAT_BADRA);
153 	m_put_rcvif_psref(ifp, &psref);
154 	m_freem(m);
155 }
156