netconfig: Move EnableNetworkConfiguration check to station
PATCH 1/4 netconfig: Move EnableNetworkConfiguration check to station
Allow p2p to use netconfig even if not enabled for Infrastructure mode connections.
--- src/netconfig.c | 17 ----------------- src/station.c | 19 ++++++++++++++++++- 2 files changed, 18 insertions(+), 18 deletions(-)
src/netconfig.c +0 -17
@@ -1193,28 +1193,11 @@ void netconfig_destroy(struct netconfig *netconfig)static int netconfig_init (void ) { -bool enabled;uint32_t r;if (netconfig_list)return -EALREADY ; -if (!l_settings_get_bool (iwd_get_config (),"General" , -"EnableNetworkConfiguration" , - &enabled)) { -if (l_settings_get_bool (iwd_get_config (),"General" , -"enable_network_config" , &enabled)) -l_warn ("[General].enable_network_config is deprecated," -" use [General].EnableNetworkConfiguration" ); -else - enabled = false; - } - -if (!enabled) { -l_info ("netconfig: Network configuration is disabled." ); -return 0 ; - } - rtnl =iwd_get_rtnl (); r = l_netlink_register(rtnl, RTNLGRP_IPV4_IFADDR,
src/station.c +18 -1
@@ -58,6 +58,7 @@ static struct l_queue *station_list;static uint32_t netdev_watch;static uint32_t mfp_setting;static bool anqp_disabled; +static bool netconfig_enabled;struct station {enum station_state state;
@@ -3098,7 +3099,8 @@ static struct station *station_create(struct netdev *netdev)l_dbus_object_add_interface (dbus,netdev_get_path (netdev),IWD_STATION_INTERFACE , station); - station->netconfig =netconfig_new (netdev_get_ifindex (netdev)); +if (netconfig_enabled) + station->netconfig =netconfig_new (netdev_get_ifindex (netdev)); station->anqp_pending =l_queue_new ();
@@ -3250,6 +3252,21 @@ static int station_init(void) &anqp_disabled)) anqp_disabled = true; +if (!l_settings_get_bool (iwd_get_config (),"General" , +"EnableNetworkConfiguration" , + &netconfig_enabled)) { +if (l_settings_get_bool (iwd_get_config (),"General" , +"enable_network_config" , + &netconfig_enabled)) +l_warn ("[General].enable_network_config is deprecated," +" use [General].EnableNetworkConfiguration" ); +else + netconfig_enabled = false; + } + +if (!netconfig_enabled) +l_info ("station: Network configuration is disabled." ); +return 0 ; }
Signature · 1 line
2.25.1
PATCH 2/4 p2p: Do DHCP as part of connection setup
Use netconfig.c functions to unconditionally run DHCP negotiation, fail the connection setup if DHCP fails. Only report connection success after netconfig returns.
--- src/p2p.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 7 deletions(-)
src/p2p.c +82 -7
@@ -53,6 +53,7 @@#include "src/module.h" #include "src/frame-xchg.h" #include "src/nl80211util.h" +#include "src/netconfig.h" #include "src/p2p.h" struct p2p_device {
@@ -92,6 +93,8 @@ struct p2p_device {uint32_t conn_netdev_watch_id;uint32_t conn_new_intf_cmd_id;struct wsc_enrollee *conn_enrollee; +struct netconfig *conn_netconfig; +struct l_timeout *conn_dhcp_timeout;struct l_timeout *config_timeout;unsigned long go_config_delay;
@@ -132,6 +135,7 @@ struct p2p_peer { };static struct l_queue *p2p_device_list; +static struct l_settings *p2p_dhcp_settings;/* * For now we only scan the common 2.4GHz channels, to be replaced with
@@ -185,7 +189,8 @@ static void p2p_discovery_user_free(void *data)static inline bool p2p_peer_operational (struct p2p_peer *peer) {return peer && peer->dev->conn_netdev && !peer->dev->conn_wsc_bss && - !peer->wsc.pending_connect && !peer->dev->disconnecting; + !peer->dev->conn_dhcp_timeout && !peer->wsc.pending_connect && + !peer->dev->disconnecting; }static bool p2p_peer_match (const void *a,const void *b)
@@ -325,6 +330,12 @@ static void p2p_connection_reset(struct p2p_device *dev)l_timeout_remove (dev->config_timeout);l_timeout_remove (dev->go_neg_req_timeout); +l_timeout_remove (dev->conn_dhcp_timeout); + +if (dev->conn_netconfig) { +netconfig_destroy (dev->conn_netconfig); + dev->conn_netconfig =NULL ; + }if (dev->conn_new_intf_cmd_id)/*
@@ -490,6 +501,71 @@ static const struct frame_xchg_prefix p2p_frame_pd_resp = { .len = 7, }; +static void p2p_netconfig_event_handler (enum netconfig_event event, +void *user_data) +{ +struct p2p_device *dev = user_data; +struct p2p_peer *peer = dev->conn_peer; + +switch (event) { +case NETCONFIG_EVENT_CONNECTED : +l_timeout_remove (dev->conn_dhcp_timeout); + +dbus_pending_reply (&peer->wsc.pending_connect, +l_dbus_message_new_method_return ( + peer->wsc.pending_connect)); +l_dbus_property_changed (dbus_get_bus (), +p2p_peer_get_path (dev->conn_peer), +IWD_P2P_PEER_INTERFACE ,"Connected" ); + +break ; +default : +l_error ("station: Unsupported netconfig event: %d." , event); +p2p_connect_failed (dev); +break ; + } +} + +static void p2p_dhcp_timeout (struct l_timeout *timeout,void *user_data) +{ +struct p2p_device *dev = user_data; + +l_debug ("" ); + +p2p_connect_failed (dev); +} + +static void p2p_dhcp_timeout_destroy (void *user_data) +{ +struct p2p_device *dev = user_data; + + dev->conn_dhcp_timeout =NULL ; +} + +static void p2p_start_dhcp (struct p2p_device *dev) +{ +uint32_t ifindex =netdev_get_ifindex (dev->conn_netdev); +unsigned int dhcp_timeout_val; + +if (!l_settings_get_uint (iwd_get_config (),"P2P" ,"DHCPTimeout" , + &dhcp_timeout_val)) + dhcp_timeout_val =10 ;/* 10s default */ + + dev->conn_netconfig =netconfig_new (ifindex); +if (!dev->conn_netconfig) { +p2p_connect_failed (dev); +return ; + } + +netconfig_configure (dev->conn_netconfig, p2p_dhcp_settings, + dev->conn_addr, p2p_netconfig_event_handler, + dev); + dev->conn_dhcp_timeout =l_timeout_create (dhcp_timeout_val, + p2p_dhcp_timeout, dev, + p2p_dhcp_timeout_destroy); +return ; +} +static void p2p_netdev_connect_cb (struct netdev *netdev,enum netdev_result result,void *event_data,void *user_data)
@@ -506,12 +582,7 @@ static void p2p_netdev_connect_cb(struct netdev *netdev,switch (result) {case NETDEV_RESULT_OK : -dbus_pending_reply (&peer->wsc.pending_connect, -l_dbus_message_new_method_return ( - peer->wsc.pending_connect)); -l_dbus_property_changed (dbus_get_bus (), -p2p_peer_get_path (dev->conn_peer), -IWD_P2P_PEER_INTERFACE ,"Connected" ); +p2p_start_dhcp (dev);break ;case NETDEV_RESULT_AUTHENTICATION_FAILED :case NETDEV_RESULT_ASSOCIATION_FAILED :
@@ -3485,6 +3556,7 @@ static int p2p_init(void)l_error ("Unable to register the %s interface" ,IWD_P2P_PEER_INTERFACE ); + p2p_dhcp_settings =l_settings_new (); p2p_device_list =l_queue_new ();return 0 ;
@@ -3496,8 +3568,11 @@ static void p2p_exit(void)l_dbus_unregister_interface (dbus_get_bus (),IWD_P2P_PEER_INTERFACE );l_queue_destroy (p2p_device_list, p2p_device_free); p2p_device_list =NULL ; +l_settings_free (p2p_dhcp_settings); + p2p_dhcp_settings =NULL ; } IWD_MODULE(p2p, p2p_init, p2p_exit) IWD_MODULE_DEPENDS(p2p, wiphy) IWD_MODULE_DEPENDS(p2p, scan) +IWD_MODULE_DEPENDS (p2p, netconfig)
Signature · 1 line
2.25.1
Re: [PATCH 2/4] p2p: Do DHCP as part of connection setup
Hi Andrew,
Quoted 7 lines · Andrew Zaborowski
Use
netconfig.cfunctions to unconditionally run DHCP negotiation, fail the connection setup if DHCP fails. Only report connection success after netconfig returns.--- src/p2p.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 7 deletions(-)
Patch 1 applied, I ..
Quoted 12 lines
src/p2p.c +12
-0+static void p2p_start_dhcp (struct p2p_device *dev) +{ +uint32_t ifindex =netdev_get_ifindex (dev->conn_netdev); +unsigned int dhcp_timeout_val; + +if (!l_settings_get_uint (iwd_get_config (),"P2P" ,"DHCPTimeout" , + &dhcp_timeout_val)) + dhcp_timeout_val =10 ;/* 10s default */ + + dev->conn_netconfig =netconfig_new (ifindex); +if (!dev->conn_netconfig) { +p2p_connect_failed (dev);
src/p2p.c static const struct frame_xchg_prefix p2p_frame_pd_resp = { · line 566
Quoted 10 lines
src/p2p.c +10
-0+return ; + } + +netconfig_configure (dev->conn_netconfig, p2p_dhcp_settings, + dev->conn_addr, p2p_netconfig_event_handler, + dev); + dev->conn_dhcp_timeout =l_timeout_create (dhcp_timeout_val, + p2p_dhcp_timeout, dev, + p2p_dhcp_timeout_destroy); +return ;
squashed this superfluous return and applied this as well.
src/p2p.c static const struct frame_xchg_prefix p2p_frame_pd_resp = { · line 571
Quoted 5 lines
src/p2p.c +2
-0+} +static void p2p_netdev_connect_cb (struct netdev *netdev,enum netdev_result result,void *event_data,void *user_data)
Regards,
-Denis
PATCH 3/4 watchlist: Save the watchlist pointer in WATCHLIST_NOTIFY_*
In reply to Andrew ZaborowskiSave the value of the watchlist pointer at the beginning of the WATCHLIST_NOTIFY_* macros as if it was a function. This will fix a frame-xchg.c scenario in which one of the watch callback removes the frame watch group and the memory where the watchlist pointer was becomes unallocated but the macro still needs to access it ones or twice while it destroys the watchlist. Another option would be for the pointer to be copied in frame-xchg.c itself.
--- src/watchlist.h | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-)
src/watchlist.h +24 -21
@@ -58,34 +58,36 @@ void watchlist_free(struct watchlist *watchlist);void __watchlist_prune_stale (struct watchlist *watchlist); -#define WATCHLIST_NOTIFY(watchlist, type, args...) \ +#define WATCHLIST_NOTIFY(list, type, args...) \ do { \ + struct watchlist *watchlist = (list); \ const struct l_queue_entry *entry = \ - l_queue_get_entries((watchlist)->items);\ + l_queue_get_entries(watchlist->items); \ \ - (watchlist)->in_notify = true; \ + watchlist->in_notify = true; \ for (; entry; entry = entry->next) { \ struct watchlist_item *item = entry->data; \ type t = item->notify; \ if (item->id == 0) \ continue; \ t(args, item->notify_data); \ - if ((watchlist)->pending_destroy) \ + if (watchlist->pending_destroy) \ break; \ } \ - (watchlist)->in_notify = false; \ - if ((watchlist)->pending_destroy) \ + watchlist->in_notify = false; \ + if (watchlist->pending_destroy) \ watchlist_destroy(watchlist); \ - else if ((watchlist)->stale_items) \ + else if (watchlist->stale_items) \ __watchlist_prune_stale(watchlist); \ } while (false) \ -#define WATCHLIST_NOTIFY_MATCHES(watchlist, match, match_data, type, args...) \ +#define WATCHLIST_NOTIFY_MATCHES(list, match, match_data, type, args...) \ do { \ + struct watchlist *watchlist = (list); \ const struct l_queue_entry *entry = \ - l_queue_get_entries((watchlist)->items);\ + l_queue_get_entries(watchlist->items); \ \ - (watchlist)->in_notify = true; \ + watchlist->in_notify = true; \ for (; entry; entry = entry->next) { \ struct watchlist_item *item = entry->data; \ type t = item->notify; \
@@ -97,34 +99,35 @@ void __watchlist_prune_stale(struct watchlist *watchlist); \t (args, item->notify_data); \ \ -if ((watchlist)->pending_destroy) \ +if (watchlist->pending_destroy) \break ; \ } \ - (watchlist)->in_notify = false; \ -if ((watchlist)->pending_destroy) \ + watchlist->in_notify = false; \ +if (watchlist->pending_destroy) \watchlist_destroy (watchlist); \ -else if ((watchlist)->stale_items) \ +else if (watchlist->stale_items) \__watchlist_prune_stale (watchlist); \ } while (false) -#define WATCHLIST_NOTIFY_NO_ARGS (watchlist, type) \ +#define WATCHLIST_NOTIFY_NO_ARGS (list, type) \ do { \ + struct watchlist *watchlist = (list); \ const struct l_queue_entry *entry = \ - l_queue_get_entries((watchlist)->items);\ + l_queue_get_entries(watchlist->items); \ \ - (watchlist)->in_notify = true; \ + watchlist->in_notify = true; \ for (; entry; entry = entry->next) { \ struct watchlist_item *item = entry->data; \ type t = item->notify; \ if (item->id == 0) \ continue; \ t(item->notify_data); \ - if ((watchlist)->pending_destroy) \ + if (watchlist->pending_destroy) \ break; \ } \ - (watchlist)->in_notify = false; \ - if ((watchlist)->pending_destroy) \ + watchlist->in_notify = false; \ + if (watchlist->pending_destroy) \ watchlist_destroy(watchlist); \ - else if ((watchlist)->stale_items) \ + else if (watchlist->stale_items) \ __watchlist_prune_stale(watchlist); \ } while (false)
Signature · 1 line
2.25.1
Re: [PATCH 3/4] watchlist: Save the watchlist pointer in WATCHLIST_NOTIFY_*
Hi Andrew,
Quoted 11 lines · Andrew Zaborowski
Save the value of the watchlist pointer at the beginning of the WATCHLIST_NOTIFY_* macros as if it was a function. This will fix a
frame-xchg.cscenario in which one of the watch callback removes the frame watch group and the memory where the watchlist pointer was becomes unallocated but the macro still needs to access it ones or twice while it destroys the watchlist. Another option would be for the pointer to be copied inframe-xchg.citself.--- src/watchlist.h | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-)
Patch 3 & 4 applied, thanks.
Regards,
-Denis
PATCH 4/4 netdev: Handle P2P-client iftype in netdev_setting_keys_failed
In reply to Andrew Zaborowski--- src/netdev.c | 1 + 1 file changed, 1 insertion(+)
src/netdev.c +1 -0
@@ -1054,6 +1054,7 @@ static void netdev_setting_keys_failed(struct netdev_handshake_state *nhs,switch (netdev->type) {case NL80211_IFTYPE_STATION : +case NL80211_IFTYPE_P2P_CLIENT : msg =netdev_build_cmd_disconnect (netdev,MMPDU_REASON_CODE_UNSPECIFIED ); netdev->disconnect_cmd_id = l_genl_family_send(nl80211, msg,
Signature · 1 line
2.25.1
PATCH 10/11 p2p: WSC client provisioning and connection
In reply to Andrew ZaborowskiAdd the final two steps of the connection setup, and corresponding disconnect logic:
* the WSC connection to the GO to do the client provisioning,
* the netdev_connect call to use the provisioned credentials for the final WPA2 connection.
--- src/p2p.c | 319 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 316 insertions(+), 3 deletions(-)
src/p2p.c +316 -3
@@ -91,6 +91,7 @@ struct p2p_device {struct netdev *conn_netdev;uint32_t conn_netdev_watch_id;uint32_t conn_new_intf_cmd_id; +struct wsc_enrollee *conn_enrollee;struct l_timeout *config_timeout;unsigned long go_config_delay;
@@ -103,6 +104,14 @@ struct p2p_device { bool enabled : 1; bool have_roc_cookie : 1; +/* +* We need to track @disconnecting because while a connect action is +* always triggered by a DBus message, meaning that @pending_message +* is going to be non-NULL, a disconnect may also be a result of an +* error at a layer higher than netdev and may last until +* netdev_disconnect, or similar, finishes. +*/ + bool disconnecting : 1; };struct p2p_discovery_user {
@@ -173,6 +182,12 @@ static void p2p_discovery_user_free(void *data)l_free (user); } +static inline bool p2p_peer_operational (struct p2p_peer *peer) +{ +return peer && peer->dev->conn_netdev && !peer->dev->conn_wsc_bss && + !peer->wsc.pending_connect && !peer->dev->disconnecting; +} +static bool p2p_peer_match (const void *a,const void *b) {const struct p2p_peer *peer = a;
@@ -296,6 +311,7 @@ static void p2p_connection_reset(struct p2p_device *dev)* age will be checked on the next scan. */ dev->conn_peer =NULL ; + dev->disconnecting = false; dev->connections_left++;if (dev->conn_pin) {
@@ -320,6 +336,9 @@ static void p2p_connection_reset(struct p2p_device *dev)*/ l_genl_family_cancel (dev->nl80211, dev->conn_new_intf_cmd_id); +if (dev->conn_enrollee) +wsc_enrollee_cancel (dev->conn_enrollee, false); +if (dev->conn_netdev) {struct l_genl_msg *msg;uint64_t wdev_id =netdev_get_wdev_id (dev->conn_netdev);
@@ -471,9 +490,254 @@ static const struct frame_xchg_prefix p2p_frame_pd_resp = { .len = 7, }; +static void p2p_netdev_connect_cb (struct netdev *netdev, +enum netdev_result result, +void *event_data,void *user_data) +{ +struct p2p_device *dev = user_data; +struct p2p_peer *peer = dev->conn_peer; + +l_debug ("result: %i" , result); + +if (!peer->wsc.pending_connect || dev->disconnecting) { +/* Shouldn't happen except maybe in the ABORTED case */ +return ; + } + +switch (result) { +case NETDEV_RESULT_OK : +dbus_pending_reply (&peer->wsc.pending_connect, +l_dbus_message_new_method_return ( + peer->wsc.pending_connect)); +l_dbus_property_changed (dbus_get_bus (), +p2p_peer_get_path (dev->conn_peer), +IWD_P2P_PEER_INTERFACE ,"Connected" ); +break ; +case NETDEV_RESULT_AUTHENTICATION_FAILED : +case NETDEV_RESULT_ASSOCIATION_FAILED : +case NETDEV_RESULT_HANDSHAKE_FAILED : +case NETDEV_RESULT_KEY_SETTING_FAILED : +/* +* In the AUTHENTICATION_FAILED and ASSOCIATION_FAILED +* cases there's nothing to disconnect. In the +* HANDSHAKE_FAILED and KEY_SETTINGS failed cases +* netdev disconnects from the GO automatically and we are +* called already from within the disconnect callback, +* so we can directly free the netdev. +*/ +p2p_connect_failed (dev); +break ; +case NETDEV_RESULT_ABORTED : +/* +* This case can only be triggered by netdev_disconnect so +* we'll wait for its callback before freeing the netdev. +* We will also have already replied to +* @peer->wsc.pending_connect so we have nothing to do here. +*/ +break ; + } +} + +static void p2p_netdev_event (struct netdev *netdev,enum netdev_event event, +void *event_data,void *user_data) +{ +struct p2p_device *dev = user_data; + +switch (event) { +case NETDEV_EVENT_DISCONNECT_BY_AP : +case NETDEV_EVENT_DISCONNECT_BY_SME : +/* +* We may get a DISCONNECT_BY_SME as a result of a +* netdev_disconnect(). In that case let the callback handle +* that. +*/ +if (dev->disconnecting) +break ; + +/* If we're not connected, .Connected is already False */ +if (!p2p_peer_operational (dev->conn_peer)) { +p2p_connect_failed (dev); +break ; + } + +l_dbus_property_changed (dbus_get_bus (), +p2p_peer_get_path (dev->conn_peer), +IWD_P2P_PEER_INTERFACE ,"Connected" ); +p2p_connection_reset (dev); +break ; +default : +break ; + }; +} + +static void p2p_handshake_event (struct handshake_state *hs, +enum handshake_event event,void *user_data, + ...) +{ +va_list args; + +va_start (args, user_data); + +switch (event) { +case HANDSHAKE_EVENT_FAILED : +netdev_handshake_failed (hs,va_arg (args, int)); +break ; +default : +break ; + } + +va_end (args); +} + +static void p2p_peer_provision_done (int err,struct wsc_credentials_info *creds, +unsigned int n_creds,void *user_data) +{ +struct p2p_peer *peer = user_data; +struct p2p_device *dev = peer->dev; +struct scan_bss *bss = dev->conn_wsc_bss; +struct handshake_state *hs =NULL ; +struct iovec ie_iov = {}; +int r = -EOPNOTSUPP ; +struct p2p_association_req info = {}; +struct ie_rsn_info bss_info = {}; +struct ie_rsn_info rsn_info = {}; +uint8_t rsne_buf[256 ]; + +l_debug ("err=%i n_creds=%u" , err, n_creds); + + dev->conn_wsc_bss =NULL ; + dev->conn_enrollee =NULL ; + +l_timeout_remove (dev->config_timeout); +l_timeout_remove (dev->go_neg_req_timeout); + +if (err <0 ) { +if (err == -ECANCELED && peer->wsc.pending_cancel) { +dbus_pending_reply (&peer->wsc.pending_cancel, +l_dbus_message_new_method_return ( + peer->wsc.pending_cancel)); + +p2p_connection_reset (dev); + }else +p2p_connect_failed (dev); + + goto done; + } + +if (strlen (creds[0 ].ssid) != bss->ssid_len || +memcmp (creds[0 ].ssid, bss->ssid, bss->ssid_len)) { +l_error ("Unsupported: the SSID from the P2P peer's WSC " +"credentials doesn't match the SSID from the " +"Probe Response IEs" ); + goto not_supported; + } + +/* +* Apparently some implementations send the intended client's address +* here (i.e. our), and some send the target BSS's (their own). +*/ +if (memcmp (creds[0 ].addr,netdev_get_address (dev->conn_netdev),6 ) && +memcmp (creds[0 ].addr, bss->addr,6 )) { +char addr1[32 ], addr2[32 ]; + +l_strlcpy (addr1,util_address_to_string (creds[0 ].addr), +sizeof (addr1)); +l_strlcpy (addr2,util_address_to_string ( +netdev_get_address (dev->conn_netdev)), +sizeof (addr2)); +l_error ("Error: WSC credentials are not for our client " +"interface (%s vs. %s)" , addr1, addr2); + goto error; + } + +if (!bss->rsne || creds[0 ].security !=SECURITY_PSK ) + goto not_supported; + + info.capability = dev->capability; + info.device_info = dev->device_info; + + ie_iov.iov_base =p2p_build_association_req (&info, &ie_iov.iov_len); +L_WARN_ON (!ie_iov.iov_base); + +scan_bss_get_rsn_info (bss, &bss_info); + + rsn_info.akm_suites =wiphy_select_akm (dev->wiphy, bss, false); +if (!rsn_info.akm_suites) + goto not_supported; + + rsn_info.pairwise_ciphers =wiphy_select_cipher (dev->wiphy, + bss_info.pairwise_ciphers); + rsn_info.group_cipher =wiphy_select_cipher (dev->wiphy, + bss_info.group_cipher); +if (!rsn_info.pairwise_ciphers || !rsn_info.group_cipher) + goto not_supported; + + rsn_info.group_management_cipher =wiphy_select_cipher (dev->wiphy, + bss_info.group_management_cipher); + rsn_info.mfpc = rsn_info.group_management_cipher !=0 ; +ie_build_rsne (&rsn_info, rsne_buf); + + hs =netdev_handshake_state_new (dev->conn_netdev); + +if (!handshake_state_set_authenticator_ie (hs, bss->rsne)) + goto not_supported; + +if (!handshake_state_set_supplicant_ie (hs, rsne_buf)) + goto not_supported; + +handshake_state_set_event_func (hs, p2p_handshake_event, dev); +handshake_state_set_ssid (hs, bss->ssid, bss->ssid_len); + +if (creds[0 ].has_passphrase) { +uint8_t psk[32 ]; + +if (crypto_psk_from_passphrase (creds[0 ].passphrase, bss->ssid, + bss->ssid_len, psk) <0 ) + goto error; + +handshake_state_set_pmk (hs, psk,32 ); + }else +handshake_state_set_pmk (hs, creds[0 ].psk,32 ); + + r =netdev_connect (dev->conn_netdev, bss, hs, &ie_iov,1 , + p2p_netdev_event, p2p_netdev_connect_cb, dev); +if (r ==0 ) + goto done; + +l_error ("netdev_connect error: %s (%i)" ,strerror (-err), -err); + +error: +not_supported: +if (r <0 ) { +if (hs) +handshake_state_free (hs); + +p2p_connect_failed (dev); + } + +done: +l_free (ie_iov.iov_base); +scan_bss_free (bss); +} +static void p2p_provision_connect (struct p2p_device *dev) { -/* TODO */ +struct iovec iov; +struct p2p_association_req info = {}; + +/* Ready to start the provisioning */ + info.capability = dev->capability; + info.device_info = dev->device_info; + + iov.iov_base =p2p_build_association_req (&info, &iov.iov_len); +L_WARN_ON (!iov.iov_base); + + dev->conn_enrollee =wsc_enrollee_new (dev->conn_netdev, + dev->conn_wsc_bss, + dev->conn_pin, &iov,1 , + p2p_peer_provision_done, + dev->conn_peer); +l_free (iov.iov_base); }static void p2p_device_netdev_watch_destroy (void *user_data)
@@ -495,7 +759,8 @@ static void p2p_device_netdev_notify(struct netdev *netdev,switch (event) {case NETDEV_WATCH_EVENT_UP :case NETDEV_WATCH_EVENT_NEW : -if (!dev->conn_wsc_bss || !netdev_get_is_up (netdev)) +if (!dev->conn_wsc_bss || dev->conn_enrollee || + !netdev_get_is_up (netdev))break ;p2p_provision_connect (dev);
@@ -1669,6 +1934,23 @@ send_error:dbus_pending_reply (&peer->wsc.pending_connect, reply); } +static void p2p_peer_disconnect_cb (struct netdev *netdev,bool result, +void *user_data) +{ +struct p2p_peer *peer = user_data; +struct p2p_device *dev = peer->dev; + +if (!peer->wsc.pending_cancel || !dev->disconnecting) +return ; + +dbus_pending_reply (&peer->wsc.pending_cancel, +l_dbus_message_new_method_return ( + peer->wsc.pending_cancel)); + +/* Independent of the result this will just drop the whole netdev */ +p2p_connection_reset (dev); +} +static void p2p_peer_disconnect (struct p2p_peer *peer) {struct p2p_device *dev = peer->dev;
@@ -1680,10 +1962,35 @@ static void p2p_peer_disconnect(struct p2p_peer *peer) goto send_reply; } +if (dev->disconnecting) { + reply =dbus_error_busy (message); + goto send_reply; + } +if (peer->wsc.pending_connect)dbus_pending_reply (&peer->wsc.pending_connect,dbus_error_aborted (peer->wsc.pending_connect)); +if (p2p_peer_operational (peer)) +l_dbus_property_changed (dbus_get_bus (),p2p_peer_get_path (peer), +IWD_P2P_PEER_INTERFACE ,"Connected" ); + + dev->disconnecting = true; + +if (dev->conn_enrollee) { +wsc_enrollee_cancel (dev->conn_enrollee, true); +return ; + } + +if (dev->conn_netdev && !dev->conn_wsc_bss) { +/* Note: in theory we need to add the P2P IEs here too */ +if (netdev_disconnect (dev->conn_netdev, p2p_peer_disconnect_cb, + peer) ==0 ) +return ; + +l_error ("netdev_disconnect failed" ); + } +p2p_connection_reset (dev); reply =l_dbus_message_new_method_return (message);
@@ -1994,6 +2301,10 @@ static bool p2p_peer_update_existing(struct scan_bss *bss,/* * We've seen this peer already, only update the scan_bss object. +* We can do this even if peer == peer->dev->conn_peer because +* its .bss is not used by .conn_netdev or .conn_enrollee. +* .conn_wsc_bss is used for both connections and it doesn't come +* from the discovery scan results. * Do we need to update DBus properties? */ scan_bss_free (peer->bss);
@@ -3117,7 +3428,9 @@ static bool p2p_peer_get_connected(struct l_dbus *dbus,struct l_dbus_message_builder *builder, void *user_data) { - bool connected = false; + struct p2p_peer *peer = user_data; +bool connected =p2p_peer_operational (peer) && + peer->dev->conn_peer == peer;l_dbus_message_builder_append_basic (builder,'b' , &connected);return true;
Signature · 1 line
2.25.1
Re: [PATCH 10/11] p2p: WSC client provisioning and connection
I re-sent these already-applied 10/11 and the 11/11 patches by mistake, please ignore.
Best regards
Quoted 420 lines · Andrew Zaborowski
Add the final two steps of the connection setup, and corresponding disconnect logic:
* the WSC connection to the GO to do the client provisioning,
* thenetdev_connectcall to use the provisioned credentials for the final WPA2 connection.--- src/p2p.c | 319 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 316 insertions(+), 3 deletions(-)src/p2p.c +316
-4@@ -91,6 +91,7 @@ struct p2p_device {struct netdev *conn_netdev;uint32_t conn_netdev_watch_id;uint32_t conn_new_intf_cmd_id; +struct wsc_enrollee *conn_enrollee;struct l_timeout *config_timeout;unsigned long go_config_delay;@@ -103,6 +104,14 @@ struct p2p_device { bool enabled : 1; bool have_roc_cookie : 1; +/* +* We need to track @disconnecting because while a connect action is +* always triggered by a DBus message, meaning that @pending_message +* is going to be non-NULL, a disconnect may also be a result of an +* error at a layer higher than netdev and may last until +* netdev_disconnect, or similar, finishes. +*/ + bool disconnecting : 1; };struct p2p_discovery_user {@@ -173,6 +182,12 @@ static void p2p_discovery_user_free(void *data)l_free (user); } +static inline bool p2p_peer_operational (struct p2p_peer *peer) +{ +return peer && peer->dev->conn_netdev && !peer->dev->conn_wsc_bss && + !peer->wsc.pending_connect && !peer->dev->disconnecting; +} +static bool p2p_peer_match (const void *a,const void *b) {const struct p2p_peer *peer = a;@@ -296,6 +311,7 @@ static void p2p_connection_reset(struct p2p_device *dev)* age will be checked on the next scan. */ dev->conn_peer =NULL ; + dev->disconnecting = false; dev->connections_left++;if (dev->conn_pin) {@@ -320,6 +336,9 @@ static void p2p_connection_reset(struct p2p_device *dev)*/ l_genl_family_cancel (dev->nl80211, dev->conn_new_intf_cmd_id); +if (dev->conn_enrollee) +wsc_enrollee_cancel (dev->conn_enrollee, false); +if (dev->conn_netdev) {struct l_genl_msg *msg;uint64_t wdev_id =netdev_get_wdev_id (dev->conn_netdev);@@ -471,9 +490,254 @@ static const struct frame_xchg_prefix p2p_frame_pd_resp = { .len = 7, }; +static void p2p_netdev_connect_cb (struct netdev *netdev, +enum netdev_result result, +void *event_data,void *user_data) +{ +struct p2p_device *dev = user_data; +struct p2p_peer *peer = dev->conn_peer; + +l_debug ("result: %i" , result); + +if (!peer->wsc.pending_connect || dev->disconnecting) { +/* Shouldn't happen except maybe in the ABORTED case */ +return ; + } + +switch (result) { +case NETDEV_RESULT_OK : +dbus_pending_reply (&peer->wsc.pending_connect, +l_dbus_message_new_method_return ( + peer->wsc.pending_connect)); +l_dbus_property_changed (dbus_get_bus (), +p2p_peer_get_path (dev->conn_peer), +IWD_P2P_PEER_INTERFACE ,"Connected" ); +break ; +case NETDEV_RESULT_AUTHENTICATION_FAILED : +case NETDEV_RESULT_ASSOCIATION_FAILED : +case NETDEV_RESULT_HANDSHAKE_FAILED : +case NETDEV_RESULT_KEY_SETTING_FAILED : +/* +* In the AUTHENTICATION_FAILED and ASSOCIATION_FAILED +* cases there's nothing to disconnect. In the +* HANDSHAKE_FAILED and KEY_SETTINGS failed cases +* netdev disconnects from the GO automatically and we are +* called already from within the disconnect callback, +* so we can directly free the netdev. +*/ +p2p_connect_failed (dev); +break ; +case NETDEV_RESULT_ABORTED : +/* +* This case can only be triggered by netdev_disconnect so +* we'll wait for its callback before freeing the netdev. +* We will also have already replied to +* @peer->wsc.pending_connect so we have nothing to do here. +*/ +break ; + } +} + +static void p2p_netdev_event (struct netdev *netdev,enum netdev_event event, +void *event_data,void *user_data) +{ +struct p2p_device *dev = user_data; + +switch (event) { +case NETDEV_EVENT_DISCONNECT_BY_AP : +case NETDEV_EVENT_DISCONNECT_BY_SME : +/* +* We may get a DISCONNECT_BY_SME as a result of a +* netdev_disconnect(). In that case let the callback handle +* that. +*/ +if (dev->disconnecting) +break ; + +/* If we're not connected, .Connected is already False */ +if (!p2p_peer_operational (dev->conn_peer)) { +p2p_connect_failed (dev); +break ; + } + +l_dbus_property_changed (dbus_get_bus (), +p2p_peer_get_path (dev->conn_peer), +IWD_P2P_PEER_INTERFACE ,"Connected" ); +p2p_connection_reset (dev); +break ; +default : +break ; + }; +} + +static void p2p_handshake_event (struct handshake_state *hs, +enum handshake_event event,void *user_data, + ...) +{ +va_list args; + +va_start (args, user_data); + +switch (event) { +case HANDSHAKE_EVENT_FAILED : +netdev_handshake_failed (hs,va_arg (args, int)); +break ; +default : +break ; + } + +va_end (args); +} + +static void p2p_peer_provision_done (int err,struct wsc_credentials_info *creds, +unsigned int n_creds,void *user_data) +{ +struct p2p_peer *peer = user_data; +struct p2p_device *dev = peer->dev; +struct scan_bss *bss = dev->conn_wsc_bss; +struct handshake_state *hs =NULL ; +struct iovec ie_iov = {}; +int r = -EOPNOTSUPP ; +struct p2p_association_req info = {}; +struct ie_rsn_info bss_info = {}; +struct ie_rsn_info rsn_info = {}; +uint8_t rsne_buf[256 ]; + +l_debug ("err=%i n_creds=%u" , err, n_creds); + + dev->conn_wsc_bss =NULL ; + dev->conn_enrollee =NULL ; + +l_timeout_remove (dev->config_timeout); +l_timeout_remove (dev->go_neg_req_timeout); + +if (err <0 ) { +if (err == -ECANCELED && peer->wsc.pending_cancel) { +dbus_pending_reply (&peer->wsc.pending_cancel, +l_dbus_message_new_method_return ( + peer->wsc.pending_cancel)); + +p2p_connection_reset (dev); + }else +p2p_connect_failed (dev); + + goto done; + } + +if (strlen (creds[0 ].ssid) != bss->ssid_len || +memcmp (creds[0 ].ssid, bss->ssid, bss->ssid_len)) { +l_error ("Unsupported: the SSID from the P2P peer's WSC " +"credentials doesn't match the SSID from the " +"Probe Response IEs" ); + goto not_supported; + } + +/* +* Apparently some implementations send the intended client's address +* here (i.e. our), and some send the target BSS's (their own). +*/ +if (memcmp (creds[0 ].addr,netdev_get_address (dev->conn_netdev),6 ) && +memcmp (creds[0 ].addr, bss->addr,6 )) { +char addr1[32 ], addr2[32 ]; + +l_strlcpy (addr1,util_address_to_string (creds[0 ].addr), +sizeof (addr1)); +l_strlcpy (addr2,util_address_to_string ( +netdev_get_address (dev->conn_netdev)), +sizeof (addr2)); +l_error ("Error: WSC credentials are not for our client " +"interface (%s vs. %s)" , addr1, addr2); + goto error; + } + +if (!bss->rsne || creds[0 ].security !=SECURITY_PSK ) + goto not_supported; + + info.capability = dev->capability; + info.device_info = dev->device_info; + + ie_iov.iov_base =p2p_build_association_req (&info, &ie_iov.iov_len); +L_WARN_ON (!ie_iov.iov_base); + +scan_bss_get_rsn_info (bss, &bss_info); + + rsn_info.akm_suites =wiphy_select_akm (dev->wiphy, bss, false); +if (!rsn_info.akm_suites) + goto not_supported; + + rsn_info.pairwise_ciphers =wiphy_select_cipher (dev->wiphy, + bss_info.pairwise_ciphers); + rsn_info.group_cipher =wiphy_select_cipher (dev->wiphy, + bss_info.group_cipher); +if (!rsn_info.pairwise_ciphers || !rsn_info.group_cipher) + goto not_supported; + + rsn_info.group_management_cipher =wiphy_select_cipher (dev->wiphy, + bss_info.group_management_cipher); + rsn_info.mfpc = rsn_info.group_management_cipher !=0 ; +ie_build_rsne (&rsn_info, rsne_buf); + + hs =netdev_handshake_state_new (dev->conn_netdev); + +if (!handshake_state_set_authenticator_ie (hs, bss->rsne)) + goto not_supported; + +if (!handshake_state_set_supplicant_ie (hs, rsne_buf)) + goto not_supported; + +handshake_state_set_event_func (hs, p2p_handshake_event, dev); +handshake_state_set_ssid (hs, bss->ssid, bss->ssid_len); + +if (creds[0 ].has_passphrase) { +uint8_t psk[32 ]; + +if (crypto_psk_from_passphrase (creds[0 ].passphrase, bss->ssid, + bss->ssid_len, psk) <0 ) + goto error; + +handshake_state_set_pmk (hs, psk,32 ); + }else +handshake_state_set_pmk (hs, creds[0 ].psk,32 ); + + r =netdev_connect (dev->conn_netdev, bss, hs, &ie_iov,1 , + p2p_netdev_event, p2p_netdev_connect_cb, dev); +if (r ==0 ) + goto done; + +l_error ("netdev_connect error: %s (%i)" ,strerror (-err), -err); + +error: +not_supported: +if (r <0 ) { +if (hs) +handshake_state_free (hs); + +p2p_connect_failed (dev); + } + +done: +l_free (ie_iov.iov_base); +scan_bss_free (bss); +} +static void p2p_provision_connect (struct p2p_device *dev) { -/* TODO */ +struct iovec iov; +struct p2p_association_req info = {}; + +/* Ready to start the provisioning */ + info.capability = dev->capability; + info.device_info = dev->device_info; + + iov.iov_base =p2p_build_association_req (&info, &iov.iov_len); +L_WARN_ON (!iov.iov_base); + + dev->conn_enrollee =wsc_enrollee_new (dev->conn_netdev, + dev->conn_wsc_bss, + dev->conn_pin, &iov,1 , + p2p_peer_provision_done, + dev->conn_peer); +l_free (iov.iov_base); }static void p2p_device_netdev_watch_destroy (void *user_data)@@ -495,7 +759,8 @@ static void p2p_device_netdev_notify(struct netdev *netdev,switch (event) {case NETDEV_WATCH_EVENT_UP :case NETDEV_WATCH_EVENT_NEW : -if (!dev->conn_wsc_bss || !netdev_get_is_up (netdev)) +if (!dev->conn_wsc_bss || dev->conn_enrollee || + !netdev_get_is_up (netdev))break ;p2p_provision_connect (dev);@@ -1669,6 +1934,23 @@ send_error:dbus_pending_reply (&peer->wsc.pending_connect, reply); } +static void p2p_peer_disconnect_cb (struct netdev *netdev,bool result, +void *user_data) +{ +struct p2p_peer *peer = user_data; +struct p2p_device *dev = peer->dev; + +if (!peer->wsc.pending_cancel || !dev->disconnecting) +return ; + +dbus_pending_reply (&peer->wsc.pending_cancel, +l_dbus_message_new_method_return ( + peer->wsc.pending_cancel)); + +/* Independent of the result this will just drop the whole netdev */ +p2p_connection_reset (dev); +} +static void p2p_peer_disconnect (struct p2p_peer *peer) {struct p2p_device *dev = peer->dev;@@ -1680,10 +1962,35 @@ static void p2p_peer_disconnect(struct p2p_peer *peer) goto send_reply; } +if (dev->disconnecting) { + reply =dbus_error_busy (message); + goto send_reply; + } +if (peer->wsc.pending_connect)dbus_pending_reply (&peer->wsc.pending_connect,dbus_error_aborted (peer->wsc.pending_connect)); +if (p2p_peer_operational (peer)) +l_dbus_property_changed (dbus_get_bus (),p2p_peer_get_path (peer), +IWD_P2P_PEER_INTERFACE ,"Connected" ); + + dev->disconnecting = true; + +if (dev->conn_enrollee) { +wsc_enrollee_cancel (dev->conn_enrollee, true); +return ; + } + +if (dev->conn_netdev && !dev->conn_wsc_bss) { +/* Note: in theory we need to add the P2P IEs here too */ +if (netdev_disconnect (dev->conn_netdev, p2p_peer_disconnect_cb, + peer) ==0 ) +return ; + +l_error ("netdev_disconnect failed" ); + } +p2p_connection_reset (dev); reply =l_dbus_message_new_method_return (message);@@ -1994,6 +2301,10 @@ static bool p2p_peer_update_existing(struct scan_bss *bss,/* * We've seen this peer already, only update the scan_bss object. +* We can do this even if peer == peer->dev->conn_peer because +* its .bss is not used by .conn_netdev or .conn_enrollee. +* .conn_wsc_bss is used for both connections and it doesn't come +* from the discovery scan results. * Do we need to update DBus properties? */ scan_bss_free (peer->bss);@@ -3117,7 +3428,9 @@ static bool p2p_peer_get_connected(struct l_dbus *dbus,struct l_dbus_message_builder *builder, void *user_data) { - bool connected = false; + struct p2p_peer *peer = user_data; +bool connected =p2p_peer_operational (peer) && + peer->dev->conn_peer == peer;l_dbus_message_builder_append_basic (builder,'b' , &connected);return true; --2.25.1
PATCH 11/11 test: Add a connect-p2p test script
In reply to Andrew Zaborowski--- test/connect-p2p | 155 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100755 test/connect-p2p
test/connect-p2p new file +155 -0
@@ -0,0 +1,155 @@ +#!/usr/bin/python3 + +import sys +import dbus +import dbus.mainloop.glib +from gi.repository import GLib +import fnmatch + +if len(sys.argv) not in [1, 2]: + print("Usage: %s [<name_mask>]" % (sys.argv[0],)) + sys.exit(1) + +name_mask = None +if len(sys.argv) >= 2: + name_mask = sys.argv[1] + +DEVICE_IF = 'net.connman.iwd.p2p.Device' +PEER_IF = 'net.connman.iwd.p2p.Peer' +WSC_IF = 'net.connman.iwd.SimpleConfiguration' + +dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + +bus = dbus.SystemBus() +manager = dbus.Interface(bus.get_object('net.connman.iwd', '/'), + 'org.freedesktop.DBus.ObjectManager') +objects = manager.GetManagedObjects() + +p2p_path = None +peers = {} +connecting = False +mainloop = None + +for path in objects: + if DEVICE_IF in objects[path] and p2p_path is None: + p2p_path = path + +if p2p_path is None: + print('No P2P interfaces') + sys.exit(-1) + +values = objects[p2p_path][DEVICE_IF] +properties = dbus.Interface(bus.get_object('net.connman.iwd', p2p_path), + 'org.freedesktop.DBus.Properties') +p2p = dbus.Interface(bus.get_object('net.connman.iwd', p2p_path), DEVICE_IF) + +def list_peers(): + global peers + + print('We have the following P2P peers') + for path in peers: + result = peers[path] + + print(' ' + result['Name'] + ' at ' + path) + +def connect(path): + global connecting, peers, mainloop + + list_peers() + + peer = dbus.Interface(bus.get_object('net.connman.iwd', path), PEER_IF) + peer_wsc = dbus.Interface(bus.get_object('net.connman.iwd', path), WSC_IF) + peer_props = peers[path] + + print('Connecting to ' + peer_props['Name']) + connecting = True + try: + peer_wsc.PushButton() + print('New connection') + except Exception as e: + connecting = False + print('Connection error: ' + repr(e)) + + if mainloop: + mainloop.quit() + else: + sys.exit(0) + +def properties_changed(interface, changed, invalidated, path): + global peers + + path = str(path) + if interface != PEER_IF or path not in peers: + return + + peers[path].update(changed) + for k in invalidated: + del peers[path][k] + +def interfaces_added(path, interfaces): + global connecting, peers + + if PEER_IF not in interfaces: + return + + path = str(path) + peers[path] = interfaces[PEER_IF] + print('Added peer ' + str(peers[path]['Name'])) + + if connecting or name_mask is None or not fnmatch.fnmatch(peers[path]['Name'], name_mask): + return + + connect(path) + +def interfaces_removed(path, interfaces): + global peers + + if PEER_IF not in interfaces: + return + + print('Removed peer ' + str(peers[path]['Name'])) + del peers[path] + +if not values['Enabled']: + properties.Set(DEVICE_IF, 'Enabled', True) + +if not values['AvailableConnections']: + # See if we have peers we can disconnect or where we can cancel an ongoing + # connection that may be in the 120s GO Negotiation timeout for example. + # Should we have a Connecting property on the WSC interface so we can know + # whether a connection is in progress? + for path in objects: + if PEER_IF not in objects[path]: + continue + peer_values = objects[path][PEER_IF] + print('Trying to disconnect from peer ' + str(peer_values['Name'])) + peer = dbus.Interface(bus.get_object('net.connman.iwd', path), PEER_IF) + try: + peer.Disconnect() + except: + pass + +for path in objects: + if PEER_IF in objects[path]: + interfaces_added(path, objects[path]) + +p2p.RequestDiscovery() + +bus.add_signal_receiver(properties_changed, + bus_name="net.connman.iwd", + dbus_interface="org.freedesktop.DBus.Properties", + signal_name="PropertiesChanged", + path_keyword="path") + +bus.add_signal_receiver(interfaces_added, + bus_name="net.connman.iwd", + dbus_interface="org.freedesktop.DBus.ObjectManager", + signal_name="InterfacesAdded") + +bus.add_signal_receiver(interfaces_removed, + bus_name="net.connman.iwd", + dbus_interface="org.freedesktop.DBus.ObjectManager", + signal_name="InterfacesRemoved") + +mainloop = GLib.MainLoop() +mainloop.run()
Signature · 1 line
2.25.1