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