-
Notifications
You must be signed in to change notification settings - Fork 31
/
addrconf.c
385 lines (326 loc) · 9.94 KB
/
addrconf.c
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
/* $KAME: addrconf.c,v 1.8 2005/09/16 11:30:13 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.h"
#include "dhcp6c_ia.h"
#include "prefixconf.h"
#include "addrconf.h"
TAILQ_HEAD(statefuladdr_list, statefuladdr);
struct iactl_na {
struct iactl common;
struct statefuladdr_list statefuladdr_head;
};
#define iacna_ia common.iactl_ia
#define iacna_callback common.callback
#define iacna_isvalid common.isvalid
#define iacna_duration common.duration
#define iacna_renew_data common.renew_data
#define iacna_rebind_data common.rebind_data
#define iacna_reestablish_data common.reestablish_data
#define iacna_release_data common.release_data
#define iacna_cleanup common.cleanup
struct statefuladdr {
TAILQ_ENTRY (statefuladdr) link;
struct dhcp6_statefuladdr addr;
time_t updatetime;
struct dhcp6_timer *timer;
struct iactl_na *ctl;
struct dhcp6_if *dhcpif;
};
static struct statefuladdr *find_addr(struct statefuladdr_list *,
struct dhcp6_statefuladdr *);
static int remove_addr(struct statefuladdr *);
static int isvalid_addr(struct iactl *);
static uint32_t duration_addr(struct iactl *);
static void cleanup_addr(struct iactl *);
static int renew_addr(struct iactl *, struct dhcp6_ia *,
struct dhcp6_eventdata **, struct dhcp6_eventdata *);
static void na_renew_data_free(struct dhcp6_eventdata *);
static struct dhcp6_timer *addr_timo(void *);
static int na_ifaddrconf(ifaddrconf_cmd_t, struct statefuladdr *);
int
update_address(struct ia *ia, struct dhcp6_statefuladdr *addr,
struct dhcp6_if *dhcpifp, struct iactl **ctlp,
void (*callback)(struct ia *))
{
struct iactl_na *iac_na = (struct iactl_na *)*ctlp;
struct statefuladdr *sa;
int sacreate = 0;
struct timeval timo;
/*
* A client discards any addresses for which the preferred
* lifetime is greater than the valid lifetime.
* [RFC3315 22.6]
*/
if (addr->vltime != DHCP6_DURATION_INFINITE &&
(addr->pltime == DHCP6_DURATION_INFINITE ||
addr->pltime > addr->vltime)) {
d_printf(LOG_INFO, FNAME, "invalid address %s: "
"pltime (%lu) is larger than vltime (%lu)",
in6addr2str(&addr->addr, 0),
addr->pltime, addr->vltime);
return (-1);
}
if (iac_na == NULL) {
if ((iac_na = malloc(sizeof(*iac_na))) == NULL) {
d_printf(LOG_NOTICE, FNAME, "memory allocation failed");
return (-1);
}
memset(iac_na, 0, sizeof(*iac_na));
iac_na->iacna_ia = ia;
iac_na->iacna_callback = callback;
iac_na->iacna_isvalid = isvalid_addr;
iac_na->iacna_duration = duration_addr;
iac_na->iacna_cleanup = cleanup_addr;
iac_na->iacna_renew_data =
iac_na->iacna_rebind_data =
iac_na->iacna_release_data =
iac_na->iacna_reestablish_data = renew_addr;
TAILQ_INIT(&iac_na->statefuladdr_head);
*ctlp = (struct iactl *)iac_na;
}
/* search for the given address, and make a new one if it fails */
if ((sa = find_addr(&iac_na->statefuladdr_head, addr)) == NULL) {
if ((sa = malloc(sizeof(*sa))) == NULL) {
d_printf(LOG_NOTICE, FNAME, "memory allocation failed");
return (-1);
}
memset(sa, 0, sizeof(*sa));
sa->addr.addr = addr->addr;
sa->ctl = iac_na;
TAILQ_INSERT_TAIL(&iac_na->statefuladdr_head, sa, link);
sacreate = 1;
}
/* update the timestamp of update */
sa->updatetime = time(NULL);
/* update the prefix according to addr */
sa->addr.pltime = addr->pltime;
sa->addr.vltime = addr->vltime;
sa->dhcpif = dhcpifp;
d_printf(LOG_DEBUG, FNAME, "%s an address %s pltime=%u, vltime=%u",
sacreate ? "create" : "update",
in6addr2str(&addr->addr, 0), (unsigned int)addr->pltime,
(unsigned int)addr->vltime);
if (sa->addr.vltime != 0)
if (na_ifaddrconf(IFADDRCONF_ADD, sa) < 0)
return (-1);
/*
* If the new vltime is 0, this address immediately expires.
* Otherwise, set up or update the associated timer.
*/
switch (sa->addr.vltime) {
case 0:
if (remove_addr(sa) < 0)
return (-1);
break;
case DHCP6_DURATION_INFINITE:
if (sa->timer)
dhcp6_remove_timer(&sa->timer);
break;
default:
if (sa->timer == NULL) {
sa->timer = dhcp6_add_timer(addr_timo, sa);
if (sa->timer == NULL) {
d_printf(LOG_NOTICE, FNAME,
"failed to add stateful addr timer");
remove_addr(sa); /* XXX */
return (-1);
}
}
/* update the timer */
timo.tv_sec = sa->addr.vltime;
timo.tv_usec = 0;
dhcp6_set_timer(&timo, sa->timer);
break;
}
return (0);
}
static struct statefuladdr *
find_addr(struct statefuladdr_list *head, struct dhcp6_statefuladdr *addr)
{
struct statefuladdr *sa;
for (sa = TAILQ_FIRST(head); sa; sa = TAILQ_NEXT(sa, link)) {
if (!IN6_ARE_ADDR_EQUAL(&sa->addr.addr, &addr->addr))
continue;
return (sa);
}
return (NULL);
}
static int
remove_addr(struct statefuladdr *sa)
{
int ret;
d_printf(LOG_DEBUG, FNAME, "remove an address %s",
in6addr2str(&sa->addr.addr, 0));
if (sa->timer)
dhcp6_remove_timer(&sa->timer);
TAILQ_REMOVE(&sa->ctl->statefuladdr_head, sa, link);
ret = na_ifaddrconf(IFADDRCONF_REMOVE, sa);
free(sa);
return (ret);
}
static int
isvalid_addr(struct iactl *iac)
{
struct iactl_na *iac_na = (struct iactl_na *)iac;
if (TAILQ_EMPTY(&iac_na->statefuladdr_head))
return (0); /* this IA is invalid */
return (1);
}
static uint32_t
duration_addr(struct iactl *iac)
{
struct iactl_na *iac_na = (struct iactl_na *)iac;
struct statefuladdr *sa;
uint32_t base = DHCP6_DURATION_INFINITE, pltime, passed;
time_t now;
/* Determine the smallest period until pltime expires. */
now = time(NULL);
for (sa = TAILQ_FIRST(&iac_na->statefuladdr_head); sa;
sa = TAILQ_NEXT(sa, link)) {
passed = now > sa->updatetime ?
(uint32_t)(now - sa->updatetime) : 0;
pltime = sa->addr.pltime > passed ?
sa->addr.pltime - passed : 0;
if (base == DHCP6_DURATION_INFINITE || pltime < base)
base = pltime;
}
return (base);
}
static void
cleanup_addr(struct iactl *iac)
{
struct iactl_na *iac_na = (struct iactl_na *)iac;
struct statefuladdr *sa;
while ((sa = TAILQ_FIRST(&iac_na->statefuladdr_head)) != NULL) {
TAILQ_REMOVE(&iac_na->statefuladdr_head, sa, link);
remove_addr(sa);
}
free(iac);
}
static int
renew_addr(struct iactl *iac, struct dhcp6_ia *iaparam,
struct dhcp6_eventdata **evdp, struct dhcp6_eventdata *evd)
{
struct iactl_na *iac_na = (struct iactl_na *)iac;
struct statefuladdr *sa;
struct dhcp6_list *ial = NULL, pl;
TAILQ_INIT(&pl);
for (sa = TAILQ_FIRST(&iac_na->statefuladdr_head); sa;
sa = TAILQ_NEXT(sa, link)) {
if (dhcp6_add_listval(&pl, DHCP6_LISTVAL_STATEFULADDR6,
&sa->addr, NULL) == NULL)
goto fail;
}
if ((ial = malloc(sizeof(*ial))) == NULL)
goto fail;
TAILQ_INIT(ial);
if (dhcp6_add_listval(ial, DHCP6_LISTVAL_IANA, iaparam, &pl) == NULL)
goto fail;
dhcp6_clear_list(&pl);
evd->type = DHCP6_EVDATA_IANA;
evd->data = (void *)ial;
evd->privdata = (void *)evdp;
evd->destructor = na_renew_data_free;
return (0);
fail:
dhcp6_clear_list(&pl);
if (ial)
free(ial);
return (-1);
}
static void
na_renew_data_free(struct dhcp6_eventdata *evd)
{
struct dhcp6_list *ial;
if (evd->type != DHCP6_EVDATA_IANA) {
d_printf(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 *
addr_timo(void *arg)
{
struct statefuladdr *sa = (struct statefuladdr *)arg;
struct ia *ia;
void (*callback)(struct ia *);
d_printf(LOG_DEBUG, FNAME, "address timeout for %s",
in6addr2str(&sa->addr.addr, 0));
ia = sa->ctl->iacna_ia;
callback = sa->ctl->iacna_callback;
if (sa->timer)
dhcp6_remove_timer(&sa->timer);
remove_addr(sa);
(*callback)(ia);
return (NULL);
}
static int
na_ifaddrconf(ifaddrconf_cmd_t cmd, struct statefuladdr *sa)
{
struct dhcp6_statefuladdr *addr;
struct sockaddr_in6 sin6;
addr = &sa->addr;
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
sin6.sin6_len = sizeof(sin6);
#endif
sin6.sin6_addr = addr->addr;
return (ifaddrconf(cmd, sa->dhcpif->ifname, &sin6, 128,
addr->pltime, addr->vltime));
}