ieee80211_output.c revision 1.1.1.6 1 /*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_output.c,v 1.26 2005/07/06 01:55:17 sam Exp $");
35
36 #include "opt_inet.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/kernel.h>
42 #include <sys/endian.h>
43
44 #include <sys/socket.h>
45
46 #include <net/bpf.h>
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_vlan_var.h>
52
53 #include <net80211/ieee80211_var.h>
54
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #endif
61
62 #ifdef IEEE80211_DEBUG
63 /*
64 * Decide if an outbound management frame should be
65 * printed when debugging is enabled. This filters some
66 * of the less interesting frames that come frequently
67 * (e.g. beacons).
68 */
69 static __inline int
70 doprint(struct ieee80211com *ic, int subtype)
71 {
72 switch (subtype) {
73 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
74 return (ic->ic_opmode == IEEE80211_M_IBSS);
75 }
76 return 1;
77 }
78 #endif
79
80 /*
81 * Send a management frame to the specified node. The node pointer
82 * must have a reference as the pointer will be passed to the driver
83 * and potentially held for a long time. If the frame is successfully
84 * dispatched to the driver, then it is responsible for freeing the
85 * reference (and potentially free'ing up any associated storage).
86 */
87 static int
88 ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni,
89 struct mbuf *m, int type)
90 {
91 struct ifnet *ifp = ic->ic_ifp;
92 struct ieee80211_frame *wh;
93
94 KASSERT(ni != NULL, ("null node"));
95
96 /*
97 * Yech, hack alert! We want to pass the node down to the
98 * driver's start routine. If we don't do so then the start
99 * routine must immediately look it up again and that can
100 * cause a lock order reversal if, for example, this frame
101 * is being sent because the station is being timedout and
102 * the frame being sent is a DEAUTH message. We could stick
103 * this in an m_tag and tack that on to the mbuf. However
104 * that's rather expensive to do for every frame so instead
105 * we stuff it in the rcvif field since outbound frames do
106 * not (presently) use this.
107 */
108 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
109 if (m == NULL)
110 return ENOMEM;
111 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
112 m->m_pkthdr.rcvif = (void *)ni;
113
114 wh = mtod(m, struct ieee80211_frame *);
115 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
116 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
117 *(u_int16_t *)wh->i_dur = 0;
118 *(u_int16_t *)wh->i_seq =
119 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
120 ni->ni_txseqs[0]++;
121 /*
122 * Hack. When sending PROBE_REQ frames while scanning we
123 * explicitly force a broadcast rather than (as before) clobber
124 * ni_macaddr and ni_bssid. This is stopgap, we need a way
125 * to communicate this directly rather than do something
126 * implicit based on surrounding state.
127 */
128 if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ &&
129 (ic->ic_flags & IEEE80211_F_SCAN)) {
130 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
131 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
132 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
133 } else {
134 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
135 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
136 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
137 }
138
139 if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
140 m->m_flags &= ~M_LINK0;
141 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
142 "[%s] encrypting frame (%s)\n",
143 ether_sprintf(wh->i_addr1), __func__);
144 wh->i_fc[1] |= IEEE80211_FC1_WEP;
145 }
146 #ifdef IEEE80211_DEBUG
147 /* avoid printing too many frames */
148 if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
149 ieee80211_msg_dumppkts(ic)) {
150 printf("[%s] send %s on channel %u\n",
151 ether_sprintf(wh->i_addr1),
152 ieee80211_mgt_subtype_name[
153 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
154 IEEE80211_FC0_SUBTYPE_SHIFT],
155 ieee80211_chan2ieee(ic, ni->ni_chan));
156 }
157 #endif
158 IEEE80211_NODE_STAT(ni, tx_mgmt);
159 IF_ENQUEUE(&ic->ic_mgtq, m);
160 ifp->if_timer = 1;
161 if_start(ifp);
162 return 0;
163 }
164
165 /*
166 * Send a null data frame to the specified node.
167 */
168 int
169 ieee80211_send_nulldata(struct ieee80211com *ic, struct ieee80211_node *ni)
170 {
171 struct ifnet *ifp = ic->ic_ifp;
172 struct mbuf *m;
173 struct ieee80211_frame *wh;
174
175 MGETHDR(m, M_NOWAIT, MT_HEADER);
176 if (m == NULL) {
177 /* XXX debug msg */
178 ic->ic_stats.is_tx_nobuf++;
179 return ENOMEM;
180 }
181 m->m_pkthdr.rcvif = (void *) ieee80211_ref_node(ni);
182
183 wh = mtod(m, struct ieee80211_frame *);
184 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA |
185 IEEE80211_FC0_SUBTYPE_NODATA;
186 *(u_int16_t *)wh->i_dur = 0;
187 *(u_int16_t *)wh->i_seq =
188 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
189 ni->ni_txseqs[0]++;
190
191 /* XXX WDS */
192 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
193 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
194 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
195 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_myaddr);
196 m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
197
198 IEEE80211_NODE_STAT(ni, tx_data);
199
200 IF_ENQUEUE(&ic->ic_mgtq, m); /* cheat */
201 if_start(ifp);
202
203 return 0;
204 }
205
206 /*
207 * Assign priority to a frame based on any vlan tag assigned
208 * to the station and/or any Diffserv setting in an IP header.
209 * Finally, if an ACM policy is setup (in station mode) it's
210 * applied.
211 */
212 int
213 ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
214 {
215 int v_wme_ac, d_wme_ac, ac;
216 #ifdef INET
217 struct ether_header *eh;
218 #endif
219
220 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
221 ac = WME_AC_BE;
222 goto done;
223 }
224
225 /*
226 * If node has a vlan tag then all traffic
227 * to it must have a matching tag.
228 */
229 v_wme_ac = 0;
230 if (ni->ni_vlan != 0) {
231 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m);
232 if (mtag == NULL) {
233 IEEE80211_NODE_STAT(ni, tx_novlantag);
234 return 1;
235 }
236 if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) !=
237 EVL_VLANOFTAG(ni->ni_vlan)) {
238 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
239 return 1;
240 }
241 /* map vlan priority to AC */
242 switch (EVL_PRIOFTAG(ni->ni_vlan)) {
243 case 1:
244 case 2:
245 v_wme_ac = WME_AC_BK;
246 break;
247 case 0:
248 case 3:
249 v_wme_ac = WME_AC_BE;
250 break;
251 case 4:
252 case 5:
253 v_wme_ac = WME_AC_VI;
254 break;
255 case 6:
256 case 7:
257 v_wme_ac = WME_AC_VO;
258 break;
259 }
260 }
261
262 #ifdef INET
263 eh = mtod(m, struct ether_header *);
264 if (eh->ether_type == htons(ETHERTYPE_IP)) {
265 const struct ip *ip = (struct ip *)
266 (mtod(m, u_int8_t *) + sizeof (*eh));
267 /*
268 * IP frame, map the TOS field.
269 */
270 switch (ip->ip_tos) {
271 case 0x08:
272 case 0x20:
273 d_wme_ac = WME_AC_BK; /* background */
274 break;
275 case 0x28:
276 case 0xa0:
277 d_wme_ac = WME_AC_VI; /* video */
278 break;
279 case 0x30: /* voice */
280 case 0xe0:
281 case 0x88: /* XXX UPSD */
282 case 0xb8:
283 d_wme_ac = WME_AC_VO;
284 break;
285 default:
286 d_wme_ac = WME_AC_BE;
287 break;
288 }
289 } else {
290 #endif /* INET */
291 d_wme_ac = WME_AC_BE;
292 #ifdef INET
293 }
294 #endif
295 /*
296 * Use highest priority AC.
297 */
298 if (v_wme_ac > d_wme_ac)
299 ac = v_wme_ac;
300 else
301 ac = d_wme_ac;
302
303 /*
304 * Apply ACM policy.
305 */
306 if (ic->ic_opmode == IEEE80211_M_STA) {
307 static const int acmap[4] = {
308 WME_AC_BK, /* WME_AC_BE */
309 WME_AC_BK, /* WME_AC_BK */
310 WME_AC_BE, /* WME_AC_VI */
311 WME_AC_VI, /* WME_AC_VO */
312 };
313 while (ac != WME_AC_BK &&
314 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
315 ac = acmap[ac];
316 }
317 done:
318 M_WME_SETAC(m, ac);
319 return 0;
320 }
321
322 /*
323 * Insure there is sufficient contiguous space to encapsulate the
324 * 802.11 data frame. If room isn't already there, arrange for it.
325 * Drivers and cipher modules assume we have done the necessary work
326 * and fail rudely if they don't find the space they need.
327 */
328 static struct mbuf *
329 ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
330 struct ieee80211_key *key, struct mbuf *m)
331 {
332 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
333 int needed_space = hdrsize;
334
335 if (key != NULL) {
336 /* XXX belongs in crypto code? */
337 needed_space += key->wk_cipher->ic_header;
338 /* XXX frags */
339 }
340 /*
341 * We know we are called just before stripping an Ethernet
342 * header and prepending an LLC header. This means we know
343 * there will be
344 * sizeof(struct ether_header) - sizeof(struct llc)
345 * bytes recovered to which we need additional space for the
346 * 802.11 header and any crypto header.
347 */
348 /* XXX check trailing space and copy instead? */
349 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
350 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
351 if (n == NULL) {
352 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
353 "%s: cannot expand storage\n", __func__);
354 ic->ic_stats.is_tx_nobuf++;
355 m_freem(m);
356 return NULL;
357 }
358 KASSERT(needed_space <= MHLEN,
359 ("not enough room, need %u got %zu\n", needed_space, MHLEN));
360 /*
361 * Setup new mbuf to have leading space to prepend the
362 * 802.11 header and any crypto header bits that are
363 * required (the latter are added when the driver calls
364 * back to ieee80211_crypto_encap to do crypto encapsulation).
365 */
366 /* NB: must be first 'cuz it clobbers m_data */
367 m_move_pkthdr(n, m);
368 n->m_len = 0; /* NB: m_gethdr does not set */
369 n->m_data += needed_space;
370 /*
371 * Pull up Ethernet header to create the expected layout.
372 * We could use m_pullup but that's overkill (i.e. we don't
373 * need the actual data) and it cannot fail so do it inline
374 * for speed.
375 */
376 /* NB: struct ether_header is known to be contiguous */
377 n->m_len += sizeof(struct ether_header);
378 m->m_len -= sizeof(struct ether_header);
379 m->m_data += sizeof(struct ether_header);
380 /*
381 * Replace the head of the chain.
382 */
383 n->m_next = m;
384 m = n;
385 }
386 return m;
387 #undef TO_BE_RECLAIMED
388 }
389
390 #define KEY_UNDEFINED(k) ((k).wk_cipher == &ieee80211_cipher_none)
391 /*
392 * Return the transmit key to use in sending a unicast frame.
393 * If a unicast key is set we use that. When no unicast key is set
394 * we fall back to the default transmit key.
395 */
396 static __inline struct ieee80211_key *
397 ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
398 {
399 if (KEY_UNDEFINED(ni->ni_ucastkey)) {
400 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
401 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
402 return NULL;
403 return &ic->ic_nw_keys[ic->ic_def_txkey];
404 } else {
405 return &ni->ni_ucastkey;
406 }
407 }
408
409 /*
410 * Return the transmit key to use in sending a multicast frame.
411 * Multicast traffic always uses the group key which is installed as
412 * the default tx key.
413 */
414 static __inline struct ieee80211_key *
415 ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
416 {
417 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
418 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
419 return NULL;
420 return &ic->ic_nw_keys[ic->ic_def_txkey];
421 }
422
423 /*
424 * Encapsulate an outbound data frame. The mbuf chain is updated.
425 * If an error is encountered NULL is returned. The caller is required
426 * to provide a node reference and pullup the ethernet header in the
427 * first mbuf.
428 */
429 struct mbuf *
430 ieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
431 struct ieee80211_node *ni)
432 {
433 struct ether_header eh;
434 struct ieee80211_frame *wh;
435 struct ieee80211_key *key;
436 struct llc *llc;
437 int hdrsize, datalen, addqos;
438
439 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
440 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
441
442 /*
443 * Insure space for additional headers. First identify
444 * transmit key to use in calculating any buffer adjustments
445 * required. This is also used below to do privacy
446 * encapsulation work. Then calculate the 802.11 header
447 * size and any padding required by the driver.
448 *
449 * Note key may be NULL if we fall back to the default
450 * transmit key and that is not set. In that case the
451 * buffer may not be expanded as needed by the cipher
452 * routines, but they will/should discard it.
453 */
454 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
455 if (ic->ic_opmode == IEEE80211_M_STA ||
456 !IEEE80211_IS_MULTICAST(eh.ether_dhost))
457 key = ieee80211_crypto_getucastkey(ic, ni);
458 else
459 key = ieee80211_crypto_getmcastkey(ic, ni);
460 if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
461 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
462 "[%s] no default transmit key (%s) deftxkey %u\n",
463 ether_sprintf(eh.ether_dhost), __func__,
464 ic->ic_def_txkey);
465 ic->ic_stats.is_tx_nodefkey++;
466 }
467 } else
468 key = NULL;
469 /* XXX 4-address format */
470 /*
471 * XXX Some ap's don't handle QoS-encapsulated EAPOL
472 * frames so suppress use. This may be an issue if other
473 * ap's require all data frames to be QoS-encapsulated
474 * once negotiated in which case we'll need to make this
475 * configurable.
476 */
477 addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
478 eh.ether_type != htons(ETHERTYPE_PAE);
479 if (addqos)
480 hdrsize = sizeof(struct ieee80211_qosframe);
481 else
482 hdrsize = sizeof(struct ieee80211_frame);
483 if (ic->ic_flags & IEEE80211_F_DATAPAD)
484 hdrsize = roundup(hdrsize, sizeof(u_int32_t));
485 m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
486 if (m == NULL) {
487 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
488 goto bad;
489 }
490
491 /* NB: this could be optimized because of ieee80211_mbuf_adjust */
492 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
493 llc = mtod(m, struct llc *);
494 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
495 llc->llc_control = LLC_UI;
496 llc->llc_snap.org_code[0] = 0;
497 llc->llc_snap.org_code[1] = 0;
498 llc->llc_snap.org_code[2] = 0;
499 llc->llc_snap.ether_type = eh.ether_type;
500 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
501
502 M_PREPEND(m, hdrsize, M_DONTWAIT);
503 if (m == NULL) {
504 ic->ic_stats.is_tx_nobuf++;
505 goto bad;
506 }
507 wh = mtod(m, struct ieee80211_frame *);
508 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
509 *(u_int16_t *)wh->i_dur = 0;
510 switch (ic->ic_opmode) {
511 case IEEE80211_M_STA:
512 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
513 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
514 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
515 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
516 break;
517 case IEEE80211_M_IBSS:
518 case IEEE80211_M_AHDEMO:
519 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
520 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
521 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
522 /*
523 * NB: always use the bssid from ic_bss as the
524 * neighbor's may be stale after an ibss merge
525 */
526 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
527 break;
528 case IEEE80211_M_HOSTAP:
529 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
530 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
531 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
532 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
533 break;
534 case IEEE80211_M_MONITOR:
535 goto bad;
536 }
537 if (m->m_flags & M_MORE_DATA)
538 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
539 if (addqos) {
540 struct ieee80211_qosframe *qwh =
541 (struct ieee80211_qosframe *) wh;
542 int ac, tid;
543
544 ac = M_WME_GETAC(m);
545 /* map from access class/queue to 11e header priorty value */
546 tid = WME_AC_TO_TID(ac);
547 qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
548 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
549 qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
550 qwh->i_qos[1] = 0;
551 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
552
553 *(u_int16_t *)wh->i_seq =
554 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
555 ni->ni_txseqs[tid]++;
556 } else {
557 *(u_int16_t *)wh->i_seq =
558 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
559 ni->ni_txseqs[0]++;
560 }
561 if (key != NULL) {
562 /*
563 * IEEE 802.1X: send EAPOL frames always in the clear.
564 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
565 */
566 if (eh.ether_type != htons(ETHERTYPE_PAE) ||
567 ((ic->ic_flags & IEEE80211_F_WPA) &&
568 (ic->ic_opmode == IEEE80211_M_STA ?
569 !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
570 wh->i_fc[1] |= IEEE80211_FC1_WEP;
571 /* XXX do fragmentation */
572 if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
573 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
574 "[%s] enmic failed, discard frame\n",
575 ether_sprintf(eh.ether_dhost));
576 ic->ic_stats.is_crypto_enmicfail++;
577 goto bad;
578 }
579 }
580 }
581
582 IEEE80211_NODE_STAT(ni, tx_data);
583 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
584
585 return m;
586 bad:
587 if (m != NULL)
588 m_freem(m);
589 return NULL;
590 }
591
592 /*
593 * Add a supported rates element id to a frame.
594 */
595 static u_int8_t *
596 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
597 {
598 int nrates;
599
600 *frm++ = IEEE80211_ELEMID_RATES;
601 nrates = rs->rs_nrates;
602 if (nrates > IEEE80211_RATE_SIZE)
603 nrates = IEEE80211_RATE_SIZE;
604 *frm++ = nrates;
605 memcpy(frm, rs->rs_rates, nrates);
606 return frm + nrates;
607 }
608
609 /*
610 * Add an extended supported rates element id to a frame.
611 */
612 static u_int8_t *
613 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
614 {
615 /*
616 * Add an extended supported rates element if operating in 11g mode.
617 */
618 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
619 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
620 *frm++ = IEEE80211_ELEMID_XRATES;
621 *frm++ = nrates;
622 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
623 frm += nrates;
624 }
625 return frm;
626 }
627
628 /*
629 * Add an ssid elemet to a frame.
630 */
631 static u_int8_t *
632 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
633 {
634 *frm++ = IEEE80211_ELEMID_SSID;
635 *frm++ = len;
636 memcpy(frm, ssid, len);
637 return frm + len;
638 }
639
640 /*
641 * Add an erp element to a frame.
642 */
643 static u_int8_t *
644 ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
645 {
646 u_int8_t erp;
647
648 *frm++ = IEEE80211_ELEMID_ERP;
649 *frm++ = 1;
650 erp = 0;
651 if (ic->ic_nonerpsta != 0)
652 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
653 if (ic->ic_flags & IEEE80211_F_USEPROT)
654 erp |= IEEE80211_ERP_USE_PROTECTION;
655 if (ic->ic_flags & IEEE80211_F_USEBARKER)
656 erp |= IEEE80211_ERP_LONG_PREAMBLE;
657 *frm++ = erp;
658 return frm;
659 }
660
661 static u_int8_t *
662 ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
663 {
664 #define WPA_OUI_BYTES 0x00, 0x50, 0xf2
665 #define ADDSHORT(frm, v) do { \
666 frm[0] = (v) & 0xff; \
667 frm[1] = (v) >> 8; \
668 frm += 2; \
669 } while (0)
670 #define ADDSELECTOR(frm, sel) do { \
671 memcpy(frm, sel, 4); \
672 frm += 4; \
673 } while (0)
674 static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
675 static const u_int8_t cipher_suite[][4] = {
676 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */
677 { WPA_OUI_BYTES, WPA_CSE_TKIP },
678 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */
679 { WPA_OUI_BYTES, WPA_CSE_CCMP },
680 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
681 { WPA_OUI_BYTES, WPA_CSE_NULL },
682 };
683 static const u_int8_t wep104_suite[4] =
684 { WPA_OUI_BYTES, WPA_CSE_WEP104 };
685 static const u_int8_t key_mgt_unspec[4] =
686 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
687 static const u_int8_t key_mgt_psk[4] =
688 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
689 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
690 u_int8_t *frm = ie;
691 u_int8_t *selcnt;
692
693 *frm++ = IEEE80211_ELEMID_VENDOR;
694 *frm++ = 0; /* length filled in below */
695 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */
696 frm += sizeof(oui);
697 ADDSHORT(frm, WPA_VERSION);
698
699 /* XXX filter out CKIP */
700
701 /* multicast cipher */
702 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
703 rsn->rsn_mcastkeylen >= 13)
704 ADDSELECTOR(frm, wep104_suite);
705 else
706 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
707
708 /* unicast cipher list */
709 selcnt = frm;
710 ADDSHORT(frm, 0); /* selector count */
711 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
712 selcnt[0]++;
713 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
714 }
715 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
716 selcnt[0]++;
717 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
718 }
719
720 /* authenticator selector list */
721 selcnt = frm;
722 ADDSHORT(frm, 0); /* selector count */
723 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
724 selcnt[0]++;
725 ADDSELECTOR(frm, key_mgt_unspec);
726 }
727 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
728 selcnt[0]++;
729 ADDSELECTOR(frm, key_mgt_psk);
730 }
731
732 /* optional capabilities */
733 if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
734 ADDSHORT(frm, rsn->rsn_caps);
735
736 /* calculate element length */
737 ie[1] = frm - ie - 2;
738 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
739 ("WPA IE too big, %u > %zu",
740 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
741 return frm;
742 #undef ADDSHORT
743 #undef ADDSELECTOR
744 #undef WPA_OUI_BYTES
745 }
746
747 static u_int8_t *
748 ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
749 {
750 #define RSN_OUI_BYTES 0x00, 0x0f, 0xac
751 #define ADDSHORT(frm, v) do { \
752 frm[0] = (v) & 0xff; \
753 frm[1] = (v) >> 8; \
754 frm += 2; \
755 } while (0)
756 #define ADDSELECTOR(frm, sel) do { \
757 memcpy(frm, sel, 4); \
758 frm += 4; \
759 } while (0)
760 static const u_int8_t cipher_suite[][4] = {
761 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */
762 { RSN_OUI_BYTES, RSN_CSE_TKIP },
763 { RSN_OUI_BYTES, RSN_CSE_WRAP },
764 { RSN_OUI_BYTES, RSN_CSE_CCMP },
765 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
766 { RSN_OUI_BYTES, RSN_CSE_NULL },
767 };
768 static const u_int8_t wep104_suite[4] =
769 { RSN_OUI_BYTES, RSN_CSE_WEP104 };
770 static const u_int8_t key_mgt_unspec[4] =
771 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
772 static const u_int8_t key_mgt_psk[4] =
773 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
774 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
775 u_int8_t *frm = ie;
776 u_int8_t *selcnt;
777
778 *frm++ = IEEE80211_ELEMID_RSN;
779 *frm++ = 0; /* length filled in below */
780 ADDSHORT(frm, RSN_VERSION);
781
782 /* XXX filter out CKIP */
783
784 /* multicast cipher */
785 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
786 rsn->rsn_mcastkeylen >= 13)
787 ADDSELECTOR(frm, wep104_suite);
788 else
789 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
790
791 /* unicast cipher list */
792 selcnt = frm;
793 ADDSHORT(frm, 0); /* selector count */
794 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
795 selcnt[0]++;
796 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
797 }
798 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
799 selcnt[0]++;
800 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
801 }
802
803 /* authenticator selector list */
804 selcnt = frm;
805 ADDSHORT(frm, 0); /* selector count */
806 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
807 selcnt[0]++;
808 ADDSELECTOR(frm, key_mgt_unspec);
809 }
810 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
811 selcnt[0]++;
812 ADDSELECTOR(frm, key_mgt_psk);
813 }
814
815 /* optional capabilities */
816 ADDSHORT(frm, rsn->rsn_caps);
817 /* XXX PMKID */
818
819 /* calculate element length */
820 ie[1] = frm - ie - 2;
821 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
822 ("RSN IE too big, %u > %zu",
823 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
824 return frm;
825 #undef ADDSELECTOR
826 #undef ADDSHORT
827 #undef RSN_OUI_BYTES
828 }
829
830 /*
831 * Add a WPA/RSN element to a frame.
832 */
833 static u_int8_t *
834 ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
835 {
836
837 KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
838 if (ic->ic_flags & IEEE80211_F_WPA2)
839 frm = ieee80211_setup_rsn_ie(ic, frm);
840 if (ic->ic_flags & IEEE80211_F_WPA1)
841 frm = ieee80211_setup_wpa_ie(ic, frm);
842 return frm;
843 }
844
845 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
846 /*
847 * Add a WME information element to a frame.
848 */
849 static u_int8_t *
850 ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
851 {
852 static const struct ieee80211_wme_info info = {
853 .wme_id = IEEE80211_ELEMID_VENDOR,
854 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
855 .wme_oui = { WME_OUI_BYTES },
856 .wme_type = WME_OUI_TYPE,
857 .wme_subtype = WME_INFO_OUI_SUBTYPE,
858 .wme_version = WME_VERSION,
859 .wme_info = 0,
860 };
861 memcpy(frm, &info, sizeof(info));
862 return frm + sizeof(info);
863 }
864
865 /*
866 * Add a WME parameters element to a frame.
867 */
868 static u_int8_t *
869 ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
870 {
871 #define SM(_v, _f) (((_v) << _f##_S) & _f)
872 #define ADDSHORT(frm, v) do { \
873 frm[0] = (v) & 0xff; \
874 frm[1] = (v) >> 8; \
875 frm += 2; \
876 } while (0)
877 /* NB: this works 'cuz a param has an info at the front */
878 static const struct ieee80211_wme_info param = {
879 .wme_id = IEEE80211_ELEMID_VENDOR,
880 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
881 .wme_oui = { WME_OUI_BYTES },
882 .wme_type = WME_OUI_TYPE,
883 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
884 .wme_version = WME_VERSION,
885 };
886 int i;
887
888 memcpy(frm, ¶m, sizeof(param));
889 frm += __offsetof(struct ieee80211_wme_info, wme_info);
890 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
891 *frm++ = 0; /* reserved field */
892 for (i = 0; i < WME_NUM_AC; i++) {
893 const struct wmeParams *ac =
894 &wme->wme_bssChanParams.cap_wmeParams[i];
895 *frm++ = SM(i, WME_PARAM_ACI)
896 | SM(ac->wmep_acm, WME_PARAM_ACM)
897 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
898 ;
899 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
900 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
901 ;
902 ADDSHORT(frm, ac->wmep_txopLimit);
903 }
904 return frm;
905 #undef SM
906 #undef ADDSHORT
907 }
908 #undef WME_OUI_BYTES
909
910 /*
911 * Send a management frame. The node is for the destination (or ic_bss
912 * when in station mode). Nodes other than ic_bss have their reference
913 * count bumped to reflect our use for an indeterminant time.
914 */
915 int
916 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
917 int type, int arg)
918 {
919 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
920 struct mbuf *m;
921 u_int8_t *frm;
922 enum ieee80211_phymode mode;
923 u_int16_t capinfo;
924 int has_challenge, is_shared_key, ret, timer, status;
925
926 KASSERT(ni != NULL, ("null node"));
927
928 /*
929 * Hold a reference on the node so it doesn't go away until after
930 * the xmit is complete all the way in the driver. On error we
931 * will remove our reference.
932 */
933 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
934 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
935 __func__, __LINE__,
936 ni, ether_sprintf(ni->ni_macaddr),
937 ieee80211_node_refcnt(ni)+1);
938 ieee80211_ref_node(ni);
939
940 timer = 0;
941 switch (type) {
942 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
943 /*
944 * prreq frame format
945 * [tlv] ssid
946 * [tlv] supported rates
947 * [tlv] extended supported rates
948 * [tlv] user-specified ie's
949 */
950 m = ieee80211_getmgtframe(&frm,
951 2 + IEEE80211_NWID_LEN
952 + 2 + IEEE80211_RATE_SIZE
953 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
954 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
955 );
956 if (m == NULL)
957 senderr(ENOMEM, is_tx_nobuf);
958
959 frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
960 mode = ieee80211_chan2mode(ic, ni->ni_chan);
961 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
962 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
963 if (ic->ic_opt_ie != NULL) {
964 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
965 frm += ic->ic_opt_ie_len;
966 }
967 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
968
969 IEEE80211_NODE_STAT(ni, tx_probereq);
970 if (ic->ic_opmode == IEEE80211_M_STA)
971 timer = IEEE80211_TRANS_WAIT;
972 break;
973
974 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
975 /*
976 * probe response frame format
977 * [8] time stamp
978 * [2] beacon interval
979 * [2] cabability information
980 * [tlv] ssid
981 * [tlv] supported rates
982 * [tlv] parameter set (FH/DS)
983 * [tlv] parameter set (IBSS)
984 * [tlv] extended rate phy (ERP)
985 * [tlv] extended supported rates
986 * [tlv] WPA
987 * [tlv] WME (optional)
988 */
989 m = ieee80211_getmgtframe(&frm,
990 8
991 + sizeof(u_int16_t)
992 + sizeof(u_int16_t)
993 + 2 + IEEE80211_NWID_LEN
994 + 2 + IEEE80211_RATE_SIZE
995 + 7 /* max(7,3) */
996 + 6
997 + 3
998 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
999 /* XXX !WPA1+WPA2 fits w/o a cluster */
1000 + (ic->ic_flags & IEEE80211_F_WPA ?
1001 2*sizeof(struct ieee80211_ie_wpa) : 0)
1002 + sizeof(struct ieee80211_wme_param)
1003 );
1004 if (m == NULL)
1005 senderr(ENOMEM, is_tx_nobuf);
1006
1007 memset(frm, 0, 8); /* timestamp should be filled later */
1008 frm += 8;
1009 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
1010 frm += 2;
1011 if (ic->ic_opmode == IEEE80211_M_IBSS)
1012 capinfo = IEEE80211_CAPINFO_IBSS;
1013 else
1014 capinfo = IEEE80211_CAPINFO_ESS;
1015 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1016 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1017 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1018 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1019 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1020 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1021 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1022 *(u_int16_t *)frm = htole16(capinfo);
1023 frm += 2;
1024
1025 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1026 ic->ic_bss->ni_esslen);
1027 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1028
1029 if (ic->ic_phytype == IEEE80211_T_FH) {
1030 *frm++ = IEEE80211_ELEMID_FHPARMS;
1031 *frm++ = 5;
1032 *frm++ = ni->ni_fhdwell & 0x00ff;
1033 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1034 *frm++ = IEEE80211_FH_CHANSET(
1035 ieee80211_chan2ieee(ic, ni->ni_chan));
1036 *frm++ = IEEE80211_FH_CHANPAT(
1037 ieee80211_chan2ieee(ic, ni->ni_chan));
1038 *frm++ = ni->ni_fhindex;
1039 } else {
1040 *frm++ = IEEE80211_ELEMID_DSPARMS;
1041 *frm++ = 1;
1042 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1043 }
1044
1045 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1046 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1047 *frm++ = 2;
1048 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1049 }
1050 if (ic->ic_flags & IEEE80211_F_WPA)
1051 frm = ieee80211_add_wpa(frm, ic);
1052 if (ic->ic_curmode == IEEE80211_MODE_11G)
1053 frm = ieee80211_add_erp(frm, ic);
1054 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1055 if (ic->ic_flags & IEEE80211_F_WME)
1056 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1057 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1058 break;
1059
1060 case IEEE80211_FC0_SUBTYPE_AUTH:
1061 status = arg >> 16;
1062 arg &= 0xffff;
1063 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1064 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1065 ni->ni_challenge != NULL);
1066
1067 /*
1068 * Deduce whether we're doing open authentication or
1069 * shared key authentication. We do the latter if
1070 * we're in the middle of a shared key authentication
1071 * handshake or if we're initiating an authentication
1072 * request and configured to use shared key.
1073 */
1074 is_shared_key = has_challenge ||
1075 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1076 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1077 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
1078
1079 m = ieee80211_getmgtframe(&frm,
1080 3 * sizeof(u_int16_t)
1081 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1082 sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
1083 );
1084 if (m == NULL)
1085 senderr(ENOMEM, is_tx_nobuf);
1086
1087 ((u_int16_t *)frm)[0] =
1088 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1089 : htole16(IEEE80211_AUTH_ALG_OPEN);
1090 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */
1091 ((u_int16_t *)frm)[2] = htole16(status);/* status */
1092
1093 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1094 ((u_int16_t *)frm)[3] =
1095 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1096 IEEE80211_ELEMID_CHALLENGE);
1097 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
1098 IEEE80211_CHALLENGE_LEN);
1099 m->m_pkthdr.len = m->m_len =
1100 4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
1101 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1102 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1103 "[%s] request encrypt frame (%s)\n",
1104 ether_sprintf(ni->ni_macaddr), __func__);
1105 m->m_flags |= M_LINK0; /* WEP-encrypt, please */
1106 }
1107 } else
1108 m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
1109
1110 /* XXX not right for shared key */
1111 if (status == IEEE80211_STATUS_SUCCESS)
1112 IEEE80211_NODE_STAT(ni, tx_auth);
1113 else
1114 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1115
1116 if (ic->ic_opmode == IEEE80211_M_STA)
1117 timer = IEEE80211_TRANS_WAIT;
1118 break;
1119
1120 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1121 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1122 "[%s] send station deauthenticate (reason %d)\n",
1123 ether_sprintf(ni->ni_macaddr), arg);
1124 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1125 if (m == NULL)
1126 senderr(ENOMEM, is_tx_nobuf);
1127 *(u_int16_t *)frm = htole16(arg); /* reason */
1128 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1129
1130 IEEE80211_NODE_STAT(ni, tx_deauth);
1131 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1132
1133 ieee80211_node_unauthorize(ic, ni); /* port closed */
1134 break;
1135
1136 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1137 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1138 /*
1139 * asreq frame format
1140 * [2] capability information
1141 * [2] listen interval
1142 * [6*] current AP address (reassoc only)
1143 * [tlv] ssid
1144 * [tlv] supported rates
1145 * [tlv] extended supported rates
1146 * [tlv] WME
1147 * [tlv] user-specified ie's
1148 */
1149 m = ieee80211_getmgtframe(&frm,
1150 sizeof(u_int16_t)
1151 + sizeof(u_int16_t)
1152 + IEEE80211_ADDR_LEN
1153 + 2 + IEEE80211_NWID_LEN
1154 + 2 + IEEE80211_RATE_SIZE
1155 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1156 + sizeof(struct ieee80211_wme_info)
1157 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1158 );
1159 if (m == NULL)
1160 senderr(ENOMEM, is_tx_nobuf);
1161
1162 capinfo = 0;
1163 if (ic->ic_opmode == IEEE80211_M_IBSS)
1164 capinfo |= IEEE80211_CAPINFO_IBSS;
1165 else /* IEEE80211_M_STA */
1166 capinfo |= IEEE80211_CAPINFO_ESS;
1167 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1168 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1169 /*
1170 * NB: Some 11a AP's reject the request when
1171 * short premable is set.
1172 */
1173 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1174 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1175 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1176 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
1177 (ic->ic_caps & IEEE80211_C_SHSLOT))
1178 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1179 *(u_int16_t *)frm = htole16(capinfo);
1180 frm += 2;
1181
1182 *(u_int16_t *)frm = htole16(ic->ic_lintval);
1183 frm += 2;
1184
1185 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1186 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1187 frm += IEEE80211_ADDR_LEN;
1188 }
1189
1190 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1191 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1192 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1193 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1194 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1195 if (ic->ic_opt_ie != NULL) {
1196 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1197 frm += ic->ic_opt_ie_len;
1198 }
1199 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1200
1201 timer = IEEE80211_TRANS_WAIT;
1202 break;
1203
1204 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1205 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1206 /*
1207 * asreq frame format
1208 * [2] capability information
1209 * [2] status
1210 * [2] association ID
1211 * [tlv] supported rates
1212 * [tlv] extended supported rates
1213 * [tlv] WME (if enabled and STA enabled)
1214 */
1215 m = ieee80211_getmgtframe(&frm,
1216 sizeof(u_int16_t)
1217 + sizeof(u_int16_t)
1218 + sizeof(u_int16_t)
1219 + 2 + IEEE80211_RATE_SIZE
1220 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1221 + sizeof(struct ieee80211_wme_param)
1222 );
1223 if (m == NULL)
1224 senderr(ENOMEM, is_tx_nobuf);
1225
1226 capinfo = IEEE80211_CAPINFO_ESS;
1227 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1228 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1229 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1230 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1231 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1232 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1233 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1234 *(u_int16_t *)frm = htole16(capinfo);
1235 frm += 2;
1236
1237 *(u_int16_t *)frm = htole16(arg); /* status */
1238 frm += 2;
1239
1240 if (arg == IEEE80211_STATUS_SUCCESS) {
1241 *(u_int16_t *)frm = htole16(ni->ni_associd);
1242 IEEE80211_NODE_STAT(ni, tx_assoc);
1243 } else
1244 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
1245 frm += 2;
1246
1247 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1248 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1249 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1250 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1251 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1252 break;
1253
1254 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1255 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1256 "[%s] send station disassociate (reason %d)\n",
1257 ether_sprintf(ni->ni_macaddr), arg);
1258 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1259 if (m == NULL)
1260 senderr(ENOMEM, is_tx_nobuf);
1261 *(u_int16_t *)frm = htole16(arg); /* reason */
1262 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1263
1264 IEEE80211_NODE_STAT(ni, tx_disassoc);
1265 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
1266 break;
1267
1268 default:
1269 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1270 "[%s] invalid mgmt frame type %u\n",
1271 ether_sprintf(ni->ni_macaddr), type);
1272 senderr(EINVAL, is_tx_unknownmgt);
1273 /* NOTREACHED */
1274 }
1275
1276 ret = ieee80211_mgmt_output(ic, ni, m, type);
1277 if (ret == 0) {
1278 if (timer)
1279 ic->ic_mgt_timer = timer;
1280 } else {
1281 bad:
1282 ieee80211_free_node(ni);
1283 }
1284 return ret;
1285 #undef senderr
1286 }
1287
1288 /*
1289 * Allocate a beacon frame and fillin the appropriate bits.
1290 */
1291 struct mbuf *
1292 ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
1293 struct ieee80211_beacon_offsets *bo)
1294 {
1295 struct ifnet *ifp = ic->ic_ifp;
1296 struct ieee80211_frame *wh;
1297 struct mbuf *m;
1298 int pktlen;
1299 u_int8_t *frm, *efrm;
1300 u_int16_t capinfo;
1301 struct ieee80211_rateset *rs;
1302
1303 /*
1304 * beacon frame format
1305 * [8] time stamp
1306 * [2] beacon interval
1307 * [2] cabability information
1308 * [tlv] ssid
1309 * [tlv] supported rates
1310 * [3] parameter set (DS)
1311 * [tlv] parameter set (IBSS/TIM)
1312 * [tlv] extended rate phy (ERP)
1313 * [tlv] extended supported rates
1314 * [tlv] WME parameters
1315 * [tlv] WPA/RSN parameters
1316 * XXX Vendor-specific OIDs (e.g. Atheros)
1317 * NB: we allocate the max space required for the TIM bitmap.
1318 */
1319 rs = &ni->ni_rates;
1320 pktlen = 8 /* time stamp */
1321 + sizeof(u_int16_t) /* beacon interval */
1322 + sizeof(u_int16_t) /* capabilities */
1323 + 2 + ni->ni_esslen /* ssid */
1324 + 2 + IEEE80211_RATE_SIZE /* supported rates */
1325 + 2 + 1 /* DS parameters */
1326 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */
1327 + 2 + 1 /* ERP */
1328 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1329 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */
1330 sizeof(struct ieee80211_wme_param) : 0)
1331 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
1332 2*sizeof(struct ieee80211_ie_wpa) : 0)
1333 ;
1334 m = ieee80211_getmgtframe(&frm, pktlen);
1335 if (m == NULL) {
1336 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1337 "%s: cannot get buf; size %u\n", __func__, pktlen);
1338 ic->ic_stats.is_tx_nobuf++;
1339 return NULL;
1340 }
1341
1342 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
1343 frm += 8;
1344 *(u_int16_t *)frm = htole16(ni->ni_intval);
1345 frm += 2;
1346 if (ic->ic_opmode == IEEE80211_M_IBSS)
1347 capinfo = IEEE80211_CAPINFO_IBSS;
1348 else
1349 capinfo = IEEE80211_CAPINFO_ESS;
1350 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1351 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1352 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1353 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1354 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1355 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1356 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1357 bo->bo_caps = (u_int16_t *)frm;
1358 *(u_int16_t *)frm = htole16(capinfo);
1359 frm += 2;
1360 *frm++ = IEEE80211_ELEMID_SSID;
1361 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
1362 *frm++ = ni->ni_esslen;
1363 memcpy(frm, ni->ni_essid, ni->ni_esslen);
1364 frm += ni->ni_esslen;
1365 } else
1366 *frm++ = 0;
1367 frm = ieee80211_add_rates(frm, rs);
1368 if (ic->ic_curmode != IEEE80211_MODE_FH) {
1369 *frm++ = IEEE80211_ELEMID_DSPARMS;
1370 *frm++ = 1;
1371 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1372 }
1373 bo->bo_tim = frm;
1374 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1375 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1376 *frm++ = 2;
1377 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1378 bo->bo_tim_len = 0;
1379 } else {
1380 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
1381
1382 tie->tim_ie = IEEE80211_ELEMID_TIM;
1383 tie->tim_len = 4; /* length */
1384 tie->tim_count = 0; /* DTIM count */
1385 tie->tim_period = ic->ic_dtim_period; /* DTIM period */
1386 tie->tim_bitctl = 0; /* bitmap control */
1387 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
1388 frm += sizeof(struct ieee80211_tim_ie);
1389 bo->bo_tim_len = 1;
1390 }
1391 bo->bo_trailer = frm;
1392 if (ic->ic_flags & IEEE80211_F_WME) {
1393 bo->bo_wme = frm;
1394 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1395 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1396 }
1397 if (ic->ic_flags & IEEE80211_F_WPA)
1398 frm = ieee80211_add_wpa(frm, ic);
1399 if (ic->ic_curmode == IEEE80211_MODE_11G)
1400 frm = ieee80211_add_erp(frm, ic);
1401 efrm = ieee80211_add_xrates(frm, rs);
1402 bo->bo_trailer_len = efrm - bo->bo_trailer;
1403 m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
1404
1405 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1406 KASSERT(m != NULL, ("no space for 802.11 header?"));
1407 wh = mtod(m, struct ieee80211_frame *);
1408 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1409 IEEE80211_FC0_SUBTYPE_BEACON;
1410 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1411 *(u_int16_t *)wh->i_dur = 0;
1412 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1413 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1414 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1415 *(u_int16_t *)wh->i_seq = 0;
1416
1417 return m;
1418 }
1419
1420 /*
1421 * Update the dynamic parts of a beacon frame based on the current state.
1422 */
1423 int
1424 ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
1425 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
1426 {
1427 int len_changed = 0;
1428 u_int16_t capinfo;
1429
1430 IEEE80211_BEACON_LOCK(ic);
1431 /* XXX faster to recalculate entirely or just changes? */
1432 if (ic->ic_opmode == IEEE80211_M_IBSS)
1433 capinfo = IEEE80211_CAPINFO_IBSS;
1434 else
1435 capinfo = IEEE80211_CAPINFO_ESS;
1436 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1437 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1438 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1439 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1440 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1441 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1442 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1443 *bo->bo_caps = htole16(capinfo);
1444
1445 if (ic->ic_flags & IEEE80211_F_WME) {
1446 struct ieee80211_wme_state *wme = &ic->ic_wme;
1447
1448 /*
1449 * Check for agressive mode change. When there is
1450 * significant high priority traffic in the BSS
1451 * throttle back BE traffic by using conservative
1452 * parameters. Otherwise BE uses agressive params
1453 * to optimize performance of legacy/non-QoS traffic.
1454 */
1455 if (wme->wme_flags & WME_F_AGGRMODE) {
1456 if (wme->wme_hipri_traffic >
1457 wme->wme_hipri_switch_thresh) {
1458 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1459 "%s: traffic %u, disable aggressive mode\n",
1460 __func__, wme->wme_hipri_traffic);
1461 wme->wme_flags &= ~WME_F_AGGRMODE;
1462 ieee80211_wme_updateparams_locked(ic);
1463 wme->wme_hipri_traffic =
1464 wme->wme_hipri_switch_hysteresis;
1465 } else
1466 wme->wme_hipri_traffic = 0;
1467 } else {
1468 if (wme->wme_hipri_traffic <=
1469 wme->wme_hipri_switch_thresh) {
1470 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1471 "%s: traffic %u, enable aggressive mode\n",
1472 __func__, wme->wme_hipri_traffic);
1473 wme->wme_flags |= WME_F_AGGRMODE;
1474 ieee80211_wme_updateparams_locked(ic);
1475 wme->wme_hipri_traffic = 0;
1476 } else
1477 wme->wme_hipri_traffic =
1478 wme->wme_hipri_switch_hysteresis;
1479 }
1480 if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
1481 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
1482 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1483 }
1484 }
1485
1486 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/
1487 struct ieee80211_tim_ie *tie =
1488 (struct ieee80211_tim_ie *) bo->bo_tim;
1489 if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
1490 u_int timlen, timoff, i;
1491 /*
1492 * ATIM/DTIM needs updating. If it fits in the
1493 * current space allocated then just copy in the
1494 * new bits. Otherwise we need to move any trailing
1495 * data to make room. Note that we know there is
1496 * contiguous space because ieee80211_beacon_allocate
1497 * insures there is space in the mbuf to write a
1498 * maximal-size virtual bitmap (based on ic_max_aid).
1499 */
1500 /*
1501 * Calculate the bitmap size and offset, copy any
1502 * trailer out of the way, and then copy in the
1503 * new bitmap and update the information element.
1504 * Note that the tim bitmap must contain at least
1505 * one byte and any offset must be even.
1506 */
1507 if (ic->ic_ps_pending != 0) {
1508 timoff = 128; /* impossibly large */
1509 for (i = 0; i < ic->ic_tim_len; i++)
1510 if (ic->ic_tim_bitmap[i]) {
1511 timoff = i &~ 1;
1512 break;
1513 }
1514 KASSERT(timoff != 128, ("tim bitmap empty!"));
1515 for (i = ic->ic_tim_len-1; i >= timoff; i--)
1516 if (ic->ic_tim_bitmap[i])
1517 break;
1518 timlen = 1 + (i - timoff);
1519 } else {
1520 timoff = 0;
1521 timlen = 1;
1522 }
1523 if (timlen != bo->bo_tim_len) {
1524 /* copy up/down trailer */
1525 ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen,
1526 bo->bo_trailer_len);
1527 bo->bo_trailer = tie->tim_bitmap+timlen;
1528 bo->bo_wme = bo->bo_trailer;
1529 bo->bo_tim_len = timlen;
1530
1531 /* update information element */
1532 tie->tim_len = 3 + timlen;
1533 tie->tim_bitctl = timoff;
1534 len_changed = 1;
1535 }
1536 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
1537 bo->bo_tim_len);
1538
1539 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
1540
1541 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1542 "%s: TIM updated, pending %u, off %u, len %u\n",
1543 __func__, ic->ic_ps_pending, timoff, timlen);
1544 }
1545 /* count down DTIM period */
1546 if (tie->tim_count == 0)
1547 tie->tim_count = tie->tim_period - 1;
1548 else
1549 tie->tim_count--;
1550 /* update state for buffered multicast frames on DTIM */
1551 if (mcast && (tie->tim_count == 1 || tie->tim_period == 1))
1552 tie->tim_bitctl |= 1;
1553 else
1554 tie->tim_bitctl &= ~1;
1555 }
1556 IEEE80211_BEACON_UNLOCK(ic);
1557
1558 return len_changed;
1559 }
1560
1561 /*
1562 * Save an outbound packet for a node in power-save sleep state.
1563 * The new packet is placed on the node's saved queue, and the TIM
1564 * is changed, if necessary.
1565 */
1566 void
1567 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
1568 struct mbuf *m)
1569 {
1570 int qlen, age;
1571
1572 IEEE80211_NODE_SAVEQ_LOCK(ni);
1573 if (_IF_QFULL(&ni->ni_savedq)) {
1574 _IF_DROP(&ni->ni_savedq);
1575 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1576 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1577 "[%s] pwr save q overflow, drops %d (size %d)\n",
1578 ether_sprintf(ni->ni_macaddr),
1579 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
1580 #ifdef IEEE80211_DEBUG
1581 if (ieee80211_msg_dumppkts(ic))
1582 ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
1583 #endif
1584 m_freem(m);
1585 return;
1586 }
1587 /*
1588 * Tag the frame with it's expiry time and insert
1589 * it in the queue. The aging interval is 4 times
1590 * the listen interval specified by the station.
1591 * Frames that sit around too long are reclaimed
1592 * using this information.
1593 */
1594 /* XXX handle overflow? */
1595 age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; /* TU -> secs */
1596 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
1597 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1598
1599 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1600 "[%s] save frame, %u now queued\n",
1601 ether_sprintf(ni->ni_macaddr), qlen);
1602
1603 if (qlen == 1)
1604 ic->ic_set_tim(ic, ni, 1);
1605 }
1606