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