[PATCH] mac80211: consolidate encryption
[linux-2.6.git] / net / mac80211 / wme.c
blobfcc8921722f428e4151c27a7d62c241f53390688
1 /*
2 * Copyright 2004, Instant802 Networks, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
11 #include <linux/module.h>
12 #include <linux/if_arp.h>
13 #include <linux/types.h>
14 #include <net/ip.h>
15 #include <net/pkt_sched.h>
17 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
19 #include "wme.h"
21 /* maximum number of hardware queues we support. */
22 #define TC_80211_MAX_QUEUES 8
24 struct ieee80211_sched_data
26 struct tcf_proto *filter_list;
27 struct Qdisc *queues[TC_80211_MAX_QUEUES];
28 struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
32 /* given a data frame determine the 802.1p/1d tag to use */
33 static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
35 struct iphdr *ip;
36 int dscp;
37 int offset;
39 struct ieee80211_sched_data *q = qdisc_priv(qd);
40 struct tcf_result res = { -1, 0 };
42 /* if there is a user set filter list, call out to that */
43 if (q->filter_list) {
44 tc_classify(skb, q->filter_list, &res);
45 if (res.class != -1)
46 return res.class;
49 /* skb->priority values from 256->263 are magic values to
50 * directly indicate a specific 802.1d priority.
51 * This is used to allow 802.1d priority to be passed directly in
52 * from VLAN tags, etc. */
53 if (skb->priority >= 256 && skb->priority <= 263)
54 return skb->priority - 256;
56 /* check there is a valid IP header present */
57 offset = ieee80211_get_hdrlen_from_skb(skb) + 8 /* LLC + proto */;
58 if (skb->protocol != __constant_htons(ETH_P_IP) ||
59 skb->len < offset + sizeof(*ip))
60 return 0;
62 ip = (struct iphdr *) (skb->data + offset);
64 dscp = ip->tos & 0xfc;
65 if (dscp & 0x1c)
66 return 0;
67 return dscp >> 5;
71 static inline int wme_downgrade_ac(struct sk_buff *skb)
73 switch (skb->priority) {
74 case 6:
75 case 7:
76 skb->priority = 5; /* VO -> VI */
77 return 0;
78 case 4:
79 case 5:
80 skb->priority = 3; /* VI -> BE */
81 return 0;
82 case 0:
83 case 3:
84 skb->priority = 2; /* BE -> BK */
85 return 0;
86 default:
87 return -1;
92 /* positive return value indicates which queue to use
93 * negative return value indicates to drop the frame */
94 static inline int classify80211(struct sk_buff *skb, struct Qdisc *qd)
96 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
97 struct ieee80211_tx_packet_data *pkt_data =
98 (struct ieee80211_tx_packet_data *) skb->cb;
99 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
100 unsigned short fc = le16_to_cpu(hdr->frame_control);
101 int qos;
102 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
104 /* see if frame is data or non data frame */
105 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
106 /* management frames go on AC_VO queue, but are sent
107 * without QoS control fields */
108 return IEEE80211_TX_QUEUE_DATA0;
111 if (unlikely(pkt_data->flags & IEEE80211_TXPD_MGMT_IFACE)) {
112 /* Data frames from hostapd (mainly, EAPOL) use AC_VO
113 * and they will include QoS control fields if
114 * the target STA is using WME. */
115 skb->priority = 7;
116 return ieee802_1d_to_ac[skb->priority];
119 /* is this a QoS frame? */
120 qos = fc & IEEE80211_STYPE_QOS_DATA;
122 if (!qos) {
123 skb->priority = 0; /* required for correct WPA/11i MIC */
124 return ieee802_1d_to_ac[skb->priority];
127 /* use the data classifier to determine what 802.1d tag the
128 * data frame has */
129 skb->priority = classify_1d(skb, qd);
131 /* in case we are a client verify acm is not set for this ac */
132 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
133 if (wme_downgrade_ac(skb)) {
134 /* No AC with lower priority has acm=0, drop packet. */
135 return -1;
139 /* look up which queue to use for frames with this 1d tag */
140 return ieee802_1d_to_ac[skb->priority];
144 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
146 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
147 struct ieee80211_sched_data *q = qdisc_priv(qd);
148 struct ieee80211_tx_packet_data *pkt_data =
149 (struct ieee80211_tx_packet_data *) skb->cb;
150 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
151 unsigned short fc = le16_to_cpu(hdr->frame_control);
152 struct Qdisc *qdisc;
153 int err, queue;
155 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
156 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
157 qd->q.qlen++;
158 return 0;
161 queue = classify80211(skb, qd);
163 /* now we know the 1d priority, fill in the QoS header if there is one
165 if (WLAN_FC_IS_QOS_DATA(fc)) {
166 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
167 u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
168 if (local->wifi_wme_noack_test)
169 qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
170 QOS_CONTROL_ACK_POLICY_SHIFT;
171 /* qos header is 2 bytes, second reserved */
172 *p = qos_hdr;
173 p++;
174 *p = 0;
177 if (unlikely(queue >= local->hw.queues)) {
178 #if 0
179 if (net_ratelimit()) {
180 printk(KERN_DEBUG "%s - queue=%d (hw does not "
181 "support) -> %d\n",
182 __func__, queue, local->hw.queues - 1);
184 #endif
185 queue = local->hw.queues - 1;
188 if (unlikely(queue < 0)) {
189 kfree_skb(skb);
190 err = NET_XMIT_DROP;
191 } else {
192 pkt_data->queue = (unsigned int) queue;
193 qdisc = q->queues[queue];
194 err = qdisc->enqueue(skb, qdisc);
195 if (err == NET_XMIT_SUCCESS) {
196 qd->q.qlen++;
197 qd->bstats.bytes += skb->len;
198 qd->bstats.packets++;
199 return NET_XMIT_SUCCESS;
202 qd->qstats.drops++;
203 return err;
207 /* TODO: clean up the cases where master_hard_start_xmit
208 * returns non 0 - it shouldn't ever do that. Once done we
209 * can remove this function */
210 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
212 struct ieee80211_sched_data *q = qdisc_priv(qd);
213 struct ieee80211_tx_packet_data *pkt_data =
214 (struct ieee80211_tx_packet_data *) skb->cb;
215 struct Qdisc *qdisc;
216 int err;
218 /* we recorded which queue to use earlier! */
219 qdisc = q->queues[pkt_data->queue];
221 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
222 qd->q.qlen++;
223 return 0;
225 qd->qstats.drops++;
226 return err;
230 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
232 struct ieee80211_sched_data *q = qdisc_priv(qd);
233 struct net_device *dev = qd->dev;
234 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
235 struct ieee80211_hw *hw = &local->hw;
236 struct sk_buff *skb;
237 struct Qdisc *qdisc;
238 int queue;
240 /* check all the h/w queues in numeric/priority order */
241 for (queue = 0; queue < hw->queues; queue++) {
242 /* see if there is room in this hardware queue */
243 if (test_bit(IEEE80211_LINK_STATE_XOFF,
244 &local->state[queue]) ||
245 test_bit(IEEE80211_LINK_STATE_PENDING,
246 &local->state[queue]))
247 continue;
249 /* there is space - try and get a frame */
250 skb = skb_dequeue(&q->requeued[queue]);
251 if (skb) {
252 qd->q.qlen--;
253 return skb;
256 qdisc = q->queues[queue];
257 skb = qdisc->dequeue(qdisc);
258 if (skb) {
259 qd->q.qlen--;
260 return skb;
263 /* returning a NULL here when all the h/w queues are full means we
264 * never need to call netif_stop_queue in the driver */
265 return NULL;
269 static void wme_qdiscop_reset(struct Qdisc* qd)
271 struct ieee80211_sched_data *q = qdisc_priv(qd);
272 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
273 struct ieee80211_hw *hw = &local->hw;
274 int queue;
276 /* QUESTION: should we have some hardware flush functionality here? */
278 for (queue = 0; queue < hw->queues; queue++) {
279 skb_queue_purge(&q->requeued[queue]);
280 qdisc_reset(q->queues[queue]);
282 qd->q.qlen = 0;
286 static void wme_qdiscop_destroy(struct Qdisc* qd)
288 struct ieee80211_sched_data *q = qdisc_priv(qd);
289 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
290 struct ieee80211_hw *hw = &local->hw;
291 int queue;
293 tcf_destroy_chain(q->filter_list);
294 q->filter_list = NULL;
296 for (queue=0; queue < hw->queues; queue++) {
297 skb_queue_purge(&q->requeued[queue]);
298 qdisc_destroy(q->queues[queue]);
299 q->queues[queue] = &noop_qdisc;
304 /* called whenever parameters are updated on existing qdisc */
305 static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
307 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
309 /* check our options block is the right size */
310 /* copy any options to our local structure */
311 /* Ignore options block for now - always use static mapping
312 struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
314 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
315 return -EINVAL;
316 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
318 return 0;
322 /* called during initial creation of qdisc on device */
323 static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
325 struct ieee80211_sched_data *q = qdisc_priv(qd);
326 struct net_device *dev = qd->dev;
327 struct ieee80211_local *local;
328 int queues;
329 int err = 0, i;
331 /* check that device is a mac80211 device */
332 if (!dev->ieee80211_ptr ||
333 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
334 return -EINVAL;
336 /* check this device is an ieee80211 master type device */
337 if (dev->type != ARPHRD_IEEE80211)
338 return -EINVAL;
340 /* check that there is no qdisc currently attached to device
341 * this ensures that we will be the root qdisc. (I can't find a better
342 * way to test this explicitly) */
343 if (dev->qdisc_sleeping != &noop_qdisc)
344 return -EINVAL;
346 if (qd->flags & TCQ_F_INGRESS)
347 return -EINVAL;
349 local = wdev_priv(dev->ieee80211_ptr);
350 queues = local->hw.queues;
352 /* if options were passed in, set them */
353 if (opt) {
354 err = wme_qdiscop_tune(qd, opt);
357 /* create child queues */
358 for (i = 0; i < queues; i++) {
359 skb_queue_head_init(&q->requeued[i]);
360 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
361 qd->handle);
362 if (!q->queues[i]) {
363 q->queues[i] = &noop_qdisc;
364 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
368 return err;
371 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
373 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
374 unsigned char *p = skb->tail;
375 struct tc_ieee80211_qopt opt;
377 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
378 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
379 */ return skb->len;
381 rtattr_failure:
382 skb_trim(skb, p - skb->data);*/
383 return -1;
387 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
388 struct Qdisc *new, struct Qdisc **old)
390 struct ieee80211_sched_data *q = qdisc_priv(qd);
391 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
392 struct ieee80211_hw *hw = &local->hw;
393 unsigned long queue = arg - 1;
395 if (queue >= hw->queues)
396 return -EINVAL;
398 if (!new)
399 new = &noop_qdisc;
401 sch_tree_lock(qd);
402 *old = q->queues[queue];
403 q->queues[queue] = new;
404 qdisc_reset(*old);
405 sch_tree_unlock(qd);
407 return 0;
411 static struct Qdisc *
412 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
414 struct ieee80211_sched_data *q = qdisc_priv(qd);
415 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
416 struct ieee80211_hw *hw = &local->hw;
417 unsigned long queue = arg - 1;
419 if (queue >= hw->queues)
420 return NULL;
422 return q->queues[queue];
426 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
428 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
429 struct ieee80211_hw *hw = &local->hw;
430 unsigned long queue = TC_H_MIN(classid);
432 if (queue - 1 >= hw->queues)
433 return 0;
435 return queue;
439 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
440 u32 classid)
442 return wme_classop_get(qd, classid);
446 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
451 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
452 struct rtattr **tca, unsigned long *arg)
454 unsigned long cl = *arg;
455 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
456 struct ieee80211_hw *hw = &local->hw;
458 if (cl - 1 > hw->queues)
459 return -ENOENT;
461 /* TODO: put code to program hardware queue parameters here,
462 * to allow programming from tc command line */
464 return 0;
468 /* we don't support deleting hardware queues
469 * when we add WMM-SA support - TSPECs may be deleted here */
470 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
472 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
473 struct ieee80211_hw *hw = &local->hw;
475 if (cl - 1 > hw->queues)
476 return -ENOENT;
477 return 0;
481 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
482 struct sk_buff *skb, struct tcmsg *tcm)
484 struct ieee80211_sched_data *q = qdisc_priv(qd);
485 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
486 struct ieee80211_hw *hw = &local->hw;
488 if (cl - 1 > hw->queues)
489 return -ENOENT;
490 tcm->tcm_handle = TC_H_MIN(cl);
491 tcm->tcm_parent = qd->handle;
492 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
493 return 0;
497 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
499 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
500 struct ieee80211_hw *hw = &local->hw;
501 int queue;
503 if (arg->stop)
504 return;
506 for (queue = 0; queue < hw->queues; queue++) {
507 if (arg->count < arg->skip) {
508 arg->count++;
509 continue;
511 /* we should return classids for our internal queues here
512 * as well as the external ones */
513 if (arg->fn(qd, queue+1, arg) < 0) {
514 arg->stop = 1;
515 break;
517 arg->count++;
522 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
523 unsigned long cl)
525 struct ieee80211_sched_data *q = qdisc_priv(qd);
527 if (cl)
528 return NULL;
530 return &q->filter_list;
534 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
535 * - these are the operations on the classes */
536 static struct Qdisc_class_ops class_ops =
538 .graft = wme_classop_graft,
539 .leaf = wme_classop_leaf,
541 .get = wme_classop_get,
542 .put = wme_classop_put,
543 .change = wme_classop_change,
544 .delete = wme_classop_delete,
545 .walk = wme_classop_walk,
547 .tcf_chain = wme_classop_find_tcf,
548 .bind_tcf = wme_classop_bind,
549 .unbind_tcf = wme_classop_put,
551 .dump = wme_classop_dump_class,
555 /* queueing discipline operations */
556 static struct Qdisc_ops wme_qdisc_ops =
558 .next = NULL,
559 .cl_ops = &class_ops,
560 .id = "ieee80211",
561 .priv_size = sizeof(struct ieee80211_sched_data),
563 .enqueue = wme_qdiscop_enqueue,
564 .dequeue = wme_qdiscop_dequeue,
565 .requeue = wme_qdiscop_requeue,
566 .drop = NULL, /* drop not needed since we are always the root qdisc */
568 .init = wme_qdiscop_init,
569 .reset = wme_qdiscop_reset,
570 .destroy = wme_qdiscop_destroy,
571 .change = wme_qdiscop_tune,
573 .dump = wme_qdiscop_dump,
577 void ieee80211_install_qdisc(struct net_device *dev)
579 struct Qdisc *qdisc;
581 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
582 if (!qdisc) {
583 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
584 return;
587 /* same handle as would be allocated by qdisc_alloc_handle() */
588 qdisc->handle = 0x80010000;
590 qdisc_lock_tree(dev);
591 list_add_tail(&qdisc->list, &dev->qdisc_list);
592 dev->qdisc_sleeping = qdisc;
593 qdisc_unlock_tree(dev);
597 int ieee80211_qdisc_installed(struct net_device *dev)
599 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
603 int ieee80211_wme_register(void)
605 return register_qdisc(&wme_qdisc_ops);
609 void ieee80211_wme_unregister(void)
611 unregister_qdisc(&wme_qdisc_ops);