macsec: fix use-after-free of skb during RX
[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{
868 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
869 skb_pull(skb, hdr_len);
870 pskb_trim_unique(skb, skb->len - icv_len);
871}
872
873static void count_rx(struct net_device *dev, int len)
874{
875 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
876
877 u64_stats_update_begin(&stats->syncp);
878 stats->rx_packets++;
879 stats->rx_bytes += len;
880 u64_stats_update_end(&stats->syncp);
881}
882
883static void macsec_decrypt_done(struct crypto_async_request *base, int err)
884{
885 struct sk_buff *skb = base->data;
886 struct net_device *dev = skb->dev;
887 struct macsec_dev *macsec = macsec_priv(dev);
888 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
c78ebe1d 889 struct macsec_rx_sc *rx_sc = rx_sa->sc;
863483c9 890 int len;
c09440f7
SD
891 u32 pn;
892
893 aead_request_free(macsec_skb_cb(skb)->req);
894
b3bdc3ac
LR
895 if (!err)
896 macsec_skb_cb(skb)->valid = true;
897
c09440f7
SD
898 rcu_read_lock_bh();
899 pn = ntohl(macsec_ethhdr(skb)->packet_number);
900 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
901 rcu_read_unlock_bh();
902 kfree_skb(skb);
903 goto out;
904 }
905
906 macsec_finalize_skb(skb, macsec->secy.icv_len,
907 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
908 macsec_reset_skb(skb, macsec->secy.netdev);
909
910 len = skb->len;
863483c9 911 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
c09440f7 912 count_rx(dev, len);
c09440f7
SD
913
914 rcu_read_unlock_bh();
915
916out:
917 macsec_rxsa_put(rx_sa);
c78ebe1d 918 macsec_rxsc_put(rx_sc);
c09440f7 919 dev_put(dev);
c09440f7
SD
920}
921
922static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
923 struct net_device *dev,
924 struct macsec_rx_sa *rx_sa,
925 sci_t sci,
926 struct macsec_secy *secy)
927{
928 int ret;
5d9649b3 929 struct scatterlist *sg;
5294b830 930 struct sk_buff *trailer;
5d9649b3 931 unsigned char *iv;
c09440f7
SD
932 struct aead_request *req;
933 struct macsec_eth_header *hdr;
934 u16 icv_len = secy->icv_len;
935
936 macsec_skb_cb(skb)->valid = false;
937 skb = skb_share_check(skb, GFP_ATOMIC);
938 if (!skb)
c3b7d0bd 939 return ERR_PTR(-ENOMEM);
c09440f7 940
5294b830
JD
941 ret = skb_cow_data(skb, 0, &trailer);
942 if (unlikely(ret < 0)) {
943 kfree_skb(skb);
944 return ERR_PTR(ret);
945 }
946 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
c09440f7
SD
947 if (!req) {
948 kfree_skb(skb);
c3b7d0bd 949 return ERR_PTR(-ENOMEM);
c09440f7
SD
950 }
951
952 hdr = (struct macsec_eth_header *)skb->data;
953 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
954
5294b830 955 sg_init_table(sg, ret);
cda7ea69
JD
956 ret = skb_to_sgvec(skb, sg, 0, skb->len);
957 if (unlikely(ret < 0)) {
5aba2ba5 958 aead_request_free(req);
cda7ea69
JD
959 kfree_skb(skb);
960 return ERR_PTR(ret);
961 }
c09440f7
SD
962
963 if (hdr->tci_an & MACSEC_TCI_E) {
964 /* confidentiality: ethernet + macsec header
965 * authenticated, encrypted payload
966 */
967 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
968
969 aead_request_set_crypt(req, sg, sg, len, iv);
970 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
971 skb = skb_unshare(skb, GFP_ATOMIC);
972 if (!skb) {
973 aead_request_free(req);
c3b7d0bd 974 return ERR_PTR(-ENOMEM);
c09440f7
SD
975 }
976 } else {
977 /* integrity only: all headers + data authenticated */
978 aead_request_set_crypt(req, sg, sg, icv_len, iv);
979 aead_request_set_ad(req, skb->len - icv_len);
980 }
981
982 macsec_skb_cb(skb)->req = req;
c09440f7
SD
983 skb->dev = dev;
984 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
985
986 dev_hold(dev);
987 ret = crypto_aead_decrypt(req);
988 if (ret == -EINPROGRESS) {
c3b7d0bd 989 return ERR_PTR(ret);
c09440f7
SD
990 } else if (ret != 0) {
991 /* decryption/authentication failed
992 * 10.6 if validateFrames is disabled, deliver anyway
993 */
994 if (ret != -EBADMSG) {
995 kfree_skb(skb);
c3b7d0bd 996 skb = ERR_PTR(ret);
c09440f7
SD
997 }
998 } else {
999 macsec_skb_cb(skb)->valid = true;
1000 }
1001 dev_put(dev);
1002
1003 aead_request_free(req);
1004
1005 return skb;
1006}
1007
1008static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
1009{
1010 struct macsec_rx_sc *rx_sc;
1011
1012 for_each_rxsc(secy, rx_sc) {
1013 if (rx_sc->sci == sci)
1014 return rx_sc;
1015 }
1016
1017 return NULL;
1018}
1019
1020static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1021{
1022 struct macsec_rx_sc *rx_sc;
1023
1024 for_each_rxsc_rtnl(secy, rx_sc) {
1025 if (rx_sc->sci == sci)
1026 return rx_sc;
1027 }
1028
1029 return NULL;
1030}
1031
1032static void handle_not_macsec(struct sk_buff *skb)
1033{
1034 struct macsec_rxh_data *rxd;
1035 struct macsec_dev *macsec;
1036
1037 rcu_read_lock();
1038 rxd = macsec_data_rcu(skb->dev);
1039
1040 /* 10.6 If the management control validateFrames is not
1041 * Strict, frames without a SecTAG are received, counted, and
1042 * delivered to the Controlled Port
1043 */
1044 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1045 struct sk_buff *nskb;
c09440f7
SD
1046 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1047
1048 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1049 u64_stats_update_begin(&secy_stats->syncp);
1050 secy_stats->stats.InPktsNoTag++;
1051 u64_stats_update_end(&secy_stats->syncp);
1052 continue;
1053 }
1054
1055 /* deliver on this port */
1056 nskb = skb_clone(skb, GFP_ATOMIC);
1057 if (!nskb)
1058 break;
1059
1060 nskb->dev = macsec->secy.netdev;
1061
863483c9 1062 if (netif_rx(nskb) == NET_RX_SUCCESS) {
c09440f7
SD
1063 u64_stats_update_begin(&secy_stats->syncp);
1064 secy_stats->stats.InPktsUntagged++;
1065 u64_stats_update_end(&secy_stats->syncp);
c09440f7
SD
1066 }
1067 }
1068
1069 rcu_read_unlock();
1070}
1071
1072static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1073{
1074 struct sk_buff *skb = *pskb;
1075 struct net_device *dev = skb->dev;
1076 struct macsec_eth_header *hdr;
1077 struct macsec_secy *secy = NULL;
1078 struct macsec_rx_sc *rx_sc;
1079 struct macsec_rx_sa *rx_sa;
1080 struct macsec_rxh_data *rxd;
1081 struct macsec_dev *macsec;
1082 sci_t sci;
1083 u32 pn;
1084 bool cbit;
1085 struct pcpu_rx_sc_stats *rxsc_stats;
1086 struct pcpu_secy_stats *secy_stats;
1087 bool pulled_sci;
5491e7c6 1088 int ret;
c09440f7
SD
1089
1090 if (skb_headroom(skb) < ETH_HLEN)
1091 goto drop_direct;
1092
1093 hdr = macsec_ethhdr(skb);
1094 if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1095 handle_not_macsec(skb);
1096
1097 /* and deliver to the uncontrolled port */
1098 return RX_HANDLER_PASS;
1099 }
1100
1101 skb = skb_unshare(skb, GFP_ATOMIC);
095c02da
AS
1102 *pskb = skb;
1103 if (!skb)
c09440f7 1104 return RX_HANDLER_CONSUMED;
c09440f7
SD
1105
1106 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1107 if (!pulled_sci) {
1108 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1109 goto drop_direct;
1110 }
1111
1112 hdr = macsec_ethhdr(skb);
1113
1114 /* Frames with a SecTAG that has the TCI E bit set but the C
1115 * bit clear are discarded, as this reserved encoding is used
1116 * to identify frames with a SecTAG that are not to be
1117 * delivered to the Controlled Port.
1118 */
1119 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1120 return RX_HANDLER_PASS;
1121
1122 /* now, pull the extra length */
1123 if (hdr->tci_an & MACSEC_TCI_SC) {
1124 if (!pulled_sci)
1125 goto drop_direct;
1126 }
1127
1128 /* ethernet header is part of crypto processing */
1129 skb_push(skb, ETH_HLEN);
1130
1131 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1132 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1133 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1134
1135 rcu_read_lock();
1136 rxd = macsec_data_rcu(skb->dev);
1137
1138 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1139 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
7979472b 1140
c78ebe1d 1141 sc = sc ? macsec_rxsc_get(sc) : NULL;
c09440f7
SD
1142
1143 if (sc) {
1144 secy = &macsec->secy;
1145 rx_sc = sc;
1146 break;
1147 }
1148 }
1149
1150 if (!secy)
1151 goto nosci;
1152
1153 dev = secy->netdev;
1154 macsec = macsec_priv(dev);
1155 secy_stats = this_cpu_ptr(macsec->stats);
1156 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1157
1158 if (!macsec_validate_skb(skb, secy->icv_len)) {
1159 u64_stats_update_begin(&secy_stats->syncp);
1160 secy_stats->stats.InPktsBadTag++;
1161 u64_stats_update_end(&secy_stats->syncp);
1162 goto drop_nosa;
1163 }
1164
1165 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1166 if (!rx_sa) {
1167 /* 10.6.1 if the SA is not in use */
1168
1169 /* If validateFrames is Strict or the C bit in the
1170 * SecTAG is set, discard
1171 */
1172 if (hdr->tci_an & MACSEC_TCI_C ||
1173 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1174 u64_stats_update_begin(&rxsc_stats->syncp);
1175 rxsc_stats->stats.InPktsNotUsingSA++;
1176 u64_stats_update_end(&rxsc_stats->syncp);
1177 goto drop_nosa;
1178 }
1179
1180 /* not Strict, the frame (with the SecTAG and ICV
1181 * removed) is delivered to the Controlled Port.
1182 */
1183 u64_stats_update_begin(&rxsc_stats->syncp);
1184 rxsc_stats->stats.InPktsUnusedSA++;
1185 u64_stats_update_end(&rxsc_stats->syncp);
1186 goto deliver;
1187 }
1188
1189 /* First, PN check to avoid decrypting obviously wrong packets */
1190 pn = ntohl(hdr->packet_number);
1191 if (secy->replay_protect) {
1192 bool late;
1193
1194 spin_lock(&rx_sa->lock);
1195 late = rx_sa->next_pn >= secy->replay_window &&
1196 pn < (rx_sa->next_pn - secy->replay_window);
1197 spin_unlock(&rx_sa->lock);
1198
1199 if (late) {
1200 u64_stats_update_begin(&rxsc_stats->syncp);
1201 rxsc_stats->stats.InPktsLate++;
1202 u64_stats_update_end(&rxsc_stats->syncp);
1203 goto drop;
1204 }
1205 }
1206
e3a3b626
BG
1207 macsec_skb_cb(skb)->rx_sa = rx_sa;
1208
c09440f7
SD
1209 /* Disabled && !changed text => skip validation */
1210 if (hdr->tci_an & MACSEC_TCI_C ||
1211 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1212 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1213
c3b7d0bd
SD
1214 if (IS_ERR(skb)) {
1215 /* the decrypt callback needs the reference */
c78ebe1d 1216 if (PTR_ERR(skb) != -EINPROGRESS) {
c3b7d0bd 1217 macsec_rxsa_put(rx_sa);
c78ebe1d
SD
1218 macsec_rxsc_put(rx_sc);
1219 }
c09440f7
SD
1220 rcu_read_unlock();
1221 *pskb = NULL;
1222 return RX_HANDLER_CONSUMED;
1223 }
1224
1225 if (!macsec_post_decrypt(skb, secy, pn))
1226 goto drop;
1227
1228deliver:
1229 macsec_finalize_skb(skb, secy->icv_len,
1230 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1231 macsec_reset_skb(skb, secy->netdev);
1232
497f358a
SD
1233 if (rx_sa)
1234 macsec_rxsa_put(rx_sa);
c78ebe1d 1235 macsec_rxsc_put(rx_sc);
5491e7c6
PA
1236
1237 ret = gro_cells_receive(&macsec->gro_cells, skb);
1238 if (ret == NET_RX_SUCCESS)
1239 count_rx(dev, skb->len);
1240 else
1241 macsec->secy.netdev->stats.rx_dropped++;
c09440f7
SD
1242
1243 rcu_read_unlock();
1244
5491e7c6
PA
1245 *pskb = NULL;
1246 return RX_HANDLER_CONSUMED;
c09440f7
SD
1247
1248drop:
1249 macsec_rxsa_put(rx_sa);
1250drop_nosa:
c78ebe1d 1251 macsec_rxsc_put(rx_sc);
c09440f7
SD
1252 rcu_read_unlock();
1253drop_direct:
1254 kfree_skb(skb);
1255 *pskb = NULL;
1256 return RX_HANDLER_CONSUMED;
1257
1258nosci:
1259 /* 10.6.1 if the SC is not found */
1260 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1261 if (!cbit)
1262 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1263 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1264
1265 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1266 struct sk_buff *nskb;
c09440f7
SD
1267
1268 secy_stats = this_cpu_ptr(macsec->stats);
1269
1270 /* If validateFrames is Strict or the C bit in the
1271 * SecTAG is set, discard
1272 */
1273 if (cbit ||
1274 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1275 u64_stats_update_begin(&secy_stats->syncp);
1276 secy_stats->stats.InPktsNoSCI++;
1277 u64_stats_update_end(&secy_stats->syncp);
1278 continue;
1279 }
1280
1281 /* not strict, the frame (with the SecTAG and ICV
1282 * removed) is delivered to the Controlled Port.
1283 */
1284 nskb = skb_clone(skb, GFP_ATOMIC);
1285 if (!nskb)
1286 break;
1287
1288 macsec_reset_skb(nskb, macsec->secy.netdev);
1289
1290 ret = netif_rx(nskb);
1291 if (ret == NET_RX_SUCCESS) {
1292 u64_stats_update_begin(&secy_stats->syncp);
1293 secy_stats->stats.InPktsUnknownSCI++;
1294 u64_stats_update_end(&secy_stats->syncp);
1295 } else {
1296 macsec->secy.netdev->stats.rx_dropped++;
1297 }
1298 }
1299
1300 rcu_read_unlock();
1301 *pskb = skb;
1302 return RX_HANDLER_PASS;
1303}
1304
1305static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1306{
1307 struct crypto_aead *tfm;
1308 int ret;
1309
6052f7fb 1310 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
34aedfee
DC
1311
1312 if (IS_ERR(tfm))
1313 return tfm;
c09440f7
SD
1314
1315 ret = crypto_aead_setkey(tfm, key, key_len);
34aedfee
DC
1316 if (ret < 0)
1317 goto fail;
c09440f7
SD
1318
1319 ret = crypto_aead_setauthsize(tfm, icv_len);
34aedfee
DC
1320 if (ret < 0)
1321 goto fail;
c09440f7
SD
1322
1323 return tfm;
34aedfee
DC
1324fail:
1325 crypto_free_aead(tfm);
1326 return ERR_PTR(ret);
c09440f7
SD
1327}
1328
1329static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1330 int icv_len)
1331{
1332 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1333 if (!rx_sa->stats)
34aedfee 1334 return -ENOMEM;
c09440f7
SD
1335
1336 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
34aedfee 1337 if (IS_ERR(rx_sa->key.tfm)) {
c09440f7 1338 free_percpu(rx_sa->stats);
34aedfee 1339 return PTR_ERR(rx_sa->key.tfm);
c09440f7
SD
1340 }
1341
1342 rx_sa->active = false;
1343 rx_sa->next_pn = 1;
e187246f 1344 refcount_set(&rx_sa->refcnt, 1);
c09440f7
SD
1345 spin_lock_init(&rx_sa->lock);
1346
1347 return 0;
1348}
1349
1350static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1351{
1352 rx_sa->active = false;
1353
1354 macsec_rxsa_put(rx_sa);
1355}
1356
1357static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1358{
1359 int i;
1360
1361 for (i = 0; i < MACSEC_NUM_AN; i++) {
1362 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1363
1364 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1365 if (sa)
1366 clear_rx_sa(sa);
1367 }
1368
1369 macsec_rxsc_put(rx_sc);
1370}
1371
1372static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1373{
1374 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1375
1376 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1377 rx_sc;
1378 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1379 if (rx_sc->sci == sci) {
1380 if (rx_sc->active)
1381 secy->n_rx_sc--;
1382 rcu_assign_pointer(*rx_scp, rx_sc->next);
1383 return rx_sc;
1384 }
1385 }
1386
1387 return NULL;
1388}
1389
1390static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1391{
1392 struct macsec_rx_sc *rx_sc;
1393 struct macsec_dev *macsec;
1394 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1395 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1396 struct macsec_secy *secy;
1397
1398 list_for_each_entry(macsec, &rxd->secys, secys) {
1399 if (find_rx_sc_rtnl(&macsec->secy, sci))
1400 return ERR_PTR(-EEXIST);
1401 }
1402
1403 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1404 if (!rx_sc)
1405 return ERR_PTR(-ENOMEM);
1406
1407 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1408 if (!rx_sc->stats) {
1409 kfree(rx_sc);
1410 return ERR_PTR(-ENOMEM);
1411 }
1412
1413 rx_sc->sci = sci;
1414 rx_sc->active = true;
8676d76f 1415 refcount_set(&rx_sc->refcnt, 1);
c09440f7
SD
1416
1417 secy = &macsec_priv(dev)->secy;
1418 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1419 rcu_assign_pointer(secy->rx_sc, rx_sc);
1420
1421 if (rx_sc->active)
1422 secy->n_rx_sc++;
1423
1424 return rx_sc;
1425}
1426
1427static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1428 int icv_len)
1429{
1430 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1431 if (!tx_sa->stats)
34aedfee 1432 return -ENOMEM;
c09440f7
SD
1433
1434 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
34aedfee 1435 if (IS_ERR(tx_sa->key.tfm)) {
c09440f7 1436 free_percpu(tx_sa->stats);
34aedfee 1437 return PTR_ERR(tx_sa->key.tfm);
c09440f7
SD
1438 }
1439
1440 tx_sa->active = false;
28206cdb 1441 refcount_set(&tx_sa->refcnt, 1);
c09440f7
SD
1442 spin_lock_init(&tx_sa->lock);
1443
1444 return 0;
1445}
1446
1447static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1448{
1449 tx_sa->active = false;
1450
1451 macsec_txsa_put(tx_sa);
1452}
1453
489111e5 1454static struct genl_family macsec_fam;
c09440f7
SD
1455
1456static struct net_device *get_dev_from_nl(struct net *net,
1457 struct nlattr **attrs)
1458{
1459 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1460 struct net_device *dev;
1461
1462 dev = __dev_get_by_index(net, ifindex);
1463 if (!dev)
1464 return ERR_PTR(-ENODEV);
1465
1466 if (!netif_is_macsec(dev))
1467 return ERR_PTR(-ENODEV);
1468
1469 return dev;
1470}
1471
1472static sci_t nla_get_sci(const struct nlattr *nla)
1473{
1474 return (__force sci_t)nla_get_u64(nla);
1475}
1476
f60d94c0
ND
1477static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1478 int padattr)
c09440f7 1479{
f60d94c0 1480 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
c09440f7
SD
1481}
1482
1483static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1484 struct nlattr **attrs,
1485 struct nlattr **tb_sa,
1486 struct net_device **devp,
1487 struct macsec_secy **secyp,
1488 struct macsec_tx_sc **scp,
1489 u8 *assoc_num)
1490{
1491 struct net_device *dev;
1492 struct macsec_secy *secy;
1493 struct macsec_tx_sc *tx_sc;
1494 struct macsec_tx_sa *tx_sa;
1495
1496 if (!tb_sa[MACSEC_SA_ATTR_AN])
1497 return ERR_PTR(-EINVAL);
1498
1499 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1500
1501 dev = get_dev_from_nl(net, attrs);
1502 if (IS_ERR(dev))
1503 return ERR_CAST(dev);
1504
1505 if (*assoc_num >= MACSEC_NUM_AN)
1506 return ERR_PTR(-EINVAL);
1507
1508 secy = &macsec_priv(dev)->secy;
1509 tx_sc = &secy->tx_sc;
1510
1511 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1512 if (!tx_sa)
1513 return ERR_PTR(-ENODEV);
1514
1515 *devp = dev;
1516 *scp = tx_sc;
1517 *secyp = secy;
1518 return tx_sa;
1519}
1520
1521static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1522 struct nlattr **attrs,
1523 struct nlattr **tb_rxsc,
1524 struct net_device **devp,
1525 struct macsec_secy **secyp)
1526{
1527 struct net_device *dev;
1528 struct macsec_secy *secy;
1529 struct macsec_rx_sc *rx_sc;
1530 sci_t sci;
1531
1532 dev = get_dev_from_nl(net, attrs);
1533 if (IS_ERR(dev))
1534 return ERR_CAST(dev);
1535
1536 secy = &macsec_priv(dev)->secy;
1537
1538 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1539 return ERR_PTR(-EINVAL);
1540
1541 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1542 rx_sc = find_rx_sc_rtnl(secy, sci);
1543 if (!rx_sc)
1544 return ERR_PTR(-ENODEV);
1545
1546 *secyp = secy;
1547 *devp = dev;
1548
1549 return rx_sc;
1550}
1551
1552static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1553 struct nlattr **attrs,
1554 struct nlattr **tb_rxsc,
1555 struct nlattr **tb_sa,
1556 struct net_device **devp,
1557 struct macsec_secy **secyp,
1558 struct macsec_rx_sc **scp,
1559 u8 *assoc_num)
1560{
1561 struct macsec_rx_sc *rx_sc;
1562 struct macsec_rx_sa *rx_sa;
1563
1564 if (!tb_sa[MACSEC_SA_ATTR_AN])
1565 return ERR_PTR(-EINVAL);
1566
1567 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1568 if (*assoc_num >= MACSEC_NUM_AN)
1569 return ERR_PTR(-EINVAL);
1570
1571 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1572 if (IS_ERR(rx_sc))
1573 return ERR_CAST(rx_sc);
1574
1575 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1576 if (!rx_sa)
1577 return ERR_PTR(-ENODEV);
1578
1579 *scp = rx_sc;
1580 return rx_sa;
1581}
1582
c09440f7
SD
1583static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1584 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1585 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1586 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1587};
1588
1589static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1590 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1591 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1592};
1593
1594static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1595 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1596 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1597 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
8acca6ac
SD
1598 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1599 .len = MACSEC_KEYID_LEN, },
c09440f7 1600 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
e8660ded 1601 .len = MACSEC_MAX_KEY_LEN, },
c09440f7
SD
1602};
1603
1604static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1605{
1606 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1607 return -EINVAL;
1608
8cb08174 1609 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
c09440f7
SD
1610 return -EINVAL;
1611
1612 return 0;
1613}
1614
1615static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1616{
1617 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1618 return -EINVAL;
1619
8cb08174 1620 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
c09440f7
SD
1621 return -EINVAL;
1622
1623 return 0;
1624}
1625
1626static bool validate_add_rxsa(struct nlattr **attrs)
1627{
1628 if (!attrs[MACSEC_SA_ATTR_AN] ||
1629 !attrs[MACSEC_SA_ATTR_KEY] ||
1630 !attrs[MACSEC_SA_ATTR_KEYID])
1631 return false;
1632
1633 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1634 return false;
1635
1636 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1637 return false;
1638
1639 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1640 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1641 return false;
1642 }
1643
8acca6ac
SD
1644 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1645 return false;
1646
c09440f7
SD
1647 return true;
1648}
1649
1650static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1651{
1652 struct net_device *dev;
1653 struct nlattr **attrs = info->attrs;
1654 struct macsec_secy *secy;
1655 struct macsec_rx_sc *rx_sc;
1656 struct macsec_rx_sa *rx_sa;
1657 unsigned char assoc_num;
1658 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1659 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
34aedfee 1660 int err;
c09440f7
SD
1661
1662 if (!attrs[MACSEC_ATTR_IFINDEX])
1663 return -EINVAL;
1664
1665 if (parse_sa_config(attrs, tb_sa))
1666 return -EINVAL;
1667
1668 if (parse_rxsc_config(attrs, tb_rxsc))
1669 return -EINVAL;
1670
1671 if (!validate_add_rxsa(tb_sa))
1672 return -EINVAL;
1673
1674 rtnl_lock();
1675 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
36b232c8 1676 if (IS_ERR(rx_sc)) {
c09440f7
SD
1677 rtnl_unlock();
1678 return PTR_ERR(rx_sc);
1679 }
1680
1681 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1682
1683 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1684 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1685 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1686 rtnl_unlock();
1687 return -EINVAL;
1688 }
1689
1690 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1691 if (rx_sa) {
1692 rtnl_unlock();
1693 return -EBUSY;
1694 }
1695
1696 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
34aedfee 1697 if (!rx_sa) {
c09440f7
SD
1698 rtnl_unlock();
1699 return -ENOMEM;
1700 }
1701
34aedfee
DC
1702 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1703 secy->key_len, secy->icv_len);
1704 if (err < 0) {
1705 kfree(rx_sa);
1706 rtnl_unlock();
1707 return err;
1708 }
1709
c09440f7
SD
1710 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1711 spin_lock_bh(&rx_sa->lock);
1712 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1713 spin_unlock_bh(&rx_sa->lock);
1714 }
1715
1716 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1717 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1718
1968a0b8 1719 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
c09440f7
SD
1720 rx_sa->sc = rx_sc;
1721 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1722
1723 rtnl_unlock();
1724
1725 return 0;
1726}
1727
1728static bool validate_add_rxsc(struct nlattr **attrs)
1729{
1730 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1731 return false;
1732
1733 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1734 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1735 return false;
1736 }
1737
1738 return true;
1739}
1740
1741static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1742{
1743 struct net_device *dev;
1744 sci_t sci = MACSEC_UNDEF_SCI;
1745 struct nlattr **attrs = info->attrs;
1746 struct macsec_rx_sc *rx_sc;
1747 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1748
1749 if (!attrs[MACSEC_ATTR_IFINDEX])
1750 return -EINVAL;
1751
1752 if (parse_rxsc_config(attrs, tb_rxsc))
1753 return -EINVAL;
1754
1755 if (!validate_add_rxsc(tb_rxsc))
1756 return -EINVAL;
1757
1758 rtnl_lock();
1759 dev = get_dev_from_nl(genl_info_net(info), attrs);
1760 if (IS_ERR(dev)) {
1761 rtnl_unlock();
1762 return PTR_ERR(dev);
1763 }
1764
1765 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1766
1767 rx_sc = create_rx_sc(dev, sci);
1768 if (IS_ERR(rx_sc)) {
1769 rtnl_unlock();
1770 return PTR_ERR(rx_sc);
1771 }
1772
1773 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1774 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1775
1776 rtnl_unlock();
1777
1778 return 0;
1779}
1780
1781static bool validate_add_txsa(struct nlattr **attrs)
1782{
1783 if (!attrs[MACSEC_SA_ATTR_AN] ||
1784 !attrs[MACSEC_SA_ATTR_PN] ||
1785 !attrs[MACSEC_SA_ATTR_KEY] ||
1786 !attrs[MACSEC_SA_ATTR_KEYID])
1787 return false;
1788
1789 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1790 return false;
1791
1792 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1793 return false;
1794
1795 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1796 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1797 return false;
1798 }
1799
8acca6ac
SD
1800 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1801 return false;
1802
c09440f7
SD
1803 return true;
1804}
1805
1806static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1807{
1808 struct net_device *dev;
1809 struct nlattr **attrs = info->attrs;
1810 struct macsec_secy *secy;
1811 struct macsec_tx_sc *tx_sc;
1812 struct macsec_tx_sa *tx_sa;
1813 unsigned char assoc_num;
1814 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
34aedfee 1815 int err;
c09440f7
SD
1816
1817 if (!attrs[MACSEC_ATTR_IFINDEX])
1818 return -EINVAL;
1819
1820 if (parse_sa_config(attrs, tb_sa))
1821 return -EINVAL;
1822
1823 if (!validate_add_txsa(tb_sa))
1824 return -EINVAL;
1825
1826 rtnl_lock();
1827 dev = get_dev_from_nl(genl_info_net(info), attrs);
1828 if (IS_ERR(dev)) {
1829 rtnl_unlock();
1830 return PTR_ERR(dev);
1831 }
1832
1833 secy = &macsec_priv(dev)->secy;
1834 tx_sc = &secy->tx_sc;
1835
1836 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1837
1838 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1839 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1840 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1841 rtnl_unlock();
1842 return -EINVAL;
1843 }
1844
1845 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1846 if (tx_sa) {
1847 rtnl_unlock();
1848 return -EBUSY;
1849 }
1850
1851 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
34aedfee 1852 if (!tx_sa) {
c09440f7
SD
1853 rtnl_unlock();
1854 return -ENOMEM;
1855 }
1856
34aedfee
DC
1857 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1858 secy->key_len, secy->icv_len);
1859 if (err < 0) {
1860 kfree(tx_sa);
1861 rtnl_unlock();
1862 return err;
1863 }
1864
1968a0b8 1865 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
c09440f7
SD
1866
1867 spin_lock_bh(&tx_sa->lock);
1868 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1869 spin_unlock_bh(&tx_sa->lock);
1870
1871 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1872 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1873
1874 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1875 secy->operational = true;
1876
1877 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1878
1879 rtnl_unlock();
1880
1881 return 0;
1882}
1883
1884static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1885{
1886 struct nlattr **attrs = info->attrs;
1887 struct net_device *dev;
1888 struct macsec_secy *secy;
1889 struct macsec_rx_sc *rx_sc;
1890 struct macsec_rx_sa *rx_sa;
1891 u8 assoc_num;
1892 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1893 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1894
1895 if (!attrs[MACSEC_ATTR_IFINDEX])
1896 return -EINVAL;
1897
1898 if (parse_sa_config(attrs, tb_sa))
1899 return -EINVAL;
1900
1901 if (parse_rxsc_config(attrs, tb_rxsc))
1902 return -EINVAL;
1903
1904 rtnl_lock();
1905 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1906 &dev, &secy, &rx_sc, &assoc_num);
1907 if (IS_ERR(rx_sa)) {
1908 rtnl_unlock();
1909 return PTR_ERR(rx_sa);
1910 }
1911
1912 if (rx_sa->active) {
1913 rtnl_unlock();
1914 return -EBUSY;
1915 }
1916
1917 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1918 clear_rx_sa(rx_sa);
1919
1920 rtnl_unlock();
1921
1922 return 0;
1923}
1924
1925static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1926{
1927 struct nlattr **attrs = info->attrs;
1928 struct net_device *dev;
1929 struct macsec_secy *secy;
1930 struct macsec_rx_sc *rx_sc;
1931 sci_t sci;
1932 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1933
1934 if (!attrs[MACSEC_ATTR_IFINDEX])
1935 return -EINVAL;
1936
1937 if (parse_rxsc_config(attrs, tb_rxsc))
1938 return -EINVAL;
1939
1940 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1941 return -EINVAL;
1942
1943 rtnl_lock();
1944 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1945 if (IS_ERR(dev)) {
1946 rtnl_unlock();
1947 return PTR_ERR(dev);
1948 }
1949
1950 secy = &macsec_priv(dev)->secy;
1951 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1952
1953 rx_sc = del_rx_sc(secy, sci);
1954 if (!rx_sc) {
1955 rtnl_unlock();
1956 return -ENODEV;
1957 }
1958
1959 free_rx_sc(rx_sc);
1960 rtnl_unlock();
1961
1962 return 0;
1963}
1964
1965static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1966{
1967 struct nlattr **attrs = info->attrs;
1968 struct net_device *dev;
1969 struct macsec_secy *secy;
1970 struct macsec_tx_sc *tx_sc;
1971 struct macsec_tx_sa *tx_sa;
1972 u8 assoc_num;
1973 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1974
1975 if (!attrs[MACSEC_ATTR_IFINDEX])
1976 return -EINVAL;
1977
1978 if (parse_sa_config(attrs, tb_sa))
1979 return -EINVAL;
1980
1981 rtnl_lock();
1982 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
1983 &dev, &secy, &tx_sc, &assoc_num);
1984 if (IS_ERR(tx_sa)) {
1985 rtnl_unlock();
1986 return PTR_ERR(tx_sa);
1987 }
1988
1989 if (tx_sa->active) {
1990 rtnl_unlock();
1991 return -EBUSY;
1992 }
1993
1994 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
1995 clear_tx_sa(tx_sa);
1996
1997 rtnl_unlock();
1998
1999 return 0;
2000}
2001
2002static bool validate_upd_sa(struct nlattr **attrs)
2003{
2004 if (!attrs[MACSEC_SA_ATTR_AN] ||
2005 attrs[MACSEC_SA_ATTR_KEY] ||
2006 attrs[MACSEC_SA_ATTR_KEYID])
2007 return false;
2008
2009 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2010 return false;
2011
2012 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2013 return false;
2014
2015 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2016 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2017 return false;
2018 }
2019
2020 return true;
2021}
2022
2023static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2024{
2025 struct nlattr **attrs = info->attrs;
2026 struct net_device *dev;
2027 struct macsec_secy *secy;
2028 struct macsec_tx_sc *tx_sc;
2029 struct macsec_tx_sa *tx_sa;
2030 u8 assoc_num;
2031 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2032
2033 if (!attrs[MACSEC_ATTR_IFINDEX])
2034 return -EINVAL;
2035
2036 if (parse_sa_config(attrs, tb_sa))
2037 return -EINVAL;
2038
2039 if (!validate_upd_sa(tb_sa))
2040 return -EINVAL;
2041
2042 rtnl_lock();
2043 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2044 &dev, &secy, &tx_sc, &assoc_num);
2045 if (IS_ERR(tx_sa)) {
2046 rtnl_unlock();
2047 return PTR_ERR(tx_sa);
2048 }
2049
2050 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2051 spin_lock_bh(&tx_sa->lock);
2052 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2053 spin_unlock_bh(&tx_sa->lock);
2054 }
2055
2056 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2057 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2058
2059 if (assoc_num == tx_sc->encoding_sa)
2060 secy->operational = tx_sa->active;
2061
2062 rtnl_unlock();
2063
2064 return 0;
2065}
2066
2067static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2068{
2069 struct nlattr **attrs = info->attrs;
2070 struct net_device *dev;
2071 struct macsec_secy *secy;
2072 struct macsec_rx_sc *rx_sc;
2073 struct macsec_rx_sa *rx_sa;
2074 u8 assoc_num;
2075 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2076 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2077
2078 if (!attrs[MACSEC_ATTR_IFINDEX])
2079 return -EINVAL;
2080
2081 if (parse_rxsc_config(attrs, tb_rxsc))
2082 return -EINVAL;
2083
2084 if (parse_sa_config(attrs, tb_sa))
2085 return -EINVAL;
2086
2087 if (!validate_upd_sa(tb_sa))
2088 return -EINVAL;
2089
2090 rtnl_lock();
2091 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2092 &dev, &secy, &rx_sc, &assoc_num);
2093 if (IS_ERR(rx_sa)) {
2094 rtnl_unlock();
2095 return PTR_ERR(rx_sa);
2096 }
2097
2098 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2099 spin_lock_bh(&rx_sa->lock);
2100 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2101 spin_unlock_bh(&rx_sa->lock);
2102 }
2103
2104 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2105 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2106
2107 rtnl_unlock();
2108 return 0;
2109}
2110
2111static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2112{
2113 struct nlattr **attrs = info->attrs;
2114 struct net_device *dev;
2115 struct macsec_secy *secy;
2116 struct macsec_rx_sc *rx_sc;
2117 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2118
2119 if (!attrs[MACSEC_ATTR_IFINDEX])
2120 return -EINVAL;
2121
2122 if (parse_rxsc_config(attrs, tb_rxsc))
2123 return -EINVAL;
2124
2125 if (!validate_add_rxsc(tb_rxsc))
2126 return -EINVAL;
2127
2128 rtnl_lock();
2129 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2130 if (IS_ERR(rx_sc)) {
2131 rtnl_unlock();
2132 return PTR_ERR(rx_sc);
2133 }
2134
2135 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2136 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2137
2138 if (rx_sc->active != new)
2139 secy->n_rx_sc += new ? 1 : -1;
2140
2141 rx_sc->active = new;
2142 }
2143
2144 rtnl_unlock();
2145
2146 return 0;
2147}
2148
2149static int copy_tx_sa_stats(struct sk_buff *skb,
7979472b 2150 struct macsec_tx_sa_stats __percpu *pstats)
c09440f7
SD
2151{
2152 struct macsec_tx_sa_stats sum = {0, };
2153 int cpu;
2154
2155 for_each_possible_cpu(cpu) {
2156 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2157
2158 sum.OutPktsProtected += stats->OutPktsProtected;
2159 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2160 }
2161
2162 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2163 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2164 return -EMSGSIZE;
2165
2166 return 0;
2167}
2168
e1427237
FW
2169static noinline_for_stack int
2170copy_rx_sa_stats(struct sk_buff *skb,
2171 struct macsec_rx_sa_stats __percpu *pstats)
c09440f7
SD
2172{
2173 struct macsec_rx_sa_stats sum = {0, };
2174 int cpu;
2175
2176 for_each_possible_cpu(cpu) {
2177 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2178
2179 sum.InPktsOK += stats->InPktsOK;
2180 sum.InPktsInvalid += stats->InPktsInvalid;
2181 sum.InPktsNotValid += stats->InPktsNotValid;
2182 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2183 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2184 }
2185
2186 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2187 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2188 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2189 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2190 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2191 return -EMSGSIZE;
2192
2193 return 0;
2194}
2195
e1427237
FW
2196static noinline_for_stack int
2197copy_rx_sc_stats(struct sk_buff *skb, struct pcpu_rx_sc_stats __percpu *pstats)
c09440f7
SD
2198{
2199 struct macsec_rx_sc_stats sum = {0, };
2200 int cpu;
2201
2202 for_each_possible_cpu(cpu) {
2203 const struct pcpu_rx_sc_stats *stats;
2204 struct macsec_rx_sc_stats tmp;
2205 unsigned int start;
2206
2207 stats = per_cpu_ptr(pstats, cpu);
2208 do {
2209 start = u64_stats_fetch_begin_irq(&stats->syncp);
2210 memcpy(&tmp, &stats->stats, sizeof(tmp));
2211 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2212
2213 sum.InOctetsValidated += tmp.InOctetsValidated;
2214 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2215 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2216 sum.InPktsDelayed += tmp.InPktsDelayed;
2217 sum.InPktsOK += tmp.InPktsOK;
2218 sum.InPktsInvalid += tmp.InPktsInvalid;
2219 sum.InPktsLate += tmp.InPktsLate;
2220 sum.InPktsNotValid += tmp.InPktsNotValid;
2221 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2222 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2223 }
2224
f60d94c0
ND
2225 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2226 sum.InOctetsValidated,
2227 MACSEC_RXSC_STATS_ATTR_PAD) ||
2228 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2229 sum.InOctetsDecrypted,
2230 MACSEC_RXSC_STATS_ATTR_PAD) ||
2231 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2232 sum.InPktsUnchecked,
2233 MACSEC_RXSC_STATS_ATTR_PAD) ||
2234 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2235 sum.InPktsDelayed,
2236 MACSEC_RXSC_STATS_ATTR_PAD) ||
2237 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2238 sum.InPktsOK,
2239 MACSEC_RXSC_STATS_ATTR_PAD) ||
2240 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2241 sum.InPktsInvalid,
2242 MACSEC_RXSC_STATS_ATTR_PAD) ||
2243 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2244 sum.InPktsLate,
2245 MACSEC_RXSC_STATS_ATTR_PAD) ||
2246 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2247 sum.InPktsNotValid,
2248 MACSEC_RXSC_STATS_ATTR_PAD) ||
2249 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2250 sum.InPktsNotUsingSA,
2251 MACSEC_RXSC_STATS_ATTR_PAD) ||
2252 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2253 sum.InPktsUnusedSA,
2254 MACSEC_RXSC_STATS_ATTR_PAD))
c09440f7
SD
2255 return -EMSGSIZE;
2256
2257 return 0;
2258}
2259
e1427237
FW
2260static noinline_for_stack int
2261copy_tx_sc_stats(struct sk_buff *skb, struct pcpu_tx_sc_stats __percpu *pstats)
c09440f7
SD
2262{
2263 struct macsec_tx_sc_stats sum = {0, };
2264 int cpu;
2265
2266 for_each_possible_cpu(cpu) {
2267 const struct pcpu_tx_sc_stats *stats;
2268 struct macsec_tx_sc_stats tmp;
2269 unsigned int start;
2270
2271 stats = per_cpu_ptr(pstats, cpu);
2272 do {
2273 start = u64_stats_fetch_begin_irq(&stats->syncp);
2274 memcpy(&tmp, &stats->stats, sizeof(tmp));
2275 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2276
2277 sum.OutPktsProtected += tmp.OutPktsProtected;
2278 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2279 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2280 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2281 }
2282
f60d94c0
ND
2283 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2284 sum.OutPktsProtected,
2285 MACSEC_TXSC_STATS_ATTR_PAD) ||
2286 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2287 sum.OutPktsEncrypted,
2288 MACSEC_TXSC_STATS_ATTR_PAD) ||
2289 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2290 sum.OutOctetsProtected,
2291 MACSEC_TXSC_STATS_ATTR_PAD) ||
2292 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2293 sum.OutOctetsEncrypted,
2294 MACSEC_TXSC_STATS_ATTR_PAD))
c09440f7
SD
2295 return -EMSGSIZE;
2296
2297 return 0;
2298}
2299
e1427237
FW
2300static noinline_for_stack int
2301copy_secy_stats(struct sk_buff *skb, struct pcpu_secy_stats __percpu *pstats)
c09440f7
SD
2302{
2303 struct macsec_dev_stats sum = {0, };
2304 int cpu;
2305
2306 for_each_possible_cpu(cpu) {
2307 const struct pcpu_secy_stats *stats;
2308 struct macsec_dev_stats tmp;
2309 unsigned int start;
2310
2311 stats = per_cpu_ptr(pstats, cpu);
2312 do {
2313 start = u64_stats_fetch_begin_irq(&stats->syncp);
2314 memcpy(&tmp, &stats->stats, sizeof(tmp));
2315 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2316
2317 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2318 sum.InPktsUntagged += tmp.InPktsUntagged;
2319 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2320 sum.InPktsNoTag += tmp.InPktsNoTag;
2321 sum.InPktsBadTag += tmp.InPktsBadTag;
2322 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2323 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2324 sum.InPktsOverrun += tmp.InPktsOverrun;
2325 }
2326
f60d94c0
ND
2327 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2328 sum.OutPktsUntagged,
2329 MACSEC_SECY_STATS_ATTR_PAD) ||
2330 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2331 sum.InPktsUntagged,
2332 MACSEC_SECY_STATS_ATTR_PAD) ||
2333 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2334 sum.OutPktsTooLong,
2335 MACSEC_SECY_STATS_ATTR_PAD) ||
2336 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2337 sum.InPktsNoTag,
2338 MACSEC_SECY_STATS_ATTR_PAD) ||
2339 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2340 sum.InPktsBadTag,
2341 MACSEC_SECY_STATS_ATTR_PAD) ||
2342 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2343 sum.InPktsUnknownSCI,
2344 MACSEC_SECY_STATS_ATTR_PAD) ||
2345 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2346 sum.InPktsNoSCI,
2347 MACSEC_SECY_STATS_ATTR_PAD) ||
2348 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2349 sum.InPktsOverrun,
2350 MACSEC_SECY_STATS_ATTR_PAD))
c09440f7
SD
2351 return -EMSGSIZE;
2352
2353 return 0;
2354}
2355
2356static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2357{
2358 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
ae0be8de
MK
2359 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
2360 MACSEC_ATTR_SECY);
ccfdec90 2361 u64 csid;
c09440f7
SD
2362
2363 if (!secy_nest)
2364 return 1;
2365
ccfdec90
FW
2366 switch (secy->key_len) {
2367 case MACSEC_GCM_AES_128_SAK_LEN:
e8660ded 2368 csid = MACSEC_DEFAULT_CIPHER_ID;
ccfdec90
FW
2369 break;
2370 case MACSEC_GCM_AES_256_SAK_LEN:
2371 csid = MACSEC_CIPHER_ID_GCM_AES_256;
2372 break;
2373 default:
2374 goto cancel;
2375 }
2376
f60d94c0
ND
2377 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2378 MACSEC_SECY_ATTR_PAD) ||
2379 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
ccfdec90 2380 csid, MACSEC_SECY_ATTR_PAD) ||
c09440f7
SD
2381 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2382 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2383 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2384 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2385 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2386 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2387 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2388 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2389 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2390 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2391 goto cancel;
2392
2393 if (secy->replay_protect) {
2394 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2395 goto cancel;
2396 }
2397
2398 nla_nest_end(skb, secy_nest);
2399 return 0;
2400
2401cancel:
2402 nla_nest_cancel(skb, secy_nest);
2403 return 1;
2404}
2405
e1427237
FW
2406static noinline_for_stack int
2407dump_secy(struct macsec_secy *secy, struct net_device *dev,
2408 struct sk_buff *skb, struct netlink_callback *cb)
c09440f7
SD
2409{
2410 struct macsec_rx_sc *rx_sc;
2411 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2412 struct nlattr *txsa_list, *rxsc_list;
2413 int i, j;
2414 void *hdr;
2415 struct nlattr *attr;
2416
2417 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2418 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2419 if (!hdr)
2420 return -EMSGSIZE;
2421
0a833c29 2422 genl_dump_check_consistent(cb, hdr);
c09440f7
SD
2423
2424 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2425 goto nla_put_failure;
2426
2427 if (nla_put_secy(secy, skb))
2428 goto nla_put_failure;
2429
ae0be8de 2430 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
c09440f7
SD
2431 if (!attr)
2432 goto nla_put_failure;
2433 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2434 nla_nest_cancel(skb, attr);
2435 goto nla_put_failure;
2436 }
2437 nla_nest_end(skb, attr);
2438
ae0be8de 2439 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
c09440f7
SD
2440 if (!attr)
2441 goto nla_put_failure;
2442 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2443 nla_nest_cancel(skb, attr);
2444 goto nla_put_failure;
2445 }
2446 nla_nest_end(skb, attr);
2447
ae0be8de 2448 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
c09440f7
SD
2449 if (!txsa_list)
2450 goto nla_put_failure;
2451 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2452 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2453 struct nlattr *txsa_nest;
2454
2455 if (!tx_sa)
2456 continue;
2457
ae0be8de 2458 txsa_nest = nla_nest_start_noflag(skb, j++);
c09440f7
SD
2459 if (!txsa_nest) {
2460 nla_nest_cancel(skb, txsa_list);
2461 goto nla_put_failure;
2462 }
2463
2464 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2465 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
8acca6ac 2466 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
c09440f7
SD
2467 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2468 nla_nest_cancel(skb, txsa_nest);
2469 nla_nest_cancel(skb, txsa_list);
2470 goto nla_put_failure;
2471 }
2472
ae0be8de 2473 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
c09440f7
SD
2474 if (!attr) {
2475 nla_nest_cancel(skb, txsa_nest);
2476 nla_nest_cancel(skb, txsa_list);
2477 goto nla_put_failure;
2478 }
2479 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2480 nla_nest_cancel(skb, attr);
2481 nla_nest_cancel(skb, txsa_nest);
2482 nla_nest_cancel(skb, txsa_list);
2483 goto nla_put_failure;
2484 }
2485 nla_nest_end(skb, attr);
2486
2487 nla_nest_end(skb, txsa_nest);
2488 }
2489 nla_nest_end(skb, txsa_list);
2490
ae0be8de 2491 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
c09440f7
SD
2492 if (!rxsc_list)
2493 goto nla_put_failure;
2494
2495 j = 1;
2496 for_each_rxsc_rtnl(secy, rx_sc) {
2497 int k;
2498 struct nlattr *rxsa_list;
ae0be8de 2499 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
c09440f7
SD
2500
2501 if (!rxsc_nest) {
2502 nla_nest_cancel(skb, rxsc_list);
2503 goto nla_put_failure;
2504 }
2505
2506 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
f60d94c0
ND
2507 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2508 MACSEC_RXSC_ATTR_PAD)) {
c09440f7
SD
2509 nla_nest_cancel(skb, rxsc_nest);
2510 nla_nest_cancel(skb, rxsc_list);
2511 goto nla_put_failure;
2512 }
2513
ae0be8de 2514 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
c09440f7
SD
2515 if (!attr) {
2516 nla_nest_cancel(skb, rxsc_nest);
2517 nla_nest_cancel(skb, rxsc_list);
2518 goto nla_put_failure;
2519 }
2520 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2521 nla_nest_cancel(skb, attr);
2522 nla_nest_cancel(skb, rxsc_nest);
2523 nla_nest_cancel(skb, rxsc_list);
2524 goto nla_put_failure;
2525 }
2526 nla_nest_end(skb, attr);
2527
ae0be8de
MK
2528 rxsa_list = nla_nest_start_noflag(skb,
2529 MACSEC_RXSC_ATTR_SA_LIST);
c09440f7
SD
2530 if (!rxsa_list) {
2531 nla_nest_cancel(skb, rxsc_nest);
2532 nla_nest_cancel(skb, rxsc_list);
2533 goto nla_put_failure;
2534 }
2535
2536 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2537 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2538 struct nlattr *rxsa_nest;
2539
2540 if (!rx_sa)
2541 continue;
2542
ae0be8de 2543 rxsa_nest = nla_nest_start_noflag(skb, k++);
c09440f7
SD
2544 if (!rxsa_nest) {
2545 nla_nest_cancel(skb, rxsa_list);
2546 nla_nest_cancel(skb, rxsc_nest);
2547 nla_nest_cancel(skb, rxsc_list);
2548 goto nla_put_failure;
2549 }
2550
ae0be8de
MK
2551 attr = nla_nest_start_noflag(skb,
2552 MACSEC_SA_ATTR_STATS);
c09440f7
SD
2553 if (!attr) {
2554 nla_nest_cancel(skb, rxsa_list);
2555 nla_nest_cancel(skb, rxsc_nest);
2556 nla_nest_cancel(skb, rxsc_list);
2557 goto nla_put_failure;
2558 }
2559 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2560 nla_nest_cancel(skb, attr);
2561 nla_nest_cancel(skb, rxsa_list);
2562 nla_nest_cancel(skb, rxsc_nest);
2563 nla_nest_cancel(skb, rxsc_list);
2564 goto nla_put_failure;
2565 }
2566 nla_nest_end(skb, attr);
2567
2568 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2569 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
8acca6ac 2570 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
c09440f7
SD
2571 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2572 nla_nest_cancel(skb, rxsa_nest);
2573 nla_nest_cancel(skb, rxsc_nest);
2574 nla_nest_cancel(skb, rxsc_list);
2575 goto nla_put_failure;
2576 }
2577 nla_nest_end(skb, rxsa_nest);
2578 }
2579
2580 nla_nest_end(skb, rxsa_list);
2581 nla_nest_end(skb, rxsc_nest);
2582 }
2583
2584 nla_nest_end(skb, rxsc_list);
2585
c09440f7
SD
2586 genlmsg_end(skb, hdr);
2587
2588 return 0;
2589
2590nla_put_failure:
c09440f7
SD
2591 genlmsg_cancel(skb, hdr);
2592 return -EMSGSIZE;
2593}
2594
96cfc505
SD
2595static int macsec_generation = 1; /* protected by RTNL */
2596
c09440f7
SD
2597static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2598{
2599 struct net *net = sock_net(skb->sk);
2600 struct net_device *dev;
2601 int dev_idx, d;
2602
2603 dev_idx = cb->args[0];
2604
2605 d = 0;
c10c63ea 2606 rtnl_lock();
96cfc505
SD
2607
2608 cb->seq = macsec_generation;
2609
c09440f7
SD
2610 for_each_netdev(net, dev) {
2611 struct macsec_secy *secy;
2612
2613 if (d < dev_idx)
2614 goto next;
2615
2616 if (!netif_is_macsec(dev))
2617 goto next;
2618
2619 secy = &macsec_priv(dev)->secy;
2620 if (dump_secy(secy, dev, skb, cb) < 0)
2621 goto done;
2622next:
2623 d++;
2624 }
2625
2626done:
c10c63ea 2627 rtnl_unlock();
c09440f7
SD
2628 cb->args[0] = d;
2629 return skb->len;
2630}
2631
2632static const struct genl_ops macsec_genl_ops[] = {
2633 {
2634 .cmd = MACSEC_CMD_GET_TXSC,
ef6243ac 2635 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2636 .dumpit = macsec_dump_txsc,
c09440f7
SD
2637 },
2638 {
2639 .cmd = MACSEC_CMD_ADD_RXSC,
ef6243ac 2640 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2641 .doit = macsec_add_rxsc,
c09440f7
SD
2642 .flags = GENL_ADMIN_PERM,
2643 },
2644 {
2645 .cmd = MACSEC_CMD_DEL_RXSC,
ef6243ac 2646 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2647 .doit = macsec_del_rxsc,
c09440f7
SD
2648 .flags = GENL_ADMIN_PERM,
2649 },
2650 {
2651 .cmd = MACSEC_CMD_UPD_RXSC,
ef6243ac 2652 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2653 .doit = macsec_upd_rxsc,
c09440f7
SD
2654 .flags = GENL_ADMIN_PERM,
2655 },
2656 {
2657 .cmd = MACSEC_CMD_ADD_TXSA,
ef6243ac 2658 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2659 .doit = macsec_add_txsa,
c09440f7
SD
2660 .flags = GENL_ADMIN_PERM,
2661 },
2662 {
2663 .cmd = MACSEC_CMD_DEL_TXSA,
ef6243ac 2664 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2665 .doit = macsec_del_txsa,
c09440f7
SD
2666 .flags = GENL_ADMIN_PERM,
2667 },
2668 {
2669 .cmd = MACSEC_CMD_UPD_TXSA,
ef6243ac 2670 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2671 .doit = macsec_upd_txsa,
c09440f7
SD
2672 .flags = GENL_ADMIN_PERM,
2673 },
2674 {
2675 .cmd = MACSEC_CMD_ADD_RXSA,
ef6243ac 2676 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2677 .doit = macsec_add_rxsa,
c09440f7
SD
2678 .flags = GENL_ADMIN_PERM,
2679 },
2680 {
2681 .cmd = MACSEC_CMD_DEL_RXSA,
ef6243ac 2682 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2683 .doit = macsec_del_rxsa,
c09440f7
SD
2684 .flags = GENL_ADMIN_PERM,
2685 },
2686 {
2687 .cmd = MACSEC_CMD_UPD_RXSA,
ef6243ac 2688 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
c09440f7 2689 .doit = macsec_upd_rxsa,
c09440f7
SD
2690 .flags = GENL_ADMIN_PERM,
2691 },
2692};
2693
56989f6d 2694static struct genl_family macsec_fam __ro_after_init = {
489111e5
JB
2695 .name = MACSEC_GENL_NAME,
2696 .hdrsize = 0,
2697 .version = MACSEC_GENL_VERSION,
2698 .maxattr = MACSEC_ATTR_MAX,
3b0f31f2 2699 .policy = macsec_genl_policy,
489111e5
JB
2700 .netnsok = true,
2701 .module = THIS_MODULE,
2702 .ops = macsec_genl_ops,
2703 .n_ops = ARRAY_SIZE(macsec_genl_ops),
2704};
2705
c09440f7
SD
2706static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2707 struct net_device *dev)
2708{
2709 struct macsec_dev *macsec = netdev_priv(dev);
2710 struct macsec_secy *secy = &macsec->secy;
2711 struct pcpu_secy_stats *secy_stats;
2712 int ret, len;
2713
2714 /* 10.5 */
2715 if (!secy->protect_frames) {
2716 secy_stats = this_cpu_ptr(macsec->stats);
2717 u64_stats_update_begin(&secy_stats->syncp);
2718 secy_stats->stats.OutPktsUntagged++;
2719 u64_stats_update_end(&secy_stats->syncp);
79c62220 2720 skb->dev = macsec->real_dev;
c09440f7
SD
2721 len = skb->len;
2722 ret = dev_queue_xmit(skb);
2723 count_tx(dev, ret, len);
2724 return ret;
2725 }
2726
2727 if (!secy->operational) {
2728 kfree_skb(skb);
2729 dev->stats.tx_dropped++;
2730 return NETDEV_TX_OK;
2731 }
2732
2733 skb = macsec_encrypt(skb, dev);
2734 if (IS_ERR(skb)) {
2735 if (PTR_ERR(skb) != -EINPROGRESS)
2736 dev->stats.tx_dropped++;
2737 return NETDEV_TX_OK;
2738 }
2739
2740 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2741
2742 macsec_encrypt_finish(skb, dev);
2743 len = skb->len;
2744 ret = dev_queue_xmit(skb);
2745 count_tx(dev, ret, len);
2746 return ret;
2747}
2748
2749#define MACSEC_FEATURES \
2750 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
e2003872
SD
2751static struct lock_class_key macsec_netdev_addr_lock_key;
2752
c09440f7
SD
2753static int macsec_dev_init(struct net_device *dev)
2754{
2755 struct macsec_dev *macsec = macsec_priv(dev);
2756 struct net_device *real_dev = macsec->real_dev;
5491e7c6 2757 int err;
c09440f7
SD
2758
2759 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2760 if (!dev->tstats)
2761 return -ENOMEM;
2762
5491e7c6
PA
2763 err = gro_cells_init(&macsec->gro_cells, dev);
2764 if (err) {
2765 free_percpu(dev->tstats);
2766 return err;
2767 }
2768
c09440f7
SD
2769 dev->features = real_dev->features & MACSEC_FEATURES;
2770 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2771
2772 dev->needed_headroom = real_dev->needed_headroom +
2773 MACSEC_NEEDED_HEADROOM;
2774 dev->needed_tailroom = real_dev->needed_tailroom +
2775 MACSEC_NEEDED_TAILROOM;
2776
2777 if (is_zero_ether_addr(dev->dev_addr))
2778 eth_hw_addr_inherit(dev, real_dev);
2779 if (is_zero_ether_addr(dev->broadcast))
2780 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2781
2782 return 0;
2783}
2784
2785static void macsec_dev_uninit(struct net_device *dev)
2786{
5491e7c6
PA
2787 struct macsec_dev *macsec = macsec_priv(dev);
2788
2789 gro_cells_destroy(&macsec->gro_cells);
c09440f7
SD
2790 free_percpu(dev->tstats);
2791}
2792
2793static netdev_features_t macsec_fix_features(struct net_device *dev,
2794 netdev_features_t features)
2795{
2796 struct macsec_dev *macsec = macsec_priv(dev);
2797 struct net_device *real_dev = macsec->real_dev;
2798
5491e7c6
PA
2799 features &= (real_dev->features & MACSEC_FEATURES) |
2800 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2801 features |= NETIF_F_LLTX;
c09440f7
SD
2802
2803 return features;
2804}
2805
2806static int macsec_dev_open(struct net_device *dev)
2807{
2808 struct macsec_dev *macsec = macsec_priv(dev);
2809 struct net_device *real_dev = macsec->real_dev;
2810 int err;
2811
c09440f7
SD
2812 err = dev_uc_add(real_dev, dev->dev_addr);
2813 if (err < 0)
2814 return err;
2815
2816 if (dev->flags & IFF_ALLMULTI) {
2817 err = dev_set_allmulti(real_dev, 1);
2818 if (err < 0)
2819 goto del_unicast;
2820 }
2821
2822 if (dev->flags & IFF_PROMISC) {
2823 err = dev_set_promiscuity(real_dev, 1);
2824 if (err < 0)
2825 goto clear_allmulti;
2826 }
2827
2828 if (netif_carrier_ok(real_dev))
2829 netif_carrier_on(dev);
2830
2831 return 0;
2832clear_allmulti:
2833 if (dev->flags & IFF_ALLMULTI)
2834 dev_set_allmulti(real_dev, -1);
2835del_unicast:
2836 dev_uc_del(real_dev, dev->dev_addr);
2837 netif_carrier_off(dev);
2838 return err;
2839}
2840
2841static int macsec_dev_stop(struct net_device *dev)
2842{
2843 struct macsec_dev *macsec = macsec_priv(dev);
2844 struct net_device *real_dev = macsec->real_dev;
2845
2846 netif_carrier_off(dev);
2847
2848 dev_mc_unsync(real_dev, dev);
2849 dev_uc_unsync(real_dev, dev);
2850
2851 if (dev->flags & IFF_ALLMULTI)
2852 dev_set_allmulti(real_dev, -1);
2853
2854 if (dev->flags & IFF_PROMISC)
2855 dev_set_promiscuity(real_dev, -1);
2856
2857 dev_uc_del(real_dev, dev->dev_addr);
2858
2859 return 0;
2860}
2861
2862static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2863{
2864 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2865
2866 if (!(dev->flags & IFF_UP))
2867 return;
2868
2869 if (change & IFF_ALLMULTI)
2870 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2871
2872 if (change & IFF_PROMISC)
2873 dev_set_promiscuity(real_dev,
2874 dev->flags & IFF_PROMISC ? 1 : -1);
2875}
2876
2877static void macsec_dev_set_rx_mode(struct net_device *dev)
2878{
2879 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2880
2881 dev_mc_sync(real_dev, dev);
2882 dev_uc_sync(real_dev, dev);
2883}
2884
2885static int macsec_set_mac_address(struct net_device *dev, void *p)
2886{
2887 struct macsec_dev *macsec = macsec_priv(dev);
2888 struct net_device *real_dev = macsec->real_dev;
2889 struct sockaddr *addr = p;
2890 int err;
2891
2892 if (!is_valid_ether_addr(addr->sa_data))
2893 return -EADDRNOTAVAIL;
2894
2895 if (!(dev->flags & IFF_UP))
2896 goto out;
2897
2898 err = dev_uc_add(real_dev, addr->sa_data);
2899 if (err < 0)
2900 return err;
2901
2902 dev_uc_del(real_dev, dev->dev_addr);
2903
2904out:
2905 ether_addr_copy(dev->dev_addr, addr->sa_data);
2906 return 0;
2907}
2908
2909static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2910{
2911 struct macsec_dev *macsec = macsec_priv(dev);
2912 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2913
2914 if (macsec->real_dev->mtu - extra < new_mtu)
2915 return -ERANGE;
2916
2917 dev->mtu = new_mtu;
2918
2919 return 0;
2920}
2921
bc1f4470 2922static void macsec_get_stats64(struct net_device *dev,
2923 struct rtnl_link_stats64 *s)
c09440f7
SD
2924{
2925 int cpu;
2926
2927 if (!dev->tstats)
bc1f4470 2928 return;
c09440f7
SD
2929
2930 for_each_possible_cpu(cpu) {
2931 struct pcpu_sw_netstats *stats;
2932 struct pcpu_sw_netstats tmp;
2933 int start;
2934
2935 stats = per_cpu_ptr(dev->tstats, cpu);
2936 do {
2937 start = u64_stats_fetch_begin_irq(&stats->syncp);
2938 tmp.rx_packets = stats->rx_packets;
2939 tmp.rx_bytes = stats->rx_bytes;
2940 tmp.tx_packets = stats->tx_packets;
2941 tmp.tx_bytes = stats->tx_bytes;
2942 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2943
2944 s->rx_packets += tmp.rx_packets;
2945 s->rx_bytes += tmp.rx_bytes;
2946 s->tx_packets += tmp.tx_packets;
2947 s->tx_bytes += tmp.tx_bytes;
2948 }
2949
2950 s->rx_dropped = dev->stats.rx_dropped;
2951 s->tx_dropped = dev->stats.tx_dropped;
c09440f7
SD
2952}
2953
2954static int macsec_get_iflink(const struct net_device *dev)
2955{
2956 return macsec_priv(dev)->real_dev->ifindex;
2957}
2958
e2003872
SD
2959static int macsec_get_nest_level(struct net_device *dev)
2960{
2961 return macsec_priv(dev)->nest_level;
2962}
2963
c09440f7
SD
2964static const struct net_device_ops macsec_netdev_ops = {
2965 .ndo_init = macsec_dev_init,
2966 .ndo_uninit = macsec_dev_uninit,
2967 .ndo_open = macsec_dev_open,
2968 .ndo_stop = macsec_dev_stop,
2969 .ndo_fix_features = macsec_fix_features,
2970 .ndo_change_mtu = macsec_change_mtu,
2971 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2972 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2973 .ndo_set_mac_address = macsec_set_mac_address,
2974 .ndo_start_xmit = macsec_start_xmit,
2975 .ndo_get_stats64 = macsec_get_stats64,
2976 .ndo_get_iflink = macsec_get_iflink,
e2003872 2977 .ndo_get_lock_subclass = macsec_get_nest_level,
c09440f7
SD
2978};
2979
2980static const struct device_type macsec_type = {
2981 .name = "macsec",
2982};
2983
2984static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2985 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
2986 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2987 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2988 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2989 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2990 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2991 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2992 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2993 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
2994 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
2995 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
2996 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
2997};
2998
2999static void macsec_free_netdev(struct net_device *dev)
3000{
3001 struct macsec_dev *macsec = macsec_priv(dev);
3002 struct net_device *real_dev = macsec->real_dev;
3003
3004 free_percpu(macsec->stats);
3005 free_percpu(macsec->secy.tx_sc.stats);
3006
3007 dev_put(real_dev);
c09440f7
SD
3008}
3009
3010static void macsec_setup(struct net_device *dev)
3011{
3012 ether_setup(dev);
91572088
JW
3013 dev->min_mtu = 0;
3014 dev->max_mtu = ETH_MAX_MTU;
e425974f 3015 dev->priv_flags |= IFF_NO_QUEUE;
c09440f7 3016 dev->netdev_ops = &macsec_netdev_ops;
cf124db5
DM
3017 dev->needs_free_netdev = true;
3018 dev->priv_destructor = macsec_free_netdev;
c24acf03 3019 SET_NETDEV_DEVTYPE(dev, &macsec_type);
c09440f7
SD
3020
3021 eth_zero_addr(dev->broadcast);
3022}
3023
ccfdec90
FW
3024static int macsec_changelink_common(struct net_device *dev,
3025 struct nlattr *data[])
c09440f7
SD
3026{
3027 struct macsec_secy *secy;
3028 struct macsec_tx_sc *tx_sc;
3029
3030 secy = &macsec_priv(dev)->secy;
3031 tx_sc = &secy->tx_sc;
3032
3033 if (data[IFLA_MACSEC_ENCODING_SA]) {
3034 struct macsec_tx_sa *tx_sa;
3035
3036 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3037 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3038
3039 secy->operational = tx_sa && tx_sa->active;
3040 }
3041
3042 if (data[IFLA_MACSEC_WINDOW])
3043 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3044
3045 if (data[IFLA_MACSEC_ENCRYPT])
3046 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3047
3048 if (data[IFLA_MACSEC_PROTECT])
3049 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3050
3051 if (data[IFLA_MACSEC_INC_SCI])
3052 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3053
3054 if (data[IFLA_MACSEC_ES])
3055 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3056
3057 if (data[IFLA_MACSEC_SCB])
3058 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3059
3060 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3061 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3062
3063 if (data[IFLA_MACSEC_VALIDATION])
3064 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
ccfdec90
FW
3065
3066 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3067 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3068 case MACSEC_CIPHER_ID_GCM_AES_128:
e8660ded 3069 case MACSEC_DEFAULT_CIPHER_ID:
ccfdec90
FW
3070 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3071 break;
3072 case MACSEC_CIPHER_ID_GCM_AES_256:
3073 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3074 break;
3075 default:
3076 return -EINVAL;
3077 }
3078 }
3079
3080 return 0;
c09440f7
SD
3081}
3082
3083static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
ad744b22
MS
3084 struct nlattr *data[],
3085 struct netlink_ext_ack *extack)
c09440f7
SD
3086{
3087 if (!data)
3088 return 0;
3089
3090 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3091 data[IFLA_MACSEC_ICV_LEN] ||
3092 data[IFLA_MACSEC_SCI] ||
3093 data[IFLA_MACSEC_PORT])
3094 return -EINVAL;
3095
ccfdec90 3096 return macsec_changelink_common(dev, data);
c09440f7
SD
3097}
3098
3099static void macsec_del_dev(struct macsec_dev *macsec)
3100{
3101 int i;
3102
3103 while (macsec->secy.rx_sc) {
3104 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3105
3106 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3107 free_rx_sc(rx_sc);
3108 }
3109
3110 for (i = 0; i < MACSEC_NUM_AN; i++) {
3111 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3112
3113 if (sa) {
3114 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3115 clear_tx_sa(sa);
3116 }
3117 }
3118}
3119
bbe11fab
SD
3120static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3121{
3122 struct macsec_dev *macsec = macsec_priv(dev);
e2003872 3123 struct net_device *real_dev = macsec->real_dev;
bbe11fab
SD
3124
3125 unregister_netdevice_queue(dev, head);
3126 list_del_rcu(&macsec->secys);
3127 macsec_del_dev(macsec);
e2003872 3128 netdev_upper_dev_unlink(real_dev, dev);
bbe11fab
SD
3129
3130 macsec_generation++;
3131}
3132
c09440f7
SD
3133static void macsec_dellink(struct net_device *dev, struct list_head *head)
3134{
3135 struct macsec_dev *macsec = macsec_priv(dev);
3136 struct net_device *real_dev = macsec->real_dev;
3137 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3138
bbe11fab 3139 macsec_common_dellink(dev, head);
96cfc505 3140
960d5848 3141 if (list_empty(&rxd->secys)) {
c09440f7 3142 netdev_rx_handler_unregister(real_dev);
960d5848
SD
3143 kfree(rxd);
3144 }
c09440f7
SD
3145}
3146
3147static int register_macsec_dev(struct net_device *real_dev,
3148 struct net_device *dev)
3149{
3150 struct macsec_dev *macsec = macsec_priv(dev);
3151 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3152
3153 if (!rxd) {
3154 int err;
3155
3156 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3157 if (!rxd)
3158 return -ENOMEM;
3159
3160 INIT_LIST_HEAD(&rxd->secys);
3161
3162 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3163 rxd);
960d5848
SD
3164 if (err < 0) {
3165 kfree(rxd);
c09440f7 3166 return err;
960d5848 3167 }
c09440f7
SD
3168 }
3169
3170 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3171 return 0;
3172}
3173
3174static bool sci_exists(struct net_device *dev, sci_t sci)
3175{
3176 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3177 struct macsec_dev *macsec;
3178
3179 list_for_each_entry(macsec, &rxd->secys, secys) {
3180 if (macsec->secy.sci == sci)
3181 return true;
3182 }
3183
3184 return false;
3185}
3186
3187static sci_t dev_to_sci(struct net_device *dev, __be16 port)
3188{
3189 return make_sci(dev->dev_addr, port);
3190}
3191
3192static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3193{
3194 struct macsec_dev *macsec = macsec_priv(dev);
3195 struct macsec_secy *secy = &macsec->secy;
3196
3197 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3198 if (!macsec->stats)
3199 return -ENOMEM;
3200
3201 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3202 if (!secy->tx_sc.stats) {
3203 free_percpu(macsec->stats);
3204 return -ENOMEM;
3205 }
3206
3207 if (sci == MACSEC_UNDEF_SCI)
3208 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3209
3210 secy->netdev = dev;
3211 secy->operational = true;
3212 secy->key_len = DEFAULT_SAK_LEN;
3213 secy->icv_len = icv_len;
3214 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3215 secy->protect_frames = true;
3216 secy->replay_protect = false;
3217
3218 secy->sci = sci;
3219 secy->tx_sc.active = true;
3220 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3221 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3222 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3223 secy->tx_sc.end_station = false;
3224 secy->tx_sc.scb = false;
3225
3226 return 0;
3227}
3228
3229static int macsec_newlink(struct net *net, struct net_device *dev,
7a3f4a18
MS
3230 struct nlattr *tb[], struct nlattr *data[],
3231 struct netlink_ext_ack *extack)
c09440f7
SD
3232{
3233 struct macsec_dev *macsec = macsec_priv(dev);
3234 struct net_device *real_dev;
3235 int err;
3236 sci_t sci;
3237 u8 icv_len = DEFAULT_ICV_LEN;
3238 rx_handler_func_t *rx_handler;
3239
3240 if (!tb[IFLA_LINK])
3241 return -EINVAL;
3242 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3243 if (!real_dev)
3244 return -ENODEV;
3245
3246 dev->priv_flags |= IFF_MACSEC;
3247
3248 macsec->real_dev = real_dev;
3249
3250 if (data && data[IFLA_MACSEC_ICV_LEN])
3251 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3252 dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3253
3254 rx_handler = rtnl_dereference(real_dev->rx_handler);
3255 if (rx_handler && rx_handler != macsec_handle_frame)
3256 return -EBUSY;
3257
3258 err = register_netdevice(dev);
3259 if (err < 0)
3260 return err;
3261
0759e552
SD
3262 dev_hold(real_dev);
3263
952fcfd0 3264 macsec->nest_level = dev_get_nest_level(real_dev) + 1;
e2003872
SD
3265 netdev_lockdep_set_classes(dev);
3266 lockdep_set_class_and_subclass(&dev->addr_list_lock,
3267 &macsec_netdev_addr_lock_key,
3268 macsec_get_nest_level(dev));
3269
42ab19ee 3270 err = netdev_upper_dev_link(real_dev, dev, extack);
e2003872 3271 if (err < 0)
bd28899d 3272 goto unregister;
e2003872 3273
c09440f7
SD
3274 /* need to be already registered so that ->init has run and
3275 * the MAC addr is set
3276 */
3277 if (data && data[IFLA_MACSEC_SCI])
3278 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3279 else if (data && data[IFLA_MACSEC_PORT])
3280 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3281 else
3282 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3283
3284 if (rx_handler && sci_exists(real_dev, sci)) {
3285 err = -EBUSY;
e2003872 3286 goto unlink;
c09440f7
SD
3287 }
3288
3289 err = macsec_add_dev(dev, sci, icv_len);
3290 if (err)
e2003872 3291 goto unlink;
c09440f7 3292
ccfdec90
FW
3293 if (data) {
3294 err = macsec_changelink_common(dev, data);
3295 if (err)
3296 goto del_dev;
3297 }
c09440f7
SD
3298
3299 err = register_macsec_dev(real_dev, dev);
3300 if (err < 0)
3301 goto del_dev;
3302
e6ac0758
SD
3303 netif_stacked_transfer_operstate(real_dev, dev);
3304 linkwatch_fire_event(dev);
3305
96cfc505
SD
3306 macsec_generation++;
3307
c09440f7
SD
3308 return 0;
3309
3310del_dev:
3311 macsec_del_dev(macsec);
e2003872
SD
3312unlink:
3313 netdev_upper_dev_unlink(real_dev, dev);
bd28899d 3314unregister:
c09440f7
SD
3315 unregister_netdevice(dev);
3316 return err;
3317}
3318
a8b8a889
MS
3319static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3320 struct netlink_ext_ack *extack)
c09440f7 3321{
74816480 3322 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
c09440f7
SD
3323 u8 icv_len = DEFAULT_ICV_LEN;
3324 int flag;
3325 bool es, scb, sci;
3326
3327 if (!data)
3328 return 0;
3329
3330 if (data[IFLA_MACSEC_CIPHER_SUITE])
3331 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3332
f04c392d 3333 if (data[IFLA_MACSEC_ICV_LEN]) {
c09440f7 3334 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
f04c392d
DC
3335 if (icv_len != DEFAULT_ICV_LEN) {
3336 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3337 struct crypto_aead *dummy_tfm;
3338
3339 dummy_tfm = macsec_alloc_tfm(dummy_key,
3340 DEFAULT_SAK_LEN,
3341 icv_len);
3342 if (IS_ERR(dummy_tfm))
3343 return PTR_ERR(dummy_tfm);
3344 crypto_free_aead(dummy_tfm);
3345 }
3346 }
c09440f7
SD
3347
3348 switch (csid) {
ccfdec90
FW
3349 case MACSEC_CIPHER_ID_GCM_AES_128:
3350 case MACSEC_CIPHER_ID_GCM_AES_256:
e8660ded 3351 case MACSEC_DEFAULT_CIPHER_ID:
c09440f7 3352 if (icv_len < MACSEC_MIN_ICV_LEN ||
2ccbe2cb 3353 icv_len > MACSEC_STD_ICV_LEN)
c09440f7
SD
3354 return -EINVAL;
3355 break;
3356 default:
3357 return -EINVAL;
3358 }
3359
3360 if (data[IFLA_MACSEC_ENCODING_SA]) {
3361 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3362 return -EINVAL;
3363 }
3364
3365 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3366 flag < IFLA_MACSEC_VALIDATION;
3367 flag++) {
3368 if (data[flag]) {
3369 if (nla_get_u8(data[flag]) > 1)
3370 return -EINVAL;
3371 }
3372 }
3373
3374 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3375 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3376 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3377
3378 if ((sci && (scb || es)) || (scb && es))
3379 return -EINVAL;
3380
3381 if (data[IFLA_MACSEC_VALIDATION] &&
3382 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3383 return -EINVAL;
3384
4b1fb935
SD
3385 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3386 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
c09440f7
SD
3387 !data[IFLA_MACSEC_WINDOW])
3388 return -EINVAL;
3389
3390 return 0;
3391}
3392
3393static struct net *macsec_get_link_net(const struct net_device *dev)
3394{
3395 return dev_net(macsec_priv(dev)->real_dev);
3396}
3397
3398static size_t macsec_get_size(const struct net_device *dev)
3399{
c9fba3ed
ZS
3400 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
3401 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
3402 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
3403 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
3404 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
3405 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
3406 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
3407 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
3408 nla_total_size(1) + /* IFLA_MACSEC_ES */
3409 nla_total_size(1) + /* IFLA_MACSEC_SCB */
3410 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
3411 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
c09440f7
SD
3412 0;
3413}
3414
3415static int macsec_fill_info(struct sk_buff *skb,
3416 const struct net_device *dev)
3417{
3418 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3419 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
ccfdec90
FW
3420 u64 csid;
3421
3422 switch (secy->key_len) {
3423 case MACSEC_GCM_AES_128_SAK_LEN:
e8660ded 3424 csid = MACSEC_DEFAULT_CIPHER_ID;
ccfdec90
FW
3425 break;
3426 case MACSEC_GCM_AES_256_SAK_LEN:
3427 csid = MACSEC_CIPHER_ID_GCM_AES_256;
3428 break;
3429 default:
3430 goto nla_put_failure;
3431 }
c09440f7 3432
f60d94c0
ND
3433 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3434 IFLA_MACSEC_PAD) ||
c09440f7 3435 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
f60d94c0 3436 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
ccfdec90 3437 csid, IFLA_MACSEC_PAD) ||
c09440f7
SD
3438 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3439 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3440 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3441 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3442 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3443 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3444 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3445 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3446 0)
3447 goto nla_put_failure;
3448
3449 if (secy->replay_protect) {
3450 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3451 goto nla_put_failure;
3452 }
3453
3454 return 0;
3455
3456nla_put_failure:
3457 return -EMSGSIZE;
3458}
3459
3460static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3461 .kind = "macsec",
3462 .priv_size = sizeof(struct macsec_dev),
3463 .maxtype = IFLA_MACSEC_MAX,
3464 .policy = macsec_rtnl_policy,
3465 .setup = macsec_setup,
3466 .validate = macsec_validate_attr,
3467 .newlink = macsec_newlink,
3468 .changelink = macsec_changelink,
3469 .dellink = macsec_dellink,
3470 .get_size = macsec_get_size,
3471 .fill_info = macsec_fill_info,
3472 .get_link_net = macsec_get_link_net,
3473};
3474
3475static bool is_macsec_master(struct net_device *dev)
3476{
3477 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3478}
3479
3480static int macsec_notify(struct notifier_block *this, unsigned long event,
3481 void *ptr)
3482{
3483 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3484 LIST_HEAD(head);
3485
3486 if (!is_macsec_master(real_dev))
3487 return NOTIFY_DONE;
3488
3489 switch (event) {
e6ac0758
SD
3490 case NETDEV_DOWN:
3491 case NETDEV_UP:
3492 case NETDEV_CHANGE: {
3493 struct macsec_dev *m, *n;
3494 struct macsec_rxh_data *rxd;
3495
3496 rxd = macsec_data_rtnl(real_dev);
3497 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3498 struct net_device *dev = m->secy.netdev;
3499
3500 netif_stacked_transfer_operstate(real_dev, dev);
3501 }
3502 break;
3503 }
c09440f7
SD
3504 case NETDEV_UNREGISTER: {
3505 struct macsec_dev *m, *n;
3506 struct macsec_rxh_data *rxd;
3507
3508 rxd = macsec_data_rtnl(real_dev);
3509 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
bbe11fab 3510 macsec_common_dellink(m->secy.netdev, &head);
c09440f7 3511 }
bbe11fab
SD
3512
3513 netdev_rx_handler_unregister(real_dev);
3514 kfree(rxd);
3515
c09440f7
SD
3516 unregister_netdevice_many(&head);
3517 break;
3518 }
3519 case NETDEV_CHANGEMTU: {
3520 struct macsec_dev *m;
3521 struct macsec_rxh_data *rxd;
3522
3523 rxd = macsec_data_rtnl(real_dev);
3524 list_for_each_entry(m, &rxd->secys, secys) {
3525 struct net_device *dev = m->secy.netdev;
3526 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3527 macsec_extra_len(true));
3528
3529 if (dev->mtu > mtu)
3530 dev_set_mtu(dev, mtu);
3531 }
3532 }
3533 }
3534
3535 return NOTIFY_OK;
3536}
3537
3538static struct notifier_block macsec_notifier = {
3539 .notifier_call = macsec_notify,
3540};
3541
3542static int __init macsec_init(void)
3543{
3544 int err;
3545
3546 pr_info("MACsec IEEE 802.1AE\n");
3547 err = register_netdevice_notifier(&macsec_notifier);
3548 if (err)
3549 return err;
3550
3551 err = rtnl_link_register(&macsec_link_ops);
3552 if (err)
3553 goto notifier;
3554
489111e5 3555 err = genl_register_family(&macsec_fam);
c09440f7
SD
3556 if (err)
3557 goto rtnl;
3558
3559 return 0;
3560
3561rtnl:
3562 rtnl_link_unregister(&macsec_link_ops);
3563notifier:
3564 unregister_netdevice_notifier(&macsec_notifier);
3565 return err;
3566}
3567
3568static void __exit macsec_exit(void)
3569{
3570 genl_unregister_family(&macsec_fam);
3571 rtnl_link_unregister(&macsec_link_ops);
3572 unregister_netdevice_notifier(&macsec_notifier);
b196c22a 3573 rcu_barrier();
c09440f7
SD
3574}
3575
3576module_init(macsec_init);
3577module_exit(macsec_exit);
3578
3579MODULE_ALIAS_RTNL_LINK("macsec");
78362998 3580MODULE_ALIAS_GENL_FAMILY("macsec");
c09440f7
SD
3581
3582MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3583MODULE_LICENSE("GPL v2");