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