Add NL80211_CMD_NAN_NDP_TERMINATE to let userspace tear down an NDP or cancel NDP setup in progress when the device offloads NDP management. Pass the initiator NDI from the request through to the driver; it may identify either the local or peer NDI and is not validated as a local interface. Add the termination parameter structure, command handler, and driver callback, and trace the initiator NDI with the other request metadata. The initiate path builds its reply before calling the driver, but the reply can still fail to reach userspace after the driver has accepted the setup (for example, if the socket receive buffer is full). In that case userspace never learns the NDP ID, so use the new callback to cancel the setup. Signed-off-by: Shivani Baranwal --- include/net/cfg80211.h | 24 +++++++++++ include/uapi/linux/nl80211.h | 7 ++++ net/wireless/nl80211.c | 79 +++++++++++++++++++++++++++++++++++- net/wireless/rdev-ops.h | 16 ++++++++ net/wireless/trace.h | 27 ++++++++++++ 5 files changed, 152 insertions(+), 1 deletion(-) diff --git a/include/net/cfg80211.h b/include/net/cfg80211.h index 5857ce6be50f..eda9a073422b 100644 --- a/include/net/cfg80211.h +++ b/include/net/cfg80211.h @@ -4495,6 +4495,21 @@ struct cfg80211_nan_ndp_respond { u16 qos_max_latency; }; +/** + * struct cfg80211_nan_ndp_terminate - NAN NDP terminate parameters + * + * @peer_nmi: peer NMI address + * @ndp_id: NDP ID to terminate + * @init_ndi: initiator NDI MAC address + * @reason: Wi-Fi Aware reason code for termination + */ +struct cfg80211_nan_ndp_terminate { + const u8 *peer_nmi; + u8 ndp_id; + const u8 *init_ndi; + u8 reason; +}; + /** * enum cfg80211_nan_conf_changes - indicates changed fields in NAN * configuration @@ -5495,6 +5510,12 @@ struct mgmt_frame_regs { * asynchronously after the handshake. If the accepted response's QoS * requirements cannot be met, the driver reports setup failure with the * Wi-Fi Aware reason QoS_UNACCEPTABLE. + * @nan_ndp_terminate: Terminate an NDP session or cancel NDP setup in + * progress. cfg80211 also uses it to cancel a locally initiated setup + * whose NDP ID reply could not be delivered to userspace. Only valid if + * the driver has set %WIPHY_NAN_FLAGS_OFFLOAD_NDP. Returning success + * acknowledges the request; report completion asynchronously with + * cfg80211_nan_ndp_terminated(). * * @set_multicast_to_unicast: configure multicast to unicast conversion for BSS * @@ -5916,6 +5937,9 @@ struct cfg80211_ops { int (*nan_ndp_respond)(struct wiphy *wiphy, struct wireless_dev *wdev, struct cfg80211_nan_ndp_respond *params); + int (*nan_ndp_terminate)(struct wiphy *wiphy, + struct wireless_dev *wdev, + struct cfg80211_nan_ndp_terminate *params); int (*set_multicast_to_unicast)(struct wiphy *wiphy, struct net_device *dev, const bool enabled); diff --git a/include/uapi/linux/nl80211.h b/include/uapi/linux/nl80211.h index 1d5e2f2f08ca..54249b98fcc7 100644 --- a/include/uapi/linux/nl80211.h +++ b/include/uapi/linux/nl80211.h @@ -1605,6 +1605,12 @@ * NDL QoS attributes may also be supplied only when accepting. If the device * cannot meet the QoS requirements in an accepted response, setup fails and * the status notification reports the Wi-Fi Aware reason QoS_UNACCEPTABLE. + * @NL80211_CMD_NAN_NDP_TERMINATE: Terminate an NDP or cancel setup in progress. + * Requires %NL80211_NAN_CAPA_OFFLOAD_NDP. Carries %NL80211_ATTR_NAN_NDP + * nested with %NL80211_NAN_NDP_ATTR_ID, %NL80211_NAN_NDP_ATTR_INIT_NDI + * and optionally %NL80211_NAN_NDP_ATTR_REASON. + * The command reply acknowledges the request; teardown completion is + * reported asynchronously by the termination notification. * * @NL80211_CMD_MAX: highest used command number * @__NL80211_CMD_AFTER_LAST: internal use @@ -1895,6 +1901,7 @@ enum nl80211_commands { NL80211_CMD_NAN_NDP_INITIATE, NL80211_CMD_NAN_NDP_RESPOND, + NL80211_CMD_NAN_NDP_TERMINATE, /* add new commands above here */ diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c index 74be0f639315..571432435bfa 100644 --- a/net/wireless/nl80211.c +++ b/net/wireless/nl80211.c @@ -18738,7 +18738,26 @@ static int nl80211_nan_ndp_initiate(struct sk_buff *skb, *(u8 *)nla_data(id_attr) = params.ndp_id; nla_nest_end(msg, ndp_attr); genlmsg_end(msg, hdr); - return genlmsg_reply(msg, info); + err = genlmsg_reply(msg, info); + if (err) { + struct cfg80211_nan_ndp_terminate terminate = { + .peer_nmi = params.peer_nmi, + .ndp_id = params.ndp_id, + .init_ndi = params.init_ndi, + }; + int term_err; + + /* Userspace never learned the NDP ID, so it cannot manage + * this NDP; cancel the setup the driver already accepted. + */ + term_err = rdev_nan_ndp_terminate(rdev, wdev, &terminate); + if (term_err) + wiphy_warn(&rdev->wiphy, + "failed to terminate NDP %u after reply error: %d (%d)\n", + params.ndp_id, err, term_err); + } + + return err; } static int nl80211_nan_ndp_respond(struct sk_buff *skb, @@ -18911,6 +18930,58 @@ static int nl80211_nan_ndp_respond(struct sk_buff *skb, return rdev_nan_ndp_respond(rdev, wdev, ¶ms); } +static int nl80211_nan_ndp_terminate(struct sk_buff *skb, + struct genl_info *info) +{ + struct cfg80211_registered_device *rdev = info->user_ptr[0]; + struct wireless_dev *wdev = info->user_ptr[1]; + struct nlattr *tb[NL80211_NAN_NDP_ATTR_MAX + 1]; + struct cfg80211_nan_ndp_terminate params = {}; + int err; + + if (wdev->iftype != NL80211_IFTYPE_NAN) { + NL_SET_ERR_MSG(info->extack, + "NDP terminate requires NAN interface"); + return -EOPNOTSUPP; + } + + if (!(rdev->wiphy.nan_capa.flags & WIPHY_NAN_FLAGS_OFFLOAD_NDP)) { + NL_SET_ERR_MSG(info->extack, + "NAN NDP offload not supported by driver"); + return -EOPNOTSUPP; + } + + if (!info->attrs[NL80211_ATTR_MAC] || + !info->attrs[NL80211_ATTR_NAN_NDP]) { + NL_SET_ERR_MSG(info->extack, + "NDP terminate requires peer NMI and NDP params"); + return -EINVAL; + } + + err = nla_parse_nested(tb, NL80211_NAN_NDP_ATTR_MAX, + info->attrs[NL80211_ATTR_NAN_NDP], + nl80211_nan_ndp_policy, + info->extack); + if (err) + return err; + + if (!tb[NL80211_NAN_NDP_ATTR_ID] || + !tb[NL80211_NAN_NDP_ATTR_INIT_NDI]) { + NL_SET_ERR_MSG(info->extack, + "NDP terminate requires NDP ID and INIT_NDI"); + return -EINVAL; + } + + params.peer_nmi = nla_data(info->attrs[NL80211_ATTR_MAC]); + params.ndp_id = nla_get_u8(tb[NL80211_NAN_NDP_ATTR_ID]); + params.init_ndi = nla_data(tb[NL80211_NAN_NDP_ATTR_INIT_NDI]); + + if (tb[NL80211_NAN_NDP_ATTR_REASON]) + params.reason = nla_get_u8(tb[NL80211_NAN_NDP_ATTR_REASON]); + + return rdev_nan_ndp_terminate(rdev, wdev, ¶ms); +} + static bool nl80211_nan_is_sched_empty(struct cfg80211_nan_local_sched *sched) { if (!sched->n_channels) @@ -22237,6 +22308,12 @@ static const struct genl_small_ops nl80211_small_ops[] = { .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP | NL80211_FLAG_CLEAR_SKB), }, + { + .cmd = NL80211_CMD_NAN_NDP_TERMINATE, + .doit = nl80211_nan_ndp_terminate, + .flags = GENL_ADMIN_PERM, + .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP), + }, }; static struct genl_family nl80211_fam __ro_after_init = { diff --git a/net/wireless/rdev-ops.h b/net/wireless/rdev-ops.h index c58b83210d60..3e4e0d768ae5 100644 --- a/net/wireless/rdev-ops.h +++ b/net/wireless/rdev-ops.h @@ -1221,6 +1221,22 @@ rdev_nan_ndp_respond(struct cfg80211_registered_device *rdev, return ret; } +static inline int +rdev_nan_ndp_terminate(struct cfg80211_registered_device *rdev, + struct wireless_dev *wdev, + struct cfg80211_nan_ndp_terminate *params) +{ + int ret; + + if (!rdev->ops->nan_ndp_terminate) + return -EOPNOTSUPP; + + trace_rdev_nan_ndp_terminate(&rdev->wiphy, wdev, params); + ret = rdev->ops->nan_ndp_terminate(&rdev->wiphy, wdev, params); + trace_rdev_return_int(&rdev->wiphy, ret); + return ret; +} + static inline int rdev_set_mac_acl(struct cfg80211_registered_device *rdev, struct net_device *dev, struct cfg80211_acl_data *params) diff --git a/net/wireless/trace.h b/net/wireless/trace.h index 8c5fdbe23747..a843b52cbcfc 100644 --- a/net/wireless/trace.h +++ b/net/wireless/trace.h @@ -2737,6 +2737,33 @@ TRACE_EVENT(rdev_nan_ndp_respond, #undef NDP_RESP_FMT_3 #undef NDP_RESP_FMT_4 +TRACE_EVENT(rdev_nan_ndp_terminate, + TP_PROTO(struct wiphy *wiphy, struct wireless_dev *wdev, + struct cfg80211_nan_ndp_terminate *params), + TP_ARGS(wiphy, wdev, params), + TP_STRUCT__entry( + WIPHY_ENTRY + WDEV_ENTRY + __array(u8, peer_nmi, ETH_ALEN) + __field(u8, ndp_id) + __array(u8, init_ndi, ETH_ALEN) + __field(u8, reason) + ), + TP_fast_assign( + WIPHY_ASSIGN; + WDEV_ASSIGN; + memcpy(__entry->peer_nmi, params->peer_nmi, ETH_ALEN); + __entry->ndp_id = params->ndp_id; + memcpy(__entry->init_ndi, params->init_ndi, ETH_ALEN); + __entry->reason = params->reason; + ), + TP_printk(WIPHY_PR_FMT ", " WDEV_PR_FMT + ", peer_nmi: %pM, ndp_id: %u, init_ndi: %pM, reason: %u", + WIPHY_PR_ARG, WDEV_PR_ARG, + __entry->peer_nmi, __entry->ndp_id, __entry->init_ndi, + __entry->reason) +); + TRACE_EVENT(rdev_set_mac_acl, TP_PROTO(struct wiphy *wiphy, struct net_device *netdev, struct cfg80211_acl_data *params), -- 2.34.1