iPXE
net80211.c
Go to the documentation of this file.
1 /*
2  * The iPXE 802.11 MAC layer.
3  *
4  * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 FILE_LICENCE ( GPL2_OR_LATER );
23 
24 #include <string.h>
25 #include <byteswap.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <ipxe/settings.h>
30 #include <ipxe/if_arp.h>
31 #include <ipxe/ethernet.h>
32 #include <ipxe/ieee80211.h>
33 #include <ipxe/netdevice.h>
34 #include <ipxe/net80211.h>
35 #include <ipxe/sec80211.h>
36 #include <ipxe/timer.h>
37 #include <ipxe/nap.h>
38 #include <ipxe/errortab.h>
39 #include <ipxe/net80211_err.h>
40 
41 /** @file
42  *
43  * 802.11 device management
44  */
45 
46 /** List of 802.11 devices */
48 
49 /** Set of device operations that does nothing */
51 
52 /** Information associated with a received management packet
53  *
54  * This is used to keep beacon signal strengths in a parallel queue to
55  * the beacons themselves.
56  */
58  int signal;
59  struct list_head list;
60 };
61 
62 /** Context for a probe operation */
64  /** 802.11 device to probe on */
66 
67  /** Value of keep_mgmt before probe was started */
69 
70  /** If scanning actively, pointer to probe packet to send */
71  struct io_buffer *probe;
72 
73  /** If non-"", the ESSID to limit ourselves to */
74  const char *essid;
75 
76  /** Time probe was started */
78 
79  /** Time last useful beacon was received */
81 
82  /** Time channel was last changed */
84 
85  /** Time to stay on each channel */
87 
88  /** Channels to hop by when changing channel */
89  int hop_step;
90 
91  /** List of best beacons for each network found so far */
92  struct list_head *beacons;
93 };
94 
95 /** Context for the association task */
97  /** Next authentication method to try using */
98  int method;
99 
100  /** Time (in ticks) of the last sent association-related packet */
102 
103  /** Number of times we have tried sending it */
105 };
106 
107 /**
108  * Detect secure 802.11 network when security support is not available
109  *
110  * @return -ENOTSUP, always.
111  */
113  enum net80211_security_proto *secprot __unused,
114  enum net80211_crypto_alg *crypt __unused ) {
115  return -ENOTSUP;
116 }
117 
118 /**
119  * @defgroup net80211_netdev Network device interface functions
120  * @{
121  */
122 static int net80211_netdev_open ( struct net_device *netdev );
123 static void net80211_netdev_close ( struct net_device *netdev );
124 static int net80211_netdev_transmit ( struct net_device *netdev,
125  struct io_buffer *iobuf );
126 static void net80211_netdev_poll ( struct net_device *netdev );
127 static void net80211_netdev_irq ( struct net_device *netdev, int enable );
128 /** @} */
129 
130 /**
131  * @defgroup net80211_linklayer 802.11 link-layer protocol functions
132  * @{
133  */
134 static int net80211_ll_push ( struct net_device *netdev,
135  struct io_buffer *iobuf, const void *ll_dest,
136  const void *ll_source, uint16_t net_proto );
137 static int net80211_ll_pull ( struct net_device *netdev,
138  struct io_buffer *iobuf, const void **ll_dest,
139  const void **ll_source, uint16_t * net_proto,
140  unsigned int *flags );
141 /** @} */
142 
143 /**
144  * @defgroup net80211_help 802.11 helper functions
145  * @{
146  */
147 static void net80211_add_channels ( struct net80211_device *dev, int start,
148  int len, int txpower );
149 static void net80211_filter_hw_channels ( struct net80211_device *dev );
150 static void net80211_set_rtscts_rate ( struct net80211_device *dev );
151 static int net80211_process_capab ( struct net80211_device *dev,
152  u16 capab );
153 static int net80211_process_ie ( struct net80211_device *dev,
154  union ieee80211_ie *ie, void *ie_end );
155 static union ieee80211_ie *
157  union ieee80211_ie *ie );
158 /** @} */
159 
160 /**
161  * @defgroup net80211_assoc_ll 802.11 association handling functions
162  * @{
163  */
164 static void net80211_step_associate ( struct net80211_device *dev );
165 static void net80211_handle_auth ( struct net80211_device *dev,
166  struct io_buffer *iob );
167 static void net80211_handle_assoc_reply ( struct net80211_device *dev,
168  struct io_buffer *iob );
169 static int net80211_send_disassoc ( struct net80211_device *dev, int reason,
170  int deauth );
171 static void net80211_handle_mgmt ( struct net80211_device *dev,
172  struct io_buffer *iob, int signal );
173 /** @} */
174 
175 /**
176  * @defgroup net80211_frag 802.11 fragment handling functions
177  * @{
178  */
179 static void net80211_free_frags ( struct net80211_device *dev, int fcid );
180 static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
181  int fcid, int nfrags, int size );
182 static void net80211_rx_frag ( struct net80211_device *dev,
183  struct io_buffer *iob, int signal );
184 /** @} */
185 
186 /**
187  * @defgroup net80211_settings 802.11 settings handlers
188  * @{
189  */
190 static int net80211_check_settings_update ( void );
191 
192 /** 802.11 settings applicator
193  *
194  * When the SSID is changed, this will cause any open devices to
195  * re-associate; when the encryption key is changed, we similarly
196  * update their state.
197  */
198 struct settings_applicator net80211_applicator __settings_applicator = {
200 };
201 
202 /** The network name to associate with
203  *
204  * If this is blank, we scan for all networks and use the one with the
205  * greatest signal strength.
206  */
207 const struct setting net80211_ssid_setting __setting ( SETTING_NETDEV_EXTRA,
208  ssid ) = {
209  .name = "ssid",
210  .description = "Wireless SSID",
211  .type = &setting_type_string,
212 };
213 
214 /** Whether to use active scanning
215  *
216  * In order to associate with a hidden SSID, it's necessary to use an
217  * active scan (send probe packets). If this setting is nonzero, an
218  * active scan on the 2.4GHz band will be used to associate.
219  */
220 const struct setting net80211_active_setting __setting ( SETTING_NETDEV_EXTRA,
221  active-scan ) = {
222  .name = "active-scan",
223  .description = "Actively scan for wireless networks",
224  .type = &setting_type_int8,
225 };
226 
227 /** The cryptographic key to use
228  *
229  * For hex WEP keys, as is common, this must be entered using the
230  * normal iPXE method for entering hex settings; an ASCII string of
231  * hex characters will not behave as expected.
232  */
233 const struct setting net80211_key_setting __setting ( SETTING_NETDEV_EXTRA,
234  key ) = {
235  .name = "key",
236  .description = "Wireless encryption key",
237  .type = &setting_type_string,
238 };
239 
240 /** @} */
241 
242 
243 /* ---------- net_device wrapper ---------- */
244 
245 /**
246  * Open 802.11 device and start association
247  *
248  * @v netdev Wrapping network device
249  * @ret rc Return status code
250  *
251  * This sets up a default conservative set of channels for probing,
252  * and starts the auto-association task unless the @c
253  * NET80211_NO_ASSOC flag is set in the wrapped 802.11 device's @c
254  * state field.
255  */
256 static int net80211_netdev_open ( struct net_device *netdev )
257 {
258  struct net80211_device *dev = netdev->priv;
259  int rc = 0;
260 
261  if ( dev->op == &net80211_null_ops )
262  return -EFAULT;
263 
264  if ( dev->op->open )
265  rc = dev->op->open ( dev );
266 
267  if ( rc < 0 )
268  return rc;
269 
270  if ( ! ( dev->state & NET80211_NO_ASSOC ) )
271  net80211_autoassociate ( dev );
272 
273  return 0;
274 }
275 
276 /**
277  * Close 802.11 device
278  *
279  * @v netdev Wrapping network device.
280  *
281  * If the association task is running, this will stop it.
282  */
283 static void net80211_netdev_close ( struct net_device *netdev )
284 {
285  struct net80211_device *dev = netdev->priv;
286 
287  if ( dev->state & NET80211_WORKING )
288  process_del ( &dev->proc_assoc );
289 
290  /* Send disassociation frame to AP, to be polite */
291  if ( dev->state & NET80211_ASSOCIATED )
293 
294  if ( dev->handshaker && dev->handshaker->stop &&
295  dev->handshaker->started )
296  dev->handshaker->stop ( dev );
297 
298  free ( dev->crypto );
299  free ( dev->handshaker );
300  dev->crypto = NULL;
301  dev->handshaker = NULL;
302 
304  dev->state = 0;
305 
306  if ( dev->op->close )
307  dev->op->close ( dev );
308 }
309 
310 /**
311  * Transmit packet on 802.11 device
312  *
313  * @v netdev Wrapping network device
314  * @v iobuf I/O buffer
315  * @ret rc Return status code
316  *
317  * If encryption is enabled for the currently associated network, the
318  * packet will be encrypted prior to transmission.
319  */
321  struct io_buffer *iobuf )
322 {
323  struct net80211_device *dev = netdev->priv;
324  struct ieee80211_frame *hdr = iobuf->data;
325  int rc = -ENOSYS;
326 
327  if ( dev->crypto && ! ( hdr->fc & IEEE80211_FC_PROTECTED ) &&
328  ( ( hdr->fc & IEEE80211_FC_TYPE ) == IEEE80211_TYPE_DATA ) ) {
329  struct io_buffer *niob = dev->crypto->encrypt ( dev->crypto,
330  iobuf );
331  if ( ! niob )
332  return -ENOMEM; /* only reason encryption could fail */
333 
334  /* Free the non-encrypted iob */
335  netdev_tx_complete ( netdev, iobuf );
336 
337  /* Transmit the encrypted iob; the Protected flag is
338  set, so we won't recurse into here again */
339  netdev_tx ( netdev, niob );
340 
341  /* Don't transmit the freed packet */
342  return 0;
343  }
344 
345  if ( dev->op->transmit )
346  rc = dev->op->transmit ( dev, iobuf );
347 
348  return rc;
349 }
350 
351 /**
352  * Poll 802.11 device for received packets and completed transmissions
353  *
354  * @v netdev Wrapping network device
355  */
356 static void net80211_netdev_poll ( struct net_device *netdev )
357 {
358  struct net80211_device *dev = netdev->priv;
359 
360  if ( dev->op->poll )
361  dev->op->poll ( dev );
362 }
363 
364 /**
365  * Enable or disable interrupts for 802.11 device
366  *
367  * @v netdev Wrapping network device
368  * @v enable Whether to enable interrupts
369  */
370 static void net80211_netdev_irq ( struct net_device *netdev, int enable )
371 {
372  struct net80211_device *dev = netdev->priv;
373 
374  if ( dev->op->irq )
375  dev->op->irq ( dev, enable );
376 }
377 
378 /** Network device operations for a wrapped 802.11 device */
381  .close = net80211_netdev_close,
382  .transmit = net80211_netdev_transmit,
383  .poll = net80211_netdev_poll,
384  .irq = net80211_netdev_irq,
385 };
386 
387 
388 /* ---------- 802.11 link-layer protocol ---------- */
389 
390 /**
391  * Determine whether a transmission rate uses ERP/OFDM
392  *
393  * @v rate Rate in 100 kbps units
394  * @ret is_erp TRUE if the rate is an ERP/OFDM rate
395  *
396  * 802.11b supports rates of 1.0, 2.0, 5.5, and 11.0 Mbps; any other
397  * rate than these on the 2.4GHz spectrum is an ERP (802.11g) rate.
398  */
399 static inline int net80211_rate_is_erp ( u16 rate )
400 {
401  if ( rate == 10 || rate == 20 || rate == 55 || rate == 110 )
402  return 0;
403  return 1;
404 }
405 
406 
407 /**
408  * Calculate one frame's contribution to 802.11 duration field
409  *
410  * @v dev 802.11 device
411  * @v bytes Amount of data to calculate duration for
412  * @ret dur Duration field in microseconds
413  *
414  * To avoid multiple stations attempting to transmit at once, 802.11
415  * provides that every packet shall include a duration field
416  * specifying a length of time for which the wireless medium will be
417  * reserved after it is transmitted. The duration is measured in
418  * microseconds and is calculated with respect to the current
419  * physical-layer parameters of the 802.11 device.
420  *
421  * For an unfragmented data or management frame, or the last fragment
422  * of a fragmented frame, the duration captures only the 10 data bytes
423  * of one ACK; call once with bytes = 10.
424  *
425  * For a fragment of a data or management rame that will be followed
426  * by more fragments, the duration captures an ACK, the following
427  * fragment, and its ACK; add the results of three calls, two with
428  * bytes = 10 and one with bytes set to the next fragment's size.
429  *
430  * For an RTS control frame, the duration captures the responding CTS,
431  * the frame being sent, and its ACK; add the results of three calls,
432  * two with bytes = 10 and one with bytes set to the next frame's size
433  * (assuming unfragmented).
434  *
435  * For a CTS-to-self control frame, the duration captures the frame
436  * being protected and its ACK; add the results of two calls, one with
437  * bytes = 10 and one with bytes set to the next frame's size.
438  *
439  * No other frame types are currently supported by iPXE.
440  */
441 u16 net80211_duration ( struct net80211_device *dev, int bytes, u16 rate )
442 {
443  struct net80211_channel *chan = &dev->channels[dev->channel];
444  u32 kbps = rate * 100;
445 
446  if ( chan->band == NET80211_BAND_5GHZ || net80211_rate_is_erp ( rate ) ) {
447  /* OFDM encoding (802.11a/g) */
448  int bits_per_symbol = ( kbps * 4 ) / 1000; /* 4us/symbol */
449  int bits = 22 + ( bytes << 3 ); /* 22-bit PLCP */
450  int symbols = ( bits + bits_per_symbol - 1 ) / bits_per_symbol;
451 
452  return 16 + 20 + ( symbols * 4 ); /* 16us SIFS, 20us preamble */
453  } else {
454  /* CCK encoding (802.11b) */
455  int phy_time = 144 + 48; /* preamble + PLCP */
456  int bits = bytes << 3;
457  int data_time = ( bits * 1000 + kbps - 1 ) / kbps;
458 
460  phy_time >>= 1;
461 
462  return 10 + phy_time + data_time; /* 10us SIFS */
463  }
464 }
465 
466 /**
467  * Add 802.11 link-layer header
468  *
469  * @v netdev Wrapping network device
470  * @v iobuf I/O buffer
471  * @v ll_dest Link-layer destination address
472  * @v ll_source Link-layer source address
473  * @v net_proto Network-layer protocol, in network byte order
474  * @ret rc Return status code
475  *
476  * This adds both the 802.11 frame header and the 802.2 LLC/SNAP
477  * header used on data packets.
478  *
479  * We also check here for state of the link that would make it invalid
480  * to send a data packet; every data packet must pass through here,
481  * and no non-data packet (e.g. management frame) should.
482  */
483 static int net80211_ll_push ( struct net_device *netdev,
484  struct io_buffer *iobuf, const void *ll_dest,
485  const void *ll_source, uint16_t net_proto )
486 {
487  struct net80211_device *dev = netdev->priv;
488  struct ieee80211_frame *hdr = iob_push ( iobuf,
491  struct ieee80211_llc_snap_header *lhdr =
492  ( void * ) hdr + IEEE80211_TYP_FRAME_HEADER_LEN;
493 
494  /* We can't send data packets if we're not associated. */
495  if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
496  if ( dev->assoc_rc )
497  return dev->assoc_rc;
498  return -ENETUNREACH;
499  }
500 
503 
504  /* We don't send fragmented frames, so duration is the time
505  for an SIFS + 10-byte ACK. */
506  hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] );
507 
508  memcpy ( hdr->addr1, dev->bssid, ETH_ALEN );
509  memcpy ( hdr->addr2, ll_source, ETH_ALEN );
510  memcpy ( hdr->addr3, ll_dest, ETH_ALEN );
511 
512  hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 );
513 
514  lhdr->dsap = IEEE80211_LLC_DSAP;
515  lhdr->ssap = IEEE80211_LLC_SSAP;
516  lhdr->ctrl = IEEE80211_LLC_CTRL;
517  memset ( lhdr->oui, 0x00, 3 );
518  lhdr->ethertype = net_proto;
519 
520  return 0;
521 }
522 
523 /**
524  * Remove 802.11 link-layer header
525  *
526  * @v netdev Wrapping network device
527  * @v iobuf I/O buffer
528  * @ret ll_dest Link-layer destination address
529  * @ret ll_source Link-layer source
530  * @ret net_proto Network-layer protocol, in network byte order
531  * @ret flags Packet flags
532  * @ret rc Return status code
533  *
534  * This expects and removes both the 802.11 frame header and the 802.2
535  * LLC/SNAP header that are used on data packets.
536  */
538  struct io_buffer *iobuf,
539  const void **ll_dest, const void **ll_source,
540  uint16_t * net_proto, unsigned int *flags )
541 {
542  struct ieee80211_frame *hdr = iobuf->data;
543  struct ieee80211_llc_snap_header *lhdr =
544  ( void * ) hdr + IEEE80211_TYP_FRAME_HEADER_LEN;
545 
546  /* Bunch of sanity checks */
547  if ( iob_len ( iobuf ) < IEEE80211_TYP_FRAME_HEADER_LEN +
549  DBGC ( netdev->priv, "802.11 %p packet too short (%zd bytes)\n",
550  netdev->priv, iob_len ( iobuf ) );
551  return -EINVAL_PKT_TOO_SHORT;
552  }
553 
554  if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION ) {
555  DBGC ( netdev->priv, "802.11 %p packet invalid version %04x\n",
557  return -EINVAL_PKT_VERSION;
558  }
559 
560  if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_DATA ||
562  DBGC ( netdev->priv, "802.11 %p packet not data/data (fc=%04x)\n",
563  netdev->priv, hdr->fc );
564  return -EINVAL_PKT_NOT_DATA;
565  }
566 
567  if ( ( hdr->fc & ( IEEE80211_FC_TODS | IEEE80211_FC_FROMDS ) ) !=
569  DBGC ( netdev->priv, "802.11 %p packet not from DS (fc=%04x)\n",
570  netdev->priv, hdr->fc );
571  return -EINVAL_PKT_NOT_FROMDS;
572  }
573 
574  if ( lhdr->dsap != IEEE80211_LLC_DSAP || lhdr->ssap != IEEE80211_LLC_SSAP ||
575  lhdr->ctrl != IEEE80211_LLC_CTRL || lhdr->oui[0] || lhdr->oui[1] ||
576  lhdr->oui[2] ) {
577  DBGC ( netdev->priv, "802.11 %p LLC header is not plain EtherType "
578  "encapsulator: %02x->%02x [%02x] %02x:%02x:%02x %04x\n",
579  netdev->priv, lhdr->dsap, lhdr->ssap, lhdr->ctrl,
580  lhdr->oui[0], lhdr->oui[1], lhdr->oui[2], lhdr->ethertype );
581  return -EINVAL_PKT_LLC_HEADER;
582  }
583 
584  iob_pull ( iobuf, sizeof ( *hdr ) + sizeof ( *lhdr ) );
585 
586  *ll_dest = hdr->addr1;
587  *ll_source = hdr->addr3;
588  *net_proto = lhdr->ethertype;
589  *flags = ( ( is_multicast_ether_addr ( hdr->addr1 ) ?
590  LL_MULTICAST : 0 ) |
591  ( is_broadcast_ether_addr ( hdr->addr1 ) ?
592  LL_BROADCAST : 0 ) );
593  return 0;
594 }
595 
596 /** 802.11 link-layer protocol */
597 static struct ll_protocol net80211_ll_protocol __ll_protocol = {
598  .name = "802.11",
599  .push = net80211_ll_push,
600  .pull = net80211_ll_pull,
601  .init_addr = eth_init_addr,
602  .ntoa = eth_ntoa,
603  .mc_hash = eth_mc_hash,
604  .eth_addr = eth_eth_addr,
605  .eui64 = eth_eui64,
606  .ll_proto = htons ( ARPHRD_ETHER ), /* "encapsulated Ethernet" */
607  .hw_addr_len = ETH_ALEN,
608  .ll_addr_len = ETH_ALEN,
609  .ll_header_len = IEEE80211_TYP_FRAME_HEADER_LEN +
611 };
612 
613 
614 /* ---------- 802.11 network management API ---------- */
615 
616 /**
617  * Get 802.11 device from wrapping network device
618  *
619  * @v netdev Wrapping network device
620  * @ret dev 802.11 device wrapped by network device, or NULL
621  *
622  * Returns NULL if the network device does not wrap an 802.11 device.
623  */
625 {
626  struct net80211_device *dev;
627 
629  if ( netdev->priv == dev )
630  return netdev->priv;
631  }
632 
633  return NULL;
634 }
635 
636 /**
637  * Set state of 802.11 device keeping management frames
638  *
639  * @v dev 802.11 device
640  * @v enable Whether to keep management frames
641  * @ret oldenab Whether management frames were enabled before this call
642  *
643  * If enable is TRUE, beacon, probe, and action frames will be kept
644  * and may be retrieved by calling net80211_mgmt_dequeue().
645  */
646 int net80211_keep_mgmt ( struct net80211_device *dev, int enable )
647 {
648  int oldenab = dev->keep_mgmt;
649 
650  dev->keep_mgmt = enable;
651  return oldenab;
652 }
653 
654 /**
655  * Get 802.11 management frame
656  *
657  * @v dev 802.11 device
658  * @ret signal Signal strength of returned management frame
659  * @ret iob I/O buffer, or NULL if no management frame is queued
660  *
661  * Frames will only be returned by this function if
662  * net80211_keep_mgmt() has been previously called with enable set to
663  * TRUE.
664  *
665  * The calling function takes ownership of the returned I/O buffer.
666  */
668  int *signal )
669 {
670  struct io_buffer *iobuf;
671  struct net80211_rx_info *rxi;
672 
673  list_for_each_entry ( rxi, &dev->mgmt_info_queue, list ) {
674  list_del ( &rxi->list );
675  if ( signal )
676  *signal = rxi->signal;
677  free ( rxi );
678 
679  assert ( ! list_empty ( &dev->mgmt_queue ) );
680  iobuf = list_first_entry ( &dev->mgmt_queue, struct io_buffer,
681  list );
682  list_del ( &iobuf->list );
683  return iobuf;
684  }
685 
686  return NULL;
687 }
688 
689 /**
690  * Transmit 802.11 management frame
691  *
692  * @v dev 802.11 device
693  * @v fc Frame Control flags for management frame
694  * @v dest Destination access point
695  * @v iob I/O buffer
696  * @ret rc Return status code
697  *
698  * The @a fc argument must contain at least an IEEE 802.11 management
699  * subtype number (e.g. IEEE80211_STYPE_PROBE_REQ). If it contains
700  * IEEE80211_FC_PROTECTED, the frame will be encrypted prior to
701  * transmission.
702  *
703  * It is required that @a iob have at least 24 bytes of headroom
704  * reserved before its data start.
705  */
706 int net80211_tx_mgmt ( struct net80211_device *dev, u16 fc, u8 dest[6],
707  struct io_buffer *iob )
708 {
709  struct ieee80211_frame *hdr = iob_push ( iob,
711 
713  ( fc & ~IEEE80211_FC_PROTECTED );
714  hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] );
715  hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 );
716 
717  memcpy ( hdr->addr1, dest, ETH_ALEN ); /* DA = RA */
718  memcpy ( hdr->addr2, dev->netdev->ll_addr, ETH_ALEN ); /* SA = TA */
719  memcpy ( hdr->addr3, dest, ETH_ALEN ); /* BSSID */
720 
721  if ( fc & IEEE80211_FC_PROTECTED ) {
722  if ( ! dev->crypto )
723  return -EINVAL_CRYPTO_REQUEST;
724 
725  struct io_buffer *eiob = dev->crypto->encrypt ( dev->crypto,
726  iob );
727  free_iob ( iob );
728  iob = eiob;
729  }
730 
731  return netdev_tx ( dev->netdev, iob );
732 }
733 
734 
735 /* ---------- Driver API ---------- */
736 
737 /** 802.11 association process descriptor */
739  PROC_DESC ( struct net80211_device, proc_assoc,
741 
742 /**
743  * Allocate 802.11 device
744  *
745  * @v priv_size Size of driver-private allocation area
746  * @ret dev Newly allocated 802.11 device
747  *
748  * This function allocates a net_device with space in its private area
749  * for both the net80211_device it will wrap and the driver-private
750  * data space requested. It initializes the link-layer-specific parts
751  * of the net_device, and links the net80211_device to the net_device
752  * appropriately.
753  */
754 struct net80211_device * net80211_alloc ( size_t priv_size )
755 {
756  struct net80211_device *dev;
757  struct net_device *netdev =
758  alloc_netdev ( sizeof ( *dev ) + priv_size );
759 
760  if ( ! netdev )
761  return NULL;
762 
763  netdev->ll_protocol = &net80211_ll_protocol;
767 
768  dev = netdev->priv;
769  dev->netdev = netdev;
770  dev->priv = ( u8 * ) dev + sizeof ( *dev );
771  dev->op = &net80211_null_ops;
772 
774  &netdev->refcnt );
775  INIT_LIST_HEAD ( &dev->mgmt_queue );
776  INIT_LIST_HEAD ( &dev->mgmt_info_queue );
777 
778  return dev;
779 }
780 
781 /**
782  * Register 802.11 device with network stack
783  *
784  * @v dev 802.11 device
785  * @v ops 802.11 device operations
786  * @v hw 802.11 hardware information
787  *
788  * This also registers the wrapping net_device with the higher network
789  * layers.
790  */
792  struct net80211_device_operations *ops,
793  struct net80211_hw_info *hw )
794 {
795  dev->op = ops;
796  dev->hw = malloc ( sizeof ( *hw ) );
797  if ( ! dev->hw )
798  return -ENOMEM;
799 
800  memcpy ( dev->hw, hw, sizeof ( *hw ) );
801  memcpy ( dev->netdev->hw_addr, hw->hwaddr, ETH_ALEN );
802 
803  /* Set some sensible channel defaults for driver's open() function */
804  memcpy ( dev->channels, dev->hw->channels,
805  NET80211_MAX_CHANNELS * sizeof ( dev->channels[0] ) );
806  dev->channel = 0;
807 
808  /* Mark device as not supporting interrupts, if applicable */
809  if ( ! ops->irq )
810  dev->netdev->state |= NETDEV_IRQ_UNSUPPORTED;
811 
812  list_add_tail ( &dev->list, &net80211_devices );
813  return register_netdev ( dev->netdev );
814 }
815 
816 /**
817  * Unregister 802.11 device from network stack
818  *
819  * @v dev 802.11 device
820  *
821  * After this call, the device operations are cleared so that they
822  * will not be called.
823  */
825 {
826  unregister_netdev ( dev->netdev );
827  list_del ( &dev->list );
828  dev->op = &net80211_null_ops;
829 }
830 
831 /**
832  * Free 802.11 device
833  *
834  * @v dev 802.11 device
835  *
836  * The device should be unregistered before this function is called.
837  */
839 {
840  free ( dev->hw );
841  rc80211_free ( dev->rctl );
842  netdev_nullify ( dev->netdev );
843  netdev_put ( dev->netdev );
844 }
845 
846 
847 /* ---------- 802.11 network management workhorse code ---------- */
848 
849 /**
850  * Set state of 802.11 device
851  *
852  * @v dev 802.11 device
853  * @v clear Bitmask of flags to clear
854  * @v set Bitmask of flags to set
855  * @v status Status or reason code for most recent operation
856  *
857  * If @a status represents a reason code, it should be OR'ed with
858  * NET80211_IS_REASON.
859  *
860  * Clearing authentication also clears association; clearing
861  * association also clears security handshaking state. Clearing
862  * association removes the link-up flag from the wrapping net_device,
863  * but setting it does not automatically set the flag; that is left to
864  * the judgment of higher-level code.
865  */
866 static inline void net80211_set_state ( struct net80211_device *dev,
867  short clear, short set,
868  u16 status )
869 {
870  /* The conditions in this function are deliberately formulated
871  to be decidable at compile-time in most cases. Since clear
872  and set are generally passed as constants, the body of this
873  function can be reduced down to a few statements by the
874  compiler. */
875 
876  const int statmsk = NET80211_STATUS_MASK | NET80211_IS_REASON;
877 
878  if ( clear & NET80211_PROBED )
879  clear |= NET80211_AUTHENTICATED;
880 
881  if ( clear & NET80211_AUTHENTICATED )
882  clear |= NET80211_ASSOCIATED;
883 
884  if ( clear & NET80211_ASSOCIATED )
885  clear |= NET80211_CRYPTO_SYNCED;
886 
887  dev->state = ( dev->state & ~clear ) | set;
888  dev->state = ( dev->state & ~statmsk ) | ( status & statmsk );
889 
890  if ( clear & NET80211_ASSOCIATED )
891  netdev_link_down ( dev->netdev );
892 
893  if ( ( clear | set ) & NET80211_ASSOCIATED )
894  dev->op->config ( dev, NET80211_CFG_ASSOC );
895 
896  if ( status != 0 ) {
897  if ( status & NET80211_IS_REASON )
898  dev->assoc_rc = -E80211_REASON ( status );
899  else
900  dev->assoc_rc = -E80211_STATUS ( status );
901  netdev_link_err ( dev->netdev, dev->assoc_rc );
902  }
903 }
904 
905 /**
906  * Add channels to 802.11 device
907  *
908  * @v dev 802.11 device
909  * @v start First channel number to add
910  * @v len Number of channels to add
911  * @v txpower TX power (dBm) to allow on added channels
912  *
913  * To replace the current list of channels instead of adding to it,
914  * set the nr_channels field of the 802.11 device to 0 before calling
915  * this function.
916  */
917 static void net80211_add_channels ( struct net80211_device *dev, int start,
918  int len, int txpower )
919 {
920  int i, chan = start;
921 
922  for ( i = dev->nr_channels; len-- && i < NET80211_MAX_CHANNELS; i++ ) {
923  dev->channels[i].channel_nr = chan;
924  dev->channels[i].maxpower = txpower;
925  dev->channels[i].hw_value = 0;
926 
927  if ( chan >= 1 && chan <= 14 ) {
928  dev->channels[i].band = NET80211_BAND_2GHZ;
929  if ( chan == 14 )
930  dev->channels[i].center_freq = 2484;
931  else
932  dev->channels[i].center_freq = 2407 + 5 * chan;
933  chan++;
934  } else {
935  dev->channels[i].band = NET80211_BAND_5GHZ;
936  dev->channels[i].center_freq = 5000 + 5 * chan;
937  chan += 4;
938  }
939  }
940 
941  dev->nr_channels = i;
942 }
943 
944 /**
945  * Filter 802.11 device channels for hardware capabilities
946  *
947  * @v dev 802.11 device
948  *
949  * Hardware may support fewer channels than regulatory restrictions
950  * allow; this function filters out channels in dev->channels that are
951  * not supported by the hardware list in dev->hwinfo. It also copies
952  * over the net80211_channel::hw_value and limits maximum TX power
953  * appropriately.
954  *
955  * Channels are matched based on center frequency, ignoring band and
956  * channel number.
957  *
958  * If the driver specifies no supported channels, the effect will be
959  * as though all were supported.
960  */
962 {
963  int delta = 0, i = 0;
964  int old_freq = dev->channels[dev->channel].center_freq;
965  struct net80211_channel *chan, *hwchan;
966 
967  if ( ! dev->hw->nr_channels )
968  return;
969 
970  dev->channel = 0;
971  for ( chan = dev->channels; chan < dev->channels + dev->nr_channels;
972  chan++, i++ ) {
973  int ok = 0;
974  for ( hwchan = dev->hw->channels;
975  hwchan < dev->hw->channels + dev->hw->nr_channels;
976  hwchan++ ) {
977  if ( hwchan->center_freq == chan->center_freq ) {
978  ok = 1;
979  break;
980  }
981  }
982 
983  if ( ! ok )
984  delta++;
985  else {
986  chan->hw_value = hwchan->hw_value;
987  if ( hwchan->maxpower != 0 &&
988  chan->maxpower > hwchan->maxpower )
989  chan->maxpower = hwchan->maxpower;
990  if ( old_freq == chan->center_freq )
991  dev->channel = i - delta;
992  if ( delta )
993  chan[-delta] = *chan;
994  }
995  }
996 
997  dev->nr_channels -= delta;
998 
999  if ( dev->channels[dev->channel].center_freq != old_freq )
1000  dev->op->config ( dev, NET80211_CFG_CHANNEL );
1001 }
1002 
1003 /**
1004  * Update 802.11 device state to reflect received capabilities field
1005  *
1006  * @v dev 802.11 device
1007  * @v capab Capabilities field in beacon, probe, or association frame
1008  * @ret rc Return status code
1009  */
1010 static int net80211_process_capab ( struct net80211_device *dev,
1011  u16 capab )
1012 {
1013  u16 old_phy = dev->phy_flags;
1014 
1015  if ( ( capab & ( IEEE80211_CAPAB_MANAGED | IEEE80211_CAPAB_ADHOC ) ) !=
1017  DBGC ( dev, "802.11 %p cannot handle IBSS network\n", dev );
1018  return -ENOSYS;
1019  }
1020 
1023 
1024  if ( capab & IEEE80211_CAPAB_SHORT_PMBL )
1026 
1027  if ( capab & IEEE80211_CAPAB_SHORT_SLOT )
1029 
1030  if ( old_phy != dev->phy_flags )
1031  dev->op->config ( dev, NET80211_CFG_PHY_PARAMS );
1032 
1033  return 0;
1034 }
1035 
1036 /**
1037  * Update 802.11 device state to reflect received information elements
1038  *
1039  * @v dev 802.11 device
1040  * @v ie Pointer to first information element
1041  * @v ie_end Pointer to tail of packet I/O buffer
1042  * @ret rc Return status code
1043  */
1044 static int net80211_process_ie ( struct net80211_device *dev,
1045  union ieee80211_ie *ie, void *ie_end )
1046 {
1047  u16 old_rate = dev->rates[dev->rate];
1048  u16 old_phy = dev->phy_flags;
1049  int have_rates = 0, i;
1050  int ds_channel = 0;
1051  int changed = 0;
1052  int band = dev->channels[dev->channel].band;
1053 
1054  if ( ! ieee80211_ie_bound ( ie, ie_end ) )
1055  return 0;
1056 
1057  for ( ; ie; ie = ieee80211_next_ie ( ie, ie_end ) ) {
1058  switch ( ie->id ) {
1059  case IEEE80211_IE_SSID:
1060  if ( ie->len <= 32 ) {
1061  memcpy ( dev->essid, ie->ssid, ie->len );
1062  dev->essid[ie->len] = 0;
1063  }
1064  break;
1065 
1066  case IEEE80211_IE_RATES:
1068  if ( ! have_rates ) {
1069  dev->nr_rates = 0;
1070  dev->basic_rates = 0;
1071  have_rates = 1;
1072  }
1073  for ( i = 0; i < ie->len &&
1074  dev->nr_rates < NET80211_MAX_RATES; i++ ) {
1075  u8 rid = ie->rates[i];
1076  u16 rate = ( rid & 0x7f ) * 5;
1077 
1078  if ( rid & 0x80 )
1079  dev->basic_rates |=
1080  ( 1 << dev->nr_rates );
1081 
1082  dev->rates[dev->nr_rates++] = rate;
1083  }
1084 
1085  break;
1086 
1087  case IEEE80211_IE_DS_PARAM:
1088  if ( dev->channel < dev->nr_channels && ds_channel ==
1089  dev->channels[dev->channel].channel_nr )
1090  break;
1091  ds_channel = ie->ds_param.current_channel;
1092  net80211_change_channel ( dev, ds_channel );
1093  break;
1094 
1095  case IEEE80211_IE_COUNTRY:
1096  dev->nr_channels = 0;
1097 
1098  DBGC ( dev, "802.11 %p setting country regulations "
1099  "for %c%c\n", dev, ie->country.name[0],
1100  ie->country.name[1] );
1101  for ( i = 0; i < ( ie->len - 3 ) / 3; i++ ) {
1102  union ieee80211_ie_country_triplet *t =
1103  &ie->country.triplet[i];
1104  if ( t->first > 200 ) {
1105  DBGC ( dev, "802.11 %p ignoring regulatory "
1106  "extension information\n", dev );
1107  } else {
1108  net80211_add_channels ( dev,
1109  t->band.first_channel,
1110  t->band.nr_channels,
1111  t->band.max_txpower );
1112  }
1113  }
1115  break;
1116 
1117  case IEEE80211_IE_ERP_INFO:
1122  if ( ! ( ie->erp_info & IEEE80211_ERP_BARKER_LONG ) )
1124  break;
1125  }
1126  }
1127 
1128  if ( have_rates ) {
1129  /* Allow only those rates that are also supported by
1130  the hardware. */
1131  int delta = 0, j;
1132 
1133  dev->rate = 0;
1134  for ( i = 0; i < dev->nr_rates; i++ ) {
1135  int ok = 0;
1136  for ( j = 0; j < dev->hw->nr_rates[band]; j++ ) {
1137  if ( dev->hw->rates[band][j] == dev->rates[i] ){
1138  ok = 1;
1139  break;
1140  }
1141  }
1142 
1143  if ( ! ok )
1144  delta++;
1145  else {
1146  dev->rates[i - delta] = dev->rates[i];
1147  if ( old_rate == dev->rates[i] )
1148  dev->rate = i - delta;
1149  }
1150  }
1151 
1152  dev->nr_rates -= delta;
1153 
1154  /* Sort available rates - sorted subclumps tend to already
1155  exist, so insertion sort works well. */
1156  for ( i = 1; i < dev->nr_rates; i++ ) {
1157  u16 rate = dev->rates[i];
1158  u32 tmp, br, mask;
1159 
1160  for ( j = i - 1; j >= 0 && dev->rates[j] >= rate; j-- )
1161  dev->rates[j + 1] = dev->rates[j];
1162  dev->rates[j + 1] = rate;
1163 
1164  /* Adjust basic_rates to match by rotating the
1165  bits from bit j+1 to bit i left one position. */
1166  mask = ( ( 1 << i ) - 1 ) & ~( ( 1 << ( j + 1 ) ) - 1 );
1167  br = dev->basic_rates;
1168  tmp = br & ( 1 << i );
1169  br = ( br & ~( mask | tmp ) ) | ( ( br & mask ) << 1 );
1170  br |= ( tmp >> ( i - j - 1 ) );
1171  dev->basic_rates = br;
1172  }
1173 
1174  net80211_set_rtscts_rate ( dev );
1175 
1176  if ( dev->rates[dev->rate] != old_rate )
1177  changed |= NET80211_CFG_RATE;
1178  }
1179 
1180  if ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE )
1182  if ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT )
1184 
1185  if ( old_phy != dev->phy_flags )
1186  changed |= NET80211_CFG_PHY_PARAMS;
1187 
1188  if ( changed )
1189  dev->op->config ( dev, changed );
1190 
1191  return 0;
1192 }
1193 
1194 /**
1195  * Create information elements for outgoing probe or association packet
1196  *
1197  * @v dev 802.11 device
1198  * @v ie Pointer to start of information element area
1199  * @ret next_ie Pointer to first byte after added information elements
1200  */
1201 static union ieee80211_ie *
1203  union ieee80211_ie *ie )
1204 {
1205  int i;
1206 
1207  ie->id = IEEE80211_IE_SSID;
1208  ie->len = strlen ( dev->essid );
1209  memcpy ( ie->ssid, dev->essid, ie->len );
1210 
1211  ie = ieee80211_next_ie ( ie, NULL );
1212 
1213  ie->id = IEEE80211_IE_RATES;
1214  ie->len = dev->nr_rates;
1215  if ( ie->len > 8 )
1216  ie->len = 8;
1217 
1218  for ( i = 0; i < ie->len; i++ ) {
1219  ie->rates[i] = dev->rates[i] / 5;
1220  if ( dev->basic_rates & ( 1 << i ) )
1221  ie->rates[i] |= 0x80;
1222  }
1223 
1224  ie = ieee80211_next_ie ( ie, NULL );
1225 
1226  if ( dev->rsn_ie && dev->rsn_ie->id == IEEE80211_IE_RSN ) {
1227  memcpy ( ie, dev->rsn_ie, dev->rsn_ie->len + 2 );
1228  ie = ieee80211_next_ie ( ie, NULL );
1229  }
1230 
1231  if ( dev->nr_rates > 8 ) {
1232  /* 802.11 requires we use an Extended Basic Rates IE
1233  for the rates beyond the eighth. */
1234 
1235  ie->id = IEEE80211_IE_EXT_RATES;
1236  ie->len = dev->nr_rates - 8;
1237 
1238  for ( ; i < dev->nr_rates; i++ ) {
1239  ie->rates[i - 8] = dev->rates[i] / 5;
1240  if ( dev->basic_rates & ( 1 << i ) )
1241  ie->rates[i - 8] |= 0x80;
1242  }
1243 
1244  ie = ieee80211_next_ie ( ie, NULL );
1245  }
1246 
1247  if ( dev->rsn_ie && dev->rsn_ie->id == IEEE80211_IE_VENDOR ) {
1248  memcpy ( ie, dev->rsn_ie, dev->rsn_ie->len + 2 );
1249  ie = ieee80211_next_ie ( ie, NULL );
1250  }
1251 
1252  return ie;
1253 }
1254 
1255 /** Seconds to wait after finding a network, to possibly find better APs for it
1256  *
1257  * This is used when a specific SSID to scan for is specified.
1258  */
1259 #define NET80211_PROBE_GATHER 1
1260 
1261 /** Seconds to wait after finding a network, to possibly find other networks
1262  *
1263  * This is used when an empty SSID is specified, to scan for all
1264  * networks.
1265  */
1266 #define NET80211_PROBE_GATHER_ALL 2
1267 
1268 /** Seconds to allow a probe to take if no network has been found */
1269 #define NET80211_PROBE_TIMEOUT 6
1270 
1271 /**
1272  * Begin probe of 802.11 networks
1273  *
1274  * @v dev 802.11 device
1275  * @v essid SSID to probe for, or "" to accept any (may not be NULL)
1276  * @v active Whether to use active scanning
1277  * @ret ctx Probe context
1278  *
1279  * Active scanning may only be used on channels 1-11 in the 2.4GHz
1280  * band, due to iPXE's lack of a complete regulatory database. If
1281  * active scanning is used, probe packets will be sent on each
1282  * channel; this can allow association with hidden-SSID networks if
1283  * the SSID is properly specified.
1284  *
1285  * A @c NULL return indicates an out-of-memory condition.
1286  *
1287  * The returned context must be periodically passed to
1288  * net80211_probe_step() until that function returns zero.
1289  */
1291  const char *essid,
1292  int active )
1293 {
1294  struct net80211_probe_ctx *ctx = zalloc ( sizeof ( *ctx ) );
1295 
1296  if ( ! ctx )
1297  return NULL;
1298 
1299  assert ( netdev_is_open ( dev->netdev ) );
1300 
1301  ctx->dev = dev;
1302  ctx->old_keep_mgmt = net80211_keep_mgmt ( dev, 1 );
1303  ctx->essid = essid;
1304  if ( dev->essid != ctx->essid )
1305  strcpy ( dev->essid, ctx->essid );
1306 
1307  if ( active ) {
1308  struct ieee80211_probe_req *probe_req;
1309  union ieee80211_ie *ie;
1310 
1311  ctx->probe = alloc_iob ( 128 );
1313  probe_req = ctx->probe->data;
1314 
1315  ie = net80211_marshal_request_info ( dev,
1316  probe_req->info_element );
1317 
1318  iob_put ( ctx->probe, ( void * ) ie - ctx->probe->data );
1319  }
1320 
1321  ctx->ticks_start = currticks();
1322  ctx->ticks_beacon = 0;
1323  ctx->ticks_channel = currticks();
1324  ctx->hop_time = TICKS_PER_SEC / ( active ? 2 : 6 );
1325 
1326  /*
1327  * Channels on 2.4GHz overlap, and the most commonly used
1328  * are 1, 6, and 11. We'll get a result faster if we check
1329  * every 5 channels, but in order to hit all of them the
1330  * number of channels must be relatively prime to 5. If it's
1331  * not, tweak the hop.
1332  */
1333  ctx->hop_step = 5;
1334  while ( dev->nr_channels % ctx->hop_step == 0 && ctx->hop_step > 1 )
1335  ctx->hop_step--;
1336 
1337  ctx->beacons = malloc ( sizeof ( *ctx->beacons ) );
1338  INIT_LIST_HEAD ( ctx->beacons );
1339 
1340  dev->channel = 0;
1341  dev->op->config ( dev, NET80211_CFG_CHANNEL );
1342 
1343  return ctx;
1344 }
1345 
1346 /**
1347  * Continue probe of 802.11 networks
1348  *
1349  * @v ctx Probe context returned by net80211_probe_start()
1350  * @ret rc Probe status
1351  *
1352  * The return code will be 0 if the probe is still going on (and this
1353  * function should be called again), a positive number if the probe
1354  * completed successfully, or a negative error code if the probe
1355  * failed for that reason.
1356  *
1357  * Whether the probe succeeded or failed, you must call
1358  * net80211_probe_finish_all() or net80211_probe_finish_best()
1359  * (depending on whether you want information on all networks or just
1360  * the best-signal one) in order to release the probe context. A
1361  * failed probe may still have acquired some valid data.
1362  */
1364 {
1365  struct net80211_device *dev = ctx->dev;
1366  u32 start_timeout = NET80211_PROBE_TIMEOUT * TICKS_PER_SEC;
1367  u32 gather_timeout = TICKS_PER_SEC;
1368  u32 now = currticks();
1369  struct io_buffer *iob;
1370  int signal;
1371  int rc;
1372  char ssid[IEEE80211_MAX_SSID_LEN + 1];
1373 
1374  gather_timeout *= ( ctx->essid[0] ? NET80211_PROBE_GATHER :
1376 
1377  /* Time out if necessary */
1378  if ( now >= ctx->ticks_start + start_timeout )
1379  return list_empty ( ctx->beacons ) ? -ETIMEDOUT : +1;
1380 
1381  if ( ctx->ticks_beacon > 0 && now >= ctx->ticks_start + gather_timeout )
1382  return +1;
1383 
1384  /* Change channels if necessary */
1385  if ( now >= ctx->ticks_channel + ctx->hop_time ) {
1386  dev->channel = ( dev->channel + ctx->hop_step )
1387  % dev->nr_channels;
1388  dev->op->config ( dev, NET80211_CFG_CHANNEL );
1389  udelay ( dev->hw->channel_change_time );
1390 
1391  ctx->ticks_channel = now;
1392 
1393  if ( ctx->probe ) {
1394  struct io_buffer *siob = ctx->probe; /* to send */
1395 
1396  /* make a copy for future use */
1397  iob = alloc_iob ( siob->tail - siob->head );
1398  iob_reserve ( iob, iob_headroom ( siob ) );
1399  memcpy ( iob_put ( iob, iob_len ( siob ) ),
1400  siob->data, iob_len ( siob ) );
1401 
1402  ctx->probe = iob;
1404  eth_broadcast,
1405  iob_disown ( siob ) );
1406  if ( rc ) {
1407  DBGC ( dev, "802.11 %p send probe failed: "
1408  "%s\n", dev, strerror ( rc ) );
1409  return rc;
1410  }
1411  }
1412  }
1413 
1414  /* Check for new management packets */
1415  while ( ( iob = net80211_mgmt_dequeue ( dev, &signal ) ) != NULL ) {
1416  struct ieee80211_frame *hdr;
1417  struct ieee80211_beacon *beacon;
1418  union ieee80211_ie *ie;
1419  struct net80211_wlan *wlan;
1420  u16 type;
1421 
1422  hdr = iob->data;
1423  type = hdr->fc & IEEE80211_FC_SUBTYPE;
1424  beacon = ( struct ieee80211_beacon * ) hdr->data;
1425 
1428  DBGC2 ( dev, "802.11 %p probe: non-beacon\n", dev );
1429  goto drop;
1430  }
1431 
1432  if ( ( void * ) beacon->info_element >= iob->tail ) {
1433  DBGC ( dev, "802.11 %p probe: beacon with no IEs\n",
1434  dev );
1435  goto drop;
1436  }
1437 
1438  ie = beacon->info_element;
1439 
1440  if ( ! ieee80211_ie_bound ( ie, iob->tail ) )
1441  ie = NULL;
1442 
1443  while ( ie && ie->id != IEEE80211_IE_SSID )
1444  ie = ieee80211_next_ie ( ie, iob->tail );
1445 
1446  if ( ! ie ) {
1447  DBGC ( dev, "802.11 %p probe: beacon with no SSID\n",
1448  dev );
1449  goto drop;
1450  }
1451 
1452  memcpy ( ssid, ie->ssid, ie->len );
1453  ssid[ie->len] = 0;
1454 
1455  if ( ctx->essid[0] && strcmp ( ctx->essid, ssid ) != 0 ) {
1456  DBGC2 ( dev, "802.11 %p probe: beacon with wrong SSID "
1457  "(%s)\n", dev, ssid );
1458  goto drop;
1459  }
1460 
1461  /* See if we've got an entry for this network */
1462  list_for_each_entry ( wlan, ctx->beacons, list ) {
1463  if ( strcmp ( wlan->essid, ssid ) != 0 )
1464  continue;
1465 
1466  if ( signal < wlan->signal ) {
1467  DBGC2 ( dev, "802.11 %p probe: beacon for %s "
1468  "(%s) with weaker signal %d\n", dev,
1469  ssid, eth_ntoa ( hdr->addr3 ), signal );
1470  goto drop;
1471  }
1472 
1473  goto fill;
1474  }
1475 
1476  /* No entry yet - make one */
1477  wlan = zalloc ( sizeof ( *wlan ) );
1478  strcpy ( wlan->essid, ssid );
1479  list_add_tail ( &wlan->list, ctx->beacons );
1480 
1481  /* Whether we're using an old entry or a new one, fill
1482  it with new data. */
1483  fill:
1484  memcpy ( wlan->bssid, hdr->addr3, ETH_ALEN );
1485  wlan->signal = signal;
1486  wlan->channel = dev->channels[dev->channel].channel_nr;
1487 
1488  /* Copy this I/O buffer into a new wlan->beacon; the
1489  * iob we've got probably came from the device driver
1490  * and may have the full 2.4k allocation, which we
1491  * don't want to keep around wasting memory.
1492  */
1493  free_iob ( wlan->beacon );
1494  wlan->beacon = alloc_iob ( iob_len ( iob ) );
1495  memcpy ( iob_put ( wlan->beacon, iob_len ( iob ) ),
1496  iob->data, iob_len ( iob ) );
1497 
1498  if ( ( rc = sec80211_detect ( wlan->beacon, &wlan->handshaking,
1499  &wlan->crypto ) ) == -ENOTSUP ) {
1500  struct ieee80211_beacon *beacon =
1501  ( struct ieee80211_beacon * ) hdr->data;
1502 
1503  if ( beacon->capability & IEEE80211_CAPAB_PRIVACY ) {
1504  DBG ( "802.11 %p probe: secured network %s but "
1505  "encryption support not compiled in\n",
1506  dev, wlan->essid );
1509  } else {
1511  wlan->crypto = NET80211_CRYPT_NONE;
1512  }
1513  } else if ( rc != 0 ) {
1514  DBGC ( dev, "802.11 %p probe warning: network "
1515  "%s with unidentifiable security "
1516  "settings: %s\n", dev, wlan->essid,
1517  strerror ( rc ) );
1518  }
1519 
1520  ctx->ticks_beacon = now;
1521 
1522  DBGC2 ( dev, "802.11 %p probe: good beacon for %s (%s)\n",
1523  dev, wlan->essid, eth_ntoa ( wlan->bssid ) );
1524 
1525  drop:
1526  free_iob ( iob );
1527  }
1528 
1529  return 0;
1530 }
1531 
1532 
1533 /**
1534  * Finish probe of 802.11 networks, returning best-signal network found
1535  *
1536  * @v ctx Probe context
1537  * @ret wlan Best-signal network found, or @c NULL if none were found
1538  *
1539  * If net80211_probe_start() was called with a particular SSID
1540  * parameter as filter, only a network with that SSID (matching
1541  * case-sensitively) can be returned from this function.
1542  */
1543 struct net80211_wlan *
1545 {
1546  struct net80211_wlan *best = NULL, *wlan;
1547 
1548  if ( ! ctx )
1549  return NULL;
1550 
1551  list_for_each_entry ( wlan, ctx->beacons, list ) {
1552  if ( ! best || best->signal < wlan->signal )
1553  best = wlan;
1554  }
1555 
1556  if ( best )
1557  list_del ( &best->list );
1558  else
1559  DBGC ( ctx->dev, "802.11 %p probe: found nothing for '%s'\n",
1560  ctx->dev, ctx->essid );
1561 
1562  net80211_free_wlanlist ( ctx->beacons );
1563 
1564  net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
1565 
1566  if ( ctx->probe )
1567  free_iob ( ctx->probe );
1568 
1569  free ( ctx );
1570 
1571  return best;
1572 }
1573 
1574 
1575 /**
1576  * Finish probe of 802.11 networks, returning all networks found
1577  *
1578  * @v ctx Probe context
1579  * @ret list List of net80211_wlan detailing networks found
1580  *
1581  * If net80211_probe_start() was called with a particular SSID
1582  * parameter as filter, this will always return either an empty or a
1583  * one-element list.
1584  */
1586 {
1587  struct list_head *beacons = ctx->beacons;
1588 
1589  net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
1590 
1591  if ( ctx->probe )
1592  free_iob ( ctx->probe );
1593 
1594  free ( ctx );
1595 
1596  return beacons;
1597 }
1598 
1599 
1600 /**
1601  * Free WLAN structure
1602  *
1603  * @v wlan WLAN structure to free
1604  */
1605 void net80211_free_wlan ( struct net80211_wlan *wlan )
1606 {
1607  if ( wlan ) {
1608  free_iob ( wlan->beacon );
1609  free ( wlan );
1610  }
1611 }
1612 
1613 
1614 /**
1615  * Free list of WLAN structures
1616  *
1617  * @v list List of WLAN structures to free
1618  */
1619 void net80211_free_wlanlist ( struct list_head *list )
1620 {
1621  struct net80211_wlan *wlan, *tmp;
1622 
1623  if ( ! list )
1624  return;
1625 
1626  list_for_each_entry_safe ( wlan, tmp, list, list ) {
1627  list_del ( &wlan->list );
1628  net80211_free_wlan ( wlan );
1629  }
1630 
1631  free ( list );
1632 }
1633 
1634 
1635 /** Number of ticks to wait for replies to association management frames */
1636 #define ASSOC_TIMEOUT TICKS_PER_SEC
1637 
1638 /** Number of times to try sending a particular association management frame */
1639 #define ASSOC_RETRIES 2
1640 
1641 /**
1642  * Step 802.11 association process
1643  *
1644  * @v dev 802.11 device
1645  */
1646 static void net80211_step_associate ( struct net80211_device *dev )
1647 {
1648  int rc = 0;
1649  int status = dev->state & NET80211_STATUS_MASK;
1650 
1651  /*
1652  * We use a sort of state machine implemented using bits in
1653  * the dev->state variable. At each call, we take the
1654  * logically first step that has not yet succeeded; either it
1655  * has not been tried yet, it's being retried, or it failed.
1656  * If it failed, we return an error indication; otherwise we
1657  * perform the step. If it succeeds, RX handling code will set
1658  * the appropriate status bit for us.
1659  *
1660  * Probe works a bit differently, since we have to step it
1661  * on every call instead of waiting for a packet to arrive
1662  * that will set the completion bit for us.
1663  */
1664 
1665  /* If we're waiting for a reply, check for timeout condition */
1666  if ( dev->state & NET80211_WAITING ) {
1667  /* Sanity check */
1668  if ( ! dev->associating )
1669  return;
1670 
1671  if ( currticks() - dev->ctx.assoc->last_packet > ASSOC_TIMEOUT ) {
1672  /* Timed out - fail if too many retries, or retry */
1673  dev->ctx.assoc->times_tried++;
1674  if ( ++dev->ctx.assoc->times_tried > ASSOC_RETRIES ) {
1675  rc = -ETIMEDOUT;
1676  goto fail;
1677  }
1678  } else {
1679  /* Didn't time out - let it keep going */
1680  return;
1681  }
1682  } else {
1683  if ( dev->state & NET80211_PROBED )
1684  dev->ctx.assoc->times_tried = 0;
1685  }
1686 
1687  if ( ! ( dev->state & NET80211_PROBED ) ) {
1688  /* state: probe */
1689 
1690  if ( ! dev->ctx.probe ) {
1691  /* start probe */
1692  int active = fetch_intz_setting ( NULL,
1693  &net80211_active_setting );
1694  int band = dev->hw->bands;
1695 
1696  if ( active )
1697  band &= ~NET80211_BAND_BIT_5GHZ;
1698 
1699  rc = net80211_prepare_probe ( dev, band, active );
1700  if ( rc )
1701  goto fail;
1702 
1703  dev->ctx.probe = net80211_probe_start ( dev, dev->essid,
1704  active );
1705  if ( ! dev->ctx.probe ) {
1706  dev->assoc_rc = -ENOMEM;
1707  goto fail;
1708  }
1709  }
1710 
1711  rc = net80211_probe_step ( dev->ctx.probe );
1712  if ( ! rc ) {
1713  return; /* still going */
1714  }
1715 
1717  dev->ctx.probe = NULL;
1718  if ( ! dev->associating ) {
1719  if ( rc > 0 ) /* "successful" probe found nothing */
1720  rc = -ETIMEDOUT;
1721  goto fail;
1722  }
1723 
1724  /* If we probed using a broadcast SSID, record that
1725  fact for the settings applicator before we clobber
1726  it with the specific SSID we've chosen. */
1727  if ( ! dev->essid[0] )
1728  dev->state |= NET80211_AUTO_SSID;
1729 
1730  DBGC ( dev, "802.11 %p found network %s (%s)\n", dev,
1731  dev->associating->essid,
1732  eth_ntoa ( dev->associating->bssid ) );
1733 
1734  dev->ctx.assoc = zalloc ( sizeof ( *dev->ctx.assoc ) );
1735  if ( ! dev->ctx.assoc ) {
1736  rc = -ENOMEM;
1737  goto fail;
1738  }
1739 
1740  dev->state |= NET80211_PROBED;
1742 
1743  return;
1744  }
1745 
1746  /* Record time of sending the packet we're about to send, for timeout */
1747  dev->ctx.assoc->last_packet = currticks();
1748 
1749  if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) {
1750  /* state: prepare and authenticate */
1751 
1752  if ( status != IEEE80211_STATUS_SUCCESS ) {
1753  /* we tried authenticating already, but failed */
1754  int method = dev->ctx.assoc->method;
1755 
1759  /* Maybe this network uses Shared Key? */
1760  dev->ctx.assoc->method =
1762  } else {
1763  goto fail;
1764  }
1765  }
1766 
1767  DBGC ( dev, "802.11 %p authenticating with method %d\n", dev,
1768  dev->ctx.assoc->method );
1769 
1770  rc = net80211_prepare_assoc ( dev, dev->associating );
1771  if ( rc )
1772  goto fail;
1773 
1774  rc = net80211_send_auth ( dev, dev->associating,
1775  dev->ctx.assoc->method );
1776  if ( rc )
1777  goto fail;
1778 
1779  return;
1780  }
1781 
1782  if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
1783  /* state: associate */
1784 
1786  goto fail;
1787 
1788  DBGC ( dev, "802.11 %p associating\n", dev );
1789 
1790  if ( dev->handshaker && dev->handshaker->start &&
1791  ! dev->handshaker->started ) {
1792  rc = dev->handshaker->start ( dev );
1793  if ( rc < 0 )
1794  goto fail;
1795  dev->handshaker->started = 1;
1796  }
1797 
1798  rc = net80211_send_assoc ( dev, dev->associating );
1799  if ( rc )
1800  goto fail;
1801 
1802  return;
1803  }
1804 
1805  if ( ! ( dev->state & NET80211_CRYPTO_SYNCED ) ) {
1806  /* state: crypto sync */
1807  DBGC ( dev, "802.11 %p security handshaking\n", dev );
1808 
1809  if ( ! dev->handshaker || ! dev->handshaker->step ) {
1810  dev->state |= NET80211_CRYPTO_SYNCED;
1811  return;
1812  }
1813 
1814  rc = dev->handshaker->step ( dev );
1815 
1816  if ( rc < 0 ) {
1817  /* Only record the returned error if we're
1818  still marked as associated, because an
1819  asynchronous error will have already been
1820  reported to net80211_deauthenticate() and
1821  assoc_rc thereby set. */
1822  if ( dev->state & NET80211_ASSOCIATED )
1823  dev->assoc_rc = rc;
1824  rc = 0;
1825  goto fail;
1826  }
1827 
1828  if ( rc > 0 ) {
1829  dev->assoc_rc = 0;
1830  dev->state |= NET80211_CRYPTO_SYNCED;
1831  }
1832  return;
1833  }
1834 
1835  /* state: done! */
1836  netdev_link_up ( dev->netdev );
1837  dev->assoc_rc = 0;
1838  dev->state &= ~NET80211_WORKING;
1839 
1840  free ( dev->ctx.assoc );
1841  dev->ctx.assoc = NULL;
1842 
1844  dev->associating = NULL;
1845 
1846  dev->rctl = rc80211_init ( dev );
1847 
1848  process_del ( &dev->proc_assoc );
1849 
1850  DBGC ( dev, "802.11 %p associated with %s (%s)\n", dev,
1851  dev->essid, eth_ntoa ( dev->bssid ) );
1852 
1853  return;
1854 
1855  fail:
1856  dev->state &= ~( NET80211_WORKING | NET80211_WAITING );
1857  if ( rc )
1858  dev->assoc_rc = rc;
1859 
1860  netdev_link_err ( dev->netdev, dev->assoc_rc );
1861 
1862  /* We never reach here from the middle of a probe, so we don't
1863  need to worry about freeing dev->ctx.probe. */
1864 
1865  if ( dev->state & NET80211_PROBED ) {
1866  free ( dev->ctx.assoc );
1867  dev->ctx.assoc = NULL;
1868  }
1869 
1871  dev->associating = NULL;
1872 
1873  process_del ( &dev->proc_assoc );
1874 
1875  DBGC ( dev, "802.11 %p association failed (state=%04x): "
1876  "%s\n", dev, dev->state, strerror ( dev->assoc_rc ) );
1877 
1878  /* Try it again: */
1879  net80211_autoassociate ( dev );
1880 }
1881 
1882 /**
1883  * Check for 802.11 SSID or key updates
1884  *
1885  * This acts as a settings applicator; if the user changes netX/ssid,
1886  * and netX is currently open, the association task will be invoked
1887  * again. If the user changes the encryption key, the current security
1888  * handshaker will be asked to update its state to match; if that is
1889  * impossible without reassociation, we reassociate.
1890  */
1892 {
1893  struct net80211_device *dev;
1894  char ssid[IEEE80211_MAX_SSID_LEN + 1];
1895  int key_reassoc;
1896 
1898  if ( ! netdev_is_open ( dev->netdev ) )
1899  continue;
1900 
1901  key_reassoc = 0;
1902  if ( dev->handshaker && dev->handshaker->change_key &&
1903  dev->handshaker->change_key ( dev ) < 0 )
1904  key_reassoc = 1;
1905 
1907  &net80211_ssid_setting, ssid,
1908  IEEE80211_MAX_SSID_LEN + 1 );
1909 
1910  if ( key_reassoc ||
1911  ( ! ( ! ssid[0] && ( dev->state & NET80211_AUTO_SSID ) ) &&
1912  strcmp ( ssid, dev->essid ) != 0 ) ) {
1913  DBGC ( dev, "802.11 %p updating association: "
1914  "%s -> %s\n", dev, dev->essid, ssid );
1915  net80211_autoassociate ( dev );
1916  }
1917  }
1918 
1919  return 0;
1920 }
1921 
1922 /**
1923  * Start 802.11 association process
1924  *
1925  * @v dev 802.11 device
1926  *
1927  * If the association process is running, it will be restarted.
1928  */
1930 {
1931  if ( ! ( dev->state & NET80211_WORKING ) ) {
1932  DBGC2 ( dev, "802.11 %p spawning association process\n", dev );
1933  process_add ( &dev->proc_assoc );
1934  } else {
1935  DBGC2 ( dev, "802.11 %p restarting association\n", dev );
1936  }
1937 
1938  /* Clean up everything an earlier association process might
1939  have been in the middle of using */
1940  if ( dev->associating )
1942 
1943  if ( ! ( dev->state & NET80211_PROBED ) )
1946  else
1947  free ( dev->ctx.assoc );
1948 
1949  /* Reset to a clean state */
1951  &net80211_ssid_setting, dev->essid,
1952  IEEE80211_MAX_SSID_LEN + 1 );
1953  dev->ctx.probe = NULL;
1954  dev->associating = NULL;
1955  dev->assoc_rc = 0;
1957 }
1958 
1959 /**
1960  * Pick TX rate for RTS/CTS packets based on data rate
1961  *
1962  * @v dev 802.11 device
1963  *
1964  * The RTS/CTS rate is the fastest TX rate marked as "basic" that is
1965  * not faster than the data rate.
1966  */
1967 static void net80211_set_rtscts_rate ( struct net80211_device *dev )
1968 {
1969  u16 datarate = dev->rates[dev->rate];
1970  u16 rtsrate = 0;
1971  int rts_idx = -1;
1972  int i;
1973 
1974  for ( i = 0; i < dev->nr_rates; i++ ) {
1975  u16 rate = dev->rates[i];
1976 
1977  if ( ! ( dev->basic_rates & ( 1 << i ) ) || rate > datarate )
1978  continue;
1979 
1980  if ( rate > rtsrate ) {
1981  rtsrate = rate;
1982  rts_idx = i;
1983  }
1984  }
1985 
1986  /* If this is in initialization, we might not have any basic
1987  rates; just use the first data rate in that case. */
1988  if ( rts_idx < 0 )
1989  rts_idx = 0;
1990 
1991  dev->rtscts_rate = rts_idx;
1992 }
1993 
1994 /**
1995  * Set data transmission rate for 802.11 device
1996  *
1997  * @v dev 802.11 device
1998  * @v rate Rate to set, as index into @c dev->rates array
1999  */
2000 void net80211_set_rate_idx ( struct net80211_device *dev, int rate )
2001 {
2002  assert ( netdev_is_open ( dev->netdev ) );
2003 
2004  if ( rate >= 0 && rate < dev->nr_rates && rate != dev->rate ) {
2005  DBGC2 ( dev, "802.11 %p changing rate from %d->%d Mbps\n",
2006  dev, dev->rates[dev->rate] / 10,
2007  dev->rates[rate] / 10 );
2008 
2009  dev->rate = rate;
2010  net80211_set_rtscts_rate ( dev );
2011  dev->op->config ( dev, NET80211_CFG_RATE );
2012  }
2013 }
2014 
2015 /**
2016  * Configure 802.11 device to transmit on a certain channel
2017  *
2018  * @v dev 802.11 device
2019  * @v channel Channel number (1-11 for 2.4GHz) to transmit on
2020  */
2022 {
2023  int i, oldchan = dev->channel;
2024 
2025  assert ( netdev_is_open ( dev->netdev ) );
2026 
2027  for ( i = 0; i < dev->nr_channels; i++ ) {
2028  if ( dev->channels[i].channel_nr == channel ) {
2029  dev->channel = i;
2030  break;
2031  }
2032  }
2033 
2034  if ( i == dev->nr_channels )
2035  return -ENOENT;
2036 
2037  if ( i != oldchan )
2038  return dev->op->config ( dev, NET80211_CFG_CHANNEL );
2039 
2040  return 0;
2041 }
2042 
2043 /**
2044  * Prepare 802.11 device channel and rate set for scanning
2045  *
2046  * @v dev 802.11 device
2047  * @v band RF band(s) on which to prepare for scanning
2048  * @v active Whether the scanning will be active
2049  * @ret rc Return status code
2050  */
2051 int net80211_prepare_probe ( struct net80211_device *dev, int band,
2052  int active )
2053 {
2054  assert ( netdev_is_open ( dev->netdev ) );
2055 
2056  if ( active && ( band & NET80211_BAND_BIT_5GHZ ) ) {
2057  DBGC ( dev, "802.11 %p cannot perform active scanning on "
2058  "5GHz band\n", dev );
2059  return -EINVAL_ACTIVE_SCAN;
2060  }
2061 
2062  if ( band == 0 ) {
2063  /* This can happen for a 5GHz-only card with 5GHz
2064  scanning masked out by an active request. */
2065  DBGC ( dev, "802.11 %p asked to prepare for scanning nothing\n",
2066  dev );
2067  return -EINVAL_ACTIVE_SCAN;
2068  }
2069 
2070  dev->nr_channels = 0;
2071 
2072  if ( active )
2074  else {
2075  if ( band & NET80211_BAND_BIT_2GHZ )
2076  net80211_add_channels ( dev, 1, 14,
2078  if ( band & NET80211_BAND_BIT_5GHZ )
2079  net80211_add_channels ( dev, 36, 8,
2081  }
2082 
2084 
2085  /* Use channel 1 for now */
2086  dev->channel = 0;
2087  dev->op->config ( dev, NET80211_CFG_CHANNEL );
2088 
2089  /* Always do active probes at lowest (presumably first) speed */
2090  dev->rate = 0;
2091  dev->nr_rates = 1;
2092  dev->rates[0] = dev->hw->rates[dev->channels[0].band][0];
2093  dev->op->config ( dev, NET80211_CFG_RATE );
2094 
2095  return 0;
2096 }
2097 
2098 /**
2099  * Prepare 802.11 device channel and rate set for communication
2100  *
2101  * @v dev 802.11 device
2102  * @v wlan WLAN to prepare for communication with
2103  * @ret rc Return status code
2104  */
2106  struct net80211_wlan *wlan )
2107 {
2108  struct ieee80211_frame *hdr = wlan->beacon->data;
2109  struct ieee80211_beacon *beacon =
2110  ( struct ieee80211_beacon * ) hdr->data;
2111  struct net80211_handshaker *handshaker;
2112  int rc;
2113 
2114  assert ( netdev_is_open ( dev->netdev ) );
2115 
2116  net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
2117  memcpy ( dev->bssid, wlan->bssid, ETH_ALEN );
2118  strcpy ( dev->essid, wlan->essid );
2119 
2120  free ( dev->rsn_ie );
2121  dev->rsn_ie = NULL;
2122 
2123  dev->last_beacon_timestamp = beacon->timestamp;
2124  dev->tx_beacon_interval = 1024 * beacon->beacon_interval;
2125 
2126  /* Barring an IE that tells us the channel outright, assume
2127  the channel we heard this AP best on is the channel it's
2128  communicating on. */
2129  net80211_change_channel ( dev, wlan->channel );
2130 
2131  rc = net80211_process_capab ( dev, beacon->capability );
2132  if ( rc )
2133  return rc;
2134 
2135  rc = net80211_process_ie ( dev, beacon->info_element,
2136  wlan->beacon->tail );
2137  if ( rc )
2138  return rc;
2139 
2140  /* Associate at the lowest rate so we know it'll get through */
2141  dev->rate = 0;
2142  dev->op->config ( dev, NET80211_CFG_RATE );
2143 
2144  /* Free old handshaker and crypto, if they exist */
2145  if ( dev->handshaker && dev->handshaker->stop &&
2146  dev->handshaker->started )
2147  dev->handshaker->stop ( dev );
2148  free ( dev->handshaker );
2149  dev->handshaker = NULL;
2150  free ( dev->crypto );
2151  free ( dev->gcrypto );
2152  dev->crypto = dev->gcrypto = NULL;
2153 
2154  /* Find new security handshaker to use */
2155  for_each_table_entry ( handshaker, NET80211_HANDSHAKERS ) {
2156  if ( handshaker->protocol == wlan->handshaking ) {
2157  dev->handshaker = zalloc ( sizeof ( *handshaker ) +
2158  handshaker->priv_len );
2159  if ( ! dev->handshaker )
2160  return -ENOMEM;
2161 
2162  memcpy ( dev->handshaker, handshaker,
2163  sizeof ( *handshaker ) );
2164  dev->handshaker->priv = ( ( void * ) dev->handshaker +
2165  sizeof ( *handshaker ) );
2166  break;
2167  }
2168  }
2169 
2170  if ( ( wlan->handshaking != NET80211_SECPROT_NONE ) &&
2171  ! dev->handshaker ) {
2172  DBGC ( dev, "802.11 %p no support for handshaking scheme %d\n",
2173  dev, wlan->handshaking );
2174  return -( ENOTSUP | ( wlan->handshaking << 8 ) );
2175  }
2176 
2177  /* Initialize security handshaker */
2178  if ( dev->handshaker ) {
2179  rc = dev->handshaker->init ( dev );
2180  if ( rc < 0 )
2181  return rc;
2182  }
2183 
2184  return 0;
2185 }
2186 
2187 /**
2188  * Send 802.11 initial authentication frame
2189  *
2190  * @v dev 802.11 device
2191  * @v wlan WLAN to authenticate with
2192  * @v method Authentication method
2193  * @ret rc Return status code
2194  *
2195  * @a method may be 0 for Open System authentication or 1 for Shared
2196  * Key authentication. Open System provides no security in association
2197  * whatsoever, relying on encryption for confidentiality, but Shared
2198  * Key actively introduces security problems and is very rarely used.
2199  */
2201  struct net80211_wlan *wlan, int method )
2202 {
2203  struct io_buffer *iob = alloc_iob ( 64 );
2204  struct ieee80211_auth *auth;
2205 
2206  net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
2208  auth = iob_put ( iob, sizeof ( *auth ) );
2209  auth->algorithm = method;
2210  auth->tx_seq = 1;
2211  auth->status = 0;
2212 
2213  return net80211_tx_mgmt ( dev, IEEE80211_STYPE_AUTH, wlan->bssid, iob );
2214 }
2215 
2216 /**
2217  * Handle receipt of 802.11 authentication frame
2218  *
2219  * @v dev 802.11 device
2220  * @v iob I/O buffer
2221  *
2222  * If the authentication method being used is Shared Key, and the
2223  * frame that was received included challenge text, the frame is
2224  * encrypted using the cryptosystem currently in effect and sent back
2225  * to the AP to complete the authentication.
2226  */
2227 static void net80211_handle_auth ( struct net80211_device *dev,
2228  struct io_buffer *iob )
2229 {
2230  struct ieee80211_frame *hdr = iob->data;
2231  struct ieee80211_auth *auth =
2232  ( struct ieee80211_auth * ) hdr->data;
2233 
2234  if ( auth->tx_seq & 1 ) {
2235  DBGC ( dev, "802.11 %p authentication received improperly "
2236  "directed frame (seq. %d)\n", dev, auth->tx_seq );
2239  return;
2240  }
2241 
2242  if ( auth->status != IEEE80211_STATUS_SUCCESS ) {
2243  DBGC ( dev, "802.11 %p authentication failed: status %d\n",
2244  dev, auth->status );
2246  auth->status );
2247  return;
2248  }
2249 
2250  if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && ! dev->crypto ) {
2251  DBGC ( dev, "802.11 %p can't perform shared-key authentication "
2252  "without a cryptosystem\n", dev );
2255  return;
2256  }
2257 
2258  if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY &&
2259  auth->tx_seq == 2 ) {
2260  /* Since the iob we got is going to be freed as soon
2261  as we return, we can do some in-place
2262  modification. */
2263  auth->tx_seq = 3;
2264  auth->status = 0;
2265 
2266  memcpy ( hdr->addr2, hdr->addr1, ETH_ALEN );
2267  memcpy ( hdr->addr1, hdr->addr3, ETH_ALEN );
2268 
2269  netdev_tx ( dev->netdev,
2270  dev->crypto->encrypt ( dev->crypto, iob ) );
2271  return;
2272  }
2273 
2276 
2277  return;
2278 }
2279 
2280 /**
2281  * Send 802.11 association frame
2282  *
2283  * @v dev 802.11 device
2284  * @v wlan WLAN to associate with
2285  * @ret rc Return status code
2286  */
2288  struct net80211_wlan *wlan )
2289 {
2290  struct io_buffer *iob = alloc_iob ( 128 );
2291  struct ieee80211_assoc_req *assoc;
2292  union ieee80211_ie *ie;
2293 
2294  net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
2295 
2297  assoc = iob->data;
2298 
2300  if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) )
2302  if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) )
2304  if ( wlan->crypto )
2306 
2307  assoc->listen_interval = 1;
2308 
2309  ie = net80211_marshal_request_info ( dev, assoc->info_element );
2310 
2311  DBGP ( "802.11 %p about to send association request:\n", dev );
2312  DBGP_HD ( iob->data, ( void * ) ie - iob->data );
2313 
2314  iob_put ( iob, ( void * ) ie - iob->data );
2315 
2317  wlan->bssid, iob );
2318 }
2319 
2320 /**
2321  * Handle receipt of 802.11 association reply frame
2322  *
2323  * @v dev 802.11 device
2324  * @v iob I/O buffer
2325  */
2327  struct io_buffer *iob )
2328 {
2329  struct ieee80211_frame *hdr = iob->data;
2330  struct ieee80211_assoc_resp *assoc =
2331  ( struct ieee80211_assoc_resp * ) hdr->data;
2332 
2333  net80211_process_capab ( dev, assoc->capability );
2334  net80211_process_ie ( dev, assoc->info_element, iob->tail );
2335 
2336  if ( assoc->status != IEEE80211_STATUS_SUCCESS ) {
2337  DBGC ( dev, "802.11 %p association failed: status %d\n",
2338  dev, assoc->status );
2340  assoc->status );
2341  return;
2342  }
2343 
2344  /* ESSID was filled before the association request was sent */
2345  memcpy ( dev->bssid, hdr->addr3, ETH_ALEN );
2346  dev->aid = assoc->aid;
2347 
2350 }
2351 
2352 
2353 /**
2354  * Send 802.11 disassociation frame
2355  *
2356  * @v dev 802.11 device
2357  * @v reason Reason for disassociation
2358  * @v deauth If TRUE, send deauthentication instead of disassociation
2359  * @ret rc Return status code
2360  */
2361 static int net80211_send_disassoc ( struct net80211_device *dev, int reason,
2362  int deauth )
2363 {
2364  struct io_buffer *iob = alloc_iob ( 64 );
2365  struct ieee80211_disassoc *disassoc;
2366 
2367  if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2368  return -EINVAL;
2369 
2370  net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
2372  disassoc = iob_put ( iob, sizeof ( *disassoc ) );
2373  disassoc->reason = reason;
2374 
2375  return net80211_tx_mgmt ( dev, deauth ? IEEE80211_STYPE_DEAUTH :
2376  IEEE80211_STYPE_DISASSOC, dev->bssid, iob );
2377 }
2378 
2379 
2380 /**
2381  * Deauthenticate from current network and try again
2382  *
2383  * @v dev 802.11 device
2384  * @v rc Return status code indicating reason
2385  *
2386  * The deauthentication will be sent using an 802.11 "unspecified
2387  * reason", as is common, but @a rc will be set as a link-up
2388  * error to aid the user in debugging.
2389  */
2390 void net80211_deauthenticate ( struct net80211_device *dev, int rc )
2391 {
2393  dev->assoc_rc = rc;
2394  netdev_link_err ( dev->netdev, rc );
2395 
2396  net80211_autoassociate ( dev );
2397 }
2398 
2399 
2400 /** Smoothing factor (1-7) for link quality calculation */
2401 #define LQ_SMOOTH 7
2402 
2403 /**
2404  * Update link quality information based on received beacon
2405  *
2406  * @v dev 802.11 device
2407  * @v iob I/O buffer containing beacon
2408  * @ret rc Return status code
2409  */
2411  struct io_buffer *iob )
2412 {
2413  struct ieee80211_frame *hdr = iob->data;
2414  struct ieee80211_beacon *beacon;
2415  u32 dt, rxi;
2416 
2417  if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2418  return;
2419 
2420  beacon = ( struct ieee80211_beacon * ) hdr->data;
2421  dt = ( u32 ) ( beacon->timestamp - dev->last_beacon_timestamp );
2422  rxi = dev->rx_beacon_interval;
2423 
2424  rxi = ( LQ_SMOOTH * rxi ) + ( ( 8 - LQ_SMOOTH ) * dt );
2425  dev->rx_beacon_interval = rxi >> 3;
2426 
2427  dev->last_beacon_timestamp = beacon->timestamp;
2428 }
2429 
2430 
2431 /**
2432  * Handle receipt of 802.11 management frame
2433  *
2434  * @v dev 802.11 device
2435  * @v iob I/O buffer
2436  * @v signal Signal strength of received frame
2437  */
2438 static void net80211_handle_mgmt ( struct net80211_device *dev,
2439  struct io_buffer *iob, int signal )
2440 {
2441  struct ieee80211_frame *hdr = iob->data;
2442  struct ieee80211_disassoc *disassoc;
2443  u16 stype = hdr->fc & IEEE80211_FC_SUBTYPE;
2444  int keep = 0;
2445  int is_deauth = ( stype == IEEE80211_STYPE_DEAUTH );
2446 
2447  if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_MGMT ) {
2448  free_iob ( iob );
2449  return; /* only handle management frames */
2450  }
2451 
2452  switch ( stype ) {
2453  /* We reconnect on deauthentication and disassociation. */
2456  disassoc = ( struct ieee80211_disassoc * ) hdr->data;
2457  net80211_set_state ( dev, is_deauth ? NET80211_AUTHENTICATED :
2459  NET80211_IS_REASON | disassoc->reason );
2460  DBGC ( dev, "802.11 %p %s: reason %d\n",
2461  dev, is_deauth ? "deauthenticated" : "disassociated",
2462  disassoc->reason );
2463 
2464  /* Try to reassociate, in case it's transient. */
2465  net80211_autoassociate ( dev );
2466 
2467  break;
2468 
2469  /* We handle authentication and association. */
2470  case IEEE80211_STYPE_AUTH:
2471  if ( ! ( dev->state & NET80211_AUTHENTICATED ) )
2472  net80211_handle_auth ( dev, iob );
2473  break;
2474 
2477  if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2478  net80211_handle_assoc_reply ( dev, iob );
2479  break;
2480 
2481  /* We pass probes and beacons onto network scanning
2482  code. Pass actions for future extensibility. */
2484  net80211_update_link_quality ( dev, iob );
2485  /* fall through */
2488  if ( dev->keep_mgmt ) {
2489  struct net80211_rx_info *rxinf;
2490  rxinf = zalloc ( sizeof ( *rxinf ) );
2491  if ( ! rxinf ) {
2492  DBGC ( dev, "802.11 %p out of memory\n", dev );
2493  break;
2494  }
2495  rxinf->signal = signal;
2496  list_add_tail ( &iob->list, &dev->mgmt_queue );
2497  list_add_tail ( &rxinf->list, &dev->mgmt_info_queue );
2498  keep = 1;
2499  }
2500  break;
2501 
2503  /* Some nodes send these broadcast. Ignore them. */
2504  break;
2505 
2508  /* We should never receive these, only send them. */
2509  DBGC ( dev, "802.11 %p received strange management request "
2510  "(%04x)\n", dev, stype );
2511  break;
2512 
2513  default:
2514  DBGC ( dev, "802.11 %p received unimplemented management "
2515  "packet (%04x)\n", dev, stype );
2516  break;
2517  }
2518 
2519  if ( ! keep )
2520  free_iob ( iob );
2521 }
2522 
2523 /* ---------- Packet handling functions ---------- */
2524 
2525 /**
2526  * Free buffers used by 802.11 fragment cache entry
2527  *
2528  * @v dev 802.11 device
2529  * @v fcid Fragment cache entry index
2530  *
2531  * After this function, the referenced entry will be marked unused.
2532  */
2533 static void net80211_free_frags ( struct net80211_device *dev, int fcid )
2534 {
2535  int j;
2536  struct net80211_frag_cache *frag = &dev->frags[fcid];
2537 
2538  for ( j = 0; j < 16; j++ ) {
2539  if ( frag->iob[j] ) {
2540  free_iob ( frag->iob[j] );
2541  frag->iob[j] = NULL;
2542  }
2543  }
2544 
2545  frag->seqnr = 0;
2546  frag->start_ticks = 0;
2547  frag->in_use = 0;
2548 }
2549 
2550 /**
2551  * Accumulate 802.11 fragments into one I/O buffer
2552  *
2553  * @v dev 802.11 device
2554  * @v fcid Fragment cache entry index
2555  * @v nfrags Number of fragments received
2556  * @v size Sum of sizes of all fragments, including headers
2557  * @ret iob I/O buffer containing reassembled packet
2558  *
2559  * This function does not free the fragment buffers.
2560  */
2561 static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
2562  int fcid, int nfrags, int size )
2563 {
2564  struct net80211_frag_cache *frag = &dev->frags[fcid];
2565  int hdrsize = IEEE80211_TYP_FRAME_HEADER_LEN;
2566  int nsize = size - hdrsize * ( nfrags - 1 );
2567  int i;
2568 
2569  struct io_buffer *niob = alloc_iob ( nsize );
2570  struct ieee80211_frame *hdr;
2571 
2572  /* Add the header from the first one... */
2573  memcpy ( iob_put ( niob, hdrsize ), frag->iob[0]->data, hdrsize );
2574 
2575  /* ... and all the data from all of them. */
2576  for ( i = 0; i < nfrags; i++ ) {
2577  int len = iob_len ( frag->iob[i] ) - hdrsize;
2578  memcpy ( iob_put ( niob, len ),
2579  frag->iob[i]->data + hdrsize, len );
2580  }
2581 
2582  /* Turn off the fragment bit. */
2583  hdr = niob->data;
2584  hdr->fc &= ~IEEE80211_FC_MORE_FRAG;
2585 
2586  return niob;
2587 }
2588 
2589 /**
2590  * Handle receipt of 802.11 fragment
2591  *
2592  * @v dev 802.11 device
2593  * @v iob I/O buffer containing fragment
2594  * @v signal Signal strength with which fragment was received
2595  */
2596 static void net80211_rx_frag ( struct net80211_device *dev,
2597  struct io_buffer *iob, int signal )
2598 {
2599  struct ieee80211_frame *hdr = iob->data;
2600  int fragnr = IEEE80211_FRAG ( hdr->seq );
2601 
2602  if ( fragnr == 0 && ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2603  /* start a frag cache entry */
2604  int i, newest = -1;
2605  u32 curr_ticks = currticks(), newest_ticks = 0;
2607 
2608  for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
2609  if ( dev->frags[i].in_use == 0 )
2610  break;
2611 
2612  if ( dev->frags[i].start_ticks + timeout >=
2613  curr_ticks ) {
2614  net80211_free_frags ( dev, i );
2615  break;
2616  }
2617 
2618  if ( dev->frags[i].start_ticks > newest_ticks ) {
2619  newest = i;
2620  newest_ticks = dev->frags[i].start_ticks;
2621  }
2622  }
2623 
2624  /* If we're being sent more concurrent fragmented
2625  packets than we can handle, drop the newest so the
2626  older ones have time to complete. */
2627  if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
2628  i = newest;
2629  net80211_free_frags ( dev, i );
2630  }
2631 
2632  dev->frags[i].in_use = 1;
2633  dev->frags[i].seqnr = IEEE80211_SEQNR ( hdr->seq );
2634  dev->frags[i].start_ticks = currticks();
2635  dev->frags[i].iob[0] = iob;
2636  return;
2637  } else {
2638  int i;
2639  for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
2640  if ( dev->frags[i].in_use && dev->frags[i].seqnr ==
2641  IEEE80211_SEQNR ( hdr->seq ) )
2642  break;
2643  }
2644  if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
2645  /* Drop non-first not-in-cache fragments */
2646  DBGC ( dev, "802.11 %p dropped fragment fc=%04x "
2647  "seq=%04x\n", dev, hdr->fc, hdr->seq );
2648  free_iob ( iob );
2649  return;
2650  }
2651 
2652  dev->frags[i].iob[fragnr] = iob;
2653 
2654  if ( ! ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2655  int j, size = 0;
2656  for ( j = 0; j < fragnr; j++ ) {
2657  size += iob_len ( dev->frags[i].iob[j] );
2658  if ( dev->frags[i].iob[j] == NULL )
2659  break;
2660  }
2661  if ( j == fragnr ) {
2662  /* We've got everything */
2663  struct io_buffer *niob =
2664  net80211_accum_frags ( dev, i, fragnr,
2665  size );
2666  net80211_free_frags ( dev, i );
2667  net80211_rx ( dev, niob, signal, 0 );
2668  } else {
2669  DBGC ( dev, "802.11 %p dropping fragmented "
2670  "packet due to out-of-order arrival, "
2671  "fc=%04x seq=%04x\n", dev, hdr->fc,
2672  hdr->seq );
2673  net80211_free_frags ( dev, i );
2674  }
2675  }
2676  }
2677 }
2678 
2679 /**
2680  * Handle receipt of 802.11 frame
2681  *
2682  * @v dev 802.11 device
2683  * @v iob I/O buffer
2684  * @v signal Received signal strength
2685  * @v rate Bitrate at which frame was received, in 100 kbps units
2686  *
2687  * If the rate or signal is unknown, 0 should be passed.
2688  */
2689 void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob,
2690  int signal, u16 rate )
2691 {
2692  struct ieee80211_frame *hdr = iob->data;
2693  u16 type = hdr->fc & IEEE80211_FC_TYPE;
2694  if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION )
2695  goto drop; /* drop invalid-version packets */
2696 
2697  if ( type == IEEE80211_TYPE_CTRL )
2698  goto drop; /* we don't handle control packets,
2699  the hardware does */
2700 
2701  if ( dev->last_rx_seq == hdr->seq )
2702  goto drop; /* avoid duplicate packet */
2703  dev->last_rx_seq = hdr->seq;
2704 
2705  if ( dev->hw->flags & NET80211_HW_RX_HAS_FCS ) {
2706  /* discard the FCS */
2707  iob_unput ( iob, 4 );
2708  }
2709 
2710  /* Only decrypt packets from our BSSID, to avoid spurious errors */
2711  if ( ( hdr->fc & IEEE80211_FC_PROTECTED ) &&
2712  ! memcmp ( hdr->addr2, dev->bssid, ETH_ALEN ) ) {
2713  /* Decrypt packet; record and drop if it fails */
2714  struct io_buffer *niob;
2715  struct net80211_crypto *crypto = dev->crypto;
2716 
2717  if ( ! dev->crypto ) {
2718  DBGC ( dev, "802.11 %p cannot decrypt packet "
2719  "without a cryptosystem\n", dev );
2720  goto drop_crypt;
2721  }
2722 
2723  if ( ( hdr->addr1[0] & 1 ) && dev->gcrypto ) {
2724  /* Use group decryption if needed */
2725  crypto = dev->gcrypto;
2726  }
2727 
2728  niob = crypto->decrypt ( crypto, iob );
2729  if ( ! niob ) {
2730  DBGC ( dev, "802.11 %p decryption error\n", dev );
2731  goto drop_crypt;
2732  }
2733  free_iob ( iob );
2734  iob = niob;
2735  hdr = iob->data;
2736  }
2737 
2738  dev->last_signal = signal;
2739 
2740  /* Fragments go into the frag cache or get dropped. */
2741  if ( IEEE80211_FRAG ( hdr->seq ) != 0
2742  || ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2743  net80211_rx_frag ( dev, iob, signal );
2744  return;
2745  }
2746 
2747  /* Management frames get handled, enqueued, or dropped. */
2748  if ( type == IEEE80211_TYPE_MGMT ) {
2749  net80211_handle_mgmt ( dev, iob, signal );
2750  return;
2751  }
2752 
2753  /* Data frames get dropped or sent to the net_device. */
2754  if ( ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA )
2755  goto drop; /* drop QoS, CFP, or null data packets */
2756 
2757  /* Update rate-control algorithm */
2758  if ( dev->rctl )
2759  rc80211_update_rx ( dev, hdr->fc & IEEE80211_FC_RETRY, rate );
2760 
2761  /* Pass packet onward */
2762  if ( dev->state & NET80211_ASSOCIATED ) {
2763  netdev_rx ( dev->netdev, iob );
2764  return;
2765  }
2766 
2767  /* No association? Drop it. */
2768  goto drop;
2769 
2770  drop_crypt:
2772  drop:
2773  DBGC2 ( dev, "802.11 %p dropped packet fc=%04x seq=%04x\n", dev,
2774  hdr->fc, hdr->seq );
2775  free_iob ( iob );
2776  return;
2777 }
2778 
2779 /** Indicate an error in receiving a packet
2780  *
2781  * @v dev 802.11 device
2782  * @v iob I/O buffer with received packet, or NULL
2783  * @v rc Error code
2784  *
2785  * This logs the error with the wrapping net_device, and frees iob if
2786  * it is passed.
2787  */
2789  struct io_buffer *iob, int rc )
2790 {
2791  netdev_rx_err ( dev->netdev, iob, rc );
2792 }
2793 
2794 /** Indicate the completed transmission of a packet
2795  *
2796  * @v dev 802.11 device
2797  * @v iob I/O buffer of transmitted packet
2798  * @v retries Number of times this packet was retransmitted
2799  * @v rc Error code, or 0 for success
2800  *
2801  * This logs an error with the wrapping net_device if one occurred,
2802  * and removes and frees the I/O buffer from its TX queue. The
2803  * provided retry information is used to tune our transmission rate.
2804  *
2805  * If the packet did not need to be retransmitted because it was
2806  * properly ACKed the first time, @a retries should be 0.
2807  */
2809  struct io_buffer *iob, int retries, int rc )
2810 {
2811  /* Update rate-control algorithm */
2812  if ( dev->rctl )
2813  rc80211_update_tx ( dev, retries, rc );
2814 
2815  /* Pass completion onward */
2816  netdev_tx_complete_err ( dev->netdev, iob, rc );
2817 }
2818 
2819 /** Common 802.11 errors */
2820 struct errortab common_wireless_errors[] __errortab = {
2829 };
2830 
2831 /* Drag in objects via net80211_ll_protocol */
2832 REQUIRING_SYMBOL ( net80211_ll_protocol );
2833 
2834 /* Drag in 802.11 configuration */
2835 REQUIRE_OBJECT ( config_net80211 );
u32 ticks_start
Time probe was started.
Definition: net80211.c:77
u16 ethertype
Ethernet Type field.
Definition: ieee80211.h:333
#define LL_MULTICAST
Packet is a multicast (including broadcast) packet.
Definition: netdevice.h:105
struct net80211_frag_cache frags[NET80211_NR_CONCURRENT_FRAGS]
Fragment reassembly state.
Definition: net80211.h:994
uint16_t u16
Definition: stdint.h:21
#define iob_pull(iobuf, len)
Definition: iobuf.h:98
CPU sleeping.
long fetch_intz_setting(struct settings *settings, const struct setting *setting)
Fetch value of signed integer setting, or zero.
Definition: settings.c:1057
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define LQ_SMOOTH
Smoothing factor (1-7) for link quality calculation.
Definition: net80211.c:2401
unsigned short uint16_t
Definition: stdint.h:11
#define DBGP_HD(...)
Definition: compiler.h:534
static void netdev_tx_complete(struct net_device *netdev, struct io_buffer *iobuf)
Complete network transmission.
Definition: netdevice.h:746
Structure representing a probed network.
Definition: net80211.h:1056
void net80211_free(struct net80211_device *dev)
Free 802.11 device.
Definition: net80211.c:838
The 802.2 LLC/SNAP header sent before actual data in a data frame.
Definition: ieee80211.h:324
struct net80211_channel channels[NET80211_MAX_CHANNELS]
List of RF channels supported by the card.
Definition: net80211.h:498
enum net80211_crypto_alg crypto
Cryptographic algorithm used on the network.
Definition: net80211.h:1087
#define iob_put(iobuf, len)
Definition: iobuf.h:116
Error message tables.
#define TICKS_PER_SEC
Number of ticks per second.
Definition: timer.h:15
void netdev_rx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard received packet.
Definition: netdevice.c:501
struct net80211_probe_ctx * probe
Definition: net80211.h:874
No security handshaking.
Definition: net80211.h:102
static int net80211_netdev_open(struct net_device *netdev)
Open 802.11 device and start association.
Definition: net80211.c:256
u64 last_beacon_timestamp
TSFT value for last beacon received, microseconds.
Definition: net80211.h:968
static union ieee80211_ie * ieee80211_next_ie(union ieee80211_ie *ie, void *end)
Advance to next 802.11 information element.
Definition: ieee80211.h:1028
#define IEEE80211_STATUS_FAILURE
Definition: ieee80211.h:453
struct net80211_probe_ctx * net80211_probe_start(struct net80211_device *dev, const char *essid, int active)
Begin probe of 802.11 networks.
Definition: net80211.c:1290
u8 channel
The channel currently in use, as an index into the channels array.
Definition: net80211.h:812
static void net80211_netdev_close(struct net_device *netdev)
Close 802.11 device.
Definition: net80211.c:283
int(* open)(struct net_device *netdev)
Open network device.
Definition: netdevice.h:222
#define IEEE80211_IE_COUNTRY
Information element ID for Country information element.
Definition: ieee80211.h:668
int eth_eth_addr(const void *ll_addr, void *eth_addr)
Generate Ethernet-compatible compressed link-layer address.
Definition: ethernet.c:222
int(* config)(struct net80211_device *dev, int changed)
Update hardware state to match 802.11 layer state.
Definition: net80211.h:381
Context for the association task.
Definition: net80211.c:96
u8 in_use
Whether this cache entry is in use.
Definition: net80211.h:536
Error codes.
#define IEEE80211_CAPAB_SHORT_PMBL
Set if PHY supports short preambles on 802.11b.
Definition: ieee80211.h:404
Dummy value used when the cryptosystem can't be detected.
Definition: net80211.h:177
int hop_step
Channels to hop by when changing channel.
Definition: net80211.c:89
#define NET80211_AUTO_SSID
Whether this association was performed using a broadcast SSID.
Definition: net80211.h:234
u32 basic_rates
Bitmask of basic rates.
Definition: net80211.h:843
#define iob_push(iobuf, len)
Definition: iobuf.h:80
union ieee80211_ie info_element[0]
List of information elements.
Definition: ieee80211.h:1134
char ssid[0]
SSID data, not NUL-terminated.
Definition: ieee80211.h:16
u8 first
Differentiator between band and ext triplets.
Definition: ieee80211.h:644
char essid[IEEE80211_MAX_SSID_LEN+1]
The human-readable ESSID (network name)
Definition: net80211.h:1064
void net80211_rx_err(struct net80211_device *dev, struct io_buffer *iob, int rc)
Indicate an error in receiving a packet.
Definition: net80211.c:2788
#define EINFO_ECONNRESET_4WAY_TIMEOUT
Definition: net80211_err.h:423
u16 fc
802.11 Frame Control field
Definition: ieee80211.h:14
#define IEEE80211_LLC_SSAP
Value for SSAP field in 802.2 LLC header for 802.11 frames: SNAP.
Definition: ieee80211.h:340
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
#define IEEE80211_CAPAB_PRIVACY
Set if the network is encrypted (by any method)
Definition: ieee80211.h:401
void(* poll)(struct net80211_device *dev)
Poll for completed and received packets.
Definition: net80211.h:357
#define IEEE80211_STYPE_ASSOC_RESP
Subtype value for association-response management frames.
Definition: ieee80211.h:125
#define IEEE80211_REASON_UNSPECIFIED
Definition: ieee80211.h:512
struct errortab common_wireless_errors [] __errortab
Common 802.11 errors.
Definition: net80211.c:2820
void net80211_rx(struct net80211_device *dev, struct io_buffer *iob, int signal, u16 rate)
Handle receipt of 802.11 frame.
Definition: net80211.c:2689
uint8_t type
Type.
Definition: ena.h:16
u8 rtscts_rate
The rate to use for RTS/CTS transmissions.
Definition: net80211.h:831
#define __einfo_errortab(einfo)
Definition: errortab.h:23
void net80211_unregister(struct net80211_device *dev)
Unregister 802.11 device from network stack.
Definition: net80211.c:824
Constants and data structures defined in IEEE 802.11, subsetted according to what iPXE knows how to u...
struct list_head mgmt_info_queue
RX management packet info queue.
Definition: net80211.h:1034
enum net80211_security_proto handshaking
Security handshaking method used on the network.
Definition: net80211.h:1084
#define IEEE80211_AUTH_SHARED_KEY
Shared Key authentication algorithm.
Definition: ieee80211.h:1157
uint8_t eth_broadcast[ETH_ALEN]
Ethernet broadcast MAC address.
Definition: ethernet.c:47
#define DBGC(...)
Definition: compiler.h:505
#define ASSOC_TIMEOUT
Number of ticks to wait for replies to association management frames.
Definition: net80211.c:1636
u32 ticks_beacon
Time last useful beacon was received.
Definition: net80211.c:80
An 802.11 data or management frame without QoS or WDS header fields.
Definition: ieee80211.h:300
A process descriptor.
Definition: process.h:31
char name[2]
ISO Alpha2 country code.
Definition: ieee80211.h:660
#define ENOENT
No such file or directory.
Definition: errno.h:514
const uint8_t * ll_broadcast
Link-layer broadcast address.
Definition: netdevice.h:383
net80211_security_proto
An 802.11 security handshaking protocol.
Definition: net80211.h:96
#define NET80211_STATUS_MASK
An error code indicating the failure mode, or 0 if successful.
Definition: net80211.h:185
static struct list_head net80211_devices
List of 802.11 devices.
Definition: net80211.c:47
A settings applicator.
Definition: settings.h:251
#define IEEE80211_FC_FROMDS
802.11 Frame Control field: From Data System flag
Definition: ieee80211.h:237
#define EINVAL_ACTIVE_SCAN
Definition: net80211_err.h:39
#define EINVAL_PKT_TOO_SHORT
Definition: net80211_err.h:21
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition: netdevice.c:186
u16 net80211_duration(struct net80211_device *dev, int bytes, u16 rate)
Calculate one frame's contribution to 802.11 duration field.
Definition: net80211.c:441
union ieee80211_ie * rsn_ie
RSN or WPA information element to include with association.
Definition: net80211.h:932
iPXE timers
union net80211_device::@549 ctx
Context for the association process.
#define NET80211_PROBE_TIMEOUT
Seconds to allow a probe to take if no network has been found.
Definition: net80211.c:1269
#define IEEE80211_FC_VERSION
802.11 Frame Control field, Version bitmask
Definition: ieee80211.h:90
u16 aid
Association ID given to us by the AP.
Definition: net80211.h:965
static void net80211_filter_hw_channels(struct net80211_device *dev)
Filter 802.11 device channels for hardware capabilities.
Definition: net80211.c:961
enum net80211_hw_info::@547 flags
A set of flags indicating peculiarities of this device.
struct list_head list
List of 802.11 devices.
Definition: net80211.h:792
REQUIRING_SYMBOL(net80211_ll_protocol)
uint8_t method
Definition: ib_mad.h:14
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
Structure tracking received fragments for a packet.
Definition: net80211.h:533
void rc80211_update_tx(struct net80211_device *dev, int retries, int rc)
Update rate-control state for transmitted packet.
Definition: rc80211.c:316
void net80211_deauthenticate(struct net80211_device *dev, int rc)
Deauthenticate from current network and try again.
Definition: net80211.c:2390
struct list_head mgmt_queue
RX management packet queue.
Definition: net80211.h:1021
u32 ticks_channel
Time channel was last changed.
Definition: net80211.c:83
Dummy value used when the handshaking type can't be detected.
Definition: net80211.h:124
void process_del(struct process *process)
Remove process from process list.
Definition: process.c:79
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:128
void eth_init_addr(const void *hw_addr, void *ll_addr)
Initialise Ethernet address.
Definition: ethernet.c:150
#define EINFO_ECONNREFUSED_ASSOC_DENIED
Definition: net80211_err.h:92
static int net80211_send_disassoc(struct net80211_device *dev, int reason, int deauth)
Send 802.11 disassociation frame.
Definition: net80211.c:2361
#define IEEE80211_STYPE_AUTH
Subtype value for authentication management frames.
Definition: ieee80211.h:188
Definition: hw.c:16
u16 tx_seq
Sequence number of this frame; first from client to AP is 1.
Definition: ieee80211.h:1144
#define NET80211_REG_TXPOWER
Maximum TX power to allow (dBm), if we don't get a regulatory hint.
Definition: net80211.h:287
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:577
Context for a probe operation.
Definition: net80211.c:63
char essid[IEEE80211_MAX_SSID_LEN+1]
SSID of the access point we are or will be associated with.
Definition: net80211.h:962
A link-layer protocol.
Definition: netdevice.h:114
#define NET80211_WORKING
Whether the auto-association task is running.
Definition: net80211.h:212
Address Resolution Protocol constants and types.
int eth_eui64(const void *ll_addr, void *eui64)
Generate EUI-64 address.
Definition: ethernet.c:234
Definitions for general secured-network routines.
#define NET80211_PROBE_GATHER_ALL
Seconds to wait after finding a network, to possibly find other networks.
Definition: net80211.c:1266
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
#define EINFO_ECONNRESET_8021X_FAILURE
Definition: net80211_err.h:479
const char * essid
If non-"", the ESSID to limit ourselves to.
Definition: net80211.c:74
static int net80211_netdev_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet on 802.11 device.
Definition: net80211.c:320
int(* open)(struct net80211_device *dev)
Open 802.11 device.
Definition: net80211.h:305
A doubly-linked list entry (or list head)
Definition: list.h:18
#define NET80211_BAND_BIT_5GHZ
Bitmask for the 5GHz band.
Definition: net80211.h:54
const char * name
Name.
Definition: settings.h:28
802.11 Country information element regulatory triplet
Definition: ieee80211.h:642
uint32_t start
Starting offset.
Definition: netvsc.h:12
u32 tx_beacon_interval
Time between AP sending beacons, microseconds.
Definition: net80211.h:971
static struct net80211_device_operations net80211_null_ops
Set of device operations that does nothing.
Definition: net80211.c:50
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
#define NET80211_BAND_2GHZ
The 2.4 GHz ISM band, unlicensed in most countries.
Definition: net80211.h:45
uint8_t status
Status.
Definition: ena.h:16
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
static void net80211_step_associate(struct net80211_device *dev)
Step 802.11 association process.
Definition: net80211.c:1646
#define IEEE80211_MAKESEQ(seqnr, frag)
Make 802.11 Sequence Control field from sequence and fragment numbers.
Definition: ieee80211.h:286
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition: netdevice.h:498
struct net_device * alloc_netdev(size_t priv_len)
Allocate network device.
Definition: netdevice.c:622
#define NET80211_IS_REASON
Whether the error code provided is a "reason" code, not a "status" code.
Definition: net80211.h:188
const char * name
Protocol name.
Definition: netdevice.h:116
u16 rates[NET80211_NR_BANDS][NET80211_MAX_RATES]
List of transmission rates supported by the card, indexed by band.
Definition: net80211.h:508
static int net80211_ll_pull(struct net_device *netdev, struct io_buffer *iobuf, const void **ll_dest, const void **ll_source, uint16_t *net_proto, unsigned int *flags)
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
#define IEEE80211_THIS_VERSION
Expected value of Version bits in Frame Control.
Definition: ieee80211.h:93
void * tail
End of data.
Definition: iobuf.h:46
#define IEEE80211_IE_EXT_RATES
Information element ID for extended rates information element.
Definition: ieee80211.h:603
Operations that must be implemented by an 802.11 driver.
Definition: net80211.h:293
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:208
void net80211_set_rate_idx(struct net80211_device *dev, int rate)
Set data transmission rate for 802.11 device.
Definition: net80211.c:2000
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * priv
Pointer to private data.
Definition: net80211.h:672
#define IEEE80211_FC_SUBTYPE
802.11 Frame Control field, Frame Subtype bitmask
Definition: ieee80211.h:110
static void net80211_handle_assoc_reply(struct net80211_device *dev, struct io_buffer *iob)
Handle receipt of 802.11 association reply frame.
Definition: net80211.c:2326
u16 status
Status code.
Definition: ieee80211.h:1147
#define IEEE80211_STYPE_DEAUTH
Subtype value for deauthentication management frames.
Definition: ieee80211.h:198
union ieee80211_ie_country_triplet triplet[0]
List of regulatory triplets.
Definition: ieee80211.h:664
struct ieee80211_ie_ds_param ds_param
DS parameter set.
Definition: ieee80211.h:991
static struct process_descriptor net80211_process_desc
802.11 association process descriptor
Definition: net80211.c:738
int started
Whether start has been called.
Definition: net80211.h:665
int(* init)(struct net80211_device *dev)
Initialize security handshaking protocol.
Definition: net80211.h:585
#define IEEE80211_STYPE_REASSOC_RESP
Subtype value for reassociation-response management frames.
Definition: ieee80211.h:141
static int netdev_is_open(struct net_device *netdev)
Check whether or not network device is open.
Definition: netdevice.h:652
u16 hw_value
Hardware channel value.
Definition: net80211.h:414
struct net80211_crypto * crypto
802.11 cryptosystem for our current network
Definition: net80211.h:940
#define EINVAL_PKT_LLC_HEADER
Definition: net80211_err.h:33
u8 oui[3]
Organization code, usually 0.
Definition: ieee80211.h:332
u8 id
Information element ID.
Definition: ieee80211.h:976
#define DBGP(...)
Definition: compiler.h:532
Information on the capabilities of an 802.11 hardware device.
Definition: net80211.h:436
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:555
u8 maxpower
Maximum allowable transmit power, in dBm.
Definition: net80211.h:425
Ethernet protocol.
struct process proc_assoc
The asynchronous association process.
Definition: net80211.h:858
u8 nr_channels
The number of channels in the channels array.
Definition: net80211.h:809
struct list_head * beacons
List of best beacons for each network found so far.
Definition: net80211.c:92
#define IEEE80211_STATUS_SUCCESS
Definition: ieee80211.h:452
#define IEEE80211_STYPE_DATA
Subtype value for ordinary data frames, with no QoS or CF add-ons.
Definition: ieee80211.h:219
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
void * priv
Driver private data.
Definition: netdevice.h:425
u8 dsap
Destination SAP ID.
Definition: ieee80211.h:327
#define IEEE80211_TYP_FRAME_HEADER_LEN
Frame header length for frames we might work with.
Definition: ieee80211.h:60
struct rc80211_ctx * rctl
Rate control state.
Definition: net80211.h:989
REQUIRE_OBJECT(config_net80211)
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
u8 nr_channels
Number of contiguous channels in band.
Definition: ieee80211.h:633
#define EINVAL_PKT_NOT_DATA
Definition: net80211_err.h:27
Information associated with a received management packet.
Definition: net80211.c:57
struct io_buffer * net80211_mgmt_dequeue(struct net80211_device *dev, int *signal)
Get 802.11 management frame.
Definition: net80211.c:667
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition: netdevice.h:768
int nr_rates[NET80211_NR_BANDS]
Number of supported rates, indexed by band.
Definition: net80211.h:511
u16 algorithm
Authentication algorithm (Open System or Shared Key)
Definition: ieee80211.h:1141
void(* stop)(struct net80211_device *dev)
Stop security handshaking handlers.
Definition: net80211.h:650
static union ieee80211_ie * net80211_marshal_request_info(struct net80211_device *dev, union ieee80211_ie *ie)
Create information elements for outgoing probe or association packet.
Definition: net80211.c:1202
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition: timer.c:60
#define SETTING_NETDEV_EXTRA
Network device additional settings.
Definition: settings.h:64
int last_signal
Signal strength of last received packet.
Definition: net80211.h:986
struct net80211_crypto * gcrypto
802.11 cryptosystem for multicast and broadcast frames
Definition: net80211.h:951
int keep_mgmt
Whether to store management packets.
Definition: net80211.h:1046
static void net80211_rx_frag(struct net80211_device *dev, struct io_buffer *iob, int signal)
Handle receipt of 802.11 fragment.
Definition: net80211.c:2596
static struct net_device * netdev
Definition: gdbudp.c:52
void(* irq)(struct net80211_device *dev, int enable)
Enable or disable interrupts.
Definition: net80211.h:364
static int net80211_check_settings_update(void)
Check for 802.11 SSID or key updates.
Definition: net80211.c:1891
struct io_buffer * probe
If scanning actively, pointer to probe packet to send.
Definition: net80211.c:71
int eth_mc_hash(unsigned int af, const void *net_addr, void *ll_addr)
Hash multicast address.
Definition: ethernet.c:193
static int is_multicast_ether_addr(const void *addr)
Check if Ethernet address is a multicast address.
Definition: ethernet.h:37
#define ieee80211_beacon
Definition: ieee80211.h:1069
int fetch_string_setting(struct settings *settings, const struct setting *setting, char *data, size_t len)
Fetch value of string setting.
Definition: settings.c:845
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:296
#define NET80211_MAX_CHANNELS
The maximum number of channels we allow to be configured simultaneously.
Definition: net80211.h:275
uint32_t channel
RNDIS channel.
Definition: netvsc.h:14
__weak int sec80211_detect(struct io_buffer *iob __unused, enum net80211_security_proto *secprot __unused, enum net80211_crypto_alg *crypt __unused)
Detect secure 802.11 network when security support is not available.
Definition: net80211.c:112
int(* change_key)(struct net80211_device *dev)
Change cryptographic key based on setting.
Definition: net80211.h:639
#define EFAULT
Bad address.
Definition: errno.h:393
int net80211_prepare_assoc(struct net80211_device *dev, struct net80211_wlan *wlan)
Prepare 802.11 device channel and rate set for communication.
Definition: net80211.c:2105
struct net80211_device * dev
802.11 device to probe on
Definition: net80211.c:65
#define IEEE80211_STATUS_AUTH_ALGO_UNSUPP
Definition: ieee80211.h:457
static void net80211_handle_auth(struct net80211_device *dev, struct io_buffer *iob)
Handle receipt of 802.11 authentication frame.
Definition: net80211.c:2227
int(* transmit)(struct net80211_device *dev, struct io_buffer *iobuf)
Transmit packet on 802.11 network device.
Definition: net80211.h:341
u8 rates[0]
Rates data.
Definition: ieee80211.h:980
Probe request frame data.
Definition: ieee80211.h:1131
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:844
static int net80211_rate_is_erp(u16 rate)
Determine whether a transmission rate uses ERP/OFDM.
Definition: net80211.c:399
int net80211_keep_mgmt(struct net80211_device *dev, int enable)
Set state of 802.11 device keeping management frames.
Definition: net80211.c:646
static void * dest
Definition: strings.h:176
#define NET80211_BAND_5GHZ
The band from 4.9 GHz to 5.7 GHz, which tends to be more restricted.
Definition: net80211.h:47
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:447
struct list_head * net80211_probe_finish_all(struct net80211_probe_ctx *ctx)
Finish probe of 802.11 networks, returning all networks found.
Definition: net80211.c:1585
#define NET80211_PHY_USE_SHORT_PREAMBLE
Whether to use 802.11b short preamble operation.
Definition: net80211.h:260
#define NET80211_WAITING
Whether the auto-association task is waiting for a reply from the AP.
Definition: net80211.h:215
static int net80211_ll_push(struct net_device *netdev, struct io_buffer *iobuf, const void *ll_dest, const void *ll_source, uint16_t net_proto)
Add 802.11 link-layer header.
Definition: net80211.c:483
u32 hop_time
Time to stay on each channel.
Definition: net80211.c:86
#define IEEE80211_IE_SSID
Information element ID for SSID information element.
Definition: ieee80211.h:582
void process_add(struct process *process)
Add process to process list.
Definition: process.c:59
u8 nr_rates
The number of transmission rates in the rates array.
Definition: net80211.h:821
Configuration settings.
#define NET80211_FRAG_TIMEOUT
Seconds we'll wait to get all fragments of a packet.
Definition: net80211.h:278
#define IEEE80211_FC_PROTECTED
802.11 Frame Control field: Protected flag
Definition: ieee80211.h:264
#define iob_unput(iobuf, len)
Definition: iobuf.h:131
#define NET80211_PHY_USE_PROTECTION
Whether to use RTS/CTS or CTS-to-self protection for transmissions.
Definition: net80211.h:251
#define NET80211_PROBE_GATHER
Seconds to wait after finding a network, to possibly find better APs for it.
Definition: net80211.c:1259
#define IEEE80211_IE_VENDOR
Information element ID for Vendor Specific information element.
Definition: ieee80211.h:960
struct io_buffer *(* decrypt)(struct net80211_crypto *crypto, struct io_buffer *iob)
Decrypt a frame using the cryptosystem.
Definition: net80211.h:759
struct net80211_device * net80211_alloc(size_t priv_size)
Allocate 802.11 device.
Definition: net80211.c:754
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
int assoc_rc
Return status code associated with state.
Definition: net80211.h:924
struct refcnt refcnt
Reference counter.
Definition: netdevice.h:350
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static struct net_device_operations net80211_netdev_ops
Network device operations for a wrapped 802.11 device.
Definition: net80211.c:379
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
#define NET80211_HANDSHAKERS
Definition: net80211.h:675
u16 center_freq
The center frequency for this channel.
Definition: net80211.h:411
The iPXE 802.11 MAC layer.
struct io_buffer * iob[16]
Buffers for each fragment.
Definition: net80211.h:545
#define IEEE80211_FC_TYPE
802.11 Frame Control field, Frame Type bitmask
Definition: ieee80211.h:97
struct net80211_assoc_ctx * assoc
Definition: net80211.h:875
u8 band
The band with which this channel is associated.
Definition: net80211.h:388
#define NET80211_CFG_PHY_PARAMS
Low-level link parameters (short preamble, protection, etc) have changed.
Definition: net80211.h:90
#define E80211_STATUS(stat)
Make return status code from 802.11 status code.
Definition: net80211_err.h:555
u32 start_ticks
Timestamp from point at which first fragment was collected.
Definition: net80211.h:542
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:667
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
const char * eth_ntoa(const void *ll_addr)
Transcribe Ethernet address.
Definition: ethernet.c:175
#define NET80211_ASSOCIATED
Whether we have successfully associated with the network.
Definition: net80211.h:201
#define IEEE80211_IE_ERP_INFO
Information element ID for ERP Information information element.
Definition: ieee80211.h:768
static void net80211_set_rtscts_rate(struct net80211_device *dev)
Pick TX rate for RTS/CTS packets based on data rate.
Definition: net80211.c:1967
Structure encapsulating the complete state of an 802.11 device.
Definition: net80211.h:786
static int net80211_process_capab(struct net80211_device *dev, u16 capab)
Update 802.11 device state to reflect received capabilities field.
Definition: net80211.c:1010
#define IEEE80211_IE_RSN
Information element ID for Robust Security Network information element.
Definition: ieee80211.h:834
uint8_t * tmp
Definition: entropy.h:156
#define IEEE80211_LLC_DSAP
Value for DSAP field in 802.2 LLC header for 802.11 frames: SNAP.
Definition: ieee80211.h:337
An 802.11 RF channel.
Definition: net80211.h:385
No security, an "Open" network.
Definition: net80211.h:131
#define IEEE80211_MAX_DATA_LEN
Maximum length of frame payload.
Definition: ieee80211.h:28
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:358
int net80211_probe_step(struct net80211_probe_ctx *ctx)
Continue probe of 802.11 networks.
Definition: net80211.c:1363
A network device.
Definition: netdevice.h:348
void netdev_link_err(struct net_device *netdev, int rc)
Mark network device as having a specific link state.
Definition: netdevice.c:163
void net80211_free_wlanlist(struct list_head *list)
Free list of WLAN structures.
Definition: net80211.c:1619
int old_keep_mgmt
Value of keep_mgmt before probe was started.
Definition: net80211.c:68
#define EINFO_ECONNREFUSED_AUTH_ALGO_UNSUPP
Definition: net80211_err.h:99
void net80211_autoassociate(struct net80211_device *dev)
Start 802.11 association process.
Definition: net80211.c:1929
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition: netdevice.h:511
int netdev_tx(struct net_device *netdev, struct io_buffer *iobuf)
Transmit raw packet via network device.
Definition: netdevice.c:290
int method
Next authentication method to try using.
Definition: net80211.c:98
static int ieee80211_ie_bound(union ieee80211_ie *ie, void *end)
Check that 802.11 information element is bounded by buffer.
Definition: ieee80211.h:1012
#define IEEE80211_TYPE_CTRL
Type value for control (layer-1, hardware-managed) frames.
Definition: ieee80211.h:103
u8 first_channel
Channel number for first channel in band.
Definition: ieee80211.h:632
struct net80211_handshaker * handshaker
Security handshaker being used.
Definition: net80211.h:879
#define IEEE80211_REASON_LEAVING
Definition: ieee80211.h:514
const struct setting net80211_ssid_setting __setting(SETTING_NETDEV_EXTRA, ssid)
The network name to associate with.
void net80211_free_wlan(struct net80211_wlan *wlan)
Free WLAN structure.
Definition: net80211.c:1605
int last_packet
Time (in ticks) of the last sent association-related packet.
Definition: net80211.c:101
struct list_head list
Definition: net80211.c:59
#define IEEE80211_STATUS_AUTH_CHALL_INVALID
Definition: ieee80211.h:459
#define NET80211_NO_ASSOC
Whether the auto-association task should be suppressed.
Definition: net80211.h:223
u16 capability
Capability flags.
Definition: ieee80211.h:1086
struct net80211_hw_info * hw
Information about the hardware, provided to net80211_register()
Definition: net80211.h:801
#define EINVAL_PKT_VERSION
Definition: net80211_err.h:24
#define IEEE80211_STYPE_ASSOC_REQ
Subtype value for association-request management frames.
Definition: ieee80211.h:118
static void process_init_stopped(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process without adding to process list.
Definition: process.h:145
void net80211_tx_complete(struct net80211_device *dev, struct io_buffer *iob, int retries, int rc)
Indicate the completed transmission of a packet.
Definition: net80211.c:2808
#define ETH_ALEN
Definition: if_ether.h:8
struct settings_applicator net80211_applicator __settings_applicator
802.11 settings applicator
Definition: net80211.c:198
Any 802.11 information element.
Definition: ieee80211.h:972
int net80211_send_assoc(struct net80211_device *dev, struct net80211_wlan *wlan)
Send 802.11 association frame.
Definition: net80211.c:2287
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
u16 rates[NET80211_MAX_RATES]
A list of all possible TX rates we might use.
Definition: net80211.h:818
struct net80211_device * net80211_get(struct net_device *netdev)
Get 802.11 device from wrapping network device.
Definition: net80211.c:624
struct net80211_wlan * net80211_probe_finish_best(struct net80211_probe_ctx *ctx)
Finish probe of 802.11 networks, returning best-signal network found.
Definition: net80211.c:1544
u8 len
Information element data length.
Definition: ieee80211.h:977
struct net80211_wlan * associating
Network with which we are associating.
Definition: net80211.h:866
A setting.
Definition: settings.h:23
static fd_set * set
Definition: posix_io.h:48
int phy_flags
Physical layer options.
Definition: net80211.h:983
#define IEEE80211_STYPE_PROBE_RESP
Subtype value for probe-response management frames.
Definition: ieee80211.h:157
Interface to an 802.11 security handshaking protocol.
Definition: net80211.h:564
static struct ll_protocol net80211_ll_protocol __ll_protocol
802.11 link-layer protocol
Definition: net80211.c:597
struct ieee80211_ie_country country
Country information.
Definition: ieee80211.h:994
struct rc80211_ctx * rc80211_init(struct net80211_device *dev __unused)
Initialize rate-control algorithm.
Definition: rc80211.c:154
Network device operations.
Definition: netdevice.h:213
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition: netdevice.c:470
struct device * dev
Underlying hardware device.
Definition: netdevice.h:360
#define IEEE80211_FC_TODS
802.11 Frame Control field: To Data System flag
Definition: ieee80211.h:229
struct list_head list
Link to allow chaining multiple structures into a list to be returned from net80211_probe_finish_all(...
Definition: net80211.h:1091
void rc80211_free(struct rc80211_ctx *ctx)
Free rate-control context.
Definition: rc80211.c:369
#define LL_BROADCAST
Packet is a broadcast packet.
Definition: netdevice.h:108
uint32_t hdr
Message header.
Definition: intelvf.h:12
Network device management.
int(* start)(struct net80211_device *dev)
Start handshaking.
Definition: net80211.h:598
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
struct ieee80211_ie_channels_channel_band channels[0]
List of (start, length) channel bands we can use.
Definition: ieee80211.h:18
#define IEEE80211_ERP_USE_PROTECTION
ERP information element: Flag set if CTS protection must be used.
Definition: ieee80211.h:774
struct net_device * netdev
The net_device that wraps us.
Definition: net80211.h:789
FILE_LICENCE(GPL2_OR_LATER)
static void net80211_free_frags(struct net80211_device *dev, int fcid)
Free buffers used by 802.11 fragment cache entry.
Definition: net80211.c:2533
#define NET80211_AUTHENTICATED
Whether we have successfully authenticated with the network.
Definition: net80211.h:198
#define IEEE80211_CAPAB_SHORT_SLOT
Set if PHY supports short slot time on 802.11g.
Definition: ieee80211.h:419
#define IEEE80211_STYPE_DISASSOC
Subtype value for disassociation management frames.
Definition: ieee80211.h:178
#define IEEE80211_AUTH_OPEN_SYSTEM
Open System authentication algorithm.
Definition: ieee80211.h:1154
int channel
The channel on which that access point communicates.
Definition: net80211.h:1078
#define iob_reserve(iobuf, len)
Definition: iobuf.h:63
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
#define EINVAL_CRYPTO_REQUEST
Definition: net80211_err.h:36
static size_t iob_headroom(struct io_buffer *iobuf)
Calculate available space at start of an I/O buffer.
Definition: iobuf.h:161
#define ieee80211_assoc_resp
Definition: ieee80211.h:1111
void netdev_tx_complete_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Complete network transmission.
Definition: netdevice.c:395
#define IEEE80211_ERP_BARKER_LONG
ERP information element: Flag set if long preambles must be used.
Definition: ieee80211.h:777
#define IEEE80211_STYPE_REASSOC_REQ
Subtype value for reassociation-request management frames.
Definition: ieee80211.h:133
Authentication frame data.
Definition: ieee80211.h:1138
#define IEEE80211_FC_MORE_FRAG
802.11 Frame Control field: More Fragments flag
Definition: ieee80211.h:240
#define NET80211_MAX_RATES
The maximum number of TX rates we allow to be configured simultaneously.
Definition: net80211.h:272
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:39
#define IEEE80211_FRAG(seq)
Extract fragment number from 802.11 Sequence Control field.
Definition: ieee80211.h:283
static volatile void * bits
Definition: bitops.h:27
#define EINVAL_PKT_NOT_FROMDS
Definition: net80211_err.h:30
uint32_t len
Length.
Definition: ena.h:14
int net80211_tx_mgmt(struct net80211_device *dev, u16 fc, u8 dest[6], struct io_buffer *iob)
Transmit 802.11 management frame.
Definition: net80211.c:706
#define DBGC2(...)
Definition: compiler.h:522
#define IEEE80211_IE_DS_PARAM
Information element ID for Direct Spectrum parameter information element.
Definition: ieee80211.h:620
#define IEEE80211_SEQNR(seq)
Extract sequence number from 802.11 Sequence Control field.
Definition: ieee80211.h:280
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:157
void * head
Start of the buffer.
Definition: iobuf.h:42
u8 ctrl
Control information.
Definition: ieee80211.h:329
u8 ssap
Source SAP ID.
Definition: ieee80211.h:328
struct net80211_device_operations * op
802.11 device operations
Definition: net80211.h:795
#define EINFO_ECONNREFUSED_FAILURE
Definition: net80211_err.h:71
static void net80211_set_state(struct net80211_device *dev, short clear, short set, u16 status)
Set state of 802.11 device.
Definition: net80211.c:866
static struct io_buffer * net80211_accum_frags(struct net80211_device *dev, int fcid, int nfrags, int size)
Accumulate 802.11 fragments into one I/O buffer.
Definition: net80211.c:2561
net80211_crypto_alg
An 802.11 data encryption algorithm.
Definition: net80211.h:129
#define NET80211_BAND_BIT_2GHZ
Bitmask for the 2GHz band.
Definition: net80211.h:52
void(* close)(struct net80211_device *dev)
Close 802.11 network device.
Definition: net80211.h:315
#define IEEE80211_CAPAB_MANAGED
Set if using an Access Point (managed mode)
Definition: ieee80211.h:389
int signal
Signal strength of beacon frame from that access point.
Definition: net80211.h:1070
u8 erp_info
ERP information flags.
Definition: ieee80211.h:984
#define E80211_REASON(reas)
Make return status code from 802.11 reason code.
Definition: net80211_err.h:598
void * data
Start of data.
Definition: iobuf.h:44
#define NET80211_PROBED
Whether we have found the network we will be associating with.
Definition: net80211.h:191
#define PROC_DESC(object_type, process, _step)
Define a process descriptor.
Definition: process.h:82
int nr_channels
Number of supported channels.
Definition: net80211.h:501
static int is_broadcast_ether_addr(const void *addr)
Check if Ethernet address is the broadcast address.
Definition: ethernet.h:61
struct net80211_channel channels[NET80211_MAX_CHANNELS]
A list of all possible channels we might use.
Definition: net80211.h:806
u16 last_rx_seq
Packet duplication elimination state.
Definition: net80211.h:1008
#define IEEE80211_STYPE_ACTION
Subtype value for action management frames.
Definition: ieee80211.h:205
#define ieee80211_disassoc
Definition: ieee80211.h:1079
struct ieee80211_ie_country_band_triplet band
Information about a band of channels.
Definition: ieee80211.h:647
Association request frame data.
Definition: ieee80211.h:1083
#define EINFO_ECONNRESET_INACTIVITY
Definition: net80211_err.h:353
uint8_t fill
Length pair.
Definition: deflate.h:12
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
uint16_t reason
Rejection reason.
Definition: ib_mad.h:20
int(* step)(struct net80211_device *dev)
Process handshaking state.
Definition: net80211.h:616
#define __weak
Declare a function as weak (use before the definition)
Definition: compiler.h:219
#define EINFO_ECONNRESET_UNSPECIFIED
Definition: net80211_err.h:332
#define ENOSYS
Function not implemented.
Definition: errno.h:564
static void net80211_netdev_irq(struct net_device *netdev, int enable)
Enable or disable interrupts for 802.11 device.
Definition: net80211.c:370
#define NET80211_CFG_RATE
Requested transmission rate (dev->rate) has changed.
Definition: net80211.h:84
#define IEEE80211_LLC_HEADER_LEN
Length of LLC/SNAP headers on data frames.
Definition: ieee80211.h:31
static void net80211_handle_mgmt(struct net80211_device *dev, struct io_buffer *iob, int signal)
Handle receipt of 802.11 management frame.
Definition: net80211.c:2438
struct io_buffer *(* encrypt)(struct net80211_crypto *crypto, struct io_buffer *iob)
Encrypt a frame using the cryptosystem.
Definition: net80211.h:733
u8 bssid[ETH_ALEN]
MAC address of the strongest-signal access point for this ESSID.
Definition: net80211.h:1067
int(* apply)(void)
Apply updated settings.
Definition: settings.h:256
#define IEEE80211_LLC_CTRL
Value for control field in 802.2 LLC header for 802.11 frames.
Definition: ieee80211.h:346
The iPXE 802.11 MAC layer errors.
char ssid[0]
SSID text.
Definition: ieee80211.h:979
static void net80211_add_channels(struct net80211_device *dev, int start, int len, int txpower)
Add channels to 802.11 device.
Definition: net80211.c:917
void timeout(int)
int net80211_send_auth(struct net80211_device *dev, struct net80211_wlan *wlan, int method)
Send 802.11 initial authentication frame.
Definition: net80211.c:2200
#define NET80211_CFG_CHANNEL
Channel choice (dev->channel) or regulatory parameters have changed.
Definition: net80211.h:81
int bands
A bitwise OR of the bands on which this device can communicate.
Definition: net80211.h:453
u8 bssid[ETH_ALEN]
MAC address of the access point most recently associated.
Definition: net80211.h:954
u16 state
State of our association to the network.
Definition: net80211.h:921
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:381
u16 seqnr
Sequence number of this MSDU (packet)
Definition: net80211.h:539
size_t max_pkt_len
Maximum packet length.
Definition: netdevice.h:403
#define IEEE80211_STYPE_BEACON
Subtype value for beacon management frames.
Definition: ieee80211.h:168
unsigned long currticks(void)
Get current system time in ticks.
Definition: timer.c:42
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30
#define ASSOC_RETRIES
Number of times to try sending a particular association management frame.
Definition: net80211.c:1639
#define ARPHRD_ETHER
Ethernet 10Mbps.
Definition: if_arp.h:16
#define IEEE80211_IE_RATES
Information element ID for rates information element.
Definition: ieee80211.h:600
union ieee80211_ie info_element[0]
List of information elements.
Definition: ieee80211.h:1092
Interface to an 802.11 cryptosystem.
Definition: net80211.h:689
struct io_buffer * beacon
The complete beacon or probe-response frame received.
Definition: net80211.h:1081
int times_tried
Number of times we have tried sending it.
Definition: net80211.c:104
uint8_t bytes[64]
Definition: ib_mad.h:16
#define NETDEV_IRQ_UNSUPPORTED
Network device interrupts are unsupported.
Definition: netdevice.h:446
u8 channel_nr
A channel number interpreted according to the band.
Definition: net80211.h:405
#define NET80211_PHY_USE_SHORT_SLOT
Whether to use 802.11g short slot operation.
Definition: net80211.h:266
#define ok(success)
Definition: test.h:46
#define ENETUNREACH
Network unreachable.
Definition: errno.h:488
u32 rx_beacon_interval
Smoothed average time between beacons, microseconds.
Definition: net80211.h:974
int net80211_change_channel(struct net80211_device *dev, int channel)
Configure 802.11 device to transmit on a certain channel.
Definition: net80211.c:2021
int net80211_prepare_probe(struct net80211_device *dev, int band, int active)
Prepare 802.11 device channel and rate set for scanning.
Definition: net80211.c:2051
static int net80211_process_ie(struct net80211_device *dev, union ieee80211_ie *ie, void *ie_end)
Update 802.11 device state to reflect received information elements.
Definition: net80211.c:1044
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
unsigned channel_change_time
Estimate of the time required to change channels, in microseconds.
Definition: net80211.h:518
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
#define NET80211_CFG_ASSOC
Association has been established with a new BSS (dev->bssid)
Definition: net80211.h:87
String functions.
#define IEEE80211_MAX_SSID_LEN
Maximum length of an ESSID.
Definition: ieee80211.h:77
#define NET80211_NR_CONCURRENT_FRAGS
The number of fragments we can receive at once.
Definition: net80211.h:284
u8 rate
The rate currently in use, as an index into the rates array.
Definition: net80211.h:824
#define htons(value)
Definition: byteswap.h:135
static void net80211_update_link_quality(struct net80211_device *dev, struct io_buffer *iob)
Update link quality information based on received beacon.
Definition: net80211.c:2410
u16 listen_interval
Interval at which we wake up, in units of the beacon interval.
Definition: ieee80211.h:1089
#define IEEE80211_STYPE_PROBE_REQ
Subtype value for probe-request management frames.
Definition: ieee80211.h:150
uint8_t u8
Definition: stdint.h:19
#define NET80211_CRYPTO_SYNCED
Whether we have completed security handshaking with the network.
Definition: net80211.h:209
union @375 key
Sense key.
Definition: scsi.h:18
uint32_t u32
Definition: stdint.h:23
u8 current_channel
Current channel number, 1-14.
Definition: ieee80211.h:616
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:366
#define IEEE80211_TYPE_DATA
Type value for data frames.
Definition: ieee80211.h:106
#define EINFO_EINVAL_CRYPTO_REQUEST
Definition: net80211_err.h:37
int net80211_register(struct net80211_device *dev, struct net80211_device_operations *ops, struct net80211_hw_info *hw)
Register 802.11 device with network stack.
Definition: net80211.c:791
#define IEEE80211_FC_RETRY
802.11 Frame Control field: Retransmission flag
Definition: ieee80211.h:243
static void net80211_netdev_poll(struct net_device *netdev)
Poll 802.11 device for received packets and completed transmissions.
Definition: net80211.c:356
#define IEEE80211_TYPE_MGMT
Type value for management (layer-2) frames.
Definition: ieee80211.h:100
if(natsemi->flags &NATSEMI_64BIT) return 1
void * memset(void *dest, int character, size_t len) __nonnull
u8 max_txpower
Maximum TX power in dBm.
Definition: ieee80211.h:634
#define IEEE80211_CAPAB_ADHOC
Set if operating in IBSS (no-AP, "Ad-Hoc") mode.
Definition: ieee80211.h:392
A persistent I/O buffer.
Definition: iobuf.h:32
u16 last_tx_seqnr
The sequence number of the last packet we sent.
Definition: net80211.h:997
uint8_t flags
Flags.
Definition: ena.h:18
void rc80211_update_rx(struct net80211_device *dev, int retry, u16 rate)
Update rate-control state for received packet.
Definition: rc80211.c:351