Coverage Report

Created: 2025-10-28 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/wpa_supplicant/wmm_ac.c
Line
Count
Source
1
/*
2
 * Wi-Fi Multimedia Admission Control (WMM-AC)
3
 * Copyright(c) 2014, Intel Mobile Communication GmbH.
4
 * Copyright(c) 2014, Intel Corporation. All rights reserved.
5
 *
6
 * This software may be distributed under the terms of the BSD license.
7
 * See README for more details.
8
 */
9
10
#include "includes.h"
11
12
#include "utils/common.h"
13
#include "utils/list.h"
14
#include "utils/eloop.h"
15
#include "common/ieee802_11_common.h"
16
#include "wpa_supplicant_i.h"
17
#include "bss.h"
18
#include "driver_i.h"
19
#include "wmm_ac.h"
20
21
static void wmm_ac_addts_req_timeout(void *eloop_ctx, void *timeout_ctx);
22
23
static const enum wmm_ac up_to_ac[8] = {
24
  WMM_AC_BK,
25
  WMM_AC_BE,
26
  WMM_AC_BE,
27
  WMM_AC_BK,
28
  WMM_AC_VI,
29
  WMM_AC_VI,
30
  WMM_AC_VO,
31
  WMM_AC_VO
32
};
33
34
35
static inline u8 wmm_ac_get_tsid(const struct wmm_tspec_element *tspec)
36
0
{
37
0
  return (tspec->ts_info[0] >> 1) & 0x0f;
38
0
}
39
40
41
static u8 wmm_ac_get_direction(const struct wmm_tspec_element *tspec)
42
0
{
43
0
  return (tspec->ts_info[0] >> 5) & 0x03;
44
0
}
45
46
47
static u8 wmm_ac_get_user_priority(const struct wmm_tspec_element *tspec)
48
0
{
49
0
  return (tspec->ts_info[1] >> 3) & 0x07;
50
0
}
51
52
53
static u8 wmm_ac_direction_to_idx(u8 direction)
54
0
{
55
0
  switch (direction) {
56
0
  case WMM_AC_DIR_UPLINK:
57
0
    return TS_DIR_IDX_UPLINK;
58
0
  case WMM_AC_DIR_DOWNLINK:
59
0
    return TS_DIR_IDX_DOWNLINK;
60
0
  case WMM_AC_DIR_BIDIRECTIONAL:
61
0
    return TS_DIR_IDX_BIDI;
62
0
  default:
63
0
    wpa_printf(MSG_ERROR, "Invalid direction: %d", direction);
64
0
    return WMM_AC_DIR_UPLINK;
65
0
  }
66
0
}
67
68
69
static int wmm_ac_add_ts(struct wpa_supplicant *wpa_s, const u8 *addr,
70
       const struct wmm_tspec_element *tspec)
71
0
{
72
0
  struct wmm_tspec_element *_tspec;
73
0
  int ret;
74
0
  u16 admitted_time = le_to_host16(tspec->medium_time);
75
0
  u8 up = wmm_ac_get_user_priority(tspec);
76
0
  u8 ac = up_to_ac[up];
77
0
  u8 dir = wmm_ac_get_direction(tspec);
78
0
  u8 tsid = wmm_ac_get_tsid(tspec);
79
0
  enum ts_dir_idx idx = wmm_ac_direction_to_idx(dir);
80
81
  /* should have been verified before, but double-check here */
82
0
  if (wpa_s->tspecs[ac][idx]) {
83
0
    wpa_printf(MSG_ERROR,
84
0
         "WMM AC: tspec (ac=%d, dir=%d) already exists!",
85
0
         ac, dir);
86
0
    return -1;
87
0
  }
88
89
  /* copy tspec */
90
0
  _tspec = os_memdup(tspec, sizeof(*_tspec));
91
0
  if (!_tspec)
92
0
    return -1;
93
94
0
  if (dir != WMM_AC_DIR_DOWNLINK) {
95
0
    ret = wpa_drv_add_ts(wpa_s, tsid, addr, up, admitted_time);
96
0
    wpa_printf(MSG_DEBUG,
97
0
         "WMM AC: Add TS: addr=" MACSTR
98
0
         " TSID=%u admitted time=%u, ret=%d",
99
0
         MAC2STR(addr), tsid, admitted_time, ret);
100
0
    if (ret < 0) {
101
0
      os_free(_tspec);
102
0
      return -1;
103
0
    }
104
0
  }
105
106
0
  wpa_s->tspecs[ac][idx] = _tspec;
107
108
0
  wpa_printf(MSG_DEBUG, "Traffic stream was created successfully");
109
110
0
  wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_ADDED
111
0
    "tsid=%d addr=" MACSTR " admitted_time=%d",
112
0
    tsid, MAC2STR(addr), admitted_time);
113
114
0
  return 0;
115
0
}
116
117
118
static void wmm_ac_del_ts_idx(struct wpa_supplicant *wpa_s, u8 ac,
119
            enum ts_dir_idx dir)
