blob: f233a6933a9760fdb9c5428952711926a84f1ae2 [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>
14#include <linux/rtnetlink.h>
15#include <linux/refcount.h>
16#include <net/genetlink.h>
17#include <net/sock.h>
18#include <net/gro_cells.h>
Olivier Deprez0e641232021-09-23 10:07:05 +020019#include <linux/if_arp.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
21#include <uapi/linux/if_macsec.h>
22
23typedef u64 __bitwise sci_t;
24
25#define MACSEC_SCI_LEN 8
26
27/* SecTAG length = macsec_eth_header without the optional SCI */
28#define MACSEC_TAG_LEN 6
29
30struct macsec_eth_header {
31 struct ethhdr eth;
32 /* SecTAG */
33 u8 tci_an;
34#if defined(__LITTLE_ENDIAN_BITFIELD)
35 u8 short_length:6,
36 unused:2;
37#elif defined(__BIG_ENDIAN_BITFIELD)
38 u8 unused:2,
39 short_length:6;
40#else
41#error "Please fix <asm/byteorder.h>"
42#endif
43 __be32 packet_number;
44 u8 secure_channel_id[8]; /* optional */
45} __packed;
46
47#define MACSEC_TCI_VERSION 0x80
48#define MACSEC_TCI_ES 0x40 /* end station */
49#define MACSEC_TCI_SC 0x20 /* SCI present */
50#define MACSEC_TCI_SCB 0x10 /* epon */
51#define MACSEC_TCI_E 0x08 /* encryption */
52#define MACSEC_TCI_C 0x04 /* changed text */
53#define MACSEC_AN_MASK 0x03 /* association number */
54#define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
55
56/* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
57#define MIN_NON_SHORT_LEN 48
58
59#define GCM_AES_IV_LEN 12
60#define DEFAULT_ICV_LEN 16
61
62#define MACSEC_NUM_AN 4 /* 2 bits for the association number */
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
73struct gcm_iv {
74 union {
75 u8 secure_channel_id[8];
76 sci_t sci;
77 };
78 __be32 pn;
79};
80
81/**
82 * struct macsec_key - SA key
83 * @id: user-provided key identifier
84 * @tfm: crypto struct, key storage
85 */
86struct macsec_key {
87 u8 id[MACSEC_KEYID_LEN];
88 struct crypto_aead *tfm;
89};
90
91struct macsec_rx_sc_stats {
92 __u64 InOctetsValidated;
93 __u64 InOctetsDecrypted;
94 __u64 InPktsUnchecked;
95 __u64 InPktsDelayed;
96 __u64 InPktsOK;
97 __u64 InPktsInvalid;
98 __u64 InPktsLate;
99 __u64 InPktsNotValid;
100 __u64 InPktsNotUsingSA;
101 __u64 InPktsUnusedSA;
102};
103
104struct macsec_rx_sa_stats {
105 __u32 InPktsOK;
106 __u32 InPktsInvalid;
107 __u32 InPktsNotValid;
108 __u32 InPktsNotUsingSA;
109 __u32 InPktsUnusedSA;
110};
111
112struct macsec_tx_sa_stats {
113 __u32 OutPktsProtected;
114 __u32 OutPktsEncrypted;
115};
116
117struct macsec_tx_sc_stats {
118 __u64 OutPktsProtected;
119 __u64 OutPktsEncrypted;
120 __u64 OutOctetsProtected;
121 __u64 OutOctetsEncrypted;
122};
123
124struct macsec_dev_stats {
125 __u64 OutPktsUntagged;
126 __u64 InPktsUntagged;
127 __u64 OutPktsTooLong;
128 __u64 InPktsNoTag;
129 __u64 InPktsBadTag;
130 __u64 InPktsUnknownSCI;
131 __u64 InPktsNoSCI;
132 __u64 InPktsOverrun;
133};
134
135/**
136 * struct macsec_rx_sa - receive secure association
137 * @active:
138 * @next_pn: packet number expected for the next packet
139 * @lock: protects next_pn manipulations
140 * @key: key structure
141 * @stats: per-SA stats
142 */
143struct macsec_rx_sa {
144 struct macsec_key key;
145 spinlock_t lock;
146 u32 next_pn;
147 refcount_t refcnt;
148 bool active;
149 struct macsec_rx_sa_stats __percpu *stats;
150 struct macsec_rx_sc *sc;
151 struct rcu_head rcu;
152};
153
154struct pcpu_rx_sc_stats {
155 struct macsec_rx_sc_stats stats;
156 struct u64_stats_sync syncp;
157};
158
159/**
160 * struct macsec_rx_sc - receive secure channel
161 * @sci: secure channel identifier for this SC
162 * @active: channel is active
163 * @sa: array of secure associations
164 * @stats: per-SC stats
165 */
166struct macsec_rx_sc {
167 struct macsec_rx_sc __rcu *next;
168 sci_t sci;
169 bool active;
170 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
171 struct pcpu_rx_sc_stats __percpu *stats;
172 refcount_t refcnt;
173 struct rcu_head rcu_head;
174};
175
176/**
177 * struct macsec_tx_sa - transmit secure association
178 * @active:
179 * @next_pn: packet number to use for the next packet
180 * @lock: protects next_pn manipulations
181 * @key: key structure
182 * @stats: per-SA stats
183 */
184struct macsec_tx_sa {
185 struct macsec_key key;
186 spinlock_t lock;
187 u32 next_pn;
188 refcount_t refcnt;
189 bool active;
190 struct macsec_tx_sa_stats __percpu *stats;
191 struct rcu_head rcu;
192};
193
194struct pcpu_tx_sc_stats {
195 struct macsec_tx_sc_stats stats;
196 struct u64_stats_sync syncp;
197};
198
199/**
200 * struct macsec_tx_sc - transmit secure channel
201 * @active:
202 * @encoding_sa: association number of the SA currently in use
203 * @encrypt: encrypt packets on transmit, or authenticate only
204 * @send_sci: always include the SCI in the SecTAG
205 * @end_station:
206 * @scb: single copy broadcast flag
207 * @sa: array of secure associations
208 * @stats: stats for this TXSC
209 */
210struct macsec_tx_sc {
211 bool active;
212 u8 encoding_sa;
213 bool encrypt;
214 bool send_sci;
215 bool end_station;
216 bool scb;
217 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
218 struct pcpu_tx_sc_stats __percpu *stats;
219};
220
221#define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
222
223/**
224 * struct macsec_secy - MACsec Security Entity
225 * @netdev: netdevice for this SecY
226 * @n_rx_sc: number of receive secure channels configured on this SecY
227 * @sci: secure channel identifier used for tx
228 * @key_len: length of keys used by the cipher suite
229 * @icv_len: length of ICV used by the cipher suite
230 * @validate_frames: validation mode
231 * @operational: MAC_Operational flag
232 * @protect_frames: enable protection for this SecY
233 * @replay_protect: enable packet number checks on receive
234 * @replay_window: size of the replay window
235 * @tx_sc: transmit secure channel
236 * @rx_sc: linked list of receive secure channels
237 */
238struct macsec_secy {
239 struct net_device *netdev;
240 unsigned int n_rx_sc;
241 sci_t sci;
242 u16 key_len;
243 u16 icv_len;
244 enum macsec_validation_type validate_frames;
245 bool operational;
246 bool protect_frames;
247 bool replay_protect;
248 u32 replay_window;
249 struct macsec_tx_sc tx_sc;
250 struct macsec_rx_sc __rcu *rx_sc;
251};
252
253struct pcpu_secy_stats {
254 struct macsec_dev_stats stats;
255 struct u64_stats_sync syncp;
256};
257
258/**
259 * struct macsec_dev - private data
260 * @secy: SecY config
261 * @real_dev: pointer to underlying netdevice
262 * @stats: MACsec device stats
263 * @secys: linked list of SecY's on the underlying device
264 */
265struct macsec_dev {
266 struct macsec_secy secy;
267 struct net_device *real_dev;
268 struct pcpu_secy_stats __percpu *stats;
269 struct list_head secys;
270 struct gro_cells gro_cells;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271};
272
273/**
274 * struct macsec_rxh_data - rx_handler private argument
275 * @secys: linked list of SecY's on this underlying device
276 */
277struct macsec_rxh_data {
278 struct list_head secys;
279};
280
281static struct macsec_dev *macsec_priv(const struct net_device *dev)
282{
283 return (struct macsec_dev *)netdev_priv(dev);
284}
285
286static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
287{
288 return rcu_dereference_bh(dev->rx_handler_data);
289}
290
291static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
292{
293 return rtnl_dereference(dev->rx_handler_data);
294}
295
296struct macsec_cb {
297 struct aead_request *req;
298 union {
299 struct macsec_tx_sa *tx_sa;
300 struct macsec_rx_sa *rx_sa;
301 };
302 u8 assoc_num;
303 bool valid;
304 bool has_sci;
305};
306
307static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
308{
309 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
310
311 if (!sa || !sa->active)
312 return NULL;
313
314 if (!refcount_inc_not_zero(&sa->refcnt))
315 return NULL;
316
317 return sa;
318}
319
320static void free_rx_sc_rcu(struct rcu_head *head)
321{
322 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
323
324 free_percpu(rx_sc->stats);
325 kfree(rx_sc);
326}
327
328static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
329{
330 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
331}
332
333static void macsec_rxsc_put(struct macsec_rx_sc *sc)
334{
335 if (refcount_dec_and_test(&sc->refcnt))
336 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
337}
338
339static void free_rxsa(struct rcu_head *head)
340{
341 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
342
343 crypto_free_aead(sa->key.tfm);
344 free_percpu(sa->stats);
345 kfree(sa);
346}
347
348static void macsec_rxsa_put(struct macsec_rx_sa *sa)
349{
350 if (refcount_dec_and_test(&sa->refcnt))
351 call_rcu(&sa->rcu, free_rxsa);
352}
353
354static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
355{
356 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
357
358 if (!sa || !sa->active)
359 return NULL;
360
361 if (!refcount_inc_not_zero(&sa->refcnt))
362 return NULL;
363
364 return sa;
365}
366
367static void free_txsa(struct rcu_head *head)
368{
369 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
370
371 crypto_free_aead(sa->key.tfm);
372 free_percpu(sa->stats);
373 kfree(sa);
374}
375
376static void macsec_txsa_put(struct macsec_tx_sa *sa)
377{
378 if (refcount_dec_and_test(&sa->refcnt))
379 call_rcu(&sa->rcu, free_txsa);
380}
381
382static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
383{
384 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
385 return (struct macsec_cb *)skb->cb;
386}
387
388#define MACSEC_PORT_ES (htons(0x0001))
389#define MACSEC_PORT_SCB (0x0000)
390#define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
391
392#define MACSEC_GCM_AES_128_SAK_LEN 16
393#define MACSEC_GCM_AES_256_SAK_LEN 32
394
395#define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
396#define DEFAULT_SEND_SCI true
397#define DEFAULT_ENCRYPT false
398#define DEFAULT_ENCODING_SA 0
399
400static bool send_sci(const struct macsec_secy *secy)
401{
402 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
403
404 return tx_sc->send_sci ||
405 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
406}
407
408static sci_t make_sci(u8 *addr, __be16 port)
409{
410 sci_t sci;
411
412 memcpy(&sci, addr, ETH_ALEN);
413 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
414
415 return sci;
416}
417
418static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
419{
420 sci_t sci;
421
422 if (sci_present)
423 memcpy(&sci, hdr->secure_channel_id,
424 sizeof(hdr->secure_channel_id));
425 else
426 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
427
428 return sci;
429}
430
431static unsigned int macsec_sectag_len(bool sci_present)
432{
433 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
434}
435
436static unsigned int macsec_hdr_len(bool sci_present)
437{
438 return macsec_sectag_len(sci_present) + ETH_HLEN;
439}
440
441static unsigned int macsec_extra_len(bool sci_present)
442{
443 return macsec_sectag_len(sci_present) + sizeof(__be16);
444}
445
446/* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
447static void macsec_fill_sectag(struct macsec_eth_header *h,
448 const struct macsec_secy *secy, u32 pn,
449 bool sci_present)
450{
451 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
452
453 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
454 h->eth.h_proto = htons(ETH_P_MACSEC);
455
456 if (sci_present) {
457 h->tci_an |= MACSEC_TCI_SC;
458 memcpy(&h->secure_channel_id, &secy->sci,
459 sizeof(h->secure_channel_id));
460 } else {
461 if (tx_sc->end_station)
462 h->tci_an |= MACSEC_TCI_ES;
463 if (tx_sc->scb)
464 h->tci_an |= MACSEC_TCI_SCB;
465 }
466
467 h->packet_number = htonl(pn);
468
469 /* with GCM, C/E clear for !encrypt, both set for encrypt */
470 if (tx_sc->encrypt)
471 h->tci_an |= MACSEC_TCI_CONFID;
472 else if (secy->icv_len != DEFAULT_ICV_LEN)
473 h->tci_an |= MACSEC_TCI_C;
474
475 h->tci_an |= tx_sc->encoding_sa;
476}
477
478static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
479{
480 if (data_len < MIN_NON_SHORT_LEN)
481 h->short_length = data_len;
482}
483
484/* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
485static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
486{
487 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
488 int len = skb->len - 2 * ETH_ALEN;
489 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
490
491 /* a) It comprises at least 17 octets */
492 if (skb->len <= 16)
493 return false;
494
495 /* b) MACsec EtherType: already checked */
496
497 /* c) V bit is clear */
498 if (h->tci_an & MACSEC_TCI_VERSION)
499 return false;
500
501 /* d) ES or SCB => !SC */
502 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
503 (h->tci_an & MACSEC_TCI_SC))
504 return false;
505
506 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
507 if (h->unused)
508 return false;
509
510 /* rx.pn != 0 (figure 10-5) */
511 if (!h->packet_number)
512 return false;
513
514 /* length check, f) g) h) i) */
515 if (h->short_length)
516 return len == extra_len + h->short_length;
517 return len >= extra_len + MIN_NON_SHORT_LEN;
518}
519
520#define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
521#define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
522
523static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
524{
525 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
526
527 gcm_iv->sci = sci;
528 gcm_iv->pn = htonl(pn);
529}
530
531static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
532{
533 return (struct macsec_eth_header *)skb_mac_header(skb);
534}
535
536static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
537{
538 u32 pn;
539
540 spin_lock_bh(&tx_sa->lock);
541 pn = tx_sa->next_pn;
542
543 tx_sa->next_pn++;
544 if (tx_sa->next_pn == 0) {
545 pr_debug("PN wrapped, transitioning to !oper\n");
546 tx_sa->active = false;
547 if (secy->protect_frames)
548 secy->operational = false;
549 }
550 spin_unlock_bh(&tx_sa->lock);
551
552 return pn;
553}
554
555static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
556{
557 struct macsec_dev *macsec = netdev_priv(dev);
558
559 skb->dev = macsec->real_dev;
560 skb_reset_mac_header(skb);
561 skb->protocol = eth_hdr(skb)->h_proto;
562}
563
564static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
565 struct macsec_tx_sa *tx_sa)
566{
567 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
568
569 u64_stats_update_begin(&txsc_stats->syncp);
570 if (tx_sc->encrypt) {
571 txsc_stats->stats.OutOctetsEncrypted += skb->len;
572 txsc_stats->stats.OutPktsEncrypted++;
573 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
574 } else {
575 txsc_stats->stats.OutOctetsProtected += skb->len;
576 txsc_stats->stats.OutPktsProtected++;
577 this_cpu_inc(tx_sa->stats->OutPktsProtected);
578 }
579 u64_stats_update_end(&txsc_stats->syncp);
580}
581
582static void count_tx(struct net_device *dev, int ret, int len)
583{
584 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
585 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
586
587 u64_stats_update_begin(&stats->syncp);
588 stats->tx_packets++;
589 stats->tx_bytes += len;
590 u64_stats_update_end(&stats->syncp);
591 }
592}
593
594static void macsec_encrypt_done(struct crypto_async_request *base, int err)
595{
596 struct sk_buff *skb = base->data;
597 struct net_device *dev = skb->dev;
598 struct macsec_dev *macsec = macsec_priv(dev);
599 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
600 int len, ret;
601
602 aead_request_free(macsec_skb_cb(skb)->req);
603
604 rcu_read_lock_bh();
605 macsec_encrypt_finish(skb, dev);
606 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
607 len = skb->len;
608 ret = dev_queue_xmit(skb);
609 count_tx(dev, ret, len);
610 rcu_read_unlock_bh();
611
612 macsec_txsa_put(sa);
613 dev_put(dev);
614}
615
616static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
617 unsigned char **iv,
618 struct scatterlist **sg,
619 int num_frags)
620{
621 size_t size, iv_offset, sg_offset;
622 struct aead_request *req;
623 void *tmp;
624
625 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
626 iv_offset = size;
627 size += GCM_AES_IV_LEN;
628
629 size = ALIGN(size, __alignof__(struct scatterlist));
630 sg_offset = size;
631 size += sizeof(struct scatterlist) * num_frags;
632
633 tmp = kmalloc(size, GFP_ATOMIC);
634 if (!tmp)
635 return NULL;
636
637 *iv = (unsigned char *)(tmp + iv_offset);
638 *sg = (struct scatterlist *)(tmp + sg_offset);
639 req = tmp;
640
641 aead_request_set_tfm(req, tfm);
642
643 return req;
644}
645
646static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
647 struct net_device *dev)
648{
649 int ret;
650 struct scatterlist *sg;
651 struct sk_buff *trailer;
652 unsigned char *iv;
653 struct ethhdr *eth;
654 struct macsec_eth_header *hh;
655 size_t unprotected_len;
656 struct aead_request *req;
657 struct macsec_secy *secy;
658 struct macsec_tx_sc *tx_sc;
659 struct macsec_tx_sa *tx_sa;
660 struct macsec_dev *macsec = macsec_priv(dev);
661 bool sci_present;
662 u32 pn;
663
664 secy = &macsec->secy;
665 tx_sc = &secy->tx_sc;
666
667 /* 10.5.1 TX SA assignment */
668 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
669 if (!tx_sa) {
670 secy->operational = false;
671 kfree_skb(skb);
672 return ERR_PTR(-EINVAL);
673 }
674
675 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
676 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
677 struct sk_buff *nskb = skb_copy_expand(skb,
678 MACSEC_NEEDED_HEADROOM,
679 MACSEC_NEEDED_TAILROOM,
680 GFP_ATOMIC);
681 if (likely(nskb)) {
682 consume_skb(skb);
683 skb = nskb;
684 } else {
685 macsec_txsa_put(tx_sa);
686 kfree_skb(skb);
687 return ERR_PTR(-ENOMEM);
688 }
689 } else {
690 skb = skb_unshare(skb, GFP_ATOMIC);
691 if (!skb) {
692 macsec_txsa_put(tx_sa);
693 return ERR_PTR(-ENOMEM);
694 }
695 }
696
697 unprotected_len = skb->len;
698 eth = eth_hdr(skb);
699 sci_present = send_sci(secy);
700 hh = skb_push(skb, macsec_extra_len(sci_present));
701 memmove(hh, eth, 2 * ETH_ALEN);
702
703 pn = tx_sa_update_pn(tx_sa, secy);
704 if (pn == 0) {
705 macsec_txsa_put(tx_sa);
706 kfree_skb(skb);
707 return ERR_PTR(-ENOLINK);
708 }
709 macsec_fill_sectag(hh, secy, pn, sci_present);
710 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
711
712 skb_put(skb, secy->icv_len);
713
714 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
715 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
716
717 u64_stats_update_begin(&secy_stats->syncp);
718 secy_stats->stats.OutPktsTooLong++;
719 u64_stats_update_end(&secy_stats->syncp);
720
721 macsec_txsa_put(tx_sa);
722 kfree_skb(skb);
723 return ERR_PTR(-EINVAL);
724 }
725
726 ret = skb_cow_data(skb, 0, &trailer);
727 if (unlikely(ret < 0)) {
728 macsec_txsa_put(tx_sa);
729 kfree_skb(skb);
730 return ERR_PTR(ret);
731 }
732
733 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
734 if (!req) {
735 macsec_txsa_put(tx_sa);
736 kfree_skb(skb);
737 return ERR_PTR(-ENOMEM);
738 }
739
740 macsec_fill_iv(iv, secy->sci, pn);
741
742 sg_init_table(sg, ret);
743 ret = skb_to_sgvec(skb, sg, 0, skb->len);
744 if (unlikely(ret < 0)) {
745 aead_request_free(req);
746 macsec_txsa_put(tx_sa);
747 kfree_skb(skb);
748 return ERR_PTR(ret);
749 }
750
751 if (tx_sc->encrypt) {
752 int len = skb->len - macsec_hdr_len(sci_present) -
753 secy->icv_len;
754 aead_request_set_crypt(req, sg, sg, len, iv);
755 aead_request_set_ad(req, macsec_hdr_len(sci_present));
756 } else {
757 aead_request_set_crypt(req, sg, sg, 0, iv);
758 aead_request_set_ad(req, skb->len - secy->icv_len);
759 }
760
761 macsec_skb_cb(skb)->req = req;
762 macsec_skb_cb(skb)->tx_sa = tx_sa;
763 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
764
765 dev_hold(skb->dev);
766 ret = crypto_aead_encrypt(req);
767 if (ret == -EINPROGRESS) {
768 return ERR_PTR(ret);
769 } else if (ret != 0) {
770 dev_put(skb->dev);
771 kfree_skb(skb);
772 aead_request_free(req);
773 macsec_txsa_put(tx_sa);
774 return ERR_PTR(-EINVAL);
775 }
776
777 dev_put(skb->dev);
778 aead_request_free(req);
779 macsec_txsa_put(tx_sa);
780
781 return skb;
782}
783
784static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
785{
786 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
787 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
788 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
789 u32 lowest_pn = 0;
790
791 spin_lock(&rx_sa->lock);
792 if (rx_sa->next_pn >= secy->replay_window)
793 lowest_pn = rx_sa->next_pn - secy->replay_window;
794
795 /* Now perform replay protection check again
796 * (see IEEE 802.1AE-2006 figure 10-5)
797 */
798 if (secy->replay_protect && pn < lowest_pn) {
799 spin_unlock(&rx_sa->lock);
800 u64_stats_update_begin(&rxsc_stats->syncp);
801 rxsc_stats->stats.InPktsLate++;
802 u64_stats_update_end(&rxsc_stats->syncp);
803 return false;
804 }
805
806 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
807 u64_stats_update_begin(&rxsc_stats->syncp);
808 if (hdr->tci_an & MACSEC_TCI_E)
809 rxsc_stats->stats.InOctetsDecrypted += skb->len;
810 else
811 rxsc_stats->stats.InOctetsValidated += skb->len;
812 u64_stats_update_end(&rxsc_stats->syncp);
813 }
814
815 if (!macsec_skb_cb(skb)->valid) {
816 spin_unlock(&rx_sa->lock);
817
818 /* 10.6.5 */
819 if (hdr->tci_an & MACSEC_TCI_C ||
820 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
821 u64_stats_update_begin(&rxsc_stats->syncp);
822 rxsc_stats->stats.InPktsNotValid++;
823 u64_stats_update_end(&rxsc_stats->syncp);
824 return false;
825 }
826
827 u64_stats_update_begin(&rxsc_stats->syncp);
828 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
829 rxsc_stats->stats.InPktsInvalid++;
830 this_cpu_inc(rx_sa->stats->InPktsInvalid);
831 } else if (pn < lowest_pn) {
832 rxsc_stats->stats.InPktsDelayed++;
833 } else {
834 rxsc_stats->stats.InPktsUnchecked++;
835 }
836 u64_stats_update_end(&rxsc_stats->syncp);
837 } else {
838 u64_stats_update_begin(&rxsc_stats->syncp);
839 if (pn < lowest_pn) {
840 rxsc_stats->stats.InPktsDelayed++;
841 } else {
842 rxsc_stats->stats.InPktsOK++;
843 this_cpu_inc(rx_sa->stats->InPktsOK);
844 }
845 u64_stats_update_end(&rxsc_stats->syncp);
846
847 if (pn >= rx_sa->next_pn)
848 rx_sa->next_pn = pn + 1;
849 spin_unlock(&rx_sa->lock);
850 }
851
852 return true;
853}
854
855static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
856{
857 skb->pkt_type = PACKET_HOST;
858 skb->protocol = eth_type_trans(skb, dev);
859
860 skb_reset_network_header(skb);
861 if (!skb_transport_header_was_set(skb))
862 skb_reset_transport_header(skb);
863 skb_reset_mac_len(skb);
864}
865
866static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
867{
David Brazdil0f672f62019-12-10 10:32:29 +0000868 skb->ip_summed = CHECKSUM_NONE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000869 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
870 skb_pull(skb, hdr_len);
871 pskb_trim_unique(skb, skb->len - icv_len);
872}
873
874static void count_rx(struct net_device *dev, int len)
875{
876 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
877
878 u64_stats_update_begin(&stats->syncp);
879 stats->rx_packets++;
880 stats->rx_bytes += len;
881 u64_stats_update_end(&stats->syncp);
882}
883
884static void macsec_decrypt_done(struct crypto_async_request *base, int err)
885{
886 struct sk_buff *skb = base->data;
887 struct net_device *dev = skb->dev;
888 struct macsec_dev *macsec = macsec_priv(dev);
889 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
890 struct macsec_rx_sc *rx_sc = rx_sa->sc;
891 int len;
892 u32 pn;
893
894 aead_request_free(macsec_skb_cb(skb)->req);
895
896 if (!err)
897 macsec_skb_cb(skb)->valid = true;
898
899 rcu_read_lock_bh();
900 pn = ntohl(macsec_ethhdr(skb)->packet_number);
901 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
902 rcu_read_unlock_bh();
903 kfree_skb(skb);
904 goto out;
905 }
906
907 macsec_finalize_skb(skb, macsec->secy.icv_len,
908 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
909 macsec_reset_skb(skb, macsec->secy.netdev);
910
911 len = skb->len;
912 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
913 count_rx(dev, len);
914
915 rcu_read_unlock_bh();
916
917out:
918 macsec_rxsa_put(rx_sa);
919 macsec_rxsc_put(rx_sc);
920 dev_put(dev);
921}
922
923static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
924 struct net_device *dev,
925 struct macsec_rx_sa *rx_sa,
926 sci_t sci,
927 struct macsec_secy *secy)
928{
929 int ret;
930 struct scatterlist *sg;
931 struct sk_buff *trailer;
932 unsigned char *iv;
933 struct aead_request *req;
934 struct macsec_eth_header *hdr;
935 u16 icv_len = secy->icv_len;
936
937 macsec_skb_cb(skb)->valid = false;
938 skb = skb_share_check(skb, GFP_ATOMIC);
939 if (!skb)
940 return ERR_PTR(-ENOMEM);
941
942 ret = skb_cow_data(skb, 0, &trailer);
943 if (unlikely(ret < 0)) {
944 kfree_skb(skb);
945 return ERR_PTR(ret);
946 }
947 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
948 if (!req) {
949 kfree_skb(skb);
950 return ERR_PTR(-ENOMEM);
951 }
952
953 hdr = (struct macsec_eth_header *)skb->data;
954 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
955
956 sg_init_table(sg, ret);
957 ret = skb_to_sgvec(skb, sg, 0, skb->len);
958 if (unlikely(ret < 0)) {
959 aead_request_free(req);
960 kfree_skb(skb);
961 return ERR_PTR(ret);
962 }
963
964 if (hdr->tci_an & MACSEC_TCI_E) {
965 /* confidentiality: ethernet + macsec header
966 * authenticated, encrypted payload
967 */
968 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
969
970 aead_request_set_crypt(req, sg, sg, len, iv);
971 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
972 skb = skb_unshare(skb, GFP_ATOMIC);
973 if (!skb) {
974 aead_request_free(req);
975 return ERR_PTR(-ENOMEM);
976 }
977 } else {
978 /* integrity only: all headers + data authenticated */
979 aead_request_set_crypt(req, sg, sg, icv_len, iv);
980 aead_request_set_ad(req, skb->len - icv_len);
981 }
982
983 macsec_skb_cb(skb)->req = req;
984 skb->dev = dev;
985 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
986
987 dev_hold(dev);
988 ret = crypto_aead_decrypt(req);
989 if (ret == -EINPROGRESS) {
990 return ERR_PTR(ret);
991 } else if (ret != 0) {
992 /* decryption/authentication failed
993 * 10.6 if validateFrames is disabled, deliver anyway
994 */
995 if (ret != -EBADMSG) {
996 kfree_skb(skb);
997 skb = ERR_PTR(ret);
998 }
999 } else {
1000 macsec_skb_cb(skb)->valid = true;
1001 }
1002 dev_put(dev);
1003
1004 aead_request_free(req);
1005
1006 return skb;
1007}
1008
1009static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
1010{
1011 struct macsec_rx_sc *rx_sc;
1012
1013 for_each_rxsc(secy, rx_sc) {
1014 if (rx_sc->sci == sci)
1015 return rx_sc;
1016 }
1017
1018 return NULL;
1019}
1020
1021static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1022{
1023 struct macsec_rx_sc *rx_sc;
1024
1025 for_each_rxsc_rtnl(secy, rx_sc) {
1026 if (rx_sc->sci == sci)
1027 return rx_sc;
1028 }
1029
1030 return NULL;
1031}
1032
1033static void handle_not_macsec(struct sk_buff *skb)
1034{
1035 struct macsec_rxh_data *rxd;
1036 struct macsec_dev *macsec;
1037
1038 rcu_read_lock();
1039 rxd = macsec_data_rcu(skb->dev);
1040
1041 /* 10.6 If the management control validateFrames is not
1042 * Strict, frames without a SecTAG are received, counted, and
1043 * delivered to the Controlled Port
1044 */
1045 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1046 struct sk_buff *nskb;
1047 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1048
1049 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1050 u64_stats_update_begin(&secy_stats->syncp);
1051 secy_stats->stats.InPktsNoTag++;
1052 u64_stats_update_end(&secy_stats->syncp);
1053 continue;
1054 }
1055
1056 /* deliver on this port */
1057 nskb = skb_clone(skb, GFP_ATOMIC);
1058 if (!nskb)
1059 break;
1060
1061 nskb->dev = macsec->secy.netdev;
1062
1063 if (netif_rx(nskb) == NET_RX_SUCCESS) {
1064 u64_stats_update_begin(&secy_stats->syncp);
1065 secy_stats->stats.InPktsUntagged++;
1066 u64_stats_update_end(&secy_stats->syncp);
1067 }
1068 }
1069
1070 rcu_read_unlock();
1071}
1072
1073static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1074{
1075 struct sk_buff *skb = *pskb;
1076 struct net_device *dev = skb->dev;
1077 struct macsec_eth_header *hdr;
1078 struct macsec_secy *secy = NULL;
1079 struct macsec_rx_sc *rx_sc;
1080 struct macsec_rx_sa *rx_sa;
1081 struct macsec_rxh_data *rxd;
1082 struct macsec_dev *macsec;
Olivier Deprez0e641232021-09-23 10:07:05 +02001083 unsigned int len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001084 sci_t sci;
1085 u32 pn;
1086 bool cbit;
1087 struct pcpu_rx_sc_stats *rxsc_stats;
1088 struct pcpu_secy_stats *secy_stats;
1089 bool pulled_sci;
1090 int ret;
1091
1092 if (skb_headroom(skb) < ETH_HLEN)
1093 goto drop_direct;
1094
1095 hdr = macsec_ethhdr(skb);
1096 if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1097 handle_not_macsec(skb);
1098
1099 /* and deliver to the uncontrolled port */
1100 return RX_HANDLER_PASS;
1101 }
1102
1103 skb = skb_unshare(skb, GFP_ATOMIC);
David Brazdil0f672f62019-12-10 10:32:29 +00001104 *pskb = skb;
1105 if (!skb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001106 return RX_HANDLER_CONSUMED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001107
1108 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1109 if (!pulled_sci) {
1110 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1111 goto drop_direct;
1112 }
1113
1114 hdr = macsec_ethhdr(skb);
1115
1116 /* Frames with a SecTAG that has the TCI E bit set but the C
1117 * bit clear are discarded, as this reserved encoding is used
1118 * to identify frames with a SecTAG that are not to be
1119 * delivered to the Controlled Port.
1120 */
1121 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1122 return RX_HANDLER_PASS;
1123
1124 /* now, pull the extra length */
1125 if (hdr->tci_an & MACSEC_TCI_SC) {
1126 if (!pulled_sci)
1127 goto drop_direct;
1128 }
1129
1130 /* ethernet header is part of crypto processing */
1131 skb_push(skb, ETH_HLEN);
1132
1133 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1134 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1135 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1136
1137 rcu_read_lock();
1138 rxd = macsec_data_rcu(skb->dev);
1139
1140 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1141 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
David Brazdil0f672f62019-12-10 10:32:29 +00001142
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143 sc = sc ? macsec_rxsc_get(sc) : NULL;
1144
1145 if (sc) {
1146 secy = &macsec->secy;
1147 rx_sc = sc;
1148 break;
1149 }
1150 }
1151
1152 if (!secy)
1153 goto nosci;
1154
1155 dev = secy->netdev;
1156 macsec = macsec_priv(dev);
1157 secy_stats = this_cpu_ptr(macsec->stats);
1158 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1159
1160 if (!macsec_validate_skb(skb, secy->icv_len)) {
1161 u64_stats_update_begin(&secy_stats->syncp);
1162 secy_stats->stats.InPktsBadTag++;
1163 u64_stats_update_end(&secy_stats->syncp);
1164 goto drop_nosa;
1165 }
1166
1167 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1168 if (!rx_sa) {
1169 /* 10.6.1 if the SA is not in use */
1170
1171 /* If validateFrames is Strict or the C bit in the
1172 * SecTAG is set, discard
1173 */
1174 if (hdr->tci_an & MACSEC_TCI_C ||
1175 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1176 u64_stats_update_begin(&rxsc_stats->syncp);
1177 rxsc_stats->stats.InPktsNotUsingSA++;
1178 u64_stats_update_end(&rxsc_stats->syncp);
1179 goto drop_nosa;
1180 }
1181
1182 /* not Strict, the frame (with the SecTAG and ICV
1183 * removed) is delivered to the Controlled Port.
1184 */
1185 u64_stats_update_begin(&rxsc_stats->syncp);
1186 rxsc_stats->stats.InPktsUnusedSA++;
1187 u64_stats_update_end(&rxsc_stats->syncp);
1188 goto deliver;
1189 }
1190
1191 /* First, PN check to avoid decrypting obviously wrong packets */
1192 pn = ntohl(hdr->packet_number);
1193 if (secy->replay_protect) {
1194 bool late;
1195
1196 spin_lock(&rx_sa->lock);
1197 late = rx_sa->next_pn >= secy->replay_window &&
1198 pn < (rx_sa->next_pn - secy->replay_window);
1199 spin_unlock(&rx_sa->lock);
1200
1201 if (late) {
1202 u64_stats_update_begin(&rxsc_stats->syncp);
1203 rxsc_stats->stats.InPktsLate++;
1204 u64_stats_update_end(&rxsc_stats->syncp);
1205 goto drop;
1206 }
1207 }
1208
1209 macsec_skb_cb(skb)->rx_sa = rx_sa;
1210
1211 /* Disabled && !changed text => skip validation */
1212 if (hdr->tci_an & MACSEC_TCI_C ||
1213 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1214 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1215
1216 if (IS_ERR(skb)) {
1217 /* the decrypt callback needs the reference */
1218 if (PTR_ERR(skb) != -EINPROGRESS) {
1219 macsec_rxsa_put(rx_sa);
1220 macsec_rxsc_put(rx_sc);
1221 }
1222 rcu_read_unlock();
1223 *pskb = NULL;
1224 return RX_HANDLER_CONSUMED;
1225 }
1226
1227 if (!macsec_post_decrypt(skb, secy, pn))
1228 goto drop;
1229
1230deliver:
1231 macsec_finalize_skb(skb, secy->icv_len,
1232 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1233 macsec_reset_skb(skb, secy->netdev);
1234
1235 if (rx_sa)
1236 macsec_rxsa_put(rx_sa);
1237 macsec_rxsc_put(rx_sc);
1238
David Brazdil0f672f62019-12-10 10:32:29 +00001239 skb_orphan(skb);
Olivier Deprez0e641232021-09-23 10:07:05 +02001240 len = skb->len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001241 ret = gro_cells_receive(&macsec->gro_cells, skb);
1242 if (ret == NET_RX_SUCCESS)
Olivier Deprez0e641232021-09-23 10:07:05 +02001243 count_rx(dev, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001244 else
1245 macsec->secy.netdev->stats.rx_dropped++;
1246
1247 rcu_read_unlock();
1248
1249 *pskb = NULL;
1250 return RX_HANDLER_CONSUMED;
1251
1252drop:
1253 macsec_rxsa_put(rx_sa);
1254drop_nosa:
1255 macsec_rxsc_put(rx_sc);
1256 rcu_read_unlock();
1257drop_direct:
1258 kfree_skb(skb);
1259 *pskb = NULL;
1260 return RX_HANDLER_CONSUMED;
1261
1262nosci:
1263 /* 10.6.1 if the SC is not found */
1264 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1265 if (!cbit)
1266 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1267 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1268
1269 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1270 struct sk_buff *nskb;
1271
1272 secy_stats = this_cpu_ptr(macsec->stats);
1273
1274 /* If validateFrames is Strict or the C bit in the
1275 * SecTAG is set, discard
1276 */
1277 if (cbit ||
1278 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1279 u64_stats_update_begin(&secy_stats->syncp);
1280 secy_stats->stats.InPktsNoSCI++;
1281 u64_stats_update_end(&secy_stats->syncp);
1282 continue;
1283 }
1284
1285 /* not strict, the frame (with the SecTAG and ICV
1286 * removed) is delivered to the Controlled Port.
1287 */
1288 nskb = skb_clone(skb, GFP_ATOMIC);
1289 if (!nskb)
1290 break;
1291
1292 macsec_reset_skb(nskb, macsec->secy.netdev);
1293
1294 ret = netif_rx(nskb);
1295 if (ret == NET_RX_SUCCESS) {
1296 u64_stats_update_begin(&secy_stats->syncp);
1297 secy_stats->stats.InPktsUnknownSCI++;
1298 u64_stats_update_end(&secy_stats->syncp);
1299 } else {
1300 macsec->secy.netdev->stats.rx_dropped++;
1301 }
1302 }
1303
1304 rcu_read_unlock();
1305 *pskb = skb;
1306 return RX_HANDLER_PASS;
1307}
1308
1309static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1310{
1311 struct crypto_aead *tfm;
1312 int ret;
1313
Olivier Deprez0e641232021-09-23 10:07:05 +02001314 /* Pick a sync gcm(aes) cipher to ensure order is preserved. */
1315 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001316
1317 if (IS_ERR(tfm))
1318 return tfm;
1319
1320 ret = crypto_aead_setkey(tfm, key, key_len);
1321 if (ret < 0)
1322 goto fail;
1323
1324 ret = crypto_aead_setauthsize(tfm, icv_len);
1325 if (ret < 0)
1326 goto fail;
1327
1328 return tfm;
1329fail:
1330 crypto_free_aead(tfm);
1331 return ERR_PTR(ret);
1332}
1333
1334static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1335 int icv_len)
1336{
1337 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1338 if (!rx_sa->stats)
1339 return -ENOMEM;
1340
1341 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1342 if (IS_ERR(rx_sa->key.tfm)) {
1343 free_percpu(rx_sa->stats);
1344 return PTR_ERR(rx_sa->key.tfm);
1345 }
1346
1347 rx_sa->active = false;
1348 rx_sa->next_pn = 1;
1349 refcount_set(&rx_sa->refcnt, 1);
1350 spin_lock_init(&rx_sa->lock);
1351
1352 return 0;
1353}
1354
1355static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1356{
1357 rx_sa->active = false;
1358
1359 macsec_rxsa_put(rx_sa);
1360}
1361
1362static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1363{
1364 int i;
1365
1366 for (i = 0; i < MACSEC_NUM_AN; i++) {
1367 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1368
1369 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1370 if (sa)
1371 clear_rx_sa(sa);
1372 }
1373
1374 macsec_rxsc_put(rx_sc);
1375}
1376
1377static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1378{
1379 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1380
1381 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1382 rx_sc;
1383 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1384 if (rx_sc->sci == sci) {
1385 if (rx_sc->active)
1386 secy->n_rx_sc--;
1387 rcu_assign_pointer(*rx_scp, rx_sc->next);
1388 return rx_sc;
1389 }
1390 }
1391
1392 return NULL;
1393}
1394
1395static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1396{
1397 struct macsec_rx_sc *rx_sc;
1398 struct macsec_dev *macsec;
1399 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1400 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1401 struct macsec_secy *secy;
1402
1403 list_for_each_entry(macsec, &rxd->secys, secys) {
1404 if (find_rx_sc_rtnl(&macsec->secy, sci))
1405 return ERR_PTR(-EEXIST);
1406 }
1407
1408 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1409 if (!rx_sc)
1410 return ERR_PTR(-ENOMEM);
1411
1412 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1413 if (!rx_sc->stats) {
1414 kfree(rx_sc);
1415 return ERR_PTR(-ENOMEM);
1416 }
1417
1418 rx_sc->sci = sci;
1419 rx_sc->active = true;
1420 refcount_set(&rx_sc->refcnt, 1);
1421
1422 secy = &macsec_priv(dev)->secy;
1423 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1424 rcu_assign_pointer(secy->rx_sc, rx_sc);
1425
1426 if (rx_sc->active)
1427 secy->n_rx_sc++;
1428
1429 return rx_sc;
1430}
1431
1432static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1433 int icv_len)
1434{
1435 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1436 if (!tx_sa->stats)
1437 return -ENOMEM;
1438
1439 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1440 if (IS_ERR(tx_sa->key.tfm)) {
1441 free_percpu(tx_sa->stats);
1442 return PTR_ERR(tx_sa->key.tfm);
1443 }
1444
1445 tx_sa->active = false;
1446 refcount_set(&tx_sa->refcnt, 1);
1447 spin_lock_init(&tx_sa->lock);
1448
1449 return 0;
1450}
1451
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
1477static sci_t nla_get_sci(const struct nlattr *nla)
1478{
1479 return (__force sci_t)nla_get_u64(nla);
1480}
1481
1482static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1483 int padattr)
1484{
1485 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1486}
1487
1488static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1489 struct nlattr **attrs,
1490 struct nlattr **tb_sa,
1491 struct net_device **devp,
1492 struct macsec_secy **secyp,
1493 struct macsec_tx_sc **scp,
1494 u8 *assoc_num)
1495{
1496 struct net_device *dev;
1497 struct macsec_secy *secy;
1498 struct macsec_tx_sc *tx_sc;
1499 struct macsec_tx_sa *tx_sa;
1500
1501 if (!tb_sa[MACSEC_SA_ATTR_AN])
1502 return ERR_PTR(-EINVAL);
1503
1504 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1505
1506 dev = get_dev_from_nl(net, attrs);
1507 if (IS_ERR(dev))
1508 return ERR_CAST(dev);
1509
1510 if (*assoc_num >= MACSEC_NUM_AN)
1511 return ERR_PTR(-EINVAL);
1512
1513 secy = &macsec_priv(dev)->secy;
1514 tx_sc = &secy->tx_sc;
1515
1516 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1517 if (!tx_sa)
1518 return ERR_PTR(-ENODEV);
1519
1520 *devp = dev;
1521 *scp = tx_sc;
1522 *secyp = secy;
1523 return tx_sa;
1524}
1525
1526static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1527 struct nlattr **attrs,
1528 struct nlattr **tb_rxsc,
1529 struct net_device **devp,
1530 struct macsec_secy **secyp)
1531{
1532 struct net_device *dev;
1533 struct macsec_secy *secy;
1534 struct macsec_rx_sc *rx_sc;
1535 sci_t sci;
1536
1537 dev = get_dev_from_nl(net, attrs);
1538 if (IS_ERR(dev))
1539 return ERR_CAST(dev);
1540
1541 secy = &macsec_priv(dev)->secy;
1542
1543 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1544 return ERR_PTR(-EINVAL);
1545
1546 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1547 rx_sc = find_rx_sc_rtnl(secy, sci);
1548 if (!rx_sc)
1549 return ERR_PTR(-ENODEV);
1550
1551 *secyp = secy;
1552 *devp = dev;
1553
1554 return rx_sc;
1555}
1556
1557static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1558 struct nlattr **attrs,
1559 struct nlattr **tb_rxsc,
1560 struct nlattr **tb_sa,
1561 struct net_device **devp,
1562 struct macsec_secy **secyp,
1563 struct macsec_rx_sc **scp,
1564 u8 *assoc_num)
1565{
1566 struct macsec_rx_sc *rx_sc;
1567 struct macsec_rx_sa *rx_sa;
1568
1569 if (!tb_sa[MACSEC_SA_ATTR_AN])
1570 return ERR_PTR(-EINVAL);
1571
1572 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1573 if (*assoc_num >= MACSEC_NUM_AN)
1574 return ERR_PTR(-EINVAL);
1575
1576 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1577 if (IS_ERR(rx_sc))
1578 return ERR_CAST(rx_sc);
1579
1580 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1581 if (!rx_sa)
1582 return ERR_PTR(-ENODEV);
1583
1584 *scp = rx_sc;
1585 return rx_sa;
1586}
1587
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001588static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1589 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1590 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1591 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1592};
1593
1594static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1595 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1596 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1597};
1598
1599static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1600 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1601 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1602 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1603 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1604 .len = MACSEC_KEYID_LEN, },
1605 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1606 .len = MACSEC_MAX_KEY_LEN, },
1607};
1608
1609static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1610{
1611 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1612 return -EINVAL;
1613
David Brazdil0f672f62019-12-10 10:32:29 +00001614 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 +00001615 return -EINVAL;
1616
1617 return 0;
1618}
1619
1620static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1621{
1622 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1623 return -EINVAL;
1624
David Brazdil0f672f62019-12-10 10:32:29 +00001625 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 +00001626 return -EINVAL;
1627
1628 return 0;
1629}
1630
1631static bool validate_add_rxsa(struct nlattr **attrs)
1632{
1633 if (!attrs[MACSEC_SA_ATTR_AN] ||
1634 !attrs[MACSEC_SA_ATTR_KEY] ||
1635 !attrs[MACSEC_SA_ATTR_KEYID])
1636 return false;
1637
1638 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1639 return false;
1640
1641 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1642 return false;
1643
1644 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1645 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1646 return false;
1647 }
1648
1649 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1650 return false;
1651
1652 return true;
1653}
1654
1655static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1656{
1657 struct net_device *dev;
1658 struct nlattr **attrs = info->attrs;
1659 struct macsec_secy *secy;
1660 struct macsec_rx_sc *rx_sc;
1661 struct macsec_rx_sa *rx_sa;
1662 unsigned char assoc_num;
1663 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1664 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1665 int err;
1666
1667 if (!attrs[MACSEC_ATTR_IFINDEX])
1668 return -EINVAL;
1669
1670 if (parse_sa_config(attrs, tb_sa))
1671 return -EINVAL;
1672
1673 if (parse_rxsc_config(attrs, tb_rxsc))
1674 return -EINVAL;
1675
1676 if (!validate_add_rxsa(tb_sa))
1677 return -EINVAL;
1678
1679 rtnl_lock();
1680 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1681 if (IS_ERR(rx_sc)) {
1682 rtnl_unlock();
1683 return PTR_ERR(rx_sc);
1684 }
1685
1686 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1687
1688 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1689 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1690 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1691 rtnl_unlock();
1692 return -EINVAL;
1693 }
1694
1695 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1696 if (rx_sa) {
1697 rtnl_unlock();
1698 return -EBUSY;
1699 }
1700
1701 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1702 if (!rx_sa) {
1703 rtnl_unlock();
1704 return -ENOMEM;
1705 }
1706
1707 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1708 secy->key_len, secy->icv_len);
1709 if (err < 0) {
1710 kfree(rx_sa);
1711 rtnl_unlock();
1712 return err;
1713 }
1714
1715 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1716 spin_lock_bh(&rx_sa->lock);
1717 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1718 spin_unlock_bh(&rx_sa->lock);
1719 }
1720
1721 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1722 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1723
1724 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1725 rx_sa->sc = rx_sc;
1726 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1727
1728 rtnl_unlock();
1729
1730 return 0;
1731}
1732
1733static bool validate_add_rxsc(struct nlattr **attrs)
1734{
1735 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1736 return false;
1737
1738 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1739 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1740 return false;
1741 }
1742
1743 return true;
1744}
1745
1746static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1747{
1748 struct net_device *dev;
1749 sci_t sci = MACSEC_UNDEF_SCI;
1750 struct nlattr **attrs = info->attrs;
1751 struct macsec_rx_sc *rx_sc;
1752 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1753
1754 if (!attrs[MACSEC_ATTR_IFINDEX])
1755 return -EINVAL;
1756
1757 if (parse_rxsc_config(attrs, tb_rxsc))
1758 return -EINVAL;
1759
1760 if (!validate_add_rxsc(tb_rxsc))
1761 return -EINVAL;
1762
1763 rtnl_lock();
1764 dev = get_dev_from_nl(genl_info_net(info), attrs);
1765 if (IS_ERR(dev)) {
1766 rtnl_unlock();
1767 return PTR_ERR(dev);
1768 }
1769
1770 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1771
1772 rx_sc = create_rx_sc(dev, sci);
1773 if (IS_ERR(rx_sc)) {
1774 rtnl_unlock();
1775 return PTR_ERR(rx_sc);
1776 }
1777
1778 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1779 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1780
1781 rtnl_unlock();
1782
1783 return 0;
1784}
1785
1786static bool validate_add_txsa(struct nlattr **attrs)
1787{
1788 if (!attrs[MACSEC_SA_ATTR_AN] ||
1789 !attrs[MACSEC_SA_ATTR_PN] ||
1790 !attrs[MACSEC_SA_ATTR_KEY] ||
1791 !attrs[MACSEC_SA_ATTR_KEYID])
1792 return false;
1793
1794 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1795 return false;
1796
1797 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1798 return false;
1799
1800 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1801 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1802 return false;
1803 }
1804
1805 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1806 return false;
1807
1808 return true;
1809}
1810
1811static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1812{
1813 struct net_device *dev;
1814 struct nlattr **attrs = info->attrs;
1815 struct macsec_secy *secy;
1816 struct macsec_tx_sc *tx_sc;
1817 struct macsec_tx_sa *tx_sa;
1818 unsigned char assoc_num;
1819 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1820 int err;
1821
1822 if (!attrs[MACSEC_ATTR_IFINDEX])
1823 return -EINVAL;
1824
1825 if (parse_sa_config(attrs, tb_sa))
1826 return -EINVAL;
1827
1828 if (!validate_add_txsa(tb_sa))
1829 return -EINVAL;
1830
1831 rtnl_lock();
1832 dev = get_dev_from_nl(genl_info_net(info), attrs);
1833 if (IS_ERR(dev)) {
1834 rtnl_unlock();
1835 return PTR_ERR(dev);
1836 }
1837
1838 secy = &macsec_priv(dev)->secy;
1839 tx_sc = &secy->tx_sc;
1840
1841 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1842
1843 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1844 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1845 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1846 rtnl_unlock();
1847 return -EINVAL;
1848 }
1849
1850 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1851 if (tx_sa) {
1852 rtnl_unlock();
1853 return -EBUSY;
1854 }
1855
1856 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1857 if (!tx_sa) {
1858 rtnl_unlock();
1859 return -ENOMEM;
1860 }
1861
1862 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1863 secy->key_len, secy->icv_len);
1864 if (err < 0) {
1865 kfree(tx_sa);
1866 rtnl_unlock();
1867 return err;
1868 }
1869
1870 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1871
1872 spin_lock_bh(&tx_sa->lock);
1873 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1874 spin_unlock_bh(&tx_sa->lock);
1875
1876 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1877 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1878
1879 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1880 secy->operational = true;
1881
1882 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1883
1884 rtnl_unlock();
1885
1886 return 0;
1887}
1888
1889static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1890{
1891 struct nlattr **attrs = info->attrs;
1892 struct net_device *dev;
1893 struct macsec_secy *secy;
1894 struct macsec_rx_sc *rx_sc;
1895 struct macsec_rx_sa *rx_sa;
1896 u8 assoc_num;
1897 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1898 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1899
1900 if (!attrs[MACSEC_ATTR_IFINDEX])
1901 return -EINVAL;
1902
1903 if (parse_sa_config(attrs, tb_sa))
1904 return -EINVAL;
1905
1906 if (parse_rxsc_config(attrs, tb_rxsc))
1907 return -EINVAL;
1908
1909 rtnl_lock();
1910 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1911 &dev, &secy, &rx_sc, &assoc_num);
1912 if (IS_ERR(rx_sa)) {
1913 rtnl_unlock();
1914 return PTR_ERR(rx_sa);
1915 }
1916
1917 if (rx_sa->active) {
1918 rtnl_unlock();
1919 return -EBUSY;
1920 }
1921
1922 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1923 clear_rx_sa(rx_sa);
1924
1925 rtnl_unlock();
1926
1927 return 0;
1928}
1929
1930static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1931{
1932 struct nlattr **attrs = info->attrs;
1933 struct net_device *dev;
1934 struct macsec_secy *secy;
1935 struct macsec_rx_sc *rx_sc;
1936 sci_t sci;
1937 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1938
1939 if (!attrs[MACSEC_ATTR_IFINDEX])
1940 return -EINVAL;
1941
1942 if (parse_rxsc_config(attrs, tb_rxsc))
1943 return -EINVAL;
1944
1945 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1946 return -EINVAL;
1947
1948 rtnl_lock();
1949 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1950 if (IS_ERR(dev)) {
1951 rtnl_unlock();
1952 return PTR_ERR(dev);
1953 }
1954
1955 secy = &macsec_priv(dev)->secy;
1956 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1957
1958 rx_sc = del_rx_sc(secy, sci);
1959 if (!rx_sc) {
1960 rtnl_unlock();
1961 return -ENODEV;
1962 }
1963
1964 free_rx_sc(rx_sc);
1965 rtnl_unlock();
1966
1967 return 0;
1968}
1969
1970static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1971{
1972 struct nlattr **attrs = info->attrs;
1973 struct net_device *dev;
1974 struct macsec_secy *secy;
1975 struct macsec_tx_sc *tx_sc;
1976 struct macsec_tx_sa *tx_sa;
1977 u8 assoc_num;
1978 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1979
1980 if (!attrs[MACSEC_ATTR_IFINDEX])
1981 return -EINVAL;
1982
1983 if (parse_sa_config(attrs, tb_sa))
1984 return -EINVAL;
1985
1986 rtnl_lock();
1987 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
1988 &dev, &secy, &tx_sc, &assoc_num);
1989 if (IS_ERR(tx_sa)) {
1990 rtnl_unlock();
1991 return PTR_ERR(tx_sa);
1992 }
1993
1994 if (tx_sa->active) {
1995 rtnl_unlock();
1996 return -EBUSY;
1997 }
1998
1999 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2000 clear_tx_sa(tx_sa);
2001
2002 rtnl_unlock();
2003
2004 return 0;
2005}
2006
2007static bool validate_upd_sa(struct nlattr **attrs)
2008{
2009 if (!attrs[MACSEC_SA_ATTR_AN] ||
2010 attrs[MACSEC_SA_ATTR_KEY] ||
2011 attrs[MACSEC_SA_ATTR_KEYID])
2012 return false;
2013
2014 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2015 return false;
2016
2017 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2018 return false;
2019
2020 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2021 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2022 return false;
2023 }
2024
2025 return true;
2026}
2027
2028static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2029{
2030 struct nlattr **attrs = info->attrs;
2031 struct net_device *dev;
2032 struct macsec_secy *secy;
2033 struct macsec_tx_sc *tx_sc;
2034 struct macsec_tx_sa *tx_sa;
2035 u8 assoc_num;
2036 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2037
2038 if (!attrs[MACSEC_ATTR_IFINDEX])
2039 return -EINVAL;
2040
2041 if (parse_sa_config(attrs, tb_sa))
2042 return -EINVAL;
2043
2044 if (!validate_upd_sa(tb_sa))
2045 return -EINVAL;
2046
2047 rtnl_lock();
2048 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2049 &dev, &secy, &tx_sc, &assoc_num);
2050 if (IS_ERR(tx_sa)) {
2051 rtnl_unlock();
2052 return PTR_ERR(tx_sa);
2053 }
2054
2055 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2056 spin_lock_bh(&tx_sa->lock);
2057 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2058 spin_unlock_bh(&tx_sa->lock);
2059 }
2060
2061 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2062 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2063
2064 if (assoc_num == tx_sc->encoding_sa)
2065 secy->operational = tx_sa->active;
2066
2067 rtnl_unlock();
2068
2069 return 0;
2070}
2071
2072static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2073{
2074 struct nlattr **attrs = info->attrs;
2075 struct net_device *dev;
2076 struct macsec_secy *secy;
2077 struct macsec_rx_sc *rx_sc;
2078 struct macsec_rx_sa *rx_sa;
2079 u8 assoc_num;
2080 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2081 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2082
2083 if (!attrs[MACSEC_ATTR_IFINDEX])
2084 return -EINVAL;
2085
2086 if (parse_rxsc_config(attrs, tb_rxsc))
2087 return -EINVAL;
2088
2089 if (parse_sa_config(attrs, tb_sa))
2090 return -EINVAL;
2091
2092 if (!validate_upd_sa(tb_sa))
2093 return -EINVAL;
2094
2095 rtnl_lock();
2096 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2097 &dev, &secy, &rx_sc, &assoc_num);
2098 if (IS_ERR(rx_sa)) {
2099 rtnl_unlock();
2100 return PTR_ERR(rx_sa);
2101 }
2102
2103 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2104 spin_lock_bh(&rx_sa->lock);
2105 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2106 spin_unlock_bh(&rx_sa->lock);
2107 }
2108
2109 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2110 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2111
2112 rtnl_unlock();
2113 return 0;
2114}
2115
2116static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2117{
2118 struct nlattr **attrs = info->attrs;
2119 struct net_device *dev;
2120 struct macsec_secy *secy;
2121 struct macsec_rx_sc *rx_sc;
2122 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2123
2124 if (!attrs[MACSEC_ATTR_IFINDEX])
2125 return -EINVAL;
2126
2127 if (parse_rxsc_config(attrs, tb_rxsc))
2128 return -EINVAL;
2129
2130 if (!validate_add_rxsc(tb_rxsc))
2131 return -EINVAL;
2132
2133 rtnl_lock();
2134 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2135 if (IS_ERR(rx_sc)) {
2136 rtnl_unlock();
2137 return PTR_ERR(rx_sc);
2138 }
2139
2140 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2141 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2142
2143 if (rx_sc->active != new)
2144 secy->n_rx_sc += new ? 1 : -1;
2145
2146 rx_sc->active = new;
2147 }
2148
2149 rtnl_unlock();
2150
2151 return 0;
2152}
2153
2154static int copy_tx_sa_stats(struct sk_buff *skb,
David Brazdil0f672f62019-12-10 10:32:29 +00002155 struct macsec_tx_sa_stats __percpu *pstats)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002156{
2157 struct macsec_tx_sa_stats sum = {0, };
2158 int cpu;
2159
2160 for_each_possible_cpu(cpu) {
2161 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2162
2163 sum.OutPktsProtected += stats->OutPktsProtected;
2164 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2165 }
2166
2167 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2168 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2169 return -EMSGSIZE;
2170
2171 return 0;
2172}
2173
David Brazdil0f672f62019-12-10 10:32:29 +00002174static noinline_for_stack int
2175copy_rx_sa_stats(struct sk_buff *skb,
2176 struct macsec_rx_sa_stats __percpu *pstats)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002177{
2178 struct macsec_rx_sa_stats sum = {0, };
2179 int cpu;
2180
2181 for_each_possible_cpu(cpu) {
2182 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2183
2184 sum.InPktsOK += stats->InPktsOK;
2185 sum.InPktsInvalid += stats->InPktsInvalid;
2186 sum.InPktsNotValid += stats->InPktsNotValid;
2187 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2188 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2189 }
2190
2191 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2192 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2193 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2194 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2195 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2196 return -EMSGSIZE;
2197
2198 return 0;
2199}
2200
David Brazdil0f672f62019-12-10 10:32:29 +00002201static noinline_for_stack int
2202copy_rx_sc_stats(struct sk_buff *skb, struct pcpu_rx_sc_stats __percpu *pstats)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002203{
2204 struct macsec_rx_sc_stats sum = {0, };
2205 int cpu;
2206
2207 for_each_possible_cpu(cpu) {
2208 const struct pcpu_rx_sc_stats *stats;
2209 struct macsec_rx_sc_stats tmp;
2210 unsigned int start;
2211
2212 stats = per_cpu_ptr(pstats, cpu);
2213 do {
2214 start = u64_stats_fetch_begin_irq(&stats->syncp);
2215 memcpy(&tmp, &stats->stats, sizeof(tmp));
2216 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2217
2218 sum.InOctetsValidated += tmp.InOctetsValidated;
2219 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2220 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2221 sum.InPktsDelayed += tmp.InPktsDelayed;
2222 sum.InPktsOK += tmp.InPktsOK;
2223 sum.InPktsInvalid += tmp.InPktsInvalid;
2224 sum.InPktsLate += tmp.InPktsLate;
2225 sum.InPktsNotValid += tmp.InPktsNotValid;
2226 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2227 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2228 }
2229
2230 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2231 sum.InOctetsValidated,
2232 MACSEC_RXSC_STATS_ATTR_PAD) ||
2233 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2234 sum.InOctetsDecrypted,
2235 MACSEC_RXSC_STATS_ATTR_PAD) ||
2236 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2237 sum.InPktsUnchecked,
2238 MACSEC_RXSC_STATS_ATTR_PAD) ||
2239 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2240 sum.InPktsDelayed,
2241 MACSEC_RXSC_STATS_ATTR_PAD) ||
2242 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2243 sum.InPktsOK,
2244 MACSEC_RXSC_STATS_ATTR_PAD) ||
2245 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2246 sum.InPktsInvalid,
2247 MACSEC_RXSC_STATS_ATTR_PAD) ||
2248 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2249 sum.InPktsLate,
2250 MACSEC_RXSC_STATS_ATTR_PAD) ||
2251 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2252 sum.InPktsNotValid,
2253 MACSEC_RXSC_STATS_ATTR_PAD) ||
2254 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2255 sum.InPktsNotUsingSA,
2256 MACSEC_RXSC_STATS_ATTR_PAD) ||
2257 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2258 sum.InPktsUnusedSA,
2259 MACSEC_RXSC_STATS_ATTR_PAD))
2260 return -EMSGSIZE;
2261
2262 return 0;
2263}
2264
David Brazdil0f672f62019-12-10 10:32:29 +00002265static noinline_for_stack int
2266copy_tx_sc_stats(struct sk_buff *skb, struct pcpu_tx_sc_stats __percpu *pstats)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002267{
2268 struct macsec_tx_sc_stats sum = {0, };
2269 int cpu;
2270
2271 for_each_possible_cpu(cpu) {
2272 const struct pcpu_tx_sc_stats *stats;
2273 struct macsec_tx_sc_stats tmp;
2274 unsigned int start;
2275
2276 stats = per_cpu_ptr(pstats, cpu);
2277 do {
2278 start = u64_stats_fetch_begin_irq(&stats->syncp);
2279 memcpy(&tmp, &stats->stats, sizeof(tmp));
2280 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2281
2282 sum.OutPktsProtected += tmp.OutPktsProtected;
2283 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2284 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2285 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2286 }
2287
2288 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2289 sum.OutPktsProtected,
2290 MACSEC_TXSC_STATS_ATTR_PAD) ||
2291 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2292 sum.OutPktsEncrypted,
2293 MACSEC_TXSC_STATS_ATTR_PAD) ||
2294 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2295 sum.OutOctetsProtected,
2296 MACSEC_TXSC_STATS_ATTR_PAD) ||
2297 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2298 sum.OutOctetsEncrypted,
2299 MACSEC_TXSC_STATS_ATTR_PAD))
2300 return -EMSGSIZE;
2301
2302 return 0;
2303}
2304
David Brazdil0f672f62019-12-10 10:32:29 +00002305static noinline_for_stack int
2306copy_secy_stats(struct sk_buff *skb, struct pcpu_secy_stats __percpu *pstats)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002307{
2308 struct macsec_dev_stats sum = {0, };
2309 int cpu;
2310
2311 for_each_possible_cpu(cpu) {
2312 const struct pcpu_secy_stats *stats;
2313 struct macsec_dev_stats tmp;
2314 unsigned int start;
2315
2316 stats = per_cpu_ptr(pstats, cpu);
2317 do {
2318 start = u64_stats_fetch_begin_irq(&stats->syncp);
2319 memcpy(&tmp, &stats->stats, sizeof(tmp));
2320 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2321
2322 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2323 sum.InPktsUntagged += tmp.InPktsUntagged;
2324 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2325 sum.InPktsNoTag += tmp.InPktsNoTag;
2326 sum.InPktsBadTag += tmp.InPktsBadTag;
2327 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2328 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2329 sum.InPktsOverrun += tmp.InPktsOverrun;
2330 }
2331
2332 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2333 sum.OutPktsUntagged,
2334 MACSEC_SECY_STATS_ATTR_PAD) ||
2335 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2336 sum.InPktsUntagged,
2337 MACSEC_SECY_STATS_ATTR_PAD) ||
2338 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2339 sum.OutPktsTooLong,
2340 MACSEC_SECY_STATS_ATTR_PAD) ||
2341 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2342 sum.InPktsNoTag,
2343 MACSEC_SECY_STATS_ATTR_PAD) ||
2344 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2345 sum.InPktsBadTag,
2346 MACSEC_SECY_STATS_ATTR_PAD) ||
2347 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2348 sum.InPktsUnknownSCI,
2349 MACSEC_SECY_STATS_ATTR_PAD) ||
2350 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2351 sum.InPktsNoSCI,
2352 MACSEC_SECY_STATS_ATTR_PAD) ||
2353 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2354 sum.InPktsOverrun,
2355 MACSEC_SECY_STATS_ATTR_PAD))
2356 return -EMSGSIZE;
2357
2358 return 0;
2359}
2360
2361static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2362{
2363 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
David Brazdil0f672f62019-12-10 10:32:29 +00002364 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
2365 MACSEC_ATTR_SECY);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002366 u64 csid;
2367
2368 if (!secy_nest)
2369 return 1;
2370
2371 switch (secy->key_len) {
2372 case MACSEC_GCM_AES_128_SAK_LEN:
2373 csid = MACSEC_DEFAULT_CIPHER_ID;
2374 break;
2375 case MACSEC_GCM_AES_256_SAK_LEN:
2376 csid = MACSEC_CIPHER_ID_GCM_AES_256;
2377 break;
2378 default:
2379 goto cancel;
2380 }
2381
2382 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2383 MACSEC_SECY_ATTR_PAD) ||
2384 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2385 csid, MACSEC_SECY_ATTR_PAD) ||
2386 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2387 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2388 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2389 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2390 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2391 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2392 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2393 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2394 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2395 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2396 goto cancel;
2397
2398 if (secy->replay_protect) {
2399 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2400 goto cancel;
2401 }
2402
2403 nla_nest_end(skb, secy_nest);
2404 return 0;
2405
2406cancel:
2407 nla_nest_cancel(skb, secy_nest);
2408 return 1;
2409}
2410
David Brazdil0f672f62019-12-10 10:32:29 +00002411static noinline_for_stack int
2412dump_secy(struct macsec_secy *secy, struct net_device *dev,
2413 struct sk_buff *skb, struct netlink_callback *cb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002414{
2415 struct macsec_rx_sc *rx_sc;
2416 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2417 struct nlattr *txsa_list, *rxsc_list;
2418 int i, j;
2419 void *hdr;
2420 struct nlattr *attr;
2421
2422 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2423 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2424 if (!hdr)
2425 return -EMSGSIZE;
2426
2427 genl_dump_check_consistent(cb, hdr);
2428
2429 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2430 goto nla_put_failure;
2431
2432 if (nla_put_secy(secy, skb))
2433 goto nla_put_failure;
2434
David Brazdil0f672f62019-12-10 10:32:29 +00002435 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002436 if (!attr)
2437 goto nla_put_failure;
2438 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2439 nla_nest_cancel(skb, attr);
2440 goto nla_put_failure;
2441 }
2442 nla_nest_end(skb, attr);
2443
David Brazdil0f672f62019-12-10 10:32:29 +00002444 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002445 if (!attr)
2446 goto nla_put_failure;
2447 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2448 nla_nest_cancel(skb, attr);
2449 goto nla_put_failure;
2450 }
2451 nla_nest_end(skb, attr);
2452
David Brazdil0f672f62019-12-10 10:32:29 +00002453 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002454 if (!txsa_list)
2455 goto nla_put_failure;
2456 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2457 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2458 struct nlattr *txsa_nest;
2459
2460 if (!tx_sa)
2461 continue;
2462
David Brazdil0f672f62019-12-10 10:32:29 +00002463 txsa_nest = nla_nest_start_noflag(skb, j++);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002464 if (!txsa_nest) {
2465 nla_nest_cancel(skb, txsa_list);
2466 goto nla_put_failure;
2467 }
2468
2469 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2470 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2471 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2472 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2473 nla_nest_cancel(skb, txsa_nest);
2474 nla_nest_cancel(skb, txsa_list);
2475 goto nla_put_failure;
2476 }
2477
David Brazdil0f672f62019-12-10 10:32:29 +00002478 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002479 if (!attr) {
2480 nla_nest_cancel(skb, txsa_nest);
2481 nla_nest_cancel(skb, txsa_list);
2482 goto nla_put_failure;
2483 }
2484 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2485 nla_nest_cancel(skb, attr);
2486 nla_nest_cancel(skb, txsa_nest);
2487 nla_nest_cancel(skb, txsa_list);
2488 goto nla_put_failure;
2489 }
2490 nla_nest_end(skb, attr);
2491
2492 nla_nest_end(skb, txsa_nest);
2493 }
2494 nla_nest_end(skb, txsa_list);
2495
David Brazdil0f672f62019-12-10 10:32:29 +00002496 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002497 if (!rxsc_list)
2498 goto nla_put_failure;
2499
2500 j = 1;
2501 for_each_rxsc_rtnl(secy, rx_sc) {
2502 int k;
2503 struct nlattr *rxsa_list;
David Brazdil0f672f62019-12-10 10:32:29 +00002504 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002505
2506 if (!rxsc_nest) {
2507 nla_nest_cancel(skb, rxsc_list);
2508 goto nla_put_failure;
2509 }
2510
2511 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2512 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2513 MACSEC_RXSC_ATTR_PAD)) {
2514 nla_nest_cancel(skb, rxsc_nest);
2515 nla_nest_cancel(skb, rxsc_list);
2516 goto nla_put_failure;
2517 }
2518
David Brazdil0f672f62019-12-10 10:32:29 +00002519 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002520 if (!attr) {
2521 nla_nest_cancel(skb, rxsc_nest);
2522 nla_nest_cancel(skb, rxsc_list);
2523 goto nla_put_failure;
2524 }
2525 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2526 nla_nest_cancel(skb, attr);
2527 nla_nest_cancel(skb, rxsc_nest);
2528 nla_nest_cancel(skb, rxsc_list);
2529 goto nla_put_failure;
2530 }
2531 nla_nest_end(skb, attr);
2532
David Brazdil0f672f62019-12-10 10:32:29 +00002533 rxsa_list = nla_nest_start_noflag(skb,
2534 MACSEC_RXSC_ATTR_SA_LIST);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002535 if (!rxsa_list) {
2536 nla_nest_cancel(skb, rxsc_nest);
2537 nla_nest_cancel(skb, rxsc_list);
2538 goto nla_put_failure;
2539 }
2540
2541 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2542 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2543 struct nlattr *rxsa_nest;
2544
2545 if (!rx_sa)
2546 continue;
2547
David Brazdil0f672f62019-12-10 10:32:29 +00002548 rxsa_nest = nla_nest_start_noflag(skb, k++);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002549 if (!rxsa_nest) {
2550 nla_nest_cancel(skb, rxsa_list);
2551 nla_nest_cancel(skb, rxsc_nest);
2552 nla_nest_cancel(skb, rxsc_list);
2553 goto nla_put_failure;
2554 }
2555
David Brazdil0f672f62019-12-10 10:32:29 +00002556 attr = nla_nest_start_noflag(skb,
2557 MACSEC_SA_ATTR_STATS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002558 if (!attr) {
2559 nla_nest_cancel(skb, rxsa_list);
2560 nla_nest_cancel(skb, rxsc_nest);
2561 nla_nest_cancel(skb, rxsc_list);
2562 goto nla_put_failure;
2563 }
2564 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2565 nla_nest_cancel(skb, attr);
2566 nla_nest_cancel(skb, rxsa_list);
2567 nla_nest_cancel(skb, rxsc_nest);
2568 nla_nest_cancel(skb, rxsc_list);
2569 goto nla_put_failure;
2570 }
2571 nla_nest_end(skb, attr);
2572
2573 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2574 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2575 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2576 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2577 nla_nest_cancel(skb, rxsa_nest);
2578 nla_nest_cancel(skb, rxsc_nest);
2579 nla_nest_cancel(skb, rxsc_list);
2580 goto nla_put_failure;
2581 }
2582 nla_nest_end(skb, rxsa_nest);
2583 }
2584
2585 nla_nest_end(skb, rxsa_list);
2586 nla_nest_end(skb, rxsc_nest);
2587 }
2588
2589 nla_nest_end(skb, rxsc_list);
2590
2591 genlmsg_end(skb, hdr);
2592
2593 return 0;
2594
2595nla_put_failure:
2596 genlmsg_cancel(skb, hdr);
2597 return -EMSGSIZE;
2598}
2599
2600static int macsec_generation = 1; /* protected by RTNL */
2601
2602static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2603{
2604 struct net *net = sock_net(skb->sk);
2605 struct net_device *dev;
2606 int dev_idx, d;
2607
2608 dev_idx = cb->args[0];
2609
2610 d = 0;
2611 rtnl_lock();
2612
2613 cb->seq = macsec_generation;
2614
2615 for_each_netdev(net, dev) {
2616 struct macsec_secy *secy;
2617
2618 if (d < dev_idx)
2619 goto next;
2620
2621 if (!netif_is_macsec(dev))
2622 goto next;
2623
2624 secy = &macsec_priv(dev)->secy;
2625 if (dump_secy(secy, dev, skb, cb) < 0)
2626 goto done;
2627next:
2628 d++;
2629 }
2630
2631done:
2632 rtnl_unlock();
2633 cb->args[0] = d;
2634 return skb->len;
2635}
2636
2637static const struct genl_ops macsec_genl_ops[] = {
2638 {
2639 .cmd = MACSEC_CMD_GET_TXSC,
David Brazdil0f672f62019-12-10 10:32:29 +00002640 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002641 .dumpit = macsec_dump_txsc,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002642 },
2643 {
2644 .cmd = MACSEC_CMD_ADD_RXSC,
David Brazdil0f672f62019-12-10 10:32:29 +00002645 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002646 .doit = macsec_add_rxsc,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002647 .flags = GENL_ADMIN_PERM,
2648 },
2649 {
2650 .cmd = MACSEC_CMD_DEL_RXSC,
David Brazdil0f672f62019-12-10 10:32:29 +00002651 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002652 .doit = macsec_del_rxsc,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002653 .flags = GENL_ADMIN_PERM,
2654 },
2655 {
2656 .cmd = MACSEC_CMD_UPD_RXSC,
David Brazdil0f672f62019-12-10 10:32:29 +00002657 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002658 .doit = macsec_upd_rxsc,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002659 .flags = GENL_ADMIN_PERM,
2660 },
2661 {
2662 .cmd = MACSEC_CMD_ADD_TXSA,
David Brazdil0f672f62019-12-10 10:32:29 +00002663 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002664 .doit = macsec_add_txsa,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002665 .flags = GENL_ADMIN_PERM,
2666 },
2667 {
2668 .cmd = MACSEC_CMD_DEL_TXSA,
David Brazdil0f672f62019-12-10 10:32:29 +00002669 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002670 .doit = macsec_del_txsa,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002671 .flags = GENL_ADMIN_PERM,
2672 },
2673 {
2674 .cmd = MACSEC_CMD_UPD_TXSA,
David Brazdil0f672f62019-12-10 10:32:29 +00002675 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002676 .doit = macsec_upd_txsa,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002677 .flags = GENL_ADMIN_PERM,
2678 },
2679 {
2680 .cmd = MACSEC_CMD_ADD_RXSA,
David Brazdil0f672f62019-12-10 10:32:29 +00002681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002682 .doit = macsec_add_rxsa,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002683 .flags = GENL_ADMIN_PERM,
2684 },
2685 {
2686 .cmd = MACSEC_CMD_DEL_RXSA,
David Brazdil0f672f62019-12-10 10:32:29 +00002687 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002688 .doit = macsec_del_rxsa,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002689 .flags = GENL_ADMIN_PERM,
2690 },
2691 {
2692 .cmd = MACSEC_CMD_UPD_RXSA,
David Brazdil0f672f62019-12-10 10:32:29 +00002693 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002694 .doit = macsec_upd_rxsa,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002695 .flags = GENL_ADMIN_PERM,
2696 },
2697};
2698
2699static struct genl_family macsec_fam __ro_after_init = {
2700 .name = MACSEC_GENL_NAME,
2701 .hdrsize = 0,
2702 .version = MACSEC_GENL_VERSION,
2703 .maxattr = MACSEC_ATTR_MAX,
David Brazdil0f672f62019-12-10 10:32:29 +00002704 .policy = macsec_genl_policy,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002705 .netnsok = true,
2706 .module = THIS_MODULE,
2707 .ops = macsec_genl_ops,
2708 .n_ops = ARRAY_SIZE(macsec_genl_ops),
2709};
2710
2711static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2712 struct net_device *dev)
2713{
2714 struct macsec_dev *macsec = netdev_priv(dev);
2715 struct macsec_secy *secy = &macsec->secy;
2716 struct pcpu_secy_stats *secy_stats;
2717 int ret, len;
2718
2719 /* 10.5 */
2720 if (!secy->protect_frames) {
2721 secy_stats = this_cpu_ptr(macsec->stats);
2722 u64_stats_update_begin(&secy_stats->syncp);
2723 secy_stats->stats.OutPktsUntagged++;
2724 u64_stats_update_end(&secy_stats->syncp);
2725 skb->dev = macsec->real_dev;
2726 len = skb->len;
2727 ret = dev_queue_xmit(skb);
2728 count_tx(dev, ret, len);
2729 return ret;
2730 }
2731
2732 if (!secy->operational) {
2733 kfree_skb(skb);
2734 dev->stats.tx_dropped++;
2735 return NETDEV_TX_OK;
2736 }
2737
2738 skb = macsec_encrypt(skb, dev);
2739 if (IS_ERR(skb)) {
2740 if (PTR_ERR(skb) != -EINPROGRESS)
2741 dev->stats.tx_dropped++;
2742 return NETDEV_TX_OK;
2743 }
2744
2745 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2746
2747 macsec_encrypt_finish(skb, dev);
2748 len = skb->len;
2749 ret = dev_queue_xmit(skb);
2750 count_tx(dev, ret, len);
2751 return ret;
2752}
2753
2754#define MACSEC_FEATURES \
2755 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002756
2757static int macsec_dev_init(struct net_device *dev)
2758{
2759 struct macsec_dev *macsec = macsec_priv(dev);
2760 struct net_device *real_dev = macsec->real_dev;
2761 int err;
2762
2763 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2764 if (!dev->tstats)
2765 return -ENOMEM;
2766
2767 err = gro_cells_init(&macsec->gro_cells, dev);
2768 if (err) {
2769 free_percpu(dev->tstats);
2770 return err;
2771 }
2772
2773 dev->features = real_dev->features & MACSEC_FEATURES;
2774 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2775
2776 dev->needed_headroom = real_dev->needed_headroom +
2777 MACSEC_NEEDED_HEADROOM;
2778 dev->needed_tailroom = real_dev->needed_tailroom +
2779 MACSEC_NEEDED_TAILROOM;
2780
2781 if (is_zero_ether_addr(dev->dev_addr))
2782 eth_hw_addr_inherit(dev, real_dev);
2783 if (is_zero_ether_addr(dev->broadcast))
2784 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2785
2786 return 0;
2787}
2788
2789static void macsec_dev_uninit(struct net_device *dev)
2790{
2791 struct macsec_dev *macsec = macsec_priv(dev);
2792
2793 gro_cells_destroy(&macsec->gro_cells);
2794 free_percpu(dev->tstats);
2795}
2796
2797static netdev_features_t macsec_fix_features(struct net_device *dev,
2798 netdev_features_t features)
2799{
2800 struct macsec_dev *macsec = macsec_priv(dev);
2801 struct net_device *real_dev = macsec->real_dev;
2802
2803 features &= (real_dev->features & MACSEC_FEATURES) |
2804 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2805 features |= NETIF_F_LLTX;
2806
2807 return features;
2808}
2809
2810static int macsec_dev_open(struct net_device *dev)
2811{
2812 struct macsec_dev *macsec = macsec_priv(dev);
2813 struct net_device *real_dev = macsec->real_dev;
2814 int err;
2815
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002816 err = dev_uc_add(real_dev, dev->dev_addr);
2817 if (err < 0)
2818 return err;
2819
2820 if (dev->flags & IFF_ALLMULTI) {
2821 err = dev_set_allmulti(real_dev, 1);
2822 if (err < 0)
2823 goto del_unicast;
2824 }
2825
2826 if (dev->flags & IFF_PROMISC) {
2827 err = dev_set_promiscuity(real_dev, 1);
2828 if (err < 0)
2829 goto clear_allmulti;
2830 }
2831
2832 if (netif_carrier_ok(real_dev))
2833 netif_carrier_on(dev);
2834
2835 return 0;
2836clear_allmulti:
2837 if (dev->flags & IFF_ALLMULTI)
2838 dev_set_allmulti(real_dev, -1);
2839del_unicast:
2840 dev_uc_del(real_dev, dev->dev_addr);
2841 netif_carrier_off(dev);
2842 return err;
2843}
2844
2845static int macsec_dev_stop(struct net_device *dev)
2846{
2847 struct macsec_dev *macsec = macsec_priv(dev);
2848 struct net_device *real_dev = macsec->real_dev;
2849
2850 netif_carrier_off(dev);
2851
2852 dev_mc_unsync(real_dev, dev);
2853 dev_uc_unsync(real_dev, dev);
2854
2855 if (dev->flags & IFF_ALLMULTI)
2856 dev_set_allmulti(real_dev, -1);
2857
2858 if (dev->flags & IFF_PROMISC)
2859 dev_set_promiscuity(real_dev, -1);
2860
2861 dev_uc_del(real_dev, dev->dev_addr);
2862
2863 return 0;
2864}
2865
2866static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2867{
2868 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2869
2870 if (!(dev->flags & IFF_UP))
2871 return;
2872
2873 if (change & IFF_ALLMULTI)
2874 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2875
2876 if (change & IFF_PROMISC)
2877 dev_set_promiscuity(real_dev,
2878 dev->flags & IFF_PROMISC ? 1 : -1);
2879}
2880
2881static void macsec_dev_set_rx_mode(struct net_device *dev)
2882{
2883 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2884
2885 dev_mc_sync(real_dev, dev);
2886 dev_uc_sync(real_dev, dev);
2887}
2888
Olivier Deprez0e641232021-09-23 10:07:05 +02002889static sci_t dev_to_sci(struct net_device *dev, __be16 port)
2890{
2891 return make_sci(dev->dev_addr, port);
2892}
2893
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002894static int macsec_set_mac_address(struct net_device *dev, void *p)
2895{
2896 struct macsec_dev *macsec = macsec_priv(dev);
2897 struct net_device *real_dev = macsec->real_dev;
2898 struct sockaddr *addr = p;
2899 int err;
2900
2901 if (!is_valid_ether_addr(addr->sa_data))
2902 return -EADDRNOTAVAIL;
2903
2904 if (!(dev->flags & IFF_UP))
2905 goto out;
2906
2907 err = dev_uc_add(real_dev, addr->sa_data);
2908 if (err < 0)
2909 return err;
2910
2911 dev_uc_del(real_dev, dev->dev_addr);
2912
2913out:
2914 ether_addr_copy(dev->dev_addr, addr->sa_data);
Olivier Deprez0e641232021-09-23 10:07:05 +02002915 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002916 return 0;
2917}
2918
2919static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2920{
2921 struct macsec_dev *macsec = macsec_priv(dev);
2922 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2923
2924 if (macsec->real_dev->mtu - extra < new_mtu)
2925 return -ERANGE;
2926
2927 dev->mtu = new_mtu;
2928
2929 return 0;
2930}
2931
2932static void macsec_get_stats64(struct net_device *dev,
2933 struct rtnl_link_stats64 *s)
2934{
2935 int cpu;
2936
2937 if (!dev->tstats)
2938 return;
2939
2940 for_each_possible_cpu(cpu) {
2941 struct pcpu_sw_netstats *stats;
2942 struct pcpu_sw_netstats tmp;
2943 int start;
2944
2945 stats = per_cpu_ptr(dev->tstats, cpu);
2946 do {
2947 start = u64_stats_fetch_begin_irq(&stats->syncp);
2948 tmp.rx_packets = stats->rx_packets;
2949 tmp.rx_bytes = stats->rx_bytes;
2950 tmp.tx_packets = stats->tx_packets;
2951 tmp.tx_bytes = stats->tx_bytes;
2952 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2953
2954 s->rx_packets += tmp.rx_packets;
2955 s->rx_bytes += tmp.rx_bytes;
2956 s->tx_packets += tmp.tx_packets;
2957 s->tx_bytes += tmp.tx_bytes;
2958 }
2959
2960 s->rx_dropped = dev->stats.rx_dropped;
2961 s->tx_dropped = dev->stats.tx_dropped;
2962}
2963
2964static int macsec_get_iflink(const struct net_device *dev)
2965{
2966 return macsec_priv(dev)->real_dev->ifindex;
2967}
2968
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002969static const struct net_device_ops macsec_netdev_ops = {
2970 .ndo_init = macsec_dev_init,
2971 .ndo_uninit = macsec_dev_uninit,
2972 .ndo_open = macsec_dev_open,
2973 .ndo_stop = macsec_dev_stop,
2974 .ndo_fix_features = macsec_fix_features,
2975 .ndo_change_mtu = macsec_change_mtu,
2976 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2977 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2978 .ndo_set_mac_address = macsec_set_mac_address,
2979 .ndo_start_xmit = macsec_start_xmit,
2980 .ndo_get_stats64 = macsec_get_stats64,
2981 .ndo_get_iflink = macsec_get_iflink,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002982};
2983
2984static const struct device_type macsec_type = {
2985 .name = "macsec",
2986};
2987
2988static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2989 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
Olivier Deprez0e641232021-09-23 10:07:05 +02002990 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002991 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2992 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2993 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2994 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2995 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2996 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2997 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2998 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
2999 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3000 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3001 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3002};
3003
3004static void macsec_free_netdev(struct net_device *dev)
3005{
3006 struct macsec_dev *macsec = macsec_priv(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003007
3008 free_percpu(macsec->stats);
3009 free_percpu(macsec->secy.tx_sc.stats);
3010
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003011}
3012
3013static void macsec_setup(struct net_device *dev)
3014{
3015 ether_setup(dev);
3016 dev->min_mtu = 0;
3017 dev->max_mtu = ETH_MAX_MTU;
3018 dev->priv_flags |= IFF_NO_QUEUE;
3019 dev->netdev_ops = &macsec_netdev_ops;
3020 dev->needs_free_netdev = true;
3021 dev->priv_destructor = macsec_free_netdev;
3022 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3023
3024 eth_zero_addr(dev->broadcast);
3025}
3026
3027static int macsec_changelink_common(struct net_device *dev,
3028 struct nlattr *data[])
3029{
3030 struct macsec_secy *secy;
3031 struct macsec_tx_sc *tx_sc;
3032
3033 secy = &macsec_priv(dev)->secy;
3034 tx_sc = &secy->tx_sc;
3035
3036 if (data[IFLA_MACSEC_ENCODING_SA]) {
3037 struct macsec_tx_sa *tx_sa;
3038
3039 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3040 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3041
3042 secy->operational = tx_sa && tx_sa->active;
3043 }
3044
3045 if (data[IFLA_MACSEC_WINDOW])
3046 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3047
3048 if (data[IFLA_MACSEC_ENCRYPT])
3049 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3050
3051 if (data[IFLA_MACSEC_PROTECT])
3052 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3053
3054 if (data[IFLA_MACSEC_INC_SCI])
3055 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3056
3057 if (data[IFLA_MACSEC_ES])
3058 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3059
3060 if (data[IFLA_MACSEC_SCB])
3061 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3062
3063 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3064 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3065
3066 if (data[IFLA_MACSEC_VALIDATION])
3067 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3068
3069 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3070 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3071 case MACSEC_CIPHER_ID_GCM_AES_128:
3072 case MACSEC_DEFAULT_CIPHER_ID:
3073 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3074 break;
3075 case MACSEC_CIPHER_ID_GCM_AES_256:
3076 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3077 break;
3078 default:
3079 return -EINVAL;
3080 }
3081 }
3082
3083 return 0;
3084}
3085
3086static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3087 struct nlattr *data[],
3088 struct netlink_ext_ack *extack)
3089{
3090 if (!data)
3091 return 0;
3092
3093 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3094 data[IFLA_MACSEC_ICV_LEN] ||
3095 data[IFLA_MACSEC_SCI] ||
3096 data[IFLA_MACSEC_PORT])
3097 return -EINVAL;
3098
3099 return macsec_changelink_common(dev, data);
3100}
3101
3102static void macsec_del_dev(struct macsec_dev *macsec)
3103{
3104 int i;
3105
3106 while (macsec->secy.rx_sc) {
3107 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3108
3109 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3110 free_rx_sc(rx_sc);
3111 }
3112
3113 for (i = 0; i < MACSEC_NUM_AN; i++) {
3114 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3115
3116 if (sa) {
3117 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3118 clear_tx_sa(sa);
3119 }
3120 }
3121}
3122
3123static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3124{
3125 struct macsec_dev *macsec = macsec_priv(dev);
3126 struct net_device *real_dev = macsec->real_dev;
3127
3128 unregister_netdevice_queue(dev, head);
3129 list_del_rcu(&macsec->secys);
3130 macsec_del_dev(macsec);
3131 netdev_upper_dev_unlink(real_dev, dev);
3132
3133 macsec_generation++;
3134}
3135
3136static void macsec_dellink(struct net_device *dev, struct list_head *head)
3137{
3138 struct macsec_dev *macsec = macsec_priv(dev);
3139 struct net_device *real_dev = macsec->real_dev;
3140 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3141
3142 macsec_common_dellink(dev, head);
3143
3144 if (list_empty(&rxd->secys)) {
3145 netdev_rx_handler_unregister(real_dev);
3146 kfree(rxd);
3147 }
3148}
3149
3150static int register_macsec_dev(struct net_device *real_dev,
3151 struct net_device *dev)
3152{
3153 struct macsec_dev *macsec = macsec_priv(dev);
3154 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3155
3156 if (!rxd) {
3157 int err;
3158
3159 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3160 if (!rxd)
3161 return -ENOMEM;
3162
3163 INIT_LIST_HEAD(&rxd->secys);
3164
3165 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3166 rxd);
3167 if (err < 0) {
3168 kfree(rxd);
3169 return err;
3170 }
3171 }
3172
3173 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3174 return 0;
3175}
3176
3177static bool sci_exists(struct net_device *dev, sci_t sci)
3178{
3179 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3180 struct macsec_dev *macsec;
3181
3182 list_for_each_entry(macsec, &rxd->secys, secys) {
3183 if (macsec->secy.sci == sci)
3184 return true;
3185 }
3186
3187 return false;
3188}
3189
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003190static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3191{
3192 struct macsec_dev *macsec = macsec_priv(dev);
3193 struct macsec_secy *secy = &macsec->secy;
3194
3195 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3196 if (!macsec->stats)
3197 return -ENOMEM;
3198
3199 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3200 if (!secy->tx_sc.stats) {
3201 free_percpu(macsec->stats);
3202 return -ENOMEM;
3203 }
3204
3205 if (sci == MACSEC_UNDEF_SCI)
3206 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3207
3208 secy->netdev = dev;
3209 secy->operational = true;
3210 secy->key_len = DEFAULT_SAK_LEN;
3211 secy->icv_len = icv_len;
3212 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3213 secy->protect_frames = true;
3214 secy->replay_protect = false;
3215
3216 secy->sci = sci;
3217 secy->tx_sc.active = true;
3218 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3219 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3220 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3221 secy->tx_sc.end_station = false;
3222 secy->tx_sc.scb = false;
3223
3224 return 0;
3225}
3226
3227static int macsec_newlink(struct net *net, struct net_device *dev,
3228 struct nlattr *tb[], struct nlattr *data[],
3229 struct netlink_ext_ack *extack)
3230{
3231 struct macsec_dev *macsec = macsec_priv(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003232 rx_handler_func_t *rx_handler;
Olivier Deprez0e641232021-09-23 10:07:05 +02003233 u8 icv_len = DEFAULT_ICV_LEN;
3234 struct net_device *real_dev;
3235 int err, mtu;
3236 sci_t sci;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003237
3238 if (!tb[IFLA_LINK])
3239 return -EINVAL;
3240 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3241 if (!real_dev)
3242 return -ENODEV;
Olivier Deprez0e641232021-09-23 10:07:05 +02003243 if (real_dev->type != ARPHRD_ETHER)
3244 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003245
3246 dev->priv_flags |= IFF_MACSEC;
3247
3248 macsec->real_dev = real_dev;
3249
3250 if (data && data[IFLA_MACSEC_ICV_LEN])
3251 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
Olivier Deprez0e641232021-09-23 10:07:05 +02003252 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3253 if (mtu < 0)
3254 dev->mtu = 0;
3255 else
3256 dev->mtu = mtu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003257
3258 rx_handler = rtnl_dereference(real_dev->rx_handler);
3259 if (rx_handler && rx_handler != macsec_handle_frame)
3260 return -EBUSY;
3261
3262 err = register_netdevice(dev);
3263 if (err < 0)
3264 return err;
3265
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003266 err = netdev_upper_dev_link(real_dev, dev, extack);
3267 if (err < 0)
3268 goto unregister;
3269
3270 /* need to be already registered so that ->init has run and
3271 * the MAC addr is set
3272 */
3273 if (data && data[IFLA_MACSEC_SCI])
3274 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3275 else if (data && data[IFLA_MACSEC_PORT])
3276 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3277 else
3278 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3279
3280 if (rx_handler && sci_exists(real_dev, sci)) {
3281 err = -EBUSY;
3282 goto unlink;
3283 }
3284
3285 err = macsec_add_dev(dev, sci, icv_len);
3286 if (err)
3287 goto unlink;
3288
3289 if (data) {
3290 err = macsec_changelink_common(dev, data);
3291 if (err)
3292 goto del_dev;
3293 }
3294
3295 err = register_macsec_dev(real_dev, dev);
3296 if (err < 0)
3297 goto del_dev;
3298
David Brazdil0f672f62019-12-10 10:32:29 +00003299 netif_stacked_transfer_operstate(real_dev, dev);
3300 linkwatch_fire_event(dev);
3301
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003302 macsec_generation++;
3303
3304 return 0;
3305
3306del_dev:
3307 macsec_del_dev(macsec);
3308unlink:
3309 netdev_upper_dev_unlink(real_dev, dev);
3310unregister:
3311 unregister_netdevice(dev);
3312 return err;
3313}
3314
3315static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3316 struct netlink_ext_ack *extack)
3317{
3318 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3319 u8 icv_len = DEFAULT_ICV_LEN;
3320 int flag;
3321 bool es, scb, sci;
3322
3323 if (!data)
3324 return 0;
3325
3326 if (data[IFLA_MACSEC_CIPHER_SUITE])
3327 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3328
3329 if (data[IFLA_MACSEC_ICV_LEN]) {
3330 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3331 if (icv_len != DEFAULT_ICV_LEN) {
3332 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3333 struct crypto_aead *dummy_tfm;
3334
3335 dummy_tfm = macsec_alloc_tfm(dummy_key,
3336 DEFAULT_SAK_LEN,
3337 icv_len);
3338 if (IS_ERR(dummy_tfm))
3339 return PTR_ERR(dummy_tfm);
3340 crypto_free_aead(dummy_tfm);
3341 }
3342 }
3343
3344 switch (csid) {
3345 case MACSEC_CIPHER_ID_GCM_AES_128:
3346 case MACSEC_CIPHER_ID_GCM_AES_256:
3347 case MACSEC_DEFAULT_CIPHER_ID:
3348 if (icv_len < MACSEC_MIN_ICV_LEN ||
3349 icv_len > MACSEC_STD_ICV_LEN)
3350 return -EINVAL;
3351 break;
3352 default:
3353 return -EINVAL;
3354 }
3355
3356 if (data[IFLA_MACSEC_ENCODING_SA]) {
3357 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3358 return -EINVAL;
3359 }
3360
3361 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3362 flag < IFLA_MACSEC_VALIDATION;
3363 flag++) {
3364 if (data[flag]) {
3365 if (nla_get_u8(data[flag]) > 1)
3366 return -EINVAL;
3367 }
3368 }
3369
3370 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3371 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3372 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3373
3374 if ((sci && (scb || es)) || (scb && es))
3375 return -EINVAL;
3376
3377 if (data[IFLA_MACSEC_VALIDATION] &&
3378 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3379 return -EINVAL;
3380
3381 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3382 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3383 !data[IFLA_MACSEC_WINDOW])
3384 return -EINVAL;
3385
3386 return 0;
3387}
3388
3389static struct net *macsec_get_link_net(const struct net_device *dev)
3390{
3391 return dev_net(macsec_priv(dev)->real_dev);
3392}
3393
3394static size_t macsec_get_size(const struct net_device *dev)
3395{
3396 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
3397 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
3398 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
3399 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
3400 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
3401 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
3402 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
3403 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
3404 nla_total_size(1) + /* IFLA_MACSEC_ES */
3405 nla_total_size(1) + /* IFLA_MACSEC_SCB */
3406 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
3407 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
3408 0;
3409}
3410
3411static int macsec_fill_info(struct sk_buff *skb,
3412 const struct net_device *dev)
3413{
3414 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3415 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3416 u64 csid;
3417
3418 switch (secy->key_len) {
3419 case MACSEC_GCM_AES_128_SAK_LEN:
3420 csid = MACSEC_DEFAULT_CIPHER_ID;
3421 break;
3422 case MACSEC_GCM_AES_256_SAK_LEN:
3423 csid = MACSEC_CIPHER_ID_GCM_AES_256;
3424 break;
3425 default:
3426 goto nla_put_failure;
3427 }
3428
3429 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3430 IFLA_MACSEC_PAD) ||
3431 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3432 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3433 csid, IFLA_MACSEC_PAD) ||
3434 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3435 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3436 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3437 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3438 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3439 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3440 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3441 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3442 0)
3443 goto nla_put_failure;
3444
3445 if (secy->replay_protect) {
3446 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3447 goto nla_put_failure;
3448 }
3449
3450 return 0;
3451
3452nla_put_failure:
3453 return -EMSGSIZE;
3454}
3455
3456static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3457 .kind = "macsec",
3458 .priv_size = sizeof(struct macsec_dev),
3459 .maxtype = IFLA_MACSEC_MAX,
3460 .policy = macsec_rtnl_policy,
3461 .setup = macsec_setup,
3462 .validate = macsec_validate_attr,
3463 .newlink = macsec_newlink,
3464 .changelink = macsec_changelink,
3465 .dellink = macsec_dellink,
3466 .get_size = macsec_get_size,
3467 .fill_info = macsec_fill_info,
3468 .get_link_net = macsec_get_link_net,
3469};
3470
3471static bool is_macsec_master(struct net_device *dev)
3472{
3473 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3474}
3475
3476static int macsec_notify(struct notifier_block *this, unsigned long event,
3477 void *ptr)
3478{
3479 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3480 LIST_HEAD(head);
3481
3482 if (!is_macsec_master(real_dev))
3483 return NOTIFY_DONE;
3484
3485 switch (event) {
David Brazdil0f672f62019-12-10 10:32:29 +00003486 case NETDEV_DOWN:
3487 case NETDEV_UP:
3488 case NETDEV_CHANGE: {
3489 struct macsec_dev *m, *n;
3490 struct macsec_rxh_data *rxd;
3491
3492 rxd = macsec_data_rtnl(real_dev);
3493 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3494 struct net_device *dev = m->secy.netdev;
3495
3496 netif_stacked_transfer_operstate(real_dev, dev);
3497 }
3498 break;
3499 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003500 case NETDEV_UNREGISTER: {
3501 struct macsec_dev *m, *n;
3502 struct macsec_rxh_data *rxd;
3503
3504 rxd = macsec_data_rtnl(real_dev);
3505 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3506 macsec_common_dellink(m->secy.netdev, &head);
3507 }
3508
3509 netdev_rx_handler_unregister(real_dev);
3510 kfree(rxd);
3511
3512 unregister_netdevice_many(&head);
3513 break;
3514 }
3515 case NETDEV_CHANGEMTU: {
3516 struct macsec_dev *m;
3517 struct macsec_rxh_data *rxd;
3518
3519 rxd = macsec_data_rtnl(real_dev);
3520 list_for_each_entry(m, &rxd->secys, secys) {
3521 struct net_device *dev = m->secy.netdev;
3522 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3523 macsec_extra_len(true));
3524
3525 if (dev->mtu > mtu)
3526 dev_set_mtu(dev, mtu);
3527 }
3528 }
3529 }
3530
3531 return NOTIFY_OK;
3532}
3533
3534static struct notifier_block macsec_notifier = {
3535 .notifier_call = macsec_notify,
3536};
3537
3538static int __init macsec_init(void)
3539{
3540 int err;
3541
3542 pr_info("MACsec IEEE 802.1AE\n");
3543 err = register_netdevice_notifier(&macsec_notifier);
3544 if (err)
3545 return err;
3546
3547 err = rtnl_link_register(&macsec_link_ops);
3548 if (err)
3549 goto notifier;
3550
3551 err = genl_register_family(&macsec_fam);
3552 if (err)
3553 goto rtnl;
3554
3555 return 0;
3556
3557rtnl:
3558 rtnl_link_unregister(&macsec_link_ops);
3559notifier:
3560 unregister_netdevice_notifier(&macsec_notifier);
3561 return err;
3562}
3563
3564static void __exit macsec_exit(void)
3565{
3566 genl_unregister_family(&macsec_fam);
3567 rtnl_link_unregister(&macsec_link_ops);
3568 unregister_netdevice_notifier(&macsec_notifier);
3569 rcu_barrier();
3570}
3571
3572module_init(macsec_init);
3573module_exit(macsec_exit);
3574
3575MODULE_ALIAS_RTNL_LINK("macsec");
3576MODULE_ALIAS_GENL_FAMILY("macsec");
3577
3578MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3579MODULE_LICENSE("GPL v2");