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