120
0
{
121
0
  struct wmm_tspec_element *tspec = wpa_s->tspecs[ac][dir];
122
0
  u8 tsid;
123
124
0
  if (!tspec)
125
0
    return;
126
127
0
  tsid = wmm_ac_get_tsid(tspec);
128
0
  wpa_printf(MSG_DEBUG, "WMM AC: Del TS ac=%d tsid=%d", ac, tsid);
129
130
  /* update the driver in case of uplink/bidi */
131
0
  if (wmm_ac_get_direction(tspec) != WMM_AC_DIR_DOWNLINK)
132
0
    wpa_drv_del_ts(wpa_s, tsid, wpa_s->bssid);
133
134
0
  wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REMOVED
135
0
    "tsid=%d addr=" MACSTR, tsid, MAC2STR(wpa_s->bssid));
136
137
0
  os_free(wpa_s->tspecs[ac][dir]);
138
0
  wpa_s->tspecs[ac][dir] = NULL;
139
0
}
140
141
142
static void wmm_ac_del_req(struct wpa_supplicant *wpa_s, int failed)
143
0
{
144
0
  struct wmm_ac_addts_request *req = wpa_s->addts_request;
145
146
0
  if (!req)
147
0
    return;
148
149
0
  if (failed)
150
0
    wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REQ_FAILED
151
0
      "tsid=%u", wmm_ac_get_tsid(&req->tspec));
152
153
0
  eloop_cancel_timeout(wmm_ac_addts_req_timeout, wpa_s, req);
154
0
  wpa_s->addts_request = NULL;
155
0
  os_free(req);
156
0
}
157
158
159
static void wmm_ac_addts_req_timeout(void *eloop_ctx, void *timeout_ctx)
160
0
{
161
0
  struct wpa_supplicant *wpa_s = eloop_ctx;
162
0
  struct wmm_ac_addts_request *addts_req = timeout_ctx;
163
164
0
  wpa_printf(MSG_DEBUG,
165
0
       "Timeout getting ADDTS response (tsid=%d up=%d)",
166
0
       wmm_ac_get_tsid(&addts_req->tspec),
167
0
       wmm_ac_get_user_priority(&addts_req->tspec));
168
169
0
  wmm_ac_del_req(wpa_s, 1);
170
0
}
171
172
173
static int wmm_ac_send_addts_request(struct wpa_supplicant *wpa_s,
174
             const struct wmm_ac_addts_request *req)
175
0
{
176
0
  struct wpabuf *buf;
177
0
  int ret;
178
179
0
  wpa_printf(MSG_DEBUG, "Sending ADDTS Request to " MACSTR,
180
0
       MAC2STR(req->address));
181
182
  /* category + action code + dialog token + status + sizeof(tspec) */
183
0
  buf = wpabuf_alloc(4 + sizeof(req->tspec));
184
0
  if (!buf) {
185
0
    wpa_printf(MSG_ERROR, "WMM AC: Allocation error");
186
0
    return -1;
187
0
  }
188
189
0
  wpabuf_put_u8(buf, WLAN_ACTION_WMM);
190
0
  wpabuf_put_u8(buf, WMM_ACTION_CODE_ADDTS_REQ);
191
0
  wpabuf_put_u8(buf, req->dialog_token);
192
0
  wpabuf_put_u8(buf, 0); /* status code */
193
0
  wpabuf_put_data(buf, &req->tspec, sizeof(req->tspec));
194
195
0
  ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, req->address,
196
0
        wpa_s->own_addr, wpa_s->bssid,
197
0
        wpabuf_head(buf), wpabuf_len(buf), 0);
198
0
  if (ret) {
199
0
    wpa_printf(MSG_WARNING,
200
0
         "WMM AC: Failed to send ADDTS Request");
201
0
  }
202
203
0
  wpabuf_free(buf);
204
0
  return ret;
205
0
}
206
207
208
static int wmm_ac_send_delts(struct wpa_supplicant *wpa_s,
209
           const struct wmm_tspec_element *tspec,
210
           const u8 *address)
211
0
{
212
0
  struct wpabuf *buf;
213
0
  int ret;
214
215
  /* category + action code + dialog token + status + sizeof(tspec) */
216
0
  buf = wpabuf_alloc(4 + sizeof(*tspec));
217
0
  if (!buf)
218
0
    return -1;
219
220
0
  wpa_printf(MSG_DEBUG, "Sending DELTS to " MACSTR, MAC2STR(address));
221
222
  /* category + action code + dialog token + status + sizeof(tspec) */
223
0
  wpabuf_put_u8(buf, WLAN_ACTION_WMM);
224
0
  wpabuf_put_u8(buf, WMM_ACTION_CODE_DELTS);
225
0
  wpabuf_put_u8(buf, 0); /* Dialog Token (not used) */
226
0
  wpabuf_put_u8(buf, 0); /* Status Code (not used) */
227
0
  wpabuf_put_data(buf, tspec, sizeof(*tspec));
228
229
0
  ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, address,
230
0
          wpa_s->own_addr, wpa_s->bssid,
231
0
          wpabuf_head(buf), wpabuf_len(buf), 0);
232
0
  if (ret)
233
0
    wpa_printf(MSG_WARNING, "Failed to send DELTS frame");
234
235
0
  wpabuf_free(buf);
236
0
  return ret;
237
0
}
238
239
240
/* return the AC using the given TSPEC tid */
241
static int wmm_ac_find_tsid(struct wpa_supplicant *wpa_s, u8 tsid,
242
          enum ts_dir_idx *dir)
