GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / wireless / util.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Wireless utility functions
4  *
5  * Copyright 2007-2009  Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2017       Intel Deutschland GmbH
8  */
9 #include <linux/export.h>
10 #include <linux/bitops.h>
11 #include <linux/etherdevice.h>
12 #include <linux/slab.h>
13 #include <net/cfg80211.h>
14 #include <net/ip.h>
15 #include <net/dsfield.h>
16 #include <linux/if_vlan.h>
17 #include <linux/mpls.h>
18 #include <linux/gcd.h>
19 #include "core.h"
20 #include "rdev-ops.h"
21
22
23 struct ieee80211_rate *
24 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
25                             u32 basic_rates, int bitrate)
26 {
27         struct ieee80211_rate *result = &sband->bitrates[0];
28         int i;
29
30         for (i = 0; i < sband->n_bitrates; i++) {
31                 if (!(basic_rates & BIT(i)))
32                         continue;
33                 if (sband->bitrates[i].bitrate > bitrate)
34                         continue;
35                 result = &sband->bitrates[i];
36         }
37
38         return result;
39 }
40 EXPORT_SYMBOL(ieee80211_get_response_rate);
41
42 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
43                               enum nl80211_bss_scan_width scan_width)
44 {
45         struct ieee80211_rate *bitrates;
46         u32 mandatory_rates = 0;
47         enum ieee80211_rate_flags mandatory_flag;
48         int i;
49
50         if (WARN_ON(!sband))
51                 return 1;
52
53         if (sband->band == NL80211_BAND_2GHZ) {
54                 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
55                     scan_width == NL80211_BSS_CHAN_WIDTH_10)
56                         mandatory_flag = IEEE80211_RATE_MANDATORY_G;
57                 else
58                         mandatory_flag = IEEE80211_RATE_MANDATORY_B;
59         } else {
60                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
61         }
62
63         bitrates = sband->bitrates;
64         for (i = 0; i < sband->n_bitrates; i++)
65                 if (bitrates[i].flags & mandatory_flag)
66                         mandatory_rates |= BIT(i);
67         return mandatory_rates;
68 }
69 EXPORT_SYMBOL(ieee80211_mandatory_rates);
70
71 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
72 {
73         /* see 802.11 17.3.8.3.2 and Annex J
74          * there are overlapping channel numbers in 5GHz and 2GHz bands */
75         if (chan <= 0)
76                 return 0; /* not supported */
77         switch (band) {
78         case NL80211_BAND_2GHZ:
79                 if (chan == 14)
80                         return 2484;
81                 else if (chan < 14)
82                         return 2407 + chan * 5;
83                 break;
84         case NL80211_BAND_5GHZ:
85                 if (chan >= 182 && chan <= 196)
86                         return 4000 + chan * 5;
87                 else
88                         return 5000 + chan * 5;
89                 break;
90         case NL80211_BAND_60GHZ:
91                 if (chan < 5)
92                         return 56160 + chan * 2160;
93                 break;
94         default:
95                 ;
96         }
97         return 0; /* not supported */
98 }
99 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
100
101 int ieee80211_frequency_to_channel(int freq)
102 {
103         /* see 802.11 17.3.8.3.2 and Annex J */
104         if (freq == 2484)
105                 return 14;
106         else if (freq < 2484)
107                 return (freq - 2407) / 5;
108         else if (freq >= 4910 && freq <= 4980)
109                 return (freq - 4000) / 5;
110         else if (freq <= 45000) /* DMG band lower limit */
111                 return (freq - 5000) / 5;
112         else if (freq >= 58320 && freq <= 64800)
113                 return (freq - 56160) / 2160;
114         else
115                 return 0;
116 }
117 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
118
119 struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
120 {
121         enum nl80211_band band;
122         struct ieee80211_supported_band *sband;
123         int i;
124
125         for (band = 0; band < NUM_NL80211_BANDS; band++) {
126                 sband = wiphy->bands[band];
127
128                 if (!sband)
129                         continue;
130
131                 for (i = 0; i < sband->n_channels; i++) {
132                         if (sband->channels[i].center_freq == freq)
133                                 return &sband->channels[i];
134                 }
135         }
136
137         return NULL;
138 }
139 EXPORT_SYMBOL(ieee80211_get_channel);
140
141 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
142 {
143         int i, want;
144
145         switch (sband->band) {
146         case NL80211_BAND_5GHZ:
147                 want = 3;
148                 for (i = 0; i < sband->n_bitrates; i++) {
149                         if (sband->bitrates[i].bitrate == 60 ||
150                             sband->bitrates[i].bitrate == 120 ||
151                             sband->bitrates[i].bitrate == 240) {
152                                 sband->bitrates[i].flags |=
153                                         IEEE80211_RATE_MANDATORY_A;
154                                 want--;
155                         }
156                 }
157                 WARN_ON(want);
158                 break;
159         case NL80211_BAND_2GHZ:
160                 want = 7;
161                 for (i = 0; i < sband->n_bitrates; i++) {
162                         switch (sband->bitrates[i].bitrate) {
163                         case 10:
164                         case 20:
165                         case 55:
166                         case 110:
167                                 sband->bitrates[i].flags |=
168                                         IEEE80211_RATE_MANDATORY_B |
169                                         IEEE80211_RATE_MANDATORY_G;
170                                 want--;
171                                 break;
172                         case 60:
173                         case 120:
174                         case 240:
175                                 sband->bitrates[i].flags |=
176                                         IEEE80211_RATE_MANDATORY_G;
177                                 want--;
178                                 /* fall through */
179                         default:
180                                 sband->bitrates[i].flags |=
181                                         IEEE80211_RATE_ERP_G;
182                                 break;
183                         }
184                 }
185                 WARN_ON(want != 0 && want != 3);
186                 break;
187         case NL80211_BAND_60GHZ:
188                 /* check for mandatory HT MCS 1..4 */
189                 WARN_ON(!sband->ht_cap.ht_supported);
190                 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
191                 break;
192         case NUM_NL80211_BANDS:
193         default:
194                 WARN_ON(1);
195                 break;
196         }
197 }
198
199 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
200 {
201         enum nl80211_band band;
202
203         for (band = 0; band < NUM_NL80211_BANDS; band++)
204                 if (wiphy->bands[band])
205                         set_mandatory_flags_band(wiphy->bands[band]);
206 }
207
208 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
209 {
210         int i;
211         for (i = 0; i < wiphy->n_cipher_suites; i++)
212                 if (cipher == wiphy->cipher_suites[i])
213                         return true;
214         return false;
215 }
216
217 static bool
218 cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
219 {
220         struct wiphy *wiphy = &rdev->wiphy;
221         int i;
222
223         for (i = 0; i < wiphy->n_cipher_suites; i++) {
224                 switch (wiphy->cipher_suites[i]) {
225                 case WLAN_CIPHER_SUITE_AES_CMAC:
226                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
227                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
228                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
229                         return true;
230                 }
231         }
232
233         return false;
234 }
235
236 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
237                             int key_idx, bool pairwise)
238 {
239         int max_key_idx;
240
241         if (pairwise)
242                 max_key_idx = 3;
243         else if (cfg80211_igtk_cipher_supported(rdev))
244                 max_key_idx = 5;
245         else
246                 max_key_idx = 3;
247
248         if (key_idx < 0 || key_idx > max_key_idx)
249                 return false;
250
251         return true;
252 }
253
254 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
255                                    struct key_params *params, int key_idx,
256                                    bool pairwise, const u8 *mac_addr)
257 {
258         if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
259                 return -EINVAL;
260
261         if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
262                 return -EINVAL;
263
264         if (pairwise && !mac_addr)
265                 return -EINVAL;
266
267         switch (params->cipher) {
268         case WLAN_CIPHER_SUITE_TKIP:
269         case WLAN_CIPHER_SUITE_CCMP:
270         case WLAN_CIPHER_SUITE_CCMP_256:
271         case WLAN_CIPHER_SUITE_GCMP:
272         case WLAN_CIPHER_SUITE_GCMP_256:
273                 /* Disallow pairwise keys with non-zero index unless it's WEP
274                  * or a vendor specific cipher (because current deployments use
275                  * pairwise WEP keys with non-zero indices and for vendor
276                  * specific ciphers this should be validated in the driver or
277                  * hardware level - but 802.11i clearly specifies to use zero)
278                  */
279                 if (pairwise && key_idx)
280                         return -EINVAL;
281                 break;
282         case WLAN_CIPHER_SUITE_AES_CMAC:
283         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
284         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
285         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
286                 /* Disallow BIP (group-only) cipher as pairwise cipher */
287                 if (pairwise)
288                         return -EINVAL;
289                 if (key_idx < 4)
290                         return -EINVAL;
291                 break;
292         case WLAN_CIPHER_SUITE_WEP40:
293         case WLAN_CIPHER_SUITE_WEP104:
294                 if (key_idx > 3)
295                         return -EINVAL;
296         default:
297                 break;
298         }
299
300         switch (params->cipher) {
301         case WLAN_CIPHER_SUITE_WEP40:
302                 if (params->key_len != WLAN_KEY_LEN_WEP40)
303                         return -EINVAL;
304                 break;
305         case WLAN_CIPHER_SUITE_TKIP:
306                 if (params->key_len != WLAN_KEY_LEN_TKIP)
307                         return -EINVAL;
308                 break;
309         case WLAN_CIPHER_SUITE_CCMP:
310                 if (params->key_len != WLAN_KEY_LEN_CCMP)
311                         return -EINVAL;
312                 break;
313         case WLAN_CIPHER_SUITE_CCMP_256:
314                 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
315                         return -EINVAL;
316                 break;
317         case WLAN_CIPHER_SUITE_GCMP:
318                 if (params->key_len != WLAN_KEY_LEN_GCMP)
319                         return -EINVAL;
320                 break;
321         case WLAN_CIPHER_SUITE_GCMP_256:
322                 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
323                         return -EINVAL;
324                 break;
325         case WLAN_CIPHER_SUITE_WEP104:
326                 if (params->key_len != WLAN_KEY_LEN_WEP104)
327                         return -EINVAL;
328                 break;
329         case WLAN_CIPHER_SUITE_AES_CMAC:
330                 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
331                         return -EINVAL;
332                 break;
333         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
334                 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
335                         return -EINVAL;
336                 break;
337         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
338                 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
339                         return -EINVAL;
340                 break;
341         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
342                 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
343                         return -EINVAL;
344                 break;
345         default:
346                 /*
347                  * We don't know anything about this algorithm,
348                  * allow using it -- but the driver must check
349                  * all parameters! We still check below whether
350                  * or not the driver supports this algorithm,
351                  * of course.
352                  */
353                 break;
354         }
355
356         if (params->seq) {
357                 switch (params->cipher) {
358                 case WLAN_CIPHER_SUITE_WEP40:
359                 case WLAN_CIPHER_SUITE_WEP104:
360                         /* These ciphers do not use key sequence */
361                         return -EINVAL;
362                 case WLAN_CIPHER_SUITE_TKIP:
363                 case WLAN_CIPHER_SUITE_CCMP:
364                 case WLAN_CIPHER_SUITE_CCMP_256:
365                 case WLAN_CIPHER_SUITE_GCMP:
366                 case WLAN_CIPHER_SUITE_GCMP_256:
367                 case WLAN_CIPHER_SUITE_AES_CMAC:
368                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
369                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
370                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
371                         if (params->seq_len != 6)
372                                 return -EINVAL;
373                         break;
374                 }
375         }
376
377         if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
378                 return -EINVAL;
379
380         return 0;
381 }
382
383 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
384 {
385         unsigned int hdrlen = 24;
386
387         if (ieee80211_is_data(fc)) {
388                 if (ieee80211_has_a4(fc))
389                         hdrlen = 30;
390                 if (ieee80211_is_data_qos(fc)) {
391                         hdrlen += IEEE80211_QOS_CTL_LEN;
392                         if (ieee80211_has_order(fc))
393                                 hdrlen += IEEE80211_HT_CTL_LEN;
394                 }
395                 goto out;
396         }
397
398         if (ieee80211_is_mgmt(fc)) {
399                 if (ieee80211_has_order(fc))
400                         hdrlen += IEEE80211_HT_CTL_LEN;
401                 goto out;
402         }
403
404         if (ieee80211_is_ctl(fc)) {
405                 /*
406                  * ACK and CTS are 10 bytes, all others 16. To see how
407                  * to get this condition consider
408                  *   subtype mask:   0b0000000011110000 (0x00F0)
409                  *   ACK subtype:    0b0000000011010000 (0x00D0)
410                  *   CTS subtype:    0b0000000011000000 (0x00C0)
411                  *   bits that matter:         ^^^      (0x00E0)
412                  *   value of those: 0b0000000011000000 (0x00C0)
413                  */
414                 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
415                         hdrlen = 10;
416                 else
417                         hdrlen = 16;
418         }
419 out:
420         return hdrlen;
421 }
422 EXPORT_SYMBOL(ieee80211_hdrlen);
423
424 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
425 {
426         const struct ieee80211_hdr *hdr =
427                         (const struct ieee80211_hdr *)skb->data;
428         unsigned int hdrlen;
429
430         if (unlikely(skb->len < 10))
431                 return 0;
432         hdrlen = ieee80211_hdrlen(hdr->frame_control);
433         if (unlikely(hdrlen > skb->len))
434                 return 0;
435         return hdrlen;
436 }
437 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
438
439 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
440 {
441         int ae = flags & MESH_FLAGS_AE;
442         /* 802.11-2012, 8.2.4.7.3 */
443         switch (ae) {
444         default:
445         case 0:
446                 return 6;
447         case MESH_FLAGS_AE_A4:
448                 return 12;
449         case MESH_FLAGS_AE_A5_A6:
450                 return 18;
451         }
452 }
453
454 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
455 {
456         return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
457 }
458 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
459
460 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
461                                   const u8 *addr, enum nl80211_iftype iftype,
462                                   u8 data_offset, bool is_amsdu)
463 {
464         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
465         struct {
466                 u8 hdr[ETH_ALEN] __aligned(2);
467                 __be16 proto;
468         } payload;
469         struct ethhdr tmp;
470         u16 hdrlen;
471         u8 mesh_flags = 0;
472
473         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
474                 return -1;
475
476         hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
477         if (skb->len < hdrlen + 8)
478                 return -1;
479
480         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
481          * header
482          * IEEE 802.11 address fields:
483          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
484          *   0     0   DA    SA    BSSID n/a
485          *   0     1   DA    BSSID SA    n/a
486          *   1     0   BSSID SA    DA    n/a
487          *   1     1   RA    TA    DA    SA
488          */
489         memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
490         memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
491
492         if (iftype == NL80211_IFTYPE_MESH_POINT)
493                 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
494
495         mesh_flags &= MESH_FLAGS_AE;
496
497         switch (hdr->frame_control &
498                 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
499         case cpu_to_le16(IEEE80211_FCTL_TODS):
500                 if (unlikely(iftype != NL80211_IFTYPE_AP &&
501                              iftype != NL80211_IFTYPE_AP_VLAN &&
502                              iftype != NL80211_IFTYPE_P2P_GO))
503                         return -1;
504                 break;
505         case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
506                 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
507                              iftype != NL80211_IFTYPE_MESH_POINT &&
508                              iftype != NL80211_IFTYPE_AP_VLAN &&
509                              iftype != NL80211_IFTYPE_STATION))
510                         return -1;
511                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
512                         if (mesh_flags == MESH_FLAGS_AE_A4)
513                                 return -1;
514                         if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
515                                 skb_copy_bits(skb, hdrlen +
516                                         offsetof(struct ieee80211s_hdr, eaddr1),
517                                         tmp.h_dest, 2 * ETH_ALEN);
518                         }
519                         hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
520                 }
521                 break;
522         case cpu_to_le16(IEEE80211_FCTL_FROMDS):
523                 if ((iftype != NL80211_IFTYPE_STATION &&
524                      iftype != NL80211_IFTYPE_P2P_CLIENT &&
525                      iftype != NL80211_IFTYPE_MESH_POINT) ||
526                     (is_multicast_ether_addr(tmp.h_dest) &&
527                      ether_addr_equal(tmp.h_source, addr)))
528                         return -1;
529                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
530                         if (mesh_flags == MESH_FLAGS_AE_A5_A6)
531                                 return -1;
532                         if (mesh_flags == MESH_FLAGS_AE_A4)
533                                 skb_copy_bits(skb, hdrlen +
534                                         offsetof(struct ieee80211s_hdr, eaddr1),
535                                         tmp.h_source, ETH_ALEN);
536                         hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
537                 }
538                 break;
539         case cpu_to_le16(0):
540                 if (iftype != NL80211_IFTYPE_ADHOC &&
541                     iftype != NL80211_IFTYPE_STATION &&
542                     iftype != NL80211_IFTYPE_OCB)
543                                 return -1;
544                 break;
545         }
546
547         skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
548         tmp.h_proto = payload.proto;
549
550         if (likely((!is_amsdu && ether_addr_equal(payload.hdr, rfc1042_header) &&
551                     tmp.h_proto != htons(ETH_P_AARP) &&
552                     tmp.h_proto != htons(ETH_P_IPX)) ||
553                    ether_addr_equal(payload.hdr, bridge_tunnel_header)))
554                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
555                  * replace EtherType */
556                 hdrlen += ETH_ALEN + 2;
557         else
558                 tmp.h_proto = htons(skb->len - hdrlen);
559
560         pskb_pull(skb, hdrlen);
561
562         if (!ehdr)
563                 ehdr = skb_push(skb, sizeof(struct ethhdr));
564         memcpy(ehdr, &tmp, sizeof(tmp));
565
566         return 0;
567 }
568 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
569
570 static void
571 __frame_add_frag(struct sk_buff *skb, struct page *page,
572                  void *ptr, int len, int size)
573 {
574         struct skb_shared_info *sh = skb_shinfo(skb);
575         int page_offset;
576
577         get_page(page);
578         page_offset = ptr - page_address(page);
579         skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
580 }
581
582 static void
583 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
584                             int offset, int len)
585 {
586         struct skb_shared_info *sh = skb_shinfo(skb);
587         const skb_frag_t *frag = &sh->frags[0];
588         struct page *frag_page;
589         void *frag_ptr;
590         int frag_len, frag_size;
591         int head_size = skb->len - skb->data_len;
592         int cur_len;
593
594         frag_page = virt_to_head_page(skb->head);
595         frag_ptr = skb->data;
596         frag_size = head_size;
597
598         while (offset >= frag_size) {
599                 offset -= frag_size;
600                 frag_page = skb_frag_page(frag);
601                 frag_ptr = skb_frag_address(frag);
602                 frag_size = skb_frag_size(frag);
603                 frag++;
604         }
605
606         frag_ptr += offset;
607         frag_len = frag_size - offset;
608
609         cur_len = min(len, frag_len);
610
611         __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
612         len -= cur_len;
613
614         while (len > 0) {
615                 frag_len = skb_frag_size(frag);
616                 cur_len = min(len, frag_len);
617                 __frame_add_frag(frame, skb_frag_page(frag),
618                                  skb_frag_address(frag), cur_len, frag_len);
619                 len -= cur_len;
620                 frag++;
621         }
622 }
623
624 static struct sk_buff *
625 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
626                        int offset, int len, bool reuse_frag)
627 {
628         struct sk_buff *frame;
629         int cur_len = len;
630
631         if (skb->len - offset < len)
632                 return NULL;
633
634         /*
635          * When reusing framents, copy some data to the head to simplify
636          * ethernet header handling and speed up protocol header processing
637          * in the stack later.
638          */
639         if (reuse_frag)
640                 cur_len = min_t(int, len, 32);
641
642         /*
643          * Allocate and reserve two bytes more for payload
644          * alignment since sizeof(struct ethhdr) is 14.
645          */
646         frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
647         if (!frame)
648                 return NULL;
649
650         skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
651         skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
652
653         len -= cur_len;
654         if (!len)
655                 return frame;
656
657         offset += cur_len;
658         __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
659
660         return frame;
661 }
662
663 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
664                               const u8 *addr, enum nl80211_iftype iftype,
665                               const unsigned int extra_headroom,
666                               const u8 *check_da, const u8 *check_sa)
667 {
668         unsigned int hlen = ALIGN(extra_headroom, 4);
669         struct sk_buff *frame = NULL;
670         u16 ethertype;
671         u8 *payload;
672         int offset = 0, remaining;
673         struct ethhdr eth;
674         bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
675         bool reuse_skb = false;
676         bool last = false;
677
678         while (!last) {
679                 unsigned int subframe_len;
680                 int len;
681                 u8 padding;
682
683                 skb_copy_bits(skb, offset, &eth, sizeof(eth));
684                 len = ntohs(eth.h_proto);
685                 subframe_len = sizeof(struct ethhdr) + len;
686                 padding = (4 - subframe_len) & 0x3;
687
688                 /* the last MSDU has no padding */
689                 remaining = skb->len - offset;
690                 if (subframe_len > remaining)
691                         goto purge;
692                 /* mitigate A-MSDU aggregation injection attacks */
693                 if (ether_addr_equal(eth.h_dest, rfc1042_header))
694                         goto purge;
695
696                 offset += sizeof(struct ethhdr);
697                 last = remaining <= subframe_len + padding;
698
699                 /* FIXME: should we really accept multicast DA? */
700                 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
701                      !ether_addr_equal(check_da, eth.h_dest)) ||
702                     (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
703                         offset += len + padding;
704                         continue;
705                 }
706
707                 /* reuse skb for the last subframe */
708                 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
709                         skb_pull(skb, offset);
710                         frame = skb;
711                         reuse_skb = true;
712                 } else {
713                         frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
714                                                        reuse_frag);
715                         if (!frame)
716                                 goto purge;
717
718                         offset += len + padding;
719                 }
720
721                 skb_reset_network_header(frame);
722                 frame->dev = skb->dev;
723                 frame->priority = skb->priority;
724
725                 payload = frame->data;
726                 ethertype = (payload[6] << 8) | payload[7];
727                 if (likely((ether_addr_equal(payload, rfc1042_header) &&
728                             ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
729                            ether_addr_equal(payload, bridge_tunnel_header))) {
730                         eth.h_proto = htons(ethertype);
731                         skb_pull(frame, ETH_ALEN + 2);
732                 }
733
734                 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
735                 __skb_queue_tail(list, frame);
736         }
737
738         if (!reuse_skb)
739                 dev_kfree_skb(skb);
740
741         return;
742
743  purge:
744         __skb_queue_purge(list);
745         dev_kfree_skb(skb);
746 }
747 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
748
749 /* Given a data frame determine the 802.1p/1d tag to use. */
750 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
751                                     struct cfg80211_qos_map *qos_map)
752 {
753         unsigned int dscp;
754         unsigned char vlan_priority;
755
756         /* skb->priority values from 256->263 are magic values to
757          * directly indicate a specific 802.1d priority.  This is used
758          * to allow 802.1d priority to be passed directly in from VLAN
759          * tags, etc.
760          */
761         if (skb->priority >= 256 && skb->priority <= 263)
762                 return skb->priority - 256;
763
764         if (skb_vlan_tag_present(skb)) {
765                 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
766                         >> VLAN_PRIO_SHIFT;
767                 if (vlan_priority > 0)
768                         return vlan_priority;
769         }
770
771         switch (skb->protocol) {
772         case htons(ETH_P_IP):
773                 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
774                 break;
775         case htons(ETH_P_IPV6):
776                 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
777                 break;
778         case htons(ETH_P_MPLS_UC):
779         case htons(ETH_P_MPLS_MC): {
780                 struct mpls_label mpls_tmp, *mpls;
781
782                 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
783                                           sizeof(*mpls), &mpls_tmp);
784                 if (!mpls)
785                         return 0;
786
787                 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
788                         >> MPLS_LS_TC_SHIFT;
789         }
790         case htons(ETH_P_80221):
791                 /* 802.21 is always network control traffic */
792                 return 7;
793         default:
794                 return 0;
795         }
796
797         if (qos_map) {
798                 unsigned int i, tmp_dscp = dscp >> 2;
799
800                 for (i = 0; i < qos_map->num_des; i++) {
801                         if (tmp_dscp == qos_map->dscp_exception[i].dscp)
802                                 return qos_map->dscp_exception[i].up;
803                 }
804
805                 for (i = 0; i < 8; i++) {
806                         if (tmp_dscp >= qos_map->up[i].low &&
807                             tmp_dscp <= qos_map->up[i].high)
808                                 return i;
809                 }
810         }
811
812         return dscp >> 5;
813 }
814 EXPORT_SYMBOL(cfg80211_classify8021d);
815
816 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
817 {
818         const struct cfg80211_bss_ies *ies;
819
820         ies = rcu_dereference(bss->ies);
821         if (!ies)
822                 return NULL;
823
824         return cfg80211_find_ie(ie, ies->data, ies->len);
825 }
826 EXPORT_SYMBOL(ieee80211_bss_get_ie);
827
828 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
829 {
830         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
831         struct net_device *dev = wdev->netdev;
832         int i;
833
834         if (!wdev->connect_keys)
835                 return;
836
837         for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
838                 if (!wdev->connect_keys->params[i].cipher)
839                         continue;
840                 if (rdev_add_key(rdev, dev, i, false, NULL,
841                                  &wdev->connect_keys->params[i])) {
842                         netdev_err(dev, "failed to set key %d\n", i);
843                         continue;
844                 }
845                 if (wdev->connect_keys->def == i &&
846                     rdev_set_default_key(rdev, dev, i, true, true)) {
847                         netdev_err(dev, "failed to set defkey %d\n", i);
848                         continue;
849                 }
850         }
851
852         kzfree(wdev->connect_keys);
853         wdev->connect_keys = NULL;
854 }
855
856 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
857 {
858         struct cfg80211_event *ev;
859         unsigned long flags;
860
861         spin_lock_irqsave(&wdev->event_lock, flags);
862         while (!list_empty(&wdev->event_list)) {
863                 ev = list_first_entry(&wdev->event_list,
864                                       struct cfg80211_event, list);
865                 list_del(&ev->list);
866                 spin_unlock_irqrestore(&wdev->event_lock, flags);
867
868                 wdev_lock(wdev);
869                 switch (ev->type) {
870                 case EVENT_CONNECT_RESULT:
871                         __cfg80211_connect_result(
872                                 wdev->netdev,
873                                 &ev->cr,
874                                 ev->cr.status == WLAN_STATUS_SUCCESS);
875                         break;
876                 case EVENT_ROAMED:
877                         __cfg80211_roamed(wdev, &ev->rm);
878                         break;
879                 case EVENT_DISCONNECTED:
880                         __cfg80211_disconnected(wdev->netdev,
881                                                 ev->dc.ie, ev->dc.ie_len,
882                                                 ev->dc.reason,
883                                                 !ev->dc.locally_generated);
884                         break;
885                 case EVENT_IBSS_JOINED:
886                         __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
887                                                ev->ij.channel);
888                         break;
889                 case EVENT_STOPPED:
890                         __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
891                         break;
892                 case EVENT_PORT_AUTHORIZED:
893                         __cfg80211_port_authorized(wdev, ev->pa.bssid);
894                         break;
895                 }
896                 wdev_unlock(wdev);
897
898                 kfree(ev);
899
900                 spin_lock_irqsave(&wdev->event_lock, flags);
901         }
902         spin_unlock_irqrestore(&wdev->event_lock, flags);
903 }
904
905 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
906 {
907         struct wireless_dev *wdev;
908
909         ASSERT_RTNL();
910
911         list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
912                 cfg80211_process_wdev_events(wdev);
913 }
914
915 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
916                           struct net_device *dev, enum nl80211_iftype ntype,
917                           struct vif_params *params)
918 {
919         int err;
920         enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
921
922         ASSERT_RTNL();
923
924         /* don't support changing VLANs, you just re-create them */
925         if (otype == NL80211_IFTYPE_AP_VLAN)
926                 return -EOPNOTSUPP;
927
928         /* cannot change into P2P device or NAN */
929         if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
930             ntype == NL80211_IFTYPE_NAN)
931                 return -EOPNOTSUPP;
932
933         if (!rdev->ops->change_virtual_intf ||
934             !(rdev->wiphy.interface_modes & (1 << ntype)))
935                 return -EOPNOTSUPP;
936
937         /* if it's part of a bridge, reject changing type to station/ibss */
938         if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
939             (ntype == NL80211_IFTYPE_ADHOC ||
940              ntype == NL80211_IFTYPE_STATION ||
941              ntype == NL80211_IFTYPE_P2P_CLIENT))
942                 return -EBUSY;
943
944         if (ntype != otype) {
945                 dev->ieee80211_ptr->use_4addr = false;
946                 dev->ieee80211_ptr->mesh_id_up_len = 0;
947                 wdev_lock(dev->ieee80211_ptr);
948                 rdev_set_qos_map(rdev, dev, NULL);
949                 wdev_unlock(dev->ieee80211_ptr);
950
951                 switch (otype) {
952                 case NL80211_IFTYPE_AP:
953                 case NL80211_IFTYPE_P2P_GO:
954                         cfg80211_stop_ap(rdev, dev, true);
955                         break;
956                 case NL80211_IFTYPE_ADHOC:
957                         cfg80211_leave_ibss(rdev, dev, false);
958                         break;
959                 case NL80211_IFTYPE_STATION:
960                 case NL80211_IFTYPE_P2P_CLIENT:
961                         wdev_lock(dev->ieee80211_ptr);
962                         cfg80211_disconnect(rdev, dev,
963                                             WLAN_REASON_DEAUTH_LEAVING, true);
964                         wdev_unlock(dev->ieee80211_ptr);
965                         break;
966                 case NL80211_IFTYPE_MESH_POINT:
967                         /* mesh should be handled? */
968                         break;
969                 case NL80211_IFTYPE_OCB:
970                         cfg80211_leave_ocb(rdev, dev);
971                         break;
972                 default:
973                         break;
974                 }
975
976                 cfg80211_process_rdev_events(rdev);
977                 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
978         }
979
980         err = rdev_change_virtual_intf(rdev, dev, ntype, params);
981
982         WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
983
984         if (!err && params && params->use_4addr != -1)
985                 dev->ieee80211_ptr->use_4addr = params->use_4addr;
986
987         if (!err) {
988                 dev->priv_flags &= ~IFF_DONT_BRIDGE;
989                 switch (ntype) {
990                 case NL80211_IFTYPE_STATION:
991                         if (dev->ieee80211_ptr->use_4addr)
992                                 break;
993                         /* fall through */
994                 case NL80211_IFTYPE_OCB:
995                 case NL80211_IFTYPE_P2P_CLIENT:
996                 case NL80211_IFTYPE_ADHOC:
997                         dev->priv_flags |= IFF_DONT_BRIDGE;
998                         break;
999                 case NL80211_IFTYPE_P2P_GO:
1000                 case NL80211_IFTYPE_AP:
1001                 case NL80211_IFTYPE_AP_VLAN:
1002                 case NL80211_IFTYPE_WDS:
1003                 case NL80211_IFTYPE_MESH_POINT:
1004                         /* bridging OK */
1005                         break;
1006                 case NL80211_IFTYPE_MONITOR:
1007                         /* monitor can't bridge anyway */
1008                         break;
1009                 case NL80211_IFTYPE_UNSPECIFIED:
1010                 case NUM_NL80211_IFTYPES:
1011                         /* not happening */
1012                         break;
1013                 case NL80211_IFTYPE_P2P_DEVICE:
1014                 case NL80211_IFTYPE_NAN:
1015                         WARN_ON(1);
1016                         break;
1017                 }
1018         }
1019
1020         if (!err && ntype != otype && netif_running(dev)) {
1021                 cfg80211_update_iface_num(rdev, ntype, 1);
1022                 cfg80211_update_iface_num(rdev, otype, -1);
1023         }
1024
1025         return err;
1026 }
1027
1028 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1029 {
1030         int modulation, streams, bitrate;
1031
1032         /* the formula below does only work for MCS values smaller than 32 */
1033         if (WARN_ON_ONCE(rate->mcs >= 32))
1034                 return 0;
1035
1036         modulation = rate->mcs & 7;
1037         streams = (rate->mcs >> 3) + 1;
1038
1039         bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1040
1041         if (modulation < 4)
1042                 bitrate *= (modulation + 1);
1043         else if (modulation == 4)
1044                 bitrate *= (modulation + 2);
1045         else
1046                 bitrate *= (modulation + 3);
1047
1048         bitrate *= streams;
1049
1050         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1051                 bitrate = (bitrate / 9) * 10;
1052
1053         /* do NOT round down here */
1054         return (bitrate + 50000) / 100000;
1055 }
1056
1057 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1058 {
1059         static const u32 __mcs2bitrate[] = {
1060                 /* control PHY */
1061                 [0] =   275,
1062                 /* SC PHY */
1063                 [1] =  3850,
1064                 [2] =  7700,
1065                 [3] =  9625,
1066                 [4] = 11550,
1067                 [5] = 12512, /* 1251.25 mbps */
1068                 [6] = 15400,
1069                 [7] = 19250,
1070                 [8] = 23100,
1071                 [9] = 25025,
1072                 [10] = 30800,
1073                 [11] = 38500,
1074                 [12] = 46200,
1075                 /* OFDM PHY */
1076                 [13] =  6930,
1077                 [14] =  8662, /* 866.25 mbps */
1078                 [15] = 13860,
1079                 [16] = 17325,
1080                 [17] = 20790,
1081                 [18] = 27720,
1082                 [19] = 34650,
1083                 [20] = 41580,
1084                 [21] = 45045,
1085                 [22] = 51975,
1086                 [23] = 62370,
1087                 [24] = 67568, /* 6756.75 mbps */
1088                 /* LP-SC PHY */
1089                 [25] =  6260,
1090                 [26] =  8340,
1091                 [27] = 11120,
1092                 [28] = 12510,
1093                 [29] = 16680,
1094                 [30] = 22240,
1095                 [31] = 25030,
1096         };
1097
1098         if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1099                 return 0;
1100
1101         return __mcs2bitrate[rate->mcs];
1102 }
1103
1104 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1105 {
1106         static const u32 base[4][10] = {
1107                 {   6500000,
1108                    13000000,
1109                    19500000,
1110                    26000000,
1111                    39000000,
1112                    52000000,
1113                    58500000,
1114                    65000000,
1115                    78000000,
1116                 /* not in the spec, but some devices use this: */
1117                    86500000,
1118                 },
1119                 {  13500000,
1120                    27000000,
1121                    40500000,
1122                    54000000,
1123                    81000000,
1124                   108000000,
1125                   121500000,
1126                   135000000,
1127                   162000000,
1128                   180000000,
1129                 },
1130                 {  29300000,
1131                    58500000,
1132                    87800000,
1133                   117000000,
1134                   175500000,
1135                   234000000,
1136                   263300000,
1137                   292500000,
1138                   351000000,
1139                   390000000,
1140                 },
1141                 {  58500000,
1142                   117000000,
1143                   175500000,
1144                   234000000,
1145                   351000000,
1146                   468000000,
1147                   526500000,
1148                   585000000,
1149                   702000000,
1150                   780000000,
1151                 },
1152         };
1153         u32 bitrate;
1154         int idx;
1155
1156         if (rate->mcs > 9)
1157                 goto warn;
1158
1159         switch (rate->bw) {
1160         case RATE_INFO_BW_160:
1161                 idx = 3;
1162                 break;
1163         case RATE_INFO_BW_80:
1164                 idx = 2;
1165                 break;
1166         case RATE_INFO_BW_40:
1167                 idx = 1;
1168                 break;
1169         case RATE_INFO_BW_5:
1170         case RATE_INFO_BW_10:
1171         default:
1172                 goto warn;
1173         case RATE_INFO_BW_20:
1174                 idx = 0;
1175         }
1176
1177         bitrate = base[idx][rate->mcs];
1178         bitrate *= rate->nss;
1179
1180         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1181                 bitrate = (bitrate / 9) * 10;
1182
1183         /* do NOT round down here */
1184         return (bitrate + 50000) / 100000;
1185  warn:
1186         WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1187                   rate->bw, rate->mcs, rate->nss);
1188         return 0;
1189 }
1190
1191 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1192 {
1193 #define SCALE 2048
1194         u16 mcs_divisors[12] = {
1195                 34133, /* 16.666666... */
1196                 17067, /*  8.333333... */
1197                 11378, /*  5.555555... */
1198                  8533, /*  4.166666... */
1199                  5689, /*  2.777777... */
1200                  4267, /*  2.083333... */
1201                  3923, /*  1.851851... */
1202                  3413, /*  1.666666... */
1203                  2844, /*  1.388888... */
1204                  2560, /*  1.250000... */
1205                  2276, /*  1.111111... */
1206                  2048, /*  1.000000... */
1207         };
1208         u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1209         u32 rates_969[3] =  { 480388888, 453700000, 408333333 };
1210         u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1211         u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1212         u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1213         u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1214         u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1215         u64 tmp;
1216         u32 result;
1217
1218         if (WARN_ON_ONCE(rate->mcs > 11))
1219                 return 0;
1220
1221         if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1222                 return 0;
1223         if (WARN_ON_ONCE(rate->he_ru_alloc >
1224                          NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1225                 return 0;
1226         if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1227                 return 0;
1228
1229         if (rate->bw == RATE_INFO_BW_160)
1230                 result = rates_160M[rate->he_gi];
1231         else if (rate->bw == RATE_INFO_BW_80 ||
1232                  (rate->bw == RATE_INFO_BW_HE_RU &&
1233                   rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1234                 result = rates_969[rate->he_gi];
1235         else if (rate->bw == RATE_INFO_BW_40 ||
1236                  (rate->bw == RATE_INFO_BW_HE_RU &&
1237                   rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1238                 result = rates_484[rate->he_gi];
1239         else if (rate->bw == RATE_INFO_BW_20 ||
1240                  (rate->bw == RATE_INFO_BW_HE_RU &&
1241                   rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1242                 result = rates_242[rate->he_gi];
1243         else if (rate->bw == RATE_INFO_BW_HE_RU &&
1244                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1245                 result = rates_106[rate->he_gi];
1246         else if (rate->bw == RATE_INFO_BW_HE_RU &&
1247                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1248                 result = rates_52[rate->he_gi];
1249         else if (rate->bw == RATE_INFO_BW_HE_RU &&
1250                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1251                 result = rates_26[rate->he_gi];
1252         else if (WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1253                       rate->bw, rate->he_ru_alloc))
1254                 return 0;
1255
1256         /* now scale to the appropriate MCS */
1257         tmp = result;
1258         tmp *= SCALE;
1259         do_div(tmp, mcs_divisors[rate->mcs]);
1260         result = tmp;
1261
1262         /* and take NSS, DCM into account */
1263         result = (result * rate->nss) / 8;
1264         if (rate->he_dcm)
1265                 result /= 2;
1266
1267         return result / 10000;
1268 }
1269
1270 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1271 {
1272         if (rate->flags & RATE_INFO_FLAGS_MCS)
1273                 return cfg80211_calculate_bitrate_ht(rate);
1274         if (rate->flags & RATE_INFO_FLAGS_60G)
1275                 return cfg80211_calculate_bitrate_60g(rate);
1276         if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1277                 return cfg80211_calculate_bitrate_vht(rate);
1278         if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1279                 return cfg80211_calculate_bitrate_he(rate);
1280
1281         return rate->legacy;
1282 }
1283 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1284
1285 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1286                           enum ieee80211_p2p_attr_id attr,
1287                           u8 *buf, unsigned int bufsize)
1288 {
1289         u8 *out = buf;
1290         u16 attr_remaining = 0;
1291         bool desired_attr = false;
1292         u16 desired_len = 0;
1293
1294         while (len > 0) {
1295                 unsigned int iedatalen;
1296                 unsigned int copy;
1297                 const u8 *iedata;
1298
1299                 if (len < 2)
1300                         return -EILSEQ;
1301                 iedatalen = ies[1];
1302                 if (iedatalen + 2 > len)
1303                         return -EILSEQ;
1304
1305                 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1306                         goto cont;
1307
1308                 if (iedatalen < 4)
1309                         goto cont;
1310
1311                 iedata = ies + 2;
1312
1313                 /* check WFA OUI, P2P subtype */
1314                 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1315                     iedata[2] != 0x9a || iedata[3] != 0x09)
1316                         goto cont;
1317
1318                 iedatalen -= 4;
1319                 iedata += 4;
1320
1321                 /* check attribute continuation into this IE */
1322                 copy = min_t(unsigned int, attr_remaining, iedatalen);
1323                 if (copy && desired_attr) {
1324                         desired_len += copy;
1325                         if (out) {
1326                                 memcpy(out, iedata, min(bufsize, copy));
1327                                 out += min(bufsize, copy);
1328                                 bufsize -= min(bufsize, copy);
1329                         }
1330
1331
1332                         if (copy == attr_remaining)
1333                                 return desired_len;
1334                 }
1335
1336                 attr_remaining -= copy;
1337                 if (attr_remaining)
1338                         goto cont;
1339
1340                 iedatalen -= copy;
1341                 iedata += copy;
1342
1343                 while (iedatalen > 0) {
1344                         u16 attr_len;
1345
1346                         /* P2P attribute ID & size must fit */
1347                         if (iedatalen < 3)
1348                                 return -EILSEQ;
1349                         desired_attr = iedata[0] == attr;
1350                         attr_len = get_unaligned_le16(iedata + 1);
1351                         iedatalen -= 3;
1352                         iedata += 3;
1353
1354                         copy = min_t(unsigned int, attr_len, iedatalen);
1355
1356                         if (desired_attr) {
1357                                 desired_len += copy;
1358                                 if (out) {
1359                                         memcpy(out, iedata, min(bufsize, copy));
1360                                         out += min(bufsize, copy);
1361                                         bufsize -= min(bufsize, copy);
1362                                 }
1363
1364                                 if (copy == attr_len)
1365                                         return desired_len;
1366                         }
1367
1368                         iedata += copy;
1369                         iedatalen -= copy;
1370                         attr_remaining = attr_len - copy;
1371                 }
1372
1373  cont:
1374                 len -= ies[1] + 2;
1375                 ies += ies[1] + 2;
1376         }
1377
1378         if (attr_remaining && desired_attr)
1379                 return -EILSEQ;
1380
1381         return -ENOENT;
1382 }
1383 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1384
1385 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1386 {
1387         int i;
1388
1389         /* Make sure array values are legal */
1390         if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1391                 return false;
1392
1393         i = 0;
1394         while (i < n_ids) {
1395                 if (ids[i] == WLAN_EID_EXTENSION) {
1396                         if (id_ext && (ids[i + 1] == id))
1397                                 return true;
1398
1399                         i += 2;
1400                         continue;
1401                 }
1402
1403                 if (ids[i] == id && !id_ext)
1404                         return true;
1405
1406                 i++;
1407         }
1408         return false;
1409 }
1410
1411 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1412 {
1413         /* we assume a validly formed IEs buffer */
1414         u8 len = ies[pos + 1];
1415
1416         pos += 2 + len;
1417
1418         /* the IE itself must have 255 bytes for fragments to follow */
1419         if (len < 255)
1420                 return pos;
1421
1422         while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1423                 len = ies[pos + 1];
1424                 pos += 2 + len;
1425         }
1426
1427         return pos;
1428 }
1429
1430 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1431                               const u8 *ids, int n_ids,
1432                               const u8 *after_ric, int n_after_ric,
1433                               size_t offset)
1434 {
1435         size_t pos = offset;
1436
1437         while (pos < ielen) {
1438                 u8 ext = 0;
1439
1440                 if (ies[pos] == WLAN_EID_EXTENSION)
1441                         ext = 2;
1442                 if ((pos + ext) >= ielen)
1443                         break;
1444
1445                 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1446                                           ies[pos] == WLAN_EID_EXTENSION))
1447                         break;
1448
1449                 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1450                         pos = skip_ie(ies, ielen, pos);
1451
1452                         while (pos < ielen) {
1453                                 if (ies[pos] == WLAN_EID_EXTENSION)
1454                                         ext = 2;
1455                                 else
1456                                         ext = 0;
1457
1458                                 if ((pos + ext) >= ielen)
1459                                         break;
1460
1461                                 if (!ieee80211_id_in_list(after_ric,
1462                                                           n_after_ric,
1463                                                           ies[pos + ext],
1464                                                           ext == 2))
1465                                         pos = skip_ie(ies, ielen, pos);
1466                                 else
1467                                         break;
1468                         }
1469                 } else {
1470                         pos = skip_ie(ies, ielen, pos);
1471                 }
1472         }
1473
1474         return pos;
1475 }
1476 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1477
1478 bool ieee80211_operating_class_to_band(u8 operating_class,
1479                                        enum nl80211_band *band)
1480 {
1481         switch (operating_class) {
1482         case 112:
1483         case 115 ... 127:
1484         case 128 ... 130:
1485                 *band = NL80211_BAND_5GHZ;
1486                 return true;
1487         case 81:
1488         case 82:
1489         case 83:
1490         case 84:
1491                 *band = NL80211_BAND_2GHZ;
1492                 return true;
1493         case 180:
1494                 *band = NL80211_BAND_60GHZ;
1495                 return true;
1496         }
1497
1498         return false;
1499 }
1500 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1501
1502 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1503                                           u8 *op_class)
1504 {
1505         u8 vht_opclass;
1506         u32 freq = chandef->center_freq1;
1507
1508         if (freq >= 2412 && freq <= 2472) {
1509                 if (chandef->width > NL80211_CHAN_WIDTH_40)
1510                         return false;
1511
1512                 /* 2.407 GHz, channels 1..13 */
1513                 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1514                         if (freq > chandef->chan->center_freq)
1515                                 *op_class = 83; /* HT40+ */
1516                         else
1517                                 *op_class = 84; /* HT40- */
1518                 } else {
1519                         *op_class = 81;
1520                 }
1521
1522                 return true;
1523         }
1524
1525         if (freq == 2484) {
1526                 if (chandef->width > NL80211_CHAN_WIDTH_40)
1527                         return false;
1528
1529                 *op_class = 82; /* channel 14 */
1530                 return true;
1531         }
1532
1533         switch (chandef->width) {
1534         case NL80211_CHAN_WIDTH_80:
1535                 vht_opclass = 128;
1536                 break;
1537         case NL80211_CHAN_WIDTH_160:
1538                 vht_opclass = 129;
1539                 break;
1540         case NL80211_CHAN_WIDTH_80P80:
1541                 vht_opclass = 130;
1542                 break;
1543         case NL80211_CHAN_WIDTH_10:
1544         case NL80211_CHAN_WIDTH_5:
1545                 return false; /* unsupported for now */
1546         default:
1547                 vht_opclass = 0;
1548                 break;
1549         }
1550
1551         /* 5 GHz, channels 36..48 */
1552         if (freq >= 5180 && freq <= 5240) {
1553                 if (vht_opclass) {
1554                         *op_class = vht_opclass;
1555                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1556                         if (freq > chandef->chan->center_freq)
1557                                 *op_class = 116;
1558                         else
1559                                 *op_class = 117;
1560                 } else {
1561                         *op_class = 115;
1562                 }
1563
1564                 return true;
1565         }
1566
1567         /* 5 GHz, channels 52..64 */
1568         if (freq >= 5260 && freq <= 5320) {
1569                 if (vht_opclass) {
1570                         *op_class = vht_opclass;
1571                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1572                         if (freq > chandef->chan->center_freq)
1573                                 *op_class = 119;
1574                         else
1575                                 *op_class = 120;
1576                 } else {
1577                         *op_class = 118;
1578                 }
1579
1580                 return true;
1581         }
1582
1583         /* 5 GHz, channels 100..144 */
1584         if (freq >= 5500 && freq <= 5720) {
1585                 if (vht_opclass) {
1586                         *op_class = vht_opclass;
1587                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1588                         if (freq > chandef->chan->center_freq)
1589                                 *op_class = 122;
1590                         else
1591                                 *op_class = 123;
1592                 } else {
1593                         *op_class = 121;
1594                 }
1595
1596                 return true;
1597         }
1598
1599         /* 5 GHz, channels 149..169 */
1600         if (freq >= 5745 && freq <= 5845) {
1601                 if (vht_opclass) {
1602                         *op_class = vht_opclass;
1603                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1604                         if (freq > chandef->chan->center_freq)
1605                                 *op_class = 126;
1606                         else
1607                                 *op_class = 127;
1608                 } else if (freq <= 5805) {
1609                         *op_class = 124;
1610                 } else {
1611                         *op_class = 125;
1612                 }
1613
1614                 return true;
1615         }
1616
1617         /* 56.16 GHz, channel 1..4 */
1618         if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1619                 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1620                         return false;
1621
1622                 *op_class = 180;
1623                 return true;
1624         }
1625
1626         /* not supported yet */
1627         return false;
1628 }
1629 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1630
1631 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1632                                        u32 *beacon_int_gcd,
1633                                        bool *beacon_int_different)
1634 {
1635         struct wireless_dev *wdev;
1636
1637         *beacon_int_gcd = 0;
1638         *beacon_int_different = false;
1639
1640         list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1641                 if (!wdev->beacon_interval)
1642                         continue;
1643
1644                 if (!*beacon_int_gcd) {
1645                         *beacon_int_gcd = wdev->beacon_interval;
1646                         continue;
1647                 }
1648
1649                 if (wdev->beacon_interval == *beacon_int_gcd)
1650                         continue;
1651
1652                 *beacon_int_different = true;
1653                 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1654         }
1655
1656         if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1657                 if (*beacon_int_gcd)
1658                         *beacon_int_different = true;
1659                 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1660         }
1661 }
1662
1663 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1664                                  enum nl80211_iftype iftype, u32 beacon_int)
1665 {
1666         /*
1667          * This is just a basic pre-condition check; if interface combinations
1668          * are possible the driver must already be checking those with a call
1669          * to cfg80211_check_combinations(), in which case we'll validate more
1670          * through the cfg80211_calculate_bi_data() call and code in
1671          * cfg80211_iter_combinations().
1672          */
1673
1674         if (beacon_int < 10 || beacon_int > 10000)
1675                 return -EINVAL;
1676
1677         return 0;
1678 }
1679
1680 int cfg80211_iter_combinations(struct wiphy *wiphy,
1681                                struct iface_combination_params *params,
1682                                void (*iter)(const struct ieee80211_iface_combination *c,
1683                                             void *data),
1684                                void *data)
1685 {
1686         const struct ieee80211_regdomain *regdom;
1687         enum nl80211_dfs_regions region = 0;
1688         int i, j, iftype;
1689         int num_interfaces = 0;
1690         u32 used_iftypes = 0;
1691         u32 beacon_int_gcd;
1692         bool beacon_int_different;
1693
1694         /*
1695          * This is a bit strange, since the iteration used to rely only on
1696          * the data given by the driver, but here it now relies on context,
1697          * in form of the currently operating interfaces.
1698          * This is OK for all current users, and saves us from having to
1699          * push the GCD calculations into all the drivers.
1700          * In the future, this should probably rely more on data that's in
1701          * cfg80211 already - the only thing not would appear to be any new
1702          * interfaces (while being brought up) and channel/radar data.
1703          */
1704         cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1705                                    &beacon_int_gcd, &beacon_int_different);
1706
1707         if (params->radar_detect) {
1708                 rcu_read_lock();
1709                 regdom = rcu_dereference(cfg80211_regdomain);
1710                 if (regdom)
1711                         region = regdom->dfs_region;
1712                 rcu_read_unlock();
1713         }
1714
1715         for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1716                 num_interfaces += params->iftype_num[iftype];
1717                 if (params->iftype_num[iftype] > 0 &&
1718                     !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1719                         used_iftypes |= BIT(iftype);
1720         }
1721
1722         for (i = 0; i < wiphy->n_iface_combinations; i++) {
1723                 const struct ieee80211_iface_combination *c;
1724                 struct ieee80211_iface_limit *limits;
1725                 u32 all_iftypes = 0;
1726
1727                 c = &wiphy->iface_combinations[i];
1728
1729                 if (num_interfaces > c->max_interfaces)
1730                         continue;
1731                 if (params->num_different_channels > c->num_different_channels)
1732                         continue;
1733
1734                 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1735                                  GFP_KERNEL);
1736                 if (!limits)
1737                         return -ENOMEM;
1738
1739                 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1740                         if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1741                                 continue;
1742                         for (j = 0; j < c->n_limits; j++) {
1743                                 all_iftypes |= limits[j].types;
1744                                 if (!(limits[j].types & BIT(iftype)))
1745                                         continue;
1746                                 if (limits[j].max < params->iftype_num[iftype])
1747                                         goto cont;
1748                                 limits[j].max -= params->iftype_num[iftype];
1749                         }
1750                 }
1751
1752                 if (params->radar_detect !=
1753                         (c->radar_detect_widths & params->radar_detect))
1754                         goto cont;
1755
1756                 if (params->radar_detect && c->radar_detect_regions &&
1757                     !(c->radar_detect_regions & BIT(region)))
1758                         goto cont;
1759
1760                 /* Finally check that all iftypes that we're currently
1761                  * using are actually part of this combination. If they
1762                  * aren't then we can't use this combination and have
1763                  * to continue to the next.
1764                  */
1765                 if ((all_iftypes & used_iftypes) != used_iftypes)
1766                         goto cont;
1767
1768                 if (beacon_int_gcd) {
1769                         if (c->beacon_int_min_gcd &&
1770                             beacon_int_gcd < c->beacon_int_min_gcd)
1771                                 goto cont;
1772                         if (!c->beacon_int_min_gcd && beacon_int_different)
1773                                 goto cont;
1774                 }
1775
1776                 /* This combination covered all interface types and
1777                  * supported the requested numbers, so we're good.
1778                  */
1779
1780                 (*iter)(c, data);
1781  cont:
1782                 kfree(limits);
1783         }
1784
1785         return 0;
1786 }
1787 EXPORT_SYMBOL(cfg80211_iter_combinations);
1788
1789 static void
1790 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1791                           void *data)
1792 {
1793         int *num = data;
1794         (*num)++;
1795 }
1796
1797 int cfg80211_check_combinations(struct wiphy *wiphy,
1798                                 struct iface_combination_params *params)
1799 {
1800         int err, num = 0;
1801
1802         err = cfg80211_iter_combinations(wiphy, params,
1803                                          cfg80211_iter_sum_ifcombs, &num);
1804         if (err)
1805                 return err;
1806         if (num == 0)
1807                 return -EBUSY;
1808
1809         return 0;
1810 }
1811 EXPORT_SYMBOL(cfg80211_check_combinations);
1812
1813 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1814                            const u8 *rates, unsigned int n_rates,
1815                            u32 *mask)
1816 {
1817         int i, j;
1818
1819         if (!sband)
1820                 return -EINVAL;
1821
1822         if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1823                 return -EINVAL;
1824
1825         *mask = 0;
1826
1827         for (i = 0; i < n_rates; i++) {
1828                 int rate = (rates[i] & 0x7f) * 5;
1829                 bool found = false;
1830
1831                 for (j = 0; j < sband->n_bitrates; j++) {
1832                         if (sband->bitrates[j].bitrate == rate) {
1833                                 found = true;
1834                                 *mask |= BIT(j);
1835                                 break;
1836                         }
1837                 }
1838                 if (!found)
1839                         return -EINVAL;
1840         }
1841
1842         /*
1843          * mask must have at least one bit set here since we
1844          * didn't accept a 0-length rates array nor allowed
1845          * entries in the array that didn't exist
1846          */
1847
1848         return 0;
1849 }
1850
1851 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1852 {
1853         enum nl80211_band band;
1854         unsigned int n_channels = 0;
1855
1856         for (band = 0; band < NUM_NL80211_BANDS; band++)
1857                 if (wiphy->bands[band])
1858                         n_channels += wiphy->bands[band]->n_channels;
1859
1860         return n_channels;
1861 }
1862 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1863
1864 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1865                          struct station_info *sinfo)
1866 {
1867         struct cfg80211_registered_device *rdev;
1868         struct wireless_dev *wdev;
1869
1870         wdev = dev->ieee80211_ptr;
1871         if (!wdev)
1872                 return -EOPNOTSUPP;
1873
1874         rdev = wiphy_to_rdev(wdev->wiphy);
1875         if (!rdev->ops->get_station)
1876                 return -EOPNOTSUPP;
1877
1878         memset(sinfo, 0, sizeof(*sinfo));
1879
1880         return rdev_get_station(rdev, dev, mac_addr, sinfo);
1881 }
1882 EXPORT_SYMBOL(cfg80211_get_station);
1883
1884 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1885 {
1886         int i;
1887
1888         if (!f)
1889                 return;
1890
1891         kfree(f->serv_spec_info);
1892         kfree(f->srf_bf);
1893         kfree(f->srf_macs);
1894         for (i = 0; i < f->num_rx_filters; i++)
1895                 kfree(f->rx_filters[i].filter);
1896
1897         for (i = 0; i < f->num_tx_filters; i++)
1898                 kfree(f->tx_filters[i].filter);
1899
1900         kfree(f->rx_filters);
1901         kfree(f->tx_filters);
1902         kfree(f);
1903 }
1904 EXPORT_SYMBOL(cfg80211_free_nan_func);
1905
1906 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1907                                 u32 center_freq_khz, u32 bw_khz)
1908 {
1909         u32 start_freq_khz, end_freq_khz;
1910
1911         start_freq_khz = center_freq_khz - (bw_khz / 2);
1912         end_freq_khz = center_freq_khz + (bw_khz / 2);
1913
1914         if (start_freq_khz >= freq_range->start_freq_khz &&
1915             end_freq_khz <= freq_range->end_freq_khz)
1916                 return true;
1917
1918         return false;
1919 }
1920
1921 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1922 {
1923         sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1924                                 sizeof(*(sinfo->pertid)),
1925                                 gfp);
1926         if (!sinfo->pertid)
1927                 return -ENOMEM;
1928
1929         return 0;
1930 }
1931 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1932
1933 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1934 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1935 const unsigned char rfc1042_header[] __aligned(2) =
1936         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1937 EXPORT_SYMBOL(rfc1042_header);
1938
1939 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1940 const unsigned char bridge_tunnel_header[] __aligned(2) =
1941         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1942 EXPORT_SYMBOL(bridge_tunnel_header);
1943
1944 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
1945                              bool is_4addr, u8 check_swif)
1946
1947 {
1948         bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
1949
1950         switch (check_swif) {
1951         case 0:
1952                 if (is_vlan && is_4addr)
1953                         return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
1954                 return wiphy->interface_modes & BIT(iftype);
1955         case 1:
1956                 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
1957                         return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
1958                 return wiphy->software_iftypes & BIT(iftype);
1959         default:
1960                 break;
1961         }
1962
1963         return false;
1964 }
1965 EXPORT_SYMBOL(cfg80211_iftype_allowed);
1966
1967 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1968 struct iapp_layer2_update {
1969         u8 da[ETH_ALEN];        /* broadcast */
1970         u8 sa[ETH_ALEN];        /* STA addr */
1971         __be16 len;             /* 6 */
1972         u8 dsap;                /* 0 */
1973         u8 ssap;                /* 0 */
1974         u8 control;
1975         u8 xid_info[3];
1976 } __packed;
1977
1978 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1979 {
1980         struct iapp_layer2_update *msg;
1981         struct sk_buff *skb;
1982
1983         /* Send Level 2 Update Frame to update forwarding tables in layer 2
1984          * bridge devices */
1985
1986         skb = dev_alloc_skb(sizeof(*msg));
1987         if (!skb)
1988                 return;
1989         msg = skb_put(skb, sizeof(*msg));
1990
1991         /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1992          * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1993
1994         eth_broadcast_addr(msg->da);
1995         ether_addr_copy(msg->sa, addr);
1996         msg->len = htons(6);
1997         msg->dsap = 0;
1998         msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
1999         msg->control = 0xaf;    /* XID response lsb.1111F101.
2000                                  * F=0 (no poll command; unsolicited frame) */
2001         msg->xid_info[0] = 0x81;        /* XID format identifier */
2002         msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
2003         msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
2004
2005         skb->dev = dev;
2006         skb->protocol = eth_type_trans(skb, dev);
2007         memset(skb->cb, 0, sizeof(skb->cb));
2008         netif_rx_ni(skb);
2009 }
2010 EXPORT_SYMBOL(cfg80211_send_layer2_update);