blob: 70ca128328736edc1e2f04156e2ae620065fcd51 [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>
Sabrina Dubrocac09440f2016-03-11 18:07:33 +010022
23#include <uapi/linux/if_macsec.h>
24
25typedef u64 __bitwise sci_t;
26
27#define MACSEC_SCI_LEN 8
28
29/* SecTAG length = macsec_eth_header without the optional SCI */
30#define MACSEC_TAG_LEN 6
31
32struct macsec_eth_header {
33 struct ethhdr eth;
34 /* SecTAG */
35 u8 tci_an;
36#if defined(__LITTLE_ENDIAN_BITFIELD)
37 u8 short_length:6,
38 unused:2;
39#elif defined(__BIG_ENDIAN_BITFIELD)
40 u8 unused:2,
41 short_length:6;
42#else
43#error "Please fix <asm/byteorder.h>"
44#endif
45 __be32 packet_number;
46 u8 secure_channel_id[8]; /* optional */
47} __packed;
48
49#define MACSEC_TCI_VERSION 0x80
50#define MACSEC_TCI_ES 0x40 /* end station */
51#define MACSEC_TCI_SC 0x20 /* SCI present */
52#define MACSEC_TCI_SCB 0x10 /* epon */
53#define MACSEC_TCI_E 0x08 /* encryption */
54#define MACSEC_TCI_C 0x04 /* changed text */
55#define MACSEC_AN_MASK 0x03 /* association number */
56#define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
57
58/* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
59#define MIN_NON_SHORT_LEN 48
60
61#define GCM_AES_IV_LEN 12
62#define DEFAULT_ICV_LEN 16
63
64#define MACSEC_NUM_AN 4 /* 2 bits for the association number */
65
66#define for_each_rxsc(secy, sc) \
67 for (sc = rcu_dereference_bh(secy->rx_sc); \
68 sc; \
69 sc = rcu_dereference_bh(sc->next))
70#define for_each_rxsc_rtnl(secy, sc) \
71 for (sc = rtnl_dereference(secy->rx_sc); \
72 sc; \
73 sc = rtnl_dereference(sc->next))
74
75struct gcm_iv {
76 union {
77 u8 secure_channel_id[8];
78 sci_t sci;
79 };
80 __be32 pn;
81};
82
83/**
84 * struct macsec_key - SA key
85 * @id: user-provided key identifier
86 * @tfm: crypto struct, key storage
87 */
88struct macsec_key {
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +020089 u8 id[MACSEC_KEYID_LEN];
Sabrina Dubrocac09440f2016-03-11 18:07:33 +010090 struct crypto_aead *tfm;
91};
92
93struct macsec_rx_sc_stats {
94 __u64 InOctetsValidated;
95 __u64 InOctetsDecrypted;
96 __u64 InPktsUnchecked;
97 __u64 InPktsDelayed;
98 __u64 InPktsOK;
99 __u64 InPktsInvalid;
100 __u64 InPktsLate;
101 __u64 InPktsNotValid;
102 __u64 InPktsNotUsingSA;
103 __u64 InPktsUnusedSA;
104};
105
106struct macsec_rx_sa_stats {
107 __u32 InPktsOK;
108 __u32 InPktsInvalid;
109 __u32 InPktsNotValid;
110 __u32 InPktsNotUsingSA;
111 __u32 InPktsUnusedSA;
112};
113
114struct macsec_tx_sa_stats {
115 __u32 OutPktsProtected;
116 __u32 OutPktsEncrypted;
117};
118
119struct macsec_tx_sc_stats {
120 __u64 OutPktsProtected;
121 __u64 OutPktsEncrypted;
122 __u64 OutOctetsProtected;
123 __u64 OutOctetsEncrypted;
124};
125
126struct macsec_dev_stats {
127 __u64 OutPktsUntagged;
128 __u64 InPktsUntagged;
129 __u64 OutPktsTooLong;
130 __u64 InPktsNoTag;
131 __u64 InPktsBadTag;
132 __u64 InPktsUnknownSCI;
133 __u64 InPktsNoSCI;
134 __u64 InPktsOverrun;
135};
136
137/**
138 * struct macsec_rx_sa - receive secure association
139 * @active:
140 * @next_pn: packet number expected for the next packet
141 * @lock: protects next_pn manipulations
142 * @key: key structure
143 * @stats: per-SA stats
144 */
145struct macsec_rx_sa {
146 struct macsec_key key;
147 spinlock_t lock;
148 u32 next_pn;
149 atomic_t refcnt;
150 bool active;
151 struct macsec_rx_sa_stats __percpu *stats;
152 struct macsec_rx_sc *sc;
153 struct rcu_head rcu;
154};
155
156struct pcpu_rx_sc_stats {
157 struct macsec_rx_sc_stats stats;
158 struct u64_stats_sync syncp;
159};
160
161/**
162 * struct macsec_rx_sc - receive secure channel
163 * @sci: secure channel identifier for this SC
164 * @active: channel is active
165 * @sa: array of secure associations
166 * @stats: per-SC stats
167 */
168struct macsec_rx_sc {
169 struct macsec_rx_sc __rcu *next;
170 sci_t sci;
171 bool active;
172 struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
173 struct pcpu_rx_sc_stats __percpu *stats;
174 atomic_t refcnt;
175 struct rcu_head rcu_head;
176};
177
178/**
179 * struct macsec_tx_sa - transmit secure association
180 * @active:
181 * @next_pn: packet number to use for the next packet
182 * @lock: protects next_pn manipulations
183 * @key: key structure
184 * @stats: per-SA stats
185 */
186struct macsec_tx_sa {
187 struct macsec_key key;
188 spinlock_t lock;
189 u32 next_pn;
190 atomic_t refcnt;
191 bool active;
192 struct macsec_tx_sa_stats __percpu *stats;
193 struct rcu_head rcu;
194};
195
196struct pcpu_tx_sc_stats {
197 struct macsec_tx_sc_stats stats;
198 struct u64_stats_sync syncp;
199};
200
201/**
202 * struct macsec_tx_sc - transmit secure channel
203 * @active:
204 * @encoding_sa: association number of the SA currently in use
205 * @encrypt: encrypt packets on transmit, or authenticate only
206 * @send_sci: always include the SCI in the SecTAG
207 * @end_station:
208 * @scb: single copy broadcast flag
209 * @sa: array of secure associations
210 * @stats: stats for this TXSC
211 */
212struct macsec_tx_sc {
213 bool active;
214 u8 encoding_sa;
215 bool encrypt;
216 bool send_sci;
217 bool end_station;
218 bool scb;
219 struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
220 struct pcpu_tx_sc_stats __percpu *stats;
221};
222
223#define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
224
225/**
226 * struct macsec_secy - MACsec Security Entity
227 * @netdev: netdevice for this SecY
228 * @n_rx_sc: number of receive secure channels configured on this SecY
229 * @sci: secure channel identifier used for tx
230 * @key_len: length of keys used by the cipher suite
231 * @icv_len: length of ICV used by the cipher suite
232 * @validate_frames: validation mode
233 * @operational: MAC_Operational flag
234 * @protect_frames: enable protection for this SecY
235 * @replay_protect: enable packet number checks on receive
236 * @replay_window: size of the replay window
237 * @tx_sc: transmit secure channel
238 * @rx_sc: linked list of receive secure channels
239 */
240struct macsec_secy {
241 struct net_device *netdev;
242 unsigned int n_rx_sc;
243 sci_t sci;
244 u16 key_len;
245 u16 icv_len;
246 enum macsec_validation_type validate_frames;
247 bool operational;
248 bool protect_frames;
249 bool replay_protect;
250 u32 replay_window;
251 struct macsec_tx_sc tx_sc;
252 struct macsec_rx_sc __rcu *rx_sc;
253};
254
255struct pcpu_secy_stats {
256 struct macsec_dev_stats stats;
257 struct u64_stats_sync syncp;
258};
259
260/**
261 * struct macsec_dev - private data
262 * @secy: SecY config
263 * @real_dev: pointer to underlying netdevice
264 * @stats: MACsec device stats
265 * @secys: linked list of SecY's on the underlying device
266 */
267struct macsec_dev {
268 struct macsec_secy secy;
269 struct net_device *real_dev;
270 struct pcpu_secy_stats __percpu *stats;
271 struct list_head secys;
Paolo Abeni5491e7c2016-07-20 18:11:32 +0200272 struct gro_cells gro_cells;
Sabrina Dubrocae2003872016-08-12 16:10:32 +0200273 unsigned int nest_level;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100274};
275
276/**
277 * struct macsec_rxh_data - rx_handler private argument
278 * @secys: linked list of SecY's on this underlying device
279 */
280struct macsec_rxh_data {
281 struct list_head secys;
282};
283
284static struct macsec_dev *macsec_priv(const struct net_device *dev)
285{
286 return (struct macsec_dev *)netdev_priv(dev);
287}
288
289static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
290{
291 return rcu_dereference_bh(dev->rx_handler_data);
292}
293
294static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
295{
296 return rtnl_dereference(dev->rx_handler_data);
297}
298
299struct macsec_cb {
300 struct aead_request *req;
301 union {
302 struct macsec_tx_sa *tx_sa;
303 struct macsec_rx_sa *rx_sa;
304 };
305 u8 assoc_num;
306 bool valid;
307 bool has_sci;
308};
309
310static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
311{
312 struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
313
314 if (!sa || !sa->active)
315 return NULL;
316
317 if (!atomic_inc_not_zero(&sa->refcnt))
318 return NULL;
319
320 return sa;
321}
322
323static void free_rx_sc_rcu(struct rcu_head *head)
324{
325 struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
326
327 free_percpu(rx_sc->stats);
328 kfree(rx_sc);
329}
330
331static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
332{
333 return atomic_inc_not_zero(&sc->refcnt) ? sc : NULL;
334}
335
336static void macsec_rxsc_put(struct macsec_rx_sc *sc)
337{
338 if (atomic_dec_and_test(&sc->refcnt))
339 call_rcu(&sc->rcu_head, free_rx_sc_rcu);
340}
341
342static void free_rxsa(struct rcu_head *head)
343{
344 struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
345
346 crypto_free_aead(sa->key.tfm);
347 free_percpu(sa->stats);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100348 kfree(sa);
349}
350
351static void macsec_rxsa_put(struct macsec_rx_sa *sa)
352{
353 if (atomic_dec_and_test(&sa->refcnt))
354 call_rcu(&sa->rcu, free_rxsa);
355}
356
357static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
358{
359 struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
360
361 if (!sa || !sa->active)
362 return NULL;
363
364 if (!atomic_inc_not_zero(&sa->refcnt))
365 return NULL;
366
367 return sa;
368}
369
370static void free_txsa(struct rcu_head *head)
371{
372 struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
373
374 crypto_free_aead(sa->key.tfm);
375 free_percpu(sa->stats);
376 kfree(sa);
377}
378
379static void macsec_txsa_put(struct macsec_tx_sa *sa)
380{
381 if (atomic_dec_and_test(&sa->refcnt))
382 call_rcu(&sa->rcu, free_txsa);
383}
384
385static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
386{
387 BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
388 return (struct macsec_cb *)skb->cb;
389}
390
391#define MACSEC_PORT_ES (htons(0x0001))
392#define MACSEC_PORT_SCB (0x0000)
393#define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
394
395#define DEFAULT_SAK_LEN 16
396#define DEFAULT_SEND_SCI true
397#define DEFAULT_ENCRYPT false
398#define DEFAULT_ENCODING_SA 0
399
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200400static bool send_sci(const struct macsec_secy *secy)
401{
402 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
403
404 return tx_sc->send_sci ||
405 (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
406}
407
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100408static sci_t make_sci(u8 *addr, __be16 port)
409{
410 sci_t sci;
411
412 memcpy(&sci, addr, ETH_ALEN);
413 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
414
415 return sci;
416}
417
418static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
419{
420 sci_t sci;
421
422 if (sci_present)
423 memcpy(&sci, hdr->secure_channel_id,
424 sizeof(hdr->secure_channel_id));
425 else
426 sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
427
428 return sci;
429}
430
431static unsigned int macsec_sectag_len(bool sci_present)
432{
433 return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
434}
435
436static unsigned int macsec_hdr_len(bool sci_present)
437{
438 return macsec_sectag_len(sci_present) + ETH_HLEN;
439}
440
441static unsigned int macsec_extra_len(bool sci_present)
442{
443 return macsec_sectag_len(sci_present) + sizeof(__be16);
444}
445
446/* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
447static void macsec_fill_sectag(struct macsec_eth_header *h,
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200448 const struct macsec_secy *secy, u32 pn,
449 bool sci_present)
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100450{
451 const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
452
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200453 memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100454 h->eth.h_proto = htons(ETH_P_MACSEC);
455
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200456 if (sci_present) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100457 h->tci_an |= MACSEC_TCI_SC;
458 memcpy(&h->secure_channel_id, &secy->sci,
459 sizeof(h->secure_channel_id));
460 } else {
461 if (tx_sc->end_station)
462 h->tci_an |= MACSEC_TCI_ES;
463 if (tx_sc->scb)
464 h->tci_an |= MACSEC_TCI_SCB;
465 }
466
467 h->packet_number = htonl(pn);
468
469 /* with GCM, C/E clear for !encrypt, both set for encrypt */
470 if (tx_sc->encrypt)
471 h->tci_an |= MACSEC_TCI_CONFID;
472 else if (secy->icv_len != DEFAULT_ICV_LEN)
473 h->tci_an |= MACSEC_TCI_C;
474
475 h->tci_an |= tx_sc->encoding_sa;
476}
477
478static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
479{
480 if (data_len < MIN_NON_SHORT_LEN)
481 h->short_length = data_len;
482}
483
484/* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
485static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
486{
487 struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
488 int len = skb->len - 2 * ETH_ALEN;
489 int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
490
491 /* a) It comprises at least 17 octets */
492 if (skb->len <= 16)
493 return false;
494
495 /* b) MACsec EtherType: already checked */
496
497 /* c) V bit is clear */
498 if (h->tci_an & MACSEC_TCI_VERSION)
499 return false;
500
501 /* d) ES or SCB => !SC */
502 if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
503 (h->tci_an & MACSEC_TCI_SC))
504 return false;
505
506 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
507 if (h->unused)
508 return false;
509
510 /* rx.pn != 0 (figure 10-5) */
511 if (!h->packet_number)
512 return false;
513
514 /* length check, f) g) h) i) */
515 if (h->short_length)
516 return len == extra_len + h->short_length;
517 return len >= extra_len + MIN_NON_SHORT_LEN;
518}
519
520#define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
Davide Caratti2ccbe2c2016-07-22 15:07:56 +0200521#define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100522
523static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
524{
525 struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
526
527 gcm_iv->sci = sci;
528 gcm_iv->pn = htonl(pn);
529}
530
531static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
532{
533 return (struct macsec_eth_header *)skb_mac_header(skb);
534}
535
536static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
537{
538 u32 pn;
539
540 spin_lock_bh(&tx_sa->lock);
541 pn = tx_sa->next_pn;
542
543 tx_sa->next_pn++;
544 if (tx_sa->next_pn == 0) {
545 pr_debug("PN wrapped, transitioning to !oper\n");
546 tx_sa->active = false;
547 if (secy->protect_frames)
548 secy->operational = false;
549 }
550 spin_unlock_bh(&tx_sa->lock);
551
552 return pn;
553}
554
555static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
556{
557 struct macsec_dev *macsec = netdev_priv(dev);
558
559 skb->dev = macsec->real_dev;
560 skb_reset_mac_header(skb);
561 skb->protocol = eth_hdr(skb)->h_proto;
562}
563
564static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
565 struct macsec_tx_sa *tx_sa)
566{
567 struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
568
569 u64_stats_update_begin(&txsc_stats->syncp);
570 if (tx_sc->encrypt) {
571 txsc_stats->stats.OutOctetsEncrypted += skb->len;
572 txsc_stats->stats.OutPktsEncrypted++;
573 this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
574 } else {
575 txsc_stats->stats.OutOctetsProtected += skb->len;
576 txsc_stats->stats.OutPktsProtected++;
577 this_cpu_inc(tx_sa->stats->OutPktsProtected);
578 }
579 u64_stats_update_end(&txsc_stats->syncp);
580}
581
582static void count_tx(struct net_device *dev, int ret, int len)
583{
584 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
585 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
586
587 u64_stats_update_begin(&stats->syncp);
588 stats->tx_packets++;
589 stats->tx_bytes += len;
590 u64_stats_update_end(&stats->syncp);
591 } else {
592 dev->stats.tx_dropped++;
593 }
594}
595
596static void macsec_encrypt_done(struct crypto_async_request *base, int err)
597{
598 struct sk_buff *skb = base->data;
599 struct net_device *dev = skb->dev;
600 struct macsec_dev *macsec = macsec_priv(dev);
601 struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
602 int len, ret;
603
604 aead_request_free(macsec_skb_cb(skb)->req);
605
606 rcu_read_lock_bh();
607 macsec_encrypt_finish(skb, dev);
608 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
609 len = skb->len;
610 ret = dev_queue_xmit(skb);
611 count_tx(dev, ret, len);
612 rcu_read_unlock_bh();
613
614 macsec_txsa_put(sa);
615 dev_put(dev);
616}
617
Sabrina Dubroca5d9649b2016-06-14 15:25:15 +0200618static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
619 unsigned char **iv,
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +0200620 struct scatterlist **sg,
621 int num_frags)
Sabrina Dubroca5d9649b2016-06-14 15:25:15 +0200622{
623 size_t size, iv_offset, sg_offset;
624 struct aead_request *req;
625 void *tmp;
626
627 size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
628 iv_offset = size;
629 size += GCM_AES_IV_LEN;
630
631 size = ALIGN(size, __alignof__(struct scatterlist));
632 sg_offset = size;
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +0200633 size += sizeof(struct scatterlist) * num_frags;
Sabrina Dubroca5d9649b2016-06-14 15:25:15 +0200634
635 tmp = kmalloc(size, GFP_ATOMIC);
636 if (!tmp)
637 return NULL;
638
639 *iv = (unsigned char *)(tmp + iv_offset);
640 *sg = (struct scatterlist *)(tmp + sg_offset);
641 req = tmp;
642
643 aead_request_set_tfm(req, tfm);
644
645 return req;
646}
647
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100648static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
649 struct net_device *dev)
650{
651 int ret;
Sabrina Dubroca5d9649b2016-06-14 15:25:15 +0200652 struct scatterlist *sg;
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +0200653 struct sk_buff *trailer;
Sabrina Dubroca5d9649b2016-06-14 15:25:15 +0200654 unsigned char *iv;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100655 struct ethhdr *eth;
656 struct macsec_eth_header *hh;
657 size_t unprotected_len;
658 struct aead_request *req;
659 struct macsec_secy *secy;
660 struct macsec_tx_sc *tx_sc;
661 struct macsec_tx_sa *tx_sa;
662 struct macsec_dev *macsec = macsec_priv(dev);
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200663 bool sci_present;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100664 u32 pn;
665
666 secy = &macsec->secy;
667 tx_sc = &secy->tx_sc;
668
669 /* 10.5.1 TX SA assignment */
670 tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
671 if (!tx_sa) {
672 secy->operational = false;
673 kfree_skb(skb);
674 return ERR_PTR(-EINVAL);
675 }
676
677 if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
678 skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
679 struct sk_buff *nskb = skb_copy_expand(skb,
680 MACSEC_NEEDED_HEADROOM,
681 MACSEC_NEEDED_TAILROOM,
682 GFP_ATOMIC);
683 if (likely(nskb)) {
684 consume_skb(skb);
685 skb = nskb;
686 } else {
687 macsec_txsa_put(tx_sa);
688 kfree_skb(skb);
689 return ERR_PTR(-ENOMEM);
690 }
691 } else {
692 skb = skb_unshare(skb, GFP_ATOMIC);
693 if (!skb) {
694 macsec_txsa_put(tx_sa);
695 return ERR_PTR(-ENOMEM);
696 }
697 }
698
699 unprotected_len = skb->len;
700 eth = eth_hdr(skb);
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200701 sci_present = send_sci(secy);
702 hh = (struct macsec_eth_header *)skb_push(skb, macsec_extra_len(sci_present));
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100703 memmove(hh, eth, 2 * ETH_ALEN);
704
705 pn = tx_sa_update_pn(tx_sa, secy);
706 if (pn == 0) {
707 macsec_txsa_put(tx_sa);
708 kfree_skb(skb);
709 return ERR_PTR(-ENOLINK);
710 }
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200711 macsec_fill_sectag(hh, secy, pn, sci_present);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100712 macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
713
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100714 skb_put(skb, secy->icv_len);
715
716 if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
717 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
718
719 u64_stats_update_begin(&secy_stats->syncp);
720 secy_stats->stats.OutPktsTooLong++;
721 u64_stats_update_end(&secy_stats->syncp);
722
723 macsec_txsa_put(tx_sa);
724 kfree_skb(skb);
725 return ERR_PTR(-EINVAL);
726 }
727
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +0200728 ret = skb_cow_data(skb, 0, &trailer);
729 if (unlikely(ret < 0)) {
730 macsec_txsa_put(tx_sa);
731 kfree_skb(skb);
732 return ERR_PTR(ret);
733 }
734
735 req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100736 if (!req) {
737 macsec_txsa_put(tx_sa);
738 kfree_skb(skb);
739 return ERR_PTR(-ENOMEM);
740 }
741
Sabrina Dubroca5d9649b2016-06-14 15:25:15 +0200742 macsec_fill_iv(iv, secy->sci, pn);
743
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +0200744 sg_init_table(sg, ret);
Jason A. Donenfeld6083f162017-06-04 04:16:25 +0200745 ret = skb_to_sgvec(skb, sg, 0, skb->len);
746 if (unlikely(ret < 0)) {
Sabrina Dubroca5f6590d2017-10-10 17:07:12 +0200747 aead_request_free(req);
Jason A. Donenfeld6083f162017-06-04 04:16:25 +0200748 macsec_txsa_put(tx_sa);
749 kfree_skb(skb);
750 return ERR_PTR(ret);
751 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100752
753 if (tx_sc->encrypt) {
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200754 int len = skb->len - macsec_hdr_len(sci_present) -
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100755 secy->icv_len;
756 aead_request_set_crypt(req, sg, sg, len, iv);
Tobias Brunnere0f841f2016-10-24 15:44:26 +0200757 aead_request_set_ad(req, macsec_hdr_len(sci_present));
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100758 } else {
759 aead_request_set_crypt(req, sg, sg, 0, iv);
760 aead_request_set_ad(req, skb->len - secy->icv_len);
761 }
762
763 macsec_skb_cb(skb)->req = req;
764 macsec_skb_cb(skb)->tx_sa = tx_sa;
765 aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
766
767 dev_hold(skb->dev);
768 ret = crypto_aead_encrypt(req);
769 if (ret == -EINPROGRESS) {
770 return ERR_PTR(ret);
771 } else if (ret != 0) {
772 dev_put(skb->dev);
773 kfree_skb(skb);
774 aead_request_free(req);
775 macsec_txsa_put(tx_sa);
776 return ERR_PTR(-EINVAL);
777 }
778
779 dev_put(skb->dev);
780 aead_request_free(req);
781 macsec_txsa_put(tx_sa);
782
783 return skb;
784}
785
786static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
787{
788 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
789 struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
790 struct macsec_eth_header *hdr = macsec_ethhdr(skb);
791 u32 lowest_pn = 0;
792
793 spin_lock(&rx_sa->lock);
794 if (rx_sa->next_pn >= secy->replay_window)
795 lowest_pn = rx_sa->next_pn - secy->replay_window;
796
797 /* Now perform replay protection check again
798 * (see IEEE 802.1AE-2006 figure 10-5)
799 */
800 if (secy->replay_protect && pn < lowest_pn) {
801 spin_unlock(&rx_sa->lock);
802 u64_stats_update_begin(&rxsc_stats->syncp);
803 rxsc_stats->stats.InPktsLate++;
804 u64_stats_update_end(&rxsc_stats->syncp);
805 return false;
806 }
807
808 if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
809 u64_stats_update_begin(&rxsc_stats->syncp);
810 if (hdr->tci_an & MACSEC_TCI_E)
811 rxsc_stats->stats.InOctetsDecrypted += skb->len;
812 else
813 rxsc_stats->stats.InOctetsValidated += skb->len;
814 u64_stats_update_end(&rxsc_stats->syncp);
815 }
816
817 if (!macsec_skb_cb(skb)->valid) {
818 spin_unlock(&rx_sa->lock);
819
820 /* 10.6.5 */
821 if (hdr->tci_an & MACSEC_TCI_C ||
822 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
823 u64_stats_update_begin(&rxsc_stats->syncp);
824 rxsc_stats->stats.InPktsNotValid++;
825 u64_stats_update_end(&rxsc_stats->syncp);
826 return false;
827 }
828
829 u64_stats_update_begin(&rxsc_stats->syncp);
830 if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
831 rxsc_stats->stats.InPktsInvalid++;
832 this_cpu_inc(rx_sa->stats->InPktsInvalid);
833 } else if (pn < lowest_pn) {
834 rxsc_stats->stats.InPktsDelayed++;
835 } else {
836 rxsc_stats->stats.InPktsUnchecked++;
837 }
838 u64_stats_update_end(&rxsc_stats->syncp);
839 } else {
840 u64_stats_update_begin(&rxsc_stats->syncp);
841 if (pn < lowest_pn) {
842 rxsc_stats->stats.InPktsDelayed++;
843 } else {
844 rxsc_stats->stats.InPktsOK++;
845 this_cpu_inc(rx_sa->stats->InPktsOK);
846 }
847 u64_stats_update_end(&rxsc_stats->syncp);
848
849 if (pn >= rx_sa->next_pn)
850 rx_sa->next_pn = pn + 1;
851 spin_unlock(&rx_sa->lock);
852 }
853
854 return true;
855}
856
857static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
858{
859 skb->pkt_type = PACKET_HOST;
860 skb->protocol = eth_type_trans(skb, dev);
861
862 skb_reset_network_header(skb);
863 if (!skb_transport_header_was_set(skb))
864 skb_reset_transport_header(skb);
865 skb_reset_mac_len(skb);
866}
867
868static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
869{
870 memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
871 skb_pull(skb, hdr_len);
872 pskb_trim_unique(skb, skb->len - icv_len);
873}
874
875static void count_rx(struct net_device *dev, int len)
876{
877 struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
878
879 u64_stats_update_begin(&stats->syncp);
880 stats->rx_packets++;
881 stats->rx_bytes += len;
882 u64_stats_update_end(&stats->syncp);
883}
884
885static void macsec_decrypt_done(struct crypto_async_request *base, int err)
886{
887 struct sk_buff *skb = base->data;
888 struct net_device *dev = skb->dev;
889 struct macsec_dev *macsec = macsec_priv(dev);
890 struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
Sabrina Dubrocac78ebe12016-07-29 15:37:53 +0200891 struct macsec_rx_sc *rx_sc = rx_sa->sc;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100892 int len, ret;
893 u32 pn;
894
895 aead_request_free(macsec_skb_cb(skb)->req);
896
897 rcu_read_lock_bh();
898 pn = ntohl(macsec_ethhdr(skb)->packet_number);
899 if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
900 rcu_read_unlock_bh();
901 kfree_skb(skb);
902 goto out;
903 }
904
905 macsec_finalize_skb(skb, macsec->secy.icv_len,
906 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
907 macsec_reset_skb(skb, macsec->secy.netdev);
908
909 len = skb->len;
Paolo Abeni5491e7c2016-07-20 18:11:32 +0200910 ret = gro_cells_receive(&macsec->gro_cells, skb);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100911 if (ret == NET_RX_SUCCESS)
912 count_rx(dev, len);
913 else
914 macsec->secy.netdev->stats.rx_dropped++;
915
916 rcu_read_unlock_bh();
917
918out:
919 macsec_rxsa_put(rx_sa);
Sabrina Dubrocac78ebe12016-07-29 15:37:53 +0200920 macsec_rxsc_put(rx_sc);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100921 dev_put(dev);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100922}
923
924static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
925 struct net_device *dev,
926 struct macsec_rx_sa *rx_sa,
927 sci_t sci,
928 struct macsec_secy *secy)
929{
930 int ret;
Sabrina Dubroca5d9649b2016-06-14 15:25:15 +0200931 struct scatterlist *sg;
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +0200932 struct sk_buff *trailer;
Sabrina Dubroca5d9649b2016-06-14 15:25:15 +0200933 unsigned char *iv;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100934 struct aead_request *req;
935 struct macsec_eth_header *hdr;
936 u16 icv_len = secy->icv_len;
937
938 macsec_skb_cb(skb)->valid = false;
939 skb = skb_share_check(skb, GFP_ATOMIC);
940 if (!skb)
Sabrina Dubrocac3b7d0b2016-04-22 11:28:04 +0200941 return ERR_PTR(-ENOMEM);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100942
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +0200943 ret = skb_cow_data(skb, 0, &trailer);
944 if (unlikely(ret < 0)) {
945 kfree_skb(skb);
946 return ERR_PTR(ret);
947 }
948 req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100949 if (!req) {
950 kfree_skb(skb);
Sabrina Dubrocac3b7d0b2016-04-22 11:28:04 +0200951 return ERR_PTR(-ENOMEM);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100952 }
953
954 hdr = (struct macsec_eth_header *)skb->data;
955 macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
956
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +0200957 sg_init_table(sg, ret);
Jason A. Donenfeld6083f162017-06-04 04:16:25 +0200958 ret = skb_to_sgvec(skb, sg, 0, skb->len);
959 if (unlikely(ret < 0)) {
Sabrina Dubroca5f6590d2017-10-10 17:07:12 +0200960 aead_request_free(req);
Jason A. Donenfeld6083f162017-06-04 04:16:25 +0200961 kfree_skb(skb);
962 return ERR_PTR(ret);
963 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100964
965 if (hdr->tci_an & MACSEC_TCI_E) {
966 /* confidentiality: ethernet + macsec header
967 * authenticated, encrypted payload
968 */
969 int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
970
971 aead_request_set_crypt(req, sg, sg, len, iv);
972 aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
973 skb = skb_unshare(skb, GFP_ATOMIC);
974 if (!skb) {
975 aead_request_free(req);
Sabrina Dubrocac3b7d0b2016-04-22 11:28:04 +0200976 return ERR_PTR(-ENOMEM);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100977 }
978 } else {
979 /* integrity only: all headers + data authenticated */
980 aead_request_set_crypt(req, sg, sg, icv_len, iv);
981 aead_request_set_ad(req, skb->len - icv_len);
982 }
983
984 macsec_skb_cb(skb)->req = req;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100985 skb->dev = dev;
986 aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
987
988 dev_hold(dev);
989 ret = crypto_aead_decrypt(req);
990 if (ret == -EINPROGRESS) {
Sabrina Dubrocac3b7d0b2016-04-22 11:28:04 +0200991 return ERR_PTR(ret);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100992 } else if (ret != 0) {
993 /* decryption/authentication failed
994 * 10.6 if validateFrames is disabled, deliver anyway
995 */
996 if (ret != -EBADMSG) {
997 kfree_skb(skb);
Sabrina Dubrocac3b7d0b2016-04-22 11:28:04 +0200998 skb = ERR_PTR(ret);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +0100999 }
1000 } else {
1001 macsec_skb_cb(skb)->valid = true;
1002 }
1003 dev_put(dev);
1004
1005 aead_request_free(req);
1006
1007 return skb;
1008}
1009
1010static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
1011{
1012 struct macsec_rx_sc *rx_sc;
1013
1014 for_each_rxsc(secy, rx_sc) {
1015 if (rx_sc->sci == sci)
1016 return rx_sc;
1017 }
1018
1019 return NULL;
1020}
1021
1022static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
1023{
1024 struct macsec_rx_sc *rx_sc;
1025
1026 for_each_rxsc_rtnl(secy, rx_sc) {
1027 if (rx_sc->sci == sci)
1028 return rx_sc;
1029 }
1030
1031 return NULL;
1032}
1033
1034static void handle_not_macsec(struct sk_buff *skb)
1035{
1036 struct macsec_rxh_data *rxd;
1037 struct macsec_dev *macsec;
1038
1039 rcu_read_lock();
1040 rxd = macsec_data_rcu(skb->dev);
1041
1042 /* 10.6 If the management control validateFrames is not
1043 * Strict, frames without a SecTAG are received, counted, and
1044 * delivered to the Controlled Port
1045 */
1046 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1047 struct sk_buff *nskb;
1048 int ret;
1049 struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
1050
1051 if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1052 u64_stats_update_begin(&secy_stats->syncp);
1053 secy_stats->stats.InPktsNoTag++;
1054 u64_stats_update_end(&secy_stats->syncp);
1055 continue;
1056 }
1057
1058 /* deliver on this port */
1059 nskb = skb_clone(skb, GFP_ATOMIC);
1060 if (!nskb)
1061 break;
1062
1063 nskb->dev = macsec->secy.netdev;
1064
1065 ret = netif_rx(nskb);
1066 if (ret == NET_RX_SUCCESS) {
1067 u64_stats_update_begin(&secy_stats->syncp);
1068 secy_stats->stats.InPktsUntagged++;
1069 u64_stats_update_end(&secy_stats->syncp);
1070 } else {
1071 macsec->secy.netdev->stats.rx_dropped++;
1072 }
1073 }
1074
1075 rcu_read_unlock();
1076}
1077
1078static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
1079{
1080 struct sk_buff *skb = *pskb;
1081 struct net_device *dev = skb->dev;
1082 struct macsec_eth_header *hdr;
1083 struct macsec_secy *secy = NULL;
1084 struct macsec_rx_sc *rx_sc;
1085 struct macsec_rx_sa *rx_sa;
1086 struct macsec_rxh_data *rxd;
1087 struct macsec_dev *macsec;
1088 sci_t sci;
1089 u32 pn;
1090 bool cbit;
1091 struct pcpu_rx_sc_stats *rxsc_stats;
1092 struct pcpu_secy_stats *secy_stats;
1093 bool pulled_sci;
Paolo Abeni5491e7c2016-07-20 18:11:32 +02001094 int ret;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001095
1096 if (skb_headroom(skb) < ETH_HLEN)
1097 goto drop_direct;
1098
1099 hdr = macsec_ethhdr(skb);
1100 if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
1101 handle_not_macsec(skb);
1102
1103 /* and deliver to the uncontrolled port */
1104 return RX_HANDLER_PASS;
1105 }
1106
1107 skb = skb_unshare(skb, GFP_ATOMIC);
Andreas Steinmetza76ca412019-06-30 22:46:42 +02001108 *pskb = skb;
1109 if (!skb)
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001110 return RX_HANDLER_CONSUMED;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001111
1112 pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
1113 if (!pulled_sci) {
1114 if (!pskb_may_pull(skb, macsec_extra_len(false)))
1115 goto drop_direct;
1116 }
1117
1118 hdr = macsec_ethhdr(skb);
1119
1120 /* Frames with a SecTAG that has the TCI E bit set but the C
1121 * bit clear are discarded, as this reserved encoding is used
1122 * to identify frames with a SecTAG that are not to be
1123 * delivered to the Controlled Port.
1124 */
1125 if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
1126 return RX_HANDLER_PASS;
1127
1128 /* now, pull the extra length */
1129 if (hdr->tci_an & MACSEC_TCI_SC) {
1130 if (!pulled_sci)
1131 goto drop_direct;
1132 }
1133
1134 /* ethernet header is part of crypto processing */
1135 skb_push(skb, ETH_HLEN);
1136
1137 macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
1138 macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
1139 sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
1140
1141 rcu_read_lock();
1142 rxd = macsec_data_rcu(skb->dev);
1143
1144 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1145 struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
Sabrina Dubrocac78ebe12016-07-29 15:37:53 +02001146 sc = sc ? macsec_rxsc_get(sc) : NULL;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001147
1148 if (sc) {
1149 secy = &macsec->secy;
1150 rx_sc = sc;
1151 break;
1152 }
1153 }
1154
1155 if (!secy)
1156 goto nosci;
1157
1158 dev = secy->netdev;
1159 macsec = macsec_priv(dev);
1160 secy_stats = this_cpu_ptr(macsec->stats);
1161 rxsc_stats = this_cpu_ptr(rx_sc->stats);
1162
1163 if (!macsec_validate_skb(skb, secy->icv_len)) {
1164 u64_stats_update_begin(&secy_stats->syncp);
1165 secy_stats->stats.InPktsBadTag++;
1166 u64_stats_update_end(&secy_stats->syncp);
1167 goto drop_nosa;
1168 }
1169
1170 rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
1171 if (!rx_sa) {
1172 /* 10.6.1 if the SA is not in use */
1173
1174 /* If validateFrames is Strict or the C bit in the
1175 * SecTAG is set, discard
1176 */
1177 if (hdr->tci_an & MACSEC_TCI_C ||
1178 secy->validate_frames == MACSEC_VALIDATE_STRICT) {
1179 u64_stats_update_begin(&rxsc_stats->syncp);
1180 rxsc_stats->stats.InPktsNotUsingSA++;
1181 u64_stats_update_end(&rxsc_stats->syncp);
1182 goto drop_nosa;
1183 }
1184
1185 /* not Strict, the frame (with the SecTAG and ICV
1186 * removed) is delivered to the Controlled Port.
1187 */
1188 u64_stats_update_begin(&rxsc_stats->syncp);
1189 rxsc_stats->stats.InPktsUnusedSA++;
1190 u64_stats_update_end(&rxsc_stats->syncp);
1191 goto deliver;
1192 }
1193
1194 /* First, PN check to avoid decrypting obviously wrong packets */
1195 pn = ntohl(hdr->packet_number);
1196 if (secy->replay_protect) {
1197 bool late;
1198
1199 spin_lock(&rx_sa->lock);
1200 late = rx_sa->next_pn >= secy->replay_window &&
1201 pn < (rx_sa->next_pn - secy->replay_window);
1202 spin_unlock(&rx_sa->lock);
1203
1204 if (late) {
1205 u64_stats_update_begin(&rxsc_stats->syncp);
1206 rxsc_stats->stats.InPktsLate++;
1207 u64_stats_update_end(&rxsc_stats->syncp);
1208 goto drop;
1209 }
1210 }
1211
Beniamino Galvanie3a3b622016-07-26 12:24:53 +02001212 macsec_skb_cb(skb)->rx_sa = rx_sa;
1213
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001214 /* Disabled && !changed text => skip validation */
1215 if (hdr->tci_an & MACSEC_TCI_C ||
1216 secy->validate_frames != MACSEC_VALIDATE_DISABLED)
1217 skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
1218
Sabrina Dubrocac3b7d0b2016-04-22 11:28:04 +02001219 if (IS_ERR(skb)) {
1220 /* the decrypt callback needs the reference */
Sabrina Dubrocac78ebe12016-07-29 15:37:53 +02001221 if (PTR_ERR(skb) != -EINPROGRESS) {
Sabrina Dubrocac3b7d0b2016-04-22 11:28:04 +02001222 macsec_rxsa_put(rx_sa);
Sabrina Dubrocac78ebe12016-07-29 15:37:53 +02001223 macsec_rxsc_put(rx_sc);
1224 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001225 rcu_read_unlock();
1226 *pskb = NULL;
1227 return RX_HANDLER_CONSUMED;
1228 }
1229
1230 if (!macsec_post_decrypt(skb, secy, pn))
1231 goto drop;
1232
1233deliver:
1234 macsec_finalize_skb(skb, secy->icv_len,
1235 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1236 macsec_reset_skb(skb, secy->netdev);
1237
Sabrina Dubroca497f3582016-04-22 11:28:03 +02001238 if (rx_sa)
1239 macsec_rxsa_put(rx_sa);
Sabrina Dubrocac78ebe12016-07-29 15:37:53 +02001240 macsec_rxsc_put(rx_sc);
Paolo Abeni5491e7c2016-07-20 18:11:32 +02001241
1242 ret = gro_cells_receive(&macsec->gro_cells, skb);
1243 if (ret == NET_RX_SUCCESS)
1244 count_rx(dev, skb->len);
1245 else
1246 macsec->secy.netdev->stats.rx_dropped++;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001247
1248 rcu_read_unlock();
1249
Paolo Abeni5491e7c2016-07-20 18:11:32 +02001250 *pskb = NULL;
1251 return RX_HANDLER_CONSUMED;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001252
1253drop:
1254 macsec_rxsa_put(rx_sa);
1255drop_nosa:
Sabrina Dubrocac78ebe12016-07-29 15:37:53 +02001256 macsec_rxsc_put(rx_sc);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001257 rcu_read_unlock();
1258drop_direct:
1259 kfree_skb(skb);
1260 *pskb = NULL;
1261 return RX_HANDLER_CONSUMED;
1262
1263nosci:
1264 /* 10.6.1 if the SC is not found */
1265 cbit = !!(hdr->tci_an & MACSEC_TCI_C);
1266 if (!cbit)
1267 macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
1268 macsec_extra_len(macsec_skb_cb(skb)->has_sci));
1269
1270 list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
1271 struct sk_buff *nskb;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001272
1273 secy_stats = this_cpu_ptr(macsec->stats);
1274
1275 /* If validateFrames is Strict or the C bit in the
1276 * SecTAG is set, discard
1277 */
1278 if (cbit ||
1279 macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
1280 u64_stats_update_begin(&secy_stats->syncp);
1281 secy_stats->stats.InPktsNoSCI++;
1282 u64_stats_update_end(&secy_stats->syncp);
1283 continue;
1284 }
1285
1286 /* not strict, the frame (with the SecTAG and ICV
1287 * removed) is delivered to the Controlled Port.
1288 */
1289 nskb = skb_clone(skb, GFP_ATOMIC);
1290 if (!nskb)
1291 break;
1292
1293 macsec_reset_skb(nskb, macsec->secy.netdev);
1294
1295 ret = netif_rx(nskb);
1296 if (ret == NET_RX_SUCCESS) {
1297 u64_stats_update_begin(&secy_stats->syncp);
1298 secy_stats->stats.InPktsUnknownSCI++;
1299 u64_stats_update_end(&secy_stats->syncp);
1300 } else {
1301 macsec->secy.netdev->stats.rx_dropped++;
1302 }
1303 }
1304
1305 rcu_read_unlock();
1306 *pskb = skb;
1307 return RX_HANDLER_PASS;
1308}
1309
1310static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
1311{
1312 struct crypto_aead *tfm;
1313 int ret;
1314
Sabrina Dubroca6052f7f2016-06-14 15:25:16 +02001315 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001316
1317 if (IS_ERR(tfm))
1318 return tfm;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001319
1320 ret = crypto_aead_setkey(tfm, key, key_len);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001321 if (ret < 0)
1322 goto fail;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001323
1324 ret = crypto_aead_setauthsize(tfm, icv_len);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001325 if (ret < 0)
1326 goto fail;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001327
1328 return tfm;
Davide Caratti34aedfe2016-07-22 15:07:57 +02001329fail:
1330 crypto_free_aead(tfm);
1331 return ERR_PTR(ret);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001332}
1333
1334static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
1335 int icv_len)
1336{
1337 rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
1338 if (!rx_sa->stats)
Davide Caratti34aedfe2016-07-22 15:07:57 +02001339 return -ENOMEM;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001340
1341 rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001342 if (IS_ERR(rx_sa->key.tfm)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001343 free_percpu(rx_sa->stats);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001344 return PTR_ERR(rx_sa->key.tfm);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001345 }
1346
1347 rx_sa->active = false;
1348 rx_sa->next_pn = 1;
1349 atomic_set(&rx_sa->refcnt, 1);
1350 spin_lock_init(&rx_sa->lock);
1351
1352 return 0;
1353}
1354
1355static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
1356{
1357 rx_sa->active = false;
1358
1359 macsec_rxsa_put(rx_sa);
1360}
1361
1362static void free_rx_sc(struct macsec_rx_sc *rx_sc)
1363{
1364 int i;
1365
1366 for (i = 0; i < MACSEC_NUM_AN; i++) {
1367 struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
1368
1369 RCU_INIT_POINTER(rx_sc->sa[i], NULL);
1370 if (sa)
1371 clear_rx_sa(sa);
1372 }
1373
1374 macsec_rxsc_put(rx_sc);
1375}
1376
1377static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
1378{
1379 struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
1380
1381 for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
1382 rx_sc;
1383 rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
1384 if (rx_sc->sci == sci) {
1385 if (rx_sc->active)
1386 secy->n_rx_sc--;
1387 rcu_assign_pointer(*rx_scp, rx_sc->next);
1388 return rx_sc;
1389 }
1390 }
1391
1392 return NULL;
1393}
1394
1395static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
1396{
1397 struct macsec_rx_sc *rx_sc;
1398 struct macsec_dev *macsec;
1399 struct net_device *real_dev = macsec_priv(dev)->real_dev;
1400 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
1401 struct macsec_secy *secy;
1402
1403 list_for_each_entry(macsec, &rxd->secys, secys) {
1404 if (find_rx_sc_rtnl(&macsec->secy, sci))
1405 return ERR_PTR(-EEXIST);
1406 }
1407
1408 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
1409 if (!rx_sc)
1410 return ERR_PTR(-ENOMEM);
1411
1412 rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
1413 if (!rx_sc->stats) {
1414 kfree(rx_sc);
1415 return ERR_PTR(-ENOMEM);
1416 }
1417
1418 rx_sc->sci = sci;
1419 rx_sc->active = true;
1420 atomic_set(&rx_sc->refcnt, 1);
1421
1422 secy = &macsec_priv(dev)->secy;
1423 rcu_assign_pointer(rx_sc->next, secy->rx_sc);
1424 rcu_assign_pointer(secy->rx_sc, rx_sc);
1425
1426 if (rx_sc->active)
1427 secy->n_rx_sc++;
1428
1429 return rx_sc;
1430}
1431
1432static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
1433 int icv_len)
1434{
1435 tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
1436 if (!tx_sa->stats)
Davide Caratti34aedfe2016-07-22 15:07:57 +02001437 return -ENOMEM;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001438
1439 tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001440 if (IS_ERR(tx_sa->key.tfm)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001441 free_percpu(tx_sa->stats);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001442 return PTR_ERR(tx_sa->key.tfm);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001443 }
1444
1445 tx_sa->active = false;
1446 atomic_set(&tx_sa->refcnt, 1);
1447 spin_lock_init(&tx_sa->lock);
1448
1449 return 0;
1450}
1451
1452static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
1453{
1454 tx_sa->active = false;
1455
1456 macsec_txsa_put(tx_sa);
1457}
1458
1459static struct genl_family macsec_fam = {
1460 .id = GENL_ID_GENERATE,
1461 .name = MACSEC_GENL_NAME,
1462 .hdrsize = 0,
1463 .version = MACSEC_GENL_VERSION,
1464 .maxattr = MACSEC_ATTR_MAX,
1465 .netnsok = true,
1466};
1467
1468static struct net_device *get_dev_from_nl(struct net *net,
1469 struct nlattr **attrs)
1470{
1471 int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
1472 struct net_device *dev;
1473
1474 dev = __dev_get_by_index(net, ifindex);
1475 if (!dev)
1476 return ERR_PTR(-ENODEV);
1477
1478 if (!netif_is_macsec(dev))
1479 return ERR_PTR(-ENODEV);
1480
1481 return dev;
1482}
1483
1484static sci_t nla_get_sci(const struct nlattr *nla)
1485{
1486 return (__force sci_t)nla_get_u64(nla);
1487}
1488
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02001489static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
1490 int padattr)
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001491{
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02001492 return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001493}
1494
1495static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
1496 struct nlattr **attrs,
1497 struct nlattr **tb_sa,
1498 struct net_device **devp,
1499 struct macsec_secy **secyp,
1500 struct macsec_tx_sc **scp,
1501 u8 *assoc_num)
1502{
1503 struct net_device *dev;
1504 struct macsec_secy *secy;
1505 struct macsec_tx_sc *tx_sc;
1506 struct macsec_tx_sa *tx_sa;
1507
1508 if (!tb_sa[MACSEC_SA_ATTR_AN])
1509 return ERR_PTR(-EINVAL);
1510
1511 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1512
1513 dev = get_dev_from_nl(net, attrs);
1514 if (IS_ERR(dev))
1515 return ERR_CAST(dev);
1516
1517 if (*assoc_num >= MACSEC_NUM_AN)
1518 return ERR_PTR(-EINVAL);
1519
1520 secy = &macsec_priv(dev)->secy;
1521 tx_sc = &secy->tx_sc;
1522
1523 tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
1524 if (!tx_sa)
1525 return ERR_PTR(-ENODEV);
1526
1527 *devp = dev;
1528 *scp = tx_sc;
1529 *secyp = secy;
1530 return tx_sa;
1531}
1532
1533static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
1534 struct nlattr **attrs,
1535 struct nlattr **tb_rxsc,
1536 struct net_device **devp,
1537 struct macsec_secy **secyp)
1538{
1539 struct net_device *dev;
1540 struct macsec_secy *secy;
1541 struct macsec_rx_sc *rx_sc;
1542 sci_t sci;
1543
1544 dev = get_dev_from_nl(net, attrs);
1545 if (IS_ERR(dev))
1546 return ERR_CAST(dev);
1547
1548 secy = &macsec_priv(dev)->secy;
1549
1550 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1551 return ERR_PTR(-EINVAL);
1552
1553 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1554 rx_sc = find_rx_sc_rtnl(secy, sci);
1555 if (!rx_sc)
1556 return ERR_PTR(-ENODEV);
1557
1558 *secyp = secy;
1559 *devp = dev;
1560
1561 return rx_sc;
1562}
1563
1564static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
1565 struct nlattr **attrs,
1566 struct nlattr **tb_rxsc,
1567 struct nlattr **tb_sa,
1568 struct net_device **devp,
1569 struct macsec_secy **secyp,
1570 struct macsec_rx_sc **scp,
1571 u8 *assoc_num)
1572{
1573 struct macsec_rx_sc *rx_sc;
1574 struct macsec_rx_sa *rx_sa;
1575
1576 if (!tb_sa[MACSEC_SA_ATTR_AN])
1577 return ERR_PTR(-EINVAL);
1578
1579 *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1580 if (*assoc_num >= MACSEC_NUM_AN)
1581 return ERR_PTR(-EINVAL);
1582
1583 rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
1584 if (IS_ERR(rx_sc))
1585 return ERR_CAST(rx_sc);
1586
1587 rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
1588 if (!rx_sa)
1589 return ERR_PTR(-ENODEV);
1590
1591 *scp = rx_sc;
1592 return rx_sa;
1593}
1594
1595
1596static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
1597 [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
1598 [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
1599 [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
1600};
1601
1602static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
1603 [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
1604 [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
1605};
1606
1607static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
1608 [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
1609 [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
1610 [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02001611 [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
1612 .len = MACSEC_KEYID_LEN, },
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001613 [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
1614 .len = MACSEC_MAX_KEY_LEN, },
1615};
1616
1617static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
1618{
1619 if (!attrs[MACSEC_ATTR_SA_CONFIG])
1620 return -EINVAL;
1621
1622 if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG],
1623 macsec_genl_sa_policy))
1624 return -EINVAL;
1625
1626 return 0;
1627}
1628
1629static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
1630{
1631 if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
1632 return -EINVAL;
1633
1634 if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG],
1635 macsec_genl_rxsc_policy))
1636 return -EINVAL;
1637
1638 return 0;
1639}
1640
1641static bool validate_add_rxsa(struct nlattr **attrs)
1642{
1643 if (!attrs[MACSEC_SA_ATTR_AN] ||
1644 !attrs[MACSEC_SA_ATTR_KEY] ||
1645 !attrs[MACSEC_SA_ATTR_KEYID])
1646 return false;
1647
1648 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1649 return false;
1650
1651 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1652 return false;
1653
1654 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1655 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1656 return false;
1657 }
1658
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02001659 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1660 return false;
1661
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001662 return true;
1663}
1664
1665static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
1666{
1667 struct net_device *dev;
1668 struct nlattr **attrs = info->attrs;
1669 struct macsec_secy *secy;
1670 struct macsec_rx_sc *rx_sc;
1671 struct macsec_rx_sa *rx_sa;
1672 unsigned char assoc_num;
1673 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1674 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
Davide Caratti34aedfe2016-07-22 15:07:57 +02001675 int err;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001676
1677 if (!attrs[MACSEC_ATTR_IFINDEX])
1678 return -EINVAL;
1679
1680 if (parse_sa_config(attrs, tb_sa))
1681 return -EINVAL;
1682
1683 if (parse_rxsc_config(attrs, tb_rxsc))
1684 return -EINVAL;
1685
1686 if (!validate_add_rxsa(tb_sa))
1687 return -EINVAL;
1688
1689 rtnl_lock();
1690 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
Sabrina Dubroca36b232c2016-07-29 15:37:54 +02001691 if (IS_ERR(rx_sc)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001692 rtnl_unlock();
1693 return PTR_ERR(rx_sc);
1694 }
1695
1696 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1697
1698 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1699 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1700 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1701 rtnl_unlock();
1702 return -EINVAL;
1703 }
1704
1705 rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
1706 if (rx_sa) {
1707 rtnl_unlock();
1708 return -EBUSY;
1709 }
1710
1711 rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001712 if (!rx_sa) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001713 rtnl_unlock();
1714 return -ENOMEM;
1715 }
1716
Davide Caratti34aedfe2016-07-22 15:07:57 +02001717 err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1718 secy->key_len, secy->icv_len);
1719 if (err < 0) {
1720 kfree(rx_sa);
1721 rtnl_unlock();
1722 return err;
1723 }
1724
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001725 if (tb_sa[MACSEC_SA_ATTR_PN]) {
1726 spin_lock_bh(&rx_sa->lock);
1727 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1728 spin_unlock_bh(&rx_sa->lock);
1729 }
1730
1731 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1732 rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1733
Sabrina Dubroca1968a0b2016-05-18 13:34:40 +02001734 nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001735 rx_sa->sc = rx_sc;
1736 rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
1737
1738 rtnl_unlock();
1739
1740 return 0;
1741}
1742
1743static bool validate_add_rxsc(struct nlattr **attrs)
1744{
1745 if (!attrs[MACSEC_RXSC_ATTR_SCI])
1746 return false;
1747
1748 if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
1749 if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
1750 return false;
1751 }
1752
1753 return true;
1754}
1755
1756static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
1757{
1758 struct net_device *dev;
1759 sci_t sci = MACSEC_UNDEF_SCI;
1760 struct nlattr **attrs = info->attrs;
1761 struct macsec_rx_sc *rx_sc;
1762 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1763
1764 if (!attrs[MACSEC_ATTR_IFINDEX])
1765 return -EINVAL;
1766
1767 if (parse_rxsc_config(attrs, tb_rxsc))
1768 return -EINVAL;
1769
1770 if (!validate_add_rxsc(tb_rxsc))
1771 return -EINVAL;
1772
1773 rtnl_lock();
1774 dev = get_dev_from_nl(genl_info_net(info), attrs);
1775 if (IS_ERR(dev)) {
1776 rtnl_unlock();
1777 return PTR_ERR(dev);
1778 }
1779
1780 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1781
1782 rx_sc = create_rx_sc(dev, sci);
1783 if (IS_ERR(rx_sc)) {
1784 rtnl_unlock();
1785 return PTR_ERR(rx_sc);
1786 }
1787
1788 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
1789 rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
1790
1791 rtnl_unlock();
1792
1793 return 0;
1794}
1795
1796static bool validate_add_txsa(struct nlattr **attrs)
1797{
1798 if (!attrs[MACSEC_SA_ATTR_AN] ||
1799 !attrs[MACSEC_SA_ATTR_PN] ||
1800 !attrs[MACSEC_SA_ATTR_KEY] ||
1801 !attrs[MACSEC_SA_ATTR_KEYID])
1802 return false;
1803
1804 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
1805 return false;
1806
1807 if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
1808 return false;
1809
1810 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
1811 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
1812 return false;
1813 }
1814
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02001815 if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
1816 return false;
1817
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001818 return true;
1819}
1820
1821static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
1822{
1823 struct net_device *dev;
1824 struct nlattr **attrs = info->attrs;
1825 struct macsec_secy *secy;
1826 struct macsec_tx_sc *tx_sc;
1827 struct macsec_tx_sa *tx_sa;
1828 unsigned char assoc_num;
1829 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
Davide Caratti34aedfe2016-07-22 15:07:57 +02001830 int err;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001831
1832 if (!attrs[MACSEC_ATTR_IFINDEX])
1833 return -EINVAL;
1834
1835 if (parse_sa_config(attrs, tb_sa))
1836 return -EINVAL;
1837
1838 if (!validate_add_txsa(tb_sa))
1839 return -EINVAL;
1840
1841 rtnl_lock();
1842 dev = get_dev_from_nl(genl_info_net(info), attrs);
1843 if (IS_ERR(dev)) {
1844 rtnl_unlock();
1845 return PTR_ERR(dev);
1846 }
1847
1848 secy = &macsec_priv(dev)->secy;
1849 tx_sc = &secy->tx_sc;
1850
1851 assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
1852
1853 if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
1854 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
1855 nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
1856 rtnl_unlock();
1857 return -EINVAL;
1858 }
1859
1860 tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
1861 if (tx_sa) {
1862 rtnl_unlock();
1863 return -EBUSY;
1864 }
1865
1866 tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
Davide Caratti34aedfe2016-07-22 15:07:57 +02001867 if (!tx_sa) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001868 rtnl_unlock();
1869 return -ENOMEM;
1870 }
1871
Davide Caratti34aedfe2016-07-22 15:07:57 +02001872 err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
1873 secy->key_len, secy->icv_len);
1874 if (err < 0) {
1875 kfree(tx_sa);
1876 rtnl_unlock();
1877 return err;
1878 }
1879
Sabrina Dubroca1968a0b2016-05-18 13:34:40 +02001880 nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01001881
1882 spin_lock_bh(&tx_sa->lock);
1883 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
1884 spin_unlock_bh(&tx_sa->lock);
1885
1886 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
1887 tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
1888
1889 if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
1890 secy->operational = true;
1891
1892 rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
1893
1894 rtnl_unlock();
1895
1896 return 0;
1897}
1898
1899static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
1900{
1901 struct nlattr **attrs = info->attrs;
1902 struct net_device *dev;
1903 struct macsec_secy *secy;
1904 struct macsec_rx_sc *rx_sc;
1905 struct macsec_rx_sa *rx_sa;
1906 u8 assoc_num;
1907 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1908 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1909
1910 if (!attrs[MACSEC_ATTR_IFINDEX])
1911 return -EINVAL;
1912
1913 if (parse_sa_config(attrs, tb_sa))
1914 return -EINVAL;
1915
1916 if (parse_rxsc_config(attrs, tb_rxsc))
1917 return -EINVAL;
1918
1919 rtnl_lock();
1920 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
1921 &dev, &secy, &rx_sc, &assoc_num);
1922 if (IS_ERR(rx_sa)) {
1923 rtnl_unlock();
1924 return PTR_ERR(rx_sa);
1925 }
1926
1927 if (rx_sa->active) {
1928 rtnl_unlock();
1929 return -EBUSY;
1930 }
1931
1932 RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
1933 clear_rx_sa(rx_sa);
1934
1935 rtnl_unlock();
1936
1937 return 0;
1938}
1939
1940static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
1941{
1942 struct nlattr **attrs = info->attrs;
1943 struct net_device *dev;
1944 struct macsec_secy *secy;
1945 struct macsec_rx_sc *rx_sc;
1946 sci_t sci;
1947 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
1948
1949 if (!attrs[MACSEC_ATTR_IFINDEX])
1950 return -EINVAL;
1951
1952 if (parse_rxsc_config(attrs, tb_rxsc))
1953 return -EINVAL;
1954
1955 if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
1956 return -EINVAL;
1957
1958 rtnl_lock();
1959 dev = get_dev_from_nl(genl_info_net(info), info->attrs);
1960 if (IS_ERR(dev)) {
1961 rtnl_unlock();
1962 return PTR_ERR(dev);
1963 }
1964
1965 secy = &macsec_priv(dev)->secy;
1966 sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
1967
1968 rx_sc = del_rx_sc(secy, sci);
1969 if (!rx_sc) {
1970 rtnl_unlock();
1971 return -ENODEV;
1972 }
1973
1974 free_rx_sc(rx_sc);
1975 rtnl_unlock();
1976
1977 return 0;
1978}
1979
1980static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
1981{
1982 struct nlattr **attrs = info->attrs;
1983 struct net_device *dev;
1984 struct macsec_secy *secy;
1985 struct macsec_tx_sc *tx_sc;
1986 struct macsec_tx_sa *tx_sa;
1987 u8 assoc_num;
1988 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
1989
1990 if (!attrs[MACSEC_ATTR_IFINDEX])
1991 return -EINVAL;
1992
1993 if (parse_sa_config(attrs, tb_sa))
1994 return -EINVAL;
1995
1996 rtnl_lock();
1997 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
1998 &dev, &secy, &tx_sc, &assoc_num);
1999 if (IS_ERR(tx_sa)) {
2000 rtnl_unlock();
2001 return PTR_ERR(tx_sa);
2002 }
2003
2004 if (tx_sa->active) {
2005 rtnl_unlock();
2006 return -EBUSY;
2007 }
2008
2009 RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
2010 clear_tx_sa(tx_sa);
2011
2012 rtnl_unlock();
2013
2014 return 0;
2015}
2016
2017static bool validate_upd_sa(struct nlattr **attrs)
2018{
2019 if (!attrs[MACSEC_SA_ATTR_AN] ||
2020 attrs[MACSEC_SA_ATTR_KEY] ||
2021 attrs[MACSEC_SA_ATTR_KEYID])
2022 return false;
2023
2024 if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
2025 return false;
2026
2027 if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
2028 return false;
2029
2030 if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
2031 if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
2032 return false;
2033 }
2034
2035 return true;
2036}
2037
2038static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
2039{
2040 struct nlattr **attrs = info->attrs;
2041 struct net_device *dev;
2042 struct macsec_secy *secy;
2043 struct macsec_tx_sc *tx_sc;
2044 struct macsec_tx_sa *tx_sa;
2045 u8 assoc_num;
2046 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2047
2048 if (!attrs[MACSEC_ATTR_IFINDEX])
2049 return -EINVAL;
2050
2051 if (parse_sa_config(attrs, tb_sa))
2052 return -EINVAL;
2053
2054 if (!validate_upd_sa(tb_sa))
2055 return -EINVAL;
2056
2057 rtnl_lock();
2058 tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
2059 &dev, &secy, &tx_sc, &assoc_num);
2060 if (IS_ERR(tx_sa)) {
2061 rtnl_unlock();
2062 return PTR_ERR(tx_sa);
2063 }
2064
2065 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2066 spin_lock_bh(&tx_sa->lock);
2067 tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2068 spin_unlock_bh(&tx_sa->lock);
2069 }
2070
2071 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2072 tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2073
2074 if (assoc_num == tx_sc->encoding_sa)
2075 secy->operational = tx_sa->active;
2076
2077 rtnl_unlock();
2078
2079 return 0;
2080}
2081
2082static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
2083{
2084 struct nlattr **attrs = info->attrs;
2085 struct net_device *dev;
2086 struct macsec_secy *secy;
2087 struct macsec_rx_sc *rx_sc;
2088 struct macsec_rx_sa *rx_sa;
2089 u8 assoc_num;
2090 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2091 struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
2092
2093 if (!attrs[MACSEC_ATTR_IFINDEX])
2094 return -EINVAL;
2095
2096 if (parse_rxsc_config(attrs, tb_rxsc))
2097 return -EINVAL;
2098
2099 if (parse_sa_config(attrs, tb_sa))
2100 return -EINVAL;
2101
2102 if (!validate_upd_sa(tb_sa))
2103 return -EINVAL;
2104
2105 rtnl_lock();
2106 rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
2107 &dev, &secy, &rx_sc, &assoc_num);
2108 if (IS_ERR(rx_sa)) {
2109 rtnl_unlock();
2110 return PTR_ERR(rx_sa);
2111 }
2112
2113 if (tb_sa[MACSEC_SA_ATTR_PN]) {
2114 spin_lock_bh(&rx_sa->lock);
2115 rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
2116 spin_unlock_bh(&rx_sa->lock);
2117 }
2118
2119 if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
2120 rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
2121
2122 rtnl_unlock();
2123 return 0;
2124}
2125
2126static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
2127{
2128 struct nlattr **attrs = info->attrs;
2129 struct net_device *dev;
2130 struct macsec_secy *secy;
2131 struct macsec_rx_sc *rx_sc;
2132 struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
2133
2134 if (!attrs[MACSEC_ATTR_IFINDEX])
2135 return -EINVAL;
2136
2137 if (parse_rxsc_config(attrs, tb_rxsc))
2138 return -EINVAL;
2139
2140 if (!validate_add_rxsc(tb_rxsc))
2141 return -EINVAL;
2142
2143 rtnl_lock();
2144 rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
2145 if (IS_ERR(rx_sc)) {
2146 rtnl_unlock();
2147 return PTR_ERR(rx_sc);
2148 }
2149
2150 if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
2151 bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
2152
2153 if (rx_sc->active != new)
2154 secy->n_rx_sc += new ? 1 : -1;
2155
2156 rx_sc->active = new;
2157 }
2158
2159 rtnl_unlock();
2160
2161 return 0;
2162}
2163
2164static int copy_tx_sa_stats(struct sk_buff *skb,
2165 struct macsec_tx_sa_stats __percpu *pstats)
2166{
2167 struct macsec_tx_sa_stats sum = {0, };
2168 int cpu;
2169
2170 for_each_possible_cpu(cpu) {
2171 const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2172
2173 sum.OutPktsProtected += stats->OutPktsProtected;
2174 sum.OutPktsEncrypted += stats->OutPktsEncrypted;
2175 }
2176
2177 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
2178 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
2179 return -EMSGSIZE;
2180
2181 return 0;
2182}
2183
2184static int copy_rx_sa_stats(struct sk_buff *skb,
2185 struct macsec_rx_sa_stats __percpu *pstats)
2186{
2187 struct macsec_rx_sa_stats sum = {0, };
2188 int cpu;
2189
2190 for_each_possible_cpu(cpu) {
2191 const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
2192
2193 sum.InPktsOK += stats->InPktsOK;
2194 sum.InPktsInvalid += stats->InPktsInvalid;
2195 sum.InPktsNotValid += stats->InPktsNotValid;
2196 sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
2197 sum.InPktsUnusedSA += stats->InPktsUnusedSA;
2198 }
2199
2200 if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
2201 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
2202 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
2203 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
2204 nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
2205 return -EMSGSIZE;
2206
2207 return 0;
2208}
2209
2210static int copy_rx_sc_stats(struct sk_buff *skb,
2211 struct pcpu_rx_sc_stats __percpu *pstats)
2212{
2213 struct macsec_rx_sc_stats sum = {0, };
2214 int cpu;
2215
2216 for_each_possible_cpu(cpu) {
2217 const struct pcpu_rx_sc_stats *stats;
2218 struct macsec_rx_sc_stats tmp;
2219 unsigned int start;
2220
2221 stats = per_cpu_ptr(pstats, cpu);
2222 do {
2223 start = u64_stats_fetch_begin_irq(&stats->syncp);
2224 memcpy(&tmp, &stats->stats, sizeof(tmp));
2225 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2226
2227 sum.InOctetsValidated += tmp.InOctetsValidated;
2228 sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
2229 sum.InPktsUnchecked += tmp.InPktsUnchecked;
2230 sum.InPktsDelayed += tmp.InPktsDelayed;
2231 sum.InPktsOK += tmp.InPktsOK;
2232 sum.InPktsInvalid += tmp.InPktsInvalid;
2233 sum.InPktsLate += tmp.InPktsLate;
2234 sum.InPktsNotValid += tmp.InPktsNotValid;
2235 sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
2236 sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
2237 }
2238
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002239 if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
2240 sum.InOctetsValidated,
2241 MACSEC_RXSC_STATS_ATTR_PAD) ||
2242 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
2243 sum.InOctetsDecrypted,
2244 MACSEC_RXSC_STATS_ATTR_PAD) ||
2245 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
2246 sum.InPktsUnchecked,
2247 MACSEC_RXSC_STATS_ATTR_PAD) ||
2248 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
2249 sum.InPktsDelayed,
2250 MACSEC_RXSC_STATS_ATTR_PAD) ||
2251 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
2252 sum.InPktsOK,
2253 MACSEC_RXSC_STATS_ATTR_PAD) ||
2254 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
2255 sum.InPktsInvalid,
2256 MACSEC_RXSC_STATS_ATTR_PAD) ||
2257 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
2258 sum.InPktsLate,
2259 MACSEC_RXSC_STATS_ATTR_PAD) ||
2260 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
2261 sum.InPktsNotValid,
2262 MACSEC_RXSC_STATS_ATTR_PAD) ||
2263 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
2264 sum.InPktsNotUsingSA,
2265 MACSEC_RXSC_STATS_ATTR_PAD) ||
2266 nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
2267 sum.InPktsUnusedSA,
2268 MACSEC_RXSC_STATS_ATTR_PAD))
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002269 return -EMSGSIZE;
2270
2271 return 0;
2272}
2273
2274static int copy_tx_sc_stats(struct sk_buff *skb,
2275 struct pcpu_tx_sc_stats __percpu *pstats)
2276{
2277 struct macsec_tx_sc_stats sum = {0, };
2278 int cpu;
2279
2280 for_each_possible_cpu(cpu) {
2281 const struct pcpu_tx_sc_stats *stats;
2282 struct macsec_tx_sc_stats tmp;
2283 unsigned int start;
2284
2285 stats = per_cpu_ptr(pstats, cpu);
2286 do {
2287 start = u64_stats_fetch_begin_irq(&stats->syncp);
2288 memcpy(&tmp, &stats->stats, sizeof(tmp));
2289 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2290
2291 sum.OutPktsProtected += tmp.OutPktsProtected;
2292 sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
2293 sum.OutOctetsProtected += tmp.OutOctetsProtected;
2294 sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
2295 }
2296
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002297 if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
2298 sum.OutPktsProtected,
2299 MACSEC_TXSC_STATS_ATTR_PAD) ||
2300 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
2301 sum.OutPktsEncrypted,
2302 MACSEC_TXSC_STATS_ATTR_PAD) ||
2303 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
2304 sum.OutOctetsProtected,
2305 MACSEC_TXSC_STATS_ATTR_PAD) ||
2306 nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
2307 sum.OutOctetsEncrypted,
2308 MACSEC_TXSC_STATS_ATTR_PAD))
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002309 return -EMSGSIZE;
2310
2311 return 0;
2312}
2313
2314static int copy_secy_stats(struct sk_buff *skb,
2315 struct pcpu_secy_stats __percpu *pstats)
2316{
2317 struct macsec_dev_stats sum = {0, };
2318 int cpu;
2319
2320 for_each_possible_cpu(cpu) {
2321 const struct pcpu_secy_stats *stats;
2322 struct macsec_dev_stats tmp;
2323 unsigned int start;
2324
2325 stats = per_cpu_ptr(pstats, cpu);
2326 do {
2327 start = u64_stats_fetch_begin_irq(&stats->syncp);
2328 memcpy(&tmp, &stats->stats, sizeof(tmp));
2329 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2330
2331 sum.OutPktsUntagged += tmp.OutPktsUntagged;
2332 sum.InPktsUntagged += tmp.InPktsUntagged;
2333 sum.OutPktsTooLong += tmp.OutPktsTooLong;
2334 sum.InPktsNoTag += tmp.InPktsNoTag;
2335 sum.InPktsBadTag += tmp.InPktsBadTag;
2336 sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
2337 sum.InPktsNoSCI += tmp.InPktsNoSCI;
2338 sum.InPktsOverrun += tmp.InPktsOverrun;
2339 }
2340
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002341 if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
2342 sum.OutPktsUntagged,
2343 MACSEC_SECY_STATS_ATTR_PAD) ||
2344 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
2345 sum.InPktsUntagged,
2346 MACSEC_SECY_STATS_ATTR_PAD) ||
2347 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
2348 sum.OutPktsTooLong,
2349 MACSEC_SECY_STATS_ATTR_PAD) ||
2350 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
2351 sum.InPktsNoTag,
2352 MACSEC_SECY_STATS_ATTR_PAD) ||
2353 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
2354 sum.InPktsBadTag,
2355 MACSEC_SECY_STATS_ATTR_PAD) ||
2356 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
2357 sum.InPktsUnknownSCI,
2358 MACSEC_SECY_STATS_ATTR_PAD) ||
2359 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
2360 sum.InPktsNoSCI,
2361 MACSEC_SECY_STATS_ATTR_PAD) ||
2362 nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
2363 sum.InPktsOverrun,
2364 MACSEC_SECY_STATS_ATTR_PAD))
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002365 return -EMSGSIZE;
2366
2367 return 0;
2368}
2369
2370static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
2371{
2372 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2373 struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
2374
2375 if (!secy_nest)
2376 return 1;
2377
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002378 if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
2379 MACSEC_SECY_ATTR_PAD) ||
2380 nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
David S. Millerc0cc5312016-04-27 15:43:10 -04002381 MACSEC_DEFAULT_CIPHER_ID,
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002382 MACSEC_SECY_ATTR_PAD) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002383 nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
2384 nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
2385 nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
2386 nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
2387 nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
2388 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
2389 nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
2390 nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
2391 nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
2392 nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
2393 goto cancel;
2394
2395 if (secy->replay_protect) {
2396 if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
2397 goto cancel;
2398 }
2399
2400 nla_nest_end(skb, secy_nest);
2401 return 0;
2402
2403cancel:
2404 nla_nest_cancel(skb, secy_nest);
2405 return 1;
2406}
2407
2408static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
2409 struct sk_buff *skb, struct netlink_callback *cb)
2410{
2411 struct macsec_rx_sc *rx_sc;
2412 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
2413 struct nlattr *txsa_list, *rxsc_list;
2414 int i, j;
2415 void *hdr;
2416 struct nlattr *attr;
2417
2418 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2419 &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
2420 if (!hdr)
2421 return -EMSGSIZE;
2422
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02002423 genl_dump_check_consistent(cb, hdr, &macsec_fam);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002424
2425 if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
2426 goto nla_put_failure;
2427
2428 if (nla_put_secy(secy, skb))
2429 goto nla_put_failure;
2430
2431 attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
2432 if (!attr)
2433 goto nla_put_failure;
2434 if (copy_tx_sc_stats(skb, tx_sc->stats)) {
2435 nla_nest_cancel(skb, attr);
2436 goto nla_put_failure;
2437 }
2438 nla_nest_end(skb, attr);
2439
2440 attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
2441 if (!attr)
2442 goto nla_put_failure;
2443 if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
2444 nla_nest_cancel(skb, attr);
2445 goto nla_put_failure;
2446 }
2447 nla_nest_end(skb, attr);
2448
2449 txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
2450 if (!txsa_list)
2451 goto nla_put_failure;
2452 for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
2453 struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
2454 struct nlattr *txsa_nest;
2455
2456 if (!tx_sa)
2457 continue;
2458
2459 txsa_nest = nla_nest_start(skb, j++);
2460 if (!txsa_nest) {
2461 nla_nest_cancel(skb, txsa_list);
2462 goto nla_put_failure;
2463 }
2464
2465 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2466 nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02002467 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002468 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
2469 nla_nest_cancel(skb, txsa_nest);
2470 nla_nest_cancel(skb, txsa_list);
2471 goto nla_put_failure;
2472 }
2473
2474 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2475 if (!attr) {
2476 nla_nest_cancel(skb, txsa_nest);
2477 nla_nest_cancel(skb, txsa_list);
2478 goto nla_put_failure;
2479 }
2480 if (copy_tx_sa_stats(skb, tx_sa->stats)) {
2481 nla_nest_cancel(skb, attr);
2482 nla_nest_cancel(skb, txsa_nest);
2483 nla_nest_cancel(skb, txsa_list);
2484 goto nla_put_failure;
2485 }
2486 nla_nest_end(skb, attr);
2487
2488 nla_nest_end(skb, txsa_nest);
2489 }
2490 nla_nest_end(skb, txsa_list);
2491
2492 rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
2493 if (!rxsc_list)
2494 goto nla_put_failure;
2495
2496 j = 1;
2497 for_each_rxsc_rtnl(secy, rx_sc) {
2498 int k;
2499 struct nlattr *rxsa_list;
2500 struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
2501
2502 if (!rxsc_nest) {
2503 nla_nest_cancel(skb, rxsc_list);
2504 goto nla_put_failure;
2505 }
2506
2507 if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02002508 nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
2509 MACSEC_RXSC_ATTR_PAD)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002510 nla_nest_cancel(skb, rxsc_nest);
2511 nla_nest_cancel(skb, rxsc_list);
2512 goto nla_put_failure;
2513 }
2514
2515 attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
2516 if (!attr) {
2517 nla_nest_cancel(skb, rxsc_nest);
2518 nla_nest_cancel(skb, rxsc_list);
2519 goto nla_put_failure;
2520 }
2521 if (copy_rx_sc_stats(skb, rx_sc->stats)) {
2522 nla_nest_cancel(skb, attr);
2523 nla_nest_cancel(skb, rxsc_nest);
2524 nla_nest_cancel(skb, rxsc_list);
2525 goto nla_put_failure;
2526 }
2527 nla_nest_end(skb, attr);
2528
2529 rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
2530 if (!rxsa_list) {
2531 nla_nest_cancel(skb, rxsc_nest);
2532 nla_nest_cancel(skb, rxsc_list);
2533 goto nla_put_failure;
2534 }
2535
2536 for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
2537 struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
2538 struct nlattr *rxsa_nest;
2539
2540 if (!rx_sa)
2541 continue;
2542
2543 rxsa_nest = nla_nest_start(skb, k++);
2544 if (!rxsa_nest) {
2545 nla_nest_cancel(skb, rxsa_list);
2546 nla_nest_cancel(skb, rxsc_nest);
2547 nla_nest_cancel(skb, rxsc_list);
2548 goto nla_put_failure;
2549 }
2550
2551 attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
2552 if (!attr) {
2553 nla_nest_cancel(skb, rxsa_list);
2554 nla_nest_cancel(skb, rxsc_nest);
2555 nla_nest_cancel(skb, rxsc_list);
2556 goto nla_put_failure;
2557 }
2558 if (copy_rx_sa_stats(skb, rx_sa->stats)) {
2559 nla_nest_cancel(skb, attr);
2560 nla_nest_cancel(skb, rxsa_list);
2561 nla_nest_cancel(skb, rxsc_nest);
2562 nla_nest_cancel(skb, rxsc_list);
2563 goto nla_put_failure;
2564 }
2565 nla_nest_end(skb, attr);
2566
2567 if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
2568 nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
Sabrina Dubroca8acca6a2016-05-07 20:19:29 +02002569 nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002570 nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
2571 nla_nest_cancel(skb, rxsa_nest);
2572 nla_nest_cancel(skb, rxsc_nest);
2573 nla_nest_cancel(skb, rxsc_list);
2574 goto nla_put_failure;
2575 }
2576 nla_nest_end(skb, rxsa_nest);
2577 }
2578
2579 nla_nest_end(skb, rxsa_list);
2580 nla_nest_end(skb, rxsc_nest);
2581 }
2582
2583 nla_nest_end(skb, rxsc_list);
2584
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002585 genlmsg_end(skb, hdr);
2586
2587 return 0;
2588
2589nla_put_failure:
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002590 genlmsg_cancel(skb, hdr);
2591 return -EMSGSIZE;
2592}
2593
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02002594static int macsec_generation = 1; /* protected by RTNL */
2595
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002596static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
2597{
2598 struct net *net = sock_net(skb->sk);
2599 struct net_device *dev;
2600 int dev_idx, d;
2601
2602 dev_idx = cb->args[0];
2603
2604 d = 0;
Sabrina Dubrocac10c63e2016-04-22 11:28:02 +02002605 rtnl_lock();
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02002606
2607 cb->seq = macsec_generation;
2608
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002609 for_each_netdev(net, dev) {
2610 struct macsec_secy *secy;
2611
2612 if (d < dev_idx)
2613 goto next;
2614
2615 if (!netif_is_macsec(dev))
2616 goto next;
2617
2618 secy = &macsec_priv(dev)->secy;
2619 if (dump_secy(secy, dev, skb, cb) < 0)
2620 goto done;
2621next:
2622 d++;
2623 }
2624
2625done:
Sabrina Dubrocac10c63e2016-04-22 11:28:02 +02002626 rtnl_unlock();
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002627 cb->args[0] = d;
2628 return skb->len;
2629}
2630
2631static const struct genl_ops macsec_genl_ops[] = {
2632 {
2633 .cmd = MACSEC_CMD_GET_TXSC,
2634 .dumpit = macsec_dump_txsc,
2635 .policy = macsec_genl_policy,
2636 },
2637 {
2638 .cmd = MACSEC_CMD_ADD_RXSC,
2639 .doit = macsec_add_rxsc,
2640 .policy = macsec_genl_policy,
2641 .flags = GENL_ADMIN_PERM,
2642 },
2643 {
2644 .cmd = MACSEC_CMD_DEL_RXSC,
2645 .doit = macsec_del_rxsc,
2646 .policy = macsec_genl_policy,
2647 .flags = GENL_ADMIN_PERM,
2648 },
2649 {
2650 .cmd = MACSEC_CMD_UPD_RXSC,
2651 .doit = macsec_upd_rxsc,
2652 .policy = macsec_genl_policy,
2653 .flags = GENL_ADMIN_PERM,
2654 },
2655 {
2656 .cmd = MACSEC_CMD_ADD_TXSA,
2657 .doit = macsec_add_txsa,
2658 .policy = macsec_genl_policy,
2659 .flags = GENL_ADMIN_PERM,
2660 },
2661 {
2662 .cmd = MACSEC_CMD_DEL_TXSA,
2663 .doit = macsec_del_txsa,
2664 .policy = macsec_genl_policy,
2665 .flags = GENL_ADMIN_PERM,
2666 },
2667 {
2668 .cmd = MACSEC_CMD_UPD_TXSA,
2669 .doit = macsec_upd_txsa,
2670 .policy = macsec_genl_policy,
2671 .flags = GENL_ADMIN_PERM,
2672 },
2673 {
2674 .cmd = MACSEC_CMD_ADD_RXSA,
2675 .doit = macsec_add_rxsa,
2676 .policy = macsec_genl_policy,
2677 .flags = GENL_ADMIN_PERM,
2678 },
2679 {
2680 .cmd = MACSEC_CMD_DEL_RXSA,
2681 .doit = macsec_del_rxsa,
2682 .policy = macsec_genl_policy,
2683 .flags = GENL_ADMIN_PERM,
2684 },
2685 {
2686 .cmd = MACSEC_CMD_UPD_RXSA,
2687 .doit = macsec_upd_rxsa,
2688 .policy = macsec_genl_policy,
2689 .flags = GENL_ADMIN_PERM,
2690 },
2691};
2692
2693static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
2694 struct net_device *dev)
2695{
2696 struct macsec_dev *macsec = netdev_priv(dev);
2697 struct macsec_secy *secy = &macsec->secy;
2698 struct pcpu_secy_stats *secy_stats;
2699 int ret, len;
2700
2701 /* 10.5 */
2702 if (!secy->protect_frames) {
2703 secy_stats = this_cpu_ptr(macsec->stats);
2704 u64_stats_update_begin(&secy_stats->syncp);
2705 secy_stats->stats.OutPktsUntagged++;
2706 u64_stats_update_end(&secy_stats->syncp);
Daniel Borkmann79c62222016-07-01 00:00:54 +02002707 skb->dev = macsec->real_dev;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002708 len = skb->len;
2709 ret = dev_queue_xmit(skb);
2710 count_tx(dev, ret, len);
2711 return ret;
2712 }
2713
2714 if (!secy->operational) {
2715 kfree_skb(skb);
2716 dev->stats.tx_dropped++;
2717 return NETDEV_TX_OK;
2718 }
2719
2720 skb = macsec_encrypt(skb, dev);
2721 if (IS_ERR(skb)) {
2722 if (PTR_ERR(skb) != -EINPROGRESS)
2723 dev->stats.tx_dropped++;
2724 return NETDEV_TX_OK;
2725 }
2726
2727 macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
2728
2729 macsec_encrypt_finish(skb, dev);
2730 len = skb->len;
2731 ret = dev_queue_xmit(skb);
2732 count_tx(dev, ret, len);
2733 return ret;
2734}
2735
2736#define MACSEC_FEATURES \
Jason A. Donenfeld3b0129d2017-04-25 19:08:18 +02002737 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
Sabrina Dubrocae2003872016-08-12 16:10:32 +02002738static struct lock_class_key macsec_netdev_addr_lock_key;
2739
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002740static int macsec_dev_init(struct net_device *dev)
2741{
2742 struct macsec_dev *macsec = macsec_priv(dev);
2743 struct net_device *real_dev = macsec->real_dev;
Paolo Abeni5491e7c2016-07-20 18:11:32 +02002744 int err;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002745
2746 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2747 if (!dev->tstats)
2748 return -ENOMEM;
2749
Paolo Abeni5491e7c2016-07-20 18:11:32 +02002750 err = gro_cells_init(&macsec->gro_cells, dev);
2751 if (err) {
2752 free_percpu(dev->tstats);
2753 return err;
2754 }
2755
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002756 dev->features = real_dev->features & MACSEC_FEATURES;
2757 dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
2758
2759 dev->needed_headroom = real_dev->needed_headroom +
2760 MACSEC_NEEDED_HEADROOM;
2761 dev->needed_tailroom = real_dev->needed_tailroom +
2762 MACSEC_NEEDED_TAILROOM;
2763
2764 if (is_zero_ether_addr(dev->dev_addr))
2765 eth_hw_addr_inherit(dev, real_dev);
2766 if (is_zero_ether_addr(dev->broadcast))
2767 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
2768
2769 return 0;
2770}
2771
2772static void macsec_dev_uninit(struct net_device *dev)
2773{
Paolo Abeni5491e7c2016-07-20 18:11:32 +02002774 struct macsec_dev *macsec = macsec_priv(dev);
2775
2776 gro_cells_destroy(&macsec->gro_cells);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002777 free_percpu(dev->tstats);
2778}
2779
2780static netdev_features_t macsec_fix_features(struct net_device *dev,
2781 netdev_features_t features)
2782{
2783 struct macsec_dev *macsec = macsec_priv(dev);
2784 struct net_device *real_dev = macsec->real_dev;
2785
Paolo Abeni5491e7c2016-07-20 18:11:32 +02002786 features &= (real_dev->features & MACSEC_FEATURES) |
2787 NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
2788 features |= NETIF_F_LLTX;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002789
2790 return features;
2791}
2792
2793static int macsec_dev_open(struct net_device *dev)
2794{
2795 struct macsec_dev *macsec = macsec_priv(dev);
2796 struct net_device *real_dev = macsec->real_dev;
2797 int err;
2798
2799 if (!(real_dev->flags & IFF_UP))
2800 return -ENETDOWN;
2801
2802 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
2875static int macsec_set_mac_address(struct net_device *dev, void *p)
2876{
2877 struct macsec_dev *macsec = macsec_priv(dev);
2878 struct net_device *real_dev = macsec->real_dev;
2879 struct sockaddr *addr = p;
2880 int err;
2881
2882 if (!is_valid_ether_addr(addr->sa_data))
2883 return -EADDRNOTAVAIL;
2884
2885 if (!(dev->flags & IFF_UP))
2886 goto out;
2887
2888 err = dev_uc_add(real_dev, addr->sa_data);
2889 if (err < 0)
2890 return err;
2891
2892 dev_uc_del(real_dev, dev->dev_addr);
2893
2894out:
2895 ether_addr_copy(dev->dev_addr, addr->sa_data);
2896 return 0;
2897}
2898
2899static int macsec_change_mtu(struct net_device *dev, int new_mtu)
2900{
2901 struct macsec_dev *macsec = macsec_priv(dev);
2902 unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
2903
2904 if (macsec->real_dev->mtu - extra < new_mtu)
2905 return -ERANGE;
2906
2907 dev->mtu = new_mtu;
2908
2909 return 0;
2910}
2911
2912static struct rtnl_link_stats64 *macsec_get_stats64(struct net_device *dev,
2913 struct rtnl_link_stats64 *s)
2914{
2915 int cpu;
2916
2917 if (!dev->tstats)
2918 return s;
2919
2920 for_each_possible_cpu(cpu) {
2921 struct pcpu_sw_netstats *stats;
2922 struct pcpu_sw_netstats tmp;
2923 int start;
2924
2925 stats = per_cpu_ptr(dev->tstats, cpu);
2926 do {
2927 start = u64_stats_fetch_begin_irq(&stats->syncp);
2928 tmp.rx_packets = stats->rx_packets;
2929 tmp.rx_bytes = stats->rx_bytes;
2930 tmp.tx_packets = stats->tx_packets;
2931 tmp.tx_bytes = stats->tx_bytes;
2932 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
2933
2934 s->rx_packets += tmp.rx_packets;
2935 s->rx_bytes += tmp.rx_bytes;
2936 s->tx_packets += tmp.tx_packets;
2937 s->tx_bytes += tmp.tx_bytes;
2938 }
2939
2940 s->rx_dropped = dev->stats.rx_dropped;
2941 s->tx_dropped = dev->stats.tx_dropped;
2942
2943 return s;
2944}
2945
2946static int macsec_get_iflink(const struct net_device *dev)
2947{
2948 return macsec_priv(dev)->real_dev->ifindex;
2949}
2950
Sabrina Dubrocae2003872016-08-12 16:10:32 +02002951
2952static int macsec_get_nest_level(struct net_device *dev)
2953{
2954 return macsec_priv(dev)->nest_level;
2955}
2956
2957
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002958static const struct net_device_ops macsec_netdev_ops = {
2959 .ndo_init = macsec_dev_init,
2960 .ndo_uninit = macsec_dev_uninit,
2961 .ndo_open = macsec_dev_open,
2962 .ndo_stop = macsec_dev_stop,
2963 .ndo_fix_features = macsec_fix_features,
2964 .ndo_change_mtu = macsec_change_mtu,
2965 .ndo_set_rx_mode = macsec_dev_set_rx_mode,
2966 .ndo_change_rx_flags = macsec_dev_change_rx_flags,
2967 .ndo_set_mac_address = macsec_set_mac_address,
2968 .ndo_start_xmit = macsec_start_xmit,
2969 .ndo_get_stats64 = macsec_get_stats64,
2970 .ndo_get_iflink = macsec_get_iflink,
Sabrina Dubrocae2003872016-08-12 16:10:32 +02002971 .ndo_get_lock_subclass = macsec_get_nest_level,
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01002972};
2973
2974static const struct device_type macsec_type = {
2975 .name = "macsec",
2976};
2977
2978static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
2979 [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
2980 [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
2981 [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
2982 [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
2983 [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
2984 [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
2985 [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
2986 [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
2987 [IFLA_MACSEC_ES] = { .type = NLA_U8 },
2988 [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
2989 [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
2990 [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
2991};
2992
2993static void macsec_free_netdev(struct net_device *dev)
2994{
2995 struct macsec_dev *macsec = macsec_priv(dev);
2996 struct net_device *real_dev = macsec->real_dev;
2997
2998 free_percpu(macsec->stats);
2999 free_percpu(macsec->secy.tx_sc.stats);
3000
3001 dev_put(real_dev);
3002 free_netdev(dev);
3003}
3004
3005static void macsec_setup(struct net_device *dev)
3006{
3007 ether_setup(dev);
Phil Suttere4259742016-04-22 14:02:42 +02003008 dev->priv_flags |= IFF_NO_QUEUE;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003009 dev->netdev_ops = &macsec_netdev_ops;
3010 dev->destructor = macsec_free_netdev;
stephen hemmingerc24acf02016-09-07 14:07:32 -07003011 SET_NETDEV_DEVTYPE(dev, &macsec_type);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003012
3013 eth_zero_addr(dev->broadcast);
3014}
3015
3016static void macsec_changelink_common(struct net_device *dev,
3017 struct nlattr *data[])
3018{
3019 struct macsec_secy *secy;
3020 struct macsec_tx_sc *tx_sc;
3021
3022 secy = &macsec_priv(dev)->secy;
3023 tx_sc = &secy->tx_sc;
3024
3025 if (data[IFLA_MACSEC_ENCODING_SA]) {
3026 struct macsec_tx_sa *tx_sa;
3027
3028 tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
3029 tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
3030
3031 secy->operational = tx_sa && tx_sa->active;
3032 }
3033
3034 if (data[IFLA_MACSEC_WINDOW])
3035 secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
3036
3037 if (data[IFLA_MACSEC_ENCRYPT])
3038 tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
3039
3040 if (data[IFLA_MACSEC_PROTECT])
3041 secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
3042
3043 if (data[IFLA_MACSEC_INC_SCI])
3044 tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
3045
3046 if (data[IFLA_MACSEC_ES])
3047 tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
3048
3049 if (data[IFLA_MACSEC_SCB])
3050 tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
3051
3052 if (data[IFLA_MACSEC_REPLAY_PROTECT])
3053 secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
3054
3055 if (data[IFLA_MACSEC_VALIDATION])
3056 secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
3057}
3058
3059static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
3060 struct nlattr *data[])
3061{
3062 if (!data)
3063 return 0;
3064
3065 if (data[IFLA_MACSEC_CIPHER_SUITE] ||
3066 data[IFLA_MACSEC_ICV_LEN] ||
3067 data[IFLA_MACSEC_SCI] ||
3068 data[IFLA_MACSEC_PORT])
3069 return -EINVAL;
3070
3071 macsec_changelink_common(dev, data);
3072
3073 return 0;
3074}
3075
3076static void macsec_del_dev(struct macsec_dev *macsec)
3077{
3078 int i;
3079
3080 while (macsec->secy.rx_sc) {
3081 struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
3082
3083 rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
3084 free_rx_sc(rx_sc);
3085 }
3086
3087 for (i = 0; i < MACSEC_NUM_AN; i++) {
3088 struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
3089
3090 if (sa) {
3091 RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
3092 clear_tx_sa(sa);
3093 }
3094 }
3095}
3096
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003097static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
3098{
3099 struct macsec_dev *macsec = macsec_priv(dev);
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003100 struct net_device *real_dev = macsec->real_dev;
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003101
3102 unregister_netdevice_queue(dev, head);
3103 list_del_rcu(&macsec->secys);
3104 macsec_del_dev(macsec);
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003105 netdev_upper_dev_unlink(real_dev, dev);
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003106
3107 macsec_generation++;
3108}
3109
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003110static void macsec_dellink(struct net_device *dev, struct list_head *head)
3111{
3112 struct macsec_dev *macsec = macsec_priv(dev);
3113 struct net_device *real_dev = macsec->real_dev;
3114 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3115
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003116 macsec_common_dellink(dev, head);
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02003117
Sabrina Dubroca960d5842016-04-22 11:28:06 +02003118 if (list_empty(&rxd->secys)) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003119 netdev_rx_handler_unregister(real_dev);
Sabrina Dubroca960d5842016-04-22 11:28:06 +02003120 kfree(rxd);
3121 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003122}
3123
3124static int register_macsec_dev(struct net_device *real_dev,
3125 struct net_device *dev)
3126{
3127 struct macsec_dev *macsec = macsec_priv(dev);
3128 struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
3129
3130 if (!rxd) {
3131 int err;
3132
3133 rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
3134 if (!rxd)
3135 return -ENOMEM;
3136
3137 INIT_LIST_HEAD(&rxd->secys);
3138
3139 err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
3140 rxd);
Sabrina Dubroca960d5842016-04-22 11:28:06 +02003141 if (err < 0) {
3142 kfree(rxd);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003143 return err;
Sabrina Dubroca960d5842016-04-22 11:28:06 +02003144 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003145 }
3146
3147 list_add_tail_rcu(&macsec->secys, &rxd->secys);
3148 return 0;
3149}
3150
3151static bool sci_exists(struct net_device *dev, sci_t sci)
3152{
3153 struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
3154 struct macsec_dev *macsec;
3155
3156 list_for_each_entry(macsec, &rxd->secys, secys) {
3157 if (macsec->secy.sci == sci)
3158 return true;
3159 }
3160
3161 return false;
3162}
3163
3164static sci_t dev_to_sci(struct net_device *dev, __be16 port)
3165{
3166 return make_sci(dev->dev_addr, port);
3167}
3168
3169static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
3170{
3171 struct macsec_dev *macsec = macsec_priv(dev);
3172 struct macsec_secy *secy = &macsec->secy;
3173
3174 macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
3175 if (!macsec->stats)
3176 return -ENOMEM;
3177
3178 secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
3179 if (!secy->tx_sc.stats) {
3180 free_percpu(macsec->stats);
3181 return -ENOMEM;
3182 }
3183
3184 if (sci == MACSEC_UNDEF_SCI)
3185 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3186
3187 secy->netdev = dev;
3188 secy->operational = true;
3189 secy->key_len = DEFAULT_SAK_LEN;
3190 secy->icv_len = icv_len;
3191 secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
3192 secy->protect_frames = true;
3193 secy->replay_protect = false;
3194
3195 secy->sci = sci;
3196 secy->tx_sc.active = true;
3197 secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
3198 secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
3199 secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
3200 secy->tx_sc.end_station = false;
3201 secy->tx_sc.scb = false;
3202
3203 return 0;
3204}
3205
3206static int macsec_newlink(struct net *net, struct net_device *dev,
3207 struct nlattr *tb[], struct nlattr *data[])
3208{
3209 struct macsec_dev *macsec = macsec_priv(dev);
3210 struct net_device *real_dev;
3211 int err;
3212 sci_t sci;
3213 u8 icv_len = DEFAULT_ICV_LEN;
3214 rx_handler_func_t *rx_handler;
3215
3216 if (!tb[IFLA_LINK])
3217 return -EINVAL;
3218 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
3219 if (!real_dev)
3220 return -ENODEV;
3221
3222 dev->priv_flags |= IFF_MACSEC;
3223
3224 macsec->real_dev = real_dev;
3225
3226 if (data && data[IFLA_MACSEC_ICV_LEN])
3227 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
3228 dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
3229
3230 rx_handler = rtnl_dereference(real_dev->rx_handler);
3231 if (rx_handler && rx_handler != macsec_handle_frame)
3232 return -EBUSY;
3233
3234 err = register_netdevice(dev);
3235 if (err < 0)
3236 return err;
3237
Sabrina Dubroca0759e552016-07-29 15:37:55 +02003238 dev_hold(real_dev);
3239
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02003240 macsec->nest_level = dev_get_nest_level(real_dev) + 1;
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003241 netdev_lockdep_set_classes(dev);
3242 lockdep_set_class_and_subclass(&dev->addr_list_lock,
3243 &macsec_netdev_addr_lock_key,
3244 macsec_get_nest_level(dev));
3245
3246 err = netdev_upper_dev_link(real_dev, dev);
3247 if (err < 0)
3248 goto unregister;
3249
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003250 /* need to be already registered so that ->init has run and
3251 * the MAC addr is set
3252 */
3253 if (data && data[IFLA_MACSEC_SCI])
3254 sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
3255 else if (data && data[IFLA_MACSEC_PORT])
3256 sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
3257 else
3258 sci = dev_to_sci(dev, MACSEC_PORT_ES);
3259
3260 if (rx_handler && sci_exists(real_dev, sci)) {
3261 err = -EBUSY;
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003262 goto unlink;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003263 }
3264
3265 err = macsec_add_dev(dev, sci, icv_len);
3266 if (err)
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003267 goto unlink;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003268
3269 if (data)
3270 macsec_changelink_common(dev, data);
3271
3272 err = register_macsec_dev(real_dev, dev);
3273 if (err < 0)
3274 goto del_dev;
3275
Sabrina Dubroca96cfc502016-04-22 11:28:05 +02003276 macsec_generation++;
3277
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003278 return 0;
3279
3280del_dev:
3281 macsec_del_dev(macsec);
Sabrina Dubrocae2003872016-08-12 16:10:32 +02003282unlink:
3283 netdev_upper_dev_unlink(real_dev, dev);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003284unregister:
3285 unregister_netdevice(dev);
3286 return err;
3287}
3288
3289static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[])
3290{
Sabrina Dubroca74816482016-04-22 11:28:08 +02003291 u64 csid = MACSEC_DEFAULT_CIPHER_ID;
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003292 u8 icv_len = DEFAULT_ICV_LEN;
3293 int flag;
3294 bool es, scb, sci;
3295
3296 if (!data)
3297 return 0;
3298
3299 if (data[IFLA_MACSEC_CIPHER_SUITE])
3300 csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
3301
Davide Carattif04c3922016-07-22 15:07:58 +02003302 if (data[IFLA_MACSEC_ICV_LEN]) {
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003303 icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
Davide Carattif04c3922016-07-22 15:07:58 +02003304 if (icv_len != DEFAULT_ICV_LEN) {
3305 char dummy_key[DEFAULT_SAK_LEN] = { 0 };
3306 struct crypto_aead *dummy_tfm;
3307
3308 dummy_tfm = macsec_alloc_tfm(dummy_key,
3309 DEFAULT_SAK_LEN,
3310 icv_len);
3311 if (IS_ERR(dummy_tfm))
3312 return PTR_ERR(dummy_tfm);
3313 crypto_free_aead(dummy_tfm);
3314 }
3315 }
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003316
3317 switch (csid) {
Sabrina Dubroca74816482016-04-22 11:28:08 +02003318 case MACSEC_DEFAULT_CIPHER_ID:
3319 case MACSEC_DEFAULT_CIPHER_ALT:
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003320 if (icv_len < MACSEC_MIN_ICV_LEN ||
Davide Caratti2ccbe2c2016-07-22 15:07:56 +02003321 icv_len > MACSEC_STD_ICV_LEN)
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003322 return -EINVAL;
3323 break;
3324 default:
3325 return -EINVAL;
3326 }
3327
3328 if (data[IFLA_MACSEC_ENCODING_SA]) {
3329 if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
3330 return -EINVAL;
3331 }
3332
3333 for (flag = IFLA_MACSEC_ENCODING_SA + 1;
3334 flag < IFLA_MACSEC_VALIDATION;
3335 flag++) {
3336 if (data[flag]) {
3337 if (nla_get_u8(data[flag]) > 1)
3338 return -EINVAL;
3339 }
3340 }
3341
3342 es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
3343 sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
3344 scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
3345
3346 if ((sci && (scb || es)) || (scb && es))
3347 return -EINVAL;
3348
3349 if (data[IFLA_MACSEC_VALIDATION] &&
3350 nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
3351 return -EINVAL;
3352
Sabrina Dubroca4b1fb932016-04-22 11:28:09 +02003353 if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
3354 nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003355 !data[IFLA_MACSEC_WINDOW])
3356 return -EINVAL;
3357
3358 return 0;
3359}
3360
3361static struct net *macsec_get_link_net(const struct net_device *dev)
3362{
3363 return dev_net(macsec_priv(dev)->real_dev);
3364}
3365
3366static size_t macsec_get_size(const struct net_device *dev)
3367{
3368 return 0 +
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02003369 nla_total_size_64bit(8) + /* SCI */
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003370 nla_total_size(1) + /* ICV_LEN */
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02003371 nla_total_size_64bit(8) + /* CIPHER_SUITE */
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003372 nla_total_size(4) + /* WINDOW */
3373 nla_total_size(1) + /* ENCODING_SA */
3374 nla_total_size(1) + /* ENCRYPT */
3375 nla_total_size(1) + /* PROTECT */
3376 nla_total_size(1) + /* INC_SCI */
3377 nla_total_size(1) + /* ES */
3378 nla_total_size(1) + /* SCB */
3379 nla_total_size(1) + /* REPLAY_PROTECT */
3380 nla_total_size(1) + /* VALIDATION */
3381 0;
3382}
3383
3384static int macsec_fill_info(struct sk_buff *skb,
3385 const struct net_device *dev)
3386{
3387 struct macsec_secy *secy = &macsec_priv(dev)->secy;
3388 struct macsec_tx_sc *tx_sc = &secy->tx_sc;
3389
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02003390 if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
3391 IFLA_MACSEC_PAD) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003392 nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
Nicolas Dichtelf60d94c2016-04-26 10:06:11 +02003393 nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
David S. Millerc0cc5312016-04-27 15:43:10 -04003394 MACSEC_DEFAULT_CIPHER_ID, IFLA_MACSEC_PAD) ||
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003395 nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
3396 nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
3397 nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
3398 nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
3399 nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
3400 nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
3401 nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
3402 nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
3403 0)
3404 goto nla_put_failure;
3405
3406 if (secy->replay_protect) {
3407 if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
3408 goto nla_put_failure;
3409 }
3410
3411 return 0;
3412
3413nla_put_failure:
3414 return -EMSGSIZE;
3415}
3416
3417static struct rtnl_link_ops macsec_link_ops __read_mostly = {
3418 .kind = "macsec",
3419 .priv_size = sizeof(struct macsec_dev),
3420 .maxtype = IFLA_MACSEC_MAX,
3421 .policy = macsec_rtnl_policy,
3422 .setup = macsec_setup,
3423 .validate = macsec_validate_attr,
3424 .newlink = macsec_newlink,
3425 .changelink = macsec_changelink,
3426 .dellink = macsec_dellink,
3427 .get_size = macsec_get_size,
3428 .fill_info = macsec_fill_info,
3429 .get_link_net = macsec_get_link_net,
3430};
3431
3432static bool is_macsec_master(struct net_device *dev)
3433{
3434 return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
3435}
3436
3437static int macsec_notify(struct notifier_block *this, unsigned long event,
3438 void *ptr)
3439{
3440 struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
3441 LIST_HEAD(head);
3442
3443 if (!is_macsec_master(real_dev))
3444 return NOTIFY_DONE;
3445
3446 switch (event) {
3447 case NETDEV_UNREGISTER: {
3448 struct macsec_dev *m, *n;
3449 struct macsec_rxh_data *rxd;
3450
3451 rxd = macsec_data_rtnl(real_dev);
3452 list_for_each_entry_safe(m, n, &rxd->secys, secys) {
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003453 macsec_common_dellink(m->secy.netdev, &head);
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003454 }
Sabrina Dubrocabbe11fa2016-08-11 15:24:27 +02003455
3456 netdev_rx_handler_unregister(real_dev);
3457 kfree(rxd);
3458
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003459 unregister_netdevice_many(&head);
3460 break;
3461 }
3462 case NETDEV_CHANGEMTU: {
3463 struct macsec_dev *m;
3464 struct macsec_rxh_data *rxd;
3465
3466 rxd = macsec_data_rtnl(real_dev);
3467 list_for_each_entry(m, &rxd->secys, secys) {
3468 struct net_device *dev = m->secy.netdev;
3469 unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
3470 macsec_extra_len(true));
3471
3472 if (dev->mtu > mtu)
3473 dev_set_mtu(dev, mtu);
3474 }
3475 }
3476 }
3477
3478 return NOTIFY_OK;
3479}
3480
3481static struct notifier_block macsec_notifier = {
3482 .notifier_call = macsec_notify,
3483};
3484
3485static int __init macsec_init(void)
3486{
3487 int err;
3488
3489 pr_info("MACsec IEEE 802.1AE\n");
3490 err = register_netdevice_notifier(&macsec_notifier);
3491 if (err)
3492 return err;
3493
3494 err = rtnl_link_register(&macsec_link_ops);
3495 if (err)
3496 goto notifier;
3497
3498 err = genl_register_family_with_ops(&macsec_fam, macsec_genl_ops);
3499 if (err)
3500 goto rtnl;
3501
3502 return 0;
3503
3504rtnl:
3505 rtnl_link_unregister(&macsec_link_ops);
3506notifier:
3507 unregister_netdevice_notifier(&macsec_notifier);
3508 return err;
3509}
3510
3511static void __exit macsec_exit(void)
3512{
3513 genl_unregister_family(&macsec_fam);
3514 rtnl_link_unregister(&macsec_link_ops);
3515 unregister_netdevice_notifier(&macsec_notifier);
Sabrina Dubrocab196c22a2016-06-14 15:25:14 +02003516 rcu_barrier();
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003517}
3518
3519module_init(macsec_init);
3520module_exit(macsec_exit);
3521
3522MODULE_ALIAS_RTNL_LINK("macsec");
Sabrina Dubroca4b4a1942017-08-22 15:36:08 +02003523MODULE_ALIAS_GENL_FAMILY("macsec");
Sabrina Dubrocac09440f2016-03-11 18:07:33 +01003524
3525MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
3526MODULE_LICENSE("GPL v2");