static int wpa_driver_privsep_scan(void *priv, struct wpa_driver_scan_params *params) { struct wpa_driver_privsep_data *drv = priv; struct privsep_cmd_scan scan; size_t i; wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv); os_memset(&scan, 0, sizeof(scan)); scan.num_ssids = params->num_ssids; for (i = 0; i < params->num_ssids; i++) { if (!params->ssids[i].ssid) continue; scan.ssid_lens[i] = params->ssids[i].ssid_len; os_memcpy(scan.ssids[i], params->ssids[i].ssid, scan.ssid_lens[i]); } for (i = 0; i < PRIVSEP_MAX_SCAN_FREQS && params->freqs && params->freqs[i]; i++) scan.freqs[i] = params->freqs[i]; scan.num_freqs = i; return wpa_priv_cmd(drv, PRIVSEP_CMD_SCAN, &scan, sizeof(scan), NULL, NULL); }
static int wpa_driver_privsep_set_country(void *priv, const char *alpha2) { struct wpa_driver_privsep_data *drv = priv; wpa_printf(MSG_DEBUG, "%s country='%s'", __func__, alpha2); return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_COUNTRY, alpha2, os_strlen(alpha2), NULL, NULL); }
static int wpa_driver_privsep_set_key(const char *ifname, void *priv, enum wpa_alg alg, const u8 *addr, int key_idx, int set_tx, const u8 *seq, size_t seq_len, const u8 *key, size_t key_len) { struct wpa_driver_privsep_data *drv = priv; struct privsep_cmd_set_key cmd; wpa_printf(MSG_DEBUG, "%s: priv=%p alg=%d key_idx=%d set_tx=%d", __func__, priv, alg, key_idx, set_tx); os_memset(&cmd, 0, sizeof(cmd)); cmd.alg = alg; if (addr) os_memcpy(cmd.addr, addr, ETH_ALEN); else os_memset(cmd.addr, 0xff, ETH_ALEN); cmd.key_idx = key_idx; cmd.set_tx = set_tx; if (seq && seq_len > 0 && seq_len < sizeof(cmd.seq)) { os_memcpy(cmd.seq, seq, seq_len); cmd.seq_len = seq_len; } if (key && key_len > 0 && key_len < sizeof(cmd.key)) { os_memcpy(cmd.key, key, key_len); cmd.key_len = key_len; } return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_KEY, &cmd, sizeof(cmd), NULL, NULL); }
static int wpa_driver_privsep_scan(void *priv, const u8 *ssid, size_t ssid_len) { struct wpa_driver_privsep_data *drv = priv; wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv); return wpa_priv_cmd(drv, PRIVSEP_CMD_SCAN, ssid, ssid_len, NULL, NULL); }
static int wpa_driver_privsep_set_wpa(void *priv, int enabled) { struct wpa_driver_privsep_data *drv = priv; wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_WPA, &enabled, sizeof(enabled), NULL, NULL); }
static int wpa_driver_privsep_scan(void *priv, struct wpa_driver_scan_params *params) { struct wpa_driver_privsep_data *drv = priv; const u8 *ssid = params->ssids[0].ssid; size_t ssid_len = params->ssids[0].ssid_len; wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv); return wpa_priv_cmd(drv, PRIVSEP_CMD_SCAN, ssid, ssid_len, NULL, NULL); }
static int wpa_driver_privsep_get_bssid(void *priv, u8 *bssid) { struct wpa_driver_privsep_data *drv = priv; int res; size_t len = ETH_ALEN; res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_BSSID, NULL, 0, bssid, &len); if (res < 0 || len != ETH_ALEN) return -1; return 0; }
static int wpa_driver_privsep_get_capa(void *priv, struct wpa_driver_capa *capa) { struct wpa_driver_privsep_data *drv = priv; int res; size_t len = sizeof(*capa); res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_CAPA, NULL, 0, capa, &len); if (res < 0 || len != sizeof(*capa)) return -1; return 0; }
static int wpa_driver_privsep_authenticate( void *priv, struct wpa_driver_auth_params *params) { struct wpa_driver_privsep_data *drv = priv; struct privsep_cmd_authenticate *data; int i, res; size_t buflen; u8 *pos; wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d bssid=" MACSTR " auth_alg=%d local_state_change=%d p2p=%d", __func__, priv, params->freq, MAC2STR(params->bssid), params->auth_alg, params->local_state_change, params->p2p); buflen = sizeof(*data) + params->ie_len + params->auth_data_len; data = os_zalloc(buflen); if (data == NULL) return -1; data->freq = params->freq; os_memcpy(data->bssid, params->bssid, ETH_ALEN); os_memcpy(data->ssid, params->ssid, params->ssid_len); data->ssid_len = params->ssid_len; data->auth_alg = params->auth_alg; data->ie_len = params->ie_len; for (i = 0; i < 4; i++) { if (params->wep_key[i]) os_memcpy(data->wep_key[i], params->wep_key[i], params->wep_key_len[i]); data->wep_key_len[i] = params->wep_key_len[i]; } data->wep_tx_keyidx = params->wep_tx_keyidx; data->local_state_change = params->local_state_change; data->p2p = params->p2p; pos = (u8 *) (data + 1); if (params->ie_len) { os_memcpy(pos, params->ie, params->ie_len); pos += params->ie_len; } if (params->auth_data_len) os_memcpy(pos, params->auth_data, params->auth_data_len); res = wpa_priv_cmd(drv, PRIVSEP_CMD_AUTHENTICATE, data, buflen, NULL, NULL); os_free(data); return res; }
static int wpa_driver_privsep_get_capa(void *priv, struct wpa_driver_capa *capa) { struct wpa_driver_privsep_data *drv = priv; int res; size_t len = sizeof(*capa); res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_CAPA, NULL, 0, capa, &len); if (res < 0 || len != sizeof(*capa)) return -1; /* For now, no support for passing extended_capa pointers */ capa->extended_capa = NULL; capa->extended_capa_mask = NULL; capa->extended_capa_len = 0; return 0; }
static int wpa_driver_privsep_get_ssid(void *priv, u8 *ssid) { struct wpa_driver_privsep_data *drv = priv; int res, ssid_len; u8 reply[sizeof(int) + 32]; size_t len = sizeof(reply); res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_SSID, NULL, 0, reply, &len); if (res < 0 || len < sizeof(int)) return -1; os_memcpy(&ssid_len, reply, sizeof(int)); if (ssid_len < 0 || ssid_len > 32 || sizeof(int) + ssid_len > len) { wpa_printf(MSG_DEBUG, "privsep: Invalid get SSID reply"); return -1; } os_memcpy(ssid, &reply[sizeof(int)], ssid_len); return ssid_len; }
void l2_packet_deinit(struct l2_packet_data *l2) { if (l2 == NULL) return; if (l2->fd >= 0) { wpa_priv_cmd(l2, PRIVSEP_CMD_L2_UNREGISTER, NULL, 0); eloop_unregister_read_sock(l2->fd); close(l2->fd); } if (l2->own_socket_path) { unlink(l2->own_socket_path); os_free(l2->own_socket_path); } os_free(l2); }
static int wpa_driver_privsep_associate( void *priv, struct wpa_driver_associate_params *params) { struct wpa_driver_privsep_data *drv = priv; struct privsep_cmd_associate *data; int res; size_t buflen; wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d " "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d", __func__, priv, params->freq.freq, params->pairwise_suite, params->group_suite, params->key_mgmt_suite, params->auth_alg, params->mode); buflen = sizeof(*data) + params->wpa_ie_len; data = os_zalloc(buflen); if (data == NULL) return -1; if (params->bssid) os_memcpy(data->bssid, params->bssid, ETH_ALEN); os_memcpy(data->ssid, params->ssid, params->ssid_len); data->ssid_len = params->ssid_len; data->hwmode = params->freq.mode; data->freq = params->freq.freq; data->channel = params->freq.channel; data->pairwise_suite = params->pairwise_suite; data->group_suite = params->group_suite; data->key_mgmt_suite = params->key_mgmt_suite; data->auth_alg = params->auth_alg; data->mode = params->mode; data->wpa_ie_len = params->wpa_ie_len; if (params->wpa_ie) os_memcpy(data + 1, params->wpa_ie, params->wpa_ie_len); /* TODO: add support for other assoc parameters */ res = wpa_priv_cmd(drv, PRIVSEP_CMD_ASSOCIATE, data, buflen, NULL, NULL); os_free(data); return res; }
static struct wpa_scan_results * wpa_driver_privsep_get_scan_results2(void *priv) { struct wpa_driver_privsep_data *drv = priv; int res, num; u8 *buf, *pos, *end; size_t reply_len = 60000; struct wpa_scan_results *results; struct wpa_scan_res *r; buf = os_malloc(reply_len); if (buf == NULL) return NULL; res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_SCAN_RESULTS, NULL, 0, buf, &reply_len); if (res < 0) { os_free(buf); return NULL; } wpa_printf(MSG_DEBUG, "privsep: Received %lu bytes of scan results", (unsigned long) reply_len); if (reply_len < sizeof(int)) { wpa_printf(MSG_DEBUG, "privsep: Invalid scan result len %lu", (unsigned long) reply_len); os_free(buf); return NULL; } pos = buf; end = buf + reply_len; os_memcpy(&num, pos, sizeof(int)); if (num < 0 || num > 1000) { os_free(buf); return NULL; } pos += sizeof(int); results = os_zalloc(sizeof(*results)); if (results == NULL) { os_free(buf); return NULL; } results->res = os_calloc(num, sizeof(struct wpa_scan_res *)); if (results->res == NULL) { os_free(results); os_free(buf); return NULL; } while (results->num < (size_t) num && pos + sizeof(int) < end) { int len; os_memcpy(&len, pos, sizeof(int)); pos += sizeof(int); if (len < 0 || len > 10000 || pos + len > end) break; r = os_malloc(len); if (r == NULL) break; os_memcpy(r, pos, len); pos += len; if (sizeof(*r) + r->ie_len > (size_t) len) { os_free(r); break; } results->res[results->num++] = r; } os_free(buf); return results; }
void l2_packet_notify_auth_start(struct l2_packet_data *l2) { wpa_priv_cmd(l2, PRIVSEP_CMD_L2_NOTIFY_AUTH_START, NULL, 0); }
struct l2_packet_data * l2_packet_init( const char *ifname, const u8 *own_addr, unsigned short protocol, void (*rx_callback)(void *ctx, const u8 *src_addr, const u8 *buf, size_t len), void *rx_callback_ctx, int l2_hdr) { struct l2_packet_data *l2; char *own_dir = "/tmp"; char *priv_dir = "/var/run/wpa_priv"; size_t len; static unsigned int counter = 0; struct sockaddr_un addr; fd_set rfds; struct timeval tv; int res; u8 reply[ETH_ALEN + 1]; int reg_cmd[2]; l2 = os_zalloc(sizeof(struct l2_packet_data)); if (l2 == NULL) return NULL; l2->rx_callback = rx_callback; l2->rx_callback_ctx = rx_callback_ctx; len = os_strlen(own_dir) + 50; l2->own_socket_path = os_malloc(len); if (l2->own_socket_path == NULL) { os_free(l2); return NULL; } os_snprintf(l2->own_socket_path, len, "%s/wpa_privsep-l2-%d-%d", own_dir, getpid(), counter++); l2->priv_addr.sun_family = AF_UNIX; os_snprintf(l2->priv_addr.sun_path, sizeof(l2->priv_addr.sun_path), "%s/%s", priv_dir, ifname); l2->fd = socket(PF_UNIX, SOCK_DGRAM, 0); if (l2->fd < 0) { wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); os_free(l2->own_socket_path); l2->own_socket_path = NULL; os_free(l2); return NULL; } os_memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; os_strlcpy(addr.sun_path, l2->own_socket_path, sizeof(addr.sun_path)); if (bind(l2->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { wpa_printf(MSG_ERROR, "l2-pkt-privsep: bind(PF_UNIX): %s", strerror(errno)); goto fail; } reg_cmd[0] = protocol; reg_cmd[1] = l2_hdr; if (wpa_priv_cmd(l2, PRIVSEP_CMD_L2_REGISTER, reg_cmd, sizeof(reg_cmd)) < 0) { wpa_printf(MSG_ERROR, "L2: Failed to register with wpa_priv"); goto fail; } FD_ZERO(&rfds); FD_SET(l2->fd, &rfds); tv.tv_sec = 5; tv.tv_usec = 0; res = select(l2->fd + 1, &rfds, NULL, NULL, &tv); if (res < 0 && errno != EINTR) { wpa_printf(MSG_ERROR, "select: %s", strerror(errno)); goto fail; } if (FD_ISSET(l2->fd, &rfds)) { res = recv(l2->fd, reply, sizeof(reply), 0); if (res < 0) { wpa_printf(MSG_ERROR, "recv: %s", strerror(errno)); goto fail; } } else { wpa_printf(MSG_DEBUG, "L2: Timeout while waiting for " "registration reply"); goto fail; } if (res != ETH_ALEN) { wpa_printf(MSG_DEBUG, "L2: Unexpected registration reply " "(len=%d)", res); } os_memcpy(l2->own_addr, reply, ETH_ALEN); eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL); return l2; fail: close(l2->fd); l2->fd = -1; unlink(l2->own_socket_path); os_free(l2->own_socket_path); l2->own_socket_path = NULL; os_free(l2); return NULL; }