| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | 
|---|
| 2 |  | 
|---|
| 3 | #ifndef __NET_PSP_H | 
|---|
| 4 | #define __NET_PSP_H | 
|---|
| 5 |  | 
|---|
| 6 | #include <linux/mutex.h> | 
|---|
| 7 | #include <linux/refcount.h> | 
|---|
| 8 |  | 
|---|
| 9 | struct netlink_ext_ack; | 
|---|
| 10 |  | 
|---|
| 11 | #define PSP_DEFAULT_UDP_PORT	1000 | 
|---|
| 12 |  | 
|---|
| 13 | struct psphdr { | 
|---|
| 14 | u8	nexthdr; | 
|---|
| 15 | u8	hdrlen; | 
|---|
| 16 | u8	crypt_offset; | 
|---|
| 17 | u8	verfl; | 
|---|
| 18 | __be32	spi; | 
|---|
| 19 | __be64	iv; | 
|---|
| 20 | __be64	vc[]; /* optional */ | 
|---|
| 21 | }; | 
|---|
| 22 |  | 
|---|
| 23 | #define PSP_ENCAP_HLEN (sizeof(struct udphdr) + sizeof(struct psphdr)) | 
|---|
| 24 |  | 
|---|
| 25 | #define PSP_SPI_KEY_ID		GENMASK(30, 0) | 
|---|
| 26 | #define PSP_SPI_KEY_PHASE	BIT(31) | 
|---|
| 27 |  | 
|---|
| 28 | #define PSPHDR_CRYPT_OFFSET	GENMASK(5, 0) | 
|---|
| 29 |  | 
|---|
| 30 | #define PSPHDR_VERFL_SAMPLE	BIT(7) | 
|---|
| 31 | #define PSPHDR_VERFL_DROP	BIT(6) | 
|---|
| 32 | #define PSPHDR_VERFL_VERSION	GENMASK(5, 2) | 
|---|
| 33 | #define PSPHDR_VERFL_VIRT	BIT(1) | 
|---|
| 34 | #define PSPHDR_VERFL_ONE	BIT(0) | 
|---|
| 35 |  | 
|---|
| 36 | #define PSP_HDRLEN_NOOPT	((sizeof(struct psphdr) - 8) / 8) | 
|---|
| 37 |  | 
|---|
| 38 | /** | 
|---|
| 39 | * struct psp_dev_config - PSP device configuration | 
|---|
| 40 | * @versions: PSP versions enabled on the device | 
|---|
| 41 | */ | 
|---|
| 42 | struct psp_dev_config { | 
|---|
| 43 | u32 versions; | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | /** | 
|---|
| 47 | * struct psp_dev - PSP device struct | 
|---|
| 48 | * @main_netdev: original netdevice of this PSP device | 
|---|
| 49 | * @ops:	driver callbacks | 
|---|
| 50 | * @caps:	device capabilities | 
|---|
| 51 | * @drv_priv:	driver priv pointer | 
|---|
| 52 | * @lock:	instance lock, protects all fields | 
|---|
| 53 | * @refcnt:	reference count for the instance | 
|---|
| 54 | * @id:		instance id | 
|---|
| 55 | * @generation:	current generation of the device key | 
|---|
| 56 | * @config:	current device configuration | 
|---|
| 57 | * @active_assocs:	list of registered associations | 
|---|
| 58 | * @prev_assocs:	associations which use old (but still usable) | 
|---|
| 59 | *			device key | 
|---|
| 60 | * @stale_assocs:	associations which use a rotated out key | 
|---|
| 61 | * | 
|---|
| 62 | * @rcu:	RCU head for freeing the structure | 
|---|
| 63 | */ | 
|---|
| 64 | struct psp_dev { | 
|---|
| 65 | struct net_device *main_netdev; | 
|---|
| 66 |  | 
|---|
| 67 | struct psp_dev_ops *ops; | 
|---|
| 68 | struct psp_dev_caps *caps; | 
|---|
| 69 | void *drv_priv; | 
|---|
| 70 |  | 
|---|
| 71 | struct mutex lock; | 
|---|
| 72 | refcount_t refcnt; | 
|---|
| 73 |  | 
|---|
| 74 | u32 id; | 
|---|
| 75 |  | 
|---|
| 76 | u8 generation; | 
|---|
| 77 |  | 
|---|
| 78 | struct psp_dev_config config; | 
|---|
| 79 |  | 
|---|
| 80 | struct list_head active_assocs; | 
|---|
| 81 | struct list_head prev_assocs; | 
|---|
| 82 | struct list_head stale_assocs; | 
|---|
| 83 |  | 
|---|
| 84 | struct rcu_head rcu; | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | #define PSP_GEN_VALID_MASK	0x7f | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 | * struct psp_dev_caps - PSP device capabilities | 
|---|
| 91 | */ | 
|---|
| 92 | struct psp_dev_caps { | 
|---|
| 93 | /** | 
|---|
| 94 | * @versions: mask of supported PSP versions | 
|---|
| 95 | * Set this field to 0 to indicate PSP is not supported at all. | 
|---|
| 96 | */ | 
|---|
| 97 | u32 versions; | 
|---|
| 98 |  | 
|---|
| 99 | /** | 
|---|
| 100 | * @assoc_drv_spc: size of driver-specific state in Tx assoc | 
|---|
| 101 | * Determines the size of struct psp_assoc::drv_data | 
|---|
| 102 | */ | 
|---|
| 103 | u32 assoc_drv_spc; | 
|---|
| 104 | }; | 
|---|
| 105 |  | 
|---|
| 106 | #define PSP_MAX_KEY	32 | 
|---|
| 107 |  | 
|---|
| 108 | #define PSP_HDR_SIZE	16	/* We don't support optional fields, yet */ | 
|---|
| 109 | #define PSP_TRL_SIZE	16	/* AES-GCM/GMAC trailer size */ | 
|---|
| 110 |  | 
|---|
| 111 | struct psp_skb_ext { | 
|---|
| 112 | __be32 spi; | 
|---|
| 113 | u16 dev_id; | 
|---|
| 114 | u8 generation; | 
|---|
| 115 | u8 version; | 
|---|
| 116 | }; | 
|---|
| 117 |  | 
|---|
| 118 | struct psp_key_parsed { | 
|---|
| 119 | __be32 spi; | 
|---|
| 120 | u8 key[PSP_MAX_KEY]; | 
|---|
| 121 | }; | 
|---|
| 122 |  | 
|---|
| 123 | struct psp_assoc { | 
|---|
| 124 | struct psp_dev *psd; | 
|---|
| 125 |  | 
|---|
| 126 | u16 dev_id; | 
|---|
| 127 | u8 generation; | 
|---|
| 128 | u8 version; | 
|---|
| 129 | u8 peer_tx; | 
|---|
| 130 |  | 
|---|
| 131 | u32 upgrade_seq; | 
|---|
| 132 |  | 
|---|
| 133 | struct psp_key_parsed tx; | 
|---|
| 134 | struct psp_key_parsed rx; | 
|---|
| 135 |  | 
|---|
| 136 | refcount_t refcnt; | 
|---|
| 137 | struct rcu_head rcu; | 
|---|
| 138 | struct work_struct work; | 
|---|
| 139 | struct list_head assocs_list; | 
|---|
| 140 |  | 
|---|
| 141 | u8 drv_data[] __aligned(8); | 
|---|
| 142 | }; | 
|---|
| 143 |  | 
|---|
| 144 | /** | 
|---|
| 145 | * struct psp_dev_ops - netdev driver facing PSP callbacks | 
|---|
| 146 | */ | 
|---|
| 147 | struct psp_dev_ops { | 
|---|
| 148 | /** | 
|---|
| 149 | * @set_config: set configuration of a PSP device | 
|---|
| 150 | * Driver can inspect @psd->config for the previous configuration. | 
|---|
| 151 | * Core will update @psd->config with @config on success. | 
|---|
| 152 | */ | 
|---|
| 153 | int (*set_config)(struct psp_dev *psd, struct psp_dev_config *conf, | 
|---|
| 154 | struct netlink_ext_ack *extack); | 
|---|
| 155 |  | 
|---|
| 156 | /** | 
|---|
| 157 | * @key_rotate: rotate the device key | 
|---|
| 158 | */ | 
|---|
| 159 | int (*key_rotate)(struct psp_dev *psd, struct netlink_ext_ack *extack); | 
|---|
| 160 |  | 
|---|
| 161 | /** | 
|---|
| 162 | * @rx_spi_alloc: allocate an Rx SPI+key pair | 
|---|
| 163 | * Allocate an Rx SPI and resulting derived key. | 
|---|
| 164 | * This key should remain valid until key rotation. | 
|---|
| 165 | */ | 
|---|
| 166 | int (*rx_spi_alloc)(struct psp_dev *psd, u32 version, | 
|---|
| 167 | struct psp_key_parsed *assoc, | 
|---|
| 168 | struct netlink_ext_ack *extack); | 
|---|
| 169 |  | 
|---|
| 170 | /** | 
|---|
| 171 | * @tx_key_add: add a Tx key to the device | 
|---|
| 172 | * Install an association in the device. Core will allocate space | 
|---|
| 173 | * for the driver to use at drv_data. | 
|---|
| 174 | */ | 
|---|
| 175 | int (*tx_key_add)(struct psp_dev *psd, struct psp_assoc *pas, | 
|---|
| 176 | struct netlink_ext_ack *extack); | 
|---|
| 177 | /** | 
|---|
| 178 | * @tx_key_del: remove a Tx key from the device | 
|---|
| 179 | * Remove an association from the device. | 
|---|
| 180 | */ | 
|---|
| 181 | void (*tx_key_del)(struct psp_dev *psd, struct psp_assoc *pas); | 
|---|
| 182 | }; | 
|---|
| 183 |  | 
|---|
| 184 | #endif /* __NET_PSP_H */ | 
|---|
| 185 |  | 
|---|