ctrl_iface_ap.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Control interface for shared AP commands
  3. * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "common/ieee802_11_defs.h"
  11. #include "hostapd.h"
  12. #include "ieee802_1x.h"
  13. #include "wpa_auth.h"
  14. #include "ieee802_11.h"
  15. #include "sta_info.h"
  16. #include "wps_hostapd.h"
  17. #include "p2p_hostapd.h"
  18. #include "ctrl_iface_ap.h"
  19. #include "ap_drv_ops.h"
  20. static int hostapd_get_sta_conn_time(struct sta_info *sta,
  21. char *buf, size_t buflen)
  22. {
  23. struct os_time now, age;
  24. int len = 0, ret;
  25. if (!sta->connected_time.sec)
  26. return 0;
  27. os_get_time(&now);
  28. os_time_sub(&now, &sta->connected_time, &age);
  29. ret = os_snprintf(buf + len, buflen - len, "connected_time=%u\n",
  30. (unsigned int) age.sec);
  31. if (ret < 0 || (size_t) ret >= buflen - len)
  32. return len;
  33. len += ret;
  34. return len;
  35. }
  36. static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
  37. struct sta_info *sta,
  38. char *buf, size_t buflen)
  39. {
  40. int len, res, ret;
  41. if (sta == NULL) {
  42. ret = os_snprintf(buf, buflen, "FAIL\n");
  43. if (ret < 0 || (size_t) ret >= buflen)
  44. return 0;
  45. return ret;
  46. }
  47. len = 0;
  48. ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
  49. MAC2STR(sta->addr));
  50. if (ret < 0 || (size_t) ret >= buflen - len)
  51. return len;
  52. len += ret;
  53. res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
  54. if (res >= 0)
  55. len += res;
  56. res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
  57. if (res >= 0)
  58. len += res;
  59. res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
  60. if (res >= 0)
  61. len += res;
  62. res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
  63. buflen - len);
  64. if (res >= 0)
  65. len += res;
  66. res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
  67. if (res >= 0)
  68. len += res;
  69. res = hostapd_get_sta_conn_time(sta, buf + len, buflen - len);
  70. if (res >= 0)
  71. len += res;
  72. return len;
  73. }
  74. int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
  75. char *buf, size_t buflen)
  76. {
  77. return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
  78. }
  79. int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
  80. char *buf, size_t buflen)
  81. {
  82. u8 addr[ETH_ALEN];
  83. int ret;
  84. if (hwaddr_aton(txtaddr, addr)) {
  85. ret = os_snprintf(buf, buflen, "FAIL\n");
  86. if (ret < 0 || (size_t) ret >= buflen)
  87. return 0;
  88. return ret;
  89. }
  90. return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
  91. buf, buflen);
  92. }
  93. int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
  94. char *buf, size_t buflen)
  95. {
  96. u8 addr[ETH_ALEN];
  97. struct sta_info *sta;
  98. int ret;
  99. if (hwaddr_aton(txtaddr, addr) ||
  100. (sta = ap_get_sta(hapd, addr)) == NULL) {
  101. ret = os_snprintf(buf, buflen, "FAIL\n");
  102. if (ret < 0 || (size_t) ret >= buflen)
  103. return 0;
  104. return ret;
  105. }
  106. return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
  107. }
  108. #ifdef CONFIG_P2P_MANAGER
  109. static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
  110. u8 minor_reason_code, const u8 *addr)
  111. {
  112. struct ieee80211_mgmt *mgmt;
  113. int ret;
  114. u8 *pos;
  115. if (hapd->driver->send_frame == NULL)
  116. return -1;
  117. mgmt = os_zalloc(sizeof(*mgmt) + 100);
  118. if (mgmt == NULL)
  119. return -1;
  120. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
  121. " with minor reason code %u (stype=%u)",
  122. MAC2STR(addr), minor_reason_code, stype);
  123. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
  124. os_memcpy(mgmt->da, addr, ETH_ALEN);
  125. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  126. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  127. if (stype == WLAN_FC_STYPE_DEAUTH) {
  128. mgmt->u.deauth.reason_code =
  129. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  130. pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
  131. } else {
  132. mgmt->u.disassoc.reason_code =
  133. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  134. pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
  135. }
  136. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  137. *pos++ = 4 + 3 + 1;
  138. WPA_PUT_BE24(pos, OUI_WFA);
  139. pos += 3;
  140. *pos++ = P2P_OUI_TYPE;
  141. *pos++ = P2P_ATTR_MINOR_REASON_CODE;
  142. WPA_PUT_LE16(pos, 1);
  143. pos += 2;
  144. *pos++ = minor_reason_code;
  145. ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
  146. pos - (u8 *) mgmt, 1);
  147. os_free(mgmt);
  148. return ret < 0 ? -1 : 0;
  149. }
  150. #endif /* CONFIG_P2P_MANAGER */
  151. int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
  152. const char *txtaddr)
  153. {
  154. u8 addr[ETH_ALEN];
  155. struct sta_info *sta;
  156. const char *pos;
  157. u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
  158. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
  159. txtaddr);
  160. if (hwaddr_aton(txtaddr, addr))
  161. return -1;
  162. pos = os_strstr(txtaddr, " test=");
  163. if (pos) {
  164. struct ieee80211_mgmt mgmt;
  165. int encrypt;
  166. if (hapd->driver->send_frame == NULL)
  167. return -1;
  168. pos += 6;
  169. encrypt = atoi(pos);
  170. os_memset(&mgmt, 0, sizeof(mgmt));
  171. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  172. WLAN_FC_STYPE_DEAUTH);
  173. os_memcpy(mgmt.da, addr, ETH_ALEN);
  174. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  175. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  176. mgmt.u.deauth.reason_code =
  177. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  178. if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
  179. IEEE80211_HDRLEN +
  180. sizeof(mgmt.u.deauth),
  181. encrypt) < 0)
  182. return -1;
  183. return 0;
  184. }
  185. #ifdef CONFIG_P2P_MANAGER
  186. pos = os_strstr(txtaddr, " p2p=");
  187. if (pos) {
  188. return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
  189. atoi(pos + 5), addr);
  190. }
  191. #endif /* CONFIG_P2P_MANAGER */
  192. pos = os_strstr(txtaddr, " reason=");
  193. if (pos)
  194. reason = atoi(pos + 8);
  195. hostapd_drv_sta_deauth(hapd, addr, reason);
  196. sta = ap_get_sta(hapd, addr);
  197. if (sta)
  198. ap_sta_deauthenticate(hapd, sta, reason);
  199. else if (addr[0] == 0xff)
  200. hostapd_free_stas(hapd);
  201. return 0;
  202. }
  203. int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
  204. const char *txtaddr)
  205. {
  206. u8 addr[ETH_ALEN];
  207. struct sta_info *sta;
  208. const char *pos;
  209. u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
  210. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
  211. txtaddr);
  212. if (hwaddr_aton(txtaddr, addr))
  213. return -1;
  214. pos = os_strstr(txtaddr, " test=");
  215. if (pos) {
  216. struct ieee80211_mgmt mgmt;
  217. int encrypt;
  218. if (hapd->driver->send_frame == NULL)
  219. return -1;
  220. pos += 6;
  221. encrypt = atoi(pos);
  222. os_memset(&mgmt, 0, sizeof(mgmt));
  223. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  224. WLAN_FC_STYPE_DISASSOC);
  225. os_memcpy(mgmt.da, addr, ETH_ALEN);
  226. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  227. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  228. mgmt.u.disassoc.reason_code =
  229. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  230. if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
  231. IEEE80211_HDRLEN +
  232. sizeof(mgmt.u.deauth),
  233. encrypt) < 0)
  234. return -1;
  235. return 0;
  236. }
  237. #ifdef CONFIG_P2P_MANAGER
  238. pos = os_strstr(txtaddr, " p2p=");
  239. if (pos) {
  240. return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
  241. atoi(pos + 5), addr);
  242. }
  243. #endif /* CONFIG_P2P_MANAGER */
  244. pos = os_strstr(txtaddr, " reason=");
  245. if (pos)
  246. reason = atoi(pos + 8);
  247. hostapd_drv_sta_disassoc(hapd, addr, reason);
  248. sta = ap_get_sta(hapd, addr);
  249. if (sta)
  250. ap_sta_disassociate(hapd, sta, reason);
  251. else if (addr[0] == 0xff)
  252. hostapd_free_stas(hapd);
  253. return 0;
  254. }
  255. int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
  256. size_t buflen)
  257. {
  258. struct hostapd_iface *iface = hapd->iface;
  259. int len = 0, ret;
  260. size_t i;
  261. ret = os_snprintf(buf + len, buflen - len,
  262. "state=%s\n"
  263. "phy=%s\n"
  264. "freq=%d\n"
  265. "num_sta_non_erp=%d\n"
  266. "num_sta_no_short_slot_time=%d\n"
  267. "num_sta_no_short_preamble=%d\n"
  268. "olbc=%d\n"
  269. "num_sta_ht_no_gf=%d\n"
  270. "num_sta_no_ht=%d\n"
  271. "num_sta_ht_20_mhz=%d\n"
  272. "olbc_ht=%d\n"
  273. "ht_op_mode=0x%x\n",
  274. hostapd_state_text(iface->state),
  275. iface->phy,
  276. iface->freq,
  277. iface->num_sta_non_erp,
  278. iface->num_sta_no_short_slot_time,
  279. iface->num_sta_no_short_preamble,
  280. iface->olbc,
  281. iface->num_sta_ht_no_gf,
  282. iface->num_sta_no_ht,
  283. iface->num_sta_ht_20mhz,
  284. iface->olbc_ht,
  285. iface->ht_op_mode);
  286. if (ret < 0 || (size_t) ret >= buflen - len)
  287. return len;
  288. len += ret;
  289. ret = os_snprintf(buf + len, buflen - len,
  290. "channel=%u\n"
  291. "secondary_channel=%d\n"
  292. "ieee80211n=%d\n"
  293. "ieee80211ac=%d\n"
  294. "vht_oper_chwidth=%d\n"
  295. "vht_oper_centr_freq_seg0_idx=%d\n"
  296. "vht_oper_centr_freq_seg1_idx=%d\n",
  297. iface->conf->channel,
  298. iface->conf->secondary_channel,
  299. iface->conf->ieee80211n,
  300. iface->conf->ieee80211ac,
  301. iface->conf->vht_oper_chwidth,
  302. iface->conf->vht_oper_centr_freq_seg0_idx,
  303. iface->conf->vht_oper_centr_freq_seg1_idx);
  304. if (ret < 0 || (size_t) ret >= buflen - len)
  305. return len;
  306. len += ret;
  307. for (i = 0; i < iface->num_bss; i++) {
  308. struct hostapd_data *bss = iface->bss[i];
  309. ret = os_snprintf(buf + len, buflen - len,
  310. "bss[%d]=%s\n"
  311. "bssid[%d]=" MACSTR "\n"
  312. "ssid[%d]=%s\n"
  313. "num_sta[%d]=%d\n",
  314. (int) i, bss->conf->iface,
  315. (int) i, MAC2STR(bss->own_addr),
  316. (int) i,
  317. wpa_ssid_txt(bss->conf->ssid.ssid,
  318. bss->conf->ssid.ssid_len),
  319. (int) i, bss->num_sta);
  320. if (ret < 0 || (size_t) ret >= buflen - len)
  321. return len;
  322. len += ret;
  323. }
  324. return len;
  325. }