80211 - Update up to FreeBSD's r287029 (only skipping a few minor changes)
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_input.c
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_wlan.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mbuf.h>   
35 #include <sys/malloc.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
38  
39 #include <sys/socket.h>
40  
41 #include <net/ethernet.h>
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <net/if_llc.h>
45 #include <net/if_media.h>
46 #include <net/vlan/if_vlan_var.h>
47
48 #include <netproto/802_11/ieee80211_var.h>
49 #include <netproto/802_11/ieee80211_input.h>
50 #ifdef IEEE80211_SUPPORT_MESH
51 #include <netproto/802_11/ieee80211_mesh.h>
52 #endif
53
54 #include <net/bpf.h>
55
56 #ifdef INET
57 #include <netinet/in.h>
58 #include <net/ethernet.h>
59 #endif
60
61 static void
62 ieee80211_process_mimo(struct ieee80211_node *ni, struct ieee80211_rx_stats *rx)
63 {
64         int i;
65
66         /* Verify the required MIMO bits are set */
67         if ((rx->r_flags & (IEEE80211_R_C_CHAIN | IEEE80211_R_C_NF | IEEE80211_R_C_RSSI)) !=
68             (IEEE80211_R_C_CHAIN | IEEE80211_R_C_NF | IEEE80211_R_C_RSSI))
69                 return;
70
71         /* XXX This assumes the MIMO radios have both ctl and ext chains */
72         for (i = 0; i < MIN(rx->c_chain, IEEE80211_MAX_CHAINS); i++) {
73                 IEEE80211_RSSI_LPF(ni->ni_mimo_rssi_ctl[i], rx->c_rssi_ctl[i]);
74                 IEEE80211_RSSI_LPF(ni->ni_mimo_rssi_ext[i], rx->c_rssi_ext[i]);
75         }
76
77         /* XXX This also assumes the MIMO radios have both ctl and ext chains */
78         for(i = 0; i < MIN(rx->c_chain, IEEE80211_MAX_CHAINS); i++) {
79                 ni->ni_mimo_noise_ctl[i] = rx->c_nf_ctl[i];
80                 ni->ni_mimo_noise_ext[i] = rx->c_nf_ext[i];
81         }
82         ni->ni_mimo_chains = rx->c_chain;
83 }
84
85 int
86 ieee80211_input_mimo(struct ieee80211_node *ni, struct mbuf *m,
87     struct ieee80211_rx_stats *rx)
88 {
89         /* XXX should assert IEEE80211_R_NF and IEEE80211_R_RSSI are set */
90         ieee80211_process_mimo(ni, rx);
91         //return ieee80211_input(ni, m, rx->rssi, rx->nf);
92         return ni->ni_vap->iv_input(ni, m, rx, rx->rssi, rx->nf);
93 }
94
95 int
96 ieee80211_input_all(struct ieee80211com *ic, struct mbuf *m, int rssi, int nf)
97 {
98         struct ieee80211_rx_stats rx;
99
100         rx.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
101         rx.nf = nf;
102         rx.rssi = rssi;
103         return ieee80211_input_mimo_all(ic, m, &rx);
104 }
105
106 int
107 ieee80211_input_mimo_all(struct ieee80211com *ic, struct mbuf *m,
108     struct ieee80211_rx_stats *rx)
109 {
110         struct ieee80211vap *vap;
111         int type = -1;
112
113         m->m_flags |= M_BCAST;          /* NB: mark for bpf tap'ing */
114
115         /* XXX locking */
116         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
117                 struct ieee80211_node *ni;
118                 struct mbuf *mcopy;
119
120                 /* NB: could check for IFF_UP but this is cheaper */
121                 if (vap->iv_state == IEEE80211_S_INIT)
122                         continue;
123                 /*
124                  * WDS vap's only receive directed traffic from the
125                  * station at the ``far end''.  That traffic should
126                  * be passed through the AP vap the station is associated
127                  * to--so don't spam them with mcast frames.
128                  */
129                 if (vap->iv_opmode == IEEE80211_M_WDS)
130                         continue;
131                 if (TAILQ_NEXT(vap, iv_next) != NULL) {
132                         /*
133                          * Packet contents are changed by ieee80211_decap
134                          * so do a deep copy of the packet.
135                          */
136                         mcopy = m_dup(m, M_NOWAIT);
137                         if (mcopy == NULL) {
138                                 /* XXX stat+msg */
139                                 continue;
140                         }
141                 } else {
142                         mcopy = m;
143                         m = NULL;
144                 }
145                 ni = ieee80211_ref_node(vap->iv_bss);
146                 type = ieee80211_input_mimo(ni, mcopy, rx);
147                 ieee80211_free_node(ni);
148         }
149         if (m != NULL)                  /* no vaps, reclaim mbuf */
150                 m_freem(m);
151         return type;
152 }
153
154 /*
155  * This function reassembles fragments.
156  *
157  * XXX should handle 3 concurrent reassemblies per-spec.
158  */
159 struct mbuf *
160 ieee80211_defrag(struct ieee80211_node *ni, struct mbuf *m, int hdrspace)
161 {
162         struct ieee80211vap *vap = ni->ni_vap;
163         struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
164         struct ieee80211_frame *lwh;
165         uint16_t rxseq;
166         uint8_t fragno;
167         uint8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
168         struct mbuf *mfrag;
169
170         KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
171
172         rxseq = le16toh(*(uint16_t *)wh->i_seq);
173         fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
174
175         /* Quick way out, if there's nothing to defragment */
176         if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
177                 return m;
178
179         /*
180          * Remove frag to insure it doesn't get reaped by timer.
181          */
182         if (ni->ni_table == NULL) {
183                 /*
184                  * Should never happen.  If the node is orphaned (not in
185                  * the table) then input packets should not reach here.
186                  * Otherwise, a concurrent request that yanks the table
187                  * should be blocked by other interlocking and/or by first
188                  * shutting the driver down.  Regardless, be defensive
189                  * here and just bail
190                  */
191                 /* XXX need msg+stat */
192                 m_freem(m);
193                 return NULL;
194         }
195         IEEE80211_NODE_LOCK(ni->ni_table);
196         mfrag = ni->ni_rxfrag[0];
197         ni->ni_rxfrag[0] = NULL;
198         IEEE80211_NODE_UNLOCK(ni->ni_table);
199
200         /*
201          * Validate new fragment is in order and
202          * related to the previous ones.
203          */
204         if (mfrag != NULL) {
205                 uint16_t last_rxseq;
206
207                 lwh = mtod(mfrag, struct ieee80211_frame *);
208                 last_rxseq = le16toh(*(uint16_t *)lwh->i_seq);
209                 /* NB: check seq # and frag together */
210                 if (rxseq != last_rxseq+1 ||
211                     !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
212                     !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
213                         /*
214                          * Unrelated fragment or no space for it,
215                          * clear current fragments.
216                          */
217                         m_freem(mfrag);
218                         mfrag = NULL;
219                 }
220         }
221
222         if (mfrag == NULL) {
223                 if (fragno != 0) {              /* !first fragment, discard */
224                         vap->iv_stats.is_rx_defrag++;
225                         IEEE80211_NODE_STAT(ni, rx_defrag);
226                         m_freem(m);
227                         return NULL;
228                 }
229                 mfrag = m;
230         } else {                                /* concatenate */
231                 m_adj(m, hdrspace);             /* strip header */
232                 m_cat(mfrag, m);
233                 /* NB: m_cat doesn't update the packet header */
234                 mfrag->m_pkthdr.len += m->m_pkthdr.len;
235                 /* track last seqnum and fragno */
236                 lwh = mtod(mfrag, struct ieee80211_frame *);
237                 *(uint16_t *) lwh->i_seq = *(uint16_t *) wh->i_seq;
238         }
239         if (more_frag) {                        /* more to come, save */
240                 ni->ni_rxfragstamp = ticks;
241                 ni->ni_rxfrag[0] = mfrag;
242                 mfrag = NULL;
243         }
244         return mfrag;
245 }
246
247 void
248 ieee80211_deliver_data(struct ieee80211vap *vap,
249         struct ieee80211_node *ni, struct mbuf *m)
250 {
251         struct ether_header *eh = mtod(m, struct ether_header *);
252         struct ifnet *ifp = vap->iv_ifp;
253
254         /* clear driver/net80211 flags before passing up */
255         m->m_flags &= ~(M_MCAST | M_BCAST);
256 #if !defined(__DragonFly__)
257         m_clrprotoflags(m);
258 #endif
259
260         /* NB: see hostap_deliver_data, this path doesn't handle hostap */
261         KASSERT(vap->iv_opmode != IEEE80211_M_HOSTAP, ("gack, hostap"));
262         /*
263          * Do accounting.
264          */
265         IFNET_STAT_INC(ifp, ipackets, 1);
266         IEEE80211_NODE_STAT(ni, rx_data);
267         IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
268         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
269                 m->m_flags |= M_MCAST;          /* XXX M_BCAST? */
270                 IEEE80211_NODE_STAT(ni, rx_mcast);
271         } else
272                 IEEE80211_NODE_STAT(ni, rx_ucast);
273         m->m_pkthdr.rcvif = ifp;
274
275         if (ni->ni_vlan != 0) {
276                 /* attach vlan tag */
277 #if defined(__DragonFly__)
278                 m->m_pkthdr.ether_vlantag = ni->ni_vlan;
279 #else
280                 m->m_pkthdr.ether_vtag = ni->ni_vlan;
281 #endif
282                 m->m_flags |= M_VLANTAG;
283         }
284         ifp->if_input(ifp, m, NULL, -1);
285 }
286
287 struct mbuf *
288 ieee80211_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen)
289 {
290         struct ieee80211_qosframe_addr4 wh;
291         struct ether_header *eh;
292         struct llc *llc;
293
294         KASSERT(hdrlen <= sizeof(wh),
295             ("hdrlen %d > max %zd", hdrlen, sizeof(wh)));
296
297         if (m->m_len < hdrlen + sizeof(*llc) &&
298             (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
299                 vap->iv_stats.is_rx_tooshort++;
300                 /* XXX msg */
301                 return NULL;
302         }
303         memcpy(&wh, mtod(m, caddr_t), hdrlen);
304         llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
305         if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
306             llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
307             llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
308             /* NB: preserve AppleTalk frames that have a native SNAP hdr */
309             !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
310               llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
311                 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
312                 llc = NULL;
313         } else {
314                 m_adj(m, hdrlen - sizeof(*eh));
315         }
316         eh = mtod(m, struct ether_header *);
317         switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
318         case IEEE80211_FC1_DIR_NODS:
319                 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
320                 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
321                 break;
322         case IEEE80211_FC1_DIR_TODS:
323                 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
324                 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
325                 break;
326         case IEEE80211_FC1_DIR_FROMDS:
327                 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
328                 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
329                 break;
330         case IEEE80211_FC1_DIR_DSTODS:
331                 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
332                 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
333                 break;
334         }
335 #ifndef __NO_STRICT_ALIGNMENT
336         if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
337                 m = ieee80211_realign(vap, m, sizeof(*eh));
338                 if (m == NULL)
339                         return NULL;
340         }
341 #endif /* !__NO_STRICT_ALIGNMENT */
342         if (llc != NULL) {
343                 eh = mtod(m, struct ether_header *);
344                 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
345         }
346         return m;
347 }
348
349 /*
350  * Decap a frame encapsulated in a fast-frame/A-MSDU.
351  */
352 struct mbuf *
353 ieee80211_decap1(struct mbuf *m, int *framelen)
354 {
355 #define FF_LLC_SIZE     (sizeof(struct ether_header) + sizeof(struct llc))
356         struct ether_header *eh;
357         struct llc *llc;
358
359         /*
360          * The frame has an 802.3 header followed by an 802.2
361          * LLC header.  The encapsulated frame length is in the
362          * first header type field; save that and overwrite it 
363          * with the true type field found in the second.  Then
364          * copy the 802.3 header up to where it belongs and
365          * adjust the mbuf contents to remove the void.
366          */
367         if (m->m_len < FF_LLC_SIZE && (m = m_pullup(m, FF_LLC_SIZE)) == NULL)
368                 return NULL;
369         eh = mtod(m, struct ether_header *);    /* 802.3 header is first */
370         llc = (struct llc *)&eh[1];             /* 802.2 header follows */
371         *framelen = ntohs(eh->ether_type)       /* encap'd frame size */
372                   + sizeof(struct ether_header) - sizeof(struct llc);
373         eh->ether_type = llc->llc_un.type_snap.ether_type;
374         ovbcopy(eh, mtod(m, uint8_t *) + sizeof(struct llc),
375                 sizeof(struct ether_header));
376         m_adj(m, sizeof(struct llc));
377         return m;
378 #undef FF_LLC_SIZE
379 }
380
381 /*
382  * Install received rate set information in the node's state block.
383  */
384 int
385 ieee80211_setup_rates(struct ieee80211_node *ni,
386         const uint8_t *rates, const uint8_t *xrates, int flags)
387 {
388         struct ieee80211vap *vap = ni->ni_vap;
389         struct ieee80211_rateset *rs = &ni->ni_rates;
390
391         memset(rs, 0, sizeof(*rs));
392         rs->rs_nrates = rates[1];
393         memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
394         if (xrates != NULL) {
395                 uint8_t nxrates;
396                 /*
397                  * Tack on 11g extended supported rate element.
398                  */
399                 nxrates = xrates[1];
400                 if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
401                         nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
402                         IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE, ni,
403                             "extended rate set too large; only using "
404                             "%u of %u rates", nxrates, xrates[1]);
405                         vap->iv_stats.is_rx_rstoobig++;
406                 }
407                 memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
408                 rs->rs_nrates += nxrates;
409         }
410         return ieee80211_fix_rate(ni, rs, flags);
411 }
412
413 /*
414  * Send a management frame error response to the specified
415  * station.  If ni is associated with the station then use
416  * it; otherwise allocate a temporary node suitable for
417  * transmitting the frame and then free the reference so
418  * it will go away as soon as the frame has been transmitted.
419  */
420 void
421 ieee80211_send_error(struct ieee80211_node *ni,
422         const uint8_t mac[IEEE80211_ADDR_LEN], int subtype, int arg)
423 {
424         struct ieee80211vap *vap = ni->ni_vap;
425         int istmp;
426
427         if (ni == vap->iv_bss) {
428                 if (vap->iv_state != IEEE80211_S_RUN) {
429                         /*
430                          * XXX hack until we get rid of this routine.
431                          * We can be called prior to the vap reaching
432                          * run state under certain conditions in which
433                          * case iv_bss->ni_chan will not be setup.
434                          * Check for this explicitly and and just ignore
435                          * the request.
436                          */
437                         return;
438                 }
439                 ni = ieee80211_tmp_node(vap, mac);
440                 if (ni == NULL) {
441                         /* XXX msg */
442                         return;
443                 }
444                 istmp = 1;
445         } else
446                 istmp = 0;
447         IEEE80211_SEND_MGMT(ni, subtype, arg);
448         if (istmp)
449                 ieee80211_free_node(ni);
450 }
451
452 int
453 ieee80211_alloc_challenge(struct ieee80211_node *ni)
454 {
455         if (ni->ni_challenge == NULL)
456                 ni->ni_challenge = (uint32_t *) kmalloc(IEEE80211_CHALLENGE_LEN,
457                     M_80211_NODE, M_INTWAIT);
458         if (ni->ni_challenge == NULL) {
459                 IEEE80211_NOTE(ni->ni_vap,
460                     IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni,
461                     "%s", "shared key challenge alloc failed");
462                 /* XXX statistic */
463         }
464         return (ni->ni_challenge != NULL);
465 }
466
467 /*
468  * Parse a Beacon or ProbeResponse frame and return the
469  * useful information in an ieee80211_scanparams structure.
470  * Status is set to 0 if no problems were found; otherwise
471  * a bitmask of IEEE80211_BPARSE_* items is returned that
472  * describes the problems detected.
473  */
474 int
475 ieee80211_parse_beacon(struct ieee80211_node *ni, struct mbuf *m,
476         struct ieee80211_channel *rxchan, struct ieee80211_scanparams *scan)
477 {
478         struct ieee80211vap *vap = ni->ni_vap;
479         struct ieee80211com *ic = ni->ni_ic;
480         struct ieee80211_frame *wh;
481         uint8_t *frm, *efrm;
482
483         wh = mtod(m, struct ieee80211_frame *);
484         frm = (uint8_t *)&wh[1];
485         efrm = mtod(m, uint8_t *) + m->m_len;
486         scan->status = 0;
487         /*
488          * beacon/probe response frame format
489          *      [8] time stamp
490          *      [2] beacon interval
491          *      [2] capability information
492          *      [tlv] ssid
493          *      [tlv] supported rates
494          *      [tlv] country information
495          *      [tlv] channel switch announcement (CSA)
496          *      [tlv] parameter set (FH/DS)
497          *      [tlv] erp information
498          *      [tlv] extended supported rates
499          *      [tlv] WME
500          *      [tlv] WPA or RSN
501          *      [tlv] HT capabilities
502          *      [tlv] HT information
503          *      [tlv] Atheros capabilities
504          *      [tlv] Mesh ID
505          *      [tlv] Mesh Configuration
506          */
507         IEEE80211_VERIFY_LENGTH(efrm - frm, 12,
508             return (scan->status = IEEE80211_BPARSE_BADIELEN));
509         memset(scan, 0, sizeof(*scan));
510         scan->tstamp  = frm;                            frm += 8;
511         scan->bintval = le16toh(*(uint16_t *)frm);      frm += 2;
512         scan->capinfo = le16toh(*(uint16_t *)frm);      frm += 2;
513         scan->bchan = ieee80211_chan2ieee(ic, rxchan);
514         scan->chan = scan->bchan;
515         scan->ies = frm;
516         scan->ies_len = efrm - frm;
517
518         while (efrm - frm > 1) {
519                 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2,
520                     return (scan->status = IEEE80211_BPARSE_BADIELEN));
521                 switch (*frm) {
522                 case IEEE80211_ELEMID_SSID:
523                         scan->ssid = frm;
524                         break;
525                 case IEEE80211_ELEMID_RATES:
526                         scan->rates = frm;
527                         break;
528                 case IEEE80211_ELEMID_COUNTRY:
529                         scan->country = frm;
530                         break;
531                 case IEEE80211_ELEMID_CSA:
532                         scan->csa = frm;
533                         break;
534                 case IEEE80211_ELEMID_QUIET:
535                         scan->quiet = frm;
536                         break;
537                 case IEEE80211_ELEMID_FHPARMS:
538                         if (ic->ic_phytype == IEEE80211_T_FH) {
539                                 scan->fhdwell = LE_READ_2(&frm[2]);
540                                 scan->chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
541                                 scan->fhindex = frm[6];
542                         }
543                         break;
544                 case IEEE80211_ELEMID_DSPARMS:
545                         /*
546                          * XXX hack this since depending on phytype
547                          * is problematic for multi-mode devices.
548                          */
549                         if (ic->ic_phytype != IEEE80211_T_FH)
550                                 scan->chan = frm[2];
551                         break;
552                 case IEEE80211_ELEMID_TIM:
553                         /* XXX ATIM? */
554                         scan->tim = frm;
555                         scan->timoff = frm - mtod(m, uint8_t *);
556                         break;
557                 case IEEE80211_ELEMID_IBSSPARMS:
558                 case IEEE80211_ELEMID_CFPARMS:
559                 case IEEE80211_ELEMID_PWRCNSTR:
560                         /* NB: avoid debugging complaints */
561                         break;
562                 case IEEE80211_ELEMID_XRATES:
563                         scan->xrates = frm;
564                         break;
565                 case IEEE80211_ELEMID_ERP:
566                         if (frm[1] != 1) {
567                                 IEEE80211_DISCARD_IE(vap,
568                                     IEEE80211_MSG_ELEMID, wh, "ERP",
569                                     "bad len %u", frm[1]);
570                                 vap->iv_stats.is_rx_elem_toobig++;
571                                 break;
572                         }
573                         scan->erp = frm[2] | 0x100;
574                         break;
575                 case IEEE80211_ELEMID_HTCAP:
576                         scan->htcap = frm;
577                         break;
578                 case IEEE80211_ELEMID_RSN:
579                         scan->rsn = frm;
580                         break;
581                 case IEEE80211_ELEMID_HTINFO:
582                         scan->htinfo = frm;
583                         break;
584 #ifdef IEEE80211_SUPPORT_MESH
585                 case IEEE80211_ELEMID_MESHID:
586                         scan->meshid = frm;
587                         break;
588                 case IEEE80211_ELEMID_MESHCONF:
589                         scan->meshconf = frm;
590                         break;
591 #endif
592                 case IEEE80211_ELEMID_VENDOR:
593                         if (iswpaoui(frm))
594                                 scan->wpa = frm;
595                         else if (iswmeparam(frm) || iswmeinfo(frm))
596                                 scan->wme = frm;
597 #ifdef IEEE80211_SUPPORT_SUPERG
598                         else if (isatherosoui(frm))
599                                 scan->ath = frm;
600 #endif
601 #ifdef IEEE80211_SUPPORT_TDMA
602                         else if (istdmaoui(frm))
603                                 scan->tdma = frm;
604 #endif
605                         else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
606                                 /*
607                                  * Accept pre-draft HT ie's if the
608                                  * standard ones have not been seen.
609                                  */
610                                 if (ishtcapoui(frm)) {
611                                         if (scan->htcap == NULL)
612                                                 scan->htcap = frm;
613                                 } else if (ishtinfooui(frm)) {
614                                         if (scan->htinfo == NULL)
615                                                 scan->htcap = frm;
616                                 }
617                         }
618                         break;
619                 default:
620                         IEEE80211_DISCARD_IE(vap, IEEE80211_MSG_ELEMID,
621                             wh, "unhandled",
622                             "id %u, len %u", *frm, frm[1]);
623                         vap->iv_stats.is_rx_elem_unknown++;
624                         break;
625                 }
626                 frm += frm[1] + 2;
627         }
628         IEEE80211_VERIFY_ELEMENT(scan->rates, IEEE80211_RATE_MAXSIZE,
629             scan->status |= IEEE80211_BPARSE_RATES_INVALID);
630         if (scan->rates != NULL && scan->xrates != NULL) {
631                 /*
632                  * NB: don't process XRATES if RATES is missing.  This
633                  * avoids a potential null ptr deref and should be ok
634                  * as the return code will already note RATES is missing
635                  * (so callers shouldn't otherwise process the frame).
636                  */
637                 IEEE80211_VERIFY_ELEMENT(scan->xrates,
638                     IEEE80211_RATE_MAXSIZE - scan->rates[1],
639                     scan->status |= IEEE80211_BPARSE_XRATES_INVALID);
640         }
641         IEEE80211_VERIFY_ELEMENT(scan->ssid, IEEE80211_NWID_LEN,
642             scan->status |= IEEE80211_BPARSE_SSID_INVALID);
643         if (scan->chan != scan->bchan && ic->ic_phytype != IEEE80211_T_FH) {
644                 /*
645                  * Frame was received on a channel different from the
646                  * one indicated in the DS params element id;
647                  * silently discard it.
648                  *
649                  * NB: this can happen due to signal leakage.
650                  *     But we should take it for FH phy because
651                  *     the rssi value should be correct even for
652                  *     different hop pattern in FH.
653                  */
654                 IEEE80211_DISCARD(vap,
655                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
656                     wh, NULL, "for off-channel %u (bchan=%u)",
657                     scan->chan, scan->bchan);
658                 vap->iv_stats.is_rx_chanmismatch++;
659                 scan->status |= IEEE80211_BPARSE_OFFCHAN;
660         }
661         if (!(IEEE80211_BINTVAL_MIN <= scan->bintval &&
662               scan->bintval <= IEEE80211_BINTVAL_MAX)) {
663                 IEEE80211_DISCARD(vap,
664                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
665                     wh, NULL, "bogus beacon interval (%d TU)",
666                     (int) scan->bintval);
667                 vap->iv_stats.is_rx_badbintval++;
668                 scan->status |= IEEE80211_BPARSE_BINTVAL_INVALID;
669         }
670         if (scan->country != NULL) {
671                 /*
672                  * Validate we have at least enough data to extract
673                  * the country code.  Not sure if we should return an
674                  * error instead of discarding the IE; consider this
675                  * being lenient as we don't depend on the data for
676                  * correct operation.
677                  */
678                 IEEE80211_VERIFY_LENGTH(scan->country[1], 3 * sizeof(uint8_t),
679                     scan->country = NULL);
680         }
681         if (scan->csa != NULL) {
682                 /*
683                  * Validate Channel Switch Announcement; this must
684                  * be the correct length or we toss the frame.
685                  */
686                 IEEE80211_VERIFY_LENGTH(scan->csa[1], 3 * sizeof(uint8_t),
687                     scan->status |= IEEE80211_BPARSE_CSA_INVALID);
688         }
689         /*
690          * Process HT ie's.  This is complicated by our
691          * accepting both the standard ie's and the pre-draft
692          * vendor OUI ie's that some vendors still use/require.
693          */
694         if (scan->htcap != NULL) {
695                 IEEE80211_VERIFY_LENGTH(scan->htcap[1],
696                      scan->htcap[0] == IEEE80211_ELEMID_VENDOR ?
697                          4 + sizeof(struct ieee80211_ie_htcap)-2 :
698                          sizeof(struct ieee80211_ie_htcap)-2,
699                      scan->htcap = NULL);
700         }
701         if (scan->htinfo != NULL) {
702                 IEEE80211_VERIFY_LENGTH(scan->htinfo[1],
703                      scan->htinfo[0] == IEEE80211_ELEMID_VENDOR ?
704                          4 + sizeof(struct ieee80211_ie_htinfo)-2 :
705                          sizeof(struct ieee80211_ie_htinfo)-2,
706                      scan->htinfo = NULL);
707         }
708         return scan->status;
709 }
710
711 /*
712  * Parse an Action frame.  Return 0 on success, non-zero on failure.
713  */
714 int
715 ieee80211_parse_action(struct ieee80211_node *ni, struct mbuf *m)
716 {
717         struct ieee80211vap *vap = ni->ni_vap;
718         const struct ieee80211_action *ia;
719         struct ieee80211_frame *wh;
720         uint8_t *frm, *efrm;
721
722         /*
723          * action frame format:
724          *      [1] category
725          *      [1] action
726          *      [tlv] parameters
727          */
728         wh = mtod(m, struct ieee80211_frame *);
729         frm = (u_int8_t *)&wh[1];
730         efrm = mtod(m, u_int8_t *) + m->m_len;
731         IEEE80211_VERIFY_LENGTH(efrm - frm,
732                 sizeof(struct ieee80211_action), return EINVAL);
733         ia = (const struct ieee80211_action *) frm;
734
735         vap->iv_stats.is_rx_action++;
736         IEEE80211_NODE_STAT(ni, rx_action);
737
738         /* verify frame payloads but defer processing */
739         switch (ia->ia_category) {
740         case IEEE80211_ACTION_CAT_BA:
741                 switch (ia->ia_action) {
742                 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
743                         IEEE80211_VERIFY_LENGTH(efrm - frm,
744                             sizeof(struct ieee80211_action_ba_addbarequest),
745                             return EINVAL);
746                         break;
747                 case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
748                         IEEE80211_VERIFY_LENGTH(efrm - frm,
749                             sizeof(struct ieee80211_action_ba_addbaresponse),
750                             return EINVAL);
751                         break;
752                 case IEEE80211_ACTION_BA_DELBA:
753                         IEEE80211_VERIFY_LENGTH(efrm - frm,
754                             sizeof(struct ieee80211_action_ba_delba),
755                             return EINVAL);
756                         break;
757                 }
758                 break;
759         case IEEE80211_ACTION_CAT_HT:
760                 switch (ia->ia_action) {
761                 case IEEE80211_ACTION_HT_TXCHWIDTH:
762                         IEEE80211_VERIFY_LENGTH(efrm - frm,
763                             sizeof(struct ieee80211_action_ht_txchwidth),
764                             return EINVAL);
765                         break;
766                 case IEEE80211_ACTION_HT_MIMOPWRSAVE:
767                         IEEE80211_VERIFY_LENGTH(efrm - frm,
768                             sizeof(struct ieee80211_action_ht_mimopowersave),
769                             return EINVAL);
770                         break;
771                 }
772                 break;
773 #ifdef IEEE80211_SUPPORT_MESH
774         case IEEE80211_ACTION_CAT_MESH:
775                 switch (ia->ia_action) {
776                 case IEEE80211_ACTION_MESH_LMETRIC:
777                         /*
778                          * XXX: verification is true only if we are using
779                          * Airtime link metric (default)
780                          */
781                         IEEE80211_VERIFY_LENGTH(efrm - frm,
782                             sizeof(struct ieee80211_meshlmetric_ie),
783                             return EINVAL);
784                         break;
785                 case IEEE80211_ACTION_MESH_HWMP:
786                         /* verify something */
787                         break;
788                 case IEEE80211_ACTION_MESH_GANN:
789                         IEEE80211_VERIFY_LENGTH(efrm - frm,
790                             sizeof(struct ieee80211_meshgann_ie),
791                             return EINVAL);
792                         break;
793                 case IEEE80211_ACTION_MESH_CC:
794                 case IEEE80211_ACTION_MESH_MCCA_SREQ:
795                 case IEEE80211_ACTION_MESH_MCCA_SREP:
796                 case IEEE80211_ACTION_MESH_MCCA_AREQ:
797                 case IEEE80211_ACTION_MESH_MCCA_ADVER:
798                 case IEEE80211_ACTION_MESH_MCCA_TRDOWN:
799                 case IEEE80211_ACTION_MESH_TBTT_REQ:
800                 case IEEE80211_ACTION_MESH_TBTT_RES:
801                         /* reject these early on, not implemented */
802                         IEEE80211_DISCARD(vap,
803                             IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
804                             wh, NULL, "not implemented yet, act=0x%02X",
805                             ia->ia_action);
806                         return EINVAL;
807                 }
808                 break;
809         case IEEE80211_ACTION_CAT_SELF_PROT:
810                 /* If TA or RA group address discard silently */
811                 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
812                         IEEE80211_IS_MULTICAST(wh->i_addr2))
813                         return EINVAL;
814                 /*
815                  * XXX: Should we verify complete length now or it is
816                  * to varying in sizes?
817                  */
818                 switch (ia->ia_action) {
819                 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
820                 case IEEE80211_ACTION_MESHPEERING_CLOSE:
821                         /* is not a peering candidate (yet) */
822                         if (ni == vap->iv_bss)
823                                 return EINVAL;
824                         break;
825                 }
826                 break;
827 #endif
828         }
829         return 0;
830 }
831
832 #ifdef IEEE80211_DEBUG
833 /*
834  * Debugging support.
835  */
836 void
837 ieee80211_ssid_mismatch(struct ieee80211vap *vap, const char *tag,
838         uint8_t mac[IEEE80211_ADDR_LEN], uint8_t *ssid)
839 {
840         kprintf("[%s] discard %s frame, ssid mismatch: ",
841                 ether_sprintf(mac), tag);
842         ieee80211_print_essid(ssid + 2, ssid[1]);
843         kprintf("\n");
844 }
845
846 /*
847  * Return the bssid of a frame.
848  */
849 static const uint8_t *
850 ieee80211_getbssid(const struct ieee80211vap *vap,
851         const struct ieee80211_frame *wh)
852 {
853         if (vap->iv_opmode == IEEE80211_M_STA)
854                 return wh->i_addr2;
855         if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
856                 return wh->i_addr1;
857         if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
858                 return wh->i_addr1;
859         return wh->i_addr3;
860 }
861
862 #include <machine/stdarg.h>
863
864 void
865 ieee80211_note(const struct ieee80211vap *vap, const char *fmt, ...)
866 {
867         char buf[128];          /* XXX */
868         osdep_va_list ap;
869
870         osdep_va_start(ap, fmt);
871         kvsnprintf(buf, sizeof(buf), fmt, ap);
872         osdep_va_end(ap);
873
874         if_printf(vap->iv_ifp, "%s", buf);      /* NB: no \n */
875 }
876
877 void
878 ieee80211_note_frame(const struct ieee80211vap *vap,
879         const struct ieee80211_frame *wh,
880         const char *fmt, ...)
881 {
882         char buf[128];          /* XXX */
883         osdep_va_list ap;
884
885         osdep_va_start(ap, fmt);
886         kvsnprintf(buf, sizeof(buf), fmt, ap);
887         osdep_va_end(ap);
888         if_printf(vap->iv_ifp, "[%s] %s\n",
889                 ether_sprintf(ieee80211_getbssid(vap, wh)), buf);
890 }
891
892 void
893 ieee80211_note_mac(const struct ieee80211vap *vap,
894         const uint8_t mac[IEEE80211_ADDR_LEN],
895         const char *fmt, ...)
896 {
897         char buf[128];          /* XXX */
898         osdep_va_list ap;
899
900         osdep_va_start(ap, fmt);
901         kvsnprintf(buf, sizeof(buf), fmt, ap);
902         osdep_va_end(ap);
903         if_printf(vap->iv_ifp, "[%s] %s\n", ether_sprintf(mac), buf);
904 }
905
906 void
907 ieee80211_discard_frame(const struct ieee80211vap *vap,
908         const struct ieee80211_frame *wh,
909         const char *type, const char *fmt, ...)
910 {
911         osdep_va_list ap;
912
913         if_printf(vap->iv_ifp, "[%s] discard ",
914                 ether_sprintf(ieee80211_getbssid(vap, wh)));
915         if (type == NULL) {
916                 kprintf("%s frame, ", ieee80211_mgt_subtype_name[
917                         (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) >>
918                         IEEE80211_FC0_SUBTYPE_SHIFT]);
919         } else
920                 kprintf("%s frame, ", type);
921         osdep_va_start(ap, fmt);
922         kvprintf(fmt, ap);
923         osdep_va_end(ap);
924         kprintf("\n");
925 }
926
927 void
928 ieee80211_discard_ie(const struct ieee80211vap *vap,
929         const struct ieee80211_frame *wh,
930         const char *type, const char *fmt, ...)
931 {
932         osdep_va_list ap;
933
934         if_printf(vap->iv_ifp, "[%s] discard ",
935                 ether_sprintf(ieee80211_getbssid(vap, wh)));
936         if (type != NULL)
937                 kprintf("%s information element, ", type);
938         else
939                 kprintf("information element, ");
940         osdep_va_start(ap, fmt);
941         kvprintf(fmt, ap);
942         osdep_va_end(ap);
943         kprintf("\n");
944 }
945
946 void
947 ieee80211_discard_mac(const struct ieee80211vap *vap,
948         const uint8_t mac[IEEE80211_ADDR_LEN],
949         const char *type, const char *fmt, ...)
950 {
951         osdep_va_list ap;
952
953         if_printf(vap->iv_ifp, "[%s] discard ", ether_sprintf(mac));
954         if (type != NULL)
955                 kprintf("%s frame, ", type);
956         else
957                 kprintf("frame, ");
958         osdep_va_start(ap, fmt);
959         kvprintf(fmt, ap);
960         osdep_va_end(ap);
961         kprintf("\n");
962 }
963 #endif /* IEEE80211_DEBUG */