1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
/* $KAME: prefixconf.c,v 1.33 2005/09/16 11:30:15 suz Exp $ */
/*
* Copyright (C) 2002 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/ioctl.h>
#include <net/if.h>
#ifdef __FreeBSD__
#include <net/if_var.h>
#endif
#include <netinet/in.h>
#ifdef __KAME__
#include <netinet6/in6_var.h>
#include <netinet6/nd6.h>
#endif
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "dhcp6.h"
#include "config.h"
#include "common.h"
#include "timer.h"
#include "dhcp6c_ia.h"
#include "prefixconf.h"
TAILQ_HEAD(siteprefix_list, siteprefix);
struct iactl_pd {
struct iactl common;
struct pifc_list *pifc_head;
struct siteprefix_list siteprefix_head;
};
#define iacpd_ia common.iactl_ia
#define iacpd_callback common.callback
#define iacpd_isvalid common.isvalid
#define iacpd_duration common.duration
#define iacpd_renew_data common.renew_data
#define iacpd_rebind_data common.rebind_data
#define iacpd_reestablish_data common.reestablish_data
#define iacpd_release_data common.release_data
#define iacpd_cleanup common.cleanup
struct siteprefix {
TAILQ_ENTRY (siteprefix) link;
struct dhcp6_prefix prefix;
time_t updatetime;
struct dhcp6_timer *timer;
struct iactl_pd *ctl;
TAILQ_HEAD(, dhcp6_ifprefix) ifprefix_list; /* interface prefixes */
};
struct dhcp6_ifprefix {
TAILQ_ENTRY(dhcp6_ifprefix) plink;
/* interface configuration */
struct prefix_ifconf *ifconf;
/* interface prefix parameters */
struct sockaddr_in6 paddr;
int plen;
/* address assigned on the interface based on the prefix */
struct sockaddr_in6 ifaddr;
};
static struct siteprefix *find_siteprefix __P((struct siteprefix_list *,
struct dhcp6_prefix *, int));
static void remove_siteprefix __P((struct siteprefix *));
static int isvalid __P((struct iactl *));
static u_int32_t duration __P((struct iactl *));
static void cleanup __P((struct iactl *));
static int renew_prefix __P((struct iactl *, struct dhcp6_ia *,
struct dhcp6_eventdata **, struct dhcp6_eventdata *));
static void renew_data_free __P((struct dhcp6_eventdata *));
static struct dhcp6_timer *siteprefix_timo __P((void *));
static int add_ifprefix __P((struct siteprefix *,
struct dhcp6_prefix *, struct prefix_ifconf *));
extern struct dhcp6_timer *client6_timo __P((void *));
static int pd_ifaddrconf __P((ifaddrconf_cmd_t, struct dhcp6_ifprefix *ifpfx));
int
update_prefix(ia, pinfo, pifc, dhcpifp, ctlp, callback)
struct ia *ia;
struct dhcp6_prefix *pinfo;
struct pifc_list *pifc;
struct dhcp6_if *dhcpifp;
struct iactl **ctlp;
void (*callback)__P((struct ia *));
{
struct iactl_pd *iac_pd = (struct iactl_pd *)*ctlp;
struct siteprefix *sp;
struct prefix_ifconf *pif;
int spcreate = 0;
struct timeval timo;
/*
* A client discards any addresses for which the preferred
* lifetime is greater than the valid lifetime.
* [RFC3315 22.6]
*/
if (pinfo->vltime != DHCP6_DURATION_INFINITE &&
(pinfo->pltime == DHCP6_DURATION_INFINITE ||
pinfo->pltime > pinfo->vltime)) {
dprintf(LOG_INFO, FNAME, "invalid prefix %s/%d: "
"pltime (%lu) is larger than vltime (%lu)",
in6addr2str(&pinfo->addr, 0), pinfo->plen,
pinfo->pltime, pinfo->vltime);
return (-1);
}
if (iac_pd == NULL) {
if ((iac_pd = malloc(sizeof(*iac_pd))) == NULL) {
dprintf(LOG_NOTICE, FNAME, "memory allocation failed");
return (-1);
}
memset(iac_pd, 0, sizeof(*iac_pd));
iac_pd->iacpd_ia = ia;
iac_pd->iacpd_callback = callback;
iac_pd->iacpd_isvalid = isvalid;
iac_pd->iacpd_duration = duration;
iac_pd->iacpd_cleanup = cleanup;
iac_pd->iacpd_renew_data =
iac_pd->iacpd_rebind_data =
iac_pd->iacpd_release_data =
iac_pd->iacpd_reestablish_data = renew_prefix;
iac_pd->pifc_head = pifc;
TAILQ_INIT(&iac_pd->siteprefix_head);
*ctlp = (struct iactl *)iac_pd;
}
/* search for the given prefix, and make a new one if it fails */
if ((sp = find_siteprefix(&iac_pd->siteprefix_head, pinfo, 1)) == NULL) {
if ((sp = malloc(sizeof(*sp))) == NULL) {
dprintf(LOG_NOTICE, FNAME, "memory allocation failed");
return (-1);
}
memset(sp, 0, sizeof(*sp));
sp->prefix.addr = pinfo->addr;
sp->prefix.plen = pinfo->plen;
sp->ctl = iac_pd;
TAILQ_INIT(&sp->ifprefix_list);
TAILQ_INSERT_TAIL(&iac_pd->siteprefix_head, sp, link);
spcreate = 1;
}
/* update the timestamp of update */
sp->updatetime = time(NULL);
/* update the prefix according to pinfo */
sp->prefix.pltime = pinfo->pltime;
sp->prefix.vltime = pinfo->vltime;
dprintf(LOG_DEBUG, FNAME, "%s a prefix %s/%d pltime=%lu, vltime=%lu",
spcreate ? "create" : "update",
in6addr2str(&pinfo->addr, 0), pinfo->plen,
pinfo->pltime, pinfo->vltime);
/* update prefix interfaces if necessary */
if (sp->prefix.vltime != 0 && spcreate) {
for (pif = TAILQ_FIRST(iac_pd->pifc_head); pif;
pif = TAILQ_NEXT(pif, link)) {
/*
* The requesting router MUST NOT assign any delegated
* prefixes or subnets from the delegated prefix(es) to
* the link through which it received the DHCP message
* from the delegating router.
* [RFC3633 Section 12.1]
*/
if (strcmp(pif->ifname, dhcpifp->ifname) == 0) {
dprintf(LOG_INFO, FNAME,
"skip %s as a prefix interface",
dhcpifp->ifname);
continue;
}
add_ifprefix(sp, pinfo, pif);
}
}
/*
* If the new vltime is 0, this prefix immediately expires.
* Otherwise, set up or update the associated timer.
*/
switch (sp->prefix.vltime) {
case 0:
remove_siteprefix(sp);
break;
case DHCP6_DURATION_INFINITE:
if (sp->timer)
dhcp6_remove_timer(&sp->timer);
break;
default:
if (sp->timer == NULL) {
sp->timer = dhcp6_add_timer(siteprefix_timo, sp);
if (sp->timer == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to add prefix timer");
remove_siteprefix(sp); /* XXX */
return (-1);
}
}
/* update the timer */
timo.tv_sec = sp->prefix.vltime;
timo.tv_usec = 0;
dhcp6_set_timer(&timo, sp->timer);
break;
}
return (0);
}
static struct siteprefix *
find_siteprefix(head, prefix, match_plen)
struct siteprefix_list *head;
struct dhcp6_prefix *prefix;
int match_plen;
{
struct siteprefix *sp;
for (sp = TAILQ_FIRST(head); sp; sp = TAILQ_NEXT(sp, link)) {
if (!IN6_ARE_ADDR_EQUAL(&sp->prefix.addr, &prefix->addr))
continue;
if (match_plen == 0 || sp->prefix.plen == prefix->plen)
return (sp);
}
return (NULL);
}
static void
remove_siteprefix(sp)
struct siteprefix *sp;
{
struct dhcp6_ifprefix *ip;
dprintf(LOG_DEBUG, FNAME, "remove a site prefix %s/%d",
in6addr2str(&sp->prefix.addr, 0), sp->prefix.plen);
if (sp->timer)
dhcp6_remove_timer(&sp->timer);
/* remove all interface prefixes */
while ((ip = TAILQ_FIRST(&sp->ifprefix_list)) != NULL) {
TAILQ_REMOVE(&sp->ifprefix_list, ip, plink);
pd_ifaddrconf(IFADDRCONF_REMOVE, ip);
free(ip);
}
TAILQ_REMOVE(&sp->ctl->siteprefix_head, sp, link);
free(sp);
}
static int
isvalid(iac)
struct iactl *iac;
{
struct iactl_pd *iac_pd = (struct iactl_pd *)iac;
if (TAILQ_EMPTY(&iac_pd->siteprefix_head))
return (0); /* this IA is invalid */
return (1);
}
static u_int32_t
duration(iac)
struct iactl *iac;
{
struct iactl_pd *iac_pd = (struct iactl_pd *)iac;
struct siteprefix *sp;
u_int32_t base = DHCP6_DURATION_INFINITE, pltime, passed;
time_t now;
/* Determine the smallest period until pltime expires. */
now = time(NULL);
for (sp = TAILQ_FIRST(&iac_pd->siteprefix_head); sp;
sp = TAILQ_NEXT(sp, link)) {
passed = now > sp->updatetime ?
(u_int32_t)(now - sp->updatetime) : 0;
pltime = sp->prefix.pltime > passed ?
sp->prefix.pltime - passed : 0;
if (base == DHCP6_DURATION_INFINITE || pltime < base)
base = pltime;
}
return (base);
}
static void
cleanup(iac)
struct iactl *iac;
{
struct iactl_pd *iac_pd = (struct iactl_pd *)iac;
struct siteprefix *sp;
while ((sp = TAILQ_FIRST(&iac_pd->siteprefix_head)) != NULL) {
TAILQ_REMOVE(&iac_pd->siteprefix_head, sp, link);
remove_siteprefix(sp);
}
free(iac);
}
static int
renew_prefix(iac, iaparam, evdp, evd)
struct iactl *iac;
struct dhcp6_ia *iaparam;
struct dhcp6_eventdata **evdp, *evd;
{
struct iactl_pd *iac_pd = (struct iactl_pd *)iac;
struct siteprefix *sp;
struct dhcp6_list *ial = NULL, pl;
TAILQ_INIT(&pl);
for (sp = TAILQ_FIRST(&iac_pd->siteprefix_head); sp;
sp = TAILQ_NEXT(sp, link)) {
if (dhcp6_add_listval(&pl, DHCP6_LISTVAL_PREFIX6,
&sp->prefix, NULL) == NULL)
goto fail;
}
if ((ial = malloc(sizeof(*ial))) == NULL)
goto fail;
TAILQ_INIT(ial);
if (dhcp6_add_listval(ial, DHCP6_LISTVAL_IAPD, iaparam, &pl) == NULL)
goto fail;
dhcp6_clear_list(&pl);
evd->type = DHCP6_EVDATA_IAPD;
evd->data = (void *)ial;
evd->privdata = (void *)evdp;
evd->destructor = renew_data_free;
return (0);
fail:
dhcp6_clear_list(&pl);
if (ial)
free(ial);
return (-1);
}
static void
renew_data_free(evd)
struct dhcp6_eventdata *evd;
{
struct dhcp6_list *ial;
if (evd->type != DHCP6_EVDATA_IAPD) {
dprintf(LOG_ERR, FNAME, "assumption failure");
exit(1);
}
if (evd->privdata)
*(struct dhcp6_eventdata **)evd->privdata = NULL;
ial = (struct dhcp6_list *)evd->data;
dhcp6_clear_list(ial);
free(ial);
}
static struct dhcp6_timer *
siteprefix_timo(arg)
void *arg;
{
struct siteprefix *sp = (struct siteprefix *)arg;
struct ia *ia;
void (*callback)__P((struct ia *));
dprintf(LOG_DEBUG, FNAME, "prefix timeout for %s/%d",
in6addr2str(&sp->prefix.addr, 0), sp->prefix.plen);
ia = sp->ctl->iacpd_ia;
callback = sp->ctl->iacpd_callback;
if (sp->timer)
dhcp6_remove_timer(&sp->timer);
remove_siteprefix(sp);
(*callback)(ia);
return (NULL);
}
static int
add_ifprefix(siteprefix, prefix, pconf)
struct siteprefix *siteprefix;
struct dhcp6_prefix *prefix;
struct prefix_ifconf *pconf;
{
struct dhcp6_ifprefix *ifpfx = NULL;
struct in6_addr *a;
u_long sla_id;
char *sp;
int b, i;
if ((ifpfx = malloc(sizeof(*ifpfx))) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate memory for ifprefix");
return (-1);
}
memset(ifpfx, 0, sizeof(*ifpfx));
ifpfx->ifconf = pconf;
ifpfx->paddr.sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
ifpfx->paddr.sin6_len = sizeof(struct sockaddr_in6);
#endif
ifpfx->paddr.sin6_addr = prefix->addr;
ifpfx->plen = prefix->plen + pconf->sla_len;
/*
* XXX: our current implementation assumes ifid len is a multiple of 8
*/
if ((pconf->ifid_len % 8) != 0) {
dprintf(LOG_ERR, FNAME,
"assumption failure on the length of interface ID");
goto bad;
}
if (ifpfx->plen + pconf->ifid_len < 0 ||
ifpfx->plen + pconf->ifid_len > 128) {
dprintf(LOG_INFO, FNAME,
"invalid prefix length %d + %d + %d",
prefix->plen, pconf->sla_len, pconf->ifid_len);
goto bad;
}
/* copy prefix and SLA ID */
a = &ifpfx->paddr.sin6_addr;
b = prefix->plen;
for (i = 0, b = prefix->plen; b > 0; b -= 8, i++)
a->s6_addr[i] = prefix->addr.s6_addr[i];
sla_id = htonl(pconf->sla_id);
sp = ((char *)&sla_id + 3);
i = (128 - pconf->ifid_len) / 8;
for (b = pconf->sla_len; b > 7; b -= 8, sp--)
a->s6_addr[--i] = *sp;
if (b)
a->s6_addr[--i] |= *sp;
/* configure the corresponding address */
ifpfx->ifaddr = ifpfx->paddr;
for (i = 15; i >= pconf->ifid_len / 8; i--)
ifpfx->ifaddr.sin6_addr.s6_addr[i] = pconf->ifid[i];
if (pd_ifaddrconf(IFADDRCONF_ADD, ifpfx))
goto bad;
/* TODO: send a control message for other processes */
TAILQ_INSERT_TAIL(&siteprefix->ifprefix_list, ifpfx, plink);
return (0);
bad:
if (ifpfx)
free(ifpfx);
return (-1);
}
#ifndef ND6_INFINITE_LIFETIME
#define ND6_INFINITE_LIFETIME 0xffffffff
#endif
static int
pd_ifaddrconf(cmd, ifpfx)
ifaddrconf_cmd_t cmd;
struct dhcp6_ifprefix *ifpfx;
{
struct prefix_ifconf *pconf;
pconf = ifpfx->ifconf;
return (ifaddrconf(cmd, pconf->ifname, &ifpfx->ifaddr, ifpfx->plen,
ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME));
}
|