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