Deleted Added
full compact
in6.c (71207) in6.c (78064)
1/* $FreeBSD: head/sys/netinet6/in6.c 71207 2001-01-18 06:07:53Z itojun $ */
2/* $KAME: in6.c,v 1.99 2000/07/11 17:00:58 jinmei Exp $ */
1/* $FreeBSD: head/sys/netinet6/in6.c 78064 2001-06-11 12:39:29Z ume $ */
2/* $KAME: in6.c,v 1.187 2001/05/24 07:43:59 itojun Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:

--- 72 unchanged lines hidden (view full) ---

83#include <net/if.h>
84#include <net/if_types.h>
85#include <net/route.h>
86#include <net/if_dl.h>
87
88#include <netinet/in.h>
89#include <netinet/in_var.h>
90#include <netinet/if_ether.h>
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:

--- 72 unchanged lines hidden (view full) ---

83#include <net/if.h>
84#include <net/if_types.h>
85#include <net/route.h>
86#include <net/if_dl.h>
87
88#include <netinet/in.h>
89#include <netinet/in_var.h>
90#include <netinet/if_ether.h>
91#ifndef SCOPEDROUTING
92#include <netinet/in_systm.h>
93#include <netinet/ip.h>
94#include <netinet/in_pcb.h>
95#endif
91
92#include <netinet6/nd6.h>
93#include <netinet/ip6.h>
94#include <netinet6/ip6_var.h>
95#include <netinet6/mld6_var.h>
96#include <netinet6/ip6_mroute.h>
97#include <netinet6/in6_ifattach.h>
98#include <netinet6/scope6_var.h>
96
97#include <netinet6/nd6.h>
98#include <netinet/ip6.h>
99#include <netinet6/ip6_var.h>
100#include <netinet6/mld6_var.h>
101#include <netinet6/ip6_mroute.h>
102#include <netinet6/in6_ifattach.h>
103#include <netinet6/scope6_var.h>
104#ifndef SCOPEDROUTING
105#include <netinet6/in6_pcb.h>
106#endif
99
100#include "gif.h"
101#if NGIF > 0
102#include <net/if_gif.h>
103#endif
104
105#include <net/net_osdep.h>
106

--- 12 unchanged lines hidden (view full) ---

119 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
120
121const struct in6_addr in6mask0 = IN6MASK0;
122const struct in6_addr in6mask32 = IN6MASK32;
123const struct in6_addr in6mask64 = IN6MASK64;
124const struct in6_addr in6mask96 = IN6MASK96;
125const struct in6_addr in6mask128 = IN6MASK128;
126
107
108#include "gif.h"
109#if NGIF > 0
110#include <net/if_gif.h>
111#endif
112
113#include <net/net_osdep.h>
114

--- 12 unchanged lines hidden (view full) ---

127 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
128
129const struct in6_addr in6mask0 = IN6MASK0;
130const struct in6_addr in6mask32 = IN6MASK32;
131const struct in6_addr in6mask64 = IN6MASK64;
132const struct in6_addr in6mask96 = IN6MASK96;
133const struct in6_addr in6mask128 = IN6MASK128;
134
135const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
136 0, 0, IN6ADDR_ANY_INIT, 0};
137
127static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
128 struct ifnet *, struct proc *));
138static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
139 struct ifnet *, struct proc *));
140static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
141 struct sockaddr_in6 *, int));
142static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
129
130struct in6_multihead in6_multihead; /* XXX BSS initialization */
131
132/*
143
144struct in6_multihead in6_multihead; /* XXX BSS initialization */
145
146/*
133 * Check if the loopback entry will be automatically generated.
134 * if 0 returned, will not be automatically generated.
135 * if 1 returned, will be automatically generated.
136 */
137static int
138in6_is_ifloop_auto(struct ifaddr *ifa)
139{
140#define SIN6(s) ((struct sockaddr_in6 *)s)
141 /*
142 * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT),
143 * or netmask is all0 or all1, then cloning will not happen,
144 * then we can't rely on its loopback entry generation.
145 */
146 if ((ifa->ifa_flags & RTF_CLONING) == 0 ||
147 (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) ||
148 (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6)
149 &&
150 IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr,
151 &in6mask128)) ||
152 ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0)
153 return 0;
154 else
155 return 1;
156#undef SIN6
157}
158
159/*
160 * Subroutine for in6_ifaddloop() and in6_ifremloop().
161 * This routine does actual work.
162 */
163static void
164in6_ifloop_request(int cmd, struct ifaddr *ifa)
165{
147 * Subroutine for in6_ifaddloop() and in6_ifremloop().
148 * This routine does actual work.
149 */
150static void
151in6_ifloop_request(int cmd, struct ifaddr *ifa)
152{
166 struct sockaddr_in6 lo_sa;
167 struct sockaddr_in6 all1_sa;
153 struct sockaddr_in6 all1_sa;
168 struct rtentry *nrt = NULL, **nrtp = NULL;
154 struct rtentry *nrt = NULL;
155 int e;
169
156
170 bzero(&lo_sa, sizeof(lo_sa));
171 bzero(&all1_sa, sizeof(all1_sa));
157 bzero(&all1_sa, sizeof(all1_sa));
172 lo_sa.sin6_family = AF_INET6;
173 lo_sa.sin6_len = sizeof(struct sockaddr_in6);
174 all1_sa = lo_sa;
175 lo_sa.sin6_addr = in6addr_loopback;
158 all1_sa.sin6_family = AF_INET6;
159 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
176 all1_sa.sin6_addr = in6mask128;
160 all1_sa.sin6_addr = in6mask128;
177
161
178 /*
162 /*
179 * So we add or remove static loopback entry, here.
180 * This request for deletion could fail, e.g. when we remove
181 * an address right after adding it.
163 * We specify the address itself as the gateway, and set the
164 * RTF_LLINFO flag, so that the corresponding host route would have
165 * the flag, and thus applications that assume traditional behavior
166 * would be happy. Note that we assume the caller of the function
167 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
168 * which changes the outgoing interface to the loopback interface.
182 */
169 */
183 if (cmd == RTM_ADD)
184 nrtp = &nrt;
185 rtrequest(cmd, ifa->ifa_addr,
186 (struct sockaddr *)&lo_sa,
187 (struct sockaddr *)&all1_sa,
188 RTF_UP|RTF_HOST, nrtp);
170 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
171 (struct sockaddr *)&all1_sa,
172 RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
173 if (e != 0) {
174 log(LOG_ERR, "in6_ifloop_request: "
175 "%s operation failed for %s (errno=%d)\n",
176 cmd == RTM_ADD ? "ADD" : "DELETE",
177 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
178 e);
179 }
189
190 /*
191 * Make sure rt_ifa be equal to IFA, the second argument of the
192 * function.
180
181 /*
182 * Make sure rt_ifa be equal to IFA, the second argument of the
183 * function.
193 * We need this because when we refer rt_ifa->ia6_flags in ip6_input,
194 * we assume that the rt_ifa points to the address instead of the
195 * loopback address.
184 * We need this because when we refer to rt_ifa->ia6_flags in
185 * ip6_input, we assume that the rt_ifa points to the address instead
186 * of the loopback address.
196 */
197 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
198 IFAFREE(nrt->rt_ifa);
187 */
188 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
189 IFAFREE(nrt->rt_ifa);
199 ifa->ifa_refcnt++;
190 IFAREF(ifa);
200 nrt->rt_ifa = ifa;
201 }
191 nrt->rt_ifa = ifa;
192 }
202 if (nrt)
203 nrt->rt_refcnt--;
193
194 /*
195 * Report the addition/removal of the address to the routing socket.
196 * XXX: since we called rtinit for a p2p interface with a destination,
197 * we end up reporting twice in such a case. Should we rather
198 * omit the second report?
199 */
200 if (nrt) {
201 rt_newaddrmsg(cmd, ifa, e, nrt);
202 if (cmd == RTM_DELETE) {
203 if (nrt->rt_refcnt <= 0) {
204 /* XXX: we should free the entry ourselves. */
205 nrt->rt_refcnt++;
206 rtfree(nrt);
207 }
208 } else {
209 /* the cmd must be RTM_ADD here */
210 nrt->rt_refcnt--;
211 }
212 }
204}
205
206/*
213}
214
215/*
207 * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link).
208 * Because, KAME needs loopback rtentry for ownaddr check in
209 * ip6_input().
216 * Add ownaddr as loopback rtentry. We previously add the route only if
217 * necessary (ex. on a p2p link). However, since we now manage addresses
218 * separately from prefixes, we should always add the route. We can't
219 * rely on the cloning mechanism from the corresponding interface route
220 * any more.
210 */
211static void
212in6_ifaddloop(struct ifaddr *ifa)
213{
221 */
222static void
223in6_ifaddloop(struct ifaddr *ifa)
224{
214 if (!in6_is_ifloop_auto(ifa)) {
215 struct rtentry *rt;
225 struct rtentry *rt;
216
226
217 /* If there is no loopback entry, allocate one. */
218 rt = rtalloc1(ifa->ifa_addr, 0, 0);
219 if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
220 in6_ifloop_request(RTM_ADD, ifa);
221 if (rt)
222 rt->rt_refcnt--;
223 }
227 /* If there is no loopback entry, allocate one. */
228 rt = rtalloc1(ifa->ifa_addr, 0, 0);
229 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
230 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
231 in6_ifloop_request(RTM_ADD, ifa);
232 if (rt)
233 rt->rt_refcnt--;
224}
225
226/*
227 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
228 * if it exists.
229 */
230static void
231in6_ifremloop(struct ifaddr *ifa)
232{
233 struct in6_ifaddr *ia;
234}
235
236/*
237 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
238 * if it exists.
239 */
240static void
241in6_ifremloop(struct ifaddr *ifa)
242{
243 struct in6_ifaddr *ia;
244 struct rtentry *rt;
234 int ia_count = 0;
235
236 /*
245 int ia_count = 0;
246
247 /*
237 * All BSD variants except BSD/OS do not remove cloned routes
248 * Some of BSD variants do not remove cloned routes
238 * from an interface direct route, when removing the direct route
249 * from an interface direct route, when removing the direct route
239 * (see commens in net/net_osdep.h).
240 * So we should remove the route corresponding to the deleted address
250 * (see comments in net/net_osdep.h). Even for variants that do remove
251 * cloned routes, they could fail to remove the cloned routes when
252 * we handle multple addresses that share a common prefix.
253 * So, we should remove the route corresponding to the deleted address
241 * regardless of the result of in6_is_ifloop_auto().
242 */
254 * regardless of the result of in6_is_ifloop_auto().
255 */
243 if (1)
244 {
245 /* If only one ifa for the loopback entry, delete it. */
246 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
247 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa),
248 &ia->ia_addr.sin6_addr)) {
249 ia_count++;
250 if (ia_count > 1)
251 break;
252 }
256
257 /*
258 * Delete the entry only if exact one ifa exists. More than one ifa
259 * can exist if we assign a same single address to multiple
260 * (probably p2p) interfaces.
261 * XXX: we should avoid such a configuration in IPv6...
262 */
263 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
264 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
265 ia_count++;
266 if (ia_count > 1)
267 break;
253 }
268 }
254 if (ia_count == 1)
269 }
270
271 if (ia_count == 1) {
272 /*
273 * Before deleting, check if a corresponding loopbacked host
274 * route surely exists. With this check, we can avoid to
275 * delete an interface direct route whose destination is same
276 * as the address being removed. This can happen when remofing
277 * a subnet-router anycast address on an interface attahced
278 * to a shared medium.
279 */
280 rt = rtalloc1(ifa->ifa_addr, 0, 0);
281 if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
282 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
283 rt->rt_refcnt--;
255 in6_ifloop_request(RTM_DELETE, ifa);
284 in6_ifloop_request(RTM_DELETE, ifa);
285 }
256 }
257}
258
259int
260in6_ifindex2scopeid(idx)
261 int idx;
262{
263 struct ifnet *ifp;

--- 12 unchanged lines hidden (view full) ---

276 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
277 return sin6->sin6_scope_id & 0xffff;
278 }
279
280 return -1;
281}
282
283int
286 }
287}
288
289int
290in6_ifindex2scopeid(idx)
291 int idx;
292{
293 struct ifnet *ifp;

--- 12 unchanged lines hidden (view full) ---

306 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
307 return sin6->sin6_scope_id & 0xffff;
308 }
309
310 return -1;
311}
312
313int
284in6_mask2len(mask)
314in6_mask2len(mask, lim0)
285 struct in6_addr *mask;
315 struct in6_addr *mask;
316 u_char *lim0;
286{
317{
287 int x, y;
318 int x = 0, y;
319 u_char *lim = lim0, *p;
288
320
289 for (x = 0; x < sizeof(*mask); x++) {
290 if (mask->s6_addr8[x] != 0xff)
321 if (lim0 == NULL ||
322 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
323 lim = (u_char *)mask + sizeof(*mask);
324 for (p = (u_char *)mask; p < lim; x++, p++) {
325 if (*p != 0xff)
291 break;
292 }
293 y = 0;
326 break;
327 }
328 y = 0;
294 if (x < sizeof(*mask)) {
329 if (p < lim) {
295 for (y = 0; y < 8; y++) {
330 for (y = 0; y < 8; y++) {
296 if ((mask->s6_addr8[x] & (0x80 >> y)) == 0)
331 if ((*p & (0x80 >> y)) == 0)
297 break;
298 }
299 }
332 break;
333 }
334 }
335
336 /*
337 * when the limit pointer is given, do a stricter check on the
338 * remaining bits.
339 */
340 if (p < lim) {
341 if (y != 0 && (*p & (0x00ff >> y)) != 0)
342 return(-1);
343 for (p = p + 1; p < lim; p++)
344 if (*p != 0)
345 return(-1);
346 }
347
300 return x * 8 + y;
301}
302
303void
304in6_len2mask(mask, len)
305 struct in6_addr *mask;
306 int len;
307{

--- 13 unchanged lines hidden (view full) ---

321in6_control(so, cmd, data, ifp, p)
322 struct socket *so;
323 u_long cmd;
324 caddr_t data;
325 struct ifnet *ifp;
326 struct proc *p;
327{
328 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
348 return x * 8 + y;
349}
350
351void
352in6_len2mask(mask, len)
353 struct in6_addr *mask;
354 int len;
355{

--- 13 unchanged lines hidden (view full) ---

369in6_control(so, cmd, data, ifp, p)
370 struct socket *so;
371 u_long cmd;
372 caddr_t data;
373 struct ifnet *ifp;
374 struct proc *p;
375{
376 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
329 struct in6_ifaddr *ia = NULL, *oia;
377 struct in6_ifaddr *ia = NULL;
330 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
378 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
331 struct sockaddr_in6 oldaddr;
332#ifdef COMPAT_IN6IFIOCTL
333 struct sockaddr_in6 net;
334#endif
335 int error = 0, hostIsNew, prefixIsNew;
336 int newifaddr;
337 int privileged;
338
339 privileged = 0;
340 if (p == NULL || !suser(p))
341 privileged++;
342
379 int privileged;
380
381 privileged = 0;
382 if (p == NULL || !suser(p))
383 privileged++;
384
343 /*
344 * xxx should prevent processes for link-local addresses?
345 */
346#if NGIF > 0
347 if (ifp && ifp->if_type == IFT_GIF) {
348 switch (cmd) {
349 case SIOCSIFPHYADDR_IN6:
350 if (!privileged)
351 return(EPERM);
352 /*fall through*/
353 case SIOCGIFPSRCADDR_IN6:
354 case SIOCGIFPDSTADDR_IN6:
355 return gif_ioctl(ifp, cmd, data);
356 }
357 }
358#endif
359 switch (cmd) {
360 case SIOCGETSGCNT_IN6:
361 case SIOCGETMIFCNT_IN6:
362 return (mrt6_ioctl(cmd, data));
363 }
364
365 if (ifp == NULL)
366 return(EOPNOTSUPP);
367
368 switch (cmd) {
369 case SIOCSNDFLUSH_IN6:
370 case SIOCSPFXFLUSH_IN6:
371 case SIOCSRTRFLUSH_IN6:
372 case SIOCSDEFIFACE_IN6:
373 case SIOCSIFINFO_FLAGS:
374 if (!privileged)
375 return(EPERM);
376 /*fall through*/
385 switch (cmd) {
386 case SIOCGETSGCNT_IN6:
387 case SIOCGETMIFCNT_IN6:
388 return (mrt6_ioctl(cmd, data));
389 }
390
391 if (ifp == NULL)
392 return(EOPNOTSUPP);
393
394 switch (cmd) {
395 case SIOCSNDFLUSH_IN6:
396 case SIOCSPFXFLUSH_IN6:
397 case SIOCSRTRFLUSH_IN6:
398 case SIOCSDEFIFACE_IN6:
399 case SIOCSIFINFO_FLAGS:
400 if (!privileged)
401 return(EPERM);
402 /*fall through*/
403 case OSIOCGIFINFO_IN6:
377 case SIOCGIFINFO_IN6:
378 case SIOCGDRLST_IN6:
379 case SIOCGPRLST_IN6:
380 case SIOCGNBRINFO_IN6:
381 case SIOCGDEFIFACE_IN6:
382 return(nd6_ioctl(cmd, data, ifp));
383 }
384
385 switch (cmd) {
386 case SIOCSIFPREFIX_IN6:
387 case SIOCDIFPREFIX_IN6:
388 case SIOCAIFPREFIX_IN6:
389 case SIOCCIFPREFIX_IN6:
390 case SIOCSGIFPREFIX_IN6:
404 case SIOCGIFINFO_IN6:
405 case SIOCGDRLST_IN6:
406 case SIOCGPRLST_IN6:
407 case SIOCGNBRINFO_IN6:
408 case SIOCGDEFIFACE_IN6:
409 return(nd6_ioctl(cmd, data, ifp));
410 }
411
412 switch (cmd) {
413 case SIOCSIFPREFIX_IN6:
414 case SIOCDIFPREFIX_IN6:
415 case SIOCAIFPREFIX_IN6:
416 case SIOCCIFPREFIX_IN6:
417 case SIOCSGIFPREFIX_IN6:
391 if (!privileged)
392 return(EPERM);
393 /*fall through*/
394 case SIOCGIFPREFIX_IN6:
418 case SIOCGIFPREFIX_IN6:
395 if (ip6_forwarding == 0)
396 return(EPERM);
397 return(in6_prefix_ioctl(so, cmd, data, ifp));
419 log(LOG_NOTICE,
420 "prefix ioctls are now invalidated. "
421 "please use ifconfig.\n");
422 return(EOPNOTSUPP);
398 }
399
400 switch(cmd) {
401 case SIOCSSCOPE6:
402 if (!privileged)
403 return(EPERM);
404 return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id));
405 break;

--- 19 unchanged lines hidden (view full) ---

425 * Find address for this interface, if it exists.
426 */
427 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
428 struct sockaddr_in6 *sa6 =
429 (struct sockaddr_in6 *)&ifra->ifra_addr;
430
431 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
432 if (sa6->sin6_addr.s6_addr16[1] == 0) {
423 }
424
425 switch(cmd) {
426 case SIOCSSCOPE6:
427 if (!privileged)
428 return(EPERM);
429 return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id));
430 break;

--- 19 unchanged lines hidden (view full) ---

450 * Find address for this interface, if it exists.
451 */
452 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
453 struct sockaddr_in6 *sa6 =
454 (struct sockaddr_in6 *)&ifra->ifra_addr;
455
456 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
457 if (sa6->sin6_addr.s6_addr16[1] == 0) {
433 /* interface ID is not embedded by the user */
458 /* link ID is not embedded by the user */
434 sa6->sin6_addr.s6_addr16[1] =
435 htons(ifp->if_index);
436 } else if (sa6->sin6_addr.s6_addr16[1] !=
437 htons(ifp->if_index)) {
459 sa6->sin6_addr.s6_addr16[1] =
460 htons(ifp->if_index);
461 } else if (sa6->sin6_addr.s6_addr16[1] !=
462 htons(ifp->if_index)) {
438 return(EINVAL); /* ifid is contradict */
463 return(EINVAL); /* link ID contradicts */
439 }
440 if (sa6->sin6_scope_id) {
441 if (sa6->sin6_scope_id !=
442 (u_int32_t)ifp->if_index)
443 return(EINVAL);
444 sa6->sin6_scope_id = 0; /* XXX: good way? */
445 }
446 }
447 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
448 }
449
450 switch (cmd) {
464 }
465 if (sa6->sin6_scope_id) {
466 if (sa6->sin6_scope_id !=
467 (u_int32_t)ifp->if_index)
468 return(EINVAL);
469 sa6->sin6_scope_id = 0; /* XXX: good way? */
470 }
471 }
472 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
473 }
474
475 switch (cmd) {
476 case SIOCSIFADDR_IN6:
477 case SIOCSIFDSTADDR_IN6:
478 case SIOCSIFNETMASK_IN6:
479 /*
480 * Since IPv6 allows a node to assign multiple addresses
481 * on a single interface, SIOCSIFxxx ioctls are not suitable
482 * and should be unused.
483 */
484 /* we decided to obsolete this command (20000704) */
485 return(EINVAL);
451
452 case SIOCDIFADDR_IN6:
453 /*
486
487 case SIOCDIFADDR_IN6:
488 /*
454 * for IPv4, we look for existing in6_ifaddr here to allow
489 * for IPv4, we look for existing in_ifaddr here to allow
455 * "ifconfig if0 delete" to remove first IPv4 address on the
456 * interface. For IPv6, as the spec allow multiple interface
457 * address from the day one, we consider "remove the first one"
490 * "ifconfig if0 delete" to remove first IPv4 address on the
491 * interface. For IPv6, as the spec allow multiple interface
492 * address from the day one, we consider "remove the first one"
458 * semantics to be not preferrable.
493 * semantics to be not preferable.
459 */
460 if (ia == NULL)
461 return(EADDRNOTAVAIL);
462 /* FALLTHROUGH */
463 case SIOCAIFADDR_IN6:
494 */
495 if (ia == NULL)
496 return(EADDRNOTAVAIL);
497 /* FALLTHROUGH */
498 case SIOCAIFADDR_IN6:
464 case SIOCSIFADDR_IN6:
465#ifdef COMPAT_IN6IFIOCTL
466 case SIOCSIFDSTADDR_IN6:
467 case SIOCSIFNETMASK_IN6:
468 /*
499 /*
469 * Since IPv6 allows a node to assign multiple addresses
470 * on a single interface, SIOCSIFxxx ioctls are not suitable
471 * and should be unused.
500 * We always require users to specify a valid IPv6 address for
501 * the corresponding operation.
472 */
502 */
473#endif
474 if (ifra->ifra_addr.sin6_family != AF_INET6)
503 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
504 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
475 return(EAFNOSUPPORT);
476 if (!privileged)
477 return(EPERM);
505 return(EAFNOSUPPORT);
506 if (!privileged)
507 return(EPERM);
478 if (ia == NULL) {
479 ia = (struct in6_ifaddr *)
480 malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
481 if (ia == NULL)
482 return (ENOBUFS);
483 bzero((caddr_t)ia, sizeof(*ia));
484 /* Initialize the address and masks */
485 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
486 ia->ia_addr.sin6_family = AF_INET6;
487 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
488#if 1
489 if (ifp->if_flags & IFF_POINTOPOINT) {
490 ia->ia_ifa.ifa_dstaddr
491 = (struct sockaddr *)&ia->ia_dstaddr;
492 ia->ia_dstaddr.sin6_family = AF_INET6;
493 ia->ia_dstaddr.sin6_len = sizeof(ia->ia_dstaddr);
494 } else {
495 ia->ia_ifa.ifa_dstaddr = NULL;
496 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
497 }
498#else /* always initilize by NULL */
499 ia->ia_ifa.ifa_dstaddr = NULL;
500 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
501#endif
502 ia->ia_ifa.ifa_netmask
503 = (struct sockaddr *)&ia->ia_prefixmask;
504
508
505 ia->ia_ifp = ifp;
506 if ((oia = in6_ifaddr) != NULL) {
507 for ( ; oia->ia_next; oia = oia->ia_next)
508 continue;
509 oia->ia_next = ia;
510 } else
511 in6_ifaddr = ia;
512 /* gain a refcnt for the link from in6_ifaddr */
513 ia->ia_ifa.ifa_refcnt++;
514
515 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
516 ifa_list);
517 /* gain another refcnt for the link from if_addrlist */
518 ia->ia_ifa.ifa_refcnt++;
519
520 newifaddr = 1;
521 } else
522 newifaddr = 0;
523
524 if (cmd == SIOCAIFADDR_IN6) {
525 /* sanity for overflow - beware unsigned */
526 struct in6_addrlifetime *lt;
527 lt = &ifra->ifra_lifetime;
528 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
529 && lt->ia6t_vltime + time_second < time_second) {
530 return EINVAL;
531 }
532 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
533 && lt->ia6t_pltime + time_second < time_second) {
534 return EINVAL;
535 }
536 }
537 break;
538
539 case SIOCGIFADDR_IN6:
540 /* This interface is basically deprecated. use SIOCGIFCONF. */
541 /* fall through */
542 case SIOCGIFAFLAG_IN6:
543 case SIOCGIFNETMASK_IN6:
544 case SIOCGIFDSTADDR_IN6:

