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/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/refcount.h>
17 #include <net/genetlink.h>
19 #include <net/gro_cells.h>
20 #include <net/macsec.h>
21 #include <linux/phy.h>
23 #include <uapi/linux/if_macsec.h>
25 #define MACSEC_SCI_LEN 8
27 /* SecTAG length = macsec_eth_header without the optional SCI */
28 #define MACSEC_TAG_LEN 6
30 struct macsec_eth_header {
34 #if defined(__LITTLE_ENDIAN_BITFIELD)
37 #elif defined(__BIG_ENDIAN_BITFIELD)
41 #error "Please fix <asm/byteorder.h>"
44 u8 secure_channel_id[8]; /* optional */
47 #define MACSEC_TCI_VERSION 0x80
48 #define MACSEC_TCI_ES 0x40 /* end station */
49 #define MACSEC_TCI_SC 0x20 /* SCI present */
50 #define MACSEC_TCI_SCB 0x10 /* epon */
51 #define MACSEC_TCI_E 0x08 /* encryption */
52 #define MACSEC_TCI_C 0x04 /* changed text */
53 #define MACSEC_AN_MASK 0x03 /* association number */
54 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
56 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
57 #define MIN_NON_SHORT_LEN 48
59 #define GCM_AES_IV_LEN 12
60 #define DEFAULT_ICV_LEN 16
62 #define for_each_rxsc(secy, sc) \
63 for (sc = rcu_dereference_bh(secy->rx_sc); \
65 sc = rcu_dereference_bh(sc->next))
66 #define for_each_rxsc_rtnl(secy, sc) \
67 for (sc = rtnl_dereference(secy->rx_sc); \
69 sc = rtnl_dereference(sc->next))
73 u8 secure_channel_id[8];
79 struct macsec_dev_stats {
80 __u64 OutPktsUntagged;
85 __u64 InPktsUnknownSCI;
90 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
92 struct pcpu_secy_stats {
93 struct macsec_dev_stats stats;
94 struct u64_stats_sync syncp;
98 * struct macsec_dev - private data
100 * @real_dev: pointer to underlying netdevice
101 * @stats: MACsec device stats
102 * @secys: linked list of SecY's on the underlying device
103 * @offload: status of offloading on the MACsec device
106 struct macsec_secy secy;
107 struct net_device *real_dev;
108 struct pcpu_secy_stats __percpu *stats;
109 struct list_head secys;
110 struct gro_cells gro_cells;
111 enum macsec_offload offload;
115 * struct macsec_rxh_data - rx_handler private argument
116 * @secys: linked list of SecY's on this underlying device
118 struct macsec_rxh_data {
119 struct list_head secys;
122 static struct macsec_dev *macsec_priv(const struct net_device *dev)
124 return (struct macsec_dev *)netdev_priv(dev);
127 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
129 return rcu_dereference_bh(dev->rx_handler_data);
132 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
134 return rtnl_dereference(dev->rx_handler_data);
138 struct aead_request *req;
140 struct macsec_tx_sa *tx_sa;
141 struct macsec_rx_sa *rx_sa;
148 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
150 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
152 if (!sa || !sa->active)
155 if (!refcount_inc_not_zero(&sa->refcnt))
161 static void free_rx_sc_rcu(struct rcu_head *head)
163 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
165 free_percpu(rx_sc->stats);
169 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
171 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
174 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
176 if (refcount_dec_and_test(&sc->refcnt))
177 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
180 static void free_rxsa(struct rcu_head *head)
182 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
184 crypto_free_aead(sa->key.tfm);
185 free_percpu(sa->stats);
189 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
191 if (refcount_dec_and_test(&sa->refcnt))
192 call_rcu(&sa->rcu, free_rxsa);
195 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
197 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
199 if (!sa || !sa->active)
202 if (!refcount_inc_not_zero(&sa->refcnt))
208 static void free_txsa(struct rcu_head *head)
210 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
212 crypto_free_aead(sa->key.tfm);
213 free_percpu(sa->stats);
217 static void macsec_txsa_put(struct macsec_tx_sa *sa)
219 if (refcount_dec_and_test(&sa->refcnt))
220 call_rcu(&sa->rcu, free_txsa);
223 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
225 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
226 return (struct macsec_cb *)skb->cb;
229 #define MACSEC_PORT_ES (htons(0x0001))
230 #define MACSEC_PORT_SCB (0x0000)
231 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
233 #define MACSEC_GCM_AES_128_SAK_LEN 16
234 #define MACSEC_GCM_AES_256_SAK_LEN 32
236 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
237 #define DEFAULT_SEND_SCI true
238 #define DEFAULT_ENCRYPT false
239 #define DEFAULT_ENCODING_SA 0
241 static bool send_sci(const struct macsec_secy *secy)
243 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
245 return tx_sc->send_sci ||
246 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
249 static sci_t make_sci(u8 *addr, __be16 port)
253 memcpy(&sci, addr, ETH_ALEN);
254 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
259 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
264 memcpy(&sci, hdr->secure_channel_id,
265 sizeof(hdr->secure_channel_id));
267 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
272 static unsigned int macsec_sectag_len(bool sci_present)
274 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
277 static unsigned int macsec_hdr_len(bool sci_present)
279 return macsec_sectag_len(sci_present) + ETH_HLEN;
282 static unsigned int macsec_extra_len(bool sci_present)
284 return macsec_sectag_len(sci_present) + sizeof(__be16);
287 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
288 static void macsec_fill_sectag(struct macsec_eth_header *h,
289 const struct macsec_secy *secy, u32 pn,
292 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
294 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
295 h->eth.h_proto = htons(ETH_P_MACSEC);
298 h->tci_an |= MACSEC_TCI_SC;
299 memcpy(&h->secure_channel_id, &secy->sci,
300 sizeof(h->secure_channel_id));
302 if (tx_sc->end_station)
303 h->tci_an |= MACSEC_TCI_ES;
305 h->tci_an |= MACSEC_TCI_SCB;
308 h->packet_number = htonl(pn);
310 /* with GCM, C/E clear for !encrypt, both set for encrypt */
312 h->tci_an |= MACSEC_TCI_CONFID;
313 else if (secy->icv_len != DEFAULT_ICV_LEN)
314 h->tci_an |= MACSEC_TCI_C;
316 h->tci_an |= tx_sc->encoding_sa;
319 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
321 if (data_len < MIN_NON_SHORT_LEN)
322 h->short_length = data_len;
325 /* Checks if a MACsec interface is being offloaded to an hardware engine */
326 static bool macsec_is_offloaded(struct macsec_dev *macsec)
328 if (macsec->offload == MACSEC_OFFLOAD_PHY)
334 /* Checks if underlying layers implement MACsec offloading functions. */
335 static bool macsec_check_offload(enum macsec_offload offload,
336 struct macsec_dev *macsec)
338 if (!macsec || !macsec->real_dev)
341 if (offload == MACSEC_OFFLOAD_PHY)
342 return macsec->real_dev->phydev &&
343 macsec->real_dev->phydev->macsec_ops;
348 static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload,
349 struct macsec_dev *macsec,
350 struct macsec_context *ctx)
353 memset(ctx, 0, sizeof(*ctx));
354 ctx->offload = offload;
356 if (offload == MACSEC_OFFLOAD_PHY)
357 ctx->phydev = macsec->real_dev->phydev;
360 return macsec->real_dev->phydev->macsec_ops;
363 /* Returns a pointer to the MACsec ops struct if any and updates the MACsec
364 * context device reference if provided.
366 static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec,
367 struct macsec_context *ctx)
369 if (!macsec_check_offload(macsec->offload, macsec))
372 return __macsec_get_ops(macsec->offload, macsec, ctx);
375 /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
376 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
378 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
379 int len = skb->len - 2 * ETH_ALEN;
380 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
382 /* a) It comprises at least 17 octets */
386 /* b) MACsec EtherType: already checked */
388 /* c) V bit is clear */
389 if (h->tci_an & MACSEC_TCI_VERSION)
392 /* d) ES or SCB => !SC */
393 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
394 (h->tci_an & MACSEC_TCI_SC))
397 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
401 /* rx.pn != 0 (figure 10-5) */
402 if (!h->packet_number)
405 /* length check, f) g) h) i) */
407 return len == extra_len + h->short_length;
408 return len >= extra_len + MIN_NON_SHORT_LEN;
411 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
412 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
414 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
416 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
419 gcm_iv->pn = htonl(pn);
422 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
424 return (struct macsec_eth_header *)skb_mac_header(skb);
427 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
429 return make_sci(dev->dev_addr, port);
432 static void __macsec_pn_wrapped(struct macsec_secy *secy,
433 struct macsec_tx_sa *tx_sa)
435 pr_debug("PN wrapped, transitioning to !oper\n");
436 tx_sa->active = false;
437 if (secy->protect_frames)
438 secy->operational = false;
441 void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa)
443 spin_lock_bh(&tx_sa->lock);
444 __macsec_pn_wrapped(secy, tx_sa);
445 spin_unlock_bh(&tx_sa->lock);
447 EXPORT_SYMBOL_GPL(macsec_pn_wrapped);
449 static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
453 spin_lock_bh(&tx_sa->lock);
457 if (tx_sa->next_pn == 0)
458 __macsec_pn_wrapped(secy, tx_sa);
459 spin_unlock_bh(&tx_sa->lock);
464 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
466 struct macsec_dev *macsec = netdev_priv(dev);
468 skb->dev = macsec->real_dev;
469 skb_reset_mac_header(skb);
470 skb->protocol = eth_hdr(skb)->h_proto;
473 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
474 struct macsec_tx_sa *tx_sa)
476 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
478 u64_stats_update_begin(&txsc_stats->syncp);
479 if (tx_sc->encrypt) {
480 txsc_stats->stats.OutOctetsEncrypted += skb->len;
481 txsc_stats->stats.OutPktsEncrypted++;
482 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
484 txsc_stats->stats.OutOctetsProtected += skb->len;
485 txsc_stats->stats.OutPktsProtected++;
486 this_cpu_inc(tx_sa->stats->OutPktsProtected);
488 u64_stats_update_end(&txsc_stats->syncp);
491 static void count_tx(struct net_device *dev, int ret, int len)
493 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
494 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
496 u64_stats_update_begin(&stats->syncp);
498 stats->tx_bytes += len;
499 u64_stats_update_end(&stats->syncp);
503 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
505 struct sk_buff *skb = base->data;
506 struct net_device *dev = skb->dev;
507 struct macsec_dev *macsec = macsec_priv(dev);
508 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
511 aead_request_free(macsec_skb_cb(skb)->req);
514 macsec_encrypt_finish(skb, dev);
515 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
517 ret = dev_queue_xmit(skb);
518 count_tx(dev, ret, len);
519 rcu_read_unlock_bh();
525 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
527 struct scatterlist **sg,
530 size_t size, iv_offset, sg_offset;
531 struct aead_request *req;
534 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
536 size += GCM_AES_IV_LEN;
538 size = ALIGN(size, __alignof__(struct scatterlist));
540 size += sizeof(struct scatterlist) * num_frags;
542 tmp = kmalloc(size, GFP_ATOMIC);
546 *iv = (unsigned char *)(tmp + iv_offset);
547 *sg = (struct scatterlist *)(tmp + sg_offset);
550 aead_request_set_tfm(req, tfm);
555 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
556 struct net_device *dev)
559 struct scatterlist *sg;
560 struct sk_buff *trailer;
563 struct macsec_eth_header *hh;
564 size_t unprotected_len;
565 struct aead_request *req;
566 struct macsec_secy *secy;
567 struct macsec_tx_sc *tx_sc;
568 struct macsec_tx_sa *tx_sa;
569 struct macsec_dev *macsec = macsec_priv(dev);
573 secy = &macsec->secy;
574 tx_sc = &secy->tx_sc;
576 /* 10.5.1 TX SA assignment */
577 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
579 secy->operational = false;
581 return ERR_PTR(-EINVAL);
584 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
585 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
586 struct sk_buff *nskb = skb_copy_expand(skb,
587 MACSEC_NEEDED_HEADROOM,
588 MACSEC_NEEDED_TAILROOM,
594 macsec_txsa_put(tx_sa);
596 return ERR_PTR(-ENOMEM);
599 skb = skb_unshare(skb, GFP_ATOMIC);
601 macsec_txsa_put(tx_sa);
602 return ERR_PTR(-ENOMEM);
606 unprotected_len = skb->len;
608 sci_present = send_sci(secy);
609 hh = skb_push(skb, macsec_extra_len(sci_present));
610 memmove(hh, eth, 2 * ETH_ALEN);
612 pn = tx_sa_update_pn(tx_sa, secy);
614 macsec_txsa_put(tx_sa);
616 return ERR_PTR(-ENOLINK);
618 macsec_fill_sectag(hh, secy, pn, sci_present);
619 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
621 skb_put(skb, secy->icv_len);
623 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
624 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
626 u64_stats_update_begin(&secy_stats->syncp);
627 secy_stats->stats.OutPktsTooLong++;
628 u64_stats_update_end(&secy_stats->syncp);
630 macsec_txsa_put(tx_sa);
632 return ERR_PTR(-EINVAL);
635 ret = skb_cow_data(skb, 0, &trailer);
636 if (unlikely(ret < 0)) {
637 macsec_txsa_put(tx_sa);
642 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
644 macsec_txsa_put(tx_sa);
646 return ERR_PTR(-ENOMEM);
649 macsec_fill_iv(iv, secy->sci, pn);
651 sg_init_table(sg, ret);
652 ret = skb_to_sgvec(skb, sg, 0, skb->len);
653 if (unlikely(ret < 0)) {
654 aead_request_free(req);
655 macsec_txsa_put(tx_sa);
660 if (tx_sc->encrypt) {
661 int len = skb->len - macsec_hdr_len(sci_present) -
663 aead_request_set_crypt(req, sg, sg, len, iv);
664 aead_request_set_ad(req, macsec_hdr_len(sci_present));
666 aead_request_set_crypt(req, sg, sg, 0, iv);
667 aead_request_set_ad(req, skb->len - secy->icv_len);
670 macsec_skb_cb(skb)->req = req;
671 macsec_skb_cb(skb)->tx_sa = tx_sa;
672 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
675 ret = crypto_aead_encrypt(req);
676 if (ret == -EINPROGRESS) {
678 } else if (ret != 0) {
681 aead_request_free(req);
682 macsec_txsa_put(tx_sa);
683 return ERR_PTR(-EINVAL);
687 aead_request_free(req);
688 macsec_txsa_put(tx_sa);
693 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
695 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
696 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
697 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
700 spin_lock(&rx_sa->lock);
701 if (rx_sa->next_pn >= secy->replay_window)
702 lowest_pn = rx_sa->next_pn - secy->replay_window;
704 /* Now perform replay protection check again
705 * (see IEEE 802.1AE-2006 figure 10-5)
707 if (secy->replay_protect && pn < lowest_pn) {
708 spin_unlock(&rx_sa->lock);
709 u64_stats_update_begin(&rxsc_stats->syncp);
710 rxsc_stats->stats.InPktsLate++;
711 u64_stats_update_end(&rxsc_stats->syncp);
715 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
716 u64_stats_update_begin(&rxsc_stats->syncp);
717 if (hdr->tci_an & MACSEC_TCI_E)
718 rxsc_stats->stats.InOctetsDecrypted += skb->len;
720 rxsc_stats->stats.InOctetsValidated += skb->len;
721 u64_stats_update_end(&rxsc_stats->syncp);
724 if (!macsec_skb_cb(skb)->valid) {
725 spin_unlock(&rx_sa->lock);
728 if (hdr->tci_an & MACSEC_TCI_C ||
729 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
730 u64_stats_update_begin(&rxsc_stats->syncp);
731 rxsc_stats->stats.InPktsNotValid++;
732 u64_stats_update_end(&rxsc_stats->syncp);
736 u64_stats_update_begin(&rxsc_stats->syncp);
737 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
738 rxsc_stats->stats.InPktsInvalid++;
739 this_cpu_inc(rx_sa->stats->InPktsInvalid);
740 } else if (pn < lowest_pn) {
741 rxsc_stats->stats.InPktsDelayed++;
743 rxsc_stats->stats.InPktsUnchecked++;
745 u64_stats_update_end(&rxsc_stats->syncp);
747 u64_stats_update_begin(&rxsc_stats->syncp);
748 if (pn < lowest_pn) {
749 rxsc_stats->stats.InPktsDelayed++;
751 rxsc_stats->stats.InPktsOK++;
752 this_cpu_inc(rx_sa->stats->InPktsOK);
754 u64_stats_update_end(&rxsc_stats->syncp);
756 if (pn >= rx_sa->next_pn)
757 rx_sa->next_pn = pn + 1;
758 spin_unlock(&rx_sa->lock);
764 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
766 skb->pkt_type = PACKET_HOST;
767 skb->protocol = eth_type_trans(skb, dev);
769 skb_reset_network_header(skb);
770 if (!skb_transport_header_was_set(skb))
771 skb_reset_transport_header(skb);
772 skb_reset_mac_len(skb);
775 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
777 skb->ip_summed = CHECKSUM_NONE;
778 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
779 skb_pull(skb, hdr_len);
780 pskb_trim_unique(skb, skb->len - icv_len);
783 static void count_rx(struct net_device *dev, int len)
785 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
787 u64_stats_update_begin(&stats->syncp);
789 stats->rx_bytes += len;
790 u64_stats_update_end(&stats->syncp);
793 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
795 struct sk_buff *skb = base->data;
796 struct net_device *dev = skb->dev;
797 struct macsec_dev *macsec = macsec_priv(dev);
798 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
799 struct macsec_rx_sc *rx_sc = rx_sa->sc;
803 aead_request_free(macsec_skb_cb(skb)->req);
806 macsec_skb_cb(skb)->valid = true;
809 pn = ntohl(macsec_ethhdr(skb)->packet_number);
810 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
811 rcu_read_unlock_bh();
816 macsec_finalize_skb(skb, macsec->secy.icv_len,
817 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
818 macsec_reset_skb(skb, macsec->secy.netdev);
821 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
824 rcu_read_unlock_bh();
827 macsec_rxsa_put(rx_sa);
828 macsec_rxsc_put(rx_sc);
832 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
833 struct net_device *dev,
834 struct macsec_rx_sa *rx_sa,
836 struct macsec_secy *secy)
839 struct scatterlist *sg;
840 struct sk_buff *trailer;
842 struct aead_request *req;
843 struct macsec_eth_header *hdr;
844 u16 icv_len = secy->icv_len;
846 macsec_skb_cb(skb)->valid = false;
847 skb = skb_share_check(skb, GFP_ATOMIC);
849 return ERR_PTR(-ENOMEM);
851 ret = skb_cow_data(skb, 0, &trailer);
852 if (unlikely(ret < 0)) {
856 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
859 return ERR_PTR(-ENOMEM);
862 hdr = (struct macsec_eth_header *)skb->data;
863 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
865 sg_init_table(sg, ret);
866 ret = skb_to_sgvec(skb, sg, 0, skb->len);
867 if (unlikely(ret < 0)) {
868 aead_request_free(req);
873 if (hdr->tci_an & MACSEC_TCI_E) {
874 /* confidentiality: ethernet + macsec header
875 * authenticated, encrypted payload
877 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
879 aead_request_set_crypt(req, sg, sg, len, iv);
880 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
881 skb = skb_unshare(skb, GFP_ATOMIC);
883 aead_request_free(req);
884 return ERR_PTR(-ENOMEM);
887 /* integrity only: all headers + data authenticated */
888 aead_request_set_crypt(req, sg, sg, icv_len, iv);
889 aead_request_set_ad(req, skb->len - icv_len);
892 macsec_skb_cb(skb)->req = req;
894 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
897 ret = crypto_aead_decrypt(req);
898 if (ret == -EINPROGRESS) {
900 } else if (ret != 0) {
901 /* decryption/authentication failed
902 * 10.6 if validateFrames is disabled, deliver anyway
904 if (ret != -EBADMSG) {
909 macsec_skb_cb(skb)->valid = true;
913 aead_request_free(req);
918 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
920 struct macsec_rx_sc *rx_sc;
922 for_each_rxsc(secy, rx_sc) {
923 if (rx_sc->sci == sci)
930 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
932 struct macsec_rx_sc *rx_sc;
934 for_each_rxsc_rtnl(secy, rx_sc) {
935 if (rx_sc->sci == sci)
942 static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
944 /* Deliver to the uncontrolled port by default */
945 enum rx_handler_result ret = RX_HANDLER_PASS;
946 struct macsec_rxh_data *rxd;
947 struct macsec_dev *macsec;
950 rxd = macsec_data_rcu(skb->dev);
952 /* 10.6 If the management control validateFrames is not
953 * Strict, frames without a SecTAG are received, counted, and
954 * delivered to the Controlled Port
956 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
957 struct sk_buff *nskb;
958 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
960 if (!macsec_is_offloaded(macsec) &&
961 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
962 u64_stats_update_begin(&secy_stats->syncp);
963 secy_stats->stats.InPktsNoTag++;
964 u64_stats_update_end(&secy_stats->syncp);
968 /* deliver on this port */
969 nskb = skb_clone(skb, GFP_ATOMIC);
973 nskb->dev = macsec->secy.netdev;
975 if (netif_rx(nskb) == NET_RX_SUCCESS) {
976 u64_stats_update_begin(&secy_stats->syncp);
977 secy_stats->stats.InPktsUntagged++;
978 u64_stats_update_end(&secy_stats->syncp);
981 if (netif_running(macsec->secy.netdev) &&
982 macsec_is_offloaded(macsec)) {
983 ret = RX_HANDLER_EXACT;
993 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
995 struct sk_buff *skb = *pskb;
996 struct net_device *dev = skb->dev;
997 struct macsec_eth_header *hdr;
998 struct macsec_secy *secy = NULL;
999 struct macsec_rx_sc *rx_sc;
1000 struct macsec_rx_sa *rx_sa;
1001 struct macsec_rxh_data *rxd;
1002 struct macsec_dev *macsec;
1006 struct pcpu_rx_sc_stats *rxsc_stats;
1007 struct pcpu_secy_stats *secy_stats;
1011 if (skb_headroom(skb) < ETH_HLEN)
1014 hdr = macsec_ethhdr(skb);
1015 if (hdr->eth.h_proto != htons(ETH_P_MACSEC))
1016 return handle_not_macsec(skb);
1018 skb = skb_unshare(skb, GFP_ATOMIC);
1021 return RX_HANDLER_CONSUMED;
1023 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1025 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1029 hdr = macsec_ethhdr(skb);
1031 /* Frames with a SecTAG that has the TCI E bit set but the C
1032 * bit clear are discarded, as this reserved encoding is used
1033 * to identify frames with a SecTAG that are not to be
1034 * delivered to the Controlled Port.
1036 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1037 return RX_HANDLER_PASS;
1039 /* now, pull the extra length */
1040 if (hdr->tci_an & MACSEC_TCI_SC) {
1045 /* ethernet header is part of crypto processing */
1046 skb_push(skb, ETH_HLEN);
1048 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1049 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1050 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1053 rxd = macsec_data_rcu(skb->dev);
1055 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1056 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1058 sc = sc ? macsec_rxsc_get(sc) : NULL;
1061 secy = &macsec->secy;
1071 macsec = macsec_priv(dev);
1072 secy_stats = this_cpu_ptr(macsec->stats);
1073 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1075 if (!macsec_validate_skb(skb, secy->icv_len)) {
1076 u64_stats_update_begin(&secy_stats->syncp);
1077 secy_stats->stats.InPktsBadTag++;
1078 u64_stats_update_end(&secy_stats->syncp);
1082 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1084 /* 10.6.1 if the SA is not in use */
1086 /* If validateFrames is Strict or the C bit in the
1087 * SecTAG is set, discard
1089 if (hdr->tci_an & MACSEC_TCI_C ||
1090 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1091 u64_stats_update_begin(&rxsc_stats->syncp);
1092 rxsc_stats->stats.InPktsNotUsingSA++;
1093 u64_stats_update_end(&rxsc_stats->syncp);
1097 /* not Strict, the frame (with the SecTAG and ICV
1098 * removed) is delivered to the Controlled Port.
1100 u64_stats_update_begin(&rxsc_stats->syncp);
1101 rxsc_stats->stats.InPktsUnusedSA++;
1102 u64_stats_update_end(&rxsc_stats->syncp);
1106 /* First, PN check to avoid decrypting obviously wrong packets */
1107 pn = ntohl(hdr->packet_number);
1108 if (secy->replay_protect) {
1111 spin_lock(&rx_sa->lock);
1112 late = rx_sa->next_pn >= secy->replay_window &&
1113 pn < (rx_sa->next_pn - secy->replay_window);
1114 spin_unlock(&rx_sa->lock);
1117 u64_stats_update_begin(&rxsc_stats->syncp);
1118 rxsc_stats->stats.InPktsLate++;
1119 u64_stats_update_end(&rxsc_stats->syncp);
1124 macsec_skb_cb(skb)->rx_sa = rx_sa;
1126 /* Disabled && !changed text => skip validation */
1127 if (hdr->tci_an & MACSEC_TCI_C ||
1128 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1129 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1132 /* the decrypt callback needs the reference */
1133 if (PTR_ERR(skb) != -EINPROGRESS) {
1134 macsec_rxsa_put(rx_sa);
1135 macsec_rxsc_put(rx_sc);
1139 return RX_HANDLER_CONSUMED;
1142 if (!macsec_post_decrypt(skb, secy, pn))
1146 macsec_finalize_skb(skb, secy->icv_len,
1147 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1148 macsec_reset_skb(skb, secy->netdev);
1151 macsec_rxsa_put(rx_sa);
1152 macsec_rxsc_put(rx_sc);
1155 ret = gro_cells_receive(&macsec->gro_cells, skb);
1156 if (ret == NET_RX_SUCCESS)
1157 count_rx(dev, skb->len);
1159 macsec->secy.netdev->stats.rx_dropped++;
1164 return RX_HANDLER_CONSUMED;
1167 macsec_rxsa_put(rx_sa);
1169 macsec_rxsc_put(rx_sc);
1174 return RX_HANDLER_CONSUMED;
1177 /* 10.6.1 if the SC is not found */
1178 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1180 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1181 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1183 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1184 struct sk_buff *nskb;
1186 secy_stats = this_cpu_ptr(macsec->stats);
1188 /* If validateFrames is Strict or the C bit in the
1189 * SecTAG is set, discard
1192 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1193 u64_stats_update_begin(&secy_stats->syncp);
1194 secy_stats->stats.InPktsNoSCI++;
1195 u64_stats_update_end(&secy_stats->syncp);
1199 /* not strict, the frame (with the SecTAG and ICV
1200 * removed) is delivered to the Controlled Port.
1202 nskb = skb_clone(skb, GFP_ATOMIC);
1206 macsec_reset_skb(nskb, macsec->secy.netdev);
1208 ret = netif_rx(nskb);
1209 if (ret == NET_RX_SUCCESS) {
1210 u64_stats_update_begin(&secy_stats->syncp);
1211 secy_stats->stats.InPktsUnknownSCI++;
1212 u64_stats_update_end(&secy_stats->syncp);
1214 macsec->secy.netdev->stats.rx_dropped++;
1220 return RX_HANDLER_PASS;
1223 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1225 struct crypto_aead *tfm;
1228 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
1233 ret = crypto_aead_setkey(tfm, key, key_len);
1237 ret = crypto_aead_setauthsize(tfm, icv_len);
1243 crypto_free_aead(tfm);
1244 return ERR_PTR(ret);
1247 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1250 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1254 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1255 if (IS_ERR(rx_sa->key.tfm)) {
1256 free_percpu(rx_sa->stats);
1257 return PTR_ERR(rx_sa->key.tfm);
1260 rx_sa->active = false;
1262 refcount_set(&rx_sa->refcnt, 1);
1263 spin_lock_init(&rx_sa->lock);
1268 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1270 rx_sa->active = false;
1272 macsec_rxsa_put(rx_sa);
1275 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1279 for (i = 0; i < MACSEC_NUM_AN; i++) {
1280 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1282 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1287 macsec_rxsc_put(rx_sc);
1290 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1292 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1294 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1296 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1297 if (rx_sc->sci == sci) {
1300 rcu_assign_pointer(*rx_scp, rx_sc->next);
1308 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1310 struct macsec_rx_sc *rx_sc;
1311 struct macsec_dev *macsec;
1312 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1313 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1314 struct macsec_secy *secy;
1316 list_for_each_entry(macsec, &rxd->secys, secys) {
1317 if (find_rx_sc_rtnl(&macsec->secy, sci))
1318 return ERR_PTR(-EEXIST);
1321 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1323 return ERR_PTR(-ENOMEM);
1325 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1326 if (!rx_sc->stats) {
1328 return ERR_PTR(-ENOMEM);
1332 rx_sc->active = true;
1333 refcount_set(&rx_sc->refcnt, 1);
1335 secy = &macsec_priv(dev)->secy;
1336 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1337 rcu_assign_pointer(secy->rx_sc, rx_sc);
1345 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1348 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1352 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1353 if (IS_ERR(tx_sa->key.tfm)) {
1354 free_percpu(tx_sa->stats);
1355 return PTR_ERR(tx_sa->key.tfm);
1358 tx_sa->active = false;
1359 refcount_set(&tx_sa->refcnt, 1);
1360 spin_lock_init(&tx_sa->lock);
1365 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1367 tx_sa->active = false;
1369 macsec_txsa_put(tx_sa);
1372 static struct genl_family macsec_fam;
1374 static struct net_device *get_dev_from_nl(struct net *net,
1375 struct nlattr **attrs)
1377 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1378 struct net_device *dev;
1380 dev = __dev_get_by_index(net, ifindex);
1382 return ERR_PTR(-ENODEV);
1384 if (!netif_is_macsec(dev))
1385 return ERR_PTR(-ENODEV);
1390 static sci_t nla_get_sci(const struct nlattr *nla)
1392 return (__force sci_t)nla_get_u64(nla);
1395 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1398 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1401 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1402 struct nlattr **attrs,
1403 struct nlattr **tb_sa,
1404 struct net_device **devp,
1405 struct macsec_secy **secyp,
1406 struct macsec_tx_sc **scp,
1409 struct net_device *dev;
1410 struct macsec_secy *secy;
1411 struct macsec_tx_sc *tx_sc;
1412 struct macsec_tx_sa *tx_sa;
1414 if (!tb_sa[MACSEC_SA_ATTR_AN])
1415 return ERR_PTR(-EINVAL);
1417 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1419 dev = get_dev_from_nl(net, attrs);
1421 return ERR_CAST(dev);
1423 if (*assoc_num >= MACSEC_NUM_AN)
1424 return ERR_PTR(-EINVAL);
1426 secy = &macsec_priv(dev)->secy;
1427 tx_sc = &secy->tx_sc;
1429 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1431 return ERR_PTR(-ENODEV);
1439 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1440 struct nlattr **attrs,
1441 struct nlattr **tb_rxsc,
1442 struct net_device **devp,
1443 struct macsec_secy **secyp)
1445 struct net_device *dev;
1446 struct macsec_secy *secy;
1447 struct macsec_rx_sc *rx_sc;
1450 dev = get_dev_from_nl(net, attrs);
1452 return ERR_CAST(dev);
1454 secy = &macsec_priv(dev)->secy;
1456 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1457 return ERR_PTR(-EINVAL);
1459 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1460 rx_sc = find_rx_sc_rtnl(secy, sci);
1462 return ERR_PTR(-ENODEV);
1470 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1471 struct nlattr **attrs,
1472 struct nlattr **tb_rxsc,
1473 struct nlattr **tb_sa,
1474 struct net_device **devp,
1475 struct macsec_secy **secyp,
1476 struct macsec_rx_sc **scp,
1479 struct macsec_rx_sc *rx_sc;
1480 struct macsec_rx_sa *rx_sa;
1482 if (!tb_sa[MACSEC_SA_ATTR_AN])
1483 return ERR_PTR(-EINVAL);
1485 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1486 if (*assoc_num >= MACSEC_NUM_AN)
1487 return ERR_PTR(-EINVAL);
1489 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1491 return ERR_CAST(rx_sc);
1493 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1495 return ERR_PTR(-ENODEV);
1501 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1502 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1503 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1504 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1505 [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
1508 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1509 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1510 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1513 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1514 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1515 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1516 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1517 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1518 .len = MACSEC_KEYID_LEN, },
1519 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1520 .len = MACSEC_MAX_KEY_LEN, },
1523 static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
1524 [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 },
1527 /* Offloads an operation to a device driver */
1528 static int macsec_offload(int (* const func)(struct macsec_context *),
1529 struct macsec_context *ctx)
1533 if (unlikely(!func))
1536 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1537 mutex_lock(&ctx->phydev->lock);
1539 /* Phase I: prepare. The drive should fail here if there are going to be
1540 * issues in the commit phase.
1542 ctx->prepare = true;
1547 /* Phase II: commit. This step cannot fail. */
1548 ctx->prepare = false;
1550 /* This should never happen: commit is not allowed to fail */
1552 WARN(1, "MACsec offloading commit failed (%d)\n", ret);
1555 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1556 mutex_unlock(&ctx->phydev->lock);
1561 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1563 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1566 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1572 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1574 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1577 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1583 static bool validate_add_rxsa(struct nlattr **attrs)
1585 if (!attrs[MACSEC_SA_ATTR_AN] ||
1586 !attrs[MACSEC_SA_ATTR_KEY] ||
1587 !attrs[MACSEC_SA_ATTR_KEYID])
1590 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1593 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1596 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1597 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1601 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1607 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1609 struct net_device *dev;
1610 struct nlattr **attrs = info->attrs;
1611 struct macsec_secy *secy;
1612 struct macsec_rx_sc *rx_sc;
1613 struct macsec_rx_sa *rx_sa;
1614 unsigned char assoc_num;
1615 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1616 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1619 if (!attrs[MACSEC_ATTR_IFINDEX])
1622 if (parse_sa_config(attrs, tb_sa))
1625 if (parse_rxsc_config(attrs, tb_rxsc))
1628 if (!validate_add_rxsa(tb_sa))
1632 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1633 if (IS_ERR(rx_sc)) {
1635 return PTR_ERR(rx_sc);
1638 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1640 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1641 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1642 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1647 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1653 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1659 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1660 secy->key_len, secy->icv_len);
1667 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1668 spin_lock_bh(&rx_sa->lock);
1669 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1670 spin_unlock_bh(&rx_sa->lock);
1673 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1674 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1678 /* If h/w offloading is available, propagate to the device */
1679 if (macsec_is_offloaded(netdev_priv(dev))) {
1680 const struct macsec_ops *ops;
1681 struct macsec_context ctx;
1683 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1689 ctx.sa.assoc_num = assoc_num;
1690 ctx.sa.rx_sa = rx_sa;
1691 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1694 err = macsec_offload(ops->mdo_add_rxsa, &ctx);
1699 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1700 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1712 static bool validate_add_rxsc(struct nlattr **attrs)
1714 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1717 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1718 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1725 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1727 struct net_device *dev;
1728 sci_t sci = MACSEC_UNDEF_SCI;
1729 struct nlattr **attrs = info->attrs;
1730 struct macsec_rx_sc *rx_sc;
1731 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1735 if (!attrs[MACSEC_ATTR_IFINDEX])
1738 if (parse_rxsc_config(attrs, tb_rxsc))
1741 if (!validate_add_rxsc(tb_rxsc))
1745 dev = get_dev_from_nl(genl_info_net(info), attrs);
1748 return PTR_ERR(dev);
1751 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1753 rx_sc = create_rx_sc(dev, sci);
1754 if (IS_ERR(rx_sc)) {
1756 return PTR_ERR(rx_sc);
1759 was_active = rx_sc->active;
1760 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1761 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1763 if (macsec_is_offloaded(netdev_priv(dev))) {
1764 const struct macsec_ops *ops;
1765 struct macsec_context ctx;
1767 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1775 ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
1785 rx_sc->active = was_active;
1790 static bool validate_add_txsa(struct nlattr **attrs)
1792 if (!attrs[MACSEC_SA_ATTR_AN] ||
1793 !attrs[MACSEC_SA_ATTR_PN] ||
1794 !attrs[MACSEC_SA_ATTR_KEY] ||
1795 !attrs[MACSEC_SA_ATTR_KEYID])
1798 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1801 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1804 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1805 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1809 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1815 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1817 struct net_device *dev;
1818 struct nlattr **attrs = info->attrs;
1819 struct macsec_secy *secy;
1820 struct macsec_tx_sc *tx_sc;
1821 struct macsec_tx_sa *tx_sa;
1822 unsigned char assoc_num;
1823 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1824 bool was_operational;
1827 if (!attrs[MACSEC_ATTR_IFINDEX])
1830 if (parse_sa_config(attrs, tb_sa))
1833 if (!validate_add_txsa(tb_sa))
1837 dev = get_dev_from_nl(genl_info_net(info), attrs);
1840 return PTR_ERR(dev);
1843 secy = &macsec_priv(dev)->secy;
1844 tx_sc = &secy->tx_sc;
1846 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1848 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1849 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1850 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1855 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1861 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1867 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1868 secy->key_len, secy->icv_len);
1875 spin_lock_bh(&tx_sa->lock);
1876 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1877 spin_unlock_bh(&tx_sa->lock);
1879 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1880 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1882 was_operational = secy->operational;
1883 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1884 secy->operational = true;
1886 /* If h/w offloading is available, propagate to the device */
1887 if (macsec_is_offloaded(netdev_priv(dev))) {
1888 const struct macsec_ops *ops;
1889 struct macsec_context ctx;
1891 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1897 ctx.sa.assoc_num = assoc_num;
1898 ctx.sa.tx_sa = tx_sa;
1899 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1902 err = macsec_offload(ops->mdo_add_txsa, &ctx);
1907 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1908 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1915 secy->operational = was_operational;
1921 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1923 struct nlattr **attrs = info->attrs;
1924 struct net_device *dev;
1925 struct macsec_secy *secy;
1926 struct macsec_rx_sc *rx_sc;
1927 struct macsec_rx_sa *rx_sa;
1929 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1930 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1933 if (!attrs[MACSEC_ATTR_IFINDEX])
1936 if (parse_sa_config(attrs, tb_sa))
1939 if (parse_rxsc_config(attrs, tb_rxsc))
1943 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1944 &dev, &secy, &rx_sc, &assoc_num);
1945 if (IS_ERR(rx_sa)) {
1947 return PTR_ERR(rx_sa);
1950 if (rx_sa->active) {
1955 /* If h/w offloading is available, propagate to the device */
1956 if (macsec_is_offloaded(netdev_priv(dev))) {
1957 const struct macsec_ops *ops;
1958 struct macsec_context ctx;
1960 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1966 ctx.sa.assoc_num = assoc_num;
1967 ctx.sa.rx_sa = rx_sa;
1969 ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
1974 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1986 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1988 struct nlattr **attrs = info->attrs;
1989 struct net_device *dev;
1990 struct macsec_secy *secy;
1991 struct macsec_rx_sc *rx_sc;
1993 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1996 if (!attrs[MACSEC_ATTR_IFINDEX])
1999 if (parse_rxsc_config(attrs, tb_rxsc))
2002 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2006 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
2009 return PTR_ERR(dev);
2012 secy = &macsec_priv(dev)->secy;
2013 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2015 rx_sc = del_rx_sc(secy, sci);
2021 /* If h/w offloading is available, propagate to the device */
2022 if (macsec_is_offloaded(netdev_priv(dev))) {
2023 const struct macsec_ops *ops;
2024 struct macsec_context ctx;
2026 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2033 ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
2048 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2050 struct nlattr **attrs = info->attrs;
2051 struct net_device *dev;
2052 struct macsec_secy *secy;
2053 struct macsec_tx_sc *tx_sc;
2054 struct macsec_tx_sa *tx_sa;
2056 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2059 if (!attrs[MACSEC_ATTR_IFINDEX])
2062 if (parse_sa_config(attrs, tb_sa))
2066 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2067 &dev, &secy, &tx_sc, &assoc_num);
2068 if (IS_ERR(tx_sa)) {
2070 return PTR_ERR(tx_sa);
2073 if (tx_sa->active) {
2078 /* If h/w offloading is available, propagate to the device */
2079 if (macsec_is_offloaded(netdev_priv(dev))) {
2080 const struct macsec_ops *ops;
2081 struct macsec_context ctx;
2083 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2089 ctx.sa.assoc_num = assoc_num;
2090 ctx.sa.tx_sa = tx_sa;
2092 ret = macsec_offload(ops->mdo_del_txsa, &ctx);
2097 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2109 static bool validate_upd_sa(struct nlattr **attrs)
2111 if (!attrs[MACSEC_SA_ATTR_AN] ||
2112 attrs[MACSEC_SA_ATTR_KEY] ||
2113 attrs[MACSEC_SA_ATTR_KEYID])
2116 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2119 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2122 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2123 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2130 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2132 struct nlattr **attrs = info->attrs;
2133 struct net_device *dev;
2134 struct macsec_secy *secy;
2135 struct macsec_tx_sc *tx_sc;
2136 struct macsec_tx_sa *tx_sa;
2138 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2139 bool was_operational, was_active;
2143 if (!attrs[MACSEC_ATTR_IFINDEX])
2146 if (parse_sa_config(attrs, tb_sa))
2149 if (!validate_upd_sa(tb_sa))
2153 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2154 &dev, &secy, &tx_sc, &assoc_num);
2155 if (IS_ERR(tx_sa)) {
2157 return PTR_ERR(tx_sa);
2160 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2161 spin_lock_bh(&tx_sa->lock);
2162 prev_pn = tx_sa->next_pn;
2163 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2164 spin_unlock_bh(&tx_sa->lock);
2167 was_active = tx_sa->active;
2168 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2169 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2171 was_operational = secy->operational;
2172 if (assoc_num == tx_sc->encoding_sa)
2173 secy->operational = tx_sa->active;
2175 /* If h/w offloading is available, propagate to the device */
2176 if (macsec_is_offloaded(netdev_priv(dev))) {
2177 const struct macsec_ops *ops;
2178 struct macsec_context ctx;
2180 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2186 ctx.sa.assoc_num = assoc_num;
2187 ctx.sa.tx_sa = tx_sa;
2189 ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
2199 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2200 spin_lock_bh(&tx_sa->lock);
2201 tx_sa->next_pn = prev_pn;
2202 spin_unlock_bh(&tx_sa->lock);
2204 tx_sa->active = was_active;
2205 secy->operational = was_operational;
2210 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2212 struct nlattr **attrs = info->attrs;
2213 struct net_device *dev;
2214 struct macsec_secy *secy;
2215 struct macsec_rx_sc *rx_sc;
2216 struct macsec_rx_sa *rx_sa;
2218 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2219 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2224 if (!attrs[MACSEC_ATTR_IFINDEX])
2227 if (parse_rxsc_config(attrs, tb_rxsc))
2230 if (parse_sa_config(attrs, tb_sa))
2233 if (!validate_upd_sa(tb_sa))
2237 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2238 &dev, &secy, &rx_sc, &assoc_num);
2239 if (IS_ERR(rx_sa)) {
2241 return PTR_ERR(rx_sa);
2244 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2245 spin_lock_bh(&rx_sa->lock);
2246 prev_pn = rx_sa->next_pn;
2247 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2248 spin_unlock_bh(&rx_sa->lock);
2251 was_active = rx_sa->active;
2252 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2253 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2255 /* If h/w offloading is available, propagate to the device */
2256 if (macsec_is_offloaded(netdev_priv(dev))) {
2257 const struct macsec_ops *ops;
2258 struct macsec_context ctx;
2260 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2266 ctx.sa.assoc_num = assoc_num;
2267 ctx.sa.rx_sa = rx_sa;
2269 ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
2278 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2279 spin_lock_bh(&rx_sa->lock);
2280 rx_sa->next_pn = prev_pn;
2281 spin_unlock_bh(&rx_sa->lock);
2283 rx_sa->active = was_active;
2288 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2290 struct nlattr **attrs = info->attrs;
2291 struct net_device *dev;
2292 struct macsec_secy *secy;
2293 struct macsec_rx_sc *rx_sc;
2294 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2295 unsigned int prev_n_rx_sc;
2299 if (!attrs[MACSEC_ATTR_IFINDEX])
2302 if (parse_rxsc_config(attrs, tb_rxsc))
2305 if (!validate_add_rxsc(tb_rxsc))
2309 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2310 if (IS_ERR(rx_sc)) {
2312 return PTR_ERR(rx_sc);
2315 was_active = rx_sc->active;
2316 prev_n_rx_sc = secy->n_rx_sc;
2317 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2318 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2320 if (rx_sc->active != new)
2321 secy->n_rx_sc += new ? 1 : -1;
2323 rx_sc->active = new;
2326 /* If h/w offloading is available, propagate to the device */
2327 if (macsec_is_offloaded(netdev_priv(dev))) {
2328 const struct macsec_ops *ops;
2329 struct macsec_context ctx;
2331 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2339 ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
2349 secy->n_rx_sc = prev_n_rx_sc;
2350 rx_sc->active = was_active;
2355 static bool macsec_is_configured(struct macsec_dev *macsec)
2357 struct macsec_secy *secy = &macsec->secy;
2358 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2361 if (secy->n_rx_sc > 0)
2364 for (i = 0; i < MACSEC_NUM_AN; i++)
2371 static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
2373 struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
2374 enum macsec_offload offload, prev_offload;
2375 int (*func)(struct macsec_context *ctx);
2376 struct nlattr **attrs = info->attrs;
2377 struct net_device *dev, *loop_dev;
2378 const struct macsec_ops *ops;
2379 struct macsec_context ctx;
2380 struct macsec_dev *macsec;
2381 struct net *loop_net;
2384 if (!attrs[MACSEC_ATTR_IFINDEX])
2387 if (!attrs[MACSEC_ATTR_OFFLOAD])
2390 if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
2391 attrs[MACSEC_ATTR_OFFLOAD],
2392 macsec_genl_offload_policy, NULL))
2395 dev = get_dev_from_nl(genl_info_net(info), attrs);
2397 return PTR_ERR(dev);
2398 macsec = macsec_priv(dev);
2400 offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
2401 if (macsec->offload == offload)
2404 /* Check if the offloading mode is supported by the underlying layers */
2405 if (offload != MACSEC_OFFLOAD_OFF &&
2406 !macsec_check_offload(offload, macsec))
2409 if (offload == MACSEC_OFFLOAD_OFF)
2410 goto skip_limitation;
2412 /* Check the physical interface isn't offloading another interface
2415 for_each_net(loop_net) {
2416 for_each_netdev(loop_net, loop_dev) {
2417 struct macsec_dev *priv;
2419 if (!netif_is_macsec(loop_dev))
2422 priv = macsec_priv(loop_dev);
2424 if (priv->real_dev == macsec->real_dev &&
2425 priv->offload != MACSEC_OFFLOAD_OFF)
2431 /* Check if the net device is busy. */
2432 if (netif_running(dev))
2437 prev_offload = macsec->offload;
2438 macsec->offload = offload;
2440 /* Check if the device already has rules configured: we do not support
2443 if (macsec_is_configured(macsec)) {
2448 ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
2455 if (prev_offload == MACSEC_OFFLOAD_OFF)
2456 func = ops->mdo_add_secy;
2458 func = ops->mdo_del_secy;
2460 ctx.secy = &macsec->secy;
2461 ret = macsec_offload(func, &ctx);
2469 macsec->offload = prev_offload;
2475 static int copy_tx_sa_stats(struct sk_buff *skb,
2476 struct macsec_tx_sa_stats __percpu *pstats)
2478 struct macsec_tx_sa_stats sum = {0, };
2481 for_each_possible_cpu(cpu) {
2482 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2484 sum.OutPktsProtected += stats->OutPktsProtected;
2485 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2488 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2489 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2495 static noinline_for_stack int
2496 copy_rx_sa_stats(struct sk_buff *skb,
2497 struct macsec_rx_sa_stats __percpu *pstats)
2499 struct macsec_rx_sa_stats sum = {0, };
2502 for_each_possible_cpu(cpu) {
2503 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2505 sum.InPktsOK += stats->InPktsOK;
2506 sum.InPktsInvalid += stats->InPktsInvalid;
2507 sum.InPktsNotValid += stats->InPktsNotValid;
2508 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2509 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2512 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2513 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2514 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2515 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2516 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2522 static noinline_for_stack int
2523 copy_rx_sc_stats(struct sk_buff *skb, struct pcpu_rx_sc_stats __percpu *pstats)
2525 struct macsec_rx_sc_stats sum = {0, };
2528 for_each_possible_cpu(cpu) {
2529 const struct pcpu_rx_sc_stats *stats;
2530 struct macsec_rx_sc_stats tmp;
2533 stats = per_cpu_ptr(pstats, cpu);
2535 start = u64_stats_fetch_begin_irq(&stats->syncp);
2536 memcpy(&tmp, &stats->stats, sizeof(tmp));
2537 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2539 sum.InOctetsValidated += tmp.InOctetsValidated;
2540 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2541 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2542 sum.InPktsDelayed += tmp.InPktsDelayed;
2543 sum.InPktsOK += tmp.InPktsOK;
2544 sum.InPktsInvalid += tmp.InPktsInvalid;
2545 sum.InPktsLate += tmp.InPktsLate;
2546 sum.InPktsNotValid += tmp.InPktsNotValid;
2547 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2548 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2551 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2552 sum.InOctetsValidated,
2553 MACSEC_RXSC_STATS_ATTR_PAD) ||
2554 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2555 sum.InOctetsDecrypted,
2556 MACSEC_RXSC_STATS_ATTR_PAD) ||
2557 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2558 sum.InPktsUnchecked,
2559 MACSEC_RXSC_STATS_ATTR_PAD) ||
2560 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2562 MACSEC_RXSC_STATS_ATTR_PAD) ||
2563 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2565 MACSEC_RXSC_STATS_ATTR_PAD) ||
2566 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2568 MACSEC_RXSC_STATS_ATTR_PAD) ||
2569 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2571 MACSEC_RXSC_STATS_ATTR_PAD) ||
2572 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2574 MACSEC_RXSC_STATS_ATTR_PAD) ||
2575 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2576 sum.InPktsNotUsingSA,
2577 MACSEC_RXSC_STATS_ATTR_PAD) ||
2578 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2580 MACSEC_RXSC_STATS_ATTR_PAD))
2586 static noinline_for_stack int
2587 copy_tx_sc_stats(struct sk_buff *skb, struct pcpu_tx_sc_stats __percpu *pstats)
2589 struct macsec_tx_sc_stats sum = {0, };
2592 for_each_possible_cpu(cpu) {
2593 const struct pcpu_tx_sc_stats *stats;
2594 struct macsec_tx_sc_stats tmp;
2597 stats = per_cpu_ptr(pstats, cpu);
2599 start = u64_stats_fetch_begin_irq(&stats->syncp);
2600 memcpy(&tmp, &stats->stats, sizeof(tmp));
2601 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2603 sum.OutPktsProtected += tmp.OutPktsProtected;
2604 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2605 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2606 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2609 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2610 sum.OutPktsProtected,
2611 MACSEC_TXSC_STATS_ATTR_PAD) ||
2612 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2613 sum.OutPktsEncrypted,
2614 MACSEC_TXSC_STATS_ATTR_PAD) ||
2615 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2616 sum.OutOctetsProtected,
2617 MACSEC_TXSC_STATS_ATTR_PAD) ||
2618 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2619 sum.OutOctetsEncrypted,
2620 MACSEC_TXSC_STATS_ATTR_PAD))
2626 static noinline_for_stack int
2627 copy_secy_stats(struct sk_buff *skb, struct pcpu_secy_stats __percpu *pstats)
2629 struct macsec_dev_stats sum = {0, };
2632 for_each_possible_cpu(cpu) {
2633 const struct pcpu_secy_stats *stats;
2634 struct macsec_dev_stats tmp;
2637 stats = per_cpu_ptr(pstats, cpu);
2639 start = u64_stats_fetch_begin_irq(&stats->syncp);
2640 memcpy(&tmp, &stats->stats, sizeof(tmp));
2641 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2643 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2644 sum.InPktsUntagged += tmp.InPktsUntagged;
2645 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2646 sum.InPktsNoTag += tmp.InPktsNoTag;
2647 sum.InPktsBadTag += tmp.InPktsBadTag;
2648 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2649 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2650 sum.InPktsOverrun += tmp.InPktsOverrun;
2653 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2654 sum.OutPktsUntagged,
2655 MACSEC_SECY_STATS_ATTR_PAD) ||
2656 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2658 MACSEC_SECY_STATS_ATTR_PAD) ||
2659 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2661 MACSEC_SECY_STATS_ATTR_PAD) ||
2662 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2664 MACSEC_SECY_STATS_ATTR_PAD) ||
2665 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2667 MACSEC_SECY_STATS_ATTR_PAD) ||
2668 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2669 sum.InPktsUnknownSCI,
2670 MACSEC_SECY_STATS_ATTR_PAD) ||
2671 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2673 MACSEC_SECY_STATS_ATTR_PAD) ||
2674 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2676 MACSEC_SECY_STATS_ATTR_PAD))
2682 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2684 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2685 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
2692 switch (secy->key_len) {
2693 case MACSEC_GCM_AES_128_SAK_LEN:
2694 csid = MACSEC_DEFAULT_CIPHER_ID;
2696 case MACSEC_GCM_AES_256_SAK_LEN:
2697 csid = MACSEC_CIPHER_ID_GCM_AES_256;
2703 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2704 MACSEC_SECY_ATTR_PAD) ||
2705 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2706 csid, MACSEC_SECY_ATTR_PAD) ||
2707 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2708 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2709 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2710 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2711 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2712 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2713 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2714 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2715 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2716 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2719 if (secy->replay_protect) {
2720 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2724 nla_nest_end(skb, secy_nest);
2728 nla_nest_cancel(skb, secy_nest);
2732 static noinline_for_stack int
2733 dump_secy(struct macsec_secy *secy, struct net_device *dev,
2734 struct sk_buff *skb, struct netlink_callback *cb)
2736 struct macsec_dev *macsec = netdev_priv(dev);
2737 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2738 struct nlattr *txsa_list, *rxsc_list;
2739 struct macsec_rx_sc *rx_sc;
2740 struct nlattr *attr;
2744 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2745 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2749 genl_dump_check_consistent(cb, hdr);
2751 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2752 goto nla_put_failure;
2754 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
2756 goto nla_put_failure;
2757 if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
2758 goto nla_put_failure;
2759 nla_nest_end(skb, attr);
2761 if (nla_put_secy(secy, skb))
2762 goto nla_put_failure;
2764 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
2766 goto nla_put_failure;
2767 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2768 nla_nest_cancel(skb, attr);
2769 goto nla_put_failure;
2771 nla_nest_end(skb, attr);
2773 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
2775 goto nla_put_failure;
2776 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2777 nla_nest_cancel(skb, attr);
2778 goto nla_put_failure;
2780 nla_nest_end(skb, attr);
2782 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
2784 goto nla_put_failure;
2785 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2786 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2787 struct nlattr *txsa_nest;
2792 txsa_nest = nla_nest_start_noflag(skb, j++);
2794 nla_nest_cancel(skb, txsa_list);
2795 goto nla_put_failure;
2798 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2799 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2800 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2801 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2802 nla_nest_cancel(skb, txsa_nest);
2803 nla_nest_cancel(skb, txsa_list);
2804 goto nla_put_failure;
2807 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
2809 nla_nest_cancel(skb, txsa_nest);
2810 nla_nest_cancel(skb, txsa_list);
2811 goto nla_put_failure;
2813 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2814 nla_nest_cancel(skb, attr);
2815 nla_nest_cancel(skb, txsa_nest);
2816 nla_nest_cancel(skb, txsa_list);
2817 goto nla_put_failure;
2819 nla_nest_end(skb, attr);
2821 nla_nest_end(skb, txsa_nest);
2823 nla_nest_end(skb, txsa_list);
2825 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
2827 goto nla_put_failure;
2830 for_each_rxsc_rtnl(secy, rx_sc) {
2832 struct nlattr *rxsa_list;
2833 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
2836 nla_nest_cancel(skb, rxsc_list);
2837 goto nla_put_failure;
2840 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2841 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2842 MACSEC_RXSC_ATTR_PAD)) {
2843 nla_nest_cancel(skb, rxsc_nest);
2844 nla_nest_cancel(skb, rxsc_list);
2845 goto nla_put_failure;
2848 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
2850 nla_nest_cancel(skb, rxsc_nest);
2851 nla_nest_cancel(skb, rxsc_list);
2852 goto nla_put_failure;
2854 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2855 nla_nest_cancel(skb, attr);
2856 nla_nest_cancel(skb, rxsc_nest);
2857 nla_nest_cancel(skb, rxsc_list);
2858 goto nla_put_failure;
2860 nla_nest_end(skb, attr);
2862 rxsa_list = nla_nest_start_noflag(skb,
2863 MACSEC_RXSC_ATTR_SA_LIST);
2865 nla_nest_cancel(skb, rxsc_nest);
2866 nla_nest_cancel(skb, rxsc_list);
2867 goto nla_put_failure;
2870 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2871 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2872 struct nlattr *rxsa_nest;
2877 rxsa_nest = nla_nest_start_noflag(skb, k++);
2879 nla_nest_cancel(skb, rxsa_list);
2880 nla_nest_cancel(skb, rxsc_nest);
2881 nla_nest_cancel(skb, rxsc_list);
2882 goto nla_put_failure;
2885 attr = nla_nest_start_noflag(skb,
2886 MACSEC_SA_ATTR_STATS);
2888 nla_nest_cancel(skb, rxsa_list);
2889 nla_nest_cancel(skb, rxsc_nest);
2890 nla_nest_cancel(skb, rxsc_list);
2891 goto nla_put_failure;
2893 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2894 nla_nest_cancel(skb, attr);
2895 nla_nest_cancel(skb, rxsa_list);
2896 nla_nest_cancel(skb, rxsc_nest);
2897 nla_nest_cancel(skb, rxsc_list);
2898 goto nla_put_failure;
2900 nla_nest_end(skb, attr);
2902 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2903 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2904 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2905 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2906 nla_nest_cancel(skb, rxsa_nest);
2907 nla_nest_cancel(skb, rxsc_nest);
2908 nla_nest_cancel(skb, rxsc_list);
2909 goto nla_put_failure;
2911 nla_nest_end(skb, rxsa_nest);
2914 nla_nest_end(skb, rxsa_list);
2915 nla_nest_end(skb, rxsc_nest);
2918 nla_nest_end(skb, rxsc_list);
2920 genlmsg_end(skb, hdr);
2925 genlmsg_cancel(skb, hdr);
2929 static int macsec_generation = 1; /* protected by RTNL */
2931 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2933 struct net *net = sock_net(skb->sk);
2934 struct net_device *dev;
2937 dev_idx = cb->args[0];
2942 cb->seq = macsec_generation;
2944 for_each_netdev(net, dev) {
2945 struct macsec_secy *secy;
2950 if (!netif_is_macsec(dev))
2953 secy = &macsec_priv(dev)->secy;
2954 if (dump_secy(secy, dev, skb, cb) < 0)
2966 static const struct genl_ops macsec_genl_ops[] = {
2968 .cmd = MACSEC_CMD_GET_TXSC,
2969 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2970 .dumpit = macsec_dump_txsc,
2973 .cmd = MACSEC_CMD_ADD_RXSC,
2974 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2975 .doit = macsec_add_rxsc,
2976 .flags = GENL_ADMIN_PERM,
2979 .cmd = MACSEC_CMD_DEL_RXSC,
2980 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2981 .doit = macsec_del_rxsc,
2982 .flags = GENL_ADMIN_PERM,
2985 .cmd = MACSEC_CMD_UPD_RXSC,
2986 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2987 .doit = macsec_upd_rxsc,
2988 .flags = GENL_ADMIN_PERM,
2991 .cmd = MACSEC_CMD_ADD_TXSA,
2992 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2993 .doit = macsec_add_txsa,
2994 .flags = GENL_ADMIN_PERM,
2997 .cmd = MACSEC_CMD_DEL_TXSA,
2998 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2999 .doit = macsec_del_txsa,
3000 .flags = GENL_ADMIN_PERM,
3003 .cmd = MACSEC_CMD_UPD_TXSA,
3004 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3005 .doit = macsec_upd_txsa,
3006 .flags = GENL_ADMIN_PERM,
3009 .cmd = MACSEC_CMD_ADD_RXSA,
3010 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3011 .doit = macsec_add_rxsa,
3012 .flags = GENL_ADMIN_PERM,
3015 .cmd = MACSEC_CMD_DEL_RXSA,
3016 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3017 .doit = macsec_del_rxsa,
3018 .flags = GENL_ADMIN_PERM,
3021 .cmd = MACSEC_CMD_UPD_RXSA,
3022 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3023 .doit = macsec_upd_rxsa,
3024 .flags = GENL_ADMIN_PERM,
3027 .cmd = MACSEC_CMD_UPD_OFFLOAD,
3028 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3029 .doit = macsec_upd_offload,
3030 .flags = GENL_ADMIN_PERM,
3034 static struct genl_family macsec_fam __ro_after_init = {
3035 .name = MACSEC_GENL_NAME,
3037 .version = MACSEC_GENL_VERSION,
3038 .maxattr = MACSEC_ATTR_MAX,
3039 .policy = macsec_genl_policy,
3041 .module = THIS_MODULE,
3042 .ops = macsec_genl_ops,
3043 .n_ops = ARRAY_SIZE(macsec_genl_ops),
3046 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
3047 struct net_device *dev)
3049 struct macsec_dev *macsec = netdev_priv(dev);
3050 struct macsec_secy *secy = &macsec->secy;
3051 struct pcpu_secy_stats *secy_stats;
3054 if (macsec_is_offloaded(netdev_priv(dev))) {
3055 skb->dev = macsec->real_dev;
3056 return dev_queue_xmit(skb);
3060 if (!secy->protect_frames) {
3061 secy_stats = this_cpu_ptr(macsec->stats);
3062 u64_stats_update_begin(&secy_stats->syncp);
3063 secy_stats->stats.OutPktsUntagged++;
3064 u64_stats_update_end(&secy_stats->syncp);
3065 skb->dev = macsec->real_dev;
3067 ret = dev_queue_xmit(skb);
3068 count_tx(dev, ret, len);
3072 if (!secy->operational) {
3074 dev->stats.tx_dropped++;
3075 return NETDEV_TX_OK;
3078 skb = macsec_encrypt(skb, dev);
3080 if (PTR_ERR(skb) != -EINPROGRESS)
3081 dev->stats.tx_dropped++;
3082 return NETDEV_TX_OK;
3085 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
3087 macsec_encrypt_finish(skb, dev);
3089 ret = dev_queue_xmit(skb);
3090 count_tx(dev, ret, len);
3094 #define MACSEC_FEATURES \
3095 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
3097 static int macsec_dev_init(struct net_device *dev)
3099 struct macsec_dev *macsec = macsec_priv(dev);
3100 struct net_device *real_dev = macsec->real_dev;
3103 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3107 err = gro_cells_init(&macsec->gro_cells, dev);
3109 free_percpu(dev->tstats);
3113 dev->features = real_dev->features & MACSEC_FEATURES;
3114 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
3116 dev->needed_headroom = real_dev->needed_headroom +
3117 MACSEC_NEEDED_HEADROOM;
3118 dev->needed_tailroom = real_dev->needed_tailroom +
3119 MACSEC_NEEDED_TAILROOM;
3121 if (is_zero_ether_addr(dev->dev_addr))
3122 eth_hw_addr_inherit(dev, real_dev);
3123 if (is_zero_ether_addr(dev->broadcast))
3124 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
3129 static void macsec_dev_uninit(struct net_device *dev)
3131 struct macsec_dev *macsec = macsec_priv(dev);
3133 gro_cells_destroy(&macsec->gro_cells);
3134 free_percpu(dev->tstats);
3137 static netdev_features_t macsec_fix_features(struct net_device *dev,
3138 netdev_features_t features)
3140 struct macsec_dev *macsec = macsec_priv(dev);
3141 struct net_device *real_dev = macsec->real_dev;
3143 features &= (real_dev->features & MACSEC_FEATURES) |
3144 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
3145 features |= NETIF_F_LLTX;
3150 static int macsec_dev_open(struct net_device *dev)
3152 struct macsec_dev *macsec = macsec_priv(dev);
3153 struct net_device *real_dev = macsec->real_dev;
3156 err = dev_uc_add(real_dev, dev->dev_addr);
3160 if (dev->flags & IFF_ALLMULTI) {
3161 err = dev_set_allmulti(real_dev, 1);
3166 if (dev->flags & IFF_PROMISC) {
3167 err = dev_set_promiscuity(real_dev, 1);
3169 goto clear_allmulti;
3172 /* If h/w offloading is available, propagate to the device */
3173 if (macsec_is_offloaded(macsec)) {
3174 const struct macsec_ops *ops;
3175 struct macsec_context ctx;
3177 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3180 goto clear_allmulti;
3183 err = macsec_offload(ops->mdo_dev_open, &ctx);
3185 goto clear_allmulti;
3188 if (netif_carrier_ok(real_dev))
3189 netif_carrier_on(dev);
3193 if (dev->flags & IFF_ALLMULTI)
3194 dev_set_allmulti(real_dev, -1);
3196 dev_uc_del(real_dev, dev->dev_addr);
3197 netif_carrier_off(dev);
3201 static int macsec_dev_stop(struct net_device *dev)
3203 struct macsec_dev *macsec = macsec_priv(dev);
3204 struct net_device *real_dev = macsec->real_dev;
3206 netif_carrier_off(dev);
3208 /* If h/w offloading is available, propagate to the device */
3209 if (macsec_is_offloaded(macsec)) {
3210 const struct macsec_ops *ops;
3211 struct macsec_context ctx;
3213 ops = macsec_get_ops(macsec, &ctx);
3215 macsec_offload(ops->mdo_dev_stop, &ctx);
3218 dev_mc_unsync(real_dev, dev);
3219 dev_uc_unsync(real_dev, dev);
3221 if (dev->flags & IFF_ALLMULTI)
3222 dev_set_allmulti(real_dev, -1);
3224 if (dev->flags & IFF_PROMISC)
3225 dev_set_promiscuity(real_dev, -1);
3227 dev_uc_del(real_dev, dev->dev_addr);
3232 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
3234 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3236 if (!(dev->flags & IFF_UP))
3239 if (change & IFF_ALLMULTI)
3240 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
3242 if (change & IFF_PROMISC)
3243 dev_set_promiscuity(real_dev,
3244 dev->flags & IFF_PROMISC ? 1 : -1);
3247 static void macsec_dev_set_rx_mode(struct net_device *dev)
3249 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3251 dev_mc_sync(real_dev, dev);
3252 dev_uc_sync(real_dev, dev);
3255 static int macsec_set_mac_address(struct net_device *dev, void *p)
3257 struct macsec_dev *macsec = macsec_priv(dev);
3258 struct net_device *real_dev = macsec->real_dev;
3259 struct sockaddr *addr = p;
3262 if (!is_valid_ether_addr(addr->sa_data))
3263 return -EADDRNOTAVAIL;
3265 if (!(dev->flags & IFF_UP))
3268 err = dev_uc_add(real_dev, addr->sa_data);
3272 dev_uc_del(real_dev, dev->dev_addr);
3275 ether_addr_copy(dev->dev_addr, addr->sa_data);
3276 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
3278 /* If h/w offloading is available, propagate to the device */
3279 if (macsec_is_offloaded(macsec)) {
3280 const struct macsec_ops *ops;
3281 struct macsec_context ctx;
3283 ops = macsec_get_ops(macsec, &ctx);
3285 ctx.secy = &macsec->secy;
3286 macsec_offload(ops->mdo_upd_secy, &ctx);
3293 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
3295 struct macsec_dev *macsec = macsec_priv(dev);
3296 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
3298 if (macsec->real_dev->mtu - extra < new_mtu)
3306 static void macsec_get_stats64(struct net_device *dev,
3307 struct rtnl_link_stats64 *s)
3314 for_each_possible_cpu(cpu) {
3315 struct pcpu_sw_netstats *stats;
3316 struct pcpu_sw_netstats tmp;
3319 stats = per_cpu_ptr(dev->tstats, cpu);
3321 start = u64_stats_fetch_begin_irq(&stats->syncp);
3322 tmp.rx_packets = stats->rx_packets;
3323 tmp.rx_bytes = stats->rx_bytes;
3324 tmp.tx_packets = stats->tx_packets;
3325 tmp.tx_bytes = stats->tx_bytes;
3326 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
3328 s->rx_packets += tmp.rx_packets;
3329 s->rx_bytes += tmp.rx_bytes;
3330 s->tx_packets += tmp.tx_packets;
3331 s->tx_bytes += tmp.tx_bytes;
3334 s->rx_dropped = dev->stats.rx_dropped;
3335 s->tx_dropped = dev->stats.tx_dropped;
3338 static int macsec_get_iflink(const struct net_device *dev)
3340 return macsec_priv(dev)->real_dev->ifindex;
3343 static const struct net_device_ops macsec_netdev_ops = {
3344 .ndo_init = macsec_dev_init,
3345 .ndo_uninit = macsec_dev_uninit,
3346 .ndo_open = macsec_dev_open,
3347 .ndo_stop = macsec_dev_stop,
3348 .ndo_fix_features = macsec_fix_features,
3349 .ndo_change_mtu = macsec_change_mtu,
3350 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
3351 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
3352 .ndo_set_mac_address = macsec_set_mac_address,
3353 .ndo_start_xmit = macsec_start_xmit,
3354 .ndo_get_stats64 = macsec_get_stats64,
3355 .ndo_get_iflink = macsec_get_iflink,
3358 static const struct device_type macsec_type = {
3362 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3363 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3364 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3365 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3366 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3367 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3368 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3369 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3370 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3371 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3372 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
3373 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3374 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3375 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3378 static void macsec_free_netdev(struct net_device *dev)
3380 struct macsec_dev *macsec = macsec_priv(dev);
3382 free_percpu(macsec->stats);
3383 free_percpu(macsec->secy.tx_sc.stats);
3387 static void macsec_setup(struct net_device *dev)
3391 dev->max_mtu = ETH_MAX_MTU;
3392 dev->priv_flags |= IFF_NO_QUEUE;
3393 dev->netdev_ops = &macsec_netdev_ops;
3394 dev->needs_free_netdev = true;
3395 dev->priv_destructor = macsec_free_netdev;
3396 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3398 eth_zero_addr(dev->broadcast);
3401 static int macsec_changelink_common(struct net_device *dev,
3402 struct nlattr *data[])
3404 struct macsec_secy *secy;
3405 struct macsec_tx_sc *tx_sc;
3407 secy = &macsec_priv(dev)->secy;
3408 tx_sc = &secy->tx_sc;
3410 if (data[IFLA_MACSEC_ENCODING_SA]) {
3411 struct macsec_tx_sa *tx_sa;
3413 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3414 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3416 secy->operational = tx_sa && tx_sa->active;
3419 if (data[IFLA_MACSEC_WINDOW])
3420 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3422 if (data[IFLA_MACSEC_ENCRYPT])
3423 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3425 if (data[IFLA_MACSEC_PROTECT])
3426 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3428 if (data[IFLA_MACSEC_INC_SCI])
3429 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3431 if (data[IFLA_MACSEC_ES])
3432 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3434 if (data[IFLA_MACSEC_SCB])
3435 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3437 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3438 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3440 if (data[IFLA_MACSEC_VALIDATION])
3441 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3443 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3444 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3445 case MACSEC_CIPHER_ID_GCM_AES_128:
3446 case MACSEC_DEFAULT_CIPHER_ID:
3447 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3449 case MACSEC_CIPHER_ID_GCM_AES_256:
3450 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3460 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3461 struct nlattr *data[],
3462 struct netlink_ext_ack *extack)
3464 struct macsec_dev *macsec = macsec_priv(dev);
3465 struct macsec_tx_sa tx_sc;
3466 struct macsec_secy secy;
3472 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3473 data[IFLA_MACSEC_ICV_LEN] ||
3474 data[IFLA_MACSEC_SCI] ||
3475 data[IFLA_MACSEC_PORT])
3478 /* Keep a copy of unmodified secy and tx_sc, in case the offload
3479 * propagation fails, to revert macsec_changelink_common.
3481 memcpy(&secy, &macsec->secy, sizeof(secy));
3482 memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
3484 ret = macsec_changelink_common(dev, data);
3488 /* If h/w offloading is available, propagate to the device */
3489 if (macsec_is_offloaded(macsec)) {
3490 const struct macsec_ops *ops;
3491 struct macsec_context ctx;
3494 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3500 ctx.secy = &macsec->secy;
3501 ret = macsec_offload(ops->mdo_upd_secy, &ctx);
3509 memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
3510 memcpy(&macsec->secy, &secy, sizeof(secy));
3515 static void macsec_del_dev(struct macsec_dev *macsec)
3519 while (macsec->secy.rx_sc) {
3520 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3522 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3526 for (i = 0; i < MACSEC_NUM_AN; i++) {
3527 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3530 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3536 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3538 struct macsec_dev *macsec = macsec_priv(dev);
3539 struct net_device *real_dev = macsec->real_dev;
3541 unregister_netdevice_queue(dev, head);
3542 list_del_rcu(&macsec->secys);
3543 macsec_del_dev(macsec);
3544 netdev_upper_dev_unlink(real_dev, dev);
3546 macsec_generation++;
3549 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3551 struct macsec_dev *macsec = macsec_priv(dev);
3552 struct net_device *real_dev = macsec->real_dev;
3553 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3555 /* If h/w offloading is available, propagate to the device */
3556 if (macsec_is_offloaded(macsec)) {
3557 const struct macsec_ops *ops;
3558 struct macsec_context ctx;
3560 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3562 ctx.secy = &macsec->secy;
3563 macsec_offload(ops->mdo_del_secy, &ctx);
3567 macsec_common_dellink(dev, head);
3569 if (list_empty(&rxd->secys)) {
3570 netdev_rx_handler_unregister(real_dev);
3575 static int register_macsec_dev(struct net_device *real_dev,
3576 struct net_device *dev)
3578 struct macsec_dev *macsec = macsec_priv(dev);
3579 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3584 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3588 INIT_LIST_HEAD(&rxd->secys);
3590 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3598 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3602 static bool sci_exists(struct net_device *dev, sci_t sci)
3604 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3605 struct macsec_dev *macsec;
3607 list_for_each_entry(macsec, &rxd->secys, secys) {
3608 if (macsec->secy.sci == sci)
3615 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3617 struct macsec_dev *macsec = macsec_priv(dev);
3618 struct macsec_secy *secy = &macsec->secy;
3620 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3624 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3625 if (!secy->tx_sc.stats) {
3626 free_percpu(macsec->stats);
3630 if (sci == MACSEC_UNDEF_SCI)
3631 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3634 secy->operational = true;
3635 secy->key_len = DEFAULT_SAK_LEN;
3636 secy->icv_len = icv_len;
3637 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3638 secy->protect_frames = true;
3639 secy->replay_protect = false;
3642 secy->tx_sc.active = true;
3643 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3644 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3645 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3646 secy->tx_sc.end_station = false;
3647 secy->tx_sc.scb = false;
3652 static int macsec_newlink(struct net *net, struct net_device *dev,
3653 struct nlattr *tb[], struct nlattr *data[],
3654 struct netlink_ext_ack *extack)
3656 struct macsec_dev *macsec = macsec_priv(dev);
3657 struct net_device *real_dev;
3660 u8 icv_len = DEFAULT_ICV_LEN;
3661 rx_handler_func_t *rx_handler;
3665 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3669 dev->priv_flags |= IFF_MACSEC;
3671 macsec->real_dev = real_dev;
3673 /* MACsec offloading is off by default */
3674 macsec->offload = MACSEC_OFFLOAD_OFF;
3676 if (data && data[IFLA_MACSEC_ICV_LEN])
3677 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3678 dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3680 rx_handler = rtnl_dereference(real_dev->rx_handler);
3681 if (rx_handler && rx_handler != macsec_handle_frame)
3684 err = register_netdevice(dev);
3688 err = netdev_upper_dev_link(real_dev, dev, extack);
3692 /* need to be already registered so that ->init has run and
3693 * the MAC addr is set
3695 if (data && data[IFLA_MACSEC_SCI])
3696 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3697 else if (data && data[IFLA_MACSEC_PORT])
3698 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3700 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3702 if (rx_handler && sci_exists(real_dev, sci)) {
3707 err = macsec_add_dev(dev, sci, icv_len);
3712 err = macsec_changelink_common(dev, data);
3717 err = register_macsec_dev(real_dev, dev);
3721 netif_stacked_transfer_operstate(real_dev, dev);
3722 linkwatch_fire_event(dev);
3724 macsec_generation++;
3729 macsec_del_dev(macsec);
3731 netdev_upper_dev_unlink(real_dev, dev);
3733 unregister_netdevice(dev);
3737 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3738 struct netlink_ext_ack *extack)
3740 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3741 u8 icv_len = DEFAULT_ICV_LEN;
3748 if (data[IFLA_MACSEC_CIPHER_SUITE])
3749 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3751 if (data[IFLA_MACSEC_ICV_LEN]) {
3752 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3753 if (icv_len != DEFAULT_ICV_LEN) {
3754 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3755 struct crypto_aead *dummy_tfm;
3757 dummy_tfm = macsec_alloc_tfm(dummy_key,
3760 if (IS_ERR(dummy_tfm))
3761 return PTR_ERR(dummy_tfm);
3762 crypto_free_aead(dummy_tfm);
3767 case MACSEC_CIPHER_ID_GCM_AES_128:
3768 case MACSEC_CIPHER_ID_GCM_AES_256:
3769 case MACSEC_DEFAULT_CIPHER_ID:
3770 if (icv_len < MACSEC_MIN_ICV_LEN ||
3771 icv_len > MACSEC_STD_ICV_LEN)
3778 if (data[IFLA_MACSEC_ENCODING_SA]) {
3779 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3783 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3784 flag < IFLA_MACSEC_VALIDATION;
3787 if (nla_get_u8(data[flag]) > 1)
3792 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3793 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3794 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3796 if ((sci && (scb || es)) || (scb && es))
3799 if (data[IFLA_MACSEC_VALIDATION] &&
3800 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3803 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3804 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3805 !data[IFLA_MACSEC_WINDOW])
3811 static struct net *macsec_get_link_net(const struct net_device *dev)
3813 return dev_net(macsec_priv(dev)->real_dev);
3816 static size_t macsec_get_size(const struct net_device *dev)
3818 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
3819 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
3820 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
3821 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
3822 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
3823 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
3824 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
3825 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
3826 nla_total_size(1) + /* IFLA_MACSEC_ES */
3827 nla_total_size(1) + /* IFLA_MACSEC_SCB */
3828 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
3829 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
3833 static int macsec_fill_info(struct sk_buff *skb,
3834 const struct net_device *dev)
3836 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3837 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3840 switch (secy->key_len) {
3841 case MACSEC_GCM_AES_128_SAK_LEN:
3842 csid = MACSEC_DEFAULT_CIPHER_ID;
3844 case MACSEC_GCM_AES_256_SAK_LEN:
3845 csid = MACSEC_CIPHER_ID_GCM_AES_256;
3848 goto nla_put_failure;
3851 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3853 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3854 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3855 csid, IFLA_MACSEC_PAD) ||
3856 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3857 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3858 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3859 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3860 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3861 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3862 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3863 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3865 goto nla_put_failure;
3867 if (secy->replay_protect) {
3868 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3869 goto nla_put_failure;
3878 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3880 .priv_size = sizeof(struct macsec_dev),
3881 .maxtype = IFLA_MACSEC_MAX,
3882 .policy = macsec_rtnl_policy,
3883 .setup = macsec_setup,
3884 .validate = macsec_validate_attr,
3885 .newlink = macsec_newlink,
3886 .changelink = macsec_changelink,
3887 .dellink = macsec_dellink,
3888 .get_size = macsec_get_size,
3889 .fill_info = macsec_fill_info,
3890 .get_link_net = macsec_get_link_net,
3893 static bool is_macsec_master(struct net_device *dev)
3895 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3898 static int macsec_notify(struct notifier_block *this, unsigned long event,
3901 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3904 if (!is_macsec_master(real_dev))
3910 case NETDEV_CHANGE: {
3911 struct macsec_dev *m, *n;
3912 struct macsec_rxh_data *rxd;
3914 rxd = macsec_data_rtnl(real_dev);
3915 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3916 struct net_device *dev = m->secy.netdev;
3918 netif_stacked_transfer_operstate(real_dev, dev);
3922 case NETDEV_UNREGISTER: {
3923 struct macsec_dev *m, *n;
3924 struct macsec_rxh_data *rxd;
3926 rxd = macsec_data_rtnl(real_dev);
3927 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3928 macsec_common_dellink(m->secy.netdev, &head);
3931 netdev_rx_handler_unregister(real_dev);
3934 unregister_netdevice_many(&head);
3937 case NETDEV_CHANGEMTU: {
3938 struct macsec_dev *m;
3939 struct macsec_rxh_data *rxd;
3941 rxd = macsec_data_rtnl(real_dev);
3942 list_for_each_entry(m, &rxd->secys, secys) {
3943 struct net_device *dev = m->secy.netdev;
3944 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3945 macsec_extra_len(true));
3948 dev_set_mtu(dev, mtu);
3956 static struct notifier_block macsec_notifier = {
3957 .notifier_call = macsec_notify,
3960 static int __init macsec_init(void)
3964 pr_info("MACsec IEEE 802.1AE\n");
3965 err = register_netdevice_notifier(&macsec_notifier);
3969 err = rtnl_link_register(&macsec_link_ops);
3973 err = genl_register_family(&macsec_fam);
3980 rtnl_link_unregister(&macsec_link_ops);
3982 unregister_netdevice_notifier(&macsec_notifier);
3986 static void __exit macsec_exit(void)
3988 genl_unregister_family(&macsec_fam);
3989 rtnl_link_unregister(&macsec_link_ops);
3990 unregister_netdevice_notifier(&macsec_notifier);
3994 module_init(macsec_init);
3995 module_exit(macsec_exit);
3997 MODULE_ALIAS_RTNL_LINK("macsec");
3998 MODULE_ALIAS_GENL_FAMILY("macsec");
4000 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
4001 MODULE_LICENSE("GPL v2");