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 struct macsec_dev_stats {
92 __u64 OutPktsUntagged;
97 __u64 InPktsUnknownSCI;
102 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
104 struct pcpu_secy_stats {
105 struct macsec_dev_stats stats;
106 struct u64_stats_sync syncp;
110 * struct macsec_dev - private data
112 * @real_dev: pointer to underlying netdevice
113 * @stats: MACsec device stats
114 * @secys: linked list of SecY's on the underlying device
115 * @offload: status of offloading on the MACsec device
118 struct macsec_secy secy;
119 struct net_device *real_dev;
120 struct pcpu_secy_stats __percpu *stats;
121 struct list_head secys;
122 struct gro_cells gro_cells;
123 enum macsec_offload offload;
127 * struct macsec_rxh_data - rx_handler private argument
128 * @secys: linked list of SecY's on this underlying device
130 struct macsec_rxh_data {
131 struct list_head secys;
134 static struct macsec_dev *macsec_priv(const struct net_device *dev)
136 return (struct macsec_dev *)netdev_priv(dev);
139 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
141 return rcu_dereference_bh(dev->rx_handler_data);
144 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
146 return rtnl_dereference(dev->rx_handler_data);
150 struct aead_request *req;
152 struct macsec_tx_sa *tx_sa;
153 struct macsec_rx_sa *rx_sa;
160 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
162 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
164 if (!sa || !sa->active)
167 if (!refcount_inc_not_zero(&sa->refcnt))
173 static void free_rx_sc_rcu(struct rcu_head *head)
175 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
177 free_percpu(rx_sc->stats);
181 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
183 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
186 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
188 if (refcount_dec_and_test(&sc->refcnt))
189 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
192 static void free_rxsa(struct rcu_head *head)
194 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
196 crypto_free_aead(sa->key.tfm);
197 free_percpu(sa->stats);
201 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
203 if (refcount_dec_and_test(&sa->refcnt))
204 call_rcu(&sa->rcu, free_rxsa);
207 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
209 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
211 if (!sa || !sa->active)
214 if (!refcount_inc_not_zero(&sa->refcnt))
220 static void free_txsa(struct rcu_head *head)
222 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
224 crypto_free_aead(sa->key.tfm);
225 free_percpu(sa->stats);
229 static void macsec_txsa_put(struct macsec_tx_sa *sa)
231 if (refcount_dec_and_test(&sa->refcnt))
232 call_rcu(&sa->rcu, free_txsa);
235 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
237 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
238 return (struct macsec_cb *)skb->cb;
241 #define MACSEC_PORT_ES (htons(0x0001))
242 #define MACSEC_PORT_SCB (0x0000)
243 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
244 #define MACSEC_UNDEF_SSCI ((__force ssci_t)0xffffffff)
246 #define MACSEC_GCM_AES_128_SAK_LEN 16
247 #define MACSEC_GCM_AES_256_SAK_LEN 32
249 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
250 #define DEFAULT_XPN false
251 #define DEFAULT_SEND_SCI true
252 #define DEFAULT_ENCRYPT false
253 #define DEFAULT_ENCODING_SA 0
255 static bool send_sci(const struct macsec_secy *secy)
257 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
259 return tx_sc->send_sci ||
260 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
263 static sci_t make_sci(u8 *addr, __be16 port)
267 memcpy(&sci, addr, ETH_ALEN);
268 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
273 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
278 memcpy(&sci, hdr->secure_channel_id,
279 sizeof(hdr->secure_channel_id));
281 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
286 static unsigned int macsec_sectag_len(bool sci_present)
288 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
291 static unsigned int macsec_hdr_len(bool sci_present)
293 return macsec_sectag_len(sci_present) + ETH_HLEN;
296 static unsigned int macsec_extra_len(bool sci_present)
298 return macsec_sectag_len(sci_present) + sizeof(__be16);
301 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
302 static void macsec_fill_sectag(struct macsec_eth_header *h,
303 const struct macsec_secy *secy, u32 pn,
306 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
308 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
309 h->eth.h_proto = htons(ETH_P_MACSEC);
312 h->tci_an |= MACSEC_TCI_SC;
313 memcpy(&h->secure_channel_id, &secy->sci,
314 sizeof(h->secure_channel_id));
316 if (tx_sc->end_station)
317 h->tci_an |= MACSEC_TCI_ES;
319 h->tci_an |= MACSEC_TCI_SCB;
322 h->packet_number = htonl(pn);
324 /* with GCM, C/E clear for !encrypt, both set for encrypt */
326 h->tci_an |= MACSEC_TCI_CONFID;
327 else if (secy->icv_len != DEFAULT_ICV_LEN)
328 h->tci_an |= MACSEC_TCI_C;
330 h->tci_an |= tx_sc->encoding_sa;
333 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
335 if (data_len < MIN_NON_SHORT_LEN)
336 h->short_length = data_len;
339 /* Checks if a MACsec interface is being offloaded to an hardware engine */
340 static bool macsec_is_offloaded(struct macsec_dev *macsec)
342 if (macsec->offload == MACSEC_OFFLOAD_PHY)
348 /* Checks if underlying layers implement MACsec offloading functions. */
349 static bool macsec_check_offload(enum macsec_offload offload,
350 struct macsec_dev *macsec)
352 if (!macsec || !macsec->real_dev)
355 if (offload == MACSEC_OFFLOAD_PHY)
356 return macsec->real_dev->phydev &&
357 macsec->real_dev->phydev->macsec_ops;
362 static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload,
363 struct macsec_dev *macsec,
364 struct macsec_context *ctx)
367 memset(ctx, 0, sizeof(*ctx));
368 ctx->offload = offload;
370 if (offload == MACSEC_OFFLOAD_PHY)
371 ctx->phydev = macsec->real_dev->phydev;
374 return macsec->real_dev->phydev->macsec_ops;
377 /* Returns a pointer to the MACsec ops struct if any and updates the MACsec
378 * context device reference if provided.
380 static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec,
381 struct macsec_context *ctx)
383 if (!macsec_check_offload(macsec->offload, macsec))
386 return __macsec_get_ops(macsec->offload, macsec, ctx);
389 /* validate MACsec packet according to IEEE 802.1AE-2018 9.12 */
390 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len, bool xpn)
392 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
393 int len = skb->len - 2 * ETH_ALEN;
394 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
396 /* a) It comprises at least 17 octets */
400 /* b) MACsec EtherType: already checked */
402 /* c) V bit is clear */
403 if (h->tci_an & MACSEC_TCI_VERSION)
406 /* d) ES or SCB => !SC */
407 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
408 (h->tci_an & MACSEC_TCI_SC))
411 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
415 /* rx.pn != 0 if not XPN (figure 10-5 with 802.11AEbw-2013 amendment) */
416 if (!h->packet_number && !xpn)
419 /* length check, f) g) h) i) */
421 return len == extra_len + h->short_length;
422 return len >= extra_len + MIN_NON_SHORT_LEN;
425 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
426 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
428 static void macsec_fill_iv_xpn(unsigned char *iv, ssci_t ssci, u64 pn,
431 struct gcm_iv_xpn *gcm_iv = (struct gcm_iv_xpn *)iv;
433 gcm_iv->ssci = ssci ^ salt.ssci;
434 gcm_iv->pn = cpu_to_be64(pn) ^ salt.pn;
437 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
439 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
442 gcm_iv->pn = htonl(pn);
445 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
447 return (struct macsec_eth_header *)skb_mac_header(skb);
450 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
452 return make_sci(dev->dev_addr, port);
455 static void __macsec_pn_wrapped(struct macsec_secy *secy,
456 struct macsec_tx_sa *tx_sa)
458 pr_debug("PN wrapped, transitioning to !oper\n");
459 tx_sa->active = false;
460 if (secy->protect_frames)
461 secy->operational = false;
464 void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa)
466 spin_lock_bh(&tx_sa->lock);
467 __macsec_pn_wrapped(secy, tx_sa);
468 spin_unlock_bh(&tx_sa->lock);
470 EXPORT_SYMBOL_GPL(macsec_pn_wrapped);
472 static pn_t tx_sa_update_pn(struct macsec_tx_sa *tx_sa,
473 struct macsec_secy *secy)
477 spin_lock_bh(&tx_sa->lock);
479 pn = tx_sa->next_pn_halves;
483 tx_sa->next_pn_halves.lower++;
485 if (tx_sa->next_pn == 0)
486 __macsec_pn_wrapped(secy, tx_sa);
487 spin_unlock_bh(&tx_sa->lock);
492 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
494 struct macsec_dev *macsec = netdev_priv(dev);
496 skb->dev = macsec->real_dev;
497 skb_reset_mac_header(skb);
498 skb->protocol = eth_hdr(skb)->h_proto;
501 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
502 struct macsec_tx_sa *tx_sa)
504 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
506 u64_stats_update_begin(&txsc_stats->syncp);
507 if (tx_sc->encrypt) {
508 txsc_stats->stats.OutOctetsEncrypted += skb->len;
509 txsc_stats->stats.OutPktsEncrypted++;
510 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
512 txsc_stats->stats.OutOctetsProtected += skb->len;
513 txsc_stats->stats.OutPktsProtected++;
514 this_cpu_inc(tx_sa->stats->OutPktsProtected);
516 u64_stats_update_end(&txsc_stats->syncp);
519 static void count_tx(struct net_device *dev, int ret, int len)
521 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
522 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
524 u64_stats_update_begin(&stats->syncp);
526 stats->tx_bytes += len;
527 u64_stats_update_end(&stats->syncp);
531 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
533 struct sk_buff *skb = base->data;
534 struct net_device *dev = skb->dev;
535 struct macsec_dev *macsec = macsec_priv(dev);
536 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
539 aead_request_free(macsec_skb_cb(skb)->req);
542 macsec_encrypt_finish(skb, dev);
543 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
545 ret = dev_queue_xmit(skb);
546 count_tx(dev, ret, len);
547 rcu_read_unlock_bh();
553 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
555 struct scatterlist **sg,
558 size_t size, iv_offset, sg_offset;
559 struct aead_request *req;
562 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
564 size += GCM_AES_IV_LEN;
566 size = ALIGN(size, __alignof__(struct scatterlist));
568 size += sizeof(struct scatterlist) * num_frags;
570 tmp = kmalloc(size, GFP_ATOMIC);
574 *iv = (unsigned char *)(tmp + iv_offset);
575 *sg = (struct scatterlist *)(tmp + sg_offset);
578 aead_request_set_tfm(req, tfm);
583 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
584 struct net_device *dev)
587 struct scatterlist *sg;
588 struct sk_buff *trailer;
591 struct macsec_eth_header *hh;
592 size_t unprotected_len;
593 struct aead_request *req;
594 struct macsec_secy *secy;
595 struct macsec_tx_sc *tx_sc;
596 struct macsec_tx_sa *tx_sa;
597 struct macsec_dev *macsec = macsec_priv(dev);
601 secy = &macsec->secy;
602 tx_sc = &secy->tx_sc;
604 /* 10.5.1 TX SA assignment */
605 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
607 secy->operational = false;
609 return ERR_PTR(-EINVAL);
612 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
613 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
614 struct sk_buff *nskb = skb_copy_expand(skb,
615 MACSEC_NEEDED_HEADROOM,
616 MACSEC_NEEDED_TAILROOM,
622 macsec_txsa_put(tx_sa);
624 return ERR_PTR(-ENOMEM);
627 skb = skb_unshare(skb, GFP_ATOMIC);
629 macsec_txsa_put(tx_sa);
630 return ERR_PTR(-ENOMEM);
634 unprotected_len = skb->len;
636 sci_present = send_sci(secy);
637 hh = skb_push(skb, macsec_extra_len(sci_present));
638 memmove(hh, eth, 2 * ETH_ALEN);
640 pn = tx_sa_update_pn(tx_sa, secy);
641 if (pn.full64 == 0) {
642 macsec_txsa_put(tx_sa);
644 return ERR_PTR(-ENOLINK);
646 macsec_fill_sectag(hh, secy, pn.lower, sci_present);
647 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
649 skb_put(skb, secy->icv_len);
651 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
652 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
654 u64_stats_update_begin(&secy_stats->syncp);
655 secy_stats->stats.OutPktsTooLong++;
656 u64_stats_update_end(&secy_stats->syncp);
658 macsec_txsa_put(tx_sa);
660 return ERR_PTR(-EINVAL);
663 ret = skb_cow_data(skb, 0, &trailer);
664 if (unlikely(ret < 0)) {
665 macsec_txsa_put(tx_sa);
670 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
672 macsec_txsa_put(tx_sa);
674 return ERR_PTR(-ENOMEM);
678 macsec_fill_iv_xpn(iv, tx_sa->ssci, pn.full64, tx_sa->key.salt);
680 macsec_fill_iv(iv, secy->sci, pn.lower);
682 sg_init_table(sg, ret);
683 ret = skb_to_sgvec(skb, sg, 0, skb->len);
684 if (unlikely(ret < 0)) {
685 aead_request_free(req);
686 macsec_txsa_put(tx_sa);
691 if (tx_sc->encrypt) {
692 int len = skb->len - macsec_hdr_len(sci_present) -
694 aead_request_set_crypt(req, sg, sg, len, iv);
695 aead_request_set_ad(req, macsec_hdr_len(sci_present));
697 aead_request_set_crypt(req, sg, sg, 0, iv);
698 aead_request_set_ad(req, skb->len - secy->icv_len);
701 macsec_skb_cb(skb)->req = req;
702 macsec_skb_cb(skb)->tx_sa = tx_sa;
703 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
706 ret = crypto_aead_encrypt(req);
707 if (ret == -EINPROGRESS) {
709 } else if (ret != 0) {
712 aead_request_free(req);
713 macsec_txsa_put(tx_sa);
714 return ERR_PTR(-EINVAL);
718 aead_request_free(req);
719 macsec_txsa_put(tx_sa);
724 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
726 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
727 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
728 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
731 spin_lock(&rx_sa->lock);
732 if (rx_sa->next_pn_halves.lower >= secy->replay_window)
733 lowest_pn = rx_sa->next_pn_halves.lower - secy->replay_window;
735 /* Now perform replay protection check again
736 * (see IEEE 802.1AE-2006 figure 10-5)
738 if (secy->replay_protect && pn < lowest_pn &&
739 (!secy->xpn || pn_same_half(pn, lowest_pn))) {
740 spin_unlock(&rx_sa->lock);
741 u64_stats_update_begin(&rxsc_stats->syncp);
742 rxsc_stats->stats.InPktsLate++;
743 u64_stats_update_end(&rxsc_stats->syncp);
747 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
748 u64_stats_update_begin(&rxsc_stats->syncp);
749 if (hdr->tci_an & MACSEC_TCI_E)
750 rxsc_stats->stats.InOctetsDecrypted += skb->len;
752 rxsc_stats->stats.InOctetsValidated += skb->len;
753 u64_stats_update_end(&rxsc_stats->syncp);
756 if (!macsec_skb_cb(skb)->valid) {
757 spin_unlock(&rx_sa->lock);
760 if (hdr->tci_an & MACSEC_TCI_C ||
761 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
762 u64_stats_update_begin(&rxsc_stats->syncp);
763 rxsc_stats->stats.InPktsNotValid++;
764 u64_stats_update_end(&rxsc_stats->syncp);
768 u64_stats_update_begin(&rxsc_stats->syncp);
769 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
770 rxsc_stats->stats.InPktsInvalid++;
771 this_cpu_inc(rx_sa->stats->InPktsInvalid);
772 } else if (pn < lowest_pn) {
773 rxsc_stats->stats.InPktsDelayed++;
775 rxsc_stats->stats.InPktsUnchecked++;
777 u64_stats_update_end(&rxsc_stats->syncp);
779 u64_stats_update_begin(&rxsc_stats->syncp);
780 if (pn < lowest_pn) {
781 rxsc_stats->stats.InPktsDelayed++;
783 rxsc_stats->stats.InPktsOK++;
784 this_cpu_inc(rx_sa->stats->InPktsOK);
786 u64_stats_update_end(&rxsc_stats->syncp);
788 // Instead of "pn >=" - to support pn overflow in xpn
789 if (pn + 1 > rx_sa->next_pn_halves.lower) {
790 rx_sa->next_pn_halves.lower = pn + 1;
791 } else if (secy->xpn &&
792 !pn_same_half(pn, rx_sa->next_pn_halves.lower)) {
793 rx_sa->next_pn_halves.upper++;
794 rx_sa->next_pn_halves.lower = pn + 1;
797 spin_unlock(&rx_sa->lock);
803 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
805 skb->pkt_type = PACKET_HOST;
806 skb->protocol = eth_type_trans(skb, dev);
808 skb_reset_network_header(skb);
809 if (!skb_transport_header_was_set(skb))
810 skb_reset_transport_header(skb);
811 skb_reset_mac_len(skb);
814 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
816 skb->ip_summed = CHECKSUM_NONE;
817 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
818 skb_pull(skb, hdr_len);
819 pskb_trim_unique(skb, skb->len - icv_len);
822 static void count_rx(struct net_device *dev, int len)
824 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
826 u64_stats_update_begin(&stats->syncp);
828 stats->rx_bytes += len;
829 u64_stats_update_end(&stats->syncp);
832 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
834 struct sk_buff *skb = base->data;
835 struct net_device *dev = skb->dev;
836 struct macsec_dev *macsec = macsec_priv(dev);
837 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
838 struct macsec_rx_sc *rx_sc = rx_sa->sc;
842 aead_request_free(macsec_skb_cb(skb)->req);
845 macsec_skb_cb(skb)->valid = true;
848 pn = ntohl(macsec_ethhdr(skb)->packet_number);
849 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
850 rcu_read_unlock_bh();
855 macsec_finalize_skb(skb, macsec->secy.icv_len,
856 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
857 macsec_reset_skb(skb, macsec->secy.netdev);
860 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
863 rcu_read_unlock_bh();
866 macsec_rxsa_put(rx_sa);
867 macsec_rxsc_put(rx_sc);
871 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
872 struct net_device *dev,
873 struct macsec_rx_sa *rx_sa,
875 struct macsec_secy *secy)
878 struct scatterlist *sg;
879 struct sk_buff *trailer;
881 struct aead_request *req;
882 struct macsec_eth_header *hdr;
884 u16 icv_len = secy->icv_len;
886 macsec_skb_cb(skb)->valid = false;
887 skb = skb_share_check(skb, GFP_ATOMIC);
889 return ERR_PTR(-ENOMEM);
891 ret = skb_cow_data(skb, 0, &trailer);
892 if (unlikely(ret < 0)) {
896 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
899 return ERR_PTR(-ENOMEM);
902 hdr = (struct macsec_eth_header *)skb->data;
903 hdr_pn = ntohl(hdr->packet_number);
906 pn_t recovered_pn = rx_sa->next_pn_halves;
908 recovered_pn.lower = hdr_pn;
909 if (hdr_pn < rx_sa->next_pn_halves.lower &&
910 !pn_same_half(hdr_pn, rx_sa->next_pn_halves.lower))
911 recovered_pn.upper++;
913 macsec_fill_iv_xpn(iv, rx_sa->ssci, recovered_pn.full64,
916 macsec_fill_iv(iv, sci, hdr_pn);
919 sg_init_table(sg, ret);
920 ret = skb_to_sgvec(skb, sg, 0, skb->len);
921 if (unlikely(ret < 0)) {
922 aead_request_free(req);
927 if (hdr->tci_an & MACSEC_TCI_E) {
928 /* confidentiality: ethernet + macsec header
929 * authenticated, encrypted payload
931 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
933 aead_request_set_crypt(req, sg, sg, len, iv);
934 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
935 skb = skb_unshare(skb, GFP_ATOMIC);
937 aead_request_free(req);
938 return ERR_PTR(-ENOMEM);
941 /* integrity only: all headers + data authenticated */
942 aead_request_set_crypt(req, sg, sg, icv_len, iv);
943 aead_request_set_ad(req, skb->len - icv_len);
946 macsec_skb_cb(skb)->req = req;
948 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
951 ret = crypto_aead_decrypt(req);
952 if (ret == -EINPROGRESS) {
954 } else if (ret != 0) {
955 /* decryption/authentication failed
956 * 10.6 if validateFrames is disabled, deliver anyway
958 if (ret != -EBADMSG) {
963 macsec_skb_cb(skb)->valid = true;
967 aead_request_free(req);
972 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
974 struct macsec_rx_sc *rx_sc;
976 for_each_rxsc(secy, rx_sc) {
977 if (rx_sc->sci == sci)
984 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
986 struct macsec_rx_sc *rx_sc;
988 for_each_rxsc_rtnl(secy, rx_sc) {
989 if (rx_sc->sci == sci)
996 static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
998 /* Deliver to the uncontrolled port by default */
999 enum rx_handler_result ret = RX_HANDLER_PASS;
1000 struct macsec_rxh_data *rxd;
1001 struct macsec_dev *macsec;
1004 rxd = macsec_data_rcu(skb->dev);
1006 /* 10.6 If the management control validateFrames is not
1007 * Strict, frames without a SecTAG are received, counted, and
1008 * delivered to the Controlled Port
1010 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1011 struct sk_buff *nskb;
1012 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1014 if (!macsec_is_offloaded(macsec) &&
1015 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1016 u64_stats_update_begin(&secy_stats->syncp);
1017 secy_stats->stats.InPktsNoTag++;
1018 u64_stats_update_end(&secy_stats->syncp);
1022 /* deliver on this port */
1023 nskb = skb_clone(skb, GFP_ATOMIC);
1027 nskb->dev = macsec->secy.netdev;
1029 if (netif_rx(nskb) == NET_RX_SUCCESS) {
1030 u64_stats_update_begin(&secy_stats->syncp);
1031 secy_stats->stats.InPktsUntagged++;
1032 u64_stats_update_end(&secy_stats->syncp);
1035 if (netif_running(macsec->secy.netdev) &&
1036 macsec_is_offloaded(macsec)) {
1037 ret = RX_HANDLER_EXACT;
1047 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1049 struct sk_buff *skb = *pskb;
1050 struct net_device *dev = skb->dev;
1051 struct macsec_eth_header *hdr;
1052 struct macsec_secy *secy = NULL;
1053 struct macsec_rx_sc *rx_sc;
1054 struct macsec_rx_sa *rx_sa;
1055 struct macsec_rxh_data *rxd;
1056 struct macsec_dev *macsec;
1060 struct pcpu_rx_sc_stats *rxsc_stats;
1061 struct pcpu_secy_stats *secy_stats;
1065 if (skb_headroom(skb) < ETH_HLEN)
1068 hdr = macsec_ethhdr(skb);
1069 if (hdr->eth.h_proto != htons(ETH_P_MACSEC))
1070 return handle_not_macsec(skb);
1072 skb = skb_unshare(skb, GFP_ATOMIC);
1075 return RX_HANDLER_CONSUMED;
1077 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1079 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1083 hdr = macsec_ethhdr(skb);
1085 /* Frames with a SecTAG that has the TCI E bit set but the C
1086 * bit clear are discarded, as this reserved encoding is used
1087 * to identify frames with a SecTAG that are not to be
1088 * delivered to the Controlled Port.
1090 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1091 return RX_HANDLER_PASS;
1093 /* now, pull the extra length */
1094 if (hdr->tci_an & MACSEC_TCI_SC) {
1099 /* ethernet header is part of crypto processing */
1100 skb_push(skb, ETH_HLEN);
1102 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1103 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1104 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1107 rxd = macsec_data_rcu(skb->dev);
1109 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1110 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1112 sc = sc ? macsec_rxsc_get(sc) : NULL;
1115 secy = &macsec->secy;
1125 macsec = macsec_priv(dev);
1126 secy_stats = this_cpu_ptr(macsec->stats);
1127 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1129 if (!macsec_validate_skb(skb, secy->icv_len, secy->xpn)) {
1130 u64_stats_update_begin(&secy_stats->syncp);
1131 secy_stats->stats.InPktsBadTag++;
1132 u64_stats_update_end(&secy_stats->syncp);
1136 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1138 /* 10.6.1 if the SA is not in use */
1140 /* If validateFrames is Strict or the C bit in the
1141 * SecTAG is set, discard
1143 if (hdr->tci_an & MACSEC_TCI_C ||
1144 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1145 u64_stats_update_begin(&rxsc_stats->syncp);
1146 rxsc_stats->stats.InPktsNotUsingSA++;
1147 u64_stats_update_end(&rxsc_stats->syncp);
1151 /* not Strict, the frame (with the SecTAG and ICV
1152 * removed) is delivered to the Controlled Port.
1154 u64_stats_update_begin(&rxsc_stats->syncp);
1155 rxsc_stats->stats.InPktsUnusedSA++;
1156 u64_stats_update_end(&rxsc_stats->syncp);
1160 /* First, PN check to avoid decrypting obviously wrong packets */
1161 hdr_pn = ntohl(hdr->packet_number);
1162 if (secy->replay_protect) {
1165 spin_lock(&rx_sa->lock);
1166 late = rx_sa->next_pn_halves.lower >= secy->replay_window &&
1167 hdr_pn < (rx_sa->next_pn_halves.lower - secy->replay_window);
1170 late = late && pn_same_half(rx_sa->next_pn_halves.lower, hdr_pn);
1171 spin_unlock(&rx_sa->lock);
1174 u64_stats_update_begin(&rxsc_stats->syncp);
1175 rxsc_stats->stats.InPktsLate++;
1176 u64_stats_update_end(&rxsc_stats->syncp);
1181 macsec_skb_cb(skb)->rx_sa = rx_sa;
1183 /* Disabled && !changed text => skip validation */
1184 if (hdr->tci_an & MACSEC_TCI_C ||
1185 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1186 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1189 /* the decrypt callback needs the reference */
1190 if (PTR_ERR(skb) != -EINPROGRESS) {
1191 macsec_rxsa_put(rx_sa);
1192 macsec_rxsc_put(rx_sc);
1196 return RX_HANDLER_CONSUMED;
1199 if (!macsec_post_decrypt(skb, secy, hdr_pn))
1203 macsec_finalize_skb(skb, secy->icv_len,
1204 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1205 macsec_reset_skb(skb, secy->netdev);
1208 macsec_rxsa_put(rx_sa);
1209 macsec_rxsc_put(rx_sc);
1212 ret = gro_cells_receive(&macsec->gro_cells, skb);
1213 if (ret == NET_RX_SUCCESS)
1214 count_rx(dev, skb->len);
1216 macsec->secy.netdev->stats.rx_dropped++;
1221 return RX_HANDLER_CONSUMED;
1224 macsec_rxsa_put(rx_sa);
1226 macsec_rxsc_put(rx_sc);
1231 return RX_HANDLER_CONSUMED;
1234 /* 10.6.1 if the SC is not found */
1235 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1237 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1238 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1240 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1241 struct sk_buff *nskb;
1243 secy_stats = this_cpu_ptr(macsec->stats);
1245 /* If validateFrames is Strict or the C bit in the
1246 * SecTAG is set, discard
1249 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1250 u64_stats_update_begin(&secy_stats->syncp);
1251 secy_stats->stats.InPktsNoSCI++;
1252 u64_stats_update_end(&secy_stats->syncp);
1256 /* not strict, the frame (with the SecTAG and ICV
1257 * removed) is delivered to the Controlled Port.
1259 nskb = skb_clone(skb, GFP_ATOMIC);
1263 macsec_reset_skb(nskb, macsec->secy.netdev);
1265 ret = netif_rx(nskb);
1266 if (ret == NET_RX_SUCCESS) {
1267 u64_stats_update_begin(&secy_stats->syncp);
1268 secy_stats->stats.InPktsUnknownSCI++;
1269 u64_stats_update_end(&secy_stats->syncp);
1271 macsec->secy.netdev->stats.rx_dropped++;
1277 return RX_HANDLER_PASS;
1280 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1282 struct crypto_aead *tfm;
1285 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
1290 ret = crypto_aead_setkey(tfm, key, key_len);
1294 ret = crypto_aead_setauthsize(tfm, icv_len);
1300 crypto_free_aead(tfm);
1301 return ERR_PTR(ret);
1304 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1307 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1311 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1312 if (IS_ERR(rx_sa->key.tfm)) {
1313 free_percpu(rx_sa->stats);
1314 return PTR_ERR(rx_sa->key.tfm);
1317 rx_sa->ssci = MACSEC_UNDEF_SSCI;
1318 rx_sa->active = false;
1320 refcount_set(&rx_sa->refcnt, 1);
1321 spin_lock_init(&rx_sa->lock);
1326 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1328 rx_sa->active = false;
1330 macsec_rxsa_put(rx_sa);
1333 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1337 for (i = 0; i < MACSEC_NUM_AN; i++) {
1338 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1340 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1345 macsec_rxsc_put(rx_sc);
1348 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1350 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1352 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1354 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1355 if (rx_sc->sci == sci) {
1358 rcu_assign_pointer(*rx_scp, rx_sc->next);
1366 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1368 struct macsec_rx_sc *rx_sc;
1369 struct macsec_dev *macsec;
1370 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1371 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1372 struct macsec_secy *secy;
1374 list_for_each_entry(macsec, &rxd->secys, secys) {
1375 if (find_rx_sc_rtnl(&macsec->secy, sci))
1376 return ERR_PTR(-EEXIST);
1379 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1381 return ERR_PTR(-ENOMEM);
1383 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1384 if (!rx_sc->stats) {
1386 return ERR_PTR(-ENOMEM);
1390 rx_sc->active = true;
1391 refcount_set(&rx_sc->refcnt, 1);
1393 secy = &macsec_priv(dev)->secy;
1394 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1395 rcu_assign_pointer(secy->rx_sc, rx_sc);
1403 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1406 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1410 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1411 if (IS_ERR(tx_sa->key.tfm)) {
1412 free_percpu(tx_sa->stats);
1413 return PTR_ERR(tx_sa->key.tfm);
1416 tx_sa->ssci = MACSEC_UNDEF_SSCI;
1417 tx_sa->active = false;
1418 refcount_set(&tx_sa->refcnt, 1);
1419 spin_lock_init(&tx_sa->lock);
1424 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1426 tx_sa->active = false;
1428 macsec_txsa_put(tx_sa);
1431 static struct genl_family macsec_fam;
1433 static struct net_device *get_dev_from_nl(struct net *net,
1434 struct nlattr **attrs)
1436 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1437 struct net_device *dev;
1439 dev = __dev_get_by_index(net, ifindex);
1441 return ERR_PTR(-ENODEV);
1443 if (!netif_is_macsec(dev))
1444 return ERR_PTR(-ENODEV);
1449 static sci_t nla_get_sci(const struct nlattr *nla)
1451 return (__force sci_t)nla_get_u64(nla);
1454 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1457 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1460 static ssci_t nla_get_ssci(const struct nlattr *nla)
1462 return (__force ssci_t)nla_get_u32(nla);
1465 static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value)
1467 return nla_put_u32(skb, attrtype, (__force u64)value);
1470 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1471 struct nlattr **attrs,
1472 struct nlattr **tb_sa,
1473 struct net_device **devp,
1474 struct macsec_secy **secyp,
1475 struct macsec_tx_sc **scp,
1478 struct net_device *dev;
1479 struct macsec_secy *secy;
1480 struct macsec_tx_sc *tx_sc;
1481 struct macsec_tx_sa *tx_sa;
1483 if (!tb_sa[MACSEC_SA_ATTR_AN])
1484 return ERR_PTR(-EINVAL);
1486 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1488 dev = get_dev_from_nl(net, attrs);
1490 return ERR_CAST(dev);
1492 if (*assoc_num >= MACSEC_NUM_AN)
1493 return ERR_PTR(-EINVAL);
1495 secy = &macsec_priv(dev)->secy;
1496 tx_sc = &secy->tx_sc;
1498 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1500 return ERR_PTR(-ENODEV);
1508 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1509 struct nlattr **attrs,
1510 struct nlattr **tb_rxsc,
1511 struct net_device **devp,
1512 struct macsec_secy **secyp)
1514 struct net_device *dev;
1515 struct macsec_secy *secy;
1516 struct macsec_rx_sc *rx_sc;
1519 dev = get_dev_from_nl(net, attrs);
1521 return ERR_CAST(dev);
1523 secy = &macsec_priv(dev)->secy;
1525 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1526 return ERR_PTR(-EINVAL);
1528 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1529 rx_sc = find_rx_sc_rtnl(secy, sci);
1531 return ERR_PTR(-ENODEV);
1539 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1540 struct nlattr **attrs,
1541 struct nlattr **tb_rxsc,
1542 struct nlattr **tb_sa,
1543 struct net_device **devp,
1544 struct macsec_secy **secyp,
1545 struct macsec_rx_sc **scp,
1548 struct macsec_rx_sc *rx_sc;
1549 struct macsec_rx_sa *rx_sa;
1551 if (!tb_sa[MACSEC_SA_ATTR_AN])
1552 return ERR_PTR(-EINVAL);
1554 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1555 if (*assoc_num >= MACSEC_NUM_AN)
1556 return ERR_PTR(-EINVAL);
1558 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1560 return ERR_CAST(rx_sc);
1562 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1564 return ERR_PTR(-ENODEV);
1570 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1571 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1572 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1573 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1574 [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
1577 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1578 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1579 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1582 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1583 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1584 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1585 [MACSEC_SA_ATTR_PN] = { .type = NLA_MIN_LEN, .len = 4 },
1586 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1587 .len = MACSEC_KEYID_LEN, },
1588 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1589 .len = MACSEC_MAX_KEY_LEN, },
1590 [MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 },
1591 [MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY,
1592 .len = MACSEC_SALT_LEN, },
1595 static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
1596 [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 },
1599 /* Offloads an operation to a device driver */
1600 static int macsec_offload(int (* const func)(struct macsec_context *),
1601 struct macsec_context *ctx)
1605 if (unlikely(!func))
1608 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1609 mutex_lock(&ctx->phydev->lock);
1611 /* Phase I: prepare. The drive should fail here if there are going to be
1612 * issues in the commit phase.
1614 ctx->prepare = true;
1619 /* Phase II: commit. This step cannot fail. */
1620 ctx->prepare = false;
1622 /* This should never happen: commit is not allowed to fail */
1624 WARN(1, "MACsec offloading commit failed (%d)\n", ret);
1627 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1628 mutex_unlock(&ctx->phydev->lock);
1633 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1635 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1638 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1644 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1646 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1649 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1655 static bool validate_add_rxsa(struct nlattr **attrs)
1657 if (!attrs[MACSEC_SA_ATTR_AN] ||
1658 !attrs[MACSEC_SA_ATTR_KEY] ||
1659 !attrs[MACSEC_SA_ATTR_KEYID])
1662 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1665 if (attrs[MACSEC_SA_ATTR_PN] &&
1666 *(u64 *)nla_data(attrs[MACSEC_SA_ATTR_PN]) == 0)
1669 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1670 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1674 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1680 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1682 struct net_device *dev;
1683 struct nlattr **attrs = info->attrs;
1684 struct macsec_secy *secy;
1685 struct macsec_rx_sc *rx_sc;
1686 struct macsec_rx_sa *rx_sa;
1687 unsigned char assoc_num;
1689 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1690 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1693 if (!attrs[MACSEC_ATTR_IFINDEX])
1696 if (parse_sa_config(attrs, tb_sa))
1699 if (parse_rxsc_config(attrs, tb_rxsc))
1702 if (!validate_add_rxsa(tb_sa))
1706 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1707 if (IS_ERR(rx_sc)) {
1709 return PTR_ERR(rx_sc);
1712 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1714 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1715 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1716 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1721 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1722 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1723 pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
1724 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1730 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1735 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
1736 pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n",
1737 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
1738 MACSEC_SA_ATTR_SALT);
1744 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1750 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1756 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1757 secy->key_len, secy->icv_len);
1764 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1765 spin_lock_bh(&rx_sa->lock);
1766 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
1767 spin_unlock_bh(&rx_sa->lock);
1770 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1771 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1775 /* If h/w offloading is available, propagate to the device */
1776 if (macsec_is_offloaded(netdev_priv(dev))) {
1777 const struct macsec_ops *ops;
1778 struct macsec_context ctx;
1780 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1786 ctx.sa.assoc_num = assoc_num;
1787 ctx.sa.rx_sa = rx_sa;
1788 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1791 err = macsec_offload(ops->mdo_add_rxsa, &ctx);
1797 rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
1798 nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
1802 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1803 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1815 static bool validate_add_rxsc(struct nlattr **attrs)
1817 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1820 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1821 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1828 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1830 struct net_device *dev;
1831 sci_t sci = MACSEC_UNDEF_SCI;
1832 struct nlattr **attrs = info->attrs;
1833 struct macsec_rx_sc *rx_sc;
1834 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1838 if (!attrs[MACSEC_ATTR_IFINDEX])
1841 if (parse_rxsc_config(attrs, tb_rxsc))
1844 if (!validate_add_rxsc(tb_rxsc))
1848 dev = get_dev_from_nl(genl_info_net(info), attrs);
1851 return PTR_ERR(dev);
1854 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1856 rx_sc = create_rx_sc(dev, sci);
1857 if (IS_ERR(rx_sc)) {
1859 return PTR_ERR(rx_sc);
1862 was_active = rx_sc->active;
1863 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1864 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1866 if (macsec_is_offloaded(netdev_priv(dev))) {
1867 const struct macsec_ops *ops;
1868 struct macsec_context ctx;
1870 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1878 ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
1888 rx_sc->active = was_active;
1893 static bool validate_add_txsa(struct nlattr **attrs)
1895 if (!attrs[MACSEC_SA_ATTR_AN] ||
1896 !attrs[MACSEC_SA_ATTR_PN] ||
1897 !attrs[MACSEC_SA_ATTR_KEY] ||
1898 !attrs[MACSEC_SA_ATTR_KEYID])
1901 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1904 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1907 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1908 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1912 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1918 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1920 struct net_device *dev;
1921 struct nlattr **attrs = info->attrs;
1922 struct macsec_secy *secy;
1923 struct macsec_tx_sc *tx_sc;
1924 struct macsec_tx_sa *tx_sa;
1925 unsigned char assoc_num;
1927 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1928 bool was_operational;
1931 if (!attrs[MACSEC_ATTR_IFINDEX])
1934 if (parse_sa_config(attrs, tb_sa))
1937 if (!validate_add_txsa(tb_sa))
1941 dev = get_dev_from_nl(genl_info_net(info), attrs);
1944 return PTR_ERR(dev);
1947 secy = &macsec_priv(dev)->secy;
1948 tx_sc = &secy->tx_sc;
1950 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1952 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1953 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1954 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1959 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1960 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1961 pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
1962 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1968 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1973 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
1974 pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n",
1975 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
1976 MACSEC_SA_ATTR_SALT);
1982 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1988 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1994 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1995 secy->key_len, secy->icv_len);
2002 spin_lock_bh(&tx_sa->lock);
2003 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2004 spin_unlock_bh(&tx_sa->lock);
2006 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2007 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2009 was_operational = secy->operational;
2010 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
2011 secy->operational = true;
2013 /* If h/w offloading is available, propagate to the device */
2014 if (macsec_is_offloaded(netdev_priv(dev))) {
2015 const struct macsec_ops *ops;
2016 struct macsec_context ctx;
2018 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2024 ctx.sa.assoc_num = assoc_num;
2025 ctx.sa.tx_sa = tx_sa;
2026 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2029 err = macsec_offload(ops->mdo_add_txsa, &ctx);
2035 tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
2036 nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
2040 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
2041 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
2048 secy->operational = was_operational;
2054 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
2056 struct nlattr **attrs = info->attrs;
2057 struct net_device *dev;
2058 struct macsec_secy *secy;
2059 struct macsec_rx_sc *rx_sc;
2060 struct macsec_rx_sa *rx_sa;
2062 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2063 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2066 if (!attrs[MACSEC_ATTR_IFINDEX])
2069 if (parse_sa_config(attrs, tb_sa))
2072 if (parse_rxsc_config(attrs, tb_rxsc))
2076 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2077 &dev, &secy, &rx_sc, &assoc_num);
2078 if (IS_ERR(rx_sa)) {
2080 return PTR_ERR(rx_sa);
2083 if (rx_sa->active) {
2088 /* If h/w offloading is available, propagate to the device */
2089 if (macsec_is_offloaded(netdev_priv(dev))) {
2090 const struct macsec_ops *ops;
2091 struct macsec_context ctx;
2093 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2099 ctx.sa.assoc_num = assoc_num;
2100 ctx.sa.rx_sa = rx_sa;
2102 ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
2107 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
2119 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
2121 struct nlattr **attrs = info->attrs;
2122 struct net_device *dev;
2123 struct macsec_secy *secy;
2124 struct macsec_rx_sc *rx_sc;
2126 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2129 if (!attrs[MACSEC_ATTR_IFINDEX])
2132 if (parse_rxsc_config(attrs, tb_rxsc))
2135 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2139 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
2142 return PTR_ERR(dev);
2145 secy = &macsec_priv(dev)->secy;
2146 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2148 rx_sc = del_rx_sc(secy, sci);
2154 /* If h/w offloading is available, propagate to the device */
2155 if (macsec_is_offloaded(netdev_priv(dev))) {
2156 const struct macsec_ops *ops;
2157 struct macsec_context ctx;
2159 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2166 ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
2181 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2183 struct nlattr **attrs = info->attrs;
2184 struct net_device *dev;
2185 struct macsec_secy *secy;
2186 struct macsec_tx_sc *tx_sc;
2187 struct macsec_tx_sa *tx_sa;
2189 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2192 if (!attrs[MACSEC_ATTR_IFINDEX])
2195 if (parse_sa_config(attrs, tb_sa))
2199 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2200 &dev, &secy, &tx_sc, &assoc_num);
2201 if (IS_ERR(tx_sa)) {
2203 return PTR_ERR(tx_sa);
2206 if (tx_sa->active) {
2211 /* If h/w offloading is available, propagate to the device */
2212 if (macsec_is_offloaded(netdev_priv(dev))) {
2213 const struct macsec_ops *ops;
2214 struct macsec_context ctx;
2216 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2222 ctx.sa.assoc_num = assoc_num;
2223 ctx.sa.tx_sa = tx_sa;
2225 ret = macsec_offload(ops->mdo_del_txsa, &ctx);
2230 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2242 static bool validate_upd_sa(struct nlattr **attrs)
2244 if (!attrs[MACSEC_SA_ATTR_AN] ||
2245 attrs[MACSEC_SA_ATTR_KEY] ||
2246 attrs[MACSEC_SA_ATTR_KEYID] ||
2247 attrs[MACSEC_SA_ATTR_SSCI] ||
2248 attrs[MACSEC_SA_ATTR_SALT])
2251 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2254 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2257 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2258 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2265 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2267 struct nlattr **attrs = info->attrs;
2268 struct net_device *dev;
2269 struct macsec_secy *secy;
2270 struct macsec_tx_sc *tx_sc;
2271 struct macsec_tx_sa *tx_sa;
2273 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2274 bool was_operational, was_active;
2280 if (!attrs[MACSEC_ATTR_IFINDEX])
2283 if (parse_sa_config(attrs, tb_sa))
2286 if (!validate_upd_sa(tb_sa))
2290 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2291 &dev, &secy, &tx_sc, &assoc_num);
2292 if (IS_ERR(tx_sa)) {
2294 return PTR_ERR(tx_sa);
2297 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2300 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2301 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2302 pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
2303 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2308 spin_lock_bh(&tx_sa->lock);
2309 prev_pn = tx_sa->next_pn_halves;
2310 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2311 spin_unlock_bh(&tx_sa->lock);
2314 was_active = tx_sa->active;
2315 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2316 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2318 was_operational = secy->operational;
2319 if (assoc_num == tx_sc->encoding_sa)
2320 secy->operational = tx_sa->active;
2322 /* If h/w offloading is available, propagate to the device */
2323 if (macsec_is_offloaded(netdev_priv(dev))) {
2324 const struct macsec_ops *ops;
2325 struct macsec_context ctx;
2327 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2333 ctx.sa.assoc_num = assoc_num;
2334 ctx.sa.tx_sa = tx_sa;
2336 ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
2346 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2347 spin_lock_bh(&tx_sa->lock);
2348 tx_sa->next_pn_halves = prev_pn;
2349 spin_unlock_bh(&tx_sa->lock);
2351 tx_sa->active = was_active;
2352 secy->operational = was_operational;
2357 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2359 struct nlattr **attrs = info->attrs;
2360 struct net_device *dev;
2361 struct macsec_secy *secy;
2362 struct macsec_rx_sc *rx_sc;
2363 struct macsec_rx_sa *rx_sa;
2365 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2366 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2373 if (!attrs[MACSEC_ATTR_IFINDEX])
2376 if (parse_rxsc_config(attrs, tb_rxsc))
2379 if (parse_sa_config(attrs, tb_sa))
2382 if (!validate_upd_sa(tb_sa))
2386 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2387 &dev, &secy, &rx_sc, &assoc_num);
2388 if (IS_ERR(rx_sa)) {
2390 return PTR_ERR(rx_sa);
2393 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2396 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2397 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2398 pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
2399 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2404 spin_lock_bh(&rx_sa->lock);
2405 prev_pn = rx_sa->next_pn_halves;
2406 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2407 spin_unlock_bh(&rx_sa->lock);
2410 was_active = rx_sa->active;
2411 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2412 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2414 /* If h/w offloading is available, propagate to the device */
2415 if (macsec_is_offloaded(netdev_priv(dev))) {
2416 const struct macsec_ops *ops;
2417 struct macsec_context ctx;
2419 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2425 ctx.sa.assoc_num = assoc_num;
2426 ctx.sa.rx_sa = rx_sa;
2428 ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
2437 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2438 spin_lock_bh(&rx_sa->lock);
2439 rx_sa->next_pn_halves = prev_pn;
2440 spin_unlock_bh(&rx_sa->lock);
2442 rx_sa->active = was_active;
2447 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2449 struct nlattr **attrs = info->attrs;
2450 struct net_device *dev;
2451 struct macsec_secy *secy;
2452 struct macsec_rx_sc *rx_sc;
2453 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2454 unsigned int prev_n_rx_sc;
2458 if (!attrs[MACSEC_ATTR_IFINDEX])
2461 if (parse_rxsc_config(attrs, tb_rxsc))
2464 if (!validate_add_rxsc(tb_rxsc))
2468 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2469 if (IS_ERR(rx_sc)) {
2471 return PTR_ERR(rx_sc);
2474 was_active = rx_sc->active;
2475 prev_n_rx_sc = secy->n_rx_sc;
2476 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2477 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2479 if (rx_sc->active != new)
2480 secy->n_rx_sc += new ? 1 : -1;
2482 rx_sc->active = new;
2485 /* If h/w offloading is available, propagate to the device */
2486 if (macsec_is_offloaded(netdev_priv(dev))) {
2487 const struct macsec_ops *ops;
2488 struct macsec_context ctx;
2490 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2498 ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
2508 secy->n_rx_sc = prev_n_rx_sc;
2509 rx_sc->active = was_active;
2514 static bool macsec_is_configured(struct macsec_dev *macsec)
2516 struct macsec_secy *secy = &macsec->secy;
2517 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2520 if (secy->n_rx_sc > 0)
2523 for (i = 0; i < MACSEC_NUM_AN; i++)
2530 static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
2532 struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
2533 enum macsec_offload offload, prev_offload;
2534 int (*func)(struct macsec_context *ctx);
2535 struct nlattr **attrs = info->attrs;
2536 struct net_device *dev, *loop_dev;
2537 const struct macsec_ops *ops;
2538 struct macsec_context ctx;
2539 struct macsec_dev *macsec;
2540 struct net *loop_net;
2543 if (!attrs[MACSEC_ATTR_IFINDEX])
2546 if (!attrs[MACSEC_ATTR_OFFLOAD])
2549 if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
2550 attrs[MACSEC_ATTR_OFFLOAD],
2551 macsec_genl_offload_policy, NULL))
2554 dev = get_dev_from_nl(genl_info_net(info), attrs);
2556 return PTR_ERR(dev);
2557 macsec = macsec_priv(dev);
2559 offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
2560 if (macsec->offload == offload)
2563 /* Check if the offloading mode is supported by the underlying layers */
2564 if (offload != MACSEC_OFFLOAD_OFF &&
2565 !macsec_check_offload(offload, macsec))
2568 if (offload == MACSEC_OFFLOAD_OFF)
2569 goto skip_limitation;
2571 /* Check the physical interface isn't offloading another interface
2574 for_each_net(loop_net) {
2575 for_each_netdev(loop_net, loop_dev) {
2576 struct macsec_dev *priv;
2578 if (!netif_is_macsec(loop_dev))
2581 priv = macsec_priv(loop_dev);
2583 if (priv->real_dev == macsec->real_dev &&
2584 priv->offload != MACSEC_OFFLOAD_OFF)
2590 /* Check if the net device is busy. */
2591 if (netif_running(dev))
2596 prev_offload = macsec->offload;
2597 macsec->offload = offload;
2599 /* Check if the device already has rules configured: we do not support
2602 if (macsec_is_configured(macsec)) {
2607 ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
2614 if (prev_offload == MACSEC_OFFLOAD_OFF)
2615 func = ops->mdo_add_secy;
2617 func = ops->mdo_del_secy;
2619 ctx.secy = &macsec->secy;
2620 ret = macsec_offload(func, &ctx);
2628 macsec->offload = prev_offload;
2634 static int copy_tx_sa_stats(struct sk_buff *skb,
2635 struct macsec_tx_sa_stats __percpu *pstats)
2637 struct macsec_tx_sa_stats sum = {0, };
2640 for_each_possible_cpu(cpu) {
2641 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2643 sum.OutPktsProtected += stats->OutPktsProtected;
2644 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2647 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2648 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2654 static noinline_for_stack int
2655 copy_rx_sa_stats(struct sk_buff *skb,
2656 struct macsec_rx_sa_stats __percpu *pstats)
2658 struct macsec_rx_sa_stats sum = {0, };
2661 for_each_possible_cpu(cpu) {
2662 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2664 sum.InPktsOK += stats->InPktsOK;
2665 sum.InPktsInvalid += stats->InPktsInvalid;
2666 sum.InPktsNotValid += stats->InPktsNotValid;
2667 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2668 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2671 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2672 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2673 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2674 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2675 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2681 static noinline_for_stack int
2682 copy_rx_sc_stats(struct sk_buff *skb, struct pcpu_rx_sc_stats __percpu *pstats)
2684 struct macsec_rx_sc_stats sum = {0, };
2687 for_each_possible_cpu(cpu) {
2688 const struct pcpu_rx_sc_stats *stats;
2689 struct macsec_rx_sc_stats tmp;
2692 stats = per_cpu_ptr(pstats, cpu);
2694 start = u64_stats_fetch_begin_irq(&stats->syncp);
2695 memcpy(&tmp, &stats->stats, sizeof(tmp));
2696 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2698 sum.InOctetsValidated += tmp.InOctetsValidated;
2699 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2700 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2701 sum.InPktsDelayed += tmp.InPktsDelayed;
2702 sum.InPktsOK += tmp.InPktsOK;
2703 sum.InPktsInvalid += tmp.InPktsInvalid;
2704 sum.InPktsLate += tmp.InPktsLate;
2705 sum.InPktsNotValid += tmp.InPktsNotValid;
2706 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2707 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2710 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2711 sum.InOctetsValidated,
2712 MACSEC_RXSC_STATS_ATTR_PAD) ||
2713 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2714 sum.InOctetsDecrypted,
2715 MACSEC_RXSC_STATS_ATTR_PAD) ||
2716 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2717 sum.InPktsUnchecked,
2718 MACSEC_RXSC_STATS_ATTR_PAD) ||
2719 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2721 MACSEC_RXSC_STATS_ATTR_PAD) ||
2722 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2724 MACSEC_RXSC_STATS_ATTR_PAD) ||
2725 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2727 MACSEC_RXSC_STATS_ATTR_PAD) ||
2728 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2730 MACSEC_RXSC_STATS_ATTR_PAD) ||
2731 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2733 MACSEC_RXSC_STATS_ATTR_PAD) ||
2734 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2735 sum.InPktsNotUsingSA,
2736 MACSEC_RXSC_STATS_ATTR_PAD) ||
2737 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2739 MACSEC_RXSC_STATS_ATTR_PAD))
2745 static noinline_for_stack int
2746 copy_tx_sc_stats(struct sk_buff *skb, struct pcpu_tx_sc_stats __percpu *pstats)
2748 struct macsec_tx_sc_stats sum = {0, };
2751 for_each_possible_cpu(cpu) {
2752 const struct pcpu_tx_sc_stats *stats;
2753 struct macsec_tx_sc_stats tmp;
2756 stats = per_cpu_ptr(pstats, cpu);
2758 start = u64_stats_fetch_begin_irq(&stats->syncp);
2759 memcpy(&tmp, &stats->stats, sizeof(tmp));
2760 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2762 sum.OutPktsProtected += tmp.OutPktsProtected;
2763 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2764 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2765 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2768 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2769 sum.OutPktsProtected,
2770 MACSEC_TXSC_STATS_ATTR_PAD) ||
2771 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2772 sum.OutPktsEncrypted,
2773 MACSEC_TXSC_STATS_ATTR_PAD) ||
2774 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2775 sum.OutOctetsProtected,
2776 MACSEC_TXSC_STATS_ATTR_PAD) ||
2777 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2778 sum.OutOctetsEncrypted,
2779 MACSEC_TXSC_STATS_ATTR_PAD))
2785 static noinline_for_stack int
2786 copy_secy_stats(struct sk_buff *skb, struct pcpu_secy_stats __percpu *pstats)
2788 struct macsec_dev_stats sum = {0, };
2791 for_each_possible_cpu(cpu) {
2792 const struct pcpu_secy_stats *stats;
2793 struct macsec_dev_stats tmp;
2796 stats = per_cpu_ptr(pstats, cpu);
2798 start = u64_stats_fetch_begin_irq(&stats->syncp);
2799 memcpy(&tmp, &stats->stats, sizeof(tmp));
2800 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2802 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2803 sum.InPktsUntagged += tmp.InPktsUntagged;
2804 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2805 sum.InPktsNoTag += tmp.InPktsNoTag;
2806 sum.InPktsBadTag += tmp.InPktsBadTag;
2807 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2808 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2809 sum.InPktsOverrun += tmp.InPktsOverrun;
2812 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2813 sum.OutPktsUntagged,
2814 MACSEC_SECY_STATS_ATTR_PAD) ||
2815 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2817 MACSEC_SECY_STATS_ATTR_PAD) ||
2818 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2820 MACSEC_SECY_STATS_ATTR_PAD) ||
2821 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2823 MACSEC_SECY_STATS_ATTR_PAD) ||
2824 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2826 MACSEC_SECY_STATS_ATTR_PAD) ||
2827 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2828 sum.InPktsUnknownSCI,
2829 MACSEC_SECY_STATS_ATTR_PAD) ||
2830 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2832 MACSEC_SECY_STATS_ATTR_PAD) ||
2833 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2835 MACSEC_SECY_STATS_ATTR_PAD))
2841 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2843 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2844 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
2851 switch (secy->key_len) {
2852 case MACSEC_GCM_AES_128_SAK_LEN:
2853 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
2855 case MACSEC_GCM_AES_256_SAK_LEN:
2856 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
2862 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2863 MACSEC_SECY_ATTR_PAD) ||
2864 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2865 csid, MACSEC_SECY_ATTR_PAD) ||
2866 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2867 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2868 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2869 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2870 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2871 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2872 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2873 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2874 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2875 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2878 if (secy->replay_protect) {
2879 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2883 nla_nest_end(skb, secy_nest);
2887 nla_nest_cancel(skb, secy_nest);
2891 static noinline_for_stack int
2892 dump_secy(struct macsec_secy *secy, struct net_device *dev,
2893 struct sk_buff *skb, struct netlink_callback *cb)
2895 struct macsec_dev *macsec = netdev_priv(dev);
2896 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2897 struct nlattr *txsa_list, *rxsc_list;
2898 struct macsec_rx_sc *rx_sc;
2899 struct nlattr *attr;
2903 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2904 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2908 genl_dump_check_consistent(cb, hdr);
2910 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2911 goto nla_put_failure;
2913 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
2915 goto nla_put_failure;
2916 if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
2917 goto nla_put_failure;
2918 nla_nest_end(skb, attr);
2920 if (nla_put_secy(secy, skb))
2921 goto nla_put_failure;
2923 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
2925 goto nla_put_failure;
2926 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2927 nla_nest_cancel(skb, attr);
2928 goto nla_put_failure;
2930 nla_nest_end(skb, attr);
2932 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
2934 goto nla_put_failure;
2935 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2936 nla_nest_cancel(skb, attr);
2937 goto nla_put_failure;
2939 nla_nest_end(skb, attr);
2941 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
2943 goto nla_put_failure;
2944 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2945 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2946 struct nlattr *txsa_nest;
2953 txsa_nest = nla_nest_start_noflag(skb, j++);
2955 nla_nest_cancel(skb, txsa_list);
2956 goto nla_put_failure;
2960 pn = tx_sa->next_pn;
2961 pn_len = MACSEC_XPN_PN_LEN;
2963 pn = tx_sa->next_pn_halves.lower;
2964 pn_len = MACSEC_DEFAULT_PN_LEN;
2967 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2968 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
2969 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2970 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) ||
2971 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2972 nla_nest_cancel(skb, txsa_nest);
2973 nla_nest_cancel(skb, txsa_list);
2974 goto nla_put_failure;
2977 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
2979 nla_nest_cancel(skb, txsa_nest);
2980 nla_nest_cancel(skb, txsa_list);
2981 goto nla_put_failure;
2983 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2984 nla_nest_cancel(skb, attr);
2985 nla_nest_cancel(skb, txsa_nest);
2986 nla_nest_cancel(skb, txsa_list);
2987 goto nla_put_failure;
2989 nla_nest_end(skb, attr);
2991 nla_nest_end(skb, txsa_nest);
2993 nla_nest_end(skb, txsa_list);
2995 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
2997 goto nla_put_failure;
3000 for_each_rxsc_rtnl(secy, rx_sc) {
3002 struct nlattr *rxsa_list;
3003 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
3006 nla_nest_cancel(skb, rxsc_list);
3007 goto nla_put_failure;
3010 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
3011 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
3012 MACSEC_RXSC_ATTR_PAD)) {
3013 nla_nest_cancel(skb, rxsc_nest);
3014 nla_nest_cancel(skb, rxsc_list);
3015 goto nla_put_failure;
3018 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
3020 nla_nest_cancel(skb, rxsc_nest);
3021 nla_nest_cancel(skb, rxsc_list);
3022 goto nla_put_failure;
3024 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
3025 nla_nest_cancel(skb, attr);
3026 nla_nest_cancel(skb, rxsc_nest);
3027 nla_nest_cancel(skb, rxsc_list);
3028 goto nla_put_failure;
3030 nla_nest_end(skb, attr);
3032 rxsa_list = nla_nest_start_noflag(skb,
3033 MACSEC_RXSC_ATTR_SA_LIST);
3035 nla_nest_cancel(skb, rxsc_nest);
3036 nla_nest_cancel(skb, rxsc_list);
3037 goto nla_put_failure;
3040 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
3041 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
3042 struct nlattr *rxsa_nest;
3049 rxsa_nest = nla_nest_start_noflag(skb, k++);
3051 nla_nest_cancel(skb, rxsa_list);
3052 nla_nest_cancel(skb, rxsc_nest);
3053 nla_nest_cancel(skb, rxsc_list);
3054 goto nla_put_failure;
3057 attr = nla_nest_start_noflag(skb,
3058 MACSEC_SA_ATTR_STATS);
3060 nla_nest_cancel(skb, rxsa_list);
3061 nla_nest_cancel(skb, rxsc_nest);
3062 nla_nest_cancel(skb, rxsc_list);
3063 goto nla_put_failure;
3065 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
3066 nla_nest_cancel(skb, attr);
3067 nla_nest_cancel(skb, rxsa_list);
3068 nla_nest_cancel(skb, rxsc_nest);
3069 nla_nest_cancel(skb, rxsc_list);
3070 goto nla_put_failure;
3072 nla_nest_end(skb, attr);
3075 pn = rx_sa->next_pn;
3076 pn_len = MACSEC_XPN_PN_LEN;
3078 pn = rx_sa->next_pn_halves.lower;
3079 pn_len = MACSEC_DEFAULT_PN_LEN;
3082 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3083 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3084 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
3085 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) ||
3086 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
3087 nla_nest_cancel(skb, rxsa_nest);
3088 nla_nest_cancel(skb, rxsc_nest);
3089 nla_nest_cancel(skb, rxsc_list);
3090 goto nla_put_failure;
3092 nla_nest_end(skb, rxsa_nest);
3095 nla_nest_end(skb, rxsa_list);
3096 nla_nest_end(skb, rxsc_nest);
3099 nla_nest_end(skb, rxsc_list);
3101 genlmsg_end(skb, hdr);
3106 genlmsg_cancel(skb, hdr);
3110 static int macsec_generation = 1; /* protected by RTNL */
3112 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
3114 struct net *net = sock_net(skb->sk);
3115 struct net_device *dev;
3118 dev_idx = cb->args[0];
3123 cb->seq = macsec_generation;
3125 for_each_netdev(net, dev) {
3126 struct macsec_secy *secy;
3131 if (!netif_is_macsec(dev))
3134 secy = &macsec_priv(dev)->secy;
3135 if (dump_secy(secy, dev, skb, cb) < 0)
3147 static const struct genl_ops macsec_genl_ops[] = {
3149 .cmd = MACSEC_CMD_GET_TXSC,
3150 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3151 .dumpit = macsec_dump_txsc,
3154 .cmd = MACSEC_CMD_ADD_RXSC,
3155 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3156 .doit = macsec_add_rxsc,
3157 .flags = GENL_ADMIN_PERM,
3160 .cmd = MACSEC_CMD_DEL_RXSC,
3161 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3162 .doit = macsec_del_rxsc,
3163 .flags = GENL_ADMIN_PERM,
3166 .cmd = MACSEC_CMD_UPD_RXSC,
3167 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3168 .doit = macsec_upd_rxsc,
3169 .flags = GENL_ADMIN_PERM,
3172 .cmd = MACSEC_CMD_ADD_TXSA,
3173 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3174 .doit = macsec_add_txsa,
3175 .flags = GENL_ADMIN_PERM,
3178 .cmd = MACSEC_CMD_DEL_TXSA,
3179 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3180 .doit = macsec_del_txsa,
3181 .flags = GENL_ADMIN_PERM,
3184 .cmd = MACSEC_CMD_UPD_TXSA,
3185 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3186 .doit = macsec_upd_txsa,
3187 .flags = GENL_ADMIN_PERM,
3190 .cmd = MACSEC_CMD_ADD_RXSA,
3191 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3192 .doit = macsec_add_rxsa,
3193 .flags = GENL_ADMIN_PERM,
3196 .cmd = MACSEC_CMD_DEL_RXSA,
3197 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3198 .doit = macsec_del_rxsa,
3199 .flags = GENL_ADMIN_PERM,
3202 .cmd = MACSEC_CMD_UPD_RXSA,
3203 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3204 .doit = macsec_upd_rxsa,
3205 .flags = GENL_ADMIN_PERM,
3208 .cmd = MACSEC_CMD_UPD_OFFLOAD,
3209 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3210 .doit = macsec_upd_offload,
3211 .flags = GENL_ADMIN_PERM,
3215 static struct genl_family macsec_fam __ro_after_init = {
3216 .name = MACSEC_GENL_NAME,
3218 .version = MACSEC_GENL_VERSION,
3219 .maxattr = MACSEC_ATTR_MAX,
3220 .policy = macsec_genl_policy,
3222 .module = THIS_MODULE,
3223 .ops = macsec_genl_ops,
3224 .n_ops = ARRAY_SIZE(macsec_genl_ops),
3227 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
3228 struct net_device *dev)
3230 struct macsec_dev *macsec = netdev_priv(dev);
3231 struct macsec_secy *secy = &macsec->secy;
3232 struct pcpu_secy_stats *secy_stats;
3235 if (macsec_is_offloaded(netdev_priv(dev))) {
3236 skb->dev = macsec->real_dev;
3237 return dev_queue_xmit(skb);
3241 if (!secy->protect_frames) {
3242 secy_stats = this_cpu_ptr(macsec->stats);
3243 u64_stats_update_begin(&secy_stats->syncp);
3244 secy_stats->stats.OutPktsUntagged++;
3245 u64_stats_update_end(&secy_stats->syncp);
3246 skb->dev = macsec->real_dev;
3248 ret = dev_queue_xmit(skb);
3249 count_tx(dev, ret, len);
3253 if (!secy->operational) {
3255 dev->stats.tx_dropped++;
3256 return NETDEV_TX_OK;
3259 skb = macsec_encrypt(skb, dev);
3261 if (PTR_ERR(skb) != -EINPROGRESS)
3262 dev->stats.tx_dropped++;
3263 return NETDEV_TX_OK;
3266 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
3268 macsec_encrypt_finish(skb, dev);
3270 ret = dev_queue_xmit(skb);
3271 count_tx(dev, ret, len);
3275 #define MACSEC_FEATURES \
3276 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
3278 static int macsec_dev_init(struct net_device *dev)
3280 struct macsec_dev *macsec = macsec_priv(dev);
3281 struct net_device *real_dev = macsec->real_dev;
3284 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3288 err = gro_cells_init(&macsec->gro_cells, dev);
3290 free_percpu(dev->tstats);
3294 dev->features = real_dev->features & MACSEC_FEATURES;
3295 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
3297 dev->needed_headroom = real_dev->needed_headroom +
3298 MACSEC_NEEDED_HEADROOM;
3299 dev->needed_tailroom = real_dev->needed_tailroom +
3300 MACSEC_NEEDED_TAILROOM;
3302 if (is_zero_ether_addr(dev->dev_addr))
3303 eth_hw_addr_inherit(dev, real_dev);
3304 if (is_zero_ether_addr(dev->broadcast))
3305 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
3310 static void macsec_dev_uninit(struct net_device *dev)
3312 struct macsec_dev *macsec = macsec_priv(dev);
3314 gro_cells_destroy(&macsec->gro_cells);
3315 free_percpu(dev->tstats);
3318 static netdev_features_t macsec_fix_features(struct net_device *dev,
3319 netdev_features_t features)
3321 struct macsec_dev *macsec = macsec_priv(dev);
3322 struct net_device *real_dev = macsec->real_dev;
3324 features &= (real_dev->features & MACSEC_FEATURES) |
3325 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
3326 features |= NETIF_F_LLTX;
3331 static int macsec_dev_open(struct net_device *dev)
3333 struct macsec_dev *macsec = macsec_priv(dev);
3334 struct net_device *real_dev = macsec->real_dev;
3337 err = dev_uc_add(real_dev, dev->dev_addr);
3341 if (dev->flags & IFF_ALLMULTI) {
3342 err = dev_set_allmulti(real_dev, 1);
3347 if (dev->flags & IFF_PROMISC) {
3348 err = dev_set_promiscuity(real_dev, 1);
3350 goto clear_allmulti;
3353 /* If h/w offloading is available, propagate to the device */
3354 if (macsec_is_offloaded(macsec)) {
3355 const struct macsec_ops *ops;
3356 struct macsec_context ctx;
3358 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3361 goto clear_allmulti;
3364 err = macsec_offload(ops->mdo_dev_open, &ctx);
3366 goto clear_allmulti;
3369 if (netif_carrier_ok(real_dev))
3370 netif_carrier_on(dev);
3374 if (dev->flags & IFF_ALLMULTI)
3375 dev_set_allmulti(real_dev, -1);
3377 dev_uc_del(real_dev, dev->dev_addr);
3378 netif_carrier_off(dev);
3382 static int macsec_dev_stop(struct net_device *dev)
3384 struct macsec_dev *macsec = macsec_priv(dev);
3385 struct net_device *real_dev = macsec->real_dev;
3387 netif_carrier_off(dev);
3389 /* If h/w offloading is available, propagate to the device */
3390 if (macsec_is_offloaded(macsec)) {
3391 const struct macsec_ops *ops;
3392 struct macsec_context ctx;
3394 ops = macsec_get_ops(macsec, &ctx);
3396 macsec_offload(ops->mdo_dev_stop, &ctx);
3399 dev_mc_unsync(real_dev, dev);
3400 dev_uc_unsync(real_dev, dev);
3402 if (dev->flags & IFF_ALLMULTI)
3403 dev_set_allmulti(real_dev, -1);
3405 if (dev->flags & IFF_PROMISC)
3406 dev_set_promiscuity(real_dev, -1);
3408 dev_uc_del(real_dev, dev->dev_addr);
3413 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
3415 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3417 if (!(dev->flags & IFF_UP))
3420 if (change & IFF_ALLMULTI)
3421 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
3423 if (change & IFF_PROMISC)
3424 dev_set_promiscuity(real_dev,
3425 dev->flags & IFF_PROMISC ? 1 : -1);
3428 static void macsec_dev_set_rx_mode(struct net_device *dev)
3430 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3432 dev_mc_sync(real_dev, dev);
3433 dev_uc_sync(real_dev, dev);
3436 static int macsec_set_mac_address(struct net_device *dev, void *p)
3438 struct macsec_dev *macsec = macsec_priv(dev);
3439 struct net_device *real_dev = macsec->real_dev;
3440 struct sockaddr *addr = p;
3443 if (!is_valid_ether_addr(addr->sa_data))
3444 return -EADDRNOTAVAIL;
3446 if (!(dev->flags & IFF_UP))
3449 err = dev_uc_add(real_dev, addr->sa_data);
3453 dev_uc_del(real_dev, dev->dev_addr);
3456 ether_addr_copy(dev->dev_addr, addr->sa_data);
3457 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
3459 /* If h/w offloading is available, propagate to the device */
3460 if (macsec_is_offloaded(macsec)) {
3461 const struct macsec_ops *ops;
3462 struct macsec_context ctx;
3464 ops = macsec_get_ops(macsec, &ctx);
3466 ctx.secy = &macsec->secy;
3467 macsec_offload(ops->mdo_upd_secy, &ctx);
3474 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
3476 struct macsec_dev *macsec = macsec_priv(dev);
3477 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
3479 if (macsec->real_dev->mtu - extra < new_mtu)
3487 static void macsec_get_stats64(struct net_device *dev,
3488 struct rtnl_link_stats64 *s)
3495 for_each_possible_cpu(cpu) {
3496 struct pcpu_sw_netstats *stats;
3497 struct pcpu_sw_netstats tmp;
3500 stats = per_cpu_ptr(dev->tstats, cpu);
3502 start = u64_stats_fetch_begin_irq(&stats->syncp);
3503 tmp.rx_packets = stats->rx_packets;
3504 tmp.rx_bytes = stats->rx_bytes;
3505 tmp.tx_packets = stats->tx_packets;
3506 tmp.tx_bytes = stats->tx_bytes;
3507 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
3509 s->rx_packets += tmp.rx_packets;
3510 s->rx_bytes += tmp.rx_bytes;
3511 s->tx_packets += tmp.tx_packets;
3512 s->tx_bytes += tmp.tx_bytes;
3515 s->rx_dropped = dev->stats.rx_dropped;
3516 s->tx_dropped = dev->stats.tx_dropped;
3519 static int macsec_get_iflink(const struct net_device *dev)
3521 return macsec_priv(dev)->real_dev->ifindex;
3524 static const struct net_device_ops macsec_netdev_ops = {
3525 .ndo_init = macsec_dev_init,
3526 .ndo_uninit = macsec_dev_uninit,
3527 .ndo_open = macsec_dev_open,
3528 .ndo_stop = macsec_dev_stop,
3529 .ndo_fix_features = macsec_fix_features,
3530 .ndo_change_mtu = macsec_change_mtu,
3531 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
3532 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
3533 .ndo_set_mac_address = macsec_set_mac_address,
3534 .ndo_start_xmit = macsec_start_xmit,
3535 .ndo_get_stats64 = macsec_get_stats64,
3536 .ndo_get_iflink = macsec_get_iflink,
3539 static const struct device_type macsec_type = {
3543 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3544 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3545 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3546 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3547 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3548 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3549 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3550 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3551 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3552 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3553 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
3554 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3555 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3556 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3559 static void macsec_free_netdev(struct net_device *dev)
3561 struct macsec_dev *macsec = macsec_priv(dev);
3563 free_percpu(macsec->stats);
3564 free_percpu(macsec->secy.tx_sc.stats);
3568 static void macsec_setup(struct net_device *dev)
3572 dev->max_mtu = ETH_MAX_MTU;
3573 dev->priv_flags |= IFF_NO_QUEUE;
3574 dev->netdev_ops = &macsec_netdev_ops;
3575 dev->needs_free_netdev = true;
3576 dev->priv_destructor = macsec_free_netdev;
3577 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3579 eth_zero_addr(dev->broadcast);
3582 static int macsec_changelink_common(struct net_device *dev,
3583 struct nlattr *data[])
3585 struct macsec_secy *secy;
3586 struct macsec_tx_sc *tx_sc;
3588 secy = &macsec_priv(dev)->secy;
3589 tx_sc = &secy->tx_sc;
3591 if (data[IFLA_MACSEC_ENCODING_SA]) {
3592 struct macsec_tx_sa *tx_sa;
3594 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3595 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3597 secy->operational = tx_sa && tx_sa->active;
3600 if (data[IFLA_MACSEC_WINDOW])
3601 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3603 if (data[IFLA_MACSEC_ENCRYPT])
3604 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3606 if (data[IFLA_MACSEC_PROTECT])
3607 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3609 if (data[IFLA_MACSEC_INC_SCI])
3610 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3612 if (data[IFLA_MACSEC_ES])
3613 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3615 if (data[IFLA_MACSEC_SCB])
3616 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3618 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3619 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3621 if (data[IFLA_MACSEC_VALIDATION])
3622 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3624 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3625 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3626 case MACSEC_CIPHER_ID_GCM_AES_128:
3627 case MACSEC_DEFAULT_CIPHER_ID:
3628 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3631 case MACSEC_CIPHER_ID_GCM_AES_256:
3632 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3635 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3636 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3639 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3640 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3651 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3652 struct nlattr *data[],
3653 struct netlink_ext_ack *extack)
3655 struct macsec_dev *macsec = macsec_priv(dev);
3656 struct macsec_tx_sa tx_sc;
3657 struct macsec_secy secy;
3663 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3664 data[IFLA_MACSEC_ICV_LEN] ||
3665 data[IFLA_MACSEC_SCI] ||
3666 data[IFLA_MACSEC_PORT])
3669 /* Keep a copy of unmodified secy and tx_sc, in case the offload
3670 * propagation fails, to revert macsec_changelink_common.
3672 memcpy(&secy, &macsec->secy, sizeof(secy));
3673 memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
3675 ret = macsec_changelink_common(dev, data);
3679 /* If h/w offloading is available, propagate to the device */
3680 if (macsec_is_offloaded(macsec)) {
3681 const struct macsec_ops *ops;
3682 struct macsec_context ctx;
3685 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3691 ctx.secy = &macsec->secy;
3692 ret = macsec_offload(ops->mdo_upd_secy, &ctx);
3700 memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
3701 memcpy(&macsec->secy, &secy, sizeof(secy));
3706 static void macsec_del_dev(struct macsec_dev *macsec)
3710 while (macsec->secy.rx_sc) {
3711 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3713 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3717 for (i = 0; i < MACSEC_NUM_AN; i++) {
3718 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3721 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3727 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3729 struct macsec_dev *macsec = macsec_priv(dev);
3730 struct net_device *real_dev = macsec->real_dev;
3732 unregister_netdevice_queue(dev, head);
3733 list_del_rcu(&macsec->secys);
3734 macsec_del_dev(macsec);
3735 netdev_upper_dev_unlink(real_dev, dev);
3737 macsec_generation++;
3740 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3742 struct macsec_dev *macsec = macsec_priv(dev);
3743 struct net_device *real_dev = macsec->real_dev;
3744 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3746 /* If h/w offloading is available, propagate to the device */
3747 if (macsec_is_offloaded(macsec)) {
3748 const struct macsec_ops *ops;
3749 struct macsec_context ctx;
3751 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3753 ctx.secy = &macsec->secy;
3754 macsec_offload(ops->mdo_del_secy, &ctx);
3758 macsec_common_dellink(dev, head);
3760 if (list_empty(&rxd->secys)) {
3761 netdev_rx_handler_unregister(real_dev);
3766 static int register_macsec_dev(struct net_device *real_dev,
3767 struct net_device *dev)
3769 struct macsec_dev *macsec = macsec_priv(dev);
3770 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3775 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3779 INIT_LIST_HEAD(&rxd->secys);
3781 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3789 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3793 static bool sci_exists(struct net_device *dev, sci_t sci)
3795 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3796 struct macsec_dev *macsec;
3798 list_for_each_entry(macsec, &rxd->secys, secys) {
3799 if (macsec->secy.sci == sci)
3806 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3808 struct macsec_dev *macsec = macsec_priv(dev);
3809 struct macsec_secy *secy = &macsec->secy;
3811 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3815 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3816 if (!secy->tx_sc.stats) {
3817 free_percpu(macsec->stats);
3821 if (sci == MACSEC_UNDEF_SCI)
3822 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3825 secy->operational = true;
3826 secy->key_len = DEFAULT_SAK_LEN;
3827 secy->icv_len = icv_len;
3828 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3829 secy->protect_frames = true;
3830 secy->replay_protect = false;
3831 secy->xpn = DEFAULT_XPN;
3834 secy->tx_sc.active = true;
3835 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3836 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3837 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3838 secy->tx_sc.end_station = false;
3839 secy->tx_sc.scb = false;
3844 static int macsec_newlink(struct net *net, struct net_device *dev,
3845 struct nlattr *tb[], struct nlattr *data[],
3846 struct netlink_ext_ack *extack)
3848 struct macsec_dev *macsec = macsec_priv(dev);
3849 struct net_device *real_dev;
3852 u8 icv_len = DEFAULT_ICV_LEN;
3853 rx_handler_func_t *rx_handler;
3857 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3860 if (real_dev->type != ARPHRD_ETHER)
3863 dev->priv_flags |= IFF_MACSEC;
3865 macsec->real_dev = real_dev;
3867 /* MACsec offloading is off by default */
3868 macsec->offload = MACSEC_OFFLOAD_OFF;
3870 if (data && data[IFLA_MACSEC_ICV_LEN])
3871 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3872 dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3874 rx_handler = rtnl_dereference(real_dev->rx_handler);
3875 if (rx_handler && rx_handler != macsec_handle_frame)
3878 err = register_netdevice(dev);
3882 err = netdev_upper_dev_link(real_dev, dev, extack);
3886 /* need to be already registered so that ->init has run and
3887 * the MAC addr is set
3889 if (data && data[IFLA_MACSEC_SCI])
3890 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3891 else if (data && data[IFLA_MACSEC_PORT])
3892 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3894 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3896 if (rx_handler && sci_exists(real_dev, sci)) {
3901 err = macsec_add_dev(dev, sci, icv_len);
3906 err = macsec_changelink_common(dev, data);
3911 err = register_macsec_dev(real_dev, dev);
3915 netif_stacked_transfer_operstate(real_dev, dev);
3916 linkwatch_fire_event(dev);
3918 macsec_generation++;
3923 macsec_del_dev(macsec);
3925 netdev_upper_dev_unlink(real_dev, dev);
3927 unregister_netdevice(dev);
3931 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3932 struct netlink_ext_ack *extack)
3934 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3935 u8 icv_len = DEFAULT_ICV_LEN;
3942 if (data[IFLA_MACSEC_CIPHER_SUITE])
3943 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3945 if (data[IFLA_MACSEC_ICV_LEN]) {
3946 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3947 if (icv_len != DEFAULT_ICV_LEN) {
3948 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3949 struct crypto_aead *dummy_tfm;
3951 dummy_tfm = macsec_alloc_tfm(dummy_key,
3954 if (IS_ERR(dummy_tfm))
3955 return PTR_ERR(dummy_tfm);
3956 crypto_free_aead(dummy_tfm);
3961 case MACSEC_CIPHER_ID_GCM_AES_128:
3962 case MACSEC_CIPHER_ID_GCM_AES_256:
3963 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3964 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3965 case MACSEC_DEFAULT_CIPHER_ID:
3966 if (icv_len < MACSEC_MIN_ICV_LEN ||
3967 icv_len > MACSEC_STD_ICV_LEN)
3974 if (data[IFLA_MACSEC_ENCODING_SA]) {
3975 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3979 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3980 flag < IFLA_MACSEC_VALIDATION;
3983 if (nla_get_u8(data[flag]) > 1)
3988 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3989 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3990 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3992 if ((sci && (scb || es)) || (scb && es))
3995 if (data[IFLA_MACSEC_VALIDATION] &&
3996 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3999 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
4000 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
4001 !data[IFLA_MACSEC_WINDOW])
4007 static struct net *macsec_get_link_net(const struct net_device *dev)
4009 return dev_net(macsec_priv(dev)->real_dev);
4012 static size_t macsec_get_size(const struct net_device *dev)
4014 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
4015 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
4016 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
4017 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
4018 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
4019 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
4020 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
4021 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
4022 nla_total_size(1) + /* IFLA_MACSEC_ES */
4023 nla_total_size(1) + /* IFLA_MACSEC_SCB */
4024 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
4025 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
4029 static int macsec_fill_info(struct sk_buff *skb,
4030 const struct net_device *dev)
4032 struct macsec_secy *secy = &macsec_priv(dev)->secy;
4033 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
4036 switch (secy->key_len) {
4037 case MACSEC_GCM_AES_128_SAK_LEN:
4038 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
4040 case MACSEC_GCM_AES_256_SAK_LEN:
4041 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
4044 goto nla_put_failure;
4047 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
4049 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
4050 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
4051 csid, IFLA_MACSEC_PAD) ||
4052 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
4053 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
4054 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
4055 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
4056 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
4057 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
4058 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
4059 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
4061 goto nla_put_failure;
4063 if (secy->replay_protect) {
4064 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
4065 goto nla_put_failure;
4074 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
4076 .priv_size = sizeof(struct macsec_dev),
4077 .maxtype = IFLA_MACSEC_MAX,
4078 .policy = macsec_rtnl_policy,
4079 .setup = macsec_setup,
4080 .validate = macsec_validate_attr,
4081 .newlink = macsec_newlink,
4082 .changelink = macsec_changelink,
4083 .dellink = macsec_dellink,
4084 .get_size = macsec_get_size,
4085 .fill_info = macsec_fill_info,
4086 .get_link_net = macsec_get_link_net,
4089 static bool is_macsec_master(struct net_device *dev)
4091 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
4094 static int macsec_notify(struct notifier_block *this, unsigned long event,
4097 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
4100 if (!is_macsec_master(real_dev))
4106 case NETDEV_CHANGE: {
4107 struct macsec_dev *m, *n;
4108 struct macsec_rxh_data *rxd;
4110 rxd = macsec_data_rtnl(real_dev);
4111 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4112 struct net_device *dev = m->secy.netdev;
4114 netif_stacked_transfer_operstate(real_dev, dev);
4118 case NETDEV_UNREGISTER: {
4119 struct macsec_dev *m, *n;
4120 struct macsec_rxh_data *rxd;
4122 rxd = macsec_data_rtnl(real_dev);
4123 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4124 macsec_common_dellink(m->secy.netdev, &head);
4127 netdev_rx_handler_unregister(real_dev);
4130 unregister_netdevice_many(&head);
4133 case NETDEV_CHANGEMTU: {
4134 struct macsec_dev *m;
4135 struct macsec_rxh_data *rxd;
4137 rxd = macsec_data_rtnl(real_dev);
4138 list_for_each_entry(m, &rxd->secys, secys) {
4139 struct net_device *dev = m->secy.netdev;
4140 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
4141 macsec_extra_len(true));
4144 dev_set_mtu(dev, mtu);
4152 static struct notifier_block macsec_notifier = {
4153 .notifier_call = macsec_notify,
4156 static int __init macsec_init(void)
4160 pr_info("MACsec IEEE 802.1AE\n");
4161 err = register_netdevice_notifier(&macsec_notifier);
4165 err = rtnl_link_register(&macsec_link_ops);
4169 err = genl_register_family(&macsec_fam);
4176 rtnl_link_unregister(&macsec_link_ops);
4178 unregister_netdevice_notifier(&macsec_notifier);
4182 static void __exit macsec_exit(void)
4184 genl_unregister_family(&macsec_fam);
4185 rtnl_link_unregister(&macsec_link_ops);
4186 unregister_netdevice_notifier(&macsec_notifier);
4190 module_init(macsec_init);
4191 module_exit(macsec_exit);
4193 MODULE_ALIAS_RTNL_LINK("macsec");
4194 MODULE_ALIAS_GENL_FAMILY("macsec");
4196 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
4197 MODULE_LICENSE("GPL v2");