usteer: add support for IPv6 remote exchange
[project/usteer.git] / sta.c
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 free(sta);
39 }
40
41 static void
42 usteer_sta_info_del(struct sta_info *si)
43 {
44 struct sta *sta = si->sta;
45
46 MSG(DEBUG, "Delete station " MAC_ADDR_FMT " entry for node %s\n",
47 MAC_ADDR_DATA(sta->addr), usteer_node_name(si->node));
48
49 usteer_timeout_cancel(&tq, &si->timeout);
50 list_del(&si->list);
51 list_del(&si->node_list);
52 free(si);
53
54 if (list_empty(&sta->nodes))
55 usteer_sta_del(sta);
56 }
57
58 void
59 usteer_sta_node_cleanup(struct usteer_node *node)
60 {
61 struct sta_info *si, *tmp;
62
63 free(node->rrm_nr);
64 node->rrm_nr = NULL;
65
66 list_for_each_entry_safe(si, tmp, &node->sta_info, node_list)
67 usteer_sta_info_del(si);
68 }
69
70 static void
71 usteer_sta_info_timeout(struct usteer_timeout_queue *q, struct usteer_timeout *t)
72 {
73 struct sta_info *si = container_of(t, struct sta_info, timeout);
74
75 usteer_sta_info_del(si);
76 }
77
78 struct sta_info *
79 usteer_sta_info_get(struct sta *sta, struct usteer_node *node, bool *create)
80 {
81 struct sta_info *si;
82
83 list_for_each_entry(si, &sta->nodes, list) {
84 if (si->node != node)
85 continue;
86
87 if (create)
88 *create = false;
89
90 return si;
91 }
92
93 if (!create)
94 return NULL;
95
96 MSG(DEBUG, "Create station " MAC_ADDR_FMT " entry for node %s\n",
97 MAC_ADDR_DATA(sta->addr), usteer_node_name(node));
98
99 si = calloc(1, sizeof(*si));
100 si->node = node;
101 si->sta = sta;
102 list_add(&si->list, &sta->nodes);
103 list_add(&si->node_list, &node->sta_info);
104 si->created = current_time;
105 *create = true;
106
107 return si;
108 }
109
110
111 void
112 usteer_sta_info_update_timeout(struct sta_info *si, int timeout)
113 {
114 if (si->connected == 1)
115 usteer_timeout_cancel(&tq, &si->timeout);
116 else if (timeout > 0)
117 usteer_timeout_set(&tq, &si->timeout, timeout);
118 else
119 usteer_sta_info_del(si);
120 }
121
122 struct sta *
123 usteer_sta_get(const uint8_t *addr, bool create)
124 {
125 struct sta *sta;
126
127 sta = avl_find_element(&stations, addr, sta, avl);
128 if (sta)
129 return sta;
130
131 if (!create)
132 return NULL;
133
134 MSG(DEBUG, "Create station entry " MAC_ADDR_FMT "\n", MAC_ADDR_DATA(addr));
135 sta = calloc(1, sizeof(*sta));
136 memcpy(sta->addr, addr, sizeof(sta->addr));
137 sta->avl.key = sta->addr;
138 avl_insert(&stations, &sta->avl);
139 INIT_LIST_HEAD(&sta->nodes);
140
141 return sta;
142 }
143
144 void
145 usteer_sta_info_update(struct sta_info *si, int signal, bool avg)
146 {
147 /* ignore probe request signal when connected */
148 if (si->connected == 1 && si->signal != NO_SIGNAL && !avg)
149 signal = NO_SIGNAL;
150
151 if (signal != NO_SIGNAL)
152 si->signal = signal;
153
154 si->seen = current_time;
155 usteer_sta_info_update_timeout(si, config.local_sta_timeout);
156 }
157
158 bool
159 usteer_handle_sta_event(struct usteer_node *node, const uint8_t *addr,
160 enum usteer_event_type type, int freq, int signal)
161 {
162 struct sta *sta;
163 struct sta_info *si;
164 uint32_t diff;
165 bool ret;
166 bool create;
167
168 sta = usteer_sta_get(addr, true);
169 if (!sta)
170 return -1;
171
172 if (freq < 4000)
173 sta->seen_2ghz = 1;
174 else
175 sta->seen_5ghz = 1;
176
177 si = usteer_sta_info_get(sta, node, &create);
178 usteer_sta_info_update(si, signal, false);
179 si->roam_scan_done = current_time;
180 si->stats[type].requests++;
181
182 diff = si->stats[type].blocked_last_time - current_time;
183 if (diff > config.sta_block_timeout)
184 si->stats[type].blocked_cur = 0;
185
186 ret = usteer_check_request(si, type);
187 if (!ret) {
188 si->stats[type].blocked_cur++;
189 si->stats[type].blocked_total++;
190 si->stats[type].blocked_last_time = current_time;
191 } else {
192 si->stats[type].blocked_cur = 0;
193 }
194
195 if (create)
196 usteer_send_sta_update(si);
197
198 return ret;
199 }
200
201 static void __usteer_init usteer_sta_init(void)
202 {
203 usteer_timeout_init(&tq);
204 tq.cb = usteer_sta_info_timeout;
205 }