[PATCH 09/20] wpa_s: fix wpa_supplicant_init_iface() for P2P Device interface

Arend van Spriel arend
Thu May 16 06:28:24 PDT 2013


Couple of issues upon initializing a P2P Device interface needed
to be solved.

Signed-hostap: Arend van Spriel <arend at broadcom.com>
---
 src/drivers/driver_nl80211.c    |  131 ++++++++++++++++++++++++---------------
 wpa_supplicant/wpa_supplicant.c |    3 +-
 2 files changed, 84 insertions(+), 50 deletions(-)

diff --git a/src/drivers/driver_nl80211.c b/src/drivers/driver_nl80211.c
index 92b8882..9d8d630 100644
--- a/src/drivers/driver_nl80211.c
+++ b/src/drivers/driver_nl80211.c
@@ -157,6 +157,7 @@ static void nl_destroy_handles(struct nl_handle **handle)
 struct nl80211_global {
 	struct dl_list interfaces;
 	int if_add_ifindex;
+	int64_t if_add_wdevid;
 	struct netlink_data *netlink;
 	struct nl_cb *nl_cb;
 	struct nl_handle *nl;
@@ -287,6 +288,11 @@ struct wpa_driver_nl80211_data {
 	int auth_p2p;
 };
 
+#define NL80211_PUT_IFACE_ID(m, b)				\
+	do {						\
+		if (nl80211_set_iface_id((m), (b)) < 0) \
+			goto nla_put_failure;		\
+	} while(0)
 
 static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
@@ -603,7 +609,7 @@ static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
 
 	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
 
-	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
+	NL80211_PUT_IFACE_ID(msg, bss);
 
 	if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
 		return data.nlmode;
@@ -3003,7 +3009,7 @@ static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
 		nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
 
 	NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
-	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
+	NL80211_PUT_IFACE_ID(msg, &drv->first_bss);
 
 	if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
 		return -1;
@@ -3406,7 +3412,6 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
 	bss = &drv->first_bss;
 	bss->drv = drv;
 	bss->ctx = ctx;
-	bss->wdev_id = -1;
 
 	os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
 	drv->monitor_ifidx = -1;
@@ -3494,9 +3499,7 @@ static int nl80211_register_frame(struct i802_bss *bss,
 
 	nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
 
-	if (nl80211_set_iface_id(msg, bss))
-		goto nla_put_failure;
-
+	NL80211_PUT_IFACE_ID(msg, bss);
 	NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
 	NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
 
@@ -3732,29 +3735,56 @@ static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
 	wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
 }
 
+static int nl80211_start_p2pdev(struct i802_bss *bss)
+{
+	struct wpa_driver_nl80211_data *drv = bss->drv;
+	struct nl_msg *msg;
+	int ret = -1;
+
+	wpa_printf(MSG_DEBUG, "nl80211: start p2p device %s (%" PRIx64 ")",
+		   bss->ifname, bss->wdev_id);
+
+	msg = nlmsg_alloc();
+	if (!msg)
+		return -1;
+
+	nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
+	NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
+
+	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+	msg = NULL;
+
+nla_put_failure:
+	nlmsg_free(msg);
+	return ret;
+}
 
 static int
 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
 {
+	enum nl80211_iftype nlmode = NL80211_IFTYPE_UNSPECIFIED;
 	struct i802_bss *bss = &drv->first_bss;
 	int send_rfkill_event = 0;
+	int dynamic_if;
+
 
 	drv->ifindex = if_nametoindex(bss->ifname);
-	drv->first_bss.ifindex = drv->ifindex;
+	bss->ifindex = drv->ifindex;
+	bss->wdev_id = drv->global->if_add_wdevid;
+
+	dynamic_if = drv->ifindex == drv->global->if_add_ifindex;
+	dynamic_if = dynamic_if || drv->global->if_add_wdevid != -1;
+	drv->global->if_add_wdevid = -1;
 
 #ifndef HOSTAPD
+	nlmode = nl80211_get_ifmode(bss);
+
 	/*
 	 * Make sure the interface starts up in station mode unless this is a
 	 * dynamically added interface (e.g., P2P) that was already configured
 	 * with proper iftype.
 	 */
-	if (drv->ifindex != drv->global->if_add_ifindex) {
-		enum nl80211_iftype nlmode;
-
-		nlmode = nl80211_get_ifmode(bss);
-		if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
-			nlmode = NL80211_IFTYPE_STATION;
-
+	if (!dynamic_if) {
 		if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
 			wpa_printf(MSG_ERROR, "nl80211: Could not configure "
 				   "driver to use %s mode",
@@ -3767,6 +3797,16 @@ wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
 		drv->nlmode = NL80211_IFTYPE_STATION;
 	}
 
+	if (wpa_driver_nl80211_capa(drv))
+		return -1;
+
+	if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
+		int ret = nl80211_start_p2pdev(bss);
+		if (ret < 0)
+			wpa_printf(MSG_ERROR, "nl80211: could not start P2P device");
+		return ret;
+	}
+
 	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
 		if (rfkill_is_blocked(drv->rfkill)) {
 			wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
@@ -3785,9 +3825,6 @@ wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
 			       1, IF_OPER_DORMANT);
 #endif /* HOSTAPD */
 
-	if (wpa_driver_nl80211_capa(drv))
-		return -1;
-
 	if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
 			       bss->addr))
 		return -1;
@@ -6421,12 +6458,8 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
 		return ret;
 	}
 
