1 /* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License. 5 * 6 * This program is distributed in the hope that it will be useful, 7 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * GNU General Public License for more details. 10 * 11 * You should have received a copy of the GNU General Public License 12 * along with this program; if not, write to the Free Software 13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 14 * 15 * Copyright (C) 2020 embedd.ch 16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> 17 * Copyright (C) 2020 John Crispin <john@phrozen.org> 18 */ 19 20 #include "usteer.h" 21 22 static int 23 avl_macaddr_cmp(const void *k1, const void *k2, void *ptr) 24 { 25 return memcmp(k1, k2, 6); 26 } 27 28 AVL_TREE(stations, avl_macaddr_cmp, false, NULL); 29 static struct usteer_timeout_queue tq; 30 31 static void 32 usteer_sta_del(struct sta *sta) 33 { 34 MSG(DEBUG, "Delete station " MAC_ADDR_FMT "\n", 35 MAC_ADDR_DATA(sta->addr)); 36 37 avl_delete(&stations, &sta->avl); 38 usteer_measurement_report_sta_cleanup(sta); 39 free(sta); 40 } 41 42 static void 43 usteer_sta_info_del(struct sta_info *si) 44 { 45 struct sta *sta = si->sta; 46 47 MSG(DEBUG, "Delete station " MAC_ADDR_FMT " entry for node %s\n", 48 MAC_ADDR_DATA(sta->addr), usteer_node_name(si->node)); 49 50 usteer_timeout_cancel(&tq, &si->timeout); 51 list_del(&si->list); 52 list_del(&si->node_list); 53 free(si); 54 55 if (list_empty(&sta->nodes)) 56 usteer_sta_del(sta); 57 } 58 59 void 60 usteer_sta_node_cleanup(struct usteer_node *node) 61 { 62 struct sta_info *si, *tmp; 63 64 free(node->rrm_nr); 65 node->rrm_nr = NULL; 66 67 list_for_each_entry_safe(si, tmp, &node->sta_info, node_list) 68 usteer_sta_info_del(si); 69 } 70 71 static void 72 usteer_sta_info_timeout(struct usteer_timeout_queue *q, struct usteer_timeout *t) 73 { 74 struct sta_info *si = container_of(t, struct sta_info, timeout); 75 76 usteer_sta_info_del(si); 77 } 78 79 static void 80 usteer_sta_update_aggressiveness(struct sta *sta) 81 { 82 struct blob_attr *cur; 83 int rem; 84 char sta_mac[18]; 85 char config_entry[20]; 86 char config_mac[18]; 87 88 sprintf(sta_mac, MAC_ADDR_FMT, MAC_ADDR_DATA(sta->addr)); 89 sta->aggressiveness = config.aggressiveness; 90 blobmsg_for_each_attr(cur, config.aggressiveness_mac_list, rem) { 91 strcpy(config_entry, blobmsg_get_string(cur)); 92 if (strlen(config_entry) != 19) 93 continue; 94 strncpy(config_mac, config_entry, 18); 95 config_mac[17] = '\0'; 96 if (strcmp(config_mac, sta_mac) != 0) 97 continue; 98 sta->aggressiveness = config_entry[18] - ''; 99 break; 100 } 101 } 102 103 struct sta_info * 104 usteer_sta_info_get(struct sta *sta, struct usteer_node *node, bool *create) 105 { 106 struct sta_info *si; 107 108 list_for_each_entry(si, &sta->nodes, list) { 109 if (si->node != node) 110 continue; 111 112 if (create) 113 *create = false; 114 115 return si; 116 } 117 118 if (!create) 119 return NULL; 120 121 MSG(DEBUG, "Create station " MAC_ADDR_FMT " entry for node %s\n", 122 MAC_ADDR_DATA(sta->addr), usteer_node_name(node)); 123 124 si = calloc(1, sizeof(*si)); 125 si->node = node; 126 si->sta = sta; 127 list_add(&si->list, &sta->nodes); 128 list_add(&si->node_list, &node->sta_info); 129 si->created = current_time; 130 *create = true; 131 132 usteer_sta_update_aggressiveness(sta); 133 134 /* Node is by default not connected. */ 135 usteer_sta_disconnected(si); 136 137 return si; 138 } 139 140 141 void 142 usteer_sta_info_update_timeout(struct sta_info *si, int timeout) 143 { 144 if (si->connected == STA_CONNECTED) 145 usteer_timeout_cancel(&tq, &si->timeout); 146 else if (timeout > 0) 147 usteer_timeout_set(&tq, &si->timeout, timeout); 148 else 149 usteer_sta_info_del(si); 150 } 151 152 struct sta * 153 usteer_sta_get(const uint8_t *addr, bool create) 154 { 155 struct sta *sta; 156 157 sta = avl_find_element(&stations, addr, sta, avl); 158 if (sta) 159 return sta; 160 161 if (!create) 162 return NULL; 163 164 MSG(DEBUG, "Create station entry " MAC_ADDR_FMT "\n", MAC_ADDR_DATA(addr)); 165 sta = calloc(1, sizeof(*sta)); 166 memcpy(sta->addr, addr, sizeof(sta->addr)); 167 sta->avl.key = sta->addr; 168 avl_insert(&stations, &sta->avl); 169 INIT_LIST_HEAD(&sta->nodes); 170 INIT_LIST_HEAD(&sta->measurements); 171 172 return sta; 173 } 174 175 void usteer_sta_disconnected(struct sta_info *si) 176 { 177 si->connected = STA_NOT_CONNECTED; 178 si->kick_time = 0; 179 si->connected_since = 0; 180 usteer_sta_info_update_timeout(si, config.local_sta_timeout); 181 } 182 183 void 184 usteer_sta_info_update(struct sta_info *si, int signal, bool avg) 185 { 186 /* ignore probe request signal when connected */ 187 if (si->connected == STA_CONNECTED && si->signal != NO_SIGNAL && !avg) 188 signal = NO_SIGNAL; 189 190 if (signal != NO_SIGNAL) { 191 si->signal = signal; 192 usteer_band_steering_sta_update(si); 193 } 194 195 si->seen = current_time; 196 197 if (si->node->freq < 4000) 198 si->sta->seen_2ghz = 1; 199 else 200 si->sta->seen_5ghz = 1; 201 202 usteer_sta_info_update_timeout(si, config.local_sta_timeout); 203 } 204 205 bool 206 usteer_handle_sta_event(struct usteer_node *node, const uint8_t *addr, 207 enum usteer_event_type type, int freq, int signal) 208 { 209 struct sta *sta; 210 struct sta_info *si; 211 uint32_t diff; 212 bool ret; 213 bool create; 214 215 sta = usteer_sta_get(addr, true); 216 if (!sta) 217 return -1; 218 219 si = usteer_sta_info_get(sta, node, &create); 220 usteer_sta_info_update(si, signal, false); 221 si->stats[type].requests++; 222 223 diff = si->stats[type].blocked_last_time - current_time; 224 if (diff > config.sta_block_timeout) 225 si->stats[type].blocked_cur = 0; 226 227 ret = usteer_check_request(si, type); 228 if (!ret) { 229 si->stats[type].blocked_cur++; 230 si->stats[type].blocked_total++; 231 si->stats[type].blocked_last_time = current_time; 232 } else { 233 si->stats[type].blocked_cur = 0; 234 } 235 236 if (create) 237 usteer_send_sta_update(si); 238 239 return ret; 240 } 241 242 bool 243 usteer_sta_supports_beacon_measurement_mode(struct sta_info *si, enum usteer_beacon_measurement_mode mode) 244 { 245 switch (mode) { 246 case BEACON_MEASUREMENT_PASSIVE: 247 return si->rrm & (1 << 4); 248 case BEACON_MEASUREMENT_ACTIVE: 249 return si->rrm & (1 << 5); 250 case BEACON_MEASUREMENT_TABLE: 251 return si->rrm & (1 << 6); 252 } 253 254 return false; 255 } 256 257 bool 258 usteer_sta_supports_link_measurement(struct sta_info *si) 259 { 260 return si->rrm & (1 << 0); 261 } 262 263 static void __usteer_init usteer_sta_init(void) 264 { 265 usteer_timeout_init(&tq); 266 tq.cb = usteer_sta_info_timeout; 267 } 268
This page was automatically generated by LXR 0.3.1. • OpenWrt