1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * MACsec netdev header, used for h/w accelerated implementations.
5 * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
10 #include <linux/u64_stats_sync.h>
11 #include <linux/if_vlan.h>
12 #include <uapi/linux/if_link.h>
13 #include <uapi/linux/if_macsec.h>
15 #define MACSEC_DEFAULT_PN_LEN 4
16 #define MACSEC_XPN_PN_LEN 8
18 #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
20 #define MACSEC_SCI_LEN 8
21 #define MACSEC_PORT_ES (htons(0x0001))
23 #define MACSEC_TCI_VERSION 0x80
24 #define MACSEC_TCI_ES 0x40 /* end station */
25 #define MACSEC_TCI_SC 0x20 /* SCI present */
26 #define MACSEC_TCI_SCB 0x10 /* epon */
27 #define MACSEC_TCI_E 0x08 /* encryption */
28 #define MACSEC_TCI_C 0x04 /* changed text */
29 #define MACSEC_AN_MASK 0x03 /* association number */
30 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
32 #define MACSEC_DEFAULT_ICV_LEN 16
34 typedef u64 __bitwise sci_t;
35 typedef u32 __bitwise ssci_t;
44 u8 bytes[MACSEC_SALT_LEN];
49 #if defined(__LITTLE_ENDIAN_BITFIELD)
52 #elif defined(__BIG_ENDIAN_BITFIELD)
56 #error "Please fix <asm/byteorder.h>"
63 * struct macsec_key - SA key
64 * @id: user-provided key identifier
65 * @tfm: crypto struct, key storage
66 * @salt: salt used to generate IV in XPN cipher suites
69 u8 id[MACSEC_KEYID_LEN];
70 struct crypto_aead *tfm;
74 struct macsec_rx_sc_stats {
75 __u64 InOctetsValidated;
76 __u64 InOctetsDecrypted;
77 __u64 InPktsUnchecked;
83 __u64 InPktsNotUsingSA;
87 struct macsec_rx_sa_stats {
91 __u32 InPktsNotUsingSA;
95 struct macsec_tx_sa_stats {
96 __u32 OutPktsProtected;
97 __u32 OutPktsEncrypted;
100 struct macsec_tx_sc_stats {
101 __u64 OutPktsProtected;
102 __u64 OutPktsEncrypted;
103 __u64 OutOctetsProtected;
104 __u64 OutOctetsEncrypted;
107 struct macsec_dev_stats {
108 __u64 OutPktsUntagged;
109 __u64 InPktsUntagged;
110 __u64 OutPktsTooLong;
113 __u64 InPktsUnknownSCI;
119 * struct macsec_rx_sa - receive secure association
121 * @next_pn: packet number expected for the next packet
122 * @lock: protects next_pn manipulations
123 * @key: key structure
124 * @ssci: short secure channel identifier
125 * @stats: per-SA stats
127 struct macsec_rx_sa {
128 struct macsec_key key;
137 struct macsec_rx_sa_stats __percpu *stats;
138 struct macsec_rx_sc *sc;
142 struct pcpu_rx_sc_stats {
143 struct macsec_rx_sc_stats stats;
144 struct u64_stats_sync syncp;
147 struct pcpu_tx_sc_stats {
148 struct macsec_tx_sc_stats stats;
149 struct u64_stats_sync syncp;
153 * struct macsec_rx_sc - receive secure channel
154 * @sci: secure channel identifier for this SC
155 * @active: channel is active
156 * @sa: array of secure associations
157 * @stats: per-SC stats
159 struct macsec_rx_sc {
160 struct macsec_rx_sc __rcu *next;
163 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
164 struct pcpu_rx_sc_stats __percpu *stats;
166 struct rcu_head rcu_head;
170 * struct macsec_tx_sa - transmit secure association
172 * @next_pn: packet number to use for the next packet
173 * @lock: protects next_pn manipulations
174 * @key: key structure
175 * @ssci: short secure channel identifier
176 * @stats: per-SA stats
178 struct macsec_tx_sa {
179 struct macsec_key key;
188 struct macsec_tx_sa_stats __percpu *stats;
193 * struct macsec_tx_sc - transmit secure channel
195 * @encoding_sa: association number of the SA currently in use
196 * @encrypt: encrypt packets on transmit, or authenticate only
197 * @send_sci: always include the SCI in the SecTAG
199 * @scb: single copy broadcast flag
200 * @sa: array of secure associations
201 * @stats: stats for this TXSC
202 * @md_dst: MACsec offload metadata dst
204 struct macsec_tx_sc {
211 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
212 struct pcpu_tx_sc_stats __percpu *stats;
213 struct metadata_dst *md_dst;
217 * struct macsec_secy - MACsec Security Entity
218 * @netdev: netdevice for this SecY
219 * @n_rx_sc: number of receive secure channels configured on this SecY
220 * @sci: secure channel identifier used for tx
221 * @key_len: length of keys used by the cipher suite
222 * @icv_len: length of ICV used by the cipher suite
223 * @validate_frames: validation mode
224 * @xpn: enable XPN for this SecY
225 * @operational: MAC_Operational flag
226 * @protect_frames: enable protection for this SecY
227 * @replay_protect: enable packet number checks on receive
228 * @replay_window: size of the replay window
229 * @tx_sc: transmit secure channel
230 * @rx_sc: linked list of receive secure channels
233 struct net_device *netdev;
234 unsigned int n_rx_sc;
238 enum macsec_validation_type validate_frames;
244 struct macsec_tx_sc tx_sc;
245 struct macsec_rx_sc __rcu *rx_sc;
249 * struct macsec_context - MACsec context for hardware offloading
251 struct macsec_context {
253 struct net_device *netdev;
254 struct phy_device *phydev;
256 enum macsec_offload offload;
258 struct macsec_secy *secy;
259 struct macsec_rx_sc *rx_sc;
261 unsigned char assoc_num;
262 u8 key[MACSEC_MAX_KEY_LEN];
264 struct macsec_rx_sa *rx_sa;
265 struct macsec_tx_sa *tx_sa;
269 struct macsec_tx_sc_stats *tx_sc_stats;
270 struct macsec_tx_sa_stats *tx_sa_stats;
271 struct macsec_rx_sc_stats *rx_sc_stats;
272 struct macsec_rx_sa_stats *rx_sa_stats;
273 struct macsec_dev_stats *dev_stats;
278 * struct macsec_ops - MACsec offloading operations
282 int (*mdo_dev_open)(struct macsec_context *ctx);
283 int (*mdo_dev_stop)(struct macsec_context *ctx);
285 int (*mdo_add_secy)(struct macsec_context *ctx);
286 int (*mdo_upd_secy)(struct macsec_context *ctx);
287 int (*mdo_del_secy)(struct macsec_context *ctx);
288 /* Security channels */
289 int (*mdo_add_rxsc)(struct macsec_context *ctx);
290 int (*mdo_upd_rxsc)(struct macsec_context *ctx);
291 int (*mdo_del_rxsc)(struct macsec_context *ctx);
292 /* Security associations */
293 int (*mdo_add_rxsa)(struct macsec_context *ctx);
294 int (*mdo_upd_rxsa)(struct macsec_context *ctx);
295 int (*mdo_del_rxsa)(struct macsec_context *ctx);
296 int (*mdo_add_txsa)(struct macsec_context *ctx);
297 int (*mdo_upd_txsa)(struct macsec_context *ctx);
298 int (*mdo_del_txsa)(struct macsec_context *ctx);
300 int (*mdo_get_dev_stats)(struct macsec_context *ctx);
301 int (*mdo_get_tx_sc_stats)(struct macsec_context *ctx);
302 int (*mdo_get_tx_sa_stats)(struct macsec_context *ctx);
303 int (*mdo_get_rx_sc_stats)(struct macsec_context *ctx);
304 int (*mdo_get_rx_sa_stats)(struct macsec_context *ctx);
307 void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa);
308 static inline bool macsec_send_sci(const struct macsec_secy *secy)
310 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
312 return tx_sc->send_sci ||
313 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
315 struct net_device *macsec_get_real_dev(const struct net_device *dev);
316 bool macsec_netdev_is_offloaded(struct net_device *dev);
318 static inline void *macsec_netdev_priv(const struct net_device *dev)
320 #if IS_ENABLED(CONFIG_VLAN_8021Q)
321 if (is_vlan_dev(dev))
322 return netdev_priv(vlan_dev_priv(dev)->real_dev);
324 return netdev_priv(dev);
327 #endif /* _NET_MACSEC_H_ */