243
0
{
244
0
  int ac;
245
0
  enum ts_dir_idx idx;
246
247
0
  for (ac = 0; ac < WMM_AC_NUM; ac++) {
248
0
    for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
249
0
      if (wpa_s->tspecs[ac][idx] &&
250
0
          wmm_ac_get_tsid(wpa_s->tspecs[ac][idx]) == tsid) {
251
0
        if (dir)
252
0
          *dir = idx;
253
0
        return ac;
254
0
      }
255
0
    }
256
0
  }
257
258
0
  return -1;
259
0
}
260
261
262
static struct wmm_ac_addts_request *
263
wmm_ac_build_addts_req(struct wpa_supplicant *wpa_s,
264
           const struct wmm_ac_ts_setup_params *params,
265
           const u8 *address)
266
0
{
267
0
  struct wmm_ac_addts_request *addts_req;
268
0
  struct wmm_tspec_element *tspec;
269
0
  u8 ac = up_to_ac[params->user_priority];
270
0
  u8 uapsd = wpa_s->wmm_ac_assoc_info->ac_params[ac].uapsd;
271
272
0
  addts_req = os_zalloc(sizeof(*addts_req));
273
0
  if (!addts_req)
274
0
    return NULL;
275
276
0
  tspec = &addts_req->tspec;
277
0
  os_memcpy(addts_req->address, address, ETH_ALEN);
278
279
  /* The dialog token cannot be zero */
280
0
  if (++wpa_s->wmm_ac_last_dialog_token == 0)
281
0
    wpa_s->wmm_ac_last_dialog_token++;
282
283
0
  addts_req->dialog_token = wpa_s->wmm_ac_last_dialog_token;
284
0
  tspec->eid = WLAN_EID_VENDOR_SPECIFIC;
285
0
  tspec->length = sizeof(*tspec) - 2; /* reduce eid and length */
286
0
  tspec->oui[0] = 0x00;
287
0
  tspec->oui[1] = 0x50;
288
0
  tspec->oui[2] = 0xf2;
289
0
  tspec->oui_type = WMM_OUI_TYPE;
290
0
  tspec->oui_subtype = WMM_OUI_SUBTYPE_TSPEC_ELEMENT;
291
0
  tspec->version = WMM_VERSION;
292
293
0
  tspec->ts_info[0] = params->tsid << 1;
294
0
  tspec->ts_info[0] |= params->direction << 5;
295
0
  tspec->ts_info[0] |= WMM_AC_ACCESS_POLICY_EDCA << 7;
296
0
  tspec->ts_info[1] = uapsd << 2;
297
0
  tspec->ts_info[1] |= params->user_priority << 3;
298
0
  tspec->ts_info[2] = 0;
299
300
0
  tspec->nominal_msdu_size = host_to_le16(params->nominal_msdu_size);
301
0
  if (params->fixed_nominal_msdu)
302
0
    tspec->nominal_msdu_size |=
303
0
      host_to_le16(WMM_AC_FIXED_MSDU_SIZE);
304
305
0
  tspec->mean_data_rate = host_to_le32(params->mean_data_rate);
306
0
  tspec->minimum_phy_rate = host_to_le32(params->minimum_phy_rate);
307
0
  tspec->surplus_bandwidth_allowance =
308
0
    host_to_le16(params->surplus_bandwidth_allowance);
309
310
0
  return addts_req;
311
0
}
312
313
314
static int param_in_range(const char *name, long value,
315
        long min_val, long max_val)
316
0
{
317
0
  if (value < min_val || (max_val >= 0 && value > max_val)) {
318
0
    wpa_printf(MSG_DEBUG,
319
0
         "WMM AC: param %s (%ld) is out of range (%ld-%ld)",
320
0
         name, value, min_val, max_val);
321
0
    return 0;
322
0
  }
323
324
0
  return 1;
325
0
}
326
327
328
static int wmm_ac_should_replace_ts(struct wpa_supplicant *wpa_s,
329
            u8 tsid, u8 ac, u8 dir)
330
0
{
331
0
  enum ts_dir_idx idx;
332
0
  int cur_ac, existing_ts = 0, replace_ts = 0;
333
334
0
  cur_ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
335
0
  if (cur_ac >= 0) {
336
0
    if (cur_ac != ac) {
337
0
      wpa_printf(MSG_DEBUG,
338
0
           "WMM AC: TSID %i already exists on different ac (%d)",
339
0
           tsid, cur_ac);
340
0
      return -1;
341
0
    }
342
343
    /* same tsid - this tspec will replace the current one */
344
0
    replace_ts |= BIT(idx);
345
0
  }
346
347
0
  for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
348
0
    if (wpa_s->tspecs[ac][idx])
349
0
      existing_ts |= BIT(idx);
350
0
  }
351
352
0
  switch (dir) {
353
0
  case WMM_AC_DIR_UPLINK:
354
    /* replace existing uplink/bidi tspecs */
355
0
    replace_ts |= existing_ts & (BIT(TS_DIR_IDX_UPLINK) |
356
0
               BIT(TS_DIR_IDX_BIDI));
357
0
    break;
358
0
  case WMM_AC_DIR_DOWNLINK:
359
    /* replace existing downlink/bidi tspecs */
360
0
    replace_ts |= existing_ts & (BIT(TS_DIR_IDX_DOWNLINK) |
361
0
               BIT(TS_DIR_IDX_BIDI));
362
0
    break;
363
0
  case WMM_AC_DIR_BIDIRECTIONAL:
364
    /* replace all existing tspecs */
365
0
    replace_ts |= existing_ts;
366
0
    break;
367
0
  default:
368
0
    return -1;
369
0
  }
