xref: /linux/drivers/net/macsec.c (revision 42a9fb3382fc2573e92f41d203b095d9a372cfc9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/net/macsec.c - MACsec device
4  *
5  * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
6  */
7 
8 #include <linux/types.h>
9 #include <linux/skbuff.h>
10 #include <linux/socket.h>
11 #include <linux/module.h>
12 #include <crypto/aead.h>
13 #include <linux/etherdevice.h>
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/refcount.h>
17 #include <net/genetlink.h>
18 #include <net/sock.h>
19 #include <net/gro_cells.h>
20 #include <net/macsec.h>
21 #include <net/dst_metadata.h>
22 #include <net/rtnetlink.h>
23 #include <net/netdev_lock.h>
24 #include <linux/phy.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/if_arp.h>
27 
28 #include <uapi/linux/if_macsec.h>
29 
30 static struct workqueue_struct *macsec_wq;
31 
32 /* SecTAG length = macsec_eth_header without the optional SCI */
33 #define MACSEC_TAG_LEN 6
34 
35 struct macsec_eth_header {
36 	struct ethhdr eth;
37 	/* SecTAG */
38 	u8  tci_an;
39 #if defined(__LITTLE_ENDIAN_BITFIELD)
40 	u8  short_length:6,
41 		  unused:2;
42 #elif defined(__BIG_ENDIAN_BITFIELD)
43 	u8        unused:2,
44 	    short_length:6;
45 #else
46 #error	"Please fix <asm/byteorder.h>"
47 #endif
48 	__be32 packet_number;
49 	u8 secure_channel_id[8]; /* optional */
50 } __packed;
51 
52 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
53 #define MIN_NON_SHORT_LEN 48
54 
55 #define GCM_AES_IV_LEN 12
56 
57 #define for_each_rxsc(secy, sc)				\
58 	for (sc = rcu_dereference_bh(secy->rx_sc);	\
59 	     sc;					\
60 	     sc = rcu_dereference_bh(sc->next))
61 #define for_each_rxsc_rtnl(secy, sc)			\
62 	for (sc = rtnl_dereference(secy->rx_sc);	\
63 	     sc;					\
64 	     sc = rtnl_dereference(sc->next))
65 
66 #define pn_same_half(pn1, pn2) (!(((pn1) >> 31) ^ ((pn2) >> 31)))
67 
68 struct gcm_iv_xpn {
69 	union {
70 		u8 short_secure_channel_id[4];
71 		ssci_t ssci;
72 	};
73 	__be64 pn;
74 } __packed;
75 
76 struct gcm_iv {
77 	union {
78 		u8 secure_channel_id[8];
79 		sci_t sci;
80 	};
81 	__be32 pn;
82 };
83 
84 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
85 
86 struct pcpu_secy_stats {
87 	struct macsec_dev_stats stats;
88 	struct u64_stats_sync syncp;
89 };
90 
91 /**
92  * struct macsec_dev - private data
93  * @secy: SecY config
94  * @real_dev: pointer to underlying netdevice
95  * @dev_tracker: refcount tracker for @real_dev reference
96  * @stats: MACsec device stats
97  * @secys: linked list of SecY's on the underlying device
98  * @gro_cells: pointer to the Generic Receive Offload cell
99  * @offload: status of offloading on the MACsec device
100  * @insert_tx_tag: when offloading, device requires to insert an
101  *	additional tag
102  */
103 struct macsec_dev {
104 	struct macsec_secy secy;
105 	struct net_device *real_dev;
106 	netdevice_tracker dev_tracker;
107 	struct pcpu_secy_stats __percpu *stats;
108 	struct list_head secys;
109 	struct gro_cells gro_cells;
110 	enum macsec_offload offload;
111 	bool insert_tx_tag;
112 };
113 
114 /**
115  * struct macsec_rxh_data - rx_handler private argument
116  * @secys: linked list of SecY's on this underlying device
117  */
118 struct macsec_rxh_data {
119 	struct list_head secys;
120 };
121 
122 static struct macsec_dev *macsec_priv(const struct net_device *dev)
123 {
124 	return (struct macsec_dev *)netdev_priv(dev);
125 }
126 
127 static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
128 {
129 	return rcu_dereference_bh(dev->rx_handler_data);
130 }
131 
132 static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
133 {
134 	return rtnl_dereference(dev->rx_handler_data);
135 }
136 
137 struct macsec_cb {
138 	struct aead_request *req;
139 	union {
140 		struct macsec_tx_sa *tx_sa;
141 		struct macsec_rx_sa *rx_sa;
142 	};
143 	u8 assoc_num;
144 	bool valid;
145 	bool has_sci;
146 };
147 
148 static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
149 {
150 	struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
151 
152 	if (!sa || !sa->active)
153 		return NULL;
154 
155 	if (!refcount_inc_not_zero(&sa->refcnt))
156 		return NULL;
157 
158 	return sa;
159 }
160 
161 static void free_rx_sc_rcu(struct rcu_head *head)
162 {
163 	struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
164 
165 	free_percpu(rx_sc->stats);
166 	kfree(rx_sc);
167 }
168 
169 static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
170 {
171 	return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
172 }
173 
174 static void macsec_rxsc_put(struct macsec_rx_sc *sc)
175 {
176 	if (refcount_dec_and_test(&sc->refcnt))
177 		call_rcu(&sc->rcu_head, free_rx_sc_rcu);
178 }
179 
180 static void free_rxsa_work(struct work_struct *work)
181 {
182 	struct macsec_rx_sa *sa =
183 		container_of(to_rcu_work(work), struct macsec_rx_sa, destroy_work);
184 
185 	crypto_free_aead(sa->key.tfm);
186 	free_percpu(sa->stats);
187 	kfree(sa);
188 }
189 
190 static void macsec_rxsa_put(struct macsec_rx_sa *sa)
191 {
192 	if (refcount_dec_and_test(&sa->refcnt))
193 		queue_rcu_work(macsec_wq, &sa->destroy_work);
194 }
195 
196 static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
197 {
198 	struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
199 
200 	if (!sa || !sa->active)
201 		return NULL;
202 
203 	if (!refcount_inc_not_zero(&sa->refcnt))
204 		return NULL;
205 
206 	return sa;
207 }
208 
209 static void free_txsa_work(struct work_struct *work)
210 {
211 	struct macsec_tx_sa *sa =
212 		container_of(to_rcu_work(work), struct macsec_tx_sa, destroy_work);
213 
214 	crypto_free_aead(sa->key.tfm);
215 	free_percpu(sa->stats);
216 	kfree(sa);
217 }
218 
219 static void macsec_txsa_put(struct macsec_tx_sa *sa)
220 {
221 	if (refcount_dec_and_test(&sa->refcnt))
222 		queue_rcu_work(macsec_wq, &sa->destroy_work);
223 }
224 
225 static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
226 {
227 	BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
228 	return (struct macsec_cb *)skb->cb;
229 }
230 
231 #define MACSEC_PORT_SCB (0x0000)
232 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
233 #define MACSEC_UNDEF_SSCI ((__force ssci_t)0xffffffff)
234 
235 #define MACSEC_GCM_AES_128_SAK_LEN 16
236 #define MACSEC_GCM_AES_256_SAK_LEN 32
237 
238 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
239 #define DEFAULT_XPN false
240 #define DEFAULT_SEND_SCI true
241 #define DEFAULT_ENCRYPT false
242 #define DEFAULT_ENCODING_SA 0
243 #define MACSEC_XPN_MAX_REPLAY_WINDOW (((1 << 30) - 1))
244 
245 static sci_t make_sci(const u8 *addr, __be16 port)
246 {
247 	sci_t sci;
248 
249 	memcpy(&sci, addr, ETH_ALEN);
250 	memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
251 
252 	return sci;
253 }
254 
255 static sci_t macsec_active_sci(struct macsec_secy *secy)
256 {
257 	struct macsec_rx_sc *rx_sc = rcu_dereference_bh(secy->rx_sc);
258 
259 	/* Case single RX SC */
260 	if (rx_sc && !rcu_dereference_bh(rx_sc->next))
261 		return (rx_sc->active) ? rx_sc->sci : 0;
262 	/* Case no RX SC or multiple */
263 	else
264 		return 0;
265 }
266 
267 static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present,
268 			      struct macsec_rxh_data *rxd)
269 {
270 	struct macsec_dev *macsec;
271 	sci_t sci = 0;
272 
273 	/* SC = 1 */
274 	if (sci_present) {
275 		memcpy(&sci, hdr->secure_channel_id,
276 		       sizeof(hdr->secure_channel_id));
277 	/* SC = 0; ES = 0 */
278 	} else if ((!(hdr->tci_an & (MACSEC_TCI_ES | MACSEC_TCI_SC))) &&
279 		   (list_is_singular(&rxd->secys))) {
280 		/* Only one SECY should exist on this scenario */
281 		macsec = list_first_or_null_rcu(&rxd->secys, struct macsec_dev,
282 						secys);
283 		if (macsec)
284 			return macsec_active_sci(&macsec->secy);
285 	} else {
286 		sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
287 	}
288 
289 	return sci;
290 }
291 
292 static unsigned int macsec_sectag_len(bool sci_present)
293 {
294 	return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
295 }
296 
297 static unsigned int macsec_hdr_len(bool sci_present)
298 {
299 	return macsec_sectag_len(sci_present) + ETH_HLEN;
300 }
301 
302 static unsigned int macsec_extra_len(bool sci_present)
303 {
304 	return macsec_sectag_len(sci_present) + sizeof(__be16);
305 }
306 
307 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
308 static void macsec_fill_sectag(struct macsec_eth_header *h,
309 			       const struct macsec_secy *secy, u32 pn,
310 			       bool sci_present)
311 {
312 	const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
313 
314 	memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
315 	h->eth.h_proto = htons(ETH_P_MACSEC);
316 
317 	if (sci_present) {
318 		h->tci_an |= MACSEC_TCI_SC;
319 		memcpy(&h->secure_channel_id, &secy->sci,
320 		       sizeof(h->secure_channel_id));
321 	} else {
322 		if (tx_sc->end_station)
323 			h->tci_an |= MACSEC_TCI_ES;
324 		if (tx_sc->scb)
325 			h->tci_an |= MACSEC_TCI_SCB;
326 	}
327 
328 	h->packet_number = htonl(pn);
329 
330 	/* with GCM, C/E clear for !encrypt, both set for encrypt */
331 	if (tx_sc->encrypt)
332 		h->tci_an |= MACSEC_TCI_CONFID;
333 	else if (secy->icv_len != MACSEC_DEFAULT_ICV_LEN)
334 		h->tci_an |= MACSEC_TCI_C;
335 
336 	h->tci_an |= tx_sc->encoding_sa;
337 }
338 
339 static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
340 {
341 	if (data_len < MIN_NON_SHORT_LEN)
342 		h->short_length = data_len;
343 }
344 
345 /* Checks if a MACsec interface is being offloaded to an hardware engine */
346 static bool macsec_is_offloaded(struct macsec_dev *macsec)
347 {
348 	if (macsec->offload == MACSEC_OFFLOAD_MAC ||
349 	    macsec->offload == MACSEC_OFFLOAD_PHY)
350 		return true;
351 
352 	return false;
353 }
354 
355 /* Checks if underlying layers implement MACsec offloading functions. */
356 static bool macsec_check_offload(enum macsec_offload offload,
357 				 struct macsec_dev *macsec)
358 {
359 	if (!macsec || !macsec->real_dev)
360 		return false;
361 
362 	if (offload == MACSEC_OFFLOAD_PHY)
363 		return macsec->real_dev->phydev &&
364 		       macsec->real_dev->phydev->macsec_ops;
365 	else if (offload == MACSEC_OFFLOAD_MAC)
366 		return macsec->real_dev->features & NETIF_F_HW_MACSEC &&
367 		       macsec->real_dev->macsec_ops;
368 
369 	return false;
370 }
371 
372 static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload,
373 						 struct macsec_dev *macsec,
374 						 struct macsec_context *ctx)
375 {
376 	if (ctx) {
377 		memset(ctx, 0, sizeof(*ctx));
378 		ctx->offload = offload;
379 
380 		if (offload == MACSEC_OFFLOAD_PHY)
381 			ctx->phydev = macsec->real_dev->phydev;
382 		else if (offload == MACSEC_OFFLOAD_MAC)
383 			ctx->netdev = macsec->real_dev;
384 	}
385 
386 	if (offload == MACSEC_OFFLOAD_PHY)
387 		return macsec->real_dev->phydev->macsec_ops;
388 	else
389 		return macsec->real_dev->macsec_ops;
390 }
391 
392 /* Returns a pointer to the MACsec ops struct if any and updates the MACsec
393  * context device reference if provided.
394  */
395 static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec,
396 					       struct macsec_context *ctx)
397 {
398 	if (!macsec_check_offload(macsec->offload, macsec))
399 		return NULL;
400 
401 	return __macsec_get_ops(macsec->offload, macsec, ctx);
402 }
403 
404 /* validate MACsec packet according to IEEE 802.1AE-2018 9.12 */
405 static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len, bool xpn)
406 {
407 	struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
408 	int len = skb->len - 2 * ETH_ALEN;
409 	int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
410 
411 	/* a) It comprises at least 17 octets */
412 	if (skb->len <= 16)
413 		return false;
414 
415 	/* b) MACsec EtherType: already checked */
416 
417 	/* c) V bit is clear */
418 	if (h->tci_an & MACSEC_TCI_VERSION)
419 		return false;
420 
421 	/* d) ES or SCB => !SC */
422 	if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
423 	    (h->tci_an & MACSEC_TCI_SC))
424 		return false;
425 
426 	/* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
427 	if (h->unused)
428 		return false;
429 
430 	/* rx.pn != 0 if not XPN (figure 10-5 with 802.11AEbw-2013 amendment) */
431 	if (!h->packet_number && !xpn)
432 		return false;
433 
434 	/* length check, f) g) h) i) */
435 	if (h->short_length)
436 		return len == extra_len + h->short_length;
437 	return len >= extra_len + MIN_NON_SHORT_LEN;
438 }
439 
440 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
441 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
442 
443 static void macsec_fill_iv_xpn(unsigned char *iv, ssci_t ssci, u64 pn,
444 			       salt_t salt)
445 {
446 	struct gcm_iv_xpn *gcm_iv = (struct gcm_iv_xpn *)iv;
447 
448 	gcm_iv->ssci = ssci ^ salt.ssci;
449 	gcm_iv->pn = cpu_to_be64(pn) ^ salt.pn;
450 }
451 
452 static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
453 {
454 	struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
455 
456 	gcm_iv->sci = sci;
457 	gcm_iv->pn = htonl(pn);
458 }
459 
460 static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
461 {
462 	return (struct macsec_eth_header *)skb_mac_header(skb);
463 }
464 
465 static void __macsec_pn_wrapped(struct macsec_secy *secy,
466 				struct macsec_tx_sa *tx_sa)
467 {
468 	pr_debug("PN wrapped, transitioning to !oper\n");
469 	tx_sa->active = false;
470 	if (secy->protect_frames)
471 		secy->operational = false;
472 }
473 
474 void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa)
475 {
476 	spin_lock_bh(&tx_sa->lock);
477 	__macsec_pn_wrapped(secy, tx_sa);
478 	spin_unlock_bh(&tx_sa->lock);
479 }
480 EXPORT_SYMBOL_GPL(macsec_pn_wrapped);
481 
482 static pn_t tx_sa_update_pn(struct macsec_tx_sa *tx_sa,
483 			    struct macsec_secy *secy)
484 {
485 	pn_t pn;
486 
487 	spin_lock_bh(&tx_sa->lock);
488 
489 	pn = tx_sa->next_pn_halves;
490 	if (secy->xpn)
491 		tx_sa->next_pn++;
492 	else
493 		tx_sa->next_pn_halves.lower++;
494 
495 	if (tx_sa->next_pn == 0)
496 		__macsec_pn_wrapped(secy, tx_sa);
497 	spin_unlock_bh(&tx_sa->lock);
498 
499 	return pn;
500 }
501 
502 static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
503 {
504 	struct macsec_dev *macsec = netdev_priv(dev);
505 
506 	skb->dev = macsec->real_dev;
507 	skb_reset_mac_header(skb);
508 	skb->protocol = eth_hdr(skb)->h_proto;
509 }
510 
511 static unsigned int macsec_msdu_len(struct sk_buff *skb)
512 {
513 	struct macsec_dev *macsec = macsec_priv(skb->dev);
514 	struct macsec_secy *secy = &macsec->secy;
515 	bool sci_present = macsec_skb_cb(skb)->has_sci;
516 
517 	return skb->len - macsec_hdr_len(sci_present) - secy->icv_len;
518 }
519 
520 static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
521 			    struct macsec_tx_sa *tx_sa)
522 {
523 	unsigned int msdu_len = macsec_msdu_len(skb);
524 	struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
525 
526 	u64_stats_update_begin(&txsc_stats->syncp);
527 	if (tx_sc->encrypt) {
528 		txsc_stats->stats.OutOctetsEncrypted += msdu_len;
529 		txsc_stats->stats.OutPktsEncrypted++;
530 		this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
531 	} else {
532 		txsc_stats->stats.OutOctetsProtected += msdu_len;
533 		txsc_stats->stats.OutPktsProtected++;
534 		this_cpu_inc(tx_sa->stats->OutPktsProtected);
535 	}
536 	u64_stats_update_end(&txsc_stats->syncp);
537 }
538 
539 static void count_tx(struct net_device *dev, int ret, int len)
540 {
541 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN))
542 		dev_sw_netstats_tx_add(dev, 1, len);
543 }
544 
545 static void macsec_encrypt_done(void *data, int err)
546 {
547 	struct sk_buff *skb = data;
548 	struct net_device *dev = skb->dev;
549 	struct macsec_dev *macsec = macsec_priv(dev);
550 	struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
551 	int len, ret;
552 
553 	aead_request_free(macsec_skb_cb(skb)->req);
554 
555 	rcu_read_lock_bh();
556 	macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
557 	/* packet is encrypted/protected so tx_bytes must be calculated */
558 	len = macsec_msdu_len(skb) + 2 * ETH_ALEN;
559 	macsec_encrypt_finish(skb, dev);
560 	ret = dev_queue_xmit(skb);
561 	count_tx(dev, ret, len);
562 	rcu_read_unlock_bh();
563 
564 	macsec_txsa_put(sa);
565 	dev_put(dev);
566 }
567 
568 static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
569 					     unsigned char **iv,
570 					     struct scatterlist **sg,
571 					     int num_frags)
572 {
573 	size_t size, iv_offset, sg_offset;
574 	struct aead_request *req;
575 	void *tmp;
576 
577 	size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
578 	iv_offset = size;
579 	size += GCM_AES_IV_LEN;
580 
581 	size = ALIGN(size, __alignof__(struct scatterlist));
582 	sg_offset = size;
583 	size += sizeof(struct scatterlist) * num_frags;
584 
585 	tmp = kmalloc(size, GFP_ATOMIC);
586 	if (!tmp)
587 		return NULL;
588 
589 	*iv = (unsigned char *)(tmp + iv_offset);
590 	*sg = (struct scatterlist *)(tmp + sg_offset);
591 	req = tmp;
592 
593 	aead_request_set_tfm(req, tfm);
594 
595 	return req;
596 }
597 
598 static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
599 				      struct net_device *dev)
600 {
601 	int ret;
602 	struct scatterlist *sg;
603 	struct sk_buff *trailer;
604 	unsigned char *iv;
605 	struct ethhdr *eth;
606 	struct macsec_eth_header *hh;
607 	size_t unprotected_len;
608 	struct aead_request *req;
609 	struct macsec_secy *secy;
610 	struct macsec_tx_sc *tx_sc;
611 	struct macsec_tx_sa *tx_sa;
612 	struct macsec_dev *macsec = macsec_priv(dev);
613 	bool sci_present;
614 	pn_t pn;
615 
616 	secy = &macsec->secy;
617 	tx_sc = &secy->tx_sc;
618 
619 	/* 10.5.1 TX SA assignment */
620 	tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
621 	if (!tx_sa) {
622 		secy->operational = false;
623 		kfree_skb(skb);
624 		return ERR_PTR(-EINVAL);
625 	}
626 
627 	if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
628 		     skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
629 		struct sk_buff *nskb = skb_copy_expand(skb,
630 						       MACSEC_NEEDED_HEADROOM,
631 						       MACSEC_NEEDED_TAILROOM,
632 						       GFP_ATOMIC);
633 		if (likely(nskb)) {
634 			consume_skb(skb);
635 			skb = nskb;
636 		} else {
637 			macsec_txsa_put(tx_sa);
638 			kfree_skb(skb);
639 			return ERR_PTR(-ENOMEM);
640 		}
641 	} else {
642 		skb = skb_unshare(skb, GFP_ATOMIC);
643 		if (!skb) {
644 			macsec_txsa_put(tx_sa);
645 			return ERR_PTR(-ENOMEM);
646 		}
647 	}
648 
649 	unprotected_len = skb->len;
650 	eth = skb_eth_hdr(skb);
651 	sci_present = macsec_send_sci(secy);
652 	hh = skb_push(skb, macsec_extra_len(sci_present));
653 	memmove(hh, eth, 2 * ETH_ALEN);
654 
655 	pn = tx_sa_update_pn(tx_sa, secy);
656 	if (pn.full64 == 0) {
657 		macsec_txsa_put(tx_sa);
658 		kfree_skb(skb);
659 		return ERR_PTR(-ENOLINK);
660 	}
661 	macsec_fill_sectag(hh, secy, pn.lower, sci_present);
662 	macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
663 
664 	skb_put(skb, secy->icv_len);
665 
666 	if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
667 		struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
668 
669 		u64_stats_update_begin(&secy_stats->syncp);
670 		secy_stats->stats.OutPktsTooLong++;
671 		u64_stats_update_end(&secy_stats->syncp);
672 
673 		macsec_txsa_put(tx_sa);
674 		kfree_skb(skb);
675 		return ERR_PTR(-EINVAL);
676 	}
677 
678 	ret = skb_cow_data(skb, 0, &trailer);
679 	if (unlikely(ret < 0)) {
680 		macsec_txsa_put(tx_sa);
681 		kfree_skb(skb);
682 		return ERR_PTR(ret);
683 	}
684 
685 	req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
686 	if (!req) {
687 		macsec_txsa_put(tx_sa);
688 		kfree_skb(skb);
689 		return ERR_PTR(-ENOMEM);
690 	}
691 
692 	if (secy->xpn)
693 		macsec_fill_iv_xpn(iv, tx_sa->ssci, pn.full64, tx_sa->key.salt);
694 	else
695 		macsec_fill_iv(iv, secy->sci, pn.lower);
696 
697 	sg_init_table(sg, ret);
698 	ret = skb_to_sgvec(skb, sg, 0, skb->len);
699 	if (unlikely(ret < 0)) {
700 		aead_request_free(req);
701 		macsec_txsa_put(tx_sa);
702 		kfree_skb(skb);
703 		return ERR_PTR(ret);
704 	}
705 
706 	if (tx_sc->encrypt) {
707 		int len = skb->len - macsec_hdr_len(sci_present) -
708 			  secy->icv_len;
709 		aead_request_set_crypt(req, sg, sg, len, iv);
710 		aead_request_set_ad(req, macsec_hdr_len(sci_present));
711 	} else {
712 		aead_request_set_crypt(req, sg, sg, 0, iv);
713 		aead_request_set_ad(req, skb->len - secy->icv_len);
714 	}
715 
716 	macsec_skb_cb(skb)->req = req;
717 	macsec_skb_cb(skb)->tx_sa = tx_sa;
718 	macsec_skb_cb(skb)->has_sci = sci_present;
719 	aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
720 
721 	dev_hold(skb->dev);
722 	ret = crypto_aead_encrypt(req);
723 	if (ret == -EINPROGRESS) {
724 		return ERR_PTR(ret);
725 	} else if (ret != 0) {
726 		dev_put(skb->dev);
727 		kfree_skb(skb);
728 		aead_request_free(req);
729 		macsec_txsa_put(tx_sa);
730 		return ERR_PTR(-EINVAL);
731 	}
732 
733 	dev_put(skb->dev);
734 	aead_request_free(req);
735 	macsec_txsa_put(tx_sa);
736 
737 	return skb;
738 }
739 
740 static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
741 {
742 	struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
743 	struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
744 	struct macsec_eth_header *hdr = macsec_ethhdr(skb);
745 	u32 lowest_pn = 0;
746 
747 	spin_lock(&rx_sa->lock);
748 	if (rx_sa->next_pn_halves.lower >= secy->replay_window)
749 		lowest_pn = rx_sa->next_pn_halves.lower - secy->replay_window;
750 
751 	/* Now perform replay protection check again
752 	 * (see IEEE 802.1AE-2006 figure 10-5)
753 	 */
754 	if (secy->replay_protect && pn < lowest_pn &&
755 	    (!secy->xpn || pn_same_half(pn, lowest_pn))) {
756 		spin_unlock(&rx_sa->lock);
757 		u64_stats_update_begin(&rxsc_stats->syncp);
758 		rxsc_stats->stats.InPktsLate++;
759 		u64_stats_update_end(&rxsc_stats->syncp);
760 		DEV_STATS_INC(secy->netdev, rx_dropped);
761 		return false;
762 	}
763 
764 	if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
765 		unsigned int msdu_len = macsec_msdu_len(skb);
766 		u64_stats_update_begin(&rxsc_stats->syncp);
767 		if (hdr->tci_an & MACSEC_TCI_E)
768 			rxsc_stats->stats.InOctetsDecrypted += msdu_len;
769 		else
770 			rxsc_stats->stats.InOctetsValidated += msdu_len;
771 		u64_stats_update_end(&rxsc_stats->syncp);
772 	}
773 
774 	if (!macsec_skb_cb(skb)->valid) {
775 		spin_unlock(&rx_sa->lock);
776 
777 		/* 10.6.5 */
778 		if (hdr->tci_an & MACSEC_TCI_C ||
779 		    secy->validate_frames == MACSEC_VALIDATE_STRICT) {
780 			u64_stats_update_begin(&rxsc_stats->syncp);
781 			rxsc_stats->stats.InPktsNotValid++;
782 			u64_stats_update_end(&rxsc_stats->syncp);
783 			this_cpu_inc(rx_sa->stats->InPktsNotValid);
784 			DEV_STATS_INC(secy->netdev, rx_errors);
785 			return false;
786 		}
787 
788 		u64_stats_update_begin(&rxsc_stats->syncp);
789 		if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
790 			rxsc_stats->stats.InPktsInvalid++;
791 			this_cpu_inc(rx_sa->stats->InPktsInvalid);
792 		} else if (pn < lowest_pn) {
793 			rxsc_stats->stats.InPktsDelayed++;
794 		} else {
795 			rxsc_stats->stats.InPktsUnchecked++;
796 		}
797 		u64_stats_update_end(&rxsc_stats->syncp);
798 	} else {
799 		u64_stats_update_begin(&rxsc_stats->syncp);
800 		if (pn < lowest_pn) {
801 			rxsc_stats->stats.InPktsDelayed++;
802 		} else {
803 			rxsc_stats->stats.InPktsOK++;
804 			this_cpu_inc(rx_sa->stats->InPktsOK);
805 		}
806 		u64_stats_update_end(&rxsc_stats->syncp);
807 
808 		// Instead of "pn >=" - to support pn overflow in xpn
809 		if (pn + 1 > rx_sa->next_pn_halves.lower) {
810 			rx_sa->next_pn_halves.lower = pn + 1;
811 		} else if (secy->xpn &&
812 			   (pn + 1 == 0 ||
813 			    !pn_same_half(pn, rx_sa->next_pn_halves.lower))) {
814 			rx_sa->next_pn_halves.upper++;
815 			rx_sa->next_pn_halves.lower = pn + 1;
816 		}
817 
818 		spin_unlock(&rx_sa->lock);
819 	}
820 
821 	return true;
822 }
823 
824 static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
825 {
826 	skb->pkt_type = PACKET_HOST;
827 	skb->protocol = eth_type_trans(skb, dev);
828 
829 	skb_reset_network_header(skb);
830 	if (!skb_transport_header_was_set(skb))
831 		skb_reset_transport_header(skb);
832 	skb_reset_mac_len(skb);
833 }
834 
835 static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
836 {
837 	skb->ip_summed = CHECKSUM_NONE;
838 	memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
839 	skb_pull(skb, hdr_len);
840 	pskb_trim_unique(skb, skb->len - icv_len);
841 }
842 
843 static void count_rx(struct net_device *dev, int len)
844 {
845 	dev_sw_netstats_rx_add(dev, len);
846 }
847 
848 static void macsec_decrypt_done(void *data, int err)
849 {
850 	struct sk_buff *skb = data;
851 	struct net_device *dev = skb->dev;
852 	struct macsec_dev *macsec = macsec_priv(dev);
853 	struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
854 	struct macsec_rx_sc *rx_sc = rx_sa->sc;
855 	int len;
856 	u32 pn;
857 
858 	aead_request_free(macsec_skb_cb(skb)->req);
859 
860 	if (!err)
861 		macsec_skb_cb(skb)->valid = true;
862 
863 	rcu_read_lock_bh();
864 	pn = ntohl(macsec_ethhdr(skb)->packet_number);
865 	if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
866 		rcu_read_unlock_bh();
867 		kfree_skb(skb);
868 		goto out;
869 	}
870 
871 	macsec_finalize_skb(skb, macsec->secy.icv_len,
872 			    macsec_extra_len(macsec_skb_cb(skb)->has_sci));
873 	len = skb->len;
874 	macsec_reset_skb(skb, macsec->secy.netdev);
875 
876 	if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
877 		count_rx(dev, len);
878 
879 	rcu_read_unlock_bh();
880 
881 out:
882 	macsec_rxsa_put(rx_sa);
883 	macsec_rxsc_put(rx_sc);
884 	dev_put(dev);
885 }
886 
887 static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
888 				      struct net_device *dev,
889 				      struct macsec_rx_sa *rx_sa,
890 				      sci_t sci,
891 				      struct macsec_secy *secy)
892 {
893 	int ret;
894 	struct scatterlist *sg;
895 	struct sk_buff *trailer;
896 	unsigned char *iv;
897 	struct aead_request *req;
898 	struct macsec_eth_header *hdr;
899 	u32 hdr_pn;
900 	u16 icv_len = secy->icv_len;
901 
902 	macsec_skb_cb(skb)->valid = false;
903 	skb = skb_share_check(skb, GFP_ATOMIC);
904 	if (!skb)
905 		return ERR_PTR(-ENOMEM);
906 
907 	ret = skb_cow_data(skb, 0, &trailer);
908 	if (unlikely(ret < 0)) {
909 		kfree_skb(skb);
910 		return ERR_PTR(ret);
911 	}
912 	req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
913 	if (!req) {
914 		kfree_skb(skb);
915 		return ERR_PTR(-ENOMEM);
916 	}
917 
918 	hdr = (struct macsec_eth_header *)skb->data;
919 	hdr_pn = ntohl(hdr->packet_number);
920 
921 	if (secy->xpn) {
922 		pn_t recovered_pn = rx_sa->next_pn_halves;
923 
924 		recovered_pn.lower = hdr_pn;
925 		if (hdr_pn < rx_sa->next_pn_halves.lower &&
926 		    !pn_same_half(hdr_pn, rx_sa->next_pn_halves.lower))
927 			recovered_pn.upper++;
928 
929 		macsec_fill_iv_xpn(iv, rx_sa->ssci, recovered_pn.full64,
930 				   rx_sa->key.salt);
931 	} else {
932 		macsec_fill_iv(iv, sci, hdr_pn);
933 	}
934 
935 	sg_init_table(sg, ret);
936 	ret = skb_to_sgvec(skb, sg, 0, skb->len);
937 	if (unlikely(ret < 0)) {
938 		aead_request_free(req);
939 		kfree_skb(skb);
940 		return ERR_PTR(ret);
941 	}
942 
943 	if (hdr->tci_an & MACSEC_TCI_E) {
944 		/* confidentiality: ethernet + macsec header
945 		 * authenticated, encrypted payload
946 		 */
947 		int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
948 
949 		aead_request_set_crypt(req, sg, sg, len, iv);
950 		aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
951 		skb = skb_unshare(skb, GFP_ATOMIC);
952 		if (!skb) {
953 			aead_request_free(req);
954 			return ERR_PTR(-ENOMEM);
955 		}
956 	} else {
957 		/* integrity only: all headers + data authenticated */
958 		aead_request_set_crypt(req, sg, sg, icv_len, iv);
959 		aead_request_set_ad(req, skb->len - icv_len);
960 	}
961 
962 	macsec_skb_cb(skb)->req = req;
963 	skb->dev = dev;
964 	aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
965 
966 	dev_hold(dev);
967 	ret = crypto_aead_decrypt(req);
968 	if (ret == -EINPROGRESS) {
969 		return ERR_PTR(ret);
970 	} else if (ret != 0) {
971 		/* decryption/authentication failed
972 		 * 10.6 if validateFrames is disabled, deliver anyway
973 		 */
974 		if (ret != -EBADMSG) {
975 			kfree_skb(skb);
976 			skb = ERR_PTR(ret);
977 		}
978 	} else {
979 		macsec_skb_cb(skb)->valid = true;
980 	}
981 	dev_put(dev);
982 
983 	aead_request_free(req);
984 
985 	return skb;
986 }
987 
988 static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
989 {
990 	struct macsec_rx_sc *rx_sc;
991 
992 	for_each_rxsc(secy, rx_sc) {
993 		if (rx_sc->sci == sci)
994 			return rx_sc;
995 	}
996 
997 	return NULL;
998 }
999 
1000 static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1001 {
1002 	struct macsec_rx_sc *rx_sc;
1003 
1004 	for_each_rxsc_rtnl(secy, rx_sc) {
1005 		if (rx_sc->sci == sci)
1006 			return rx_sc;
1007 	}
1008 
1009 	return NULL;
1010 }
1011 
1012 static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
1013 {
1014 	/* Deliver to the uncontrolled port by default */
1015 	enum rx_handler_result ret = RX_HANDLER_PASS;
1016 	struct ethhdr *hdr = eth_hdr(skb);
1017 	struct metadata_dst *md_dst;
1018 	struct macsec_rxh_data *rxd;
1019 	struct macsec_dev *macsec;
1020 	bool is_macsec_md_dst;
1021 
1022 	rcu_read_lock();
1023 	rxd = macsec_data_rcu(skb->dev);
1024 	md_dst = skb_metadata_dst(skb);
1025 	is_macsec_md_dst = md_dst && md_dst->type == METADATA_MACSEC;
1026 
1027 	list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1028 		struct sk_buff *nskb;
1029 		struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1030 		struct net_device *ndev = macsec->secy.netdev;
1031 
1032 		/* If h/w offloading is enabled, HW decodes frames and strips
1033 		 * the SecTAG, so we have to deduce which port to deliver to.
1034 		 */
1035 		if (macsec_is_offloaded(macsec) && netif_running(ndev)) {
1036 			const struct macsec_ops *ops;
1037 
1038 			ops = macsec_get_ops(macsec, NULL);
1039 
1040 			if (ops->rx_uses_md_dst && !is_macsec_md_dst)
1041 				continue;
1042 
1043 			if (is_macsec_md_dst) {
1044 				struct macsec_rx_sc *rx_sc;
1045 
1046 				/* All drivers that implement MACsec offload
1047 				 * support using skb metadata destinations must
1048 				 * indicate that they do so.
1049 				 */
1050 				DEBUG_NET_WARN_ON_ONCE(!ops->rx_uses_md_dst);
1051 				rx_sc = find_rx_sc(&macsec->secy,
1052 						   md_dst->u.macsec_info.sci);
1053 				if (!rx_sc)
1054 					continue;
1055 				/* device indicated macsec offload occurred */
1056 				skb->dev = ndev;
1057 				skb->pkt_type = PACKET_HOST;
1058 				eth_skb_pkt_type(skb, ndev);
1059 				ret = RX_HANDLER_ANOTHER;
1060 				goto out;
1061 			}
1062 
1063 			/* This datapath is insecure because it is unable to
1064 			 * enforce isolation of broadcast/multicast traffic and
1065 			 * unicast traffic with promiscuous mode on the macsec
1066 			 * netdev. Since the core stack has no mechanism to
1067 			 * check that the hardware did indeed receive MACsec
1068 			 * traffic, it is possible that the response handling
1069 			 * done by the MACsec port was to a plaintext packet.
1070 			 * This violates the MACsec protocol standard.
1071 			 */
1072 			if (ether_addr_equal_64bits(hdr->h_dest,
1073 						    ndev->dev_addr)) {
1074 				/* exact match, divert skb to this port */
1075 				skb->dev = ndev;
1076 				skb->pkt_type = PACKET_HOST;
1077 				ret = RX_HANDLER_ANOTHER;
1078 				goto out;
1079 			} else if (is_multicast_ether_addr_64bits(
1080 					   hdr->h_dest)) {
1081 				/* multicast frame, deliver on this port too */
1082 				nskb = skb_clone(skb, GFP_ATOMIC);
1083 				if (!nskb)
1084 					break;
1085 
1086 				nskb->dev = ndev;
1087 				eth_skb_pkt_type(nskb, ndev);
1088 
1089 				__netif_rx(nskb);
1090 			} else if (ndev->flags & IFF_PROMISC) {
1091 				skb->dev = ndev;
1092 				skb->pkt_type = PACKET_HOST;
1093 				ret = RX_HANDLER_ANOTHER;
1094 				goto out;
1095 			}
1096 
1097 			continue;
1098 		}
1099 
1100 		/* 10.6 If the management control validateFrames is not
1101 		 * Strict, frames without a SecTAG are received, counted, and
1102 		 * delivered to the Controlled Port
1103 		 */
1104 		if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1105 			u64_stats_update_begin(&secy_stats->syncp);
1106 			secy_stats->stats.InPktsNoTag++;
1107 			u64_stats_update_end(&secy_stats->syncp);
1108 			DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1109 			continue;
1110 		}
1111 
1112 		/* deliver on this port */
1113 		nskb = skb_clone(skb, GFP_ATOMIC);
1114 		if (!nskb)
1115 			break;
1116 
1117 		nskb->dev = ndev;
1118 
1119 		if (__netif_rx(nskb) == NET_RX_SUCCESS) {
1120 			u64_stats_update_begin(&secy_stats->syncp);
1121 			secy_stats->stats.InPktsUntagged++;
1122 			u64_stats_update_end(&secy_stats->syncp);
1123 		}
1124 	}
1125 
1126 out:
1127 	rcu_read_unlock();
1128 	return ret;
1129 }
1130 
1131 static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1132 {
1133 	struct sk_buff *skb = *pskb;
1134 	struct net_device *dev = skb->dev;
1135 	struct macsec_eth_header *hdr;
1136 	struct macsec_secy *secy = NULL;
1137 	struct macsec_rx_sc *rx_sc;
1138 	struct macsec_rx_sa *rx_sa;
1139 	struct macsec_rxh_data *rxd;
1140 	struct macsec_dev *macsec;
1141 	unsigned int len;
1142 	sci_t sci = 0;
1143 	u32 hdr_pn;
1144 	bool cbit;
1145 	struct pcpu_rx_sc_stats *rxsc_stats;
1146 	struct pcpu_secy_stats *secy_stats;
1147 	bool pulled_sci;
1148 	int ret;
1149 
1150 	if (skb_headroom(skb) < ETH_HLEN)
1151 		goto drop_direct;
1152 
1153 	hdr = macsec_ethhdr(skb);
1154 	if (hdr->eth.h_proto != htons(ETH_P_MACSEC))
1155 		return handle_not_macsec(skb);
1156 
1157 	skb = skb_unshare(skb, GFP_ATOMIC);
1158 	*pskb = skb;
1159 	if (!skb)
1160 		return RX_HANDLER_CONSUMED;
1161 
1162 	pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1163 	if (!pulled_sci) {
1164 		if (!pskb_may_pull(skb, macsec_extra_len(false)))
1165 			goto drop_direct;
1166 	}
1167 
1168 	hdr = macsec_ethhdr(skb);
1169 
1170 	/* Frames with a SecTAG that has the TCI E bit set but the C
1171 	 * bit clear are discarded, as this reserved encoding is used
1172 	 * to identify frames with a SecTAG that are not to be
1173 	 * delivered to the Controlled Port.
1174 	 */
1175 	if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1176 		return RX_HANDLER_PASS;
1177 
1178 	/* now, pull the extra length */
1179 	if (hdr->tci_an & MACSEC_TCI_SC) {
1180 		if (!pulled_sci)
1181 			goto drop_direct;
1182 	}
1183 
1184 	/* ethernet header is part of crypto processing */
1185 	skb_push(skb, ETH_HLEN);
1186 
1187 	macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1188 	macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1189 
1190 	rcu_read_lock();
1191 	rxd = macsec_data_rcu(skb->dev);
1192 
1193 	sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci, rxd);
1194 	if (!sci)
1195 		goto drop_nosc;
1196 
1197 	list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1198 		struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1199 
1200 		sc = sc ? macsec_rxsc_get(sc) : NULL;
1201 
1202 		if (sc) {
1203 			secy = &macsec->secy;
1204 			rx_sc = sc;
1205 			break;
1206 		}
1207 	}
1208 
1209 	if (!secy)
1210 		goto nosci;
1211 
1212 	dev = secy->netdev;
1213 	macsec = macsec_priv(dev);
1214 	secy_stats = this_cpu_ptr(macsec->stats);
1215 	rxsc_stats = this_cpu_ptr(rx_sc->stats);
1216 
1217 	if (!macsec_validate_skb(skb, secy->icv_len, secy->xpn)) {
1218 		u64_stats_update_begin(&secy_stats->syncp);
1219 		secy_stats->stats.InPktsBadTag++;
1220 		u64_stats_update_end(&secy_stats->syncp);
1221 		DEV_STATS_INC(secy->netdev, rx_errors);
1222 		goto drop_nosa;
1223 	}
1224 
1225 	rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1226 	if (!rx_sa) {
1227 		/* 10.6.1 if the SA is not in use */
1228 
1229 		/* If validateFrames is Strict or the C bit in the
1230 		 * SecTAG is set, discard
1231 		 */
1232 		if (hdr->tci_an & MACSEC_TCI_C ||
1233 		    secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1234 			u64_stats_update_begin(&rxsc_stats->syncp);
1235 			rxsc_stats->stats.InPktsNotUsingSA++;
1236 			u64_stats_update_end(&rxsc_stats->syncp);
1237 			DEV_STATS_INC(secy->netdev, rx_errors);
1238 			goto drop_nosa;
1239 		}
1240 
1241 		/* not Strict, the frame (with the SecTAG and ICV
1242 		 * removed) is delivered to the Controlled Port.
1243 		 */
1244 		u64_stats_update_begin(&rxsc_stats->syncp);
1245 		rxsc_stats->stats.InPktsUnusedSA++;
1246 		u64_stats_update_end(&rxsc_stats->syncp);
1247 		goto deliver;
1248 	}
1249 
1250 	/* First, PN check to avoid decrypting obviously wrong packets */
1251 	hdr_pn = ntohl(hdr->packet_number);
1252 	if (secy->replay_protect) {
1253 		bool late;
1254 
1255 		spin_lock(&rx_sa->lock);
1256 		late = rx_sa->next_pn_halves.lower >= secy->replay_window &&
1257 		       hdr_pn < (rx_sa->next_pn_halves.lower - secy->replay_window);
1258 
1259 		if (secy->xpn)
1260 			late = late && pn_same_half(rx_sa->next_pn_halves.lower, hdr_pn);
1261 		spin_unlock(&rx_sa->lock);
1262 
1263 		if (late) {
1264 			u64_stats_update_begin(&rxsc_stats->syncp);
1265 			rxsc_stats->stats.InPktsLate++;
1266 			u64_stats_update_end(&rxsc_stats->syncp);
1267 			DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1268 			goto drop;
1269 		}
1270 	}
1271 
1272 	macsec_skb_cb(skb)->rx_sa = rx_sa;
1273 
1274 	/* Disabled && !changed text => skip validation */
1275 	if (hdr->tci_an & MACSEC_TCI_C ||
1276 	    secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1277 		skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1278 
1279 	if (IS_ERR(skb)) {
1280 		/* the decrypt callback needs the reference */
1281 		if (PTR_ERR(skb) != -EINPROGRESS) {
1282 			macsec_rxsa_put(rx_sa);
1283 			macsec_rxsc_put(rx_sc);
1284 		}
1285 		rcu_read_unlock();
1286 		*pskb = NULL;
1287 		return RX_HANDLER_CONSUMED;
1288 	}
1289 
1290 	if (!macsec_post_decrypt(skb, secy, hdr_pn))
1291 		goto drop;
1292 
1293 deliver:
1294 	macsec_finalize_skb(skb, secy->icv_len,
1295 			    macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1296 	len = skb->len;
1297 	macsec_reset_skb(skb, secy->netdev);
1298 
1299 	if (rx_sa)
1300 		macsec_rxsa_put(rx_sa);
1301 	macsec_rxsc_put(rx_sc);
1302 
1303 	skb_orphan(skb);
1304 	ret = gro_cells_receive(&macsec->gro_cells, skb);
1305 	if (ret == NET_RX_SUCCESS)
1306 		count_rx(dev, len);
1307 	else
1308 		DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1309 
1310 	rcu_read_unlock();
1311 
1312 	*pskb = NULL;
1313 	return RX_HANDLER_CONSUMED;
1314 
1315 drop:
1316 	macsec_rxsa_put(rx_sa);
1317 drop_nosa:
1318 	macsec_rxsc_put(rx_sc);
1319 drop_nosc:
1320 	rcu_read_unlock();
1321 drop_direct:
1322 	kfree_skb(skb);
1323 	*pskb = NULL;
1324 	return RX_HANDLER_CONSUMED;
1325 
1326 nosci:
1327 	/* 10.6.1 if the SC is not found */
1328 	cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1329 	if (!cbit)
1330 		macsec_finalize_skb(skb, MACSEC_DEFAULT_ICV_LEN,
1331 				    macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1332 
1333 	list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1334 		struct sk_buff *nskb;
1335 
1336 		secy_stats = this_cpu_ptr(macsec->stats);
1337 
1338 		/* If validateFrames is Strict or the C bit in the
1339 		 * SecTAG is set, discard
1340 		 */
1341 		if (cbit ||
1342 		    macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1343 			u64_stats_update_begin(&secy_stats->syncp);
1344 			secy_stats->stats.InPktsNoSCI++;
1345 			u64_stats_update_end(&secy_stats->syncp);
1346 			DEV_STATS_INC(macsec->secy.netdev, rx_errors);
1347 			continue;
1348 		}
1349 
1350 		/* not strict, the frame (with the SecTAG and ICV
1351 		 * removed) is delivered to the Controlled Port.
1352 		 */
1353 		nskb = skb_clone(skb, GFP_ATOMIC);
1354 		if (!nskb)
1355 			break;
1356 
1357 		macsec_reset_skb(nskb, macsec->secy.netdev);
1358 
1359 		ret = __netif_rx(nskb);
1360 		if (ret == NET_RX_SUCCESS) {
1361 			u64_stats_update_begin(&secy_stats->syncp);
1362 			secy_stats->stats.InPktsUnknownSCI++;
1363 			u64_stats_update_end(&secy_stats->syncp);
1364 		} else {
1365 			DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
1366 		}
1367 	}
1368 
1369 	rcu_read_unlock();
1370 	*pskb = skb;
1371 	return RX_HANDLER_PASS;
1372 }
1373 
1374 static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1375 {
1376 	struct crypto_aead *tfm;
1377 	int ret;
1378 
1379 	tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
1380 
1381 	if (IS_ERR(tfm))
1382 		return tfm;
1383 
1384 	ret = crypto_aead_setkey(tfm, key, key_len);
1385 	if (ret < 0)
1386 		goto fail;
1387 
1388 	ret = crypto_aead_setauthsize(tfm, icv_len);
1389 	if (ret < 0)
1390 		goto fail;
1391 
1392 	return tfm;
1393 fail:
1394 	crypto_free_aead(tfm);
1395 	return ERR_PTR(ret);
1396 }
1397 
1398 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1399 		      int icv_len)
1400 {
1401 	rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1402 	if (!rx_sa->stats)
1403 		return -ENOMEM;
1404 
1405 	rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1406 	if (IS_ERR(rx_sa->key.tfm)) {
1407 		free_percpu(rx_sa->stats);
1408 		return PTR_ERR(rx_sa->key.tfm);
1409 	}
1410 
1411 	rx_sa->ssci = MACSEC_UNDEF_SSCI;
1412 	rx_sa->active = false;
1413 	rx_sa->next_pn = 1;
1414 	refcount_set(&rx_sa->refcnt, 1);
1415 	spin_lock_init(&rx_sa->lock);
1416 	INIT_RCU_WORK(&rx_sa->destroy_work, free_rxsa_work);
1417 
1418 	return 0;
1419 }
1420 
1421 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1422 {
1423 	rx_sa->active = false;
1424 
1425 	macsec_rxsa_put(rx_sa);
1426 }
1427 
1428 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1429 {
1430 	int i;
1431 
1432 	for (i = 0; i < MACSEC_NUM_AN; i++) {
1433 		struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1434 
1435 		RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1436 		if (sa)
1437 			clear_rx_sa(sa);
1438 	}
1439 
1440 	macsec_rxsc_put(rx_sc);
1441 }
1442 
1443 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1444 {
1445 	struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1446 
1447 	for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1448 	     rx_sc;
1449 	     rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1450 		if (rx_sc->sci == sci) {
1451 			if (rx_sc->active)
1452 				secy->n_rx_sc--;
1453 			rcu_assign_pointer(*rx_scp, rx_sc->next);
1454 			return rx_sc;
1455 		}
1456 	}
1457 
1458 	return NULL;
1459 }
1460 
1461 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci,
1462 					 bool active)
1463 {
1464 	struct macsec_rx_sc *rx_sc;
1465 	struct macsec_dev *macsec;
1466 	struct net_device *real_dev = macsec_priv(dev)->real_dev;
1467 	struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1468 	struct macsec_secy *secy;
1469 
1470 	list_for_each_entry(macsec, &rxd->secys, secys) {
1471 		if (find_rx_sc_rtnl(&macsec->secy, sci))
1472 			return ERR_PTR(-EEXIST);
1473 	}
1474 
1475 	rx_sc = kzalloc_obj(*rx_sc);
1476 	if (!rx_sc)
1477 		return ERR_PTR(-ENOMEM);
1478 
1479 	rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1480 	if (!rx_sc->stats) {
1481 		kfree(rx_sc);
1482 		return ERR_PTR(-ENOMEM);
1483 	}
1484 
1485 	rx_sc->sci = sci;
1486 	rx_sc->active = active;
1487 	refcount_set(&rx_sc->refcnt, 1);
1488 
1489 	secy = &macsec_priv(dev)->secy;
1490 	rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1491 	rcu_assign_pointer(secy->rx_sc, rx_sc);
1492 
1493 	if (rx_sc->active)
1494 		secy->n_rx_sc++;
1495 
1496 	return rx_sc;
1497 }
1498 
1499 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1500 		      int icv_len)
1501 {
1502 	tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1503 	if (!tx_sa->stats)
1504 		return -ENOMEM;
1505 
1506 	tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1507 	if (IS_ERR(tx_sa->key.tfm)) {
1508 		free_percpu(tx_sa->stats);
1509 		return PTR_ERR(tx_sa->key.tfm);
1510 	}
1511 
1512 	tx_sa->ssci = MACSEC_UNDEF_SSCI;
1513 	tx_sa->active = false;
1514 	refcount_set(&tx_sa->refcnt, 1);
1515 	spin_lock_init(&tx_sa->lock);
1516 	INIT_RCU_WORK(&tx_sa->destroy_work, free_txsa_work);
1517 
1518 	return 0;
1519 }
1520 
1521 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1522 {
1523 	tx_sa->active = false;
1524 
1525 	macsec_txsa_put(tx_sa);
1526 }
1527 
1528 static struct genl_family macsec_fam;
1529 
1530 static struct net_device *get_dev_from_nl(struct net *net,
1531 					  struct nlattr **attrs)
1532 {
1533 	int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1534 	struct net_device *dev;
1535 
1536 	dev = __dev_get_by_index(net, ifindex);
1537 	if (!dev)
1538 		return ERR_PTR(-ENODEV);
1539 
1540 	if (!netif_is_macsec(dev))
1541 		return ERR_PTR(-ENODEV);
1542 
1543 	return dev;
1544 }
1545 
1546 static enum macsec_offload nla_get_offload(const struct nlattr *nla)
1547 {
1548 	return (__force enum macsec_offload)nla_get_u8(nla);
1549 }
1550 
1551 static sci_t nla_get_sci(const struct nlattr *nla)
1552 {
1553 	return (__force sci_t)nla_get_u64(nla);
1554 }
1555 
1556 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1557 		       int padattr)
1558 {
1559 	return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1560 }
1561 
1562 static ssci_t nla_get_ssci(const struct nlattr *nla)
1563 {
1564 	return (__force ssci_t)nla_get_u32(nla);
1565 }
1566 
1567 static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value)
1568 {
1569 	return nla_put_u32(skb, attrtype, (__force u64)value);
1570 }
1571 
1572 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1573 					     struct nlattr **attrs,
1574 					     struct nlattr **tb_sa,
1575 					     struct net_device **devp,
1576 					     struct macsec_secy **secyp,
1577 					     struct macsec_tx_sc **scp,
1578 					     u8 *assoc_num)
1579 {
1580 	struct net_device *dev;
1581 	struct macsec_secy *secy;
1582 	struct macsec_tx_sc *tx_sc;
1583 	struct macsec_tx_sa *tx_sa;
1584 
1585 	if (!tb_sa[MACSEC_SA_ATTR_AN])
1586 		return ERR_PTR(-EINVAL);
1587 
1588 	*assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1589 
1590 	dev = get_dev_from_nl(net, attrs);
1591 	if (IS_ERR(dev))
1592 		return ERR_CAST(dev);
1593 
1594 	secy = &macsec_priv(dev)->secy;
1595 	tx_sc = &secy->tx_sc;
1596 
1597 	tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1598 	if (!tx_sa)
1599 		return ERR_PTR(-ENODEV);
1600 
1601 	*devp = dev;
1602 	*scp = tx_sc;
1603 	*secyp = secy;
1604 	return tx_sa;
1605 }
1606 
1607 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1608 					     struct nlattr **attrs,
1609 					     struct nlattr **tb_rxsc,
1610 					     struct net_device **devp,
1611 					     struct macsec_secy **secyp)
1612 {
1613 	struct net_device *dev;
1614 	struct macsec_secy *secy;
1615 	struct macsec_rx_sc *rx_sc;
1616 	sci_t sci;
1617 
1618 	dev = get_dev_from_nl(net, attrs);
1619 	if (IS_ERR(dev))
1620 		return ERR_CAST(dev);
1621 
1622 	secy = &macsec_priv(dev)->secy;
1623 
1624 	if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1625 		return ERR_PTR(-EINVAL);
1626 
1627 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1628 	rx_sc = find_rx_sc_rtnl(secy, sci);
1629 	if (!rx_sc)
1630 		return ERR_PTR(-ENODEV);
1631 
1632 	*secyp = secy;
1633 	*devp = dev;
1634 
1635 	return rx_sc;
1636 }
1637 
1638 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1639 					     struct nlattr **attrs,
1640 					     struct nlattr **tb_rxsc,
1641 					     struct nlattr **tb_sa,
1642 					     struct net_device **devp,
1643 					     struct macsec_secy **secyp,
1644 					     struct macsec_rx_sc **scp,
1645 					     u8 *assoc_num)
1646 {
1647 	struct macsec_rx_sc *rx_sc;
1648 	struct macsec_rx_sa *rx_sa;
1649 
1650 	if (!tb_sa[MACSEC_SA_ATTR_AN])
1651 		return ERR_PTR(-EINVAL);
1652 
1653 	*assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1654 
1655 	rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1656 	if (IS_ERR(rx_sc))
1657 		return ERR_CAST(rx_sc);
1658 
1659 	rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1660 	if (!rx_sa)
1661 		return ERR_PTR(-ENODEV);
1662 
1663 	*scp = rx_sc;
1664 	return rx_sa;
1665 }
1666 
1667 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1668 	[MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1669 	[MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1670 	[MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1671 	[MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
1672 };
1673 
1674 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1675 	[MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1676 	[MACSEC_RXSC_ATTR_ACTIVE] = NLA_POLICY_MAX(NLA_U8, 1),
1677 };
1678 
1679 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1680 	[MACSEC_SA_ATTR_AN] = NLA_POLICY_MAX(NLA_U8, MACSEC_NUM_AN - 1),
1681 	[MACSEC_SA_ATTR_ACTIVE] = NLA_POLICY_MAX(NLA_U8, 1),
1682 	[MACSEC_SA_ATTR_PN] = NLA_POLICY_MIN(NLA_UINT, 1),
1683 	[MACSEC_SA_ATTR_KEYID] = NLA_POLICY_EXACT_LEN(MACSEC_KEYID_LEN),
1684 	[MACSEC_SA_ATTR_KEY] = NLA_POLICY_MAX_LEN(MACSEC_MAX_KEY_LEN),
1685 	[MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 },
1686 	[MACSEC_SA_ATTR_SALT] = NLA_POLICY_EXACT_LEN(MACSEC_SALT_LEN),
1687 };
1688 
1689 static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
1690 	[MACSEC_OFFLOAD_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U8, MACSEC_OFFLOAD_MAX),
1691 };
1692 
1693 /* Offloads an operation to a device driver */
1694 static int macsec_offload(int (* const func)(struct macsec_context *),
1695 			  struct macsec_context *ctx)
1696 {
1697 	int ret;
1698 
1699 	if (unlikely(!func))
1700 		return 0;
1701 
1702 	if (ctx->offload == MACSEC_OFFLOAD_PHY)
1703 		mutex_lock(&ctx->phydev->lock);
1704 
1705 	ret = (*func)(ctx);
1706 
1707 	if (ctx->offload == MACSEC_OFFLOAD_PHY)
1708 		mutex_unlock(&ctx->phydev->lock);
1709 
1710 	return ret;
1711 }
1712 
1713 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1714 {
1715 	if (!attrs[MACSEC_ATTR_SA_CONFIG])
1716 		return -EINVAL;
1717 
1718 	if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1719 		return -EINVAL;
1720 
1721 	return 0;
1722 }
1723 
1724 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1725 {
1726 	if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1727 		return -EINVAL;
1728 
1729 	if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1730 		return -EINVAL;
1731 
1732 	return 0;
1733 }
1734 
1735 static bool validate_add_rxsa(struct nlattr **attrs)
1736 {
1737 	if (!attrs[MACSEC_SA_ATTR_AN] ||
1738 	    !attrs[MACSEC_SA_ATTR_KEY] ||
1739 	    !attrs[MACSEC_SA_ATTR_KEYID])
1740 		return false;
1741 
1742 	return true;
1743 }
1744 
1745 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1746 {
1747 	struct net_device *dev;
1748 	struct nlattr **attrs = info->attrs;
1749 	struct macsec_secy *secy;
1750 	struct macsec_rx_sc *rx_sc;
1751 	struct macsec_rx_sa *rx_sa;
1752 	unsigned char assoc_num;
1753 	int pn_len;
1754 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1755 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1756 	int err;
1757 
1758 	if (!attrs[MACSEC_ATTR_IFINDEX])
1759 		return -EINVAL;
1760 
1761 	if (parse_sa_config(attrs, tb_sa))
1762 		return -EINVAL;
1763 
1764 	if (parse_rxsc_config(attrs, tb_rxsc))
1765 		return -EINVAL;
1766 
1767 	if (!validate_add_rxsa(tb_sa))
1768 		return -EINVAL;
1769 
1770 	rtnl_lock();
1771 	rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1772 	if (IS_ERR(rx_sc)) {
1773 		rtnl_unlock();
1774 		return PTR_ERR(rx_sc);
1775 	}
1776 
1777 	assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1778 
1779 	if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1780 		pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1781 			  nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1782 		rtnl_unlock();
1783 		return -EINVAL;
1784 	}
1785 
1786 	pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1787 	if (tb_sa[MACSEC_SA_ATTR_PN] &&
1788 	    nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1789 		pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
1790 			  nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1791 		rtnl_unlock();
1792 		return -EINVAL;
1793 	}
1794 
1795 	if (secy->xpn) {
1796 		if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1797 			rtnl_unlock();
1798 			return -EINVAL;
1799 		}
1800 	}
1801 
1802 	rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1803 	if (rx_sa) {
1804 		rtnl_unlock();
1805 		return -EBUSY;
1806 	}
1807 
1808 	rx_sa = kmalloc_obj(*rx_sa);
1809 	if (!rx_sa) {
1810 		rtnl_unlock();
1811 		return -ENOMEM;
1812 	}
1813 
1814 	err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1815 			 secy->key_len, secy->icv_len);
1816 	if (err < 0) {
1817 		kfree(rx_sa);
1818 		rtnl_unlock();
1819 		return err;
1820 	}
1821 
1822 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
1823 		spin_lock_bh(&rx_sa->lock);
1824 		rx_sa->next_pn = nla_get_uint(tb_sa[MACSEC_SA_ATTR_PN]);
1825 		spin_unlock_bh(&rx_sa->lock);
1826 	}
1827 
1828 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1829 		rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1830 
1831 	rx_sa->sc = rx_sc;
1832 
1833 	if (secy->xpn) {
1834 		rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
1835 		nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
1836 			   MACSEC_SALT_LEN);
1837 	}
1838 
1839 	/* If h/w offloading is available, propagate to the device */
1840 	if (macsec_is_offloaded(netdev_priv(dev))) {
1841 		const struct macsec_ops *ops;
1842 		struct macsec_context ctx;
1843 
1844 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
1845 		if (!ops) {
1846 			err = -EOPNOTSUPP;
1847 			goto cleanup;
1848 		}
1849 
1850 		ctx.sa.assoc_num = assoc_num;
1851 		ctx.sa.rx_sa = rx_sa;
1852 		ctx.secy = secy;
1853 		memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1854 		       secy->key_len);
1855 
1856 		err = macsec_offload(ops->mdo_add_rxsa, &ctx);
1857 		memzero_explicit(ctx.sa.key, secy->key_len);
1858 		if (err)
1859 			goto cleanup;
1860 	}
1861 
1862 	nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1863 	rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1864 
1865 	rtnl_unlock();
1866 
1867 	return 0;
1868 
1869 cleanup:
1870 	macsec_rxsa_put(rx_sa);
1871 	rtnl_unlock();
1872 	return err;
1873 }
1874 
1875 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1876 {
1877 	struct net_device *dev;
1878 	sci_t sci = MACSEC_UNDEF_SCI;
1879 	struct nlattr **attrs = info->attrs;
1880 	struct macsec_rx_sc *rx_sc;
1881 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1882 	struct macsec_secy *secy;
1883 	bool active = true;
1884 	int ret;
1885 
1886 	if (!attrs[MACSEC_ATTR_IFINDEX])
1887 		return -EINVAL;
1888 
1889 	if (parse_rxsc_config(attrs, tb_rxsc))
1890 		return -EINVAL;
1891 
1892 	if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1893 		return -EINVAL;
1894 
1895 	rtnl_lock();
1896 	dev = get_dev_from_nl(genl_info_net(info), attrs);
1897 	if (IS_ERR(dev)) {
1898 		rtnl_unlock();
1899 		return PTR_ERR(dev);
1900 	}
1901 
1902 	secy = &macsec_priv(dev)->secy;
1903 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1904 
1905 	if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1906 		active = nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1907 
1908 	rx_sc = create_rx_sc(dev, sci, active);
1909 	if (IS_ERR(rx_sc)) {
1910 		rtnl_unlock();
1911 		return PTR_ERR(rx_sc);
1912 	}
1913 
1914 	if (macsec_is_offloaded(netdev_priv(dev))) {
1915 		const struct macsec_ops *ops;
1916 		struct macsec_context ctx;
1917 
1918 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
1919 		if (!ops) {
1920 			ret = -EOPNOTSUPP;
1921 			goto cleanup;
1922 		}
1923 
1924 		ctx.rx_sc = rx_sc;
1925 		ctx.secy = secy;
1926 
1927 		ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
1928 		if (ret)
1929 			goto cleanup;
1930 	}
1931 
1932 	rtnl_unlock();
1933 
1934 	return 0;
1935 
1936 cleanup:
1937 	del_rx_sc(secy, sci);
1938 	free_rx_sc(rx_sc);
1939 	rtnl_unlock();
1940 	return ret;
1941 }
1942 
1943 static bool validate_add_txsa(struct nlattr **attrs)
1944 {
1945 	if (!attrs[MACSEC_SA_ATTR_AN] ||
1946 	    !attrs[MACSEC_SA_ATTR_PN] ||
1947 	    !attrs[MACSEC_SA_ATTR_KEY] ||
1948 	    !attrs[MACSEC_SA_ATTR_KEYID])
1949 		return false;
1950 
1951 	return true;
1952 }
1953 
1954 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1955 {
1956 	struct net_device *dev;
1957 	struct nlattr **attrs = info->attrs;
1958 	struct macsec_secy *secy;
1959 	struct macsec_tx_sc *tx_sc;
1960 	struct macsec_tx_sa *tx_sa;
1961 	unsigned char assoc_num;
1962 	int pn_len;
1963 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1964 	bool was_operational;
1965 	int err;
1966 
1967 	if (!attrs[MACSEC_ATTR_IFINDEX])
1968 		return -EINVAL;
1969 
1970 	if (parse_sa_config(attrs, tb_sa))
1971 		return -EINVAL;
1972 
1973 	if (!validate_add_txsa(tb_sa))
1974 		return -EINVAL;
1975 
1976 	rtnl_lock();
1977 	dev = get_dev_from_nl(genl_info_net(info), attrs);
1978 	if (IS_ERR(dev)) {
1979 		rtnl_unlock();
1980 		return PTR_ERR(dev);
1981 	}
1982 
1983 	secy = &macsec_priv(dev)->secy;
1984 	tx_sc = &secy->tx_sc;
1985 
1986 	assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1987 
1988 	if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1989 		pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1990 			  nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1991 		rtnl_unlock();
1992 		return -EINVAL;
1993 	}
1994 
1995 	pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1996 	if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1997 		pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
1998 			  nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1999 		rtnl_unlock();
2000 		return -EINVAL;
2001 	}
2002 
2003 	if (secy->xpn) {
2004 		if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
2005 			rtnl_unlock();
2006 			return -EINVAL;
2007 		}
2008 	}
2009 
2010 	tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
2011 	if (tx_sa) {
2012 		rtnl_unlock();
2013 		return -EBUSY;
2014 	}
2015 
2016 	tx_sa = kmalloc_obj(*tx_sa);
2017 	if (!tx_sa) {
2018 		rtnl_unlock();
2019 		return -ENOMEM;
2020 	}
2021 
2022 	err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2023 			 secy->key_len, secy->icv_len);
2024 	if (err < 0) {
2025 		kfree(tx_sa);
2026 		rtnl_unlock();
2027 		return err;
2028 	}
2029 
2030 	spin_lock_bh(&tx_sa->lock);
2031 	tx_sa->next_pn = nla_get_uint(tb_sa[MACSEC_SA_ATTR_PN]);
2032 	spin_unlock_bh(&tx_sa->lock);
2033 
2034 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2035 		tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2036 
2037 	was_operational = secy->operational;
2038 	if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
2039 		secy->operational = true;
2040 
2041 	if (secy->xpn) {
2042 		tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
2043 		nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
2044 			   MACSEC_SALT_LEN);
2045 	}
2046 
2047 	/* If h/w offloading is available, propagate to the device */
2048 	if (macsec_is_offloaded(netdev_priv(dev))) {
2049 		const struct macsec_ops *ops;
2050 		struct macsec_context ctx;
2051 
2052 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2053 		if (!ops) {
2054 			err = -EOPNOTSUPP;
2055 			goto cleanup;
2056 		}
2057 
2058 		ctx.sa.assoc_num = assoc_num;
2059 		ctx.sa.tx_sa = tx_sa;
2060 		ctx.secy = secy;
2061 		memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2062 		       secy->key_len);
2063 
2064 		err = macsec_offload(ops->mdo_add_txsa, &ctx);
2065 		memzero_explicit(ctx.sa.key, secy->key_len);
2066 		if (err)
2067 			goto cleanup;
2068 	}
2069 
2070 	nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
2071 	rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
2072 
2073 	rtnl_unlock();
2074 
2075 	return 0;
2076 
2077 cleanup:
2078 	secy->operational = was_operational;
2079 	macsec_txsa_put(tx_sa);
2080 	rtnl_unlock();
2081 	return err;
2082 }
2083 
2084 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
2085 {
2086 	struct nlattr **attrs = info->attrs;
2087 	struct net_device *dev;
2088 	struct macsec_secy *secy;
2089 	struct macsec_rx_sc *rx_sc;
2090 	struct macsec_rx_sa *rx_sa;
2091 	u8 assoc_num;
2092 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2093 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2094 	int ret;
2095 
2096 	if (!attrs[MACSEC_ATTR_IFINDEX])
2097 		return -EINVAL;
2098 
2099 	if (parse_sa_config(attrs, tb_sa))
2100 		return -EINVAL;
2101 
2102 	if (parse_rxsc_config(attrs, tb_rxsc))
2103 		return -EINVAL;
2104 
2105 	rtnl_lock();
2106 	rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2107 				 &dev, &secy, &rx_sc, &assoc_num);
2108 	if (IS_ERR(rx_sa)) {
2109 		rtnl_unlock();
2110 		return PTR_ERR(rx_sa);
2111 	}
2112 
2113 	if (rx_sa->active) {
2114 		rtnl_unlock();
2115 		return -EBUSY;
2116 	}
2117 
2118 	/* If h/w offloading is available, propagate to the device */
2119 	if (macsec_is_offloaded(netdev_priv(dev))) {
2120 		const struct macsec_ops *ops;
2121 		struct macsec_context ctx;
2122 
2123 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2124 		if (!ops) {
2125 			ret = -EOPNOTSUPP;
2126 			goto cleanup;
2127 		}
2128 
2129 		ctx.sa.assoc_num = assoc_num;
2130 		ctx.sa.rx_sa = rx_sa;
2131 		ctx.secy = secy;
2132 
2133 		ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
2134 		if (ret)
2135 			goto cleanup;
2136 	}
2137 
2138 	RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
2139 	clear_rx_sa(rx_sa);
2140 
2141 	rtnl_unlock();
2142 
2143 	return 0;
2144 
2145 cleanup:
2146 	rtnl_unlock();
2147 	return ret;
2148 }
2149 
2150 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
2151 {
2152 	struct nlattr **attrs = info->attrs;
2153 	struct net_device *dev;
2154 	struct macsec_secy *secy;
2155 	struct macsec_rx_sc *rx_sc;
2156 	sci_t sci;
2157 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2158 	int ret;
2159 
2160 	if (!attrs[MACSEC_ATTR_IFINDEX])
2161 		return -EINVAL;
2162 
2163 	if (parse_rxsc_config(attrs, tb_rxsc))
2164 		return -EINVAL;
2165 
2166 	if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2167 		return -EINVAL;
2168 
2169 	rtnl_lock();
2170 	dev = get_dev_from_nl(genl_info_net(info), info->attrs);
2171 	if (IS_ERR(dev)) {
2172 		rtnl_unlock();
2173 		return PTR_ERR(dev);
2174 	}
2175 
2176 	secy = &macsec_priv(dev)->secy;
2177 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2178 
2179 	rx_sc = del_rx_sc(secy, sci);
2180 	if (!rx_sc) {
2181 		rtnl_unlock();
2182 		return -ENODEV;
2183 	}
2184 
2185 	/* If h/w offloading is available, propagate to the device */
2186 	if (macsec_is_offloaded(netdev_priv(dev))) {
2187 		const struct macsec_ops *ops;
2188 		struct macsec_context ctx;
2189 
2190 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2191 		if (!ops) {
2192 			ret = -EOPNOTSUPP;
2193 			goto cleanup;
2194 		}
2195 
2196 		ctx.rx_sc = rx_sc;
2197 		ctx.secy = secy;
2198 		ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
2199 		if (ret)
2200 			goto cleanup;
2201 	}
2202 
2203 	free_rx_sc(rx_sc);
2204 	rtnl_unlock();
2205 
2206 	return 0;
2207 
2208 cleanup:
2209 	rtnl_unlock();
2210 	return ret;
2211 }
2212 
2213 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2214 {
2215 	struct nlattr **attrs = info->attrs;
2216 	struct net_device *dev;
2217 	struct macsec_secy *secy;
2218 	struct macsec_tx_sc *tx_sc;
2219 	struct macsec_tx_sa *tx_sa;
2220 	u8 assoc_num;
2221 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2222 	int ret;
2223 
2224 	if (!attrs[MACSEC_ATTR_IFINDEX])
2225 		return -EINVAL;
2226 
2227 	if (parse_sa_config(attrs, tb_sa))
2228 		return -EINVAL;
2229 
2230 	rtnl_lock();
2231 	tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2232 				 &dev, &secy, &tx_sc, &assoc_num);
2233 	if (IS_ERR(tx_sa)) {
2234 		rtnl_unlock();
2235 		return PTR_ERR(tx_sa);
2236 	}
2237 
2238 	if (tx_sa->active) {
2239 		rtnl_unlock();
2240 		return -EBUSY;
2241 	}
2242 
2243 	/* If h/w offloading is available, propagate to the device */
2244 	if (macsec_is_offloaded(netdev_priv(dev))) {
2245 		const struct macsec_ops *ops;
2246 		struct macsec_context ctx;
2247 
2248 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2249 		if (!ops) {
2250 			ret = -EOPNOTSUPP;
2251 			goto cleanup;
2252 		}
2253 
2254 		ctx.sa.assoc_num = assoc_num;
2255 		ctx.sa.tx_sa = tx_sa;
2256 		ctx.secy = secy;
2257 
2258 		ret = macsec_offload(ops->mdo_del_txsa, &ctx);
2259 		if (ret)
2260 			goto cleanup;
2261 	}
2262 
2263 	RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2264 	clear_tx_sa(tx_sa);
2265 
2266 	rtnl_unlock();
2267 
2268 	return 0;
2269 
2270 cleanup:
2271 	rtnl_unlock();
2272 	return ret;
2273 }
2274 
2275 static bool validate_upd_sa(struct nlattr **attrs)
2276 {
2277 	if (!attrs[MACSEC_SA_ATTR_AN] ||
2278 	    attrs[MACSEC_SA_ATTR_KEY] ||
2279 	    attrs[MACSEC_SA_ATTR_KEYID] ||
2280 	    attrs[MACSEC_SA_ATTR_SSCI] ||
2281 	    attrs[MACSEC_SA_ATTR_SALT])
2282 		return false;
2283 
2284 	return true;
2285 }
2286 
2287 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2288 {
2289 	struct nlattr **attrs = info->attrs;
2290 	struct net_device *dev;
2291 	struct macsec_secy *secy;
2292 	struct macsec_tx_sc *tx_sc;
2293 	struct macsec_tx_sa *tx_sa;
2294 	u8 assoc_num;
2295 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2296 	bool was_operational, was_active;
2297 	pn_t prev_pn;
2298 	int ret = 0;
2299 
2300 	prev_pn.full64 = 0;
2301 
2302 	if (!attrs[MACSEC_ATTR_IFINDEX])
2303 		return -EINVAL;
2304 
2305 	if (parse_sa_config(attrs, tb_sa))
2306 		return -EINVAL;
2307 
2308 	if (!validate_upd_sa(tb_sa))
2309 		return -EINVAL;
2310 
2311 	rtnl_lock();
2312 	tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2313 				 &dev, &secy, &tx_sc, &assoc_num);
2314 	if (IS_ERR(tx_sa)) {
2315 		rtnl_unlock();
2316 		return PTR_ERR(tx_sa);
2317 	}
2318 
2319 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2320 		int pn_len;
2321 
2322 		pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2323 		if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2324 			pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
2325 				  nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2326 			rtnl_unlock();
2327 			return -EINVAL;
2328 		}
2329 
2330 		spin_lock_bh(&tx_sa->lock);
2331 		prev_pn = tx_sa->next_pn_halves;
2332 		tx_sa->next_pn = nla_get_uint(tb_sa[MACSEC_SA_ATTR_PN]);
2333 		spin_unlock_bh(&tx_sa->lock);
2334 	}
2335 
2336 	was_active = tx_sa->active;
2337 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2338 		tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2339 
2340 	was_operational = secy->operational;
2341 	if (assoc_num == tx_sc->encoding_sa)
2342 		secy->operational = tx_sa->active;
2343 
2344 	/* If h/w offloading is available, propagate to the device */
2345 	if (macsec_is_offloaded(netdev_priv(dev))) {
2346 		const struct macsec_ops *ops;
2347 		struct macsec_context ctx;
2348 
2349 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2350 		if (!ops) {
2351 			ret = -EOPNOTSUPP;
2352 			goto cleanup;
2353 		}
2354 
2355 		ctx.sa.assoc_num = assoc_num;
2356 		ctx.sa.tx_sa = tx_sa;
2357 		ctx.sa.update_pn = !!prev_pn.full64;
2358 		ctx.secy = secy;
2359 
2360 		ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
2361 		if (ret)
2362 			goto cleanup;
2363 	}
2364 
2365 	rtnl_unlock();
2366 
2367 	return 0;
2368 
2369 cleanup:
2370 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2371 		spin_lock_bh(&tx_sa->lock);
2372 		tx_sa->next_pn_halves = prev_pn;
2373 		spin_unlock_bh(&tx_sa->lock);
2374 	}
2375 	tx_sa->active = was_active;
2376 	secy->operational = was_operational;
2377 	rtnl_unlock();
2378 	return ret;
2379 }
2380 
2381 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2382 {
2383 	struct nlattr **attrs = info->attrs;
2384 	struct net_device *dev;
2385 	struct macsec_secy *secy;
2386 	struct macsec_rx_sc *rx_sc;
2387 	struct macsec_rx_sa *rx_sa;
2388 	u8 assoc_num;
2389 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2390 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2391 	bool was_active;
2392 	pn_t prev_pn;
2393 	int ret = 0;
2394 
2395 	prev_pn.full64 = 0;
2396 
2397 	if (!attrs[MACSEC_ATTR_IFINDEX])
2398 		return -EINVAL;
2399 
2400 	if (parse_rxsc_config(attrs, tb_rxsc))
2401 		return -EINVAL;
2402 
2403 	if (parse_sa_config(attrs, tb_sa))
2404 		return -EINVAL;
2405 
2406 	if (!validate_upd_sa(tb_sa))
2407 		return -EINVAL;
2408 
2409 	rtnl_lock();
2410 	rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2411 				 &dev, &secy, &rx_sc, &assoc_num);
2412 	if (IS_ERR(rx_sa)) {
2413 		rtnl_unlock();
2414 		return PTR_ERR(rx_sa);
2415 	}
2416 
2417 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2418 		int pn_len;
2419 
2420 		pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2421 		if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2422 			pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
2423 				  nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2424 			rtnl_unlock();
2425 			return -EINVAL;
2426 		}
2427 
2428 		spin_lock_bh(&rx_sa->lock);
2429 		prev_pn = rx_sa->next_pn_halves;
2430 		rx_sa->next_pn = nla_get_uint(tb_sa[MACSEC_SA_ATTR_PN]);
2431 		spin_unlock_bh(&rx_sa->lock);
2432 	}
2433 
2434 	was_active = rx_sa->active;
2435 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2436 		rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2437 
2438 	/* If h/w offloading is available, propagate to the device */
2439 	if (macsec_is_offloaded(netdev_priv(dev))) {
2440 		const struct macsec_ops *ops;
2441 		struct macsec_context ctx;
2442 
2443 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2444 		if (!ops) {
2445 			ret = -EOPNOTSUPP;
2446 			goto cleanup;
2447 		}
2448 
2449 		ctx.sa.assoc_num = assoc_num;
2450 		ctx.sa.rx_sa = rx_sa;
2451 		ctx.sa.update_pn = !!prev_pn.full64;
2452 		ctx.secy = secy;
2453 
2454 		ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
2455 		if (ret)
2456 			goto cleanup;
2457 	}
2458 
2459 	rtnl_unlock();
2460 	return 0;
2461 
2462 cleanup:
2463 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2464 		spin_lock_bh(&rx_sa->lock);
2465 		rx_sa->next_pn_halves = prev_pn;
2466 		spin_unlock_bh(&rx_sa->lock);
2467 	}
2468 	rx_sa->active = was_active;
2469 	rtnl_unlock();
2470 	return ret;
2471 }
2472 
2473 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2474 {
2475 	struct nlattr **attrs = info->attrs;
2476 	struct net_device *dev;
2477 	struct macsec_secy *secy;
2478 	struct macsec_rx_sc *rx_sc;
2479 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2480 	unsigned int prev_n_rx_sc;
2481 	bool was_active;
2482 	int ret;
2483 
2484 	if (!attrs[MACSEC_ATTR_IFINDEX])
2485 		return -EINVAL;
2486 
2487 	if (parse_rxsc_config(attrs, tb_rxsc))
2488 		return -EINVAL;
2489 
2490 	if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2491 		return -EINVAL;
2492 
2493 	rtnl_lock();
2494 	rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2495 	if (IS_ERR(rx_sc)) {
2496 		rtnl_unlock();
2497 		return PTR_ERR(rx_sc);
2498 	}
2499 
2500 	was_active = rx_sc->active;
2501 	prev_n_rx_sc = secy->n_rx_sc;
2502 	if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2503 		bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2504 
2505 		if (rx_sc->active != new)
2506 			secy->n_rx_sc += new ? 1 : -1;
2507 
2508 		rx_sc->active = new;
2509 	}
2510 
2511 	/* If h/w offloading is available, propagate to the device */
2512 	if (macsec_is_offloaded(netdev_priv(dev))) {
2513 		const struct macsec_ops *ops;
2514 		struct macsec_context ctx;
2515 
2516 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2517 		if (!ops) {
2518 			ret = -EOPNOTSUPP;
2519 			goto cleanup;
2520 		}
2521 
2522 		ctx.rx_sc = rx_sc;
2523 		ctx.secy = secy;
2524 
2525 		ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
2526 		if (ret)
2527 			goto cleanup;
2528 	}
2529 
2530 	rtnl_unlock();
2531 
2532 	return 0;
2533 
2534 cleanup:
2535 	secy->n_rx_sc = prev_n_rx_sc;
2536 	rx_sc->active = was_active;
2537 	rtnl_unlock();
2538 	return ret;
2539 }
2540 
2541 static bool macsec_is_configured(struct macsec_dev *macsec)
2542 {
2543 	struct macsec_secy *secy = &macsec->secy;
2544 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2545 	int i;
2546 
2547 	if (secy->rx_sc)
2548 		return true;
2549 
2550 	for (i = 0; i < MACSEC_NUM_AN; i++)
2551 		if (tx_sc->sa[i])
2552 			return true;
2553 
2554 	return false;
2555 }
2556 
2557 static bool macsec_needs_tx_tag(struct macsec_dev *macsec,
2558 				const struct macsec_ops *ops)
2559 {
2560 	return macsec->offload == MACSEC_OFFLOAD_PHY &&
2561 		ops->mdo_insert_tx_tag;
2562 }
2563 
2564 static void macsec_set_head_tail_room(struct net_device *dev)
2565 {
2566 	struct macsec_dev *macsec = macsec_priv(dev);
2567 	struct net_device *real_dev = macsec->real_dev;
2568 	int needed_headroom, needed_tailroom;
2569 	const struct macsec_ops *ops;
2570 
2571 	ops = macsec_get_ops(macsec, NULL);
2572 	if (ops) {
2573 		needed_headroom = ops->needed_headroom;
2574 		needed_tailroom = ops->needed_tailroom;
2575 	} else {
2576 		needed_headroom = MACSEC_NEEDED_HEADROOM;
2577 		needed_tailroom = MACSEC_NEEDED_TAILROOM;
2578 	}
2579 
2580 	dev->needed_headroom = real_dev->needed_headroom + needed_headroom;
2581 	dev->needed_tailroom = real_dev->needed_tailroom + needed_tailroom;
2582 }
2583 
2584 static void macsec_inherit_tso_max(struct net_device *dev)
2585 {
2586 	struct macsec_dev *macsec = macsec_priv(dev);
2587 
2588 	/* if macsec is offloaded, we need to follow the lower
2589 	 * device's capabilities. otherwise, we can ignore them.
2590 	 */
2591 	if (macsec_is_offloaded(macsec))
2592 		netif_inherit_tso_max(dev, macsec->real_dev);
2593 }
2594 
2595 static int macsec_update_offload(struct net_device *dev,
2596 				 enum macsec_offload offload,
2597 				 struct netlink_ext_ack *extack)
2598 {
2599 	enum macsec_offload prev_offload;
2600 	const struct macsec_ops *ops;
2601 	struct macsec_context ctx;
2602 	struct macsec_dev *macsec;
2603 	int ret = 0;
2604 
2605 	macsec = macsec_priv(dev);
2606 
2607 	/* Check if the offloading mode is supported by the underlying layers */
2608 	if (offload != MACSEC_OFFLOAD_OFF &&
2609 	    !macsec_check_offload(offload, macsec))
2610 		return -EOPNOTSUPP;
2611 
2612 	/* Check if the net device is busy. */
2613 	if (netif_running(dev))
2614 		return -EBUSY;
2615 
2616 	/* Check if the device already has rules configured: we do not support
2617 	 * rules migration.
2618 	 */
2619 	if (macsec_is_configured(macsec))
2620 		return -EBUSY;
2621 
2622 	prev_offload = macsec->offload;
2623 
2624 	ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
2625 			       macsec, &ctx);
2626 	if (!ops)
2627 		return -EOPNOTSUPP;
2628 
2629 	ctx.secy = &macsec->secy;
2630 	ret = offload == MACSEC_OFFLOAD_OFF ? macsec_offload(ops->mdo_del_secy, &ctx)
2631 					    : macsec_offload(ops->mdo_add_secy, &ctx);
2632 	if (ret)
2633 		return ret;
2634 
2635 	/* Remove VLAN filters when disabling offload. */
2636 	if (offload == MACSEC_OFFLOAD_OFF) {
2637 		vlan_drop_rx_ctag_filter_info(dev);
2638 		vlan_drop_rx_stag_filter_info(dev);
2639 	}
2640 	WRITE_ONCE(macsec->offload, offload);
2641 	/* Add VLAN filters when enabling offload. */
2642 	if (prev_offload == MACSEC_OFFLOAD_OFF) {
2643 		ret = vlan_get_rx_ctag_filter_info(dev);
2644 		if (ret) {
2645 			NL_SET_ERR_MSG_FMT(extack,
2646 					   "adding ctag VLAN filters failed, err %d",
2647 					   ret);
2648 			goto rollback_offload;
2649 		}
2650 		ret = vlan_get_rx_stag_filter_info(dev);
2651 		if (ret) {
2652 			NL_SET_ERR_MSG_FMT(extack,
2653 					   "adding stag VLAN filters failed, err %d",
2654 					   ret);
2655 			vlan_drop_rx_ctag_filter_info(dev);
2656 			goto rollback_offload;
2657 		}
2658 	}
2659 
2660 	macsec_set_head_tail_room(dev);
2661 	macsec->insert_tx_tag = macsec_needs_tx_tag(macsec, ops);
2662 
2663 	macsec_inherit_tso_max(dev);
2664 
2665 	netdev_update_features(dev);
2666 
2667 	return 0;
2668 
2669 rollback_offload:
2670 	WRITE_ONCE(macsec->offload, prev_offload);
2671 	macsec_offload(ops->mdo_del_secy, &ctx);
2672 
2673 	return ret;
2674 }
2675 
2676 static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
2677 {
2678 	struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
2679 	struct nlattr **attrs = info->attrs;
2680 	enum macsec_offload offload;
2681 	struct macsec_dev *macsec;
2682 	struct net_device *dev;
2683 	int ret = 0;
2684 
2685 	if (!attrs[MACSEC_ATTR_IFINDEX])
2686 		return -EINVAL;
2687 
2688 	if (!attrs[MACSEC_ATTR_OFFLOAD])
2689 		return -EINVAL;
2690 
2691 	if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
2692 					attrs[MACSEC_ATTR_OFFLOAD],
2693 					macsec_genl_offload_policy, NULL))
2694 		return -EINVAL;
2695 
2696 	rtnl_lock();
2697 
2698 	dev = get_dev_from_nl(genl_info_net(info), attrs);
2699 	if (IS_ERR(dev)) {
2700 		ret = PTR_ERR(dev);
2701 		goto out;
2702 	}
2703 	macsec = macsec_priv(dev);
2704 
2705 	if (!tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]) {
2706 		ret = -EINVAL;
2707 		goto out;
2708 	}
2709 
2710 	offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
2711 
2712 	if (macsec->offload != offload)
2713 		ret = macsec_update_offload(dev, offload, info->extack);
2714 out:
2715 	rtnl_unlock();
2716 	return ret;
2717 }
2718 
2719 static void get_tx_sa_stats(struct net_device *dev, int an,
2720 			    struct macsec_tx_sa *tx_sa,
2721 			    struct macsec_tx_sa_stats *sum)
2722 {
2723 	struct macsec_dev *macsec = macsec_priv(dev);
2724 	int cpu;
2725 
2726 	/* If h/w offloading is available, propagate to the device */
2727 	if (macsec_is_offloaded(macsec)) {
2728 		const struct macsec_ops *ops;
2729 		struct macsec_context ctx;
2730 
2731 		ops = macsec_get_ops(macsec, &ctx);
2732 		if (ops) {
2733 			ctx.sa.assoc_num = an;
2734 			ctx.sa.tx_sa = tx_sa;
2735 			ctx.stats.tx_sa_stats = sum;
2736 			ctx.secy = &macsec_priv(dev)->secy;
2737 			macsec_offload(ops->mdo_get_tx_sa_stats, &ctx);
2738 		}
2739 		return;
2740 	}
2741 
2742 	for_each_possible_cpu(cpu) {
2743 		const struct macsec_tx_sa_stats *stats =
2744 			per_cpu_ptr(tx_sa->stats, cpu);
2745 
2746 		sum->OutPktsProtected += stats->OutPktsProtected;
2747 		sum->OutPktsEncrypted += stats->OutPktsEncrypted;
2748 	}
2749 }
2750 
2751 static int copy_tx_sa_stats(struct sk_buff *skb, struct macsec_tx_sa_stats *sum)
2752 {
2753 	if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED,
2754 			sum->OutPktsProtected) ||
2755 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2756 			sum->OutPktsEncrypted))
2757 		return -EMSGSIZE;
2758 
2759 	return 0;
2760 }
2761 
2762 static void get_rx_sa_stats(struct net_device *dev,
2763 			    struct macsec_rx_sc *rx_sc, int an,
2764 			    struct macsec_rx_sa *rx_sa,
2765 			    struct macsec_rx_sa_stats *sum)
2766 {
2767 	struct macsec_dev *macsec = macsec_priv(dev);
2768 	int cpu;
2769 
2770 	/* If h/w offloading is available, propagate to the device */
2771 	if (macsec_is_offloaded(macsec)) {
2772 		const struct macsec_ops *ops;
2773 		struct macsec_context ctx;
2774 
2775 		ops = macsec_get_ops(macsec, &ctx);
2776 		if (ops) {
2777 			ctx.sa.assoc_num = an;
2778 			ctx.sa.rx_sa = rx_sa;
2779 			ctx.stats.rx_sa_stats = sum;
2780 			ctx.secy = &macsec_priv(dev)->secy;
2781 			ctx.rx_sc = rx_sc;
2782 			macsec_offload(ops->mdo_get_rx_sa_stats, &ctx);
2783 		}
2784 		return;
2785 	}
2786 
2787 	for_each_possible_cpu(cpu) {
2788 		const struct macsec_rx_sa_stats *stats =
2789 			per_cpu_ptr(rx_sa->stats, cpu);
2790 
2791 		sum->InPktsOK         += stats->InPktsOK;
2792 		sum->InPktsInvalid    += stats->InPktsInvalid;
2793 		sum->InPktsNotValid   += stats->InPktsNotValid;
2794 		sum->InPktsNotUsingSA += stats->InPktsNotUsingSA;
2795 		sum->InPktsUnusedSA   += stats->InPktsUnusedSA;
2796 	}
2797 }
2798 
2799 static int copy_rx_sa_stats(struct sk_buff *skb,
2800 			    struct macsec_rx_sa_stats *sum)
2801 {
2802 	if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum->InPktsOK) ||
2803 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID,
2804 			sum->InPktsInvalid) ||
2805 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID,
2806 			sum->InPktsNotValid) ||
2807 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2808 			sum->InPktsNotUsingSA) ||
2809 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA,
2810 			sum->InPktsUnusedSA))
2811 		return -EMSGSIZE;
2812 
2813 	return 0;
2814 }
2815 
2816 static void get_rx_sc_stats(struct net_device *dev,
2817 			    struct macsec_rx_sc *rx_sc,
2818 			    struct macsec_rx_sc_stats *sum)
2819 {
2820 	struct macsec_dev *macsec = macsec_priv(dev);
2821 	int cpu;
2822 
2823 	/* If h/w offloading is available, propagate to the device */
2824 	if (macsec_is_offloaded(macsec)) {
2825 		const struct macsec_ops *ops;
2826 		struct macsec_context ctx;
2827 
2828 		ops = macsec_get_ops(macsec, &ctx);
2829 		if (ops) {
2830 			ctx.stats.rx_sc_stats = sum;
2831 			ctx.secy = &macsec_priv(dev)->secy;
2832 			ctx.rx_sc = rx_sc;
2833 			macsec_offload(ops->mdo_get_rx_sc_stats, &ctx);
2834 		}
2835 		return;
2836 	}
2837 
2838 	for_each_possible_cpu(cpu) {
2839 		const struct pcpu_rx_sc_stats *stats;
2840 		struct macsec_rx_sc_stats tmp;
2841 		unsigned int start;
2842 
2843 		stats = per_cpu_ptr(rx_sc->stats, cpu);
2844 		do {
2845 			start = u64_stats_fetch_begin(&stats->syncp);
2846 			u64_stats_copy(&tmp, &stats->stats, sizeof(tmp));
2847 		} while (u64_stats_fetch_retry(&stats->syncp, start));
2848 
2849 		sum->InOctetsValidated += tmp.InOctetsValidated;
2850 		sum->InOctetsDecrypted += tmp.InOctetsDecrypted;
2851 		sum->InPktsUnchecked   += tmp.InPktsUnchecked;
2852 		sum->InPktsDelayed     += tmp.InPktsDelayed;
2853 		sum->InPktsOK          += tmp.InPktsOK;
2854 		sum->InPktsInvalid     += tmp.InPktsInvalid;
2855 		sum->InPktsLate        += tmp.InPktsLate;
2856 		sum->InPktsNotValid    += tmp.InPktsNotValid;
2857 		sum->InPktsNotUsingSA  += tmp.InPktsNotUsingSA;
2858 		sum->InPktsUnusedSA    += tmp.InPktsUnusedSA;
2859 	}
2860 }
2861 
2862 static int copy_rx_sc_stats(struct sk_buff *skb, struct macsec_rx_sc_stats *sum)
2863 {
2864 	if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2865 			      sum->InOctetsValidated,
2866 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2867 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2868 			      sum->InOctetsDecrypted,
2869 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2870 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2871 			      sum->InPktsUnchecked,
2872 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2873 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2874 			      sum->InPktsDelayed,
2875 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2876 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2877 			      sum->InPktsOK,
2878 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2879 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2880 			      sum->InPktsInvalid,
2881 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2882 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2883 			      sum->InPktsLate,
2884 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2885 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2886 			      sum->InPktsNotValid,
2887 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2888 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2889 			      sum->InPktsNotUsingSA,
2890 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2891 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2892 			      sum->InPktsUnusedSA,
2893 			      MACSEC_RXSC_STATS_ATTR_PAD))
2894 		return -EMSGSIZE;
2895 
2896 	return 0;
2897 }
2898 
2899 static void get_tx_sc_stats(struct net_device *dev,
2900 			    struct macsec_tx_sc_stats *sum)
2901 {
2902 	struct macsec_dev *macsec = macsec_priv(dev);
2903 	int cpu;
2904 
2905 	/* If h/w offloading is available, propagate to the device */
2906 	if (macsec_is_offloaded(macsec)) {
2907 		const struct macsec_ops *ops;
2908 		struct macsec_context ctx;
2909 
2910 		ops = macsec_get_ops(macsec, &ctx);
2911 		if (ops) {
2912 			ctx.stats.tx_sc_stats = sum;
2913 			ctx.secy = &macsec_priv(dev)->secy;
2914 			macsec_offload(ops->mdo_get_tx_sc_stats, &ctx);
2915 		}
2916 		return;
2917 	}
2918 
2919 	for_each_possible_cpu(cpu) {
2920 		const struct pcpu_tx_sc_stats *stats;
2921 		struct macsec_tx_sc_stats tmp;
2922 		unsigned int start;
2923 
2924 		stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu);
2925 		do {
2926 			start = u64_stats_fetch_begin(&stats->syncp);
2927 			u64_stats_copy(&tmp, &stats->stats, sizeof(tmp));
2928 		} while (u64_stats_fetch_retry(&stats->syncp, start));
2929 
2930 		sum->OutPktsProtected   += tmp.OutPktsProtected;
2931 		sum->OutPktsEncrypted   += tmp.OutPktsEncrypted;
2932 		sum->OutOctetsProtected += tmp.OutOctetsProtected;
2933 		sum->OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2934 	}
2935 }
2936 
2937 static int copy_tx_sc_stats(struct sk_buff *skb, struct macsec_tx_sc_stats *sum)
2938 {
2939 	if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2940 			      sum->OutPktsProtected,
2941 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2942 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2943 			      sum->OutPktsEncrypted,
2944 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2945 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2946 			      sum->OutOctetsProtected,
2947 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2948 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2949 			      sum->OutOctetsEncrypted,
2950 			      MACSEC_TXSC_STATS_ATTR_PAD))
2951 		return -EMSGSIZE;
2952 
2953 	return 0;
2954 }
2955 
2956 static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum)
2957 {
2958 	struct macsec_dev *macsec = macsec_priv(dev);
2959 	int cpu;
2960 
2961 	/* If h/w offloading is available, propagate to the device */
2962 	if (macsec_is_offloaded(macsec)) {
2963 		const struct macsec_ops *ops;
2964 		struct macsec_context ctx;
2965 
2966 		ops = macsec_get_ops(macsec, &ctx);
2967 		if (ops) {
2968 			ctx.stats.dev_stats = sum;
2969 			ctx.secy = &macsec_priv(dev)->secy;
2970 			macsec_offload(ops->mdo_get_dev_stats, &ctx);
2971 		}
2972 		return;
2973 	}
2974 
2975 	for_each_possible_cpu(cpu) {
2976 		const struct pcpu_secy_stats *stats;
2977 		struct macsec_dev_stats tmp;
2978 		unsigned int start;
2979 
2980 		stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu);
2981 		do {
2982 			start = u64_stats_fetch_begin(&stats->syncp);
2983 			u64_stats_copy(&tmp, &stats->stats, sizeof(tmp));
2984 		} while (u64_stats_fetch_retry(&stats->syncp, start));
2985 
2986 		sum->OutPktsUntagged  += tmp.OutPktsUntagged;
2987 		sum->InPktsUntagged   += tmp.InPktsUntagged;
2988 		sum->OutPktsTooLong   += tmp.OutPktsTooLong;
2989 		sum->InPktsNoTag      += tmp.InPktsNoTag;
2990 		sum->InPktsBadTag     += tmp.InPktsBadTag;
2991 		sum->InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2992 		sum->InPktsNoSCI      += tmp.InPktsNoSCI;
2993 		sum->InPktsOverrun    += tmp.InPktsOverrun;
2994 	}
2995 }
2996 
2997 static int copy_secy_stats(struct sk_buff *skb, struct macsec_dev_stats *sum)
2998 {
2999 	if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
3000 			      sum->OutPktsUntagged,
3001 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3002 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
3003 			      sum->InPktsUntagged,
3004 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3005 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
3006 			      sum->OutPktsTooLong,
3007 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3008 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
3009 			      sum->InPktsNoTag,
3010 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3011 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
3012 			      sum->InPktsBadTag,
3013 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3014 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
3015 			      sum->InPktsUnknownSCI,
3016 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3017 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
3018 			      sum->InPktsNoSCI,
3019 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3020 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
3021 			      sum->InPktsOverrun,
3022 			      MACSEC_SECY_STATS_ATTR_PAD))
3023 		return -EMSGSIZE;
3024 
3025 	return 0;
3026 }
3027 
3028 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
3029 {
3030 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3031 	struct nlattr *secy_nest = nla_nest_start_noflag(skb,
3032 							 MACSEC_ATTR_SECY);
3033 	u64 csid;
3034 
3035 	if (!secy_nest)
3036 		return 1;
3037 
3038 	switch (secy->key_len) {
3039 	case MACSEC_GCM_AES_128_SAK_LEN:
3040 		csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
3041 		break;
3042 	case MACSEC_GCM_AES_256_SAK_LEN:
3043 		csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
3044 		break;
3045 	default:
3046 		goto cancel;
3047 	}
3048 
3049 	if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
3050 			MACSEC_SECY_ATTR_PAD) ||
3051 	    nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
3052 			      csid, MACSEC_SECY_ATTR_PAD) ||
3053 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
3054 	    nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
3055 	    nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
3056 	    nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
3057 	    nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
3058 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
3059 	    nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
3060 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
3061 	    nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
3062 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
3063 		goto cancel;
3064 
3065 	if (secy->replay_protect) {
3066 		if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
3067 			goto cancel;
3068 	}
3069 
3070 	nla_nest_end(skb, secy_nest);
3071 	return 0;
3072 
3073 cancel:
3074 	nla_nest_cancel(skb, secy_nest);
3075 	return 1;
3076 }
3077 
3078 static noinline_for_stack int
3079 dump_secy(struct macsec_secy *secy, struct net_device *dev,
3080 	  struct sk_buff *skb, struct netlink_callback *cb)
3081 {
3082 	struct macsec_tx_sc_stats tx_sc_stats = {0, };
3083 	struct macsec_tx_sa_stats tx_sa_stats = {0, };
3084 	struct macsec_rx_sc_stats rx_sc_stats = {0, };
3085 	struct macsec_rx_sa_stats rx_sa_stats = {0, };
3086 	struct macsec_dev *macsec = netdev_priv(dev);
3087 	struct macsec_dev_stats dev_stats = {0, };
3088 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3089 	struct nlattr *txsa_list, *rxsc_list;
3090 	struct macsec_rx_sc *rx_sc;
3091 	struct nlattr *attr;
3092 	void *hdr;
3093 	int i, j;
3094 
3095 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3096 			  &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
3097 	if (!hdr)
3098 		return -EMSGSIZE;
3099 
3100 	genl_dump_check_consistent(cb, hdr);
3101 
3102 	if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
3103 		goto nla_put_failure;
3104 
3105 	attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
3106 	if (!attr)
3107 		goto nla_put_failure;
3108 	if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
3109 		goto nla_put_failure;
3110 	nla_nest_end(skb, attr);
3111 
3112 	if (nla_put_secy(secy, skb))
3113 		goto nla_put_failure;
3114 
3115 	attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
3116 	if (!attr)
3117 		goto nla_put_failure;
3118 
3119 	get_tx_sc_stats(dev, &tx_sc_stats);
3120 	if (copy_tx_sc_stats(skb, &tx_sc_stats)) {
3121 		nla_nest_cancel(skb, attr);
3122 		goto nla_put_failure;
3123 	}
3124 	nla_nest_end(skb, attr);
3125 
3126 	attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
3127 	if (!attr)
3128 		goto nla_put_failure;
3129 	get_secy_stats(dev, &dev_stats);
3130 	if (copy_secy_stats(skb, &dev_stats)) {
3131 		nla_nest_cancel(skb, attr);
3132 		goto nla_put_failure;
3133 	}
3134 	nla_nest_end(skb, attr);
3135 
3136 	txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
3137 	if (!txsa_list)
3138 		goto nla_put_failure;
3139 	for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
3140 		struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
3141 		struct nlattr *txsa_nest;
3142 		u64 pn;
3143 		int pn_len;
3144 
3145 		if (!tx_sa)
3146 			continue;
3147 
3148 		txsa_nest = nla_nest_start_noflag(skb, j++);
3149 		if (!txsa_nest) {
3150 			nla_nest_cancel(skb, txsa_list);
3151 			goto nla_put_failure;
3152 		}
3153 
3154 		attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
3155 		if (!attr) {
3156 			nla_nest_cancel(skb, txsa_nest);
3157 			nla_nest_cancel(skb, txsa_list);
3158 			goto nla_put_failure;
3159 		}
3160 		memset(&tx_sa_stats, 0, sizeof(tx_sa_stats));
3161 		get_tx_sa_stats(dev, i, tx_sa, &tx_sa_stats);
3162 		if (copy_tx_sa_stats(skb, &tx_sa_stats)) {
3163 			nla_nest_cancel(skb, attr);
3164 			nla_nest_cancel(skb, txsa_nest);
3165 			nla_nest_cancel(skb, txsa_list);
3166 			goto nla_put_failure;
3167 		}
3168 		nla_nest_end(skb, attr);
3169 
3170 		if (secy->xpn) {
3171 			pn = tx_sa->next_pn;
3172 			pn_len = MACSEC_XPN_PN_LEN;
3173 		} else {
3174 			pn = tx_sa->next_pn_halves.lower;
3175 			pn_len = MACSEC_DEFAULT_PN_LEN;
3176 		}
3177 
3178 		if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3179 		    nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3180 		    nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
3181 		    (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) ||
3182 		    nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
3183 			nla_nest_cancel(skb, txsa_nest);
3184 			nla_nest_cancel(skb, txsa_list);
3185 			goto nla_put_failure;
3186 		}
3187 
3188 		nla_nest_end(skb, txsa_nest);
3189 	}
3190 	nla_nest_end(skb, txsa_list);
3191 
3192 	rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
3193 	if (!rxsc_list)
3194 		goto nla_put_failure;
3195 
3196 	j = 1;
3197 	for_each_rxsc_rtnl(secy, rx_sc) {
3198 		int k;
3199 		struct nlattr *rxsa_list;
3200 		struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
3201 
3202 		if (!rxsc_nest) {
3203 			nla_nest_cancel(skb, rxsc_list);
3204 			goto nla_put_failure;
3205 		}
3206 
3207 		if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
3208 		    nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
3209 				MACSEC_RXSC_ATTR_PAD)) {
3210 			nla_nest_cancel(skb, rxsc_nest);
3211 			nla_nest_cancel(skb, rxsc_list);
3212 			goto nla_put_failure;
3213 		}
3214 
3215 		attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
3216 		if (!attr) {
3217 			nla_nest_cancel(skb, rxsc_nest);
3218 			nla_nest_cancel(skb, rxsc_list);
3219 			goto nla_put_failure;
3220 		}
3221 		memset(&rx_sc_stats, 0, sizeof(rx_sc_stats));
3222 		get_rx_sc_stats(dev, rx_sc, &rx_sc_stats);
3223 		if (copy_rx_sc_stats(skb, &rx_sc_stats)) {
3224 			nla_nest_cancel(skb, attr);
3225 			nla_nest_cancel(skb, rxsc_nest);
3226 			nla_nest_cancel(skb, rxsc_list);
3227 			goto nla_put_failure;
3228 		}
3229 		nla_nest_end(skb, attr);
3230 
3231 		rxsa_list = nla_nest_start_noflag(skb,
3232 						  MACSEC_RXSC_ATTR_SA_LIST);
3233 		if (!rxsa_list) {
3234 			nla_nest_cancel(skb, rxsc_nest);
3235 			nla_nest_cancel(skb, rxsc_list);
3236 			goto nla_put_failure;
3237 		}
3238 
3239 		for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
3240 			struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
3241 			struct nlattr *rxsa_nest;
3242 			u64 pn;
3243 			int pn_len;
3244 
3245 			if (!rx_sa)
3246 				continue;
3247 
3248 			rxsa_nest = nla_nest_start_noflag(skb, k++);
3249 			if (!rxsa_nest) {
3250 				nla_nest_cancel(skb, rxsa_list);
3251 				nla_nest_cancel(skb, rxsc_nest);
3252 				nla_nest_cancel(skb, rxsc_list);
3253 				goto nla_put_failure;
3254 			}
3255 
3256 			attr = nla_nest_start_noflag(skb,
3257 						     MACSEC_SA_ATTR_STATS);
3258 			if (!attr) {
3259 				nla_nest_cancel(skb, rxsa_list);
3260 				nla_nest_cancel(skb, rxsc_nest);
3261 				nla_nest_cancel(skb, rxsc_list);
3262 				goto nla_put_failure;
3263 			}
3264 			memset(&rx_sa_stats, 0, sizeof(rx_sa_stats));
3265 			get_rx_sa_stats(dev, rx_sc, i, rx_sa, &rx_sa_stats);
3266 			if (copy_rx_sa_stats(skb, &rx_sa_stats)) {
3267 				nla_nest_cancel(skb, attr);
3268 				nla_nest_cancel(skb, rxsa_list);
3269 				nla_nest_cancel(skb, rxsc_nest);
3270 				nla_nest_cancel(skb, rxsc_list);
3271 				goto nla_put_failure;
3272 			}
3273 			nla_nest_end(skb, attr);
3274 
3275 			if (secy->xpn) {
3276 				pn = rx_sa->next_pn;
3277 				pn_len = MACSEC_XPN_PN_LEN;
3278 			} else {
3279 				pn = rx_sa->next_pn_halves.lower;
3280 				pn_len = MACSEC_DEFAULT_PN_LEN;
3281 			}
3282 
3283 			if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3284 			    nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3285 			    nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
3286 			    (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) ||
3287 			    nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
3288 				nla_nest_cancel(skb, rxsa_nest);
3289 				nla_nest_cancel(skb, rxsc_nest);
3290 				nla_nest_cancel(skb, rxsc_list);
3291 				goto nla_put_failure;
3292 			}
3293 			nla_nest_end(skb, rxsa_nest);
3294 		}
3295 
3296 		nla_nest_end(skb, rxsa_list);
3297 		nla_nest_end(skb, rxsc_nest);
3298 	}
3299 
3300 	nla_nest_end(skb, rxsc_list);
3301 
3302 	genlmsg_end(skb, hdr);
3303 
3304 	return 0;
3305 
3306 nla_put_failure:
3307 	genlmsg_cancel(skb, hdr);
3308 	return -EMSGSIZE;
3309 }
3310 
3311 static int macsec_generation = 1; /* protected by RTNL */
3312 
3313 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
3314 {
3315 	struct net *net = sock_net(skb->sk);
3316 	struct net_device *dev;
3317 	int dev_idx, d;
3318 
3319 	dev_idx = cb->args[0];
3320 
3321 	d = 0;
3322 	rtnl_lock();
3323 
3324 	cb->seq = macsec_generation;
3325 
3326 	for_each_netdev(net, dev) {
3327 		struct macsec_secy *secy;
3328 
3329 		if (d < dev_idx)
3330 			goto next;
3331 
3332 		if (!netif_is_macsec(dev))
3333 			goto next;
3334 
3335 		secy = &macsec_priv(dev)->secy;
3336 		if (dump_secy(secy, dev, skb, cb) < 0)
3337 			goto done;
3338 next:
3339 		d++;
3340 	}
3341 
3342 done:
3343 	rtnl_unlock();
3344 	cb->args[0] = d;
3345 	return skb->len;
3346 }
3347 
3348 static const struct genl_small_ops macsec_genl_ops[] = {
3349 	{
3350 		.cmd = MACSEC_CMD_GET_TXSC,
3351 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3352 		.dumpit = macsec_dump_txsc,
3353 	},
3354 	{
3355 		.cmd = MACSEC_CMD_ADD_RXSC,
3356 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3357 		.doit = macsec_add_rxsc,
3358 		.flags = GENL_ADMIN_PERM,
3359 	},
3360 	{
3361 		.cmd = MACSEC_CMD_DEL_RXSC,
3362 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3363 		.doit = macsec_del_rxsc,
3364 		.flags = GENL_ADMIN_PERM,
3365 	},
3366 	{
3367 		.cmd = MACSEC_CMD_UPD_RXSC,
3368 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3369 		.doit = macsec_upd_rxsc,
3370 		.flags = GENL_ADMIN_PERM,
3371 	},
3372 	{
3373 		.cmd = MACSEC_CMD_ADD_TXSA,
3374 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3375 		.doit = macsec_add_txsa,
3376 		.flags = GENL_ADMIN_PERM,
3377 	},
3378 	{
3379 		.cmd = MACSEC_CMD_DEL_TXSA,
3380 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3381 		.doit = macsec_del_txsa,
3382 		.flags = GENL_ADMIN_PERM,
3383 	},
3384 	{
3385 		.cmd = MACSEC_CMD_UPD_TXSA,
3386 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3387 		.doit = macsec_upd_txsa,
3388 		.flags = GENL_ADMIN_PERM,
3389 	},
3390 	{
3391 		.cmd = MACSEC_CMD_ADD_RXSA,
3392 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3393 		.doit = macsec_add_rxsa,
3394 		.flags = GENL_ADMIN_PERM,
3395 	},
3396 	{
3397 		.cmd = MACSEC_CMD_DEL_RXSA,
3398 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3399 		.doit = macsec_del_rxsa,
3400 		.flags = GENL_ADMIN_PERM,
3401 	},
3402 	{
3403 		.cmd = MACSEC_CMD_UPD_RXSA,
3404 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3405 		.doit = macsec_upd_rxsa,
3406 		.flags = GENL_ADMIN_PERM,
3407 	},
3408 	{
3409 		.cmd = MACSEC_CMD_UPD_OFFLOAD,
3410 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3411 		.doit = macsec_upd_offload,
3412 		.flags = GENL_ADMIN_PERM,
3413 	},
3414 };
3415 
3416 static struct genl_family macsec_fam __ro_after_init = {
3417 	.name		= MACSEC_GENL_NAME,
3418 	.hdrsize	= 0,
3419 	.version	= MACSEC_GENL_VERSION,
3420 	.maxattr	= MACSEC_ATTR_MAX,
3421 	.policy = macsec_genl_policy,
3422 	.netnsok	= true,
3423 	.module		= THIS_MODULE,
3424 	.small_ops	= macsec_genl_ops,
3425 	.n_small_ops	= ARRAY_SIZE(macsec_genl_ops),
3426 	.resv_start_op	= MACSEC_CMD_UPD_OFFLOAD + 1,
3427 };
3428 
3429 static struct sk_buff *macsec_insert_tx_tag(struct sk_buff *skb,
3430 					    struct net_device *dev)
3431 {
3432 	struct macsec_dev *macsec = macsec_priv(dev);
3433 	const struct macsec_ops *ops;
3434 	struct phy_device *phydev;
3435 	struct macsec_context ctx;
3436 	int skb_final_len;
3437 	int err;
3438 
3439 	ops = macsec_get_ops(macsec, &ctx);
3440 	skb_final_len = skb->len - ETH_HLEN + ops->needed_headroom +
3441 		ops->needed_tailroom;
3442 	if (unlikely(skb_final_len > macsec->real_dev->mtu)) {
3443 		err = -EINVAL;
3444 		goto cleanup;
3445 	}
3446 
3447 	phydev = macsec->real_dev->phydev;
3448 
3449 	err = skb_ensure_writable_head_tail(skb, dev);
3450 	if (unlikely(err < 0))
3451 		goto cleanup;
3452 
3453 	err = ops->mdo_insert_tx_tag(phydev, skb);
3454 	if (unlikely(err))
3455 		goto cleanup;
3456 
3457 	return skb;
3458 cleanup:
3459 	kfree_skb(skb);
3460 	return ERR_PTR(err);
3461 }
3462 
3463 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
3464 				     struct net_device *dev)
3465 {
3466 	struct macsec_dev *macsec = netdev_priv(dev);
3467 	struct macsec_secy *secy = &macsec->secy;
3468 	struct pcpu_secy_stats *secy_stats;
3469 	int ret, len;
3470 
3471 	if (macsec_is_offloaded(netdev_priv(dev))) {
3472 		struct metadata_dst *md_dst = secy->tx_sc.md_dst;
3473 
3474 		skb_dst_drop(skb);
3475 		dst_hold(&md_dst->dst);
3476 		skb_dst_set(skb, &md_dst->dst);
3477 
3478 		if (macsec->insert_tx_tag) {
3479 			skb = macsec_insert_tx_tag(skb, dev);
3480 			if (IS_ERR(skb)) {
3481 				DEV_STATS_INC(dev, tx_dropped);
3482 				return NETDEV_TX_OK;
3483 			}
3484 		}
3485 
3486 		skb->dev = macsec->real_dev;
3487 		return dev_queue_xmit(skb);
3488 	}
3489 
3490 	/* 10.5 */
3491 	if (!secy->protect_frames) {
3492 		secy_stats = this_cpu_ptr(macsec->stats);
3493 		u64_stats_update_begin(&secy_stats->syncp);
3494 		secy_stats->stats.OutPktsUntagged++;
3495 		u64_stats_update_end(&secy_stats->syncp);
3496 		skb->dev = macsec->real_dev;
3497 		len = skb->len;
3498 		ret = dev_queue_xmit(skb);
3499 		count_tx(dev, ret, len);
3500 		return ret;
3501 	}
3502 
3503 	if (!secy->operational) {
3504 		kfree_skb(skb);
3505 		DEV_STATS_INC(dev, tx_dropped);
3506 		return NETDEV_TX_OK;
3507 	}
3508 
3509 	len = skb->len;
3510 	skb = macsec_encrypt(skb, dev);
3511 	if (IS_ERR(skb)) {
3512 		if (PTR_ERR(skb) != -EINPROGRESS)
3513 			DEV_STATS_INC(dev, tx_dropped);
3514 		return NETDEV_TX_OK;
3515 	}
3516 
3517 	macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
3518 
3519 	macsec_encrypt_finish(skb, dev);
3520 	ret = dev_queue_xmit(skb);
3521 	count_tx(dev, ret, len);
3522 	return ret;
3523 }
3524 
3525 #define MACSEC_FEATURES \
3526 	(NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
3527 	 NETIF_F_HW_VLAN_STAG_FILTER | NETIF_F_HW_VLAN_CTAG_FILTER)
3528 
3529 #define MACSEC_OFFLOAD_FEATURES \
3530 	(MACSEC_FEATURES | NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES | \
3531 	 NETIF_F_LRO | NETIF_F_RXHASH | NETIF_F_CSUM_MASK | NETIF_F_RXCSUM)
3532 
3533 static int macsec_dev_init(struct net_device *dev)
3534 {
3535 	struct macsec_dev *macsec = macsec_priv(dev);
3536 	struct net_device *real_dev = macsec->real_dev;
3537 	int err;
3538 
3539 	err = gro_cells_init(&macsec->gro_cells, dev);
3540 	if (err)
3541 		return err;
3542 
3543 	err = -ENOMEM;
3544 	macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3545 	if (!macsec->stats)
3546 		goto destroy_gro_cells;
3547 
3548 	macsec->secy.tx_sc.stats =
3549 		netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3550 	if (!macsec->secy.tx_sc.stats)
3551 		goto free_secy_stats;
3552 
3553 	macsec->secy.tx_sc.md_dst = metadata_dst_alloc(0, METADATA_MACSEC,
3554 						       GFP_KERNEL);
3555 	if (!macsec->secy.tx_sc.md_dst)
3556 		goto free_tx_sc_stats;
3557 	macsec->secy.tx_sc.md_dst->u.macsec_info.sci = macsec->secy.sci;
3558 
3559 	macsec_inherit_tso_max(dev);
3560 
3561 	dev->hw_features = real_dev->hw_features & MACSEC_OFFLOAD_FEATURES;
3562 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3563 
3564 	dev->features = real_dev->features & MACSEC_OFFLOAD_FEATURES;
3565 	dev->features |= NETIF_F_GSO_SOFTWARE;
3566 	dev->lltx = true;
3567 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
3568 
3569 	macsec_set_head_tail_room(dev);
3570 
3571 	if (is_zero_ether_addr(dev->broadcast))
3572 		memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
3573 
3574 	/* Get macsec's reference to real_dev */
3575 	netdev_hold(real_dev, &macsec->dev_tracker, GFP_KERNEL);
3576 
3577 	return 0;
3578 
3579 free_tx_sc_stats:
3580 	free_percpu(macsec->secy.tx_sc.stats);
3581 free_secy_stats:
3582 	free_percpu(macsec->stats);
3583 destroy_gro_cells:
3584 	gro_cells_destroy(&macsec->gro_cells);
3585 	return err;
3586 }
3587 
3588 static void macsec_dev_uninit(struct net_device *dev)
3589 {
3590 	struct macsec_dev *macsec = macsec_priv(dev);
3591 
3592 	gro_cells_destroy(&macsec->gro_cells);
3593 }
3594 
3595 static netdev_features_t macsec_fix_features(struct net_device *dev,
3596 					     netdev_features_t features)
3597 {
3598 	struct macsec_dev *macsec = macsec_priv(dev);
3599 	struct net_device *real_dev = macsec->real_dev;
3600 	netdev_features_t mask;
3601 
3602 	mask = macsec_is_offloaded(macsec) ? MACSEC_OFFLOAD_FEATURES
3603 					   : MACSEC_FEATURES;
3604 
3605 	features &= (real_dev->features & mask) |
3606 		    NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
3607 
3608 	return features;
3609 }
3610 
3611 static int macsec_dev_open(struct net_device *dev)
3612 {
3613 	struct macsec_dev *macsec = macsec_priv(dev);
3614 	struct net_device *real_dev = macsec->real_dev;
3615 	int err;
3616 
3617 	err = dev_uc_add(real_dev, dev->dev_addr);
3618 	if (err < 0)
3619 		return err;
3620 
3621 	if (dev->flags & IFF_ALLMULTI) {
3622 		err = dev_set_allmulti(real_dev, 1);
3623 		if (err < 0)
3624 			goto del_unicast;
3625 	}
3626 
3627 	if (dev->flags & IFF_PROMISC) {
3628 		err = dev_set_promiscuity(real_dev, 1);
3629 		if (err < 0)
3630 			goto clear_allmulti;
3631 	}
3632 
3633 	/* If h/w offloading is available, propagate to the device */
3634 	if (macsec_is_offloaded(macsec)) {
3635 		const struct macsec_ops *ops;
3636 		struct macsec_context ctx;
3637 
3638 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
3639 		if (!ops) {
3640 			err = -EOPNOTSUPP;
3641 			goto clear_promisc;
3642 		}
3643 
3644 		ctx.secy = &macsec->secy;
3645 		err = macsec_offload(ops->mdo_dev_open, &ctx);
3646 		if (err)
3647 			goto clear_promisc;
3648 	}
3649 
3650 	if (netif_carrier_ok(real_dev))
3651 		netif_carrier_on(dev);
3652 
3653 	return 0;
3654 clear_promisc:
3655 	if (dev->flags & IFF_PROMISC)
3656 		dev_set_promiscuity(real_dev, -1);
3657 clear_allmulti:
3658 	if (dev->flags & IFF_ALLMULTI)
3659 		dev_set_allmulti(real_dev, -1);
3660 del_unicast:
3661 	dev_uc_del(real_dev, dev->dev_addr);
3662 	netif_carrier_off(dev);
3663 	return err;
3664 }
3665 
3666 static int macsec_dev_stop(struct net_device *dev)
3667 {
3668 	struct macsec_dev *macsec = macsec_priv(dev);
3669 	struct net_device *real_dev = macsec->real_dev;
3670 
3671 	netif_carrier_off(dev);
3672 
3673 	/* If h/w offloading is available, propagate to the device */
3674 	if (macsec_is_offloaded(macsec)) {
3675 		const struct macsec_ops *ops;
3676 		struct macsec_context ctx;
3677 
3678 		ops = macsec_get_ops(macsec, &ctx);
3679 		if (ops) {
3680 			ctx.secy = &macsec->secy;
3681 			macsec_offload(ops->mdo_dev_stop, &ctx);
3682 		}
3683 	}
3684 
3685 	dev_mc_unsync(real_dev, dev);
3686 	dev_uc_unsync(real_dev, dev);
3687 
3688 	if (dev->flags & IFF_ALLMULTI)
3689 		dev_set_allmulti(real_dev, -1);
3690 
3691 	if (dev->flags & IFF_PROMISC)
3692 		dev_set_promiscuity(real_dev, -1);
3693 
3694 	dev_uc_del(real_dev, dev->dev_addr);
3695 
3696 	return 0;
3697 }
3698 
3699 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
3700 {
3701 	struct net_device *real_dev = macsec_priv(dev)->real_dev;
3702 
3703 	if (!(dev->flags & IFF_UP))
3704 		return;
3705 
3706 	if (change & IFF_ALLMULTI)
3707 		dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
3708 
3709 	if (change & IFF_PROMISC)
3710 		dev_set_promiscuity(real_dev,
3711 				    dev->flags & IFF_PROMISC ? 1 : -1);
3712 }
3713 
3714 static void macsec_dev_set_rx_mode(struct net_device *dev)
3715 {
3716 	struct net_device *real_dev = macsec_priv(dev)->real_dev;
3717 
3718 	dev_mc_sync(real_dev, dev);
3719 	dev_uc_sync(real_dev, dev);
3720 }
3721 
3722 static int macsec_set_mac_address(struct net_device *dev, void *p)
3723 {
3724 	struct macsec_dev *macsec = macsec_priv(dev);
3725 	struct net_device *real_dev = macsec->real_dev;
3726 	struct sockaddr *addr = p;
3727 	u8  old_addr[ETH_ALEN];
3728 	int err;
3729 
3730 	if (!is_valid_ether_addr(addr->sa_data))
3731 		return -EADDRNOTAVAIL;
3732 
3733 	if (dev->flags & IFF_UP) {
3734 		err = dev_uc_add(real_dev, addr->sa_data);
3735 		if (err < 0)
3736 			return err;
3737 	}
3738 
3739 	ether_addr_copy(old_addr, dev->dev_addr);
3740 	eth_hw_addr_set(dev, addr->sa_data);
3741 
3742 	/* If h/w offloading is available, propagate to the device */
3743 	if (macsec_is_offloaded(macsec)) {
3744 		const struct macsec_ops *ops;
3745 		struct macsec_context ctx;
3746 
3747 		ops = macsec_get_ops(macsec, &ctx);
3748 		if (!ops) {
3749 			err = -EOPNOTSUPP;
3750 			goto restore_old_addr;
3751 		}
3752 
3753 		ctx.secy = &macsec->secy;
3754 		err = macsec_offload(ops->mdo_upd_secy, &ctx);
3755 		if (err)
3756 			goto restore_old_addr;
3757 	}
3758 
3759 	if (dev->flags & IFF_UP)
3760 		dev_uc_del(real_dev, old_addr);
3761 
3762 	return 0;
3763 
3764 restore_old_addr:
3765 	if (dev->flags & IFF_UP)
3766 		dev_uc_del(real_dev, addr->sa_data);
3767 
3768 	eth_hw_addr_set(dev, old_addr);
3769 
3770 	return err;
3771 }
3772 
3773 static int macsec_vlan_rx_add_vid(struct net_device *dev,
3774 				  __be16 proto, u16 vid)
3775 {
3776 	struct macsec_dev *macsec = netdev_priv(dev);
3777 
3778 	if (!macsec_is_offloaded(macsec))
3779 		return 0;
3780 
3781 	return vlan_vid_add(macsec->real_dev, proto, vid);
3782 }
3783 
3784 static int macsec_vlan_rx_kill_vid(struct net_device *dev,
3785 				   __be16 proto, u16 vid)
3786 {
3787 	struct macsec_dev *macsec = netdev_priv(dev);
3788 
3789 	if (!macsec_is_offloaded(macsec))
3790 		return 0;
3791 
3792 	vlan_vid_del(macsec->real_dev, proto, vid);
3793 	return 0;
3794 }
3795 
3796 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
3797 {
3798 	struct macsec_dev *macsec = macsec_priv(dev);
3799 	unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
3800 
3801 	if (macsec->real_dev->mtu - extra < new_mtu)
3802 		return -ERANGE;
3803 
3804 	WRITE_ONCE(dev->mtu, new_mtu);
3805 
3806 	return 0;
3807 }
3808 
3809 static void macsec_get_stats64(struct net_device *dev,
3810 			       struct rtnl_link_stats64 *s)
3811 {
3812 	if (!dev->tstats)
3813 		return;
3814 
3815 	dev_fetch_sw_netstats(s, dev->tstats);
3816 
3817 	s->rx_dropped = DEV_STATS_READ(dev, rx_dropped);
3818 	s->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
3819 	s->rx_errors = DEV_STATS_READ(dev, rx_errors);
3820 }
3821 
3822 static int macsec_get_iflink(const struct net_device *dev)
3823 {
3824 	return READ_ONCE(macsec_priv(dev)->real_dev->ifindex);
3825 }
3826 
3827 static const struct net_device_ops macsec_netdev_ops = {
3828 	.ndo_init		= macsec_dev_init,
3829 	.ndo_uninit		= macsec_dev_uninit,
3830 	.ndo_open		= macsec_dev_open,
3831 	.ndo_stop		= macsec_dev_stop,
3832 	.ndo_fix_features	= macsec_fix_features,
3833 	.ndo_change_mtu		= macsec_change_mtu,
3834 	.ndo_set_rx_mode	= macsec_dev_set_rx_mode,
3835 	.ndo_change_rx_flags	= macsec_dev_change_rx_flags,
3836 	.ndo_set_mac_address	= macsec_set_mac_address,
3837 	.ndo_vlan_rx_add_vid	= macsec_vlan_rx_add_vid,
3838 	.ndo_vlan_rx_kill_vid	= macsec_vlan_rx_kill_vid,
3839 	.ndo_start_xmit		= macsec_start_xmit,
3840 	.ndo_get_stats64	= macsec_get_stats64,
3841 	.ndo_get_iflink		= macsec_get_iflink,
3842 };
3843 
3844 static const struct device_type macsec_type = {
3845 	.name = "macsec",
3846 };
3847 
3848 static int validate_cipher_suite(const struct nlattr *attr,
3849 				 struct netlink_ext_ack *extack);
3850 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3851 	[IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3852 	[IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3853 	[IFLA_MACSEC_ICV_LEN] = NLA_POLICY_RANGE(NLA_U8, MACSEC_MIN_ICV_LEN, MACSEC_STD_ICV_LEN),
3854 	[IFLA_MACSEC_CIPHER_SUITE] = NLA_POLICY_VALIDATE_FN(NLA_U64, validate_cipher_suite),
3855 	[IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3856 	[IFLA_MACSEC_ENCODING_SA] = NLA_POLICY_MAX(NLA_U8, MACSEC_NUM_AN - 1),
3857 	[IFLA_MACSEC_ENCRYPT] = NLA_POLICY_MAX(NLA_U8, 1),
3858 	[IFLA_MACSEC_PROTECT] = NLA_POLICY_MAX(NLA_U8, 1),
3859 	[IFLA_MACSEC_INC_SCI] = NLA_POLICY_MAX(NLA_U8, 1),
3860 	[IFLA_MACSEC_ES] = NLA_POLICY_MAX(NLA_U8, 1),
3861 	[IFLA_MACSEC_SCB] = NLA_POLICY_MAX(NLA_U8, 1),
3862 	[IFLA_MACSEC_REPLAY_PROTECT] = NLA_POLICY_MAX(NLA_U8, 1),
3863 	[IFLA_MACSEC_VALIDATION] = NLA_POLICY_MAX(NLA_U8, MACSEC_VALIDATE_MAX),
3864 	[IFLA_MACSEC_OFFLOAD] = NLA_POLICY_MAX(NLA_U8, MACSEC_OFFLOAD_MAX),
3865 };
3866 
3867 static void macsec_free_netdev(struct net_device *dev)
3868 {
3869 	struct macsec_dev *macsec = macsec_priv(dev);
3870 
3871 	dst_release(&macsec->secy.tx_sc.md_dst->dst);
3872 	free_percpu(macsec->stats);
3873 	free_percpu(macsec->secy.tx_sc.stats);
3874 
3875 	/* Get rid of the macsec's reference to real_dev */
3876 	netdev_put(macsec->real_dev, &macsec->dev_tracker);
3877 }
3878 
3879 static void macsec_setup(struct net_device *dev)
3880 {
3881 	ether_setup(dev);
3882 	dev->min_mtu = 0;
3883 	dev->max_mtu = ETH_MAX_MTU;
3884 	dev->priv_flags |= IFF_NO_QUEUE | IFF_UNICAST_FLT;
3885 	dev->netdev_ops = &macsec_netdev_ops;
3886 	dev->needs_free_netdev = true;
3887 	dev->priv_destructor = macsec_free_netdev;
3888 	SET_NETDEV_DEVTYPE(dev, &macsec_type);
3889 
3890 	eth_zero_addr(dev->broadcast);
3891 }
3892 
3893 static int macsec_changelink_common(struct net_device *dev,
3894 				    struct nlattr *data[])
3895 {
3896 	struct macsec_secy *secy;
3897 	struct macsec_tx_sc *tx_sc;
3898 
3899 	secy = &macsec_priv(dev)->secy;
3900 	tx_sc = &secy->tx_sc;
3901 
3902 	if (data[IFLA_MACSEC_ENCODING_SA]) {
3903 		struct macsec_tx_sa *tx_sa;
3904 		u8 encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3905 
3906 		WRITE_ONCE(tx_sc->encoding_sa, encoding_sa);
3907 		tx_sa = rtnl_dereference(tx_sc->sa[encoding_sa]);
3908 
3909 		secy->operational = tx_sa && tx_sa->active;
3910 	}
3911 
3912 	if (data[IFLA_MACSEC_ENCRYPT])
3913 		WRITE_ONCE(tx_sc->encrypt, !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]));
3914 
3915 	if (data[IFLA_MACSEC_PROTECT])
3916 		WRITE_ONCE(secy->protect_frames, !!nla_get_u8(data[IFLA_MACSEC_PROTECT]));
3917 
3918 	if (data[IFLA_MACSEC_INC_SCI])
3919 		WRITE_ONCE(tx_sc->send_sci, !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]));
3920 
3921 	if (data[IFLA_MACSEC_ES])
3922 		WRITE_ONCE(tx_sc->end_station, !!nla_get_u8(data[IFLA_MACSEC_ES]));
3923 
3924 	if (data[IFLA_MACSEC_SCB])
3925 		WRITE_ONCE(tx_sc->scb, !!nla_get_u8(data[IFLA_MACSEC_SCB]));
3926 
3927 	if (data[IFLA_MACSEC_REPLAY_PROTECT])
3928 		WRITE_ONCE(secy->replay_protect, !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]));
3929 
3930 	if (data[IFLA_MACSEC_VALIDATION])
3931 		WRITE_ONCE(secy->validate_frames, nla_get_u8(data[IFLA_MACSEC_VALIDATION]));
3932 
3933 	if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3934 		switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3935 		case MACSEC_CIPHER_ID_GCM_AES_128:
3936 		case MACSEC_DEFAULT_CIPHER_ID:
3937 			WRITE_ONCE(secy->key_len, MACSEC_GCM_AES_128_SAK_LEN);
3938 			WRITE_ONCE(secy->xpn, false);
3939 			break;
3940 		case MACSEC_CIPHER_ID_GCM_AES_256:
3941 			WRITE_ONCE(secy->key_len, MACSEC_GCM_AES_256_SAK_LEN);
3942 			WRITE_ONCE(secy->xpn, false);
3943 			break;
3944 		case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3945 			WRITE_ONCE(secy->key_len, MACSEC_GCM_AES_128_SAK_LEN);
3946 			WRITE_ONCE(secy->xpn, true);
3947 			break;
3948 		case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3949 			WRITE_ONCE(secy->key_len, MACSEC_GCM_AES_256_SAK_LEN);
3950 			WRITE_ONCE(secy->xpn, true);
3951 			break;
3952 		default:
3953 			return -EINVAL;
3954 		}
3955 	}
3956 
3957 	if (data[IFLA_MACSEC_WINDOW]) {
3958 		u32 replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3959 
3960 		/* IEEE 802.1AEbw-2013 10.7.8 - maximum replay window
3961 		 * for XPN cipher suites */
3962 		if (secy->xpn &&
3963 		    replay_window > MACSEC_XPN_MAX_REPLAY_WINDOW)
3964 			return -EINVAL;
3965 		WRITE_ONCE(secy->replay_window, replay_window);
3966 	}
3967 
3968 	return 0;
3969 }
3970 
3971 /* Both a change of the offload state (mdo_add_secy()/mdo_del_secy()) and
3972  * any request against an already offloaded device (mdo_upd_secy()) reach
3973  * the driver of real_dev, which may live in a different network namespace
3974  * from the macsec device. Require CAP_NET_ADMIN over real_dev's namespace
3975  * for those; local attributes of a non-offloaded device are unaffected.
3976  */
3977 static int macsec_changelink_check_netns(struct net_device *dev,
3978 					 struct nlattr *data[],
3979 					 struct netlink_ext_ack *extack)
3980 {
3981 	struct macsec_dev *macsec = macsec_priv(dev);
3982 	bool reaches_real_dev;
3983 
3984 	reaches_real_dev = macsec_is_offloaded(macsec);
3985 	if (data[IFLA_MACSEC_OFFLOAD] &&
3986 	    nla_get_u8(data[IFLA_MACSEC_OFFLOAD]) != macsec->offload)
3987 		reaches_real_dev = true;
3988 
3989 	if (!reaches_real_dev ||
3990 	    rtnl_dev_link_net_capable(dev, dev_net(macsec->real_dev)))
3991 		return 0;
3992 
3993 	NL_SET_ERR_MSG(extack,
3994 		       "Changing a MACsec device whose real device is in another network namespace requires CAP_NET_ADMIN in that namespace");
3995 	return -EPERM;
3996 }
3997 
3998 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3999 			     struct nlattr *data[],
4000 			     struct netlink_ext_ack *extack)
4001 {
4002 	struct macsec_dev *macsec = macsec_priv(dev);
4003 	bool macsec_offload_state_change = false;
4004 	enum macsec_offload offload;
4005 	struct macsec_tx_sc tx_sc;
4006 	struct macsec_secy secy;
4007 	int ret;
4008 
4009 	if (!data)
4010 		return 0;
4011 
4012 	ret = macsec_changelink_check_netns(dev, data, extack);
4013 	if (ret)
4014 		return ret;
4015 
4016 	if (data[IFLA_MACSEC_CIPHER_SUITE] ||
4017 	    data[IFLA_MACSEC_ICV_LEN] ||
4018 	    data[IFLA_MACSEC_SCI] ||
4019 	    data[IFLA_MACSEC_PORT])
4020 		return -EINVAL;
4021 
4022 	/* Keep a copy of unmodified secy and tx_sc, in case the offload
4023 	 * propagation fails, to revert macsec_changelink_common.
4024 	 */
4025 	memcpy(&secy, &macsec->secy, sizeof(secy));
4026 	memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
4027 
4028 	ret = macsec_changelink_common(dev, data);
4029 	if (ret)
4030 		goto cleanup;
4031 
4032 	if (data[IFLA_MACSEC_OFFLOAD]) {
4033 		offload = nla_get_u8(data[IFLA_MACSEC_OFFLOAD]);
4034 		if (macsec->offload != offload) {
4035 			macsec_offload_state_change = true;
4036 			ret = macsec_update_offload(dev, offload, extack);
4037 			if (ret)
4038 				goto cleanup;
4039 		}
4040 	}
4041 
4042 	/* If h/w offloading is available, propagate to the device */
4043 	if (!macsec_offload_state_change && macsec_is_offloaded(macsec)) {
4044 		const struct macsec_ops *ops;
4045 		struct macsec_context ctx;
4046 
4047 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
4048 		if (!ops) {
4049 			ret = -EOPNOTSUPP;
4050 			goto cleanup;
4051 		}
4052 
4053 		ctx.secy = &macsec->secy;
4054 		ret = macsec_offload(ops->mdo_upd_secy, &ctx);
4055 		if (ret)
4056 			goto cleanup;
4057 	}
4058 
4059 	return 0;
4060 
4061 cleanup:
4062 	memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
4063 	memcpy(&macsec->secy, &secy, sizeof(secy));
4064 
4065 	return ret;
4066 }
4067 
4068 static void macsec_del_dev(struct macsec_dev *macsec)
4069 {
4070 	int i;
4071 
4072 	while (macsec->secy.rx_sc) {
4073 		struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
4074 
4075 		rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
4076 		free_rx_sc(rx_sc);
4077 	}
4078 
4079 	for (i = 0; i < MACSEC_NUM_AN; i++) {
4080 		struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
4081 
4082 		if (sa) {
4083 			RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
4084 			clear_tx_sa(sa);
4085 		}
4086 	}
4087 }
4088 
4089 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
4090 {
4091 	struct macsec_dev *macsec = macsec_priv(dev);
4092 	struct net_device *real_dev = macsec->real_dev;
4093 
4094 	/* If h/w offloading is available, propagate to the device */
4095 	if (macsec_is_offloaded(macsec)) {
4096 		const struct macsec_ops *ops;
4097 		struct macsec_context ctx;
4098 
4099 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
4100 		if (ops) {
4101 			ctx.secy = &macsec->secy;
4102 			macsec_offload(ops->mdo_del_secy, &ctx);
4103 		}
4104 	}
4105 
4106 	unregister_netdevice_queue(dev, head);
4107 	list_del_rcu(&macsec->secys);
4108 	macsec_del_dev(macsec);
4109 	netdev_upper_dev_unlink(real_dev, dev);
4110 
4111 	macsec_generation++;
4112 }
4113 
4114 static void macsec_dellink(struct net_device *dev, struct list_head *head)
4115 {
4116 	struct macsec_dev *macsec = macsec_priv(dev);
4117 	struct net_device *real_dev = macsec->real_dev;
4118 	struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
4119 
4120 	macsec_common_dellink(dev, head);
4121 
4122 	if (list_empty(&rxd->secys)) {
4123 		netdev_rx_handler_unregister(real_dev);
4124 		kfree(rxd);
4125 	}
4126 }
4127 
4128 static int register_macsec_dev(struct net_device *real_dev,
4129 			       struct net_device *dev)
4130 {
4131 	struct macsec_dev *macsec = macsec_priv(dev);
4132 	struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
4133 
4134 	if (!rxd) {
4135 		int err;
4136 
4137 		rxd = kmalloc_obj(*rxd);
4138 		if (!rxd)
4139 			return -ENOMEM;
4140 
4141 		INIT_LIST_HEAD(&rxd->secys);
4142 
4143 		err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
4144 						 rxd);
4145 		if (err < 0) {
4146 			kfree(rxd);
4147 			return err;
4148 		}
4149 	}
4150 
4151 	list_add_tail_rcu(&macsec->secys, &rxd->secys);
4152 	return 0;
4153 }
4154 
4155 static bool sci_exists(struct net_device *dev, sci_t sci)
4156 {
4157 	struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
4158 	struct macsec_dev *macsec;
4159 
4160 	list_for_each_entry(macsec, &rxd->secys, secys) {
4161 		if (macsec->secy.sci == sci)
4162 			return true;
4163 	}
4164 
4165 	return false;
4166 }
4167 
4168 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
4169 {
4170 	return make_sci(dev->dev_addr, port);
4171 }
4172 
4173 static void macsec_init_secy(struct net_device *dev, sci_t sci, u8 icv_len)
4174 {
4175 	struct macsec_dev *macsec = macsec_priv(dev);
4176 	struct macsec_secy *secy = &macsec->secy;
4177 
4178 	if (sci == MACSEC_UNDEF_SCI)
4179 		sci = dev_to_sci(dev, MACSEC_PORT_ES);
4180 
4181 	secy->netdev = dev;
4182 	secy->operational = true;
4183 	secy->key_len = DEFAULT_SAK_LEN;
4184 	secy->icv_len = icv_len;
4185 	secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
4186 	secy->protect_frames = true;
4187 	secy->replay_protect = false;
4188 	secy->xpn = DEFAULT_XPN;
4189 
4190 	secy->sci = sci;
4191 	secy->tx_sc.active = true;
4192 	secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
4193 	secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
4194 	secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
4195 	secy->tx_sc.end_station = false;
4196 	secy->tx_sc.scb = false;
4197 }
4198 
4199 static struct lock_class_key macsec_netdev_addr_lock_key;
4200 
4201 static int macsec_newlink(struct net_device *dev,
4202 			  struct rtnl_newlink_params *params,
4203 			  struct netlink_ext_ack *extack)
4204 {
4205 	struct net *link_net = rtnl_newlink_link_net(params);
4206 	struct macsec_dev *macsec = macsec_priv(dev);
4207 	struct nlattr **data = params->data;
4208 	struct nlattr **tb = params->tb;
4209 	rx_handler_func_t *rx_handler;
4210 	u8 icv_len = MACSEC_DEFAULT_ICV_LEN;
4211 	struct net_device *real_dev;
4212 	int err, mtu;
4213 	sci_t sci;
4214 
4215 	if (!tb[IFLA_LINK])
4216 		return -EINVAL;
4217 	real_dev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK]));
4218 	if (!real_dev)
4219 		return -ENODEV;
4220 	if (real_dev->type != ARPHRD_ETHER)
4221 		return -EINVAL;
4222 
4223 	dev->priv_flags |= IFF_MACSEC;
4224 
4225 	macsec->real_dev = real_dev;
4226 
4227 	if (data && data[IFLA_MACSEC_OFFLOAD])
4228 		macsec->offload = nla_get_offload(data[IFLA_MACSEC_OFFLOAD]);
4229 	else
4230 		/* MACsec offloading is off by default */
4231 		macsec->offload = MACSEC_OFFLOAD_OFF;
4232 
4233 	/* Check if the offloading mode is supported by the underlying layers */
4234 	if (macsec->offload != MACSEC_OFFLOAD_OFF &&
4235 	    !macsec_check_offload(macsec->offload, macsec))
4236 		return -EOPNOTSUPP;
4237 
4238 	/* send_sci must be set to true when transmit sci explicitly is set */
4239 	if ((data && data[IFLA_MACSEC_SCI]) &&
4240 	    (data && data[IFLA_MACSEC_INC_SCI])) {
4241 		u8 send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
4242 
4243 		if (!send_sci)
4244 			return -EINVAL;
4245 	}
4246 
4247 	if (data && data[IFLA_MACSEC_ICV_LEN])
4248 		icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4249 	mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
4250 	if (mtu < 0)
4251 		dev->mtu = 0;
4252 	else
4253 		dev->mtu = mtu;
4254 
4255 	rx_handler = rtnl_dereference(real_dev->rx_handler);
4256 	if (rx_handler && rx_handler != macsec_handle_frame)
4257 		return -EBUSY;
4258 
4259 	if (is_zero_ether_addr(dev->dev_addr))
4260 		eth_hw_addr_inherit(dev, real_dev);
4261 
4262 	if (data && data[IFLA_MACSEC_SCI])
4263 		sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
4264 	else if (data && data[IFLA_MACSEC_PORT])
4265 		sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
4266 	else
4267 		sci = dev_to_sci(dev, MACSEC_PORT_ES);
4268 
4269 	/* Registration can notify listeners before returning. */
4270 	macsec_init_secy(dev, sci, icv_len);
4271 	if (data) {
4272 		err = macsec_changelink_common(dev, data);
4273 		if (err)
4274 			return err;
4275 	}
4276 
4277 	err = register_netdevice(dev);
4278 	if (err < 0)
4279 		return err;
4280 
4281 	netdev_lockdep_set_classes(dev);
4282 	lockdep_set_class(&dev->addr_list_lock,
4283 			  &macsec_netdev_addr_lock_key);
4284 
4285 	err = netdev_upper_dev_link(real_dev, dev, extack);
4286 	if (err < 0)
4287 		goto unregister;
4288 
4289 	if (rx_handler && sci_exists(real_dev, sci)) {
4290 		err = -EBUSY;
4291 		goto unlink;
4292 	}
4293 
4294 	/* If h/w offloading is available, propagate to the device */
4295 	if (macsec_is_offloaded(macsec)) {
4296 		const struct macsec_ops *ops;
4297 		struct macsec_context ctx;
4298 
4299 		ops = macsec_get_ops(macsec, &ctx);
4300 		if (ops) {
4301 			ctx.secy = &macsec->secy;
4302 			err = macsec_offload(ops->mdo_add_secy, &ctx);
4303 			if (err)
4304 				goto del_dev;
4305 
4306 			macsec->insert_tx_tag =
4307 				macsec_needs_tx_tag(macsec, ops);
4308 		}
4309 	}
4310 
4311 	err = register_macsec_dev(real_dev, dev);
4312 	if (err < 0)
4313 		goto del_dev;
4314 
4315 	netdev_update_features(dev);
4316 	netif_stacked_transfer_operstate(real_dev, dev);
4317 	linkwatch_fire_event(dev);
4318 
4319 	macsec_generation++;
4320 
4321 	return 0;
4322 
4323 del_dev:
4324 	macsec_del_dev(macsec);
4325 unlink:
4326 	netdev_upper_dev_unlink(real_dev, dev);
4327 unregister:
4328 	unregister_netdevice(dev);
4329 	return err;
4330 }
4331 
4332 static int validate_cipher_suite(const struct nlattr *attr,
4333 				 struct netlink_ext_ack *extack)
4334 {
4335 	switch (nla_get_u64(attr)) {
4336 	case MACSEC_CIPHER_ID_GCM_AES_128:
4337 	case MACSEC_CIPHER_ID_GCM_AES_256:
4338 	case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
4339 	case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
4340 	case MACSEC_DEFAULT_CIPHER_ID:
4341 		return 0;
4342 	default:
4343 		return -EINVAL;
4344 	}
4345 }
4346 
4347 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
4348 				struct netlink_ext_ack *extack)
4349 {
4350 	u8 icv_len = MACSEC_DEFAULT_ICV_LEN;
4351 	bool es, scb, sci;
4352 
4353 	if (!data)
4354 		return 0;
4355 
4356 	if (data[IFLA_MACSEC_ICV_LEN]) {
4357 		icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4358 		if (icv_len != MACSEC_DEFAULT_ICV_LEN) {
4359 			char dummy_key[DEFAULT_SAK_LEN] = { 0 };
4360 			struct crypto_aead *dummy_tfm;
4361 
4362 			dummy_tfm = macsec_alloc_tfm(dummy_key,
4363 						     DEFAULT_SAK_LEN,
4364 						     icv_len);
4365 			if (IS_ERR(dummy_tfm))
4366 				return PTR_ERR(dummy_tfm);
4367 			crypto_free_aead(dummy_tfm);
4368 		}
4369 	}
4370 
4371 	es  = nla_get_u8_default(data[IFLA_MACSEC_ES], false);
4372 	sci = nla_get_u8_default(data[IFLA_MACSEC_INC_SCI], false);
4373 	scb = nla_get_u8_default(data[IFLA_MACSEC_SCB], false);
4374 
4375 	if ((sci && (scb || es)) || (scb && es))
4376 		return -EINVAL;
4377 
4378 	if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
4379 	     nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
4380 	    !data[IFLA_MACSEC_WINDOW])
4381 		return -EINVAL;
4382 
4383 	return 0;
4384 }
4385 
4386 static struct net *macsec_get_link_net(const struct net_device *dev)
4387 {
4388 	return dev_net(macsec_priv(dev)->real_dev);
4389 }
4390 
4391 struct net_device *macsec_get_real_dev(const struct net_device *dev)
4392 {
4393 	return macsec_priv(dev)->real_dev;
4394 }
4395 EXPORT_SYMBOL_GPL(macsec_get_real_dev);
4396 
4397 bool macsec_netdev_is_offloaded(struct net_device *dev)
4398 {
4399 	return macsec_is_offloaded(macsec_priv(dev));
4400 }
4401 EXPORT_SYMBOL_GPL(macsec_netdev_is_offloaded);
4402 
4403 static size_t macsec_get_size(const struct net_device *dev)
4404 {
4405 	return  nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
4406 		nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
4407 		nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
4408 		nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
4409 		nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
4410 		nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
4411 		nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
4412 		nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
4413 		nla_total_size(1) + /* IFLA_MACSEC_ES */
4414 		nla_total_size(1) + /* IFLA_MACSEC_SCB */
4415 		nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
4416 		nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
4417 		nla_total_size(1) + /* IFLA_MACSEC_OFFLOAD */
4418 		0;
4419 }
4420 
4421 static int macsec_fill_info(struct sk_buff *skb,
4422 			    const struct net_device *dev)
4423 {
4424 	const struct macsec_tx_sc *tx_sc;
4425 	const struct macsec_dev *macsec;
4426 	const struct macsec_secy *secy;
4427 	u64 csid;
4428 
4429 	macsec = macsec_priv(dev);
4430 	secy = &macsec->secy;
4431 	tx_sc = &secy->tx_sc;
4432 
4433 	switch (READ_ONCE(secy->key_len)) {
4434 	case MACSEC_GCM_AES_128_SAK_LEN:
4435 		csid = READ_ONCE(secy->xpn) ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
4436 		break;
4437 	case MACSEC_GCM_AES_256_SAK_LEN:
4438 		csid = READ_ONCE(secy->xpn) ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
4439 		break;
4440 	default:
4441 		goto nla_put_failure;
4442 	}
4443 
4444 	if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
4445 			IFLA_MACSEC_PAD) ||
4446 	    nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
4447 	    nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
4448 			      csid, IFLA_MACSEC_PAD) ||
4449 	    nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, READ_ONCE(tx_sc->encoding_sa)) ||
4450 	    nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, READ_ONCE(tx_sc->encrypt)) ||
4451 	    nla_put_u8(skb, IFLA_MACSEC_PROTECT, READ_ONCE(secy->protect_frames)) ||
4452 	    nla_put_u8(skb, IFLA_MACSEC_INC_SCI, READ_ONCE(tx_sc->send_sci)) ||
4453 	    nla_put_u8(skb, IFLA_MACSEC_ES, READ_ONCE(tx_sc->end_station)) ||
4454 	    nla_put_u8(skb, IFLA_MACSEC_SCB, READ_ONCE(tx_sc->scb)) ||
4455 	    nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, READ_ONCE(secy->replay_protect)) ||
4456 	    nla_put_u8(skb, IFLA_MACSEC_VALIDATION, READ_ONCE(secy->validate_frames)) ||
4457 	    nla_put_u8(skb, IFLA_MACSEC_OFFLOAD, READ_ONCE(macsec->offload)) ||
4458 	    0)
4459 		goto nla_put_failure;
4460 
4461 	if (READ_ONCE(secy->replay_protect)) {
4462 		if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, READ_ONCE(secy->replay_window)))
4463 			goto nla_put_failure;
4464 	}
4465 
4466 	return 0;
4467 
4468 nla_put_failure:
4469 	return -EMSGSIZE;
4470 }
4471 
4472 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
4473 	.kind		= "macsec",
4474 	.priv_size	= sizeof(struct macsec_dev),
4475 	.maxtype	= IFLA_MACSEC_MAX,
4476 	.policy		= macsec_rtnl_policy,
4477 	.setup		= macsec_setup,
4478 	.validate	= macsec_validate_attr,
4479 	.newlink	= macsec_newlink,
4480 	.changelink	= macsec_changelink,
4481 	.dellink	= macsec_dellink,
4482 	.get_size	= macsec_get_size,
4483 	.fill_info	= macsec_fill_info,
4484 	.get_link_net	= macsec_get_link_net,
4485 };
4486 
4487 static bool is_macsec_master(struct net_device *dev)
4488 {
4489 	return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
4490 }
4491 
4492 static int macsec_notify(struct notifier_block *this, unsigned long event,
4493 			 void *ptr)
4494 {
4495 	struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
4496 	struct macsec_rxh_data *rxd;
4497 	struct macsec_dev *m, *n;
4498 	LIST_HEAD(head);
4499 
4500 	if (!is_macsec_master(real_dev))
4501 		return NOTIFY_DONE;
4502 
4503 	rxd = macsec_data_rtnl(real_dev);
4504 
4505 	switch (event) {
4506 	case NETDEV_DOWN:
4507 	case NETDEV_UP:
4508 	case NETDEV_CHANGE:
4509 		list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4510 			struct net_device *dev = m->secy.netdev;
4511 
4512 			netif_stacked_transfer_operstate(real_dev, dev);
4513 		}
4514 		break;
4515 	case NETDEV_UNREGISTER:
4516 		list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4517 			macsec_common_dellink(m->secy.netdev, &head);
4518 		}
4519 
4520 		netdev_rx_handler_unregister(real_dev);
4521 		kfree(rxd);
4522 
4523 		unregister_netdevice_many(&head);
4524 		break;
4525 	case NETDEV_CHANGEMTU:
4526 		list_for_each_entry(m, &rxd->secys, secys) {
4527 			struct net_device *dev = m->secy.netdev;
4528 			unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
4529 							    macsec_extra_len(true));
4530 
4531 			if (dev->mtu > mtu)
4532 				dev_set_mtu(dev, mtu);
4533 		}
4534 		break;
4535 	case NETDEV_FEAT_CHANGE:
4536 		list_for_each_entry(m, &rxd->secys, secys) {
4537 			macsec_inherit_tso_max(m->secy.netdev);
4538 			netdev_update_features(m->secy.netdev);
4539 		}
4540 		break;
4541 	}
4542 
4543 	return NOTIFY_OK;
4544 }
4545 
4546 static struct notifier_block macsec_notifier = {
4547 	.notifier_call = macsec_notify,
4548 };
4549 
4550 static int __init macsec_init(void)
4551 {
4552 	int err;
4553 
4554 	macsec_wq = alloc_workqueue("macsec", WQ_UNBOUND, 0);
4555 	if (!macsec_wq)
4556 		return -ENOMEM;
4557 
4558 	pr_info("MACsec IEEE 802.1AE\n");
4559 	err = register_netdevice_notifier(&macsec_notifier);
4560 	if (err)
4561 		goto err_destroy_wq;
4562 
4563 	err = rtnl_link_register(&macsec_link_ops);
4564 	if (err)
4565 		goto err_notifier;
4566 
4567 	err = genl_register_family(&macsec_fam);
4568 	if (err)
4569 		goto err_rtnl;
4570 
4571 	return 0;
4572 
4573 err_rtnl:
4574 	rtnl_link_unregister(&macsec_link_ops);
4575 err_notifier:
4576 	unregister_netdevice_notifier(&macsec_notifier);
4577 err_destroy_wq:
4578 	/* Precautionary, mirrors macsec_exit() to stay safe if work
4579 	 * ever becomes queueable before this point in the future.
4580 	 */
4581 	rcu_barrier();
4582 	destroy_workqueue(macsec_wq);
4583 	return err;
4584 }
4585 
4586 static void __exit macsec_exit(void)
4587 {
4588 	genl_unregister_family(&macsec_fam);
4589 	rtnl_link_unregister(&macsec_link_ops);
4590 	unregister_netdevice_notifier(&macsec_notifier);
4591 	rcu_barrier();
4592 	destroy_workqueue(macsec_wq);
4593 }
4594 
4595 module_init(macsec_init);
4596 module_exit(macsec_exit);
4597 
4598 MODULE_ALIAS_RTNL_LINK("macsec");
4599 MODULE_ALIAS_GENL_FAMILY("macsec");
4600 
4601 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
4602 MODULE_LICENSE("GPL v2");
4603