Use NL80211_CMD_NAN_NDP_TERMINATE to notify userspace when an NDP is terminated. Include the NDP identity and reason, and add the LOCALLY_GENERATED output flag to distinguish locally initiated termination from peer-initiated termination. Add the termination parameter and cfg80211 notification function, validate and trace the event, and deliver it to the NAN owner or multicast group. Signed-off-by: Shivani Baranwal --- include/net/cfg80211.h | 36 +++++++++++++++++++++++ include/uapi/linux/nl80211.h | 16 +++++++++-- net/wireless/nl80211.c | 56 ++++++++++++++++++++++++++++++++++++ net/wireless/trace.h | 29 +++++++++++++++++++ 4 files changed, 135 insertions(+), 2 deletions(-) diff --git a/include/net/cfg80211.h b/include/net/cfg80211.h index 93a69f2a8db9..e1ed02e5b371 100644 --- a/include/net/cfg80211.h +++ b/include/net/cfg80211.h @@ -4587,6 +4587,25 @@ struct cfg80211_nan_ndp_status_params { u8 transport_protocol; }; +/** + * struct cfg80211_nan_ndp_terminated_params - NAN NDP terminated event + * + * @peer_nmi: peer NMI address + * @ndp_id: NDP ID + * @init_ndi: initiator NDI MAC address + * @reason: Wi-Fi Aware reason code for termination + * @locally_generated: if true, teardown was initiated or detected locally + * (userspace or device) rather than by a peer termination request. + * Includes NDI down/removal, NAN stop, and peer loss. + */ +struct cfg80211_nan_ndp_terminated_params { + const u8 *peer_nmi; + u8 ndp_id; + const u8 *init_ndi; + u8 reason; + bool locally_generated; +}; + /** * enum cfg80211_nan_conf_changes - indicates changed fields in NAN * configuration @@ -11782,6 +11801,23 @@ cfg80211_nan_ndp_status(struct wireless_dev *wdev, const struct cfg80211_nan_ndp_status_params *params, gfp_t gfp); +/** + * cfg80211_nan_ndp_terminated - notify userspace that an NDP was terminated + * @wdev: the wireless device reporting the event + * @params: NDP termination parameters + * @gfp: allocation flags + * + * Notify userspace that a NAN Data Path was torn down. Call exactly once for + * each established NDP, including teardown due to NDI down/removal, NAN stop, + * or peer loss. Only valid when %WIPHY_NAN_FLAGS_OFFLOAD_NDP is set. + * The locally_generated field is true when teardown was initiated or detected + * locally (userspace or device), and false when the peer initiated it. + */ +void +cfg80211_nan_ndp_terminated(struct wireless_dev *wdev, + const struct cfg80211_nan_ndp_terminated_params *params, + gfp_t gfp); + /** * cfg80211_incumbent_signal_notify - Notify userspace of incumbent signal detection * @wiphy: the wiphy to use diff --git a/include/uapi/linux/nl80211.h b/include/uapi/linux/nl80211.h index 69654a365f83..16326b62e581 100644 --- a/include/uapi/linux/nl80211.h +++ b/include/uapi/linux/nl80211.h @@ -1612,7 +1612,13 @@ * 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. + * termination notification. This command is also used as a notification + * when an NDP is terminated. The notification carries %NL80211_ATTR_MAC + * and %NL80211_ATTR_NAN_NDP with %NL80211_NAN_NDP_ATTR_ID, + * %NL80211_NAN_NDP_ATTR_INIT_NDI, %NL80211_NAN_NDP_ATTR_REASON, and, when + * applicable, %NL80211_NAN_NDP_ATTR_LOCALLY_GENERATED. Exactly one + * notification is sent for each established NDP that terminates, including + * teardown due to NDI down/removal, NAN stop, or peer loss. * @NL80211_CMD_NAN_NDP_STATUS: Notification-only event sent to userspace on the * initiator and responder sides when NDP setup finishes, successfully or * not. It carries %NL80211_ATTR_MAC and %NL80211_ATTR_NAN_NDP, including @@ -8246,7 +8252,12 @@ enum nl80211_nan_conf_attributes { * reason code (%NL80211_ATTR_REASON_CODE). Used for rejection with * %NL80211_CMD_NAN_NDP_RESPOND, setup failure with * %NL80211_CMD_NAN_NDP_STATUS, or termination with - * %NL80211_CMD_NAN_NDP_TERMINATE. + * %NL80211_CMD_NAN_NDP_TERMINATE, including its termination notification. + * @NL80211_NAN_NDP_ATTR_LOCALLY_GENERATED: output-only flag, present in the + * termination notification when teardown was initiated or detected locally + * (by userspace or the device), rather than by a peer termination request. + * This includes teardown on NDI down/removal, NAN stop, and peer loss; the + * flag is absent when the peer initiated termination. * @__NL80211_NAN_NDP_ATTR_LAST: Internal. * @NL80211_NAN_NDP_ATTR_MAX: Highest NDP attribute. */ @@ -8271,6 +8282,7 @@ enum nl80211_nan_ndp_attributes { NL80211_NAN_NDP_ATTR_QOS_MAX_LATENCY, NL80211_NAN_NDP_ATTR_ACCEPT, NL80211_NAN_NDP_ATTR_REASON, + NL80211_NAN_NDP_ATTR_LOCALLY_GENERATED, /* keep last */ __NL80211_NAN_NDP_ATTR_LAST, diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c index 580252bb001c..9d557e16aec8 100644 --- a/net/wireless/nl80211.c +++ b/net/wireless/nl80211.c @@ -19170,6 +19170,62 @@ cfg80211_nan_ndp_status(struct wireless_dev *wdev, } EXPORT_SYMBOL(cfg80211_nan_ndp_status); +void +cfg80211_nan_ndp_terminated(struct wireless_dev *wdev, + const struct cfg80211_nan_ndp_terminated_params *params, + gfp_t gfp) +{ + struct wiphy *wiphy = wdev->wiphy; + struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); + struct nlattr *ndp_attr; + struct sk_buff *msg; + void *hdr; + + if (WARN_ON_ONCE(!params->peer_nmi || !params->init_ndi || + !params->ndp_id || + wdev->iftype != NL80211_IFTYPE_NAN || + !(wiphy->nan_capa.flags & + WIPHY_NAN_FLAGS_OFFLOAD_NDP))) + return; + + trace_cfg80211_nan_ndp_terminated(wiphy, wdev, params); + + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); + if (!msg) + return; + + hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_NDP_TERMINATE); + if (!hdr) + goto nla_put_failure; + + if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) || + nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev), + NL80211_ATTR_PAD) || + nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->peer_nmi)) + goto nla_put_failure; + + ndp_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_NDP); + if (!ndp_attr) + goto nla_put_failure; + + if (nla_put_u8(msg, NL80211_NAN_NDP_ATTR_ID, params->ndp_id) || + nla_put(msg, NL80211_NAN_NDP_ATTR_INIT_NDI, ETH_ALEN, + params->init_ndi) || + nla_put_u8(msg, NL80211_NAN_NDP_ATTR_REASON, params->reason) || + (params->locally_generated && + nla_put_flag(msg, NL80211_NAN_NDP_ATTR_LOCALLY_GENERATED))) + goto nla_put_failure; + + nla_nest_end(msg, ndp_attr); + genlmsg_end(msg, hdr); + nl80211_send_nan_ndp_event(wdev, msg, gfp); + return; + +nla_put_failure: + nlmsg_free(msg); +} +EXPORT_SYMBOL(cfg80211_nan_ndp_terminated); + static bool nl80211_nan_is_sched_empty(struct cfg80211_nan_local_sched *sched) { if (!sched->n_channels) diff --git a/net/wireless/trace.h b/net/wireless/trace.h index 1b41af59d40e..71488140a9ad 100644 --- a/net/wireless/trace.h +++ b/net/wireless/trace.h @@ -4846,6 +4846,35 @@ TRACE_EVENT(cfg80211_nan_ndp_status, #undef CFG_NDP_STATUS_FMT_2 #undef CFG_NDP_STATUS_FMT_3 +TRACE_EVENT(cfg80211_nan_ndp_terminated, + TP_PROTO(struct wiphy *wiphy, struct wireless_dev *wdev, + const struct cfg80211_nan_ndp_terminated_params *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) + __field(bool, locally_generated) + ), + 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; + __entry->locally_generated = params->locally_generated; + ), + TP_printk(WIPHY_PR_FMT ", " WDEV_PR_FMT + ", peer_nmi: %pM, ndp_id: %u, init_ndi: %pM, reason: %u, locally_generated: %d", + WIPHY_PR_ARG, WDEV_PR_ARG, __entry->peer_nmi, + __entry->ndp_id, __entry->init_ndi, __entry->reason, + __entry->locally_generated) +); + TRACE_EVENT(cfg80211_incumbent_signal_notify, TP_PROTO(struct wiphy *wiphy, const struct cfg80211_chan_def *chandef, -- 2.34.1