--- 68 unchanged lines hidden (view full) ---

613 /* return EAFNOSUPPORT? */
614 bzero(&ifr->ifr_ifru.ifru_stat,
615 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
616 } else
617 ifr->ifr_ifru.ifru_icmp6stat =
618 *icmp6_ifstat[ifp->if_index];
619 break;
620
509 break;
510
511 case SIOCGIFADDR_IN6:
512 /* This interface is basically deprecated. use SIOCGIFCONF. */
513 /* fall through */
514 case SIOCGIFAFLAG_IN6:
515 case SIOCGIFNETMASK_IN6:
516 case SIOCGIFDSTADDR_IN6:

--- 68 unchanged lines hidden (view full) ---

585 /* return EAFNOSUPPORT? */
586 bzero(&ifr->ifr_ifru.ifru_stat,
587 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
588 } else
589 ifr->ifr_ifru.ifru_icmp6stat =
590 *icmp6_ifstat[ifp->if_index];
591 break;
592
621#ifdef COMPAT_IN6IFIOCTL /* should be unused */
622 case SIOCSIFDSTADDR_IN6:
623 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
624 return(EINVAL);
625 oldaddr = ia->ia_dstaddr;
626 ia->ia_dstaddr = ifr->ifr_dstaddr;
627
628 /* link-local index check */
629 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
630 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
631 /* interface ID is not embedded by the user */
632 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
633 = htons(ifp->if_index);
634 } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
635 htons(ifp->if_index)) {
636 ia->ia_dstaddr = oldaddr;
637 return(EINVAL); /* ifid is contradict */
638 }
639 }
640
641 if (ifp->if_ioctl && (error = (ifp->if_ioctl)
642 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
643 ia->ia_dstaddr = oldaddr;
644 return(error);
645 }
646 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
647 if (ia->ia_flags & IFA_ROUTE) {
648 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
649 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
650 ia->ia_ifa.ifa_dstaddr =
651 (struct sockaddr *)&ia->ia_dstaddr;
652 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
653 }
654 break;
655
656#endif
657 case SIOCGIFALIFETIME_IN6:
658 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
659 break;
660
661 case SIOCSIFALIFETIME_IN6:
662 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
663 /* for sanity */
664 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
665 ia->ia6_lifetime.ia6t_expire =
666 time_second + ia->ia6_lifetime.ia6t_vltime;
667 } else
668 ia->ia6_lifetime.ia6t_expire = 0;
669 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
670 ia->ia6_lifetime.ia6t_preferred =
671 time_second + ia->ia6_lifetime.ia6t_pltime;
672 } else
673 ia->ia6_lifetime.ia6t_preferred = 0;
674 break;
675
593 case SIOCGIFALIFETIME_IN6:
594 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
595 break;
596
597 case SIOCSIFALIFETIME_IN6:
598 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
599 /* for sanity */
600 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
601 ia->ia6_lifetime.ia6t_expire =
602 time_second + ia->ia6_lifetime.ia6t_vltime;
603 } else
604 ia->ia6_lifetime.ia6t_expire = 0;
605 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
606 ia->ia6_lifetime.ia6t_preferred =
607 time_second + ia->ia6_lifetime.ia6t_pltime;
608 } else
609 ia->ia6_lifetime.ia6t_preferred = 0;
610 break;
611
676 case SIOCSIFADDR_IN6:
677 error = in6_ifinit(ifp, ia, &ifr->ifr_addr, 1);
678#if 0
612 case SIOCAIFADDR_IN6:
613 {
614 int i, error = 0;
615 struct nd_prefix pr0, *pr;
616
679 /*
617 /*
680 * the code chokes if we are to assign multiple addresses with
681 * the same address prefix (rtinit() will return EEXIST, which
682 * is not fatal actually). we will get memory leak if we
683 * don't do it.
684 * -> we may want to hide EEXIST from rtinit().
618 * first, make or update the interface address structure,
619 * and link it to the list.
685 */
620 */
686 undo:
687 if (error && newifaddr) {
688 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
689 /* release a refcnt for the link from if_addrlist */
690 IFAFREE(&ia->ia_ifa);
621 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
622 return(error);
691
623
692 oia = ia;
693 if (oia == (ia = in6_ifaddr))
694 in6_ifaddr = ia->ia_next;
695 else {
696 while (ia->ia_next && (ia->ia_next != oia))
697 ia = ia->ia_next;
698 if (ia->ia_next)
699 ia->ia_next = oia->ia_next;
700 else {
701 printf("Didn't unlink in6_ifaddr "
702 "from list\n");
624 /*
625 * then, make the prefix on-link on the interface.
626 * XXX: we'd rather create the prefix before the address, but
627 * we need at least one address to install the corresponding
628 * interface route, so we configure the address first.
629 */
630
631 /*
632 * convert mask to prefix length (prefixmask has already
633 * been validated in in6_update_ifa().
634 */
635 bzero(&pr0, sizeof(pr0));
636 pr0.ndpr_ifp = ifp;
637 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
638 NULL);
639 if (pr0.ndpr_plen == 128)
640 break; /* we don't need to install a host route. */
641 pr0.ndpr_prefix = ifra->ifra_addr;
642 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
643 /* apply the mask for safety. */
644 for (i = 0; i < 4; i++) {
645 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
646 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
647 }
648 /*
649 * XXX: since we don't have enough APIs, we just set inifinity
650 * to lifetimes. They can be overridden by later advertised
651 * RAs (when accept_rtadv is non 0), but we'd rather intend
652 * such a behavior.
653 */
654 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
655 pr0.ndpr_raf_auto =
656 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
657 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
658 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
659
660 /* add the prefix if there's one. */
661 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
662 /*
663 * nd6_prelist_add will install the corresponding
664 * interface route.
665 */
666 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
667 return(error);
668 if (pr == NULL) {
669 log(LOG_ERR, "nd6_prelist_add succedded but "
670 "no prefix\n");
671 return(EINVAL); /* XXX panic here? */
672 }
673 }
674 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
675 == NULL) {
676 /* XXX: this should not happen! */
677 log(LOG_ERR, "in6_control: addition succeeded, but"
678 " no ifaddr\n");
679 } else {
680 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
681 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
682 ia->ia6_ndpr = pr;
683 pr->ndpr_refcnt++;
684
685 /*
686 * If this is the first autoconf address from
687 * the prefix, create a temporary address
688 * as well (when specified).
689 */
690 if (ip6_use_tempaddr &&
691 pr->ndpr_refcnt == 1) {
692 int e;
693 if ((e = in6_tmpifadd(ia, 1)) != 0) {
694 log(LOG_NOTICE, "in6_control: "
695 "failed to create a "
696 "temporary address, "
697 "errno=%d\n",
698 e);
699 }
703 }
704 }
700 }
701 }
705 /* release another refcnt for the link from in6_ifaddr */
706 IFAFREE(&oia->ia_ifa);
702
703 /*
704 * this might affect the status of autoconfigured
705 * addresses, that is, this address might make
706 * other addresses detached.
707 */
708 pfxlist_onlink_check();
707 }
709 }
708#endif
709 return error;
710 break;
711 }
710
712
711#ifdef COMPAT_IN6IFIOCTL /* XXX should be unused */
712 case SIOCSIFNETMASK_IN6:
713 ia->ia_prefixmask = ifr->ifr_addr;
714 bzero(&net, sizeof(net));
715 net.sin6_len = sizeof(struct sockaddr_in6);
716 net.sin6_family = AF_INET6;
717 net.sin6_port = htons(0);
718 net.sin6_flowinfo = htonl(0);
719 net.sin6_addr.s6_addr32[0]
720 = ia->ia_addr.sin6_addr.s6_addr32[0] &
721 ia->ia_prefixmask.sin6_addr.s6_addr32[0];
722 net.sin6_addr.s6_addr32[1]
723 = ia->ia_addr.sin6_addr.s6_addr32[1] &
724 ia->ia_prefixmask.sin6_addr.s6_addr32[1];
725 net.sin6_addr.s6_addr32[2]
726 = ia->ia_addr.sin6_addr.s6_addr32[2] &
727 ia->ia_prefixmask.sin6_addr.s6_addr32[2];
728 net.sin6_addr.s6_addr32[3]
729 = ia->ia_addr.sin6_addr.s6_addr32[3] &
730 ia->ia_prefixmask.sin6_addr.s6_addr32[3];
731 ia->ia_net = net;
713 case SIOCDIFADDR_IN6:
714 {
715 int i = 0;
716 struct nd_prefix pr0, *pr;
717
718 /*
719 * If the address being deleted is the only one that owns
720 * the corresponding prefix, expire the prefix as well.
721 * XXX: theoretically, we don't have to warry about such
722 * relationship, since we separate the address management
723 * and the prefix management. We do this, however, to provide
724 * as much backward compatibility as possible in terms of
725 * the ioctl operation.
726 */
727 bzero(&pr0, sizeof(pr0));
728 pr0.ndpr_ifp = ifp;
729 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
730 NULL);
731 if (pr0.ndpr_plen == 128)
732 goto purgeaddr;
733 pr0.ndpr_prefix = ia->ia_addr;
734 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
735 for (i = 0; i < 4; i++) {
736 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
737 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
738 }
739 /*
740 * The logic of the following condition is a bit complicated.
741 * We expire the prefix when
742 * 1. the address obeys autoconfiguration and it is the
743 * only owner of the associated prefix, or
744 * 2. the address does not obey autoconf and there is no
745 * other owner of the prefix.
746 */
747 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
748 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
749 pr->ndpr_refcnt == 1) ||
750 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
751 pr->ndpr_refcnt == 0))) {
752 pr->ndpr_expire = 1; /* XXX: just for expiration */
753 }
754
755 purgeaddr:
756 in6_purgeaddr(&ia->ia_ifa);
732 break;
757 break;
733#endif
758 }
734
759
735 case SIOCAIFADDR_IN6:
736 prefixIsNew = 0;
737 hostIsNew = 1;
760 default:
761 if (ifp == NULL || ifp->if_ioctl == 0)
762 return(EOPNOTSUPP);
763 return((*ifp->if_ioctl)(ifp, cmd, data));
764 }
738
765
739 if (ifra->ifra_addr.sin6_len == 0) {
740 ifra->ifra_addr = ia->ia_addr;
741 hostIsNew = 0;
742 } else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
743 &ia->ia_addr.sin6_addr))
744 hostIsNew = 0;
766 return(0);
767}
745
768
746 /* Validate address families: */
769/*
770 * Update parameters of an IPv6 interface address.
771 * If necessary, a new entry is created and linked into address chains.
772 * This function is separated from in6_control().
773 * XXX: should this be performed under splnet()?
774 */
775int
776in6_update_ifa(ifp, ifra, ia)
777 struct ifnet *ifp;
778 struct in6_aliasreq *ifra;
779 struct in6_ifaddr *ia;
780{
781 int error = 0, hostIsNew = 0, plen = -1;
782 struct in6_ifaddr *oia;
783 struct sockaddr_in6 dst6;
784 struct in6_addrlifetime *lt;
785
786 /* Validate parameters */
787 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
788 return(EINVAL);
789
790 /*
791 * The destination address for a p2p link must have a family
792 * of AF_UNSPEC or AF_INET6.
793 */
794 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
795 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
796 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
797 return(EAFNOSUPPORT);
798 /*
799 * validate ifra_prefixmask. don't check sin6_family, netmask
800 * does not carry fields other than sin6_len.
801 */
802 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
803 return(EINVAL);
804 /*
805 * Because the IPv6 address architecture is classless, we require
806 * users to specify a (non 0) prefix length (mask) for a new address.
807 * We also require the prefix (when specified) mask is valid, and thus
808 * reject a non-consecutive mask.
809 */
810 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
811 return(EINVAL);
812 if (ifra->ifra_prefixmask.sin6_len != 0) {
813 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
814 (u_char *)&ifra->ifra_prefixmask +
815 ifra->ifra_prefixmask.sin6_len);
816 if (plen <= 0)
817 return(EINVAL);
818 }
819 else {
747 /*
820 /*
748 * The destination address for a p2p link must have a family
749 * of AF_UNSPEC or AF_INET6.
821 * In this case, ia must not be NULL. We just use its prefix
822 * length.
750 */
823 */
751 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
752 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
753 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
754 return(EAFNOSUPPORT);
824 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
825 }
826 /*
827 * If the destination address on a p2p interface is specified,
828 * and the address is a scoped one, validate/set the scope
829 * zone identifier.
830 */
831 dst6 = ifra->ifra_dstaddr;
832 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
833 (dst6.sin6_family == AF_INET6)) {
834 int scopeid;
835
836#ifndef SCOPEDROUTING
837 if ((error = in6_recoverscope(&dst6,
838 &ifra->ifra_dstaddr.sin6_addr,
839 ifp)) != 0)
840 return(error);
841#endif
842 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
843 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
844 dst6.sin6_scope_id = scopeid;
845 else if (dst6.sin6_scope_id != scopeid)
846 return(EINVAL); /* scope ID mismatch. */
847#ifndef SCOPEDROUTING
848 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
849 != 0)
850 return(error);
851 dst6.sin6_scope_id = 0; /* XXX */
852#endif
853 }
854 /*
855 * The destination address can be specified only for a p2p or a
856 * loopback interface. If specified, the corresponding prefix length
857 * must be 128.
858 */
859 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
860 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
861 /* XXX: noisy message */
862 log(LOG_INFO, "in6_update_ifa: a destination can be "
863 "specified for a p2p or a loopback IF only\n");
864 return(EINVAL);
865 }
866 if (plen != 128) {
867 /*
868 * The following message seems noisy, but we dare to
869 * add it for diagnosis.
870 */
871 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
872 "when dstaddr is specified\n");
873 return(EINVAL);
874 }
875 }
876 /* lifetime consistency check */
877 lt = &ifra->ifra_lifetime;
878 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
879 && lt->ia6t_vltime + time_second < time_second) {
880 return EINVAL;
881 }
882 if (lt->ia6t_vltime == 0) {
755 /*
883 /*
756 * The prefixmask must have a family of AF_UNSPEC or AF_INET6.
884 * the following log might be noisy, but this is a typical
885 * configuration mistake or a tool's bug.
757 */
886 */
758 if (ifra->ifra_prefixmask.sin6_family != AF_INET6 &&
759 ifra->ifra_prefixmask.sin6_family != AF_UNSPEC)
760 return(EAFNOSUPPORT);
887 log(LOG_INFO,
888 "in6_update_ifa: valid lifetime is 0 for %s\n",
889 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
890 }
891 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
892 && lt->ia6t_pltime + time_second < time_second) {
893 return EINVAL;
894 }
761
895
762 if (ifra->ifra_prefixmask.sin6_len) {
763 in6_ifscrub(ifp, ia);
764 ia->ia_prefixmask = ifra->ifra_prefixmask;
765 prefixIsNew = 1;
896 /*
897 * If this is a new address, allocate a new ifaddr and link it
898 * into chains.
899 */
900 if (ia == NULL) {
901 hostIsNew = 1;
902 ia = (struct in6_ifaddr *)
903 malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
904 if (ia == NULL)
905 return (ENOBUFS);
906 bzero((caddr_t)ia, sizeof(*ia));
907 /* Initialize the address and masks */
908 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
909 ia->ia_addr.sin6_family = AF_INET6;
910 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
911 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
912 /*
913 * XXX: some functions expect that ifa_dstaddr is not
914 * NULL for p2p interfaces.
915 */
916 ia->ia_ifa.ifa_dstaddr
917 = (struct sockaddr *)&ia->ia_dstaddr;
918 } else {
919 ia->ia_ifa.ifa_dstaddr = NULL;
766 }
920 }
767 if ((ifp->if_flags & IFF_POINTOPOINT) &&
768 (ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
769 in6_ifscrub(ifp, ia);
770 oldaddr = ia->ia_dstaddr;
771 ia->ia_dstaddr = ifra->ifra_dstaddr;
772 /* link-local index check: should be a separate function? */
773 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
774 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
775 /*
776 * interface ID is not embedded by
777 * the user
778 */
779 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
780 = htons(ifp->if_index);
781 } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
782 htons(ifp->if_index)) {
783 ia->ia_dstaddr = oldaddr;
784 return(EINVAL); /* ifid is contradict */
785 }
786 }
787 prefixIsNew = 1; /* We lie; but effect's the same */
921 ia->ia_ifa.ifa_netmask
922 = (struct sockaddr *)&ia->ia_prefixmask;
923
924 ia->ia_ifp = ifp;
925 if ((oia = in6_ifaddr) != NULL) {
926 for ( ; oia->ia_next; oia = oia->ia_next)
927 continue;
928 oia->ia_next = ia;
929 } else
930 in6_ifaddr = ia;
931
932 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
933 ifa_list);
934 }
935
936 /* set prefix mask */
937 if (ifra->ifra_prefixmask.sin6_len) {
938 /*
939 * We prohibit changing the prefix length of an existing
940 * address, because
941 * + such an operation should be rare in IPv6, and
942 * + the operation would confuse prefix management.
943 */
944 if (ia->ia_prefixmask.sin6_len &&
945 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
946 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
947 " existing (%s) address should not be changed\n",
948 ip6_sprintf(&ia->ia_addr.sin6_addr));
949 error = EINVAL;
950 goto unlink;
788 }
951 }
789 if (hostIsNew || prefixIsNew) {
790 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0);
791#if 0
792 if (error)
793 goto undo;
794#endif
952 ia->ia_prefixmask = ifra->ifra_prefixmask;
953 }
954
955 /*
956 * If a new destination address is specified, scrub the old one and
957 * install the new destination. Note that the interface must be
958 * p2p or loopback (see the check above.)
959 */
960 if (dst6.sin6_family == AF_INET6 &&
961 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
962 &ia->ia_dstaddr.sin6_addr)) {
963 int e;
964
965 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
966 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
967 != 0) {
968 log(LOG_ERR, "in6_update_ifa: failed to remove "
969 "a route to the old destination: %s\n",
970 ip6_sprintf(&ia->ia_addr.sin6_addr));
971 /* proceed anyway... */
795 }
972 }
796 if (hostIsNew && (ifp->if_flags & IFF_MULTICAST)) {
797 int error_local = 0;
973 else
974 ia->ia_flags &= ~IFA_ROUTE;
975 ia->ia_dstaddr = dst6;
976 }
798
977
978 /* reset the interface and routing table appropriately. */
979 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
980 goto unlink;
981
982 /*
983 * Beyond this point, we should call in6_purgeaddr upon an error,
984 * not just go to unlink.
985 */
986
987#if 0 /* disable this mechanism for now */
988 /* update prefix list */
989 if (hostIsNew &&
990 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
991 int iilen;
992
993 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
994 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
995 in6_purgeaddr((struct ifaddr *)ia);
996 return(error);
997 }
998 }
999#endif
1000
1001 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1002 struct sockaddr_in6 mltaddr, mltmask;
1003 struct in6_multi *in6m;
1004
1005 if (hostIsNew) {
799 /*
800 * join solicited multicast addr for new host id
801 */
802 struct in6_addr llsol;
803 bzero(&llsol, sizeof(struct in6_addr));
804 llsol.s6_addr16[0] = htons(0xff02);
805 llsol.s6_addr16[1] = htons(ifp->if_index);
806 llsol.s6_addr32[1] = 0;
807 llsol.s6_addr32[2] = htonl(1);
808 llsol.s6_addr32[3] =
809 ifra->ifra_addr.sin6_addr.s6_addr32[3];
810 llsol.s6_addr8[12] = 0xff;
1006 /*
1007 * join solicited multicast addr for new host id
1008 */
1009 struct in6_addr llsol;
1010 bzero(&llsol, sizeof(struct in6_addr));
1011 llsol.s6_addr16[0] = htons(0xff02);
1012 llsol.s6_addr16[1] = htons(ifp->if_index);
1013 llsol.s6_addr32[1] = 0;
1014 llsol.s6_addr32[2] = htonl(1);
1015 llsol.s6_addr32[3] =
1016 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1017 llsol.s6_addr8[12] = 0xff;
811 (void)in6_addmulti(&llsol, ifp, &error_local);
812 if (error == 0)
813 error = error_local;
1018 (void)in6_addmulti(&llsol, ifp, &error);
1019 if (error != 0) {
1020 log(LOG_WARNING,
1021 "in6_update_ifa: addmulti failed for "
1022 "%s on %s (errno=%d)\n",
1023 ip6_sprintf(&llsol), if_name(ifp),
1024 error);
1025 in6_purgeaddr((struct ifaddr *)ia);
1026 return(error);
1027 }
814 }
815
1028 }
1029
816 ia->ia6_flags = ifra->ifra_flags;
817 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1030 bzero(&mltmask, sizeof(mltmask));
1031 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1032 mltmask.sin6_family = AF_INET6;
1033 mltmask.sin6_addr = in6mask32;
818
1034
819 ia->ia6_lifetime = ifra->ifra_lifetime;
820 /* for sanity */
821 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
822 ia->ia6_lifetime.ia6t_expire =
823 time_second + ia->ia6_lifetime.ia6t_vltime;
824 } else
825 ia->ia6_lifetime.ia6t_expire = 0;
826 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
827 ia->ia6_lifetime.ia6t_preferred =
828 time_second + ia->ia6_lifetime.ia6t_pltime;
829 } else
830 ia->ia6_lifetime.ia6t_preferred = 0;
831
832 /*
1035 /*
833 * make sure to initialize ND6 information. this is to
834 * workaround issues with interfaces with IPv6 addresses,
835 * which have never brought # up. we are assuming that it is
836 * safe to nd6_ifattach multiple times.
1036 * join link-local all-nodes address
837 */
1037 */
838 nd6_ifattach(ifp);
1038 bzero(&mltaddr, sizeof(mltaddr));
1039 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1040 mltaddr.sin6_family = AF_INET6;
1041 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1042 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
839
1043
1044 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1045 if (in6m == NULL) {
1046 rtrequest(RTM_ADD,
1047 (struct sockaddr *)&mltaddr,
1048 (struct sockaddr *)&ia->ia_addr,
1049 (struct sockaddr *)&mltmask,
1050 RTF_UP|RTF_CLONING, /* xxx */
1051 (struct rtentry **)0);
1052 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1053 if (error != 0) {
1054 log(LOG_WARNING,
1055 "in6_update_ifa: addmulti failed for "
1056 "%s on %s (errno=%d)\n",
1057 ip6_sprintf(&mltaddr.sin6_addr),
1058 if_name(ifp), error);
1059 }
1060 }
1061
840 /*
1062 /*
841 * Perform DAD, if needed.
842 * XXX It may be of use, if we can administratively
843 * disable DAD.
1063 * join node information group address
844 */
1064 */
845 switch (ifp->if_type) {
846 case IFT_ARCNET:
847 case IFT_ETHER:
848 case IFT_FDDI:
849#if 0
850 case IFT_ATM:
851 case IFT_SLIP:
852 case IFT_PPP:
853#endif
854 {
855 ia->ia6_flags |= IN6_IFF_TENTATIVE;
856 nd6_dad_start((struct ifaddr *)ia, NULL);
1065#define hostnamelen strlen(hostname)
1066 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1067 == 0) {
1068 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1069 if (in6m == NULL && ia != NULL) {
1070 (void)in6_addmulti(&mltaddr.sin6_addr,
1071 ifp, &error);
1072 if (error != 0) {
1073 log(LOG_WARNING, "in6_update_ifa: "
1074 "addmulti failed for "
1075 "%s on %s (errno=%d)\n",
1076 ip6_sprintf(&mltaddr.sin6_addr),
1077 if_name(ifp), error);
1078 }
857 }
1079 }
858 break;
859#ifdef IFT_DUMMY
860 case IFT_DUMMY:
861#endif
862 case IFT_FAITH:
863 case IFT_GIF:
864 case IFT_LOOP:
865 default:
866 break;
867 }
1080 }
1081#undef hostnamelen
868
1082
869 if (hostIsNew) {
870 int iilen;
871 int error_local = 0;
1083 /*
1084 * join node-local all-nodes address, on loopback.
1085 * XXX: since "node-local" is obsoleted by interface-local,
1086 * we have to join the group on every interface with
1087 * some interface-boundary restriction.
1088 */
1089 if (ifp->if_flags & IFF_LOOPBACK) {
1090 struct in6_addr loop6 = in6addr_loopback;
1091 ia = in6ifa_ifpwithaddr(ifp, &loop6);
872
1092
873 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) -
874 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
875 error_local = in6_prefix_add_ifid(iilen, ia);
876 if (error == 0)
877 error = error_local;
1093 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1094
1095 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1096 if (in6m == NULL && ia != NULL) {
1097 rtrequest(RTM_ADD,
1098 (struct sockaddr *)&mltaddr,
1099 (struct sockaddr *)&ia->ia_addr,
1100 (struct sockaddr *)&mltmask,
1101 RTF_UP,
1102 (struct rtentry **)0);
1103 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1104 &error);
1105 if (error != 0) {
1106 log(LOG_WARNING, "in6_update_ifa: "
1107 "addmulti failed for %s on %s "
1108 "(errno=%d)\n",
1109 ip6_sprintf(&mltaddr.sin6_addr),
1110 if_name(ifp), error);
1111 }
1112 }
878 }
1113 }
1114 }
879
1115
880 return(error);
1116 ia->ia6_flags = ifra->ifra_flags;
1117 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1118 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
881
1119
882 case SIOCDIFADDR_IN6:
883 in6_purgeaddr(&ia->ia_ifa, ifp);
884 break;
1120 ia->ia6_lifetime = ifra->ifra_lifetime;
1121 /* for sanity */
1122 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1123 ia->ia6_lifetime.ia6t_expire =
1124 time_second + ia->ia6_lifetime.ia6t_vltime;
1125 } else
1126 ia->ia6_lifetime.ia6t_expire = 0;
1127 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1128 ia->ia6_lifetime.ia6t_preferred =
1129 time_second + ia->ia6_lifetime.ia6t_pltime;
1130 } else
1131 ia->ia6_lifetime.ia6t_preferred = 0;
885
1132
886 default:
887 if (ifp == NULL || ifp->if_ioctl == 0)
888 return(EOPNOTSUPP);
889 return((*ifp->if_ioctl)(ifp, cmd, data));
1133 /*
1134 * make sure to initialize ND6 information. this is to workaround
1135 * issues with interfaces with IPv6 addresses, which have never brought
1136 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1137 */
1138 nd6_ifattach(ifp);
1139
1140 /*
1141 * Perform DAD, if needed.
1142 * XXX It may be of use, if we can administratively
1143 * disable DAD.
1144 */
1145 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1146 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1147 nd6_dad_start((struct ifaddr *)ia, NULL);
890 }
1148 }
891 return(0);
1149
1150 return(error);
1151
1152 unlink:
1153 /*
1154 * XXX: if a change of an existing address failed, keep the entry
1155 * anyway.
1156 */
1157 if (hostIsNew)
1158 in6_unlink_ifa(ia, ifp);
1159 return(error);
892}
893
894void
1160}
1161
1162void
895in6_purgeaddr(ifa, ifp)
1163in6_purgeaddr(ifa)
896 struct ifaddr *ifa;
1164 struct ifaddr *ifa;
897 struct ifnet *ifp;
898{
1165{
899 struct in6_ifaddr *oia, *ia = (void *) ifa;
900 int plen;
1166 struct ifnet *ifp = ifa->ifa_ifp;
1167 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
901
1168
902 in6_ifscrub(ifp, ia);
1169 /* stop DAD processing */
1170 nd6_dad_stop(ifa);
903
1171
1172 /*
1173 * delete route to the destination of the address being purged.
1174 * The interface must be p2p or loopback in this case.
1175 */
1176 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1177 int e;
1178
1179 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1180 != 0) {
1181 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1182 "a route to the p2p destination: %s on %s, "
1183 "errno=%d\n",
1184 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1185 e);
1186 /* proceed anyway... */
1187 }
1188 else
1189 ia->ia_flags &= ~IFA_ROUTE;
1190 }
1191
1192 /* Remove ownaddr's loopback rtentry, if it exists. */
1193 in6_ifremloop(&(ia->ia_ifa));
1194
904 if (ifp->if_flags & IFF_MULTICAST) {
905 /*
906 * delete solicited multicast addr for deleting host id
907 */
908 struct in6_multi *in6m;
909 struct in6_addr llsol;
910 bzero(&llsol, sizeof(struct in6_addr));
911 llsol.s6_addr16[0] = htons(0xff02);

--- 4 unchanged lines hidden (view full) ---

916 ia->ia_addr.sin6_addr.s6_addr32[3];
917 llsol.s6_addr8[12] = 0xff;
918
919 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
920 if (in6m)
921 in6_delmulti(in6m);
922 }
923
1195 if (ifp->if_flags & IFF_MULTICAST) {
1196 /*
1197 * delete solicited multicast addr for deleting host id
1198 */
1199 struct in6_multi *in6m;
1200 struct in6_addr llsol;
1201 bzero(&llsol, sizeof(struct in6_addr));
1202 llsol.s6_addr16[0] = htons(0xff02);

--- 4 unchanged lines hidden (view full) ---

1207 ia->ia_addr.sin6_addr.s6_addr32[3];
1208 llsol.s6_addr8[12] = 0xff;
1209
1210 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1211 if (in6m)
1212 in6_delmulti(in6m);
1213 }
1214
1215 in6_unlink_ifa(ia, ifp);
1216}
1217
1218static void
1219in6_unlink_ifa(ia, ifp)
1220 struct in6_ifaddr *ia;
1221 struct ifnet *ifp;
1222{
1223 int plen, iilen;
1224 struct in6_ifaddr *oia;
1225 int s = splnet();
1226
924 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1227 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
925 /* release a refcnt for the link from if_addrlist */
926 IFAFREE(&ia->ia_ifa);
927
928 oia = ia;
929 if (oia == (ia = in6_ifaddr))
930 in6_ifaddr = ia->ia_next;
931 else {
932 while (ia->ia_next && (ia->ia_next != oia))
933 ia = ia->ia_next;
934 if (ia->ia_next)
935 ia->ia_next = oia->ia_next;
1228
1229 oia = ia;
1230 if (oia == (ia = in6_ifaddr))
1231 in6_ifaddr = ia->ia_next;
1232 else {
1233 while (ia->ia_next && (ia->ia_next != oia))
1234 ia = ia->ia_next;
1235 if (ia->ia_next)
1236 ia->ia_next = oia->ia_next;
936 else
937 printf("Didn't unlink in6_ifaddr from list\n");
1237 else {
1238 /* search failed */
1239 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1240 }
938 }
1241 }
939 {
940 int iilen;
941
1242
942 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr);
1243 if (oia->ia6_ifpr) { /* check for safety */
1244 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
943 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
944 in6_prefix_remove_ifid(iilen, oia);
945 }
946
947 /*
1245 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1246 in6_prefix_remove_ifid(iilen, oia);
1247 }
1248
1249 /*
948 * Check if we have another address that has the same prefix of
949 * the purged address. If we have one, reinstall the corresponding
950 * interface route.
1250 * When an autoconfigured address is being removed, release the
1251 * reference to the base prefix. Also, since the release might
1252 * affect the status of other (detached) addresses, call
1253 * pfxlist_onlink_check().
951 */
1254 */
952 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
953 int e;
954
955 if (in6_are_prefix_equal(&ia->ia_addr.sin6_addr,
956 &oia->ia_addr.sin6_addr, plen)) {
957 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
958 ia->ia_flags)) == 0) {
959 ia->ia_flags |= IFA_ROUTE;
960 break;
961 }
962 else {
963 log(LOG_NOTICE,
964 "in6_purgeaddr: failed to add an interface"
965 " route for %s/%d on %s, errno = %d\n",
966 ip6_sprintf(&ia->ia_addr.sin6_addr),
967 plen, if_name(ia->ia_ifp), e);
968 /* still trying */
969 }
1255 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1256 if (oia->ia6_ndpr == NULL) {
1257 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1258 "%p has no prefix\n", oia);
1259 } else {
1260 oia->ia6_ndpr->ndpr_refcnt--;
1261 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1262 oia->ia6_ndpr = NULL;
970 }
1263 }
1264
1265 pfxlist_onlink_check();
971 }
1266 }
972
973 /* release another refcnt for the link from in6_ifaddr */
1267
1268 /*
1269 * release another refcnt for the link from in6_ifaddr.
1270 * Note that we should decrement the refcnt at least once for all *BSD.
1271 */
974 IFAFREE(&oia->ia_ifa);
1272 IFAFREE(&oia->ia_ifa);
1273
1274 splx(s);
975}
976
1275}
1276
1277void
1278in6_purgeif(ifp)
1279 struct ifnet *ifp;
1280{
1281 struct ifaddr *ifa, *nifa;
1282
1283 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1284 {
1285 nifa = TAILQ_NEXT(ifa, ifa_list);
1286 if (ifa->ifa_addr->sa_family != AF_INET6)
1287 continue;
1288 in6_purgeaddr(ifa);
1289 }
1290
1291 in6_ifdetach(ifp);
1292}
1293
977/*
978 * SIOC[GAD]LIFADDR.
979 * SIOCGLIFADDR: get first address. (?)
980 * SIOCGLIFADDR with IFLR_PREFIX:
981 * get first address that matches the specified prefix.
982 * SIOCALIFADDR: add the specified address.
983 * SIOCALIFADDR with IFLR_PREFIX:
984 * add the specified prefix, filling hostid part from

--- 117 unchanged lines hidden (view full) ---

1102 if (hostid) {
1103 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1104 hostid->s6_addr32[2];
1105 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1106 hostid->s6_addr32[3];
1107 }
1108 }
1109
1294/*
1295 * SIOC[GAD]LIFADDR.
1296 * SIOCGLIFADDR: get first address. (?)
1297 * SIOCGLIFADDR with IFLR_PREFIX:
1298 * get first address that matches the specified prefix.
1299 * SIOCALIFADDR: add the specified address.
1300 * SIOCALIFADDR with IFLR_PREFIX:
1301 * add the specified prefix, filling hostid part from

--- 117 unchanged lines hidden (view full) ---

1419 if (hostid) {
1420 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1421 hostid->s6_addr32[2];
1422 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1423 hostid->s6_addr32[3];
1424 }
1425 }
1426
1110 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1111 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1112 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1113
1114 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1115 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1116 }
1117 case SIOCGLIFADDR:
1118 case SIOCDLIFADDR:

--- 35 unchanged lines hidden (view full) ---

1154 }
1155
1156 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1157 {
1158 if (ifa->ifa_addr->sa_family != AF_INET6)
1159 continue;
1160 if (!cmp)
1161 break;
1427 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1428 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1429
1430 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1431 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1432 }
1433 case SIOCGLIFADDR:
1434 case SIOCDLIFADDR:

--- 35 unchanged lines hidden (view full) ---

1470 }
1471
1472 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1473 {
1474 if (ifa->ifa_addr->sa_family != AF_INET6)
1475 continue;
1476 if (!cmp)
1477 break;
1478
1162 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1479 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1480#ifndef SCOPEDROUTING
1481 /*
1482 * XXX: this is adhoc, but is necessary to allow
1483 * a user to specify fe80::/64 (not /10) for a
1484 * link-local address.
1485 */
1486 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1487 candidate.s6_addr16[1] = 0;
1488#endif
1163 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1164 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1165 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1166 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1167 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1168 break;
1169 }
1170 if (!ifa)
1171 return EADDRNOTAVAIL;
1172 ia = ifa2ia6(ifa);
1173
1174 if (cmd == SIOCGLIFADDR) {
1489 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1490 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1491 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1492 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1493 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1494 break;
1495 }
1496 if (!ifa)
1497 return EADDRNOTAVAIL;
1498 ia = ifa2ia6(ifa);
1499
1500 if (cmd == SIOCGLIFADDR) {
1501#ifndef SCOPEDROUTING
1502 struct sockaddr_in6 *s6;
1503#endif
1504
1175 /* fill in the if_laddrreq structure */
1176 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1505 /* fill in the if_laddrreq structure */
1506 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1177
1507#ifndef SCOPEDROUTING /* XXX see above */
1508 s6 = (struct sockaddr_in6 *)&iflr->addr;
1509 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1510 s6->sin6_addr.s6_addr16[1] = 0;
1511 s6->sin6_scope_id =
1512 in6_addr2scopeid(ifp, &s6->sin6_addr);
1513 }
1514#endif
1178 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1179 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1180 ia->ia_dstaddr.sin6_len);
1515 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1516 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1517 ia->ia_dstaddr.sin6_len);
1518#ifndef SCOPEDROUTING /* XXX see above */
1519 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1520 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1521 s6->sin6_addr.s6_addr16[1] = 0;
1522 s6->sin6_scope_id =
1523 in6_addr2scopeid(ifp,
1524 &s6->sin6_addr);
1525 }
1526#endif
1181 } else
1182 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1183
1184 iflr->prefixlen =
1527 } else
1528 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1529
1530 iflr->prefixlen =
1185 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
1531 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1532 NULL);
1186
1187 iflr->flags = ia->ia6_flags; /*XXX*/
1188
1189 return 0;
1190 } else {
1191 struct in6_aliasreq ifra;
1192
1193 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */

--- 19 unchanged lines hidden (view full) ---

1213 }
1214 }
1215 }
1216
1217 return EOPNOTSUPP; /*just for safety*/
1218}
1219
1220/*
1533
1534 iflr->flags = ia->ia6_flags; /*XXX*/
1535
1536 return 0;
1537 } else {
1538 struct in6_aliasreq ifra;
1539
1540 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */

--- 19 unchanged lines hidden (view full) ---

1560 }
1561 }
1562 }
1563
1564 return EOPNOTSUPP; /*just for safety*/
1565}
1566
1567/*
1221 * Delete any existing route for an interface.
1222 */
1223void
1224in6_ifscrub(ifp, ia)
1225 register struct ifnet *ifp;
1226 register struct in6_ifaddr *ia;
1227{
1228 if ((ia->ia_flags & IFA_ROUTE) == 0)
1229 return;
1230 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
1231 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1232 else
1233 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1234 ia->ia_flags &= ~IFA_ROUTE;
1235
1236 /* Remove ownaddr's loopback rtentry, if it exists. */
1237 in6_ifremloop(&(ia->ia_ifa));
1238}
1239
1240/*
1241 * Initialize an interface's intetnet6 address
1242 * and routing table entry.
1243 */
1568 * Initialize an interface's intetnet6 address
1569 * and routing table entry.
1570 */
1244int
1245in6_ifinit(ifp, ia, sin6, scrub)
1571static int
1572in6_ifinit(ifp, ia, sin6, newhost)
1246 struct ifnet *ifp;
1247 struct in6_ifaddr *ia;
1248 struct sockaddr_in6 *sin6;
1573 struct ifnet *ifp;
1574 struct in6_ifaddr *ia;
1575 struct sockaddr_in6 *sin6;
1249 int scrub;
1576 int newhost;
1250{
1577{
1251 struct sockaddr_in6 oldaddr;
1252 int error, flags = RTF_UP;
1578 int error = 0, plen, ifacount = 0;
1253 int s = splimp();
1579 int s = splimp();
1580 struct ifaddr *ifa;
1254
1581
1255 oldaddr = ia->ia_addr;
1256 ia->ia_addr = *sin6;
1257 /*
1258 * Give the interface a chance to initialize
1259 * if this is its first address,
1260 * and to validate the address if necessary.
1261 */
1582 /*
1583 * Give the interface a chance to initialize
1584 * if this is its first address,
1585 * and to validate the address if necessary.
1586 */
1262 if (ifp->if_ioctl &&
1263 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1587 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1588 {
1589 if (ifa->ifa_addr == NULL)
1590 continue; /* just for safety */
1591 if (ifa->ifa_addr->sa_family != AF_INET6)
1592 continue;
1593 ifacount++;
1594 }
1595
1596 ia->ia_addr = *sin6;
1597
1598 if (ifacount <= 1 && ifp->if_ioctl &&
1599 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1264 splx(s);
1600 splx(s);
1265 ia->ia_addr = oldaddr;
1266 return(error);
1267 }
1601 return(error);
1602 }
1603 splx(s);
1268
1604
1269 switch (ifp->if_type) {
1270 case IFT_ARCNET:
1271 case IFT_ETHER:
1272 case IFT_FDDI:
1273 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1274 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1275 break;
1276 case IFT_PPP:
1277 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
1278 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1279 break;
1280 }
1605 ia->ia_ifa.ifa_metric = ifp->if_metric;
1281
1606
1282 splx(s);
1283 if (scrub) {
1284 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1285 in6_ifscrub(ifp, ia);
1286 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1287 }
1288 /* xxx
1289 * in_socktrim
1290 */
1607 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1608
1291 /*
1609 /*
1292 * Add route for the network.
1610 * Special case:
1611 * If the destination address is specified for a point-to-point
1612 * interface, install a route to the destination as an interface
1613 * direct route.
1293 */
1614 */
1294 ia->ia_ifa.ifa_metric = ifp->if_metric;
1295 if (ifp->if_flags & IFF_LOOPBACK) {
1296 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1297 flags |= RTF_HOST;
1298 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1299 if (ia->ia_dstaddr.sin6_family != AF_INET6)
1300 return(0);
1301 flags |= RTF_HOST;
1302 }
1303 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
1615 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1616 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1617 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1618 RTF_UP | RTF_HOST)) != 0)
1619 return(error);
1304 ia->ia_flags |= IFA_ROUTE;
1620 ia->ia_flags |= IFA_ROUTE;
1305 /* XXX check if the subnet route points to the same interface */
1306 if (error == EEXIST)
1307 error = 0;
1621 }
1622 if (plen < 128) {
1623 /*
1624 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1625 */
1626 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1627 }
1308
1309 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1628
1629 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1310 in6_ifaddloop(&(ia->ia_ifa));
1630 if (newhost) {
1631 /* set the rtrequest function to create llinfo */
1632 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1633 in6_ifaddloop(&(ia->ia_ifa));
1634 }
1311
1312 return(error);
1313}
1314
1315/*
1316 * Add an address to the list of IP6 multicast addresses for a
1317 * given interface.
1318 */
1319struct in6_multi *
1320in6_addmulti(maddr6, ifp, errorp)
1635
1636 return(error);
1637}
1638
1639/*
1640 * Add an address to the list of IP6 multicast addresses for a
1641 * given interface.
1642 */
1643struct in6_multi *
1644in6_addmulti(maddr6, ifp, errorp)
1321 register struct in6_addr *maddr6;
1322 register struct ifnet *ifp;
1645 struct in6_addr *maddr6;
1646 struct ifnet *ifp;
1323 int *errorp;
1324{
1325 struct in6_multi *in6m;
1326 struct sockaddr_in6 sin6;
1327 struct ifmultiaddr *ifma;
1328 int s = splnet();
1329
1330 *errorp = 0;

--- 72 unchanged lines hidden (view full) ---

1403/*
1404 * Find an IPv6 interface link-local address specific to an interface.
1405 */
1406struct in6_ifaddr *
1407in6ifa_ifpforlinklocal(ifp, ignoreflags)
1408 struct ifnet *ifp;
1409 int ignoreflags;
1410{
1647 int *errorp;
1648{
1649 struct in6_multi *in6m;
1650 struct sockaddr_in6 sin6;
1651 struct ifmultiaddr *ifma;
1652 int s = splnet();
1653
1654 *errorp = 0;

--- 72 unchanged lines hidden (view full) ---

1727/*
1728 * Find an IPv6 interface link-local address specific to an interface.
1729 */
1730struct in6_ifaddr *
1731in6ifa_ifpforlinklocal(ifp, ignoreflags)
1732 struct ifnet *ifp;
1733 int ignoreflags;
1734{
1411 register struct ifaddr *ifa;
1735 struct ifaddr *ifa;
1412
1413 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1414 {
1415 if (ifa->ifa_addr == NULL)
1416 continue; /* just for safety */
1417 if (ifa->ifa_addr->sa_family != AF_INET6)
1418 continue;
1419 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {

--- 11 unchanged lines hidden (view full) ---

1431/*
1432 * find the internet address corresponding to a given interface and address.
1433 */
1434struct in6_ifaddr *
1435in6ifa_ifpwithaddr(ifp, addr)
1436 struct ifnet *ifp;
1437 struct in6_addr *addr;
1438{
1736
1737 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1738 {
1739 if (ifa->ifa_addr == NULL)
1740 continue; /* just for safety */
1741 if (ifa->ifa_addr->sa_family != AF_INET6)
1742 continue;
1743 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {

--- 11 unchanged lines hidden (view full) ---

1755/*
1756 * find the internet address corresponding to a given interface and address.
1757 */
1758struct in6_ifaddr *
1759in6ifa_ifpwithaddr(ifp, addr)
1760 struct ifnet *ifp;
1761 struct in6_addr *addr;
1762{
1439 register struct ifaddr *ifa;
1763 struct ifaddr *ifa;
1440
1441 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1442 {
1443 if (ifa->ifa_addr == NULL)
1444 continue; /* just for safety */
1445 if (ifa->ifa_addr->sa_family != AF_INET6)
1446 continue;
1447 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))

--- 5 unchanged lines hidden (view full) ---

1453
1454/*
1455 * Convert IP6 address to printable (loggable) representation.
1456 */
1457static char digits[] = "0123456789abcdef";
1458static int ip6round = 0;
1459char *
1460ip6_sprintf(addr)
1764
1765 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1766 {
1767 if (ifa->ifa_addr == NULL)
1768 continue; /* just for safety */
1769 if (ifa->ifa_addr->sa_family != AF_INET6)
1770 continue;
1771 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))

--- 5 unchanged lines hidden (view full) ---

1777
1778/*
1779 * Convert IP6 address to printable (loggable) representation.
1780 */
1781static char digits[] = "0123456789abcdef";
1782static int ip6round = 0;
1783char *
1784ip6_sprintf(addr)
1461register struct in6_addr *addr;
1785 const struct in6_addr *addr;
1462{
1463 static char ip6buf[8][48];
1786{
1787 static char ip6buf[8][48];
1464 register int i;
1465 register char *cp;
1466 register u_short *a = (u_short *)addr;
1467 register u_char *d;
1788 int i;
1789 char *cp;
1790 u_short *a = (u_short *)addr;
1791 u_char *d;
1468 int dcolon = 0;
1469
1470 ip6round = (ip6round + 1) & 7;
1471 cp = ip6buf[ip6round];
1472
1473 for (i = 0; i < 8; i++) {
1474 if (dcolon == 1) {
1475 if (*a == 0) {

--- 41 unchanged lines hidden (view full) ---

1517 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1518 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1519 &ia->ia_prefixmask.sin6_addr))
1520 return 1;
1521
1522 return (0);
1523}
1524
1792 int dcolon = 0;
1793
1794 ip6round = (ip6round + 1) & 7;
1795 cp = ip6buf[ip6round];
1796
1797 for (i = 0; i < 8; i++) {
1798 if (dcolon == 1) {
1799 if (*a == 0) {

--- 41 unchanged lines hidden (view full) ---

1841 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1842 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1843 &ia->ia_prefixmask.sin6_addr))
1844 return 1;
1845
1846 return (0);
1847}
1848
1849int
1850in6_is_addr_deprecated(sa6)
1851 struct sockaddr_in6 *sa6;
1852{
1853 struct in6_ifaddr *ia;
1854
1855 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1856 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1857 &sa6->sin6_addr) &&
1858#ifdef SCOPEDROUTING
1859 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1860#endif
1861 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1862 return(1); /* true */
1863
1864 /* XXX: do we still have to go thru the rest of the list? */
1865 }
1866
1867 return(0); /* false */
1868}
1869
1525/*
1526 * return length of part which dst and src are equal
1527 * hard coding...
1528 */
1529int
1530in6_matchlen(src, dst)
1531struct in6_addr *src, *dst;
1532{

--- 64 unchanged lines hidden (view full) ---

1597 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1598}
1599
1600/*
1601 * return the best address out of the same scope
1602 */
1603struct in6_ifaddr *
1604in6_ifawithscope(oifp, dst)
1870/*
1871 * return length of part which dst and src are equal
1872 * hard coding...
1873 */
1874int
1875in6_matchlen(src, dst)
1876struct in6_addr *src, *dst;
1877{

--- 64 unchanged lines hidden (view full) ---

1942 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1943}
1944
1945/*
1946 * return the best address out of the same scope
1947 */
1948struct in6_ifaddr *
1949in6_ifawithscope(oifp, dst)
1605 register struct ifnet *oifp;
1606 register struct in6_addr *dst;
1950 struct ifnet *oifp;
1951 struct in6_addr *dst;
1607{
1608 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1609 int blen = -1;
1610 struct ifaddr *ifa;
1611 struct ifnet *ifp;
1612 struct in6_ifaddr *ifa_best = NULL;
1613
1614 if (oifp == NULL) {
1952{
1953 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1954 int blen = -1;
1955 struct ifaddr *ifa;
1956 struct ifnet *ifp;
1957 struct in6_ifaddr *ifa_best = NULL;
1958
1959 if (oifp == NULL) {
1960#if 0
1615 printf("in6_ifawithscope: output interface is not specified\n");
1961 printf("in6_ifawithscope: output interface is not specified\n");
1962#endif
1616 return(NULL);
1617 }
1618
1619 /*
1620 * We search for all addresses on all interfaces from the beginning.
1621 * Comparing an interface with the outgoing interface will be done
1622 * only at the final stage of tiebreaking.
1623 */

--- 46 unchanged lines hidden (view full) ---

1670
1671 /*
1672 * If ifa_best has a smaller scope than dst and
1673 * the current address has a larger one than
1674 * (or equal to) dst, always replace ifa_best.
1675 * Also, if the current address has a smaller scope
1676 * than dst, ignore it unless ifa_best also has a
1677 * smaller scope.
1963 return(NULL);
1964 }
1965
1966 /*
1967 * We search for all addresses on all interfaces from the beginning.
1968 * Comparing an interface with the outgoing interface will be done
1969 * only at the final stage of tiebreaking.
1970 */

--- 46 unchanged lines hidden (view full) ---

2017
2018 /*
2019 * If ifa_best has a smaller scope than dst and
2020 * the current address has a larger one than
2021 * (or equal to) dst, always replace ifa_best.
2022 * Also, if the current address has a smaller scope
2023 * than dst, ignore it unless ifa_best also has a
2024 * smaller scope.
2025 * Consequently, after the two if-clause below,
2026 * the followings must be satisfied:
2027 * (scope(src) < scope(dst) &&
2028 * scope(best) < scope(dst))
2029 * OR
2030 * (scope(best) >= scope(dst) &&
2031 * scope(src) >= scope(dst))
1678 */
1679 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
1680 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2032 */
2033 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2034 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
1681 goto replace;
2035 goto replace; /* (A) */
1682 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
1683 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2036 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2037 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
1684 continue;
2038 continue; /* (B) */
1685
1686 /*
1687 * A deprecated address SHOULD NOT be used in new
1688 * communications if an alternate (non-deprecated)
1689 * address is available and has sufficient scope.
1690 * RFC 2462, Section 5.5.4.
1691 */
1692 if (((struct in6_ifaddr *)ifa)->ia6_flags &

--- 12 unchanged lines hidden (view full) ---

1705 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
1706 == 0)
1707 continue;
1708 }
1709
1710 /*
1711 * A non-deprecated address is always preferred
1712 * to a deprecated one regardless of scopes and
2039
2040 /*
2041 * A deprecated address SHOULD NOT be used in new
2042 * communications if an alternate (non-deprecated)
2043 * address is available and has sufficient scope.
2044 * RFC 2462, Section 5.5.4.
2045 */
2046 if (((struct in6_ifaddr *)ifa)->ia6_flags &

--- 12 unchanged lines hidden (view full) ---

2059 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2060 == 0)
2061 continue;
2062 }
2063
2064 /*
2065 * A non-deprecated address is always preferred
2066 * to a deprecated one regardless of scopes and
1713 * address matching.
2067 * address matching (Note invariants ensured by the
2068 * conditions (A) and (B) above.)
1714 */
1715 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
1716 (((struct in6_ifaddr *)ifa)->ia6_flags &
1717 IN6_IFF_DEPRECATED) == 0)
1718 goto replace;
1719
1720 /*
2069 */
2070 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2071 (((struct in6_ifaddr *)ifa)->ia6_flags &
2072 IN6_IFF_DEPRECATED) == 0)
2073 goto replace;
2074
2075 /*
2076 * When we use temporary addresses described in
2077 * RFC 3041, we prefer temporary addresses to
2078 * public autoconf addresses. Again, note the
2079 * invariants from (A) and (B). Also note that we
2080 * don't have any preference between static addresses
2081 * and autoconf addresses (despite of whether or not
2082 * the latter is temporary or public.)
2083 */
2084 if (ip6_use_tempaddr) {
2085 struct in6_ifaddr *ifat;
2086
2087 ifat = (struct in6_ifaddr *)ifa;
2088 if ((ifa_best->ia6_flags &
2089 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2090 == IN6_IFF_AUTOCONF &&
2091 (ifat->ia6_flags &
2092 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2093 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2094 goto replace;
2095 }
2096 if ((ifa_best->ia6_flags &
2097 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2098 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2099 (ifat->ia6_flags &
2100 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2101 == IN6_IFF_AUTOCONF) {
2102 continue;
2103 }
2104 }
2105
2106 /*
1721 * At this point, we have two cases:
1722 * 1. we are looking at a non-deprecated address,
1723 * and ifa_best is also non-deprecated.
1724 * 2. we are looking at a deprecated address,
1725 * and ifa_best is also deprecated.
1726 * Also, we do not have to consider a case where
1727 * the scope of if_best is larger(smaller) than dst and
1728 * the scope of the current address is smaller(larger)

--- 9 unchanged lines hidden (view full) ---

1738 * - if the address is on the outgoing I/F (outI/F)
1739 *
1740 * Roughly speaking, the selection policy is
1741 * - the most important item is scope. The same scope
1742 * is best. Then search for a larger scope.
1743 * Smaller scopes are the last resort.
1744 * - A deprecated address is chosen only when we have
1745 * no address that has an enough scope, but is
2107 * At this point, we have two cases:
2108 * 1. we are looking at a non-deprecated address,
2109 * and ifa_best is also non-deprecated.
2110 * 2. we are looking at a deprecated address,
2111 * and ifa_best is also deprecated.
2112 * Also, we do not have to consider a case where
2113 * the scope of if_best is larger(smaller) than dst and
2114 * the scope of the current address is smaller(larger)

--- 9 unchanged lines hidden (view full) ---

2124 * - if the address is on the outgoing I/F (outI/F)
2125 *
2126 * Roughly speaking, the selection policy is
2127 * - the most important item is scope. The same scope
2128 * is best. Then search for a larger scope.
2129 * Smaller scopes are the last resort.
2130 * - A deprecated address is chosen only when we have
2131 * no address that has an enough scope, but is
1746 * prefered to any addresses of smaller scopes.
1747 * - Longest address match against dst is considered
1748 * only for addresses that has the same scope of dst.
2132 * prefered to any addresses of smaller scopes
2133 * (this must be already done above.)
2134 * - addresses on the outgoing I/F are preferred to
2135 * ones on other interfaces if none of above
2136 * tiebreaks. In the table below, the column "bI"
2137 * means if the best_ifa is on the outgoing
2138 * interface, and the column "sI" means if the ifa
2139 * is on the outgoing interface.
1749 * - If there is no other reasons to choose one,
2140 * - If there is no other reasons to choose one,
1750 * addresses on the outgoing I/F are preferred.
2141 * longest address match against dst is considered.
1751 *
1752 * The precise decision table is as follows:
2142 *
2143 * The precise decision table is as follows:
1753 * dscopecmp bscopecmp matchcmp outI/F | replace?
1754 * !equal equal N/A Yes | Yes (1)
1755 * !equal equal N/A No | No (2)
1756 * larger larger N/A N/A | No (3)
1757 * larger smaller N/A N/A | Yes (4)
1758 * smaller larger N/A N/A | Yes (5)
1759 * smaller smaller N/A N/A | No (6)
1760 * equal smaller N/A N/A | Yes (7)
1761 * equal larger (already done)
1762 * equal equal larger N/A | Yes (8)
1763 * equal equal smaller N/A | No (9)
1764 * equal equal equal Yes | Yes (a)
1765 * eaual eqaul equal No | No (b)
2144 * dscopecmp bscopecmp match bI oI | replace?
2145 * N/A equal N/A Y N | No (1)
2146 * N/A equal N/A N Y | Yes (2)
2147 * N/A equal larger N/A | Yes (3)
2148 * N/A equal !larger N/A | No (4)
2149 * larger larger N/A N/A | No (5)
2150 * larger smaller N/A N/A | Yes (6)
2151 * smaller larger N/A N/A | Yes (7)
2152 * smaller smaller N/A N/A | No (8)
2153 * equal smaller N/A N/A | Yes (9)
2154 * equal larger (already done at A above)
1766 */
1767 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
1768 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
1769
2155 */
2156 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2157 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2158
1770 if (dscopecmp && bscopecmp == 0) {
1771 if (oifp == ifp) /* (1) */
2159 if (bscopecmp == 0) {
2160 struct ifnet *bifp = ifa_best->ia_ifp;
2161
2162 if (bifp == oifp && ifp != oifp) /* (1) */
2163 continue;
2164 if (bifp != oifp && ifp == oifp) /* (2) */
1772 goto replace;
2165 goto replace;
1773 continue; /* (2) */
2166
2167 /*
2168 * Both bifp and ifp are on the outgoing
2169 * interface, or both two are on a different
2170 * interface from the outgoing I/F.
2171 * now we need address matching against dst
2172 * for tiebreaking.
2173 */
2174 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2175 matchcmp = tlen - blen;
2176 if (matchcmp > 0) /* (3) */
2177 goto replace;
2178 continue; /* (4) */
1774 }
1775 if (dscopecmp > 0) {
2179 }
2180 if (dscopecmp > 0) {
1776 if (bscopecmp > 0) /* (3) */
2181 if (bscopecmp > 0) /* (5) */
1777 continue;
2182 continue;
1778 goto replace; /* (4) */
2183 goto replace; /* (6) */
1779 }
1780 if (dscopecmp < 0) {
2184 }
2185 if (dscopecmp < 0) {
1781 if (bscopecmp > 0) /* (5) */
2186 if (bscopecmp > 0) /* (7) */
1782 goto replace;
2187 goto replace;
1783 continue; /* (6) */
2188 continue; /* (8) */
1784 }
1785
1786 /* now dscopecmp must be 0 */
1787 if (bscopecmp < 0)
2189 }
2190
2191 /* now dscopecmp must be 0 */
2192 if (bscopecmp < 0)
1788 goto replace; /* (7) */
2193 goto replace; /* (9) */
1789
2194
1790 /*
1791 * At last both dscopecmp and bscopecmp must be 0.
1792 * We need address matching against dst for
1793 * tiebreaking.
1794 */
1795 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1796 matchcmp = tlen - blen;
1797 if (matchcmp > 0) /* (8) */
1798 goto replace;
1799 if (matchcmp < 0) /* (9) */
1800 continue;
1801 if (oifp == ifp) /* (a) */
1802 goto replace;
1803 continue; /* (b) */
1804
1805 replace:
1806 ifa_best = (struct in6_ifaddr *)ifa;
1807 blen = tlen >= 0 ? tlen :
1808 in6_matchlen(IFA_IN6(ifa), dst);
1809 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
1810 }
1811 }
1812

--- 19 unchanged lines hidden (view full) ---

1832}
1833
1834/*
1835 * return the best address out of the same scope. if no address was
1836 * found, return the first valid address from designated IF.
1837 */
1838struct in6_ifaddr *
1839in6_ifawithifp(ifp, dst)
2195 replace:
2196 ifa_best = (struct in6_ifaddr *)ifa;
2197 blen = tlen >= 0 ? tlen :
2198 in6_matchlen(IFA_IN6(ifa), dst);
2199 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2200 }
2201 }
2202

--- 19 unchanged lines hidden (view full) ---

2222}
2223
2224/*
2225 * return the best address out of the same scope. if no address was
2226 * found, return the first valid address from designated IF.
2227 */
2228struct in6_ifaddr *
2229in6_ifawithifp(ifp, dst)
1840 register struct ifnet *ifp;
1841 register struct in6_addr *dst;
2230 struct ifnet *ifp;
2231 struct in6_addr *dst;
1842{
1843 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1844 struct ifaddr *ifa;
1845 struct in6_ifaddr *besta = 0;
1846 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1847
1848 dep[0] = dep[1] = NULL;
1849

--- 88 unchanged lines hidden (view full) ---

1938 if (ifa->ifa_addr->sa_family != AF_INET6)
1939 continue;
1940 ia = (struct in6_ifaddr *)ifa;
1941 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
1942 nd6_dad_start(ifa, &dad_delay);
1943 }
1944}
1945
2232{
2233 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2234 struct ifaddr *ifa;
2235 struct in6_ifaddr *besta = 0;
2236 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
2237
2238 dep[0] = dep[1] = NULL;
2239

--- 88 unchanged lines hidden (view full) ---

2328 if (ifa->ifa_addr->sa_family != AF_INET6)
2329 continue;
2330 ia = (struct in6_ifaddr *)ifa;
2331 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2332 nd6_dad_start(ifa, &dad_delay);
2333 }
2334}
2335
2336int
2337in6if_do_dad(ifp)
2338 struct ifnet *ifp;
2339{
2340 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2341 return(0);
2342
2343 switch (ifp->if_type) {
2344#ifdef IFT_DUMMY
2345 case IFT_DUMMY:
2346#endif
2347 case IFT_FAITH:
2348 /*
2349 * These interfaces do not have the IFF_LOOPBACK flag,
2350 * but loop packets back. We do not have to do DAD on such
2351 * interfaces. We should even omit it, because loop-backed
2352 * NS would confuse the DAD procedure.
2353 */
2354 return(0);
2355 default:
2356 /*
2357 * Our DAD routine requires the interface up and running.
2358 * However, some interfaces can be up before the RUNNING
2359 * status. Additionaly, users may try to assign addresses
2360 * before the interface becomes up (or running).
2361 * We simply skip DAD in such a case as a work around.
2362 * XXX: we should rather mark "tentative" on such addresses,
2363 * and do DAD after the interface becomes ready.
2364 */
2365 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2366 (IFF_UP|IFF_RUNNING))
2367 return(0);
2368
2369 return(1);
2370 }
2371}
2372
1946/*
1947 * Calculate max IPv6 MTU through all the interfaces and store it
1948 * to in6_maxmtu.
1949 */
1950void
1951in6_setmaxmtu()
1952{
1953 unsigned long maxmtu = 0;

--- 70 unchanged lines hidden ---
2373/*
2374 * Calculate max IPv6 MTU through all the interfaces and store it
2375 * to in6_maxmtu.
2376 */
2377void
2378in6_setmaxmtu()
2379{
2380 unsigned long maxmtu = 0;

--- 70 unchanged lines hidden ---