xref: /linux/drivers/net/macsec.c (revision 27605c8c0f69e319df156b471974e4e223035378)
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 
macsec_priv(const struct net_device * dev)119 static struct macsec_dev *macsec_priv(const struct net_device *dev)
120 {
121 	return (struct macsec_dev *)netdev_priv(dev);
122 }
123 
macsec_data_rcu(const struct net_device * dev)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 
macsec_data_rtnl(const struct net_device * dev)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 
macsec_rxsa_get(struct macsec_rx_sa __rcu * ptr)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 
free_rx_sc_rcu(struct rcu_head * head)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 
macsec_rxsc_get(struct macsec_rx_sc * sc)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 
macsec_rxsc_put(struct macsec_rx_sc * sc)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 
free_rxsa(struct rcu_head * head)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 
macsec_rxsa_put(struct macsec_rx_sa * sa)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 
macsec_txsa_get(struct macsec_tx_sa __rcu * ptr)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 
free_txsa(struct rcu_head * head)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 
macsec_txsa_put(struct macsec_tx_sa * sa)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 
macsec_skb_cb(struct sk_buff * skb)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 
make_sci(const u8 * addr,__be16 port)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 
macsec_active_sci(struct macsec_secy * secy)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 
macsec_frame_sci(struct macsec_eth_header * hdr,bool sci_present,struct macsec_rxh_data * rxd)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 
macsec_sectag_len(bool sci_present)287 static unsigned int macsec_sectag_len(bool sci_present)
288 {
289 	return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
290 }
291 
macsec_hdr_len(bool sci_present)292 static unsigned int macsec_hdr_len(bool sci_present)
293 {
294 	return macsec_sectag_len(sci_present) + ETH_HLEN;
295 }
296 
macsec_extra_len(bool sci_present)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 */
macsec_fill_sectag(struct macsec_eth_header * h,const struct macsec_secy * secy,u32 pn,bool sci_present)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 
macsec_set_shortlen(struct macsec_eth_header * h,size_t data_len)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 */
macsec_is_offloaded(struct macsec_dev * macsec)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. */
macsec_check_offload(enum macsec_offload offload,struct macsec_dev * macsec)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 
__macsec_get_ops(enum macsec_offload offload,struct macsec_dev * macsec,struct macsec_context * ctx)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  */
macsec_get_ops(struct macsec_dev * macsec,struct macsec_context * ctx)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 */
macsec_validate_skb(struct sk_buff * skb,u16 icv_len,bool xpn)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 
macsec_fill_iv_xpn(unsigned char * iv,ssci_t ssci,u64 pn,salt_t salt)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 
macsec_fill_iv(unsigned char * iv,sci_t sci,u32 pn)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 
macsec_ethhdr(struct sk_buff * skb)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 
__macsec_pn_wrapped(struct macsec_secy * secy,struct macsec_tx_sa * tx_sa)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 
macsec_pn_wrapped(struct macsec_secy * secy,struct macsec_tx_sa * tx_sa)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 
tx_sa_update_pn(struct macsec_tx_sa * tx_sa,struct macsec_secy * secy)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 
macsec_encrypt_finish(struct sk_buff * skb,struct net_device * dev)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 
macsec_msdu_len(struct sk_buff * skb)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 
macsec_count_tx(struct sk_buff * skb,struct macsec_tx_sc * tx_sc,struct macsec_tx_sa * tx_sa)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 
count_tx(struct net_device * dev,int ret,int len)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 
macsec_encrypt_done(void * data,int err)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 
macsec_alloc_req(struct crypto_aead * tfm,unsigned char ** iv,struct scatterlist ** sg,int num_frags)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 
macsec_encrypt(struct sk_buff * skb,struct net_device * dev)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 
macsec_post_decrypt(struct sk_buff * skb,struct macsec_secy * secy,u32 pn)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 
macsec_reset_skb(struct sk_buff * skb,struct net_device * dev)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 
macsec_finalize_skb(struct sk_buff * skb,u8 icv_len,u8 hdr_len)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 
count_rx(struct net_device * dev,int len)837 static void count_rx(struct net_device *dev, int len)
838 {
839 	dev_sw_netstats_rx_add(dev, len);
840 }
841 
macsec_decrypt_done(void * data,int err)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 
macsec_decrypt(struct sk_buff * skb,struct net_device * dev,struct macsec_rx_sa * rx_sa,sci_t sci,struct macsec_secy * secy)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 
find_rx_sc(struct macsec_secy * secy,sci_t sci)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 
find_rx_sc_rtnl(struct macsec_secy * secy,sci_t sci)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 
handle_not_macsec(struct sk_buff * skb)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 
macsec_handle_frame(struct sk_buff ** pskb)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 
macsec_alloc_tfm(char * key,int key_len,int icv_len)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 
init_rx_sa(struct macsec_rx_sa * rx_sa,char * sak,int key_len,int icv_len)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 
clear_rx_sa(struct macsec_rx_sa * rx_sa)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 
free_rx_sc(struct macsec_rx_sc * rx_sc)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 
del_rx_sc(struct macsec_secy * secy,sci_t sci)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 
create_rx_sc(struct net_device * dev,sci_t sci,bool active)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(sizeof(*rx_sc), GFP_KERNEL);
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 
init_tx_sa(struct macsec_tx_sa * tx_sa,char * sak,int key_len,int icv_len)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 
clear_tx_sa(struct macsec_tx_sa * tx_sa)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 
get_dev_from_nl(struct net * net,struct nlattr ** attrs)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 
nla_get_offload(const struct nlattr * nla)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 
nla_get_sci(const struct nlattr * nla)1543 static sci_t nla_get_sci(const struct nlattr *nla)
1544 {
1545 	return (__force sci_t)nla_get_u64(nla);
1546 }
1547 
nla_put_sci(struct sk_buff * skb,int attrtype,sci_t value,int padattr)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 
nla_get_ssci(const struct nlattr * nla)1554 static ssci_t nla_get_ssci(const struct nlattr *nla)
1555 {
1556 	return (__force ssci_t)nla_get_u32(nla);
1557 }
1558 
nla_put_ssci(struct sk_buff * skb,int attrtype,ssci_t value)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 
get_txsa_from_nl(struct net * net,struct nlattr ** attrs,struct nlattr ** tb_sa,struct net_device ** devp,struct macsec_secy ** secyp,struct macsec_tx_sc ** scp,u8 * assoc_num)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 	if (*assoc_num >= MACSEC_NUM_AN)
1587 		return ERR_PTR(-EINVAL);
1588 
1589 	secy = &macsec_priv(dev)->secy;
1590 	tx_sc = &secy->tx_sc;
1591 
1592 	tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1593 	if (!tx_sa)
1594 		return ERR_PTR(-ENODEV);
1595 
1596 	*devp = dev;
1597 	*scp = tx_sc;
1598 	*secyp = secy;
1599 	return tx_sa;
1600 }
1601 
get_rxsc_from_nl(struct net * net,struct nlattr ** attrs,struct nlattr ** tb_rxsc,struct net_device ** devp,struct macsec_secy ** secyp)1602 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1603 					     struct nlattr **attrs,
1604 					     struct nlattr **tb_rxsc,
1605 					     struct net_device **devp,
1606 					     struct macsec_secy **secyp)
1607 {
1608 	struct net_device *dev;
1609 	struct macsec_secy *secy;
1610 	struct macsec_rx_sc *rx_sc;
1611 	sci_t sci;
1612 
1613 	dev = get_dev_from_nl(net, attrs);
1614 	if (IS_ERR(dev))
1615 		return ERR_CAST(dev);
1616 
1617 	secy = &macsec_priv(dev)->secy;
1618 
1619 	if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1620 		return ERR_PTR(-EINVAL);
1621 
1622 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1623 	rx_sc = find_rx_sc_rtnl(secy, sci);
1624 	if (!rx_sc)
1625 		return ERR_PTR(-ENODEV);
1626 
1627 	*secyp = secy;
1628 	*devp = dev;
1629 
1630 	return rx_sc;
1631 }
1632 
get_rxsa_from_nl(struct net * net,struct nlattr ** attrs,struct nlattr ** tb_rxsc,struct nlattr ** tb_sa,struct net_device ** devp,struct macsec_secy ** secyp,struct macsec_rx_sc ** scp,u8 * assoc_num)1633 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1634 					     struct nlattr **attrs,
1635 					     struct nlattr **tb_rxsc,
1636 					     struct nlattr **tb_sa,
1637 					     struct net_device **devp,
1638 					     struct macsec_secy **secyp,
1639 					     struct macsec_rx_sc **scp,
1640 					     u8 *assoc_num)
1641 {
1642 	struct macsec_rx_sc *rx_sc;
1643 	struct macsec_rx_sa *rx_sa;
1644 
1645 	if (!tb_sa[MACSEC_SA_ATTR_AN])
1646 		return ERR_PTR(-EINVAL);
1647 
1648 	*assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1649 	if (*assoc_num >= MACSEC_NUM_AN)
1650 		return ERR_PTR(-EINVAL);
1651 
1652 	rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1653 	if (IS_ERR(rx_sc))
1654 		return ERR_CAST(rx_sc);
1655 
1656 	rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1657 	if (!rx_sa)
1658 		return ERR_PTR(-ENODEV);
1659 
1660 	*scp = rx_sc;
1661 	return rx_sa;
1662 }
1663 
1664 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1665 	[MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1666 	[MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1667 	[MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1668 	[MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
1669 };
1670 
1671 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1672 	[MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1673 	[MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1674 };
1675 
1676 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1677 	[MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1678 	[MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1679 	[MACSEC_SA_ATTR_PN] = NLA_POLICY_MIN_LEN(4),
1680 	[MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1681 				   .len = MACSEC_KEYID_LEN, },
1682 	[MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1683 				 .len = MACSEC_MAX_KEY_LEN, },
1684 	[MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 },
1685 	[MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY,
1686 				  .len = MACSEC_SALT_LEN, },
1687 };
1688 
1689 static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
1690 	[MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 },
1691 };
1692 
1693 /* Offloads an operation to a device driver */
macsec_offload(int (* const func)(struct macsec_context *),struct macsec_context * ctx)1694 static int macsec_offload(int (* const func)(struct macsec_context *),
1695 			  struct macsec_context *ctx)
1696 {
1697 	int ret;
1698 
1699 	if (unlikely(!func))
1700 		return 0;
1701 
1702 	if (ctx->offload == MACSEC_OFFLOAD_PHY)
1703 		mutex_lock(&ctx->phydev->lock);
1704 
1705 	ret = (*func)(ctx);
1706 
1707 	if (ctx->offload == MACSEC_OFFLOAD_PHY)
1708 		mutex_unlock(&ctx->phydev->lock);
1709 
1710 	return ret;
1711 }
1712 
parse_sa_config(struct nlattr ** attrs,struct nlattr ** tb_sa)1713 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1714 {
1715 	if (!attrs[MACSEC_ATTR_SA_CONFIG])
1716 		return -EINVAL;
1717 
1718 	if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1719 		return -EINVAL;
1720 
1721 	return 0;
1722 }
1723 
parse_rxsc_config(struct nlattr ** attrs,struct nlattr ** tb_rxsc)1724 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1725 {
1726 	if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1727 		return -EINVAL;
1728 
1729 	if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1730 		return -EINVAL;
1731 
1732 	return 0;
1733 }
1734 
validate_add_rxsa(struct nlattr ** attrs)1735 static bool validate_add_rxsa(struct nlattr **attrs)
1736 {
1737 	if (!attrs[MACSEC_SA_ATTR_AN] ||
1738 	    !attrs[MACSEC_SA_ATTR_KEY] ||
1739 	    !attrs[MACSEC_SA_ATTR_KEYID])
1740 		return false;
1741 
1742 	if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1743 		return false;
1744 
1745 	if (attrs[MACSEC_SA_ATTR_PN] &&
1746 	    nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
1747 		return false;
1748 
1749 	if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1750 		if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1751 			return false;
1752 	}
1753 
1754 	if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1755 		return false;
1756 
1757 	return true;
1758 }
1759 
macsec_add_rxsa(struct sk_buff * skb,struct genl_info * info)1760 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1761 {
1762 	struct net_device *dev;
1763 	struct nlattr **attrs = info->attrs;
1764 	struct macsec_secy *secy;
1765 	struct macsec_rx_sc *rx_sc;
1766 	struct macsec_rx_sa *rx_sa;
1767 	unsigned char assoc_num;
1768 	int pn_len;
1769 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1770 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1771 	int err;
1772 
1773 	if (!attrs[MACSEC_ATTR_IFINDEX])
1774 		return -EINVAL;
1775 
1776 	if (parse_sa_config(attrs, tb_sa))
1777 		return -EINVAL;
1778 
1779 	if (parse_rxsc_config(attrs, tb_rxsc))
1780 		return -EINVAL;
1781 
1782 	if (!validate_add_rxsa(tb_sa))
1783 		return -EINVAL;
1784 
1785 	rtnl_lock();
1786 	rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1787 	if (IS_ERR(rx_sc)) {
1788 		rtnl_unlock();
1789 		return PTR_ERR(rx_sc);
1790 	}
1791 
1792 	assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1793 
1794 	if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1795 		pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1796 			  nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1797 		rtnl_unlock();
1798 		return -EINVAL;
1799 	}
1800 
1801 	pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1802 	if (tb_sa[MACSEC_SA_ATTR_PN] &&
1803 	    nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1804 		pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
1805 			  nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1806 		rtnl_unlock();
1807 		return -EINVAL;
1808 	}
1809 
1810 	if (secy->xpn) {
1811 		if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1812 			rtnl_unlock();
1813 			return -EINVAL;
1814 		}
1815 
1816 		if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
1817 			pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n",
1818 				  nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
1819 				  MACSEC_SALT_LEN);
1820 			rtnl_unlock();
1821 			return -EINVAL;
1822 		}
1823 	}
1824 
1825 	rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1826 	if (rx_sa) {
1827 		rtnl_unlock();
1828 		return -EBUSY;
1829 	}
1830 
1831 	rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1832 	if (!rx_sa) {
1833 		rtnl_unlock();
1834 		return -ENOMEM;
1835 	}
1836 
1837 	err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1838 			 secy->key_len, secy->icv_len);
1839 	if (err < 0) {
1840 		kfree(rx_sa);
1841 		rtnl_unlock();
1842 		return err;
1843 	}
1844 
1845 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
1846 		spin_lock_bh(&rx_sa->lock);
1847 		rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
1848 		spin_unlock_bh(&rx_sa->lock);
1849 	}
1850 
1851 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1852 		rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1853 
1854 	rx_sa->sc = rx_sc;
1855 
1856 	if (secy->xpn) {
1857 		rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
1858 		nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
1859 			   MACSEC_SALT_LEN);
1860 	}
1861 
1862 	/* If h/w offloading is available, propagate to the device */
1863 	if (macsec_is_offloaded(netdev_priv(dev))) {
1864 		const struct macsec_ops *ops;
1865 		struct macsec_context ctx;
1866 
1867 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
1868 		if (!ops) {
1869 			err = -EOPNOTSUPP;
1870 			goto cleanup;
1871 		}
1872 
1873 		ctx.sa.assoc_num = assoc_num;
1874 		ctx.sa.rx_sa = rx_sa;
1875 		ctx.secy = secy;
1876 		memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1877 		       secy->key_len);
1878 
1879 		err = macsec_offload(ops->mdo_add_rxsa, &ctx);
1880 		memzero_explicit(ctx.sa.key, secy->key_len);
1881 		if (err)
1882 			goto cleanup;
1883 	}
1884 
1885 	nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1886 	rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1887 
1888 	rtnl_unlock();
1889 
1890 	return 0;
1891 
1892 cleanup:
1893 	macsec_rxsa_put(rx_sa);
1894 	rtnl_unlock();
1895 	return err;
1896 }
1897 
validate_add_rxsc(struct nlattr ** attrs)1898 static bool validate_add_rxsc(struct nlattr **attrs)
1899 {
1900 	if (!attrs[MACSEC_RXSC_ATTR_SCI])
1901 		return false;
1902 
1903 	if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1904 		if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1905 			return false;
1906 	}
1907 
1908 	return true;
1909 }
1910 
macsec_add_rxsc(struct sk_buff * skb,struct genl_info * info)1911 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1912 {
1913 	struct net_device *dev;
1914 	sci_t sci = MACSEC_UNDEF_SCI;
1915 	struct nlattr **attrs = info->attrs;
1916 	struct macsec_rx_sc *rx_sc;
1917 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1918 	struct macsec_secy *secy;
1919 	bool active = true;
1920 	int ret;
1921 
1922 	if (!attrs[MACSEC_ATTR_IFINDEX])
1923 		return -EINVAL;
1924 
1925 	if (parse_rxsc_config(attrs, tb_rxsc))
1926 		return -EINVAL;
1927 
1928 	if (!validate_add_rxsc(tb_rxsc))
1929 		return -EINVAL;
1930 
1931 	rtnl_lock();
1932 	dev = get_dev_from_nl(genl_info_net(info), attrs);
1933 	if (IS_ERR(dev)) {
1934 		rtnl_unlock();
1935 		return PTR_ERR(dev);
1936 	}
1937 
1938 	secy = &macsec_priv(dev)->secy;
1939 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1940 
1941 	if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1942 		active = nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1943 
1944 	rx_sc = create_rx_sc(dev, sci, active);
1945 	if (IS_ERR(rx_sc)) {
1946 		rtnl_unlock();
1947 		return PTR_ERR(rx_sc);
1948 	}
1949 
1950 	if (macsec_is_offloaded(netdev_priv(dev))) {
1951 		const struct macsec_ops *ops;
1952 		struct macsec_context ctx;
1953 
1954 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
1955 		if (!ops) {
1956 			ret = -EOPNOTSUPP;
1957 			goto cleanup;
1958 		}
1959 
1960 		ctx.rx_sc = rx_sc;
1961 		ctx.secy = secy;
1962 
1963 		ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
1964 		if (ret)
1965 			goto cleanup;
1966 	}
1967 
1968 	rtnl_unlock();
1969 
1970 	return 0;
1971 
1972 cleanup:
1973 	del_rx_sc(secy, sci);
1974 	free_rx_sc(rx_sc);
1975 	rtnl_unlock();
1976 	return ret;
1977 }
1978 
validate_add_txsa(struct nlattr ** attrs)1979 static bool validate_add_txsa(struct nlattr **attrs)
1980 {
1981 	if (!attrs[MACSEC_SA_ATTR_AN] ||
1982 	    !attrs[MACSEC_SA_ATTR_PN] ||
1983 	    !attrs[MACSEC_SA_ATTR_KEY] ||
1984 	    !attrs[MACSEC_SA_ATTR_KEYID])
1985 		return false;
1986 
1987 	if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1988 		return false;
1989 
1990 	if (nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
1991 		return false;
1992 
1993 	if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1994 		if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1995 			return false;
1996 	}
1997 
1998 	if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1999 		return false;
2000 
2001 	return true;
2002 }
2003 
macsec_add_txsa(struct sk_buff * skb,struct genl_info * info)2004 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
2005 {
2006 	struct net_device *dev;
2007 	struct nlattr **attrs = info->attrs;
2008 	struct macsec_secy *secy;
2009 	struct macsec_tx_sc *tx_sc;
2010 	struct macsec_tx_sa *tx_sa;
2011 	unsigned char assoc_num;
2012 	int pn_len;
2013 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2014 	bool was_operational;
2015 	int err;
2016 
2017 	if (!attrs[MACSEC_ATTR_IFINDEX])
2018 		return -EINVAL;
2019 
2020 	if (parse_sa_config(attrs, tb_sa))
2021 		return -EINVAL;
2022 
2023 	if (!validate_add_txsa(tb_sa))
2024 		return -EINVAL;
2025 
2026 	rtnl_lock();
2027 	dev = get_dev_from_nl(genl_info_net(info), attrs);
2028 	if (IS_ERR(dev)) {
2029 		rtnl_unlock();
2030 		return PTR_ERR(dev);
2031 	}
2032 
2033 	secy = &macsec_priv(dev)->secy;
2034 	tx_sc = &secy->tx_sc;
2035 
2036 	assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
2037 
2038 	if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
2039 		pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
2040 			  nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
2041 		rtnl_unlock();
2042 		return -EINVAL;
2043 	}
2044 
2045 	pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2046 	if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2047 		pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
2048 			  nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2049 		rtnl_unlock();
2050 		return -EINVAL;
2051 	}
2052 
2053 	if (secy->xpn) {
2054 		if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
2055 			rtnl_unlock();
2056 			return -EINVAL;
2057 		}
2058 
2059 		if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
2060 			pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n",
2061 				  nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
2062 				  MACSEC_SALT_LEN);
2063 			rtnl_unlock();
2064 			return -EINVAL;
2065 		}
2066 	}
2067 
2068 	tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
2069 	if (tx_sa) {
2070 		rtnl_unlock();
2071 		return -EBUSY;
2072 	}
2073 
2074 	tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
2075 	if (!tx_sa) {
2076 		rtnl_unlock();
2077 		return -ENOMEM;
2078 	}
2079 
2080 	err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2081 			 secy->key_len, secy->icv_len);
2082 	if (err < 0) {
2083 		kfree(tx_sa);
2084 		rtnl_unlock();
2085 		return err;
2086 	}
2087 
2088 	spin_lock_bh(&tx_sa->lock);
2089 	tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2090 	spin_unlock_bh(&tx_sa->lock);
2091 
2092 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2093 		tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2094 
2095 	was_operational = secy->operational;
2096 	if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
2097 		secy->operational = true;
2098 
2099 	if (secy->xpn) {
2100 		tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
2101 		nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
2102 			   MACSEC_SALT_LEN);
2103 	}
2104 
2105 	/* If h/w offloading is available, propagate to the device */
2106 	if (macsec_is_offloaded(netdev_priv(dev))) {
2107 		const struct macsec_ops *ops;
2108 		struct macsec_context ctx;
2109 
2110 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2111 		if (!ops) {
2112 			err = -EOPNOTSUPP;
2113 			goto cleanup;
2114 		}
2115 
2116 		ctx.sa.assoc_num = assoc_num;
2117 		ctx.sa.tx_sa = tx_sa;
2118 		ctx.secy = secy;
2119 		memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2120 		       secy->key_len);
2121 
2122 		err = macsec_offload(ops->mdo_add_txsa, &ctx);
2123 		memzero_explicit(ctx.sa.key, secy->key_len);
2124 		if (err)
2125 			goto cleanup;
2126 	}
2127 
2128 	nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
2129 	rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
2130 
2131 	rtnl_unlock();
2132 
2133 	return 0;
2134 
2135 cleanup:
2136 	secy->operational = was_operational;
2137 	macsec_txsa_put(tx_sa);
2138 	rtnl_unlock();
2139 	return err;
2140 }
2141 
macsec_del_rxsa(struct sk_buff * skb,struct genl_info * info)2142 static int macsec_del_rxsa(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 	struct macsec_rx_sa *rx_sa;
2149 	u8 assoc_num;
2150 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2151 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2152 	int ret;
2153 
2154 	if (!attrs[MACSEC_ATTR_IFINDEX])
2155 		return -EINVAL;
2156 
2157 	if (parse_sa_config(attrs, tb_sa))
2158 		return -EINVAL;
2159 
2160 	if (parse_rxsc_config(attrs, tb_rxsc))
2161 		return -EINVAL;
2162 
2163 	rtnl_lock();
2164 	rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2165 				 &dev, &secy, &rx_sc, &assoc_num);
2166 	if (IS_ERR(rx_sa)) {
2167 		rtnl_unlock();
2168 		return PTR_ERR(rx_sa);
2169 	}
2170 
2171 	if (rx_sa->active) {
2172 		rtnl_unlock();
2173 		return -EBUSY;
2174 	}
2175 
2176 	/* If h/w offloading is available, propagate to the device */
2177 	if (macsec_is_offloaded(netdev_priv(dev))) {
2178 		const struct macsec_ops *ops;
2179 		struct macsec_context ctx;
2180 
2181 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2182 		if (!ops) {
2183 			ret = -EOPNOTSUPP;
2184 			goto cleanup;
2185 		}
2186 
2187 		ctx.sa.assoc_num = assoc_num;
2188 		ctx.sa.rx_sa = rx_sa;
2189 		ctx.secy = secy;
2190 
2191 		ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
2192 		if (ret)
2193 			goto cleanup;
2194 	}
2195 
2196 	RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
2197 	clear_rx_sa(rx_sa);
2198 
2199 	rtnl_unlock();
2200 
2201 	return 0;
2202 
2203 cleanup:
2204 	rtnl_unlock();
2205 	return ret;
2206 }
2207 
macsec_del_rxsc(struct sk_buff * skb,struct genl_info * info)2208 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
2209 {
2210 	struct nlattr **attrs = info->attrs;
2211 	struct net_device *dev;
2212 	struct macsec_secy *secy;
2213 	struct macsec_rx_sc *rx_sc;
2214 	sci_t sci;
2215 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2216 	int ret;
2217 
2218 	if (!attrs[MACSEC_ATTR_IFINDEX])
2219 		return -EINVAL;
2220 
2221 	if (parse_rxsc_config(attrs, tb_rxsc))
2222 		return -EINVAL;
2223 
2224 	if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2225 		return -EINVAL;
2226 
2227 	rtnl_lock();
2228 	dev = get_dev_from_nl(genl_info_net(info), info->attrs);
2229 	if (IS_ERR(dev)) {
2230 		rtnl_unlock();
2231 		return PTR_ERR(dev);
2232 	}
2233 
2234 	secy = &macsec_priv(dev)->secy;
2235 	sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2236 
2237 	rx_sc = del_rx_sc(secy, sci);
2238 	if (!rx_sc) {
2239 		rtnl_unlock();
2240 		return -ENODEV;
2241 	}
2242 
2243 	/* If h/w offloading is available, propagate to the device */
2244 	if (macsec_is_offloaded(netdev_priv(dev))) {
2245 		const struct macsec_ops *ops;
2246 		struct macsec_context ctx;
2247 
2248 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2249 		if (!ops) {
2250 			ret = -EOPNOTSUPP;
2251 			goto cleanup;
2252 		}
2253 
2254 		ctx.rx_sc = rx_sc;
2255 		ctx.secy = secy;
2256 		ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
2257 		if (ret)
2258 			goto cleanup;
2259 	}
2260 
2261 	free_rx_sc(rx_sc);
2262 	rtnl_unlock();
2263 
2264 	return 0;
2265 
2266 cleanup:
2267 	rtnl_unlock();
2268 	return ret;
2269 }
2270 
macsec_del_txsa(struct sk_buff * skb,struct genl_info * info)2271 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2272 {
2273 	struct nlattr **attrs = info->attrs;
2274 	struct net_device *dev;
2275 	struct macsec_secy *secy;
2276 	struct macsec_tx_sc *tx_sc;
2277 	struct macsec_tx_sa *tx_sa;
2278 	u8 assoc_num;
2279 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2280 	int ret;
2281 
2282 	if (!attrs[MACSEC_ATTR_IFINDEX])
2283 		return -EINVAL;
2284 
2285 	if (parse_sa_config(attrs, tb_sa))
2286 		return -EINVAL;
2287 
2288 	rtnl_lock();
2289 	tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2290 				 &dev, &secy, &tx_sc, &assoc_num);
2291 	if (IS_ERR(tx_sa)) {
2292 		rtnl_unlock();
2293 		return PTR_ERR(tx_sa);
2294 	}
2295 
2296 	if (tx_sa->active) {
2297 		rtnl_unlock();
2298 		return -EBUSY;
2299 	}
2300 
2301 	/* If h/w offloading is available, propagate to the device */
2302 	if (macsec_is_offloaded(netdev_priv(dev))) {
2303 		const struct macsec_ops *ops;
2304 		struct macsec_context ctx;
2305 
2306 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2307 		if (!ops) {
2308 			ret = -EOPNOTSUPP;
2309 			goto cleanup;
2310 		}
2311 
2312 		ctx.sa.assoc_num = assoc_num;
2313 		ctx.sa.tx_sa = tx_sa;
2314 		ctx.secy = secy;
2315 
2316 		ret = macsec_offload(ops->mdo_del_txsa, &ctx);
2317 		if (ret)
2318 			goto cleanup;
2319 	}
2320 
2321 	RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2322 	clear_tx_sa(tx_sa);
2323 
2324 	rtnl_unlock();
2325 
2326 	return 0;
2327 
2328 cleanup:
2329 	rtnl_unlock();
2330 	return ret;
2331 }
2332 
validate_upd_sa(struct nlattr ** attrs)2333 static bool validate_upd_sa(struct nlattr **attrs)
2334 {
2335 	if (!attrs[MACSEC_SA_ATTR_AN] ||
2336 	    attrs[MACSEC_SA_ATTR_KEY] ||
2337 	    attrs[MACSEC_SA_ATTR_KEYID] ||
2338 	    attrs[MACSEC_SA_ATTR_SSCI] ||
2339 	    attrs[MACSEC_SA_ATTR_SALT])
2340 		return false;
2341 
2342 	if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2343 		return false;
2344 
2345 	if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
2346 		return false;
2347 
2348 	if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2349 		if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2350 			return false;
2351 	}
2352 
2353 	return true;
2354 }
2355 
macsec_upd_txsa(struct sk_buff * skb,struct genl_info * info)2356 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2357 {
2358 	struct nlattr **attrs = info->attrs;
2359 	struct net_device *dev;
2360 	struct macsec_secy *secy;
2361 	struct macsec_tx_sc *tx_sc;
2362 	struct macsec_tx_sa *tx_sa;
2363 	u8 assoc_num;
2364 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2365 	bool was_operational, was_active;
2366 	pn_t prev_pn;
2367 	int ret = 0;
2368 
2369 	prev_pn.full64 = 0;
2370 
2371 	if (!attrs[MACSEC_ATTR_IFINDEX])
2372 		return -EINVAL;
2373 
2374 	if (parse_sa_config(attrs, tb_sa))
2375 		return -EINVAL;
2376 
2377 	if (!validate_upd_sa(tb_sa))
2378 		return -EINVAL;
2379 
2380 	rtnl_lock();
2381 	tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2382 				 &dev, &secy, &tx_sc, &assoc_num);
2383 	if (IS_ERR(tx_sa)) {
2384 		rtnl_unlock();
2385 		return PTR_ERR(tx_sa);
2386 	}
2387 
2388 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2389 		int pn_len;
2390 
2391 		pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2392 		if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2393 			pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
2394 				  nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2395 			rtnl_unlock();
2396 			return -EINVAL;
2397 		}
2398 
2399 		spin_lock_bh(&tx_sa->lock);
2400 		prev_pn = tx_sa->next_pn_halves;
2401 		tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2402 		spin_unlock_bh(&tx_sa->lock);
2403 	}
2404 
2405 	was_active = tx_sa->active;
2406 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2407 		tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2408 
2409 	was_operational = secy->operational;
2410 	if (assoc_num == tx_sc->encoding_sa)
2411 		secy->operational = tx_sa->active;
2412 
2413 	/* If h/w offloading is available, propagate to the device */
2414 	if (macsec_is_offloaded(netdev_priv(dev))) {
2415 		const struct macsec_ops *ops;
2416 		struct macsec_context ctx;
2417 
2418 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2419 		if (!ops) {
2420 			ret = -EOPNOTSUPP;
2421 			goto cleanup;
2422 		}
2423 
2424 		ctx.sa.assoc_num = assoc_num;
2425 		ctx.sa.tx_sa = tx_sa;
2426 		ctx.sa.update_pn = !!prev_pn.full64;
2427 		ctx.secy = secy;
2428 
2429 		ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
2430 		if (ret)
2431 			goto cleanup;
2432 	}
2433 
2434 	rtnl_unlock();
2435 
2436 	return 0;
2437 
2438 cleanup:
2439 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2440 		spin_lock_bh(&tx_sa->lock);
2441 		tx_sa->next_pn_halves = prev_pn;
2442 		spin_unlock_bh(&tx_sa->lock);
2443 	}
2444 	tx_sa->active = was_active;
2445 	secy->operational = was_operational;
2446 	rtnl_unlock();
2447 	return ret;
2448 }
2449 
macsec_upd_rxsa(struct sk_buff * skb,struct genl_info * info)2450 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2451 {
2452 	struct nlattr **attrs = info->attrs;
2453 	struct net_device *dev;
2454 	struct macsec_secy *secy;
2455 	struct macsec_rx_sc *rx_sc;
2456 	struct macsec_rx_sa *rx_sa;
2457 	u8 assoc_num;
2458 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2459 	struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2460 	bool was_active;
2461 	pn_t prev_pn;
2462 	int ret = 0;
2463 
2464 	prev_pn.full64 = 0;
2465 
2466 	if (!attrs[MACSEC_ATTR_IFINDEX])
2467 		return -EINVAL;
2468 
2469 	if (parse_rxsc_config(attrs, tb_rxsc))
2470 		return -EINVAL;
2471 
2472 	if (parse_sa_config(attrs, tb_sa))
2473 		return -EINVAL;
2474 
2475 	if (!validate_upd_sa(tb_sa))
2476 		return -EINVAL;
2477 
2478 	rtnl_lock();
2479 	rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2480 				 &dev, &secy, &rx_sc, &assoc_num);
2481 	if (IS_ERR(rx_sa)) {
2482 		rtnl_unlock();
2483 		return PTR_ERR(rx_sa);
2484 	}
2485 
2486 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2487 		int pn_len;
2488 
2489 		pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2490 		if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2491 			pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
2492 				  nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2493 			rtnl_unlock();
2494 			return -EINVAL;
2495 		}
2496 
2497 		spin_lock_bh(&rx_sa->lock);
2498 		prev_pn = rx_sa->next_pn_halves;
2499 		rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2500 		spin_unlock_bh(&rx_sa->lock);
2501 	}
2502 
2503 	was_active = rx_sa->active;
2504 	if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2505 		rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2506 
2507 	/* If h/w offloading is available, propagate to the device */
2508 	if (macsec_is_offloaded(netdev_priv(dev))) {
2509 		const struct macsec_ops *ops;
2510 		struct macsec_context ctx;
2511 
2512 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2513 		if (!ops) {
2514 			ret = -EOPNOTSUPP;
2515 			goto cleanup;
2516 		}
2517 
2518 		ctx.sa.assoc_num = assoc_num;
2519 		ctx.sa.rx_sa = rx_sa;
2520 		ctx.sa.update_pn = !!prev_pn.full64;
2521 		ctx.secy = secy;
2522 
2523 		ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
2524 		if (ret)
2525 			goto cleanup;
2526 	}
2527 
2528 	rtnl_unlock();
2529 	return 0;
2530 
2531 cleanup:
2532 	if (tb_sa[MACSEC_SA_ATTR_PN]) {
2533 		spin_lock_bh(&rx_sa->lock);
2534 		rx_sa->next_pn_halves = prev_pn;
2535 		spin_unlock_bh(&rx_sa->lock);
2536 	}
2537 	rx_sa->active = was_active;
2538 	rtnl_unlock();
2539 	return ret;
2540 }
2541 
macsec_upd_rxsc(struct sk_buff * skb,struct genl_info * info)2542 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2543 {
2544 	struct nlattr **attrs = info->attrs;
2545 	struct net_device *dev;
2546 	struct macsec_secy *secy;
2547 	struct macsec_rx_sc *rx_sc;
2548 	struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2549 	unsigned int prev_n_rx_sc;
2550 	bool was_active;
2551 	int ret;
2552 
2553 	if (!attrs[MACSEC_ATTR_IFINDEX])
2554 		return -EINVAL;
2555 
2556 	if (parse_rxsc_config(attrs, tb_rxsc))
2557 		return -EINVAL;
2558 
2559 	if (!validate_add_rxsc(tb_rxsc))
2560 		return -EINVAL;
2561 
2562 	rtnl_lock();
2563 	rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2564 	if (IS_ERR(rx_sc)) {
2565 		rtnl_unlock();
2566 		return PTR_ERR(rx_sc);
2567 	}
2568 
2569 	was_active = rx_sc->active;
2570 	prev_n_rx_sc = secy->n_rx_sc;
2571 	if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2572 		bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2573 
2574 		if (rx_sc->active != new)
2575 			secy->n_rx_sc += new ? 1 : -1;
2576 
2577 		rx_sc->active = new;
2578 	}
2579 
2580 	/* If h/w offloading is available, propagate to the device */
2581 	if (macsec_is_offloaded(netdev_priv(dev))) {
2582 		const struct macsec_ops *ops;
2583 		struct macsec_context ctx;
2584 
2585 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
2586 		if (!ops) {
2587 			ret = -EOPNOTSUPP;
2588 			goto cleanup;
2589 		}
2590 
2591 		ctx.rx_sc = rx_sc;
2592 		ctx.secy = secy;
2593 
2594 		ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
2595 		if (ret)
2596 			goto cleanup;
2597 	}
2598 
2599 	rtnl_unlock();
2600 
2601 	return 0;
2602 
2603 cleanup:
2604 	secy->n_rx_sc = prev_n_rx_sc;
2605 	rx_sc->active = was_active;
2606 	rtnl_unlock();
2607 	return ret;
2608 }
2609 
macsec_is_configured(struct macsec_dev * macsec)2610 static bool macsec_is_configured(struct macsec_dev *macsec)
2611 {
2612 	struct macsec_secy *secy = &macsec->secy;
2613 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2614 	int i;
2615 
2616 	if (secy->rx_sc)
2617 		return true;
2618 
2619 	for (i = 0; i < MACSEC_NUM_AN; i++)
2620 		if (tx_sc->sa[i])
2621 			return true;
2622 
2623 	return false;
2624 }
2625 
macsec_needs_tx_tag(struct macsec_dev * macsec,const struct macsec_ops * ops)2626 static bool macsec_needs_tx_tag(struct macsec_dev *macsec,
2627 				const struct macsec_ops *ops)
2628 {
2629 	return macsec->offload == MACSEC_OFFLOAD_PHY &&
2630 		ops->mdo_insert_tx_tag;
2631 }
2632 
macsec_set_head_tail_room(struct net_device * dev)2633 static void macsec_set_head_tail_room(struct net_device *dev)
2634 {
2635 	struct macsec_dev *macsec = macsec_priv(dev);
2636 	struct net_device *real_dev = macsec->real_dev;
2637 	int needed_headroom, needed_tailroom;
2638 	const struct macsec_ops *ops;
2639 
2640 	ops = macsec_get_ops(macsec, NULL);
2641 	if (ops) {
2642 		needed_headroom = ops->needed_headroom;
2643 		needed_tailroom = ops->needed_tailroom;
2644 	} else {
2645 		needed_headroom = MACSEC_NEEDED_HEADROOM;
2646 		needed_tailroom = MACSEC_NEEDED_TAILROOM;
2647 	}
2648 
2649 	dev->needed_headroom = real_dev->needed_headroom + needed_headroom;
2650 	dev->needed_tailroom = real_dev->needed_tailroom + needed_tailroom;
2651 }
2652 
macsec_inherit_tso_max(struct net_device * dev)2653 static void macsec_inherit_tso_max(struct net_device *dev)
2654 {
2655 	struct macsec_dev *macsec = macsec_priv(dev);
2656 
2657 	/* if macsec is offloaded, we need to follow the lower
2658 	 * device's capabilities. otherwise, we can ignore them.
2659 	 */
2660 	if (macsec_is_offloaded(macsec))
2661 		netif_inherit_tso_max(dev, macsec->real_dev);
2662 }
2663 
macsec_update_offload(struct net_device * dev,enum macsec_offload offload)2664 static int macsec_update_offload(struct net_device *dev, enum macsec_offload offload)
2665 {
2666 	enum macsec_offload prev_offload;
2667 	const struct macsec_ops *ops;
2668 	struct macsec_context ctx;
2669 	struct macsec_dev *macsec;
2670 	int ret = 0;
2671 
2672 	macsec = macsec_priv(dev);
2673 
2674 	/* Check if the offloading mode is supported by the underlying layers */
2675 	if (offload != MACSEC_OFFLOAD_OFF &&
2676 	    !macsec_check_offload(offload, macsec))
2677 		return -EOPNOTSUPP;
2678 
2679 	/* Check if the net device is busy. */
2680 	if (netif_running(dev))
2681 		return -EBUSY;
2682 
2683 	/* Check if the device already has rules configured: we do not support
2684 	 * rules migration.
2685 	 */
2686 	if (macsec_is_configured(macsec))
2687 		return -EBUSY;
2688 
2689 	prev_offload = macsec->offload;
2690 
2691 	ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
2692 			       macsec, &ctx);
2693 	if (!ops)
2694 		return -EOPNOTSUPP;
2695 
2696 	macsec->offload = offload;
2697 
2698 	ctx.secy = &macsec->secy;
2699 	ret = offload == MACSEC_OFFLOAD_OFF ? macsec_offload(ops->mdo_del_secy, &ctx)
2700 					    : macsec_offload(ops->mdo_add_secy, &ctx);
2701 	if (ret) {
2702 		macsec->offload = prev_offload;
2703 		return ret;
2704 	}
2705 
2706 	macsec_set_head_tail_room(dev);
2707 	macsec->insert_tx_tag = macsec_needs_tx_tag(macsec, ops);
2708 
2709 	macsec_inherit_tso_max(dev);
2710 
2711 	netdev_update_features(dev);
2712 
2713 	return ret;
2714 }
2715 
macsec_upd_offload(struct sk_buff * skb,struct genl_info * info)2716 static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
2717 {
2718 	struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
2719 	struct nlattr **attrs = info->attrs;
2720 	enum macsec_offload offload;
2721 	struct macsec_dev *macsec;
2722 	struct net_device *dev;
2723 	int ret = 0;
2724 
2725 	if (!attrs[MACSEC_ATTR_IFINDEX])
2726 		return -EINVAL;
2727 
2728 	if (!attrs[MACSEC_ATTR_OFFLOAD])
2729 		return -EINVAL;
2730 
2731 	if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
2732 					attrs[MACSEC_ATTR_OFFLOAD],
2733 					macsec_genl_offload_policy, NULL))
2734 		return -EINVAL;
2735 
2736 	rtnl_lock();
2737 
2738 	dev = get_dev_from_nl(genl_info_net(info), attrs);
2739 	if (IS_ERR(dev)) {
2740 		ret = PTR_ERR(dev);
2741 		goto out;
2742 	}
2743 	macsec = macsec_priv(dev);
2744 
2745 	if (!tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]) {
2746 		ret = -EINVAL;
2747 		goto out;
2748 	}
2749 
2750 	offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
2751 
2752 	if (macsec->offload != offload)
2753 		ret = macsec_update_offload(dev, offload);
2754 out:
2755 	rtnl_unlock();
2756 	return ret;
2757 }
2758 
get_tx_sa_stats(struct net_device * dev,int an,struct macsec_tx_sa * tx_sa,struct macsec_tx_sa_stats * sum)2759 static void get_tx_sa_stats(struct net_device *dev, int an,
2760 			    struct macsec_tx_sa *tx_sa,
2761 			    struct macsec_tx_sa_stats *sum)
2762 {
2763 	struct macsec_dev *macsec = macsec_priv(dev);
2764 	int cpu;
2765 
2766 	/* If h/w offloading is available, propagate to the device */
2767 	if (macsec_is_offloaded(macsec)) {
2768 		const struct macsec_ops *ops;
2769 		struct macsec_context ctx;
2770 
2771 		ops = macsec_get_ops(macsec, &ctx);
2772 		if (ops) {
2773 			ctx.sa.assoc_num = an;
2774 			ctx.sa.tx_sa = tx_sa;
2775 			ctx.stats.tx_sa_stats = sum;
2776 			ctx.secy = &macsec_priv(dev)->secy;
2777 			macsec_offload(ops->mdo_get_tx_sa_stats, &ctx);
2778 		}
2779 		return;
2780 	}
2781 
2782 	for_each_possible_cpu(cpu) {
2783 		const struct macsec_tx_sa_stats *stats =
2784 			per_cpu_ptr(tx_sa->stats, cpu);
2785 
2786 		sum->OutPktsProtected += stats->OutPktsProtected;
2787 		sum->OutPktsEncrypted += stats->OutPktsEncrypted;
2788 	}
2789 }
2790 
copy_tx_sa_stats(struct sk_buff * skb,struct macsec_tx_sa_stats * sum)2791 static int copy_tx_sa_stats(struct sk_buff *skb, struct macsec_tx_sa_stats *sum)
2792 {
2793 	if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED,
2794 			sum->OutPktsProtected) ||
2795 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2796 			sum->OutPktsEncrypted))
2797 		return -EMSGSIZE;
2798 
2799 	return 0;
2800 }
2801 
get_rx_sa_stats(struct net_device * dev,struct macsec_rx_sc * rx_sc,int an,struct macsec_rx_sa * rx_sa,struct macsec_rx_sa_stats * sum)2802 static void get_rx_sa_stats(struct net_device *dev,
2803 			    struct macsec_rx_sc *rx_sc, int an,
2804 			    struct macsec_rx_sa *rx_sa,
2805 			    struct macsec_rx_sa_stats *sum)
2806 {
2807 	struct macsec_dev *macsec = macsec_priv(dev);
2808 	int cpu;
2809 
2810 	/* If h/w offloading is available, propagate to the device */
2811 	if (macsec_is_offloaded(macsec)) {
2812 		const struct macsec_ops *ops;
2813 		struct macsec_context ctx;
2814 
2815 		ops = macsec_get_ops(macsec, &ctx);
2816 		if (ops) {
2817 			ctx.sa.assoc_num = an;
2818 			ctx.sa.rx_sa = rx_sa;
2819 			ctx.stats.rx_sa_stats = sum;
2820 			ctx.secy = &macsec_priv(dev)->secy;
2821 			ctx.rx_sc = rx_sc;
2822 			macsec_offload(ops->mdo_get_rx_sa_stats, &ctx);
2823 		}
2824 		return;
2825 	}
2826 
2827 	for_each_possible_cpu(cpu) {
2828 		const struct macsec_rx_sa_stats *stats =
2829 			per_cpu_ptr(rx_sa->stats, cpu);
2830 
2831 		sum->InPktsOK         += stats->InPktsOK;
2832 		sum->InPktsInvalid    += stats->InPktsInvalid;
2833 		sum->InPktsNotValid   += stats->InPktsNotValid;
2834 		sum->InPktsNotUsingSA += stats->InPktsNotUsingSA;
2835 		sum->InPktsUnusedSA   += stats->InPktsUnusedSA;
2836 	}
2837 }
2838 
copy_rx_sa_stats(struct sk_buff * skb,struct macsec_rx_sa_stats * sum)2839 static int copy_rx_sa_stats(struct sk_buff *skb,
2840 			    struct macsec_rx_sa_stats *sum)
2841 {
2842 	if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum->InPktsOK) ||
2843 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID,
2844 			sum->InPktsInvalid) ||
2845 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID,
2846 			sum->InPktsNotValid) ||
2847 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2848 			sum->InPktsNotUsingSA) ||
2849 	    nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA,
2850 			sum->InPktsUnusedSA))
2851 		return -EMSGSIZE;
2852 
2853 	return 0;
2854 }
2855 
get_rx_sc_stats(struct net_device * dev,struct macsec_rx_sc * rx_sc,struct macsec_rx_sc_stats * sum)2856 static void get_rx_sc_stats(struct net_device *dev,
2857 			    struct macsec_rx_sc *rx_sc,
2858 			    struct macsec_rx_sc_stats *sum)
2859 {
2860 	struct macsec_dev *macsec = macsec_priv(dev);
2861 	int cpu;
2862 
2863 	/* If h/w offloading is available, propagate to the device */
2864 	if (macsec_is_offloaded(macsec)) {
2865 		const struct macsec_ops *ops;
2866 		struct macsec_context ctx;
2867 
2868 		ops = macsec_get_ops(macsec, &ctx);
2869 		if (ops) {
2870 			ctx.stats.rx_sc_stats = sum;
2871 			ctx.secy = &macsec_priv(dev)->secy;
2872 			ctx.rx_sc = rx_sc;
2873 			macsec_offload(ops->mdo_get_rx_sc_stats, &ctx);
2874 		}
2875 		return;
2876 	}
2877 
2878 	for_each_possible_cpu(cpu) {
2879 		const struct pcpu_rx_sc_stats *stats;
2880 		struct macsec_rx_sc_stats tmp;
2881 		unsigned int start;
2882 
2883 		stats = per_cpu_ptr(rx_sc->stats, cpu);
2884 		do {
2885 			start = u64_stats_fetch_begin(&stats->syncp);
2886 			memcpy(&tmp, &stats->stats, sizeof(tmp));
2887 		} while (u64_stats_fetch_retry(&stats->syncp, start));
2888 
2889 		sum->InOctetsValidated += tmp.InOctetsValidated;
2890 		sum->InOctetsDecrypted += tmp.InOctetsDecrypted;
2891 		sum->InPktsUnchecked   += tmp.InPktsUnchecked;
2892 		sum->InPktsDelayed     += tmp.InPktsDelayed;
2893 		sum->InPktsOK          += tmp.InPktsOK;
2894 		sum->InPktsInvalid     += tmp.InPktsInvalid;
2895 		sum->InPktsLate        += tmp.InPktsLate;
2896 		sum->InPktsNotValid    += tmp.InPktsNotValid;
2897 		sum->InPktsNotUsingSA  += tmp.InPktsNotUsingSA;
2898 		sum->InPktsUnusedSA    += tmp.InPktsUnusedSA;
2899 	}
2900 }
2901 
copy_rx_sc_stats(struct sk_buff * skb,struct macsec_rx_sc_stats * sum)2902 static int copy_rx_sc_stats(struct sk_buff *skb, struct macsec_rx_sc_stats *sum)
2903 {
2904 	if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2905 			      sum->InOctetsValidated,
2906 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2907 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2908 			      sum->InOctetsDecrypted,
2909 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2910 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2911 			      sum->InPktsUnchecked,
2912 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2913 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2914 			      sum->InPktsDelayed,
2915 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2916 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2917 			      sum->InPktsOK,
2918 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2919 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2920 			      sum->InPktsInvalid,
2921 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2922 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2923 			      sum->InPktsLate,
2924 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2925 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2926 			      sum->InPktsNotValid,
2927 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2928 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2929 			      sum->InPktsNotUsingSA,
2930 			      MACSEC_RXSC_STATS_ATTR_PAD) ||
2931 	    nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2932 			      sum->InPktsUnusedSA,
2933 			      MACSEC_RXSC_STATS_ATTR_PAD))
2934 		return -EMSGSIZE;
2935 
2936 	return 0;
2937 }
2938 
get_tx_sc_stats(struct net_device * dev,struct macsec_tx_sc_stats * sum)2939 static void get_tx_sc_stats(struct net_device *dev,
2940 			    struct macsec_tx_sc_stats *sum)
2941 {
2942 	struct macsec_dev *macsec = macsec_priv(dev);
2943 	int cpu;
2944 
2945 	/* If h/w offloading is available, propagate to the device */
2946 	if (macsec_is_offloaded(macsec)) {
2947 		const struct macsec_ops *ops;
2948 		struct macsec_context ctx;
2949 
2950 		ops = macsec_get_ops(macsec, &ctx);
2951 		if (ops) {
2952 			ctx.stats.tx_sc_stats = sum;
2953 			ctx.secy = &macsec_priv(dev)->secy;
2954 			macsec_offload(ops->mdo_get_tx_sc_stats, &ctx);
2955 		}
2956 		return;
2957 	}
2958 
2959 	for_each_possible_cpu(cpu) {
2960 		const struct pcpu_tx_sc_stats *stats;
2961 		struct macsec_tx_sc_stats tmp;
2962 		unsigned int start;
2963 
2964 		stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu);
2965 		do {
2966 			start = u64_stats_fetch_begin(&stats->syncp);
2967 			memcpy(&tmp, &stats->stats, sizeof(tmp));
2968 		} while (u64_stats_fetch_retry(&stats->syncp, start));
2969 
2970 		sum->OutPktsProtected   += tmp.OutPktsProtected;
2971 		sum->OutPktsEncrypted   += tmp.OutPktsEncrypted;
2972 		sum->OutOctetsProtected += tmp.OutOctetsProtected;
2973 		sum->OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2974 	}
2975 }
2976 
copy_tx_sc_stats(struct sk_buff * skb,struct macsec_tx_sc_stats * sum)2977 static int copy_tx_sc_stats(struct sk_buff *skb, struct macsec_tx_sc_stats *sum)
2978 {
2979 	if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2980 			      sum->OutPktsProtected,
2981 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2982 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2983 			      sum->OutPktsEncrypted,
2984 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2985 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2986 			      sum->OutOctetsProtected,
2987 			      MACSEC_TXSC_STATS_ATTR_PAD) ||
2988 	    nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2989 			      sum->OutOctetsEncrypted,
2990 			      MACSEC_TXSC_STATS_ATTR_PAD))
2991 		return -EMSGSIZE;
2992 
2993 	return 0;
2994 }
2995 
get_secy_stats(struct net_device * dev,struct macsec_dev_stats * sum)2996 static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum)
2997 {
2998 	struct macsec_dev *macsec = macsec_priv(dev);
2999 	int cpu;
3000 
3001 	/* If h/w offloading is available, propagate to the device */
3002 	if (macsec_is_offloaded(macsec)) {
3003 		const struct macsec_ops *ops;
3004 		struct macsec_context ctx;
3005 
3006 		ops = macsec_get_ops(macsec, &ctx);
3007 		if (ops) {
3008 			ctx.stats.dev_stats = sum;
3009 			ctx.secy = &macsec_priv(dev)->secy;
3010 			macsec_offload(ops->mdo_get_dev_stats, &ctx);
3011 		}
3012 		return;
3013 	}
3014 
3015 	for_each_possible_cpu(cpu) {
3016 		const struct pcpu_secy_stats *stats;
3017 		struct macsec_dev_stats tmp;
3018 		unsigned int start;
3019 
3020 		stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu);
3021 		do {
3022 			start = u64_stats_fetch_begin(&stats->syncp);
3023 			memcpy(&tmp, &stats->stats, sizeof(tmp));
3024 		} while (u64_stats_fetch_retry(&stats->syncp, start));
3025 
3026 		sum->OutPktsUntagged  += tmp.OutPktsUntagged;
3027 		sum->InPktsUntagged   += tmp.InPktsUntagged;
3028 		sum->OutPktsTooLong   += tmp.OutPktsTooLong;
3029 		sum->InPktsNoTag      += tmp.InPktsNoTag;
3030 		sum->InPktsBadTag     += tmp.InPktsBadTag;
3031 		sum->InPktsUnknownSCI += tmp.InPktsUnknownSCI;
3032 		sum->InPktsNoSCI      += tmp.InPktsNoSCI;
3033 		sum->InPktsOverrun    += tmp.InPktsOverrun;
3034 	}
3035 }
3036 
copy_secy_stats(struct sk_buff * skb,struct macsec_dev_stats * sum)3037 static int copy_secy_stats(struct sk_buff *skb, struct macsec_dev_stats *sum)
3038 {
3039 	if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
3040 			      sum->OutPktsUntagged,
3041 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3042 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
3043 			      sum->InPktsUntagged,
3044 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3045 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
3046 			      sum->OutPktsTooLong,
3047 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3048 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
3049 			      sum->InPktsNoTag,
3050 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3051 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
3052 			      sum->InPktsBadTag,
3053 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3054 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
3055 			      sum->InPktsUnknownSCI,
3056 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3057 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
3058 			      sum->InPktsNoSCI,
3059 			      MACSEC_SECY_STATS_ATTR_PAD) ||
3060 	    nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
3061 			      sum->InPktsOverrun,
3062 			      MACSEC_SECY_STATS_ATTR_PAD))
3063 		return -EMSGSIZE;
3064 
3065 	return 0;
3066 }
3067 
nla_put_secy(struct macsec_secy * secy,struct sk_buff * skb)3068 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
3069 {
3070 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3071 	struct nlattr *secy_nest = nla_nest_start_noflag(skb,
3072 							 MACSEC_ATTR_SECY);
3073 	u64 csid;
3074 
3075 	if (!secy_nest)
3076 		return 1;
3077 
3078 	switch (secy->key_len) {
3079 	case MACSEC_GCM_AES_128_SAK_LEN:
3080 		csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
3081 		break;
3082 	case MACSEC_GCM_AES_256_SAK_LEN:
3083 		csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
3084 		break;
3085 	default:
3086 		goto cancel;
3087 	}
3088 
3089 	if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
3090 			MACSEC_SECY_ATTR_PAD) ||
3091 	    nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
3092 			      csid, MACSEC_SECY_ATTR_PAD) ||
3093 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
3094 	    nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
3095 	    nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
3096 	    nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
3097 	    nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
3098 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
3099 	    nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
3100 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
3101 	    nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
3102 	    nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
3103 		goto cancel;
3104 
3105 	if (secy->replay_protect) {
3106 		if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
3107 			goto cancel;
3108 	}
3109 
3110 	nla_nest_end(skb, secy_nest);
3111 	return 0;
3112 
3113 cancel:
3114 	nla_nest_cancel(skb, secy_nest);
3115 	return 1;
3116 }
3117 
3118 static noinline_for_stack int
dump_secy(struct macsec_secy * secy,struct net_device * dev,struct sk_buff * skb,struct netlink_callback * cb)3119 dump_secy(struct macsec_secy *secy, struct net_device *dev,
3120 	  struct sk_buff *skb, struct netlink_callback *cb)
3121 {
3122 	struct macsec_tx_sc_stats tx_sc_stats = {0, };
3123 	struct macsec_tx_sa_stats tx_sa_stats = {0, };
3124 	struct macsec_rx_sc_stats rx_sc_stats = {0, };
3125 	struct macsec_rx_sa_stats rx_sa_stats = {0, };
3126 	struct macsec_dev *macsec = netdev_priv(dev);
3127 	struct macsec_dev_stats dev_stats = {0, };
3128 	struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3129 	struct nlattr *txsa_list, *rxsc_list;
3130 	struct macsec_rx_sc *rx_sc;
3131 	struct nlattr *attr;
3132 	void *hdr;
3133 	int i, j;
3134 
3135 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3136 			  &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
3137 	if (!hdr)
3138 		return -EMSGSIZE;
3139 
3140 	genl_dump_check_consistent(cb, hdr);
3141 
3142 	if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
3143 		goto nla_put_failure;
3144 
3145 	attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
3146 	if (!attr)
3147 		goto nla_put_failure;
3148 	if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
3149 		goto nla_put_failure;
3150 	nla_nest_end(skb, attr);
3151 
3152 	if (nla_put_secy(secy, skb))
3153 		goto nla_put_failure;
3154 
3155 	attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
3156 	if (!attr)
3157 		goto nla_put_failure;
3158 
3159 	get_tx_sc_stats(dev, &tx_sc_stats);
3160 	if (copy_tx_sc_stats(skb, &tx_sc_stats)) {
3161 		nla_nest_cancel(skb, attr);
3162 		goto nla_put_failure;
3163 	}
3164 	nla_nest_end(skb, attr);
3165 
3166 	attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
3167 	if (!attr)
3168 		goto nla_put_failure;
3169 	get_secy_stats(dev, &dev_stats);
3170 	if (copy_secy_stats(skb, &dev_stats)) {
3171 		nla_nest_cancel(skb, attr);
3172 		goto nla_put_failure;
3173 	}
3174 	nla_nest_end(skb, attr);
3175 
3176 	txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
3177 	if (!txsa_list)
3178 		goto nla_put_failure;
3179 	for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
3180 		struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
3181 		struct nlattr *txsa_nest;
3182 		u64 pn;
3183 		int pn_len;
3184 
3185 		if (!tx_sa)
3186 			continue;
3187 
3188 		txsa_nest = nla_nest_start_noflag(skb, j++);
3189 		if (!txsa_nest) {
3190 			nla_nest_cancel(skb, txsa_list);
3191 			goto nla_put_failure;
3192 		}
3193 
3194 		attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
3195 		if (!attr) {
3196 			nla_nest_cancel(skb, txsa_nest);
3197 			nla_nest_cancel(skb, txsa_list);
3198 			goto nla_put_failure;
3199 		}
3200 		memset(&tx_sa_stats, 0, sizeof(tx_sa_stats));
3201 		get_tx_sa_stats(dev, i, tx_sa, &tx_sa_stats);
3202 		if (copy_tx_sa_stats(skb, &tx_sa_stats)) {
3203 			nla_nest_cancel(skb, attr);
3204 			nla_nest_cancel(skb, txsa_nest);
3205 			nla_nest_cancel(skb, txsa_list);
3206 			goto nla_put_failure;
3207 		}
3208 		nla_nest_end(skb, attr);
3209 
3210 		if (secy->xpn) {
3211 			pn = tx_sa->next_pn;
3212 			pn_len = MACSEC_XPN_PN_LEN;
3213 		} else {
3214 			pn = tx_sa->next_pn_halves.lower;
3215 			pn_len = MACSEC_DEFAULT_PN_LEN;
3216 		}
3217 
3218 		if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3219 		    nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3220 		    nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
3221 		    (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) ||
3222 		    nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
3223 			nla_nest_cancel(skb, txsa_nest);
3224 			nla_nest_cancel(skb, txsa_list);
3225 			goto nla_put_failure;
3226 		}
3227 
3228 		nla_nest_end(skb, txsa_nest);
3229 	}
3230 	nla_nest_end(skb, txsa_list);
3231 
3232 	rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
3233 	if (!rxsc_list)
3234 		goto nla_put_failure;
3235 
3236 	j = 1;
3237 	for_each_rxsc_rtnl(secy, rx_sc) {
3238 		int k;
3239 		struct nlattr *rxsa_list;
3240 		struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
3241 
3242 		if (!rxsc_nest) {
3243 			nla_nest_cancel(skb, rxsc_list);
3244 			goto nla_put_failure;
3245 		}
3246 
3247 		if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
3248 		    nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
3249 				MACSEC_RXSC_ATTR_PAD)) {
3250 			nla_nest_cancel(skb, rxsc_nest);
3251 			nla_nest_cancel(skb, rxsc_list);
3252 			goto nla_put_failure;
3253 		}
3254 
3255 		attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
3256 		if (!attr) {
3257 			nla_nest_cancel(skb, rxsc_nest);
3258 			nla_nest_cancel(skb, rxsc_list);
3259 			goto nla_put_failure;
3260 		}
3261 		memset(&rx_sc_stats, 0, sizeof(rx_sc_stats));
3262 		get_rx_sc_stats(dev, rx_sc, &rx_sc_stats);
3263 		if (copy_rx_sc_stats(skb, &rx_sc_stats)) {
3264 			nla_nest_cancel(skb, attr);
3265 			nla_nest_cancel(skb, rxsc_nest);
3266 			nla_nest_cancel(skb, rxsc_list);
3267 			goto nla_put_failure;
3268 		}
3269 		nla_nest_end(skb, attr);
3270 
3271 		rxsa_list = nla_nest_start_noflag(skb,
3272 						  MACSEC_RXSC_ATTR_SA_LIST);
3273 		if (!rxsa_list) {
3274 			nla_nest_cancel(skb, rxsc_nest);
3275 			nla_nest_cancel(skb, rxsc_list);
3276 			goto nla_put_failure;
3277 		}
3278 
3279 		for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
3280 			struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
3281 			struct nlattr *rxsa_nest;
3282 			u64 pn;
3283 			int pn_len;
3284 
3285 			if (!rx_sa)
3286 				continue;
3287 
3288 			rxsa_nest = nla_nest_start_noflag(skb, k++);
3289 			if (!rxsa_nest) {
3290 				nla_nest_cancel(skb, rxsa_list);
3291 				nla_nest_cancel(skb, rxsc_nest);
3292 				nla_nest_cancel(skb, rxsc_list);
3293 				goto nla_put_failure;
3294 			}
3295 
3296 			attr = nla_nest_start_noflag(skb,
3297 						     MACSEC_SA_ATTR_STATS);
3298 			if (!attr) {
3299 				nla_nest_cancel(skb, rxsa_list);
3300 				nla_nest_cancel(skb, rxsc_nest);
3301 				nla_nest_cancel(skb, rxsc_list);
3302 				goto nla_put_failure;
3303 			}
3304 			memset(&rx_sa_stats, 0, sizeof(rx_sa_stats));
3305 			get_rx_sa_stats(dev, rx_sc, i, rx_sa, &rx_sa_stats);
3306 			if (copy_rx_sa_stats(skb, &rx_sa_stats)) {
3307 				nla_nest_cancel(skb, attr);
3308 				nla_nest_cancel(skb, rxsa_list);
3309 				nla_nest_cancel(skb, rxsc_nest);
3310 				nla_nest_cancel(skb, rxsc_list);
3311 				goto nla_put_failure;
3312 			}
3313 			nla_nest_end(skb, attr);
3314 
3315 			if (secy->xpn) {
3316 				pn = rx_sa->next_pn;
3317 				pn_len = MACSEC_XPN_PN_LEN;
3318 			} else {
3319 				pn = rx_sa->next_pn_halves.lower;
3320 				pn_len = MACSEC_DEFAULT_PN_LEN;
3321 			}
3322 
3323 			if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3324 			    nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3325 			    nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
3326 			    (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) ||
3327 			    nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
3328 				nla_nest_cancel(skb, rxsa_nest);
3329 				nla_nest_cancel(skb, rxsc_nest);
3330 				nla_nest_cancel(skb, rxsc_list);
3331 				goto nla_put_failure;
3332 			}
3333 			nla_nest_end(skb, rxsa_nest);
3334 		}
3335 
3336 		nla_nest_end(skb, rxsa_list);
3337 		nla_nest_end(skb, rxsc_nest);
3338 	}
3339 
3340 	nla_nest_end(skb, rxsc_list);
3341 
3342 	genlmsg_end(skb, hdr);
3343 
3344 	return 0;
3345 
3346 nla_put_failure:
3347 	genlmsg_cancel(skb, hdr);
3348 	return -EMSGSIZE;
3349 }
3350 
3351 static int macsec_generation = 1; /* protected by RTNL */
3352 
macsec_dump_txsc(struct sk_buff * skb,struct netlink_callback * cb)3353 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
3354 {
3355 	struct net *net = sock_net(skb->sk);
3356 	struct net_device *dev;
3357 	int dev_idx, d;
3358 
3359 	dev_idx = cb->args[0];
3360 
3361 	d = 0;
3362 	rtnl_lock();
3363 
3364 	cb->seq = macsec_generation;
3365 
3366 	for_each_netdev(net, dev) {
3367 		struct macsec_secy *secy;
3368 
3369 		if (d < dev_idx)
3370 			goto next;
3371 
3372 		if (!netif_is_macsec(dev))
3373 			goto next;
3374 
3375 		secy = &macsec_priv(dev)->secy;
3376 		if (dump_secy(secy, dev, skb, cb) < 0)
3377 			goto done;
3378 next:
3379 		d++;
3380 	}
3381 
3382 done:
3383 	rtnl_unlock();
3384 	cb->args[0] = d;
3385 	return skb->len;
3386 }
3387 
3388 static const struct genl_small_ops macsec_genl_ops[] = {
3389 	{
3390 		.cmd = MACSEC_CMD_GET_TXSC,
3391 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3392 		.dumpit = macsec_dump_txsc,
3393 	},
3394 	{
3395 		.cmd = MACSEC_CMD_ADD_RXSC,
3396 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3397 		.doit = macsec_add_rxsc,
3398 		.flags = GENL_ADMIN_PERM,
3399 	},
3400 	{
3401 		.cmd = MACSEC_CMD_DEL_RXSC,
3402 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3403 		.doit = macsec_del_rxsc,
3404 		.flags = GENL_ADMIN_PERM,
3405 	},
3406 	{
3407 		.cmd = MACSEC_CMD_UPD_RXSC,
3408 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3409 		.doit = macsec_upd_rxsc,
3410 		.flags = GENL_ADMIN_PERM,
3411 	},
3412 	{
3413 		.cmd = MACSEC_CMD_ADD_TXSA,
3414 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3415 		.doit = macsec_add_txsa,
3416 		.flags = GENL_ADMIN_PERM,
3417 	},
3418 	{
3419 		.cmd = MACSEC_CMD_DEL_TXSA,
3420 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3421 		.doit = macsec_del_txsa,
3422 		.flags = GENL_ADMIN_PERM,
3423 	},
3424 	{
3425 		.cmd = MACSEC_CMD_UPD_TXSA,
3426 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3427 		.doit = macsec_upd_txsa,
3428 		.flags = GENL_ADMIN_PERM,
3429 	},
3430 	{
3431 		.cmd = MACSEC_CMD_ADD_RXSA,
3432 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3433 		.doit = macsec_add_rxsa,
3434 		.flags = GENL_ADMIN_PERM,
3435 	},
3436 	{
3437 		.cmd = MACSEC_CMD_DEL_RXSA,
3438 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3439 		.doit = macsec_del_rxsa,
3440 		.flags = GENL_ADMIN_PERM,
3441 	},
3442 	{
3443 		.cmd = MACSEC_CMD_UPD_RXSA,
3444 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3445 		.doit = macsec_upd_rxsa,
3446 		.flags = GENL_ADMIN_PERM,
3447 	},
3448 	{
3449 		.cmd = MACSEC_CMD_UPD_OFFLOAD,
3450 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3451 		.doit = macsec_upd_offload,
3452 		.flags = GENL_ADMIN_PERM,
3453 	},
3454 };
3455 
3456 static struct genl_family macsec_fam __ro_after_init = {
3457 	.name		= MACSEC_GENL_NAME,
3458 	.hdrsize	= 0,
3459 	.version	= MACSEC_GENL_VERSION,
3460 	.maxattr	= MACSEC_ATTR_MAX,
3461 	.policy = macsec_genl_policy,
3462 	.netnsok	= true,
3463 	.module		= THIS_MODULE,
3464 	.small_ops	= macsec_genl_ops,
3465 	.n_small_ops	= ARRAY_SIZE(macsec_genl_ops),
3466 	.resv_start_op	= MACSEC_CMD_UPD_OFFLOAD + 1,
3467 };
3468 
macsec_insert_tx_tag(struct sk_buff * skb,struct net_device * dev)3469 static struct sk_buff *macsec_insert_tx_tag(struct sk_buff *skb,
3470 					    struct net_device *dev)
3471 {
3472 	struct macsec_dev *macsec = macsec_priv(dev);
3473 	const struct macsec_ops *ops;
3474 	struct phy_device *phydev;
3475 	struct macsec_context ctx;
3476 	int skb_final_len;
3477 	int err;
3478 
3479 	ops = macsec_get_ops(macsec, &ctx);
3480 	skb_final_len = skb->len - ETH_HLEN + ops->needed_headroom +
3481 		ops->needed_tailroom;
3482 	if (unlikely(skb_final_len > macsec->real_dev->mtu)) {
3483 		err = -EINVAL;
3484 		goto cleanup;
3485 	}
3486 
3487 	phydev = macsec->real_dev->phydev;
3488 
3489 	err = skb_ensure_writable_head_tail(skb, dev);
3490 	if (unlikely(err < 0))
3491 		goto cleanup;
3492 
3493 	err = ops->mdo_insert_tx_tag(phydev, skb);
3494 	if (unlikely(err))
3495 		goto cleanup;
3496 
3497 	return skb;
3498 cleanup:
3499 	kfree_skb(skb);
3500 	return ERR_PTR(err);
3501 }
3502 
macsec_start_xmit(struct sk_buff * skb,struct net_device * dev)3503 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
3504 				     struct net_device *dev)
3505 {
3506 	struct macsec_dev *macsec = netdev_priv(dev);
3507 	struct macsec_secy *secy = &macsec->secy;
3508 	struct pcpu_secy_stats *secy_stats;
3509 	int ret, len;
3510 
3511 	if (macsec_is_offloaded(netdev_priv(dev))) {
3512 		struct metadata_dst *md_dst = secy->tx_sc.md_dst;
3513 
3514 		skb_dst_drop(skb);
3515 		dst_hold(&md_dst->dst);
3516 		skb_dst_set(skb, &md_dst->dst);
3517 
3518 		if (macsec->insert_tx_tag) {
3519 			skb = macsec_insert_tx_tag(skb, dev);
3520 			if (IS_ERR(skb)) {
3521 				DEV_STATS_INC(dev, tx_dropped);
3522 				return NETDEV_TX_OK;
3523 			}
3524 		}
3525 
3526 		skb->dev = macsec->real_dev;
3527 		return dev_queue_xmit(skb);
3528 	}
3529 
3530 	/* 10.5 */
3531 	if (!secy->protect_frames) {
3532 		secy_stats = this_cpu_ptr(macsec->stats);
3533 		u64_stats_update_begin(&secy_stats->syncp);
3534 		secy_stats->stats.OutPktsUntagged++;
3535 		u64_stats_update_end(&secy_stats->syncp);
3536 		skb->dev = macsec->real_dev;
3537 		len = skb->len;
3538 		ret = dev_queue_xmit(skb);
3539 		count_tx(dev, ret, len);
3540 		return ret;
3541 	}
3542 
3543 	if (!secy->operational) {
3544 		kfree_skb(skb);
3545 		DEV_STATS_INC(dev, tx_dropped);
3546 		return NETDEV_TX_OK;
3547 	}
3548 
3549 	len = skb->len;
3550 	skb = macsec_encrypt(skb, dev);
3551 	if (IS_ERR(skb)) {
3552 		if (PTR_ERR(skb) != -EINPROGRESS)
3553 			DEV_STATS_INC(dev, tx_dropped);
3554 		return NETDEV_TX_OK;
3555 	}
3556 
3557 	macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
3558 
3559 	macsec_encrypt_finish(skb, dev);
3560 	ret = dev_queue_xmit(skb);
3561 	count_tx(dev, ret, len);
3562 	return ret;
3563 }
3564 
3565 #define MACSEC_FEATURES \
3566 	(NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
3567 
3568 #define MACSEC_OFFLOAD_FEATURES \
3569 	(MACSEC_FEATURES | NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES | \
3570 	 NETIF_F_LRO | NETIF_F_RXHASH | NETIF_F_CSUM_MASK | NETIF_F_RXCSUM)
3571 
macsec_dev_init(struct net_device * dev)3572 static int macsec_dev_init(struct net_device *dev)
3573 {
3574 	struct macsec_dev *macsec = macsec_priv(dev);
3575 	struct net_device *real_dev = macsec->real_dev;
3576 	int err;
3577 
3578 	err = gro_cells_init(&macsec->gro_cells, dev);
3579 	if (err)
3580 		return err;
3581 
3582 	macsec_inherit_tso_max(dev);
3583 
3584 	dev->hw_features = real_dev->hw_features & MACSEC_OFFLOAD_FEATURES;
3585 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3586 
3587 	dev->features = real_dev->features & MACSEC_OFFLOAD_FEATURES;
3588 	dev->features |= NETIF_F_GSO_SOFTWARE;
3589 	dev->lltx = true;
3590 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
3591 
3592 	macsec_set_head_tail_room(dev);
3593 
3594 	if (is_zero_ether_addr(dev->dev_addr))
3595 		eth_hw_addr_inherit(dev, real_dev);
3596 	if (is_zero_ether_addr(dev->broadcast))
3597 		memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
3598 
3599 	/* Get macsec's reference to real_dev */
3600 	netdev_hold(real_dev, &macsec->dev_tracker, GFP_KERNEL);
3601 
3602 	return 0;
3603 }
3604 
macsec_dev_uninit(struct net_device * dev)3605 static void macsec_dev_uninit(struct net_device *dev)
3606 {
3607 	struct macsec_dev *macsec = macsec_priv(dev);
3608 
3609 	gro_cells_destroy(&macsec->gro_cells);
3610 }
3611 
macsec_fix_features(struct net_device * dev,netdev_features_t features)3612 static netdev_features_t macsec_fix_features(struct net_device *dev,
3613 					     netdev_features_t features)
3614 {
3615 	struct macsec_dev *macsec = macsec_priv(dev);
3616 	struct net_device *real_dev = macsec->real_dev;
3617 	netdev_features_t mask;
3618 
3619 	mask = macsec_is_offloaded(macsec) ? MACSEC_OFFLOAD_FEATURES
3620 					   : MACSEC_FEATURES;
3621 
3622 	features &= (real_dev->features & mask) |
3623 		    NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
3624 
3625 	return features;
3626 }
3627 
macsec_dev_open(struct net_device * dev)3628 static int macsec_dev_open(struct net_device *dev)
3629 {
3630 	struct macsec_dev *macsec = macsec_priv(dev);
3631 	struct net_device *real_dev = macsec->real_dev;
3632 	int err;
3633 
3634 	err = dev_uc_add(real_dev, dev->dev_addr);
3635 	if (err < 0)
3636 		return err;
3637 
3638 	if (dev->flags & IFF_ALLMULTI) {
3639 		err = dev_set_allmulti(real_dev, 1);
3640 		if (err < 0)
3641 			goto del_unicast;
3642 	}
3643 
3644 	if (dev->flags & IFF_PROMISC) {
3645 		err = dev_set_promiscuity(real_dev, 1);
3646 		if (err < 0)
3647 			goto clear_allmulti;
3648 	}
3649 
3650 	/* If h/w offloading is available, propagate to the device */
3651 	if (macsec_is_offloaded(macsec)) {
3652 		const struct macsec_ops *ops;
3653 		struct macsec_context ctx;
3654 
3655 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
3656 		if (!ops) {
3657 			err = -EOPNOTSUPP;
3658 			goto clear_allmulti;
3659 		}
3660 
3661 		ctx.secy = &macsec->secy;
3662 		err = macsec_offload(ops->mdo_dev_open, &ctx);
3663 		if (err)
3664 			goto clear_allmulti;
3665 	}
3666 
3667 	if (netif_carrier_ok(real_dev))
3668 		netif_carrier_on(dev);
3669 
3670 	return 0;
3671 clear_allmulti:
3672 	if (dev->flags & IFF_ALLMULTI)
3673 		dev_set_allmulti(real_dev, -1);
3674 del_unicast:
3675 	dev_uc_del(real_dev, dev->dev_addr);
3676 	netif_carrier_off(dev);
3677 	return err;
3678 }
3679 
macsec_dev_stop(struct net_device * dev)3680 static int macsec_dev_stop(struct net_device *dev)
3681 {
3682 	struct macsec_dev *macsec = macsec_priv(dev);
3683 	struct net_device *real_dev = macsec->real_dev;
3684 
3685 	netif_carrier_off(dev);
3686 
3687 	/* If h/w offloading is available, propagate to the device */
3688 	if (macsec_is_offloaded(macsec)) {
3689 		const struct macsec_ops *ops;
3690 		struct macsec_context ctx;
3691 
3692 		ops = macsec_get_ops(macsec, &ctx);
3693 		if (ops) {
3694 			ctx.secy = &macsec->secy;
3695 			macsec_offload(ops->mdo_dev_stop, &ctx);
3696 		}
3697 	}
3698 
3699 	dev_mc_unsync(real_dev, dev);
3700 	dev_uc_unsync(real_dev, dev);
3701 
3702 	if (dev->flags & IFF_ALLMULTI)
3703 		dev_set_allmulti(real_dev, -1);
3704 
3705 	if (dev->flags & IFF_PROMISC)
3706 		dev_set_promiscuity(real_dev, -1);
3707 
3708 	dev_uc_del(real_dev, dev->dev_addr);
3709 
3710 	return 0;
3711 }
3712 
macsec_dev_change_rx_flags(struct net_device * dev,int change)3713 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
3714 {
3715 	struct net_device *real_dev = macsec_priv(dev)->real_dev;
3716 
3717 	if (!(dev->flags & IFF_UP))
3718 		return;
3719 
3720 	if (change & IFF_ALLMULTI)
3721 		dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
3722 
3723 	if (change & IFF_PROMISC)
3724 		dev_set_promiscuity(real_dev,
3725 				    dev->flags & IFF_PROMISC ? 1 : -1);
3726 }
3727 
macsec_dev_set_rx_mode(struct net_device * dev)3728 static void macsec_dev_set_rx_mode(struct net_device *dev)
3729 {
3730 	struct net_device *real_dev = macsec_priv(dev)->real_dev;
3731 
3732 	dev_mc_sync(real_dev, dev);
3733 	dev_uc_sync(real_dev, dev);
3734 }
3735 
macsec_set_mac_address(struct net_device * dev,void * p)3736 static int macsec_set_mac_address(struct net_device *dev, void *p)
3737 {
3738 	struct macsec_dev *macsec = macsec_priv(dev);
3739 	struct net_device *real_dev = macsec->real_dev;
3740 	struct sockaddr *addr = p;
3741 	u8  old_addr[ETH_ALEN];
3742 	int err;
3743 
3744 	if (!is_valid_ether_addr(addr->sa_data))
3745 		return -EADDRNOTAVAIL;
3746 
3747 	if (dev->flags & IFF_UP) {
3748 		err = dev_uc_add(real_dev, addr->sa_data);
3749 		if (err < 0)
3750 			return err;
3751 	}
3752 
3753 	ether_addr_copy(old_addr, dev->dev_addr);
3754 	eth_hw_addr_set(dev, addr->sa_data);
3755 
3756 	/* If h/w offloading is available, propagate to the device */
3757 	if (macsec_is_offloaded(macsec)) {
3758 		const struct macsec_ops *ops;
3759 		struct macsec_context ctx;
3760 
3761 		ops = macsec_get_ops(macsec, &ctx);
3762 		if (!ops) {
3763 			err = -EOPNOTSUPP;
3764 			goto restore_old_addr;
3765 		}
3766 
3767 		ctx.secy = &macsec->secy;
3768 		err = macsec_offload(ops->mdo_upd_secy, &ctx);
3769 		if (err)
3770 			goto restore_old_addr;
3771 	}
3772 
3773 	if (dev->flags & IFF_UP)
3774 		dev_uc_del(real_dev, old_addr);
3775 
3776 	return 0;
3777 
3778 restore_old_addr:
3779 	if (dev->flags & IFF_UP)
3780 		dev_uc_del(real_dev, addr->sa_data);
3781 
3782 	eth_hw_addr_set(dev, old_addr);
3783 
3784 	return err;
3785 }
3786 
macsec_change_mtu(struct net_device * dev,int new_mtu)3787 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
3788 {
3789 	struct macsec_dev *macsec = macsec_priv(dev);
3790 	unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
3791 
3792 	if (macsec->real_dev->mtu - extra < new_mtu)
3793 		return -ERANGE;
3794 
3795 	WRITE_ONCE(dev->mtu, new_mtu);
3796 
3797 	return 0;
3798 }
3799 
macsec_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * s)3800 static void macsec_get_stats64(struct net_device *dev,
3801 			       struct rtnl_link_stats64 *s)
3802 {
3803 	if (!dev->tstats)
3804 		return;
3805 
3806 	dev_fetch_sw_netstats(s, dev->tstats);
3807 
3808 	s->rx_dropped = DEV_STATS_READ(dev, rx_dropped);
3809 	s->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
3810 	s->rx_errors = DEV_STATS_READ(dev, rx_errors);
3811 }
3812 
macsec_get_iflink(const struct net_device * dev)3813 static int macsec_get_iflink(const struct net_device *dev)
3814 {
3815 	return READ_ONCE(macsec_priv(dev)->real_dev->ifindex);
3816 }
3817 
3818 static const struct net_device_ops macsec_netdev_ops = {
3819 	.ndo_init		= macsec_dev_init,
3820 	.ndo_uninit		= macsec_dev_uninit,
3821 	.ndo_open		= macsec_dev_open,
3822 	.ndo_stop		= macsec_dev_stop,
3823 	.ndo_fix_features	= macsec_fix_features,
3824 	.ndo_change_mtu		= macsec_change_mtu,
3825 	.ndo_set_rx_mode	= macsec_dev_set_rx_mode,
3826 	.ndo_change_rx_flags	= macsec_dev_change_rx_flags,
3827 	.ndo_set_mac_address	= macsec_set_mac_address,
3828 	.ndo_start_xmit		= macsec_start_xmit,
3829 	.ndo_get_stats64	= macsec_get_stats64,
3830 	.ndo_get_iflink		= macsec_get_iflink,
3831 };
3832 
3833 static const struct device_type macsec_type = {
3834 	.name = "macsec",
3835 };
3836 
3837 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3838 	[IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3839 	[IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3840 	[IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3841 	[IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3842 	[IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3843 	[IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3844 	[IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3845 	[IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3846 	[IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3847 	[IFLA_MACSEC_ES] = { .type = NLA_U8 },
3848 	[IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3849 	[IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3850 	[IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3851 	[IFLA_MACSEC_OFFLOAD] = { .type = NLA_U8 },
3852 };
3853 
macsec_free_netdev(struct net_device * dev)3854 static void macsec_free_netdev(struct net_device *dev)
3855 {
3856 	struct macsec_dev *macsec = macsec_priv(dev);
3857 
3858 	dst_release(&macsec->secy.tx_sc.md_dst->dst);
3859 	free_percpu(macsec->stats);
3860 	free_percpu(macsec->secy.tx_sc.stats);
3861 
3862 	/* Get rid of the macsec's reference to real_dev */
3863 	netdev_put(macsec->real_dev, &macsec->dev_tracker);
3864 }
3865 
macsec_setup(struct net_device * dev)3866 static void macsec_setup(struct net_device *dev)
3867 {
3868 	ether_setup(dev);
3869 	dev->min_mtu = 0;
3870 	dev->max_mtu = ETH_MAX_MTU;
3871 	dev->priv_flags |= IFF_NO_QUEUE;
3872 	dev->netdev_ops = &macsec_netdev_ops;
3873 	dev->needs_free_netdev = true;
3874 	dev->priv_destructor = macsec_free_netdev;
3875 	SET_NETDEV_DEVTYPE(dev, &macsec_type);
3876 
3877 	eth_zero_addr(dev->broadcast);
3878 }
3879 
macsec_changelink_common(struct net_device * dev,struct nlattr * data[])3880 static int macsec_changelink_common(struct net_device *dev,
3881 				    struct nlattr *data[])
3882 {
3883 	struct macsec_secy *secy;
3884 	struct macsec_tx_sc *tx_sc;
3885 
3886 	secy = &macsec_priv(dev)->secy;
3887 	tx_sc = &secy->tx_sc;
3888 
3889 	if (data[IFLA_MACSEC_ENCODING_SA]) {
3890 		struct macsec_tx_sa *tx_sa;
3891 
3892 		tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3893 		tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3894 
3895 		secy->operational = tx_sa && tx_sa->active;
3896 	}
3897 
3898 	if (data[IFLA_MACSEC_ENCRYPT])
3899 		tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3900 
3901 	if (data[IFLA_MACSEC_PROTECT])
3902 		secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3903 
3904 	if (data[IFLA_MACSEC_INC_SCI])
3905 		tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3906 
3907 	if (data[IFLA_MACSEC_ES])
3908 		tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3909 
3910 	if (data[IFLA_MACSEC_SCB])
3911 		tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3912 
3913 	if (data[IFLA_MACSEC_REPLAY_PROTECT])
3914 		secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3915 
3916 	if (data[IFLA_MACSEC_VALIDATION])
3917 		secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3918 
3919 	if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3920 		switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3921 		case MACSEC_CIPHER_ID_GCM_AES_128:
3922 		case MACSEC_DEFAULT_CIPHER_ID:
3923 			secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3924 			secy->xpn = false;
3925 			break;
3926 		case MACSEC_CIPHER_ID_GCM_AES_256:
3927 			secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3928 			secy->xpn = false;
3929 			break;
3930 		case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3931 			secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3932 			secy->xpn = true;
3933 			break;
3934 		case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3935 			secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3936 			secy->xpn = true;
3937 			break;
3938 		default:
3939 			return -EINVAL;
3940 		}
3941 	}
3942 
3943 	if (data[IFLA_MACSEC_WINDOW]) {
3944 		secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3945 
3946 		/* IEEE 802.1AEbw-2013 10.7.8 - maximum replay window
3947 		 * for XPN cipher suites */
3948 		if (secy->xpn &&
3949 		    secy->replay_window > MACSEC_XPN_MAX_REPLAY_WINDOW)
3950 			return -EINVAL;
3951 	}
3952 
3953 	return 0;
3954 }
3955 
macsec_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)3956 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3957 			     struct nlattr *data[],
3958 			     struct netlink_ext_ack *extack)
3959 {
3960 	struct macsec_dev *macsec = macsec_priv(dev);
3961 	bool macsec_offload_state_change = false;
3962 	enum macsec_offload offload;
3963 	struct macsec_tx_sc tx_sc;
3964 	struct macsec_secy secy;
3965 	int ret;
3966 
3967 	if (!data)
3968 		return 0;
3969 
3970 	if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3971 	    data[IFLA_MACSEC_ICV_LEN] ||
3972 	    data[IFLA_MACSEC_SCI] ||
3973 	    data[IFLA_MACSEC_PORT])
3974 		return -EINVAL;
3975 
3976 	/* Keep a copy of unmodified secy and tx_sc, in case the offload
3977 	 * propagation fails, to revert macsec_changelink_common.
3978 	 */
3979 	memcpy(&secy, &macsec->secy, sizeof(secy));
3980 	memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
3981 
3982 	ret = macsec_changelink_common(dev, data);
3983 	if (ret)
3984 		goto cleanup;
3985 
3986 	if (data[IFLA_MACSEC_OFFLOAD]) {
3987 		offload = nla_get_u8(data[IFLA_MACSEC_OFFLOAD]);
3988 		if (macsec->offload != offload) {
3989 			macsec_offload_state_change = true;
3990 			ret = macsec_update_offload(dev, offload);
3991 			if (ret)
3992 				goto cleanup;
3993 		}
3994 	}
3995 
3996 	/* If h/w offloading is available, propagate to the device */
3997 	if (!macsec_offload_state_change && macsec_is_offloaded(macsec)) {
3998 		const struct macsec_ops *ops;
3999 		struct macsec_context ctx;
4000 
4001 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
4002 		if (!ops) {
4003 			ret = -EOPNOTSUPP;
4004 			goto cleanup;
4005 		}
4006 
4007 		ctx.secy = &macsec->secy;
4008 		ret = macsec_offload(ops->mdo_upd_secy, &ctx);
4009 		if (ret)
4010 			goto cleanup;
4011 	}
4012 
4013 	return 0;
4014 
4015 cleanup:
4016 	memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
4017 	memcpy(&macsec->secy, &secy, sizeof(secy));
4018 
4019 	return ret;
4020 }
4021 
macsec_del_dev(struct macsec_dev * macsec)4022 static void macsec_del_dev(struct macsec_dev *macsec)
4023 {
4024 	int i;
4025 
4026 	while (macsec->secy.rx_sc) {
4027 		struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
4028 
4029 		rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
4030 		free_rx_sc(rx_sc);
4031 	}
4032 
4033 	for (i = 0; i < MACSEC_NUM_AN; i++) {
4034 		struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
4035 
4036 		if (sa) {
4037 			RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
4038 			clear_tx_sa(sa);
4039 		}
4040 	}
4041 }
4042 
macsec_common_dellink(struct net_device * dev,struct list_head * head)4043 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
4044 {
4045 	struct macsec_dev *macsec = macsec_priv(dev);
4046 	struct net_device *real_dev = macsec->real_dev;
4047 
4048 	/* If h/w offloading is available, propagate to the device */
4049 	if (macsec_is_offloaded(macsec)) {
4050 		const struct macsec_ops *ops;
4051 		struct macsec_context ctx;
4052 
4053 		ops = macsec_get_ops(netdev_priv(dev), &ctx);
4054 		if (ops) {
4055 			ctx.secy = &macsec->secy;
4056 			macsec_offload(ops->mdo_del_secy, &ctx);
4057 		}
4058 	}
4059 
4060 	unregister_netdevice_queue(dev, head);
4061 	list_del_rcu(&macsec->secys);
4062 	macsec_del_dev(macsec);
4063 	netdev_upper_dev_unlink(real_dev, dev);
4064 
4065 	macsec_generation++;
4066 }
4067 
macsec_dellink(struct net_device * dev,struct list_head * head)4068 static void macsec_dellink(struct net_device *dev, struct list_head *head)
4069 {
4070 	struct macsec_dev *macsec = macsec_priv(dev);
4071 	struct net_device *real_dev = macsec->real_dev;
4072 	struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
4073 
4074 	macsec_common_dellink(dev, head);
4075 
4076 	if (list_empty(&rxd->secys)) {
4077 		netdev_rx_handler_unregister(real_dev);
4078 		kfree(rxd);
4079 	}
4080 }
4081 
register_macsec_dev(struct net_device * real_dev,struct net_device * dev)4082 static int register_macsec_dev(struct net_device *real_dev,
4083 			       struct net_device *dev)
4084 {
4085 	struct macsec_dev *macsec = macsec_priv(dev);
4086 	struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
4087 
4088 	if (!rxd) {
4089 		int err;
4090 
4091 		rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
4092 		if (!rxd)
4093 			return -ENOMEM;
4094 
4095 		INIT_LIST_HEAD(&rxd->secys);
4096 
4097 		err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
4098 						 rxd);
4099 		if (err < 0) {
4100 			kfree(rxd);
4101 			return err;
4102 		}
4103 	}
4104 
4105 	list_add_tail_rcu(&macsec->secys, &rxd->secys);
4106 	return 0;
4107 }
4108 
sci_exists(struct net_device * dev,sci_t sci)4109 static bool sci_exists(struct net_device *dev, sci_t sci)
4110 {
4111 	struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
4112 	struct macsec_dev *macsec;
4113 
4114 	list_for_each_entry(macsec, &rxd->secys, secys) {
4115 		if (macsec->secy.sci == sci)
4116 			return true;
4117 	}
4118 
4119 	return false;
4120 }
4121 
dev_to_sci(struct net_device * dev,__be16 port)4122 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
4123 {
4124 	return make_sci(dev->dev_addr, port);
4125 }
4126 
macsec_add_dev(struct net_device * dev,sci_t sci,u8 icv_len)4127 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
4128 {
4129 	struct macsec_dev *macsec = macsec_priv(dev);
4130 	struct macsec_secy *secy = &macsec->secy;
4131 
4132 	macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
4133 	if (!macsec->stats)
4134 		return -ENOMEM;
4135 
4136 	secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
4137 	if (!secy->tx_sc.stats)
4138 		return -ENOMEM;
4139 
4140 	secy->tx_sc.md_dst = metadata_dst_alloc(0, METADATA_MACSEC, GFP_KERNEL);
4141 	if (!secy->tx_sc.md_dst)
4142 		/* macsec and secy percpu stats will be freed when unregistering
4143 		 * net_device in macsec_free_netdev()
4144 		 */
4145 		return -ENOMEM;
4146 
4147 	if (sci == MACSEC_UNDEF_SCI)
4148 		sci = dev_to_sci(dev, MACSEC_PORT_ES);
4149 
4150 	secy->netdev = dev;
4151 	secy->operational = true;
4152 	secy->key_len = DEFAULT_SAK_LEN;
4153 	secy->icv_len = icv_len;
4154 	secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
4155 	secy->protect_frames = true;
4156 	secy->replay_protect = false;
4157 	secy->xpn = DEFAULT_XPN;
4158 
4159 	secy->sci = sci;
4160 	secy->tx_sc.md_dst->u.macsec_info.sci = sci;
4161 	secy->tx_sc.active = true;
4162 	secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
4163 	secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
4164 	secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
4165 	secy->tx_sc.end_station = false;
4166 	secy->tx_sc.scb = false;
4167 
4168 	return 0;
4169 }
4170 
4171 static struct lock_class_key macsec_netdev_addr_lock_key;
4172 
macsec_newlink(struct net_device * dev,struct rtnl_newlink_params * params,struct netlink_ext_ack * extack)4173 static int macsec_newlink(struct net_device *dev,
4174 			  struct rtnl_newlink_params *params,
4175 			  struct netlink_ext_ack *extack)
4176 {
4177 	struct net *link_net = rtnl_newlink_link_net(params);
4178 	struct macsec_dev *macsec = macsec_priv(dev);
4179 	struct nlattr **data = params->data;
4180 	struct nlattr **tb = params->tb;
4181 	rx_handler_func_t *rx_handler;
4182 	u8 icv_len = MACSEC_DEFAULT_ICV_LEN;
4183 	struct net_device *real_dev;
4184 	int err, mtu;
4185 	sci_t sci;
4186 
4187 	if (!tb[IFLA_LINK])
4188 		return -EINVAL;
4189 	real_dev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK]));
4190 	if (!real_dev)
4191 		return -ENODEV;
4192 	if (real_dev->type != ARPHRD_ETHER)
4193 		return -EINVAL;
4194 
4195 	dev->priv_flags |= IFF_MACSEC;
4196 
4197 	macsec->real_dev = real_dev;
4198 
4199 	if (data && data[IFLA_MACSEC_OFFLOAD])
4200 		macsec->offload = nla_get_offload(data[IFLA_MACSEC_OFFLOAD]);
4201 	else
4202 		/* MACsec offloading is off by default */
4203 		macsec->offload = MACSEC_OFFLOAD_OFF;
4204 
4205 	/* Check if the offloading mode is supported by the underlying layers */
4206 	if (macsec->offload != MACSEC_OFFLOAD_OFF &&
4207 	    !macsec_check_offload(macsec->offload, macsec))
4208 		return -EOPNOTSUPP;
4209 
4210 	/* send_sci must be set to true when transmit sci explicitly is set */
4211 	if ((data && data[IFLA_MACSEC_SCI]) &&
4212 	    (data && data[IFLA_MACSEC_INC_SCI])) {
4213 		u8 send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
4214 
4215 		if (!send_sci)
4216 			return -EINVAL;
4217 	}
4218 
4219 	if (data && data[IFLA_MACSEC_ICV_LEN])
4220 		icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4221 	mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
4222 	if (mtu < 0)
4223 		dev->mtu = 0;
4224 	else
4225 		dev->mtu = mtu;
4226 
4227 	rx_handler = rtnl_dereference(real_dev->rx_handler);
4228 	if (rx_handler && rx_handler != macsec_handle_frame)
4229 		return -EBUSY;
4230 
4231 	err = register_netdevice(dev);
4232 	if (err < 0)
4233 		return err;
4234 
4235 	netdev_lockdep_set_classes(dev);
4236 	lockdep_set_class(&dev->addr_list_lock,
4237 			  &macsec_netdev_addr_lock_key);
4238 
4239 	err = netdev_upper_dev_link(real_dev, dev, extack);
4240 	if (err < 0)
4241 		goto unregister;
4242 
4243 	/* need to be already registered so that ->init has run and
4244 	 * the MAC addr is set
4245 	 */
4246 	if (data && data[IFLA_MACSEC_SCI])
4247 		sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
4248 	else if (data && data[IFLA_MACSEC_PORT])
4249 		sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
4250 	else
4251 		sci = dev_to_sci(dev, MACSEC_PORT_ES);
4252 
4253 	if (rx_handler && sci_exists(real_dev, sci)) {
4254 		err = -EBUSY;
4255 		goto unlink;
4256 	}
4257 
4258 	err = macsec_add_dev(dev, sci, icv_len);
4259 	if (err)
4260 		goto unlink;
4261 
4262 	if (data) {
4263 		err = macsec_changelink_common(dev, data);
4264 		if (err)
4265 			goto del_dev;
4266 	}
4267 
4268 	/* If h/w offloading is available, propagate to the device */
4269 	if (macsec_is_offloaded(macsec)) {
4270 		const struct macsec_ops *ops;
4271 		struct macsec_context ctx;
4272 
4273 		ops = macsec_get_ops(macsec, &ctx);
4274 		if (ops) {
4275 			ctx.secy = &macsec->secy;
4276 			err = macsec_offload(ops->mdo_add_secy, &ctx);
4277 			if (err)
4278 				goto del_dev;
4279 
4280 			macsec->insert_tx_tag =
4281 				macsec_needs_tx_tag(macsec, ops);
4282 		}
4283 	}
4284 
4285 	err = register_macsec_dev(real_dev, dev);
4286 	if (err < 0)
4287 		goto del_dev;
4288 
4289 	netif_stacked_transfer_operstate(real_dev, dev);
4290 	linkwatch_fire_event(dev);
4291 
4292 	macsec_generation++;
4293 
4294 	return 0;
4295 
4296 del_dev:
4297 	macsec_del_dev(macsec);
4298 unlink:
4299 	netdev_upper_dev_unlink(real_dev, dev);
4300 unregister:
4301 	unregister_netdevice(dev);
4302 	return err;
4303 }
4304 
macsec_validate_attr(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)4305 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
4306 				struct netlink_ext_ack *extack)
4307 {
4308 	u64 csid = MACSEC_DEFAULT_CIPHER_ID;
4309 	u8 icv_len = MACSEC_DEFAULT_ICV_LEN;
4310 	int flag;
4311 	bool es, scb, sci;
4312 
4313 	if (!data)
4314 		return 0;
4315 
4316 	if (data[IFLA_MACSEC_CIPHER_SUITE])
4317 		csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
4318 
4319 	if (data[IFLA_MACSEC_ICV_LEN]) {
4320 		icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4321 		if (icv_len != MACSEC_DEFAULT_ICV_LEN) {
4322 			char dummy_key[DEFAULT_SAK_LEN] = { 0 };
4323 			struct crypto_aead *dummy_tfm;
4324 
4325 			dummy_tfm = macsec_alloc_tfm(dummy_key,
4326 						     DEFAULT_SAK_LEN,
4327 						     icv_len);
4328 			if (IS_ERR(dummy_tfm))
4329 				return PTR_ERR(dummy_tfm);
4330 			crypto_free_aead(dummy_tfm);
4331 		}
4332 	}
4333 
4334 	switch (csid) {
4335 	case MACSEC_CIPHER_ID_GCM_AES_128:
4336 	case MACSEC_CIPHER_ID_GCM_AES_256:
4337 	case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
4338 	case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
4339 	case MACSEC_DEFAULT_CIPHER_ID:
4340 		if (icv_len < MACSEC_MIN_ICV_LEN ||
4341 		    icv_len > MACSEC_STD_ICV_LEN)
4342 			return -EINVAL;
4343 		break;
4344 	default:
4345 		return -EINVAL;
4346 	}
4347 
4348 	if (data[IFLA_MACSEC_ENCODING_SA]) {
4349 		if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
4350 			return -EINVAL;
4351 	}
4352 
4353 	for (flag = IFLA_MACSEC_ENCODING_SA + 1;
4354 	     flag < IFLA_MACSEC_VALIDATION;
4355 	     flag++) {
4356 		if (data[flag]) {
4357 			if (nla_get_u8(data[flag]) > 1)
4358 				return -EINVAL;
4359 		}
4360 	}
4361 
4362 	es  = nla_get_u8_default(data[IFLA_MACSEC_ES], false);
4363 	sci = nla_get_u8_default(data[IFLA_MACSEC_INC_SCI], false);
4364 	scb = nla_get_u8_default(data[IFLA_MACSEC_SCB], false);
4365 
4366 	if ((sci && (scb || es)) || (scb && es))
4367 		return -EINVAL;
4368 
4369 	if (data[IFLA_MACSEC_VALIDATION] &&
4370 	    nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
4371 		return -EINVAL;
4372 
4373 	if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
4374 	     nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
4375 	    !data[IFLA_MACSEC_WINDOW])
4376 		return -EINVAL;
4377 
4378 	return 0;
4379 }
4380 
macsec_get_link_net(const struct net_device * dev)4381 static struct net *macsec_get_link_net(const struct net_device *dev)
4382 {
4383 	return dev_net(macsec_priv(dev)->real_dev);
4384 }
4385 
macsec_get_real_dev(const struct net_device * dev)4386 struct net_device *macsec_get_real_dev(const struct net_device *dev)
4387 {
4388 	return macsec_priv(dev)->real_dev;
4389 }
4390 EXPORT_SYMBOL_GPL(macsec_get_real_dev);
4391 
macsec_netdev_is_offloaded(struct net_device * dev)4392 bool macsec_netdev_is_offloaded(struct net_device *dev)
4393 {
4394 	return macsec_is_offloaded(macsec_priv(dev));
4395 }
4396 EXPORT_SYMBOL_GPL(macsec_netdev_is_offloaded);
4397 
macsec_get_size(const struct net_device * dev)4398 static size_t macsec_get_size(const struct net_device *dev)
4399 {
4400 	return  nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
4401 		nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
4402 		nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
4403 		nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
4404 		nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
4405 		nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
4406 		nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
4407 		nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
4408 		nla_total_size(1) + /* IFLA_MACSEC_ES */
4409 		nla_total_size(1) + /* IFLA_MACSEC_SCB */
4410 		nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
4411 		nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
4412 		nla_total_size(1) + /* IFLA_MACSEC_OFFLOAD */
4413 		0;
4414 }
4415 
macsec_fill_info(struct sk_buff * skb,const struct net_device * dev)4416 static int macsec_fill_info(struct sk_buff *skb,
4417 			    const struct net_device *dev)
4418 {
4419 	struct macsec_tx_sc *tx_sc;
4420 	struct macsec_dev *macsec;
4421 	struct macsec_secy *secy;
4422 	u64 csid;
4423 
4424 	macsec = macsec_priv(dev);
4425 	secy = &macsec->secy;
4426 	tx_sc = &secy->tx_sc;
4427 
4428 	switch (secy->key_len) {
4429 	case MACSEC_GCM_AES_128_SAK_LEN:
4430 		csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
4431 		break;
4432 	case MACSEC_GCM_AES_256_SAK_LEN:
4433 		csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
4434 		break;
4435 	default:
4436 		goto nla_put_failure;
4437 	}
4438 
4439 	if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
4440 			IFLA_MACSEC_PAD) ||
4441 	    nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
4442 	    nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
4443 			      csid, IFLA_MACSEC_PAD) ||
4444 	    nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
4445 	    nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
4446 	    nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
4447 	    nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
4448 	    nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
4449 	    nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
4450 	    nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
4451 	    nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
4452 	    nla_put_u8(skb, IFLA_MACSEC_OFFLOAD, macsec->offload) ||
4453 	    0)
4454 		goto nla_put_failure;
4455 
4456 	if (secy->replay_protect) {
4457 		if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
4458 			goto nla_put_failure;
4459 	}
4460 
4461 	return 0;
4462 
4463 nla_put_failure:
4464 	return -EMSGSIZE;
4465 }
4466 
4467 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
4468 	.kind		= "macsec",
4469 	.priv_size	= sizeof(struct macsec_dev),
4470 	.maxtype	= IFLA_MACSEC_MAX,
4471 	.policy		= macsec_rtnl_policy,
4472 	.setup		= macsec_setup,
4473 	.validate	= macsec_validate_attr,
4474 	.newlink	= macsec_newlink,
4475 	.changelink	= macsec_changelink,
4476 	.dellink	= macsec_dellink,
4477 	.get_size	= macsec_get_size,
4478 	.fill_info	= macsec_fill_info,
4479 	.get_link_net	= macsec_get_link_net,
4480 };
4481 
is_macsec_master(struct net_device * dev)4482 static bool is_macsec_master(struct net_device *dev)
4483 {
4484 	return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
4485 }
4486 
macsec_notify(struct notifier_block * this,unsigned long event,void * ptr)4487 static int macsec_notify(struct notifier_block *this, unsigned long event,
4488 			 void *ptr)
4489 {
4490 	struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
4491 	struct macsec_rxh_data *rxd;
4492 	struct macsec_dev *m, *n;
4493 	LIST_HEAD(head);
4494 
4495 	if (!is_macsec_master(real_dev))
4496 		return NOTIFY_DONE;
4497 
4498 	rxd = macsec_data_rtnl(real_dev);
4499 
4500 	switch (event) {
4501 	case NETDEV_DOWN:
4502 	case NETDEV_UP:
4503 	case NETDEV_CHANGE:
4504 		list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4505 			struct net_device *dev = m->secy.netdev;
4506 
4507 			netif_stacked_transfer_operstate(real_dev, dev);
4508 		}
4509 		break;
4510 	case NETDEV_UNREGISTER:
4511 		list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4512 			macsec_common_dellink(m->secy.netdev, &head);
4513 		}
4514 
4515 		netdev_rx_handler_unregister(real_dev);
4516 		kfree(rxd);
4517 
4518 		unregister_netdevice_many(&head);
4519 		break;
4520 	case NETDEV_CHANGEMTU:
4521 		list_for_each_entry(m, &rxd->secys, secys) {
4522 			struct net_device *dev = m->secy.netdev;
4523 			unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
4524 							    macsec_extra_len(true));
4525 
4526 			if (dev->mtu > mtu)
4527 				dev_set_mtu(dev, mtu);
4528 		}
4529 		break;
4530 	case NETDEV_FEAT_CHANGE:
4531 		list_for_each_entry(m, &rxd->secys, secys) {
4532 			macsec_inherit_tso_max(m->secy.netdev);
4533 			netdev_update_features(m->secy.netdev);
4534 		}
4535 		break;
4536 	}
4537 
4538 	return NOTIFY_OK;
4539 }
4540 
4541 static struct notifier_block macsec_notifier = {
4542 	.notifier_call = macsec_notify,
4543 };
4544 
macsec_init(void)4545 static int __init macsec_init(void)
4546 {
4547 	int err;
4548 
4549 	pr_info("MACsec IEEE 802.1AE\n");
4550 	err = register_netdevice_notifier(&macsec_notifier);
4551 	if (err)
4552 		return err;
4553 
4554 	err = rtnl_link_register(&macsec_link_ops);
4555 	if (err)
4556 		goto notifier;
4557 
4558 	err = genl_register_family(&macsec_fam);
4559 	if (err)
4560 		goto rtnl;
4561 
4562 	return 0;
4563 
4564 rtnl:
4565 	rtnl_link_unregister(&macsec_link_ops);
4566 notifier:
4567 	unregister_netdevice_notifier(&macsec_notifier);
4568 	return err;
4569 }
4570 
macsec_exit(void)4571 static void __exit macsec_exit(void)
4572 {
4573 	genl_unregister_family(&macsec_fam);
4574 	rtnl_link_unregister(&macsec_link_ops);
4575 	unregister_netdevice_notifier(&macsec_notifier);
4576 	rcu_barrier();
4577 }
4578 
4579 module_init(macsec_init);
4580 module_exit(macsec_exit);
4581 
4582 MODULE_ALIAS_RTNL_LINK("macsec");
4583 MODULE_ALIAS_GENL_FAMILY("macsec");
4584 
4585 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
4586 MODULE_LICENSE("GPL v2");
4587