pmksa.c (6049B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2023 Cruise LLC. 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 #define _GNU_SOURCE 28 #include <stdint.h> 29 #include <errno.h> 30 31 #include <ell/ell.h> 32 #include "ell/useful.h" 33 34 #include "src/module.h" 35 #include "src/pmksa.h" 36 37 #define PMKID "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" 38 #define PMKID_STR(x) x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], \ 39 x[8], x[9], x[10], x[11], x[12], x[13], x[14], x[15] 40 41 static uint64_t dot11RSNAConfigPMKLifetime = 43200ULL * L_USEC_PER_SEC; 42 static uint32_t pmksa_cache_capacity = 255; 43 static pmksa_cache_add_func_t driver_add; 44 static pmksa_cache_remove_func_t driver_remove; 45 static pmksa_cache_flush_func_t driver_flush; 46 47 struct min_heap { 48 struct pmksa **data; 49 uint32_t capacity; 50 uint32_t used; 51 }; 52 53 static struct min_heap cache; 54 55 static __always_inline void swap_ptr(void *l, void *r) 56 { 57 struct pmksa **lp = l; 58 struct pmksa **rp = r; 59 60 SWAP(*lp, *rp); 61 } 62 63 static __always_inline 64 bool pmksa_compare_expiration(const void *l, const void *r) 65 { 66 const struct pmksa * const *lp = l; 67 const struct pmksa * const *rp = r; 68 69 return (*lp)->expiration < (*rp)->expiration; 70 } 71 72 static struct l_minheap_ops ops = { 73 .elem_size = sizeof(struct pmksa *), 74 .swap = swap_ptr, 75 .less = pmksa_compare_expiration, 76 }; 77 78 static int pmksa_cache_find(const uint8_t spa[static 6], 79 const uint8_t aa[static 6], 80 const uint8_t *ssid, size_t ssid_len, 81 uint32_t akm) 82 { 83 unsigned int i; 84 85 for (i = 0; i < cache.used; i++) { 86 struct pmksa *pmksa = cache.data[i]; 87 88 if (memcmp(pmksa->spa, spa, sizeof(pmksa->spa))) 89 continue; 90 91 if (memcmp(pmksa->aa, aa, sizeof(pmksa->aa))) 92 continue; 93 94 if (ssid_len != pmksa->ssid_len) 95 continue; 96 97 if (memcmp(pmksa->ssid, ssid, ssid_len)) 98 continue; 99 100 if (akm & pmksa->akm) 101 return i; 102 } 103 104 return -ENOENT; 105 } 106 107 /* 108 * Try to obtain a PMKSA entry from the cache. If the the entry matching 109 * the parameters is found, it is removed from the cache and returned to the 110 * caller. The caller is responsible for managing the returned pmksa 111 * structure 112 */ 113 struct pmksa *pmksa_cache_get(const uint8_t spa[static 6], 114 const uint8_t aa[static 6], 115 const uint8_t *ssid, size_t ssid_len, 116 uint32_t akm) 117 { 118 struct pmksa *pmksa; 119 int r = pmksa_cache_find(spa, aa, ssid, ssid_len, akm); 120 121 if (r < 0) 122 return NULL; 123 124 cache.used -= 1; 125 if ((uint32_t) r == cache.used) 126 goto done; 127 128 SWAP(cache.data[r], cache.data[cache.used]); 129 __minheap_sift_down(cache.data, cache.used, r, &ops); 130 131 done: 132 pmksa = cache.data[cache.used]; 133 134 l_debug("Returning entry with PMKID: "PMKID, PMKID_STR(pmksa->pmkid)); 135 136 return pmksa; 137 } 138 139 /* 140 * Put a PMKSA into the cache. It will be sorted in soonest-to-expire order. 141 * If the cache is full, then soonest-to-expire entry is removed first. 142 */ 143 int pmksa_cache_put(struct pmksa *pmksa) 144 { 145 l_debug("Adding entry with PMKID: "PMKID, PMKID_STR(pmksa->pmkid)); 146 147 if (cache.used == cache.capacity) { 148 pmksa_cache_free(cache.data[0]); 149 cache.data[0] = pmksa; 150 __minheap_sift_down(cache.data, cache.used, 0, &ops); 151 return 0; 152 } 153 154 cache.data[cache.used] = pmksa; 155 __minheap_sift_up(cache.data, cache.used, &ops); 156 cache.used += 1; 157 158 if (driver_add) 159 driver_add(pmksa); 160 161 return 0; 162 } 163 164 /* 165 * Expire all PMKSA entries with expiration time smaller or equal to the cutoff 166 * time. 167 */ 168 int pmksa_cache_expire(uint64_t cutoff) 169 { 170 int i; 171 int used = cache.used; 172 int remaining = 0; 173 174 for (i = 0; i < used; i++) { 175 if (cache.data[i]->expiration <= cutoff) { 176 pmksa_cache_free(cache.data[i]); 177 continue; 178 } 179 180 cache.data[remaining] = cache.data[i]; 181 remaining += 1; 182 } 183 184 cache.used = remaining; 185 186 for (i = cache.used >> 1; i >= 0; i--) 187 __minheap_sift_down(cache.data, cache.used, i, &ops); 188 189 return used - remaining; 190 } 191 192 /* 193 * Flush all PMKSA entries from the cache, regardless of expiration time. 194 */ 195 int pmksa_cache_flush(void) 196 { 197 uint32_t i; 198 199 /* 200 * The driver flush operation is done via a single kernel API call which 201 * is why below we use l_free instead of pmksa_cache_free as to not 202 * induce a DEL_PMKSA kernel call for each entry. 203 */ 204 if (driver_flush) 205 driver_flush(); 206 207 for (i = 0; i < cache.used; i++) 208 l_free(cache.data[i]); 209 210 memset(cache.data, 0, cache.capacity * sizeof(struct pmksa *)); 211 cache.used = 0; 212 213 return 0; 214 } 215 216 int pmksa_cache_free(struct pmksa *pmksa) 217 { 218 if (driver_remove) 219 driver_remove(pmksa); 220 221 l_free(pmksa); 222 223 return 0; 224 } 225 226 struct pmksa **__pmksa_cache_get_all(uint32_t *out_n_entries) 227 { 228 if (out_n_entries) 229 *out_n_entries = cache.used; 230 231 return cache.data; 232 } 233 234 uint64_t pmksa_lifetime(void) 235 { 236 return dot11RSNAConfigPMKLifetime; 237 } 238 239 void __pmksa_set_config(const struct l_settings *config) 240 { 241 l_settings_get_uint(config, "PMKSA", "Capacity", 242 &pmksa_cache_capacity); 243 } 244 245 void __pmksa_set_driver_callbacks(pmksa_cache_add_func_t add, 246 pmksa_cache_remove_func_t remove, 247 pmksa_cache_flush_func_t flush) 248 { 249 driver_add = add; 250 driver_remove = remove; 251 driver_flush = flush; 252 } 253 254 static int pmksa_init(void) 255 { 256 cache.capacity = pmksa_cache_capacity; 257 cache.used = 0; 258 cache.data = l_new(struct pmksa *, cache.capacity); 259 260 return 0; 261 } 262 263 static void pmksa_exit(void) 264 { 265 pmksa_cache_flush(); 266 l_free(cache.data); 267 } 268 269 IWD_MODULE(pmksa, pmksa_init, pmksa_exit);