1
2
3
4
5
6
7
8#include <linux/types.h>
9#include <linux/skbuff.h>
10#include <linux/socket.h>
11#include <linux/module.h>
12#include <crypto/aead.h>
13#include <linux/etherdevice.h>
14#include <linux/netdevice.h>
15#include <linux/rtnetlink.h>
16#include <linux/refcount.h>
17#include <net/genetlink.h>
18#include <net/sock.h>
19#include <net/gro_cells.h>
20#include <net/macsec.h>
21#include <linux/phy.h>
22#include <linux/byteorder/generic.h>
23#include <linux/if_arp.h>
24
25#include <uapi/linux/if_macsec.h>
26
27#define MACSEC_SCI_LEN 8
28
29
30#define MACSEC_TAG_LEN 6
31
32struct macsec_eth_header {
33 struct ethhdr eth;
34
35 u8 tci_an;
36#if defined(__LITTLE_ENDIAN_BITFIELD)
37 u8 short_length:6,
38 unused:2;
39#elif defined(__BIG_ENDIAN_BITFIELD)
40 u8 unused:2,
41 short_length:6;
42#else
43#error "Please fix <asm/byteorder.h>"
44#endif
45 __be32 packet_number;
46 u8 secure_channel_id[8];
47} __packed;
48
49#define MACSEC_TCI_VERSION 0x80
50#define MACSEC_TCI_ES 0x40
51#define MACSEC_TCI_SC 0x20
52#define MACSEC_TCI_SCB 0x10
53#define MACSEC_TCI_E 0x08
54#define MACSEC_TCI_C 0x04
55#define MACSEC_AN_MASK 0x03
56#define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
57
58
59#define MIN_NON_SHORT_LEN 48
60
61#define GCM_AES_IV_LEN 12
62#define DEFAULT_ICV_LEN 16
63
64#define for_each_rxsc(secy, sc) \
65 for (sc = rcu_dereference_bh(secy->rx_sc); \
66 sc; \
67 sc = rcu_dereference_bh(sc->next))
68#define for_each_rxsc_rtnl(secy, sc) \
69 for (sc = rtnl_dereference(secy->rx_sc); \
70 sc; \
71 sc = rtnl_dereference(sc->next))
72
73#define pn_same_half(pn1, pn2) (!(((pn1) >> 31) ^ ((pn2) >> 31)))
74
75struct gcm_iv_xpn {
76 union {
77 u8 short_secure_channel_id[4];
78 ssci_t ssci;
79 };
80 __be64 pn;
81} __packed;
82
83struct gcm_iv {
84 union {
85 u8 secure_channel_id[8];
86 sci_t sci;
87 };
88 __be32 pn;
89};
90
91#define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
92
93struct pcpu_secy_stats {
94 struct macsec_dev_stats stats;
95 struct u64_stats_sync syncp;
96};
97
98
99
100
101
102
103
104
105
106
107
108struct macsec_dev {
109 struct macsec_secy secy;
110 struct net_device *real_dev;
111 netdevice_tracker dev_tracker;
112 struct pcpu_secy_stats __percpu *stats;
113 struct list_head secys;
114 struct gro_cells gro_cells;
115 enum macsec_offload offload;
116};
117
118
119
120
121
122struct macsec_rxh_data {
123 struct list_head secys;
124};
125
126static struct macsec_dev *macsec_priv(const struct net_device *dev)
127{
128 return (struct macsec_dev *)netdev_priv(dev);
129}
130
131static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
132{
133 return rcu_dereference_bh(dev->rx_handler_data);
134}
135
136static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
137{
138 return rtnl_dereference(dev->rx_handler_data);
139}
140
141struct macsec_cb {
142 struct aead_request *req;
143 union {
144 struct macsec_tx_sa *tx_sa;
145 struct macsec_rx_sa *rx_sa;
146 };
147 u8 assoc_num;
148 bool valid;
149 bool has_sci;
150};
151
152static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
153{
154 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
155
156 if (!sa || !sa->active)
157 return NULL;
158
159 if (!refcount_inc_not_zero(&sa->refcnt))
160 return NULL;
161
162 return sa;
163}
164
165static struct macsec_rx_sa *macsec_active_rxsa_get(struct macsec_rx_sc *rx_sc)
166{
167 struct macsec_rx_sa *sa = NULL;
168 int an;
169
170 for (an = 0; an < MACSEC_NUM_AN; an++) {
171 sa = macsec_rxsa_get(rx_sc->sa[an]);
172 if (sa)
173 break;
174 }
175 return sa;
176}
177
178static void free_rx_sc_rcu(struct rcu_head *head)
179{
180 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
181
182 free_percpu(rx_sc->stats);
183 kfree(rx_sc);
184}
185
186static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
187{
188 return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
189}
190
191static void macsec_rxsc_put(struct macsec_rx_sc *sc)
192{
193 if (refcount_dec_and_test(&sc->refcnt))
194 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
195}
196
197static void free_rxsa(struct rcu_head *head)
198{
199 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
200
201 crypto_free_aead(sa->key.tfm);
202 free_percpu(sa->stats);
203 kfree(sa);
204}
205
206static void macsec_rxsa_put(struct macsec_rx_sa *sa)
207{
208 if (refcount_dec_and_test(&sa->refcnt))
209 call_rcu(&sa->rcu, free_rxsa);
210}
211
212static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
213{
214 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
215
216 if (!sa || !sa->active)
217 return NULL;
218
219 if (!refcount_inc_not_zero(&sa->refcnt))
220 return NULL;
221
222 return sa;
223}
224
225static void free_txsa(struct rcu_head *head)
226{
227 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
228
229 crypto_free_aead(sa->key.tfm);
230 free_percpu(sa->stats);
231 kfree(sa);
232}
233
234static void macsec_txsa_put(struct macsec_tx_sa *sa)
235{
236 if (refcount_dec_and_test(&sa->refcnt))
237 call_rcu(&sa->rcu, free_txsa);
238}
239
240static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
241{
242 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
243 return (struct macsec_cb *)skb->cb;
244}
245
246#define MACSEC_PORT_ES (htons(0x0001))
247#define MACSEC_PORT_SCB (0x0000)
248#define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
249#define MACSEC_UNDEF_SSCI ((__force ssci_t)0xffffffff)
250
251#define MACSEC_GCM_AES_128_SAK_LEN 16
252#define MACSEC_GCM_AES_256_SAK_LEN 32
253
254#define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
255#define DEFAULT_XPN false
256#define DEFAULT_SEND_SCI true
257#define DEFAULT_ENCRYPT false
258#define DEFAULT_ENCODING_SA 0
259#define MACSEC_XPN_MAX_REPLAY_WINDOW (((1 << 30) - 1))
260
261static bool send_sci(const struct macsec_secy *secy)
262{
263 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
264
265 return tx_sc->send_sci ||
266 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
267}
268
269static sci_t make_sci(const u8 *addr, __be16 port)
270{
271 sci_t sci;
272
273 memcpy(&sci, addr, ETH_ALEN);
274 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
275
276 return sci;
277}
278
279static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
280{
281 sci_t sci;
282
283 if (sci_present)
284 memcpy(&sci, hdr->secure_channel_id,
285 sizeof(hdr->secure_channel_id));
286 else
287 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
288
289 return sci;
290}
291
292static unsigned int macsec_sectag_len(bool sci_present)
293{
294 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
295}
296
297static unsigned int macsec_hdr_len(bool sci_present)
298{
299 return macsec_sectag_len(sci_present) + ETH_HLEN;
300}
301
302static unsigned int macsec_extra_len(bool sci_present)
303{
304 return macsec_sectag_len(sci_present) + sizeof(__be16);
305}
306
307
308static void macsec_fill_sectag(struct macsec_eth_header *h,
309 const struct macsec_secy *secy, u32 pn,
310 bool sci_present)
311{
312 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
313
314 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
315 h->eth.h_proto = htons(ETH_P_MACSEC);
316
317 if (sci_present) {
318 h->tci_an |= MACSEC_TCI_SC;
319 memcpy(&h->secure_channel_id, &secy->sci,
320 sizeof(h->secure_channel_id));
321 } else {
322 if (tx_sc->end_station)
323 h->tci_an |= MACSEC_TCI_ES;
324 if (tx_sc->scb)
325 h->tci_an |= MACSEC_TCI_SCB;
326 }
327
328 h->packet_number = htonl(pn);
329
330
331 if (tx_sc->encrypt)
332 h->tci_an |= MACSEC_TCI_CONFID;
333 else if (secy->icv_len != DEFAULT_ICV_LEN)
334 h->tci_an |= MACSEC_TCI_C;
335
336 h->tci_an |= tx_sc->encoding_sa;
337}
338
339static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
340{
341 if (data_len < MIN_NON_SHORT_LEN)
342 h->short_length = data_len;
343}
344
345
346static bool macsec_is_offloaded(struct macsec_dev *macsec)
347{
348 if (macsec->offload == MACSEC_OFFLOAD_MAC ||
349 macsec->offload == MACSEC_OFFLOAD_PHY)
350 return true;
351
352 return false;
353}
354
355
356static bool macsec_check_offload(enum macsec_offload offload,
357 struct macsec_dev *macsec)
358{
359 if (!macsec || !macsec->real_dev)
360 return false;
361
362 if (offload == MACSEC_OFFLOAD_PHY)
363 return macsec->real_dev->phydev &&
364 macsec->real_dev->phydev->macsec_ops;
365 else if (offload == MACSEC_OFFLOAD_MAC)
366 return macsec->real_dev->features & NETIF_F_HW_MACSEC &&
367 macsec->real_dev->macsec_ops;
368
369 return false;
370}
371
372static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload,
373 struct macsec_dev *macsec,
374 struct macsec_context *ctx)
375{
376 if (ctx) {
377 memset(ctx, 0, sizeof(*ctx));
378 ctx->offload = offload;
379
380 if (offload == MACSEC_OFFLOAD_PHY)
381 ctx->phydev = macsec->real_dev->phydev;
382 else if (offload == MACSEC_OFFLOAD_MAC)
383 ctx->netdev = macsec->real_dev;
384 }
385
386 if (offload == MACSEC_OFFLOAD_PHY)
387 return macsec->real_dev->phydev->macsec_ops;
388 else
389 return macsec->real_dev->macsec_ops;
390}
391
392
393
394
395static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec,
396 struct macsec_context *ctx)
397{
398 if (!macsec_check_offload(macsec->offload, macsec))
399 return NULL;
400
401 return __macsec_get_ops(macsec->offload, macsec, ctx);
402}
403
404
405static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len, bool xpn)
406{
407 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
408 int len = skb->len - 2 * ETH_ALEN;
409 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
410
411
412 if (skb->len <= 16)
413 return false;
414
415
416
417
418 if (h->tci_an & MACSEC_TCI_VERSION)
419 return false;
420
421
422 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
423 (h->tci_an & MACSEC_TCI_SC))
424 return false;
425
426
427 if (h->unused)
428 return false;
429
430
431 if (!h->packet_number && !xpn)
432 return false;
433
434
435 if (h->short_length)
436 return len == extra_len + h->short_length;
437 return len >= extra_len + MIN_NON_SHORT_LEN;
438}
439
440#define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
441#define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
442
443static void macsec_fill_iv_xpn(unsigned char *iv, ssci_t ssci, u64 pn,
444 salt_t salt)
445{
446 struct gcm_iv_xpn *gcm_iv = (struct gcm_iv_xpn *)iv;
447
448 gcm_iv->ssci = ssci ^ salt.ssci;
449 gcm_iv->pn = cpu_to_be64(pn) ^ salt.pn;
450}
451
452static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
453{
454 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
455
456 gcm_iv->sci = sci;
457 gcm_iv->pn = htonl(pn);
458}
459
460static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
461{
462 return (struct macsec_eth_header *)skb_mac_header(skb);
463}
464
465static void __macsec_pn_wrapped(struct macsec_secy *secy,
466 struct macsec_tx_sa *tx_sa)
467{
468 pr_debug("PN wrapped, transitioning to !oper\n");
469 tx_sa->active = false;
470 if (secy->protect_frames)
471 secy->operational = false;
472}
473
474void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa)
475{
476 spin_lock_bh(&tx_sa->lock);
477 __macsec_pn_wrapped(secy, tx_sa);
478 spin_unlock_bh(&tx_sa->lock);
479}
480EXPORT_SYMBOL_GPL(macsec_pn_wrapped);
481
482static pn_t tx_sa_update_pn(struct macsec_tx_sa *tx_sa,
483 struct macsec_secy *secy)
484{
485 pn_t pn;
486
487 spin_lock_bh(&tx_sa->lock);
488
489 pn = tx_sa->next_pn_halves;
490 if (secy->xpn)
491 tx_sa->next_pn++;
492 else
493 tx_sa->next_pn_halves.lower++;
494
495 if (tx_sa->next_pn == 0)
496 __macsec_pn_wrapped(secy, tx_sa);
497 spin_unlock_bh(&tx_sa->lock);
498
499 return pn;
500}
501
502static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
503{
504 struct macsec_dev *macsec = netdev_priv(dev);
505
506 skb->dev = macsec->real_dev;
507 skb_reset_mac_header(skb);
508 skb->protocol = eth_hdr(skb)->h_proto;
509}
510
511static unsigned int macsec_msdu_len(struct sk_buff *skb)
512{
513 struct macsec_dev *macsec = macsec_priv(skb->dev);
514 struct macsec_secy *secy = &macsec->secy;
515 bool sci_present = macsec_skb_cb(skb)->has_sci;
516
517 return skb->len - macsec_hdr_len(sci_present) - secy->icv_len;
518}
519
520static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
521 struct macsec_tx_sa *tx_sa)
522{
523 unsigned int msdu_len = macsec_msdu_len(skb);
524 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
525
526 u64_stats_update_begin(&txsc_stats->syncp);
527 if (tx_sc->encrypt) {
528 txsc_stats->stats.OutOctetsEncrypted += msdu_len;
529 txsc_stats->stats.OutPktsEncrypted++;
530 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
531 } else {
532 txsc_stats->stats.OutOctetsProtected += msdu_len;
533 txsc_stats->stats.OutPktsProtected++;
534 this_cpu_inc(tx_sa->stats->OutPktsProtected);
535 }
536 u64_stats_update_end(&txsc_stats->syncp);
537}
538
539static void count_tx(struct net_device *dev, int ret, int len)
540{
541 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
542 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
543
544 u64_stats_update_begin(&stats->syncp);
545 u64_stats_inc(&stats->tx_packets);
546 u64_stats_add(&stats->tx_bytes, len);
547 u64_stats_update_end(&stats->syncp);
548 }
549}
550
551static void macsec_encrypt_done(struct crypto_async_request *base, int err)
552{
553 struct sk_buff *skb = base->data;
554 struct net_device *dev = skb->dev;
555 struct macsec_dev *macsec = macsec_priv(dev);
556 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
557 int len, ret;
558
559 aead_request_free(macsec_skb_cb(skb)->req);
560
561 rcu_read_lock_bh();
562 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
563
564 len = macsec_msdu_len(skb) + 2 * ETH_ALEN;
565 macsec_encrypt_finish(skb, dev);
566 ret = dev_queue_xmit(skb);
567 count_tx(dev, ret, len);
568 rcu_read_unlock_bh();
569
570 macsec_txsa_put(sa);
571 dev_put(dev);
572}
573
574static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
575 unsigned char **iv,
576 struct scatterlist **sg,
577 int num_frags)
578{
579 size_t size, iv_offset, sg_offset;
580 struct aead_request *req;
581 void *tmp;
582
583 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
584 iv_offset = size;
585 size += GCM_AES_IV_LEN;
586
587 size = ALIGN(size, __alignof__(struct scatterlist));
588 sg_offset = size;
589 size += sizeof(struct scatterlist) * num_frags;
590
591 tmp = kmalloc(size, GFP_ATOMIC);
592 if (!tmp)
593 return NULL;
594
595 *iv = (unsigned char *)(tmp + iv_offset);
596 *sg = (struct scatterlist *)(tmp + sg_offset);
597 req = tmp;
598
599 aead_request_set_tfm(req, tfm);
600
601 return req;
602}
603
604static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
605 struct net_device *dev)
606{
607 int ret;
608 struct scatterlist *sg;
609 struct sk_buff *trailer;
610 unsigned char *iv;
611 struct ethhdr *eth;
612 struct macsec_eth_header *hh;
613 size_t unprotected_len;
614 struct aead_request *req;
615 struct macsec_secy *secy;
616 struct macsec_tx_sc *tx_sc;
617 struct macsec_tx_sa *tx_sa;
618 struct macsec_dev *macsec = macsec_priv(dev);
619 bool sci_present;
620 pn_t pn;
621
622 secy = &macsec->secy;
623 tx_sc = &secy->tx_sc;
624
625
626 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
627 if (!tx_sa) {
628 secy->operational = false;
629 kfree_skb(skb);
630 return ERR_PTR(-EINVAL);
631 }
632
633 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
634 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
635 struct sk_buff *nskb = skb_copy_expand(skb,
636 MACSEC_NEEDED_HEADROOM,
637 MACSEC_NEEDED_TAILROOM,
638 GFP_ATOMIC);
639 if (likely(nskb)) {
640 consume_skb(skb);
641 skb = nskb;
642 } else {
643 macsec_txsa_put(tx_sa);
644 kfree_skb(skb);
645 return ERR_PTR(-ENOMEM);
646 }
647 } else {
648 skb = skb_unshare(skb, GFP_ATOMIC);
649 if (!skb) {
650 macsec_txsa_put(tx_sa);
651 return ERR_PTR(-ENOMEM);
652 }
653 }
654
655 unprotected_len = skb->len;
656 eth = eth_hdr(skb);
657 sci_present = send_sci(secy);
658 hh = skb_push(skb, macsec_extra_len(sci_present));
659 memmove(hh, eth, 2 * ETH_ALEN);
660
661 pn = tx_sa_update_pn(tx_sa, secy);
662 if (pn.full64 == 0) {
663 macsec_txsa_put(tx_sa);
664 kfree_skb(skb);
665 return ERR_PTR(-ENOLINK);
666 }
667 macsec_fill_sectag(hh, secy, pn.lower, sci_present);
668 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
669
670 skb_put(skb, secy->icv_len);
671
672 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
673 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
674
675 u64_stats_update_begin(&secy_stats->syncp);
676 secy_stats->stats.OutPktsTooLong++;
677 u64_stats_update_end(&secy_stats->syncp);
678
679 macsec_txsa_put(tx_sa);
680 kfree_skb(skb);
681 return ERR_PTR(-EINVAL);
682 }
683
684 ret = skb_cow_data(skb, 0, &trailer);
685 if (unlikely(ret < 0)) {
686 macsec_txsa_put(tx_sa);
687 kfree_skb(skb);
688 return ERR_PTR(ret);
689 }
690
691 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
692 if (!req) {
693 macsec_txsa_put(tx_sa);
694 kfree_skb(skb);
695 return ERR_PTR(-ENOMEM);
696 }
697
698 if (secy->xpn)
699 macsec_fill_iv_xpn(iv, tx_sa->ssci, pn.full64, tx_sa->key.salt);
700 else
701 macsec_fill_iv(iv, secy->sci, pn.lower);
702
703 sg_init_table(sg, ret);
704 ret = skb_to_sgvec(skb, sg, 0, skb->len);
705 if (unlikely(ret < 0)) {
706 aead_request_free(req);
707 macsec_txsa_put(tx_sa);
708 kfree_skb(skb);
709 return ERR_PTR(ret);
710 }
711
712 if (tx_sc->encrypt) {
713 int len = skb->len - macsec_hdr_len(sci_present) -
714 secy->icv_len;
715 aead_request_set_crypt(req, sg, sg, len, iv);
716 aead_request_set_ad(req, macsec_hdr_len(sci_present));
717 } else {
718 aead_request_set_crypt(req, sg, sg, 0, iv);
719 aead_request_set_ad(req, skb->len - secy->icv_len);
720 }
721
722 macsec_skb_cb(skb)->req = req;
723 macsec_skb_cb(skb)->tx_sa = tx_sa;
724 macsec_skb_cb(skb)->has_sci = sci_present;
725 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
726
727 dev_hold(skb->dev);
728 ret = crypto_aead_encrypt(req);
729 if (ret == -EINPROGRESS) {
730 return ERR_PTR(ret);
731 } else if (ret != 0) {
732 dev_put(skb->dev);
733 kfree_skb(skb);
734 aead_request_free(req);
735 macsec_txsa_put(tx_sa);
736 return ERR_PTR(-EINVAL);
737 }
738
739 dev_put(skb->dev);
740 aead_request_free(req);
741 macsec_txsa_put(tx_sa);
742
743 return skb;
744}
745
746static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
747{
748 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
749 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
750 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
751 u32 lowest_pn = 0;
752
753 spin_lock(&rx_sa->lock);
754 if (rx_sa->next_pn_halves.lower >= secy->replay_window)
755 lowest_pn = rx_sa->next_pn_halves.lower - secy->replay_window;
756
757
758
759
760 if (secy->replay_protect && pn < lowest_pn &&
761 (!secy->xpn || pn_same_half(pn, lowest_pn))) {
762 spin_unlock(&rx_sa->lock);
763 u64_stats_update_begin(&rxsc_stats->syncp);
764 rxsc_stats->stats.InPktsLate++;
765 u64_stats_update_end(&rxsc_stats->syncp);
766 secy->netdev->stats.rx_dropped++;
767 return false;
768 }
769
770 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
771 unsigned int msdu_len = macsec_msdu_len(skb);
772 u64_stats_update_begin(&rxsc_stats->syncp);
773 if (hdr->tci_an & MACSEC_TCI_E)
774 rxsc_stats->stats.InOctetsDecrypted += msdu_len;
775 else
776 rxsc_stats->stats.InOctetsValidated += msdu_len;
777 u64_stats_update_end(&rxsc_stats->syncp);
778 }
779
780 if (!macsec_skb_cb(skb)->valid) {
781 spin_unlock(&rx_sa->lock);
782
783
784 if (hdr->tci_an & MACSEC_TCI_C ||
785 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
786 u64_stats_update_begin(&rxsc_stats->syncp);
787 rxsc_stats->stats.InPktsNotValid++;
788 u64_stats_update_end(&rxsc_stats->syncp);
789 this_cpu_inc(rx_sa->stats->InPktsNotValid);
790 secy->netdev->stats.rx_errors++;
791 return false;
792 }
793
794 u64_stats_update_begin(&rxsc_stats->syncp);
795 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
796 rxsc_stats->stats.InPktsInvalid++;
797 this_cpu_inc(rx_sa->stats->InPktsInvalid);
798 } else if (pn < lowest_pn) {
799 rxsc_stats->stats.InPktsDelayed++;
800 } else {
801 rxsc_stats->stats.InPktsUnchecked++;
802 }
803 u64_stats_update_end(&rxsc_stats->syncp);
804 } else {
805 u64_stats_update_begin(&rxsc_stats->syncp);
806 if (pn < lowest_pn) {
807 rxsc_stats->stats.InPktsDelayed++;
808 } else {
809 rxsc_stats->stats.InPktsOK++;
810 this_cpu_inc(rx_sa->stats->InPktsOK);
811 }
812 u64_stats_update_end(&rxsc_stats->syncp);
813
814
815 if (pn + 1 > rx_sa->next_pn_halves.lower) {
816 rx_sa->next_pn_halves.lower = pn + 1;
817 } else if (secy->xpn &&
818 !pn_same_half(pn, rx_sa->next_pn_halves.lower)) {
819 rx_sa->next_pn_halves.upper++;
820 rx_sa->next_pn_halves.lower = pn + 1;
821 }
822
823 spin_unlock(&rx_sa->lock);
824 }
825
826 return true;
827}
828
829static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
830{
831 skb->pkt_type = PACKET_HOST;
832 skb->protocol = eth_type_trans(skb, dev);
833
834 skb_reset_network_header(skb);
835 if (!skb_transport_header_was_set(skb))
836 skb_reset_transport_header(skb);
837 skb_reset_mac_len(skb);
838}
839
840static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
841{
842 skb->ip_summed = CHECKSUM_NONE;
843 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
844 skb_pull(skb, hdr_len);
845 pskb_trim_unique(skb, skb->len - icv_len);
846}
847
848static void count_rx(struct net_device *dev, int len)
849{
850 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
851
852 u64_stats_update_begin(&stats->syncp);
853 u64_stats_inc(&stats->rx_packets);
854 u64_stats_add(&stats->rx_bytes, len);
855 u64_stats_update_end(&stats->syncp);
856}
857
858static void macsec_decrypt_done(struct crypto_async_request *base, int err)
859{
860 struct sk_buff *skb = base->data;
861 struct net_device *dev = skb->dev;
862 struct macsec_dev *macsec = macsec_priv(dev);
863 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
864 struct macsec_rx_sc *rx_sc = rx_sa->sc;
865 int len;
866 u32 pn;
867
868 aead_request_free(macsec_skb_cb(skb)->req);
869
870 if (!err)
871 macsec_skb_cb(skb)->valid = true;
872
873 rcu_read_lock_bh();
874 pn = ntohl(macsec_ethhdr(skb)->packet_number);
875 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
876 rcu_read_unlock_bh();
877 kfree_skb(skb);
878 goto out;
879 }
880
881 macsec_finalize_skb(skb, macsec->secy.icv_len,
882 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
883 len = skb->len;
884 macsec_reset_skb(skb, macsec->secy.netdev);
885
886 if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
887 count_rx(dev, len);
888
889 rcu_read_unlock_bh();
890
891out:
892 macsec_rxsa_put(rx_sa);
893 macsec_rxsc_put(rx_sc);
894 dev_put(dev);
895}
896
897static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
898 struct net_device *dev,
899 struct macsec_rx_sa *rx_sa,
900 sci_t sci,
901 struct macsec_secy *secy)
902{
903 int ret;
904 struct scatterlist *sg;
905 struct sk_buff *trailer;
906 unsigned char *iv;
907 struct aead_request *req;
908 struct macsec_eth_header *hdr;
909 u32 hdr_pn;
910 u16 icv_len = secy->icv_len;
911
912 macsec_skb_cb(skb)->valid = false;
913 skb = skb_share_check(skb, GFP_ATOMIC);
914 if (!skb)
915 return ERR_PTR(-ENOMEM);
916
917 ret = skb_cow_data(skb, 0, &trailer);
918 if (unlikely(ret < 0)) {
919 kfree_skb(skb);
920 return ERR_PTR(ret);
921 }
922 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
923 if (!req) {
924 kfree_skb(skb);
925 return ERR_PTR(-ENOMEM);
926 }
927
928 hdr = (struct macsec_eth_header *)skb->data;
929 hdr_pn = ntohl(hdr->packet_number);
930
931 if (secy->xpn) {
932 pn_t recovered_pn = rx_sa->next_pn_halves;
933
934 recovered_pn.lower = hdr_pn;
935 if (hdr_pn < rx_sa->next_pn_halves.lower &&
936 !pn_same_half(hdr_pn, rx_sa->next_pn_halves.lower))
937 recovered_pn.upper++;
938
939 macsec_fill_iv_xpn(iv, rx_sa->ssci, recovered_pn.full64,
940 rx_sa->key.salt);
941 } else {
942 macsec_fill_iv(iv, sci, hdr_pn);
943 }
944
945 sg_init_table(sg, ret);
946 ret = skb_to_sgvec(skb, sg, 0, skb->len);
947 if (unlikely(ret < 0)) {
948 aead_request_free(req);
949 kfree_skb(skb);
950 return ERR_PTR(ret);
951 }
952
953 if (hdr->tci_an & MACSEC_TCI_E) {
954
955
956
957 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
958
959 aead_request_set_crypt(req, sg, sg, len, iv);
960 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
961 skb = skb_unshare(skb, GFP_ATOMIC);
962 if (!skb) {
963 aead_request_free(req);
964 return ERR_PTR(-ENOMEM);
965 }
966 } else {
967
968 aead_request_set_crypt(req, sg, sg, icv_len, iv);
969 aead_request_set_ad(req, skb->len - icv_len);
970 }
971
972 macsec_skb_cb(skb)->req = req;
973 skb->dev = dev;
974 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
975
976 dev_hold(dev);
977 ret = crypto_aead_decrypt(req);
978 if (ret == -EINPROGRESS) {
979 return ERR_PTR(ret);
980 } else if (ret != 0) {
981
982
983
984 if (ret != -EBADMSG) {
985 kfree_skb(skb);
986 skb = ERR_PTR(ret);
987 }
988 } else {
989 macsec_skb_cb(skb)->valid = true;
990 }
991 dev_put(dev);
992
993 aead_request_free(req);
994
995 return skb;
996}
997
998static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
999{
1000 struct macsec_rx_sc *rx_sc;
1001
1002 for_each_rxsc(secy, rx_sc) {
1003 if (rx_sc->sci == sci)
1004 return rx_sc;
1005 }
1006
1007 return NULL;
1008}
1009
1010static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1011{
1012 struct macsec_rx_sc *rx_sc;
1013
1014 for_each_rxsc_rtnl(secy, rx_sc) {
1015 if (rx_sc->sci == sci)
1016 return rx_sc;
1017 }
1018
1019 return NULL;
1020}
1021
1022static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
1023{
1024
1025 enum rx_handler_result ret = RX_HANDLER_PASS;
1026 struct ethhdr *hdr = eth_hdr(skb);
1027 struct macsec_rxh_data *rxd;
1028 struct macsec_dev *macsec;
1029
1030 rcu_read_lock();
1031 rxd = macsec_data_rcu(skb->dev);
1032
1033 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1034 struct sk_buff *nskb;
1035 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1036 struct net_device *ndev = macsec->secy.netdev;
1037
1038
1039
1040
1041 if (macsec_is_offloaded(macsec) && netif_running(ndev)) {
1042 if (ether_addr_equal_64bits(hdr->h_dest,
1043 ndev->dev_addr)) {
1044
1045 skb->dev = ndev;
1046 skb->pkt_type = PACKET_HOST;
1047 ret = RX_HANDLER_ANOTHER;
1048 goto out;
1049 } else if (is_multicast_ether_addr_64bits(
1050 hdr->h_dest)) {
1051
1052 nskb = skb_clone(skb, GFP_ATOMIC);
1053 if (!nskb)
1054 break;
1055
1056 nskb->dev = ndev;
1057 if (ether_addr_equal_64bits(hdr->h_dest,
1058 ndev->broadcast))
1059 nskb->pkt_type = PACKET_BROADCAST;
1060 else
1061 nskb->pkt_type = PACKET_MULTICAST;
1062
1063 __netif_rx(nskb);
1064 }
1065 continue;
1066 }
1067
1068
1069
1070
1071
1072 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1073 u64_stats_update_begin(&secy_stats->syncp);
1074 secy_stats->stats.InPktsNoTag++;
1075 u64_stats_update_end(&secy_stats->syncp);
1076 macsec->secy.netdev->stats.rx_dropped++;
1077 continue;
1078 }
1079
1080
1081 nskb = skb_clone(skb, GFP_ATOMIC);
1082 if (!nskb)
1083 break;
1084
1085 nskb->dev = ndev;
1086
1087 if (__netif_rx(nskb) == NET_RX_SUCCESS) {
1088 u64_stats_update_begin(&secy_stats->syncp);
1089 secy_stats->stats.InPktsUntagged++;
1090 u64_stats_update_end(&secy_stats->syncp);
1091 }
1092 }
1093
1094out:
1095 rcu_read_unlock();
1096 return ret;
1097}
1098
1099static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1100{
1101 struct sk_buff *skb = *pskb;
1102 struct net_device *dev = skb->dev;
1103 struct macsec_eth_header *hdr;
1104 struct macsec_secy *secy = NULL;
1105 struct macsec_rx_sc *rx_sc;
1106 struct macsec_rx_sa *rx_sa;
1107 struct macsec_rxh_data *rxd;
1108 struct macsec_dev *macsec;
1109 unsigned int len;
1110 sci_t sci;
1111 u32 hdr_pn;
1112 bool cbit;
1113 struct pcpu_rx_sc_stats *rxsc_stats;
1114 struct pcpu_secy_stats *secy_stats;
1115 bool pulled_sci;
1116 int ret;
1117
1118 if (skb_headroom(skb) < ETH_HLEN)
1119 goto drop_direct;
1120
1121 hdr = macsec_ethhdr(skb);
1122 if (hdr->eth.h_proto != htons(ETH_P_MACSEC))
1123 return handle_not_macsec(skb);
1124
1125 skb = skb_unshare(skb, GFP_ATOMIC);
1126 *pskb = skb;
1127 if (!skb)
1128 return RX_HANDLER_CONSUMED;
1129
1130 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1131 if (!pulled_sci) {
1132 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1133 goto drop_direct;
1134 }
1135
1136 hdr = macsec_ethhdr(skb);
1137
1138
1139
1140
1141
1142
1143 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1144 return RX_HANDLER_PASS;
1145
1146
1147 if (hdr->tci_an & MACSEC_TCI_SC) {
1148 if (!pulled_sci)
1149 goto drop_direct;
1150 }
1151
1152
1153 skb_push(skb, ETH_HLEN);
1154
1155 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1156 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1157 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1158
1159 rcu_read_lock();
1160 rxd = macsec_data_rcu(skb->dev);
1161
1162 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1163 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
1164
1165 sc = sc ? macsec_rxsc_get(sc) : NULL;
1166
1167 if (sc) {
1168 secy = &macsec->secy;
1169 rx_sc = sc;
1170 break;
1171 }
1172 }
1173
1174 if (!secy)
1175 goto nosci;
1176
1177 dev = secy->netdev;
1178 macsec = macsec_priv(dev);
1179 secy_stats = this_cpu_ptr(macsec->stats);
1180 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1181
1182 if (!macsec_validate_skb(skb, secy->icv_len, secy->xpn)) {
1183 u64_stats_update_begin(&secy_stats->syncp);
1184 secy_stats->stats.InPktsBadTag++;
1185 u64_stats_update_end(&secy_stats->syncp);
1186 secy->netdev->stats.rx_errors++;
1187 goto drop_nosa;
1188 }
1189
1190 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1191 if (!rx_sa) {
1192
1193
1194
1195
1196
1197 struct macsec_rx_sa *active_rx_sa = macsec_active_rxsa_get(rx_sc);
1198 if (hdr->tci_an & MACSEC_TCI_C ||
1199 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1200 u64_stats_update_begin(&rxsc_stats->syncp);
1201 rxsc_stats->stats.InPktsNotUsingSA++;
1202 u64_stats_update_end(&rxsc_stats->syncp);
1203 secy->netdev->stats.rx_errors++;
1204 if (active_rx_sa)
1205 this_cpu_inc(active_rx_sa->stats->InPktsNotUsingSA);
1206 goto drop_nosa;
1207 }
1208
1209
1210
1211
1212 u64_stats_update_begin(&rxsc_stats->syncp);
1213 rxsc_stats->stats.InPktsUnusedSA++;
1214 u64_stats_update_end(&rxsc_stats->syncp);
1215 if (active_rx_sa)
1216 this_cpu_inc(active_rx_sa->stats->InPktsUnusedSA);
1217 goto deliver;
1218 }
1219
1220
1221 hdr_pn = ntohl(hdr->packet_number);
1222 if (secy->replay_protect) {
1223 bool late;
1224
1225 spin_lock(&rx_sa->lock);
1226 late = rx_sa->next_pn_halves.lower >= secy->replay_window &&
1227 hdr_pn < (rx_sa->next_pn_halves.lower - secy->replay_window);
1228
1229 if (secy->xpn)
1230 late = late && pn_same_half(rx_sa->next_pn_halves.lower, hdr_pn);
1231 spin_unlock(&rx_sa->lock);
1232
1233 if (late) {
1234 u64_stats_update_begin(&rxsc_stats->syncp);
1235 rxsc_stats->stats.InPktsLate++;
1236 u64_stats_update_end(&rxsc_stats->syncp);
1237 macsec->secy.netdev->stats.rx_dropped++;
1238 goto drop;
1239 }
1240 }
1241
1242 macsec_skb_cb(skb)->rx_sa = rx_sa;
1243
1244
1245 if (hdr->tci_an & MACSEC_TCI_C ||
1246 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1247 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1248
1249 if (IS_ERR(skb)) {
1250
1251 if (PTR_ERR(skb) != -EINPROGRESS) {
1252 macsec_rxsa_put(rx_sa);
1253 macsec_rxsc_put(rx_sc);
1254 }
1255 rcu_read_unlock();
1256 *pskb = NULL;
1257 return RX_HANDLER_CONSUMED;
1258 }
1259
1260 if (!macsec_post_decrypt(skb, secy, hdr_pn))
1261 goto drop;
1262
1263deliver:
1264 macsec_finalize_skb(skb, secy->icv_len,
1265 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1266 len = skb->len;
1267 macsec_reset_skb(skb, secy->netdev);
1268
1269 if (rx_sa)
1270 macsec_rxsa_put(rx_sa);
1271 macsec_rxsc_put(rx_sc);
1272
1273 skb_orphan(skb);
1274 ret = gro_cells_receive(&macsec->gro_cells, skb);
1275 if (ret == NET_RX_SUCCESS)
1276 count_rx(dev, len);
1277 else
1278 macsec->secy.netdev->stats.rx_dropped++;
1279
1280 rcu_read_unlock();
1281
1282 *pskb = NULL;
1283 return RX_HANDLER_CONSUMED;
1284
1285drop:
1286 macsec_rxsa_put(rx_sa);
1287drop_nosa:
1288 macsec_rxsc_put(rx_sc);
1289 rcu_read_unlock();
1290drop_direct:
1291 kfree_skb(skb);
1292 *pskb = NULL;
1293 return RX_HANDLER_CONSUMED;
1294
1295nosci:
1296
1297 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1298 if (!cbit)
1299 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1300 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1301
1302 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1303 struct sk_buff *nskb;
1304
1305 secy_stats = this_cpu_ptr(macsec->stats);
1306
1307
1308
1309
1310 if (cbit ||
1311 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1312 u64_stats_update_begin(&secy_stats->syncp);
1313 secy_stats->stats.InPktsNoSCI++;
1314 u64_stats_update_end(&secy_stats->syncp);
1315 macsec->secy.netdev->stats.rx_errors++;
1316 continue;
1317 }
1318
1319
1320
1321
1322 nskb = skb_clone(skb, GFP_ATOMIC);
1323 if (!nskb)
1324 break;
1325
1326 macsec_reset_skb(nskb, macsec->secy.netdev);
1327
1328 ret = __netif_rx(nskb);
1329 if (ret == NET_RX_SUCCESS) {
1330 u64_stats_update_begin(&secy_stats->syncp);
1331 secy_stats->stats.InPktsUnknownSCI++;
1332 u64_stats_update_end(&secy_stats->syncp);
1333 } else {
1334 macsec->secy.netdev->stats.rx_dropped++;
1335 }
1336 }
1337
1338 rcu_read_unlock();
1339 *pskb = skb;
1340 return RX_HANDLER_PASS;
1341}
1342
1343static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1344{
1345 struct crypto_aead *tfm;
1346 int ret;
1347
1348
1349 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
1350
1351 if (IS_ERR(tfm))
1352 return tfm;
1353
1354 ret = crypto_aead_setkey(tfm, key, key_len);
1355 if (ret < 0)
1356 goto fail;
1357
1358 ret = crypto_aead_setauthsize(tfm, icv_len);
1359 if (ret < 0)
1360 goto fail;
1361
1362 return tfm;
1363fail:
1364 crypto_free_aead(tfm);
1365 return ERR_PTR(ret);
1366}
1367
1368static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1369 int icv_len)
1370{
1371 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1372 if (!rx_sa->stats)
1373 return -ENOMEM;
1374
1375 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1376 if (IS_ERR(rx_sa->key.tfm)) {
1377 free_percpu(rx_sa->stats);
1378 return PTR_ERR(rx_sa->key.tfm);
1379 }
1380
1381 rx_sa->ssci = MACSEC_UNDEF_SSCI;
1382 rx_sa->active = false;
1383 rx_sa->next_pn = 1;
1384 refcount_set(&rx_sa->refcnt, 1);
1385 spin_lock_init(&rx_sa->lock);
1386
1387 return 0;
1388}
1389
1390static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1391{
1392 rx_sa->active = false;
1393
1394 macsec_rxsa_put(rx_sa);
1395}
1396
1397static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1398{
1399 int i;
1400
1401 for (i = 0; i < MACSEC_NUM_AN; i++) {
1402 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1403
1404 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1405 if (sa)
1406 clear_rx_sa(sa);
1407 }
1408
1409 macsec_rxsc_put(rx_sc);
1410}
1411
1412static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1413{
1414 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1415
1416 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1417 rx_sc;
1418 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1419 if (rx_sc->sci == sci) {
1420 if (rx_sc->active)
1421 secy->n_rx_sc--;
1422 rcu_assign_pointer(*rx_scp, rx_sc->next);
1423 return rx_sc;
1424 }
1425 }
1426
1427 return NULL;
1428}
1429
1430static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci,
1431 bool active)
1432{
1433 struct macsec_rx_sc *rx_sc;
1434 struct macsec_dev *macsec;
1435 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1436 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1437 struct macsec_secy *secy;
1438
1439 list_for_each_entry(macsec, &rxd->secys, secys) {
1440 if (find_rx_sc_rtnl(&macsec->secy, sci))
1441 return ERR_PTR(-EEXIST);
1442 }
1443
1444 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1445 if (!rx_sc)
1446 return ERR_PTR(-ENOMEM);
1447
1448 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1449 if (!rx_sc->stats) {
1450 kfree(rx_sc);
1451 return ERR_PTR(-ENOMEM);
1452 }
1453
1454 rx_sc->sci = sci;
1455 rx_sc->active = active;
1456 refcount_set(&rx_sc->refcnt, 1);
1457
1458 secy = &macsec_priv(dev)->secy;
1459 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1460 rcu_assign_pointer(secy->rx_sc, rx_sc);
1461
1462 if (rx_sc->active)
1463 secy->n_rx_sc++;
1464
1465 return rx_sc;
1466}
1467
1468static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1469 int icv_len)
1470{
1471 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1472 if (!tx_sa->stats)
1473 return -ENOMEM;
1474
1475 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1476 if (IS_ERR(tx_sa->key.tfm)) {
1477 free_percpu(tx_sa->stats);
1478 return PTR_ERR(tx_sa->key.tfm);
1479 }
1480
1481 tx_sa->ssci = MACSEC_UNDEF_SSCI;
1482 tx_sa->active = false;
1483 refcount_set(&tx_sa->refcnt, 1);
1484 spin_lock_init(&tx_sa->lock);
1485
1486 return 0;
1487}
1488
1489static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1490{
1491 tx_sa->active = false;
1492
1493 macsec_txsa_put(tx_sa);
1494}
1495
1496static struct genl_family macsec_fam;
1497
1498static struct net_device *get_dev_from_nl(struct net *net,
1499 struct nlattr **attrs)
1500{
1501 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1502 struct net_device *dev;
1503
1504 dev = __dev_get_by_index(net, ifindex);
1505 if (!dev)
1506 return ERR_PTR(-ENODEV);
1507
1508 if (!netif_is_macsec(dev))
1509 return ERR_PTR(-ENODEV);
1510
1511 return dev;
1512}
1513
1514static enum macsec_offload nla_get_offload(const struct nlattr *nla)
1515{
1516 return (__force enum macsec_offload)nla_get_u8(nla);
1517}
1518
1519static sci_t nla_get_sci(const struct nlattr *nla)
1520{
1521 return (__force sci_t)nla_get_u64(nla);
1522}
1523
1524static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1525 int padattr)
1526{
1527 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1528}
1529
1530static ssci_t nla_get_ssci(const struct nlattr *nla)
1531{
1532 return (__force ssci_t)nla_get_u32(nla);
1533}
1534
1535static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value)
1536{
1537 return nla_put_u32(skb, attrtype, (__force u64)value);
1538}
1539
1540static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1541 struct nlattr **attrs,
1542 struct nlattr **tb_sa,
1543 struct net_device **devp,
1544 struct macsec_secy **secyp,
1545 struct macsec_tx_sc **scp,
1546 u8 *assoc_num)
1547{
1548 struct net_device *dev;
1549 struct macsec_secy *secy;
1550 struct macsec_tx_sc *tx_sc;
1551 struct macsec_tx_sa *tx_sa;
1552
1553 if (!tb_sa[MACSEC_SA_ATTR_AN])
1554 return ERR_PTR(-EINVAL);
1555
1556 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1557
1558 dev = get_dev_from_nl(net, attrs);
1559 if (IS_ERR(dev))
1560 return ERR_CAST(dev);
1561
1562 if (*assoc_num >= MACSEC_NUM_AN)
1563 return ERR_PTR(-EINVAL);
1564
1565 secy = &macsec_priv(dev)->secy;
1566 tx_sc = &secy->tx_sc;
1567
1568 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1569 if (!tx_sa)
1570 return ERR_PTR(-ENODEV);
1571
1572 *devp = dev;
1573 *scp = tx_sc;
1574 *secyp = secy;
1575 return tx_sa;
1576}
1577
1578static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1579 struct nlattr **attrs,
1580 struct nlattr **tb_rxsc,
1581 struct net_device **devp,
1582 struct macsec_secy **secyp)
1583{
1584 struct net_device *dev;
1585 struct macsec_secy *secy;
1586 struct macsec_rx_sc *rx_sc;
1587 sci_t sci;
1588
1589 dev = get_dev_from_nl(net, attrs);
1590 if (IS_ERR(dev))
1591 return ERR_CAST(dev);
1592
1593 secy = &macsec_priv(dev)->secy;
1594
1595 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1596 return ERR_PTR(-EINVAL);
1597
1598 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1599 rx_sc = find_rx_sc_rtnl(secy, sci);
1600 if (!rx_sc)
1601 return ERR_PTR(-ENODEV);
1602
1603 *secyp = secy;
1604 *devp = dev;
1605
1606 return rx_sc;
1607}
1608
1609static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1610 struct nlattr **attrs,
1611 struct nlattr **tb_rxsc,
1612 struct nlattr **tb_sa,
1613 struct net_device **devp,
1614 struct macsec_secy **secyp,
1615 struct macsec_rx_sc **scp,
1616 u8 *assoc_num)
1617{
1618 struct macsec_rx_sc *rx_sc;
1619 struct macsec_rx_sa *rx_sa;
1620
1621 if (!tb_sa[MACSEC_SA_ATTR_AN])
1622 return ERR_PTR(-EINVAL);
1623
1624 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1625 if (*assoc_num >= MACSEC_NUM_AN)
1626 return ERR_PTR(-EINVAL);
1627
1628 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1629 if (IS_ERR(rx_sc))
1630 return ERR_CAST(rx_sc);
1631
1632 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1633 if (!rx_sa)
1634 return ERR_PTR(-ENODEV);
1635
1636 *scp = rx_sc;
1637 return rx_sa;
1638}
1639
1640static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1641 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1642 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1643 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1644 [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
1645};
1646
1647static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1648 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1649 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1650};
1651
1652static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1653 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1654 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1655 [MACSEC_SA_ATTR_PN] = NLA_POLICY_MIN_LEN(4),
1656 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1657 .len = MACSEC_KEYID_LEN, },
1658 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1659 .len = MACSEC_MAX_KEY_LEN, },
1660 [MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 },
1661 [MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY,
1662 .len = MACSEC_SALT_LEN, },
1663};
1664
1665static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
1666 [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 },
1667};
1668
1669
1670static int macsec_offload(int (* const func)(struct macsec_context *),
1671 struct macsec_context *ctx)
1672{
1673 int ret;
1674
1675 if (unlikely(!func))
1676 return 0;
1677
1678 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1679 mutex_lock(&ctx->phydev->lock);
1680
1681
1682
1683
1684 ctx->prepare = true;
1685 ret = (*func)(ctx);
1686 if (ret)
1687 goto phy_unlock;
1688
1689
1690 ctx->prepare = false;
1691 ret = (*func)(ctx);
1692
1693 if (unlikely(ret))
1694 WARN(1, "MACsec offloading commit failed (%d)\n", ret);
1695
1696phy_unlock:
1697 if (ctx->offload == MACSEC_OFFLOAD_PHY)
1698 mutex_unlock(&ctx->phydev->lock);
1699
1700 return ret;
1701}
1702
1703static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1704{
1705 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1706 return -EINVAL;
1707
1708 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1709 return -EINVAL;
1710
1711 return 0;
1712}
1713
1714static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1715{
1716 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1717 return -EINVAL;
1718
1719 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1720 return -EINVAL;
1721
1722 return 0;
1723}
1724
1725static bool validate_add_rxsa(struct nlattr **attrs)
1726{
1727 if (!attrs[MACSEC_SA_ATTR_AN] ||
1728 !attrs[MACSEC_SA_ATTR_KEY] ||
1729 !attrs[MACSEC_SA_ATTR_KEYID])
1730 return false;
1731
1732 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1733 return false;
1734
1735 if (attrs[MACSEC_SA_ATTR_PN] &&
1736 nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
1737 return false;
1738
1739 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1740 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1741 return false;
1742 }
1743
1744 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1745 return false;
1746
1747 return true;
1748}
1749
1750static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1751{
1752 struct net_device *dev;
1753 struct nlattr **attrs = info->attrs;
1754 struct macsec_secy *secy;
1755 struct macsec_rx_sc *rx_sc;
1756 struct macsec_rx_sa *rx_sa;
1757 unsigned char assoc_num;
1758 int pn_len;
1759 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1760 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1761 int err;
1762
1763 if (!attrs[MACSEC_ATTR_IFINDEX])
1764 return -EINVAL;
1765
1766 if (parse_sa_config(attrs, tb_sa))
1767 return -EINVAL;
1768
1769 if (parse_rxsc_config(attrs, tb_rxsc))
1770 return -EINVAL;
1771
1772 if (!validate_add_rxsa(tb_sa))
1773 return -EINVAL;
1774
1775 rtnl_lock();
1776 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1777 if (IS_ERR(rx_sc)) {
1778 rtnl_unlock();
1779 return PTR_ERR(rx_sc);
1780 }
1781
1782 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1783
1784 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1785 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1786 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1787 rtnl_unlock();
1788 return -EINVAL;
1789 }
1790
1791 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
1792 if (tb_sa[MACSEC_SA_ATTR_PN] &&
1793 nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
1794 pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
1795 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
1796 rtnl_unlock();
1797 return -EINVAL;
1798 }
1799
1800 if (secy->xpn) {
1801 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
1802 rtnl_unlock();
1803 return -EINVAL;
1804 }
1805
1806 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
1807 pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n",
1808 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
1809 MACSEC_SALT_LEN);
1810 rtnl_unlock();
1811 return -EINVAL;
1812 }
1813 }
1814
1815 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1816 if (rx_sa) {
1817 rtnl_unlock();
1818 return -EBUSY;
1819 }
1820
1821 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1822 if (!rx_sa) {
1823 rtnl_unlock();
1824 return -ENOMEM;
1825 }
1826
1827 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1828 secy->key_len, secy->icv_len);
1829 if (err < 0) {
1830 kfree(rx_sa);
1831 rtnl_unlock();
1832 return err;
1833 }
1834
1835 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1836 spin_lock_bh(&rx_sa->lock);
1837 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
1838 spin_unlock_bh(&rx_sa->lock);
1839 }
1840
1841 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1842 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1843
1844 rx_sa->sc = rx_sc;
1845
1846
1847 if (macsec_is_offloaded(netdev_priv(dev))) {
1848 const struct macsec_ops *ops;
1849 struct macsec_context ctx;
1850
1851 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1852 if (!ops) {
1853 err = -EOPNOTSUPP;
1854 goto cleanup;
1855 }
1856
1857 ctx.sa.assoc_num = assoc_num;
1858 ctx.sa.rx_sa = rx_sa;
1859 ctx.secy = secy;
1860 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1861 secy->key_len);
1862
1863 err = macsec_offload(ops->mdo_add_rxsa, &ctx);
1864 memzero_explicit(ctx.sa.key, secy->key_len);
1865 if (err)
1866 goto cleanup;
1867 }
1868
1869 if (secy->xpn) {
1870 rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
1871 nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
1872 MACSEC_SALT_LEN);
1873 }
1874
1875 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1876 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1877
1878 rtnl_unlock();
1879
1880 return 0;
1881
1882cleanup:
1883 macsec_rxsa_put(rx_sa);
1884 rtnl_unlock();
1885 return err;
1886}
1887
1888static bool validate_add_rxsc(struct nlattr **attrs)
1889{
1890 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1891 return false;
1892
1893 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1894 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1895 return false;
1896 }
1897
1898 return true;
1899}
1900
1901static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1902{
1903 struct net_device *dev;
1904 sci_t sci = MACSEC_UNDEF_SCI;
1905 struct nlattr **attrs = info->attrs;
1906 struct macsec_rx_sc *rx_sc;
1907 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1908 struct macsec_secy *secy;
1909 bool active = true;
1910 int ret;
1911
1912 if (!attrs[MACSEC_ATTR_IFINDEX])
1913 return -EINVAL;
1914
1915 if (parse_rxsc_config(attrs, tb_rxsc))
1916 return -EINVAL;
1917
1918 if (!validate_add_rxsc(tb_rxsc))
1919 return -EINVAL;
1920
1921 rtnl_lock();
1922 dev = get_dev_from_nl(genl_info_net(info), attrs);
1923 if (IS_ERR(dev)) {
1924 rtnl_unlock();
1925 return PTR_ERR(dev);
1926 }
1927
1928 secy = &macsec_priv(dev)->secy;
1929 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1930
1931 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1932 active = nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1933
1934 rx_sc = create_rx_sc(dev, sci, active);
1935 if (IS_ERR(rx_sc)) {
1936 rtnl_unlock();
1937 return PTR_ERR(rx_sc);
1938 }
1939
1940 if (macsec_is_offloaded(netdev_priv(dev))) {
1941 const struct macsec_ops *ops;
1942 struct macsec_context ctx;
1943
1944 ops = macsec_get_ops(netdev_priv(dev), &ctx);
1945 if (!ops) {
1946 ret = -EOPNOTSUPP;
1947 goto cleanup;
1948 }
1949
1950 ctx.rx_sc = rx_sc;
1951 ctx.secy = secy;
1952
1953 ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
1954 if (ret)
1955 goto cleanup;
1956 }
1957
1958 rtnl_unlock();
1959
1960 return 0;
1961
1962cleanup:
1963 del_rx_sc(secy, sci);
1964 free_rx_sc(rx_sc);
1965 rtnl_unlock();
1966 return ret;
1967}
1968
1969static bool validate_add_txsa(struct nlattr **attrs)
1970{
1971 if (!attrs[MACSEC_SA_ATTR_AN] ||
1972 !attrs[MACSEC_SA_ATTR_PN] ||
1973 !attrs[MACSEC_SA_ATTR_KEY] ||
1974 !attrs[MACSEC_SA_ATTR_KEYID])
1975 return false;
1976
1977 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1978 return false;
1979
1980 if (nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
1981 return false;
1982
1983 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1984 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1985 return false;
1986 }
1987
1988 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1989 return false;
1990
1991 return true;
1992}
1993
1994static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1995{
1996 struct net_device *dev;
1997 struct nlattr **attrs = info->attrs;
1998 struct macsec_secy *secy;
1999 struct macsec_tx_sc *tx_sc;
2000 struct macsec_tx_sa *tx_sa;
2001 unsigned char assoc_num;
2002 int pn_len;
2003 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2004 bool was_operational;
2005 int err;
2006
2007 if (!attrs[MACSEC_ATTR_IFINDEX])
2008 return -EINVAL;
2009
2010 if (parse_sa_config(attrs, tb_sa))
2011 return -EINVAL;
2012
2013 if (!validate_add_txsa(tb_sa))
2014 return -EINVAL;
2015
2016 rtnl_lock();
2017 dev = get_dev_from_nl(genl_info_net(info), attrs);
2018 if (IS_ERR(dev)) {
2019 rtnl_unlock();
2020 return PTR_ERR(dev);
2021 }
2022
2023 secy = &macsec_priv(dev)->secy;
2024 tx_sc = &secy->tx_sc;
2025
2026 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
2027
2028 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
2029 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
2030 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
2031 rtnl_unlock();
2032 return -EINVAL;
2033 }
2034
2035 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2036 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2037 pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
2038 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2039 rtnl_unlock();
2040 return -EINVAL;
2041 }
2042
2043 if (secy->xpn) {
2044 if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
2045 rtnl_unlock();
2046 return -EINVAL;
2047 }
2048
2049 if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
2050 pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n",
2051 nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
2052 MACSEC_SALT_LEN);
2053 rtnl_unlock();
2054 return -EINVAL;
2055 }
2056 }
2057
2058 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
2059 if (tx_sa) {
2060 rtnl_unlock();
2061 return -EBUSY;
2062 }
2063
2064 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
2065 if (!tx_sa) {
2066 rtnl_unlock();
2067 return -ENOMEM;
2068 }
2069
2070 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2071 secy->key_len, secy->icv_len);
2072 if (err < 0) {
2073 kfree(tx_sa);
2074 rtnl_unlock();
2075 return err;
2076 }
2077
2078 spin_lock_bh(&tx_sa->lock);
2079 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2080 spin_unlock_bh(&tx_sa->lock);
2081
2082 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2083 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2084
2085 was_operational = secy->operational;
2086 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
2087 secy->operational = true;
2088
2089
2090 if (macsec_is_offloaded(netdev_priv(dev))) {
2091 const struct macsec_ops *ops;
2092 struct macsec_context ctx;
2093
2094 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2095 if (!ops) {
2096 err = -EOPNOTSUPP;
2097 goto cleanup;
2098 }
2099
2100 ctx.sa.assoc_num = assoc_num;
2101 ctx.sa.tx_sa = tx_sa;
2102 ctx.secy = secy;
2103 memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
2104 secy->key_len);
2105
2106 err = macsec_offload(ops->mdo_add_txsa, &ctx);
2107 memzero_explicit(ctx.sa.key, secy->key_len);
2108 if (err)
2109 goto cleanup;
2110 }
2111
2112 if (secy->xpn) {
2113 tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
2114 nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
2115 MACSEC_SALT_LEN);
2116 }
2117
2118 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
2119 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
2120
2121 rtnl_unlock();
2122
2123 return 0;
2124
2125cleanup:
2126 secy->operational = was_operational;
2127 macsec_txsa_put(tx_sa);
2128 rtnl_unlock();
2129 return err;
2130}
2131
2132static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
2133{
2134 struct nlattr **attrs = info->attrs;
2135 struct net_device *dev;
2136 struct macsec_secy *secy;
2137 struct macsec_rx_sc *rx_sc;
2138 struct macsec_rx_sa *rx_sa;
2139 u8 assoc_num;
2140 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2141 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2142 int ret;
2143
2144 if (!attrs[MACSEC_ATTR_IFINDEX])
2145 return -EINVAL;
2146
2147 if (parse_sa_config(attrs, tb_sa))
2148 return -EINVAL;
2149
2150 if (parse_rxsc_config(attrs, tb_rxsc))
2151 return -EINVAL;
2152
2153 rtnl_lock();
2154 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2155 &dev, &secy, &rx_sc, &assoc_num);
2156 if (IS_ERR(rx_sa)) {
2157 rtnl_unlock();
2158 return PTR_ERR(rx_sa);
2159 }
2160
2161 if (rx_sa->active) {
2162 rtnl_unlock();
2163 return -EBUSY;
2164 }
2165
2166
2167 if (macsec_is_offloaded(netdev_priv(dev))) {
2168 const struct macsec_ops *ops;
2169 struct macsec_context ctx;
2170
2171 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2172 if (!ops) {
2173 ret = -EOPNOTSUPP;
2174 goto cleanup;
2175 }
2176
2177 ctx.sa.assoc_num = assoc_num;
2178 ctx.sa.rx_sa = rx_sa;
2179 ctx.secy = secy;
2180
2181 ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
2182 if (ret)
2183 goto cleanup;
2184 }
2185
2186 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
2187 clear_rx_sa(rx_sa);
2188
2189 rtnl_unlock();
2190
2191 return 0;
2192
2193cleanup:
2194 rtnl_unlock();
2195 return ret;
2196}
2197
2198static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
2199{
2200 struct nlattr **attrs = info->attrs;
2201 struct net_device *dev;
2202 struct macsec_secy *secy;
2203 struct macsec_rx_sc *rx_sc;
2204 sci_t sci;
2205 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2206 int ret;
2207
2208 if (!attrs[MACSEC_ATTR_IFINDEX])
2209 return -EINVAL;
2210
2211 if (parse_rxsc_config(attrs, tb_rxsc))
2212 return -EINVAL;
2213
2214 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
2215 return -EINVAL;
2216
2217 rtnl_lock();
2218 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
2219 if (IS_ERR(dev)) {
2220 rtnl_unlock();
2221 return PTR_ERR(dev);
2222 }
2223
2224 secy = &macsec_priv(dev)->secy;
2225 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
2226
2227 rx_sc = del_rx_sc(secy, sci);
2228 if (!rx_sc) {
2229 rtnl_unlock();
2230 return -ENODEV;
2231 }
2232
2233
2234 if (macsec_is_offloaded(netdev_priv(dev))) {
2235 const struct macsec_ops *ops;
2236 struct macsec_context ctx;
2237
2238 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2239 if (!ops) {
2240 ret = -EOPNOTSUPP;
2241 goto cleanup;
2242 }
2243
2244 ctx.rx_sc = rx_sc;
2245 ctx.secy = secy;
2246 ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
2247 if (ret)
2248 goto cleanup;
2249 }
2250
2251 free_rx_sc(rx_sc);
2252 rtnl_unlock();
2253
2254 return 0;
2255
2256cleanup:
2257 rtnl_unlock();
2258 return ret;
2259}
2260
2261static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
2262{
2263 struct nlattr **attrs = info->attrs;
2264 struct net_device *dev;
2265 struct macsec_secy *secy;
2266 struct macsec_tx_sc *tx_sc;
2267 struct macsec_tx_sa *tx_sa;
2268 u8 assoc_num;
2269 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2270 int ret;
2271
2272 if (!attrs[MACSEC_ATTR_IFINDEX])
2273 return -EINVAL;
2274
2275 if (parse_sa_config(attrs, tb_sa))
2276 return -EINVAL;
2277
2278 rtnl_lock();
2279 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2280 &dev, &secy, &tx_sc, &assoc_num);
2281 if (IS_ERR(tx_sa)) {
2282 rtnl_unlock();
2283 return PTR_ERR(tx_sa);
2284 }
2285
2286 if (tx_sa->active) {
2287 rtnl_unlock();
2288 return -EBUSY;
2289 }
2290
2291
2292 if (macsec_is_offloaded(netdev_priv(dev))) {
2293 const struct macsec_ops *ops;
2294 struct macsec_context ctx;
2295
2296 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2297 if (!ops) {
2298 ret = -EOPNOTSUPP;
2299 goto cleanup;
2300 }
2301
2302 ctx.sa.assoc_num = assoc_num;
2303 ctx.sa.tx_sa = tx_sa;
2304 ctx.secy = secy;
2305
2306 ret = macsec_offload(ops->mdo_del_txsa, &ctx);
2307 if (ret)
2308 goto cleanup;
2309 }
2310
2311 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2312 clear_tx_sa(tx_sa);
2313
2314 rtnl_unlock();
2315
2316 return 0;
2317
2318cleanup:
2319 rtnl_unlock();
2320 return ret;
2321}
2322
2323static bool validate_upd_sa(struct nlattr **attrs)
2324{
2325 if (!attrs[MACSEC_SA_ATTR_AN] ||
2326 attrs[MACSEC_SA_ATTR_KEY] ||
2327 attrs[MACSEC_SA_ATTR_KEYID] ||
2328 attrs[MACSEC_SA_ATTR_SSCI] ||
2329 attrs[MACSEC_SA_ATTR_SALT])
2330 return false;
2331
2332 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2333 return false;
2334
2335 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
2336 return false;
2337
2338 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2339 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2340 return false;
2341 }
2342
2343 return true;
2344}
2345
2346static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2347{
2348 struct nlattr **attrs = info->attrs;
2349 struct net_device *dev;
2350 struct macsec_secy *secy;
2351 struct macsec_tx_sc *tx_sc;
2352 struct macsec_tx_sa *tx_sa;
2353 u8 assoc_num;
2354 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2355 bool was_operational, was_active;
2356 pn_t prev_pn;
2357 int ret = 0;
2358
2359 prev_pn.full64 = 0;
2360
2361 if (!attrs[MACSEC_ATTR_IFINDEX])
2362 return -EINVAL;
2363
2364 if (parse_sa_config(attrs, tb_sa))
2365 return -EINVAL;
2366
2367 if (!validate_upd_sa(tb_sa))
2368 return -EINVAL;
2369
2370 rtnl_lock();
2371 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2372 &dev, &secy, &tx_sc, &assoc_num);
2373 if (IS_ERR(tx_sa)) {
2374 rtnl_unlock();
2375 return PTR_ERR(tx_sa);
2376 }
2377
2378 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2379 int pn_len;
2380
2381 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2382 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2383 pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
2384 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2385 rtnl_unlock();
2386 return -EINVAL;
2387 }
2388
2389 spin_lock_bh(&tx_sa->lock);
2390 prev_pn = tx_sa->next_pn_halves;
2391 tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2392 spin_unlock_bh(&tx_sa->lock);
2393 }
2394
2395 was_active = tx_sa->active;
2396 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2397 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2398
2399 was_operational = secy->operational;
2400 if (assoc_num == tx_sc->encoding_sa)
2401 secy->operational = tx_sa->active;
2402
2403
2404 if (macsec_is_offloaded(netdev_priv(dev))) {
2405 const struct macsec_ops *ops;
2406 struct macsec_context ctx;
2407
2408 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2409 if (!ops) {
2410 ret = -EOPNOTSUPP;
2411 goto cleanup;
2412 }
2413
2414 ctx.sa.assoc_num = assoc_num;
2415 ctx.sa.tx_sa = tx_sa;
2416 ctx.secy = secy;
2417
2418 ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
2419 if (ret)
2420 goto cleanup;
2421 }
2422
2423 rtnl_unlock();
2424
2425 return 0;
2426
2427cleanup:
2428 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2429 spin_lock_bh(&tx_sa->lock);
2430 tx_sa->next_pn_halves = prev_pn;
2431 spin_unlock_bh(&tx_sa->lock);
2432 }
2433 tx_sa->active = was_active;
2434 secy->operational = was_operational;
2435 rtnl_unlock();
2436 return ret;
2437}
2438
2439static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2440{
2441 struct nlattr **attrs = info->attrs;
2442 struct net_device *dev;
2443 struct macsec_secy *secy;
2444 struct macsec_rx_sc *rx_sc;
2445 struct macsec_rx_sa *rx_sa;
2446 u8 assoc_num;
2447 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2448 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2449 bool was_active;
2450 pn_t prev_pn;
2451 int ret = 0;
2452
2453 prev_pn.full64 = 0;
2454
2455 if (!attrs[MACSEC_ATTR_IFINDEX])
2456 return -EINVAL;
2457
2458 if (parse_rxsc_config(attrs, tb_rxsc))
2459 return -EINVAL;
2460
2461 if (parse_sa_config(attrs, tb_sa))
2462 return -EINVAL;
2463
2464 if (!validate_upd_sa(tb_sa))
2465 return -EINVAL;
2466
2467 rtnl_lock();
2468 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2469 &dev, &secy, &rx_sc, &assoc_num);
2470 if (IS_ERR(rx_sa)) {
2471 rtnl_unlock();
2472 return PTR_ERR(rx_sa);
2473 }
2474
2475 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2476 int pn_len;
2477
2478 pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
2479 if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
2480 pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
2481 nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
2482 rtnl_unlock();
2483 return -EINVAL;
2484 }
2485
2486 spin_lock_bh(&rx_sa->lock);
2487 prev_pn = rx_sa->next_pn_halves;
2488 rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
2489 spin_unlock_bh(&rx_sa->lock);
2490 }
2491
2492 was_active = rx_sa->active;
2493 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2494 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2495
2496
2497 if (macsec_is_offloaded(netdev_priv(dev))) {
2498 const struct macsec_ops *ops;
2499 struct macsec_context ctx;
2500
2501 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2502 if (!ops) {
2503 ret = -EOPNOTSUPP;
2504 goto cleanup;
2505 }
2506
2507 ctx.sa.assoc_num = assoc_num;
2508 ctx.sa.rx_sa = rx_sa;
2509 ctx.secy = secy;
2510
2511 ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
2512 if (ret)
2513 goto cleanup;
2514 }
2515
2516 rtnl_unlock();
2517 return 0;
2518
2519cleanup:
2520 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2521 spin_lock_bh(&rx_sa->lock);
2522 rx_sa->next_pn_halves = prev_pn;
2523 spin_unlock_bh(&rx_sa->lock);
2524 }
2525 rx_sa->active = was_active;
2526 rtnl_unlock();
2527 return ret;
2528}
2529
2530static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2531{
2532 struct nlattr **attrs = info->attrs;
2533 struct net_device *dev;
2534 struct macsec_secy *secy;
2535 struct macsec_rx_sc *rx_sc;
2536 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2537 unsigned int prev_n_rx_sc;
2538 bool was_active;
2539 int ret;
2540
2541 if (!attrs[MACSEC_ATTR_IFINDEX])
2542 return -EINVAL;
2543
2544 if (parse_rxsc_config(attrs, tb_rxsc))
2545 return -EINVAL;
2546
2547 if (!validate_add_rxsc(tb_rxsc))
2548 return -EINVAL;
2549
2550 rtnl_lock();
2551 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2552 if (IS_ERR(rx_sc)) {
2553 rtnl_unlock();
2554 return PTR_ERR(rx_sc);
2555 }
2556
2557 was_active = rx_sc->active;
2558 prev_n_rx_sc = secy->n_rx_sc;
2559 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2560 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2561
2562 if (rx_sc->active != new)
2563 secy->n_rx_sc += new ? 1 : -1;
2564
2565 rx_sc->active = new;
2566 }
2567
2568
2569 if (macsec_is_offloaded(netdev_priv(dev))) {
2570 const struct macsec_ops *ops;
2571 struct macsec_context ctx;
2572
2573 ops = macsec_get_ops(netdev_priv(dev), &ctx);
2574 if (!ops) {
2575 ret = -EOPNOTSUPP;
2576 goto cleanup;
2577 }
2578
2579 ctx.rx_sc = rx_sc;
2580 ctx.secy = secy;
2581
2582 ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
2583 if (ret)
2584 goto cleanup;
2585 }
2586
2587 rtnl_unlock();
2588
2589 return 0;
2590
2591cleanup:
2592 secy->n_rx_sc = prev_n_rx_sc;
2593 rx_sc->active = was_active;
2594 rtnl_unlock();
2595 return ret;
2596}
2597
2598static bool macsec_is_configured(struct macsec_dev *macsec)
2599{
2600 struct macsec_secy *secy = &macsec->secy;
2601 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2602 int i;
2603
2604 if (secy->rx_sc)
2605 return true;
2606
2607 for (i = 0; i < MACSEC_NUM_AN; i++)
2608 if (tx_sc->sa[i])
2609 return true;
2610
2611 return false;
2612}
2613
2614static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
2615{
2616 struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
2617 enum macsec_offload offload, prev_offload;
2618 int (*func)(struct macsec_context *ctx);
2619 struct nlattr **attrs = info->attrs;
2620 struct net_device *dev;
2621 const struct macsec_ops *ops;
2622 struct macsec_context ctx;
2623 struct macsec_dev *macsec;
2624 int ret;
2625
2626 if (!attrs[MACSEC_ATTR_IFINDEX])
2627 return -EINVAL;
2628
2629 if (!attrs[MACSEC_ATTR_OFFLOAD])
2630 return -EINVAL;
2631
2632 if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
2633 attrs[MACSEC_ATTR_OFFLOAD],
2634 macsec_genl_offload_policy, NULL))
2635 return -EINVAL;
2636
2637 dev = get_dev_from_nl(genl_info_net(info), attrs);
2638 if (IS_ERR(dev))
2639 return PTR_ERR(dev);
2640 macsec = macsec_priv(dev);
2641
2642 if (!tb_offload[MACSEC_OFFLOAD_ATTR_TYPE])
2643 return -EINVAL;
2644
2645 offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
2646 if (macsec->offload == offload)
2647 return 0;
2648
2649
2650 if (offload != MACSEC_OFFLOAD_OFF &&
2651 !macsec_check_offload(offload, macsec))
2652 return -EOPNOTSUPP;
2653
2654
2655 if (netif_running(dev))
2656 return -EBUSY;
2657
2658 rtnl_lock();
2659
2660 prev_offload = macsec->offload;
2661 macsec->offload = offload;
2662
2663
2664
2665
2666 if (macsec_is_configured(macsec)) {
2667 ret = -EBUSY;
2668 goto rollback;
2669 }
2670
2671 ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
2672 macsec, &ctx);
2673 if (!ops) {
2674 ret = -EOPNOTSUPP;
2675 goto rollback;
2676 }
2677
2678 if (prev_offload == MACSEC_OFFLOAD_OFF)
2679 func = ops->mdo_add_secy;
2680 else
2681 func = ops->mdo_del_secy;
2682
2683 ctx.secy = &macsec->secy;
2684 ret = macsec_offload(func, &ctx);
2685 if (ret)
2686 goto rollback;
2687
2688
2689
2690
2691 netdev_update_features(dev);
2692
2693 rtnl_unlock();
2694 return 0;
2695
2696rollback:
2697 macsec->offload = prev_offload;
2698
2699 rtnl_unlock();
2700 return ret;
2701}
2702
2703static void get_tx_sa_stats(struct net_device *dev, int an,
2704 struct macsec_tx_sa *tx_sa,
2705 struct macsec_tx_sa_stats *sum)
2706{
2707 struct macsec_dev *macsec = macsec_priv(dev);
2708 int cpu;
2709
2710
2711 if (macsec_is_offloaded(macsec)) {
2712 const struct macsec_ops *ops;
2713 struct macsec_context ctx;
2714
2715 ops = macsec_get_ops(macsec, &ctx);
2716 if (ops) {
2717 ctx.sa.assoc_num = an;
2718 ctx.sa.tx_sa = tx_sa;
2719 ctx.stats.tx_sa_stats = sum;
2720 ctx.secy = &macsec_priv(dev)->secy;
2721 macsec_offload(ops->mdo_get_tx_sa_stats, &ctx);
2722 }
2723 return;
2724 }
2725
2726 for_each_possible_cpu(cpu) {
2727 const struct macsec_tx_sa_stats *stats =
2728 per_cpu_ptr(tx_sa->stats, cpu);
2729
2730 sum->OutPktsProtected += stats->OutPktsProtected;
2731 sum->OutPktsEncrypted += stats->OutPktsEncrypted;
2732 }
2733}
2734
2735static int copy_tx_sa_stats(struct sk_buff *skb, struct macsec_tx_sa_stats *sum)
2736{
2737 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED,
2738 sum->OutPktsProtected) ||
2739 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2740 sum->OutPktsEncrypted))
2741 return -EMSGSIZE;
2742
2743 return 0;
2744}
2745
2746static void get_rx_sa_stats(struct net_device *dev,
2747 struct macsec_rx_sc *rx_sc, int an,
2748 struct macsec_rx_sa *rx_sa,
2749 struct macsec_rx_sa_stats *sum)
2750{
2751 struct macsec_dev *macsec = macsec_priv(dev);
2752 int cpu;
2753
2754
2755 if (macsec_is_offloaded(macsec)) {
2756 const struct macsec_ops *ops;
2757 struct macsec_context ctx;
2758
2759 ops = macsec_get_ops(macsec, &ctx);
2760 if (ops) {
2761 ctx.sa.assoc_num = an;
2762 ctx.sa.rx_sa = rx_sa;
2763 ctx.stats.rx_sa_stats = sum;
2764 ctx.secy = &macsec_priv(dev)->secy;
2765 ctx.rx_sc = rx_sc;
2766 macsec_offload(ops->mdo_get_rx_sa_stats, &ctx);
2767 }
2768 return;
2769 }
2770
2771 for_each_possible_cpu(cpu) {
2772 const struct macsec_rx_sa_stats *stats =
2773 per_cpu_ptr(rx_sa->stats, cpu);
2774
2775 sum->InPktsOK += stats->InPktsOK;
2776 sum->InPktsInvalid += stats->InPktsInvalid;
2777 sum->InPktsNotValid += stats->InPktsNotValid;
2778 sum->InPktsNotUsingSA += stats->InPktsNotUsingSA;
2779 sum->InPktsUnusedSA += stats->InPktsUnusedSA;
2780 }
2781}
2782
2783static int copy_rx_sa_stats(struct sk_buff *skb,
2784 struct macsec_rx_sa_stats *sum)
2785{
2786 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum->InPktsOK) ||
2787 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID,
2788 sum->InPktsInvalid) ||
2789 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID,
2790 sum->InPktsNotValid) ||
2791 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2792 sum->InPktsNotUsingSA) ||
2793 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA,
2794 sum->InPktsUnusedSA))
2795 return -EMSGSIZE;
2796
2797 return 0;
2798}
2799
2800static void get_rx_sc_stats(struct net_device *dev,
2801 struct macsec_rx_sc *rx_sc,
2802 struct macsec_rx_sc_stats *sum)
2803{
2804 struct macsec_dev *macsec = macsec_priv(dev);
2805 int cpu;
2806
2807
2808 if (macsec_is_offloaded(macsec)) {
2809 const struct macsec_ops *ops;
2810 struct macsec_context ctx;
2811
2812 ops = macsec_get_ops(macsec, &ctx);
2813 if (ops) {
2814 ctx.stats.rx_sc_stats = sum;
2815 ctx.secy = &macsec_priv(dev)->secy;
2816 ctx.rx_sc = rx_sc;
2817 macsec_offload(ops->mdo_get_rx_sc_stats, &ctx);
2818 }
2819 return;
2820 }
2821
2822 for_each_possible_cpu(cpu) {
2823 const struct pcpu_rx_sc_stats *stats;
2824 struct macsec_rx_sc_stats tmp;
2825 unsigned int start;
2826
2827 stats = per_cpu_ptr(rx_sc->stats, cpu);
2828 do {
2829 start = u64_stats_fetch_begin_irq(&stats->syncp);
2830 memcpy(&tmp, &stats->stats, sizeof(tmp));
2831 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2832
2833 sum->InOctetsValidated += tmp.InOctetsValidated;
2834 sum->InOctetsDecrypted += tmp.InOctetsDecrypted;
2835 sum->InPktsUnchecked += tmp.InPktsUnchecked;
2836 sum->InPktsDelayed += tmp.InPktsDelayed;
2837 sum->InPktsOK += tmp.InPktsOK;
2838 sum->InPktsInvalid += tmp.InPktsInvalid;
2839 sum->InPktsLate += tmp.InPktsLate;
2840 sum->InPktsNotValid += tmp.InPktsNotValid;
2841 sum->InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2842 sum->InPktsUnusedSA += tmp.InPktsUnusedSA;
2843 }
2844}
2845
2846static int copy_rx_sc_stats(struct sk_buff *skb, struct macsec_rx_sc_stats *sum)
2847{
2848 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2849 sum->InOctetsValidated,
2850 MACSEC_RXSC_STATS_ATTR_PAD) ||
2851 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2852 sum->InOctetsDecrypted,
2853 MACSEC_RXSC_STATS_ATTR_PAD) ||
2854 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2855 sum->InPktsUnchecked,
2856 MACSEC_RXSC_STATS_ATTR_PAD) ||
2857 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2858 sum->InPktsDelayed,
2859 MACSEC_RXSC_STATS_ATTR_PAD) ||
2860 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2861 sum->InPktsOK,
2862 MACSEC_RXSC_STATS_ATTR_PAD) ||
2863 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2864 sum->InPktsInvalid,
2865 MACSEC_RXSC_STATS_ATTR_PAD) ||
2866 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2867 sum->InPktsLate,
2868 MACSEC_RXSC_STATS_ATTR_PAD) ||
2869 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2870 sum->InPktsNotValid,
2871 MACSEC_RXSC_STATS_ATTR_PAD) ||
2872 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2873 sum->InPktsNotUsingSA,
2874 MACSEC_RXSC_STATS_ATTR_PAD) ||
2875 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2876 sum->InPktsUnusedSA,
2877 MACSEC_RXSC_STATS_ATTR_PAD))
2878 return -EMSGSIZE;
2879
2880 return 0;
2881}
2882
2883static void get_tx_sc_stats(struct net_device *dev,
2884 struct macsec_tx_sc_stats *sum)
2885{
2886 struct macsec_dev *macsec = macsec_priv(dev);
2887 int cpu;
2888
2889
2890 if (macsec_is_offloaded(macsec)) {
2891 const struct macsec_ops *ops;
2892 struct macsec_context ctx;
2893
2894 ops = macsec_get_ops(macsec, &ctx);
2895 if (ops) {
2896 ctx.stats.tx_sc_stats = sum;
2897 ctx.secy = &macsec_priv(dev)->secy;
2898 macsec_offload(ops->mdo_get_tx_sc_stats, &ctx);
2899 }
2900 return;
2901 }
2902
2903 for_each_possible_cpu(cpu) {
2904 const struct pcpu_tx_sc_stats *stats;
2905 struct macsec_tx_sc_stats tmp;
2906 unsigned int start;
2907
2908 stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu);
2909 do {
2910 start = u64_stats_fetch_begin_irq(&stats->syncp);
2911 memcpy(&tmp, &stats->stats, sizeof(tmp));
2912 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2913
2914 sum->OutPktsProtected += tmp.OutPktsProtected;
2915 sum->OutPktsEncrypted += tmp.OutPktsEncrypted;
2916 sum->OutOctetsProtected += tmp.OutOctetsProtected;
2917 sum->OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2918 }
2919}
2920
2921static int copy_tx_sc_stats(struct sk_buff *skb, struct macsec_tx_sc_stats *sum)
2922{
2923 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2924 sum->OutPktsProtected,
2925 MACSEC_TXSC_STATS_ATTR_PAD) ||
2926 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2927 sum->OutPktsEncrypted,
2928 MACSEC_TXSC_STATS_ATTR_PAD) ||
2929 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2930 sum->OutOctetsProtected,
2931 MACSEC_TXSC_STATS_ATTR_PAD) ||
2932 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2933 sum->OutOctetsEncrypted,
2934 MACSEC_TXSC_STATS_ATTR_PAD))
2935 return -EMSGSIZE;
2936
2937 return 0;
2938}
2939
2940static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum)
2941{
2942 struct macsec_dev *macsec = macsec_priv(dev);
2943 int cpu;
2944
2945
2946 if (macsec_is_offloaded(macsec)) {
2947 const struct macsec_ops *ops;
2948 struct macsec_context ctx;
2949
2950 ops = macsec_get_ops(macsec, &ctx);
2951 if (ops) {
2952 ctx.stats.dev_stats = sum;
2953 ctx.secy = &macsec_priv(dev)->secy;
2954 macsec_offload(ops->mdo_get_dev_stats, &ctx);
2955 }
2956 return;
2957 }
2958
2959 for_each_possible_cpu(cpu) {
2960 const struct pcpu_secy_stats *stats;
2961 struct macsec_dev_stats tmp;
2962 unsigned int start;
2963
2964 stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu);
2965 do {
2966 start = u64_stats_fetch_begin_irq(&stats->syncp);
2967 memcpy(&tmp, &stats->stats, sizeof(tmp));
2968 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2969
2970 sum->OutPktsUntagged += tmp.OutPktsUntagged;
2971 sum->InPktsUntagged += tmp.InPktsUntagged;
2972 sum->OutPktsTooLong += tmp.OutPktsTooLong;
2973 sum->InPktsNoTag += tmp.InPktsNoTag;
2974 sum->InPktsBadTag += tmp.InPktsBadTag;
2975 sum->InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2976 sum->InPktsNoSCI += tmp.InPktsNoSCI;
2977 sum->InPktsOverrun += tmp.InPktsOverrun;
2978 }
2979}
2980
2981static int copy_secy_stats(struct sk_buff *skb, struct macsec_dev_stats *sum)
2982{
2983 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2984 sum->OutPktsUntagged,
2985 MACSEC_SECY_STATS_ATTR_PAD) ||
2986 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2987 sum->InPktsUntagged,
2988 MACSEC_SECY_STATS_ATTR_PAD) ||
2989 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2990 sum->OutPktsTooLong,
2991 MACSEC_SECY_STATS_ATTR_PAD) ||
2992 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2993 sum->InPktsNoTag,
2994 MACSEC_SECY_STATS_ATTR_PAD) ||
2995 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2996 sum->InPktsBadTag,
2997 MACSEC_SECY_STATS_ATTR_PAD) ||
2998 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2999 sum->InPktsUnknownSCI,
3000 MACSEC_SECY_STATS_ATTR_PAD) ||
3001 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
3002 sum->InPktsNoSCI,
3003 MACSEC_SECY_STATS_ATTR_PAD) ||
3004 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
3005 sum->InPktsOverrun,
3006 MACSEC_SECY_STATS_ATTR_PAD))
3007 return -EMSGSIZE;
3008
3009 return 0;
3010}
3011
3012static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
3013{
3014 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3015 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
3016 MACSEC_ATTR_SECY);
3017 u64 csid;
3018
3019 if (!secy_nest)
3020 return 1;
3021
3022 switch (secy->key_len) {
3023 case MACSEC_GCM_AES_128_SAK_LEN:
3024 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
3025 break;
3026 case MACSEC_GCM_AES_256_SAK_LEN:
3027 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
3028 break;
3029 default:
3030 goto cancel;
3031 }
3032
3033 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
3034 MACSEC_SECY_ATTR_PAD) ||
3035 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
3036 csid, MACSEC_SECY_ATTR_PAD) ||
3037 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
3038 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
3039 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
3040 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
3041 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
3042 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
3043 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
3044 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
3045 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
3046 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
3047 goto cancel;
3048
3049 if (secy->replay_protect) {
3050 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
3051 goto cancel;
3052 }
3053
3054 nla_nest_end(skb, secy_nest);
3055 return 0;
3056
3057cancel:
3058 nla_nest_cancel(skb, secy_nest);
3059 return 1;
3060}
3061
3062static noinline_for_stack int
3063dump_secy(struct macsec_secy *secy, struct net_device *dev,
3064 struct sk_buff *skb, struct netlink_callback *cb)
3065{
3066 struct macsec_tx_sc_stats tx_sc_stats = {0, };
3067 struct macsec_tx_sa_stats tx_sa_stats = {0, };
3068 struct macsec_rx_sc_stats rx_sc_stats = {0, };
3069 struct macsec_rx_sa_stats rx_sa_stats = {0, };
3070 struct macsec_dev *macsec = netdev_priv(dev);
3071 struct macsec_dev_stats dev_stats = {0, };
3072 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3073 struct nlattr *txsa_list, *rxsc_list;
3074 struct macsec_rx_sc *rx_sc;
3075 struct nlattr *attr;
3076 void *hdr;
3077 int i, j;
3078
3079 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3080 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
3081 if (!hdr)
3082 return -EMSGSIZE;
3083
3084 genl_dump_check_consistent(cb, hdr);
3085
3086 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
3087 goto nla_put_failure;
3088
3089 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
3090 if (!attr)
3091 goto nla_put_failure;
3092 if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
3093 goto nla_put_failure;
3094 nla_nest_end(skb, attr);
3095
3096 if (nla_put_secy(secy, skb))
3097 goto nla_put_failure;
3098
3099 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
3100 if (!attr)
3101 goto nla_put_failure;
3102
3103 get_tx_sc_stats(dev, &tx_sc_stats);
3104 if (copy_tx_sc_stats(skb, &tx_sc_stats)) {
3105 nla_nest_cancel(skb, attr);
3106 goto nla_put_failure;
3107 }
3108 nla_nest_end(skb, attr);
3109
3110 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
3111 if (!attr)
3112 goto nla_put_failure;
3113 get_secy_stats(dev, &dev_stats);
3114 if (copy_secy_stats(skb, &dev_stats)) {
3115 nla_nest_cancel(skb, attr);
3116 goto nla_put_failure;
3117 }
3118 nla_nest_end(skb, attr);
3119
3120 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
3121 if (!txsa_list)
3122 goto nla_put_failure;
3123 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
3124 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
3125 struct nlattr *txsa_nest;
3126 u64 pn;
3127 int pn_len;
3128
3129 if (!tx_sa)
3130 continue;
3131
3132 txsa_nest = nla_nest_start_noflag(skb, j++);
3133 if (!txsa_nest) {
3134 nla_nest_cancel(skb, txsa_list);
3135 goto nla_put_failure;
3136 }
3137
3138 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
3139 if (!attr) {
3140 nla_nest_cancel(skb, txsa_nest);
3141 nla_nest_cancel(skb, txsa_list);
3142 goto nla_put_failure;
3143 }
3144 memset(&tx_sa_stats, 0, sizeof(tx_sa_stats));
3145 get_tx_sa_stats(dev, i, tx_sa, &tx_sa_stats);
3146 if (copy_tx_sa_stats(skb, &tx_sa_stats)) {
3147 nla_nest_cancel(skb, attr);
3148 nla_nest_cancel(skb, txsa_nest);
3149 nla_nest_cancel(skb, txsa_list);
3150 goto nla_put_failure;
3151 }
3152 nla_nest_end(skb, attr);
3153
3154 if (secy->xpn) {
3155 pn = tx_sa->next_pn;
3156 pn_len = MACSEC_XPN_PN_LEN;
3157 } else {
3158 pn = tx_sa->next_pn_halves.lower;
3159 pn_len = MACSEC_DEFAULT_PN_LEN;
3160 }
3161
3162 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3163 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3164 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
3165 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) ||
3166 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
3167 nla_nest_cancel(skb, txsa_nest);
3168 nla_nest_cancel(skb, txsa_list);
3169 goto nla_put_failure;
3170 }
3171
3172 nla_nest_end(skb, txsa_nest);
3173 }
3174 nla_nest_end(skb, txsa_list);
3175
3176 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
3177 if (!rxsc_list)
3178 goto nla_put_failure;
3179
3180 j = 1;
3181 for_each_rxsc_rtnl(secy, rx_sc) {
3182 int k;
3183 struct nlattr *rxsa_list;
3184 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
3185
3186 if (!rxsc_nest) {
3187 nla_nest_cancel(skb, rxsc_list);
3188 goto nla_put_failure;
3189 }
3190
3191 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
3192 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
3193 MACSEC_RXSC_ATTR_PAD)) {
3194 nla_nest_cancel(skb, rxsc_nest);
3195 nla_nest_cancel(skb, rxsc_list);
3196 goto nla_put_failure;
3197 }
3198
3199 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
3200 if (!attr) {
3201 nla_nest_cancel(skb, rxsc_nest);
3202 nla_nest_cancel(skb, rxsc_list);
3203 goto nla_put_failure;
3204 }
3205 memset(&rx_sc_stats, 0, sizeof(rx_sc_stats));
3206 get_rx_sc_stats(dev, rx_sc, &rx_sc_stats);
3207 if (copy_rx_sc_stats(skb, &rx_sc_stats)) {
3208 nla_nest_cancel(skb, attr);
3209 nla_nest_cancel(skb, rxsc_nest);
3210 nla_nest_cancel(skb, rxsc_list);
3211 goto nla_put_failure;
3212 }
3213 nla_nest_end(skb, attr);
3214
3215 rxsa_list = nla_nest_start_noflag(skb,
3216 MACSEC_RXSC_ATTR_SA_LIST);
3217 if (!rxsa_list) {
3218 nla_nest_cancel(skb, rxsc_nest);
3219 nla_nest_cancel(skb, rxsc_list);
3220 goto nla_put_failure;
3221 }
3222
3223 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
3224 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
3225 struct nlattr *rxsa_nest;
3226 u64 pn;
3227 int pn_len;
3228
3229 if (!rx_sa)
3230 continue;
3231
3232 rxsa_nest = nla_nest_start_noflag(skb, k++);
3233 if (!rxsa_nest) {
3234 nla_nest_cancel(skb, rxsa_list);
3235 nla_nest_cancel(skb, rxsc_nest);
3236 nla_nest_cancel(skb, rxsc_list);
3237 goto nla_put_failure;
3238 }
3239
3240 attr = nla_nest_start_noflag(skb,
3241 MACSEC_SA_ATTR_STATS);
3242 if (!attr) {
3243 nla_nest_cancel(skb, rxsa_list);
3244 nla_nest_cancel(skb, rxsc_nest);
3245 nla_nest_cancel(skb, rxsc_list);
3246 goto nla_put_failure;
3247 }
3248 memset(&rx_sa_stats, 0, sizeof(rx_sa_stats));
3249 get_rx_sa_stats(dev, rx_sc, i, rx_sa, &rx_sa_stats);
3250 if (copy_rx_sa_stats(skb, &rx_sa_stats)) {
3251 nla_nest_cancel(skb, attr);
3252 nla_nest_cancel(skb, rxsa_list);
3253 nla_nest_cancel(skb, rxsc_nest);
3254 nla_nest_cancel(skb, rxsc_list);
3255 goto nla_put_failure;
3256 }
3257 nla_nest_end(skb, attr);
3258
3259 if (secy->xpn) {
3260 pn = rx_sa->next_pn;
3261 pn_len = MACSEC_XPN_PN_LEN;
3262 } else {
3263 pn = rx_sa->next_pn_halves.lower;
3264 pn_len = MACSEC_DEFAULT_PN_LEN;
3265 }
3266
3267 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
3268 nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
3269 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
3270 (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) ||
3271 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
3272 nla_nest_cancel(skb, rxsa_nest);
3273 nla_nest_cancel(skb, rxsc_nest);
3274 nla_nest_cancel(skb, rxsc_list);
3275 goto nla_put_failure;
3276 }
3277 nla_nest_end(skb, rxsa_nest);
3278 }
3279
3280 nla_nest_end(skb, rxsa_list);
3281 nla_nest_end(skb, rxsc_nest);
3282 }
3283
3284 nla_nest_end(skb, rxsc_list);
3285
3286 genlmsg_end(skb, hdr);
3287
3288 return 0;
3289
3290nla_put_failure:
3291 genlmsg_cancel(skb, hdr);
3292 return -EMSGSIZE;
3293}
3294
3295static int macsec_generation = 1;
3296
3297static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
3298{
3299 struct net *net = sock_net(skb->sk);
3300 struct net_device *dev;
3301 int dev_idx, d;
3302
3303 dev_idx = cb->args[0];
3304
3305 d = 0;
3306 rtnl_lock();
3307
3308 cb->seq = macsec_generation;
3309
3310 for_each_netdev(net, dev) {
3311 struct macsec_secy *secy;
3312
3313 if (d < dev_idx)
3314 goto next;
3315
3316 if (!netif_is_macsec(dev))
3317 goto next;
3318
3319 secy = &macsec_priv(dev)->secy;
3320 if (dump_secy(secy, dev, skb, cb) < 0)
3321 goto done;
3322next:
3323 d++;
3324 }
3325
3326done:
3327 rtnl_unlock();
3328 cb->args[0] = d;
3329 return skb->len;
3330}
3331
3332static const struct genl_small_ops macsec_genl_ops[] = {
3333 {
3334 .cmd = MACSEC_CMD_GET_TXSC,
3335 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3336 .dumpit = macsec_dump_txsc,
3337 },
3338 {
3339 .cmd = MACSEC_CMD_ADD_RXSC,
3340 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3341 .doit = macsec_add_rxsc,
3342 .flags = GENL_ADMIN_PERM,
3343 },
3344 {
3345 .cmd = MACSEC_CMD_DEL_RXSC,
3346 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3347 .doit = macsec_del_rxsc,
3348 .flags = GENL_ADMIN_PERM,
3349 },
3350 {
3351 .cmd = MACSEC_CMD_UPD_RXSC,
3352 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3353 .doit = macsec_upd_rxsc,
3354 .flags = GENL_ADMIN_PERM,
3355 },
3356 {
3357 .cmd = MACSEC_CMD_ADD_TXSA,
3358 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3359 .doit = macsec_add_txsa,
3360 .flags = GENL_ADMIN_PERM,
3361 },
3362 {
3363 .cmd = MACSEC_CMD_DEL_TXSA,
3364 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3365 .doit = macsec_del_txsa,
3366 .flags = GENL_ADMIN_PERM,
3367 },
3368 {
3369 .cmd = MACSEC_CMD_UPD_TXSA,
3370 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3371 .doit = macsec_upd_txsa,
3372 .flags = GENL_ADMIN_PERM,
3373 },
3374 {
3375 .cmd = MACSEC_CMD_ADD_RXSA,
3376 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3377 .doit = macsec_add_rxsa,
3378 .flags = GENL_ADMIN_PERM,
3379 },
3380 {
3381 .cmd = MACSEC_CMD_DEL_RXSA,
3382 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3383 .doit = macsec_del_rxsa,
3384 .flags = GENL_ADMIN_PERM,
3385 },
3386 {
3387 .cmd = MACSEC_CMD_UPD_RXSA,
3388 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3389 .doit = macsec_upd_rxsa,
3390 .flags = GENL_ADMIN_PERM,
3391 },
3392 {
3393 .cmd = MACSEC_CMD_UPD_OFFLOAD,
3394 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3395 .doit = macsec_upd_offload,
3396 .flags = GENL_ADMIN_PERM,
3397 },
3398};
3399
3400static struct genl_family macsec_fam __ro_after_init = {
3401 .name = MACSEC_GENL_NAME,
3402 .hdrsize = 0,
3403 .version = MACSEC_GENL_VERSION,
3404 .maxattr = MACSEC_ATTR_MAX,
3405 .policy = macsec_genl_policy,
3406 .netnsok = true,
3407 .module = THIS_MODULE,
3408 .small_ops = macsec_genl_ops,
3409 .n_small_ops = ARRAY_SIZE(macsec_genl_ops),
3410};
3411
3412static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
3413 struct net_device *dev)
3414{
3415 struct macsec_dev *macsec = netdev_priv(dev);
3416 struct macsec_secy *secy = &macsec->secy;
3417 struct pcpu_secy_stats *secy_stats;
3418 int ret, len;
3419
3420 if (macsec_is_offloaded(netdev_priv(dev))) {
3421 skb->dev = macsec->real_dev;
3422 return dev_queue_xmit(skb);
3423 }
3424
3425
3426 if (!secy->protect_frames) {
3427 secy_stats = this_cpu_ptr(macsec->stats);
3428 u64_stats_update_begin(&secy_stats->syncp);
3429 secy_stats->stats.OutPktsUntagged++;
3430 u64_stats_update_end(&secy_stats->syncp);
3431 skb->dev = macsec->real_dev;
3432 len = skb->len;
3433 ret = dev_queue_xmit(skb);
3434 count_tx(dev, ret, len);
3435 return ret;
3436 }
3437
3438 if (!secy->operational) {
3439 kfree_skb(skb);
3440 dev->stats.tx_dropped++;
3441 return NETDEV_TX_OK;
3442 }
3443
3444 len = skb->len;
3445 skb = macsec_encrypt(skb, dev);
3446 if (IS_ERR(skb)) {
3447 if (PTR_ERR(skb) != -EINPROGRESS)
3448 dev->stats.tx_dropped++;
3449 return NETDEV_TX_OK;
3450 }
3451
3452 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
3453
3454 macsec_encrypt_finish(skb, dev);
3455 ret = dev_queue_xmit(skb);
3456 count_tx(dev, ret, len);
3457 return ret;
3458}
3459
3460#define SW_MACSEC_FEATURES \
3461 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
3462
3463
3464
3465
3466
3467#define REAL_DEV_FEATURES(dev) \
3468 ((dev)->features & ~(NETIF_F_VLAN_FEATURES | NETIF_F_HW_MACSEC))
3469
3470static int macsec_dev_init(struct net_device *dev)
3471{
3472 struct macsec_dev *macsec = macsec_priv(dev);
3473 struct net_device *real_dev = macsec->real_dev;
3474 int err;
3475
3476 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3477 if (!dev->tstats)
3478 return -ENOMEM;
3479
3480 err = gro_cells_init(&macsec->gro_cells, dev);
3481 if (err) {
3482 free_percpu(dev->tstats);
3483 return err;
3484 }
3485
3486 if (macsec_is_offloaded(macsec)) {
3487 dev->features = REAL_DEV_FEATURES(real_dev);
3488 } else {
3489 dev->features = real_dev->features & SW_MACSEC_FEATURES;
3490 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
3491 }
3492
3493 dev->needed_headroom = real_dev->needed_headroom +
3494 MACSEC_NEEDED_HEADROOM;
3495 dev->needed_tailroom = real_dev->needed_tailroom +
3496 MACSEC_NEEDED_TAILROOM;
3497
3498 if (is_zero_ether_addr(dev->dev_addr))
3499 eth_hw_addr_inherit(dev, real_dev);
3500 if (is_zero_ether_addr(dev->broadcast))
3501 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
3502
3503
3504 netdev_hold(real_dev, &macsec->dev_tracker, GFP_KERNEL);
3505
3506 return 0;
3507}
3508
3509static void macsec_dev_uninit(struct net_device *dev)
3510{
3511 struct macsec_dev *macsec = macsec_priv(dev);
3512
3513 gro_cells_destroy(&macsec->gro_cells);
3514 free_percpu(dev->tstats);
3515}
3516
3517static netdev_features_t macsec_fix_features(struct net_device *dev,
3518 netdev_features_t features)
3519{
3520 struct macsec_dev *macsec = macsec_priv(dev);
3521 struct net_device *real_dev = macsec->real_dev;
3522
3523 if (macsec_is_offloaded(macsec))
3524 return REAL_DEV_FEATURES(real_dev);
3525
3526 features &= (real_dev->features & SW_MACSEC_FEATURES) |
3527 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
3528 features |= NETIF_F_LLTX;
3529
3530 return features;
3531}
3532
3533static int macsec_dev_open(struct net_device *dev)
3534{
3535 struct macsec_dev *macsec = macsec_priv(dev);
3536 struct net_device *real_dev = macsec->real_dev;
3537 int err;
3538
3539 err = dev_uc_add(real_dev, dev->dev_addr);
3540 if (err < 0)
3541 return err;
3542
3543 if (dev->flags & IFF_ALLMULTI) {
3544 err = dev_set_allmulti(real_dev, 1);
3545 if (err < 0)
3546 goto del_unicast;
3547 }
3548
3549 if (dev->flags & IFF_PROMISC) {
3550 err = dev_set_promiscuity(real_dev, 1);
3551 if (err < 0)
3552 goto clear_allmulti;
3553 }
3554
3555
3556 if (macsec_is_offloaded(macsec)) {
3557 const struct macsec_ops *ops;
3558 struct macsec_context ctx;
3559
3560 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3561 if (!ops) {
3562 err = -EOPNOTSUPP;
3563 goto clear_allmulti;
3564 }
3565
3566 ctx.secy = &macsec->secy;
3567 err = macsec_offload(ops->mdo_dev_open, &ctx);
3568 if (err)
3569 goto clear_allmulti;
3570 }
3571
3572 if (netif_carrier_ok(real_dev))
3573 netif_carrier_on(dev);
3574
3575 return 0;
3576clear_allmulti:
3577 if (dev->flags & IFF_ALLMULTI)
3578 dev_set_allmulti(real_dev, -1);
3579del_unicast:
3580 dev_uc_del(real_dev, dev->dev_addr);
3581 netif_carrier_off(dev);
3582 return err;
3583}
3584
3585static int macsec_dev_stop(struct net_device *dev)
3586{
3587 struct macsec_dev *macsec = macsec_priv(dev);
3588 struct net_device *real_dev = macsec->real_dev;
3589
3590 netif_carrier_off(dev);
3591
3592
3593 if (macsec_is_offloaded(macsec)) {
3594 const struct macsec_ops *ops;
3595 struct macsec_context ctx;
3596
3597 ops = macsec_get_ops(macsec, &ctx);
3598 if (ops) {
3599 ctx.secy = &macsec->secy;
3600 macsec_offload(ops->mdo_dev_stop, &ctx);
3601 }
3602 }
3603
3604 dev_mc_unsync(real_dev, dev);
3605 dev_uc_unsync(real_dev, dev);
3606
3607 if (dev->flags & IFF_ALLMULTI)
3608 dev_set_allmulti(real_dev, -1);
3609
3610 if (dev->flags & IFF_PROMISC)
3611 dev_set_promiscuity(real_dev, -1);
3612
3613 dev_uc_del(real_dev, dev->dev_addr);
3614
3615 return 0;
3616}
3617
3618static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
3619{
3620 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3621
3622 if (!(dev->flags & IFF_UP))
3623 return;
3624
3625 if (change & IFF_ALLMULTI)
3626 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
3627
3628 if (change & IFF_PROMISC)
3629 dev_set_promiscuity(real_dev,
3630 dev->flags & IFF_PROMISC ? 1 : -1);
3631}
3632
3633static void macsec_dev_set_rx_mode(struct net_device *dev)
3634{
3635 struct net_device *real_dev = macsec_priv(dev)->real_dev;
3636
3637 dev_mc_sync(real_dev, dev);
3638 dev_uc_sync(real_dev, dev);
3639}
3640
3641static int macsec_set_mac_address(struct net_device *dev, void *p)
3642{
3643 struct macsec_dev *macsec = macsec_priv(dev);
3644 struct net_device *real_dev = macsec->real_dev;
3645 struct sockaddr *addr = p;
3646 int err;
3647
3648 if (!is_valid_ether_addr(addr->sa_data))
3649 return -EADDRNOTAVAIL;
3650
3651 if (!(dev->flags & IFF_UP))
3652 goto out;
3653
3654 err = dev_uc_add(real_dev, addr->sa_data);
3655 if (err < 0)
3656 return err;
3657
3658 dev_uc_del(real_dev, dev->dev_addr);
3659
3660out:
3661 eth_hw_addr_set(dev, addr->sa_data);
3662
3663
3664 if (macsec_is_offloaded(macsec)) {
3665 const struct macsec_ops *ops;
3666 struct macsec_context ctx;
3667
3668 ops = macsec_get_ops(macsec, &ctx);
3669 if (ops) {
3670 ctx.secy = &macsec->secy;
3671 macsec_offload(ops->mdo_upd_secy, &ctx);
3672 }
3673 }
3674
3675 return 0;
3676}
3677
3678static int macsec_change_mtu(struct net_device *dev, int new_mtu)
3679{
3680 struct macsec_dev *macsec = macsec_priv(dev);
3681 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
3682
3683 if (macsec->real_dev->mtu - extra < new_mtu)
3684 return -ERANGE;
3685
3686 dev->mtu = new_mtu;
3687
3688 return 0;
3689}
3690
3691static void macsec_get_stats64(struct net_device *dev,
3692 struct rtnl_link_stats64 *s)
3693{
3694 if (!dev->tstats)
3695 return;
3696
3697 dev_fetch_sw_netstats(s, dev->tstats);
3698
3699 s->rx_dropped = dev->stats.rx_dropped;
3700 s->tx_dropped = dev->stats.tx_dropped;
3701 s->rx_errors = dev->stats.rx_errors;
3702}
3703
3704static int macsec_get_iflink(const struct net_device *dev)
3705{
3706 return macsec_priv(dev)->real_dev->ifindex;
3707}
3708
3709static const struct net_device_ops macsec_netdev_ops = {
3710 .ndo_init = macsec_dev_init,
3711 .ndo_uninit = macsec_dev_uninit,
3712 .ndo_open = macsec_dev_open,
3713 .ndo_stop = macsec_dev_stop,
3714 .ndo_fix_features = macsec_fix_features,
3715 .ndo_change_mtu = macsec_change_mtu,
3716 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
3717 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
3718 .ndo_set_mac_address = macsec_set_mac_address,
3719 .ndo_start_xmit = macsec_start_xmit,
3720 .ndo_get_stats64 = macsec_get_stats64,
3721 .ndo_get_iflink = macsec_get_iflink,
3722};
3723
3724static const struct device_type macsec_type = {
3725 .name = "macsec",
3726};
3727
3728static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
3729 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
3730 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
3731 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
3732 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
3733 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
3734 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
3735 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
3736 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
3737 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
3738 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
3739 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
3740 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
3741 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3742};
3743
3744static void macsec_free_netdev(struct net_device *dev)
3745{
3746 struct macsec_dev *macsec = macsec_priv(dev);
3747
3748 free_percpu(macsec->stats);
3749 free_percpu(macsec->secy.tx_sc.stats);
3750
3751
3752 netdev_put(macsec->real_dev, &macsec->dev_tracker);
3753}
3754
3755static void macsec_setup(struct net_device *dev)
3756{
3757 ether_setup(dev);
3758 dev->min_mtu = 0;
3759 dev->max_mtu = ETH_MAX_MTU;
3760 dev->priv_flags |= IFF_NO_QUEUE;
3761 dev->netdev_ops = &macsec_netdev_ops;
3762 dev->needs_free_netdev = true;
3763 dev->priv_destructor = macsec_free_netdev;
3764 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3765
3766 eth_zero_addr(dev->broadcast);
3767}
3768
3769static int macsec_changelink_common(struct net_device *dev,
3770 struct nlattr *data[])
3771{
3772 struct macsec_secy *secy;
3773 struct macsec_tx_sc *tx_sc;
3774
3775 secy = &macsec_priv(dev)->secy;
3776 tx_sc = &secy->tx_sc;
3777
3778 if (data[IFLA_MACSEC_ENCODING_SA]) {
3779 struct macsec_tx_sa *tx_sa;
3780
3781 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3782 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3783
3784 secy->operational = tx_sa && tx_sa->active;
3785 }
3786
3787 if (data[IFLA_MACSEC_ENCRYPT])
3788 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3789
3790 if (data[IFLA_MACSEC_PROTECT])
3791 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3792
3793 if (data[IFLA_MACSEC_INC_SCI])
3794 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3795
3796 if (data[IFLA_MACSEC_ES])
3797 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3798
3799 if (data[IFLA_MACSEC_SCB])
3800 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3801
3802 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3803 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3804
3805 if (data[IFLA_MACSEC_VALIDATION])
3806 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3807
3808 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3809 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3810 case MACSEC_CIPHER_ID_GCM_AES_128:
3811 case MACSEC_DEFAULT_CIPHER_ID:
3812 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3813 secy->xpn = false;
3814 break;
3815 case MACSEC_CIPHER_ID_GCM_AES_256:
3816 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3817 secy->xpn = false;
3818 break;
3819 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
3820 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3821 secy->xpn = true;
3822 break;
3823 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
3824 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3825 secy->xpn = true;
3826 break;
3827 default:
3828 return -EINVAL;
3829 }
3830 }
3831
3832 if (data[IFLA_MACSEC_WINDOW]) {
3833 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3834
3835
3836
3837 if (secy->xpn &&
3838 secy->replay_window > MACSEC_XPN_MAX_REPLAY_WINDOW)
3839 return -EINVAL;
3840 }
3841
3842 return 0;
3843}
3844
3845static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3846 struct nlattr *data[],
3847 struct netlink_ext_ack *extack)
3848{
3849 struct macsec_dev *macsec = macsec_priv(dev);
3850 struct macsec_tx_sc tx_sc;
3851 struct macsec_secy secy;
3852 int ret;
3853
3854 if (!data)
3855 return 0;
3856
3857 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3858 data[IFLA_MACSEC_ICV_LEN] ||
3859 data[IFLA_MACSEC_SCI] ||
3860 data[IFLA_MACSEC_PORT])
3861 return -EINVAL;
3862
3863
3864
3865
3866 memcpy(&secy, &macsec->secy, sizeof(secy));
3867 memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
3868
3869 ret = macsec_changelink_common(dev, data);
3870 if (ret)
3871 goto cleanup;
3872
3873
3874 if (macsec_is_offloaded(macsec)) {
3875 const struct macsec_ops *ops;
3876 struct macsec_context ctx;
3877 int ret;
3878
3879 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3880 if (!ops) {
3881 ret = -EOPNOTSUPP;
3882 goto cleanup;
3883 }
3884
3885 ctx.secy = &macsec->secy;
3886 ret = macsec_offload(ops->mdo_upd_secy, &ctx);
3887 if (ret)
3888 goto cleanup;
3889 }
3890
3891 return 0;
3892
3893cleanup:
3894 memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
3895 memcpy(&macsec->secy, &secy, sizeof(secy));
3896
3897 return ret;
3898}
3899
3900static void macsec_del_dev(struct macsec_dev *macsec)
3901{
3902 int i;
3903
3904 while (macsec->secy.rx_sc) {
3905 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3906
3907 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3908 free_rx_sc(rx_sc);
3909 }
3910
3911 for (i = 0; i < MACSEC_NUM_AN; i++) {
3912 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3913
3914 if (sa) {
3915 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3916 clear_tx_sa(sa);
3917 }
3918 }
3919}
3920
3921static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3922{
3923 struct macsec_dev *macsec = macsec_priv(dev);
3924 struct net_device *real_dev = macsec->real_dev;
3925
3926
3927 if (macsec_is_offloaded(macsec)) {
3928 const struct macsec_ops *ops;
3929 struct macsec_context ctx;
3930
3931 ops = macsec_get_ops(netdev_priv(dev), &ctx);
3932 if (ops) {
3933 ctx.secy = &macsec->secy;
3934 macsec_offload(ops->mdo_del_secy, &ctx);
3935 }
3936 }
3937
3938 unregister_netdevice_queue(dev, head);
3939 list_del_rcu(&macsec->secys);
3940 macsec_del_dev(macsec);
3941 netdev_upper_dev_unlink(real_dev, dev);
3942
3943 macsec_generation++;
3944}
3945
3946static void macsec_dellink(struct net_device *dev, struct list_head *head)
3947{
3948 struct macsec_dev *macsec = macsec_priv(dev);
3949 struct net_device *real_dev = macsec->real_dev;
3950 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3951
3952 macsec_common_dellink(dev, head);
3953
3954 if (list_empty(&rxd->secys)) {
3955 netdev_rx_handler_unregister(real_dev);
3956 kfree(rxd);
3957 }
3958}
3959
3960static int register_macsec_dev(struct net_device *real_dev,
3961 struct net_device *dev)
3962{
3963 struct macsec_dev *macsec = macsec_priv(dev);
3964 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3965
3966 if (!rxd) {
3967 int err;
3968
3969 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3970 if (!rxd)
3971 return -ENOMEM;
3972
3973 INIT_LIST_HEAD(&rxd->secys);
3974
3975 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3976 rxd);
3977 if (err < 0) {
3978 kfree(rxd);
3979 return err;
3980 }
3981 }
3982
3983 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3984 return 0;
3985}
3986
3987static bool sci_exists(struct net_device *dev, sci_t sci)
3988{
3989 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3990 struct macsec_dev *macsec;
3991
3992 list_for_each_entry(macsec, &rxd->secys, secys) {
3993 if (macsec->secy.sci == sci)
3994 return true;
3995 }
3996
3997 return false;
3998}
3999
4000static sci_t dev_to_sci(struct net_device *dev, __be16 port)
4001{
4002 return make_sci(dev->dev_addr, port);
4003}
4004
4005static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
4006{
4007 struct macsec_dev *macsec = macsec_priv(dev);
4008 struct macsec_secy *secy = &macsec->secy;
4009
4010 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
4011 if (!macsec->stats)
4012 return -ENOMEM;
4013
4014 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
4015 if (!secy->tx_sc.stats) {
4016 free_percpu(macsec->stats);
4017 return -ENOMEM;
4018 }
4019
4020 if (sci == MACSEC_UNDEF_SCI)
4021 sci = dev_to_sci(dev, MACSEC_PORT_ES);
4022
4023 secy->netdev = dev;
4024 secy->operational = true;
4025 secy->key_len = DEFAULT_SAK_LEN;
4026 secy->icv_len = icv_len;
4027 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
4028 secy->protect_frames = true;
4029 secy->replay_protect = false;
4030 secy->xpn = DEFAULT_XPN;
4031
4032 secy->sci = sci;
4033 secy->tx_sc.active = true;
4034 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
4035 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
4036 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
4037 secy->tx_sc.end_station = false;
4038 secy->tx_sc.scb = false;
4039
4040 return 0;
4041}
4042
4043static struct lock_class_key macsec_netdev_addr_lock_key;
4044
4045static int macsec_newlink(struct net *net, struct net_device *dev,
4046 struct nlattr *tb[], struct nlattr *data[],
4047 struct netlink_ext_ack *extack)
4048{
4049 struct macsec_dev *macsec = macsec_priv(dev);
4050 rx_handler_func_t *rx_handler;
4051 u8 icv_len = DEFAULT_ICV_LEN;
4052 struct net_device *real_dev;
4053 int err, mtu;
4054 sci_t sci;
4055
4056 if (!tb[IFLA_LINK])
4057 return -EINVAL;
4058 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
4059 if (!real_dev)
4060 return -ENODEV;
4061 if (real_dev->type != ARPHRD_ETHER)
4062 return -EINVAL;
4063
4064 dev->priv_flags |= IFF_MACSEC;
4065
4066 macsec->real_dev = real_dev;
4067
4068 if (data && data[IFLA_MACSEC_OFFLOAD])
4069 macsec->offload = nla_get_offload(data[IFLA_MACSEC_OFFLOAD]);
4070 else
4071
4072 macsec->offload = MACSEC_OFFLOAD_OFF;
4073
4074
4075 if (macsec->offload != MACSEC_OFFLOAD_OFF &&
4076 !macsec_check_offload(macsec->offload, macsec))
4077 return -EOPNOTSUPP;
4078
4079
4080 if ((data && data[IFLA_MACSEC_SCI]) &&
4081 (data && data[IFLA_MACSEC_INC_SCI])) {
4082 u8 send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
4083
4084 if (!send_sci)
4085 return -EINVAL;
4086 }
4087
4088 if (data && data[IFLA_MACSEC_ICV_LEN])
4089 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4090 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
4091 if (mtu < 0)
4092 dev->mtu = 0;
4093 else
4094 dev->mtu = mtu;
4095
4096 rx_handler = rtnl_dereference(real_dev->rx_handler);
4097 if (rx_handler && rx_handler != macsec_handle_frame)
4098 return -EBUSY;
4099
4100 err = register_netdevice(dev);
4101 if (err < 0)
4102 return err;
4103
4104 netdev_lockdep_set_classes(dev);
4105 lockdep_set_class(&dev->addr_list_lock,
4106 &macsec_netdev_addr_lock_key);
4107
4108 err = netdev_upper_dev_link(real_dev, dev, extack);
4109 if (err < 0)
4110 goto unregister;
4111
4112
4113
4114
4115 if (data && data[IFLA_MACSEC_SCI])
4116 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
4117 else if (data && data[IFLA_MACSEC_PORT])
4118 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
4119 else
4120 sci = dev_to_sci(dev, MACSEC_PORT_ES);
4121
4122 if (rx_handler && sci_exists(real_dev, sci)) {
4123 err = -EBUSY;
4124 goto unlink;
4125 }
4126
4127 err = macsec_add_dev(dev, sci, icv_len);
4128 if (err)
4129 goto unlink;
4130
4131 if (data) {
4132 err = macsec_changelink_common(dev, data);
4133 if (err)
4134 goto del_dev;
4135 }
4136
4137
4138 if (macsec_is_offloaded(macsec)) {
4139 const struct macsec_ops *ops;
4140 struct macsec_context ctx;
4141
4142 ops = macsec_get_ops(macsec, &ctx);
4143 if (ops) {
4144 ctx.secy = &macsec->secy;
4145 err = macsec_offload(ops->mdo_add_secy, &ctx);
4146 if (err)
4147 goto del_dev;
4148 }
4149 }
4150
4151 err = register_macsec_dev(real_dev, dev);
4152 if (err < 0)
4153 goto del_dev;
4154
4155 netif_stacked_transfer_operstate(real_dev, dev);
4156 linkwatch_fire_event(dev);
4157
4158 macsec_generation++;
4159
4160 return 0;
4161
4162del_dev:
4163 macsec_del_dev(macsec);
4164unlink:
4165 netdev_upper_dev_unlink(real_dev, dev);
4166unregister:
4167 unregister_netdevice(dev);
4168 return err;
4169}
4170
4171static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
4172 struct netlink_ext_ack *extack)
4173{
4174 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
4175 u8 icv_len = DEFAULT_ICV_LEN;
4176 int flag;
4177 bool es, scb, sci;
4178
4179 if (!data)
4180 return 0;
4181
4182 if (data[IFLA_MACSEC_CIPHER_SUITE])
4183 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
4184
4185 if (data[IFLA_MACSEC_ICV_LEN]) {
4186 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
4187 if (icv_len != DEFAULT_ICV_LEN) {
4188 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
4189 struct crypto_aead *dummy_tfm;
4190
4191 dummy_tfm = macsec_alloc_tfm(dummy_key,
4192 DEFAULT_SAK_LEN,
4193 icv_len);
4194 if (IS_ERR(dummy_tfm))
4195 return PTR_ERR(dummy_tfm);
4196 crypto_free_aead(dummy_tfm);
4197 }
4198 }
4199
4200 switch (csid) {
4201 case MACSEC_CIPHER_ID_GCM_AES_128:
4202 case MACSEC_CIPHER_ID_GCM_AES_256:
4203 case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
4204 case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
4205 case MACSEC_DEFAULT_CIPHER_ID:
4206 if (icv_len < MACSEC_MIN_ICV_LEN ||
4207 icv_len > MACSEC_STD_ICV_LEN)
4208 return -EINVAL;
4209 break;
4210 default:
4211 return -EINVAL;
4212 }
4213
4214 if (data[IFLA_MACSEC_ENCODING_SA]) {
4215 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
4216 return -EINVAL;
4217 }
4218
4219 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
4220 flag < IFLA_MACSEC_VALIDATION;
4221 flag++) {
4222 if (data[flag]) {
4223 if (nla_get_u8(data[flag]) > 1)
4224 return -EINVAL;
4225 }
4226 }
4227
4228 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
4229 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
4230 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
4231
4232 if ((sci && (scb || es)) || (scb && es))
4233 return -EINVAL;
4234
4235 if (data[IFLA_MACSEC_VALIDATION] &&
4236 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
4237 return -EINVAL;
4238
4239 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
4240 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
4241 !data[IFLA_MACSEC_WINDOW])
4242 return -EINVAL;
4243
4244 return 0;
4245}
4246
4247static struct net *macsec_get_link_net(const struct net_device *dev)
4248{
4249 return dev_net(macsec_priv(dev)->real_dev);
4250}
4251
4252static size_t macsec_get_size(const struct net_device *dev)
4253{
4254 return nla_total_size_64bit(8) +
4255 nla_total_size(1) +
4256 nla_total_size_64bit(8) +
4257 nla_total_size(4) +
4258 nla_total_size(1) +
4259 nla_total_size(1) +
4260 nla_total_size(1) +
4261 nla_total_size(1) +
4262 nla_total_size(1) +
4263 nla_total_size(1) +
4264 nla_total_size(1) +
4265 nla_total_size(1) +
4266 0;
4267}
4268
4269static int macsec_fill_info(struct sk_buff *skb,
4270 const struct net_device *dev)
4271{
4272 struct macsec_secy *secy = &macsec_priv(dev)->secy;
4273 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
4274 u64 csid;
4275
4276 switch (secy->key_len) {
4277 case MACSEC_GCM_AES_128_SAK_LEN:
4278 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
4279 break;
4280 case MACSEC_GCM_AES_256_SAK_LEN:
4281 csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
4282 break;
4283 default:
4284 goto nla_put_failure;
4285 }
4286
4287 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
4288 IFLA_MACSEC_PAD) ||
4289 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
4290 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
4291 csid, IFLA_MACSEC_PAD) ||
4292 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
4293 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
4294 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
4295 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
4296 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
4297 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
4298 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
4299 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
4300 0)
4301 goto nla_put_failure;
4302
4303 if (secy->replay_protect) {
4304 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
4305 goto nla_put_failure;
4306 }
4307
4308 return 0;
4309
4310nla_put_failure:
4311 return -EMSGSIZE;
4312}
4313
4314static struct rtnl_link_ops macsec_link_ops __read_mostly = {
4315 .kind = "macsec",
4316 .priv_size = sizeof(struct macsec_dev),
4317 .maxtype = IFLA_MACSEC_MAX,
4318 .policy = macsec_rtnl_policy,
4319 .setup = macsec_setup,
4320 .validate = macsec_validate_attr,
4321 .newlink = macsec_newlink,
4322 .changelink = macsec_changelink,
4323 .dellink = macsec_dellink,
4324 .get_size = macsec_get_size,
4325 .fill_info = macsec_fill_info,
4326 .get_link_net = macsec_get_link_net,
4327};
4328
4329static bool is_macsec_master(struct net_device *dev)
4330{
4331 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
4332}
4333
4334static int macsec_notify(struct notifier_block *this, unsigned long event,
4335 void *ptr)
4336{
4337 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
4338 LIST_HEAD(head);
4339
4340 if (!is_macsec_master(real_dev))
4341 return NOTIFY_DONE;
4342
4343 switch (event) {
4344 case NETDEV_DOWN:
4345 case NETDEV_UP:
4346 case NETDEV_CHANGE: {
4347 struct macsec_dev *m, *n;
4348 struct macsec_rxh_data *rxd;
4349
4350 rxd = macsec_data_rtnl(real_dev);
4351 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4352 struct net_device *dev = m->secy.netdev;
4353
4354 netif_stacked_transfer_operstate(real_dev, dev);
4355 }
4356 break;
4357 }
4358 case NETDEV_UNREGISTER: {
4359 struct macsec_dev *m, *n;
4360 struct macsec_rxh_data *rxd;
4361
4362 rxd = macsec_data_rtnl(real_dev);
4363 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
4364 macsec_common_dellink(m->secy.netdev, &head);
4365 }
4366
4367 netdev_rx_handler_unregister(real_dev);
4368 kfree(rxd);
4369
4370 unregister_netdevice_many(&head);
4371 break;
4372 }
4373 case NETDEV_CHANGEMTU: {
4374 struct macsec_dev *m;
4375 struct macsec_rxh_data *rxd;
4376
4377 rxd = macsec_data_rtnl(real_dev);
4378 list_for_each_entry(m, &rxd->secys, secys) {
4379