Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / macsec.c
blob4ad3b877e5fd9afff1d28aca975cee4d9ec32dde
1 /*
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 <linux/refcount.h>
20 #include <net/genetlink.h>
21 #include <net/sock.h>
22 #include <net/gro_cells.h>
23 #include <linux/if_arp.h>
25 #include <uapi/linux/if_macsec.h>
27 typedef u64 __bitwise sci_t;
29 #define MACSEC_SCI_LEN 8
31 /* SecTAG length = macsec_eth_header without the optional SCI */
32 #define MACSEC_TAG_LEN 6
34 struct macsec_eth_header {
35 struct ethhdr eth;
36 /* SecTAG */
37 u8 tci_an;
38 #if defined(__LITTLE_ENDIAN_BITFIELD)
39 u8 short_length:6,
40 unused:2;
41 #elif defined(__BIG_ENDIAN_BITFIELD)
42 u8 unused:2,
43 short_length:6;
44 #else
45 #error "Please fix <asm/byteorder.h>"
46 #endif
47 __be32 packet_number;
48 u8 secure_channel_id[8]; /* optional */
49 } __packed;
51 #define MACSEC_TCI_VERSION 0x80
52 #define MACSEC_TCI_ES 0x40 /* end station */
53 #define MACSEC_TCI_SC 0x20 /* SCI present */
54 #define MACSEC_TCI_SCB 0x10 /* epon */
55 #define MACSEC_TCI_E 0x08 /* encryption */
56 #define MACSEC_TCI_C 0x04 /* changed text */
57 #define MACSEC_AN_MASK 0x03 /* association number */
58 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
60 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
61 #define MIN_NON_SHORT_LEN 48
63 #define GCM_AES_IV_LEN 12
64 #define DEFAULT_ICV_LEN 16
66 #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
68 #define for_each_rxsc(secy, sc) \
69 for (sc = rcu_dereference_bh(secy->rx_sc); \
70 sc; \
71 sc = rcu_dereference_bh(sc->next))
72 #define for_each_rxsc_rtnl(secy, sc) \
73 for (sc = rtnl_dereference(secy->rx_sc); \
74 sc; \
75 sc = rtnl_dereference(sc->next))
77 struct gcm_iv {
78 union {
79 u8 secure_channel_id[8];
80 sci_t sci;
82 __be32 pn;
85 /**
86 * struct macsec_key - SA key
87 * @id: user-provided key identifier
88 * @tfm: crypto struct, key storage
90 struct macsec_key {
91 u8 id[MACSEC_KEYID_LEN];
92 struct crypto_aead *tfm;
95 struct macsec_rx_sc_stats {
96 __u64 InOctetsValidated;
97 __u64 InOctetsDecrypted;
98 __u64 InPktsUnchecked;
99 __u64 InPktsDelayed;
100 __u64 InPktsOK;
101 __u64 InPktsInvalid;
102 __u64 InPktsLate;
103 __u64 InPktsNotValid;
104 __u64 InPktsNotUsingSA;
105 __u64 InPktsUnusedSA;
108 struct macsec_rx_sa_stats {
109 __u32 InPktsOK;
110 __u32 InPktsInvalid;
111 __u32 InPktsNotValid;
112 __u32 InPktsNotUsingSA;
113 __u32 InPktsUnusedSA;
116 struct macsec_tx_sa_stats {
117 __u32 OutPktsProtected;
118 __u32 OutPktsEncrypted;
121 struct macsec_tx_sc_stats {
122 __u64 OutPktsProtected;
123 __u64 OutPktsEncrypted;
124 __u64 OutOctetsProtected;
125 __u64 OutOctetsEncrypted;
128 struct macsec_dev_stats {
129 __u64 OutPktsUntagged;
130 __u64 InPktsUntagged;
131 __u64 OutPktsTooLong;
132 __u64 InPktsNoTag;
133 __u64 InPktsBadTag;
134 __u64 InPktsUnknownSCI;
135 __u64 InPktsNoSCI;
136 __u64 InPktsOverrun;
140 * struct macsec_rx_sa - receive secure association
141 * @active:
142 * @next_pn: packet number expected for the next packet
143 * @lock: protects next_pn manipulations
144 * @key: key structure
145 * @stats: per-SA stats
147 struct macsec_rx_sa {
148 struct macsec_key key;
149 spinlock_t lock;
150 u32 next_pn;
151 refcount_t refcnt;
152 bool active;
153 struct macsec_rx_sa_stats __percpu *stats;
154 struct macsec_rx_sc *sc;
155 struct rcu_head rcu;
158 struct pcpu_rx_sc_stats {
159 struct macsec_rx_sc_stats stats;
160 struct u64_stats_sync syncp;
164 * struct macsec_rx_sc - receive secure channel
165 * @sci: secure channel identifier for this SC
166 * @active: channel is active
167 * @sa: array of secure associations
168 * @stats: per-SC stats
170 struct macsec_rx_sc {
171 struct macsec_rx_sc __rcu *next;
172 sci_t sci;
173 bool active;
174 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
175 struct pcpu_rx_sc_stats __percpu *stats;
176 refcount_t refcnt;
177 struct rcu_head rcu_head;
181 * struct macsec_tx_sa - transmit secure association
182 * @active:
183 * @next_pn: packet number to use for the next packet
184 * @lock: protects next_pn manipulations
185 * @key: key structure
186 * @stats: per-SA stats
188 struct macsec_tx_sa {
189 struct macsec_key key;
190 spinlock_t lock;
191 u32 next_pn;
192 refcount_t refcnt;
193 bool active;
194 struct macsec_tx_sa_stats __percpu *stats;
195 struct rcu_head rcu;
198 struct pcpu_tx_sc_stats {
199 struct macsec_tx_sc_stats stats;
200 struct u64_stats_sync syncp;
204 * struct macsec_tx_sc - transmit secure channel
205 * @active:
206 * @encoding_sa: association number of the SA currently in use
207 * @encrypt: encrypt packets on transmit, or authenticate only
208 * @send_sci: always include the SCI in the SecTAG
209 * @end_station:
210 * @scb: single copy broadcast flag
211 * @sa: array of secure associations
212 * @stats: stats for this TXSC
214 struct macsec_tx_sc {
215 bool active;
216 u8 encoding_sa;
217 bool encrypt;
218 bool send_sci;
219 bool end_station;
220 bool scb;
221 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
222 struct pcpu_tx_sc_stats __percpu *stats;
225 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
228 * struct macsec_secy - MACsec Security Entity
229 * @netdev: netdevice for this SecY
230 * @n_rx_sc: number of receive secure channels configured on this SecY
231 * @sci: secure channel identifier used for tx
232 * @key_len: length of keys used by the cipher suite
233 * @icv_len: length of ICV used by the cipher suite
234 * @validate_frames: validation mode
235 * @operational: MAC_Operational flag
236 * @protect_frames: enable protection for this SecY
237 * @replay_protect: enable packet number checks on receive
238 * @replay_window: size of the replay window
239 * @tx_sc: transmit secure channel
240 * @rx_sc: linked list of receive secure channels
242 struct macsec_secy {
243 struct net_device *netdev;
244 unsigned int n_rx_sc;
245 sci_t sci;
246 u16 key_len;
247 u16 icv_len;
248 enum macsec_validation_type validate_frames;
249 bool operational;
250 bool protect_frames;
251 bool replay_protect;
252 u32 replay_window;
253 struct macsec_tx_sc tx_sc;
254 struct macsec_rx_sc __rcu *rx_sc;
257 struct pcpu_secy_stats {
258 struct macsec_dev_stats stats;
259 struct u64_stats_sync syncp;
263 * struct macsec_dev - private data
264 * @secy: SecY config
265 * @real_dev: pointer to underlying netdevice
266 * @stats: MACsec device stats
267 * @secys: linked list of SecY's on the underlying device
269 struct macsec_dev {
270 struct macsec_secy secy;
271 struct net_device *real_dev;
272 struct pcpu_secy_stats __percpu *stats;
273 struct list_head secys;
274 struct gro_cells gro_cells;
275 unsigned int nest_level;
279 * struct macsec_rxh_data - rx_handler private argument
280 * @secys: linked list of SecY's on this underlying device
282 struct macsec_rxh_data {
283 struct list_head secys;
286 static struct macsec_dev *macsec_priv(const struct net_device *dev)
288 return (struct macsec_dev *)netdev_priv(dev);
291 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
293 return rcu_dereference_bh(dev->rx_handler_data);
296 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
298 return rtnl_dereference(dev->rx_handler_data);
301 struct macsec_cb {
302 struct aead_request *req;
303 union {
304 struct macsec_tx_sa *tx_sa;
305 struct macsec_rx_sa *rx_sa;
307 u8 assoc_num;
308 bool valid;
309 bool has_sci;
312 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
314 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
316 if (!sa || !sa->active)
317 return NULL;
319 if (!refcount_inc_not_zero(&sa->refcnt))
320 return NULL;
322 return sa;
325 static void free_rx_sc_rcu(struct rcu_head *head)
327 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
329 free_percpu(rx_sc->stats);
330 kfree(rx_sc);
333 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
335 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
338 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
340 if (refcount_dec_and_test(&sc->refcnt))
341 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
344 static void free_rxsa(struct rcu_head *head)
346 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
348 crypto_free_aead(sa->key.tfm);
349 free_percpu(sa->stats);
350 kfree(sa);
353 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
355 if (refcount_dec_and_test(&sa->refcnt))
356 call_rcu(&sa->rcu, free_rxsa);
359 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
361 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
363 if (!sa || !sa->active)
364 return NULL;
366 if (!refcount_inc_not_zero(&sa->refcnt))
367 return NULL;
369 return sa;
372 static void free_txsa(struct rcu_head *head)
374 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
376 crypto_free_aead(sa->key.tfm);
377 free_percpu(sa->stats);
378 kfree(sa);
381 static void macsec_txsa_put(struct macsec_tx_sa *sa)
383 if (refcount_dec_and_test(&sa->refcnt))
384 call_rcu(&sa->rcu, free_txsa);
387 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
389 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
390 return (struct macsec_cb *)skb->cb;
393 #define MACSEC_PORT_ES (htons(0x0001))
394 #define MACSEC_PORT_SCB (0x0000)
395 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
397 #define MACSEC_GCM_AES_128_SAK_LEN 16
398 #define MACSEC_GCM_AES_256_SAK_LEN 32
400 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
401 #define DEFAULT_SEND_SCI true
402 #define DEFAULT_ENCRYPT false
403 #define DEFAULT_ENCODING_SA 0
405 static bool send_sci(const struct macsec_secy *secy)
407 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
409 return tx_sc->send_sci ||
410 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
413 static sci_t make_sci(u8 *addr, __be16 port)
415 sci_t sci;
417 memcpy(&sci, addr, ETH_ALEN);
418 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
420 return sci;
423 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
425 sci_t sci;
427 if (sci_present)
428 memcpy(&sci, hdr->secure_channel_id,
429 sizeof(hdr->secure_channel_id));
430 else
431 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
433 return sci;
436 static unsigned int macsec_sectag_len(bool sci_present)
438 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
441 static unsigned int macsec_hdr_len(bool sci_present)
443 return macsec_sectag_len(sci_present) + ETH_HLEN;
446 static unsigned int macsec_extra_len(bool sci_present)
448 return macsec_sectag_len(sci_present) + sizeof(__be16);
451 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
452 static void macsec_fill_sectag(struct macsec_eth_header *h,
453 const struct macsec_secy *secy, u32 pn,
454 bool sci_present)
456 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
458 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
459 h->eth.h_proto = htons(ETH_P_MACSEC);
461 if (sci_present) {
462 h->tci_an |= MACSEC_TCI_SC;
463 memcpy(&h->secure_channel_id, &secy->sci,
464 sizeof(h->secure_channel_id));
465 } else {
466 if (tx_sc->end_station)
467 h->tci_an |= MACSEC_TCI_ES;
468 if (tx_sc->scb)
469 h->tci_an |= MACSEC_TCI_SCB;
472 h->packet_number = htonl(pn);
474 /* with GCM, C/E clear for !encrypt, both set for encrypt */
475 if (tx_sc->encrypt)
476 h->tci_an |= MACSEC_TCI_CONFID;
477 else if (secy->icv_len != DEFAULT_ICV_LEN)
478 h->tci_an |= MACSEC_TCI_C;
480 h->tci_an |= tx_sc->encoding_sa;
483 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
485 if (data_len < MIN_NON_SHORT_LEN)
486 h->short_length = data_len;
489 /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
490 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
492 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
493 int len = skb->len - 2 * ETH_ALEN;
494 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
496 /* a) It comprises at least 17 octets */
497 if (skb->len <= 16)
498 return false;
500 /* b) MACsec EtherType: already checked */
502 /* c) V bit is clear */
503 if (h->tci_an & MACSEC_TCI_VERSION)
504 return false;
506 /* d) ES or SCB => !SC */
507 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
508 (h->tci_an & MACSEC_TCI_SC))
509 return false;
511 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
512 if (h->unused)
513 return false;
515 /* rx.pn != 0 (figure 10-5) */
516 if (!h->packet_number)
517 return false;
519 /* length check, f) g) h) i) */
520 if (h->short_length)
521 return len == extra_len + h->short_length;
522 return len >= extra_len + MIN_NON_SHORT_LEN;
525 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
526 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
528 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
530 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
532 gcm_iv->sci = sci;
533 gcm_iv->pn = htonl(pn);
536 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
538 return (struct macsec_eth_header *)skb_mac_header(skb);
541 static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
543 u32 pn;
545 spin_lock_bh(&tx_sa->lock);
546 pn = tx_sa->next_pn;
548 tx_sa->next_pn++;
549 if (tx_sa->next_pn == 0) {
550 pr_debug("PN wrapped, transitioning to !oper\n");
551 tx_sa->active = false;
552 if (secy->protect_frames)
553 secy->operational = false;
555 spin_unlock_bh(&tx_sa->lock);
557 return pn;
560 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
562 struct macsec_dev *macsec = netdev_priv(dev);
564 skb->dev = macsec->real_dev;
565 skb_reset_mac_header(skb);
566 skb->protocol = eth_hdr(skb)->h_proto;
569 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
570 struct macsec_tx_sa *tx_sa)
572 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
574 u64_stats_update_begin(&txsc_stats->syncp);
575 if (tx_sc->encrypt) {
576 txsc_stats->stats.OutOctetsEncrypted += skb->len;
577 txsc_stats->stats.OutPktsEncrypted++;
578 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
579 } else {
580 txsc_stats->stats.OutOctetsProtected += skb->len;
581 txsc_stats->stats.OutPktsProtected++;
582 this_cpu_inc(tx_sa->stats->OutPktsProtected);
584 u64_stats_update_end(&txsc_stats->syncp);
587 static void count_tx(struct net_device *dev, int ret, int len)
589 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
590 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
592 u64_stats_update_begin(&stats->syncp);
593 stats->tx_packets++;
594 stats->tx_bytes += len;
595 u64_stats_update_end(&stats->syncp);
599 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
601 struct sk_buff *skb = base->data;
602 struct net_device *dev = skb->dev;
603 struct macsec_dev *macsec = macsec_priv(dev);
604 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
605 int len, ret;
607 aead_request_free(macsec_skb_cb(skb)->req);
609 rcu_read_lock_bh();
610 macsec_encrypt_finish(skb, dev);
611 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
612 len = skb->len;
613 ret = dev_queue_xmit(skb);
614 count_tx(dev, ret, len);
615 rcu_read_unlock_bh();
617 macsec_txsa_put(sa);
618 dev_put(dev);
621 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
622 unsigned char **iv,
623 struct scatterlist **sg,
624 int num_frags)
626 size_t size, iv_offset, sg_offset;
627 struct aead_request *req;
628 void *tmp;
630 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
631 iv_offset = size;
632 size += GCM_AES_IV_LEN;
634 size = ALIGN(size, __alignof__(struct scatterlist));
635 sg_offset = size;
636 size += sizeof(struct scatterlist) * num_frags;
638 tmp = kmalloc(size, GFP_ATOMIC);
639 if (!tmp)
640 return NULL;
642 *iv = (unsigned char *)(tmp + iv_offset);
643 *sg = (struct scatterlist *)(tmp + sg_offset);
644 req = tmp;
646 aead_request_set_tfm(req, tfm);
648 return req;
651 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
652 struct net_device *dev)
654 int ret;
655 struct scatterlist *sg;
656 struct sk_buff *trailer;
657 unsigned char *iv;
658 struct ethhdr *eth;
659 struct macsec_eth_header *hh;
660 size_t unprotected_len;
661 struct aead_request *req;
662 struct macsec_secy *secy;
663 struct macsec_tx_sc *tx_sc;
664 struct macsec_tx_sa *tx_sa;
665 struct macsec_dev *macsec = macsec_priv(dev);
666 bool sci_present;
667 u32 pn;
669 secy = &macsec->secy;
670 tx_sc = &secy->tx_sc;
672 /* 10.5.1 TX SA assignment */
673 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
674 if (!tx_sa) {
675 secy->operational = false;
676 kfree_skb(skb);
677 return ERR_PTR(-EINVAL);
680 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
681 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
682 struct sk_buff *nskb = skb_copy_expand(skb,
683 MACSEC_NEEDED_HEADROOM,
684 MACSEC_NEEDED_TAILROOM,
685 GFP_ATOMIC);
686 if (likely(nskb)) {
687 consume_skb(skb);
688 skb = nskb;
689 } else {
690 macsec_txsa_put(tx_sa);
691 kfree_skb(skb);
692 return ERR_PTR(-ENOMEM);
694 } else {
695 skb = skb_unshare(skb, GFP_ATOMIC);
696 if (!skb) {
697 macsec_txsa_put(tx_sa);
698 return ERR_PTR(-ENOMEM);
702 unprotected_len = skb->len;
703 eth = eth_hdr(skb);
704 sci_present = send_sci(secy);
705 hh = skb_push(skb, macsec_extra_len(sci_present));
706 memmove(hh, eth, 2 * ETH_ALEN);
708 pn = tx_sa_update_pn(tx_sa, secy);
709 if (pn == 0) {
710 macsec_txsa_put(tx_sa);
711 kfree_skb(skb);
712 return ERR_PTR(-ENOLINK);
714 macsec_fill_sectag(hh, secy, pn, sci_present);
715 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
717 skb_put(skb, secy->icv_len);
719 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
720 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
722 u64_stats_update_begin(&secy_stats->syncp);
723 secy_stats->stats.OutPktsTooLong++;
724 u64_stats_update_end(&secy_stats->syncp);
726 macsec_txsa_put(tx_sa);
727 kfree_skb(skb);
728 return ERR_PTR(-EINVAL);
731 ret = skb_cow_data(skb, 0, &trailer);
732 if (unlikely(ret < 0)) {
733 macsec_txsa_put(tx_sa);
734 kfree_skb(skb);
735 return ERR_PTR(ret);
738 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
739 if (!req) {
740 macsec_txsa_put(tx_sa);
741 kfree_skb(skb);
742 return ERR_PTR(-ENOMEM);
745 macsec_fill_iv(iv, secy->sci, pn);
747 sg_init_table(sg, ret);
748 ret = skb_to_sgvec(skb, sg, 0, skb->len);
749 if (unlikely(ret < 0)) {
750 aead_request_free(req);
751 macsec_txsa_put(tx_sa);
752 kfree_skb(skb);
753 return ERR_PTR(ret);
756 if (tx_sc->encrypt) {
757 int len = skb->len - macsec_hdr_len(sci_present) -
758 secy->icv_len;
759 aead_request_set_crypt(req, sg, sg, len, iv);
760 aead_request_set_ad(req, macsec_hdr_len(sci_present));
761 } else {
762 aead_request_set_crypt(req, sg, sg, 0, iv);
763 aead_request_set_ad(req, skb->len - secy->icv_len);
766 macsec_skb_cb(skb)->req = req;
767 macsec_skb_cb(skb)->tx_sa = tx_sa;
768 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
770 dev_hold(skb->dev);
771 ret = crypto_aead_encrypt(req);
772 if (ret == -EINPROGRESS) {
773 return ERR_PTR(ret);
774 } else if (ret != 0) {
775 dev_put(skb->dev);
776 kfree_skb(skb);
777 aead_request_free(req);
778 macsec_txsa_put(tx_sa);
779 return ERR_PTR(-EINVAL);
782 dev_put(skb->dev);
783 aead_request_free(req);
784 macsec_txsa_put(tx_sa);
786 return skb;
789 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
791 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
792 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
793 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
794 u32 lowest_pn = 0;
796 spin_lock(&rx_sa->lock);
797 if (rx_sa->next_pn >= secy->replay_window)
798 lowest_pn = rx_sa->next_pn - secy->replay_window;
800 /* Now perform replay protection check again
801 * (see IEEE 802.1AE-2006 figure 10-5)
803 if (secy->replay_protect && pn < lowest_pn) {
804 spin_unlock(&rx_sa->lock);
805 u64_stats_update_begin(&rxsc_stats->syncp);
806 rxsc_stats->stats.InPktsLate++;
807 u64_stats_update_end(&rxsc_stats->syncp);
808 return false;
811 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
812 u64_stats_update_begin(&rxsc_stats->syncp);
813 if (hdr->tci_an & MACSEC_TCI_E)
814 rxsc_stats->stats.InOctetsDecrypted += skb->len;
815 else
816 rxsc_stats->stats.InOctetsValidated += skb->len;
817 u64_stats_update_end(&rxsc_stats->syncp);
820 if (!macsec_skb_cb(skb)->valid) {
821 spin_unlock(&rx_sa->lock);
823 /* 10.6.5 */
824 if (hdr->tci_an & MACSEC_TCI_C ||
825 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
826 u64_stats_update_begin(&rxsc_stats->syncp);
827 rxsc_stats->stats.InPktsNotValid++;
828 u64_stats_update_end(&rxsc_stats->syncp);
829 return false;
832 u64_stats_update_begin(&rxsc_stats->syncp);
833 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
834 rxsc_stats->stats.InPktsInvalid++;
835 this_cpu_inc(rx_sa->stats->InPktsInvalid);
836 } else if (pn < lowest_pn) {
837 rxsc_stats->stats.InPktsDelayed++;
838 } else {
839 rxsc_stats->stats.InPktsUnchecked++;
841 u64_stats_update_end(&rxsc_stats->syncp);
842 } else {
843 u64_stats_update_begin(&rxsc_stats->syncp);
844 if (pn < lowest_pn) {
845 rxsc_stats->stats.InPktsDelayed++;
846 } else {
847 rxsc_stats->stats.InPktsOK++;
848 this_cpu_inc(rx_sa->stats->InPktsOK);
850 u64_stats_update_end(&rxsc_stats->syncp);
852 if (pn >= rx_sa->next_pn)
853 rx_sa->next_pn = pn + 1;
854 spin_unlock(&rx_sa->lock);
857 return true;
860 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
862 skb->pkt_type = PACKET_HOST;
863 skb->protocol = eth_type_trans(skb, dev);
865 skb_reset_network_header(skb);
866 if (!skb_transport_header_was_set(skb))
867 skb_reset_transport_header(skb);
868 skb_reset_mac_len(skb);
871 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
873 skb->ip_summed = CHECKSUM_NONE;
874 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
875 skb_pull(skb, hdr_len);
876 pskb_trim_unique(skb, skb->len - icv_len);
879 static void count_rx(struct net_device *dev, int len)
881 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
883 u64_stats_update_begin(&stats->syncp);
884 stats->rx_packets++;
885 stats->rx_bytes += len;
886 u64_stats_update_end(&stats->syncp);
889 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
891 struct sk_buff *skb = base->data;
892 struct net_device *dev = skb->dev;
893 struct macsec_dev *macsec = macsec_priv(dev);
894 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
895 struct macsec_rx_sc *rx_sc = rx_sa->sc;
896 int len;
897 u32 pn;
899 aead_request_free(macsec_skb_cb(skb)->req);
901 if (!err)
902 macsec_skb_cb(skb)->valid = true;
904 rcu_read_lock_bh();
905 pn = ntohl(macsec_ethhdr(skb)->packet_number);
906 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
907 rcu_read_unlock_bh();
908 kfree_skb(skb);
909 goto out;
912 macsec_finalize_skb(skb, macsec->secy.icv_len,
913 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
914 macsec_reset_skb(skb, macsec->secy.netdev);
916 len = skb->len;
917 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
918 count_rx(dev, len);
920 rcu_read_unlock_bh();
922 out:
923 macsec_rxsa_put(rx_sa);
924 macsec_rxsc_put(rx_sc);
925 dev_put(dev);
928 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
929 struct net_device *dev,
930 struct macsec_rx_sa *rx_sa,
931 sci_t sci,
932 struct macsec_secy *secy)
934 int ret;
935 struct scatterlist *sg;
936 struct sk_buff *trailer;
937 unsigned char *iv;
938 struct aead_request *req;
939 struct macsec_eth_header *hdr;
940 u16 icv_len = secy->icv_len;
942 macsec_skb_cb(skb)->valid = false;
943 skb = skb_share_check(skb, GFP_ATOMIC);
944 if (!skb)
945 return ERR_PTR(-ENOMEM);
947 ret = skb_cow_data(skb, 0, &trailer);
948 if (unlikely(ret < 0)) {
949 kfree_skb(skb);
950 return ERR_PTR(ret);
952 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
953 if (!req) {
954 kfree_skb(skb);
955 return ERR_PTR(-ENOMEM);
958 hdr = (struct macsec_eth_header *)skb->data;
959 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
961 sg_init_table(sg, ret);
962 ret = skb_to_sgvec(skb, sg, 0, skb->len);
963 if (unlikely(ret < 0)) {
964 aead_request_free(req);
965 kfree_skb(skb);
966 return ERR_PTR(ret);
969 if (hdr->tci_an & MACSEC_TCI_E) {
970 /* confidentiality: ethernet + macsec header
971 * authenticated, encrypted payload
973 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
975 aead_request_set_crypt(req, sg, sg, len, iv);
976 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
977 skb = skb_unshare(skb, GFP_ATOMIC);
978 if (!skb) {
979 aead_request_free(req);
980 return ERR_PTR(-ENOMEM);
982 } else {
983 /* integrity only: all headers + data authenticated */
984 aead_request_set_crypt(req, sg, sg, icv_len, iv);
985 aead_request_set_ad(req, skb->len - icv_len);
988 macsec_skb_cb(skb)->req = req;
989 skb->dev = dev;
990 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
992 dev_hold(dev);
993 ret = crypto_aead_decrypt(req);
994 if (ret == -EINPROGRESS) {
995 return ERR_PTR(ret);
996 } else if (ret != 0) {
997 /* decryption/authentication failed
998 * 10.6 if validateFrames is disabled, deliver anyway
1000 if (ret != -EBADMSG) {
1001 kfree_skb(skb);
1002 skb = ERR_PTR(ret);
1004 } else {
1005 macsec_skb_cb(skb)->valid = true;
1007 dev_put(dev);
1009 aead_request_free(req);
1011 return skb;
1014 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
1016 struct macsec_rx_sc *rx_sc;
1018 for_each_rxsc(secy, rx_sc) {
1019 if (rx_sc->sci == sci)
1020 return rx_sc;
1023 return NULL;
1026 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1028 struct macsec_rx_sc *rx_sc;
1030 for_each_rxsc_rtnl(secy, rx_sc) {
1031 if (rx_sc->sci == sci)
1032 return rx_sc;
1035 return NULL;
1038 static void handle_not_macsec(struct sk_buff *skb)
1040 struct macsec_rxh_data *rxd;
1041 struct macsec_dev *macsec;
1043 rcu_read_lock();
1044 rxd = macsec_data_rcu(skb->dev);
1046 /* 10.6 If the management control validateFrames is not
1047 * Strict, frames without a SecTAG are received, counted, and
1048 * delivered to the Controlled Port
1050 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1051 struct sk_buff *nskb;
1052 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1054 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1055 u64_stats_update_begin(&secy_stats->syncp);
1056 secy_stats->stats.InPktsNoTag++;
1057 u64_stats_update_end(&secy_stats->syncp);
1058 continue;
1061 /* deliver on this port */
1062 nskb = skb_clone(skb, GFP_ATOMIC);
1063 if (!nskb)
1064 break;
1066 nskb->dev = macsec->secy.netdev;
1068 if (netif_rx(nskb) == NET_RX_SUCCESS) {
1069 u64_stats_update_begin(&secy_stats->syncp);
1070 secy_stats->stats.InPktsUntagged++;
1071 u64_stats_update_end(&secy_stats->syncp);
1075 rcu_read_unlock();
1078 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1080 struct sk_buff *skb = *pskb;
1081 struct net_device *dev = skb->dev;
1082 struct macsec_eth_header *hdr;
1083 struct macsec_secy *secy = NULL;
1084 struct macsec_rx_sc *rx_sc;
1085 struct macsec_rx_sa *rx_sa;
1086 struct macsec_rxh_data *rxd;
1087 struct macsec_dev *macsec;
1088 sci_t sci;
1089 u32 pn;
1090 bool cbit;
1091 struct pcpu_rx_sc_stats *rxsc_stats;
1092 struct pcpu_secy_stats *secy_stats;
1093 bool pulled_sci;
1094 int ret;
1096 if (skb_headroom(skb) < ETH_HLEN)
1097 goto drop_direct;
1099 hdr = macsec_ethhdr(skb);
1100 if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1101 handle_not_macsec(skb);
1103 /* and deliver to the uncontrolled port */
1104 return RX_HANDLER_PASS;
1107 skb = skb_unshare(skb, GFP_ATOMIC);
1108 *pskb = skb;
1109 if (!skb)
1110 return RX_HANDLER_CONSUMED;
1112 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1113 if (!pulled_sci) {
1114 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1115 goto drop_direct;
1118 hdr = macsec_ethhdr(skb);
1120 /* Frames with a SecTAG that has the TCI E bit set but the C
1121 * bit clear are discarded, as this reserved encoding is used
1122 * to identify frames with a SecTAG that are not to be
1123 * delivered to the Controlled Port.
1125 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1126 return RX_HANDLER_PASS;
1128 /* now, pull the extra length */
1129 if (hdr->tci_an & MACSEC_TCI_SC) {
1130 if (!pulled_sci)
1131 goto drop_direct;
1134 /* ethernet header is part of crypto processing */
1135 skb_push(skb, ETH_HLEN);
1137 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1138 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1139 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1141 rcu_read_lock();
1142 rxd = macsec_data_rcu(skb->dev);
1144 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1145 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1146 sc = sc ? macsec_rxsc_get(sc) : NULL;
1148 if (sc) {
1149 secy = &macsec->secy;
1150 rx_sc = sc;
1151 break;
1155 if (!secy)
1156 goto nosci;
1158 dev = secy->netdev;
1159 macsec = macsec_priv(dev);
1160 secy_stats = this_cpu_ptr(macsec->stats);
1161 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1163 if (!macsec_validate_skb(skb, secy->icv_len)) {
1164 u64_stats_update_begin(&secy_stats->syncp);
1165 secy_stats->stats.InPktsBadTag++;
1166 u64_stats_update_end(&secy_stats->syncp);
1167 goto drop_nosa;
1170 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1171 if (!rx_sa) {
1172 /* 10.6.1 if the SA is not in use */
1174 /* If validateFrames is Strict or the C bit in the
1175 * SecTAG is set, discard
1177 if (hdr->tci_an & MACSEC_TCI_C ||
1178 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1179 u64_stats_update_begin(&rxsc_stats->syncp);
1180 rxsc_stats->stats.InPktsNotUsingSA++;
1181 u64_stats_update_end(&rxsc_stats->syncp);
1182 goto drop_nosa;
1185 /* not Strict, the frame (with the SecTAG and ICV
1186 * removed) is delivered to the Controlled Port.
1188 u64_stats_update_begin(&rxsc_stats->syncp);
1189 rxsc_stats->stats.InPktsUnusedSA++;
1190 u64_stats_update_end(&rxsc_stats->syncp);
1191 goto deliver;
1194 /* First, PN check to avoid decrypting obviously wrong packets */
1195 pn = ntohl(hdr->packet_number);
1196 if (secy->replay_protect) {
1197 bool late;
1199 spin_lock(&rx_sa->lock);
1200 late = rx_sa->next_pn >= secy->replay_window &&
1201 pn < (rx_sa->next_pn - secy->replay_window);
1202 spin_unlock(&rx_sa->lock);
1204 if (late) {
1205 u64_stats_update_begin(&rxsc_stats->syncp);
1206 rxsc_stats->stats.InPktsLate++;
1207 u64_stats_update_end(&rxsc_stats->syncp);
1208 goto drop;
1212 macsec_skb_cb(skb)->rx_sa = rx_sa;
1214 /* Disabled && !changed text => skip validation */
1215 if (hdr->tci_an & MACSEC_TCI_C ||
1216 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1217 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1219 if (IS_ERR(skb)) {
1220 /* the decrypt callback needs the reference */
1221 if (PTR_ERR(skb) != -EINPROGRESS) {
1222 macsec_rxsa_put(rx_sa);
1223 macsec_rxsc_put(rx_sc);
1225 rcu_read_unlock();
1226 *pskb = NULL;
1227 return RX_HANDLER_CONSUMED;
1230 if (!macsec_post_decrypt(skb, secy, pn))
1231 goto drop;
1233 deliver:
1234 macsec_finalize_skb(skb, secy->icv_len,
1235 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1236 macsec_reset_skb(skb, secy->netdev);
1238 if (rx_sa)
1239 macsec_rxsa_put(rx_sa);
1240 macsec_rxsc_put(rx_sc);
1242 skb_orphan(skb);
1243 ret = gro_cells_receive(&macsec->gro_cells, skb);
1244 if (ret == NET_RX_SUCCESS)
1245 count_rx(dev, skb->len);
1246 else
1247 macsec->secy.netdev->stats.rx_dropped++;
1249 rcu_read_unlock();
1251 *pskb = NULL;
1252 return RX_HANDLER_CONSUMED;
1254 drop:
1255 macsec_rxsa_put(rx_sa);
1256 drop_nosa:
1257 macsec_rxsc_put(rx_sc);
1258 rcu_read_unlock();
1259 drop_direct:
1260 kfree_skb(skb);
1261 *pskb = NULL;
1262 return RX_HANDLER_CONSUMED;
1264 nosci:
1265 /* 10.6.1 if the SC is not found */
1266 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1267 if (!cbit)
1268 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1269 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1271 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1272 struct sk_buff *nskb;
1274 secy_stats = this_cpu_ptr(macsec->stats);
1276 /* If validateFrames is Strict or the C bit in the
1277 * SecTAG is set, discard
1279 if (cbit ||
1280 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1281 u64_stats_update_begin(&secy_stats->syncp);
1282 secy_stats->stats.InPktsNoSCI++;
1283 u64_stats_update_end(&secy_stats->syncp);
1284 continue;
1287 /* not strict, the frame (with the SecTAG and ICV
1288 * removed) is delivered to the Controlled Port.
1290 nskb = skb_clone(skb, GFP_ATOMIC);
1291 if (!nskb)
1292 break;
1294 macsec_reset_skb(nskb, macsec->secy.netdev);
1296 ret = netif_rx(nskb);
1297 if (ret == NET_RX_SUCCESS) {
1298 u64_stats_update_begin(&secy_stats->syncp);
1299 secy_stats->stats.InPktsUnknownSCI++;
1300 u64_stats_update_end(&secy_stats->syncp);
1301 } else {
1302 macsec->secy.netdev->stats.rx_dropped++;
1306 rcu_read_unlock();
1307 *pskb = skb;
1308 return RX_HANDLER_PASS;
1311 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1313 struct crypto_aead *tfm;
1314 int ret;
1316 /* Pick a sync gcm(aes) cipher to ensure order is preserved. */
1317 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
1319 if (IS_ERR(tfm))
1320 return tfm;
1322 ret = crypto_aead_setkey(tfm, key, key_len);
1323 if (ret < 0)
1324 goto fail;
1326 ret = crypto_aead_setauthsize(tfm, icv_len);
1327 if (ret < 0)
1328 goto fail;
1330 return tfm;
1331 fail:
1332 crypto_free_aead(tfm);
1333 return ERR_PTR(ret);
1336 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1337 int icv_len)
1339 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1340 if (!rx_sa->stats)
1341 return -ENOMEM;
1343 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1344 if (IS_ERR(rx_sa->key.tfm)) {
1345 free_percpu(rx_sa->stats);
1346 return PTR_ERR(rx_sa->key.tfm);
1349 rx_sa->active = false;
1350 rx_sa->next_pn = 1;
1351 refcount_set(&rx_sa->refcnt, 1);
1352 spin_lock_init(&rx_sa->lock);
1354 return 0;
1357 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1359 rx_sa->active = false;
1361 macsec_rxsa_put(rx_sa);
1364 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1366 int i;
1368 for (i = 0; i < MACSEC_NUM_AN; i++) {
1369 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1371 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1372 if (sa)
1373 clear_rx_sa(sa);
1376 macsec_rxsc_put(rx_sc);
1379 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1381 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1383 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1384 rx_sc;
1385 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1386 if (rx_sc->sci == sci) {
1387 if (rx_sc->active)
1388 secy->n_rx_sc--;
1389 rcu_assign_pointer(*rx_scp, rx_sc->next);
1390 return rx_sc;
1394 return NULL;
1397 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1399 struct macsec_rx_sc *rx_sc;
1400 struct macsec_dev *macsec;
1401 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1402 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1403 struct macsec_secy *secy;
1405 list_for_each_entry(macsec, &rxd->secys, secys) {
1406 if (find_rx_sc_rtnl(&macsec->secy, sci))
1407 return ERR_PTR(-EEXIST);
1410 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1411 if (!rx_sc)
1412 return ERR_PTR(-ENOMEM);
1414 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1415 if (!rx_sc->stats) {
1416 kfree(rx_sc);
1417 return ERR_PTR(-ENOMEM);
1420 rx_sc->sci = sci;
1421 rx_sc->active = true;
1422 refcount_set(&rx_sc->refcnt, 1);
1424 secy = &macsec_priv(dev)->secy;
1425 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1426 rcu_assign_pointer(secy->rx_sc, rx_sc);
1428 if (rx_sc->active)
1429 secy->n_rx_sc++;
1431 return rx_sc;
1434 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1435 int icv_len)
1437 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1438 if (!tx_sa->stats)
1439 return -ENOMEM;
1441 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1442 if (IS_ERR(tx_sa->key.tfm)) {
1443 free_percpu(tx_sa->stats);
1444 return PTR_ERR(tx_sa->key.tfm);
1447 tx_sa->active = false;
1448 refcount_set(&tx_sa->refcnt, 1);
1449 spin_lock_init(&tx_sa->lock);
1451 return 0;
1454 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1456 tx_sa->active = false;
1458 macsec_txsa_put(tx_sa);
1461 static struct genl_family macsec_fam;
1463 static struct net_device *get_dev_from_nl(struct net *net,
1464 struct nlattr **attrs)
1466 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1467 struct net_device *dev;
1469 dev = __dev_get_by_index(net, ifindex);
1470 if (!dev)
1471 return ERR_PTR(-ENODEV);
1473 if (!netif_is_macsec(dev))
1474 return ERR_PTR(-ENODEV);
1476 return dev;
1479 static sci_t nla_get_sci(const struct nlattr *nla)
1481 return (__force sci_t)nla_get_u64(nla);
1484 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1485 int padattr)
1487 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1490 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1491 struct nlattr **attrs,
1492 struct nlattr **tb_sa,
1493 struct net_device **devp,
1494 struct macsec_secy **secyp,
1495 struct macsec_tx_sc **scp,
1496 u8 *assoc_num)
1498 struct net_device *dev;
1499 struct macsec_secy *secy;
1500 struct macsec_tx_sc *tx_sc;
1501 struct macsec_tx_sa *tx_sa;
1503 if (!tb_sa[MACSEC_SA_ATTR_AN])
1504 return ERR_PTR(-EINVAL);
1506 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1508 dev = get_dev_from_nl(net, attrs);
1509 if (IS_ERR(dev))
1510 return ERR_CAST(dev);
1512 if (*assoc_num >= MACSEC_NUM_AN)
1513 return ERR_PTR(-EINVAL);
1515 secy = &macsec_priv(dev)->secy;
1516 tx_sc = &secy->tx_sc;
1518 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1519 if (!tx_sa)
1520 return ERR_PTR(-ENODEV);
1522 *devp = dev;
1523 *scp = tx_sc;
1524 *secyp = secy;
1525 return tx_sa;
1528 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1529 struct nlattr **attrs,
1530 struct nlattr **tb_rxsc,
1531 struct net_device **devp,
1532 struct macsec_secy **secyp)
1534 struct net_device *dev;
1535 struct macsec_secy *secy;
1536 struct macsec_rx_sc *rx_sc;
1537 sci_t sci;
1539 dev = get_dev_from_nl(net, attrs);
1540 if (IS_ERR(dev))
1541 return ERR_CAST(dev);
1543 secy = &macsec_priv(dev)->secy;
1545 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1546 return ERR_PTR(-EINVAL);
1548 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1549 rx_sc = find_rx_sc_rtnl(secy, sci);
1550 if (!rx_sc)
1551 return ERR_PTR(-ENODEV);
1553 *secyp = secy;
1554 *devp = dev;
1556 return rx_sc;
1559 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1560 struct nlattr **attrs,
1561 struct nlattr **tb_rxsc,
1562 struct nlattr **tb_sa,
1563 struct net_device **devp,
1564 struct macsec_secy **secyp,
1565 struct macsec_rx_sc **scp,
1566 u8 *assoc_num)
1568 struct macsec_rx_sc *rx_sc;
1569 struct macsec_rx_sa *rx_sa;
1571 if (!tb_sa[MACSEC_SA_ATTR_AN])
1572 return ERR_PTR(-EINVAL);
1574 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1575 if (*assoc_num >= MACSEC_NUM_AN)
1576 return ERR_PTR(-EINVAL);
1578 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1579 if (IS_ERR(rx_sc))
1580 return ERR_CAST(rx_sc);
1582 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1583 if (!rx_sa)
1584 return ERR_PTR(-ENODEV);
1586 *scp = rx_sc;
1587 return rx_sa;
1591 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1592 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1593 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1594 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1597 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1598 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1599 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1602 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1603 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1604 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1605 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1606 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1607 .len = MACSEC_KEYID_LEN, },
1608 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1609 .len = MACSEC_MAX_KEY_LEN, },
1612 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1614 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1615 return -EINVAL;
1617 if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX,
1618 attrs[MACSEC_ATTR_SA_CONFIG],
1619 macsec_genl_sa_policy, NULL))
1620 return -EINVAL;
1622 return 0;
1625 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1627 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1628 return -EINVAL;
1630 if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX,
1631 attrs[MACSEC_ATTR_RXSC_CONFIG],
1632 macsec_genl_rxsc_policy, NULL))
1633 return -EINVAL;
1635 return 0;
1638 static bool validate_add_rxsa(struct nlattr **attrs)
1640 if (!attrs[MACSEC_SA_ATTR_AN] ||
1641 !attrs[MACSEC_SA_ATTR_KEY] ||
1642 !attrs[MACSEC_SA_ATTR_KEYID])
1643 return false;
1645 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1646 return false;
1648 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1649 return false;
1651 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1652 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1653 return false;
1656 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1657 return false;
1659 return true;
1662 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1664 struct net_device *dev;
1665 struct nlattr **attrs = info->attrs;
1666 struct macsec_secy *secy;
1667 struct macsec_rx_sc *rx_sc;
1668 struct macsec_rx_sa *rx_sa;
1669 unsigned char assoc_num;
1670 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1671 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1672 int err;
1674 if (!attrs[MACSEC_ATTR_IFINDEX])
1675 return -EINVAL;
1677 if (parse_sa_config(attrs, tb_sa))
1678 return -EINVAL;
1680 if (parse_rxsc_config(attrs, tb_rxsc))
1681 return -EINVAL;
1683 if (!validate_add_rxsa(tb_sa))
1684 return -EINVAL;
1686 rtnl_lock();
1687 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1688 if (IS_ERR(rx_sc)) {
1689 rtnl_unlock();
1690 return PTR_ERR(rx_sc);
1693 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1695 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1696 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1697 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1698 rtnl_unlock();
1699 return -EINVAL;
1702 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1703 if (rx_sa) {
1704 rtnl_unlock();
1705 return -EBUSY;
1708 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1709 if (!rx_sa) {
1710 rtnl_unlock();
1711 return -ENOMEM;
1714 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1715 secy->key_len, secy->icv_len);
1716 if (err < 0) {
1717 kfree(rx_sa);
1718 rtnl_unlock();
1719 return err;
1722 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1723 spin_lock_bh(&rx_sa->lock);
1724 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1725 spin_unlock_bh(&rx_sa->lock);
1728 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1729 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1731 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1732 rx_sa->sc = rx_sc;
1733 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1735 rtnl_unlock();
1737 return 0;
1740 static bool validate_add_rxsc(struct nlattr **attrs)
1742 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1743 return false;
1745 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1746 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1747 return false;
1750 return true;
1753 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1755 struct net_device *dev;
1756 sci_t sci = MACSEC_UNDEF_SCI;
1757 struct nlattr **attrs = info->attrs;
1758 struct macsec_rx_sc *rx_sc;
1759 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1761 if (!attrs[MACSEC_ATTR_IFINDEX])
1762 return -EINVAL;
1764 if (parse_rxsc_config(attrs, tb_rxsc))
1765 return -EINVAL;
1767 if (!validate_add_rxsc(tb_rxsc))
1768 return -EINVAL;
1770 rtnl_lock();
1771 dev = get_dev_from_nl(genl_info_net(info), attrs);
1772 if (IS_ERR(dev)) {
1773 rtnl_unlock();
1774 return PTR_ERR(dev);
1777 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1779 rx_sc = create_rx_sc(dev, sci);
1780 if (IS_ERR(rx_sc)) {
1781 rtnl_unlock();
1782 return PTR_ERR(rx_sc);
1785 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1786 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1788 rtnl_unlock();
1790 return 0;
1793 static bool validate_add_txsa(struct nlattr **attrs)
1795 if (!attrs[MACSEC_SA_ATTR_AN] ||
1796 !attrs[MACSEC_SA_ATTR_PN] ||
1797 !attrs[MACSEC_SA_ATTR_KEY] ||
1798 !attrs[MACSEC_SA_ATTR_KEYID])
1799 return false;
1801 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1802 return false;
1804 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1805 return false;
1807 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1808 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1809 return false;
1812 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1813 return false;
1815 return true;
1818 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1820 struct net_device *dev;
1821 struct nlattr **attrs = info->attrs;
1822 struct macsec_secy *secy;
1823 struct macsec_tx_sc *tx_sc;
1824 struct macsec_tx_sa *tx_sa;
1825 unsigned char assoc_num;
1826 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1827 int err;
1829 if (!attrs[MACSEC_ATTR_IFINDEX])
1830 return -EINVAL;
1832 if (parse_sa_config(attrs, tb_sa))
1833 return -EINVAL;
1835 if (!validate_add_txsa(tb_sa))
1836 return -EINVAL;
1838 rtnl_lock();
1839 dev = get_dev_from_nl(genl_info_net(info), attrs);
1840 if (IS_ERR(dev)) {
1841 rtnl_unlock();
1842 return PTR_ERR(dev);
1845 secy = &macsec_priv(dev)->secy;
1846 tx_sc = &secy->tx_sc;
1848 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1850 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1851 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1852 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1853 rtnl_unlock();
1854 return -EINVAL;
1857 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1858 if (tx_sa) {
1859 rtnl_unlock();
1860 return -EBUSY;
1863 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1864 if (!tx_sa) {
1865 rtnl_unlock();
1866 return -ENOMEM;
1869 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1870 secy->key_len, secy->icv_len);
1871 if (err < 0) {
1872 kfree(tx_sa);
1873 rtnl_unlock();
1874 return err;
1877 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1879 spin_lock_bh(&tx_sa->lock);
1880 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1881 spin_unlock_bh(&tx_sa->lock);
1883 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1884 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1886 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1887 secy->operational = true;
1889 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1891 rtnl_unlock();
1893 return 0;
1896 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1898 struct nlattr **attrs = info->attrs;
1899 struct net_device *dev;
1900 struct macsec_secy *secy;
1901 struct macsec_rx_sc *rx_sc;
1902 struct macsec_rx_sa *rx_sa;
1903 u8 assoc_num;
1904 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1905 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1907 if (!attrs[MACSEC_ATTR_IFINDEX])
1908 return -EINVAL;
1910 if (parse_sa_config(attrs, tb_sa))
1911 return -EINVAL;
1913 if (parse_rxsc_config(attrs, tb_rxsc))
1914 return -EINVAL;
1916 rtnl_lock();
1917 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1918 &dev, &secy, &rx_sc, &assoc_num);
1919 if (IS_ERR(rx_sa)) {
1920 rtnl_unlock();
1921 return PTR_ERR(rx_sa);
1924 if (rx_sa->active) {
1925 rtnl_unlock();
1926 return -EBUSY;
1929 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1930 clear_rx_sa(rx_sa);
1932 rtnl_unlock();
1934 return 0;
1937 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1939 struct nlattr **attrs = info->attrs;
1940 struct net_device *dev;
1941 struct macsec_secy *secy;
1942 struct macsec_rx_sc *rx_sc;
1943 sci_t sci;
1944 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1946 if (!attrs[MACSEC_ATTR_IFINDEX])
1947 return -EINVAL;
1949 if (parse_rxsc_config(attrs, tb_rxsc))
1950 return -EINVAL;
1952 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1953 return -EINVAL;
1955 rtnl_lock();
1956 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1957 if (IS_ERR(dev)) {
1958 rtnl_unlock();
1959 return PTR_ERR(dev);
1962 secy = &macsec_priv(dev)->secy;
1963 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1965 rx_sc = del_rx_sc(secy, sci);
1966 if (!rx_sc) {
1967 rtnl_unlock();
1968 return -ENODEV;
1971 free_rx_sc(rx_sc);
1972 rtnl_unlock();
1974 return 0;
1977 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1979 struct nlattr **attrs = info->attrs;
1980 struct net_device *dev;
1981 struct macsec_secy *secy;
1982 struct macsec_tx_sc *tx_sc;
1983 struct macsec_tx_sa *tx_sa;
1984 u8 assoc_num;
1985 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1987 if (!attrs[MACSEC_ATTR_IFINDEX])
1988 return -EINVAL;
1990 if (parse_sa_config(attrs, tb_sa))
1991 return -EINVAL;
1993 rtnl_lock();
1994 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
1995 &dev, &secy, &tx_sc, &assoc_num);
1996 if (IS_ERR(tx_sa)) {
1997 rtnl_unlock();
1998 return PTR_ERR(tx_sa);
2001 if (tx_sa->active) {
2002 rtnl_unlock();
2003 return -EBUSY;
2006 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2007 clear_tx_sa(tx_sa);
2009 rtnl_unlock();
2011 return 0;
2014 static bool validate_upd_sa(struct nlattr **attrs)
2016 if (!attrs[MACSEC_SA_ATTR_AN] ||
2017 attrs[MACSEC_SA_ATTR_KEY] ||
2018 attrs[MACSEC_SA_ATTR_KEYID])
2019 return false;
2021 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2022 return false;
2024 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2025 return false;
2027 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2028 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2029 return false;
2032 return true;
2035 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2037 struct nlattr **attrs = info->attrs;
2038 struct net_device *dev;
2039 struct macsec_secy *secy;
2040 struct macsec_tx_sc *tx_sc;
2041 struct macsec_tx_sa *tx_sa;
2042 u8 assoc_num;
2043 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2045 if (!attrs[MACSEC_ATTR_IFINDEX])
2046 return -EINVAL;
2048 if (parse_sa_config(attrs, tb_sa))
2049 return -EINVAL;
2051 if (!validate_upd_sa(tb_sa))
2052 return -EINVAL;
2054 rtnl_lock();
2055 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2056 &dev, &secy, &tx_sc, &assoc_num);
2057 if (IS_ERR(tx_sa)) {
2058 rtnl_unlock();
2059 return PTR_ERR(tx_sa);
2062 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2063 spin_lock_bh(&tx_sa->lock);
2064 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2065 spin_unlock_bh(&tx_sa->lock);
2068 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2069 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2071 if (assoc_num == tx_sc->encoding_sa)
2072 secy->operational = tx_sa->active;
2074 rtnl_unlock();
2076 return 0;
2079 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2081 struct nlattr **attrs = info->attrs;
2082 struct net_device *dev;
2083 struct macsec_secy *secy;
2084 struct macsec_rx_sc *rx_sc;
2085 struct macsec_rx_sa *rx_sa;
2086 u8 assoc_num;
2087 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2088 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2090 if (!attrs[MACSEC_ATTR_IFINDEX])
2091 return -EINVAL;
2093 if (parse_rxsc_config(attrs, tb_rxsc))
2094 return -EINVAL;
2096 if (parse_sa_config(attrs, tb_sa))
2097 return -EINVAL;
2099 if (!validate_upd_sa(tb_sa))
2100 return -EINVAL;
2102 rtnl_lock();
2103 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2104 &dev, &secy, &rx_sc, &assoc_num);
2105 if (IS_ERR(rx_sa)) {
2106 rtnl_unlock();
2107 return PTR_ERR(rx_sa);
2110 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2111 spin_lock_bh(&rx_sa->lock);
2112 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2113 spin_unlock_bh(&rx_sa->lock);
2116 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2117 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2119 rtnl_unlock();
2120 return 0;
2123 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2125 struct nlattr **attrs = info->attrs;
2126 struct net_device *dev;
2127 struct macsec_secy *secy;
2128 struct macsec_rx_sc *rx_sc;
2129 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2131 if (!attrs[MACSEC_ATTR_IFINDEX])
2132 return -EINVAL;
2134 if (parse_rxsc_config(attrs, tb_rxsc))
2135 return -EINVAL;
2137 if (!validate_add_rxsc(tb_rxsc))
2138 return -EINVAL;
2140 rtnl_lock();
2141 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2142 if (IS_ERR(rx_sc)) {
2143 rtnl_unlock();
2144 return PTR_ERR(rx_sc);
2147 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2148 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2150 if (rx_sc->active != new)
2151 secy->n_rx_sc += new ? 1 : -1;
2153 rx_sc->active = new;
2156 rtnl_unlock();
2158 return 0;
2161 static int copy_tx_sa_stats(struct sk_buff *skb,
2162 struct macsec_tx_sa_stats __percpu *pstats)
2164 struct macsec_tx_sa_stats sum = {0, };
2165 int cpu;
2167 for_each_possible_cpu(cpu) {
2168 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2170 sum.OutPktsProtected += stats->OutPktsProtected;
2171 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2174 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2175 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2176 return -EMSGSIZE;
2178 return 0;
2181 static int copy_rx_sa_stats(struct sk_buff *skb,
2182 struct macsec_rx_sa_stats __percpu *pstats)
2184 struct macsec_rx_sa_stats sum = {0, };
2185 int cpu;
2187 for_each_possible_cpu(cpu) {
2188 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2190 sum.InPktsOK += stats->InPktsOK;
2191 sum.InPktsInvalid += stats->InPktsInvalid;
2192 sum.InPktsNotValid += stats->InPktsNotValid;
2193 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2194 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2197 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2198 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2199 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2200 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2201 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2202 return -EMSGSIZE;
2204 return 0;
2207 static int copy_rx_sc_stats(struct sk_buff *skb,
2208 struct pcpu_rx_sc_stats __percpu *pstats)
2210 struct macsec_rx_sc_stats sum = {0, };
2211 int cpu;
2213 for_each_possible_cpu(cpu) {
2214 const struct pcpu_rx_sc_stats *stats;
2215 struct macsec_rx_sc_stats tmp;
2216 unsigned int start;
2218 stats = per_cpu_ptr(pstats, cpu);
2219 do {
2220 start = u64_stats_fetch_begin_irq(&stats->syncp);
2221 memcpy(&tmp, &stats->stats, sizeof(tmp));
2222 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2224 sum.InOctetsValidated += tmp.InOctetsValidated;
2225 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2226 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2227 sum.InPktsDelayed += tmp.InPktsDelayed;
2228 sum.InPktsOK += tmp.InPktsOK;
2229 sum.InPktsInvalid += tmp.InPktsInvalid;
2230 sum.InPktsLate += tmp.InPktsLate;
2231 sum.InPktsNotValid += tmp.InPktsNotValid;
2232 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2233 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2236 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2237 sum.InOctetsValidated,
2238 MACSEC_RXSC_STATS_ATTR_PAD) ||
2239 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2240 sum.InOctetsDecrypted,
2241 MACSEC_RXSC_STATS_ATTR_PAD) ||
2242 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2243 sum.InPktsUnchecked,
2244 MACSEC_RXSC_STATS_ATTR_PAD) ||
2245 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2246 sum.InPktsDelayed,
2247 MACSEC_RXSC_STATS_ATTR_PAD) ||
2248 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2249 sum.InPktsOK,
2250 MACSEC_RXSC_STATS_ATTR_PAD) ||
2251 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2252 sum.InPktsInvalid,
2253 MACSEC_RXSC_STATS_ATTR_PAD) ||
2254 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2255 sum.InPktsLate,
2256 MACSEC_RXSC_STATS_ATTR_PAD) ||
2257 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2258 sum.InPktsNotValid,
2259 MACSEC_RXSC_STATS_ATTR_PAD) ||
2260 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2261 sum.InPktsNotUsingSA,
2262 MACSEC_RXSC_STATS_ATTR_PAD) ||
2263 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2264 sum.InPktsUnusedSA,
2265 MACSEC_RXSC_STATS_ATTR_PAD))
2266 return -EMSGSIZE;
2268 return 0;
2271 static int copy_tx_sc_stats(struct sk_buff *skb,
2272 struct pcpu_tx_sc_stats __percpu *pstats)
2274 struct macsec_tx_sc_stats sum = {0, };
2275 int cpu;
2277 for_each_possible_cpu(cpu) {
2278 const struct pcpu_tx_sc_stats *stats;
2279 struct macsec_tx_sc_stats tmp;
2280 unsigned int start;
2282 stats = per_cpu_ptr(pstats, cpu);
2283 do {
2284 start = u64_stats_fetch_begin_irq(&stats->syncp);
2285 memcpy(&tmp, &stats->stats, sizeof(tmp));
2286 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2288 sum.OutPktsProtected += tmp.OutPktsProtected;
2289 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2290 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2291 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2294 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2295 sum.OutPktsProtected,
2296 MACSEC_TXSC_STATS_ATTR_PAD) ||
2297 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2298 sum.OutPktsEncrypted,
2299 MACSEC_TXSC_STATS_ATTR_PAD) ||
2300 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2301 sum.OutOctetsProtected,
2302 MACSEC_TXSC_STATS_ATTR_PAD) ||
2303 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2304 sum.OutOctetsEncrypted,
2305 MACSEC_TXSC_STATS_ATTR_PAD))
2306 return -EMSGSIZE;
2308 return 0;
2311 static int copy_secy_stats(struct sk_buff *skb,
2312 struct pcpu_secy_stats __percpu *pstats)
2314 struct macsec_dev_stats sum = {0, };
2315 int cpu;
2317 for_each_possible_cpu(cpu) {
2318 const struct pcpu_secy_stats *stats;
2319 struct macsec_dev_stats tmp;
2320 unsigned int start;
2322 stats = per_cpu_ptr(pstats, cpu);
2323 do {
2324 start = u64_stats_fetch_begin_irq(&stats->syncp);
2325 memcpy(&tmp, &stats->stats, sizeof(tmp));
2326 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2328 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2329 sum.InPktsUntagged += tmp.InPktsUntagged;
2330 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2331 sum.InPktsNoTag += tmp.InPktsNoTag;
2332 sum.InPktsBadTag += tmp.InPktsBadTag;
2333 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2334 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2335 sum.InPktsOverrun += tmp.InPktsOverrun;
2338 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2339 sum.OutPktsUntagged,
2340 MACSEC_SECY_STATS_ATTR_PAD) ||
2341 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2342 sum.InPktsUntagged,
2343 MACSEC_SECY_STATS_ATTR_PAD) ||
2344 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2345 sum.OutPktsTooLong,
2346 MACSEC_SECY_STATS_ATTR_PAD) ||
2347 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2348 sum.InPktsNoTag,
2349 MACSEC_SECY_STATS_ATTR_PAD) ||
2350 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2351 sum.InPktsBadTag,
2352 MACSEC_SECY_STATS_ATTR_PAD) ||
2353 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2354 sum.InPktsUnknownSCI,
2355 MACSEC_SECY_STATS_ATTR_PAD) ||
2356 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2357 sum.InPktsNoSCI,
2358 MACSEC_SECY_STATS_ATTR_PAD) ||
2359 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2360 sum.InPktsOverrun,
2361 MACSEC_SECY_STATS_ATTR_PAD))
2362 return -EMSGSIZE;
2364 return 0;
2367 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2369 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2370 struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
2371 u64 csid;
2373 if (!secy_nest)
2374 return 1;
2376 switch (secy->key_len) {
2377 case MACSEC_GCM_AES_128_SAK_LEN:
2378 csid = MACSEC_DEFAULT_CIPHER_ID;
2379 break;
2380 case MACSEC_GCM_AES_256_SAK_LEN:
2381 csid = MACSEC_CIPHER_ID_GCM_AES_256;
2382 break;
2383 default:
2384 goto cancel;
2387 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2388 MACSEC_SECY_ATTR_PAD) ||
2389 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2390 csid, MACSEC_SECY_ATTR_PAD) ||
2391 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2392 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2393 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2394 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2395 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2396 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2397 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2398 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2399 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2400 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2401 goto cancel;
2403 if (secy->replay_protect) {
2404 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2405 goto cancel;
2408 nla_nest_end(skb, secy_nest);
2409 return 0;
2411 cancel:
2412 nla_nest_cancel(skb, secy_nest);
2413 return 1;
2416 static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
2417 struct sk_buff *skb, struct netlink_callback *cb)
2419 struct macsec_rx_sc *rx_sc;
2420 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2421 struct nlattr *txsa_list, *rxsc_list;
2422 int i, j;
2423 void *hdr;
2424 struct nlattr *attr;
2426 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2427 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2428 if (!hdr)
2429 return -EMSGSIZE;
2431 genl_dump_check_consistent(cb, hdr);
2433 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2434 goto nla_put_failure;
2436 if (nla_put_secy(secy, skb))
2437 goto nla_put_failure;
2439 attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
2440 if (!attr)
2441 goto nla_put_failure;
2442 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2443 nla_nest_cancel(skb, attr);
2444 goto nla_put_failure;
2446 nla_nest_end(skb, attr);
2448 attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
2449 if (!attr)
2450 goto nla_put_failure;
2451 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2452 nla_nest_cancel(skb, attr);
2453 goto nla_put_failure;
2455 nla_nest_end(skb, attr);
2457 txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
2458 if (!txsa_list)
2459 goto nla_put_failure;
2460 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2461 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2462 struct nlattr *txsa_nest;
2464 if (!tx_sa)
2465 continue;
2467 txsa_nest = nla_nest_start(skb, j++);
2468 if (!txsa_nest) {
2469 nla_nest_cancel(skb, txsa_list);
2470 goto nla_put_failure;
2473 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2474 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2475 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2476 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2477 nla_nest_cancel(skb, txsa_nest);
2478 nla_nest_cancel(skb, txsa_list);
2479 goto nla_put_failure;
2482 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2483 if (!attr) {
2484 nla_nest_cancel(skb, txsa_nest);
2485 nla_nest_cancel(skb, txsa_list);
2486 goto nla_put_failure;
2488 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2489 nla_nest_cancel(skb, attr);
2490 nla_nest_cancel(skb, txsa_nest);
2491 nla_nest_cancel(skb, txsa_list);
2492 goto nla_put_failure;
2494 nla_nest_end(skb, attr);
2496 nla_nest_end(skb, txsa_nest);
2498 nla_nest_end(skb, txsa_list);
2500 rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
2501 if (!rxsc_list)
2502 goto nla_put_failure;
2504 j = 1;
2505 for_each_rxsc_rtnl(secy, rx_sc) {
2506 int k;
2507 struct nlattr *rxsa_list;
2508 struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
2510 if (!rxsc_nest) {
2511 nla_nest_cancel(skb, rxsc_list);
2512 goto nla_put_failure;
2515 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2516 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2517 MACSEC_RXSC_ATTR_PAD)) {
2518 nla_nest_cancel(skb, rxsc_nest);
2519 nla_nest_cancel(skb, rxsc_list);
2520 goto nla_put_failure;
2523 attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
2524 if (!attr) {
2525 nla_nest_cancel(skb, rxsc_nest);
2526 nla_nest_cancel(skb, rxsc_list);
2527 goto nla_put_failure;
2529 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2530 nla_nest_cancel(skb, attr);
2531 nla_nest_cancel(skb, rxsc_nest);
2532 nla_nest_cancel(skb, rxsc_list);
2533 goto nla_put_failure;
2535 nla_nest_end(skb, attr);
2537 rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
2538 if (!rxsa_list) {
2539 nla_nest_cancel(skb, rxsc_nest);
2540 nla_nest_cancel(skb, rxsc_list);
2541 goto nla_put_failure;
2544 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2545 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2546 struct nlattr *rxsa_nest;
2548 if (!rx_sa)
2549 continue;
2551 rxsa_nest = nla_nest_start(skb, k++);
2552 if (!rxsa_nest) {
2553 nla_nest_cancel(skb, rxsa_list);
2554 nla_nest_cancel(skb, rxsc_nest);
2555 nla_nest_cancel(skb, rxsc_list);
2556 goto nla_put_failure;
2559 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2560 if (!attr) {
2561 nla_nest_cancel(skb, rxsa_list);
2562 nla_nest_cancel(skb, rxsc_nest);
2563 nla_nest_cancel(skb, rxsc_list);
2564 goto nla_put_failure;
2566 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2567 nla_nest_cancel(skb, attr);
2568 nla_nest_cancel(skb, rxsa_list);
2569 nla_nest_cancel(skb, rxsc_nest);
2570 nla_nest_cancel(skb, rxsc_list);
2571 goto nla_put_failure;
2573 nla_nest_end(skb, attr);
2575 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2576 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2577 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2578 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2579 nla_nest_cancel(skb, rxsa_nest);
2580 nla_nest_cancel(skb, rxsc_nest);
2581 nla_nest_cancel(skb, rxsc_list);
2582 goto nla_put_failure;
2584 nla_nest_end(skb, rxsa_nest);
2587 nla_nest_end(skb, rxsa_list);
2588 nla_nest_end(skb, rxsc_nest);
2591 nla_nest_end(skb, rxsc_list);
2593 genlmsg_end(skb, hdr);
2595 return 0;
2597 nla_put_failure:
2598 genlmsg_cancel(skb, hdr);
2599 return -EMSGSIZE;
2602 static int macsec_generation = 1; /* protected by RTNL */
2604 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2606 struct net *net = sock_net(skb->sk);
2607 struct net_device *dev;
2608 int dev_idx, d;
2610 dev_idx = cb->args[0];
2612 d = 0;
2613 rtnl_lock();
2615 cb->seq = macsec_generation;
2617 for_each_netdev(net, dev) {
2618 struct macsec_secy *secy;
2620 if (d < dev_idx)
2621 goto next;
2623 if (!netif_is_macsec(dev))
2624 goto next;
2626 secy = &macsec_priv(dev)->secy;
2627 if (dump_secy(secy, dev, skb, cb) < 0)
2628 goto done;
2629 next:
2630 d++;
2633 done:
2634 rtnl_unlock();
2635 cb->args[0] = d;
2636 return skb->len;
2639 static const struct genl_ops macsec_genl_ops[] = {
2641 .cmd = MACSEC_CMD_GET_TXSC,
2642 .dumpit = macsec_dump_txsc,
2643 .policy = macsec_genl_policy,
2646 .cmd = MACSEC_CMD_ADD_RXSC,
2647 .doit = macsec_add_rxsc,
2648 .policy = macsec_genl_policy,
2649 .flags = GENL_ADMIN_PERM,
2652 .cmd = MACSEC_CMD_DEL_RXSC,
2653 .doit = macsec_del_rxsc,
2654 .policy = macsec_genl_policy,
2655 .flags = GENL_ADMIN_PERM,
2658 .cmd = MACSEC_CMD_UPD_RXSC,
2659 .doit = macsec_upd_rxsc,
2660 .policy = macsec_genl_policy,
2661 .flags = GENL_ADMIN_PERM,
2664 .cmd = MACSEC_CMD_ADD_TXSA,
2665 .doit = macsec_add_txsa,
2666 .policy = macsec_genl_policy,
2667 .flags = GENL_ADMIN_PERM,
2670 .cmd = MACSEC_CMD_DEL_TXSA,
2671 .doit = macsec_del_txsa,
2672 .policy = macsec_genl_policy,
2673 .flags = GENL_ADMIN_PERM,
2676 .cmd = MACSEC_CMD_UPD_TXSA,
2677 .doit = macsec_upd_txsa,
2678 .policy = macsec_genl_policy,
2679 .flags = GENL_ADMIN_PERM,
2682 .cmd = MACSEC_CMD_ADD_RXSA,
2683 .doit = macsec_add_rxsa,
2684 .policy = macsec_genl_policy,
2685 .flags = GENL_ADMIN_PERM,
2688 .cmd = MACSEC_CMD_DEL_RXSA,
2689 .doit = macsec_del_rxsa,
2690 .policy = macsec_genl_policy,
2691 .flags = GENL_ADMIN_PERM,
2694 .cmd = MACSEC_CMD_UPD_RXSA,
2695 .doit = macsec_upd_rxsa,
2696 .policy = macsec_genl_policy,
2697 .flags = GENL_ADMIN_PERM,
2701 static struct genl_family macsec_fam __ro_after_init = {
2702 .name = MACSEC_GENL_NAME,
2703 .hdrsize = 0,
2704 .version = MACSEC_GENL_VERSION,
2705 .maxattr = MACSEC_ATTR_MAX,
2706 .netnsok = true,
2707 .module = THIS_MODULE,
2708 .ops = macsec_genl_ops,
2709 .n_ops = ARRAY_SIZE(macsec_genl_ops),
2712 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2713 struct net_device *dev)
2715 struct macsec_dev *macsec = netdev_priv(dev);
2716 struct macsec_secy *secy = &macsec->secy;
2717 struct pcpu_secy_stats *secy_stats;
2718 int ret, len;
2720 /* 10.5 */
2721 if (!secy->protect_frames) {
2722 secy_stats = this_cpu_ptr(macsec->stats);
2723 u64_stats_update_begin(&secy_stats->syncp);
2724 secy_stats->stats.OutPktsUntagged++;
2725 u64_stats_update_end(&secy_stats->syncp);
2726 skb->dev = macsec->real_dev;
2727 len = skb->len;
2728 ret = dev_queue_xmit(skb);
2729 count_tx(dev, ret, len);
2730 return ret;
2733 if (!secy->operational) {
2734 kfree_skb(skb);
2735 dev->stats.tx_dropped++;
2736 return NETDEV_TX_OK;
2739 skb = macsec_encrypt(skb, dev);
2740 if (IS_ERR(skb)) {
2741 if (PTR_ERR(skb) != -EINPROGRESS)
2742 dev->stats.tx_dropped++;
2743 return NETDEV_TX_OK;
2746 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2748 macsec_encrypt_finish(skb, dev);
2749 len = skb->len;
2750 ret = dev_queue_xmit(skb);
2751 count_tx(dev, ret, len);
2752 return ret;
2755 #define MACSEC_FEATURES \
2756 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
2757 static struct lock_class_key macsec_netdev_addr_lock_key;
2759 static int macsec_dev_init(struct net_device *dev)
2761 struct macsec_dev *macsec = macsec_priv(dev);
2762 struct net_device *real_dev = macsec->real_dev;
2763 int err;
2765 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2766 if (!dev->tstats)
2767 return -ENOMEM;
2769 err = gro_cells_init(&macsec->gro_cells, dev);
2770 if (err) {
2771 free_percpu(dev->tstats);
2772 return err;
2775 dev->features = real_dev->features & MACSEC_FEATURES;
2776 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2778 dev->needed_headroom = real_dev->needed_headroom +
2779 MACSEC_NEEDED_HEADROOM;
2780 dev->needed_tailroom = real_dev->needed_tailroom +
2781 MACSEC_NEEDED_TAILROOM;
2783 if (is_zero_ether_addr(dev->dev_addr))
2784 eth_hw_addr_inherit(dev, real_dev);
2785 if (is_zero_ether_addr(dev->broadcast))
2786 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2788 return 0;
2791 static void macsec_dev_uninit(struct net_device *dev)
2793 struct macsec_dev *macsec = macsec_priv(dev);
2795 gro_cells_destroy(&macsec->gro_cells);
2796 free_percpu(dev->tstats);
2799 static netdev_features_t macsec_fix_features(struct net_device *dev,
2800 netdev_features_t features)
2802 struct macsec_dev *macsec = macsec_priv(dev);
2803 struct net_device *real_dev = macsec->real_dev;
2805 features &= (real_dev->features & MACSEC_FEATURES) |
2806 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2807 features |= NETIF_F_LLTX;
2809 return features;
2812 static int macsec_dev_open(struct net_device *dev)
2814 struct macsec_dev *macsec = macsec_priv(dev);
2815 struct net_device *real_dev = macsec->real_dev;
2816 int err;
2818 err = dev_uc_add(real_dev, dev->dev_addr);
2819 if (err < 0)
2820 return err;
2822 if (dev->flags & IFF_ALLMULTI) {
2823 err = dev_set_allmulti(real_dev, 1);
2824 if (err < 0)
2825 goto del_unicast;
2828 if (dev->flags & IFF_PROMISC) {
2829 err = dev_set_promiscuity(real_dev, 1);
2830 if (err < 0)
2831 goto clear_allmulti;
2834 if (netif_carrier_ok(real_dev))
2835 netif_carrier_on(dev);
2837 return 0;
2838 clear_allmulti:
2839 if (dev->flags & IFF_ALLMULTI)
2840 dev_set_allmulti(real_dev, -1);
2841 del_unicast:
2842 dev_uc_del(real_dev, dev->dev_addr);
2843 netif_carrier_off(dev);
2844 return err;
2847 static int macsec_dev_stop(struct net_device *dev)
2849 struct macsec_dev *macsec = macsec_priv(dev);
2850 struct net_device *real_dev = macsec->real_dev;
2852 netif_carrier_off(dev);
2854 dev_mc_unsync(real_dev, dev);
2855 dev_uc_unsync(real_dev, dev);
2857 if (dev->flags & IFF_ALLMULTI)
2858 dev_set_allmulti(real_dev, -1);
2860 if (dev->flags & IFF_PROMISC)
2861 dev_set_promiscuity(real_dev, -1);
2863 dev_uc_del(real_dev, dev->dev_addr);
2865 return 0;
2868 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2870 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2872 if (!(dev->flags & IFF_UP))
2873 return;
2875 if (change & IFF_ALLMULTI)
2876 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2878 if (change & IFF_PROMISC)
2879 dev_set_promiscuity(real_dev,
2880 dev->flags & IFF_PROMISC ? 1 : -1);
2883 static void macsec_dev_set_rx_mode(struct net_device *dev)
2885 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2887 dev_mc_sync(real_dev, dev);
2888 dev_uc_sync(real_dev, dev);
2891 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
2893 return make_sci(dev->dev_addr, port);
2896 static int macsec_set_mac_address(struct net_device *dev, void *p)
2898 struct macsec_dev *macsec = macsec_priv(dev);
2899 struct net_device *real_dev = macsec->real_dev;
2900 struct sockaddr *addr = p;
2901 int err;
2903 if (!is_valid_ether_addr(addr->sa_data))
2904 return -EADDRNOTAVAIL;
2906 if (!(dev->flags & IFF_UP))
2907 goto out;
2909 err = dev_uc_add(real_dev, addr->sa_data);
2910 if (err < 0)
2911 return err;
2913 dev_uc_del(real_dev, dev->dev_addr);
2915 out:
2916 ether_addr_copy(dev->dev_addr, addr->sa_data);
2917 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
2918 return 0;
2921 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2923 struct macsec_dev *macsec = macsec_priv(dev);
2924 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2926 if (macsec->real_dev->mtu - extra < new_mtu)
2927 return -ERANGE;
2929 dev->mtu = new_mtu;
2931 return 0;
2934 static void macsec_get_stats64(struct net_device *dev,
2935 struct rtnl_link_stats64 *s)
2937 int cpu;
2939 if (!dev->tstats)
2940 return;
2942 for_each_possible_cpu(cpu) {
2943 struct pcpu_sw_netstats *stats;
2944 struct pcpu_sw_netstats tmp;
2945 int start;
2947 stats = per_cpu_ptr(dev->tstats, cpu);
2948 do {
2949 start = u64_stats_fetch_begin_irq(&stats->syncp);
2950 tmp.rx_packets = stats->rx_packets;
2951 tmp.rx_bytes = stats->rx_bytes;
2952 tmp.tx_packets = stats->tx_packets;
2953 tmp.tx_bytes = stats->tx_bytes;
2954 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2956 s->rx_packets += tmp.rx_packets;
2957 s->rx_bytes += tmp.rx_bytes;
2958 s->tx_packets += tmp.tx_packets;
2959 s->tx_bytes += tmp.tx_bytes;
2962 s->rx_dropped = dev->stats.rx_dropped;
2963 s->tx_dropped = dev->stats.tx_dropped;
2966 static int macsec_get_iflink(const struct net_device *dev)
2968 return macsec_priv(dev)->real_dev->ifindex;
2972 static int macsec_get_nest_level(struct net_device *dev)
2974 return macsec_priv(dev)->nest_level;
2978 static const struct net_device_ops macsec_netdev_ops = {
2979 .ndo_init = macsec_dev_init,
2980 .ndo_uninit = macsec_dev_uninit,
2981 .ndo_open = macsec_dev_open,
2982 .ndo_stop = macsec_dev_stop,
2983 .ndo_fix_features = macsec_fix_features,
2984 .ndo_change_mtu = macsec_change_mtu,
2985 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2986 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2987 .ndo_set_mac_address = macsec_set_mac_address,
2988 .ndo_start_xmit = macsec_start_xmit,
2989 .ndo_get_stats64 = macsec_get_stats64,
2990 .ndo_get_iflink = macsec_get_iflink,
2991 .ndo_get_lock_subclass = macsec_get_nest_level,
2994 static const struct device_type macsec_type = {
2995 .name = "macsec",
2998 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2999 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3000 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3001 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3002 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3003 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3004 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3005 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3006 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3007 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3008 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
3009 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3010 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3011 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3014 static void macsec_free_netdev(struct net_device *dev)
3016 struct macsec_dev *macsec = macsec_priv(dev);
3018 free_percpu(macsec->stats);
3019 free_percpu(macsec->secy.tx_sc.stats);
3023 static void macsec_setup(struct net_device *dev)
3025 ether_setup(dev);
3026 dev->min_mtu = 0;
3027 dev->max_mtu = ETH_MAX_MTU;
3028 dev->priv_flags |= IFF_NO_QUEUE;
3029 dev->netdev_ops = &macsec_netdev_ops;
3030 dev->needs_free_netdev = true;
3031 dev->priv_destructor = macsec_free_netdev;
3032 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3034 eth_zero_addr(dev->broadcast);
3037 static int macsec_changelink_common(struct net_device *dev,
3038 struct nlattr *data[])
3040 struct macsec_secy *secy;
3041 struct macsec_tx_sc *tx_sc;
3043 secy = &macsec_priv(dev)->secy;
3044 tx_sc = &secy->tx_sc;
3046 if (data[IFLA_MACSEC_ENCODING_SA]) {
3047 struct macsec_tx_sa *tx_sa;
3049 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3050 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3052 secy->operational = tx_sa && tx_sa->active;
3055 if (data[IFLA_MACSEC_WINDOW])
3056 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3058 if (data[IFLA_MACSEC_ENCRYPT])
3059 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3061 if (data[IFLA_MACSEC_PROTECT])
3062 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3064 if (data[IFLA_MACSEC_INC_SCI])
3065 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3067 if (data[IFLA_MACSEC_ES])
3068 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3070 if (data[IFLA_MACSEC_SCB])
3071 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3073 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3074 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3076 if (data[IFLA_MACSEC_VALIDATION])
3077 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3079 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3080 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3081 case MACSEC_CIPHER_ID_GCM_AES_128:
3082 case MACSEC_DEFAULT_CIPHER_ID:
3083 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3084 break;
3085 case MACSEC_CIPHER_ID_GCM_AES_256:
3086 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3087 break;
3088 default:
3089 return -EINVAL;
3093 return 0;
3096 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3097 struct nlattr *data[],
3098 struct netlink_ext_ack *extack)
3100 if (!data)
3101 return 0;
3103 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3104 data[IFLA_MACSEC_ICV_LEN] ||
3105 data[IFLA_MACSEC_SCI] ||
3106 data[IFLA_MACSEC_PORT])
3107 return -EINVAL;
3109 return macsec_changelink_common(dev, data);
3112 static void macsec_del_dev(struct macsec_dev *macsec)
3114 int i;
3116 while (macsec->secy.rx_sc) {
3117 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3119 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3120 free_rx_sc(rx_sc);
3123 for (i = 0; i < MACSEC_NUM_AN; i++) {
3124 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3126 if (sa) {
3127 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3128 clear_tx_sa(sa);
3133 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3135 struct macsec_dev *macsec = macsec_priv(dev);
3136 struct net_device *real_dev = macsec->real_dev;
3138 unregister_netdevice_queue(dev, head);
3139 list_del_rcu(&macsec->secys);
3140 macsec_del_dev(macsec);
3141 netdev_upper_dev_unlink(real_dev, dev);
3143 macsec_generation++;
3146 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3148 struct macsec_dev *macsec = macsec_priv(dev);
3149 struct net_device *real_dev = macsec->real_dev;
3150 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3152 macsec_common_dellink(dev, head);
3154 if (list_empty(&rxd->secys)) {
3155 netdev_rx_handler_unregister(real_dev);
3156 kfree(rxd);
3160 static int register_macsec_dev(struct net_device *real_dev,
3161 struct net_device *dev)
3163 struct macsec_dev *macsec = macsec_priv(dev);
3164 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3166 if (!rxd) {
3167 int err;
3169 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3170 if (!rxd)
3171 return -ENOMEM;
3173 INIT_LIST_HEAD(&rxd->secys);
3175 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3176 rxd);
3177 if (err < 0) {
3178 kfree(rxd);
3179 return err;
3183 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3184 return 0;
3187 static bool sci_exists(struct net_device *dev, sci_t sci)
3189 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3190 struct macsec_dev *macsec;
3192 list_for_each_entry(macsec, &rxd->secys, secys) {
3193 if (macsec->secy.sci == sci)
3194 return true;
3197 return false;
3200 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3202 struct macsec_dev *macsec = macsec_priv(dev);
3203 struct macsec_secy *secy = &macsec->secy;
3205 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3206 if (!macsec->stats)
3207 return -ENOMEM;
3209 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3210 if (!secy->tx_sc.stats) {
3211 free_percpu(macsec->stats);
3212 return -ENOMEM;
3215 if (sci == MACSEC_UNDEF_SCI)
3216 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3218 secy->netdev = dev;
3219 secy->operational = true;
3220 secy->key_len = DEFAULT_SAK_LEN;
3221 secy->icv_len = icv_len;
3222 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3223 secy->protect_frames = true;
3224 secy->replay_protect = false;
3226 secy->sci = sci;
3227 secy->tx_sc.active = true;
3228 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3229 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3230 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3231 secy->tx_sc.end_station = false;
3232 secy->tx_sc.scb = false;
3234 return 0;
3237 static int macsec_newlink(struct net *net, struct net_device *dev,
3238 struct nlattr *tb[], struct nlattr *data[],
3239 struct netlink_ext_ack *extack)
3241 struct macsec_dev *macsec = macsec_priv(dev);
3242 rx_handler_func_t *rx_handler;
3243 u8 icv_len = DEFAULT_ICV_LEN;
3244 struct net_device *real_dev;
3245 int err, mtu;
3246 sci_t sci;
3248 if (!tb[IFLA_LINK])
3249 return -EINVAL;
3250 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3251 if (!real_dev)
3252 return -ENODEV;
3253 if (real_dev->type != ARPHRD_ETHER)
3254 return -EINVAL;
3256 dev->priv_flags |= IFF_MACSEC;
3258 macsec->real_dev = real_dev;
3260 if (data && data[IFLA_MACSEC_ICV_LEN])
3261 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3262 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3263 if (mtu < 0)
3264 dev->mtu = 0;
3265 else
3266 dev->mtu = mtu;
3268 rx_handler = rtnl_dereference(real_dev->rx_handler);
3269 if (rx_handler && rx_handler != macsec_handle_frame)
3270 return -EBUSY;
3272 err = register_netdevice(dev);
3273 if (err < 0)
3274 return err;
3276 macsec->nest_level = dev_get_nest_level(real_dev) + 1;
3277 netdev_lockdep_set_classes(dev);
3278 lockdep_set_class_and_subclass(&dev->addr_list_lock,
3279 &macsec_netdev_addr_lock_key,
3280 macsec_get_nest_level(dev));
3282 err = netdev_upper_dev_link(real_dev, dev, extack);
3283 if (err < 0)
3284 goto unregister;
3286 /* need to be already registered so that ->init has run and
3287 * the MAC addr is set
3289 if (data && data[IFLA_MACSEC_SCI])
3290 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3291 else if (data && data[IFLA_MACSEC_PORT])
3292 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3293 else
3294 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3296 if (rx_handler && sci_exists(real_dev, sci)) {
3297 err = -EBUSY;
3298 goto unlink;
3301 err = macsec_add_dev(dev, sci, icv_len);
3302 if (err)
3303 goto unlink;
3305 if (data) {
3306 err = macsec_changelink_common(dev, data);
3307 if (err)
3308 goto del_dev;
3311 err = register_macsec_dev(real_dev, dev);
3312 if (err < 0)
3313 goto del_dev;
3315 netif_stacked_transfer_operstate(real_dev, dev);
3316 linkwatch_fire_event(dev);
3318 macsec_generation++;
3320 return 0;
3322 del_dev:
3323 macsec_del_dev(macsec);
3324 unlink:
3325 netdev_upper_dev_unlink(real_dev, dev);
3326 unregister:
3327 unregister_netdevice(dev);
3328 return err;
3331 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3332 struct netlink_ext_ack *extack)
3334 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3335 u8 icv_len = DEFAULT_ICV_LEN;
3336 int flag;
3337 bool es, scb, sci;
3339 if (!data)
3340 return 0;
3342 if (data[IFLA_MACSEC_CIPHER_SUITE])
3343 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3345 if (data[IFLA_MACSEC_ICV_LEN]) {
3346 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3347 if (icv_len != DEFAULT_ICV_LEN) {
3348 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3349 struct crypto_aead *dummy_tfm;
3351 dummy_tfm = macsec_alloc_tfm(dummy_key,
3352 DEFAULT_SAK_LEN,
3353 icv_len);
3354 if (IS_ERR(dummy_tfm))
3355 return PTR_ERR(dummy_tfm);
3356 crypto_free_aead(dummy_tfm);
3360 switch (csid) {
3361 case MACSEC_CIPHER_ID_GCM_AES_128:
3362 case MACSEC_CIPHER_ID_GCM_AES_256:
3363 case MACSEC_DEFAULT_CIPHER_ID:
3364 if (icv_len < MACSEC_MIN_ICV_LEN ||
3365 icv_len > MACSEC_STD_ICV_LEN)
3366 return -EINVAL;
3367 break;
3368 default:
3369 return -EINVAL;
3372 if (data[IFLA_MACSEC_ENCODING_SA]) {
3373 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3374 return -EINVAL;
3377 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3378 flag < IFLA_MACSEC_VALIDATION;
3379 flag++) {
3380 if (data[flag]) {
3381 if (nla_get_u8(data[flag]) > 1)
3382 return -EINVAL;
3386 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3387 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3388 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3390 if ((sci && (scb || es)) || (scb && es))
3391 return -EINVAL;
3393 if (data[IFLA_MACSEC_VALIDATION] &&
3394 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3395 return -EINVAL;
3397 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3398 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3399 !data[IFLA_MACSEC_WINDOW])
3400 return -EINVAL;
3402 return 0;
3405 static struct net *macsec_get_link_net(const struct net_device *dev)
3407 return dev_net(macsec_priv(dev)->real_dev);
3410 static size_t macsec_get_size(const struct net_device *dev)
3412 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
3413 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
3414 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
3415 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
3416 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
3417 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
3418 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
3419 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
3420 nla_total_size(1) + /* IFLA_MACSEC_ES */
3421 nla_total_size(1) + /* IFLA_MACSEC_SCB */
3422 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
3423 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
3427 static int macsec_fill_info(struct sk_buff *skb,
3428 const struct net_device *dev)
3430 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3431 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3432 u64 csid;
3434 switch (secy->key_len) {
3435 case MACSEC_GCM_AES_128_SAK_LEN:
3436 csid = MACSEC_DEFAULT_CIPHER_ID;
3437 break;
3438 case MACSEC_GCM_AES_256_SAK_LEN:
3439 csid = MACSEC_CIPHER_ID_GCM_AES_256;
3440 break;
3441 default:
3442 goto nla_put_failure;
3445 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3446 IFLA_MACSEC_PAD) ||
3447 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3448 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3449 csid, IFLA_MACSEC_PAD) ||
3450 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3451 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3452 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3453 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3454 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3455 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3456 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3457 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3459 goto nla_put_failure;
3461 if (secy->replay_protect) {
3462 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3463 goto nla_put_failure;
3466 return 0;
3468 nla_put_failure:
3469 return -EMSGSIZE;
3472 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3473 .kind = "macsec",
3474 .priv_size = sizeof(struct macsec_dev),
3475 .maxtype = IFLA_MACSEC_MAX,
3476 .policy = macsec_rtnl_policy,
3477 .setup = macsec_setup,
3478 .validate = macsec_validate_attr,
3479 .newlink = macsec_newlink,
3480 .changelink = macsec_changelink,
3481 .dellink = macsec_dellink,
3482 .get_size = macsec_get_size,
3483 .fill_info = macsec_fill_info,
3484 .get_link_net = macsec_get_link_net,
3487 static bool is_macsec_master(struct net_device *dev)
3489 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3492 static int macsec_notify(struct notifier_block *this, unsigned long event,
3493 void *ptr)
3495 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3496 LIST_HEAD(head);
3498 if (!is_macsec_master(real_dev))
3499 return NOTIFY_DONE;
3501 switch (event) {
3502 case NETDEV_DOWN:
3503 case NETDEV_UP:
3504 case NETDEV_CHANGE: {
3505 struct macsec_dev *m, *n;
3506 struct macsec_rxh_data *rxd;
3508 rxd = macsec_data_rtnl(real_dev);
3509 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3510 struct net_device *dev = m->secy.netdev;
3512 netif_stacked_transfer_operstate(real_dev, dev);
3514 break;
3516 case NETDEV_UNREGISTER: {
3517 struct macsec_dev *m, *n;
3518 struct macsec_rxh_data *rxd;
3520 rxd = macsec_data_rtnl(real_dev);
3521 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3522 macsec_common_dellink(m->secy.netdev, &head);
3525 netdev_rx_handler_unregister(real_dev);
3526 kfree(rxd);
3528 unregister_netdevice_many(&head);
3529 break;
3531 case NETDEV_CHANGEMTU: {
3532 struct macsec_dev *m;
3533 struct macsec_rxh_data *rxd;
3535 rxd = macsec_data_rtnl(real_dev);
3536 list_for_each_entry(m, &rxd->secys, secys) {
3537 struct net_device *dev = m->secy.netdev;
3538 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3539 macsec_extra_len(true));
3541 if (dev->mtu > mtu)
3542 dev_set_mtu(dev, mtu);
3547 return NOTIFY_OK;
3550 static struct notifier_block macsec_notifier = {
3551 .notifier_call = macsec_notify,
3554 static int __init macsec_init(void)
3556 int err;
3558 pr_info("MACsec IEEE 802.1AE\n");
3559 err = register_netdevice_notifier(&macsec_notifier);
3560 if (err)
3561 return err;
3563 err = rtnl_link_register(&macsec_link_ops);
3564 if (err)
3565 goto notifier;
3567 err = genl_register_family(&macsec_fam);
3568 if (err)
3569 goto rtnl;
3571 return 0;
3573 rtnl:
3574 rtnl_link_unregister(&macsec_link_ops);
3575 notifier:
3576 unregister_netdevice_notifier(&macsec_notifier);
3577 return err;
3580 static void __exit macsec_exit(void)
3582 genl_unregister_family(&macsec_fam);
3583 rtnl_link_unregister(&macsec_link_ops);
3584 unregister_netdevice_notifier(&macsec_notifier);
3585 rcu_barrier();
3588 module_init(macsec_init);
3589 module_exit(macsec_exit);
3591 MODULE_ALIAS_RTNL_LINK("macsec");
3592 MODULE_ALIAS_GENL_FAMILY("macsec");
3594 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3595 MODULE_LICENSE("GPL v2");