macsec.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * MACsec netdev header, used for h/w accelerated implementations.
  4. *
  5. * Copyright (c) 2015 Sabrina Dubroca <[email protected]>
  6. */
  7. #ifndef _NET_MACSEC_H_
  8. #define _NET_MACSEC_H_
  9. #include <linux/u64_stats_sync.h>
  10. #include <uapi/linux/if_link.h>
  11. #include <uapi/linux/if_macsec.h>
  12. #define MACSEC_DEFAULT_PN_LEN 4
  13. #define MACSEC_XPN_PN_LEN 8
  14. #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
  15. #define MACSEC_SCI_LEN 8
  16. #define MACSEC_PORT_ES (htons(0x0001))
  17. #define MACSEC_TCI_VERSION 0x80
  18. #define MACSEC_TCI_ES 0x40 /* end station */
  19. #define MACSEC_TCI_SC 0x20 /* SCI present */
  20. #define MACSEC_TCI_SCB 0x10 /* epon */
  21. #define MACSEC_TCI_E 0x08 /* encryption */
  22. #define MACSEC_TCI_C 0x04 /* changed text */
  23. #define MACSEC_AN_MASK 0x03 /* association number */
  24. #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
  25. #define MACSEC_DEFAULT_ICV_LEN 16
  26. typedef u64 __bitwise sci_t;
  27. typedef u32 __bitwise ssci_t;
  28. struct metadata_dst;
  29. typedef union salt {
  30. struct {
  31. u32 ssci;
  32. u64 pn;
  33. } __packed;
  34. u8 bytes[MACSEC_SALT_LEN];
  35. } __packed salt_t;
  36. typedef union pn {
  37. struct {
  38. #if defined(__LITTLE_ENDIAN_BITFIELD)
  39. u32 lower;
  40. u32 upper;
  41. #elif defined(__BIG_ENDIAN_BITFIELD)
  42. u32 upper;
  43. u32 lower;
  44. #else
  45. #error "Please fix <asm/byteorder.h>"
  46. #endif
  47. };
  48. u64 full64;
  49. } pn_t;
  50. /**
  51. * struct macsec_key - SA key
  52. * @id: user-provided key identifier
  53. * @tfm: crypto struct, key storage
  54. * @salt: salt used to generate IV in XPN cipher suites
  55. */
  56. struct macsec_key {
  57. u8 id[MACSEC_KEYID_LEN];
  58. struct crypto_aead *tfm;
  59. salt_t salt;
  60. };
  61. struct macsec_rx_sc_stats {
  62. __u64 InOctetsValidated;
  63. __u64 InOctetsDecrypted;
  64. __u64 InPktsUnchecked;
  65. __u64 InPktsDelayed;
  66. __u64 InPktsOK;
  67. __u64 InPktsInvalid;
  68. __u64 InPktsLate;
  69. __u64 InPktsNotValid;
  70. __u64 InPktsNotUsingSA;
  71. __u64 InPktsUnusedSA;
  72. };
  73. struct macsec_rx_sa_stats {
  74. __u32 InPktsOK;
  75. __u32 InPktsInvalid;
  76. __u32 InPktsNotValid;
  77. __u32 InPktsNotUsingSA;
  78. __u32 InPktsUnusedSA;
  79. };
  80. struct macsec_tx_sa_stats {
  81. __u32 OutPktsProtected;
  82. __u32 OutPktsEncrypted;
  83. };
  84. struct macsec_tx_sc_stats {
  85. __u64 OutPktsProtected;
  86. __u64 OutPktsEncrypted;
  87. __u64 OutOctetsProtected;
  88. __u64 OutOctetsEncrypted;
  89. };
  90. struct macsec_dev_stats {
  91. __u64 OutPktsUntagged;
  92. __u64 InPktsUntagged;
  93. __u64 OutPktsTooLong;
  94. __u64 InPktsNoTag;
  95. __u64 InPktsBadTag;
  96. __u64 InPktsUnknownSCI;
  97. __u64 InPktsNoSCI;
  98. __u64 InPktsOverrun;
  99. };
  100. /**
  101. * struct macsec_rx_sa - receive secure association
  102. * @active:
  103. * @next_pn: packet number expected for the next packet
  104. * @lock: protects next_pn manipulations
  105. * @key: key structure
  106. * @ssci: short secure channel identifier
  107. * @stats: per-SA stats
  108. */
  109. struct macsec_rx_sa {
  110. struct macsec_key key;
  111. ssci_t ssci;
  112. spinlock_t lock;
  113. union {
  114. pn_t next_pn_halves;
  115. u64 next_pn;
  116. };
  117. refcount_t refcnt;
  118. bool active;
  119. struct macsec_rx_sa_stats __percpu *stats;
  120. struct macsec_rx_sc *sc;
  121. struct rcu_head rcu;
  122. };
  123. struct pcpu_rx_sc_stats {
  124. struct macsec_rx_sc_stats stats;
  125. struct u64_stats_sync syncp;
  126. };
  127. struct pcpu_tx_sc_stats {
  128. struct macsec_tx_sc_stats stats;
  129. struct u64_stats_sync syncp;
  130. };
  131. /**
  132. * struct macsec_rx_sc - receive secure channel
  133. * @sci: secure channel identifier for this SC
  134. * @active: channel is active
  135. * @sa: array of secure associations
  136. * @stats: per-SC stats
  137. */
  138. struct macsec_rx_sc {
  139. struct macsec_rx_sc __rcu *next;
  140. sci_t sci;
  141. bool active;
  142. struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
  143. struct pcpu_rx_sc_stats __percpu *stats;
  144. refcount_t refcnt;
  145. struct rcu_head rcu_head;
  146. };
  147. /**
  148. * struct macsec_tx_sa - transmit secure association
  149. * @active:
  150. * @next_pn: packet number to use for the next packet
  151. * @lock: protects next_pn manipulations
  152. * @key: key structure
  153. * @ssci: short secure channel identifier
  154. * @stats: per-SA stats
  155. */
  156. struct macsec_tx_sa {
  157. struct macsec_key key;
  158. ssci_t ssci;
  159. spinlock_t lock;
  160. union {
  161. pn_t next_pn_halves;
  162. u64 next_pn;
  163. };
  164. refcount_t refcnt;
  165. bool active;
  166. struct macsec_tx_sa_stats __percpu *stats;
  167. struct rcu_head rcu;
  168. };
  169. /**
  170. * struct macsec_tx_sc - transmit secure channel
  171. * @active:
  172. * @encoding_sa: association number of the SA currently in use
  173. * @encrypt: encrypt packets on transmit, or authenticate only
  174. * @send_sci: always include the SCI in the SecTAG
  175. * @end_station:
  176. * @scb: single copy broadcast flag
  177. * @sa: array of secure associations
  178. * @stats: stats for this TXSC
  179. * @md_dst: MACsec offload metadata dst
  180. */
  181. struct macsec_tx_sc {
  182. bool active;
  183. u8 encoding_sa;
  184. bool encrypt;
  185. bool send_sci;
  186. bool end_station;
  187. bool scb;
  188. struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
  189. struct pcpu_tx_sc_stats __percpu *stats;
  190. struct metadata_dst *md_dst;
  191. };
  192. /**
  193. * struct macsec_secy - MACsec Security Entity
  194. * @netdev: netdevice for this SecY
  195. * @n_rx_sc: number of receive secure channels configured on this SecY
  196. * @sci: secure channel identifier used for tx
  197. * @key_len: length of keys used by the cipher suite
  198. * @icv_len: length of ICV used by the cipher suite
  199. * @validate_frames: validation mode
  200. * @xpn: enable XPN for this SecY
  201. * @operational: MAC_Operational flag
  202. * @protect_frames: enable protection for this SecY
  203. * @replay_protect: enable packet number checks on receive
  204. * @replay_window: size of the replay window
  205. * @tx_sc: transmit secure channel
  206. * @rx_sc: linked list of receive secure channels
  207. */
  208. struct macsec_secy {
  209. struct net_device *netdev;
  210. unsigned int n_rx_sc;
  211. sci_t sci;
  212. u16 key_len;
  213. u16 icv_len;
  214. enum macsec_validation_type validate_frames;
  215. bool xpn;
  216. bool operational;
  217. bool protect_frames;
  218. bool replay_protect;
  219. u32 replay_window;
  220. struct macsec_tx_sc tx_sc;
  221. struct macsec_rx_sc __rcu *rx_sc;
  222. };
  223. /**
  224. * struct macsec_context - MACsec context for hardware offloading
  225. */
  226. struct macsec_context {
  227. union {
  228. struct net_device *netdev;
  229. struct phy_device *phydev;
  230. };
  231. enum macsec_offload offload;
  232. struct macsec_secy *secy;
  233. struct macsec_rx_sc *rx_sc;
  234. struct {
  235. unsigned char assoc_num;
  236. u8 key[MACSEC_MAX_KEY_LEN];
  237. union {
  238. struct macsec_rx_sa *rx_sa;
  239. struct macsec_tx_sa *tx_sa;
  240. };
  241. } sa;
  242. union {
  243. struct macsec_tx_sc_stats *tx_sc_stats;
  244. struct macsec_tx_sa_stats *tx_sa_stats;
  245. struct macsec_rx_sc_stats *rx_sc_stats;
  246. struct macsec_rx_sa_stats *rx_sa_stats;
  247. struct macsec_dev_stats *dev_stats;
  248. } stats;
  249. };
  250. /**
  251. * struct macsec_ops - MACsec offloading operations
  252. */
  253. struct macsec_ops {
  254. /* Device wide */
  255. int (*mdo_dev_open)(struct macsec_context *ctx);
  256. int (*mdo_dev_stop)(struct macsec_context *ctx);
  257. /* SecY */
  258. int (*mdo_add_secy)(struct macsec_context *ctx);
  259. int (*mdo_upd_secy)(struct macsec_context *ctx);
  260. int (*mdo_del_secy)(struct macsec_context *ctx);
  261. /* Security channels */
  262. int (*mdo_add_rxsc)(struct macsec_context *ctx);
  263. int (*mdo_upd_rxsc)(struct macsec_context *ctx);
  264. int (*mdo_del_rxsc)(struct macsec_context *ctx);
  265. /* Security associations */
  266. int (*mdo_add_rxsa)(struct macsec_context *ctx);
  267. int (*mdo_upd_rxsa)(struct macsec_context *ctx);
  268. int (*mdo_del_rxsa)(struct macsec_context *ctx);
  269. int (*mdo_add_txsa)(struct macsec_context *ctx);
  270. int (*mdo_upd_txsa)(struct macsec_context *ctx);
  271. int (*mdo_del_txsa)(struct macsec_context *ctx);
  272. /* Statistics */
  273. int (*mdo_get_dev_stats)(struct macsec_context *ctx);
  274. int (*mdo_get_tx_sc_stats)(struct macsec_context *ctx);
  275. int (*mdo_get_tx_sa_stats)(struct macsec_context *ctx);
  276. int (*mdo_get_rx_sc_stats)(struct macsec_context *ctx);
  277. int (*mdo_get_rx_sa_stats)(struct macsec_context *ctx);
  278. };
  279. void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa);
  280. static inline bool macsec_send_sci(const struct macsec_secy *secy)
  281. {
  282. const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  283. return tx_sc->send_sci ||
  284. (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
  285. }
  286. #endif /* _NET_MACSEC_H_ */