xref: /linux/net/mac80211/ocb.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OCB mode implementation
4  *
5  * Copyright: (c) 2014 Czech Technical University in Prague
6  *            (c) 2014 Volkswagen Group Research
7  * Copyright (C) 2022 - 2024, 2026 Intel Corporation
8  * Author:    Rostislav Lisovy <rostislav.lisovy@fel.cvut.cz>
9  * Funded by: Volkswagen Group Research
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/if_ether.h>
14 #include <linux/skbuff.h>
15 #include <linux/if_arp.h>
16 #include <linux/etherdevice.h>
17 #include <linux/rtnetlink.h>
18 #include <net/mac80211.h>
19 #include <linux/unaligned.h>
20 
21 #include "ieee80211_i.h"
22 #include "driver-ops.h"
23 #include "rate.h"
24 
25 #define IEEE80211_OCB_HOUSEKEEPING_INTERVAL		(60 * HZ)
26 #define IEEE80211_OCB_PEER_INACTIVITY_LIMIT		(240 * HZ)
27 #define IEEE80211_OCB_MAX_STA_ENTRIES			128
28 
29 /**
30  * enum ocb_deferred_task_flags - mac80211 OCB deferred tasks
31  * @OCB_WORK_HOUSEKEEPING: run the periodic OCB housekeeping tasks
32  *
33  * These flags are used in @wrkq_flags field of &struct ieee80211_if_ocb
34  */
35 enum ocb_deferred_task_flags {
36 	OCB_WORK_HOUSEKEEPING,
37 };
38 
39 void ieee80211_ocb_rx_no_sta(struct ieee80211_sub_if_data *sdata,
40 			     const u8 *bssid, const u8 *addr,
41 			     u32 supp_rates)
42 {
43 	struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
44 	struct ieee80211_local *local = sdata->local;
45 	struct ieee80211_chanctx_conf *chanctx_conf;
46 	struct ieee80211_supported_band *sband;
47 	struct sta_info *sta;
48 	int band;
49 
50 	if (!ifocb->joined)
51 		return;
52 
53 	/* XXX: Consider removing the least recently used entry and
54 	 *      allow new one to be added.
55 	 */
56 	if (local->num_sta >= IEEE80211_OCB_MAX_STA_ENTRIES) {
57 		net_info_ratelimited("%s: No room for a new OCB STA entry %pM\n",
58 				     sdata->name, addr);
59 		return;
60 	}
61 
62 	ocb_dbg(sdata, "Adding new OCB station %pM\n", addr);
63 
64 	rcu_read_lock();
65 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
66 	if (WARN_ON_ONCE(!chanctx_conf)) {
67 		rcu_read_unlock();
68 		return;
69 	}
70 	band = chanctx_conf->def.chan->band;
71 	rcu_read_unlock();
72 
73 	if (!atomic_add_unless(&ifocb->incomplete_sta_count, 1,
74 			       IEEE80211_OCB_MAX_STA_ENTRIES)) {
75 		net_info_ratelimited("%s: No room for a new OCB STA entry %pM\n",
76 				     sdata->name, addr);
77 		return;
78 	}
79 
80 	sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
81 	if (!sta) {
82 		atomic_dec(&ifocb->incomplete_sta_count);
83 		return;
84 	}
85 
86 	/* Add only mandatory rates for now */
87 	sband = local->hw.wiphy->bands[band];
88 	sta->sta.deflink.supp_rates[band] = ieee80211_mandatory_rates(sband);
89 
90 	spin_lock(&ifocb->incomplete_lock);
91 	list_add(&sta->list, &ifocb->incomplete_stations);
92 	spin_unlock(&ifocb->incomplete_lock);
93 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
94 }
95 
96 static struct sta_info *ieee80211_ocb_finish_sta(struct sta_info *sta)
97 	__acquires(RCU)
98 {
99 	struct ieee80211_sub_if_data *sdata = sta->sdata;
100 	u8 addr[ETH_ALEN];
101 
102 	memcpy(addr, sta->sta.addr, ETH_ALEN);
103 
104 	ieee80211_sta_init_nss_bw_capa(&sta->deflink,
105 				       &sdata->deflink.conf->chanreq.oper);
106 
107 	ocb_dbg(sdata, "Adding new IBSS station %pM (dev=%s)\n",
108 		addr, sdata->name);
109 
110 	sta_info_move_state(sta, IEEE80211_STA_AUTH);
111 	sta_info_move_state(sta, IEEE80211_STA_ASSOC);
112 	sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
113 
114 	rate_control_rate_init(&sta->deflink);
115 
116 	/* If it fails, maybe we raced another insertion? */
117 	if (sta_info_insert_rcu(sta))
118 		return sta_info_get(sdata, addr);
119 	return sta;
120 }
121 
122 static void ieee80211_ocb_housekeeping(struct ieee80211_sub_if_data *sdata)
123 {
124 	struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
125 
126 	ocb_dbg(sdata, "Running ocb housekeeping\n");
127 
128 	ieee80211_sta_expire(sdata, IEEE80211_OCB_PEER_INACTIVITY_LIMIT);
129 
130 	mod_timer(&ifocb->housekeeping_timer,
131 		  round_jiffies(jiffies + IEEE80211_OCB_HOUSEKEEPING_INTERVAL));
132 }
133 
134 void ieee80211_ocb_work(struct ieee80211_sub_if_data *sdata)
135 {
136 	struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
137 	struct sta_info *sta;
138 
139 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
140 
141 	if (ifocb->joined != true)
142 		return;
143 
144 	spin_lock_bh(&ifocb->incomplete_lock);
145 	while (!list_empty(&ifocb->incomplete_stations)) {
146 		sta = list_first_entry(&ifocb->incomplete_stations,
147 				       struct sta_info, list);
148 		list_del(&sta->list);
149 		spin_unlock_bh(&ifocb->incomplete_lock);
150 
151 		if (sdata->local->num_sta >= IEEE80211_OCB_MAX_STA_ENTRIES) {
152 			sta_info_free(sdata->local, sta);
153 		} else {
154 			ieee80211_ocb_finish_sta(sta);
155 			rcu_read_unlock();
156 		}
157 		atomic_dec(&ifocb->incomplete_sta_count);
158 		spin_lock_bh(&ifocb->incomplete_lock);
159 	}
160 	spin_unlock_bh(&ifocb->incomplete_lock);
161 
162 	if (test_and_clear_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags))
163 		ieee80211_ocb_housekeeping(sdata);
164 }
165 
166 static void ieee80211_ocb_housekeeping_timer(struct timer_list *t)
167 {
168 	struct ieee80211_sub_if_data *sdata =
169 		timer_container_of(sdata, t, u.ocb.housekeeping_timer);
170 	struct ieee80211_local *local = sdata->local;
171 	struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
172 
173 	set_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags);
174 
175 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
176 }
177 
178 void ieee80211_ocb_setup_sdata(struct ieee80211_sub_if_data *sdata)
179 {
180 	struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
181 
182 	timer_setup(&ifocb->housekeeping_timer,
183 		    ieee80211_ocb_housekeeping_timer, 0);
184 	INIT_LIST_HEAD(&ifocb->incomplete_stations);
185 	spin_lock_init(&ifocb->incomplete_lock);
186 }
187 
188 int ieee80211_ocb_join(struct ieee80211_sub_if_data *sdata,
189 		       struct ocb_setup *setup)
190 {
191 	struct ieee80211_chan_req chanreq = { .oper = setup->chandef };
192 	struct ieee80211_local *local = sdata->local;
193 	struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
194 	u64 changed = BSS_CHANGED_OCB | BSS_CHANGED_BSSID;
195 	int err;
196 
197 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
198 
199 	if (ifocb->joined == true)
200 		return -EINVAL;
201 
202 	sdata->deflink.operating_11g_mode = true;
203 	sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
204 	sdata->deflink.needed_rx_chains = sdata->local->rx_chains;
205 
206 	err = ieee80211_link_use_channel(&sdata->deflink, &chanreq,
207 					 IEEE80211_CHANCTX_SHARED);
208 	if (err)
209 		return err;
210 
211 	ieee80211_bss_info_change_notify(sdata, changed);
212 
213 	ifocb->joined = true;
214 
215 	set_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags);
216 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
217 
218 	netif_carrier_on(sdata->dev);
219 	return 0;
220 }
221 
222 int ieee80211_ocb_leave(struct ieee80211_sub_if_data *sdata)
223 {
224 	struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
225 	struct ieee80211_local *local = sdata->local;
226 	struct sta_info *sta;
227 
228 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
229 
230 	ifocb->joined = false;
231 	sta_info_flush(sdata, -1);
232 
233 	spin_lock_bh(&ifocb->incomplete_lock);
234 	while (!list_empty(&ifocb->incomplete_stations)) {
235 		sta = list_first_entry(&ifocb->incomplete_stations,
236 				       struct sta_info, list);
237 		list_del(&sta->list);
238 		spin_unlock_bh(&ifocb->incomplete_lock);
239 
240 		sta_info_free(local, sta);
241 		atomic_dec(&ifocb->incomplete_sta_count);
242 		spin_lock_bh(&ifocb->incomplete_lock);
243 	}
244 	spin_unlock_bh(&ifocb->incomplete_lock);
245 
246 	netif_carrier_off(sdata->dev);
247 	clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
248 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_OCB);
249 
250 	ieee80211_link_release_channel(&sdata->deflink);
251 
252 	skb_queue_purge(&sdata->skb_queue);
253 
254 	timer_delete_sync(&sdata->u.ocb.housekeeping_timer);
255 	/* If the timer fired while we waited for it, it will have
256 	 * requeued the work. Now the work will be running again
257 	 * but will not rearm the timer again because it checks
258 	 * whether we are connected to the network or not -- at this
259 	 * point we shouldn't be anymore.
260 	 */
261 
262 	return 0;
263 }
264