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