-#ifdef CONFIG_CLIENT_MLME
-static int wpa_driver_nl80211_open_mlme(struct wpa_driver_nl80211_data *drv)
-{
- if (wpa_driver_nl80211_set_userspace_mlme(drv, 1) < 0) {
- wpa_printf(MSG_ERROR, "nl80211: Failed to enable userspace "
- "MLME");
- return -1;
- }
- if (wpa_driver_nl80211_create_monitor_interface(drv)) {
- wpa_printf(MSG_ERROR, "nl80211: Failed to create monitor "
- "interface");
- return -1;
- }
- return 0;
-}
-#endif /* CONFIG_CLIENT_MLME */
-
-
-static int wpa_driver_nl80211_set_param(void *priv, const char *param)
-{
-#ifdef CONFIG_CLIENT_MLME
- struct wpa_driver_nl80211_data *drv = priv;
-
- if (param == NULL)
- return 0;
-
- wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
-
- if (os_strstr(param, "use_mlme=1")) {
- wpa_printf(MSG_DEBUG, "nl80211: Using user space MLME");
- drv->capa.flags |= WPA_DRIVER_FLAGS_USER_SPACE_MLME;
-
- if (wpa_driver_nl80211_open_mlme(drv))
- return -1;
- }
-#endif /* CONFIG_CLIENT_MLME */
-
- return 0;
-}
-
-
-#ifdef CONFIG_CLIENT_MLME
-
-static int ack_wait_handler(struct nl_msg *msg, void *arg)
-{
- int *finished = arg;
-
- *finished = 1;
- return NL_STOP;
-}
-
-
-struct phy_info_arg {
- u16 *num_modes;
- struct wpa_hw_modes *modes;
- int error;
-};
-
-
-static int phy_info_handler(struct nl_msg *msg, void *arg)
-{
- struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
- struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
- struct phy_info_arg *phy_info = arg;
-
- struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
-
- struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
- static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1]
- = {
- [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
- [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
- [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
- [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
- [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
- };
-
- struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
- static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
- [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
- [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
- { .type = NLA_FLAG },
- };
-
- struct nlattr *nl_band;
- struct nlattr *nl_freq;
- struct nlattr *nl_rate;
- int rem_band, rem_freq, rem_rate;
- struct wpa_hw_modes *mode;
- int idx, mode_is_set;
-
- nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
- genlmsg_attrlen(gnlh, 0), NULL);
-
- if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
- return NL_SKIP;
-
- nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS],
- rem_band) {
- mode = os_realloc(phy_info->modes,
- (*phy_info->num_modes + 1) * sizeof(*mode));
- if (!mode)
- return NL_SKIP;
- phy_info->modes = mode;
-
- mode_is_set = 0;
-
- mode = &phy_info->modes[*(phy_info->num_modes)];
- os_memset(mode, 0, sizeof(*mode));
- *(phy_info->num_modes) += 1;
-
- nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
- nla_len(nl_band), NULL);
-
- nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS],
- rem_freq) {
- nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
- nla_data(nl_freq), nla_len(nl_freq),
- freq_policy);
- if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
- continue;
- mode->num_channels++;
- }
-
- mode->channels = os_zalloc(mode->num_channels *
- sizeof(struct wpa_channel_data));
- if (!mode->channels)
- return NL_SKIP;
-
- idx = 0;
-
- nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS],
- rem_freq) {
- nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
- nla_data(nl_freq), nla_len(nl_freq),
- freq_policy);
- if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
- continue;
-
- mode->channels[idx].freq = nla_get_u32(
- tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
- mode->channels[idx].flag |= WPA_CHAN_W_SCAN |
- WPA_CHAN_W_ACTIVE_SCAN |
- WPA_CHAN_W_IBSS;
-
- if (!mode_is_set) {
- /* crude heuristic */
- if (mode->channels[idx].freq < 4000)
- mode->mode = WPA_MODE_IEEE80211B;
- else
- mode->mode = WPA_MODE_IEEE80211A;
- mode_is_set = 1;
- }
-
- /* crude heuristic */
- if (mode->channels[idx].freq < 4000) {
- if (mode->channels[idx].freq == 2848)
- mode->channels[idx].chan = 14;
- else
- mode->channels[idx].chan =
- (mode->channels[idx].freq -
- 2407) / 5;
- } else
- mode->channels[idx].chan =
- mode->channels[idx].freq / 5 - 1000;
-
- if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
- mode->channels[idx].flag &= ~WPA_CHAN_W_SCAN;
- if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
- mode->channels[idx].flag &=
- ~WPA_CHAN_W_ACTIVE_SCAN;
- if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
- mode->channels[idx].flag &= ~WPA_CHAN_W_IBSS;
- idx++;
- }
-
- nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES],
- rem_rate) {
- nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
- nla_data(nl_rate), nla_len(nl_rate),
- rate_policy);
- if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
- continue;
- mode->num_rates++;
- }
-
- mode->rates = os_zalloc(mode->num_rates *
- sizeof(struct wpa_rate_data));
- if (!mode->rates)
- return NL_SKIP;
-
- idx = 0;
-
- nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES],
- rem_rate) {
- nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
- nla_data(nl_rate), nla_len(nl_rate),
- rate_policy);
- if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
- continue;
- mode->rates[idx].rate = nla_get_u32(
- tb_rate[NL80211_BITRATE_ATTR_RATE]);
-
- /* crude heuristic */
- if (mode->mode == WPA_MODE_IEEE80211B &&
- mode->rates[idx].rate > 200)
- mode->mode = WPA_MODE_IEEE80211G;
-
- if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
- mode->rates[idx].flags |= WPA_RATE_PREAMBLE2;
-
- idx++;
- }
- }
-
- phy_info->error = 0;
-
- return NL_SKIP;
-}
-
-
-static struct wpa_hw_modes *
-wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
-{
- struct wpa_driver_nl80211_data *drv = priv;
- struct nl_msg *msg;
- int err = -1;
- struct nl_cb *cb = NULL;
- int finished = 0;
- struct phy_info_arg result = {
- .num_modes = num_modes,
- .modes = NULL,
- .error = 1,
- };
-
- *num_modes = 0;
- *flags = 0;
-
- msg = nlmsg_alloc();
- if (!msg)
- return NULL;
-
- genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
- 0, NL80211_CMD_GET_WIPHY, 0);
-
- NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
-
- cb = nl_cb_clone(drv->nl_cb);
- if (!cb)
- goto out;
-
- if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
- goto out;
-
- nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
- nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
-
- err = nl_recvmsgs(drv->nl_handle, cb);
-
- if (!finished)
- err = nl_wait_for_ack(drv->nl_handle);
-
- if (err < 0 || result.error) {
- wpa_supplicant_sta_free_hw_features(result.modes, *num_modes);
- result.modes = NULL;
- }
-
- out:
- nl_cb_put(cb);
- nla_put_failure:
- if (err)
- fprintf(stderr, "failed to get information: %d\n", err);
- nlmsg_free(msg);
- return result.modes;
-}
-
-
-static int wpa_driver_nl80211_set_channel(void *priv, wpa_hw_mode phymode,
- int chan, int freq)
-{
- return wpa_driver_nl80211_set_freq(priv, freq);
-}
-
-
-static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
- size_t data_len)
-{
- struct wpa_driver_nl80211_data *drv = priv;
- __u8 rtap_hdr[] = {
- 0x00, 0x00, /* radiotap version */
- 0x0e, 0x00, /* radiotap length */
- 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
- 0x0c, /* F_WEP | F_FRAG (encrypt/fragment if required) */
- 0x00, /* padding */
- 0x00, 0x00, /* RX and TX flags to indicate that */
- 0x00, 0x00, /* this is the injected frame directly */
- };
- struct iovec iov[2] = {
- {
- .iov_base = &rtap_hdr,
- .iov_len = sizeof(rtap_hdr),
- },
- {
- .iov_base = (void *) data,
- .iov_len = data_len,
- }
- };
- struct msghdr msg = {
- .msg_name = NULL,
- .msg_namelen = 0,
- .msg_iov = iov,
- .msg_iovlen = 2,
- .msg_control = NULL,
- .msg_controllen = 0,
- .msg_flags = 0,
- };
-
- if (sendmsg(drv->monitor_sock, &msg, 0) < 0) {
- perror("send[MLME]");
- return -1;
- }
-
- return 0;
-}
-
-
-static int wpa_driver_nl80211_mlme_add_sta(void *priv, const u8 *addr,
- const u8 *supp_rates,
- size_t supp_rates_len)
-{
- struct wpa_driver_nl80211_data *drv = priv;
- struct nl_msg *msg;
- int ret = -1;
-
- msg = nlmsg_alloc();
- if (!msg)
- goto out;
-
- genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
- 0, NL80211_CMD_NEW_STATION, 0);
-
- NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
- NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
- /* TODO: Get proper Association ID and listen interval */
- NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
- NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, supp_rates_len,
- supp_rates);
- NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, 1);
-
- ret = nl_send_auto_complete(drv->nl_handle, msg);
- if (ret < 0)
- goto nla_put_failure;
-
- ret = nl_wait_for_ack(drv->nl_handle);
- /* ignore EEXIST, this happens if a STA associates while associated */
- if (ret == -EEXIST || ret >= 0)
- ret = 0;
-
- nla_put_failure:
- nlmsg_free(msg);
-
- out:
- return ret;
-}
-
-
-static int wpa_driver_nl80211_mlme_remove_sta(void *priv, const u8 *addr)
-{
- struct wpa_driver_nl80211_data *drv = priv;
- struct nl_msg *msg;
- int ret = -1;
-
- msg = nlmsg_alloc();
- if (!msg)
- goto out;
-
- genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
- 0, NL80211_CMD_DEL_STATION, 0);
-
- NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
- NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
-
- ret = 0;
-
- if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
- nl_wait_for_ack(drv->nl_handle) < 0) {
- ret = -1;
- }
-
- nla_put_failure:
- nlmsg_free(msg);
-
- out:
- return ret;
-}
-
-#endif /* CONFIG_CLIENT_MLME */
-
-