]>
Commit | Line | Data |
---|---|---|
1 | /* $KAME: in6.c,v 1.72 2000/03/30 03:45:26 itojun Exp $ */ | |
2 | ||
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 | ||
32 | /* | |
33 | * Copyright (c) 1982, 1986, 1991, 1993 | |
34 | * The Regents of the University of California. All rights reserved. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. All advertising materials mentioning features or use of this software | |
45 | * must display the following acknowledgement: | |
46 | * This product includes software developed by the University of | |
47 | * California, Berkeley and its contributors. | |
48 | * 4. Neither the name of the University nor the names of its contributors | |
49 | * may be used to endorse or promote products derived from this software | |
50 | * without specific prior written permission. | |
51 | * | |
52 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
53 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
55 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
56 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
57 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
58 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
59 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
60 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
61 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
62 | * SUCH DAMAGE. | |
63 | * | |
64 | * @(#)in.c 8.2 (Berkeley) 11/15/93 | |
65 | */ | |
66 | ||
67 | #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__) | |
68 | #include "opt_inet.h" | |
69 | #endif | |
70 | ||
71 | #include <sys/param.h> | |
72 | #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) | |
73 | #include <sys/ioctl.h> | |
74 | #endif | |
75 | #include <sys/errno.h> | |
76 | #include <sys/malloc.h> | |
77 | #include <sys/socket.h> | |
78 | #include <sys/socketvar.h> | |
79 | #include <sys/sockio.h> | |
80 | #include <sys/systm.h> | |
81 | #include <sys/time.h> | |
82 | #include <sys/kernel.h> | |
83 | #include <sys/syslog.h> | |
84 | ||
85 | #include <net/if.h> | |
86 | #include <net/if_types.h> | |
87 | #include <net/route.h> | |
88 | #include "gif.h" | |
89 | #if NGIF > 0 | |
90 | #include <net/if_gif.h> | |
91 | #endif | |
92 | #include <net/if_dl.h> | |
93 | ||
94 | #include <netinet/in.h> | |
95 | #include <netinet/in_var.h> | |
96 | #if __NetBSD__ | |
97 | #include <net/if_ether.h> | |
98 | #else | |
99 | #include <netinet/if_ether.h> | |
100 | #endif | |
101 | ||
102 | #include <netinet6/nd6.h> | |
103 | #include <netinet/ip6.h> | |
104 | #include <netinet6/ip6_var.h> | |
105 | #include <netinet6/mld6_var.h> | |
106 | #include <netinet6/ip6_mroute.h> | |
107 | #include <netinet6/in6_ifattach.h> | |
108 | ||
109 | #include <net/net_osdep.h> | |
110 | ||
111 | #if MIP6 | |
112 | #include <netinet6/mip6.h> | |
113 | #include <netinet6/mip6_common.h> | |
114 | ||
115 | struct nd_prefix *(*mip6_get_home_prefix_hook) __P((void)); | |
116 | #endif /* MIP6 */ | |
117 | ||
118 | #if defined(__FreeBSD__) && __FreeBSD__ >= 3 | |
119 | MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address"); | |
120 | #endif | |
121 | ||
122 | /* | |
123 | * Definitions of some costant IP6 addresses. | |
124 | */ | |
125 | const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; | |
126 | const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; | |
127 | const struct in6_addr in6addr_nodelocal_allnodes = | |
128 | IN6ADDR_NODELOCAL_ALLNODES_INIT; | |
129 | const struct in6_addr in6addr_linklocal_allnodes = | |
130 | IN6ADDR_LINKLOCAL_ALLNODES_INIT; | |
131 | const struct in6_addr in6addr_linklocal_allrouters = | |
132 | IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; | |
133 | ||
134 | const struct in6_addr in6mask0 = IN6MASK0; | |
135 | const struct in6_addr in6mask32 = IN6MASK32; | |
136 | const struct in6_addr in6mask64 = IN6MASK64; | |
137 | const struct in6_addr in6mask96 = IN6MASK96; | |
138 | const struct in6_addr in6mask128 = IN6MASK128; | |
139 | ||
140 | #if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3) && !defined (__APPLE__) | |
141 | static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, | |
142 | struct ifnet *, struct proc *)); | |
143 | #else | |
144 | static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, | |
145 | struct ifnet *)); | |
146 | #endif | |
147 | ||
148 | #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__) | |
149 | struct in6_multihead in6_multihead; /* XXX BSS initialization */ | |
150 | #else | |
151 | /* | |
152 | * This structure is used to keep track of in6_multi chains which belong to | |
153 | * deleted interface addresses. | |
154 | */ | |
155 | static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */ | |
156 | ||
157 | struct multi6_kludge { | |
158 | LIST_ENTRY(multi6_kludge) mk_entry; | |
159 | struct ifnet *mk_ifp; | |
160 | struct in6_multihead mk_head; | |
161 | }; | |
162 | #endif | |
163 | ||
164 | /* | |
165 | * Check if the loopback entry will be automatically generated. | |
166 | * if 0 returned, will not be automatically generated. | |
167 | * if 1 returned, will be automatically generated. | |
168 | */ | |
169 | static int | |
170 | in6_is_ifloop_auto(struct ifaddr *ifa) | |
171 | { | |
172 | #define SIN6(s) ((struct sockaddr_in6 *)s) | |
173 | /* | |
174 | * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT), | |
175 | * or netmask is all0 or all1, then cloning will not happen, | |
176 | * then we can't rely on its loopback entry generation. | |
177 | */ | |
178 | if ((ifa->ifa_flags & RTF_CLONING) == 0 || | |
179 | (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) || | |
180 | (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6) | |
181 | && | |
182 | IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr, | |
183 | &in6mask128)) || | |
184 | ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0) | |
185 | return 0; | |
186 | else | |
187 | return 1; | |
188 | #undef SIN6 | |
189 | } | |
190 | ||
191 | /* | |
192 | * Subroutine for in6_ifaddloop() and in6_ifremloop(). | |
193 | * This routine does actual work. | |
194 | */ | |
195 | static void | |
196 | in6_ifloop_request(int cmd, struct ifaddr *ifa) | |
197 | { | |
198 | struct sockaddr_in6 lo_sa; | |
199 | struct sockaddr_in6 all1_sa; | |
200 | struct rtentry *nrt = NULL; | |
201 | ||
202 | bzero(&lo_sa, sizeof(lo_sa)); | |
203 | bzero(&all1_sa, sizeof(all1_sa)); | |
204 | lo_sa.sin6_family = AF_INET6; | |
205 | lo_sa.sin6_len = sizeof(struct sockaddr_in6); | |
206 | all1_sa = lo_sa; | |
207 | lo_sa.sin6_addr = in6addr_loopback; | |
208 | all1_sa.sin6_addr = in6mask128; | |
209 | ||
210 | /* So we add or remove static loopback entry, here. */ | |
211 | rtrequest(cmd, ifa->ifa_addr, | |
212 | (struct sockaddr *)&lo_sa, | |
213 | (struct sockaddr *)&all1_sa, | |
214 | RTF_UP|RTF_HOST, &nrt); | |
215 | ||
216 | /* | |
217 | * Make sure rt_ifa be equal to IFA, the second argument of the | |
218 | * function. | |
219 | * We need this because when we refer rt_ifa->ia6_flags in ip6_input, | |
220 | * we assume that the rt_ifa points to the address instead of the | |
221 | * loopback address. | |
222 | */ | |
223 | if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) { | |
224 | IFAFREE(nrt->rt_ifa); | |
225 | ifa->ifa_refcnt++; | |
226 | nrt->rt_ifa = ifa; | |
227 | nrt->rt_dlt = ifa->ifa_dlt; | |
228 | } | |
229 | if (nrt) | |
230 | nrt->rt_refcnt--; | |
231 | } | |
232 | ||
233 | /* | |
234 | * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). | |
235 | * Because, KAME needs loopback rtentry for ownaddr check in | |
236 | * ip6_input(). | |
237 | */ | |
238 | static void | |
239 | in6_ifaddloop(struct ifaddr *ifa) | |
240 | { | |
241 | if (!in6_is_ifloop_auto(ifa)) { | |
242 | struct rtentry *rt; | |
243 | ||
244 | /* If there is no loopback entry, allocate one. */ | |
245 | rt = rtalloc1(ifa->ifa_addr, 0 | |
246 | #if defined(__FreeBSD__) || defined (__APPLE__) | |
247 | , 0 | |
248 | #endif /* __FreeBSD__ */ | |
249 | ); | |
250 | if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) | |
251 | in6_ifloop_request(RTM_ADD, ifa); | |
252 | if (rt) | |
253 | rt->rt_refcnt--; | |
254 | } | |
255 | } | |
256 | ||
257 | /* | |
258 | * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(), | |
259 | * if it exists. | |
260 | */ | |
261 | static void | |
262 | in6_ifremloop(struct ifaddr *ifa) | |
263 | { | |
264 | if (!in6_is_ifloop_auto(ifa)) { | |
265 | struct in6_ifaddr *ia; | |
266 | int ia_count = 0; | |
267 | ||
268 | /* If only one ifa for the loopback entry, delete it. */ | |
269 | for (ia = in6_ifaddr; ia; ia = ia->ia_next) { | |
270 | if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), | |
271 | &ia->ia_addr.sin6_addr)) { | |
272 | ia_count++; | |
273 | if (ia_count > 1) | |
274 | break; | |
275 | } | |
276 | } | |
277 | if (ia_count == 1) | |
278 | in6_ifloop_request(RTM_DELETE, ifa); | |
279 | } | |
280 | } | |
281 | ||
282 | int | |
283 | in6_ifindex2scopeid(idx) | |
284 | int idx; | |
285 | { | |
286 | struct ifnet *ifp; | |
287 | struct ifaddr *ifa; | |
288 | struct sockaddr_in6 *sin6; | |
289 | ||
290 | if (idx < 0 || if_index < idx) | |
291 | return -1; | |
292 | ifp = ifindex2ifnet[idx]; | |
293 | ||
294 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
295 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
296 | #else | |
297 | for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) | |
298 | #endif | |
299 | { | |
300 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
301 | continue; | |
302 | sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; | |
303 | if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) | |
304 | return sin6->sin6_scope_id & 0xffff; | |
305 | } | |
306 | ||
307 | return -1; | |
308 | } | |
309 | ||
310 | int | |
311 | in6_mask2len(mask) | |
312 | struct in6_addr *mask; | |
313 | { | |
314 | int x, y; | |
315 | ||
316 | for (x = 0; x < sizeof(*mask); x++) { | |
317 | if (mask->s6_addr8[x] != 0xff) | |
318 | break; | |
319 | } | |
320 | y = 0; | |
321 | if (x < sizeof(*mask)) { | |
322 | for (y = 0; y < 8; y++) { | |
323 | if ((mask->s6_addr8[x] & (0x80 >> y)) == 0) | |
324 | break; | |
325 | } | |
326 | } | |
327 | return x * 8 + y; | |
328 | } | |
329 | ||
330 | void | |
331 | in6_len2mask(mask, len) | |
332 | struct in6_addr *mask; | |
333 | int len; | |
334 | { | |
335 | int i; | |
336 | ||
337 | bzero(mask, sizeof(*mask)); | |
338 | for (i = 0; i < len / 8; i++) | |
339 | mask->s6_addr8[i] = 0xff; | |
340 | if (len % 8) | |
341 | mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff; | |
342 | } | |
343 | ||
344 | #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) | |
345 | #define ia62ifa(ia6) (&((ia6)->ia_ifa)) | |
346 | ||
347 | int | |
348 | #if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3) && !defined (__APPLE__) | |
349 | in6_control(so, cmd, data, ifp, p) | |
350 | struct socket *so; | |
351 | u_long cmd; | |
352 | caddr_t data; | |
353 | struct ifnet *ifp; | |
354 | struct proc *p; | |
355 | #else | |
356 | in6_control(so, cmd, data, ifp) | |
357 | struct socket *so; | |
358 | u_long cmd; | |
359 | caddr_t data; | |
360 | struct ifnet *ifp; | |
361 | #endif | |
362 | { | |
363 | struct in6_ifreq *ifr = (struct in6_ifreq *)data; | |
364 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
365 | struct ifaddr *ifa; | |
366 | #endif | |
367 | struct in6_ifaddr *ia = NULL, *oia; | |
368 | struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; | |
369 | struct sockaddr_in6 oldaddr; | |
370 | #ifdef COMPAT_IN6IFIOCTL | |
371 | struct sockaddr_in6 net; | |
372 | #endif | |
373 | int error = 0, hostIsNew, prefixIsNew; | |
374 | int newifaddr; | |
375 | #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__) | |
376 | time_t time_second = (time_t)time.tv_sec; | |
377 | #endif | |
378 | int privileged; | |
379 | u_long dl_tag; | |
380 | ||
381 | privileged = 0; | |
382 | #if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3) && !defined (__APPLE__) | |
383 | if (p && !suser(p->p_ucred, &p->p_acflag)) | |
384 | privileged++; | |
385 | #else | |
386 | if ((so->so_state & SS_PRIV) != 0) | |
387 | privileged++; | |
388 | #endif | |
389 | ||
390 | /* | |
391 | * xxx should prevent processes for link-local addresses? | |
392 | */ | |
393 | #if NGIF > 0 | |
394 | if (ifp && ifp->if_type == IFT_GIF) { | |
395 | switch (cmd) { | |
396 | case SIOCSIFPHYADDR_IN6: | |
397 | if (!privileged) | |
398 | return(EPERM); | |
399 | /*fall through*/ | |
400 | case SIOCGIFPSRCADDR_IN6: | |
401 | case SIOCGIFPDSTADDR_IN6: | |
402 | return gif_ioctl(ifp, cmd, data); | |
403 | } | |
404 | } | |
405 | #endif | |
406 | switch (cmd) { | |
407 | case SIOCGETSGCNT_IN6: | |
408 | case SIOCGETMIFCNT_IN6: | |
409 | return (mrt6_ioctl(cmd, data)); | |
410 | } | |
411 | #if MIP6 | |
412 | /* These require root privileges */ | |
413 | switch (cmd) { | |
414 | case SIOCSDEBUG_MIP6: | |
415 | case SIOCSBCFLUSH_MIP6: | |
416 | case SIOCSDEFCONFIG_MIP6: | |
417 | case SIOCSBRUPDATE_MIP6: | |
418 | case SIOCSENABLEBR_MIP6: | |
419 | case SIOCSATTACH_MIP6: | |
420 | case SIOCSRELEASE_MIP6: | |
421 | ||
422 | case SIOCSHALISTFLUSH_MIP6: | |
423 | case SIOCSHAPREF_MIP6: | |
424 | case SIOCSFWDSLUNICAST_MIP6: | |
425 | case SIOCSFWDSLMULTICAST_MIP6: | |
426 | ||
427 | case SIOCSFORADDRFLUSH_MIP6: | |
428 | case SIOCSHADDRFLUSH_MIP6: | |
429 | case SIOCSBULISTFLUSH_MIP6: | |
430 | case SIOCACOADDR_MIP6: | |
431 | case SIOCAHOMEADDR_MIP6: | |
432 | case SIOCSBULIFETIME_MIP6: | |
433 | case SIOCSHRLIFETIME_MIP6: | |
434 | case SIOCDCOADDR_MIP6: | |
435 | case SIOCSPROMMODE_MIP6: | |
436 | case SIOCSBU2CN_MIP6: | |
437 | case SIOCSREVTUNNEL_MIP6: | |
438 | case SIOCSAUTOCONFIG_MIP6: | |
439 | case SIOCSEAGERMD_MIP6: | |
440 | if (!privileged) | |
441 | return(EPERM); | |
442 | /* Anyone can use these or the user is root */ | |
443 | /* case SIOCXVERYSAFECOMMAND_MIP6: */ | |
444 | #if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3) && !defined(__APPLE__) | |
445 | return mip6_ioctl(so, cmd, data, ifp, p); | |
446 | #else | |
447 | return mip6_ioctl(so, cmd, data, ifp); | |
448 | #endif | |
449 | } | |
450 | #endif /* MIP6 */ | |
451 | ||
452 | if (ifp == NULL) | |
453 | return(EOPNOTSUPP); | |
454 | ||
455 | switch (cmd) { | |
456 | case SIOCSNDFLUSH_IN6: | |
457 | case SIOCSPFXFLUSH_IN6: | |
458 | case SIOCSRTRFLUSH_IN6: | |
459 | case SIOCSDEFIFACE_IN6: | |
460 | case SIOCSIFINFO_FLAGS: | |
461 | if (!privileged) | |
462 | return(EPERM); | |
463 | /*fall through*/ | |
464 | case SIOCGIFINFO_IN6: | |
465 | case SIOCGDRLST_IN6: | |
466 | case SIOCGPRLST_IN6: | |
467 | case SIOCGNBRINFO_IN6: | |
468 | case SIOCGDEFIFACE_IN6: | |
469 | return(nd6_ioctl(cmd, data, ifp)); | |
470 | } | |
471 | ||
472 | switch (cmd) { | |
473 | case SIOCSIFPREFIX_IN6: | |
474 | case SIOCDIFPREFIX_IN6: | |
475 | case SIOCAIFPREFIX_IN6: | |
476 | case SIOCCIFPREFIX_IN6: | |
477 | case SIOCSGIFPREFIX_IN6: | |
478 | if (!privileged) | |
479 | return(EPERM); | |
480 | /*fall through*/ | |
481 | case SIOCGIFPREFIX_IN6: | |
482 | if (ip6_forwarding == 0) | |
483 | return(EPERM); | |
484 | return(in6_prefix_ioctl(so, cmd, data, ifp)); | |
485 | } | |
486 | ||
487 | switch (cmd) { | |
488 | case SIOCALIFADDR: | |
489 | case SIOCDLIFADDR: | |
490 | if (!privileged) | |
491 | return(EPERM); | |
492 | /*fall through*/ | |
493 | case SIOCGLIFADDR: | |
494 | #if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3) && !defined (__APPLE__) | |
495 | return in6_lifaddr_ioctl(so, cmd, data, ifp, p); | |
496 | #else | |
497 | return in6_lifaddr_ioctl(so, cmd, data, ifp); | |
498 | #endif | |
499 | } | |
500 | ||
501 | /* | |
502 | * Find address for this interface, if it exists. | |
503 | */ | |
504 | if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */ | |
505 | struct sockaddr_in6 *sa6 = | |
506 | (struct sockaddr_in6 *)&ifra->ifra_addr; | |
507 | ||
508 | if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) { | |
509 | if (sa6->sin6_addr.s6_addr16[1] == 0) { | |
510 | /* interface ID is not embedded by the user */ | |
511 | sa6->sin6_addr.s6_addr16[1] = | |
512 | htons(ifp->if_index); | |
513 | } else if (sa6->sin6_addr.s6_addr16[1] != | |
514 | htons(ifp->if_index)) { | |
515 | return(EINVAL); /* ifid is contradict */ | |
516 | } | |
517 | if (sa6->sin6_scope_id) { | |
518 | if (sa6->sin6_scope_id != | |
519 | (u_int32_t)ifp->if_index) | |
520 | return(EINVAL); | |
521 | sa6->sin6_scope_id = 0; /* XXX: good way? */ | |
522 | } | |
523 | } | |
524 | ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr); | |
525 | } | |
526 | ||
527 | switch (cmd) { | |
528 | ||
529 | case SIOCDIFADDR_IN6: | |
530 | /* | |
531 | * for IPv4, we look for existing in6_ifaddr here to allow | |
532 | * "ifconfig if0 delete" to remove first IPv4 address on the | |
533 | * interface. For IPv6, as the spec allow multiple interface | |
534 | * address from the day one, we consider "remove the first one" | |
535 | * semantics to be not preferrable. | |
536 | */ | |
537 | if (ia == NULL) | |
538 | return(EADDRNOTAVAIL); | |
539 | /* FALLTHROUGH */ | |
540 | case SIOCAIFADDR_IN6: | |
541 | case SIOCSIFADDR_IN6: | |
542 | #if COMPAT_IN6IFIOCTL | |
543 | case SIOCSIFDSTADDR_IN6: | |
544 | case SIOCSIFNETMASK_IN6: | |
545 | /* | |
546 | * Since IPv6 allows a node to assign multiple addresses | |
547 | * on a single interface, SIOCSIFxxx ioctls are not suitable | |
548 | * and should be unused. | |
549 | */ | |
550 | #endif | |
551 | if (ifra->ifra_addr.sin6_family != AF_INET6) | |
552 | return(EAFNOSUPPORT); | |
553 | if (!privileged) | |
554 | return(EPERM); | |
555 | if (ia == NULL) { | |
556 | ia = (struct in6_ifaddr *) | |
557 | _MALLOC(sizeof(*ia), M_IFADDR, M_WAITOK); | |
558 | if (ia == NULL) | |
559 | return (ENOBUFS); | |
560 | bzero((caddr_t)ia, sizeof(*ia)); | |
561 | /* Initialize the address and masks */ | |
562 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; | |
563 | ia->ia_addr.sin6_family = AF_INET6; | |
564 | ia->ia_addr.sin6_len = sizeof(ia->ia_addr); | |
565 | if (ifp->if_flags & IFF_POINTOPOINT) { | |
566 | ia->ia_ifa.ifa_dstaddr | |
567 | = (struct sockaddr *)&ia->ia_dstaddr; | |
568 | ia->ia_dstaddr.sin6_family = AF_INET6; | |
569 | ia->ia_dstaddr.sin6_len = sizeof(ia->ia_dstaddr); | |
570 | } else { | |
571 | ia->ia_ifa.ifa_dstaddr = NULL; | |
572 | bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr)); | |
573 | } | |
574 | ia->ia_ifa.ifa_netmask | |
575 | = (struct sockaddr *)&ia->ia_prefixmask; | |
576 | ||
577 | ia->ia_ifp = ifp; | |
578 | ||
579 | printf("in6_control: Attach dl_tag for if=%s%n\n", ifp->if_name, ifp->if_unit); | |
580 | ||
581 | if (strcmp(ifp->if_name, "en") == 0) | |
582 | dl_tag = ether_attach_inet6(ifp); | |
583 | ||
584 | if (strcmp(ifp->if_name, "lo") == 0) | |
585 | dl_tag = lo_attach_inet(ifp); | |
586 | #if NGIF > 0 | |
587 | if (strcmp(ifp->if_name, "gif") == 0) | |
588 | dl_tag = gif_attach_inet(ifp); | |
589 | #endif | |
590 | /* End of temp code */ | |
591 | ia->ia_ifa.ifa_dlt = dl_tag; | |
592 | ||
593 | ||
594 | if ((oia = in6_ifaddr) != NULL) { | |
595 | for ( ; oia->ia_next; oia = oia->ia_next) | |
596 | continue; | |
597 | oia->ia_next = ia; | |
598 | } else | |
599 | in6_ifaddr = ia; | |
600 | ia->ia_ifa.ifa_refcnt++; | |
601 | ||
602 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
603 | if ((ifa = ifp->if_addrlist) != NULL) { | |
604 | for ( ; ifa->ifa_next; ifa = ifa->ifa_next) | |
605 | continue; | |
606 | ifa->ifa_next = ia62ifa(ia); | |
607 | } else | |
608 | ifp->if_addrlist = ia62ifa(ia); | |
609 | #else | |
610 | TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, | |
611 | ifa_list); | |
612 | #endif | |
613 | ia->ia_ifa.ifa_refcnt++; | |
614 | ||
615 | newifaddr = 1; | |
616 | } else | |
617 | newifaddr = 0; | |
618 | ||
619 | if (cmd == SIOCAIFADDR_IN6) { | |
620 | /* sanity for overflow - beware unsigned */ | |
621 | struct in6_addrlifetime *lt; | |
622 | lt = &ifra->ifra_lifetime; | |
623 | if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME | |
624 | && lt->ia6t_vltime + time_second < time_second) { | |
625 | return EINVAL; | |
626 | } | |
627 | if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME | |
628 | && lt->ia6t_pltime + time_second < time_second) { | |
629 | return EINVAL; | |
630 | } | |
631 | } | |
632 | break; | |
633 | ||
634 | case SIOCGIFADDR_IN6: | |
635 | /* This interface is basically deprecated. use SIOCGIFCONF. */ | |
636 | /* fall through */ | |
637 | case SIOCGIFAFLAG_IN6: | |
638 | case SIOCGIFNETMASK_IN6: | |
639 | case SIOCGIFDSTADDR_IN6: | |
640 | case SIOCGIFALIFETIME_IN6: | |
641 | /* must think again about its semantics */ | |
642 | if (ia == NULL) | |
643 | return(EADDRNOTAVAIL); | |
644 | break; | |
645 | case SIOCSIFALIFETIME_IN6: | |
646 | { | |
647 | struct in6_addrlifetime *lt; | |
648 | ||
649 | if (!privileged) | |
650 | return(EPERM); | |
651 | if (ia == NULL) | |
652 | return(EADDRNOTAVAIL); | |
653 | /* sanity for overflow - beware unsigned */ | |
654 | lt = &ifr->ifr_ifru.ifru_lifetime; | |
655 | if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME | |
656 | && lt->ia6t_vltime + time_second < time_second) { | |
657 | return EINVAL; | |
658 | } | |
659 | if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME | |
660 | && lt->ia6t_pltime + time_second < time_second) { | |
661 | return EINVAL; | |
662 | } | |
663 | break; | |
664 | } | |
665 | } | |
666 | ||
667 | switch (cmd) { | |
668 | ||
669 | case SIOCGIFADDR_IN6: | |
670 | ifr->ifr_addr = ia->ia_addr; | |
671 | break; | |
672 | ||
673 | case SIOCGIFDSTADDR_IN6: | |
674 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) | |
675 | return(EINVAL); | |
676 | ifr->ifr_dstaddr = ia->ia_dstaddr; | |
677 | break; | |
678 | ||
679 | case SIOCGIFNETMASK_IN6: | |
680 | ifr->ifr_addr = ia->ia_prefixmask; | |
681 | break; | |
682 | ||
683 | case SIOCGIFAFLAG_IN6: | |
684 | ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; | |
685 | break; | |
686 | ||
687 | case SIOCGIFSTAT_IN6: | |
688 | if (ifp == NULL) | |
689 | return EINVAL; | |
690 | if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax | |
691 | || in6_ifstat[ifp->if_index] == NULL) { | |
692 | /* return EAFNOSUPPORT? */ | |
693 | bzero(&ifr->ifr_ifru.ifru_stat, | |
694 | sizeof(ifr->ifr_ifru.ifru_stat)); | |
695 | } else | |
696 | ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index]; | |
697 | break; | |
698 | ||
699 | case SIOCGIFSTAT_ICMP6: | |
700 | if (ifp == NULL) | |
701 | return EINVAL; | |
702 | if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax || | |
703 | icmp6_ifstat[ifp->if_index] == NULL) { | |
704 | /* return EAFNOSUPPORT? */ | |
705 | bzero(&ifr->ifr_ifru.ifru_stat, | |
706 | sizeof(ifr->ifr_ifru.ifru_icmp6stat)); | |
707 | } else | |
708 | ifr->ifr_ifru.ifru_icmp6stat = | |
709 | *icmp6_ifstat[ifp->if_index]; | |
710 | break; | |
711 | #if COMPAT_IN6IFIOCTL /* should be unused */ | |
712 | case SIOCSIFDSTADDR_IN6: | |
713 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) | |
714 | return(EINVAL); | |
715 | oldaddr = ia->ia_dstaddr; | |
716 | ia->ia_dstaddr = ifr->ifr_dstaddr; | |
717 | ||
718 | /* link-local index check */ | |
719 | if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) { | |
720 | if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) { | |
721 | /* interface ID is not embedded by the user */ | |
722 | ia->ia_dstaddr.sin6_addr.s6_addr16[1] | |
723 | = htons(ifp->if_index); | |
724 | } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] != | |
725 | htons(ifp->if_index)) { | |
726 | ia->ia_dstaddr = oldaddr; | |
727 | return(EINVAL); /* ifid is contradict */ | |
728 | } | |
729 | } | |
730 | #ifdef __APPLE__ | |
731 | error = dlil_ioctl(0, ifp, SIOCSIFDSTADDR, (caddr_t)ia); | |
732 | if (error == EOPNOTSUPP) | |
733 | error = 0; | |
734 | if (error) { | |
735 | ia->ia_dstaddr = oldaddr; | |
736 | return(error); | |
737 | } | |
738 | #else | |
739 | if (ifp->if_ioctl && (error = (ifp->if_ioctl) | |
740 | (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { | |
741 | #endif | |
742 | if (ia->ia_flags & IFA_ROUTE) { | |
743 | ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; | |
744 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); | |
745 | ia->ia_ifa.ifa_dstaddr = | |
746 | (struct sockaddr *)&ia->ia_dstaddr; | |
747 | rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); | |
748 | } | |
749 | break; | |
750 | ||
751 | #endif | |
752 | case SIOCGIFALIFETIME_IN6: | |
753 | ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; | |
754 | break; | |
755 | ||
756 | case SIOCSIFALIFETIME_IN6: | |
757 | ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; | |
758 | /* for sanity */ | |
759 | if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { | |
760 | ia->ia6_lifetime.ia6t_expire = | |
761 | time_second + ia->ia6_lifetime.ia6t_vltime; | |
762 | } else | |
763 | ia->ia6_lifetime.ia6t_expire = 0; | |
764 | if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { | |
765 | ia->ia6_lifetime.ia6t_preferred = | |
766 | time_second + ia->ia6_lifetime.ia6t_pltime; | |
767 | } else | |
768 | ia->ia6_lifetime.ia6t_preferred = 0; | |
769 | break; | |
770 | ||
771 | case SIOCSIFADDR_IN6: | |
772 | error = in6_ifinit(ifp, ia, &ifr->ifr_addr, 1); | |
773 | #if 0 | |
774 | /* | |
775 | * the code chokes if we are to assign multiple addresses with | |
776 | * the same address prefix (rtinit() will return EEXIST, which | |
777 | * is not fatal actually). we will get memory leak if we | |
778 | * don't do it. | |
779 | * -> we may want to hide EEXIST from rtinit(). | |
780 | */ | |
781 | undo: | |
782 | if (error && newifaddr) { | |
783 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
784 | if ((ifa = ifp->if_addrlist) == ia62ifa(ia)) | |
785 | ifp->if_addrlist = ifa->ifa_next; | |
786 | else { | |
787 | while (ifa->ifa_next && | |
788 | (ifa->ifa_next != ia62ifa(ia))) | |
789 | ifa = ifa->ifa_next; | |
790 | if (ifa->ifa_next) | |
791 | ifa->ifa_next = ia62ifa(ia)->ifa_next; | |
792 | else { | |
793 | printf("Couldn't unlink in6_ifaddr " | |
794 | "from ifp\n"); | |
795 | } | |
796 | } | |
797 | #else | |
798 | TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); | |
799 | #endif | |
800 | IFAFREE(&ia->ia_ifa); | |
801 | ||
802 | oia = ia; | |
803 | if (oia == (ia = in6_ifaddr)) | |
804 | in6_ifaddr = ia->ia_next; | |
805 | else { | |
806 | while (ia->ia_next && (ia->ia_next != oia)) | |
807 | ia = ia->ia_next; | |
808 | if (ia->ia_next) | |
809 | ia->ia_next = oia->ia_next; | |
810 | else { | |
811 | printf("Didn't unlink in6_ifaddr " | |
812 | "from list\n"); | |
813 | } | |
814 | } | |
815 | IFAFREE(&ia->ia_ifa); | |
816 | } | |
817 | #endif | |
818 | return error; | |
819 | ||
820 | #if COMPAT_IN6IFIOCTL /* XXX should be unused */ | |
821 | case SIOCSIFNETMASK_IN6: | |
822 | ia->ia_prefixmask = ifr->ifr_addr; | |
823 | bzero(&net, sizeof(net)); | |
824 | net.sin6_len = sizeof(struct sockaddr_in6); | |
825 | net.sin6_family = AF_INET6; | |
826 | net.sin6_port = htons(0); | |
827 | net.sin6_flowinfo = htonl(0); | |
828 | net.sin6_addr.s6_addr32[0] | |
829 | = ia->ia_addr.sin6_addr.s6_addr32[0] & | |
830 | ia->ia_prefixmask.sin6_addr.s6_addr32[0]; | |
831 | net.sin6_addr.s6_addr32[1] | |
832 | = ia->ia_addr.sin6_addr.s6_addr32[1] & | |
833 | ia->ia_prefixmask.sin6_addr.s6_addr32[1]; | |
834 | net.sin6_addr.s6_addr32[2] | |
835 | = ia->ia_addr.sin6_addr.s6_addr32[2] & | |
836 | ia->ia_prefixmask.sin6_addr.s6_addr32[2]; | |
837 | net.sin6_addr.s6_addr32[3] | |
838 | = ia->ia_addr.sin6_addr.s6_addr32[3] & | |
839 | ia->ia_prefixmask.sin6_addr.s6_addr32[3]; | |
840 | ia->ia_net = net; | |
841 | break; | |
842 | #endif | |
843 | ||
844 | case SIOCAIFADDR_IN6: | |
845 | prefixIsNew = 0; | |
846 | hostIsNew = 1; | |
847 | ||
848 | if (ifra->ifra_addr.sin6_len == 0) { | |
849 | ifra->ifra_addr = ia->ia_addr; | |
850 | hostIsNew = 0; | |
851 | } else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr, | |
852 | &ia->ia_addr.sin6_addr)) | |
853 | hostIsNew = 0; | |
854 | ||
855 | /* Validate address families: */ | |
856 | /* | |
857 | * The destination address for a p2p link must have a family | |
858 | * of AF_UNSPEC or AF_INET6. | |
859 | */ | |
860 | if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && | |
861 | ifra->ifra_dstaddr.sin6_family != AF_INET6 && | |
862 | ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) | |
863 | return(EAFNOSUPPORT); | |
864 | /* | |
865 | * The prefixmask must have a family of AF_UNSPEC or AF_INET6. | |
866 | */ | |
867 | if (ifra->ifra_prefixmask.sin6_family != AF_INET6 && | |
868 | ifra->ifra_prefixmask.sin6_family != AF_UNSPEC) | |
869 | return(EAFNOSUPPORT); | |
870 | ||
871 | if (ifra->ifra_prefixmask.sin6_len) { | |
872 | in6_ifscrub(ifp, ia); | |
873 | ia->ia_prefixmask = ifra->ifra_prefixmask; | |
874 | prefixIsNew = 1; | |
875 | } | |
876 | if ((ifp->if_flags & IFF_POINTOPOINT) && | |
877 | (ifra->ifra_dstaddr.sin6_family == AF_INET6)) { | |
878 | in6_ifscrub(ifp, ia); | |
879 | oldaddr = ia->ia_dstaddr; | |
880 | ia->ia_dstaddr = ifra->ifra_dstaddr; | |
881 | /* link-local index check: should be a separate function? */ | |
882 | if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) { | |
883 | if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) { | |
884 | /* | |
885 | * interface ID is not embedded by | |
886 | * the user | |
887 | */ | |
888 | ia->ia_dstaddr.sin6_addr.s6_addr16[1] | |
889 | = htons(ifp->if_index); | |
890 | } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] != | |
891 | htons(ifp->if_index)) { | |
892 | ia->ia_dstaddr = oldaddr; | |
893 | return(EINVAL); /* ifid is contradict */ | |
894 | } | |
895 | } | |
896 | prefixIsNew = 1; /* We lie; but effect's the same */ | |
897 | } | |
898 | if (hostIsNew || prefixIsNew) { | |
899 | error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0); | |
900 | #if 0 | |
901 | if (error) | |
902 | goto undo; | |
903 | #endif | |
904 | } | |
905 | if (hostIsNew && (ifp->if_flags & IFF_MULTICAST)) { | |
906 | int error_local = 0; | |
907 | ||
908 | /* | |
909 | * join solicited multicast addr for new host id | |
910 | */ | |
911 | struct in6_addr llsol; | |
912 | bzero(&llsol, sizeof(struct in6_addr)); | |
913 | llsol.s6_addr16[0] = htons(0xff02); | |
914 | llsol.s6_addr16[1] = htons(ifp->if_index); | |
915 | llsol.s6_addr32[1] = 0; | |
916 | llsol.s6_addr32[2] = htonl(1); | |
917 | llsol.s6_addr32[3] = | |
918 | ifra->ifra_addr.sin6_addr.s6_addr32[3]; | |
919 | llsol.s6_addr8[12] = 0xff; | |
920 | (void)in6_addmulti(&llsol, ifp, &error_local); | |
921 | if (error == 0) | |
922 | error = error_local; | |
923 | } | |
924 | ||
925 | ia->ia6_flags = ifra->ifra_flags; | |
926 | ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/ | |
927 | ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */ | |
928 | ||
929 | ia->ia6_lifetime = ifra->ifra_lifetime; | |
930 | /* for sanity */ | |
931 | if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { | |
932 | ia->ia6_lifetime.ia6t_expire = | |
933 | time_second + ia->ia6_lifetime.ia6t_vltime; | |
934 | } else | |
935 | ia->ia6_lifetime.ia6t_expire = 0; | |
936 | if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { | |
937 | ia->ia6_lifetime.ia6t_preferred = | |
938 | time_second + ia->ia6_lifetime.ia6t_pltime; | |
939 | } else | |
940 | ia->ia6_lifetime.ia6t_preferred = 0; | |
941 | ||
942 | /* | |
943 | * Perform DAD, if needed. | |
944 | * XXX It may be of use, if we can administratively | |
945 | * disable DAD. | |
946 | */ | |
947 | switch (ifp->if_type) { | |
948 | case IFT_ARCNET: | |
949 | case IFT_ETHER: | |
950 | case IFT_FDDI: | |
951 | #if 0 | |
952 | case IFT_ATM: | |
953 | case IFT_SLIP: | |
954 | case IFT_PPP: | |
955 | #endif | |
956 | /* Mobile IPv6 modification */ | |
957 | if ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) { | |
958 | ia->ia6_flags |= IN6_IFF_TENTATIVE; | |
959 | nd6_dad_start((struct ifaddr *)ia, NULL); | |
960 | } | |
961 | break; | |
962 | case IFT_DUMMY: | |
963 | case IFT_FAITH: | |
964 | case IFT_GIF: | |
965 | case IFT_LOOP: | |
966 | default: | |
967 | break; | |
968 | } | |
969 | ||
970 | if (hostIsNew) { | |
971 | int iilen; | |
972 | int error_local = 0; | |
973 | ||
974 | iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - | |
975 | in6_mask2len(&ia->ia_prefixmask.sin6_addr); | |
976 | error_local = in6_prefix_add_ifid(iilen, ia); | |
977 | if (error == 0) | |
978 | error = error_local; | |
979 | } | |
980 | ||
981 | return(error); | |
982 | ||
983 | case SIOCDIFADDR_IN6: | |
984 | in6_purgeaddr(&ia->ia_ifa, ifp); | |
985 | break; | |
986 | ||
987 | default: | |
988 | #ifdef __APPLE__ | |
989 | error = dlil_ioctl(0, ifp, cmd, (caddr_t)data); | |
990 | if (error == EOPNOTSUPP) | |
991 | error = 0; | |
992 | return error; | |
993 | ||
994 | #else | |
995 | if (ifp == NULL || ifp->if_ioctl == 0) | |
996 | return(EOPNOTSUPP); | |
997 | return((*ifp->if_ioctl)(ifp, cmd, data)); | |
998 | #endif | |
999 | } | |
1000 | return(0); | |
1001 | } | |
1002 | ||
1003 | void | |
1004 | in6_purgeaddr(ifa, ifp) | |
1005 | struct ifaddr *ifa; | |
1006 | struct ifnet *ifp; | |
1007 | { | |
1008 | struct in6_ifaddr *oia, *ia = (void *) ifa; | |
1009 | ||
1010 | in6_ifscrub(ifp, ia); | |
1011 | ||
1012 | if (ifp->if_flags & IFF_MULTICAST) { | |
1013 | /* | |
1014 | * delete solicited multicast addr for deleting host id | |
1015 | */ | |
1016 | struct in6_multi *in6m; | |
1017 | struct in6_addr llsol; | |
1018 | bzero(&llsol, sizeof(struct in6_addr)); | |
1019 | llsol.s6_addr16[0] = htons(0xff02); | |
1020 | llsol.s6_addr16[1] = htons(ifp->if_index); | |
1021 | llsol.s6_addr32[1] = 0; | |
1022 | llsol.s6_addr32[2] = htonl(1); | |
1023 | llsol.s6_addr32[3] = | |
1024 | ia->ia_addr.sin6_addr.s6_addr32[3]; | |
1025 | llsol.s6_addr8[12] = 0xff; | |
1026 | ||
1027 | IN6_LOOKUP_MULTI(llsol, ifp, in6m); | |
1028 | if (in6m) | |
1029 | in6_delmulti(in6m); | |
1030 | } | |
1031 | ||
1032 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
1033 | if ((ifa = ifp->if_addrlist) == ia62ifa(ia)) | |
1034 | ifp->if_addrlist = ifa->ifa_next; | |
1035 | else { | |
1036 | while (ifa->ifa_next && | |
1037 | (ifa->ifa_next != ia62ifa(ia))) | |
1038 | ifa = ifa->ifa_next; | |
1039 | if (ifa->ifa_next) | |
1040 | ifa->ifa_next = ia62ifa(ia)->ifa_next; | |
1041 | else | |
1042 | printf("Couldn't unlink in6_ifaddr from ifp\n"); | |
1043 | } | |
1044 | #else | |
1045 | TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); | |
1046 | #endif | |
1047 | IFAFREE(&ia->ia_ifa); | |
1048 | ||
1049 | oia = ia; | |
1050 | if (oia == (ia = in6_ifaddr)) | |
1051 | in6_ifaddr = ia->ia_next; | |
1052 | else { | |
1053 | while (ia->ia_next && (ia->ia_next != oia)) | |
1054 | ia = ia->ia_next; | |
1055 | if (ia->ia_next) | |
1056 | ia->ia_next = oia->ia_next; | |
1057 | else | |
1058 | printf("Didn't unlink in6_ifaddr from list\n"); | |
1059 | } | |
1060 | { | |
1061 | int iilen; | |
1062 | ||
1063 | iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - | |
1064 | in6_mask2len(&oia->ia_prefixmask.sin6_addr); | |
1065 | in6_prefix_remove_ifid(iilen, oia); | |
1066 | } | |
1067 | #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__) | |
1068 | if (oia->ia6_multiaddrs.lh_first != NULL) | |
1069 | in6_savemkludge(oia); | |
1070 | #endif | |
1071 | ||
1072 | IFAFREE(&oia->ia_ifa); | |
1073 | } | |
1074 | ||
1075 | /* | |
1076 | * SIOC[GAD]LIFADDR. | |
1077 | * SIOCGLIFADDR: get first address. (???) | |
1078 | * SIOCGLIFADDR with IFLR_PREFIX: | |
1079 | * get first address that matches the specified prefix. | |
1080 | * SIOCALIFADDR: add the specified address. | |
1081 | * SIOCALIFADDR with IFLR_PREFIX: | |
1082 | * add the specified prefix, filling hostid part from | |
1083 | * the first link-local address. prefixlen must be <= 64. | |
1084 | * SIOCDLIFADDR: delete the specified address. | |
1085 | * SIOCDLIFADDR with IFLR_PREFIX: | |
1086 | * delete the first address that matches the specified prefix. | |
1087 | * return values: | |
1088 | * EINVAL on invalid parameters | |
1089 | * EADDRNOTAVAIL on prefix match failed/specified address not found | |
1090 | * other values may be returned from in6_ioctl() | |
1091 | * | |
1092 | * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. | |
1093 | * this is to accomodate address naming scheme other than RFC2374, | |
1094 | * in the future. | |
1095 | * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 | |
1096 | * address encoding scheme. (see figure on page 8) | |
1097 | */ | |
1098 | static int | |
1099 | #if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3) && !defined (__APPLE__) | |
1100 | in6_lifaddr_ioctl(so, cmd, data, ifp, p) | |
1101 | struct socket *so; | |
1102 | u_long cmd; | |
1103 | caddr_t data; | |
1104 | struct ifnet *ifp; | |
1105 | struct proc *p; | |
1106 | #else | |
1107 | in6_lifaddr_ioctl(so, cmd, data, ifp) | |
1108 | struct socket *so; | |
1109 | u_long cmd; | |
1110 | caddr_t data; | |
1111 | struct ifnet *ifp; | |
1112 | #endif | |
1113 | { | |
1114 | struct if_laddrreq *iflr = (struct if_laddrreq *)data; | |
1115 | struct ifaddr *ifa; | |
1116 | struct sockaddr *sa; | |
1117 | ||
1118 | /* sanity checks */ | |
1119 | if (!data || !ifp) { | |
1120 | panic("invalid argument to in6_lifaddr_ioctl"); | |
1121 | /*NOTRECHED*/ | |
1122 | } | |
1123 | ||
1124 | switch (cmd) { | |
1125 | case SIOCGLIFADDR: | |
1126 | /* address must be specified on GET with IFLR_PREFIX */ | |
1127 | if ((iflr->flags & IFLR_PREFIX) == 0) | |
1128 | break; | |
1129 | /*FALLTHROUGH*/ | |
1130 | case SIOCALIFADDR: | |
1131 | case SIOCDLIFADDR: | |
1132 | /* address must be specified on ADD and DELETE */ | |
1133 | sa = (struct sockaddr *)&iflr->addr; | |
1134 | if (sa->sa_family != AF_INET6) | |
1135 | return EINVAL; | |
1136 | if (sa->sa_len != sizeof(struct sockaddr_in6)) | |
1137 | return EINVAL; | |
1138 | /* XXX need improvement */ | |
1139 | sa = (struct sockaddr *)&iflr->dstaddr; | |
1140 | if (sa->sa_family && sa->sa_family != AF_INET6) | |
1141 | return EINVAL; | |
1142 | if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) | |
1143 | return EINVAL; | |
1144 | break; | |
1145 | default: /*shouldn't happen*/ | |
1146 | #if 0 | |
1147 | panic("invalid cmd to in6_lifaddr_ioctl"); | |
1148 | /*NOTREACHED*/ | |
1149 | #else | |
1150 | return EOPNOTSUPP; | |
1151 | #endif | |
1152 | } | |
1153 | if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) | |
1154 | return EINVAL; | |
1155 | ||
1156 | switch (cmd) { | |
1157 | case SIOCALIFADDR: | |
1158 | { | |
1159 | struct in6_aliasreq ifra; | |
1160 | struct in6_addr *hostid = NULL; | |
1161 | int prefixlen; | |
1162 | ||
1163 | if ((iflr->flags & IFLR_PREFIX) != 0) { | |
1164 | struct sockaddr_in6 *sin6; | |
1165 | ||
1166 | /* | |
1167 | * hostid is to fill in the hostid part of the | |
1168 | * address. hostid points to the first link-local | |
1169 | * address attached to the interface. | |
1170 | */ | |
1171 | ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); | |
1172 | if (!ifa) | |
1173 | return EADDRNOTAVAIL; | |
1174 | hostid = IFA_IN6(ifa); | |
1175 | ||
1176 | /* prefixlen must be <= 64. */ | |
1177 | if (64 < iflr->prefixlen) | |
1178 | return EINVAL; | |
1179 | prefixlen = iflr->prefixlen; | |
1180 | ||
1181 | /* hostid part must be zero. */ | |
1182 | sin6 = (struct sockaddr_in6 *)&iflr->addr; | |
1183 | if (sin6->sin6_addr.s6_addr32[2] != 0 | |
1184 | || sin6->sin6_addr.s6_addr32[3] != 0) { | |
1185 | return EINVAL; | |
1186 | } | |
1187 | } else | |
1188 | prefixlen = iflr->prefixlen; | |
1189 | ||
1190 | /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ | |
1191 | bzero(&ifra, sizeof(ifra)); | |
1192 | bcopy(iflr->iflr_name, ifra.ifra_name, | |
1193 | sizeof(ifra.ifra_name)); | |
1194 | ||
1195 | bcopy(&iflr->addr, &ifra.ifra_addr, | |
1196 | ((struct sockaddr *)&iflr->addr)->sa_len); | |
1197 | if (hostid) { | |
1198 | /* fill in hostid part */ | |
1199 | ifra.ifra_addr.sin6_addr.s6_addr32[2] = | |
1200 | hostid->s6_addr32[2]; | |
1201 | ifra.ifra_addr.sin6_addr.s6_addr32[3] = | |
1202 | hostid->s6_addr32[3]; | |
1203 | } | |
1204 | ||
1205 | if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ | |
1206 | bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, | |
1207 | ((struct sockaddr *)&iflr->dstaddr)->sa_len); | |
1208 | if (hostid) { | |
1209 | ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = | |
1210 | hostid->s6_addr32[2]; | |
1211 | ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = | |
1212 | hostid->s6_addr32[3]; | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | ifra.ifra_prefixmask.sin6_family = AF_INET6; | |
1217 | ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); | |
1218 | in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); | |
1219 | ||
1220 | ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; | |
1221 | #if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3) && !defined (__APPLE__) | |
1222 | return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p); | |
1223 | #else | |
1224 | return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp); | |
1225 | #endif | |
1226 | } | |
1227 | case SIOCGLIFADDR: | |
1228 | case SIOCDLIFADDR: | |
1229 | { | |
1230 | struct in6_ifaddr *ia; | |
1231 | struct in6_addr mask, candidate, match; | |
1232 | struct sockaddr_in6 *sin6; | |
1233 | int cmp; | |
1234 | ||
1235 | bzero(&mask, sizeof(mask)); | |
1236 | if (iflr->flags & IFLR_PREFIX) { | |
1237 | /* lookup a prefix rather than address. */ | |
1238 | in6_len2mask(&mask, iflr->prefixlen); | |
1239 | ||
1240 | sin6 = (struct sockaddr_in6 *)&iflr->addr; | |
1241 | bcopy(&sin6->sin6_addr, &match, sizeof(match)); | |
1242 | match.s6_addr32[0] &= mask.s6_addr32[0]; | |
1243 | match.s6_addr32[1] &= mask.s6_addr32[1]; | |
1244 | match.s6_addr32[2] &= mask.s6_addr32[2]; | |
1245 | match.s6_addr32[3] &= mask.s6_addr32[3]; | |
1246 | ||
1247 | /* if you set extra bits, that's wrong */ | |
1248 | if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) | |
1249 | return EINVAL; | |
1250 | ||
1251 | cmp = 1; | |
1252 | } else { | |
1253 | if (cmd == SIOCGLIFADDR) { | |
1254 | /* on getting an address, take the 1st match */ | |
1255 | cmp = 0; /*XXX*/ | |
1256 | } else { | |
1257 | /* on deleting an address, do exact match */ | |
1258 | in6_len2mask(&mask, 128); | |
1259 | sin6 = (struct sockaddr_in6 *)&iflr->addr; | |
1260 | bcopy(&sin6->sin6_addr, &match, sizeof(match)); | |
1261 | ||
1262 | cmp = 1; | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
1267 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
1268 | #else | |
1269 | for (ifa = ifp->if_addrlist.tqh_first; | |
1270 | ifa; | |
1271 | ifa = ifa->ifa_list.tqe_next) | |
1272 | #endif | |
1273 | { | |
1274 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
1275 | continue; | |
1276 | if (!cmp) | |
1277 | break; | |
1278 | bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); | |
1279 | candidate.s6_addr32[0] &= mask.s6_addr32[0]; | |
1280 | candidate.s6_addr32[1] &= mask.s6_addr32[1]; | |
1281 | candidate.s6_addr32[2] &= mask.s6_addr32[2]; | |
1282 | candidate.s6_addr32[3] &= mask.s6_addr32[3]; | |
1283 | if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) | |
1284 | break; | |
1285 | } | |
1286 | if (!ifa) | |
1287 | return EADDRNOTAVAIL; | |
1288 | ia = ifa2ia6(ifa); | |
1289 | ||
1290 | if (cmd == SIOCGLIFADDR) { | |
1291 | /* fill in the if_laddrreq structure */ | |
1292 | bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); | |
1293 | ||
1294 | if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { | |
1295 | bcopy(&ia->ia_dstaddr, &iflr->dstaddr, | |
1296 | ia->ia_dstaddr.sin6_len); | |
1297 | } else | |
1298 | bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); | |
1299 | ||
1300 | iflr->prefixlen = | |
1301 | in6_mask2len(&ia->ia_prefixmask.sin6_addr); | |
1302 | ||
1303 | iflr->flags = ia->ia6_flags; /*XXX*/ | |
1304 | ||
1305 | return 0; | |
1306 | } else { | |
1307 | struct in6_aliasreq ifra; | |
1308 | ||
1309 | /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ | |
1310 | bzero(&ifra, sizeof(ifra)); | |
1311 | bcopy(iflr->iflr_name, ifra.ifra_name, | |
1312 | sizeof(ifra.ifra_name)); | |
1313 | ||
1314 | bcopy(&ia->ia_addr, &ifra.ifra_addr, | |
1315 | ia->ia_addr.sin6_len); | |
1316 | if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { | |
1317 | bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, | |
1318 | ia->ia_dstaddr.sin6_len); | |
1319 | } else { | |
1320 | bzero(&ifra.ifra_dstaddr, | |
1321 | sizeof(ifra.ifra_dstaddr)); | |
1322 | } | |
1323 | bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, | |
1324 | ia->ia_prefixmask.sin6_len); | |
1325 | ||
1326 | ifra.ifra_flags = ia->ia6_flags; | |
1327 | #if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3) && !defined (__APPLE__) | |
1328 | return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, | |
1329 | ifp, p); | |
1330 | #else | |
1331 | return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, | |
1332 | ifp); | |
1333 | #endif | |
1334 | } | |
1335 | } | |
1336 | } | |
1337 | ||
1338 | return EOPNOTSUPP; /*just for safety*/ | |
1339 | } | |
1340 | ||
1341 | /* | |
1342 | * Delete any existing route for an interface. | |
1343 | */ | |
1344 | void | |
1345 | in6_ifscrub(ifp, ia) | |
1346 | register struct ifnet *ifp; | |
1347 | register struct in6_ifaddr *ia; | |
1348 | { | |
1349 | if ((ia->ia_flags & IFA_ROUTE) == 0) | |
1350 | return; | |
1351 | if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) | |
1352 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); | |
1353 | else | |
1354 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); | |
1355 | ia->ia_flags &= ~IFA_ROUTE; | |
1356 | ||
1357 | /* Remove ownaddr's loopback rtentry, if it exists. */ | |
1358 | in6_ifremloop(&(ia->ia_ifa)); | |
1359 | } | |
1360 | ||
1361 | /* | |
1362 | * Initialize an interface's intetnet6 address | |
1363 | * and routing table entry. | |
1364 | */ | |
1365 | int | |
1366 | in6_ifinit(ifp, ia, sin6, scrub) | |
1367 | struct ifnet *ifp; | |
1368 | struct in6_ifaddr *ia; | |
1369 | struct sockaddr_in6 *sin6; | |
1370 | int scrub; | |
1371 | { | |
1372 | struct sockaddr_in6 oldaddr; | |
1373 | int error, flags = RTF_UP; | |
1374 | u_long dl_tag; | |
1375 | int s = splimp(); | |
1376 | ||
1377 | oldaddr = ia->ia_addr; | |
1378 | ia->ia_addr = *sin6; | |
1379 | /* | |
1380 | * Give the interface a chance to initialize | |
1381 | * if this is its first address, | |
1382 | * and to validate the address if necessary. | |
1383 | */ | |
1384 | #ifdef __APPLE__ | |
1385 | error = dlil_ioctl(0, ifp, SIOCSIFADDR, (caddr_t)ia) ; | |
1386 | if (error == EOPNOTSUPP) | |
1387 | error = 0; | |
1388 | if (error) { | |
1389 | ||
1390 | #else | |
1391 | if (ifp->if_ioctl && | |
1392 | (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { | |
1393 | #endif | |
1394 | printf("in6_ifinit SIOCSIFADDR for if=%s returns error=%x\n", if_name(ifp), error); | |
1395 | splx(s); | |
1396 | ia->ia_addr = oldaddr; | |
1397 | return(error); | |
1398 | } | |
1399 | ||
1400 | switch (ifp->if_type) { | |
1401 | case IFT_ARCNET: | |
1402 | case IFT_ETHER: | |
1403 | case IFT_FDDI: | |
1404 | ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; | |
1405 | ia->ia_ifa.ifa_flags |= RTF_CLONING; | |
1406 | break; | |
1407 | case IFT_PPP: | |
1408 | ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest; | |
1409 | ia->ia_ifa.ifa_flags |= RTF_CLONING; | |
1410 | break; | |
1411 | } | |
1412 | ||
1413 | splx(s); | |
1414 | if (scrub) { | |
1415 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; | |
1416 | in6_ifscrub(ifp, ia); | |
1417 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; | |
1418 | } | |
1419 | /* xxx | |
1420 | * in_socktrim | |
1421 | */ | |
1422 | /* | |
1423 | * Add route for the network. | |
1424 | */ | |
1425 | ia->ia_ifa.ifa_metric = ifp->if_metric; | |
1426 | if (ifp->if_flags & IFF_LOOPBACK) { | |
1427 | ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; | |
1428 | flags |= RTF_HOST; | |
1429 | } else if (ifp->if_flags & IFF_POINTOPOINT) { | |
1430 | if (ia->ia_dstaddr.sin6_family != AF_INET6) | |
1431 | return(0); | |
1432 | flags |= RTF_HOST; | |
1433 | } | |
1434 | if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) | |
1435 | ia->ia_flags |= IFA_ROUTE; | |
1436 | ||
1437 | /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */ | |
1438 | in6_ifaddloop(&(ia->ia_ifa)); | |
1439 | ||
1440 | #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__) | |
1441 | if (ifp->if_flags & IFF_MULTICAST) | |
1442 | in6_restoremkludge(ia, ifp); | |
1443 | #endif | |
1444 | ||
1445 | #ifdef __APPLE__ | |
1446 | printf("in6_ifinit: Attach dl_tag for if=%s%n\n", ifp->if_name, ifp->if_unit); | |
1447 | ||
1448 | if (strcmp(ifp->if_name, "en") == 0) | |
1449 | dl_tag = ether_attach_inet6(ifp); | |
1450 | ||
1451 | if (strcmp(ifp->if_name, "lo") == 0) | |
1452 | dl_tag = lo_attach_inet(ifp); | |
1453 | #if NGIF > 0 | |
1454 | if (strcmp(ifp->if_name, "gif") == 0) | |
1455 | dl_tag = gif_attach_inet(ifp); | |
1456 | #endif | |
1457 | /* End of temp code */ | |
1458 | ia->ia_ifa.ifa_dlt = dl_tag; | |
1459 | #endif | |
1460 | ||
1461 | return(error); | |
1462 | } | |
1463 | ||
1464 | #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__) | |
1465 | /* | |
1466 | * Multicast address kludge: | |
1467 | * If there were any multicast addresses attached to this interface address, | |
1468 | * either move them to another address on this interface, or save them until | |
1469 | * such time as this interface is reconfigured for IPv6. | |
1470 | */ | |
1471 | void | |
1472 | in6_savemkludge(oia) | |
1473 | struct in6_ifaddr *oia; | |
1474 | { | |
1475 | struct in6_ifaddr *ia; | |
1476 | struct in6_multi *in6m, *next; | |
1477 | ||
1478 | IFP_TO_IA6(oia->ia_ifp, ia); | |
1479 | if (ia) { /* there is another address */ | |
1480 | for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){ | |
1481 | next = in6m->in6m_entry.le_next; | |
1482 | IFAFREE(&in6m->in6m_ia->ia_ifa); | |
1483 | ia->ia_ifa.ifa_refcnt++; | |
1484 | in6m->in6m_ia = ia; | |
1485 | LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); | |
1486 | } | |
1487 | } else { /* last address on this if deleted, save */ | |
1488 | struct multi6_kludge *mk; | |
1489 | ||
1490 | mk = _MALLOC(sizeof(*mk), M_IPMADDR, M_WAITOK); | |
1491 | ||
1492 | LIST_INIT(&mk->mk_head); | |
1493 | mk->mk_ifp = oia->ia_ifp; | |
1494 | ||
1495 | for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){ | |
1496 | next = in6m->in6m_entry.le_next; | |
1497 | IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */ | |
1498 | in6m->in6m_ia = NULL; | |
1499 | LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry); | |
1500 | } | |
1501 | ||
1502 | if (mk->mk_head.lh_first != NULL) { | |
1503 | LIST_INSERT_HEAD(&in6_mk, mk, mk_entry); | |
1504 | } else { | |
1505 | FREE(mk, M_IPMADDR); | |
1506 | } | |
1507 | } | |
1508 | } | |
1509 | ||
1510 | /* | |
1511 | * Continuation of multicast address hack: | |
1512 | * If there was a multicast group list previously saved for this interface, | |
1513 | * then we re-attach it to the first address configured on the i/f. | |
1514 | */ | |
1515 | void | |
1516 | in6_restoremkludge(ia, ifp) | |
1517 | struct in6_ifaddr *ia; | |
1518 | struct ifnet *ifp; | |
1519 | { | |
1520 | struct multi6_kludge *mk; | |
1521 | ||
1522 | for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { | |
1523 | if (mk->mk_ifp == ifp) { | |
1524 | struct in6_multi *in6m, *next; | |
1525 | ||
1526 | for (in6m = mk->mk_head.lh_first; in6m; in6m = next){ | |
1527 | next = in6m->in6m_entry.le_next; | |
1528 | in6m->in6m_ia = ia; | |
1529 | ia->ia_ifa.ifa_refcnt++; | |
1530 | LIST_INSERT_HEAD(&ia->ia6_multiaddrs, | |
1531 | in6m, in6m_entry); | |
1532 | } | |
1533 | LIST_REMOVE(mk, mk_entry); | |
1534 | _FREE(mk, M_IPMADDR); | |
1535 | break; | |
1536 | } | |
1537 | } | |
1538 | } | |
1539 | ||
1540 | void | |
1541 | in6_purgemkludge(ifp) | |
1542 | struct ifnet *ifp; | |
1543 | { | |
1544 | struct multi6_kludge *mk; | |
1545 | struct in6_multi *in6m; | |
1546 | ||
1547 | for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { | |
1548 | if (mk->mk_ifp != ifp) | |
1549 | continue; | |
1550 | ||
1551 | /* leave from all multicast groups joined */ | |
1552 | while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) | |
1553 | in6_delmulti(in6m); | |
1554 | LIST_REMOVE(mk, mk_entry); | |
1555 | _FREE(mk, M_IPMADDR); | |
1556 | break; | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | /* | |
1561 | * Add an address to the list of IP6 multicast addresses for a | |
1562 | * given interface. | |
1563 | */ | |
1564 | struct in6_multi * | |
1565 | in6_addmulti(maddr6, ifp, errorp) | |
1566 | register struct in6_addr *maddr6; | |
1567 | register struct ifnet *ifp; | |
1568 | int *errorp; | |
1569 | { | |
1570 | struct in6_ifaddr *ia; | |
1571 | struct in6_ifreq ifr; | |
1572 | struct in6_multi *in6m; | |
1573 | #if __NetBSD__ | |
1574 | int s = splsoftnet(); | |
1575 | #else | |
1576 | int s = splnet(); | |
1577 | #endif | |
1578 | ||
1579 | *errorp = 0; | |
1580 | /* | |
1581 | * See if address already in list. | |
1582 | */ | |
1583 | IN6_LOOKUP_MULTI(*maddr6, ifp, in6m); | |
1584 | if (in6m != NULL) { | |
1585 | /* | |
1586 | * Found it; just increment the refrence count. | |
1587 | */ | |
1588 | in6m->in6m_refcount++; | |
1589 | } else { | |
1590 | /* | |
1591 | * New address; allocate a new multicast record | |
1592 | * and link it into the interface's multicast list. | |
1593 | */ | |
1594 | in6m = (struct in6_multi *) | |
1595 | _MALLOC(sizeof(*in6m), M_IPMADDR, M_NOTWAIT); | |
1596 | if (in6m == NULL) { | |
1597 | splx(s); | |
1598 | *errorp = ENOBUFS; | |
1599 | return(NULL); | |
1600 | } | |
1601 | in6m->in6m_addr = *maddr6; | |
1602 | in6m->in6m_ifp = ifp; | |
1603 | in6m->in6m_refcount = 1; | |
1604 | IFP_TO_IA6(ifp, ia); | |
1605 | if (ia == NULL) { | |
1606 | _FREE(in6m, M_IPMADDR); | |
1607 | splx(s); | |
1608 | *errorp = EADDRNOTAVAIL; /* appropriate? */ | |
1609 | return(NULL); | |
1610 | } | |
1611 | in6m->in6m_ia = ia; | |
1612 | ia->ia_ifa.ifa_refcnt++; /* gain a reference */ | |
1613 | LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); | |
1614 | ||
1615 | /* | |
1616 | * Ask the network driver to update its multicast reception | |
1617 | * filter appropriately for the new address. | |
1618 | */ | |
1619 | bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6)); | |
1620 | ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); | |
1621 | ifr.ifr_addr.sin6_family = AF_INET6; | |
1622 | ifr.ifr_addr.sin6_addr = *maddr6; | |
1623 | #ifdef __APPLE__ | |
1624 | *errorp = dlil_ioctl(0, ifp, SIOCADDMULTI, (caddr_t)&ifr); | |
1625 | printf("in6_addmulti: if=%s%n dlil_ioctl returns=%d\n", ifp->if_name, ifp->if_unit, *errorp); | |
1626 | if (*errorp == EOPNOTSUPP) | |
1627 | *errorp = 0; | |
1628 | ||
1629 | #else | |
1630 | if (ifp->if_ioctl == NULL) | |
1631 | *errorp = ENXIO; /* XXX: appropriate? */ | |
1632 | else | |
1633 | *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, | |
1634 | (caddr_t)&ifr); | |
1635 | #endif | |
1636 | if (*errorp) { | |
1637 | LIST_REMOVE(in6m, in6m_entry); | |
1638 | _FREE(in6m, M_IPMADDR); | |
1639 | splx(s); | |
1640 | return(NULL); | |
1641 | } | |
1642 | /* | |
1643 | * Let MLD6 know that we have joined a new IP6 multicast | |
1644 | * group. | |
1645 | */ | |
1646 | mld6_start_listening(in6m); | |
1647 | } | |
1648 | splx(s); | |
1649 | return(in6m); | |
1650 | } | |
1651 | ||
1652 | /* | |
1653 | * Delete a multicast address record. | |
1654 | */ | |
1655 | void | |
1656 | in6_delmulti(in6m) | |
1657 | struct in6_multi *in6m; | |
1658 | { | |
1659 | struct in6_ifreq ifr; | |
1660 | #if __NetBSD__ | |
1661 | int s = splsoftnet(); | |
1662 | #else | |
1663 | int s = splnet(); | |
1664 | #endif | |
1665 | ||
1666 | if (--in6m->in6m_refcount == 0) { | |
1667 | /* | |
1668 | * No remaining claims to this record; let MLD6 know | |
1669 | * that we are leaving the multicast group. | |
1670 | */ | |
1671 | mld6_stop_listening(in6m); | |
1672 | ||
1673 | /* | |
1674 | * Unlink from list. | |
1675 | */ | |
1676 | LIST_REMOVE(in6m, in6m_entry); | |
1677 | if (in6m->in6m_ia) | |
1678 | IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */ | |
1679 | ||
1680 | /* | |
1681 | * Notify the network driver to update its multicast | |
1682 | * reception filter. | |
1683 | */ | |
1684 | bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6)); | |
1685 | ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); | |
1686 | ifr.ifr_addr.sin6_family = AF_INET6; | |
1687 | ifr.ifr_addr.sin6_addr = in6m->in6m_addr; | |
1688 | #ifdef __APPLE__ | |
1689 | dlil_ioctl(0, in6m->in6m_ifp, SIOCDELMULTI, (caddr_t)&ifr); | |
1690 | #else | |
1691 | (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp, | |
1692 | SIOCDELMULTI, (caddr_t)&ifr); | |
1693 | #endif | |
1694 | _FREE(in6m, M_IPMADDR); | |
1695 | } | |
1696 | splx(s); | |
1697 | } | |
1698 | #else /* not FreeBSD3 */ | |
1699 | /* | |
1700 | * Add an address to the list of IP6 multicast addresses for a | |
1701 | * given interface. | |
1702 | */ | |
1703 | struct in6_multi * | |
1704 | in6_addmulti(maddr6, ifp, errorp) | |
1705 | register struct in6_addr *maddr6; | |
1706 | register struct ifnet *ifp; | |
1707 | int *errorp; | |
1708 | { | |
1709 | struct in6_multi *in6m; | |
1710 | struct sockaddr_in6 sin6; | |
1711 | struct ifmultiaddr *ifma; | |
1712 | int s = splnet(); | |
1713 | ||
1714 | *errorp = 0; | |
1715 | ||
1716 | /* | |
1717 | * Call generic routine to add membership or increment | |
1718 | * refcount. It wants addresses in the form of a sockaddr, | |
1719 | * so we build one here (being careful to zero the unused bytes). | |
1720 | */ | |
1721 | bzero(&sin6, sizeof sin6); | |
1722 | sin6.sin6_family = AF_INET6; | |
1723 | sin6.sin6_len = sizeof sin6; | |
1724 | sin6.sin6_addr = *maddr6; | |
1725 | *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma); | |
1726 | if (*errorp) { | |
1727 | splx(s); | |
1728 | return 0; | |
1729 | } | |
1730 | ||
1731 | /* | |
1732 | * If ifma->ifma_protospec is null, then if_addmulti() created | |
1733 | * a new record. Otherwise, we are done. | |
1734 | */ | |
1735 | if (ifma->ifma_protospec != 0) | |
1736 | return ifma->ifma_protospec; | |
1737 | ||
1738 | /* XXX - if_addmulti uses M_WAITOK. Can this really be called | |
1739 | at interrupt time? If so, need to fix if_addmulti. XXX */ | |
1740 | in6m = (struct in6_multi *)_MALLOC(sizeof(*in6m), M_IPMADDR, M_NOWAIT); | |
1741 | if (in6m == NULL) { | |
1742 | splx(s); | |
1743 | return (NULL); | |
1744 | } | |
1745 | ||
1746 | bzero(in6m, sizeof *in6m); | |
1747 | in6m->in6m_addr = *maddr6; | |
1748 | in6m->in6m_ifp = ifp; | |
1749 | in6m->in6m_ifma = ifma; | |
1750 | ifma->ifma_protospec = in6m; | |
1751 | LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry); | |
1752 | ||
1753 | /* | |
1754 | * Let MLD6 know that we have joined a new IP6 multicast | |
1755 | * group. | |
1756 | */ | |
1757 | mld6_start_listening(in6m); | |
1758 | splx(s); | |
1759 | return(in6m); | |
1760 | } | |
1761 | ||
1762 | /* | |
1763 | * Delete a multicast address record. | |
1764 | */ | |
1765 | void | |
1766 | in6_delmulti(in6m) | |
1767 | struct in6_multi *in6m; | |
1768 | { | |
1769 | struct ifmultiaddr *ifma = in6m->in6m_ifma; | |
1770 | int s = splnet(); | |
1771 | ||
1772 | if (ifma->ifma_refcount == 1) { | |
1773 | /* | |
1774 | * No remaining claims to this record; let MLD6 know | |
1775 | * that we are leaving the multicast group. | |
1776 | */ | |
1777 | mld6_stop_listening(in6m); | |
1778 | ifma->ifma_protospec = 0; | |
1779 | LIST_REMOVE(in6m, in6m_entry); | |
1780 | _FREE(in6m, M_IPMADDR); | |
1781 | } | |
1782 | /* XXX - should be separate API for when we have an ifma? */ | |
1783 | if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); | |
1784 | splx(s); | |
1785 | } | |
1786 | #endif /* not FreeBSD3 */ | |
1787 | ||
1788 | /* | |
1789 | * Find an IPv6 interface link-local address specific to an interface. | |
1790 | */ | |
1791 | struct in6_ifaddr * | |
1792 | in6ifa_ifpforlinklocal(ifp, ignoreflags) | |
1793 | struct ifnet *ifp; | |
1794 | int ignoreflags; | |
1795 | { | |
1796 | register struct ifaddr *ifa; | |
1797 | ||
1798 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
1799 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
1800 | #else | |
1801 | for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) | |
1802 | #endif | |
1803 | { | |
1804 | if (ifa->ifa_addr == NULL) | |
1805 | continue; /* just for safety */ | |
1806 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
1807 | continue; | |
1808 | if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { | |
1809 | if ((((struct in6_ifaddr *)ifa)->ia6_flags & | |
1810 | ignoreflags) != 0) | |
1811 | continue; | |
1812 | break; | |
1813 | } | |
1814 | } | |
1815 | ||
1816 | return((struct in6_ifaddr *)ifa); | |
1817 | } | |
1818 | ||
1819 | ||
1820 | /* | |
1821 | * find the internet address corresponding to a given interface and address. | |
1822 | */ | |
1823 | struct in6_ifaddr * | |
1824 | in6ifa_ifpwithaddr(ifp, addr) | |
1825 | struct ifnet *ifp; | |
1826 | struct in6_addr *addr; | |
1827 | { | |
1828 | register struct ifaddr *ifa; | |
1829 | ||
1830 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
1831 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
1832 | #else | |
1833 | for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) | |
1834 | #endif | |
1835 | { | |
1836 | if (ifa->ifa_addr == NULL) | |
1837 | continue; /* just for safety */ | |
1838 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
1839 | continue; | |
1840 | if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) | |
1841 | break; | |
1842 | } | |
1843 | ||
1844 | return((struct in6_ifaddr *)ifa); | |
1845 | } | |
1846 | ||
1847 | /* | |
1848 | * Convert IP6 address to printable (loggable) representation. | |
1849 | */ | |
1850 | static char digits[] = "0123456789abcdef"; | |
1851 | static int ip6round = 0; | |
1852 | char * | |
1853 | ip6_sprintf(addr) | |
1854 | register struct in6_addr *addr; | |
1855 | { | |
1856 | static char ip6buf[8][48]; | |
1857 | register int i; | |
1858 | register char *cp; | |
1859 | register u_short *a = (u_short *)addr; | |
1860 | register u_char *d; | |
1861 | int dcolon = 0; | |
1862 | ||
1863 | ip6round = (ip6round + 1) & 7; | |
1864 | cp = ip6buf[ip6round]; | |
1865 | ||
1866 | for (i = 0; i < 8; i++) { | |
1867 | if (dcolon == 1) { | |
1868 | if (*a == 0) { | |
1869 | if (i == 7) | |
1870 | *cp++ = ':'; | |
1871 | a++; | |
1872 | continue; | |
1873 | } else | |
1874 | dcolon = 2; | |
1875 | } | |
1876 | if (*a == 0) { | |
1877 | if (dcolon == 0 && *(a + 1) == 0) { | |
1878 | if (i == 0) | |
1879 | *cp++ = ':'; | |
1880 | *cp++ = ':'; | |
1881 | dcolon = 1; | |
1882 | } else { | |
1883 | *cp++ = '0'; | |
1884 | *cp++ = ':'; | |
1885 | } | |
1886 | a++; | |
1887 | continue; | |
1888 | } | |
1889 | d = (u_char *)a; | |
1890 | *cp++ = digits[*d >> 4]; | |
1891 | *cp++ = digits[*d++ & 0xf]; | |
1892 | *cp++ = digits[*d >> 4]; | |
1893 | *cp++ = digits[*d & 0xf]; | |
1894 | *cp++ = ':'; | |
1895 | a++; | |
1896 | } | |
1897 | *--cp = 0; | |
1898 | return(ip6buf[ip6round]); | |
1899 | } | |
1900 | ||
1901 | int | |
1902 | in6_localaddr(in6) | |
1903 | struct in6_addr *in6; | |
1904 | { | |
1905 | struct in6_ifaddr *ia; | |
1906 | ||
1907 | if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) | |
1908 | return 1; | |
1909 | ||
1910 | for (ia = in6_ifaddr; ia; ia = ia->ia_next) | |
1911 | if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, | |
1912 | &ia->ia_prefixmask.sin6_addr)) | |
1913 | return 1; | |
1914 | ||
1915 | return (0); | |
1916 | } | |
1917 | ||
1918 | /* | |
1919 | * Get a scope of the address. Node-local, link-local, site-local or global. | |
1920 | */ | |
1921 | int | |
1922 | in6_addrscope (addr) | |
1923 | struct in6_addr *addr; | |
1924 | { | |
1925 | int scope; | |
1926 | ||
1927 | if (addr->s6_addr8[0] == 0xfe) { | |
1928 | scope = addr->s6_addr8[1] & 0xc0; | |
1929 | ||
1930 | switch (scope) { | |
1931 | case 0x80: | |
1932 | return IPV6_ADDR_SCOPE_LINKLOCAL; | |
1933 | break; | |
1934 | case 0xc0: | |
1935 | return IPV6_ADDR_SCOPE_SITELOCAL; | |
1936 | break; | |
1937 | default: | |
1938 | return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */ | |
1939 | break; | |
1940 | } | |
1941 | } | |
1942 | ||
1943 | ||
1944 | if (addr->s6_addr8[0] == 0xff) { | |
1945 | scope = addr->s6_addr8[1] & 0x0f; | |
1946 | ||
1947 | /* | |
1948 | * due to other scope such as reserved, | |
1949 | * return scope doesn't work. | |
1950 | */ | |
1951 | switch (scope) { | |
1952 | case IPV6_ADDR_SCOPE_NODELOCAL: | |
1953 | return IPV6_ADDR_SCOPE_NODELOCAL; | |
1954 | break; | |
1955 | case IPV6_ADDR_SCOPE_LINKLOCAL: | |
1956 | return IPV6_ADDR_SCOPE_LINKLOCAL; | |
1957 | break; | |
1958 | case IPV6_ADDR_SCOPE_SITELOCAL: | |
1959 | return IPV6_ADDR_SCOPE_SITELOCAL; | |
1960 | break; | |
1961 | default: | |
1962 | return IPV6_ADDR_SCOPE_GLOBAL; | |
1963 | break; | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) { | |
1968 | if (addr->s6_addr8[15] == 1) /* loopback */ | |
1969 | return IPV6_ADDR_SCOPE_NODELOCAL; | |
1970 | if (addr->s6_addr8[15] == 0) /* unspecified */ | |
1971 | return IPV6_ADDR_SCOPE_LINKLOCAL; | |
1972 | } | |
1973 | ||
1974 | return IPV6_ADDR_SCOPE_GLOBAL; | |
1975 | } | |
1976 | ||
1977 | int | |
1978 | in6_addr2scopeid(ifp, addr) | |
1979 | struct ifnet *ifp; /* must not be NULL */ | |
1980 | struct in6_addr *addr; /* must not be NULL */ | |
1981 | { | |
1982 | int scope = in6_addrscope(addr); | |
1983 | ||
1984 | switch(scope) { | |
1985 | case IPV6_ADDR_SCOPE_NODELOCAL: | |
1986 | return(-1); /* XXX: is this an appropriate value? */ | |
1987 | ||
1988 | case IPV6_ADDR_SCOPE_LINKLOCAL: | |
1989 | /* XXX: we do not distinguish between a link and an I/F. */ | |
1990 | return(ifp->if_index); | |
1991 | ||
1992 | case IPV6_ADDR_SCOPE_SITELOCAL: | |
1993 | return(0); /* XXX: invalid. */ | |
1994 | ||
1995 | default: | |
1996 | return(0); /* XXX: treat as global. */ | |
1997 | } | |
1998 | } | |
1999 | ||
2000 | /* | |
2001 | * return length of part which dst and src are equal | |
2002 | * hard coding... | |
2003 | */ | |
2004 | ||
2005 | int | |
2006 | in6_matchlen(src, dst) | |
2007 | struct in6_addr *src, *dst; | |
2008 | { | |
2009 | int match = 0; | |
2010 | u_char *s = (u_char *)src, *d = (u_char *)dst; | |
2011 | u_char *lim = s + 16, r; | |
2012 | ||
2013 | while (s < lim) | |
2014 | if ((r = (*d++ ^ *s++)) != 0) { | |
2015 | while (r < 128) { | |
2016 | match++; | |
2017 | r <<= 1; | |
2018 | } | |
2019 | break; | |
2020 | } else | |
2021 | match += 8; | |
2022 | return match; | |
2023 | } | |
2024 | ||
2025 | int | |
2026 | in6_are_prefix_equal(p1, p2, len) | |
2027 | struct in6_addr *p1, *p2; | |
2028 | int len; | |
2029 | { | |
2030 | int bytelen, bitlen; | |
2031 | ||
2032 | /* sanity check */ | |
2033 | if (0 > len || len > 128) { | |
2034 | log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", | |
2035 | len); | |
2036 | return(0); | |
2037 | } | |
2038 | ||
2039 | bytelen = len / 8; | |
2040 | bitlen = len % 8; | |
2041 | ||
2042 | if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) | |
2043 | return(0); | |
2044 | if (p1->s6_addr[bytelen] >> (8 - bitlen) != | |
2045 | p2->s6_addr[bytelen] >> (8 - bitlen)) | |
2046 | return(0); | |
2047 | ||
2048 | return(1); | |
2049 | } | |
2050 | ||
2051 | void | |
2052 | in6_prefixlen2mask(maskp, len) | |
2053 | struct in6_addr *maskp; | |
2054 | int len; | |
2055 | { | |
2056 | u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; | |
2057 | int bytelen, bitlen, i; | |
2058 | ||
2059 | /* sanity check */ | |
2060 | if (0 > len || len > 128) { | |
2061 | log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", | |
2062 | len); | |
2063 | return; | |
2064 | } | |
2065 | ||
2066 | bzero(maskp, sizeof(*maskp)); | |
2067 | bytelen = len / 8; | |
2068 | bitlen = len % 8; | |
2069 | for (i = 0; i < bytelen; i++) | |
2070 | maskp->s6_addr[i] = 0xff; | |
2071 | if (bitlen) | |
2072 | maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; | |
2073 | } | |
2074 | ||
2075 | /* | |
2076 | * return the best address out of the same scope | |
2077 | */ | |
2078 | struct in6_ifaddr * | |
2079 | in6_ifawithscope(oifp, dst) | |
2080 | register struct ifnet *oifp; | |
2081 | register struct in6_addr *dst; | |
2082 | { | |
2083 | int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0; | |
2084 | int blen = -1; | |
2085 | struct ifaddr *ifa; | |
2086 | struct ifnet *ifp; | |
2087 | struct in6_ifaddr *ifa_best = NULL; | |
2088 | ||
2089 | if (oifp == NULL) { | |
2090 | printf("in6_ifawithscope: output interface is not specified\n"); | |
2091 | return(NULL); | |
2092 | } | |
2093 | ||
2094 | /* | |
2095 | * We search for all addresses on all interfaces from the beginning. | |
2096 | * Comparing an interface with the outgoing interface will be done | |
2097 | * only at the final stage of tiebreaking. | |
2098 | */ | |
2099 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
2100 | for (ifp = ifnet; ifp; ifp = ifp->if_next) | |
2101 | #else | |
2102 | for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) | |
2103 | #endif | |
2104 | { | |
2105 | /* | |
2106 | * We can never take an address that breaks the scope zone | |
2107 | * of the destination. | |
2108 | */ | |
2109 | if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst)) | |
2110 | continue; | |
2111 | ||
2112 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
2113 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
2114 | #elif defined(__FreeBSD__) && __FreeBSD__ >= 4 | |
2115 | TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) | |
2116 | #else | |
2117 | for (ifa = ifp->if_addrlist.tqh_first; ifa; | |
2118 | ifa = ifa->ifa_list.tqe_next) | |
2119 | #endif | |
2120 | { | |
2121 | int tlen = -1, dscopecmp, bscopecmp, matchcmp; | |
2122 | ||
2123 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
2124 | continue; | |
2125 | ||
2126 | src_scope = in6_addrscope(IFA_IN6(ifa)); | |
2127 | ||
2128 | #if ADDRSELECT_DEBUG /* should be removed after stabilization */ | |
2129 | dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); | |
2130 | printf("in6_ifawithscope: dst=%s bestaddr=%s, " | |
2131 | "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, " | |
2132 | "matchlen=%d, flgs=%x\n", | |
2133 | ip6_sprintf(dst), | |
2134 | ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none", | |
2135 | ip6_sprintf(IFA_IN6(ifa)), src_scope, | |
2136 | dscopecmp, | |
2137 | ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1, | |
2138 | in6_matchlen(IFA_IN6(ifa), dst), | |
2139 | ((struct in6_ifaddr *)ifa)->ia6_flags); | |
2140 | #endif | |
2141 | ||
2142 | /* | |
2143 | * Don't use an address before completing DAD | |
2144 | * nor a duplicated address. | |
2145 | */ | |
2146 | if (((struct in6_ifaddr *)ifa)->ia6_flags & | |
2147 | IN6_IFF_NOTREADY) | |
2148 | continue; | |
2149 | ||
2150 | /* XXX: is there any case to allow anycasts? */ | |
2151 | if (((struct in6_ifaddr *)ifa)->ia6_flags & | |
2152 | IN6_IFF_ANYCAST) | |
2153 | continue; | |
2154 | ||
2155 | if (((struct in6_ifaddr *)ifa)->ia6_flags & | |
2156 | IN6_IFF_DETACHED) | |
2157 | continue; | |
2158 | ||
2159 | /* | |
2160 | * If this is the first address we find, | |
2161 | * keep it anyway. | |
2162 | */ | |
2163 | if (ifa_best == NULL) | |
2164 | goto replace; | |
2165 | ||
2166 | /* | |
2167 | * ifa_best is never NULL beyond this line except | |
2168 | * within the block labeled "replace". | |
2169 | */ | |
2170 | ||
2171 | /* | |
2172 | * If ifa_best has a smaller scope than dst and | |
2173 | * the current address has a larger one than | |
2174 | * (or equal to) dst, always replace ifa_best. | |
2175 | * Also, if the current address has a smaller scope | |
2176 | * than dst, ignore it unless ifa_best also has a | |
2177 | * smaller scope. | |
2178 | */ | |
2179 | if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 && | |
2180 | IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0) | |
2181 | goto replace; | |
2182 | if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 && | |
2183 | IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) | |
2184 | continue; | |
2185 | ||
2186 | /* | |
2187 | * A deprecated address SHOULD NOT be used in new | |
2188 | * communications if an alternate (non-deprecated) | |
2189 | * address is available and has sufficient scope. | |
2190 | * RFC 2462, Section 5.5.4. | |
2191 | */ | |
2192 | if (((struct in6_ifaddr *)ifa)->ia6_flags & | |
2193 | IN6_IFF_DEPRECATED) { | |
2194 | /* | |
2195 | * Ignore any deprecated addresses if | |
2196 | * specified by configuration. | |
2197 | */ | |
2198 | if (!ip6_use_deprecated) | |
2199 | continue; | |
2200 | ||
2201 | /* | |
2202 | * If we have already found a non-deprecated | |
2203 | * candidate, just ignore deprecated addresses. | |
2204 | */ | |
2205 | if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) | |
2206 | == 0) | |
2207 | continue; | |
2208 | } | |
2209 | ||
2210 | /* | |
2211 | * A non-deprecated address is always preferred | |
2212 | * to a deprecated one regardless of scopes and | |
2213 | * address matching. | |
2214 | */ | |
2215 | if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) && | |
2216 | (((struct in6_ifaddr *)ifa)->ia6_flags & | |
2217 | IN6_IFF_DEPRECATED) == 0) | |
2218 | goto replace; | |
2219 | ||
2220 | /* | |
2221 | * At this point, we have two cases: | |
2222 | * 1. we are looking at a non-deprecated address, | |
2223 | * and ifa_best is also non-deprecated. | |
2224 | * 2. we are looking at a deprecated address, | |
2225 | * and ifa_best is also deprecated. | |
2226 | * Also, we do not have to consider a case where | |
2227 | * the scope of if_best is larger(smaller) than dst and | |
2228 | * the scope of the current address is smaller(larger) | |
2229 | * than dst. Such a case has already been covered. | |
2230 | * Tiebreaking is done according to the following | |
2231 | * items: | |
2232 | * - the scope comparison between the address and | |
2233 | * dst (dscopecmp) | |
2234 | * - the scope comparison between the address and | |
2235 | * ifa_best (bscopecmp) | |
2236 | * - if the address match dst longer than ifa_best | |
2237 | * (matchcmp) | |
2238 | * - if the address is on the outgoing I/F (outI/F) | |
2239 | * | |
2240 | * Roughly speaking, the selection policy is | |
2241 | * - the most important item is scope. The same scope | |
2242 | * is best. Then search for a larger scope. | |
2243 | * Smaller scopes are the last resort. | |
2244 | * - A deprecated address is chosen only when we have | |
2245 | * no address that has an enough scope, but is | |
2246 | * prefered to any addresses of smaller scopes. | |
2247 | * - Longest address match against dst is considered | |
2248 | * only for addresses that has the same scope of dst. | |
2249 | * - If there is no other reasons to choose one, | |
2250 | * addresses on the outgoing I/F are preferred. | |
2251 | * | |
2252 | * The precise decision table is as follows: | |
2253 | * dscopecmp bscopecmp matchcmp outI/F | replace? | |
2254 | * !equal equal N/A Yes | Yes (1) | |
2255 | * !equal equal N/A No | No (2) | |
2256 | * larger larger N/A N/A | No (3) | |
2257 | * larger smaller N/A N/A | Yes (4) | |
2258 | * smaller larger N/A N/A | Yes (5) | |
2259 | * smaller smaller N/A N/A | No (6) | |
2260 | * equal smaller N/A N/A | Yes (7) | |
2261 | * equal larger (already done) | |
2262 | * equal equal larger N/A | Yes (8) | |
2263 | * equal equal smaller N/A | No (9) | |
2264 | * equal equal equal Yes | Yes (a) | |
2265 | * eaual eqaul equal No | No (b) | |
2266 | */ | |
2267 | dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); | |
2268 | bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope); | |
2269 | ||
2270 | if (dscopecmp && bscopecmp == 0) { | |
2271 | if (oifp == ifp) /* (1) */ | |
2272 | goto replace; | |
2273 | continue; /* (2) */ | |
2274 | } | |
2275 | if (dscopecmp > 0) { | |
2276 | if (bscopecmp > 0) /* (3) */ | |
2277 | continue; | |
2278 | goto replace; /* (4) */ | |
2279 | } | |
2280 | if (dscopecmp < 0) { | |
2281 | if (bscopecmp > 0) /* (5) */ | |
2282 | goto replace; | |
2283 | continue; /* (6) */ | |
2284 | } | |
2285 | ||
2286 | /* now dscopecmp must be 0 */ | |
2287 | if (bscopecmp < 0) | |
2288 | goto replace; /* (7) */ | |
2289 | ||
2290 | /* | |
2291 | * At last both dscopecmp and bscopecmp must be 0. | |
2292 | * We need address matching against dst for | |
2293 | * tiebreaking. | |
2294 | */ | |
2295 | tlen = in6_matchlen(IFA_IN6(ifa), dst); | |
2296 | matchcmp = tlen - blen; | |
2297 | if (matchcmp > 0) /* (8) */ | |
2298 | goto replace; | |
2299 | if (matchcmp < 0) /* (9) */ | |
2300 | continue; | |
2301 | if (oifp == ifp) /* (a) */ | |
2302 | goto replace; | |
2303 | continue; /* (b) */ | |
2304 | ||
2305 | replace: | |
2306 | ifa_best = (struct in6_ifaddr *)ifa; | |
2307 | blen = tlen >= 0 ? tlen : | |
2308 | in6_matchlen(IFA_IN6(ifa), dst); | |
2309 | best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr); | |
2310 | } | |
2311 | } | |
2312 | ||
2313 | /* count statistics for future improvements */ | |
2314 | if (ifa_best == NULL) | |
2315 | ip6stat.ip6s_sources_none++; | |
2316 | else { | |
2317 | if (oifp == ifa_best->ia_ifp) | |
2318 | ip6stat.ip6s_sources_sameif[best_scope]++; | |
2319 | else | |
2320 | ip6stat.ip6s_sources_otherif[best_scope]++; | |
2321 | ||
2322 | if (best_scope == dst_scope) | |
2323 | ip6stat.ip6s_sources_samescope[best_scope]++; | |
2324 | else | |
2325 | ip6stat.ip6s_sources_otherscope[best_scope]++; | |
2326 | ||
2327 | if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0) | |
2328 | ip6stat.ip6s_sources_deprecated[best_scope]++; | |
2329 | } | |
2330 | ||
2331 | return(ifa_best); | |
2332 | } | |
2333 | ||
2334 | /* | |
2335 | * return the best address out of the same scope. if no address was | |
2336 | * found, return the first valid address from designated IF. | |
2337 | */ | |
2338 | ||
2339 | struct in6_ifaddr * | |
2340 | in6_ifawithifp(ifp, dst) | |
2341 | register struct ifnet *ifp; | |
2342 | register struct in6_addr *dst; | |
2343 | { | |
2344 | int dst_scope = in6_addrscope(dst), blen = -1, tlen; | |
2345 | struct ifaddr *ifa; | |
2346 | struct in6_ifaddr *besta = 0; | |
2347 | struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/ | |
2348 | ||
2349 | dep[0] = dep[1] = NULL; | |
2350 | ||
2351 | #if 0 | |
2352 | #if MIP6 | |
2353 | /* | |
2354 | * This is needed to assure that the Home Address is used for | |
2355 | * outgoing packets when not at home. We can't choose any other | |
2356 | * address if we want to keep connections up during movement. | |
2357 | */ | |
2358 | if (mip6_get_home_prefix_hook) { /* Only Mobile Node */ | |
2359 | struct nd_prefix *pr; | |
2360 | if ((pr = (*mip6_get_home_prefix_hook)()) && | |
2361 | !IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr)) | |
2362 | { | |
2363 | if (dst_scope == in6_addrscope(&pr->ndpr_addr)) { | |
2364 | #if MIP6_DEBUG | |
2365 | /* Noisy but useful */ | |
2366 | mip6_debug("%s: Local address %s is chosen " | |
2367 | "for pcb to dest %s.\n", | |
2368 | __FUNCTION__, | |
2369 | ip6_sprintf(&pr->ndpr_addr), | |
2370 | ip6_sprintf(dst)); | |
2371 | #endif | |
2372 | return(in6ifa_ifpwithaddr(ifp, &pr->ndpr_addr)); | |
2373 | } | |
2374 | } | |
2375 | } | |
2376 | #endif /* MIP6 */ | |
2377 | #endif /* 0 */ | |
2378 | ||
2379 | /* | |
2380 | * We first look for addresses in the same scope. | |
2381 | * If there is one, return it. | |
2382 | * If two or more, return one which matches the dst longest. | |
2383 | * If none, return one of global addresses assigned other ifs. | |
2384 | */ | |
2385 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
2386 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
2387 | #else | |
2388 | for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) | |
2389 | #endif | |
2390 | { | |
2391 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
2392 | continue; | |
2393 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) | |
2394 | continue; /* XXX: is there any case to allow anycast? */ | |
2395 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) | |
2396 | continue; /* don't use this interface */ | |
2397 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) | |
2398 | continue; | |
2399 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { | |
2400 | if (ip6_use_deprecated) | |
2401 | dep[0] = (struct in6_ifaddr *)ifa; | |
2402 | continue; | |
2403 | } | |
2404 | ||
2405 | if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { | |
2406 | /* | |
2407 | * call in6_matchlen() as few as possible | |
2408 | */ | |
2409 | if (besta) { | |
2410 | if (blen == -1) | |
2411 | blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); | |
2412 | tlen = in6_matchlen(IFA_IN6(ifa), dst); | |
2413 | if (tlen > blen) { | |
2414 | blen = tlen; | |
2415 | besta = (struct in6_ifaddr *)ifa; | |
2416 | } | |
2417 | } else | |
2418 | besta = (struct in6_ifaddr *)ifa; | |
2419 | } | |
2420 | } | |
2421 | if (besta) | |
2422 | return(besta); | |
2423 | ||
2424 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
2425 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
2426 | #else | |
2427 | for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) | |
2428 | #endif | |
2429 | { | |
2430 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
2431 | continue; | |
2432 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) | |
2433 | continue; /* XXX: is there any case to allow anycast? */ | |
2434 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) | |
2435 | continue; /* don't use this interface */ | |
2436 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) | |
2437 | continue; | |
2438 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { | |
2439 | if (ip6_use_deprecated) | |
2440 | dep[1] = (struct in6_ifaddr *)ifa; | |
2441 | continue; | |
2442 | } | |
2443 | ||
2444 | return (struct in6_ifaddr *)ifa; | |
2445 | } | |
2446 | ||
2447 | /* use the last-resort values, that are, deprecated addresses */ | |
2448 | if (dep[0]) | |
2449 | return dep[0]; | |
2450 | if (dep[1]) | |
2451 | return dep[1]; | |
2452 | ||
2453 | return NULL; | |
2454 | } | |
2455 | ||
2456 | /* | |
2457 | * perform DAD when interface becomes IFF_UP. | |
2458 | */ | |
2459 | void | |
2460 | in6_if_up(ifp) | |
2461 | struct ifnet *ifp; | |
2462 | { | |
2463 | struct ifaddr *ifa; | |
2464 | struct in6_ifaddr *ia; | |
2465 | struct sockaddr_dl *sdl; | |
2466 | int type; | |
2467 | #if __bsdi__ | |
2468 | u_char ea[ETHER_ADDR_LEN]; | |
2469 | #else | |
2470 | struct ether_addr ea; | |
2471 | #endif | |
2472 | int off; | |
2473 | int dad_delay; /* delay ticks before DAD output */ | |
2474 | ||
2475 | bzero(&ea, sizeof(ea)); | |
2476 | sdl = NULL; | |
2477 | ||
2478 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
2479 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
2480 | #else | |
2481 | for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) | |
2482 | #endif | |
2483 | { | |
2484 | if (ifa->ifa_addr->sa_family == AF_INET6 | |
2485 | && IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) { | |
2486 | goto dad; | |
2487 | } | |
2488 | if (ifa->ifa_addr->sa_family != AF_LINK) | |
2489 | continue; | |
2490 | sdl = (struct sockaddr_dl *)ifa->ifa_addr; | |
2491 | break; | |
2492 | } | |
2493 | ||
2494 | switch (ifp->if_type) { | |
2495 | case IFT_LOOP: | |
2496 | in6_ifattach(ifp, IN6_IFT_LOOP, NULL, 1); | |
2497 | break; | |
2498 | case IFT_SLIP: | |
2499 | case IFT_PPP: | |
2500 | case IFT_DUMMY: | |
2501 | case IFT_GIF: | |
2502 | case IFT_FAITH: | |
2503 | type = IN6_IFT_P2P; | |
2504 | in6_ifattach(ifp, type, 0, 1); | |
2505 | break; | |
2506 | #if IFT_STF | |
2507 | case IFT_STF: | |
2508 | /* | |
2509 | * This is VERY awkward to call nd6_ifattach while we will | |
2510 | * not do ND at all on the interface. It is necessary for | |
2511 | * initializing default hoplimit, and ND mtu. | |
2512 | */ | |
2513 | nd6_ifattach(ifp); | |
2514 | break; | |
2515 | #endif | |
2516 | case IFT_ETHER: | |
2517 | case IFT_FDDI: | |
2518 | case IFT_ATM: | |
2519 | type = IN6_IFT_802; | |
2520 | if (sdl == NULL) | |
2521 | break; | |
2522 | off = sdl->sdl_nlen; | |
2523 | if (bcmp(&sdl->sdl_data[off], &ea, sizeof(ea)) != 0) | |
2524 | in6_ifattach(ifp, type, LLADDR(sdl), 0); | |
2525 | break; | |
2526 | case IFT_ARCNET: | |
2527 | type = IN6_IFT_ARCNET; | |
2528 | if (sdl == NULL) | |
2529 | break; | |
2530 | off = sdl->sdl_nlen; | |
2531 | if (sdl->sdl_data[off] != 0) /* XXX ?: */ | |
2532 | in6_ifattach(ifp, type, LLADDR(sdl), 0); | |
2533 | break; | |
2534 | default: | |
2535 | break; | |
2536 | } | |
2537 | ||
2538 | dad: | |
2539 | dad_delay = 0; | |
2540 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
2541 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) | |
2542 | #else | |
2543 | for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) | |
2544 | #endif | |
2545 | { | |
2546 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
2547 | continue; | |
2548 | ia = (struct in6_ifaddr *)ifa; | |
2549 | if (ia->ia6_flags & IN6_IFF_TENTATIVE) | |
2550 | nd6_dad_start(ifa, &dad_delay); | |
2551 | } | |
2552 | } | |
2553 | ||
2554 | /* | |
2555 | * Calculate max IPv6 MTU through all the interfaces and store it | |
2556 | * to in6_maxmtu. | |
2557 | */ | |
2558 | void | |
2559 | in6_setmaxmtu() | |
2560 | { | |
2561 | unsigned long maxmtu = 0; | |
2562 | struct ifnet *ifp; | |
2563 | ||
2564 | #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) | |
2565 | for (ifp = ifnet; ifp; ifp = ifp->if_next) | |
2566 | #else | |
2567 | for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) | |
2568 | #endif | |
2569 | { | |
2570 | if ((ifp->if_flags & IFF_LOOPBACK) == 0 && | |
2571 | nd_ifinfo[ifp->if_index].linkmtu > maxmtu) | |
2572 | maxmtu = nd_ifinfo[ifp->if_index].linkmtu; | |
2573 | } | |
2574 | if (maxmtu) /* update only when maxmtu is positive */ | |
2575 | in6_maxmtu = maxmtu; | |
2576 | } | |
2577 | ||
2578 | #if MAPPED_ADDR_ENABLED | |
2579 | /* | |
2580 | * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be | |
2581 | * v4 mapped addr or v4 compat addr | |
2582 | */ | |
2583 | void | |
2584 | in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) | |
2585 | { | |
2586 | bzero(sin, sizeof(*sin)); | |
2587 | sin->sin_len = sizeof(struct sockaddr_in); | |
2588 | sin->sin_family = AF_INET; | |
2589 | sin->sin_port = sin6->sin6_port; | |
2590 | sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; | |
2591 | } | |
2592 | ||
2593 | /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ | |
2594 | void | |
2595 | in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) | |
2596 | { | |
2597 | bzero(sin6, sizeof(*sin6)); | |
2598 | sin6->sin6_len = sizeof(struct sockaddr_in6); | |
2599 | sin6->sin6_family = AF_INET6; | |
2600 | sin6->sin6_port = sin->sin_port; | |
2601 | sin6->sin6_addr.s6_addr32[0] = 0; | |
2602 | sin6->sin6_addr.s6_addr32[1] = 0; | |
2603 | sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; | |
2604 | sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; | |
2605 | } | |
2606 | ||
2607 | /* Convert sockaddr_in6 into sockaddr_in. */ | |
2608 | void | |
2609 | in6_sin6_2_sin_in_sock(struct sockaddr *nam) | |
2610 | { | |
2611 | struct sockaddr_in *sin_p; | |
2612 | struct sockaddr_in6 sin6; | |
2613 | ||
2614 | /* | |
2615 | * Save original sockaddr_in6 addr and convert it | |
2616 | * to sockaddr_in. | |
2617 | */ | |
2618 | sin6 = *(struct sockaddr_in6 *)nam; | |
2619 | sin_p = (struct sockaddr_in *)nam; | |
2620 | in6_sin6_2_sin(sin_p, &sin6); | |
2621 | } | |
2622 | ||
2623 | /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ | |
2624 | void | |
2625 | in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) | |
2626 | { | |
2627 | struct sockaddr_in *sin_p; | |
2628 | struct sockaddr_in6 *sin6_p; | |
2629 | ||
2630 | MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, | |
2631 | M_WAITOK); | |
2632 | sin_p = (struct sockaddr_in *)*nam; | |
2633 | in6_sin_2_v4mapsin6(sin_p, sin6_p); | |
2634 | FREE(*nam, M_SONAME); | |
2635 | *nam = (struct sockaddr *)sin6_p; | |
2636 | } | |
2637 | #endif /* MAPPED_ADDR_ENABLED */ | |
2638 |