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);
137 free_netdev(pDevice->apdev);
138 pDevice->apdev = NULL;
139 pDevice->bEnable8021x = false;
140 pDevice->bEnableHostWEP = false;
141 pDevice->bEncryptionEnable = false;
148 * Set enable/disable hostapd mode
160 int vt6656_hostap_set_hostapd(struct vnt_private *pDevice,
161 int val, int rtnl_locked)
163 if (val < 0 || val > 1)
166 if (pDevice->bEnableHostapd == val)
169 pDevice->bEnableHostapd = val;
172 return hostap_enable_hostapd(pDevice, rtnl_locked);
174 return hostap_disable_hostapd(pDevice, rtnl_locked);
179 * remove station function supported for hostap daemon
190 static int hostap_remove_sta(struct vnt_private *pDevice,
191 struct viawget_hostapd_param *param)
193 unsigned int uNodeIndex;
195 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
196 BSSvRemoveOneNode(pDevice, uNodeIndex);
206 * add a station from hostap daemon
217 static int hostap_add_sta(struct vnt_private *pDevice,
218 struct viawget_hostapd_param *param)
220 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
221 unsigned int uNodeIndex;
223 if (!BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex))
224 BSSvCreateOneNode(pDevice, &uNodeIndex);
226 memcpy(pMgmt->sNodeDBTable[uNodeIndex].abyMACAddr, param->sta_addr, WLAN_ADDR_LEN);
227 pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_ASSOC;
228 pMgmt->sNodeDBTable[uNodeIndex].wCapInfo = param->u.add_sta.capability;
229 // TODO listenInterval
230 // pMgmt->sNodeDBTable[uNodeIndex].wListenInterval = 1;
231 pMgmt->sNodeDBTable[uNodeIndex].bPSEnable = false;
232 pMgmt->sNodeDBTable[uNodeIndex].bySuppRate = param->u.add_sta.tx_supp_rates;
235 pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate =
236 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate;
237 // set max basic rate
238 pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate = RATE_2M;
239 // Todo: check sta preamble, if ap can't support, set status code
240 pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble =
241 WLAN_GET_CAP_INFO_SHORTPREAMBLE(pMgmt->sNodeDBTable[uNodeIndex].wCapInfo);
243 pMgmt->sNodeDBTable[uNodeIndex].wAID = (u16)param->u.add_sta.aid;
245 pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer = jiffies;
247 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Add STA AID= %d \n", pMgmt->sNodeDBTable[uNodeIndex].wAID);
248 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "MAC=%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X \n",
256 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Max Support rate = %d \n",
257 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate);
276 static int hostap_get_info_sta(struct vnt_private *pDevice,
277 struct viawget_hostapd_param *param)
279 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
280 unsigned int uNodeIndex;
282 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
283 param->u.get_info_sta.inactive_sec =
284 (jiffies - pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer) / HZ;
286 //param->u.get_info_sta.txexc = pMgmt->sNodeDBTable[uNodeIndex].uTxAttempts;
308 static int hostap_set_flags_sta(struct vnt_private *pDevice,
309 struct viawget_hostapd_param *param)
311 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
312 unsigned int uNodeIndex;
314 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &uNodeIndex)) {
315 pMgmt->sNodeDBTable[uNodeIndex].dwFlags |= param->u.set_flags_sta.flags_or;
316 pMgmt->sNodeDBTable[uNodeIndex].dwFlags &= param->u.set_flags_sta.flags_and;
317 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " dwFlags = %x\n",
318 (unsigned int) pMgmt->sNodeDBTable[uNodeIndex].dwFlags);
329 * set generic element (wpa ie)
340 static int hostap_set_generic_element(struct vnt_private *pDevice,
341 struct viawget_hostapd_param *param)
343 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
345 memcpy( pMgmt->abyWPAIE,
346 param->u.generic_elem.data,
347 param->u.generic_elem.len
350 pMgmt->wWPAIELen = param->u.generic_elem.len;
352 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pMgmt->wWPAIELen = %d\n", pMgmt->wWPAIELen);
355 if (pMgmt->wWPAIELen == 0) {
356 pMgmt->eAuthenMode = WMAC_AUTH_OPEN;
357 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " No WPAIE, Disable WPA \n");
360 if ((pMgmt->abyWPAIE[0] == WLAN_EID_RSN_WPA) ||
361 (pMgmt->abyWPAIE[0] == WLAN_EID_RSN)) {
362 pMgmt->eAuthenMode = WMAC_AUTH_WPANONE;
363 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set WPAIE enable WPA\n");
373 * flush station nodes table.
384 static void hostap_flush_sta(struct vnt_private *pDevice)
386 // reserved node index =0 for multicast node.
387 BSSvClearNodeDBTable(pDevice, 1);
388 pDevice->uAssocCount = 0;
395 * set each stations encryption key
406 static int hostap_set_encryption(struct vnt_private *pDevice,
407 struct viawget_hostapd_param *param, int param_len)
409 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
411 u8 abyKey[MAX_KEY_LEN];
412 u8 abySeq[MAX_KEY_LEN];
413 NDIS_802_11_KEY_RSC KeyRSC;
414 u8 byKeyDecMode = KEY_CTL_WEP;
418 bool bKeyTableFull = false;
421 param->u.crypt.err = 0;
423 if (param->u.crypt.alg > WPA_ALG_CCMP)
426 if ((param->u.crypt.idx > 3) || (param->u.crypt.key_len > MAX_KEY_LEN)) {
427 param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
428 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_KEY_SET_FAILED\n");
432 if (is_broadcast_ether_addr(param->sta_addr)) {
433 if (param->u.crypt.idx >= MAX_GROUP_KEY)
438 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &iNodeIndex) == false) {
439 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
440 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
444 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: sta_index %d \n", iNodeIndex);
445 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: alg %d \n", param->u.crypt.alg);
447 if (param->u.crypt.alg == WPA_ALG_NONE) {
449 if (pMgmt->sNodeDBTable[iNodeIndex].bOnFly == true) {
450 if (KeybRemoveKey( pDevice,
453 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex
455 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "KeybRemoveKey fail \n");
457 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
459 pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = 0;
460 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = 0;
461 pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = 0;
462 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = 0;
463 pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
464 pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
465 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = 0;
466 memset(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
474 memcpy(abyKey, param->u.crypt.key, param->u.crypt.key_len);
475 // copy to node key tbl
476 pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = param->u.crypt.idx;
477 pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = param->u.crypt.key_len;
478 memcpy(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
480 param->u.crypt.key_len
483 dwKeyIndex = (u32)(param->u.crypt.idx);
484 if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) {
485 pDevice->byKeyIndex = (u8)dwKeyIndex;
486 pDevice->bTransmitKey = true;
487 dwKeyIndex |= (1 << 31);
490 if (param->u.crypt.alg == WPA_ALG_WEP) {
492 if ((pDevice->bEnable8021x == false) || (iNodeIndex == 0)) {
493 KeybSetDefaultKey( pDevice,
495 dwKeyIndex & ~(BIT30 | USE_KEYRSC),
496 param->u.crypt.key_len,
503 // 8021x enable, individual key
504 dwKeyIndex |= (1 << 30); // set pairwise key
505 if (KeybSetKey(pDevice, &(pDevice->sKey),
507 dwKeyIndex & ~(USE_KEYRSC),
508 param->u.crypt.key_len,
509 &KeyRSC, (u8 *)abyKey,
513 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
517 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
518 bKeyTableFull = true;
521 pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
522 pDevice->bEncryptionEnable = true;
523 pMgmt->byCSSPK = KEY_CTL_WEP;
524 pMgmt->byCSSGK = KEY_CTL_WEP;
525 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = KEY_CTL_WEP;
526 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
530 if (param->u.crypt.seq) {
531 memcpy(&abySeq, param->u.crypt.seq, 8);
532 for (ii = 0 ; ii < 8 ; ii++)
533 KeyRSC |= (unsigned long)abySeq[ii] << (ii * 8);
535 dwKeyIndex |= 1 << 29;
536 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = KeyRSC;
539 if (param->u.crypt.alg == WPA_ALG_TKIP) {
540 if (param->u.crypt.key_len != MAX_KEY_LEN)
542 pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
543 byKeyDecMode = KEY_CTL_TKIP;
544 pMgmt->byCSSPK = KEY_CTL_TKIP;
545 pMgmt->byCSSGK = KEY_CTL_TKIP;
548 if (param->u.crypt.alg == WPA_ALG_CCMP) {
549 if ((param->u.crypt.key_len != AES_KEY_LEN) ||
550 (pDevice->byLocalID <= REV_ID_VT3253_A1))
552 pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
553 byKeyDecMode = KEY_CTL_CCMP;
554 pMgmt->byCSSPK = KEY_CTL_CCMP;
555 pMgmt->byCSSGK = KEY_CTL_CCMP;
558 if (iNodeIndex == 0) {
559 KeybSetDefaultKey( pDevice,
562 param->u.crypt.key_len,
567 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
570 dwKeyIndex |= (1 << 30); // set pairwise key
571 if (KeybSetKey(pDevice,
575 param->u.crypt.key_len,
581 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
585 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
586 bKeyTableFull = true;
587 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Key Table Full\n");
592 if (bKeyTableFull == true) {
593 wKeyCtl &= 0x7F00; // clear all key control filed
594 wKeyCtl |= (byKeyDecMode << 4);
595 wKeyCtl |= (byKeyDecMode);
596 wKeyCtl |= 0x0044; // use group key for all address
597 wKeyCtl |= 0x4000; // disable KeyTable[MAX_KEY_TABLE-1] on-fly to genernate rx int
599 //MACvSetDefaultKeyCtl(pDevice->PortOffset, wKeyCtl, MAX_KEY_TABLE-1, pDevice->byLocalID);
602 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Set key sta_index= %d \n", iNodeIndex);
603 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " tx_index=%d len=%d \n", param->u.crypt.idx,
604 param->u.crypt.key_len );
605 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " key=%x-%x-%x-%x-%x-xxxxx \n",
606 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
607 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[1],
608 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[2],
609 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[3],
610 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[4]
614 pDevice->bEncryptionEnable = true;
615 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = byKeyDecMode;
616 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
617 pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
618 pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
625 * get each stations encryption key
636 static int hostap_get_encryption(struct vnt_private *pDevice,
637 struct viawget_hostapd_param *param,
640 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
645 param->u.crypt.err = 0;
647 if (is_broadcast_ether_addr(param->sta_addr)) {
650 if (BSSbIsSTAInNodeDB(pDevice, param->sta_addr, &iNodeIndex) == false) {
651 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
652 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
656 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: %d\n", iNodeIndex);
657 memset(param->u.crypt.seq, 0, 8);
658 for (ii = 0 ; ii < 8 ; ii++) {
659 param->u.crypt.seq[ii] = (u8)pMgmt->sNodeDBTable[iNodeIndex].KeyRSC >> (ii * 8);
667 * vt6656_hostap_ioctl main function supported for hostap daemon.
679 int vt6656_hostap_ioctl(struct vnt_private *pDevice, struct iw_point *p)
681 struct viawget_hostapd_param *param;
685 if (p->length < sizeof(struct viawget_hostapd_param) ||
686 p->length > VIAWGET_HOSTAPD_MAX_BUF_SIZE || !p->pointer)
689 param = kmalloc((int)p->length, GFP_KERNEL);
693 if (copy_from_user(param, p->pointer, p->length)) {
698 switch (param->cmd) {
699 case VIAWGET_HOSTAPD_SET_ENCRYPTION:
700 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ENCRYPTION \n");
701 spin_lock_irq(&pDevice->lock);
702 ret = hostap_set_encryption(pDevice, param, p->length);
703 spin_unlock_irq(&pDevice->lock);
705 case VIAWGET_HOSTAPD_GET_ENCRYPTION:
706 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_ENCRYPTION \n");
707 spin_lock_irq(&pDevice->lock);
708 ret = hostap_get_encryption(pDevice, param, p->length);
709 spin_unlock_irq(&pDevice->lock);
711 case VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR:
712 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR \n");
715 case VIAWGET_HOSTAPD_FLUSH:
716 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_FLUSH \n");
717 spin_lock_irq(&pDevice->lock);
718 hostap_flush_sta(pDevice);
719 spin_unlock_irq(&pDevice->lock);
721 case VIAWGET_HOSTAPD_ADD_STA:
722 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_ADD_STA \n");
723 spin_lock_irq(&pDevice->lock);
724 ret = hostap_add_sta(pDevice, param);
725 spin_unlock_irq(&pDevice->lock);
727 case VIAWGET_HOSTAPD_REMOVE_STA:
728 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_REMOVE_STA \n");
729 spin_lock_irq(&pDevice->lock);
730 ret = hostap_remove_sta(pDevice, param);
731 spin_unlock_irq(&pDevice->lock);
733 case VIAWGET_HOSTAPD_GET_INFO_STA:
734 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_INFO_STA \n");
735 ret = hostap_get_info_sta(pDevice, param);
738 case VIAWGET_HOSTAPD_SET_FLAGS_STA:
739 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_FLAGS_STA \n");
740 ret = hostap_set_flags_sta(pDevice, param);
743 case VIAWGET_HOSTAPD_MLME:
744 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_MLME \n");
747 case VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT:
748 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT \n");
749 ret = hostap_set_generic_element(pDevice, param);
752 case VIAWGET_HOSTAPD_SCAN_REQ:
753 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SCAN_REQ \n");
756 case VIAWGET_HOSTAPD_STA_CLEAR_STATS:
757 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_STA_CLEAR_STATS \n");
761 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "vt6656_hostap_ioctl: unknown cmd=%d\n",
767 if ((ret == 0) && ap_ioctl) {
768 if (copy_to_user(p->pointer, param, p->length)) {