-	if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
-		wpa_printf(MSG_DEBUG,
-			   "nl80211 New P2P device interface %s created",
-			   ifname);
+	if (iftype == NL80211_IFTYPE_P2P_DEVICE)
 		return 0;
-	}
 
 	ifidx = if_nametoindex(ifname);
 	wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
@@ -7668,7 +7701,7 @@ static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
 		return -ENOMEM;
 
 	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
-	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
+	NL80211_PUT_IFACE_ID(msg, &drv->first_bss);
 	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
 
 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
@@ -8589,20 +8622,23 @@ static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
 
 #endif /* CONFIG_P2P */
 
-static int nl80211_create_p2p_dev_handler(struct nl_msg *msg, void *arg)
+struct wdev_info {
+	int64_t wdev_id;
+	u8 macaddr[ETH_ALEN];
+};
+
+static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
 {
 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
-	struct i802_bss *p2p_dev = arg;
+	struct wdev_info *wi = arg;
 
 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
 			genlmsg_attrlen(gnlh, 0), NULL);
 	if (tb[NL80211_ATTR_WDEV])
-		p2p_dev->wdev_id =
-			nla_get_u64(tb[NL80211_ATTR_WDEV]);
-
+		wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
 	if (tb[NL80211_ATTR_MAC])
-		os_memcpy(p2p_dev->addr,
+		os_memcpy(wi->macaddr,
 			  nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
 
 	return NL_SKIP;
@@ -8616,7 +8652,6 @@ static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
 {
 	enum nl80211_iftype nlmode;
 	struct i802_bss *bss = priv;
-	struct i802_bss *p2pdev;
 	struct wpa_driver_nl80211_data *drv = bss->drv;
 	int ifidx;
 #ifdef HOSTAPD
@@ -8633,21 +8668,24 @@ static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
 		os_memcpy(if_addr, addr, ETH_ALEN);
 	nlmode = wpa_driver_nl80211_if_type(type);
 	if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
-		p2pdev = os_zalloc(sizeof(*p2pdev));
-		if (!p2pdev)
-			return -1;
-		*p2pdev = *bss;
-		os_strncpy(p2pdev->ifname, ifname, IFNAMSIZ);
+		struct wdev_info p2pdev_info;
+
+		os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
 		ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
-					     0, nl80211_create_p2p_dev_handler,
-					     p2pdev);
-		if (p2pdev->wdev_id == -1 || ifidx != 0 ||
-		    is_zero_ether_addr((const u8 *)&p2pdev->addr)) {
+					     0, nl80211_wdev_handler,
+					     &p2pdev_info);
+		if (p2pdev_info.wdev_id == -1 || ifidx != 0) {
 			wpa_printf(MSG_ERROR, "nl80211: Failed to create a p2p device"
 					" interface %s", ifname);
-			os_free(p2pdev);
 			return -1;
 		}
+
+		drv->global->if_add_wdevid = p2pdev_info.wdev_id;
+		if (!is_zero_ether_addr(p2pdev_info.macaddr))
+			os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
+		wpa_printf(MSG_DEBUG,
+			   "nl80211: New P2P device interface %s (%" PRIx64 ") created",
+			   ifname, p2pdev_info.wdev_id);
 	} else {
 		ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
 					     0, NULL, NULL);
@@ -8829,9 +8867,7 @@ static int nl80211_send_frame_cmd(struct i802_bss *bss,
 		   freq, wait, no_cck, no_ack, offchanok);
 	nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
 
-	if (nl80211_set_iface_id(msg, bss))
-		goto nla_put_failure;
-
+	NL80211_PUT_IFACE_ID(msg, bss);
 	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
 	if (wait)
 		NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
@@ -8953,9 +8989,7 @@ static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
 
 	nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
 
-	if (nl80211_set_iface_id(msg, bss))
-		goto nla_put_failure;
-
+	NL80211_PUT_IFACE_ID(msg, bss);
 	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
 	NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
 
@@ -9002,9 +9036,7 @@ static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
 
 	nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
 
-	if (nl80211_set_iface_id(msg, bss))
-		goto nla_put_failure;
-
+	NL80211_PUT_IFACE_ID(msg, bss);
 	NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
 
 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
@@ -9348,6 +9380,7 @@ static void * nl80211_global_init(void)
 	global->ioctl_sock = -1;
 	dl_list_init(&global->interfaces);
 	global->if_add_ifindex = -1;
+	global->if_add_wdevid = -1;
 
 	cfg = os_zalloc(sizeof(*cfg));
 	if (cfg == NULL)
diff --git a/wpa_supplicant/wpa_supplicant.c b/wpa_supplicant/wpa_supplicant.c
index 7c3b180..525e757 100644
--- a/wpa_supplicant/wpa_supplicant.c
+++ b/wpa_supplicant/wpa_supplicant.c
@@ -2323,7 +2323,8 @@ int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
 		if (addr)
 			os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
 	} else if (!(wpa_s->drv_flags &
-		     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
+		     (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
+		      WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))) {
 		l2_packet_deinit(wpa_s->l2);
 		wpa_s->l2 = l2_packet_init(wpa_s->ifname,
 					   wpa_drv_get_mac_addr(wpa_s),
-- 
1.7.10.4





More information about the Hostap mailing list