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