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_MAC ||
343 macsec->offload == MACSEC_OFFLOAD_PHY)
349 /* Checks if underlying layers implement MACsec offloading functions. */
350 static bool macsec_check_offload(enum macsec_offload offload,
351 struct macsec_dev *macsec)
353 if (!macsec || !macsec->real_dev)
356 if (offload == MACSEC_OFFLOAD_PHY)
357 return macsec->real_dev->phydev &&
358 macsec->real_dev->phydev->macsec_ops;
359 else if (offload == MACSEC_OFFLOAD_MAC)
360 return macsec->real_dev->features & NETIF_F_HW_MACSEC &&
361 macsec->real_dev->macsec_ops;
366 static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload,
367 struct macsec_dev *macsec,
368 struct macsec_context *ctx)
371 memset(ctx, 0, sizeof(*ctx));
372 ctx->offload = offload;
374 if (offload == MACSEC_OFFLOAD_PHY)
375 ctx->phydev = macsec->real_dev->phydev;
376 else if (offload == MACSEC_OFFLOAD_MAC)
377 ctx->netdev = macsec->real_dev;
380 if (offload == MACSEC_OFFLOAD_PHY)
381 return macsec->real_dev->phydev->macsec_ops;
383 return macsec->real_dev->macsec_ops;
386 /* Returns a pointer to the MACsec ops struct if any and updates the MACsec
387 * context device reference if provided.
389 static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec,
390 struct macsec_context *ctx)
392 if (!macsec_check_offload(macsec->offload, macsec))
395 return __macsec_get_ops(macsec->offload, macsec, ctx);
398 /* validate MACsec packet according to IEEE 802.1AE-2018 9.12 */
399 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len, bool xpn)
401 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
402 int len = skb->len - 2 * ETH_ALEN;
403 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
405 /* a) It comprises at least 17 octets */
409 /* b) MACsec EtherType: already checked */
411 /* c) V bit is clear */
412 if (h->tci_an & MACSEC_TCI_VERSION)
415 /* d) ES or SCB => !SC */
416 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
417 (h->tci_an & MACSEC_TCI_SC))
420 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
424 /* rx.pn != 0 if not XPN (figure 10-5 with 802.11AEbw-2013 amendment) */
425 if (!h->packet_number && !xpn)
428 /* length check, f) g) h) i) */
430 return len == extra_len + h->short_length;
431 return len >= extra_len + MIN_NON_SHORT_LEN;
434 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
435 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
437 static void macsec_fill_iv_xpn(unsigned char *iv, ssci_t ssci, u64 pn,
440 struct gcm_iv_xpn *gcm_iv = (struct gcm_iv_xpn *)iv;
442 gcm_iv->ssci = ssci ^ salt.ssci;
443 gcm_iv->pn = cpu_to_be64(pn) ^ salt.pn;
446 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
448 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
451 gcm_iv->pn = htonl(pn);
454 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
456 return (struct macsec_eth_header *)skb_mac_header(skb);
459 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
461 return make_sci(dev->dev_addr, port);
464 static void __macsec_pn_wrapped(struct macsec_secy *secy,
465 struct macsec_tx_sa *tx_sa)
467 pr_debug("PN wrapped, transitioning to !oper\n");
468 tx_sa->active = false;
469 if (secy->protect_frames)
470 secy->operational = false;
473 void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa)
475 spin_lock_bh(&tx_sa->lock);
476 __macsec_pn_wrapped(secy, tx_sa);
477 spin_unlock_bh(&tx_sa->lock);
479 EXPORT_SYMBOL_GPL(macsec_pn_wrapped);
481 static pn_t tx_sa_update_pn(struct macsec_tx_sa *tx_sa,
482 struct macsec_secy *secy)
486 spin_lock_bh(&tx_sa->lock);
488 pn = tx_sa->next_pn_halves;
492 tx_sa->next_pn_halves.lower++;
494 if (tx_sa->next_pn == 0)
495 __macsec_pn_wrapped(secy, tx_sa);
496 spin_unlock_bh(&tx_sa->lock);
501 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
503 struct macsec_dev *macsec = netdev_priv(dev);
505 skb->dev = macsec->real_dev;
506 skb_reset_mac_header(skb);
507 skb->protocol = eth_hdr(skb)->h_proto;
510 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
511 struct macsec_tx_sa *tx_sa)
513 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
515 u64_stats_update_begin(&txsc_stats->syncp);
516 if (tx_sc->encrypt) {
517 txsc_stats->stats.OutOctetsEncrypted += skb->len;
518 txsc_stats->stats.OutPktsEncrypted++;
519 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
521 txsc_stats->stats.OutOctetsProtected += skb->len;
522 txsc_stats->stats.OutPktsProtected++;
523 this_cpu_inc(tx_sa->stats->OutPktsProtected);
525 u64_stats_update_end(&txsc_stats->syncp);
528 static void count_tx(struct net_device *dev, int ret, int len)
530 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
531 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
533 u64_stats_update_begin(&stats->syncp);
535 stats->tx_bytes += len;
536 u64_stats_update_end(&stats->syncp);
540 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
542 struct sk_buff *skb = base->data;
543 struct net_device *dev = skb->dev;
544 struct macsec_dev *macsec = macsec_priv(dev);
545 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
548 aead_request_free(macsec_skb_cb(skb)->req);
551 macsec_encrypt_finish(skb, dev);
552 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
554 ret = dev_queue_xmit(skb);
555 count_tx(dev, ret, len);
556 rcu_read_unlock_bh();
562 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
564 struct scatterlist **sg,
567 size_t size, iv_offset, sg_offset;
568 struct aead_request *req;
571 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
573 size += GCM_AES_IV_LEN;
575 size = ALIGN(size, __alignof__(struct scatterlist));
577 size += sizeof(struct scatterlist) * num_frags;
579 tmp = kmalloc(size, GFP_ATOMIC);
583 *iv = (unsigned char *)(tmp + iv_offset);
584 *sg = (struct scatterlist *)(tmp + sg_offset);
587 aead_request_set_tfm(req, tfm);
592 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
593 struct net_device *dev)
596 struct scatterlist *sg;
597 struct sk_buff *trailer;
600 struct macsec_eth_header *hh;
601 size_t unprotected_len;
602 struct aead_request *req;
603 struct macsec_secy *secy;
604 struct macsec_tx_sc *tx_sc;
605 struct macsec_tx_sa *tx_sa;
606 struct macsec_dev *macsec = macsec_priv(dev);
610 secy = &macsec->secy;
611 tx_sc = &secy->tx_sc;
613 /* 10.5.1 TX SA assignment */
614 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
616 secy->operational = false;
618 return ERR_PTR(-EINVAL);
621 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
622 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
623 struct sk_buff *nskb = skb_copy_expand(skb,
624 MACSEC_NEEDED_HEADROOM,
625 MACSEC_NEEDED_TAILROOM,
631 macsec_txsa_put(tx_sa);
633 return ERR_PTR(-ENOMEM);
636 skb = skb_unshare(skb, GFP_ATOMIC);
638 macsec_txsa_put(tx_sa);
639 return ERR_PTR(-ENOMEM);
643 unprotected_len = skb->len;
645 sci_present = send_sci(secy);
646 hh = skb_push(skb, macsec_extra_len(sci_present));
647 memmove(hh, eth, 2 * ETH_ALEN);
649 pn = tx_sa_update_pn(tx_sa, secy);
650 if (pn.full64 == 0) {
651 macsec_txsa_put(tx_sa);
653 return ERR_PTR(-ENOLINK);
655 macsec_fill_sectag(hh, secy, pn.lower, sci_present);
656 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
658 skb_put(skb, secy->icv_len);
660 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
661 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
663 u64_stats_update_begin(&secy_stats->syncp);
664 secy_stats->stats.OutPktsTooLong++;
665 u64_stats_update_end(&secy_stats->syncp);
667 macsec_txsa_put(tx_sa);
669 return ERR_PTR(-EINVAL);
672 ret = skb_cow_data(skb, 0, &trailer);
673 if (unlikely(ret < 0)) {
674 macsec_txsa_put(tx_sa);
679 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
681 macsec_txsa_put(tx_sa);
683 return ERR_PTR(-ENOMEM);
687 macsec_fill_iv_xpn(iv, tx_sa->ssci, pn.full64, tx_sa->key.salt);
689 macsec_fill_iv(iv, secy->sci, pn.lower);
691 sg_init_table(sg, ret);
692 ret = skb_to_sgvec(skb, sg, 0, skb->len);
693 if (unlikely(ret < 0)) {
694 aead_request_free(req);
695 macsec_txsa_put(tx_sa);
700 if (tx_sc->encrypt) {
701 int len = skb->len - macsec_hdr_len(sci_present) -
703 aead_request_set_crypt(req, sg, sg, len, iv);
704 aead_request_set_ad(req, macsec_hdr_len(sci_present));
706 aead_request_set_crypt(req, sg, sg, 0, iv);
707 aead_request_set_ad(req, skb->len - secy->icv_len);
710 macsec_skb_cb(skb)->req = req;
711 macsec_skb_cb(skb)->tx_sa = tx_sa;
712 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
715 ret = crypto_aead_encrypt(req);
716 if (ret == -EINPROGRESS) {
718 } else if (ret != 0) {
721 aead_request_free(req);
722 macsec_txsa_put(tx_sa);
723 return ERR_PTR(-EINVAL);
727 aead_request_free(req);
728 macsec_txsa_put(tx_sa);
733 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
735 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
736 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
737 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
740 spin_lock(&rx_sa->lock);
741 if (rx_sa->next_pn_halves.lower >= secy->replay_window)
742 lowest_pn = rx_sa->next_pn_halves.lower - secy->replay_window;
744 /* Now perform replay protection check again
745 * (see IEEE 802.1AE-2006 figure 10-5)
747 if (secy->replay_protect && pn < lowest_pn &&
748 (!secy->xpn || pn_same_half(pn, lowest_pn))) {
749 spin_unlock(&rx_sa->lock);
750 u64_stats_update_begin(&rxsc_stats->syncp);
751 rxsc_stats->stats.InPktsLate++;
752 u64_stats_update_end(&rxsc_stats->syncp);
756 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
757 u64_stats_update_begin(&rxsc_stats->syncp);
758 if (hdr->tci_an & MACSEC_TCI_E)
759 rxsc_stats->stats.InOctetsDecrypted += skb->len;
761 rxsc_stats->stats.InOctetsValidated += skb->len;
762 u64_stats_update_end(&rxsc_stats->syncp);
765 if (!macsec_skb_cb(skb)->valid) {
766 spin_unlock(&rx_sa->lock);
769 if (hdr->tci_an & MACSEC_TCI_C ||
770 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
771 u64_stats_update_begin(&rxsc_stats->syncp);
772 rxsc_stats->stats.InPktsNotValid++;
773 u64_stats_update_end(&rxsc_stats->syncp);
777 u64_stats_update_begin(&rxsc_stats->syncp);
778 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
779 rxsc_stats->stats.InPktsInvalid++;
780 this_cpu_inc(rx_sa->stats->InPktsInvalid);
781 } else if (pn < lowest_pn) {
782 rxsc_stats->stats.InPktsDelayed++;
784 rxsc_stats->stats.InPktsUnchecked++;
786 u64_stats_update_end(&rxsc_stats->syncp);
788 u64_stats_update_begin(&rxsc_stats->syncp);
789 if (pn < lowest_pn) {
790 rxsc_stats->stats.InPktsDelayed++;
792 rxsc_stats->stats.InPktsOK++;
793 this_cpu_inc(rx_sa->stats->InPktsOK);
795 u64_stats_update_end(&rxsc_stats->syncp);
797 // Instead of "pn >=" - to support pn overflow in xpn
798 if (pn + 1 > rx_sa->next_pn_halves.lower) {
799 rx_sa->next_pn_halves.lower = pn + 1;
800 } else if (secy->xpn &&
801 !pn_same_half(pn, rx_sa->next_pn_halves.lower)) {
802 rx_sa->next_pn_halves.upper++;
803 rx_sa->next_pn_halves.lower = pn + 1;
806 spin_unlock(&rx_sa->lock);
812 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
814 skb->pkt_type = PACKET_HOST;
815 skb->protocol = eth_type_trans(skb, dev);
817 skb_reset_network_header(skb);
818 if (!skb_transport_header_was_set(skb))
819 skb_reset_transport_header(skb);
820 skb_reset_mac_len(skb);
823 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
825 skb->ip_summed = CHECKSUM_NONE;
826 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
827 skb_pull(skb, hdr_len);
828 pskb_trim_unique(skb, skb->len - icv_len);
831 static void count_rx(struct net_device *dev, int len)
833 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
835 u64_stats_update_begin(&stats->syncp);
837 stats->rx_bytes += len;
838 u64_stats_update_end(&stats->syncp);
841 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
843 struct sk_buff *skb = base->data;
844 struct net_device *dev = skb->dev;
845 struct macsec_dev *macsec = macsec_priv(dev);
846 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
847 struct macsec_rx_sc *rx_sc = rx_sa->sc;
851 aead_request_free(macsec_skb_cb(skb)->req);
854 macsec_skb_cb(skb)->valid = true;
857 pn = ntohl(macsec_ethhdr(skb)->packet_number);
858 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
859 rcu_read_unlock_bh();
864 macsec_finalize_skb(skb, macsec->secy.icv_len,
865 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
866 macsec_reset_skb(skb, macsec->secy.netdev);
869 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
872 rcu_read_unlock_bh();
875 macsec_rxsa_put(rx_sa);
876 macsec_rxsc_put(rx_sc);
880 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
881 struct net_device *dev,
882 struct macsec_rx_sa *rx_sa,
884 struct macsec_secy *secy)
887 struct scatterlist *sg;
888 struct sk_buff *trailer;
890 struct aead_request *req;
891 struct macsec_eth_header *hdr;
893 u16 icv_len = secy->icv_len;
895 macsec_skb_cb(skb)->valid = false;
896 skb = skb_share_check(skb, GFP_ATOMIC);
898 return ERR_PTR(-ENOMEM);
900 ret = skb_cow_data(skb, 0, &trailer);
901 if (unlikely(ret < 0)) {
905 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
908 return ERR_PTR(-ENOMEM);
911 hdr = (struct macsec_eth_header *)skb->data;
912 hdr_pn = ntohl(hdr->packet_number);
915 pn_t recovered_pn = rx_sa->next_pn_halves;
917 recovered_pn.lower = hdr_pn;
918 if (hdr_pn < rx_sa->next_pn_halves.lower &&
919 !pn_same_half(hdr_pn, rx_sa->next_pn_halves.lower))
920 recovered_pn.upper++;
922 macsec_fill_iv_xpn(iv, rx_sa->ssci, recovered_pn.full64,
925 macsec_fill_iv(iv, sci, hdr_pn);
928 sg_init_table(sg, ret);
929 ret = skb_to_sgvec(skb, sg, 0, skb->len);
930 if (unlikely(ret < 0)) {
931 aead_request_free(req);
936 if (hdr->tci_an & MACSEC_TCI_E) {
937 /* confidentiality: ethernet + macsec header
938 * authenticated, encrypted payload
940 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
942 aead_request_set_crypt(req, sg, sg, len, iv);
943 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
944 skb = skb_unshare(skb, GFP_ATOMIC);
946 aead_request_free(req);
947 return ERR_PTR(-ENOMEM);
950 /* integrity only: all headers + data authenticated */
951 aead_request_set_crypt(req, sg, sg, icv_len, iv);
952 aead_request_set_ad(req, skb->len - icv_len);
955 macsec_skb_cb(skb)->req = req;
957 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
960 ret = crypto_aead_decrypt(req);
961 if (ret == -EINPROGRESS) {
963 } else if (ret != 0) {
964 /* decryption/authentication failed
965 * 10.6 if validateFrames is disabled, deliver anyway
967 if (ret != -EBADMSG) {
972 macsec_skb_cb(skb)->valid = true;
976 aead_request_free(req);
981 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
983 struct macsec_rx_sc *rx_sc;
985 for_each_rxsc(secy, rx_sc) {
986 if (rx_sc->sci == sci)
993 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
995 struct macsec_rx_sc *rx_sc;
997 for_each_rxsc_rtnl(secy, rx_sc) {
998 if (rx_sc->sci == sci)
1005 static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
1007 /* Deliver to the uncontrolled port by default */
1008 enum rx_handler_result ret = RX_HANDLER_PASS;
1009 struct macsec_rxh_data *rxd;
1010 struct macsec_dev *macsec;
1013 rxd = macsec_data_rcu(skb->dev);
1015 /* 10.6 If the management control validateFrames is not
1016 * Strict, frames without a SecTAG are received, counted, and
1017 * delivered to the Controlled Port
1019 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1020 struct sk_buff *nskb;
1021 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1023 if (!macsec_is_offloaded(macsec) &&
1024 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1025 u64_stats_update_begin(&secy_stats->syncp);
1026 secy_stats->stats.InPktsNoTag++;
1027 u64_stats_update_end(&secy_stats->syncp);
1031 /* deliver on this port */
1032 nskb = skb_clone(skb, GFP_ATOMIC);
1036 nskb->dev = macsec->secy.netdev;
1038 if (netif_rx(nskb) == NET_RX_SUCCESS) {
1039 u64_stats_update_begin(&secy_stats->syncp);
1040 secy_stats->stats.InPktsUntagged++;
1041 u64_stats_update_end(&secy_stats->syncp);
1044 if (netif_running(macsec->secy.netdev) &&
1045 macsec_is_offloaded(macsec)) {
1046 ret = RX_HANDLER_EXACT;
1056 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1058 struct sk_buff *skb = *pskb;
1059 struct net_device *dev = skb->dev;
1060 struct macsec_eth_header *hdr;
1061 struct macsec_secy *secy = NULL;
1062 struct macsec_rx_sc *rx_sc;
1063 struct macsec_rx_sa *rx_sa;
1064 struct macsec_rxh_data *rxd;
1065 struct macsec_dev *macsec;
1069 struct pcpu_rx_sc_stats *rxsc_stats;
1070 struct pcpu_secy_stats *secy_stats;
1074 if (skb_headroom(skb) < ETH_HLEN)
1077 hdr = macsec_ethhdr(skb);
1078 if (hdr->eth.h_proto != htons(ETH_P_MACSEC))
1079 return handle_not_macsec(skb);
1081 skb = skb_unshare(skb, GFP_ATOMIC);
1084 return RX_HANDLER_CONSUMED;
1086 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1088 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1092 hdr = macsec_ethhdr(skb);
1094 /* Frames with a SecTAG that has the TCI E bit set but the C
1095 * bit clear are discarded, as this reserved encoding is used
1096 * to identify frames with a SecTAG that are not to be
1097 * delivered to the Controlled Port.
1099 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1100 return RX_HANDLER_PASS;
1102 /* now, pull the extra length */
1103 if (hdr->tci_an & MACSEC_TCI_SC) {
1108 /* ethernet header is part of crypto processing */
1109 skb_push(skb, ETH_HLEN);
1111 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1112 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1113 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1116 rxd = macsec_data_rcu(skb->dev);
1118 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1119 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1121 sc = sc ? macsec_rxsc_get(sc) : NULL;
1124 secy = &macsec->secy;
1134 macsec = macsec_priv(dev);
1135 secy_stats = this_cpu_ptr(macsec->stats);
1136 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1138 if (!macsec_validate_skb(skb, secy->icv_len, secy->xpn)) {
1139 u64_stats_update_begin(&secy_stats->syncp);
1140 secy_stats->stats.InPktsBadTag++;
1141 u64_stats_update_end(&secy_stats->syncp);
1145 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1147 /* 10.6.1 if the SA is not in use */
1149 /* If validateFrames is Strict or the C bit in the
1150 * SecTAG is set, discard
1152 if (hdr->tci_an & MACSEC_TCI_C ||
1153 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1154 u64_stats_update_begin(&rxsc_stats->syncp);
1155 rxsc_stats->stats.InPktsNotUsingSA++;
1156 u64_stats_update_end(&rxsc_stats->syncp);
1160 /* not Strict, the frame (with the SecTAG and ICV
1161 * removed) is delivered to the Controlled Port.
1163 u64_stats_update_begin(&rxsc_stats->syncp);
1164 rxsc_stats->stats.InPktsUnusedSA++;
1165 u64_stats_update_end(&rxsc_stats->syncp);
1169 /* First, PN check to avoid decrypting obviously wrong packets */
1170 hdr_pn = ntohl(hdr->packet_number);
1171 if (secy->replay_protect) {
1174 spin_lock(&rx_sa->lock);
1175 late = rx_sa->next_pn_halves.lower >= secy->replay_window &&
1176 hdr_pn < (rx_sa->next_pn_halves.lower - secy->replay_window);
1179 late = late && pn_same_half(rx_sa->next_pn_halves.lower, hdr_pn);
1180 spin_unlock(&rx_sa->lock);
1183 u64_stats_update_begin(&rxsc_stats->syncp);
1184 rxsc_stats->stats.InPktsLate++;
1185 u64_stats_update_end(&rxsc_stats->syncp);
1190 macsec_skb_cb(skb)->rx_sa = rx_sa;
1192 /* Disabled && !changed text => skip validation */
1193 if (hdr->tci_an & MACSEC_TCI_C ||
1194 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1195 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1198 /* the decrypt callback needs the reference */
1199 if (PTR_ERR(skb) != -EINPROGRESS) {
1200 macsec_rxsa_put(rx_sa);
1201 macsec_rxsc_put(rx_sc);
1205 return RX_HANDLER_CONSUMED;
1208 if (!macsec_post_decrypt(skb, secy, hdr_pn))
1212 macsec_finalize_skb(skb, secy->icv_len,
1213 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1214 macsec_reset_skb(skb, secy->netdev);
1217 macsec_rxsa_put(rx_sa);
1218 macsec_rxsc_put(rx_sc);
1221 ret = gro_cells_receive(&macsec->gro_cells, skb);
1222 if (ret == NET_RX_SUCCESS)
1223 count_rx(dev, skb->len);
1225 macsec->secy.netdev->stats.rx_dropped++;
1230 return RX_HANDLER_CONSUMED;
1233 macsec_rxsa_put(rx_sa);
1235 macsec_rxsc_put(rx_sc);
1240 return RX_HANDLER_CONSUMED;
1243 /* 10.6.1 if the SC is not found */
1244 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1246 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1247 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1249 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1250 struct sk_buff *nskb;
1252 secy_stats = this_cpu_ptr(macsec->stats);
1254 /* If validateFrames is Strict or the C bit in the
1255 * SecTAG is set, discard
1258 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1259 u64_stats_update_begin(&secy_stats->syncp);
1260 secy_stats->stats.InPktsNoSCI++;
1261 u64_stats_update_end(&secy_stats->syncp);
1265 /* not strict, the frame (with the SecTAG and ICV
1266 * removed) is delivered to the Controlled Port.
1268 nskb = skb_clone(skb, GFP_ATOMIC);
1272 macsec_reset_skb(nskb, macsec->secy.netdev);
1274 ret = netif_rx(nskb);
1275 if (ret == NET_RX_SUCCESS) {
1276 u64_stats_update_begin(&secy_stats->syncp);
1277 secy_stats->stats.InPktsUnknownSCI++;
1278 u64_stats_update_end(&secy_stats->syncp);
1280 macsec->secy.netdev->stats.rx_dropped++;
1286 return RX_HANDLER_PASS;
1289 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1291 struct crypto_aead *tfm;
1294 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
1299 ret = crypto_aead_setkey(tfm, key, key_len);
1303 ret = crypto_aead_setauthsize(tfm, icv_len);
1309 crypto_free_aead(tfm);
1310 return ERR_PTR(ret);
1313 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1316 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1320 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1321 if (IS_ERR(rx_sa->key.tfm)) {
1322 free_percpu(rx_sa->stats);
1323 return PTR_ERR(rx_sa->key.tfm);
1326 rx_sa->ssci = MACSEC_UNDEF_SSCI;
1327 rx_sa->active = false;
1329 refcount_set(&rx_sa->refcnt, 1);
1330 spin_lock_init(&rx_sa->lock);
1335 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1337 rx_sa->active = false;
1339 macsec_rxsa_put(rx_sa);
1342 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1346 for (i = 0; i < MACSEC_NUM_AN; i++) {
1347 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1349 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1354 macsec_rxsc_put(rx_sc);
1357 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1359 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1361 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1363 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1364 if (rx_sc->sci == sci) {
1367 rcu_assign_pointer(*rx_scp, rx_sc->next);
1375 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1377 struct macsec_rx_sc *rx_sc;
1378 struct macsec_dev *macsec;
1379 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1380 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1381 struct macsec_secy *secy;
1383 list_for_each_entry(macsec, &rxd->secys, secys) {
1384 if (find_rx_sc_rtnl(&macsec->secy, sci))
1385 return ERR_PTR(-EEXIST);
1388 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1390 return ERR_PTR(-ENOMEM);
1392 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1393 if (!rx_sc->stats) {
1395 return ERR_PTR(-ENOMEM);
1399 rx_sc->active = true;
1400 refcount_set(&rx_sc->refcnt, 1);
1402 secy = &macsec_priv(dev)->secy;
1403 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1404 rcu_assign_pointer(secy->rx_sc, rx_sc);
1412 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1415 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1419 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1420 if (IS_ERR(tx_sa->key.tfm)) {
1421 free_percpu(tx_sa->stats);
1422 return PTR_ERR(tx_sa->key.tfm);
1425 tx_sa->ssci = MACSEC_UNDEF_SSCI;
1426 tx_sa->active = false;
1427 refcount_set(&tx_sa->refcnt, 1);
1428 spin_lock_init(&tx_sa->lock);
1433 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1435 tx_sa->active = false;
1437 macsec_txsa_put(tx_sa);
1440 static struct genl_family macsec_fam;
1442 static struct net_device *get_dev_from_nl(struct net *net,
1443 struct nlattr **attrs)
1445 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1446 struct net_device *dev;
1448 dev = __dev_get_by_index(net, ifindex);
1450 return ERR_PTR(-ENODEV);
1452 if (!netif_is_macsec(dev))
1453 return ERR_PTR(-ENODEV);
1458 static sci_t nla_get_sci(const struct nlattr *nla)
1460 return (__force sci_t)nla_get_u64(nla);
1463 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1466 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1469 static ssci_t nla_get_ssci(const struct nlattr *nla)
1471 return (__force ssci_t)nla_get_u32(nla);
1474 static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value)
1476 return nla_put_u32(skb, attrtype, (__force u64)value);
1479 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1480 struct nlattr **attrs,
1481 struct nlattr **tb_sa,
1482 struct net_device **devp,
1483 struct macsec_secy **secyp,
1484 struct macsec_tx_sc **scp,
1487 struct net_device *dev;
1488 struct macsec_secy *secy;
1489 struct macsec_tx_sc *tx_sc;
1490 struct macsec_tx_sa *tx_sa;
1492 if (!tb_sa[MACSEC_SA_ATTR_AN])
1493 return ERR_PTR(-EINVAL);
1495 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1497 dev = get_dev_from_nl(net, attrs);
1499 return ERR_CAST(dev);
1501 if (*assoc_num >= MACSEC_NUM_AN)
1502 return ERR_PTR(-EINVAL);
1504 secy = &macsec_priv(dev)->secy;
1505 tx_sc = &secy->tx_sc;
1507 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1509 return ERR_PTR(-ENODEV);
1517 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1518 struct nlattr **attrs,
1519 struct nlattr **tb_rxsc,
1520 struct net_device **devp,
1521 struct macsec_secy **secyp)
1523 struct net_device *dev;
1524 struct macsec_secy *secy;
1525 struct macsec_rx_sc *rx_sc;
1528 dev = get_dev_from_nl(net, attrs);
1530 return ERR_CAST(dev);
1532 secy = &macsec_priv(dev)->secy;
1534 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1535 return ERR_PTR(-EINVAL);
1537 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1538 rx_sc = find_rx_sc_rtnl(secy, sci);
1540 return ERR_PTR(-ENODEV);
1548 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1549 struct nlattr **attrs,
1550 struct nlattr **tb_rxsc,
1551 struct nlattr **tb_sa,
1552 struct net_device **devp,
1553 struct macsec_secy **secyp,
1554 struct macsec_rx_sc **scp,
1557 struct macsec_rx_sc *rx_sc;
1558 struct macsec_rx_sa *rx_sa;
1560 if (!tb_sa[MACSEC_SA_ATTR_AN])
1561 return ERR_PTR(-EINVAL);
1563 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1564 if (*assoc_num >= MACSEC_NUM_AN)
1565 return ERR_PTR(-EINVAL);
1567 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1569 return ERR_CAST(rx_sc);
1571 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1573 return ERR_PTR(-ENODEV);
1579 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1580 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1581 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1582 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1583 [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
1586 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1587 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1588 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1591 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1592 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1593 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1594 [MACSEC_SA_ATTR_PN] = { .type = NLA_MIN_LEN, .len = 4 },
1595 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1596 .len = MACSEC_KEYID_LEN, },
1597 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1598 .len = MACSEC_MAX_KEY_LEN, },
1599 [MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 },
1600 [MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY,
1601 .len = MACSEC_SALT_LEN, },
1604 static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
1605 [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 },
1608 /* Offloads an operation to a device driver */
1609 static int macsec_offload(int (* const func)(struct macsec_context *),
1610 struct macsec_context *ctx)
1614 if (unlikely(!func))
1617 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1618 mutex_lock(&ctx->phydev->lock);
1620 /* Phase I: prepare. The drive should fail here if there are going to be
1621 * issues in the commit phase.
1623 ctx->prepare = true;
1628 /* Phase II: commit. This step cannot fail. */
1629 ctx->prepare = false;
1631 /* This should never happen: commit is not allowed to fail */
1633 WARN(1, "MACsec offloading commit failed (%d)\n", ret);
1636 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1637 mutex_unlock(&ctx->phydev->lock);
1642 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1644 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1647 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1653 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1655 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1658 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1664 static bool validate_add_rxsa(struct nlattr **attrs)
1666 if (!attrs[MACSEC_SA_ATTR_AN] ||
1667 !attrs[MACSEC_SA_ATTR_KEY] ||
1668 !attrs[MACSEC_SA_ATTR_KEYID])
1671 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1674 if (attrs[MACSEC_SA_ATTR_PN] &&
1675 *(u64 *)nla_data(attrs[MACSEC_SA_ATTR_PN]) == 0)
1678 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1679 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1683 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1689 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1691 struct net_device *dev;
1692 struct nlattr **attrs = info->attrs;
1693 struct macsec_secy *secy;
1694 struct macsec_rx_sc *rx_sc;
1695 struct macsec_rx_sa *rx_sa;
1696 unsigned char assoc_num;
1698 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1699 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1702 if (!attrs[MACSEC_ATTR_IFINDEX])
1705 if (parse_sa_config(attrs, tb_sa))
1708 if (parse_rxsc_config(attrs, tb_rxsc))
1711 if (!validate_add_rxsa(tb_sa))
1715 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1716 if (IS_ERR(rx_sc)) {
1718 return PTR_ERR(rx_sc);
1721 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1723 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1724 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1725 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1730 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1731 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1732 pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
1733 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1739 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1744 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
1745 pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n",
1746 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
1747 MACSEC_SA_ATTR_SALT);
1753 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1759 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1765 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1766 secy->key_len, secy->icv_len);
1773 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1774 spin_lock_bh(&rx_sa->lock);
1775 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
1776 spin_unlock_bh(&rx_sa->lock);
1779 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1780 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1784 /* If h/w offloading is available, propagate to the device */
1785 if (macsec_is_offloaded(netdev_priv(dev))) {
1786 const struct macsec_ops *ops;
1787 struct macsec_context ctx;
1789 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1795 ctx.sa.assoc_num = assoc_num;
1796 ctx.sa.rx_sa = rx_sa;
1797 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1800 err = macsec_offload(ops->mdo_add_rxsa, &ctx);
1806 rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
1807 nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
1811 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1812 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1824 static bool validate_add_rxsc(struct nlattr **attrs)
1826 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1829 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1830 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1837 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1839 struct net_device *dev;
1840 sci_t sci = MACSEC_UNDEF_SCI;
1841 struct nlattr **attrs = info->attrs;
1842 struct macsec_rx_sc *rx_sc;
1843 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1847 if (!attrs[MACSEC_ATTR_IFINDEX])
1850 if (parse_rxsc_config(attrs, tb_rxsc))
1853 if (!validate_add_rxsc(tb_rxsc))
1857 dev = get_dev_from_nl(genl_info_net(info), attrs);
1860 return PTR_ERR(dev);
1863 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1865 rx_sc = create_rx_sc(dev, sci);
1866 if (IS_ERR(rx_sc)) {
1868 return PTR_ERR(rx_sc);
1871 was_active = rx_sc->active;
1872 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1873 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1875 if (macsec_is_offloaded(netdev_priv(dev))) {
1876 const struct macsec_ops *ops;
1877 struct macsec_context ctx;
1879 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1887 ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
1897 rx_sc->active = was_active;
1902 static bool validate_add_txsa(struct nlattr **attrs)
1904 if (!attrs[MACSEC_SA_ATTR_AN] ||
1905 !attrs[MACSEC_SA_ATTR_PN] ||
1906 !attrs[MACSEC_SA_ATTR_KEY] ||
1907 !attrs[MACSEC_SA_ATTR_KEYID])
1910 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1913 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1916 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1917 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1921 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1927 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1929 struct net_device *dev;
1930 struct nlattr **attrs = info->attrs;
1931 struct macsec_secy *secy;
1932 struct macsec_tx_sc *tx_sc;
1933 struct macsec_tx_sa *tx_sa;
1934 unsigned char assoc_num;
1936 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1937 bool was_operational;
1940 if (!attrs[MACSEC_ATTR_IFINDEX])
1943 if (parse_sa_config(attrs, tb_sa))
1946 if (!validate_add_txsa(tb_sa))
1950 dev = get_dev_from_nl(genl_info_net(info), attrs);
1953 return PTR_ERR(dev);
1956 secy = &macsec_priv(dev)->secy;
1957 tx_sc = &secy->tx_sc;
1959 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1961 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1962 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1963 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1968 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1969 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1970 pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
1971 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1977 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1982 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
1983 pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n",
1984 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
1985 MACSEC_SA_ATTR_SALT);
1991 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1997 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
2003 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2004 secy->key_len, secy->icv_len);
2011 spin_lock_bh(&tx_sa->lock);
2012 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2013 spin_unlock_bh(&tx_sa->lock);
2015 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2016 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2018 was_operational = secy->operational;
2019 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
2020 secy->operational = true;
2022 /* If h/w offloading is available, propagate to the device */
2023 if (macsec_is_offloaded(netdev_priv(dev))) {
2024 const struct macsec_ops *ops;
2025 struct macsec_context ctx;
2027 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2033 ctx.sa.assoc_num = assoc_num;
2034 ctx.sa.tx_sa = tx_sa;
2035 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2038 err = macsec_offload(ops->mdo_add_txsa, &ctx);
2044 tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
2045 nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
2049 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
2050 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
2057 secy->operational = was_operational;
2063 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
2065 struct nlattr **attrs = info->attrs;
2066 struct net_device *dev;
2067 struct macsec_secy *secy;
2068 struct macsec_rx_sc *rx_sc;
2069 struct macsec_rx_sa *rx_sa;
2071 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2072 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2075 if (!attrs[MACSEC_ATTR_IFINDEX])
2078 if (parse_sa_config(attrs, tb_sa))
2081 if (parse_rxsc_config(attrs, tb_rxsc))
2085 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2086 &dev, &secy, &rx_sc, &assoc_num);
2087 if (IS_ERR(rx_sa)) {
2089 return PTR_ERR(rx_sa);
2092 if (rx_sa->active) {
2097 /* If h/w offloading is available, propagate to the device */
2098 if (macsec_is_offloaded(netdev_priv(dev))) {
2099 const struct macsec_ops *ops;
2100 struct macsec_context ctx;
2102 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2108 ctx.sa.assoc_num = assoc_num;
2109 ctx.sa.rx_sa = rx_sa;
2111 ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
2116 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
2128 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
2130 struct nlattr **attrs = info->attrs;
2131 struct net_device *dev;
2132 struct macsec_secy *secy;
2133 struct macsec_rx_sc *rx_sc;
2135 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2138 if (!attrs[MACSEC_ATTR_IFINDEX])
2141 if (parse_rxsc_config(attrs, tb_rxsc))
2144 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2148 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
2151 return PTR_ERR(dev);
2154 secy = &macsec_priv(dev)->secy;
2155 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2157 rx_sc = del_rx_sc(secy, sci);
2163 /* If h/w offloading is available, propagate to the device */
2164 if (macsec_is_offloaded(netdev_priv(dev))) {
2165 const struct macsec_ops *ops;
2166 struct macsec_context ctx;
2168 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2175 ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
2190 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2192 struct nlattr **attrs = info->attrs;
2193 struct net_device *dev;
2194 struct macsec_secy *secy;
2195 struct macsec_tx_sc *tx_sc;
2196 struct macsec_tx_sa *tx_sa;
2198 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2201 if (!attrs[MACSEC_ATTR_IFINDEX])
2204 if (parse_sa_config(attrs, tb_sa))
2208 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2209 &dev, &secy, &tx_sc, &assoc_num);
2210 if (IS_ERR(tx_sa)) {
2212 return PTR_ERR(tx_sa);
2215 if (tx_sa->active) {
2220 /* If h/w offloading is available, propagate to the device */
2221 if (macsec_is_offloaded(netdev_priv(dev))) {
2222 const struct macsec_ops *ops;
2223 struct macsec_context ctx;
2225 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2231 ctx.sa.assoc_num = assoc_num;
2232 ctx.sa.tx_sa = tx_sa;
2234 ret = macsec_offload(ops->mdo_del_txsa, &ctx);
2239 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2251 static bool validate_upd_sa(struct nlattr **attrs)
2253 if (!attrs[MACSEC_SA_ATTR_AN] ||
2254 attrs[MACSEC_SA_ATTR_KEY] ||
2255 attrs[MACSEC_SA_ATTR_KEYID] ||
2256 attrs[MACSEC_SA_ATTR_SSCI] ||
2257 attrs[MACSEC_SA_ATTR_SALT])
2260 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2263 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2266 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2267 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2274 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2276 struct nlattr **attrs = info->attrs;
2277 struct net_device *dev;
2278 struct macsec_secy *secy;
2279 struct macsec_tx_sc *tx_sc;
2280 struct macsec_tx_sa *tx_sa;
2282 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2283 bool was_operational, was_active;
2289 if (!attrs[MACSEC_ATTR_IFINDEX])
2292 if (parse_sa_config(attrs, tb_sa))
2295 if (!validate_upd_sa(tb_sa))
2299 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2300 &dev, &secy, &tx_sc, &assoc_num);
2301 if (IS_ERR(tx_sa)) {
2303 return PTR_ERR(tx_sa);
2306 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2309 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2310 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2311 pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
2312 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2317 spin_lock_bh(&tx_sa->lock);
2318 prev_pn = tx_sa->next_pn_halves;
2319 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2320 spin_unlock_bh(&tx_sa->lock);
2323 was_active = tx_sa->active;
2324 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2325 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2327 was_operational = secy->operational;
2328 if (assoc_num == tx_sc->encoding_sa)
2329 secy->operational = tx_sa->active;
2331 /* If h/w offloading is available, propagate to the device */
2332 if (macsec_is_offloaded(netdev_priv(dev))) {
2333 const struct macsec_ops *ops;
2334 struct macsec_context ctx;
2336 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2342 ctx.sa.assoc_num = assoc_num;
2343 ctx.sa.tx_sa = tx_sa;
2345 ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
2355 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2356 spin_lock_bh(&tx_sa->lock);
2357 tx_sa->next_pn_halves = prev_pn;
2358 spin_unlock_bh(&tx_sa->lock);
2360 tx_sa->active = was_active;
2361 secy->operational = was_operational;
2366 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2368 struct nlattr **attrs = info->attrs;
2369 struct net_device *dev;
2370 struct macsec_secy *secy;
2371 struct macsec_rx_sc *rx_sc;
2372 struct macsec_rx_sa *rx_sa;
2374 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2375 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2382 if (!attrs[MACSEC_ATTR_IFINDEX])
2385 if (parse_rxsc_config(attrs, tb_rxsc))
2388 if (parse_sa_config(attrs, tb_sa))
2391 if (!validate_upd_sa(tb_sa))
2395 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2396 &dev, &secy, &rx_sc, &assoc_num);
2397 if (IS_ERR(rx_sa)) {
2399 return PTR_ERR(rx_sa);
2402 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2405 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2406 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2407 pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
2408 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2413 spin_lock_bh(&rx_sa->lock);
2414 prev_pn = rx_sa->next_pn_halves;
2415 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2416 spin_unlock_bh(&rx_sa->lock);
2419 was_active = rx_sa->active;
2420 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2421 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2423 /* If h/w offloading is available, propagate to the device */
2424 if (macsec_is_offloaded(netdev_priv(dev))) {
2425 const struct macsec_ops *ops;
2426 struct macsec_context ctx;
2428 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2434 ctx.sa.assoc_num = assoc_num;
2435 ctx.sa.rx_sa = rx_sa;
2437 ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
2446 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2447 spin_lock_bh(&rx_sa->lock);
2448 rx_sa->next_pn_halves = prev_pn;
2449 spin_unlock_bh(&rx_sa->lock);
2451 rx_sa->active = was_active;
2456 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2458 struct nlattr **attrs = info->attrs;
2459 struct net_device *dev;
2460 struct macsec_secy *secy;
2461 struct macsec_rx_sc *rx_sc;
2462 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2463 unsigned int prev_n_rx_sc;
2467 if (!attrs[MACSEC_ATTR_IFINDEX])
2470 if (parse_rxsc_config(attrs, tb_rxsc))
2473 if (!validate_add_rxsc(tb_rxsc))
2477 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2478 if (IS_ERR(rx_sc)) {
2480 return PTR_ERR(rx_sc);
2483 was_active = rx_sc->active;
2484 prev_n_rx_sc = secy->n_rx_sc;
2485 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2486 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2488 if (rx_sc->active != new)
2489 secy->n_rx_sc += new ? 1 : -1;
2491 rx_sc->active = new;
2494 /* If h/w offloading is available, propagate to the device */
2495 if (macsec_is_offloaded(netdev_priv(dev))) {
2496 const struct macsec_ops *ops;
2497 struct macsec_context ctx;
2499 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2507 ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
2517 secy->n_rx_sc = prev_n_rx_sc;
2518 rx_sc->active = was_active;
2523 static bool macsec_is_configured(struct macsec_dev *macsec)
2525 struct macsec_secy *secy = &macsec->secy;
2526 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2529 if (secy->n_rx_sc > 0)
2532 for (i = 0; i < MACSEC_NUM_AN; i++)
2539 static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
2541 struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
2542 enum macsec_offload offload, prev_offload;
2543 int (*func)(struct macsec_context *ctx);
2544 struct nlattr **attrs = info->attrs;
2545 struct net_device *dev, *loop_dev;
2546 const struct macsec_ops *ops;
2547 struct macsec_context ctx;
2548 struct macsec_dev *macsec;
2549 struct net *loop_net;
2552 if (!attrs[MACSEC_ATTR_IFINDEX])
2555 if (!attrs[MACSEC_ATTR_OFFLOAD])
2558 if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
2559 attrs[MACSEC_ATTR_OFFLOAD],
2560 macsec_genl_offload_policy, NULL))
2563 dev = get_dev_from_nl(genl_info_net(info), attrs);
2565 return PTR_ERR(dev);
2566 macsec = macsec_priv(dev);
2568 offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
2569 if (macsec->offload == offload)
2572 /* Check if the offloading mode is supported by the underlying layers */
2573 if (offload != MACSEC_OFFLOAD_OFF &&
2574 !macsec_check_offload(offload, macsec))
2577 if (offload == MACSEC_OFFLOAD_OFF)
2578 goto skip_limitation;
2580 /* Check the physical interface isn't offloading another interface
2583 for_each_net(loop_net) {
2584 for_each_netdev(loop_net, loop_dev) {
2585 struct macsec_dev *priv;
2587 if (!netif_is_macsec(loop_dev))
2590 priv = macsec_priv(loop_dev);
2592 if (priv->real_dev == macsec->real_dev &&
2593 priv->offload != MACSEC_OFFLOAD_OFF)
2599 /* Check if the net device is busy. */
2600 if (netif_running(dev))
2605 prev_offload = macsec->offload;
2606 macsec->offload = offload;
2608 /* Check if the device already has rules configured: we do not support
2611 if (macsec_is_configured(macsec)) {
2616 ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
2623 if (prev_offload == MACSEC_OFFLOAD_OFF)
2624 func = ops->mdo_add_secy;
2626 func = ops->mdo_del_secy;
2628 ctx.secy = &macsec->secy;
2629 ret = macsec_offload(func, &ctx);
2637 macsec->offload = prev_offload;
2643 static int copy_tx_sa_stats(struct sk_buff *skb,
2644 struct macsec_tx_sa_stats __percpu *pstats)
2646 struct macsec_tx_sa_stats sum = {0, };
2649 for_each_possible_cpu(cpu) {
2650 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2652 sum.OutPktsProtected += stats->OutPktsProtected;
2653 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2656 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2657 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2663 static noinline_for_stack int
2664 copy_rx_sa_stats(struct sk_buff *skb,
2665 struct macsec_rx_sa_stats __percpu *pstats)
2667 struct macsec_rx_sa_stats sum = {0, };
2670 for_each_possible_cpu(cpu) {
2671 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2673 sum.InPktsOK += stats->InPktsOK;
2674 sum.InPktsInvalid += stats->InPktsInvalid;
2675 sum.InPktsNotValid += stats->InPktsNotValid;
2676 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2677 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2680 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2681 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2682 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2683 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2684 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2690 static noinline_for_stack int
2691 copy_rx_sc_stats(struct sk_buff *skb, struct pcpu_rx_sc_stats __percpu *pstats)
2693 struct macsec_rx_sc_stats sum = {0, };
2696 for_each_possible_cpu(cpu) {
2697 const struct pcpu_rx_sc_stats *stats;
2698 struct macsec_rx_sc_stats tmp;
2701 stats = per_cpu_ptr(pstats, cpu);
2703 start = u64_stats_fetch_begin_irq(&stats->syncp);
2704 memcpy(&tmp, &stats->stats, sizeof(tmp));
2705 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2707 sum.InOctetsValidated += tmp.InOctetsValidated;
2708 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2709 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2710 sum.InPktsDelayed += tmp.InPktsDelayed;
2711 sum.InPktsOK += tmp.InPktsOK;
2712 sum.InPktsInvalid += tmp.InPktsInvalid;
2713 sum.InPktsLate += tmp.InPktsLate;
2714 sum.InPktsNotValid += tmp.InPktsNotValid;
2715 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2716 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2719 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2720 sum.InOctetsValidated,
2721 MACSEC_RXSC_STATS_ATTR_PAD) ||
2722 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2723 sum.InOctetsDecrypted,
2724 MACSEC_RXSC_STATS_ATTR_PAD) ||
2725 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2726 sum.InPktsUnchecked,
2727 MACSEC_RXSC_STATS_ATTR_PAD) ||
2728 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2730 MACSEC_RXSC_STATS_ATTR_PAD) ||
2731 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2733 MACSEC_RXSC_STATS_ATTR_PAD) ||
2734 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2736 MACSEC_RXSC_STATS_ATTR_PAD) ||
2737 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2739 MACSEC_RXSC_STATS_ATTR_PAD) ||
2740 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2742 MACSEC_RXSC_STATS_ATTR_PAD) ||
2743 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2744 sum.InPktsNotUsingSA,
2745 MACSEC_RXSC_STATS_ATTR_PAD) ||
2746 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2748 MACSEC_RXSC_STATS_ATTR_PAD))
2754 static noinline_for_stack int
2755 copy_tx_sc_stats(struct sk_buff *skb, struct pcpu_tx_sc_stats __percpu *pstats)
2757 struct macsec_tx_sc_stats sum = {0, };
2760 for_each_possible_cpu(cpu) {
2761 const struct pcpu_tx_sc_stats *stats;
2762 struct macsec_tx_sc_stats tmp;
2765 stats = per_cpu_ptr(pstats, cpu);
2767 start = u64_stats_fetch_begin_irq(&stats->syncp);
2768 memcpy(&tmp, &stats->stats, sizeof(tmp));
2769 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2771 sum.OutPktsProtected += tmp.OutPktsProtected;
2772 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2773 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2774 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2777 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2778 sum.OutPktsProtected,
2779 MACSEC_TXSC_STATS_ATTR_PAD) ||
2780 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2781 sum.OutPktsEncrypted,
2782 MACSEC_TXSC_STATS_ATTR_PAD) ||
2783 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2784 sum.OutOctetsProtected,
2785 MACSEC_TXSC_STATS_ATTR_PAD) ||
2786 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2787 sum.OutOctetsEncrypted,
2788 MACSEC_TXSC_STATS_ATTR_PAD))
2794 static noinline_for_stack int
2795 copy_secy_stats(struct sk_buff *skb, struct pcpu_secy_stats __percpu *pstats)
2797 struct macsec_dev_stats sum = {0, };
2800 for_each_possible_cpu(cpu) {
2801 const struct pcpu_secy_stats *stats;
2802 struct macsec_dev_stats tmp;
2805 stats = per_cpu_ptr(pstats, cpu);
2807 start = u64_stats_fetch_begin_irq(&stats->syncp);
2808 memcpy(&tmp, &stats->stats, sizeof(tmp));
2809 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2811 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2812 sum.InPktsUntagged += tmp.InPktsUntagged;
2813 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2814 sum.InPktsNoTag += tmp.InPktsNoTag;
2815 sum.InPktsBadTag += tmp.InPktsBadTag;
2816 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2817 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2818 sum.InPktsOverrun += tmp.InPktsOverrun;
2821 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2822 sum.OutPktsUntagged,
2823 MACSEC_SECY_STATS_ATTR_PAD) ||
2824 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2826 MACSEC_SECY_STATS_ATTR_PAD) ||
2827 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2829 MACSEC_SECY_STATS_ATTR_PAD) ||
2830 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2832 MACSEC_SECY_STATS_ATTR_PAD) ||
2833 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2835 MACSEC_SECY_STATS_ATTR_PAD) ||
2836 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2837 sum.InPktsUnknownSCI,
2838 MACSEC_SECY_STATS_ATTR_PAD) ||
2839 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2841 MACSEC_SECY_STATS_ATTR_PAD) ||
2842 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2844 MACSEC_SECY_STATS_ATTR_PAD))
2850 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2852 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2853 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
2860 switch (secy->key_len) {
2861 case MACSEC_GCM_AES_128_SAK_LEN:
2862 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
2864 case MACSEC_GCM_AES_256_SAK_LEN:
2865 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
2871 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2872 MACSEC_SECY_ATTR_PAD) ||
2873 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2874 csid, MACSEC_SECY_ATTR_PAD) ||
2875 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2876 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2877 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2878 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2879 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2880 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2881 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2882 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2883 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2884 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2887 if (secy->replay_protect) {
2888 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2892 nla_nest_end(skb, secy_nest);
2896 nla_nest_cancel(skb, secy_nest);
2900 static noinline_for_stack int
2901 dump_secy(struct macsec_secy *secy, struct net_device *dev,
2902 struct sk_buff *skb, struct netlink_callback *cb)
2904 struct macsec_dev *macsec = netdev_priv(dev);
2905 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2906 struct nlattr *txsa_list, *rxsc_list;
2907 struct macsec_rx_sc *rx_sc;
2908 struct nlattr *attr;
2912 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2913 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2917 genl_dump_check_consistent(cb, hdr);
2919 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2920 goto nla_put_failure;
2922 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
2924 goto nla_put_failure;
2925 if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
2926 goto nla_put_failure;
2927 nla_nest_end(skb, attr);
2929 if (nla_put_secy(secy, skb))
2930 goto nla_put_failure;
2932 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
2934 goto nla_put_failure;
2935 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2936 nla_nest_cancel(skb, attr);
2937 goto nla_put_failure;
2939 nla_nest_end(skb, attr);
2941 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
2943 goto nla_put_failure;
2944 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2945 nla_nest_cancel(skb, attr);
2946 goto nla_put_failure;
2948 nla_nest_end(skb, attr);
2950 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
2952 goto nla_put_failure;
2953 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2954 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2955 struct nlattr *txsa_nest;
2962 txsa_nest = nla_nest_start_noflag(skb, j++);
2964 nla_nest_cancel(skb, txsa_list);
2965 goto nla_put_failure;
2969 pn = tx_sa->next_pn;
2970 pn_len = MACSEC_XPN_PN_LEN;
2972 pn = tx_sa->next_pn_halves.lower;
2973 pn_len = MACSEC_DEFAULT_PN_LEN;
2976 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2977 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
2978 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2979 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) ||
2980 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2981 nla_nest_cancel(skb, txsa_nest);
2982 nla_nest_cancel(skb, txsa_list);
2983 goto nla_put_failure;
2986 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
2988 nla_nest_cancel(skb, txsa_nest);
2989 nla_nest_cancel(skb, txsa_list);
2990 goto nla_put_failure;
2992 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2993 nla_nest_cancel(skb, attr);
2994 nla_nest_cancel(skb, txsa_nest);
2995 nla_nest_cancel(skb, txsa_list);
2996 goto nla_put_failure;
2998 nla_nest_end(skb, attr);
3000 nla_nest_end(skb, txsa_nest);
3002 nla_nest_end(skb, txsa_list);
3004 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
3006 goto nla_put_failure;
3009 for_each_rxsc_rtnl(secy, rx_sc) {
3011 struct nlattr *rxsa_list;
3012 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
3015 nla_nest_cancel(skb, rxsc_list);
3016 goto nla_put_failure;
3019 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
3020 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
3021 MACSEC_RXSC_ATTR_PAD)) {
3022 nla_nest_cancel(skb, rxsc_nest);
3023 nla_nest_cancel(skb, rxsc_list);
3024 goto nla_put_failure;
3027 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
3029 nla_nest_cancel(skb, rxsc_nest);
3030 nla_nest_cancel(skb, rxsc_list);
3031 goto nla_put_failure;
3033 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
3034 nla_nest_cancel(skb, attr);
3035 nla_nest_cancel(skb, rxsc_nest);
3036 nla_nest_cancel(skb, rxsc_list);
3037 goto nla_put_failure;
3039 nla_nest_end(skb, attr);
3041 rxsa_list = nla_nest_start_noflag(skb,
3042 MACSEC_RXSC_ATTR_SA_LIST);
3044 nla_nest_cancel(skb, rxsc_nest);
3045 nla_nest_cancel(skb, rxsc_list);
3046 goto nla_put_failure;
3049 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
3050 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
3051 struct nlattr *rxsa_nest;
3058 rxsa_nest = nla_nest_start_noflag(skb, k++);
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;
3066 attr = nla_nest_start_noflag(skb,
3067 MACSEC_SA_ATTR_STATS);
3069 nla_nest_cancel(skb, rxsa_list);
3070 nla_nest_cancel(skb, rxsc_nest);
3071 nla_nest_cancel(skb, rxsc_list);
3072 goto nla_put_failure;
3074 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
3075 nla_nest_cancel(skb, attr);
3076 nla_nest_cancel(skb, rxsa_list);
3077 nla_nest_cancel(skb, rxsc_nest);
3078 nla_nest_cancel(skb, rxsc_list);
3079 goto nla_put_failure;
3081 nla_nest_end(skb, attr);
3084 pn = rx_sa->next_pn;
3085 pn_len = MACSEC_XPN_PN_LEN;
3087 pn = rx_sa->next_pn_halves.lower;
3088 pn_len = MACSEC_DEFAULT_PN_LEN;
3091 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3092 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3093 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
3094 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) ||
3095 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
3096 nla_nest_cancel(skb, rxsa_nest);
3097 nla_nest_cancel(skb, rxsc_nest);
3098 nla_nest_cancel(skb, rxsc_list);
3099 goto nla_put_failure;
3101 nla_nest_end(skb, rxsa_nest);
3104 nla_nest_end(skb, rxsa_list);
3105 nla_nest_end(skb, rxsc_nest);
3108 nla_nest_end(skb, rxsc_list);
3110 genlmsg_end(skb, hdr);
3115 genlmsg_cancel(skb, hdr);
3119 static int macsec_generation = 1; /* protected by RTNL */
3121 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
3123 struct net *net = sock_net(skb->sk);
3124 struct net_device *dev;
3127 dev_idx = cb->args[0];
3132 cb->seq = macsec_generation;
3134 for_each_netdev(net, dev) {
3135 struct macsec_secy *secy;
3140 if (!netif_is_macsec(dev))
3143 secy = &macsec_priv(dev)->secy;
3144 if (dump_secy(secy, dev, skb, cb) < 0)
3156 static const struct genl_ops macsec_genl_ops[] = {
3158 .cmd = MACSEC_CMD_GET_TXSC,
3159 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3160 .dumpit = macsec_dump_txsc,
3163 .cmd = MACSEC_CMD_ADD_RXSC,
3164 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3165 .doit = macsec_add_rxsc,
3166 .flags = GENL_ADMIN_PERM,
3169 .cmd = MACSEC_CMD_DEL_RXSC,
3170 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3171 .doit = macsec_del_rxsc,
3172 .flags = GENL_ADMIN_PERM,
3175 .cmd = MACSEC_CMD_UPD_RXSC,
3176 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3177 .doit = macsec_upd_rxsc,
3178 .flags = GENL_ADMIN_PERM,
3181 .cmd = MACSEC_CMD_ADD_TXSA,
3182 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3183 .doit = macsec_add_txsa,
3184 .flags = GENL_ADMIN_PERM,
3187 .cmd = MACSEC_CMD_DEL_TXSA,
3188 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3189 .doit = macsec_del_txsa,
3190 .flags = GENL_ADMIN_PERM,
3193 .cmd = MACSEC_CMD_UPD_TXSA,
3194 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3195 .doit = macsec_upd_txsa,
3196 .flags = GENL_ADMIN_PERM,
3199 .cmd = MACSEC_CMD_ADD_RXSA,
3200 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3201 .doit = macsec_add_rxsa,
3202 .flags = GENL_ADMIN_PERM,
3205 .cmd = MACSEC_CMD_DEL_RXSA,
3206 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3207 .doit = macsec_del_rxsa,
3208 .flags = GENL_ADMIN_PERM,
3211 .cmd = MACSEC_CMD_UPD_RXSA,
3212 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3213 .doit = macsec_upd_rxsa,
3214 .flags = GENL_ADMIN_PERM,
3217 .cmd = MACSEC_CMD_UPD_OFFLOAD,
3218 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3219 .doit = macsec_upd_offload,
3220 .flags = GENL_ADMIN_PERM,
3224 static struct genl_family macsec_fam __ro_after_init = {
3225 .name = MACSEC_GENL_NAME,
3227 .version = MACSEC_GENL_VERSION,
3228 .maxattr = MACSEC_ATTR_MAX,
3229 .policy = macsec_genl_policy,
3231 .module = THIS_MODULE,
3232 .ops = macsec_genl_ops,
3233 .n_ops = ARRAY_SIZE(macsec_genl_ops),
3236 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
3237 struct net_device *dev)
3239 struct macsec_dev *macsec = netdev_priv(dev);
3240 struct macsec_secy *secy = &macsec->secy;
3241 struct pcpu_secy_stats *secy_stats;
3244 if (macsec_is_offloaded(netdev_priv(dev))) {
3245 skb->dev = macsec->real_dev;
3246 return dev_queue_xmit(skb);
3250 if (!secy->protect_frames) {
3251 secy_stats = this_cpu_ptr(macsec->stats);
3252 u64_stats_update_begin(&secy_stats->syncp);
3253 secy_stats->stats.OutPktsUntagged++;
3254 u64_stats_update_end(&secy_stats->syncp);
3255 skb->dev = macsec->real_dev;
3257 ret = dev_queue_xmit(skb);
3258 count_tx(dev, ret, len);
3262 if (!secy->operational) {
3264 dev->stats.tx_dropped++;
3265 return NETDEV_TX_OK;
3268 skb = macsec_encrypt(skb, dev);
3270 if (PTR_ERR(skb) != -EINPROGRESS)
3271 dev->stats.tx_dropped++;
3272 return NETDEV_TX_OK;
3275 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
3277 macsec_encrypt_finish(skb, dev);
3279 ret = dev_queue_xmit(skb);
3280 count_tx(dev, ret, len);
3284 #define MACSEC_FEATURES \
3285 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
3287 static int macsec_dev_init(struct net_device *dev)
3289 struct macsec_dev *macsec = macsec_priv(dev);
3290 struct net_device *real_dev = macsec->real_dev;
3293 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3297 err = gro_cells_init(&macsec->gro_cells, dev);
3299 free_percpu(dev->tstats);
3303 dev->features = real_dev->features & MACSEC_FEATURES;
3304 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
3306 dev->needed_headroom = real_dev->needed_headroom +
3307 MACSEC_NEEDED_HEADROOM;
3308 dev->needed_tailroom = real_dev->needed_tailroom +
3309 MACSEC_NEEDED_TAILROOM;
3311 if (is_zero_ether_addr(dev->dev_addr))
3312 eth_hw_addr_inherit(dev, real_dev);
3313 if (is_zero_ether_addr(dev->broadcast))
3314 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
3319 static void macsec_dev_uninit(struct net_device *dev)
3321 struct macsec_dev *macsec = macsec_priv(dev);
3323 gro_cells_destroy(&macsec->gro_cells);
3324 free_percpu(dev->tstats);
3327 static netdev_features_t macsec_fix_features(struct net_device *dev,
3328 netdev_features_t features)
3330 struct macsec_dev *macsec = macsec_priv(dev);
3331 struct net_device *real_dev = macsec->real_dev;
3333 features &= (real_dev->features & MACSEC_FEATURES) |
3334 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
3335 features |= NETIF_F_LLTX;
3340 static int macsec_dev_open(struct net_device *dev)
3342 struct macsec_dev *macsec = macsec_priv(dev);
3343 struct net_device *real_dev = macsec->real_dev;
3346 err = dev_uc_add(real_dev, dev->dev_addr);
3350 if (dev->flags & IFF_ALLMULTI) {
3351 err = dev_set_allmulti(real_dev, 1);
3356 if (dev->flags & IFF_PROMISC) {
3357 err = dev_set_promiscuity(real_dev, 1);
3359 goto clear_allmulti;
3362 /* If h/w offloading is available, propagate to the device */
3363 if (macsec_is_offloaded(macsec)) {
3364 const struct macsec_ops *ops;
3365 struct macsec_context ctx;
3367 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3370 goto clear_allmulti;
3373 err = macsec_offload(ops->mdo_dev_open, &ctx);
3375 goto clear_allmulti;
3378 if (netif_carrier_ok(real_dev))
3379 netif_carrier_on(dev);
3383 if (dev->flags & IFF_ALLMULTI)
3384 dev_set_allmulti(real_dev, -1);
3386 dev_uc_del(real_dev, dev->dev_addr);
3387 netif_carrier_off(dev);
3391 static int macsec_dev_stop(struct net_device *dev)
3393 struct macsec_dev *macsec = macsec_priv(dev);
3394 struct net_device *real_dev = macsec->real_dev;
3396 netif_carrier_off(dev);
3398 /* If h/w offloading is available, propagate to the device */
3399 if (macsec_is_offloaded(macsec)) {
3400 const struct macsec_ops *ops;
3401 struct macsec_context ctx;
3403 ops = macsec_get_ops(macsec, &ctx);
3405 macsec_offload(ops->mdo_dev_stop, &ctx);
3408 dev_mc_unsync(real_dev, dev);
3409 dev_uc_unsync(real_dev, dev);
3411 if (dev->flags & IFF_ALLMULTI)
3412 dev_set_allmulti(real_dev, -1);
3414 if (dev->flags & IFF_PROMISC)
3415 dev_set_promiscuity(real_dev, -1);
3417 dev_uc_del(real_dev, dev->dev_addr);
3422 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
3424 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3426 if (!(dev->flags & IFF_UP))
3429 if (change & IFF_ALLMULTI)
3430 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
3432 if (change & IFF_PROMISC)
3433 dev_set_promiscuity(real_dev,
3434 dev->flags & IFF_PROMISC ? 1 : -1);
3437 static void macsec_dev_set_rx_mode(struct net_device *dev)
3439 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3441 dev_mc_sync(real_dev, dev);
3442 dev_uc_sync(real_dev, dev);
3445 static int macsec_set_mac_address(struct net_device *dev, void *p)
3447 struct macsec_dev *macsec = macsec_priv(dev);
3448 struct net_device *real_dev = macsec->real_dev;
3449 struct sockaddr *addr = p;
3452 if (!is_valid_ether_addr(addr->sa_data))
3453 return -EADDRNOTAVAIL;
3455 if (!(dev->flags & IFF_UP))
3458 err = dev_uc_add(real_dev, addr->sa_data);
3462 dev_uc_del(real_dev, dev->dev_addr);
3465 ether_addr_copy(dev->dev_addr, addr->sa_data);
3466 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
3468 /* If h/w offloading is available, propagate to the device */
3469 if (macsec_is_offloaded(macsec)) {
3470 const struct macsec_ops *ops;
3471 struct macsec_context ctx;
3473 ops = macsec_get_ops(macsec, &ctx);
3475 ctx.secy = &macsec->secy;
3476 macsec_offload(ops->mdo_upd_secy, &ctx);
3483 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
3485 struct macsec_dev *macsec = macsec_priv(dev);
3486 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
3488 if (macsec->real_dev->mtu - extra < new_mtu)
3496 static void macsec_get_stats64(struct net_device *dev,
3497 struct rtnl_link_stats64 *s)
3504 for_each_possible_cpu(cpu) {
3505 struct pcpu_sw_netstats *stats;
3506 struct pcpu_sw_netstats tmp;
3509 stats = per_cpu_ptr(dev->tstats, cpu);
3511 start = u64_stats_fetch_begin_irq(&stats->syncp);
3512 tmp.rx_packets = stats->rx_packets;
3513 tmp.rx_bytes = stats->rx_bytes;
3514 tmp.tx_packets = stats->tx_packets;
3515 tmp.tx_bytes = stats->tx_bytes;
3516 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
3518 s->rx_packets += tmp.rx_packets;
3519 s->rx_bytes += tmp.rx_bytes;
3520 s->tx_packets += tmp.tx_packets;
3521 s->tx_bytes += tmp.tx_bytes;
3524 s->rx_dropped = dev->stats.rx_dropped;
3525 s->tx_dropped = dev->stats.tx_dropped;
3528 static int macsec_get_iflink(const struct net_device *dev)
3530 return macsec_priv(dev)->real_dev->ifindex;
3533 static const struct net_device_ops macsec_netdev_ops = {
3534 .ndo_init = macsec_dev_init,
3535 .ndo_uninit = macsec_dev_uninit,
3536 .ndo_open = macsec_dev_open,
3537 .ndo_stop = macsec_dev_stop,
3538 .ndo_fix_features = macsec_fix_features,
3539 .ndo_change_mtu = macsec_change_mtu,
3540 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
3541 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
3542 .ndo_set_mac_address = macsec_set_mac_address,
3543 .ndo_start_xmit = macsec_start_xmit,
3544 .ndo_get_stats64 = macsec_get_stats64,
3545 .ndo_get_iflink = macsec_get_iflink,
3548 static const struct device_type macsec_type = {
3552 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3553 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3554 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3555 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3556 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3557 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3558 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3559 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3560 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3561 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3562 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
3563 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3564 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3565 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3568 static void macsec_free_netdev(struct net_device *dev)
3570 struct macsec_dev *macsec = macsec_priv(dev);
3572 free_percpu(macsec->stats);
3573 free_percpu(macsec->secy.tx_sc.stats);
3577 static void macsec_setup(struct net_device *dev)
3581 dev->max_mtu = ETH_MAX_MTU;
3582 dev->priv_flags |= IFF_NO_QUEUE;
3583 dev->netdev_ops = &macsec_netdev_ops;
3584 dev->needs_free_netdev = true;
3585 dev->priv_destructor = macsec_free_netdev;
3586 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3588 eth_zero_addr(dev->broadcast);
3591 static int macsec_changelink_common(struct net_device *dev,
3592 struct nlattr *data[])
3594 struct macsec_secy *secy;
3595 struct macsec_tx_sc *tx_sc;
3597 secy = &macsec_priv(dev)->secy;
3598 tx_sc = &secy->tx_sc;
3600 if (data[IFLA_MACSEC_ENCODING_SA]) {
3601 struct macsec_tx_sa *tx_sa;
3603 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3604 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3606 secy->operational = tx_sa && tx_sa->active;
3609 if (data[IFLA_MACSEC_WINDOW])
3610 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3612 if (data[IFLA_MACSEC_ENCRYPT])
3613 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3615 if (data[IFLA_MACSEC_PROTECT])
3616 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3618 if (data[IFLA_MACSEC_INC_SCI])
3619 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3621 if (data[IFLA_MACSEC_ES])
3622 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3624 if (data[IFLA_MACSEC_SCB])
3625 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3627 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3628 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3630 if (data[IFLA_MACSEC_VALIDATION])
3631 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3633 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3634 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3635 case MACSEC_CIPHER_ID_GCM_AES_128:
3636 case MACSEC_DEFAULT_CIPHER_ID:
3637 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3640 case MACSEC_CIPHER_ID_GCM_AES_256:
3641 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3644 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3645 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3648 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3649 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3660 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3661 struct nlattr *data[],
3662 struct netlink_ext_ack *extack)
3664 struct macsec_dev *macsec = macsec_priv(dev);
3665 struct macsec_tx_sa tx_sc;
3666 struct macsec_secy secy;
3672 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3673 data[IFLA_MACSEC_ICV_LEN] ||
3674 data[IFLA_MACSEC_SCI] ||
3675 data[IFLA_MACSEC_PORT])
3678 /* Keep a copy of unmodified secy and tx_sc, in case the offload
3679 * propagation fails, to revert macsec_changelink_common.
3681 memcpy(&secy, &macsec->secy, sizeof(secy));
3682 memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
3684 ret = macsec_changelink_common(dev, data);
3688 /* If h/w offloading is available, propagate to the device */
3689 if (macsec_is_offloaded(macsec)) {
3690 const struct macsec_ops *ops;
3691 struct macsec_context ctx;
3694 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3700 ctx.secy = &macsec->secy;
3701 ret = macsec_offload(ops->mdo_upd_secy, &ctx);
3709 memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
3710 memcpy(&macsec->secy, &secy, sizeof(secy));
3715 static void macsec_del_dev(struct macsec_dev *macsec)
3719 while (macsec->secy.rx_sc) {
3720 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3722 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3726 for (i = 0; i < MACSEC_NUM_AN; i++) {
3727 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3730 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3736 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3738 struct macsec_dev *macsec = macsec_priv(dev);
3739 struct net_device *real_dev = macsec->real_dev;
3741 unregister_netdevice_queue(dev, head);
3742 list_del_rcu(&macsec->secys);
3743 macsec_del_dev(macsec);
3744 netdev_upper_dev_unlink(real_dev, dev);
3746 macsec_generation++;
3749 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3751 struct macsec_dev *macsec = macsec_priv(dev);
3752 struct net_device *real_dev = macsec->real_dev;
3753 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3755 /* If h/w offloading is available, propagate to the device */
3756 if (macsec_is_offloaded(macsec)) {
3757 const struct macsec_ops *ops;
3758 struct macsec_context ctx;
3760 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3762 ctx.secy = &macsec->secy;
3763 macsec_offload(ops->mdo_del_secy, &ctx);
3767 macsec_common_dellink(dev, head);
3769 if (list_empty(&rxd->secys)) {
3770 netdev_rx_handler_unregister(real_dev);
3775 static int register_macsec_dev(struct net_device *real_dev,
3776 struct net_device *dev)
3778 struct macsec_dev *macsec = macsec_priv(dev);
3779 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3784 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3788 INIT_LIST_HEAD(&rxd->secys);
3790 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3798 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3802 static bool sci_exists(struct net_device *dev, sci_t sci)
3804 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3805 struct macsec_dev *macsec;
3807 list_for_each_entry(macsec, &rxd->secys, secys) {
3808 if (macsec->secy.sci == sci)
3815 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3817 struct macsec_dev *macsec = macsec_priv(dev);
3818 struct macsec_secy *secy = &macsec->secy;
3820 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3824 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3825 if (!secy->tx_sc.stats) {
3826 free_percpu(macsec->stats);
3830 if (sci == MACSEC_UNDEF_SCI)
3831 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3834 secy->operational = true;
3835 secy->key_len = DEFAULT_SAK_LEN;
3836 secy->icv_len = icv_len;
3837 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3838 secy->protect_frames = true;
3839 secy->replay_protect = false;
3840 secy->xpn = DEFAULT_XPN;
3843 secy->tx_sc.active = true;
3844 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3845 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3846 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3847 secy->tx_sc.end_station = false;
3848 secy->tx_sc.scb = false;
3853 static int macsec_newlink(struct net *net, struct net_device *dev,
3854 struct nlattr *tb[], struct nlattr *data[],
3855 struct netlink_ext_ack *extack)
3857 struct macsec_dev *macsec = macsec_priv(dev);
3858 struct net_device *real_dev;
3861 u8 icv_len = DEFAULT_ICV_LEN;
3862 rx_handler_func_t *rx_handler;
3866 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3869 if (real_dev->type != ARPHRD_ETHER)
3872 dev->priv_flags |= IFF_MACSEC;
3874 macsec->real_dev = real_dev;
3876 /* MACsec offloading is off by default */
3877 macsec->offload = MACSEC_OFFLOAD_OFF;
3879 if (data && data[IFLA_MACSEC_ICV_LEN])
3880 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3881 dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3883 rx_handler = rtnl_dereference(real_dev->rx_handler);
3884 if (rx_handler && rx_handler != macsec_handle_frame)
3887 err = register_netdevice(dev);
3891 err = netdev_upper_dev_link(real_dev, dev, extack);
3895 /* need to be already registered so that ->init has run and
3896 * the MAC addr is set
3898 if (data && data[IFLA_MACSEC_SCI])
3899 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3900 else if (data && data[IFLA_MACSEC_PORT])
3901 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3903 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3905 if (rx_handler && sci_exists(real_dev, sci)) {
3910 err = macsec_add_dev(dev, sci, icv_len);
3915 err = macsec_changelink_common(dev, data);
3920 err = register_macsec_dev(real_dev, dev);
3924 netif_stacked_transfer_operstate(real_dev, dev);
3925 linkwatch_fire_event(dev);
3927 macsec_generation++;
3932 macsec_del_dev(macsec);
3934 netdev_upper_dev_unlink(real_dev, dev);
3936 unregister_netdevice(dev);
3940 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3941 struct netlink_ext_ack *extack)
3943 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3944 u8 icv_len = DEFAULT_ICV_LEN;
3951 if (data[IFLA_MACSEC_CIPHER_SUITE])
3952 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3954 if (data[IFLA_MACSEC_ICV_LEN]) {
3955 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3956 if (icv_len != DEFAULT_ICV_LEN) {
3957 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3958 struct crypto_aead *dummy_tfm;
3960 dummy_tfm = macsec_alloc_tfm(dummy_key,
3963 if (IS_ERR(dummy_tfm))
3964 return PTR_ERR(dummy_tfm);
3965 crypto_free_aead(dummy_tfm);
3970 case MACSEC_CIPHER_ID_GCM_AES_128:
3971 case MACSEC_CIPHER_ID_GCM_AES_256:
3972 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3973 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3974 case MACSEC_DEFAULT_CIPHER_ID:
3975 if (icv_len < MACSEC_MIN_ICV_LEN ||
3976 icv_len > MACSEC_STD_ICV_LEN)
3983 if (data[IFLA_MACSEC_ENCODING_SA]) {
3984 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3988 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3989 flag < IFLA_MACSEC_VALIDATION;
3992 if (nla_get_u8(data[flag]) > 1)
3997 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3998 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3999 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
4001 if ((sci && (scb || es)) || (scb && es))
4004 if (data[IFLA_MACSEC_VALIDATION] &&
4005 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
4008 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
4009 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
4010 !data[IFLA_MACSEC_WINDOW])
4016 static struct net *macsec_get_link_net(const struct net_device *dev)
4018 return dev_net(macsec_priv(dev)->real_dev);
4021 static size_t macsec_get_size(const struct net_device *dev)
4023 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
4024 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
4025 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
4026 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
4027 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
4028 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
4029 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
4030 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
4031 nla_total_size(1) + /* IFLA_MACSEC_ES */
4032 nla_total_size(1) + /* IFLA_MACSEC_SCB */
4033 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
4034 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
4038 static int macsec_fill_info(struct sk_buff *skb,
4039 const struct net_device *dev)
4041 struct macsec_secy *secy = &macsec_priv(dev)->secy;
4042 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
4045 switch (secy->key_len) {
4046 case MACSEC_GCM_AES_128_SAK_LEN:
4047 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
4049 case MACSEC_GCM_AES_256_SAK_LEN:
4050 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
4053 goto nla_put_failure;
4056 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
4058 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
4059 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
4060 csid, IFLA_MACSEC_PAD) ||
4061 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
4062 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
4063 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
4064 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
4065 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
4066 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
4067 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
4068 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
4070 goto nla_put_failure;
4072 if (secy->replay_protect) {
4073 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
4074 goto nla_put_failure;
4083 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
4085 .priv_size = sizeof(struct macsec_dev),
4086 .maxtype = IFLA_MACSEC_MAX,
4087 .policy = macsec_rtnl_policy,
4088 .setup = macsec_setup,
4089 .validate = macsec_validate_attr,
4090 .newlink = macsec_newlink,
4091 .changelink = macsec_changelink,
4092 .dellink = macsec_dellink,
4093 .get_size = macsec_get_size,
4094 .fill_info = macsec_fill_info,
4095 .get_link_net = macsec_get_link_net,
4098 static bool is_macsec_master(struct net_device *dev)
4100 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
4103 static int macsec_notify(struct notifier_block *this, unsigned long event,
4106 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
4109 if (!is_macsec_master(real_dev))
4115 case NETDEV_CHANGE: {
4116 struct macsec_dev *m, *n;
4117 struct macsec_rxh_data *rxd;
4119 rxd = macsec_data_rtnl(real_dev);
4120 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4121 struct net_device *dev = m->secy.netdev;
4123 netif_stacked_transfer_operstate(real_dev, dev);
4127 case NETDEV_UNREGISTER: {
4128 struct macsec_dev *m, *n;
4129 struct macsec_rxh_data *rxd;
4131 rxd = macsec_data_rtnl(real_dev);
4132 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4133 macsec_common_dellink(m->secy.netdev, &head);
4136 netdev_rx_handler_unregister(real_dev);
4139 unregister_netdevice_many(&head);
4142 case NETDEV_CHANGEMTU: {
4143 struct macsec_dev *m;
4144 struct macsec_rxh_data *rxd;
4146 rxd = macsec_data_rtnl(real_dev);
4147 list_for_each_entry(m, &rxd->secys, secys) {
4148 struct net_device *dev = m->secy.netdev;
4149 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
4150 macsec_extra_len(true));
4153 dev_set_mtu(dev, mtu);
4161 static struct notifier_block macsec_notifier = {
4162 .notifier_call = macsec_notify,
4165 static int __init macsec_init(void)
4169 pr_info("MACsec IEEE 802.1AE\n");
4170 err = register_netdevice_notifier(&macsec_notifier);
4174 err = rtnl_link_register(&macsec_link_ops);
4178 err = genl_register_family(&macsec_fam);
4185 rtnl_link_unregister(&macsec_link_ops);
4187 unregister_netdevice_notifier(&macsec_notifier);
4191 static void __exit macsec_exit(void)
4193 genl_unregister_family(&macsec_fam);
4194 rtnl_link_unregister(&macsec_link_ops);
4195 unregister_netdevice_notifier(&macsec_notifier);
4199 module_init(macsec_init);
4200 module_exit(macsec_exit);
4202 MODULE_ALIAS_RTNL_LINK("macsec");
4203 MODULE_ALIAS_GENL_FAMILY("macsec");
4205 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
4206 MODULE_LICENSE("GPL v2");