| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * drivers/net/macsec.c - MACsec device |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net> |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 8 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | | - * (at your option) any later version. |
|---|
| 10 | 6 | */ |
|---|
| 11 | 7 | |
|---|
| 12 | 8 | #include <linux/types.h> |
|---|
| .. | .. |
|---|
| 15 | 11 | #include <linux/module.h> |
|---|
| 16 | 12 | #include <crypto/aead.h> |
|---|
| 17 | 13 | #include <linux/etherdevice.h> |
|---|
| 14 | +#include <linux/netdevice.h> |
|---|
| 18 | 15 | #include <linux/rtnetlink.h> |
|---|
| 19 | 16 | #include <linux/refcount.h> |
|---|
| 20 | 17 | #include <net/genetlink.h> |
|---|
| 21 | 18 | #include <net/sock.h> |
|---|
| 22 | 19 | #include <net/gro_cells.h> |
|---|
| 20 | +#include <net/macsec.h> |
|---|
| 21 | +#include <linux/phy.h> |
|---|
| 22 | +#include <linux/byteorder/generic.h> |
|---|
| 23 | 23 | #include <linux/if_arp.h> |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | #include <uapi/linux/if_macsec.h> |
|---|
| 26 | | - |
|---|
| 27 | | -typedef u64 __bitwise sci_t; |
|---|
| 28 | 26 | |
|---|
| 29 | 27 | #define MACSEC_SCI_LEN 8 |
|---|
| 30 | 28 | |
|---|
| .. | .. |
|---|
| 63 | 61 | #define GCM_AES_IV_LEN 12 |
|---|
| 64 | 62 | #define DEFAULT_ICV_LEN 16 |
|---|
| 65 | 63 | |
|---|
| 66 | | -#define MACSEC_NUM_AN 4 /* 2 bits for the association number */ |
|---|
| 67 | | - |
|---|
| 68 | | -#define for_each_rxsc(secy, sc) \ |
|---|
| 64 | +#define for_each_rxsc(secy, sc) \ |
|---|
| 69 | 65 | for (sc = rcu_dereference_bh(secy->rx_sc); \ |
|---|
| 70 | | - sc; \ |
|---|
| 66 | + sc; \ |
|---|
| 71 | 67 | sc = rcu_dereference_bh(sc->next)) |
|---|
| 72 | 68 | #define for_each_rxsc_rtnl(secy, sc) \ |
|---|
| 73 | 69 | for (sc = rtnl_dereference(secy->rx_sc); \ |
|---|
| 74 | 70 | sc; \ |
|---|
| 75 | 71 | sc = rtnl_dereference(sc->next)) |
|---|
| 72 | + |
|---|
| 73 | +#define pn_same_half(pn1, pn2) (!(((pn1) >> 31) ^ ((pn2) >> 31))) |
|---|
| 74 | + |
|---|
| 75 | +struct gcm_iv_xpn { |
|---|
| 76 | + union { |
|---|
| 77 | + u8 short_secure_channel_id[4]; |
|---|
| 78 | + ssci_t ssci; |
|---|
| 79 | + }; |
|---|
| 80 | + __be64 pn; |
|---|
| 81 | +} __packed; |
|---|
| 76 | 82 | |
|---|
| 77 | 83 | struct gcm_iv { |
|---|
| 78 | 84 | union { |
|---|
| .. | .. |
|---|
| 82 | 88 | __be32 pn; |
|---|
| 83 | 89 | }; |
|---|
| 84 | 90 | |
|---|
| 85 | | -/** |
|---|
| 86 | | - * struct macsec_key - SA key |
|---|
| 87 | | - * @id: user-provided key identifier |
|---|
| 88 | | - * @tfm: crypto struct, key storage |
|---|
| 89 | | - */ |
|---|
| 90 | | -struct macsec_key { |
|---|
| 91 | | - u8 id[MACSEC_KEYID_LEN]; |
|---|
| 92 | | - struct crypto_aead *tfm; |
|---|
| 93 | | -}; |
|---|
| 94 | | - |
|---|
| 95 | | -struct macsec_rx_sc_stats { |
|---|
| 96 | | - __u64 InOctetsValidated; |
|---|
| 97 | | - __u64 InOctetsDecrypted; |
|---|
| 98 | | - __u64 InPktsUnchecked; |
|---|
| 99 | | - __u64 InPktsDelayed; |
|---|
| 100 | | - __u64 InPktsOK; |
|---|
| 101 | | - __u64 InPktsInvalid; |
|---|
| 102 | | - __u64 InPktsLate; |
|---|
| 103 | | - __u64 InPktsNotValid; |
|---|
| 104 | | - __u64 InPktsNotUsingSA; |
|---|
| 105 | | - __u64 InPktsUnusedSA; |
|---|
| 106 | | -}; |
|---|
| 107 | | - |
|---|
| 108 | | -struct macsec_rx_sa_stats { |
|---|
| 109 | | - __u32 InPktsOK; |
|---|
| 110 | | - __u32 InPktsInvalid; |
|---|
| 111 | | - __u32 InPktsNotValid; |
|---|
| 112 | | - __u32 InPktsNotUsingSA; |
|---|
| 113 | | - __u32 InPktsUnusedSA; |
|---|
| 114 | | -}; |
|---|
| 115 | | - |
|---|
| 116 | | -struct macsec_tx_sa_stats { |
|---|
| 117 | | - __u32 OutPktsProtected; |
|---|
| 118 | | - __u32 OutPktsEncrypted; |
|---|
| 119 | | -}; |
|---|
| 120 | | - |
|---|
| 121 | | -struct macsec_tx_sc_stats { |
|---|
| 122 | | - __u64 OutPktsProtected; |
|---|
| 123 | | - __u64 OutPktsEncrypted; |
|---|
| 124 | | - __u64 OutOctetsProtected; |
|---|
| 125 | | - __u64 OutOctetsEncrypted; |
|---|
| 126 | | -}; |
|---|
| 127 | | - |
|---|
| 128 | | -struct macsec_dev_stats { |
|---|
| 129 | | - __u64 OutPktsUntagged; |
|---|
| 130 | | - __u64 InPktsUntagged; |
|---|
| 131 | | - __u64 OutPktsTooLong; |
|---|
| 132 | | - __u64 InPktsNoTag; |
|---|
| 133 | | - __u64 InPktsBadTag; |
|---|
| 134 | | - __u64 InPktsUnknownSCI; |
|---|
| 135 | | - __u64 InPktsNoSCI; |
|---|
| 136 | | - __u64 InPktsOverrun; |
|---|
| 137 | | -}; |
|---|
| 138 | | - |
|---|
| 139 | | -/** |
|---|
| 140 | | - * struct macsec_rx_sa - receive secure association |
|---|
| 141 | | - * @active: |
|---|
| 142 | | - * @next_pn: packet number expected for the next packet |
|---|
| 143 | | - * @lock: protects next_pn manipulations |
|---|
| 144 | | - * @key: key structure |
|---|
| 145 | | - * @stats: per-SA stats |
|---|
| 146 | | - */ |
|---|
| 147 | | -struct macsec_rx_sa { |
|---|
| 148 | | - struct macsec_key key; |
|---|
| 149 | | - spinlock_t lock; |
|---|
| 150 | | - u32 next_pn; |
|---|
| 151 | | - refcount_t refcnt; |
|---|
| 152 | | - bool active; |
|---|
| 153 | | - struct macsec_rx_sa_stats __percpu *stats; |
|---|
| 154 | | - struct macsec_rx_sc *sc; |
|---|
| 155 | | - struct rcu_head rcu; |
|---|
| 156 | | -}; |
|---|
| 157 | | - |
|---|
| 158 | | -struct pcpu_rx_sc_stats { |
|---|
| 159 | | - struct macsec_rx_sc_stats stats; |
|---|
| 160 | | - struct u64_stats_sync syncp; |
|---|
| 161 | | -}; |
|---|
| 162 | | - |
|---|
| 163 | | -/** |
|---|
| 164 | | - * struct macsec_rx_sc - receive secure channel |
|---|
| 165 | | - * @sci: secure channel identifier for this SC |
|---|
| 166 | | - * @active: channel is active |
|---|
| 167 | | - * @sa: array of secure associations |
|---|
| 168 | | - * @stats: per-SC stats |
|---|
| 169 | | - */ |
|---|
| 170 | | -struct macsec_rx_sc { |
|---|
| 171 | | - struct macsec_rx_sc __rcu *next; |
|---|
| 172 | | - sci_t sci; |
|---|
| 173 | | - bool active; |
|---|
| 174 | | - struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN]; |
|---|
| 175 | | - struct pcpu_rx_sc_stats __percpu *stats; |
|---|
| 176 | | - refcount_t refcnt; |
|---|
| 177 | | - struct rcu_head rcu_head; |
|---|
| 178 | | -}; |
|---|
| 179 | | - |
|---|
| 180 | | -/** |
|---|
| 181 | | - * struct macsec_tx_sa - transmit secure association |
|---|
| 182 | | - * @active: |
|---|
| 183 | | - * @next_pn: packet number to use for the next packet |
|---|
| 184 | | - * @lock: protects next_pn manipulations |
|---|
| 185 | | - * @key: key structure |
|---|
| 186 | | - * @stats: per-SA stats |
|---|
| 187 | | - */ |
|---|
| 188 | | -struct macsec_tx_sa { |
|---|
| 189 | | - struct macsec_key key; |
|---|
| 190 | | - spinlock_t lock; |
|---|
| 191 | | - u32 next_pn; |
|---|
| 192 | | - refcount_t refcnt; |
|---|
| 193 | | - bool active; |
|---|
| 194 | | - struct macsec_tx_sa_stats __percpu *stats; |
|---|
| 195 | | - struct rcu_head rcu; |
|---|
| 196 | | -}; |
|---|
| 197 | | - |
|---|
| 198 | | -struct pcpu_tx_sc_stats { |
|---|
| 199 | | - struct macsec_tx_sc_stats stats; |
|---|
| 200 | | - struct u64_stats_sync syncp; |
|---|
| 201 | | -}; |
|---|
| 202 | | - |
|---|
| 203 | | -/** |
|---|
| 204 | | - * struct macsec_tx_sc - transmit secure channel |
|---|
| 205 | | - * @active: |
|---|
| 206 | | - * @encoding_sa: association number of the SA currently in use |
|---|
| 207 | | - * @encrypt: encrypt packets on transmit, or authenticate only |
|---|
| 208 | | - * @send_sci: always include the SCI in the SecTAG |
|---|
| 209 | | - * @end_station: |
|---|
| 210 | | - * @scb: single copy broadcast flag |
|---|
| 211 | | - * @sa: array of secure associations |
|---|
| 212 | | - * @stats: stats for this TXSC |
|---|
| 213 | | - */ |
|---|
| 214 | | -struct macsec_tx_sc { |
|---|
| 215 | | - bool active; |
|---|
| 216 | | - u8 encoding_sa; |
|---|
| 217 | | - bool encrypt; |
|---|
| 218 | | - bool send_sci; |
|---|
| 219 | | - bool end_station; |
|---|
| 220 | | - bool scb; |
|---|
| 221 | | - struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN]; |
|---|
| 222 | | - struct pcpu_tx_sc_stats __percpu *stats; |
|---|
| 223 | | -}; |
|---|
| 224 | | - |
|---|
| 225 | 91 | #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT |
|---|
| 226 | | - |
|---|
| 227 | | -/** |
|---|
| 228 | | - * struct macsec_secy - MACsec Security Entity |
|---|
| 229 | | - * @netdev: netdevice for this SecY |
|---|
| 230 | | - * @n_rx_sc: number of receive secure channels configured on this SecY |
|---|
| 231 | | - * @sci: secure channel identifier used for tx |
|---|
| 232 | | - * @key_len: length of keys used by the cipher suite |
|---|
| 233 | | - * @icv_len: length of ICV used by the cipher suite |
|---|
| 234 | | - * @validate_frames: validation mode |
|---|
| 235 | | - * @operational: MAC_Operational flag |
|---|
| 236 | | - * @protect_frames: enable protection for this SecY |
|---|
| 237 | | - * @replay_protect: enable packet number checks on receive |
|---|
| 238 | | - * @replay_window: size of the replay window |
|---|
| 239 | | - * @tx_sc: transmit secure channel |
|---|
| 240 | | - * @rx_sc: linked list of receive secure channels |
|---|
| 241 | | - */ |
|---|
| 242 | | -struct macsec_secy { |
|---|
| 243 | | - struct net_device *netdev; |
|---|
| 244 | | - unsigned int n_rx_sc; |
|---|
| 245 | | - sci_t sci; |
|---|
| 246 | | - u16 key_len; |
|---|
| 247 | | - u16 icv_len; |
|---|
| 248 | | - enum macsec_validation_type validate_frames; |
|---|
| 249 | | - bool operational; |
|---|
| 250 | | - bool protect_frames; |
|---|
| 251 | | - bool replay_protect; |
|---|
| 252 | | - u32 replay_window; |
|---|
| 253 | | - struct macsec_tx_sc tx_sc; |
|---|
| 254 | | - struct macsec_rx_sc __rcu *rx_sc; |
|---|
| 255 | | -}; |
|---|
| 256 | 92 | |
|---|
| 257 | 93 | struct pcpu_secy_stats { |
|---|
| 258 | 94 | struct macsec_dev_stats stats; |
|---|
| .. | .. |
|---|
| 265 | 101 | * @real_dev: pointer to underlying netdevice |
|---|
| 266 | 102 | * @stats: MACsec device stats |
|---|
| 267 | 103 | * @secys: linked list of SecY's on the underlying device |
|---|
| 104 | + * @offload: status of offloading on the MACsec device |
|---|
| 268 | 105 | */ |
|---|
| 269 | 106 | struct macsec_dev { |
|---|
| 270 | 107 | struct macsec_secy secy; |
|---|
| .. | .. |
|---|
| 272 | 109 | struct pcpu_secy_stats __percpu *stats; |
|---|
| 273 | 110 | struct list_head secys; |
|---|
| 274 | 111 | struct gro_cells gro_cells; |
|---|
| 275 | | - unsigned int nest_level; |
|---|
| 112 | + enum macsec_offload offload; |
|---|
| 276 | 113 | }; |
|---|
| 277 | 114 | |
|---|
| 278 | 115 | /** |
|---|
| .. | .. |
|---|
| 393 | 230 | #define MACSEC_PORT_ES (htons(0x0001)) |
|---|
| 394 | 231 | #define MACSEC_PORT_SCB (0x0000) |
|---|
| 395 | 232 | #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL) |
|---|
| 233 | +#define MACSEC_UNDEF_SSCI ((__force ssci_t)0xffffffff) |
|---|
| 396 | 234 | |
|---|
| 397 | 235 | #define MACSEC_GCM_AES_128_SAK_LEN 16 |
|---|
| 398 | 236 | #define MACSEC_GCM_AES_256_SAK_LEN 32 |
|---|
| 399 | 237 | |
|---|
| 400 | 238 | #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN |
|---|
| 239 | +#define DEFAULT_XPN false |
|---|
| 401 | 240 | #define DEFAULT_SEND_SCI true |
|---|
| 402 | 241 | #define DEFAULT_ENCRYPT false |
|---|
| 403 | 242 | #define DEFAULT_ENCODING_SA 0 |
|---|
| 243 | +#define MACSEC_XPN_MAX_REPLAY_WINDOW (((1 << 30) - 1)) |
|---|
| 404 | 244 | |
|---|
| 405 | 245 | static bool send_sci(const struct macsec_secy *secy) |
|---|
| 406 | 246 | { |
|---|
| .. | .. |
|---|
| 486 | 326 | h->short_length = data_len; |
|---|
| 487 | 327 | } |
|---|
| 488 | 328 | |
|---|
| 489 | | -/* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */ |
|---|
| 490 | | -static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len) |
|---|
| 329 | +/* Checks if a MACsec interface is being offloaded to an hardware engine */ |
|---|
| 330 | +static bool macsec_is_offloaded(struct macsec_dev *macsec) |
|---|
| 331 | +{ |
|---|
| 332 | + if (macsec->offload == MACSEC_OFFLOAD_MAC || |
|---|
| 333 | + macsec->offload == MACSEC_OFFLOAD_PHY) |
|---|
| 334 | + return true; |
|---|
| 335 | + |
|---|
| 336 | + return false; |
|---|
| 337 | +} |
|---|
| 338 | + |
|---|
| 339 | +/* Checks if underlying layers implement MACsec offloading functions. */ |
|---|
| 340 | +static bool macsec_check_offload(enum macsec_offload offload, |
|---|
| 341 | + struct macsec_dev *macsec) |
|---|
| 342 | +{ |
|---|
| 343 | + if (!macsec || !macsec->real_dev) |
|---|
| 344 | + return false; |
|---|
| 345 | + |
|---|
| 346 | + if (offload == MACSEC_OFFLOAD_PHY) |
|---|
| 347 | + return macsec->real_dev->phydev && |
|---|
| 348 | + macsec->real_dev->phydev->macsec_ops; |
|---|
| 349 | + else if (offload == MACSEC_OFFLOAD_MAC) |
|---|
| 350 | + return macsec->real_dev->features & NETIF_F_HW_MACSEC && |
|---|
| 351 | + macsec->real_dev->macsec_ops; |
|---|
| 352 | + |
|---|
| 353 | + return false; |
|---|
| 354 | +} |
|---|
| 355 | + |
|---|
| 356 | +static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload, |
|---|
| 357 | + struct macsec_dev *macsec, |
|---|
| 358 | + struct macsec_context *ctx) |
|---|
| 359 | +{ |
|---|
| 360 | + if (ctx) { |
|---|
| 361 | + memset(ctx, 0, sizeof(*ctx)); |
|---|
| 362 | + ctx->offload = offload; |
|---|
| 363 | + |
|---|
| 364 | + if (offload == MACSEC_OFFLOAD_PHY) |
|---|
| 365 | + ctx->phydev = macsec->real_dev->phydev; |
|---|
| 366 | + else if (offload == MACSEC_OFFLOAD_MAC) |
|---|
| 367 | + ctx->netdev = macsec->real_dev; |
|---|
| 368 | + } |
|---|
| 369 | + |
|---|
| 370 | + if (offload == MACSEC_OFFLOAD_PHY) |
|---|
| 371 | + return macsec->real_dev->phydev->macsec_ops; |
|---|
| 372 | + else |
|---|
| 373 | + return macsec->real_dev->macsec_ops; |
|---|
| 374 | +} |
|---|
| 375 | + |
|---|
| 376 | +/* Returns a pointer to the MACsec ops struct if any and updates the MACsec |
|---|
| 377 | + * context device reference if provided. |
|---|
| 378 | + */ |
|---|
| 379 | +static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec, |
|---|
| 380 | + struct macsec_context *ctx) |
|---|
| 381 | +{ |
|---|
| 382 | + if (!macsec_check_offload(macsec->offload, macsec)) |
|---|
| 383 | + return NULL; |
|---|
| 384 | + |
|---|
| 385 | + return __macsec_get_ops(macsec->offload, macsec, ctx); |
|---|
| 386 | +} |
|---|
| 387 | + |
|---|
| 388 | +/* validate MACsec packet according to IEEE 802.1AE-2018 9.12 */ |
|---|
| 389 | +static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len, bool xpn) |
|---|
| 491 | 390 | { |
|---|
| 492 | 391 | struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data; |
|---|
| 493 | 392 | int len = skb->len - 2 * ETH_ALEN; |
|---|
| .. | .. |
|---|
| 512 | 411 | if (h->unused) |
|---|
| 513 | 412 | return false; |
|---|
| 514 | 413 | |
|---|
| 515 | | - /* rx.pn != 0 (figure 10-5) */ |
|---|
| 516 | | - if (!h->packet_number) |
|---|
| 414 | + /* rx.pn != 0 if not XPN (figure 10-5 with 802.11AEbw-2013 amendment) */ |
|---|
| 415 | + if (!h->packet_number && !xpn) |
|---|
| 517 | 416 | return false; |
|---|
| 518 | 417 | |
|---|
| 519 | 418 | /* length check, f) g) h) i) */ |
|---|
| .. | .. |
|---|
| 524 | 423 | |
|---|
| 525 | 424 | #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true)) |
|---|
| 526 | 425 | #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN |
|---|
| 426 | + |
|---|
| 427 | +static void macsec_fill_iv_xpn(unsigned char *iv, ssci_t ssci, u64 pn, |
|---|
| 428 | + salt_t salt) |
|---|
| 429 | +{ |
|---|
| 430 | + struct gcm_iv_xpn *gcm_iv = (struct gcm_iv_xpn *)iv; |
|---|
| 431 | + |
|---|
| 432 | + gcm_iv->ssci = ssci ^ salt.ssci; |
|---|
| 433 | + gcm_iv->pn = cpu_to_be64(pn) ^ salt.pn; |
|---|
| 434 | +} |
|---|
| 527 | 435 | |
|---|
| 528 | 436 | static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn) |
|---|
| 529 | 437 | { |
|---|
| .. | .. |
|---|
| 538 | 446 | return (struct macsec_eth_header *)skb_mac_header(skb); |
|---|
| 539 | 447 | } |
|---|
| 540 | 448 | |
|---|
| 541 | | -static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy) |
|---|
| 449 | +static sci_t dev_to_sci(struct net_device *dev, __be16 port) |
|---|
| 542 | 450 | { |
|---|
| 543 | | - u32 pn; |
|---|
| 451 | + return make_sci(dev->dev_addr, port); |
|---|
| 452 | +} |
|---|
| 453 | + |
|---|
| 454 | +static void __macsec_pn_wrapped(struct macsec_secy *secy, |
|---|
| 455 | + struct macsec_tx_sa *tx_sa) |
|---|
| 456 | +{ |
|---|
| 457 | + pr_debug("PN wrapped, transitioning to !oper\n"); |
|---|
| 458 | + tx_sa->active = false; |
|---|
| 459 | + if (secy->protect_frames) |
|---|
| 460 | + secy->operational = false; |
|---|
| 461 | +} |
|---|
| 462 | + |
|---|
| 463 | +void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa) |
|---|
| 464 | +{ |
|---|
| 465 | + spin_lock_bh(&tx_sa->lock); |
|---|
| 466 | + __macsec_pn_wrapped(secy, tx_sa); |
|---|
| 467 | + spin_unlock_bh(&tx_sa->lock); |
|---|
| 468 | +} |
|---|
| 469 | +EXPORT_SYMBOL_GPL(macsec_pn_wrapped); |
|---|
| 470 | + |
|---|
| 471 | +static pn_t tx_sa_update_pn(struct macsec_tx_sa *tx_sa, |
|---|
| 472 | + struct macsec_secy *secy) |
|---|
| 473 | +{ |
|---|
| 474 | + pn_t pn; |
|---|
| 544 | 475 | |
|---|
| 545 | 476 | spin_lock_bh(&tx_sa->lock); |
|---|
| 546 | | - pn = tx_sa->next_pn; |
|---|
| 547 | 477 | |
|---|
| 548 | | - tx_sa->next_pn++; |
|---|
| 549 | | - if (tx_sa->next_pn == 0) { |
|---|
| 550 | | - pr_debug("PN wrapped, transitioning to !oper\n"); |
|---|
| 551 | | - tx_sa->active = false; |
|---|
| 552 | | - if (secy->protect_frames) |
|---|
| 553 | | - secy->operational = false; |
|---|
| 554 | | - } |
|---|
| 478 | + pn = tx_sa->next_pn_halves; |
|---|
| 479 | + if (secy->xpn) |
|---|
| 480 | + tx_sa->next_pn++; |
|---|
| 481 | + else |
|---|
| 482 | + tx_sa->next_pn_halves.lower++; |
|---|
| 483 | + |
|---|
| 484 | + if (tx_sa->next_pn == 0) |
|---|
| 485 | + __macsec_pn_wrapped(secy, tx_sa); |
|---|
| 555 | 486 | spin_unlock_bh(&tx_sa->lock); |
|---|
| 556 | 487 | |
|---|
| 557 | 488 | return pn; |
|---|
| .. | .. |
|---|
| 664 | 595 | struct macsec_tx_sa *tx_sa; |
|---|
| 665 | 596 | struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| 666 | 597 | bool sci_present; |
|---|
| 667 | | - u32 pn; |
|---|
| 598 | + pn_t pn; |
|---|
| 668 | 599 | |
|---|
| 669 | 600 | secy = &macsec->secy; |
|---|
| 670 | 601 | tx_sc = &secy->tx_sc; |
|---|
| .. | .. |
|---|
| 706 | 637 | memmove(hh, eth, 2 * ETH_ALEN); |
|---|
| 707 | 638 | |
|---|
| 708 | 639 | pn = tx_sa_update_pn(tx_sa, secy); |
|---|
| 709 | | - if (pn == 0) { |
|---|
| 640 | + if (pn.full64 == 0) { |
|---|
| 710 | 641 | macsec_txsa_put(tx_sa); |
|---|
| 711 | 642 | kfree_skb(skb); |
|---|
| 712 | 643 | return ERR_PTR(-ENOLINK); |
|---|
| 713 | 644 | } |
|---|
| 714 | | - macsec_fill_sectag(hh, secy, pn, sci_present); |
|---|
| 645 | + macsec_fill_sectag(hh, secy, pn.lower, sci_present); |
|---|
| 715 | 646 | macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN); |
|---|
| 716 | 647 | |
|---|
| 717 | 648 | skb_put(skb, secy->icv_len); |
|---|
| .. | .. |
|---|
| 742 | 673 | return ERR_PTR(-ENOMEM); |
|---|
| 743 | 674 | } |
|---|
| 744 | 675 | |
|---|
| 745 | | - macsec_fill_iv(iv, secy->sci, pn); |
|---|
| 676 | + if (secy->xpn) |
|---|
| 677 | + macsec_fill_iv_xpn(iv, tx_sa->ssci, pn.full64, tx_sa->key.salt); |
|---|
| 678 | + else |
|---|
| 679 | + macsec_fill_iv(iv, secy->sci, pn.lower); |
|---|
| 746 | 680 | |
|---|
| 747 | 681 | sg_init_table(sg, ret); |
|---|
| 748 | 682 | ret = skb_to_sgvec(skb, sg, 0, skb->len); |
|---|
| .. | .. |
|---|
| 794 | 728 | u32 lowest_pn = 0; |
|---|
| 795 | 729 | |
|---|
| 796 | 730 | spin_lock(&rx_sa->lock); |
|---|
| 797 | | - if (rx_sa->next_pn >= secy->replay_window) |
|---|
| 798 | | - lowest_pn = rx_sa->next_pn - secy->replay_window; |
|---|
| 731 | + if (rx_sa->next_pn_halves.lower >= secy->replay_window) |
|---|
| 732 | + lowest_pn = rx_sa->next_pn_halves.lower - secy->replay_window; |
|---|
| 799 | 733 | |
|---|
| 800 | 734 | /* Now perform replay protection check again |
|---|
| 801 | 735 | * (see IEEE 802.1AE-2006 figure 10-5) |
|---|
| 802 | 736 | */ |
|---|
| 803 | | - if (secy->replay_protect && pn < lowest_pn) { |
|---|
| 737 | + if (secy->replay_protect && pn < lowest_pn && |
|---|
| 738 | + (!secy->xpn || pn_same_half(pn, lowest_pn))) { |
|---|
| 804 | 739 | spin_unlock(&rx_sa->lock); |
|---|
| 805 | 740 | u64_stats_update_begin(&rxsc_stats->syncp); |
|---|
| 806 | 741 | rxsc_stats->stats.InPktsLate++; |
|---|
| .. | .. |
|---|
| 849 | 784 | } |
|---|
| 850 | 785 | u64_stats_update_end(&rxsc_stats->syncp); |
|---|
| 851 | 786 | |
|---|
| 852 | | - if (pn >= rx_sa->next_pn) |
|---|
| 853 | | - rx_sa->next_pn = pn + 1; |
|---|
| 787 | + // Instead of "pn >=" - to support pn overflow in xpn |
|---|
| 788 | + if (pn + 1 > rx_sa->next_pn_halves.lower) { |
|---|
| 789 | + rx_sa->next_pn_halves.lower = pn + 1; |
|---|
| 790 | + } else if (secy->xpn && |
|---|
| 791 | + !pn_same_half(pn, rx_sa->next_pn_halves.lower)) { |
|---|
| 792 | + rx_sa->next_pn_halves.upper++; |
|---|
| 793 | + rx_sa->next_pn_halves.lower = pn + 1; |
|---|
| 794 | + } |
|---|
| 795 | + |
|---|
| 854 | 796 | spin_unlock(&rx_sa->lock); |
|---|
| 855 | 797 | } |
|---|
| 856 | 798 | |
|---|
| .. | .. |
|---|
| 937 | 879 | unsigned char *iv; |
|---|
| 938 | 880 | struct aead_request *req; |
|---|
| 939 | 881 | struct macsec_eth_header *hdr; |
|---|
| 882 | + u32 hdr_pn; |
|---|
| 940 | 883 | u16 icv_len = secy->icv_len; |
|---|
| 941 | 884 | |
|---|
| 942 | 885 | macsec_skb_cb(skb)->valid = false; |
|---|
| .. | .. |
|---|
| 956 | 899 | } |
|---|
| 957 | 900 | |
|---|
| 958 | 901 | hdr = (struct macsec_eth_header *)skb->data; |
|---|
| 959 | | - macsec_fill_iv(iv, sci, ntohl(hdr->packet_number)); |
|---|
| 902 | + hdr_pn = ntohl(hdr->packet_number); |
|---|
| 903 | + |
|---|
| 904 | + if (secy->xpn) { |
|---|
| 905 | + pn_t recovered_pn = rx_sa->next_pn_halves; |
|---|
| 906 | + |
|---|
| 907 | + recovered_pn.lower = hdr_pn; |
|---|
| 908 | + if (hdr_pn < rx_sa->next_pn_halves.lower && |
|---|
| 909 | + !pn_same_half(hdr_pn, rx_sa->next_pn_halves.lower)) |
|---|
| 910 | + recovered_pn.upper++; |
|---|
| 911 | + |
|---|
| 912 | + macsec_fill_iv_xpn(iv, rx_sa->ssci, recovered_pn.full64, |
|---|
| 913 | + rx_sa->key.salt); |
|---|
| 914 | + } else { |
|---|
| 915 | + macsec_fill_iv(iv, sci, hdr_pn); |
|---|
| 916 | + } |
|---|
| 960 | 917 | |
|---|
| 961 | 918 | sg_init_table(sg, ret); |
|---|
| 962 | 919 | ret = skb_to_sgvec(skb, sg, 0, skb->len); |
|---|
| .. | .. |
|---|
| 1035 | 992 | return NULL; |
|---|
| 1036 | 993 | } |
|---|
| 1037 | 994 | |
|---|
| 1038 | | -static void handle_not_macsec(struct sk_buff *skb) |
|---|
| 995 | +static enum rx_handler_result handle_not_macsec(struct sk_buff *skb) |
|---|
| 1039 | 996 | { |
|---|
| 997 | + /* Deliver to the uncontrolled port by default */ |
|---|
| 998 | + enum rx_handler_result ret = RX_HANDLER_PASS; |
|---|
| 999 | + struct ethhdr *hdr = eth_hdr(skb); |
|---|
| 1040 | 1000 | struct macsec_rxh_data *rxd; |
|---|
| 1041 | 1001 | struct macsec_dev *macsec; |
|---|
| 1042 | 1002 | |
|---|
| 1043 | 1003 | rcu_read_lock(); |
|---|
| 1044 | 1004 | rxd = macsec_data_rcu(skb->dev); |
|---|
| 1045 | 1005 | |
|---|
| 1046 | | - /* 10.6 If the management control validateFrames is not |
|---|
| 1047 | | - * Strict, frames without a SecTAG are received, counted, and |
|---|
| 1048 | | - * delivered to the Controlled Port |
|---|
| 1049 | | - */ |
|---|
| 1050 | 1006 | list_for_each_entry_rcu(macsec, &rxd->secys, secys) { |
|---|
| 1051 | 1007 | struct sk_buff *nskb; |
|---|
| 1052 | 1008 | struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats); |
|---|
| 1009 | + struct net_device *ndev = macsec->secy.netdev; |
|---|
| 1053 | 1010 | |
|---|
| 1011 | + /* If h/w offloading is enabled, HW decodes frames and strips |
|---|
| 1012 | + * the SecTAG, so we have to deduce which port to deliver to. |
|---|
| 1013 | + */ |
|---|
| 1014 | + if (macsec_is_offloaded(macsec) && netif_running(ndev)) { |
|---|
| 1015 | + if (ether_addr_equal_64bits(hdr->h_dest, |
|---|
| 1016 | + ndev->dev_addr)) { |
|---|
| 1017 | + /* exact match, divert skb to this port */ |
|---|
| 1018 | + skb->dev = ndev; |
|---|
| 1019 | + skb->pkt_type = PACKET_HOST; |
|---|
| 1020 | + ret = RX_HANDLER_ANOTHER; |
|---|
| 1021 | + goto out; |
|---|
| 1022 | + } else if (is_multicast_ether_addr_64bits( |
|---|
| 1023 | + hdr->h_dest)) { |
|---|
| 1024 | + /* multicast frame, deliver on this port too */ |
|---|
| 1025 | + nskb = skb_clone(skb, GFP_ATOMIC); |
|---|
| 1026 | + if (!nskb) |
|---|
| 1027 | + break; |
|---|
| 1028 | + |
|---|
| 1029 | + nskb->dev = ndev; |
|---|
| 1030 | + if (ether_addr_equal_64bits(hdr->h_dest, |
|---|
| 1031 | + ndev->broadcast)) |
|---|
| 1032 | + nskb->pkt_type = PACKET_BROADCAST; |
|---|
| 1033 | + else |
|---|
| 1034 | + nskb->pkt_type = PACKET_MULTICAST; |
|---|
| 1035 | + |
|---|
| 1036 | + netif_rx(nskb); |
|---|
| 1037 | + } |
|---|
| 1038 | + continue; |
|---|
| 1039 | + } |
|---|
| 1040 | + |
|---|
| 1041 | + /* 10.6 If the management control validateFrames is not |
|---|
| 1042 | + * Strict, frames without a SecTAG are received, counted, and |
|---|
| 1043 | + * delivered to the Controlled Port |
|---|
| 1044 | + */ |
|---|
| 1054 | 1045 | if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) { |
|---|
| 1055 | 1046 | u64_stats_update_begin(&secy_stats->syncp); |
|---|
| 1056 | 1047 | secy_stats->stats.InPktsNoTag++; |
|---|
| .. | .. |
|---|
| 1063 | 1054 | if (!nskb) |
|---|
| 1064 | 1055 | break; |
|---|
| 1065 | 1056 | |
|---|
| 1066 | | - nskb->dev = macsec->secy.netdev; |
|---|
| 1057 | + nskb->dev = ndev; |
|---|
| 1067 | 1058 | |
|---|
| 1068 | 1059 | if (netif_rx(nskb) == NET_RX_SUCCESS) { |
|---|
| 1069 | 1060 | u64_stats_update_begin(&secy_stats->syncp); |
|---|
| .. | .. |
|---|
| 1072 | 1063 | } |
|---|
| 1073 | 1064 | } |
|---|
| 1074 | 1065 | |
|---|
| 1066 | +out: |
|---|
| 1075 | 1067 | rcu_read_unlock(); |
|---|
| 1068 | + return ret; |
|---|
| 1076 | 1069 | } |
|---|
| 1077 | 1070 | |
|---|
| 1078 | 1071 | static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb) |
|---|
| .. | .. |
|---|
| 1087 | 1080 | struct macsec_dev *macsec; |
|---|
| 1088 | 1081 | unsigned int len; |
|---|
| 1089 | 1082 | sci_t sci; |
|---|
| 1090 | | - u32 pn; |
|---|
| 1083 | + u32 hdr_pn; |
|---|
| 1091 | 1084 | bool cbit; |
|---|
| 1092 | 1085 | struct pcpu_rx_sc_stats *rxsc_stats; |
|---|
| 1093 | 1086 | struct pcpu_secy_stats *secy_stats; |
|---|
| .. | .. |
|---|
| 1098 | 1091 | goto drop_direct; |
|---|
| 1099 | 1092 | |
|---|
| 1100 | 1093 | hdr = macsec_ethhdr(skb); |
|---|
| 1101 | | - if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) { |
|---|
| 1102 | | - handle_not_macsec(skb); |
|---|
| 1103 | | - |
|---|
| 1104 | | - /* and deliver to the uncontrolled port */ |
|---|
| 1105 | | - return RX_HANDLER_PASS; |
|---|
| 1106 | | - } |
|---|
| 1094 | + if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) |
|---|
| 1095 | + return handle_not_macsec(skb); |
|---|
| 1107 | 1096 | |
|---|
| 1108 | 1097 | skb = skb_unshare(skb, GFP_ATOMIC); |
|---|
| 1109 | 1098 | *pskb = skb; |
|---|
| .. | .. |
|---|
| 1144 | 1133 | |
|---|
| 1145 | 1134 | list_for_each_entry_rcu(macsec, &rxd->secys, secys) { |
|---|
| 1146 | 1135 | struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci); |
|---|
| 1136 | + |
|---|
| 1147 | 1137 | sc = sc ? macsec_rxsc_get(sc) : NULL; |
|---|
| 1148 | 1138 | |
|---|
| 1149 | 1139 | if (sc) { |
|---|
| .. | .. |
|---|
| 1161 | 1151 | secy_stats = this_cpu_ptr(macsec->stats); |
|---|
| 1162 | 1152 | rxsc_stats = this_cpu_ptr(rx_sc->stats); |
|---|
| 1163 | 1153 | |
|---|
| 1164 | | - if (!macsec_validate_skb(skb, secy->icv_len)) { |
|---|
| 1154 | + if (!macsec_validate_skb(skb, secy->icv_len, secy->xpn)) { |
|---|
| 1165 | 1155 | u64_stats_update_begin(&secy_stats->syncp); |
|---|
| 1166 | 1156 | secy_stats->stats.InPktsBadTag++; |
|---|
| 1167 | 1157 | u64_stats_update_end(&secy_stats->syncp); |
|---|
| .. | .. |
|---|
| 1193 | 1183 | } |
|---|
| 1194 | 1184 | |
|---|
| 1195 | 1185 | /* First, PN check to avoid decrypting obviously wrong packets */ |
|---|
| 1196 | | - pn = ntohl(hdr->packet_number); |
|---|
| 1186 | + hdr_pn = ntohl(hdr->packet_number); |
|---|
| 1197 | 1187 | if (secy->replay_protect) { |
|---|
| 1198 | 1188 | bool late; |
|---|
| 1199 | 1189 | |
|---|
| 1200 | 1190 | spin_lock(&rx_sa->lock); |
|---|
| 1201 | | - late = rx_sa->next_pn >= secy->replay_window && |
|---|
| 1202 | | - pn < (rx_sa->next_pn - secy->replay_window); |
|---|
| 1191 | + late = rx_sa->next_pn_halves.lower >= secy->replay_window && |
|---|
| 1192 | + hdr_pn < (rx_sa->next_pn_halves.lower - secy->replay_window); |
|---|
| 1193 | + |
|---|
| 1194 | + if (secy->xpn) |
|---|
| 1195 | + late = late && pn_same_half(rx_sa->next_pn_halves.lower, hdr_pn); |
|---|
| 1203 | 1196 | spin_unlock(&rx_sa->lock); |
|---|
| 1204 | 1197 | |
|---|
| 1205 | 1198 | if (late) { |
|---|
| .. | .. |
|---|
| 1228 | 1221 | return RX_HANDLER_CONSUMED; |
|---|
| 1229 | 1222 | } |
|---|
| 1230 | 1223 | |
|---|
| 1231 | | - if (!macsec_post_decrypt(skb, secy, pn)) |
|---|
| 1224 | + if (!macsec_post_decrypt(skb, secy, hdr_pn)) |
|---|
| 1232 | 1225 | goto drop; |
|---|
| 1233 | 1226 | |
|---|
| 1234 | 1227 | deliver: |
|---|
| .. | .. |
|---|
| 1315 | 1308 | struct crypto_aead *tfm; |
|---|
| 1316 | 1309 | int ret; |
|---|
| 1317 | 1310 | |
|---|
| 1318 | | - /* Pick a sync gcm(aes) cipher to ensure order is preserved. */ |
|---|
| 1319 | | - tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC); |
|---|
| 1311 | + tfm = crypto_alloc_aead("gcm(aes)", 0, 0); |
|---|
| 1320 | 1312 | |
|---|
| 1321 | 1313 | if (IS_ERR(tfm)) |
|---|
| 1322 | 1314 | return tfm; |
|---|
| .. | .. |
|---|
| 1348 | 1340 | return PTR_ERR(rx_sa->key.tfm); |
|---|
| 1349 | 1341 | } |
|---|
| 1350 | 1342 | |
|---|
| 1343 | + rx_sa->ssci = MACSEC_UNDEF_SSCI; |
|---|
| 1351 | 1344 | rx_sa->active = false; |
|---|
| 1352 | 1345 | rx_sa->next_pn = 1; |
|---|
| 1353 | 1346 | refcount_set(&rx_sa->refcnt, 1); |
|---|
| .. | .. |
|---|
| 1396 | 1389 | return NULL; |
|---|
| 1397 | 1390 | } |
|---|
| 1398 | 1391 | |
|---|
| 1399 | | -static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci) |
|---|
| 1392 | +static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci, |
|---|
| 1393 | + bool active) |
|---|
| 1400 | 1394 | { |
|---|
| 1401 | 1395 | struct macsec_rx_sc *rx_sc; |
|---|
| 1402 | 1396 | struct macsec_dev *macsec; |
|---|
| .. | .. |
|---|
| 1420 | 1414 | } |
|---|
| 1421 | 1415 | |
|---|
| 1422 | 1416 | rx_sc->sci = sci; |
|---|
| 1423 | | - rx_sc->active = true; |
|---|
| 1417 | + rx_sc->active = active; |
|---|
| 1424 | 1418 | refcount_set(&rx_sc->refcnt, 1); |
|---|
| 1425 | 1419 | |
|---|
| 1426 | 1420 | secy = &macsec_priv(dev)->secy; |
|---|
| .. | .. |
|---|
| 1446 | 1440 | return PTR_ERR(tx_sa->key.tfm); |
|---|
| 1447 | 1441 | } |
|---|
| 1448 | 1442 | |
|---|
| 1443 | + tx_sa->ssci = MACSEC_UNDEF_SSCI; |
|---|
| 1449 | 1444 | tx_sa->active = false; |
|---|
| 1450 | 1445 | refcount_set(&tx_sa->refcnt, 1); |
|---|
| 1451 | 1446 | spin_lock_init(&tx_sa->lock); |
|---|
| .. | .. |
|---|
| 1478 | 1473 | return dev; |
|---|
| 1479 | 1474 | } |
|---|
| 1480 | 1475 | |
|---|
| 1476 | +static enum macsec_offload nla_get_offload(const struct nlattr *nla) |
|---|
| 1477 | +{ |
|---|
| 1478 | + return (__force enum macsec_offload)nla_get_u8(nla); |
|---|
| 1479 | +} |
|---|
| 1480 | + |
|---|
| 1481 | 1481 | static sci_t nla_get_sci(const struct nlattr *nla) |
|---|
| 1482 | 1482 | { |
|---|
| 1483 | 1483 | return (__force sci_t)nla_get_u64(nla); |
|---|
| .. | .. |
|---|
| 1487 | 1487 | int padattr) |
|---|
| 1488 | 1488 | { |
|---|
| 1489 | 1489 | return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr); |
|---|
| 1490 | +} |
|---|
| 1491 | + |
|---|
| 1492 | +static ssci_t nla_get_ssci(const struct nlattr *nla) |
|---|
| 1493 | +{ |
|---|
| 1494 | + return (__force ssci_t)nla_get_u32(nla); |
|---|
| 1495 | +} |
|---|
| 1496 | + |
|---|
| 1497 | +static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value) |
|---|
| 1498 | +{ |
|---|
| 1499 | + return nla_put_u32(skb, attrtype, (__force u64)value); |
|---|
| 1490 | 1500 | } |
|---|
| 1491 | 1501 | |
|---|
| 1492 | 1502 | static struct macsec_tx_sa *get_txsa_from_nl(struct net *net, |
|---|
| .. | .. |
|---|
| 1589 | 1599 | return rx_sa; |
|---|
| 1590 | 1600 | } |
|---|
| 1591 | 1601 | |
|---|
| 1592 | | - |
|---|
| 1593 | 1602 | static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = { |
|---|
| 1594 | 1603 | [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 }, |
|---|
| 1595 | 1604 | [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED }, |
|---|
| 1596 | 1605 | [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED }, |
|---|
| 1606 | + [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED }, |
|---|
| 1597 | 1607 | }; |
|---|
| 1598 | 1608 | |
|---|
| 1599 | 1609 | static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = { |
|---|
| .. | .. |
|---|
| 1604 | 1614 | static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = { |
|---|
| 1605 | 1615 | [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 }, |
|---|
| 1606 | 1616 | [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 }, |
|---|
| 1607 | | - [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 }, |
|---|
| 1617 | + [MACSEC_SA_ATTR_PN] = NLA_POLICY_MIN_LEN(4), |
|---|
| 1608 | 1618 | [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY, |
|---|
| 1609 | 1619 | .len = MACSEC_KEYID_LEN, }, |
|---|
| 1610 | 1620 | [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY, |
|---|
| 1611 | 1621 | .len = MACSEC_MAX_KEY_LEN, }, |
|---|
| 1622 | + [MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 }, |
|---|
| 1623 | + [MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY, |
|---|
| 1624 | + .len = MACSEC_SALT_LEN, }, |
|---|
| 1612 | 1625 | }; |
|---|
| 1626 | + |
|---|
| 1627 | +static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = { |
|---|
| 1628 | + [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 }, |
|---|
| 1629 | +}; |
|---|
| 1630 | + |
|---|
| 1631 | +/* Offloads an operation to a device driver */ |
|---|
| 1632 | +static int macsec_offload(int (* const func)(struct macsec_context *), |
|---|
| 1633 | + struct macsec_context *ctx) |
|---|
| 1634 | +{ |
|---|
| 1635 | + int ret; |
|---|
| 1636 | + |
|---|
| 1637 | + if (unlikely(!func)) |
|---|
| 1638 | + return 0; |
|---|
| 1639 | + |
|---|
| 1640 | + if (ctx->offload == MACSEC_OFFLOAD_PHY) |
|---|
| 1641 | + mutex_lock(&ctx->phydev->lock); |
|---|
| 1642 | + |
|---|
| 1643 | + /* Phase I: prepare. The drive should fail here if there are going to be |
|---|
| 1644 | + * issues in the commit phase. |
|---|
| 1645 | + */ |
|---|
| 1646 | + ctx->prepare = true; |
|---|
| 1647 | + ret = (*func)(ctx); |
|---|
| 1648 | + if (ret) |
|---|
| 1649 | + goto phy_unlock; |
|---|
| 1650 | + |
|---|
| 1651 | + /* Phase II: commit. This step cannot fail. */ |
|---|
| 1652 | + ctx->prepare = false; |
|---|
| 1653 | + ret = (*func)(ctx); |
|---|
| 1654 | + /* This should never happen: commit is not allowed to fail */ |
|---|
| 1655 | + if (unlikely(ret)) |
|---|
| 1656 | + WARN(1, "MACsec offloading commit failed (%d)\n", ret); |
|---|
| 1657 | + |
|---|
| 1658 | +phy_unlock: |
|---|
| 1659 | + if (ctx->offload == MACSEC_OFFLOAD_PHY) |
|---|
| 1660 | + mutex_unlock(&ctx->phydev->lock); |
|---|
| 1661 | + |
|---|
| 1662 | + return ret; |
|---|
| 1663 | +} |
|---|
| 1613 | 1664 | |
|---|
| 1614 | 1665 | static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa) |
|---|
| 1615 | 1666 | { |
|---|
| 1616 | 1667 | if (!attrs[MACSEC_ATTR_SA_CONFIG]) |
|---|
| 1617 | 1668 | return -EINVAL; |
|---|
| 1618 | 1669 | |
|---|
| 1619 | | - if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX, |
|---|
| 1620 | | - attrs[MACSEC_ATTR_SA_CONFIG], |
|---|
| 1621 | | - macsec_genl_sa_policy, NULL)) |
|---|
| 1670 | + if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL)) |
|---|
| 1622 | 1671 | return -EINVAL; |
|---|
| 1623 | 1672 | |
|---|
| 1624 | 1673 | return 0; |
|---|
| .. | .. |
|---|
| 1629 | 1678 | if (!attrs[MACSEC_ATTR_RXSC_CONFIG]) |
|---|
| 1630 | 1679 | return -EINVAL; |
|---|
| 1631 | 1680 | |
|---|
| 1632 | | - if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX, |
|---|
| 1633 | | - attrs[MACSEC_ATTR_RXSC_CONFIG], |
|---|
| 1634 | | - macsec_genl_rxsc_policy, NULL)) |
|---|
| 1681 | + if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL)) |
|---|
| 1635 | 1682 | return -EINVAL; |
|---|
| 1636 | 1683 | |
|---|
| 1637 | 1684 | return 0; |
|---|
| .. | .. |
|---|
| 1647 | 1694 | if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN) |
|---|
| 1648 | 1695 | return false; |
|---|
| 1649 | 1696 | |
|---|
| 1650 | | - if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0) |
|---|
| 1697 | + if (attrs[MACSEC_SA_ATTR_PN] && |
|---|
| 1698 | + nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0) |
|---|
| 1651 | 1699 | return false; |
|---|
| 1652 | 1700 | |
|---|
| 1653 | 1701 | if (attrs[MACSEC_SA_ATTR_ACTIVE]) { |
|---|
| .. | .. |
|---|
| 1669 | 1717 | struct macsec_rx_sc *rx_sc; |
|---|
| 1670 | 1718 | struct macsec_rx_sa *rx_sa; |
|---|
| 1671 | 1719 | unsigned char assoc_num; |
|---|
| 1720 | + int pn_len; |
|---|
| 1672 | 1721 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
|---|
| 1673 | 1722 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
|---|
| 1674 | 1723 | int err; |
|---|
| .. | .. |
|---|
| 1701 | 1750 | return -EINVAL; |
|---|
| 1702 | 1751 | } |
|---|
| 1703 | 1752 | |
|---|
| 1753 | + pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN; |
|---|
| 1754 | + if (tb_sa[MACSEC_SA_ATTR_PN] && |
|---|
| 1755 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) { |
|---|
| 1756 | + pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n", |
|---|
| 1757 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len); |
|---|
| 1758 | + rtnl_unlock(); |
|---|
| 1759 | + return -EINVAL; |
|---|
| 1760 | + } |
|---|
| 1761 | + |
|---|
| 1762 | + if (secy->xpn) { |
|---|
| 1763 | + if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) { |
|---|
| 1764 | + rtnl_unlock(); |
|---|
| 1765 | + return -EINVAL; |
|---|
| 1766 | + } |
|---|
| 1767 | + |
|---|
| 1768 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) { |
|---|
| 1769 | + pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n", |
|---|
| 1770 | + nla_len(tb_sa[MACSEC_SA_ATTR_SALT]), |
|---|
| 1771 | + MACSEC_SALT_LEN); |
|---|
| 1772 | + rtnl_unlock(); |
|---|
| 1773 | + return -EINVAL; |
|---|
| 1774 | + } |
|---|
| 1775 | + } |
|---|
| 1776 | + |
|---|
| 1704 | 1777 | rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]); |
|---|
| 1705 | 1778 | if (rx_sa) { |
|---|
| 1706 | 1779 | rtnl_unlock(); |
|---|
| .. | .. |
|---|
| 1723 | 1796 | |
|---|
| 1724 | 1797 | if (tb_sa[MACSEC_SA_ATTR_PN]) { |
|---|
| 1725 | 1798 | spin_lock_bh(&rx_sa->lock); |
|---|
| 1726 | | - rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]); |
|---|
| 1799 | + rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]); |
|---|
| 1727 | 1800 | spin_unlock_bh(&rx_sa->lock); |
|---|
| 1728 | 1801 | } |
|---|
| 1729 | 1802 | |
|---|
| 1730 | 1803 | if (tb_sa[MACSEC_SA_ATTR_ACTIVE]) |
|---|
| 1731 | 1804 | rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]); |
|---|
| 1732 | 1805 | |
|---|
| 1733 | | - nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN); |
|---|
| 1734 | 1806 | rx_sa->sc = rx_sc; |
|---|
| 1807 | + |
|---|
| 1808 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 1809 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 1810 | + const struct macsec_ops *ops; |
|---|
| 1811 | + struct macsec_context ctx; |
|---|
| 1812 | + |
|---|
| 1813 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 1814 | + if (!ops) { |
|---|
| 1815 | + err = -EOPNOTSUPP; |
|---|
| 1816 | + goto cleanup; |
|---|
| 1817 | + } |
|---|
| 1818 | + |
|---|
| 1819 | + ctx.sa.assoc_num = assoc_num; |
|---|
| 1820 | + ctx.sa.rx_sa = rx_sa; |
|---|
| 1821 | + ctx.secy = secy; |
|---|
| 1822 | + memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]), |
|---|
| 1823 | + secy->key_len); |
|---|
| 1824 | + |
|---|
| 1825 | + err = macsec_offload(ops->mdo_add_rxsa, &ctx); |
|---|
| 1826 | + memzero_explicit(ctx.sa.key, secy->key_len); |
|---|
| 1827 | + if (err) |
|---|
| 1828 | + goto cleanup; |
|---|
| 1829 | + } |
|---|
| 1830 | + |
|---|
| 1831 | + if (secy->xpn) { |
|---|
| 1832 | + rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]); |
|---|
| 1833 | + nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT], |
|---|
| 1834 | + MACSEC_SALT_LEN); |
|---|
| 1835 | + } |
|---|
| 1836 | + |
|---|
| 1837 | + nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN); |
|---|
| 1735 | 1838 | rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa); |
|---|
| 1736 | 1839 | |
|---|
| 1737 | 1840 | rtnl_unlock(); |
|---|
| 1738 | 1841 | |
|---|
| 1739 | 1842 | return 0; |
|---|
| 1843 | + |
|---|
| 1844 | +cleanup: |
|---|
| 1845 | + macsec_rxsa_put(rx_sa); |
|---|
| 1846 | + rtnl_unlock(); |
|---|
| 1847 | + return err; |
|---|
| 1740 | 1848 | } |
|---|
| 1741 | 1849 | |
|---|
| 1742 | 1850 | static bool validate_add_rxsc(struct nlattr **attrs) |
|---|
| .. | .. |
|---|
| 1759 | 1867 | struct nlattr **attrs = info->attrs; |
|---|
| 1760 | 1868 | struct macsec_rx_sc *rx_sc; |
|---|
| 1761 | 1869 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
|---|
| 1870 | + struct macsec_secy *secy; |
|---|
| 1871 | + bool active = true; |
|---|
| 1872 | + int ret; |
|---|
| 1762 | 1873 | |
|---|
| 1763 | 1874 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| 1764 | 1875 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 1776 | 1887 | return PTR_ERR(dev); |
|---|
| 1777 | 1888 | } |
|---|
| 1778 | 1889 | |
|---|
| 1890 | + secy = &macsec_priv(dev)->secy; |
|---|
| 1779 | 1891 | sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]); |
|---|
| 1780 | 1892 | |
|---|
| 1781 | | - rx_sc = create_rx_sc(dev, sci); |
|---|
| 1893 | + if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) |
|---|
| 1894 | + active = nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]); |
|---|
| 1895 | + |
|---|
| 1896 | + rx_sc = create_rx_sc(dev, sci, active); |
|---|
| 1782 | 1897 | if (IS_ERR(rx_sc)) { |
|---|
| 1783 | 1898 | rtnl_unlock(); |
|---|
| 1784 | 1899 | return PTR_ERR(rx_sc); |
|---|
| 1785 | 1900 | } |
|---|
| 1786 | 1901 | |
|---|
| 1787 | | - if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) |
|---|
| 1788 | | - rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]); |
|---|
| 1902 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 1903 | + const struct macsec_ops *ops; |
|---|
| 1904 | + struct macsec_context ctx; |
|---|
| 1905 | + |
|---|
| 1906 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 1907 | + if (!ops) { |
|---|
| 1908 | + ret = -EOPNOTSUPP; |
|---|
| 1909 | + goto cleanup; |
|---|
| 1910 | + } |
|---|
| 1911 | + |
|---|
| 1912 | + ctx.rx_sc = rx_sc; |
|---|
| 1913 | + ctx.secy = secy; |
|---|
| 1914 | + |
|---|
| 1915 | + ret = macsec_offload(ops->mdo_add_rxsc, &ctx); |
|---|
| 1916 | + if (ret) |
|---|
| 1917 | + goto cleanup; |
|---|
| 1918 | + } |
|---|
| 1789 | 1919 | |
|---|
| 1790 | 1920 | rtnl_unlock(); |
|---|
| 1791 | 1921 | |
|---|
| 1792 | 1922 | return 0; |
|---|
| 1923 | + |
|---|
| 1924 | +cleanup: |
|---|
| 1925 | + del_rx_sc(secy, sci); |
|---|
| 1926 | + free_rx_sc(rx_sc); |
|---|
| 1927 | + rtnl_unlock(); |
|---|
| 1928 | + return ret; |
|---|
| 1793 | 1929 | } |
|---|
| 1794 | 1930 | |
|---|
| 1795 | 1931 | static bool validate_add_txsa(struct nlattr **attrs) |
|---|
| .. | .. |
|---|
| 1803 | 1939 | if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN) |
|---|
| 1804 | 1940 | return false; |
|---|
| 1805 | 1941 | |
|---|
| 1806 | | - if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0) |
|---|
| 1942 | + if (nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0) |
|---|
| 1807 | 1943 | return false; |
|---|
| 1808 | 1944 | |
|---|
| 1809 | 1945 | if (attrs[MACSEC_SA_ATTR_ACTIVE]) { |
|---|
| .. | .. |
|---|
| 1825 | 1961 | struct macsec_tx_sc *tx_sc; |
|---|
| 1826 | 1962 | struct macsec_tx_sa *tx_sa; |
|---|
| 1827 | 1963 | unsigned char assoc_num; |
|---|
| 1964 | + int pn_len; |
|---|
| 1828 | 1965 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
|---|
| 1966 | + bool was_operational; |
|---|
| 1829 | 1967 | int err; |
|---|
| 1830 | 1968 | |
|---|
| 1831 | 1969 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| .. | .. |
|---|
| 1856 | 1994 | return -EINVAL; |
|---|
| 1857 | 1995 | } |
|---|
| 1858 | 1996 | |
|---|
| 1997 | + pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN; |
|---|
| 1998 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) { |
|---|
| 1999 | + pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n", |
|---|
| 2000 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len); |
|---|
| 2001 | + rtnl_unlock(); |
|---|
| 2002 | + return -EINVAL; |
|---|
| 2003 | + } |
|---|
| 2004 | + |
|---|
| 2005 | + if (secy->xpn) { |
|---|
| 2006 | + if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) { |
|---|
| 2007 | + rtnl_unlock(); |
|---|
| 2008 | + return -EINVAL; |
|---|
| 2009 | + } |
|---|
| 2010 | + |
|---|
| 2011 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) { |
|---|
| 2012 | + pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n", |
|---|
| 2013 | + nla_len(tb_sa[MACSEC_SA_ATTR_SALT]), |
|---|
| 2014 | + MACSEC_SALT_LEN); |
|---|
| 2015 | + rtnl_unlock(); |
|---|
| 2016 | + return -EINVAL; |
|---|
| 2017 | + } |
|---|
| 2018 | + } |
|---|
| 2019 | + |
|---|
| 1859 | 2020 | tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]); |
|---|
| 1860 | 2021 | if (tx_sa) { |
|---|
| 1861 | 2022 | rtnl_unlock(); |
|---|
| .. | .. |
|---|
| 1876 | 2037 | return err; |
|---|
| 1877 | 2038 | } |
|---|
| 1878 | 2039 | |
|---|
| 1879 | | - nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN); |
|---|
| 1880 | | - |
|---|
| 1881 | 2040 | spin_lock_bh(&tx_sa->lock); |
|---|
| 1882 | | - tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]); |
|---|
| 2041 | + tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]); |
|---|
| 1883 | 2042 | spin_unlock_bh(&tx_sa->lock); |
|---|
| 1884 | 2043 | |
|---|
| 1885 | 2044 | if (tb_sa[MACSEC_SA_ATTR_ACTIVE]) |
|---|
| 1886 | 2045 | tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]); |
|---|
| 1887 | 2046 | |
|---|
| 2047 | + was_operational = secy->operational; |
|---|
| 1888 | 2048 | if (assoc_num == tx_sc->encoding_sa && tx_sa->active) |
|---|
| 1889 | 2049 | secy->operational = true; |
|---|
| 1890 | 2050 | |
|---|
| 2051 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2052 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 2053 | + const struct macsec_ops *ops; |
|---|
| 2054 | + struct macsec_context ctx; |
|---|
| 2055 | + |
|---|
| 2056 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 2057 | + if (!ops) { |
|---|
| 2058 | + err = -EOPNOTSUPP; |
|---|
| 2059 | + goto cleanup; |
|---|
| 2060 | + } |
|---|
| 2061 | + |
|---|
| 2062 | + ctx.sa.assoc_num = assoc_num; |
|---|
| 2063 | + ctx.sa.tx_sa = tx_sa; |
|---|
| 2064 | + ctx.secy = secy; |
|---|
| 2065 | + memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]), |
|---|
| 2066 | + secy->key_len); |
|---|
| 2067 | + |
|---|
| 2068 | + err = macsec_offload(ops->mdo_add_txsa, &ctx); |
|---|
| 2069 | + memzero_explicit(ctx.sa.key, secy->key_len); |
|---|
| 2070 | + if (err) |
|---|
| 2071 | + goto cleanup; |
|---|
| 2072 | + } |
|---|
| 2073 | + |
|---|
| 2074 | + if (secy->xpn) { |
|---|
| 2075 | + tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]); |
|---|
| 2076 | + nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT], |
|---|
| 2077 | + MACSEC_SALT_LEN); |
|---|
| 2078 | + } |
|---|
| 2079 | + |
|---|
| 2080 | + nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN); |
|---|
| 1891 | 2081 | rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa); |
|---|
| 1892 | 2082 | |
|---|
| 1893 | 2083 | rtnl_unlock(); |
|---|
| 1894 | 2084 | |
|---|
| 1895 | 2085 | return 0; |
|---|
| 2086 | + |
|---|
| 2087 | +cleanup: |
|---|
| 2088 | + secy->operational = was_operational; |
|---|
| 2089 | + macsec_txsa_put(tx_sa); |
|---|
| 2090 | + rtnl_unlock(); |
|---|
| 2091 | + return err; |
|---|
| 1896 | 2092 | } |
|---|
| 1897 | 2093 | |
|---|
| 1898 | 2094 | static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info) |
|---|
| .. | .. |
|---|
| 1905 | 2101 | u8 assoc_num; |
|---|
| 1906 | 2102 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
|---|
| 1907 | 2103 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
|---|
| 2104 | + int ret; |
|---|
| 1908 | 2105 | |
|---|
| 1909 | 2106 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| 1910 | 2107 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 1928 | 2125 | return -EBUSY; |
|---|
| 1929 | 2126 | } |
|---|
| 1930 | 2127 | |
|---|
| 2128 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2129 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 2130 | + const struct macsec_ops *ops; |
|---|
| 2131 | + struct macsec_context ctx; |
|---|
| 2132 | + |
|---|
| 2133 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 2134 | + if (!ops) { |
|---|
| 2135 | + ret = -EOPNOTSUPP; |
|---|
| 2136 | + goto cleanup; |
|---|
| 2137 | + } |
|---|
| 2138 | + |
|---|
| 2139 | + ctx.sa.assoc_num = assoc_num; |
|---|
| 2140 | + ctx.sa.rx_sa = rx_sa; |
|---|
| 2141 | + ctx.secy = secy; |
|---|
| 2142 | + |
|---|
| 2143 | + ret = macsec_offload(ops->mdo_del_rxsa, &ctx); |
|---|
| 2144 | + if (ret) |
|---|
| 2145 | + goto cleanup; |
|---|
| 2146 | + } |
|---|
| 2147 | + |
|---|
| 1931 | 2148 | RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL); |
|---|
| 1932 | 2149 | clear_rx_sa(rx_sa); |
|---|
| 1933 | 2150 | |
|---|
| 1934 | 2151 | rtnl_unlock(); |
|---|
| 1935 | 2152 | |
|---|
| 1936 | 2153 | return 0; |
|---|
| 2154 | + |
|---|
| 2155 | +cleanup: |
|---|
| 2156 | + rtnl_unlock(); |
|---|
| 2157 | + return ret; |
|---|
| 1937 | 2158 | } |
|---|
| 1938 | 2159 | |
|---|
| 1939 | 2160 | static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info) |
|---|
| .. | .. |
|---|
| 1944 | 2165 | struct macsec_rx_sc *rx_sc; |
|---|
| 1945 | 2166 | sci_t sci; |
|---|
| 1946 | 2167 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
|---|
| 2168 | + int ret; |
|---|
| 1947 | 2169 | |
|---|
| 1948 | 2170 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| 1949 | 2171 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 1970 | 2192 | return -ENODEV; |
|---|
| 1971 | 2193 | } |
|---|
| 1972 | 2194 | |
|---|
| 2195 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2196 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 2197 | + const struct macsec_ops *ops; |
|---|
| 2198 | + struct macsec_context ctx; |
|---|
| 2199 | + |
|---|
| 2200 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 2201 | + if (!ops) { |
|---|
| 2202 | + ret = -EOPNOTSUPP; |
|---|
| 2203 | + goto cleanup; |
|---|
| 2204 | + } |
|---|
| 2205 | + |
|---|
| 2206 | + ctx.rx_sc = rx_sc; |
|---|
| 2207 | + ctx.secy = secy; |
|---|
| 2208 | + ret = macsec_offload(ops->mdo_del_rxsc, &ctx); |
|---|
| 2209 | + if (ret) |
|---|
| 2210 | + goto cleanup; |
|---|
| 2211 | + } |
|---|
| 2212 | + |
|---|
| 1973 | 2213 | free_rx_sc(rx_sc); |
|---|
| 1974 | 2214 | rtnl_unlock(); |
|---|
| 1975 | 2215 | |
|---|
| 1976 | 2216 | return 0; |
|---|
| 2217 | + |
|---|
| 2218 | +cleanup: |
|---|
| 2219 | + rtnl_unlock(); |
|---|
| 2220 | + return ret; |
|---|
| 1977 | 2221 | } |
|---|
| 1978 | 2222 | |
|---|
| 1979 | 2223 | static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info) |
|---|
| .. | .. |
|---|
| 1985 | 2229 | struct macsec_tx_sa *tx_sa; |
|---|
| 1986 | 2230 | u8 assoc_num; |
|---|
| 1987 | 2231 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
|---|
| 2232 | + int ret; |
|---|
| 1988 | 2233 | |
|---|
| 1989 | 2234 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| 1990 | 2235 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 2005 | 2250 | return -EBUSY; |
|---|
| 2006 | 2251 | } |
|---|
| 2007 | 2252 | |
|---|
| 2253 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2254 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 2255 | + const struct macsec_ops *ops; |
|---|
| 2256 | + struct macsec_context ctx; |
|---|
| 2257 | + |
|---|
| 2258 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 2259 | + if (!ops) { |
|---|
| 2260 | + ret = -EOPNOTSUPP; |
|---|
| 2261 | + goto cleanup; |
|---|
| 2262 | + } |
|---|
| 2263 | + |
|---|
| 2264 | + ctx.sa.assoc_num = assoc_num; |
|---|
| 2265 | + ctx.sa.tx_sa = tx_sa; |
|---|
| 2266 | + ctx.secy = secy; |
|---|
| 2267 | + |
|---|
| 2268 | + ret = macsec_offload(ops->mdo_del_txsa, &ctx); |
|---|
| 2269 | + if (ret) |
|---|
| 2270 | + goto cleanup; |
|---|
| 2271 | + } |
|---|
| 2272 | + |
|---|
| 2008 | 2273 | RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL); |
|---|
| 2009 | 2274 | clear_tx_sa(tx_sa); |
|---|
| 2010 | 2275 | |
|---|
| 2011 | 2276 | rtnl_unlock(); |
|---|
| 2012 | 2277 | |
|---|
| 2013 | 2278 | return 0; |
|---|
| 2279 | + |
|---|
| 2280 | +cleanup: |
|---|
| 2281 | + rtnl_unlock(); |
|---|
| 2282 | + return ret; |
|---|
| 2014 | 2283 | } |
|---|
| 2015 | 2284 | |
|---|
| 2016 | 2285 | static bool validate_upd_sa(struct nlattr **attrs) |
|---|
| 2017 | 2286 | { |
|---|
| 2018 | 2287 | if (!attrs[MACSEC_SA_ATTR_AN] || |
|---|
| 2019 | 2288 | attrs[MACSEC_SA_ATTR_KEY] || |
|---|
| 2020 | | - attrs[MACSEC_SA_ATTR_KEYID]) |
|---|
| 2289 | + attrs[MACSEC_SA_ATTR_KEYID] || |
|---|
| 2290 | + attrs[MACSEC_SA_ATTR_SSCI] || |
|---|
| 2291 | + attrs[MACSEC_SA_ATTR_SALT]) |
|---|
| 2021 | 2292 | return false; |
|---|
| 2022 | 2293 | |
|---|
| 2023 | 2294 | if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN) |
|---|
| 2024 | 2295 | return false; |
|---|
| 2025 | 2296 | |
|---|
| 2026 | | - if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0) |
|---|
| 2297 | + if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0) |
|---|
| 2027 | 2298 | return false; |
|---|
| 2028 | 2299 | |
|---|
| 2029 | 2300 | if (attrs[MACSEC_SA_ATTR_ACTIVE]) { |
|---|
| .. | .. |
|---|
| 2043 | 2314 | struct macsec_tx_sa *tx_sa; |
|---|
| 2044 | 2315 | u8 assoc_num; |
|---|
| 2045 | 2316 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
|---|
| 2317 | + bool was_operational, was_active; |
|---|
| 2318 | + pn_t prev_pn; |
|---|
| 2319 | + int ret = 0; |
|---|
| 2320 | + |
|---|
| 2321 | + prev_pn.full64 = 0; |
|---|
| 2046 | 2322 | |
|---|
| 2047 | 2323 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| 2048 | 2324 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 2062 | 2338 | } |
|---|
| 2063 | 2339 | |
|---|
| 2064 | 2340 | if (tb_sa[MACSEC_SA_ATTR_PN]) { |
|---|
| 2341 | + int pn_len; |
|---|
| 2342 | + |
|---|
| 2343 | + pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN; |
|---|
| 2344 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) { |
|---|
| 2345 | + pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n", |
|---|
| 2346 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len); |
|---|
| 2347 | + rtnl_unlock(); |
|---|
| 2348 | + return -EINVAL; |
|---|
| 2349 | + } |
|---|
| 2350 | + |
|---|
| 2065 | 2351 | spin_lock_bh(&tx_sa->lock); |
|---|
| 2066 | | - tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]); |
|---|
| 2352 | + prev_pn = tx_sa->next_pn_halves; |
|---|
| 2353 | + tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]); |
|---|
| 2067 | 2354 | spin_unlock_bh(&tx_sa->lock); |
|---|
| 2068 | 2355 | } |
|---|
| 2069 | 2356 | |
|---|
| 2357 | + was_active = tx_sa->active; |
|---|
| 2070 | 2358 | if (tb_sa[MACSEC_SA_ATTR_ACTIVE]) |
|---|
| 2071 | 2359 | tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]); |
|---|
| 2072 | 2360 | |
|---|
| 2361 | + was_operational = secy->operational; |
|---|
| 2073 | 2362 | if (assoc_num == tx_sc->encoding_sa) |
|---|
| 2074 | 2363 | secy->operational = tx_sa->active; |
|---|
| 2364 | + |
|---|
| 2365 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2366 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 2367 | + const struct macsec_ops *ops; |
|---|
| 2368 | + struct macsec_context ctx; |
|---|
| 2369 | + |
|---|
| 2370 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 2371 | + if (!ops) { |
|---|
| 2372 | + ret = -EOPNOTSUPP; |
|---|
| 2373 | + goto cleanup; |
|---|
| 2374 | + } |
|---|
| 2375 | + |
|---|
| 2376 | + ctx.sa.assoc_num = assoc_num; |
|---|
| 2377 | + ctx.sa.tx_sa = tx_sa; |
|---|
| 2378 | + ctx.secy = secy; |
|---|
| 2379 | + |
|---|
| 2380 | + ret = macsec_offload(ops->mdo_upd_txsa, &ctx); |
|---|
| 2381 | + if (ret) |
|---|
| 2382 | + goto cleanup; |
|---|
| 2383 | + } |
|---|
| 2075 | 2384 | |
|---|
| 2076 | 2385 | rtnl_unlock(); |
|---|
| 2077 | 2386 | |
|---|
| 2078 | 2387 | return 0; |
|---|
| 2388 | + |
|---|
| 2389 | +cleanup: |
|---|
| 2390 | + if (tb_sa[MACSEC_SA_ATTR_PN]) { |
|---|
| 2391 | + spin_lock_bh(&tx_sa->lock); |
|---|
| 2392 | + tx_sa->next_pn_halves = prev_pn; |
|---|
| 2393 | + spin_unlock_bh(&tx_sa->lock); |
|---|
| 2394 | + } |
|---|
| 2395 | + tx_sa->active = was_active; |
|---|
| 2396 | + secy->operational = was_operational; |
|---|
| 2397 | + rtnl_unlock(); |
|---|
| 2398 | + return ret; |
|---|
| 2079 | 2399 | } |
|---|
| 2080 | 2400 | |
|---|
| 2081 | 2401 | static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info) |
|---|
| .. | .. |
|---|
| 2088 | 2408 | u8 assoc_num; |
|---|
| 2089 | 2409 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
|---|
| 2090 | 2410 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
|---|
| 2411 | + bool was_active; |
|---|
| 2412 | + pn_t prev_pn; |
|---|
| 2413 | + int ret = 0; |
|---|
| 2414 | + |
|---|
| 2415 | + prev_pn.full64 = 0; |
|---|
| 2091 | 2416 | |
|---|
| 2092 | 2417 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| 2093 | 2418 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 2110 | 2435 | } |
|---|
| 2111 | 2436 | |
|---|
| 2112 | 2437 | if (tb_sa[MACSEC_SA_ATTR_PN]) { |
|---|
| 2438 | + int pn_len; |
|---|
| 2439 | + |
|---|
| 2440 | + pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN; |
|---|
| 2441 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) { |
|---|
| 2442 | + pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n", |
|---|
| 2443 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len); |
|---|
| 2444 | + rtnl_unlock(); |
|---|
| 2445 | + return -EINVAL; |
|---|
| 2446 | + } |
|---|
| 2447 | + |
|---|
| 2113 | 2448 | spin_lock_bh(&rx_sa->lock); |
|---|
| 2114 | | - rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]); |
|---|
| 2449 | + prev_pn = rx_sa->next_pn_halves; |
|---|
| 2450 | + rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]); |
|---|
| 2115 | 2451 | spin_unlock_bh(&rx_sa->lock); |
|---|
| 2116 | 2452 | } |
|---|
| 2117 | 2453 | |
|---|
| 2454 | + was_active = rx_sa->active; |
|---|
| 2118 | 2455 | if (tb_sa[MACSEC_SA_ATTR_ACTIVE]) |
|---|
| 2119 | 2456 | rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]); |
|---|
| 2120 | 2457 | |
|---|
| 2458 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2459 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 2460 | + const struct macsec_ops *ops; |
|---|
| 2461 | + struct macsec_context ctx; |
|---|
| 2462 | + |
|---|
| 2463 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 2464 | + if (!ops) { |
|---|
| 2465 | + ret = -EOPNOTSUPP; |
|---|
| 2466 | + goto cleanup; |
|---|
| 2467 | + } |
|---|
| 2468 | + |
|---|
| 2469 | + ctx.sa.assoc_num = assoc_num; |
|---|
| 2470 | + ctx.sa.rx_sa = rx_sa; |
|---|
| 2471 | + ctx.secy = secy; |
|---|
| 2472 | + |
|---|
| 2473 | + ret = macsec_offload(ops->mdo_upd_rxsa, &ctx); |
|---|
| 2474 | + if (ret) |
|---|
| 2475 | + goto cleanup; |
|---|
| 2476 | + } |
|---|
| 2477 | + |
|---|
| 2121 | 2478 | rtnl_unlock(); |
|---|
| 2122 | 2479 | return 0; |
|---|
| 2480 | + |
|---|
| 2481 | +cleanup: |
|---|
| 2482 | + if (tb_sa[MACSEC_SA_ATTR_PN]) { |
|---|
| 2483 | + spin_lock_bh(&rx_sa->lock); |
|---|
| 2484 | + rx_sa->next_pn_halves = prev_pn; |
|---|
| 2485 | + spin_unlock_bh(&rx_sa->lock); |
|---|
| 2486 | + } |
|---|
| 2487 | + rx_sa->active = was_active; |
|---|
| 2488 | + rtnl_unlock(); |
|---|
| 2489 | + return ret; |
|---|
| 2123 | 2490 | } |
|---|
| 2124 | 2491 | |
|---|
| 2125 | 2492 | static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info) |
|---|
| .. | .. |
|---|
| 2129 | 2496 | struct macsec_secy *secy; |
|---|
| 2130 | 2497 | struct macsec_rx_sc *rx_sc; |
|---|
| 2131 | 2498 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
|---|
| 2499 | + unsigned int prev_n_rx_sc; |
|---|
| 2500 | + bool was_active; |
|---|
| 2501 | + int ret; |
|---|
| 2132 | 2502 | |
|---|
| 2133 | 2503 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| 2134 | 2504 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 2146 | 2516 | return PTR_ERR(rx_sc); |
|---|
| 2147 | 2517 | } |
|---|
| 2148 | 2518 | |
|---|
| 2519 | + was_active = rx_sc->active; |
|---|
| 2520 | + prev_n_rx_sc = secy->n_rx_sc; |
|---|
| 2149 | 2521 | if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) { |
|---|
| 2150 | 2522 | bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]); |
|---|
| 2151 | 2523 | |
|---|
| .. | .. |
|---|
| 2155 | 2527 | rx_sc->active = new; |
|---|
| 2156 | 2528 | } |
|---|
| 2157 | 2529 | |
|---|
| 2530 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2531 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 2532 | + const struct macsec_ops *ops; |
|---|
| 2533 | + struct macsec_context ctx; |
|---|
| 2534 | + |
|---|
| 2535 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 2536 | + if (!ops) { |
|---|
| 2537 | + ret = -EOPNOTSUPP; |
|---|
| 2538 | + goto cleanup; |
|---|
| 2539 | + } |
|---|
| 2540 | + |
|---|
| 2541 | + ctx.rx_sc = rx_sc; |
|---|
| 2542 | + ctx.secy = secy; |
|---|
| 2543 | + |
|---|
| 2544 | + ret = macsec_offload(ops->mdo_upd_rxsc, &ctx); |
|---|
| 2545 | + if (ret) |
|---|
| 2546 | + goto cleanup; |
|---|
| 2547 | + } |
|---|
| 2548 | + |
|---|
| 2158 | 2549 | rtnl_unlock(); |
|---|
| 2159 | 2550 | |
|---|
| 2160 | 2551 | return 0; |
|---|
| 2552 | + |
|---|
| 2553 | +cleanup: |
|---|
| 2554 | + secy->n_rx_sc = prev_n_rx_sc; |
|---|
| 2555 | + rx_sc->active = was_active; |
|---|
| 2556 | + rtnl_unlock(); |
|---|
| 2557 | + return ret; |
|---|
| 2161 | 2558 | } |
|---|
| 2162 | 2559 | |
|---|
| 2163 | | -static int copy_tx_sa_stats(struct sk_buff *skb, |
|---|
| 2164 | | - struct macsec_tx_sa_stats __percpu *pstats) |
|---|
| 2560 | +static bool macsec_is_configured(struct macsec_dev *macsec) |
|---|
| 2165 | 2561 | { |
|---|
| 2166 | | - struct macsec_tx_sa_stats sum = {0, }; |
|---|
| 2167 | | - int cpu; |
|---|
| 2562 | + struct macsec_secy *secy = &macsec->secy; |
|---|
| 2563 | + struct macsec_tx_sc *tx_sc = &secy->tx_sc; |
|---|
| 2564 | + int i; |
|---|
| 2168 | 2565 | |
|---|
| 2169 | | - for_each_possible_cpu(cpu) { |
|---|
| 2170 | | - const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu); |
|---|
| 2566 | + if (secy->rx_sc) |
|---|
| 2567 | + return true; |
|---|
| 2171 | 2568 | |
|---|
| 2172 | | - sum.OutPktsProtected += stats->OutPktsProtected; |
|---|
| 2173 | | - sum.OutPktsEncrypted += stats->OutPktsEncrypted; |
|---|
| 2569 | + for (i = 0; i < MACSEC_NUM_AN; i++) |
|---|
| 2570 | + if (tx_sc->sa[i]) |
|---|
| 2571 | + return true; |
|---|
| 2572 | + |
|---|
| 2573 | + return false; |
|---|
| 2574 | +} |
|---|
| 2575 | + |
|---|
| 2576 | +static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info) |
|---|
| 2577 | +{ |
|---|
| 2578 | + struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1]; |
|---|
| 2579 | + enum macsec_offload offload, prev_offload; |
|---|
| 2580 | + int (*func)(struct macsec_context *ctx); |
|---|
| 2581 | + struct nlattr **attrs = info->attrs; |
|---|
| 2582 | + struct net_device *dev; |
|---|
| 2583 | + const struct macsec_ops *ops; |
|---|
| 2584 | + struct macsec_context ctx; |
|---|
| 2585 | + struct macsec_dev *macsec; |
|---|
| 2586 | + int ret = 0; |
|---|
| 2587 | + |
|---|
| 2588 | + if (!attrs[MACSEC_ATTR_IFINDEX]) |
|---|
| 2589 | + return -EINVAL; |
|---|
| 2590 | + |
|---|
| 2591 | + if (!attrs[MACSEC_ATTR_OFFLOAD]) |
|---|
| 2592 | + return -EINVAL; |
|---|
| 2593 | + |
|---|
| 2594 | + if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX, |
|---|
| 2595 | + attrs[MACSEC_ATTR_OFFLOAD], |
|---|
| 2596 | + macsec_genl_offload_policy, NULL)) |
|---|
| 2597 | + return -EINVAL; |
|---|
| 2598 | + |
|---|
| 2599 | + rtnl_lock(); |
|---|
| 2600 | + |
|---|
| 2601 | + dev = get_dev_from_nl(genl_info_net(info), attrs); |
|---|
| 2602 | + if (IS_ERR(dev)) { |
|---|
| 2603 | + ret = PTR_ERR(dev); |
|---|
| 2604 | + goto out; |
|---|
| 2605 | + } |
|---|
| 2606 | + macsec = macsec_priv(dev); |
|---|
| 2607 | + |
|---|
| 2608 | + if (!tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]) { |
|---|
| 2609 | + ret = -EINVAL; |
|---|
| 2610 | + goto out; |
|---|
| 2174 | 2611 | } |
|---|
| 2175 | 2612 | |
|---|
| 2176 | | - if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) || |
|---|
| 2177 | | - nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted)) |
|---|
| 2613 | + offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]); |
|---|
| 2614 | + if (macsec->offload == offload) |
|---|
| 2615 | + goto out; |
|---|
| 2616 | + |
|---|
| 2617 | + /* Check if the offloading mode is supported by the underlying layers */ |
|---|
| 2618 | + if (offload != MACSEC_OFFLOAD_OFF && |
|---|
| 2619 | + !macsec_check_offload(offload, macsec)) { |
|---|
| 2620 | + ret = -EOPNOTSUPP; |
|---|
| 2621 | + goto out; |
|---|
| 2622 | + } |
|---|
| 2623 | + |
|---|
| 2624 | + /* Check if the net device is busy. */ |
|---|
| 2625 | + if (netif_running(dev)) { |
|---|
| 2626 | + ret = -EBUSY; |
|---|
| 2627 | + goto out; |
|---|
| 2628 | + } |
|---|
| 2629 | + |
|---|
| 2630 | + prev_offload = macsec->offload; |
|---|
| 2631 | + macsec->offload = offload; |
|---|
| 2632 | + |
|---|
| 2633 | + /* Check if the device already has rules configured: we do not support |
|---|
| 2634 | + * rules migration. |
|---|
| 2635 | + */ |
|---|
| 2636 | + if (macsec_is_configured(macsec)) { |
|---|
| 2637 | + ret = -EBUSY; |
|---|
| 2638 | + goto rollback; |
|---|
| 2639 | + } |
|---|
| 2640 | + |
|---|
| 2641 | + ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload, |
|---|
| 2642 | + macsec, &ctx); |
|---|
| 2643 | + if (!ops) { |
|---|
| 2644 | + ret = -EOPNOTSUPP; |
|---|
| 2645 | + goto rollback; |
|---|
| 2646 | + } |
|---|
| 2647 | + |
|---|
| 2648 | + if (prev_offload == MACSEC_OFFLOAD_OFF) |
|---|
| 2649 | + func = ops->mdo_add_secy; |
|---|
| 2650 | + else |
|---|
| 2651 | + func = ops->mdo_del_secy; |
|---|
| 2652 | + |
|---|
| 2653 | + ctx.secy = &macsec->secy; |
|---|
| 2654 | + ret = macsec_offload(func, &ctx); |
|---|
| 2655 | + if (ret) |
|---|
| 2656 | + goto rollback; |
|---|
| 2657 | + |
|---|
| 2658 | + rtnl_unlock(); |
|---|
| 2659 | + return 0; |
|---|
| 2660 | + |
|---|
| 2661 | +rollback: |
|---|
| 2662 | + macsec->offload = prev_offload; |
|---|
| 2663 | +out: |
|---|
| 2664 | + rtnl_unlock(); |
|---|
| 2665 | + return ret; |
|---|
| 2666 | +} |
|---|
| 2667 | + |
|---|
| 2668 | +static void get_tx_sa_stats(struct net_device *dev, int an, |
|---|
| 2669 | + struct macsec_tx_sa *tx_sa, |
|---|
| 2670 | + struct macsec_tx_sa_stats *sum) |
|---|
| 2671 | +{ |
|---|
| 2672 | + struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| 2673 | + int cpu; |
|---|
| 2674 | + |
|---|
| 2675 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2676 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 2677 | + const struct macsec_ops *ops; |
|---|
| 2678 | + struct macsec_context ctx; |
|---|
| 2679 | + |
|---|
| 2680 | + ops = macsec_get_ops(macsec, &ctx); |
|---|
| 2681 | + if (ops) { |
|---|
| 2682 | + ctx.sa.assoc_num = an; |
|---|
| 2683 | + ctx.sa.tx_sa = tx_sa; |
|---|
| 2684 | + ctx.stats.tx_sa_stats = sum; |
|---|
| 2685 | + ctx.secy = &macsec_priv(dev)->secy; |
|---|
| 2686 | + macsec_offload(ops->mdo_get_tx_sa_stats, &ctx); |
|---|
| 2687 | + } |
|---|
| 2688 | + return; |
|---|
| 2689 | + } |
|---|
| 2690 | + |
|---|
| 2691 | + for_each_possible_cpu(cpu) { |
|---|
| 2692 | + const struct macsec_tx_sa_stats *stats = |
|---|
| 2693 | + per_cpu_ptr(tx_sa->stats, cpu); |
|---|
| 2694 | + |
|---|
| 2695 | + sum->OutPktsProtected += stats->OutPktsProtected; |
|---|
| 2696 | + sum->OutPktsEncrypted += stats->OutPktsEncrypted; |
|---|
| 2697 | + } |
|---|
| 2698 | +} |
|---|
| 2699 | + |
|---|
| 2700 | +static int copy_tx_sa_stats(struct sk_buff *skb, struct macsec_tx_sa_stats *sum) |
|---|
| 2701 | +{ |
|---|
| 2702 | + if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, |
|---|
| 2703 | + sum->OutPktsProtected) || |
|---|
| 2704 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, |
|---|
| 2705 | + sum->OutPktsEncrypted)) |
|---|
| 2178 | 2706 | return -EMSGSIZE; |
|---|
| 2179 | 2707 | |
|---|
| 2180 | 2708 | return 0; |
|---|
| 2709 | +} |
|---|
| 2710 | + |
|---|
| 2711 | +static void get_rx_sa_stats(struct net_device *dev, |
|---|
| 2712 | + struct macsec_rx_sc *rx_sc, int an, |
|---|
| 2713 | + struct macsec_rx_sa *rx_sa, |
|---|
| 2714 | + struct macsec_rx_sa_stats *sum) |
|---|
| 2715 | +{ |
|---|
| 2716 | + struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| 2717 | + int cpu; |
|---|
| 2718 | + |
|---|
| 2719 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2720 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 2721 | + const struct macsec_ops *ops; |
|---|
| 2722 | + struct macsec_context ctx; |
|---|
| 2723 | + |
|---|
| 2724 | + ops = macsec_get_ops(macsec, &ctx); |
|---|
| 2725 | + if (ops) { |
|---|
| 2726 | + ctx.sa.assoc_num = an; |
|---|
| 2727 | + ctx.sa.rx_sa = rx_sa; |
|---|
| 2728 | + ctx.stats.rx_sa_stats = sum; |
|---|
| 2729 | + ctx.secy = &macsec_priv(dev)->secy; |
|---|
| 2730 | + ctx.rx_sc = rx_sc; |
|---|
| 2731 | + macsec_offload(ops->mdo_get_rx_sa_stats, &ctx); |
|---|
| 2732 | + } |
|---|
| 2733 | + return; |
|---|
| 2734 | + } |
|---|
| 2735 | + |
|---|
| 2736 | + for_each_possible_cpu(cpu) { |
|---|
| 2737 | + const struct macsec_rx_sa_stats *stats = |
|---|
| 2738 | + per_cpu_ptr(rx_sa->stats, cpu); |
|---|
| 2739 | + |
|---|
| 2740 | + sum->InPktsOK += stats->InPktsOK; |
|---|
| 2741 | + sum->InPktsInvalid += stats->InPktsInvalid; |
|---|
| 2742 | + sum->InPktsNotValid += stats->InPktsNotValid; |
|---|
| 2743 | + sum->InPktsNotUsingSA += stats->InPktsNotUsingSA; |
|---|
| 2744 | + sum->InPktsUnusedSA += stats->InPktsUnusedSA; |
|---|
| 2745 | + } |
|---|
| 2181 | 2746 | } |
|---|
| 2182 | 2747 | |
|---|
| 2183 | 2748 | static int copy_rx_sa_stats(struct sk_buff *skb, |
|---|
| 2184 | | - struct macsec_rx_sa_stats __percpu *pstats) |
|---|
| 2749 | + struct macsec_rx_sa_stats *sum) |
|---|
| 2185 | 2750 | { |
|---|
| 2186 | | - struct macsec_rx_sa_stats sum = {0, }; |
|---|
| 2187 | | - int cpu; |
|---|
| 2188 | | - |
|---|
| 2189 | | - for_each_possible_cpu(cpu) { |
|---|
| 2190 | | - const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu); |
|---|
| 2191 | | - |
|---|
| 2192 | | - sum.InPktsOK += stats->InPktsOK; |
|---|
| 2193 | | - sum.InPktsInvalid += stats->InPktsInvalid; |
|---|
| 2194 | | - sum.InPktsNotValid += stats->InPktsNotValid; |
|---|
| 2195 | | - sum.InPktsNotUsingSA += stats->InPktsNotUsingSA; |
|---|
| 2196 | | - sum.InPktsUnusedSA += stats->InPktsUnusedSA; |
|---|
| 2197 | | - } |
|---|
| 2198 | | - |
|---|
| 2199 | | - if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) || |
|---|
| 2200 | | - nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) || |
|---|
| 2201 | | - nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) || |
|---|
| 2202 | | - nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) || |
|---|
| 2203 | | - nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA)) |
|---|
| 2751 | + if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum->InPktsOK) || |
|---|
| 2752 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, |
|---|
| 2753 | + sum->InPktsInvalid) || |
|---|
| 2754 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, |
|---|
| 2755 | + sum->InPktsNotValid) || |
|---|
| 2756 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, |
|---|
| 2757 | + sum->InPktsNotUsingSA) || |
|---|
| 2758 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, |
|---|
| 2759 | + sum->InPktsUnusedSA)) |
|---|
| 2204 | 2760 | return -EMSGSIZE; |
|---|
| 2205 | 2761 | |
|---|
| 2206 | 2762 | return 0; |
|---|
| 2207 | 2763 | } |
|---|
| 2208 | 2764 | |
|---|
| 2209 | | -static int copy_rx_sc_stats(struct sk_buff *skb, |
|---|
| 2210 | | - struct pcpu_rx_sc_stats __percpu *pstats) |
|---|
| 2765 | +static void get_rx_sc_stats(struct net_device *dev, |
|---|
| 2766 | + struct macsec_rx_sc *rx_sc, |
|---|
| 2767 | + struct macsec_rx_sc_stats *sum) |
|---|
| 2211 | 2768 | { |
|---|
| 2212 | | - struct macsec_rx_sc_stats sum = {0, }; |
|---|
| 2769 | + struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| 2213 | 2770 | int cpu; |
|---|
| 2771 | + |
|---|
| 2772 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2773 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 2774 | + const struct macsec_ops *ops; |
|---|
| 2775 | + struct macsec_context ctx; |
|---|
| 2776 | + |
|---|
| 2777 | + ops = macsec_get_ops(macsec, &ctx); |
|---|
| 2778 | + if (ops) { |
|---|
| 2779 | + ctx.stats.rx_sc_stats = sum; |
|---|
| 2780 | + ctx.secy = &macsec_priv(dev)->secy; |
|---|
| 2781 | + ctx.rx_sc = rx_sc; |
|---|
| 2782 | + macsec_offload(ops->mdo_get_rx_sc_stats, &ctx); |
|---|
| 2783 | + } |
|---|
| 2784 | + return; |
|---|
| 2785 | + } |
|---|
| 2214 | 2786 | |
|---|
| 2215 | 2787 | for_each_possible_cpu(cpu) { |
|---|
| 2216 | 2788 | const struct pcpu_rx_sc_stats *stats; |
|---|
| 2217 | 2789 | struct macsec_rx_sc_stats tmp; |
|---|
| 2218 | 2790 | unsigned int start; |
|---|
| 2219 | 2791 | |
|---|
| 2220 | | - stats = per_cpu_ptr(pstats, cpu); |
|---|
| 2792 | + stats = per_cpu_ptr(rx_sc->stats, cpu); |
|---|
| 2221 | 2793 | do { |
|---|
| 2222 | 2794 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
|---|
| 2223 | 2795 | memcpy(&tmp, &stats->stats, sizeof(tmp)); |
|---|
| 2224 | 2796 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
|---|
| 2225 | 2797 | |
|---|
| 2226 | | - sum.InOctetsValidated += tmp.InOctetsValidated; |
|---|
| 2227 | | - sum.InOctetsDecrypted += tmp.InOctetsDecrypted; |
|---|
| 2228 | | - sum.InPktsUnchecked += tmp.InPktsUnchecked; |
|---|
| 2229 | | - sum.InPktsDelayed += tmp.InPktsDelayed; |
|---|
| 2230 | | - sum.InPktsOK += tmp.InPktsOK; |
|---|
| 2231 | | - sum.InPktsInvalid += tmp.InPktsInvalid; |
|---|
| 2232 | | - sum.InPktsLate += tmp.InPktsLate; |
|---|
| 2233 | | - sum.InPktsNotValid += tmp.InPktsNotValid; |
|---|
| 2234 | | - sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA; |
|---|
| 2235 | | - sum.InPktsUnusedSA += tmp.InPktsUnusedSA; |
|---|
| 2798 | + sum->InOctetsValidated += tmp.InOctetsValidated; |
|---|
| 2799 | + sum->InOctetsDecrypted += tmp.InOctetsDecrypted; |
|---|
| 2800 | + sum->InPktsUnchecked += tmp.InPktsUnchecked; |
|---|
| 2801 | + sum->InPktsDelayed += tmp.InPktsDelayed; |
|---|
| 2802 | + sum->InPktsOK += tmp.InPktsOK; |
|---|
| 2803 | + sum->InPktsInvalid += tmp.InPktsInvalid; |
|---|
| 2804 | + sum->InPktsLate += tmp.InPktsLate; |
|---|
| 2805 | + sum->InPktsNotValid += tmp.InPktsNotValid; |
|---|
| 2806 | + sum->InPktsNotUsingSA += tmp.InPktsNotUsingSA; |
|---|
| 2807 | + sum->InPktsUnusedSA += tmp.InPktsUnusedSA; |
|---|
| 2236 | 2808 | } |
|---|
| 2809 | +} |
|---|
| 2237 | 2810 | |
|---|
| 2811 | +static int copy_rx_sc_stats(struct sk_buff *skb, struct macsec_rx_sc_stats *sum) |
|---|
| 2812 | +{ |
|---|
| 2238 | 2813 | if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED, |
|---|
| 2239 | | - sum.InOctetsValidated, |
|---|
| 2814 | + sum->InOctetsValidated, |
|---|
| 2240 | 2815 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2241 | 2816 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED, |
|---|
| 2242 | | - sum.InOctetsDecrypted, |
|---|
| 2817 | + sum->InOctetsDecrypted, |
|---|
| 2243 | 2818 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2244 | 2819 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED, |
|---|
| 2245 | | - sum.InPktsUnchecked, |
|---|
| 2820 | + sum->InPktsUnchecked, |
|---|
| 2246 | 2821 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2247 | 2822 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED, |
|---|
| 2248 | | - sum.InPktsDelayed, |
|---|
| 2823 | + sum->InPktsDelayed, |
|---|
| 2249 | 2824 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2250 | 2825 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK, |
|---|
| 2251 | | - sum.InPktsOK, |
|---|
| 2826 | + sum->InPktsOK, |
|---|
| 2252 | 2827 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2253 | 2828 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID, |
|---|
| 2254 | | - sum.InPktsInvalid, |
|---|
| 2829 | + sum->InPktsInvalid, |
|---|
| 2255 | 2830 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2256 | 2831 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE, |
|---|
| 2257 | | - sum.InPktsLate, |
|---|
| 2832 | + sum->InPktsLate, |
|---|
| 2258 | 2833 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2259 | 2834 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID, |
|---|
| 2260 | | - sum.InPktsNotValid, |
|---|
| 2835 | + sum->InPktsNotValid, |
|---|
| 2261 | 2836 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2262 | 2837 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA, |
|---|
| 2263 | | - sum.InPktsNotUsingSA, |
|---|
| 2838 | + sum->InPktsNotUsingSA, |
|---|
| 2264 | 2839 | MACSEC_RXSC_STATS_ATTR_PAD) || |
|---|
| 2265 | 2840 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA, |
|---|
| 2266 | | - sum.InPktsUnusedSA, |
|---|
| 2841 | + sum->InPktsUnusedSA, |
|---|
| 2267 | 2842 | MACSEC_RXSC_STATS_ATTR_PAD)) |
|---|
| 2268 | 2843 | return -EMSGSIZE; |
|---|
| 2269 | 2844 | |
|---|
| 2270 | 2845 | return 0; |
|---|
| 2271 | 2846 | } |
|---|
| 2272 | 2847 | |
|---|
| 2273 | | -static int copy_tx_sc_stats(struct sk_buff *skb, |
|---|
| 2274 | | - struct pcpu_tx_sc_stats __percpu *pstats) |
|---|
| 2848 | +static void get_tx_sc_stats(struct net_device *dev, |
|---|
| 2849 | + struct macsec_tx_sc_stats *sum) |
|---|
| 2275 | 2850 | { |
|---|
| 2276 | | - struct macsec_tx_sc_stats sum = {0, }; |
|---|
| 2851 | + struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| 2277 | 2852 | int cpu; |
|---|
| 2853 | + |
|---|
| 2854 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2855 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 2856 | + const struct macsec_ops *ops; |
|---|
| 2857 | + struct macsec_context ctx; |
|---|
| 2858 | + |
|---|
| 2859 | + ops = macsec_get_ops(macsec, &ctx); |
|---|
| 2860 | + if (ops) { |
|---|
| 2861 | + ctx.stats.tx_sc_stats = sum; |
|---|
| 2862 | + ctx.secy = &macsec_priv(dev)->secy; |
|---|
| 2863 | + macsec_offload(ops->mdo_get_tx_sc_stats, &ctx); |
|---|
| 2864 | + } |
|---|
| 2865 | + return; |
|---|
| 2866 | + } |
|---|
| 2278 | 2867 | |
|---|
| 2279 | 2868 | for_each_possible_cpu(cpu) { |
|---|
| 2280 | 2869 | const struct pcpu_tx_sc_stats *stats; |
|---|
| 2281 | 2870 | struct macsec_tx_sc_stats tmp; |
|---|
| 2282 | 2871 | unsigned int start; |
|---|
| 2283 | 2872 | |
|---|
| 2284 | | - stats = per_cpu_ptr(pstats, cpu); |
|---|
| 2873 | + stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu); |
|---|
| 2285 | 2874 | do { |
|---|
| 2286 | 2875 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
|---|
| 2287 | 2876 | memcpy(&tmp, &stats->stats, sizeof(tmp)); |
|---|
| 2288 | 2877 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
|---|
| 2289 | 2878 | |
|---|
| 2290 | | - sum.OutPktsProtected += tmp.OutPktsProtected; |
|---|
| 2291 | | - sum.OutPktsEncrypted += tmp.OutPktsEncrypted; |
|---|
| 2292 | | - sum.OutOctetsProtected += tmp.OutOctetsProtected; |
|---|
| 2293 | | - sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted; |
|---|
| 2879 | + sum->OutPktsProtected += tmp.OutPktsProtected; |
|---|
| 2880 | + sum->OutPktsEncrypted += tmp.OutPktsEncrypted; |
|---|
| 2881 | + sum->OutOctetsProtected += tmp.OutOctetsProtected; |
|---|
| 2882 | + sum->OutOctetsEncrypted += tmp.OutOctetsEncrypted; |
|---|
| 2294 | 2883 | } |
|---|
| 2884 | +} |
|---|
| 2295 | 2885 | |
|---|
| 2886 | +static int copy_tx_sc_stats(struct sk_buff *skb, struct macsec_tx_sc_stats *sum) |
|---|
| 2887 | +{ |
|---|
| 2296 | 2888 | if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED, |
|---|
| 2297 | | - sum.OutPktsProtected, |
|---|
| 2889 | + sum->OutPktsProtected, |
|---|
| 2298 | 2890 | MACSEC_TXSC_STATS_ATTR_PAD) || |
|---|
| 2299 | 2891 | nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED, |
|---|
| 2300 | | - sum.OutPktsEncrypted, |
|---|
| 2892 | + sum->OutPktsEncrypted, |
|---|
| 2301 | 2893 | MACSEC_TXSC_STATS_ATTR_PAD) || |
|---|
| 2302 | 2894 | nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED, |
|---|
| 2303 | | - sum.OutOctetsProtected, |
|---|
| 2895 | + sum->OutOctetsProtected, |
|---|
| 2304 | 2896 | MACSEC_TXSC_STATS_ATTR_PAD) || |
|---|
| 2305 | 2897 | nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED, |
|---|
| 2306 | | - sum.OutOctetsEncrypted, |
|---|
| 2898 | + sum->OutOctetsEncrypted, |
|---|
| 2307 | 2899 | MACSEC_TXSC_STATS_ATTR_PAD)) |
|---|
| 2308 | 2900 | return -EMSGSIZE; |
|---|
| 2309 | 2901 | |
|---|
| 2310 | 2902 | return 0; |
|---|
| 2311 | 2903 | } |
|---|
| 2312 | 2904 | |
|---|
| 2313 | | -static int copy_secy_stats(struct sk_buff *skb, |
|---|
| 2314 | | - struct pcpu_secy_stats __percpu *pstats) |
|---|
| 2905 | +static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum) |
|---|
| 2315 | 2906 | { |
|---|
| 2316 | | - struct macsec_dev_stats sum = {0, }; |
|---|
| 2907 | + struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| 2317 | 2908 | int cpu; |
|---|
| 2909 | + |
|---|
| 2910 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 2911 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 2912 | + const struct macsec_ops *ops; |
|---|
| 2913 | + struct macsec_context ctx; |
|---|
| 2914 | + |
|---|
| 2915 | + ops = macsec_get_ops(macsec, &ctx); |
|---|
| 2916 | + if (ops) { |
|---|
| 2917 | + ctx.stats.dev_stats = sum; |
|---|
| 2918 | + ctx.secy = &macsec_priv(dev)->secy; |
|---|
| 2919 | + macsec_offload(ops->mdo_get_dev_stats, &ctx); |
|---|
| 2920 | + } |
|---|
| 2921 | + return; |
|---|
| 2922 | + } |
|---|
| 2318 | 2923 | |
|---|
| 2319 | 2924 | for_each_possible_cpu(cpu) { |
|---|
| 2320 | 2925 | const struct pcpu_secy_stats *stats; |
|---|
| 2321 | 2926 | struct macsec_dev_stats tmp; |
|---|
| 2322 | 2927 | unsigned int start; |
|---|
| 2323 | 2928 | |
|---|
| 2324 | | - stats = per_cpu_ptr(pstats, cpu); |
|---|
| 2929 | + stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu); |
|---|
| 2325 | 2930 | do { |
|---|
| 2326 | 2931 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
|---|
| 2327 | 2932 | memcpy(&tmp, &stats->stats, sizeof(tmp)); |
|---|
| 2328 | 2933 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
|---|
| 2329 | 2934 | |
|---|
| 2330 | | - sum.OutPktsUntagged += tmp.OutPktsUntagged; |
|---|
| 2331 | | - sum.InPktsUntagged += tmp.InPktsUntagged; |
|---|
| 2332 | | - sum.OutPktsTooLong += tmp.OutPktsTooLong; |
|---|
| 2333 | | - sum.InPktsNoTag += tmp.InPktsNoTag; |
|---|
| 2334 | | - sum.InPktsBadTag += tmp.InPktsBadTag; |
|---|
| 2335 | | - sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI; |
|---|
| 2336 | | - sum.InPktsNoSCI += tmp.InPktsNoSCI; |
|---|
| 2337 | | - sum.InPktsOverrun += tmp.InPktsOverrun; |
|---|
| 2935 | + sum->OutPktsUntagged += tmp.OutPktsUntagged; |
|---|
| 2936 | + sum->InPktsUntagged += tmp.InPktsUntagged; |
|---|
| 2937 | + sum->OutPktsTooLong += tmp.OutPktsTooLong; |
|---|
| 2938 | + sum->InPktsNoTag += tmp.InPktsNoTag; |
|---|
| 2939 | + sum->InPktsBadTag += tmp.InPktsBadTag; |
|---|
| 2940 | + sum->InPktsUnknownSCI += tmp.InPktsUnknownSCI; |
|---|
| 2941 | + sum->InPktsNoSCI += tmp.InPktsNoSCI; |
|---|
| 2942 | + sum->InPktsOverrun += tmp.InPktsOverrun; |
|---|
| 2338 | 2943 | } |
|---|
| 2944 | +} |
|---|
| 2339 | 2945 | |
|---|
| 2946 | +static int copy_secy_stats(struct sk_buff *skb, struct macsec_dev_stats *sum) |
|---|
| 2947 | +{ |
|---|
| 2340 | 2948 | if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED, |
|---|
| 2341 | | - sum.OutPktsUntagged, |
|---|
| 2949 | + sum->OutPktsUntagged, |
|---|
| 2342 | 2950 | MACSEC_SECY_STATS_ATTR_PAD) || |
|---|
| 2343 | 2951 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED, |
|---|
| 2344 | | - sum.InPktsUntagged, |
|---|
| 2952 | + sum->InPktsUntagged, |
|---|
| 2345 | 2953 | MACSEC_SECY_STATS_ATTR_PAD) || |
|---|
| 2346 | 2954 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG, |
|---|
| 2347 | | - sum.OutPktsTooLong, |
|---|
| 2955 | + sum->OutPktsTooLong, |
|---|
| 2348 | 2956 | MACSEC_SECY_STATS_ATTR_PAD) || |
|---|
| 2349 | 2957 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG, |
|---|
| 2350 | | - sum.InPktsNoTag, |
|---|
| 2958 | + sum->InPktsNoTag, |
|---|
| 2351 | 2959 | MACSEC_SECY_STATS_ATTR_PAD) || |
|---|
| 2352 | 2960 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG, |
|---|
| 2353 | | - sum.InPktsBadTag, |
|---|
| 2961 | + sum->InPktsBadTag, |
|---|
| 2354 | 2962 | MACSEC_SECY_STATS_ATTR_PAD) || |
|---|
| 2355 | 2963 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI, |
|---|
| 2356 | | - sum.InPktsUnknownSCI, |
|---|
| 2964 | + sum->InPktsUnknownSCI, |
|---|
| 2357 | 2965 | MACSEC_SECY_STATS_ATTR_PAD) || |
|---|
| 2358 | 2966 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI, |
|---|
| 2359 | | - sum.InPktsNoSCI, |
|---|
| 2967 | + sum->InPktsNoSCI, |
|---|
| 2360 | 2968 | MACSEC_SECY_STATS_ATTR_PAD) || |
|---|
| 2361 | 2969 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN, |
|---|
| 2362 | | - sum.InPktsOverrun, |
|---|
| 2970 | + sum->InPktsOverrun, |
|---|
| 2363 | 2971 | MACSEC_SECY_STATS_ATTR_PAD)) |
|---|
| 2364 | 2972 | return -EMSGSIZE; |
|---|
| 2365 | 2973 | |
|---|
| .. | .. |
|---|
| 2369 | 2977 | static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb) |
|---|
| 2370 | 2978 | { |
|---|
| 2371 | 2979 | struct macsec_tx_sc *tx_sc = &secy->tx_sc; |
|---|
| 2372 | | - struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY); |
|---|
| 2980 | + struct nlattr *secy_nest = nla_nest_start_noflag(skb, |
|---|
| 2981 | + MACSEC_ATTR_SECY); |
|---|
| 2373 | 2982 | u64 csid; |
|---|
| 2374 | 2983 | |
|---|
| 2375 | 2984 | if (!secy_nest) |
|---|
| .. | .. |
|---|
| 2377 | 2986 | |
|---|
| 2378 | 2987 | switch (secy->key_len) { |
|---|
| 2379 | 2988 | case MACSEC_GCM_AES_128_SAK_LEN: |
|---|
| 2380 | | - csid = MACSEC_DEFAULT_CIPHER_ID; |
|---|
| 2989 | + csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID; |
|---|
| 2381 | 2990 | break; |
|---|
| 2382 | 2991 | case MACSEC_GCM_AES_256_SAK_LEN: |
|---|
| 2383 | | - csid = MACSEC_CIPHER_ID_GCM_AES_256; |
|---|
| 2992 | + csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256; |
|---|
| 2384 | 2993 | break; |
|---|
| 2385 | 2994 | default: |
|---|
| 2386 | 2995 | goto cancel; |
|---|
| .. | .. |
|---|
| 2415 | 3024 | return 1; |
|---|
| 2416 | 3025 | } |
|---|
| 2417 | 3026 | |
|---|
| 2418 | | -static int dump_secy(struct macsec_secy *secy, struct net_device *dev, |
|---|
| 2419 | | - struct sk_buff *skb, struct netlink_callback *cb) |
|---|
| 3027 | +static noinline_for_stack int |
|---|
| 3028 | +dump_secy(struct macsec_secy *secy, struct net_device *dev, |
|---|
| 3029 | + struct sk_buff *skb, struct netlink_callback *cb) |
|---|
| 2420 | 3030 | { |
|---|
| 2421 | | - struct macsec_rx_sc *rx_sc; |
|---|
| 3031 | + struct macsec_tx_sc_stats tx_sc_stats = {0, }; |
|---|
| 3032 | + struct macsec_tx_sa_stats tx_sa_stats = {0, }; |
|---|
| 3033 | + struct macsec_rx_sc_stats rx_sc_stats = {0, }; |
|---|
| 3034 | + struct macsec_rx_sa_stats rx_sa_stats = {0, }; |
|---|
| 3035 | + struct macsec_dev *macsec = netdev_priv(dev); |
|---|
| 3036 | + struct macsec_dev_stats dev_stats = {0, }; |
|---|
| 2422 | 3037 | struct macsec_tx_sc *tx_sc = &secy->tx_sc; |
|---|
| 2423 | 3038 | struct nlattr *txsa_list, *rxsc_list; |
|---|
| 2424 | | - int i, j; |
|---|
| 2425 | | - void *hdr; |
|---|
| 3039 | + struct macsec_rx_sc *rx_sc; |
|---|
| 2426 | 3040 | struct nlattr *attr; |
|---|
| 3041 | + void *hdr; |
|---|
| 3042 | + int i, j; |
|---|
| 2427 | 3043 | |
|---|
| 2428 | 3044 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
|---|
| 2429 | 3045 | &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC); |
|---|
| .. | .. |
|---|
| 2435 | 3051 | if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex)) |
|---|
| 2436 | 3052 | goto nla_put_failure; |
|---|
| 2437 | 3053 | |
|---|
| 3054 | + attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD); |
|---|
| 3055 | + if (!attr) |
|---|
| 3056 | + goto nla_put_failure; |
|---|
| 3057 | + if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload)) |
|---|
| 3058 | + goto nla_put_failure; |
|---|
| 3059 | + nla_nest_end(skb, attr); |
|---|
| 3060 | + |
|---|
| 2438 | 3061 | if (nla_put_secy(secy, skb)) |
|---|
| 2439 | 3062 | goto nla_put_failure; |
|---|
| 2440 | 3063 | |
|---|
| 2441 | | - attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS); |
|---|
| 3064 | + attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS); |
|---|
| 2442 | 3065 | if (!attr) |
|---|
| 2443 | 3066 | goto nla_put_failure; |
|---|
| 2444 | | - if (copy_tx_sc_stats(skb, tx_sc->stats)) { |
|---|
| 3067 | + |
|---|
| 3068 | + get_tx_sc_stats(dev, &tx_sc_stats); |
|---|
| 3069 | + if (copy_tx_sc_stats(skb, &tx_sc_stats)) { |
|---|
| 2445 | 3070 | nla_nest_cancel(skb, attr); |
|---|
| 2446 | 3071 | goto nla_put_failure; |
|---|
| 2447 | 3072 | } |
|---|
| 2448 | 3073 | nla_nest_end(skb, attr); |
|---|
| 2449 | 3074 | |
|---|
| 2450 | | - attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS); |
|---|
| 3075 | + attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS); |
|---|
| 2451 | 3076 | if (!attr) |
|---|
| 2452 | 3077 | goto nla_put_failure; |
|---|
| 2453 | | - if (copy_secy_stats(skb, macsec_priv(dev)->stats)) { |
|---|
| 3078 | + get_secy_stats(dev, &dev_stats); |
|---|
| 3079 | + if (copy_secy_stats(skb, &dev_stats)) { |
|---|
| 2454 | 3080 | nla_nest_cancel(skb, attr); |
|---|
| 2455 | 3081 | goto nla_put_failure; |
|---|
| 2456 | 3082 | } |
|---|
| 2457 | 3083 | nla_nest_end(skb, attr); |
|---|
| 2458 | 3084 | |
|---|
| 2459 | | - txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST); |
|---|
| 3085 | + txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST); |
|---|
| 2460 | 3086 | if (!txsa_list) |
|---|
| 2461 | 3087 | goto nla_put_failure; |
|---|
| 2462 | 3088 | for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) { |
|---|
| 2463 | 3089 | struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]); |
|---|
| 2464 | 3090 | struct nlattr *txsa_nest; |
|---|
| 3091 | + u64 pn; |
|---|
| 3092 | + int pn_len; |
|---|
| 2465 | 3093 | |
|---|
| 2466 | 3094 | if (!tx_sa) |
|---|
| 2467 | 3095 | continue; |
|---|
| 2468 | 3096 | |
|---|
| 2469 | | - txsa_nest = nla_nest_start(skb, j++); |
|---|
| 3097 | + txsa_nest = nla_nest_start_noflag(skb, j++); |
|---|
| 2470 | 3098 | if (!txsa_nest) { |
|---|
| 2471 | 3099 | nla_nest_cancel(skb, txsa_list); |
|---|
| 2472 | 3100 | goto nla_put_failure; |
|---|
| 2473 | 3101 | } |
|---|
| 2474 | 3102 | |
|---|
| 2475 | | - if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) || |
|---|
| 2476 | | - nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) || |
|---|
| 2477 | | - nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) || |
|---|
| 2478 | | - nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) { |
|---|
| 2479 | | - nla_nest_cancel(skb, txsa_nest); |
|---|
| 2480 | | - nla_nest_cancel(skb, txsa_list); |
|---|
| 2481 | | - goto nla_put_failure; |
|---|
| 2482 | | - } |
|---|
| 2483 | | - |
|---|
| 2484 | | - attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS); |
|---|
| 3103 | + attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS); |
|---|
| 2485 | 3104 | if (!attr) { |
|---|
| 2486 | 3105 | nla_nest_cancel(skb, txsa_nest); |
|---|
| 2487 | 3106 | nla_nest_cancel(skb, txsa_list); |
|---|
| 2488 | 3107 | goto nla_put_failure; |
|---|
| 2489 | 3108 | } |
|---|
| 2490 | | - if (copy_tx_sa_stats(skb, tx_sa->stats)) { |
|---|
| 3109 | + memset(&tx_sa_stats, 0, sizeof(tx_sa_stats)); |
|---|
| 3110 | + get_tx_sa_stats(dev, i, tx_sa, &tx_sa_stats); |
|---|
| 3111 | + if (copy_tx_sa_stats(skb, &tx_sa_stats)) { |
|---|
| 2491 | 3112 | nla_nest_cancel(skb, attr); |
|---|
| 2492 | 3113 | nla_nest_cancel(skb, txsa_nest); |
|---|
| 2493 | 3114 | nla_nest_cancel(skb, txsa_list); |
|---|
| .. | .. |
|---|
| 2495 | 3116 | } |
|---|
| 2496 | 3117 | nla_nest_end(skb, attr); |
|---|
| 2497 | 3118 | |
|---|
| 3119 | + if (secy->xpn) { |
|---|
| 3120 | + pn = tx_sa->next_pn; |
|---|
| 3121 | + pn_len = MACSEC_XPN_PN_LEN; |
|---|
| 3122 | + } else { |
|---|
| 3123 | + pn = tx_sa->next_pn_halves.lower; |
|---|
| 3124 | + pn_len = MACSEC_DEFAULT_PN_LEN; |
|---|
| 3125 | + } |
|---|
| 3126 | + |
|---|
| 3127 | + if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) || |
|---|
| 3128 | + nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) || |
|---|
| 3129 | + nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) || |
|---|
| 3130 | + (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) || |
|---|
| 3131 | + nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) { |
|---|
| 3132 | + nla_nest_cancel(skb, txsa_nest); |
|---|
| 3133 | + nla_nest_cancel(skb, txsa_list); |
|---|
| 3134 | + goto nla_put_failure; |
|---|
| 3135 | + } |
|---|
| 3136 | + |
|---|
| 2498 | 3137 | nla_nest_end(skb, txsa_nest); |
|---|
| 2499 | 3138 | } |
|---|
| 2500 | 3139 | nla_nest_end(skb, txsa_list); |
|---|
| 2501 | 3140 | |
|---|
| 2502 | | - rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST); |
|---|
| 3141 | + rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST); |
|---|
| 2503 | 3142 | if (!rxsc_list) |
|---|
| 2504 | 3143 | goto nla_put_failure; |
|---|
| 2505 | 3144 | |
|---|
| .. | .. |
|---|
| 2507 | 3146 | for_each_rxsc_rtnl(secy, rx_sc) { |
|---|
| 2508 | 3147 | int k; |
|---|
| 2509 | 3148 | struct nlattr *rxsa_list; |
|---|
| 2510 | | - struct nlattr *rxsc_nest = nla_nest_start(skb, j++); |
|---|
| 3149 | + struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++); |
|---|
| 2511 | 3150 | |
|---|
| 2512 | 3151 | if (!rxsc_nest) { |
|---|
| 2513 | 3152 | nla_nest_cancel(skb, rxsc_list); |
|---|
| .. | .. |
|---|
| 2522 | 3161 | goto nla_put_failure; |
|---|
| 2523 | 3162 | } |
|---|
| 2524 | 3163 | |
|---|
| 2525 | | - attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS); |
|---|
| 3164 | + attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS); |
|---|
| 2526 | 3165 | if (!attr) { |
|---|
| 2527 | 3166 | nla_nest_cancel(skb, rxsc_nest); |
|---|
| 2528 | 3167 | nla_nest_cancel(skb, rxsc_list); |
|---|
| 2529 | 3168 | goto nla_put_failure; |
|---|
| 2530 | 3169 | } |
|---|
| 2531 | | - if (copy_rx_sc_stats(skb, rx_sc->stats)) { |
|---|
| 3170 | + memset(&rx_sc_stats, 0, sizeof(rx_sc_stats)); |
|---|
| 3171 | + get_rx_sc_stats(dev, rx_sc, &rx_sc_stats); |
|---|
| 3172 | + if (copy_rx_sc_stats(skb, &rx_sc_stats)) { |
|---|
| 2532 | 3173 | nla_nest_cancel(skb, attr); |
|---|
| 2533 | 3174 | nla_nest_cancel(skb, rxsc_nest); |
|---|
| 2534 | 3175 | nla_nest_cancel(skb, rxsc_list); |
|---|
| .. | .. |
|---|
| 2536 | 3177 | } |
|---|
| 2537 | 3178 | nla_nest_end(skb, attr); |
|---|
| 2538 | 3179 | |
|---|
| 2539 | | - rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST); |
|---|
| 3180 | + rxsa_list = nla_nest_start_noflag(skb, |
|---|
| 3181 | + MACSEC_RXSC_ATTR_SA_LIST); |
|---|
| 2540 | 3182 | if (!rxsa_list) { |
|---|
| 2541 | 3183 | nla_nest_cancel(skb, rxsc_nest); |
|---|
| 2542 | 3184 | nla_nest_cancel(skb, rxsc_list); |
|---|
| .. | .. |
|---|
| 2546 | 3188 | for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) { |
|---|
| 2547 | 3189 | struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]); |
|---|
| 2548 | 3190 | struct nlattr *rxsa_nest; |
|---|
| 3191 | + u64 pn; |
|---|
| 3192 | + int pn_len; |
|---|
| 2549 | 3193 | |
|---|
| 2550 | 3194 | if (!rx_sa) |
|---|
| 2551 | 3195 | continue; |
|---|
| 2552 | 3196 | |
|---|
| 2553 | | - rxsa_nest = nla_nest_start(skb, k++); |
|---|
| 3197 | + rxsa_nest = nla_nest_start_noflag(skb, k++); |
|---|
| 2554 | 3198 | if (!rxsa_nest) { |
|---|
| 2555 | 3199 | nla_nest_cancel(skb, rxsa_list); |
|---|
| 2556 | 3200 | nla_nest_cancel(skb, rxsc_nest); |
|---|
| .. | .. |
|---|
| 2558 | 3202 | goto nla_put_failure; |
|---|
| 2559 | 3203 | } |
|---|
| 2560 | 3204 | |
|---|
| 2561 | | - attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS); |
|---|
| 3205 | + attr = nla_nest_start_noflag(skb, |
|---|
| 3206 | + MACSEC_SA_ATTR_STATS); |
|---|
| 2562 | 3207 | if (!attr) { |
|---|
| 2563 | 3208 | nla_nest_cancel(skb, rxsa_list); |
|---|
| 2564 | 3209 | nla_nest_cancel(skb, rxsc_nest); |
|---|
| 2565 | 3210 | nla_nest_cancel(skb, rxsc_list); |
|---|
| 2566 | 3211 | goto nla_put_failure; |
|---|
| 2567 | 3212 | } |
|---|
| 2568 | | - if (copy_rx_sa_stats(skb, rx_sa->stats)) { |
|---|
| 3213 | + memset(&rx_sa_stats, 0, sizeof(rx_sa_stats)); |
|---|
| 3214 | + get_rx_sa_stats(dev, rx_sc, i, rx_sa, &rx_sa_stats); |
|---|
| 3215 | + if (copy_rx_sa_stats(skb, &rx_sa_stats)) { |
|---|
| 2569 | 3216 | nla_nest_cancel(skb, attr); |
|---|
| 2570 | 3217 | nla_nest_cancel(skb, rxsa_list); |
|---|
| 2571 | 3218 | nla_nest_cancel(skb, rxsc_nest); |
|---|
| .. | .. |
|---|
| 2574 | 3221 | } |
|---|
| 2575 | 3222 | nla_nest_end(skb, attr); |
|---|
| 2576 | 3223 | |
|---|
| 3224 | + if (secy->xpn) { |
|---|
| 3225 | + pn = rx_sa->next_pn; |
|---|
| 3226 | + pn_len = MACSEC_XPN_PN_LEN; |
|---|
| 3227 | + } else { |
|---|
| 3228 | + pn = rx_sa->next_pn_halves.lower; |
|---|
| 3229 | + pn_len = MACSEC_DEFAULT_PN_LEN; |
|---|
| 3230 | + } |
|---|
| 3231 | + |
|---|
| 2577 | 3232 | if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) || |
|---|
| 2578 | | - nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) || |
|---|
| 3233 | + nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) || |
|---|
| 2579 | 3234 | nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) || |
|---|
| 3235 | + (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) || |
|---|
| 2580 | 3236 | nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) { |
|---|
| 2581 | 3237 | nla_nest_cancel(skb, rxsa_nest); |
|---|
| 2582 | 3238 | nla_nest_cancel(skb, rxsc_nest); |
|---|
| .. | .. |
|---|
| 2638 | 3294 | return skb->len; |
|---|
| 2639 | 3295 | } |
|---|
| 2640 | 3296 | |
|---|
| 2641 | | -static const struct genl_ops macsec_genl_ops[] = { |
|---|
| 3297 | +static const struct genl_small_ops macsec_genl_ops[] = { |
|---|
| 2642 | 3298 | { |
|---|
| 2643 | 3299 | .cmd = MACSEC_CMD_GET_TXSC, |
|---|
| 3300 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2644 | 3301 | .dumpit = macsec_dump_txsc, |
|---|
| 2645 | | - .policy = macsec_genl_policy, |
|---|
| 2646 | 3302 | }, |
|---|
| 2647 | 3303 | { |
|---|
| 2648 | 3304 | .cmd = MACSEC_CMD_ADD_RXSC, |
|---|
| 3305 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2649 | 3306 | .doit = macsec_add_rxsc, |
|---|
| 2650 | | - .policy = macsec_genl_policy, |
|---|
| 2651 | 3307 | .flags = GENL_ADMIN_PERM, |
|---|
| 2652 | 3308 | }, |
|---|
| 2653 | 3309 | { |
|---|
| 2654 | 3310 | .cmd = MACSEC_CMD_DEL_RXSC, |
|---|
| 3311 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2655 | 3312 | .doit = macsec_del_rxsc, |
|---|
| 2656 | | - .policy = macsec_genl_policy, |
|---|
| 2657 | 3313 | .flags = GENL_ADMIN_PERM, |
|---|
| 2658 | 3314 | }, |
|---|
| 2659 | 3315 | { |
|---|
| 2660 | 3316 | .cmd = MACSEC_CMD_UPD_RXSC, |
|---|
| 3317 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2661 | 3318 | .doit = macsec_upd_rxsc, |
|---|
| 2662 | | - .policy = macsec_genl_policy, |
|---|
| 2663 | 3319 | .flags = GENL_ADMIN_PERM, |
|---|
| 2664 | 3320 | }, |
|---|
| 2665 | 3321 | { |
|---|
| 2666 | 3322 | .cmd = MACSEC_CMD_ADD_TXSA, |
|---|
| 3323 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2667 | 3324 | .doit = macsec_add_txsa, |
|---|
| 2668 | | - .policy = macsec_genl_policy, |
|---|
| 2669 | 3325 | .flags = GENL_ADMIN_PERM, |
|---|
| 2670 | 3326 | }, |
|---|
| 2671 | 3327 | { |
|---|
| 2672 | 3328 | .cmd = MACSEC_CMD_DEL_TXSA, |
|---|
| 3329 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2673 | 3330 | .doit = macsec_del_txsa, |
|---|
| 2674 | | - .policy = macsec_genl_policy, |
|---|
| 2675 | 3331 | .flags = GENL_ADMIN_PERM, |
|---|
| 2676 | 3332 | }, |
|---|
| 2677 | 3333 | { |
|---|
| 2678 | 3334 | .cmd = MACSEC_CMD_UPD_TXSA, |
|---|
| 3335 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2679 | 3336 | .doit = macsec_upd_txsa, |
|---|
| 2680 | | - .policy = macsec_genl_policy, |
|---|
| 2681 | 3337 | .flags = GENL_ADMIN_PERM, |
|---|
| 2682 | 3338 | }, |
|---|
| 2683 | 3339 | { |
|---|
| 2684 | 3340 | .cmd = MACSEC_CMD_ADD_RXSA, |
|---|
| 3341 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2685 | 3342 | .doit = macsec_add_rxsa, |
|---|
| 2686 | | - .policy = macsec_genl_policy, |
|---|
| 2687 | 3343 | .flags = GENL_ADMIN_PERM, |
|---|
| 2688 | 3344 | }, |
|---|
| 2689 | 3345 | { |
|---|
| 2690 | 3346 | .cmd = MACSEC_CMD_DEL_RXSA, |
|---|
| 3347 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2691 | 3348 | .doit = macsec_del_rxsa, |
|---|
| 2692 | | - .policy = macsec_genl_policy, |
|---|
| 2693 | 3349 | .flags = GENL_ADMIN_PERM, |
|---|
| 2694 | 3350 | }, |
|---|
| 2695 | 3351 | { |
|---|
| 2696 | 3352 | .cmd = MACSEC_CMD_UPD_RXSA, |
|---|
| 3353 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 2697 | 3354 | .doit = macsec_upd_rxsa, |
|---|
| 2698 | | - .policy = macsec_genl_policy, |
|---|
| 3355 | + .flags = GENL_ADMIN_PERM, |
|---|
| 3356 | + }, |
|---|
| 3357 | + { |
|---|
| 3358 | + .cmd = MACSEC_CMD_UPD_OFFLOAD, |
|---|
| 3359 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
|---|
| 3360 | + .doit = macsec_upd_offload, |
|---|
| 2699 | 3361 | .flags = GENL_ADMIN_PERM, |
|---|
| 2700 | 3362 | }, |
|---|
| 2701 | 3363 | }; |
|---|
| .. | .. |
|---|
| 2705 | 3367 | .hdrsize = 0, |
|---|
| 2706 | 3368 | .version = MACSEC_GENL_VERSION, |
|---|
| 2707 | 3369 | .maxattr = MACSEC_ATTR_MAX, |
|---|
| 3370 | + .policy = macsec_genl_policy, |
|---|
| 2708 | 3371 | .netnsok = true, |
|---|
| 2709 | 3372 | .module = THIS_MODULE, |
|---|
| 2710 | | - .ops = macsec_genl_ops, |
|---|
| 2711 | | - .n_ops = ARRAY_SIZE(macsec_genl_ops), |
|---|
| 3373 | + .small_ops = macsec_genl_ops, |
|---|
| 3374 | + .n_small_ops = ARRAY_SIZE(macsec_genl_ops), |
|---|
| 2712 | 3375 | }; |
|---|
| 2713 | 3376 | |
|---|
| 2714 | 3377 | static netdev_tx_t macsec_start_xmit(struct sk_buff *skb, |
|---|
| .. | .. |
|---|
| 2718 | 3381 | struct macsec_secy *secy = &macsec->secy; |
|---|
| 2719 | 3382 | struct pcpu_secy_stats *secy_stats; |
|---|
| 2720 | 3383 | int ret, len; |
|---|
| 3384 | + |
|---|
| 3385 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
|---|
| 3386 | + skb->dev = macsec->real_dev; |
|---|
| 3387 | + return dev_queue_xmit(skb); |
|---|
| 3388 | + } |
|---|
| 2721 | 3389 | |
|---|
| 2722 | 3390 | /* 10.5 */ |
|---|
| 2723 | 3391 | if (!secy->protect_frames) { |
|---|
| .. | .. |
|---|
| 2756 | 3424 | |
|---|
| 2757 | 3425 | #define MACSEC_FEATURES \ |
|---|
| 2758 | 3426 | (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST) |
|---|
| 2759 | | -static struct lock_class_key macsec_netdev_addr_lock_key; |
|---|
| 2760 | 3427 | |
|---|
| 2761 | 3428 | static int macsec_dev_init(struct net_device *dev) |
|---|
| 2762 | 3429 | { |
|---|
| .. | .. |
|---|
| 2833 | 3500 | goto clear_allmulti; |
|---|
| 2834 | 3501 | } |
|---|
| 2835 | 3502 | |
|---|
| 3503 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 3504 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 3505 | + const struct macsec_ops *ops; |
|---|
| 3506 | + struct macsec_context ctx; |
|---|
| 3507 | + |
|---|
| 3508 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 3509 | + if (!ops) { |
|---|
| 3510 | + err = -EOPNOTSUPP; |
|---|
| 3511 | + goto clear_allmulti; |
|---|
| 3512 | + } |
|---|
| 3513 | + |
|---|
| 3514 | + ctx.secy = &macsec->secy; |
|---|
| 3515 | + err = macsec_offload(ops->mdo_dev_open, &ctx); |
|---|
| 3516 | + if (err) |
|---|
| 3517 | + goto clear_allmulti; |
|---|
| 3518 | + } |
|---|
| 3519 | + |
|---|
| 2836 | 3520 | if (netif_carrier_ok(real_dev)) |
|---|
| 2837 | 3521 | netif_carrier_on(dev); |
|---|
| 2838 | 3522 | |
|---|
| .. | .. |
|---|
| 2852 | 3536 | struct net_device *real_dev = macsec->real_dev; |
|---|
| 2853 | 3537 | |
|---|
| 2854 | 3538 | netif_carrier_off(dev); |
|---|
| 3539 | + |
|---|
| 3540 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 3541 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 3542 | + const struct macsec_ops *ops; |
|---|
| 3543 | + struct macsec_context ctx; |
|---|
| 3544 | + |
|---|
| 3545 | + ops = macsec_get_ops(macsec, &ctx); |
|---|
| 3546 | + if (ops) { |
|---|
| 3547 | + ctx.secy = &macsec->secy; |
|---|
| 3548 | + macsec_offload(ops->mdo_dev_stop, &ctx); |
|---|
| 3549 | + } |
|---|
| 3550 | + } |
|---|
| 2855 | 3551 | |
|---|
| 2856 | 3552 | dev_mc_unsync(real_dev, dev); |
|---|
| 2857 | 3553 | dev_uc_unsync(real_dev, dev); |
|---|
| .. | .. |
|---|
| 2890 | 3586 | dev_uc_sync(real_dev, dev); |
|---|
| 2891 | 3587 | } |
|---|
| 2892 | 3588 | |
|---|
| 2893 | | -static sci_t dev_to_sci(struct net_device *dev, __be16 port) |
|---|
| 2894 | | -{ |
|---|
| 2895 | | - return make_sci(dev->dev_addr, port); |
|---|
| 2896 | | -} |
|---|
| 2897 | | - |
|---|
| 2898 | 3589 | static int macsec_set_mac_address(struct net_device *dev, void *p) |
|---|
| 2899 | 3590 | { |
|---|
| 2900 | 3591 | struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| .. | .. |
|---|
| 2917 | 3608 | out: |
|---|
| 2918 | 3609 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
|---|
| 2919 | 3610 | macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES); |
|---|
| 3611 | + |
|---|
| 3612 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 3613 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 3614 | + const struct macsec_ops *ops; |
|---|
| 3615 | + struct macsec_context ctx; |
|---|
| 3616 | + |
|---|
| 3617 | + ops = macsec_get_ops(macsec, &ctx); |
|---|
| 3618 | + if (ops) { |
|---|
| 3619 | + ctx.secy = &macsec->secy; |
|---|
| 3620 | + macsec_offload(ops->mdo_upd_secy, &ctx); |
|---|
| 3621 | + } |
|---|
| 3622 | + } |
|---|
| 3623 | + |
|---|
| 2920 | 3624 | return 0; |
|---|
| 2921 | 3625 | } |
|---|
| 2922 | 3626 | |
|---|
| .. | .. |
|---|
| 2936 | 3640 | static void macsec_get_stats64(struct net_device *dev, |
|---|
| 2937 | 3641 | struct rtnl_link_stats64 *s) |
|---|
| 2938 | 3642 | { |
|---|
| 2939 | | - int cpu; |
|---|
| 2940 | | - |
|---|
| 2941 | 3643 | if (!dev->tstats) |
|---|
| 2942 | 3644 | return; |
|---|
| 2943 | 3645 | |
|---|
| 2944 | | - for_each_possible_cpu(cpu) { |
|---|
| 2945 | | - struct pcpu_sw_netstats *stats; |
|---|
| 2946 | | - struct pcpu_sw_netstats tmp; |
|---|
| 2947 | | - int start; |
|---|
| 2948 | | - |
|---|
| 2949 | | - stats = per_cpu_ptr(dev->tstats, cpu); |
|---|
| 2950 | | - do { |
|---|
| 2951 | | - start = u64_stats_fetch_begin_irq(&stats->syncp); |
|---|
| 2952 | | - tmp.rx_packets = stats->rx_packets; |
|---|
| 2953 | | - tmp.rx_bytes = stats->rx_bytes; |
|---|
| 2954 | | - tmp.tx_packets = stats->tx_packets; |
|---|
| 2955 | | - tmp.tx_bytes = stats->tx_bytes; |
|---|
| 2956 | | - } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
|---|
| 2957 | | - |
|---|
| 2958 | | - s->rx_packets += tmp.rx_packets; |
|---|
| 2959 | | - s->rx_bytes += tmp.rx_bytes; |
|---|
| 2960 | | - s->tx_packets += tmp.tx_packets; |
|---|
| 2961 | | - s->tx_bytes += tmp.tx_bytes; |
|---|
| 2962 | | - } |
|---|
| 3646 | + dev_fetch_sw_netstats(s, dev->tstats); |
|---|
| 2963 | 3647 | |
|---|
| 2964 | 3648 | s->rx_dropped = dev->stats.rx_dropped; |
|---|
| 2965 | 3649 | s->tx_dropped = dev->stats.tx_dropped; |
|---|
| .. | .. |
|---|
| 2969 | 3653 | { |
|---|
| 2970 | 3654 | return macsec_priv(dev)->real_dev->ifindex; |
|---|
| 2971 | 3655 | } |
|---|
| 2972 | | - |
|---|
| 2973 | | - |
|---|
| 2974 | | -static int macsec_get_nest_level(struct net_device *dev) |
|---|
| 2975 | | -{ |
|---|
| 2976 | | - return macsec_priv(dev)->nest_level; |
|---|
| 2977 | | -} |
|---|
| 2978 | | - |
|---|
| 2979 | 3656 | |
|---|
| 2980 | 3657 | static const struct net_device_ops macsec_netdev_ops = { |
|---|
| 2981 | 3658 | .ndo_init = macsec_dev_init, |
|---|
| .. | .. |
|---|
| 2990 | 3667 | .ndo_start_xmit = macsec_start_xmit, |
|---|
| 2991 | 3668 | .ndo_get_stats64 = macsec_get_stats64, |
|---|
| 2992 | 3669 | .ndo_get_iflink = macsec_get_iflink, |
|---|
| 2993 | | - .ndo_get_lock_subclass = macsec_get_nest_level, |
|---|
| 2994 | 3670 | }; |
|---|
| 2995 | 3671 | |
|---|
| 2996 | 3672 | static const struct device_type macsec_type = { |
|---|
| .. | .. |
|---|
| 3011 | 3687 | [IFLA_MACSEC_SCB] = { .type = NLA_U8 }, |
|---|
| 3012 | 3688 | [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 }, |
|---|
| 3013 | 3689 | [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 }, |
|---|
| 3690 | + [IFLA_MACSEC_OFFLOAD] = { .type = NLA_U8 }, |
|---|
| 3014 | 3691 | }; |
|---|
| 3015 | 3692 | |
|---|
| 3016 | 3693 | static void macsec_free_netdev(struct net_device *dev) |
|---|
| .. | .. |
|---|
| 3054 | 3731 | secy->operational = tx_sa && tx_sa->active; |
|---|
| 3055 | 3732 | } |
|---|
| 3056 | 3733 | |
|---|
| 3057 | | - if (data[IFLA_MACSEC_WINDOW]) |
|---|
| 3058 | | - secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]); |
|---|
| 3059 | | - |
|---|
| 3060 | 3734 | if (data[IFLA_MACSEC_ENCRYPT]) |
|---|
| 3061 | 3735 | tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]); |
|---|
| 3062 | 3736 | |
|---|
| .. | .. |
|---|
| 3083 | 3757 | case MACSEC_CIPHER_ID_GCM_AES_128: |
|---|
| 3084 | 3758 | case MACSEC_DEFAULT_CIPHER_ID: |
|---|
| 3085 | 3759 | secy->key_len = MACSEC_GCM_AES_128_SAK_LEN; |
|---|
| 3760 | + secy->xpn = false; |
|---|
| 3086 | 3761 | break; |
|---|
| 3087 | 3762 | case MACSEC_CIPHER_ID_GCM_AES_256: |
|---|
| 3088 | 3763 | secy->key_len = MACSEC_GCM_AES_256_SAK_LEN; |
|---|
| 3764 | + secy->xpn = false; |
|---|
| 3765 | + break; |
|---|
| 3766 | + case MACSEC_CIPHER_ID_GCM_AES_XPN_128: |
|---|
| 3767 | + secy->key_len = MACSEC_GCM_AES_128_SAK_LEN; |
|---|
| 3768 | + secy->xpn = true; |
|---|
| 3769 | + break; |
|---|
| 3770 | + case MACSEC_CIPHER_ID_GCM_AES_XPN_256: |
|---|
| 3771 | + secy->key_len = MACSEC_GCM_AES_256_SAK_LEN; |
|---|
| 3772 | + secy->xpn = true; |
|---|
| 3089 | 3773 | break; |
|---|
| 3090 | 3774 | default: |
|---|
| 3091 | 3775 | return -EINVAL; |
|---|
| 3092 | 3776 | } |
|---|
| 3777 | + } |
|---|
| 3778 | + |
|---|
| 3779 | + if (data[IFLA_MACSEC_WINDOW]) { |
|---|
| 3780 | + secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]); |
|---|
| 3781 | + |
|---|
| 3782 | + /* IEEE 802.1AEbw-2013 10.7.8 - maximum replay window |
|---|
| 3783 | + * for XPN cipher suites */ |
|---|
| 3784 | + if (secy->xpn && |
|---|
| 3785 | + secy->replay_window > MACSEC_XPN_MAX_REPLAY_WINDOW) |
|---|
| 3786 | + return -EINVAL; |
|---|
| 3093 | 3787 | } |
|---|
| 3094 | 3788 | |
|---|
| 3095 | 3789 | return 0; |
|---|
| .. | .. |
|---|
| 3099 | 3793 | struct nlattr *data[], |
|---|
| 3100 | 3794 | struct netlink_ext_ack *extack) |
|---|
| 3101 | 3795 | { |
|---|
| 3796 | + struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| 3797 | + struct macsec_tx_sc tx_sc; |
|---|
| 3798 | + struct macsec_secy secy; |
|---|
| 3799 | + int ret; |
|---|
| 3800 | + |
|---|
| 3102 | 3801 | if (!data) |
|---|
| 3103 | 3802 | return 0; |
|---|
| 3104 | 3803 | |
|---|
| .. | .. |
|---|
| 3108 | 3807 | data[IFLA_MACSEC_PORT]) |
|---|
| 3109 | 3808 | return -EINVAL; |
|---|
| 3110 | 3809 | |
|---|
| 3111 | | - return macsec_changelink_common(dev, data); |
|---|
| 3810 | + /* Keep a copy of unmodified secy and tx_sc, in case the offload |
|---|
| 3811 | + * propagation fails, to revert macsec_changelink_common. |
|---|
| 3812 | + */ |
|---|
| 3813 | + memcpy(&secy, &macsec->secy, sizeof(secy)); |
|---|
| 3814 | + memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc)); |
|---|
| 3815 | + |
|---|
| 3816 | + ret = macsec_changelink_common(dev, data); |
|---|
| 3817 | + if (ret) |
|---|
| 3818 | + goto cleanup; |
|---|
| 3819 | + |
|---|
| 3820 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 3821 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 3822 | + const struct macsec_ops *ops; |
|---|
| 3823 | + struct macsec_context ctx; |
|---|
| 3824 | + |
|---|
| 3825 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 3826 | + if (!ops) { |
|---|
| 3827 | + ret = -EOPNOTSUPP; |
|---|
| 3828 | + goto cleanup; |
|---|
| 3829 | + } |
|---|
| 3830 | + |
|---|
| 3831 | + ctx.secy = &macsec->secy; |
|---|
| 3832 | + ret = macsec_offload(ops->mdo_upd_secy, &ctx); |
|---|
| 3833 | + if (ret) |
|---|
| 3834 | + goto cleanup; |
|---|
| 3835 | + } |
|---|
| 3836 | + |
|---|
| 3837 | + return 0; |
|---|
| 3838 | + |
|---|
| 3839 | +cleanup: |
|---|
| 3840 | + memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc)); |
|---|
| 3841 | + memcpy(&macsec->secy, &secy, sizeof(secy)); |
|---|
| 3842 | + |
|---|
| 3843 | + return ret; |
|---|
| 3112 | 3844 | } |
|---|
| 3113 | 3845 | |
|---|
| 3114 | 3846 | static void macsec_del_dev(struct macsec_dev *macsec) |
|---|
| .. | .. |
|---|
| 3136 | 3868 | { |
|---|
| 3137 | 3869 | struct macsec_dev *macsec = macsec_priv(dev); |
|---|
| 3138 | 3870 | struct net_device *real_dev = macsec->real_dev; |
|---|
| 3871 | + |
|---|
| 3872 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 3873 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 3874 | + const struct macsec_ops *ops; |
|---|
| 3875 | + struct macsec_context ctx; |
|---|
| 3876 | + |
|---|
| 3877 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
|---|
| 3878 | + if (ops) { |
|---|
| 3879 | + ctx.secy = &macsec->secy; |
|---|
| 3880 | + macsec_offload(ops->mdo_del_secy, &ctx); |
|---|
| 3881 | + } |
|---|
| 3882 | + } |
|---|
| 3139 | 3883 | |
|---|
| 3140 | 3884 | unregister_netdevice_queue(dev, head); |
|---|
| 3141 | 3885 | list_del_rcu(&macsec->secys); |
|---|
| .. | .. |
|---|
| 3224 | 3968 | secy->validate_frames = MACSEC_VALIDATE_DEFAULT; |
|---|
| 3225 | 3969 | secy->protect_frames = true; |
|---|
| 3226 | 3970 | secy->replay_protect = false; |
|---|
| 3971 | + secy->xpn = DEFAULT_XPN; |
|---|
| 3227 | 3972 | |
|---|
| 3228 | 3973 | secy->sci = sci; |
|---|
| 3229 | 3974 | secy->tx_sc.active = true; |
|---|
| .. | .. |
|---|
| 3235 | 3980 | |
|---|
| 3236 | 3981 | return 0; |
|---|
| 3237 | 3982 | } |
|---|
| 3983 | + |
|---|
| 3984 | +static struct lock_class_key macsec_netdev_addr_lock_key; |
|---|
| 3238 | 3985 | |
|---|
| 3239 | 3986 | static int macsec_newlink(struct net *net, struct net_device *dev, |
|---|
| 3240 | 3987 | struct nlattr *tb[], struct nlattr *data[], |
|---|
| .. | .. |
|---|
| 3258 | 4005 | dev->priv_flags |= IFF_MACSEC; |
|---|
| 3259 | 4006 | |
|---|
| 3260 | 4007 | macsec->real_dev = real_dev; |
|---|
| 4008 | + |
|---|
| 4009 | + if (data && data[IFLA_MACSEC_OFFLOAD]) |
|---|
| 4010 | + macsec->offload = nla_get_offload(data[IFLA_MACSEC_OFFLOAD]); |
|---|
| 4011 | + else |
|---|
| 4012 | + /* MACsec offloading is off by default */ |
|---|
| 4013 | + macsec->offload = MACSEC_OFFLOAD_OFF; |
|---|
| 4014 | + |
|---|
| 4015 | + /* Check if the offloading mode is supported by the underlying layers */ |
|---|
| 4016 | + if (macsec->offload != MACSEC_OFFLOAD_OFF && |
|---|
| 4017 | + !macsec_check_offload(macsec->offload, macsec)) |
|---|
| 4018 | + return -EOPNOTSUPP; |
|---|
| 3261 | 4019 | |
|---|
| 3262 | 4020 | /* send_sci must be set to true when transmit sci explicitly is set */ |
|---|
| 3263 | 4021 | if ((data && data[IFLA_MACSEC_SCI]) && |
|---|
| .. | .. |
|---|
| 3284 | 4042 | if (err < 0) |
|---|
| 3285 | 4043 | return err; |
|---|
| 3286 | 4044 | |
|---|
| 3287 | | - macsec->nest_level = dev_get_nest_level(real_dev) + 1; |
|---|
| 3288 | 4045 | netdev_lockdep_set_classes(dev); |
|---|
| 3289 | | - lockdep_set_class_and_subclass(&dev->addr_list_lock, |
|---|
| 3290 | | - &macsec_netdev_addr_lock_key, |
|---|
| 3291 | | - macsec_get_nest_level(dev)); |
|---|
| 4046 | + lockdep_set_class(&dev->addr_list_lock, |
|---|
| 4047 | + &macsec_netdev_addr_lock_key); |
|---|
| 3292 | 4048 | |
|---|
| 3293 | 4049 | err = netdev_upper_dev_link(real_dev, dev, extack); |
|---|
| 3294 | 4050 | if (err < 0) |
|---|
| .. | .. |
|---|
| 3317 | 4073 | err = macsec_changelink_common(dev, data); |
|---|
| 3318 | 4074 | if (err) |
|---|
| 3319 | 4075 | goto del_dev; |
|---|
| 4076 | + } |
|---|
| 4077 | + |
|---|
| 4078 | + /* If h/w offloading is available, propagate to the device */ |
|---|
| 4079 | + if (macsec_is_offloaded(macsec)) { |
|---|
| 4080 | + const struct macsec_ops *ops; |
|---|
| 4081 | + struct macsec_context ctx; |
|---|
| 4082 | + |
|---|
| 4083 | + ops = macsec_get_ops(macsec, &ctx); |
|---|
| 4084 | + if (ops) { |
|---|
| 4085 | + ctx.secy = &macsec->secy; |
|---|
| 4086 | + err = macsec_offload(ops->mdo_add_secy, &ctx); |
|---|
| 4087 | + if (err) |
|---|
| 4088 | + goto del_dev; |
|---|
| 4089 | + } |
|---|
| 3320 | 4090 | } |
|---|
| 3321 | 4091 | |
|---|
| 3322 | 4092 | err = register_macsec_dev(real_dev, dev); |
|---|
| .. | .. |
|---|
| 3371 | 4141 | switch (csid) { |
|---|
| 3372 | 4142 | case MACSEC_CIPHER_ID_GCM_AES_128: |
|---|
| 3373 | 4143 | case MACSEC_CIPHER_ID_GCM_AES_256: |
|---|
| 4144 | + case MACSEC_CIPHER_ID_GCM_AES_XPN_128: |
|---|
| 4145 | + case MACSEC_CIPHER_ID_GCM_AES_XPN_256: |
|---|
| 3374 | 4146 | case MACSEC_DEFAULT_CIPHER_ID: |
|---|
| 3375 | 4147 | if (icv_len < MACSEC_MIN_ICV_LEN || |
|---|
| 3376 | 4148 | icv_len > MACSEC_STD_ICV_LEN) |
|---|
| .. | .. |
|---|
| 3444 | 4216 | |
|---|
| 3445 | 4217 | switch (secy->key_len) { |
|---|
| 3446 | 4218 | case MACSEC_GCM_AES_128_SAK_LEN: |
|---|
| 3447 | | - csid = MACSEC_DEFAULT_CIPHER_ID; |
|---|
| 4219 | + csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID; |
|---|
| 3448 | 4220 | break; |
|---|
| 3449 | 4221 | case MACSEC_GCM_AES_256_SAK_LEN: |
|---|
| 3450 | | - csid = MACSEC_CIPHER_ID_GCM_AES_256; |
|---|
| 4222 | + csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256; |
|---|
| 3451 | 4223 | break; |
|---|
| 3452 | 4224 | default: |
|---|
| 3453 | 4225 | goto nla_put_failure; |
|---|