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