Merge branch 'vendor/LIBEDIT'
[dragonfly.git] / contrib / hostapd / hostapd / wme.c
1 /*
2  * hostapd / WMM (Wi-Fi Multimedia)
3  * Copyright 2002-2003, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17
18 #include "hostapd.h"
19 #include "ieee802_11.h"
20 #include "wme.h"
21 #include "sta_info.h"
22 #include "driver.h"
23
24
25 /* TODO: maintain separate sequence and fragment numbers for each AC
26  * TODO: IGMP snooping to track which multicasts to forward - and use QOS-DATA
27  * if only WMM stations are receiving a certain group */
28
29
30 static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci)
31 {
32         u8 ret;
33         ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK;
34         if (acm)
35                 ret |= WMM_AC_ACM;
36         ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK;
37         return ret;
38 }
39
40
41 static inline u8 wmm_ecw(int ecwmin, int ecwmax)
42 {
43         return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) |
44                 ((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK);
45 }
46
47
48 /*
49  * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association
50  * Response frames.
51  */
52 u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid)
53 {
54         u8 *pos = eid;
55         struct wmm_parameter_element *wmm =
56                 (struct wmm_parameter_element *) (pos + 2);
57         int e;
58
59         if (!hapd->conf->wmm_enabled)
60                 return eid;
61         eid[0] = WLAN_EID_VENDOR_SPECIFIC;
62         wmm->oui[0] = 0x00;
63         wmm->oui[1] = 0x50;
64         wmm->oui[2] = 0xf2;
65         wmm->oui_type = WMM_OUI_TYPE;
66         wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT;
67         wmm->version = WMM_VERSION;
68         wmm->qos_info = hapd->parameter_set_count & 0xf;
69
70         /* fill in a parameter set record for each AC */
71         for (e = 0; e < 4; e++) {
72                 struct wmm_ac_parameter *ac = &wmm->ac[e];
73                 struct hostapd_wmm_ac_params *acp =
74                         &hapd->iconf->wmm_ac_params[e];
75
76                 ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
77                                               acp->admission_control_mandatory,
78                                               e);
79                 ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
80                 ac->txop_limit = host_to_le16(acp->txop_limit);
81         }
82
83         pos = (u8 *) (wmm + 1);
84         eid[1] = pos - eid - 2; /* element length */
85
86         return pos;
87 }
88
89
90 /* This function is called when a station sends an association request with
91  * WMM info element. The function returns zero on success or non-zero on any
92  * error in WMM element. eid does not include Element ID and Length octets. */
93 int hostapd_eid_wmm_valid(struct hostapd_data *hapd, u8 *eid, size_t len)
94 {
95         struct wmm_information_element *wmm;
96
97         wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
98
99         if (len < sizeof(struct wmm_information_element)) {
100                 wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
101                            (unsigned long) len);
102                 return -1;
103         }
104
105         wmm = (struct wmm_information_element *) eid;
106         wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x  "
107                    "OUI type %d  OUI sub-type %d  version %d  QoS info 0x%x",
108                    wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
109                    wmm->oui_subtype, wmm->version, wmm->qos_info);
110         if (wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
111             wmm->version != WMM_VERSION) {
112                 wpa_printf(MSG_DEBUG, "Unsupported WMM IE Subtype/Version");
113                 return -1;
114         }
115
116         return 0;
117 }
118
119
120 /* This function is called when a station sends an ACK frame for an AssocResp
121  * frame (status=success) and the matching AssocReq contained a WMM element.
122  */
123 int hostapd_wmm_sta_config(struct hostapd_data *hapd, struct sta_info *sta)
124 {
125         /* update kernel STA data for WMM related items (WLAN_STA_WPA flag) */
126         if (sta->flags & WLAN_STA_WMM)
127                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
128                                       WLAN_STA_WMM, ~0);
129         else
130                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
131                                       0, ~WLAN_STA_WMM);
132
133         return 0;
134 }
135
136
137 static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
138                             const struct wmm_tspec_element *tspec,
139                             u8 action_code, u8 dialogue_token, u8 status_code)
140 {
141         u8 buf[256];
142         struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
143         struct wmm_tspec_element *t = (struct wmm_tspec_element *)
144                 m->u.action.u.wmm_action.variable;
145         int len;
146
147         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
148                        HOSTAPD_LEVEL_DEBUG,
149                        "action response - reason %d", status_code);
150         os_memset(buf, 0, sizeof(buf));
151         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
152                                         WLAN_FC_STYPE_ACTION);
153         os_memcpy(m->da, addr, ETH_ALEN);
154         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
155         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
156         m->u.action.category = WLAN_ACTION_WMM;
157         m->u.action.u.wmm_action.action_code = action_code;
158         m->u.action.u.wmm_action.dialog_token = dialogue_token;
159         m->u.action.u.wmm_action.status_code = status_code;
160         os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
161         len = ((u8 *) (t + 1)) - buf;
162
163         if (hostapd_send_mgmt_frame(hapd, m, len, 0) < 0)
164                 perror("wmm_send_action: send");
165 }
166
167
168 static void wmm_addts_req(struct hostapd_data *hapd,
169                           struct ieee80211_mgmt *mgmt,
170                           struct wmm_tspec_element *tspec, size_t len)
171 {
172         u8 *end = ((u8 *) mgmt) + len;
173         int medium_time, pps, duration;
174         int up, psb, dir, tid;
175         u16 val, surplus;
176
177         if ((u8 *) (tspec + 1) > end) {
178                 wpa_printf(MSG_DEBUG, "WMM: TSPEC overflow in ADDTS Request");
179                 return;
180         }
181
182         wpa_printf(MSG_DEBUG, "WMM: ADDTS Request (Dialog Token %d) for TSPEC "
183                    "from " MACSTR,
184                    mgmt->u.action.u.wmm_action.dialog_token,
185                    MAC2STR(mgmt->sa));
186
187         up = (tspec->ts_info[1] >> 3) & 0x07;
188         psb = (tspec->ts_info[1] >> 2) & 0x01;
189         dir = (tspec->ts_info[0] >> 5) & 0x03;
190         tid = (tspec->ts_info[0] >> 1) & 0x0f;
191         wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d",
192                    up, psb, dir, tid);
193         val = le_to_host16(tspec->nominal_msdu_size);
194         wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s",
195                    val & 0x7fff, val & 0x8000 ? " (fixed)" : "");
196         wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps",
197                    le_to_host32(tspec->mean_data_rate));
198         wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps",
199                    le_to_host32(tspec->minimum_phy_rate));
200         val = le_to_host16(tspec->surplus_bandwidth_allowance);
201         wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u",
202                    val >> 13, 10000 * (val & 0x1fff) / 0x2000);
203
204         val = le_to_host16(tspec->nominal_msdu_size);
205         if (val == 0) {
206                 wpa_printf(MSG_DEBUG, "WMM: Invalid Nominal MSDU Size (0)");
207                 goto invalid;
208         }
209         /* pps = Ceiling((Mean Data Rate / 8) / Nominal MSDU Size) */
210         pps = ((le_to_host32(tspec->mean_data_rate) / 8) + val - 1) / val;
211         wpa_printf(MSG_DEBUG, "WMM: Packets-per-second estimate for TSPEC: %d",
212                    pps);
213
214         if (le_to_host32(tspec->minimum_phy_rate) < 1000000) {
215                 wpa_printf(MSG_DEBUG, "WMM: Too small Minimum PHY Rate");
216                 goto invalid;
217         }
218
219         duration = (le_to_host16(tspec->nominal_msdu_size) & 0x7fff) * 8 /
220                 (le_to_host32(tspec->minimum_phy_rate) / 1000000) +
221                 50 /* FIX: proper SIFS + ACK duration */;
222
223         /* unsigned binary number with an implicit binary point after the
224          * leftmost 3 bits, i.e., 0x2000 = 1.0 */
225         surplus = le_to_host16(tspec->surplus_bandwidth_allowance);
226         if (surplus <= 0x2000) {
227                 wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance not "
228                            "greater than unity");
229                 goto invalid;
230         }
231
232         medium_time = surplus * pps * duration / 0x2000;
233         wpa_printf(MSG_DEBUG, "WMM: Estimated medium time: %u", medium_time);
234
235         /*
236          * TODO: store list of granted (and still active) TSPECs and check
237          * whether there is available medium time for this request. For now,
238          * just refuse requests that would by themselves take very large
239          * portion of the available bandwidth.
240          */
241         if (medium_time > 750000) {
242                 wpa_printf(MSG_DEBUG, "WMM: Refuse TSPEC request for over "
243                            "75%% of available bandwidth");
244                 wmm_send_action(hapd, mgmt->sa, tspec,
245                                 WMM_ACTION_CODE_ADDTS_RESP,
246                                 mgmt->u.action.u.wmm_action.dialog_token,
247                                 WMM_ADDTS_STATUS_REFUSED);
248                 return;
249         }
250
251         /* Convert to 32 microseconds per second unit */
252         tspec->medium_time = host_to_le16(medium_time / 32);
253
254         wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP,
255                         mgmt->u.action.u.wmm_action.dialog_token,
256                         WMM_ADDTS_STATUS_ADMISSION_ACCEPTED);
257         return;
258
259 invalid:
260         wmm_send_action(hapd, mgmt->sa, tspec,
261                         WMM_ACTION_CODE_ADDTS_RESP,
262                         mgmt->u.action.u.wmm_action.dialog_token,
263                         WMM_ADDTS_STATUS_INVALID_PARAMETERS);
264 }
265
266
267 void hostapd_wmm_action(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
268                         size_t len)
269 {
270         int action_code;
271         int left = len - IEEE80211_HDRLEN - 4;
272         u8 *pos = ((u8 *) mgmt) + IEEE80211_HDRLEN + 4;
273         struct ieee802_11_elems elems;
274         struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
275
276         /* check that the request comes from a valid station */
277         if (!sta ||
278             (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
279             (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
280                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
281                                HOSTAPD_LEVEL_DEBUG,
282                                "wmm action received is not from associated wmm"
283                                " station");
284                 /* TODO: respond with action frame refused status code */
285                 return;
286         }
287
288         /* extract the tspec info element */
289         if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
290                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
291                                HOSTAPD_LEVEL_DEBUG,
292                                "hostapd_wmm_action - could not parse wmm "
293                                "action");
294                 /* TODO: respond with action frame invalid parameters status
295                  * code */
296                 return;
297         }
298
299         if (!elems.wmm_tspec ||
300             elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
301                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
302                                HOSTAPD_LEVEL_DEBUG,
303                                "hostapd_wmm_action - missing or wrong length "
304                                "tspec");
305                 /* TODO: respond with action frame invalid parameters status
306                  * code */
307                 return;
308         }
309
310         /* TODO: check the request is for an AC with ACM set, if not, refuse
311          * request */
312
313         action_code = mgmt->u.action.u.wmm_action.action_code;
314         switch (action_code) {
315         case WMM_ACTION_CODE_ADDTS_REQ:
316                 wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
317                               (elems.wmm_tspec - 2), len);
318                 return;
319 #if 0
320         /* TODO: needed for client implementation */
321         case WMM_ACTION_CODE_ADDTS_RESP:
322                 wmm_setup_request(hapd, mgmt, len);
323                 return;
324         /* TODO: handle station teardown requests */
325         case WMM_ACTION_CODE_DELTS:
326                 wmm_teardown(hapd, mgmt, len);
327                 return;
328 #endif
329         }
330
331         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
332                        HOSTAPD_LEVEL_DEBUG,
333                        "hostapd_wmm_action - unknown action code %d",
334                        action_code);
335 }