2 * drivers/net/macsec.c - MACsec device
4 * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/types.h>
13 #include <linux/skbuff.h>
14 #include <linux/socket.h>
15 #include <linux/module.h>
16 #include <crypto/aead.h>
17 #include <linux/etherdevice.h>
18 #include <linux/rtnetlink.h>
19 #include <net/genetlink.h>
21 #include <net/gro_cells.h>
22 #include <linux/if_arp.h>
24 #include <uapi/linux/if_macsec.h>
26 typedef u64 __bitwise sci_t;
28 #define MACSEC_SCI_LEN 8
30 /* SecTAG length = macsec_eth_header without the optional SCI */
31 #define MACSEC_TAG_LEN 6
33 struct macsec_eth_header {
37 #if defined(__LITTLE_ENDIAN_BITFIELD)
40 #elif defined(__BIG_ENDIAN_BITFIELD)
44 #error "Please fix <asm/byteorder.h>"
47 u8 secure_channel_id[8]; /* optional */
50 #define MACSEC_TCI_VERSION 0x80
51 #define MACSEC_TCI_ES 0x40 /* end station */
52 #define MACSEC_TCI_SC 0x20 /* SCI present */
53 #define MACSEC_TCI_SCB 0x10 /* epon */
54 #define MACSEC_TCI_E 0x08 /* encryption */
55 #define MACSEC_TCI_C 0x04 /* changed text */
56 #define MACSEC_AN_MASK 0x03 /* association number */
57 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
59 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
60 #define MIN_NON_SHORT_LEN 48
62 #define GCM_AES_IV_LEN 12
63 #define DEFAULT_ICV_LEN 16
65 #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
67 #define for_each_rxsc(secy, sc) \
68 for (sc = rcu_dereference_bh(secy->rx_sc); \
70 sc = rcu_dereference_bh(sc->next))
71 #define for_each_rxsc_rtnl(secy, sc) \
72 for (sc = rtnl_dereference(secy->rx_sc); \
74 sc = rtnl_dereference(sc->next))
78 u8 secure_channel_id[8];
85 * struct macsec_key - SA key
86 * @id: user-provided key identifier
87 * @tfm: crypto struct, key storage
90 u8 id[MACSEC_KEYID_LEN];
91 struct crypto_aead *tfm;
94 struct macsec_rx_sc_stats {
95 __u64 InOctetsValidated;
96 __u64 InOctetsDecrypted;
97 __u64 InPktsUnchecked;
102 __u64 InPktsNotValid;
103 __u64 InPktsNotUsingSA;
104 __u64 InPktsUnusedSA;
107 struct macsec_rx_sa_stats {
110 __u32 InPktsNotValid;
111 __u32 InPktsNotUsingSA;
112 __u32 InPktsUnusedSA;
115 struct macsec_tx_sa_stats {
116 __u32 OutPktsProtected;
117 __u32 OutPktsEncrypted;
120 struct macsec_tx_sc_stats {
121 __u64 OutPktsProtected;
122 __u64 OutPktsEncrypted;
123 __u64 OutOctetsProtected;
124 __u64 OutOctetsEncrypted;
127 struct macsec_dev_stats {
128 __u64 OutPktsUntagged;
129 __u64 InPktsUntagged;
130 __u64 OutPktsTooLong;
133 __u64 InPktsUnknownSCI;
139 * struct macsec_rx_sa - receive secure association
141 * @next_pn: packet number expected for the next packet
142 * @lock: protects next_pn manipulations
143 * @key: key structure
144 * @stats: per-SA stats
146 struct macsec_rx_sa {
147 struct macsec_key key;
152 struct macsec_rx_sa_stats __percpu *stats;
153 struct macsec_rx_sc *sc;
157 struct pcpu_rx_sc_stats {
158 struct macsec_rx_sc_stats stats;
159 struct u64_stats_sync syncp;
163 * struct macsec_rx_sc - receive secure channel
164 * @sci: secure channel identifier for this SC
165 * @active: channel is active
166 * @sa: array of secure associations
167 * @stats: per-SC stats
169 struct macsec_rx_sc {
170 struct macsec_rx_sc __rcu *next;
173 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
174 struct pcpu_rx_sc_stats __percpu *stats;
176 struct rcu_head rcu_head;
180 * struct macsec_tx_sa - transmit secure association
182 * @next_pn: packet number to use for the next packet
183 * @lock: protects next_pn manipulations
184 * @key: key structure
185 * @stats: per-SA stats
187 struct macsec_tx_sa {
188 struct macsec_key key;
193 struct macsec_tx_sa_stats __percpu *stats;
197 struct pcpu_tx_sc_stats {
198 struct macsec_tx_sc_stats stats;
199 struct u64_stats_sync syncp;
203 * struct macsec_tx_sc - transmit secure channel
205 * @encoding_sa: association number of the SA currently in use
206 * @encrypt: encrypt packets on transmit, or authenticate only
207 * @send_sci: always include the SCI in the SecTAG
209 * @scb: single copy broadcast flag
210 * @sa: array of secure associations
211 * @stats: stats for this TXSC
213 struct macsec_tx_sc {
220 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
221 struct pcpu_tx_sc_stats __percpu *stats;
224 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
227 * struct macsec_secy - MACsec Security Entity
228 * @netdev: netdevice for this SecY
229 * @n_rx_sc: number of receive secure channels configured on this SecY
230 * @sci: secure channel identifier used for tx
231 * @key_len: length of keys used by the cipher suite
232 * @icv_len: length of ICV used by the cipher suite
233 * @validate_frames: validation mode
234 * @operational: MAC_Operational flag
235 * @protect_frames: enable protection for this SecY
236 * @replay_protect: enable packet number checks on receive
237 * @replay_window: size of the replay window
238 * @tx_sc: transmit secure channel
239 * @rx_sc: linked list of receive secure channels
242 struct net_device *netdev;
243 unsigned int n_rx_sc;
247 enum macsec_validation_type validate_frames;
252 struct macsec_tx_sc tx_sc;
253 struct macsec_rx_sc __rcu *rx_sc;
256 struct pcpu_secy_stats {
257 struct macsec_dev_stats stats;
258 struct u64_stats_sync syncp;
262 * struct macsec_dev - private data
264 * @real_dev: pointer to underlying netdevice
265 * @stats: MACsec device stats
266 * @secys: linked list of SecY's on the underlying device
269 struct macsec_secy secy;
270 struct net_device *real_dev;
271 struct pcpu_secy_stats __percpu *stats;
272 struct list_head secys;
273 struct gro_cells gro_cells;
274 unsigned int nest_level;
278 * struct macsec_rxh_data - rx_handler private argument
279 * @secys: linked list of SecY's on this underlying device
281 struct macsec_rxh_data {
282 struct list_head secys;
285 static struct macsec_dev *macsec_priv(const struct net_device *dev)
287 return (struct macsec_dev *)netdev_priv(dev);
290 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
292 return rcu_dereference_bh(dev->rx_handler_data);
295 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
297 return rtnl_dereference(dev->rx_handler_data);
301 struct aead_request *req;
303 struct macsec_tx_sa *tx_sa;
304 struct macsec_rx_sa *rx_sa;
311 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
313 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
315 if (!sa || !sa->active)
318 if (!atomic_inc_not_zero(&sa->refcnt))
324 static void free_rx_sc_rcu(struct rcu_head *head)
326 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
328 free_percpu(rx_sc->stats);
332 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
334 return atomic_inc_not_zero(&sc->refcnt) ? sc : NULL;
337 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
339 if (atomic_dec_and_test(&sc->refcnt))
340 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
343 static void free_rxsa(struct rcu_head *head)
345 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
347 crypto_free_aead(sa->key.tfm);
348 free_percpu(sa->stats);
352 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
354 if (atomic_dec_and_test(&sa->refcnt))
355 call_rcu(&sa->rcu, free_rxsa);
358 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
360 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
362 if (!sa || !sa->active)
365 if (!atomic_inc_not_zero(&sa->refcnt))
371 static void free_txsa(struct rcu_head *head)
373 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
375 crypto_free_aead(sa->key.tfm);
376 free_percpu(sa->stats);
380 static void macsec_txsa_put(struct macsec_tx_sa *sa)
382 if (atomic_dec_and_test(&sa->refcnt))
383 call_rcu(&sa->rcu, free_txsa);
386 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
388 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
389 return (struct macsec_cb *)skb->cb;
392 #define MACSEC_PORT_ES (htons(0x0001))
393 #define MACSEC_PORT_SCB (0x0000)
394 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
396 #define DEFAULT_SAK_LEN 16
397 #define DEFAULT_SEND_SCI true
398 #define DEFAULT_ENCRYPT false
399 #define DEFAULT_ENCODING_SA 0
401 static bool send_sci(const struct macsec_secy *secy)
403 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
405 return tx_sc->send_sci ||
406 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
409 static sci_t make_sci(u8 *addr, __be16 port)
413 memcpy(&sci, addr, ETH_ALEN);
414 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
419 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
424 memcpy(&sci, hdr->secure_channel_id,
425 sizeof(hdr->secure_channel_id));
427 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
432 static unsigned int macsec_sectag_len(bool sci_present)
434 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
437 static unsigned int macsec_hdr_len(bool sci_present)
439 return macsec_sectag_len(sci_present) + ETH_HLEN;
442 static unsigned int macsec_extra_len(bool sci_present)
444 return macsec_sectag_len(sci_present) + sizeof(__be16);
447 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
448 static void macsec_fill_sectag(struct macsec_eth_header *h,
449 const struct macsec_secy *secy, u32 pn,
452 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
454 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
455 h->eth.h_proto = htons(ETH_P_MACSEC);
458 h->tci_an |= MACSEC_TCI_SC;
459 memcpy(&h->secure_channel_id, &secy->sci,
460 sizeof(h->secure_channel_id));
462 if (tx_sc->end_station)
463 h->tci_an |= MACSEC_TCI_ES;
465 h->tci_an |= MACSEC_TCI_SCB;
468 h->packet_number = htonl(pn);
470 /* with GCM, C/E clear for !encrypt, both set for encrypt */
472 h->tci_an |= MACSEC_TCI_CONFID;
473 else if (secy->icv_len != DEFAULT_ICV_LEN)
474 h->tci_an |= MACSEC_TCI_C;
476 h->tci_an |= tx_sc->encoding_sa;
479 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
481 if (data_len < MIN_NON_SHORT_LEN)
482 h->short_length = data_len;
485 /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
486 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
488 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
489 int len = skb->len - 2 * ETH_ALEN;
490 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
492 /* a) It comprises at least 17 octets */
496 /* b) MACsec EtherType: already checked */
498 /* c) V bit is clear */
499 if (h->tci_an & MACSEC_TCI_VERSION)
502 /* d) ES or SCB => !SC */
503 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
504 (h->tci_an & MACSEC_TCI_SC))
507 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
511 /* rx.pn != 0 (figure 10-5) */
512 if (!h->packet_number)
515 /* length check, f) g) h) i) */
517 return len == extra_len + h->short_length;
518 return len >= extra_len + MIN_NON_SHORT_LEN;
521 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
522 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
524 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
526 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
529 gcm_iv->pn = htonl(pn);
532 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
534 return (struct macsec_eth_header *)skb_mac_header(skb);
537 static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
541 spin_lock_bh(&tx_sa->lock);
545 if (tx_sa->next_pn == 0) {
546 pr_debug("PN wrapped, transitioning to !oper\n");
547 tx_sa->active = false;
548 if (secy->protect_frames)
549 secy->operational = false;
551 spin_unlock_bh(&tx_sa->lock);
556 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
558 struct macsec_dev *macsec = netdev_priv(dev);
560 skb->dev = macsec->real_dev;
561 skb_reset_mac_header(skb);
562 skb->protocol = eth_hdr(skb)->h_proto;
565 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
566 struct macsec_tx_sa *tx_sa)
568 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
570 u64_stats_update_begin(&txsc_stats->syncp);
571 if (tx_sc->encrypt) {
572 txsc_stats->stats.OutOctetsEncrypted += skb->len;
573 txsc_stats->stats.OutPktsEncrypted++;
574 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
576 txsc_stats->stats.OutOctetsProtected += skb->len;
577 txsc_stats->stats.OutPktsProtected++;
578 this_cpu_inc(tx_sa->stats->OutPktsProtected);
580 u64_stats_update_end(&txsc_stats->syncp);
583 static void count_tx(struct net_device *dev, int ret, int len)
585 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
586 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
588 u64_stats_update_begin(&stats->syncp);
590 stats->tx_bytes += len;
591 u64_stats_update_end(&stats->syncp);
593 dev->stats.tx_dropped++;
597 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
599 struct sk_buff *skb = base->data;
600 struct net_device *dev = skb->dev;
601 struct macsec_dev *macsec = macsec_priv(dev);
602 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
605 aead_request_free(macsec_skb_cb(skb)->req);
608 macsec_encrypt_finish(skb, dev);
609 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
611 ret = dev_queue_xmit(skb);
612 count_tx(dev, ret, len);
613 rcu_read_unlock_bh();
619 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
621 struct scatterlist **sg,
624 size_t size, iv_offset, sg_offset;
625 struct aead_request *req;
628 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
630 size += GCM_AES_IV_LEN;
632 size = ALIGN(size, __alignof__(struct scatterlist));
634 size += sizeof(struct scatterlist) * num_frags;
636 tmp = kmalloc(size, GFP_ATOMIC);
640 *iv = (unsigned char *)(tmp + iv_offset);
641 *sg = (struct scatterlist *)(tmp + sg_offset);
644 aead_request_set_tfm(req, tfm);
649 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
650 struct net_device *dev)
653 struct scatterlist *sg;
654 struct sk_buff *trailer;
657 struct macsec_eth_header *hh;
658 size_t unprotected_len;
659 struct aead_request *req;
660 struct macsec_secy *secy;
661 struct macsec_tx_sc *tx_sc;
662 struct macsec_tx_sa *tx_sa;
663 struct macsec_dev *macsec = macsec_priv(dev);
667 secy = &macsec->secy;
668 tx_sc = &secy->tx_sc;
670 /* 10.5.1 TX SA assignment */
671 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
673 secy->operational = false;
675 return ERR_PTR(-EINVAL);
678 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
679 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
680 struct sk_buff *nskb = skb_copy_expand(skb,
681 MACSEC_NEEDED_HEADROOM,
682 MACSEC_NEEDED_TAILROOM,
688 macsec_txsa_put(tx_sa);
690 return ERR_PTR(-ENOMEM);
693 skb = skb_unshare(skb, GFP_ATOMIC);
695 macsec_txsa_put(tx_sa);
696 return ERR_PTR(-ENOMEM);
700 unprotected_len = skb->len;
702 sci_present = send_sci(secy);
703 hh = (struct macsec_eth_header *)skb_push(skb, macsec_extra_len(sci_present));
704 memmove(hh, eth, 2 * ETH_ALEN);
706 pn = tx_sa_update_pn(tx_sa, secy);
708 macsec_txsa_put(tx_sa);
710 return ERR_PTR(-ENOLINK);
712 macsec_fill_sectag(hh, secy, pn, sci_present);
713 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
715 skb_put(skb, secy->icv_len);
717 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
718 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
720 u64_stats_update_begin(&secy_stats->syncp);
721 secy_stats->stats.OutPktsTooLong++;
722 u64_stats_update_end(&secy_stats->syncp);
724 macsec_txsa_put(tx_sa);
726 return ERR_PTR(-EINVAL);
729 ret = skb_cow_data(skb, 0, &trailer);
730 if (unlikely(ret < 0)) {
731 macsec_txsa_put(tx_sa);
736 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
738 macsec_txsa_put(tx_sa);
740 return ERR_PTR(-ENOMEM);
743 macsec_fill_iv(iv, secy->sci, pn);
745 sg_init_table(sg, ret);
746 ret = skb_to_sgvec(skb, sg, 0, skb->len);
747 if (unlikely(ret < 0)) {
748 aead_request_free(req);
749 macsec_txsa_put(tx_sa);
754 if (tx_sc->encrypt) {
755 int len = skb->len - macsec_hdr_len(sci_present) -
757 aead_request_set_crypt(req, sg, sg, len, iv);
758 aead_request_set_ad(req, macsec_hdr_len(sci_present));
760 aead_request_set_crypt(req, sg, sg, 0, iv);
761 aead_request_set_ad(req, skb->len - secy->icv_len);
764 macsec_skb_cb(skb)->req = req;
765 macsec_skb_cb(skb)->tx_sa = tx_sa;
766 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
769 ret = crypto_aead_encrypt(req);
770 if (ret == -EINPROGRESS) {
772 } else if (ret != 0) {
775 aead_request_free(req);
776 macsec_txsa_put(tx_sa);
777 return ERR_PTR(-EINVAL);
781 aead_request_free(req);
782 macsec_txsa_put(tx_sa);
787 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
789 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
790 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
791 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
794 spin_lock(&rx_sa->lock);
795 if (rx_sa->next_pn >= secy->replay_window)
796 lowest_pn = rx_sa->next_pn - secy->replay_window;
798 /* Now perform replay protection check again
799 * (see IEEE 802.1AE-2006 figure 10-5)
801 if (secy->replay_protect && pn < lowest_pn) {
802 spin_unlock(&rx_sa->lock);
803 u64_stats_update_begin(&rxsc_stats->syncp);
804 rxsc_stats->stats.InPktsLate++;
805 u64_stats_update_end(&rxsc_stats->syncp);
809 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
810 u64_stats_update_begin(&rxsc_stats->syncp);
811 if (hdr->tci_an & MACSEC_TCI_E)
812 rxsc_stats->stats.InOctetsDecrypted += skb->len;
814 rxsc_stats->stats.InOctetsValidated += skb->len;
815 u64_stats_update_end(&rxsc_stats->syncp);
818 if (!macsec_skb_cb(skb)->valid) {
819 spin_unlock(&rx_sa->lock);
822 if (hdr->tci_an & MACSEC_TCI_C ||
823 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
824 u64_stats_update_begin(&rxsc_stats->syncp);
825 rxsc_stats->stats.InPktsNotValid++;
826 u64_stats_update_end(&rxsc_stats->syncp);
830 u64_stats_update_begin(&rxsc_stats->syncp);
831 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
832 rxsc_stats->stats.InPktsInvalid++;
833 this_cpu_inc(rx_sa->stats->InPktsInvalid);
834 } else if (pn < lowest_pn) {
835 rxsc_stats->stats.InPktsDelayed++;
837 rxsc_stats->stats.InPktsUnchecked++;
839 u64_stats_update_end(&rxsc_stats->syncp);
841 u64_stats_update_begin(&rxsc_stats->syncp);
842 if (pn < lowest_pn) {
843 rxsc_stats->stats.InPktsDelayed++;
845 rxsc_stats->stats.InPktsOK++;
846 this_cpu_inc(rx_sa->stats->InPktsOK);
848 u64_stats_update_end(&rxsc_stats->syncp);
850 if (pn >= rx_sa->next_pn)
851 rx_sa->next_pn = pn + 1;
852 spin_unlock(&rx_sa->lock);
858 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
860 skb->pkt_type = PACKET_HOST;
861 skb->protocol = eth_type_trans(skb, dev);
863 skb_reset_network_header(skb);
864 if (!skb_transport_header_was_set(skb))
865 skb_reset_transport_header(skb);
866 skb_reset_mac_len(skb);
869 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
871 skb->ip_summed = CHECKSUM_NONE;
872 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
873 skb_pull(skb, hdr_len);
874 pskb_trim_unique(skb, skb->len - icv_len);
877 static void count_rx(struct net_device *dev, int len)
879 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
881 u64_stats_update_begin(&stats->syncp);
883 stats->rx_bytes += len;
884 u64_stats_update_end(&stats->syncp);
887 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
889 struct sk_buff *skb = base->data;
890 struct net_device *dev = skb->dev;
891 struct macsec_dev *macsec = macsec_priv(dev);
892 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
893 struct macsec_rx_sc *rx_sc = rx_sa->sc;
897 aead_request_free(macsec_skb_cb(skb)->req);
900 pn = ntohl(macsec_ethhdr(skb)->packet_number);
901 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
902 rcu_read_unlock_bh();
907 macsec_finalize_skb(skb, macsec->secy.icv_len,
908 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
909 macsec_reset_skb(skb, macsec->secy.netdev);
912 ret = gro_cells_receive(&macsec->gro_cells, skb);
913 if (ret == NET_RX_SUCCESS)
916 macsec->secy.netdev->stats.rx_dropped++;
918 rcu_read_unlock_bh();
921 macsec_rxsa_put(rx_sa);
922 macsec_rxsc_put(rx_sc);
926 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
927 struct net_device *dev,
928 struct macsec_rx_sa *rx_sa,
930 struct macsec_secy *secy)
933 struct scatterlist *sg;
934 struct sk_buff *trailer;
936 struct aead_request *req;
937 struct macsec_eth_header *hdr;
938 u16 icv_len = secy->icv_len;
940 macsec_skb_cb(skb)->valid = false;
941 skb = skb_share_check(skb, GFP_ATOMIC);
943 return ERR_PTR(-ENOMEM);
945 ret = skb_cow_data(skb, 0, &trailer);
946 if (unlikely(ret < 0)) {
950 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
953 return ERR_PTR(-ENOMEM);
956 hdr = (struct macsec_eth_header *)skb->data;
957 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
959 sg_init_table(sg, ret);
960 ret = skb_to_sgvec(skb, sg, 0, skb->len);
961 if (unlikely(ret < 0)) {
962 aead_request_free(req);
967 if (hdr->tci_an & MACSEC_TCI_E) {
968 /* confidentiality: ethernet + macsec header
969 * authenticated, encrypted payload
971 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
973 aead_request_set_crypt(req, sg, sg, len, iv);
974 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
975 skb = skb_unshare(skb, GFP_ATOMIC);
977 aead_request_free(req);
978 return ERR_PTR(-ENOMEM);
981 /* integrity only: all headers + data authenticated */
982 aead_request_set_crypt(req, sg, sg, icv_len, iv);
983 aead_request_set_ad(req, skb->len - icv_len);
986 macsec_skb_cb(skb)->req = req;
988 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
991 ret = crypto_aead_decrypt(req);
992 if (ret == -EINPROGRESS) {
994 } else if (ret != 0) {
995 /* decryption/authentication failed
996 * 10.6 if validateFrames is disabled, deliver anyway
998 if (ret != -EBADMSG) {
1003 macsec_skb_cb(skb)->valid = true;
1007 aead_request_free(req);
1012 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
1014 struct macsec_rx_sc *rx_sc;
1016 for_each_rxsc(secy, rx_sc) {
1017 if (rx_sc->sci == sci)
1024 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1026 struct macsec_rx_sc *rx_sc;
1028 for_each_rxsc_rtnl(secy, rx_sc) {
1029 if (rx_sc->sci == sci)
1036 static void handle_not_macsec(struct sk_buff *skb)
1038 struct macsec_rxh_data *rxd;
1039 struct macsec_dev *macsec;
1042 rxd = macsec_data_rcu(skb->dev);
1044 /* 10.6 If the management control validateFrames is not
1045 * Strict, frames without a SecTAG are received, counted, and
1046 * delivered to the Controlled Port
1048 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1049 struct sk_buff *nskb;
1051 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1053 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1054 u64_stats_update_begin(&secy_stats->syncp);
1055 secy_stats->stats.InPktsNoTag++;
1056 u64_stats_update_end(&secy_stats->syncp);
1060 /* deliver on this port */
1061 nskb = skb_clone(skb, GFP_ATOMIC);
1065 nskb->dev = macsec->secy.netdev;
1067 ret = netif_rx(nskb);
1068 if (ret == NET_RX_SUCCESS) {
1069 u64_stats_update_begin(&secy_stats->syncp);
1070 secy_stats->stats.InPktsUntagged++;
1071 u64_stats_update_end(&secy_stats->syncp);
1073 macsec->secy.netdev->stats.rx_dropped++;
1080 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1082 struct sk_buff *skb = *pskb;
1083 struct net_device *dev = skb->dev;
1084 struct macsec_eth_header *hdr;
1085 struct macsec_secy *secy = NULL;
1086 struct macsec_rx_sc *rx_sc;
1087 struct macsec_rx_sa *rx_sa;
1088 struct macsec_rxh_data *rxd;
1089 struct macsec_dev *macsec;
1094 struct pcpu_rx_sc_stats *rxsc_stats;
1095 struct pcpu_secy_stats *secy_stats;
1099 if (skb_headroom(skb) < ETH_HLEN)
1102 hdr = macsec_ethhdr(skb);
1103 if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1104 handle_not_macsec(skb);
1106 /* and deliver to the uncontrolled port */
1107 return RX_HANDLER_PASS;
1110 skb = skb_unshare(skb, GFP_ATOMIC);
1113 return RX_HANDLER_CONSUMED;
1115 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1117 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1121 hdr = macsec_ethhdr(skb);
1123 /* Frames with a SecTAG that has the TCI E bit set but the C
1124 * bit clear are discarded, as this reserved encoding is used
1125 * to identify frames with a SecTAG that are not to be
1126 * delivered to the Controlled Port.
1128 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1129 return RX_HANDLER_PASS;
1131 /* now, pull the extra length */
1132 if (hdr->tci_an & MACSEC_TCI_SC) {
1137 /* ethernet header is part of crypto processing */
1138 skb_push(skb, ETH_HLEN);
1140 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1141 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1142 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1145 rxd = macsec_data_rcu(skb->dev);
1147 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1148 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1149 sc = sc ? macsec_rxsc_get(sc) : NULL;
1152 secy = &macsec->secy;
1162 macsec = macsec_priv(dev);
1163 secy_stats = this_cpu_ptr(macsec->stats);
1164 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1166 if (!macsec_validate_skb(skb, secy->icv_len)) {
1167 u64_stats_update_begin(&secy_stats->syncp);
1168 secy_stats->stats.InPktsBadTag++;
1169 u64_stats_update_end(&secy_stats->syncp);
1173 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1175 /* 10.6.1 if the SA is not in use */
1177 /* If validateFrames is Strict or the C bit in the
1178 * SecTAG is set, discard
1180 if (hdr->tci_an & MACSEC_TCI_C ||
1181 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1182 u64_stats_update_begin(&rxsc_stats->syncp);
1183 rxsc_stats->stats.InPktsNotUsingSA++;
1184 u64_stats_update_end(&rxsc_stats->syncp);
1188 /* not Strict, the frame (with the SecTAG and ICV
1189 * removed) is delivered to the Controlled Port.
1191 u64_stats_update_begin(&rxsc_stats->syncp);
1192 rxsc_stats->stats.InPktsUnusedSA++;
1193 u64_stats_update_end(&rxsc_stats->syncp);
1197 /* First, PN check to avoid decrypting obviously wrong packets */
1198 pn = ntohl(hdr->packet_number);
1199 if (secy->replay_protect) {
1202 spin_lock(&rx_sa->lock);
1203 late = rx_sa->next_pn >= secy->replay_window &&
1204 pn < (rx_sa->next_pn - secy->replay_window);
1205 spin_unlock(&rx_sa->lock);
1208 u64_stats_update_begin(&rxsc_stats->syncp);
1209 rxsc_stats->stats.InPktsLate++;
1210 u64_stats_update_end(&rxsc_stats->syncp);
1215 macsec_skb_cb(skb)->rx_sa = rx_sa;
1217 /* Disabled && !changed text => skip validation */
1218 if (hdr->tci_an & MACSEC_TCI_C ||
1219 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1220 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1223 /* the decrypt callback needs the reference */
1224 if (PTR_ERR(skb) != -EINPROGRESS) {
1225 macsec_rxsa_put(rx_sa);
1226 macsec_rxsc_put(rx_sc);
1230 return RX_HANDLER_CONSUMED;
1233 if (!macsec_post_decrypt(skb, secy, pn))
1237 macsec_finalize_skb(skb, secy->icv_len,
1238 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1239 macsec_reset_skb(skb, secy->netdev);
1242 macsec_rxsa_put(rx_sa);
1243 macsec_rxsc_put(rx_sc);
1247 ret = gro_cells_receive(&macsec->gro_cells, skb);
1248 if (ret == NET_RX_SUCCESS)
1251 macsec->secy.netdev->stats.rx_dropped++;
1256 return RX_HANDLER_CONSUMED;
1259 macsec_rxsa_put(rx_sa);
1261 macsec_rxsc_put(rx_sc);
1266 return RX_HANDLER_CONSUMED;
1269 /* 10.6.1 if the SC is not found */
1270 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1272 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1273 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1275 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1276 struct sk_buff *nskb;
1278 secy_stats = this_cpu_ptr(macsec->stats);
1280 /* If validateFrames is Strict or the C bit in the
1281 * SecTAG is set, discard
1284 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1285 u64_stats_update_begin(&secy_stats->syncp);
1286 secy_stats->stats.InPktsNoSCI++;
1287 u64_stats_update_end(&secy_stats->syncp);
1291 /* not strict, the frame (with the SecTAG and ICV
1292 * removed) is delivered to the Controlled Port.
1294 nskb = skb_clone(skb, GFP_ATOMIC);
1298 macsec_reset_skb(nskb, macsec->secy.netdev);
1300 ret = netif_rx(nskb);
1301 if (ret == NET_RX_SUCCESS) {
1302 u64_stats_update_begin(&secy_stats->syncp);
1303 secy_stats->stats.InPktsUnknownSCI++;
1304 u64_stats_update_end(&secy_stats->syncp);
1306 macsec->secy.netdev->stats.rx_dropped++;
1312 return RX_HANDLER_PASS;
1315 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1317 struct crypto_aead *tfm;
1320 /* Pick a sync gcm(aes) cipher to ensure order is preserved. */
1321 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
1326 ret = crypto_aead_setkey(tfm, key, key_len);
1330 ret = crypto_aead_setauthsize(tfm, icv_len);
1336 crypto_free_aead(tfm);
1337 return ERR_PTR(ret);
1340 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1343 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1347 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1348 if (IS_ERR(rx_sa->key.tfm)) {
1349 free_percpu(rx_sa->stats);
1350 return PTR_ERR(rx_sa->key.tfm);
1353 rx_sa->active = false;
1355 atomic_set(&rx_sa->refcnt, 1);
1356 spin_lock_init(&rx_sa->lock);
1361 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1363 rx_sa->active = false;
1365 macsec_rxsa_put(rx_sa);
1368 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1372 for (i = 0; i < MACSEC_NUM_AN; i++) {
1373 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1375 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1380 macsec_rxsc_put(rx_sc);
1383 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1385 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1387 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1389 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1390 if (rx_sc->sci == sci) {
1393 rcu_assign_pointer(*rx_scp, rx_sc->next);
1401 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1403 struct macsec_rx_sc *rx_sc;
1404 struct macsec_dev *macsec;
1405 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1406 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1407 struct macsec_secy *secy;
1409 list_for_each_entry(macsec, &rxd->secys, secys) {
1410 if (find_rx_sc_rtnl(&macsec->secy, sci))
1411 return ERR_PTR(-EEXIST);
1414 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1416 return ERR_PTR(-ENOMEM);
1418 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1419 if (!rx_sc->stats) {
1421 return ERR_PTR(-ENOMEM);
1425 rx_sc->active = true;
1426 atomic_set(&rx_sc->refcnt, 1);
1428 secy = &macsec_priv(dev)->secy;
1429 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1430 rcu_assign_pointer(secy->rx_sc, rx_sc);
1438 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1441 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1445 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1446 if (IS_ERR(tx_sa->key.tfm)) {
1447 free_percpu(tx_sa->stats);
1448 return PTR_ERR(tx_sa->key.tfm);
1451 tx_sa->active = false;
1452 atomic_set(&tx_sa->refcnt, 1);
1453 spin_lock_init(&tx_sa->lock);
1458 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1460 tx_sa->active = false;
1462 macsec_txsa_put(tx_sa);
1465 static struct genl_family macsec_fam = {
1466 .id = GENL_ID_GENERATE,
1467 .name = MACSEC_GENL_NAME,
1469 .version = MACSEC_GENL_VERSION,
1470 .maxattr = MACSEC_ATTR_MAX,
1474 static struct net_device *get_dev_from_nl(struct net *net,
1475 struct nlattr **attrs)
1477 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1478 struct net_device *dev;
1480 dev = __dev_get_by_index(net, ifindex);
1482 return ERR_PTR(-ENODEV);
1484 if (!netif_is_macsec(dev))
1485 return ERR_PTR(-ENODEV);
1490 static sci_t nla_get_sci(const struct nlattr *nla)
1492 return (__force sci_t)nla_get_u64(nla);
1495 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1498 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1501 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1502 struct nlattr **attrs,
1503 struct nlattr **tb_sa,
1504 struct net_device **devp,
1505 struct macsec_secy **secyp,
1506 struct macsec_tx_sc **scp,
1509 struct net_device *dev;
1510 struct macsec_secy *secy;
1511 struct macsec_tx_sc *tx_sc;
1512 struct macsec_tx_sa *tx_sa;
1514 if (!tb_sa[MACSEC_SA_ATTR_AN])
1515 return ERR_PTR(-EINVAL);
1517 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1519 dev = get_dev_from_nl(net, attrs);
1521 return ERR_CAST(dev);
1523 if (*assoc_num >= MACSEC_NUM_AN)
1524 return ERR_PTR(-EINVAL);
1526 secy = &macsec_priv(dev)->secy;
1527 tx_sc = &secy->tx_sc;
1529 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1531 return ERR_PTR(-ENODEV);
1539 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1540 struct nlattr **attrs,
1541 struct nlattr **tb_rxsc,
1542 struct net_device **devp,
1543 struct macsec_secy **secyp)
1545 struct net_device *dev;
1546 struct macsec_secy *secy;
1547 struct macsec_rx_sc *rx_sc;
1550 dev = get_dev_from_nl(net, attrs);
1552 return ERR_CAST(dev);
1554 secy = &macsec_priv(dev)->secy;
1556 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1557 return ERR_PTR(-EINVAL);
1559 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1560 rx_sc = find_rx_sc_rtnl(secy, sci);
1562 return ERR_PTR(-ENODEV);
1570 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1571 struct nlattr **attrs,
1572 struct nlattr **tb_rxsc,
1573 struct nlattr **tb_sa,
1574 struct net_device **devp,
1575 struct macsec_secy **secyp,
1576 struct macsec_rx_sc **scp,
1579 struct macsec_rx_sc *rx_sc;
1580 struct macsec_rx_sa *rx_sa;
1582 if (!tb_sa[MACSEC_SA_ATTR_AN])
1583 return ERR_PTR(-EINVAL);
1585 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1586 if (*assoc_num >= MACSEC_NUM_AN)
1587 return ERR_PTR(-EINVAL);
1589 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1591 return ERR_CAST(rx_sc);
1593 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1595 return ERR_PTR(-ENODEV);
1602 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1603 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1604 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1605 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1608 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1609 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1610 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1613 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1614 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1615 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1616 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1617 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1618 .len = MACSEC_KEYID_LEN, },
1619 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1620 .len = MACSEC_MAX_KEY_LEN, },
1623 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1625 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1628 if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG],
1629 macsec_genl_sa_policy))
1635 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1637 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1640 if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG],
1641 macsec_genl_rxsc_policy))
1647 static bool validate_add_rxsa(struct nlattr **attrs)
1649 if (!attrs[MACSEC_SA_ATTR_AN] ||
1650 !attrs[MACSEC_SA_ATTR_KEY] ||
1651 !attrs[MACSEC_SA_ATTR_KEYID])
1654 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1657 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1660 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1661 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1665 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1671 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1673 struct net_device *dev;
1674 struct nlattr **attrs = info->attrs;
1675 struct macsec_secy *secy;
1676 struct macsec_rx_sc *rx_sc;
1677 struct macsec_rx_sa *rx_sa;
1678 unsigned char assoc_num;
1679 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1680 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1683 if (!attrs[MACSEC_ATTR_IFINDEX])
1686 if (parse_sa_config(attrs, tb_sa))
1689 if (parse_rxsc_config(attrs, tb_rxsc))
1692 if (!validate_add_rxsa(tb_sa))
1696 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1697 if (IS_ERR(rx_sc)) {
1699 return PTR_ERR(rx_sc);
1702 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1704 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1705 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1706 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1711 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1717 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1723 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1724 secy->key_len, secy->icv_len);
1731 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1732 spin_lock_bh(&rx_sa->lock);
1733 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1734 spin_unlock_bh(&rx_sa->lock);
1737 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1738 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1740 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1742 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1749 static bool validate_add_rxsc(struct nlattr **attrs)
1751 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1754 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1755 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1762 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1764 struct net_device *dev;
1765 sci_t sci = MACSEC_UNDEF_SCI;
1766 struct nlattr **attrs = info->attrs;
1767 struct macsec_rx_sc *rx_sc;
1768 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1770 if (!attrs[MACSEC_ATTR_IFINDEX])
1773 if (parse_rxsc_config(attrs, tb_rxsc))
1776 if (!validate_add_rxsc(tb_rxsc))
1780 dev = get_dev_from_nl(genl_info_net(info), attrs);
1783 return PTR_ERR(dev);
1786 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1788 rx_sc = create_rx_sc(dev, sci);
1789 if (IS_ERR(rx_sc)) {
1791 return PTR_ERR(rx_sc);
1794 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1795 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1802 static bool validate_add_txsa(struct nlattr **attrs)
1804 if (!attrs[MACSEC_SA_ATTR_AN] ||
1805 !attrs[MACSEC_SA_ATTR_PN] ||
1806 !attrs[MACSEC_SA_ATTR_KEY] ||
1807 !attrs[MACSEC_SA_ATTR_KEYID])
1810 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1813 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1816 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1817 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1821 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1827 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1829 struct net_device *dev;
1830 struct nlattr **attrs = info->attrs;
1831 struct macsec_secy *secy;
1832 struct macsec_tx_sc *tx_sc;
1833 struct macsec_tx_sa *tx_sa;
1834 unsigned char assoc_num;
1835 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1838 if (!attrs[MACSEC_ATTR_IFINDEX])
1841 if (parse_sa_config(attrs, tb_sa))
1844 if (!validate_add_txsa(tb_sa))
1848 dev = get_dev_from_nl(genl_info_net(info), attrs);
1851 return PTR_ERR(dev);
1854 secy = &macsec_priv(dev)->secy;
1855 tx_sc = &secy->tx_sc;
1857 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1859 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1860 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1861 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1866 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1872 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1878 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1879 secy->key_len, secy->icv_len);
1886 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1888 spin_lock_bh(&tx_sa->lock);
1889 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1890 spin_unlock_bh(&tx_sa->lock);
1892 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1893 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1895 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1896 secy->operational = true;
1898 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1905 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1907 struct nlattr **attrs = info->attrs;
1908 struct net_device *dev;
1909 struct macsec_secy *secy;
1910 struct macsec_rx_sc *rx_sc;
1911 struct macsec_rx_sa *rx_sa;
1913 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1914 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1916 if (!attrs[MACSEC_ATTR_IFINDEX])
1919 if (parse_sa_config(attrs, tb_sa))
1922 if (parse_rxsc_config(attrs, tb_rxsc))
1926 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1927 &dev, &secy, &rx_sc, &assoc_num);
1928 if (IS_ERR(rx_sa)) {
1930 return PTR_ERR(rx_sa);
1933 if (rx_sa->active) {
1938 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1946 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1948 struct nlattr **attrs = info->attrs;
1949 struct net_device *dev;
1950 struct macsec_secy *secy;
1951 struct macsec_rx_sc *rx_sc;
1953 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1955 if (!attrs[MACSEC_ATTR_IFINDEX])
1958 if (parse_rxsc_config(attrs, tb_rxsc))
1961 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1965 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1968 return PTR_ERR(dev);
1971 secy = &macsec_priv(dev)->secy;
1972 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1974 rx_sc = del_rx_sc(secy, sci);
1986 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1988 struct nlattr **attrs = info->attrs;
1989 struct net_device *dev;
1990 struct macsec_secy *secy;
1991 struct macsec_tx_sc *tx_sc;
1992 struct macsec_tx_sa *tx_sa;
1994 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1996 if (!attrs[MACSEC_ATTR_IFINDEX])
1999 if (parse_sa_config(attrs, tb_sa))
2003 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2004 &dev, &secy, &tx_sc, &assoc_num);
2005 if (IS_ERR(tx_sa)) {
2007 return PTR_ERR(tx_sa);
2010 if (tx_sa->active) {
2015 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2023 static bool validate_upd_sa(struct nlattr **attrs)
2025 if (!attrs[MACSEC_SA_ATTR_AN] ||
2026 attrs[MACSEC_SA_ATTR_KEY] ||
2027 attrs[MACSEC_SA_ATTR_KEYID])
2030 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2033 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2036 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2037 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2044 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2046 struct nlattr **attrs = info->attrs;
2047 struct net_device *dev;
2048 struct macsec_secy *secy;
2049 struct macsec_tx_sc *tx_sc;
2050 struct macsec_tx_sa *tx_sa;
2052 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2054 if (!attrs[MACSEC_ATTR_IFINDEX])
2057 if (parse_sa_config(attrs, tb_sa))
2060 if (!validate_upd_sa(tb_sa))
2064 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2065 &dev, &secy, &tx_sc, &assoc_num);
2066 if (IS_ERR(tx_sa)) {
2068 return PTR_ERR(tx_sa);
2071 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2072 spin_lock_bh(&tx_sa->lock);
2073 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2074 spin_unlock_bh(&tx_sa->lock);
2077 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2078 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2080 if (assoc_num == tx_sc->encoding_sa)
2081 secy->operational = tx_sa->active;
2088 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2090 struct nlattr **attrs = info->attrs;
2091 struct net_device *dev;
2092 struct macsec_secy *secy;
2093 struct macsec_rx_sc *rx_sc;
2094 struct macsec_rx_sa *rx_sa;
2096 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2097 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2099 if (!attrs[MACSEC_ATTR_IFINDEX])
2102 if (parse_rxsc_config(attrs, tb_rxsc))
2105 if (parse_sa_config(attrs, tb_sa))
2108 if (!validate_upd_sa(tb_sa))
2112 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2113 &dev, &secy, &rx_sc, &assoc_num);
2114 if (IS_ERR(rx_sa)) {
2116 return PTR_ERR(rx_sa);
2119 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2120 spin_lock_bh(&rx_sa->lock);
2121 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2122 spin_unlock_bh(&rx_sa->lock);
2125 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2126 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2132 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2134 struct nlattr **attrs = info->attrs;
2135 struct net_device *dev;
2136 struct macsec_secy *secy;
2137 struct macsec_rx_sc *rx_sc;
2138 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2140 if (!attrs[MACSEC_ATTR_IFINDEX])
2143 if (parse_rxsc_config(attrs, tb_rxsc))
2146 if (!validate_add_rxsc(tb_rxsc))
2150 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2151 if (IS_ERR(rx_sc)) {
2153 return PTR_ERR(rx_sc);
2156 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2157 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2159 if (rx_sc->active != new)
2160 secy->n_rx_sc += new ? 1 : -1;
2162 rx_sc->active = new;
2170 static int copy_tx_sa_stats(struct sk_buff *skb,
2171 struct macsec_tx_sa_stats __percpu *pstats)
2173 struct macsec_tx_sa_stats sum = {0, };
2176 for_each_possible_cpu(cpu) {
2177 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2179 sum.OutPktsProtected += stats->OutPktsProtected;
2180 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2183 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2184 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2190 static int copy_rx_sa_stats(struct sk_buff *skb,
2191 struct macsec_rx_sa_stats __percpu *pstats)
2193 struct macsec_rx_sa_stats sum = {0, };
2196 for_each_possible_cpu(cpu) {
2197 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2199 sum.InPktsOK += stats->InPktsOK;
2200 sum.InPktsInvalid += stats->InPktsInvalid;
2201 sum.InPktsNotValid += stats->InPktsNotValid;
2202 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2203 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2206 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2207 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2208 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2209 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2210 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2216 static int copy_rx_sc_stats(struct sk_buff *skb,
2217 struct pcpu_rx_sc_stats __percpu *pstats)
2219 struct macsec_rx_sc_stats sum = {0, };
2222 for_each_possible_cpu(cpu) {
2223 const struct pcpu_rx_sc_stats *stats;
2224 struct macsec_rx_sc_stats tmp;
2227 stats = per_cpu_ptr(pstats, cpu);
2229 start = u64_stats_fetch_begin_irq(&stats->syncp);
2230 memcpy(&tmp, &stats->stats, sizeof(tmp));
2231 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2233 sum.InOctetsValidated += tmp.InOctetsValidated;
2234 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2235 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2236 sum.InPktsDelayed += tmp.InPktsDelayed;
2237 sum.InPktsOK += tmp.InPktsOK;
2238 sum.InPktsInvalid += tmp.InPktsInvalid;
2239 sum.InPktsLate += tmp.InPktsLate;
2240 sum.InPktsNotValid += tmp.InPktsNotValid;
2241 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2242 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2245 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2246 sum.InOctetsValidated,
2247 MACSEC_RXSC_STATS_ATTR_PAD) ||
2248 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2249 sum.InOctetsDecrypted,
2250 MACSEC_RXSC_STATS_ATTR_PAD) ||
2251 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2252 sum.InPktsUnchecked,
2253 MACSEC_RXSC_STATS_ATTR_PAD) ||
2254 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2256 MACSEC_RXSC_STATS_ATTR_PAD) ||
2257 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2259 MACSEC_RXSC_STATS_ATTR_PAD) ||
2260 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2262 MACSEC_RXSC_STATS_ATTR_PAD) ||
2263 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2265 MACSEC_RXSC_STATS_ATTR_PAD) ||
2266 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2268 MACSEC_RXSC_STATS_ATTR_PAD) ||
2269 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2270 sum.InPktsNotUsingSA,
2271 MACSEC_RXSC_STATS_ATTR_PAD) ||
2272 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2274 MACSEC_RXSC_STATS_ATTR_PAD))
2280 static int copy_tx_sc_stats(struct sk_buff *skb,
2281 struct pcpu_tx_sc_stats __percpu *pstats)
2283 struct macsec_tx_sc_stats sum = {0, };
2286 for_each_possible_cpu(cpu) {
2287 const struct pcpu_tx_sc_stats *stats;
2288 struct macsec_tx_sc_stats tmp;
2291 stats = per_cpu_ptr(pstats, cpu);
2293 start = u64_stats_fetch_begin_irq(&stats->syncp);
2294 memcpy(&tmp, &stats->stats, sizeof(tmp));
2295 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2297 sum.OutPktsProtected += tmp.OutPktsProtected;
2298 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2299 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2300 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2303 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2304 sum.OutPktsProtected,
2305 MACSEC_TXSC_STATS_ATTR_PAD) ||
2306 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2307 sum.OutPktsEncrypted,
2308 MACSEC_TXSC_STATS_ATTR_PAD) ||
2309 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2310 sum.OutOctetsProtected,
2311 MACSEC_TXSC_STATS_ATTR_PAD) ||
2312 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2313 sum.OutOctetsEncrypted,
2314 MACSEC_TXSC_STATS_ATTR_PAD))
2320 static int copy_secy_stats(struct sk_buff *skb,
2321 struct pcpu_secy_stats __percpu *pstats)
2323 struct macsec_dev_stats sum = {0, };
2326 for_each_possible_cpu(cpu) {
2327 const struct pcpu_secy_stats *stats;
2328 struct macsec_dev_stats tmp;
2331 stats = per_cpu_ptr(pstats, cpu);
2333 start = u64_stats_fetch_begin_irq(&stats->syncp);
2334 memcpy(&tmp, &stats->stats, sizeof(tmp));
2335 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2337 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2338 sum.InPktsUntagged += tmp.InPktsUntagged;
2339 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2340 sum.InPktsNoTag += tmp.InPktsNoTag;
2341 sum.InPktsBadTag += tmp.InPktsBadTag;
2342 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2343 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2344 sum.InPktsOverrun += tmp.InPktsOverrun;
2347 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2348 sum.OutPktsUntagged,
2349 MACSEC_SECY_STATS_ATTR_PAD) ||
2350 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2352 MACSEC_SECY_STATS_ATTR_PAD) ||
2353 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2355 MACSEC_SECY_STATS_ATTR_PAD) ||
2356 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2358 MACSEC_SECY_STATS_ATTR_PAD) ||
2359 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2361 MACSEC_SECY_STATS_ATTR_PAD) ||
2362 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2363 sum.InPktsUnknownSCI,
2364 MACSEC_SECY_STATS_ATTR_PAD) ||
2365 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2367 MACSEC_SECY_STATS_ATTR_PAD) ||
2368 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2370 MACSEC_SECY_STATS_ATTR_PAD))
2376 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2378 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2379 struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
2384 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2385 MACSEC_SECY_ATTR_PAD) ||
2386 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2387 MACSEC_DEFAULT_CIPHER_ID,
2388 MACSEC_SECY_ATTR_PAD) ||
2389 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2390 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2391 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2392 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2393 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2394 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2395 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2396 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2397 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2398 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2401 if (secy->replay_protect) {
2402 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2406 nla_nest_end(skb, secy_nest);
2410 nla_nest_cancel(skb, secy_nest);
2414 static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
2415 struct sk_buff *skb, struct netlink_callback *cb)
2417 struct macsec_rx_sc *rx_sc;
2418 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2419 struct nlattr *txsa_list, *rxsc_list;
2422 struct nlattr *attr;
2424 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2425 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2429 genl_dump_check_consistent(cb, hdr, &macsec_fam);
2431 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2432 goto nla_put_failure;
2434 if (nla_put_secy(secy, skb))
2435 goto nla_put_failure;
2437 attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
2439 goto nla_put_failure;
2440 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2441 nla_nest_cancel(skb, attr);
2442 goto nla_put_failure;
2444 nla_nest_end(skb, attr);
2446 attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
2448 goto nla_put_failure;
2449 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2450 nla_nest_cancel(skb, attr);
2451 goto nla_put_failure;
2453 nla_nest_end(skb, attr);
2455 txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
2457 goto nla_put_failure;
2458 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2459 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2460 struct nlattr *txsa_nest;
2465 txsa_nest = nla_nest_start(skb, j++);
2467 nla_nest_cancel(skb, txsa_list);
2468 goto nla_put_failure;
2471 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2472 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2473 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2474 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2475 nla_nest_cancel(skb, txsa_nest);
2476 nla_nest_cancel(skb, txsa_list);
2477 goto nla_put_failure;
2480 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2482 nla_nest_cancel(skb, txsa_nest);
2483 nla_nest_cancel(skb, txsa_list);
2484 goto nla_put_failure;
2486 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2487 nla_nest_cancel(skb, attr);
2488 nla_nest_cancel(skb, txsa_nest);
2489 nla_nest_cancel(skb, txsa_list);
2490 goto nla_put_failure;
2492 nla_nest_end(skb, attr);
2494 nla_nest_end(skb, txsa_nest);
2496 nla_nest_end(skb, txsa_list);
2498 rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
2500 goto nla_put_failure;
2503 for_each_rxsc_rtnl(secy, rx_sc) {
2505 struct nlattr *rxsa_list;
2506 struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
2509 nla_nest_cancel(skb, rxsc_list);
2510 goto nla_put_failure;
2513 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2514 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2515 MACSEC_RXSC_ATTR_PAD)) {
2516 nla_nest_cancel(skb, rxsc_nest);
2517 nla_nest_cancel(skb, rxsc_list);
2518 goto nla_put_failure;
2521 attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
2523 nla_nest_cancel(skb, rxsc_nest);
2524 nla_nest_cancel(skb, rxsc_list);
2525 goto nla_put_failure;
2527 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2528 nla_nest_cancel(skb, attr);
2529 nla_nest_cancel(skb, rxsc_nest);
2530 nla_nest_cancel(skb, rxsc_list);
2531 goto nla_put_failure;
2533 nla_nest_end(skb, attr);
2535 rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
2537 nla_nest_cancel(skb, rxsc_nest);
2538 nla_nest_cancel(skb, rxsc_list);
2539 goto nla_put_failure;
2542 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2543 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2544 struct nlattr *rxsa_nest;
2549 rxsa_nest = nla_nest_start(skb, k++);
2551 nla_nest_cancel(skb, rxsa_list);
2552 nla_nest_cancel(skb, rxsc_nest);
2553 nla_nest_cancel(skb, rxsc_list);
2554 goto nla_put_failure;
2557 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2559 nla_nest_cancel(skb, rxsa_list);
2560 nla_nest_cancel(skb, rxsc_nest);
2561 nla_nest_cancel(skb, rxsc_list);
2562 goto nla_put_failure;
2564 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2565 nla_nest_cancel(skb, attr);
2566 nla_nest_cancel(skb, rxsa_list);
2567 nla_nest_cancel(skb, rxsc_nest);
2568 nla_nest_cancel(skb, rxsc_list);
2569 goto nla_put_failure;
2571 nla_nest_end(skb, attr);
2573 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2574 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2575 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2576 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2577 nla_nest_cancel(skb, rxsa_nest);
2578 nla_nest_cancel(skb, rxsc_nest);
2579 nla_nest_cancel(skb, rxsc_list);
2580 goto nla_put_failure;
2582 nla_nest_end(skb, rxsa_nest);
2585 nla_nest_end(skb, rxsa_list);
2586 nla_nest_end(skb, rxsc_nest);
2589 nla_nest_end(skb, rxsc_list);
2591 genlmsg_end(skb, hdr);
2596 genlmsg_cancel(skb, hdr);
2600 static int macsec_generation = 1; /* protected by RTNL */
2602 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2604 struct net *net = sock_net(skb->sk);
2605 struct net_device *dev;
2608 dev_idx = cb->args[0];
2613 cb->seq = macsec_generation;
2615 for_each_netdev(net, dev) {
2616 struct macsec_secy *secy;
2621 if (!netif_is_macsec(dev))
2624 secy = &macsec_priv(dev)->secy;
2625 if (dump_secy(secy, dev, skb, cb) < 0)
2637 static const struct genl_ops macsec_genl_ops[] = {
2639 .cmd = MACSEC_CMD_GET_TXSC,
2640 .dumpit = macsec_dump_txsc,
2641 .policy = macsec_genl_policy,
2644 .cmd = MACSEC_CMD_ADD_RXSC,
2645 .doit = macsec_add_rxsc,
2646 .policy = macsec_genl_policy,
2647 .flags = GENL_ADMIN_PERM,
2650 .cmd = MACSEC_CMD_DEL_RXSC,
2651 .doit = macsec_del_rxsc,
2652 .policy = macsec_genl_policy,
2653 .flags = GENL_ADMIN_PERM,
2656 .cmd = MACSEC_CMD_UPD_RXSC,
2657 .doit = macsec_upd_rxsc,
2658 .policy = macsec_genl_policy,
2659 .flags = GENL_ADMIN_PERM,
2662 .cmd = MACSEC_CMD_ADD_TXSA,
2663 .doit = macsec_add_txsa,
2664 .policy = macsec_genl_policy,
2665 .flags = GENL_ADMIN_PERM,
2668 .cmd = MACSEC_CMD_DEL_TXSA,
2669 .doit = macsec_del_txsa,
2670 .policy = macsec_genl_policy,
2671 .flags = GENL_ADMIN_PERM,
2674 .cmd = MACSEC_CMD_UPD_TXSA,
2675 .doit = macsec_upd_txsa,
2676 .policy = macsec_genl_policy,
2677 .flags = GENL_ADMIN_PERM,
2680 .cmd = MACSEC_CMD_ADD_RXSA,
2681 .doit = macsec_add_rxsa,
2682 .policy = macsec_genl_policy,
2683 .flags = GENL_ADMIN_PERM,
2686 .cmd = MACSEC_CMD_DEL_RXSA,
2687 .doit = macsec_del_rxsa,
2688 .policy = macsec_genl_policy,
2689 .flags = GENL_ADMIN_PERM,
2692 .cmd = MACSEC_CMD_UPD_RXSA,
2693 .doit = macsec_upd_rxsa,
2694 .policy = macsec_genl_policy,
2695 .flags = GENL_ADMIN_PERM,
2699 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2700 struct net_device *dev)
2702 struct macsec_dev *macsec = netdev_priv(dev);
2703 struct macsec_secy *secy = &macsec->secy;
2704 struct pcpu_secy_stats *secy_stats;
2708 if (!secy->protect_frames) {
2709 secy_stats = this_cpu_ptr(macsec->stats);
2710 u64_stats_update_begin(&secy_stats->syncp);
2711 secy_stats->stats.OutPktsUntagged++;
2712 u64_stats_update_end(&secy_stats->syncp);
2713 skb->dev = macsec->real_dev;
2715 ret = dev_queue_xmit(skb);
2716 count_tx(dev, ret, len);
2720 if (!secy->operational) {
2722 dev->stats.tx_dropped++;
2723 return NETDEV_TX_OK;
2726 skb = macsec_encrypt(skb, dev);
2728 if (PTR_ERR(skb) != -EINPROGRESS)
2729 dev->stats.tx_dropped++;
2730 return NETDEV_TX_OK;
2733 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2735 macsec_encrypt_finish(skb, dev);
2737 ret = dev_queue_xmit(skb);
2738 count_tx(dev, ret, len);
2742 #define MACSEC_FEATURES \
2743 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
2744 static struct lock_class_key macsec_netdev_addr_lock_key;
2746 static int macsec_dev_init(struct net_device *dev)
2748 struct macsec_dev *macsec = macsec_priv(dev);
2749 struct net_device *real_dev = macsec->real_dev;
2752 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2756 err = gro_cells_init(&macsec->gro_cells, dev);
2758 free_percpu(dev->tstats);
2762 dev->features = real_dev->features & MACSEC_FEATURES;
2763 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2765 dev->needed_headroom = real_dev->needed_headroom +
2766 MACSEC_NEEDED_HEADROOM;
2767 dev->needed_tailroom = real_dev->needed_tailroom +
2768 MACSEC_NEEDED_TAILROOM;
2770 if (is_zero_ether_addr(dev->dev_addr))
2771 eth_hw_addr_inherit(dev, real_dev);
2772 if (is_zero_ether_addr(dev->broadcast))
2773 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2778 static void macsec_dev_uninit(struct net_device *dev)
2780 struct macsec_dev *macsec = macsec_priv(dev);
2782 gro_cells_destroy(&macsec->gro_cells);
2783 free_percpu(dev->tstats);
2786 static netdev_features_t macsec_fix_features(struct net_device *dev,
2787 netdev_features_t features)
2789 struct macsec_dev *macsec = macsec_priv(dev);
2790 struct net_device *real_dev = macsec->real_dev;
2792 features &= (real_dev->features & MACSEC_FEATURES) |
2793 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2794 features |= NETIF_F_LLTX;
2799 static int macsec_dev_open(struct net_device *dev)
2801 struct macsec_dev *macsec = macsec_priv(dev);
2802 struct net_device *real_dev = macsec->real_dev;
2805 err = dev_uc_add(real_dev, dev->dev_addr);
2809 if (dev->flags & IFF_ALLMULTI) {
2810 err = dev_set_allmulti(real_dev, 1);
2815 if (dev->flags & IFF_PROMISC) {
2816 err = dev_set_promiscuity(real_dev, 1);
2818 goto clear_allmulti;
2821 if (netif_carrier_ok(real_dev))
2822 netif_carrier_on(dev);
2826 if (dev->flags & IFF_ALLMULTI)
2827 dev_set_allmulti(real_dev, -1);
2829 dev_uc_del(real_dev, dev->dev_addr);
2830 netif_carrier_off(dev);
2834 static int macsec_dev_stop(struct net_device *dev)
2836 struct macsec_dev *macsec = macsec_priv(dev);
2837 struct net_device *real_dev = macsec->real_dev;
2839 netif_carrier_off(dev);
2841 dev_mc_unsync(real_dev, dev);
2842 dev_uc_unsync(real_dev, dev);
2844 if (dev->flags & IFF_ALLMULTI)
2845 dev_set_allmulti(real_dev, -1);
2847 if (dev->flags & IFF_PROMISC)
2848 dev_set_promiscuity(real_dev, -1);
2850 dev_uc_del(real_dev, dev->dev_addr);
2855 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2857 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2859 if (!(dev->flags & IFF_UP))
2862 if (change & IFF_ALLMULTI)
2863 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2865 if (change & IFF_PROMISC)
2866 dev_set_promiscuity(real_dev,
2867 dev->flags & IFF_PROMISC ? 1 : -1);
2870 static void macsec_dev_set_rx_mode(struct net_device *dev)
2872 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2874 dev_mc_sync(real_dev, dev);
2875 dev_uc_sync(real_dev, dev);
2878 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
2880 return make_sci(dev->dev_addr, port);
2883 static int macsec_set_mac_address(struct net_device *dev, void *p)
2885 struct macsec_dev *macsec = macsec_priv(dev);
2886 struct net_device *real_dev = macsec->real_dev;
2887 struct sockaddr *addr = p;
2890 if (!is_valid_ether_addr(addr->sa_data))
2891 return -EADDRNOTAVAIL;
2893 if (!(dev->flags & IFF_UP))
2896 err = dev_uc_add(real_dev, addr->sa_data);
2900 dev_uc_del(real_dev, dev->dev_addr);
2903 ether_addr_copy(dev->dev_addr, addr->sa_data);
2904 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
2908 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2910 struct macsec_dev *macsec = macsec_priv(dev);
2911 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2913 if (macsec->real_dev->mtu - extra < new_mtu)
2921 static struct rtnl_link_stats64 *macsec_get_stats64(struct net_device *dev,
2922 struct rtnl_link_stats64 *s)
2929 for_each_possible_cpu(cpu) {
2930 struct pcpu_sw_netstats *stats;
2931 struct pcpu_sw_netstats tmp;
2934 stats = per_cpu_ptr(dev->tstats, cpu);
2936 start = u64_stats_fetch_begin_irq(&stats->syncp);
2937 tmp.rx_packets = stats->rx_packets;
2938 tmp.rx_bytes = stats->rx_bytes;
2939 tmp.tx_packets = stats->tx_packets;
2940 tmp.tx_bytes = stats->tx_bytes;
2941 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2943 s->rx_packets += tmp.rx_packets;
2944 s->rx_bytes += tmp.rx_bytes;
2945 s->tx_packets += tmp.tx_packets;
2946 s->tx_bytes += tmp.tx_bytes;
2949 s->rx_dropped = dev->stats.rx_dropped;
2950 s->tx_dropped = dev->stats.tx_dropped;
2955 static int macsec_get_iflink(const struct net_device *dev)
2957 return macsec_priv(dev)->real_dev->ifindex;
2961 static int macsec_get_nest_level(struct net_device *dev)
2963 return macsec_priv(dev)->nest_level;
2967 static const struct net_device_ops macsec_netdev_ops = {
2968 .ndo_init = macsec_dev_init,
2969 .ndo_uninit = macsec_dev_uninit,
2970 .ndo_open = macsec_dev_open,
2971 .ndo_stop = macsec_dev_stop,
2972 .ndo_fix_features = macsec_fix_features,
2973 .ndo_change_mtu = macsec_change_mtu,
2974 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2975 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2976 .ndo_set_mac_address = macsec_set_mac_address,
2977 .ndo_start_xmit = macsec_start_xmit,
2978 .ndo_get_stats64 = macsec_get_stats64,
2979 .ndo_get_iflink = macsec_get_iflink,
2980 .ndo_get_lock_subclass = macsec_get_nest_level,
2983 static const struct device_type macsec_type = {
2987 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2988 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
2989 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
2990 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2991 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2992 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2993 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2994 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2995 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2996 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2997 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
2998 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
2999 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3000 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3003 static void macsec_free_netdev(struct net_device *dev)
3005 struct macsec_dev *macsec = macsec_priv(dev);
3006 struct net_device *real_dev = macsec->real_dev;
3008 free_percpu(macsec->stats);
3009 free_percpu(macsec->secy.tx_sc.stats);
3015 static void macsec_setup(struct net_device *dev)
3018 dev->priv_flags |= IFF_NO_QUEUE;
3019 dev->netdev_ops = &macsec_netdev_ops;
3020 dev->destructor = macsec_free_netdev;
3021 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3023 eth_zero_addr(dev->broadcast);
3026 static void macsec_changelink_common(struct net_device *dev,
3027 struct nlattr *data[])
3029 struct macsec_secy *secy;
3030 struct macsec_tx_sc *tx_sc;
3032 secy = &macsec_priv(dev)->secy;
3033 tx_sc = &secy->tx_sc;
3035 if (data[IFLA_MACSEC_ENCODING_SA]) {
3036 struct macsec_tx_sa *tx_sa;
3038 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3039 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3041 secy->operational = tx_sa && tx_sa->active;
3044 if (data[IFLA_MACSEC_WINDOW])
3045 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3047 if (data[IFLA_MACSEC_ENCRYPT])
3048 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3050 if (data[IFLA_MACSEC_PROTECT])
3051 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3053 if (data[IFLA_MACSEC_INC_SCI])
3054 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3056 if (data[IFLA_MACSEC_ES])
3057 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3059 if (data[IFLA_MACSEC_SCB])
3060 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3062 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3063 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3065 if (data[IFLA_MACSEC_VALIDATION])
3066 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3069 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3070 struct nlattr *data[])
3075 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3076 data[IFLA_MACSEC_ICV_LEN] ||
3077 data[IFLA_MACSEC_SCI] ||
3078 data[IFLA_MACSEC_PORT])
3081 macsec_changelink_common(dev, data);
3086 static void macsec_del_dev(struct macsec_dev *macsec)
3090 while (macsec->secy.rx_sc) {
3091 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3093 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3097 for (i = 0; i < MACSEC_NUM_AN; i++) {
3098 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3101 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3107 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3109 struct macsec_dev *macsec = macsec_priv(dev);
3110 struct net_device *real_dev = macsec->real_dev;
3112 unregister_netdevice_queue(dev, head);
3113 list_del_rcu(&macsec->secys);
3114 macsec_del_dev(macsec);
3115 netdev_upper_dev_unlink(real_dev, dev);
3117 macsec_generation++;
3120 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3122 struct macsec_dev *macsec = macsec_priv(dev);
3123 struct net_device *real_dev = macsec->real_dev;
3124 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3126 macsec_common_dellink(dev, head);
3128 if (list_empty(&rxd->secys)) {
3129 netdev_rx_handler_unregister(real_dev);
3134 static int register_macsec_dev(struct net_device *real_dev,
3135 struct net_device *dev)
3137 struct macsec_dev *macsec = macsec_priv(dev);
3138 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3143 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3147 INIT_LIST_HEAD(&rxd->secys);
3149 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3157 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3161 static bool sci_exists(struct net_device *dev, sci_t sci)
3163 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3164 struct macsec_dev *macsec;
3166 list_for_each_entry(macsec, &rxd->secys, secys) {
3167 if (macsec->secy.sci == sci)
3174 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3176 struct macsec_dev *macsec = macsec_priv(dev);
3177 struct macsec_secy *secy = &macsec->secy;
3179 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3183 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3184 if (!secy->tx_sc.stats) {
3185 free_percpu(macsec->stats);
3189 if (sci == MACSEC_UNDEF_SCI)
3190 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3193 secy->operational = true;
3194 secy->key_len = DEFAULT_SAK_LEN;
3195 secy->icv_len = icv_len;
3196 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3197 secy->protect_frames = true;
3198 secy->replay_protect = false;
3201 secy->tx_sc.active = true;
3202 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3203 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3204 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3205 secy->tx_sc.end_station = false;
3206 secy->tx_sc.scb = false;
3211 static int macsec_newlink(struct net *net, struct net_device *dev,
3212 struct nlattr *tb[], struct nlattr *data[])
3214 struct macsec_dev *macsec = macsec_priv(dev);
3215 rx_handler_func_t *rx_handler;
3216 u8 icv_len = DEFAULT_ICV_LEN;
3217 struct net_device *real_dev;
3223 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3226 if (real_dev->type != ARPHRD_ETHER)
3229 dev->priv_flags |= IFF_MACSEC;
3231 macsec->real_dev = real_dev;
3233 /* send_sci must be set to true when transmit sci explicitly is set */
3234 if ((data && data[IFLA_MACSEC_SCI]) &&
3235 (data && data[IFLA_MACSEC_INC_SCI])) {
3236 u8 send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3242 if (data && data[IFLA_MACSEC_ICV_LEN])
3243 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3244 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3250 rx_handler = rtnl_dereference(real_dev->rx_handler);
3251 if (rx_handler && rx_handler != macsec_handle_frame)
3254 err = register_netdevice(dev);
3260 macsec->nest_level = dev_get_nest_level(real_dev) + 1;
3261 netdev_lockdep_set_classes(dev);
3262 lockdep_set_class_and_subclass(&dev->addr_list_lock,
3263 &macsec_netdev_addr_lock_key,
3264 macsec_get_nest_level(dev));
3266 err = netdev_upper_dev_link(real_dev, dev);
3270 /* need to be already registered so that ->init has run and
3271 * the MAC addr is set
3273 if (data && data[IFLA_MACSEC_SCI])
3274 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3275 else if (data && data[IFLA_MACSEC_PORT])
3276 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3278 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3280 if (rx_handler && sci_exists(real_dev, sci)) {
3285 err = macsec_add_dev(dev, sci, icv_len);
3290 macsec_changelink_common(dev, data);
3292 err = register_macsec_dev(real_dev, dev);
3296 netif_stacked_transfer_operstate(real_dev, dev);
3297 linkwatch_fire_event(dev);
3299 macsec_generation++;
3304 macsec_del_dev(macsec);
3306 netdev_upper_dev_unlink(real_dev, dev);
3308 unregister_netdevice(dev);
3312 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[])
3314 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3315 u8 icv_len = DEFAULT_ICV_LEN;
3322 if (data[IFLA_MACSEC_CIPHER_SUITE])
3323 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3325 if (data[IFLA_MACSEC_ICV_LEN]) {
3326 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3327 if (icv_len != DEFAULT_ICV_LEN) {
3328 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3329 struct crypto_aead *dummy_tfm;
3331 dummy_tfm = macsec_alloc_tfm(dummy_key,
3334 if (IS_ERR(dummy_tfm))
3335 return PTR_ERR(dummy_tfm);
3336 crypto_free_aead(dummy_tfm);
3341 case MACSEC_DEFAULT_CIPHER_ID:
3342 case MACSEC_DEFAULT_CIPHER_ALT:
3343 if (icv_len < MACSEC_MIN_ICV_LEN ||
3344 icv_len > MACSEC_STD_ICV_LEN)
3351 if (data[IFLA_MACSEC_ENCODING_SA]) {
3352 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3356 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3357 flag < IFLA_MACSEC_VALIDATION;
3360 if (nla_get_u8(data[flag]) > 1)
3365 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3366 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3367 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3369 if ((sci && (scb || es)) || (scb && es))
3372 if (data[IFLA_MACSEC_VALIDATION] &&
3373 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3376 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3377 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3378 !data[IFLA_MACSEC_WINDOW])
3384 static struct net *macsec_get_link_net(const struct net_device *dev)
3386 return dev_net(macsec_priv(dev)->real_dev);
3389 static size_t macsec_get_size(const struct net_device *dev)
3392 nla_total_size_64bit(8) + /* SCI */
3393 nla_total_size(1) + /* ICV_LEN */
3394 nla_total_size_64bit(8) + /* CIPHER_SUITE */
3395 nla_total_size(4) + /* WINDOW */
3396 nla_total_size(1) + /* ENCODING_SA */
3397 nla_total_size(1) + /* ENCRYPT */
3398 nla_total_size(1) + /* PROTECT */
3399 nla_total_size(1) + /* INC_SCI */
3400 nla_total_size(1) + /* ES */
3401 nla_total_size(1) + /* SCB */
3402 nla_total_size(1) + /* REPLAY_PROTECT */
3403 nla_total_size(1) + /* VALIDATION */
3407 static int macsec_fill_info(struct sk_buff *skb,
3408 const struct net_device *dev)
3410 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3411 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3413 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3415 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3416 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3417 MACSEC_DEFAULT_CIPHER_ID, IFLA_MACSEC_PAD) ||
3418 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3419 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3420 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3421 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3422 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3423 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3424 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3425 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3427 goto nla_put_failure;
3429 if (secy->replay_protect) {
3430 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3431 goto nla_put_failure;
3440 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3442 .priv_size = sizeof(struct macsec_dev),
3443 .maxtype = IFLA_MACSEC_MAX,
3444 .policy = macsec_rtnl_policy,
3445 .setup = macsec_setup,
3446 .validate = macsec_validate_attr,
3447 .newlink = macsec_newlink,
3448 .changelink = macsec_changelink,
3449 .dellink = macsec_dellink,
3450 .get_size = macsec_get_size,
3451 .fill_info = macsec_fill_info,
3452 .get_link_net = macsec_get_link_net,
3455 static bool is_macsec_master(struct net_device *dev)
3457 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3460 static int macsec_notify(struct notifier_block *this, unsigned long event,
3463 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3466 if (!is_macsec_master(real_dev))
3472 case NETDEV_CHANGE: {
3473 struct macsec_dev *m, *n;
3474 struct macsec_rxh_data *rxd;
3476 rxd = macsec_data_rtnl(real_dev);
3477 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3478 struct net_device *dev = m->secy.netdev;
3480 netif_stacked_transfer_operstate(real_dev, dev);
3484 case NETDEV_UNREGISTER: {
3485 struct macsec_dev *m, *n;
3486 struct macsec_rxh_data *rxd;
3488 rxd = macsec_data_rtnl(real_dev);
3489 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3490 macsec_common_dellink(m->secy.netdev, &head);
3493 netdev_rx_handler_unregister(real_dev);
3496 unregister_netdevice_many(&head);
3499 case NETDEV_CHANGEMTU: {
3500 struct macsec_dev *m;
3501 struct macsec_rxh_data *rxd;
3503 rxd = macsec_data_rtnl(real_dev);
3504 list_for_each_entry(m, &rxd->secys, secys) {
3505 struct net_device *dev = m->secy.netdev;
3506 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3507 macsec_extra_len(true));
3510 dev_set_mtu(dev, mtu);
3518 static struct notifier_block macsec_notifier = {
3519 .notifier_call = macsec_notify,
3522 static int __init macsec_init(void)
3526 pr_info("MACsec IEEE 802.1AE\n");
3527 err = register_netdevice_notifier(&macsec_notifier);
3531 err = rtnl_link_register(&macsec_link_ops);
3535 err = genl_register_family_with_ops(&macsec_fam, macsec_genl_ops);
3542 rtnl_link_unregister(&macsec_link_ops);
3544 unregister_netdevice_notifier(&macsec_notifier);
3548 static void __exit macsec_exit(void)
3550 genl_unregister_family(&macsec_fam);
3551 rtnl_link_unregister(&macsec_link_ops);
3552 unregister_netdevice_notifier(&macsec_notifier);
3556 module_init(macsec_init);
3557 module_exit(macsec_exit);
3559 MODULE_ALIAS_RTNL_LINK("macsec");
3560 MODULE_ALIAS_GENL_FAMILY("macsec");
3562 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3563 MODULE_LICENSE("GPL v2");