Use NL80211_CMD_NAN_NDP_INITIATE to notify userspace when the device receives an NDP Request from a peer. Provide the received request parameters through cfg80211_nan_ndp_request() so userspace can choose the responder NDI and accept or reject the request. Carry transport port and protocol values received from the peer in the request indication, so userspace can learn how to reach the peer service. Validate the notification parameters, trace them without PMKID bytes, and deliver the event to the NAN owner or multicast group. Signed-off-by: Shivani Baranwal --- include/net/cfg80211.h | 55 ++++++++++++++++++++ include/uapi/linux/nl80211.h | 9 +++- net/wireless/nl80211.c | 99 ++++++++++++++++++++++++++++++++++++ net/wireless/trace.h | 83 ++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+), 1 deletion(-) diff --git a/include/net/cfg80211.h b/include/net/cfg80211.h index eda9a073422b..caff65d0c801 100644 --- a/include/net/cfg80211.h +++ b/include/net/cfg80211.h @@ -4510,6 +4510,46 @@ struct cfg80211_nan_ndp_terminate { u8 reason; }; +/** + * struct cfg80211_nan_ndp_request_params - NAN NDP request event parameters + * + * Sent to userspace when the driver receives a NDP Request frame. + * + * @peer_nmi: peer NMI address + * @ndp_id: NDP ID from the received frame + * @init_ndi: initiator NDI MAC from the received frame + * @publish_id: publisher's service instance ID + * @ssi: service-specific info (may be NULL) + * @ssi_len: length of @ssi + * @cipher_suite: initiator's cipher suite ID + * @pmkid: PMKID from the peer's SCIA attribute (16 bytes), or NULL if absent + * @gtk_required: if true, initiator requires GTK protection + * @gtk_cipher: GTK cipher suite ID, included when @gtk_required is true + * @ipv6_intf_id: initiator's IPv6 link-local interface identifier, or NULL + * if absent + * @transport_info: if true, the request contained transport information + * @transport_port: transport port received from the peer when + * @transport_info is true + * @transport_protocol: IANA protocol number received from the peer when + * @transport_info is true + */ +struct cfg80211_nan_ndp_request_params { + const u8 *peer_nmi; + u8 ndp_id; + const u8 *init_ndi; + u8 publish_id; + const u8 *ssi; + u8 ssi_len; + u32 cipher_suite; + const u8 *pmkid; + bool gtk_required; + u32 gtk_cipher; + const u8 *ipv6_intf_id; + bool transport_info; + u16 transport_port; + u8 transport_protocol; +}; + /** * enum cfg80211_nan_conf_changes - indicates changed fields in NAN * configuration @@ -11674,6 +11714,21 @@ cfg80211_s1g_get_primary_sibling(struct wiphy *wiphy, } +/** + * cfg80211_nan_ndp_request - notify userspace of a received NDP request + * @wdev: the wireless device reporting the event + * @params: NDP request parameters parsed from the received frame + * @gfp: allocation flags + * + * Notify userspace that a NAN Data Path request was received from a peer. + * This starts a responder-side setup attempt; the driver reports its final + * result once, including on rejection, failure, or timeout. Only valid when + * %WIPHY_NAN_FLAGS_OFFLOAD_NDP is set. + */ +void cfg80211_nan_ndp_request(struct wireless_dev *wdev, + const struct cfg80211_nan_ndp_request_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 54249b98fcc7..5c2dbe4f1463 100644 --- a/include/uapi/linux/nl80211.h +++ b/include/uapi/linux/nl80211.h @@ -1593,6 +1593,12 @@ * Aware reason QoS_UNACCEPTABLE. * Each accepted setup attempt completes asynchronously with exactly one * %NL80211_CMD_NAN_NDP_STATUS notification, including on failure or timeout. + * This command is also used as a notification when an NDP Request is + * received from a peer. The notification carries %NL80211_ATTR_MAC and + * %NL80211_ATTR_NAN_NDP with the received parameters. Userspace must ensure + * the responder NDI exists before sending @NL80211_CMD_NAN_NDP_RESPOND. + * Each indicated request is a setup attempt and completes with exactly one + * %NL80211_CMD_NAN_NDP_STATUS notification, including on failure or timeout. * @NL80211_CMD_NAN_NDP_RESPOND: Respond to a received NDP request as * the responder. Requires %NL80211_NAN_CAPA_OFFLOAD_NDP. The NDI * interface specified in %NL80211_NAN_NDP_ATTR_RESP_NDI must already @@ -8212,7 +8218,8 @@ enum nl80211_nan_conf_attributes { * %NL80211_CMD_NAN_NDP_INITIATE and in an accepting * %NL80211_CMD_NAN_NDP_RESPOND. The driver carries these values in the NDPE * Service Info TLV (Generic Service Protocol). If the peer does not support - * NDPE, these values are not conveyed. + * NDPE, these values are not conveyed. Peer-request notifications include + * the pair when it was included by the peer. * @NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL: u8 IANA protocol number. Must be * supplied with @NL80211_NAN_NDP_ATTR_TRANSPORT_PORT. * @NL80211_NAN_NDP_ATTR_QOS_MIN_SLOTS: optional u8 Minimum Time Slots field diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c index 571432435bfa..c9b7a5b4600a 100644 --- a/net/wireless/nl80211.c +++ b/net/wireless/nl80211.c @@ -18982,6 +18982,105 @@ static int nl80211_nan_ndp_terminate(struct sk_buff *skb, return rdev_nan_ndp_terminate(rdev, wdev, ¶ms); } +static void nl80211_send_nan_ndp_event(struct wireless_dev *wdev, + struct sk_buff *msg, gfp_t gfp) +{ + struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); + + if (wdev->owner_nlportid) + genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, + wdev->owner_nlportid); + else + genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), + msg, 0, NL80211_MCGRP_NAN, gfp); +} + +void cfg80211_nan_ndp_request(struct wireless_dev *wdev, + const struct cfg80211_nan_ndp_request_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_request(wiphy, wdev, params); + + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); + if (!msg) + return; + + hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_NDP_INITIATE); + 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_PUBLISH_ID, + params->publish_id)) + goto nla_put_failure; + + if (params->ssi && params->ssi_len && + nla_put(msg, NL80211_NAN_NDP_ATTR_SSI, params->ssi_len, + params->ssi)) + goto nla_put_failure; + + if (params->cipher_suite && + nla_put_u32(msg, NL80211_NAN_NDP_ATTR_CIPHER_SUITE, + params->cipher_suite)) + goto nla_put_failure; + + if (params->pmkid && + nla_put(msg, NL80211_NAN_NDP_ATTR_PMKID, WLAN_PMKID_LEN, + params->pmkid)) + goto nla_put_failure; + + if (params->gtk_required && + (nla_put_flag(msg, NL80211_NAN_NDP_ATTR_GTK_REQUIRED) || + nla_put_u32(msg, NL80211_NAN_NDP_ATTR_GTK_CIPHER, + params->gtk_cipher))) + goto nla_put_failure; + + if (params->ipv6_intf_id && + nla_put(msg, NL80211_NAN_NDP_ATTR_IPV6_INTF_ID, + NL80211_NAN_NDP_IPV6_INTF_ID_LEN, params->ipv6_intf_id)) + goto nla_put_failure; + + if (params->transport_info && + (nla_put_u16(msg, NL80211_NAN_NDP_ATTR_TRANSPORT_PORT, + params->transport_port) || + nla_put_u8(msg, NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL, + params->transport_protocol))) + 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_request); + 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 a843b52cbcfc..be7b3162c821 100644 --- a/net/wireless/trace.h +++ b/net/wireless/trace.h @@ -4690,6 +4690,89 @@ TRACE_EVENT(cfg80211_nan_cluster_joined, __entry->new_cluster ? " [new]" : "") ); +#define CFG80211_NDP_REQUEST_FMT_1 \ + ", peer_nmi: %pM, ndp_id: %u, init_ndi: %pM, " +#define CFG80211_NDP_REQUEST_FMT_2 \ + "publish_id: %u, ssi_len: %u, ssi: %s, " +#define CFG80211_NDP_REQUEST_FMT_3 \ + "cipher_suite: %u, pmkid_present: %d, " +#define CFG80211_NDP_REQUEST_FMT_4 \ + "gtk_required: %d, gtk_cipher: %u, " +#define CFG80211_NDP_REQUEST_FMT_5 \ + "ipv6_intf_id_present: %d, ipv6_intf_id: %s" + +TRACE_EVENT(cfg80211_nan_ndp_request, + TP_PROTO(struct wiphy *wiphy, struct wireless_dev *wdev, + const struct cfg80211_nan_ndp_request_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, publish_id) + __field(u8, ssi_len) + __dynamic_array(u8, ssi, params->ssi_len) + __field(u32, cipher_suite) + __field(bool, pmkid_present) + __field(bool, gtk_required) + __field(u32, gtk_cipher) + __field(bool, transport_info) + __field(u16, transport_port) + __field(u8, transport_protocol) + __field(bool, ipv6_intf_id_present) + __array(u8, ipv6_intf_id, NL80211_NAN_NDP_IPV6_INTF_ID_LEN) + ), + 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->publish_id = params->publish_id; + __entry->ssi_len = params->ssi_len; + if (params->ssi && params->ssi_len) + memcpy(__get_dynamic_array(ssi), params->ssi, + params->ssi_len); + __entry->cipher_suite = params->cipher_suite; + __entry->pmkid_present = !!params->pmkid; + __entry->gtk_required = params->gtk_required; + __entry->gtk_cipher = params->gtk_cipher; + __entry->transport_info = params->transport_info; + __entry->transport_port = params->transport_port; + __entry->transport_protocol = params->transport_protocol; + __entry->ipv6_intf_id_present = !!params->ipv6_intf_id; + if (params->ipv6_intf_id) + memcpy(__entry->ipv6_intf_id, params->ipv6_intf_id, + NL80211_NAN_NDP_IPV6_INTF_ID_LEN); + else + memset(__entry->ipv6_intf_id, 0, + NL80211_NAN_NDP_IPV6_INTF_ID_LEN); + ), + TP_printk(WIPHY_PR_FMT ", " WDEV_PR_FMT + CFG80211_NDP_REQUEST_FMT_1 CFG80211_NDP_REQUEST_FMT_2 + CFG80211_NDP_REQUEST_FMT_3 CFG80211_NDP_REQUEST_FMT_4 + CFG80211_NDP_REQUEST_FMT_5 + ", transport_info: %d, transport_port: %u, transport_protocol: %u", + WIPHY_PR_ARG, WDEV_PR_ARG, __entry->peer_nmi, __entry->ndp_id, + __entry->init_ndi, __entry->publish_id, __entry->ssi_len, + __print_array(__get_dynamic_array(ssi), __entry->ssi_len, + sizeof(u8)), + __entry->cipher_suite, __entry->pmkid_present, + __entry->gtk_required, __entry->gtk_cipher, + __entry->ipv6_intf_id_present, + __print_array(__entry->ipv6_intf_id, + NL80211_NAN_NDP_IPV6_INTF_ID_LEN, sizeof(u8)), + __entry->transport_info, __entry->transport_port, + __entry->transport_protocol) +); +#undef CFG80211_NDP_REQUEST_FMT_1 +#undef CFG80211_NDP_REQUEST_FMT_2 +#undef CFG80211_NDP_REQUEST_FMT_3 +#undef CFG80211_NDP_REQUEST_FMT_4 +#undef CFG80211_NDP_REQUEST_FMT_5 + TRACE_EVENT(cfg80211_incumbent_signal_notify, TP_PROTO(struct wiphy *wiphy, const struct cfg80211_chan_def *chandef, -- 2.34.1