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