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