2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Purpose: handle hostap daemon ioctl input/out functions
42 #define VIAWGET_HOSTAPD_MAX_BUF_SIZE 1024
43 #define HOSTAP_CRYPT_FLAG_SET_TX_KEY BIT0
44 #define HOSTAP_CRYPT_ERR_UNKNOWN_ADDR 3
45 #define HOSTAP_CRYPT_ERR_KEY_SET_FAILED 5
47 static int msglevel =MSG_LEVEL_INFO;
51 * register net_device (AP) for hostap daemon
63 static int hostap_enable_hostapd(struct vnt_private *pDevice, int rtnl_locked)
65 struct vnt_private *apdev_priv;
66 struct net_device *dev = pDevice->dev;
68 const struct net_device_ops apdev_netdev_ops = {
69 .ndo_start_xmit = pDevice->tx_80211,
72 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Enabling hostapd mode\n", dev->name);
74 pDevice->apdev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
75 if (pDevice->apdev == NULL)
78 apdev_priv = netdev_priv(pDevice->apdev);
79 *apdev_priv = *pDevice;
80 memcpy(pDevice->apdev->dev_addr, dev->dev_addr, ETH_ALEN);
82 pDevice->apdev->netdev_ops = &apdev_netdev_ops;
84 pDevice->apdev->type = ARPHRD_IEEE80211;
86 pDevice->apdev->base_addr = dev->base_addr;
87 pDevice->apdev->irq = dev->irq;
88 pDevice->apdev->mem_start = dev->mem_start;
89 pDevice->apdev->mem_end = dev->mem_end;
90 sprintf(pDevice->apdev->name, "%sap", dev->name);
92 ret = register_netdevice(pDevice->apdev);
94 ret = register_netdev(pDevice->apdev);
96 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: register_netdevice(AP) failed!\n",
101 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Registered netdevice %s for AP management\n",
102 dev->name, pDevice->apdev->name);
104 KeyvInitTable(pDevice,&pDevice->sKey);
111 * unregister net_device(AP)
123 static int hostap_disable_hostapd(struct vnt_private *pDevice, int rtnl_locked)
126 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: disabling hostapd mode\n", pDevice->dev->name);
128 if (pDevice->apdev && pDevice->apdev->name && pDevice->apdev->name[0]) {
130 unregister_netdevice(pDevice->apdev);
132 unregister_netdev(pDevice->apdev);
133 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Netdevice %s unregistered\n",
134 pDevice->dev->name, pDevice->apdev->name);
136 kfree(pDevice->apdev);
137 pDevice->apdev = NULL;
138 pDevice->bEnable8021x = false;
139 pDevice->bEnableHostWEP = false;
140 pDevice->bEncryptionEnable = false;
147 * Set enable/disable hostapd mode
159 int vt6656_hostap_set_hostapd(struct vnt_private *pDevice,
160 int val, int rtnl_locked)
162 if (val < 0 || val > 1)
165 if (pDevice->bEnableHostapd == val)
168 pDevice->bEnableHostapd = val;
171 return hostap_enable_hostapd(pDevice, rtnl_locked);
173 return hostap_disable_hostapd(pDevice, rtnl_locked);
178 * remove station function supported for hostap daemon
189 static int hostap_remove_sta(struct vnt_private *pDevice,
190 struct viawget_hostapd_param *param)
192 unsigned int uNodeIndex;
194 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
195 BSSvRemoveOneNode(pDevice, uNodeIndex);
205 * add a station from hostap daemon
216 static int hostap_add_sta(struct vnt_private *pDevice,
217 struct viawget_hostapd_param *param)
219 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
220 unsigned int uNodeIndex;
222 if (!BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex))
223 BSSvCreateOneNode(pDevice, &uNodeIndex);
225 memcpy(pMgmt->sNodeDBTable[uNodeIndex].abyMACAddr, param->sta_addr, WLAN_ADDR_LEN);
226 pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_ASSOC;
227 pMgmt->sNodeDBTable[uNodeIndex].wCapInfo = param->u.add_sta.capability;
228 // TODO listenInterval
229 // pMgmt->sNodeDBTable[uNodeIndex].wListenInterval = 1;
230 pMgmt->sNodeDBTable[uNodeIndex].bPSEnable = false;
231 pMgmt->sNodeDBTable[uNodeIndex].bySuppRate = param->u.add_sta.tx_supp_rates;
234 pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate =
235 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate;
236 // set max basic rate
237 pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate = RATE_2M;
238 // Todo: check sta preamble, if ap can't support, set status code
239 pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble =
240 WLAN_GET_CAP_INFO_SHORTPREAMBLE(pMgmt->sNodeDBTable[uNodeIndex].wCapInfo);
242 pMgmt->sNodeDBTable[uNodeIndex].wAID = (u16)param->u.add_sta.aid;
244 pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer = jiffies;
246 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Add STA AID= %d \n", pMgmt->sNodeDBTable[uNodeIndex].wAID);
247 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "MAC=%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X \n",
255 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Max Support rate = %d \n",
256 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate);
275 static int hostap_get_info_sta(struct vnt_private *pDevice,
276 struct viawget_hostapd_param *param)
278 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
279 unsigned int uNodeIndex;
281 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
282 param->u.get_info_sta.inactive_sec =
283 (jiffies - pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer) / HZ;
285 //param->u.get_info_sta.txexc = pMgmt->sNodeDBTable[uNodeIndex].uTxAttempts;
307 static int hostap_set_flags_sta(struct vnt_private *pDevice,
308 struct viawget_hostapd_param *param)
310 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
311 unsigned int uNodeIndex;
313 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
314 pMgmt->sNodeDBTable[uNodeIndex].dwFlags |= param->u.set_flags_sta.flags_or;
315 pMgmt->sNodeDBTable[uNodeIndex].dwFlags &= param->u.set_flags_sta.flags_and;
316 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " dwFlags = %x\n",
317 (unsigned int) pMgmt->sNodeDBTable[uNodeIndex].dwFlags);
328 * set generic element (wpa ie)
339 static int hostap_set_generic_element(struct vnt_private *pDevice,
340 struct viawget_hostapd_param *param)
342 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
344 memcpy( pMgmt->abyWPAIE,
345 param->u.generic_elem.data,
346 param->u.generic_elem.len
349 pMgmt->wWPAIELen = param->u.generic_elem.len;
351 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pMgmt->wWPAIELen = %d\n", pMgmt->wWPAIELen);
354 if (pMgmt->wWPAIELen == 0) {
355 pMgmt->eAuthenMode = WMAC_AUTH_OPEN;
356 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " No WPAIE, Disable WPA \n");
359 if ((pMgmt->abyWPAIE[0] == WLAN_EID_RSN_WPA) ||
360 (pMgmt->abyWPAIE[0] == WLAN_EID_RSN)) {
361 pMgmt->eAuthenMode = WMAC_AUTH_WPANONE;
362 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set WPAIE enable WPA\n");
372 * flush station nodes table.
383 static void hostap_flush_sta(struct vnt_private *pDevice)
385 // reserved node index =0 for multicast node.
386 BSSvClearNodeDBTable(pDevice, 1);
387 pDevice->uAssocCount = 0;
394 * set each stations encryption key
405 static int hostap_set_encryption(struct vnt_private *pDevice,
406 struct viawget_hostapd_param *param, int param_len)
408 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
410 u8 abyKey[MAX_KEY_LEN];
411 u8 abySeq[MAX_KEY_LEN];
412 NDIS_802_11_KEY_RSC KeyRSC;
413 u8 byKeyDecMode = KEY_CTL_WEP;
417 int bKeyTableFull = false;
420 param->u.crypt.err = 0;
422 if (param->u.crypt.alg > WPA_ALG_CCMP)
425 if ((param->u.crypt.idx > 3) || (param->u.crypt.key_len > MAX_KEY_LEN)) {
426 param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
427 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_KEY_SET_FAILED\n");
431 if (is_broadcast_ether_addr(param->sta_addr)) {
432 if (param->u.crypt.idx >= MAX_GROUP_KEY)
437 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &iNodeIndex) == false) {
438 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
439 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
443 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: sta_index %d \n", iNodeIndex);
444 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: alg %d \n", param->u.crypt.alg);
446 if (param->u.crypt.alg == WPA_ALG_NONE) {
448 if (pMgmt->sNodeDBTable[iNodeIndex].bOnFly == true) {
449 if (KeybRemoveKey( pDevice,
452 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex
454 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "KeybRemoveKey fail \n");
456 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
458 pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = 0;
459 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = 0;
460 pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = 0;
461 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = 0;
462 pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
463 pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
464 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = 0;
465 memset(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
473 memcpy(abyKey, param->u.crypt.key, param->u.crypt.key_len);
474 // copy to node key tbl
475 pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = param->u.crypt.idx;
476 pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = param->u.crypt.key_len;
477 memcpy(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
479 param->u.crypt.key_len
482 dwKeyIndex = (u32)(param->u.crypt.idx);
483 if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) {
484 pDevice->byKeyIndex = (u8)dwKeyIndex;
485 pDevice->bTransmitKey = true;
486 dwKeyIndex |= (1 << 31);
489 if (param->u.crypt.alg == WPA_ALG_WEP) {
491 if ((pDevice->bEnable8021x == false) || (iNodeIndex == 0)) {
492 KeybSetDefaultKey( pDevice,
494 dwKeyIndex & ~(BIT30 | USE_KEYRSC),
495 param->u.crypt.key_len,
502 // 8021x enable, individual key
503 dwKeyIndex |= (1 << 30); // set pairwise key
504 if (KeybSetKey(pDevice, &(pDevice->sKey),
506 dwKeyIndex & ~(USE_KEYRSC),
507 param->u.crypt.key_len,
508 &KeyRSC, (u8 *)abyKey,
512 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
516 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
517 bKeyTableFull = true;
520 pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
521 pDevice->bEncryptionEnable = true;
522 pMgmt->byCSSPK = KEY_CTL_WEP;
523 pMgmt->byCSSGK = KEY_CTL_WEP;
524 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = KEY_CTL_WEP;
525 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
529 if (param->u.crypt.seq) {
530 memcpy(&abySeq, param->u.crypt.seq, 8);
531 for (ii = 0 ; ii < 8 ; ii++)
532 KeyRSC |= (unsigned long)abySeq[ii] << (ii * 8);
534 dwKeyIndex |= 1 << 29;
535 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = KeyRSC;
538 if (param->u.crypt.alg == WPA_ALG_TKIP) {
539 if (param->u.crypt.key_len != MAX_KEY_LEN)
541 pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
542 byKeyDecMode = KEY_CTL_TKIP;
543 pMgmt->byCSSPK = KEY_CTL_TKIP;
544 pMgmt->byCSSGK = KEY_CTL_TKIP;
547 if (param->u.crypt.alg == WPA_ALG_CCMP) {
548 if ((param->u.crypt.key_len != AES_KEY_LEN) ||
549 (pDevice->byLocalID <= REV_ID_VT3253_A1))
551 pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
552 byKeyDecMode = KEY_CTL_CCMP;
553 pMgmt->byCSSPK = KEY_CTL_CCMP;
554 pMgmt->byCSSGK = KEY_CTL_CCMP;
557 if (iNodeIndex == 0) {
558 KeybSetDefaultKey( pDevice,
561 param->u.crypt.key_len,
566 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
569 dwKeyIndex |= (1 << 30); // set pairwise key
570 if (KeybSetKey(pDevice,
574 param->u.crypt.key_len,
580 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
584 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
585 bKeyTableFull = true;
586 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Key Table Full\n");
591 if (bKeyTableFull == true) {
592 wKeyCtl &= 0x7F00; // clear all key control filed
593 wKeyCtl |= (byKeyDecMode << 4);
594 wKeyCtl |= (byKeyDecMode);
595 wKeyCtl |= 0x0044; // use group key for all address
596 wKeyCtl |= 0x4000; // disable KeyTable[MAX_KEY_TABLE-1] on-fly to genernate rx int
598 //MACvSetDefaultKeyCtl(pDevice->PortOffset, wKeyCtl, MAX_KEY_TABLE-1, pDevice->byLocalID);
601 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Set key sta_index= %d \n", iNodeIndex);
602 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " tx_index=%d len=%d \n", param->u.crypt.idx,
603 param->u.crypt.key_len );
604 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " key=%x-%x-%x-%x-%x-xxxxx \n",
605 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
606 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[1],
607 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[2],
608 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[3],
609 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[4]
613 pDevice->bEncryptionEnable = true;
614 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = byKeyDecMode;
615 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
616 pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
617 pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
624 * get each stations encryption key
635 static int hostap_get_encryption(struct vnt_private *pDevice,
636 struct viawget_hostapd_param *param,
639 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
644 param->u.crypt.err = 0;
646 if (is_broadcast_ether_addr(param->sta_addr)) {
649 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &iNodeIndex) == false) {
650 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
651 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
655 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: %d\n", iNodeIndex);
656 memset(param->u.crypt.seq, 0, 8);
657 for (ii = 0 ; ii < 8 ; ii++) {
658 param->u.crypt.seq[ii] = (u8)pMgmt->sNodeDBTable[iNodeIndex].KeyRSC >> (ii * 8);
666 * vt6656_hostap_ioctl main function supported for hostap daemon.
678 int vt6656_hostap_ioctl(struct vnt_private *pDevice, struct iw_point *p)
680 struct viawget_hostapd_param *param;
684 if (p->length < sizeof(struct viawget_hostapd_param) ||
685 p->length > VIAWGET_HOSTAPD_MAX_BUF_SIZE || !p->pointer)
688 param = kmalloc((int)p->length, (int)GFP_KERNEL);
692 if (copy_from_user(param, p->pointer, p->length)) {
697 switch (param->cmd) {
698 case VIAWGET_HOSTAPD_SET_ENCRYPTION:
699 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ENCRYPTION \n");
700 spin_lock_irq(&pDevice->lock);
701 ret = hostap_set_encryption(pDevice, param, p->length);
702 spin_unlock_irq(&pDevice->lock);
704 case VIAWGET_HOSTAPD_GET_ENCRYPTION:
705 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_ENCRYPTION \n");
706 spin_lock_irq(&pDevice->lock);
707 ret = hostap_get_encryption(pDevice, param, p->length);
708 spin_unlock_irq(&pDevice->lock);
710 case VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR:
711 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR \n");
714 case VIAWGET_HOSTAPD_FLUSH:
715 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_FLUSH \n");
716 spin_lock_irq(&pDevice->lock);
717 hostap_flush_sta(pDevice);
718 spin_unlock_irq(&pDevice->lock);
720 case VIAWGET_HOSTAPD_ADD_STA:
721 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_ADD_STA \n");
722 spin_lock_irq(&pDevice->lock);
723 ret = hostap_add_sta(pDevice, param);
724 spin_unlock_irq(&pDevice->lock);
726 case VIAWGET_HOSTAPD_REMOVE_STA:
727 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_REMOVE_STA \n");
728 spin_lock_irq(&pDevice->lock);
729 ret = hostap_remove_sta(pDevice, param);
730 spin_unlock_irq(&pDevice->lock);
732 case VIAWGET_HOSTAPD_GET_INFO_STA:
733 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_INFO_STA \n");
734 ret = hostap_get_info_sta(pDevice, param);
737 case VIAWGET_HOSTAPD_SET_FLAGS_STA:
738 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_FLAGS_STA \n");
739 ret = hostap_set_flags_sta(pDevice, param);
742 case VIAWGET_HOSTAPD_MLME:
743 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_MLME \n");
746 case VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT:
747 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT \n");
748 ret = hostap_set_generic_element(pDevice, param);
751 case VIAWGET_HOSTAPD_SCAN_REQ:
752 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SCAN_REQ \n");
755 case VIAWGET_HOSTAPD_STA_CLEAR_STATS:
756 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_STA_CLEAR_STATS \n");
760 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "vt6656_hostap_ioctl: unknown cmd=%d\n",
766 if ((ret == 0) && ap_ioctl) {
767 if (copy_to_user(p->pointer, param, p->length)) {