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