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