eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

blacklist.c (6781B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2019  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 <ell/ell.h>
     28 
     29 #include "src/blacklist.h"
     30 #include "src/util.h"
     31 #include "src/iwd.h"
     32 #include "src/module.h"
     33 
     34 /*
     35  * The current timeout is multiplied by this value after an entry is blacklisted
     36  * more than once.
     37  */
     38 #define BLACKLIST_DEFAULT_MULTIPLIER	30
     39 
     40 /* Initial timeout for a new blacklist entry */
     41 #define BLACKLIST_DEFAULT_TIMEOUT	60
     42 
     43 /* The maximum amount of time a BSS can be blacklisted for */
     44 #define BLACKLIST_DEFAULT_MAX_TIMEOUT	86400
     45 
     46 static uint64_t blacklist_multiplier;
     47 static uint64_t blacklist_initial_timeout;
     48 static uint64_t blacklist_ap_busy_initial_timeout;
     49 static uint64_t blacklist_max_timeout;
     50 
     51 struct blacklist_entry {
     52 	uint8_t addr[6];
     53 	uint64_t added_time;
     54 	uint64_t expire_time;
     55 	enum blacklist_reason reason;
     56 };
     57 
     58 struct blacklist_search {
     59 	const uint8_t *addr;
     60 	enum blacklist_reason reason;
     61 };
     62 
     63 static struct l_queue *blacklist;
     64 
     65 static uint64_t get_reason_timeout(enum blacklist_reason reason)
     66 {
     67 	switch (reason) {
     68 	case BLACKLIST_REASON_CONNECT_FAILED:
     69 		return blacklist_initial_timeout;
     70 	case BLACKLIST_REASON_AP_BUSY:
     71 		return blacklist_ap_busy_initial_timeout;
     72 	default:
     73 		l_warn("Unhandled blacklist reason: %u", reason);
     74 		return 0;
     75 	}
     76 }
     77 
     78 static bool check_if_expired(void *data, void *user_data)
     79 {
     80 	struct blacklist_entry *entry = data;
     81 	uint64_t now = l_get_u64(user_data);
     82 
     83 	if (l_time_after(now, entry->expire_time)) {
     84 		l_debug("Removing entry "MAC" on prune", MAC_STR(entry->addr));
     85 		l_free(entry);
     86 		return true;
     87 	}
     88 
     89 	return false;
     90 }
     91 
     92 static void blacklist_prune(void)
     93 {
     94 	uint64_t now = l_time_now();
     95 
     96 	l_queue_foreach_remove(blacklist, check_if_expired, &now);
     97 }
     98 
     99 static bool match_addr(const void *a, const void *b)
    100 {
    101 	const struct blacklist_entry *entry = a;
    102 	const uint8_t *addr = b;
    103 
    104 	if (!memcmp(entry->addr, addr, 6))
    105 		return true;
    106 
    107 	return false;
    108 }
    109 
    110 static bool match_addr_and_reason(const void *a, const void *b)
    111 {
    112 	const struct blacklist_entry *entry = a;
    113 	const struct blacklist_search *search = b;
    114 
    115 	if (entry->reason != search->reason)
    116 		return false;
    117 
    118 	if (!memcmp(entry->addr, search->addr, 6))
    119 		return true;
    120 
    121 	return false;
    122 }
    123 
    124 void blacklist_add_bss(const uint8_t *addr, enum blacklist_reason reason)
    125 {
    126 	struct blacklist_entry *entry;
    127 	uint64_t timeout;
    128 
    129 	blacklist_prune();
    130 
    131 	timeout = get_reason_timeout(reason);
    132 	if (!timeout)
    133 		return;
    134 
    135 	entry = l_queue_find(blacklist, match_addr, addr);
    136 
    137 	if (entry) {
    138 		uint64_t offset;
    139 
    140 		if (reason < entry->reason) {
    141 			l_debug("Promoting "MAC" blacklist to reason %u",
    142 					MAC_STR(addr), reason);
    143 			/* Reset this to the new timeout and reason */
    144 			entry->reason = reason;
    145 			entry->added_time = l_time_now();
    146 			entry->expire_time = l_time_offset(entry->added_time,
    147 								timeout);
    148 			return;
    149 		} else if (reason > entry->reason) {
    150 			l_debug("Ignoring blacklist extension of "MAC", "
    151 				"current blacklist status is more severe!",
    152 				MAC_STR(addr));
    153 			return;
    154 		}
    155 
    156 		offset = l_time_diff(entry->added_time, entry->expire_time);
    157 
    158 		offset *= blacklist_multiplier;
    159 
    160 		if (offset > blacklist_max_timeout)
    161 			offset = blacklist_max_timeout;
    162 
    163 		entry->expire_time = l_time_offset(entry->added_time, offset);
    164 
    165 		return;
    166 	}
    167 
    168 	entry = l_new(struct blacklist_entry, 1);
    169 
    170 	entry->added_time = l_time_now();
    171 	entry->expire_time = l_time_offset(entry->added_time, timeout);
    172 	entry->reason = reason;
    173 	memcpy(entry->addr, addr, 6);
    174 
    175 	l_queue_push_tail(blacklist, entry);
    176 }
    177 
    178 bool blacklist_contains_bss(const uint8_t *addr, enum blacklist_reason reason)
    179 {
    180 	struct blacklist_search search = {
    181 		.addr = addr,
    182 		.reason = reason
    183 	};
    184 
    185 	blacklist_prune();
    186 
    187 	return l_queue_find(blacklist, match_addr_and_reason, &search) != NULL;
    188 }
    189 
    190 void blacklist_remove_bss(const uint8_t *addr, enum blacklist_reason reason)
    191 {
    192 	struct blacklist_entry *entry;
    193 	struct blacklist_search search = {
    194 		.addr = addr,
    195 		.reason = reason
    196 	};
    197 
    198 	blacklist_prune();
    199 
    200 	entry = l_queue_remove_if(blacklist, match_addr_and_reason, &search);
    201 
    202 	if (!entry)
    203 		return;
    204 
    205 	l_free(entry);
    206 }
    207 
    208 static int blacklist_init(void)
    209 {
    210 	const struct l_settings *config = iwd_get_config();
    211 
    212 	if (!l_settings_get_uint64(config, "Blacklist", "InitialTimeout",
    213 					&blacklist_initial_timeout))
    214 		blacklist_initial_timeout = BLACKLIST_DEFAULT_TIMEOUT;
    215 
    216 	/* For easier user configuration the timeout values are in seconds */
    217 	blacklist_initial_timeout *= L_USEC_PER_SEC;
    218 
    219 	if (!l_settings_get_uint64(config, "Blacklist",
    220 					"InitialRoamRequestedTimeout",
    221 					&blacklist_ap_busy_initial_timeout))
    222 		blacklist_ap_busy_initial_timeout = BLACKLIST_DEFAULT_TIMEOUT;
    223 	else
    224 		l_warn("[Blacklist].InitialRoamRequestedTimeout is deprecated, "
    225 			"use [Blacklist].InitialAccessPointBusyTimeout");
    226 
    227 	if (!l_settings_get_uint64(config, "Blacklist",
    228 					"InitialAccessPointBusyTimeout",
    229 					&blacklist_ap_busy_initial_timeout))
    230 		blacklist_ap_busy_initial_timeout = BLACKLIST_DEFAULT_TIMEOUT;
    231 
    232 	/* For easier user configuration the timeout values are in seconds */
    233 	blacklist_ap_busy_initial_timeout *= L_USEC_PER_SEC;
    234 
    235 	if (!l_settings_get_uint64(config, "Blacklist",
    236 					"Multiplier",
    237 					&blacklist_multiplier))
    238 		blacklist_multiplier = BLACKLIST_DEFAULT_MULTIPLIER;
    239 
    240 	if (blacklist_multiplier == 0) {
    241 		l_warn("[Blacklist].Multiplier cannot be zero, setting to 1");
    242 		blacklist_multiplier = 1;
    243 	}
    244 
    245 	if (!l_settings_get_uint64(config, "Blacklist",
    246 					"MaximumTimeout",
    247 					&blacklist_max_timeout))
    248 		blacklist_max_timeout = BLACKLIST_DEFAULT_MAX_TIMEOUT;
    249 
    250 	blacklist_max_timeout *= L_USEC_PER_SEC;
    251 
    252 	if (blacklist_initial_timeout > blacklist_max_timeout)
    253 		l_warn("[Blacklist].InitialTimeout exceeded "
    254 			"[Blacklist].MaximumTimeout!");
    255 
    256 	if (!blacklist_initial_timeout)
    257 		l_debug("initial timeout was zero, blacklist will be disabled");
    258 
    259 	blacklist = l_queue_new();
    260 
    261 	return 0;
    262 }
    263 
    264 static void blacklist_exit(void)
    265 {
    266 	l_queue_destroy(blacklist, l_free);
    267 }
    268 
    269 IWD_MODULE(blacklist, blacklist_init, blacklist_exit)