Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Host AP driver Info Frame processing (part of hostap.o module) */
0003 
0004 #include <linux/if_arp.h>
0005 #include <linux/sched.h>
0006 #include <linux/slab.h>
0007 #include <linux/export.h>
0008 #include <linux/etherdevice.h>
0009 #include "hostap_wlan.h"
0010 #include "hostap.h"
0011 #include "hostap_ap.h"
0012 
0013 /* Called only as a tasklet (software IRQ) */
0014 static void prism2_info_commtallies16(local_info_t *local, unsigned char *buf,
0015                       int left)
0016 {
0017     struct hfa384x_comm_tallies *tallies;
0018 
0019     if (left < sizeof(struct hfa384x_comm_tallies)) {
0020         printk(KERN_DEBUG "%s: too short (len=%d) commtallies "
0021                "info frame\n", local->dev->name, left);
0022         return;
0023     }
0024 
0025     tallies = (struct hfa384x_comm_tallies *) buf;
0026 #define ADD_COMM_TALLIES(name) \
0027 local->comm_tallies.name += le16_to_cpu(tallies->name)
0028     ADD_COMM_TALLIES(tx_unicast_frames);
0029     ADD_COMM_TALLIES(tx_multicast_frames);
0030     ADD_COMM_TALLIES(tx_fragments);
0031     ADD_COMM_TALLIES(tx_unicast_octets);
0032     ADD_COMM_TALLIES(tx_multicast_octets);
0033     ADD_COMM_TALLIES(tx_deferred_transmissions);
0034     ADD_COMM_TALLIES(tx_single_retry_frames);
0035     ADD_COMM_TALLIES(tx_multiple_retry_frames);
0036     ADD_COMM_TALLIES(tx_retry_limit_exceeded);
0037     ADD_COMM_TALLIES(tx_discards);
0038     ADD_COMM_TALLIES(rx_unicast_frames);
0039     ADD_COMM_TALLIES(rx_multicast_frames);
0040     ADD_COMM_TALLIES(rx_fragments);
0041     ADD_COMM_TALLIES(rx_unicast_octets);
0042     ADD_COMM_TALLIES(rx_multicast_octets);
0043     ADD_COMM_TALLIES(rx_fcs_errors);
0044     ADD_COMM_TALLIES(rx_discards_no_buffer);
0045     ADD_COMM_TALLIES(tx_discards_wrong_sa);
0046     ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
0047     ADD_COMM_TALLIES(rx_message_in_msg_fragments);
0048     ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
0049 #undef ADD_COMM_TALLIES
0050 }
0051 
0052 
0053 /* Called only as a tasklet (software IRQ) */
0054 static void prism2_info_commtallies32(local_info_t *local, unsigned char *buf,
0055                       int left)
0056 {
0057     struct hfa384x_comm_tallies32 *tallies;
0058 
0059     if (left < sizeof(struct hfa384x_comm_tallies32)) {
0060         printk(KERN_DEBUG "%s: too short (len=%d) commtallies32 "
0061                "info frame\n", local->dev->name, left);
0062         return;
0063     }
0064 
0065     tallies = (struct hfa384x_comm_tallies32 *) buf;
0066 #define ADD_COMM_TALLIES(name) \
0067 local->comm_tallies.name += le32_to_cpu(tallies->name)
0068     ADD_COMM_TALLIES(tx_unicast_frames);
0069     ADD_COMM_TALLIES(tx_multicast_frames);
0070     ADD_COMM_TALLIES(tx_fragments);
0071     ADD_COMM_TALLIES(tx_unicast_octets);
0072     ADD_COMM_TALLIES(tx_multicast_octets);
0073     ADD_COMM_TALLIES(tx_deferred_transmissions);
0074     ADD_COMM_TALLIES(tx_single_retry_frames);
0075     ADD_COMM_TALLIES(tx_multiple_retry_frames);
0076     ADD_COMM_TALLIES(tx_retry_limit_exceeded);
0077     ADD_COMM_TALLIES(tx_discards);
0078     ADD_COMM_TALLIES(rx_unicast_frames);
0079     ADD_COMM_TALLIES(rx_multicast_frames);
0080     ADD_COMM_TALLIES(rx_fragments);
0081     ADD_COMM_TALLIES(rx_unicast_octets);
0082     ADD_COMM_TALLIES(rx_multicast_octets);
0083     ADD_COMM_TALLIES(rx_fcs_errors);
0084     ADD_COMM_TALLIES(rx_discards_no_buffer);
0085     ADD_COMM_TALLIES(tx_discards_wrong_sa);
0086     ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
0087     ADD_COMM_TALLIES(rx_message_in_msg_fragments);
0088     ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
0089 #undef ADD_COMM_TALLIES
0090 }
0091 
0092 
0093 /* Called only as a tasklet (software IRQ) */
0094 static void prism2_info_commtallies(local_info_t *local, unsigned char *buf,
0095                     int left)
0096 {
0097     if (local->tallies32)
0098         prism2_info_commtallies32(local, buf, left);
0099     else
0100         prism2_info_commtallies16(local, buf, left);
0101 }
0102 
0103 
0104 #ifndef PRISM2_NO_STATION_MODES
0105 #ifndef PRISM2_NO_DEBUG
0106 static const char* hfa384x_linkstatus_str(u16 linkstatus)
0107 {
0108     switch (linkstatus) {
0109     case HFA384X_LINKSTATUS_CONNECTED:
0110         return "Connected";
0111     case HFA384X_LINKSTATUS_DISCONNECTED:
0112         return "Disconnected";
0113     case HFA384X_LINKSTATUS_AP_CHANGE:
0114         return "Access point change";
0115     case HFA384X_LINKSTATUS_AP_OUT_OF_RANGE:
0116         return "Access point out of range";
0117     case HFA384X_LINKSTATUS_AP_IN_RANGE:
0118         return "Access point in range";
0119     case HFA384X_LINKSTATUS_ASSOC_FAILED:
0120         return "Association failed";
0121     default:
0122         return "Unknown";
0123     }
0124 }
0125 #endif /* PRISM2_NO_DEBUG */
0126 
0127 
0128 /* Called only as a tasklet (software IRQ) */
0129 static void prism2_info_linkstatus(local_info_t *local, unsigned char *buf,
0130                     int left)
0131 {
0132     u16 val;
0133     int non_sta_mode;
0134 
0135     /* Alloc new JoinRequests to occur since LinkStatus for the previous
0136      * has been received */
0137     local->last_join_time = 0;
0138 
0139     if (left != 2) {
0140         printk(KERN_DEBUG "%s: invalid linkstatus info frame "
0141                "length %d\n", local->dev->name, left);
0142         return;
0143     }
0144 
0145     non_sta_mode = local->iw_mode == IW_MODE_MASTER ||
0146         local->iw_mode == IW_MODE_REPEAT ||
0147         local->iw_mode == IW_MODE_MONITOR;
0148 
0149     val = buf[0] | (buf[1] << 8);
0150     if (!non_sta_mode || val != HFA384X_LINKSTATUS_DISCONNECTED) {
0151         PDEBUG(DEBUG_EXTRA, "%s: LinkStatus=%d (%s)\n",
0152                local->dev->name, val, hfa384x_linkstatus_str(val));
0153     }
0154 
0155     if (non_sta_mode) {
0156         netif_carrier_on(local->dev);
0157         netif_carrier_on(local->ddev);
0158         return;
0159     }
0160 
0161     /* Get current BSSID later in scheduled task */
0162     set_bit(PRISM2_INFO_PENDING_LINKSTATUS, &local->pending_info);
0163     local->prev_link_status = val;
0164     schedule_work(&local->info_queue);
0165 }
0166 
0167 
0168 static void prism2_host_roaming(local_info_t *local)
0169 {
0170     struct hfa384x_join_request req;
0171     struct net_device *dev = local->dev;
0172     struct hfa384x_hostscan_result *selected, *entry;
0173     int i;
0174     unsigned long flags;
0175 
0176     if (local->last_join_time &&
0177         time_before(jiffies, local->last_join_time + 10 * HZ)) {
0178         PDEBUG(DEBUG_EXTRA, "%s: last join request has not yet been "
0179                "completed - waiting for it before issuing new one\n",
0180                dev->name);
0181         return;
0182     }
0183 
0184     /* ScanResults are sorted: first ESS results in decreasing signal
0185      * quality then IBSS results in similar order.
0186      * Trivial roaming policy: just select the first entry.
0187      * This could probably be improved by adding hysteresis to limit
0188      * number of handoffs, etc.
0189      *
0190      * Could do periodic RID_SCANREQUEST or Inquire F101 to get new
0191      * ScanResults */
0192     spin_lock_irqsave(&local->lock, flags);
0193     if (local->last_scan_results == NULL ||
0194         local->last_scan_results_count == 0) {
0195         spin_unlock_irqrestore(&local->lock, flags);
0196         PDEBUG(DEBUG_EXTRA, "%s: no scan results for host roaming\n",
0197                dev->name);
0198         return;
0199     }
0200 
0201     selected = &local->last_scan_results[0];
0202 
0203     if (local->preferred_ap[0] || local->preferred_ap[1] ||
0204         local->preferred_ap[2] || local->preferred_ap[3] ||
0205         local->preferred_ap[4] || local->preferred_ap[5]) {
0206         /* Try to find preferred AP */
0207         PDEBUG(DEBUG_EXTRA, "%s: Preferred AP BSSID %pM\n",
0208                dev->name, local->preferred_ap);
0209         for (i = 0; i < local->last_scan_results_count; i++) {
0210             entry = &local->last_scan_results[i];
0211             if (memcmp(local->preferred_ap, entry->bssid, 6) == 0)
0212             {
0213                 PDEBUG(DEBUG_EXTRA, "%s: using preferred AP "
0214                        "selection\n", dev->name);
0215                 selected = entry;
0216                 break;
0217             }
0218         }
0219     }
0220 
0221     memcpy(req.bssid, selected->bssid, ETH_ALEN);
0222     req.channel = selected->chid;
0223     spin_unlock_irqrestore(&local->lock, flags);
0224 
0225     PDEBUG(DEBUG_EXTRA, "%s: JoinRequest: BSSID=%pM"
0226            " channel=%d\n",
0227            dev->name, req.bssid, le16_to_cpu(req.channel));
0228     if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
0229                  sizeof(req))) {
0230         printk(KERN_DEBUG "%s: JoinRequest failed\n", dev->name);
0231     }
0232     local->last_join_time = jiffies;
0233 }
0234 
0235 
0236 static void hostap_report_scan_complete(local_info_t *local)
0237 {
0238     union iwreq_data wrqu;
0239 
0240     /* Inform user space about new scan results (just empty event,
0241      * SIOCGIWSCAN can be used to fetch data */
0242     wrqu.data.length = 0;
0243     wrqu.data.flags = 0;
0244     wireless_send_event(local->dev, SIOCGIWSCAN, &wrqu, NULL);
0245 
0246     /* Allow SIOCGIWSCAN handling to occur since we have received
0247      * scanning result */
0248     local->scan_timestamp = 0;
0249 }
0250 
0251 
0252 /* Called only as a tasklet (software IRQ) */
0253 static void prism2_info_scanresults(local_info_t *local, unsigned char *buf,
0254                     int left)
0255 {
0256     u16 *pos;
0257     int new_count, i;
0258     unsigned long flags;
0259     struct hfa384x_scan_result *res;
0260     struct hfa384x_hostscan_result *results, *prev;
0261 
0262     if (left < 4) {
0263         printk(KERN_DEBUG "%s: invalid scanresult info frame "
0264                "length %d\n", local->dev->name, left);
0265         return;
0266     }
0267 
0268     pos = (u16 *) buf;
0269     pos++;
0270     pos++;
0271     left -= 4;
0272 
0273     new_count = left / sizeof(struct hfa384x_scan_result);
0274     results = kmalloc_array(new_count,
0275                 sizeof(struct hfa384x_hostscan_result),
0276                 GFP_ATOMIC);
0277     if (results == NULL)
0278         return;
0279 
0280     /* Convert to hostscan result format. */
0281     res = (struct hfa384x_scan_result *) pos;
0282     for (i = 0; i < new_count; i++) {
0283         memcpy(&results[i], &res[i],
0284                sizeof(struct hfa384x_scan_result));
0285         results[i].atim = 0;
0286     }
0287 
0288     spin_lock_irqsave(&local->lock, flags);
0289     local->last_scan_type = PRISM2_SCAN;
0290     prev = local->last_scan_results;
0291     local->last_scan_results = results;
0292     local->last_scan_results_count = new_count;
0293     spin_unlock_irqrestore(&local->lock, flags);
0294     kfree(prev);
0295 
0296     hostap_report_scan_complete(local);
0297 
0298     /* Perform rest of ScanResults handling later in scheduled task */
0299     set_bit(PRISM2_INFO_PENDING_SCANRESULTS, &local->pending_info);
0300     schedule_work(&local->info_queue);
0301 }
0302 
0303 
0304 /* Called only as a tasklet (software IRQ) */
0305 static void prism2_info_hostscanresults(local_info_t *local,
0306                     unsigned char *buf, int left)
0307 {
0308     int i, result_size, copy_len, new_count;
0309     struct hfa384x_hostscan_result *results, *prev;
0310     unsigned long flags;
0311     __le16 *pos;
0312     u8 *ptr;
0313 
0314     wake_up_interruptible(&local->hostscan_wq);
0315 
0316     if (left < 4) {
0317         printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
0318                "length %d\n", local->dev->name, left);
0319         return;
0320     }
0321 
0322     pos = (__le16 *) buf;
0323     copy_len = result_size = le16_to_cpu(*pos);
0324     if (result_size == 0) {
0325         printk(KERN_DEBUG "%s: invalid result_size (0) in "
0326                "hostscanresults\n", local->dev->name);
0327         return;
0328     }
0329     if (copy_len > sizeof(struct hfa384x_hostscan_result))
0330         copy_len = sizeof(struct hfa384x_hostscan_result);
0331 
0332     pos++;
0333     pos++;
0334     left -= 4;
0335     ptr = (u8 *) pos;
0336 
0337     new_count = left / result_size;
0338     results = kcalloc(new_count, sizeof(struct hfa384x_hostscan_result),
0339               GFP_ATOMIC);
0340     if (results == NULL)
0341         return;
0342 
0343     for (i = 0; i < new_count; i++) {
0344         memcpy(&results[i], ptr, copy_len);
0345         ptr += result_size;
0346         left -= result_size;
0347     }
0348 
0349     if (left) {
0350         printk(KERN_DEBUG "%s: short HostScan result entry (%d/%d)\n",
0351                local->dev->name, left, result_size);
0352     }
0353 
0354     spin_lock_irqsave(&local->lock, flags);
0355     local->last_scan_type = PRISM2_HOSTSCAN;
0356     prev = local->last_scan_results;
0357     local->last_scan_results = results;
0358     local->last_scan_results_count = new_count;
0359     spin_unlock_irqrestore(&local->lock, flags);
0360     kfree(prev);
0361 
0362     hostap_report_scan_complete(local);
0363 }
0364 #endif /* PRISM2_NO_STATION_MODES */
0365 
0366 
0367 /* Called only as a tasklet (software IRQ) */
0368 void hostap_info_process(local_info_t *local, struct sk_buff *skb)
0369 {
0370     struct hfa384x_info_frame *info;
0371     unsigned char *buf;
0372     int left;
0373 #ifndef PRISM2_NO_DEBUG
0374     int i;
0375 #endif /* PRISM2_NO_DEBUG */
0376 
0377     info = (struct hfa384x_info_frame *) skb->data;
0378     buf = skb->data + sizeof(*info);
0379     left = skb->len - sizeof(*info);
0380 
0381     switch (le16_to_cpu(info->type)) {
0382     case HFA384X_INFO_COMMTALLIES:
0383         prism2_info_commtallies(local, buf, left);
0384         break;
0385 
0386 #ifndef PRISM2_NO_STATION_MODES
0387     case HFA384X_INFO_LINKSTATUS:
0388         prism2_info_linkstatus(local, buf, left);
0389         break;
0390 
0391     case HFA384X_INFO_SCANRESULTS:
0392         prism2_info_scanresults(local, buf, left);
0393         break;
0394 
0395     case HFA384X_INFO_HOSTSCANRESULTS:
0396         prism2_info_hostscanresults(local, buf, left);
0397         break;
0398 #endif /* PRISM2_NO_STATION_MODES */
0399 
0400 #ifndef PRISM2_NO_DEBUG
0401     default:
0402         PDEBUG(DEBUG_EXTRA, "%s: INFO - len=%d type=0x%04x\n",
0403                local->dev->name, le16_to_cpu(info->len),
0404                le16_to_cpu(info->type));
0405         PDEBUG(DEBUG_EXTRA, "Unknown info frame:");
0406         for (i = 0; i < (left < 100 ? left : 100); i++)
0407             PDEBUG2(DEBUG_EXTRA, " %02x", buf[i]);
0408         PDEBUG2(DEBUG_EXTRA, "\n");
0409         break;
0410 #endif /* PRISM2_NO_DEBUG */
0411     }
0412 }
0413 
0414 
0415 #ifndef PRISM2_NO_STATION_MODES
0416 static void handle_info_queue_linkstatus(local_info_t *local)
0417 {
0418     int val = local->prev_link_status;
0419     int connected;
0420     union iwreq_data wrqu;
0421 
0422     connected =
0423         val == HFA384X_LINKSTATUS_CONNECTED ||
0424         val == HFA384X_LINKSTATUS_AP_CHANGE ||
0425         val == HFA384X_LINKSTATUS_AP_IN_RANGE;
0426 
0427     if (local->func->get_rid(local->dev, HFA384X_RID_CURRENTBSSID,
0428                  local->bssid, ETH_ALEN, 1) < 0) {
0429         printk(KERN_DEBUG "%s: could not read CURRENTBSSID after "
0430                "LinkStatus event\n", local->dev->name);
0431     } else {
0432         PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=%pM\n",
0433                local->dev->name,
0434                (unsigned char *) local->bssid);
0435         if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
0436             hostap_add_sta(local->ap, local->bssid);
0437     }
0438 
0439     /* Get BSSID if we have a valid AP address */
0440     if (connected) {
0441         netif_carrier_on(local->dev);
0442         netif_carrier_on(local->ddev);
0443         memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
0444     } else {
0445         netif_carrier_off(local->dev);
0446         netif_carrier_off(local->ddev);
0447         eth_zero_addr(wrqu.ap_addr.sa_data);
0448     }
0449     wrqu.ap_addr.sa_family = ARPHRD_ETHER;
0450 
0451     /*
0452      * Filter out sequential disconnect events in order not to cause a
0453      * flood of SIOCGIWAP events that have a race condition with EAPOL
0454      * frames and can confuse wpa_supplicant about the current association
0455      * status.
0456      */
0457     if (connected || local->prev_linkstatus_connected)
0458         wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
0459     local->prev_linkstatus_connected = connected;
0460 }
0461 
0462 
0463 static void handle_info_queue_scanresults(local_info_t *local)
0464 {
0465     if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
0466         prism2_host_roaming(local);
0467 
0468     if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
0469         !is_zero_ether_addr(local->preferred_ap)) {
0470         /*
0471          * Firmware seems to be getting into odd state in host_roaming
0472          * mode 2 when hostscan is used without join command, so try
0473          * to fix this by re-joining the current AP. This does not
0474          * actually trigger a new association if the current AP is
0475          * still in the scan results.
0476          */
0477         prism2_host_roaming(local);
0478     }
0479 }
0480 
0481 
0482 /* Called only as scheduled task after receiving info frames (used to avoid
0483  * pending too much time in HW IRQ handler). */
0484 static void handle_info_queue(struct work_struct *work)
0485 {
0486     local_info_t *local = container_of(work, local_info_t, info_queue);
0487 
0488     if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
0489                    &local->pending_info))
0490         handle_info_queue_linkstatus(local);
0491 
0492     if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
0493                    &local->pending_info))
0494         handle_info_queue_scanresults(local);
0495 }
0496 #endif /* PRISM2_NO_STATION_MODES */
0497 
0498 
0499 void hostap_info_init(local_info_t *local)
0500 {
0501     skb_queue_head_init(&local->info_list);
0502 #ifndef PRISM2_NO_STATION_MODES
0503     INIT_WORK(&local->info_queue, handle_info_queue);
0504 #endif /* PRISM2_NO_STATION_MODES */
0505 }
0506 
0507 
0508 EXPORT_SYMBOL(hostap_info_init);
0509 EXPORT_SYMBOL(hostap_info_process);