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