1 /* 2 * SPDX-FileCopyrightText: 2026 David Härdeman <david@hardeman.nu> 3 * 4 * SPDX-License-Identifier: GPL2.0-only 5 */ 6 7 /* 8 * Per-interface database: 9 * 10 * - iface->addr6_db: IPv6 addresses in use by clients 11 * - iface->duid_db: DUIDs in use by clients 12 */ 13 14 #include <stdlib.h> 15 #include <string.h> 16 17 #include "odhcpd.h" 18 19 static int addr6_db_cmp(const void *k1, const void *k2, _o_unused void *ptr) 20 { 21 return memcmp(k1, k2, sizeof(struct in6_addr)); 22 } 23 24 /* Key: duid_len, DUID */ 25 static int duid_db_cmp(const void *k1, const void *k2, _o_unused void *ptr) 26 { 27 const uint8_t *d1 = k1, *d2 = k2; 28 29 if (d1[0] != d2[0]) 30 return d1[0] - d2[0]; 31 32 return memcmp(&d1[1], &d2[1], d1[0]); 33 } 34 35 void db_init_iface(struct interface *iface) 36 { 37 avl_init(&iface->addr6_db, addr6_db_cmp, false, NULL); 38 avl_init(&iface->duid_db, duid_db_cmp, false, NULL); 39 } 40 41 struct duid_rec *duid_rec_find(struct interface *iface, const uint8_t *duid, size_t len) 42 { 43 uint8_t key[1 + DUID_MAX_LEN]; 44 struct duid_rec *rec; 45 46 if (len < DUID_MIN_LEN || len > DUID_MAX_LEN) 47 return NULL; 48 49 key[0] = len; 50 memcpy(&key[1], duid, len); 51 52 return avl_find_element(&iface->duid_db, key, rec, avl); 53 } 54 55 /* Get/create a DUID record */ 56 struct duid_rec *duid_rec_get(struct interface *iface, const uint8_t *duid, size_t len) 57 { 58 struct duid_rec *rec = duid_rec_find(iface, duid, len); 59 60 if (rec) { 61 rec->refcnt++; 62 return rec; 63 } 64 65 if (len < DUID_MIN_LEN || len > DUID_MAX_LEN) 66 return NULL; 67 68 rec = calloc(1, sizeof(*rec) + len); 69 if (!rec) 70 return NULL; 71 72 rec->iface = iface; 73 rec->refcnt = 1; 74 INIT_LIST_HEAD(&rec->addr6_recs); 75 INIT_LIST_HEAD(&rec->lease_cfg_list); 76 rec->duid_len = len; 77 memcpy(rec->duid, duid, len); 78 rec->avl.key = &rec->duid_len; 79 80 if (avl_insert(&iface->duid_db, &rec->avl)) { 81 free(rec); 82 return NULL; 83 } 84 85 config_link_duid_rec(rec); 86 87 return rec; 88 } 89 90 void duid_rec_put(struct duid_rec *rec) 91 { 92 if (!rec || --rec->refcnt > 0) 93 return; 94 95 avl_delete(&rec->iface->duid_db, &rec->avl); 96 list_del(&rec->lease_cfg_list); 97 free(rec->hostname); 98 free(rec); 99 } 100 101 struct addr6_rec *addr6_rec_find(struct interface *iface, const struct in6_addr *addr6) 102 { 103 struct addr6_rec *rec; 104 105 return avl_find_element(&iface->addr6_db, addr6, rec, avl); 106 } 107 108 /* Add an address to a DUID record */ 109 struct addr6_rec *addr6_rec_add(struct interface *iface, const struct in6_addr *addr6, 110 struct duid_rec *duid_rec) 111 { 112 struct addr6_rec *rec = calloc(1, sizeof(*rec)); 113 114 if (!rec) 115 return NULL; 116 117 rec->addr6 = *addr6; 118 rec->avl.key = &rec->addr6; 119 120 if (avl_insert(&iface->addr6_db, &rec->avl)) { 121 free(rec); 122 return NULL; 123 } 124 125 duid_rec->refcnt++; 126 rec->duid_rec = duid_rec; 127 list_add_tail(&rec->duid_rec_list, &duid_rec->addr6_recs); 128 129 return rec; 130 } 131 132 /* Move an address to another DUID record */ 133 void addr6_rec_set_duid_rec(struct addr6_rec *rec, struct duid_rec *duid_rec) 134 { 135 struct duid_rec *old = rec->duid_rec; 136 137 if (old == duid_rec) 138 return; 139 140 duid_rec->refcnt++; 141 rec->duid_rec = duid_rec; 142 list_move_tail(&rec->duid_rec_list, &duid_rec->addr6_recs); 143 duid_rec_put(old); 144 } 145 146 void addr6_rec_del(struct interface *iface, struct addr6_rec *rec) 147 { 148 avl_delete(&iface->addr6_db, &rec->avl); 149 list_del(&rec->duid_rec_list); 150 duid_rec_put(rec->duid_rec); 151 free(rec); 152 } 153 154 /* Delete the addresses of a DUID record (which may free it), return the count */ 155 size_t duid_rec_del_addr6s(struct duid_rec *rec) 156 { 157 struct addr6_rec *a, *tmp; 158 size_t cnt = 0; 159 160 rec->refcnt++; 161 list_for_each_entry_safe(a, tmp, &rec->addr6_recs, duid_rec_list) { 162 addr6_rec_del(rec->iface, a); 163 cnt++; 164 } 165 duid_rec_put(rec); 166 167 return cnt; 168 } 169 170 /* Delete the addresses within a prefix, return the count */ 171 size_t addr6_db_del_prefix(struct interface *iface, const struct in6_addr *prefix, uint8_t len) 172 { 173 struct addr6_rec *rec, *next; 174 struct in6_addr start = IN6ADDR_ANY_INIT; 175 size_t cnt = 0; 176 177 if (len > 128) 178 return 0; 179 180 /* The lowest address in the prefix */ 181 memcpy(&start, prefix, len / 8); 182 if (len % 8) 183 start.s6_addr[len / 8] = prefix->s6_addr[len / 8] & (0xff << (8 - len % 8)); 184 185 if (avl_is_empty(&iface->addr6_db)) 186 return 0; 187 188 rec = avl_find_ge_element(&iface->addr6_db, &start, rec, avl); 189 while (rec && !odhcpd_bmemcmp(&rec->addr6, &start, len)) { 190 next = avl_is_last(&iface->addr6_db, &rec->avl) ? 191 NULL : avl_next_element(rec, avl); 192 addr6_rec_del(iface, rec); 193 rec = next; 194 cnt++; 195 } 196 197 return cnt; 198 } 199 200 /* Delete all records of an interface */ 201 void db_flush_iface(struct interface *iface) 202 { 203 struct addr6_rec *rec, *tmp; 204 205 avl_for_each_element_safe(&iface->addr6_db, rec, avl, tmp) 206 addr6_rec_del(iface, rec); 207 } 208
This page was automatically generated by LXR 0.3.1. • OpenWrt