Add support for negotiating the PSP capabilities:
- Introduce VIRTCHNL2_OP_PSP_GET_CAPS (565), enum virtchnl2_psp_caps
  and struct virtchnl2_psp_get_caps (64 bytes, sent from the stack).
- Introduce the common PSP key definitions:
  enum virtchnl2_psp_key_type, VIRTCHNL2_PSP_KEY_SIZE_V0/V1,
  enum virtchnl2_psp_domain_type and struct virtchnl2_psp_key_domain.
- Implement idpf_send_get_psp_caps(), which requests all PSP
  capabilities, and call it from idpf_psp_negotiate_caps(). PSP stays
  disabled unless PSP version 0 keys are granted. The granted
  capabilities are stored in adapter->psp.psp_caps.

Signed-off-by: Stan Iliev <[email protected]>
---
 drivers/net/ethernet/intel/idpf/idpf_psp.c    | 15 ++++-
 .../net/ethernet/intel/idpf/idpf_psp_def.h    |  3 +
 .../ethernet/intel/idpf/idpf_psp_virtchnl.c   | 66 +++++++++++++++++++
 .../ethernet/intel/idpf/idpf_psp_virtchnl.h   |  2 +
 include/linux/net/intel/virtchnl2.h           | 65 ++++++++++++++++++
 5 files changed, 150 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp.c 
b/drivers/net/ethernet/intel/idpf/idpf_psp.c
index c4f46f7337ef..252ddb773cf5 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_psp.c
+++ b/drivers/net/ethernet/intel/idpf/idpf_psp.c
@@ -18,8 +18,9 @@
 static int idpf_psp_negotiate_caps(struct idpf_adapter *adapter)
 {
        struct virtchnl2_crypto_get_caps crypto_caps = { };
+       struct virtchnl2_psp_get_caps psp_caps = { };
        struct device *dev = &adapter->pdev->dev;
-       u32 sadb_entries;
+       u32 caps, sadb_entries;
        int err;
 
        err = idpf_send_get_crypto_caps(adapter, &crypto_caps);
@@ -51,6 +52,18 @@ static int idpf_psp_negotiate_caps(struct idpf_adapter 
*adapter)
                return -EOPNOTSUPP;
        }
 
+       err = idpf_send_get_psp_caps(adapter, &psp_caps);
+       if (err)
+               return err;
+
+       /* The driver only allocates and inserts PSP version 0 keys */
+       caps = le32_to_cpu(psp_caps.psp_caps);
+       if (!(caps & VIRTCHNL2_CAP_PSP_PROTO_V0)) {
+               dev_notice(dev, "PSP version 0 keys not granted by CP\n");
+               return -EOPNOTSUPP;
+       }
+
+       adapter->psp.psp_caps = caps;
        adapter->psp.sadb_entries = sadb_entries;
        adapter->psp.saidx_width = crypto_caps.saidx_width;
 
diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp_def.h 
b/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
index 3a9e40284106..0829d0bbd92b 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
+++ b/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
@@ -17,6 +17,8 @@
  *          idpf_is_psp_ena()
  * @sadb_entries: SADB size granted by VIRTCHNL2_OP_CRYPTO_GET_CAPS
  * @saidx_width: SA index width granted by VIRTCHNL2_OP_CRYPTO_GET_CAPS
+ * @psp_caps: PSP capabilities granted by VIRTCHNL2_OP_PSP_GET_CAPS, see
+ *           enum virtchnl2_psp_caps
  * @psp_dev: the PSP device, see idpf_psp_register()
  * @caps: capabilities of @psp_dev
  */
@@ -24,6 +26,7 @@ struct idpf_adapter_psp {
        bool enabled;
        u32 sadb_entries;
        u8 saidx_width;
+       u32 psp_caps;
        struct psp_dev *psp_dev;
        struct psp_dev_caps caps;
 };
diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp_virtchnl.c 
b/drivers/net/ethernet/intel/idpf/idpf_psp_virtchnl.c
index 65f7a9f63f90..ba114324d818 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_psp_virtchnl.c
+++ b/drivers/net/ethernet/intel/idpf/idpf_psp_virtchnl.c
@@ -69,3 +69,69 @@ int idpf_send_get_crypto_caps(struct idpf_adapter *adapter,
        libie_ctlq_release_rx_buf(&xn_params.recv_mem);
        return err;
 }
