1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/net/macsec.c - MACsec device
5 * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
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>
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>
26 #include <uapi/linux/if_macsec.h>
28 /* SecTAG length = macsec_eth_header without the optional SCI */
29 #define MACSEC_TAG_LEN 6
31 struct macsec_eth_header
{
35 #if defined(__LITTLE_ENDIAN_BITFIELD)
38 #elif defined(__BIG_ENDIAN_BITFIELD)
42 #error "Please fix <asm/byteorder.h>"
45 u8 secure_channel_id
[8]; /* optional */
48 /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
49 #define MIN_NON_SHORT_LEN 48
51 #define GCM_AES_IV_LEN 12
53 #define for_each_rxsc(secy, sc) \
54 for (sc = rcu_dereference_bh(secy->rx_sc); \
56 sc = rcu_dereference_bh(sc->next))
57 #define for_each_rxsc_rtnl(secy, sc) \
58 for (sc = rtnl_dereference(secy->rx_sc); \
60 sc = rtnl_dereference(sc->next))
62 #define pn_same_half(pn1, pn2) (!(((pn1) >> 31) ^ ((pn2) >> 31)))
66 u8 short_secure_channel_id
[4];
74 u8 secure_channel_id
[8];
80 #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
82 struct pcpu_secy_stats
{
83 struct macsec_dev_stats stats
;
84 struct u64_stats_sync syncp
;
88 * struct macsec_dev - private data
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 * @insert_tx_tag: when offloading, device requires to insert an
100 struct macsec_secy secy
;
101 struct net_device
*real_dev
;
102 netdevice_tracker dev_tracker
;
103 struct pcpu_secy_stats __percpu
*stats
;
104 struct list_head secys
;
105 struct gro_cells gro_cells
;
106 enum macsec_offload offload
;
111 * struct macsec_rxh_data - rx_handler private argument
112 * @secys: linked list of SecY's on this underlying device
114 struct macsec_rxh_data
{
115 struct list_head secys
;
118 static struct macsec_dev
*macsec_priv(const struct net_device
*dev
)
120 return (struct macsec_dev
*)netdev_priv(dev
);
123 static struct macsec_rxh_data
*macsec_data_rcu(const struct net_device
*dev
)
125 return rcu_dereference_bh(dev
->rx_handler_data
);
128 static struct macsec_rxh_data
*macsec_data_rtnl(const struct net_device
*dev
)
130 return rtnl_dereference(dev
->rx_handler_data
);
134 struct aead_request
*req
;
136 struct macsec_tx_sa
*tx_sa
;
137 struct macsec_rx_sa
*rx_sa
;
144 static struct macsec_rx_sa
*macsec_rxsa_get(struct macsec_rx_sa __rcu
*ptr
)
146 struct macsec_rx_sa
*sa
= rcu_dereference_bh(ptr
);
148 if (!sa
|| !sa
->active
)
151 if (!refcount_inc_not_zero(&sa
->refcnt
))
157 static void free_rx_sc_rcu(struct rcu_head
*head
)
159 struct macsec_rx_sc
*rx_sc
= container_of(head
, struct macsec_rx_sc
, rcu_head
);
161 free_percpu(rx_sc
->stats
);
165 static struct macsec_rx_sc
*macsec_rxsc_get(struct macsec_rx_sc
*sc
)
167 return refcount_inc_not_zero(&sc
->refcnt
) ? sc
: NULL
;
170 static void macsec_rxsc_put(struct macsec_rx_sc
*sc
)
172 if (refcount_dec_and_test(&sc
->refcnt
))
173 call_rcu(&sc
->rcu_head
, free_rx_sc_rcu
);
176 static void free_rxsa(struct rcu_head
*head
)
178 struct macsec_rx_sa
*sa
= container_of(head
, struct macsec_rx_sa
, rcu
);
180 crypto_free_aead(sa
->key
.tfm
);
181 free_percpu(sa
->stats
);
185 static void macsec_rxsa_put(struct macsec_rx_sa
*sa
)
187 if (refcount_dec_and_test(&sa
->refcnt
))
188 call_rcu(&sa
->rcu
, free_rxsa
);
191 static struct macsec_tx_sa
*macsec_txsa_get(struct macsec_tx_sa __rcu
*ptr
)
193 struct macsec_tx_sa
*sa
= rcu_dereference_bh(ptr
);
195 if (!sa
|| !sa
->active
)
198 if (!refcount_inc_not_zero(&sa
->refcnt
))
204 static void free_txsa(struct rcu_head
*head
)
206 struct macsec_tx_sa
*sa
= container_of(head
, struct macsec_tx_sa
, rcu
);
208 crypto_free_aead(sa
->key
.tfm
);
209 free_percpu(sa
->stats
);
213 static void macsec_txsa_put(struct macsec_tx_sa
*sa
)
215 if (refcount_dec_and_test(&sa
->refcnt
))
216 call_rcu(&sa
->rcu
, free_txsa
);
219 static struct macsec_cb
*macsec_skb_cb(struct sk_buff
*skb
)
221 BUILD_BUG_ON(sizeof(struct macsec_cb
) > sizeof(skb
->cb
));
222 return (struct macsec_cb
*)skb
->cb
;
225 #define MACSEC_PORT_SCB (0x0000)
226 #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
227 #define MACSEC_UNDEF_SSCI ((__force ssci_t)0xffffffff)
229 #define MACSEC_GCM_AES_128_SAK_LEN 16
230 #define MACSEC_GCM_AES_256_SAK_LEN 32
232 #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
233 #define DEFAULT_XPN false
234 #define DEFAULT_SEND_SCI true
235 #define DEFAULT_ENCRYPT false
236 #define DEFAULT_ENCODING_SA 0
237 #define MACSEC_XPN_MAX_REPLAY_WINDOW (((1 << 30) - 1))
239 static sci_t
make_sci(const u8
*addr
, __be16 port
)
243 memcpy(&sci
, addr
, ETH_ALEN
);
244 memcpy(((char *)&sci
) + ETH_ALEN
, &port
, sizeof(port
));
249 static sci_t
macsec_frame_sci(struct macsec_eth_header
*hdr
, bool sci_present
)
254 memcpy(&sci
, hdr
->secure_channel_id
,
255 sizeof(hdr
->secure_channel_id
));
257 sci
= make_sci(hdr
->eth
.h_source
, MACSEC_PORT_ES
);
262 static unsigned int macsec_sectag_len(bool sci_present
)
264 return MACSEC_TAG_LEN
+ (sci_present
? MACSEC_SCI_LEN
: 0);
267 static unsigned int macsec_hdr_len(bool sci_present
)
269 return macsec_sectag_len(sci_present
) + ETH_HLEN
;
272 static unsigned int macsec_extra_len(bool sci_present
)
274 return macsec_sectag_len(sci_present
) + sizeof(__be16
);
277 /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
278 static void macsec_fill_sectag(struct macsec_eth_header
*h
,
279 const struct macsec_secy
*secy
, u32 pn
,
282 const struct macsec_tx_sc
*tx_sc
= &secy
->tx_sc
;
284 memset(&h
->tci_an
, 0, macsec_sectag_len(sci_present
));
285 h
->eth
.h_proto
= htons(ETH_P_MACSEC
);
288 h
->tci_an
|= MACSEC_TCI_SC
;
289 memcpy(&h
->secure_channel_id
, &secy
->sci
,
290 sizeof(h
->secure_channel_id
));
292 if (tx_sc
->end_station
)
293 h
->tci_an
|= MACSEC_TCI_ES
;
295 h
->tci_an
|= MACSEC_TCI_SCB
;
298 h
->packet_number
= htonl(pn
);
300 /* with GCM, C/E clear for !encrypt, both set for encrypt */
302 h
->tci_an
|= MACSEC_TCI_CONFID
;
303 else if (secy
->icv_len
!= MACSEC_DEFAULT_ICV_LEN
)
304 h
->tci_an
|= MACSEC_TCI_C
;
306 h
->tci_an
|= tx_sc
->encoding_sa
;
309 static void macsec_set_shortlen(struct macsec_eth_header
*h
, size_t data_len
)
311 if (data_len
< MIN_NON_SHORT_LEN
)
312 h
->short_length
= data_len
;
315 /* Checks if a MACsec interface is being offloaded to an hardware engine */
316 static bool macsec_is_offloaded(struct macsec_dev
*macsec
)
318 if (macsec
->offload
== MACSEC_OFFLOAD_MAC
||
319 macsec
->offload
== MACSEC_OFFLOAD_PHY
)
325 /* Checks if underlying layers implement MACsec offloading functions. */
326 static bool macsec_check_offload(enum macsec_offload offload
,
327 struct macsec_dev
*macsec
)
329 if (!macsec
|| !macsec
->real_dev
)
332 if (offload
== MACSEC_OFFLOAD_PHY
)
333 return macsec
->real_dev
->phydev
&&
334 macsec
->real_dev
->phydev
->macsec_ops
;
335 else if (offload
== MACSEC_OFFLOAD_MAC
)
336 return macsec
->real_dev
->features
& NETIF_F_HW_MACSEC
&&
337 macsec
->real_dev
->macsec_ops
;
342 static const struct macsec_ops
*__macsec_get_ops(enum macsec_offload offload
,
343 struct macsec_dev
*macsec
,
344 struct macsec_context
*ctx
)
347 memset(ctx
, 0, sizeof(*ctx
));
348 ctx
->offload
= offload
;
350 if (offload
== MACSEC_OFFLOAD_PHY
)
351 ctx
->phydev
= macsec
->real_dev
->phydev
;
352 else if (offload
== MACSEC_OFFLOAD_MAC
)
353 ctx
->netdev
= macsec
->real_dev
;
356 if (offload
== MACSEC_OFFLOAD_PHY
)
357 return macsec
->real_dev
->phydev
->macsec_ops
;
359 return macsec
->real_dev
->macsec_ops
;
362 /* Returns a pointer to the MACsec ops struct if any and updates the MACsec
363 * context device reference if provided.
365 static const struct macsec_ops
*macsec_get_ops(struct macsec_dev
*macsec
,
366 struct macsec_context
*ctx
)
368 if (!macsec_check_offload(macsec
->offload
, macsec
))
371 return __macsec_get_ops(macsec
->offload
, macsec
, ctx
);
374 /* validate MACsec packet according to IEEE 802.1AE-2018 9.12 */
375 static bool macsec_validate_skb(struct sk_buff
*skb
, u16 icv_len
, bool xpn
)
377 struct macsec_eth_header
*h
= (struct macsec_eth_header
*)skb
->data
;
378 int len
= skb
->len
- 2 * ETH_ALEN
;
379 int extra_len
= macsec_extra_len(!!(h
->tci_an
& MACSEC_TCI_SC
)) + icv_len
;
381 /* a) It comprises at least 17 octets */
385 /* b) MACsec EtherType: already checked */
387 /* c) V bit is clear */
388 if (h
->tci_an
& MACSEC_TCI_VERSION
)
391 /* d) ES or SCB => !SC */
392 if ((h
->tci_an
& MACSEC_TCI_ES
|| h
->tci_an
& MACSEC_TCI_SCB
) &&
393 (h
->tci_an
& MACSEC_TCI_SC
))
396 /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
400 /* rx.pn != 0 if not XPN (figure 10-5 with 802.11AEbw-2013 amendment) */
401 if (!h
->packet_number
&& !xpn
)
404 /* length check, f) g) h) i) */
406 return len
== extra_len
+ h
->short_length
;
407 return len
>= extra_len
+ MIN_NON_SHORT_LEN
;
410 #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
411 #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
413 static void macsec_fill_iv_xpn(unsigned char *iv
, ssci_t ssci
, u64 pn
,
416 struct gcm_iv_xpn
*gcm_iv
= (struct gcm_iv_xpn
*)iv
;
418 gcm_iv
->ssci
= ssci
^ salt
.ssci
;
419 gcm_iv
->pn
= cpu_to_be64(pn
) ^ salt
.pn
;
422 static void macsec_fill_iv(unsigned char *iv
, sci_t sci
, u32 pn
)
424 struct gcm_iv
*gcm_iv
= (struct gcm_iv
*)iv
;
427 gcm_iv
->pn
= htonl(pn
);
430 static struct macsec_eth_header
*macsec_ethhdr(struct sk_buff
*skb
)
432 return (struct macsec_eth_header
*)skb_mac_header(skb
);
435 static void __macsec_pn_wrapped(struct macsec_secy
*secy
,
436 struct macsec_tx_sa
*tx_sa
)
438 pr_debug("PN wrapped, transitioning to !oper\n");
439 tx_sa
->active
= false;
440 if (secy
->protect_frames
)
441 secy
->operational
= false;
444 void macsec_pn_wrapped(struct macsec_secy
*secy
, struct macsec_tx_sa
*tx_sa
)
446 spin_lock_bh(&tx_sa
->lock
);
447 __macsec_pn_wrapped(secy
, tx_sa
);
448 spin_unlock_bh(&tx_sa
->lock
);
450 EXPORT_SYMBOL_GPL(macsec_pn_wrapped
);
452 static pn_t
tx_sa_update_pn(struct macsec_tx_sa
*tx_sa
,
453 struct macsec_secy
*secy
)
457 spin_lock_bh(&tx_sa
->lock
);
459 pn
= tx_sa
->next_pn_halves
;
463 tx_sa
->next_pn_halves
.lower
++;
465 if (tx_sa
->next_pn
== 0)
466 __macsec_pn_wrapped(secy
, tx_sa
);
467 spin_unlock_bh(&tx_sa
->lock
);
472 static void macsec_encrypt_finish(struct sk_buff
*skb
, struct net_device
*dev
)
474 struct macsec_dev
*macsec
= netdev_priv(dev
);
476 skb
->dev
= macsec
->real_dev
;
477 skb_reset_mac_header(skb
);
478 skb
->protocol
= eth_hdr(skb
)->h_proto
;
481 static unsigned int macsec_msdu_len(struct sk_buff
*skb
)
483 struct macsec_dev
*macsec
= macsec_priv(skb
->dev
);
484 struct macsec_secy
*secy
= &macsec
->secy
;
485 bool sci_present
= macsec_skb_cb(skb
)->has_sci
;
487 return skb
->len
- macsec_hdr_len(sci_present
) - secy
->icv_len
;
490 static void macsec_count_tx(struct sk_buff
*skb
, struct macsec_tx_sc
*tx_sc
,
491 struct macsec_tx_sa
*tx_sa
)
493 unsigned int msdu_len
= macsec_msdu_len(skb
);
494 struct pcpu_tx_sc_stats
*txsc_stats
= this_cpu_ptr(tx_sc
->stats
);
496 u64_stats_update_begin(&txsc_stats
->syncp
);
497 if (tx_sc
->encrypt
) {
498 txsc_stats
->stats
.OutOctetsEncrypted
+= msdu_len
;
499 txsc_stats
->stats
.OutPktsEncrypted
++;
500 this_cpu_inc(tx_sa
->stats
->OutPktsEncrypted
);
502 txsc_stats
->stats
.OutOctetsProtected
+= msdu_len
;
503 txsc_stats
->stats
.OutPktsProtected
++;
504 this_cpu_inc(tx_sa
->stats
->OutPktsProtected
);
506 u64_stats_update_end(&txsc_stats
->syncp
);
509 static void count_tx(struct net_device
*dev
, int ret
, int len
)
511 if (likely(ret
== NET_XMIT_SUCCESS
|| ret
== NET_XMIT_CN
))
512 dev_sw_netstats_tx_add(dev
, 1, len
);
515 static void macsec_encrypt_done(void *data
, int err
)
517 struct sk_buff
*skb
= data
;
518 struct net_device
*dev
= skb
->dev
;
519 struct macsec_dev
*macsec
= macsec_priv(dev
);
520 struct macsec_tx_sa
*sa
= macsec_skb_cb(skb
)->tx_sa
;
523 aead_request_free(macsec_skb_cb(skb
)->req
);
526 macsec_count_tx(skb
, &macsec
->secy
.tx_sc
, macsec_skb_cb(skb
)->tx_sa
);
527 /* packet is encrypted/protected so tx_bytes must be calculated */
528 len
= macsec_msdu_len(skb
) + 2 * ETH_ALEN
;
529 macsec_encrypt_finish(skb
, dev
);
530 ret
= dev_queue_xmit(skb
);
531 count_tx(dev
, ret
, len
);
532 rcu_read_unlock_bh();
538 static struct aead_request
*macsec_alloc_req(struct crypto_aead
*tfm
,
540 struct scatterlist
**sg
,
543 size_t size
, iv_offset
, sg_offset
;
544 struct aead_request
*req
;
547 size
= sizeof(struct aead_request
) + crypto_aead_reqsize(tfm
);
549 size
+= GCM_AES_IV_LEN
;
551 size
= ALIGN(size
, __alignof__(struct scatterlist
));
553 size
+= sizeof(struct scatterlist
) * num_frags
;
555 tmp
= kmalloc(size
, GFP_ATOMIC
);
559 *iv
= (unsigned char *)(tmp
+ iv_offset
);
560 *sg
= (struct scatterlist
*)(tmp
+ sg_offset
);
563 aead_request_set_tfm(req
, tfm
);
568 static struct sk_buff
*macsec_encrypt(struct sk_buff
*skb
,
569 struct net_device
*dev
)
572 struct scatterlist
*sg
;
573 struct sk_buff
*trailer
;
576 struct macsec_eth_header
*hh
;
577 size_t unprotected_len
;
578 struct aead_request
*req
;
579 struct macsec_secy
*secy
;
580 struct macsec_tx_sc
*tx_sc
;
581 struct macsec_tx_sa
*tx_sa
;
582 struct macsec_dev
*macsec
= macsec_priv(dev
);
586 secy
= &macsec
->secy
;
587 tx_sc
= &secy
->tx_sc
;
589 /* 10.5.1 TX SA assignment */
590 tx_sa
= macsec_txsa_get(tx_sc
->sa
[tx_sc
->encoding_sa
]);
592 secy
->operational
= false;
594 return ERR_PTR(-EINVAL
);
597 if (unlikely(skb_headroom(skb
) < MACSEC_NEEDED_HEADROOM
||
598 skb_tailroom(skb
) < MACSEC_NEEDED_TAILROOM
)) {
599 struct sk_buff
*nskb
= skb_copy_expand(skb
,
600 MACSEC_NEEDED_HEADROOM
,
601 MACSEC_NEEDED_TAILROOM
,
607 macsec_txsa_put(tx_sa
);
609 return ERR_PTR(-ENOMEM
);
612 skb
= skb_unshare(skb
, GFP_ATOMIC
);
614 macsec_txsa_put(tx_sa
);
615 return ERR_PTR(-ENOMEM
);
619 unprotected_len
= skb
->len
;
621 sci_present
= macsec_send_sci(secy
);
622 hh
= skb_push(skb
, macsec_extra_len(sci_present
));
623 memmove(hh
, eth
, 2 * ETH_ALEN
);
625 pn
= tx_sa_update_pn(tx_sa
, secy
);
626 if (pn
.full64
== 0) {
627 macsec_txsa_put(tx_sa
);
629 return ERR_PTR(-ENOLINK
);
631 macsec_fill_sectag(hh
, secy
, pn
.lower
, sci_present
);
632 macsec_set_shortlen(hh
, unprotected_len
- 2 * ETH_ALEN
);
634 skb_put(skb
, secy
->icv_len
);
636 if (skb
->len
- ETH_HLEN
> macsec_priv(dev
)->real_dev
->mtu
) {
637 struct pcpu_secy_stats
*secy_stats
= this_cpu_ptr(macsec
->stats
);
639 u64_stats_update_begin(&secy_stats
->syncp
);
640 secy_stats
->stats
.OutPktsTooLong
++;
641 u64_stats_update_end(&secy_stats
->syncp
);
643 macsec_txsa_put(tx_sa
);
645 return ERR_PTR(-EINVAL
);
648 ret
= skb_cow_data(skb
, 0, &trailer
);
649 if (unlikely(ret
< 0)) {
650 macsec_txsa_put(tx_sa
);
655 req
= macsec_alloc_req(tx_sa
->key
.tfm
, &iv
, &sg
, ret
);
657 macsec_txsa_put(tx_sa
);
659 return ERR_PTR(-ENOMEM
);
663 macsec_fill_iv_xpn(iv
, tx_sa
->ssci
, pn
.full64
, tx_sa
->key
.salt
);
665 macsec_fill_iv(iv
, secy
->sci
, pn
.lower
);
667 sg_init_table(sg
, ret
);
668 ret
= skb_to_sgvec(skb
, sg
, 0, skb
->len
);
669 if (unlikely(ret
< 0)) {
670 aead_request_free(req
);
671 macsec_txsa_put(tx_sa
);
676 if (tx_sc
->encrypt
) {
677 int len
= skb
->len
- macsec_hdr_len(sci_present
) -
679 aead_request_set_crypt(req
, sg
, sg
, len
, iv
);
680 aead_request_set_ad(req
, macsec_hdr_len(sci_present
));
682 aead_request_set_crypt(req
, sg
, sg
, 0, iv
);
683 aead_request_set_ad(req
, skb
->len
- secy
->icv_len
);
686 macsec_skb_cb(skb
)->req
= req
;
687 macsec_skb_cb(skb
)->tx_sa
= tx_sa
;
688 macsec_skb_cb(skb
)->has_sci
= sci_present
;
689 aead_request_set_callback(req
, 0, macsec_encrypt_done
, skb
);
692 ret
= crypto_aead_encrypt(req
);
693 if (ret
== -EINPROGRESS
) {
695 } else if (ret
!= 0) {
698 aead_request_free(req
);
699 macsec_txsa_put(tx_sa
);
700 return ERR_PTR(-EINVAL
);
704 aead_request_free(req
);
705 macsec_txsa_put(tx_sa
);
710 static bool macsec_post_decrypt(struct sk_buff
*skb
, struct macsec_secy
*secy
, u32 pn
)
712 struct macsec_rx_sa
*rx_sa
= macsec_skb_cb(skb
)->rx_sa
;
713 struct pcpu_rx_sc_stats
*rxsc_stats
= this_cpu_ptr(rx_sa
->sc
->stats
);
714 struct macsec_eth_header
*hdr
= macsec_ethhdr(skb
);
717 spin_lock(&rx_sa
->lock
);
718 if (rx_sa
->next_pn_halves
.lower
>= secy
->replay_window
)
719 lowest_pn
= rx_sa
->next_pn_halves
.lower
- secy
->replay_window
;
721 /* Now perform replay protection check again
722 * (see IEEE 802.1AE-2006 figure 10-5)
724 if (secy
->replay_protect
&& pn
< lowest_pn
&&
725 (!secy
->xpn
|| pn_same_half(pn
, lowest_pn
))) {
726 spin_unlock(&rx_sa
->lock
);
727 u64_stats_update_begin(&rxsc_stats
->syncp
);
728 rxsc_stats
->stats
.InPktsLate
++;
729 u64_stats_update_end(&rxsc_stats
->syncp
);
730 DEV_STATS_INC(secy
->netdev
, rx_dropped
);
734 if (secy
->validate_frames
!= MACSEC_VALIDATE_DISABLED
) {
735 unsigned int msdu_len
= macsec_msdu_len(skb
);
736 u64_stats_update_begin(&rxsc_stats
->syncp
);
737 if (hdr
->tci_an
& MACSEC_TCI_E
)
738 rxsc_stats
->stats
.InOctetsDecrypted
+= msdu_len
;
740 rxsc_stats
->stats
.InOctetsValidated
+= msdu_len
;
741 u64_stats_update_end(&rxsc_stats
->syncp
);
744 if (!macsec_skb_cb(skb
)->valid
) {
745 spin_unlock(&rx_sa
->lock
);
748 if (hdr
->tci_an
& MACSEC_TCI_C
||
749 secy
->validate_frames
== MACSEC_VALIDATE_STRICT
) {
750 u64_stats_update_begin(&rxsc_stats
->syncp
);
751 rxsc_stats
->stats
.InPktsNotValid
++;
752 u64_stats_update_end(&rxsc_stats
->syncp
);
753 this_cpu_inc(rx_sa
->stats
->InPktsNotValid
);
754 DEV_STATS_INC(secy
->netdev
, rx_errors
);
758 u64_stats_update_begin(&rxsc_stats
->syncp
);
759 if (secy
->validate_frames
== MACSEC_VALIDATE_CHECK
) {
760 rxsc_stats
->stats
.InPktsInvalid
++;
761 this_cpu_inc(rx_sa
->stats
->InPktsInvalid
);
762 } else if (pn
< lowest_pn
) {
763 rxsc_stats
->stats
.InPktsDelayed
++;
765 rxsc_stats
->stats
.InPktsUnchecked
++;
767 u64_stats_update_end(&rxsc_stats
->syncp
);
769 u64_stats_update_begin(&rxsc_stats
->syncp
);
770 if (pn
< lowest_pn
) {
771 rxsc_stats
->stats
.InPktsDelayed
++;
773 rxsc_stats
->stats
.InPktsOK
++;
774 this_cpu_inc(rx_sa
->stats
->InPktsOK
);
776 u64_stats_update_end(&rxsc_stats
->syncp
);
778 // Instead of "pn >=" - to support pn overflow in xpn
779 if (pn
+ 1 > rx_sa
->next_pn_halves
.lower
) {
780 rx_sa
->next_pn_halves
.lower
= pn
+ 1;
781 } else if (secy
->xpn
&&
782 !pn_same_half(pn
, rx_sa
->next_pn_halves
.lower
)) {
783 rx_sa
->next_pn_halves
.upper
++;
784 rx_sa
->next_pn_halves
.lower
= pn
+ 1;
787 spin_unlock(&rx_sa
->lock
);
793 static void macsec_reset_skb(struct sk_buff
*skb
, struct net_device
*dev
)
795 skb
->pkt_type
= PACKET_HOST
;
796 skb
->protocol
= eth_type_trans(skb
, dev
);
798 skb_reset_network_header(skb
);
799 if (!skb_transport_header_was_set(skb
))
800 skb_reset_transport_header(skb
);
801 skb_reset_mac_len(skb
);
804 static void macsec_finalize_skb(struct sk_buff
*skb
, u8 icv_len
, u8 hdr_len
)
806 skb
->ip_summed
= CHECKSUM_NONE
;
807 memmove(skb
->data
+ hdr_len
, skb
->data
, 2 * ETH_ALEN
);
808 skb_pull(skb
, hdr_len
);
809 pskb_trim_unique(skb
, skb
->len
- icv_len
);
812 static void count_rx(struct net_device
*dev
, int len
)
814 dev_sw_netstats_rx_add(dev
, len
);
817 static void macsec_decrypt_done(void *data
, int err
)
819 struct sk_buff
*skb
= data
;
820 struct net_device
*dev
= skb
->dev
;
821 struct macsec_dev
*macsec
= macsec_priv(dev
);
822 struct macsec_rx_sa
*rx_sa
= macsec_skb_cb(skb
)->rx_sa
;
823 struct macsec_rx_sc
*rx_sc
= rx_sa
->sc
;
827 aead_request_free(macsec_skb_cb(skb
)->req
);
830 macsec_skb_cb(skb
)->valid
= true;
833 pn
= ntohl(macsec_ethhdr(skb
)->packet_number
);
834 if (!macsec_post_decrypt(skb
, &macsec
->secy
, pn
)) {
835 rcu_read_unlock_bh();
840 macsec_finalize_skb(skb
, macsec
->secy
.icv_len
,
841 macsec_extra_len(macsec_skb_cb(skb
)->has_sci
));
843 macsec_reset_skb(skb
, macsec
->secy
.netdev
);
845 if (gro_cells_receive(&macsec
->gro_cells
, skb
) == NET_RX_SUCCESS
)
848 rcu_read_unlock_bh();
851 macsec_rxsa_put(rx_sa
);
852 macsec_rxsc_put(rx_sc
);
856 static struct sk_buff
*macsec_decrypt(struct sk_buff
*skb
,
857 struct net_device
*dev
,
858 struct macsec_rx_sa
*rx_sa
,
860 struct macsec_secy
*secy
)
863 struct scatterlist
*sg
;
864 struct sk_buff
*trailer
;
866 struct aead_request
*req
;
867 struct macsec_eth_header
*hdr
;
869 u16 icv_len
= secy
->icv_len
;
871 macsec_skb_cb(skb
)->valid
= false;
872 skb
= skb_share_check(skb
, GFP_ATOMIC
);
874 return ERR_PTR(-ENOMEM
);
876 ret
= skb_cow_data(skb
, 0, &trailer
);
877 if (unlikely(ret
< 0)) {
881 req
= macsec_alloc_req(rx_sa
->key
.tfm
, &iv
, &sg
, ret
);
884 return ERR_PTR(-ENOMEM
);
887 hdr
= (struct macsec_eth_header
*)skb
->data
;
888 hdr_pn
= ntohl(hdr
->packet_number
);
891 pn_t recovered_pn
= rx_sa
->next_pn_halves
;
893 recovered_pn
.lower
= hdr_pn
;
894 if (hdr_pn
< rx_sa
->next_pn_halves
.lower
&&
895 !pn_same_half(hdr_pn
, rx_sa
->next_pn_halves
.lower
))
896 recovered_pn
.upper
++;
898 macsec_fill_iv_xpn(iv
, rx_sa
->ssci
, recovered_pn
.full64
,
901 macsec_fill_iv(iv
, sci
, hdr_pn
);
904 sg_init_table(sg
, ret
);
905 ret
= skb_to_sgvec(skb
, sg
, 0, skb
->len
);
906 if (unlikely(ret
< 0)) {
907 aead_request_free(req
);
912 if (hdr
->tci_an
& MACSEC_TCI_E
) {
913 /* confidentiality: ethernet + macsec header
914 * authenticated, encrypted payload
916 int len
= skb
->len
- macsec_hdr_len(macsec_skb_cb(skb
)->has_sci
);
918 aead_request_set_crypt(req
, sg
, sg
, len
, iv
);
919 aead_request_set_ad(req
, macsec_hdr_len(macsec_skb_cb(skb
)->has_sci
));
920 skb
= skb_unshare(skb
, GFP_ATOMIC
);
922 aead_request_free(req
);
923 return ERR_PTR(-ENOMEM
);
926 /* integrity only: all headers + data authenticated */
927 aead_request_set_crypt(req
, sg
, sg
, icv_len
, iv
);
928 aead_request_set_ad(req
, skb
->len
- icv_len
);
931 macsec_skb_cb(skb
)->req
= req
;
933 aead_request_set_callback(req
, 0, macsec_decrypt_done
, skb
);
936 ret
= crypto_aead_decrypt(req
);
937 if (ret
== -EINPROGRESS
) {
939 } else if (ret
!= 0) {
940 /* decryption/authentication failed
941 * 10.6 if validateFrames is disabled, deliver anyway
943 if (ret
!= -EBADMSG
) {
948 macsec_skb_cb(skb
)->valid
= true;
952 aead_request_free(req
);
957 static struct macsec_rx_sc
*find_rx_sc(struct macsec_secy
*secy
, sci_t sci
)
959 struct macsec_rx_sc
*rx_sc
;
961 for_each_rxsc(secy
, rx_sc
) {
962 if (rx_sc
->sci
== sci
)
969 static struct macsec_rx_sc
*find_rx_sc_rtnl(struct macsec_secy
*secy
, sci_t sci
)
971 struct macsec_rx_sc
*rx_sc
;
973 for_each_rxsc_rtnl(secy
, rx_sc
) {
974 if (rx_sc
->sci
== sci
)
981 static enum rx_handler_result
handle_not_macsec(struct sk_buff
*skb
)
983 /* Deliver to the uncontrolled port by default */
984 enum rx_handler_result ret
= RX_HANDLER_PASS
;
985 struct ethhdr
*hdr
= eth_hdr(skb
);
986 struct metadata_dst
*md_dst
;
987 struct macsec_rxh_data
*rxd
;
988 struct macsec_dev
*macsec
;
989 bool is_macsec_md_dst
;
992 rxd
= macsec_data_rcu(skb
->dev
);
993 md_dst
= skb_metadata_dst(skb
);
994 is_macsec_md_dst
= md_dst
&& md_dst
->type
== METADATA_MACSEC
;
996 list_for_each_entry_rcu(macsec
, &rxd
->secys
, secys
) {
997 struct sk_buff
*nskb
;
998 struct pcpu_secy_stats
*secy_stats
= this_cpu_ptr(macsec
->stats
);
999 struct net_device
*ndev
= macsec
->secy
.netdev
;
1001 /* If h/w offloading is enabled, HW decodes frames and strips
1002 * the SecTAG, so we have to deduce which port to deliver to.
1004 if (macsec_is_offloaded(macsec
) && netif_running(ndev
)) {
1005 const struct macsec_ops
*ops
;
1007 ops
= macsec_get_ops(macsec
, NULL
);
1009 if (ops
->rx_uses_md_dst
&& !is_macsec_md_dst
)
1012 if (is_macsec_md_dst
) {
1013 struct macsec_rx_sc
*rx_sc
;
1015 /* All drivers that implement MACsec offload
1016 * support using skb metadata destinations must
1017 * indicate that they do so.
1019 DEBUG_NET_WARN_ON_ONCE(!ops
->rx_uses_md_dst
);
1020 rx_sc
= find_rx_sc(&macsec
->secy
,
1021 md_dst
->u
.macsec_info
.sci
);
1024 /* device indicated macsec offload occurred */
1026 skb
->pkt_type
= PACKET_HOST
;
1027 eth_skb_pkt_type(skb
, ndev
);
1028 ret
= RX_HANDLER_ANOTHER
;
1032 /* This datapath is insecure because it is unable to
1033 * enforce isolation of broadcast/multicast traffic and
1034 * unicast traffic with promiscuous mode on the macsec
1035 * netdev. Since the core stack has no mechanism to
1036 * check that the hardware did indeed receive MACsec
1037 * traffic, it is possible that the response handling
1038 * done by the MACsec port was to a plaintext packet.
1039 * This violates the MACsec protocol standard.
1041 if (ether_addr_equal_64bits(hdr
->h_dest
,
1043 /* exact match, divert skb to this port */
1045 skb
->pkt_type
= PACKET_HOST
;
1046 ret
= RX_HANDLER_ANOTHER
;
1048 } else if (is_multicast_ether_addr_64bits(
1050 /* multicast frame, deliver on this port too */
1051 nskb
= skb_clone(skb
, GFP_ATOMIC
);
1056 eth_skb_pkt_type(nskb
, ndev
);
1059 } else if (ndev
->flags
& IFF_PROMISC
) {
1061 skb
->pkt_type
= PACKET_HOST
;
1062 ret
= RX_HANDLER_ANOTHER
;
1069 /* 10.6 If the management control validateFrames is not
1070 * Strict, frames without a SecTAG are received, counted, and
1071 * delivered to the Controlled Port
1073 if (macsec
->secy
.validate_frames
== MACSEC_VALIDATE_STRICT
) {
1074 u64_stats_update_begin(&secy_stats
->syncp
);
1075 secy_stats
->stats
.InPktsNoTag
++;
1076 u64_stats_update_end(&secy_stats
->syncp
);
1077 DEV_STATS_INC(macsec
->secy
.netdev
, rx_dropped
);
1081 /* deliver on this port */
1082 nskb
= skb_clone(skb
, GFP_ATOMIC
);
1088 if (__netif_rx(nskb
) == NET_RX_SUCCESS
) {
1089 u64_stats_update_begin(&secy_stats
->syncp
);
1090 secy_stats
->stats
.InPktsUntagged
++;
1091 u64_stats_update_end(&secy_stats
->syncp
);
1100 static rx_handler_result_t
macsec_handle_frame(struct sk_buff
**pskb
)
1102 struct sk_buff
*skb
= *pskb
;
1103 struct net_device
*dev
= skb
->dev
;
1104 struct macsec_eth_header
*hdr
;
1105 struct macsec_secy
*secy
= NULL
;
1106 struct macsec_rx_sc
*rx_sc
;
1107 struct macsec_rx_sa
*rx_sa
;
1108 struct macsec_rxh_data
*rxd
;
1109 struct macsec_dev
*macsec
;
1114 struct pcpu_rx_sc_stats
*rxsc_stats
;
1115 struct pcpu_secy_stats
*secy_stats
;
1119 if (skb_headroom(skb
) < ETH_HLEN
)
1122 hdr
= macsec_ethhdr(skb
);
1123 if (hdr
->eth
.h_proto
!= htons(ETH_P_MACSEC
))
1124 return handle_not_macsec(skb
);
1126 skb
= skb_unshare(skb
, GFP_ATOMIC
);
1129 return RX_HANDLER_CONSUMED
;
1131 pulled_sci
= pskb_may_pull(skb
, macsec_extra_len(true));
1133 if (!pskb_may_pull(skb
, macsec_extra_len(false)))
1137 hdr
= macsec_ethhdr(skb
);
1139 /* Frames with a SecTAG that has the TCI E bit set but the C
1140 * bit clear are discarded, as this reserved encoding is used
1141 * to identify frames with a SecTAG that are not to be
1142 * delivered to the Controlled Port.
1144 if ((hdr
->tci_an
& (MACSEC_TCI_C
| MACSEC_TCI_E
)) == MACSEC_TCI_E
)
1145 return RX_HANDLER_PASS
;
1147 /* now, pull the extra length */
1148 if (hdr
->tci_an
& MACSEC_TCI_SC
) {
1153 /* ethernet header is part of crypto processing */
1154 skb_push(skb
, ETH_HLEN
);
1156 macsec_skb_cb(skb
)->has_sci
= !!(hdr
->tci_an
& MACSEC_TCI_SC
);
1157 macsec_skb_cb(skb
)->assoc_num
= hdr
->tci_an
& MACSEC_AN_MASK
;
1158 sci
= macsec_frame_sci(hdr
, macsec_skb_cb(skb
)->has_sci
);
1161 rxd
= macsec_data_rcu(skb
->dev
);
1163 list_for_each_entry_rcu(macsec
, &rxd
->secys
, secys
) {
1164 struct macsec_rx_sc
*sc
= find_rx_sc(&macsec
->secy
, sci
);
1166 sc
= sc
? macsec_rxsc_get(sc
) : NULL
;
1169 secy
= &macsec
->secy
;
1179 macsec
= macsec_priv(dev
);
1180 secy_stats
= this_cpu_ptr(macsec
->stats
);
1181 rxsc_stats
= this_cpu_ptr(rx_sc
->stats
);
1183 if (!macsec_validate_skb(skb
, secy
->icv_len
, secy
->xpn
)) {
1184 u64_stats_update_begin(&secy_stats
->syncp
);
1185 secy_stats
->stats
.InPktsBadTag
++;
1186 u64_stats_update_end(&secy_stats
->syncp
);
1187 DEV_STATS_INC(secy
->netdev
, rx_errors
);
1191 rx_sa
= macsec_rxsa_get(rx_sc
->sa
[macsec_skb_cb(skb
)->assoc_num
]);
1193 /* 10.6.1 if the SA is not in use */
1195 /* If validateFrames is Strict or the C bit in the
1196 * SecTAG is set, discard
1198 if (hdr
->tci_an
& MACSEC_TCI_C
||
1199 secy
->validate_frames
== MACSEC_VALIDATE_STRICT
) {
1200 u64_stats_update_begin(&rxsc_stats
->syncp
);
1201 rxsc_stats
->stats
.InPktsNotUsingSA
++;
1202 u64_stats_update_end(&rxsc_stats
->syncp
);
1203 DEV_STATS_INC(secy
->netdev
, rx_errors
);
1207 /* not Strict, the frame (with the SecTAG and ICV
1208 * removed) is delivered to the Controlled Port.
1210 u64_stats_update_begin(&rxsc_stats
->syncp
);
1211 rxsc_stats
->stats
.InPktsUnusedSA
++;
1212 u64_stats_update_end(&rxsc_stats
->syncp
);
1216 /* First, PN check to avoid decrypting obviously wrong packets */
1217 hdr_pn
= ntohl(hdr
->packet_number
);
1218 if (secy
->replay_protect
) {
1221 spin_lock(&rx_sa
->lock
);
1222 late
= rx_sa
->next_pn_halves
.lower
>= secy
->replay_window
&&
1223 hdr_pn
< (rx_sa
->next_pn_halves
.lower
- secy
->replay_window
);
1226 late
= late
&& pn_same_half(rx_sa
->next_pn_halves
.lower
, hdr_pn
);
1227 spin_unlock(&rx_sa
->lock
);
1230 u64_stats_update_begin(&rxsc_stats
->syncp
);
1231 rxsc_stats
->stats
.InPktsLate
++;
1232 u64_stats_update_end(&rxsc_stats
->syncp
);
1233 DEV_STATS_INC(macsec
->secy
.netdev
, rx_dropped
);
1238 macsec_skb_cb(skb
)->rx_sa
= rx_sa
;
1240 /* Disabled && !changed text => skip validation */
1241 if (hdr
->tci_an
& MACSEC_TCI_C
||
1242 secy
->validate_frames
!= MACSEC_VALIDATE_DISABLED
)
1243 skb
= macsec_decrypt(skb
, dev
, rx_sa
, sci
, secy
);
1246 /* the decrypt callback needs the reference */
1247 if (PTR_ERR(skb
) != -EINPROGRESS
) {
1248 macsec_rxsa_put(rx_sa
);
1249 macsec_rxsc_put(rx_sc
);
1253 return RX_HANDLER_CONSUMED
;
1256 if (!macsec_post_decrypt(skb
, secy
, hdr_pn
))
1260 macsec_finalize_skb(skb
, secy
->icv_len
,
1261 macsec_extra_len(macsec_skb_cb(skb
)->has_sci
));
1263 macsec_reset_skb(skb
, secy
->netdev
);
1266 macsec_rxsa_put(rx_sa
);
1267 macsec_rxsc_put(rx_sc
);
1270 ret
= gro_cells_receive(&macsec
->gro_cells
, skb
);
1271 if (ret
== NET_RX_SUCCESS
)
1274 DEV_STATS_INC(macsec
->secy
.netdev
, rx_dropped
);
1279 return RX_HANDLER_CONSUMED
;
1282 macsec_rxsa_put(rx_sa
);
1284 macsec_rxsc_put(rx_sc
);
1289 return RX_HANDLER_CONSUMED
;
1292 /* 10.6.1 if the SC is not found */
1293 cbit
= !!(hdr
->tci_an
& MACSEC_TCI_C
);
1295 macsec_finalize_skb(skb
, MACSEC_DEFAULT_ICV_LEN
,
1296 macsec_extra_len(macsec_skb_cb(skb
)->has_sci
));
1298 list_for_each_entry_rcu(macsec
, &rxd
->secys
, secys
) {
1299 struct sk_buff
*nskb
;
1301 secy_stats
= this_cpu_ptr(macsec
->stats
);
1303 /* If validateFrames is Strict or the C bit in the
1304 * SecTAG is set, discard
1307 macsec
->secy
.validate_frames
== MACSEC_VALIDATE_STRICT
) {
1308 u64_stats_update_begin(&secy_stats
->syncp
);
1309 secy_stats
->stats
.InPktsNoSCI
++;
1310 u64_stats_update_end(&secy_stats
->syncp
);
1311 DEV_STATS_INC(macsec
->secy
.netdev
, rx_errors
);
1315 /* not strict, the frame (with the SecTAG and ICV
1316 * removed) is delivered to the Controlled Port.
1318 nskb
= skb_clone(skb
, GFP_ATOMIC
);
1322 macsec_reset_skb(nskb
, macsec
->secy
.netdev
);
1324 ret
= __netif_rx(nskb
);
1325 if (ret
== NET_RX_SUCCESS
) {
1326 u64_stats_update_begin(&secy_stats
->syncp
);
1327 secy_stats
->stats
.InPktsUnknownSCI
++;
1328 u64_stats_update_end(&secy_stats
->syncp
);
1330 DEV_STATS_INC(macsec
->secy
.netdev
, rx_dropped
);
1336 return RX_HANDLER_PASS
;
1339 static struct crypto_aead
*macsec_alloc_tfm(char *key
, int key_len
, int icv_len
)
1341 struct crypto_aead
*tfm
;
1344 tfm
= crypto_alloc_aead("gcm(aes)", 0, 0);
1349 ret
= crypto_aead_setkey(tfm
, key
, key_len
);
1353 ret
= crypto_aead_setauthsize(tfm
, icv_len
);
1359 crypto_free_aead(tfm
);
1360 return ERR_PTR(ret
);
1363 static int init_rx_sa(struct macsec_rx_sa
*rx_sa
, char *sak
, int key_len
,
1366 rx_sa
->stats
= alloc_percpu(struct macsec_rx_sa_stats
);
1370 rx_sa
->key
.tfm
= macsec_alloc_tfm(sak
, key_len
, icv_len
);
1371 if (IS_ERR(rx_sa
->key
.tfm
)) {
1372 free_percpu(rx_sa
->stats
);
1373 return PTR_ERR(rx_sa
->key
.tfm
);
1376 rx_sa
->ssci
= MACSEC_UNDEF_SSCI
;
1377 rx_sa
->active
= false;
1379 refcount_set(&rx_sa
->refcnt
, 1);
1380 spin_lock_init(&rx_sa
->lock
);
1385 static void clear_rx_sa(struct macsec_rx_sa
*rx_sa
)
1387 rx_sa
->active
= false;
1389 macsec_rxsa_put(rx_sa
);
1392 static void free_rx_sc(struct macsec_rx_sc
*rx_sc
)
1396 for (i
= 0; i
< MACSEC_NUM_AN
; i
++) {
1397 struct macsec_rx_sa
*sa
= rtnl_dereference(rx_sc
->sa
[i
]);
1399 RCU_INIT_POINTER(rx_sc
->sa
[i
], NULL
);
1404 macsec_rxsc_put(rx_sc
);
1407 static struct macsec_rx_sc
*del_rx_sc(struct macsec_secy
*secy
, sci_t sci
)
1409 struct macsec_rx_sc
*rx_sc
, __rcu
**rx_scp
;
1411 for (rx_scp
= &secy
->rx_sc
, rx_sc
= rtnl_dereference(*rx_scp
);
1413 rx_scp
= &rx_sc
->next
, rx_sc
= rtnl_dereference(*rx_scp
)) {
1414 if (rx_sc
->sci
== sci
) {
1417 rcu_assign_pointer(*rx_scp
, rx_sc
->next
);
1425 static struct macsec_rx_sc
*create_rx_sc(struct net_device
*dev
, sci_t sci
,
1428 struct macsec_rx_sc
*rx_sc
;
1429 struct macsec_dev
*macsec
;
1430 struct net_device
*real_dev
= macsec_priv(dev
)->real_dev
;
1431 struct macsec_rxh_data
*rxd
= macsec_data_rtnl(real_dev
);
1432 struct macsec_secy
*secy
;
1434 list_for_each_entry(macsec
, &rxd
->secys
, secys
) {
1435 if (find_rx_sc_rtnl(&macsec
->secy
, sci
))
1436 return ERR_PTR(-EEXIST
);
1439 rx_sc
= kzalloc(sizeof(*rx_sc
), GFP_KERNEL
);
1441 return ERR_PTR(-ENOMEM
);
1443 rx_sc
->stats
= netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats
);
1444 if (!rx_sc
->stats
) {
1446 return ERR_PTR(-ENOMEM
);
1450 rx_sc
->active
= active
;
1451 refcount_set(&rx_sc
->refcnt
, 1);
1453 secy
= &macsec_priv(dev
)->secy
;
1454 rcu_assign_pointer(rx_sc
->next
, secy
->rx_sc
);
1455 rcu_assign_pointer(secy
->rx_sc
, rx_sc
);
1463 static int init_tx_sa(struct macsec_tx_sa
*tx_sa
, char *sak
, int key_len
,
1466 tx_sa
->stats
= alloc_percpu(struct macsec_tx_sa_stats
);
1470 tx_sa
->key
.tfm
= macsec_alloc_tfm(sak
, key_len
, icv_len
);
1471 if (IS_ERR(tx_sa
->key
.tfm
)) {
1472 free_percpu(tx_sa
->stats
);
1473 return PTR_ERR(tx_sa
->key
.tfm
);
1476 tx_sa
->ssci
= MACSEC_UNDEF_SSCI
;
1477 tx_sa
->active
= false;
1478 refcount_set(&tx_sa
->refcnt
, 1);
1479 spin_lock_init(&tx_sa
->lock
);
1484 static void clear_tx_sa(struct macsec_tx_sa
*tx_sa
)
1486 tx_sa
->active
= false;
1488 macsec_txsa_put(tx_sa
);
1491 static struct genl_family macsec_fam
;
1493 static struct net_device
*get_dev_from_nl(struct net
*net
,
1494 struct nlattr
**attrs
)
1496 int ifindex
= nla_get_u32(attrs
[MACSEC_ATTR_IFINDEX
]);
1497 struct net_device
*dev
;
1499 dev
= __dev_get_by_index(net
, ifindex
);
1501 return ERR_PTR(-ENODEV
);
1503 if (!netif_is_macsec(dev
))
1504 return ERR_PTR(-ENODEV
);
1509 static enum macsec_offload
nla_get_offload(const struct nlattr
*nla
)
1511 return (__force
enum macsec_offload
)nla_get_u8(nla
);
1514 static sci_t
nla_get_sci(const struct nlattr
*nla
)
1516 return (__force sci_t
)nla_get_u64(nla
);
1519 static int nla_put_sci(struct sk_buff
*skb
, int attrtype
, sci_t value
,
1522 return nla_put_u64_64bit(skb
, attrtype
, (__force u64
)value
, padattr
);
1525 static ssci_t
nla_get_ssci(const struct nlattr
*nla
)
1527 return (__force ssci_t
)nla_get_u32(nla
);
1530 static int nla_put_ssci(struct sk_buff
*skb
, int attrtype
, ssci_t value
)
1532 return nla_put_u32(skb
, attrtype
, (__force u64
)value
);
1535 static struct macsec_tx_sa
*get_txsa_from_nl(struct net
*net
,
1536 struct nlattr
**attrs
,
1537 struct nlattr
**tb_sa
,
1538 struct net_device
**devp
,
1539 struct macsec_secy
**secyp
,
1540 struct macsec_tx_sc
**scp
,
1543 struct net_device
*dev
;
1544 struct macsec_secy
*secy
;
1545 struct macsec_tx_sc
*tx_sc
;
1546 struct macsec_tx_sa
*tx_sa
;
1548 if (!tb_sa
[MACSEC_SA_ATTR_AN
])
1549 return ERR_PTR(-EINVAL
);
1551 *assoc_num
= nla_get_u8(tb_sa
[MACSEC_SA_ATTR_AN
]);
1553 dev
= get_dev_from_nl(net
, attrs
);
1555 return ERR_CAST(dev
);
1557 if (*assoc_num
>= MACSEC_NUM_AN
)
1558 return ERR_PTR(-EINVAL
);
1560 secy
= &macsec_priv(dev
)->secy
;
1561 tx_sc
= &secy
->tx_sc
;
1563 tx_sa
= rtnl_dereference(tx_sc
->sa
[*assoc_num
]);
1565 return ERR_PTR(-ENODEV
);
1573 static struct macsec_rx_sc
*get_rxsc_from_nl(struct net
*net
,
1574 struct nlattr
**attrs
,
1575 struct nlattr
**tb_rxsc
,
1576 struct net_device
**devp
,
1577 struct macsec_secy
**secyp
)
1579 struct net_device
*dev
;
1580 struct macsec_secy
*secy
;
1581 struct macsec_rx_sc
*rx_sc
;
1584 dev
= get_dev_from_nl(net
, attrs
);
1586 return ERR_CAST(dev
);
1588 secy
= &macsec_priv(dev
)->secy
;
1590 if (!tb_rxsc
[MACSEC_RXSC_ATTR_SCI
])
1591 return ERR_PTR(-EINVAL
);
1593 sci
= nla_get_sci(tb_rxsc
[MACSEC_RXSC_ATTR_SCI
]);
1594 rx_sc
= find_rx_sc_rtnl(secy
, sci
);
1596 return ERR_PTR(-ENODEV
);
1604 static struct macsec_rx_sa
*get_rxsa_from_nl(struct net
*net
,
1605 struct nlattr
**attrs
,
1606 struct nlattr
**tb_rxsc
,
1607 struct nlattr
**tb_sa
,
1608 struct net_device
**devp
,
1609 struct macsec_secy
**secyp
,
1610 struct macsec_rx_sc
**scp
,
1613 struct macsec_rx_sc
*rx_sc
;
1614 struct macsec_rx_sa
*rx_sa
;
1616 if (!tb_sa
[MACSEC_SA_ATTR_AN
])
1617 return ERR_PTR(-EINVAL
);
1619 *assoc_num
= nla_get_u8(tb_sa
[MACSEC_SA_ATTR_AN
]);
1620 if (*assoc_num
>= MACSEC_NUM_AN
)
1621 return ERR_PTR(-EINVAL
);
1623 rx_sc
= get_rxsc_from_nl(net
, attrs
, tb_rxsc
, devp
, secyp
);
1625 return ERR_CAST(rx_sc
);
1627 rx_sa
= rtnl_dereference(rx_sc
->sa
[*assoc_num
]);
1629 return ERR_PTR(-ENODEV
);
1635 static const struct nla_policy macsec_genl_policy
[NUM_MACSEC_ATTR
] = {
1636 [MACSEC_ATTR_IFINDEX
] = { .type
= NLA_U32
},
1637 [MACSEC_ATTR_RXSC_CONFIG
] = { .type
= NLA_NESTED
},
1638 [MACSEC_ATTR_SA_CONFIG
] = { .type
= NLA_NESTED
},
1639 [MACSEC_ATTR_OFFLOAD
] = { .type
= NLA_NESTED
},
1642 static const struct nla_policy macsec_genl_rxsc_policy
[NUM_MACSEC_RXSC_ATTR
] = {
1643 [MACSEC_RXSC_ATTR_SCI
] = { .type
= NLA_U64
},
1644 [MACSEC_RXSC_ATTR_ACTIVE
] = { .type
= NLA_U8
},
1647 static const struct nla_policy macsec_genl_sa_policy
[NUM_MACSEC_SA_ATTR
] = {
1648 [MACSEC_SA_ATTR_AN
] = { .type
= NLA_U8
},
1649 [MACSEC_SA_ATTR_ACTIVE
] = { .type
= NLA_U8
},
1650 [MACSEC_SA_ATTR_PN
] = NLA_POLICY_MIN_LEN(4),
1651 [MACSEC_SA_ATTR_KEYID
] = { .type
= NLA_BINARY
,
1652 .len
= MACSEC_KEYID_LEN
, },
1653 [MACSEC_SA_ATTR_KEY
] = { .type
= NLA_BINARY
,
1654 .len
= MACSEC_MAX_KEY_LEN
, },
1655 [MACSEC_SA_ATTR_SSCI
] = { .type
= NLA_U32
},
1656 [MACSEC_SA_ATTR_SALT
] = { .type
= NLA_BINARY
,
1657 .len
= MACSEC_SALT_LEN
, },
1660 static const struct nla_policy macsec_genl_offload_policy
[NUM_MACSEC_OFFLOAD_ATTR
] = {
1661 [MACSEC_OFFLOAD_ATTR_TYPE
] = { .type
= NLA_U8
},
1664 /* Offloads an operation to a device driver */
1665 static int macsec_offload(int (* const func
)(struct macsec_context
*),
1666 struct macsec_context
*ctx
)
1670 if (unlikely(!func
))
1673 if (ctx
->offload
== MACSEC_OFFLOAD_PHY
)
1674 mutex_lock(&ctx
->phydev
->lock
);
1678 if (ctx
->offload
== MACSEC_OFFLOAD_PHY
)
1679 mutex_unlock(&ctx
->phydev
->lock
);
1684 static int parse_sa_config(struct nlattr
**attrs
, struct nlattr
**tb_sa
)
1686 if (!attrs
[MACSEC_ATTR_SA_CONFIG
])
1689 if (nla_parse_nested_deprecated(tb_sa
, MACSEC_SA_ATTR_MAX
, attrs
[MACSEC_ATTR_SA_CONFIG
], macsec_genl_sa_policy
, NULL
))
1695 static int parse_rxsc_config(struct nlattr
**attrs
, struct nlattr
**tb_rxsc
)
1697 if (!attrs
[MACSEC_ATTR_RXSC_CONFIG
])
1700 if (nla_parse_nested_deprecated(tb_rxsc
, MACSEC_RXSC_ATTR_MAX
, attrs
[MACSEC_ATTR_RXSC_CONFIG
], macsec_genl_rxsc_policy
, NULL
))
1706 static bool validate_add_rxsa(struct nlattr
**attrs
)
1708 if (!attrs
[MACSEC_SA_ATTR_AN
] ||
1709 !attrs
[MACSEC_SA_ATTR_KEY
] ||
1710 !attrs
[MACSEC_SA_ATTR_KEYID
])
1713 if (nla_get_u8(attrs
[MACSEC_SA_ATTR_AN
]) >= MACSEC_NUM_AN
)
1716 if (attrs
[MACSEC_SA_ATTR_PN
] &&
1717 nla_get_u64(attrs
[MACSEC_SA_ATTR_PN
]) == 0)
1720 if (attrs
[MACSEC_SA_ATTR_ACTIVE
]) {
1721 if (nla_get_u8(attrs
[MACSEC_SA_ATTR_ACTIVE
]) > 1)
1725 if (nla_len(attrs
[MACSEC_SA_ATTR_KEYID
]) != MACSEC_KEYID_LEN
)
1731 static int macsec_add_rxsa(struct sk_buff
*skb
, struct genl_info
*info
)
1733 struct net_device
*dev
;
1734 struct nlattr
**attrs
= info
->attrs
;
1735 struct macsec_secy
*secy
;
1736 struct macsec_rx_sc
*rx_sc
;
1737 struct macsec_rx_sa
*rx_sa
;
1738 unsigned char assoc_num
;
1740 struct nlattr
*tb_rxsc
[MACSEC_RXSC_ATTR_MAX
+ 1];
1741 struct nlattr
*tb_sa
[MACSEC_SA_ATTR_MAX
+ 1];
1744 if (!attrs
[MACSEC_ATTR_IFINDEX
])
1747 if (parse_sa_config(attrs
, tb_sa
))
1750 if (parse_rxsc_config(attrs
, tb_rxsc
))
1753 if (!validate_add_rxsa(tb_sa
))
1757 rx_sc
= get_rxsc_from_nl(genl_info_net(info
), attrs
, tb_rxsc
, &dev
, &secy
);
1758 if (IS_ERR(rx_sc
)) {
1760 return PTR_ERR(rx_sc
);
1763 assoc_num
= nla_get_u8(tb_sa
[MACSEC_SA_ATTR_AN
]);
1765 if (nla_len(tb_sa
[MACSEC_SA_ATTR_KEY
]) != secy
->key_len
) {
1766 pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
1767 nla_len(tb_sa
[MACSEC_SA_ATTR_KEY
]), secy
->key_len
);
1772 pn_len
= secy
->xpn
? MACSEC_XPN_PN_LEN
: MACSEC_DEFAULT_PN_LEN
;
1773 if (tb_sa
[MACSEC_SA_ATTR_PN
] &&
1774 nla_len(tb_sa
[MACSEC_SA_ATTR_PN
]) != pn_len
) {
1775 pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
1776 nla_len(tb_sa
[MACSEC_SA_ATTR_PN
]), pn_len
);
1782 if (!tb_sa
[MACSEC_SA_ATTR_SSCI
] || !tb_sa
[MACSEC_SA_ATTR_SALT
]) {
1787 if (nla_len(tb_sa
[MACSEC_SA_ATTR_SALT
]) != MACSEC_SALT_LEN
) {
1788 pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n",
1789 nla_len(tb_sa
[MACSEC_SA_ATTR_SALT
]),
1796 rx_sa
= rtnl_dereference(rx_sc
->sa
[assoc_num
]);
1802 rx_sa
= kmalloc(sizeof(*rx_sa
), GFP_KERNEL
);
1808 err
= init_rx_sa(rx_sa
, nla_data(tb_sa
[MACSEC_SA_ATTR_KEY
]),
1809 secy
->key_len
, secy
->icv_len
);
1816 if (tb_sa
[MACSEC_SA_ATTR_PN
]) {
1817 spin_lock_bh(&rx_sa
->lock
);
1818 rx_sa
->next_pn
= nla_get_u64(tb_sa
[MACSEC_SA_ATTR_PN
]);
1819 spin_unlock_bh(&rx_sa
->lock
);
1822 if (tb_sa
[MACSEC_SA_ATTR_ACTIVE
])
1823 rx_sa
->active
= !!nla_get_u8(tb_sa
[MACSEC_SA_ATTR_ACTIVE
]);
1828 rx_sa
->ssci
= nla_get_ssci(tb_sa
[MACSEC_SA_ATTR_SSCI
]);
1829 nla_memcpy(rx_sa
->key
.salt
.bytes
, tb_sa
[MACSEC_SA_ATTR_SALT
],
1833 /* If h/w offloading is available, propagate to the device */
1834 if (macsec_is_offloaded(netdev_priv(dev
))) {
1835 const struct macsec_ops
*ops
;
1836 struct macsec_context ctx
;
1838 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
1844 ctx
.sa
.assoc_num
= assoc_num
;
1845 ctx
.sa
.rx_sa
= rx_sa
;
1847 memcpy(ctx
.sa
.key
, nla_data(tb_sa
[MACSEC_SA_ATTR_KEY
]),
1850 err
= macsec_offload(ops
->mdo_add_rxsa
, &ctx
);
1851 memzero_explicit(ctx
.sa
.key
, secy
->key_len
);
1856 nla_memcpy(rx_sa
->key
.id
, tb_sa
[MACSEC_SA_ATTR_KEYID
], MACSEC_KEYID_LEN
);
1857 rcu_assign_pointer(rx_sc
->sa
[assoc_num
], rx_sa
);
1864 macsec_rxsa_put(rx_sa
);
1869 static bool validate_add_rxsc(struct nlattr
**attrs
)
1871 if (!attrs
[MACSEC_RXSC_ATTR_SCI
])
1874 if (attrs
[MACSEC_RXSC_ATTR_ACTIVE
]) {
1875 if (nla_get_u8(attrs
[MACSEC_RXSC_ATTR_ACTIVE
]) > 1)
1882 static int macsec_add_rxsc(struct sk_buff
*skb
, struct genl_info
*info
)
1884 struct net_device
*dev
;
1885 sci_t sci
= MACSEC_UNDEF_SCI
;
1886 struct nlattr
**attrs
= info
->attrs
;
1887 struct macsec_rx_sc
*rx_sc
;
1888 struct nlattr
*tb_rxsc
[MACSEC_RXSC_ATTR_MAX
+ 1];
1889 struct macsec_secy
*secy
;
1893 if (!attrs
[MACSEC_ATTR_IFINDEX
])
1896 if (parse_rxsc_config(attrs
, tb_rxsc
))
1899 if (!validate_add_rxsc(tb_rxsc
))
1903 dev
= get_dev_from_nl(genl_info_net(info
), attrs
);
1906 return PTR_ERR(dev
);
1909 secy
= &macsec_priv(dev
)->secy
;
1910 sci
= nla_get_sci(tb_rxsc
[MACSEC_RXSC_ATTR_SCI
]);
1912 if (tb_rxsc
[MACSEC_RXSC_ATTR_ACTIVE
])
1913 active
= nla_get_u8(tb_rxsc
[MACSEC_RXSC_ATTR_ACTIVE
]);
1915 rx_sc
= create_rx_sc(dev
, sci
, active
);
1916 if (IS_ERR(rx_sc
)) {
1918 return PTR_ERR(rx_sc
);
1921 if (macsec_is_offloaded(netdev_priv(dev
))) {
1922 const struct macsec_ops
*ops
;
1923 struct macsec_context ctx
;
1925 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
1934 ret
= macsec_offload(ops
->mdo_add_rxsc
, &ctx
);
1944 del_rx_sc(secy
, sci
);
1950 static bool validate_add_txsa(struct nlattr
**attrs
)
1952 if (!attrs
[MACSEC_SA_ATTR_AN
] ||
1953 !attrs
[MACSEC_SA_ATTR_PN
] ||
1954 !attrs
[MACSEC_SA_ATTR_KEY
] ||
1955 !attrs
[MACSEC_SA_ATTR_KEYID
])
1958 if (nla_get_u8(attrs
[MACSEC_SA_ATTR_AN
]) >= MACSEC_NUM_AN
)
1961 if (nla_get_u64(attrs
[MACSEC_SA_ATTR_PN
]) == 0)
1964 if (attrs
[MACSEC_SA_ATTR_ACTIVE
]) {
1965 if (nla_get_u8(attrs
[MACSEC_SA_ATTR_ACTIVE
]) > 1)
1969 if (nla_len(attrs
[MACSEC_SA_ATTR_KEYID
]) != MACSEC_KEYID_LEN
)
1975 static int macsec_add_txsa(struct sk_buff
*skb
, struct genl_info
*info
)
1977 struct net_device
*dev
;
1978 struct nlattr
**attrs
= info
->attrs
;
1979 struct macsec_secy
*secy
;
1980 struct macsec_tx_sc
*tx_sc
;
1981 struct macsec_tx_sa
*tx_sa
;
1982 unsigned char assoc_num
;
1984 struct nlattr
*tb_sa
[MACSEC_SA_ATTR_MAX
+ 1];
1985 bool was_operational
;
1988 if (!attrs
[MACSEC_ATTR_IFINDEX
])
1991 if (parse_sa_config(attrs
, tb_sa
))
1994 if (!validate_add_txsa(tb_sa
))
1998 dev
= get_dev_from_nl(genl_info_net(info
), attrs
);
2001 return PTR_ERR(dev
);
2004 secy
= &macsec_priv(dev
)->secy
;
2005 tx_sc
= &secy
->tx_sc
;
2007 assoc_num
= nla_get_u8(tb_sa
[MACSEC_SA_ATTR_AN
]);
2009 if (nla_len(tb_sa
[MACSEC_SA_ATTR_KEY
]) != secy
->key_len
) {
2010 pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
2011 nla_len(tb_sa
[MACSEC_SA_ATTR_KEY
]), secy
->key_len
);
2016 pn_len
= secy
->xpn
? MACSEC_XPN_PN_LEN
: MACSEC_DEFAULT_PN_LEN
;
2017 if (nla_len(tb_sa
[MACSEC_SA_ATTR_PN
]) != pn_len
) {
2018 pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
2019 nla_len(tb_sa
[MACSEC_SA_ATTR_PN
]), pn_len
);
2025 if (!tb_sa
[MACSEC_SA_ATTR_SSCI
] || !tb_sa
[MACSEC_SA_ATTR_SALT
]) {
2030 if (nla_len(tb_sa
[MACSEC_SA_ATTR_SALT
]) != MACSEC_SALT_LEN
) {
2031 pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n",
2032 nla_len(tb_sa
[MACSEC_SA_ATTR_SALT
]),
2039 tx_sa
= rtnl_dereference(tx_sc
->sa
[assoc_num
]);
2045 tx_sa
= kmalloc(sizeof(*tx_sa
), GFP_KERNEL
);
2051 err
= init_tx_sa(tx_sa
, nla_data(tb_sa
[MACSEC_SA_ATTR_KEY
]),
2052 secy
->key_len
, secy
->icv_len
);
2059 spin_lock_bh(&tx_sa
->lock
);
2060 tx_sa
->next_pn
= nla_get_u64(tb_sa
[MACSEC_SA_ATTR_PN
]);
2061 spin_unlock_bh(&tx_sa
->lock
);
2063 if (tb_sa
[MACSEC_SA_ATTR_ACTIVE
])
2064 tx_sa
->active
= !!nla_get_u8(tb_sa
[MACSEC_SA_ATTR_ACTIVE
]);
2066 was_operational
= secy
->operational
;
2067 if (assoc_num
== tx_sc
->encoding_sa
&& tx_sa
->active
)
2068 secy
->operational
= true;
2071 tx_sa
->ssci
= nla_get_ssci(tb_sa
[MACSEC_SA_ATTR_SSCI
]);
2072 nla_memcpy(tx_sa
->key
.salt
.bytes
, tb_sa
[MACSEC_SA_ATTR_SALT
],
2076 /* If h/w offloading is available, propagate to the device */
2077 if (macsec_is_offloaded(netdev_priv(dev
))) {
2078 const struct macsec_ops
*ops
;
2079 struct macsec_context ctx
;
2081 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
2087 ctx
.sa
.assoc_num
= assoc_num
;
2088 ctx
.sa
.tx_sa
= tx_sa
;
2090 memcpy(ctx
.sa
.key
, nla_data(tb_sa
[MACSEC_SA_ATTR_KEY
]),
2093 err
= macsec_offload(ops
->mdo_add_txsa
, &ctx
);
2094 memzero_explicit(ctx
.sa
.key
, secy
->key_len
);
2099 nla_memcpy(tx_sa
->key
.id
, tb_sa
[MACSEC_SA_ATTR_KEYID
], MACSEC_KEYID_LEN
);
2100 rcu_assign_pointer(tx_sc
->sa
[assoc_num
], tx_sa
);
2107 secy
->operational
= was_operational
;
2108 macsec_txsa_put(tx_sa
);
2113 static int macsec_del_rxsa(struct sk_buff
*skb
, struct genl_info
*info
)
2115 struct nlattr
**attrs
= info
->attrs
;
2116 struct net_device
*dev
;
2117 struct macsec_secy
*secy
;
2118 struct macsec_rx_sc
*rx_sc
;
2119 struct macsec_rx_sa
*rx_sa
;
2121 struct nlattr
*tb_rxsc
[MACSEC_RXSC_ATTR_MAX
+ 1];
2122 struct nlattr
*tb_sa
[MACSEC_SA_ATTR_MAX
+ 1];
2125 if (!attrs
[MACSEC_ATTR_IFINDEX
])
2128 if (parse_sa_config(attrs
, tb_sa
))
2131 if (parse_rxsc_config(attrs
, tb_rxsc
))
2135 rx_sa
= get_rxsa_from_nl(genl_info_net(info
), attrs
, tb_rxsc
, tb_sa
,
2136 &dev
, &secy
, &rx_sc
, &assoc_num
);
2137 if (IS_ERR(rx_sa
)) {
2139 return PTR_ERR(rx_sa
);
2142 if (rx_sa
->active
) {
2147 /* If h/w offloading is available, propagate to the device */
2148 if (macsec_is_offloaded(netdev_priv(dev
))) {
2149 const struct macsec_ops
*ops
;
2150 struct macsec_context ctx
;
2152 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
2158 ctx
.sa
.assoc_num
= assoc_num
;
2159 ctx
.sa
.rx_sa
= rx_sa
;
2162 ret
= macsec_offload(ops
->mdo_del_rxsa
, &ctx
);
2167 RCU_INIT_POINTER(rx_sc
->sa
[assoc_num
], NULL
);
2179 static int macsec_del_rxsc(struct sk_buff
*skb
, struct genl_info
*info
)
2181 struct nlattr
**attrs
= info
->attrs
;
2182 struct net_device
*dev
;
2183 struct macsec_secy
*secy
;
2184 struct macsec_rx_sc
*rx_sc
;
2186 struct nlattr
*tb_rxsc
[MACSEC_RXSC_ATTR_MAX
+ 1];
2189 if (!attrs
[MACSEC_ATTR_IFINDEX
])
2192 if (parse_rxsc_config(attrs
, tb_rxsc
))
2195 if (!tb_rxsc
[MACSEC_RXSC_ATTR_SCI
])
2199 dev
= get_dev_from_nl(genl_info_net(info
), info
->attrs
);
2202 return PTR_ERR(dev
);
2205 secy
= &macsec_priv(dev
)->secy
;
2206 sci
= nla_get_sci(tb_rxsc
[MACSEC_RXSC_ATTR_SCI
]);
2208 rx_sc
= del_rx_sc(secy
, sci
);
2214 /* If h/w offloading is available, propagate to the device */
2215 if (macsec_is_offloaded(netdev_priv(dev
))) {
2216 const struct macsec_ops
*ops
;
2217 struct macsec_context ctx
;
2219 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
2227 ret
= macsec_offload(ops
->mdo_del_rxsc
, &ctx
);
2242 static int macsec_del_txsa(struct sk_buff
*skb
, struct genl_info
*info
)
2244 struct nlattr
**attrs
= info
->attrs
;
2245 struct net_device
*dev
;
2246 struct macsec_secy
*secy
;
2247 struct macsec_tx_sc
*tx_sc
;
2248 struct macsec_tx_sa
*tx_sa
;
2250 struct nlattr
*tb_sa
[MACSEC_SA_ATTR_MAX
+ 1];
2253 if (!attrs
[MACSEC_ATTR_IFINDEX
])
2256 if (parse_sa_config(attrs
, tb_sa
))
2260 tx_sa
= get_txsa_from_nl(genl_info_net(info
), attrs
, tb_sa
,
2261 &dev
, &secy
, &tx_sc
, &assoc_num
);
2262 if (IS_ERR(tx_sa
)) {
2264 return PTR_ERR(tx_sa
);
2267 if (tx_sa
->active
) {
2272 /* If h/w offloading is available, propagate to the device */
2273 if (macsec_is_offloaded(netdev_priv(dev
))) {
2274 const struct macsec_ops
*ops
;
2275 struct macsec_context ctx
;
2277 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
2283 ctx
.sa
.assoc_num
= assoc_num
;
2284 ctx
.sa
.tx_sa
= tx_sa
;
2287 ret
= macsec_offload(ops
->mdo_del_txsa
, &ctx
);
2292 RCU_INIT_POINTER(tx_sc
->sa
[assoc_num
], NULL
);
2304 static bool validate_upd_sa(struct nlattr
**attrs
)
2306 if (!attrs
[MACSEC_SA_ATTR_AN
] ||
2307 attrs
[MACSEC_SA_ATTR_KEY
] ||
2308 attrs
[MACSEC_SA_ATTR_KEYID
] ||
2309 attrs
[MACSEC_SA_ATTR_SSCI
] ||
2310 attrs
[MACSEC_SA_ATTR_SALT
])
2313 if (nla_get_u8(attrs
[MACSEC_SA_ATTR_AN
]) >= MACSEC_NUM_AN
)
2316 if (attrs
[MACSEC_SA_ATTR_PN
] && nla_get_u64(attrs
[MACSEC_SA_ATTR_PN
]) == 0)
2319 if (attrs
[MACSEC_SA_ATTR_ACTIVE
]) {
2320 if (nla_get_u8(attrs
[MACSEC_SA_ATTR_ACTIVE
]) > 1)
2327 static int macsec_upd_txsa(struct sk_buff
*skb
, struct genl_info
*info
)
2329 struct nlattr
**attrs
= info
->attrs
;
2330 struct net_device
*dev
;
2331 struct macsec_secy
*secy
;
2332 struct macsec_tx_sc
*tx_sc
;
2333 struct macsec_tx_sa
*tx_sa
;
2335 struct nlattr
*tb_sa
[MACSEC_SA_ATTR_MAX
+ 1];
2336 bool was_operational
, was_active
;
2342 if (!attrs
[MACSEC_ATTR_IFINDEX
])
2345 if (parse_sa_config(attrs
, tb_sa
))
2348 if (!validate_upd_sa(tb_sa
))
2352 tx_sa
= get_txsa_from_nl(genl_info_net(info
), attrs
, tb_sa
,
2353 &dev
, &secy
, &tx_sc
, &assoc_num
);
2354 if (IS_ERR(tx_sa
)) {
2356 return PTR_ERR(tx_sa
);
2359 if (tb_sa
[MACSEC_SA_ATTR_PN
]) {
2362 pn_len
= secy
->xpn
? MACSEC_XPN_PN_LEN
: MACSEC_DEFAULT_PN_LEN
;
2363 if (nla_len(tb_sa
[MACSEC_SA_ATTR_PN
]) != pn_len
) {
2364 pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
2365 nla_len(tb_sa
[MACSEC_SA_ATTR_PN
]), pn_len
);
2370 spin_lock_bh(&tx_sa
->lock
);
2371 prev_pn
= tx_sa
->next_pn_halves
;
2372 tx_sa
->next_pn
= nla_get_u64(tb_sa
[MACSEC_SA_ATTR_PN
]);
2373 spin_unlock_bh(&tx_sa
->lock
);
2376 was_active
= tx_sa
->active
;
2377 if (tb_sa
[MACSEC_SA_ATTR_ACTIVE
])
2378 tx_sa
->active
= nla_get_u8(tb_sa
[MACSEC_SA_ATTR_ACTIVE
]);
2380 was_operational
= secy
->operational
;
2381 if (assoc_num
== tx_sc
->encoding_sa
)
2382 secy
->operational
= tx_sa
->active
;
2384 /* If h/w offloading is available, propagate to the device */
2385 if (macsec_is_offloaded(netdev_priv(dev
))) {
2386 const struct macsec_ops
*ops
;
2387 struct macsec_context ctx
;
2389 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
2395 ctx
.sa
.assoc_num
= assoc_num
;
2396 ctx
.sa
.tx_sa
= tx_sa
;
2397 ctx
.sa
.update_pn
= !!prev_pn
.full64
;
2400 ret
= macsec_offload(ops
->mdo_upd_txsa
, &ctx
);
2410 if (tb_sa
[MACSEC_SA_ATTR_PN
]) {
2411 spin_lock_bh(&tx_sa
->lock
);
2412 tx_sa
->next_pn_halves
= prev_pn
;
2413 spin_unlock_bh(&tx_sa
->lock
);
2415 tx_sa
->active
= was_active
;
2416 secy
->operational
= was_operational
;
2421 static int macsec_upd_rxsa(struct sk_buff
*skb
, struct genl_info
*info
)
2423 struct nlattr
**attrs
= info
->attrs
;
2424 struct net_device
*dev
;
2425 struct macsec_secy
*secy
;
2426 struct macsec_rx_sc
*rx_sc
;
2427 struct macsec_rx_sa
*rx_sa
;
2429 struct nlattr
*tb_rxsc
[MACSEC_RXSC_ATTR_MAX
+ 1];
2430 struct nlattr
*tb_sa
[MACSEC_SA_ATTR_MAX
+ 1];
2437 if (!attrs
[MACSEC_ATTR_IFINDEX
])
2440 if (parse_rxsc_config(attrs
, tb_rxsc
))
2443 if (parse_sa_config(attrs
, tb_sa
))
2446 if (!validate_upd_sa(tb_sa
))
2450 rx_sa
= get_rxsa_from_nl(genl_info_net(info
), attrs
, tb_rxsc
, tb_sa
,
2451 &dev
, &secy
, &rx_sc
, &assoc_num
);
2452 if (IS_ERR(rx_sa
)) {
2454 return PTR_ERR(rx_sa
);
2457 if (tb_sa
[MACSEC_SA_ATTR_PN
]) {
2460 pn_len
= secy
->xpn
? MACSEC_XPN_PN_LEN
: MACSEC_DEFAULT_PN_LEN
;
2461 if (nla_len(tb_sa
[MACSEC_SA_ATTR_PN
]) != pn_len
) {
2462 pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
2463 nla_len(tb_sa
[MACSEC_SA_ATTR_PN
]), pn_len
);
2468 spin_lock_bh(&rx_sa
->lock
);
2469 prev_pn
= rx_sa
->next_pn_halves
;
2470 rx_sa
->next_pn
= nla_get_u64(tb_sa
[MACSEC_SA_ATTR_PN
]);
2471 spin_unlock_bh(&rx_sa
->lock
);
2474 was_active
= rx_sa
->active
;
2475 if (tb_sa
[MACSEC_SA_ATTR_ACTIVE
])
2476 rx_sa
->active
= nla_get_u8(tb_sa
[MACSEC_SA_ATTR_ACTIVE
]);
2478 /* If h/w offloading is available, propagate to the device */
2479 if (macsec_is_offloaded(netdev_priv(dev
))) {
2480 const struct macsec_ops
*ops
;
2481 struct macsec_context ctx
;
2483 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
2489 ctx
.sa
.assoc_num
= assoc_num
;
2490 ctx
.sa
.rx_sa
= rx_sa
;
2491 ctx
.sa
.update_pn
= !!prev_pn
.full64
;
2494 ret
= macsec_offload(ops
->mdo_upd_rxsa
, &ctx
);
2503 if (tb_sa
[MACSEC_SA_ATTR_PN
]) {
2504 spin_lock_bh(&rx_sa
->lock
);
2505 rx_sa
->next_pn_halves
= prev_pn
;
2506 spin_unlock_bh(&rx_sa
->lock
);
2508 rx_sa
->active
= was_active
;
2513 static int macsec_upd_rxsc(struct sk_buff
*skb
, struct genl_info
*info
)
2515 struct nlattr
**attrs
= info
->attrs
;
2516 struct net_device
*dev
;
2517 struct macsec_secy
*secy
;
2518 struct macsec_rx_sc
*rx_sc
;
2519 struct nlattr
*tb_rxsc
[MACSEC_RXSC_ATTR_MAX
+ 1];
2520 unsigned int prev_n_rx_sc
;
2524 if (!attrs
[MACSEC_ATTR_IFINDEX
])
2527 if (parse_rxsc_config(attrs
, tb_rxsc
))
2530 if (!validate_add_rxsc(tb_rxsc
))
2534 rx_sc
= get_rxsc_from_nl(genl_info_net(info
), attrs
, tb_rxsc
, &dev
, &secy
);
2535 if (IS_ERR(rx_sc
)) {
2537 return PTR_ERR(rx_sc
);
2540 was_active
= rx_sc
->active
;
2541 prev_n_rx_sc
= secy
->n_rx_sc
;
2542 if (tb_rxsc
[MACSEC_RXSC_ATTR_ACTIVE
]) {
2543 bool new = !!nla_get_u8(tb_rxsc
[MACSEC_RXSC_ATTR_ACTIVE
]);
2545 if (rx_sc
->active
!= new)
2546 secy
->n_rx_sc
+= new ? 1 : -1;
2548 rx_sc
->active
= new;
2551 /* If h/w offloading is available, propagate to the device */
2552 if (macsec_is_offloaded(netdev_priv(dev
))) {
2553 const struct macsec_ops
*ops
;
2554 struct macsec_context ctx
;
2556 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
2565 ret
= macsec_offload(ops
->mdo_upd_rxsc
, &ctx
);
2575 secy
->n_rx_sc
= prev_n_rx_sc
;
2576 rx_sc
->active
= was_active
;
2581 static bool macsec_is_configured(struct macsec_dev
*macsec
)
2583 struct macsec_secy
*secy
= &macsec
->secy
;
2584 struct macsec_tx_sc
*tx_sc
= &secy
->tx_sc
;
2590 for (i
= 0; i
< MACSEC_NUM_AN
; i
++)
2597 static bool macsec_needs_tx_tag(struct macsec_dev
*macsec
,
2598 const struct macsec_ops
*ops
)
2600 return macsec
->offload
== MACSEC_OFFLOAD_PHY
&&
2601 ops
->mdo_insert_tx_tag
;
2604 static void macsec_set_head_tail_room(struct net_device
*dev
)
2606 struct macsec_dev
*macsec
= macsec_priv(dev
);
2607 struct net_device
*real_dev
= macsec
->real_dev
;
2608 int needed_headroom
, needed_tailroom
;
2609 const struct macsec_ops
*ops
;
2611 ops
= macsec_get_ops(macsec
, NULL
);
2613 needed_headroom
= ops
->needed_headroom
;
2614 needed_tailroom
= ops
->needed_tailroom
;
2616 needed_headroom
= MACSEC_NEEDED_HEADROOM
;
2617 needed_tailroom
= MACSEC_NEEDED_TAILROOM
;
2620 dev
->needed_headroom
= real_dev
->needed_headroom
+ needed_headroom
;
2621 dev
->needed_tailroom
= real_dev
->needed_tailroom
+ needed_tailroom
;
2624 static void macsec_inherit_tso_max(struct net_device
*dev
)
2626 struct macsec_dev
*macsec
= macsec_priv(dev
);
2628 /* if macsec is offloaded, we need to follow the lower
2629 * device's capabilities. otherwise, we can ignore them.
2631 if (macsec_is_offloaded(macsec
))
2632 netif_inherit_tso_max(dev
, macsec
->real_dev
);
2635 static int macsec_update_offload(struct net_device
*dev
, enum macsec_offload offload
)
2637 enum macsec_offload prev_offload
;
2638 const struct macsec_ops
*ops
;
2639 struct macsec_context ctx
;
2640 struct macsec_dev
*macsec
;
2643 macsec
= macsec_priv(dev
);
2645 /* Check if the offloading mode is supported by the underlying layers */
2646 if (offload
!= MACSEC_OFFLOAD_OFF
&&
2647 !macsec_check_offload(offload
, macsec
))
2650 /* Check if the net device is busy. */
2651 if (netif_running(dev
))
2654 /* Check if the device already has rules configured: we do not support
2657 if (macsec_is_configured(macsec
))
2660 prev_offload
= macsec
->offload
;
2662 ops
= __macsec_get_ops(offload
== MACSEC_OFFLOAD_OFF
? prev_offload
: offload
,
2667 macsec
->offload
= offload
;
2669 ctx
.secy
= &macsec
->secy
;
2670 ret
= offload
== MACSEC_OFFLOAD_OFF
? macsec_offload(ops
->mdo_del_secy
, &ctx
)
2671 : macsec_offload(ops
->mdo_add_secy
, &ctx
);
2673 macsec
->offload
= prev_offload
;
2677 macsec_set_head_tail_room(dev
);
2678 macsec
->insert_tx_tag
= macsec_needs_tx_tag(macsec
, ops
);
2680 macsec_inherit_tso_max(dev
);
2682 netdev_update_features(dev
);
2687 static int macsec_upd_offload(struct sk_buff
*skb
, struct genl_info
*info
)
2689 struct nlattr
*tb_offload
[MACSEC_OFFLOAD_ATTR_MAX
+ 1];
2690 struct nlattr
**attrs
= info
->attrs
;
2691 enum macsec_offload offload
;
2692 struct macsec_dev
*macsec
;
2693 struct net_device
*dev
;
2696 if (!attrs
[MACSEC_ATTR_IFINDEX
])
2699 if (!attrs
[MACSEC_ATTR_OFFLOAD
])
2702 if (nla_parse_nested_deprecated(tb_offload
, MACSEC_OFFLOAD_ATTR_MAX
,
2703 attrs
[MACSEC_ATTR_OFFLOAD
],
2704 macsec_genl_offload_policy
, NULL
))
2709 dev
= get_dev_from_nl(genl_info_net(info
), attrs
);
2714 macsec
= macsec_priv(dev
);
2716 if (!tb_offload
[MACSEC_OFFLOAD_ATTR_TYPE
]) {
2721 offload
= nla_get_u8(tb_offload
[MACSEC_OFFLOAD_ATTR_TYPE
]);
2723 if (macsec
->offload
!= offload
)
2724 ret
= macsec_update_offload(dev
, offload
);
2730 static void get_tx_sa_stats(struct net_device
*dev
, int an
,
2731 struct macsec_tx_sa
*tx_sa
,
2732 struct macsec_tx_sa_stats
*sum
)
2734 struct macsec_dev
*macsec
= macsec_priv(dev
);
2737 /* If h/w offloading is available, propagate to the device */
2738 if (macsec_is_offloaded(macsec
)) {
2739 const struct macsec_ops
*ops
;
2740 struct macsec_context ctx
;
2742 ops
= macsec_get_ops(macsec
, &ctx
);
2744 ctx
.sa
.assoc_num
= an
;
2745 ctx
.sa
.tx_sa
= tx_sa
;
2746 ctx
.stats
.tx_sa_stats
= sum
;
2747 ctx
.secy
= &macsec_priv(dev
)->secy
;
2748 macsec_offload(ops
->mdo_get_tx_sa_stats
, &ctx
);
2753 for_each_possible_cpu(cpu
) {
2754 const struct macsec_tx_sa_stats
*stats
=
2755 per_cpu_ptr(tx_sa
->stats
, cpu
);
2757 sum
->OutPktsProtected
+= stats
->OutPktsProtected
;
2758 sum
->OutPktsEncrypted
+= stats
->OutPktsEncrypted
;
2762 static int copy_tx_sa_stats(struct sk_buff
*skb
, struct macsec_tx_sa_stats
*sum
)
2764 if (nla_put_u32(skb
, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED
,
2765 sum
->OutPktsProtected
) ||
2766 nla_put_u32(skb
, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED
,
2767 sum
->OutPktsEncrypted
))
2773 static void get_rx_sa_stats(struct net_device
*dev
,
2774 struct macsec_rx_sc
*rx_sc
, int an
,
2775 struct macsec_rx_sa
*rx_sa
,
2776 struct macsec_rx_sa_stats
*sum
)
2778 struct macsec_dev
*macsec
= macsec_priv(dev
);
2781 /* If h/w offloading is available, propagate to the device */
2782 if (macsec_is_offloaded(macsec
)) {
2783 const struct macsec_ops
*ops
;
2784 struct macsec_context ctx
;
2786 ops
= macsec_get_ops(macsec
, &ctx
);
2788 ctx
.sa
.assoc_num
= an
;
2789 ctx
.sa
.rx_sa
= rx_sa
;
2790 ctx
.stats
.rx_sa_stats
= sum
;
2791 ctx
.secy
= &macsec_priv(dev
)->secy
;
2793 macsec_offload(ops
->mdo_get_rx_sa_stats
, &ctx
);
2798 for_each_possible_cpu(cpu
) {
2799 const struct macsec_rx_sa_stats
*stats
=
2800 per_cpu_ptr(rx_sa
->stats
, cpu
);
2802 sum
->InPktsOK
+= stats
->InPktsOK
;
2803 sum
->InPktsInvalid
+= stats
->InPktsInvalid
;
2804 sum
->InPktsNotValid
+= stats
->InPktsNotValid
;
2805 sum
->InPktsNotUsingSA
+= stats
->InPktsNotUsingSA
;
2806 sum
->InPktsUnusedSA
+= stats
->InPktsUnusedSA
;
2810 static int copy_rx_sa_stats(struct sk_buff
*skb
,
2811 struct macsec_rx_sa_stats
*sum
)
2813 if (nla_put_u32(skb
, MACSEC_SA_STATS_ATTR_IN_PKTS_OK
, sum
->InPktsOK
) ||
2814 nla_put_u32(skb
, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID
,
2815 sum
->InPktsInvalid
) ||
2816 nla_put_u32(skb
, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID
,
2817 sum
->InPktsNotValid
) ||
2818 nla_put_u32(skb
, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA
,
2819 sum
->InPktsNotUsingSA
) ||
2820 nla_put_u32(skb
, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA
,
2821 sum
->InPktsUnusedSA
))
2827 static void get_rx_sc_stats(struct net_device
*dev
,
2828 struct macsec_rx_sc
*rx_sc
,
2829 struct macsec_rx_sc_stats
*sum
)
2831 struct macsec_dev
*macsec
= macsec_priv(dev
);
2834 /* If h/w offloading is available, propagate to the device */
2835 if (macsec_is_offloaded(macsec
)) {
2836 const struct macsec_ops
*ops
;
2837 struct macsec_context ctx
;
2839 ops
= macsec_get_ops(macsec
, &ctx
);
2841 ctx
.stats
.rx_sc_stats
= sum
;
2842 ctx
.secy
= &macsec_priv(dev
)->secy
;
2844 macsec_offload(ops
->mdo_get_rx_sc_stats
, &ctx
);
2849 for_each_possible_cpu(cpu
) {
2850 const struct pcpu_rx_sc_stats
*stats
;
2851 struct macsec_rx_sc_stats tmp
;
2854 stats
= per_cpu_ptr(rx_sc
->stats
, cpu
);
2856 start
= u64_stats_fetch_begin(&stats
->syncp
);
2857 memcpy(&tmp
, &stats
->stats
, sizeof(tmp
));
2858 } while (u64_stats_fetch_retry(&stats
->syncp
, start
));
2860 sum
->InOctetsValidated
+= tmp
.InOctetsValidated
;
2861 sum
->InOctetsDecrypted
+= tmp
.InOctetsDecrypted
;
2862 sum
->InPktsUnchecked
+= tmp
.InPktsUnchecked
;
2863 sum
->InPktsDelayed
+= tmp
.InPktsDelayed
;
2864 sum
->InPktsOK
+= tmp
.InPktsOK
;
2865 sum
->InPktsInvalid
+= tmp
.InPktsInvalid
;
2866 sum
->InPktsLate
+= tmp
.InPktsLate
;
2867 sum
->InPktsNotValid
+= tmp
.InPktsNotValid
;
2868 sum
->InPktsNotUsingSA
+= tmp
.InPktsNotUsingSA
;
2869 sum
->InPktsUnusedSA
+= tmp
.InPktsUnusedSA
;
2873 static int copy_rx_sc_stats(struct sk_buff
*skb
, struct macsec_rx_sc_stats
*sum
)
2875 if (nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED
,
2876 sum
->InOctetsValidated
,
2877 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2878 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED
,
2879 sum
->InOctetsDecrypted
,
2880 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2881 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED
,
2882 sum
->InPktsUnchecked
,
2883 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2884 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED
,
2886 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2887 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK
,
2889 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2890 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID
,
2892 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2893 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE
,
2895 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2896 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID
,
2897 sum
->InPktsNotValid
,
2898 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2899 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA
,
2900 sum
->InPktsNotUsingSA
,
2901 MACSEC_RXSC_STATS_ATTR_PAD
) ||
2902 nla_put_u64_64bit(skb
, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA
,
2903 sum
->InPktsUnusedSA
,
2904 MACSEC_RXSC_STATS_ATTR_PAD
))
2910 static void get_tx_sc_stats(struct net_device
*dev
,
2911 struct macsec_tx_sc_stats
*sum
)
2913 struct macsec_dev
*macsec
= macsec_priv(dev
);
2916 /* If h/w offloading is available, propagate to the device */
2917 if (macsec_is_offloaded(macsec
)) {
2918 const struct macsec_ops
*ops
;
2919 struct macsec_context ctx
;
2921 ops
= macsec_get_ops(macsec
, &ctx
);
2923 ctx
.stats
.tx_sc_stats
= sum
;
2924 ctx
.secy
= &macsec_priv(dev
)->secy
;
2925 macsec_offload(ops
->mdo_get_tx_sc_stats
, &ctx
);
2930 for_each_possible_cpu(cpu
) {
2931 const struct pcpu_tx_sc_stats
*stats
;
2932 struct macsec_tx_sc_stats tmp
;
2935 stats
= per_cpu_ptr(macsec_priv(dev
)->secy
.tx_sc
.stats
, cpu
);
2937 start
= u64_stats_fetch_begin(&stats
->syncp
);
2938 memcpy(&tmp
, &stats
->stats
, sizeof(tmp
));
2939 } while (u64_stats_fetch_retry(&stats
->syncp
, start
));
2941 sum
->OutPktsProtected
+= tmp
.OutPktsProtected
;
2942 sum
->OutPktsEncrypted
+= tmp
.OutPktsEncrypted
;
2943 sum
->OutOctetsProtected
+= tmp
.OutOctetsProtected
;
2944 sum
->OutOctetsEncrypted
+= tmp
.OutOctetsEncrypted
;
2948 static int copy_tx_sc_stats(struct sk_buff
*skb
, struct macsec_tx_sc_stats
*sum
)
2950 if (nla_put_u64_64bit(skb
, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED
,
2951 sum
->OutPktsProtected
,
2952 MACSEC_TXSC_STATS_ATTR_PAD
) ||
2953 nla_put_u64_64bit(skb
, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED
,
2954 sum
->OutPktsEncrypted
,
2955 MACSEC_TXSC_STATS_ATTR_PAD
) ||
2956 nla_put_u64_64bit(skb
, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED
,
2957 sum
->OutOctetsProtected
,
2958 MACSEC_TXSC_STATS_ATTR_PAD
) ||
2959 nla_put_u64_64bit(skb
, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED
,
2960 sum
->OutOctetsEncrypted
,
2961 MACSEC_TXSC_STATS_ATTR_PAD
))
2967 static void get_secy_stats(struct net_device
*dev
, struct macsec_dev_stats
*sum
)
2969 struct macsec_dev
*macsec
= macsec_priv(dev
);
2972 /* If h/w offloading is available, propagate to the device */
2973 if (macsec_is_offloaded(macsec
)) {
2974 const struct macsec_ops
*ops
;
2975 struct macsec_context ctx
;
2977 ops
= macsec_get_ops(macsec
, &ctx
);
2979 ctx
.stats
.dev_stats
= sum
;
2980 ctx
.secy
= &macsec_priv(dev
)->secy
;
2981 macsec_offload(ops
->mdo_get_dev_stats
, &ctx
);
2986 for_each_possible_cpu(cpu
) {
2987 const struct pcpu_secy_stats
*stats
;
2988 struct macsec_dev_stats tmp
;
2991 stats
= per_cpu_ptr(macsec_priv(dev
)->stats
, cpu
);
2993 start
= u64_stats_fetch_begin(&stats
->syncp
);
2994 memcpy(&tmp
, &stats
->stats
, sizeof(tmp
));
2995 } while (u64_stats_fetch_retry(&stats
->syncp
, start
));
2997 sum
->OutPktsUntagged
+= tmp
.OutPktsUntagged
;
2998 sum
->InPktsUntagged
+= tmp
.InPktsUntagged
;
2999 sum
->OutPktsTooLong
+= tmp
.OutPktsTooLong
;
3000 sum
->InPktsNoTag
+= tmp
.InPktsNoTag
;
3001 sum
->InPktsBadTag
+= tmp
.InPktsBadTag
;
3002 sum
->InPktsUnknownSCI
+= tmp
.InPktsUnknownSCI
;
3003 sum
->InPktsNoSCI
+= tmp
.InPktsNoSCI
;
3004 sum
->InPktsOverrun
+= tmp
.InPktsOverrun
;
3008 static int copy_secy_stats(struct sk_buff
*skb
, struct macsec_dev_stats
*sum
)
3010 if (nla_put_u64_64bit(skb
, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED
,
3011 sum
->OutPktsUntagged
,
3012 MACSEC_SECY_STATS_ATTR_PAD
) ||
3013 nla_put_u64_64bit(skb
, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED
,
3014 sum
->InPktsUntagged
,
3015 MACSEC_SECY_STATS_ATTR_PAD
) ||
3016 nla_put_u64_64bit(skb
, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG
,
3017 sum
->OutPktsTooLong
,
3018 MACSEC_SECY_STATS_ATTR_PAD
) ||
3019 nla_put_u64_64bit(skb
, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG
,
3021 MACSEC_SECY_STATS_ATTR_PAD
) ||
3022 nla_put_u64_64bit(skb
, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG
,
3024 MACSEC_SECY_STATS_ATTR_PAD
) ||
3025 nla_put_u64_64bit(skb
, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI
,
3026 sum
->InPktsUnknownSCI
,
3027 MACSEC_SECY_STATS_ATTR_PAD
) ||
3028 nla_put_u64_64bit(skb
, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI
,
3030 MACSEC_SECY_STATS_ATTR_PAD
) ||
3031 nla_put_u64_64bit(skb
, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN
,
3033 MACSEC_SECY_STATS_ATTR_PAD
))
3039 static int nla_put_secy(struct macsec_secy
*secy
, struct sk_buff
*skb
)
3041 struct macsec_tx_sc
*tx_sc
= &secy
->tx_sc
;
3042 struct nlattr
*secy_nest
= nla_nest_start_noflag(skb
,
3049 switch (secy
->key_len
) {
3050 case MACSEC_GCM_AES_128_SAK_LEN
:
3051 csid
= secy
->xpn
? MACSEC_CIPHER_ID_GCM_AES_XPN_128
: MACSEC_DEFAULT_CIPHER_ID
;
3053 case MACSEC_GCM_AES_256_SAK_LEN
:
3054 csid
= secy
->xpn
? MACSEC_CIPHER_ID_GCM_AES_XPN_256
: MACSEC_CIPHER_ID_GCM_AES_256
;
3060 if (nla_put_sci(skb
, MACSEC_SECY_ATTR_SCI
, secy
->sci
,
3061 MACSEC_SECY_ATTR_PAD
) ||
3062 nla_put_u64_64bit(skb
, MACSEC_SECY_ATTR_CIPHER_SUITE
,
3063 csid
, MACSEC_SECY_ATTR_PAD
) ||
3064 nla_put_u8(skb
, MACSEC_SECY_ATTR_ICV_LEN
, secy
->icv_len
) ||
3065 nla_put_u8(skb
, MACSEC_SECY_ATTR_OPER
, secy
->operational
) ||
3066 nla_put_u8(skb
, MACSEC_SECY_ATTR_PROTECT
, secy
->protect_frames
) ||
3067 nla_put_u8(skb
, MACSEC_SECY_ATTR_REPLAY
, secy
->replay_protect
) ||
3068 nla_put_u8(skb
, MACSEC_SECY_ATTR_VALIDATE
, secy
->validate_frames
) ||
3069 nla_put_u8(skb
, MACSEC_SECY_ATTR_ENCRYPT
, tx_sc
->encrypt
) ||
3070 nla_put_u8(skb
, MACSEC_SECY_ATTR_INC_SCI
, tx_sc
->send_sci
) ||
3071 nla_put_u8(skb
, MACSEC_SECY_ATTR_ES
, tx_sc
->end_station
) ||
3072 nla_put_u8(skb
, MACSEC_SECY_ATTR_SCB
, tx_sc
->scb
) ||
3073 nla_put_u8(skb
, MACSEC_SECY_ATTR_ENCODING_SA
, tx_sc
->encoding_sa
))
3076 if (secy
->replay_protect
) {
3077 if (nla_put_u32(skb
, MACSEC_SECY_ATTR_WINDOW
, secy
->replay_window
))
3081 nla_nest_end(skb
, secy_nest
);
3085 nla_nest_cancel(skb
, secy_nest
);
3089 static noinline_for_stack
int
3090 dump_secy(struct macsec_secy
*secy
, struct net_device
*dev
,
3091 struct sk_buff
*skb
, struct netlink_callback
*cb
)
3093 struct macsec_tx_sc_stats tx_sc_stats
= {0, };
3094 struct macsec_tx_sa_stats tx_sa_stats
= {0, };
3095 struct macsec_rx_sc_stats rx_sc_stats
= {0, };
3096 struct macsec_rx_sa_stats rx_sa_stats
= {0, };
3097 struct macsec_dev
*macsec
= netdev_priv(dev
);
3098 struct macsec_dev_stats dev_stats
= {0, };
3099 struct macsec_tx_sc
*tx_sc
= &secy
->tx_sc
;
3100 struct nlattr
*txsa_list
, *rxsc_list
;
3101 struct macsec_rx_sc
*rx_sc
;
3102 struct nlattr
*attr
;
3106 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3107 &macsec_fam
, NLM_F_MULTI
, MACSEC_CMD_GET_TXSC
);
3111 genl_dump_check_consistent(cb
, hdr
);
3113 if (nla_put_u32(skb
, MACSEC_ATTR_IFINDEX
, dev
->ifindex
))
3114 goto nla_put_failure
;
3116 attr
= nla_nest_start_noflag(skb
, MACSEC_ATTR_OFFLOAD
);
3118 goto nla_put_failure
;
3119 if (nla_put_u8(skb
, MACSEC_OFFLOAD_ATTR_TYPE
, macsec
->offload
))
3120 goto nla_put_failure
;
3121 nla_nest_end(skb
, attr
);
3123 if (nla_put_secy(secy
, skb
))
3124 goto nla_put_failure
;
3126 attr
= nla_nest_start_noflag(skb
, MACSEC_ATTR_TXSC_STATS
);
3128 goto nla_put_failure
;
3130 get_tx_sc_stats(dev
, &tx_sc_stats
);
3131 if (copy_tx_sc_stats(skb
, &tx_sc_stats
)) {
3132 nla_nest_cancel(skb
, attr
);
3133 goto nla_put_failure
;
3135 nla_nest_end(skb
, attr
);
3137 attr
= nla_nest_start_noflag(skb
, MACSEC_ATTR_SECY_STATS
);
3139 goto nla_put_failure
;
3140 get_secy_stats(dev
, &dev_stats
);
3141 if (copy_secy_stats(skb
, &dev_stats
)) {
3142 nla_nest_cancel(skb
, attr
);
3143 goto nla_put_failure
;
3145 nla_nest_end(skb
, attr
);
3147 txsa_list
= nla_nest_start_noflag(skb
, MACSEC_ATTR_TXSA_LIST
);
3149 goto nla_put_failure
;
3150 for (i
= 0, j
= 1; i
< MACSEC_NUM_AN
; i
++) {
3151 struct macsec_tx_sa
*tx_sa
= rtnl_dereference(tx_sc
->sa
[i
]);
3152 struct nlattr
*txsa_nest
;
3159 txsa_nest
= nla_nest_start_noflag(skb
, j
++);
3161 nla_nest_cancel(skb
, txsa_list
);
3162 goto nla_put_failure
;
3165 attr
= nla_nest_start_noflag(skb
, MACSEC_SA_ATTR_STATS
);
3167 nla_nest_cancel(skb
, txsa_nest
);
3168 nla_nest_cancel(skb
, txsa_list
);
3169 goto nla_put_failure
;
3171 memset(&tx_sa_stats
, 0, sizeof(tx_sa_stats
));
3172 get_tx_sa_stats(dev
, i
, tx_sa
, &tx_sa_stats
);
3173 if (copy_tx_sa_stats(skb
, &tx_sa_stats
)) {
3174 nla_nest_cancel(skb
, attr
);
3175 nla_nest_cancel(skb
, txsa_nest
);
3176 nla_nest_cancel(skb
, txsa_list
);
3177 goto nla_put_failure
;
3179 nla_nest_end(skb
, attr
);
3182 pn
= tx_sa
->next_pn
;
3183 pn_len
= MACSEC_XPN_PN_LEN
;
3185 pn
= tx_sa
->next_pn_halves
.lower
;
3186 pn_len
= MACSEC_DEFAULT_PN_LEN
;
3189 if (nla_put_u8(skb
, MACSEC_SA_ATTR_AN
, i
) ||
3190 nla_put(skb
, MACSEC_SA_ATTR_PN
, pn_len
, &pn
) ||
3191 nla_put(skb
, MACSEC_SA_ATTR_KEYID
, MACSEC_KEYID_LEN
, tx_sa
->key
.id
) ||
3192 (secy
->xpn
&& nla_put_ssci(skb
, MACSEC_SA_ATTR_SSCI
, tx_sa
->ssci
)) ||
3193 nla_put_u8(skb
, MACSEC_SA_ATTR_ACTIVE
, tx_sa
->active
)) {
3194 nla_nest_cancel(skb
, txsa_nest
);
3195 nla_nest_cancel(skb
, txsa_list
);
3196 goto nla_put_failure
;
3199 nla_nest_end(skb
, txsa_nest
);
3201 nla_nest_end(skb
, txsa_list
);
3203 rxsc_list
= nla_nest_start_noflag(skb
, MACSEC_ATTR_RXSC_LIST
);
3205 goto nla_put_failure
;
3208 for_each_rxsc_rtnl(secy
, rx_sc
) {
3210 struct nlattr
*rxsa_list
;
3211 struct nlattr
*rxsc_nest
= nla_nest_start_noflag(skb
, j
++);
3214 nla_nest_cancel(skb
, rxsc_list
);
3215 goto nla_put_failure
;
3218 if (nla_put_u8(skb
, MACSEC_RXSC_ATTR_ACTIVE
, rx_sc
->active
) ||
3219 nla_put_sci(skb
, MACSEC_RXSC_ATTR_SCI
, rx_sc
->sci
,
3220 MACSEC_RXSC_ATTR_PAD
)) {
3221 nla_nest_cancel(skb
, rxsc_nest
);
3222 nla_nest_cancel(skb
, rxsc_list
);
3223 goto nla_put_failure
;
3226 attr
= nla_nest_start_noflag(skb
, MACSEC_RXSC_ATTR_STATS
);
3228 nla_nest_cancel(skb
, rxsc_nest
);
3229 nla_nest_cancel(skb
, rxsc_list
);
3230 goto nla_put_failure
;
3232 memset(&rx_sc_stats
, 0, sizeof(rx_sc_stats
));
3233 get_rx_sc_stats(dev
, rx_sc
, &rx_sc_stats
);
3234 if (copy_rx_sc_stats(skb
, &rx_sc_stats
)) {
3235 nla_nest_cancel(skb
, attr
);
3236 nla_nest_cancel(skb
, rxsc_nest
);
3237 nla_nest_cancel(skb
, rxsc_list
);
3238 goto nla_put_failure
;
3240 nla_nest_end(skb
, attr
);
3242 rxsa_list
= nla_nest_start_noflag(skb
,
3243 MACSEC_RXSC_ATTR_SA_LIST
);
3245 nla_nest_cancel(skb
, rxsc_nest
);
3246 nla_nest_cancel(skb
, rxsc_list
);
3247 goto nla_put_failure
;
3250 for (i
= 0, k
= 1; i
< MACSEC_NUM_AN
; i
++) {
3251 struct macsec_rx_sa
*rx_sa
= rtnl_dereference(rx_sc
->sa
[i
]);
3252 struct nlattr
*rxsa_nest
;
3259 rxsa_nest
= nla_nest_start_noflag(skb
, k
++);
3261 nla_nest_cancel(skb
, rxsa_list
);
3262 nla_nest_cancel(skb
, rxsc_nest
);
3263 nla_nest_cancel(skb
, rxsc_list
);
3264 goto nla_put_failure
;
3267 attr
= nla_nest_start_noflag(skb
,
3268 MACSEC_SA_ATTR_STATS
);
3270 nla_nest_cancel(skb
, rxsa_list
);
3271 nla_nest_cancel(skb
, rxsc_nest
);
3272 nla_nest_cancel(skb
, rxsc_list
);
3273 goto nla_put_failure
;
3275 memset(&rx_sa_stats
, 0, sizeof(rx_sa_stats
));
3276 get_rx_sa_stats(dev
, rx_sc
, i
, rx_sa
, &rx_sa_stats
);
3277 if (copy_rx_sa_stats(skb
, &rx_sa_stats
)) {
3278 nla_nest_cancel(skb
, attr
);
3279 nla_nest_cancel(skb
, rxsa_list
);
3280 nla_nest_cancel(skb
, rxsc_nest
);
3281 nla_nest_cancel(skb
, rxsc_list
);
3282 goto nla_put_failure
;
3284 nla_nest_end(skb
, attr
);
3287 pn
= rx_sa
->next_pn
;
3288 pn_len
= MACSEC_XPN_PN_LEN
;
3290 pn
= rx_sa
->next_pn_halves
.lower
;
3291 pn_len
= MACSEC_DEFAULT_PN_LEN
;
3294 if (nla_put_u8(skb
, MACSEC_SA_ATTR_AN
, i
) ||
3295 nla_put(skb
, MACSEC_SA_ATTR_PN
, pn_len
, &pn
) ||
3296 nla_put(skb
, MACSEC_SA_ATTR_KEYID
, MACSEC_KEYID_LEN
, rx_sa
->key
.id
) ||
3297 (secy
->xpn
&& nla_put_ssci(skb
, MACSEC_SA_ATTR_SSCI
, rx_sa
->ssci
)) ||
3298 nla_put_u8(skb
, MACSEC_SA_ATTR_ACTIVE
, rx_sa
->active
)) {
3299 nla_nest_cancel(skb
, rxsa_nest
);
3300 nla_nest_cancel(skb
, rxsc_nest
);
3301 nla_nest_cancel(skb
, rxsc_list
);
3302 goto nla_put_failure
;
3304 nla_nest_end(skb
, rxsa_nest
);
3307 nla_nest_end(skb
, rxsa_list
);
3308 nla_nest_end(skb
, rxsc_nest
);
3311 nla_nest_end(skb
, rxsc_list
);
3313 genlmsg_end(skb
, hdr
);
3318 genlmsg_cancel(skb
, hdr
);
3322 static int macsec_generation
= 1; /* protected by RTNL */
3324 static int macsec_dump_txsc(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3326 struct net
*net
= sock_net(skb
->sk
);
3327 struct net_device
*dev
;
3330 dev_idx
= cb
->args
[0];
3335 cb
->seq
= macsec_generation
;
3337 for_each_netdev(net
, dev
) {
3338 struct macsec_secy
*secy
;
3343 if (!netif_is_macsec(dev
))
3346 secy
= &macsec_priv(dev
)->secy
;
3347 if (dump_secy(secy
, dev
, skb
, cb
) < 0)
3359 static const struct genl_small_ops macsec_genl_ops
[] = {
3361 .cmd
= MACSEC_CMD_GET_TXSC
,
3362 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3363 .dumpit
= macsec_dump_txsc
,
3366 .cmd
= MACSEC_CMD_ADD_RXSC
,
3367 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3368 .doit
= macsec_add_rxsc
,
3369 .flags
= GENL_ADMIN_PERM
,
3372 .cmd
= MACSEC_CMD_DEL_RXSC
,
3373 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3374 .doit
= macsec_del_rxsc
,
3375 .flags
= GENL_ADMIN_PERM
,
3378 .cmd
= MACSEC_CMD_UPD_RXSC
,
3379 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3380 .doit
= macsec_upd_rxsc
,
3381 .flags
= GENL_ADMIN_PERM
,
3384 .cmd
= MACSEC_CMD_ADD_TXSA
,
3385 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3386 .doit
= macsec_add_txsa
,
3387 .flags
= GENL_ADMIN_PERM
,
3390 .cmd
= MACSEC_CMD_DEL_TXSA
,
3391 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3392 .doit
= macsec_del_txsa
,
3393 .flags
= GENL_ADMIN_PERM
,
3396 .cmd
= MACSEC_CMD_UPD_TXSA
,
3397 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3398 .doit
= macsec_upd_txsa
,
3399 .flags
= GENL_ADMIN_PERM
,
3402 .cmd
= MACSEC_CMD_ADD_RXSA
,
3403 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3404 .doit
= macsec_add_rxsa
,
3405 .flags
= GENL_ADMIN_PERM
,
3408 .cmd
= MACSEC_CMD_DEL_RXSA
,
3409 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3410 .doit
= macsec_del_rxsa
,
3411 .flags
= GENL_ADMIN_PERM
,
3414 .cmd
= MACSEC_CMD_UPD_RXSA
,
3415 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3416 .doit
= macsec_upd_rxsa
,
3417 .flags
= GENL_ADMIN_PERM
,
3420 .cmd
= MACSEC_CMD_UPD_OFFLOAD
,
3421 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3422 .doit
= macsec_upd_offload
,
3423 .flags
= GENL_ADMIN_PERM
,
3427 static struct genl_family macsec_fam __ro_after_init
= {
3428 .name
= MACSEC_GENL_NAME
,
3430 .version
= MACSEC_GENL_VERSION
,
3431 .maxattr
= MACSEC_ATTR_MAX
,
3432 .policy
= macsec_genl_policy
,
3434 .module
= THIS_MODULE
,
3435 .small_ops
= macsec_genl_ops
,
3436 .n_small_ops
= ARRAY_SIZE(macsec_genl_ops
),
3437 .resv_start_op
= MACSEC_CMD_UPD_OFFLOAD
+ 1,
3440 static struct sk_buff
*macsec_insert_tx_tag(struct sk_buff
*skb
,
3441 struct net_device
*dev
)
3443 struct macsec_dev
*macsec
= macsec_priv(dev
);
3444 const struct macsec_ops
*ops
;
3445 struct phy_device
*phydev
;
3446 struct macsec_context ctx
;
3450 ops
= macsec_get_ops(macsec
, &ctx
);
3451 skb_final_len
= skb
->len
- ETH_HLEN
+ ops
->needed_headroom
+
3452 ops
->needed_tailroom
;
3453 if (unlikely(skb_final_len
> macsec
->real_dev
->mtu
)) {
3458 phydev
= macsec
->real_dev
->phydev
;
3460 err
= skb_ensure_writable_head_tail(skb
, dev
);
3461 if (unlikely(err
< 0))
3464 err
= ops
->mdo_insert_tx_tag(phydev
, skb
);
3471 return ERR_PTR(err
);
3474 static netdev_tx_t
macsec_start_xmit(struct sk_buff
*skb
,
3475 struct net_device
*dev
)
3477 struct macsec_dev
*macsec
= netdev_priv(dev
);
3478 struct macsec_secy
*secy
= &macsec
->secy
;
3479 struct pcpu_secy_stats
*secy_stats
;
3482 if (macsec_is_offloaded(netdev_priv(dev
))) {
3483 struct metadata_dst
*md_dst
= secy
->tx_sc
.md_dst
;
3486 dst_hold(&md_dst
->dst
);
3487 skb_dst_set(skb
, &md_dst
->dst
);
3489 if (macsec
->insert_tx_tag
) {
3490 skb
= macsec_insert_tx_tag(skb
, dev
);
3492 DEV_STATS_INC(dev
, tx_dropped
);
3493 return NETDEV_TX_OK
;
3497 skb
->dev
= macsec
->real_dev
;
3498 return dev_queue_xmit(skb
);
3502 if (!secy
->protect_frames
) {
3503 secy_stats
= this_cpu_ptr(macsec
->stats
);
3504 u64_stats_update_begin(&secy_stats
->syncp
);
3505 secy_stats
->stats
.OutPktsUntagged
++;
3506 u64_stats_update_end(&secy_stats
->syncp
);
3507 skb
->dev
= macsec
->real_dev
;
3509 ret
= dev_queue_xmit(skb
);
3510 count_tx(dev
, ret
, len
);
3514 if (!secy
->operational
) {
3516 DEV_STATS_INC(dev
, tx_dropped
);
3517 return NETDEV_TX_OK
;
3521 skb
= macsec_encrypt(skb
, dev
);
3523 if (PTR_ERR(skb
) != -EINPROGRESS
)
3524 DEV_STATS_INC(dev
, tx_dropped
);
3525 return NETDEV_TX_OK
;
3528 macsec_count_tx(skb
, &macsec
->secy
.tx_sc
, macsec_skb_cb(skb
)->tx_sa
);
3530 macsec_encrypt_finish(skb
, dev
);
3531 ret
= dev_queue_xmit(skb
);
3532 count_tx(dev
, ret
, len
);
3536 #define MACSEC_FEATURES \
3537 (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
3539 #define MACSEC_OFFLOAD_FEATURES \
3540 (MACSEC_FEATURES | NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES | \
3541 NETIF_F_LRO | NETIF_F_RXHASH | NETIF_F_CSUM_MASK | NETIF_F_RXCSUM)
3543 static int macsec_dev_init(struct net_device
*dev
)
3545 struct macsec_dev
*macsec
= macsec_priv(dev
);
3546 struct net_device
*real_dev
= macsec
->real_dev
;
3549 err
= gro_cells_init(&macsec
->gro_cells
, dev
);
3553 macsec_inherit_tso_max(dev
);
3555 dev
->hw_features
= real_dev
->hw_features
& MACSEC_OFFLOAD_FEATURES
;
3556 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
3558 dev
->features
= real_dev
->features
& MACSEC_OFFLOAD_FEATURES
;
3559 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
3561 dev
->pcpu_stat_type
= NETDEV_PCPU_STAT_TSTATS
;
3563 macsec_set_head_tail_room(dev
);
3565 if (is_zero_ether_addr(dev
->dev_addr
))
3566 eth_hw_addr_inherit(dev
, real_dev
);
3567 if (is_zero_ether_addr(dev
->broadcast
))
3568 memcpy(dev
->broadcast
, real_dev
->broadcast
, dev
->addr_len
);
3570 /* Get macsec's reference to real_dev */
3571 netdev_hold(real_dev
, &macsec
->dev_tracker
, GFP_KERNEL
);
3576 static void macsec_dev_uninit(struct net_device
*dev
)
3578 struct macsec_dev
*macsec
= macsec_priv(dev
);
3580 gro_cells_destroy(&macsec
->gro_cells
);
3583 static netdev_features_t
macsec_fix_features(struct net_device
*dev
,
3584 netdev_features_t features
)
3586 struct macsec_dev
*macsec
= macsec_priv(dev
);
3587 struct net_device
*real_dev
= macsec
->real_dev
;
3588 netdev_features_t mask
;
3590 mask
= macsec_is_offloaded(macsec
) ? MACSEC_OFFLOAD_FEATURES
3593 features
&= (real_dev
->features
& mask
) |
3594 NETIF_F_GSO_SOFTWARE
| NETIF_F_SOFT_FEATURES
;
3599 static int macsec_dev_open(struct net_device
*dev
)
3601 struct macsec_dev
*macsec
= macsec_priv(dev
);
3602 struct net_device
*real_dev
= macsec
->real_dev
;
3605 err
= dev_uc_add(real_dev
, dev
->dev_addr
);
3609 if (dev
->flags
& IFF_ALLMULTI
) {
3610 err
= dev_set_allmulti(real_dev
, 1);
3615 if (dev
->flags
& IFF_PROMISC
) {
3616 err
= dev_set_promiscuity(real_dev
, 1);
3618 goto clear_allmulti
;
3621 /* If h/w offloading is available, propagate to the device */
3622 if (macsec_is_offloaded(macsec
)) {
3623 const struct macsec_ops
*ops
;
3624 struct macsec_context ctx
;
3626 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
3629 goto clear_allmulti
;
3632 ctx
.secy
= &macsec
->secy
;
3633 err
= macsec_offload(ops
->mdo_dev_open
, &ctx
);
3635 goto clear_allmulti
;
3638 if (netif_carrier_ok(real_dev
))
3639 netif_carrier_on(dev
);
3643 if (dev
->flags
& IFF_ALLMULTI
)
3644 dev_set_allmulti(real_dev
, -1);
3646 dev_uc_del(real_dev
, dev
->dev_addr
);
3647 netif_carrier_off(dev
);
3651 static int macsec_dev_stop(struct net_device
*dev
)
3653 struct macsec_dev
*macsec
= macsec_priv(dev
);
3654 struct net_device
*real_dev
= macsec
->real_dev
;
3656 netif_carrier_off(dev
);
3658 /* If h/w offloading is available, propagate to the device */
3659 if (macsec_is_offloaded(macsec
)) {
3660 const struct macsec_ops
*ops
;
3661 struct macsec_context ctx
;
3663 ops
= macsec_get_ops(macsec
, &ctx
);
3665 ctx
.secy
= &macsec
->secy
;
3666 macsec_offload(ops
->mdo_dev_stop
, &ctx
);
3670 dev_mc_unsync(real_dev
, dev
);
3671 dev_uc_unsync(real_dev
, dev
);
3673 if (dev
->flags
& IFF_ALLMULTI
)
3674 dev_set_allmulti(real_dev
, -1);
3676 if (dev
->flags
& IFF_PROMISC
)
3677 dev_set_promiscuity(real_dev
, -1);
3679 dev_uc_del(real_dev
, dev
->dev_addr
);
3684 static void macsec_dev_change_rx_flags(struct net_device
*dev
, int change
)
3686 struct net_device
*real_dev
= macsec_priv(dev
)->real_dev
;
3688 if (!(dev
->flags
& IFF_UP
))
3691 if (change
& IFF_ALLMULTI
)
3692 dev_set_allmulti(real_dev
, dev
->flags
& IFF_ALLMULTI
? 1 : -1);
3694 if (change
& IFF_PROMISC
)
3695 dev_set_promiscuity(real_dev
,
3696 dev
->flags
& IFF_PROMISC
? 1 : -1);
3699 static void macsec_dev_set_rx_mode(struct net_device
*dev
)
3701 struct net_device
*real_dev
= macsec_priv(dev
)->real_dev
;
3703 dev_mc_sync(real_dev
, dev
);
3704 dev_uc_sync(real_dev
, dev
);
3707 static int macsec_set_mac_address(struct net_device
*dev
, void *p
)
3709 struct macsec_dev
*macsec
= macsec_priv(dev
);
3710 struct net_device
*real_dev
= macsec
->real_dev
;
3711 struct sockaddr
*addr
= p
;
3712 u8 old_addr
[ETH_ALEN
];
3715 if (!is_valid_ether_addr(addr
->sa_data
))
3716 return -EADDRNOTAVAIL
;
3718 if (dev
->flags
& IFF_UP
) {
3719 err
= dev_uc_add(real_dev
, addr
->sa_data
);
3724 ether_addr_copy(old_addr
, dev
->dev_addr
);
3725 eth_hw_addr_set(dev
, addr
->sa_data
);
3727 /* If h/w offloading is available, propagate to the device */
3728 if (macsec_is_offloaded(macsec
)) {
3729 const struct macsec_ops
*ops
;
3730 struct macsec_context ctx
;
3732 ops
= macsec_get_ops(macsec
, &ctx
);
3735 goto restore_old_addr
;
3738 ctx
.secy
= &macsec
->secy
;
3739 err
= macsec_offload(ops
->mdo_upd_secy
, &ctx
);
3741 goto restore_old_addr
;
3744 if (dev
->flags
& IFF_UP
)
3745 dev_uc_del(real_dev
, old_addr
);
3750 if (dev
->flags
& IFF_UP
)
3751 dev_uc_del(real_dev
, addr
->sa_data
);
3753 eth_hw_addr_set(dev
, old_addr
);
3758 static int macsec_change_mtu(struct net_device
*dev
, int new_mtu
)
3760 struct macsec_dev
*macsec
= macsec_priv(dev
);
3761 unsigned int extra
= macsec
->secy
.icv_len
+ macsec_extra_len(true);
3763 if (macsec
->real_dev
->mtu
- extra
< new_mtu
)
3766 WRITE_ONCE(dev
->mtu
, new_mtu
);
3771 static void macsec_get_stats64(struct net_device
*dev
,
3772 struct rtnl_link_stats64
*s
)
3777 dev_fetch_sw_netstats(s
, dev
->tstats
);
3779 s
->rx_dropped
= DEV_STATS_READ(dev
, rx_dropped
);
3780 s
->tx_dropped
= DEV_STATS_READ(dev
, tx_dropped
);
3781 s
->rx_errors
= DEV_STATS_READ(dev
, rx_errors
);
3784 static int macsec_get_iflink(const struct net_device
*dev
)
3786 return READ_ONCE(macsec_priv(dev
)->real_dev
->ifindex
);
3789 static const struct net_device_ops macsec_netdev_ops
= {
3790 .ndo_init
= macsec_dev_init
,
3791 .ndo_uninit
= macsec_dev_uninit
,
3792 .ndo_open
= macsec_dev_open
,
3793 .ndo_stop
= macsec_dev_stop
,
3794 .ndo_fix_features
= macsec_fix_features
,
3795 .ndo_change_mtu
= macsec_change_mtu
,
3796 .ndo_set_rx_mode
= macsec_dev_set_rx_mode
,
3797 .ndo_change_rx_flags
= macsec_dev_change_rx_flags
,
3798 .ndo_set_mac_address
= macsec_set_mac_address
,
3799 .ndo_start_xmit
= macsec_start_xmit
,
3800 .ndo_get_stats64
= macsec_get_stats64
,
3801 .ndo_get_iflink
= macsec_get_iflink
,
3804 static const struct device_type macsec_type
= {
3808 static const struct nla_policy macsec_rtnl_policy
[IFLA_MACSEC_MAX
+ 1] = {
3809 [IFLA_MACSEC_SCI
] = { .type
= NLA_U64
},
3810 [IFLA_MACSEC_PORT
] = { .type
= NLA_U16
},
3811 [IFLA_MACSEC_ICV_LEN
] = { .type
= NLA_U8
},
3812 [IFLA_MACSEC_CIPHER_SUITE
] = { .type
= NLA_U64
},
3813 [IFLA_MACSEC_WINDOW
] = { .type
= NLA_U32
},
3814 [IFLA_MACSEC_ENCODING_SA
] = { .type
= NLA_U8
},
3815 [IFLA_MACSEC_ENCRYPT
] = { .type
= NLA_U8
},
3816 [IFLA_MACSEC_PROTECT
] = { .type
= NLA_U8
},
3817 [IFLA_MACSEC_INC_SCI
] = { .type
= NLA_U8
},
3818 [IFLA_MACSEC_ES
] = { .type
= NLA_U8
},
3819 [IFLA_MACSEC_SCB
] = { .type
= NLA_U8
},
3820 [IFLA_MACSEC_REPLAY_PROTECT
] = { .type
= NLA_U8
},
3821 [IFLA_MACSEC_VALIDATION
] = { .type
= NLA_U8
},
3822 [IFLA_MACSEC_OFFLOAD
] = { .type
= NLA_U8
},
3825 static void macsec_free_netdev(struct net_device
*dev
)
3827 struct macsec_dev
*macsec
= macsec_priv(dev
);
3829 dst_release(&macsec
->secy
.tx_sc
.md_dst
->dst
);
3830 free_percpu(macsec
->stats
);
3831 free_percpu(macsec
->secy
.tx_sc
.stats
);
3833 /* Get rid of the macsec's reference to real_dev */
3834 netdev_put(macsec
->real_dev
, &macsec
->dev_tracker
);
3837 static void macsec_setup(struct net_device
*dev
)
3841 dev
->max_mtu
= ETH_MAX_MTU
;
3842 dev
->priv_flags
|= IFF_NO_QUEUE
;
3843 dev
->netdev_ops
= &macsec_netdev_ops
;
3844 dev
->needs_free_netdev
= true;
3845 dev
->priv_destructor
= macsec_free_netdev
;
3846 SET_NETDEV_DEVTYPE(dev
, &macsec_type
);
3848 eth_zero_addr(dev
->broadcast
);
3851 static int macsec_changelink_common(struct net_device
*dev
,
3852 struct nlattr
*data
[])
3854 struct macsec_secy
*secy
;
3855 struct macsec_tx_sc
*tx_sc
;
3857 secy
= &macsec_priv(dev
)->secy
;
3858 tx_sc
= &secy
->tx_sc
;
3860 if (data
[IFLA_MACSEC_ENCODING_SA
]) {
3861 struct macsec_tx_sa
*tx_sa
;
3863 tx_sc
->encoding_sa
= nla_get_u8(data
[IFLA_MACSEC_ENCODING_SA
]);
3864 tx_sa
= rtnl_dereference(tx_sc
->sa
[tx_sc
->encoding_sa
]);
3866 secy
->operational
= tx_sa
&& tx_sa
->active
;
3869 if (data
[IFLA_MACSEC_ENCRYPT
])
3870 tx_sc
->encrypt
= !!nla_get_u8(data
[IFLA_MACSEC_ENCRYPT
]);
3872 if (data
[IFLA_MACSEC_PROTECT
])
3873 secy
->protect_frames
= !!nla_get_u8(data
[IFLA_MACSEC_PROTECT
]);
3875 if (data
[IFLA_MACSEC_INC_SCI
])
3876 tx_sc
->send_sci
= !!nla_get_u8(data
[IFLA_MACSEC_INC_SCI
]);
3878 if (data
[IFLA_MACSEC_ES
])
3879 tx_sc
->end_station
= !!nla_get_u8(data
[IFLA_MACSEC_ES
]);
3881 if (data
[IFLA_MACSEC_SCB
])
3882 tx_sc
->scb
= !!nla_get_u8(data
[IFLA_MACSEC_SCB
]);
3884 if (data
[IFLA_MACSEC_REPLAY_PROTECT
])
3885 secy
->replay_protect
= !!nla_get_u8(data
[IFLA_MACSEC_REPLAY_PROTECT
]);
3887 if (data
[IFLA_MACSEC_VALIDATION
])
3888 secy
->validate_frames
= nla_get_u8(data
[IFLA_MACSEC_VALIDATION
]);
3890 if (data
[IFLA_MACSEC_CIPHER_SUITE
]) {
3891 switch (nla_get_u64(data
[IFLA_MACSEC_CIPHER_SUITE
])) {
3892 case MACSEC_CIPHER_ID_GCM_AES_128
:
3893 case MACSEC_DEFAULT_CIPHER_ID
:
3894 secy
->key_len
= MACSEC_GCM_AES_128_SAK_LEN
;
3897 case MACSEC_CIPHER_ID_GCM_AES_256
:
3898 secy
->key_len
= MACSEC_GCM_AES_256_SAK_LEN
;
3901 case MACSEC_CIPHER_ID_GCM_AES_XPN_128
:
3902 secy
->key_len
= MACSEC_GCM_AES_128_SAK_LEN
;
3905 case MACSEC_CIPHER_ID_GCM_AES_XPN_256
:
3906 secy
->key_len
= MACSEC_GCM_AES_256_SAK_LEN
;
3914 if (data
[IFLA_MACSEC_WINDOW
]) {
3915 secy
->replay_window
= nla_get_u32(data
[IFLA_MACSEC_WINDOW
]);
3917 /* IEEE 802.1AEbw-2013 10.7.8 - maximum replay window
3918 * for XPN cipher suites */
3920 secy
->replay_window
> MACSEC_XPN_MAX_REPLAY_WINDOW
)
3927 static int macsec_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
3928 struct nlattr
*data
[],
3929 struct netlink_ext_ack
*extack
)
3931 struct macsec_dev
*macsec
= macsec_priv(dev
);
3932 bool macsec_offload_state_change
= false;
3933 enum macsec_offload offload
;
3934 struct macsec_tx_sc tx_sc
;
3935 struct macsec_secy secy
;
3941 if (data
[IFLA_MACSEC_CIPHER_SUITE
] ||
3942 data
[IFLA_MACSEC_ICV_LEN
] ||
3943 data
[IFLA_MACSEC_SCI
] ||
3944 data
[IFLA_MACSEC_PORT
])
3947 /* Keep a copy of unmodified secy and tx_sc, in case the offload
3948 * propagation fails, to revert macsec_changelink_common.
3950 memcpy(&secy
, &macsec
->secy
, sizeof(secy
));
3951 memcpy(&tx_sc
, &macsec
->secy
.tx_sc
, sizeof(tx_sc
));
3953 ret
= macsec_changelink_common(dev
, data
);
3957 if (data
[IFLA_MACSEC_OFFLOAD
]) {
3958 offload
= nla_get_u8(data
[IFLA_MACSEC_OFFLOAD
]);
3959 if (macsec
->offload
!= offload
) {
3960 macsec_offload_state_change
= true;
3961 ret
= macsec_update_offload(dev
, offload
);
3967 /* If h/w offloading is available, propagate to the device */
3968 if (!macsec_offload_state_change
&& macsec_is_offloaded(macsec
)) {
3969 const struct macsec_ops
*ops
;
3970 struct macsec_context ctx
;
3972 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
3978 ctx
.secy
= &macsec
->secy
;
3979 ret
= macsec_offload(ops
->mdo_upd_secy
, &ctx
);
3987 memcpy(&macsec
->secy
.tx_sc
, &tx_sc
, sizeof(tx_sc
));
3988 memcpy(&macsec
->secy
, &secy
, sizeof(secy
));
3993 static void macsec_del_dev(struct macsec_dev
*macsec
)
3997 while (macsec
->secy
.rx_sc
) {
3998 struct macsec_rx_sc
*rx_sc
= rtnl_dereference(macsec
->secy
.rx_sc
);
4000 rcu_assign_pointer(macsec
->secy
.rx_sc
, rx_sc
->next
);
4004 for (i
= 0; i
< MACSEC_NUM_AN
; i
++) {
4005 struct macsec_tx_sa
*sa
= rtnl_dereference(macsec
->secy
.tx_sc
.sa
[i
]);
4008 RCU_INIT_POINTER(macsec
->secy
.tx_sc
.sa
[i
], NULL
);
4014 static void macsec_common_dellink(struct net_device
*dev
, struct list_head
*head
)
4016 struct macsec_dev
*macsec
= macsec_priv(dev
);
4017 struct net_device
*real_dev
= macsec
->real_dev
;
4019 /* If h/w offloading is available, propagate to the device */
4020 if (macsec_is_offloaded(macsec
)) {
4021 const struct macsec_ops
*ops
;
4022 struct macsec_context ctx
;
4024 ops
= macsec_get_ops(netdev_priv(dev
), &ctx
);
4026 ctx
.secy
= &macsec
->secy
;
4027 macsec_offload(ops
->mdo_del_secy
, &ctx
);
4031 unregister_netdevice_queue(dev
, head
);
4032 list_del_rcu(&macsec
->secys
);
4033 macsec_del_dev(macsec
);
4034 netdev_upper_dev_unlink(real_dev
, dev
);
4036 macsec_generation
++;
4039 static void macsec_dellink(struct net_device
*dev
, struct list_head
*head
)
4041 struct macsec_dev
*macsec
= macsec_priv(dev
);
4042 struct net_device
*real_dev
= macsec
->real_dev
;
4043 struct macsec_rxh_data
*rxd
= macsec_data_rtnl(real_dev
);
4045 macsec_common_dellink(dev
, head
);
4047 if (list_empty(&rxd
->secys
)) {
4048 netdev_rx_handler_unregister(real_dev
);
4053 static int register_macsec_dev(struct net_device
*real_dev
,
4054 struct net_device
*dev
)
4056 struct macsec_dev
*macsec
= macsec_priv(dev
);
4057 struct macsec_rxh_data
*rxd
= macsec_data_rtnl(real_dev
);
4062 rxd
= kmalloc(sizeof(*rxd
), GFP_KERNEL
);
4066 INIT_LIST_HEAD(&rxd
->secys
);
4068 err
= netdev_rx_handler_register(real_dev
, macsec_handle_frame
,
4076 list_add_tail_rcu(&macsec
->secys
, &rxd
->secys
);
4080 static bool sci_exists(struct net_device
*dev
, sci_t sci
)
4082 struct macsec_rxh_data
*rxd
= macsec_data_rtnl(dev
);
4083 struct macsec_dev
*macsec
;
4085 list_for_each_entry(macsec
, &rxd
->secys
, secys
) {
4086 if (macsec
->secy
.sci
== sci
)
4093 static sci_t
dev_to_sci(struct net_device
*dev
, __be16 port
)
4095 return make_sci(dev
->dev_addr
, port
);
4098 static int macsec_add_dev(struct net_device
*dev
, sci_t sci
, u8 icv_len
)
4100 struct macsec_dev
*macsec
= macsec_priv(dev
);
4101 struct macsec_secy
*secy
= &macsec
->secy
;
4103 macsec
->stats
= netdev_alloc_pcpu_stats(struct pcpu_secy_stats
);
4107 secy
->tx_sc
.stats
= netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats
);
4108 if (!secy
->tx_sc
.stats
)
4111 secy
->tx_sc
.md_dst
= metadata_dst_alloc(0, METADATA_MACSEC
, GFP_KERNEL
);
4112 if (!secy
->tx_sc
.md_dst
)
4113 /* macsec and secy percpu stats will be freed when unregistering
4114 * net_device in macsec_free_netdev()
4118 if (sci
== MACSEC_UNDEF_SCI
)
4119 sci
= dev_to_sci(dev
, MACSEC_PORT_ES
);
4122 secy
->operational
= true;
4123 secy
->key_len
= DEFAULT_SAK_LEN
;
4124 secy
->icv_len
= icv_len
;
4125 secy
->validate_frames
= MACSEC_VALIDATE_DEFAULT
;
4126 secy
->protect_frames
= true;
4127 secy
->replay_protect
= false;
4128 secy
->xpn
= DEFAULT_XPN
;
4131 secy
->tx_sc
.md_dst
->u
.macsec_info
.sci
= sci
;
4132 secy
->tx_sc
.active
= true;
4133 secy
->tx_sc
.encoding_sa
= DEFAULT_ENCODING_SA
;
4134 secy
->tx_sc
.encrypt
= DEFAULT_ENCRYPT
;
4135 secy
->tx_sc
.send_sci
= DEFAULT_SEND_SCI
;
4136 secy
->tx_sc
.end_station
= false;
4137 secy
->tx_sc
.scb
= false;
4142 static struct lock_class_key macsec_netdev_addr_lock_key
;
4144 static int macsec_newlink(struct net
*net
, struct net_device
*dev
,
4145 struct nlattr
*tb
[], struct nlattr
*data
[],
4146 struct netlink_ext_ack
*extack
)
4148 struct macsec_dev
*macsec
= macsec_priv(dev
);
4149 rx_handler_func_t
*rx_handler
;
4150 u8 icv_len
= MACSEC_DEFAULT_ICV_LEN
;
4151 struct net_device
*real_dev
;
4157 real_dev
= __dev_get_by_index(net
, nla_get_u32(tb
[IFLA_LINK
]));
4160 if (real_dev
->type
!= ARPHRD_ETHER
)
4163 dev
->priv_flags
|= IFF_MACSEC
;
4165 macsec
->real_dev
= real_dev
;
4167 if (data
&& data
[IFLA_MACSEC_OFFLOAD
])
4168 macsec
->offload
= nla_get_offload(data
[IFLA_MACSEC_OFFLOAD
]);
4170 /* MACsec offloading is off by default */
4171 macsec
->offload
= MACSEC_OFFLOAD_OFF
;
4173 /* Check if the offloading mode is supported by the underlying layers */
4174 if (macsec
->offload
!= MACSEC_OFFLOAD_OFF
&&
4175 !macsec_check_offload(macsec
->offload
, macsec
))
4178 /* send_sci must be set to true when transmit sci explicitly is set */
4179 if ((data
&& data
[IFLA_MACSEC_SCI
]) &&
4180 (data
&& data
[IFLA_MACSEC_INC_SCI
])) {
4181 u8 send_sci
= !!nla_get_u8(data
[IFLA_MACSEC_INC_SCI
]);
4187 if (data
&& data
[IFLA_MACSEC_ICV_LEN
])
4188 icv_len
= nla_get_u8(data
[IFLA_MACSEC_ICV_LEN
]);
4189 mtu
= real_dev
->mtu
- icv_len
- macsec_extra_len(true);
4195 rx_handler
= rtnl_dereference(real_dev
->rx_handler
);
4196 if (rx_handler
&& rx_handler
!= macsec_handle_frame
)
4199 err
= register_netdevice(dev
);
4203 netdev_lockdep_set_classes(dev
);
4204 lockdep_set_class(&dev
->addr_list_lock
,
4205 &macsec_netdev_addr_lock_key
);
4207 err
= netdev_upper_dev_link(real_dev
, dev
, extack
);
4211 /* need to be already registered so that ->init has run and
4212 * the MAC addr is set
4214 if (data
&& data
[IFLA_MACSEC_SCI
])
4215 sci
= nla_get_sci(data
[IFLA_MACSEC_SCI
]);
4216 else if (data
&& data
[IFLA_MACSEC_PORT
])
4217 sci
= dev_to_sci(dev
, nla_get_be16(data
[IFLA_MACSEC_PORT
]));
4219 sci
= dev_to_sci(dev
, MACSEC_PORT_ES
);
4221 if (rx_handler
&& sci_exists(real_dev
, sci
)) {
4226 err
= macsec_add_dev(dev
, sci
, icv_len
);
4231 err
= macsec_changelink_common(dev
, data
);
4236 /* If h/w offloading is available, propagate to the device */
4237 if (macsec_is_offloaded(macsec
)) {
4238 const struct macsec_ops
*ops
;
4239 struct macsec_context ctx
;
4241 ops
= macsec_get_ops(macsec
, &ctx
);
4243 ctx
.secy
= &macsec
->secy
;
4244 err
= macsec_offload(ops
->mdo_add_secy
, &ctx
);
4248 macsec
->insert_tx_tag
=
4249 macsec_needs_tx_tag(macsec
, ops
);
4253 err
= register_macsec_dev(real_dev
, dev
);
4257 netif_stacked_transfer_operstate(real_dev
, dev
);
4258 linkwatch_fire_event(dev
);
4260 macsec_generation
++;
4265 macsec_del_dev(macsec
);
4267 netdev_upper_dev_unlink(real_dev
, dev
);
4269 unregister_netdevice(dev
);
4273 static int macsec_validate_attr(struct nlattr
*tb
[], struct nlattr
*data
[],
4274 struct netlink_ext_ack
*extack
)
4276 u64 csid
= MACSEC_DEFAULT_CIPHER_ID
;
4277 u8 icv_len
= MACSEC_DEFAULT_ICV_LEN
;
4284 if (data
[IFLA_MACSEC_CIPHER_SUITE
])
4285 csid
= nla_get_u64(data
[IFLA_MACSEC_CIPHER_SUITE
]);
4287 if (data
[IFLA_MACSEC_ICV_LEN
]) {
4288 icv_len
= nla_get_u8(data
[IFLA_MACSEC_ICV_LEN
]);
4289 if (icv_len
!= MACSEC_DEFAULT_ICV_LEN
) {
4290 char dummy_key
[DEFAULT_SAK_LEN
] = { 0 };
4291 struct crypto_aead
*dummy_tfm
;
4293 dummy_tfm
= macsec_alloc_tfm(dummy_key
,
4296 if (IS_ERR(dummy_tfm
))
4297 return PTR_ERR(dummy_tfm
);
4298 crypto_free_aead(dummy_tfm
);
4303 case MACSEC_CIPHER_ID_GCM_AES_128
:
4304 case MACSEC_CIPHER_ID_GCM_AES_256
:
4305 case MACSEC_CIPHER_ID_GCM_AES_XPN_128
:
4306 case MACSEC_CIPHER_ID_GCM_AES_XPN_256
:
4307 case MACSEC_DEFAULT_CIPHER_ID
:
4308 if (icv_len
< MACSEC_MIN_ICV_LEN
||
4309 icv_len
> MACSEC_STD_ICV_LEN
)
4316 if (data
[IFLA_MACSEC_ENCODING_SA
]) {
4317 if (nla_get_u8(data
[IFLA_MACSEC_ENCODING_SA
]) >= MACSEC_NUM_AN
)
4321 for (flag
= IFLA_MACSEC_ENCODING_SA
+ 1;
4322 flag
< IFLA_MACSEC_VALIDATION
;
4325 if (nla_get_u8(data
[flag
]) > 1)
4330 es
= nla_get_u8_default(data
[IFLA_MACSEC_ES
], false);
4331 sci
= nla_get_u8_default(data
[IFLA_MACSEC_INC_SCI
], false);
4332 scb
= nla_get_u8_default(data
[IFLA_MACSEC_SCB
], false);
4334 if ((sci
&& (scb
|| es
)) || (scb
&& es
))
4337 if (data
[IFLA_MACSEC_VALIDATION
] &&
4338 nla_get_u8(data
[IFLA_MACSEC_VALIDATION
]) > MACSEC_VALIDATE_MAX
)
4341 if ((data
[IFLA_MACSEC_REPLAY_PROTECT
] &&
4342 nla_get_u8(data
[IFLA_MACSEC_REPLAY_PROTECT
])) &&
4343 !data
[IFLA_MACSEC_WINDOW
])
4349 static struct net
*macsec_get_link_net(const struct net_device
*dev
)
4351 return dev_net(macsec_priv(dev
)->real_dev
);
4354 struct net_device
*macsec_get_real_dev(const struct net_device
*dev
)
4356 return macsec_priv(dev
)->real_dev
;
4358 EXPORT_SYMBOL_GPL(macsec_get_real_dev
);
4360 bool macsec_netdev_is_offloaded(struct net_device
*dev
)
4362 return macsec_is_offloaded(macsec_priv(dev
));
4364 EXPORT_SYMBOL_GPL(macsec_netdev_is_offloaded
);
4366 static size_t macsec_get_size(const struct net_device
*dev
)
4368 return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
4369 nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
4370 nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
4371 nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
4372 nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
4373 nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
4374 nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
4375 nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
4376 nla_total_size(1) + /* IFLA_MACSEC_ES */
4377 nla_total_size(1) + /* IFLA_MACSEC_SCB */
4378 nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
4379 nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
4380 nla_total_size(1) + /* IFLA_MACSEC_OFFLOAD */
4384 static int macsec_fill_info(struct sk_buff
*skb
,
4385 const struct net_device
*dev
)
4387 struct macsec_tx_sc
*tx_sc
;
4388 struct macsec_dev
*macsec
;
4389 struct macsec_secy
*secy
;
4392 macsec
= macsec_priv(dev
);
4393 secy
= &macsec
->secy
;
4394 tx_sc
= &secy
->tx_sc
;
4396 switch (secy
->key_len
) {
4397 case MACSEC_GCM_AES_128_SAK_LEN
:
4398 csid
= secy
->xpn
? MACSEC_CIPHER_ID_GCM_AES_XPN_128
: MACSEC_DEFAULT_CIPHER_ID
;
4400 case MACSEC_GCM_AES_256_SAK_LEN
:
4401 csid
= secy
->xpn
? MACSEC_CIPHER_ID_GCM_AES_XPN_256
: MACSEC_CIPHER_ID_GCM_AES_256
;
4404 goto nla_put_failure
;
4407 if (nla_put_sci(skb
, IFLA_MACSEC_SCI
, secy
->sci
,
4409 nla_put_u8(skb
, IFLA_MACSEC_ICV_LEN
, secy
->icv_len
) ||
4410 nla_put_u64_64bit(skb
, IFLA_MACSEC_CIPHER_SUITE
,
4411 csid
, IFLA_MACSEC_PAD
) ||
4412 nla_put_u8(skb
, IFLA_MACSEC_ENCODING_SA
, tx_sc
->encoding_sa
) ||
4413 nla_put_u8(skb
, IFLA_MACSEC_ENCRYPT
, tx_sc
->encrypt
) ||
4414 nla_put_u8(skb
, IFLA_MACSEC_PROTECT
, secy
->protect_frames
) ||
4415 nla_put_u8(skb
, IFLA_MACSEC_INC_SCI
, tx_sc
->send_sci
) ||
4416 nla_put_u8(skb
, IFLA_MACSEC_ES
, tx_sc
->end_station
) ||
4417 nla_put_u8(skb
, IFLA_MACSEC_SCB
, tx_sc
->scb
) ||
4418 nla_put_u8(skb
, IFLA_MACSEC_REPLAY_PROTECT
, secy
->replay_protect
) ||
4419 nla_put_u8(skb
, IFLA_MACSEC_VALIDATION
, secy
->validate_frames
) ||
4420 nla_put_u8(skb
, IFLA_MACSEC_OFFLOAD
, macsec
->offload
) ||
4422 goto nla_put_failure
;
4424 if (secy
->replay_protect
) {
4425 if (nla_put_u32(skb
, IFLA_MACSEC_WINDOW
, secy
->replay_window
))
4426 goto nla_put_failure
;
4435 static struct rtnl_link_ops macsec_link_ops __read_mostly
= {
4437 .priv_size
= sizeof(struct macsec_dev
),
4438 .maxtype
= IFLA_MACSEC_MAX
,
4439 .policy
= macsec_rtnl_policy
,
4440 .setup
= macsec_setup
,
4441 .validate
= macsec_validate_attr
,
4442 .newlink
= macsec_newlink
,
4443 .changelink
= macsec_changelink
,
4444 .dellink
= macsec_dellink
,
4445 .get_size
= macsec_get_size
,
4446 .fill_info
= macsec_fill_info
,
4447 .get_link_net
= macsec_get_link_net
,
4450 static bool is_macsec_master(struct net_device
*dev
)
4452 return rcu_access_pointer(dev
->rx_handler
) == macsec_handle_frame
;
4455 static int macsec_notify(struct notifier_block
*this, unsigned long event
,
4458 struct net_device
*real_dev
= netdev_notifier_info_to_dev(ptr
);
4459 struct macsec_rxh_data
*rxd
;
4460 struct macsec_dev
*m
, *n
;
4463 if (!is_macsec_master(real_dev
))
4466 rxd
= macsec_data_rtnl(real_dev
);
4472 list_for_each_entry_safe(m
, n
, &rxd
->secys
, secys
) {
4473 struct net_device
*dev
= m
->secy
.netdev
;
4475 netif_stacked_transfer_operstate(real_dev
, dev
);
4478 case NETDEV_UNREGISTER
:
4479 list_for_each_entry_safe(m
, n
, &rxd
->secys
, secys
) {
4480 macsec_common_dellink(m
->secy
.netdev
, &head
);
4483 netdev_rx_handler_unregister(real_dev
);
4486 unregister_netdevice_many(&head
);
4488 case NETDEV_CHANGEMTU
:
4489 list_for_each_entry(m
, &rxd
->secys
, secys
) {
4490 struct net_device
*dev
= m
->secy
.netdev
;
4491 unsigned int mtu
= real_dev
->mtu
- (m
->secy
.icv_len
+
4492 macsec_extra_len(true));
4495 dev_set_mtu(dev
, mtu
);
4498 case NETDEV_FEAT_CHANGE
:
4499 list_for_each_entry(m
, &rxd
->secys
, secys
) {
4500 macsec_inherit_tso_max(m
->secy
.netdev
);
4501 netdev_update_features(m
->secy
.netdev
);
4509 static struct notifier_block macsec_notifier
= {
4510 .notifier_call
= macsec_notify
,
4513 static int __init
macsec_init(void)
4517 pr_info("MACsec IEEE 802.1AE\n");
4518 err
= register_netdevice_notifier(&macsec_notifier
);
4522 err
= rtnl_link_register(&macsec_link_ops
);
4526 err
= genl_register_family(&macsec_fam
);
4533 rtnl_link_unregister(&macsec_link_ops
);
4535 unregister_netdevice_notifier(&macsec_notifier
);
4539 static void __exit
macsec_exit(void)
4541 genl_unregister_family(&macsec_fam
);
4542 rtnl_link_unregister(&macsec_link_ops
);
4543 unregister_netdevice_notifier(&macsec_notifier
);
4547 module_init(macsec_init
);
4548 module_exit(macsec_exit
);
4550 MODULE_ALIAS_RTNL_LINK("macsec");
4551 MODULE_ALIAS_GENL_FAMILY("macsec");
4553 MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
4554 MODULE_LICENSE("GPL v2");