nd6_rtr.c revision 1.3 1 1.3 thorpej /* $NetBSD: nd6_rtr.c,v 1.3 1999/07/03 21:30:19 thorpej Exp $ */
2 1.3 thorpej
3 1.2 itojun /*
4 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.2 itojun * All rights reserved.
6 1.2 itojun *
7 1.2 itojun * Redistribution and use in source and binary forms, with or without
8 1.2 itojun * modification, are permitted provided that the following conditions
9 1.2 itojun * are met:
10 1.2 itojun * 1. Redistributions of source code must retain the above copyright
11 1.2 itojun * notice, this list of conditions and the following disclaimer.
12 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 itojun * notice, this list of conditions and the following disclaimer in the
14 1.2 itojun * documentation and/or other materials provided with the distribution.
15 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.2 itojun * may be used to endorse or promote products derived from this software
17 1.2 itojun * without specific prior written permission.
18 1.2 itojun *
19 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.2 itojun * SUCH DAMAGE.
30 1.2 itojun */
31 1.2 itojun
32 1.2 itojun #include <sys/param.h>
33 1.2 itojun #include <sys/systm.h>
34 1.2 itojun #include <sys/malloc.h>
35 1.2 itojun #include <sys/mbuf.h>
36 1.2 itojun #include <sys/socket.h>
37 1.2 itojun #include <sys/sockio.h>
38 1.2 itojun #include <sys/time.h>
39 1.2 itojun #include <sys/kernel.h>
40 1.2 itojun #include <sys/errno.h>
41 1.2 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
42 1.2 itojun #include <sys/ioctl.h>
43 1.2 itojun #endif
44 1.2 itojun #include <sys/syslog.h>
45 1.2 itojun
46 1.2 itojun #include <net/if.h>
47 1.2 itojun #include <net/if_types.h>
48 1.2 itojun #include <net/if_dl.h>
49 1.2 itojun #include <net/route.h>
50 1.2 itojun #include <net/radix.h>
51 1.2 itojun
52 1.2 itojun #include <netinet/in.h>
53 1.2 itojun #include <netinet6/in6_systm.h>
54 1.2 itojun #include <netinet6/in6_var.h>
55 1.2 itojun #include <netinet6/ip6.h>
56 1.2 itojun #include <netinet6/ip6_var.h>
57 1.2 itojun #include <netinet6/nd6.h>
58 1.2 itojun #include <netinet6/icmp6.h>
59 1.2 itojun
60 1.2 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
61 1.2 itojun #define time_second time.tv_sec
62 1.2 itojun #endif
63 1.2 itojun
64 1.2 itojun #define SDL(s) ((struct sockaddr_dl *)s)
65 1.2 itojun
66 1.2 itojun static struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *));
67 1.2 itojun static int prelist_add __P((struct nd_prefix *, struct nd_defrouter *));
68 1.2 itojun static struct nd_prefix *prefix_lookup __P((struct nd_prefix *));
69 1.2 itojun static struct in6_ifaddr *in6_ifadd __P((struct ifnet *, struct in6_addr *,
70 1.2 itojun struct in6_addr *, int));
71 1.2 itojun static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *,
72 1.2 itojun struct nd_defrouter *));
73 1.2 itojun static void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *));
74 1.2 itojun static void pfxrtr_del __P((struct nd_pfxrouter *));
75 1.2 itojun static void pfxlist_onlink_check __P((void));
76 1.2 itojun static void nd6_detach_prefix __P((struct nd_prefix *));
77 1.2 itojun static void nd6_attach_prefix __P((struct nd_prefix *));
78 1.2 itojun
79 1.2 itojun static int in6_are_prefix_equal __P((struct in6_addr *, struct in6_addr *, int));
80 1.2 itojun static void in6_prefixlen2mask __P((struct in6_addr *, int));
81 1.2 itojun static void in6_init_address_ltimes __P((struct nd_prefix *ndpr,
82 1.2 itojun struct in6_addrlifetime *lt6));
83 1.2 itojun
84 1.2 itojun static int rt6_deleteroute __P((struct radix_node *, void *));
85 1.2 itojun
86 1.2 itojun #if 0
87 1.2 itojun extern struct timeval time;
88 1.2 itojun #endif
89 1.2 itojun extern int nd6_recalc_reachtm_interval;
90 1.2 itojun
91 1.2 itojun #if 0
92 1.2 itojun static u_char bmask [] = {
93 1.2 itojun 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe,
94 1.2 itojun };
95 1.2 itojun #endif
96 1.2 itojun
97 1.2 itojun /*
98 1.2 itojun * Receive Router Solicitation Message - just for routers.
99 1.2 itojun * Router solicitation/advertisement is mostly managed by userland program
100 1.2 itojun * (rtadvd) so here we have no function like nd6_ra_output().
101 1.2 itojun *
102 1.2 itojun * Based on RFC 2461
103 1.2 itojun */
104 1.2 itojun void
105 1.2 itojun nd6_rs_input(m, off, icmp6len)
106 1.2 itojun struct mbuf *m;
107 1.2 itojun int off, icmp6len;
108 1.2 itojun {
109 1.2 itojun struct ifnet *ifp = m->m_pkthdr.rcvif;
110 1.2 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
111 1.2 itojun struct nd_router_solicit *nd_rs
112 1.2 itojun = (struct nd_router_solicit *)((caddr_t)ip6 + off);
113 1.2 itojun struct in6_addr saddr6 = ip6->ip6_src;
114 1.2 itojun #if 0
115 1.2 itojun struct in6_addr daddr6 = ip6->ip6_dst;
116 1.2 itojun #endif
117 1.2 itojun char *lladdr = NULL;
118 1.2 itojun int lladdrlen = 0;
119 1.2 itojun #if 0
120 1.2 itojun struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL;
121 1.2 itojun struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
122 1.2 itojun struct rtentry *rt = NULL;
123 1.2 itojun int is_newentry;
124 1.2 itojun #endif
125 1.2 itojun union nd_opts ndopts;
126 1.2 itojun
127 1.2 itojun /* If I'm not a router, ignore it. */
128 1.2 itojun if (ip6_accept_rtadv != 0 || ip6_forwarding != 1)
129 1.2 itojun return;
130 1.2 itojun
131 1.2 itojun /* Sanity checks */
132 1.2 itojun if (ip6->ip6_hlim != 255) {
133 1.2 itojun log(LOG_ERR,
134 1.2 itojun "nd6_rs_input: invalid hlim %d\n", ip6->ip6_hlim);
135 1.2 itojun return;
136 1.2 itojun }
137 1.2 itojun
138 1.2 itojun /*
139 1.2 itojun * Don't update the neighbor cache, if src = ::.
140 1.2 itojun * This indicates that the src has no IP address assigned yet.
141 1.2 itojun */
142 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
143 1.2 itojun return;
144 1.2 itojun
145 1.2 itojun icmp6len -= sizeof(*nd_rs);
146 1.2 itojun nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
147 1.2 itojun if (nd6_options(&ndopts) < 0) {
148 1.2 itojun log(LOG_INFO, "nd6_rs_input: invalid ND option, ignored\n");
149 1.2 itojun return;
150 1.2 itojun }
151 1.2 itojun
152 1.2 itojun if (ndopts.nd_opts_src_lladdr) {
153 1.2 itojun lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
154 1.2 itojun lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
155 1.2 itojun }
156 1.2 itojun
157 1.2 itojun if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
158 1.2 itojun log(LOG_INFO,
159 1.2 itojun "nd6_rs_input: lladdrlen mismatch for %s "
160 1.2 itojun "(if %d, RS packet %d)\n",
161 1.2 itojun ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2);
162 1.2 itojun }
163 1.2 itojun
164 1.2 itojun nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT);
165 1.2 itojun }
166 1.2 itojun
167 1.2 itojun /*
168 1.2 itojun * Receive Router Advertisement Message.
169 1.2 itojun *
170 1.2 itojun * Based on RFC 2461
171 1.2 itojun * TODO: on-link bit on prefix information
172 1.2 itojun * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
173 1.2 itojun */
174 1.2 itojun void
175 1.2 itojun nd6_ra_input(m, off, icmp6len)
176 1.2 itojun struct mbuf *m;
177 1.2 itojun int off, icmp6len;
178 1.2 itojun {
179 1.2 itojun struct ifnet *ifp = m->m_pkthdr.rcvif;
180 1.2 itojun struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
181 1.2 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
182 1.2 itojun struct nd_router_advert *nd_ra =
183 1.2 itojun (struct nd_router_advert *)((caddr_t)ip6 + off);
184 1.2 itojun struct in6_addr saddr6 = ip6->ip6_src;
185 1.2 itojun #if 0
186 1.2 itojun struct in6_addr daddr6 = ip6->ip6_dst;
187 1.2 itojun int flags = nd_ra->nd_ra_flags_reserved;
188 1.2 itojun int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
189 1.2 itojun int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
190 1.2 itojun #endif
191 1.2 itojun union nd_opts ndopts;
192 1.2 itojun struct nd_defrouter *dr;
193 1.2 itojun
194 1.2 itojun if (ip6_accept_rtadv == 0)
195 1.2 itojun return;
196 1.2 itojun
197 1.2 itojun if (ip6->ip6_hlim != 255) {
198 1.2 itojun log(LOG_ERR,
199 1.2 itojun "nd6_ra_input: invalid hlim %d\n", ip6->ip6_hlim);
200 1.2 itojun return;
201 1.2 itojun }
202 1.2 itojun
203 1.2 itojun if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
204 1.2 itojun log(LOG_ERR,
205 1.2 itojun "nd6_ra_input: src %s is not link-local\n",
206 1.2 itojun ip6_sprintf(&saddr6));
207 1.2 itojun return;
208 1.2 itojun }
209 1.2 itojun
210 1.2 itojun icmp6len -= sizeof(*nd_ra);
211 1.2 itojun nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
212 1.2 itojun if (nd6_options(&ndopts) < 0) {
213 1.2 itojun log(LOG_INFO, "nd6_ra_input: invalid ND option, ignored\n");
214 1.2 itojun return;
215 1.2 itojun }
216 1.2 itojun
217 1.2 itojun {
218 1.2 itojun struct nd_defrouter dr0;
219 1.2 itojun u_int32_t advreachable = nd_ra->nd_ra_reachable;
220 1.2 itojun
221 1.2 itojun dr0.rtaddr = saddr6;
222 1.2 itojun dr0.flags = nd_ra->nd_ra_flags_reserved;
223 1.2 itojun dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
224 1.2 itojun dr0.expire = time_second + dr0.rtlifetime;
225 1.2 itojun dr0.ifp = ifp;
226 1.2 itojun /* unspecified or not? (RFC 2461 6.3.4) */
227 1.2 itojun if (advreachable) {
228 1.2 itojun NTOHL(advreachable);
229 1.2 itojun if (advreachable <= MAX_REACHABLE_TIME &&
230 1.2 itojun ndi->basereachable != advreachable) {
231 1.2 itojun ndi->basereachable = advreachable;
232 1.2 itojun ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
233 1.2 itojun ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
234 1.2 itojun }
235 1.2 itojun }
236 1.2 itojun if (nd_ra->nd_ra_retransmit)
237 1.2 itojun ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
238 1.2 itojun if (nd_ra->nd_ra_curhoplimit)
239 1.2 itojun ndi->chlim = nd_ra->nd_ra_curhoplimit;
240 1.2 itojun dr = defrtrlist_update(&dr0);
241 1.2 itojun }
242 1.2 itojun
243 1.2 itojun /*
244 1.2 itojun * prefix
245 1.2 itojun */
246 1.2 itojun if (ndopts.nd_opts_pi) {
247 1.2 itojun struct nd_opt_hdr *pt;
248 1.2 itojun struct nd_opt_prefix_info *pi;
249 1.2 itojun struct nd_prefix pr;
250 1.2 itojun
251 1.2 itojun for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
252 1.2 itojun pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
253 1.2 itojun pt = (struct nd_opt_hdr *)((caddr_t)pt +
254 1.2 itojun (pt->nd_opt_len << 3))) {
255 1.2 itojun if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
256 1.2 itojun continue;
257 1.2 itojun pi = (struct nd_opt_prefix_info *)pt;
258 1.2 itojun
259 1.2 itojun if (pi->nd_opt_pi_len != 4) {
260 1.2 itojun log(LOG_INFO, "nd6_ra_input: invalid option "
261 1.2 itojun "len %d for prefix information option, "
262 1.2 itojun "ignored\n", pi->nd_opt_pi_len);
263 1.2 itojun continue;
264 1.2 itojun }
265 1.2 itojun
266 1.2 itojun if (128 < pi->nd_opt_pi_prefix_len) {
267 1.2 itojun log(LOG_INFO, "nd6_ra_input: invalid prefix "
268 1.2 itojun "len %d for prefix information option, "
269 1.2 itojun "ignored\n", pi->nd_opt_pi_prefix_len);
270 1.2 itojun continue;
271 1.2 itojun }
272 1.2 itojun
273 1.2 itojun if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
274 1.2 itojun || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
275 1.2 itojun log(LOG_INFO, "nd6_ra_input: invalid prefix "
276 1.2 itojun "%s, ignored\n",
277 1.2 itojun ip6_sprintf(&pi->nd_opt_pi_prefix));
278 1.2 itojun continue;
279 1.2 itojun }
280 1.2 itojun
281 1.2 itojun /* aggregatable unicast address, rfc2374 */
282 1.2 itojun if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20
283 1.2 itojun && pi->nd_opt_pi_prefix_len != 64) {
284 1.2 itojun log(LOG_INFO, "nd6_ra_input: invalid prefixlen "
285 1.2 itojun "%d for rfc2374 prefix %s, ignored\n",
286 1.2 itojun pi->nd_opt_pi_prefix_len,
287 1.2 itojun ip6_sprintf(&pi->nd_opt_pi_prefix));
288 1.2 itojun continue;
289 1.2 itojun }
290 1.2 itojun
291 1.2 itojun bzero(&pr, sizeof(pr));
292 1.2 itojun pr.ndpr_prefix.sin6_family = AF_INET6;
293 1.2 itojun pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
294 1.2 itojun pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
295 1.2 itojun pr.ndpr_ifpr.ifpr_prefix =
296 1.2 itojun (struct sockaddr *)&pr.ndpr_prefix;
297 1.2 itojun /* TODO: link into interface prefix list */
298 1.2 itojun
299 1.2 itojun pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
300 1.2 itojun
301 1.2 itojun pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
302 1.2 itojun ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
303 1.2 itojun pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
304 1.2 itojun ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
305 1.2 itojun pr.ndpr_rrf_decrvalid = 1;
306 1.2 itojun pr.ndpr_rrf_decrprefd = 1;
307 1.2 itojun pr.ndpr_origin = PR_ORIG_RA;
308 1.2 itojun pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
309 1.2 itojun pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
310 1.2 itojun pr.ndpr_pltime =
311 1.2 itojun ntohl(pi->nd_opt_pi_preferred_time);
312 1.2 itojun
313 1.2 itojun if (in6_init_prefix_ltimes(&pr))
314 1.2 itojun continue; /* prefix lifetime init failed */
315 1.2 itojun
316 1.2 itojun (void)prelist_update(&pr, dr, m);
317 1.2 itojun }
318 1.2 itojun }
319 1.2 itojun
320 1.2 itojun /*
321 1.2 itojun * MTU
322 1.2 itojun */
323 1.2 itojun if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
324 1.2 itojun u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
325 1.2 itojun
326 1.2 itojun /* lower bound */
327 1.2 itojun if (mtu < IPV6_MMTU) {
328 1.2 itojun log(LOG_INFO, "nd6_ra_input: bogus mtu option "
329 1.2 itojun "mtu=%d sent from %s, ignoring\n",
330 1.2 itojun mtu, ip6_sprintf(&ip6->ip6_src));
331 1.2 itojun goto skip;
332 1.2 itojun }
333 1.2 itojun
334 1.2 itojun /* upper bound */
335 1.2 itojun if (ndi->maxmtu) {
336 1.2 itojun if (mtu <= ndi->maxmtu) {
337 1.2 itojun int change = (ndi->linkmtu != mtu);
338 1.2 itojun
339 1.2 itojun ndi->linkmtu = mtu;
340 1.2 itojun if (change) /* in6_maxmtu may change */
341 1.2 itojun in6_setmaxmtu();
342 1.2 itojun } else {
343 1.2 itojun log(LOG_INFO, "nd6_ra_input: bogus mtu "
344 1.2 itojun "mtu=%d sent from %s; "
345 1.2 itojun "exceeds maxmtu %d, ignoring\n",
346 1.2 itojun mtu, ip6_sprintf(&ip6->ip6_src),
347 1.2 itojun ndi->maxmtu);
348 1.2 itojun }
349 1.2 itojun } else {
350 1.2 itojun log(LOG_INFO, "nd6_ra_input: mtu option "
351 1.2 itojun "mtu=%d sent from %s; maxmtu unknown, "
352 1.2 itojun "ignoring\n",
353 1.2 itojun mtu, ip6_sprintf(&ip6->ip6_src));
354 1.2 itojun }
355 1.2 itojun }
356 1.2 itojun
357 1.2 itojun skip:
358 1.2 itojun
359 1.2 itojun /*
360 1.2 itojun * Src linkaddress
361 1.2 itojun */
362 1.2 itojun {
363 1.2 itojun char *lladdr = NULL;
364 1.2 itojun int lladdrlen = 0;
365 1.2 itojun
366 1.2 itojun if (ndopts.nd_opts_src_lladdr) {
367 1.2 itojun lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
368 1.2 itojun lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
369 1.2 itojun }
370 1.2 itojun
371 1.2 itojun if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
372 1.2 itojun log(LOG_INFO,
373 1.2 itojun "nd6_ra_input: lladdrlen mismatch for %s "
374 1.2 itojun "(if %d, RA packet %d)\n",
375 1.2 itojun ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2);
376 1.2 itojun }
377 1.2 itojun
378 1.2 itojun nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT);
379 1.2 itojun }
380 1.2 itojun }
381 1.2 itojun
382 1.2 itojun /*
383 1.2 itojun * default router list proccessing sub routines
384 1.2 itojun */
385 1.2 itojun void
386 1.2 itojun defrouter_addreq(new)
387 1.2 itojun struct nd_defrouter *new;
388 1.2 itojun {
389 1.2 itojun struct sockaddr_in6 def, mask, gate;
390 1.2 itojun int s;
391 1.2 itojun #if 0
392 1.2 itojun register struct radix_node *rn;
393 1.2 itojun register struct radix_node_head *rnh;
394 1.2 itojun struct sockaddr *ndst;
395 1.2 itojun struct ifnet *ifp = new->ifp;
396 1.2 itojun struct ifaddr *ifa;
397 1.2 itojun struct rtentry *rt;
398 1.2 itojun extern struct pool rtentry_pool;
399 1.2 itojun #endif
400 1.2 itojun
401 1.2 itojun Bzero(&def, sizeof(def));
402 1.2 itojun Bzero(&mask, sizeof(mask));
403 1.2 itojun Bzero(&gate, sizeof(gate));
404 1.2 itojun
405 1.2 itojun def.sin6_len = mask.sin6_len = gate.sin6_len
406 1.2 itojun = sizeof(struct sockaddr_in6);
407 1.2 itojun def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
408 1.2 itojun gate.sin6_addr = new->rtaddr;
409 1.2 itojun
410 1.2 itojun #if 1
411 1.2 itojun s = splnet();
412 1.2 itojun (void)rtrequest(RTM_ADD, (struct sockaddr *)&def,
413 1.2 itojun (struct sockaddr *)&gate, (struct sockaddr *)&mask,
414 1.2 itojun RTF_GATEWAY, NULL);
415 1.2 itojun splx(s);
416 1.2 itojun return;
417 1.2 itojun #else
418 1.2 itojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
419 1.2 itojun if (!ifa)
420 1.2 itojun return;
421 1.2 itojun if ((rnh = rt_tables[AF_INET6]) == 0)
422 1.2 itojun return;
423 1.2 itojun
424 1.2 itojun s = splnet();
425 1.2 itojun #if 0
426 1.2 itojun R_Malloc(rt, struct rtentry *, sizeof(*rt));
427 1.2 itojun #else
428 1.2 itojun rt = pool_get(&rtentry_pool, PR_NOWAIT);
429 1.2 itojun #endif
430 1.2 itojun if (!rt)
431 1.2 itojun goto bad;
432 1.2 itojun Bzero(rt, sizeof(*rt));
433 1.2 itojun rt->rt_flags = RTF_UP | RTF_GATEWAY;
434 1.2 itojun if (rt_setgate(rt, (struct sockaddr *)&def, (struct sockaddr *)&gate)){
435 1.2 itojun Free(rt);
436 1.2 itojun goto bad;
437 1.2 itojun }
438 1.2 itojun ndst = rt_key(rt);
439 1.2 itojun Bcopy(&def, ndst, sizeof(def));
440 1.2 itojun rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)&mask,
441 1.2 itojun rnh, rt->rt_nodes);
442 1.2 itojun if (rn == 0) {
443 1.2 itojun Free(rt_key(rt));
444 1.2 itojun Free(rt);
445 1.2 itojun goto bad;
446 1.2 itojun }
447 1.2 itojun ifa->ifa_refcnt++;
448 1.2 itojun rt->rt_ifa = ifa;
449 1.2 itojun rt->rt_ifp = ifp;
450 1.2 itojun rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu;
451 1.2 itojun /* xxx
452 1.2 itojun * many codes should be stolen from route.c
453 1.2 itojun */
454 1.2 itojun bad:
455 1.2 itojun splx(s);
456 1.2 itojun return;
457 1.2 itojun #endif
458 1.2 itojun }
459 1.2 itojun
460 1.2 itojun struct nd_defrouter *
461 1.2 itojun defrouter_lookup(addr, ifp)
462 1.2 itojun struct in6_addr *addr;
463 1.2 itojun struct ifnet *ifp;
464 1.2 itojun {
465 1.2 itojun struct nd_defrouter *dr;
466 1.2 itojun
467 1.2 itojun for(dr = nd_defrouter.lh_first; dr; dr = dr->dr_next)
468 1.2 itojun if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
469 1.2 itojun return(dr);
470 1.2 itojun
471 1.2 itojun return(NULL); /* search failed */
472 1.2 itojun }
473 1.2 itojun
474 1.2 itojun void
475 1.2 itojun defrouter_delreq(dr, dofree)
476 1.2 itojun struct nd_defrouter *dr;
477 1.2 itojun int dofree;
478 1.2 itojun {
479 1.2 itojun struct sockaddr_in6 def, mask, gate;
480 1.2 itojun
481 1.2 itojun Bzero(&def, sizeof(def));
482 1.2 itojun Bzero(&mask, sizeof(mask));
483 1.2 itojun Bzero(&gate, sizeof(gate));
484 1.2 itojun
485 1.2 itojun def.sin6_len = mask.sin6_len = gate.sin6_len
486 1.2 itojun = sizeof(struct sockaddr_in6);
487 1.2 itojun def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
488 1.2 itojun gate.sin6_addr = dr->rtaddr;
489 1.2 itojun
490 1.2 itojun rtrequest(RTM_DELETE, (struct sockaddr *)&def,
491 1.2 itojun (struct sockaddr *)&gate,
492 1.2 itojun (struct sockaddr *)&mask,
493 1.2 itojun RTF_GATEWAY, (struct rtentry **)0);
494 1.2 itojun
495 1.2 itojun if (dofree)
496 1.2 itojun free(dr, M_IP6NDP);
497 1.2 itojun
498 1.2 itojun if (nd_defrouter.lh_first)
499 1.2 itojun defrouter_addreq(nd_defrouter.lh_first);
500 1.2 itojun
501 1.2 itojun /*
502 1.2 itojun * xxx update the Destination Cache entries for all
503 1.2 itojun * destinations using that neighbor as a router (7.2.5)
504 1.2 itojun */
505 1.2 itojun }
506 1.2 itojun
507 1.2 itojun void
508 1.2 itojun defrtrlist_del(dr)
509 1.2 itojun struct nd_defrouter *dr;
510 1.2 itojun {
511 1.2 itojun struct nd_defrouter *deldr = NULL;
512 1.2 itojun struct nd_prefix *pr;
513 1.2 itojun
514 1.2 itojun /*
515 1.2 itojun * Flush all the routing table entries that use the router
516 1.2 itojun * as a next hop.
517 1.2 itojun */
518 1.2 itojun if (!ip6_forwarding && ip6_accept_rtadv) {
519 1.2 itojun /* above is a good condition? */
520 1.2 itojun rt6_flush(&dr->rtaddr, dr->ifp);
521 1.2 itojun }
522 1.2 itojun
523 1.2 itojun if (dr == nd_defrouter.lh_first)
524 1.2 itojun deldr = dr; /* The router is primary. */
525 1.2 itojun
526 1.2 itojun LIST_REMOVE(dr, dr_entry);
527 1.2 itojun
528 1.2 itojun /*
529 1.2 itojun * Also delete all the pointers to the router in each prefix lists.
530 1.2 itojun */
531 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
532 1.2 itojun struct nd_pfxrouter *pfxrtr;
533 1.2 itojun if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
534 1.2 itojun pfxrtr_del(pfxrtr);
535 1.2 itojun }
536 1.2 itojun pfxlist_onlink_check();
537 1.2 itojun
538 1.2 itojun /*
539 1.2 itojun * If the router is the primary one, delete the default route
540 1.2 itojun * entry in the routing table.
541 1.2 itojun */
542 1.2 itojun if (deldr)
543 1.2 itojun defrouter_delreq(deldr, 0);
544 1.2 itojun free(dr, M_IP6NDP);
545 1.2 itojun }
546 1.2 itojun
547 1.2 itojun static struct nd_defrouter *
548 1.2 itojun defrtrlist_update(new)
549 1.2 itojun struct nd_defrouter *new;
550 1.2 itojun {
551 1.2 itojun struct nd_defrouter *dr, *n;
552 1.2 itojun int s = splnet();
553 1.2 itojun
554 1.2 itojun if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
555 1.2 itojun /* entry exists */
556 1.2 itojun if (new->rtlifetime == 0) {
557 1.2 itojun defrtrlist_del(dr);
558 1.2 itojun dr = NULL;
559 1.2 itojun } else {
560 1.2 itojun /* override */
561 1.2 itojun dr->flags = new->flags; /* xxx flag check */
562 1.2 itojun dr->rtlifetime = new->rtlifetime;
563 1.2 itojun dr->expire = new->expire;
564 1.2 itojun }
565 1.2 itojun splx(s);
566 1.2 itojun return(dr);
567 1.2 itojun }
568 1.2 itojun
569 1.2 itojun /* entry does not exist */
570 1.2 itojun if (new->rtlifetime == 0) {
571 1.2 itojun splx(s);
572 1.2 itojun return(NULL);
573 1.2 itojun }
574 1.2 itojun
575 1.2 itojun n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
576 1.2 itojun if (n == NULL) {
577 1.2 itojun splx(s);
578 1.2 itojun return(NULL);
579 1.2 itojun }
580 1.2 itojun bzero(n, sizeof(*n));
581 1.2 itojun *n = *new;
582 1.2 itojun if (nd_defrouter.lh_first == NULL) {
583 1.2 itojun LIST_INSERT_HEAD(&nd_defrouter, n, dr_entry);
584 1.2 itojun defrouter_addreq(n);
585 1.2 itojun } else {
586 1.2 itojun LIST_INSERT_AFTER(nd_defrouter.lh_first, n, dr_entry);
587 1.2 itojun defrouter_addreq(n);
588 1.2 itojun }
589 1.2 itojun splx(s);
590 1.2 itojun
591 1.2 itojun return(n);
592 1.2 itojun }
593 1.2 itojun
594 1.2 itojun static struct nd_pfxrouter *
595 1.2 itojun pfxrtr_lookup(pr, dr)
596 1.2 itojun struct nd_prefix *pr;
597 1.2 itojun struct nd_defrouter *dr;
598 1.2 itojun {
599 1.2 itojun struct nd_pfxrouter *search;
600 1.2 itojun
601 1.2 itojun for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
602 1.2 itojun if (search->router == dr)
603 1.2 itojun break;
604 1.2 itojun }
605 1.2 itojun
606 1.2 itojun return(search);
607 1.2 itojun }
608 1.2 itojun
609 1.2 itojun static void
610 1.2 itojun pfxrtr_add(pr, dr)
611 1.2 itojun struct nd_prefix *pr;
612 1.2 itojun struct nd_defrouter *dr;
613 1.2 itojun {
614 1.2 itojun struct nd_pfxrouter *new;
615 1.2 itojun
616 1.2 itojun new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
617 1.2 itojun if (new == NULL)
618 1.2 itojun return;
619 1.2 itojun bzero(new, sizeof(*new));
620 1.2 itojun new->router = dr;
621 1.2 itojun
622 1.2 itojun LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
623 1.2 itojun
624 1.2 itojun pfxlist_onlink_check();
625 1.2 itojun }
626 1.2 itojun
627 1.2 itojun static void
628 1.2 itojun pfxrtr_del(pfr)
629 1.2 itojun struct nd_pfxrouter *pfr;
630 1.2 itojun {
631 1.2 itojun LIST_REMOVE(pfr, pfr_entry);
632 1.2 itojun free(pfr, M_IP6NDP);
633 1.2 itojun }
634 1.2 itojun
635 1.2 itojun static struct nd_prefix *
636 1.2 itojun prefix_lookup(pr)
637 1.2 itojun struct nd_prefix *pr;
638 1.2 itojun {
639 1.2 itojun struct nd_prefix *search;
640 1.2 itojun
641 1.2 itojun for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
642 1.2 itojun if (pr->ndpr_ifp == search->ndpr_ifp &&
643 1.2 itojun pr->ndpr_plen == search->ndpr_plen &&
644 1.2 itojun in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
645 1.2 itojun &search->ndpr_prefix.sin6_addr,
646 1.2 itojun pr->ndpr_plen)
647 1.2 itojun ) {
648 1.2 itojun break;
649 1.2 itojun }
650 1.2 itojun }
651 1.2 itojun
652 1.2 itojun return(search);
653 1.2 itojun }
654 1.2 itojun
655 1.2 itojun static int
656 1.2 itojun prelist_add(pr, dr)
657 1.2 itojun struct nd_prefix *pr;
658 1.2 itojun struct nd_defrouter *dr;
659 1.2 itojun {
660 1.2 itojun struct nd_prefix *new;
661 1.2 itojun int i, s;
662 1.2 itojun
663 1.2 itojun new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
664 1.2 itojun if (new == NULL)
665 1.2 itojun return ENOMEM;
666 1.2 itojun bzero(new, sizeof(*new));
667 1.2 itojun *new = *pr;
668 1.2 itojun /* let ndpr_ifpr.ifpr_prefix point ndpr_prefix. */
669 1.2 itojun new->ndpr_ifpr.ifpr_prefix = (struct sockaddr *)&new->ndpr_prefix;
670 1.2 itojun
671 1.2 itojun /* initilization */
672 1.2 itojun new->ndpr_statef_onlink = pr->ndpr_statef_onlink;
673 1.2 itojun LIST_INIT(&new->ndpr_advrtrs);
674 1.2 itojun in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
675 1.2 itojun /* make prefix in the canonical form */
676 1.2 itojun for (i = 0; i < 4; i++)
677 1.2 itojun new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
678 1.2 itojun new->ndpr_mask.s6_addr32[i];
679 1.2 itojun
680 1.2 itojun /* xxx ND_OPT_PI_FLAG_ONLINK processing */
681 1.2 itojun
682 1.2 itojun s = splnet();
683 1.2 itojun /* link ndpr_entry to if_prefixlist */
684 1.2 itojun {
685 1.2 itojun struct ifnet *ifp = new->ndpr_ifp;
686 1.2 itojun struct ifprefix *ifpr;
687 1.2 itojun
688 1.2 itojun if ((ifpr = ifp->if_prefixlist) != NULL) {
689 1.2 itojun for ( ; ifpr->ifpr_next; ifpr = ifpr->ifpr_next)
690 1.2 itojun continue;
691 1.2 itojun ifpr->ifpr_next = ndpr2ifpr(new);
692 1.2 itojun }
693 1.2 itojun else
694 1.2 itojun ifp->if_prefixlist = ndpr2ifpr(new);
695 1.2 itojun }
696 1.2 itojun /* link ndpr_entry to nd_prefix list */
697 1.2 itojun LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
698 1.2 itojun splx(s);
699 1.2 itojun
700 1.2 itojun if (dr)
701 1.2 itojun pfxrtr_add(new, dr);
702 1.2 itojun
703 1.2 itojun return 0;
704 1.2 itojun }
705 1.2 itojun
706 1.2 itojun void
707 1.2 itojun prelist_remove(pr)
708 1.2 itojun struct nd_prefix *pr;
709 1.2 itojun {
710 1.2 itojun struct nd_pfxrouter *pfr, *next;
711 1.2 itojun int s;
712 1.2 itojun
713 1.2 itojun s = splnet();
714 1.2 itojun /* unlink ndpr_entry from if_prefixlist */
715 1.2 itojun {
716 1.2 itojun struct ifnet *ifp = pr->ndpr_ifp;
717 1.2 itojun struct ifprefix *ifpr;
718 1.2 itojun
719 1.2 itojun if ((ifpr = ifp->if_prefixlist) == ndpr2ifpr(pr))
720 1.2 itojun ifp->if_prefixlist = ifpr->ifpr_next;
721 1.2 itojun else {
722 1.2 itojun while (ifpr->ifpr_next &&
723 1.2 itojun (ifpr->ifpr_next != ndpr2ifpr(pr)))
724 1.2 itojun ifpr = ifpr->ifpr_next;
725 1.2 itojun if (ifpr->ifpr_next)
726 1.2 itojun ifpr->ifpr_next = ndpr2ifpr(pr)->ifpr_next;
727 1.2 itojun else
728 1.2 itojun printf("Couldn't unlink nd_prefix from ifp\n");
729 1.2 itojun }
730 1.2 itojun }
731 1.2 itojun /* unlink ndpr_entry from nd_prefix list */
732 1.2 itojun LIST_REMOVE(pr, ndpr_entry);
733 1.2 itojun splx(s);
734 1.2 itojun
735 1.2 itojun /* free list of routers that adversed the prefix */
736 1.2 itojun for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
737 1.2 itojun next = pfr->pfr_next;
738 1.2 itojun
739 1.2 itojun free(pfr, M_IP6NDP);
740 1.2 itojun }
741 1.2 itojun free(pr, M_IP6NDP);
742 1.2 itojun
743 1.2 itojun pfxlist_onlink_check();
744 1.2 itojun }
745 1.2 itojun
746 1.2 itojun /*
747 1.2 itojun * NOTE: We set address lifetime to keep
748 1.2 itojun * address lifetime <= prefix lifetime
749 1.2 itojun * invariant. This is to simplify on-link determination code.
750 1.2 itojun * If onlink determination is udated, this routine may have to be updated too.
751 1.2 itojun */
752 1.2 itojun int
753 1.2 itojun prelist_update(new, dr, m)
754 1.2 itojun struct nd_prefix *new;
755 1.2 itojun struct nd_defrouter *dr; /* may be NULL */
756 1.2 itojun struct mbuf *m;
757 1.2 itojun {
758 1.2 itojun struct in6_ifaddr *ia6 = NULL;
759 1.2 itojun struct nd_prefix *pr;
760 1.2 itojun int s = splnet();
761 1.2 itojun int error = 0;
762 1.2 itojun int auth;
763 1.2 itojun struct in6_addrlifetime *lt6;
764 1.2 itojun
765 1.2 itojun if (m) {
766 1.2 itojun /*
767 1.2 itojun * Authenticity for NA consists authentication for
768 1.2 itojun * both IP header and IP datagrams, doesn't it ?
769 1.2 itojun */
770 1.2 itojun auth = (m->m_flags & M_AUTHIPHDR
771 1.2 itojun && m->m_flags & M_AUTHIPDGM) ? 1 : 0;
772 1.2 itojun } else
773 1.2 itojun auth = 0;
774 1.2 itojun
775 1.2 itojun if ((pr = prefix_lookup(new)) != NULL) {
776 1.2 itojun if (pr->ndpr_ifp != new->ndpr_ifp) {
777 1.2 itojun error = EADDRNOTAVAIL;
778 1.2 itojun goto end;
779 1.2 itojun }
780 1.2 itojun
781 1.2 itojun /*
782 1.2 itojun * If the origin of the already-installed prefix is more
783 1.2 itojun * preferable than the new one, ignore installation request.
784 1.2 itojun */
785 1.2 itojun if (pr->ndpr_origin > new->ndpr_origin) {
786 1.2 itojun error = EPERM;
787 1.2 itojun goto end;
788 1.2 itojun }
789 1.2 itojun
790 1.2 itojun /* update prefix information */
791 1.2 itojun pr->ndpr_flags.prf_ra = new->ndpr_flags.prf_ra;
792 1.2 itojun if (pr->ndpr_origin >= PR_ORIG_RR)
793 1.2 itojun pr->ndpr_flags.prf_rr = new->ndpr_flags.prf_rr;
794 1.2 itojun pr->ndpr_vltime = new->ndpr_vltime;
795 1.2 itojun pr->ndpr_pltime = new->ndpr_pltime;
796 1.2 itojun pr->ndpr_preferred = new->ndpr_preferred;
797 1.2 itojun pr->ndpr_expire = new->ndpr_expire;
798 1.2 itojun pr->ndpr_statef_delmark = 0; /* cancel deletion */
799 1.2 itojun
800 1.2 itojun /*
801 1.2 itojun * RFC 2462 5.5.3 (d) or (e)
802 1.2 itojun * We got a prefix which we have seen in the past.
803 1.2 itojun */
804 1.2 itojun if (!new->ndpr_raf_auto)
805 1.2 itojun goto noautoconf1;
806 1.2 itojun
807 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
808 1.2 itojun ia6 = NULL;
809 1.2 itojun else
810 1.2 itojun ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
811 1.2 itojun
812 1.2 itojun if (ia6 == NULL) {
813 1.2 itojun /*
814 1.2 itojun * Special case:
815 1.2 itojun * (1) We have seen the prefix advertised before, but
816 1.2 itojun * we have never performed autoconfig for this prefix.
817 1.2 itojun * This is because Autonomous bit was 0 previously, or
818 1.2 itojun * autoconfig failed due to some other reasons.
819 1.2 itojun * (2) We have seen the prefix advertised before and
820 1.2 itojun * we have performed autoconfig in the past, but
821 1.2 itojun * we seem to have no interface address right now.
822 1.2 itojun * This is because the interface address have expired.
823 1.2 itojun *
824 1.2 itojun * This prefix is fresh, with respect to autoconfig
825 1.2 itojun * process.
826 1.2 itojun *
827 1.2 itojun * Add an address based on RFC 2462 5.5.3 (d).
828 1.2 itojun */
829 1.2 itojun ia6 = in6_ifadd(pr->ndpr_ifp,
830 1.2 itojun &pr->ndpr_prefix.sin6_addr, &pr->ndpr_addr,
831 1.2 itojun new->ndpr_plen);
832 1.2 itojun if (!ia6) {
833 1.2 itojun error = EADDRNOTAVAIL;
834 1.2 itojun log(LOG_ERR, "prelist_update: failed to add a "
835 1.2 itojun "new address\n");
836 1.2 itojun goto noautoconf1;
837 1.2 itojun }
838 1.2 itojun
839 1.2 itojun lt6 = &ia6->ia6_lifetime;
840 1.2 itojun
841 1.2 itojun /* address lifetime <= prefix lifetime */
842 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
843 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
844 1.2 itojun in6_init_address_ltimes(new, lt6);
845 1.2 itojun } else {
846 1.2 itojun #define TWOHOUR (120*60)
847 1.2 itojun /*
848 1.2 itojun * We have seen the prefix before, and we have added
849 1.2 itojun * interface address in the past. We still have
850 1.2 itojun * the interface address assigned.
851 1.2 itojun *
852 1.2 itojun * update address lifetime based on RFC 2462
853 1.2 itojun * 5.5.3 (e).
854 1.2 itojun */
855 1.2 itojun int update = 0;
856 1.2 itojun
857 1.2 itojun lt6 = &ia6->ia6_lifetime;
858 1.2 itojun
859 1.2 itojun #if 0 /* RFC 2462 5.5.3 (e) */
860 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
861 1.2 itojun if (TWOHOUR < new->ndpr_vltime
862 1.2 itojun || lt6pr->nd < new->ndpr_vltime) {
863 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
864 1.2 itojun update++;
865 1.2 itojun } else if (auth
866 1.2 itojun && lt6->ia6t_vltime <= TWOHOUR0
867 1.2 itojun && new->ndpr_vltime <= lt6->ia6t_vltime) {
868 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
869 1.2 itojun update++;
870 1.2 itojun } else {
871 1.2 itojun lt6->ia6t_vltime = TWOHOUR;
872 1.2 itojun update++;
873 1.2 itojun }
874 1.2 itojun if (TWOHOUR < new->ndpr_pltime
875 1.2 itojun || lt6->ia6t_pltime < new->ndpr_pltime) {
876 1.2 itojun new->ndpr_apltime = new->ndpr_pltime;
877 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
878 1.2 itojun update++;
879 1.2 itojun } else if (auth
880 1.2 itojun && lt6->ia6t_pltime <= TWOHOUR
881 1.2 itojun && new->ndpr_pltime <= lt6->ia6t_pltime) {
882 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
883 1.2 itojun update++;
884 1.2 itojun } else {
885 1.2 itojun lt6->ia6t_pltime = TWOHOUR;
886 1.2 itojun update++;
887 1.2 itojun }
888 1.2 itojun
889 1.2 itojun #else /* update from Jim Bound, (ipng 6712) */
890 1.2 itojun if (TWOHOUR < new->ndpr_vltime
891 1.2 itojun || lt6->ia6t_vltime < new->ndpr_vltime) {
892 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
893 1.2 itojun update++;
894 1.2 itojun } else if (auth) {
895 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
896 1.2 itojun update++;
897 1.2 itojun }
898 1.2 itojun if (TWOHOUR < new->ndpr_pltime
899 1.2 itojun || lt6->ia6t_pltime < new->ndpr_pltime) {
900 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
901 1.2 itojun update++;
902 1.2 itojun } else if (auth) {
903 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
904 1.2 itojun update++;
905 1.2 itojun }
906 1.2 itojun #endif
907 1.2 itojun if (update)
908 1.2 itojun in6_init_address_ltimes(new, lt6);
909 1.2 itojun }
910 1.2 itojun
911 1.2 itojun noautoconf1:
912 1.2 itojun
913 1.2 itojun #if 0
914 1.2 itojun /* address lifetime expire processing, RFC 2462 5.5.4. */
915 1.2 itojun if (pr->ndpr_preferred && pr->ndpr_preferred < time_second) {
916 1.2 itojun struct in6_ifaddr *ia6;
917 1.2 itojun
918 1.2 itojun ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
919 1.2 itojun if (ia6)
920 1.2 itojun ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
921 1.2 itojun }
922 1.2 itojun #endif
923 1.2 itojun
924 1.2 itojun if (dr && pfxrtr_lookup(pr, dr) == NULL)
925 1.2 itojun pfxrtr_add(pr, dr);
926 1.2 itojun } else {
927 1.2 itojun int error_tmp;
928 1.2 itojun
929 1.2 itojun if (new->ndpr_vltime == 0) goto end;
930 1.2 itojun
931 1.2 itojun bzero(&new->ndpr_addr, sizeof(struct in6_addr));
932 1.2 itojun
933 1.2 itojun /*
934 1.2 itojun * RFC 2462 5.5.3 (d)
935 1.2 itojun * We got a fresh prefix. Perform some sanity checks
936 1.2 itojun * and add an interface address by appending interface ID
937 1.2 itojun * to the advertised prefix.
938 1.2 itojun */
939 1.2 itojun if (!new->ndpr_raf_auto)
940 1.2 itojun goto noautoconf2;
941 1.2 itojun
942 1.2 itojun ia6 = in6_ifadd(new->ndpr_ifp, &new->ndpr_prefix.sin6_addr,
943 1.2 itojun &new->ndpr_addr, new->ndpr_plen);
944 1.2 itojun if (!ia6) {
945 1.2 itojun error = EADDRNOTAVAIL;
946 1.2 itojun log(LOG_ERR, "prelist_update: "
947 1.2 itojun "failed to add a new address\n");
948 1.2 itojun goto noautoconf2;
949 1.2 itojun }
950 1.2 itojun /* set onlink bit if an interface route is configured */
951 1.2 itojun new->ndpr_statef_onlink = (ia6->ia_flags & IFA_ROUTE) ? 1 : 0;
952 1.2 itojun
953 1.2 itojun lt6 = &ia6->ia6_lifetime;
954 1.2 itojun
955 1.2 itojun /* address lifetime <= prefix lifetime */
956 1.2 itojun lt6->ia6t_vltime = new->ndpr_vltime;
957 1.2 itojun lt6->ia6t_pltime = new->ndpr_pltime;
958 1.2 itojun in6_init_address_ltimes(new, lt6);
959 1.2 itojun
960 1.2 itojun noautoconf2:
961 1.2 itojun error_tmp = prelist_add(new, dr);
962 1.2 itojun error = error_tmp ? error_tmp : error;
963 1.2 itojun }
964 1.2 itojun
965 1.2 itojun end:
966 1.2 itojun splx(s);
967 1.2 itojun return error;
968 1.2 itojun }
969 1.2 itojun
970 1.2 itojun /*
971 1.2 itojun * Check if each prefix in the prefix list has at least one available router
972 1.2 itojun * that advertised the prefix.
973 1.2 itojun * If the check fails, the prefix may be off-link because, for example,
974 1.2 itojun * we have moved from the network but the lifetime of the prefix has not
975 1.2 itojun * been expired yet. So we should not use the prefix if there is another
976 1.2 itojun * prefix that has an available router.
977 1.2 itojun * But if there is no prefix that has an availble router, we still regards
978 1.2 itojun * all the prefixes as on-link. This is because we can't tell if all the
979 1.2 itojun * routers are simply dead or if we really moved from the network and there
980 1.2 itojun * is no router around us.
981 1.2 itojun */
982 1.2 itojun static void
983 1.2 itojun pfxlist_onlink_check()
984 1.2 itojun {
985 1.2 itojun struct nd_prefix *pr;
986 1.2 itojun
987 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next)
988 1.2 itojun if (pr->ndpr_advrtrs.lh_first) /* pr has an available router */
989 1.2 itojun break;
990 1.2 itojun
991 1.2 itojun if (pr) {
992 1.2 itojun /*
993 1.2 itojun * There is at least one prefix that has a router. First,
994 1.2 itojun * detach prefixes which has no advertising router and then
995 1.2 itojun * attach other prefixes. The order is important since an
996 1.2 itojun * attached prefix and a detached prefix may have a same
997 1.2 itojun * interface route.
998 1.2 itojun */
999 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1000 1.2 itojun if (pr->ndpr_advrtrs.lh_first == NULL &&
1001 1.2 itojun pr->ndpr_statef_onlink) {
1002 1.2 itojun pr->ndpr_statef_onlink = 0;
1003 1.2 itojun nd6_detach_prefix(pr);
1004 1.2 itojun }
1005 1.2 itojun }
1006 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1007 1.2 itojun if (pr->ndpr_advrtrs.lh_first &&
1008 1.2 itojun pr->ndpr_statef_onlink == 0)
1009 1.2 itojun nd6_attach_prefix(pr);
1010 1.2 itojun }
1011 1.2 itojun }
1012 1.2 itojun else {
1013 1.2 itojun /* there is no prefix that has a router */
1014 1.2 itojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next)
1015 1.2 itojun if (pr->ndpr_statef_onlink == 0)
1016 1.2 itojun nd6_attach_prefix(pr);
1017 1.2 itojun }
1018 1.2 itojun }
1019 1.2 itojun
1020 1.2 itojun static void
1021 1.2 itojun nd6_detach_prefix(pr)
1022 1.2 itojun struct nd_prefix *pr;
1023 1.2 itojun {
1024 1.2 itojun struct in6_ifaddr *ia6;
1025 1.2 itojun struct sockaddr_in6 sa6, mask6;
1026 1.2 itojun
1027 1.2 itojun /*
1028 1.2 itojun * Delete the interface route associated with the prefix.
1029 1.2 itojun */
1030 1.2 itojun bzero(&sa6, sizeof(sa6));
1031 1.2 itojun sa6.sin6_family = AF_INET6;
1032 1.2 itojun sa6.sin6_len = sizeof(sa6);
1033 1.2 itojun bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1034 1.2 itojun sizeof(struct in6_addr));
1035 1.2 itojun bzero(&mask6, sizeof(mask6));
1036 1.2 itojun mask6.sin6_family = AF_INET6;
1037 1.2 itojun mask6.sin6_len = sizeof(sa6);
1038 1.2 itojun bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1039 1.2 itojun {
1040 1.2 itojun int e;
1041 1.2 itojun
1042 1.2 itojun e = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1043 1.2 itojun (struct sockaddr *)&mask6, 0, NULL);
1044 1.2 itojun if (e) {
1045 1.2 itojun log(LOG_ERR,
1046 1.2 itojun "nd6_detach_prefix: failed to delete route: "
1047 1.2 itojun "%s/%d (errno = %d)\n",
1048 1.2 itojun ip6_sprintf(&sa6.sin6_addr),
1049 1.2 itojun pr->ndpr_ifpr.ifpr_plen,
1050 1.2 itojun e);
1051 1.2 itojun }
1052 1.2 itojun }
1053 1.2 itojun
1054 1.2 itojun /*
1055 1.2 itojun * Mark the address derived from the prefix detached so that
1056 1.2 itojun * it won't be used as a source address for a new connection.
1057 1.2 itojun */
1058 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1059 1.2 itojun ia6 = NULL;
1060 1.2 itojun else
1061 1.2 itojun ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
1062 1.2 itojun if (ia6)
1063 1.2 itojun ia6->ia6_flags |= IN6_IFF_DETACHED;
1064 1.2 itojun }
1065 1.2 itojun
1066 1.2 itojun static void
1067 1.2 itojun nd6_attach_prefix(pr)
1068 1.2 itojun struct nd_prefix *pr;
1069 1.2 itojun {
1070 1.2 itojun struct ifaddr *ifa;
1071 1.2 itojun struct in6_ifaddr *ia6;
1072 1.2 itojun
1073 1.2 itojun /*
1074 1.2 itojun * Add the interface route associated with the prefix(if necessary)
1075 1.2 itojun * Should we consider if the L bit is set in pr->ndpr_flags?
1076 1.2 itojun */
1077 1.2 itojun ifa = ifaof_ifpforaddr((struct sockaddr *)&pr->ndpr_prefix,
1078 1.2 itojun pr->ndpr_ifp);
1079 1.2 itojun if (ifa == NULL) {
1080 1.2 itojun log(LOG_ERR,
1081 1.2 itojun "nd6_attach_prefix: failed to find any ifaddr"
1082 1.2 itojun " to add route for a prefix(%s/%d)\n",
1083 1.2 itojun ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen);
1084 1.2 itojun }
1085 1.2 itojun else {
1086 1.2 itojun int e;
1087 1.2 itojun struct sockaddr_in6 mask6;
1088 1.2 itojun
1089 1.2 itojun bzero(&mask6, sizeof(mask6));
1090 1.2 itojun mask6.sin6_family = AF_INET6;
1091 1.2 itojun mask6.sin6_len = sizeof(mask6);
1092 1.2 itojun mask6.sin6_addr = pr->ndpr_mask;
1093 1.2 itojun e = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1094 1.2 itojun ifa->ifa_addr, (struct sockaddr *)&mask6,
1095 1.2 itojun ifa->ifa_flags, NULL);
1096 1.2 itojun if (e == 0)
1097 1.2 itojun pr->ndpr_statef_onlink = 1;
1098 1.2 itojun else {
1099 1.2 itojun log(LOG_ERR,
1100 1.2 itojun "nd6_attach_prefix: failed to add route for"
1101 1.2 itojun " a prefix(%s/%d), errno = %d\n",
1102 1.2 itojun ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen, e);
1103 1.2 itojun }
1104 1.2 itojun }
1105 1.2 itojun
1106 1.2 itojun /*
1107 1.2 itojun * Now the address derived from the prefix can be used as a source
1108 1.2 itojun * for a new connection, so clear the detached flag.
1109 1.2 itojun */
1110 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1111 1.2 itojun ia6 = NULL;
1112 1.2 itojun else
1113 1.2 itojun ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
1114 1.2 itojun if (ia6) {
1115 1.2 itojun ia6->ia6_flags &= ~IN6_IFF_DETACHED;
1116 1.2 itojun if (pr->ndpr_statef_onlink)
1117 1.2 itojun ia6->ia_flags |= IFA_ROUTE;
1118 1.2 itojun }
1119 1.2 itojun }
1120 1.2 itojun
1121 1.2 itojun static struct in6_ifaddr *
1122 1.2 itojun in6_ifadd(ifp, in6, addr, prefixlen)
1123 1.2 itojun struct ifnet *ifp;
1124 1.2 itojun struct in6_addr *in6;
1125 1.2 itojun struct in6_addr *addr;
1126 1.2 itojun int prefixlen; /* prefix len of the new prefix in "in6" */
1127 1.2 itojun {
1128 1.2 itojun struct ifaddr *ifa;
1129 1.2 itojun struct in6_ifaddr *ia, *ib, *oia;
1130 1.2 itojun int s, error;
1131 1.2 itojun struct in6_addr mask;
1132 1.2 itojun
1133 1.2 itojun in6_len2mask(&mask, prefixlen);
1134 1.2 itojun
1135 1.2 itojun /* find link-local address (will be interface ID) */
1136 1.2 itojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
1137 1.2 itojun if (ifa)
1138 1.2 itojun ib = (struct in6_ifaddr *)ifa;
1139 1.2 itojun else
1140 1.2 itojun return NULL;
1141 1.2 itojun
1142 1.2 itojun #if 0 /* don't care link local addr state, and always do DAD */
1143 1.2 itojun /* if link-local address is not eligible, do not autoconfigure. */
1144 1.2 itojun if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1145 1.2 itojun printf("in6_ifadd: link-local address not ready\n");
1146 1.2 itojun return NULL;
1147 1.2 itojun }
1148 1.2 itojun #endif
1149 1.2 itojun
1150 1.2 itojun /* prefixlen + ifidlen must be equal to 128 */
1151 1.2 itojun if (prefixlen != in6_mask2len(&ib->ia_prefixmask.sin6_addr)) {
1152 1.2 itojun log(LOG_ERR, "in6_ifadd: wrong prefixlen for %s"
1153 1.2 itojun "(prefix=%d ifid=%d)\n", ifp->if_xname,
1154 1.2 itojun prefixlen,
1155 1.2 itojun 128 - in6_mask2len(&ib->ia_prefixmask.sin6_addr));
1156 1.2 itojun return NULL;
1157 1.2 itojun }
1158 1.2 itojun
1159 1.2 itojun /* make ifaddr */
1160 1.2 itojun ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_DONTWAIT);
1161 1.2 itojun if (ia == NULL) {
1162 1.2 itojun printf("ENOBUFS in in6_ifadd %d\n", __LINE__);
1163 1.2 itojun return NULL;
1164 1.2 itojun }
1165 1.2 itojun
1166 1.2 itojun bzero((caddr_t)ia, sizeof(*ia));
1167 1.2 itojun ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1168 1.2 itojun ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1169 1.2 itojun ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1170 1.2 itojun ia->ia_ifp = ifp;
1171 1.2 itojun
1172 1.2 itojun /* link to in6_ifaddr */
1173 1.2 itojun if ((oia = in6_ifaddr) != NULL) {
1174 1.2 itojun for( ; oia->ia_next; oia = oia->ia_next)
1175 1.2 itojun continue;
1176 1.2 itojun oia->ia_next = ia;
1177 1.2 itojun } else
1178 1.2 itojun in6_ifaddr = ia;
1179 1.2 itojun
1180 1.2 itojun /* link to if_addrlist */
1181 1.2 itojun if (ifp->if_addrlist.tqh_first != NULL) {
1182 1.2 itojun TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
1183 1.2 itojun ifa_list);
1184 1.2 itojun }
1185 1.2 itojun #if 0
1186 1.2 itojun else {
1187 1.2 itojun /*
1188 1.2 itojun * this should not be the case because there is at least one
1189 1.2 itojun * link-local address(see the beginning of the function).
1190 1.2 itojun */
1191 1.2 itojun TAILQ_INIT(&ifp->if_addrlist);
1192 1.2 itojun }
1193 1.2 itojun #endif
1194 1.2 itojun
1195 1.2 itojun /* new address */
1196 1.2 itojun ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
1197 1.2 itojun ia->ia_addr.sin6_family = AF_INET6;
1198 1.2 itojun /* prefix */
1199 1.2 itojun bcopy(in6, &ia->ia_addr.sin6_addr, sizeof(ia->ia_addr.sin6_addr));
1200 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1201 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1202 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1203 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1204 1.2 itojun /* interface ID */
1205 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[0]
1206 1.2 itojun |= (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1207 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[1]
1208 1.2 itojun |= (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1209 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[2]
1210 1.2 itojun |= (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1211 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[3]
1212 1.2 itojun |= (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1213 1.2 itojun
1214 1.2 itojun /* new prefix */
1215 1.2 itojun ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1216 1.2 itojun ia->ia_prefixmask.sin6_family = AF_INET6;
1217 1.2 itojun bcopy(&mask, &ia->ia_prefixmask.sin6_addr,
1218 1.2 itojun sizeof(ia->ia_prefixmask.sin6_addr));
1219 1.2 itojun
1220 1.2 itojun /* same routine */
1221 1.2 itojun ia->ia_ifa.ifa_rtrequest =
1222 1.2 itojun (ifp->if_type == IFT_PPP) ? nd6_p2p_rtrequest : nd6_rtrequest;
1223 1.2 itojun ia->ia_ifa.ifa_flags |= RTF_CLONING;
1224 1.2 itojun ia->ia_ifa.ifa_metric = ifp->if_metric;
1225 1.2 itojun
1226 1.2 itojun /* add interface route */
1227 1.2 itojun if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP|RTF_CLONING))) {
1228 1.2 itojun #ifdef __NetBSD__
1229 1.2 itojun log(LOG_NOTICE, "in6_ifadd: failed to add an interface route "
1230 1.2 itojun "for %s/%d on %s, errno = %d\n",
1231 1.2 itojun ip6_sprintf(&ia->ia_addr.sin6_addr), prefixlen,
1232 1.2 itojun ifp->if_xname, error);
1233 1.2 itojun #else
1234 1.2 itojun log(LOG_NOTICE, "in6_ifadd: failed to add an interface route "
1235 1.2 itojun "for %s/%d on %s%d, errno = %d\n",
1236 1.2 itojun ip6_sprintf(&ia->ia_addr.sin6_addr), prefixlen,
1237 1.2 itojun ifp->if_name, ifp->if_unit, error);
1238 1.2 itojun #endif
1239 1.2 itojun }
1240 1.2 itojun else
1241 1.2 itojun ia->ia_flags |= IFA_ROUTE;
1242 1.2 itojun
1243 1.2 itojun *addr = ia->ia_addr.sin6_addr;
1244 1.2 itojun
1245 1.2 itojun if (ifp->if_flags & IFF_MULTICAST) {
1246 1.2 itojun int error; /* not used */
1247 1.2 itojun struct in6_addr sol6;
1248 1.2 itojun
1249 1.2 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
1250 1.2 itojun /* Restore saved multicast addresses(if any). */
1251 1.2 itojun in6_restoremkludge(ia, ifp);
1252 1.2 itojun #endif
1253 1.2 itojun
1254 1.2 itojun /* join solicited node multicast address */
1255 1.2 itojun bzero(&sol6, sizeof(sol6));
1256 1.2 itojun sol6.s6_addr16[0] = htons(0xff02);
1257 1.2 itojun sol6.s6_addr16[1] = htons(ifp->if_index);
1258 1.2 itojun sol6.s6_addr32[1] = 0;
1259 1.2 itojun sol6.s6_addr32[2] = htonl(1);
1260 1.2 itojun sol6.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
1261 1.2 itojun sol6.s6_addr8[12] = 0xff;
1262 1.2 itojun (void)in6_addmulti(&sol6, ifp, &error);
1263 1.2 itojun }
1264 1.2 itojun
1265 1.2 itojun ia->ia6_flags |= IN6_IFF_TENTATIVE;
1266 1.2 itojun
1267 1.2 itojun /*
1268 1.2 itojun * To make the interface up. Only AF_INET6 in ia is used...
1269 1.2 itojun */
1270 1.2 itojun s = splimp();
1271 1.2 itojun if (ifp->if_ioctl && (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) {
1272 1.2 itojun splx(s);
1273 1.2 itojun return NULL;
1274 1.2 itojun }
1275 1.2 itojun splx(s);
1276 1.2 itojun
1277 1.2 itojun /* Perform DAD, if needed. */
1278 1.2 itojun nd6_dad_start((struct ifaddr *)ia, NULL);
1279 1.2 itojun
1280 1.2 itojun return ia;
1281 1.2 itojun }
1282 1.2 itojun
1283 1.2 itojun int
1284 1.2 itojun in6_ifdel(ifp, in6)
1285 1.2 itojun struct ifnet *ifp;
1286 1.2 itojun struct in6_addr *in6;
1287 1.2 itojun {
1288 1.2 itojun struct in6_ifaddr *ia = (struct in6_ifaddr *)NULL;
1289 1.2 itojun struct in6_ifaddr *oia = (struct in6_ifaddr *)NULL;
1290 1.2 itojun
1291 1.2 itojun if (!ifp)
1292 1.2 itojun return -1;
1293 1.2 itojun
1294 1.2 itojun ia = in6ifa_ifpwithaddr(ifp, in6);
1295 1.2 itojun if (!ia)
1296 1.2 itojun return -1;
1297 1.2 itojun
1298 1.2 itojun if (ifp->if_flags & IFF_MULTICAST) {
1299 1.2 itojun /*
1300 1.2 itojun * delete solicited multicast addr for deleting host id
1301 1.2 itojun */
1302 1.2 itojun struct in6_multi *in6m;
1303 1.2 itojun struct in6_addr llsol;
1304 1.2 itojun bzero(&llsol, sizeof(struct in6_addr));
1305 1.2 itojun llsol.s6_addr16[0] = htons(0xff02);
1306 1.2 itojun llsol.s6_addr16[1] = htons(ifp->if_index);
1307 1.2 itojun llsol.s6_addr32[1] = 0;
1308 1.2 itojun llsol.s6_addr32[2] = htonl(1);
1309 1.2 itojun llsol.s6_addr32[3] =
1310 1.2 itojun ia->ia_addr.sin6_addr.s6_addr32[3];
1311 1.2 itojun llsol.s6_addr8[12] = 0xff;
1312 1.2 itojun
1313 1.2 itojun IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1314 1.2 itojun if (in6m)
1315 1.2 itojun in6_delmulti(in6m);
1316 1.2 itojun }
1317 1.2 itojun
1318 1.2 itojun if (ia->ia_flags & IFA_ROUTE) {
1319 1.2 itojun rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1320 1.2 itojun ia->ia_flags &= ~IFA_ROUTE;
1321 1.2 itojun }
1322 1.2 itojun
1323 1.2 itojun TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
1324 1.2 itojun
1325 1.2 itojun /* lladdr is never deleted */
1326 1.2 itojun oia = ia;
1327 1.2 itojun if (oia == (ia = in6_ifaddr))
1328 1.2 itojun in6_ifaddr = ia->ia_next;
1329 1.2 itojun else {
1330 1.2 itojun while (ia->ia_next && (ia->ia_next != oia))
1331 1.2 itojun ia = ia->ia_next;
1332 1.2 itojun if (ia->ia_next)
1333 1.2 itojun ia->ia_next = oia->ia_next;
1334 1.2 itojun else
1335 1.2 itojun return -1;
1336 1.2 itojun }
1337 1.2 itojun
1338 1.2 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
1339 1.2 itojun in6_savemkludge(oia);
1340 1.2 itojun #endif
1341 1.2 itojun IFAFREE((&oia->ia_ifa));
1342 1.2 itojun /* xxx
1343 1.2 itojun rtrequest(RTM_DELETE,
1344 1.2 itojun (struct sockaddr *)&ia->ia_addr,
1345 1.2 itojun (struct sockaddr *)0
1346 1.2 itojun (struct sockaddr *)&ia->ia_prefixmask,
1347 1.2 itojun RTF_UP|RTF_CLONING,
1348 1.2 itojun (struct rtentry **)0);
1349 1.2 itojun */
1350 1.2 itojun return 0;
1351 1.2 itojun }
1352 1.2 itojun
1353 1.2 itojun static int
1354 1.2 itojun in6_are_prefix_equal(p1, p2, len)
1355 1.2 itojun struct in6_addr *p1, *p2;
1356 1.2 itojun int len;
1357 1.2 itojun {
1358 1.2 itojun int bytelen, bitlen;
1359 1.2 itojun
1360 1.2 itojun /* sanity check */
1361 1.2 itojun if (0 > len || len > 128) {
1362 1.2 itojun log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1363 1.2 itojun len);
1364 1.2 itojun return(0);
1365 1.2 itojun }
1366 1.2 itojun
1367 1.2 itojun bytelen = len / 8;
1368 1.2 itojun bitlen = len % 8;
1369 1.2 itojun
1370 1.2 itojun if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1371 1.2 itojun return(0);
1372 1.2 itojun if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1373 1.2 itojun p2->s6_addr[bytelen] >> (8 - bitlen))
1374 1.2 itojun return(0);
1375 1.2 itojun
1376 1.2 itojun return(1);
1377 1.2 itojun }
1378 1.2 itojun
1379 1.2 itojun static void
1380 1.2 itojun in6_prefixlen2mask(maskp, len)
1381 1.2 itojun struct in6_addr *maskp;
1382 1.2 itojun int len;
1383 1.2 itojun {
1384 1.2 itojun u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1385 1.2 itojun int bytelen, bitlen, i;
1386 1.2 itojun
1387 1.2 itojun /* sanity check */
1388 1.2 itojun if (0 > len || len > 128) {
1389 1.2 itojun log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1390 1.2 itojun len);
1391 1.2 itojun return;
1392 1.2 itojun }
1393 1.2 itojun
1394 1.2 itojun bzero(maskp, sizeof(*maskp));
1395 1.2 itojun bytelen = len / 8;
1396 1.2 itojun bitlen = len % 8;
1397 1.2 itojun for (i = 0; i < bytelen; i++)
1398 1.2 itojun maskp->s6_addr[i] = 0xff;
1399 1.2 itojun if (bitlen)
1400 1.2 itojun maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1401 1.2 itojun }
1402 1.2 itojun
1403 1.2 itojun int
1404 1.2 itojun in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1405 1.2 itojun {
1406 1.2 itojun /* check if preferred lifetime > valid lifetime */
1407 1.2 itojun if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
1408 1.2 itojun log(LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
1409 1.2 itojun "(%d) is greater than valid lifetime(%d)\n",
1410 1.2 itojun (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime);
1411 1.2 itojun return (EINVAL);
1412 1.2 itojun }
1413 1.2 itojun if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME ||
1414 1.2 itojun ndpr->ndpr_rrf_decrprefd == 0)
1415 1.2 itojun ndpr->ndpr_preferred = 0;
1416 1.2 itojun else
1417 1.2 itojun ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
1418 1.2 itojun if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME ||
1419 1.2 itojun ndpr->ndpr_rrf_decrvalid == 0)
1420 1.2 itojun ndpr->ndpr_expire = 0;
1421 1.2 itojun else
1422 1.2 itojun ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
1423 1.2 itojun
1424 1.2 itojun return 0;
1425 1.2 itojun }
1426 1.2 itojun
1427 1.2 itojun static void
1428 1.2 itojun in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1429 1.2 itojun {
1430 1.2 itojun /* init ia6t_expire */
1431 1.2 itojun if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME ||
1432 1.2 itojun (new->ndpr_origin >= PR_ORIG_RR && new->ndpr_rrf_decrvalid == 0))
1433 1.2 itojun lt6->ia6t_expire = 0;
1434 1.2 itojun else {
1435 1.2 itojun lt6->ia6t_expire = time_second;
1436 1.2 itojun lt6->ia6t_expire += lt6->ia6t_vltime;
1437 1.2 itojun }
1438 1.2 itojun /* init ia6t_preferred */
1439 1.2 itojun if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME ||
1440 1.2 itojun (new->ndpr_origin >= PR_ORIG_RR && new->ndpr_rrf_decrprefd == 0))
1441 1.2 itojun lt6->ia6t_preferred = 0;
1442 1.2 itojun else {
1443 1.2 itojun lt6->ia6t_preferred = time_second;
1444 1.2 itojun lt6->ia6t_preferred += lt6->ia6t_pltime;
1445 1.2 itojun }
1446 1.2 itojun /* ensure addr lifetime <= prefix lifetime */
1447 1.2 itojun if (new->ndpr_expire && lt6->ia6t_expire &&
1448 1.2 itojun new->ndpr_expire < lt6->ia6t_expire)
1449 1.2 itojun lt6->ia6t_expire = new->ndpr_expire;
1450 1.2 itojun if (new->ndpr_preferred && lt6->ia6t_preferred
1451 1.2 itojun && new->ndpr_preferred < lt6->ia6t_preferred)
1452 1.2 itojun lt6->ia6t_preferred = new->ndpr_preferred;
1453 1.2 itojun }
1454 1.2 itojun
1455 1.2 itojun /*
1456 1.2 itojun * Delete all the routing table entries that use the specified gateway.
1457 1.2 itojun * XXX: this function causes search through all entries of routing table, so
1458 1.2 itojun * it shouldn't be called when acting as a router.
1459 1.2 itojun */
1460 1.2 itojun void
1461 1.2 itojun rt6_flush(gateway, ifp)
1462 1.2 itojun struct in6_addr *gateway;
1463 1.2 itojun struct ifnet *ifp;
1464 1.2 itojun {
1465 1.2 itojun struct radix_node_head *rnh = rt_tables[AF_INET6];
1466 1.2 itojun int s = splnet();
1467 1.2 itojun
1468 1.2 itojun /* We'll care only link-local addresses */
1469 1.2 itojun if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
1470 1.2 itojun splx(s);
1471 1.2 itojun return;
1472 1.2 itojun }
1473 1.2 itojun /* XXX: hack for KAME's link-local address kludge */
1474 1.2 itojun gateway->s6_addr16[1] = htons(ifp->if_index);
1475 1.2 itojun
1476 1.2 itojun rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
1477 1.2 itojun splx(s);
1478 1.2 itojun }
1479 1.2 itojun
1480 1.2 itojun static int
1481 1.2 itojun rt6_deleteroute(rn, arg)
1482 1.2 itojun struct radix_node *rn;
1483 1.2 itojun void *arg;
1484 1.2 itojun {
1485 1.2 itojun #define SIN6(s) ((struct sockaddr_in6 *)s)
1486 1.2 itojun struct rtentry *rt = (struct rtentry *)rn;
1487 1.2 itojun struct in6_addr *gate = (struct in6_addr *)arg;
1488 1.2 itojun
1489 1.2 itojun if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
1490 1.2 itojun return(0);
1491 1.2 itojun
1492 1.2 itojun if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
1493 1.2 itojun return(0);
1494 1.2 itojun
1495 1.2 itojun /*
1496 1.2 itojun * We delete only host route. This means, in particular, we don't
1497 1.2 itojun * delete default route.
1498 1.2 itojun */
1499 1.2 itojun if ((rt->rt_flags & RTF_HOST) == 0)
1500 1.2 itojun return(0);
1501 1.2 itojun
1502 1.2 itojun return(rtrequest(RTM_DELETE, rt_key(rt),
1503 1.2 itojun rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0));
1504 1.2 itojun #undef SIN6
1505 1.2 itojun }
1506