Linux 4.9.243
[linux/fpc-iii.git] / drivers / net / macsec.c
blob774b9db0c811f8063ce247329b563d921ea9af78
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 <net/genetlink.h>
20 #include <net/sock.h>
21 #include <net/gro_cells.h>
22 #include <linux/if_arp.h>
24 #include <uapi/linux/if_macsec.h>
26 typedef u64 __bitwise sci_t;
28 #define MACSEC_SCI_LEN 8
30 /* SecTAG length = macsec_eth_header without the optional SCI */
31 #define MACSEC_TAG_LEN 6
33 struct macsec_eth_header {
34 struct ethhdr eth;
35 /* SecTAG */
36 u8 tci_an;
37 #if defined(__LITTLE_ENDIAN_BITFIELD)
38 u8 short_length:6,
39 unused:2;
40 #elif defined(__BIG_ENDIAN_BITFIELD)
41 u8 unused:2,
42 short_length:6;
43 #else
44 #error "Please fix <asm/byteorder.h>"
45 #endif
46 __be32 packet_number;
47 u8 secure_channel_id[8]; /* optional */
48 } __packed;
50 #define MACSEC_TCI_VERSION 0x80
51 #define MACSEC_TCI_ES 0x40 /* end station */
52 #define MACSEC_TCI_SC 0x20 /* SCI present */
53 #define MACSEC_TCI_SCB 0x10 /* epon */
54 #define MACSEC_TCI_E 0x08 /* encryption */
55 #define MACSEC_TCI_C 0x04 /* changed text */
56 #define MACSEC_AN_MASK 0x03 /* association number */
57 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
59 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
60 #define MIN_NON_SHORT_LEN 48
62 #define GCM_AES_IV_LEN 12
63 #define DEFAULT_ICV_LEN 16
65 #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
67 #define for_each_rxsc(secy, sc) \
68 for (sc = rcu_dereference_bh(secy->rx_sc); \
69 sc; \
70 sc = rcu_dereference_bh(sc->next))
71 #define for_each_rxsc_rtnl(secy, sc) \
72 for (sc = rtnl_dereference(secy->rx_sc); \
73 sc; \
74 sc = rtnl_dereference(sc->next))
76 struct gcm_iv {
77 union {
78 u8 secure_channel_id[8];
79 sci_t sci;
81 __be32 pn;
84 /**
85 * struct macsec_key - SA key
86 * @id: user-provided key identifier
87 * @tfm: crypto struct, key storage
89 struct macsec_key {
90 u8 id[MACSEC_KEYID_LEN];
91 struct crypto_aead *tfm;
94 struct macsec_rx_sc_stats {
95 __u64 InOctetsValidated;
96 __u64 InOctetsDecrypted;
97 __u64 InPktsUnchecked;
98 __u64 InPktsDelayed;
99 __u64 InPktsOK;
100 __u64 InPktsInvalid;
101 __u64 InPktsLate;
102 __u64 InPktsNotValid;
103 __u64 InPktsNotUsingSA;
104 __u64 InPktsUnusedSA;
107 struct macsec_rx_sa_stats {
108 __u32 InPktsOK;
109 __u32 InPktsInvalid;
110 __u32 InPktsNotValid;
111 __u32 InPktsNotUsingSA;
112 __u32 InPktsUnusedSA;
115 struct macsec_tx_sa_stats {
116 __u32 OutPktsProtected;
117 __u32 OutPktsEncrypted;
120 struct macsec_tx_sc_stats {
121 __u64 OutPktsProtected;
122 __u64 OutPktsEncrypted;
123 __u64 OutOctetsProtected;
124 __u64 OutOctetsEncrypted;
127 struct macsec_dev_stats {
128 __u64 OutPktsUntagged;
129 __u64 InPktsUntagged;
130 __u64 OutPktsTooLong;
131 __u64 InPktsNoTag;
132 __u64 InPktsBadTag;
133 __u64 InPktsUnknownSCI;
134 __u64 InPktsNoSCI;
135 __u64 InPktsOverrun;
139 * struct macsec_rx_sa - receive secure association
140 * @active:
141 * @next_pn: packet number expected for the next packet
142 * @lock: protects next_pn manipulations
143 * @key: key structure
144 * @stats: per-SA stats
146 struct macsec_rx_sa {
147 struct macsec_key key;
148 spinlock_t lock;
149 u32 next_pn;
150 atomic_t refcnt;
151 bool active;
152 struct macsec_rx_sa_stats __percpu *stats;
153 struct macsec_rx_sc *sc;
154 struct rcu_head rcu;
157 struct pcpu_rx_sc_stats {
158 struct macsec_rx_sc_stats stats;
159 struct u64_stats_sync syncp;
163 * struct macsec_rx_sc - receive secure channel
164 * @sci: secure channel identifier for this SC
165 * @active: channel is active
166 * @sa: array of secure associations
167 * @stats: per-SC stats
169 struct macsec_rx_sc {
170 struct macsec_rx_sc __rcu *next;
171 sci_t sci;
172 bool active;
173 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
174 struct pcpu_rx_sc_stats __percpu *stats;
175 atomic_t refcnt;
176 struct rcu_head rcu_head;
180 * struct macsec_tx_sa - transmit secure association
181 * @active:
182 * @next_pn: packet number to use for the next packet
183 * @lock: protects next_pn manipulations
184 * @key: key structure
185 * @stats: per-SA stats
187 struct macsec_tx_sa {
188 struct macsec_key key;
189 spinlock_t lock;
190 u32 next_pn;
191 atomic_t refcnt;
192 bool active;
193 struct macsec_tx_sa_stats __percpu *stats;
194 struct rcu_head rcu;
197 struct pcpu_tx_sc_stats {
198 struct macsec_tx_sc_stats stats;
199 struct u64_stats_sync syncp;
203 * struct macsec_tx_sc - transmit secure channel
204 * @active:
205 * @encoding_sa: association number of the SA currently in use
206 * @encrypt: encrypt packets on transmit, or authenticate only
207 * @send_sci: always include the SCI in the SecTAG
208 * @end_station:
209 * @scb: single copy broadcast flag
210 * @sa: array of secure associations
211 * @stats: stats for this TXSC
213 struct macsec_tx_sc {
214 bool active;
215 u8 encoding_sa;
216 bool encrypt;
217 bool send_sci;
218 bool end_station;
219 bool scb;
220 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
221 struct pcpu_tx_sc_stats __percpu *stats;
224 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
227 * struct macsec_secy - MACsec Security Entity
228 * @netdev: netdevice for this SecY
229 * @n_rx_sc: number of receive secure channels configured on this SecY
230 * @sci: secure channel identifier used for tx
231 * @key_len: length of keys used by the cipher suite
232 * @icv_len: length of ICV used by the cipher suite
233 * @validate_frames: validation mode
234 * @operational: MAC_Operational flag
235 * @protect_frames: enable protection for this SecY
236 * @replay_protect: enable packet number checks on receive
237 * @replay_window: size of the replay window
238 * @tx_sc: transmit secure channel
239 * @rx_sc: linked list of receive secure channels
241 struct macsec_secy {
242 struct net_device *netdev;
243 unsigned int n_rx_sc;
244 sci_t sci;
245 u16 key_len;
246 u16 icv_len;
247 enum macsec_validation_type validate_frames;
248 bool operational;
249 bool protect_frames;
250 bool replay_protect;
251 u32 replay_window;
252 struct macsec_tx_sc tx_sc;
253 struct macsec_rx_sc __rcu *rx_sc;
256 struct pcpu_secy_stats {
257 struct macsec_dev_stats stats;
258 struct u64_stats_sync syncp;
262 * struct macsec_dev - private data
263 * @secy: SecY config
264 * @real_dev: pointer to underlying netdevice
265 * @stats: MACsec device stats
266 * @secys: linked list of SecY's on the underlying device
268 struct macsec_dev {
269 struct macsec_secy secy;
270 struct net_device *real_dev;
271 struct pcpu_secy_stats __percpu *stats;
272 struct list_head secys;
273 struct gro_cells gro_cells;
274 unsigned int nest_level;
278 * struct macsec_rxh_data - rx_handler private argument
279 * @secys: linked list of SecY's on this underlying device
281 struct macsec_rxh_data {
282 struct list_head secys;
285 static struct macsec_dev *macsec_priv(const struct net_device *dev)
287 return (struct macsec_dev *)netdev_priv(dev);
290 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
292 return rcu_dereference_bh(dev->rx_handler_data);
295 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
297 return rtnl_dereference(dev->rx_handler_data);
300 struct macsec_cb {
301 struct aead_request *req;
302 union {
303 struct macsec_tx_sa *tx_sa;
304 struct macsec_rx_sa *rx_sa;
306 u8 assoc_num;
307 bool valid;
308 bool has_sci;
311 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
313 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
315 if (!sa || !sa->active)
316 return NULL;
318 if (!atomic_inc_not_zero(&sa->refcnt))
319 return NULL;
321 return sa;
324 static void free_rx_sc_rcu(struct rcu_head *head)
326 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
328 free_percpu(rx_sc->stats);
329 kfree(rx_sc);
332 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
334 return atomic_inc_not_zero(&sc->refcnt) ? sc : NULL;
337 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
339 if (atomic_dec_and_test(&sc->refcnt))
340 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
343 static void free_rxsa(struct rcu_head *head)
345 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
347 crypto_free_aead(sa->key.tfm);
348 free_percpu(sa->stats);
349 kfree(sa);
352 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
354 if (atomic_dec_and_test(&sa->refcnt))
355 call_rcu(&sa->rcu, free_rxsa);
358 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
360 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
362 if (!sa || !sa->active)
363 return NULL;
365 if (!atomic_inc_not_zero(&sa->refcnt))
366 return NULL;
368 return sa;
371 static void free_txsa(struct rcu_head *head)
373 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
375 crypto_free_aead(sa->key.tfm);
376 free_percpu(sa->stats);
377 kfree(sa);
380 static void macsec_txsa_put(struct macsec_tx_sa *sa)
382 if (atomic_dec_and_test(&sa->refcnt))
383 call_rcu(&sa->rcu, free_txsa);
386 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
388 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
389 return (struct macsec_cb *)skb->cb;
392 #define MACSEC_PORT_ES (htons(0x0001))
393 #define MACSEC_PORT_SCB (0x0000)
394 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
396 #define DEFAULT_SAK_LEN 16
397 #define DEFAULT_SEND_SCI true
398 #define DEFAULT_ENCRYPT false
399 #define DEFAULT_ENCODING_SA 0
401 static bool send_sci(const struct macsec_secy *secy)
403 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
405 return tx_sc->send_sci ||
406 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
409 static sci_t make_sci(u8 *addr, __be16 port)
411 sci_t sci;
413 memcpy(&sci, addr, ETH_ALEN);
414 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
416 return sci;
419 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
421 sci_t sci;
423 if (sci_present)
424 memcpy(&sci, hdr->secure_channel_id,
425 sizeof(hdr->secure_channel_id));
426 else
427 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
429 return sci;
432 static unsigned int macsec_sectag_len(bool sci_present)
434 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
437 static unsigned int macsec_hdr_len(bool sci_present)
439 return macsec_sectag_len(sci_present) + ETH_HLEN;
442 static unsigned int macsec_extra_len(bool sci_present)
444 return macsec_sectag_len(sci_present) + sizeof(__be16);
447 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
448 static void macsec_fill_sectag(struct macsec_eth_header *h,
449 const struct macsec_secy *secy, u32 pn,
450 bool sci_present)
452 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
454 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
455 h->eth.h_proto = htons(ETH_P_MACSEC);
457 if (sci_present) {
458 h->tci_an |= MACSEC_TCI_SC;
459 memcpy(&h->secure_channel_id, &secy->sci,
460 sizeof(h->secure_channel_id));
461 } else {
462 if (tx_sc->end_station)
463 h->tci_an |= MACSEC_TCI_ES;
464 if (tx_sc->scb)
465 h->tci_an |= MACSEC_TCI_SCB;
468 h->packet_number = htonl(pn);
470 /* with GCM, C/E clear for !encrypt, both set for encrypt */
471 if (tx_sc->encrypt)
472 h->tci_an |= MACSEC_TCI_CONFID;
473 else if (secy->icv_len != DEFAULT_ICV_LEN)
474 h->tci_an |= MACSEC_TCI_C;
476 h->tci_an |= tx_sc->encoding_sa;
479 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
481 if (data_len < MIN_NON_SHORT_LEN)
482 h->short_length = data_len;
485 /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
486 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
488 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
489 int len = skb->len - 2 * ETH_ALEN;
490 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
492 /* a) It comprises at least 17 octets */
493 if (skb->len <= 16)
494 return false;
496 /* b) MACsec EtherType: already checked */
498 /* c) V bit is clear */
499 if (h->tci_an & MACSEC_TCI_VERSION)
500 return false;
502 /* d) ES or SCB => !SC */
503 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
504 (h->tci_an & MACSEC_TCI_SC))
505 return false;
507 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
508 if (h->unused)
509 return false;
511 /* rx.pn != 0 (figure 10-5) */
512 if (!h->packet_number)
513 return false;
515 /* length check, f) g) h) i) */
516 if (h->short_length)
517 return len == extra_len + h->short_length;
518 return len >= extra_len + MIN_NON_SHORT_LEN;
521 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
522 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
524 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
526 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
528 gcm_iv->sci = sci;
529 gcm_iv->pn = htonl(pn);
532 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
534 return (struct macsec_eth_header *)skb_mac_header(skb);
537 static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
539 u32 pn;
541 spin_lock_bh(&tx_sa->lock);
542 pn = tx_sa->next_pn;
544 tx_sa->next_pn++;
545 if (tx_sa->next_pn == 0) {
546 pr_debug("PN wrapped, transitioning to !oper\n");
547 tx_sa->active = false;
548 if (secy->protect_frames)
549 secy->operational = false;
551 spin_unlock_bh(&tx_sa->lock);
553 return pn;
556 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
558 struct macsec_dev *macsec = netdev_priv(dev);
560 skb->dev = macsec->real_dev;
561 skb_reset_mac_header(skb);
562 skb->protocol = eth_hdr(skb)->h_proto;
565 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
566 struct macsec_tx_sa *tx_sa)
568 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
570 u64_stats_update_begin(&txsc_stats->syncp);
571 if (tx_sc->encrypt) {
572 txsc_stats->stats.OutOctetsEncrypted += skb->len;
573 txsc_stats->stats.OutPktsEncrypted++;
574 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
575 } else {
576 txsc_stats->stats.OutOctetsProtected += skb->len;
577 txsc_stats->stats.OutPktsProtected++;
578 this_cpu_inc(tx_sa->stats->OutPktsProtected);
580 u64_stats_update_end(&txsc_stats->syncp);
583 static void count_tx(struct net_device *dev, int ret, int len)
585 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
586 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
588 u64_stats_update_begin(&stats->syncp);
589 stats->tx_packets++;
590 stats->tx_bytes += len;
591 u64_stats_update_end(&stats->syncp);
592 } else {
593 dev->stats.tx_dropped++;
597 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
599 struct sk_buff *skb = base->data;
600 struct net_device *dev = skb->dev;
601 struct macsec_dev *macsec = macsec_priv(dev);
602 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
603 int len, ret;
605 aead_request_free(macsec_skb_cb(skb)->req);
607 rcu_read_lock_bh();
608 macsec_encrypt_finish(skb, dev);
609 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
610 len = skb->len;
611 ret = dev_queue_xmit(skb);
612 count_tx(dev, ret, len);
613 rcu_read_unlock_bh();
615 macsec_txsa_put(sa);
616 dev_put(dev);
619 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
620 unsigned char **iv,
621 struct scatterlist **sg,
622 int num_frags)
624 size_t size, iv_offset, sg_offset;
625 struct aead_request *req;
626 void *tmp;
628 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
629 iv_offset = size;
630 size += GCM_AES_IV_LEN;
632 size = ALIGN(size, __alignof__(struct scatterlist));
633 sg_offset = size;
634 size += sizeof(struct scatterlist) * num_frags;
636 tmp = kmalloc(size, GFP_ATOMIC);
637 if (!tmp)
638 return NULL;
640 *iv = (unsigned char *)(tmp + iv_offset);
641 *sg = (struct scatterlist *)(tmp + sg_offset);
642 req = tmp;
644 aead_request_set_tfm(req, tfm);
646 return req;
649 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
650 struct net_device *dev)
652 int ret;
653 struct scatterlist *sg;
654 struct sk_buff *trailer;
655 unsigned char *iv;
656 struct ethhdr *eth;
657 struct macsec_eth_header *hh;
658 size_t unprotected_len;
659 struct aead_request *req;
660 struct macsec_secy *secy;
661 struct macsec_tx_sc *tx_sc;
662 struct macsec_tx_sa *tx_sa;
663 struct macsec_dev *macsec = macsec_priv(dev);
664 bool sci_present;
665 u32 pn;
667 secy = &macsec->secy;
668 tx_sc = &secy->tx_sc;
670 /* 10.5.1 TX SA assignment */
671 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
672 if (!tx_sa) {
673 secy->operational = false;
674 kfree_skb(skb);
675 return ERR_PTR(-EINVAL);
678 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
679 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
680 struct sk_buff *nskb = skb_copy_expand(skb,
681 MACSEC_NEEDED_HEADROOM,
682 MACSEC_NEEDED_TAILROOM,
683 GFP_ATOMIC);
684 if (likely(nskb)) {
685 consume_skb(skb);
686 skb = nskb;
687 } else {
688 macsec_txsa_put(tx_sa);
689 kfree_skb(skb);
690 return ERR_PTR(-ENOMEM);
692 } else {
693 skb = skb_unshare(skb, GFP_ATOMIC);
694 if (!skb) {
695 macsec_txsa_put(tx_sa);
696 return ERR_PTR(-ENOMEM);
700 unprotected_len = skb->len;
701 eth = eth_hdr(skb);
702 sci_present = send_sci(secy);
703 hh = (struct macsec_eth_header *)skb_push(skb, macsec_extra_len(sci_present));
704 memmove(hh, eth, 2 * ETH_ALEN);
706 pn = tx_sa_update_pn(tx_sa, secy);
707 if (pn == 0) {
708 macsec_txsa_put(tx_sa);
709 kfree_skb(skb);
710 return ERR_PTR(-ENOLINK);
712 macsec_fill_sectag(hh, secy, pn, sci_present);
713 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
715 skb_put(skb, secy->icv_len);
717 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
718 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
720 u64_stats_update_begin(&secy_stats->syncp);
721 secy_stats->stats.OutPktsTooLong++;
722 u64_stats_update_end(&secy_stats->syncp);
724 macsec_txsa_put(tx_sa);
725 kfree_skb(skb);
726 return ERR_PTR(-EINVAL);
729 ret = skb_cow_data(skb, 0, &trailer);
730 if (unlikely(ret < 0)) {
731 macsec_txsa_put(tx_sa);
732 kfree_skb(skb);
733 return ERR_PTR(ret);
736 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
737 if (!req) {
738 macsec_txsa_put(tx_sa);
739 kfree_skb(skb);
740 return ERR_PTR(-ENOMEM);
743 macsec_fill_iv(iv, secy->sci, pn);
745 sg_init_table(sg, ret);
746 ret = skb_to_sgvec(skb, sg, 0, skb->len);
747 if (unlikely(ret < 0)) {
748 aead_request_free(req);
749 macsec_txsa_put(tx_sa);
750 kfree_skb(skb);
751 return ERR_PTR(ret);
754 if (tx_sc->encrypt) {
755 int len = skb->len - macsec_hdr_len(sci_present) -
756 secy->icv_len;
757 aead_request_set_crypt(req, sg, sg, len, iv);
758 aead_request_set_ad(req, macsec_hdr_len(sci_present));
759 } else {
760 aead_request_set_crypt(req, sg, sg, 0, iv);
761 aead_request_set_ad(req, skb->len - secy->icv_len);
764 macsec_skb_cb(skb)->req = req;
765 macsec_skb_cb(skb)->tx_sa = tx_sa;
766 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
768 dev_hold(skb->dev);
769 ret = crypto_aead_encrypt(req);
770 if (ret == -EINPROGRESS) {
771 return ERR_PTR(ret);
772 } else if (ret != 0) {
773 dev_put(skb->dev);
774 kfree_skb(skb);
775 aead_request_free(req);
776 macsec_txsa_put(tx_sa);
777 return ERR_PTR(-EINVAL);
780 dev_put(skb->dev);
781 aead_request_free(req);
782 macsec_txsa_put(tx_sa);
784 return skb;
787 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
789 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
790 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
791 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
792 u32 lowest_pn = 0;
794 spin_lock(&rx_sa->lock);
795 if (rx_sa->next_pn >= secy->replay_window)
796 lowest_pn = rx_sa->next_pn - secy->replay_window;
798 /* Now perform replay protection check again
799 * (see IEEE 802.1AE-2006 figure 10-5)
801 if (secy->replay_protect && pn < lowest_pn) {
802 spin_unlock(&rx_sa->lock);
803 u64_stats_update_begin(&rxsc_stats->syncp);
804 rxsc_stats->stats.InPktsLate++;
805 u64_stats_update_end(&rxsc_stats->syncp);
806 return false;
809 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
810 u64_stats_update_begin(&rxsc_stats->syncp);
811 if (hdr->tci_an & MACSEC_TCI_E)
812 rxsc_stats->stats.InOctetsDecrypted += skb->len;
813 else
814 rxsc_stats->stats.InOctetsValidated += skb->len;
815 u64_stats_update_end(&rxsc_stats->syncp);
818 if (!macsec_skb_cb(skb)->valid) {
819 spin_unlock(&rx_sa->lock);
821 /* 10.6.5 */
822 if (hdr->tci_an & MACSEC_TCI_C ||
823 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
824 u64_stats_update_begin(&rxsc_stats->syncp);
825 rxsc_stats->stats.InPktsNotValid++;
826 u64_stats_update_end(&rxsc_stats->syncp);
827 return false;
830 u64_stats_update_begin(&rxsc_stats->syncp);
831 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
832 rxsc_stats->stats.InPktsInvalid++;
833 this_cpu_inc(rx_sa->stats->InPktsInvalid);
834 } else if (pn < lowest_pn) {
835 rxsc_stats->stats.InPktsDelayed++;
836 } else {
837 rxsc_stats->stats.InPktsUnchecked++;
839 u64_stats_update_end(&rxsc_stats->syncp);
840 } else {
841 u64_stats_update_begin(&rxsc_stats->syncp);
842 if (pn < lowest_pn) {
843 rxsc_stats->stats.InPktsDelayed++;
844 } else {
845 rxsc_stats->stats.InPktsOK++;
846 this_cpu_inc(rx_sa->stats->InPktsOK);
848 u64_stats_update_end(&rxsc_stats->syncp);
850 if (pn >= rx_sa->next_pn)
851 rx_sa->next_pn = pn + 1;
852 spin_unlock(&rx_sa->lock);
855 return true;
858 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
860 skb->pkt_type = PACKET_HOST;
861 skb->protocol = eth_type_trans(skb, dev);
863 skb_reset_network_header(skb);
864 if (!skb_transport_header_was_set(skb))
865 skb_reset_transport_header(skb);
866 skb_reset_mac_len(skb);
869 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
871 skb->ip_summed = CHECKSUM_NONE;
872 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
873 skb_pull(skb, hdr_len);
874 pskb_trim_unique(skb, skb->len - icv_len);
877 static void count_rx(struct net_device *dev, int len)
879 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
881 u64_stats_update_begin(&stats->syncp);
882 stats->rx_packets++;
883 stats->rx_bytes += len;
884 u64_stats_update_end(&stats->syncp);
887 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
889 struct sk_buff *skb = base->data;
890 struct net_device *dev = skb->dev;
891 struct macsec_dev *macsec = macsec_priv(dev);
892 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
893 struct macsec_rx_sc *rx_sc = rx_sa->sc;
894 int len, ret;
895 u32 pn;
897 aead_request_free(macsec_skb_cb(skb)->req);
899 rcu_read_lock_bh();
900 pn = ntohl(macsec_ethhdr(skb)->packet_number);
901 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
902 rcu_read_unlock_bh();
903 kfree_skb(skb);
904 goto out;
907 macsec_finalize_skb(skb, macsec->secy.icv_len,
908 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
909 macsec_reset_skb(skb, macsec->secy.netdev);
911 len = skb->len;
912 ret = gro_cells_receive(&macsec->gro_cells, skb);
913 if (ret == NET_RX_SUCCESS)
914 count_rx(dev, len);
915 else
916 macsec->secy.netdev->stats.rx_dropped++;
918 rcu_read_unlock_bh();
920 out:
921 macsec_rxsa_put(rx_sa);
922 macsec_rxsc_put(rx_sc);
923 dev_put(dev);
926 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
927 struct net_device *dev,
928 struct macsec_rx_sa *rx_sa,
929 sci_t sci,
930 struct macsec_secy *secy)
932 int ret;
933 struct scatterlist *sg;
934 struct sk_buff *trailer;
935 unsigned char *iv;
936 struct aead_request *req;
937 struct macsec_eth_header *hdr;
938 u16 icv_len = secy->icv_len;
940 macsec_skb_cb(skb)->valid = false;
941 skb = skb_share_check(skb, GFP_ATOMIC);
942 if (!skb)
943 return ERR_PTR(-ENOMEM);
945 ret = skb_cow_data(skb, 0, &trailer);
946 if (unlikely(ret < 0)) {
947 kfree_skb(skb);
948 return ERR_PTR(ret);
950 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
951 if (!req) {
952 kfree_skb(skb);
953 return ERR_PTR(-ENOMEM);
956 hdr = (struct macsec_eth_header *)skb->data;
957 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
959 sg_init_table(sg, ret);
960 ret = skb_to_sgvec(skb, sg, 0, skb->len);
961 if (unlikely(ret < 0)) {
962 aead_request_free(req);
963 kfree_skb(skb);
964 return ERR_PTR(ret);
967 if (hdr->tci_an & MACSEC_TCI_E) {
968 /* confidentiality: ethernet + macsec header
969 * authenticated, encrypted payload
971 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
973 aead_request_set_crypt(req, sg, sg, len, iv);
974 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
975 skb = skb_unshare(skb, GFP_ATOMIC);
976 if (!skb) {
977 aead_request_free(req);
978 return ERR_PTR(-ENOMEM);
980 } else {
981 /* integrity only: all headers + data authenticated */
982 aead_request_set_crypt(req, sg, sg, icv_len, iv);
983 aead_request_set_ad(req, skb->len - icv_len);
986 macsec_skb_cb(skb)->req = req;
987 skb->dev = dev;
988 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
990 dev_hold(dev);
991 ret = crypto_aead_decrypt(req);
992 if (ret == -EINPROGRESS) {
993 return ERR_PTR(ret);
994 } else if (ret != 0) {
995 /* decryption/authentication failed
996 * 10.6 if validateFrames is disabled, deliver anyway
998 if (ret != -EBADMSG) {
999 kfree_skb(skb);
1000 skb = ERR_PTR(ret);
1002 } else {
1003 macsec_skb_cb(skb)->valid = true;
1005 dev_put(dev);
1007 aead_request_free(req);
1009 return skb;
1012 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
1014 struct macsec_rx_sc *rx_sc;
1016 for_each_rxsc(secy, rx_sc) {
1017 if (rx_sc->sci == sci)
1018 return rx_sc;
1021 return NULL;
1024 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1026 struct macsec_rx_sc *rx_sc;
1028 for_each_rxsc_rtnl(secy, rx_sc) {
1029 if (rx_sc->sci == sci)
1030 return rx_sc;
1033 return NULL;
1036 static void handle_not_macsec(struct sk_buff *skb)
1038 struct macsec_rxh_data *rxd;
1039 struct macsec_dev *macsec;
1041 rcu_read_lock();
1042 rxd = macsec_data_rcu(skb->dev);
1044 /* 10.6 If the management control validateFrames is not
1045 * Strict, frames without a SecTAG are received, counted, and
1046 * delivered to the Controlled Port
1048 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1049 struct sk_buff *nskb;
1050 int ret;
1051 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1053 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1054 u64_stats_update_begin(&secy_stats->syncp);
1055 secy_stats->stats.InPktsNoTag++;
1056 u64_stats_update_end(&secy_stats->syncp);
1057 continue;
1060 /* deliver on this port */
1061 nskb = skb_clone(skb, GFP_ATOMIC);
1062 if (!nskb)
1063 break;
1065 nskb->dev = macsec->secy.netdev;
1067 ret = netif_rx(nskb);
1068 if (ret == NET_RX_SUCCESS) {
1069 u64_stats_update_begin(&secy_stats->syncp);
1070 secy_stats->stats.InPktsUntagged++;
1071 u64_stats_update_end(&secy_stats->syncp);
1072 } else {
1073 macsec->secy.netdev->stats.rx_dropped++;
1077 rcu_read_unlock();
1080 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1082 struct sk_buff *skb = *pskb;
1083 struct net_device *dev = skb->dev;
1084 struct macsec_eth_header *hdr;
1085 struct macsec_secy *secy = NULL;
1086 struct macsec_rx_sc *rx_sc;
1087 struct macsec_rx_sa *rx_sa;
1088 struct macsec_rxh_data *rxd;
1089 struct macsec_dev *macsec;
1090 unsigned int len;
1091 sci_t sci;
1092 u32 pn;
1093 bool cbit;
1094 struct pcpu_rx_sc_stats *rxsc_stats;
1095 struct pcpu_secy_stats *secy_stats;
1096 bool pulled_sci;
1097 int ret;
1099 if (skb_headroom(skb) < ETH_HLEN)
1100 goto drop_direct;
1102 hdr = macsec_ethhdr(skb);
1103 if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1104 handle_not_macsec(skb);
1106 /* and deliver to the uncontrolled port */
1107 return RX_HANDLER_PASS;
1110 skb = skb_unshare(skb, GFP_ATOMIC);
1111 *pskb = skb;
1112 if (!skb)
1113 return RX_HANDLER_CONSUMED;
1115 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1116 if (!pulled_sci) {
1117 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1118 goto drop_direct;
1121 hdr = macsec_ethhdr(skb);
1123 /* Frames with a SecTAG that has the TCI E bit set but the C
1124 * bit clear are discarded, as this reserved encoding is used
1125 * to identify frames with a SecTAG that are not to be
1126 * delivered to the Controlled Port.
1128 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1129 return RX_HANDLER_PASS;
1131 /* now, pull the extra length */
1132 if (hdr->tci_an & MACSEC_TCI_SC) {
1133 if (!pulled_sci)
1134 goto drop_direct;
1137 /* ethernet header is part of crypto processing */
1138 skb_push(skb, ETH_HLEN);
1140 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1141 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1142 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1144 rcu_read_lock();
1145 rxd = macsec_data_rcu(skb->dev);
1147 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1148 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1149 sc = sc ? macsec_rxsc_get(sc) : NULL;
1151 if (sc) {
1152 secy = &macsec->secy;
1153 rx_sc = sc;
1154 break;
1158 if (!secy)
1159 goto nosci;
1161 dev = secy->netdev;
1162 macsec = macsec_priv(dev);
1163 secy_stats = this_cpu_ptr(macsec->stats);
1164 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1166 if (!macsec_validate_skb(skb, secy->icv_len)) {
1167 u64_stats_update_begin(&secy_stats->syncp);
1168 secy_stats->stats.InPktsBadTag++;
1169 u64_stats_update_end(&secy_stats->syncp);
1170 goto drop_nosa;
1173 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1174 if (!rx_sa) {
1175 /* 10.6.1 if the SA is not in use */
1177 /* If validateFrames is Strict or the C bit in the
1178 * SecTAG is set, discard
1180 if (hdr->tci_an & MACSEC_TCI_C ||
1181 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1182 u64_stats_update_begin(&rxsc_stats->syncp);
1183 rxsc_stats->stats.InPktsNotUsingSA++;
1184 u64_stats_update_end(&rxsc_stats->syncp);
1185 goto drop_nosa;
1188 /* not Strict, the frame (with the SecTAG and ICV
1189 * removed) is delivered to the Controlled Port.
1191 u64_stats_update_begin(&rxsc_stats->syncp);
1192 rxsc_stats->stats.InPktsUnusedSA++;
1193 u64_stats_update_end(&rxsc_stats->syncp);
1194 goto deliver;
1197 /* First, PN check to avoid decrypting obviously wrong packets */
1198 pn = ntohl(hdr->packet_number);
1199 if (secy->replay_protect) {
1200 bool late;
1202 spin_lock(&rx_sa->lock);
1203 late = rx_sa->next_pn >= secy->replay_window &&
1204 pn < (rx_sa->next_pn - secy->replay_window);
1205 spin_unlock(&rx_sa->lock);
1207 if (late) {
1208 u64_stats_update_begin(&rxsc_stats->syncp);
1209 rxsc_stats->stats.InPktsLate++;
1210 u64_stats_update_end(&rxsc_stats->syncp);
1211 goto drop;
1215 macsec_skb_cb(skb)->rx_sa = rx_sa;
1217 /* Disabled && !changed text => skip validation */
1218 if (hdr->tci_an & MACSEC_TCI_C ||
1219 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1220 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1222 if (IS_ERR(skb)) {
1223 /* the decrypt callback needs the reference */
1224 if (PTR_ERR(skb) != -EINPROGRESS) {
1225 macsec_rxsa_put(rx_sa);
1226 macsec_rxsc_put(rx_sc);
1228 rcu_read_unlock();
1229 *pskb = NULL;
1230 return RX_HANDLER_CONSUMED;
1233 if (!macsec_post_decrypt(skb, secy, pn))
1234 goto drop;
1236 deliver:
1237 macsec_finalize_skb(skb, secy->icv_len,
1238 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1239 macsec_reset_skb(skb, secy->netdev);
1241 if (rx_sa)
1242 macsec_rxsa_put(rx_sa);
1243 macsec_rxsc_put(rx_sc);
1245 skb_orphan(skb);
1246 len = skb->len;
1247 ret = gro_cells_receive(&macsec->gro_cells, skb);
1248 if (ret == NET_RX_SUCCESS)
1249 count_rx(dev, len);
1250 else
1251 macsec->secy.netdev->stats.rx_dropped++;
1253 rcu_read_unlock();
1255 *pskb = NULL;
1256 return RX_HANDLER_CONSUMED;
1258 drop:
1259 macsec_rxsa_put(rx_sa);
1260 drop_nosa:
1261 macsec_rxsc_put(rx_sc);
1262 rcu_read_unlock();
1263 drop_direct:
1264 kfree_skb(skb);
1265 *pskb = NULL;
1266 return RX_HANDLER_CONSUMED;
1268 nosci:
1269 /* 10.6.1 if the SC is not found */
1270 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1271 if (!cbit)
1272 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1273 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1275 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1276 struct sk_buff *nskb;
1278 secy_stats = this_cpu_ptr(macsec->stats);
1280 /* If validateFrames is Strict or the C bit in the
1281 * SecTAG is set, discard
1283 if (cbit ||
1284 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1285 u64_stats_update_begin(&secy_stats->syncp);
1286 secy_stats->stats.InPktsNoSCI++;
1287 u64_stats_update_end(&secy_stats->syncp);
1288 continue;
1291 /* not strict, the frame (with the SecTAG and ICV
1292 * removed) is delivered to the Controlled Port.
1294 nskb = skb_clone(skb, GFP_ATOMIC);
1295 if (!nskb)
1296 break;
1298 macsec_reset_skb(nskb, macsec->secy.netdev);
1300 ret = netif_rx(nskb);
1301 if (ret == NET_RX_SUCCESS) {
1302 u64_stats_update_begin(&secy_stats->syncp);
1303 secy_stats->stats.InPktsUnknownSCI++;
1304 u64_stats_update_end(&secy_stats->syncp);
1305 } else {
1306 macsec->secy.netdev->stats.rx_dropped++;
1310 rcu_read_unlock();
1311 *pskb = skb;
1312 return RX_HANDLER_PASS;
1315 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1317 struct crypto_aead *tfm;
1318 int ret;
1320 /* Pick a sync gcm(aes) cipher to ensure order is preserved. */
1321 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
1323 if (IS_ERR(tfm))
1324 return tfm;
1326 ret = crypto_aead_setkey(tfm, key, key_len);
1327 if (ret < 0)
1328 goto fail;
1330 ret = crypto_aead_setauthsize(tfm, icv_len);
1331 if (ret < 0)
1332 goto fail;
1334 return tfm;
1335 fail:
1336 crypto_free_aead(tfm);
1337 return ERR_PTR(ret);
1340 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1341 int icv_len)
1343 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1344 if (!rx_sa->stats)
1345 return -ENOMEM;
1347 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1348 if (IS_ERR(rx_sa->key.tfm)) {
1349 free_percpu(rx_sa->stats);
1350 return PTR_ERR(rx_sa->key.tfm);
1353 rx_sa->active = false;
1354 rx_sa->next_pn = 1;
1355 atomic_set(&rx_sa->refcnt, 1);
1356 spin_lock_init(&rx_sa->lock);
1358 return 0;
1361 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1363 rx_sa->active = false;
1365 macsec_rxsa_put(rx_sa);
1368 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1370 int i;
1372 for (i = 0; i < MACSEC_NUM_AN; i++) {
1373 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1375 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1376 if (sa)
1377 clear_rx_sa(sa);
1380 macsec_rxsc_put(rx_sc);
1383 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1385 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1387 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1388 rx_sc;
1389 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1390 if (rx_sc->sci == sci) {
1391 if (rx_sc->active)
1392 secy->n_rx_sc--;
1393 rcu_assign_pointer(*rx_scp, rx_sc->next);
1394 return rx_sc;
1398 return NULL;
1401 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1403 struct macsec_rx_sc *rx_sc;
1404 struct macsec_dev *macsec;
1405 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1406 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1407 struct macsec_secy *secy;
1409 list_for_each_entry(macsec, &rxd->secys, secys) {
1410 if (find_rx_sc_rtnl(&macsec->secy, sci))
1411 return ERR_PTR(-EEXIST);
1414 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1415 if (!rx_sc)
1416 return ERR_PTR(-ENOMEM);
1418 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1419 if (!rx_sc->stats) {
1420 kfree(rx_sc);
1421 return ERR_PTR(-ENOMEM);
1424 rx_sc->sci = sci;
1425 rx_sc->active = true;
1426 atomic_set(&rx_sc->refcnt, 1);
1428 secy = &macsec_priv(dev)->secy;
1429 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1430 rcu_assign_pointer(secy->rx_sc, rx_sc);
1432 if (rx_sc->active)
1433 secy->n_rx_sc++;
1435 return rx_sc;
1438 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1439 int icv_len)
1441 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1442 if (!tx_sa->stats)
1443 return -ENOMEM;
1445 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1446 if (IS_ERR(tx_sa->key.tfm)) {
1447 free_percpu(tx_sa->stats);
1448 return PTR_ERR(tx_sa->key.tfm);
1451 tx_sa->active = false;
1452 atomic_set(&tx_sa->refcnt, 1);
1453 spin_lock_init(&tx_sa->lock);
1455 return 0;
1458 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1460 tx_sa->active = false;
1462 macsec_txsa_put(tx_sa);
1465 static struct genl_family macsec_fam = {
1466 .id = GENL_ID_GENERATE,
1467 .name = MACSEC_GENL_NAME,
1468 .hdrsize = 0,
1469 .version = MACSEC_GENL_VERSION,
1470 .maxattr = MACSEC_ATTR_MAX,
1471 .netnsok = true,
1474 static struct net_device *get_dev_from_nl(struct net *net,
1475 struct nlattr **attrs)
1477 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1478 struct net_device *dev;
1480 dev = __dev_get_by_index(net, ifindex);
1481 if (!dev)
1482 return ERR_PTR(-ENODEV);
1484 if (!netif_is_macsec(dev))
1485 return ERR_PTR(-ENODEV);
1487 return dev;
1490 static sci_t nla_get_sci(const struct nlattr *nla)
1492 return (__force sci_t)nla_get_u64(nla);
1495 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1496 int padattr)
1498 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1501 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1502 struct nlattr **attrs,
1503 struct nlattr **tb_sa,
1504 struct net_device **devp,
1505 struct macsec_secy **secyp,
1506 struct macsec_tx_sc **scp,
1507 u8 *assoc_num)
1509 struct net_device *dev;
1510 struct macsec_secy *secy;
1511 struct macsec_tx_sc *tx_sc;
1512 struct macsec_tx_sa *tx_sa;
1514 if (!tb_sa[MACSEC_SA_ATTR_AN])
1515 return ERR_PTR(-EINVAL);
1517 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1519 dev = get_dev_from_nl(net, attrs);
1520 if (IS_ERR(dev))
1521 return ERR_CAST(dev);
1523 if (*assoc_num >= MACSEC_NUM_AN)
1524 return ERR_PTR(-EINVAL);
1526 secy = &macsec_priv(dev)->secy;
1527 tx_sc = &secy->tx_sc;
1529 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1530 if (!tx_sa)
1531 return ERR_PTR(-ENODEV);
1533 *devp = dev;
1534 *scp = tx_sc;
1535 *secyp = secy;
1536 return tx_sa;
1539 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1540 struct nlattr **attrs,
1541 struct nlattr **tb_rxsc,
1542 struct net_device **devp,
1543 struct macsec_secy **secyp)
1545 struct net_device *dev;
1546 struct macsec_secy *secy;
1547 struct macsec_rx_sc *rx_sc;
1548 sci_t sci;
1550 dev = get_dev_from_nl(net, attrs);
1551 if (IS_ERR(dev))
1552 return ERR_CAST(dev);
1554 secy = &macsec_priv(dev)->secy;
1556 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1557 return ERR_PTR(-EINVAL);
1559 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1560 rx_sc = find_rx_sc_rtnl(secy, sci);
1561 if (!rx_sc)
1562 return ERR_PTR(-ENODEV);
1564 *secyp = secy;
1565 *devp = dev;
1567 return rx_sc;
1570 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1571 struct nlattr **attrs,
1572 struct nlattr **tb_rxsc,
1573 struct nlattr **tb_sa,
1574 struct net_device **devp,
1575 struct macsec_secy **secyp,
1576 struct macsec_rx_sc **scp,
1577 u8 *assoc_num)
1579 struct macsec_rx_sc *rx_sc;
1580 struct macsec_rx_sa *rx_sa;
1582 if (!tb_sa[MACSEC_SA_ATTR_AN])
1583 return ERR_PTR(-EINVAL);
1585 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1586 if (*assoc_num >= MACSEC_NUM_AN)
1587 return ERR_PTR(-EINVAL);
1589 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1590 if (IS_ERR(rx_sc))
1591 return ERR_CAST(rx_sc);
1593 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1594 if (!rx_sa)
1595 return ERR_PTR(-ENODEV);
1597 *scp = rx_sc;
1598 return rx_sa;
1602 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1603 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1604 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1605 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1608 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1609 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1610 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1613 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1614 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1615 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1616 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1617 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1618 .len = MACSEC_KEYID_LEN, },
1619 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1620 .len = MACSEC_MAX_KEY_LEN, },
1623 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1625 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1626 return -EINVAL;
1628 if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG],
1629 macsec_genl_sa_policy))
1630 return -EINVAL;
1632 return 0;
1635 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1637 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1638 return -EINVAL;
1640 if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG],
1641 macsec_genl_rxsc_policy))
1642 return -EINVAL;
1644 return 0;
1647 static bool validate_add_rxsa(struct nlattr **attrs)
1649 if (!attrs[MACSEC_SA_ATTR_AN] ||
1650 !attrs[MACSEC_SA_ATTR_KEY] ||
1651 !attrs[MACSEC_SA_ATTR_KEYID])
1652 return false;
1654 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1655 return false;
1657 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1658 return false;
1660 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1661 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1662 return false;
1665 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1666 return false;
1668 return true;
1671 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1673 struct net_device *dev;
1674 struct nlattr **attrs = info->attrs;
1675 struct macsec_secy *secy;
1676 struct macsec_rx_sc *rx_sc;
1677 struct macsec_rx_sa *rx_sa;
1678 unsigned char assoc_num;
1679 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1680 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1681 int err;
1683 if (!attrs[MACSEC_ATTR_IFINDEX])
1684 return -EINVAL;
1686 if (parse_sa_config(attrs, tb_sa))
1687 return -EINVAL;
1689 if (parse_rxsc_config(attrs, tb_rxsc))
1690 return -EINVAL;
1692 if (!validate_add_rxsa(tb_sa))
1693 return -EINVAL;
1695 rtnl_lock();
1696 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1697 if (IS_ERR(rx_sc)) {
1698 rtnl_unlock();
1699 return PTR_ERR(rx_sc);
1702 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1704 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1705 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1706 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1707 rtnl_unlock();
1708 return -EINVAL;
1711 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1712 if (rx_sa) {
1713 rtnl_unlock();
1714 return -EBUSY;
1717 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1718 if (!rx_sa) {
1719 rtnl_unlock();
1720 return -ENOMEM;
1723 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1724 secy->key_len, secy->icv_len);
1725 if (err < 0) {
1726 kfree(rx_sa);
1727 rtnl_unlock();
1728 return err;
1731 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1732 spin_lock_bh(&rx_sa->lock);
1733 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1734 spin_unlock_bh(&rx_sa->lock);
1737 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1738 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1740 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1741 rx_sa->sc = rx_sc;
1742 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1744 rtnl_unlock();
1746 return 0;
1749 static bool validate_add_rxsc(struct nlattr **attrs)
1751 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1752 return false;
1754 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1755 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1756 return false;
1759 return true;
1762 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1764 struct net_device *dev;
1765 sci_t sci = MACSEC_UNDEF_SCI;
1766 struct nlattr **attrs = info->attrs;
1767 struct macsec_rx_sc *rx_sc;
1768 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1770 if (!attrs[MACSEC_ATTR_IFINDEX])
1771 return -EINVAL;
1773 if (parse_rxsc_config(attrs, tb_rxsc))
1774 return -EINVAL;
1776 if (!validate_add_rxsc(tb_rxsc))
1777 return -EINVAL;
1779 rtnl_lock();
1780 dev = get_dev_from_nl(genl_info_net(info), attrs);
1781 if (IS_ERR(dev)) {
1782 rtnl_unlock();
1783 return PTR_ERR(dev);
1786 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1788 rx_sc = create_rx_sc(dev, sci);
1789 if (IS_ERR(rx_sc)) {
1790 rtnl_unlock();
1791 return PTR_ERR(rx_sc);
1794 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1795 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1797 rtnl_unlock();
1799 return 0;
1802 static bool validate_add_txsa(struct nlattr **attrs)
1804 if (!attrs[MACSEC_SA_ATTR_AN] ||
1805 !attrs[MACSEC_SA_ATTR_PN] ||
1806 !attrs[MACSEC_SA_ATTR_KEY] ||
1807 !attrs[MACSEC_SA_ATTR_KEYID])
1808 return false;
1810 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1811 return false;
1813 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1814 return false;
1816 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1817 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1818 return false;
1821 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1822 return false;
1824 return true;
1827 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1829 struct net_device *dev;
1830 struct nlattr **attrs = info->attrs;
1831 struct macsec_secy *secy;
1832 struct macsec_tx_sc *tx_sc;
1833 struct macsec_tx_sa *tx_sa;
1834 unsigned char assoc_num;
1835 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1836 int err;
1838 if (!attrs[MACSEC_ATTR_IFINDEX])
1839 return -EINVAL;
1841 if (parse_sa_config(attrs, tb_sa))
1842 return -EINVAL;
1844 if (!validate_add_txsa(tb_sa))
1845 return -EINVAL;
1847 rtnl_lock();
1848 dev = get_dev_from_nl(genl_info_net(info), attrs);
1849 if (IS_ERR(dev)) {
1850 rtnl_unlock();
1851 return PTR_ERR(dev);
1854 secy = &macsec_priv(dev)->secy;
1855 tx_sc = &secy->tx_sc;
1857 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1859 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1860 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1861 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1862 rtnl_unlock();
1863 return -EINVAL;
1866 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1867 if (tx_sa) {
1868 rtnl_unlock();
1869 return -EBUSY;
1872 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1873 if (!tx_sa) {
1874 rtnl_unlock();
1875 return -ENOMEM;
1878 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1879 secy->key_len, secy->icv_len);
1880 if (err < 0) {
1881 kfree(tx_sa);
1882 rtnl_unlock();
1883 return err;
1886 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1888 spin_lock_bh(&tx_sa->lock);
1889 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1890 spin_unlock_bh(&tx_sa->lock);
1892 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1893 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1895 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1896 secy->operational = true;
1898 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1900 rtnl_unlock();
1902 return 0;
1905 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1907 struct nlattr **attrs = info->attrs;
1908 struct net_device *dev;
1909 struct macsec_secy *secy;
1910 struct macsec_rx_sc *rx_sc;
1911 struct macsec_rx_sa *rx_sa;
1912 u8 assoc_num;
1913 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1914 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1916 if (!attrs[MACSEC_ATTR_IFINDEX])
1917 return -EINVAL;
1919 if (parse_sa_config(attrs, tb_sa))
1920 return -EINVAL;
1922 if (parse_rxsc_config(attrs, tb_rxsc))
1923 return -EINVAL;
1925 rtnl_lock();
1926 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1927 &dev, &secy, &rx_sc, &assoc_num);
1928 if (IS_ERR(rx_sa)) {
1929 rtnl_unlock();
1930 return PTR_ERR(rx_sa);
1933 if (rx_sa->active) {
1934 rtnl_unlock();
1935 return -EBUSY;
1938 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1939 clear_rx_sa(rx_sa);
1941 rtnl_unlock();
1943 return 0;
1946 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1948 struct nlattr **attrs = info->attrs;
1949 struct net_device *dev;
1950 struct macsec_secy *secy;
1951 struct macsec_rx_sc *rx_sc;
1952 sci_t sci;
1953 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1955 if (!attrs[MACSEC_ATTR_IFINDEX])
1956 return -EINVAL;
1958 if (parse_rxsc_config(attrs, tb_rxsc))
1959 return -EINVAL;
1961 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1962 return -EINVAL;
1964 rtnl_lock();
1965 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1966 if (IS_ERR(dev)) {
1967 rtnl_unlock();
1968 return PTR_ERR(dev);
1971 secy = &macsec_priv(dev)->secy;
1972 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1974 rx_sc = del_rx_sc(secy, sci);
1975 if (!rx_sc) {
1976 rtnl_unlock();
1977 return -ENODEV;
1980 free_rx_sc(rx_sc);
1981 rtnl_unlock();
1983 return 0;
1986 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1988 struct nlattr **attrs = info->attrs;
1989 struct net_device *dev;
1990 struct macsec_secy *secy;
1991 struct macsec_tx_sc *tx_sc;
1992 struct macsec_tx_sa *tx_sa;
1993 u8 assoc_num;
1994 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1996 if (!attrs[MACSEC_ATTR_IFINDEX])
1997 return -EINVAL;
1999 if (parse_sa_config(attrs, tb_sa))
2000 return -EINVAL;
2002 rtnl_lock();
2003 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2004 &dev, &secy, &tx_sc, &assoc_num);
2005 if (IS_ERR(tx_sa)) {
2006 rtnl_unlock();
2007 return PTR_ERR(tx_sa);
2010 if (tx_sa->active) {
2011 rtnl_unlock();
2012 return -EBUSY;
2015 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2016 clear_tx_sa(tx_sa);
2018 rtnl_unlock();
2020 return 0;
2023 static bool validate_upd_sa(struct nlattr **attrs)
2025 if (!attrs[MACSEC_SA_ATTR_AN] ||
2026 attrs[MACSEC_SA_ATTR_KEY] ||
2027 attrs[MACSEC_SA_ATTR_KEYID])
2028 return false;
2030 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2031 return false;
2033 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2034 return false;
2036 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2037 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2038 return false;
2041 return true;
2044 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2046 struct nlattr **attrs = info->attrs;
2047 struct net_device *dev;
2048 struct macsec_secy *secy;
2049 struct macsec_tx_sc *tx_sc;
2050 struct macsec_tx_sa *tx_sa;
2051 u8 assoc_num;
2052 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2054 if (!attrs[MACSEC_ATTR_IFINDEX])
2055 return -EINVAL;
2057 if (parse_sa_config(attrs, tb_sa))
2058 return -EINVAL;
2060 if (!validate_upd_sa(tb_sa))
2061 return -EINVAL;
2063 rtnl_lock();
2064 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2065 &dev, &secy, &tx_sc, &assoc_num);
2066 if (IS_ERR(tx_sa)) {
2067 rtnl_unlock();
2068 return PTR_ERR(tx_sa);
2071 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2072 spin_lock_bh(&tx_sa->lock);
2073 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2074 spin_unlock_bh(&tx_sa->lock);
2077 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2078 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2080 if (assoc_num == tx_sc->encoding_sa)
2081 secy->operational = tx_sa->active;
2083 rtnl_unlock();
2085 return 0;
2088 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2090 struct nlattr **attrs = info->attrs;
2091 struct net_device *dev;
2092 struct macsec_secy *secy;
2093 struct macsec_rx_sc *rx_sc;
2094 struct macsec_rx_sa *rx_sa;
2095 u8 assoc_num;
2096 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2097 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2099 if (!attrs[MACSEC_ATTR_IFINDEX])
2100 return -EINVAL;
2102 if (parse_rxsc_config(attrs, tb_rxsc))
2103 return -EINVAL;
2105 if (parse_sa_config(attrs, tb_sa))
2106 return -EINVAL;
2108 if (!validate_upd_sa(tb_sa))
2109 return -EINVAL;
2111 rtnl_lock();
2112 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2113 &dev, &secy, &rx_sc, &assoc_num);
2114 if (IS_ERR(rx_sa)) {
2115 rtnl_unlock();
2116 return PTR_ERR(rx_sa);
2119 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2120 spin_lock_bh(&rx_sa->lock);
2121 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2122 spin_unlock_bh(&rx_sa->lock);
2125 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2126 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2128 rtnl_unlock();
2129 return 0;
2132 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2134 struct nlattr **attrs = info->attrs;
2135 struct net_device *dev;
2136 struct macsec_secy *secy;
2137 struct macsec_rx_sc *rx_sc;
2138 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2140 if (!attrs[MACSEC_ATTR_IFINDEX])
2141 return -EINVAL;
2143 if (parse_rxsc_config(attrs, tb_rxsc))
2144 return -EINVAL;
2146 if (!validate_add_rxsc(tb_rxsc))
2147 return -EINVAL;
2149 rtnl_lock();
2150 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2151 if (IS_ERR(rx_sc)) {
2152 rtnl_unlock();
2153 return PTR_ERR(rx_sc);
2156 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2157 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2159 if (rx_sc->active != new)
2160 secy->n_rx_sc += new ? 1 : -1;
2162 rx_sc->active = new;
2165 rtnl_unlock();
2167 return 0;
2170 static int copy_tx_sa_stats(struct sk_buff *skb,
2171 struct macsec_tx_sa_stats __percpu *pstats)
2173 struct macsec_tx_sa_stats sum = {0, };
2174 int cpu;
2176 for_each_possible_cpu(cpu) {
2177 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2179 sum.OutPktsProtected += stats->OutPktsProtected;
2180 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2183 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2184 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2185 return -EMSGSIZE;
2187 return 0;
2190 static int copy_rx_sa_stats(struct sk_buff *skb,
2191 struct macsec_rx_sa_stats __percpu *pstats)
2193 struct macsec_rx_sa_stats sum = {0, };
2194 int cpu;
2196 for_each_possible_cpu(cpu) {
2197 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2199 sum.InPktsOK += stats->InPktsOK;
2200 sum.InPktsInvalid += stats->InPktsInvalid;
2201 sum.InPktsNotValid += stats->InPktsNotValid;
2202 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2203 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2206 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2207 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2208 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2209 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2210 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2211 return -EMSGSIZE;
2213 return 0;
2216 static int copy_rx_sc_stats(struct sk_buff *skb,
2217 struct pcpu_rx_sc_stats __percpu *pstats)
2219 struct macsec_rx_sc_stats sum = {0, };
2220 int cpu;
2222 for_each_possible_cpu(cpu) {
2223 const struct pcpu_rx_sc_stats *stats;
2224 struct macsec_rx_sc_stats tmp;
2225 unsigned int start;
2227 stats = per_cpu_ptr(pstats, cpu);
2228 do {
2229 start = u64_stats_fetch_begin_irq(&stats->syncp);
2230 memcpy(&tmp, &stats->stats, sizeof(tmp));
2231 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2233 sum.InOctetsValidated += tmp.InOctetsValidated;
2234 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2235 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2236 sum.InPktsDelayed += tmp.InPktsDelayed;
2237 sum.InPktsOK += tmp.InPktsOK;
2238 sum.InPktsInvalid += tmp.InPktsInvalid;
2239 sum.InPktsLate += tmp.InPktsLate;
2240 sum.InPktsNotValid += tmp.InPktsNotValid;
2241 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2242 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2245 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2246 sum.InOctetsValidated,
2247 MACSEC_RXSC_STATS_ATTR_PAD) ||
2248 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2249 sum.InOctetsDecrypted,
2250 MACSEC_RXSC_STATS_ATTR_PAD) ||
2251 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2252 sum.InPktsUnchecked,
2253 MACSEC_RXSC_STATS_ATTR_PAD) ||
2254 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2255 sum.InPktsDelayed,
2256 MACSEC_RXSC_STATS_ATTR_PAD) ||
2257 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2258 sum.InPktsOK,
2259 MACSEC_RXSC_STATS_ATTR_PAD) ||
2260 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2261 sum.InPktsInvalid,
2262 MACSEC_RXSC_STATS_ATTR_PAD) ||
2263 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2264 sum.InPktsLate,
2265 MACSEC_RXSC_STATS_ATTR_PAD) ||
2266 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2267 sum.InPktsNotValid,
2268 MACSEC_RXSC_STATS_ATTR_PAD) ||
2269 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2270 sum.InPktsNotUsingSA,
2271 MACSEC_RXSC_STATS_ATTR_PAD) ||
2272 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2273 sum.InPktsUnusedSA,
2274 MACSEC_RXSC_STATS_ATTR_PAD))
2275 return -EMSGSIZE;
2277 return 0;
2280 static int copy_tx_sc_stats(struct sk_buff *skb,
2281 struct pcpu_tx_sc_stats __percpu *pstats)
2283 struct macsec_tx_sc_stats sum = {0, };
2284 int cpu;
2286 for_each_possible_cpu(cpu) {
2287 const struct pcpu_tx_sc_stats *stats;
2288 struct macsec_tx_sc_stats tmp;
2289 unsigned int start;
2291 stats = per_cpu_ptr(pstats, cpu);
2292 do {
2293 start = u64_stats_fetch_begin_irq(&stats->syncp);
2294 memcpy(&tmp, &stats->stats, sizeof(tmp));
2295 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2297 sum.OutPktsProtected += tmp.OutPktsProtected;
2298 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2299 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2300 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2303 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2304 sum.OutPktsProtected,
2305 MACSEC_TXSC_STATS_ATTR_PAD) ||
2306 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2307 sum.OutPktsEncrypted,
2308 MACSEC_TXSC_STATS_ATTR_PAD) ||
2309 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2310 sum.OutOctetsProtected,
2311 MACSEC_TXSC_STATS_ATTR_PAD) ||
2312 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2313 sum.OutOctetsEncrypted,
2314 MACSEC_TXSC_STATS_ATTR_PAD))
2315 return -EMSGSIZE;
2317 return 0;
2320 static int copy_secy_stats(struct sk_buff *skb,
2321 struct pcpu_secy_stats __percpu *pstats)
2323 struct macsec_dev_stats sum = {0, };
2324 int cpu;
2326 for_each_possible_cpu(cpu) {
2327 const struct pcpu_secy_stats *stats;
2328 struct macsec_dev_stats tmp;
2329 unsigned int start;
2331 stats = per_cpu_ptr(pstats, cpu);
2332 do {
2333 start = u64_stats_fetch_begin_irq(&stats->syncp);
2334 memcpy(&tmp, &stats->stats, sizeof(tmp));
2335 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2337 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2338 sum.InPktsUntagged += tmp.InPktsUntagged;
2339 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2340 sum.InPktsNoTag += tmp.InPktsNoTag;
2341 sum.InPktsBadTag += tmp.InPktsBadTag;
2342 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2343 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2344 sum.InPktsOverrun += tmp.InPktsOverrun;
2347 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2348 sum.OutPktsUntagged,
2349 MACSEC_SECY_STATS_ATTR_PAD) ||
2350 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2351 sum.InPktsUntagged,
2352 MACSEC_SECY_STATS_ATTR_PAD) ||
2353 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2354 sum.OutPktsTooLong,
2355 MACSEC_SECY_STATS_ATTR_PAD) ||
2356 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2357 sum.InPktsNoTag,
2358 MACSEC_SECY_STATS_ATTR_PAD) ||
2359 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2360 sum.InPktsBadTag,
2361 MACSEC_SECY_STATS_ATTR_PAD) ||
2362 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2363 sum.InPktsUnknownSCI,
2364 MACSEC_SECY_STATS_ATTR_PAD) ||
2365 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2366 sum.InPktsNoSCI,
2367 MACSEC_SECY_STATS_ATTR_PAD) ||
2368 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2369 sum.InPktsOverrun,
2370 MACSEC_SECY_STATS_ATTR_PAD))
2371 return -EMSGSIZE;
2373 return 0;
2376 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2378 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2379 struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
2381 if (!secy_nest)
2382 return 1;
2384 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2385 MACSEC_SECY_ATTR_PAD) ||
2386 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2387 MACSEC_DEFAULT_CIPHER_ID,
2388 MACSEC_SECY_ATTR_PAD) ||
2389 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2390 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2391 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2392 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2393 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2394 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2395 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2396 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2397 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2398 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2399 goto cancel;
2401 if (secy->replay_protect) {
2402 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2403 goto cancel;
2406 nla_nest_end(skb, secy_nest);
2407 return 0;
2409 cancel:
2410 nla_nest_cancel(skb, secy_nest);
2411 return 1;
2414 static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
2415 struct sk_buff *skb, struct netlink_callback *cb)
2417 struct macsec_rx_sc *rx_sc;
2418 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2419 struct nlattr *txsa_list, *rxsc_list;
2420 int i, j;
2421 void *hdr;
2422 struct nlattr *attr;
2424 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2425 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2426 if (!hdr)
2427 return -EMSGSIZE;
2429 genl_dump_check_consistent(cb, hdr, &macsec_fam);
2431 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2432 goto nla_put_failure;
2434 if (nla_put_secy(secy, skb))
2435 goto nla_put_failure;
2437 attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
2438 if (!attr)
2439 goto nla_put_failure;
2440 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2441 nla_nest_cancel(skb, attr);
2442 goto nla_put_failure;
2444 nla_nest_end(skb, attr);
2446 attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
2447 if (!attr)
2448 goto nla_put_failure;
2449 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2450 nla_nest_cancel(skb, attr);
2451 goto nla_put_failure;
2453 nla_nest_end(skb, attr);
2455 txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
2456 if (!txsa_list)
2457 goto nla_put_failure;
2458 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2459 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2460 struct nlattr *txsa_nest;
2462 if (!tx_sa)
2463 continue;
2465 txsa_nest = nla_nest_start(skb, j++);
2466 if (!txsa_nest) {
2467 nla_nest_cancel(skb, txsa_list);
2468 goto nla_put_failure;
2471 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2472 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2473 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2474 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2475 nla_nest_cancel(skb, txsa_nest);
2476 nla_nest_cancel(skb, txsa_list);
2477 goto nla_put_failure;
2480 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2481 if (!attr) {
2482 nla_nest_cancel(skb, txsa_nest);
2483 nla_nest_cancel(skb, txsa_list);
2484 goto nla_put_failure;
2486 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2487 nla_nest_cancel(skb, attr);
2488 nla_nest_cancel(skb, txsa_nest);
2489 nla_nest_cancel(skb, txsa_list);
2490 goto nla_put_failure;
2492 nla_nest_end(skb, attr);
2494 nla_nest_end(skb, txsa_nest);
2496 nla_nest_end(skb, txsa_list);
2498 rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
2499 if (!rxsc_list)
2500 goto nla_put_failure;
2502 j = 1;
2503 for_each_rxsc_rtnl(secy, rx_sc) {
2504 int k;
2505 struct nlattr *rxsa_list;
2506 struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
2508 if (!rxsc_nest) {
2509 nla_nest_cancel(skb, rxsc_list);
2510 goto nla_put_failure;
2513 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2514 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2515 MACSEC_RXSC_ATTR_PAD)) {
2516 nla_nest_cancel(skb, rxsc_nest);
2517 nla_nest_cancel(skb, rxsc_list);
2518 goto nla_put_failure;
2521 attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
2522 if (!attr) {
2523 nla_nest_cancel(skb, rxsc_nest);
2524 nla_nest_cancel(skb, rxsc_list);
2525 goto nla_put_failure;
2527 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2528 nla_nest_cancel(skb, attr);
2529 nla_nest_cancel(skb, rxsc_nest);
2530 nla_nest_cancel(skb, rxsc_list);
2531 goto nla_put_failure;
2533 nla_nest_end(skb, attr);
2535 rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
2536 if (!rxsa_list) {
2537 nla_nest_cancel(skb, rxsc_nest);
2538 nla_nest_cancel(skb, rxsc_list);
2539 goto nla_put_failure;
2542 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2543 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2544 struct nlattr *rxsa_nest;
2546 if (!rx_sa)
2547 continue;
2549 rxsa_nest = nla_nest_start(skb, k++);
2550 if (!rxsa_nest) {
2551 nla_nest_cancel(skb, rxsa_list);
2552 nla_nest_cancel(skb, rxsc_nest);
2553 nla_nest_cancel(skb, rxsc_list);
2554 goto nla_put_failure;
2557 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2558 if (!attr) {
2559 nla_nest_cancel(skb, rxsa_list);
2560 nla_nest_cancel(skb, rxsc_nest);
2561 nla_nest_cancel(skb, rxsc_list);
2562 goto nla_put_failure;
2564 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2565 nla_nest_cancel(skb, attr);
2566 nla_nest_cancel(skb, rxsa_list);
2567 nla_nest_cancel(skb, rxsc_nest);
2568 nla_nest_cancel(skb, rxsc_list);
2569 goto nla_put_failure;
2571 nla_nest_end(skb, attr);
2573 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2574 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2575 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2576 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2577 nla_nest_cancel(skb, rxsa_nest);
2578 nla_nest_cancel(skb, rxsc_nest);
2579 nla_nest_cancel(skb, rxsc_list);
2580 goto nla_put_failure;
2582 nla_nest_end(skb, rxsa_nest);
2585 nla_nest_end(skb, rxsa_list);
2586 nla_nest_end(skb, rxsc_nest);
2589 nla_nest_end(skb, rxsc_list);
2591 genlmsg_end(skb, hdr);
2593 return 0;
2595 nla_put_failure:
2596 genlmsg_cancel(skb, hdr);
2597 return -EMSGSIZE;
2600 static int macsec_generation = 1; /* protected by RTNL */
2602 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2604 struct net *net = sock_net(skb->sk);
2605 struct net_device *dev;
2606 int dev_idx, d;
2608 dev_idx = cb->args[0];
2610 d = 0;
2611 rtnl_lock();
2613 cb->seq = macsec_generation;
2615 for_each_netdev(net, dev) {
2616 struct macsec_secy *secy;
2618 if (d < dev_idx)
2619 goto next;
2621 if (!netif_is_macsec(dev))
2622 goto next;
2624 secy = &macsec_priv(dev)->secy;
2625 if (dump_secy(secy, dev, skb, cb) < 0)
2626 goto done;
2627 next:
2628 d++;
2631 done:
2632 rtnl_unlock();
2633 cb->args[0] = d;
2634 return skb->len;
2637 static const struct genl_ops macsec_genl_ops[] = {
2639 .cmd = MACSEC_CMD_GET_TXSC,
2640 .dumpit = macsec_dump_txsc,
2641 .policy = macsec_genl_policy,
2644 .cmd = MACSEC_CMD_ADD_RXSC,
2645 .doit = macsec_add_rxsc,
2646 .policy = macsec_genl_policy,
2647 .flags = GENL_ADMIN_PERM,
2650 .cmd = MACSEC_CMD_DEL_RXSC,
2651 .doit = macsec_del_rxsc,
2652 .policy = macsec_genl_policy,
2653 .flags = GENL_ADMIN_PERM,
2656 .cmd = MACSEC_CMD_UPD_RXSC,
2657 .doit = macsec_upd_rxsc,
2658 .policy = macsec_genl_policy,
2659 .flags = GENL_ADMIN_PERM,
2662 .cmd = MACSEC_CMD_ADD_TXSA,
2663 .doit = macsec_add_txsa,
2664 .policy = macsec_genl_policy,
2665 .flags = GENL_ADMIN_PERM,
2668 .cmd = MACSEC_CMD_DEL_TXSA,
2669 .doit = macsec_del_txsa,
2670 .policy = macsec_genl_policy,
2671 .flags = GENL_ADMIN_PERM,
2674 .cmd = MACSEC_CMD_UPD_TXSA,
2675 .doit = macsec_upd_txsa,
2676 .policy = macsec_genl_policy,
2677 .flags = GENL_ADMIN_PERM,
2680 .cmd = MACSEC_CMD_ADD_RXSA,
2681 .doit = macsec_add_rxsa,
2682 .policy = macsec_genl_policy,
2683 .flags = GENL_ADMIN_PERM,
2686 .cmd = MACSEC_CMD_DEL_RXSA,
2687 .doit = macsec_del_rxsa,
2688 .policy = macsec_genl_policy,
2689 .flags = GENL_ADMIN_PERM,
2692 .cmd = MACSEC_CMD_UPD_RXSA,
2693 .doit = macsec_upd_rxsa,
2694 .policy = macsec_genl_policy,
2695 .flags = GENL_ADMIN_PERM,
2699 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2700 struct net_device *dev)
2702 struct macsec_dev *macsec = netdev_priv(dev);
2703 struct macsec_secy *secy = &macsec->secy;
2704 struct pcpu_secy_stats *secy_stats;
2705 int ret, len;
2707 /* 10.5 */
2708 if (!secy->protect_frames) {
2709 secy_stats = this_cpu_ptr(macsec->stats);
2710 u64_stats_update_begin(&secy_stats->syncp);
2711 secy_stats->stats.OutPktsUntagged++;
2712 u64_stats_update_end(&secy_stats->syncp);
2713 skb->dev = macsec->real_dev;
2714 len = skb->len;
2715 ret = dev_queue_xmit(skb);
2716 count_tx(dev, ret, len);
2717 return ret;
2720 if (!secy->operational) {
2721 kfree_skb(skb);
2722 dev->stats.tx_dropped++;
2723 return NETDEV_TX_OK;
2726 skb = macsec_encrypt(skb, dev);
2727 if (IS_ERR(skb)) {
2728 if (PTR_ERR(skb) != -EINPROGRESS)
2729 dev->stats.tx_dropped++;
2730 return NETDEV_TX_OK;
2733 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2735 macsec_encrypt_finish(skb, dev);
2736 len = skb->len;
2737 ret = dev_queue_xmit(skb);
2738 count_tx(dev, ret, len);
2739 return ret;
2742 #define MACSEC_FEATURES \
2743 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
2744 static struct lock_class_key macsec_netdev_addr_lock_key;
2746 static int macsec_dev_init(struct net_device *dev)
2748 struct macsec_dev *macsec = macsec_priv(dev);
2749 struct net_device *real_dev = macsec->real_dev;
2750 int err;
2752 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2753 if (!dev->tstats)
2754 return -ENOMEM;
2756 err = gro_cells_init(&macsec->gro_cells, dev);
2757 if (err) {
2758 free_percpu(dev->tstats);
2759 return err;
2762 dev->features = real_dev->features & MACSEC_FEATURES;
2763 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2765 dev->needed_headroom = real_dev->needed_headroom +
2766 MACSEC_NEEDED_HEADROOM;
2767 dev->needed_tailroom = real_dev->needed_tailroom +
2768 MACSEC_NEEDED_TAILROOM;
2770 if (is_zero_ether_addr(dev->dev_addr))
2771 eth_hw_addr_inherit(dev, real_dev);
2772 if (is_zero_ether_addr(dev->broadcast))
2773 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2775 return 0;
2778 static void macsec_dev_uninit(struct net_device *dev)
2780 struct macsec_dev *macsec = macsec_priv(dev);
2782 gro_cells_destroy(&macsec->gro_cells);
2783 free_percpu(dev->tstats);
2786 static netdev_features_t macsec_fix_features(struct net_device *dev,
2787 netdev_features_t features)
2789 struct macsec_dev *macsec = macsec_priv(dev);
2790 struct net_device *real_dev = macsec->real_dev;
2792 features &= (real_dev->features & MACSEC_FEATURES) |
2793 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2794 features |= NETIF_F_LLTX;
2796 return features;
2799 static int macsec_dev_open(struct net_device *dev)
2801 struct macsec_dev *macsec = macsec_priv(dev);
2802 struct net_device *real_dev = macsec->real_dev;
2803 int err;
2805 err = dev_uc_add(real_dev, dev->dev_addr);
2806 if (err < 0)
2807 return err;
2809 if (dev->flags & IFF_ALLMULTI) {
2810 err = dev_set_allmulti(real_dev, 1);
2811 if (err < 0)
2812 goto del_unicast;
2815 if (dev->flags & IFF_PROMISC) {
2816 err = dev_set_promiscuity(real_dev, 1);
2817 if (err < 0)
2818 goto clear_allmulti;
2821 if (netif_carrier_ok(real_dev))
2822 netif_carrier_on(dev);
2824 return 0;
2825 clear_allmulti:
2826 if (dev->flags & IFF_ALLMULTI)
2827 dev_set_allmulti(real_dev, -1);
2828 del_unicast:
2829 dev_uc_del(real_dev, dev->dev_addr);
2830 netif_carrier_off(dev);
2831 return err;
2834 static int macsec_dev_stop(struct net_device *dev)
2836 struct macsec_dev *macsec = macsec_priv(dev);
2837 struct net_device *real_dev = macsec->real_dev;
2839 netif_carrier_off(dev);
2841 dev_mc_unsync(real_dev, dev);
2842 dev_uc_unsync(real_dev, dev);
2844 if (dev->flags & IFF_ALLMULTI)
2845 dev_set_allmulti(real_dev, -1);
2847 if (dev->flags & IFF_PROMISC)
2848 dev_set_promiscuity(real_dev, -1);
2850 dev_uc_del(real_dev, dev->dev_addr);
2852 return 0;
2855 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2857 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2859 if (!(dev->flags & IFF_UP))
2860 return;
2862 if (change & IFF_ALLMULTI)
2863 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2865 if (change & IFF_PROMISC)
2866 dev_set_promiscuity(real_dev,
2867 dev->flags & IFF_PROMISC ? 1 : -1);
2870 static void macsec_dev_set_rx_mode(struct net_device *dev)
2872 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2874 dev_mc_sync(real_dev, dev);
2875 dev_uc_sync(real_dev, dev);
2878 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
2880 return make_sci(dev->dev_addr, port);
2883 static int macsec_set_mac_address(struct net_device *dev, void *p)
2885 struct macsec_dev *macsec = macsec_priv(dev);
2886 struct net_device *real_dev = macsec->real_dev;
2887 struct sockaddr *addr = p;
2888 int err;
2890 if (!is_valid_ether_addr(addr->sa_data))
2891 return -EADDRNOTAVAIL;
2893 if (!(dev->flags & IFF_UP))
2894 goto out;
2896 err = dev_uc_add(real_dev, addr->sa_data);
2897 if (err < 0)
2898 return err;
2900 dev_uc_del(real_dev, dev->dev_addr);
2902 out:
2903 ether_addr_copy(dev->dev_addr, addr->sa_data);
2904 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
2905 return 0;
2908 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2910 struct macsec_dev *macsec = macsec_priv(dev);
2911 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2913 if (macsec->real_dev->mtu - extra < new_mtu)
2914 return -ERANGE;
2916 dev->mtu = new_mtu;
2918 return 0;
2921 static struct rtnl_link_stats64 *macsec_get_stats64(struct net_device *dev,
2922 struct rtnl_link_stats64 *s)
2924 int cpu;
2926 if (!dev->tstats)
2927 return s;
2929 for_each_possible_cpu(cpu) {
2930 struct pcpu_sw_netstats *stats;
2931 struct pcpu_sw_netstats tmp;
2932 int start;
2934 stats = per_cpu_ptr(dev->tstats, cpu);
2935 do {
2936 start = u64_stats_fetch_begin_irq(&stats->syncp);
2937 tmp.rx_packets = stats->rx_packets;
2938 tmp.rx_bytes = stats->rx_bytes;
2939 tmp.tx_packets = stats->tx_packets;
2940 tmp.tx_bytes = stats->tx_bytes;
2941 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2943 s->rx_packets += tmp.rx_packets;
2944 s->rx_bytes += tmp.rx_bytes;
2945 s->tx_packets += tmp.tx_packets;
2946 s->tx_bytes += tmp.tx_bytes;
2949 s->rx_dropped = dev->stats.rx_dropped;
2950 s->tx_dropped = dev->stats.tx_dropped;
2952 return s;
2955 static int macsec_get_iflink(const struct net_device *dev)
2957 return macsec_priv(dev)->real_dev->ifindex;
2961 static int macsec_get_nest_level(struct net_device *dev)
2963 return macsec_priv(dev)->nest_level;
2967 static const struct net_device_ops macsec_netdev_ops = {
2968 .ndo_init = macsec_dev_init,
2969 .ndo_uninit = macsec_dev_uninit,
2970 .ndo_open = macsec_dev_open,
2971 .ndo_stop = macsec_dev_stop,
2972 .ndo_fix_features = macsec_fix_features,
2973 .ndo_change_mtu = macsec_change_mtu,
2974 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2975 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2976 .ndo_set_mac_address = macsec_set_mac_address,
2977 .ndo_start_xmit = macsec_start_xmit,
2978 .ndo_get_stats64 = macsec_get_stats64,
2979 .ndo_get_iflink = macsec_get_iflink,
2980 .ndo_get_lock_subclass = macsec_get_nest_level,
2983 static const struct device_type macsec_type = {
2984 .name = "macsec",
2987 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2988 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
2989 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
2990 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2991 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2992 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2993 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2994 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2995 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2996 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2997 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
2998 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
2999 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3000 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3003 static void macsec_free_netdev(struct net_device *dev)
3005 struct macsec_dev *macsec = macsec_priv(dev);
3006 struct net_device *real_dev = macsec->real_dev;
3008 free_percpu(macsec->stats);
3009 free_percpu(macsec->secy.tx_sc.stats);
3011 dev_put(real_dev);
3012 free_netdev(dev);
3015 static void macsec_setup(struct net_device *dev)
3017 ether_setup(dev);
3018 dev->priv_flags |= IFF_NO_QUEUE;
3019 dev->netdev_ops = &macsec_netdev_ops;
3020 dev->destructor = macsec_free_netdev;
3021 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3023 eth_zero_addr(dev->broadcast);
3026 static void macsec_changelink_common(struct net_device *dev,
3027 struct nlattr *data[])
3029 struct macsec_secy *secy;
3030 struct macsec_tx_sc *tx_sc;
3032 secy = &macsec_priv(dev)->secy;
3033 tx_sc = &secy->tx_sc;
3035 if (data[IFLA_MACSEC_ENCODING_SA]) {
3036 struct macsec_tx_sa *tx_sa;
3038 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3039 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3041 secy->operational = tx_sa && tx_sa->active;
3044 if (data[IFLA_MACSEC_WINDOW])
3045 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3047 if (data[IFLA_MACSEC_ENCRYPT])
3048 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3050 if (data[IFLA_MACSEC_PROTECT])
3051 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3053 if (data[IFLA_MACSEC_INC_SCI])
3054 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3056 if (data[IFLA_MACSEC_ES])
3057 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3059 if (data[IFLA_MACSEC_SCB])
3060 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3062 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3063 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3065 if (data[IFLA_MACSEC_VALIDATION])
3066 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3069 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3070 struct nlattr *data[])
3072 if (!data)
3073 return 0;
3075 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3076 data[IFLA_MACSEC_ICV_LEN] ||
3077 data[IFLA_MACSEC_SCI] ||
3078 data[IFLA_MACSEC_PORT])
3079 return -EINVAL;
3081 macsec_changelink_common(dev, data);
3083 return 0;
3086 static void macsec_del_dev(struct macsec_dev *macsec)
3088 int i;
3090 while (macsec->secy.rx_sc) {
3091 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3093 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3094 free_rx_sc(rx_sc);
3097 for (i = 0; i < MACSEC_NUM_AN; i++) {
3098 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3100 if (sa) {
3101 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3102 clear_tx_sa(sa);
3107 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3109 struct macsec_dev *macsec = macsec_priv(dev);
3110 struct net_device *real_dev = macsec->real_dev;
3112 unregister_netdevice_queue(dev, head);
3113 list_del_rcu(&macsec->secys);
3114 macsec_del_dev(macsec);
3115 netdev_upper_dev_unlink(real_dev, dev);
3117 macsec_generation++;
3120 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3122 struct macsec_dev *macsec = macsec_priv(dev);
3123 struct net_device *real_dev = macsec->real_dev;
3124 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3126 macsec_common_dellink(dev, head);
3128 if (list_empty(&rxd->secys)) {
3129 netdev_rx_handler_unregister(real_dev);
3130 kfree(rxd);
3134 static int register_macsec_dev(struct net_device *real_dev,
3135 struct net_device *dev)
3137 struct macsec_dev *macsec = macsec_priv(dev);
3138 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3140 if (!rxd) {
3141 int err;
3143 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3144 if (!rxd)
3145 return -ENOMEM;
3147 INIT_LIST_HEAD(&rxd->secys);
3149 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3150 rxd);
3151 if (err < 0) {
3152 kfree(rxd);
3153 return err;
3157 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3158 return 0;
3161 static bool sci_exists(struct net_device *dev, sci_t sci)
3163 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3164 struct macsec_dev *macsec;
3166 list_for_each_entry(macsec, &rxd->secys, secys) {
3167 if (macsec->secy.sci == sci)
3168 return true;
3171 return false;
3174 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3176 struct macsec_dev *macsec = macsec_priv(dev);
3177 struct macsec_secy *secy = &macsec->secy;
3179 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3180 if (!macsec->stats)
3181 return -ENOMEM;
3183 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3184 if (!secy->tx_sc.stats) {
3185 free_percpu(macsec->stats);
3186 return -ENOMEM;
3189 if (sci == MACSEC_UNDEF_SCI)
3190 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3192 secy->netdev = dev;
3193 secy->operational = true;
3194 secy->key_len = DEFAULT_SAK_LEN;
3195 secy->icv_len = icv_len;
3196 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3197 secy->protect_frames = true;
3198 secy->replay_protect = false;
3200 secy->sci = sci;
3201 secy->tx_sc.active = true;
3202 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3203 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3204 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3205 secy->tx_sc.end_station = false;
3206 secy->tx_sc.scb = false;
3208 return 0;
3211 static int macsec_newlink(struct net *net, struct net_device *dev,
3212 struct nlattr *tb[], struct nlattr *data[])
3214 struct macsec_dev *macsec = macsec_priv(dev);
3215 rx_handler_func_t *rx_handler;
3216 u8 icv_len = DEFAULT_ICV_LEN;
3217 struct net_device *real_dev;
3218 int err, mtu;
3219 sci_t sci;
3221 if (!tb[IFLA_LINK])
3222 return -EINVAL;
3223 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3224 if (!real_dev)
3225 return -ENODEV;
3226 if (real_dev->type != ARPHRD_ETHER)
3227 return -EINVAL;
3229 dev->priv_flags |= IFF_MACSEC;
3231 macsec->real_dev = real_dev;
3233 if (data && data[IFLA_MACSEC_ICV_LEN])
3234 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3235 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3236 if (mtu < 0)
3237 dev->mtu = 0;
3238 else
3239 dev->mtu = mtu;
3241 rx_handler = rtnl_dereference(real_dev->rx_handler);
3242 if (rx_handler && rx_handler != macsec_handle_frame)
3243 return -EBUSY;
3245 err = register_netdevice(dev);
3246 if (err < 0)
3247 return err;
3249 dev_hold(real_dev);
3251 macsec->nest_level = dev_get_nest_level(real_dev) + 1;
3252 netdev_lockdep_set_classes(dev);
3253 lockdep_set_class_and_subclass(&dev->addr_list_lock,
3254 &macsec_netdev_addr_lock_key,
3255 macsec_get_nest_level(dev));
3257 err = netdev_upper_dev_link(real_dev, dev);
3258 if (err < 0)
3259 goto unregister;
3261 /* need to be already registered so that ->init has run and
3262 * the MAC addr is set
3264 if (data && data[IFLA_MACSEC_SCI])
3265 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3266 else if (data && data[IFLA_MACSEC_PORT])
3267 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3268 else
3269 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3271 if (rx_handler && sci_exists(real_dev, sci)) {
3272 err = -EBUSY;
3273 goto unlink;
3276 err = macsec_add_dev(dev, sci, icv_len);
3277 if (err)
3278 goto unlink;
3280 if (data)
3281 macsec_changelink_common(dev, data);
3283 err = register_macsec_dev(real_dev, dev);
3284 if (err < 0)
3285 goto del_dev;
3287 netif_stacked_transfer_operstate(real_dev, dev);
3288 linkwatch_fire_event(dev);
3290 macsec_generation++;
3292 return 0;
3294 del_dev:
3295 macsec_del_dev(macsec);
3296 unlink:
3297 netdev_upper_dev_unlink(real_dev, dev);
3298 unregister:
3299 unregister_netdevice(dev);
3300 return err;
3303 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[])
3305 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3306 u8 icv_len = DEFAULT_ICV_LEN;
3307 int flag;
3308 bool es, scb, sci;
3310 if (!data)
3311 return 0;
3313 if (data[IFLA_MACSEC_CIPHER_SUITE])
3314 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3316 if (data[IFLA_MACSEC_ICV_LEN]) {
3317 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3318 if (icv_len != DEFAULT_ICV_LEN) {
3319 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3320 struct crypto_aead *dummy_tfm;
3322 dummy_tfm = macsec_alloc_tfm(dummy_key,
3323 DEFAULT_SAK_LEN,
3324 icv_len);
3325 if (IS_ERR(dummy_tfm))
3326 return PTR_ERR(dummy_tfm);
3327 crypto_free_aead(dummy_tfm);
3331 switch (csid) {
3332 case MACSEC_DEFAULT_CIPHER_ID:
3333 case MACSEC_DEFAULT_CIPHER_ALT:
3334 if (icv_len < MACSEC_MIN_ICV_LEN ||
3335 icv_len > MACSEC_STD_ICV_LEN)
3336 return -EINVAL;
3337 break;
3338 default:
3339 return -EINVAL;
3342 if (data[IFLA_MACSEC_ENCODING_SA]) {
3343 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3344 return -EINVAL;
3347 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3348 flag < IFLA_MACSEC_VALIDATION;
3349 flag++) {
3350 if (data[flag]) {
3351 if (nla_get_u8(data[flag]) > 1)
3352 return -EINVAL;
3356 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3357 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3358 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3360 if ((sci && (scb || es)) || (scb && es))
3361 return -EINVAL;
3363 if (data[IFLA_MACSEC_VALIDATION] &&
3364 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3365 return -EINVAL;
3367 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3368 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3369 !data[IFLA_MACSEC_WINDOW])
3370 return -EINVAL;
3372 return 0;
3375 static struct net *macsec_get_link_net(const struct net_device *dev)
3377 return dev_net(macsec_priv(dev)->real_dev);
3380 static size_t macsec_get_size(const struct net_device *dev)
3382 return 0 +
3383 nla_total_size_64bit(8) + /* SCI */
3384 nla_total_size(1) + /* ICV_LEN */
3385 nla_total_size_64bit(8) + /* CIPHER_SUITE */
3386 nla_total_size(4) + /* WINDOW */
3387 nla_total_size(1) + /* ENCODING_SA */
3388 nla_total_size(1) + /* ENCRYPT */
3389 nla_total_size(1) + /* PROTECT */
3390 nla_total_size(1) + /* INC_SCI */
3391 nla_total_size(1) + /* ES */
3392 nla_total_size(1) + /* SCB */
3393 nla_total_size(1) + /* REPLAY_PROTECT */
3394 nla_total_size(1) + /* VALIDATION */
3398 static int macsec_fill_info(struct sk_buff *skb,
3399 const struct net_device *dev)
3401 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3402 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3404 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3405 IFLA_MACSEC_PAD) ||
3406 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3407 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3408 MACSEC_DEFAULT_CIPHER_ID, IFLA_MACSEC_PAD) ||
3409 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3410 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3411 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3412 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3413 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3414 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3415 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3416 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3418 goto nla_put_failure;
3420 if (secy->replay_protect) {
3421 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3422 goto nla_put_failure;
3425 return 0;
3427 nla_put_failure:
3428 return -EMSGSIZE;
3431 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3432 .kind = "macsec",
3433 .priv_size = sizeof(struct macsec_dev),
3434 .maxtype = IFLA_MACSEC_MAX,
3435 .policy = macsec_rtnl_policy,
3436 .setup = macsec_setup,
3437 .validate = macsec_validate_attr,
3438 .newlink = macsec_newlink,
3439 .changelink = macsec_changelink,
3440 .dellink = macsec_dellink,
3441 .get_size = macsec_get_size,
3442 .fill_info = macsec_fill_info,
3443 .get_link_net = macsec_get_link_net,
3446 static bool is_macsec_master(struct net_device *dev)
3448 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3451 static int macsec_notify(struct notifier_block *this, unsigned long event,
3452 void *ptr)
3454 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3455 LIST_HEAD(head);
3457 if (!is_macsec_master(real_dev))
3458 return NOTIFY_DONE;
3460 switch (event) {
3461 case NETDEV_DOWN:
3462 case NETDEV_UP:
3463 case NETDEV_CHANGE: {
3464 struct macsec_dev *m, *n;
3465 struct macsec_rxh_data *rxd;
3467 rxd = macsec_data_rtnl(real_dev);
3468 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3469 struct net_device *dev = m->secy.netdev;
3471 netif_stacked_transfer_operstate(real_dev, dev);
3473 break;
3475 case NETDEV_UNREGISTER: {
3476 struct macsec_dev *m, *n;
3477 struct macsec_rxh_data *rxd;
3479 rxd = macsec_data_rtnl(real_dev);
3480 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3481 macsec_common_dellink(m->secy.netdev, &head);
3484 netdev_rx_handler_unregister(real_dev);
3485 kfree(rxd);
3487 unregister_netdevice_many(&head);
3488 break;
3490 case NETDEV_CHANGEMTU: {
3491 struct macsec_dev *m;
3492 struct macsec_rxh_data *rxd;
3494 rxd = macsec_data_rtnl(real_dev);
3495 list_for_each_entry(m, &rxd->secys, secys) {
3496 struct net_device *dev = m->secy.netdev;
3497 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3498 macsec_extra_len(true));
3500 if (dev->mtu > mtu)
3501 dev_set_mtu(dev, mtu);
3506 return NOTIFY_OK;
3509 static struct notifier_block macsec_notifier = {
3510 .notifier_call = macsec_notify,
3513 static int __init macsec_init(void)
3515 int err;
3517 pr_info("MACsec IEEE 802.1AE\n");
3518 err = register_netdevice_notifier(&macsec_notifier);
3519 if (err)
3520 return err;
3522 err = rtnl_link_register(&macsec_link_ops);
3523 if (err)
3524 goto notifier;
3526 err = genl_register_family_with_ops(&macsec_fam, macsec_genl_ops);
3527 if (err)
3528 goto rtnl;
3530 return 0;
3532 rtnl:
3533 rtnl_link_unregister(&macsec_link_ops);
3534 notifier:
3535 unregister_netdevice_notifier(&macsec_notifier);
3536 return err;
3539 static void __exit macsec_exit(void)
3541 genl_unregister_family(&macsec_fam);
3542 rtnl_link_unregister(&macsec_link_ops);
3543 unregister_netdevice_notifier(&macsec_notifier);
3544 rcu_barrier();
3547 module_init(macsec_init);
3548 module_exit(macsec_exit);
3550 MODULE_ALIAS_RTNL_LINK("macsec");
3551 MODULE_ALIAS_GENL_FAMILY("macsec");
3553 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3554 MODULE_LICENSE("GPL v2");