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