projects/linux·blob master · refs

239 lines · 5.5 KiB

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#include <net/net_trackers.h>
9
10struct netlink_ext_ack;
11
12#define PSP_DEFAULT_UDP_PORT 1000
13
14struct psphdr {
15 u8 nexthdr;
16 u8 hdrlen;
17 u8 crypt_offset;
18 u8 verfl;
19 __be32 spi;
20 __be64 iv;
21 __be64 vc[]; /* optional */
22};
23
24#define PSP_ENCAP_HLEN (sizeof(struct udphdr) + sizeof(struct psphdr))
25
26#define PSP_SPI_KEY_ID GENMASK(30, 0)
27#define PSP_SPI_KEY_PHASE BIT(31)
28
29#define PSPHDR_CRYPT_OFFSET GENMASK(5, 0)
30
31#define PSPHDR_VERFL_SAMPLE BIT(7)
32#define PSPHDR_VERFL_DROP BIT(6)
33#define PSPHDR_VERFL_VERSION GENMASK(5, 2)
34#define PSPHDR_VERFL_VIRT BIT(1)
35#define PSPHDR_VERFL_ONE BIT(0)
36
37#define PSP_HDRLEN_NOOPT ((sizeof(struct psphdr) - 8) / 8)
38
39/**
40 * struct psp_dev_config - PSP device configuration
41 * @versions: PSP versions enabled on the device
42 */
43struct psp_dev_config {
44 u32 versions;
45};
46
47/* Max number of devices that can be associated with a single PSP device.
48 * Each entry consumes ~24 bytes in the netlink dev-get response, and the
49 * response must fit in GENLMSG_DEFAULT_SIZE (~3.7KB).
50 */
51#define PSP_ASSOC_DEV_MAX 128
52
53/**
54 * struct psp_assoc_dev - wrapper for associated net_device
55 * @dev_list: list node for psp_dev::assoc_dev_list
56 * @assoc_dev: the associated net_device
57 * @dev_tracker: tracker for the net_device reference
58 */
59struct psp_assoc_dev {
60 struct list_head dev_list;
61 struct net_device *assoc_dev;
62 netdevice_tracker dev_tracker;
63};
64
65/**
66 * struct psp_dev - PSP device struct
67 * @main_netdev: original netdevice of this PSP device
68 * @assoc_dev_list: list of psp_assoc_dev entries associated with this PSP device
69 * @assoc_dev_cnt: number of entries in @assoc_dev_list
70 * @ops: driver callbacks
71 * @caps: device capabilities
72 * @drv_priv: driver priv pointer
73 * @lock: instance lock, protects all fields
74 * @refcnt: reference count for the instance
75 * @id: instance id
76 * @generation: current generation of the device key
77 * @config: current device configuration
78 * @active_assocs: list of registered associations
79 * @prev_assocs: associations which use old (but still usable)
80 * device key
81 * @stale_assocs: associations which use a rotated out key
82 *
83 * @stats: statistics maintained by the core
84 * @stats.rotations: See stats attr key-rotations
85 * @stats.stales: See stats attr stale-events
86 *
87 * @rcu: RCU head for freeing the structure
88 */
89struct psp_dev {
90 struct net_device *main_netdev;
91 struct list_head assoc_dev_list;
92 int assoc_dev_cnt;
93
94 struct psp_dev_ops *ops;
95 struct psp_dev_caps *caps;
96 void *drv_priv;
97
98 struct mutex lock;
99 refcount_t refcnt;
100
101 u32 id;
102
103 u8 generation;
104
105 struct psp_dev_config config;
106
107 struct list_head active_assocs;
108 struct list_head prev_assocs;
109 struct list_head stale_assocs;
110
111 struct {
112 unsigned long rotations;
113 unsigned long stales;
114 } stats;
115
116 struct rcu_head rcu;
117};
118
119#define PSP_GEN_VALID_MASK 0x7f
120
121/**
122 * struct psp_dev_caps - PSP device capabilities
123 */
124struct psp_dev_caps {
125 /**
126 * @versions: mask of supported PSP versions
127 * Set this field to 0 to indicate PSP is not supported at all.
128 */
129 u32 versions;
130
131 /**
132 * @assoc_drv_spc: size of driver-specific state in Tx assoc
133 * Determines the size of struct psp_assoc::drv_data
134 */
135 u32 assoc_drv_spc;
136};
137
138#define PSP_MAX_KEY 32
139
140#define PSP_HDR_SIZE 16 /* We don't support optional fields, yet */
141#define PSP_TRL_SIZE 16 /* AES-GCM/GMAC trailer size */
142
143struct psp_skb_ext {
144 __be32 spi;
145 u16 dev_id;
146 u8 generation;
147 u8 version;
148};
149
150struct psp_key_parsed {
151 __be32 spi;
152 u8 key[PSP_MAX_KEY];
153};
154
155struct psp_assoc {
156 struct psp_dev *psd;
157
158 u16 dev_id;
159 u8 generation;
160 u8 version;
161 u8 peer_tx;
162
163 u32 upgrade_seq;
164
165 struct psp_key_parsed tx;
166 struct psp_key_parsed rx;
167
168 refcount_t refcnt;
169 struct rcu_head rcu;
170 struct work_struct work;
171 struct list_head assocs_list;
172
173 u8 drv_data[] __aligned(8);
174};
175
176struct psp_dev_stats {
177 union {
178 struct {
179 u64 rx_packets;
180 u64 rx_bytes;
181 u64 rx_auth_fail;
182 u64 rx_error;
183 u64 rx_bad;
184 u64 tx_packets;
185 u64 tx_bytes;
186 u64 tx_error;
187 };
188 DECLARE_FLEX_ARRAY(u64, required);
189 };
190};
191
192/**
193 * struct psp_dev_ops - netdev driver facing PSP callbacks
194 */
195struct psp_dev_ops {
196 /**
197 * @set_config: set configuration of a PSP device
198 * Driver can inspect @psd->config for the previous configuration.
199 * Core will update @psd->config with @config on success.
200 */
201 int (*set_config)(struct psp_dev *psd, struct psp_dev_config *conf,
202 struct netlink_ext_ack *extack);
203
204 /**
205 * @key_rotate: rotate the device key
206 */
207 int (*key_rotate)(struct psp_dev *psd, struct netlink_ext_ack *extack);
208
209 /**
210 * @rx_spi_alloc: allocate an Rx SPI+key pair
211 * Allocate an Rx SPI and resulting derived key.
212 * This key should remain valid until key rotation.
213 */
214 int (*rx_spi_alloc)(struct psp_dev *psd, u32 version,
215 struct psp_key_parsed *assoc,
216 struct netlink_ext_ack *extack);
217
218 /**
219 * @tx_key_add: add a Tx key to the device
220 * Install an association in the device. Core will allocate space
221 * for the driver to use at drv_data.
222 */
223 int (*tx_key_add)(struct psp_dev *psd, struct psp_assoc *pas,
224 struct netlink_ext_ack *extack);
225 /**
226 * @tx_key_del: remove a Tx key from the device
227 * Remove an association from the device.
228 */
229 void (*tx_key_del)(struct psp_dev *psd, struct psp_assoc *pas);
230
231 /**
232 * @get_stats: get statistics from the device
233 * Stats required by the spec must be maintained and filled in.
234 * Stats must be filled in member-by-member, never memset the struct.
235 */
236 void (*get_stats)(struct psp_dev *psd, struct psp_dev_stats *stats);
237};
238
239#endif /* __NET_PSP_H */