📄 driver.c
字号:
*i++ = param; *i++ = value; if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) { perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]"); return -1; } return 0;}static int hostap_set_ieee8021x(void *priv, int enabled){ struct hostap_driver_data *drv = priv; /* enable kernel driver support for IEEE 802.1X */ if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) { printf("Could not setup IEEE 802.1X support in kernel driver." "\n"); return -1; } if (!enabled) return 0; /* use host driver implementation of encryption to allow * individual keys and passing plaintext EAPOL frames */ if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) || hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) { printf("Could not setup host-based encryption in kernel " "driver.\n"); return -1; } return 0;}static int hostap_set_privacy(void *priv, int enabled){ struct hostap_drvier_data *drv = priv; return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED, enabled);} static int hostap_set_ssid(void *priv, u8 *buf, int len){ struct hostap_driver_data *drv = priv; struct iwreq iwr; memset(&iwr, 0, sizeof(iwr)); strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ); iwr.u.essid.flags = 1; /* SSID active */ iwr.u.essid.pointer = (caddr_t) buf; iwr.u.essid.length = len + 1; if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) { perror("ioctl[SIOCSIWESSID]"); printf("len=%d\n", len); return -1; } return 0;}static int hostap_flush(void *priv){ struct hostap_driver_data *drv = priv; struct prism2_hostapd_param param; memset(¶m, 0, sizeof(param)); param.cmd = PRISM2_HOSTAPD_FLUSH; return hostapd_ioctl(drv, ¶m, sizeof(param));}static int hostap_read_sta_data(void *priv, struct hostap_sta_driver_data *data, u8 *addr){ struct hostap_driver_data *drv = priv; char buf[1024], line[128], *pos; FILE *f; unsigned long val; memset(data, 0, sizeof(*data)); snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR, drv->iface, MAC2STR(addr)); f = fopen(buf, "r"); if (!f) return -1; /* Need to read proc file with in one piece, so use large enough * buffer. */ setbuffer(f, buf, sizeof(buf)); while (fgets(line, sizeof(line), f)) { pos = strchr(line, '='); if (!pos) continue; *pos++ = '\0'; val = strtoul(pos, NULL, 10); if (strcmp(line, "rx_packets") == 0) data->rx_packets = val; else if (strcmp(line, "tx_packets") == 0) data->tx_packets = val; else if (strcmp(line, "rx_bytes") == 0) data->rx_bytes = val; else if (strcmp(line, "tx_bytes") == 0) data->tx_bytes = val; } fclose(f); return 0;}static int hostap_sta_add(void *priv, u8 *addr, u16 aid, u16 capability, u8 tx_supp_rates){ struct hostap_driver_data *drv = priv; struct prism2_hostapd_param param; memset(¶m, 0, sizeof(param)); param.cmd = PRISM2_HOSTAPD_ADD_STA; memcpy(param.sta_addr, addr, ETH_ALEN); param.u.add_sta.aid = aid; param.u.add_sta.capability = capability; param.u.add_sta.tx_supp_rates = tx_supp_rates; return hostapd_ioctl(drv, ¶m, sizeof(param));}static int hostap_sta_remove(void *priv, u8 *addr){ struct hostap_driver_data *drv = priv; struct prism2_hostapd_param param; hostap_set_sta_authorized(drv, addr, 0); memset(¶m, 0, sizeof(param)); param.cmd = PRISM2_HOSTAPD_REMOVE_STA; memcpy(param.sta_addr, addr, ETH_ALEN); if (hostapd_ioctl(drv, ¶m, sizeof(param))) { printf("Could not remove station from kernel driver.\n"); return -1; } return 0;}static int hostap_get_inact_sec(void *priv, u8 *addr){ struct hostap_driver_data *drv = priv; struct prism2_hostapd_param param; memset(¶m, 0, sizeof(param)); param.cmd = PRISM2_HOSTAPD_GET_INFO_STA; memcpy(param.sta_addr, addr, ETH_ALEN); if (hostapd_ioctl(drv, ¶m, sizeof(param))) { return -1; } return param.u.get_info_sta.inactive_sec;}static int hostap_sta_clear_stats(void *priv, u8 *addr){ struct hostap_driver_data *drv = priv; struct prism2_hostapd_param param; memset(¶m, 0, sizeof(param)); param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS; memcpy(param.sta_addr, addr, ETH_ALEN); if (hostapd_ioctl(drv, ¶m, sizeof(param))) { return -1; } return 0;}static int hostap_set_assoc_ap(void *priv, u8 *addr){ struct hostap_driver_data *drv = priv; struct prism2_hostapd_param param; memset(¶m, 0, sizeof(param)); param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR; memcpy(param.sta_addr, addr, ETH_ALEN); if (hostapd_ioctl(drv, ¶m, sizeof(param))) return -1; return 0;}static int hostap_set_generic_elem(void *priv, const u8 *elem, size_t elem_len){ struct hostap_driver_data *drv = priv; struct prism2_hostapd_param *param; int res; size_t blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len; if (blen < sizeof(*param)) blen = sizeof(*param); param = (struct prism2_hostapd_param *) malloc(blen); if (param == NULL) return -1; memset(param, 0, blen); param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT; param->u.generic_elem.len = elem_len; memcpy(param->u.generic_elem.data, elem, elem_len); res = hostapd_ioctl(drv, param, blen); free(param); return res;}static voidhostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv, char *custom){ struct hostapd_data *hapd = drv->hapd; HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "Custom wireless event: '%s'\n", custom); if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) { char *pos; u8 addr[ETH_ALEN]; pos = strstr(custom, "addr="); if (pos == NULL) { HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "MLME-MICHAELMICFAILURE.indication " "without sender address ignored\n"); return; } pos += 5; if (hwaddr_aton(pos, addr) == 0) { ieee80211_michael_mic_failure(drv->hapd, addr, 1); } else { HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "MLME-MICHAELMICFAILURE.indication " "with invalid MAC address"); } }}static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv, char *data, int len){ struct hostapd_data *hapd = drv->hapd; struct iw_event *iwe; char *pos, *end, *custom, *buf; pos = data; end = data + len; while (pos + IW_EV_LCP_LEN <= end) { iwe = (struct iw_event *) pos; HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE, "Wireless event: " "cmd=0x%x len=%d\n", iwe->cmd, iwe->len); if (iwe->len <= IW_EV_LCP_LEN) return; switch (iwe->cmd) { case IWEVCUSTOM: custom = pos + IW_EV_POINT_LEN; if (custom + iwe->u.data.length > end) return; buf = malloc(iwe->u.data.length + 1); if (buf == NULL) return; memcpy(buf, custom, iwe->u.data.length); buf[iwe->u.data.length] = '\0'; hostapd_wireless_event_wireless_custom(drv, buf); free(buf); break; } pos += iwe->len; }}static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv, struct nlmsghdr *h, int len){ struct ifinfomsg *ifi; int attrlen, nlmsg_len, rta_len; struct rtattr * attr; if (len < sizeof(*ifi)) return; ifi = NLMSG_DATA(h); /* TODO: use ifi->ifi_index to filter out wireless events from other * interfaces */ nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg)); attrlen = h->nlmsg_len - nlmsg_len; if (attrlen < 0) return; attr = (struct rtattr *) (((char *) ifi) + nlmsg_len); rta_len = RTA_ALIGN(sizeof(struct rtattr)); while (RTA_OK(attr, attrlen)) { if (attr->rta_type == IFLA_WIRELESS) { hostapd_wireless_event_wireless( drv, ((char *) attr) + rta_len, attr->rta_len - rta_len); } attr = RTA_NEXT(attr, attrlen); }}static void hostapd_wireless_event_receive(int sock, void *eloop_ctx, void *sock_ctx){ char buf[256]; int left; struct sockaddr_nl from; socklen_t fromlen; struct nlmsghdr *h; struct hostap_driver_data *drv = eloop_ctx; fromlen = sizeof(from); left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *) &from, &fromlen); if (left < 0) { if (errno != EINTR && errno != EAGAIN) perror("recvfrom(netlink)"); return; } h = (struct nlmsghdr *) buf; while (left >= sizeof(*h)) { int len, plen; len = h->nlmsg_len; plen = len - sizeof(*h); if (len > left || plen < 0) { printf("Malformed netlink message: " "len=%d left=%d plen=%d\n", len, left, plen); break; } switch (h->nlmsg_type) { case RTM_NEWLINK: hostapd_wireless_event_rtm_newlink(drv, h, plen); break; } len = NLMSG_ALIGN(len); left -= len; h = (struct nlmsghdr *) ((char *) h + len); } if (left > 0) { printf("%d extra bytes in the end of netlink message\n", left); }}static int hostap_wireless_event_init(void *priv){ struct hostap_driver_data *drv = priv; int s; struct sockaddr_nl local; drv->wext_sock = -1; s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (s < 0) { perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)"); return -1; } memset(&local, 0, sizeof(local)); local.nl_family = AF_NETLINK; local.nl_groups = RTMGRP_LINK; if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) { perror("bind(netlink)"); close(s); return -1; } eloop_register_read_sock(s, hostapd_wireless_event_receive, drv, NULL); drv->wext_sock = s; return 0;}static void hostap_wireless_event_deinit(void *priv){ struct hostap_driver_data *drv = priv; if (drv->wext_sock < 0) return; eloop_unregister_read_sock(drv->wext_sock); close(drv->wext_sock);}static int hostap_init(struct hostapd_data *hapd){ struct hostap_driver_data *drv; drv = malloc(sizeof(struct hostap_driver_data)); if (drv == NULL) { printf("Could not allocate memory for hostapd driver data\n"); return -1; } memset(drv, 0, sizeof(*drv)); drv->ops = hostap_driver_ops; drv->hapd = hapd; drv->ioctl_sock = drv->sock = -1; memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface)); drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0); if (drv->ioctl_sock < 0) { perror("socket[PF_INET,SOCK_DGRAM]"); return -1; } if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) { printf("Could not enable hostapd mode for interface %s\n", drv->iface); return -1; } if (hapd->conf->assoc_ap && hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 1)) { printf("Could not enable hostapd STA mode for interface %s\n", drv->iface); return -1; } if (hostap_init_sockets(drv)) return -1; hapd->driver = &drv->ops; return 0;}static void hostap_driver_deinit(void *priv){ struct hostap_driver_data *drv = priv; drv->hapd->driver = NULL; (void) hostap_set_iface_flags(drv, 0); (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0); (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0); if (drv->ioctl_sock >= 0) close(drv->ioctl_sock); if (drv->sock >= 0) close(drv->sock); free(drv);}static int hostap_sta_deauth(void *priv, u8 *addr, int reason){ struct hostap_driver_data *drv = priv; struct ieee80211_mgmt mgmt; memset(&mgmt, 0, sizeof(mgmt)); mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH); memcpy(mgmt.da, addr, ETH_ALEN); memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN); memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN); mgmt.u.deauth.reason_code = host_to_le16(reason); return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN + sizeof(mgmt.u.deauth), 0);}static int hostap_sta_disassoc(void *priv, u8 *addr, int reason){ struct hostap_driver_data *drv = priv; struct ieee80211_mgmt mgmt; memset(&mgmt, 0, sizeof(mgmt)); mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DISASSOC); memcpy(mgmt.da, addr, ETH_ALEN); memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN); memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN); mgmt.u.disassoc.reason_code = host_to_le16(reason); return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN + sizeof(mgmt.u.disassoc), 0);}static const struct driver_ops hostap_driver_ops = { .name = "hostap", .init = hostap_init, .deinit = hostap_driver_deinit, .wireless_event_init = hostap_wireless_event_init, .wireless_event_deinit = hostap_wireless_event_deinit, .set_ieee8021x = hostap_set_ieee8021x, .set_privacy = hostap_set_privacy, .set_encryption = hostap_set_encryption, .get_seqnum = hostap_get_seqnum, .flush = hostap_flush, .set_generic_elem = hostap_set_generic_elem, .read_sta_data = hostap_read_sta_data, .send_eapol = hostap_send_eapol, .set_sta_authorized = hostap_set_sta_authorized, .sta_deauth = hostap_sta_deauth, .sta_disassoc = hostap_sta_disassoc, .sta_remove = hostap_sta_remove, .set_ssid = hostap_set_ssid, .send_mgmt_frame = hostap_send_mgmt_frame, .set_assoc_ap = hostap_set_assoc_ap, .sta_add = hostap_sta_add, .get_inact_sec = hostap_get_inact_sec, .sta_clear_stats = hostap_sta_clear_stats,};void hostap_driver_register(void){ driver_register(hostap_driver_ops.name, &hostap_driver_ops);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -