Add NL80211_CMD_NAN_NDP_TERMINATE to let userspace tear down an established NDP session 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. Signed-off-by: Shivani Baranwal --- include/net/cfg80211.h | 22 ++++++++++++++ include/uapi/linux/nl80211.h | 7 +++++ net/wireless/nl80211.c | 58 ++++++++++++++++++++++++++++++++++++ net/wireless/rdev-ops.h | 16 ++++++++++ net/wireless/trace.h | 27 +++++++++++++++++ 5 files changed, 130 insertions(+) diff --git a/include/net/cfg80211.h b/include/net/cfg80211.h index 151555fa8404..e07e167caaf7 100644 --- a/include/net/cfg80211.h +++ b/include/net/cfg80211.h @@ -4443,6 +4443,21 @@ struct cfg80211_nan_ndp_respond { u8 transport_protocol; }; +/** + * 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 @@ -5436,6 +5451,10 @@ struct mgmt_frame_regs { * Only valid if the driver has set %WIPHY_NAN_FLAGS_OFFLOAD_NDP. Returning * success acknowledges the response command only; setup outcome is reported * asynchronously after the handshake. + * @nan_ndp_terminate: Terminate an established NDP session. + * 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 * @@ -5850,6 +5869,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 bdd52fcafe85..0303b9f22a5d 100644 --- a/include/uapi/linux/nl80211.h +++ b/include/uapi/linux/nl80211.h @@ -1512,6 +1512,12 @@ * processing; setup outcome is reported asynchronously by * %NL80211_CMD_NAN_NDP_STATUS. * Transport port and protocol may be supplied together only when accepting. + * @NL80211_CMD_NAN_NDP_TERMINATE: Terminate an established NDP. + * 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 @@ -1797,6 +1803,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 85b8181b7c68..abd17ee04c94 100644 --- a/net/wireless/nl80211.c +++ b/net/wireless/nl80211.c @@ -18919,6 +18919,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) @@ -22094,6 +22146,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 d65dd39a1bcb..8bcc999be166 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 7888a2c8d256..c7a3c0e4b565 100644 --- a/net/wireless/trace.h +++ b/net/wireless/trace.h @@ -2732,6 +2732,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