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 <net/dst_metadata.h>
22 #include <linux/phy.h>
23 #include <linux/byteorder/generic.h>
24 #include <linux/if_arp.h>
26 #include <uapi/linux/if_macsec.h>
28 /* SecTAG length = macsec_eth_header without the optional SCI */
29 #define MACSEC_TAG_LEN 6
31 struct macsec_eth_header {
35 #if defined(__LITTLE_ENDIAN_BITFIELD)
38 #elif defined(__BIG_ENDIAN_BITFIELD)
42 #error "Please fix <asm/byteorder.h>"
45 u8 secure_channel_id[8]; /* optional */
48 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
49 #define MIN_NON_SHORT_LEN 48
51 #define GCM_AES_IV_LEN 12
53 #define for_each_rxsc(secy, sc) \
54 for (sc = rcu_dereference_bh(secy->rx_sc); \
56 sc = rcu_dereference_bh(sc->next))
57 #define for_each_rxsc_rtnl(secy, sc) \
58 for (sc = rtnl_dereference(secy->rx_sc); \
60 sc = rtnl_dereference(sc->next))
62 #define pn_same_half(pn1, pn2) (!(((pn1) >> 31) ^ ((pn2) >> 31)))
66 u8 short_secure_channel_id[4];
74 u8 secure_channel_id[8];
80 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
82 struct pcpu_secy_stats {
83 struct macsec_dev_stats stats;
84 struct u64_stats_sync syncp;
88 * struct macsec_dev - private data
90 * @real_dev: pointer to underlying netdevice
91 * @dev_tracker: refcount tracker for @real_dev reference
92 * @stats: MACsec device stats
93 * @secys: linked list of SecY's on the underlying device
94 * @gro_cells: pointer to the Generic Receive Offload cell
95 * @offload: status of offloading on the MACsec device
96 * @insert_tx_tag: when offloading, device requires to insert an
100 struct macsec_secy secy;
101 struct net_device *real_dev;
102 netdevice_tracker dev_tracker;
103 struct pcpu_secy_stats __percpu *stats;
104 struct list_head secys;
105 struct gro_cells gro_cells;
106 enum macsec_offload offload;
111 * struct macsec_rxh_data - rx_handler private argument
112 * @secys: linked list of SecY's on this underlying device
114 struct macsec_rxh_data {
115 struct list_head secys;
118 static struct macsec_dev *macsec_priv(const struct net_device *dev)
120 return (struct macsec_dev *)netdev_priv(dev);
123 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
125 return rcu_dereference_bh(dev->rx_handler_data);
128 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
130 return rtnl_dereference(dev->rx_handler_data);
134 struct aead_request *req;
136 struct macsec_tx_sa *tx_sa;
137 struct macsec_rx_sa *rx_sa;
144 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
146 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
148 if (!sa || !sa->active)
151 if (!refcount_inc_not_zero(&sa->refcnt))
157 static struct macsec_rx_sa *macsec_active_rxsa_get(struct macsec_rx_sc *rx_sc)
159 struct macsec_rx_sa *sa = NULL;
162 for (an = 0; an < MACSEC_NUM_AN; an++) {
163 sa = macsec_rxsa_get(rx_sc->sa[an]);
170 static void free_rx_sc_rcu(struct rcu_head *head)
172 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
174 free_percpu(rx_sc->stats);
178 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
180 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
183 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
185 if (refcount_dec_and_test(&sc->refcnt))
186 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
189 static void free_rxsa(struct rcu_head *head)
191 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
193 crypto_free_aead(sa->key.tfm);
194 free_percpu(sa->stats);
198 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
200 if (refcount_dec_and_test(&sa->refcnt))
201 call_rcu(&sa->rcu, free_rxsa);
204 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
206 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
208 if (!sa || !sa->active)
211 if (!refcount_inc_not_zero(&sa->refcnt))
217 static void free_txsa(struct rcu_head *head)
219 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
221 crypto_free_aead(sa->key.tfm);
222 free_percpu(sa->stats);
226 static void macsec_txsa_put(struct macsec_tx_sa *sa)
228 if (refcount_dec_and_test(&sa->refcnt))
229 call_rcu(&sa->rcu, free_txsa);
232 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
234 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
235 return (struct macsec_cb *)skb->cb;
238 #define MACSEC_PORT_SCB (0x0000)
239 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
240 #define MACSEC_UNDEF_SSCI ((__force ssci_t)0xffffffff)
242 #define MACSEC_GCM_AES_128_SAK_LEN 16
243 #define MACSEC_GCM_AES_256_SAK_LEN 32
245 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
246 #define DEFAULT_XPN false
247 #define DEFAULT_SEND_SCI true
248 #define DEFAULT_ENCRYPT false
249 #define DEFAULT_ENCODING_SA 0
250 #define MACSEC_XPN_MAX_REPLAY_WINDOW (((1 << 30) - 1))
252 static sci_t make_sci(const u8 *addr, __be16 port)
256 memcpy(&sci, addr, ETH_ALEN);
257 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
262 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
267 memcpy(&sci, hdr->secure_channel_id,
268 sizeof(hdr->secure_channel_id));
270 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
275 static unsigned int macsec_sectag_len(bool sci_present)
277 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
280 static unsigned int macsec_hdr_len(bool sci_present)
282 return macsec_sectag_len(sci_present) + ETH_HLEN;
285 static unsigned int macsec_extra_len(bool sci_present)
287 return macsec_sectag_len(sci_present) + sizeof(__be16);
290 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
291 static void macsec_fill_sectag(struct macsec_eth_header *h,
292 const struct macsec_secy *secy, u32 pn,
295 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
297 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
298 h->eth.h_proto = htons(ETH_P_MACSEC);
301 h->tci_an |= MACSEC_TCI_SC;
302 memcpy(&h->secure_channel_id, &secy->sci,
303 sizeof(h->secure_channel_id));
305 if (tx_sc->end_station)
306 h->tci_an |= MACSEC_TCI_ES;
308 h->tci_an |= MACSEC_TCI_SCB;
311 h->packet_number = htonl(pn);
313 /* with GCM, C/E clear for !encrypt, both set for encrypt */
315 h->tci_an |= MACSEC_TCI_CONFID;
316 else if (secy->icv_len != MACSEC_DEFAULT_ICV_LEN)
317 h->tci_an |= MACSEC_TCI_C;
319 h->tci_an |= tx_sc->encoding_sa;
322 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
324 if (data_len < MIN_NON_SHORT_LEN)
325 h->short_length = data_len;
328 /* Checks if a MACsec interface is being offloaded to an hardware engine */
329 static bool macsec_is_offloaded(struct macsec_dev *macsec)
331 if (macsec->offload == MACSEC_OFFLOAD_MAC ||
332 macsec->offload == MACSEC_OFFLOAD_PHY)
338 /* Checks if underlying layers implement MACsec offloading functions. */
339 static bool macsec_check_offload(enum macsec_offload offload,
340 struct macsec_dev *macsec)
342 if (!macsec || !macsec->real_dev)
345 if (offload == MACSEC_OFFLOAD_PHY)
346 return macsec->real_dev->phydev &&
347 macsec->real_dev->phydev->macsec_ops;
348 else if (offload == MACSEC_OFFLOAD_MAC)
349 return macsec->real_dev->features & NETIF_F_HW_MACSEC &&
350 macsec->real_dev->macsec_ops;
355 static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload,
356 struct macsec_dev *macsec,
357 struct macsec_context *ctx)
360 memset(ctx, 0, sizeof(*ctx));
361 ctx->offload = offload;
363 if (offload == MACSEC_OFFLOAD_PHY)
364 ctx->phydev = macsec->real_dev->phydev;
365 else if (offload == MACSEC_OFFLOAD_MAC)
366 ctx->netdev = macsec->real_dev;
369 if (offload == MACSEC_OFFLOAD_PHY)
370 return macsec->real_dev->phydev->macsec_ops;
372 return macsec->real_dev->macsec_ops;
375 /* Returns a pointer to the MACsec ops struct if any and updates the MACsec
376 * context device reference if provided.
378 static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec,
379 struct macsec_context *ctx)
381 if (!macsec_check_offload(macsec->offload, macsec))
384 return __macsec_get_ops(macsec->offload, macsec, ctx);
387 /* validate MACsec packet according to IEEE 802.1AE-2018 9.12 */
388 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len, bool xpn)
390 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
391 int len = skb->len - 2 * ETH_ALEN;
392 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
394 /* a) It comprises at least 17 octets */
398 /* b) MACsec EtherType: already checked */
400 /* c) V bit is clear */
401 if (h->tci_an & MACSEC_TCI_VERSION)
404 /* d) ES or SCB => !SC */
405 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
406 (h->tci_an & MACSEC_TCI_SC))
409 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
413 /* rx.pn != 0 if not XPN (figure 10-5 with 802.11AEbw-2013 amendment) */
414 if (!h->packet_number && !xpn)
417 /* length check, f) g) h) i) */
419 return len == extra_len + h->short_length;
420 return len >= extra_len + MIN_NON_SHORT_LEN;
423 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
424 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
426 static void macsec_fill_iv_xpn(unsigned char *iv, ssci_t ssci, u64 pn,
429 struct gcm_iv_xpn *gcm_iv = (struct gcm_iv_xpn *)iv;
431 gcm_iv->ssci = ssci ^ salt.ssci;
432 gcm_iv->pn = cpu_to_be64(pn) ^ salt.pn;
435 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
437 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
440 gcm_iv->pn = htonl(pn);
443 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
445 return (struct macsec_eth_header *)skb_mac_header(skb);
448 static void __macsec_pn_wrapped(struct macsec_secy *secy,
449 struct macsec_tx_sa *tx_sa)
451 pr_debug("PN wrapped, transitioning to !oper\n");
452 tx_sa->active = false;
453 if (secy->protect_frames)
454 secy->operational = false;
457 void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa)
459 spin_lock_bh(&tx_sa->lock);
460 __macsec_pn_wrapped(secy, tx_sa);
461 spin_unlock_bh(&tx_sa->lock);
463 EXPORT_SYMBOL_GPL(macsec_pn_wrapped);
465 static pn_t tx_sa_update_pn(struct macsec_tx_sa *tx_sa,
466 struct macsec_secy *secy)
470 spin_lock_bh(&tx_sa->lock);
472 pn = tx_sa->next_pn_halves;
476 tx_sa->next_pn_halves.lower++;
478 if (tx_sa->next_pn == 0)
479 __macsec_pn_wrapped(secy, tx_sa);
480 spin_unlock_bh(&tx_sa->lock);
485 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
487 struct macsec_dev *macsec = netdev_priv(dev);
489 skb->dev = macsec->real_dev;
490 skb_reset_mac_header(skb);
491 skb->protocol = eth_hdr(skb)->h_proto;
494 static unsigned int macsec_msdu_len(struct sk_buff *skb)
496 struct macsec_dev *macsec = macsec_priv(skb->dev);
497 struct macsec_secy *secy = &macsec->secy;
498 bool sci_present = macsec_skb_cb(skb)->has_sci;
500 return skb->len - macsec_hdr_len(sci_present) - secy->icv_len;
503 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
504 struct macsec_tx_sa *tx_sa)
506 unsigned int msdu_len = macsec_msdu_len(skb);
507 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
509 u64_stats_update_begin(&txsc_stats->syncp);
510 if (tx_sc->encrypt) {
511 txsc_stats->stats.OutOctetsEncrypted += msdu_len;
512 txsc_stats->stats.OutPktsEncrypted++;
513 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
515 txsc_stats->stats.OutOctetsProtected += msdu_len;
516 txsc_stats->stats.OutPktsProtected++;
517 this_cpu_inc(tx_sa->stats->OutPktsProtected);
519 u64_stats_update_end(&txsc_stats->syncp);
522 static void count_tx(struct net_device *dev, int ret, int len)
524 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN))
525 dev_sw_netstats_tx_add(dev, 1, len);
528 static void macsec_encrypt_done(void *data, int err)
530 struct sk_buff *skb = data;
531 struct net_device *dev = skb->dev;
532 struct macsec_dev *macsec = macsec_priv(dev);
533 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
536 aead_request_free(macsec_skb_cb(skb)->req);
539 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
540 /* packet is encrypted/protected so tx_bytes must be calculated */
541 len = macsec_msdu_len(skb) + 2 * ETH_ALEN;
542 macsec_encrypt_finish(skb, dev);
543 ret = dev_queue_xmit(skb);
544 count_tx(dev, ret, len);
545 rcu_read_unlock_bh();
551 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
553 struct scatterlist **sg,
556 size_t size, iv_offset, sg_offset;
557 struct aead_request *req;
560 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
562 size += GCM_AES_IV_LEN;
564 size = ALIGN(size, __alignof__(struct scatterlist));
566 size += sizeof(struct scatterlist) * num_frags;
568 tmp = kmalloc(size, GFP_ATOMIC);
572 *iv = (unsigned char *)(tmp + iv_offset);
573 *sg = (struct scatterlist *)(tmp + sg_offset);
576 aead_request_set_tfm(req, tfm);
581 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
582 struct net_device *dev)
585 struct scatterlist *sg;
586 struct sk_buff *trailer;
589 struct macsec_eth_header *hh;
590 size_t unprotected_len;
591 struct aead_request *req;
592 struct macsec_secy *secy;
593 struct macsec_tx_sc *tx_sc;
594 struct macsec_tx_sa *tx_sa;
595 struct macsec_dev *macsec = macsec_priv(dev);
599 secy = &macsec->secy;
600 tx_sc = &secy->tx_sc;
602 /* 10.5.1 TX SA assignment */
603 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
605 secy->operational = false;
607 return ERR_PTR(-EINVAL);
610 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
611 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
612 struct sk_buff *nskb = skb_copy_expand(skb,
613 MACSEC_NEEDED_HEADROOM,
614 MACSEC_NEEDED_TAILROOM,
620 macsec_txsa_put(tx_sa);
622 return ERR_PTR(-ENOMEM);
625 skb = skb_unshare(skb, GFP_ATOMIC);
627 macsec_txsa_put(tx_sa);
628 return ERR_PTR(-ENOMEM);
632 unprotected_len = skb->len;
634 sci_present = macsec_send_sci(secy);
635 hh = skb_push(skb, macsec_extra_len(sci_present));
636 memmove(hh, eth, 2 * ETH_ALEN);
638 pn = tx_sa_update_pn(tx_sa, secy);
639 if (pn.full64 == 0) {
640 macsec_txsa_put(tx_sa);
642 return ERR_PTR(-ENOLINK);
644 macsec_fill_sectag(hh, secy, pn.lower, sci_present);
645 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
647 skb_put(skb, secy->icv_len);
649 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
650 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
652 u64_stats_update_begin(&secy_stats->syncp);
653 secy_stats->stats.OutPktsTooLong++;
654 u64_stats_update_end(&secy_stats->syncp);
656 macsec_txsa_put(tx_sa);
658 return ERR_PTR(-EINVAL);
661 ret = skb_cow_data(skb, 0, &trailer);
662 if (unlikely(ret < 0)) {
663 macsec_txsa_put(tx_sa);
668 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
670 macsec_txsa_put(tx_sa);
672 return ERR_PTR(-ENOMEM);
676 macsec_fill_iv_xpn(iv, tx_sa->ssci, pn.full64, tx_sa->key.salt);
678 macsec_fill_iv(iv, secy->sci, pn.lower);
680 sg_init_table(sg, ret);
681 ret = skb_to_sgvec(skb, sg, 0, skb->len);
682 if (unlikely(ret < 0)) {
683 aead_request_free(req);
684 macsec_txsa_put(tx_sa);
689 if (tx_sc->encrypt) {
690 int len = skb->len - macsec_hdr_len(sci_present) -
692 aead_request_set_crypt(req, sg, sg, len, iv);
693 aead_request_set_ad(req, macsec_hdr_len(sci_present));
695 aead_request_set_crypt(req, sg, sg, 0, iv);
696 aead_request_set_ad(req, skb->len - secy->icv_len);
699 macsec_skb_cb(skb)->req = req;
700 macsec_skb_cb(skb)->tx_sa = tx_sa;
701 macsec_skb_cb(skb)->has_sci = sci_present;
702 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
705 ret = crypto_aead_encrypt(req);
706 if (ret == -EINPROGRESS) {
708 } else if (ret != 0) {
711 aead_request_free(req);
712 macsec_txsa_put(tx_sa);
713 return ERR_PTR(-EINVAL);
717 aead_request_free(req);
718 macsec_txsa_put(tx_sa);
723 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
725 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
726 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
727 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
730 spin_lock(&rx_sa->lock);
731 if (rx_sa->next_pn_halves.lower >= secy->replay_window)
732 lowest_pn = rx_sa->next_pn_halves.lower - secy->replay_window;
734 /* Now perform replay protection check again
735 * (see IEEE 802.1AE-2006 figure 10-5)
737 if (secy->replay_protect && pn < lowest_pn &&
738 (!secy->xpn || pn_same_half(pn, lowest_pn))) {
739 spin_unlock(&rx_sa->lock);
740 u64_stats_update_begin(&rxsc_stats->syncp);
741 rxsc_stats->stats.InPktsLate++;
742 u64_stats_update_end(&rxsc_stats->syncp);
743 DEV_STATS_INC(secy->netdev, rx_dropped);
747 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
748 unsigned int msdu_len = macsec_msdu_len(skb);
749 u64_stats_update_begin(&rxsc_stats->syncp);
750 if (hdr->tci_an & MACSEC_TCI_E)
751 rxsc_stats->stats.InOctetsDecrypted += msdu_len;
753 rxsc_stats->stats.InOctetsValidated += msdu_len;
754 u64_stats_update_end(&rxsc_stats->syncp);
757 if (!macsec_skb_cb(skb)->valid) {
758 spin_unlock(&rx_sa->lock);
761 if (hdr->tci_an & MACSEC_TCI_C ||
762 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
763 u64_stats_update_begin(&rxsc_stats->syncp);
764 rxsc_stats->stats.InPktsNotValid++;
765 u64_stats_update_end(&rxsc_stats->syncp);
766 this_cpu_inc(rx_sa->stats->InPktsNotValid);
767 DEV_STATS_INC(secy->netdev, rx_errors);
771 u64_stats_update_begin(&rxsc_stats->syncp);
772 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
773 rxsc_stats->stats.InPktsInvalid++;
774 this_cpu_inc(rx_sa->stats->InPktsInvalid);
775 } else if (pn < lowest_pn) {
776 rxsc_stats->stats.InPktsDelayed++;
778 rxsc_stats->stats.InPktsUnchecked++;
780 u64_stats_update_end(&rxsc_stats->syncp);
782 u64_stats_update_begin(&rxsc_stats->syncp);
783 if (pn < lowest_pn) {
784 rxsc_stats->stats.InPktsDelayed++;
786 rxsc_stats->stats.InPktsOK++;
787 this_cpu_inc(rx_sa->stats->InPktsOK);
789 u64_stats_update_end(&rxsc_stats->syncp);
791 // Instead of "pn >=" - to support pn overflow in xpn
792 if (pn + 1 > rx_sa->next_pn_halves.lower) {
793 rx_sa->next_pn_halves.lower = pn + 1;
794 } else if (secy->xpn &&
795 !pn_same_half(pn, rx_sa->next_pn_halves.lower)) {
796 rx_sa->next_pn_halves.upper++;
797 rx_sa->next_pn_halves.lower = pn + 1;
800 spin_unlock(&rx_sa->lock);
806 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
808 skb->pkt_type = PACKET_HOST;
809 skb->protocol = eth_type_trans(skb, dev);
811 skb_reset_network_header(skb);
812 if (!skb_transport_header_was_set(skb))
813 skb_reset_transport_header(skb);
814 skb_reset_mac_len(skb);
817 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
819 skb->ip_summed = CHECKSUM_NONE;
820 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
821 skb_pull(skb, hdr_len);
822 pskb_trim_unique(skb, skb->len - icv_len);
825 static void count_rx(struct net_device *dev, int len)
827 dev_sw_netstats_rx_add(dev, len);
830 static void macsec_decrypt_done(void *data, int err)
832 struct sk_buff *skb = data;
833 struct net_device *dev = skb->dev;
834 struct macsec_dev *macsec = macsec_priv(dev);
835 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
836 struct macsec_rx_sc *rx_sc = rx_sa->sc;
840 aead_request_free(macsec_skb_cb(skb)->req);
843 macsec_skb_cb(skb)->valid = true;
846 pn = ntohl(macsec_ethhdr(skb)->packet_number);
847 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
848 rcu_read_unlock_bh();
853 macsec_finalize_skb(skb, macsec->secy.icv_len,
854 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
856 macsec_reset_skb(skb, macsec->secy.netdev);
858 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
861 rcu_read_unlock_bh();
864 macsec_rxsa_put(rx_sa);
865 macsec_rxsc_put(rx_sc);
869 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
870 struct net_device *dev,
871 struct macsec_rx_sa *rx_sa,
873 struct macsec_secy *secy)
876 struct scatterlist *sg;
877 struct sk_buff *trailer;
879 struct aead_request *req;
880 struct macsec_eth_header *hdr;
882 u16 icv_len = secy->icv_len;
884 macsec_skb_cb(skb)->valid = false;
885 skb = skb_share_check(skb, GFP_ATOMIC);
887 return ERR_PTR(-ENOMEM);
889 ret = skb_cow_data(skb, 0, &trailer);
890 if (unlikely(ret < 0)) {
894 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
897 return ERR_PTR(-ENOMEM);
900 hdr = (struct macsec_eth_header *)skb->data;
901 hdr_pn = ntohl(hdr->packet_number);
904 pn_t recovered_pn = rx_sa->next_pn_halves;
906 recovered_pn.lower = hdr_pn;
907 if (hdr_pn < rx_sa->next_pn_halves.lower &&
908 !pn_same_half(hdr_pn, rx_sa->next_pn_halves.lower))
909 recovered_pn.upper++;
911 macsec_fill_iv_xpn(iv, rx_sa->ssci, recovered_pn.full64,
914 macsec_fill_iv(iv, sci, hdr_pn);
917 sg_init_table(sg, ret);
918 ret = skb_to_sgvec(skb, sg, 0, skb->len);
919 if (unlikely(ret < 0)) {
920 aead_request_free(req);
925 if (hdr->tci_an & MACSEC_TCI_E) {
926 /* confidentiality: ethernet + macsec header
927 * authenticated, encrypted payload
929 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
931 aead_request_set_crypt(req, sg, sg, len, iv);
932 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
933 skb = skb_unshare(skb, GFP_ATOMIC);
935 aead_request_free(req);
936 return ERR_PTR(-ENOMEM);
939 /* integrity only: all headers + data authenticated */
940 aead_request_set_crypt(req, sg, sg, icv_len, iv);
941 aead_request_set_ad(req, skb->len - icv_len);
944 macsec_skb_cb(skb)->req = req;
946 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
949 ret = crypto_aead_decrypt(req);
950 if (ret == -EINPROGRESS) {
952 } else if (ret != 0) {
953 /* decryption/authentication failed
954 * 10.6 if validateFrames is disabled, deliver anyway
956 if (ret != -EBADMSG) {
961 macsec_skb_cb(skb)->valid = true;
965 aead_request_free(req);
970 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
972 struct macsec_rx_sc *rx_sc;
974 for_each_rxsc(secy, rx_sc) {
975 if (rx_sc->sci == sci)
982 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
984 struct macsec_rx_sc *rx_sc;
986 for_each_rxsc_rtnl(secy, rx_sc) {
987 if (rx_sc->sci == sci)
994 static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
996 /* Deliver to the uncontrolled port by default */
997 enum rx_handler_result ret = RX_HANDLER_PASS;
998 struct ethhdr *hdr = eth_hdr(skb);
999 struct metadata_dst *md_dst;
1000 struct macsec_rxh_data *rxd;
1001 struct macsec_dev *macsec;
1004 rxd = macsec_data_rcu(skb->dev);
1005 md_dst = skb_metadata_dst(skb);
1007 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1008 struct sk_buff *nskb;
1009 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1010 struct net_device *ndev = macsec->secy.netdev;
1012 /* If h/w offloading is enabled, HW decodes frames and strips
1013 * the SecTAG, so we have to deduce which port to deliver to.
1015 if (macsec_is_offloaded(macsec) && netif_running(ndev)) {
1016 struct macsec_rx_sc *rx_sc = NULL;
1018 if (md_dst && md_dst->type == METADATA_MACSEC)
1019 rx_sc = find_rx_sc(&macsec->secy, md_dst->u.macsec_info.sci);
1021 if (md_dst && md_dst->type == METADATA_MACSEC && !rx_sc)
1024 if (ether_addr_equal_64bits(hdr->h_dest,
1026 /* exact match, divert skb to this port */
1028 skb->pkt_type = PACKET_HOST;
1029 ret = RX_HANDLER_ANOTHER;
1031 } else if (is_multicast_ether_addr_64bits(
1033 /* multicast frame, deliver on this port too */
1034 nskb = skb_clone(skb, GFP_ATOMIC);
1039 if (ether_addr_equal_64bits(hdr->h_dest,
1041 nskb->pkt_type = PACKET_BROADCAST;
1043 nskb->pkt_type = PACKET_MULTICAST;
1046 } else if (rx_sc || ndev->flags & IFF_PROMISC) {
1048 skb->pkt_type = PACKET_HOST;
1049 ret = RX_HANDLER_ANOTHER;
1056 /* 10.6 If the management control validateFrames is not
1057 * Strict, frames without a SecTAG are received, counted, and
1058 * delivered to the Controlled Port
1060 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1061 u64_stats_update_begin(&secy_stats->syncp);
1062 secy_stats->stats.InPktsNoTag++;
1063 u64_stats_update_end(&secy_stats->syncp);
1064 DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1068 /* deliver on this port */
1069 nskb = skb_clone(skb, GFP_ATOMIC);
1075 if (__netif_rx(nskb) == NET_RX_SUCCESS) {
1076 u64_stats_update_begin(&secy_stats->syncp);
1077 secy_stats->stats.InPktsUntagged++;
1078 u64_stats_update_end(&secy_stats->syncp);
1087 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1089 struct sk_buff *skb = *pskb;
1090 struct net_device *dev = skb->dev;
1091 struct macsec_eth_header *hdr;
1092 struct macsec_secy *secy = NULL;
1093 struct macsec_rx_sc *rx_sc;
1094 struct macsec_rx_sa *rx_sa;
1095 struct macsec_rxh_data *rxd;
1096 struct macsec_dev *macsec;
1101 struct pcpu_rx_sc_stats *rxsc_stats;
1102 struct pcpu_secy_stats *secy_stats;
1106 if (skb_headroom(skb) < ETH_HLEN)
1109 hdr = macsec_ethhdr(skb);
1110 if (hdr->eth.h_proto != htons(ETH_P_MACSEC))
1111 return handle_not_macsec(skb);
1113 skb = skb_unshare(skb, GFP_ATOMIC);
1116 return RX_HANDLER_CONSUMED;
1118 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1120 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1124 hdr = macsec_ethhdr(skb);
1126 /* Frames with a SecTAG that has the TCI E bit set but the C
1127 * bit clear are discarded, as this reserved encoding is used
1128 * to identify frames with a SecTAG that are not to be
1129 * delivered to the Controlled Port.
1131 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1132 return RX_HANDLER_PASS;
1134 /* now, pull the extra length */
1135 if (hdr->tci_an & MACSEC_TCI_SC) {
1140 /* ethernet header is part of crypto processing */
1141 skb_push(skb, ETH_HLEN);
1143 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1144 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1145 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1148 rxd = macsec_data_rcu(skb->dev);
1150 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1151 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1153 sc = sc ? macsec_rxsc_get(sc) : NULL;
1156 secy = &macsec->secy;
1166 macsec = macsec_priv(dev);
1167 secy_stats = this_cpu_ptr(macsec->stats);
1168 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1170 if (!macsec_validate_skb(skb, secy->icv_len, secy->xpn)) {
1171 u64_stats_update_begin(&secy_stats->syncp);
1172 secy_stats->stats.InPktsBadTag++;
1173 u64_stats_update_end(&secy_stats->syncp);
1174 DEV_STATS_INC(secy->netdev, rx_errors);
1178 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1180 /* 10.6.1 if the SA is not in use */
1182 /* If validateFrames is Strict or the C bit in the
1183 * SecTAG is set, discard
1185 struct macsec_rx_sa *active_rx_sa = macsec_active_rxsa_get(rx_sc);
1186 if (hdr->tci_an & MACSEC_TCI_C ||
1187 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1188 u64_stats_update_begin(&rxsc_stats->syncp);
1189 rxsc_stats->stats.InPktsNotUsingSA++;
1190 u64_stats_update_end(&rxsc_stats->syncp);
1191 DEV_STATS_INC(secy->netdev, rx_errors);
1193 this_cpu_inc(active_rx_sa->stats->InPktsNotUsingSA);
1197 /* not Strict, the frame (with the SecTAG and ICV
1198 * removed) is delivered to the Controlled Port.
1200 u64_stats_update_begin(&rxsc_stats->syncp);
1201 rxsc_stats->stats.InPktsUnusedSA++;
1202 u64_stats_update_end(&rxsc_stats->syncp);
1204 this_cpu_inc(active_rx_sa->stats->InPktsUnusedSA);
1208 /* First, PN check to avoid decrypting obviously wrong packets */
1209 hdr_pn = ntohl(hdr->packet_number);
1210 if (secy->replay_protect) {
1213 spin_lock(&rx_sa->lock);
1214 late = rx_sa->next_pn_halves.lower >= secy->replay_window &&
1215 hdr_pn < (rx_sa->next_pn_halves.lower - secy->replay_window);
1218 late = late && pn_same_half(rx_sa->next_pn_halves.lower, hdr_pn);
1219 spin_unlock(&rx_sa->lock);
1222 u64_stats_update_begin(&rxsc_stats->syncp);
1223 rxsc_stats->stats.InPktsLate++;
1224 u64_stats_update_end(&rxsc_stats->syncp);
1225 DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1230 macsec_skb_cb(skb)->rx_sa = rx_sa;
1232 /* Disabled && !changed text => skip validation */
1233 if (hdr->tci_an & MACSEC_TCI_C ||
1234 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1235 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1238 /* the decrypt callback needs the reference */
1239 if (PTR_ERR(skb) != -EINPROGRESS) {
1240 macsec_rxsa_put(rx_sa);
1241 macsec_rxsc_put(rx_sc);
1245 return RX_HANDLER_CONSUMED;
1248 if (!macsec_post_decrypt(skb, secy, hdr_pn))
1252 macsec_finalize_skb(skb, secy->icv_len,
1253 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1255 macsec_reset_skb(skb, secy->netdev);
1258 macsec_rxsa_put(rx_sa);
1259 macsec_rxsc_put(rx_sc);
1262 ret = gro_cells_receive(&macsec->gro_cells, skb);
1263 if (ret == NET_RX_SUCCESS)
1266 DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1271 return RX_HANDLER_CONSUMED;
1274 macsec_rxsa_put(rx_sa);
1276 macsec_rxsc_put(rx_sc);
1281 return RX_HANDLER_CONSUMED;
1284 /* 10.6.1 if the SC is not found */
1285 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1287 macsec_finalize_skb(skb, MACSEC_DEFAULT_ICV_LEN,
1288 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1290 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1291 struct sk_buff *nskb;
1293 secy_stats = this_cpu_ptr(macsec->stats);
1295 /* If validateFrames is Strict or the C bit in the
1296 * SecTAG is set, discard
1299 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1300 u64_stats_update_begin(&secy_stats->syncp);
1301 secy_stats->stats.InPktsNoSCI++;
1302 u64_stats_update_end(&secy_stats->syncp);
1303 DEV_STATS_INC(macsec->secy.netdev, rx_errors);
1307 /* not strict, the frame (with the SecTAG and ICV
1308 * removed) is delivered to the Controlled Port.
1310 nskb = skb_clone(skb, GFP_ATOMIC);
1314 macsec_reset_skb(nskb, macsec->secy.netdev);
1316 ret = __netif_rx(nskb);
1317 if (ret == NET_RX_SUCCESS) {
1318 u64_stats_update_begin(&secy_stats->syncp);
1319 secy_stats->stats.InPktsUnknownSCI++;
1320 u64_stats_update_end(&secy_stats->syncp);
1322 DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1328 return RX_HANDLER_PASS;
1331 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1333 struct crypto_aead *tfm;
1336 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
1341 ret = crypto_aead_setkey(tfm, key, key_len);
1345 ret = crypto_aead_setauthsize(tfm, icv_len);
1351 crypto_free_aead(tfm);
1352 return ERR_PTR(ret);
1355 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1358 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1362 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1363 if (IS_ERR(rx_sa->key.tfm)) {
1364 free_percpu(rx_sa->stats);
1365 return PTR_ERR(rx_sa->key.tfm);
1368 rx_sa->ssci = MACSEC_UNDEF_SSCI;
1369 rx_sa->active = false;
1371 refcount_set(&rx_sa->refcnt, 1);
1372 spin_lock_init(&rx_sa->lock);
1377 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1379 rx_sa->active = false;
1381 macsec_rxsa_put(rx_sa);
1384 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1388 for (i = 0; i < MACSEC_NUM_AN; i++) {
1389 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1391 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1396 macsec_rxsc_put(rx_sc);
1399 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1401 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1403 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1405 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1406 if (rx_sc->sci == sci) {
1409 rcu_assign_pointer(*rx_scp, rx_sc->next);
1417 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci,
1420 struct macsec_rx_sc *rx_sc;
1421 struct macsec_dev *macsec;
1422 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1423 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1424 struct macsec_secy *secy;
1426 list_for_each_entry(macsec, &rxd->secys, secys) {
1427 if (find_rx_sc_rtnl(&macsec->secy, sci))
1428 return ERR_PTR(-EEXIST);
1431 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1433 return ERR_PTR(-ENOMEM);
1435 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1436 if (!rx_sc->stats) {
1438 return ERR_PTR(-ENOMEM);
1442 rx_sc->active = active;
1443 refcount_set(&rx_sc->refcnt, 1);
1445 secy = &macsec_priv(dev)->secy;
1446 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1447 rcu_assign_pointer(secy->rx_sc, rx_sc);
1455 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1458 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1462 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1463 if (IS_ERR(tx_sa->key.tfm)) {
1464 free_percpu(tx_sa->stats);
1465 return PTR_ERR(tx_sa->key.tfm);
1468 tx_sa->ssci = MACSEC_UNDEF_SSCI;
1469 tx_sa->active = false;
1470 refcount_set(&tx_sa->refcnt, 1);
1471 spin_lock_init(&tx_sa->lock);
1476 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1478 tx_sa->active = false;
1480 macsec_txsa_put(tx_sa);
1483 static struct genl_family macsec_fam;
1485 static struct net_device *get_dev_from_nl(struct net *net,
1486 struct nlattr **attrs)
1488 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1489 struct net_device *dev;
1491 dev = __dev_get_by_index(net, ifindex);
1493 return ERR_PTR(-ENODEV);
1495 if (!netif_is_macsec(dev))
1496 return ERR_PTR(-ENODEV);
1501 static enum macsec_offload nla_get_offload(const struct nlattr *nla)
1503 return (__force enum macsec_offload)nla_get_u8(nla);
1506 static sci_t nla_get_sci(const struct nlattr *nla)
1508 return (__force sci_t)nla_get_u64(nla);
1511 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1514 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1517 static ssci_t nla_get_ssci(const struct nlattr *nla)
1519 return (__force ssci_t)nla_get_u32(nla);
1522 static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value)
1524 return nla_put_u32(skb, attrtype, (__force u64)value);
1527 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1528 struct nlattr **attrs,
1529 struct nlattr **tb_sa,
1530 struct net_device **devp,
1531 struct macsec_secy **secyp,
1532 struct macsec_tx_sc **scp,
1535 struct net_device *dev;
1536 struct macsec_secy *secy;
1537 struct macsec_tx_sc *tx_sc;
1538 struct macsec_tx_sa *tx_sa;
1540 if (!tb_sa[MACSEC_SA_ATTR_AN])
1541 return ERR_PTR(-EINVAL);
1543 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1545 dev = get_dev_from_nl(net, attrs);
1547 return ERR_CAST(dev);
1549 if (*assoc_num >= MACSEC_NUM_AN)
1550 return ERR_PTR(-EINVAL);
1552 secy = &macsec_priv(dev)->secy;
1553 tx_sc = &secy->tx_sc;
1555 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1557 return ERR_PTR(-ENODEV);
1565 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1566 struct nlattr **attrs,
1567 struct nlattr **tb_rxsc,
1568 struct net_device **devp,
1569 struct macsec_secy **secyp)
1571 struct net_device *dev;
1572 struct macsec_secy *secy;
1573 struct macsec_rx_sc *rx_sc;
1576 dev = get_dev_from_nl(net, attrs);
1578 return ERR_CAST(dev);
1580 secy = &macsec_priv(dev)->secy;
1582 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1583 return ERR_PTR(-EINVAL);
1585 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1586 rx_sc = find_rx_sc_rtnl(secy, sci);
1588 return ERR_PTR(-ENODEV);
1596 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1597 struct nlattr **attrs,
1598 struct nlattr **tb_rxsc,
1599 struct nlattr **tb_sa,
1600 struct net_device **devp,
1601 struct macsec_secy **secyp,
1602 struct macsec_rx_sc **scp,
1605 struct macsec_rx_sc *rx_sc;
1606 struct macsec_rx_sa *rx_sa;
1608 if (!tb_sa[MACSEC_SA_ATTR_AN])
1609 return ERR_PTR(-EINVAL);
1611 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1612 if (*assoc_num >= MACSEC_NUM_AN)
1613 return ERR_PTR(-EINVAL);
1615 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1617 return ERR_CAST(rx_sc);
1619 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1621 return ERR_PTR(-ENODEV);
1627 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1628 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1629 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1630 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1631 [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
1634 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1635 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1636 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1639 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1640 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1641 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1642 [MACSEC_SA_ATTR_PN] = NLA_POLICY_MIN_LEN(4),
1643 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1644 .len = MACSEC_KEYID_LEN, },
1645 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1646 .len = MACSEC_MAX_KEY_LEN, },
1647 [MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 },
1648 [MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY,
1649 .len = MACSEC_SALT_LEN, },
1652 static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
1653 [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 },
1656 /* Offloads an operation to a device driver */
1657 static int macsec_offload(int (* const func)(struct macsec_context *),
1658 struct macsec_context *ctx)
1662 if (unlikely(!func))
1665 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1666 mutex_lock(&ctx->phydev->lock);
1670 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1671 mutex_unlock(&ctx->phydev->lock);
1676 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1678 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1681 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1687 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1689 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1692 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1698 static bool validate_add_rxsa(struct nlattr **attrs)
1700 if (!attrs[MACSEC_SA_ATTR_AN] ||
1701 !attrs[MACSEC_SA_ATTR_KEY] ||
1702 !attrs[MACSEC_SA_ATTR_KEYID])
1705 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1708 if (attrs[MACSEC_SA_ATTR_PN] &&
1709 nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
1712 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1713 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1717 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1723 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1725 struct net_device *dev;
1726 struct nlattr **attrs = info->attrs;
1727 struct macsec_secy *secy;
1728 struct macsec_rx_sc *rx_sc;
1729 struct macsec_rx_sa *rx_sa;
1730 unsigned char assoc_num;
1732 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1733 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1736 if (!attrs[MACSEC_ATTR_IFINDEX])
1739 if (parse_sa_config(attrs, tb_sa))
1742 if (parse_rxsc_config(attrs, tb_rxsc))
1745 if (!validate_add_rxsa(tb_sa))
1749 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1750 if (IS_ERR(rx_sc)) {
1752 return PTR_ERR(rx_sc);
1755 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1757 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1758 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1759 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1764 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1765 if (tb_sa[MACSEC_SA_ATTR_PN] &&
1766 nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1767 pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
1768 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1774 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1779 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
1780 pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n",
1781 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
1788 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1794 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1800 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1801 secy->key_len, secy->icv_len);
1808 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1809 spin_lock_bh(&rx_sa->lock);
1810 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
1811 spin_unlock_bh(&rx_sa->lock);
1814 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1815 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1820 rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
1821 nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
1825 /* If h/w offloading is available, propagate to the device */
1826 if (macsec_is_offloaded(netdev_priv(dev))) {
1827 const struct macsec_ops *ops;
1828 struct macsec_context ctx;
1830 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1836 ctx.sa.assoc_num = assoc_num;
1837 ctx.sa.rx_sa = rx_sa;
1839 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1842 err = macsec_offload(ops->mdo_add_rxsa, &ctx);
1843 memzero_explicit(ctx.sa.key, secy->key_len);
1848 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1849 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1856 macsec_rxsa_put(rx_sa);
1861 static bool validate_add_rxsc(struct nlattr **attrs)
1863 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1866 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1867 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1874 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1876 struct net_device *dev;
1877 sci_t sci = MACSEC_UNDEF_SCI;
1878 struct nlattr **attrs = info->attrs;
1879 struct macsec_rx_sc *rx_sc;
1880 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1881 struct macsec_secy *secy;
1885 if (!attrs[MACSEC_ATTR_IFINDEX])
1888 if (parse_rxsc_config(attrs, tb_rxsc))
1891 if (!validate_add_rxsc(tb_rxsc))
1895 dev = get_dev_from_nl(genl_info_net(info), attrs);
1898 return PTR_ERR(dev);
1901 secy = &macsec_priv(dev)->secy;
1902 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1904 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1905 active = nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1907 rx_sc = create_rx_sc(dev, sci, active);
1908 if (IS_ERR(rx_sc)) {
1910 return PTR_ERR(rx_sc);
1913 if (macsec_is_offloaded(netdev_priv(dev))) {
1914 const struct macsec_ops *ops;
1915 struct macsec_context ctx;
1917 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1926 ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
1936 del_rx_sc(secy, sci);
1942 static bool validate_add_txsa(struct nlattr **attrs)
1944 if (!attrs[MACSEC_SA_ATTR_AN] ||
1945 !attrs[MACSEC_SA_ATTR_PN] ||
1946 !attrs[MACSEC_SA_ATTR_KEY] ||
1947 !attrs[MACSEC_SA_ATTR_KEYID])
1950 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1953 if (nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
1956 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1957 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1961 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1967 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1969 struct net_device *dev;
1970 struct nlattr **attrs = info->attrs;
1971 struct macsec_secy *secy;
1972 struct macsec_tx_sc *tx_sc;
1973 struct macsec_tx_sa *tx_sa;
1974 unsigned char assoc_num;
1976 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1977 bool was_operational;
1980 if (!attrs[MACSEC_ATTR_IFINDEX])
1983 if (parse_sa_config(attrs, tb_sa))
1986 if (!validate_add_txsa(tb_sa))
1990 dev = get_dev_from_nl(genl_info_net(info), attrs);
1993 return PTR_ERR(dev);
1996 secy = &macsec_priv(dev)->secy;
1997 tx_sc = &secy->tx_sc;
1999 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
2001 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
2002 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
2003 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
2008 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2009 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2010 pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
2011 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2017 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
2022 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
2023 pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n",
2024 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
2031 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
2037 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
2043 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2044 secy->key_len, secy->icv_len);
2051 spin_lock_bh(&tx_sa->lock);
2052 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2053 spin_unlock_bh(&tx_sa->lock);
2055 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2056 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2058 was_operational = secy->operational;
2059 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
2060 secy->operational = true;
2063 tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
2064 nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
2068 /* If h/w offloading is available, propagate to the device */
2069 if (macsec_is_offloaded(netdev_priv(dev))) {
2070 const struct macsec_ops *ops;
2071 struct macsec_context ctx;
2073 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2079 ctx.sa.assoc_num = assoc_num;
2080 ctx.sa.tx_sa = tx_sa;
2082 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2085 err = macsec_offload(ops->mdo_add_txsa, &ctx);
2086 memzero_explicit(ctx.sa.key, secy->key_len);
2091 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
2092 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
2099 secy->operational = was_operational;
2100 macsec_txsa_put(tx_sa);
2105 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
2107 struct nlattr **attrs = info->attrs;
2108 struct net_device *dev;
2109 struct macsec_secy *secy;
2110 struct macsec_rx_sc *rx_sc;
2111 struct macsec_rx_sa *rx_sa;
2113 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2114 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2117 if (!attrs[MACSEC_ATTR_IFINDEX])
2120 if (parse_sa_config(attrs, tb_sa))
2123 if (parse_rxsc_config(attrs, tb_rxsc))
2127 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2128 &dev, &secy, &rx_sc, &assoc_num);
2129 if (IS_ERR(rx_sa)) {
2131 return PTR_ERR(rx_sa);
2134 if (rx_sa->active) {
2139 /* If h/w offloading is available, propagate to the device */
2140 if (macsec_is_offloaded(netdev_priv(dev))) {
2141 const struct macsec_ops *ops;
2142 struct macsec_context ctx;
2144 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2150 ctx.sa.assoc_num = assoc_num;
2151 ctx.sa.rx_sa = rx_sa;
2154 ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
2159 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
2171 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
2173 struct nlattr **attrs = info->attrs;
2174 struct net_device *dev;
2175 struct macsec_secy *secy;
2176 struct macsec_rx_sc *rx_sc;
2178 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2181 if (!attrs[MACSEC_ATTR_IFINDEX])
2184 if (parse_rxsc_config(attrs, tb_rxsc))
2187 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2191 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
2194 return PTR_ERR(dev);
2197 secy = &macsec_priv(dev)->secy;
2198 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2200 rx_sc = del_rx_sc(secy, sci);
2206 /* If h/w offloading is available, propagate to the device */
2207 if (macsec_is_offloaded(netdev_priv(dev))) {
2208 const struct macsec_ops *ops;
2209 struct macsec_context ctx;
2211 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2219 ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
2234 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2236 struct nlattr **attrs = info->attrs;
2237 struct net_device *dev;
2238 struct macsec_secy *secy;
2239 struct macsec_tx_sc *tx_sc;
2240 struct macsec_tx_sa *tx_sa;
2242 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2245 if (!attrs[MACSEC_ATTR_IFINDEX])
2248 if (parse_sa_config(attrs, tb_sa))
2252 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2253 &dev, &secy, &tx_sc, &assoc_num);
2254 if (IS_ERR(tx_sa)) {
2256 return PTR_ERR(tx_sa);
2259 if (tx_sa->active) {
2264 /* If h/w offloading is available, propagate to the device */
2265 if (macsec_is_offloaded(netdev_priv(dev))) {
2266 const struct macsec_ops *ops;
2267 struct macsec_context ctx;
2269 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2275 ctx.sa.assoc_num = assoc_num;
2276 ctx.sa.tx_sa = tx_sa;
2279 ret = macsec_offload(ops->mdo_del_txsa, &ctx);
2284 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2296 static bool validate_upd_sa(struct nlattr **attrs)
2298 if (!attrs[MACSEC_SA_ATTR_AN] ||
2299 attrs[MACSEC_SA_ATTR_KEY] ||
2300 attrs[MACSEC_SA_ATTR_KEYID] ||
2301 attrs[MACSEC_SA_ATTR_SSCI] ||
2302 attrs[MACSEC_SA_ATTR_SALT])
2305 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2308 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
2311 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2312 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2319 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2321 struct nlattr **attrs = info->attrs;
2322 struct net_device *dev;
2323 struct macsec_secy *secy;
2324 struct macsec_tx_sc *tx_sc;
2325 struct macsec_tx_sa *tx_sa;
2327 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2328 bool was_operational, was_active;
2334 if (!attrs[MACSEC_ATTR_IFINDEX])
2337 if (parse_sa_config(attrs, tb_sa))
2340 if (!validate_upd_sa(tb_sa))
2344 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2345 &dev, &secy, &tx_sc, &assoc_num);
2346 if (IS_ERR(tx_sa)) {
2348 return PTR_ERR(tx_sa);
2351 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2354 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2355 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2356 pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
2357 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2362 spin_lock_bh(&tx_sa->lock);
2363 prev_pn = tx_sa->next_pn_halves;
2364 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2365 spin_unlock_bh(&tx_sa->lock);
2368 was_active = tx_sa->active;
2369 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2370 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2372 was_operational = secy->operational;
2373 if (assoc_num == tx_sc->encoding_sa)
2374 secy->operational = tx_sa->active;
2376 /* If h/w offloading is available, propagate to the device */
2377 if (macsec_is_offloaded(netdev_priv(dev))) {
2378 const struct macsec_ops *ops;
2379 struct macsec_context ctx;
2381 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2387 ctx.sa.assoc_num = assoc_num;
2388 ctx.sa.tx_sa = tx_sa;
2389 ctx.sa.update_pn = !!prev_pn.full64;
2392 ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
2402 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2403 spin_lock_bh(&tx_sa->lock);
2404 tx_sa->next_pn_halves = prev_pn;
2405 spin_unlock_bh(&tx_sa->lock);
2407 tx_sa->active = was_active;
2408 secy->operational = was_operational;
2413 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2415 struct nlattr **attrs = info->attrs;
2416 struct net_device *dev;
2417 struct macsec_secy *secy;
2418 struct macsec_rx_sc *rx_sc;
2419 struct macsec_rx_sa *rx_sa;
2421 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2422 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2429 if (!attrs[MACSEC_ATTR_IFINDEX])
2432 if (parse_rxsc_config(attrs, tb_rxsc))
2435 if (parse_sa_config(attrs, tb_sa))
2438 if (!validate_upd_sa(tb_sa))
2442 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2443 &dev, &secy, &rx_sc, &assoc_num);
2444 if (IS_ERR(rx_sa)) {
2446 return PTR_ERR(rx_sa);
2449 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2452 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2453 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2454 pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
2455 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2460 spin_lock_bh(&rx_sa->lock);
2461 prev_pn = rx_sa->next_pn_halves;
2462 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2463 spin_unlock_bh(&rx_sa->lock);
2466 was_active = rx_sa->active;
2467 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2468 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2470 /* If h/w offloading is available, propagate to the device */
2471 if (macsec_is_offloaded(netdev_priv(dev))) {
2472 const struct macsec_ops *ops;
2473 struct macsec_context ctx;
2475 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2481 ctx.sa.assoc_num = assoc_num;
2482 ctx.sa.rx_sa = rx_sa;
2483 ctx.sa.update_pn = !!prev_pn.full64;
2486 ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
2495 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2496 spin_lock_bh(&rx_sa->lock);
2497 rx_sa->next_pn_halves = prev_pn;
2498 spin_unlock_bh(&rx_sa->lock);
2500 rx_sa->active = was_active;
2505 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2507 struct nlattr **attrs = info->attrs;
2508 struct net_device *dev;
2509 struct macsec_secy *secy;
2510 struct macsec_rx_sc *rx_sc;
2511 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2512 unsigned int prev_n_rx_sc;
2516 if (!attrs[MACSEC_ATTR_IFINDEX])
2519 if (parse_rxsc_config(attrs, tb_rxsc))
2522 if (!validate_add_rxsc(tb_rxsc))
2526 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2527 if (IS_ERR(rx_sc)) {
2529 return PTR_ERR(rx_sc);
2532 was_active = rx_sc->active;
2533 prev_n_rx_sc = secy->n_rx_sc;
2534 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2535 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2537 if (rx_sc->active != new)
2538 secy->n_rx_sc += new ? 1 : -1;
2540 rx_sc->active = new;
2543 /* If h/w offloading is available, propagate to the device */
2544 if (macsec_is_offloaded(netdev_priv(dev))) {
2545 const struct macsec_ops *ops;
2546 struct macsec_context ctx;
2548 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2557 ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
2567 secy->n_rx_sc = prev_n_rx_sc;
2568 rx_sc->active = was_active;
2573 static bool macsec_is_configured(struct macsec_dev *macsec)
2575 struct macsec_secy *secy = &macsec->secy;
2576 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2582 for (i = 0; i < MACSEC_NUM_AN; i++)
2589 static bool macsec_needs_tx_tag(struct macsec_dev *macsec,
2590 const struct macsec_ops *ops)
2592 return macsec->offload == MACSEC_OFFLOAD_PHY &&
2593 ops->mdo_insert_tx_tag;
2596 static void macsec_set_head_tail_room(struct net_device *dev)
2598 struct macsec_dev *macsec = macsec_priv(dev);
2599 struct net_device *real_dev = macsec->real_dev;
2600 int needed_headroom, needed_tailroom;
2601 const struct macsec_ops *ops;
2603 ops = macsec_get_ops(macsec, NULL);
2605 needed_headroom = ops->needed_headroom;
2606 needed_tailroom = ops->needed_tailroom;
2608 needed_headroom = MACSEC_NEEDED_HEADROOM;
2609 needed_tailroom = MACSEC_NEEDED_TAILROOM;
2612 dev->needed_headroom = real_dev->needed_headroom + needed_headroom;
2613 dev->needed_tailroom = real_dev->needed_tailroom + needed_tailroom;
2616 static int macsec_update_offload(struct net_device *dev, enum macsec_offload offload)
2618 enum macsec_offload prev_offload;
2619 const struct macsec_ops *ops;
2620 struct macsec_context ctx;
2621 struct macsec_dev *macsec;
2624 macsec = macsec_priv(dev);
2626 /* Check if the offloading mode is supported by the underlying layers */
2627 if (offload != MACSEC_OFFLOAD_OFF &&
2628 !macsec_check_offload(offload, macsec))
2631 /* Check if the net device is busy. */
2632 if (netif_running(dev))
2635 /* Check if the device already has rules configured: we do not support
2638 if (macsec_is_configured(macsec))
2641 prev_offload = macsec->offload;
2643 ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
2648 macsec->offload = offload;
2650 ctx.secy = &macsec->secy;
2651 ret = offload == MACSEC_OFFLOAD_OFF ? macsec_offload(ops->mdo_del_secy, &ctx)
2652 : macsec_offload(ops->mdo_add_secy, &ctx);
2654 macsec->offload = prev_offload;
2658 macsec_set_head_tail_room(dev);
2659 macsec->insert_tx_tag = macsec_needs_tx_tag(macsec, ops);
2664 static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
2666 struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
2667 struct nlattr **attrs = info->attrs;
2668 enum macsec_offload offload;
2669 struct macsec_dev *macsec;
2670 struct net_device *dev;
2673 if (!attrs[MACSEC_ATTR_IFINDEX])
2676 if (!attrs[MACSEC_ATTR_OFFLOAD])
2679 if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
2680 attrs[MACSEC_ATTR_OFFLOAD],
2681 macsec_genl_offload_policy, NULL))
2686 dev = get_dev_from_nl(genl_info_net(info), attrs);
2691 macsec = macsec_priv(dev);
2693 if (!tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]) {
2698 offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
2700 if (macsec->offload != offload)
2701 ret = macsec_update_offload(dev, offload);
2707 static void get_tx_sa_stats(struct net_device *dev, int an,
2708 struct macsec_tx_sa *tx_sa,
2709 struct macsec_tx_sa_stats *sum)
2711 struct macsec_dev *macsec = macsec_priv(dev);
2714 /* If h/w offloading is available, propagate to the device */
2715 if (macsec_is_offloaded(macsec)) {
2716 const struct macsec_ops *ops;
2717 struct macsec_context ctx;
2719 ops = macsec_get_ops(macsec, &ctx);
2721 ctx.sa.assoc_num = an;
2722 ctx.sa.tx_sa = tx_sa;
2723 ctx.stats.tx_sa_stats = sum;
2724 ctx.secy = &macsec_priv(dev)->secy;
2725 macsec_offload(ops->mdo_get_tx_sa_stats, &ctx);
2730 for_each_possible_cpu(cpu) {
2731 const struct macsec_tx_sa_stats *stats =
2732 per_cpu_ptr(tx_sa->stats, cpu);
2734 sum->OutPktsProtected += stats->OutPktsProtected;
2735 sum->OutPktsEncrypted += stats->OutPktsEncrypted;
2739 static int copy_tx_sa_stats(struct sk_buff *skb, struct macsec_tx_sa_stats *sum)
2741 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED,
2742 sum->OutPktsProtected) ||
2743 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2744 sum->OutPktsEncrypted))
2750 static void get_rx_sa_stats(struct net_device *dev,
2751 struct macsec_rx_sc *rx_sc, int an,
2752 struct macsec_rx_sa *rx_sa,
2753 struct macsec_rx_sa_stats *sum)
2755 struct macsec_dev *macsec = macsec_priv(dev);
2758 /* If h/w offloading is available, propagate to the device */
2759 if (macsec_is_offloaded(macsec)) {
2760 const struct macsec_ops *ops;
2761 struct macsec_context ctx;
2763 ops = macsec_get_ops(macsec, &ctx);
2765 ctx.sa.assoc_num = an;
2766 ctx.sa.rx_sa = rx_sa;
2767 ctx.stats.rx_sa_stats = sum;
2768 ctx.secy = &macsec_priv(dev)->secy;
2770 macsec_offload(ops->mdo_get_rx_sa_stats, &ctx);
2775 for_each_possible_cpu(cpu) {
2776 const struct macsec_rx_sa_stats *stats =
2777 per_cpu_ptr(rx_sa->stats, cpu);
2779 sum->InPktsOK += stats->InPktsOK;
2780 sum->InPktsInvalid += stats->InPktsInvalid;
2781 sum->InPktsNotValid += stats->InPktsNotValid;
2782 sum->InPktsNotUsingSA += stats->InPktsNotUsingSA;
2783 sum->InPktsUnusedSA += stats->InPktsUnusedSA;
2787 static int copy_rx_sa_stats(struct sk_buff *skb,
2788 struct macsec_rx_sa_stats *sum)
2790 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum->InPktsOK) ||
2791 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID,
2792 sum->InPktsInvalid) ||
2793 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID,
2794 sum->InPktsNotValid) ||
2795 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2796 sum->InPktsNotUsingSA) ||
2797 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA,
2798 sum->InPktsUnusedSA))
2804 static void get_rx_sc_stats(struct net_device *dev,
2805 struct macsec_rx_sc *rx_sc,
2806 struct macsec_rx_sc_stats *sum)
2808 struct macsec_dev *macsec = macsec_priv(dev);
2811 /* If h/w offloading is available, propagate to the device */
2812 if (macsec_is_offloaded(macsec)) {
2813 const struct macsec_ops *ops;
2814 struct macsec_context ctx;
2816 ops = macsec_get_ops(macsec, &ctx);
2818 ctx.stats.rx_sc_stats = sum;
2819 ctx.secy = &macsec_priv(dev)->secy;
2821 macsec_offload(ops->mdo_get_rx_sc_stats, &ctx);
2826 for_each_possible_cpu(cpu) {
2827 const struct pcpu_rx_sc_stats *stats;
2828 struct macsec_rx_sc_stats tmp;
2831 stats = per_cpu_ptr(rx_sc->stats, cpu);
2833 start = u64_stats_fetch_begin(&stats->syncp);
2834 memcpy(&tmp, &stats->stats, sizeof(tmp));
2835 } while (u64_stats_fetch_retry(&stats->syncp, start));
2837 sum->InOctetsValidated += tmp.InOctetsValidated;
2838 sum->InOctetsDecrypted += tmp.InOctetsDecrypted;
2839 sum->InPktsUnchecked += tmp.InPktsUnchecked;
2840 sum->InPktsDelayed += tmp.InPktsDelayed;
2841 sum->InPktsOK += tmp.InPktsOK;
2842 sum->InPktsInvalid += tmp.InPktsInvalid;
2843 sum->InPktsLate += tmp.InPktsLate;
2844 sum->InPktsNotValid += tmp.InPktsNotValid;
2845 sum->InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2846 sum->InPktsUnusedSA += tmp.InPktsUnusedSA;
2850 static int copy_rx_sc_stats(struct sk_buff *skb, struct macsec_rx_sc_stats *sum)
2852 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2853 sum->InOctetsValidated,
2854 MACSEC_RXSC_STATS_ATTR_PAD) ||
2855 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2856 sum->InOctetsDecrypted,
2857 MACSEC_RXSC_STATS_ATTR_PAD) ||
2858 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2859 sum->InPktsUnchecked,
2860 MACSEC_RXSC_STATS_ATTR_PAD) ||
2861 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2863 MACSEC_RXSC_STATS_ATTR_PAD) ||
2864 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2866 MACSEC_RXSC_STATS_ATTR_PAD) ||
2867 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2869 MACSEC_RXSC_STATS_ATTR_PAD) ||
2870 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2872 MACSEC_RXSC_STATS_ATTR_PAD) ||
2873 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2874 sum->InPktsNotValid,
2875 MACSEC_RXSC_STATS_ATTR_PAD) ||
2876 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2877 sum->InPktsNotUsingSA,
2878 MACSEC_RXSC_STATS_ATTR_PAD) ||
2879 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2880 sum->InPktsUnusedSA,
2881 MACSEC_RXSC_STATS_ATTR_PAD))
2887 static void get_tx_sc_stats(struct net_device *dev,
2888 struct macsec_tx_sc_stats *sum)
2890 struct macsec_dev *macsec = macsec_priv(dev);
2893 /* If h/w offloading is available, propagate to the device */
2894 if (macsec_is_offloaded(macsec)) {
2895 const struct macsec_ops *ops;
2896 struct macsec_context ctx;
2898 ops = macsec_get_ops(macsec, &ctx);
2900 ctx.stats.tx_sc_stats = sum;
2901 ctx.secy = &macsec_priv(dev)->secy;
2902 macsec_offload(ops->mdo_get_tx_sc_stats, &ctx);
2907 for_each_possible_cpu(cpu) {
2908 const struct pcpu_tx_sc_stats *stats;
2909 struct macsec_tx_sc_stats tmp;
2912 stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu);
2914 start = u64_stats_fetch_begin(&stats->syncp);
2915 memcpy(&tmp, &stats->stats, sizeof(tmp));
2916 } while (u64_stats_fetch_retry(&stats->syncp, start));
2918 sum->OutPktsProtected += tmp.OutPktsProtected;
2919 sum->OutPktsEncrypted += tmp.OutPktsEncrypted;
2920 sum->OutOctetsProtected += tmp.OutOctetsProtected;
2921 sum->OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2925 static int copy_tx_sc_stats(struct sk_buff *skb, struct macsec_tx_sc_stats *sum)
2927 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2928 sum->OutPktsProtected,
2929 MACSEC_TXSC_STATS_ATTR_PAD) ||
2930 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2931 sum->OutPktsEncrypted,
2932 MACSEC_TXSC_STATS_ATTR_PAD) ||
2933 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2934 sum->OutOctetsProtected,
2935 MACSEC_TXSC_STATS_ATTR_PAD) ||
2936 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2937 sum->OutOctetsEncrypted,
2938 MACSEC_TXSC_STATS_ATTR_PAD))
2944 static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum)
2946 struct macsec_dev *macsec = macsec_priv(dev);
2949 /* If h/w offloading is available, propagate to the device */
2950 if (macsec_is_offloaded(macsec)) {
2951 const struct macsec_ops *ops;
2952 struct macsec_context ctx;
2954 ops = macsec_get_ops(macsec, &ctx);
2956 ctx.stats.dev_stats = sum;
2957 ctx.secy = &macsec_priv(dev)->secy;
2958 macsec_offload(ops->mdo_get_dev_stats, &ctx);
2963 for_each_possible_cpu(cpu) {
2964 const struct pcpu_secy_stats *stats;
2965 struct macsec_dev_stats tmp;
2968 stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu);
2970 start = u64_stats_fetch_begin(&stats->syncp);
2971 memcpy(&tmp, &stats->stats, sizeof(tmp));
2972 } while (u64_stats_fetch_retry(&stats->syncp, start));
2974 sum->OutPktsUntagged += tmp.OutPktsUntagged;
2975 sum->InPktsUntagged += tmp.InPktsUntagged;
2976 sum->OutPktsTooLong += tmp.OutPktsTooLong;
2977 sum->InPktsNoTag += tmp.InPktsNoTag;
2978 sum->InPktsBadTag += tmp.InPktsBadTag;
2979 sum->InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2980 sum->InPktsNoSCI += tmp.InPktsNoSCI;
2981 sum->InPktsOverrun += tmp.InPktsOverrun;
2985 static int copy_secy_stats(struct sk_buff *skb, struct macsec_dev_stats *sum)
2987 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2988 sum->OutPktsUntagged,
2989 MACSEC_SECY_STATS_ATTR_PAD) ||
2990 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2991 sum->InPktsUntagged,
2992 MACSEC_SECY_STATS_ATTR_PAD) ||
2993 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2994 sum->OutPktsTooLong,
2995 MACSEC_SECY_STATS_ATTR_PAD) ||
2996 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2998 MACSEC_SECY_STATS_ATTR_PAD) ||
2999 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
3001 MACSEC_SECY_STATS_ATTR_PAD) ||
3002 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
3003 sum->InPktsUnknownSCI,
3004 MACSEC_SECY_STATS_ATTR_PAD) ||
3005 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
3007 MACSEC_SECY_STATS_ATTR_PAD) ||
3008 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
3010 MACSEC_SECY_STATS_ATTR_PAD))
3016 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
3018 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3019 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
3026 switch (secy->key_len) {
3027 case MACSEC_GCM_AES_128_SAK_LEN:
3028 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
3030 case MACSEC_GCM_AES_256_SAK_LEN:
3031 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
3037 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
3038 MACSEC_SECY_ATTR_PAD) ||
3039 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
3040 csid, MACSEC_SECY_ATTR_PAD) ||
3041 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
3042 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
3043 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
3044 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
3045 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
3046 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
3047 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
3048 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
3049 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
3050 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
3053 if (secy->replay_protect) {
3054 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
3058 nla_nest_end(skb, secy_nest);
3062 nla_nest_cancel(skb, secy_nest);
3066 static noinline_for_stack int
3067 dump_secy(struct macsec_secy *secy, struct net_device *dev,
3068 struct sk_buff *skb, struct netlink_callback *cb)
3070 struct macsec_tx_sc_stats tx_sc_stats = {0, };
3071 struct macsec_tx_sa_stats tx_sa_stats = {0, };
3072 struct macsec_rx_sc_stats rx_sc_stats = {0, };
3073 struct macsec_rx_sa_stats rx_sa_stats = {0, };
3074 struct macsec_dev *macsec = netdev_priv(dev);
3075 struct macsec_dev_stats dev_stats = {0, };
3076 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3077 struct nlattr *txsa_list, *rxsc_list;
3078 struct macsec_rx_sc *rx_sc;
3079 struct nlattr *attr;
3083 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3084 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
3088 genl_dump_check_consistent(cb, hdr);
3090 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
3091 goto nla_put_failure;
3093 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
3095 goto nla_put_failure;
3096 if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
3097 goto nla_put_failure;
3098 nla_nest_end(skb, attr);
3100 if (nla_put_secy(secy, skb))
3101 goto nla_put_failure;
3103 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
3105 goto nla_put_failure;
3107 get_tx_sc_stats(dev, &tx_sc_stats);
3108 if (copy_tx_sc_stats(skb, &tx_sc_stats)) {
3109 nla_nest_cancel(skb, attr);
3110 goto nla_put_failure;
3112 nla_nest_end(skb, attr);
3114 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
3116 goto nla_put_failure;
3117 get_secy_stats(dev, &dev_stats);
3118 if (copy_secy_stats(skb, &dev_stats)) {
3119 nla_nest_cancel(skb, attr);
3120 goto nla_put_failure;
3122 nla_nest_end(skb, attr);
3124 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
3126 goto nla_put_failure;
3127 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
3128 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
3129 struct nlattr *txsa_nest;
3136 txsa_nest = nla_nest_start_noflag(skb, j++);
3138 nla_nest_cancel(skb, txsa_list);
3139 goto nla_put_failure;
3142 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
3144 nla_nest_cancel(skb, txsa_nest);
3145 nla_nest_cancel(skb, txsa_list);
3146 goto nla_put_failure;
3148 memset(&tx_sa_stats, 0, sizeof(tx_sa_stats));
3149 get_tx_sa_stats(dev, i, tx_sa, &tx_sa_stats);
3150 if (copy_tx_sa_stats(skb, &tx_sa_stats)) {
3151 nla_nest_cancel(skb, attr);
3152 nla_nest_cancel(skb, txsa_nest);
3153 nla_nest_cancel(skb, txsa_list);
3154 goto nla_put_failure;
3156 nla_nest_end(skb, attr);
3159 pn = tx_sa->next_pn;
3160 pn_len = MACSEC_XPN_PN_LEN;
3162 pn = tx_sa->next_pn_halves.lower;
3163 pn_len = MACSEC_DEFAULT_PN_LEN;
3166 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3167 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3168 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
3169 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) ||
3170 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
3171 nla_nest_cancel(skb, txsa_nest);
3172 nla_nest_cancel(skb, txsa_list);
3173 goto nla_put_failure;
3176 nla_nest_end(skb, txsa_nest);
3178 nla_nest_end(skb, txsa_list);
3180 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
3182 goto nla_put_failure;
3185 for_each_rxsc_rtnl(secy, rx_sc) {
3187 struct nlattr *rxsa_list;
3188 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
3191 nla_nest_cancel(skb, rxsc_list);
3192 goto nla_put_failure;
3195 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
3196 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
3197 MACSEC_RXSC_ATTR_PAD)) {
3198 nla_nest_cancel(skb, rxsc_nest);
3199 nla_nest_cancel(skb, rxsc_list);
3200 goto nla_put_failure;
3203 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
3205 nla_nest_cancel(skb, rxsc_nest);
3206 nla_nest_cancel(skb, rxsc_list);
3207 goto nla_put_failure;
3209 memset(&rx_sc_stats, 0, sizeof(rx_sc_stats));
3210 get_rx_sc_stats(dev, rx_sc, &rx_sc_stats);
3211 if (copy_rx_sc_stats(skb, &rx_sc_stats)) {
3212 nla_nest_cancel(skb, attr);
3213 nla_nest_cancel(skb, rxsc_nest);
3214 nla_nest_cancel(skb, rxsc_list);
3215 goto nla_put_failure;
3217 nla_nest_end(skb, attr);
3219 rxsa_list = nla_nest_start_noflag(skb,
3220 MACSEC_RXSC_ATTR_SA_LIST);
3222 nla_nest_cancel(skb, rxsc_nest);
3223 nla_nest_cancel(skb, rxsc_list);
3224 goto nla_put_failure;
3227 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
3228 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
3229 struct nlattr *rxsa_nest;
3236 rxsa_nest = nla_nest_start_noflag(skb, k++);
3238 nla_nest_cancel(skb, rxsa_list);
3239 nla_nest_cancel(skb, rxsc_nest);
3240 nla_nest_cancel(skb, rxsc_list);
3241 goto nla_put_failure;
3244 attr = nla_nest_start_noflag(skb,
3245 MACSEC_SA_ATTR_STATS);
3247 nla_nest_cancel(skb, rxsa_list);
3248 nla_nest_cancel(skb, rxsc_nest);
3249 nla_nest_cancel(skb, rxsc_list);
3250 goto nla_put_failure;
3252 memset(&rx_sa_stats, 0, sizeof(rx_sa_stats));
3253 get_rx_sa_stats(dev, rx_sc, i, rx_sa, &rx_sa_stats);
3254 if (copy_rx_sa_stats(skb, &rx_sa_stats)) {
3255 nla_nest_cancel(skb, attr);
3256 nla_nest_cancel(skb, rxsa_list);
3257 nla_nest_cancel(skb, rxsc_nest);
3258 nla_nest_cancel(skb, rxsc_list);
3259 goto nla_put_failure;
3261 nla_nest_end(skb, attr);
3264 pn = rx_sa->next_pn;
3265 pn_len = MACSEC_XPN_PN_LEN;
3267 pn = rx_sa->next_pn_halves.lower;
3268 pn_len = MACSEC_DEFAULT_PN_LEN;
3271 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3272 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3273 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
3274 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) ||
3275 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
3276 nla_nest_cancel(skb, rxsa_nest);
3277 nla_nest_cancel(skb, rxsc_nest);
3278 nla_nest_cancel(skb, rxsc_list);
3279 goto nla_put_failure;
3281 nla_nest_end(skb, rxsa_nest);
3284 nla_nest_end(skb, rxsa_list);
3285 nla_nest_end(skb, rxsc_nest);
3288 nla_nest_end(skb, rxsc_list);
3290 genlmsg_end(skb, hdr);
3295 genlmsg_cancel(skb, hdr);
3299 static int macsec_generation = 1; /* protected by RTNL */
3301 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
3303 struct net *net = sock_net(skb->sk);
3304 struct net_device *dev;
3307 dev_idx = cb->args[0];
3312 cb->seq = macsec_generation;
3314 for_each_netdev(net, dev) {
3315 struct macsec_secy *secy;
3320 if (!netif_is_macsec(dev))
3323 secy = &macsec_priv(dev)->secy;
3324 if (dump_secy(secy, dev, skb, cb) < 0)
3336 static const struct genl_small_ops macsec_genl_ops[] = {
3338 .cmd = MACSEC_CMD_GET_TXSC,
3339 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3340 .dumpit = macsec_dump_txsc,
3343 .cmd = MACSEC_CMD_ADD_RXSC,
3344 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3345 .doit = macsec_add_rxsc,
3346 .flags = GENL_ADMIN_PERM,
3349 .cmd = MACSEC_CMD_DEL_RXSC,
3350 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3351 .doit = macsec_del_rxsc,
3352 .flags = GENL_ADMIN_PERM,
3355 .cmd = MACSEC_CMD_UPD_RXSC,
3356 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3357 .doit = macsec_upd_rxsc,
3358 .flags = GENL_ADMIN_PERM,
3361 .cmd = MACSEC_CMD_ADD_TXSA,
3362 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3363 .doit = macsec_add_txsa,
3364 .flags = GENL_ADMIN_PERM,
3367 .cmd = MACSEC_CMD_DEL_TXSA,
3368 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3369 .doit = macsec_del_txsa,
3370 .flags = GENL_ADMIN_PERM,
3373 .cmd = MACSEC_CMD_UPD_TXSA,
3374 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3375 .doit = macsec_upd_txsa,
3376 .flags = GENL_ADMIN_PERM,
3379 .cmd = MACSEC_CMD_ADD_RXSA,
3380 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3381 .doit = macsec_add_rxsa,
3382 .flags = GENL_ADMIN_PERM,
3385 .cmd = MACSEC_CMD_DEL_RXSA,
3386 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3387 .doit = macsec_del_rxsa,
3388 .flags = GENL_ADMIN_PERM,
3391 .cmd = MACSEC_CMD_UPD_RXSA,
3392 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3393 .doit = macsec_upd_rxsa,
3394 .flags = GENL_ADMIN_PERM,
3397 .cmd = MACSEC_CMD_UPD_OFFLOAD,
3398 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3399 .doit = macsec_upd_offload,
3400 .flags = GENL_ADMIN_PERM,
3404 static struct genl_family macsec_fam __ro_after_init = {
3405 .name = MACSEC_GENL_NAME,
3407 .version = MACSEC_GENL_VERSION,
3408 .maxattr = MACSEC_ATTR_MAX,
3409 .policy = macsec_genl_policy,
3411 .module = THIS_MODULE,
3412 .small_ops = macsec_genl_ops,
3413 .n_small_ops = ARRAY_SIZE(macsec_genl_ops),
3414 .resv_start_op = MACSEC_CMD_UPD_OFFLOAD + 1,
3417 static struct sk_buff *macsec_insert_tx_tag(struct sk_buff *skb,
3418 struct net_device *dev)
3420 struct macsec_dev *macsec = macsec_priv(dev);
3421 const struct macsec_ops *ops;
3422 struct phy_device *phydev;
3423 struct macsec_context ctx;
3427 ops = macsec_get_ops(macsec, &ctx);
3428 skb_final_len = skb->len - ETH_HLEN + ops->needed_headroom +
3429 ops->needed_tailroom;
3430 if (unlikely(skb_final_len > macsec->real_dev->mtu)) {
3435 phydev = macsec->real_dev->phydev;
3437 err = skb_ensure_writable_head_tail(skb, dev);
3438 if (unlikely(err < 0))
3441 err = ops->mdo_insert_tx_tag(phydev, skb);
3448 return ERR_PTR(err);
3451 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
3452 struct net_device *dev)
3454 struct macsec_dev *macsec = netdev_priv(dev);
3455 struct macsec_secy *secy = &macsec->secy;
3456 struct pcpu_secy_stats *secy_stats;
3459 if (macsec_is_offloaded(netdev_priv(dev))) {
3460 struct metadata_dst *md_dst = secy->tx_sc.md_dst;
3463 dst_hold(&md_dst->dst);
3464 skb_dst_set(skb, &md_dst->dst);
3466 if (macsec->insert_tx_tag) {
3467 skb = macsec_insert_tx_tag(skb, dev);
3469 DEV_STATS_INC(dev, tx_dropped);
3470 return NETDEV_TX_OK;
3474 skb->dev = macsec->real_dev;
3475 return dev_queue_xmit(skb);
3479 if (!secy->protect_frames) {
3480 secy_stats = this_cpu_ptr(macsec->stats);
3481 u64_stats_update_begin(&secy_stats->syncp);
3482 secy_stats->stats.OutPktsUntagged++;
3483 u64_stats_update_end(&secy_stats->syncp);
3484 skb->dev = macsec->real_dev;
3486 ret = dev_queue_xmit(skb);
3487 count_tx(dev, ret, len);
3491 if (!secy->operational) {
3493 DEV_STATS_INC(dev, tx_dropped);
3494 return NETDEV_TX_OK;
3498 skb = macsec_encrypt(skb, dev);
3500 if (PTR_ERR(skb) != -EINPROGRESS)
3501 DEV_STATS_INC(dev, tx_dropped);
3502 return NETDEV_TX_OK;
3505 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
3507 macsec_encrypt_finish(skb, dev);
3508 ret = dev_queue_xmit(skb);
3509 count_tx(dev, ret, len);
3513 #define MACSEC_FEATURES \
3514 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
3516 static int macsec_dev_init(struct net_device *dev)
3518 struct macsec_dev *macsec = macsec_priv(dev);
3519 struct net_device *real_dev = macsec->real_dev;
3522 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3526 err = gro_cells_init(&macsec->gro_cells, dev);
3528 free_percpu(dev->tstats);
3532 dev->features = real_dev->features & MACSEC_FEATURES;
3533 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
3535 macsec_set_head_tail_room(dev);
3537 if (is_zero_ether_addr(dev->dev_addr))
3538 eth_hw_addr_inherit(dev, real_dev);
3539 if (is_zero_ether_addr(dev->broadcast))
3540 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
3542 /* Get macsec's reference to real_dev */
3543 netdev_hold(real_dev, &macsec->dev_tracker, GFP_KERNEL);
3548 static void macsec_dev_uninit(struct net_device *dev)
3550 struct macsec_dev *macsec = macsec_priv(dev);
3552 gro_cells_destroy(&macsec->gro_cells);
3553 free_percpu(dev->tstats);
3556 static netdev_features_t macsec_fix_features(struct net_device *dev,
3557 netdev_features_t features)
3559 struct macsec_dev *macsec = macsec_priv(dev);
3560 struct net_device *real_dev = macsec->real_dev;
3562 features &= (real_dev->features & MACSEC_FEATURES) |
3563 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
3564 features |= NETIF_F_LLTX;
3569 static int macsec_dev_open(struct net_device *dev)
3571 struct macsec_dev *macsec = macsec_priv(dev);
3572 struct net_device *real_dev = macsec->real_dev;
3575 err = dev_uc_add(real_dev, dev->dev_addr);
3579 if (dev->flags & IFF_ALLMULTI) {
3580 err = dev_set_allmulti(real_dev, 1);
3585 if (dev->flags & IFF_PROMISC) {
3586 err = dev_set_promiscuity(real_dev, 1);
3588 goto clear_allmulti;
3591 /* If h/w offloading is available, propagate to the device */
3592 if (macsec_is_offloaded(macsec)) {
3593 const struct macsec_ops *ops;
3594 struct macsec_context ctx;
3596 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3599 goto clear_allmulti;
3602 ctx.secy = &macsec->secy;
3603 err = macsec_offload(ops->mdo_dev_open, &ctx);
3605 goto clear_allmulti;
3608 if (netif_carrier_ok(real_dev))
3609 netif_carrier_on(dev);
3613 if (dev->flags & IFF_ALLMULTI)
3614 dev_set_allmulti(real_dev, -1);
3616 dev_uc_del(real_dev, dev->dev_addr);
3617 netif_carrier_off(dev);
3621 static int macsec_dev_stop(struct net_device *dev)
3623 struct macsec_dev *macsec = macsec_priv(dev);
3624 struct net_device *real_dev = macsec->real_dev;
3626 netif_carrier_off(dev);
3628 /* If h/w offloading is available, propagate to the device */
3629 if (macsec_is_offloaded(macsec)) {
3630 const struct macsec_ops *ops;
3631 struct macsec_context ctx;
3633 ops = macsec_get_ops(macsec, &ctx);
3635 ctx.secy = &macsec->secy;
3636 macsec_offload(ops->mdo_dev_stop, &ctx);
3640 dev_mc_unsync(real_dev, dev);
3641 dev_uc_unsync(real_dev, dev);
3643 if (dev->flags & IFF_ALLMULTI)
3644 dev_set_allmulti(real_dev, -1);
3646 if (dev->flags & IFF_PROMISC)
3647 dev_set_promiscuity(real_dev, -1);
3649 dev_uc_del(real_dev, dev->dev_addr);
3654 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
3656 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3658 if (!(dev->flags & IFF_UP))
3661 if (change & IFF_ALLMULTI)
3662 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
3664 if (change & IFF_PROMISC)
3665 dev_set_promiscuity(real_dev,
3666 dev->flags & IFF_PROMISC ? 1 : -1);
3669 static void macsec_dev_set_rx_mode(struct net_device *dev)
3671 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3673 dev_mc_sync(real_dev, dev);
3674 dev_uc_sync(real_dev, dev);
3677 static int macsec_set_mac_address(struct net_device *dev, void *p)
3679 struct macsec_dev *macsec = macsec_priv(dev);
3680 struct net_device *real_dev = macsec->real_dev;
3681 struct sockaddr *addr = p;
3682 u8 old_addr[ETH_ALEN];
3685 if (!is_valid_ether_addr(addr->sa_data))
3686 return -EADDRNOTAVAIL;
3688 if (dev->flags & IFF_UP) {
3689 err = dev_uc_add(real_dev, addr->sa_data);
3694 ether_addr_copy(old_addr, dev->dev_addr);
3695 eth_hw_addr_set(dev, addr->sa_data);
3697 /* If h/w offloading is available, propagate to the device */
3698 if (macsec_is_offloaded(macsec)) {
3699 const struct macsec_ops *ops;
3700 struct macsec_context ctx;
3702 ops = macsec_get_ops(macsec, &ctx);
3705 goto restore_old_addr;
3708 ctx.secy = &macsec->secy;
3709 err = macsec_offload(ops->mdo_upd_secy, &ctx);
3711 goto restore_old_addr;
3714 if (dev->flags & IFF_UP)
3715 dev_uc_del(real_dev, old_addr);
3720 if (dev->flags & IFF_UP)
3721 dev_uc_del(real_dev, addr->sa_data);
3723 eth_hw_addr_set(dev, old_addr);
3728 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
3730 struct macsec_dev *macsec = macsec_priv(dev);
3731 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
3733 if (macsec->real_dev->mtu - extra < new_mtu)
3741 static void macsec_get_stats64(struct net_device *dev,
3742 struct rtnl_link_stats64 *s)
3747 dev_fetch_sw_netstats(s, dev->tstats);
3749 s->rx_dropped = DEV_STATS_READ(dev, rx_dropped);
3750 s->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
3751 s->rx_errors = DEV_STATS_READ(dev, rx_errors);
3754 static int macsec_get_iflink(const struct net_device *dev)
3756 return macsec_priv(dev)->real_dev->ifindex;
3759 static const struct net_device_ops macsec_netdev_ops = {
3760 .ndo_init = macsec_dev_init,
3761 .ndo_uninit = macsec_dev_uninit,
3762 .ndo_open = macsec_dev_open,
3763 .ndo_stop = macsec_dev_stop,
3764 .ndo_fix_features = macsec_fix_features,
3765 .ndo_change_mtu = macsec_change_mtu,
3766 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
3767 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
3768 .ndo_set_mac_address = macsec_set_mac_address,
3769 .ndo_start_xmit = macsec_start_xmit,
3770 .ndo_get_stats64 = macsec_get_stats64,
3771 .ndo_get_iflink = macsec_get_iflink,
3774 static const struct device_type macsec_type = {
3778 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3779 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3780 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3781 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3782 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3783 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3784 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3785 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3786 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3787 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3788 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
3789 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3790 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3791 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3792 [IFLA_MACSEC_OFFLOAD] = { .type = NLA_U8 },
3795 static void macsec_free_netdev(struct net_device *dev)
3797 struct macsec_dev *macsec = macsec_priv(dev);
3799 if (macsec->secy.tx_sc.md_dst)
3800 metadata_dst_free(macsec->secy.tx_sc.md_dst);
3801 free_percpu(macsec->stats);
3802 free_percpu(macsec->secy.tx_sc.stats);
3804 /* Get rid of the macsec's reference to real_dev */
3805 netdev_put(macsec->real_dev, &macsec->dev_tracker);
3808 static void macsec_setup(struct net_device *dev)
3812 dev->max_mtu = ETH_MAX_MTU;
3813 dev->priv_flags |= IFF_NO_QUEUE;
3814 dev->netdev_ops = &macsec_netdev_ops;
3815 dev->needs_free_netdev = true;
3816 dev->priv_destructor = macsec_free_netdev;
3817 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3819 eth_zero_addr(dev->broadcast);
3822 static int macsec_changelink_common(struct net_device *dev,
3823 struct nlattr *data[])
3825 struct macsec_secy *secy;
3826 struct macsec_tx_sc *tx_sc;
3828 secy = &macsec_priv(dev)->secy;
3829 tx_sc = &secy->tx_sc;
3831 if (data[IFLA_MACSEC_ENCODING_SA]) {
3832 struct macsec_tx_sa *tx_sa;
3834 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3835 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3837 secy->operational = tx_sa && tx_sa->active;
3840 if (data[IFLA_MACSEC_ENCRYPT])
3841 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3843 if (data[IFLA_MACSEC_PROTECT])
3844 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3846 if (data[IFLA_MACSEC_INC_SCI])
3847 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3849 if (data[IFLA_MACSEC_ES])
3850 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3852 if (data[IFLA_MACSEC_SCB])
3853 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3855 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3856 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3858 if (data[IFLA_MACSEC_VALIDATION])
3859 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3861 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3862 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3863 case MACSEC_CIPHER_ID_GCM_AES_128:
3864 case MACSEC_DEFAULT_CIPHER_ID:
3865 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3868 case MACSEC_CIPHER_ID_GCM_AES_256:
3869 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3872 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3873 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3876 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3877 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3885 if (data[IFLA_MACSEC_WINDOW]) {
3886 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3888 /* IEEE 802.1AEbw-2013 10.7.8 - maximum replay window
3889 * for XPN cipher suites */
3891 secy->replay_window > MACSEC_XPN_MAX_REPLAY_WINDOW)
3898 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3899 struct nlattr *data[],
3900 struct netlink_ext_ack *extack)
3902 struct macsec_dev *macsec = macsec_priv(dev);
3903 bool macsec_offload_state_change = false;
3904 enum macsec_offload offload;
3905 struct macsec_tx_sc tx_sc;
3906 struct macsec_secy secy;
3912 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3913 data[IFLA_MACSEC_ICV_LEN] ||
3914 data[IFLA_MACSEC_SCI] ||
3915 data[IFLA_MACSEC_PORT])
3918 /* Keep a copy of unmodified secy and tx_sc, in case the offload
3919 * propagation fails, to revert macsec_changelink_common.
3921 memcpy(&secy, &macsec->secy, sizeof(secy));
3922 memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
3924 ret = macsec_changelink_common(dev, data);
3928 if (data[IFLA_MACSEC_OFFLOAD]) {
3929 offload = nla_get_u8(data[IFLA_MACSEC_OFFLOAD]);
3930 if (macsec->offload != offload) {
3931 macsec_offload_state_change = true;
3932 ret = macsec_update_offload(dev, offload);
3938 /* If h/w offloading is available, propagate to the device */
3939 if (!macsec_offload_state_change && macsec_is_offloaded(macsec)) {
3940 const struct macsec_ops *ops;
3941 struct macsec_context ctx;
3943 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3949 ctx.secy = &macsec->secy;
3950 ret = macsec_offload(ops->mdo_upd_secy, &ctx);
3958 memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
3959 memcpy(&macsec->secy, &secy, sizeof(secy));
3964 static void macsec_del_dev(struct macsec_dev *macsec)
3968 while (macsec->secy.rx_sc) {
3969 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3971 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3975 for (i = 0; i < MACSEC_NUM_AN; i++) {
3976 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3979 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3985 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3987 struct macsec_dev *macsec = macsec_priv(dev);
3988 struct net_device *real_dev = macsec->real_dev;
3990 /* If h/w offloading is available, propagate to the device */
3991 if (macsec_is_offloaded(macsec)) {
3992 const struct macsec_ops *ops;
3993 struct macsec_context ctx;
3995 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3997 ctx.secy = &macsec->secy;
3998 macsec_offload(ops->mdo_del_secy, &ctx);
4002 unregister_netdevice_queue(dev, head);
4003 list_del_rcu(&macsec->secys);
4004 macsec_del_dev(macsec);
4005 netdev_upper_dev_unlink(real_dev, dev);
4007 macsec_generation++;
4010 static void macsec_dellink(struct net_device *dev, struct list_head *head)
4012 struct macsec_dev *macsec = macsec_priv(dev);
4013 struct net_device *real_dev = macsec->real_dev;
4014 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
4016 macsec_common_dellink(dev, head);
4018 if (list_empty(&rxd->secys)) {
4019 netdev_rx_handler_unregister(real_dev);
4024 static int register_macsec_dev(struct net_device *real_dev,
4025 struct net_device *dev)
4027 struct macsec_dev *macsec = macsec_priv(dev);
4028 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
4033 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
4037 INIT_LIST_HEAD(&rxd->secys);
4039 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
4047 list_add_tail_rcu(&macsec->secys, &rxd->secys);
4051 static bool sci_exists(struct net_device *dev, sci_t sci)
4053 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
4054 struct macsec_dev *macsec;
4056 list_for_each_entry(macsec, &rxd->secys, secys) {
4057 if (macsec->secy.sci == sci)
4064 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
4066 return make_sci(dev->dev_addr, port);
4069 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
4071 struct macsec_dev *macsec = macsec_priv(dev);
4072 struct macsec_secy *secy = &macsec->secy;
4074 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
4078 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
4079 if (!secy->tx_sc.stats)
4082 secy->tx_sc.md_dst = metadata_dst_alloc(0, METADATA_MACSEC, GFP_KERNEL);
4083 if (!secy->tx_sc.md_dst)
4084 /* macsec and secy percpu stats will be freed when unregistering
4085 * net_device in macsec_free_netdev()
4089 if (sci == MACSEC_UNDEF_SCI)
4090 sci = dev_to_sci(dev, MACSEC_PORT_ES);
4093 secy->operational = true;
4094 secy->key_len = DEFAULT_SAK_LEN;
4095 secy->icv_len = icv_len;
4096 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
4097 secy->protect_frames = true;
4098 secy->replay_protect = false;
4099 secy->xpn = DEFAULT_XPN;
4102 secy->tx_sc.md_dst->u.macsec_info.sci = sci;
4103 secy->tx_sc.active = true;
4104 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
4105 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
4106 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
4107 secy->tx_sc.end_station = false;
4108 secy->tx_sc.scb = false;
4113 static struct lock_class_key macsec_netdev_addr_lock_key;
4115 static int macsec_newlink(struct net *net, struct net_device *dev,
4116 struct nlattr *tb[], struct nlattr *data[],
4117 struct netlink_ext_ack *extack)
4119 struct macsec_dev *macsec = macsec_priv(dev);
4120 rx_handler_func_t *rx_handler;
4121 u8 icv_len = MACSEC_DEFAULT_ICV_LEN;
4122 struct net_device *real_dev;
4128 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
4131 if (real_dev->type != ARPHRD_ETHER)
4134 dev->priv_flags |= IFF_MACSEC;
4136 macsec->real_dev = real_dev;
4138 if (data && data[IFLA_MACSEC_OFFLOAD])
4139 macsec->offload = nla_get_offload(data[IFLA_MACSEC_OFFLOAD]);
4141 /* MACsec offloading is off by default */
4142 macsec->offload = MACSEC_OFFLOAD_OFF;
4144 /* Check if the offloading mode is supported by the underlying layers */
4145 if (macsec->offload != MACSEC_OFFLOAD_OFF &&
4146 !macsec_check_offload(macsec->offload, macsec))
4149 /* send_sci must be set to true when transmit sci explicitly is set */
4150 if ((data && data[IFLA_MACSEC_SCI]) &&
4151 (data && data[IFLA_MACSEC_INC_SCI])) {
4152 u8 send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
4158 if (data && data[IFLA_MACSEC_ICV_LEN])
4159 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4160 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
4166 rx_handler = rtnl_dereference(real_dev->rx_handler);
4167 if (rx_handler && rx_handler != macsec_handle_frame)
4170 err = register_netdevice(dev);
4174 netdev_lockdep_set_classes(dev);
4175 lockdep_set_class(&dev->addr_list_lock,
4176 &macsec_netdev_addr_lock_key);
4178 err = netdev_upper_dev_link(real_dev, dev, extack);
4182 /* need to be already registered so that ->init has run and
4183 * the MAC addr is set
4185 if (data && data[IFLA_MACSEC_SCI])
4186 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
4187 else if (data && data[IFLA_MACSEC_PORT])
4188 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
4190 sci = dev_to_sci(dev, MACSEC_PORT_ES);
4192 if (rx_handler && sci_exists(real_dev, sci)) {
4197 err = macsec_add_dev(dev, sci, icv_len);
4202 err = macsec_changelink_common(dev, data);
4207 /* If h/w offloading is available, propagate to the device */
4208 if (macsec_is_offloaded(macsec)) {
4209 const struct macsec_ops *ops;
4210 struct macsec_context ctx;
4212 ops = macsec_get_ops(macsec, &ctx);
4214 ctx.secy = &macsec->secy;
4215 err = macsec_offload(ops->mdo_add_secy, &ctx);
4219 macsec->insert_tx_tag =
4220 macsec_needs_tx_tag(macsec, ops);
4224 err = register_macsec_dev(real_dev, dev);
4228 netif_stacked_transfer_operstate(real_dev, dev);
4229 linkwatch_fire_event(dev);
4231 macsec_generation++;
4236 macsec_del_dev(macsec);
4238 netdev_upper_dev_unlink(real_dev, dev);
4240 unregister_netdevice(dev);
4244 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
4245 struct netlink_ext_ack *extack)
4247 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
4248 u8 icv_len = MACSEC_DEFAULT_ICV_LEN;
4255 if (data[IFLA_MACSEC_CIPHER_SUITE])
4256 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
4258 if (data[IFLA_MACSEC_ICV_LEN]) {
4259 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4260 if (icv_len != MACSEC_DEFAULT_ICV_LEN) {
4261 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
4262 struct crypto_aead *dummy_tfm;
4264 dummy_tfm = macsec_alloc_tfm(dummy_key,
4267 if (IS_ERR(dummy_tfm))
4268 return PTR_ERR(dummy_tfm);
4269 crypto_free_aead(dummy_tfm);
4274 case MACSEC_CIPHER_ID_GCM_AES_128:
4275 case MACSEC_CIPHER_ID_GCM_AES_256:
4276 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
4277 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
4278 case MACSEC_DEFAULT_CIPHER_ID:
4279 if (icv_len < MACSEC_MIN_ICV_LEN ||
4280 icv_len > MACSEC_STD_ICV_LEN)
4287 if (data[IFLA_MACSEC_ENCODING_SA]) {
4288 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
4292 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
4293 flag < IFLA_MACSEC_VALIDATION;
4296 if (nla_get_u8(data[flag]) > 1)
4301 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
4302 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
4303 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
4305 if ((sci && (scb || es)) || (scb && es))
4308 if (data[IFLA_MACSEC_VALIDATION] &&
4309 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
4312 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
4313 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
4314 !data[IFLA_MACSEC_WINDOW])
4320 static struct net *macsec_get_link_net(const struct net_device *dev)
4322 return dev_net(macsec_priv(dev)->real_dev);
4325 struct net_device *macsec_get_real_dev(const struct net_device *dev)
4327 return macsec_priv(dev)->real_dev;
4329 EXPORT_SYMBOL_GPL(macsec_get_real_dev);
4331 bool macsec_netdev_is_offloaded(struct net_device *dev)
4333 return macsec_is_offloaded(macsec_priv(dev));
4335 EXPORT_SYMBOL_GPL(macsec_netdev_is_offloaded);
4337 static size_t macsec_get_size(const struct net_device *dev)
4339 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
4340 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
4341 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
4342 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
4343 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
4344 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
4345 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
4346 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
4347 nla_total_size(1) + /* IFLA_MACSEC_ES */
4348 nla_total_size(1) + /* IFLA_MACSEC_SCB */
4349 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
4350 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
4351 nla_total_size(1) + /* IFLA_MACSEC_OFFLOAD */
4355 static int macsec_fill_info(struct sk_buff *skb,
4356 const struct net_device *dev)
4358 struct macsec_tx_sc *tx_sc;
4359 struct macsec_dev *macsec;
4360 struct macsec_secy *secy;
4363 macsec = macsec_priv(dev);
4364 secy = &macsec->secy;
4365 tx_sc = &secy->tx_sc;
4367 switch (secy->key_len) {
4368 case MACSEC_GCM_AES_128_SAK_LEN:
4369 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
4371 case MACSEC_GCM_AES_256_SAK_LEN:
4372 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
4375 goto nla_put_failure;
4378 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
4380 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
4381 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
4382 csid, IFLA_MACSEC_PAD) ||
4383 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
4384 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
4385 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
4386 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
4387 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
4388 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
4389 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
4390 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
4391 nla_put_u8(skb, IFLA_MACSEC_OFFLOAD, macsec->offload) ||
4393 goto nla_put_failure;
4395 if (secy->replay_protect) {
4396 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
4397 goto nla_put_failure;
4406 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
4408 .priv_size = sizeof(struct macsec_dev),
4409 .maxtype = IFLA_MACSEC_MAX,
4410 .policy = macsec_rtnl_policy,
4411 .setup = macsec_setup,
4412 .validate = macsec_validate_attr,
4413 .newlink = macsec_newlink,
4414 .changelink = macsec_changelink,
4415 .dellink = macsec_dellink,
4416 .get_size = macsec_get_size,
4417 .fill_info = macsec_fill_info,
4418 .get_link_net = macsec_get_link_net,
4421 static bool is_macsec_master(struct net_device *dev)
4423 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
4426 static int macsec_notify(struct notifier_block *this, unsigned long event,
4429 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
4432 if (!is_macsec_master(real_dev))
4438 case NETDEV_CHANGE: {
4439 struct macsec_dev *m, *n;
4440 struct macsec_rxh_data *rxd;
4442 rxd = macsec_data_rtnl(real_dev);
4443 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4444 struct net_device *dev = m->secy.netdev;
4446 netif_stacked_transfer_operstate(real_dev, dev);
4450 case NETDEV_UNREGISTER: {
4451 struct macsec_dev *m, *n;
4452 struct macsec_rxh_data *rxd;
4454 rxd = macsec_data_rtnl(real_dev);
4455 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4456 macsec_common_dellink(m->secy.netdev, &head);
4459 netdev_rx_handler_unregister(real_dev);
4462 unregister_netdevice_many(&head);
4465 case NETDEV_CHANGEMTU: {
4466 struct macsec_dev *m;
4467 struct macsec_rxh_data *rxd;
4469 rxd = macsec_data_rtnl(real_dev);
4470 list_for_each_entry(m, &rxd->secys, secys) {
4471 struct net_device *dev = m->secy.netdev;
4472 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
4473 macsec_extra_len(true));
4476 dev_set_mtu(dev, mtu);
4484 static struct notifier_block macsec_notifier = {
4485 .notifier_call = macsec_notify,
4488 static int __init macsec_init(void)
4492 pr_info("MACsec IEEE 802.1AE\n");
4493 err = register_netdevice_notifier(&macsec_notifier);
4497 err = rtnl_link_register(&macsec_link_ops);
4501 err = genl_register_family(&macsec_fam);
4508 rtnl_link_unregister(&macsec_link_ops);
4510 unregister_netdevice_notifier(&macsec_notifier);
4514 static void __exit macsec_exit(void)
4516 genl_unregister_family(&macsec_fam);
4517 rtnl_link_unregister(&macsec_link_ops);
4518 unregister_netdevice_notifier(&macsec_notifier);
4522 module_init(macsec_init);
4523 module_exit(macsec_exit);
4525 MODULE_ALIAS_RTNL_LINK("macsec");
4526 MODULE_ALIAS_GENL_FAMILY("macsec");
4528 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
4529 MODULE_LICENSE("GPL v2");