blob: da8bf327a3e98230777d9bab725d23414e650a6c [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
Sabrina Dubroca6052f7f2016-06-14 15:25:16 +02001318 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001319
1320 if (IS_ERR(tfm))
1321 return tfm;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001322
1323 ret = crypto_aead_setkey(tfm, key, key_len);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001324 if (ret < 0)
1325 goto fail;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001326
1327 ret = crypto_aead_setauthsize(tfm, icv_len);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001328 if (ret < 0)
1329 goto fail;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001330
1331 return tfm;
Davide Caratti34aedfe2016-07-22 15:07:57 +02001332fail:
1333 crypto_free_aead(tfm);
1334 return ERR_PTR(ret);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001335}
1336
1337static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1338 int icv_len)
1339{
1340 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1341 if (!rx_sa->stats)
Davide Caratti34aedfe2016-07-22 15:07:57 +02001342 return -ENOMEM;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001343
1344 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001345 if (IS_ERR(rx_sa->key.tfm)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001346 free_percpu(rx_sa->stats);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001347 return PTR_ERR(rx_sa->key.tfm);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001348 }
1349
1350 rx_sa->active = false;
1351 rx_sa->next_pn = 1;
1352 atomic_set(&rx_sa->refcnt, 1);
1353 spin_lock_init(&rx_sa->lock);
1354
1355 return 0;
1356}
1357
1358static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1359{
1360 rx_sa->active = false;
1361
1362 macsec_rxsa_put(rx_sa);
1363}
1364
1365static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1366{
1367 int i;
1368
1369 for (i = 0; i < MACSEC_NUM_AN; i++) {
1370 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1371
1372 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1373 if (sa)
1374 clear_rx_sa(sa);
1375 }
1376
1377 macsec_rxsc_put(rx_sc);
1378}
1379
1380static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1381{
1382 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1383
1384 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1385 rx_sc;
1386 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1387 if (rx_sc->sci == sci) {
1388 if (rx_sc->active)
1389 secy->n_rx_sc--;
1390 rcu_assign_pointer(*rx_scp, rx_sc->next);
1391 return rx_sc;
1392 }
1393 }
1394
1395 return NULL;
1396}
1397
1398static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1399{
1400 struct macsec_rx_sc *rx_sc;
1401 struct macsec_dev *macsec;
1402 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1403 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1404 struct macsec_secy *secy;
1405
1406 list_for_each_entry(macsec, &rxd->secys, secys) {
1407 if (find_rx_sc_rtnl(&macsec->secy, sci))
1408 return ERR_PTR(-EEXIST);
1409 }
1410
1411 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1412 if (!rx_sc)
1413 return ERR_PTR(-ENOMEM);
1414
1415 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1416 if (!rx_sc->stats) {
1417 kfree(rx_sc);
1418 return ERR_PTR(-ENOMEM);
1419 }
1420
1421 rx_sc->sci = sci;
1422 rx_sc->active = true;
1423 atomic_set(&rx_sc->refcnt, 1);
1424
1425 secy = &macsec_priv(dev)->secy;
1426 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1427 rcu_assign_pointer(secy->rx_sc, rx_sc);
1428
1429 if (rx_sc->active)
1430 secy->n_rx_sc++;
1431
1432 return rx_sc;
1433}
1434
1435static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1436 int icv_len)
1437{
1438 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1439 if (!tx_sa->stats)
Davide Caratti34aedfe2016-07-22 15:07:57 +02001440 return -ENOMEM;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001441
1442 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001443 if (IS_ERR(tx_sa->key.tfm)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001444 free_percpu(tx_sa->stats);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001445 return PTR_ERR(tx_sa->key.tfm);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001446 }
1447
1448 tx_sa->active = false;
1449 atomic_set(&tx_sa->refcnt, 1);
1450 spin_lock_init(&tx_sa->lock);
1451
1452 return 0;
1453}
1454
1455static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1456{
1457 tx_sa->active = false;
1458
1459 macsec_txsa_put(tx_sa);
1460}
1461
1462static struct genl_family macsec_fam = {
1463 .id = GENL_ID_GENERATE,
1464 .name = MACSEC_GENL_NAME,
1465 .hdrsize = 0,
1466 .version = MACSEC_GENL_VERSION,
1467 .maxattr = MACSEC_ATTR_MAX,
1468 .netnsok = true,
1469};
1470
1471static struct net_device *get_dev_from_nl(struct net *net,
1472 struct nlattr **attrs)
1473{
1474 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1475 struct net_device *dev;
1476
1477 dev = __dev_get_by_index(net, ifindex);
1478 if (!dev)
1479 return ERR_PTR(-ENODEV);
1480
1481 if (!netif_is_macsec(dev))
1482 return ERR_PTR(-ENODEV);
1483
1484 return dev;
1485}
1486
1487static sci_t nla_get_sci(const struct nlattr *nla)
1488{
1489 return (__force sci_t)nla_get_u64(nla);
1490}
1491
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02001492static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1493 int padattr)
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001494{
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02001495 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001496}
1497
1498static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1499 struct nlattr **attrs,
1500 struct nlattr **tb_sa,
1501 struct net_device **devp,
1502 struct macsec_secy **secyp,
1503 struct macsec_tx_sc **scp,
1504 u8 *assoc_num)
1505{
1506 struct net_device *dev;
1507 struct macsec_secy *secy;
1508 struct macsec_tx_sc *tx_sc;
1509 struct macsec_tx_sa *tx_sa;
1510
1511 if (!tb_sa[MACSEC_SA_ATTR_AN])
1512 return ERR_PTR(-EINVAL);
1513
1514 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1515
1516 dev = get_dev_from_nl(net, attrs);
1517 if (IS_ERR(dev))
1518 return ERR_CAST(dev);
1519
1520 if (*assoc_num >= MACSEC_NUM_AN)
1521 return ERR_PTR(-EINVAL);
1522
1523 secy = &macsec_priv(dev)->secy;
1524 tx_sc = &secy->tx_sc;
1525
1526 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1527 if (!tx_sa)
1528 return ERR_PTR(-ENODEV);
1529
1530 *devp = dev;
1531 *scp = tx_sc;
1532 *secyp = secy;
1533 return tx_sa;
1534}
1535
1536static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1537 struct nlattr **attrs,
1538 struct nlattr **tb_rxsc,
1539 struct net_device **devp,
1540 struct macsec_secy **secyp)
1541{
1542 struct net_device *dev;
1543 struct macsec_secy *secy;
1544 struct macsec_rx_sc *rx_sc;
1545 sci_t sci;
1546
1547 dev = get_dev_from_nl(net, attrs);
1548 if (IS_ERR(dev))
1549 return ERR_CAST(dev);
1550
1551 secy = &macsec_priv(dev)->secy;
1552
1553 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1554 return ERR_PTR(-EINVAL);
1555
1556 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1557 rx_sc = find_rx_sc_rtnl(secy, sci);
1558 if (!rx_sc)
1559 return ERR_PTR(-ENODEV);
1560
1561 *secyp = secy;
1562 *devp = dev;
1563
1564 return rx_sc;
1565}
1566
1567static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1568 struct nlattr **attrs,
1569 struct nlattr **tb_rxsc,
1570 struct nlattr **tb_sa,
1571 struct net_device **devp,
1572 struct macsec_secy **secyp,
1573 struct macsec_rx_sc **scp,
1574 u8 *assoc_num)
1575{
1576 struct macsec_rx_sc *rx_sc;
1577 struct macsec_rx_sa *rx_sa;
1578
1579 if (!tb_sa[MACSEC_SA_ATTR_AN])
1580 return ERR_PTR(-EINVAL);
1581
1582 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1583 if (*assoc_num >= MACSEC_NUM_AN)
1584 return ERR_PTR(-EINVAL);
1585
1586 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1587 if (IS_ERR(rx_sc))
1588 return ERR_CAST(rx_sc);
1589
1590 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1591 if (!rx_sa)
1592 return ERR_PTR(-ENODEV);
1593
1594 *scp = rx_sc;
1595 return rx_sa;
1596}
1597
1598
1599static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1600 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1601 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1602 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1603};
1604
1605static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1606 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1607 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1608};
1609
1610static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1611 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1612 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1613 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02001614 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1615 .len = MACSEC_KEYID_LEN, },
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001616 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1617 .len = MACSEC_MAX_KEY_LEN, },
1618};
1619
1620static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1621{
1622 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1623 return -EINVAL;
1624
1625 if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG],
1626 macsec_genl_sa_policy))
1627 return -EINVAL;
1628
1629 return 0;
1630}
1631
1632static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1633{
1634 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1635 return -EINVAL;
1636
1637 if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG],
1638 macsec_genl_rxsc_policy))
1639 return -EINVAL;
1640
1641 return 0;
1642}
1643
1644static bool validate_add_rxsa(struct nlattr **attrs)
1645{
1646 if (!attrs[MACSEC_SA_ATTR_AN] ||
1647 !attrs[MACSEC_SA_ATTR_KEY] ||
1648 !attrs[MACSEC_SA_ATTR_KEYID])
1649 return false;
1650
1651 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1652 return false;
1653
1654 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1655 return false;
1656
1657 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1658 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1659 return false;
1660 }
1661
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02001662 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1663 return false;
1664
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001665 return true;
1666}
1667
1668static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1669{
1670 struct net_device *dev;
1671 struct nlattr **attrs = info->attrs;
1672 struct macsec_secy *secy;
1673 struct macsec_rx_sc *rx_sc;
1674 struct macsec_rx_sa *rx_sa;
1675 unsigned char assoc_num;
1676 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1677 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
Davide Caratti34aedfe2016-07-22 15:07:57 +02001678 int err;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001679
1680 if (!attrs[MACSEC_ATTR_IFINDEX])
1681 return -EINVAL;
1682
1683 if (parse_sa_config(attrs, tb_sa))
1684 return -EINVAL;
1685
1686 if (parse_rxsc_config(attrs, tb_rxsc))
1687 return -EINVAL;
1688
1689 if (!validate_add_rxsa(tb_sa))
1690 return -EINVAL;
1691
1692 rtnl_lock();
1693 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
Sabrina Dubroca36b232c2016-07-29 15:37:54 +02001694 if (IS_ERR(rx_sc)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001695 rtnl_unlock();
1696 return PTR_ERR(rx_sc);
1697 }
1698
1699 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1700
1701 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1702 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1703 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1704 rtnl_unlock();
1705 return -EINVAL;
1706 }
1707
1708 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1709 if (rx_sa) {
1710 rtnl_unlock();
1711 return -EBUSY;
1712 }
1713
1714 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001715 if (!rx_sa) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001716 rtnl_unlock();
1717 return -ENOMEM;
1718 }
1719
Davide Caratti34aedfe2016-07-22 15:07:57 +02001720 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1721 secy->key_len, secy->icv_len);
1722 if (err < 0) {
1723 kfree(rx_sa);
1724 rtnl_unlock();
1725 return err;
1726 }
1727
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001728 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1729 spin_lock_bh(&rx_sa->lock);
1730 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1731 spin_unlock_bh(&rx_sa->lock);
1732 }
1733
1734 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1735 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1736
Sabrina Dubroca1968a0b2016-05-18 13:34:40 +02001737 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001738 rx_sa->sc = rx_sc;
1739 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1740
1741 rtnl_unlock();
1742
1743 return 0;
1744}
1745
1746static bool validate_add_rxsc(struct nlattr **attrs)
1747{
1748 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1749 return false;
1750
1751 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1752 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1753 return false;
1754 }
1755
1756 return true;
1757}
1758
1759static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1760{
1761 struct net_device *dev;
1762 sci_t sci = MACSEC_UNDEF_SCI;
1763 struct nlattr **attrs = info->attrs;
1764 struct macsec_rx_sc *rx_sc;
1765 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1766
1767 if (!attrs[MACSEC_ATTR_IFINDEX])
1768 return -EINVAL;
1769
1770 if (parse_rxsc_config(attrs, tb_rxsc))
1771 return -EINVAL;
1772
1773 if (!validate_add_rxsc(tb_rxsc))
1774 return -EINVAL;
1775
1776 rtnl_lock();
1777 dev = get_dev_from_nl(genl_info_net(info), attrs);
1778 if (IS_ERR(dev)) {
1779 rtnl_unlock();
1780 return PTR_ERR(dev);
1781 }
1782
1783 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1784
1785 rx_sc = create_rx_sc(dev, sci);
1786 if (IS_ERR(rx_sc)) {
1787 rtnl_unlock();
1788 return PTR_ERR(rx_sc);
1789 }
1790
1791 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1792 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1793
1794 rtnl_unlock();
1795
1796 return 0;
1797}
1798
1799static bool validate_add_txsa(struct nlattr **attrs)
1800{
1801 if (!attrs[MACSEC_SA_ATTR_AN] ||
1802 !attrs[MACSEC_SA_ATTR_PN] ||
1803 !attrs[MACSEC_SA_ATTR_KEY] ||
1804 !attrs[MACSEC_SA_ATTR_KEYID])
1805 return false;
1806
1807 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1808 return false;
1809
1810 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1811 return false;
1812
1813 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1814 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1815 return false;
1816 }
1817
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02001818 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1819 return false;
1820
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001821 return true;
1822}
1823
1824static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1825{
1826 struct net_device *dev;
1827 struct nlattr **attrs = info->attrs;
1828 struct macsec_secy *secy;
1829 struct macsec_tx_sc *tx_sc;
1830 struct macsec_tx_sa *tx_sa;
1831 unsigned char assoc_num;
1832 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
Davide Caratti34aedfe2016-07-22 15:07:57 +02001833 int err;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001834
1835 if (!attrs[MACSEC_ATTR_IFINDEX])
1836 return -EINVAL;
1837
1838 if (parse_sa_config(attrs, tb_sa))
1839 return -EINVAL;
1840
1841 if (!validate_add_txsa(tb_sa))
1842 return -EINVAL;
1843
1844 rtnl_lock();
1845 dev = get_dev_from_nl(genl_info_net(info), attrs);
1846 if (IS_ERR(dev)) {
1847 rtnl_unlock();
1848 return PTR_ERR(dev);
1849 }
1850
1851 secy = &macsec_priv(dev)->secy;
1852 tx_sc = &secy->tx_sc;
1853
1854 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1855
1856 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1857 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1858 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1859 rtnl_unlock();
1860 return -EINVAL;
1861 }
1862
1863 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1864 if (tx_sa) {
1865 rtnl_unlock();
1866 return -EBUSY;
1867 }
1868
1869 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001870 if (!tx_sa) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001871 rtnl_unlock();
1872 return -ENOMEM;
1873 }
1874
Davide Caratti34aedfe2016-07-22 15:07:57 +02001875 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1876 secy->key_len, secy->icv_len);
1877 if (err < 0) {
1878 kfree(tx_sa);
1879 rtnl_unlock();
1880 return err;
1881 }
1882
Sabrina Dubroca1968a0b2016-05-18 13:34:40 +02001883 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001884
1885 spin_lock_bh(&tx_sa->lock);
1886 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1887 spin_unlock_bh(&tx_sa->lock);
1888
1889 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1890 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1891
1892 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1893 secy->operational = true;
1894
1895 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1896
1897 rtnl_unlock();
1898
1899 return 0;
1900}
1901
1902static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1903{
1904 struct nlattr **attrs = info->attrs;
1905 struct net_device *dev;
1906 struct macsec_secy *secy;
1907 struct macsec_rx_sc *rx_sc;
1908 struct macsec_rx_sa *rx_sa;
1909 u8 assoc_num;
1910 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1911 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1912
1913 if (!attrs[MACSEC_ATTR_IFINDEX])
1914 return -EINVAL;
1915
1916 if (parse_sa_config(attrs, tb_sa))
1917 return -EINVAL;
1918
1919 if (parse_rxsc_config(attrs, tb_rxsc))
1920 return -EINVAL;
1921
1922 rtnl_lock();
1923 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1924 &dev, &secy, &rx_sc, &assoc_num);
1925 if (IS_ERR(rx_sa)) {
1926 rtnl_unlock();
1927 return PTR_ERR(rx_sa);
1928 }
1929
1930 if (rx_sa->active) {
1931 rtnl_unlock();
1932 return -EBUSY;
1933 }
1934
1935 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1936 clear_rx_sa(rx_sa);
1937
1938 rtnl_unlock();
1939
1940 return 0;
1941}
1942
1943static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1944{
1945 struct nlattr **attrs = info->attrs;
1946 struct net_device *dev;
1947 struct macsec_secy *secy;
1948 struct macsec_rx_sc *rx_sc;
1949 sci_t sci;
1950 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1951
1952 if (!attrs[MACSEC_ATTR_IFINDEX])
1953 return -EINVAL;
1954
1955 if (parse_rxsc_config(attrs, tb_rxsc))
1956 return -EINVAL;
1957
1958 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1959 return -EINVAL;
1960
1961 rtnl_lock();
1962 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1963 if (IS_ERR(dev)) {
1964 rtnl_unlock();
1965 return PTR_ERR(dev);
1966 }
1967
1968 secy = &macsec_priv(dev)->secy;
1969 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1970
1971 rx_sc = del_rx_sc(secy, sci);
1972 if (!rx_sc) {
1973 rtnl_unlock();
1974 return -ENODEV;
1975 }
1976
1977 free_rx_sc(rx_sc);
1978 rtnl_unlock();
1979
1980 return 0;
1981}
1982
1983static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1984{
1985 struct nlattr **attrs = info->attrs;
1986 struct net_device *dev;
1987 struct macsec_secy *secy;
1988 struct macsec_tx_sc *tx_sc;
1989 struct macsec_tx_sa *tx_sa;
1990 u8 assoc_num;
1991 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1992
1993 if (!attrs[MACSEC_ATTR_IFINDEX])
1994 return -EINVAL;
1995
1996 if (parse_sa_config(attrs, tb_sa))
1997 return -EINVAL;
1998
1999 rtnl_lock();
2000 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2001 &dev, &secy, &tx_sc, &assoc_num);
2002 if (IS_ERR(tx_sa)) {
2003 rtnl_unlock();
2004 return PTR_ERR(tx_sa);
2005 }
2006
2007 if (tx_sa->active) {
2008 rtnl_unlock();
2009 return -EBUSY;
2010 }
2011
2012 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2013 clear_tx_sa(tx_sa);
2014
2015 rtnl_unlock();
2016
2017 return 0;
2018}
2019
2020static bool validate_upd_sa(struct nlattr **attrs)
2021{
2022 if (!attrs[MACSEC_SA_ATTR_AN] ||
2023 attrs[MACSEC_SA_ATTR_KEY] ||
2024 attrs[MACSEC_SA_ATTR_KEYID])
2025 return false;
2026
2027 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2028 return false;
2029
2030 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2031 return false;
2032
2033 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2034 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2035 return false;
2036 }
2037
2038 return true;
2039}
2040
2041static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2042{
2043 struct nlattr **attrs = info->attrs;
2044 struct net_device *dev;
2045 struct macsec_secy *secy;
2046 struct macsec_tx_sc *tx_sc;
2047 struct macsec_tx_sa *tx_sa;
2048 u8 assoc_num;
2049 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2050
2051 if (!attrs[MACSEC_ATTR_IFINDEX])
2052 return -EINVAL;
2053
2054 if (parse_sa_config(attrs, tb_sa))
2055 return -EINVAL;
2056
2057 if (!validate_upd_sa(tb_sa))
2058 return -EINVAL;
2059
2060 rtnl_lock();
2061 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2062 &dev, &secy, &tx_sc, &assoc_num);
2063 if (IS_ERR(tx_sa)) {
2064 rtnl_unlock();
2065 return PTR_ERR(tx_sa);
2066 }
2067
2068 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2069 spin_lock_bh(&tx_sa->lock);
2070 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2071 spin_unlock_bh(&tx_sa->lock);
2072 }
2073
2074 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2075 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2076
2077 if (assoc_num == tx_sc->encoding_sa)
2078 secy->operational = tx_sa->active;
2079
2080 rtnl_unlock();
2081
2082 return 0;
2083}
2084
2085static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2086{
2087 struct nlattr **attrs = info->attrs;
2088 struct net_device *dev;
2089 struct macsec_secy *secy;
2090 struct macsec_rx_sc *rx_sc;
2091 struct macsec_rx_sa *rx_sa;
2092 u8 assoc_num;
2093 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2094 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2095
2096 if (!attrs[MACSEC_ATTR_IFINDEX])
2097 return -EINVAL;
2098
2099 if (parse_rxsc_config(attrs, tb_rxsc))
2100 return -EINVAL;
2101
2102 if (parse_sa_config(attrs, tb_sa))
2103 return -EINVAL;
2104
2105 if (!validate_upd_sa(tb_sa))
2106 return -EINVAL;
2107
2108 rtnl_lock();
2109 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2110 &dev, &secy, &rx_sc, &assoc_num);
2111 if (IS_ERR(rx_sa)) {
2112 rtnl_unlock();
2113 return PTR_ERR(rx_sa);
2114 }
2115
2116 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2117 spin_lock_bh(&rx_sa->lock);
2118 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2119 spin_unlock_bh(&rx_sa->lock);
2120 }
2121
2122 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2123 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2124
2125 rtnl_unlock();
2126 return 0;
2127}
2128
2129static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2130{
2131 struct nlattr **attrs = info->attrs;
2132 struct net_device *dev;
2133 struct macsec_secy *secy;
2134 struct macsec_rx_sc *rx_sc;
2135 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2136
2137 if (!attrs[MACSEC_ATTR_IFINDEX])
2138 return -EINVAL;
2139
2140 if (parse_rxsc_config(attrs, tb_rxsc))
2141 return -EINVAL;
2142
2143 if (!validate_add_rxsc(tb_rxsc))
2144 return -EINVAL;
2145
2146 rtnl_lock();
2147 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2148 if (IS_ERR(rx_sc)) {
2149 rtnl_unlock();
2150 return PTR_ERR(rx_sc);
2151 }
2152
2153 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2154 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2155
2156 if (rx_sc->active != new)
2157 secy->n_rx_sc += new ? 1 : -1;
2158
2159 rx_sc->active = new;
2160 }
2161
2162 rtnl_unlock();
2163
2164 return 0;
2165}
2166
2167static int copy_tx_sa_stats(struct sk_buff *skb,
2168 struct macsec_tx_sa_stats __percpu *pstats)
2169{
2170 struct macsec_tx_sa_stats sum = {0, };
2171 int cpu;
2172
2173 for_each_possible_cpu(cpu) {
2174 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2175
2176 sum.OutPktsProtected += stats->OutPktsProtected;
2177 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2178 }
2179
2180 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2181 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2182 return -EMSGSIZE;
2183
2184 return 0;
2185}
2186
2187static int copy_rx_sa_stats(struct sk_buff *skb,
2188 struct macsec_rx_sa_stats __percpu *pstats)
2189{
2190 struct macsec_rx_sa_stats sum = {0, };
2191 int cpu;
2192
2193 for_each_possible_cpu(cpu) {
2194 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2195
2196 sum.InPktsOK += stats->InPktsOK;
2197 sum.InPktsInvalid += stats->InPktsInvalid;
2198 sum.InPktsNotValid += stats->InPktsNotValid;
2199 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2200 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2201 }
2202
2203 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2204 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2205 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2206 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2207 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2208 return -EMSGSIZE;
2209
2210 return 0;
2211}
2212
2213static int copy_rx_sc_stats(struct sk_buff *skb,
2214 struct pcpu_rx_sc_stats __percpu *pstats)
2215{
2216 struct macsec_rx_sc_stats sum = {0, };
2217 int cpu;
2218
2219 for_each_possible_cpu(cpu) {
2220 const struct pcpu_rx_sc_stats *stats;
2221 struct macsec_rx_sc_stats tmp;
2222 unsigned int start;
2223
2224 stats = per_cpu_ptr(pstats, cpu);
2225 do {
2226 start = u64_stats_fetch_begin_irq(&stats->syncp);
2227 memcpy(&tmp, &stats->stats, sizeof(tmp));
2228 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2229
2230 sum.InOctetsValidated += tmp.InOctetsValidated;
2231 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2232 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2233 sum.InPktsDelayed += tmp.InPktsDelayed;
2234 sum.InPktsOK += tmp.InPktsOK;
2235 sum.InPktsInvalid += tmp.InPktsInvalid;
2236 sum.InPktsLate += tmp.InPktsLate;
2237 sum.InPktsNotValid += tmp.InPktsNotValid;
2238 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2239 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2240 }
2241
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002242 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2243 sum.InOctetsValidated,
2244 MACSEC_RXSC_STATS_ATTR_PAD) ||
2245 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2246 sum.InOctetsDecrypted,
2247 MACSEC_RXSC_STATS_ATTR_PAD) ||
2248 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2249 sum.InPktsUnchecked,
2250 MACSEC_RXSC_STATS_ATTR_PAD) ||
2251 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2252 sum.InPktsDelayed,
2253 MACSEC_RXSC_STATS_ATTR_PAD) ||
2254 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2255 sum.InPktsOK,
2256 MACSEC_RXSC_STATS_ATTR_PAD) ||
2257 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2258 sum.InPktsInvalid,
2259 MACSEC_RXSC_STATS_ATTR_PAD) ||
2260 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2261 sum.InPktsLate,
2262 MACSEC_RXSC_STATS_ATTR_PAD) ||
2263 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2264 sum.InPktsNotValid,
2265 MACSEC_RXSC_STATS_ATTR_PAD) ||
2266 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2267 sum.InPktsNotUsingSA,
2268 MACSEC_RXSC_STATS_ATTR_PAD) ||
2269 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2270 sum.InPktsUnusedSA,
2271 MACSEC_RXSC_STATS_ATTR_PAD))
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002272 return -EMSGSIZE;
2273
2274 return 0;
2275}
2276
2277static int copy_tx_sc_stats(struct sk_buff *skb,
2278 struct pcpu_tx_sc_stats __percpu *pstats)
2279{
2280 struct macsec_tx_sc_stats sum = {0, };
2281 int cpu;
2282
2283 for_each_possible_cpu(cpu) {
2284 const struct pcpu_tx_sc_stats *stats;
2285 struct macsec_tx_sc_stats tmp;
2286 unsigned int start;
2287
2288 stats = per_cpu_ptr(pstats, cpu);
2289 do {
2290 start = u64_stats_fetch_begin_irq(&stats->syncp);
2291 memcpy(&tmp, &stats->stats, sizeof(tmp));
2292 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2293
2294 sum.OutPktsProtected += tmp.OutPktsProtected;
2295 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2296 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2297 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2298 }
2299
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002300 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2301 sum.OutPktsProtected,
2302 MACSEC_TXSC_STATS_ATTR_PAD) ||
2303 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2304 sum.OutPktsEncrypted,
2305 MACSEC_TXSC_STATS_ATTR_PAD) ||
2306 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2307 sum.OutOctetsProtected,
2308 MACSEC_TXSC_STATS_ATTR_PAD) ||
2309 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2310 sum.OutOctetsEncrypted,
2311 MACSEC_TXSC_STATS_ATTR_PAD))
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002312 return -EMSGSIZE;
2313
2314 return 0;
2315}
2316
2317static int copy_secy_stats(struct sk_buff *skb,
2318 struct pcpu_secy_stats __percpu *pstats)
2319{
2320 struct macsec_dev_stats sum = {0, };
2321 int cpu;
2322
2323 for_each_possible_cpu(cpu) {
2324 const struct pcpu_secy_stats *stats;
2325 struct macsec_dev_stats tmp;
2326 unsigned int start;
2327
2328 stats = per_cpu_ptr(pstats, cpu);
2329 do {
2330 start = u64_stats_fetch_begin_irq(&stats->syncp);
2331 memcpy(&tmp, &stats->stats, sizeof(tmp));
2332 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2333
2334 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2335 sum.InPktsUntagged += tmp.InPktsUntagged;
2336 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2337 sum.InPktsNoTag += tmp.InPktsNoTag;
2338 sum.InPktsBadTag += tmp.InPktsBadTag;
2339 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2340 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2341 sum.InPktsOverrun += tmp.InPktsOverrun;
2342 }
2343
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002344 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2345 sum.OutPktsUntagged,
2346 MACSEC_SECY_STATS_ATTR_PAD) ||
2347 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2348 sum.InPktsUntagged,
2349 MACSEC_SECY_STATS_ATTR_PAD) ||
2350 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2351 sum.OutPktsTooLong,
2352 MACSEC_SECY_STATS_ATTR_PAD) ||
2353 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2354 sum.InPktsNoTag,
2355 MACSEC_SECY_STATS_ATTR_PAD) ||
2356 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2357 sum.InPktsBadTag,
2358 MACSEC_SECY_STATS_ATTR_PAD) ||
2359 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2360 sum.InPktsUnknownSCI,
2361 MACSEC_SECY_STATS_ATTR_PAD) ||
2362 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2363 sum.InPktsNoSCI,
2364 MACSEC_SECY_STATS_ATTR_PAD) ||
2365 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2366 sum.InPktsOverrun,
2367 MACSEC_SECY_STATS_ATTR_PAD))
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002368 return -EMSGSIZE;
2369
2370 return 0;
2371}
2372
2373static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2374{
2375 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2376 struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
2377
2378 if (!secy_nest)
2379 return 1;
2380
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002381 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2382 MACSEC_SECY_ATTR_PAD) ||
2383 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
David S. Millerc0cc5312016-04-27 15:43:10 -04002384 MACSEC_DEFAULT_CIPHER_ID,
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002385 MACSEC_SECY_ATTR_PAD) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002386 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2387 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2388 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2389 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2390 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2391 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2392 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2393 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2394 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2395 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2396 goto cancel;
2397
2398 if (secy->replay_protect) {
2399 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2400 goto cancel;
2401 }
2402
2403 nla_nest_end(skb, secy_nest);
2404 return 0;
2405
2406cancel:
2407 nla_nest_cancel(skb, secy_nest);
2408 return 1;
2409}
2410
2411static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
2412 struct sk_buff *skb, struct netlink_callback *cb)
2413{
2414 struct macsec_rx_sc *rx_sc;
2415 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2416 struct nlattr *txsa_list, *rxsc_list;
2417 int i, j;
2418 void *hdr;
2419 struct nlattr *attr;
2420
2421 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2422 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2423 if (!hdr)
2424 return -EMSGSIZE;
2425
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02002426 genl_dump_check_consistent(cb, hdr, &macsec_fam);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002427
2428 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2429 goto nla_put_failure;
2430
2431 if (nla_put_secy(secy, skb))
2432 goto nla_put_failure;
2433
2434 attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
2435 if (!attr)
2436 goto nla_put_failure;
2437 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2438 nla_nest_cancel(skb, attr);
2439 goto nla_put_failure;
2440 }
2441 nla_nest_end(skb, attr);
2442
2443 attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
2444 if (!attr)
2445 goto nla_put_failure;
2446 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2447 nla_nest_cancel(skb, attr);
2448 goto nla_put_failure;
2449 }
2450 nla_nest_end(skb, attr);
2451
2452 txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
2453 if (!txsa_list)
2454 goto nla_put_failure;
2455 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2456 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2457 struct nlattr *txsa_nest;
2458
2459 if (!tx_sa)
2460 continue;
2461
2462 txsa_nest = nla_nest_start(skb, j++);
2463 if (!txsa_nest) {
2464 nla_nest_cancel(skb, txsa_list);
2465 goto nla_put_failure;
2466 }
2467
2468 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2469 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02002470 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002471 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2472 nla_nest_cancel(skb, txsa_nest);
2473 nla_nest_cancel(skb, txsa_list);
2474 goto nla_put_failure;
2475 }
2476
2477 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2478 if (!attr) {
2479 nla_nest_cancel(skb, txsa_nest);
2480 nla_nest_cancel(skb, txsa_list);
2481 goto nla_put_failure;
2482 }
2483 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2484 nla_nest_cancel(skb, attr);
2485 nla_nest_cancel(skb, txsa_nest);
2486 nla_nest_cancel(skb, txsa_list);
2487 goto nla_put_failure;
2488 }
2489 nla_nest_end(skb, attr);
2490
2491 nla_nest_end(skb, txsa_nest);
2492 }
2493 nla_nest_end(skb, txsa_list);
2494
2495 rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
2496 if (!rxsc_list)
2497 goto nla_put_failure;
2498
2499 j = 1;
2500 for_each_rxsc_rtnl(secy, rx_sc) {
2501 int k;
2502 struct nlattr *rxsa_list;
2503 struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
2504
2505 if (!rxsc_nest) {
2506 nla_nest_cancel(skb, rxsc_list);
2507 goto nla_put_failure;
2508 }
2509
2510 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002511 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2512 MACSEC_RXSC_ATTR_PAD)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002513 nla_nest_cancel(skb, rxsc_nest);
2514 nla_nest_cancel(skb, rxsc_list);
2515 goto nla_put_failure;
2516 }
2517
2518 attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
2519 if (!attr) {
2520 nla_nest_cancel(skb, rxsc_nest);
2521 nla_nest_cancel(skb, rxsc_list);
2522 goto nla_put_failure;
2523 }
2524 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2525 nla_nest_cancel(skb, attr);
2526 nla_nest_cancel(skb, rxsc_nest);
2527 nla_nest_cancel(skb, rxsc_list);
2528 goto nla_put_failure;
2529 }
2530 nla_nest_end(skb, attr);
2531
2532 rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
2533 if (!rxsa_list) {
2534 nla_nest_cancel(skb, rxsc_nest);
2535 nla_nest_cancel(skb, rxsc_list);
2536 goto nla_put_failure;
2537 }
2538
2539 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2540 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2541 struct nlattr *rxsa_nest;
2542
2543 if (!rx_sa)
2544 continue;
2545
2546 rxsa_nest = nla_nest_start(skb, k++);
2547 if (!rxsa_nest) {
2548 nla_nest_cancel(skb, rxsa_list);
2549 nla_nest_cancel(skb, rxsc_nest);
2550 nla_nest_cancel(skb, rxsc_list);
2551 goto nla_put_failure;
2552 }
2553
2554 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2555 if (!attr) {
2556 nla_nest_cancel(skb, rxsa_list);
2557 nla_nest_cancel(skb, rxsc_nest);
2558 nla_nest_cancel(skb, rxsc_list);
2559 goto nla_put_failure;
2560 }
2561 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2562 nla_nest_cancel(skb, attr);
2563 nla_nest_cancel(skb, rxsa_list);
2564 nla_nest_cancel(skb, rxsc_nest);
2565 nla_nest_cancel(skb, rxsc_list);
2566 goto nla_put_failure;
2567 }
2568 nla_nest_end(skb, attr);
2569
2570 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2571 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02002572 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002573 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2574 nla_nest_cancel(skb, rxsa_nest);
2575 nla_nest_cancel(skb, rxsc_nest);
2576 nla_nest_cancel(skb, rxsc_list);
2577 goto nla_put_failure;
2578 }
2579 nla_nest_end(skb, rxsa_nest);
2580 }
2581
2582 nla_nest_end(skb, rxsa_list);
2583 nla_nest_end(skb, rxsc_nest);
2584 }
2585
2586 nla_nest_end(skb, rxsc_list);
2587
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002588 genlmsg_end(skb, hdr);
2589
2590 return 0;
2591
2592nla_put_failure:
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002593 genlmsg_cancel(skb, hdr);
2594 return -EMSGSIZE;
2595}
2596
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02002597static int macsec_generation = 1; /* protected by RTNL */
2598
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002599static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2600{
2601 struct net *net = sock_net(skb->sk);
2602 struct net_device *dev;
2603 int dev_idx, d;
2604
2605 dev_idx = cb->args[0];
2606
2607 d = 0;
Sabrina Dubrocac10c63e2016-04-22 11:28:02 +02002608 rtnl_lock();
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02002609
2610 cb->seq = macsec_generation;
2611
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002612 for_each_netdev(net, dev) {
2613 struct macsec_secy *secy;
2614
2615 if (d < dev_idx)
2616 goto next;
2617
2618 if (!netif_is_macsec(dev))
2619 goto next;
2620
2621 secy = &macsec_priv(dev)->secy;
2622 if (dump_secy(secy, dev, skb, cb) < 0)
2623 goto done;
2624next:
2625 d++;
2626 }
2627
2628done:
Sabrina Dubrocac10c63e2016-04-22 11:28:02 +02002629 rtnl_unlock();
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002630 cb->args[0] = d;
2631 return skb->len;
2632}
2633
2634static const struct genl_ops macsec_genl_ops[] = {
2635 {
2636 .cmd = MACSEC_CMD_GET_TXSC,
2637 .dumpit = macsec_dump_txsc,
2638 .policy = macsec_genl_policy,
2639 },
2640 {
2641 .cmd = MACSEC_CMD_ADD_RXSC,
2642 .doit = macsec_add_rxsc,
2643 .policy = macsec_genl_policy,
2644 .flags = GENL_ADMIN_PERM,
2645 },
2646 {
2647 .cmd = MACSEC_CMD_DEL_RXSC,
2648 .doit = macsec_del_rxsc,
2649 .policy = macsec_genl_policy,
2650 .flags = GENL_ADMIN_PERM,
2651 },
2652 {
2653 .cmd = MACSEC_CMD_UPD_RXSC,
2654 .doit = macsec_upd_rxsc,
2655 .policy = macsec_genl_policy,
2656 .flags = GENL_ADMIN_PERM,
2657 },
2658 {
2659 .cmd = MACSEC_CMD_ADD_TXSA,
2660 .doit = macsec_add_txsa,
2661 .policy = macsec_genl_policy,
2662 .flags = GENL_ADMIN_PERM,
2663 },
2664 {
2665 .cmd = MACSEC_CMD_DEL_TXSA,
2666 .doit = macsec_del_txsa,
2667 .policy = macsec_genl_policy,
2668 .flags = GENL_ADMIN_PERM,
2669 },
2670 {
2671 .cmd = MACSEC_CMD_UPD_TXSA,
2672 .doit = macsec_upd_txsa,
2673 .policy = macsec_genl_policy,
2674 .flags = GENL_ADMIN_PERM,
2675 },
2676 {
2677 .cmd = MACSEC_CMD_ADD_RXSA,
2678 .doit = macsec_add_rxsa,
2679 .policy = macsec_genl_policy,
2680 .flags = GENL_ADMIN_PERM,
2681 },
2682 {
2683 .cmd = MACSEC_CMD_DEL_RXSA,
2684 .doit = macsec_del_rxsa,
2685 .policy = macsec_genl_policy,
2686 .flags = GENL_ADMIN_PERM,
2687 },
2688 {
2689 .cmd = MACSEC_CMD_UPD_RXSA,
2690 .doit = macsec_upd_rxsa,
2691 .policy = macsec_genl_policy,
2692 .flags = GENL_ADMIN_PERM,
2693 },
2694};
2695
2696static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2697 struct net_device *dev)
2698{
2699 struct macsec_dev *macsec = netdev_priv(dev);
2700 struct macsec_secy *secy = &macsec->secy;
2701 struct pcpu_secy_stats *secy_stats;
2702 int ret, len;
2703
2704 /* 10.5 */
2705 if (!secy->protect_frames) {
2706 secy_stats = this_cpu_ptr(macsec->stats);
2707 u64_stats_update_begin(&secy_stats->syncp);
2708 secy_stats->stats.OutPktsUntagged++;
2709 u64_stats_update_end(&secy_stats->syncp);
Daniel Borkmann79c62222016-07-01 00:00:54 +02002710 skb->dev = macsec->real_dev;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002711 len = skb->len;
2712 ret = dev_queue_xmit(skb);
2713 count_tx(dev, ret, len);
2714 return ret;
2715 }
2716
2717 if (!secy->operational) {
2718 kfree_skb(skb);
2719 dev->stats.tx_dropped++;
2720 return NETDEV_TX_OK;
2721 }
2722
2723 skb = macsec_encrypt(skb, dev);
2724 if (IS_ERR(skb)) {
2725 if (PTR_ERR(skb) != -EINPROGRESS)
2726 dev->stats.tx_dropped++;
2727 return NETDEV_TX_OK;
2728 }
2729
2730 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2731
2732 macsec_encrypt_finish(skb, dev);
2733 len = skb->len;
2734 ret = dev_queue_xmit(skb);
2735 count_tx(dev, ret, len);
2736 return ret;
2737}
2738
2739#define MACSEC_FEATURES \
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +02002740 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
Sabrina Dubrocae2003872016-08-12 16:10:32 +02002741static struct lock_class_key macsec_netdev_addr_lock_key;
2742
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002743static int macsec_dev_init(struct net_device *dev)
2744{
2745 struct macsec_dev *macsec = macsec_priv(dev);
2746 struct net_device *real_dev = macsec->real_dev;
Paolo Abeni5491e7c2016-07-20 18:11:32 +02002747 int err;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002748
2749 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2750 if (!dev->tstats)
2751 return -ENOMEM;
2752
Paolo Abeni5491e7c2016-07-20 18:11:32 +02002753 err = gro_cells_init(&macsec->gro_cells, dev);
2754 if (err) {
2755 free_percpu(dev->tstats);
2756 return err;
2757 }
2758
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002759 dev->features = real_dev->features & MACSEC_FEATURES;
2760 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2761
2762 dev->needed_headroom = real_dev->needed_headroom +
2763 MACSEC_NEEDED_HEADROOM;
2764 dev->needed_tailroom = real_dev->needed_tailroom +
2765 MACSEC_NEEDED_TAILROOM;
2766
2767 if (is_zero_ether_addr(dev->dev_addr))
2768 eth_hw_addr_inherit(dev, real_dev);
2769 if (is_zero_ether_addr(dev->broadcast))
2770 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2771
2772 return 0;
2773}
2774
2775static void macsec_dev_uninit(struct net_device *dev)
2776{
Paolo Abeni5491e7c2016-07-20 18:11:32 +02002777 struct macsec_dev *macsec = macsec_priv(dev);
2778
2779 gro_cells_destroy(&macsec->gro_cells);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002780 free_percpu(dev->tstats);
2781}
2782
2783static netdev_features_t macsec_fix_features(struct net_device *dev,
2784 netdev_features_t features)
2785{
2786 struct macsec_dev *macsec = macsec_priv(dev);
2787 struct net_device *real_dev = macsec->real_dev;
2788
Paolo Abeni5491e7c2016-07-20 18:11:32 +02002789 features &= (real_dev->features & MACSEC_FEATURES) |
2790 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2791 features |= NETIF_F_LLTX;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002792
2793 return features;
2794}
2795
2796static int macsec_dev_open(struct net_device *dev)
2797{
2798 struct macsec_dev *macsec = macsec_priv(dev);
2799 struct net_device *real_dev = macsec->real_dev;
2800 int err;
2801
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002802 err = dev_uc_add(real_dev, dev->dev_addr);
2803 if (err < 0)
2804 return err;
2805
2806 if (dev->flags & IFF_ALLMULTI) {
2807 err = dev_set_allmulti(real_dev, 1);
2808 if (err < 0)
2809 goto del_unicast;
2810 }
2811
2812 if (dev->flags & IFF_PROMISC) {
2813 err = dev_set_promiscuity(real_dev, 1);
2814 if (err < 0)
2815 goto clear_allmulti;
2816 }
2817
2818 if (netif_carrier_ok(real_dev))
2819 netif_carrier_on(dev);
2820
2821 return 0;
2822clear_allmulti:
2823 if (dev->flags & IFF_ALLMULTI)
2824 dev_set_allmulti(real_dev, -1);
2825del_unicast:
2826 dev_uc_del(real_dev, dev->dev_addr);
2827 netif_carrier_off(dev);
2828 return err;
2829}
2830
2831static int macsec_dev_stop(struct net_device *dev)
2832{
2833 struct macsec_dev *macsec = macsec_priv(dev);
2834 struct net_device *real_dev = macsec->real_dev;
2835
2836 netif_carrier_off(dev);
2837
2838 dev_mc_unsync(real_dev, dev);
2839 dev_uc_unsync(real_dev, dev);
2840
2841 if (dev->flags & IFF_ALLMULTI)
2842 dev_set_allmulti(real_dev, -1);
2843
2844 if (dev->flags & IFF_PROMISC)
2845 dev_set_promiscuity(real_dev, -1);
2846
2847 dev_uc_del(real_dev, dev->dev_addr);
2848
2849 return 0;
2850}
2851
2852static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
2853{
2854 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2855
2856 if (!(dev->flags & IFF_UP))
2857 return;
2858
2859 if (change & IFF_ALLMULTI)
2860 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
2861
2862 if (change & IFF_PROMISC)
2863 dev_set_promiscuity(real_dev,
2864 dev->flags & IFF_PROMISC ? 1 : -1);
2865}
2866
2867static void macsec_dev_set_rx_mode(struct net_device *dev)
2868{
2869 struct net_device *real_dev = macsec_priv(dev)->real_dev;
2870
2871 dev_mc_sync(real_dev, dev);
2872 dev_uc_sync(real_dev, dev);
2873}
2874
Dmitry Bogdanovfc094da2020-03-10 18:22:24 +03002875static sci_t dev_to_sci(struct net_device *dev, __be16 port)
2876{
2877 return make_sci(dev->dev_addr, port);
2878}
2879
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002880static int macsec_set_mac_address(struct net_device *dev, void *p)
2881{
2882 struct macsec_dev *macsec = macsec_priv(dev);
2883 struct net_device *real_dev = macsec->real_dev;
2884 struct sockaddr *addr = p;
2885 int err;
2886
2887 if (!is_valid_ether_addr(addr->sa_data))
2888 return -EADDRNOTAVAIL;
2889
2890 if (!(dev->flags & IFF_UP))
2891 goto out;
2892
2893 err = dev_uc_add(real_dev, addr->sa_data);
2894 if (err < 0)
2895 return err;
2896
2897 dev_uc_del(real_dev, dev->dev_addr);
2898
2899out:
2900 ether_addr_copy(dev->dev_addr, addr->sa_data);
Dmitry Bogdanovfc094da2020-03-10 18:22:24 +03002901 macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002902 return 0;
2903}
2904
2905static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2906{
2907 struct macsec_dev *macsec = macsec_priv(dev);
2908 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2909
2910 if (macsec->real_dev->mtu - extra < new_mtu)
2911 return -ERANGE;
2912
2913 dev->mtu = new_mtu;
2914
2915 return 0;
2916}
2917
2918static struct rtnl_link_stats64 *macsec_get_stats64(struct net_device *dev,
2919 struct rtnl_link_stats64 *s)
2920{
2921 int cpu;
2922
2923 if (!dev->tstats)
2924 return s;
2925
2926 for_each_possible_cpu(cpu) {
2927 struct pcpu_sw_netstats *stats;
2928 struct pcpu_sw_netstats tmp;
2929 int start;
2930
2931 stats = per_cpu_ptr(dev->tstats, cpu);
2932 do {
2933 start = u64_stats_fetch_begin_irq(&stats->syncp);
2934 tmp.rx_packets = stats->rx_packets;
2935 tmp.rx_bytes = stats->rx_bytes;
2936 tmp.tx_packets = stats->tx_packets;
2937 tmp.tx_bytes = stats->tx_bytes;
2938 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2939
2940 s->rx_packets += tmp.rx_packets;
2941 s->rx_bytes += tmp.rx_bytes;
2942 s->tx_packets += tmp.tx_packets;
2943 s->tx_bytes += tmp.tx_bytes;
2944 }
2945
2946 s->rx_dropped = dev->stats.rx_dropped;
2947 s->tx_dropped = dev->stats.tx_dropped;
2948
2949 return s;
2950}
2951
2952static int macsec_get_iflink(const struct net_device *dev)
2953{
2954 return macsec_priv(dev)->real_dev->ifindex;
2955}
2956
Sabrina Dubrocae2003872016-08-12 16:10:32 +02002957
2958static int macsec_get_nest_level(struct net_device *dev)
2959{
2960 return macsec_priv(dev)->nest_level;
2961}
2962
2963
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002964static const struct net_device_ops macsec_netdev_ops = {
2965 .ndo_init = macsec_dev_init,
2966 .ndo_uninit = macsec_dev_uninit,
2967 .ndo_open = macsec_dev_open,
2968 .ndo_stop = macsec_dev_stop,
2969 .ndo_fix_features = macsec_fix_features,
2970 .ndo_change_mtu = macsec_change_mtu,
2971 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2972 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2973 .ndo_set_mac_address = macsec_set_mac_address,
2974 .ndo_start_xmit = macsec_start_xmit,
2975 .ndo_get_stats64 = macsec_get_stats64,
2976 .ndo_get_iflink = macsec_get_iflink,
Sabrina Dubrocae2003872016-08-12 16:10:32 +02002977 .ndo_get_lock_subclass = macsec_get_nest_level,
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002978};
2979
2980static const struct device_type macsec_type = {
2981 .name = "macsec",
2982};
2983
2984static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2985 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
Jakub Kicinski1abae2c2020-03-02 21:05:17 -08002986 [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002987 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2988 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2989 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2990 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2991 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2992 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2993 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2994 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
2995 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
2996 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
2997 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
2998};
2999
3000static void macsec_free_netdev(struct net_device *dev)
3001{
3002 struct macsec_dev *macsec = macsec_priv(dev);
3003 struct net_device *real_dev = macsec->real_dev;
3004
3005 free_percpu(macsec->stats);
3006 free_percpu(macsec->secy.tx_sc.stats);
3007
3008 dev_put(real_dev);
3009 free_netdev(dev);
3010}
3011
3012static void macsec_setup(struct net_device *dev)
3013{
3014 ether_setup(dev);
Phil Suttere4259742016-04-22 14:02:42 +02003015 dev->priv_flags |= IFF_NO_QUEUE;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003016 dev->netdev_ops = &macsec_netdev_ops;
3017 dev->destructor = macsec_free_netdev;
stephen hemmingerc24acf02016-09-07 14:07:32 -07003018 SET_NETDEV_DEVTYPE(dev, &macsec_type);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003019
3020 eth_zero_addr(dev->broadcast);
3021}
3022
3023static void macsec_changelink_common(struct net_device *dev,
3024 struct nlattr *data[])
3025{
3026 struct macsec_secy *secy;
3027 struct macsec_tx_sc *tx_sc;
3028
3029 secy = &macsec_priv(dev)->secy;
3030 tx_sc = &secy->tx_sc;
3031
3032 if (data[IFLA_MACSEC_ENCODING_SA]) {
3033 struct macsec_tx_sa *tx_sa;
3034
3035 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3036 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3037
3038 secy->operational = tx_sa && tx_sa->active;
3039 }
3040
3041 if (data[IFLA_MACSEC_WINDOW])
3042 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3043
3044 if (data[IFLA_MACSEC_ENCRYPT])
3045 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3046
3047 if (data[IFLA_MACSEC_PROTECT])
3048 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3049
3050 if (data[IFLA_MACSEC_INC_SCI])
3051 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3052
3053 if (data[IFLA_MACSEC_ES])
3054 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3055
3056 if (data[IFLA_MACSEC_SCB])
3057 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3058
3059 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3060 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3061
3062 if (data[IFLA_MACSEC_VALIDATION])
3063 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3064}
3065
3066static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3067 struct nlattr *data[])
3068{
3069 if (!data)
3070 return 0;
3071
3072 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3073 data[IFLA_MACSEC_ICV_LEN] ||
3074 data[IFLA_MACSEC_SCI] ||
3075 data[IFLA_MACSEC_PORT])
3076 return -EINVAL;
3077
3078 macsec_changelink_common(dev, data);
3079
3080 return 0;
3081}
3082
3083static void macsec_del_dev(struct macsec_dev *macsec)
3084{
3085 int i;
3086
3087 while (macsec->secy.rx_sc) {
3088 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3089
3090 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3091 free_rx_sc(rx_sc);
3092 }
3093
3094 for (i = 0; i < MACSEC_NUM_AN; i++) {
3095 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3096
3097 if (sa) {
3098 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3099 clear_tx_sa(sa);
3100 }
3101 }
3102}
3103
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003104static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3105{
3106 struct macsec_dev *macsec = macsec_priv(dev);
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003107 struct net_device *real_dev = macsec->real_dev;
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003108
3109 unregister_netdevice_queue(dev, head);
3110 list_del_rcu(&macsec->secys);
3111 macsec_del_dev(macsec);
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003112 netdev_upper_dev_unlink(real_dev, dev);
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003113
3114 macsec_generation++;
3115}
3116
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003117static void macsec_dellink(struct net_device *dev, struct list_head *head)
3118{
3119 struct macsec_dev *macsec = macsec_priv(dev);
3120 struct net_device *real_dev = macsec->real_dev;
3121 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3122
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003123 macsec_common_dellink(dev, head);
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02003124
Sabrina Dubroca960d5842016-04-22 11:28:06 +02003125 if (list_empty(&rxd->secys)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003126 netdev_rx_handler_unregister(real_dev);
Sabrina Dubroca960d5842016-04-22 11:28:06 +02003127 kfree(rxd);
3128 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003129}
3130
3131static int register_macsec_dev(struct net_device *real_dev,
3132 struct net_device *dev)
3133{
3134 struct macsec_dev *macsec = macsec_priv(dev);
3135 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3136
3137 if (!rxd) {
3138 int err;
3139
3140 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3141 if (!rxd)
3142 return -ENOMEM;
3143
3144 INIT_LIST_HEAD(&rxd->secys);
3145
3146 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3147 rxd);
Sabrina Dubroca960d5842016-04-22 11:28:06 +02003148 if (err < 0) {
3149 kfree(rxd);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003150 return err;
Sabrina Dubroca960d5842016-04-22 11:28:06 +02003151 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003152 }
3153
3154 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3155 return 0;
3156}
3157
3158static bool sci_exists(struct net_device *dev, sci_t sci)
3159{
3160 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3161 struct macsec_dev *macsec;
3162
3163 list_for_each_entry(macsec, &rxd->secys, secys) {
3164 if (macsec->secy.sci == sci)
3165 return true;
3166 }
3167
3168 return false;
3169}
3170
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003171static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3172{
3173 struct macsec_dev *macsec = macsec_priv(dev);
3174 struct macsec_secy *secy = &macsec->secy;
3175
3176 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3177 if (!macsec->stats)
3178 return -ENOMEM;
3179
3180 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3181 if (!secy->tx_sc.stats) {
3182 free_percpu(macsec->stats);
3183 return -ENOMEM;
3184 }
3185
3186 if (sci == MACSEC_UNDEF_SCI)
3187 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3188
3189 secy->netdev = dev;
3190 secy->operational = true;
3191 secy->key_len = DEFAULT_SAK_LEN;
3192 secy->icv_len = icv_len;
3193 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3194 secy->protect_frames = true;
3195 secy->replay_protect = false;
3196
3197 secy->sci = sci;
3198 secy->tx_sc.active = true;
3199 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3200 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3201 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3202 secy->tx_sc.end_station = false;
3203 secy->tx_sc.scb = false;
3204
3205 return 0;
3206}
3207
3208static int macsec_newlink(struct net *net, struct net_device *dev,
3209 struct nlattr *tb[], struct nlattr *data[])
3210{
3211 struct macsec_dev *macsec = macsec_priv(dev);
3212 struct net_device *real_dev;
3213 int err;
3214 sci_t sci;
3215 u8 icv_len = DEFAULT_ICV_LEN;
3216 rx_handler_func_t *rx_handler;
3217
3218 if (!tb[IFLA_LINK])
3219 return -EINVAL;
3220 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3221 if (!real_dev)
3222 return -ENODEV;
Willem de Bruijn52d58912020-03-22 13:51:13 -04003223 if (real_dev->type != ARPHRD_ETHER)
3224 return -EINVAL;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003225
3226 dev->priv_flags |= IFF_MACSEC;
3227
3228 macsec->real_dev = real_dev;
3229
3230 if (data && data[IFLA_MACSEC_ICV_LEN])
3231 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3232 dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3233
3234 rx_handler = rtnl_dereference(real_dev->rx_handler);
3235 if (rx_handler && rx_handler != macsec_handle_frame)
3236 return -EBUSY;
3237
3238 err = register_netdevice(dev);
3239 if (err < 0)
3240 return err;
3241
Sabrina Dubroca0759e552016-07-29 15:37:55 +02003242 dev_hold(real_dev);
3243
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02003244 macsec->nest_level = dev_get_nest_level(real_dev) + 1;
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003245 netdev_lockdep_set_classes(dev);
3246 lockdep_set_class_and_subclass(&dev->addr_list_lock,
3247 &macsec_netdev_addr_lock_key,
3248 macsec_get_nest_level(dev));
3249
3250 err = netdev_upper_dev_link(real_dev, dev);
3251 if (err < 0)
3252 goto unregister;
3253
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003254 /* need to be already registered so that ->init has run and
3255 * the MAC addr is set
3256 */
3257 if (data && data[IFLA_MACSEC_SCI])
3258 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3259 else if (data && data[IFLA_MACSEC_PORT])
3260 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3261 else
3262 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3263
3264 if (rx_handler && sci_exists(real_dev, sci)) {
3265 err = -EBUSY;
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003266 goto unlink;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003267 }
3268
3269 err = macsec_add_dev(dev, sci, icv_len);
3270 if (err)
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003271 goto unlink;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003272
3273 if (data)
3274 macsec_changelink_common(dev, data);
3275
3276 err = register_macsec_dev(real_dev, dev);
3277 if (err < 0)
3278 goto del_dev;
3279
Sabrina Dubroca8f70a4d2018-10-28 09:33:09 +01003280 netif_stacked_transfer_operstate(real_dev, dev);
3281 linkwatch_fire_event(dev);
3282
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02003283 macsec_generation++;
3284
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003285 return 0;
3286
3287del_dev:
3288 macsec_del_dev(macsec);
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003289unlink:
3290 netdev_upper_dev_unlink(real_dev, dev);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003291unregister:
3292 unregister_netdevice(dev);
3293 return err;
3294}
3295
3296static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[])
3297{
Sabrina Dubroca74816482016-04-22 11:28:08 +02003298 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003299 u8 icv_len = DEFAULT_ICV_LEN;
3300 int flag;
3301 bool es, scb, sci;
3302
3303 if (!data)
3304 return 0;
3305
3306 if (data[IFLA_MACSEC_CIPHER_SUITE])
3307 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3308
Davide Carattif04c3922016-07-22 15:07:58 +02003309 if (data[IFLA_MACSEC_ICV_LEN]) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003310 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
Davide Carattif04c3922016-07-22 15:07:58 +02003311 if (icv_len != DEFAULT_ICV_LEN) {
3312 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3313 struct crypto_aead *dummy_tfm;
3314
3315 dummy_tfm = macsec_alloc_tfm(dummy_key,
3316 DEFAULT_SAK_LEN,
3317 icv_len);
3318 if (IS_ERR(dummy_tfm))
3319 return PTR_ERR(dummy_tfm);
3320 crypto_free_aead(dummy_tfm);
3321 }
3322 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003323
3324 switch (csid) {
Sabrina Dubroca74816482016-04-22 11:28:08 +02003325 case MACSEC_DEFAULT_CIPHER_ID:
3326 case MACSEC_DEFAULT_CIPHER_ALT:
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003327 if (icv_len < MACSEC_MIN_ICV_LEN ||
Davide Caratti2ccbe2c2016-07-22 15:07:56 +02003328 icv_len > MACSEC_STD_ICV_LEN)
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003329 return -EINVAL;
3330 break;
3331 default:
3332 return -EINVAL;
3333 }
3334
3335 if (data[IFLA_MACSEC_ENCODING_SA]) {
3336 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3337 return -EINVAL;
3338 }
3339
3340 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3341 flag < IFLA_MACSEC_VALIDATION;
3342 flag++) {
3343 if (data[flag]) {
3344 if (nla_get_u8(data[flag]) > 1)
3345 return -EINVAL;
3346 }
3347 }
3348
3349 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3350 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3351 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3352
3353 if ((sci && (scb || es)) || (scb && es))
3354 return -EINVAL;
3355
3356 if (data[IFLA_MACSEC_VALIDATION] &&
3357 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3358 return -EINVAL;
3359
Sabrina Dubroca4b1fb932016-04-22 11:28:09 +02003360 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3361 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003362 !data[IFLA_MACSEC_WINDOW])
3363 return -EINVAL;
3364
3365 return 0;
3366}
3367
3368static struct net *macsec_get_link_net(const struct net_device *dev)
3369{
3370 return dev_net(macsec_priv(dev)->real_dev);
3371}
3372
3373static size_t macsec_get_size(const struct net_device *dev)
3374{
3375 return 0 +
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02003376 nla_total_size_64bit(8) + /* SCI */
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003377 nla_total_size(1) + /* ICV_LEN */
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02003378 nla_total_size_64bit(8) + /* CIPHER_SUITE */
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003379 nla_total_size(4) + /* WINDOW */
3380 nla_total_size(1) + /* ENCODING_SA */
3381 nla_total_size(1) + /* ENCRYPT */
3382 nla_total_size(1) + /* PROTECT */
3383 nla_total_size(1) + /* INC_SCI */
3384 nla_total_size(1) + /* ES */
3385 nla_total_size(1) + /* SCB */
3386 nla_total_size(1) + /* REPLAY_PROTECT */
3387 nla_total_size(1) + /* VALIDATION */
3388 0;
3389}
3390
3391static int macsec_fill_info(struct sk_buff *skb,
3392 const struct net_device *dev)
3393{
3394 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3395 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3396
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02003397 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3398 IFLA_MACSEC_PAD) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003399 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02003400 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
David S. Millerc0cc5312016-04-27 15:43:10 -04003401 MACSEC_DEFAULT_CIPHER_ID, IFLA_MACSEC_PAD) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003402 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3403 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3404 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3405 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3406 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3407 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3408 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3409 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3410 0)
3411 goto nla_put_failure;
3412
3413 if (secy->replay_protect) {
3414 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3415 goto nla_put_failure;
3416 }
3417
3418 return 0;
3419
3420nla_put_failure:
3421 return -EMSGSIZE;
3422}
3423
3424static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3425 .kind = "macsec",
3426 .priv_size = sizeof(struct macsec_dev),
3427 .maxtype = IFLA_MACSEC_MAX,
3428 .policy = macsec_rtnl_policy,
3429 .setup = macsec_setup,
3430 .validate = macsec_validate_attr,
3431 .newlink = macsec_newlink,
3432 .changelink = macsec_changelink,
3433 .dellink = macsec_dellink,
3434 .get_size = macsec_get_size,
3435 .fill_info = macsec_fill_info,
3436 .get_link_net = macsec_get_link_net,
3437};
3438
3439static bool is_macsec_master(struct net_device *dev)
3440{
3441 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3442}
3443
3444static int macsec_notify(struct notifier_block *this, unsigned long event,
3445 void *ptr)
3446{
3447 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3448 LIST_HEAD(head);
3449
3450 if (!is_macsec_master(real_dev))
3451 return NOTIFY_DONE;
3452
3453 switch (event) {
Sabrina Dubroca8f70a4d2018-10-28 09:33:09 +01003454 case NETDEV_DOWN:
3455 case NETDEV_UP:
3456 case NETDEV_CHANGE: {
3457 struct macsec_dev *m, *n;
3458 struct macsec_rxh_data *rxd;
3459
3460 rxd = macsec_data_rtnl(real_dev);
3461 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
3462 struct net_device *dev = m->secy.netdev;
3463
3464 netif_stacked_transfer_operstate(real_dev, dev);
3465 }
3466 break;
3467 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003468 case NETDEV_UNREGISTER: {
3469 struct macsec_dev *m, *n;
3470 struct macsec_rxh_data *rxd;
3471
3472 rxd = macsec_data_rtnl(real_dev);
3473 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003474 macsec_common_dellink(m->secy.netdev, &head);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003475 }
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003476
3477 netdev_rx_handler_unregister(real_dev);
3478 kfree(rxd);
3479
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003480 unregister_netdevice_many(&head);
3481 break;
3482 }
3483 case NETDEV_CHANGEMTU: {
3484 struct macsec_dev *m;
3485 struct macsec_rxh_data *rxd;
3486
3487 rxd = macsec_data_rtnl(real_dev);
3488 list_for_each_entry(m, &rxd->secys, secys) {
3489 struct net_device *dev = m->secy.netdev;
3490 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3491 macsec_extra_len(true));
3492
3493 if (dev->mtu > mtu)
3494 dev_set_mtu(dev, mtu);
3495 }
3496 }
3497 }
3498
3499 return NOTIFY_OK;
3500}
3501
3502static struct notifier_block macsec_notifier = {
3503 .notifier_call = macsec_notify,
3504};
3505
3506static int __init macsec_init(void)
3507{
3508 int err;
3509
3510 pr_info("MACsec IEEE 802.1AE\n");
3511 err = register_netdevice_notifier(&macsec_notifier);
3512 if (err)
3513 return err;
3514
3515 err = rtnl_link_register(&macsec_link_ops);
3516 if (err)
3517 goto notifier;
3518
3519 err = genl_register_family_with_ops(&macsec_fam, macsec_genl_ops);
3520 if (err)
3521 goto rtnl;
3522
3523 return 0;
3524
3525rtnl:
3526 rtnl_link_unregister(&macsec_link_ops);
3527notifier:
3528 unregister_netdevice_notifier(&macsec_notifier);
3529 return err;
3530}
3531
3532static void __exit macsec_exit(void)
3533{
3534 genl_unregister_family(&macsec_fam);
3535 rtnl_link_unregister(&macsec_link_ops);
3536 unregister_netdevice_notifier(&macsec_notifier);
Sabrina Dubrocab196c22a2016-06-14 15:25:14 +02003537 rcu_barrier();
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003538}
3539
3540module_init(macsec_init);
3541module_exit(macsec_exit);
3542
3543MODULE_ALIAS_RTNL_LINK("macsec");
Sabrina Dubroca4b4a1942017-08-22 15:36:08 +02003544MODULE_ALIAS_GENL_FAMILY("macsec");
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003545
3546MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3547MODULE_LICENSE("GPL v2");