370
371
0
  return replace_ts;
372
0
}
373
374
375
static int wmm_ac_ts_req_is_valid(struct wpa_supplicant *wpa_s,
376
          const struct wmm_ac_ts_setup_params *params)
377
0
{
378
0
  enum wmm_ac req_ac;
379
380
0
#define PARAM_IN_RANGE(field, min_value, max_value) \
381
0
  param_in_range(#field, params->field, min_value, max_value)
382
383
0
  if (!PARAM_IN_RANGE(tsid, 0, WMM_AC_MAX_TID) ||
384
0
      !PARAM_IN_RANGE(user_priority, 0, WMM_AC_MAX_USER_PRIORITY) ||
385
0
      !PARAM_IN_RANGE(nominal_msdu_size, 1, WMM_AC_MAX_NOMINAL_MSDU) ||
386
0
      !PARAM_IN_RANGE(mean_data_rate, 1, -1) ||
387
0
      !PARAM_IN_RANGE(minimum_phy_rate, 1, -1) ||
388
0
      !PARAM_IN_RANGE(surplus_bandwidth_allowance, WMM_AC_MIN_SBA_UNITY,
389
0
          -1))
390
0
    return 0;
391
0
#undef PARAM_IN_RANGE
392
393
0
  if (!(params->direction == WMM_TSPEC_DIRECTION_UPLINK ||
394
0
        params->direction == WMM_TSPEC_DIRECTION_DOWNLINK ||
395
0
        params->direction == WMM_TSPEC_DIRECTION_BI_DIRECTIONAL)) {
396
0
    wpa_printf(MSG_DEBUG, "WMM AC: invalid TS direction: %d",
397
0
         params->direction);
398
0
    return 0;
399
0
  }
400
401
0
  req_ac = up_to_ac[params->user_priority];
402
403
  /* Requested access category must have acm */
404
0
  if (!wpa_s->wmm_ac_assoc_info->ac_params[req_ac].acm) {
405
0
    wpa_printf(MSG_DEBUG, "WMM AC: AC %d is not ACM", req_ac);
406
0
    return 0;
407
0
  }
408
409
0
  if (wmm_ac_should_replace_ts(wpa_s, params->tsid, req_ac,
410
0
             params->direction) < 0)
411
0
    return 0;
412
413
0
  return 1;
414
0
}
415
416
417
static struct wmm_ac_assoc_data *
418
wmm_ac_process_param_elem(struct wpa_supplicant *wpa_s, const u8 *ies,
419
        size_t ies_len)
420
0
{
421
0
  struct ieee802_11_elems elems;
422
0
  struct wmm_parameter_element *wmm_params;
423
0
  struct wmm_ac_assoc_data *assoc_data;
424
0
  int i;
425
426
  /* Parsing WMM Parameter Element */
427
0
  if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
428
0
    wpa_printf(MSG_DEBUG, "WMM AC: could not parse assoc ies");
429
0
    return NULL;
430
0
  }
431
432
0
  if (!elems.wmm) {
433
0
    wpa_printf(MSG_DEBUG, "WMM AC: No WMM IE");
434
0
    return NULL;
435
0
  }
436
437
0
  if (elems.wmm_len != sizeof(*wmm_params)) {
438
0
    wpa_printf(MSG_DEBUG, "WMM AC: Invalid WMM ie length");
439
0
    return NULL;
440
0
  }
441
442
0
  wmm_params = (struct wmm_parameter_element *)(elems.wmm);
443
444
0
  assoc_data = os_zalloc(sizeof(*assoc_data));
445
0
  if (!assoc_data)
446
0
    return NULL;
447
448
0
  for (i = 0; i < WMM_AC_NUM; i++)
449
0
    assoc_data->ac_params[i].acm =
450
0
      !!(wmm_params->ac[i].aci_aifsn & WMM_AC_ACM);
451
452
0
  wpa_printf(MSG_DEBUG,
453
0
       "WMM AC: AC mandatory: AC_BE=%u AC_BK=%u AC_VI=%u AC_VO=%u",
454
0
       assoc_data->ac_params[WMM_AC_BE].acm,
455
0
       assoc_data->ac_params[WMM_AC_BK].acm,
456
0
       assoc_data->ac_params[WMM_AC_VI].acm,
457
0
       assoc_data->ac_params[WMM_AC_VO].acm);
458
459
0
  return assoc_data;
460
0
}
461
462
463
static int wmm_ac_init(struct wpa_supplicant *wpa_s, const u8 *ies,
464
           size_t ies_len, const struct wmm_params *wmm_params)
