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(PSDevice pDevice, int rtnl_locked)
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(PSDevice 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;
148 * Set enable/disable hostapd mode
160 int vt6656_hostap_set_hostapd(PSDevice pDevice, 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);
179 * remove station function supported for hostap daemon
190 static int hostap_remove_sta(PSDevice pDevice,
191 struct viawget_hostapd_param *param)
193 unsigned int uNodeIndex;
196 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
197 BSSvRemoveOneNode(pDevice, uNodeIndex);
207 * add a station from hostap daemon
218 static int hostap_add_sta(PSDevice pDevice,
219 struct viawget_hostapd_param *param)
221 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
222 unsigned int uNodeIndex;
225 if (!BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
226 BSSvCreateOneNode((PSDevice)pDevice, &uNodeIndex);
228 memcpy(pMgmt->sNodeDBTable[uNodeIndex].abyMACAddr, param->sta_addr, WLAN_ADDR_LEN);
229 pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_ASSOC;
230 pMgmt->sNodeDBTable[uNodeIndex].wCapInfo = param->u.add_sta.capability;
231 // TODO listenInterval
232 // pMgmt->sNodeDBTable[uNodeIndex].wListenInterval = 1;
233 pMgmt->sNodeDBTable[uNodeIndex].bPSEnable = FALSE;
234 pMgmt->sNodeDBTable[uNodeIndex].bySuppRate = param->u.add_sta.tx_supp_rates;
237 pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate =
238 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate;
239 // set max basic rate
240 pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate = RATE_2M;
241 // Todo: check sta preamble, if ap can't support, set status code
242 pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble =
243 WLAN_GET_CAP_INFO_SHORTPREAMBLE(pMgmt->sNodeDBTable[uNodeIndex].wCapInfo);
245 pMgmt->sNodeDBTable[uNodeIndex].wAID = (WORD)param->u.add_sta.aid;
247 pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer = jiffies;
249 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Add STA AID= %d \n", pMgmt->sNodeDBTable[uNodeIndex].wAID);
250 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "MAC=%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X \n",
258 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Max Support rate = %d \n",
259 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate);
278 static int hostap_get_info_sta(PSDevice pDevice,
279 struct viawget_hostapd_param *param)
281 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
282 unsigned int uNodeIndex;
284 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
285 param->u.get_info_sta.inactive_sec =
286 (jiffies - pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer) / HZ;
288 //param->u.get_info_sta.txexc = pMgmt->sNodeDBTable[uNodeIndex].uTxAttempts;
311 static int hostap_set_flags_sta(PSDevice pDevice,
312 struct viawget_hostapd_param *param)
314 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
315 unsigned int uNodeIndex;
317 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
318 pMgmt->sNodeDBTable[uNodeIndex].dwFlags |= param->u.set_flags_sta.flags_or;
319 pMgmt->sNodeDBTable[uNodeIndex].dwFlags &= param->u.set_flags_sta.flags_and;
320 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " dwFlags = %x\n",
321 (unsigned int) pMgmt->sNodeDBTable[uNodeIndex].dwFlags);
334 * set generic element (wpa ie)
345 static int hostap_set_generic_element(PSDevice pDevice,
346 struct viawget_hostapd_param *param)
348 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
352 memcpy( pMgmt->abyWPAIE,
353 param->u.generic_elem.data,
354 param->u.generic_elem.len
357 pMgmt->wWPAIELen = param->u.generic_elem.len;
359 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pMgmt->wWPAIELen = %d\n", pMgmt->wWPAIELen);
362 if (pMgmt->wWPAIELen == 0) {
363 pMgmt->eAuthenMode = WMAC_AUTH_OPEN;
364 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " No WPAIE, Disable WPA \n");
367 if ((pMgmt->abyWPAIE[0] == WLAN_EID_RSN_WPA) ||
368 (pMgmt->abyWPAIE[0] == WLAN_EID_RSN)) {
369 pMgmt->eAuthenMode = WMAC_AUTH_WPANONE;
370 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set WPAIE enable WPA\n");
380 * flush station nodes table.
391 static void hostap_flush_sta(PSDevice pDevice)
393 // reserved node index =0 for multicast node.
394 BSSvClearNodeDBTable(pDevice, 1);
395 pDevice->uAssocCount = 0;
402 * set each stations encryption key
413 static int hostap_set_encryption(PSDevice pDevice,
414 struct viawget_hostapd_param *param,
417 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
418 DWORD dwKeyIndex = 0;
419 BYTE abyKey[MAX_KEY_LEN];
420 BYTE abySeq[MAX_KEY_LEN];
421 NDIS_802_11_KEY_RSC KeyRSC;
422 BYTE byKeyDecMode = KEY_CTL_WEP;
426 BOOL bKeyTableFull = FALSE;
430 param->u.crypt.err = 0;
432 if (param->u.crypt.alg > WPA_ALG_CCMP)
436 if ((param->u.crypt.idx > 3) || (param->u.crypt.key_len > MAX_KEY_LEN)) {
437 param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
438 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_KEY_SET_FAILED\n");
442 if (is_broadcast_ether_addr(param->sta_addr)) {
443 if (param->u.crypt.idx >= MAX_GROUP_KEY)
448 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &iNodeIndex) == FALSE) {
449 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
450 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
454 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: sta_index %d \n", iNodeIndex);
455 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: alg %d \n", param->u.crypt.alg);
457 if (param->u.crypt.alg == WPA_ALG_NONE) {
459 if (pMgmt->sNodeDBTable[iNodeIndex].bOnFly == TRUE) {
460 if (KeybRemoveKey( pDevice,
463 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex
465 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "KeybRemoveKey fail \n");
467 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = FALSE;
469 pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = 0;
470 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = 0;
471 pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = 0;
472 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = 0;
473 pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
474 pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
475 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = 0;
476 memset(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
484 memcpy(abyKey, param->u.crypt.key, param->u.crypt.key_len);
485 // copy to node key tbl
486 pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = param->u.crypt.idx;
487 pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = param->u.crypt.key_len;
488 memcpy(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
490 param->u.crypt.key_len
493 dwKeyIndex = (DWORD)(param->u.crypt.idx);
494 if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) {
495 pDevice->byKeyIndex = (BYTE)dwKeyIndex;
496 pDevice->bTransmitKey = TRUE;
497 dwKeyIndex |= (1 << 31);
500 if (param->u.crypt.alg == WPA_ALG_WEP) {
502 if ((pDevice->bEnable8021x == FALSE) || (iNodeIndex == 0)) {
503 KeybSetDefaultKey( pDevice,
505 dwKeyIndex & ~(BIT30 | USE_KEYRSC),
506 param->u.crypt.key_len,
513 // 8021x enable, individual key
514 dwKeyIndex |= (1 << 30); // set pairwise key
515 if (KeybSetKey(pDevice,
518 dwKeyIndex & ~(USE_KEYRSC),
519 param->u.crypt.key_len,
526 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = TRUE;
530 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = FALSE;
531 bKeyTableFull = TRUE;
534 pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
535 pDevice->bEncryptionEnable = TRUE;
536 pMgmt->byCSSPK = KEY_CTL_WEP;
537 pMgmt->byCSSGK = KEY_CTL_WEP;
538 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = KEY_CTL_WEP;
539 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
543 if (param->u.crypt.seq) {
544 memcpy(&abySeq, param->u.crypt.seq, 8);
545 for (ii = 0 ; ii < 8 ; ii++)
546 KeyRSC |= (unsigned long)abySeq[ii] << (ii * 8);
548 dwKeyIndex |= 1 << 29;
549 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = KeyRSC;
552 if (param->u.crypt.alg == WPA_ALG_TKIP) {
553 if (param->u.crypt.key_len != MAX_KEY_LEN)
555 pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
556 byKeyDecMode = KEY_CTL_TKIP;
557 pMgmt->byCSSPK = KEY_CTL_TKIP;
558 pMgmt->byCSSGK = KEY_CTL_TKIP;
561 if (param->u.crypt.alg == WPA_ALG_CCMP) {
562 if ((param->u.crypt.key_len != AES_KEY_LEN) ||
563 (pDevice->byLocalID <= REV_ID_VT3253_A1))
565 pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
566 byKeyDecMode = KEY_CTL_CCMP;
567 pMgmt->byCSSPK = KEY_CTL_CCMP;
568 pMgmt->byCSSGK = KEY_CTL_CCMP;
572 if (iNodeIndex == 0) {
573 KeybSetDefaultKey( pDevice,
576 param->u.crypt.key_len,
581 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = TRUE;
584 dwKeyIndex |= (1 << 30); // set pairwise key
585 if (KeybSetKey(pDevice,
589 param->u.crypt.key_len,
595 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = TRUE;
599 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = FALSE;
600 bKeyTableFull = TRUE;
601 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Key Table Full\n");
606 if (bKeyTableFull == TRUE) {
607 wKeyCtl &= 0x7F00; // clear all key control filed
608 wKeyCtl |= (byKeyDecMode << 4);
609 wKeyCtl |= (byKeyDecMode);
610 wKeyCtl |= 0x0044; // use group key for all address
611 wKeyCtl |= 0x4000; // disable KeyTable[MAX_KEY_TABLE-1] on-fly to genernate rx int
613 //MACvSetDefaultKeyCtl(pDevice->PortOffset, wKeyCtl, MAX_KEY_TABLE-1, pDevice->byLocalID);
616 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Set key sta_index= %d \n", iNodeIndex);
617 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " tx_index=%d len=%d \n", param->u.crypt.idx,
618 param->u.crypt.key_len );
619 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " key=%x-%x-%x-%x-%x-xxxxx \n",
620 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
621 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[1],
622 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[2],
623 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[3],
624 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[4]
628 pDevice->bEncryptionEnable = TRUE;
629 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = byKeyDecMode;
630 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
631 pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
632 pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
641 * get each stations encryption key
652 static int hostap_get_encryption(PSDevice pDevice,
653 struct viawget_hostapd_param *param,
656 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
662 param->u.crypt.err = 0;
664 if (is_broadcast_ether_addr(param->sta_addr)) {
667 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &iNodeIndex) == FALSE) {
668 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
669 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
673 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: %d\n", iNodeIndex);
674 memset(param->u.crypt.seq, 0, 8);
675 for (ii = 0 ; ii < 8 ; ii++) {
676 param->u.crypt.seq[ii] = (BYTE)pMgmt->sNodeDBTable[iNodeIndex].KeyRSC >> (ii * 8);
685 * vt6656_hostap_ioctl main function supported for hostap daemon.
697 int vt6656_hostap_ioctl(PSDevice pDevice, struct iw_point *p)
699 struct viawget_hostapd_param *param;
703 if (p->length < sizeof(struct viawget_hostapd_param) ||
704 p->length > VIAWGET_HOSTAPD_MAX_BUF_SIZE || !p->pointer)
707 param = kmalloc((int)p->length, (int)GFP_KERNEL);
711 if (copy_from_user(param, p->pointer, p->length)) {
716 switch (param->cmd) {
717 case VIAWGET_HOSTAPD_SET_ENCRYPTION:
718 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ENCRYPTION \n");
719 spin_lock_irq(&pDevice->lock);
720 ret = hostap_set_encryption(pDevice, param, p->length);
721 spin_unlock_irq(&pDevice->lock);
723 case VIAWGET_HOSTAPD_GET_ENCRYPTION:
724 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_ENCRYPTION \n");
725 spin_lock_irq(&pDevice->lock);
726 ret = hostap_get_encryption(pDevice, param, p->length);
727 spin_unlock_irq(&pDevice->lock);
729 case VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR:
730 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR \n");
733 case VIAWGET_HOSTAPD_FLUSH:
734 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_FLUSH \n");
735 spin_lock_irq(&pDevice->lock);
736 hostap_flush_sta(pDevice);
737 spin_unlock_irq(&pDevice->lock);
739 case VIAWGET_HOSTAPD_ADD_STA:
740 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_ADD_STA \n");
741 spin_lock_irq(&pDevice->lock);
742 ret = hostap_add_sta(pDevice, param);
743 spin_unlock_irq(&pDevice->lock);
745 case VIAWGET_HOSTAPD_REMOVE_STA:
746 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_REMOVE_STA \n");
747 spin_lock_irq(&pDevice->lock);
748 ret = hostap_remove_sta(pDevice, param);
749 spin_unlock_irq(&pDevice->lock);
751 case VIAWGET_HOSTAPD_GET_INFO_STA:
752 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_INFO_STA \n");
753 ret = hostap_get_info_sta(pDevice, param);
756 case VIAWGET_HOSTAPD_SET_FLAGS_STA:
757 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_FLAGS_STA \n");
758 ret = hostap_set_flags_sta(pDevice, param);
761 case VIAWGET_HOSTAPD_MLME:
762 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_MLME \n");
765 case VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT:
766 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT \n");
767 ret = hostap_set_generic_element(pDevice, param);
770 case VIAWGET_HOSTAPD_SCAN_REQ:
771 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SCAN_REQ \n");
774 case VIAWGET_HOSTAPD_STA_CLEAR_STATS:
775 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_STA_CLEAR_STATS \n");
779 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "vt6656_hostap_ioctl: unknown cmd=%d\n",
786 if ((ret == 0) && ap_ioctl) {
787 if (copy_to_user(p->pointer, param, p->length)) {