xref: /linux/drivers/net/macsec.c (revision c73690ca16b1dd17b5e45d23fb2d76d083fb03bf)
1 /*
2  * drivers/net/macsec.c - MACsec device
3  *
4  * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
5  *
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.
10  */
11 
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 
23 #include <uapi/linux/if_macsec.h>
24 
25 typedef u64 __bitwise sci_t;
26 
27 #define MACSEC_SCI_LEN 8
28 
29 /* SecTAG length = macsec_eth_header without the optional SCI */
30 #define MACSEC_TAG_LEN 6
31 
32 struct macsec_eth_header {
33 	struct ethhdr eth;
34 	/* SecTAG */
35 	u8  tci_an;
36 #if defined(__LITTLE_ENDIAN_BITFIELD)
37 	u8  short_length:6,
38 		  unused:2;
39 #elif defined(__BIG_ENDIAN_BITFIELD)
40 	u8        unused:2,
41 	    short_length:6;
42 #else
43 #error	"Please fix <asm/byteorder.h>"
44 #endif
45 	__be32 packet_number;
46 	u8 secure_channel_id[8]; /* optional */
47 } __packed;
48 
49 #define MACSEC_TCI_VERSION 0x80
50 #define MACSEC_TCI_ES      0x40 /* end station */
51 #define MACSEC_TCI_SC      0x20 /* SCI present */
52 #define MACSEC_TCI_SCB     0x10 /* epon */
53 #define MACSEC_TCI_E       0x08 /* encryption */
54 #define MACSEC_TCI_C       0x04 /* changed text */
55 #define MACSEC_AN_MASK     0x03 /* association number */
56 #define MACSEC_TCI_CONFID  (MACSEC_TCI_E | MACSEC_TCI_C)
57 
58 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
59 #define MIN_NON_SHORT_LEN 48
60 
61 #define GCM_AES_IV_LEN 12
62 #define DEFAULT_ICV_LEN 16
63 
64 #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
65 
66 #define for_each_rxsc(secy, sc)			\
67 	for (sc = rcu_dereference_bh(secy->rx_sc);	\
68 	     sc;				\
69 	     sc = rcu_dereference_bh(sc->next))
70 #define for_each_rxsc_rtnl(secy, sc)			\
71 	for (sc = rtnl_dereference(secy->rx_sc);	\
72 	     sc;					\
73 	     sc = rtnl_dereference(sc->next))
74 
75 struct gcm_iv {
76 	union {
77 		u8 secure_channel_id[8];
78 		sci_t sci;
79 	};
80 	__be32 pn;
81 };
82 
83 /**
84  * struct macsec_key - SA key
85  * @id: user-provided key identifier
86  * @tfm: crypto struct, key storage
87  */
88 struct macsec_key {
89 	u8 id[MACSEC_KEYID_LEN];
90 	struct crypto_aead *tfm;
91 };
92 
93 struct macsec_rx_sc_stats {
94 	__u64 InOctetsValidated;
95 	__u64 InOctetsDecrypted;
96 	__u64 InPktsUnchecked;
97 	__u64 InPktsDelayed;
98 	__u64 InPktsOK;
99 	__u64 InPktsInvalid;
100 	__u64 InPktsLate;
101 	__u64 InPktsNotValid;
102 	__u64 InPktsNotUsingSA;
103 	__u64 InPktsUnusedSA;
104 };
105 
106 struct macsec_rx_sa_stats {
107 	__u32 InPktsOK;
108 	__u32 InPktsInvalid;
109 	__u32 InPktsNotValid;
110 	__u32 InPktsNotUsingSA;
111 	__u32 InPktsUnusedSA;
112 };
113 
114 struct macsec_tx_sa_stats {
115 	__u32 OutPktsProtected;
116 	__u32 OutPktsEncrypted;
117 };
118 
119 struct macsec_tx_sc_stats {
120 	__u64 OutPktsProtected;
121 	__u64 OutPktsEncrypted;
122 	__u64 OutOctetsProtected;
123 	__u64 OutOctetsEncrypted;
124 };
125 
126 struct macsec_dev_stats {
127 	__u64 OutPktsUntagged;
128 	__u64 InPktsUntagged;
129 	__u64 OutPktsTooLong;
130 	__u64 InPktsNoTag;
131 	__u64 InPktsBadTag;
132 	__u64 InPktsUnknownSCI;
133 	__u64 InPktsNoSCI;
134 	__u64 InPktsOverrun;
135 };
136 
137 /**
138  * struct macsec_rx_sa - receive secure association
139  * @active:
140  * @next_pn: packet number expected for the next packet
141  * @lock: protects next_pn manipulations
142  * @key: key structure
143  * @stats: per-SA stats
144  */
145 struct macsec_rx_sa {
146 	struct macsec_key key;
147 	spinlock_t lock;
148 	u32 next_pn;
149 	atomic_t refcnt;
150 	bool active;
151 	struct macsec_rx_sa_stats __percpu *stats;
152 	struct macsec_rx_sc *sc;
153 	struct rcu_head rcu;
154 };
155 
156 struct pcpu_rx_sc_stats {
157 	struct macsec_rx_sc_stats stats;
158 	struct u64_stats_sync syncp;
159 };
160 
161 /**
162  * struct macsec_rx_sc - receive secure channel
163  * @sci: secure channel identifier for this SC
164  * @active: channel is active
165  * @sa: array of secure associations
166  * @stats: per-SC stats
167  */
168 struct macsec_rx_sc {
169 	struct macsec_rx_sc __rcu *next;
170 	sci_t sci;
171 	bool active;
172 	struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
173 	struct pcpu_rx_sc_stats __percpu *stats;
174 	atomic_t refcnt;
175 	struct rcu_head rcu_head;
176 };
177 
178 /**
179  * struct macsec_tx_sa - transmit secure association
180  * @active:
181  * @next_pn: packet number to use for the next packet
182  * @lock: protects next_pn manipulations
183  * @key: key structure
184  * @stats: per-SA stats
185  */
186 struct macsec_tx_sa {
187 	struct macsec_key key;
188 	spinlock_t lock;
189 	u32 next_pn;
190 	atomic_t refcnt;
191 	bool active;
192 	struct macsec_tx_sa_stats __percpu *stats;
193 	struct rcu_head rcu;
194 };
195 
196 struct pcpu_tx_sc_stats {
197 	struct macsec_tx_sc_stats stats;
198 	struct u64_stats_sync syncp;
199 };
200 
201 /**
202  * struct macsec_tx_sc - transmit secure channel
203  * @active:
204  * @encoding_sa: association number of the SA currently in use
205  * @encrypt: encrypt packets on transmit, or authenticate only
206  * @send_sci: always include the SCI in the SecTAG
207  * @end_station:
208  * @scb: single copy broadcast flag
209  * @sa: array of secure associations
210  * @stats: stats for this TXSC
211  */
212 struct macsec_tx_sc {
213 	bool active;
214 	u8 encoding_sa;
215 	bool encrypt;
216 	bool send_sci;
217 	bool end_station;
218 	bool scb;
219 	struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
220 	struct pcpu_tx_sc_stats __percpu *stats;
221 };
222 
223 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
224 
225 /**
226  * struct macsec_secy - MACsec Security Entity
227  * @netdev: netdevice for this SecY
228  * @n_rx_sc: number of receive secure channels configured on this SecY
229  * @sci: secure channel identifier used for tx
230  * @key_len: length of keys used by the cipher suite
231  * @icv_len: length of ICV used by the cipher suite
232  * @validate_frames: validation mode
233  * @operational: MAC_Operational flag
234  * @protect_frames: enable protection for this SecY
235  * @replay_protect: enable packet number checks on receive
236  * @replay_window: size of the replay window
237  * @tx_sc: transmit secure channel
238  * @rx_sc: linked list of receive secure channels
239  */
240 struct macsec_secy {
241 	struct net_device *netdev;
242 	unsigned int n_rx_sc;
243 	sci_t sci;
244 	u16 key_len;
245 	u16 icv_len;
246 	enum macsec_validation_type validate_frames;
247 	bool operational;
248 	bool protect_frames;
249 	bool replay_protect;
250 	u32 replay_window;
251 	struct macsec_tx_sc tx_sc;
252 	struct macsec_rx_sc __rcu *rx_sc;
253 };
254 
255 struct pcpu_secy_stats {
256 	struct macsec_dev_stats stats;
257 	struct u64_stats_sync syncp;
258 };
259 
260 /**
261  * struct macsec_dev - private data
262  * @secy: SecY config
263  * @real_dev: pointer to underlying netdevice
264  * @stats: MACsec device stats
265  * @secys: linked list of SecY's on the underlying device
266  */
267 struct macsec_dev {
268 	struct macsec_secy secy;
269 	struct net_device *real_dev;
270 	struct pcpu_secy_stats __percpu *stats;
271 	struct list_head secys;
272 	struct gro_cells gro_cells;
273 	unsigned int nest_level;
274 };
275 
276 /**
277  * struct macsec_rxh_data - rx_handler private argument
278  * @secys: linked list of SecY's on this underlying device
279  */
280 struct macsec_rxh_data {
281 	struct list_head secys;
282 };
283 
284 static struct macsec_dev *macsec_priv(const struct net_device *dev)
285 {
286 	return (struct macsec_dev *)netdev_priv(dev);
287 }
288 
289 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
290 {
291 	return rcu_dereference_bh(dev->rx_handler_data);
292 }
293 
294 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
295 {
296 	return rtnl_dereference(dev->rx_handler_data);
297 }
298 
299 struct macsec_cb {
300 	struct aead_request *req;
301 	union {
302 		struct macsec_tx_sa *tx_sa;
303 		struct macsec_rx_sa *rx_sa;
304 	};
305 	u8 assoc_num;
306 	bool valid;
307 	bool has_sci;
308 };
309 
310 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
311 {
312 	struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
313 
314 	if (!sa || !sa->active)
315 		return NULL;
316 
317 	if (!atomic_inc_not_zero(&sa->refcnt))
318 		return NULL;
319 
320 	return sa;
321 }
322 
323 static void free_rx_sc_rcu(struct rcu_head *head)
324 {
325 	struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
326 
327 	free_percpu(rx_sc->stats);
328 	kfree(rx_sc);
329 }
330 
331 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
332 {
333 	return atomic_inc_not_zero(&sc->refcnt) ? sc : NULL;
334 }
335 
336 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
337 {
338 	if (atomic_dec_and_test(&sc->refcnt))
339 		call_rcu(&sc->rcu_head, free_rx_sc_rcu);
340 }
341 
342 static void free_rxsa(struct rcu_head *head)
343 {
344 	struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
345 
346 	crypto_free_aead(sa->key.tfm);
347 	free_percpu(sa->stats);
348 	kfree(sa);
349 }
350 
351 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
352 {
353 	if (atomic_dec_and_test(&sa->refcnt))
354 		call_rcu(&sa->rcu, free_rxsa);
355 }
356 
357 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
358 {
359 	struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
360 
361 	if (!sa || !sa->active)
362 		return NULL;
363 
364 	if (!atomic_inc_not_zero(&sa->refcnt))
365 		return NULL;
366 
367 	return sa;
368 }
369 
370 static void free_txsa(struct rcu_head *head)
371 {
372 	struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
373 
374 	crypto_free_aead(sa->key.tfm);
375 	free_percpu(sa->stats);
376 	kfree(sa);
377 }
378 
379 static void macsec_txsa_put(struct macsec_tx_sa *sa)
380 {
381 	if (atomic_dec_and_test(&sa->refcnt))
382 		call_rcu(&sa->rcu, free_txsa);
383 }
384 
385 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
386 {
387 	BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
388 	return (struct macsec_cb *)skb->cb;
389 }
390 
391 #define MACSEC_PORT_ES (htons(0x0001))
392 #define MACSEC_PORT_SCB (0x0000)
393 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
394 
395 #define DEFAULT_SAK_LEN 16
396 #define DEFAULT_SEND_SCI true
397 #define DEFAULT_ENCRYPT false
398 #define DEFAULT_ENCODING_SA 0
399 
400 static bool send_sci(const struct macsec_secy *secy)
401 {
402 	const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
403 
404 	return tx_sc->send_sci ||
405 		(secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
406 }
407 
408 static sci_t make_sci(u8 *addr, __be16 port)
409 {
410 	sci_t sci;
411 
412 	memcpy(&sci, addr, ETH_ALEN);
413 	memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
414 
415 	return sci;
416 }
417 
418 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
419 {
420 	sci_t sci;
421 
422 	if (sci_present)
423 		memcpy(&sci, hdr->secure_channel_id,
424 		       sizeof(hdr->secure_channel_id));
425 	else
426 		sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
427 
428 	return sci;
429 }
430 
431 static unsigned int macsec_sectag_len(bool sci_present)
432 {
433 	return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
434 }
435 
436 static unsigned int macsec_hdr_len(bool sci_present)
437 {
438 	return macsec_sectag_len(sci_present) + ETH_HLEN;
439 }
440 
441 static unsigned int macsec_extra_len(bool sci_present)
442 {
443 	return macsec_sectag_len(sci_present) + sizeof(__be16);
444 }
445 
446 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
447 static void macsec_fill_sectag(struct macsec_eth_header *h,
448 			       const struct macsec_secy *secy, u32 pn,
449 			       bool sci_present)
450 {
451 	const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
452 
453 	memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
454 	h->eth.h_proto = htons(ETH_P_MACSEC);
455 
456 	if (sci_present) {
457 		h->tci_an |= MACSEC_TCI_SC;
458 		memcpy(&h->secure_channel_id, &secy->sci,
459 		       sizeof(h->secure_channel_id));
460 	} else {
461 		if (tx_sc->end_station)
462 			h->tci_an |= MACSEC_TCI_ES;
463 		if (tx_sc->scb)
464 			h->tci_an |= MACSEC_TCI_SCB;
465 	}
466 
467 	h->packet_number = htonl(pn);
468 
469 	/* with GCM, C/E clear for !encrypt, both set for encrypt */
470 	if (tx_sc->encrypt)
471 		h->tci_an |= MACSEC_TCI_CONFID;
472 	else if (secy->icv_len != DEFAULT_ICV_LEN)
473 		h->tci_an |= MACSEC_TCI_C;
474 
475 	h->tci_an |= tx_sc->encoding_sa;
476 }
477 
478 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
479 {
480 	if (data_len < MIN_NON_SHORT_LEN)
481 		h->short_length = data_len;
482 }
483 
484 /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
485 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
486 {
487 	struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
488 	int len = skb->len - 2 * ETH_ALEN;
489 	int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
490 
491 	/* a) It comprises at least 17 octets */
492 	if (skb->len <= 16)
493 		return false;
494 
495 	/* b) MACsec EtherType: already checked */
496 
497 	/* c) V bit is clear */
498 	if (h->tci_an & MACSEC_TCI_VERSION)
499 		return false;
500 
501 	/* d) ES or SCB => !SC */
502 	if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
503 	    (h->tci_an & MACSEC_TCI_SC))
504 		return false;
505 
506 	/* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
507 	if (h->unused)
508 		return false;
509 
510 	/* rx.pn != 0 (figure 10-5) */
511 	if (!h->packet_number)
512 		return false;
513 
514 	/* length check, f) g) h) i) */
515 	if (h->short_length)
516 		return len == extra_len + h->short_length;
517 	return len >= extra_len + MIN_NON_SHORT_LEN;
518 }
519 
520 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
521 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
522 
523 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
524 {
525 	struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
526 
527 	gcm_iv->sci = sci;
528 	gcm_iv->pn = htonl(pn);
529 }
530 
531 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
532 {
533 	return (struct macsec_eth_header *)skb_mac_header(skb);
534 }
535 
536 static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
537 {
538 	u32 pn;
539 
540 	spin_lock_bh(&tx_sa->lock);
541 	pn = tx_sa->next_pn;
542 
543 	tx_sa->next_pn++;
544 	if (tx_sa->next_pn == 0) {
545 		pr_debug("PN wrapped, transitioning to !oper\n");
546 		tx_sa->active = false;
547 		if (secy->protect_frames)
548 			secy->operational = false;
549 	}
550 	spin_unlock_bh(&tx_sa->lock);
551 
552 	return pn;
553 }
554 
555 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
556 {
557 	struct macsec_dev *macsec = netdev_priv(dev);
558 
559 	skb->dev = macsec->real_dev;
560 	skb_reset_mac_header(skb);
561 	skb->protocol = eth_hdr(skb)->h_proto;
562 }
563 
564 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
565 			    struct macsec_tx_sa *tx_sa)
566 {
567 	struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
568 
569 	u64_stats_update_begin(&txsc_stats->syncp);
570 	if (tx_sc->encrypt) {
571 		txsc_stats->stats.OutOctetsEncrypted += skb->len;
572 		txsc_stats->stats.OutPktsEncrypted++;
573 		this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
574 	} else {
575 		txsc_stats->stats.OutOctetsProtected += skb->len;
576 		txsc_stats->stats.OutPktsProtected++;
577 		this_cpu_inc(tx_sa->stats->OutPktsProtected);
578 	}
579 	u64_stats_update_end(&txsc_stats->syncp);
580 }
581 
582 static void count_tx(struct net_device *dev, int ret, int len)
583 {
584 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
585 		struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
586 
587 		u64_stats_update_begin(&stats->syncp);
588 		stats->tx_packets++;
589 		stats->tx_bytes += len;
590 		u64_stats_update_end(&stats->syncp);
591 	} else {
592 		dev->stats.tx_dropped++;
593 	}
594 }
595 
596 static void macsec_encrypt_done(struct crypto_async_request *base, int err)
597 {
598 	struct sk_buff *skb = base->data;
599 	struct net_device *dev = skb->dev;
600 	struct macsec_dev *macsec = macsec_priv(dev);
601 	struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
602 	int len, ret;
603 
604 	aead_request_free(macsec_skb_cb(skb)->req);
605 
606 	rcu_read_lock_bh();
607 	macsec_encrypt_finish(skb, dev);
608 	macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
609 	len = skb->len;
610 	ret = dev_queue_xmit(skb);
611 	count_tx(dev, ret, len);
612 	rcu_read_unlock_bh();
613 
614 	macsec_txsa_put(sa);
615 	dev_put(dev);
616 }
617 
618 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
619 					     unsigned char **iv,
620 					     struct scatterlist **sg)
621 {
622 	size_t size, iv_offset, sg_offset;
623 	struct aead_request *req;
624 	void *tmp;
625 
626 	size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
627 	iv_offset = size;
628 	size += GCM_AES_IV_LEN;
629 
630 	size = ALIGN(size, __alignof__(struct scatterlist));
631 	sg_offset = size;
632 	size += sizeof(struct scatterlist) * (MAX_SKB_FRAGS + 1);
633 
634 	tmp = kmalloc(size, GFP_ATOMIC);
635 	if (!tmp)
636 		return NULL;
637 
638 	*iv = (unsigned char *)(tmp + iv_offset);
639 	*sg = (struct scatterlist *)(tmp + sg_offset);
640 	req = tmp;
641 
642 	aead_request_set_tfm(req, tfm);
643 
644 	return req;
645 }
646 
647 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
648 				      struct net_device *dev)
649 {
650 	int ret;
651 	struct scatterlist *sg;
652 	unsigned char *iv;
653 	struct ethhdr *eth;
654 	struct macsec_eth_header *hh;
655 	size_t unprotected_len;
656 	struct aead_request *req;
657 	struct macsec_secy *secy;
658 	struct macsec_tx_sc *tx_sc;
659 	struct macsec_tx_sa *tx_sa;
660 	struct macsec_dev *macsec = macsec_priv(dev);
661 	bool sci_present;
662 	u32 pn;
663 
664 	secy = &macsec->secy;
665 	tx_sc = &secy->tx_sc;
666 
667 	/* 10.5.1 TX SA assignment */
668 	tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
669 	if (!tx_sa) {
670 		secy->operational = false;
671 		kfree_skb(skb);
672 		return ERR_PTR(-EINVAL);
673 	}
674 
675 	if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
676 		     skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
677 		struct sk_buff *nskb = skb_copy_expand(skb,
678 						       MACSEC_NEEDED_HEADROOM,
679 						       MACSEC_NEEDED_TAILROOM,
680 						       GFP_ATOMIC);
681 		if (likely(nskb)) {
682 			consume_skb(skb);
683 			skb = nskb;
684 		} else {
685 			macsec_txsa_put(tx_sa);
686 			kfree_skb(skb);
687 			return ERR_PTR(-ENOMEM);
688 		}
689 	} else {
690 		skb = skb_unshare(skb, GFP_ATOMIC);
691 		if (!skb) {
692 			macsec_txsa_put(tx_sa);
693 			return ERR_PTR(-ENOMEM);
694 		}
695 	}
696 
697 	unprotected_len = skb->len;
698 	eth = eth_hdr(skb);
699 	sci_present = send_sci(secy);
700 	hh = (struct macsec_eth_header *)skb_push(skb, macsec_extra_len(sci_present));
701 	memmove(hh, eth, 2 * ETH_ALEN);
702 
703 	pn = tx_sa_update_pn(tx_sa, secy);
704 	if (pn == 0) {
705 		macsec_txsa_put(tx_sa);
706 		kfree_skb(skb);
707 		return ERR_PTR(-ENOLINK);
708 	}
709 	macsec_fill_sectag(hh, secy, pn, sci_present);
710 	macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
711 
712 	skb_put(skb, secy->icv_len);
713 
714 	if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
715 		struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
716 
717 		u64_stats_update_begin(&secy_stats->syncp);
718 		secy_stats->stats.OutPktsTooLong++;
719 		u64_stats_update_end(&secy_stats->syncp);
720 
721 		macsec_txsa_put(tx_sa);
722 		kfree_skb(skb);
723 		return ERR_PTR(-EINVAL);
724 	}
725 
726 	req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg);
727 	if (!req) {
728 		macsec_txsa_put(tx_sa);
729 		kfree_skb(skb);
730 		return ERR_PTR(-ENOMEM);
731 	}
732 
733 	macsec_fill_iv(iv, secy->sci, pn);
734 
735 	sg_init_table(sg, MAX_SKB_FRAGS + 1);
736 	skb_to_sgvec(skb, sg, 0, skb->len);
737 
738 	if (tx_sc->encrypt) {
739 		int len = skb->len - macsec_hdr_len(sci_present) -
740 			  secy->icv_len;
741 		aead_request_set_crypt(req, sg, sg, len, iv);
742 		aead_request_set_ad(req, macsec_hdr_len(sci_present));
743 	} else {
744 		aead_request_set_crypt(req, sg, sg, 0, iv);
745 		aead_request_set_ad(req, skb->len - secy->icv_len);
746 	}
747 
748 	macsec_skb_cb(skb)->req = req;
749 	macsec_skb_cb(skb)->tx_sa = tx_sa;
750 	aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
751 
752 	dev_hold(skb->dev);
753 	ret = crypto_aead_encrypt(req);
754 	if (ret == -EINPROGRESS) {
755 		return ERR_PTR(ret);
756 	} else if (ret != 0) {
757 		dev_put(skb->dev);
758 		kfree_skb(skb);
759 		aead_request_free(req);
760 		macsec_txsa_put(tx_sa);
761 		return ERR_PTR(-EINVAL);
762 	}
763 
764 	dev_put(skb->dev);
765 	aead_request_free(req);
766 	macsec_txsa_put(tx_sa);
767 
768 	return skb;
769 }
770 
771 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
772 {
773 	struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
774 	struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
775 	struct macsec_eth_header *hdr = macsec_ethhdr(skb);
776 	u32 lowest_pn = 0;
777 
778 	spin_lock(&rx_sa->lock);
779 	if (rx_sa->next_pn >= secy->replay_window)
780 		lowest_pn = rx_sa->next_pn - secy->replay_window;
781 
782 	/* Now perform replay protection check again
783 	 * (see IEEE 802.1AE-2006 figure 10-5)
784 	 */
785 	if (secy->replay_protect && pn < lowest_pn) {
786 		spin_unlock(&rx_sa->lock);
787 		u64_stats_update_begin(&rxsc_stats->syncp);
788 		rxsc_stats->stats.InPktsLate++;
789 		u64_stats_update_end(&rxsc_stats->syncp);
790 		return false;
791 	}
792 
793 	if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
794 		u64_stats_update_begin(&rxsc_stats->syncp);
795 		if (hdr->tci_an & MACSEC_TCI_E)
796 			rxsc_stats->stats.InOctetsDecrypted += skb->len;
797 		else
798 			rxsc_stats->stats.InOctetsValidated += skb->len;
799 		u64_stats_update_end(&rxsc_stats->syncp);
800 	}
801 
802 	if (!macsec_skb_cb(skb)->valid) {
803 		spin_unlock(&rx_sa->lock);
804 
805 		/* 10.6.5 */
806 		if (hdr->tci_an & MACSEC_TCI_C ||
807 		    secy->validate_frames == MACSEC_VALIDATE_STRICT) {
808 			u64_stats_update_begin(&rxsc_stats->syncp);
809 			rxsc_stats->stats.InPktsNotValid++;
810 			u64_stats_update_end(&rxsc_stats->syncp);
811 			return false;
812 		}
813 
814 		u64_stats_update_begin(&rxsc_stats->syncp);
815 		if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
816 			rxsc_stats->stats.InPktsInvalid++;
817 			this_cpu_inc(rx_sa->stats->InPktsInvalid);
818 		} else if (pn < lowest_pn) {
819 			rxsc_stats->stats.InPktsDelayed++;
820 		} else {
821 			rxsc_stats->stats.InPktsUnchecked++;
822 		}
823 		u64_stats_update_end(&rxsc_stats->syncp);
824 	} else {
825 		u64_stats_update_begin(&rxsc_stats->syncp);
826 		if (pn < lowest_pn) {
827 			rxsc_stats->stats.InPktsDelayed++;
828 		} else {
829 			rxsc_stats->stats.InPktsOK++;
830 			this_cpu_inc(rx_sa->stats->InPktsOK);
831 		}
832 		u64_stats_update_end(&rxsc_stats->syncp);
833 
834 		if (pn >= rx_sa->next_pn)
835 			rx_sa->next_pn = pn + 1;
836 		spin_unlock(&rx_sa->lock);
837 	}
838 
839 	return true;
840 }
841 
842 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
843 {
844 	skb->pkt_type = PACKET_HOST;
845 	skb->protocol = eth_type_trans(skb, dev);
846 
847 	skb_reset_network_header(skb);
848 	if (!skb_transport_header_was_set(skb))
849 		skb_reset_transport_header(skb);
850 	skb_reset_mac_len(skb);
851 }
852 
853 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
854 {
855 	memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
856 	skb_pull(skb, hdr_len);
857 	pskb_trim_unique(skb, skb->len - icv_len);
858 }
859 
860 static void count_rx(struct net_device *dev, int len)
861 {
862 	struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
863 
864 	u64_stats_update_begin(&stats->syncp);
865 	stats->rx_packets++;
866 	stats->rx_bytes += len;
867 	u64_stats_update_end(&stats->syncp);
868 }
869 
870 static void macsec_decrypt_done(struct crypto_async_request *base, int err)
871 {
872 	struct sk_buff *skb = base->data;
873 	struct net_device *dev = skb->dev;
874 	struct macsec_dev *macsec = macsec_priv(dev);
875 	struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
876 	struct macsec_rx_sc *rx_sc = rx_sa->sc;
877 	int len, ret;
878 	u32 pn;
879 
880 	aead_request_free(macsec_skb_cb(skb)->req);
881 
882 	if (!err)
883 		macsec_skb_cb(skb)->valid = true;
884 
885 	rcu_read_lock_bh();
886 	pn = ntohl(macsec_ethhdr(skb)->packet_number);
887 	if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
888 		rcu_read_unlock_bh();
889 		kfree_skb(skb);
890 		goto out;
891 	}
892 
893 	macsec_finalize_skb(skb, macsec->secy.icv_len,
894 			    macsec_extra_len(macsec_skb_cb(skb)->has_sci));
895 	macsec_reset_skb(skb, macsec->secy.netdev);
896 
897 	len = skb->len;
898 	ret = gro_cells_receive(&macsec->gro_cells, skb);
899 	if (ret == NET_RX_SUCCESS)
900 		count_rx(dev, len);
901 	else
902 		macsec->secy.netdev->stats.rx_dropped++;
903 
904 	rcu_read_unlock_bh();
905 
906 out:
907 	macsec_rxsa_put(rx_sa);
908 	macsec_rxsc_put(rx_sc);
909 	dev_put(dev);
910 }
911 
912 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
913 				      struct net_device *dev,
914 				      struct macsec_rx_sa *rx_sa,
915 				      sci_t sci,
916 				      struct macsec_secy *secy)
917 {
918 	int ret;
919 	struct scatterlist *sg;
920 	unsigned char *iv;
921 	struct aead_request *req;
922 	struct macsec_eth_header *hdr;
923 	u16 icv_len = secy->icv_len;
924 
925 	macsec_skb_cb(skb)->valid = false;
926 	skb = skb_share_check(skb, GFP_ATOMIC);
927 	if (!skb)
928 		return ERR_PTR(-ENOMEM);
929 
930 	req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg);
931 	if (!req) {
932 		kfree_skb(skb);
933 		return ERR_PTR(-ENOMEM);
934 	}
935 
936 	hdr = (struct macsec_eth_header *)skb->data;
937 	macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
938 
939 	sg_init_table(sg, MAX_SKB_FRAGS + 1);
940 	skb_to_sgvec(skb, sg, 0, skb->len);
941 
942 	if (hdr->tci_an & MACSEC_TCI_E) {
943 		/* confidentiality: ethernet + macsec header
944 		 * authenticated, encrypted payload
945 		 */
946 		int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
947 
948 		aead_request_set_crypt(req, sg, sg, len, iv);
949 		aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
950 		skb = skb_unshare(skb, GFP_ATOMIC);
951 		if (!skb) {
952 			aead_request_free(req);
953 			return ERR_PTR(-ENOMEM);
954 		}
955 	} else {
956 		/* integrity only: all headers + data authenticated */
957 		aead_request_set_crypt(req, sg, sg, icv_len, iv);
958 		aead_request_set_ad(req, skb->len - icv_len);
959 	}
960 
961 	macsec_skb_cb(skb)->req = req;
962 	skb->dev = dev;
963 	aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
964 
965 	dev_hold(dev);
966 	ret = crypto_aead_decrypt(req);
967 	if (ret == -EINPROGRESS) {
968 		return ERR_PTR(ret);
969 	} else if (ret != 0) {
970 		/* decryption/authentication failed
971 		 * 10.6 if validateFrames is disabled, deliver anyway
972 		 */
973 		if (ret != -EBADMSG) {
974 			kfree_skb(skb);
975 			skb = ERR_PTR(ret);
976 		}
977 	} else {
978 		macsec_skb_cb(skb)->valid = true;
979 	}
980 	dev_put(dev);
981 
982 	aead_request_free(req);
983 
984 	return skb;
985 }
986 
987 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
988 {
989 	struct macsec_rx_sc *rx_sc;
990 
991 	for_each_rxsc(secy, rx_sc) {
992 		if (rx_sc->sci == sci)
993 			return rx_sc;
994 	}
995 
996 	return NULL;
997 }
998 
999 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1000 {
1001 	struct macsec_rx_sc *rx_sc;
1002 
1003 	for_each_rxsc_rtnl(secy, rx_sc) {
1004 		if (rx_sc->sci == sci)
1005 			return rx_sc;
1006 	}
1007 
1008 	return NULL;
1009 }
1010 
1011 static void handle_not_macsec(struct sk_buff *skb)
1012 {
1013 	struct macsec_rxh_data *rxd;
1014 	struct macsec_dev *macsec;
1015 
1016 	rcu_read_lock();
1017 	rxd = macsec_data_rcu(skb->dev);
1018 
1019 	/* 10.6 If the management control validateFrames is not
1020 	 * Strict, frames without a SecTAG are received, counted, and
1021 	 * delivered to the Controlled Port
1022 	 */
1023 	list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1024 		struct sk_buff *nskb;
1025 		int ret;
1026 		struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1027 
1028 		if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1029 			u64_stats_update_begin(&secy_stats->syncp);
1030 			secy_stats->stats.InPktsNoTag++;
1031 			u64_stats_update_end(&secy_stats->syncp);
1032 			continue;
1033 		}
1034 
1035 		/* deliver on this port */
1036 		nskb = skb_clone(skb, GFP_ATOMIC);
1037 		if (!nskb)
1038 			break;
1039 
1040 		nskb->dev = macsec->secy.netdev;
1041 
1042 		ret = netif_rx(nskb);
1043 		if (ret == NET_RX_SUCCESS) {
1044 			u64_stats_update_begin(&secy_stats->syncp);
1045 			secy_stats->stats.InPktsUntagged++;
1046 			u64_stats_update_end(&secy_stats->syncp);
1047 		} else {
1048 			macsec->secy.netdev->stats.rx_dropped++;
1049 		}
1050 	}
1051 
1052 	rcu_read_unlock();
1053 }
1054 
1055 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1056 {
1057 	struct sk_buff *skb = *pskb;
1058 	struct net_device *dev = skb->dev;
1059 	struct macsec_eth_header *hdr;
1060 	struct macsec_secy *secy = NULL;
1061 	struct macsec_rx_sc *rx_sc;
1062 	struct macsec_rx_sa *rx_sa;
1063 	struct macsec_rxh_data *rxd;
1064 	struct macsec_dev *macsec;
1065 	sci_t sci;
1066 	u32 pn;
1067 	bool cbit;
1068 	struct pcpu_rx_sc_stats *rxsc_stats;
1069 	struct pcpu_secy_stats *secy_stats;
1070 	bool pulled_sci;
1071 	int ret;
1072 
1073 	if (skb_headroom(skb) < ETH_HLEN)
1074 		goto drop_direct;
1075 
1076 	hdr = macsec_ethhdr(skb);
1077 	if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1078 		handle_not_macsec(skb);
1079 
1080 		/* and deliver to the uncontrolled port */
1081 		return RX_HANDLER_PASS;
1082 	}
1083 
1084 	skb = skb_unshare(skb, GFP_ATOMIC);
1085 	if (!skb) {
1086 		*pskb = NULL;
1087 		return RX_HANDLER_CONSUMED;
1088 	}
1089 
1090 	pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1091 	if (!pulled_sci) {
1092 		if (!pskb_may_pull(skb, macsec_extra_len(false)))
1093 			goto drop_direct;
1094 	}
1095 
1096 	hdr = macsec_ethhdr(skb);
1097 
1098 	/* Frames with a SecTAG that has the TCI E bit set but the C
1099 	 * bit clear are discarded, as this reserved encoding is used
1100 	 * to identify frames with a SecTAG that are not to be
1101 	 * delivered to the Controlled Port.
1102 	 */
1103 	if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1104 		return RX_HANDLER_PASS;
1105 
1106 	/* now, pull the extra length */
1107 	if (hdr->tci_an & MACSEC_TCI_SC) {
1108 		if (!pulled_sci)
1109 			goto drop_direct;
1110 	}
1111 
1112 	/* ethernet header is part of crypto processing */
1113 	skb_push(skb, ETH_HLEN);
1114 
1115 	macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1116 	macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1117 	sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1118 
1119 	rcu_read_lock();
1120 	rxd = macsec_data_rcu(skb->dev);
1121 
1122 	list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1123 		struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1124 		sc = sc ? macsec_rxsc_get(sc) : NULL;
1125 
1126 		if (sc) {
1127 			secy = &macsec->secy;
1128 			rx_sc = sc;
1129 			break;
1130 		}
1131 	}
1132 
1133 	if (!secy)
1134 		goto nosci;
1135 
1136 	dev = secy->netdev;
1137 	macsec = macsec_priv(dev);
1138 	secy_stats = this_cpu_ptr(macsec->stats);
1139 	rxsc_stats = this_cpu_ptr(rx_sc->stats);
1140 
1141 	if (!macsec_validate_skb(skb, secy->icv_len)) {
1142 		u64_stats_update_begin(&secy_stats->syncp);
1143 		secy_stats->stats.InPktsBadTag++;
1144 		u64_stats_update_end(&secy_stats->syncp);
1145 		goto drop_nosa;
1146 	}
1147 
1148 	rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1149 	if (!rx_sa) {
1150 		/* 10.6.1 if the SA is not in use */
1151 
1152 		/* If validateFrames is Strict or the C bit in the
1153 		 * SecTAG is set, discard
1154 		 */
1155 		if (hdr->tci_an & MACSEC_TCI_C ||
1156 		    secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1157 			u64_stats_update_begin(&rxsc_stats->syncp);
1158 			rxsc_stats->stats.InPktsNotUsingSA++;
1159 			u64_stats_update_end(&rxsc_stats->syncp);
1160 			goto drop_nosa;
1161 		}
1162 
1163 		/* not Strict, the frame (with the SecTAG and ICV
1164 		 * removed) is delivered to the Controlled Port.
1165 		 */
1166 		u64_stats_update_begin(&rxsc_stats->syncp);
1167 		rxsc_stats->stats.InPktsUnusedSA++;
1168 		u64_stats_update_end(&rxsc_stats->syncp);
1169 		goto deliver;
1170 	}
1171 
1172 	/* First, PN check to avoid decrypting obviously wrong packets */
1173 	pn = ntohl(hdr->packet_number);
1174 	if (secy->replay_protect) {
1175 		bool late;
1176 
1177 		spin_lock(&rx_sa->lock);
1178 		late = rx_sa->next_pn >= secy->replay_window &&
1179 		       pn < (rx_sa->next_pn - secy->replay_window);
1180 		spin_unlock(&rx_sa->lock);
1181 
1182 		if (late) {
1183 			u64_stats_update_begin(&rxsc_stats->syncp);
1184 			rxsc_stats->stats.InPktsLate++;
1185 			u64_stats_update_end(&rxsc_stats->syncp);
1186 			goto drop;
1187 		}
1188 	}
1189 
1190 	macsec_skb_cb(skb)->rx_sa = rx_sa;
1191 
1192 	/* Disabled && !changed text => skip validation */
1193 	if (hdr->tci_an & MACSEC_TCI_C ||
1194 	    secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1195 		skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1196 
1197 	if (IS_ERR(skb)) {
1198 		/* the decrypt callback needs the reference */
1199 		if (PTR_ERR(skb) != -EINPROGRESS) {
1200 			macsec_rxsa_put(rx_sa);
1201 			macsec_rxsc_put(rx_sc);
1202 		}
1203 		rcu_read_unlock();
1204 		*pskb = NULL;
1205 		return RX_HANDLER_CONSUMED;
1206 	}
1207 
1208 	if (!macsec_post_decrypt(skb, secy, pn))
1209 		goto drop;
1210 
1211 deliver:
1212 	macsec_finalize_skb(skb, secy->icv_len,
1213 			    macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1214 	macsec_reset_skb(skb, secy->netdev);
1215 
1216 	if (rx_sa)
1217 		macsec_rxsa_put(rx_sa);
1218 	macsec_rxsc_put(rx_sc);
1219 
1220 	ret = gro_cells_receive(&macsec->gro_cells, skb);
1221 	if (ret == NET_RX_SUCCESS)
1222 		count_rx(dev, skb->len);
1223 	else
1224 		macsec->secy.netdev->stats.rx_dropped++;
1225 
1226 	rcu_read_unlock();
1227 
1228 	*pskb = NULL;
1229 	return RX_HANDLER_CONSUMED;
1230 
1231 drop:
1232 	macsec_rxsa_put(rx_sa);
1233 drop_nosa:
1234 	macsec_rxsc_put(rx_sc);
1235 	rcu_read_unlock();
1236 drop_direct:
1237 	kfree_skb(skb);
1238 	*pskb = NULL;
1239 	return RX_HANDLER_CONSUMED;
1240 
1241 nosci:
1242 	/* 10.6.1 if the SC is not found */
1243 	cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1244 	if (!cbit)
1245 		macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1246 				    macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1247 
1248 	list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1249 		struct sk_buff *nskb;
1250 
1251 		secy_stats = this_cpu_ptr(macsec->stats);
1252 
1253 		/* If validateFrames is Strict or the C bit in the
1254 		 * SecTAG is set, discard
1255 		 */
1256 		if (cbit ||
1257 		    macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1258 			u64_stats_update_begin(&secy_stats->syncp);
1259 			secy_stats->stats.InPktsNoSCI++;
1260 			u64_stats_update_end(&secy_stats->syncp);
1261 			continue;
1262 		}
1263 
1264 		/* not strict, the frame (with the SecTAG and ICV
1265 		 * removed) is delivered to the Controlled Port.
1266 		 */
1267 		nskb = skb_clone(skb, GFP_ATOMIC);
1268 		if (!nskb)
1269 			break;
1270 
1271 		macsec_reset_skb(nskb, macsec->secy.netdev);
1272 
1273 		ret = netif_rx(nskb);
1274 		if (ret == NET_RX_SUCCESS) {
1275 			u64_stats_update_begin(&secy_stats->syncp);
1276 			secy_stats->stats.InPktsUnknownSCI++;
1277 			u64_stats_update_end(&secy_stats->syncp);
1278 		} else {
1279 			macsec->secy.netdev->stats.rx_dropped++;
1280 		}
1281 	}
1282 
1283 	rcu_read_unlock();
1284 	*pskb = skb;
1285 	return RX_HANDLER_PASS;
1286 }
1287 
1288 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1289 {
1290 	struct crypto_aead *tfm;
1291 	int ret;
1292 
1293 	tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
1294 
1295 	if (IS_ERR(tfm))
1296 		return tfm;
1297 
1298 	ret = crypto_aead_setkey(tfm, key, key_len);
1299 	if (ret < 0)
1300 		goto fail;
1301 
1302 	ret = crypto_aead_setauthsize(tfm, icv_len);
1303 	if (ret < 0)
1304 		goto fail;
1305 
1306 	return tfm;
1307 fail:
1308 	crypto_free_aead(tfm);
1309 	return ERR_PTR(ret);
1310 }
1311 
1312 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1313 		      int icv_len)
1314 {
1315 	rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1316 	if (!rx_sa->stats)
1317 		return -ENOMEM;
1318 
1319 	rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1320 	if (IS_ERR(rx_sa->key.tfm)) {
1321 		free_percpu(rx_sa->stats);
1322 		return PTR_ERR(rx_sa->key.tfm);
1323 	}
1324 
1325 	rx_sa->active = false;
1326 	rx_sa->next_pn = 1;
1327 	atomic_set(&rx_sa->refcnt, 1);
1328 	spin_lock_init(&rx_sa->lock);
1329 
1330 	return 0;
1331 }
1332 
1333 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1334 {
1335 	rx_sa->active = false;
1336 
1337 	macsec_rxsa_put(rx_sa);
1338 }
1339 
1340 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1341 {
1342 	int i;
1343 
1344 	for (i = 0; i < MACSEC_NUM_AN; i++) {
1345 		struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1346 
1347 		RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1348 		if (sa)
1349 			clear_rx_sa(sa);
1350 	}
1351 
1352 	macsec_rxsc_put(rx_sc);
1353 }
1354 
1355 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1356 {
1357 	struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1358 
1359 	for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1360 	     rx_sc;
1361 	     rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1362 		if (rx_sc->sci == sci) {
1363 			if (rx_sc->active)
1364 				secy->n_rx_sc--;
1365 			rcu_assign_pointer(*rx_scp, rx_sc->next);
1366 			return rx_sc;
1367 		}
1368 	}
1369 
1370 	return NULL;
1371 }
1372 
1373 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1374 {
1375 	struct macsec_rx_sc *rx_sc;
1376 	struct macsec_dev *macsec;
1377 	struct net_device *real_dev = macsec_priv(dev)->real_dev;
1378 	struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1379 	struct macsec_secy *secy;
1380 
1381 	list_for_each_entry(macsec, &rxd->secys, secys) {
1382 		if (find_rx_sc_rtnl(&macsec->secy, sci))
1383 			return ERR_PTR(-EEXIST);
1384 	}
1385 
1386 	rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1387 	if (!rx_sc)
1388 		return ERR_PTR(-ENOMEM);
1389 
1390 	rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1391 	if (!rx_sc->stats) {
1392 		kfree(rx_sc);
1393 		return ERR_PTR(-ENOMEM);
1394 	}
1395 
1396 	rx_sc->sci = sci;
1397 	rx_sc->active = true;
1398 	atomic_set(&rx_sc->refcnt, 1);
1399 
1400 	secy = &macsec_priv(dev)->secy;
1401 	rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1402 	rcu_assign_pointer(secy->rx_sc, rx_sc);
1403 
1404 	if (rx_sc->active)
1405 		secy->n_rx_sc++;
1406 
1407 	return rx_sc;
1408 }
1409 
1410 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1411 		      int icv_len)
1412 {
1413 	tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1414 	if (!tx_sa->stats)
1415 		return -ENOMEM;
1416 
1417 	tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1418 	if (IS_ERR(tx_sa->key.tfm)) {
1419 		free_percpu(tx_sa->stats);
1420 		return PTR_ERR(tx_sa->key.tfm);
1421 	}
1422 
1423 	tx_sa->active = false;
1424 	atomic_set(&tx_sa->refcnt, 1);
1425 	spin_lock_init(&tx_sa->lock);
1426 
1427 	return 0;
1428 }
1429 
1430 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1431 {
1432 	tx_sa->active = false;
1433 
1434 	macsec_txsa_put(tx_sa);
1435 }
1436 
1437 static struct genl_family macsec_fam;
1438 
1439 static struct net_device *get_dev_from_nl(struct net *net,
1440 					  struct nlattr **attrs)
1441 {
1442 	int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1443 	struct net_device *dev;
1444 
1445 	dev = __dev_get_by_index(net, ifindex);
1446 	if (!dev)
1447 		return ERR_PTR(-ENODEV);
1448 
1449 	if (!netif_is_macsec(dev))
1450 		return ERR_PTR(-ENODEV);
1451 
1452 	return dev;
1453 }
1454 
1455 static sci_t nla_get_sci(const struct nlattr *nla)
1456 {
1457 	return (__force sci_t)nla_get_u64(nla);
1458 }
1459 
1460 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1461 		       int padattr)
1462 {
1463 	return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1464 }
1465 
1466 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1467 					     struct nlattr **attrs,
1468 					     struct nlattr **tb_sa,
1469 					     struct net_device **devp,
1470 					     struct macsec_secy **secyp,
1471 					     struct macsec_tx_sc **scp,
1472 					     u8 *assoc_num)
1473 {
1474 	struct net_device *dev;
1475 	struct macsec_secy *secy;
1476 	struct macsec_tx_sc *tx_sc;
1477 	struct macsec_tx_sa *tx_sa;
1478 
1479 	if (!tb_sa[MACSEC_SA_ATTR_AN])
1480 		return ERR_PTR(-EINVAL);
1481 
1482 	*assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1483 
1484 	dev = get_dev_from_nl(net, attrs);
1485 	if (IS_ERR(dev))
1486 		return ERR_CAST(dev);
1487 
1488 	if (*assoc_num >= MACSEC_NUM_AN)
1489 		return ERR_PTR(-EINVAL);
1490 
1491 	secy = &macsec_priv(dev)->secy;
1492 	tx_sc = &secy->tx_sc;
1493 
1494 	tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1495 	if (!tx_sa)
1496 		return ERR_PTR(-ENODEV);
1497 
1498 	*devp = dev;
1499 	*scp = tx_sc;
1500 	*secyp = secy;
1501 	return tx_sa;
1502 }
1503 
1504 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1505 					     struct nlattr **attrs,
1506 					     struct nlattr **tb_rxsc,
1507 					     struct net_device **devp,
1508 					     struct macsec_secy **secyp)
1509 {
1510 	struct net_device *dev;
1511 	struct macsec_secy *secy;
1512 	struct macsec_rx_sc *rx_sc;
1513 	sci_t sci;
1514 
1515 	dev = get_dev_from_nl(net, attrs);
1516 	if (IS_ERR(dev))
1517 		return ERR_CAST(dev);
1518 
1519 	secy = &macsec_priv(dev)->secy;
1520 
1521 	if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1522 		return ERR_PTR(-EINVAL);
1523 
1524 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1525 	rx_sc = find_rx_sc_rtnl(secy, sci);
1526 	if (!rx_sc)
1527 		return ERR_PTR(-ENODEV);
1528 
1529 	*secyp = secy;
1530 	*devp = dev;
1531 
1532 	return rx_sc;
1533 }
1534 
1535 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1536 					     struct nlattr **attrs,
1537 					     struct nlattr **tb_rxsc,
1538 					     struct nlattr **tb_sa,
1539 					     struct net_device **devp,
1540 					     struct macsec_secy **secyp,
1541 					     struct macsec_rx_sc **scp,
1542 					     u8 *assoc_num)
1543 {
1544 	struct macsec_rx_sc *rx_sc;
1545 	struct macsec_rx_sa *rx_sa;
1546 
1547 	if (!tb_sa[MACSEC_SA_ATTR_AN])
1548 		return ERR_PTR(-EINVAL);
1549 
1550 	*assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1551 	if (*assoc_num >= MACSEC_NUM_AN)
1552 		return ERR_PTR(-EINVAL);
1553 
1554 	rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1555 	if (IS_ERR(rx_sc))
1556 		return ERR_CAST(rx_sc);
1557 
1558 	rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1559 	if (!rx_sa)
1560 		return ERR_PTR(-ENODEV);
1561 
1562 	*scp = rx_sc;
1563 	return rx_sa;
1564 }
1565 
1566 
1567 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1568 	[MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1569 	[MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1570 	[MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1571 };
1572 
1573 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1574 	[MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1575 	[MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1576 };
1577 
1578 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1579 	[MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1580 	[MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1581 	[MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1582 	[MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1583 				   .len = MACSEC_KEYID_LEN, },
1584 	[MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1585 				 .len = MACSEC_MAX_KEY_LEN, },
1586 };
1587 
1588 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1589 {
1590 	if (!attrs[MACSEC_ATTR_SA_CONFIG])
1591 		return -EINVAL;
1592 
1593 	if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX,
1594 			     attrs[MACSEC_ATTR_SA_CONFIG],
1595 			     macsec_genl_sa_policy, NULL))
1596 		return -EINVAL;
1597 
1598 	return 0;
1599 }
1600 
1601 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1602 {
1603 	if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1604 		return -EINVAL;
1605 
1606 	if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX,
1607 			     attrs[MACSEC_ATTR_RXSC_CONFIG],
1608 			     macsec_genl_rxsc_policy, NULL))
1609 		return -EINVAL;
1610 
1611 	return 0;
1612 }
1613 
1614 static bool validate_add_rxsa(struct nlattr **attrs)
1615 {
1616 	if (!attrs[MACSEC_SA_ATTR_AN] ||
1617 	    !attrs[MACSEC_SA_ATTR_KEY] ||
1618 	    !attrs[MACSEC_SA_ATTR_KEYID])
1619 		return false;
1620 
1621 	if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1622 		return false;
1623 
1624 	if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1625 		return false;
1626 
1627 	if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1628 		if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1629 			return false;
1630 	}
1631 
1632 	if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1633 		return false;
1634 
1635 	return true;
1636 }
1637 
1638 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1639 {
1640 	struct net_device *dev;
1641 	struct nlattr **attrs = info->attrs;
1642 	struct macsec_secy *secy;
1643 	struct macsec_rx_sc *rx_sc;
1644 	struct macsec_rx_sa *rx_sa;
1645 	unsigned char assoc_num;
1646 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1647 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1648 	int err;
1649 
1650 	if (!attrs[MACSEC_ATTR_IFINDEX])
1651 		return -EINVAL;
1652 
1653 	if (parse_sa_config(attrs, tb_sa))
1654 		return -EINVAL;
1655 
1656 	if (parse_rxsc_config(attrs, tb_rxsc))
1657 		return -EINVAL;
1658 
1659 	if (!validate_add_rxsa(tb_sa))
1660 		return -EINVAL;
1661 
1662 	rtnl_lock();
1663 	rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1664 	if (IS_ERR(rx_sc)) {
1665 		rtnl_unlock();
1666 		return PTR_ERR(rx_sc);
1667 	}
1668 
1669 	assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1670 
1671 	if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1672 		pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1673 			  nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1674 		rtnl_unlock();
1675 		return -EINVAL;
1676 	}
1677 
1678 	rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1679 	if (rx_sa) {
1680 		rtnl_unlock();
1681 		return -EBUSY;
1682 	}
1683 
1684 	rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1685 	if (!rx_sa) {
1686 		rtnl_unlock();
1687 		return -ENOMEM;
1688 	}
1689 
1690 	err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1691 			 secy->key_len, secy->icv_len);
1692 	if (err < 0) {
1693 		kfree(rx_sa);
1694 		rtnl_unlock();
1695 		return err;
1696 	}
1697 
1698 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
1699 		spin_lock_bh(&rx_sa->lock);
1700 		rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1701 		spin_unlock_bh(&rx_sa->lock);
1702 	}
1703 
1704 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1705 		rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1706 
1707 	nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1708 	rx_sa->sc = rx_sc;
1709 	rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1710 
1711 	rtnl_unlock();
1712 
1713 	return 0;
1714 }
1715 
1716 static bool validate_add_rxsc(struct nlattr **attrs)
1717 {
1718 	if (!attrs[MACSEC_RXSC_ATTR_SCI])
1719 		return false;
1720 
1721 	if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1722 		if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1723 			return false;
1724 	}
1725 
1726 	return true;
1727 }
1728 
1729 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1730 {
1731 	struct net_device *dev;
1732 	sci_t sci = MACSEC_UNDEF_SCI;
1733 	struct nlattr **attrs = info->attrs;
1734 	struct macsec_rx_sc *rx_sc;
1735 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1736 
1737 	if (!attrs[MACSEC_ATTR_IFINDEX])
1738 		return -EINVAL;
1739 
1740 	if (parse_rxsc_config(attrs, tb_rxsc))
1741 		return -EINVAL;
1742 
1743 	if (!validate_add_rxsc(tb_rxsc))
1744 		return -EINVAL;
1745 
1746 	rtnl_lock();
1747 	dev = get_dev_from_nl(genl_info_net(info), attrs);
1748 	if (IS_ERR(dev)) {
1749 		rtnl_unlock();
1750 		return PTR_ERR(dev);
1751 	}
1752 
1753 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1754 
1755 	rx_sc = create_rx_sc(dev, sci);
1756 	if (IS_ERR(rx_sc)) {
1757 		rtnl_unlock();
1758 		return PTR_ERR(rx_sc);
1759 	}
1760 
1761 	if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1762 		rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1763 
1764 	rtnl_unlock();
1765 
1766 	return 0;
1767 }
1768 
1769 static bool validate_add_txsa(struct nlattr **attrs)
1770 {
1771 	if (!attrs[MACSEC_SA_ATTR_AN] ||
1772 	    !attrs[MACSEC_SA_ATTR_PN] ||
1773 	    !attrs[MACSEC_SA_ATTR_KEY] ||
1774 	    !attrs[MACSEC_SA_ATTR_KEYID])
1775 		return false;
1776 
1777 	if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1778 		return false;
1779 
1780 	if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1781 		return false;
1782 
1783 	if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1784 		if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1785 			return false;
1786 	}
1787 
1788 	if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1789 		return false;
1790 
1791 	return true;
1792 }
1793 
1794 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1795 {
1796 	struct net_device *dev;
1797 	struct nlattr **attrs = info->attrs;
1798 	struct macsec_secy *secy;
1799 	struct macsec_tx_sc *tx_sc;
1800 	struct macsec_tx_sa *tx_sa;
1801 	unsigned char assoc_num;
1802 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1803 	int err;
1804 
1805 	if (!attrs[MACSEC_ATTR_IFINDEX])
1806 		return -EINVAL;
1807 
1808 	if (parse_sa_config(attrs, tb_sa))
1809 		return -EINVAL;
1810 
1811 	if (!validate_add_txsa(tb_sa))
1812 		return -EINVAL;
1813 
1814 	rtnl_lock();
1815 	dev = get_dev_from_nl(genl_info_net(info), attrs);
1816 	if (IS_ERR(dev)) {
1817 		rtnl_unlock();
1818 		return PTR_ERR(dev);
1819 	}
1820 
1821 	secy = &macsec_priv(dev)->secy;
1822 	tx_sc = &secy->tx_sc;
1823 
1824 	assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1825 
1826 	if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1827 		pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1828 			  nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1829 		rtnl_unlock();
1830 		return -EINVAL;
1831 	}
1832 
1833 	tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1834 	if (tx_sa) {
1835 		rtnl_unlock();
1836 		return -EBUSY;
1837 	}
1838 
1839 	tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1840 	if (!tx_sa) {
1841 		rtnl_unlock();
1842 		return -ENOMEM;
1843 	}
1844 
1845 	err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1846 			 secy->key_len, secy->icv_len);
1847 	if (err < 0) {
1848 		kfree(tx_sa);
1849 		rtnl_unlock();
1850 		return err;
1851 	}
1852 
1853 	nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1854 
1855 	spin_lock_bh(&tx_sa->lock);
1856 	tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1857 	spin_unlock_bh(&tx_sa->lock);
1858 
1859 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1860 		tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1861 
1862 	if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1863 		secy->operational = true;
1864 
1865 	rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1866 
1867 	rtnl_unlock();
1868 
1869 	return 0;
1870 }
1871 
1872 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1873 {
1874 	struct nlattr **attrs = info->attrs;
1875 	struct net_device *dev;
1876 	struct macsec_secy *secy;
1877 	struct macsec_rx_sc *rx_sc;
1878 	struct macsec_rx_sa *rx_sa;
1879 	u8 assoc_num;
1880 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1881 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1882 
1883 	if (!attrs[MACSEC_ATTR_IFINDEX])
1884 		return -EINVAL;
1885 
1886 	if (parse_sa_config(attrs, tb_sa))
1887 		return -EINVAL;
1888 
1889 	if (parse_rxsc_config(attrs, tb_rxsc))
1890 		return -EINVAL;
1891 
1892 	rtnl_lock();
1893 	rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1894 				 &dev, &secy, &rx_sc, &assoc_num);
1895 	if (IS_ERR(rx_sa)) {
1896 		rtnl_unlock();
1897 		return PTR_ERR(rx_sa);
1898 	}
1899 
1900 	if (rx_sa->active) {
1901 		rtnl_unlock();
1902 		return -EBUSY;
1903 	}
1904 
1905 	RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1906 	clear_rx_sa(rx_sa);
1907 
1908 	rtnl_unlock();
1909 
1910 	return 0;
1911 }
1912 
1913 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1914 {
1915 	struct nlattr **attrs = info->attrs;
1916 	struct net_device *dev;
1917 	struct macsec_secy *secy;
1918 	struct macsec_rx_sc *rx_sc;
1919 	sci_t sci;
1920 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1921 
1922 	if (!attrs[MACSEC_ATTR_IFINDEX])
1923 		return -EINVAL;
1924 
1925 	if (parse_rxsc_config(attrs, tb_rxsc))
1926 		return -EINVAL;
1927 
1928 	if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1929 		return -EINVAL;
1930 
1931 	rtnl_lock();
1932 	dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1933 	if (IS_ERR(dev)) {
1934 		rtnl_unlock();
1935 		return PTR_ERR(dev);
1936 	}
1937 
1938 	secy = &macsec_priv(dev)->secy;
1939 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1940 
1941 	rx_sc = del_rx_sc(secy, sci);
1942 	if (!rx_sc) {
1943 		rtnl_unlock();
1944 		return -ENODEV;
1945 	}
1946 
1947 	free_rx_sc(rx_sc);
1948 	rtnl_unlock();
1949 
1950 	return 0;
1951 }
1952 
1953 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1954 {
1955 	struct nlattr **attrs = info->attrs;
1956 	struct net_device *dev;
1957 	struct macsec_secy *secy;
1958 	struct macsec_tx_sc *tx_sc;
1959 	struct macsec_tx_sa *tx_sa;
1960 	u8 assoc_num;
1961 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1962 
1963 	if (!attrs[MACSEC_ATTR_IFINDEX])
1964 		return -EINVAL;
1965 
1966 	if (parse_sa_config(attrs, tb_sa))
1967 		return -EINVAL;
1968 
1969 	rtnl_lock();
1970 	tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
1971 				 &dev, &secy, &tx_sc, &assoc_num);
1972 	if (IS_ERR(tx_sa)) {
1973 		rtnl_unlock();
1974 		return PTR_ERR(tx_sa);
1975 	}
1976 
1977 	if (tx_sa->active) {
1978 		rtnl_unlock();
1979 		return -EBUSY;
1980 	}
1981 
1982 	RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
1983 	clear_tx_sa(tx_sa);
1984 
1985 	rtnl_unlock();
1986 
1987 	return 0;
1988 }
1989 
1990 static bool validate_upd_sa(struct nlattr **attrs)
1991 {
1992 	if (!attrs[MACSEC_SA_ATTR_AN] ||
1993 	    attrs[MACSEC_SA_ATTR_KEY] ||
1994 	    attrs[MACSEC_SA_ATTR_KEYID])
1995 		return false;
1996 
1997 	if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1998 		return false;
1999 
2000 	if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2001 		return false;
2002 
2003 	if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2004 		if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2005 			return false;
2006 	}
2007 
2008 	return true;
2009 }
2010 
2011 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2012 {
2013 	struct nlattr **attrs = info->attrs;
2014 	struct net_device *dev;
2015 	struct macsec_secy *secy;
2016 	struct macsec_tx_sc *tx_sc;
2017 	struct macsec_tx_sa *tx_sa;
2018 	u8 assoc_num;
2019 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2020 
2021 	if (!attrs[MACSEC_ATTR_IFINDEX])
2022 		return -EINVAL;
2023 
2024 	if (parse_sa_config(attrs, tb_sa))
2025 		return -EINVAL;
2026 
2027 	if (!validate_upd_sa(tb_sa))
2028 		return -EINVAL;
2029 
2030 	rtnl_lock();
2031 	tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2032 				 &dev, &secy, &tx_sc, &assoc_num);
2033 	if (IS_ERR(tx_sa)) {
2034 		rtnl_unlock();
2035 		return PTR_ERR(tx_sa);
2036 	}
2037 
2038 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2039 		spin_lock_bh(&tx_sa->lock);
2040 		tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2041 		spin_unlock_bh(&tx_sa->lock);
2042 	}
2043 
2044 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2045 		tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2046 
2047 	if (assoc_num == tx_sc->encoding_sa)
2048 		secy->operational = tx_sa->active;
2049 
2050 	rtnl_unlock();
2051 
2052 	return 0;
2053 }
2054 
2055 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2056 {
2057 	struct nlattr **attrs = info->attrs;
2058 	struct net_device *dev;
2059 	struct macsec_secy *secy;
2060 	struct macsec_rx_sc *rx_sc;
2061 	struct macsec_rx_sa *rx_sa;
2062 	u8 assoc_num;
2063 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2064 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2065 
2066 	if (!attrs[MACSEC_ATTR_IFINDEX])
2067 		return -EINVAL;
2068 
2069 	if (parse_rxsc_config(attrs, tb_rxsc))
2070 		return -EINVAL;
2071 
2072 	if (parse_sa_config(attrs, tb_sa))
2073 		return -EINVAL;
2074 
2075 	if (!validate_upd_sa(tb_sa))
2076 		return -EINVAL;
2077 
2078 	rtnl_lock();
2079 	rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2080 				 &dev, &secy, &rx_sc, &assoc_num);
2081 	if (IS_ERR(rx_sa)) {
2082 		rtnl_unlock();
2083 		return PTR_ERR(rx_sa);
2084 	}
2085 
2086 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2087 		spin_lock_bh(&rx_sa->lock);
2088 		rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2089 		spin_unlock_bh(&rx_sa->lock);
2090 	}
2091 
2092 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2093 		rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2094 
2095 	rtnl_unlock();
2096 	return 0;
2097 }
2098 
2099 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2100 {
2101 	struct nlattr **attrs = info->attrs;
2102 	struct net_device *dev;
2103 	struct macsec_secy *secy;
2104 	struct macsec_rx_sc *rx_sc;
2105 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2106 
2107 	if (!attrs[MACSEC_ATTR_IFINDEX])
2108 		return -EINVAL;
2109 
2110 	if (parse_rxsc_config(attrs, tb_rxsc))
2111 		return -EINVAL;
2112 
2113 	if (!validate_add_rxsc(tb_rxsc))
2114 		return -EINVAL;
2115 
2116 	rtnl_lock();
2117 	rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2118 	if (IS_ERR(rx_sc)) {
2119 		rtnl_unlock();
2120 		return PTR_ERR(rx_sc);
2121 	}
2122 
2123 	if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2124 		bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2125 
2126 		if (rx_sc->active != new)
2127 			secy->n_rx_sc += new ? 1 : -1;
2128 
2129 		rx_sc->active = new;
2130 	}
2131 
2132 	rtnl_unlock();
2133 
2134 	return 0;
2135 }
2136 
2137 static int copy_tx_sa_stats(struct sk_buff *skb,
2138 			     struct macsec_tx_sa_stats __percpu *pstats)
2139 {
2140 	struct macsec_tx_sa_stats sum = {0, };
2141 	int cpu;
2142 
2143 	for_each_possible_cpu(cpu) {
2144 		const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2145 
2146 		sum.OutPktsProtected += stats->OutPktsProtected;
2147 		sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2148 	}
2149 
2150 	if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2151 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2152 		return -EMSGSIZE;
2153 
2154 	return 0;
2155 }
2156 
2157 static int copy_rx_sa_stats(struct sk_buff *skb,
2158 			     struct macsec_rx_sa_stats __percpu *pstats)
2159 {
2160 	struct macsec_rx_sa_stats sum = {0, };
2161 	int cpu;
2162 
2163 	for_each_possible_cpu(cpu) {
2164 		const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2165 
2166 		sum.InPktsOK         += stats->InPktsOK;
2167 		sum.InPktsInvalid    += stats->InPktsInvalid;
2168 		sum.InPktsNotValid   += stats->InPktsNotValid;
2169 		sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2170 		sum.InPktsUnusedSA   += stats->InPktsUnusedSA;
2171 	}
2172 
2173 	if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2174 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2175 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2176 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2177 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2178 		return -EMSGSIZE;
2179 
2180 	return 0;
2181 }
2182 
2183 static int copy_rx_sc_stats(struct sk_buff *skb,
2184 			     struct pcpu_rx_sc_stats __percpu *pstats)
2185 {
2186 	struct macsec_rx_sc_stats sum = {0, };
2187 	int cpu;
2188 
2189 	for_each_possible_cpu(cpu) {
2190 		const struct pcpu_rx_sc_stats *stats;
2191 		struct macsec_rx_sc_stats tmp;
2192 		unsigned int start;
2193 
2194 		stats = per_cpu_ptr(pstats, cpu);
2195 		do {
2196 			start = u64_stats_fetch_begin_irq(&stats->syncp);
2197 			memcpy(&tmp, &stats->stats, sizeof(tmp));
2198 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2199 
2200 		sum.InOctetsValidated += tmp.InOctetsValidated;
2201 		sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2202 		sum.InPktsUnchecked   += tmp.InPktsUnchecked;
2203 		sum.InPktsDelayed     += tmp.InPktsDelayed;
2204 		sum.InPktsOK          += tmp.InPktsOK;
2205 		sum.InPktsInvalid     += tmp.InPktsInvalid;
2206 		sum.InPktsLate        += tmp.InPktsLate;
2207 		sum.InPktsNotValid    += tmp.InPktsNotValid;
2208 		sum.InPktsNotUsingSA  += tmp.InPktsNotUsingSA;
2209 		sum.InPktsUnusedSA    += tmp.InPktsUnusedSA;
2210 	}
2211 
2212 	if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2213 			      sum.InOctetsValidated,
2214 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2215 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2216 			      sum.InOctetsDecrypted,
2217 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2218 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2219 			      sum.InPktsUnchecked,
2220 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2221 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2222 			      sum.InPktsDelayed,
2223 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2224 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2225 			      sum.InPktsOK,
2226 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2227 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2228 			      sum.InPktsInvalid,
2229 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2230 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2231 			      sum.InPktsLate,
2232 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2233 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2234 			      sum.InPktsNotValid,
2235 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2236 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2237 			      sum.InPktsNotUsingSA,
2238 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2239 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2240 			      sum.InPktsUnusedSA,
2241 			      MACSEC_RXSC_STATS_ATTR_PAD))
2242 		return -EMSGSIZE;
2243 
2244 	return 0;
2245 }
2246 
2247 static int copy_tx_sc_stats(struct sk_buff *skb,
2248 			     struct pcpu_tx_sc_stats __percpu *pstats)
2249 {
2250 	struct macsec_tx_sc_stats sum = {0, };
2251 	int cpu;
2252 
2253 	for_each_possible_cpu(cpu) {
2254 		const struct pcpu_tx_sc_stats *stats;
2255 		struct macsec_tx_sc_stats tmp;
2256 		unsigned int start;
2257 
2258 		stats = per_cpu_ptr(pstats, cpu);
2259 		do {
2260 			start = u64_stats_fetch_begin_irq(&stats->syncp);
2261 			memcpy(&tmp, &stats->stats, sizeof(tmp));
2262 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2263 
2264 		sum.OutPktsProtected   += tmp.OutPktsProtected;
2265 		sum.OutPktsEncrypted   += tmp.OutPktsEncrypted;
2266 		sum.OutOctetsProtected += tmp.OutOctetsProtected;
2267 		sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2268 	}
2269 
2270 	if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2271 			      sum.OutPktsProtected,
2272 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2273 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2274 			      sum.OutPktsEncrypted,
2275 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2276 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2277 			      sum.OutOctetsProtected,
2278 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2279 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2280 			      sum.OutOctetsEncrypted,
2281 			      MACSEC_TXSC_STATS_ATTR_PAD))
2282 		return -EMSGSIZE;
2283 
2284 	return 0;
2285 }
2286 
2287 static int copy_secy_stats(struct sk_buff *skb,
2288 			    struct pcpu_secy_stats __percpu *pstats)
2289 {
2290 	struct macsec_dev_stats sum = {0, };
2291 	int cpu;
2292 
2293 	for_each_possible_cpu(cpu) {
2294 		const struct pcpu_secy_stats *stats;
2295 		struct macsec_dev_stats tmp;
2296 		unsigned int start;
2297 
2298 		stats = per_cpu_ptr(pstats, cpu);
2299 		do {
2300 			start = u64_stats_fetch_begin_irq(&stats->syncp);
2301 			memcpy(&tmp, &stats->stats, sizeof(tmp));
2302 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2303 
2304 		sum.OutPktsUntagged  += tmp.OutPktsUntagged;
2305 		sum.InPktsUntagged   += tmp.InPktsUntagged;
2306 		sum.OutPktsTooLong   += tmp.OutPktsTooLong;
2307 		sum.InPktsNoTag      += tmp.InPktsNoTag;
2308 		sum.InPktsBadTag     += tmp.InPktsBadTag;
2309 		sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2310 		sum.InPktsNoSCI      += tmp.InPktsNoSCI;
2311 		sum.InPktsOverrun    += tmp.InPktsOverrun;
2312 	}
2313 
2314 	if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2315 			      sum.OutPktsUntagged,
2316 			      MACSEC_SECY_STATS_ATTR_PAD) ||
2317 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2318 			      sum.InPktsUntagged,
2319 			      MACSEC_SECY_STATS_ATTR_PAD) ||
2320 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2321 			      sum.OutPktsTooLong,
2322 			      MACSEC_SECY_STATS_ATTR_PAD) ||
2323 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2324 			      sum.InPktsNoTag,
2325 			      MACSEC_SECY_STATS_ATTR_PAD) ||
2326 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2327 			      sum.InPktsBadTag,
2328 			      MACSEC_SECY_STATS_ATTR_PAD) ||
2329 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2330 			      sum.InPktsUnknownSCI,
2331 			      MACSEC_SECY_STATS_ATTR_PAD) ||
2332 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2333 			      sum.InPktsNoSCI,
2334 			      MACSEC_SECY_STATS_ATTR_PAD) ||
2335 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2336 			      sum.InPktsOverrun,
2337 			      MACSEC_SECY_STATS_ATTR_PAD))
2338 		return -EMSGSIZE;
2339 
2340 	return 0;
2341 }
2342 
2343 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2344 {
2345 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2346 	struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
2347 
2348 	if (!secy_nest)
2349 		return 1;
2350 
2351 	if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2352 			MACSEC_SECY_ATTR_PAD) ||
2353 	    nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2354 			      MACSEC_DEFAULT_CIPHER_ID,
2355 			      MACSEC_SECY_ATTR_PAD) ||
2356 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2357 	    nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2358 	    nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2359 	    nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2360 	    nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2361 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2362 	    nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2363 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2364 	    nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2365 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2366 		goto cancel;
2367 
2368 	if (secy->replay_protect) {
2369 		if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2370 			goto cancel;
2371 	}
2372 
2373 	nla_nest_end(skb, secy_nest);
2374 	return 0;
2375 
2376 cancel:
2377 	nla_nest_cancel(skb, secy_nest);
2378 	return 1;
2379 }
2380 
2381 static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
2382 		     struct sk_buff *skb, struct netlink_callback *cb)
2383 {
2384 	struct macsec_rx_sc *rx_sc;
2385 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2386 	struct nlattr *txsa_list, *rxsc_list;
2387 	int i, j;
2388 	void *hdr;
2389 	struct nlattr *attr;
2390 
2391 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2392 			  &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2393 	if (!hdr)
2394 		return -EMSGSIZE;
2395 
2396 	genl_dump_check_consistent(cb, hdr, &macsec_fam);
2397 
2398 	if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2399 		goto nla_put_failure;
2400 
2401 	if (nla_put_secy(secy, skb))
2402 		goto nla_put_failure;
2403 
2404 	attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
2405 	if (!attr)
2406 		goto nla_put_failure;
2407 	if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2408 		nla_nest_cancel(skb, attr);
2409 		goto nla_put_failure;
2410 	}
2411 	nla_nest_end(skb, attr);
2412 
2413 	attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
2414 	if (!attr)
2415 		goto nla_put_failure;
2416 	if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2417 		nla_nest_cancel(skb, attr);
2418 		goto nla_put_failure;
2419 	}
2420 	nla_nest_end(skb, attr);
2421 
2422 	txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
2423 	if (!txsa_list)
2424 		goto nla_put_failure;
2425 	for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2426 		struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2427 		struct nlattr *txsa_nest;
2428 
2429 		if (!tx_sa)
2430 			continue;
2431 
2432 		txsa_nest = nla_nest_start(skb, j++);
2433 		if (!txsa_nest) {
2434 			nla_nest_cancel(skb, txsa_list);
2435 			goto nla_put_failure;
2436 		}
2437 
2438 		if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2439 		    nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2440 		    nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2441 		    nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2442 			nla_nest_cancel(skb, txsa_nest);
2443 			nla_nest_cancel(skb, txsa_list);
2444 			goto nla_put_failure;
2445 		}
2446 
2447 		attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2448 		if (!attr) {
2449 			nla_nest_cancel(skb, txsa_nest);
2450 			nla_nest_cancel(skb, txsa_list);
2451 			goto nla_put_failure;
2452 		}
2453 		if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2454 			nla_nest_cancel(skb, attr);
2455 			nla_nest_cancel(skb, txsa_nest);
2456 			nla_nest_cancel(skb, txsa_list);
2457 			goto nla_put_failure;
2458 		}
2459 		nla_nest_end(skb, attr);
2460 
2461 		nla_nest_end(skb, txsa_nest);
2462 	}
2463 	nla_nest_end(skb, txsa_list);
2464 
2465 	rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
2466 	if (!rxsc_list)
2467 		goto nla_put_failure;
2468 
2469 	j = 1;
2470 	for_each_rxsc_rtnl(secy, rx_sc) {
2471 		int k;
2472 		struct nlattr *rxsa_list;
2473 		struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
2474 
2475 		if (!rxsc_nest) {
2476 			nla_nest_cancel(skb, rxsc_list);
2477 			goto nla_put_failure;
2478 		}
2479 
2480 		if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2481 		    nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2482 				MACSEC_RXSC_ATTR_PAD)) {
2483 			nla_nest_cancel(skb, rxsc_nest);
2484 			nla_nest_cancel(skb, rxsc_list);
2485 			goto nla_put_failure;
2486 		}
2487 
2488 		attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
2489 		if (!attr) {
2490 			nla_nest_cancel(skb, rxsc_nest);
2491 			nla_nest_cancel(skb, rxsc_list);
2492 			goto nla_put_failure;
2493 		}
2494 		if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2495 			nla_nest_cancel(skb, attr);
2496 			nla_nest_cancel(skb, rxsc_nest);
2497 			nla_nest_cancel(skb, rxsc_list);
2498 			goto nla_put_failure;
2499 		}
2500 		nla_nest_end(skb, attr);
2501 
2502 		rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
2503 		if (!rxsa_list) {
2504 			nla_nest_cancel(skb, rxsc_nest);
2505 			nla_nest_cancel(skb, rxsc_list);
2506 			goto nla_put_failure;
2507 		}
2508 
2509 		for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2510 			struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2511 			struct nlattr *rxsa_nest;
2512 
2513 			if (!rx_sa)
2514 				continue;
2515 
2516 			rxsa_nest = nla_nest_start(skb, k++);
2517 			if (!rxsa_nest) {
2518 				nla_nest_cancel(skb, rxsa_list);
2519 				nla_nest_cancel(skb, rxsc_nest);
2520 				nla_nest_cancel(skb, rxsc_list);
2521 				goto nla_put_failure;
2522 			}
2523 
2524 			attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2525 			if (!attr) {
2526 				nla_nest_cancel(skb, rxsa_list);
2527 				nla_nest_cancel(skb, rxsc_nest);
2528 				nla_nest_cancel(skb, rxsc_list);
2529 				goto nla_put_failure;
2530 			}
2531 			if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2532 				nla_nest_cancel(skb, attr);
2533 				nla_nest_cancel(skb, rxsa_list);
2534 				nla_nest_cancel(skb, rxsc_nest);
2535 				nla_nest_cancel(skb, rxsc_list);
2536 				goto nla_put_failure;
2537 			}
2538 			nla_nest_end(skb, attr);
2539 
2540 			if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2541 			    nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2542 			    nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2543 			    nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2544 				nla_nest_cancel(skb, rxsa_nest);
2545 				nla_nest_cancel(skb, rxsc_nest);
2546 				nla_nest_cancel(skb, rxsc_list);
2547 				goto nla_put_failure;
2548 			}
2549 			nla_nest_end(skb, rxsa_nest);
2550 		}
2551 
2552 		nla_nest_end(skb, rxsa_list);
2553 		nla_nest_end(skb, rxsc_nest);
2554 	}
2555 
2556 	nla_nest_end(skb, rxsc_list);
2557 
2558 	genlmsg_end(skb, hdr);
2559 
2560 	return 0;
2561 
2562 nla_put_failure:
2563 	genlmsg_cancel(skb, hdr);
2564 	return -EMSGSIZE;
2565 }
2566 
2567 static int macsec_generation = 1; /* protected by RTNL */
2568 
2569 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2570 {
2571 	struct net *net = sock_net(skb->sk);
2572 	struct net_device *dev;
2573 	int dev_idx, d;
2574 
2575 	dev_idx = cb->args[0];
2576 
2577 	d = 0;
2578 	rtnl_lock();
2579 
2580 	cb->seq = macsec_generation;
2581 
2582 	for_each_netdev(net, dev) {
2583 		struct macsec_secy *secy;
2584 
2585 		if (d < dev_idx)
2586 			goto next;
2587 
2588 		if (!netif_is_macsec(dev))
2589 			goto next;
2590 
2591 		secy = &macsec_priv(dev)->secy;
2592 		if (dump_secy(secy, dev, skb, cb) < 0)
2593 			goto done;
2594 next:
2595 		d++;
2596 	}
2597 
2598 done:
2599 	rtnl_unlock();
2600 	cb->args[0] = d;
2601 	return skb->len;
2602 }
2603 
2604 static const struct genl_ops macsec_genl_ops[] = {
2605 	{
2606 		.cmd = MACSEC_CMD_GET_TXSC,
2607 		.dumpit = macsec_dump_txsc,
2608 		.policy = macsec_genl_policy,
2609 	},
2610 	{
2611 		.cmd = MACSEC_CMD_ADD_RXSC,
2612 		.doit = macsec_add_rxsc,
2613 		.policy = macsec_genl_policy,
2614 		.flags = GENL_ADMIN_PERM,
2615 	},
2616 	{
2617 		.cmd = MACSEC_CMD_DEL_RXSC,
2618 		.doit = macsec_del_rxsc,
2619 		.policy = macsec_genl_policy,
2620 		.flags = GENL_ADMIN_PERM,
2621 	},
2622 	{
2623 		.cmd = MACSEC_CMD_UPD_RXSC,
2624 		.doit = macsec_upd_rxsc,
2625 		.policy = macsec_genl_policy,
2626 		.flags = GENL_ADMIN_PERM,
2627 	},
2628 	{
2629 		.cmd = MACSEC_CMD_ADD_TXSA,
2630 		.doit = macsec_add_txsa,
2631 		.policy = macsec_genl_policy,
2632 		.flags = GENL_ADMIN_PERM,
2633 	},
2634 	{
2635 		.cmd = MACSEC_CMD_DEL_TXSA,
2636 		.doit = macsec_del_txsa,
2637 		.policy = macsec_genl_policy,
2638 		.flags = GENL_ADMIN_PERM,
2639 	},
2640 	{
2641 		.cmd = MACSEC_CMD_UPD_TXSA,
2642 		.doit = macsec_upd_txsa,
2643 		.policy = macsec_genl_policy,
2644 		.flags = GENL_ADMIN_PERM,
2645 	},
2646 	{
2647 		.cmd = MACSEC_CMD_ADD_RXSA,
2648 		.doit = macsec_add_rxsa,
2649 		.policy = macsec_genl_policy,
2650 		.flags = GENL_ADMIN_PERM,
2651 	},
2652 	{
2653 		.cmd = MACSEC_CMD_DEL_RXSA,
2654 		.doit = macsec_del_rxsa,
2655 		.policy = macsec_genl_policy,
2656 		.flags = GENL_ADMIN_PERM,
2657 	},
2658 	{
2659 		.cmd = MACSEC_CMD_UPD_RXSA,
2660 		.doit = macsec_upd_rxsa,
2661 		.policy = macsec_genl_policy,
2662 		.flags = GENL_ADMIN_PERM,
2663 	},
2664 };
2665 
2666 static struct genl_family macsec_fam __ro_after_init = {
2667 	.name		= MACSEC_GENL_NAME,
2668 	.hdrsize	= 0,
2669 	.version	= MACSEC_GENL_VERSION,
2670 	.maxattr	= MACSEC_ATTR_MAX,
2671 	.netnsok	= true,
2672 	.module		= THIS_MODULE,
2673 	.ops		= macsec_genl_ops,
2674 	.n_ops		= ARRAY_SIZE(macsec_genl_ops),
2675 };
2676 
2677 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2678 				     struct net_device *dev)
2679 {
2680 	struct macsec_dev *macsec = netdev_priv(dev);
2681 	struct macsec_secy *secy = &macsec->secy;
2682 	struct pcpu_secy_stats *secy_stats;
2683 	int ret, len;
2684 
2685 	/* 10.5 */
2686 	if (!secy->protect_frames) {
2687 		secy_stats = this_cpu_ptr(macsec->stats);
2688 		u64_stats_update_begin(&secy_stats->syncp);
2689 		secy_stats->stats.OutPktsUntagged++;
2690 		u64_stats_update_end(&secy_stats->syncp);
2691 		skb->dev = macsec->real_dev;
2692 		len = skb->len;
2693 		ret = dev_queue_xmit(skb);
2694 		count_tx(dev, ret, len);
2695 		return ret;
2696 	}
2697 
2698 	if (!secy->operational) {
2699 		kfree_skb(skb);
2700 		dev->stats.tx_dropped++;
2701 		return NETDEV_TX_OK;
2702 	}
2703 
2704 	skb = macsec_encrypt(skb, dev);
2705 	if (IS_ERR(skb)) {
2706 		if (PTR_ERR(skb) != -EINPROGRESS)
2707 			dev->stats.tx_dropped++;
2708 		return NETDEV_TX_OK;
2709 	}
2710 
2711 	macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2712 
2713 	macsec_encrypt_finish(skb, dev);
2714 	len = skb->len;
2715 	ret = dev_queue_xmit(skb);
2716 	count_tx(dev, ret, len);
2717 	return ret;
2718 }
2719 
2720 #define MACSEC_FEATURES \
2721 	(NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
2722 static struct lock_class_key macsec_netdev_addr_lock_key;
2723 
2724 static int macsec_dev_init(struct net_device *dev)
2725 {
2726 	struct macsec_dev *macsec = macsec_priv(dev);
2727 	struct net_device *real_dev = macsec->real_dev;
2728 	int err;
2729 
2730 	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2731 	if (!dev->tstats)
2732 		return -ENOMEM;
2733 
2734 	err = gro_cells_init(&macsec->gro_cells, dev);
2735 	if (err) {
2736 		free_percpu(dev->tstats);
2737 		return err;
2738 	}
2739 
2740 	dev->features = real_dev->features & MACSEC_FEATURES;
2741 	dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2742 
2743 	dev->needed_headroom = real_dev->needed_headroom +
2744 			       MACSEC_NEEDED_HEADROOM;
2745 	dev->needed_tailroom = real_dev->needed_tailroom +
2746 			       MACSEC_NEEDED_TAILROOM;
2747 
2748 	if (is_zero_ether_addr(dev->dev_addr))
2749 		eth_hw_addr_inherit(dev, real_dev);
2750 	if (is_zero_ether_addr(dev->broadcast))
2751 		memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2752 
2753 	return 0;
2754 }
2755 
2756 static void macsec_dev_uninit(struct net_device *dev)
2757 {
2758 	struct macsec_dev *macsec = macsec_priv(dev);
2759 
2760 	gro_cells_destroy(&macsec->gro_cells);
2761 	free_percpu(dev->tstats);
2762 }
2763 
2764 static netdev_features_t macsec_fix_features(struct net_device *dev,
2765 					     netdev_features_t features)
2766 {
2767 	struct macsec_dev *macsec = macsec_priv(dev);
2768 	struct net_device *real_dev = macsec->real_dev;
2769 
2770 	features &= (real_dev->features & MACSEC_FEATURES) |
2771 		    NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2772 	features |= NETIF_F_LLTX;
2773 
2774 	return features;
2775 }
2776 
2777 static int macsec_dev_open(struct net_device *dev)
2778 {
2779 	struct macsec_dev *macsec = macsec_priv(dev);
2780 	struct net_device *real_dev = macsec->real_dev;
2781 	int err;
2782 
2783 	if (!(real_dev->flags & IFF_UP))
2784 		return -ENETDOWN;
2785 
2786 	err = dev_uc_add(real_dev, dev->dev_addr);
2787 	if (err < 0)
2788 		return err;
2789 
2790 	if (dev->flags & IFF_ALLMULTI) {
2791 		err = dev_set_allmulti(real_dev, 1);
2792 		if (err < 0)
2793 			goto del_unicast;
2794 	}
2795 
2796 	if (dev->flags & IFF_PROMISC) {
2797 		err = dev_set_promiscuity(real_dev, 1);
2798 		if (err < 0)
2799 			goto clear_allmulti;
2800 	}
2801 
2802 	if (netif_carrier_ok(real_dev))
2803 		netif_carrier_on(dev);
2804 
2805 	return 0;
2806 clear_allmulti:
2807 	if (dev->flags & IFF_ALLMULTI)
2808 		dev_set_allmulti(real_dev, -1);
2809 del_unicast:
2810 	dev_uc_del(real_dev, dev->dev_addr);
2811 	netif_carrier_off(dev);
2812 	return err;
2813 }
2814 
2815 static int macsec_dev_stop(struct net_device *dev)
2816 {
2817 	struct macsec_dev *macsec = macsec_priv(dev);
2818 	struct net_device *real_dev = macsec->real_dev;
2819 
2820 	netif_carrier_off(dev);
2821 
2822 	dev_mc_unsync(real_dev, dev);
2823 	dev_uc_unsync(real_dev, dev);
2824 
2825 	if (dev->flags & IFF_ALLMULTI)
2826 		dev_set_allmulti(real_dev, -1);
2827 
2828 	if (dev->flags & IFF_PROMISC)
2829 		dev_set_promiscuity(real_dev, -1);
2830 
2831 	dev_uc_del(real_dev, dev->dev_addr);
2832 
2833 	return 0;
2834 }
2835 
2836 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2837 {
2838 	struct net_device *real_dev = macsec_priv(dev)->real_dev;
2839 
2840 	if (!(dev->flags & IFF_UP))
2841 		return;
2842 
2843 	if (change & IFF_ALLMULTI)
2844 		dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2845 
2846 	if (change & IFF_PROMISC)
2847 		dev_set_promiscuity(real_dev,
2848 				    dev->flags & IFF_PROMISC ? 1 : -1);
2849 }
2850 
2851 static void macsec_dev_set_rx_mode(struct net_device *dev)
2852 {
2853 	struct net_device *real_dev = macsec_priv(dev)->real_dev;
2854 
2855 	dev_mc_sync(real_dev, dev);
2856 	dev_uc_sync(real_dev, dev);
2857 }
2858 
2859 static int macsec_set_mac_address(struct net_device *dev, void *p)
2860 {
2861 	struct macsec_dev *macsec = macsec_priv(dev);
2862 	struct net_device *real_dev = macsec->real_dev;
2863 	struct sockaddr *addr = p;
2864 	int err;
2865 
2866 	if (!is_valid_ether_addr(addr->sa_data))
2867 		return -EADDRNOTAVAIL;
2868 
2869 	if (!(dev->flags & IFF_UP))
2870 		goto out;
2871 
2872 	err = dev_uc_add(real_dev, addr->sa_data);
2873 	if (err < 0)
2874 		return err;
2875 
2876 	dev_uc_del(real_dev, dev->dev_addr);
2877 
2878 out:
2879 	ether_addr_copy(dev->dev_addr, addr->sa_data);
2880 	return 0;
2881 }
2882 
2883 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2884 {
2885 	struct macsec_dev *macsec = macsec_priv(dev);
2886 	unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2887 
2888 	if (macsec->real_dev->mtu - extra < new_mtu)
2889 		return -ERANGE;
2890 
2891 	dev->mtu = new_mtu;
2892 
2893 	return 0;
2894 }
2895 
2896 static void macsec_get_stats64(struct net_device *dev,
2897 			       struct rtnl_link_stats64 *s)
2898 {
2899 	int cpu;
2900 
2901 	if (!dev->tstats)
2902 		return;
2903 
2904 	for_each_possible_cpu(cpu) {
2905 		struct pcpu_sw_netstats *stats;
2906 		struct pcpu_sw_netstats tmp;
2907 		int start;
2908 
2909 		stats = per_cpu_ptr(dev->tstats, cpu);
2910 		do {
2911 			start = u64_stats_fetch_begin_irq(&stats->syncp);
2912 			tmp.rx_packets = stats->rx_packets;
2913 			tmp.rx_bytes   = stats->rx_bytes;
2914 			tmp.tx_packets = stats->tx_packets;
2915 			tmp.tx_bytes   = stats->tx_bytes;
2916 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2917 
2918 		s->rx_packets += tmp.rx_packets;
2919 		s->rx_bytes   += tmp.rx_bytes;
2920 		s->tx_packets += tmp.tx_packets;
2921 		s->tx_bytes   += tmp.tx_bytes;
2922 	}
2923 
2924 	s->rx_dropped = dev->stats.rx_dropped;
2925 	s->tx_dropped = dev->stats.tx_dropped;
2926 }
2927 
2928 static int macsec_get_iflink(const struct net_device *dev)
2929 {
2930 	return macsec_priv(dev)->real_dev->ifindex;
2931 }
2932 
2933 
2934 static int macsec_get_nest_level(struct net_device *dev)
2935 {
2936 	return macsec_priv(dev)->nest_level;
2937 }
2938 
2939 
2940 static const struct net_device_ops macsec_netdev_ops = {
2941 	.ndo_init		= macsec_dev_init,
2942 	.ndo_uninit		= macsec_dev_uninit,
2943 	.ndo_open		= macsec_dev_open,
2944 	.ndo_stop		= macsec_dev_stop,
2945 	.ndo_fix_features	= macsec_fix_features,
2946 	.ndo_change_mtu		= macsec_change_mtu,
2947 	.ndo_set_rx_mode	= macsec_dev_set_rx_mode,
2948 	.ndo_change_rx_flags	= macsec_dev_change_rx_flags,
2949 	.ndo_set_mac_address	= macsec_set_mac_address,
2950 	.ndo_start_xmit		= macsec_start_xmit,
2951 	.ndo_get_stats64	= macsec_get_stats64,
2952 	.ndo_get_iflink		= macsec_get_iflink,
2953 	.ndo_get_lock_subclass  = macsec_get_nest_level,
2954 };
2955 
2956 static const struct device_type macsec_type = {
2957 	.name = "macsec",
2958 };
2959 
2960 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2961 	[IFLA_MACSEC_SCI] = { .type = NLA_U64 },
2962 	[IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2963 	[IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2964 	[IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2965 	[IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2966 	[IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2967 	[IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2968 	[IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2969 	[IFLA_MACSEC_ES] = { .type = NLA_U8 },
2970 	[IFLA_MACSEC_SCB] = { .type = NLA_U8 },
2971 	[IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
2972 	[IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
2973 };
2974 
2975 static void macsec_free_netdev(struct net_device *dev)
2976 {
2977 	struct macsec_dev *macsec = macsec_priv(dev);
2978 	struct net_device *real_dev = macsec->real_dev;
2979 
2980 	free_percpu(macsec->stats);
2981 	free_percpu(macsec->secy.tx_sc.stats);
2982 
2983 	dev_put(real_dev);
2984 	free_netdev(dev);
2985 }
2986 
2987 static void macsec_setup(struct net_device *dev)
2988 {
2989 	ether_setup(dev);
2990 	dev->min_mtu = 0;
2991 	dev->max_mtu = ETH_MAX_MTU;
2992 	dev->priv_flags |= IFF_NO_QUEUE;
2993 	dev->netdev_ops = &macsec_netdev_ops;
2994 	dev->destructor = macsec_free_netdev;
2995 	SET_NETDEV_DEVTYPE(dev, &macsec_type);
2996 
2997 	eth_zero_addr(dev->broadcast);
2998 }
2999 
3000 static void macsec_changelink_common(struct net_device *dev,
3001 				     struct nlattr *data[])
3002 {
3003 	struct macsec_secy *secy;
3004 	struct macsec_tx_sc *tx_sc;
3005 
3006 	secy = &macsec_priv(dev)->secy;
3007 	tx_sc = &secy->tx_sc;
3008 
3009 	if (data[IFLA_MACSEC_ENCODING_SA]) {
3010 		struct macsec_tx_sa *tx_sa;
3011 
3012 		tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3013 		tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3014 
3015 		secy->operational = tx_sa && tx_sa->active;
3016 	}
3017 
3018 	if (data[IFLA_MACSEC_WINDOW])
3019 		secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3020 
3021 	if (data[IFLA_MACSEC_ENCRYPT])
3022 		tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3023 
3024 	if (data[IFLA_MACSEC_PROTECT])
3025 		secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3026 
3027 	if (data[IFLA_MACSEC_INC_SCI])
3028 		tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3029 
3030 	if (data[IFLA_MACSEC_ES])
3031 		tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3032 
3033 	if (data[IFLA_MACSEC_SCB])
3034 		tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3035 
3036 	if (data[IFLA_MACSEC_REPLAY_PROTECT])
3037 		secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3038 
3039 	if (data[IFLA_MACSEC_VALIDATION])
3040 		secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3041 }
3042 
3043 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3044 			     struct nlattr *data[])
3045 {
3046 	if (!data)
3047 		return 0;
3048 
3049 	if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3050 	    data[IFLA_MACSEC_ICV_LEN] ||
3051 	    data[IFLA_MACSEC_SCI] ||
3052 	    data[IFLA_MACSEC_PORT])
3053 		return -EINVAL;
3054 
3055 	macsec_changelink_common(dev, data);
3056 
3057 	return 0;
3058 }
3059 
3060 static void macsec_del_dev(struct macsec_dev *macsec)
3061 {
3062 	int i;
3063 
3064 	while (macsec->secy.rx_sc) {
3065 		struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3066 
3067 		rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3068 		free_rx_sc(rx_sc);
3069 	}
3070 
3071 	for (i = 0; i < MACSEC_NUM_AN; i++) {
3072 		struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3073 
3074 		if (sa) {
3075 			RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3076 			clear_tx_sa(sa);
3077 		}
3078 	}
3079 }
3080 
3081 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3082 {
3083 	struct macsec_dev *macsec = macsec_priv(dev);
3084 	struct net_device *real_dev = macsec->real_dev;
3085 
3086 	unregister_netdevice_queue(dev, head);
3087 	list_del_rcu(&macsec->secys);
3088 	macsec_del_dev(macsec);
3089 	netdev_upper_dev_unlink(real_dev, dev);
3090 
3091 	macsec_generation++;
3092 }
3093 
3094 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3095 {
3096 	struct macsec_dev *macsec = macsec_priv(dev);
3097 	struct net_device *real_dev = macsec->real_dev;
3098 	struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3099 
3100 	macsec_common_dellink(dev, head);
3101 
3102 	if (list_empty(&rxd->secys)) {
3103 		netdev_rx_handler_unregister(real_dev);
3104 		kfree(rxd);
3105 	}
3106 }
3107 
3108 static int register_macsec_dev(struct net_device *real_dev,
3109 			       struct net_device *dev)
3110 {
3111 	struct macsec_dev *macsec = macsec_priv(dev);
3112 	struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3113 
3114 	if (!rxd) {
3115 		int err;
3116 
3117 		rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3118 		if (!rxd)
3119 			return -ENOMEM;
3120 
3121 		INIT_LIST_HEAD(&rxd->secys);
3122 
3123 		err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3124 						 rxd);
3125 		if (err < 0) {
3126 			kfree(rxd);
3127 			return err;
3128 		}
3129 	}
3130 
3131 	list_add_tail_rcu(&macsec->secys, &rxd->secys);
3132 	return 0;
3133 }
3134 
3135 static bool sci_exists(struct net_device *dev, sci_t sci)
3136 {
3137 	struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3138 	struct macsec_dev *macsec;
3139 
3140 	list_for_each_entry(macsec, &rxd->secys, secys) {
3141 		if (macsec->secy.sci == sci)
3142 			return true;
3143 	}
3144 
3145 	return false;
3146 }
3147 
3148 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
3149 {
3150 	return make_sci(dev->dev_addr, port);
3151 }
3152 
3153 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3154 {
3155 	struct macsec_dev *macsec = macsec_priv(dev);
3156 	struct macsec_secy *secy = &macsec->secy;
3157 
3158 	macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3159 	if (!macsec->stats)
3160 		return -ENOMEM;
3161 
3162 	secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3163 	if (!secy->tx_sc.stats) {
3164 		free_percpu(macsec->stats);
3165 		return -ENOMEM;
3166 	}
3167 
3168 	if (sci == MACSEC_UNDEF_SCI)
3169 		sci = dev_to_sci(dev, MACSEC_PORT_ES);
3170 
3171 	secy->netdev = dev;
3172 	secy->operational = true;
3173 	secy->key_len = DEFAULT_SAK_LEN;
3174 	secy->icv_len = icv_len;
3175 	secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3176 	secy->protect_frames = true;
3177 	secy->replay_protect = false;
3178 
3179 	secy->sci = sci;
3180 	secy->tx_sc.active = true;
3181 	secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3182 	secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3183 	secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3184 	secy->tx_sc.end_station = false;
3185 	secy->tx_sc.scb = false;
3186 
3187 	return 0;
3188 }
3189 
3190 static int macsec_newlink(struct net *net, struct net_device *dev,
3191 			  struct nlattr *tb[], struct nlattr *data[])
3192 {
3193 	struct macsec_dev *macsec = macsec_priv(dev);
3194 	struct net_device *real_dev;
3195 	int err;
3196 	sci_t sci;
3197 	u8 icv_len = DEFAULT_ICV_LEN;
3198 	rx_handler_func_t *rx_handler;
3199 
3200 	if (!tb[IFLA_LINK])
3201 		return -EINVAL;
3202 	real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3203 	if (!real_dev)
3204 		return -ENODEV;
3205 
3206 	dev->priv_flags |= IFF_MACSEC;
3207 
3208 	macsec->real_dev = real_dev;
3209 
3210 	if (data && data[IFLA_MACSEC_ICV_LEN])
3211 		icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3212 	dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3213 
3214 	rx_handler = rtnl_dereference(real_dev->rx_handler);
3215 	if (rx_handler && rx_handler != macsec_handle_frame)
3216 		return -EBUSY;
3217 
3218 	err = register_netdevice(dev);
3219 	if (err < 0)
3220 		return err;
3221 
3222 	dev_hold(real_dev);
3223 
3224 	macsec->nest_level = dev_get_nest_level(real_dev) + 1;
3225 	netdev_lockdep_set_classes(dev);
3226 	lockdep_set_class_and_subclass(&dev->addr_list_lock,
3227 				       &macsec_netdev_addr_lock_key,
3228 				       macsec_get_nest_level(dev));
3229 
3230 	err = netdev_upper_dev_link(real_dev, dev);
3231 	if (err < 0)
3232 		goto unregister;
3233 
3234 	/* need to be already registered so that ->init has run and
3235 	 * the MAC addr is set
3236 	 */
3237 	if (data && data[IFLA_MACSEC_SCI])
3238 		sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3239 	else if (data && data[IFLA_MACSEC_PORT])
3240 		sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3241 	else
3242 		sci = dev_to_sci(dev, MACSEC_PORT_ES);
3243 
3244 	if (rx_handler && sci_exists(real_dev, sci)) {
3245 		err = -EBUSY;
3246 		goto unlink;
3247 	}
3248 
3249 	err = macsec_add_dev(dev, sci, icv_len);
3250 	if (err)
3251 		goto unlink;
3252 
3253 	if (data)
3254 		macsec_changelink_common(dev, data);
3255 
3256 	err = register_macsec_dev(real_dev, dev);
3257 	if (err < 0)
3258 		goto del_dev;
3259 
3260 	macsec_generation++;
3261 
3262 	return 0;
3263 
3264 del_dev:
3265 	macsec_del_dev(macsec);
3266 unlink:
3267 	netdev_upper_dev_unlink(real_dev, dev);
3268 unregister:
3269 	unregister_netdevice(dev);
3270 	return err;
3271 }
3272 
3273 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[])
3274 {
3275 	u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3276 	u8 icv_len = DEFAULT_ICV_LEN;
3277 	int flag;
3278 	bool es, scb, sci;
3279 
3280 	if (!data)
3281 		return 0;
3282 
3283 	if (data[IFLA_MACSEC_CIPHER_SUITE])
3284 		csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3285 
3286 	if (data[IFLA_MACSEC_ICV_LEN]) {
3287 		icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3288 		if (icv_len != DEFAULT_ICV_LEN) {
3289 			char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3290 			struct crypto_aead *dummy_tfm;
3291 
3292 			dummy_tfm = macsec_alloc_tfm(dummy_key,
3293 						     DEFAULT_SAK_LEN,
3294 						     icv_len);
3295 			if (IS_ERR(dummy_tfm))
3296 				return PTR_ERR(dummy_tfm);
3297 			crypto_free_aead(dummy_tfm);
3298 		}
3299 	}
3300 
3301 	switch (csid) {
3302 	case MACSEC_DEFAULT_CIPHER_ID:
3303 	case MACSEC_DEFAULT_CIPHER_ALT:
3304 		if (icv_len < MACSEC_MIN_ICV_LEN ||
3305 		    icv_len > MACSEC_STD_ICV_LEN)
3306 			return -EINVAL;
3307 		break;
3308 	default:
3309 		return -EINVAL;
3310 	}
3311 
3312 	if (data[IFLA_MACSEC_ENCODING_SA]) {
3313 		if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3314 			return -EINVAL;
3315 	}
3316 
3317 	for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3318 	     flag < IFLA_MACSEC_VALIDATION;
3319 	     flag++) {
3320 		if (data[flag]) {
3321 			if (nla_get_u8(data[flag]) > 1)
3322 				return -EINVAL;
3323 		}
3324 	}
3325 
3326 	es  = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3327 	sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3328 	scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3329 
3330 	if ((sci && (scb || es)) || (scb && es))
3331 		return -EINVAL;
3332 
3333 	if (data[IFLA_MACSEC_VALIDATION] &&
3334 	    nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3335 		return -EINVAL;
3336 
3337 	if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3338 	     nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3339 	    !data[IFLA_MACSEC_WINDOW])
3340 		return -EINVAL;
3341 
3342 	return 0;
3343 }
3344 
3345 static struct net *macsec_get_link_net(const struct net_device *dev)
3346 {
3347 	return dev_net(macsec_priv(dev)->real_dev);
3348 }
3349 
3350 static size_t macsec_get_size(const struct net_device *dev)
3351 {
3352 	return  nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
3353 		nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
3354 		nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
3355 		nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
3356 		nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
3357 		nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
3358 		nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
3359 		nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
3360 		nla_total_size(1) + /* IFLA_MACSEC_ES */
3361 		nla_total_size(1) + /* IFLA_MACSEC_SCB */
3362 		nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
3363 		nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
3364 		0;
3365 }
3366 
3367 static int macsec_fill_info(struct sk_buff *skb,
3368 			    const struct net_device *dev)
3369 {
3370 	struct macsec_secy *secy = &macsec_priv(dev)->secy;
3371 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3372 
3373 	if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3374 			IFLA_MACSEC_PAD) ||
3375 	    nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3376 	    nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3377 			      MACSEC_DEFAULT_CIPHER_ID, IFLA_MACSEC_PAD) ||
3378 	    nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3379 	    nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3380 	    nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3381 	    nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3382 	    nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3383 	    nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3384 	    nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3385 	    nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3386 	    0)
3387 		goto nla_put_failure;
3388 
3389 	if (secy->replay_protect) {
3390 		if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3391 			goto nla_put_failure;
3392 	}
3393 
3394 	return 0;
3395 
3396 nla_put_failure:
3397 	return -EMSGSIZE;
3398 }
3399 
3400 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3401 	.kind		= "macsec",
3402 	.priv_size	= sizeof(struct macsec_dev),
3403 	.maxtype	= IFLA_MACSEC_MAX,
3404 	.policy		= macsec_rtnl_policy,
3405 	.setup		= macsec_setup,
3406 	.validate	= macsec_validate_attr,
3407 	.newlink	= macsec_newlink,
3408 	.changelink	= macsec_changelink,
3409 	.dellink	= macsec_dellink,
3410 	.get_size	= macsec_get_size,
3411 	.fill_info	= macsec_fill_info,
3412 	.get_link_net	= macsec_get_link_net,
3413 };
3414 
3415 static bool is_macsec_master(struct net_device *dev)
3416 {
3417 	return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3418 }
3419 
3420 static int macsec_notify(struct notifier_block *this, unsigned long event,
3421 			 void *ptr)
3422 {
3423 	struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3424 	LIST_HEAD(head);
3425 
3426 	if (!is_macsec_master(real_dev))
3427 		return NOTIFY_DONE;
3428 
3429 	switch (event) {
3430 	case NETDEV_UNREGISTER: {
3431 		struct macsec_dev *m, *n;
3432 		struct macsec_rxh_data *rxd;
3433 
3434 		rxd = macsec_data_rtnl(real_dev);
3435 		list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3436 			macsec_common_dellink(m->secy.netdev, &head);
3437 		}
3438 
3439 		netdev_rx_handler_unregister(real_dev);
3440 		kfree(rxd);
3441 
3442 		unregister_netdevice_many(&head);
3443 		break;
3444 	}
3445 	case NETDEV_CHANGEMTU: {
3446 		struct macsec_dev *m;
3447 		struct macsec_rxh_data *rxd;
3448 
3449 		rxd = macsec_data_rtnl(real_dev);
3450 		list_for_each_entry(m, &rxd->secys, secys) {
3451 			struct net_device *dev = m->secy.netdev;
3452 			unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3453 							    macsec_extra_len(true));
3454 
3455 			if (dev->mtu > mtu)
3456 				dev_set_mtu(dev, mtu);
3457 		}
3458 	}
3459 	}
3460 
3461 	return NOTIFY_OK;
3462 }
3463 
3464 static struct notifier_block macsec_notifier = {
3465 	.notifier_call = macsec_notify,
3466 };
3467 
3468 static int __init macsec_init(void)
3469 {
3470 	int err;
3471 
3472 	pr_info("MACsec IEEE 802.1AE\n");
3473 	err = register_netdevice_notifier(&macsec_notifier);
3474 	if (err)
3475 		return err;
3476 
3477 	err = rtnl_link_register(&macsec_link_ops);
3478 	if (err)
3479 		goto notifier;
3480 
3481 	err = genl_register_family(&macsec_fam);
3482 	if (err)
3483 		goto rtnl;
3484 
3485 	return 0;
3486 
3487 rtnl:
3488 	rtnl_link_unregister(&macsec_link_ops);
3489 notifier:
3490 	unregister_netdevice_notifier(&macsec_notifier);
3491 	return err;
3492 }
3493 
3494 static void __exit macsec_exit(void)
3495 {
3496 	genl_unregister_family(&macsec_fam);
3497 	rtnl_link_unregister(&macsec_link_ops);
3498 	unregister_netdevice_notifier(&macsec_notifier);
3499 	rcu_barrier();
3500 }
3501 
3502 module_init(macsec_init);
3503 module_exit(macsec_exit);
3504 
3505 MODULE_ALIAS_RTNL_LINK("macsec");
3506 
3507 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3508 MODULE_LICENSE("GPL v2");
3509