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
|
/*
* SPDX-FileCopyrightText: 2026 David Härdeman <david@hardeman.nu>
*
* SPDX-License-Identifier: GPL2.0-only
*/
/*
* Per-interface database:
*
* - iface->addr6_db: IPv6 addresses in use by clients
* - iface->duid_db: DUIDs in use by clients
*/
#include <stdlib.h>
#include <string.h>
#include "odhcpd.h"
static int addr6_db_cmp(const void *k1, const void *k2, _o_unused void *ptr)
{
return memcmp(k1, k2, sizeof(struct in6_addr));
}
/* Key: duid_len, DUID */
static int duid_db_cmp(const void *k1, const void *k2, _o_unused void *ptr)
{
const uint8_t *d1 = k1, *d2 = k2;
if (d1[0] != d2[0])
return d1[0] - d2[0];
return memcmp(&d1[1], &d2[1], d1[0]);
}
void db_init_iface(struct interface *iface)
{
avl_init(&iface->addr6_db, addr6_db_cmp, false, NULL);
avl_init(&iface->duid_db, duid_db_cmp, false, NULL);
}
struct duid_rec *duid_rec_find(struct interface *iface, const uint8_t *duid, size_t len)
{
uint8_t key[1 + DUID_MAX_LEN];
struct duid_rec *rec;
if (len < DUID_MIN_LEN || len > DUID_MAX_LEN)
return NULL;
key[0] = len;
memcpy(&key[1], duid, len);
return avl_find_element(&iface->duid_db, key, rec, avl);
}
/* Get/create a DUID record */
struct duid_rec *duid_rec_get(struct interface *iface, const uint8_t *duid, size_t len)
{
struct duid_rec *rec = duid_rec_find(iface, duid, len);
if (rec) {
rec->refcnt++;
return rec;
}
if (len < DUID_MIN_LEN || len > DUID_MAX_LEN)
return NULL;
rec = calloc(1, sizeof(*rec) + len);
if (!rec)
return NULL;
rec->iface = iface;
rec->refcnt = 1;
INIT_LIST_HEAD(&rec->addr6_recs);
INIT_LIST_HEAD(&rec->lease_cfg_list);
rec->duid_len = len;
memcpy(rec->duid, duid, len);
rec->avl.key = &rec->duid_len;
if (avl_insert(&iface->duid_db, &rec->avl)) {
free(rec);
return NULL;
}
config_link_duid_rec(rec);
return rec;
}
void duid_rec_put(struct duid_rec *rec)
{
if (!rec || --rec->refcnt > 0)
return;
avl_delete(&rec->iface->duid_db, &rec->avl);
list_del(&rec->lease_cfg_list);
free(rec->hostname);
free(rec);
}
struct addr6_rec *addr6_rec_find(struct interface *iface, const struct in6_addr *addr6)
{
struct addr6_rec *rec;
return avl_find_element(&iface->addr6_db, addr6, rec, avl);
}
/* Add an address to a DUID record */
struct addr6_rec *addr6_rec_add(struct interface *iface, const struct in6_addr *addr6,
struct duid_rec *duid_rec)
{
struct addr6_rec *rec = calloc(1, sizeof(*rec));
if (!rec)
return NULL;
rec->addr6 = *addr6;
rec->avl.key = &rec->addr6;
if (avl_insert(&iface->addr6_db, &rec->avl)) {
free(rec);
return NULL;
}
duid_rec->refcnt++;
rec->duid_rec = duid_rec;
list_add_tail(&rec->duid_rec_list, &duid_rec->addr6_recs);
return rec;
}
/* Move an address to another DUID record */
void addr6_rec_set_duid_rec(struct addr6_rec *rec, struct duid_rec *duid_rec)
{
struct duid_rec *old = rec->duid_rec;
if (old == duid_rec)
return;
duid_rec->refcnt++;
rec->duid_rec = duid_rec;
list_move_tail(&rec->duid_rec_list, &duid_rec->addr6_recs);
duid_rec_put(old);
}
void addr6_rec_del(struct interface *iface, struct addr6_rec *rec)
{
avl_delete(&iface->addr6_db, &rec->avl);
list_del(&rec->duid_rec_list);
duid_rec_put(rec->duid_rec);
free(rec);
}
/* Delete the addresses of a DUID record (which may free it), return the count */
size_t duid_rec_del_addr6s(struct duid_rec *rec)
{
struct addr6_rec *a, *tmp;
size_t cnt = 0;
rec->refcnt++;
list_for_each_entry_safe(a, tmp, &rec->addr6_recs, duid_rec_list) {
addr6_rec_del(rec->iface, a);
cnt++;
}
duid_rec_put(rec);
return cnt;
}
/* Delete the addresses within a prefix, return the count */
size_t addr6_db_del_prefix(struct interface *iface, const struct in6_addr *prefix, uint8_t len)
{
struct addr6_rec *rec, *next;
struct in6_addr start = IN6ADDR_ANY_INIT;
size_t cnt = 0;
if (len > 128)
return 0;
/* The lowest address in the prefix */
memcpy(&start, prefix, len / 8);
if (len % 8)
start.s6_addr[len / 8] = prefix->s6_addr[len / 8] & (0xff << (8 - len % 8));
if (avl_is_empty(&iface->addr6_db))
return 0;
rec = avl_find_ge_element(&iface->addr6_db, &start, rec, avl);
while (rec && !odhcpd_bmemcmp(&rec->addr6, &start, len)) {
next = avl_is_last(&iface->addr6_db, &rec->avl) ?
NULL : avl_next_element(rec, avl);
addr6_rec_del(iface, rec);
rec = next;
cnt++;
}
return cnt;
}
/* Delete all records of an interface */
void db_flush_iface(struct interface *iface)
{
struct addr6_rec *rec, *tmp;
avl_for_each_element_safe(&iface->addr6_db, rec, avl, tmp)
addr6_rec_del(iface, rec);
}
|