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