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