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