Add NL80211_CMD_NAN_NDP_RESPOND so userspace can accept or reject an incoming NDP request when the device offloads NDP management. Require a local responder NDI only when accepting; the initiator NDI belongs to the peer and is not validated as a local interface. Restrict rejection requests to the reason and optional SSI, and enforce the NAN security requirements when accepting. Pass PMK material by pointer, clear the request skb, and trace request parameters without recording key bytes. Allow the responder to provide transport port and protocol information when accepting an NDP request. Reject channel request attributes on the response command, since channel selection is requested by the initiator. Signed-off-by: Shivani Baranwal --- include/net/cfg80211.h | 55 +++++++++++ include/uapi/linux/nl80211.h | 28 +++++- net/wireless/nl80211.c | 175 +++++++++++++++++++++++++++++++++++ net/wireless/rdev-ops.h | 16 ++++ net/wireless/trace.h | 86 +++++++++++++++++ 5 files changed, 357 insertions(+), 3 deletions(-) diff --git a/include/net/cfg80211.h b/include/net/cfg80211.h index eb753c83722e..151555fa8404 100644 --- a/include/net/cfg80211.h +++ b/include/net/cfg80211.h @@ -4395,6 +4395,54 @@ struct cfg80211_nan_ndp_initiate { bool chan_forced; }; +/** + * struct cfg80211_nan_ndp_respond - NAN NDP respond parameters + * + * @peer_nmi: peer NMI address + * @ndp_id: NDP ID from the received NDP Request + * @init_ndi: initiator NDI MAC from the received NDP Request + * @resp_ndi: responder NDI MAC. The corresponding + * %NL80211_IFTYPE_NAN_DATA interface must exist when accepting. + * @accept: if true, accept the NDP; if false, reject it + * @reason: Wi-Fi Aware reason code for rejection (valid when @accept is false) + * @ssi: response service-specific info (may be NULL) + * @ssi_len: length of @ssi + * @cipher_suite: NAN cipher suite ID (must match the initiator's) + * @pmk: ND-PMK used to derive the PTK; NULL if absent. Its length depends on + * the cipher suite and is given by @pmk_len. Requires non-zero + * @cipher_suite and %NL80211_EXT_FEATURE_SECURE_NAN. + * @pmk_len: length of @pmk in bytes + * @gtk_required: if true, GTK protection is required for group-addressed data + * frames + * @gtk_cipher: GTK cipher suite ID, valid when @gtk_required is true + * @ipv6_intf_id: IPv6 link-local interface identifier, or NULL if absent + * @transport_info: if true, convey @transport_port and @transport_protocol to + * the peer in the NDPE Service Info TLV (Generic Service Protocol). These + * values are not conveyed if the peer does not support NDPE. + * @transport_port: transport port to convey when @transport_info is true + * @transport_protocol: IANA protocol number to convey when @transport_info is + * true + */ +struct cfg80211_nan_ndp_respond { + const u8 *peer_nmi; + u8 ndp_id; + const u8 *init_ndi; + const u8 *resp_ndi; + bool accept; + u8 reason; + const u8 *ssi; + u8 ssi_len; + u32 cipher_suite; + const u8 *pmk; + u8 pmk_len; + 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 @@ -5384,6 +5432,10 @@ struct mgmt_frame_regs { * specify a channel, drivers advertising NDP offload must honor it as a * channel preference. If the channel is forced and cannot be included in the * NDL, the driver must fail the request. + * @nan_ndp_respond: Respond to an incoming NDP request as the responder. + * 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. * * @set_multicast_to_unicast: configure multicast to unicast conversion for BSS * @@ -5795,6 +5847,9 @@ struct cfg80211_ops { int (*nan_ndp_initiate)(struct wiphy *wiphy, struct wireless_dev *wdev, struct cfg80211_nan_ndp_initiate *params); + int (*nan_ndp_respond)(struct wiphy *wiphy, + struct wireless_dev *wdev, + struct cfg80211_nan_ndp_respond *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 d6b601556248..bdd52fcafe85 100644 --- a/include/uapi/linux/nl80211.h +++ b/include/uapi/linux/nl80211.h @@ -1503,6 +1503,15 @@ * failure with %NL80211_CMD_NAN_NDP_STATUS. The device chooses the bandwidth. * Each accepted setup attempt completes asynchronously 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 + * exist as a %NL80211_IFTYPE_NAN_DATA interface. Carries + * %NL80211_ATTR_NAN_NDP nested with %NL80211_NAN_NDP_ATTR_* fields. + * The command reply only acknowledges that the response was accepted for + * 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_MAX: highest used command number * @__NL80211_CMD_AFTER_LAST: internal use @@ -1787,6 +1796,7 @@ enum nl80211_commands { NL80211_CMD_UPDATE_BEACON, NL80211_CMD_NAN_NDP_INITIATE, + NL80211_CMD_NAN_NDP_RESPOND, /* add new commands above here */ @@ -8071,6 +8081,8 @@ enum nl80211_nan_conf_attributes { * For locally initiated NDPs, the corresponding * %NL80211_IFTYPE_NAN_DATA interface must exist. * @NL80211_NAN_NDP_ATTR_RESP_NDI: ETH_ALEN-byte responder NDI MAC address. + * When accepting with %NL80211_CMD_NAN_NDP_RESPOND, this must identify an + * existing local %NL80211_IFTYPE_NAN_DATA interface. * @NL80211_NAN_NDP_ATTR_SSI: binary. Service-specific info carried in * the NDP setup frames. * @NL80211_NAN_NDP_ATTR_CIPHER_SUITE: u32 NAN cipher suite ID; 0 for open NDP. @@ -8091,9 +8103,11 @@ enum nl80211_nan_conf_attributes { * identifier, %NL80211_NAN_NDP_IPV6_INTF_ID_LEN bytes. The driver conveys it * in the NDPE attribute and selects NDP or NDPE based on peer capabilities. * @NL80211_NAN_NDP_ATTR_TRANSPORT_PORT: u16 transport port. Must be supplied - * with @NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL. The driver carries the pair - * in the NDPE Service Info TLV (Generic Service Protocol). If the peer does - * not support NDPE, the values are not conveyed. + * with @NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL. The pair may be used in + * %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. * @NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL: u8 IANA protocol number. Must be * supplied with @NL80211_NAN_NDP_ATTR_TRANSPORT_PORT. * @NL80211_NAN_NDP_ATTR_CHANNEL_FREQ: optional u32 channel frequency in MHz. @@ -8106,6 +8120,12 @@ enum nl80211_nan_conf_attributes { * @NL80211_NAN_NDP_ATTR_CHANNEL_FREQ. The requested channel must be included * in the NDL. If it cannot be used, for example because it is incompatible * with peer availability, setup fails. + * @NL80211_NAN_NDP_ATTR_ACCEPT: flag. If present in + * %NL80211_CMD_NAN_NDP_RESPOND, it accepts the request; if absent, it is + * rejected. + * @NL80211_NAN_NDP_ATTR_REASON: u8 Wi-Fi Aware reason code, not an IEEE 802.11 + * reason code (%NL80211_ATTR_REASON_CODE). Used for rejection with + * %NL80211_CMD_NAN_NDP_RESPOND. * @__NL80211_NAN_NDP_ATTR_LAST: Internal. * @NL80211_NAN_NDP_ATTR_MAX: Highest NDP attribute. */ @@ -8128,6 +8148,8 @@ enum nl80211_nan_ndp_attributes { NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL, NL80211_NAN_NDP_ATTR_CHANNEL_FREQ, NL80211_NAN_NDP_ATTR_CHANNEL_FORCED, + NL80211_NAN_NDP_ATTR_ACCEPT, + NL80211_NAN_NDP_ATTR_REASON, /* keep last */ __NL80211_NAN_NDP_ATTR_LAST, diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c index fb1190a60748..85b8181b7c68 100644 --- a/net/wireless/nl80211.c +++ b/net/wireless/nl80211.c @@ -826,6 +826,8 @@ nl80211_nan_ndp_policy[NL80211_NAN_NDP_ATTR_MAX + 1] = { [NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL] = { .type = NLA_U8 }, [NL80211_NAN_NDP_ATTR_CHANNEL_FREQ] = { .type = NLA_U32 }, [NL80211_NAN_NDP_ATTR_CHANNEL_FORCED] = { .type = NLA_FLAG }, + [NL80211_NAN_NDP_ATTR_ACCEPT] = { .type = NLA_FLAG }, + [NL80211_NAN_NDP_ATTR_REASON] = { .type = NLA_U8 }, }; static const struct nla_policy @@ -18751,6 +18753,172 @@ static int nl80211_nan_ndp_initiate(struct sk_buff *skb, return -ENOBUFS; } +static int nl80211_nan_ndp_respond(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_respond params = {}; + bool secure_nan; + int err; + + if (wdev->iftype != NL80211_IFTYPE_NAN) { + NL_SET_ERR_MSG(info->extack, + "NDP respond 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 respond 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_TRANSPORT_PORT] != + !!tb[NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL]) { + NL_SET_ERR_MSG(info->extack, + "Transport port and protocol must be supplied together"); + return -EINVAL; + } + + if (tb[NL80211_NAN_NDP_ATTR_CHANNEL_FREQ] || + tb[NL80211_NAN_NDP_ATTR_CHANNEL_FORCED]) { + NL_SET_ERR_MSG(info->extack, + "Channel request is only valid with NDP initiate"); + return -EINVAL; + } + + if (!tb[NL80211_NAN_NDP_ATTR_ID] || + !tb[NL80211_NAN_NDP_ATTR_INIT_NDI]) { + NL_SET_ERR_MSG(info->extack, + "NDP respond 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]); + params.accept = nla_get_flag(tb[NL80211_NAN_NDP_ATTR_ACCEPT]); + + if (params.accept) { + if (tb[NL80211_NAN_NDP_ATTR_REASON]) { + NL_SET_ERR_MSG_ATTR(info->extack, + tb[NL80211_NAN_NDP_ATTR_REASON], + "REASON is only valid when rejecting"); + return -EINVAL; + } + + if (!tb[NL80211_NAN_NDP_ATTR_RESP_NDI]) { + NL_SET_ERR_MSG(info->extack, + "NDP accept requires RESP_NDI"); + return -EINVAL; + } + params.resp_ndi = nla_data(tb[NL80211_NAN_NDP_ATTR_RESP_NDI]); + if (!nl80211_nan_ndi_exists(rdev, + params.resp_ndi)) { + NL_SET_ERR_MSG_ATTR(info->extack, + tb[NL80211_NAN_NDP_ATTR_RESP_NDI], + "RESP_NDI must identify an existing NAN Data interface"); + return -ENODEV; + } + + if (tb[NL80211_NAN_NDP_ATTR_TRANSPORT_PORT]) { + params.transport_info = true; + params.transport_port = + nla_get_u16(tb[NL80211_NAN_NDP_ATTR_TRANSPORT_PORT]); + params.transport_protocol = + nla_get_u8(tb[NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL]); + } + } else { + if (tb[NL80211_NAN_NDP_ATTR_RESP_NDI] || + tb[NL80211_NAN_NDP_ATTR_CIPHER_SUITE] || + tb[NL80211_NAN_NDP_ATTR_PMK] || + tb[NL80211_NAN_NDP_ATTR_GTK_REQUIRED] || + tb[NL80211_NAN_NDP_ATTR_GTK_CIPHER] || + tb[NL80211_NAN_NDP_ATTR_IPV6_INTF_ID] || + tb[NL80211_NAN_NDP_ATTR_TRANSPORT_PORT] || + tb[NL80211_NAN_NDP_ATTR_TRANSPORT_PROTOCOL]) { + NL_SET_ERR_MSG(info->extack, + "RESP_NDI, cipher, PMK, GTK, IPv6 IID, transport info only valid when accepting"); + return -EINVAL; + } + + if (tb[NL80211_NAN_NDP_ATTR_REASON]) + params.reason = + nla_get_u8(tb[NL80211_NAN_NDP_ATTR_REASON]); + } + + if (tb[NL80211_NAN_NDP_ATTR_SSI]) { + params.ssi = nla_data(tb[NL80211_NAN_NDP_ATTR_SSI]); + params.ssi_len = nla_len(tb[NL80211_NAN_NDP_ATTR_SSI]); + } + + if (tb[NL80211_NAN_NDP_ATTR_CIPHER_SUITE]) + params.cipher_suite = + nla_get_u32(tb[NL80211_NAN_NDP_ATTR_CIPHER_SUITE]); + + secure_nan = wiphy_ext_feature_isset(&rdev->wiphy, + NL80211_EXT_FEATURE_SECURE_NAN); + if (params.cipher_suite && !secure_nan) { + NL_SET_ERR_MSG_ATTR(info->extack, + tb[NL80211_NAN_NDP_ATTR_CIPHER_SUITE], + "NDP cipher suite requires SECURE_NAN"); + return -EOPNOTSUPP; + } + + if ((tb[NL80211_NAN_NDP_ATTR_PMK] || + tb[NL80211_NAN_NDP_ATTR_GTK_REQUIRED]) && + (!secure_nan || !params.cipher_suite)) { + NL_SET_ERR_MSG(info->extack, + "PMK and GTK require SECURE_NAN and a non-zero cipher suite"); + return -EINVAL; + } + + if (tb[NL80211_NAN_NDP_ATTR_PMK]) { + params.pmk = nla_data(tb[NL80211_NAN_NDP_ATTR_PMK]); + params.pmk_len = nla_len(tb[NL80211_NAN_NDP_ATTR_PMK]); + } + + if (tb[NL80211_NAN_NDP_ATTR_GTK_REQUIRED]) { + if (!tb[NL80211_NAN_NDP_ATTR_GTK_CIPHER]) { + NL_SET_ERR_MSG_ATTR(info->extack, + tb[NL80211_NAN_NDP_ATTR_GTK_REQUIRED], + "GTK_CIPHER is required when GTK_REQUIRED is set"); + return -EINVAL; + } + + params.gtk_required = true; + params.gtk_cipher = + nla_get_u32(tb[NL80211_NAN_NDP_ATTR_GTK_CIPHER]); + } else if (tb[NL80211_NAN_NDP_ATTR_GTK_CIPHER]) { + NL_SET_ERR_MSG_ATTR(info->extack, + tb[NL80211_NAN_NDP_ATTR_GTK_CIPHER], + "GTK_CIPHER requires GTK_REQUIRED"); + return -EINVAL; + } + + if (tb[NL80211_NAN_NDP_ATTR_IPV6_INTF_ID]) + params.ipv6_intf_id = + nla_data(tb[NL80211_NAN_NDP_ATTR_IPV6_INTF_ID]); + + return rdev_nan_ndp_respond(rdev, wdev, ¶ms); +} + static bool nl80211_nan_is_sched_empty(struct cfg80211_nan_local_sched *sched) { if (!sched->n_channels) @@ -21919,6 +22087,13 @@ 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_RESPOND, + .doit = nl80211_nan_ndp_respond, + .flags = GENL_ADMIN_PERM, + .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP | + NL80211_FLAG_CLEAR_SKB), + }, }; static struct genl_family nl80211_fam __ro_after_init = { diff --git a/net/wireless/rdev-ops.h b/net/wireless/rdev-ops.h index df8ec338e59d..d65dd39a1bcb 100644 --- a/net/wireless/rdev-ops.h +++ b/net/wireless/rdev-ops.h @@ -1205,6 +1205,22 @@ rdev_nan_ndp_initiate(struct cfg80211_registered_device *rdev, return ret; } +static inline int +rdev_nan_ndp_respond(struct cfg80211_registered_device *rdev, + struct wireless_dev *wdev, + struct cfg80211_nan_ndp_respond *params) +{ + int ret; + + if (!rdev->ops->nan_ndp_respond) + return -EOPNOTSUPP; + + trace_rdev_nan_ndp_respond(&rdev->wiphy, wdev, params); + ret = rdev->ops->nan_ndp_respond(&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 131fb5657004..7888a2c8d256 100644 --- a/net/wireless/trace.h +++ b/net/wireless/trace.h @@ -2646,6 +2646,92 @@ TRACE_EVENT(rdev_nan_ndp_initiate, #undef RDEV_NAN_NDP_INITIATE_FMT_4 #undef RDEV_NAN_NDP_INITIATE_FMT_5 +#define NDP_RESP_FMT_1 \ + ", peer_nmi: %pM, ndp_id: %u, init_ndi: %pM, " +#define NDP_RESP_FMT_2 \ + "resp_ndi: %pM, accept: %d, reason: %u, ssi_len: %u, ssi: %s, " +#define NDP_RESP_FMT_3 \ + "cipher_suite: %u, pmk_len: %u, gtk_required: %d, gtk_cipher: %u, " +#define NDP_RESP_FMT_4 \ + "ipv6_intf_id_present: %d, ipv6_intf_id: %s" + +TRACE_EVENT(rdev_nan_ndp_respond, + TP_PROTO(struct wiphy *wiphy, struct wireless_dev *wdev, + struct cfg80211_nan_ndp_respond *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) + __array(u8, resp_ndi, ETH_ALEN) + __field(bool, accept) + __field(u8, reason) + __field(u8, ssi_len) + __dynamic_array(u8, ssi, params->ssi_len) + __field(u32, cipher_suite) + __field(u8, pmk_len) + __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); + if (params->resp_ndi) + memcpy(__entry->resp_ndi, params->resp_ndi, ETH_ALEN); + else + memset(__entry->resp_ndi, 0, ETH_ALEN); + __entry->accept = params->accept; + __entry->reason = params->reason; + __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->pmk_len = params->pmk ? params->pmk_len : 0; + __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 + NDP_RESP_FMT_1 NDP_RESP_FMT_2 NDP_RESP_FMT_3 NDP_RESP_FMT_4 + ", 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->resp_ndi, __entry->accept, + __entry->reason, __entry->ssi_len, + __print_array(__get_dynamic_array(ssi), __entry->ssi_len, + sizeof(u8)), + __entry->cipher_suite, __entry->pmk_len, + __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 NDP_RESP_FMT_1 +#undef NDP_RESP_FMT_2 +#undef NDP_RESP_FMT_3 +#undef NDP_RESP_FMT_4 + TRACE_EVENT(rdev_set_mac_acl, TP_PROTO(struct wiphy *wiphy, struct net_device *netdev, struct cfg80211_acl_data *params), -- 2.34.1