ip-pool.c (10463B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2021 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <errno.h> 28 #include <netinet/in.h> 29 #include <arpa/inet.h> 30 #include <linux/rtnetlink.h> 31 32 #include <ell/ell.h> 33 34 #include "src/util.h" 35 #include "src/iwd.h" 36 #include "src/module.h" 37 #include "src/netdev.h" 38 #include "src/netconfig.h" 39 #include "src/ip-pool.h" 40 41 struct ip_pool_addr4_record { 42 uint32_t ifindex; 43 struct l_rtnl_address *addr; 44 }; 45 46 struct ip_pool_addr4_range { 47 uint32_t start; 48 uint32_t end; 49 }; 50 51 static struct l_queue *used_addr4_list; 52 static struct l_netlink *rtnl; 53 54 static int ip_pool_addr4_range_compare(const void *a, const void *b, 55 void *user_data) 56 { 57 const struct ip_pool_addr4_range *range_a = a; 58 const struct ip_pool_addr4_range *range_b = b; 59 60 return range_a->start > range_b->start ? 1 : -1; 61 } 62 63 /* 64 * Append any address ranges within an input start/end range which contain 65 * at least one full subnet and don't intersect with any subnets already in 66 * use. This may result in the input range being split into multiple ranges 67 * of different sizes or being skipped altogether. 68 * All inputs must be rounded to the subnet boundary and the @used queue 69 * sorted by the subnet start address. 70 */ 71 static void ip_pool_append_range(struct l_queue *to, 72 const struct ip_pool_addr4_range *range, 73 struct l_queue *used, const char *str) 74 { 75 const struct l_queue_entry *entry = l_queue_get_entries(used); 76 const struct ip_pool_addr4_range *used_range = 77 entry ? entry->data : NULL; 78 uint32_t start = range->start; 79 bool print = true; 80 81 while (range->end > start) { 82 while (used_range && used_range->end <= start) { 83 entry = entry->next; 84 used_range = entry ? entry->data : NULL; 85 } 86 87 /* No more used ranges that intersect with @start/@range->end */ 88 if (!used_range || range->end <= used_range->start) { 89 struct ip_pool_addr4_range *sub = 90 l_new(struct ip_pool_addr4_range, 1); 91 92 sub->start = start; 93 sub->end = range->end; 94 l_queue_push_tail(to, sub); 95 l_queue_insert(used, l_memdup(sub, sizeof(*sub)), 96 ip_pool_addr4_range_compare, NULL); 97 return; 98 } 99 100 if (print) { 101 l_debug("Address spec %s intersects with at least one " 102 "subnet already in use on the system or " 103 "specified in the settings", str); 104 print = false; 105 } 106 107 /* Now we know @used_range is non-NULL and intersects */ 108 if (start < used_range->start) { 109 struct ip_pool_addr4_range *sub = 110 l_new(struct ip_pool_addr4_range, 1); 111 112 sub->start = start; 113 sub->end = used_range->start; 114 l_queue_push_tail(to, sub); 115 l_queue_insert(used, l_memdup(sub, sizeof(*sub)), 116 ip_pool_addr4_range_compare, NULL); 117 } 118 119 /* Skip to the start of the next subnet */ 120 start = used_range->end; 121 } 122 } 123 124 /* 125 * Select a subnet and a host address from a defined space. 126 * 127 * Returns: 0 when an address was selected and written to *out_addr, 128 * -EEXIST if all available subnet addresses are in use, 129 * -EINVAL if there was a different error. 130 */ 131 int ip_pool_select_addr4(const char **addr_str_list, uint8_t subnet_prefix_len, 132 struct l_rtnl_address **out_addr) 133 { 134 uint32_t total = 0; 135 uint32_t selected; 136 unsigned int i; 137 uint32_t subnet_size = 1 << (32 - subnet_prefix_len); 138 uint32_t host_mask = subnet_size - 1; 139 uint32_t subnet_mask = ~host_mask; 140 uint32_t host_addr = 0; 141 struct l_queue *ranges = l_queue_new(); 142 struct l_queue *used = l_queue_new(); 143 struct in_addr ia; 144 const struct l_queue_entry *entry; 145 int err = -EINVAL; 146 char ipstr[INET_ADDRSTRLEN]; 147 148 if (!addr_str_list || !addr_str_list[0]) 149 goto cleanup; 150 151 /* Build a sorted list of used/unavailable subnets */ 152 for (entry = l_queue_get_entries(used_addr4_list); 153 entry; entry = entry->next) { 154 const struct ip_pool_addr4_record *rec = entry->data; 155 struct ip_pool_addr4_range *range; 156 char addr_str[INET_ADDRSTRLEN]; 157 uint8_t used_prefix_len = 158 l_rtnl_address_get_prefix_length(rec->addr); 159 uint32_t used_subnet_size; 160 161 if (l_rtnl_address_get_family(rec->addr) != AF_INET || 162 !l_rtnl_address_get_address(rec->addr, 163 addr_str) || 164 used_prefix_len < 1 || 165 inet_pton(AF_INET, addr_str, &ia) != 1) 166 continue; 167 168 used_subnet_size = 1 << (32 - used_prefix_len); 169 170 range = l_new(struct ip_pool_addr4_range, 1); 171 range->start = ntohl(ia.s_addr) & subnet_mask; 172 range->end = (range->start + used_subnet_size + subnet_size - 173 1) & subnet_mask; 174 l_queue_insert(used, range, ip_pool_addr4_range_compare, NULL); 175 } 176 177 /* Build the list of available subnets */ 178 179 /* Check for the static IP syntax: Address=<IP> */ 180 if (l_strv_length((char **) addr_str_list) == 1 && 181 inet_pton(AF_INET, *addr_str_list, &ia) == 1) { 182 struct ip_pool_addr4_range range; 183 184 host_addr = ntohl(ia.s_addr); 185 range.start = host_addr & subnet_mask; 186 range.end = range.start + subnet_size; 187 ip_pool_append_range(ranges, &range, used, *addr_str_list); 188 goto check_avail; 189 } 190 191 for (i = 0; addr_str_list[i]; i++) { 192 struct ip_pool_addr4_range range; 193 uint32_t addr; 194 uint8_t addr_prefix; 195 196 if (!util_ip_prefix_tohl(addr_str_list[i], &addr_prefix, &addr, 197 NULL, NULL)) { 198 l_error("Can't parse %s as a subnet address", 199 addr_str_list[i]); 200 goto cleanup; 201 } 202 203 if (addr_prefix > subnet_prefix_len) { 204 l_debug("Address spec %s smaller than requested " 205 "subnet (prefix len %i)", addr_str_list[i], 206 subnet_prefix_len); 207 continue; 208 } 209 210 range.start = addr & subnet_mask; 211 range.end = range.start + (1 << (32 - addr_prefix)); 212 ip_pool_append_range(ranges, &range, used, addr_str_list[i]); 213 } 214 215 check_avail: 216 if (l_queue_isempty(ranges)) { 217 l_error("No IP subnets available for new Access Point after " 218 "eliminating those already in use on the system"); 219 err = -EEXIST; 220 goto cleanup; 221 } 222 223 if (host_addr) 224 goto done; 225 226 /* Count available @subnet_prefix_len-sized subnets */ 227 for (entry = l_queue_get_entries(ranges); entry; entry = entry->next) { 228 struct ip_pool_addr4_range *range = entry->data; 229 230 total += (range->end - range->start) >> 231 (32 - subnet_prefix_len); 232 } 233 234 selected = l_getrandom_uint32() % total; 235 236 /* Find the @selected'th @subnet_prefix_len-sized subnet */ 237 for (entry = l_queue_get_entries(ranges);; entry = entry->next) { 238 struct ip_pool_addr4_range *range = entry->data; 239 uint32_t count = (range->end - range->start) >> 240 (32 - subnet_prefix_len); 241 242 if (selected < count) { 243 host_addr = range->start + 244 (selected << (32 - subnet_prefix_len)); 245 break; 246 } 247 248 selected -= count; 249 } 250 251 if ((host_addr & host_mask) == 0) 252 host_addr += 1; 253 254 done: 255 err = 0; 256 ia.s_addr = htonl(host_addr); 257 if (L_WARN_ON(!inet_ntop(AF_INET, &ia, ipstr, INET_ADDRSTRLEN))) 258 err = -errno; 259 else 260 *out_addr = l_rtnl_address_new(ipstr, subnet_prefix_len); 261 262 cleanup: 263 l_queue_destroy(ranges, l_free); 264 l_queue_destroy(used, l_free); 265 return err; 266 } 267 268 static void ip_pool_addr4_record_free(void *data) 269 { 270 struct ip_pool_addr4_record *rec = data; 271 272 l_rtnl_address_free(rec->addr); 273 l_free(rec); 274 } 275 276 static bool ip_pool_addr4_match_ifindex(const void *a, const void *b) 277 { 278 const struct ip_pool_addr4_record *addr = a; 279 280 return addr->ifindex == L_PTR_TO_UINT(b); 281 } 282 283 struct l_rtnl_address *ip_pool_get_addr4(uint32_t ifindex) 284 { 285 const struct ip_pool_addr4_record *rec = 286 l_queue_find(used_addr4_list, ip_pool_addr4_match_ifindex, 287 L_UINT_TO_PTR(ifindex)); 288 289 return rec ? l_rtnl_address_clone(rec->addr) : 0; 290 } 291 292 static bool ip_pool_addr4_match_free(void *data, void *user_data) 293 { 294 const struct ip_pool_addr4_record *a = data; 295 const struct ip_pool_addr4_record *b = user_data; 296 char a_addr_str[INET_ADDRSTRLEN]; 297 char b_addr_str[INET_ADDRSTRLEN]; 298 299 if (a->ifindex != b->ifindex || 300 l_rtnl_address_get_prefix_length(a->addr) != 301 l_rtnl_address_get_prefix_length(b->addr)) 302 return false; 303 304 if (!l_rtnl_address_get_address(a->addr, a_addr_str) || 305 !l_rtnl_address_get_address(b->addr, b_addr_str) || 306 strcmp(a_addr_str, b_addr_str)) 307 return false; 308 309 ip_pool_addr4_record_free(data); 310 return true; 311 } 312 313 static void ip_pool_addr_notify(uint16_t type, const void *data, uint32_t len, 314 void *user_data) 315 { 316 const struct ifaddrmsg *ifa = data; 317 struct l_rtnl_address *addr; 318 319 if (ifa->ifa_family != AF_INET || ifa->ifa_prefixlen < 1) 320 return; 321 322 len -= NLMSG_ALIGN(sizeof(struct ifaddrmsg)); 323 324 addr = l_rtnl_ifaddr_extract(ifa, len); 325 if (!addr) 326 return; 327 328 if (type == RTM_NEWADDR) { 329 struct ip_pool_addr4_record *rec; 330 331 rec = l_new(struct ip_pool_addr4_record, 1); 332 rec->ifindex = ifa->ifa_index; 333 rec->addr = l_steal_ptr(addr); 334 l_queue_push_tail(used_addr4_list, rec); 335 } else if (type == RTM_DELADDR) { 336 struct ip_pool_addr4_record rec; 337 338 rec.ifindex = ifa->ifa_index; 339 rec.addr = addr; 340 341 l_queue_foreach_remove(used_addr4_list, 342 ip_pool_addr4_match_free, &rec); 343 } 344 345 l_rtnl_address_free(addr); 346 } 347 348 static void ip_pool_addr4_dump_cb(int error, 349 uint16_t type, const void *data, 350 uint32_t len, void *user_data) 351 { 352 if (error) { 353 l_error("addr4_dump_cb: %s (%i)", strerror(-error), -error); 354 return; 355 } 356 357 ip_pool_addr_notify(type, data, len, user_data); 358 } 359 360 static int ip_pool_init(void) 361 { 362 if (!netconfig_enabled()) 363 return 0; 364 365 rtnl = iwd_get_rtnl(); 366 367 if (!l_netlink_register(rtnl, RTNLGRP_IPV4_IFADDR, 368 ip_pool_addr_notify, NULL, NULL)) { 369 l_error("Failed to register for RTNL link notifications"); 370 return -EIO; 371 } 372 373 if (!l_rtnl_ifaddr4_dump(rtnl, ip_pool_addr4_dump_cb, NULL, NULL)) { 374 l_error("Sending the IPv4 addr dump req failed"); 375 return -EIO; 376 } 377 378 used_addr4_list = l_queue_new(); 379 return 0; 380 } 381 382 static void ip_pool_exit(void) 383 { 384 l_queue_destroy(used_addr4_list, ip_pool_addr4_record_free); 385 used_addr4_list = NULL; 386 } 387 388 IWD_MODULE(ip_pool, ip_pool_init, ip_pool_exit)