2 * drivers/net/macsec.c - MACsec device
4 * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/types.h>
13 #include <linux/skbuff.h>
14 #include <linux/socket.h>
15 #include <linux/module.h>
16 #include <crypto/aead.h>
17 #include <linux/etherdevice.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/refcount.h>
20 #include <net/genetlink.h>
22 #include <net/gro_cells.h>
23 #include <linux/if_arp.h>
25 #include <uapi/linux/if_macsec.h>
27 typedef u64 __bitwise sci_t;
29 #define MACSEC_SCI_LEN 8
31 /* SecTAG length = macsec_eth_header without the optional SCI */
32 #define MACSEC_TAG_LEN 6
34 struct macsec_eth_header {
38 #if defined(__LITTLE_ENDIAN_BITFIELD)
41 #elif defined(__BIG_ENDIAN_BITFIELD)
45 #error "Please fix <asm/byteorder.h>"
48 u8 secure_channel_id[8]; /* optional */
51 #define MACSEC_TCI_VERSION 0x80
52 #define MACSEC_TCI_ES 0x40 /* end station */
53 #define MACSEC_TCI_SC 0x20 /* SCI present */
54 #define MACSEC_TCI_SCB 0x10 /* epon */
55 #define MACSEC_TCI_E 0x08 /* encryption */
56 #define MACSEC_TCI_C 0x04 /* changed text */
57 #define MACSEC_AN_MASK 0x03 /* association number */
58 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
60 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
61 #define MIN_NON_SHORT_LEN 48
63 #define GCM_AES_IV_LEN 12
64 #define DEFAULT_ICV_LEN 16
66 #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
68 #define for_each_rxsc(secy, sc) \
69 for (sc = rcu_dereference_bh(secy->rx_sc); \
71 sc = rcu_dereference_bh(sc->next))
72 #define for_each_rxsc_rtnl(secy, sc) \
73 for (sc = rtnl_dereference(secy->rx_sc); \
75 sc = rtnl_dereference(sc->next))
79 u8 secure_channel_id[8];
86 * struct macsec_key - SA key
87 * @id: user-provided key identifier
88 * @tfm: crypto struct, key storage
91 u8 id[MACSEC_KEYID_LEN];
92 struct crypto_aead *tfm;
95 struct macsec_rx_sc_stats {
96 __u64 InOctetsValidated;
97 __u64 InOctetsDecrypted;
98 __u64 InPktsUnchecked;
103 __u64 InPktsNotValid;
104 __u64 InPktsNotUsingSA;
105 __u64 InPktsUnusedSA;
108 struct macsec_rx_sa_stats {
111 __u32 InPktsNotValid;
112 __u32 InPktsNotUsingSA;
113 __u32 InPktsUnusedSA;
116 struct macsec_tx_sa_stats {
117 __u32 OutPktsProtected;
118 __u32 OutPktsEncrypted;
121 struct macsec_tx_sc_stats {
122 __u64 OutPktsProtected;
123 __u64 OutPktsEncrypted;
124 __u64 OutOctetsProtected;
125 __u64 OutOctetsEncrypted;
128 struct macsec_dev_stats {
129 __u64 OutPktsUntagged;
130 __u64 InPktsUntagged;
131 __u64 OutPktsTooLong;
134 __u64 InPktsUnknownSCI;
140 * struct macsec_rx_sa - receive secure association
142 * @next_pn: packet number expected for the next packet
143 * @lock: protects next_pn manipulations
144 * @key: key structure
145 * @stats: per-SA stats
147 struct macsec_rx_sa {
148 struct macsec_key key;
153 struct macsec_rx_sa_stats __percpu *stats;
154 struct macsec_rx_sc *sc;
158 struct pcpu_rx_sc_stats {
159 struct macsec_rx_sc_stats stats;
160 struct u64_stats_sync syncp;
164 * struct macsec_rx_sc - receive secure channel
165 * @sci: secure channel identifier for this SC
166 * @active: channel is active
167 * @sa: array of secure associations
168 * @stats: per-SC stats
170 struct macsec_rx_sc {
171 struct macsec_rx_sc __rcu *next;
174 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
175 struct pcpu_rx_sc_stats __percpu *stats;
177 struct rcu_head rcu_head;
181 * struct macsec_tx_sa - transmit secure association
183 * @next_pn: packet number to use for the next packet
184 * @lock: protects next_pn manipulations
185 * @key: key structure
186 * @stats: per-SA stats
188 struct macsec_tx_sa {
189 struct macsec_key key;
194 struct macsec_tx_sa_stats __percpu *stats;
198 struct pcpu_tx_sc_stats {
199 struct macsec_tx_sc_stats stats;
200 struct u64_stats_sync syncp;
204 * struct macsec_tx_sc - transmit secure channel
206 * @encoding_sa: association number of the SA currently in use
207 * @encrypt: encrypt packets on transmit, or authenticate only
208 * @send_sci: always include the SCI in the SecTAG
210 * @scb: single copy broadcast flag
211 * @sa: array of secure associations
212 * @stats: stats for this TXSC
214 struct macsec_tx_sc {
221 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
222 struct pcpu_tx_sc_stats __percpu *stats;
225 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
228 * struct macsec_secy - MACsec Security Entity
229 * @netdev: netdevice for this SecY
230 * @n_rx_sc: number of receive secure channels configured on this SecY
231 * @sci: secure channel identifier used for tx
232 * @key_len: length of keys used by the cipher suite
233 * @icv_len: length of ICV used by the cipher suite
234 * @validate_frames: validation mode
235 * @operational: MAC_Operational flag
236 * @protect_frames: enable protection for this SecY
237 * @replay_protect: enable packet number checks on receive
238 * @replay_window: size of the replay window
239 * @tx_sc: transmit secure channel
240 * @rx_sc: linked list of receive secure channels
243 struct net_device *netdev;
244 unsigned int n_rx_sc;
248 enum macsec_validation_type validate_frames;
253 struct macsec_tx_sc tx_sc;
254 struct macsec_rx_sc __rcu *rx_sc;
257 struct pcpu_secy_stats {
258 struct macsec_dev_stats stats;
259 struct u64_stats_sync syncp;
263 * struct macsec_dev - private data
265 * @real_dev: pointer to underlying netdevice
266 * @stats: MACsec device stats
267 * @secys: linked list of SecY's on the underlying device
270 struct macsec_secy secy;
271 struct net_device *real_dev;
272 struct pcpu_secy_stats __percpu *stats;
273 struct list_head secys;
274 struct gro_cells gro_cells;
275 unsigned int nest_level;
279 * struct macsec_rxh_data - rx_handler private argument
280 * @secys: linked list of SecY's on this underlying device
282 struct macsec_rxh_data {
283 struct list_head secys;
286 static struct macsec_dev *macsec_priv(const struct net_device *dev)
288 return (struct macsec_dev *)netdev_priv(dev);
291 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
293 return rcu_dereference_bh(dev->rx_handler_data);
296 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
298 return rtnl_dereference(dev->rx_handler_data);
302 struct aead_request *req;
304 struct macsec_tx_sa *tx_sa;
305 struct macsec_rx_sa *rx_sa;
312 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
314 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
316 if (!sa || !sa->active)
319 if (!refcount_inc_not_zero(&sa->refcnt))
325 static struct macsec_rx_sa *macsec_active_rxsa_get(struct macsec_rx_sc *rx_sc)
327 struct macsec_rx_sa *sa = NULL;
330 for (an = 0; an < MACSEC_NUM_AN; an++) {
331 sa = macsec_rxsa_get(rx_sc->sa[an]);
338 static void free_rx_sc_rcu(struct rcu_head *head)
340 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
342 free_percpu(rx_sc->stats);
346 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
348 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
351 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
353 if (refcount_dec_and_test(&sc->refcnt))
354 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
357 static void free_rxsa(struct rcu_head *head)
359 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
361 crypto_free_aead(sa->key.tfm);
362 free_percpu(sa->stats);
366 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
368 if (refcount_dec_and_test(&sa->refcnt))
369 call_rcu(&sa->rcu, free_rxsa);
372 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
374 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
376 if (!sa || !sa->active)
379 if (!refcount_inc_not_zero(&sa->refcnt))
385 static void free_txsa(struct rcu_head *head)
387 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
389 crypto_free_aead(sa->key.tfm);
390 free_percpu(sa->stats);
394 static void macsec_txsa_put(struct macsec_tx_sa *sa)
396 if (refcount_dec_and_test(&sa->refcnt))
397 call_rcu(&sa->rcu, free_txsa);
400 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
402 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
403 return (struct macsec_cb *)skb->cb;
406 #define MACSEC_PORT_ES (htons(0x0001))
407 #define MACSEC_PORT_SCB (0x0000)
408 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
410 #define MACSEC_GCM_AES_128_SAK_LEN 16
411 #define MACSEC_GCM_AES_256_SAK_LEN 32
413 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
414 #define DEFAULT_SEND_SCI true
415 #define DEFAULT_ENCRYPT false
416 #define DEFAULT_ENCODING_SA 0
418 static bool send_sci(const struct macsec_secy *secy)
420 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
422 return tx_sc->send_sci ||
423 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
426 static sci_t make_sci(u8 *addr, __be16 port)
430 memcpy(&sci, addr, ETH_ALEN);
431 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
436 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
441 memcpy(&sci, hdr->secure_channel_id,
442 sizeof(hdr->secure_channel_id));
444 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
449 static unsigned int macsec_sectag_len(bool sci_present)
451 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
454 static unsigned int macsec_hdr_len(bool sci_present)
456 return macsec_sectag_len(sci_present) + ETH_HLEN;
459 static unsigned int macsec_extra_len(bool sci_present)
461 return macsec_sectag_len(sci_present) + sizeof(__be16);
464 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
465 static void macsec_fill_sectag(struct macsec_eth_header *h,
466 const struct macsec_secy *secy, u32 pn,
469 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
471 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
472 h->eth.h_proto = htons(ETH_P_MACSEC);
475 h->tci_an |= MACSEC_TCI_SC;
476 memcpy(&h->secure_channel_id, &secy->sci,
477 sizeof(h->secure_channel_id));
479 if (tx_sc->end_station)
480 h->tci_an |= MACSEC_TCI_ES;
482 h->tci_an |= MACSEC_TCI_SCB;
485 h->packet_number = htonl(pn);
487 /* with GCM, C/E clear for !encrypt, both set for encrypt */
489 h->tci_an |= MACSEC_TCI_CONFID;
490 else if (secy->icv_len != DEFAULT_ICV_LEN)
491 h->tci_an |= MACSEC_TCI_C;
493 h->tci_an |= tx_sc->encoding_sa;
496 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
498 if (data_len < MIN_NON_SHORT_LEN)
499 h->short_length = data_len;
502 /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
503 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
505 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
506 int len = skb->len - 2 * ETH_ALEN;
507 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
509 /* a) It comprises at least 17 octets */
513 /* b) MACsec EtherType: already checked */
515 /* c) V bit is clear */
516 if (h->tci_an & MACSEC_TCI_VERSION)
519 /* d) ES or SCB => !SC */
520 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
521 (h->tci_an & MACSEC_TCI_SC))
524 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
528 /* rx.pn != 0 (figure 10-5) */
529 if (!h->packet_number)
532 /* length check, f) g) h) i) */
534 return len == extra_len + h->short_length;
535 return len >= extra_len + MIN_NON_SHORT_LEN;
538 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
539 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
541 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
543 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
546 gcm_iv->pn = htonl(pn);
549 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
551 return (struct macsec_eth_header *)skb_mac_header(skb);
554 static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
558 spin_lock_bh(&tx_sa->lock);
562 if (tx_sa->next_pn == 0) {
563 pr_debug("PN wrapped, transitioning to !oper\n");
564 tx_sa->active = false;
565 if (secy->protect_frames)
566 secy->operational = false;
568 spin_unlock_bh(&tx_sa->lock);
573 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
575 struct macsec_dev *macsec = netdev_priv(dev);
577 skb->dev = macsec->real_dev;
578 skb_reset_mac_header(skb);
579 skb->protocol = eth_hdr(skb)->h_proto;
582 static unsigned int macsec_msdu_len(struct sk_buff *skb)
584 struct macsec_dev *macsec = macsec_priv(skb->dev);
585 struct macsec_secy *secy = &macsec->secy;
586 bool sci_present = macsec_skb_cb(skb)->has_sci;
588 return skb->len - macsec_hdr_len(sci_present) - secy->icv_len;
591 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
592 struct macsec_tx_sa *tx_sa)
594 unsigned int msdu_len = macsec_msdu_len(skb);
595 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
597 u64_stats_update_begin(&txsc_stats->syncp);
598 if (tx_sc->encrypt) {
599 txsc_stats->stats.OutOctetsEncrypted += msdu_len;
600 txsc_stats->stats.OutPktsEncrypted++;
601 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
603 txsc_stats->stats.OutOctetsProtected += msdu_len;
604 txsc_stats->stats.OutPktsProtected++;
605 this_cpu_inc(tx_sa->stats->OutPktsProtected);
607 u64_stats_update_end(&txsc_stats->syncp);
610 static void count_tx(struct net_device *dev, int ret, int len)
612 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
613 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
615 u64_stats_update_begin(&stats->syncp);
617 stats->tx_bytes += len;
618 u64_stats_update_end(&stats->syncp);
622 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
624 struct sk_buff *skb = base->data;
625 struct net_device *dev = skb->dev;
626 struct macsec_dev *macsec = macsec_priv(dev);
627 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
630 aead_request_free(macsec_skb_cb(skb)->req);
633 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
634 /* packet is encrypted/protected so tx_bytes must be calculated */
635 len = macsec_msdu_len(skb) + 2 * ETH_ALEN;
636 macsec_encrypt_finish(skb, dev);
637 ret = dev_queue_xmit(skb);
638 count_tx(dev, ret, len);
639 rcu_read_unlock_bh();
645 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
647 struct scatterlist **sg,
650 size_t size, iv_offset, sg_offset;
651 struct aead_request *req;
654 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
656 size += GCM_AES_IV_LEN;
658 size = ALIGN(size, __alignof__(struct scatterlist));
660 size += sizeof(struct scatterlist) * num_frags;
662 tmp = kmalloc(size, GFP_ATOMIC);
666 *iv = (unsigned char *)(tmp + iv_offset);
667 *sg = (struct scatterlist *)(tmp + sg_offset);
670 aead_request_set_tfm(req, tfm);
675 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
676 struct net_device *dev)
679 struct scatterlist *sg;
680 struct sk_buff *trailer;
683 struct macsec_eth_header *hh;
684 size_t unprotected_len;
685 struct aead_request *req;
686 struct macsec_secy *secy;
687 struct macsec_tx_sc *tx_sc;
688 struct macsec_tx_sa *tx_sa;
689 struct macsec_dev *macsec = macsec_priv(dev);
693 secy = &macsec->secy;
694 tx_sc = &secy->tx_sc;
696 /* 10.5.1 TX SA assignment */
697 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
699 secy->operational = false;
701 return ERR_PTR(-EINVAL);
704 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
705 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
706 struct sk_buff *nskb = skb_copy_expand(skb,
707 MACSEC_NEEDED_HEADROOM,
708 MACSEC_NEEDED_TAILROOM,
714 macsec_txsa_put(tx_sa);
716 return ERR_PTR(-ENOMEM);
719 skb = skb_unshare(skb, GFP_ATOMIC);
721 macsec_txsa_put(tx_sa);
722 return ERR_PTR(-ENOMEM);
726 unprotected_len = skb->len;
728 sci_present = send_sci(secy);
729 hh = skb_push(skb, macsec_extra_len(sci_present));
730 memmove(hh, eth, 2 * ETH_ALEN);
732 pn = tx_sa_update_pn(tx_sa, secy);
734 macsec_txsa_put(tx_sa);
736 return ERR_PTR(-ENOLINK);
738 macsec_fill_sectag(hh, secy, pn, sci_present);
739 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
741 skb_put(skb, secy->icv_len);
743 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
744 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
746 u64_stats_update_begin(&secy_stats->syncp);
747 secy_stats->stats.OutPktsTooLong++;
748 u64_stats_update_end(&secy_stats->syncp);
750 macsec_txsa_put(tx_sa);
752 return ERR_PTR(-EINVAL);
755 ret = skb_cow_data(skb, 0, &trailer);
756 if (unlikely(ret < 0)) {
757 macsec_txsa_put(tx_sa);
762 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
764 macsec_txsa_put(tx_sa);
766 return ERR_PTR(-ENOMEM);
769 macsec_fill_iv(iv, secy->sci, pn);
771 sg_init_table(sg, ret);
772 ret = skb_to_sgvec(skb, sg, 0, skb->len);
773 if (unlikely(ret < 0)) {
774 aead_request_free(req);
775 macsec_txsa_put(tx_sa);
780 if (tx_sc->encrypt) {
781 int len = skb->len - macsec_hdr_len(sci_present) -
783 aead_request_set_crypt(req, sg, sg, len, iv);
784 aead_request_set_ad(req, macsec_hdr_len(sci_present));
786 aead_request_set_crypt(req, sg, sg, 0, iv);
787 aead_request_set_ad(req, skb->len - secy->icv_len);
790 macsec_skb_cb(skb)->req = req;
791 macsec_skb_cb(skb)->tx_sa = tx_sa;
792 macsec_skb_cb(skb)->has_sci = sci_present;
793 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
796 ret = crypto_aead_encrypt(req);
797 if (ret == -EINPROGRESS) {
799 } else if (ret != 0) {
802 aead_request_free(req);
803 macsec_txsa_put(tx_sa);
804 return ERR_PTR(-EINVAL);
808 aead_request_free(req);
809 macsec_txsa_put(tx_sa);
814 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
816 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
817 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
818 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
821 spin_lock(&rx_sa->lock);
822 if (rx_sa->next_pn >= secy->replay_window)
823 lowest_pn = rx_sa->next_pn - secy->replay_window;
825 /* Now perform replay protection check again
826 * (see IEEE 802.1AE-2006 figure 10-5)
828 if (secy->replay_protect && pn < lowest_pn) {
829 spin_unlock(&rx_sa->lock);
830 u64_stats_update_begin(&rxsc_stats->syncp);
831 rxsc_stats->stats.InPktsLate++;
832 u64_stats_update_end(&rxsc_stats->syncp);
833 DEV_STATS_INC(secy->netdev, rx_dropped);
837 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
838 unsigned int msdu_len = macsec_msdu_len(skb);
839 u64_stats_update_begin(&rxsc_stats->syncp);
840 if (hdr->tci_an & MACSEC_TCI_E)
841 rxsc_stats->stats.InOctetsDecrypted += msdu_len;
843 rxsc_stats->stats.InOctetsValidated += msdu_len;
844 u64_stats_update_end(&rxsc_stats->syncp);
847 if (!macsec_skb_cb(skb)->valid) {
848 spin_unlock(&rx_sa->lock);
851 if (hdr->tci_an & MACSEC_TCI_C ||
852 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
853 u64_stats_update_begin(&rxsc_stats->syncp);
854 rxsc_stats->stats.InPktsNotValid++;
855 u64_stats_update_end(&rxsc_stats->syncp);
856 this_cpu_inc(rx_sa->stats->InPktsNotValid);
857 DEV_STATS_INC(secy->netdev, rx_errors);
861 u64_stats_update_begin(&rxsc_stats->syncp);
862 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
863 rxsc_stats->stats.InPktsInvalid++;
864 this_cpu_inc(rx_sa->stats->InPktsInvalid);
865 } else if (pn < lowest_pn) {
866 rxsc_stats->stats.InPktsDelayed++;
868 rxsc_stats->stats.InPktsUnchecked++;
870 u64_stats_update_end(&rxsc_stats->syncp);
872 u64_stats_update_begin(&rxsc_stats->syncp);
873 if (pn < lowest_pn) {
874 rxsc_stats->stats.InPktsDelayed++;
876 rxsc_stats->stats.InPktsOK++;
877 this_cpu_inc(rx_sa->stats->InPktsOK);
879 u64_stats_update_end(&rxsc_stats->syncp);
881 if (pn >= rx_sa->next_pn)
882 rx_sa->next_pn = pn + 1;
883 spin_unlock(&rx_sa->lock);
889 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
891 skb->pkt_type = PACKET_HOST;
892 skb->protocol = eth_type_trans(skb, dev);
894 skb_reset_network_header(skb);
895 if (!skb_transport_header_was_set(skb))
896 skb_reset_transport_header(skb);
897 skb_reset_mac_len(skb);
900 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
902 skb->ip_summed = CHECKSUM_NONE;
903 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
904 skb_pull(skb, hdr_len);
905 pskb_trim_unique(skb, skb->len - icv_len);
908 static void count_rx(struct net_device *dev, int len)
910 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
912 u64_stats_update_begin(&stats->syncp);
914 stats->rx_bytes += len;
915 u64_stats_update_end(&stats->syncp);
918 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
920 struct sk_buff *skb = base->data;
921 struct net_device *dev = skb->dev;
922 struct macsec_dev *macsec = macsec_priv(dev);
923 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
924 struct macsec_rx_sc *rx_sc = rx_sa->sc;
928 aead_request_free(macsec_skb_cb(skb)->req);
931 macsec_skb_cb(skb)->valid = true;
934 pn = ntohl(macsec_ethhdr(skb)->packet_number);
935 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
936 rcu_read_unlock_bh();
941 macsec_finalize_skb(skb, macsec->secy.icv_len,
942 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
944 macsec_reset_skb(skb, macsec->secy.netdev);
946 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
949 rcu_read_unlock_bh();
952 macsec_rxsa_put(rx_sa);
953 macsec_rxsc_put(rx_sc);
957 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
958 struct net_device *dev,
959 struct macsec_rx_sa *rx_sa,
961 struct macsec_secy *secy)
964 struct scatterlist *sg;
965 struct sk_buff *trailer;
967 struct aead_request *req;
968 struct macsec_eth_header *hdr;
969 u16 icv_len = secy->icv_len;
971 macsec_skb_cb(skb)->valid = false;
972 skb = skb_share_check(skb, GFP_ATOMIC);
974 return ERR_PTR(-ENOMEM);
976 ret = skb_cow_data(skb, 0, &trailer);
977 if (unlikely(ret < 0)) {
981 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
984 return ERR_PTR(-ENOMEM);
987 hdr = (struct macsec_eth_header *)skb->data;
988 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
990 sg_init_table(sg, ret);
991 ret = skb_to_sgvec(skb, sg, 0, skb->len);
992 if (unlikely(ret < 0)) {
993 aead_request_free(req);
998 if (hdr->tci_an & MACSEC_TCI_E) {
999 /* confidentiality: ethernet + macsec header
1000 * authenticated, encrypted payload
1002 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
1004 aead_request_set_crypt(req, sg, sg, len, iv);
1005 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
1006 skb = skb_unshare(skb, GFP_ATOMIC);
1008 aead_request_free(req);
1009 return ERR_PTR(-ENOMEM);
1012 /* integrity only: all headers + data authenticated */
1013 aead_request_set_crypt(req, sg, sg, icv_len, iv);
1014 aead_request_set_ad(req, skb->len - icv_len);
1017 macsec_skb_cb(skb)->req = req;
1019 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
1022 ret = crypto_aead_decrypt(req);
1023 if (ret == -EINPROGRESS) {
1024 return ERR_PTR(ret);
1025 } else if (ret != 0) {
1026 /* decryption/authentication failed
1027 * 10.6 if validateFrames is disabled, deliver anyway
1029 if (ret != -EBADMSG) {
1034 macsec_skb_cb(skb)->valid = true;
1038 aead_request_free(req);
1043 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
1045 struct macsec_rx_sc *rx_sc;
1047 for_each_rxsc(secy, rx_sc) {
1048 if (rx_sc->sci == sci)
1055 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1057 struct macsec_rx_sc *rx_sc;
1059 for_each_rxsc_rtnl(secy, rx_sc) {
1060 if (rx_sc->sci == sci)
1067 static void handle_not_macsec(struct sk_buff *skb)
1069 struct macsec_rxh_data *rxd;
1070 struct macsec_dev *macsec;
1073 rxd = macsec_data_rcu(skb->dev);
1075 /* 10.6 If the management control validateFrames is not
1076 * Strict, frames without a SecTAG are received, counted, and
1077 * delivered to the Controlled Port
1079 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1080 struct sk_buff *nskb;
1081 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1083 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1084 u64_stats_update_begin(&secy_stats->syncp);
1085 secy_stats->stats.InPktsNoTag++;
1086 u64_stats_update_end(&secy_stats->syncp);
1087 DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1091 /* deliver on this port */
1092 nskb = skb_clone(skb, GFP_ATOMIC);
1096 nskb->dev = macsec->secy.netdev;
1098 if (netif_rx(nskb) == NET_RX_SUCCESS) {
1099 u64_stats_update_begin(&secy_stats->syncp);
1100 secy_stats->stats.InPktsUntagged++;
1101 u64_stats_update_end(&secy_stats->syncp);
1108 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1110 struct sk_buff *skb = *pskb;
1111 struct net_device *dev = skb->dev;
1112 struct macsec_eth_header *hdr;
1113 struct macsec_secy *secy = NULL;
1114 struct macsec_rx_sc *rx_sc;
1115 struct macsec_rx_sa *rx_sa;
1116 struct macsec_rxh_data *rxd;
1117 struct macsec_dev *macsec;
1122 struct pcpu_rx_sc_stats *rxsc_stats;
1123 struct pcpu_secy_stats *secy_stats;
1127 if (skb_headroom(skb) < ETH_HLEN)
1130 hdr = macsec_ethhdr(skb);
1131 if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1132 handle_not_macsec(skb);
1134 /* and deliver to the uncontrolled port */
1135 return RX_HANDLER_PASS;
1138 skb = skb_unshare(skb, GFP_ATOMIC);
1141 return RX_HANDLER_CONSUMED;
1143 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1145 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1149 hdr = macsec_ethhdr(skb);
1151 /* Frames with a SecTAG that has the TCI E bit set but the C
1152 * bit clear are discarded, as this reserved encoding is used
1153 * to identify frames with a SecTAG that are not to be
1154 * delivered to the Controlled Port.
1156 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1157 return RX_HANDLER_PASS;
1159 /* now, pull the extra length */
1160 if (hdr->tci_an & MACSEC_TCI_SC) {
1165 /* ethernet header is part of crypto processing */
1166 skb_push(skb, ETH_HLEN);
1168 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1169 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1170 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1173 rxd = macsec_data_rcu(skb->dev);
1175 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1176 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1177 sc = sc ? macsec_rxsc_get(sc) : NULL;
1180 secy = &macsec->secy;
1190 macsec = macsec_priv(dev);
1191 secy_stats = this_cpu_ptr(macsec->stats);
1192 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1194 if (!macsec_validate_skb(skb, secy->icv_len)) {
1195 u64_stats_update_begin(&secy_stats->syncp);
1196 secy_stats->stats.InPktsBadTag++;
1197 u64_stats_update_end(&secy_stats->syncp);
1198 DEV_STATS_INC(secy->netdev, rx_errors);
1202 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1204 /* 10.6.1 if the SA is not in use */
1206 /* If validateFrames is Strict or the C bit in the
1207 * SecTAG is set, discard
1209 struct macsec_rx_sa *active_rx_sa = macsec_active_rxsa_get(rx_sc);
1210 if (hdr->tci_an & MACSEC_TCI_C ||
1211 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1212 u64_stats_update_begin(&rxsc_stats->syncp);
1213 rxsc_stats->stats.InPktsNotUsingSA++;
1214 u64_stats_update_end(&rxsc_stats->syncp);
1215 DEV_STATS_INC(secy->netdev, rx_errors);
1217 this_cpu_inc(active_rx_sa->stats->InPktsNotUsingSA);
1221 /* not Strict, the frame (with the SecTAG and ICV
1222 * removed) is delivered to the Controlled Port.
1224 u64_stats_update_begin(&rxsc_stats->syncp);
1225 rxsc_stats->stats.InPktsUnusedSA++;
1226 u64_stats_update_end(&rxsc_stats->syncp);
1228 this_cpu_inc(active_rx_sa->stats->InPktsUnusedSA);
1232 /* First, PN check to avoid decrypting obviously wrong packets */
1233 pn = ntohl(hdr->packet_number);
1234 if (secy->replay_protect) {
1237 spin_lock(&rx_sa->lock);
1238 late = rx_sa->next_pn >= secy->replay_window &&
1239 pn < (rx_sa->next_pn - secy->replay_window);
1240 spin_unlock(&rx_sa->lock);
1243 u64_stats_update_begin(&rxsc_stats->syncp);
1244 rxsc_stats->stats.InPktsLate++;
1245 u64_stats_update_end(&rxsc_stats->syncp);
1246 DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1251 macsec_skb_cb(skb)->rx_sa = rx_sa;
1253 /* Disabled && !changed text => skip validation */
1254 if (hdr->tci_an & MACSEC_TCI_C ||
1255 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1256 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1259 /* the decrypt callback needs the reference */
1260 if (PTR_ERR(skb) != -EINPROGRESS) {
1261 macsec_rxsa_put(rx_sa);
1262 macsec_rxsc_put(rx_sc);
1266 return RX_HANDLER_CONSUMED;
1269 if (!macsec_post_decrypt(skb, secy, pn))
1273 macsec_finalize_skb(skb, secy->icv_len,
1274 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1276 macsec_reset_skb(skb, secy->netdev);
1279 macsec_rxsa_put(rx_sa);
1280 macsec_rxsc_put(rx_sc);
1283 ret = gro_cells_receive(&macsec->gro_cells, skb);
1284 if (ret == NET_RX_SUCCESS)
1287 DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1292 return RX_HANDLER_CONSUMED;
1295 macsec_rxsa_put(rx_sa);
1297 macsec_rxsc_put(rx_sc);
1302 return RX_HANDLER_CONSUMED;
1305 /* 10.6.1 if the SC is not found */
1306 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1308 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1309 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1311 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1312 struct sk_buff *nskb;
1314 secy_stats = this_cpu_ptr(macsec->stats);
1316 /* If validateFrames is Strict or the C bit in the
1317 * SecTAG is set, discard
1320 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1321 u64_stats_update_begin(&secy_stats->syncp);
1322 secy_stats->stats.InPktsNoSCI++;
1323 u64_stats_update_end(&secy_stats->syncp);
1324 DEV_STATS_INC(macsec->secy.netdev, rx_errors);
1328 /* not strict, the frame (with the SecTAG and ICV
1329 * removed) is delivered to the Controlled Port.
1331 nskb = skb_clone(skb, GFP_ATOMIC);
1335 macsec_reset_skb(nskb, macsec->secy.netdev);
1337 ret = netif_rx(nskb);
1338 if (ret == NET_RX_SUCCESS) {
1339 u64_stats_update_begin(&secy_stats->syncp);
1340 secy_stats->stats.InPktsUnknownSCI++;
1341 u64_stats_update_end(&secy_stats->syncp);
1343 DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1349 return RX_HANDLER_PASS;
1352 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1354 struct crypto_aead *tfm;
1357 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
1362 ret = crypto_aead_setkey(tfm, key, key_len);
1366 ret = crypto_aead_setauthsize(tfm, icv_len);
1372 crypto_free_aead(tfm);
1373 return ERR_PTR(ret);
1376 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1379 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1383 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1384 if (IS_ERR(rx_sa->key.tfm)) {
1385 free_percpu(rx_sa->stats);
1386 return PTR_ERR(rx_sa->key.tfm);
1389 rx_sa->active = false;
1391 refcount_set(&rx_sa->refcnt, 1);
1392 spin_lock_init(&rx_sa->lock);
1397 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1399 rx_sa->active = false;
1401 macsec_rxsa_put(rx_sa);
1404 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1408 for (i = 0; i < MACSEC_NUM_AN; i++) {
1409 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1411 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1416 macsec_rxsc_put(rx_sc);
1419 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1421 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1423 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1425 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1426 if (rx_sc->sci == sci) {
1429 rcu_assign_pointer(*rx_scp, rx_sc->next);
1437 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1439 struct macsec_rx_sc *rx_sc;
1440 struct macsec_dev *macsec;
1441 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1442 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1443 struct macsec_secy *secy;
1445 list_for_each_entry(macsec, &rxd->secys, secys) {
1446 if (find_rx_sc_rtnl(&macsec->secy, sci))
1447 return ERR_PTR(-EEXIST);
1450 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1452 return ERR_PTR(-ENOMEM);
1454 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1455 if (!rx_sc->stats) {
1457 return ERR_PTR(-ENOMEM);
1461 rx_sc->active = true;
1462 refcount_set(&rx_sc->refcnt, 1);
1464 secy = &macsec_priv(dev)->secy;
1465 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1466 rcu_assign_pointer(secy->rx_sc, rx_sc);
1474 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1477 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1481 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1482 if (IS_ERR(tx_sa->key.tfm)) {
1483 free_percpu(tx_sa->stats);
1484 return PTR_ERR(tx_sa->key.tfm);
1487 tx_sa->active = false;
1488 refcount_set(&tx_sa->refcnt, 1);
1489 spin_lock_init(&tx_sa->lock);
1494 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1496 tx_sa->active = false;
1498 macsec_txsa_put(tx_sa);
1501 static struct genl_family macsec_fam;
1503 static struct net_device *get_dev_from_nl(struct net *net,
1504 struct nlattr **attrs)
1506 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1507 struct net_device *dev;
1509 dev = __dev_get_by_index(net, ifindex);
1511 return ERR_PTR(-ENODEV);
1513 if (!netif_is_macsec(dev))
1514 return ERR_PTR(-ENODEV);
1519 static sci_t nla_get_sci(const struct nlattr *nla)
1521 return (__force sci_t)nla_get_u64(nla);
1524 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1527 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1530 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1531 struct nlattr **attrs,
1532 struct nlattr **tb_sa,
1533 struct net_device **devp,
1534 struct macsec_secy **secyp,
1535 struct macsec_tx_sc **scp,
1538 struct net_device *dev;
1539 struct macsec_secy *secy;
1540 struct macsec_tx_sc *tx_sc;
1541 struct macsec_tx_sa *tx_sa;
1543 if (!tb_sa[MACSEC_SA_ATTR_AN])
1544 return ERR_PTR(-EINVAL);
1546 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1548 dev = get_dev_from_nl(net, attrs);
1550 return ERR_CAST(dev);
1552 if (*assoc_num >= MACSEC_NUM_AN)
1553 return ERR_PTR(-EINVAL);
1555 secy = &macsec_priv(dev)->secy;
1556 tx_sc = &secy->tx_sc;
1558 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1560 return ERR_PTR(-ENODEV);
1568 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1569 struct nlattr **attrs,
1570 struct nlattr **tb_rxsc,
1571 struct net_device **devp,
1572 struct macsec_secy **secyp)
1574 struct net_device *dev;
1575 struct macsec_secy *secy;
1576 struct macsec_rx_sc *rx_sc;
1579 dev = get_dev_from_nl(net, attrs);
1581 return ERR_CAST(dev);
1583 secy = &macsec_priv(dev)->secy;
1585 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1586 return ERR_PTR(-EINVAL);
1588 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1589 rx_sc = find_rx_sc_rtnl(secy, sci);
1591 return ERR_PTR(-ENODEV);
1599 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1600 struct nlattr **attrs,
1601 struct nlattr **tb_rxsc,
1602 struct nlattr **tb_sa,
1603 struct net_device **devp,
1604 struct macsec_secy **secyp,
1605 struct macsec_rx_sc **scp,
1608 struct macsec_rx_sc *rx_sc;
1609 struct macsec_rx_sa *rx_sa;
1611 if (!tb_sa[MACSEC_SA_ATTR_AN])
1612 return ERR_PTR(-EINVAL);
1614 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1615 if (*assoc_num >= MACSEC_NUM_AN)
1616 return ERR_PTR(-EINVAL);
1618 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1620 return ERR_CAST(rx_sc);
1622 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1624 return ERR_PTR(-ENODEV);
1631 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1632 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1633 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1634 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1637 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1638 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1639 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1642 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1643 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1644 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1645 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1646 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1647 .len = MACSEC_KEYID_LEN, },
1648 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1649 .len = MACSEC_MAX_KEY_LEN, },
1652 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1654 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1657 if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX,
1658 attrs[MACSEC_ATTR_SA_CONFIG],
1659 macsec_genl_sa_policy, NULL))
1665 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1667 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1670 if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX,
1671 attrs[MACSEC_ATTR_RXSC_CONFIG],
1672 macsec_genl_rxsc_policy, NULL))
1678 static bool validate_add_rxsa(struct nlattr **attrs)
1680 if (!attrs[MACSEC_SA_ATTR_AN] ||
1681 !attrs[MACSEC_SA_ATTR_KEY] ||
1682 !attrs[MACSEC_SA_ATTR_KEYID])
1685 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1688 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1691 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1692 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1696 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1702 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1704 struct net_device *dev;
1705 struct nlattr **attrs = info->attrs;
1706 struct macsec_secy *secy;
1707 struct macsec_rx_sc *rx_sc;
1708 struct macsec_rx_sa *rx_sa;
1709 unsigned char assoc_num;
1710 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1711 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1714 if (!attrs[MACSEC_ATTR_IFINDEX])
1717 if (parse_sa_config(attrs, tb_sa))
1720 if (parse_rxsc_config(attrs, tb_rxsc))
1723 if (!validate_add_rxsa(tb_sa))
1727 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1728 if (IS_ERR(rx_sc)) {
1730 return PTR_ERR(rx_sc);
1733 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1735 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1736 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1737 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1742 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1748 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1754 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1755 secy->key_len, secy->icv_len);
1762 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1763 spin_lock_bh(&rx_sa->lock);
1764 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1765 spin_unlock_bh(&rx_sa->lock);
1768 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1769 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1771 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1773 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1780 static bool validate_add_rxsc(struct nlattr **attrs)
1782 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1785 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1786 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1793 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1795 struct net_device *dev;
1796 sci_t sci = MACSEC_UNDEF_SCI;
1797 struct nlattr **attrs = info->attrs;
1798 struct macsec_rx_sc *rx_sc;
1799 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1801 if (!attrs[MACSEC_ATTR_IFINDEX])
1804 if (parse_rxsc_config(attrs, tb_rxsc))
1807 if (!validate_add_rxsc(tb_rxsc))
1811 dev = get_dev_from_nl(genl_info_net(info), attrs);
1814 return PTR_ERR(dev);
1817 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1819 rx_sc = create_rx_sc(dev, sci);
1820 if (IS_ERR(rx_sc)) {
1822 return PTR_ERR(rx_sc);
1825 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1826 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1833 static bool validate_add_txsa(struct nlattr **attrs)
1835 if (!attrs[MACSEC_SA_ATTR_AN] ||
1836 !attrs[MACSEC_SA_ATTR_PN] ||
1837 !attrs[MACSEC_SA_ATTR_KEY] ||
1838 !attrs[MACSEC_SA_ATTR_KEYID])
1841 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1844 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1847 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1848 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1852 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1858 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1860 struct net_device *dev;
1861 struct nlattr **attrs = info->attrs;
1862 struct macsec_secy *secy;
1863 struct macsec_tx_sc *tx_sc;
1864 struct macsec_tx_sa *tx_sa;
1865 unsigned char assoc_num;
1866 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1869 if (!attrs[MACSEC_ATTR_IFINDEX])
1872 if (parse_sa_config(attrs, tb_sa))
1875 if (!validate_add_txsa(tb_sa))
1879 dev = get_dev_from_nl(genl_info_net(info), attrs);
1882 return PTR_ERR(dev);
1885 secy = &macsec_priv(dev)->secy;
1886 tx_sc = &secy->tx_sc;
1888 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1890 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1891 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1892 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1897 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1903 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1909 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1910 secy->key_len, secy->icv_len);
1917 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1919 spin_lock_bh(&tx_sa->lock);
1920 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1921 spin_unlock_bh(&tx_sa->lock);
1923 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1924 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1926 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1927 secy->operational = true;
1929 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1936 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1938 struct nlattr **attrs = info->attrs;
1939 struct net_device *dev;
1940 struct macsec_secy *secy;
1941 struct macsec_rx_sc *rx_sc;
1942 struct macsec_rx_sa *rx_sa;
1944 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1945 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1947 if (!attrs[MACSEC_ATTR_IFINDEX])
1950 if (parse_sa_config(attrs, tb_sa))
1953 if (parse_rxsc_config(attrs, tb_rxsc))
1957 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1958 &dev, &secy, &rx_sc, &assoc_num);
1959 if (IS_ERR(rx_sa)) {
1961 return PTR_ERR(rx_sa);
1964 if (rx_sa->active) {
1969 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1977 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1979 struct nlattr **attrs = info->attrs;
1980 struct net_device *dev;
1981 struct macsec_secy *secy;
1982 struct macsec_rx_sc *rx_sc;
1984 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1986 if (!attrs[MACSEC_ATTR_IFINDEX])
1989 if (parse_rxsc_config(attrs, tb_rxsc))
1992 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1996 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1999 return PTR_ERR(dev);
2002 secy = &macsec_priv(dev)->secy;
2003 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2005 rx_sc = del_rx_sc(secy, sci);
2017 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2019 struct nlattr **attrs = info->attrs;
2020 struct net_device *dev;
2021 struct macsec_secy *secy;
2022 struct macsec_tx_sc *tx_sc;
2023 struct macsec_tx_sa *tx_sa;
2025 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2027 if (!attrs[MACSEC_ATTR_IFINDEX])
2030 if (parse_sa_config(attrs, tb_sa))
2034 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2035 &dev, &secy, &tx_sc, &assoc_num);
2036 if (IS_ERR(tx_sa)) {
2038 return PTR_ERR(tx_sa);
2041 if (tx_sa->active) {
2046 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2054 static bool validate_upd_sa(struct nlattr **attrs)
2056 if (!attrs[MACSEC_SA_ATTR_AN] ||
2057 attrs[MACSEC_SA_ATTR_KEY] ||
2058 attrs[MACSEC_SA_ATTR_KEYID])
2061 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2064 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2067 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2068 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2075 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2077 struct nlattr **attrs = info->attrs;
2078 struct net_device *dev;
2079 struct macsec_secy *secy;
2080 struct macsec_tx_sc *tx_sc;
2081 struct macsec_tx_sa *tx_sa;
2083 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2085 if (!attrs[MACSEC_ATTR_IFINDEX])
2088 if (parse_sa_config(attrs, tb_sa))
2091 if (!validate_upd_sa(tb_sa))
2095 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2096 &dev, &secy, &tx_sc, &assoc_num);
2097 if (IS_ERR(tx_sa)) {
2099 return PTR_ERR(tx_sa);
2102 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2103 spin_lock_bh(&tx_sa->lock);
2104 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2105 spin_unlock_bh(&tx_sa->lock);
2108 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2109 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2111 if (assoc_num == tx_sc->encoding_sa)
2112 secy->operational = tx_sa->active;
2119 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2121 struct nlattr **attrs = info->attrs;
2122 struct net_device *dev;
2123 struct macsec_secy *secy;
2124 struct macsec_rx_sc *rx_sc;
2125 struct macsec_rx_sa *rx_sa;
2127 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2128 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2130 if (!attrs[MACSEC_ATTR_IFINDEX])
2133 if (parse_rxsc_config(attrs, tb_rxsc))
2136 if (parse_sa_config(attrs, tb_sa))
2139 if (!validate_upd_sa(tb_sa))
2143 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2144 &dev, &secy, &rx_sc, &assoc_num);
2145 if (IS_ERR(rx_sa)) {
2147 return PTR_ERR(rx_sa);
2150 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2151 spin_lock_bh(&rx_sa->lock);
2152 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2153 spin_unlock_bh(&rx_sa->lock);
2156 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2157 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2163 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2165 struct nlattr **attrs = info->attrs;
2166 struct net_device *dev;
2167 struct macsec_secy *secy;
2168 struct macsec_rx_sc *rx_sc;
2169 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2171 if (!attrs[MACSEC_ATTR_IFINDEX])
2174 if (parse_rxsc_config(attrs, tb_rxsc))
2177 if (!validate_add_rxsc(tb_rxsc))
2181 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2182 if (IS_ERR(rx_sc)) {
2184 return PTR_ERR(rx_sc);
2187 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2188 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2190 if (rx_sc->active != new)
2191 secy->n_rx_sc += new ? 1 : -1;
2193 rx_sc->active = new;
2201 static int copy_tx_sa_stats(struct sk_buff *skb,
2202 struct macsec_tx_sa_stats __percpu *pstats)
2204 struct macsec_tx_sa_stats sum = {0, };
2207 for_each_possible_cpu(cpu) {
2208 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2210 sum.OutPktsProtected += stats->OutPktsProtected;
2211 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2214 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2215 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2221 static int copy_rx_sa_stats(struct sk_buff *skb,
2222 struct macsec_rx_sa_stats __percpu *pstats)
2224 struct macsec_rx_sa_stats sum = {0, };
2227 for_each_possible_cpu(cpu) {
2228 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2230 sum.InPktsOK += stats->InPktsOK;
2231 sum.InPktsInvalid += stats->InPktsInvalid;
2232 sum.InPktsNotValid += stats->InPktsNotValid;
2233 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2234 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2237 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2238 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2239 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2240 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2241 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2247 static int copy_rx_sc_stats(struct sk_buff *skb,
2248 struct pcpu_rx_sc_stats __percpu *pstats)
2250 struct macsec_rx_sc_stats sum = {0, };
2253 for_each_possible_cpu(cpu) {
2254 const struct pcpu_rx_sc_stats *stats;
2255 struct macsec_rx_sc_stats tmp;
2258 stats = per_cpu_ptr(pstats, cpu);
2260 start = u64_stats_fetch_begin_irq(&stats->syncp);
2261 memcpy(&tmp, &stats->stats, sizeof(tmp));
2262 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2264 sum.InOctetsValidated += tmp.InOctetsValidated;
2265 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2266 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2267 sum.InPktsDelayed += tmp.InPktsDelayed;
2268 sum.InPktsOK += tmp.InPktsOK;
2269 sum.InPktsInvalid += tmp.InPktsInvalid;
2270 sum.InPktsLate += tmp.InPktsLate;
2271 sum.InPktsNotValid += tmp.InPktsNotValid;
2272 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2273 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2276 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2277 sum.InOctetsValidated,
2278 MACSEC_RXSC_STATS_ATTR_PAD) ||
2279 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2280 sum.InOctetsDecrypted,
2281 MACSEC_RXSC_STATS_ATTR_PAD) ||
2282 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2283 sum.InPktsUnchecked,
2284 MACSEC_RXSC_STATS_ATTR_PAD) ||
2285 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2287 MACSEC_RXSC_STATS_ATTR_PAD) ||
2288 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2290 MACSEC_RXSC_STATS_ATTR_PAD) ||
2291 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2293 MACSEC_RXSC_STATS_ATTR_PAD) ||
2294 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2296 MACSEC_RXSC_STATS_ATTR_PAD) ||
2297 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2299 MACSEC_RXSC_STATS_ATTR_PAD) ||
2300 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2301 sum.InPktsNotUsingSA,
2302 MACSEC_RXSC_STATS_ATTR_PAD) ||
2303 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2305 MACSEC_RXSC_STATS_ATTR_PAD))
2311 static int copy_tx_sc_stats(struct sk_buff *skb,
2312 struct pcpu_tx_sc_stats __percpu *pstats)
2314 struct macsec_tx_sc_stats sum = {0, };
2317 for_each_possible_cpu(cpu) {
2318 const struct pcpu_tx_sc_stats *stats;
2319 struct macsec_tx_sc_stats tmp;
2322 stats = per_cpu_ptr(pstats, cpu);
2324 start = u64_stats_fetch_begin_irq(&stats->syncp);
2325 memcpy(&tmp, &stats->stats, sizeof(tmp));
2326 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2328 sum.OutPktsProtected += tmp.OutPktsProtected;
2329 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2330 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2331 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2334 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2335 sum.OutPktsProtected,
2336 MACSEC_TXSC_STATS_ATTR_PAD) ||
2337 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2338 sum.OutPktsEncrypted,
2339 MACSEC_TXSC_STATS_ATTR_PAD) ||
2340 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2341 sum.OutOctetsProtected,
2342 MACSEC_TXSC_STATS_ATTR_PAD) ||
2343 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2344 sum.OutOctetsEncrypted,
2345 MACSEC_TXSC_STATS_ATTR_PAD))
2351 static int copy_secy_stats(struct sk_buff *skb,
2352 struct pcpu_secy_stats __percpu *pstats)
2354 struct macsec_dev_stats sum = {0, };
2357 for_each_possible_cpu(cpu) {
2358 const struct pcpu_secy_stats *stats;
2359 struct macsec_dev_stats tmp;
2362 stats = per_cpu_ptr(pstats, cpu);
2364 start = u64_stats_fetch_begin_irq(&stats->syncp);
2365 memcpy(&tmp, &stats->stats, sizeof(tmp));
2366 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2368 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2369 sum.InPktsUntagged += tmp.InPktsUntagged;
2370 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2371 sum.InPktsNoTag += tmp.InPktsNoTag;
2372 sum.InPktsBadTag += tmp.InPktsBadTag;
2373 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2374 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2375 sum.InPktsOverrun += tmp.InPktsOverrun;
2378 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2379 sum.OutPktsUntagged,
2380 MACSEC_SECY_STATS_ATTR_PAD) ||
2381 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2383 MACSEC_SECY_STATS_ATTR_PAD) ||
2384 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2386 MACSEC_SECY_STATS_ATTR_PAD) ||
2387 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2389 MACSEC_SECY_STATS_ATTR_PAD) ||
2390 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2392 MACSEC_SECY_STATS_ATTR_PAD) ||
2393 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2394 sum.InPktsUnknownSCI,
2395 MACSEC_SECY_STATS_ATTR_PAD) ||
2396 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2398 MACSEC_SECY_STATS_ATTR_PAD) ||
2399 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2401 MACSEC_SECY_STATS_ATTR_PAD))
2407 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2409 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2410 struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
2416 switch (secy->key_len) {
2417 case MACSEC_GCM_AES_128_SAK_LEN:
2418 csid = MACSEC_DEFAULT_CIPHER_ID;
2420 case MACSEC_GCM_AES_256_SAK_LEN:
2421 csid = MACSEC_CIPHER_ID_GCM_AES_256;
2427 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2428 MACSEC_SECY_ATTR_PAD) ||
2429 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2430 csid, MACSEC_SECY_ATTR_PAD) ||
2431 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2432 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2433 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2434 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2435 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2436 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2437 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2438 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2439 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2440 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2443 if (secy->replay_protect) {
2444 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2448 nla_nest_end(skb, secy_nest);
2452 nla_nest_cancel(skb, secy_nest);
2456 static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
2457 struct sk_buff *skb, struct netlink_callback *cb)
2459 struct macsec_rx_sc *rx_sc;
2460 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2461 struct nlattr *txsa_list, *rxsc_list;
2464 struct nlattr *attr;
2466 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2467 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2471 genl_dump_check_consistent(cb, hdr);
2473 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2474 goto nla_put_failure;
2476 if (nla_put_secy(secy, skb))
2477 goto nla_put_failure;
2479 attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
2481 goto nla_put_failure;
2482 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2483 nla_nest_cancel(skb, attr);
2484 goto nla_put_failure;
2486 nla_nest_end(skb, attr);
2488 attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
2490 goto nla_put_failure;
2491 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2492 nla_nest_cancel(skb, attr);
2493 goto nla_put_failure;
2495 nla_nest_end(skb, attr);
2497 txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
2499 goto nla_put_failure;
2500 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2501 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2502 struct nlattr *txsa_nest;
2507 txsa_nest = nla_nest_start(skb, j++);
2509 nla_nest_cancel(skb, txsa_list);
2510 goto nla_put_failure;
2513 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2514 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2515 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2516 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2517 nla_nest_cancel(skb, txsa_nest);
2518 nla_nest_cancel(skb, txsa_list);
2519 goto nla_put_failure;
2522 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2524 nla_nest_cancel(skb, txsa_nest);
2525 nla_nest_cancel(skb, txsa_list);
2526 goto nla_put_failure;
2528 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2529 nla_nest_cancel(skb, attr);
2530 nla_nest_cancel(skb, txsa_nest);
2531 nla_nest_cancel(skb, txsa_list);
2532 goto nla_put_failure;
2534 nla_nest_end(skb, attr);
2536 nla_nest_end(skb, txsa_nest);
2538 nla_nest_end(skb, txsa_list);
2540 rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
2542 goto nla_put_failure;
2545 for_each_rxsc_rtnl(secy, rx_sc) {
2547 struct nlattr *rxsa_list;
2548 struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
2551 nla_nest_cancel(skb, rxsc_list);
2552 goto nla_put_failure;
2555 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2556 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2557 MACSEC_RXSC_ATTR_PAD)) {
2558 nla_nest_cancel(skb, rxsc_nest);
2559 nla_nest_cancel(skb, rxsc_list);
2560 goto nla_put_failure;
2563 attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
2565 nla_nest_cancel(skb, rxsc_nest);
2566 nla_nest_cancel(skb, rxsc_list);
2567 goto nla_put_failure;
2569 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2570 nla_nest_cancel(skb, attr);
2571 nla_nest_cancel(skb, rxsc_nest);
2572 nla_nest_cancel(skb, rxsc_list);
2573 goto nla_put_failure;
2575 nla_nest_end(skb, attr);
2577 rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
2579 nla_nest_cancel(skb, rxsc_nest);
2580 nla_nest_cancel(skb, rxsc_list);
2581 goto nla_put_failure;
2584 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2585 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2586 struct nlattr *rxsa_nest;
2591 rxsa_nest = nla_nest_start(skb, k++);
2593 nla_nest_cancel(skb, rxsa_list);
2594 nla_nest_cancel(skb, rxsc_nest);
2595 nla_nest_cancel(skb, rxsc_list);
2596 goto nla_put_failure;
2599 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2601 nla_nest_cancel(skb, rxsa_list);
2602 nla_nest_cancel(skb, rxsc_nest);
2603 nla_nest_cancel(skb, rxsc_list);
2604 goto nla_put_failure;
2606 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2607 nla_nest_cancel(skb, attr);
2608 nla_nest_cancel(skb, rxsa_list);
2609 nla_nest_cancel(skb, rxsc_nest);
2610 nla_nest_cancel(skb, rxsc_list);
2611 goto nla_put_failure;
2613 nla_nest_end(skb, attr);
2615 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2616 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2617 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2618 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2619 nla_nest_cancel(skb, rxsa_nest);
2620 nla_nest_cancel(skb, rxsc_nest);
2621 nla_nest_cancel(skb, rxsc_list);
2622 goto nla_put_failure;
2624 nla_nest_end(skb, rxsa_nest);
2627 nla_nest_end(skb, rxsa_list);
2628 nla_nest_end(skb, rxsc_nest);
2631 nla_nest_end(skb, rxsc_list);
2633 genlmsg_end(skb, hdr);
2638 genlmsg_cancel(skb, hdr);
2642 static int macsec_generation = 1; /* protected by RTNL */
2644 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2646 struct net *net = sock_net(skb->sk);
2647 struct net_device *dev;
2650 dev_idx = cb->args[0];
2655 cb->seq = macsec_generation;
2657 for_each_netdev(net, dev) {
2658 struct macsec_secy *secy;
2663 if (!netif_is_macsec(dev))
2666 secy = &macsec_priv(dev)->secy;
2667 if (dump_secy(secy, dev, skb, cb) < 0)
2679 static const struct genl_ops macsec_genl_ops[] = {
2681 .cmd = MACSEC_CMD_GET_TXSC,
2682 .dumpit = macsec_dump_txsc,
2683 .policy = macsec_genl_policy,
2686 .cmd = MACSEC_CMD_ADD_RXSC,
2687 .doit = macsec_add_rxsc,
2688 .policy = macsec_genl_policy,
2689 .flags = GENL_ADMIN_PERM,
2692 .cmd = MACSEC_CMD_DEL_RXSC,
2693 .doit = macsec_del_rxsc,
2694 .policy = macsec_genl_policy,
2695 .flags = GENL_ADMIN_PERM,
2698 .cmd = MACSEC_CMD_UPD_RXSC,
2699 .doit = macsec_upd_rxsc,
2700 .policy = macsec_genl_policy,
2701 .flags = GENL_ADMIN_PERM,
2704 .cmd = MACSEC_CMD_ADD_TXSA,
2705 .doit = macsec_add_txsa,
2706 .policy = macsec_genl_policy,
2707 .flags = GENL_ADMIN_PERM,
2710 .cmd = MACSEC_CMD_DEL_TXSA,
2711 .doit = macsec_del_txsa,
2712 .policy = macsec_genl_policy,
2713 .flags = GENL_ADMIN_PERM,
2716 .cmd = MACSEC_CMD_UPD_TXSA,
2717 .doit = macsec_upd_txsa,
2718 .policy = macsec_genl_policy,
2719 .flags = GENL_ADMIN_PERM,
2722 .cmd = MACSEC_CMD_ADD_RXSA,
2723 .doit = macsec_add_rxsa,
2724 .policy = macsec_genl_policy,
2725 .flags = GENL_ADMIN_PERM,
2728 .cmd = MACSEC_CMD_DEL_RXSA,
2729 .doit = macsec_del_rxsa,
2730 .policy = macsec_genl_policy,
2731 .flags = GENL_ADMIN_PERM,
2734 .cmd = MACSEC_CMD_UPD_RXSA,
2735 .doit = macsec_upd_rxsa,
2736 .policy = macsec_genl_policy,
2737 .flags = GENL_ADMIN_PERM,
2741 static struct genl_family macsec_fam __ro_after_init = {
2742 .name = MACSEC_GENL_NAME,
2744 .version = MACSEC_GENL_VERSION,
2745 .maxattr = MACSEC_ATTR_MAX,
2747 .module = THIS_MODULE,
2748 .ops = macsec_genl_ops,
2749 .n_ops = ARRAY_SIZE(macsec_genl_ops),
2752 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2753 struct net_device *dev)
2755 struct macsec_dev *macsec = netdev_priv(dev);
2756 struct macsec_secy *secy = &macsec->secy;
2757 struct pcpu_secy_stats *secy_stats;
2761 if (!secy->protect_frames) {
2762 secy_stats = this_cpu_ptr(macsec->stats);
2763 u64_stats_update_begin(&secy_stats->syncp);
2764 secy_stats->stats.OutPktsUntagged++;
2765 u64_stats_update_end(&secy_stats->syncp);
2766 skb->dev = macsec->real_dev;
2768 ret = dev_queue_xmit(skb);
2769 count_tx(dev, ret, len);
2773 if (!secy->operational) {
2775 DEV_STATS_INC(dev, tx_dropped);
2776 return NETDEV_TX_OK;
2780 skb = macsec_encrypt(skb, dev);
2782 if (PTR_ERR(skb) != -EINPROGRESS)
2783 DEV_STATS_INC(dev, tx_dropped);
2784 return NETDEV_TX_OK;
2787 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2789 macsec_encrypt_finish(skb, dev);
2790 ret = dev_queue_xmit(skb);
2791 count_tx(dev, ret, len);
2795 #define MACSEC_FEATURES \
2796 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
2797 static struct lock_class_key macsec_netdev_addr_lock_key;
2799 static int macsec_dev_init(struct net_device *dev)
2801 struct macsec_dev *macsec = macsec_priv(dev);
2802 struct net_device *real_dev = macsec->real_dev;
2805 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2809 err = gro_cells_init(&macsec->gro_cells, dev);
2811 free_percpu(dev->tstats);
2815 dev->features = real_dev->features & MACSEC_FEATURES;
2816 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2818 dev->needed_headroom = real_dev->needed_headroom +
2819 MACSEC_NEEDED_HEADROOM;
2820 dev->needed_tailroom = real_dev->needed_tailroom +
2821 MACSEC_NEEDED_TAILROOM;
2823 if (is_zero_ether_addr(dev->dev_addr))
2824 eth_hw_addr_inherit(dev, real_dev);
2825 if (is_zero_ether_addr(dev->broadcast))
2826 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2831 static void macsec_dev_uninit(struct net_device *dev)
2833 struct macsec_dev *macsec = macsec_priv(dev);
2835 gro_cells_destroy(&macsec->gro_cells);
2836 free_percpu(dev->tstats);
2839 static netdev_features_t macsec_fix_features(struct net_device *dev,
2840 netdev_features_t features)
2842 struct macsec_dev *macsec = macsec_priv(dev);
2843 struct net_device *real_dev = macsec->real_dev;
2845 features &= (real_dev->features & MACSEC_FEATURES) |
2846 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2847 features |= NETIF_F_LLTX;
2852 static int macsec_dev_open(struct net_device *dev)
2854 struct macsec_dev *macsec = macsec_priv(dev);
2855 struct net_device *real_dev = macsec->real_dev;
2858 err = dev_uc_add(real_dev, dev->dev_addr);
2862 if (dev->flags & IFF_ALLMULTI) {
2863 err = dev_set_allmulti(real_dev, 1);
2868 if (dev->flags & IFF_PROMISC) {
2869 err = dev_set_promiscuity(real_dev, 1);
2871 goto clear_allmulti;
2874 if (netif_carrier_ok(real_dev))
2875 netif_carrier_on(dev);
2879 if (dev->flags & IFF_ALLMULTI)
2880 dev_set_allmulti(real_dev, -1);
2882 dev_uc_del(real_dev, dev->dev_addr);
2883 netif_carrier_off(dev);
2887 static int macsec_dev_stop(struct net_device *dev)
2889 struct macsec_dev *macsec = macsec_priv(dev);
2890 struct net_device *real_dev = macsec->real_dev;
2892 netif_carrier_off(dev);
2894 dev_mc_unsync(real_dev, dev);
2895 dev_uc_unsync(real_dev, dev);
2897 if (dev->flags & IFF_ALLMULTI)
2898 dev_set_allmulti(real_dev, -1);
2900 if (dev->flags & IFF_PROMISC)
2901 dev_set_promiscuity(real_dev, -1);
2903 dev_uc_del(real_dev, dev->dev_addr);
2908 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2910 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2912 if (!(dev->flags & IFF_UP))
2915 if (change & IFF_ALLMULTI)
2916 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2918 if (change & IFF_PROMISC)
2919 dev_set_promiscuity(real_dev,
2920 dev->flags & IFF_PROMISC ? 1 : -1);
2923 static void macsec_dev_set_rx_mode(struct net_device *dev)
2925 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2927 dev_mc_sync(real_dev, dev);
2928 dev_uc_sync(real_dev, dev);
2931 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
2933 return make_sci(dev->dev_addr, port);
2936 static int macsec_set_mac_address(struct net_device *dev, void *p)
2938 struct macsec_dev *macsec = macsec_priv(dev);
2939 struct net_device *real_dev = macsec->real_dev;
2940 struct sockaddr *addr = p;
2943 if (!is_valid_ether_addr(addr->sa_data))
2944 return -EADDRNOTAVAIL;
2946 if (!(dev->flags & IFF_UP))
2949 err = dev_uc_add(real_dev, addr->sa_data);
2953 dev_uc_del(real_dev, dev->dev_addr);
2956 ether_addr_copy(dev->dev_addr, addr->sa_data);
2957 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
2961 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2963 struct macsec_dev *macsec = macsec_priv(dev);
2964 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2966 if (macsec->real_dev->mtu - extra < new_mtu)
2974 static void macsec_get_stats64(struct net_device *dev,
2975 struct rtnl_link_stats64 *s)
2982 for_each_possible_cpu(cpu) {
2983 struct pcpu_sw_netstats *stats;
2984 struct pcpu_sw_netstats tmp;
2987 stats = per_cpu_ptr(dev->tstats, cpu);
2989 start = u64_stats_fetch_begin_irq(&stats->syncp);
2990 tmp.rx_packets = stats->rx_packets;
2991 tmp.rx_bytes = stats->rx_bytes;
2992 tmp.tx_packets = stats->tx_packets;
2993 tmp.tx_bytes = stats->tx_bytes;
2994 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2996 s->rx_packets += tmp.rx_packets;
2997 s->rx_bytes += tmp.rx_bytes;
2998 s->tx_packets += tmp.tx_packets;
2999 s->tx_bytes += tmp.tx_bytes;
3002 s->rx_dropped = DEV_STATS_READ(dev, rx_dropped);
3003 s->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
3004 s->rx_errors = DEV_STATS_READ(dev, rx_errors);
3007 static int macsec_get_iflink(const struct net_device *dev)
3009 return macsec_priv(dev)->real_dev->ifindex;
3013 static int macsec_get_nest_level(struct net_device *dev)
3015 return macsec_priv(dev)->nest_level;
3019 static const struct net_device_ops macsec_netdev_ops = {
3020 .ndo_init = macsec_dev_init,
3021 .ndo_uninit = macsec_dev_uninit,
3022 .ndo_open = macsec_dev_open,
3023 .ndo_stop = macsec_dev_stop,
3024 .ndo_fix_features = macsec_fix_features,
3025 .ndo_change_mtu = macsec_change_mtu,
3026 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
3027 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
3028 .ndo_set_mac_address = macsec_set_mac_address,
3029 .ndo_start_xmit = macsec_start_xmit,
3030 .ndo_get_stats64 = macsec_get_stats64,
3031 .ndo_get_iflink = macsec_get_iflink,
3032 .ndo_get_lock_subclass = macsec_get_nest_level,
3035 static const struct device_type macsec_type = {
3039 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3040 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3041 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3042 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3043 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3044 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3045 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3046 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3047 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3048 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3049 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
3050 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3051 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3052 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3055 static void macsec_free_netdev(struct net_device *dev)
3057 struct macsec_dev *macsec = macsec_priv(dev);
3059 free_percpu(macsec->stats);
3060 free_percpu(macsec->secy.tx_sc.stats);
3064 static void macsec_setup(struct net_device *dev)
3068 dev->max_mtu = ETH_MAX_MTU;
3069 dev->priv_flags |= IFF_NO_QUEUE;
3070 dev->netdev_ops = &macsec_netdev_ops;
3071 dev->needs_free_netdev = true;
3072 dev->priv_destructor = macsec_free_netdev;
3073 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3075 eth_zero_addr(dev->broadcast);
3078 static int macsec_changelink_common(struct net_device *dev,
3079 struct nlattr *data[])
3081 struct macsec_secy *secy;
3082 struct macsec_tx_sc *tx_sc;
3084 secy = &macsec_priv(dev)->secy;
3085 tx_sc = &secy->tx_sc;
3087 if (data[IFLA_MACSEC_ENCODING_SA]) {
3088 struct macsec_tx_sa *tx_sa;
3090 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3091 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3093 secy->operational = tx_sa && tx_sa->active;
3096 if (data[IFLA_MACSEC_WINDOW])
3097 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3099 if (data[IFLA_MACSEC_ENCRYPT])
3100 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3102 if (data[IFLA_MACSEC_PROTECT])
3103 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3105 if (data[IFLA_MACSEC_INC_SCI])
3106 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3108 if (data[IFLA_MACSEC_ES])
3109 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3111 if (data[IFLA_MACSEC_SCB])
3112 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3114 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3115 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3117 if (data[IFLA_MACSEC_VALIDATION])
3118 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3120 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3121 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3122 case MACSEC_CIPHER_ID_GCM_AES_128:
3123 case MACSEC_DEFAULT_CIPHER_ID:
3124 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3126 case MACSEC_CIPHER_ID_GCM_AES_256:
3127 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3137 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3138 struct nlattr *data[],
3139 struct netlink_ext_ack *extack)
3144 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3145 data[IFLA_MACSEC_ICV_LEN] ||
3146 data[IFLA_MACSEC_SCI] ||
3147 data[IFLA_MACSEC_PORT])
3150 return macsec_changelink_common(dev, data);
3153 static void macsec_del_dev(struct macsec_dev *macsec)
3157 while (macsec->secy.rx_sc) {
3158 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3160 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3164 for (i = 0; i < MACSEC_NUM_AN; i++) {
3165 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3168 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3174 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3176 struct macsec_dev *macsec = macsec_priv(dev);
3177 struct net_device *real_dev = macsec->real_dev;
3179 unregister_netdevice_queue(dev, head);
3180 list_del_rcu(&macsec->secys);
3181 macsec_del_dev(macsec);
3182 netdev_upper_dev_unlink(real_dev, dev);
3184 macsec_generation++;
3187 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3189 struct macsec_dev *macsec = macsec_priv(dev);
3190 struct net_device *real_dev = macsec->real_dev;
3191 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3193 macsec_common_dellink(dev, head);
3195 if (list_empty(&rxd->secys)) {
3196 netdev_rx_handler_unregister(real_dev);
3201 static int register_macsec_dev(struct net_device *real_dev,
3202 struct net_device *dev)
3204 struct macsec_dev *macsec = macsec_priv(dev);
3205 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3210 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3214 INIT_LIST_HEAD(&rxd->secys);
3216 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3224 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3228 static bool sci_exists(struct net_device *dev, sci_t sci)
3230 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3231 struct macsec_dev *macsec;
3233 list_for_each_entry(macsec, &rxd->secys, secys) {
3234 if (macsec->secy.sci == sci)
3241 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3243 struct macsec_dev *macsec = macsec_priv(dev);
3244 struct macsec_secy *secy = &macsec->secy;
3246 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3250 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3251 if (!secy->tx_sc.stats) {
3252 free_percpu(macsec->stats);
3256 if (sci == MACSEC_UNDEF_SCI)
3257 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3260 secy->operational = true;
3261 secy->key_len = DEFAULT_SAK_LEN;
3262 secy->icv_len = icv_len;
3263 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3264 secy->protect_frames = true;
3265 secy->replay_protect = false;
3268 secy->tx_sc.active = true;
3269 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3270 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3271 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3272 secy->tx_sc.end_station = false;
3273 secy->tx_sc.scb = false;
3278 static int macsec_newlink(struct net *net, struct net_device *dev,
3279 struct nlattr *tb[], struct nlattr *data[],
3280 struct netlink_ext_ack *extack)
3282 struct macsec_dev *macsec = macsec_priv(dev);
3283 rx_handler_func_t *rx_handler;
3284 u8 icv_len = DEFAULT_ICV_LEN;
3285 struct net_device *real_dev;
3291 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3294 if (real_dev->type != ARPHRD_ETHER)
3297 dev->priv_flags |= IFF_MACSEC;
3299 macsec->real_dev = real_dev;
3301 /* send_sci must be set to true when transmit sci explicitly is set */
3302 if ((data && data[IFLA_MACSEC_SCI]) &&
3303 (data && data[IFLA_MACSEC_INC_SCI])) {
3304 u8 send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3310 if (data && data[IFLA_MACSEC_ICV_LEN])
3311 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3312 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3318 rx_handler = rtnl_dereference(real_dev->rx_handler);
3319 if (rx_handler && rx_handler != macsec_handle_frame)
3322 err = register_netdevice(dev);
3326 macsec->nest_level = dev_get_nest_level(real_dev) + 1;
3327 netdev_lockdep_set_classes(dev);
3328 lockdep_set_class_and_subclass(&dev->addr_list_lock,
3329 &macsec_netdev_addr_lock_key,
3330 macsec_get_nest_level(dev));
3332 err = netdev_upper_dev_link(real_dev, dev, extack);
3336 /* need to be already registered so that ->init has run and
3337 * the MAC addr is set
3339 if (data && data[IFLA_MACSEC_SCI])
3340 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3341 else if (data && data[IFLA_MACSEC_PORT])
3342 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3344 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3346 if (rx_handler && sci_exists(real_dev, sci)) {
3351 err = macsec_add_dev(dev, sci, icv_len);
3356 err = macsec_changelink_common(dev, data);
3361 err = register_macsec_dev(real_dev, dev);
3365 netif_stacked_transfer_operstate(real_dev, dev);
3366 linkwatch_fire_event(dev);
3368 macsec_generation++;
3373 macsec_del_dev(macsec);
3375 netdev_upper_dev_unlink(real_dev, dev);
3377 unregister_netdevice(dev);
3381 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3382 struct netlink_ext_ack *extack)
3384 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3385 u8 icv_len = DEFAULT_ICV_LEN;
3392 if (data[IFLA_MACSEC_CIPHER_SUITE])
3393 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3395 if (data[IFLA_MACSEC_ICV_LEN]) {
3396 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3397 if (icv_len != DEFAULT_ICV_LEN) {
3398 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3399 struct crypto_aead *dummy_tfm;
3401 dummy_tfm = macsec_alloc_tfm(dummy_key,
3404 if (IS_ERR(dummy_tfm))
3405 return PTR_ERR(dummy_tfm);
3406 crypto_free_aead(dummy_tfm);
3411 case MACSEC_CIPHER_ID_GCM_AES_128:
3412 case MACSEC_CIPHER_ID_GCM_AES_256:
3413 case MACSEC_DEFAULT_CIPHER_ID:
3414 if (icv_len < MACSEC_MIN_ICV_LEN ||
3415 icv_len > MACSEC_STD_ICV_LEN)
3422 if (data[IFLA_MACSEC_ENCODING_SA]) {
3423 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3427 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3428 flag < IFLA_MACSEC_VALIDATION;
3431 if (nla_get_u8(data[flag]) > 1)
3436 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3437 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3438 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3440 if ((sci && (scb || es)) || (scb && es))
3443 if (data[IFLA_MACSEC_VALIDATION] &&
3444 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3447 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3448 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3449 !data[IFLA_MACSEC_WINDOW])
3455 static struct net *macsec_get_link_net(const struct net_device *dev)
3457 return dev_net(macsec_priv(dev)->real_dev);
3460 static size_t macsec_get_size(const struct net_device *dev)
3462 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
3463 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
3464 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
3465 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
3466 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
3467 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
3468 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
3469 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
3470 nla_total_size(1) + /* IFLA_MACSEC_ES */
3471 nla_total_size(1) + /* IFLA_MACSEC_SCB */
3472 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
3473 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
3477 static int macsec_fill_info(struct sk_buff *skb,
3478 const struct net_device *dev)
3480 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3481 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3484 switch (secy->key_len) {
3485 case MACSEC_GCM_AES_128_SAK_LEN:
3486 csid = MACSEC_DEFAULT_CIPHER_ID;
3488 case MACSEC_GCM_AES_256_SAK_LEN:
3489 csid = MACSEC_CIPHER_ID_GCM_AES_256;
3492 goto nla_put_failure;
3495 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3497 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3498 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3499 csid, IFLA_MACSEC_PAD) ||
3500 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3501 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3502 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3503 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3504 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3505 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3506 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3507 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3509 goto nla_put_failure;
3511 if (secy->replay_protect) {
3512 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3513 goto nla_put_failure;
3522 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3524 .priv_size = sizeof(struct macsec_dev),
3525 .maxtype = IFLA_MACSEC_MAX,
3526 .policy = macsec_rtnl_policy,
3527 .setup = macsec_setup,
3528 .validate = macsec_validate_attr,
3529 .newlink = macsec_newlink,
3530 .changelink = macsec_changelink,
3531 .dellink = macsec_dellink,
3532 .get_size = macsec_get_size,
3533 .fill_info = macsec_fill_info,
3534 .get_link_net = macsec_get_link_net,
3537 static bool is_macsec_master(struct net_device *dev)
3539 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3542 static int macsec_notify(struct notifier_block *this, unsigned long event,
3545 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3548 if (!is_macsec_master(real_dev))
3554 case NETDEV_CHANGE: {
3555 struct macsec_dev *m, *n;
3556 struct macsec_rxh_data *rxd;
3558 rxd = macsec_data_rtnl(real_dev);
3559 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3560 struct net_device *dev = m->secy.netdev;
3562 netif_stacked_transfer_operstate(real_dev, dev);
3566 case NETDEV_UNREGISTER: {
3567 struct macsec_dev *m, *n;
3568 struct macsec_rxh_data *rxd;
3570 rxd = macsec_data_rtnl(real_dev);
3571 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3572 macsec_common_dellink(m->secy.netdev, &head);
3575 netdev_rx_handler_unregister(real_dev);
3578 unregister_netdevice_many(&head);
3581 case NETDEV_CHANGEMTU: {
3582 struct macsec_dev *m;
3583 struct macsec_rxh_data *rxd;
3585 rxd = macsec_data_rtnl(real_dev);
3586 list_for_each_entry(m, &rxd->secys, secys) {
3587 struct net_device *dev = m->secy.netdev;
3588 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3589 macsec_extra_len(true));
3592 dev_set_mtu(dev, mtu);
3600 static struct notifier_block macsec_notifier = {
3601 .notifier_call = macsec_notify,
3604 static int __init macsec_init(void)
3608 pr_info("MACsec IEEE 802.1AE\n");
3609 err = register_netdevice_notifier(&macsec_notifier);
3613 err = rtnl_link_register(&macsec_link_ops);
3617 err = genl_register_family(&macsec_fam);
3624 rtnl_link_unregister(&macsec_link_ops);
3626 unregister_netdevice_notifier(&macsec_notifier);
3630 static void __exit macsec_exit(void)
3632 genl_unregister_family(&macsec_fam);
3633 rtnl_link_unregister(&macsec_link_ops);
3634 unregister_netdevice_notifier(&macsec_notifier);
3638 module_init(macsec_init);
3639 module_exit(macsec_exit);
3641 MODULE_ALIAS_RTNL_LINK("macsec");
3642 MODULE_ALIAS_GENL_FAMILY("macsec");
3644 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3645 MODULE_LICENSE("GPL v2");