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