BACKPORT: cfg80211: allow userspace to take ownership of interfaces

(cherry pick from commit 78f22b6a3a9254460d23060530b48ae02a9394e3)

When dynamically creating interfaces from userspace, e.g. for P2P usage,
such interfaces are usually owned by the process that created them, i.e.
wpa_supplicant. Should wpa_supplicant crash, such interfaces will often
cease operating properly and cause problems on restarting the process.

To avoid this problem, introduce an ownership concept for interfaces. If
an interface is owned by a netlink socket, then it will be destroyed if
the netlink socket is closed for any reason, including if the process it
belongs to crashed. This gives us a race-free way to get rid of any such
interfaces.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Bug: 25561044
Change-Id: I5a9c8883c5c204ac5d2917ab8492b44daf4b71e7
Git-commit: 5586920dfea6a8dc963a55572de0256a4faa636e
Git-repo: https://android.googlesource.com/kernel/common.git
[resolved trivial merge conflicts]
Signed-off-by: Srinivasarao P <spathi@codeaurora.org>
This commit is contained in:
Johannes Berg 2014-03-24 17:57:27 +01:00 committed by syphyr
parent cffd694b3f
commit fb18d28480
5 changed files with 91 additions and 1 deletions

View File

@ -3022,6 +3022,7 @@ struct cfg80211_cached_keys;
* @p2p_started: true if this is a P2P Device that has been started
* @cac_started: true if DFS channel availability check has been started
* @cac_start_time: timestamp (jiffies) when the dfs state was entered.
* @owner_nlportid: (private) owner socket port ID
*/
struct wireless_dev {
struct wiphy *wiphy;
@ -3076,6 +3077,8 @@ struct wireless_dev {
bool cac_started;
unsigned long cac_start_time;
u32 owner_nlportid;
#ifdef CONFIG_CFG80211_WEXT
/* wext data */
struct {

View File

@ -1547,6 +1547,10 @@ enum nl80211_commands {
* @NL80211_ATTR_TDLS_PEER_CAPABILITY: flags for TDLS peer capabilities, u32.
* As specified in the &enum nl80211_tdls_peer_capability.
*
* @NL80211_ATTR_IFACE_SOCKET_OWNER: flag attribute, if set during interface
* creation then the new interface will be owned by the netlink socket
* that created it and will be destroyed when the socket is closed
*
* @NL80211_ATTR_MAX: highest attribute number currently defined
* @__NL80211_ATTR_AFTER_LAST: internal use
*/
@ -1883,6 +1887,8 @@ enum nl80211_attrs {
NL80211_ATTR_TDLS_PEER_CAPABILITY,
NL80211_ATTR_IFACE_SOCKET_OWNER,
/* add attributes here, update the policy in nl80211.c */
__NL80211_ATTR_AFTER_LAST,

View File

@ -308,6 +308,45 @@ static void cfg80211_event_work(struct work_struct *work)
rtnl_unlock();
}
void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
{
struct cfg80211_iface_destroy *item;
ASSERT_RTNL();
spin_lock_irq(&rdev->destroy_list_lock);
while ((item = list_first_entry_or_null(&rdev->destroy_list,
struct cfg80211_iface_destroy,
list))) {
struct wireless_dev *wdev, *tmp;
u32 nlportid = item->nlportid;
list_del(&item->list);
kfree(item);
spin_unlock_irq(&rdev->destroy_list_lock);
list_for_each_entry_safe(wdev, tmp, &rdev->wdev_list, list) {
if (nlportid == wdev->owner_nlportid)
rdev_del_virtual_intf(rdev, wdev);
}
spin_lock_irq(&rdev->destroy_list_lock);
}
spin_unlock_irq(&rdev->destroy_list_lock);
}
static void cfg80211_destroy_iface_wk(struct work_struct *work)
{
struct cfg80211_registered_device *rdev;
rdev = container_of(work, struct cfg80211_registered_device,
destroy_work);
rtnl_lock();
cfg80211_destroy_ifaces(rdev);
rtnl_unlock();
}
/* exported functions */
struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
@ -376,6 +415,10 @@ struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
rdev->wiphy.dev.class = &ieee80211_class;
rdev->wiphy.dev.platform_data = rdev;
INIT_LIST_HEAD(&rdev->destroy_list);
spin_lock_init(&rdev->destroy_list_lock);
INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
#ifdef CONFIG_CFG80211_DEFAULT_PS
rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
#endif
@ -744,6 +787,7 @@ void wiphy_unregister(struct wiphy *wiphy)
cancel_work_sync(&rdev->conn_work);
flush_work(&rdev->event_work);
cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
flush_work(&rdev->destroy_work);
if (rdev->wowlan && rdev->ops->set_wakeup)
rdev_set_wakeup(rdev, false);

View File

@ -90,6 +90,10 @@ struct cfg80211_registered_device {
/* netlink port which started critical protocol (0 means not started) */
u32 crit_proto_nlportid;
spinlock_t destroy_list_lock;
struct list_head destroy_list;
struct work_struct destroy_work;
/* must be last because of the way we do wiphy_priv(),
* and it should at least be aligned to NETDEV_ALIGN */
struct wiphy wiphy __aligned(NETDEV_ALIGN);
@ -262,6 +266,13 @@ struct cfg80211_beacon_registration {
u32 nlportid;
};
struct cfg80211_iface_destroy {
struct list_head list;
u32 nlportid;
};
void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev);
/* free object */
extern void cfg80211_dev_free(struct cfg80211_registered_device *rdev);

View File

@ -390,6 +390,7 @@ static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
[NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
[NL80211_ATTR_IFACE_SOCKET_OWNER] = { .type = NLA_FLAG },
};
/* policy for the key attributes */
@ -2524,6 +2525,9 @@ static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
u32 flags;
/* to avoid failing a new interface creation due to pending removal */
cfg80211_destroy_ifaces(rdev);
memset(&params, 0, sizeof(params));
if (!info->attrs[NL80211_ATTR_IFNAME])
@ -2568,6 +2572,9 @@ static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
return PTR_ERR(wdev);
}
if (info->attrs[NL80211_ATTR_IFACE_SOCKET_OWNER])
wdev->owner_nlportid = info->snd_portid;
switch (type) {
case NL80211_IFTYPE_MESH_POINT:
if (!info->attrs[NL80211_ATTR_MESH_ID])
@ -11186,9 +11193,15 @@ static int nl80211_netlink_notify(struct notifier_block * nb,
rcu_read_lock();
list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
bool schedule_destroy_work = false;
list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
cfg80211_mlme_unregister_socket(wdev, notify->portid);
if (wdev->owner_nlportid == notify->portid)
schedule_destroy_work = true;
}
spin_lock_bh(&rdev->beacon_registrations_lock);
list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
list) {
@ -11199,6 +11212,19 @@ static int nl80211_netlink_notify(struct notifier_block * nb,
}
}
spin_unlock_bh(&rdev->beacon_registrations_lock);
if (schedule_destroy_work) {
struct cfg80211_iface_destroy *destroy;
destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
if (destroy) {
destroy->nlportid = notify->portid;
spin_lock(&rdev->destroy_list_lock);
list_add(&destroy->list, &rdev->destroy_list);
spin_unlock(&rdev->destroy_list_lock);
schedule_work(&rdev->destroy_work);
}
}
}
rcu_read_unlock();