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