This source file includes following definitions.
- macsec_priv
- macsec_data_rcu
- macsec_data_rtnl
- macsec_rxsa_get
- free_rx_sc_rcu
- macsec_rxsc_get
- macsec_rxsc_put
- free_rxsa
- macsec_rxsa_put
- macsec_txsa_get
- free_txsa
- macsec_txsa_put
- macsec_skb_cb
- send_sci
- make_sci
- macsec_frame_sci
- macsec_sectag_len
- macsec_hdr_len
- macsec_extra_len
- macsec_fill_sectag
- macsec_set_shortlen
- macsec_validate_skb
- macsec_fill_iv
- macsec_ethhdr
- tx_sa_update_pn
- macsec_encrypt_finish
- macsec_count_tx
- count_tx
- macsec_encrypt_done
- macsec_alloc_req
- macsec_encrypt
- macsec_post_decrypt
- macsec_reset_skb
- macsec_finalize_skb
- count_rx
- macsec_decrypt_done
- macsec_decrypt
- find_rx_sc
- find_rx_sc_rtnl
- handle_not_macsec
- macsec_handle_frame
- macsec_alloc_tfm
- init_rx_sa
- clear_rx_sa
- free_rx_sc
- del_rx_sc
- create_rx_sc
- init_tx_sa
- clear_tx_sa
- get_dev_from_nl
- nla_get_sci
- nla_put_sci
- get_txsa_from_nl
- get_rxsc_from_nl
- get_rxsa_from_nl
- parse_sa_config
- parse_rxsc_config
- validate_add_rxsa
- macsec_add_rxsa
- validate_add_rxsc
- macsec_add_rxsc
- validate_add_txsa
- macsec_add_txsa
- macsec_del_rxsa
- macsec_del_rxsc
- macsec_del_txsa
- validate_upd_sa
- macsec_upd_txsa
- macsec_upd_rxsa
- macsec_upd_rxsc
- copy_tx_sa_stats
- copy_rx_sa_stats
- copy_rx_sc_stats
- copy_tx_sc_stats
- copy_secy_stats
- nla_put_secy
- dump_secy
- macsec_dump_txsc
- macsec_start_xmit
- macsec_dev_init
- macsec_dev_uninit
- macsec_fix_features
- macsec_dev_open
- macsec_dev_stop
- macsec_dev_change_rx_flags
- macsec_dev_set_rx_mode
- dev_to_sci
- macsec_set_mac_address
- macsec_change_mtu
- macsec_get_stats64
- macsec_get_iflink
- macsec_free_netdev
- macsec_setup
- macsec_changelink_common
- macsec_changelink
- macsec_del_dev
- macsec_common_dellink
- macsec_dellink
- register_macsec_dev
- sci_exists
- macsec_add_dev
- macsec_newlink
- macsec_validate_attr
- macsec_get_link_net
- macsec_get_size
- macsec_fill_info
- is_macsec_master
- macsec_notify
- macsec_init
- macsec_exit
1
2
3
4
5
6
7
8 #include <linux/types.h>
9 #include <linux/skbuff.h>
10 #include <linux/socket.h>
11 #include <linux/module.h>
12 #include <crypto/aead.h>
13 #include <linux/etherdevice.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/refcount.h>
16 #include <net/genetlink.h>
17 #include <net/sock.h>
18 #include <net/gro_cells.h>
19 #include <linux/if_arp.h>
20
21 #include <uapi/linux/if_macsec.h>
22
23 typedef u64 __bitwise sci_t;
24
25 #define MACSEC_SCI_LEN 8
26
27
28 #define MACSEC_TAG_LEN 6
29
30 struct macsec_eth_header {
31 struct ethhdr eth;
32
33 u8 tci_an;
34 #if defined(__LITTLE_ENDIAN_BITFIELD)
35 u8 short_length:6,
36 unused:2;
37 #elif defined(__BIG_ENDIAN_BITFIELD)
38 u8 unused:2,
39 short_length:6;
40 #else
41 #error "Please fix <asm/byteorder.h>"
42 #endif
43 __be32 packet_number;
44 u8 secure_channel_id[8];
45 } __packed;
46
47 #define MACSEC_TCI_VERSION 0x80
48 #define MACSEC_TCI_ES 0x40
49 #define MACSEC_TCI_SC 0x20
50 #define MACSEC_TCI_SCB 0x10
51 #define MACSEC_TCI_E 0x08
52 #define MACSEC_TCI_C 0x04
53 #define MACSEC_AN_MASK 0x03
54 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
55
56
57 #define MIN_NON_SHORT_LEN 48
58
59 #define GCM_AES_IV_LEN 12
60 #define DEFAULT_ICV_LEN 16
61
62 #define MACSEC_NUM_AN 4
63
64 #define for_each_rxsc(secy, sc) \
65 for (sc = rcu_dereference_bh(secy->rx_sc); \
66 sc; \
67 sc = rcu_dereference_bh(sc->next))
68 #define for_each_rxsc_rtnl(secy, sc) \
69 for (sc = rtnl_dereference(secy->rx_sc); \
70 sc; \
71 sc = rtnl_dereference(sc->next))
72
73 struct gcm_iv {
74 union {
75 u8 secure_channel_id[8];
76 sci_t sci;
77 };
78 __be32 pn;
79 };
80
81
82
83
84
85
86 struct macsec_key {
87 u8 id[MACSEC_KEYID_LEN];
88 struct crypto_aead *tfm;
89 };
90
91 struct macsec_rx_sc_stats {
92 __u64 InOctetsValidated;
93 __u64 InOctetsDecrypted;
94 __u64 InPktsUnchecked;
95 __u64 InPktsDelayed;
96 __u64 InPktsOK;
97 __u64 InPktsInvalid;
98 __u64 InPktsLate;
99 __u64 InPktsNotValid;
100 __u64 InPktsNotUsingSA;
101 __u64 InPktsUnusedSA;
102 };
103
104 struct macsec_rx_sa_stats {
105 __u32 InPktsOK;
106 __u32 InPktsInvalid;
107 __u32 InPktsNotValid;
108 __u32 InPktsNotUsingSA;
109 __u32 InPktsUnusedSA;
110 };
111
112 struct macsec_tx_sa_stats {
113 __u32 OutPktsProtected;
114 __u32 OutPktsEncrypted;
115 };
116
117 struct macsec_tx_sc_stats {
118 __u64 OutPktsProtected;
119 __u64 OutPktsEncrypted;
120 __u64 OutOctetsProtected;
121 __u64 OutOctetsEncrypted;
122 };
123
124 struct macsec_dev_stats {
125 __u64 OutPktsUntagged;
126 __u64 InPktsUntagged;
127 __u64 OutPktsTooLong;
128 __u64 InPktsNoTag;
129 __u64 InPktsBadTag;
130 __u64 InPktsUnknownSCI;
131 __u64 InPktsNoSCI;
132 __u64 InPktsOverrun;
133 };
134
135
136
137
138
139
140
141
142
143 struct macsec_rx_sa {
144 struct macsec_key key;
145 spinlock_t lock;
146 u32 next_pn;
147 refcount_t refcnt;
148 bool active;
149 struct macsec_rx_sa_stats __percpu *stats;
150 struct macsec_rx_sc *sc;
151 struct rcu_head rcu;
152 };
153
154 struct pcpu_rx_sc_stats {
155 struct macsec_rx_sc_stats stats;
156 struct u64_stats_sync syncp;
157 };
158
159
160
161
162
163
164
165
166 struct macsec_rx_sc {
167 struct macsec_rx_sc __rcu *next;
168 sci_t sci;
169 bool active;
170 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
171 struct pcpu_rx_sc_stats __percpu *stats;
172 refcount_t refcnt;
173 struct rcu_head rcu_head;
174 };
175
176
177
178
179
180
181
182
183
184 struct macsec_tx_sa {
185 struct macsec_key key;
186 spinlock_t lock;
187 u32 next_pn;
188 refcount_t refcnt;
189 bool active;
190 struct macsec_tx_sa_stats __percpu *stats;
191 struct rcu_head rcu;
192 };
193
194 struct pcpu_tx_sc_stats {
195 struct macsec_tx_sc_stats stats;
196 struct u64_stats_sync syncp;
197 };
198
199
200
201
202
203
204
205
206
207
208
209
210 struct macsec_tx_sc {
211 bool active;
212 u8 encoding_sa;
213 bool encrypt;
214 bool send_sci;
215 bool end_station;
216 bool scb;
217 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
218 struct pcpu_tx_sc_stats __percpu *stats;
219 };
220
221 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 struct macsec_secy {
239 struct net_device *netdev;
240 unsigned int n_rx_sc;
241 sci_t sci;
242 u16 key_len;
243 u16 icv_len;
244 enum macsec_validation_type validate_frames;
245 bool operational;
246 bool protect_frames;
247 bool replay_protect;
248 u32 replay_window;
249 struct macsec_tx_sc tx_sc;
250 struct macsec_rx_sc __rcu *rx_sc;
251 };
252
253 struct pcpu_secy_stats {
254 struct macsec_dev_stats stats;
255 struct u64_stats_sync syncp;
256 };
257
258
259
260
261
262
263
264
265 struct macsec_dev {
266 struct macsec_secy secy;
267 struct net_device *real_dev;
268 struct pcpu_secy_stats __percpu *stats;
269 struct list_head secys;
270 struct gro_cells gro_cells;
271 };
272
273
274
275
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
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
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
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
492 if (skb->len <= 16)
493 return false;
494
495
496
497
498 if (h->tci_an & MACSEC_TCI_VERSION)
499 return false;
500
501
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
507 if (h->unused)
508 return false;
509
510
511 if (!h->packet_number)
512 return false;
513
514
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
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
796
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
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
966
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
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
993
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
1042
1043
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
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
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
1116
1117
1118
1119
1120 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1121 return RX_HANDLER_PASS;
1122
1123
1124 if (hdr->tci_an & MACSEC_TCI_SC) {
1125 if (!pulled_sci)
1126 goto drop_direct;
1127 }
1128
1129
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
1169
1170
1171
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
1182
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
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
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
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
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
1273
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
1284
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
1313 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
1314
1315 if (IS_ERR(tfm))
1316 return tfm;
1317
1318 ret = crypto_aead_setkey(tfm, key, key_len);
1319 if (ret < 0)
1320 goto fail;
1321
1322 ret = crypto_aead_setauthsize(tfm, icv_len);
1323 if (ret < 0)
1324 goto fail;
1325
1326 return tfm;
1327 fail:
1328 crypto_free_aead(tfm);
1329 return ERR_PTR(ret);
1330 }
1331
1332 static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1333 int icv_len)
1334 {
1335 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1336 if (!rx_sa->stats)
1337 return -ENOMEM;
1338
1339 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1340 if (IS_ERR(rx_sa->key.tfm)) {
1341 free_percpu(rx_sa->stats);
1342 return PTR_ERR(rx_sa->key.tfm);
1343 }
1344
1345 rx_sa->active = false;
1346 rx_sa->next_pn = 1;
1347 refcount_set(&rx_sa->refcnt, 1);
1348 spin_lock_init(&rx_sa->lock);
1349
1350 return 0;
1351 }
1352
1353 static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1354 {
1355 rx_sa->active = false;
1356
1357 macsec_rxsa_put(rx_sa);
1358 }
1359
1360 static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1361 {
1362 int i;
1363
1364 for (i = 0; i < MACSEC_NUM_AN; i++) {
1365 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1366
1367 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1368 if (sa)
1369 clear_rx_sa(sa);
1370 }
1371
1372 macsec_rxsc_put(rx_sc);
1373 }
1374
1375 static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1376 {
1377 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1378
1379 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1380 rx_sc;
1381 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1382 if (rx_sc->sci == sci) {
1383 if (rx_sc->active)
1384 secy->n_rx_sc--;
1385 rcu_assign_pointer(*rx_scp, rx_sc->next);
1386 return rx_sc;
1387 }
1388 }
1389
1390 return NULL;
1391 }
1392
1393 static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1394 {
1395 struct macsec_rx_sc *rx_sc;
1396 struct macsec_dev *macsec;
1397 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1398 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1399 struct macsec_secy *secy;
1400
1401 list_for_each_entry(macsec, &rxd->secys, secys) {
1402 if (find_rx_sc_rtnl(&macsec->secy, sci))
1403 return ERR_PTR(-EEXIST);
1404 }
1405
1406 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1407 if (!rx_sc)
1408 return ERR_PTR(-ENOMEM);
1409
1410 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1411 if (!rx_sc->stats) {
1412 kfree(rx_sc);
1413 return ERR_PTR(-ENOMEM);
1414 }
1415
1416 rx_sc->sci = sci;
1417 rx_sc->active = true;
1418 refcount_set(&rx_sc->refcnt, 1);
1419
1420 secy = &macsec_priv(dev)->secy;
1421 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1422 rcu_assign_pointer(secy->rx_sc, rx_sc);
1423
1424 if (rx_sc->active)
1425 secy->n_rx_sc++;
1426
1427 return rx_sc;
1428 }
1429
1430 static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1431 int icv_len)
1432 {
1433 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1434 if (!tx_sa->stats)
1435 return -ENOMEM;
1436
1437 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
1438 if (IS_ERR(tx_sa->key.tfm)) {
1439 free_percpu(tx_sa->stats);
1440 return PTR_ERR(tx_sa->key.tfm);
1441 }
1442
1443 tx_sa->active = false;
1444 refcount_set(&tx_sa->refcnt, 1);
1445 spin_lock_init(&tx_sa->lock);
1446
1447 return 0;
1448 }
1449
1450 static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1451 {
1452 tx_sa->active = false;
1453
1454 macsec_txsa_put(tx_sa);
1455 }
1456
1457 static struct genl_family macsec_fam;
1458
1459 static struct net_device *get_dev_from_nl(struct net *net,
1460 struct nlattr **attrs)
1461 {
1462 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1463 struct net_device *dev;
1464
1465 dev = __dev_get_by_index(net, ifindex);
1466 if (!dev)
1467 return ERR_PTR(-ENODEV);
1468
1469 if (!netif_is_macsec(dev))
1470 return ERR_PTR(-ENODEV);
1471
1472 return dev;
1473 }
1474
1475 static sci_t nla_get_sci(const struct nlattr *nla)
1476 {
1477 return (__force sci_t)nla_get_u64(nla);
1478 }
1479
1480 static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1481 int padattr)
1482 {
1483 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
1484 }
1485
1486 static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1487 struct nlattr **attrs,
1488 struct nlattr **tb_sa,
1489 struct net_device **devp,
1490 struct macsec_secy **secyp,
1491 struct macsec_tx_sc **scp,
1492 u8 *assoc_num)
1493 {
1494 struct net_device *dev;
1495 struct macsec_secy *secy;
1496 struct macsec_tx_sc *tx_sc;
1497 struct macsec_tx_sa *tx_sa;
1498
1499 if (!tb_sa[MACSEC_SA_ATTR_AN])
1500 return ERR_PTR(-EINVAL);
1501
1502 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1503
1504 dev = get_dev_from_nl(net, attrs);
1505 if (IS_ERR(dev))
1506 return ERR_CAST(dev);
1507
1508 if (*assoc_num >= MACSEC_NUM_AN)
1509 return ERR_PTR(-EINVAL);
1510
1511 secy = &macsec_priv(dev)->secy;
1512 tx_sc = &secy->tx_sc;
1513
1514 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1515 if (!tx_sa)
1516 return ERR_PTR(-ENODEV);
1517
1518 *devp = dev;
1519 *scp = tx_sc;
1520 *secyp = secy;
1521 return tx_sa;
1522 }
1523
1524 static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1525 struct nlattr **attrs,
1526 struct nlattr **tb_rxsc,
1527 struct net_device **devp,
1528 struct macsec_secy **secyp)
1529 {
1530 struct net_device *dev;
1531 struct macsec_secy *secy;
1532 struct macsec_rx_sc *rx_sc;
1533 sci_t sci;
1534
1535 dev = get_dev_from_nl(net, attrs);
1536 if (IS_ERR(dev))
1537 return ERR_CAST(dev);
1538
1539 secy = &macsec_priv(dev)->secy;
1540
1541 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1542 return ERR_PTR(-EINVAL);
1543
1544 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1545 rx_sc = find_rx_sc_rtnl(secy, sci);
1546 if (!rx_sc)
1547 return ERR_PTR(-ENODEV);
1548
1549 *secyp = secy;
1550 *devp = dev;
1551
1552 return rx_sc;
1553 }
1554
1555 static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1556 struct nlattr **attrs,
1557 struct nlattr **tb_rxsc,
1558 struct nlattr **tb_sa,
1559 struct net_device **devp,
1560 struct macsec_secy **secyp,
1561 struct macsec_rx_sc **scp,
1562 u8 *assoc_num)
1563 {
1564 struct macsec_rx_sc *rx_sc;
1565 struct macsec_rx_sa *rx_sa;
1566
1567 if (!tb_sa[MACSEC_SA_ATTR_AN])
1568 return ERR_PTR(-EINVAL);
1569
1570 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1571 if (*assoc_num >= MACSEC_NUM_AN)
1572 return ERR_PTR(-EINVAL);
1573
1574 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1575 if (IS_ERR(rx_sc))
1576 return ERR_CAST(rx_sc);
1577
1578 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1579 if (!rx_sa)
1580 return ERR_PTR(-ENODEV);
1581
1582 *scp = rx_sc;
1583 return rx_sa;
1584 }
1585
1586 static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1587 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1588 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1589 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1590 };
1591
1592 static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1593 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1594 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1595 };
1596
1597 static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1598 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1599 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1600 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
1601 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1602 .len = MACSEC_KEYID_LEN, },
1603 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1604 .len = MACSEC_MAX_KEY_LEN, },
1605 };
1606
1607 static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1608 {
1609 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1610 return -EINVAL;
1611
1612 if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
1613 return -EINVAL;
1614
1615 return 0;
1616 }
1617
1618 static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1619 {
1620 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1621 return -EINVAL;
1622
1623 if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
1624 return -EINVAL;
1625
1626 return 0;
1627 }
1628
1629 static bool validate_add_rxsa(struct nlattr **attrs)
1630 {
1631 if (!attrs[MACSEC_SA_ATTR_AN] ||
1632 !attrs[MACSEC_SA_ATTR_KEY] ||
1633 !attrs[MACSEC_SA_ATTR_KEYID])
1634 return false;
1635
1636 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1637 return false;
1638
1639 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1640 return false;
1641
1642 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1643 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1644 return false;
1645 }
1646
1647 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1648 return false;
1649
1650 return true;
1651 }
1652
1653 static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1654 {
1655 struct net_device *dev;
1656 struct nlattr **attrs = info->attrs;
1657 struct macsec_secy *secy;
1658 struct macsec_rx_sc *rx_sc;
1659 struct macsec_rx_sa *rx_sa;
1660 unsigned char assoc_num;
1661 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1662 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1663 int err;
1664
1665 if (!attrs[MACSEC_ATTR_IFINDEX])
1666 return -EINVAL;
1667
1668 if (parse_sa_config(attrs, tb_sa))
1669 return -EINVAL;
1670
1671 if (parse_rxsc_config(attrs, tb_rxsc))
1672 return -EINVAL;
1673
1674 if (!validate_add_rxsa(tb_sa))
1675 return -EINVAL;
1676
1677 rtnl_lock();
1678 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
1679 if (IS_ERR(rx_sc)) {
1680 rtnl_unlock();
1681 return PTR_ERR(rx_sc);
1682 }
1683
1684 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1685
1686 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1687 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1688 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1689 rtnl_unlock();
1690 return -EINVAL;
1691 }
1692
1693 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1694 if (rx_sa) {
1695 rtnl_unlock();
1696 return -EBUSY;
1697 }
1698
1699 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
1700 if (!rx_sa) {
1701 rtnl_unlock();
1702 return -ENOMEM;
1703 }
1704
1705 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1706 secy->key_len, secy->icv_len);
1707 if (err < 0) {
1708 kfree(rx_sa);
1709 rtnl_unlock();
1710 return err;
1711 }
1712
1713 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1714 spin_lock_bh(&rx_sa->lock);
1715 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1716 spin_unlock_bh(&rx_sa->lock);
1717 }
1718
1719 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1720 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1721
1722 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1723 rx_sa->sc = rx_sc;
1724 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1725
1726 rtnl_unlock();
1727
1728 return 0;
1729 }
1730
1731 static bool validate_add_rxsc(struct nlattr **attrs)
1732 {
1733 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1734 return false;
1735
1736 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1737 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1738 return false;
1739 }
1740
1741 return true;
1742 }
1743
1744 static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1745 {
1746 struct net_device *dev;
1747 sci_t sci = MACSEC_UNDEF_SCI;
1748 struct nlattr **attrs = info->attrs;
1749 struct macsec_rx_sc *rx_sc;
1750 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1751
1752 if (!attrs[MACSEC_ATTR_IFINDEX])
1753 return -EINVAL;
1754
1755 if (parse_rxsc_config(attrs, tb_rxsc))
1756 return -EINVAL;
1757
1758 if (!validate_add_rxsc(tb_rxsc))
1759 return -EINVAL;
1760
1761 rtnl_lock();
1762 dev = get_dev_from_nl(genl_info_net(info), attrs);
1763 if (IS_ERR(dev)) {
1764 rtnl_unlock();
1765 return PTR_ERR(dev);
1766 }
1767
1768 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1769
1770 rx_sc = create_rx_sc(dev, sci);
1771 if (IS_ERR(rx_sc)) {
1772 rtnl_unlock();
1773 return PTR_ERR(rx_sc);
1774 }
1775
1776 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1777 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1778
1779 rtnl_unlock();
1780
1781 return 0;
1782 }
1783
1784 static bool validate_add_txsa(struct nlattr **attrs)
1785 {
1786 if (!attrs[MACSEC_SA_ATTR_AN] ||
1787 !attrs[MACSEC_SA_ATTR_PN] ||
1788 !attrs[MACSEC_SA_ATTR_KEY] ||
1789 !attrs[MACSEC_SA_ATTR_KEYID])
1790 return false;
1791
1792 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1793 return false;
1794
1795 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1796 return false;
1797
1798 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1799 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1800 return false;
1801 }
1802
1803 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1804 return false;
1805
1806 return true;
1807 }
1808
1809 static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1810 {
1811 struct net_device *dev;
1812 struct nlattr **attrs = info->attrs;
1813 struct macsec_secy *secy;
1814 struct macsec_tx_sc *tx_sc;
1815 struct macsec_tx_sa *tx_sa;
1816 unsigned char assoc_num;
1817 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1818 int err;
1819
1820 if (!attrs[MACSEC_ATTR_IFINDEX])
1821 return -EINVAL;
1822
1823 if (parse_sa_config(attrs, tb_sa))
1824 return -EINVAL;
1825
1826 if (!validate_add_txsa(tb_sa))
1827 return -EINVAL;
1828
1829 rtnl_lock();
1830 dev = get_dev_from_nl(genl_info_net(info), attrs);
1831 if (IS_ERR(dev)) {
1832 rtnl_unlock();
1833 return PTR_ERR(dev);
1834 }
1835
1836 secy = &macsec_priv(dev)->secy;
1837 tx_sc = &secy->tx_sc;
1838
1839 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1840
1841 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1842 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1843 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1844 rtnl_unlock();
1845 return -EINVAL;
1846 }
1847
1848 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1849 if (tx_sa) {
1850 rtnl_unlock();
1851 return -EBUSY;
1852 }
1853
1854 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
1855 if (!tx_sa) {
1856 rtnl_unlock();
1857 return -ENOMEM;
1858 }
1859
1860 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1861 secy->key_len, secy->icv_len);
1862 if (err < 0) {
1863 kfree(tx_sa);
1864 rtnl_unlock();
1865 return err;
1866 }
1867
1868 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
1869
1870 spin_lock_bh(&tx_sa->lock);
1871 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1872 spin_unlock_bh(&tx_sa->lock);
1873
1874 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1875 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1876
1877 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1878 secy->operational = true;
1879
1880 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1881
1882 rtnl_unlock();
1883
1884 return 0;
1885 }
1886
1887 static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1888 {
1889 struct nlattr **attrs = info->attrs;
1890 struct net_device *dev;
1891 struct macsec_secy *secy;
1892 struct macsec_rx_sc *rx_sc;
1893 struct macsec_rx_sa *rx_sa;
1894 u8 assoc_num;
1895 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1896 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1897
1898 if (!attrs[MACSEC_ATTR_IFINDEX])
1899 return -EINVAL;
1900
1901 if (parse_sa_config(attrs, tb_sa))
1902 return -EINVAL;
1903
1904 if (parse_rxsc_config(attrs, tb_rxsc))
1905 return -EINVAL;
1906
1907 rtnl_lock();
1908 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1909 &dev, &secy, &rx_sc, &assoc_num);
1910 if (IS_ERR(rx_sa)) {
1911 rtnl_unlock();
1912 return PTR_ERR(rx_sa);
1913 }
1914
1915 if (rx_sa->active) {
1916 rtnl_unlock();
1917 return -EBUSY;
1918 }
1919
1920 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1921 clear_rx_sa(rx_sa);
1922
1923 rtnl_unlock();
1924
1925 return 0;
1926 }
1927
1928 static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1929 {
1930 struct nlattr **attrs = info->attrs;
1931 struct net_device *dev;
1932 struct macsec_secy *secy;
1933 struct macsec_rx_sc *rx_sc;
1934 sci_t sci;
1935 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1936
1937 if (!attrs[MACSEC_ATTR_IFINDEX])
1938 return -EINVAL;
1939
1940 if (parse_rxsc_config(attrs, tb_rxsc))
1941 return -EINVAL;
1942
1943 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1944 return -EINVAL;
1945
1946 rtnl_lock();
1947 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1948 if (IS_ERR(dev)) {
1949 rtnl_unlock();
1950 return PTR_ERR(dev);
1951 }
1952
1953 secy = &macsec_priv(dev)->secy;
1954 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1955
1956 rx_sc = del_rx_sc(secy, sci);
1957 if (!rx_sc) {
1958 rtnl_unlock();
1959 return -ENODEV;
1960 }
1961
1962 free_rx_sc(rx_sc);
1963 rtnl_unlock();
1964
1965 return 0;
1966 }
1967
1968 static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1969 {
1970 struct nlattr **attrs = info->attrs;
1971 struct net_device *dev;
1972 struct macsec_secy *secy;
1973 struct macsec_tx_sc *tx_sc;
1974 struct macsec_tx_sa *tx_sa;
1975 u8 assoc_num;
1976 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1977
1978 if (!attrs[MACSEC_ATTR_IFINDEX])
1979 return -EINVAL;
1980
1981 if (parse_sa_config(attrs, tb_sa))
1982 return -EINVAL;
1983
1984 rtnl_lock();
1985 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
1986 &dev, &secy, &tx_sc, &assoc_num);
1987 if (IS_ERR(tx_sa)) {
1988 rtnl_unlock();
1989 return PTR_ERR(tx_sa);
1990 }
1991
1992 if (tx_sa->active) {
1993 rtnl_unlock();
1994 return -EBUSY;
1995 }
1996
1997 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
1998 clear_tx_sa(tx_sa);
1999
2000 rtnl_unlock();
2001
2002 return 0;
2003 }
2004
2005 static bool validate_upd_sa(struct nlattr **attrs)
2006 {
2007 if (!attrs[MACSEC_SA_ATTR_AN] ||
2008 attrs[MACSEC_SA_ATTR_KEY] ||
2009 attrs[MACSEC_SA_ATTR_KEYID])
2010 return false;
2011
2012 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2013 return false;
2014
2015 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2016 return false;
2017
2018 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2019 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2020 return false;
2021 }
2022
2023 return true;
2024 }
2025
2026 static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2027 {
2028 struct nlattr **attrs = info->attrs;
2029 struct net_device *dev;
2030 struct macsec_secy *secy;
2031 struct macsec_tx_sc *tx_sc;
2032 struct macsec_tx_sa *tx_sa;
2033 u8 assoc_num;
2034 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2035
2036 if (!attrs[MACSEC_ATTR_IFINDEX])
2037 return -EINVAL;
2038
2039 if (parse_sa_config(attrs, tb_sa))
2040 return -EINVAL;
2041
2042 if (!validate_upd_sa(tb_sa))
2043 return -EINVAL;
2044
2045 rtnl_lock();
2046 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2047 &dev, &secy, &tx_sc, &assoc_num);
2048 if (IS_ERR(tx_sa)) {
2049 rtnl_unlock();
2050 return PTR_ERR(tx_sa);
2051 }
2052
2053 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2054 spin_lock_bh(&tx_sa->lock);
2055 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2056 spin_unlock_bh(&tx_sa->lock);
2057 }
2058
2059 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2060 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2061
2062 if (assoc_num == tx_sc->encoding_sa)
2063 secy->operational = tx_sa->active;
2064
2065 rtnl_unlock();
2066
2067 return 0;
2068 }
2069
2070 static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2071 {
2072 struct nlattr **attrs = info->attrs;
2073 struct net_device *dev;
2074 struct macsec_secy *secy;
2075 struct macsec_rx_sc *rx_sc;
2076 struct macsec_rx_sa *rx_sa;
2077 u8 assoc_num;
2078 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2079 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2080
2081 if (!attrs[MACSEC_ATTR_IFINDEX])
2082 return -EINVAL;
2083
2084 if (parse_rxsc_config(attrs, tb_rxsc))
2085 return -EINVAL;
2086
2087 if (parse_sa_config(attrs, tb_sa))
2088 return -EINVAL;
2089
2090 if (!validate_upd_sa(tb_sa))
2091 return -EINVAL;
2092
2093 rtnl_lock();
2094 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2095 &dev, &secy, &rx_sc, &assoc_num);
2096 if (IS_ERR(rx_sa)) {
2097 rtnl_unlock();
2098 return PTR_ERR(rx_sa);
2099 }
2100
2101 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2102 spin_lock_bh(&rx_sa->lock);
2103 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2104 spin_unlock_bh(&rx_sa->lock);
2105 }
2106
2107 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2108 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2109
2110 rtnl_unlock();
2111 return 0;
2112 }
2113
2114 static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2115 {
2116 struct nlattr **attrs = info->attrs;
2117 struct net_device *dev;
2118 struct macsec_secy *secy;
2119 struct macsec_rx_sc *rx_sc;
2120 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2121
2122 if (!attrs[MACSEC_ATTR_IFINDEX])
2123 return -EINVAL;
2124
2125 if (parse_rxsc_config(attrs, tb_rxsc))
2126 return -EINVAL;
2127
2128 if (!validate_add_rxsc(tb_rxsc))
2129 return -EINVAL;
2130
2131 rtnl_lock();
2132 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2133 if (IS_ERR(rx_sc)) {
2134 rtnl_unlock();
2135 return PTR_ERR(rx_sc);
2136 }
2137
2138 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2139 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2140
2141 if (rx_sc->active != new)
2142 secy->n_rx_sc += new ? 1 : -1;
2143
2144 rx_sc->active = new;
2145 }
2146
2147 rtnl_unlock();
2148
2149 return 0;
2150 }
2151
2152 static int copy_tx_sa_stats(struct sk_buff *skb,
2153 struct macsec_tx_sa_stats __percpu *pstats)
2154 {
2155 struct macsec_tx_sa_stats sum = {0, };
2156 int cpu;
2157
2158 for_each_possible_cpu(cpu) {
2159 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2160
2161 sum.OutPktsProtected += stats->OutPktsProtected;
2162 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2163 }
2164
2165 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2166 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2167 return -EMSGSIZE;
2168
2169 return 0;
2170 }
2171
2172 static noinline_for_stack int
2173 copy_rx_sa_stats(struct sk_buff *skb,
2174 struct macsec_rx_sa_stats __percpu *pstats)
2175 {
2176 struct macsec_rx_sa_stats sum = {0, };
2177 int cpu;
2178
2179 for_each_possible_cpu(cpu) {
2180 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2181
2182 sum.InPktsOK += stats->InPktsOK;
2183 sum.InPktsInvalid += stats->InPktsInvalid;
2184 sum.InPktsNotValid += stats->InPktsNotValid;
2185 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2186 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2187 }
2188
2189 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2190 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2191 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2192 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2193 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2194 return -EMSGSIZE;
2195
2196 return 0;
2197 }
2198
2199 static noinline_for_stack int
2200 copy_rx_sc_stats(struct sk_buff *skb, struct pcpu_rx_sc_stats __percpu *pstats)
2201 {
2202 struct macsec_rx_sc_stats sum = {0, };
2203 int cpu;
2204
2205 for_each_possible_cpu(cpu) {
2206 const struct pcpu_rx_sc_stats *stats;
2207 struct macsec_rx_sc_stats tmp;
2208 unsigned int start;
2209
2210 stats = per_cpu_ptr(pstats, cpu);
2211 do {
2212 start = u64_stats_fetch_begin_irq(&stats->syncp);
2213 memcpy(&tmp, &stats->stats, sizeof(tmp));
2214 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2215
2216 sum.InOctetsValidated += tmp.InOctetsValidated;
2217 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2218 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2219 sum.InPktsDelayed += tmp.InPktsDelayed;
2220 sum.InPktsOK += tmp.InPktsOK;
2221 sum.InPktsInvalid += tmp.InPktsInvalid;
2222 sum.InPktsLate += tmp.InPktsLate;
2223 sum.InPktsNotValid += tmp.InPktsNotValid;
2224 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2225 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2226 }
2227
2228 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2229 sum.InOctetsValidated,
2230 MACSEC_RXSC_STATS_ATTR_PAD) ||
2231 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2232 sum.InOctetsDecrypted,
2233 MACSEC_RXSC_STATS_ATTR_PAD) ||
2234 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2235 sum.InPktsUnchecked,
2236 MACSEC_RXSC_STATS_ATTR_PAD) ||
2237 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2238 sum.InPktsDelayed,
2239 MACSEC_RXSC_STATS_ATTR_PAD) ||
2240 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2241 sum.InPktsOK,
2242 MACSEC_RXSC_STATS_ATTR_PAD) ||
2243 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2244 sum.InPktsInvalid,
2245 MACSEC_RXSC_STATS_ATTR_PAD) ||
2246 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2247 sum.InPktsLate,
2248 MACSEC_RXSC_STATS_ATTR_PAD) ||
2249 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2250 sum.InPktsNotValid,
2251 MACSEC_RXSC_STATS_ATTR_PAD) ||
2252 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2253 sum.InPktsNotUsingSA,
2254 MACSEC_RXSC_STATS_ATTR_PAD) ||
2255 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2256 sum.InPktsUnusedSA,
2257 MACSEC_RXSC_STATS_ATTR_PAD))
2258 return -EMSGSIZE;
2259
2260 return 0;
2261 }
2262
2263 static noinline_for_stack int
2264 copy_tx_sc_stats(struct sk_buff *skb, struct pcpu_tx_sc_stats __percpu *pstats)
2265 {
2266 struct macsec_tx_sc_stats sum = {0, };
2267 int cpu;
2268
2269 for_each_possible_cpu(cpu) {
2270 const struct pcpu_tx_sc_stats *stats;
2271 struct macsec_tx_sc_stats tmp;
2272 unsigned int start;
2273
2274 stats = per_cpu_ptr(pstats, cpu);
2275 do {
2276 start = u64_stats_fetch_begin_irq(&stats->syncp);
2277 memcpy(&tmp, &stats->stats, sizeof(tmp));
2278 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2279
2280 sum.OutPktsProtected += tmp.OutPktsProtected;
2281 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2282 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2283 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2284 }
2285
2286 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2287 sum.OutPktsProtected,
2288 MACSEC_TXSC_STATS_ATTR_PAD) ||
2289 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2290 sum.OutPktsEncrypted,
2291 MACSEC_TXSC_STATS_ATTR_PAD) ||
2292 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2293 sum.OutOctetsProtected,
2294 MACSEC_TXSC_STATS_ATTR_PAD) ||
2295 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2296 sum.OutOctetsEncrypted,
2297 MACSEC_TXSC_STATS_ATTR_PAD))
2298 return -EMSGSIZE;
2299
2300 return 0;
2301 }
2302
2303 static noinline_for_stack int
2304 copy_secy_stats(struct sk_buff *skb, struct pcpu_secy_stats __percpu *pstats)
2305 {
2306 struct macsec_dev_stats sum = {0, };
2307 int cpu;
2308
2309 for_each_possible_cpu(cpu) {
2310 const struct pcpu_secy_stats *stats;
2311 struct macsec_dev_stats tmp;
2312 unsigned int start;
2313
2314 stats = per_cpu_ptr(pstats, cpu);
2315 do {
2316 start = u64_stats_fetch_begin_irq(&stats->syncp);
2317 memcpy(&tmp, &stats->stats, sizeof(tmp));
2318 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2319
2320 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2321 sum.InPktsUntagged += tmp.InPktsUntagged;
2322 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2323 sum.InPktsNoTag += tmp.InPktsNoTag;
2324 sum.InPktsBadTag += tmp.InPktsBadTag;
2325 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2326 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2327 sum.InPktsOverrun += tmp.InPktsOverrun;
2328 }
2329
2330 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2331 sum.OutPktsUntagged,
2332 MACSEC_SECY_STATS_ATTR_PAD) ||
2333 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2334 sum.InPktsUntagged,
2335 MACSEC_SECY_STATS_ATTR_PAD) ||
2336 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2337 sum.OutPktsTooLong,
2338 MACSEC_SECY_STATS_ATTR_PAD) ||
2339 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2340 sum.InPktsNoTag,
2341 MACSEC_SECY_STATS_ATTR_PAD) ||
2342 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2343 sum.InPktsBadTag,
2344 MACSEC_SECY_STATS_ATTR_PAD) ||
2345 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2346 sum.InPktsUnknownSCI,
2347 MACSEC_SECY_STATS_ATTR_PAD) ||
2348 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2349 sum.InPktsNoSCI,
2350 MACSEC_SECY_STATS_ATTR_PAD) ||
2351 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2352 sum.InPktsOverrun,
2353 MACSEC_SECY_STATS_ATTR_PAD))
2354 return -EMSGSIZE;
2355
2356 return 0;
2357 }
2358
2359 static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2360 {
2361 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2362 struct nlattr *secy_nest = nla_nest_start_noflag(skb,
2363 MACSEC_ATTR_SECY);
2364 u64 csid;
2365
2366 if (!secy_nest)
2367 return 1;
2368
2369 switch (secy->key_len) {
2370 case MACSEC_GCM_AES_128_SAK_LEN:
2371 csid = MACSEC_DEFAULT_CIPHER_ID;
2372 break;
2373 case MACSEC_GCM_AES_256_SAK_LEN:
2374 csid = MACSEC_CIPHER_ID_GCM_AES_256;
2375 break;
2376 default:
2377 goto cancel;
2378 }
2379
2380 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2381 MACSEC_SECY_ATTR_PAD) ||
2382 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
2383 csid, MACSEC_SECY_ATTR_PAD) ||
2384 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2385 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2386 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2387 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2388 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2389 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2390 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2391 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2392 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2393 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2394 goto cancel;
2395
2396 if (secy->replay_protect) {
2397 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2398 goto cancel;
2399 }
2400
2401 nla_nest_end(skb, secy_nest);
2402 return 0;
2403
2404 cancel:
2405 nla_nest_cancel(skb, secy_nest);
2406 return 1;
2407 }
2408
2409 static noinline_for_stack int
2410 dump_secy(struct macsec_secy *secy, struct net_device *dev,
2411 struct sk_buff *skb, struct netlink_callback *cb)
2412 {
2413 struct macsec_rx_sc *rx_sc;
2414 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2415 struct nlattr *txsa_list, *rxsc_list;
2416 int i, j;
2417 void *hdr;
2418 struct nlattr *attr;
2419
2420 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2421 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2422 if (!hdr)
2423 return -EMSGSIZE;
2424
2425 genl_dump_check_consistent(cb, hdr);
2426
2427 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2428 goto nla_put_failure;
2429
2430 if (nla_put_secy(secy, skb))
2431 goto nla_put_failure;
2432
2433 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
2434 if (!attr)
2435 goto nla_put_failure;
2436 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2437 nla_nest_cancel(skb, attr);
2438 goto nla_put_failure;
2439 }
2440 nla_nest_end(skb, attr);
2441
2442 attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
2443 if (!attr)
2444 goto nla_put_failure;
2445 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2446 nla_nest_cancel(skb, attr);
2447 goto nla_put_failure;
2448 }
2449 nla_nest_end(skb, attr);
2450
2451 txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
2452 if (!txsa_list)
2453 goto nla_put_failure;
2454 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2455 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2456 struct nlattr *txsa_nest;
2457
2458 if (!tx_sa)
2459 continue;
2460
2461 txsa_nest = nla_nest_start_noflag(skb, j++);
2462 if (!txsa_nest) {
2463 nla_nest_cancel(skb, txsa_list);
2464 goto nla_put_failure;
2465 }
2466
2467 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2468 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
2469 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
2470 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2471 nla_nest_cancel(skb, txsa_nest);
2472 nla_nest_cancel(skb, txsa_list);
2473 goto nla_put_failure;
2474 }
2475
2476 attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
2477 if (!attr) {
2478 nla_nest_cancel(skb, txsa_nest);
2479 nla_nest_cancel(skb, txsa_list);
2480 goto nla_put_failure;
2481 }
2482 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2483 nla_nest_cancel(skb, attr);
2484 nla_nest_cancel(skb, txsa_nest);
2485 nla_nest_cancel(skb, txsa_list);
2486 goto nla_put_failure;
2487 }
2488 nla_nest_end(skb, attr);
2489
2490 nla_nest_end(skb, txsa_nest);
2491 }
2492 nla_nest_end(skb, txsa_list);
2493
2494 rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
2495 if (!rxsc_list)
2496 goto nla_put_failure;
2497
2498 j = 1;
2499 for_each_rxsc_rtnl(secy, rx_sc) {
2500 int k;
2501 struct nlattr *rxsa_list;
2502 struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
2503
2504 if (!rxsc_nest) {
2505 nla_nest_cancel(skb, rxsc_list);
2506 goto nla_put_failure;
2507 }
2508
2509 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
2510 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2511 MACSEC_RXSC_ATTR_PAD)) {
2512 nla_nest_cancel(skb, rxsc_nest);
2513 nla_nest_cancel(skb, rxsc_list);
2514 goto nla_put_failure;
2515 }
2516
2517 attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
2518 if (!attr) {
2519 nla_nest_cancel(skb, rxsc_nest);
2520 nla_nest_cancel(skb, rxsc_list);
2521 goto nla_put_failure;
2522 }
2523 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2524 nla_nest_cancel(skb, attr);
2525 nla_nest_cancel(skb, rxsc_nest);
2526 nla_nest_cancel(skb, rxsc_list);
2527 goto nla_put_failure;
2528 }
2529 nla_nest_end(skb, attr);
2530
2531 rxsa_list = nla_nest_start_noflag(skb,
2532 MACSEC_RXSC_ATTR_SA_LIST);
2533 if (!rxsa_list) {
2534 nla_nest_cancel(skb, rxsc_nest);
2535 nla_nest_cancel(skb, rxsc_list);
2536 goto nla_put_failure;
2537 }
2538
2539 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2540 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2541 struct nlattr *rxsa_nest;
2542
2543 if (!rx_sa)
2544 continue;
2545
2546 rxsa_nest = nla_nest_start_noflag(skb, k++);
2547 if (!rxsa_nest) {
2548 nla_nest_cancel(skb, rxsa_list);
2549 nla_nest_cancel(skb, rxsc_nest);
2550 nla_nest_cancel(skb, rxsc_list);
2551 goto nla_put_failure;
2552 }
2553
2554 attr = nla_nest_start_noflag(skb,
2555 MACSEC_SA_ATTR_STATS);
2556 if (!attr) {
2557 nla_nest_cancel(skb, rxsa_list);
2558 nla_nest_cancel(skb, rxsc_nest);
2559 nla_nest_cancel(skb, rxsc_list);
2560 goto nla_put_failure;
2561 }
2562 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2563 nla_nest_cancel(skb, attr);
2564 nla_nest_cancel(skb, rxsa_list);
2565 nla_nest_cancel(skb, rxsc_nest);
2566 nla_nest_cancel(skb, rxsc_list);
2567 goto nla_put_failure;
2568 }
2569 nla_nest_end(skb, attr);
2570
2571 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2572 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
2573 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
2574 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2575 nla_nest_cancel(skb, rxsa_nest);
2576 nla_nest_cancel(skb, rxsc_nest);
2577 nla_nest_cancel(skb, rxsc_list);
2578 goto nla_put_failure;
2579 }
2580 nla_nest_end(skb, rxsa_nest);
2581 }
2582
2583 nla_nest_end(skb, rxsa_list);
2584 nla_nest_end(skb, rxsc_nest);
2585 }
2586
2587 nla_nest_end(skb, rxsc_list);
2588
2589 genlmsg_end(skb, hdr);
2590
2591 return 0;
2592
2593 nla_put_failure:
2594 genlmsg_cancel(skb, hdr);
2595 return -EMSGSIZE;
2596 }
2597
2598 static int macsec_generation = 1;
2599
2600 static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2601 {
2602 struct net *net = sock_net(skb->sk);
2603 struct net_device *dev;
2604 int dev_idx, d;
2605
2606 dev_idx = cb->args[0];
2607
2608 d = 0;
2609 rtnl_lock();
2610
2611 cb->seq = macsec_generation;
2612
2613 for_each_netdev(net, dev) {
2614 struct macsec_secy *secy;
2615
2616 if (d < dev_idx)
2617 goto next;
2618
2619 if (!netif_is_macsec(dev))
2620 goto next;
2621
2622 secy = &macsec_priv(dev)->secy;
2623 if (dump_secy(secy, dev, skb, cb) < 0)
2624 goto done;
2625 next:
2626 d++;
2627 }
2628
2629 done:
2630 rtnl_unlock();
2631 cb->args[0] = d;
2632 return skb->len;
2633 }
2634
2635 static const struct genl_ops macsec_genl_ops[] = {
2636 {
2637 .cmd = MACSEC_CMD_GET_TXSC,
2638 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2639 .dumpit = macsec_dump_txsc,
2640 },
2641 {
2642 .cmd = MACSEC_CMD_ADD_RXSC,
2643 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2644 .doit = macsec_add_rxsc,
2645 .flags = GENL_ADMIN_PERM,
2646 },
2647 {
2648 .cmd = MACSEC_CMD_DEL_RXSC,
2649 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2650 .doit = macsec_del_rxsc,
2651 .flags = GENL_ADMIN_PERM,
2652 },
2653 {
2654 .cmd = MACSEC_CMD_UPD_RXSC,
2655 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2656 .doit = macsec_upd_rxsc,
2657 .flags = GENL_ADMIN_PERM,
2658 },
2659 {
2660 .cmd = MACSEC_CMD_ADD_TXSA,
2661 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2662 .doit = macsec_add_txsa,
2663 .flags = GENL_ADMIN_PERM,
2664 },
2665 {
2666 .cmd = MACSEC_CMD_DEL_TXSA,
2667 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2668 .doit = macsec_del_txsa,
2669 .flags = GENL_ADMIN_PERM,
2670 },
2671 {
2672 .cmd = MACSEC_CMD_UPD_TXSA,
2673 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2674 .doit = macsec_upd_txsa,
2675 .flags = GENL_ADMIN_PERM,
2676 },
2677 {
2678 .cmd = MACSEC_CMD_ADD_RXSA,
2679 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2680 .doit = macsec_add_rxsa,
2681 .flags = GENL_ADMIN_PERM,
2682 },
2683 {
2684 .cmd = MACSEC_CMD_DEL_RXSA,
2685 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2686 .doit = macsec_del_rxsa,
2687 .flags = GENL_ADMIN_PERM,
2688 },
2689 {
2690 .cmd = MACSEC_CMD_UPD_RXSA,
2691 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2692 .doit = macsec_upd_rxsa,
2693 .flags = GENL_ADMIN_PERM,
2694 },
2695 };
2696
2697 static struct genl_family macsec_fam __ro_after_init = {
2698 .name = MACSEC_GENL_NAME,
2699 .hdrsize = 0,
2700 .version = MACSEC_GENL_VERSION,
2701 .maxattr = MACSEC_ATTR_MAX,
2702 .policy = macsec_genl_policy,
2703 .netnsok = true,
2704 .module = THIS_MODULE,
2705 .ops = macsec_genl_ops,
2706 .n_ops = ARRAY_SIZE(macsec_genl_ops),
2707 };
2708
2709 static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2710 struct net_device *dev)
2711 {
2712 struct macsec_dev *macsec = netdev_priv(dev);
2713 struct macsec_secy *secy = &macsec->secy;
2714 struct pcpu_secy_stats *secy_stats;
2715 int ret, len;
2716
2717
2718 if (!secy->protect_frames) {
2719 secy_stats = this_cpu_ptr(macsec->stats);
2720 u64_stats_update_begin(&secy_stats->syncp);
2721 secy_stats->stats.OutPktsUntagged++;
2722 u64_stats_update_end(&secy_stats->syncp);
2723 skb->dev = macsec->real_dev;
2724 len = skb->len;
2725 ret = dev_queue_xmit(skb);
2726 count_tx(dev, ret, len);
2727 return ret;
2728 }
2729
2730 if (!secy->operational) {
2731 kfree_skb(skb);
2732 dev->stats.tx_dropped++;
2733 return NETDEV_TX_OK;
2734 }
2735
2736 skb = macsec_encrypt(skb, dev);
2737 if (IS_ERR(skb)) {
2738 if (PTR_ERR(skb) != -EINPROGRESS)
2739 dev->stats.tx_dropped++;
2740 return NETDEV_TX_OK;
2741 }
2742
2743 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2744
2745 macsec_encrypt_finish(skb, dev);
2746 len = skb->len;
2747 ret = dev_queue_xmit(skb);
2748 count_tx(dev, ret, len);
2749 return ret;
2750 }
2751
2752 #define MACSEC_FEATURES \
2753 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
2754
2755 static int macsec_dev_init(struct net_device *dev)
2756 {
2757 struct macsec_dev *macsec = macsec_priv(dev);
2758 struct net_device *real_dev = macsec->real_dev;
2759 int err;
2760
2761 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2762 if (!dev->tstats)
2763 return -ENOMEM;
2764
2765 err = gro_cells_init(&macsec->gro_cells, dev);
2766 if (err) {
2767 free_percpu(dev->tstats);
2768 return err;
2769 }
2770
2771 dev->features = real_dev->features & MACSEC_FEATURES;
2772 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2773
2774 dev->needed_headroom = real_dev->needed_headroom +
2775 MACSEC_NEEDED_HEADROOM;
2776 dev->needed_tailroom = real_dev->needed_tailroom +
2777 MACSEC_NEEDED_TAILROOM;
2778
2779 if (is_zero_ether_addr(dev->dev_addr))
2780 eth_hw_addr_inherit(dev, real_dev);
2781 if (is_zero_ether_addr(dev->broadcast))
2782 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2783
2784 return 0;
2785 }
2786
2787 static void macsec_dev_uninit(struct net_device *dev)
2788 {
2789 struct macsec_dev *macsec = macsec_priv(dev);
2790
2791 gro_cells_destroy(&macsec->gro_cells);
2792 free_percpu(dev->tstats);
2793 }
2794
2795 static netdev_features_t macsec_fix_features(struct net_device *dev,
2796 netdev_features_t features)
2797 {
2798 struct macsec_dev *macsec = macsec_priv(dev);
2799 struct net_device *real_dev = macsec->real_dev;
2800
2801 features &= (real_dev->features & MACSEC_FEATURES) |
2802 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2803 features |= NETIF_F_LLTX;
2804
2805 return features;
2806 }
2807
2808 static int macsec_dev_open(struct net_device *dev)
2809 {
2810 struct macsec_dev *macsec = macsec_priv(dev);
2811 struct net_device *real_dev = macsec->real_dev;
2812 int err;
2813
2814 err = dev_uc_add(real_dev, dev->dev_addr);
2815 if (err < 0)
2816 return err;
2817
2818 if (dev->flags & IFF_ALLMULTI) {
2819 err = dev_set_allmulti(real_dev, 1);
2820 if (err < 0)
2821 goto del_unicast;
2822 }
2823
2824 if (dev->flags & IFF_PROMISC) {
2825 err = dev_set_promiscuity(real_dev, 1);
2826 if (err < 0)
2827 goto clear_allmulti;
2828 }
2829
2830 if (netif_carrier_ok(real_dev))
2831 netif_carrier_on(dev);
2832
2833 return 0;
2834 clear_allmulti:
2835 if (dev->flags & IFF_ALLMULTI)
2836 dev_set_allmulti(real_dev, -1);
2837 del_unicast:
2838 dev_uc_del(real_dev, dev->dev_addr);
2839 netif_carrier_off(dev);
2840 return err;
2841 }
2842
2843 static int macsec_dev_stop(struct net_device *dev)
2844 {
2845 struct macsec_dev *macsec = macsec_priv(dev);
2846 struct net_device *real_dev = macsec->real_dev;
2847
2848 netif_carrier_off(dev);
2849
2850 dev_mc_unsync(real_dev, dev);
2851 dev_uc_unsync(real_dev, dev);
2852
2853 if (dev->flags & IFF_ALLMULTI)
2854 dev_set_allmulti(real_dev, -1);
2855
2856 if (dev->flags & IFF_PROMISC)
2857 dev_set_promiscuity(real_dev, -1);
2858
2859 dev_uc_del(real_dev, dev->dev_addr);
2860
2861 return 0;
2862 }
2863
2864 static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2865 {
2866 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2867
2868 if (!(dev->flags & IFF_UP))
2869 return;
2870
2871 if (change & IFF_ALLMULTI)
2872 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2873
2874 if (change & IFF_PROMISC)
2875 dev_set_promiscuity(real_dev,
2876 dev->flags & IFF_PROMISC ? 1 : -1);
2877 }
2878
2879 static void macsec_dev_set_rx_mode(struct net_device *dev)
2880 {
2881 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2882
2883 dev_mc_sync(real_dev, dev);
2884 dev_uc_sync(real_dev, dev);
2885 }
2886
2887 static sci_t dev_to_sci(struct net_device *dev, __be16 port)
2888 {
2889 return make_sci(dev->dev_addr, port);
2890 }
2891
2892 static int macsec_set_mac_address(struct net_device *dev, void *p)
2893 {
2894 struct macsec_dev *macsec = macsec_priv(dev);
2895 struct net_device *real_dev = macsec->real_dev;
2896 struct sockaddr *addr = p;
2897 int err;
2898
2899 if (!is_valid_ether_addr(addr->sa_data))
2900 return -EADDRNOTAVAIL;
2901
2902 if (!(dev->flags & IFF_UP))
2903 goto out;
2904
2905 err = dev_uc_add(real_dev, addr->sa_data);
2906 if (err < 0)
2907 return err;
2908
2909 dev_uc_del(real_dev, dev->dev_addr);
2910
2911 out:
2912 ether_addr_copy(dev->dev_addr, addr->sa_data);
2913 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
2914 return 0;
2915 }
2916
2917 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2918 {
2919 struct macsec_dev *macsec = macsec_priv(dev);
2920 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2921
2922 if (macsec->real_dev->mtu - extra < new_mtu)
2923 return -ERANGE;
2924
2925 dev->mtu = new_mtu;
2926
2927 return 0;
2928 }
2929
2930 static void macsec_get_stats64(struct net_device *dev,
2931 struct rtnl_link_stats64 *s)
2932 {
2933 int cpu;
2934
2935 if (!dev->tstats)
2936 return;
2937
2938 for_each_possible_cpu(cpu) {
2939 struct pcpu_sw_netstats *stats;
2940 struct pcpu_sw_netstats tmp;
2941 int start;
2942
2943 stats = per_cpu_ptr(dev->tstats, cpu);
2944 do {
2945 start = u64_stats_fetch_begin_irq(&stats->syncp);
2946 tmp.rx_packets = stats->rx_packets;
2947 tmp.rx_bytes = stats->rx_bytes;
2948 tmp.tx_packets = stats->tx_packets;
2949 tmp.tx_bytes = stats->tx_bytes;
2950 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2951
2952 s->rx_packets += tmp.rx_packets;
2953 s->rx_bytes += tmp.rx_bytes;
2954 s->tx_packets += tmp.tx_packets;
2955 s->tx_bytes += tmp.tx_bytes;
2956 }
2957
2958 s->rx_dropped = dev->stats.rx_dropped;
2959 s->tx_dropped = dev->stats.tx_dropped;
2960 }
2961
2962 static int macsec_get_iflink(const struct net_device *dev)
2963 {
2964 return macsec_priv(dev)->real_dev->ifindex;
2965 }
2966
2967 static const struct net_device_ops macsec_netdev_ops = {
2968 .ndo_init = macsec_dev_init,
2969 .ndo_uninit = macsec_dev_uninit,
2970 .ndo_open = macsec_dev_open,
2971 .ndo_stop = macsec_dev_stop,
2972 .ndo_fix_features = macsec_fix_features,
2973 .ndo_change_mtu = macsec_change_mtu,
2974 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2975 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2976 .ndo_set_mac_address = macsec_set_mac_address,
2977 .ndo_start_xmit = macsec_start_xmit,
2978 .ndo_get_stats64 = macsec_get_stats64,
2979 .ndo_get_iflink = macsec_get_iflink,
2980 };
2981
2982 static const struct device_type macsec_type = {
2983 .name = "macsec",
2984 };
2985
2986 static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2987 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
2988 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
2989 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2990 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2991 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2992 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2993 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2994 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2995 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2996 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
2997 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
2998 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
2999 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
3000 };
3001
3002 static void macsec_free_netdev(struct net_device *dev)
3003 {
3004 struct macsec_dev *macsec = macsec_priv(dev);
3005
3006 free_percpu(macsec->stats);
3007 free_percpu(macsec->secy.tx_sc.stats);
3008
3009 }
3010
3011 static void macsec_setup(struct net_device *dev)
3012 {
3013 ether_setup(dev);
3014 dev->min_mtu = 0;
3015 dev->max_mtu = ETH_MAX_MTU;
3016 dev->priv_flags |= IFF_NO_QUEUE;
3017 dev->netdev_ops = &macsec_netdev_ops;
3018 dev->needs_free_netdev = true;
3019 dev->priv_destructor = macsec_free_netdev;
3020 SET_NETDEV_DEVTYPE(dev, &macsec_type);
3021
3022 eth_zero_addr(dev->broadcast);
3023 }
3024
3025 static int macsec_changelink_common(struct net_device *dev,
3026 struct nlattr *data[])
3027 {
3028 struct macsec_secy *secy;
3029 struct macsec_tx_sc *tx_sc;
3030
3031 secy = &macsec_priv(dev)->secy;
3032 tx_sc = &secy->tx_sc;
3033
3034 if (data[IFLA_MACSEC_ENCODING_SA]) {
3035 struct macsec_tx_sa *tx_sa;
3036
3037 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3038 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3039
3040 secy->operational = tx_sa && tx_sa->active;
3041 }
3042
3043 if (data[IFLA_MACSEC_WINDOW])
3044 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3045
3046 if (data[IFLA_MACSEC_ENCRYPT])
3047 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3048
3049 if (data[IFLA_MACSEC_PROTECT])
3050 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3051
3052 if (data[IFLA_MACSEC_INC_SCI])
3053 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3054
3055 if (data[IFLA_MACSEC_ES])
3056 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3057
3058 if (data[IFLA_MACSEC_SCB])
3059 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3060
3061 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3062 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3063
3064 if (data[IFLA_MACSEC_VALIDATION])
3065 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3066
3067 if (data[IFLA_MACSEC_CIPHER_SUITE]) {
3068 switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
3069 case MACSEC_CIPHER_ID_GCM_AES_128:
3070 case MACSEC_DEFAULT_CIPHER_ID:
3071 secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
3072 break;
3073 case MACSEC_CIPHER_ID_GCM_AES_256:
3074 secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
3075 break;
3076 default:
3077 return -EINVAL;
3078 }
3079 }
3080
3081 return 0;
3082 }
3083
3084 static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3085 struct nlattr *data[],
3086 struct netlink_ext_ack *extack)
3087 {
3088 if (!data)
3089 return 0;
3090
3091 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3092 data[IFLA_MACSEC_ICV_LEN] ||
3093 data[IFLA_MACSEC_SCI] ||
3094 data[IFLA_MACSEC_PORT])
3095 return -EINVAL;
3096
3097 return macsec_changelink_common(dev, data);
3098 }
3099
3100 static void macsec_del_dev(struct macsec_dev *macsec)
3101 {
3102 int i;
3103
3104 while (macsec->secy.rx_sc) {
3105 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3106
3107 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3108 free_rx_sc(rx_sc);
3109 }
3110
3111 for (i = 0; i < MACSEC_NUM_AN; i++) {
3112 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3113
3114 if (sa) {
3115 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3116 clear_tx_sa(sa);
3117 }
3118 }
3119 }
3120
3121 static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3122 {
3123 struct macsec_dev *macsec = macsec_priv(dev);
3124 struct net_device *real_dev = macsec->real_dev;
3125
3126 unregister_netdevice_queue(dev, head);
3127 list_del_rcu(&macsec->secys);
3128 macsec_del_dev(macsec);
3129 netdev_upper_dev_unlink(real_dev, dev);
3130
3131 macsec_generation++;
3132 }
3133
3134 static void macsec_dellink(struct net_device *dev, struct list_head *head)
3135 {
3136 struct macsec_dev *macsec = macsec_priv(dev);
3137 struct net_device *real_dev = macsec->real_dev;
3138 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3139
3140 macsec_common_dellink(dev, head);
3141
3142 if (list_empty(&rxd->secys)) {
3143 netdev_rx_handler_unregister(real_dev);
3144 kfree(rxd);
3145 }
3146 }
3147
3148 static int register_macsec_dev(struct net_device *real_dev,
3149 struct net_device *dev)
3150 {
3151 struct macsec_dev *macsec = macsec_priv(dev);
3152 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3153
3154 if (!rxd) {
3155 int err;
3156
3157 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3158 if (!rxd)
3159 return -ENOMEM;
3160
3161 INIT_LIST_HEAD(&rxd->secys);
3162
3163 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3164 rxd);
3165 if (err < 0) {
3166 kfree(rxd);
3167 return err;
3168 }
3169 }
3170
3171 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3172 return 0;
3173 }
3174
3175 static bool sci_exists(struct net_device *dev, sci_t sci)
3176 {
3177 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3178 struct macsec_dev *macsec;
3179
3180 list_for_each_entry(macsec, &rxd->secys, secys) {
3181 if (macsec->secy.sci == sci)
3182 return true;
3183 }
3184
3185 return false;
3186 }
3187
3188 static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3189 {
3190 struct macsec_dev *macsec = macsec_priv(dev);
3191 struct macsec_secy *secy = &macsec->secy;
3192
3193 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3194 if (!macsec->stats)
3195 return -ENOMEM;
3196
3197 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3198 if (!secy->tx_sc.stats) {
3199 free_percpu(macsec->stats);
3200 return -ENOMEM;
3201 }
3202
3203 if (sci == MACSEC_UNDEF_SCI)
3204 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3205
3206 secy->netdev = dev;
3207 secy->operational = true;
3208 secy->key_len = DEFAULT_SAK_LEN;
3209 secy->icv_len = icv_len;
3210 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3211 secy->protect_frames = true;
3212 secy->replay_protect = false;
3213
3214 secy->sci = sci;
3215 secy->tx_sc.active = true;
3216 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3217 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3218 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3219 secy->tx_sc.end_station = false;
3220 secy->tx_sc.scb = false;
3221
3222 return 0;
3223 }
3224
3225 static int macsec_newlink(struct net *net, struct net_device *dev,
3226 struct nlattr *tb[], struct nlattr *data[],
3227 struct netlink_ext_ack *extack)
3228 {
3229 struct macsec_dev *macsec = macsec_priv(dev);
3230 rx_handler_func_t *rx_handler;
3231 u8 icv_len = DEFAULT_ICV_LEN;
3232 struct net_device *real_dev;
3233 int err, mtu;
3234 sci_t sci;
3235
3236 if (!tb[IFLA_LINK])
3237 return -EINVAL;
3238 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3239 if (!real_dev)
3240 return -ENODEV;
3241 if (real_dev->type != ARPHRD_ETHER)
3242 return -EINVAL;
3243
3244 dev->priv_flags |= IFF_MACSEC;
3245
3246 macsec->real_dev = real_dev;
3247
3248 if (data && data[IFLA_MACSEC_ICV_LEN])
3249 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3250 mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3251 if (mtu < 0)
3252 dev->mtu = 0;
3253 else
3254 dev->mtu = mtu;
3255
3256 rx_handler = rtnl_dereference(real_dev->rx_handler);
3257 if (rx_handler && rx_handler != macsec_handle_frame)
3258 return -EBUSY;
3259
3260 err = register_netdevice(dev);
3261 if (err < 0)
3262 return err;
3263
3264 err = netdev_upper_dev_link(real_dev, dev, extack);
3265 if (err < 0)
3266 goto unregister;
3267
3268
3269
3270
3271 if (data && data[IFLA_MACSEC_SCI])
3272 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3273 else if (data && data[IFLA_MACSEC_PORT])
3274 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3275 else
3276 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3277
3278 if (rx_handler && sci_exists(real_dev, sci)) {
3279 err = -EBUSY;
3280 goto unlink;
3281 }
3282
3283 err = macsec_add_dev(dev, sci, icv_len);
3284 if (err)
3285 goto unlink;
3286
3287 if (data) {
3288 err = macsec_changelink_common(dev, data);
3289 if (err)
3290 goto del_dev;
3291 }
3292
3293 err = register_macsec_dev(real_dev, dev);
3294 if (err < 0)
3295 goto del_dev;
3296
3297 netif_stacked_transfer_operstate(real_dev, dev);
3298 linkwatch_fire_event(dev);
3299
3300 macsec_generation++;
3301
3302 return 0;
3303
3304 del_dev:
3305 macsec_del_dev(macsec);
3306 unlink:
3307 netdev_upper_dev_unlink(real_dev, dev);
3308 unregister:
3309 unregister_netdevice(dev);
3310 return err;
3311 }
3312
3313 static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
3314 struct netlink_ext_ack *extack)
3315 {
3316 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
3317 u8 icv_len = DEFAULT_ICV_LEN;
3318 int flag;
3319 bool es, scb, sci;
3320
3321 if (!data)
3322 return 0;
3323
3324 if (data[IFLA_MACSEC_CIPHER_SUITE])
3325 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3326
3327 if (data[IFLA_MACSEC_ICV_LEN]) {
3328 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3329 if (icv_len != DEFAULT_ICV_LEN) {
3330 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3331 struct crypto_aead *dummy_tfm;
3332
3333 dummy_tfm = macsec_alloc_tfm(dummy_key,
3334 DEFAULT_SAK_LEN,
3335 icv_len);
3336 if (IS_ERR(dummy_tfm))
3337 return PTR_ERR(dummy_tfm);
3338 crypto_free_aead(dummy_tfm);
3339 }
3340 }
3341
3342 switch (csid) {
3343 case MACSEC_CIPHER_ID_GCM_AES_128:
3344 case MACSEC_CIPHER_ID_GCM_AES_256:
3345 case MACSEC_DEFAULT_CIPHER_ID:
3346 if (icv_len < MACSEC_MIN_ICV_LEN ||
3347 icv_len > MACSEC_STD_ICV_LEN)
3348 return -EINVAL;
3349 break;
3350 default:
3351 return -EINVAL;
3352 }
3353
3354 if (data[IFLA_MACSEC_ENCODING_SA]) {
3355 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3356 return -EINVAL;
3357 }
3358
3359 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3360 flag < IFLA_MACSEC_VALIDATION;
3361 flag++) {
3362 if (data[flag]) {
3363 if (nla_get_u8(data[flag]) > 1)
3364 return -EINVAL;
3365 }
3366 }
3367
3368 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3369 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3370 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3371
3372 if ((sci && (scb || es)) || (scb && es))
3373 return -EINVAL;
3374
3375 if (data[IFLA_MACSEC_VALIDATION] &&
3376 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3377 return -EINVAL;
3378
3379 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3380 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
3381 !data[IFLA_MACSEC_WINDOW])
3382 return -EINVAL;
3383
3384 return 0;
3385 }
3386
3387 static struct net *macsec_get_link_net(const struct net_device *dev)
3388 {
3389 return dev_net(macsec_priv(dev)->real_dev);
3390 }
3391
3392 static size_t macsec_get_size(const struct net_device *dev)
3393 {
3394 return nla_total_size_64bit(8) +
3395 nla_total_size(1) +
3396 nla_total_size_64bit(8) +
3397 nla_total_size(4) +
3398 nla_total_size(1) +
3399 nla_total_size(1) +
3400 nla_total_size(1) +
3401 nla_total_size(1) +
3402 nla_total_size(1) +
3403 nla_total_size(1) +
3404 nla_total_size(1) +
3405 nla_total_size(1) +
3406 0;
3407 }
3408
3409 static int macsec_fill_info(struct sk_buff *skb,
3410 const struct net_device *dev)
3411 {
3412 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3413 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3414 u64 csid;
3415
3416 switch (secy->key_len) {
3417 case MACSEC_GCM_AES_128_SAK_LEN:
3418 csid = MACSEC_DEFAULT_CIPHER_ID;
3419 break;
3420 case MACSEC_GCM_AES_256_SAK_LEN:
3421 csid = MACSEC_CIPHER_ID_GCM_AES_256;
3422 break;
3423 default:
3424 goto nla_put_failure;
3425 }
3426
3427 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3428 IFLA_MACSEC_PAD) ||
3429 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
3430 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
3431 csid, IFLA_MACSEC_PAD) ||
3432 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3433 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3434 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3435 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3436 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3437 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3438 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3439 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3440 0)
3441 goto nla_put_failure;
3442
3443 if (secy->replay_protect) {
3444 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3445 goto nla_put_failure;
3446 }
3447
3448 return 0;
3449
3450 nla_put_failure:
3451 return -EMSGSIZE;
3452 }
3453
3454 static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3455 .kind = "macsec",
3456 .priv_size = sizeof(struct macsec_dev),
3457 .maxtype = IFLA_MACSEC_MAX,
3458 .policy = macsec_rtnl_policy,
3459 .setup = macsec_setup,
3460 .validate = macsec_validate_attr,
3461 .newlink = macsec_newlink,
3462 .changelink = macsec_changelink,
3463 .dellink = macsec_dellink,
3464 .get_size = macsec_get_size,
3465 .fill_info = macsec_fill_info,
3466 .get_link_net = macsec_get_link_net,
3467 };
3468
3469 static bool is_macsec_master(struct net_device *dev)
3470 {
3471 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3472 }
3473
3474 static int macsec_notify(struct notifier_block *this, unsigned long event,
3475 void *ptr)
3476 {
3477 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3478 LIST_HEAD(head);
3479
3480 if (!is_macsec_master(real_dev))
3481 return NOTIFY_DONE;
3482
3483 switch (event) {
3484 case NETDEV_DOWN:
3485 case NETDEV_UP:
3486 case NETDEV_CHANGE: {
3487 struct macsec_dev *m, *n;
3488 struct macsec_rxh_data *rxd;
3489
3490 rxd = macsec_data_rtnl(real_dev);
3491 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3492 struct net_device *dev = m->secy.netdev;
3493
3494 netif_stacked_transfer_operstate(real_dev, dev);
3495 }
3496 break;
3497 }
3498 case NETDEV_UNREGISTER: {
3499 struct macsec_dev *m, *n;
3500 struct macsec_rxh_data *rxd;
3501
3502 rxd = macsec_data_rtnl(real_dev);
3503 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3504 macsec_common_dellink(m->secy.netdev, &head);
3505 }
3506
3507 netdev_rx_handler_unregister(real_dev);
3508 kfree(rxd);
3509
3510 unregister_netdevice_many(&head);
3511 break;
3512 }
3513 case NETDEV_CHANGEMTU: {
3514 struct macsec_dev *m;
3515 struct macsec_rxh_data *rxd;
3516
3517 rxd = macsec_data_rtnl(real_dev);
3518 list_for_each_entry(m, &rxd->secys, secys) {
3519 struct net_device *dev = m->secy.netdev;
3520 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3521 macsec_extra_len(true));
3522
3523 if (dev->mtu > mtu)
3524 dev_set_mtu(dev, mtu);
3525 }
3526 }
3527 }
3528
3529 return NOTIFY_OK;
3530 }
3531
3532 static struct notifier_block macsec_notifier = {
3533 .notifier_call = macsec_notify,
3534 };
3535
3536 static int __init macsec_init(void)
3537 {
3538 int err;
3539
3540 pr_info("MACsec IEEE 802.1AE\n");
3541 err = register_netdevice_notifier(&macsec_notifier);
3542 if (err)
3543 return err;
3544
3545 err = rtnl_link_register(&macsec_link_ops);
3546 if (err)
3547 goto notifier;
3548
3549 err = genl_register_family(&macsec_fam);
3550 if (err)
3551 goto rtnl;
3552
3553 return 0;
3554
3555 rtnl:
3556 rtnl_link_unregister(&macsec_link_ops);
3557 notifier:
3558 unregister_netdevice_notifier(&macsec_notifier);
3559 return err;
3560 }
3561
3562 static void __exit macsec_exit(void)
3563 {
3564 genl_unregister_family(&macsec_fam);
3565 rtnl_link_unregister(&macsec_link_ops);
3566 unregister_netdevice_notifier(&macsec_notifier);
3567 rcu_barrier();
3568 }
3569
3570 module_init(macsec_init);
3571 module_exit(macsec_exit);
3572
3573 MODULE_ALIAS_RTNL_LINK("macsec");
3574 MODULE_ALIAS_GENL_FAMILY("macsec");
3575
3576 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3577 MODULE_LICENSE("GPL v2");