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