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