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