ieee80211_node.c revision 118887
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002, 2003 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: head/sys/net80211/ieee80211_node.c 118887 2003-08-13 22:09:44Z sam $");
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/malloc.h>
42#include <sys/kernel.h>
43#include <sys/socket.h>
44#include <sys/sockio.h>
45#include <sys/endian.h>
46#include <sys/errno.h>
47#include <sys/bus.h>
48#include <sys/proc.h>
49#include <sys/sysctl.h>
50
51#include <machine/atomic.h>
52
53#include <net/if.h>
54#include <net/if_dl.h>
55#include <net/if_media.h>
56#include <net/if_arp.h>
57#include <net/ethernet.h>
58#include <net/if_llc.h>
59
60#include <net80211/ieee80211_var.h>
61
62#include <net/bpf.h>
63
64#ifdef INET
65#include <netinet/in.h>
66#include <netinet/if_ether.h>
67#endif
68
69static struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *);
70static void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *);
71static void ieee80211_node_copy(struct ieee80211com *,
72		struct ieee80211_node *, const struct ieee80211_node *);
73static void ieee80211_setup_node(struct ieee80211com *ic,
74		struct ieee80211_node *ni, u_int8_t *macaddr);
75static void _ieee80211_free_node(struct ieee80211com *,
76		struct ieee80211_node *);
77
78void
79ieee80211_node_attach(struct ifnet *ifp)
80{
81	struct ieee80211com *ic = (void *)ifp;
82
83	/* XXX need unit */
84	mtx_init(&ic->ic_nodelock, ifp->if_name, "802.11 node table", MTX_DEF);
85	TAILQ_INIT(&ic->ic_node);
86	ic->ic_node_alloc = ieee80211_node_alloc;
87	ic->ic_node_free = ieee80211_node_free;
88	ic->ic_node_copy = ieee80211_node_copy;
89}
90
91void
92ieee80211_node_lateattach(struct ifnet *ifp)
93{
94	struct ieee80211com *ic = (void *)ifp;
95
96	ic->ic_bss = (*ic->ic_node_alloc)(ic);
97	KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node"));
98	ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC;
99}
100
101void
102ieee80211_node_detach(struct ifnet *ifp)
103{
104	struct ieee80211com *ic = (void *)ifp;
105
106	if (ic->ic_bss != NULL)
107		(*ic->ic_node_free)(ic, ic->ic_bss);
108	ieee80211_free_allnodes(ic);
109	mtx_destroy(&ic->ic_nodelock);
110}
111
112/*
113 * AP scanning support.
114 */
115
116/*
117 * Initialize the active channel set based on the set
118 * of available channels and the current PHY mode.
119 */
120static void
121ieee80211_reset_scan(struct ifnet *ifp)
122{
123	struct ieee80211com *ic = (void *)ifp;
124
125	memcpy(ic->ic_chan_scan, ic->ic_chan_active,
126		sizeof(ic->ic_chan_active));
127	/* NB: hack, setup so next_scan starts with the first channel */
128	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
129		ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
130}
131
132/*
133 * Begin an active scan.
134 */
135void
136ieee80211_begin_scan(struct ifnet *ifp)
137{
138	struct ieee80211com *ic = (void *)ifp;
139
140	/*
141	 * In all but hostap mode scanning starts off in
142	 * an active mode before switching to passive.
143	 */
144	if (ic->ic_opmode != IEEE80211_M_HOSTAP)
145		ic->ic_flags |= IEEE80211_F_ASCAN;
146	if (ifp->if_flags & IFF_DEBUG)
147		if_printf(ifp, "begin %s scan\n",
148			(ic->ic_flags & IEEE80211_F_ASCAN) ?
149				"active" : "passive");
150	/*
151	 * Clear scan state and flush any previously seen
152	 * AP's.  Note that the latter assumes we don't act
153	 * as both an AP and a station, otherwise we'll
154	 * potentially flush state of stations associated
155	 * with us.
156	 */
157	ieee80211_reset_scan(ifp);
158	ieee80211_free_allnodes(ic);
159
160	/* Scan the next channel. */
161	ieee80211_next_scan(ifp);
162}
163
164/*
165 * Switch to the next channel marked for scanning.
166 */
167void
168ieee80211_next_scan(struct ifnet *ifp)
169{
170	struct ieee80211com *ic = (void *)ifp;
171	struct ieee80211_channel *chan;
172
173	chan = ic->ic_bss->ni_chan;
174	for (;;) {
175		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
176			chan = &ic->ic_channels[0];
177		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
178			/*
179			 * Honor channels marked passive-only
180			 * during an active scan.
181			 */
182			if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
183			    (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
184				break;
185		}
186		if (chan == ic->ic_bss->ni_chan) {
187			ieee80211_end_scan(ifp);
188			return;
189		}
190	}
191	clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
192	IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
193	    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
194	    ieee80211_chan2ieee(ic, chan)));
195	ic->ic_bss->ni_chan = chan;
196	ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
197}
198
199void
200ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
201{
202	struct ieee80211_node *ni;
203	struct ifnet *ifp = &ic->ic_if;
204
205	ni = ic->ic_bss;
206	if (ifp->if_flags & IFF_DEBUG)
207		if_printf(ifp, "creating ibss\n");
208	ic->ic_flags |= IEEE80211_F_SIBSS;
209	ni->ni_chan = chan;
210	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
211	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
212	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
213	if (ic->ic_opmode == IEEE80211_M_IBSS)
214		ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
215	ni->ni_esslen = ic->ic_des_esslen;
216	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
217	ni->ni_rssi = 0;
218	ni->ni_rstamp = 0;
219	ni->ni_rantenna = 0;
220	memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
221	ni->ni_intval = ic->ic_lintval;
222	ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
223	if (ic->ic_flags & IEEE80211_F_WEPON)
224		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
225	if (ic->ic_phytype == IEEE80211_T_FH) {
226		ni->ni_fhdwell = 200;	/* XXX */
227		ni->ni_fhindex = 1;
228	}
229	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
230}
231
232/*
233 * Complete a scan of potential channels.
234 */
235void
236ieee80211_end_scan(struct ifnet *ifp)
237{
238	struct ieee80211com *ic = (void *)ifp;
239	struct ieee80211_node *ni, *nextbs, *selbs;
240	u_int8_t rate;
241	int i, fail;
242
243	ic->ic_flags &= ~IEEE80211_F_ASCAN;
244	ni = TAILQ_FIRST(&ic->ic_node);
245
246	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
247		/* XXX off stack? */
248		u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
249		/*
250		 * The passive scan to look for existing AP's completed,
251		 * select a channel to camp on.  Identify the channels
252		 * that already have one or more AP's and try to locate
253		 * an unnoccupied one.  If that fails, pick a random
254		 * channel from the active set.
255		 */
256		for (; ni != NULL; ni = nextbs) {
257			ieee80211_ref_node(ni);
258			nextbs = TAILQ_NEXT(ni, ni_list);
259			setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
260			ieee80211_free_node(ic, ni);
261		}
262		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
263			if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
264				break;
265		if (i == IEEE80211_CHAN_MAX) {
266			fail = arc4random() & 3;	/* random 0-3 */
267			for (i = 0; i < IEEE80211_CHAN_MAX; i++)
268				if (isset(ic->ic_chan_active, i) && fail-- == 0)
269					break;
270		}
271		ieee80211_create_ibss(ic, &ic->ic_channels[i]);
272		return;
273	}
274	if (ni == NULL) {
275		IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
276  notfound:
277		if (ic->ic_opmode == IEEE80211_M_IBSS &&
278		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
279		    ic->ic_des_esslen != 0) {
280			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
281			return;
282		}
283		/*
284		 * Reset the list of channels to scan and start again.
285		 */
286		ieee80211_reset_scan(ifp);
287		ieee80211_next_scan(ifp);
288		return;
289	}
290	selbs = NULL;
291	if (ifp->if_flags & IFF_DEBUG)
292		if_printf(ifp, "\tmacaddr          bssid         chan  rssi rate ant flag  wep  essid\n");
293	for (; ni != NULL; ni = nextbs) {
294		ieee80211_ref_node(ni);
295		nextbs = TAILQ_NEXT(ni, ni_list);
296		if (ni->ni_fails) {
297			/*
298			 * The configuration of the access points may change
299			 * during my scan.  So delete the entry for the AP
300			 * and retry to associate if there is another beacon.
301			 */
302			if (ni->ni_fails++ > 2)
303				ieee80211_free_node(ic, ni);
304			continue;
305		}
306		fail = 0;
307		if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
308			fail |= 0x01;
309		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
310		    ni->ni_chan != ic->ic_des_chan)
311			fail |= 0x01;
312		if (ic->ic_opmode == IEEE80211_M_IBSS) {
313			if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
314				fail |= 0x02;
315		} else {
316			if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
317				fail |= 0x02;
318		}
319		if (ic->ic_flags & IEEE80211_F_WEPON) {
320			if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
321				fail |= 0x04;
322		} else {
323			if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
324				fail |= 0x04;
325		}
326		rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
327		if (rate & IEEE80211_RATE_BASIC)
328			fail |= 0x08;
329		if (ic->ic_des_esslen != 0 &&
330		    (ni->ni_esslen != ic->ic_des_esslen ||
331		     memcmp(ni->ni_essid, ic->ic_des_essid,
332		     ic->ic_des_esslen != 0)))
333			fail |= 0x10;
334		if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
335		    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
336			fail |= 0x20;
337		if (ifp->if_flags & IFF_DEBUG) {
338			printf(" %c %s", fail ? '-' : '+',
339			    ether_sprintf(ni->ni_macaddr));
340			printf(" %s%c", ether_sprintf(ni->ni_bssid),
341			    fail & 0x20 ? '!' : ' ');
342			printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
343				fail & 0x01 ? '!' : ' ');
344			printf(" %+4d", ni->ni_rssi);
345			printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
346			    fail & 0x08 ? '!' : ' ');
347			printf(" %3d", ni->ni_rantenna);
348			printf(" %4s%c",
349			    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
350			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
351			    "????",
352			    fail & 0x02 ? '!' : ' ');
353			printf(" %3s%c ",
354			    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
355			    "wep" : "no",
356			    fail & 0x04 ? '!' : ' ');
357			ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
358			printf("%s\n", fail & 0x10 ? "!" : "");
359		}
360		if (!fail) {
361			if (selbs == NULL)
362				selbs = ni;
363			else if (ni->ni_rssi > selbs->ni_rssi) {
364				ieee80211_unref_node(&selbs);
365				selbs = ni;
366			} else
367				ieee80211_unref_node(&ni);
368		} else {
369			ieee80211_unref_node(&ni);
370		}
371	}
372	if (selbs == NULL)
373		goto notfound;
374	(*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
375	if (ic->ic_opmode == IEEE80211_M_IBSS) {
376		ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
377		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
378		if (ic->ic_bss->ni_rates.rs_nrates == 0) {
379			selbs->ni_fails++;
380			ieee80211_unref_node(&selbs);
381			goto notfound;
382		}
383		ieee80211_unref_node(&selbs);
384		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
385	} else {
386		ieee80211_unref_node(&selbs);
387		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
388	}
389}
390
391static struct ieee80211_node *
392ieee80211_node_alloc(struct ieee80211com *ic)
393{
394	return malloc(sizeof(struct ieee80211_node), M_DEVBUF,
395		M_NOWAIT | M_ZERO);
396}
397
398static void
399ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
400{
401	free(ni, M_DEVBUF);
402}
403
404static void
405ieee80211_node_copy(struct ieee80211com *ic,
406	struct ieee80211_node *dst, const struct ieee80211_node *src)
407{
408	*dst = *src;
409}
410
411static void
412ieee80211_setup_node(struct ieee80211com *ic,
413	struct ieee80211_node *ni, u_int8_t *macaddr)
414{
415	int hash;
416
417	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
418	hash = IEEE80211_NODE_HASH(macaddr);
419	ni->ni_refcnt = 1;		/* mark referenced */
420	mtx_lock(&ic->ic_nodelock);
421	TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
422	LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
423	/*
424	 * Note we don't enable the inactive timer when acting
425	 * as a station.  Nodes created in this mode represent
426	 * AP's identified while scanning.  If we time them out
427	 * then several things happen: we can't return the data
428	 * to users to show the list of AP's we encountered, and
429	 * more importantly, we'll incorrectly deauthenticate
430	 * ourself because the inactivity timer will kick us off.
431	 */
432	if (ic->ic_opmode != IEEE80211_M_STA)
433		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
434	mtx_unlock(&ic->ic_nodelock);
435}
436
437struct ieee80211_node *
438ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
439{
440	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
441	if (ni != NULL)
442		ieee80211_setup_node(ic, ni, macaddr);
443	return ni;
444}
445
446struct ieee80211_node *
447ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
448{
449	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
450	if (ni != NULL) {
451		memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node));
452		ieee80211_setup_node(ic, ni, macaddr);
453	}
454	return ni;
455}
456
457struct ieee80211_node *
458ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
459{
460	struct ieee80211_node *ni;
461	int hash;
462
463	hash = IEEE80211_NODE_HASH(macaddr);
464	mtx_lock(&ic->ic_nodelock);
465	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
466		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
467			atomic_add_int(&ni->ni_refcnt, 1); /* mark referenced */
468			break;
469		}
470	}
471	mtx_unlock(&ic->ic_nodelock);
472	return ni;
473}
474
475/*
476 * Like find but search based on the channel too.
477 */
478struct ieee80211_node *
479ieee80211_lookup_node(struct ieee80211com *ic,
480	u_int8_t *macaddr, struct ieee80211_channel *chan)
481{
482	struct ieee80211_node *ni;
483	int hash;
484
485	hash = IEEE80211_NODE_HASH(macaddr);
486	mtx_lock(&ic->ic_nodelock);
487	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
488		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) {
489			atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */
490			break;
491		}
492	}
493	mtx_unlock(&ic->ic_nodelock);
494	return ni;
495}
496
497static void
498_ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
499{
500	TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
501	LIST_REMOVE(ni, ni_hash);
502	if (TAILQ_EMPTY(&ic->ic_node))
503		ic->ic_inact_timer = 0;
504	(*ic->ic_node_free)(ic, ni);
505}
506
507void
508ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
509{
510	/* XXX need equivalent of atomic_dec_and_test */
511	atomic_subtract_int(&ni->ni_refcnt, 1);
512	if (atomic_cmpset_int(&ni->ni_refcnt, 0, 1)) {
513		mtx_lock(&ic->ic_nodelock);
514		_ieee80211_free_node(ic, ni);
515		mtx_unlock(&ic->ic_nodelock);
516	}
517}
518
519void
520ieee80211_free_allnodes(struct ieee80211com *ic)
521{
522	struct ieee80211_node *ni;
523
524	mtx_lock(&ic->ic_nodelock);
525	while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
526		_ieee80211_free_node(ic, ni);
527	mtx_unlock(&ic->ic_nodelock);
528}
529
530void
531ieee80211_timeout_nodes(struct ieee80211com *ic)
532{
533	struct ieee80211_node *ni, *nextbs;
534
535	mtx_lock(&ic->ic_nodelock);
536	for (ni = TAILQ_FIRST(&ic->ic_node); ni != NULL;) {
537		if (++ni->ni_inact <= IEEE80211_INACT_MAX) {
538			ni = TAILQ_NEXT(ni, ni_list);
539			continue;
540		}
541		/* NB: don't honor reference count */
542		IEEE80211_DPRINTF(("station %s timed out "
543			    "due to inactivity (%u secs)\n",
544			    ether_sprintf(ni->ni_macaddr),
545			    ni->ni_inact));
546		nextbs = TAILQ_NEXT(ni, ni_list);
547		IEEE80211_SEND_MGMT(ic, ni,
548		    IEEE80211_FC0_SUBTYPE_DEAUTH,
549		    IEEE80211_REASON_AUTH_EXPIRE);
550		_ieee80211_free_node(ic, ni);
551		ni = nextbs;
552	}
553	if (!TAILQ_EMPTY(&ic->ic_node))
554		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
555	mtx_unlock(&ic->ic_nodelock);
556}
557
558void
559ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
560{
561	struct ieee80211_node *ni;
562
563	mtx_lock(&ic->ic_nodelock);
564	TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
565		(*f)(arg, ni);
566	mtx_unlock(&ic->ic_nodelock);
567}
568