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