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