+
+/**
+ * idpf_print_psp_caps - Print values inside virtchnl2_psp_get_caps
+ * @adapter: Driver specific private structure
+ * @caps: Pointer to virtchnl2_psp_get_caps structure
+ */
+static void idpf_print_psp_caps(struct idpf_adapter *adapter,
+                               const struct virtchnl2_psp_get_caps *caps)
+{
+       u32 psp_caps = le32_to_cpu(caps->psp_caps);
+
+       dev_dbg(&adapter->pdev->dev,
+               "virtchnl2_psp_get_caps: psp_caps=0x%08x (PROTO_V0=%u, 
PROTO_V1=%u, REQUEST_ROTATION=%u, DOMAIN=%u)\n",
+               psp_caps,
+               !!(psp_caps & VIRTCHNL2_CAP_PSP_PROTO_V0),
+               !!(psp_caps & VIRTCHNL2_CAP_PSP_PROTO_V1),
+               !!(psp_caps & VIRTCHNL2_CAP_PSP_REQUEST_ROTATION),
+               !!(psp_caps & VIRTCHNL2_CAP_PSP_DOMAIN));
+}
+
+/**
+ * idpf_send_get_psp_caps - Send VIRTCHNL2_OP_PSP_GET_CAPS request to CP
+ * @adapter: Driver specific private structure
+ * @caps: Output buffer for virtchnl2_psp_get_caps
+ *
+ * Request all PSP capabilities, PSP capabilities that are not requested are
+ * not available.
+ *
+ * Return: 0 on success, negative on failure.
+ */
+int idpf_send_get_psp_caps(struct idpf_adapter *adapter,
+                          struct virtchnl2_psp_get_caps *caps)
+{
+       struct libie_ctlq_xn_send_params xn_params = {
+               .chnl_opcode    = VIRTCHNL2_OP_PSP_GET_CAPS,
+               .timeout_ms     = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC,
+       };
+       struct virtchnl2_psp_get_caps send_caps = {
+               .psp_caps = cpu_to_le32(VIRTCHNL2_CAP_PSP_PROTO_V0 |
+                                       VIRTCHNL2_CAP_PSP_PROTO_V1 |
+                                       VIRTCHNL2_CAP_PSP_REQUEST_ROTATION |
+                                       VIRTCHNL2_CAP_PSP_DOMAIN),
+       };
+       int err;
+
+       err = idpf_send_mb_msg_stack(adapter, &xn_params, &send_caps);
+       if (err) {
+               dev_err(&adapter->pdev->dev, "Failed to get PSP caps: %d\n",
+                       err);
+               return err;
+       }
+
+       if (xn_params.recv_mem.iov_len < sizeof(*caps)) {
+               dev_err(&adapter->pdev->dev, "Invalid PSP caps reply size: 
%zu\n",
+                       xn_params.recv_mem.iov_len);
+               err = -EIO;
+               goto release_rx;
+       }
+
+       memcpy(caps, xn_params.recv_mem.iov_base, sizeof(*caps));
+       idpf_print_psp_caps(adapter, caps);
+
+release_rx:
+       libie_ctlq_release_rx_buf(&xn_params.recv_mem);
+       return err;
+}
diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp_virtchnl.h 
b/drivers/net/ethernet/intel/idpf/idpf_psp_virtchnl.h
index cf82ac9182ef..b1c64575b417 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_psp_virtchnl.h
+++ b/drivers/net/ethernet/intel/idpf/idpf_psp_virtchnl.h
@@ -9,5 +9,7 @@
 
 int idpf_send_get_crypto_caps(struct idpf_adapter *adapter,
                              struct virtchnl2_crypto_get_caps *caps);
+int idpf_send_get_psp_caps(struct idpf_adapter *adapter,
+                          struct virtchnl2_psp_get_caps *caps);
 
 #endif /* !_IDPF_PSP_VIRTCHNL_H_ */
