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>
22 #include <linux/byteorder/generic.h>
23 #include <linux/if_arp.h>
25 #include <uapi/linux/if_macsec.h>
27 #define MACSEC_SCI_LEN 8
29 /* SecTAG length = macsec_eth_header without the optional SCI */
30 #define MACSEC_TAG_LEN 6
32 struct macsec_eth_header {
36 #if defined(__LITTLE_ENDIAN_BITFIELD)
39 #elif defined(__BIG_ENDIAN_BITFIELD)
43 #error "Please fix <asm/byteorder.h>"
46 u8 secure_channel_id[8]; /* optional */
49 #define MACSEC_TCI_VERSION 0x80
50 #define MACSEC_TCI_ES 0x40 /* end station */
51 #define MACSEC_TCI_SC 0x20 /* SCI present */
52 #define MACSEC_TCI_SCB 0x10 /* epon */
53 #define MACSEC_TCI_E 0x08 /* encryption */
54 #define MACSEC_TCI_C 0x04 /* changed text */
55 #define MACSEC_AN_MASK 0x03 /* association number */
56 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
58 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
59 #define MIN_NON_SHORT_LEN 48
61 #define GCM_AES_IV_LEN 12
62 #define DEFAULT_ICV_LEN 16
64 #define for_each_rxsc(secy, sc) \
65 for (sc = rcu_dereference_bh(secy->rx_sc); \
67 sc = rcu_dereference_bh(sc->next))
68 #define for_each_rxsc_rtnl(secy, sc) \
69 for (sc = rtnl_dereference(secy->rx_sc); \
71 sc = rtnl_dereference(sc->next))
73 #define pn_same_half(pn1, pn2) (!(((pn1) >> 31) ^ ((pn2) >> 31)))
77 u8 short_secure_channel_id[4];
85 u8 secure_channel_id[8];
91 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
93 struct pcpu_secy_stats {
94 struct macsec_dev_stats stats;
95 struct u64_stats_sync syncp;
99 * struct macsec_dev - private data
101 * @real_dev: pointer to underlying netdevice
102 * @dev_tracker: refcount tracker for @real_dev reference
103 * @stats: MACsec device stats
104 * @secys: linked list of SecY's on the underlying device
105 * @gro_cells: pointer to the Generic Receive Offload cell
106 * @offload: status of offloading on the MACsec device
109 struct macsec_secy secy;
110 struct net_device *real_dev;
111 netdevice_tracker dev_tracker;
112 struct pcpu_secy_stats __percpu *stats;
113 struct list_head secys;
114 struct gro_cells gro_cells;
115 enum macsec_offload offload;
119 * struct macsec_rxh_data - rx_handler private argument
120 * @secys: linked list of SecY's on this underlying device
122 struct macsec_rxh_data {
123 struct list_head secys;
126 static struct macsec_dev *macsec_priv(const struct net_device *dev)
128 return (struct macsec_dev *)netdev_priv(dev);
131 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
133 return rcu_dereference_bh(dev->rx_handler_data);
136 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
138 return rtnl_dereference(dev->rx_handler_data);
142 struct aead_request *req;
144 struct macsec_tx_sa *tx_sa;
145 struct macsec_rx_sa *rx_sa;
152 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
154 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
156 if (!sa || !sa->active)
159 if (!refcount_inc_not_zero(&sa->refcnt))
165 static void free_rx_sc_rcu(struct rcu_head *head)
167 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
169 free_percpu(rx_sc->stats);
173 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
175 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
178 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
180 if (refcount_dec_and_test(&sc->refcnt))
181 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
184 static void free_rxsa(struct rcu_head *head)
186 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
188 crypto_free_aead(sa->key.tfm);
189 free_percpu(sa->stats);
193 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
195 if (refcount_dec_and_test(&sa->refcnt))
196 call_rcu(&sa->rcu, free_rxsa);
199 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
201 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
203 if (!sa || !sa->active)
206 if (!refcount_inc_not_zero(&sa->refcnt))
212 static void free_txsa(struct rcu_head *head)
214 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
216 crypto_free_aead(sa->key.tfm);
217 free_percpu(sa->stats);
221 static void macsec_txsa_put(struct macsec_tx_sa *sa)
223 if (refcount_dec_and_test(&sa->refcnt))
224 call_rcu(&sa->rcu, free_txsa);
227 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
229 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
230 return (struct macsec_cb *)skb->cb;
233 #define MACSEC_PORT_ES (htons(0x0001))
234 #define MACSEC_PORT_SCB (0x0000)
235 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
236 #define MACSEC_UNDEF_SSCI ((__force ssci_t)0xffffffff)
238 #define MACSEC_GCM_AES_128_SAK_LEN 16
239 #define MACSEC_GCM_AES_256_SAK_LEN 32
241 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
242 #define DEFAULT_XPN false
243 #define DEFAULT_SEND_SCI true
244 #define DEFAULT_ENCRYPT false
245 #define DEFAULT_ENCODING_SA 0
246 #define MACSEC_XPN_MAX_REPLAY_WINDOW (((1 << 30) - 1))
248 static bool send_sci(const struct macsec_secy *secy)
250 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
252 return tx_sc->send_sci ||
253 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
256 static sci_t make_sci(const u8 *addr, __be16 port)
260 memcpy(&sci, addr, ETH_ALEN);
261 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
266 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
271 memcpy(&sci, hdr->secure_channel_id,
272 sizeof(hdr->secure_channel_id));
274 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
279 static unsigned int macsec_sectag_len(bool sci_present)
281 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
284 static unsigned int macsec_hdr_len(bool sci_present)
286 return macsec_sectag_len(sci_present) + ETH_HLEN;
289 static unsigned int macsec_extra_len(bool sci_present)
291 return macsec_sectag_len(sci_present) + sizeof(__be16);
294 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
295 static void macsec_fill_sectag(struct macsec_eth_header *h,
296 const struct macsec_secy *secy, u32 pn,
299 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
301 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
302 h->eth.h_proto = htons(ETH_P_MACSEC);
305 h->tci_an |= MACSEC_TCI_SC;
306 memcpy(&h->secure_channel_id, &secy->sci,
307 sizeof(h->secure_channel_id));
309 if (tx_sc->end_station)
310 h->tci_an |= MACSEC_TCI_ES;
312 h->tci_an |= MACSEC_TCI_SCB;
315 h->packet_number = htonl(pn);
317 /* with GCM, C/E clear for !encrypt, both set for encrypt */
319 h->tci_an |= MACSEC_TCI_CONFID;
320 else if (secy->icv_len != DEFAULT_ICV_LEN)
321 h->tci_an |= MACSEC_TCI_C;
323 h->tci_an |= tx_sc->encoding_sa;
326 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
328 if (data_len < MIN_NON_SHORT_LEN)
329 h->short_length = data_len;
332 /* Checks if a MACsec interface is being offloaded to an hardware engine */
333 static bool macsec_is_offloaded(struct macsec_dev *macsec)
335 if (macsec->offload == MACSEC_OFFLOAD_MAC ||
336 macsec->offload == MACSEC_OFFLOAD_PHY)
342 /* Checks if underlying layers implement MACsec offloading functions. */
343 static bool macsec_check_offload(enum macsec_offload offload,
344 struct macsec_dev *macsec)
346 if (!macsec || !macsec->real_dev)
349 if (offload == MACSEC_OFFLOAD_PHY)
350 return macsec->real_dev->phydev &&
351 macsec->real_dev->phydev->macsec_ops;
352 else if (offload == MACSEC_OFFLOAD_MAC)
353 return macsec->real_dev->features & NETIF_F_HW_MACSEC &&
354 macsec->real_dev->macsec_ops;
359 static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload,
360 struct macsec_dev *macsec,
361 struct macsec_context *ctx)
364 memset(ctx, 0, sizeof(*ctx));
365 ctx->offload = offload;
367 if (offload == MACSEC_OFFLOAD_PHY)
368 ctx->phydev = macsec->real_dev->phydev;
369 else if (offload == MACSEC_OFFLOAD_MAC)
370 ctx->netdev = macsec->real_dev;
373 if (offload == MACSEC_OFFLOAD_PHY)
374 return macsec->real_dev->phydev->macsec_ops;
376 return macsec->real_dev->macsec_ops;
379 /* Returns a pointer to the MACsec ops struct if any and updates the MACsec
380 * context device reference if provided.
382 static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec,
383 struct macsec_context *ctx)
385 if (!macsec_check_offload(macsec->offload, macsec))
388 return __macsec_get_ops(macsec->offload, macsec, ctx);
391 /* validate MACsec packet according to IEEE 802.1AE-2018 9.12 */
392 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len, bool xpn)
394 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
395 int len = skb->len - 2 * ETH_ALEN;
396 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
398 /* a) It comprises at least 17 octets */
402 /* b) MACsec EtherType: already checked */
404 /* c) V bit is clear */
405 if (h->tci_an & MACSEC_TCI_VERSION)
408 /* d) ES or SCB => !SC */
409 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
410 (h->tci_an & MACSEC_TCI_SC))
413 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
417 /* rx.pn != 0 if not XPN (figure 10-5 with 802.11AEbw-2013 amendment) */
418 if (!h->packet_number && !xpn)
421 /* length check, f) g) h) i) */
423 return len == extra_len + h->short_length;
424 return len >= extra_len + MIN_NON_SHORT_LEN;
427 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
428 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
430 static void macsec_fill_iv_xpn(unsigned char *iv, ssci_t ssci, u64 pn,
433 struct gcm_iv_xpn *gcm_iv = (struct gcm_iv_xpn *)iv;
435 gcm_iv->ssci = ssci ^ salt.ssci;
436 gcm_iv->pn = cpu_to_be64(pn) ^ salt.pn;
439 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
441 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
444 gcm_iv->pn = htonl(pn);
447 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
449 return (struct macsec_eth_header *)skb_mac_header(skb);
452 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
454 return make_sci(dev->dev_addr, port);
457 static void __macsec_pn_wrapped(struct macsec_secy *secy,
458 struct macsec_tx_sa *tx_sa)
460 pr_debug("PN wrapped, transitioning to !oper\n");
461 tx_sa->active = false;
462 if (secy->protect_frames)
463 secy->operational = false;
466 void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa)
468 spin_lock_bh(&tx_sa->lock);
469 __macsec_pn_wrapped(secy, tx_sa);
470 spin_unlock_bh(&tx_sa->lock);
472 EXPORT_SYMBOL_GPL(macsec_pn_wrapped);
474 static pn_t tx_sa_update_pn(struct macsec_tx_sa *tx_sa,
475 struct macsec_secy *secy)
479 spin_lock_bh(&tx_sa->lock);
481 pn = tx_sa->next_pn_halves;
485 tx_sa->next_pn_halves.lower++;
487 if (tx_sa->next_pn == 0)
488 __macsec_pn_wrapped(secy, tx_sa);
489 spin_unlock_bh(&tx_sa->lock);
494 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
496 struct macsec_dev *macsec = netdev_priv(dev);
498 skb->dev = macsec->real_dev;
499 skb_reset_mac_header(skb);
500 skb->protocol = eth_hdr(skb)->h_proto;
503 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
504 struct macsec_tx_sa *tx_sa)
506 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
508 u64_stats_update_begin(&txsc_stats->syncp);
509 if (tx_sc->encrypt) {
510 txsc_stats->stats.OutOctetsEncrypted += skb->len;
511 txsc_stats->stats.OutPktsEncrypted++;
512 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
514 txsc_stats->stats.OutOctetsProtected += skb->len;
515 txsc_stats->stats.OutPktsProtected++;
516 this_cpu_inc(tx_sa->stats->OutPktsProtected);
518 u64_stats_update_end(&txsc_stats->syncp);
521 static void count_tx(struct net_device *dev, int ret, int len)
523 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
524 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
526 u64_stats_update_begin(&stats->syncp);
528 stats->tx_bytes += len;
529 u64_stats_update_end(&stats->syncp);
533 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
535 struct sk_buff *skb = base->data;
536 struct net_device *dev = skb->dev;
537 struct macsec_dev *macsec = macsec_priv(dev);
538 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
541 aead_request_free(macsec_skb_cb(skb)->req);
544 macsec_encrypt_finish(skb, dev);
545 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
547 ret = dev_queue_xmit(skb);
548 count_tx(dev, ret, len);
549 rcu_read_unlock_bh();
555 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
557 struct scatterlist **sg,
560 size_t size, iv_offset, sg_offset;
561 struct aead_request *req;
564 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
566 size += GCM_AES_IV_LEN;
568 size = ALIGN(size, __alignof__(struct scatterlist));
570 size += sizeof(struct scatterlist) * num_frags;
572 tmp = kmalloc(size, GFP_ATOMIC);
576 *iv = (unsigned char *)(tmp + iv_offset);
577 *sg = (struct scatterlist *)(tmp + sg_offset);
580 aead_request_set_tfm(req, tfm);
585 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
586 struct net_device *dev)
589 struct scatterlist *sg;
590 struct sk_buff *trailer;
593 struct macsec_eth_header *hh;
594 size_t unprotected_len;
595 struct aead_request *req;
596 struct macsec_secy *secy;
597 struct macsec_tx_sc *tx_sc;
598 struct macsec_tx_sa *tx_sa;
599 struct macsec_dev *macsec = macsec_priv(dev);
603 secy = &macsec->secy;
604 tx_sc = &secy->tx_sc;
606 /* 10.5.1 TX SA assignment */
607 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
609 secy->operational = false;
611 return ERR_PTR(-EINVAL);
614 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
615 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
616 struct sk_buff *nskb = skb_copy_expand(skb,
617 MACSEC_NEEDED_HEADROOM,
618 MACSEC_NEEDED_TAILROOM,
624 macsec_txsa_put(tx_sa);
626 return ERR_PTR(-ENOMEM);
629 skb = skb_unshare(skb, GFP_ATOMIC);
631 macsec_txsa_put(tx_sa);
632 return ERR_PTR(-ENOMEM);
636 unprotected_len = skb->len;
638 sci_present = send_sci(secy);
639 hh = skb_push(skb, macsec_extra_len(sci_present));
640 memmove(hh, eth, 2 * ETH_ALEN);
642 pn = tx_sa_update_pn(tx_sa, secy);
643 if (pn.full64 == 0) {
644 macsec_txsa_put(tx_sa);
646 return ERR_PTR(-ENOLINK);
648 macsec_fill_sectag(hh, secy, pn.lower, sci_present);
649 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
651 skb_put(skb, secy->icv_len);
653 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
654 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
656 u64_stats_update_begin(&secy_stats->syncp);
657 secy_stats->stats.OutPktsTooLong++;
658 u64_stats_update_end(&secy_stats->syncp);
660 macsec_txsa_put(tx_sa);
662 return ERR_PTR(-EINVAL);
665 ret = skb_cow_data(skb, 0, &trailer);
666 if (unlikely(ret < 0)) {
667 macsec_txsa_put(tx_sa);
672 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
674 macsec_txsa_put(tx_sa);
676 return ERR_PTR(-ENOMEM);
680 macsec_fill_iv_xpn(iv, tx_sa->ssci, pn.full64, tx_sa->key.salt);
682 macsec_fill_iv(iv, secy->sci, pn.lower);
684 sg_init_table(sg, ret);
685 ret = skb_to_sgvec(skb, sg, 0, skb->len);
686 if (unlikely(ret < 0)) {
687 aead_request_free(req);
688 macsec_txsa_put(tx_sa);
693 if (tx_sc->encrypt) {
694 int len = skb->len - macsec_hdr_len(sci_present) -
696 aead_request_set_crypt(req, sg, sg, len, iv);
697 aead_request_set_ad(req, macsec_hdr_len(sci_present));
699 aead_request_set_crypt(req, sg, sg, 0, iv);
700 aead_request_set_ad(req, skb->len - secy->icv_len);
703 macsec_skb_cb(skb)->req = req;
704 macsec_skb_cb(skb)->tx_sa = tx_sa;
705 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
708 ret = crypto_aead_encrypt(req);
709 if (ret == -EINPROGRESS) {
711 } else if (ret != 0) {
714 aead_request_free(req);
715 macsec_txsa_put(tx_sa);
716 return ERR_PTR(-EINVAL);
720 aead_request_free(req);
721 macsec_txsa_put(tx_sa);
726 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
728 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
729 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
730 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
733 spin_lock(&rx_sa->lock);
734 if (rx_sa->next_pn_halves.lower >= secy->replay_window)
735 lowest_pn = rx_sa->next_pn_halves.lower - secy->replay_window;
737 /* Now perform replay protection check again
738 * (see IEEE 802.1AE-2006 figure 10-5)
740 if (secy->replay_protect && pn < lowest_pn &&
741 (!secy->xpn || pn_same_half(pn, lowest_pn))) {
742 spin_unlock(&rx_sa->lock);
743 u64_stats_update_begin(&rxsc_stats->syncp);
744 rxsc_stats->stats.InPktsLate++;
745 u64_stats_update_end(&rxsc_stats->syncp);
749 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
750 u64_stats_update_begin(&rxsc_stats->syncp);
751 if (hdr->tci_an & MACSEC_TCI_E)
752 rxsc_stats->stats.InOctetsDecrypted += skb->len;
754 rxsc_stats->stats.InOctetsValidated += skb->len;
755 u64_stats_update_end(&rxsc_stats->syncp);
758 if (!macsec_skb_cb(skb)->valid) {
759 spin_unlock(&rx_sa->lock);
762 if (hdr->tci_an & MACSEC_TCI_C ||
763 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
764 u64_stats_update_begin(&rxsc_stats->syncp);
765 rxsc_stats->stats.InPktsNotValid++;
766 u64_stats_update_end(&rxsc_stats->syncp);
770 u64_stats_update_begin(&rxsc_stats->syncp);
771 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
772 rxsc_stats->stats.InPktsInvalid++;
773 this_cpu_inc(rx_sa->stats->InPktsInvalid);
774 } else if (pn < lowest_pn) {
775 rxsc_stats->stats.InPktsDelayed++;
777 rxsc_stats->stats.InPktsUnchecked++;
779 u64_stats_update_end(&rxsc_stats->syncp);
781 u64_stats_update_begin(&rxsc_stats->syncp);
782 if (pn < lowest_pn) {
783 rxsc_stats->stats.InPktsDelayed++;
785 rxsc_stats->stats.InPktsOK++;
786 this_cpu_inc(rx_sa->stats->InPktsOK);
788 u64_stats_update_end(&rxsc_stats->syncp);
790 // Instead of "pn >=" - to support pn overflow in xpn
791 if (pn + 1 > rx_sa->next_pn_halves.lower) {
792 rx_sa->next_pn_halves.lower = pn + 1;
793 } else if (secy->xpn &&
794 !pn_same_half(pn, rx_sa->next_pn_halves.lower)) {
795 rx_sa->next_pn_halves.upper++;
796 rx_sa->next_pn_halves.lower = pn + 1;
799 spin_unlock(&rx_sa->lock);
805 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
807 skb->pkt_type = PACKET_HOST;
808 skb->protocol = eth_type_trans(skb, dev);
810 skb_reset_network_header(skb);
811 if (!skb_transport_header_was_set(skb))
812 skb_reset_transport_header(skb);
813 skb_reset_mac_len(skb);
816 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
818 skb->ip_summed = CHECKSUM_NONE;
819 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
820 skb_pull(skb, hdr_len);
821 pskb_trim_unique(skb, skb->len - icv_len);
824 static void count_rx(struct net_device *dev, int len)
826 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
828 u64_stats_update_begin(&stats->syncp);
830 stats->rx_bytes += len;
831 u64_stats_update_end(&stats->syncp);
834 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
836 struct sk_buff *skb = base->data;
837 struct net_device *dev = skb->dev;
838 struct macsec_dev *macsec = macsec_priv(dev);
839 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
840 struct macsec_rx_sc *rx_sc = rx_sa->sc;
844 aead_request_free(macsec_skb_cb(skb)->req);
847 macsec_skb_cb(skb)->valid = true;
850 pn = ntohl(macsec_ethhdr(skb)->packet_number);
851 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
852 rcu_read_unlock_bh();
857 macsec_finalize_skb(skb, macsec->secy.icv_len,
858 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
859 macsec_reset_skb(skb, macsec->secy.netdev);
862 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
865 rcu_read_unlock_bh();
868 macsec_rxsa_put(rx_sa);
869 macsec_rxsc_put(rx_sc);
873 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
874 struct net_device *dev,
875 struct macsec_rx_sa *rx_sa,
877 struct macsec_secy *secy)
880 struct scatterlist *sg;
881 struct sk_buff *trailer;
883 struct aead_request *req;
884 struct macsec_eth_header *hdr;
886 u16 icv_len = secy->icv_len;
888 macsec_skb_cb(skb)->valid = false;
889 skb = skb_share_check(skb, GFP_ATOMIC);
891 return ERR_PTR(-ENOMEM);
893 ret = skb_cow_data(skb, 0, &trailer);
894 if (unlikely(ret < 0)) {
898 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
901 return ERR_PTR(-ENOMEM);
904 hdr = (struct macsec_eth_header *)skb->data;
905 hdr_pn = ntohl(hdr->packet_number);
908 pn_t recovered_pn = rx_sa->next_pn_halves;
910 recovered_pn.lower = hdr_pn;
911 if (hdr_pn < rx_sa->next_pn_halves.lower &&
912 !pn_same_half(hdr_pn, rx_sa->next_pn_halves.lower))
913 recovered_pn.upper++;
915 macsec_fill_iv_xpn(iv, rx_sa->ssci, recovered_pn.full64,
918 macsec_fill_iv(iv, sci, hdr_pn);
921 sg_init_table(sg, ret);
922 ret = skb_to_sgvec(skb, sg, 0, skb->len);
923 if (unlikely(ret < 0)) {
924 aead_request_free(req);
929 if (hdr->tci_an & MACSEC_TCI_E) {
930 /* confidentiality: ethernet + macsec header
931 * authenticated, encrypted payload
933 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
935 aead_request_set_crypt(req, sg, sg, len, iv);
936 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
937 skb = skb_unshare(skb, GFP_ATOMIC);
939 aead_request_free(req);
940 return ERR_PTR(-ENOMEM);
943 /* integrity only: all headers + data authenticated */
944 aead_request_set_crypt(req, sg, sg, icv_len, iv);
945 aead_request_set_ad(req, skb->len - icv_len);
948 macsec_skb_cb(skb)->req = req;
950 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
953 ret = crypto_aead_decrypt(req);
954 if (ret == -EINPROGRESS) {
956 } else if (ret != 0) {
957 /* decryption/authentication failed
958 * 10.6 if validateFrames is disabled, deliver anyway
960 if (ret != -EBADMSG) {
965 macsec_skb_cb(skb)->valid = true;
969 aead_request_free(req);
974 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
976 struct macsec_rx_sc *rx_sc;
978 for_each_rxsc(secy, rx_sc) {
979 if (rx_sc->sci == sci)
986 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
988 struct macsec_rx_sc *rx_sc;
990 for_each_rxsc_rtnl(secy, rx_sc) {
991 if (rx_sc->sci == sci)
998 static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
1000 /* Deliver to the uncontrolled port by default */
1001 enum rx_handler_result ret = RX_HANDLER_PASS;
1002 struct ethhdr *hdr = eth_hdr(skb);
1003 struct macsec_rxh_data *rxd;
1004 struct macsec_dev *macsec;
1007 rxd = macsec_data_rcu(skb->dev);
1009 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1010 struct sk_buff *nskb;
1011 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1012 struct net_device *ndev = macsec->secy.netdev;
1014 /* If h/w offloading is enabled, HW decodes frames and strips
1015 * the SecTAG, so we have to deduce which port to deliver to.
1017 if (macsec_is_offloaded(macsec) && netif_running(ndev)) {
1018 if (ether_addr_equal_64bits(hdr->h_dest,
1020 /* exact match, divert skb to this port */
1022 skb->pkt_type = PACKET_HOST;
1023 ret = RX_HANDLER_ANOTHER;
1025 } else if (is_multicast_ether_addr_64bits(
1027 /* multicast frame, deliver on this port too */
1028 nskb = skb_clone(skb, GFP_ATOMIC);
1033 if (ether_addr_equal_64bits(hdr->h_dest,
1035 nskb->pkt_type = PACKET_BROADCAST;
1037 nskb->pkt_type = PACKET_MULTICAST;
1044 /* 10.6 If the management control validateFrames is not
1045 * Strict, frames without a SecTAG are received, counted, and
1046 * delivered to the Controlled Port
1048 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1049 u64_stats_update_begin(&secy_stats->syncp);
1050 secy_stats->stats.InPktsNoTag++;
1051 u64_stats_update_end(&secy_stats->syncp);
1055 /* deliver on this port */
1056 nskb = skb_clone(skb, GFP_ATOMIC);
1062 if (__netif_rx(nskb) == NET_RX_SUCCESS) {
1063 u64_stats_update_begin(&secy_stats->syncp);
1064 secy_stats->stats.InPktsUntagged++;
1065 u64_stats_update_end(&secy_stats->syncp);
1074 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1076 struct sk_buff *skb = *pskb;
1077 struct net_device *dev = skb->dev;
1078 struct macsec_eth_header *hdr;
1079 struct macsec_secy *secy = NULL;
1080 struct macsec_rx_sc *rx_sc;
1081 struct macsec_rx_sa *rx_sa;
1082 struct macsec_rxh_data *rxd;
1083 struct macsec_dev *macsec;
1088 struct pcpu_rx_sc_stats *rxsc_stats;
1089 struct pcpu_secy_stats *secy_stats;
1093 if (skb_headroom(skb) < ETH_HLEN)
1096 hdr = macsec_ethhdr(skb);
1097 if (hdr->eth.h_proto != htons(ETH_P_MACSEC))
1098 return handle_not_macsec(skb);
1100 skb = skb_unshare(skb, GFP_ATOMIC);
1103 return RX_HANDLER_CONSUMED;
1105 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1107 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1111 hdr = macsec_ethhdr(skb);
1113 /* Frames with a SecTAG that has the TCI E bit set but the C
1114 * bit clear are discarded, as this reserved encoding is used
1115 * to identify frames with a SecTAG that are not to be
1116 * delivered to the Controlled Port.
1118 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1119 return RX_HANDLER_PASS;
1121 /* now, pull the extra length */
1122 if (hdr->tci_an & MACSEC_TCI_SC) {
1127 /* ethernet header is part of crypto processing */
1128 skb_push(skb, ETH_HLEN);
1130 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1131 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1132 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1135 rxd = macsec_data_rcu(skb->dev);
1137 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1138 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1140 sc = sc ? macsec_rxsc_get(sc) : NULL;
1143 secy = &macsec->secy;
1153 macsec = macsec_priv(dev);
1154 secy_stats = this_cpu_ptr(macsec->stats);
1155 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1157 if (!macsec_validate_skb(skb, secy->icv_len, secy->xpn)) {
1158 u64_stats_update_begin(&secy_stats->syncp);
1159 secy_stats->stats.InPktsBadTag++;
1160 u64_stats_update_end(&secy_stats->syncp);
1164 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1166 /* 10.6.1 if the SA is not in use */
1168 /* If validateFrames is Strict or the C bit in the
1169 * SecTAG is set, discard
1171 if (hdr->tci_an & MACSEC_TCI_C ||
1172 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1173 u64_stats_update_begin(&rxsc_stats->syncp);
1174 rxsc_stats->stats.InPktsNotUsingSA++;
1175 u64_stats_update_end(&rxsc_stats->syncp);
1179 /* not Strict, the frame (with the SecTAG and ICV
1180 * removed) is delivered to the Controlled Port.
1182 u64_stats_update_begin(&rxsc_stats->syncp);
1183 rxsc_stats->stats.InPktsUnusedSA++;
1184 u64_stats_update_end(&rxsc_stats->syncp);
1188 /* First, PN check to avoid decrypting obviously wrong packets */
1189 hdr_pn = ntohl(hdr->packet_number);
1190 if (secy->replay_protect) {
1193 spin_lock(&rx_sa->lock);
1194 late = rx_sa->next_pn_halves.lower >= secy->replay_window &&
1195 hdr_pn < (rx_sa->next_pn_halves.lower - secy->replay_window);
1198 late = late && pn_same_half(rx_sa->next_pn_halves.lower, hdr_pn);
1199 spin_unlock(&rx_sa->lock);
1202 u64_stats_update_begin(&rxsc_stats->syncp);
1203 rxsc_stats->stats.InPktsLate++;
1204 u64_stats_update_end(&rxsc_stats->syncp);
1209 macsec_skb_cb(skb)->rx_sa = rx_sa;
1211 /* Disabled && !changed text => skip validation */
1212 if (hdr->tci_an & MACSEC_TCI_C ||
1213 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1214 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1217 /* the decrypt callback needs the reference */
1218 if (PTR_ERR(skb) != -EINPROGRESS) {
1219 macsec_rxsa_put(rx_sa);
1220 macsec_rxsc_put(rx_sc);
1224 return RX_HANDLER_CONSUMED;
1227 if (!macsec_post_decrypt(skb, secy, hdr_pn))
1231 macsec_finalize_skb(skb, secy->icv_len,
1232 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1233 macsec_reset_skb(skb, secy->netdev);
1236 macsec_rxsa_put(rx_sa);
1237 macsec_rxsc_put(rx_sc);
1241 ret = gro_cells_receive(&macsec->gro_cells, skb);
1242 if (ret == NET_RX_SUCCESS)
1245 macsec->secy.netdev->stats.rx_dropped++;
1250 return RX_HANDLER_CONSUMED;
1253 macsec_rxsa_put(rx_sa);
1255 macsec_rxsc_put(rx_sc);
1260 return RX_HANDLER_CONSUMED;
1263 /* 10.6.1 if the SC is not found */
1264 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1266 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1267 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1269 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1270 struct sk_buff *nskb;
1272 secy_stats = this_cpu_ptr(macsec->stats);
1274 /* If validateFrames is Strict or the C bit in the
1275 * SecTAG is set, discard
1278 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1279 u64_stats_update_begin(&secy_stats->syncp);
1280 secy_stats->stats.InPktsNoSCI++;
1281 u64_stats_update_end(&secy_stats->syncp);
1285 /* not strict, the frame (with the SecTAG and ICV
1286 * removed) is delivered to the Controlled Port.
1288 nskb = skb_clone(skb, GFP_ATOMIC);
1292 macsec_reset_skb(nskb, macsec->secy.netdev);
1294 ret = __netif_rx(nskb);
1295 if (ret == NET_RX_SUCCESS) {
1296 u64_stats_update_begin(&secy_stats->syncp);
1297 secy_stats->stats.InPktsUnknownSCI++;
1298 u64_stats_update_end(&secy_stats->syncp);
1300 macsec->secy.netdev->stats.rx_dropped++;
1306 return RX_HANDLER_PASS;
1309 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1311 struct crypto_aead *tfm;
1314 /* Pick a sync gcm(aes) cipher to ensure order is preserved. */
1315 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
1320 ret = crypto_aead_setkey(tfm, key, key_len);
1324 ret = crypto_aead_setauthsize(tfm, icv_len);
1330 crypto_free_aead(tfm);
1331 return ERR_PTR(ret);
1334 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1337 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1341 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1342 if (IS_ERR(rx_sa->key.tfm)) {
1343 free_percpu(rx_sa->stats);
1344 return PTR_ERR(rx_sa->key.tfm);
1347 rx_sa->ssci = MACSEC_UNDEF_SSCI;
1348 rx_sa->active = false;
1350 refcount_set(&rx_sa->refcnt, 1);
1351 spin_lock_init(&rx_sa->lock);
1356 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1358 rx_sa->active = false;
1360 macsec_rxsa_put(rx_sa);
1363 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1367 for (i = 0; i < MACSEC_NUM_AN; i++) {
1368 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1370 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1375 macsec_rxsc_put(rx_sc);
1378 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1380 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1382 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1384 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1385 if (rx_sc->sci == sci) {
1388 rcu_assign_pointer(*rx_scp, rx_sc->next);
1396 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1398 struct macsec_rx_sc *rx_sc;
1399 struct macsec_dev *macsec;
1400 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1401 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1402 struct macsec_secy *secy;
1404 list_for_each_entry(macsec, &rxd->secys, secys) {
1405 if (find_rx_sc_rtnl(&macsec->secy, sci))
1406 return ERR_PTR(-EEXIST);
1409 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1411 return ERR_PTR(-ENOMEM);
1413 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1414 if (!rx_sc->stats) {
1416 return ERR_PTR(-ENOMEM);
1420 rx_sc->active = true;
1421 refcount_set(&rx_sc->refcnt, 1);
1423 secy = &macsec_priv(dev)->secy;
1424 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1425 rcu_assign_pointer(secy->rx_sc, rx_sc);
1433 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1436 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1440 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1441 if (IS_ERR(tx_sa->key.tfm)) {
1442 free_percpu(tx_sa->stats);
1443 return PTR_ERR(tx_sa->key.tfm);
1446 tx_sa->ssci = MACSEC_UNDEF_SSCI;
1447 tx_sa->active = false;
1448 refcount_set(&tx_sa->refcnt, 1);
1449 spin_lock_init(&tx_sa->lock);
1454 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1456 tx_sa->active = false;
1458 macsec_txsa_put(tx_sa);
1461 static struct genl_family macsec_fam;
1463 static struct net_device *get_dev_from_nl(struct net *net,
1464 struct nlattr **attrs)
1466 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1467 struct net_device *dev;
1469 dev = __dev_get_by_index(net, ifindex);
1471 return ERR_PTR(-ENODEV);
1473 if (!netif_is_macsec(dev))
1474 return ERR_PTR(-ENODEV);
1479 static enum macsec_offload nla_get_offload(const struct nlattr *nla)
1481 return (__force enum macsec_offload)nla_get_u8(nla);
1484 static sci_t nla_get_sci(const struct nlattr *nla)
1486 return (__force sci_t)nla_get_u64(nla);
1489 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1492 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1495 static ssci_t nla_get_ssci(const struct nlattr *nla)
1497 return (__force ssci_t)nla_get_u32(nla);
1500 static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value)
1502 return nla_put_u32(skb, attrtype, (__force u64)value);
1505 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1506 struct nlattr **attrs,
1507 struct nlattr **tb_sa,
1508 struct net_device **devp,
1509 struct macsec_secy **secyp,
1510 struct macsec_tx_sc **scp,
1513 struct net_device *dev;
1514 struct macsec_secy *secy;
1515 struct macsec_tx_sc *tx_sc;
1516 struct macsec_tx_sa *tx_sa;
1518 if (!tb_sa[MACSEC_SA_ATTR_AN])
1519 return ERR_PTR(-EINVAL);
1521 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1523 dev = get_dev_from_nl(net, attrs);
1525 return ERR_CAST(dev);
1527 if (*assoc_num >= MACSEC_NUM_AN)
1528 return ERR_PTR(-EINVAL);
1530 secy = &macsec_priv(dev)->secy;
1531 tx_sc = &secy->tx_sc;
1533 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1535 return ERR_PTR(-ENODEV);
1543 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1544 struct nlattr **attrs,
1545 struct nlattr **tb_rxsc,
1546 struct net_device **devp,
1547 struct macsec_secy **secyp)
1549 struct net_device *dev;
1550 struct macsec_secy *secy;
1551 struct macsec_rx_sc *rx_sc;
1554 dev = get_dev_from_nl(net, attrs);
1556 return ERR_CAST(dev);
1558 secy = &macsec_priv(dev)->secy;
1560 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1561 return ERR_PTR(-EINVAL);
1563 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1564 rx_sc = find_rx_sc_rtnl(secy, sci);
1566 return ERR_PTR(-ENODEV);
1574 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1575 struct nlattr **attrs,
1576 struct nlattr **tb_rxsc,
1577 struct nlattr **tb_sa,
1578 struct net_device **devp,
1579 struct macsec_secy **secyp,
1580 struct macsec_rx_sc **scp,
1583 struct macsec_rx_sc *rx_sc;
1584 struct macsec_rx_sa *rx_sa;
1586 if (!tb_sa[MACSEC_SA_ATTR_AN])
1587 return ERR_PTR(-EINVAL);
1589 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1590 if (*assoc_num >= MACSEC_NUM_AN)
1591 return ERR_PTR(-EINVAL);
1593 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1595 return ERR_CAST(rx_sc);
1597 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1599 return ERR_PTR(-ENODEV);
1605 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1606 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1607 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1608 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1609 [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
1612 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1613 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1614 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1617 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1618 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1619 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1620 [MACSEC_SA_ATTR_PN] = NLA_POLICY_MIN_LEN(4),
1621 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1622 .len = MACSEC_KEYID_LEN, },
1623 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1624 .len = MACSEC_MAX_KEY_LEN, },
1625 [MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 },
1626 [MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY,
1627 .len = MACSEC_SALT_LEN, },
1630 static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
1631 [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 },
1634 /* Offloads an operation to a device driver */
1635 static int macsec_offload(int (* const func)(struct macsec_context *),
1636 struct macsec_context *ctx)
1640 if (unlikely(!func))
1643 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1644 mutex_lock(&ctx->phydev->lock);
1646 /* Phase I: prepare. The drive should fail here if there are going to be
1647 * issues in the commit phase.
1649 ctx->prepare = true;
1654 /* Phase II: commit. This step cannot fail. */
1655 ctx->prepare = false;
1657 /* This should never happen: commit is not allowed to fail */
1659 WARN(1, "MACsec offloading commit failed (%d)\n", ret);
1662 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1663 mutex_unlock(&ctx->phydev->lock);
1668 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1670 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1673 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1679 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1681 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1684 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1690 static bool validate_add_rxsa(struct nlattr **attrs)
1692 if (!attrs[MACSEC_SA_ATTR_AN] ||
1693 !attrs[MACSEC_SA_ATTR_KEY] ||
1694 !attrs[MACSEC_SA_ATTR_KEYID])
1697 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1700 if (attrs[MACSEC_SA_ATTR_PN] &&
1701 nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
1704 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1705 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1709 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1715 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1717 struct net_device *dev;
1718 struct nlattr **attrs = info->attrs;
1719 struct macsec_secy *secy;
1720 struct macsec_rx_sc *rx_sc;
1721 struct macsec_rx_sa *rx_sa;
1722 unsigned char assoc_num;
1724 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1725 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1728 if (!attrs[MACSEC_ATTR_IFINDEX])
1731 if (parse_sa_config(attrs, tb_sa))
1734 if (parse_rxsc_config(attrs, tb_rxsc))
1737 if (!validate_add_rxsa(tb_sa))
1741 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1742 if (IS_ERR(rx_sc)) {
1744 return PTR_ERR(rx_sc);
1747 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1749 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1750 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1751 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1756 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1757 if (tb_sa[MACSEC_SA_ATTR_PN] &&
1758 nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1759 pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
1760 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1766 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1771 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
1772 pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n",
1773 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
1780 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1786 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1792 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1793 secy->key_len, secy->icv_len);
1800 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1801 spin_lock_bh(&rx_sa->lock);
1802 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
1803 spin_unlock_bh(&rx_sa->lock);
1806 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1807 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1811 /* If h/w offloading is available, propagate to the device */
1812 if (macsec_is_offloaded(netdev_priv(dev))) {
1813 const struct macsec_ops *ops;
1814 struct macsec_context ctx;
1816 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1822 ctx.sa.assoc_num = assoc_num;
1823 ctx.sa.rx_sa = rx_sa;
1825 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1828 err = macsec_offload(ops->mdo_add_rxsa, &ctx);
1834 rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
1835 nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
1839 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1840 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1847 macsec_rxsa_put(rx_sa);
1852 static bool validate_add_rxsc(struct nlattr **attrs)
1854 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1857 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1858 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1865 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1867 struct net_device *dev;
1868 sci_t sci = MACSEC_UNDEF_SCI;
1869 struct nlattr **attrs = info->attrs;
1870 struct macsec_rx_sc *rx_sc;
1871 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1872 struct macsec_secy *secy;
1876 if (!attrs[MACSEC_ATTR_IFINDEX])
1879 if (parse_rxsc_config(attrs, tb_rxsc))
1882 if (!validate_add_rxsc(tb_rxsc))
1886 dev = get_dev_from_nl(genl_info_net(info), attrs);
1889 return PTR_ERR(dev);
1892 secy = &macsec_priv(dev)->secy;
1893 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1895 rx_sc = create_rx_sc(dev, sci);
1896 if (IS_ERR(rx_sc)) {
1898 return PTR_ERR(rx_sc);
1901 was_active = rx_sc->active;
1902 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1903 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1905 if (macsec_is_offloaded(netdev_priv(dev))) {
1906 const struct macsec_ops *ops;
1907 struct macsec_context ctx;
1909 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1918 ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
1928 rx_sc->active = was_active;
1933 static bool validate_add_txsa(struct nlattr **attrs)
1935 if (!attrs[MACSEC_SA_ATTR_AN] ||
1936 !attrs[MACSEC_SA_ATTR_PN] ||
1937 !attrs[MACSEC_SA_ATTR_KEY] ||
1938 !attrs[MACSEC_SA_ATTR_KEYID])
1941 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1944 if (nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
1947 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1948 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1952 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1958 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1960 struct net_device *dev;
1961 struct nlattr **attrs = info->attrs;
1962 struct macsec_secy *secy;
1963 struct macsec_tx_sc *tx_sc;
1964 struct macsec_tx_sa *tx_sa;
1965 unsigned char assoc_num;
1967 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1968 bool was_operational;
1971 if (!attrs[MACSEC_ATTR_IFINDEX])
1974 if (parse_sa_config(attrs, tb_sa))
1977 if (!validate_add_txsa(tb_sa))
1981 dev = get_dev_from_nl(genl_info_net(info), attrs);
1984 return PTR_ERR(dev);
1987 secy = &macsec_priv(dev)->secy;
1988 tx_sc = &secy->tx_sc;
1990 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1992 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1993 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1994 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1999 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2000 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2001 pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
2002 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2008 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
2013 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
2014 pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n",
2015 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
2022 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
2028 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
2034 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2035 secy->key_len, secy->icv_len);
2042 spin_lock_bh(&tx_sa->lock);
2043 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2044 spin_unlock_bh(&tx_sa->lock);
2046 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2047 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2049 was_operational = secy->operational;
2050 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
2051 secy->operational = true;
2053 /* If h/w offloading is available, propagate to the device */
2054 if (macsec_is_offloaded(netdev_priv(dev))) {
2055 const struct macsec_ops *ops;
2056 struct macsec_context ctx;
2058 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2064 ctx.sa.assoc_num = assoc_num;
2065 ctx.sa.tx_sa = tx_sa;
2067 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2070 err = macsec_offload(ops->mdo_add_txsa, &ctx);
2076 tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
2077 nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
2081 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
2082 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
2089 secy->operational = was_operational;
2090 macsec_txsa_put(tx_sa);
2095 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
2097 struct nlattr **attrs = info->attrs;
2098 struct net_device *dev;
2099 struct macsec_secy *secy;
2100 struct macsec_rx_sc *rx_sc;
2101 struct macsec_rx_sa *rx_sa;
2103 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2104 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2107 if (!attrs[MACSEC_ATTR_IFINDEX])
2110 if (parse_sa_config(attrs, tb_sa))
2113 if (parse_rxsc_config(attrs, tb_rxsc))
2117 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2118 &dev, &secy, &rx_sc, &assoc_num);
2119 if (IS_ERR(rx_sa)) {
2121 return PTR_ERR(rx_sa);
2124 if (rx_sa->active) {
2129 /* If h/w offloading is available, propagate to the device */
2130 if (macsec_is_offloaded(netdev_priv(dev))) {
2131 const struct macsec_ops *ops;
2132 struct macsec_context ctx;
2134 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2140 ctx.sa.assoc_num = assoc_num;
2141 ctx.sa.rx_sa = rx_sa;
2144 ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
2149 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
2161 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
2163 struct nlattr **attrs = info->attrs;
2164 struct net_device *dev;
2165 struct macsec_secy *secy;
2166 struct macsec_rx_sc *rx_sc;
2168 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2171 if (!attrs[MACSEC_ATTR_IFINDEX])
2174 if (parse_rxsc_config(attrs, tb_rxsc))
2177 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2181 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
2184 return PTR_ERR(dev);
2187 secy = &macsec_priv(dev)->secy;
2188 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2190 rx_sc = del_rx_sc(secy, sci);
2196 /* If h/w offloading is available, propagate to the device */
2197 if (macsec_is_offloaded(netdev_priv(dev))) {
2198 const struct macsec_ops *ops;
2199 struct macsec_context ctx;
2201 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2209 ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
2224 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2226 struct nlattr **attrs = info->attrs;
2227 struct net_device *dev;
2228 struct macsec_secy *secy;
2229 struct macsec_tx_sc *tx_sc;
2230 struct macsec_tx_sa *tx_sa;
2232 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2235 if (!attrs[MACSEC_ATTR_IFINDEX])
2238 if (parse_sa_config(attrs, tb_sa))
2242 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2243 &dev, &secy, &tx_sc, &assoc_num);
2244 if (IS_ERR(tx_sa)) {
2246 return PTR_ERR(tx_sa);
2249 if (tx_sa->active) {
2254 /* If h/w offloading is available, propagate to the device */
2255 if (macsec_is_offloaded(netdev_priv(dev))) {
2256 const struct macsec_ops *ops;
2257 struct macsec_context ctx;
2259 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2265 ctx.sa.assoc_num = assoc_num;
2266 ctx.sa.tx_sa = tx_sa;
2269 ret = macsec_offload(ops->mdo_del_txsa, &ctx);
2274 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2286 static bool validate_upd_sa(struct nlattr **attrs)
2288 if (!attrs[MACSEC_SA_ATTR_AN] ||
2289 attrs[MACSEC_SA_ATTR_KEY] ||
2290 attrs[MACSEC_SA_ATTR_KEYID] ||
2291 attrs[MACSEC_SA_ATTR_SSCI] ||
2292 attrs[MACSEC_SA_ATTR_SALT])
2295 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2298 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
2301 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2302 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2309 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2311 struct nlattr **attrs = info->attrs;
2312 struct net_device *dev;
2313 struct macsec_secy *secy;
2314 struct macsec_tx_sc *tx_sc;
2315 struct macsec_tx_sa *tx_sa;
2317 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2318 bool was_operational, was_active;
2324 if (!attrs[MACSEC_ATTR_IFINDEX])
2327 if (parse_sa_config(attrs, tb_sa))
2330 if (!validate_upd_sa(tb_sa))
2334 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2335 &dev, &secy, &tx_sc, &assoc_num);
2336 if (IS_ERR(tx_sa)) {
2338 return PTR_ERR(tx_sa);
2341 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2344 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2345 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2346 pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
2347 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2352 spin_lock_bh(&tx_sa->lock);
2353 prev_pn = tx_sa->next_pn_halves;
2354 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2355 spin_unlock_bh(&tx_sa->lock);
2358 was_active = tx_sa->active;
2359 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2360 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2362 was_operational = secy->operational;
2363 if (assoc_num == tx_sc->encoding_sa)
2364 secy->operational = tx_sa->active;
2366 /* If h/w offloading is available, propagate to the device */
2367 if (macsec_is_offloaded(netdev_priv(dev))) {
2368 const struct macsec_ops *ops;
2369 struct macsec_context ctx;
2371 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2377 ctx.sa.assoc_num = assoc_num;
2378 ctx.sa.tx_sa = tx_sa;
2381 ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
2391 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2392 spin_lock_bh(&tx_sa->lock);
2393 tx_sa->next_pn_halves = prev_pn;
2394 spin_unlock_bh(&tx_sa->lock);
2396 tx_sa->active = was_active;
2397 secy->operational = was_operational;
2402 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2404 struct nlattr **attrs = info->attrs;
2405 struct net_device *dev;
2406 struct macsec_secy *secy;
2407 struct macsec_rx_sc *rx_sc;
2408 struct macsec_rx_sa *rx_sa;
2410 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2411 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2418 if (!attrs[MACSEC_ATTR_IFINDEX])
2421 if (parse_rxsc_config(attrs, tb_rxsc))
2424 if (parse_sa_config(attrs, tb_sa))
2427 if (!validate_upd_sa(tb_sa))
2431 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2432 &dev, &secy, &rx_sc, &assoc_num);
2433 if (IS_ERR(rx_sa)) {
2435 return PTR_ERR(rx_sa);
2438 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2441 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2442 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2443 pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
2444 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2449 spin_lock_bh(&rx_sa->lock);
2450 prev_pn = rx_sa->next_pn_halves;
2451 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2452 spin_unlock_bh(&rx_sa->lock);
2455 was_active = rx_sa->active;
2456 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2457 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2459 /* If h/w offloading is available, propagate to the device */
2460 if (macsec_is_offloaded(netdev_priv(dev))) {
2461 const struct macsec_ops *ops;
2462 struct macsec_context ctx;
2464 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2470 ctx.sa.assoc_num = assoc_num;
2471 ctx.sa.rx_sa = rx_sa;
2474 ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
2483 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2484 spin_lock_bh(&rx_sa->lock);
2485 rx_sa->next_pn_halves = prev_pn;
2486 spin_unlock_bh(&rx_sa->lock);
2488 rx_sa->active = was_active;
2493 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2495 struct nlattr **attrs = info->attrs;
2496 struct net_device *dev;
2497 struct macsec_secy *secy;
2498 struct macsec_rx_sc *rx_sc;
2499 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2500 unsigned int prev_n_rx_sc;
2504 if (!attrs[MACSEC_ATTR_IFINDEX])
2507 if (parse_rxsc_config(attrs, tb_rxsc))
2510 if (!validate_add_rxsc(tb_rxsc))
2514 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2515 if (IS_ERR(rx_sc)) {
2517 return PTR_ERR(rx_sc);
2520 was_active = rx_sc->active;
2521 prev_n_rx_sc = secy->n_rx_sc;
2522 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2523 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2525 if (rx_sc->active != new)
2526 secy->n_rx_sc += new ? 1 : -1;
2528 rx_sc->active = new;
2531 /* If h/w offloading is available, propagate to the device */
2532 if (macsec_is_offloaded(netdev_priv(dev))) {
2533 const struct macsec_ops *ops;
2534 struct macsec_context ctx;
2536 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2545 ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
2555 secy->n_rx_sc = prev_n_rx_sc;
2556 rx_sc->active = was_active;
2561 static bool macsec_is_configured(struct macsec_dev *macsec)
2563 struct macsec_secy *secy = &macsec->secy;
2564 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2567 if (secy->n_rx_sc > 0)
2570 for (i = 0; i < MACSEC_NUM_AN; i++)
2577 static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
2579 struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
2580 enum macsec_offload offload, prev_offload;
2581 int (*func)(struct macsec_context *ctx);
2582 struct nlattr **attrs = info->attrs;
2583 struct net_device *dev;
2584 const struct macsec_ops *ops;
2585 struct macsec_context ctx;
2586 struct macsec_dev *macsec;
2589 if (!attrs[MACSEC_ATTR_IFINDEX])
2592 if (!attrs[MACSEC_ATTR_OFFLOAD])
2595 if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
2596 attrs[MACSEC_ATTR_OFFLOAD],
2597 macsec_genl_offload_policy, NULL))
2600 dev = get_dev_from_nl(genl_info_net(info), attrs);
2602 return PTR_ERR(dev);
2603 macsec = macsec_priv(dev);
2605 if (!tb_offload[MACSEC_OFFLOAD_ATTR_TYPE])
2608 offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
2609 if (macsec->offload == offload)
2612 /* Check if the offloading mode is supported by the underlying layers */
2613 if (offload != MACSEC_OFFLOAD_OFF &&
2614 !macsec_check_offload(offload, macsec))
2617 /* Check if the net device is busy. */
2618 if (netif_running(dev))
2623 prev_offload = macsec->offload;
2624 macsec->offload = offload;
2626 /* Check if the device already has rules configured: we do not support
2629 if (macsec_is_configured(macsec)) {
2634 ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
2641 if (prev_offload == MACSEC_OFFLOAD_OFF)
2642 func = ops->mdo_add_secy;
2644 func = ops->mdo_del_secy;
2646 ctx.secy = &macsec->secy;
2647 ret = macsec_offload(func, &ctx);
2651 /* Force features update, since they are different for SW MACSec and
2652 * HW offloading cases.
2654 netdev_update_features(dev);
2660 macsec->offload = prev_offload;
2666 static void get_tx_sa_stats(struct net_device *dev, int an,
2667 struct macsec_tx_sa *tx_sa,
2668 struct macsec_tx_sa_stats *sum)
2670 struct macsec_dev *macsec = macsec_priv(dev);
2673 /* If h/w offloading is available, propagate to the device */
2674 if (macsec_is_offloaded(macsec)) {
2675 const struct macsec_ops *ops;
2676 struct macsec_context ctx;
2678 ops = macsec_get_ops(macsec, &ctx);
2680 ctx.sa.assoc_num = an;
2681 ctx.sa.tx_sa = tx_sa;
2682 ctx.stats.tx_sa_stats = sum;
2683 ctx.secy = &macsec_priv(dev)->secy;
2684 macsec_offload(ops->mdo_get_tx_sa_stats, &ctx);
2689 for_each_possible_cpu(cpu) {
2690 const struct macsec_tx_sa_stats *stats =
2691 per_cpu_ptr(tx_sa->stats, cpu);
2693 sum->OutPktsProtected += stats->OutPktsProtected;
2694 sum->OutPktsEncrypted += stats->OutPktsEncrypted;
2698 static int copy_tx_sa_stats(struct sk_buff *skb, struct macsec_tx_sa_stats *sum)
2700 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED,
2701 sum->OutPktsProtected) ||
2702 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2703 sum->OutPktsEncrypted))
2709 static void get_rx_sa_stats(struct net_device *dev,
2710 struct macsec_rx_sc *rx_sc, int an,
2711 struct macsec_rx_sa *rx_sa,
2712 struct macsec_rx_sa_stats *sum)
2714 struct macsec_dev *macsec = macsec_priv(dev);
2717 /* If h/w offloading is available, propagate to the device */
2718 if (macsec_is_offloaded(macsec)) {
2719 const struct macsec_ops *ops;
2720 struct macsec_context ctx;
2722 ops = macsec_get_ops(macsec, &ctx);
2724 ctx.sa.assoc_num = an;
2725 ctx.sa.rx_sa = rx_sa;
2726 ctx.stats.rx_sa_stats = sum;
2727 ctx.secy = &macsec_priv(dev)->secy;
2729 macsec_offload(ops->mdo_get_rx_sa_stats, &ctx);
2734 for_each_possible_cpu(cpu) {
2735 const struct macsec_rx_sa_stats *stats =
2736 per_cpu_ptr(rx_sa->stats, cpu);
2738 sum->InPktsOK += stats->InPktsOK;
2739 sum->InPktsInvalid += stats->InPktsInvalid;
2740 sum->InPktsNotValid += stats->InPktsNotValid;
2741 sum->InPktsNotUsingSA += stats->InPktsNotUsingSA;
2742 sum->InPktsUnusedSA += stats->InPktsUnusedSA;
2746 static int copy_rx_sa_stats(struct sk_buff *skb,
2747 struct macsec_rx_sa_stats *sum)
2749 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum->InPktsOK) ||
2750 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID,
2751 sum->InPktsInvalid) ||
2752 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID,
2753 sum->InPktsNotValid) ||
2754 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2755 sum->InPktsNotUsingSA) ||
2756 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA,
2757 sum->InPktsUnusedSA))
2763 static void get_rx_sc_stats(struct net_device *dev,
2764 struct macsec_rx_sc *rx_sc,
2765 struct macsec_rx_sc_stats *sum)
2767 struct macsec_dev *macsec = macsec_priv(dev);
2770 /* If h/w offloading is available, propagate to the device */
2771 if (macsec_is_offloaded(macsec)) {
2772 const struct macsec_ops *ops;
2773 struct macsec_context ctx;
2775 ops = macsec_get_ops(macsec, &ctx);
2777 ctx.stats.rx_sc_stats = sum;
2778 ctx.secy = &macsec_priv(dev)->secy;
2780 macsec_offload(ops->mdo_get_rx_sc_stats, &ctx);
2785 for_each_possible_cpu(cpu) {
2786 const struct pcpu_rx_sc_stats *stats;
2787 struct macsec_rx_sc_stats tmp;
2790 stats = per_cpu_ptr(rx_sc->stats, cpu);
2792 start = u64_stats_fetch_begin_irq(&stats->syncp);
2793 memcpy(&tmp, &stats->stats, sizeof(tmp));
2794 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2796 sum->InOctetsValidated += tmp.InOctetsValidated;
2797 sum->InOctetsDecrypted += tmp.InOctetsDecrypted;
2798 sum->InPktsUnchecked += tmp.InPktsUnchecked;
2799 sum->InPktsDelayed += tmp.InPktsDelayed;
2800 sum->InPktsOK += tmp.InPktsOK;
2801 sum->InPktsInvalid += tmp.InPktsInvalid;
2802 sum->InPktsLate += tmp.InPktsLate;
2803 sum->InPktsNotValid += tmp.InPktsNotValid;
2804 sum->InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2805 sum->InPktsUnusedSA += tmp.InPktsUnusedSA;
2809 static int copy_rx_sc_stats(struct sk_buff *skb, struct macsec_rx_sc_stats *sum)
2811 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2812 sum->InOctetsValidated,
2813 MACSEC_RXSC_STATS_ATTR_PAD) ||
2814 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2815 sum->InOctetsDecrypted,
2816 MACSEC_RXSC_STATS_ATTR_PAD) ||
2817 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2818 sum->InPktsUnchecked,
2819 MACSEC_RXSC_STATS_ATTR_PAD) ||
2820 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2822 MACSEC_RXSC_STATS_ATTR_PAD) ||
2823 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2825 MACSEC_RXSC_STATS_ATTR_PAD) ||
2826 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2828 MACSEC_RXSC_STATS_ATTR_PAD) ||
2829 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2831 MACSEC_RXSC_STATS_ATTR_PAD) ||
2832 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2833 sum->InPktsNotValid,
2834 MACSEC_RXSC_STATS_ATTR_PAD) ||
2835 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2836 sum->InPktsNotUsingSA,
2837 MACSEC_RXSC_STATS_ATTR_PAD) ||
2838 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2839 sum->InPktsUnusedSA,
2840 MACSEC_RXSC_STATS_ATTR_PAD))
2846 static void get_tx_sc_stats(struct net_device *dev,
2847 struct macsec_tx_sc_stats *sum)
2849 struct macsec_dev *macsec = macsec_priv(dev);
2852 /* If h/w offloading is available, propagate to the device */
2853 if (macsec_is_offloaded(macsec)) {
2854 const struct macsec_ops *ops;
2855 struct macsec_context ctx;
2857 ops = macsec_get_ops(macsec, &ctx);
2859 ctx.stats.tx_sc_stats = sum;
2860 ctx.secy = &macsec_priv(dev)->secy;
2861 macsec_offload(ops->mdo_get_tx_sc_stats, &ctx);
2866 for_each_possible_cpu(cpu) {
2867 const struct pcpu_tx_sc_stats *stats;
2868 struct macsec_tx_sc_stats tmp;
2871 stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu);
2873 start = u64_stats_fetch_begin_irq(&stats->syncp);
2874 memcpy(&tmp, &stats->stats, sizeof(tmp));
2875 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2877 sum->OutPktsProtected += tmp.OutPktsProtected;
2878 sum->OutPktsEncrypted += tmp.OutPktsEncrypted;
2879 sum->OutOctetsProtected += tmp.OutOctetsProtected;
2880 sum->OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2884 static int copy_tx_sc_stats(struct sk_buff *skb, struct macsec_tx_sc_stats *sum)
2886 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2887 sum->OutPktsProtected,
2888 MACSEC_TXSC_STATS_ATTR_PAD) ||
2889 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2890 sum->OutPktsEncrypted,
2891 MACSEC_TXSC_STATS_ATTR_PAD) ||
2892 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2893 sum->OutOctetsProtected,
2894 MACSEC_TXSC_STATS_ATTR_PAD) ||
2895 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2896 sum->OutOctetsEncrypted,
2897 MACSEC_TXSC_STATS_ATTR_PAD))
2903 static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum)
2905 struct macsec_dev *macsec = macsec_priv(dev);
2908 /* If h/w offloading is available, propagate to the device */
2909 if (macsec_is_offloaded(macsec)) {
2910 const struct macsec_ops *ops;
2911 struct macsec_context ctx;
2913 ops = macsec_get_ops(macsec, &ctx);
2915 ctx.stats.dev_stats = sum;
2916 ctx.secy = &macsec_priv(dev)->secy;
2917 macsec_offload(ops->mdo_get_dev_stats, &ctx);
2922 for_each_possible_cpu(cpu) {
2923 const struct pcpu_secy_stats *stats;
2924 struct macsec_dev_stats tmp;
2927 stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu);
2929 start = u64_stats_fetch_begin_irq(&stats->syncp);
2930 memcpy(&tmp, &stats->stats, sizeof(tmp));
2931 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2933 sum->OutPktsUntagged += tmp.OutPktsUntagged;
2934 sum->InPktsUntagged += tmp.InPktsUntagged;
2935 sum->OutPktsTooLong += tmp.OutPktsTooLong;
2936 sum->InPktsNoTag += tmp.InPktsNoTag;
2937 sum->InPktsBadTag += tmp.InPktsBadTag;
2938 sum->InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2939 sum->InPktsNoSCI += tmp.InPktsNoSCI;
2940 sum->InPktsOverrun += tmp.InPktsOverrun;
2944 static int copy_secy_stats(struct sk_buff *skb, struct macsec_dev_stats *sum)
2946 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2947 sum->OutPktsUntagged,
2948 MACSEC_SECY_STATS_ATTR_PAD) ||
2949 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2950 sum->InPktsUntagged,
2951 MACSEC_SECY_STATS_ATTR_PAD) ||
2952 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2953 sum->OutPktsTooLong,
2954 MACSEC_SECY_STATS_ATTR_PAD) ||
2955 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2957 MACSEC_SECY_STATS_ATTR_PAD) ||
2958 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2960 MACSEC_SECY_STATS_ATTR_PAD) ||
2961 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2962 sum->InPktsUnknownSCI,
2963 MACSEC_SECY_STATS_ATTR_PAD) ||
2964 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2966 MACSEC_SECY_STATS_ATTR_PAD) ||
2967 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2969 MACSEC_SECY_STATS_ATTR_PAD))
2975 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2977 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2978 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
2985 switch (secy->key_len) {
2986 case MACSEC_GCM_AES_128_SAK_LEN:
2987 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
2989 case MACSEC_GCM_AES_256_SAK_LEN:
2990 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
2996 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2997 MACSEC_SECY_ATTR_PAD) ||
2998 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2999 csid, MACSEC_SECY_ATTR_PAD) ||
3000 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
3001 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
3002 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
3003 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
3004 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
3005 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
3006 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
3007 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
3008 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
3009 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
3012 if (secy->replay_protect) {
3013 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
3017 nla_nest_end(skb, secy_nest);
3021 nla_nest_cancel(skb, secy_nest);
3025 static noinline_for_stack int
3026 dump_secy(struct macsec_secy *secy, struct net_device *dev,
3027 struct sk_buff *skb, struct netlink_callback *cb)
3029 struct macsec_tx_sc_stats tx_sc_stats = {0, };
3030 struct macsec_tx_sa_stats tx_sa_stats = {0, };
3031 struct macsec_rx_sc_stats rx_sc_stats = {0, };
3032 struct macsec_rx_sa_stats rx_sa_stats = {0, };
3033 struct macsec_dev *macsec = netdev_priv(dev);
3034 struct macsec_dev_stats dev_stats = {0, };
3035 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3036 struct nlattr *txsa_list, *rxsc_list;
3037 struct macsec_rx_sc *rx_sc;
3038 struct nlattr *attr;
3042 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3043 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
3047 genl_dump_check_consistent(cb, hdr);
3049 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
3050 goto nla_put_failure;
3052 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
3054 goto nla_put_failure;
3055 if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
3056 goto nla_put_failure;
3057 nla_nest_end(skb, attr);
3059 if (nla_put_secy(secy, skb))
3060 goto nla_put_failure;
3062 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
3064 goto nla_put_failure;
3066 get_tx_sc_stats(dev, &tx_sc_stats);
3067 if (copy_tx_sc_stats(skb, &tx_sc_stats)) {
3068 nla_nest_cancel(skb, attr);
3069 goto nla_put_failure;
3071 nla_nest_end(skb, attr);
3073 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
3075 goto nla_put_failure;
3076 get_secy_stats(dev, &dev_stats);
3077 if (copy_secy_stats(skb, &dev_stats)) {
3078 nla_nest_cancel(skb, attr);
3079 goto nla_put_failure;
3081 nla_nest_end(skb, attr);
3083 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
3085 goto nla_put_failure;
3086 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
3087 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
3088 struct nlattr *txsa_nest;
3095 txsa_nest = nla_nest_start_noflag(skb, j++);
3097 nla_nest_cancel(skb, txsa_list);
3098 goto nla_put_failure;
3101 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
3103 nla_nest_cancel(skb, txsa_nest);
3104 nla_nest_cancel(skb, txsa_list);
3105 goto nla_put_failure;
3107 memset(&tx_sa_stats, 0, sizeof(tx_sa_stats));
3108 get_tx_sa_stats(dev, i, tx_sa, &tx_sa_stats);
3109 if (copy_tx_sa_stats(skb, &tx_sa_stats)) {
3110 nla_nest_cancel(skb, attr);
3111 nla_nest_cancel(skb, txsa_nest);
3112 nla_nest_cancel(skb, txsa_list);
3113 goto nla_put_failure;
3115 nla_nest_end(skb, attr);
3118 pn = tx_sa->next_pn;
3119 pn_len = MACSEC_XPN_PN_LEN;
3121 pn = tx_sa->next_pn_halves.lower;
3122 pn_len = MACSEC_DEFAULT_PN_LEN;
3125 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3126 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3127 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
3128 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) ||
3129 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
3130 nla_nest_cancel(skb, txsa_nest);
3131 nla_nest_cancel(skb, txsa_list);
3132 goto nla_put_failure;
3135 nla_nest_end(skb, txsa_nest);
3137 nla_nest_end(skb, txsa_list);
3139 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
3141 goto nla_put_failure;
3144 for_each_rxsc_rtnl(secy, rx_sc) {
3146 struct nlattr *rxsa_list;
3147 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
3150 nla_nest_cancel(skb, rxsc_list);
3151 goto nla_put_failure;
3154 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
3155 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
3156 MACSEC_RXSC_ATTR_PAD)) {
3157 nla_nest_cancel(skb, rxsc_nest);
3158 nla_nest_cancel(skb, rxsc_list);
3159 goto nla_put_failure;
3162 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
3164 nla_nest_cancel(skb, rxsc_nest);
3165 nla_nest_cancel(skb, rxsc_list);
3166 goto nla_put_failure;
3168 memset(&rx_sc_stats, 0, sizeof(rx_sc_stats));
3169 get_rx_sc_stats(dev, rx_sc, &rx_sc_stats);
3170 if (copy_rx_sc_stats(skb, &rx_sc_stats)) {
3171 nla_nest_cancel(skb, attr);
3172 nla_nest_cancel(skb, rxsc_nest);
3173 nla_nest_cancel(skb, rxsc_list);
3174 goto nla_put_failure;
3176 nla_nest_end(skb, attr);
3178 rxsa_list = nla_nest_start_noflag(skb,
3179 MACSEC_RXSC_ATTR_SA_LIST);
3181 nla_nest_cancel(skb, rxsc_nest);
3182 nla_nest_cancel(skb, rxsc_list);
3183 goto nla_put_failure;
3186 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
3187 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
3188 struct nlattr *rxsa_nest;
3195 rxsa_nest = nla_nest_start_noflag(skb, k++);
3197 nla_nest_cancel(skb, rxsa_list);
3198 nla_nest_cancel(skb, rxsc_nest);
3199 nla_nest_cancel(skb, rxsc_list);
3200 goto nla_put_failure;
3203 attr = nla_nest_start_noflag(skb,
3204 MACSEC_SA_ATTR_STATS);
3206 nla_nest_cancel(skb, rxsa_list);
3207 nla_nest_cancel(skb, rxsc_nest);
3208 nla_nest_cancel(skb, rxsc_list);
3209 goto nla_put_failure;
3211 memset(&rx_sa_stats, 0, sizeof(rx_sa_stats));
3212 get_rx_sa_stats(dev, rx_sc, i, rx_sa, &rx_sa_stats);
3213 if (copy_rx_sa_stats(skb, &rx_sa_stats)) {
3214 nla_nest_cancel(skb, attr);
3215 nla_nest_cancel(skb, rxsa_list);
3216 nla_nest_cancel(skb, rxsc_nest);
3217 nla_nest_cancel(skb, rxsc_list);
3218 goto nla_put_failure;
3220 nla_nest_end(skb, attr);
3223 pn = rx_sa->next_pn;
3224 pn_len = MACSEC_XPN_PN_LEN;
3226 pn = rx_sa->next_pn_halves.lower;
3227 pn_len = MACSEC_DEFAULT_PN_LEN;
3230 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3231 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3232 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
3233 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) ||
3234 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
3235 nla_nest_cancel(skb, rxsa_nest);
3236 nla_nest_cancel(skb, rxsc_nest);
3237 nla_nest_cancel(skb, rxsc_list);
3238 goto nla_put_failure;
3240 nla_nest_end(skb, rxsa_nest);
3243 nla_nest_end(skb, rxsa_list);
3244 nla_nest_end(skb, rxsc_nest);
3247 nla_nest_end(skb, rxsc_list);
3249 genlmsg_end(skb, hdr);
3254 genlmsg_cancel(skb, hdr);
3258 static int macsec_generation = 1; /* protected by RTNL */
3260 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
3262 struct net *net = sock_net(skb->sk);
3263 struct net_device *dev;
3266 dev_idx = cb->args[0];
3271 cb->seq = macsec_generation;
3273 for_each_netdev(net, dev) {
3274 struct macsec_secy *secy;
3279 if (!netif_is_macsec(dev))
3282 secy = &macsec_priv(dev)->secy;
3283 if (dump_secy(secy, dev, skb, cb) < 0)
3295 static const struct genl_small_ops macsec_genl_ops[] = {
3297 .cmd = MACSEC_CMD_GET_TXSC,
3298 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3299 .dumpit = macsec_dump_txsc,
3302 .cmd = MACSEC_CMD_ADD_RXSC,
3303 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3304 .doit = macsec_add_rxsc,
3305 .flags = GENL_ADMIN_PERM,
3308 .cmd = MACSEC_CMD_DEL_RXSC,
3309 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3310 .doit = macsec_del_rxsc,
3311 .flags = GENL_ADMIN_PERM,
3314 .cmd = MACSEC_CMD_UPD_RXSC,
3315 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3316 .doit = macsec_upd_rxsc,
3317 .flags = GENL_ADMIN_PERM,
3320 .cmd = MACSEC_CMD_ADD_TXSA,
3321 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3322 .doit = macsec_add_txsa,
3323 .flags = GENL_ADMIN_PERM,
3326 .cmd = MACSEC_CMD_DEL_TXSA,
3327 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3328 .doit = macsec_del_txsa,
3329 .flags = GENL_ADMIN_PERM,
3332 .cmd = MACSEC_CMD_UPD_TXSA,
3333 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3334 .doit = macsec_upd_txsa,
3335 .flags = GENL_ADMIN_PERM,
3338 .cmd = MACSEC_CMD_ADD_RXSA,
3339 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3340 .doit = macsec_add_rxsa,
3341 .flags = GENL_ADMIN_PERM,
3344 .cmd = MACSEC_CMD_DEL_RXSA,
3345 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3346 .doit = macsec_del_rxsa,
3347 .flags = GENL_ADMIN_PERM,
3350 .cmd = MACSEC_CMD_UPD_RXSA,
3351 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3352 .doit = macsec_upd_rxsa,
3353 .flags = GENL_ADMIN_PERM,
3356 .cmd = MACSEC_CMD_UPD_OFFLOAD,
3357 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3358 .doit = macsec_upd_offload,
3359 .flags = GENL_ADMIN_PERM,
3363 static struct genl_family macsec_fam __ro_after_init = {
3364 .name = MACSEC_GENL_NAME,
3366 .version = MACSEC_GENL_VERSION,
3367 .maxattr = MACSEC_ATTR_MAX,
3368 .policy = macsec_genl_policy,
3370 .module = THIS_MODULE,
3371 .small_ops = macsec_genl_ops,
3372 .n_small_ops = ARRAY_SIZE(macsec_genl_ops),
3375 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
3376 struct net_device *dev)
3378 struct macsec_dev *macsec = netdev_priv(dev);
3379 struct macsec_secy *secy = &macsec->secy;
3380 struct pcpu_secy_stats *secy_stats;
3383 if (macsec_is_offloaded(netdev_priv(dev))) {
3384 skb->dev = macsec->real_dev;
3385 return dev_queue_xmit(skb);
3389 if (!secy->protect_frames) {
3390 secy_stats = this_cpu_ptr(macsec->stats);
3391 u64_stats_update_begin(&secy_stats->syncp);
3392 secy_stats->stats.OutPktsUntagged++;
3393 u64_stats_update_end(&secy_stats->syncp);
3394 skb->dev = macsec->real_dev;
3396 ret = dev_queue_xmit(skb);
3397 count_tx(dev, ret, len);
3401 if (!secy->operational) {
3403 dev->stats.tx_dropped++;
3404 return NETDEV_TX_OK;
3407 skb = macsec_encrypt(skb, dev);
3409 if (PTR_ERR(skb) != -EINPROGRESS)
3410 dev->stats.tx_dropped++;
3411 return NETDEV_TX_OK;
3414 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
3416 macsec_encrypt_finish(skb, dev);
3418 ret = dev_queue_xmit(skb);
3419 count_tx(dev, ret, len);
3423 #define SW_MACSEC_FEATURES \
3424 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
3426 /* If h/w offloading is enabled, use real device features save for
3427 * VLAN_FEATURES - they require additional ops
3428 * HW_MACSEC - no reason to report it
3430 #define REAL_DEV_FEATURES(dev) \
3431 ((dev)->features & ~(NETIF_F_VLAN_FEATURES | NETIF_F_HW_MACSEC))
3433 static int macsec_dev_init(struct net_device *dev)
3435 struct macsec_dev *macsec = macsec_priv(dev);
3436 struct net_device *real_dev = macsec->real_dev;
3439 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3443 err = gro_cells_init(&macsec->gro_cells, dev);
3445 free_percpu(dev->tstats);
3449 if (macsec_is_offloaded(macsec)) {
3450 dev->features = REAL_DEV_FEATURES(real_dev);
3452 dev->features = real_dev->features & SW_MACSEC_FEATURES;
3453 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
3456 dev->needed_headroom = real_dev->needed_headroom +
3457 MACSEC_NEEDED_HEADROOM;
3458 dev->needed_tailroom = real_dev->needed_tailroom +
3459 MACSEC_NEEDED_TAILROOM;
3461 if (is_zero_ether_addr(dev->dev_addr))
3462 eth_hw_addr_inherit(dev, real_dev);
3463 if (is_zero_ether_addr(dev->broadcast))
3464 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
3466 /* Get macsec's reference to real_dev */
3467 dev_hold_track(real_dev, &macsec->dev_tracker, GFP_KERNEL);
3472 static void macsec_dev_uninit(struct net_device *dev)
3474 struct macsec_dev *macsec = macsec_priv(dev);
3476 gro_cells_destroy(&macsec->gro_cells);
3477 free_percpu(dev->tstats);
3480 static netdev_features_t macsec_fix_features(struct net_device *dev,
3481 netdev_features_t features)
3483 struct macsec_dev *macsec = macsec_priv(dev);
3484 struct net_device *real_dev = macsec->real_dev;
3486 if (macsec_is_offloaded(macsec))
3487 return REAL_DEV_FEATURES(real_dev);
3489 features &= (real_dev->features & SW_MACSEC_FEATURES) |
3490 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
3491 features |= NETIF_F_LLTX;
3496 static int macsec_dev_open(struct net_device *dev)
3498 struct macsec_dev *macsec = macsec_priv(dev);
3499 struct net_device *real_dev = macsec->real_dev;
3502 err = dev_uc_add(real_dev, dev->dev_addr);
3506 if (dev->flags & IFF_ALLMULTI) {
3507 err = dev_set_allmulti(real_dev, 1);
3512 if (dev->flags & IFF_PROMISC) {
3513 err = dev_set_promiscuity(real_dev, 1);
3515 goto clear_allmulti;
3518 /* If h/w offloading is available, propagate to the device */
3519 if (macsec_is_offloaded(macsec)) {
3520 const struct macsec_ops *ops;
3521 struct macsec_context ctx;
3523 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3526 goto clear_allmulti;
3529 ctx.secy = &macsec->secy;
3530 err = macsec_offload(ops->mdo_dev_open, &ctx);
3532 goto clear_allmulti;
3535 if (netif_carrier_ok(real_dev))
3536 netif_carrier_on(dev);
3540 if (dev->flags & IFF_ALLMULTI)
3541 dev_set_allmulti(real_dev, -1);
3543 dev_uc_del(real_dev, dev->dev_addr);
3544 netif_carrier_off(dev);
3548 static int macsec_dev_stop(struct net_device *dev)
3550 struct macsec_dev *macsec = macsec_priv(dev);
3551 struct net_device *real_dev = macsec->real_dev;
3553 netif_carrier_off(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(macsec, &ctx);
3562 ctx.secy = &macsec->secy;
3563 macsec_offload(ops->mdo_dev_stop, &ctx);
3567 dev_mc_unsync(real_dev, dev);
3568 dev_uc_unsync(real_dev, dev);
3570 if (dev->flags & IFF_ALLMULTI)
3571 dev_set_allmulti(real_dev, -1);
3573 if (dev->flags & IFF_PROMISC)
3574 dev_set_promiscuity(real_dev, -1);
3576 dev_uc_del(real_dev, dev->dev_addr);
3581 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
3583 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3585 if (!(dev->flags & IFF_UP))
3588 if (change & IFF_ALLMULTI)
3589 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
3591 if (change & IFF_PROMISC)
3592 dev_set_promiscuity(real_dev,
3593 dev->flags & IFF_PROMISC ? 1 : -1);
3596 static void macsec_dev_set_rx_mode(struct net_device *dev)
3598 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3600 dev_mc_sync(real_dev, dev);
3601 dev_uc_sync(real_dev, dev);
3604 static int macsec_set_mac_address(struct net_device *dev, void *p)
3606 struct macsec_dev *macsec = macsec_priv(dev);
3607 struct net_device *real_dev = macsec->real_dev;
3608 struct sockaddr *addr = p;
3611 if (!is_valid_ether_addr(addr->sa_data))
3612 return -EADDRNOTAVAIL;
3614 if (!(dev->flags & IFF_UP))
3617 err = dev_uc_add(real_dev, addr->sa_data);
3621 dev_uc_del(real_dev, dev->dev_addr);
3624 eth_hw_addr_set(dev, addr->sa_data);
3625 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
3627 /* If h/w offloading is available, propagate to the device */
3628 if (macsec_is_offloaded(macsec)) {
3629 const struct macsec_ops *ops;
3630 struct macsec_context ctx;
3632 ops = macsec_get_ops(macsec, &ctx);
3634 ctx.secy = &macsec->secy;
3635 macsec_offload(ops->mdo_upd_secy, &ctx);
3642 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
3644 struct macsec_dev *macsec = macsec_priv(dev);
3645 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
3647 if (macsec->real_dev->mtu - extra < new_mtu)
3655 static void macsec_get_stats64(struct net_device *dev,
3656 struct rtnl_link_stats64 *s)
3661 dev_fetch_sw_netstats(s, dev->tstats);
3663 s->rx_dropped = dev->stats.rx_dropped;
3664 s->tx_dropped = dev->stats.tx_dropped;
3667 static int macsec_get_iflink(const struct net_device *dev)
3669 return macsec_priv(dev)->real_dev->ifindex;
3672 static const struct net_device_ops macsec_netdev_ops = {
3673 .ndo_init = macsec_dev_init,
3674 .ndo_uninit = macsec_dev_uninit,
3675 .ndo_open = macsec_dev_open,
3676 .ndo_stop = macsec_dev_stop,
3677 .ndo_fix_features = macsec_fix_features,
3678 .ndo_change_mtu = macsec_change_mtu,
3679 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
3680 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
3681 .ndo_set_mac_address = macsec_set_mac_address,
3682 .ndo_start_xmit = macsec_start_xmit,
3683 .ndo_get_stats64 = macsec_get_stats64,
3684 .ndo_get_iflink = macsec_get_iflink,
3687 static const struct device_type macsec_type = {
3691 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3692 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3693 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3694 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3695 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3696 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3697 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3698 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3699 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3700 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3701 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
3702 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3703 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3704 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3707 static void macsec_free_netdev(struct net_device *dev)
3709 struct macsec_dev *macsec = macsec_priv(dev);
3711 free_percpu(macsec->stats);
3712 free_percpu(macsec->secy.tx_sc.stats);
3714 /* Get rid of the macsec's reference to real_dev */
3715 dev_put_track(macsec->real_dev, &macsec->dev_tracker);
3718 static void macsec_setup(struct net_device *dev)
3722 dev->max_mtu = ETH_MAX_MTU;
3723 dev->priv_flags |= IFF_NO_QUEUE;
3724 dev->netdev_ops = &macsec_netdev_ops;
3725 dev->needs_free_netdev = true;
3726 dev->priv_destructor = macsec_free_netdev;
3727 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3729 eth_zero_addr(dev->broadcast);
3732 static int macsec_changelink_common(struct net_device *dev,
3733 struct nlattr *data[])
3735 struct macsec_secy *secy;
3736 struct macsec_tx_sc *tx_sc;
3738 secy = &macsec_priv(dev)->secy;
3739 tx_sc = &secy->tx_sc;
3741 if (data[IFLA_MACSEC_ENCODING_SA]) {
3742 struct macsec_tx_sa *tx_sa;
3744 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3745 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3747 secy->operational = tx_sa && tx_sa->active;
3750 if (data[IFLA_MACSEC_ENCRYPT])
3751 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3753 if (data[IFLA_MACSEC_PROTECT])
3754 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3756 if (data[IFLA_MACSEC_INC_SCI])
3757 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3759 if (data[IFLA_MACSEC_ES])
3760 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3762 if (data[IFLA_MACSEC_SCB])
3763 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3765 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3766 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3768 if (data[IFLA_MACSEC_VALIDATION])
3769 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3771 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3772 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3773 case MACSEC_CIPHER_ID_GCM_AES_128:
3774 case MACSEC_DEFAULT_CIPHER_ID:
3775 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3778 case MACSEC_CIPHER_ID_GCM_AES_256:
3779 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3782 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3783 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3786 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3787 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3795 if (data[IFLA_MACSEC_WINDOW]) {
3796 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3798 /* IEEE 802.1AEbw-2013 10.7.8 - maximum replay window
3799 * for XPN cipher suites */
3801 secy->replay_window > MACSEC_XPN_MAX_REPLAY_WINDOW)
3808 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3809 struct nlattr *data[],
3810 struct netlink_ext_ack *extack)
3812 struct macsec_dev *macsec = macsec_priv(dev);
3813 struct macsec_tx_sc tx_sc;
3814 struct macsec_secy secy;
3820 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3821 data[IFLA_MACSEC_ICV_LEN] ||
3822 data[IFLA_MACSEC_SCI] ||
3823 data[IFLA_MACSEC_PORT])
3826 /* Keep a copy of unmodified secy and tx_sc, in case the offload
3827 * propagation fails, to revert macsec_changelink_common.
3829 memcpy(&secy, &macsec->secy, sizeof(secy));
3830 memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
3832 ret = macsec_changelink_common(dev, data);
3836 /* If h/w offloading is available, propagate to the device */
3837 if (macsec_is_offloaded(macsec)) {
3838 const struct macsec_ops *ops;
3839 struct macsec_context ctx;
3842 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3848 ctx.secy = &macsec->secy;
3849 ret = macsec_offload(ops->mdo_upd_secy, &ctx);
3857 memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
3858 memcpy(&macsec->secy, &secy, sizeof(secy));
3863 static void macsec_del_dev(struct macsec_dev *macsec)
3867 while (macsec->secy.rx_sc) {
3868 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3870 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3874 for (i = 0; i < MACSEC_NUM_AN; i++) {
3875 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3878 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3884 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3886 struct macsec_dev *macsec = macsec_priv(dev);
3887 struct net_device *real_dev = macsec->real_dev;
3889 /* If h/w offloading is available, propagate to the device */
3890 if (macsec_is_offloaded(macsec)) {
3891 const struct macsec_ops *ops;
3892 struct macsec_context ctx;
3894 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3896 ctx.secy = &macsec->secy;
3897 macsec_offload(ops->mdo_del_secy, &ctx);
3901 unregister_netdevice_queue(dev, head);
3902 list_del_rcu(&macsec->secys);
3903 macsec_del_dev(macsec);
3904 netdev_upper_dev_unlink(real_dev, dev);
3906 macsec_generation++;
3909 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3911 struct macsec_dev *macsec = macsec_priv(dev);
3912 struct net_device *real_dev = macsec->real_dev;
3913 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3915 macsec_common_dellink(dev, head);
3917 if (list_empty(&rxd->secys)) {
3918 netdev_rx_handler_unregister(real_dev);
3923 static int register_macsec_dev(struct net_device *real_dev,
3924 struct net_device *dev)
3926 struct macsec_dev *macsec = macsec_priv(dev);
3927 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3932 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3936 INIT_LIST_HEAD(&rxd->secys);
3938 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3946 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3950 static bool sci_exists(struct net_device *dev, sci_t sci)
3952 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3953 struct macsec_dev *macsec;
3955 list_for_each_entry(macsec, &rxd->secys, secys) {
3956 if (macsec->secy.sci == sci)
3963 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3965 struct macsec_dev *macsec = macsec_priv(dev);
3966 struct macsec_secy *secy = &macsec->secy;
3968 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3972 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3973 if (!secy->tx_sc.stats) {
3974 free_percpu(macsec->stats);
3978 if (sci == MACSEC_UNDEF_SCI)
3979 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3982 secy->operational = true;
3983 secy->key_len = DEFAULT_SAK_LEN;
3984 secy->icv_len = icv_len;
3985 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3986 secy->protect_frames = true;
3987 secy->replay_protect = false;
3988 secy->xpn = DEFAULT_XPN;
3991 secy->tx_sc.active = true;
3992 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3993 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3994 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3995 secy->tx_sc.end_station = false;
3996 secy->tx_sc.scb = false;
4001 static struct lock_class_key macsec_netdev_addr_lock_key;
4003 static int macsec_newlink(struct net *net, struct net_device *dev,
4004 struct nlattr *tb[], struct nlattr *data[],
4005 struct netlink_ext_ack *extack)
4007 struct macsec_dev *macsec = macsec_priv(dev);
4008 rx_handler_func_t *rx_handler;
4009 u8 icv_len = DEFAULT_ICV_LEN;
4010 struct net_device *real_dev;
4016 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
4019 if (real_dev->type != ARPHRD_ETHER)
4022 dev->priv_flags |= IFF_MACSEC;
4024 macsec->real_dev = real_dev;
4026 if (data && data[IFLA_MACSEC_OFFLOAD])
4027 macsec->offload = nla_get_offload(data[IFLA_MACSEC_OFFLOAD]);
4029 /* MACsec offloading is off by default */
4030 macsec->offload = MACSEC_OFFLOAD_OFF;
4032 /* Check if the offloading mode is supported by the underlying layers */
4033 if (macsec->offload != MACSEC_OFFLOAD_OFF &&
4034 !macsec_check_offload(macsec->offload, macsec))
4037 /* send_sci must be set to true when transmit sci explicitly is set */
4038 if ((data && data[IFLA_MACSEC_SCI]) &&
4039 (data && data[IFLA_MACSEC_INC_SCI])) {
4040 u8 send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
4046 if (data && data[IFLA_MACSEC_ICV_LEN])
4047 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4048 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
4054 rx_handler = rtnl_dereference(real_dev->rx_handler);
4055 if (rx_handler && rx_handler != macsec_handle_frame)
4058 err = register_netdevice(dev);
4062 netdev_lockdep_set_classes(dev);
4063 lockdep_set_class(&dev->addr_list_lock,
4064 &macsec_netdev_addr_lock_key);
4066 err = netdev_upper_dev_link(real_dev, dev, extack);
4070 /* need to be already registered so that ->init has run and
4071 * the MAC addr is set
4073 if (data && data[IFLA_MACSEC_SCI])
4074 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
4075 else if (data && data[IFLA_MACSEC_PORT])
4076 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
4078 sci = dev_to_sci(dev, MACSEC_PORT_ES);
4080 if (rx_handler && sci_exists(real_dev, sci)) {
4085 err = macsec_add_dev(dev, sci, icv_len);
4090 err = macsec_changelink_common(dev, data);
4095 /* If h/w offloading is available, propagate to the device */
4096 if (macsec_is_offloaded(macsec)) {
4097 const struct macsec_ops *ops;
4098 struct macsec_context ctx;
4100 ops = macsec_get_ops(macsec, &ctx);
4102 ctx.secy = &macsec->secy;
4103 err = macsec_offload(ops->mdo_add_secy, &ctx);
4109 err = register_macsec_dev(real_dev, dev);
4113 netif_stacked_transfer_operstate(real_dev, dev);
4114 linkwatch_fire_event(dev);
4116 macsec_generation++;
4121 macsec_del_dev(macsec);
4123 netdev_upper_dev_unlink(real_dev, dev);
4125 unregister_netdevice(dev);
4129 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
4130 struct netlink_ext_ack *extack)
4132 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
4133 u8 icv_len = DEFAULT_ICV_LEN;
4140 if (data[IFLA_MACSEC_CIPHER_SUITE])
4141 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
4143 if (data[IFLA_MACSEC_ICV_LEN]) {
4144 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4145 if (icv_len != DEFAULT_ICV_LEN) {
4146 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
4147 struct crypto_aead *dummy_tfm;
4149 dummy_tfm = macsec_alloc_tfm(dummy_key,
4152 if (IS_ERR(dummy_tfm))
4153 return PTR_ERR(dummy_tfm);
4154 crypto_free_aead(dummy_tfm);
4159 case MACSEC_CIPHER_ID_GCM_AES_128:
4160 case MACSEC_CIPHER_ID_GCM_AES_256:
4161 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
4162 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
4163 case MACSEC_DEFAULT_CIPHER_ID:
4164 if (icv_len < MACSEC_MIN_ICV_LEN ||
4165 icv_len > MACSEC_STD_ICV_LEN)
4172 if (data[IFLA_MACSEC_ENCODING_SA]) {
4173 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
4177 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
4178 flag < IFLA_MACSEC_VALIDATION;
4181 if (nla_get_u8(data[flag]) > 1)
4186 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
4187 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
4188 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
4190 if ((sci && (scb || es)) || (scb && es))
4193 if (data[IFLA_MACSEC_VALIDATION] &&
4194 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
4197 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
4198 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
4199 !data[IFLA_MACSEC_WINDOW])
4205 static struct net *macsec_get_link_net(const struct net_device *dev)
4207 return dev_net(macsec_priv(dev)->real_dev);
4210 static size_t macsec_get_size(const struct net_device *dev)
4212 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
4213 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
4214 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
4215 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
4216 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
4217 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
4218 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
4219 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
4220 nla_total_size(1) + /* IFLA_MACSEC_ES */
4221 nla_total_size(1) + /* IFLA_MACSEC_SCB */
4222 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
4223 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
4227 static int macsec_fill_info(struct sk_buff *skb,
4228 const struct net_device *dev)
4230 struct macsec_secy *secy = &macsec_priv(dev)->secy;
4231 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
4234 switch (secy->key_len) {
4235 case MACSEC_GCM_AES_128_SAK_LEN:
4236 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
4238 case MACSEC_GCM_AES_256_SAK_LEN:
4239 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
4242 goto nla_put_failure;
4245 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
4247 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
4248 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
4249 csid, IFLA_MACSEC_PAD) ||
4250 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
4251 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
4252 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
4253 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
4254 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
4255 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
4256 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
4257 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
4259 goto nla_put_failure;
4261 if (secy->replay_protect) {
4262 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
4263 goto nla_put_failure;
4272 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
4274 .priv_size = sizeof(struct macsec_dev),
4275 .maxtype = IFLA_MACSEC_MAX,
4276 .policy = macsec_rtnl_policy,
4277 .setup = macsec_setup,
4278 .validate = macsec_validate_attr,
4279 .newlink = macsec_newlink,
4280 .changelink = macsec_changelink,
4281 .dellink = macsec_dellink,
4282 .get_size = macsec_get_size,
4283 .fill_info = macsec_fill_info,
4284 .get_link_net = macsec_get_link_net,
4287 static bool is_macsec_master(struct net_device *dev)
4289 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
4292 static int macsec_notify(struct notifier_block *this, unsigned long event,
4295 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
4298 if (!is_macsec_master(real_dev))
4304 case NETDEV_CHANGE: {
4305 struct macsec_dev *m, *n;
4306 struct macsec_rxh_data *rxd;
4308 rxd = macsec_data_rtnl(real_dev);
4309 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4310 struct net_device *dev = m->secy.netdev;
4312 netif_stacked_transfer_operstate(real_dev, dev);
4316 case NETDEV_UNREGISTER: {
4317 struct macsec_dev *m, *n;
4318 struct macsec_rxh_data *rxd;
4320 rxd = macsec_data_rtnl(real_dev);
4321 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4322 macsec_common_dellink(m->secy.netdev, &head);
4325 netdev_rx_handler_unregister(real_dev);
4328 unregister_netdevice_many(&head);
4331 case NETDEV_CHANGEMTU: {
4332 struct macsec_dev *m;
4333 struct macsec_rxh_data *rxd;
4335 rxd = macsec_data_rtnl(real_dev);
4336 list_for_each_entry(m, &rxd->secys, secys) {
4337 struct net_device *dev = m->secy.netdev;
4338 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
4339 macsec_extra_len(true));
4342 dev_set_mtu(dev, mtu);
4350 static struct notifier_block macsec_notifier = {
4351 .notifier_call = macsec_notify,
4354 static int __init macsec_init(void)
4358 pr_info("MACsec IEEE 802.1AE\n");
4359 err = register_netdevice_notifier(&macsec_notifier);
4363 err = rtnl_link_register(&macsec_link_ops);
4367 err = genl_register_family(&macsec_fam);
4374 rtnl_link_unregister(&macsec_link_ops);
4376 unregister_netdevice_notifier(&macsec_notifier);
4380 static void __exit macsec_exit(void)
4382 genl_unregister_family(&macsec_fam);
4383 rtnl_link_unregister(&macsec_link_ops);
4384 unregister_netdevice_notifier(&macsec_notifier);
4388 module_init(macsec_init);
4389 module_exit(macsec_exit);
4391 MODULE_ALIAS_RTNL_LINK("macsec");
4392 MODULE_ALIAS_GENL_FAMILY("macsec");
4394 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
4395 MODULE_LICENSE("GPL v2");