465
0
{
466
0
  struct wmm_ac_assoc_data *assoc_data;
467
0
  u8 ac;
468
469
0
  if (wpa_s->wmm_ac_assoc_info) {
470
0
    wpa_printf(MSG_ERROR, "WMM AC: Already initialized");
471
0
    return -1;
472
0
  }
473
474
0
  if (!ies || !(wmm_params->info_bitmap & WMM_PARAMS_UAPSD_QUEUES_INFO)) {
475
    /* WMM AC not in use for this connection */
476
0
    return -1;
477
0
  }
478
479
0
  os_memset(wpa_s->tspecs, 0, sizeof(wpa_s->tspecs));
480
0
  wpa_s->wmm_ac_last_dialog_token = 0;
481
0
  wpa_s->addts_request = NULL;
482
483
0
  assoc_data = wmm_ac_process_param_elem(wpa_s, ies, ies_len);
484
0
  if (!assoc_data)
485
0
    return -1;
486
487
0
  wpa_printf(MSG_DEBUG, "WMM AC: U-APSD queues=0x%x",
488
0
       wmm_params->uapsd_queues);
489
490
0
  for (ac = 0; ac < WMM_AC_NUM; ac++) {
491
0
    assoc_data->ac_params[ac].uapsd =
492
0
      !!(wmm_params->uapsd_queues & BIT(ac));
493
0
  }
494
495
0
  wpa_s->wmm_ac_assoc_info = assoc_data;
496
0
  return 0;
497
0
}
498
499
500
static void wmm_ac_del_ts(struct wpa_supplicant *wpa_s, u8 ac, int dir_bitmap)
501
0
{
502
0
  enum ts_dir_idx idx;
503
504
0
  for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
505
0
    if (!(dir_bitmap & BIT(idx)))
506
0
      continue;
507
508
0
    wmm_ac_del_ts_idx(wpa_s, ac, idx);
509
0
  }
510
0
}
511
512
513
static void wmm_ac_deinit(struct wpa_supplicant *wpa_s)
514
0
{
515
0
  int i;
516
517
0
  for (i = 0; i < WMM_AC_NUM; i++)
518
0
    wmm_ac_del_ts(wpa_s, i, TS_DIR_IDX_ALL);
519
520
  /* delete pending add_ts request */
521
0
  wmm_ac_del_req(wpa_s, 1);
522
523
0
  os_free(wpa_s->wmm_ac_assoc_info);
524
0
  wpa_s->wmm_ac_assoc_info = NULL;
525
0
}
526
527
528
void wmm_ac_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *ies,
529
       size_t ies_len, const struct wmm_params *wmm_params)
530
0
{
531
0
  if (wmm_ac_init(wpa_s, ies, ies_len, wmm_params))
532
0
    return;
533
534
0
  wpa_printf(MSG_DEBUG,
535
0
       "WMM AC: Valid WMM association, WMM AC is enabled");
536
0
}
537
538
539
void wmm_ac_notify_disassoc(struct wpa_supplicant *wpa_s)
540
0
{
541
0
  if (!wpa_s->wmm_ac_assoc_info)
542
0
    return;
543
544
0
  wmm_ac_deinit(wpa_s);
545
0
  wpa_printf(MSG_DEBUG, "WMM AC: WMM AC is disabled");
546
0
}
547
548
549
int wpas_wmm_ac_delts(struct wpa_supplicant *wpa_s, u8 tsid)
550
0
{
551
0
  struct wmm_tspec_element tspec;
552
0
  int ac;
553
0
  enum ts_dir_idx dir;
554
555
0
  if (!wpa_s->wmm_ac_assoc_info) {
556
0
    wpa_printf(MSG_DEBUG,
557
0
         "WMM AC: Failed to delete TS, WMM AC is disabled");
558
0
    return -1;
559
0
  }
560
561
0
  ac = wmm_ac_find_tsid(wpa_s, tsid, &dir);
562
0
  if (ac < 0) {
563
0
    wpa_printf(MSG_DEBUG, "WMM AC: TS does not exist");
564
0
    return -1;
565
0
  }
566
567
0
  tspec = *wpa_s->tspecs[ac][dir];
568
569
0
  wmm_ac_del_ts_idx(wpa_s, ac, dir);
570
571
0
  wmm_ac_send_delts(wpa_s, &tspec, wpa_s->bssid);
572
573
0
  return 0;
574
0
}
575
576
577
int wpas_wmm_ac_addts(struct wpa_supplicant *wpa_s,
578
          struct wmm_ac_ts_setup_params *params)
579
0
{
580
0
  struct wmm_ac_addts_request *addts_req;
581
582
0
  if (!wpa_s->wmm_ac_assoc_info) {
583
0
    wpa_printf(MSG_DEBUG,
584
0
         "WMM AC: Cannot add TS - missing assoc data");
585
0
    return -1;
586
0
  }
587
588
0
  if (wpa_s->addts_request) {
589
0
    wpa_printf(MSG_DEBUG,
590
0
         "WMM AC: can't add TS - ADDTS request is already pending");
591
0
    return -1;
592
0
  }
593
594
  /*
595
   * we can setup downlink TS even without driver support.
596
   * however, we need driver support for the other directions.
597
   */
598
0
  if (params->direction != WMM_AC_DIR_DOWNLINK &&
599
0
      !wpa_s->wmm_ac_supported) {
600
0
    wpa_printf(MSG_DEBUG,
601
0
         "Cannot set uplink/bidi TS without driver support");
602
0
    return -1;
603
0
  }
604
605
0
  if (!wmm_ac_ts_req_is_valid(wpa_s, params))
606
0
    return -1;
607
608
0
  wpa_printf(MSG_DEBUG, "WMM AC: TS setup request (addr=" MACSTR
609
0
       " tsid=%u user priority=%u direction=%d)",
610
0
       MAC2STR(wpa_s->bssid), params->tsid,
611
0
       params->user_priority, params->direction);
612
613
0
  addts_req = wmm_ac_build_addts_req(wpa_s, params, wpa_s->bssid);
614
0
  if (!addts_req)
615
0
    return -1;
616
617
0
  if (wmm_ac_send_addts_request(wpa_s, addts_req))
618
0
    goto err;
619
620
  /* save as pending and set ADDTS resp timeout to 1 second */
621
0
  wpa_s->addts_request = addts_req;
622
0
  eloop_register_timeout(1, 0, wmm_ac_addts_req_timeout,
623
0
             wpa_s, addts_req);
624
0
  return 0;
625
0
err:
626
0
  os_free(addts_req);
627
0
  return -1;
628
0
}
629
630
631
static void wmm_ac_handle_delts(struct wpa_supplicant *wpa_s, const u8 *sa,
632
        const struct wmm_tspec_element *tspec)
