Use bit mask/shift and helper functions instead of C bit fields
[wpasupplicant] / 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_i.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 u8 wmm_oui[3] = { 0x00, 0x50, 0xf2 };
31
32
33 static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci)
34 {
35         u8 ret;
36         ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK;
37         if (acm)
38                 ret |= WMM_AC_ACM;
39         ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK;
40         return ret;
41 }
42
43
44 static inline u8 wmm_ecw(int ecwmin, int ecwmax)
45 {
46         return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) |
47                 ((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK);
48 }
49
50
51 /*
52  * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association
53  * Response frames.
54  */
55 u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid)
56 {
57         u8 *pos = eid;
58         struct wmm_parameter_element *wmm =
59                 (struct wmm_parameter_element *) (pos + 2);
60         int e;
61
62         if (!hapd->conf->wmm_enabled)
63                 return eid;
64         eid[0] = WLAN_EID_VENDOR_SPECIFIC;
65         wmm->oui[0] = 0x00;
66         wmm->oui[1] = 0x50;
67         wmm->oui[2] = 0xf2;
68         wmm->oui_type = WMM_OUI_TYPE;
69         wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT;
70         wmm->version = WMM_VERSION;
71         wmm->qos_info = hapd->parameter_set_count & 0xf;
72
73         /* fill in a parameter set record for each AC */
74         for (e = 0; e < 4; e++) {
75                 struct wmm_ac_parameter *ac = &wmm->ac[e];
76                 struct hostapd_wmm_ac_params *acp =
77                         &hapd->iconf->wmm_ac_params[e];
78
79                 ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
80                                               acp->admission_control_mandatory,
81                                               e);
82                 ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
83                 ac->txop_limit = host_to_le16(acp->txop_limit);
84         }
85
86         pos = (u8 *) (wmm + 1);
87         eid[1] = pos - eid - 2; /* element length */
88
89         return pos;
90 }
91
92
93 /* This function is called when a station sends an association request with
94  * WMM info element. The function returns zero on success or non-zero on any
95  * error in WMM element. eid does not include Element ID and Length octets. */
96 int hostapd_eid_wmm_valid(struct hostapd_data *hapd, u8 *eid, size_t len)
97 {
98         struct wmm_information_element *wmm;
99
100         wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
101
102         if (len < sizeof(struct wmm_information_element)) {
103                 wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
104                            (unsigned long) len);
105                 return -1;
106         }
107
108         wmm = (struct wmm_information_element *) eid;
109         wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x  "
110                    "OUI type %d  OUI sub-type %d  version %d",
111                    wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
112                    wmm->oui_subtype, wmm->version);
113         if (os_memcmp(wmm->oui, wmm_oui, sizeof(wmm_oui)) != 0 ||
114             wmm->oui_type != WMM_OUI_TYPE ||
115             wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
116             wmm->version != WMM_VERSION) {
117                 wpa_printf(MSG_DEBUG, "Unsupported WMM IE OUI/Type/Subtype/"
118                            "Version");
119                 return -1;
120         }
121
122         return 0;
123 }
124
125
126 /* This function is called when a station sends an ACK frame for an AssocResp
127  * frame (status=success) and the matching AssocReq contained a WMM element.
128  */
129 int hostapd_wmm_sta_config(struct hostapd_data *hapd, struct sta_info *sta)
130 {
131         /* update kernel STA data for WMM related items (WLAN_STA_WPA flag) */
132         if (sta->flags & WLAN_STA_WMM)
133                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
134                                       WLAN_STA_WMM, ~0);
135         else
136                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
137                                       0, ~WLAN_STA_WMM);
138
139         return 0;
140 }
141
142
143 static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
144                             const struct wmm_tspec_element *tspec,
145                             u8 action_code, u8 dialogue_token, u8 status_code)
146 {
147         u8 buf[256];
148         struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
149         struct wmm_tspec_element *t = (struct wmm_tspec_element *)
150                 m->u.action.u.wmm_action.variable;
151         int len;
152
153         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
154                        HOSTAPD_LEVEL_DEBUG,
155                        "action response - reason %d", status_code);
156         os_memset(buf, 0, sizeof(buf));
157         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
158                                         WLAN_FC_STYPE_ACTION);
159         os_memcpy(m->da, addr, ETH_ALEN);
160         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
161         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
162         m->u.action.category = WLAN_ACTION_WMM;
163         m->u.action.u.wmm_action.action_code = action_code;
164         m->u.action.u.wmm_action.dialog_token = dialogue_token;
165         m->u.action.u.wmm_action.status_code = status_code;
166         os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
167         len = ((u8 *) (t + 1)) - buf;
168
169         if (hostapd_send_mgmt_frame(hapd, m, len, 0) < 0)
170                 perror("wmm_send_action: send");
171 }
172
173
174 /* given frame data payload size in bytes, and data_rate in bits per second
175  * returns time to complete frame exchange */
176 /* FIX: should not use floating point types */
177 static double wmm_frame_exchange_time(int bytes, int data_rate, int encryption,
178                                       int cts_protection)
179 {
180         /* TODO: account for MAC/PHY headers correctly */
181         /* TODO: account for encryption headers */
182         /* TODO: account for WDS headers */
183         /* TODO: account for CTS protection */
184         /* TODO: account for SIFS + ACK at minimum PHY rate */
185         return (bytes + 400) * 8.0 / data_rate;
186 }
187
188
189 static void wmm_addts_req(struct hostapd_data *hapd,
190                           struct ieee80211_mgmt *mgmt,
191                           struct wmm_tspec_element *tspec, size_t len)
192 {
193         /* FIX: should not use floating point types */
194         double medium_time, pps;
195
196         /* TODO: account for airtime and answer no to tspec setup requests
197          * when none left!! */
198
199         pps = (le_to_host32(tspec->mean_data_rate) / 8.0) /
200                 le_to_host16(tspec->nominal_msdu_size);
201         medium_time = (le_to_host16(tspec->surplus_bandwidth_allowance) / 8) *
202                 pps *
203                 wmm_frame_exchange_time(le_to_host16(tspec->nominal_msdu_size),
204                                         le_to_host32(tspec->minimum_phy_rate),
205                                         0, 0);
206         tspec->medium_time = host_to_le16(medium_time * 1000000.0 / 32.0);
207
208         wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP,
209                         mgmt->u.action.u.wmm_action.dialog_token,
210                         WMM_ADDTS_STATUS_ADMISSION_ACCEPTED);
211 }
212
213
214 void hostapd_wmm_action(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
215                         size_t len)
216 {
217         int action_code;
218         int left = len - IEEE80211_HDRLEN - 4;
219         u8 *pos = ((u8 *) mgmt) + IEEE80211_HDRLEN + 4;
220         struct ieee802_11_elems elems;
221         struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
222
223         /* check that the request comes from a valid station */
224         if (!sta ||
225             (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
226             (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
227                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
228                                HOSTAPD_LEVEL_DEBUG,
229                                "wmm action received is not from associated wmm"
230                                " station");
231                 /* TODO: respond with action frame refused status code */
232                 return;
233         }
234
235         /* extract the tspec info element */
236         if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
237                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
238                                HOSTAPD_LEVEL_DEBUG,
239                                "hostapd_wmm_action - could not parse wmm "
240                                "action");
241                 /* TODO: respond with action frame invalid parameters status
242                  * code */
243                 return;
244         }
245
246         if (!elems.wmm_tspec ||
247             elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
248                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
249                                HOSTAPD_LEVEL_DEBUG,
250                                "hostapd_wmm_action - missing or wrong length "
251                                "tspec");
252                 /* TODO: respond with action frame invalid parameters status
253                  * code */
254                 return;
255         }
256
257         /* TODO: check the request is for an AC with ACM set, if not, refuse
258          * request */
259
260         action_code = mgmt->u.action.u.wmm_action.action_code;
261         switch (action_code) {
262         case WMM_ACTION_CODE_ADDTS_REQ:
263                 wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
264                               (elems.wmm_tspec - 2), len);
265                 return;
266 #if 0
267         /* TODO: needed for client implementation */
268         case WMM_ACTION_CODE_ADDTS_RESP:
269                 wmm_setup_request(hapd, mgmt, len);
270                 return;
271         /* TODO: handle station teardown requests */
272         case WMM_ACTION_CODE_DELTS:
273                 wmm_teardown(hapd, mgmt, len);
274                 return;
275 #endif
276         }
277
278         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
279                        HOSTAPD_LEVEL_DEBUG,
280                        "hostapd_wmm_action - unknown action code %d",
281                        action_code);
282 }