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