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