633
0
{
634
0
  int ac;
635
0
  u8 tsid;
636
0
  enum ts_dir_idx idx;
637
638
0
  tsid = wmm_ac_get_tsid(tspec);
639
640
0
  wpa_printf(MSG_DEBUG,
641
0
       "WMM AC: DELTS frame has been received TSID=%u addr="
642
0
       MACSTR, tsid, MAC2STR(sa));
643
644
0
  ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
645
0
  if (ac < 0) {
646
0
    wpa_printf(MSG_DEBUG,
647
0
         "WMM AC: Ignoring DELTS frame - TSID does not exist");
648
0
    return;
649
0
  }
650
651
0
  wmm_ac_del_ts_idx(wpa_s, ac, idx);
652
653
0
  wpa_printf(MSG_DEBUG,
654
0
       "TS was deleted successfully (tsid=%u address=" MACSTR ")",
655
0
       tsid, MAC2STR(sa));
656
0
}
657
658
659
static void wmm_ac_handle_addts_resp(struct wpa_supplicant *wpa_s, const u8 *sa,
660
    const u8 resp_dialog_token, const u8 status_code,
661
    const struct wmm_tspec_element *tspec)
662
0
{
663
0
  struct wmm_ac_addts_request *req = wpa_s->addts_request;
664
0
  u8 ac, tsid, up, dir;
665
0
  int replace_tspecs;
666
667
0
  tsid = wmm_ac_get_tsid(tspec);
668
0
  dir = wmm_ac_get_direction(tspec);
669
0
  up = wmm_ac_get_user_priority(tspec);
670
0
  ac = up_to_ac[up];
671
672
  /* make sure we have a matching addts request */
673
0
  if (!req || req->dialog_token != resp_dialog_token) {
674
0
    wpa_printf(MSG_DEBUG,
675
0
         "WMM AC: no req with dialog=%u, ignoring frame",
676
0
         resp_dialog_token);
677
0
    return;
678
0
  }
679
680
  /* make sure the params are the same */
681
0
  if (!ether_addr_equal(req->address, sa) ||
682
0
      tsid != wmm_ac_get_tsid(&req->tspec) ||
683
0
      up != wmm_ac_get_user_priority(&req->tspec) ||
684
0
      dir != wmm_ac_get_direction(&req->tspec)) {
685
0
    wpa_printf(MSG_DEBUG,
686
0
         "WMM AC: ADDTS params do not match, ignoring frame");
687
0
    return;
688
0
  }
689
690
  /* delete pending request */
691
0
  wmm_ac_del_req(wpa_s, 0);
692
693
0
  wpa_printf(MSG_DEBUG,
694
0
       "ADDTS response status=%d tsid=%u up=%u direction=%u",
695
0
       status_code, tsid, up, dir);
696
697
0
  if (status_code != WMM_ADDTS_STATUS_ADMISSION_ACCEPTED) {
698
0
    wpa_printf(MSG_INFO, "WMM AC: ADDTS request was rejected");
699
0
    goto err_msg;
700
0
  }
701
702
0
  replace_tspecs = wmm_ac_should_replace_ts(wpa_s, tsid, ac, dir);
703
0
  if (replace_tspecs < 0)
704
0
    goto err_delts;
705
706
0
  wpa_printf(MSG_DEBUG, "ts idx replace bitmap: 0x%x", replace_tspecs);
707
708
  /* when replacing tspecs - delete first */
709
0
  wmm_ac_del_ts(wpa_s, ac, replace_tspecs);
710
711
  /* Creating a new traffic stream */
712
0
  wpa_printf(MSG_DEBUG,
713
0
       "WMM AC: adding a new TS with TSID=%u address="MACSTR
714
0
       " medium time=%u access category=%d dir=%d ",
715
0
       tsid, MAC2STR(sa),
716
0
       le_to_host16(tspec->medium_time), ac, dir);
717
718
0
  if (wmm_ac_add_ts(wpa_s, sa, tspec))
719
0
    goto err_delts;
720
721
0
  return;
722
723
0
err_delts:
724
  /* ask the ap to delete the tspec */
725
0
  wmm_ac_send_delts(wpa_s, tspec, sa);
726
0
err_msg:
727
0
  wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REQ_FAILED "tsid=%u",
728
0
    tsid);
