1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/net/macsec.c - MACsec device
5 * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
8 #include <linux/types.h>
9 #include <linux/skbuff.h>
10 #include <linux/socket.h>
11 #include <linux/module.h>
12 #include <crypto/aead.h>
13 #include <linux/etherdevice.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/refcount.h>
16 #include <net/genetlink.h>
18 #include <net/gro_cells.h>
20 #include <uapi/linux/if_macsec.h>
22 typedef u64 __bitwise sci_t;
24 #define MACSEC_SCI_LEN 8
26 /* SecTAG length = macsec_eth_header without the optional SCI */
27 #define MACSEC_TAG_LEN 6
29 struct macsec_eth_header {
33 #if defined(__LITTLE_ENDIAN_BITFIELD)
36 #elif defined(__BIG_ENDIAN_BITFIELD)
40 #error "Please fix <asm/byteorder.h>"
43 u8 secure_channel_id[8]; /* optional */
46 #define MACSEC_TCI_VERSION 0x80
47 #define MACSEC_TCI_ES 0x40 /* end station */
48 #define MACSEC_TCI_SC 0x20 /* SCI present */
49 #define MACSEC_TCI_SCB 0x10 /* epon */
50 #define MACSEC_TCI_E 0x08 /* encryption */
51 #define MACSEC_TCI_C 0x04 /* changed text */
52 #define MACSEC_AN_MASK 0x03 /* association number */
53 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
55 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
56 #define MIN_NON_SHORT_LEN 48
58 #define GCM_AES_IV_LEN 12
59 #define DEFAULT_ICV_LEN 16
61 #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
63 #define for_each_rxsc(secy, sc) \
64 for (sc = rcu_dereference_bh(secy->rx_sc); \
66 sc = rcu_dereference_bh(sc->next))
67 #define for_each_rxsc_rtnl(secy, sc) \
68 for (sc = rtnl_dereference(secy->rx_sc); \
70 sc = rtnl_dereference(sc->next))
74 u8 secure_channel_id[8];
81 * struct macsec_key - SA key
82 * @id: user-provided key identifier
83 * @tfm: crypto struct, key storage
86 u8 id[MACSEC_KEYID_LEN];
87 struct crypto_aead *tfm;
90 struct macsec_rx_sc_stats {
91 __u64 InOctetsValidated;
92 __u64 InOctetsDecrypted;
93 __u64 InPktsUnchecked;
99 __u64 InPktsNotUsingSA;
100 __u64 InPktsUnusedSA;
103 struct macsec_rx_sa_stats {
106 __u32 InPktsNotValid;
107 __u32 InPktsNotUsingSA;
108 __u32 InPktsUnusedSA;
111 struct macsec_tx_sa_stats {
112 __u32 OutPktsProtected;
113 __u32 OutPktsEncrypted;
116 struct macsec_tx_sc_stats {
117 __u64 OutPktsProtected;
118 __u64 OutPktsEncrypted;
119 __u64 OutOctetsProtected;
120 __u64 OutOctetsEncrypted;
123 struct macsec_dev_stats {
124 __u64 OutPktsUntagged;
125 __u64 InPktsUntagged;
126 __u64 OutPktsTooLong;
129 __u64 InPktsUnknownSCI;
135 * struct macsec_rx_sa - receive secure association
137 * @next_pn: packet number expected for the next packet
138 * @lock: protects next_pn manipulations
139 * @key: key structure
140 * @stats: per-SA stats
142 struct macsec_rx_sa {
143 struct macsec_key key;
148 struct macsec_rx_sa_stats __percpu *stats;
149 struct macsec_rx_sc *sc;
153 struct pcpu_rx_sc_stats {
154 struct macsec_rx_sc_stats stats;
155 struct u64_stats_sync syncp;
159 * struct macsec_rx_sc - receive secure channel
160 * @sci: secure channel identifier for this SC
161 * @active: channel is active
162 * @sa: array of secure associations
163 * @stats: per-SC stats
165 struct macsec_rx_sc {
166 struct macsec_rx_sc __rcu *next;
169 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
170 struct pcpu_rx_sc_stats __percpu *stats;
172 struct rcu_head rcu_head;
176 * struct macsec_tx_sa - transmit secure association
178 * @next_pn: packet number to use for the next packet
179 * @lock: protects next_pn manipulations
180 * @key: key structure
181 * @stats: per-SA stats
183 struct macsec_tx_sa {
184 struct macsec_key key;
189 struct macsec_tx_sa_stats __percpu *stats;
193 struct pcpu_tx_sc_stats {
194 struct macsec_tx_sc_stats stats;
195 struct u64_stats_sync syncp;
199 * struct macsec_tx_sc - transmit secure channel
201 * @encoding_sa: association number of the SA currently in use
202 * @encrypt: encrypt packets on transmit, or authenticate only
203 * @send_sci: always include the SCI in the SecTAG
205 * @scb: single copy broadcast flag
206 * @sa: array of secure associations
207 * @stats: stats for this TXSC
209 struct macsec_tx_sc {
216 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
217 struct pcpu_tx_sc_stats __percpu *stats;
220 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
223 * struct macsec_secy - MACsec Security Entity
224 * @netdev: netdevice for this SecY
225 * @n_rx_sc: number of receive secure channels configured on this SecY
226 * @sci: secure channel identifier used for tx
227 * @key_len: length of keys used by the cipher suite
228 * @icv_len: length of ICV used by the cipher suite
229 * @validate_frames: validation mode
230 * @operational: MAC_Operational flag
231 * @protect_frames: enable protection for this SecY
232 * @replay_protect: enable packet number checks on receive
233 * @replay_window: size of the replay window
234 * @tx_sc: transmit secure channel
235 * @rx_sc: linked list of receive secure channels
238 struct net_device *netdev;
239 unsigned int n_rx_sc;
243 enum macsec_validation_type validate_frames;
248 struct macsec_tx_sc tx_sc;
249 struct macsec_rx_sc __rcu *rx_sc;
252 struct pcpu_secy_stats {
253 struct macsec_dev_stats stats;
254 struct u64_stats_sync syncp;
258 * struct macsec_dev - private data
260 * @real_dev: pointer to underlying netdevice
261 * @stats: MACsec device stats
262 * @secys: linked list of SecY's on the underlying device
265 struct macsec_secy secy;
266 struct net_device *real_dev;
267 struct pcpu_secy_stats __percpu *stats;
268 struct list_head secys;
269 struct gro_cells gro_cells;
270 unsigned int nest_level;
274 * struct macsec_rxh_data - rx_handler private argument
275 * @secys: linked list of SecY's on this underlying device
277 struct macsec_rxh_data {
278 struct list_head secys;
281 static struct macsec_dev *macsec_priv(const struct net_device *dev)
283 return (struct macsec_dev *)netdev_priv(dev);
286 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
288 return rcu_dereference_bh(dev->rx_handler_data);
291 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
293 return rtnl_dereference(dev->rx_handler_data);
297 struct aead_request *req;
299 struct macsec_tx_sa *tx_sa;
300 struct macsec_rx_sa *rx_sa;
307 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
309 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
311 if (!sa || !sa->active)
314 if (!refcount_inc_not_zero(&sa->refcnt))
320 static void free_rx_sc_rcu(struct rcu_head *head)
322 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
324 free_percpu(rx_sc->stats);
328 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
330 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
333 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
335 if (refcount_dec_and_test(&sc->refcnt))
336 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
339 static void free_rxsa(struct rcu_head *head)
341 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
343 crypto_free_aead(sa->key.tfm);
344 free_percpu(sa->stats);
348 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
350 if (refcount_dec_and_test(&sa->refcnt))
351 call_rcu(&sa->rcu, free_rxsa);
354 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
356 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
358 if (!sa || !sa->active)
361 if (!refcount_inc_not_zero(&sa->refcnt))
367 static void free_txsa(struct rcu_head *head)
369 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
371 crypto_free_aead(sa->key.tfm);
372 free_percpu(sa->stats);
376 static void macsec_txsa_put(struct macsec_tx_sa *sa)
378 if (refcount_dec_and_test(&sa->refcnt))
379 call_rcu(&sa->rcu, free_txsa);
382 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
384 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
385 return (struct macsec_cb *)skb->cb;
388 #define MACSEC_PORT_ES (htons(0x0001))
389 #define MACSEC_PORT_SCB (0x0000)
390 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
392 #define MACSEC_GCM_AES_128_SAK_LEN 16
393 #define MACSEC_GCM_AES_256_SAK_LEN 32
395 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
396 #define DEFAULT_SEND_SCI true
397 #define DEFAULT_ENCRYPT false
398 #define DEFAULT_ENCODING_SA 0
400 static bool send_sci(const struct macsec_secy *secy)
402 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
404 return tx_sc->send_sci ||
405 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
408 static sci_t make_sci(u8 *addr, __be16 port)
412 memcpy(&sci, addr, ETH_ALEN);
413 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
418 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
423 memcpy(&sci, hdr->secure_channel_id,
424 sizeof(hdr->secure_channel_id));
426 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
431 static unsigned int macsec_sectag_len(bool sci_present)
433 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
436 static unsigned int macsec_hdr_len(bool sci_present)
438 return macsec_sectag_len(sci_present) + ETH_HLEN;
441 static unsigned int macsec_extra_len(bool sci_present)
443 return macsec_sectag_len(sci_present) + sizeof(__be16);
446 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
447 static void macsec_fill_sectag(struct macsec_eth_header *h,
448 const struct macsec_secy *secy, u32 pn,
451 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
453 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
454 h->eth.h_proto = htons(ETH_P_MACSEC);
457 h->tci_an |= MACSEC_TCI_SC;
458 memcpy(&h->secure_channel_id, &secy->sci,
459 sizeof(h->secure_channel_id));
461 if (tx_sc->end_station)
462 h->tci_an |= MACSEC_TCI_ES;
464 h->tci_an |= MACSEC_TCI_SCB;
467 h->packet_number = htonl(pn);
469 /* with GCM, C/E clear for !encrypt, both set for encrypt */
471 h->tci_an |= MACSEC_TCI_CONFID;
472 else if (secy->icv_len != DEFAULT_ICV_LEN)
473 h->tci_an |= MACSEC_TCI_C;
475 h->tci_an |= tx_sc->encoding_sa;
478 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
480 if (data_len < MIN_NON_SHORT_LEN)
481 h->short_length = data_len;
484 /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
485 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
487 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
488 int len = skb->len - 2 * ETH_ALEN;
489 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
491 /* a) It comprises at least 17 octets */
495 /* b) MACsec EtherType: already checked */
497 /* c) V bit is clear */
498 if (h->tci_an & MACSEC_TCI_VERSION)
501 /* d) ES or SCB => !SC */
502 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
503 (h->tci_an & MACSEC_TCI_SC))
506 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
510 /* rx.pn != 0 (figure 10-5) */
511 if (!h->packet_number)
514 /* length check, f) g) h) i) */
516 return len == extra_len + h->short_length;
517 return len >= extra_len + MIN_NON_SHORT_LEN;
520 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
521 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
523 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
525 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
528 gcm_iv->pn = htonl(pn);
531 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
533 return (struct macsec_eth_header *)skb_mac_header(skb);
536 static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
540 spin_lock_bh(&tx_sa->lock);
544 if (tx_sa->next_pn == 0) {
545 pr_debug("PN wrapped, transitioning to !oper\n");
546 tx_sa->active = false;
547 if (secy->protect_frames)
548 secy->operational = false;
550 spin_unlock_bh(&tx_sa->lock);
555 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
557 struct macsec_dev *macsec = netdev_priv(dev);
559 skb->dev = macsec->real_dev;
560 skb_reset_mac_header(skb);
561 skb->protocol = eth_hdr(skb)->h_proto;
564 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
565 struct macsec_tx_sa *tx_sa)
567 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
569 u64_stats_update_begin(&txsc_stats->syncp);
570 if (tx_sc->encrypt) {
571 txsc_stats->stats.OutOctetsEncrypted += skb->len;
572 txsc_stats->stats.OutPktsEncrypted++;
573 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
575 txsc_stats->stats.OutOctetsProtected += skb->len;
576 txsc_stats->stats.OutPktsProtected++;
577 this_cpu_inc(tx_sa->stats->OutPktsProtected);
579 u64_stats_update_end(&txsc_stats->syncp);
582 static void count_tx(struct net_device *dev, int ret, int len)
584 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
585 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
587 u64_stats_update_begin(&stats->syncp);
589 stats->tx_bytes += len;
590 u64_stats_update_end(&stats->syncp);
594 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
596 struct sk_buff *skb = base->data;
597 struct net_device *dev = skb->dev;
598 struct macsec_dev *macsec = macsec_priv(dev);
599 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
602 aead_request_free(macsec_skb_cb(skb)->req);
605 macsec_encrypt_finish(skb, dev);
606 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
608 ret = dev_queue_xmit(skb);
609 count_tx(dev, ret, len);
610 rcu_read_unlock_bh();
616 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
618 struct scatterlist **sg,
621 size_t size, iv_offset, sg_offset;
622 struct aead_request *req;
625 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
627 size += GCM_AES_IV_LEN;
629 size = ALIGN(size, __alignof__(struct scatterlist));
631 size += sizeof(struct scatterlist) * num_frags;
633 tmp = kmalloc(size, GFP_ATOMIC);
637 *iv = (unsigned char *)(tmp + iv_offset);
638 *sg = (struct scatterlist *)(tmp + sg_offset);
641 aead_request_set_tfm(req, tfm);
646 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
647 struct net_device *dev)
650 struct scatterlist *sg;
651 struct sk_buff *trailer;
654 struct macsec_eth_header *hh;
655 size_t unprotected_len;
656 struct aead_request *req;
657 struct macsec_secy *secy;
658 struct macsec_tx_sc *tx_sc;
659 struct macsec_tx_sa *tx_sa;
660 struct macsec_dev *macsec = macsec_priv(dev);
664 secy = &macsec->secy;
665 tx_sc = &secy->tx_sc;
667 /* 10.5.1 TX SA assignment */
668 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
670 secy->operational = false;
672 return ERR_PTR(-EINVAL);
675 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
676 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
677 struct sk_buff *nskb = skb_copy_expand(skb,
678 MACSEC_NEEDED_HEADROOM,
679 MACSEC_NEEDED_TAILROOM,
685 macsec_txsa_put(tx_sa);
687 return ERR_PTR(-ENOMEM);
690 skb = skb_unshare(skb, GFP_ATOMIC);
692 macsec_txsa_put(tx_sa);
693 return ERR_PTR(-ENOMEM);
697 unprotected_len = skb->len;
699 sci_present = send_sci(secy);
700 hh = skb_push(skb, macsec_extra_len(sci_present));
701 memmove(hh, eth, 2 * ETH_ALEN);
703 pn = tx_sa_update_pn(tx_sa, secy);
705 macsec_txsa_put(tx_sa);
707 return ERR_PTR(-ENOLINK);
709 macsec_fill_sectag(hh, secy, pn, sci_present);
710 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
712 skb_put(skb, secy->icv_len);
714 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
715 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
717 u64_stats_update_begin(&secy_stats->syncp);
718 secy_stats->stats.OutPktsTooLong++;
719 u64_stats_update_end(&secy_stats->syncp);
721 macsec_txsa_put(tx_sa);
723 return ERR_PTR(-EINVAL);
726 ret = skb_cow_data(skb, 0, &trailer);
727 if (unlikely(ret < 0)) {
728 macsec_txsa_put(tx_sa);
733 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
735 macsec_txsa_put(tx_sa);
737 return ERR_PTR(-ENOMEM);
740 macsec_fill_iv(iv, secy->sci, pn);
742 sg_init_table(sg, ret);
743 ret = skb_to_sgvec(skb, sg, 0, skb->len);
744 if (unlikely(ret < 0)) {
745 aead_request_free(req);
746 macsec_txsa_put(tx_sa);
751 if (tx_sc->encrypt) {
752 int len = skb->len - macsec_hdr_len(sci_present) -
754 aead_request_set_crypt(req, sg, sg, len, iv);
755 aead_request_set_ad(req, macsec_hdr_len(sci_present));
757 aead_request_set_crypt(req, sg, sg, 0, iv);
758 aead_request_set_ad(req, skb->len - secy->icv_len);
761 macsec_skb_cb(skb)->req = req;
762 macsec_skb_cb(skb)->tx_sa = tx_sa;
763 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
766 ret = crypto_aead_encrypt(req);
767 if (ret == -EINPROGRESS) {
769 } else if (ret != 0) {
772 aead_request_free(req);
773 macsec_txsa_put(tx_sa);
774 return ERR_PTR(-EINVAL);
778 aead_request_free(req);
779 macsec_txsa_put(tx_sa);
784 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
786 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
787 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
788 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
791 spin_lock(&rx_sa->lock);
792 if (rx_sa->next_pn >= secy->replay_window)
793 lowest_pn = rx_sa->next_pn - secy->replay_window;
795 /* Now perform replay protection check again
796 * (see IEEE 802.1AE-2006 figure 10-5)
798 if (secy->replay_protect && pn < lowest_pn) {
799 spin_unlock(&rx_sa->lock);
800 u64_stats_update_begin(&rxsc_stats->syncp);
801 rxsc_stats->stats.InPktsLate++;
802 u64_stats_update_end(&rxsc_stats->syncp);
806 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
807 u64_stats_update_begin(&rxsc_stats->syncp);
808 if (hdr->tci_an & MACSEC_TCI_E)
809 rxsc_stats->stats.InOctetsDecrypted += skb->len;
811 rxsc_stats->stats.InOctetsValidated += skb->len;
812 u64_stats_update_end(&rxsc_stats->syncp);
815 if (!macsec_skb_cb(skb)->valid) {
816 spin_unlock(&rx_sa->lock);
819 if (hdr->tci_an & MACSEC_TCI_C ||
820 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
821 u64_stats_update_begin(&rxsc_stats->syncp);
822 rxsc_stats->stats.InPktsNotValid++;
823 u64_stats_update_end(&rxsc_stats->syncp);
827 u64_stats_update_begin(&rxsc_stats->syncp);
828 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
829 rxsc_stats->stats.InPktsInvalid++;
830 this_cpu_inc(rx_sa->stats->InPktsInvalid);
831 } else if (pn < lowest_pn) {
832 rxsc_stats->stats.InPktsDelayed++;
834 rxsc_stats->stats.InPktsUnchecked++;
836 u64_stats_update_end(&rxsc_stats->syncp);
838 u64_stats_update_begin(&rxsc_stats->syncp);
839 if (pn < lowest_pn) {
840 rxsc_stats->stats.InPktsDelayed++;
842 rxsc_stats->stats.InPktsOK++;
843 this_cpu_inc(rx_sa->stats->InPktsOK);
845 u64_stats_update_end(&rxsc_stats->syncp);
847 if (pn >= rx_sa->next_pn)
848 rx_sa->next_pn = pn + 1;
849 spin_unlock(&rx_sa->lock);
855 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
857 skb->pkt_type = PACKET_HOST;
858 skb->protocol = eth_type_trans(skb, dev);
860 skb_reset_network_header(skb);
861 if (!skb_transport_header_was_set(skb))
862 skb_reset_transport_header(skb);
863 skb_reset_mac_len(skb);
866 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
868 skb->ip_summed = CHECKSUM_NONE;
869 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
870 skb_pull(skb, hdr_len);
871 pskb_trim_unique(skb, skb->len - icv_len);
874 static void count_rx(struct net_device *dev, int len)
876 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
878 u64_stats_update_begin(&stats->syncp);
880 stats->rx_bytes += len;
881 u64_stats_update_end(&stats->syncp);
884 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
886 struct sk_buff *skb = base->data;
887 struct net_device *dev = skb->dev;
888 struct macsec_dev *macsec = macsec_priv(dev);
889 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
890 struct macsec_rx_sc *rx_sc = rx_sa->sc;
894 aead_request_free(macsec_skb_cb(skb)->req);
897 macsec_skb_cb(skb)->valid = true;
900 pn = ntohl(macsec_ethhdr(skb)->packet_number);
901 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
902 rcu_read_unlock_bh();
907 macsec_finalize_skb(skb, macsec->secy.icv_len,
908 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
909 macsec_reset_skb(skb, macsec->secy.netdev);
912 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
915 rcu_read_unlock_bh();
918 macsec_rxsa_put(rx_sa);
919 macsec_rxsc_put(rx_sc);
923 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
924 struct net_device *dev,
925 struct macsec_rx_sa *rx_sa,
927 struct macsec_secy *secy)
930 struct scatterlist *sg;
931 struct sk_buff *trailer;
933 struct aead_request *req;
934 struct macsec_eth_header *hdr;
935 u16 icv_len = secy->icv_len;
937 macsec_skb_cb(skb)->valid = false;
938 skb = skb_share_check(skb, GFP_ATOMIC);
940 return ERR_PTR(-ENOMEM);
942 ret = skb_cow_data(skb, 0, &trailer);
943 if (unlikely(ret < 0)) {
947 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
950 return ERR_PTR(-ENOMEM);
953 hdr = (struct macsec_eth_header *)skb->data;
954 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
956 sg_init_table(sg, ret);
957 ret = skb_to_sgvec(skb, sg, 0, skb->len);
958 if (unlikely(ret < 0)) {
959 aead_request_free(req);
964 if (hdr->tci_an & MACSEC_TCI_E) {
965 /* confidentiality: ethernet + macsec header
966 * authenticated, encrypted payload
968 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
970 aead_request_set_crypt(req, sg, sg, len, iv);
971 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
972 skb = skb_unshare(skb, GFP_ATOMIC);
974 aead_request_free(req);
975 return ERR_PTR(-ENOMEM);
978 /* integrity only: all headers + data authenticated */
979 aead_request_set_crypt(req, sg, sg, icv_len, iv);
980 aead_request_set_ad(req, skb->len - icv_len);
983 macsec_skb_cb(skb)->req = req;
985 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
988 ret = crypto_aead_decrypt(req);
989 if (ret == -EINPROGRESS) {
991 } else if (ret != 0) {
992 /* decryption/authentication failed
993 * 10.6 if validateFrames is disabled, deliver anyway
995 if (ret != -EBADMSG) {
1000 macsec_skb_cb(skb)->valid = true;
1004 aead_request_free(req);
1009 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
1011 struct macsec_rx_sc *rx_sc;
1013 for_each_rxsc(secy, rx_sc) {
1014 if (rx_sc->sci == sci)
1021 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1023 struct macsec_rx_sc *rx_sc;
1025 for_each_rxsc_rtnl(secy, rx_sc) {
1026 if (rx_sc->sci == sci)
1033 static void handle_not_macsec(struct sk_buff *skb)
1035 struct macsec_rxh_data *rxd;
1036 struct macsec_dev *macsec;
1039 rxd = macsec_data_rcu(skb->dev);
1041 /* 10.6 If the management control validateFrames is not
1042 * Strict, frames without a SecTAG are received, counted, and
1043 * delivered to the Controlled Port
1045 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1046 struct sk_buff *nskb;
1047 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1049 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1050 u64_stats_update_begin(&secy_stats->syncp);
1051 secy_stats->stats.InPktsNoTag++;
1052 u64_stats_update_end(&secy_stats->syncp);
1056 /* deliver on this port */
1057 nskb = skb_clone(skb, GFP_ATOMIC);
1061 nskb->dev = macsec->secy.netdev;
1063 if (netif_rx(nskb) == NET_RX_SUCCESS) {
1064 u64_stats_update_begin(&secy_stats->syncp);
1065 secy_stats->stats.InPktsUntagged++;
1066 u64_stats_update_end(&secy_stats->syncp);
1073 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1075 struct sk_buff *skb = *pskb;
1076 struct net_device *dev = skb->dev;
1077 struct macsec_eth_header *hdr;
1078 struct macsec_secy *secy = NULL;
1079 struct macsec_rx_sc *rx_sc;
1080 struct macsec_rx_sa *rx_sa;
1081 struct macsec_rxh_data *rxd;
1082 struct macsec_dev *macsec;
1086 struct pcpu_rx_sc_stats *rxsc_stats;
1087 struct pcpu_secy_stats *secy_stats;
1091 if (skb_headroom(skb) < ETH_HLEN)
1094 hdr = macsec_ethhdr(skb);
1095 if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1096 handle_not_macsec(skb);
1098 /* and deliver to the uncontrolled port */
1099 return RX_HANDLER_PASS;
1102 skb = skb_unshare(skb, GFP_ATOMIC);
1105 return RX_HANDLER_CONSUMED;
1107 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1109 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1113 hdr = macsec_ethhdr(skb);
1115 /* Frames with a SecTAG that has the TCI E bit set but the C
1116 * bit clear are discarded, as this reserved encoding is used
1117 * to identify frames with a SecTAG that are not to be
1118 * delivered to the Controlled Port.
1120 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1121 return RX_HANDLER_PASS;
1123 /* now, pull the extra length */
1124 if (hdr->tci_an & MACSEC_TCI_SC) {
1129 /* ethernet header is part of crypto processing */
1130 skb_push(skb, ETH_HLEN);
1132 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1133 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1134 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1137 rxd = macsec_data_rcu(skb->dev);
1139 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1140 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1142 sc = sc ? macsec_rxsc_get(sc) : NULL;
1145 secy = &macsec->secy;
1155 macsec = macsec_priv(dev);
1156 secy_stats = this_cpu_ptr(macsec->stats);
1157 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1159 if (!macsec_validate_skb(skb, secy->icv_len)) {
1160 u64_stats_update_begin(&secy_stats->syncp);
1161 secy_stats->stats.InPktsBadTag++;
1162 u64_stats_update_end(&secy_stats->syncp);
1166 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1168 /* 10.6.1 if the SA is not in use */
1170 /* If validateFrames is Strict or the C bit in the
1171 * SecTAG is set, discard
1173 if (hdr->tci_an & MACSEC_TCI_C ||
1174 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1175 u64_stats_update_begin(&rxsc_stats->syncp);
1176 rxsc_stats->stats.InPktsNotUsingSA++;
1177 u64_stats_update_end(&rxsc_stats->syncp);
1181 /* not Strict, the frame (with the SecTAG and ICV
1182 * removed) is delivered to the Controlled Port.
1184 u64_stats_update_begin(&rxsc_stats->syncp);
1185 rxsc_stats->stats.InPktsUnusedSA++;
1186 u64_stats_update_end(&rxsc_stats->syncp);
1190 /* First, PN check to avoid decrypting obviously wrong packets */
1191 pn = ntohl(hdr->packet_number);
1192 if (secy->replay_protect) {
1195 spin_lock(&rx_sa->lock);
1196 late = rx_sa->next_pn >= secy->replay_window &&
1197 pn < (rx_sa->next_pn - secy->replay_window);
1198 spin_unlock(&rx_sa->lock);
1201 u64_stats_update_begin(&rxsc_stats->syncp);
1202 rxsc_stats->stats.InPktsLate++;
1203 u64_stats_update_end(&rxsc_stats->syncp);
1208 macsec_skb_cb(skb)->rx_sa = rx_sa;
1210 /* Disabled && !changed text => skip validation */
1211 if (hdr->tci_an & MACSEC_TCI_C ||
1212 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1213 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1216 /* the decrypt callback needs the reference */
1217 if (PTR_ERR(skb) != -EINPROGRESS) {
1218 macsec_rxsa_put(rx_sa);
1219 macsec_rxsc_put(rx_sc);
1223 return RX_HANDLER_CONSUMED;
1226 if (!macsec_post_decrypt(skb, secy, pn))
1230 macsec_finalize_skb(skb, secy->icv_len,
1231 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1232 macsec_reset_skb(skb, secy->netdev);
1235 macsec_rxsa_put(rx_sa);
1236 macsec_rxsc_put(rx_sc);
1239 ret = gro_cells_receive(&macsec->gro_cells, skb);
1240 if (ret == NET_RX_SUCCESS)
1241 count_rx(dev, skb->len);
1243 macsec->secy.netdev->stats.rx_dropped++;
1248 return RX_HANDLER_CONSUMED;
1251 macsec_rxsa_put(rx_sa);
1253 macsec_rxsc_put(rx_sc);
1258 return RX_HANDLER_CONSUMED;
1261 /* 10.6.1 if the SC is not found */
1262 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1264 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1265 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1267 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1268 struct sk_buff *nskb;
1270 secy_stats = this_cpu_ptr(macsec->stats);
1272 /* If validateFrames is Strict or the C bit in the
1273 * SecTAG is set, discard
1276 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1277 u64_stats_update_begin(&secy_stats->syncp);
1278 secy_stats->stats.InPktsNoSCI++;
1279 u64_stats_update_end(&secy_stats->syncp);
1283 /* not strict, the frame (with the SecTAG and ICV
1284 * removed) is delivered to the Controlled Port.
1286 nskb = skb_clone(skb, GFP_ATOMIC);
1290 macsec_reset_skb(nskb, macsec->secy.netdev);
1292 ret = netif_rx(nskb);
1293 if (ret == NET_RX_SUCCESS) {
1294 u64_stats_update_begin(&secy_stats->syncp);
1295 secy_stats->stats.InPktsUnknownSCI++;
1296 u64_stats_update_end(&secy_stats->syncp);
1298 macsec->secy.netdev->stats.rx_dropped++;
1304 return RX_HANDLER_PASS;
1307 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1309 struct crypto_aead *tfm;
1312 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
1317 ret = crypto_aead_setkey(tfm, key, key_len);
1321 ret = crypto_aead_setauthsize(tfm, icv_len);
1327 crypto_free_aead(tfm);
1328 return ERR_PTR(ret);
1331 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1334 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1338 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1339 if (IS_ERR(rx_sa->key.tfm)) {
1340 free_percpu(rx_sa->stats);
1341 return PTR_ERR(rx_sa->key.tfm);
1344 rx_sa->active = false;
1346 refcount_set(&rx_sa->refcnt, 1);
1347 spin_lock_init(&rx_sa->lock);
1352 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1354 rx_sa->active = false;
1356 macsec_rxsa_put(rx_sa);
1359 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1363 for (i = 0; i < MACSEC_NUM_AN; i++) {
1364 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1366 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1371 macsec_rxsc_put(rx_sc);
1374 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1376 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1378 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1380 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1381 if (rx_sc->sci == sci) {
1384 rcu_assign_pointer(*rx_scp, rx_sc->next);
1392 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1394 struct macsec_rx_sc *rx_sc;
1395 struct macsec_dev *macsec;
1396 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1397 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1398 struct macsec_secy *secy;
1400 list_for_each_entry(macsec, &rxd->secys, secys) {
1401 if (find_rx_sc_rtnl(&macsec->secy, sci))
1402 return ERR_PTR(-EEXIST);
1405 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1407 return ERR_PTR(-ENOMEM);
1409 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1410 if (!rx_sc->stats) {
1412 return ERR_PTR(-ENOMEM);
1416 rx_sc->active = true;
1417 refcount_set(&rx_sc->refcnt, 1);
1419 secy = &macsec_priv(dev)->secy;
1420 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1421 rcu_assign_pointer(secy->rx_sc, rx_sc);
1429 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1432 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1436 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1437 if (IS_ERR(tx_sa->key.tfm)) {
1438 free_percpu(tx_sa->stats);
1439 return PTR_ERR(tx_sa->key.tfm);
1442 tx_sa->active = false;
1443 refcount_set(&tx_sa->refcnt, 1);
1444 spin_lock_init(&tx_sa->lock);
1449 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1451 tx_sa->active = false;
1453 macsec_txsa_put(tx_sa);
1456 static struct genl_family macsec_fam;
1458 static struct net_device *get_dev_from_nl(struct net *net,
1459 struct nlattr **attrs)
1461 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1462 struct net_device *dev;
1464 dev = __dev_get_by_index(net, ifindex);
1466 return ERR_PTR(-ENODEV);
1468 if (!netif_is_macsec(dev))
1469 return ERR_PTR(-ENODEV);
1474 static sci_t nla_get_sci(const struct nlattr *nla)
1476 return (__force sci_t)nla_get_u64(nla);
1479 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1482 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1485 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1486 struct nlattr **attrs,
1487 struct nlattr **tb_sa,
1488 struct net_device **devp,
1489 struct macsec_secy **secyp,
1490 struct macsec_tx_sc **scp,
1493 struct net_device *dev;
1494 struct macsec_secy *secy;
1495 struct macsec_tx_sc *tx_sc;
1496 struct macsec_tx_sa *tx_sa;
1498 if (!tb_sa[MACSEC_SA_ATTR_AN])
1499 return ERR_PTR(-EINVAL);
1501 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1503 dev = get_dev_from_nl(net, attrs);
1505 return ERR_CAST(dev);
1507 if (*assoc_num >= MACSEC_NUM_AN)
1508 return ERR_PTR(-EINVAL);
1510 secy = &macsec_priv(dev)->secy;
1511 tx_sc = &secy->tx_sc;
1513 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1515 return ERR_PTR(-ENODEV);
1523 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1524 struct nlattr **attrs,
1525 struct nlattr **tb_rxsc,
1526 struct net_device **devp,
1527 struct macsec_secy **secyp)
1529 struct net_device *dev;
1530 struct macsec_secy *secy;
1531 struct macsec_rx_sc *rx_sc;
1534 dev = get_dev_from_nl(net, attrs);
1536 return ERR_CAST(dev);
1538 secy = &macsec_priv(dev)->secy;
1540 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1541 return ERR_PTR(-EINVAL);
1543 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1544 rx_sc = find_rx_sc_rtnl(secy, sci);
1546 return ERR_PTR(-ENODEV);
1554 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1555 struct nlattr **attrs,
1556 struct nlattr **tb_rxsc,
1557 struct nlattr **tb_sa,
1558 struct net_device **devp,
1559 struct macsec_secy **secyp,
1560 struct macsec_rx_sc **scp,
1563 struct macsec_rx_sc *rx_sc;
1564 struct macsec_rx_sa *rx_sa;
1566 if (!tb_sa[MACSEC_SA_ATTR_AN])
1567 return ERR_PTR(-EINVAL);
1569 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1570 if (*assoc_num >= MACSEC_NUM_AN)
1571 return ERR_PTR(-EINVAL);
1573 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1575 return ERR_CAST(rx_sc);
1577 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1579 return ERR_PTR(-ENODEV);
1585 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1586 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1587 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1588 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1591 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1592 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1593 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1596 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1597 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1598 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1599 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1600 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1601 .len = MACSEC_KEYID_LEN, },
1602 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1603 .len = MACSEC_MAX_KEY_LEN, },
1606 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1608 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1611 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1617 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1619 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1622 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1628 static bool validate_add_rxsa(struct nlattr **attrs)
1630 if (!attrs[MACSEC_SA_ATTR_AN] ||
1631 !attrs[MACSEC_SA_ATTR_KEY] ||
1632 !attrs[MACSEC_SA_ATTR_KEYID])
1635 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1638 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1641 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1642 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1646 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1652 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1654 struct net_device *dev;
1655 struct nlattr **attrs = info->attrs;
1656 struct macsec_secy *secy;
1657 struct macsec_rx_sc *rx_sc;
1658 struct macsec_rx_sa *rx_sa;
1659 unsigned char assoc_num;
1660 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1661 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1664 if (!attrs[MACSEC_ATTR_IFINDEX])
1667 if (parse_sa_config(attrs, tb_sa))
1670 if (parse_rxsc_config(attrs, tb_rxsc))
1673 if (!validate_add_rxsa(tb_sa))
1677 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1678 if (IS_ERR(rx_sc)) {
1680 return PTR_ERR(rx_sc);
1683 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1685 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1686 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1687 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1692 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1698 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1704 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1705 secy->key_len, secy->icv_len);
1712 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1713 spin_lock_bh(&rx_sa->lock);
1714 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1715 spin_unlock_bh(&rx_sa->lock);
1718 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1719 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1721 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1723 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1730 static bool validate_add_rxsc(struct nlattr **attrs)
1732 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1735 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1736 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1743 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1745 struct net_device *dev;
1746 sci_t sci = MACSEC_UNDEF_SCI;
1747 struct nlattr **attrs = info->attrs;
1748 struct macsec_rx_sc *rx_sc;
1749 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1751 if (!attrs[MACSEC_ATTR_IFINDEX])
1754 if (parse_rxsc_config(attrs, tb_rxsc))
1757 if (!validate_add_rxsc(tb_rxsc))
1761 dev = get_dev_from_nl(genl_info_net(info), attrs);
1764 return PTR_ERR(dev);
1767 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1769 rx_sc = create_rx_sc(dev, sci);
1770 if (IS_ERR(rx_sc)) {
1772 return PTR_ERR(rx_sc);
1775 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1776 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1783 static bool validate_add_txsa(struct nlattr **attrs)
1785 if (!attrs[MACSEC_SA_ATTR_AN] ||
1786 !attrs[MACSEC_SA_ATTR_PN] ||
1787 !attrs[MACSEC_SA_ATTR_KEY] ||
1788 !attrs[MACSEC_SA_ATTR_KEYID])
1791 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1794 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1797 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1798 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1802 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1808 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1810 struct net_device *dev;
1811 struct nlattr **attrs = info->attrs;
1812 struct macsec_secy *secy;
1813 struct macsec_tx_sc *tx_sc;
1814 struct macsec_tx_sa *tx_sa;
1815 unsigned char assoc_num;
1816 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1819 if (!attrs[MACSEC_ATTR_IFINDEX])
1822 if (parse_sa_config(attrs, tb_sa))
1825 if (!validate_add_txsa(tb_sa))
1829 dev = get_dev_from_nl(genl_info_net(info), attrs);
1832 return PTR_ERR(dev);
1835 secy = &macsec_priv(dev)->secy;
1836 tx_sc = &secy->tx_sc;
1838 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1840 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1841 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1842 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1847 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1853 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1859 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1860 secy->key_len, secy->icv_len);
1867 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1869 spin_lock_bh(&tx_sa->lock);
1870 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1871 spin_unlock_bh(&tx_sa->lock);
1873 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1874 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1876 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1877 secy->operational = true;
1879 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1886 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1888 struct nlattr **attrs = info->attrs;
1889 struct net_device *dev;
1890 struct macsec_secy *secy;
1891 struct macsec_rx_sc *rx_sc;
1892 struct macsec_rx_sa *rx_sa;
1894 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1895 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1897 if (!attrs[MACSEC_ATTR_IFINDEX])
1900 if (parse_sa_config(attrs, tb_sa))
1903 if (parse_rxsc_config(attrs, tb_rxsc))
1907 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1908 &dev, &secy, &rx_sc, &assoc_num);
1909 if (IS_ERR(rx_sa)) {
1911 return PTR_ERR(rx_sa);
1914 if (rx_sa->active) {
1919 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1927 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1929 struct nlattr **attrs = info->attrs;
1930 struct net_device *dev;
1931 struct macsec_secy *secy;
1932 struct macsec_rx_sc *rx_sc;
1934 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1936 if (!attrs[MACSEC_ATTR_IFINDEX])
1939 if (parse_rxsc_config(attrs, tb_rxsc))
1942 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1946 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1949 return PTR_ERR(dev);
1952 secy = &macsec_priv(dev)->secy;
1953 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1955 rx_sc = del_rx_sc(secy, sci);
1967 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1969 struct nlattr **attrs = info->attrs;
1970 struct net_device *dev;
1971 struct macsec_secy *secy;
1972 struct macsec_tx_sc *tx_sc;
1973 struct macsec_tx_sa *tx_sa;
1975 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1977 if (!attrs[MACSEC_ATTR_IFINDEX])
1980 if (parse_sa_config(attrs, tb_sa))
1984 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
1985 &dev, &secy, &tx_sc, &assoc_num);
1986 if (IS_ERR(tx_sa)) {
1988 return PTR_ERR(tx_sa);
1991 if (tx_sa->active) {
1996 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2004 static bool validate_upd_sa(struct nlattr **attrs)
2006 if (!attrs[MACSEC_SA_ATTR_AN] ||
2007 attrs[MACSEC_SA_ATTR_KEY] ||
2008 attrs[MACSEC_SA_ATTR_KEYID])
2011 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2014 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2017 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2018 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2025 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2027 struct nlattr **attrs = info->attrs;
2028 struct net_device *dev;
2029 struct macsec_secy *secy;
2030 struct macsec_tx_sc *tx_sc;
2031 struct macsec_tx_sa *tx_sa;
2033 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2035 if (!attrs[MACSEC_ATTR_IFINDEX])
2038 if (parse_sa_config(attrs, tb_sa))
2041 if (!validate_upd_sa(tb_sa))
2045 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2046 &dev, &secy, &tx_sc, &assoc_num);
2047 if (IS_ERR(tx_sa)) {
2049 return PTR_ERR(tx_sa);
2052 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2053 spin_lock_bh(&tx_sa->lock);
2054 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2055 spin_unlock_bh(&tx_sa->lock);
2058 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2059 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2061 if (assoc_num == tx_sc->encoding_sa)
2062 secy->operational = tx_sa->active;
2069 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2071 struct nlattr **attrs = info->attrs;
2072 struct net_device *dev;
2073 struct macsec_secy *secy;
2074 struct macsec_rx_sc *rx_sc;
2075 struct macsec_rx_sa *rx_sa;
2077 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2078 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2080 if (!attrs[MACSEC_ATTR_IFINDEX])
2083 if (parse_rxsc_config(attrs, tb_rxsc))
2086 if (parse_sa_config(attrs, tb_sa))
2089 if (!validate_upd_sa(tb_sa))
2093 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2094 &dev, &secy, &rx_sc, &assoc_num);
2095 if (IS_ERR(rx_sa)) {
2097 return PTR_ERR(rx_sa);
2100 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2101 spin_lock_bh(&rx_sa->lock);
2102 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2103 spin_unlock_bh(&rx_sa->lock);
2106 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2107 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2113 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2115 struct nlattr **attrs = info->attrs;
2116 struct net_device *dev;
2117 struct macsec_secy *secy;
2118 struct macsec_rx_sc *rx_sc;
2119 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2121 if (!attrs[MACSEC_ATTR_IFINDEX])
2124 if (parse_rxsc_config(attrs, tb_rxsc))
2127 if (!validate_add_rxsc(tb_rxsc))
2131 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2132 if (IS_ERR(rx_sc)) {
2134 return PTR_ERR(rx_sc);
2137 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2138 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2140 if (rx_sc->active != new)
2141 secy->n_rx_sc += new ? 1 : -1;
2143 rx_sc->active = new;
2151 static int copy_tx_sa_stats(struct sk_buff *skb,
2152 struct macsec_tx_sa_stats __percpu *pstats)
2154 struct macsec_tx_sa_stats sum = {0, };
2157 for_each_possible_cpu(cpu) {
2158 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2160 sum.OutPktsProtected += stats->OutPktsProtected;
2161 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2164 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2165 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2171 static noinline_for_stack int
2172 copy_rx_sa_stats(struct sk_buff *skb,
2173 struct macsec_rx_sa_stats __percpu *pstats)
2175 struct macsec_rx_sa_stats sum = {0, };
2178 for_each_possible_cpu(cpu) {
2179 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2181 sum.InPktsOK += stats->InPktsOK;
2182 sum.InPktsInvalid += stats->InPktsInvalid;
2183 sum.InPktsNotValid += stats->InPktsNotValid;
2184 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2185 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2188 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2189 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2190 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2191 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2192 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2198 static noinline_for_stack int
2199 copy_rx_sc_stats(struct sk_buff *skb, struct pcpu_rx_sc_stats __percpu *pstats)
2201 struct macsec_rx_sc_stats sum = {0, };
2204 for_each_possible_cpu(cpu) {
2205 const struct pcpu_rx_sc_stats *stats;
2206 struct macsec_rx_sc_stats tmp;
2209 stats = per_cpu_ptr(pstats, cpu);
2211 start = u64_stats_fetch_begin_irq(&stats->syncp);
2212 memcpy(&tmp, &stats->stats, sizeof(tmp));
2213 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2215 sum.InOctetsValidated += tmp.InOctetsValidated;
2216 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2217 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2218 sum.InPktsDelayed += tmp.InPktsDelayed;
2219 sum.InPktsOK += tmp.InPktsOK;
2220 sum.InPktsInvalid += tmp.InPktsInvalid;
2221 sum.InPktsLate += tmp.InPktsLate;
2222 sum.InPktsNotValid += tmp.InPktsNotValid;
2223 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2224 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2227 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2228 sum.InOctetsValidated,
2229 MACSEC_RXSC_STATS_ATTR_PAD) ||
2230 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2231 sum.InOctetsDecrypted,
2232 MACSEC_RXSC_STATS_ATTR_PAD) ||
2233 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2234 sum.InPktsUnchecked,
2235 MACSEC_RXSC_STATS_ATTR_PAD) ||
2236 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2238 MACSEC_RXSC_STATS_ATTR_PAD) ||
2239 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2241 MACSEC_RXSC_STATS_ATTR_PAD) ||
2242 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2244 MACSEC_RXSC_STATS_ATTR_PAD) ||
2245 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2247 MACSEC_RXSC_STATS_ATTR_PAD) ||
2248 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2250 MACSEC_RXSC_STATS_ATTR_PAD) ||
2251 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2252 sum.InPktsNotUsingSA,
2253 MACSEC_RXSC_STATS_ATTR_PAD) ||
2254 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2256 MACSEC_RXSC_STATS_ATTR_PAD))
2262 static noinline_for_stack int
2263 copy_tx_sc_stats(struct sk_buff *skb, struct pcpu_tx_sc_stats __percpu *pstats)
2265 struct macsec_tx_sc_stats sum = {0, };
2268 for_each_possible_cpu(cpu) {
2269 const struct pcpu_tx_sc_stats *stats;
2270 struct macsec_tx_sc_stats tmp;
2273 stats = per_cpu_ptr(pstats, cpu);
2275 start = u64_stats_fetch_begin_irq(&stats->syncp);
2276 memcpy(&tmp, &stats->stats, sizeof(tmp));
2277 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2279 sum.OutPktsProtected += tmp.OutPktsProtected;
2280 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2281 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2282 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2285 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2286 sum.OutPktsProtected,
2287 MACSEC_TXSC_STATS_ATTR_PAD) ||
2288 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2289 sum.OutPktsEncrypted,
2290 MACSEC_TXSC_STATS_ATTR_PAD) ||
2291 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2292 sum.OutOctetsProtected,
2293 MACSEC_TXSC_STATS_ATTR_PAD) ||
2294 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2295 sum.OutOctetsEncrypted,
2296 MACSEC_TXSC_STATS_ATTR_PAD))
2302 static noinline_for_stack int
2303 copy_secy_stats(struct sk_buff *skb, struct pcpu_secy_stats __percpu *pstats)
2305 struct macsec_dev_stats sum = {0, };
2308 for_each_possible_cpu(cpu) {
2309 const struct pcpu_secy_stats *stats;
2310 struct macsec_dev_stats tmp;
2313 stats = per_cpu_ptr(pstats, cpu);
2315 start = u64_stats_fetch_begin_irq(&stats->syncp);
2316 memcpy(&tmp, &stats->stats, sizeof(tmp));
2317 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2319 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2320 sum.InPktsUntagged += tmp.InPktsUntagged;
2321 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2322 sum.InPktsNoTag += tmp.InPktsNoTag;
2323 sum.InPktsBadTag += tmp.InPktsBadTag;
2324 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2325 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2326 sum.InPktsOverrun += tmp.InPktsOverrun;
2329 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2330 sum.OutPktsUntagged,
2331 MACSEC_SECY_STATS_ATTR_PAD) ||
2332 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2334 MACSEC_SECY_STATS_ATTR_PAD) ||
2335 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2337 MACSEC_SECY_STATS_ATTR_PAD) ||
2338 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2340 MACSEC_SECY_STATS_ATTR_PAD) ||
2341 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2343 MACSEC_SECY_STATS_ATTR_PAD) ||
2344 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2345 sum.InPktsUnknownSCI,
2346 MACSEC_SECY_STATS_ATTR_PAD) ||
2347 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2349 MACSEC_SECY_STATS_ATTR_PAD) ||
2350 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2352 MACSEC_SECY_STATS_ATTR_PAD))
2358 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2360 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2361 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
2368 switch (secy->key_len) {
2369 case MACSEC_GCM_AES_128_SAK_LEN:
2370 csid = MACSEC_DEFAULT_CIPHER_ID;
2372 case MACSEC_GCM_AES_256_SAK_LEN:
2373 csid = MACSEC_CIPHER_ID_GCM_AES_256;
2379 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2380 MACSEC_SECY_ATTR_PAD) ||
2381 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2382 csid, MACSEC_SECY_ATTR_PAD) ||
2383 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2384 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2385 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2386 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2387 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2388 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2389 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2390 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2391 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2392 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2395 if (secy->replay_protect) {
2396 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2400 nla_nest_end(skb, secy_nest);
2404 nla_nest_cancel(skb, secy_nest);
2408 static noinline_for_stack int
2409 dump_secy(struct macsec_secy *secy, struct net_device *dev,
2410 struct sk_buff *skb, struct netlink_callback *cb)
2412 struct macsec_rx_sc *rx_sc;
2413 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2414 struct nlattr *txsa_list, *rxsc_list;
2417 struct nlattr *attr;
2419 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2420 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2424 genl_dump_check_consistent(cb, hdr);
2426 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2427 goto nla_put_failure;
2429 if (nla_put_secy(secy, skb))
2430 goto nla_put_failure;
2432 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
2434 goto nla_put_failure;
2435 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2436 nla_nest_cancel(skb, attr);
2437 goto nla_put_failure;
2439 nla_nest_end(skb, attr);
2441 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
2443 goto nla_put_failure;
2444 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2445 nla_nest_cancel(skb, attr);
2446 goto nla_put_failure;
2448 nla_nest_end(skb, attr);
2450 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
2452 goto nla_put_failure;
2453 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2454 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2455 struct nlattr *txsa_nest;
2460 txsa_nest = nla_nest_start_noflag(skb, j++);
2462 nla_nest_cancel(skb, txsa_list);
2463 goto nla_put_failure;
2466 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2467 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2468 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2469 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2470 nla_nest_cancel(skb, txsa_nest);
2471 nla_nest_cancel(skb, txsa_list);
2472 goto nla_put_failure;
2475 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
2477 nla_nest_cancel(skb, txsa_nest);
2478 nla_nest_cancel(skb, txsa_list);
2479 goto nla_put_failure;
2481 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2482 nla_nest_cancel(skb, attr);
2483 nla_nest_cancel(skb, txsa_nest);
2484 nla_nest_cancel(skb, txsa_list);
2485 goto nla_put_failure;
2487 nla_nest_end(skb, attr);
2489 nla_nest_end(skb, txsa_nest);
2491 nla_nest_end(skb, txsa_list);
2493 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
2495 goto nla_put_failure;
2498 for_each_rxsc_rtnl(secy, rx_sc) {
2500 struct nlattr *rxsa_list;
2501 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
2504 nla_nest_cancel(skb, rxsc_list);
2505 goto nla_put_failure;
2508 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2509 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2510 MACSEC_RXSC_ATTR_PAD)) {
2511 nla_nest_cancel(skb, rxsc_nest);
2512 nla_nest_cancel(skb, rxsc_list);
2513 goto nla_put_failure;
2516 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
2518 nla_nest_cancel(skb, rxsc_nest);
2519 nla_nest_cancel(skb, rxsc_list);
2520 goto nla_put_failure;
2522 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2523 nla_nest_cancel(skb, attr);
2524 nla_nest_cancel(skb, rxsc_nest);
2525 nla_nest_cancel(skb, rxsc_list);
2526 goto nla_put_failure;
2528 nla_nest_end(skb, attr);
2530 rxsa_list = nla_nest_start_noflag(skb,
2531 MACSEC_RXSC_ATTR_SA_LIST);
2533 nla_nest_cancel(skb, rxsc_nest);
2534 nla_nest_cancel(skb, rxsc_list);
2535 goto nla_put_failure;
2538 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2539 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2540 struct nlattr *rxsa_nest;
2545 rxsa_nest = nla_nest_start_noflag(skb, k++);
2547 nla_nest_cancel(skb, rxsa_list);
2548 nla_nest_cancel(skb, rxsc_nest);
2549 nla_nest_cancel(skb, rxsc_list);
2550 goto nla_put_failure;
2553 attr = nla_nest_start_noflag(skb,
2554 MACSEC_SA_ATTR_STATS);
2556 nla_nest_cancel(skb, rxsa_list);
2557 nla_nest_cancel(skb, rxsc_nest);
2558 nla_nest_cancel(skb, rxsc_list);
2559 goto nla_put_failure;
2561 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2562 nla_nest_cancel(skb, attr);
2563 nla_nest_cancel(skb, rxsa_list);
2564 nla_nest_cancel(skb, rxsc_nest);
2565 nla_nest_cancel(skb, rxsc_list);
2566 goto nla_put_failure;
2568 nla_nest_end(skb, attr);
2570 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2571 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2572 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2573 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2574 nla_nest_cancel(skb, rxsa_nest);
2575 nla_nest_cancel(skb, rxsc_nest);
2576 nla_nest_cancel(skb, rxsc_list);
2577 goto nla_put_failure;
2579 nla_nest_end(skb, rxsa_nest);
2582 nla_nest_end(skb, rxsa_list);
2583 nla_nest_end(skb, rxsc_nest);
2586 nla_nest_end(skb, rxsc_list);
2588 genlmsg_end(skb, hdr);
2593 genlmsg_cancel(skb, hdr);
2597 static int macsec_generation = 1; /* protected by RTNL */
2599 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2601 struct net *net = sock_net(skb->sk);
2602 struct net_device *dev;
2605 dev_idx = cb->args[0];
2610 cb->seq = macsec_generation;
2612 for_each_netdev(net, dev) {
2613 struct macsec_secy *secy;
2618 if (!netif_is_macsec(dev))
2621 secy = &macsec_priv(dev)->secy;
2622 if (dump_secy(secy, dev, skb, cb) < 0)
2634 static const struct genl_ops macsec_genl_ops[] = {
2636 .cmd = MACSEC_CMD_GET_TXSC,
2637 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2638 .dumpit = macsec_dump_txsc,
2641 .cmd = MACSEC_CMD_ADD_RXSC,
2642 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2643 .doit = macsec_add_rxsc,
2644 .flags = GENL_ADMIN_PERM,
2647 .cmd = MACSEC_CMD_DEL_RXSC,
2648 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2649 .doit = macsec_del_rxsc,
2650 .flags = GENL_ADMIN_PERM,
2653 .cmd = MACSEC_CMD_UPD_RXSC,
2654 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2655 .doit = macsec_upd_rxsc,
2656 .flags = GENL_ADMIN_PERM,
2659 .cmd = MACSEC_CMD_ADD_TXSA,
2660 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2661 .doit = macsec_add_txsa,
2662 .flags = GENL_ADMIN_PERM,
2665 .cmd = MACSEC_CMD_DEL_TXSA,
2666 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2667 .doit = macsec_del_txsa,
2668 .flags = GENL_ADMIN_PERM,
2671 .cmd = MACSEC_CMD_UPD_TXSA,
2672 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2673 .doit = macsec_upd_txsa,
2674 .flags = GENL_ADMIN_PERM,
2677 .cmd = MACSEC_CMD_ADD_RXSA,
2678 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2679 .doit = macsec_add_rxsa,
2680 .flags = GENL_ADMIN_PERM,
2683 .cmd = MACSEC_CMD_DEL_RXSA,
2684 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2685 .doit = macsec_del_rxsa,
2686 .flags = GENL_ADMIN_PERM,
2689 .cmd = MACSEC_CMD_UPD_RXSA,
2690 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2691 .doit = macsec_upd_rxsa,
2692 .flags = GENL_ADMIN_PERM,
2696 static struct genl_family macsec_fam __ro_after_init = {
2697 .name = MACSEC_GENL_NAME,
2699 .version = MACSEC_GENL_VERSION,
2700 .maxattr = MACSEC_ATTR_MAX,
2701 .policy = macsec_genl_policy,
2703 .module = THIS_MODULE,
2704 .ops = macsec_genl_ops,
2705 .n_ops = ARRAY_SIZE(macsec_genl_ops),
2708 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2709 struct net_device *dev)
2711 struct macsec_dev *macsec = netdev_priv(dev);
2712 struct macsec_secy *secy = &macsec->secy;
2713 struct pcpu_secy_stats *secy_stats;
2717 if (!secy->protect_frames) {
2718 secy_stats = this_cpu_ptr(macsec->stats);
2719 u64_stats_update_begin(&secy_stats->syncp);
2720 secy_stats->stats.OutPktsUntagged++;
2721 u64_stats_update_end(&secy_stats->syncp);
2722 skb->dev = macsec->real_dev;
2724 ret = dev_queue_xmit(skb);
2725 count_tx(dev, ret, len);
2729 if (!secy->operational) {
2731 dev->stats.tx_dropped++;
2732 return NETDEV_TX_OK;
2735 skb = macsec_encrypt(skb, dev);
2737 if (PTR_ERR(skb) != -EINPROGRESS)
2738 dev->stats.tx_dropped++;
2739 return NETDEV_TX_OK;
2742 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2744 macsec_encrypt_finish(skb, dev);
2746 ret = dev_queue_xmit(skb);
2747 count_tx(dev, ret, len);
2751 #define MACSEC_FEATURES \
2752 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
2753 static struct lock_class_key macsec_netdev_addr_lock_key;
2755 static int macsec_dev_init(struct net_device *dev)
2757 struct macsec_dev *macsec = macsec_priv(dev);
2758 struct net_device *real_dev = macsec->real_dev;
2761 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2765 err = gro_cells_init(&macsec->gro_cells, dev);
2767 free_percpu(dev->tstats);
2771 dev->features = real_dev->features & MACSEC_FEATURES;
2772 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2774 dev->needed_headroom = real_dev->needed_headroom +
2775 MACSEC_NEEDED_HEADROOM;
2776 dev->needed_tailroom = real_dev->needed_tailroom +
2777 MACSEC_NEEDED_TAILROOM;
2779 if (is_zero_ether_addr(dev->dev_addr))
2780 eth_hw_addr_inherit(dev, real_dev);
2781 if (is_zero_ether_addr(dev->broadcast))
2782 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2787 static void macsec_dev_uninit(struct net_device *dev)
2789 struct macsec_dev *macsec = macsec_priv(dev);
2791 gro_cells_destroy(&macsec->gro_cells);
2792 free_percpu(dev->tstats);
2795 static netdev_features_t macsec_fix_features(struct net_device *dev,
2796 netdev_features_t features)
2798 struct macsec_dev *macsec = macsec_priv(dev);
2799 struct net_device *real_dev = macsec->real_dev;
2801 features &= (real_dev->features & MACSEC_FEATURES) |
2802 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2803 features |= NETIF_F_LLTX;
2808 static int macsec_dev_open(struct net_device *dev)
2810 struct macsec_dev *macsec = macsec_priv(dev);
2811 struct net_device *real_dev = macsec->real_dev;
2814 err = dev_uc_add(real_dev, dev->dev_addr);
2818 if (dev->flags & IFF_ALLMULTI) {
2819 err = dev_set_allmulti(real_dev, 1);
2824 if (dev->flags & IFF_PROMISC) {
2825 err = dev_set_promiscuity(real_dev, 1);
2827 goto clear_allmulti;
2830 if (netif_carrier_ok(real_dev))
2831 netif_carrier_on(dev);
2835 if (dev->flags & IFF_ALLMULTI)
2836 dev_set_allmulti(real_dev, -1);
2838 dev_uc_del(real_dev, dev->dev_addr);
2839 netif_carrier_off(dev);
2843 static int macsec_dev_stop(struct net_device *dev)
2845 struct macsec_dev *macsec = macsec_priv(dev);
2846 struct net_device *real_dev = macsec->real_dev;
2848 netif_carrier_off(dev);
2850 dev_mc_unsync(real_dev, dev);
2851 dev_uc_unsync(real_dev, dev);
2853 if (dev->flags & IFF_ALLMULTI)
2854 dev_set_allmulti(real_dev, -1);
2856 if (dev->flags & IFF_PROMISC)
2857 dev_set_promiscuity(real_dev, -1);
2859 dev_uc_del(real_dev, dev->dev_addr);
2864 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2866 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2868 if (!(dev->flags & IFF_UP))
2871 if (change & IFF_ALLMULTI)
2872 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2874 if (change & IFF_PROMISC)
2875 dev_set_promiscuity(real_dev,
2876 dev->flags & IFF_PROMISC ? 1 : -1);
2879 static void macsec_dev_set_rx_mode(struct net_device *dev)
2881 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2883 dev_mc_sync(real_dev, dev);
2884 dev_uc_sync(real_dev, dev);
2887 static int macsec_set_mac_address(struct net_device *dev, void *p)
2889 struct macsec_dev *macsec = macsec_priv(dev);
2890 struct net_device *real_dev = macsec->real_dev;
2891 struct sockaddr *addr = p;
2894 if (!is_valid_ether_addr(addr->sa_data))
2895 return -EADDRNOTAVAIL;
2897 if (!(dev->flags & IFF_UP))
2900 err = dev_uc_add(real_dev, addr->sa_data);
2904 dev_uc_del(real_dev, dev->dev_addr);
2907 ether_addr_copy(dev->dev_addr, addr->sa_data);
2911 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2913 struct macsec_dev *macsec = macsec_priv(dev);
2914 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2916 if (macsec->real_dev->mtu - extra < new_mtu)
2924 static void macsec_get_stats64(struct net_device *dev,
2925 struct rtnl_link_stats64 *s)
2932 for_each_possible_cpu(cpu) {
2933 struct pcpu_sw_netstats *stats;
2934 struct pcpu_sw_netstats tmp;
2937 stats = per_cpu_ptr(dev->tstats, cpu);
2939 start = u64_stats_fetch_begin_irq(&stats->syncp);
2940 tmp.rx_packets = stats->rx_packets;
2941 tmp.rx_bytes = stats->rx_bytes;
2942 tmp.tx_packets = stats->tx_packets;
2943 tmp.tx_bytes = stats->tx_bytes;
2944 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2946 s->rx_packets += tmp.rx_packets;
2947 s->rx_bytes += tmp.rx_bytes;
2948 s->tx_packets += tmp.tx_packets;
2949 s->tx_bytes += tmp.tx_bytes;
2952 s->rx_dropped = dev->stats.rx_dropped;
2953 s->tx_dropped = dev->stats.tx_dropped;
2956 static int macsec_get_iflink(const struct net_device *dev)
2958 return macsec_priv(dev)->real_dev->ifindex;
2961 static int macsec_get_nest_level(struct net_device *dev)
2963 return macsec_priv(dev)->nest_level;
2966 static const struct net_device_ops macsec_netdev_ops = {
2967 .ndo_init = macsec_dev_init,
2968 .ndo_uninit = macsec_dev_uninit,
2969 .ndo_open = macsec_dev_open,
2970 .ndo_stop = macsec_dev_stop,
2971 .ndo_fix_features = macsec_fix_features,
2972 .ndo_change_mtu = macsec_change_mtu,
2973 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2974 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2975 .ndo_set_mac_address = macsec_set_mac_address,
2976 .ndo_start_xmit = macsec_start_xmit,
2977 .ndo_get_stats64 = macsec_get_stats64,
2978 .ndo_get_iflink = macsec_get_iflink,
2979 .ndo_get_lock_subclass = macsec_get_nest_level,
2982 static const struct device_type macsec_type = {
2986 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2987 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
2988 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2989 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2990 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2991 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2992 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2993 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2994 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2995 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
2996 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
2997 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
2998 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3001 static void macsec_free_netdev(struct net_device *dev)
3003 struct macsec_dev *macsec = macsec_priv(dev);
3004 struct net_device *real_dev = macsec->real_dev;
3006 free_percpu(macsec->stats);
3007 free_percpu(macsec->secy.tx_sc.stats);
3012 static void macsec_setup(struct net_device *dev)
3016 dev->max_mtu = ETH_MAX_MTU;
3017 dev->priv_flags |= IFF_NO_QUEUE;
3018 dev->netdev_ops = &macsec_netdev_ops;
3019 dev->needs_free_netdev = true;
3020 dev->priv_destructor = macsec_free_netdev;
3021 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3023 eth_zero_addr(dev->broadcast);
3026 static int macsec_changelink_common(struct net_device *dev,
3027 struct nlattr *data[])
3029 struct macsec_secy *secy;
3030 struct macsec_tx_sc *tx_sc;
3032 secy = &macsec_priv(dev)->secy;
3033 tx_sc = &secy->tx_sc;
3035 if (data[IFLA_MACSEC_ENCODING_SA]) {
3036 struct macsec_tx_sa *tx_sa;
3038 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3039 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3041 secy->operational = tx_sa && tx_sa->active;
3044 if (data[IFLA_MACSEC_WINDOW])
3045 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3047 if (data[IFLA_MACSEC_ENCRYPT])
3048 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3050 if (data[IFLA_MACSEC_PROTECT])
3051 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3053 if (data[IFLA_MACSEC_INC_SCI])
3054 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3056 if (data[IFLA_MACSEC_ES])
3057 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3059 if (data[IFLA_MACSEC_SCB])
3060 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3062 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3063 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3065 if (data[IFLA_MACSEC_VALIDATION])
3066 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3068 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3069 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3070 case MACSEC_CIPHER_ID_GCM_AES_128:
3071 case MACSEC_DEFAULT_CIPHER_ID:
3072 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3074 case MACSEC_CIPHER_ID_GCM_AES_256:
3075 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3085 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3086 struct nlattr *data[],
3087 struct netlink_ext_ack *extack)
3092 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3093 data[IFLA_MACSEC_ICV_LEN] ||
3094 data[IFLA_MACSEC_SCI] ||
3095 data[IFLA_MACSEC_PORT])
3098 return macsec_changelink_common(dev, data);
3101 static void macsec_del_dev(struct macsec_dev *macsec)
3105 while (macsec->secy.rx_sc) {
3106 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3108 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3112 for (i = 0; i < MACSEC_NUM_AN; i++) {
3113 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3116 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3122 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3124 struct macsec_dev *macsec = macsec_priv(dev);
3125 struct net_device *real_dev = macsec->real_dev;
3127 unregister_netdevice_queue(dev, head);
3128 list_del_rcu(&macsec->secys);
3129 macsec_del_dev(macsec);
3130 netdev_upper_dev_unlink(real_dev, dev);
3132 macsec_generation++;
3135 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3137 struct macsec_dev *macsec = macsec_priv(dev);
3138 struct net_device *real_dev = macsec->real_dev;
3139 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3141 macsec_common_dellink(dev, head);
3143 if (list_empty(&rxd->secys)) {
3144 netdev_rx_handler_unregister(real_dev);
3149 static int register_macsec_dev(struct net_device *real_dev,
3150 struct net_device *dev)
3152 struct macsec_dev *macsec = macsec_priv(dev);
3153 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3158 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3162 INIT_LIST_HEAD(&rxd->secys);
3164 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3172 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3176 static bool sci_exists(struct net_device *dev, sci_t sci)
3178 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3179 struct macsec_dev *macsec;
3181 list_for_each_entry(macsec, &rxd->secys, secys) {
3182 if (macsec->secy.sci == sci)
3189 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
3191 return make_sci(dev->dev_addr, port);
3194 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3196 struct macsec_dev *macsec = macsec_priv(dev);
3197 struct macsec_secy *secy = &macsec->secy;
3199 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3203 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3204 if (!secy->tx_sc.stats) {
3205 free_percpu(macsec->stats);
3209 if (sci == MACSEC_UNDEF_SCI)
3210 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3213 secy->operational = true;
3214 secy->key_len = DEFAULT_SAK_LEN;
3215 secy->icv_len = icv_len;
3216 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3217 secy->protect_frames = true;
3218 secy->replay_protect = false;
3221 secy->tx_sc.active = true;
3222 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3223 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3224 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3225 secy->tx_sc.end_station = false;
3226 secy->tx_sc.scb = false;
3231 static int macsec_newlink(struct net *net, struct net_device *dev,
3232 struct nlattr *tb[], struct nlattr *data[],
3233 struct netlink_ext_ack *extack)
3235 struct macsec_dev *macsec = macsec_priv(dev);
3236 struct net_device *real_dev;
3239 u8 icv_len = DEFAULT_ICV_LEN;
3240 rx_handler_func_t *rx_handler;
3244 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3248 dev->priv_flags |= IFF_MACSEC;
3250 macsec->real_dev = real_dev;
3252 if (data && data[IFLA_MACSEC_ICV_LEN])
3253 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3254 dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3256 rx_handler = rtnl_dereference(real_dev->rx_handler);
3257 if (rx_handler && rx_handler != macsec_handle_frame)
3260 err = register_netdevice(dev);
3266 macsec->nest_level = dev_get_nest_level(real_dev) + 1;
3267 netdev_lockdep_set_classes(dev);
3268 lockdep_set_class_and_subclass(&dev->addr_list_lock,
3269 &macsec_netdev_addr_lock_key,
3270 macsec_get_nest_level(dev));
3272 err = netdev_upper_dev_link(real_dev, dev, extack);
3276 /* need to be already registered so that ->init has run and
3277 * the MAC addr is set
3279 if (data && data[IFLA_MACSEC_SCI])
3280 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3281 else if (data && data[IFLA_MACSEC_PORT])
3282 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3284 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3286 if (rx_handler && sci_exists(real_dev, sci)) {
3291 err = macsec_add_dev(dev, sci, icv_len);
3296 err = macsec_changelink_common(dev, data);
3301 err = register_macsec_dev(real_dev, dev);
3305 netif_stacked_transfer_operstate(real_dev, dev);
3306 linkwatch_fire_event(dev);
3308 macsec_generation++;
3313 macsec_del_dev(macsec);
3315 netdev_upper_dev_unlink(real_dev, dev);
3317 unregister_netdevice(dev);
3321 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3322 struct netlink_ext_ack *extack)
3324 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3325 u8 icv_len = DEFAULT_ICV_LEN;
3332 if (data[IFLA_MACSEC_CIPHER_SUITE])
3333 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3335 if (data[IFLA_MACSEC_ICV_LEN]) {
3336 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3337 if (icv_len != DEFAULT_ICV_LEN) {
3338 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3339 struct crypto_aead *dummy_tfm;
3341 dummy_tfm = macsec_alloc_tfm(dummy_key,
3344 if (IS_ERR(dummy_tfm))
3345 return PTR_ERR(dummy_tfm);
3346 crypto_free_aead(dummy_tfm);
3351 case MACSEC_CIPHER_ID_GCM_AES_128:
3352 case MACSEC_CIPHER_ID_GCM_AES_256:
3353 case MACSEC_DEFAULT_CIPHER_ID:
3354 if (icv_len < MACSEC_MIN_ICV_LEN ||
3355 icv_len > MACSEC_STD_ICV_LEN)
3362 if (data[IFLA_MACSEC_ENCODING_SA]) {
3363 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3367 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3368 flag < IFLA_MACSEC_VALIDATION;
3371 if (nla_get_u8(data[flag]) > 1)
3376 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3377 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3378 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3380 if ((sci && (scb || es)) || (scb && es))
3383 if (data[IFLA_MACSEC_VALIDATION] &&
3384 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3387 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3388 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3389 !data[IFLA_MACSEC_WINDOW])
3395 static struct net *macsec_get_link_net(const struct net_device *dev)
3397 return dev_net(macsec_priv(dev)->real_dev);
3400 static size_t macsec_get_size(const struct net_device *dev)
3402 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
3403 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
3404 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
3405 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
3406 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
3407 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
3408 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
3409 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
3410 nla_total_size(1) + /* IFLA_MACSEC_ES */
3411 nla_total_size(1) + /* IFLA_MACSEC_SCB */
3412 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
3413 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
3417 static int macsec_fill_info(struct sk_buff *skb,
3418 const struct net_device *dev)
3420 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3421 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3424 switch (secy->key_len) {
3425 case MACSEC_GCM_AES_128_SAK_LEN:
3426 csid = MACSEC_DEFAULT_CIPHER_ID;
3428 case MACSEC_GCM_AES_256_SAK_LEN:
3429 csid = MACSEC_CIPHER_ID_GCM_AES_256;
3432 goto nla_put_failure;
3435 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3437 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3438 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3439 csid, IFLA_MACSEC_PAD) ||
3440 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3441 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3442 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3443 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3444 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3445 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3446 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3447 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3449 goto nla_put_failure;
3451 if (secy->replay_protect) {
3452 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3453 goto nla_put_failure;
3462 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3464 .priv_size = sizeof(struct macsec_dev),
3465 .maxtype = IFLA_MACSEC_MAX,
3466 .policy = macsec_rtnl_policy,
3467 .setup = macsec_setup,
3468 .validate = macsec_validate_attr,
3469 .newlink = macsec_newlink,
3470 .changelink = macsec_changelink,
3471 .dellink = macsec_dellink,
3472 .get_size = macsec_get_size,
3473 .fill_info = macsec_fill_info,
3474 .get_link_net = macsec_get_link_net,
3477 static bool is_macsec_master(struct net_device *dev)
3479 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3482 static int macsec_notify(struct notifier_block *this, unsigned long event,
3485 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3488 if (!is_macsec_master(real_dev))
3494 case NETDEV_CHANGE: {
3495 struct macsec_dev *m, *n;
3496 struct macsec_rxh_data *rxd;
3498 rxd = macsec_data_rtnl(real_dev);
3499 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3500 struct net_device *dev = m->secy.netdev;
3502 netif_stacked_transfer_operstate(real_dev, dev);
3506 case NETDEV_UNREGISTER: {
3507 struct macsec_dev *m, *n;
3508 struct macsec_rxh_data *rxd;
3510 rxd = macsec_data_rtnl(real_dev);
3511 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3512 macsec_common_dellink(m->secy.netdev, &head);
3515 netdev_rx_handler_unregister(real_dev);
3518 unregister_netdevice_many(&head);
3521 case NETDEV_CHANGEMTU: {
3522 struct macsec_dev *m;
3523 struct macsec_rxh_data *rxd;
3525 rxd = macsec_data_rtnl(real_dev);
3526 list_for_each_entry(m, &rxd->secys, secys) {
3527 struct net_device *dev = m->secy.netdev;
3528 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3529 macsec_extra_len(true));
3532 dev_set_mtu(dev, mtu);
3540 static struct notifier_block macsec_notifier = {
3541 .notifier_call = macsec_notify,
3544 static int __init macsec_init(void)
3548 pr_info("MACsec IEEE 802.1AE\n");
3549 err = register_netdevice_notifier(&macsec_notifier);
3553 err = rtnl_link_register(&macsec_link_ops);
3557 err = genl_register_family(&macsec_fam);
3564 rtnl_link_unregister(&macsec_link_ops);
3566 unregister_netdevice_notifier(&macsec_notifier);
3570 static void __exit macsec_exit(void)
3572 genl_unregister_family(&macsec_fam);
3573 rtnl_link_unregister(&macsec_link_ops);
3574 unregister_netdevice_notifier(&macsec_notifier);
3578 module_init(macsec_init);
3579 module_exit(macsec_exit);
3581 MODULE_ALIAS_RTNL_LINK("macsec");
3582 MODULE_ALIAS_GENL_FAMILY("macsec");
3584 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3585 MODULE_LICENSE("GPL v2");