diff --git a/include/linux/net/intel/virtchnl2.h 
b/include/linux/net/intel/virtchnl2.h
index 59c379afcb71..45cdfc0651e3 100644
--- a/include/linux/net/intel/virtchnl2.h
+++ b/include/linux/net/intel/virtchnl2.h
@@ -86,6 +86,7 @@ enum virtchnl2_op {
        VIRTCHNL2_OP_DEL_FLOW_RULE                      = 553,
        /* Crypto/PSP opcodes */
        VIRTCHNL2_OP_CRYPTO_GET_CAPS                    = 564,
+       VIRTCHNL2_OP_PSP_GET_CAPS                       = 565,
 };
 
 /**
@@ -1843,4 +1844,68 @@ struct virtchnl2_crypto_get_caps {
 };
 VIRTCHNL2_CHECK_STRUCT_LEN(256, virtchnl2_crypto_get_caps);
 
+/* PSP capability flags */
+enum virtchnl2_psp_caps {
+       /* PSP capability 0 is reserved */
+       VIRTCHNL2_CAP_PSP_PROTO_V0                      = BIT(1),
+       VIRTCHNL2_CAP_PSP_PROTO_V1                      = BIT(2),
+       VIRTCHNL2_CAP_PSP_REQUEST_ROTATION              = BIT(3),
+       /* If not granted, the domain field of PSP requests is ignored */
+       VIRTCHNL2_CAP_PSP_DOMAIN                        = BIT(4),
+       /* PSP capabilities 16 and above are reserved for OEM */
+};
+
+/**
+ * struct virtchnl2_psp_get_caps - PSP global capabilities
+ * @psp_caps: See enum virtchnl2_psp_caps
+ * @padding: Padding to 64 bytes
+ *
+ * Request and response of VIRTCHNL2_OP_PSP_GET_CAPS. PSP capabilities are set
+ * per function: a capability that is not requested is not available.
+ */
+struct virtchnl2_psp_get_caps {
+       __le32 psp_caps;
+       u8 padding[60];
+};
+VIRTCHNL2_CHECK_STRUCT_LEN(64, virtchnl2_psp_get_caps);
+
+enum virtchnl2_psp_key_type {
+       VIRTCHNL2_PSP_KEY_TYPE_V0                       = 1,
+       VIRTCHNL2_PSP_KEY_TYPE_V1                       = 2,
+};
+
+#define VIRTCHNL2_PSP_KEY_SIZE_V0                      16
+#define VIRTCHNL2_PSP_KEY_SIZE_V1                      32
+
+/* PSP key domain types. Types VIRTCHNL2_PSP_DOMAIN_RESERVED_START to
+ * VIRTCHNL2_PSP_DOMAIN_RESERVED_END are reserved for future versions of the
+ * API, types VIRTCHNL2_PSP_DOMAIN_OEM_START to VIRTCHNL2_PSP_DOMAIN_OEM_END
+ * are OEM specific.
+ */
+enum virtchnl2_psp_domain_type {
+       VIRTCHNL2_PSP_DOMAIN_NONE                       = 0,
+       VIRTCHNL2_PSP_DOMAIN_RESERVED_START             = 1,
+       VIRTCHNL2_PSP_DOMAIN_RESERVED_END               = 0x7fff,
+       VIRTCHNL2_PSP_DOMAIN_OEM_START                  = 0x8000,
+       VIRTCHNL2_PSP_DOMAIN_OEM_END                    = 0xffff,
+};
+
+/**
+ * struct virtchnl2_psp_key_domain - PSP key domain
+ * @type: See enum virtchnl2_psp_domain_type
+ * @pad: Reserved/padding
+ * @value: Security context used as part of the PSP SPI binding, must be 0
+ *        if @type is VIRTCHNL2_PSP_DOMAIN_NONE
+ *
+ * Key domains prevent steering attacks: PSP does not authenticate the IP
+ * header, so a compromised VM changing the destination IP address must not
+ * be able to decrypt the packets of another VM on the same host.
+ */
+struct virtchnl2_psp_key_domain {
+       __le16 type;
+       u8 pad[6];
+       __le64 value;
+};
+VIRTCHNL2_CHECK_STRUCT_LEN(16, virtchnl2_psp_key_domain);
+
 #endif /* _VIRTCHNL_2_H_ */
-- 
2.56.0.679.gc527c65306-goog

Reply via email to