729
0
}
730
731
732
void wmm_ac_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
733
      const u8 *sa, const u8 *data, size_t len)
734
0
{
735
0
  u8 action;
736
0
  u8 dialog_token;
737
0
  u8 status_code;
738
0
  struct ieee802_11_elems elems;
739
0
  struct wmm_tspec_element *tspec;
740
741
0
  if (wpa_s->wmm_ac_assoc_info == NULL) {
742
0
    wpa_printf(MSG_DEBUG,
743
0
         "WMM AC: WMM AC is disabled, ignoring action frame");
744
0
    return;
745
0
  }
746
747
0
  action = data[0];
748
749
0
  if (action != WMM_ACTION_CODE_ADDTS_RESP &&
750
0
      action != WMM_ACTION_CODE_DELTS) {
751
0
    wpa_printf(MSG_DEBUG,
752
0
         "WMM AC: Unknown action (%d), ignoring action frame",
753
0
         action);
754
0
    return;
755
0
  }
756
757
  /* WMM AC action frame */
758
0
  if (!ether_addr_equal(da, wpa_s->own_addr)) {
759
0
    wpa_printf(MSG_DEBUG, "WMM AC: frame destination addr="MACSTR
760
0
         " is other than ours, ignoring frame", MAC2STR(da));
761
0
    return;
762
0
  }
763
764
0
  if (!ether_addr_equal(sa, wpa_s->bssid)) {
765
0
    wpa_printf(MSG_DEBUG, "WMM AC: ignore frame with sa " MACSTR
766
0
         " different other than our bssid", MAC2STR(da));
767
0
    return;
768
0
  }
769
770
0
  if (len < 2 + sizeof(struct wmm_tspec_element)) {
771
0
    wpa_printf(MSG_DEBUG,
772
0
         "WMM AC: Short ADDTS response ignored (len=%lu)",
773
0
         (unsigned long) len);
774
0
    return;
775
0
  }
776
777
0
  data++;
778
0
  len--;
779
0
  dialog_token = data[0];
780
0
  status_code = data[1];
781
782
0
  if (ieee802_11_parse_elems(data + 2, len - 2, &elems, 1) != ParseOK) {
783
0
    wpa_printf(MSG_DEBUG,
784
0
         "WMM AC: Could not parse WMM AC action from " MACSTR,
785
0
         MAC2STR(sa));
786
0
    return;
787
0
  }
788
789
  /* the struct also contains the type and value, so decrease it */
790
0
  if (elems.wmm_tspec_len != sizeof(struct wmm_tspec_element) - 2) {
791
0
    wpa_printf(MSG_DEBUG, "WMM AC: missing or wrong length TSPEC");
792
0
    return;
793
0
  }
794
795
0
  tspec = (struct wmm_tspec_element *)(elems.wmm_tspec - 2);
796
797
0
  wpa_printf(MSG_DEBUG, "WMM AC: RX WMM AC Action from " MACSTR,
798
0
       MAC2STR(sa));
799
0
  wpa_hexdump(MSG_MSGDUMP, "WMM AC: WMM AC Action content", data, len);
800
801
0
  switch (action) {
802
0
  case WMM_ACTION_CODE_ADDTS_RESP:
803
0
    wmm_ac_handle_addts_resp(wpa_s, sa, dialog_token, status_code,
804
0
           tspec);
805
0
    break;
806
0
  case WMM_ACTION_CODE_DELTS:
807
0
    wmm_ac_handle_delts(wpa_s, sa, tspec);
808
0
    break;
809
0
  default:
810
0
    break;
811
0
  }
812
0
}
813
814
815
static const char * get_ac_str(u8 ac)
816
0
{
817
0
  switch (ac) {
818
0
  case WMM_AC_BE:
819
0
    return "BE";
820
0
  case WMM_AC_BK:
821
0
    return "BK";
822
0
  case WMM_AC_VI:
823
0
    return "VI";
824
0
  case WMM_AC_VO:
825
0
    return "VO";
826
0
  default:
827
0
    return "N/A";
828
0
  }
829
0
}
830
831
832
static const char * get_direction_str(u8 direction)
833
0
{
834
0
  switch (direction) {
835
0
  case WMM_AC_DIR_DOWNLINK:
836
0
    return "Downlink";
837
0
  case WMM_AC_DIR_UPLINK:
838
0
    return "Uplink";
839
0
  case WMM_AC_DIR_BIDIRECTIONAL:
840
0
    return "Bi-directional";
841
0
  default:
842
0
    return "N/A";
843
0
  }
844
0
}
845
846
847
int wpas_wmm_ac_status(struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
848
0
{
849
0
  struct wmm_ac_assoc_data *assoc_info = wpa_s->wmm_ac_assoc_info;
850
0
  enum ts_dir_idx idx;
851
0
  int pos = 0;
852
0
  u8 ac, up;
853
854
0
  if (!assoc_info) {
855
0
    return wpa_scnprintf(buf, buflen - pos,
856
0
             "Not associated to a WMM AP, WMM AC is Disabled\n");
857
0
  }
858
859
0
  pos += wpa_scnprintf(buf + pos, buflen - pos, "WMM AC is Enabled\n");
860
861
0
  for (ac = 0; ac < WMM_AC_NUM; ac++) {
862
0
    int ts_count = 0;
863
864
0
    pos += wpa_scnprintf(buf + pos, buflen - pos,
865
0
             "%s: acm=%d uapsd=%d\n",
866
0
             get_ac_str(ac),
867
0
             assoc_info->ac_params[ac].acm,
868
0
             assoc_info->ac_params[ac].uapsd);
869
870
0
    for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
871
0
      struct wmm_tspec_element *tspec;
872
0
      u8 dir, tsid;
873
0
      const char *dir_str;
874
875
0
      tspec = wpa_s->tspecs[ac][idx];
876
0
      if (!tspec)
877
0
        continue;
878
879
0
      ts_count++;
880
881
0
      dir = wmm_ac_get_direction(tspec);
882
0
      dir_str = get_direction_str(dir);
883
0
      tsid = wmm_ac_get_tsid(tspec);
884
0
      up = wmm_ac_get_user_priority(tspec);
885
886
0
      pos += wpa_scnprintf(buf + pos, buflen - pos,
887
0
               "\tTSID=%u UP=%u\n"
888
0
               "\tAddress = "MACSTR"\n"
889
0
               "\tWMM AC dir = %s\n"
890
0
               "\tTotal admitted time = %u\n\n",
891
0
               tsid, up,
892
0
               MAC2STR(wpa_s->bssid),
893
0
               dir_str,
894
0
               le_to_host16(tspec->medium_time));
895
0
    }
896
897
0
    if (!ts_count) {
898
0
      pos += wpa_scnprintf(buf + pos, buflen - pos,
899
0
               "\t(No Traffic Stream)\n\n");
900
0
    }
901
0
  }
902
903
0
  return pos;
904
0
}
905
906
907
static u8 wmm_ac_get_tspecs_count(struct wpa_supplicant *wpa_s)
908
0
{
909
0
  int ac, dir, tspecs_count = 0;
910
911
0
  for (ac = 0; ac < WMM_AC_NUM; ac++) {
912
0
    for (dir = 0; dir < TS_DIR_IDX_COUNT; dir++) {
913
0
      if (wpa_s->tspecs[ac][dir])
914
0
        tspecs_count++;
915
0
    }
916
0
  }
917
918
0
  return tspecs_count;
919
0
}
920
921
922
void wmm_ac_save_tspecs(struct wpa_supplicant *wpa_s)
923
0
{
924
0
  int ac, dir, tspecs_count;
925
926
0
  wpa_printf(MSG_DEBUG, "WMM AC: Save last configured tspecs");
927
928
0
  if (!wpa_s->wmm_ac_assoc_info)
929
0
    return;
930
931
0
  tspecs_count = wmm_ac_get_tspecs_count(wpa_s);
932
0
  if (!tspecs_count) {
933
0
    wpa_printf(MSG_DEBUG, "WMM AC: No configured TSPECs");
934
0
    return;
935
0
  }
936
937
0
  wpa_printf(MSG_DEBUG, "WMM AC: Saving tspecs");
938
939
0
  wmm_ac_clear_saved_tspecs(wpa_s);
940
0
  wpa_s->last_tspecs = os_calloc(tspecs_count,
941
0
               sizeof(*wpa_s->last_tspecs));
942
0
  if (!wpa_s->last_tspecs) {
943
0
    wpa_printf(MSG_ERROR, "WMM AC: Failed to save tspecs!");
944
0
    return;
945
0
  }
946
947
0
  for (ac = 0; ac < WMM_AC_NUM; ac++) {
948
0
    for (dir = 0; dir < TS_DIR_IDX_COUNT; dir++) {
949
0
      if (!wpa_s->tspecs[ac][dir])
950
0
        continue;
951
952
0
      wpa_s->last_tspecs[wpa_s->last_tspecs_count++] =
953
0
        *wpa_s->tspecs[ac][dir];
954
0
    }
955
0
  }
956
957
0
  wpa_printf(MSG_DEBUG, "WMM AC: Successfully saved %d TSPECs",
958
0
       wpa_s->last_tspecs_count);
959
0
}
960
961
962
void wmm_ac_clear_saved_tspecs(struct wpa_supplicant *wpa_s)
963
0
{
964
0
  if (wpa_s->last_tspecs) {
965
0
    wpa_printf(MSG_DEBUG, "WMM AC: Clear saved tspecs");
966
0
    os_free(wpa_s->last_tspecs);
967
0
    wpa_s->last_tspecs = NULL;
968
0
    wpa_s->last_tspecs_count = 0;
969
0
  }
970
0
}
971
972
973
int wmm_ac_restore_tspecs(struct wpa_supplicant *wpa_s)
974
0
{
975
0
  unsigned int i;
976
977
0
  if (!wpa_s->wmm_ac_assoc_info || !wpa_s->last_tspecs_count)
978
0
    return 0;
979
980
0
  wpa_printf(MSG_DEBUG, "WMM AC: Restore %u saved tspecs",
981
0
       wpa_s->last_tspecs_count);
982
983
0
  for (i = 0; i < wpa_s->last_tspecs_count; i++)
984
0
    wmm_ac_add_ts(wpa_s, wpa_s->bssid, &wpa_s->last_tspecs[i]);
985
986
0
  return 0;
987
0
}