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