Line data Source code
1 : /* $OpenBSD: nd6_rtr.c,v 1.166 2018/01/23 22:06:42 bluhm Exp $ */
2 : /* $KAME: nd6_rtr.c,v 1.97 2001/02/07 11:09:13 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/param.h>
34 : #include <sys/systm.h>
35 : #include <sys/timeout.h>
36 : #include <sys/malloc.h>
37 : #include <sys/mbuf.h>
38 : #include <sys/socket.h>
39 : #include <sys/sockio.h>
40 : #include <sys/time.h>
41 : #include <sys/kernel.h>
42 : #include <sys/errno.h>
43 : #include <sys/ioctl.h>
44 : #include <sys/syslog.h>
45 : #include <sys/queue.h>
46 :
47 : #include <net/if.h>
48 : #include <net/if_dl.h>
49 : #include <net/if_var.h>
50 : #include <net/route.h>
51 : #include <net/rtable.h>
52 :
53 : #include <netinet/in.h>
54 : #include <netinet6/in6_var.h>
55 : #include <netinet/ip6.h>
56 : #include <netinet6/ip6_var.h>
57 : #include <netinet6/nd6.h>
58 : #include <netinet/icmp6.h>
59 :
60 : int rt6_deleteroute(struct rtentry *, void *, unsigned int);
61 :
62 : /*
63 : * Process Source Link-layer Address Options from
64 : * Router Solicitation / Advertisement Messages.
65 : */
66 : void
67 0 : nd6_rtr_cache(struct mbuf *m, int off, int icmp6len, int icmp6_type)
68 : {
69 : struct ifnet *ifp;
70 0 : struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
71 : struct nd_router_solicit *nd_rs;
72 : struct nd_router_advert *nd_ra;
73 0 : struct in6_addr saddr6 = ip6->ip6_src;
74 : char *lladdr = NULL;
75 : int lladdrlen = 0;
76 0 : union nd_opts ndopts;
77 0 : char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
78 :
79 0 : KASSERT(icmp6_type == ND_ROUTER_SOLICIT || icmp6_type ==
80 : ND_ROUTER_ADVERT);
81 :
82 : /* Sanity checks */
83 0 : if (ip6->ip6_hlim != 255) {
84 0 : nd6log((LOG_ERR,
85 : "%s: invalid hlim (%d) from %s to %s on %u\n",
86 : __func__, ip6->ip6_hlim,
87 : inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src)),
88 : inet_ntop(AF_INET6, &ip6->ip6_dst, dst, sizeof(dst)),
89 : m->m_pkthdr.ph_ifidx));
90 : goto bad;
91 : }
92 :
93 0 : switch (icmp6_type) {
94 : case ND_ROUTER_SOLICIT:
95 : /*
96 : * Don't update the neighbor cache, if src = ::.
97 : * This indicates that the src has no IP address assigned yet.
98 : */
99 0 : if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
100 : goto freeit;
101 :
102 0 : IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off,
103 : icmp6len);
104 0 : if (nd_rs == NULL) {
105 0 : icmp6stat_inc(icp6s_tooshort);
106 0 : return;
107 : }
108 :
109 0 : icmp6len -= sizeof(*nd_rs);
110 0 : nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
111 0 : break;
112 : case ND_ROUTER_ADVERT:
113 0 : if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
114 0 : nd6log((LOG_ERR,
115 : "%s: src %s is not link-local\n", __func__,
116 : inet_ntop(AF_INET6, &saddr6, src, sizeof(src))));
117 : goto bad;
118 : }
119 :
120 0 : IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off,
121 : icmp6len);
122 0 : if (nd_ra == NULL) {
123 0 : icmp6stat_inc(icp6s_tooshort);
124 0 : return;
125 : }
126 :
127 0 : icmp6len -= sizeof(*nd_ra);
128 0 : nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
129 0 : break;
130 : }
131 :
132 0 : if (nd6_options(&ndopts) < 0) {
133 0 : nd6log((LOG_INFO,
134 : "%s: invalid ND option, ignored\n", __func__));
135 : /* nd6_options have incremented stats */
136 : goto freeit;
137 : }
138 :
139 0 : if (ndopts.nd_opts_src_lladdr) {
140 0 : lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
141 0 : lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
142 0 : }
143 :
144 0 : ifp = if_get(m->m_pkthdr.ph_ifidx);
145 0 : if (ifp == NULL)
146 : goto freeit;
147 :
148 0 : if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
149 0 : nd6log((LOG_INFO,
150 : "%s: lladdrlen mismatch for %s (if %d, RA/RS packet %d)\n",
151 : __func__, inet_ntop(AF_INET6, &saddr6, src, sizeof(src)),
152 : ifp->if_addrlen, lladdrlen - 2));
153 0 : if_put(ifp);
154 0 : goto bad;
155 : }
156 :
157 0 : nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, icmp6_type, 0);
158 0 : if_put(ifp);
159 :
160 : freeit:
161 0 : m_freem(m);
162 0 : return;
163 :
164 : bad:
165 0 : icmp6stat_inc(icmp6_type == ND_ROUTER_SOLICIT ? icp6s_badrs :
166 : icp6s_badra);
167 0 : m_freem(m);
168 0 : }
169 :
170 : /*
171 : * Delete all the routing table entries that use the specified gateway.
172 : * XXX: this function causes search through all entries of routing table, so
173 : * it shouldn't be called when acting as a router.
174 : * The gateway must already contain KAME's hack for link-local scope.
175 : */
176 : void
177 0 : rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
178 : {
179 0 : NET_ASSERT_LOCKED();
180 :
181 : /* We'll care only link-local addresses */
182 0 : if (!IN6_IS_ADDR_LINKLOCAL(gateway))
183 : return;
184 :
185 0 : KASSERT(gateway->s6_addr16[1] != 0);
186 :
187 0 : rtable_walk(ifp->if_rdomain, AF_INET6, rt6_deleteroute, gateway);
188 0 : }
189 :
190 : int
191 0 : rt6_deleteroute(struct rtentry *rt, void *arg, unsigned int id)
192 : {
193 : struct ifnet *ifp;
194 0 : struct rt_addrinfo info;
195 0 : struct in6_addr *gate = (struct in6_addr *)arg;
196 0 : struct sockaddr_in6 sa_mask;
197 : int error = 0;
198 :
199 0 : if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
200 0 : return (0);
201 :
202 0 : if (!IN6_ARE_ADDR_EQUAL(gate, &satosin6(rt->rt_gateway)->sin6_addr))
203 0 : return (0);
204 :
205 : /*
206 : * Do not delete a static route.
207 : * XXX: this seems to be a bit ad-hoc. Should we consider the
208 : * 'cloned' bit instead?
209 : */
210 0 : if ((rt->rt_flags & RTF_STATIC) != 0)
211 0 : return (0);
212 :
213 : /*
214 : * We delete only host route. This means, in particular, we don't
215 : * delete default route.
216 : */
217 0 : if ((rt->rt_flags & RTF_HOST) == 0)
218 0 : return (0);
219 :
220 0 : ifp = if_get(rt->rt_ifidx);
221 0 : if (ifp != NULL) {
222 0 : bzero(&info, sizeof(info));
223 0 : info.rti_flags = rt->rt_flags;
224 0 : info.rti_info[RTAX_DST] = rt_key(rt);
225 0 : info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
226 0 : info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask);
227 0 : error = rtrequest_delete(&info, RTP_ANY, ifp, NULL, id);
228 0 : }
229 0 : if_put(ifp);
230 :
231 0 : return (error != 0 ? error : EAGAIN);
232 0 : }
|