Commit | Line | Data |
---|---|---|
f0706e82 | 1 | /* |
c5d54fbf | 2 | * Interface handling |
0d143fe1 | 3 | * |
f0706e82 JB |
4 | * Copyright 2002-2005, Instant802 Networks, Inc. |
5 | * Copyright 2005-2006, Devicescape Software, Inc. | |
6 | * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz> | |
75636525 | 7 | * Copyright 2008, Johannes Berg <johannes@sipsolutions.net> |
f0706e82 JB |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
f0706e82 JB |
14 | #include <linux/kernel.h> |
15 | #include <linux/if_arp.h> | |
16 | #include <linux/netdevice.h> | |
17 | #include <linux/rtnetlink.h> | |
18 | #include <net/mac80211.h> | |
cf0277e7 | 19 | #include <net/ieee80211_radiotap.h> |
f0706e82 JB |
20 | #include "ieee80211_i.h" |
21 | #include "sta_info.h" | |
e9f207f0 | 22 | #include "debugfs_netdev.h" |
ee385855 | 23 | #include "mesh.h" |
0d143fe1 | 24 | #include "led.h" |
24487981 | 25 | #include "driver-ops.h" |
cf0277e7 | 26 | #include "wme.h" |
1be7fe8d | 27 | #include "rate.h" |
0d143fe1 | 28 | |
c771c9d8 JB |
29 | /** |
30 | * DOC: Interface list locking | |
31 | * | |
32 | * The interface list in each struct ieee80211_local is protected | |
33 | * three-fold: | |
34 | * | |
35 | * (1) modifications may only be done under the RTNL | |
36 | * (2) modifications and readers are protected against each other by | |
37 | * the iflist_mtx. | |
38 | * (3) modifications are done in an RCU manner so atomic readers | |
39 | * can traverse the list in RCU-safe blocks. | |
40 | * | |
41 | * As a consequence, reads (traversals) of the list can be protected | |
42 | * by either the RTNL, the iflist_mtx or RCU. | |
43 | */ | |
44 | ||
1ea6f9c0 JB |
45 | bool __ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata) |
46 | { | |
47 | struct ieee80211_chanctx_conf *chanctx_conf; | |
48 | int power; | |
49 | ||
50 | rcu_read_lock(); | |
51 | chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); | |
52 | if (!chanctx_conf) { | |
53 | rcu_read_unlock(); | |
54 | return false; | |
55 | } | |
56 | ||
4bf88530 | 57 | power = chanctx_conf->def.chan->max_power; |
1ea6f9c0 JB |
58 | rcu_read_unlock(); |
59 | ||
60 | if (sdata->user_power_level != IEEE80211_UNSET_POWER_LEVEL) | |
61 | power = min(power, sdata->user_power_level); | |
62 | ||
63 | if (sdata->ap_power_level != IEEE80211_UNSET_POWER_LEVEL) | |
64 | power = min(power, sdata->ap_power_level); | |
65 | ||
66 | if (power != sdata->vif.bss_conf.txpower) { | |
67 | sdata->vif.bss_conf.txpower = power; | |
68 | ieee80211_hw_config(sdata->local, 0); | |
69 | return true; | |
70 | } | |
71 | ||
72 | return false; | |
73 | } | |
74 | ||
75 | void ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata) | |
76 | { | |
77 | if (__ieee80211_recalc_txpower(sdata)) | |
78 | ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_TXPOWER); | |
79 | } | |
c771c9d8 | 80 | |
382a103b | 81 | u32 ieee80211_idle_off(struct ieee80211_local *local) |
cc45ae54 JB |
82 | { |
83 | if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE)) | |
84 | return 0; | |
85 | ||
86 | local->hw.conf.flags &= ~IEEE80211_CONF_IDLE; | |
87 | return IEEE80211_CONF_CHANGE_IDLE; | |
88 | } | |
89 | ||
90 | static u32 ieee80211_idle_on(struct ieee80211_local *local) | |
91 | { | |
92 | if (local->hw.conf.flags & IEEE80211_CONF_IDLE) | |
93 | return 0; | |
94 | ||
39ecc01d | 95 | ieee80211_flush_queues(local, NULL); |
cc45ae54 JB |
96 | |
97 | local->hw.conf.flags |= IEEE80211_CONF_IDLE; | |
98 | return IEEE80211_CONF_CHANGE_IDLE; | |
99 | } | |
100 | ||
fd0f979a | 101 | void ieee80211_recalc_idle(struct ieee80211_local *local) |
cc45ae54 | 102 | { |
fd0f979a | 103 | bool working = false, scanning, active; |
cc45ae54 JB |
104 | unsigned int led_trig_start = 0, led_trig_stop = 0; |
105 | struct ieee80211_roc_work *roc; | |
fd0f979a | 106 | u32 change; |
cc45ae54 | 107 | |
cc45ae54 JB |
108 | lockdep_assert_held(&local->mtx); |
109 | ||
b759f4dd | 110 | active = !list_empty(&local->chanctx_list) || local->monitors; |
cc45ae54 JB |
111 | |
112 | if (!local->ops->remain_on_channel) { | |
113 | list_for_each_entry(roc, &local->roc_list, list) { | |
114 | working = true; | |
fd0f979a | 115 | break; |
cc45ae54 JB |
116 | } |
117 | } | |
118 | ||
f1e3e051 JB |
119 | scanning = test_bit(SCAN_SW_SCANNING, &local->scanning) || |
120 | test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); | |
cc45ae54 | 121 | |
cc45ae54 JB |
122 | if (working || scanning) |
123 | led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK; | |
124 | else | |
125 | led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK; | |
126 | ||
fd0f979a | 127 | if (active) |
cc45ae54 JB |
128 | led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED; |
129 | else | |
130 | led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED; | |
131 | ||
132 | ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop); | |
133 | ||
fd0f979a JB |
134 | if (working || scanning || active) |
135 | change = ieee80211_idle_off(local); | |
cc45ae54 | 136 | else |
fd0f979a JB |
137 | change = ieee80211_idle_on(local); |
138 | if (change) | |
139 | ieee80211_hw_config(local, change); | |
cc45ae54 JB |
140 | } |
141 | ||
0d143fe1 JB |
142 | static int ieee80211_change_mtu(struct net_device *dev, int new_mtu) |
143 | { | |
09b1426e | 144 | if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) |
0d143fe1 | 145 | return -EINVAL; |
0d143fe1 | 146 | |
0d143fe1 JB |
147 | dev->mtu = new_mtu; |
148 | return 0; | |
149 | } | |
150 | ||
478622e8 HS |
151 | static int ieee80211_verify_mac(struct ieee80211_local *local, u8 *addr) |
152 | { | |
153 | struct ieee80211_sub_if_data *sdata; | |
154 | u64 new, mask, tmp; | |
155 | u8 *m; | |
156 | int ret = 0; | |
157 | ||
158 | if (is_zero_ether_addr(local->hw.wiphy->addr_mask)) | |
159 | return 0; | |
160 | ||
161 | m = addr; | |
162 | new = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | | |
163 | ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | | |
164 | ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); | |
165 | ||
166 | m = local->hw.wiphy->addr_mask; | |
167 | mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | | |
168 | ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | | |
169 | ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); | |
170 | ||
171 | ||
172 | mutex_lock(&local->iflist_mtx); | |
173 | list_for_each_entry(sdata, &local->interfaces, list) { | |
174 | if (sdata->vif.type == NL80211_IFTYPE_MONITOR) | |
175 | continue; | |
176 | ||
177 | m = sdata->vif.addr; | |
178 | tmp = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | | |
179 | ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | | |
180 | ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); | |
181 | ||
182 | if ((new & ~mask) != (tmp & ~mask)) { | |
183 | ret = -EINVAL; | |
184 | break; | |
185 | } | |
186 | } | |
187 | mutex_unlock(&local->iflist_mtx); | |
188 | ||
189 | return ret; | |
190 | } | |
191 | ||
47846c9b JB |
192 | static int ieee80211_change_mac(struct net_device *dev, void *addr) |
193 | { | |
194 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
fc5f7577 | 195 | struct sockaddr *sa = addr; |
47846c9b JB |
196 | int ret; |
197 | ||
9607e6b6 | 198 | if (ieee80211_sdata_running(sdata)) |
47846c9b JB |
199 | return -EBUSY; |
200 | ||
478622e8 HS |
201 | ret = ieee80211_verify_mac(sdata->local, sa->sa_data); |
202 | if (ret) | |
203 | return ret; | |
204 | ||
fc5f7577 | 205 | ret = eth_mac_addr(dev, sa); |
47846c9b JB |
206 | |
207 | if (ret == 0) | |
fc5f7577 | 208 | memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN); |
47846c9b JB |
209 | |
210 | return ret; | |
211 | } | |
212 | ||
0d143fe1 JB |
213 | static inline int identical_mac_addr_allowed(int type1, int type2) |
214 | { | |
215 | return type1 == NL80211_IFTYPE_MONITOR || | |
216 | type2 == NL80211_IFTYPE_MONITOR || | |
f142c6b9 JB |
217 | type1 == NL80211_IFTYPE_P2P_DEVICE || |
218 | type2 == NL80211_IFTYPE_P2P_DEVICE || | |
0d143fe1 JB |
219 | (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) || |
220 | (type1 == NL80211_IFTYPE_WDS && | |
221 | (type2 == NL80211_IFTYPE_WDS || | |
222 | type2 == NL80211_IFTYPE_AP)) || | |
223 | (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) || | |
224 | (type1 == NL80211_IFTYPE_AP_VLAN && | |
225 | (type2 == NL80211_IFTYPE_AP || | |
226 | type2 == NL80211_IFTYPE_AP_VLAN)); | |
227 | } | |
228 | ||
87490f6d JB |
229 | static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata, |
230 | enum nl80211_iftype iftype) | |
0d143fe1 | 231 | { |
b4a4bf5d | 232 | struct ieee80211_local *local = sdata->local; |
87490f6d | 233 | struct ieee80211_sub_if_data *nsdata; |
0d143fe1 | 234 | |
87490f6d | 235 | ASSERT_RTNL(); |
0d143fe1 JB |
236 | |
237 | /* we hold the RTNL here so can safely walk the list */ | |
238 | list_for_each_entry(nsdata, &local->interfaces, list) { | |
371a255e | 239 | if (nsdata != sdata && ieee80211_sdata_running(nsdata)) { |
0d143fe1 JB |
240 | /* |
241 | * Allow only a single IBSS interface to be up at any | |
242 | * time. This is restricted because beacon distribution | |
243 | * cannot work properly if both are in the same IBSS. | |
244 | * | |
245 | * To remove this restriction we'd have to disallow them | |
246 | * from setting the same SSID on different IBSS interfaces | |
247 | * belonging to the same hardware. Then, however, we're | |
248 | * faced with having to adopt two different TSF timers... | |
249 | */ | |
87490f6d | 250 | if (iftype == NL80211_IFTYPE_ADHOC && |
0d143fe1 JB |
251 | nsdata->vif.type == NL80211_IFTYPE_ADHOC) |
252 | return -EBUSY; | |
253 | ||
254 | /* | |
255 | * The remaining checks are only performed for interfaces | |
256 | * with the same MAC address. | |
257 | */ | |
371a255e JB |
258 | if (!ether_addr_equal(sdata->vif.addr, |
259 | nsdata->vif.addr)) | |
0d143fe1 JB |
260 | continue; |
261 | ||
262 | /* | |
263 | * check whether it may have the same address | |
264 | */ | |
87490f6d | 265 | if (!identical_mac_addr_allowed(iftype, |
0d143fe1 JB |
266 | nsdata->vif.type)) |
267 | return -ENOTUNIQ; | |
268 | ||
269 | /* | |
270 | * can only add VLANs to enabled APs | |
271 | */ | |
87490f6d | 272 | if (iftype == NL80211_IFTYPE_AP_VLAN && |
0d143fe1 JB |
273 | nsdata->vif.type == NL80211_IFTYPE_AP) |
274 | sdata->bss = &nsdata->u.ap; | |
275 | } | |
276 | } | |
277 | ||
87490f6d JB |
278 | return 0; |
279 | } | |
280 | ||
3a25a8c8 JB |
281 | static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata) |
282 | { | |
283 | int n_queues = sdata->local->hw.queues; | |
284 | int i; | |
285 | ||
0ef24e52 IP |
286 | if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) { |
287 | for (i = 0; i < IEEE80211_NUM_ACS; i++) { | |
288 | if (WARN_ON_ONCE(sdata->vif.hw_queue[i] == | |
289 | IEEE80211_INVAL_HW_QUEUE)) | |
290 | return -EINVAL; | |
291 | if (WARN_ON_ONCE(sdata->vif.hw_queue[i] >= | |
292 | n_queues)) | |
293 | return -EINVAL; | |
294 | } | |
3a25a8c8 JB |
295 | } |
296 | ||
8ffb5c00 BC |
297 | if ((sdata->vif.type != NL80211_IFTYPE_AP && |
298 | sdata->vif.type != NL80211_IFTYPE_MESH_POINT) || | |
bb3e10fb | 299 | !(sdata->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)) { |
3a25a8c8 JB |
300 | sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE; |
301 | return 0; | |
302 | } | |
303 | ||
304 | if (WARN_ON_ONCE(sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE)) | |
305 | return -EINVAL; | |
306 | ||
307 | if (WARN_ON_ONCE(sdata->vif.cab_queue >= n_queues)) | |
308 | return -EINVAL; | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
85416a4f CL |
313 | void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata, |
314 | const int offset) | |
315 | { | |
316 | struct ieee80211_local *local = sdata->local; | |
317 | u32 flags = sdata->u.mntr_flags; | |
318 | ||
319 | #define ADJUST(_f, _s) do { \ | |
320 | if (flags & MONITOR_FLAG_##_f) \ | |
321 | local->fif_##_s += offset; \ | |
322 | } while (0) | |
323 | ||
324 | ADJUST(FCSFAIL, fcsfail); | |
325 | ADJUST(PLCPFAIL, plcpfail); | |
326 | ADJUST(CONTROL, control); | |
327 | ADJUST(CONTROL, pspoll); | |
328 | ADJUST(OTHER_BSS, other_bss); | |
329 | ||
330 | #undef ADJUST | |
331 | } | |
332 | ||
3a25a8c8 JB |
333 | static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata) |
334 | { | |
335 | struct ieee80211_local *local = sdata->local; | |
336 | int i; | |
337 | ||
338 | for (i = 0; i < IEEE80211_NUM_ACS; i++) { | |
339 | if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) | |
340 | sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE; | |
a9d3c05c | 341 | else if (local->hw.queues >= IEEE80211_NUM_ACS) |
3a25a8c8 | 342 | sdata->vif.hw_queue[i] = i; |
a9d3c05c JB |
343 | else |
344 | sdata->vif.hw_queue[i] = 0; | |
3a25a8c8 JB |
345 | } |
346 | sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE; | |
347 | } | |
348 | ||
3c3e21e7 | 349 | int ieee80211_add_virtual_monitor(struct ieee80211_local *local) |
4b6f1dd6 JB |
350 | { |
351 | struct ieee80211_sub_if_data *sdata; | |
8b305780 | 352 | int ret; |
4b6f1dd6 JB |
353 | |
354 | if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF)) | |
355 | return 0; | |
356 | ||
8b305780 | 357 | ASSERT_RTNL(); |
685fb72b | 358 | |
4b6f1dd6 | 359 | if (local->monitor_sdata) |
8b305780 | 360 | return 0; |
4b6f1dd6 JB |
361 | |
362 | sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL); | |
8b305780 JB |
363 | if (!sdata) |
364 | return -ENOMEM; | |
4b6f1dd6 JB |
365 | |
366 | /* set up data */ | |
367 | sdata->local = local; | |
368 | sdata->vif.type = NL80211_IFTYPE_MONITOR; | |
369 | snprintf(sdata->name, IFNAMSIZ, "%s-monitor", | |
370 | wiphy_name(local->hw.wiphy)); | |
371 | ||
3a25a8c8 JB |
372 | ieee80211_set_default_queues(sdata); |
373 | ||
4b6f1dd6 JB |
374 | ret = drv_add_interface(local, sdata); |
375 | if (WARN_ON(ret)) { | |
376 | /* ok .. stupid driver, it asked for this! */ | |
377 | kfree(sdata); | |
8b305780 | 378 | return ret; |
4b6f1dd6 JB |
379 | } |
380 | ||
3a25a8c8 JB |
381 | ret = ieee80211_check_queues(sdata); |
382 | if (ret) { | |
383 | kfree(sdata); | |
8b305780 | 384 | return ret; |
3a25a8c8 JB |
385 | } |
386 | ||
4bf88530 | 387 | ret = ieee80211_vif_use_channel(sdata, &local->monitor_chandef, |
55de908a JB |
388 | IEEE80211_CHANCTX_EXCLUSIVE); |
389 | if (ret) { | |
390 | drv_remove_interface(local, sdata); | |
391 | kfree(sdata); | |
8b305780 | 392 | return ret; |
55de908a JB |
393 | } |
394 | ||
8b305780 | 395 | mutex_lock(&local->iflist_mtx); |
4b6f1dd6 | 396 | rcu_assign_pointer(local->monitor_sdata, sdata); |
685fb72b | 397 | mutex_unlock(&local->iflist_mtx); |
8b305780 JB |
398 | |
399 | return 0; | |
4b6f1dd6 JB |
400 | } |
401 | ||
3c3e21e7 | 402 | void ieee80211_del_virtual_monitor(struct ieee80211_local *local) |
4b6f1dd6 JB |
403 | { |
404 | struct ieee80211_sub_if_data *sdata; | |
405 | ||
406 | if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF)) | |
407 | return; | |
408 | ||
8b305780 JB |
409 | ASSERT_RTNL(); |
410 | ||
685fb72b | 411 | mutex_lock(&local->iflist_mtx); |
4b6f1dd6 | 412 | |
685fb72b JB |
413 | sdata = rcu_dereference_protected(local->monitor_sdata, |
414 | lockdep_is_held(&local->iflist_mtx)); | |
8b305780 JB |
415 | if (!sdata) { |
416 | mutex_unlock(&local->iflist_mtx); | |
417 | return; | |
418 | } | |
4b6f1dd6 JB |
419 | |
420 | rcu_assign_pointer(local->monitor_sdata, NULL); | |
8b305780 JB |
421 | mutex_unlock(&local->iflist_mtx); |
422 | ||
4b6f1dd6 JB |
423 | synchronize_net(); |
424 | ||
55de908a JB |
425 | ieee80211_vif_release_channel(sdata); |
426 | ||
4b6f1dd6 JB |
427 | drv_remove_interface(local, sdata); |
428 | ||
429 | kfree(sdata); | |
430 | } | |
431 | ||
34d4bc4d JB |
432 | /* |
433 | * NOTE: Be very careful when changing this function, it must NOT return | |
434 | * an error on interface type changes that have been pre-checked, so most | |
435 | * checks should be in ieee80211_check_concurrent_iface. | |
436 | */ | |
f142c6b9 | 437 | int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up) |
87490f6d | 438 | { |
f142c6b9 JB |
439 | struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev); |
440 | struct net_device *dev = wdev->netdev; | |
87490f6d JB |
441 | struct ieee80211_local *local = sdata->local; |
442 | struct sta_info *sta; | |
443 | u32 changed = 0; | |
444 | int res; | |
445 | u32 hw_reconf_flags = 0; | |
446 | ||
0d143fe1 JB |
447 | switch (sdata->vif.type) { |
448 | case NL80211_IFTYPE_WDS: | |
449 | if (!is_valid_ether_addr(sdata->u.wds.remote_addr)) | |
450 | return -ENOLINK; | |
451 | break; | |
665c93a9 JB |
452 | case NL80211_IFTYPE_AP_VLAN: { |
453 | struct ieee80211_sub_if_data *master; | |
454 | ||
0d143fe1 JB |
455 | if (!sdata->bss) |
456 | return -ENOLINK; | |
665c93a9 | 457 | |
0d143fe1 | 458 | list_add(&sdata->u.vlan.list, &sdata->bss->vlans); |
665c93a9 JB |
459 | |
460 | master = container_of(sdata->bss, | |
461 | struct ieee80211_sub_if_data, u.ap); | |
462 | sdata->control_port_protocol = | |
463 | master->control_port_protocol; | |
464 | sdata->control_port_no_encrypt = | |
465 | master->control_port_no_encrypt; | |
0d143fe1 | 466 | break; |
665c93a9 | 467 | } |
0d143fe1 JB |
468 | case NL80211_IFTYPE_AP: |
469 | sdata->bss = &sdata->u.ap; | |
470 | break; | |
471 | case NL80211_IFTYPE_MESH_POINT: | |
0d143fe1 JB |
472 | case NL80211_IFTYPE_STATION: |
473 | case NL80211_IFTYPE_MONITOR: | |
474 | case NL80211_IFTYPE_ADHOC: | |
f142c6b9 | 475 | case NL80211_IFTYPE_P2P_DEVICE: |
0d143fe1 JB |
476 | /* no special treatment */ |
477 | break; | |
478 | case NL80211_IFTYPE_UNSPECIFIED: | |
2e161f78 | 479 | case NUM_NL80211_IFTYPES: |
2ca27bcf JB |
480 | case NL80211_IFTYPE_P2P_CLIENT: |
481 | case NL80211_IFTYPE_P2P_GO: | |
0d143fe1 JB |
482 | /* cannot happen */ |
483 | WARN_ON(1); | |
484 | break; | |
485 | } | |
486 | ||
487 | if (local->open_count == 0) { | |
24487981 | 488 | res = drv_start(local); |
0d143fe1 JB |
489 | if (res) |
490 | goto err_del_bss; | |
e8975581 JB |
491 | /* we're brought up, everything changes */ |
492 | hw_reconf_flags = ~0; | |
1f87f7d3 | 493 | ieee80211_led_radio(local, true); |
67408c8c JB |
494 | ieee80211_mod_tpt_led_trig(local, |
495 | IEEE80211_TPT_LEDTRIG_FL_RADIO, 0); | |
0d143fe1 JB |
496 | } |
497 | ||
498 | /* | |
bf533e0b JB |
499 | * Copy the hopefully now-present MAC address to |
500 | * this interface, if it has the special null one. | |
0d143fe1 | 501 | */ |
f142c6b9 | 502 | if (dev && is_zero_ether_addr(dev->dev_addr)) { |
bf533e0b JB |
503 | memcpy(dev->dev_addr, |
504 | local->hw.wiphy->perm_addr, | |
505 | ETH_ALEN); | |
506 | memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN); | |
507 | ||
508 | if (!is_valid_ether_addr(dev->dev_addr)) { | |
4d6c36fa JB |
509 | res = -EADDRNOTAVAIL; |
510 | goto err_stop; | |
0adc23f5 | 511 | } |
0d143fe1 JB |
512 | } |
513 | ||
0d143fe1 JB |
514 | switch (sdata->vif.type) { |
515 | case NL80211_IFTYPE_AP_VLAN: | |
4d76d21b JB |
516 | /* no need to tell driver, but set carrier and chanctx */ |
517 | if (rtnl_dereference(sdata->bss->beacon)) { | |
518 | ieee80211_vif_vlan_copy_chanctx(sdata); | |
3edaf3e6 | 519 | netif_carrier_on(dev); |
4d76d21b | 520 | } else { |
3edaf3e6 | 521 | netif_carrier_off(dev); |
4d76d21b | 522 | } |
0d143fe1 JB |
523 | break; |
524 | case NL80211_IFTYPE_MONITOR: | |
525 | if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) { | |
526 | local->cooked_mntrs++; | |
527 | break; | |
528 | } | |
529 | ||
075e0847 JB |
530 | if (local->monitors == 0 && local->open_count == 0) { |
531 | res = ieee80211_add_virtual_monitor(local); | |
532 | if (res) | |
533 | goto err_stop; | |
534 | } | |
535 | ||
0d143fe1 JB |
536 | /* must be before the call to ieee80211_configure_filter */ |
537 | local->monitors++; | |
e8975581 | 538 | if (local->monitors == 1) { |
0869aea0 JB |
539 | local->hw.conf.flags |= IEEE80211_CONF_MONITOR; |
540 | hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR; | |
e8975581 | 541 | } |
0d143fe1 | 542 | |
85416a4f | 543 | ieee80211_adjust_monitor_flags(sdata, 1); |
0d143fe1 | 544 | ieee80211_configure_filter(local); |
801d929c FF |
545 | mutex_lock(&local->mtx); |
546 | ieee80211_recalc_idle(local); | |
547 | mutex_unlock(&local->mtx); | |
53e9b1de DG |
548 | |
549 | netif_carrier_on(dev); | |
0d143fe1 | 550 | break; |
0d143fe1 | 551 | default: |
34d4bc4d | 552 | if (coming_up) { |
075e0847 JB |
553 | ieee80211_del_virtual_monitor(local); |
554 | ||
7b7eab6f | 555 | res = drv_add_interface(local, sdata); |
34d4bc4d JB |
556 | if (res) |
557 | goto err_stop; | |
3a25a8c8 JB |
558 | res = ieee80211_check_queues(sdata); |
559 | if (res) | |
560 | goto err_del_interface; | |
34d4bc4d | 561 | } |
0d143fe1 | 562 | |
29cbe68c | 563 | if (sdata->vif.type == NL80211_IFTYPE_AP) { |
e3b90ca2 | 564 | local->fif_pspoll++; |
7be5086d | 565 | local->fif_probe_req++; |
e3b90ca2 | 566 | |
e3b90ca2 | 567 | ieee80211_configure_filter(local); |
7be5086d JB |
568 | } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { |
569 | local->fif_probe_req++; | |
a3c9aa51 | 570 | } |
e3b90ca2 | 571 | |
f142c6b9 JB |
572 | if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) |
573 | changed |= ieee80211_reset_erp_info(sdata); | |
0d143fe1 | 574 | ieee80211_bss_info_change_notify(sdata, changed); |
0d143fe1 | 575 | |
c405c629 JB |
576 | switch (sdata->vif.type) { |
577 | case NL80211_IFTYPE_STATION: | |
578 | case NL80211_IFTYPE_ADHOC: | |
579 | case NL80211_IFTYPE_AP: | |
580 | case NL80211_IFTYPE_MESH_POINT: | |
0d143fe1 | 581 | netif_carrier_off(dev); |
c405c629 | 582 | break; |
1411af15 | 583 | case NL80211_IFTYPE_WDS: |
f142c6b9 | 584 | case NL80211_IFTYPE_P2P_DEVICE: |
1411af15 | 585 | break; |
c405c629 | 586 | default: |
a2310824 JB |
587 | /* not reached */ |
588 | WARN_ON(1); | |
c405c629 | 589 | } |
59034591 EP |
590 | |
591 | /* | |
592 | * set default queue parameters so drivers don't | |
593 | * need to initialise the hardware if the hardware | |
594 | * doesn't start up with sane defaults | |
595 | */ | |
3abead59 | 596 | ieee80211_set_wmm_default(sdata, true); |
0d143fe1 JB |
597 | } |
598 | ||
2d2080c3 JB |
599 | set_bit(SDATA_STATE_RUNNING, &sdata->state); |
600 | ||
0d143fe1 JB |
601 | if (sdata->vif.type == NL80211_IFTYPE_WDS) { |
602 | /* Create STA entry for the WDS peer */ | |
603 | sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr, | |
604 | GFP_KERNEL); | |
605 | if (!sta) { | |
606 | res = -ENOMEM; | |
607 | goto err_del_interface; | |
608 | } | |
609 | ||
83d5cc01 JB |
610 | sta_info_pre_move_state(sta, IEEE80211_STA_AUTH); |
611 | sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC); | |
612 | sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED); | |
0d143fe1 JB |
613 | |
614 | res = sta_info_insert(sta); | |
615 | if (res) { | |
616 | /* STA has been freed */ | |
617 | goto err_del_interface; | |
618 | } | |
1be7fe8d BJ |
619 | |
620 | rate_control_rate_init(sta); | |
1411af15 | 621 | netif_carrier_on(dev); |
f142c6b9 JB |
622 | } else if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) { |
623 | rcu_assign_pointer(local->p2p_sdata, sdata); | |
0d143fe1 JB |
624 | } |
625 | ||
0d143fe1 JB |
626 | /* |
627 | * set_multicast_list will be invoked by the networking core | |
628 | * which will check whether any increments here were done in | |
629 | * error and sync them down to the hardware as filter flags. | |
630 | */ | |
631 | if (sdata->flags & IEEE80211_SDATA_ALLMULTI) | |
632 | atomic_inc(&local->iff_allmultis); | |
633 | ||
634 | if (sdata->flags & IEEE80211_SDATA_PROMISC) | |
635 | atomic_inc(&local->iff_promiscs); | |
636 | ||
34d4bc4d JB |
637 | if (coming_up) |
638 | local->open_count++; | |
639 | ||
59034591 | 640 | if (hw_reconf_flags) |
e8975581 | 641 | ieee80211_hw_config(local, hw_reconf_flags); |
0d143fe1 | 642 | |
10f644a4 | 643 | ieee80211_recalc_ps(local, -1); |
965bedad | 644 | |
2b730daa JB |
645 | if (dev) { |
646 | unsigned long flags; | |
647 | int n_acs = IEEE80211_NUM_ACS; | |
648 | int ac; | |
649 | ||
650 | if (local->hw.queues < IEEE80211_NUM_ACS) | |
651 | n_acs = 1; | |
652 | ||
653 | spin_lock_irqsave(&local->queue_stop_reason_lock, flags); | |
654 | if (sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE || | |
655 | (local->queue_stop_reasons[sdata->vif.cab_queue] == 0 && | |
656 | skb_queue_empty(&local->pending[sdata->vif.cab_queue]))) { | |
657 | for (ac = 0; ac < n_acs; ac++) { | |
658 | int ac_queue = sdata->vif.hw_queue[ac]; | |
659 | ||
660 | if (local->queue_stop_reasons[ac_queue] == 0 && | |
661 | skb_queue_empty(&local->pending[ac_queue])) | |
662 | netif_start_subqueue(dev, ac); | |
663 | } | |
664 | } | |
665 | spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); | |
666 | } | |
0d143fe1 JB |
667 | |
668 | return 0; | |
669 | err_del_interface: | |
7b7eab6f | 670 | drv_remove_interface(local, sdata); |
0d143fe1 | 671 | err_stop: |
24487981 JB |
672 | if (!local->open_count) |
673 | drv_stop(local); | |
0d143fe1 JB |
674 | err_del_bss: |
675 | sdata->bss = NULL; | |
676 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) | |
677 | list_del(&sdata->u.vlan.list); | |
4d6c36fa | 678 | /* might already be clear but that doesn't matter */ |
2d2080c3 | 679 | clear_bit(SDATA_STATE_RUNNING, &sdata->state); |
0d143fe1 JB |
680 | return res; |
681 | } | |
682 | ||
34d4bc4d | 683 | static int ieee80211_open(struct net_device *dev) |
0d143fe1 JB |
684 | { |
685 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
34d4bc4d JB |
686 | int err; |
687 | ||
688 | /* fail early if user set an invalid address */ | |
2fcf2824 | 689 | if (!is_valid_ether_addr(dev->dev_addr)) |
34d4bc4d JB |
690 | return -EADDRNOTAVAIL; |
691 | ||
692 | err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type); | |
693 | if (err) | |
694 | return err; | |
695 | ||
f142c6b9 | 696 | return ieee80211_do_open(&sdata->wdev, true); |
34d4bc4d JB |
697 | } |
698 | ||
699 | static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, | |
700 | bool going_down) | |
701 | { | |
0d143fe1 | 702 | struct ieee80211_local *local = sdata->local; |
5061b0c2 JB |
703 | unsigned long flags; |
704 | struct sk_buff *skb, *tmp; | |
e8975581 | 705 | u32 hw_reconf_flags = 0; |
09f4114e | 706 | int i, flushed; |
397a7a24 | 707 | struct ps_data *ps; |
0d143fe1 | 708 | |
c29acf20 RM |
709 | clear_bit(SDATA_STATE_RUNNING, &sdata->state); |
710 | ||
e2fd5dbc | 711 | if (rcu_access_pointer(local->scan_sdata) == sdata) |
352ffad6 BC |
712 | ieee80211_scan_cancel(local); |
713 | ||
0d143fe1 JB |
714 | /* |
715 | * Stop TX on this interface first. | |
716 | */ | |
f142c6b9 JB |
717 | if (sdata->dev) |
718 | netif_tx_stop_all_queues(sdata->dev); | |
0d143fe1 | 719 | |
c8f994ee | 720 | ieee80211_roc_purge(local, sdata); |
af6b6374 | 721 | |
572078be JB |
722 | if (sdata->vif.type == NL80211_IFTYPE_STATION) |
723 | ieee80211_mgd_stop(sdata); | |
724 | ||
0d143fe1 JB |
725 | /* |
726 | * Remove all stations associated with this interface. | |
727 | * | |
728 | * This must be done before calling ops->remove_interface() | |
729 | * because otherwise we can later invoke ops->sta_notify() | |
730 | * whenever the STAs are removed, and that invalidates driver | |
731 | * assumptions about always getting a vif pointer that is valid | |
732 | * (because if we remove a STA after ops->remove_interface() | |
733 | * the driver will have removed the vif info already!) | |
734 | * | |
09f4114e JB |
735 | * This is relevant only in WDS mode, in all other modes we've |
736 | * already removed all stations when disconnecting or similar, | |
737 | * so warn otherwise. | |
051007d9 JB |
738 | * |
739 | * We call sta_info_flush_cleanup() later, to combine RCU waits. | |
0d143fe1 | 740 | */ |
051007d9 | 741 | flushed = sta_info_flush_defer(sdata); |
09f4114e JB |
742 | WARN_ON_ONCE((sdata->vif.type != NL80211_IFTYPE_WDS && flushed > 0) || |
743 | (sdata->vif.type == NL80211_IFTYPE_WDS && flushed != 1)); | |
0d143fe1 | 744 | |
afdc7c18 | 745 | /* don't count this interface for promisc/allmulti while it is down */ |
0d143fe1 JB |
746 | if (sdata->flags & IEEE80211_SDATA_ALLMULTI) |
747 | atomic_dec(&local->iff_allmultis); | |
748 | ||
749 | if (sdata->flags & IEEE80211_SDATA_PROMISC) | |
750 | atomic_dec(&local->iff_promiscs); | |
751 | ||
7be5086d | 752 | if (sdata->vif.type == NL80211_IFTYPE_AP) { |
e3b90ca2 | 753 | local->fif_pspoll--; |
7be5086d JB |
754 | local->fif_probe_req--; |
755 | } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { | |
756 | local->fif_probe_req--; | |
757 | } | |
e3b90ca2 | 758 | |
f142c6b9 JB |
759 | if (sdata->dev) { |
760 | netif_addr_lock_bh(sdata->dev); | |
761 | spin_lock_bh(&local->filter_lock); | |
762 | __hw_addr_unsync(&local->mc_list, &sdata->dev->mc, | |
763 | sdata->dev->addr_len); | |
764 | spin_unlock_bh(&local->filter_lock); | |
765 | netif_addr_unlock_bh(sdata->dev); | |
f142c6b9 | 766 | } |
3ac64bee | 767 | |
7cbf0ba5 VN |
768 | del_timer_sync(&local->dynamic_ps_timer); |
769 | cancel_work_sync(&local->dynamic_ps_enable_work); | |
0d143fe1 | 770 | |
04ecd257 JB |
771 | cancel_work_sync(&sdata->recalc_smps); |
772 | ||
164eb02d SW |
773 | cancel_delayed_work_sync(&sdata->dfs_cac_timer_work); |
774 | ||
775 | if (sdata->wdev.cac_started) { | |
b2c0958b | 776 | WARN_ON(local->suspended); |
164eb02d SW |
777 | mutex_lock(&local->iflist_mtx); |
778 | ieee80211_vif_release_channel(sdata); | |
779 | mutex_unlock(&local->iflist_mtx); | |
780 | cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_ABORTED, | |
781 | GFP_KERNEL); | |
782 | } | |
783 | ||
0d143fe1 JB |
784 | /* APs need special treatment */ |
785 | if (sdata->vif.type == NL80211_IFTYPE_AP) { | |
57c9fff3 | 786 | struct ieee80211_sub_if_data *vlan, *tmpsdata; |
0d143fe1 JB |
787 | |
788 | /* down all dependent devices, that is VLANs */ | |
57c9fff3 | 789 | list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans, |
0d143fe1 JB |
790 | u.vlan.list) |
791 | dev_close(vlan->dev); | |
792 | WARN_ON(!list_empty(&sdata->u.ap.vlans)); | |
397a7a24 MB |
793 | } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { |
794 | /* remove all packets in parent bc_buf pointing to this dev */ | |
795 | ps = &sdata->bss->ps; | |
796 | ||
797 | spin_lock_irqsave(&ps->bc_buf.lock, flags); | |
798 | skb_queue_walk_safe(&ps->bc_buf, skb, tmp) { | |
799 | if (skb->dev == sdata->dev) { | |
800 | __skb_unlink(skb, &ps->bc_buf); | |
801 | local->total_ps_buffered--; | |
802 | ieee80211_free_txskb(&local->hw, skb); | |
803 | } | |
804 | } | |
805 | spin_unlock_irqrestore(&ps->bc_buf.lock, flags); | |
0d143fe1 JB |
806 | } |
807 | ||
34d4bc4d JB |
808 | if (going_down) |
809 | local->open_count--; | |
0d143fe1 JB |
810 | |
811 | switch (sdata->vif.type) { | |
812 | case NL80211_IFTYPE_AP_VLAN: | |
813 | list_del(&sdata->u.vlan.list); | |
4d76d21b | 814 | rcu_assign_pointer(sdata->vif.chanctx_conf, NULL); |
0d143fe1 JB |
815 | /* no need to tell driver */ |
816 | break; | |
817 | case NL80211_IFTYPE_MONITOR: | |
818 | if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) { | |
819 | local->cooked_mntrs--; | |
820 | break; | |
821 | } | |
822 | ||
823 | local->monitors--; | |
e8975581 | 824 | if (local->monitors == 0) { |
0869aea0 JB |
825 | local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR; |
826 | hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR; | |
e8975581 | 827 | } |
0d143fe1 | 828 | |
85416a4f | 829 | ieee80211_adjust_monitor_flags(sdata, -1); |
0d143fe1 | 830 | break; |
f142c6b9 JB |
831 | case NL80211_IFTYPE_P2P_DEVICE: |
832 | /* relies on synchronize_rcu() below */ | |
833 | rcu_assign_pointer(local->p2p_sdata, NULL); | |
834 | /* fall through */ | |
0d143fe1 | 835 | default: |
0b7dff4f | 836 | cancel_work_sync(&sdata->work); |
35f20c14 JB |
837 | /* |
838 | * When we get here, the interface is marked down. | |
75de9113 JB |
839 | * |
840 | * sta_info_flush_cleanup() requires rcu_barrier() | |
841 | * first to wait for the station call_rcu() calls | |
842 | * to complete, here we need at least sychronize_rcu() | |
843 | * it to wait for the RX path in case it is using the | |
844 | * interface and enqueuing frames at this very time on | |
845 | * another CPU. | |
35f20c14 | 846 | */ |
75de9113 | 847 | rcu_barrier(); |
051007d9 JB |
848 | sta_info_flush_cleanup(sdata); |
849 | ||
b9dcf712 JB |
850 | /* |
851 | * Free all remaining keys, there shouldn't be any, | |
7b4396bd | 852 | * except maybe in WDS mode? |
b9dcf712 | 853 | */ |
ad0e2b5a | 854 | ieee80211_free_keys(sdata); |
b9dcf712 | 855 | |
1861b845 JB |
856 | /* fall through */ |
857 | case NL80211_IFTYPE_AP: | |
858 | skb_queue_purge(&sdata->skb_queue); | |
0d143fe1 JB |
859 | } |
860 | ||
861 | sdata->bss = NULL; | |
862 | ||
5061b0c2 JB |
863 | spin_lock_irqsave(&local->queue_stop_reason_lock, flags); |
864 | for (i = 0; i < IEEE80211_MAX_QUEUES; i++) { | |
865 | skb_queue_walk_safe(&local->pending[i], skb, tmp) { | |
866 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); | |
867 | if (info->control.vif == &sdata->vif) { | |
868 | __skb_unlink(skb, &local->pending[i]); | |
d4fa14cd | 869 | ieee80211_free_txskb(&local->hw, skb); |
5061b0c2 JB |
870 | } |
871 | } | |
872 | } | |
873 | spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); | |
075e0847 | 874 | |
b2c0958b JB |
875 | if (local->open_count == 0) |
876 | ieee80211_clear_tx_pending(local); | |
877 | ||
878 | /* | |
879 | * If the interface goes down while suspended, presumably because | |
880 | * the device was unplugged and that happens before our resume, | |
881 | * then the driver is already unconfigured and the remainder of | |
882 | * this function isn't needed. | |
883 | * XXX: what about WoWLAN? If the device has software state, e.g. | |
884 | * memory allocated, it might expect teardown commands from | |
885 | * mac80211 here? | |
886 | */ | |
887 | if (local->suspended) { | |
888 | WARN_ON(local->wowlan); | |
889 | WARN_ON(rtnl_dereference(local->monitor_sdata)); | |
890 | return; | |
891 | } | |
892 | ||
893 | switch (sdata->vif.type) { | |
894 | case NL80211_IFTYPE_AP_VLAN: | |
895 | break; | |
896 | case NL80211_IFTYPE_MONITOR: | |
897 | if (local->monitors == 0) | |
898 | ieee80211_del_virtual_monitor(local); | |
899 | ||
900 | mutex_lock(&local->mtx); | |
901 | ieee80211_recalc_idle(local); | |
902 | mutex_unlock(&local->mtx); | |
903 | break; | |
904 | default: | |
905 | if (going_down) | |
906 | drv_remove_interface(local, sdata); | |
907 | } | |
908 | ||
909 | ieee80211_recalc_ps(local, -1); | |
910 | ||
911 | if (local->open_count == 0) { | |
912 | ieee80211_stop_device(local); | |
913 | ||
914 | /* no reconfiguring after stop! */ | |
915 | return; | |
916 | } | |
917 | ||
918 | /* do after stop to avoid reconfiguring when we stop anyway */ | |
919 | ieee80211_configure_filter(local); | |
920 | ieee80211_hw_config(local, hw_reconf_flags); | |
921 | ||
922 | if (local->monitors == local->open_count) | |
075e0847 | 923 | ieee80211_add_virtual_monitor(local); |
34d4bc4d JB |
924 | } |
925 | ||
926 | static int ieee80211_stop(struct net_device *dev) | |
927 | { | |
928 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
929 | ||
930 | ieee80211_do_stop(sdata, true); | |
5061b0c2 | 931 | |
0d143fe1 JB |
932 | return 0; |
933 | } | |
934 | ||
935 | static void ieee80211_set_multicast_list(struct net_device *dev) | |
936 | { | |
0d143fe1 | 937 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
b4a4bf5d | 938 | struct ieee80211_local *local = sdata->local; |
0d143fe1 JB |
939 | int allmulti, promisc, sdata_allmulti, sdata_promisc; |
940 | ||
941 | allmulti = !!(dev->flags & IFF_ALLMULTI); | |
942 | promisc = !!(dev->flags & IFF_PROMISC); | |
943 | sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI); | |
944 | sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC); | |
945 | ||
946 | if (allmulti != sdata_allmulti) { | |
947 | if (dev->flags & IFF_ALLMULTI) | |
948 | atomic_inc(&local->iff_allmultis); | |
949 | else | |
950 | atomic_dec(&local->iff_allmultis); | |
951 | sdata->flags ^= IEEE80211_SDATA_ALLMULTI; | |
952 | } | |
953 | ||
954 | if (promisc != sdata_promisc) { | |
955 | if (dev->flags & IFF_PROMISC) | |
956 | atomic_inc(&local->iff_promiscs); | |
957 | else | |
958 | atomic_dec(&local->iff_promiscs); | |
959 | sdata->flags ^= IEEE80211_SDATA_PROMISC; | |
960 | } | |
488b366a AB |
961 | |
962 | /* | |
963 | * TODO: If somebody needs this on AP interfaces, | |
964 | * it can be enabled easily but multicast | |
965 | * addresses from VLANs need to be synced. | |
966 | */ | |
967 | if (sdata->vif.type != NL80211_IFTYPE_MONITOR && | |
968 | sdata->vif.type != NL80211_IFTYPE_AP_VLAN && | |
969 | sdata->vif.type != NL80211_IFTYPE_AP) | |
970 | drv_set_multicast_list(local, sdata, &dev->mc); | |
971 | ||
3b8d81e0 | 972 | spin_lock_bh(&local->filter_lock); |
22bedad3 | 973 | __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len); |
3b8d81e0 | 974 | spin_unlock_bh(&local->filter_lock); |
3ac64bee | 975 | ieee80211_queue_work(&local->hw, &local->reconfig_filter); |
0d143fe1 JB |
976 | } |
977 | ||
75636525 JB |
978 | /* |
979 | * Called when the netdev is removed or, by the code below, before | |
980 | * the interface type changes. | |
981 | */ | |
f142c6b9 | 982 | static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata) |
f0706e82 | 983 | { |
75636525 | 984 | int flushed; |
f0706e82 JB |
985 | int i; |
986 | ||
75636525 JB |
987 | /* free extra data */ |
988 | ieee80211_free_keys(sdata); | |
989 | ||
aee14ceb JM |
990 | ieee80211_debugfs_remove_netdev(sdata); |
991 | ||
f0706e82 | 992 | for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) |
75636525 JB |
993 | __skb_queue_purge(&sdata->fragments[i].skb_list); |
994 | sdata->fragment_next = 0; | |
11a843b7 | 995 | |
b9dcf712 JB |
996 | if (ieee80211_vif_is_mesh(&sdata->vif)) |
997 | mesh_rmc_free(sdata); | |
75636525 | 998 | |
b998e8bb | 999 | flushed = sta_info_flush(sdata); |
75636525 | 1000 | WARN_ON(flushed); |
f0706e82 JB |
1001 | } |
1002 | ||
f142c6b9 JB |
1003 | static void ieee80211_uninit(struct net_device *dev) |
1004 | { | |
1005 | ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev)); | |
1006 | } | |
1007 | ||
cf0277e7 JB |
1008 | static u16 ieee80211_netdev_select_queue(struct net_device *dev, |
1009 | struct sk_buff *skb) | |
1010 | { | |
1011 | return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb); | |
1012 | } | |
1013 | ||
587e729e JB |
1014 | static const struct net_device_ops ieee80211_dataif_ops = { |
1015 | .ndo_open = ieee80211_open, | |
1016 | .ndo_stop = ieee80211_stop, | |
f142c6b9 | 1017 | .ndo_uninit = ieee80211_uninit, |
587e729e | 1018 | .ndo_start_xmit = ieee80211_subif_start_xmit, |
afc4b13d | 1019 | .ndo_set_rx_mode = ieee80211_set_multicast_list, |
587e729e | 1020 | .ndo_change_mtu = ieee80211_change_mtu, |
47846c9b | 1021 | .ndo_set_mac_address = ieee80211_change_mac, |
cf0277e7 | 1022 | .ndo_select_queue = ieee80211_netdev_select_queue, |
587e729e JB |
1023 | }; |
1024 | ||
cf0277e7 JB |
1025 | static u16 ieee80211_monitor_select_queue(struct net_device *dev, |
1026 | struct sk_buff *skb) | |
1027 | { | |
1028 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
1029 | struct ieee80211_local *local = sdata->local; | |
1030 | struct ieee80211_hdr *hdr; | |
1031 | struct ieee80211_radiotap_header *rtap = (void *)skb->data; | |
1032 | ||
32c5057b | 1033 | if (local->hw.queues < IEEE80211_NUM_ACS) |
cf0277e7 JB |
1034 | return 0; |
1035 | ||
1036 | if (skb->len < 4 || | |
b49bb574 | 1037 | skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */) |
cf0277e7 JB |
1038 | return 0; /* doesn't matter, frame will be dropped */ |
1039 | ||
b49bb574 | 1040 | hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len)); |
cf0277e7 | 1041 | |
00e96dec | 1042 | return ieee80211_select_queue_80211(sdata, skb, hdr); |
cf0277e7 JB |
1043 | } |
1044 | ||
587e729e JB |
1045 | static const struct net_device_ops ieee80211_monitorif_ops = { |
1046 | .ndo_open = ieee80211_open, | |
1047 | .ndo_stop = ieee80211_stop, | |
f142c6b9 | 1048 | .ndo_uninit = ieee80211_uninit, |
587e729e | 1049 | .ndo_start_xmit = ieee80211_monitor_start_xmit, |
afc4b13d | 1050 | .ndo_set_rx_mode = ieee80211_set_multicast_list, |
587e729e JB |
1051 | .ndo_change_mtu = ieee80211_change_mtu, |
1052 | .ndo_set_mac_address = eth_mac_addr, | |
cf0277e7 | 1053 | .ndo_select_queue = ieee80211_monitor_select_queue, |
587e729e JB |
1054 | }; |
1055 | ||
1056 | static void ieee80211_if_setup(struct net_device *dev) | |
1057 | { | |
1058 | ether_setup(dev); | |
550fd08c | 1059 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
587e729e | 1060 | dev->netdev_ops = &ieee80211_dataif_ops; |
587e729e JB |
1061 | dev->destructor = free_netdev; |
1062 | } | |
1063 | ||
1fa57d01 JB |
1064 | static void ieee80211_iface_work(struct work_struct *work) |
1065 | { | |
1066 | struct ieee80211_sub_if_data *sdata = | |
1067 | container_of(work, struct ieee80211_sub_if_data, work); | |
1068 | struct ieee80211_local *local = sdata->local; | |
1069 | struct sk_buff *skb; | |
344eec67 | 1070 | struct sta_info *sta; |
c1475ca9 | 1071 | struct ieee80211_ra_tid *ra_tid; |
1fa57d01 JB |
1072 | |
1073 | if (!ieee80211_sdata_running(sdata)) | |
1074 | return; | |
1075 | ||
1076 | if (local->scanning) | |
1077 | return; | |
1078 | ||
1079 | /* | |
1080 | * ieee80211_queue_work() should have picked up most cases, | |
1081 | * here we'll pick the rest. | |
1082 | */ | |
1083 | if (WARN(local->suspended, | |
1084 | "interface work scheduled while going to suspend\n")) | |
1085 | return; | |
1086 | ||
1087 | /* first process frames */ | |
1088 | while ((skb = skb_dequeue(&sdata->skb_queue))) { | |
bed7ee6e JB |
1089 | struct ieee80211_mgmt *mgmt = (void *)skb->data; |
1090 | ||
c1475ca9 JB |
1091 | if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) { |
1092 | ra_tid = (void *)&skb->cb; | |
1093 | ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra, | |
1094 | ra_tid->tid); | |
1095 | } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) { | |
1096 | ra_tid = (void *)&skb->cb; | |
1097 | ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra, | |
1098 | ra_tid->tid); | |
1099 | } else if (ieee80211_is_action(mgmt->frame_control) && | |
1100 | mgmt->u.action.category == WLAN_CATEGORY_BACK) { | |
bed7ee6e | 1101 | int len = skb->len; |
bed7ee6e | 1102 | |
a93e3644 | 1103 | mutex_lock(&local->sta_mtx); |
875ae5f6 | 1104 | sta = sta_info_get_bss(sdata, mgmt->sa); |
bed7ee6e JB |
1105 | if (sta) { |
1106 | switch (mgmt->u.action.u.addba_req.action_code) { | |
1107 | case WLAN_ACTION_ADDBA_REQ: | |
1108 | ieee80211_process_addba_request( | |
1109 | local, sta, mgmt, len); | |
1110 | break; | |
1111 | case WLAN_ACTION_ADDBA_RESP: | |
1112 | ieee80211_process_addba_resp(local, sta, | |
1113 | mgmt, len); | |
1114 | break; | |
1115 | case WLAN_ACTION_DELBA: | |
1116 | ieee80211_process_delba(sdata, sta, | |
1117 | mgmt, len); | |
1118 | break; | |
1119 | default: | |
1120 | WARN_ON(1); | |
1121 | break; | |
1122 | } | |
1123 | } | |
a93e3644 | 1124 | mutex_unlock(&local->sta_mtx); |
344eec67 JB |
1125 | } else if (ieee80211_is_data_qos(mgmt->frame_control)) { |
1126 | struct ieee80211_hdr *hdr = (void *)mgmt; | |
1127 | /* | |
1128 | * So the frame isn't mgmt, but frame_control | |
1129 | * is at the right place anyway, of course, so | |
1130 | * the if statement is correct. | |
1131 | * | |
1132 | * Warn if we have other data frame types here, | |
1133 | * they must not get here. | |
1134 | */ | |
1135 | WARN_ON(hdr->frame_control & | |
1136 | cpu_to_le16(IEEE80211_STYPE_NULLFUNC)); | |
1137 | WARN_ON(!(hdr->seq_ctrl & | |
1138 | cpu_to_le16(IEEE80211_SCTL_FRAG))); | |
1139 | /* | |
1140 | * This was a fragment of a frame, received while | |
1141 | * a block-ack session was active. That cannot be | |
1142 | * right, so terminate the session. | |
1143 | */ | |
a93e3644 | 1144 | mutex_lock(&local->sta_mtx); |
875ae5f6 | 1145 | sta = sta_info_get_bss(sdata, mgmt->sa); |
344eec67 JB |
1146 | if (sta) { |
1147 | u16 tid = *ieee80211_get_qos_ctl(hdr) & | |
1148 | IEEE80211_QOS_CTL_TID_MASK; | |
1149 | ||
1150 | __ieee80211_stop_rx_ba_session( | |
1151 | sta, tid, WLAN_BACK_RECIPIENT, | |
53f73c09 JB |
1152 | WLAN_REASON_QSTA_REQUIRE_SETUP, |
1153 | true); | |
344eec67 | 1154 | } |
a93e3644 | 1155 | mutex_unlock(&local->sta_mtx); |
bed7ee6e | 1156 | } else switch (sdata->vif.type) { |
1fa57d01 JB |
1157 | case NL80211_IFTYPE_STATION: |
1158 | ieee80211_sta_rx_queued_mgmt(sdata, skb); | |
1159 | break; | |
1160 | case NL80211_IFTYPE_ADHOC: | |
1161 | ieee80211_ibss_rx_queued_mgmt(sdata, skb); | |
1162 | break; | |
1163 | case NL80211_IFTYPE_MESH_POINT: | |
1164 | if (!ieee80211_vif_is_mesh(&sdata->vif)) | |
1165 | break; | |
1166 | ieee80211_mesh_rx_queued_mgmt(sdata, skb); | |
1167 | break; | |
1168 | default: | |
1169 | WARN(1, "frame for unexpected interface type"); | |
1fa57d01 JB |
1170 | break; |
1171 | } | |
36b3a628 JB |
1172 | |
1173 | kfree_skb(skb); | |
1fa57d01 JB |
1174 | } |
1175 | ||
1176 | /* then other type-dependent work */ | |
1177 | switch (sdata->vif.type) { | |
1178 | case NL80211_IFTYPE_STATION: | |
1179 | ieee80211_sta_work(sdata); | |
1180 | break; | |
1181 | case NL80211_IFTYPE_ADHOC: | |
1182 | ieee80211_ibss_work(sdata); | |
1183 | break; | |
1184 | case NL80211_IFTYPE_MESH_POINT: | |
1185 | if (!ieee80211_vif_is_mesh(&sdata->vif)) | |
1186 | break; | |
1187 | ieee80211_mesh_work(sdata); | |
1188 | break; | |
1189 | default: | |
1190 | break; | |
1191 | } | |
1192 | } | |
1193 | ||
04ecd257 JB |
1194 | static void ieee80211_recalc_smps_work(struct work_struct *work) |
1195 | { | |
1196 | struct ieee80211_sub_if_data *sdata = | |
1197 | container_of(work, struct ieee80211_sub_if_data, recalc_smps); | |
1198 | ||
1199 | ieee80211_recalc_smps(sdata); | |
1200 | } | |
1fa57d01 | 1201 | |
75636525 JB |
1202 | /* |
1203 | * Helper function to initialise an interface to a specific type. | |
1204 | */ | |
1205 | static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata, | |
05c914fe | 1206 | enum nl80211_iftype type) |
f0706e82 | 1207 | { |
75636525 JB |
1208 | /* clear type-dependent union */ |
1209 | memset(&sdata->u, 0, sizeof(sdata->u)); | |
1210 | ||
1211 | /* and set some type-dependent values */ | |
1212 | sdata->vif.type = type; | |
2ca27bcf | 1213 | sdata->vif.p2p = false; |
60719ffd | 1214 | sdata->wdev.iftype = type; |
75636525 | 1215 | |
a621fa4d JB |
1216 | sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE); |
1217 | sdata->control_port_no_encrypt = false; | |
1218 | ||
b53be792 SW |
1219 | sdata->noack_map = 0; |
1220 | ||
f142c6b9 JB |
1221 | /* only monitor/p2p-device differ */ |
1222 | if (sdata->dev) { | |
1223 | sdata->dev->netdev_ops = &ieee80211_dataif_ops; | |
1224 | sdata->dev->type = ARPHRD_ETHER; | |
1225 | } | |
75636525 | 1226 | |
35f20c14 | 1227 | skb_queue_head_init(&sdata->skb_queue); |
1fa57d01 | 1228 | INIT_WORK(&sdata->work, ieee80211_iface_work); |
04ecd257 | 1229 | INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work); |
35f20c14 | 1230 | |
75636525 | 1231 | switch (type) { |
2ca27bcf JB |
1232 | case NL80211_IFTYPE_P2P_GO: |
1233 | type = NL80211_IFTYPE_AP; | |
1234 | sdata->vif.type = type; | |
1235 | sdata->vif.p2p = true; | |
1236 | /* fall through */ | |
05c914fe | 1237 | case NL80211_IFTYPE_AP: |
d012a605 | 1238 | skb_queue_head_init(&sdata->u.ap.ps.bc_buf); |
75636525 | 1239 | INIT_LIST_HEAD(&sdata->u.ap.vlans); |
ad2d223a | 1240 | sdata->vif.bss_conf.bssid = sdata->vif.addr; |
75636525 | 1241 | break; |
2ca27bcf JB |
1242 | case NL80211_IFTYPE_P2P_CLIENT: |
1243 | type = NL80211_IFTYPE_STATION; | |
1244 | sdata->vif.type = type; | |
1245 | sdata->vif.p2p = true; | |
1246 | /* fall through */ | |
05c914fe | 1247 | case NL80211_IFTYPE_STATION: |
ad2d223a | 1248 | sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid; |
9c6bd790 | 1249 | ieee80211_sta_setup_sdata(sdata); |
472dbc45 | 1250 | break; |
46900298 | 1251 | case NL80211_IFTYPE_ADHOC: |
ad2d223a | 1252 | sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid; |
46900298 JB |
1253 | ieee80211_ibss_setup_sdata(sdata); |
1254 | break; | |
05c914fe | 1255 | case NL80211_IFTYPE_MESH_POINT: |
75636525 JB |
1256 | if (ieee80211_vif_is_mesh(&sdata->vif)) |
1257 | ieee80211_mesh_init_sdata(sdata); | |
1258 | break; | |
05c914fe | 1259 | case NL80211_IFTYPE_MONITOR: |
75636525 | 1260 | sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP; |
587e729e | 1261 | sdata->dev->netdev_ops = &ieee80211_monitorif_ops; |
75636525 JB |
1262 | sdata->u.mntr_flags = MONITOR_FLAG_CONTROL | |
1263 | MONITOR_FLAG_OTHER_BSS; | |
1264 | break; | |
05c914fe | 1265 | case NL80211_IFTYPE_WDS: |
ad2d223a JB |
1266 | sdata->vif.bss_conf.bssid = NULL; |
1267 | break; | |
05c914fe | 1268 | case NL80211_IFTYPE_AP_VLAN: |
ad2d223a | 1269 | break; |
98104fde | 1270 | case NL80211_IFTYPE_P2P_DEVICE: |
ad2d223a | 1271 | sdata->vif.bss_conf.bssid = sdata->vif.addr; |
f142c6b9 | 1272 | break; |
05c914fe | 1273 | case NL80211_IFTYPE_UNSPECIFIED: |
2e161f78 | 1274 | case NUM_NL80211_IFTYPES: |
75636525 JB |
1275 | BUG(); |
1276 | break; | |
1277 | } | |
1278 | ||
1279 | ieee80211_debugfs_add_netdev(sdata); | |
1280 | } | |
1281 | ||
34d4bc4d JB |
1282 | static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata, |
1283 | enum nl80211_iftype type) | |
1284 | { | |
1285 | struct ieee80211_local *local = sdata->local; | |
1286 | int ret, err; | |
2ca27bcf JB |
1287 | enum nl80211_iftype internal_type = type; |
1288 | bool p2p = false; | |
34d4bc4d JB |
1289 | |
1290 | ASSERT_RTNL(); | |
1291 | ||
1292 | if (!local->ops->change_interface) | |
1293 | return -EBUSY; | |
1294 | ||
1295 | switch (sdata->vif.type) { | |
1296 | case NL80211_IFTYPE_AP: | |
1297 | case NL80211_IFTYPE_STATION: | |
1298 | case NL80211_IFTYPE_ADHOC: | |
1299 | /* | |
1300 | * Could maybe also all others here? | |
1301 | * Just not sure how that interacts | |
1302 | * with the RX/config path e.g. for | |
1303 | * mesh. | |
1304 | */ | |
1305 | break; | |
1306 | default: | |
1307 | return -EBUSY; | |
1308 | } | |
1309 | ||
1310 | switch (type) { | |
1311 | case NL80211_IFTYPE_AP: | |
1312 | case NL80211_IFTYPE_STATION: | |
1313 | case NL80211_IFTYPE_ADHOC: | |
1314 | /* | |
1315 | * Could probably support everything | |
1316 | * but WDS here (WDS do_open can fail | |
1317 | * under memory pressure, which this | |
1318 | * code isn't prepared to handle). | |
1319 | */ | |
1320 | break; | |
2ca27bcf JB |
1321 | case NL80211_IFTYPE_P2P_CLIENT: |
1322 | p2p = true; | |
1323 | internal_type = NL80211_IFTYPE_STATION; | |
1324 | break; | |
1325 | case NL80211_IFTYPE_P2P_GO: | |
1326 | p2p = true; | |
1327 | internal_type = NL80211_IFTYPE_AP; | |
1328 | break; | |
34d4bc4d JB |
1329 | default: |
1330 | return -EBUSY; | |
1331 | } | |
1332 | ||
2ca27bcf | 1333 | ret = ieee80211_check_concurrent_iface(sdata, internal_type); |
34d4bc4d JB |
1334 | if (ret) |
1335 | return ret; | |
1336 | ||
1337 | ieee80211_do_stop(sdata, false); | |
1338 | ||
f142c6b9 | 1339 | ieee80211_teardown_sdata(sdata); |
34d4bc4d | 1340 | |
2ca27bcf | 1341 | ret = drv_change_interface(local, sdata, internal_type, p2p); |
34d4bc4d JB |
1342 | if (ret) |
1343 | type = sdata->vif.type; | |
1344 | ||
3a25a8c8 JB |
1345 | /* |
1346 | * Ignore return value here, there's not much we can do since | |
1347 | * the driver changed the interface type internally already. | |
1348 | * The warnings will hopefully make driver authors fix it :-) | |
1349 | */ | |
1350 | ieee80211_check_queues(sdata); | |
1351 | ||
34d4bc4d JB |
1352 | ieee80211_setup_sdata(sdata, type); |
1353 | ||
f142c6b9 | 1354 | err = ieee80211_do_open(&sdata->wdev, false); |
34d4bc4d JB |
1355 | WARN(err, "type change: do_open returned %d", err); |
1356 | ||
1357 | return ret; | |
1358 | } | |
1359 | ||
f3947e2d | 1360 | int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata, |
05c914fe | 1361 | enum nl80211_iftype type) |
75636525 | 1362 | { |
34d4bc4d JB |
1363 | int ret; |
1364 | ||
f3947e2d JB |
1365 | ASSERT_RTNL(); |
1366 | ||
2ca27bcf | 1367 | if (type == ieee80211_vif_type_p2p(&sdata->vif)) |
f3947e2d JB |
1368 | return 0; |
1369 | ||
34d4bc4d JB |
1370 | if (ieee80211_sdata_running(sdata)) { |
1371 | ret = ieee80211_runtime_change_iftype(sdata, type); | |
1372 | if (ret) | |
1373 | return ret; | |
1374 | } else { | |
1375 | /* Purge and reset type-dependent state. */ | |
f142c6b9 | 1376 | ieee80211_teardown_sdata(sdata); |
34d4bc4d JB |
1377 | ieee80211_setup_sdata(sdata, type); |
1378 | } | |
75636525 JB |
1379 | |
1380 | /* reset some values that shouldn't be kept across type changes */ | |
75636525 | 1381 | sdata->drop_unencrypted = 0; |
9bc383de JB |
1382 | if (type == NL80211_IFTYPE_STATION) |
1383 | sdata->u.mgd.use_4addr = false; | |
f3947e2d JB |
1384 | |
1385 | return 0; | |
f0706e82 JB |
1386 | } |
1387 | ||
fa9029f8 | 1388 | static void ieee80211_assign_perm_addr(struct ieee80211_local *local, |
f142c6b9 | 1389 | u8 *perm_addr, enum nl80211_iftype type) |
fa9029f8 JB |
1390 | { |
1391 | struct ieee80211_sub_if_data *sdata; | |
1392 | u64 mask, start, addr, val, inc; | |
1393 | u8 *m; | |
1394 | u8 tmp_addr[ETH_ALEN]; | |
1395 | int i; | |
1396 | ||
1397 | /* default ... something at least */ | |
f142c6b9 | 1398 | memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN); |
fa9029f8 JB |
1399 | |
1400 | if (is_zero_ether_addr(local->hw.wiphy->addr_mask) && | |
1401 | local->hw.wiphy->n_addresses <= 1) | |
1402 | return; | |
1403 | ||
fa9029f8 JB |
1404 | mutex_lock(&local->iflist_mtx); |
1405 | ||
1406 | switch (type) { | |
1407 | case NL80211_IFTYPE_MONITOR: | |
1408 | /* doesn't matter */ | |
1409 | break; | |
1410 | case NL80211_IFTYPE_WDS: | |
1411 | case NL80211_IFTYPE_AP_VLAN: | |
1412 | /* match up with an AP interface */ | |
1413 | list_for_each_entry(sdata, &local->interfaces, list) { | |
1414 | if (sdata->vif.type != NL80211_IFTYPE_AP) | |
1415 | continue; | |
f142c6b9 | 1416 | memcpy(perm_addr, sdata->vif.addr, ETH_ALEN); |
fa9029f8 JB |
1417 | break; |
1418 | } | |
1419 | /* keep default if no AP interface present */ | |
1420 | break; | |
6d71117a JB |
1421 | case NL80211_IFTYPE_P2P_CLIENT: |
1422 | case NL80211_IFTYPE_P2P_GO: | |
1423 | if (local->hw.flags & IEEE80211_HW_P2P_DEV_ADDR_FOR_INTF) { | |
1424 | list_for_each_entry(sdata, &local->interfaces, list) { | |
1425 | if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) | |
1426 | continue; | |
1427 | if (!ieee80211_sdata_running(sdata)) | |
1428 | continue; | |
1429 | memcpy(perm_addr, sdata->vif.addr, ETH_ALEN); | |
1430 | goto out_unlock; | |
1431 | } | |
1432 | } | |
1433 | /* otherwise fall through */ | |
fa9029f8 JB |
1434 | default: |
1435 | /* assign a new address if possible -- try n_addresses first */ | |
1436 | for (i = 0; i < local->hw.wiphy->n_addresses; i++) { | |
1437 | bool used = false; | |
1438 | ||
1439 | list_for_each_entry(sdata, &local->interfaces, list) { | |
1440 | if (memcmp(local->hw.wiphy->addresses[i].addr, | |
1441 | sdata->vif.addr, ETH_ALEN) == 0) { | |
1442 | used = true; | |
1443 | break; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | if (!used) { | |
f142c6b9 | 1448 | memcpy(perm_addr, |
fa9029f8 JB |
1449 | local->hw.wiphy->addresses[i].addr, |
1450 | ETH_ALEN); | |
1451 | break; | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | /* try mask if available */ | |
1456 | if (is_zero_ether_addr(local->hw.wiphy->addr_mask)) | |
1457 | break; | |
1458 | ||
1459 | m = local->hw.wiphy->addr_mask; | |
1460 | mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | | |
1461 | ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | | |
1462 | ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); | |
1463 | ||
1464 | if (__ffs64(mask) + hweight64(mask) != fls64(mask)) { | |
1465 | /* not a contiguous mask ... not handled now! */ | |
bdcbd8e0 | 1466 | pr_info("not contiguous\n"); |
fa9029f8 JB |
1467 | break; |
1468 | } | |
1469 | ||
1470 | m = local->hw.wiphy->perm_addr; | |
1471 | start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | | |
1472 | ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | | |
1473 | ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); | |
1474 | ||
1475 | inc = 1ULL<<__ffs64(mask); | |
1476 | val = (start & mask); | |
1477 | addr = (start & ~mask) | (val & mask); | |
1478 | do { | |
1479 | bool used = false; | |
1480 | ||
1481 | tmp_addr[5] = addr >> 0*8; | |
1482 | tmp_addr[4] = addr >> 1*8; | |
1483 | tmp_addr[3] = addr >> 2*8; | |
1484 | tmp_addr[2] = addr >> 3*8; | |
1485 | tmp_addr[1] = addr >> 4*8; | |
1486 | tmp_addr[0] = addr >> 5*8; | |
1487 | ||
1488 | val += inc; | |
1489 | ||
1490 | list_for_each_entry(sdata, &local->interfaces, list) { | |
1491 | if (memcmp(tmp_addr, sdata->vif.addr, | |
1492 | ETH_ALEN) == 0) { | |
1493 | used = true; | |
1494 | break; | |
1495 | } | |
1496 | } | |
1497 | ||
1498 | if (!used) { | |
f142c6b9 | 1499 | memcpy(perm_addr, tmp_addr, ETH_ALEN); |
fa9029f8 JB |
1500 | break; |
1501 | } | |
1502 | addr = (start & ~mask) | (val & mask); | |
1503 | } while (addr != start); | |
1504 | ||
1505 | break; | |
1506 | } | |
1507 | ||
6d71117a | 1508 | out_unlock: |
fa9029f8 JB |
1509 | mutex_unlock(&local->iflist_mtx); |
1510 | } | |
1511 | ||
97f97b1f JB |
1512 | static void ieee80211_cleanup_sdata_stas_wk(struct work_struct *wk) |
1513 | { | |
1514 | struct ieee80211_sub_if_data *sdata; | |
1515 | ||
1516 | sdata = container_of(wk, struct ieee80211_sub_if_data, cleanup_stations_wk); | |
1517 | ||
1518 | ieee80211_cleanup_sdata_stas(sdata); | |
1519 | } | |
1520 | ||
3e122be0 | 1521 | int ieee80211_if_add(struct ieee80211_local *local, const char *name, |
84efbb84 | 1522 | struct wireless_dev **new_wdev, enum nl80211_iftype type, |
ee385855 | 1523 | struct vif_params *params) |
f0706e82 | 1524 | { |
f142c6b9 | 1525 | struct net_device *ndev = NULL; |
f0706e82 | 1526 | struct ieee80211_sub_if_data *sdata = NULL; |
75636525 | 1527 | int ret, i; |
ded81f6b | 1528 | int txqs = 1; |
f0706e82 JB |
1529 | |
1530 | ASSERT_RTNL(); | |
75636525 | 1531 | |
f142c6b9 JB |
1532 | if (type == NL80211_IFTYPE_P2P_DEVICE) { |
1533 | struct wireless_dev *wdev; | |
1534 | ||
1535 | sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, | |
1536 | GFP_KERNEL); | |
1537 | if (!sdata) | |
1538 | return -ENOMEM; | |
1539 | wdev = &sdata->wdev; | |
1540 | ||
1541 | sdata->dev = NULL; | |
1542 | strlcpy(sdata->name, name, IFNAMSIZ); | |
1543 | ieee80211_assign_perm_addr(local, wdev->address, type); | |
1544 | memcpy(sdata->vif.addr, wdev->address, ETH_ALEN); | |
1545 | } else { | |
1546 | if (local->hw.queues >= IEEE80211_NUM_ACS) | |
1547 | txqs = IEEE80211_NUM_ACS; | |
1548 | ||
1549 | ndev = alloc_netdev_mqs(sizeof(*sdata) + | |
1550 | local->hw.vif_data_size, | |
1551 | name, ieee80211_if_setup, txqs, 1); | |
1552 | if (!ndev) | |
1553 | return -ENOMEM; | |
1554 | dev_net_set(ndev, wiphy_net(local->hw.wiphy)); | |
1555 | ||
1556 | ndev->needed_headroom = local->tx_headroom + | |
1557 | 4*6 /* four MAC addresses */ | |
1558 | + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */ | |
1559 | + 6 /* mesh */ | |
1560 | + 8 /* rfc1042/bridge tunnel */ | |
1561 | - ETH_HLEN /* ethernet hard_header_len */ | |
1562 | + IEEE80211_ENCRYPT_HEADROOM; | |
1563 | ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM; | |
1564 | ||
1565 | ret = dev_alloc_name(ndev, ndev->name); | |
1566 | if (ret < 0) { | |
1567 | free_netdev(ndev); | |
1568 | return ret; | |
1569 | } | |
1570 | ||
1571 | ieee80211_assign_perm_addr(local, ndev->perm_addr, type); | |
1572 | memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN); | |
1573 | SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy)); | |
1574 | ||
1575 | /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */ | |
1576 | sdata = netdev_priv(ndev); | |
1577 | ndev->ieee80211_ptr = &sdata->wdev; | |
1578 | memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN); | |
1579 | memcpy(sdata->name, ndev->name, IFNAMSIZ); | |
1580 | ||
1581 | sdata->dev = ndev; | |
1582 | } | |
75636525 JB |
1583 | |
1584 | /* initialise type-independent data */ | |
f0706e82 | 1585 | sdata->wdev.wiphy = local->hw.wiphy; |
f0706e82 | 1586 | sdata->local = local; |
75636525 JB |
1587 | |
1588 | for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) | |
1589 | skb_queue_head_init(&sdata->fragments[i].skb_list); | |
1590 | ||
1591 | INIT_LIST_HEAD(&sdata->key_list); | |
1592 | ||
97f97b1f JB |
1593 | spin_lock_init(&sdata->cleanup_stations_lock); |
1594 | INIT_LIST_HEAD(&sdata->cleanup_stations); | |
1595 | INIT_WORK(&sdata->cleanup_stations_wk, ieee80211_cleanup_sdata_stas_wk); | |
164eb02d SW |
1596 | INIT_DELAYED_WORK(&sdata->dfs_cac_timer_work, |
1597 | ieee80211_dfs_cac_timer_work); | |
8d1f7ecd JB |
1598 | INIT_DELAYED_WORK(&sdata->dec_tailroom_needed_wk, |
1599 | ieee80211_delayed_tailroom_dec); | |
97f97b1f | 1600 | |
37eb0b16 JM |
1601 | for (i = 0; i < IEEE80211_NUM_BANDS; i++) { |
1602 | struct ieee80211_supported_band *sband; | |
1603 | sband = local->hw.wiphy->bands[i]; | |
1604 | sdata->rc_rateidx_mask[i] = | |
1605 | sband ? (1 << sband->n_bitrates) - 1 : 0; | |
19468413 SW |
1606 | if (sband) |
1607 | memcpy(sdata->rc_rateidx_mcs_mask[i], | |
1608 | sband->ht_cap.mcs.rx_mask, | |
1609 | sizeof(sdata->rc_rateidx_mcs_mask[i])); | |
1610 | else | |
1611 | memset(sdata->rc_rateidx_mcs_mask[i], 0, | |
1612 | sizeof(sdata->rc_rateidx_mcs_mask[i])); | |
37eb0b16 | 1613 | } |
75636525 | 1614 | |
3a25a8c8 JB |
1615 | ieee80211_set_default_queues(sdata); |
1616 | ||
1ea6f9c0 JB |
1617 | sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL; |
1618 | sdata->user_power_level = local->user_power_level; | |
1619 | ||
75636525 JB |
1620 | /* setup type-dependent data */ |
1621 | ieee80211_setup_sdata(sdata, type); | |
f0706e82 | 1622 | |
f142c6b9 JB |
1623 | if (ndev) { |
1624 | if (params) { | |
1625 | ndev->ieee80211_ptr->use_4addr = params->use_4addr; | |
1626 | if (type == NL80211_IFTYPE_STATION) | |
1627 | sdata->u.mgd.use_4addr = params->use_4addr; | |
1628 | } | |
9bc383de | 1629 | |
f142c6b9 | 1630 | ndev->features |= local->hw.netdev_features; |
72d78728 | 1631 | |
f142c6b9 JB |
1632 | ret = register_netdevice(ndev); |
1633 | if (ret) { | |
1634 | free_netdev(ndev); | |
1635 | return ret; | |
1636 | } | |
1637 | } | |
f0706e82 | 1638 | |
c771c9d8 | 1639 | mutex_lock(&local->iflist_mtx); |
79010420 | 1640 | list_add_tail_rcu(&sdata->list, &local->interfaces); |
c771c9d8 | 1641 | mutex_unlock(&local->iflist_mtx); |
79010420 | 1642 | |
84efbb84 JB |
1643 | if (new_wdev) |
1644 | *new_wdev = &sdata->wdev; | |
f0706e82 | 1645 | |
f0706e82 | 1646 | return 0; |
f0706e82 JB |
1647 | } |
1648 | ||
f698d856 | 1649 | void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata) |
f0706e82 | 1650 | { |
f0706e82 | 1651 | ASSERT_RTNL(); |
11a843b7 | 1652 | |
c771c9d8 | 1653 | mutex_lock(&sdata->local->iflist_mtx); |
75636525 | 1654 | list_del_rcu(&sdata->list); |
c771c9d8 JB |
1655 | mutex_unlock(&sdata->local->iflist_mtx); |
1656 | ||
75636525 | 1657 | synchronize_rcu(); |
f142c6b9 JB |
1658 | |
1659 | if (sdata->dev) { | |
1660 | unregister_netdevice(sdata->dev); | |
1661 | } else { | |
1662 | cfg80211_unregister_wdev(&sdata->wdev); | |
1663 | kfree(sdata); | |
1664 | } | |
1665 | } | |
1666 | ||
1667 | void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata) | |
1668 | { | |
1669 | if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state))) | |
1670 | return; | |
1671 | ieee80211_do_stop(sdata, true); | |
1672 | ieee80211_teardown_sdata(sdata); | |
f0706e82 JB |
1673 | } |
1674 | ||
75636525 JB |
1675 | /* |
1676 | * Remove all interfaces, may only be called at hardware unregistration | |
1677 | * time because it doesn't do RCU-safe list removals. | |
1678 | */ | |
1679 | void ieee80211_remove_interfaces(struct ieee80211_local *local) | |
f0706e82 | 1680 | { |
75636525 | 1681 | struct ieee80211_sub_if_data *sdata, *tmp; |
efe117ab | 1682 | LIST_HEAD(unreg_list); |
f142c6b9 | 1683 | LIST_HEAD(wdev_list); |
f0706e82 JB |
1684 | |
1685 | ASSERT_RTNL(); | |
1686 | ||
efe117ab | 1687 | mutex_lock(&local->iflist_mtx); |
75636525 JB |
1688 | list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) { |
1689 | list_del(&sdata->list); | |
c771c9d8 | 1690 | |
f142c6b9 JB |
1691 | if (sdata->dev) |
1692 | unregister_netdevice_queue(sdata->dev, &unreg_list); | |
1693 | else | |
1694 | list_add(&sdata->list, &wdev_list); | |
f0706e82 | 1695 | } |
efe117ab ED |
1696 | mutex_unlock(&local->iflist_mtx); |
1697 | unregister_netdevice_many(&unreg_list); | |
5f04d506 | 1698 | list_del(&unreg_list); |
f142c6b9 JB |
1699 | |
1700 | list_for_each_entry_safe(sdata, tmp, &wdev_list, list) { | |
1701 | list_del(&sdata->list); | |
1702 | cfg80211_unregister_wdev(&sdata->wdev); | |
1703 | kfree(sdata); | |
1704 | } | |
f0706e82 | 1705 | } |
5cff20e6 | 1706 | |
47846c9b JB |
1707 | static int netdev_notify(struct notifier_block *nb, |
1708 | unsigned long state, | |
1709 | void *ndev) | |
1710 | { | |
1711 | struct net_device *dev = ndev; | |
1712 | struct ieee80211_sub_if_data *sdata; | |
1713 | ||
1714 | if (state != NETDEV_CHANGENAME) | |
1715 | return 0; | |
1716 | ||
1717 | if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy) | |
1718 | return 0; | |
1719 | ||
1720 | if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid) | |
1721 | return 0; | |
1722 | ||
1723 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
1724 | ||
2f5265e6 | 1725 | memcpy(sdata->name, dev->name, IFNAMSIZ); |
47846c9b JB |
1726 | |
1727 | ieee80211_debugfs_rename_netdev(sdata); | |
1728 | return 0; | |
1729 | } | |
1730 | ||
1731 | static struct notifier_block mac80211_netdev_notifier = { | |
1732 | .notifier_call = netdev_notify, | |
1733 | }; | |
1734 | ||
1735 | int ieee80211_iface_init(void) | |
1736 | { | |
1737 | return register_netdevice_notifier(&mac80211_netdev_notifier); | |
1738 | } | |
1739 | ||
1740 | void ieee80211_iface_exit(void) | |
1741 | { | |
1742 | unregister_netdevice_notifier(&mac80211_netdev_notifier); | |
1743 | } |