]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/mac80211/tdls.c
mac80211: use TDLS initiator in tdls_mgmt operations
[karo-tx-linux.git] / net / mac80211 / tdls.c
1 /*
2  * mac80211 TDLS handling code
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  * Copyright 2014, Intel Corporation
6  *
7  * This file is GPLv2 as found in COPYING.
8  */
9
10 #include <linux/ieee80211.h>
11 #include "ieee80211_i.h"
12
13 /* give usermode some time for retries in setting up the TDLS session */
14 #define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
15
16 void ieee80211_tdls_peer_del_work(struct work_struct *wk)
17 {
18         struct ieee80211_sub_if_data *sdata;
19         struct ieee80211_local *local;
20
21         sdata = container_of(wk, struct ieee80211_sub_if_data,
22                              tdls_peer_del_work.work);
23         local = sdata->local;
24
25         mutex_lock(&local->mtx);
26         if (!is_zero_ether_addr(sdata->tdls_peer)) {
27                 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->tdls_peer);
28                 sta_info_destroy_addr(sdata, sdata->tdls_peer);
29                 eth_zero_addr(sdata->tdls_peer);
30         }
31         mutex_unlock(&local->mtx);
32 }
33
34 static void ieee80211_tdls_add_ext_capab(struct sk_buff *skb)
35 {
36         u8 *pos = (void *)skb_put(skb, 7);
37
38         *pos++ = WLAN_EID_EXT_CAPABILITY;
39         *pos++ = 5; /* len */
40         *pos++ = 0x0;
41         *pos++ = 0x0;
42         *pos++ = 0x0;
43         *pos++ = 0x0;
44         *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
45 }
46
47 static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata)
48 {
49         struct ieee80211_local *local = sdata->local;
50         u16 capab;
51
52         capab = 0;
53         if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
54                 return capab;
55
56         if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
57                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
58         if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
59                 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
60
61         return capab;
62 }
63
64 static void ieee80211_tdls_add_link_ie(struct sk_buff *skb, const u8 *src_addr,
65                                        const u8 *peer, const u8 *bssid)
66 {
67         struct ieee80211_tdls_lnkie *lnkid;
68
69         lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
70
71         lnkid->ie_type = WLAN_EID_LINK_ID;
72         lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
73
74         memcpy(lnkid->bssid, bssid, ETH_ALEN);
75         memcpy(lnkid->init_sta, src_addr, ETH_ALEN);
76         memcpy(lnkid->resp_sta, peer, ETH_ALEN);
77 }
78
79 static int
80 ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
81                                const u8 *peer, u8 action_code, u8 dialog_token,
82                                u16 status_code, struct sk_buff *skb)
83 {
84         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
85         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
86         struct ieee80211_tdls_data *tf;
87
88         tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
89
90         memcpy(tf->da, peer, ETH_ALEN);
91         memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
92         tf->ether_type = cpu_to_be16(ETH_P_TDLS);
93         tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
94
95         switch (action_code) {
96         case WLAN_TDLS_SETUP_REQUEST:
97                 tf->category = WLAN_CATEGORY_TDLS;
98                 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
99
100                 skb_put(skb, sizeof(tf->u.setup_req));
101                 tf->u.setup_req.dialog_token = dialog_token;
102                 tf->u.setup_req.capability =
103                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
104
105                 ieee80211_add_srates_ie(sdata, skb, false, band);
106                 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
107                 ieee80211_tdls_add_ext_capab(skb);
108                 break;
109         case WLAN_TDLS_SETUP_RESPONSE:
110                 tf->category = WLAN_CATEGORY_TDLS;
111                 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
112
113                 skb_put(skb, sizeof(tf->u.setup_resp));
114                 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
115                 tf->u.setup_resp.dialog_token = dialog_token;
116                 tf->u.setup_resp.capability =
117                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
118
119                 ieee80211_add_srates_ie(sdata, skb, false, band);
120                 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
121                 ieee80211_tdls_add_ext_capab(skb);
122                 break;
123         case WLAN_TDLS_SETUP_CONFIRM:
124                 tf->category = WLAN_CATEGORY_TDLS;
125                 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
126
127                 skb_put(skb, sizeof(tf->u.setup_cfm));
128                 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
129                 tf->u.setup_cfm.dialog_token = dialog_token;
130                 break;
131         case WLAN_TDLS_TEARDOWN:
132                 tf->category = WLAN_CATEGORY_TDLS;
133                 tf->action_code = WLAN_TDLS_TEARDOWN;
134
135                 skb_put(skb, sizeof(tf->u.teardown));
136                 tf->u.teardown.reason_code = cpu_to_le16(status_code);
137                 break;
138         case WLAN_TDLS_DISCOVERY_REQUEST:
139                 tf->category = WLAN_CATEGORY_TDLS;
140                 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
141
142                 skb_put(skb, sizeof(tf->u.discover_req));
143                 tf->u.discover_req.dialog_token = dialog_token;
144                 break;
145         default:
146                 return -EINVAL;
147         }
148
149         return 0;
150 }
151
152 static int
153 ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
154                            const u8 *peer, u8 action_code, u8 dialog_token,
155                            u16 status_code, struct sk_buff *skb)
156 {
157         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
158         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
159         struct ieee80211_mgmt *mgmt;
160
161         mgmt = (void *)skb_put(skb, 24);
162         memset(mgmt, 0, 24);
163         memcpy(mgmt->da, peer, ETH_ALEN);
164         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
165         memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
166
167         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
168                                           IEEE80211_STYPE_ACTION);
169
170         switch (action_code) {
171         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
172                 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
173                 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
174                 mgmt->u.action.u.tdls_discover_resp.action_code =
175                         WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
176                 mgmt->u.action.u.tdls_discover_resp.dialog_token =
177                         dialog_token;
178                 mgmt->u.action.u.tdls_discover_resp.capability =
179                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
180
181                 ieee80211_add_srates_ie(sdata, skb, false, band);
182                 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
183                 ieee80211_tdls_add_ext_capab(skb);
184                 break;
185         default:
186                 return -EINVAL;
187         }
188
189         return 0;
190 }
191
192 static int
193 ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
194                                 const u8 *peer, u8 action_code,
195                                 u8 dialog_token, u16 status_code,
196                                 u32 peer_capability, bool initiator,
197                                 const u8 *extra_ies, size_t extra_ies_len)
198 {
199         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
200         struct ieee80211_local *local = sdata->local;
201         struct sk_buff *skb = NULL;
202         bool send_direct;
203         const u8 *init_addr, *rsp_addr;
204         int ret;
205
206         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
207                             max(sizeof(struct ieee80211_mgmt),
208                                 sizeof(struct ieee80211_tdls_data)) +
209                             50 + /* supported rates */
210                             7 + /* ext capab */
211                             extra_ies_len +
212                             sizeof(struct ieee80211_tdls_lnkie));
213         if (!skb)
214                 return -ENOMEM;
215
216         skb_reserve(skb, local->hw.extra_tx_headroom);
217
218         switch (action_code) {
219         case WLAN_TDLS_SETUP_REQUEST:
220         case WLAN_TDLS_SETUP_RESPONSE:
221         case WLAN_TDLS_SETUP_CONFIRM:
222         case WLAN_TDLS_TEARDOWN:
223         case WLAN_TDLS_DISCOVERY_REQUEST:
224                 ret = ieee80211_prep_tdls_encap_data(wiphy, dev, peer,
225                                                      action_code, dialog_token,
226                                                      status_code, skb);
227                 send_direct = false;
228                 break;
229         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
230                 ret = ieee80211_prep_tdls_direct(wiphy, dev, peer, action_code,
231                                                  dialog_token, status_code,
232                                                  skb);
233                 send_direct = true;
234                 break;
235         default:
236                 ret = -ENOTSUPP;
237                 break;
238         }
239
240         if (ret < 0)
241                 goto fail;
242
243         if (extra_ies_len)
244                 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
245
246         /* sanity check for initiator */
247         switch (action_code) {
248         case WLAN_TDLS_SETUP_REQUEST:
249         case WLAN_TDLS_SETUP_CONFIRM:
250         case WLAN_TDLS_DISCOVERY_REQUEST:
251                 if (!initiator) {
252                         ret = -EINVAL;
253                         goto fail;
254                 }
255                 break;
256         case WLAN_TDLS_SETUP_RESPONSE:
257         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
258                 if (initiator) {
259                         ret = -EINVAL;
260                         goto fail;
261                 }
262                 break;
263         case WLAN_TDLS_TEARDOWN:
264                 /* any value is ok */
265                 break;
266         default:
267                 ret = -ENOTSUPP;
268                 goto fail;
269         }
270
271         if (initiator) {
272                 init_addr = sdata->vif.addr;
273                 rsp_addr = peer;
274         } else {
275                 init_addr = peer;
276                 rsp_addr = sdata->vif.addr;
277         }
278
279         ieee80211_tdls_add_link_ie(skb, init_addr, rsp_addr,
280                                    sdata->u.mgd.bssid);
281
282         if (send_direct) {
283                 ieee80211_tx_skb(sdata, skb);
284                 return 0;
285         }
286
287         /*
288          * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
289          * we should default to AC_VI.
290          */
291         switch (action_code) {
292         case WLAN_TDLS_SETUP_REQUEST:
293         case WLAN_TDLS_SETUP_RESPONSE:
294                 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
295                 skb->priority = 2;
296                 break;
297         default:
298                 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
299                 skb->priority = 5;
300                 break;
301         }
302
303         /* disable bottom halves when entering the Tx path */
304         local_bh_disable();
305         ret = ieee80211_subif_start_xmit(skb, dev);
306         local_bh_enable();
307
308         return ret;
309
310 fail:
311         dev_kfree_skb(skb);
312         return ret;
313 }
314
315 int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
316                         const u8 *peer, u8 action_code, u8 dialog_token,
317                         u16 status_code, u32 peer_capability,
318                         bool initiator, const u8 *extra_ies,
319                         size_t extra_ies_len)
320 {
321         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
322         struct ieee80211_local *local = sdata->local;
323         int ret;
324
325         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
326                 return -ENOTSUPP;
327
328         /* make sure we are in managed mode, and associated */
329         if (sdata->vif.type != NL80211_IFTYPE_STATION ||
330             !sdata->u.mgd.associated)
331                 return -EINVAL;
332
333         mutex_lock(&local->mtx);
334
335         /* we don't support concurrent TDLS peer setups */
336         if (!is_zero_ether_addr(sdata->tdls_peer) &&
337             !ether_addr_equal(sdata->tdls_peer, peer) &&
338             (action_code == WLAN_TDLS_SETUP_REQUEST ||
339              action_code == WLAN_TDLS_SETUP_RESPONSE)) {
340                 ret = -EBUSY;
341                 goto exit;
342         }
343
344         ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
345                                               dialog_token, status_code,
346                                               peer_capability, initiator,
347                                               extra_ies, extra_ies_len);
348         if (ret < 0)
349                 goto exit;
350
351         if (action_code == WLAN_TDLS_SETUP_REQUEST ||
352             action_code == WLAN_TDLS_SETUP_RESPONSE) {
353                 memcpy(sdata->tdls_peer, peer, ETH_ALEN);
354                 ieee80211_queue_delayed_work(&sdata->local->hw,
355                                              &sdata->tdls_peer_del_work,
356                                              TDLS_PEER_SETUP_TIMEOUT);
357         }
358
359 exit:
360         mutex_unlock(&local->mtx);
361
362         tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
363                  action_code, peer, ret);
364         return ret;
365 }
366
367 int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
368                         const u8 *peer, enum nl80211_tdls_operation oper)
369 {
370         struct sta_info *sta;
371         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
372         struct ieee80211_local *local = sdata->local;
373         int ret;
374
375         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
376                 return -ENOTSUPP;
377
378         if (sdata->vif.type != NL80211_IFTYPE_STATION)
379                 return -EINVAL;
380
381         switch (oper) {
382         case NL80211_TDLS_ENABLE_LINK:
383         case NL80211_TDLS_DISABLE_LINK:
384                 break;
385         case NL80211_TDLS_TEARDOWN:
386         case NL80211_TDLS_SETUP:
387         case NL80211_TDLS_DISCOVERY_REQ:
388                 /* We don't support in-driver setup/teardown/discovery */
389                 return -ENOTSUPP;
390         }
391
392         mutex_lock(&local->mtx);
393         tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
394
395         switch (oper) {
396         case NL80211_TDLS_ENABLE_LINK:
397                 rcu_read_lock();
398                 sta = sta_info_get(sdata, peer);
399                 if (!sta) {
400                         rcu_read_unlock();
401                         ret = -ENOLINK;
402                         break;
403                 }
404
405                 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
406                 rcu_read_unlock();
407
408                 WARN_ON_ONCE(is_zero_ether_addr(sdata->tdls_peer) ||
409                              !ether_addr_equal(sdata->tdls_peer, peer));
410                 ret = 0;
411                 break;
412         case NL80211_TDLS_DISABLE_LINK:
413                 ret = sta_info_destroy_addr(sdata, peer);
414                 break;
415         default:
416                 ret = -ENOTSUPP;
417                 break;
418         }
419
420         if (ret == 0 && ether_addr_equal(sdata->tdls_peer, peer)) {
421                 cancel_delayed_work(&sdata->tdls_peer_del_work);
422                 eth_zero_addr(sdata->tdls_peer);
423         }
424
425         mutex_unlock(&local->mtx);
426         return ret;
427 }