.. | .. |
---|
| 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: |
---|
.. | .. |
---|
1348 | 1341 | return PTR_ERR(rx_sa->key.tfm); |
---|
1349 | 1342 | } |
---|
1350 | 1343 | |
---|
| 1344 | + rx_sa->ssci = MACSEC_UNDEF_SSCI; |
---|
1351 | 1345 | rx_sa->active = false; |
---|
1352 | 1346 | rx_sa->next_pn = 1; |
---|
1353 | 1347 | refcount_set(&rx_sa->refcnt, 1); |
---|
.. | .. |
---|
1396 | 1390 | return NULL; |
---|
1397 | 1391 | } |
---|
1398 | 1392 | |
---|
1399 | | -static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci) |
---|
| 1393 | +static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci, |
---|
| 1394 | + bool active) |
---|
1400 | 1395 | { |
---|
1401 | 1396 | struct macsec_rx_sc *rx_sc; |
---|
1402 | 1397 | struct macsec_dev *macsec; |
---|
.. | .. |
---|
1420 | 1415 | } |
---|
1421 | 1416 | |
---|
1422 | 1417 | rx_sc->sci = sci; |
---|
1423 | | - rx_sc->active = true; |
---|
| 1418 | + rx_sc->active = active; |
---|
1424 | 1419 | refcount_set(&rx_sc->refcnt, 1); |
---|
1425 | 1420 | |
---|
1426 | 1421 | secy = &macsec_priv(dev)->secy; |
---|
.. | .. |
---|
1446 | 1441 | return PTR_ERR(tx_sa->key.tfm); |
---|
1447 | 1442 | } |
---|
1448 | 1443 | |
---|
| 1444 | + tx_sa->ssci = MACSEC_UNDEF_SSCI; |
---|
1449 | 1445 | tx_sa->active = false; |
---|
1450 | 1446 | refcount_set(&tx_sa->refcnt, 1); |
---|
1451 | 1447 | spin_lock_init(&tx_sa->lock); |
---|
.. | .. |
---|
1478 | 1474 | return dev; |
---|
1479 | 1475 | } |
---|
1480 | 1476 | |
---|
| 1477 | +static enum macsec_offload nla_get_offload(const struct nlattr *nla) |
---|
| 1478 | +{ |
---|
| 1479 | + return (__force enum macsec_offload)nla_get_u8(nla); |
---|
| 1480 | +} |
---|
| 1481 | + |
---|
1481 | 1482 | static sci_t nla_get_sci(const struct nlattr *nla) |
---|
1482 | 1483 | { |
---|
1483 | 1484 | return (__force sci_t)nla_get_u64(nla); |
---|
.. | .. |
---|
1487 | 1488 | int padattr) |
---|
1488 | 1489 | { |
---|
1489 | 1490 | return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr); |
---|
| 1491 | +} |
---|
| 1492 | + |
---|
| 1493 | +static ssci_t nla_get_ssci(const struct nlattr *nla) |
---|
| 1494 | +{ |
---|
| 1495 | + return (__force ssci_t)nla_get_u32(nla); |
---|
| 1496 | +} |
---|
| 1497 | + |
---|
| 1498 | +static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value) |
---|
| 1499 | +{ |
---|
| 1500 | + return nla_put_u32(skb, attrtype, (__force u64)value); |
---|
1490 | 1501 | } |
---|
1491 | 1502 | |
---|
1492 | 1503 | static struct macsec_tx_sa *get_txsa_from_nl(struct net *net, |
---|
.. | .. |
---|
1589 | 1600 | return rx_sa; |
---|
1590 | 1601 | } |
---|
1591 | 1602 | |
---|
1592 | | - |
---|
1593 | 1603 | static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = { |
---|
1594 | 1604 | [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 }, |
---|
1595 | 1605 | [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED }, |
---|
1596 | 1606 | [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED }, |
---|
| 1607 | + [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED }, |
---|
1597 | 1608 | }; |
---|
1598 | 1609 | |
---|
1599 | 1610 | static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = { |
---|
.. | .. |
---|
1604 | 1615 | static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = { |
---|
1605 | 1616 | [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 }, |
---|
1606 | 1617 | [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 }, |
---|
1607 | | - [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 }, |
---|
| 1618 | + [MACSEC_SA_ATTR_PN] = NLA_POLICY_MIN_LEN(4), |
---|
1608 | 1619 | [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY, |
---|
1609 | 1620 | .len = MACSEC_KEYID_LEN, }, |
---|
1610 | 1621 | [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY, |
---|
1611 | 1622 | .len = MACSEC_MAX_KEY_LEN, }, |
---|
| 1623 | + [MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 }, |
---|
| 1624 | + [MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY, |
---|
| 1625 | + .len = MACSEC_SALT_LEN, }, |
---|
1612 | 1626 | }; |
---|
| 1627 | + |
---|
| 1628 | +static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = { |
---|
| 1629 | + [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 }, |
---|
| 1630 | +}; |
---|
| 1631 | + |
---|
| 1632 | +/* Offloads an operation to a device driver */ |
---|
| 1633 | +static int macsec_offload(int (* const func)(struct macsec_context *), |
---|
| 1634 | + struct macsec_context *ctx) |
---|
| 1635 | +{ |
---|
| 1636 | + int ret; |
---|
| 1637 | + |
---|
| 1638 | + if (unlikely(!func)) |
---|
| 1639 | + return 0; |
---|
| 1640 | + |
---|
| 1641 | + if (ctx->offload == MACSEC_OFFLOAD_PHY) |
---|
| 1642 | + mutex_lock(&ctx->phydev->lock); |
---|
| 1643 | + |
---|
| 1644 | + /* Phase I: prepare. The drive should fail here if there are going to be |
---|
| 1645 | + * issues in the commit phase. |
---|
| 1646 | + */ |
---|
| 1647 | + ctx->prepare = true; |
---|
| 1648 | + ret = (*func)(ctx); |
---|
| 1649 | + if (ret) |
---|
| 1650 | + goto phy_unlock; |
---|
| 1651 | + |
---|
| 1652 | + /* Phase II: commit. This step cannot fail. */ |
---|
| 1653 | + ctx->prepare = false; |
---|
| 1654 | + ret = (*func)(ctx); |
---|
| 1655 | + /* This should never happen: commit is not allowed to fail */ |
---|
| 1656 | + if (unlikely(ret)) |
---|
| 1657 | + WARN(1, "MACsec offloading commit failed (%d)\n", ret); |
---|
| 1658 | + |
---|
| 1659 | +phy_unlock: |
---|
| 1660 | + if (ctx->offload == MACSEC_OFFLOAD_PHY) |
---|
| 1661 | + mutex_unlock(&ctx->phydev->lock); |
---|
| 1662 | + |
---|
| 1663 | + return ret; |
---|
| 1664 | +} |
---|
1613 | 1665 | |
---|
1614 | 1666 | static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa) |
---|
1615 | 1667 | { |
---|
1616 | 1668 | if (!attrs[MACSEC_ATTR_SA_CONFIG]) |
---|
1617 | 1669 | return -EINVAL; |
---|
1618 | 1670 | |
---|
1619 | | - if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX, |
---|
1620 | | - attrs[MACSEC_ATTR_SA_CONFIG], |
---|
1621 | | - macsec_genl_sa_policy, NULL)) |
---|
| 1671 | + if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL)) |
---|
1622 | 1672 | return -EINVAL; |
---|
1623 | 1673 | |
---|
1624 | 1674 | return 0; |
---|
.. | .. |
---|
1629 | 1679 | if (!attrs[MACSEC_ATTR_RXSC_CONFIG]) |
---|
1630 | 1680 | return -EINVAL; |
---|
1631 | 1681 | |
---|
1632 | | - if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX, |
---|
1633 | | - attrs[MACSEC_ATTR_RXSC_CONFIG], |
---|
1634 | | - macsec_genl_rxsc_policy, NULL)) |
---|
| 1682 | + if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL)) |
---|
1635 | 1683 | return -EINVAL; |
---|
1636 | 1684 | |
---|
1637 | 1685 | return 0; |
---|
.. | .. |
---|
1647 | 1695 | if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN) |
---|
1648 | 1696 | return false; |
---|
1649 | 1697 | |
---|
1650 | | - if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0) |
---|
| 1698 | + if (attrs[MACSEC_SA_ATTR_PN] && |
---|
| 1699 | + nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0) |
---|
1651 | 1700 | return false; |
---|
1652 | 1701 | |
---|
1653 | 1702 | if (attrs[MACSEC_SA_ATTR_ACTIVE]) { |
---|
.. | .. |
---|
1669 | 1718 | struct macsec_rx_sc *rx_sc; |
---|
1670 | 1719 | struct macsec_rx_sa *rx_sa; |
---|
1671 | 1720 | unsigned char assoc_num; |
---|
| 1721 | + int pn_len; |
---|
1672 | 1722 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
---|
1673 | 1723 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
---|
1674 | 1724 | int err; |
---|
.. | .. |
---|
1701 | 1751 | return -EINVAL; |
---|
1702 | 1752 | } |
---|
1703 | 1753 | |
---|
| 1754 | + pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN; |
---|
| 1755 | + if (tb_sa[MACSEC_SA_ATTR_PN] && |
---|
| 1756 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) { |
---|
| 1757 | + pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n", |
---|
| 1758 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len); |
---|
| 1759 | + rtnl_unlock(); |
---|
| 1760 | + return -EINVAL; |
---|
| 1761 | + } |
---|
| 1762 | + |
---|
| 1763 | + if (secy->xpn) { |
---|
| 1764 | + if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) { |
---|
| 1765 | + rtnl_unlock(); |
---|
| 1766 | + return -EINVAL; |
---|
| 1767 | + } |
---|
| 1768 | + |
---|
| 1769 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) { |
---|
| 1770 | + pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n", |
---|
| 1771 | + nla_len(tb_sa[MACSEC_SA_ATTR_SALT]), |
---|
| 1772 | + MACSEC_SALT_LEN); |
---|
| 1773 | + rtnl_unlock(); |
---|
| 1774 | + return -EINVAL; |
---|
| 1775 | + } |
---|
| 1776 | + } |
---|
| 1777 | + |
---|
1704 | 1778 | rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]); |
---|
1705 | 1779 | if (rx_sa) { |
---|
1706 | 1780 | rtnl_unlock(); |
---|
.. | .. |
---|
1723 | 1797 | |
---|
1724 | 1798 | if (tb_sa[MACSEC_SA_ATTR_PN]) { |
---|
1725 | 1799 | spin_lock_bh(&rx_sa->lock); |
---|
1726 | | - rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]); |
---|
| 1800 | + rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]); |
---|
1727 | 1801 | spin_unlock_bh(&rx_sa->lock); |
---|
1728 | 1802 | } |
---|
1729 | 1803 | |
---|
1730 | 1804 | if (tb_sa[MACSEC_SA_ATTR_ACTIVE]) |
---|
1731 | 1805 | rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]); |
---|
1732 | 1806 | |
---|
1733 | | - nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN); |
---|
1734 | 1807 | rx_sa->sc = rx_sc; |
---|
| 1808 | + |
---|
| 1809 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 1810 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 1811 | + const struct macsec_ops *ops; |
---|
| 1812 | + struct macsec_context ctx; |
---|
| 1813 | + |
---|
| 1814 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 1815 | + if (!ops) { |
---|
| 1816 | + err = -EOPNOTSUPP; |
---|
| 1817 | + goto cleanup; |
---|
| 1818 | + } |
---|
| 1819 | + |
---|
| 1820 | + ctx.sa.assoc_num = assoc_num; |
---|
| 1821 | + ctx.sa.rx_sa = rx_sa; |
---|
| 1822 | + ctx.secy = secy; |
---|
| 1823 | + memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]), |
---|
| 1824 | + secy->key_len); |
---|
| 1825 | + |
---|
| 1826 | + err = macsec_offload(ops->mdo_add_rxsa, &ctx); |
---|
| 1827 | + memzero_explicit(ctx.sa.key, secy->key_len); |
---|
| 1828 | + if (err) |
---|
| 1829 | + goto cleanup; |
---|
| 1830 | + } |
---|
| 1831 | + |
---|
| 1832 | + if (secy->xpn) { |
---|
| 1833 | + rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]); |
---|
| 1834 | + nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT], |
---|
| 1835 | + MACSEC_SALT_LEN); |
---|
| 1836 | + } |
---|
| 1837 | + |
---|
| 1838 | + nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN); |
---|
1735 | 1839 | rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa); |
---|
1736 | 1840 | |
---|
1737 | 1841 | rtnl_unlock(); |
---|
1738 | 1842 | |
---|
1739 | 1843 | return 0; |
---|
| 1844 | + |
---|
| 1845 | +cleanup: |
---|
| 1846 | + macsec_rxsa_put(rx_sa); |
---|
| 1847 | + rtnl_unlock(); |
---|
| 1848 | + return err; |
---|
1740 | 1849 | } |
---|
1741 | 1850 | |
---|
1742 | 1851 | static bool validate_add_rxsc(struct nlattr **attrs) |
---|
.. | .. |
---|
1759 | 1868 | struct nlattr **attrs = info->attrs; |
---|
1760 | 1869 | struct macsec_rx_sc *rx_sc; |
---|
1761 | 1870 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
---|
| 1871 | + struct macsec_secy *secy; |
---|
| 1872 | + bool active = true; |
---|
| 1873 | + int ret; |
---|
1762 | 1874 | |
---|
1763 | 1875 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
1764 | 1876 | return -EINVAL; |
---|
.. | .. |
---|
1776 | 1888 | return PTR_ERR(dev); |
---|
1777 | 1889 | } |
---|
1778 | 1890 | |
---|
| 1891 | + secy = &macsec_priv(dev)->secy; |
---|
1779 | 1892 | sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]); |
---|
1780 | 1893 | |
---|
1781 | | - rx_sc = create_rx_sc(dev, sci); |
---|
| 1894 | + if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) |
---|
| 1895 | + active = nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]); |
---|
| 1896 | + |
---|
| 1897 | + rx_sc = create_rx_sc(dev, sci, active); |
---|
1782 | 1898 | if (IS_ERR(rx_sc)) { |
---|
1783 | 1899 | rtnl_unlock(); |
---|
1784 | 1900 | return PTR_ERR(rx_sc); |
---|
1785 | 1901 | } |
---|
1786 | 1902 | |
---|
1787 | | - if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) |
---|
1788 | | - rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]); |
---|
| 1903 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 1904 | + const struct macsec_ops *ops; |
---|
| 1905 | + struct macsec_context ctx; |
---|
| 1906 | + |
---|
| 1907 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 1908 | + if (!ops) { |
---|
| 1909 | + ret = -EOPNOTSUPP; |
---|
| 1910 | + goto cleanup; |
---|
| 1911 | + } |
---|
| 1912 | + |
---|
| 1913 | + ctx.rx_sc = rx_sc; |
---|
| 1914 | + ctx.secy = secy; |
---|
| 1915 | + |
---|
| 1916 | + ret = macsec_offload(ops->mdo_add_rxsc, &ctx); |
---|
| 1917 | + if (ret) |
---|
| 1918 | + goto cleanup; |
---|
| 1919 | + } |
---|
1789 | 1920 | |
---|
1790 | 1921 | rtnl_unlock(); |
---|
1791 | 1922 | |
---|
1792 | 1923 | return 0; |
---|
| 1924 | + |
---|
| 1925 | +cleanup: |
---|
| 1926 | + del_rx_sc(secy, sci); |
---|
| 1927 | + free_rx_sc(rx_sc); |
---|
| 1928 | + rtnl_unlock(); |
---|
| 1929 | + return ret; |
---|
1793 | 1930 | } |
---|
1794 | 1931 | |
---|
1795 | 1932 | static bool validate_add_txsa(struct nlattr **attrs) |
---|
.. | .. |
---|
1803 | 1940 | if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN) |
---|
1804 | 1941 | return false; |
---|
1805 | 1942 | |
---|
1806 | | - if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0) |
---|
| 1943 | + if (nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0) |
---|
1807 | 1944 | return false; |
---|
1808 | 1945 | |
---|
1809 | 1946 | if (attrs[MACSEC_SA_ATTR_ACTIVE]) { |
---|
.. | .. |
---|
1825 | 1962 | struct macsec_tx_sc *tx_sc; |
---|
1826 | 1963 | struct macsec_tx_sa *tx_sa; |
---|
1827 | 1964 | unsigned char assoc_num; |
---|
| 1965 | + int pn_len; |
---|
1828 | 1966 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
---|
| 1967 | + bool was_operational; |
---|
1829 | 1968 | int err; |
---|
1830 | 1969 | |
---|
1831 | 1970 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
.. | .. |
---|
1856 | 1995 | return -EINVAL; |
---|
1857 | 1996 | } |
---|
1858 | 1997 | |
---|
| 1998 | + pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN; |
---|
| 1999 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) { |
---|
| 2000 | + pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n", |
---|
| 2001 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len); |
---|
| 2002 | + rtnl_unlock(); |
---|
| 2003 | + return -EINVAL; |
---|
| 2004 | + } |
---|
| 2005 | + |
---|
| 2006 | + if (secy->xpn) { |
---|
| 2007 | + if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) { |
---|
| 2008 | + rtnl_unlock(); |
---|
| 2009 | + return -EINVAL; |
---|
| 2010 | + } |
---|
| 2011 | + |
---|
| 2012 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) { |
---|
| 2013 | + pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n", |
---|
| 2014 | + nla_len(tb_sa[MACSEC_SA_ATTR_SALT]), |
---|
| 2015 | + MACSEC_SALT_LEN); |
---|
| 2016 | + rtnl_unlock(); |
---|
| 2017 | + return -EINVAL; |
---|
| 2018 | + } |
---|
| 2019 | + } |
---|
| 2020 | + |
---|
1859 | 2021 | tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]); |
---|
1860 | 2022 | if (tx_sa) { |
---|
1861 | 2023 | rtnl_unlock(); |
---|
.. | .. |
---|
1876 | 2038 | return err; |
---|
1877 | 2039 | } |
---|
1878 | 2040 | |
---|
1879 | | - nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN); |
---|
1880 | | - |
---|
1881 | 2041 | spin_lock_bh(&tx_sa->lock); |
---|
1882 | | - tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]); |
---|
| 2042 | + tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]); |
---|
1883 | 2043 | spin_unlock_bh(&tx_sa->lock); |
---|
1884 | 2044 | |
---|
1885 | 2045 | if (tb_sa[MACSEC_SA_ATTR_ACTIVE]) |
---|
1886 | 2046 | tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]); |
---|
1887 | 2047 | |
---|
| 2048 | + was_operational = secy->operational; |
---|
1888 | 2049 | if (assoc_num == tx_sc->encoding_sa && tx_sa->active) |
---|
1889 | 2050 | secy->operational = true; |
---|
1890 | 2051 | |
---|
| 2052 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2053 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 2054 | + const struct macsec_ops *ops; |
---|
| 2055 | + struct macsec_context ctx; |
---|
| 2056 | + |
---|
| 2057 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 2058 | + if (!ops) { |
---|
| 2059 | + err = -EOPNOTSUPP; |
---|
| 2060 | + goto cleanup; |
---|
| 2061 | + } |
---|
| 2062 | + |
---|
| 2063 | + ctx.sa.assoc_num = assoc_num; |
---|
| 2064 | + ctx.sa.tx_sa = tx_sa; |
---|
| 2065 | + ctx.secy = secy; |
---|
| 2066 | + memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]), |
---|
| 2067 | + secy->key_len); |
---|
| 2068 | + |
---|
| 2069 | + err = macsec_offload(ops->mdo_add_txsa, &ctx); |
---|
| 2070 | + memzero_explicit(ctx.sa.key, secy->key_len); |
---|
| 2071 | + if (err) |
---|
| 2072 | + goto cleanup; |
---|
| 2073 | + } |
---|
| 2074 | + |
---|
| 2075 | + if (secy->xpn) { |
---|
| 2076 | + tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]); |
---|
| 2077 | + nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT], |
---|
| 2078 | + MACSEC_SALT_LEN); |
---|
| 2079 | + } |
---|
| 2080 | + |
---|
| 2081 | + nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN); |
---|
1891 | 2082 | rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa); |
---|
1892 | 2083 | |
---|
1893 | 2084 | rtnl_unlock(); |
---|
1894 | 2085 | |
---|
1895 | 2086 | return 0; |
---|
| 2087 | + |
---|
| 2088 | +cleanup: |
---|
| 2089 | + secy->operational = was_operational; |
---|
| 2090 | + macsec_txsa_put(tx_sa); |
---|
| 2091 | + rtnl_unlock(); |
---|
| 2092 | + return err; |
---|
1896 | 2093 | } |
---|
1897 | 2094 | |
---|
1898 | 2095 | static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info) |
---|
.. | .. |
---|
1905 | 2102 | u8 assoc_num; |
---|
1906 | 2103 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
---|
1907 | 2104 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
---|
| 2105 | + int ret; |
---|
1908 | 2106 | |
---|
1909 | 2107 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
1910 | 2108 | return -EINVAL; |
---|
.. | .. |
---|
1928 | 2126 | return -EBUSY; |
---|
1929 | 2127 | } |
---|
1930 | 2128 | |
---|
| 2129 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2130 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 2131 | + const struct macsec_ops *ops; |
---|
| 2132 | + struct macsec_context ctx; |
---|
| 2133 | + |
---|
| 2134 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 2135 | + if (!ops) { |
---|
| 2136 | + ret = -EOPNOTSUPP; |
---|
| 2137 | + goto cleanup; |
---|
| 2138 | + } |
---|
| 2139 | + |
---|
| 2140 | + ctx.sa.assoc_num = assoc_num; |
---|
| 2141 | + ctx.sa.rx_sa = rx_sa; |
---|
| 2142 | + ctx.secy = secy; |
---|
| 2143 | + |
---|
| 2144 | + ret = macsec_offload(ops->mdo_del_rxsa, &ctx); |
---|
| 2145 | + if (ret) |
---|
| 2146 | + goto cleanup; |
---|
| 2147 | + } |
---|
| 2148 | + |
---|
1931 | 2149 | RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL); |
---|
1932 | 2150 | clear_rx_sa(rx_sa); |
---|
1933 | 2151 | |
---|
1934 | 2152 | rtnl_unlock(); |
---|
1935 | 2153 | |
---|
1936 | 2154 | return 0; |
---|
| 2155 | + |
---|
| 2156 | +cleanup: |
---|
| 2157 | + rtnl_unlock(); |
---|
| 2158 | + return ret; |
---|
1937 | 2159 | } |
---|
1938 | 2160 | |
---|
1939 | 2161 | static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info) |
---|
.. | .. |
---|
1944 | 2166 | struct macsec_rx_sc *rx_sc; |
---|
1945 | 2167 | sci_t sci; |
---|
1946 | 2168 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
---|
| 2169 | + int ret; |
---|
1947 | 2170 | |
---|
1948 | 2171 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
1949 | 2172 | return -EINVAL; |
---|
.. | .. |
---|
1970 | 2193 | return -ENODEV; |
---|
1971 | 2194 | } |
---|
1972 | 2195 | |
---|
| 2196 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2197 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 2198 | + const struct macsec_ops *ops; |
---|
| 2199 | + struct macsec_context ctx; |
---|
| 2200 | + |
---|
| 2201 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 2202 | + if (!ops) { |
---|
| 2203 | + ret = -EOPNOTSUPP; |
---|
| 2204 | + goto cleanup; |
---|
| 2205 | + } |
---|
| 2206 | + |
---|
| 2207 | + ctx.rx_sc = rx_sc; |
---|
| 2208 | + ctx.secy = secy; |
---|
| 2209 | + ret = macsec_offload(ops->mdo_del_rxsc, &ctx); |
---|
| 2210 | + if (ret) |
---|
| 2211 | + goto cleanup; |
---|
| 2212 | + } |
---|
| 2213 | + |
---|
1973 | 2214 | free_rx_sc(rx_sc); |
---|
1974 | 2215 | rtnl_unlock(); |
---|
1975 | 2216 | |
---|
1976 | 2217 | return 0; |
---|
| 2218 | + |
---|
| 2219 | +cleanup: |
---|
| 2220 | + rtnl_unlock(); |
---|
| 2221 | + return ret; |
---|
1977 | 2222 | } |
---|
1978 | 2223 | |
---|
1979 | 2224 | static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info) |
---|
.. | .. |
---|
1985 | 2230 | struct macsec_tx_sa *tx_sa; |
---|
1986 | 2231 | u8 assoc_num; |
---|
1987 | 2232 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
---|
| 2233 | + int ret; |
---|
1988 | 2234 | |
---|
1989 | 2235 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
1990 | 2236 | return -EINVAL; |
---|
.. | .. |
---|
2005 | 2251 | return -EBUSY; |
---|
2006 | 2252 | } |
---|
2007 | 2253 | |
---|
| 2254 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2255 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 2256 | + const struct macsec_ops *ops; |
---|
| 2257 | + struct macsec_context ctx; |
---|
| 2258 | + |
---|
| 2259 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 2260 | + if (!ops) { |
---|
| 2261 | + ret = -EOPNOTSUPP; |
---|
| 2262 | + goto cleanup; |
---|
| 2263 | + } |
---|
| 2264 | + |
---|
| 2265 | + ctx.sa.assoc_num = assoc_num; |
---|
| 2266 | + ctx.sa.tx_sa = tx_sa; |
---|
| 2267 | + ctx.secy = secy; |
---|
| 2268 | + |
---|
| 2269 | + ret = macsec_offload(ops->mdo_del_txsa, &ctx); |
---|
| 2270 | + if (ret) |
---|
| 2271 | + goto cleanup; |
---|
| 2272 | + } |
---|
| 2273 | + |
---|
2008 | 2274 | RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL); |
---|
2009 | 2275 | clear_tx_sa(tx_sa); |
---|
2010 | 2276 | |
---|
2011 | 2277 | rtnl_unlock(); |
---|
2012 | 2278 | |
---|
2013 | 2279 | return 0; |
---|
| 2280 | + |
---|
| 2281 | +cleanup: |
---|
| 2282 | + rtnl_unlock(); |
---|
| 2283 | + return ret; |
---|
2014 | 2284 | } |
---|
2015 | 2285 | |
---|
2016 | 2286 | static bool validate_upd_sa(struct nlattr **attrs) |
---|
2017 | 2287 | { |
---|
2018 | 2288 | if (!attrs[MACSEC_SA_ATTR_AN] || |
---|
2019 | 2289 | attrs[MACSEC_SA_ATTR_KEY] || |
---|
2020 | | - attrs[MACSEC_SA_ATTR_KEYID]) |
---|
| 2290 | + attrs[MACSEC_SA_ATTR_KEYID] || |
---|
| 2291 | + attrs[MACSEC_SA_ATTR_SSCI] || |
---|
| 2292 | + attrs[MACSEC_SA_ATTR_SALT]) |
---|
2021 | 2293 | return false; |
---|
2022 | 2294 | |
---|
2023 | 2295 | if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN) |
---|
2024 | 2296 | return false; |
---|
2025 | 2297 | |
---|
2026 | | - if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0) |
---|
| 2298 | + if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0) |
---|
2027 | 2299 | return false; |
---|
2028 | 2300 | |
---|
2029 | 2301 | if (attrs[MACSEC_SA_ATTR_ACTIVE]) { |
---|
.. | .. |
---|
2043 | 2315 | struct macsec_tx_sa *tx_sa; |
---|
2044 | 2316 | u8 assoc_num; |
---|
2045 | 2317 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
---|
| 2318 | + bool was_operational, was_active; |
---|
| 2319 | + pn_t prev_pn; |
---|
| 2320 | + int ret = 0; |
---|
| 2321 | + |
---|
| 2322 | + prev_pn.full64 = 0; |
---|
2046 | 2323 | |
---|
2047 | 2324 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
2048 | 2325 | return -EINVAL; |
---|
.. | .. |
---|
2062 | 2339 | } |
---|
2063 | 2340 | |
---|
2064 | 2341 | if (tb_sa[MACSEC_SA_ATTR_PN]) { |
---|
| 2342 | + int pn_len; |
---|
| 2343 | + |
---|
| 2344 | + pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN; |
---|
| 2345 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) { |
---|
| 2346 | + pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n", |
---|
| 2347 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len); |
---|
| 2348 | + rtnl_unlock(); |
---|
| 2349 | + return -EINVAL; |
---|
| 2350 | + } |
---|
| 2351 | + |
---|
2065 | 2352 | spin_lock_bh(&tx_sa->lock); |
---|
2066 | | - tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]); |
---|
| 2353 | + prev_pn = tx_sa->next_pn_halves; |
---|
| 2354 | + tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]); |
---|
2067 | 2355 | spin_unlock_bh(&tx_sa->lock); |
---|
2068 | 2356 | } |
---|
2069 | 2357 | |
---|
| 2358 | + was_active = tx_sa->active; |
---|
2070 | 2359 | if (tb_sa[MACSEC_SA_ATTR_ACTIVE]) |
---|
2071 | 2360 | tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]); |
---|
2072 | 2361 | |
---|
| 2362 | + was_operational = secy->operational; |
---|
2073 | 2363 | if (assoc_num == tx_sc->encoding_sa) |
---|
2074 | 2364 | secy->operational = tx_sa->active; |
---|
| 2365 | + |
---|
| 2366 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2367 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 2368 | + const struct macsec_ops *ops; |
---|
| 2369 | + struct macsec_context ctx; |
---|
| 2370 | + |
---|
| 2371 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 2372 | + if (!ops) { |
---|
| 2373 | + ret = -EOPNOTSUPP; |
---|
| 2374 | + goto cleanup; |
---|
| 2375 | + } |
---|
| 2376 | + |
---|
| 2377 | + ctx.sa.assoc_num = assoc_num; |
---|
| 2378 | + ctx.sa.tx_sa = tx_sa; |
---|
| 2379 | + ctx.secy = secy; |
---|
| 2380 | + |
---|
| 2381 | + ret = macsec_offload(ops->mdo_upd_txsa, &ctx); |
---|
| 2382 | + if (ret) |
---|
| 2383 | + goto cleanup; |
---|
| 2384 | + } |
---|
2075 | 2385 | |
---|
2076 | 2386 | rtnl_unlock(); |
---|
2077 | 2387 | |
---|
2078 | 2388 | return 0; |
---|
| 2389 | + |
---|
| 2390 | +cleanup: |
---|
| 2391 | + if (tb_sa[MACSEC_SA_ATTR_PN]) { |
---|
| 2392 | + spin_lock_bh(&tx_sa->lock); |
---|
| 2393 | + tx_sa->next_pn_halves = prev_pn; |
---|
| 2394 | + spin_unlock_bh(&tx_sa->lock); |
---|
| 2395 | + } |
---|
| 2396 | + tx_sa->active = was_active; |
---|
| 2397 | + secy->operational = was_operational; |
---|
| 2398 | + rtnl_unlock(); |
---|
| 2399 | + return ret; |
---|
2079 | 2400 | } |
---|
2080 | 2401 | |
---|
2081 | 2402 | static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info) |
---|
.. | .. |
---|
2088 | 2409 | u8 assoc_num; |
---|
2089 | 2410 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
---|
2090 | 2411 | struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1]; |
---|
| 2412 | + bool was_active; |
---|
| 2413 | + pn_t prev_pn; |
---|
| 2414 | + int ret = 0; |
---|
| 2415 | + |
---|
| 2416 | + prev_pn.full64 = 0; |
---|
2091 | 2417 | |
---|
2092 | 2418 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
2093 | 2419 | return -EINVAL; |
---|
.. | .. |
---|
2110 | 2436 | } |
---|
2111 | 2437 | |
---|
2112 | 2438 | if (tb_sa[MACSEC_SA_ATTR_PN]) { |
---|
| 2439 | + int pn_len; |
---|
| 2440 | + |
---|
| 2441 | + pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN; |
---|
| 2442 | + if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) { |
---|
| 2443 | + pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n", |
---|
| 2444 | + nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len); |
---|
| 2445 | + rtnl_unlock(); |
---|
| 2446 | + return -EINVAL; |
---|
| 2447 | + } |
---|
| 2448 | + |
---|
2113 | 2449 | spin_lock_bh(&rx_sa->lock); |
---|
2114 | | - rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]); |
---|
| 2450 | + prev_pn = rx_sa->next_pn_halves; |
---|
| 2451 | + rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]); |
---|
2115 | 2452 | spin_unlock_bh(&rx_sa->lock); |
---|
2116 | 2453 | } |
---|
2117 | 2454 | |
---|
| 2455 | + was_active = rx_sa->active; |
---|
2118 | 2456 | if (tb_sa[MACSEC_SA_ATTR_ACTIVE]) |
---|
2119 | 2457 | rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]); |
---|
2120 | 2458 | |
---|
| 2459 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2460 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 2461 | + const struct macsec_ops *ops; |
---|
| 2462 | + struct macsec_context ctx; |
---|
| 2463 | + |
---|
| 2464 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 2465 | + if (!ops) { |
---|
| 2466 | + ret = -EOPNOTSUPP; |
---|
| 2467 | + goto cleanup; |
---|
| 2468 | + } |
---|
| 2469 | + |
---|
| 2470 | + ctx.sa.assoc_num = assoc_num; |
---|
| 2471 | + ctx.sa.rx_sa = rx_sa; |
---|
| 2472 | + ctx.secy = secy; |
---|
| 2473 | + |
---|
| 2474 | + ret = macsec_offload(ops->mdo_upd_rxsa, &ctx); |
---|
| 2475 | + if (ret) |
---|
| 2476 | + goto cleanup; |
---|
| 2477 | + } |
---|
| 2478 | + |
---|
2121 | 2479 | rtnl_unlock(); |
---|
2122 | 2480 | return 0; |
---|
| 2481 | + |
---|
| 2482 | +cleanup: |
---|
| 2483 | + if (tb_sa[MACSEC_SA_ATTR_PN]) { |
---|
| 2484 | + spin_lock_bh(&rx_sa->lock); |
---|
| 2485 | + rx_sa->next_pn_halves = prev_pn; |
---|
| 2486 | + spin_unlock_bh(&rx_sa->lock); |
---|
| 2487 | + } |
---|
| 2488 | + rx_sa->active = was_active; |
---|
| 2489 | + rtnl_unlock(); |
---|
| 2490 | + return ret; |
---|
2123 | 2491 | } |
---|
2124 | 2492 | |
---|
2125 | 2493 | static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info) |
---|
.. | .. |
---|
2129 | 2497 | struct macsec_secy *secy; |
---|
2130 | 2498 | struct macsec_rx_sc *rx_sc; |
---|
2131 | 2499 | struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1]; |
---|
| 2500 | + unsigned int prev_n_rx_sc; |
---|
| 2501 | + bool was_active; |
---|
| 2502 | + int ret; |
---|
2132 | 2503 | |
---|
2133 | 2504 | if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
2134 | 2505 | return -EINVAL; |
---|
.. | .. |
---|
2146 | 2517 | return PTR_ERR(rx_sc); |
---|
2147 | 2518 | } |
---|
2148 | 2519 | |
---|
| 2520 | + was_active = rx_sc->active; |
---|
| 2521 | + prev_n_rx_sc = secy->n_rx_sc; |
---|
2149 | 2522 | if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) { |
---|
2150 | 2523 | bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]); |
---|
2151 | 2524 | |
---|
.. | .. |
---|
2155 | 2528 | rx_sc->active = new; |
---|
2156 | 2529 | } |
---|
2157 | 2530 | |
---|
| 2531 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2532 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 2533 | + const struct macsec_ops *ops; |
---|
| 2534 | + struct macsec_context ctx; |
---|
| 2535 | + |
---|
| 2536 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 2537 | + if (!ops) { |
---|
| 2538 | + ret = -EOPNOTSUPP; |
---|
| 2539 | + goto cleanup; |
---|
| 2540 | + } |
---|
| 2541 | + |
---|
| 2542 | + ctx.rx_sc = rx_sc; |
---|
| 2543 | + ctx.secy = secy; |
---|
| 2544 | + |
---|
| 2545 | + ret = macsec_offload(ops->mdo_upd_rxsc, &ctx); |
---|
| 2546 | + if (ret) |
---|
| 2547 | + goto cleanup; |
---|
| 2548 | + } |
---|
| 2549 | + |
---|
2158 | 2550 | rtnl_unlock(); |
---|
2159 | 2551 | |
---|
2160 | 2552 | return 0; |
---|
| 2553 | + |
---|
| 2554 | +cleanup: |
---|
| 2555 | + secy->n_rx_sc = prev_n_rx_sc; |
---|
| 2556 | + rx_sc->active = was_active; |
---|
| 2557 | + rtnl_unlock(); |
---|
| 2558 | + return ret; |
---|
2161 | 2559 | } |
---|
2162 | 2560 | |
---|
2163 | | -static int copy_tx_sa_stats(struct sk_buff *skb, |
---|
2164 | | - struct macsec_tx_sa_stats __percpu *pstats) |
---|
| 2561 | +static bool macsec_is_configured(struct macsec_dev *macsec) |
---|
2165 | 2562 | { |
---|
2166 | | - struct macsec_tx_sa_stats sum = {0, }; |
---|
2167 | | - int cpu; |
---|
| 2563 | + struct macsec_secy *secy = &macsec->secy; |
---|
| 2564 | + struct macsec_tx_sc *tx_sc = &secy->tx_sc; |
---|
| 2565 | + int i; |
---|
2168 | 2566 | |
---|
2169 | | - for_each_possible_cpu(cpu) { |
---|
2170 | | - const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu); |
---|
| 2567 | + if (secy->rx_sc) |
---|
| 2568 | + return true; |
---|
2171 | 2569 | |
---|
2172 | | - sum.OutPktsProtected += stats->OutPktsProtected; |
---|
2173 | | - sum.OutPktsEncrypted += stats->OutPktsEncrypted; |
---|
| 2570 | + for (i = 0; i < MACSEC_NUM_AN; i++) |
---|
| 2571 | + if (tx_sc->sa[i]) |
---|
| 2572 | + return true; |
---|
| 2573 | + |
---|
| 2574 | + return false; |
---|
| 2575 | +} |
---|
| 2576 | + |
---|
| 2577 | +static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info) |
---|
| 2578 | +{ |
---|
| 2579 | + struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1]; |
---|
| 2580 | + enum macsec_offload offload, prev_offload; |
---|
| 2581 | + int (*func)(struct macsec_context *ctx); |
---|
| 2582 | + struct nlattr **attrs = info->attrs; |
---|
| 2583 | + struct net_device *dev; |
---|
| 2584 | + const struct macsec_ops *ops; |
---|
| 2585 | + struct macsec_context ctx; |
---|
| 2586 | + struct macsec_dev *macsec; |
---|
| 2587 | + int ret; |
---|
| 2588 | + |
---|
| 2589 | + if (!attrs[MACSEC_ATTR_IFINDEX]) |
---|
| 2590 | + return -EINVAL; |
---|
| 2591 | + |
---|
| 2592 | + if (!attrs[MACSEC_ATTR_OFFLOAD]) |
---|
| 2593 | + return -EINVAL; |
---|
| 2594 | + |
---|
| 2595 | + if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX, |
---|
| 2596 | + attrs[MACSEC_ATTR_OFFLOAD], |
---|
| 2597 | + macsec_genl_offload_policy, NULL)) |
---|
| 2598 | + return -EINVAL; |
---|
| 2599 | + |
---|
| 2600 | + dev = get_dev_from_nl(genl_info_net(info), attrs); |
---|
| 2601 | + if (IS_ERR(dev)) |
---|
| 2602 | + return PTR_ERR(dev); |
---|
| 2603 | + macsec = macsec_priv(dev); |
---|
| 2604 | + |
---|
| 2605 | + if (!tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]) |
---|
| 2606 | + return -EINVAL; |
---|
| 2607 | + |
---|
| 2608 | + offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]); |
---|
| 2609 | + if (macsec->offload == offload) |
---|
| 2610 | + return 0; |
---|
| 2611 | + |
---|
| 2612 | + /* Check if the offloading mode is supported by the underlying layers */ |
---|
| 2613 | + if (offload != MACSEC_OFFLOAD_OFF && |
---|
| 2614 | + !macsec_check_offload(offload, macsec)) |
---|
| 2615 | + return -EOPNOTSUPP; |
---|
| 2616 | + |
---|
| 2617 | + /* Check if the net device is busy. */ |
---|
| 2618 | + if (netif_running(dev)) |
---|
| 2619 | + return -EBUSY; |
---|
| 2620 | + |
---|
| 2621 | + rtnl_lock(); |
---|
| 2622 | + |
---|
| 2623 | + prev_offload = macsec->offload; |
---|
| 2624 | + macsec->offload = offload; |
---|
| 2625 | + |
---|
| 2626 | + /* Check if the device already has rules configured: we do not support |
---|
| 2627 | + * rules migration. |
---|
| 2628 | + */ |
---|
| 2629 | + if (macsec_is_configured(macsec)) { |
---|
| 2630 | + ret = -EBUSY; |
---|
| 2631 | + goto rollback; |
---|
2174 | 2632 | } |
---|
2175 | 2633 | |
---|
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)) |
---|
| 2634 | + ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload, |
---|
| 2635 | + macsec, &ctx); |
---|
| 2636 | + if (!ops) { |
---|
| 2637 | + ret = -EOPNOTSUPP; |
---|
| 2638 | + goto rollback; |
---|
| 2639 | + } |
---|
| 2640 | + |
---|
| 2641 | + if (prev_offload == MACSEC_OFFLOAD_OFF) |
---|
| 2642 | + func = ops->mdo_add_secy; |
---|
| 2643 | + else |
---|
| 2644 | + func = ops->mdo_del_secy; |
---|
| 2645 | + |
---|
| 2646 | + ctx.secy = &macsec->secy; |
---|
| 2647 | + ret = macsec_offload(func, &ctx); |
---|
| 2648 | + if (ret) |
---|
| 2649 | + goto rollback; |
---|
| 2650 | + |
---|
| 2651 | + rtnl_unlock(); |
---|
| 2652 | + return 0; |
---|
| 2653 | + |
---|
| 2654 | +rollback: |
---|
| 2655 | + macsec->offload = prev_offload; |
---|
| 2656 | + |
---|
| 2657 | + rtnl_unlock(); |
---|
| 2658 | + return ret; |
---|
| 2659 | +} |
---|
| 2660 | + |
---|
| 2661 | +static void get_tx_sa_stats(struct net_device *dev, int an, |
---|
| 2662 | + struct macsec_tx_sa *tx_sa, |
---|
| 2663 | + struct macsec_tx_sa_stats *sum) |
---|
| 2664 | +{ |
---|
| 2665 | + struct macsec_dev *macsec = macsec_priv(dev); |
---|
| 2666 | + int cpu; |
---|
| 2667 | + |
---|
| 2668 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2669 | + if (macsec_is_offloaded(macsec)) { |
---|
| 2670 | + const struct macsec_ops *ops; |
---|
| 2671 | + struct macsec_context ctx; |
---|
| 2672 | + |
---|
| 2673 | + ops = macsec_get_ops(macsec, &ctx); |
---|
| 2674 | + if (ops) { |
---|
| 2675 | + ctx.sa.assoc_num = an; |
---|
| 2676 | + ctx.sa.tx_sa = tx_sa; |
---|
| 2677 | + ctx.stats.tx_sa_stats = sum; |
---|
| 2678 | + ctx.secy = &macsec_priv(dev)->secy; |
---|
| 2679 | + macsec_offload(ops->mdo_get_tx_sa_stats, &ctx); |
---|
| 2680 | + } |
---|
| 2681 | + return; |
---|
| 2682 | + } |
---|
| 2683 | + |
---|
| 2684 | + for_each_possible_cpu(cpu) { |
---|
| 2685 | + const struct macsec_tx_sa_stats *stats = |
---|
| 2686 | + per_cpu_ptr(tx_sa->stats, cpu); |
---|
| 2687 | + |
---|
| 2688 | + sum->OutPktsProtected += stats->OutPktsProtected; |
---|
| 2689 | + sum->OutPktsEncrypted += stats->OutPktsEncrypted; |
---|
| 2690 | + } |
---|
| 2691 | +} |
---|
| 2692 | + |
---|
| 2693 | +static int copy_tx_sa_stats(struct sk_buff *skb, struct macsec_tx_sa_stats *sum) |
---|
| 2694 | +{ |
---|
| 2695 | + if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, |
---|
| 2696 | + sum->OutPktsProtected) || |
---|
| 2697 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, |
---|
| 2698 | + sum->OutPktsEncrypted)) |
---|
2178 | 2699 | return -EMSGSIZE; |
---|
2179 | 2700 | |
---|
2180 | 2701 | return 0; |
---|
| 2702 | +} |
---|
| 2703 | + |
---|
| 2704 | +static void get_rx_sa_stats(struct net_device *dev, |
---|
| 2705 | + struct macsec_rx_sc *rx_sc, int an, |
---|
| 2706 | + struct macsec_rx_sa *rx_sa, |
---|
| 2707 | + struct macsec_rx_sa_stats *sum) |
---|
| 2708 | +{ |
---|
| 2709 | + struct macsec_dev *macsec = macsec_priv(dev); |
---|
| 2710 | + int cpu; |
---|
| 2711 | + |
---|
| 2712 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2713 | + if (macsec_is_offloaded(macsec)) { |
---|
| 2714 | + const struct macsec_ops *ops; |
---|
| 2715 | + struct macsec_context ctx; |
---|
| 2716 | + |
---|
| 2717 | + ops = macsec_get_ops(macsec, &ctx); |
---|
| 2718 | + if (ops) { |
---|
| 2719 | + ctx.sa.assoc_num = an; |
---|
| 2720 | + ctx.sa.rx_sa = rx_sa; |
---|
| 2721 | + ctx.stats.rx_sa_stats = sum; |
---|
| 2722 | + ctx.secy = &macsec_priv(dev)->secy; |
---|
| 2723 | + ctx.rx_sc = rx_sc; |
---|
| 2724 | + macsec_offload(ops->mdo_get_rx_sa_stats, &ctx); |
---|
| 2725 | + } |
---|
| 2726 | + return; |
---|
| 2727 | + } |
---|
| 2728 | + |
---|
| 2729 | + for_each_possible_cpu(cpu) { |
---|
| 2730 | + const struct macsec_rx_sa_stats *stats = |
---|
| 2731 | + per_cpu_ptr(rx_sa->stats, cpu); |
---|
| 2732 | + |
---|
| 2733 | + sum->InPktsOK += stats->InPktsOK; |
---|
| 2734 | + sum->InPktsInvalid += stats->InPktsInvalid; |
---|
| 2735 | + sum->InPktsNotValid += stats->InPktsNotValid; |
---|
| 2736 | + sum->InPktsNotUsingSA += stats->InPktsNotUsingSA; |
---|
| 2737 | + sum->InPktsUnusedSA += stats->InPktsUnusedSA; |
---|
| 2738 | + } |
---|
2181 | 2739 | } |
---|
2182 | 2740 | |
---|
2183 | 2741 | static int copy_rx_sa_stats(struct sk_buff *skb, |
---|
2184 | | - struct macsec_rx_sa_stats __percpu *pstats) |
---|
| 2742 | + struct macsec_rx_sa_stats *sum) |
---|
2185 | 2743 | { |
---|
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)) |
---|
| 2744 | + if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum->InPktsOK) || |
---|
| 2745 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, |
---|
| 2746 | + sum->InPktsInvalid) || |
---|
| 2747 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, |
---|
| 2748 | + sum->InPktsNotValid) || |
---|
| 2749 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, |
---|
| 2750 | + sum->InPktsNotUsingSA) || |
---|
| 2751 | + nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, |
---|
| 2752 | + sum->InPktsUnusedSA)) |
---|
2204 | 2753 | return -EMSGSIZE; |
---|
2205 | 2754 | |
---|
2206 | 2755 | return 0; |
---|
2207 | 2756 | } |
---|
2208 | 2757 | |
---|
2209 | | -static int copy_rx_sc_stats(struct sk_buff *skb, |
---|
2210 | | - struct pcpu_rx_sc_stats __percpu *pstats) |
---|
| 2758 | +static void get_rx_sc_stats(struct net_device *dev, |
---|
| 2759 | + struct macsec_rx_sc *rx_sc, |
---|
| 2760 | + struct macsec_rx_sc_stats *sum) |
---|
2211 | 2761 | { |
---|
2212 | | - struct macsec_rx_sc_stats sum = {0, }; |
---|
| 2762 | + struct macsec_dev *macsec = macsec_priv(dev); |
---|
2213 | 2763 | int cpu; |
---|
| 2764 | + |
---|
| 2765 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2766 | + if (macsec_is_offloaded(macsec)) { |
---|
| 2767 | + const struct macsec_ops *ops; |
---|
| 2768 | + struct macsec_context ctx; |
---|
| 2769 | + |
---|
| 2770 | + ops = macsec_get_ops(macsec, &ctx); |
---|
| 2771 | + if (ops) { |
---|
| 2772 | + ctx.stats.rx_sc_stats = sum; |
---|
| 2773 | + ctx.secy = &macsec_priv(dev)->secy; |
---|
| 2774 | + ctx.rx_sc = rx_sc; |
---|
| 2775 | + macsec_offload(ops->mdo_get_rx_sc_stats, &ctx); |
---|
| 2776 | + } |
---|
| 2777 | + return; |
---|
| 2778 | + } |
---|
2214 | 2779 | |
---|
2215 | 2780 | for_each_possible_cpu(cpu) { |
---|
2216 | 2781 | const struct pcpu_rx_sc_stats *stats; |
---|
2217 | 2782 | struct macsec_rx_sc_stats tmp; |
---|
2218 | 2783 | unsigned int start; |
---|
2219 | 2784 | |
---|
2220 | | - stats = per_cpu_ptr(pstats, cpu); |
---|
| 2785 | + stats = per_cpu_ptr(rx_sc->stats, cpu); |
---|
2221 | 2786 | do { |
---|
2222 | 2787 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
---|
2223 | 2788 | memcpy(&tmp, &stats->stats, sizeof(tmp)); |
---|
2224 | 2789 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
---|
2225 | 2790 | |
---|
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; |
---|
| 2791 | + sum->InOctetsValidated += tmp.InOctetsValidated; |
---|
| 2792 | + sum->InOctetsDecrypted += tmp.InOctetsDecrypted; |
---|
| 2793 | + sum->InPktsUnchecked += tmp.InPktsUnchecked; |
---|
| 2794 | + sum->InPktsDelayed += tmp.InPktsDelayed; |
---|
| 2795 | + sum->InPktsOK += tmp.InPktsOK; |
---|
| 2796 | + sum->InPktsInvalid += tmp.InPktsInvalid; |
---|
| 2797 | + sum->InPktsLate += tmp.InPktsLate; |
---|
| 2798 | + sum->InPktsNotValid += tmp.InPktsNotValid; |
---|
| 2799 | + sum->InPktsNotUsingSA += tmp.InPktsNotUsingSA; |
---|
| 2800 | + sum->InPktsUnusedSA += tmp.InPktsUnusedSA; |
---|
2236 | 2801 | } |
---|
| 2802 | +} |
---|
2237 | 2803 | |
---|
| 2804 | +static int copy_rx_sc_stats(struct sk_buff *skb, struct macsec_rx_sc_stats *sum) |
---|
| 2805 | +{ |
---|
2238 | 2806 | if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED, |
---|
2239 | | - sum.InOctetsValidated, |
---|
| 2807 | + sum->InOctetsValidated, |
---|
2240 | 2808 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2241 | 2809 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED, |
---|
2242 | | - sum.InOctetsDecrypted, |
---|
| 2810 | + sum->InOctetsDecrypted, |
---|
2243 | 2811 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2244 | 2812 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED, |
---|
2245 | | - sum.InPktsUnchecked, |
---|
| 2813 | + sum->InPktsUnchecked, |
---|
2246 | 2814 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2247 | 2815 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED, |
---|
2248 | | - sum.InPktsDelayed, |
---|
| 2816 | + sum->InPktsDelayed, |
---|
2249 | 2817 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2250 | 2818 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK, |
---|
2251 | | - sum.InPktsOK, |
---|
| 2819 | + sum->InPktsOK, |
---|
2252 | 2820 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2253 | 2821 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID, |
---|
2254 | | - sum.InPktsInvalid, |
---|
| 2822 | + sum->InPktsInvalid, |
---|
2255 | 2823 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2256 | 2824 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE, |
---|
2257 | | - sum.InPktsLate, |
---|
| 2825 | + sum->InPktsLate, |
---|
2258 | 2826 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2259 | 2827 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID, |
---|
2260 | | - sum.InPktsNotValid, |
---|
| 2828 | + sum->InPktsNotValid, |
---|
2261 | 2829 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2262 | 2830 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA, |
---|
2263 | | - sum.InPktsNotUsingSA, |
---|
| 2831 | + sum->InPktsNotUsingSA, |
---|
2264 | 2832 | MACSEC_RXSC_STATS_ATTR_PAD) || |
---|
2265 | 2833 | nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA, |
---|
2266 | | - sum.InPktsUnusedSA, |
---|
| 2834 | + sum->InPktsUnusedSA, |
---|
2267 | 2835 | MACSEC_RXSC_STATS_ATTR_PAD)) |
---|
2268 | 2836 | return -EMSGSIZE; |
---|
2269 | 2837 | |
---|
2270 | 2838 | return 0; |
---|
2271 | 2839 | } |
---|
2272 | 2840 | |
---|
2273 | | -static int copy_tx_sc_stats(struct sk_buff *skb, |
---|
2274 | | - struct pcpu_tx_sc_stats __percpu *pstats) |
---|
| 2841 | +static void get_tx_sc_stats(struct net_device *dev, |
---|
| 2842 | + struct macsec_tx_sc_stats *sum) |
---|
2275 | 2843 | { |
---|
2276 | | - struct macsec_tx_sc_stats sum = {0, }; |
---|
| 2844 | + struct macsec_dev *macsec = macsec_priv(dev); |
---|
2277 | 2845 | int cpu; |
---|
| 2846 | + |
---|
| 2847 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2848 | + if (macsec_is_offloaded(macsec)) { |
---|
| 2849 | + const struct macsec_ops *ops; |
---|
| 2850 | + struct macsec_context ctx; |
---|
| 2851 | + |
---|
| 2852 | + ops = macsec_get_ops(macsec, &ctx); |
---|
| 2853 | + if (ops) { |
---|
| 2854 | + ctx.stats.tx_sc_stats = sum; |
---|
| 2855 | + ctx.secy = &macsec_priv(dev)->secy; |
---|
| 2856 | + macsec_offload(ops->mdo_get_tx_sc_stats, &ctx); |
---|
| 2857 | + } |
---|
| 2858 | + return; |
---|
| 2859 | + } |
---|
2278 | 2860 | |
---|
2279 | 2861 | for_each_possible_cpu(cpu) { |
---|
2280 | 2862 | const struct pcpu_tx_sc_stats *stats; |
---|
2281 | 2863 | struct macsec_tx_sc_stats tmp; |
---|
2282 | 2864 | unsigned int start; |
---|
2283 | 2865 | |
---|
2284 | | - stats = per_cpu_ptr(pstats, cpu); |
---|
| 2866 | + stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu); |
---|
2285 | 2867 | do { |
---|
2286 | 2868 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
---|
2287 | 2869 | memcpy(&tmp, &stats->stats, sizeof(tmp)); |
---|
2288 | 2870 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
---|
2289 | 2871 | |
---|
2290 | | - sum.OutPktsProtected += tmp.OutPktsProtected; |
---|
2291 | | - sum.OutPktsEncrypted += tmp.OutPktsEncrypted; |
---|
2292 | | - sum.OutOctetsProtected += tmp.OutOctetsProtected; |
---|
2293 | | - sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted; |
---|
| 2872 | + sum->OutPktsProtected += tmp.OutPktsProtected; |
---|
| 2873 | + sum->OutPktsEncrypted += tmp.OutPktsEncrypted; |
---|
| 2874 | + sum->OutOctetsProtected += tmp.OutOctetsProtected; |
---|
| 2875 | + sum->OutOctetsEncrypted += tmp.OutOctetsEncrypted; |
---|
2294 | 2876 | } |
---|
| 2877 | +} |
---|
2295 | 2878 | |
---|
| 2879 | +static int copy_tx_sc_stats(struct sk_buff *skb, struct macsec_tx_sc_stats *sum) |
---|
| 2880 | +{ |
---|
2296 | 2881 | if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED, |
---|
2297 | | - sum.OutPktsProtected, |
---|
| 2882 | + sum->OutPktsProtected, |
---|
2298 | 2883 | MACSEC_TXSC_STATS_ATTR_PAD) || |
---|
2299 | 2884 | nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED, |
---|
2300 | | - sum.OutPktsEncrypted, |
---|
| 2885 | + sum->OutPktsEncrypted, |
---|
2301 | 2886 | MACSEC_TXSC_STATS_ATTR_PAD) || |
---|
2302 | 2887 | nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED, |
---|
2303 | | - sum.OutOctetsProtected, |
---|
| 2888 | + sum->OutOctetsProtected, |
---|
2304 | 2889 | MACSEC_TXSC_STATS_ATTR_PAD) || |
---|
2305 | 2890 | nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED, |
---|
2306 | | - sum.OutOctetsEncrypted, |
---|
| 2891 | + sum->OutOctetsEncrypted, |
---|
2307 | 2892 | MACSEC_TXSC_STATS_ATTR_PAD)) |
---|
2308 | 2893 | return -EMSGSIZE; |
---|
2309 | 2894 | |
---|
2310 | 2895 | return 0; |
---|
2311 | 2896 | } |
---|
2312 | 2897 | |
---|
2313 | | -static int copy_secy_stats(struct sk_buff *skb, |
---|
2314 | | - struct pcpu_secy_stats __percpu *pstats) |
---|
| 2898 | +static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum) |
---|
2315 | 2899 | { |
---|
2316 | | - struct macsec_dev_stats sum = {0, }; |
---|
| 2900 | + struct macsec_dev *macsec = macsec_priv(dev); |
---|
2317 | 2901 | int cpu; |
---|
| 2902 | + |
---|
| 2903 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 2904 | + if (macsec_is_offloaded(macsec)) { |
---|
| 2905 | + const struct macsec_ops *ops; |
---|
| 2906 | + struct macsec_context ctx; |
---|
| 2907 | + |
---|
| 2908 | + ops = macsec_get_ops(macsec, &ctx); |
---|
| 2909 | + if (ops) { |
---|
| 2910 | + ctx.stats.dev_stats = sum; |
---|
| 2911 | + ctx.secy = &macsec_priv(dev)->secy; |
---|
| 2912 | + macsec_offload(ops->mdo_get_dev_stats, &ctx); |
---|
| 2913 | + } |
---|
| 2914 | + return; |
---|
| 2915 | + } |
---|
2318 | 2916 | |
---|
2319 | 2917 | for_each_possible_cpu(cpu) { |
---|
2320 | 2918 | const struct pcpu_secy_stats *stats; |
---|
2321 | 2919 | struct macsec_dev_stats tmp; |
---|
2322 | 2920 | unsigned int start; |
---|
2323 | 2921 | |
---|
2324 | | - stats = per_cpu_ptr(pstats, cpu); |
---|
| 2922 | + stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu); |
---|
2325 | 2923 | do { |
---|
2326 | 2924 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
---|
2327 | 2925 | memcpy(&tmp, &stats->stats, sizeof(tmp)); |
---|
2328 | 2926 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
---|
2329 | 2927 | |
---|
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; |
---|
| 2928 | + sum->OutPktsUntagged += tmp.OutPktsUntagged; |
---|
| 2929 | + sum->InPktsUntagged += tmp.InPktsUntagged; |
---|
| 2930 | + sum->OutPktsTooLong += tmp.OutPktsTooLong; |
---|
| 2931 | + sum->InPktsNoTag += tmp.InPktsNoTag; |
---|
| 2932 | + sum->InPktsBadTag += tmp.InPktsBadTag; |
---|
| 2933 | + sum->InPktsUnknownSCI += tmp.InPktsUnknownSCI; |
---|
| 2934 | + sum->InPktsNoSCI += tmp.InPktsNoSCI; |
---|
| 2935 | + sum->InPktsOverrun += tmp.InPktsOverrun; |
---|
2338 | 2936 | } |
---|
| 2937 | +} |
---|
2339 | 2938 | |
---|
| 2939 | +static int copy_secy_stats(struct sk_buff *skb, struct macsec_dev_stats *sum) |
---|
| 2940 | +{ |
---|
2340 | 2941 | if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED, |
---|
2341 | | - sum.OutPktsUntagged, |
---|
| 2942 | + sum->OutPktsUntagged, |
---|
2342 | 2943 | MACSEC_SECY_STATS_ATTR_PAD) || |
---|
2343 | 2944 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED, |
---|
2344 | | - sum.InPktsUntagged, |
---|
| 2945 | + sum->InPktsUntagged, |
---|
2345 | 2946 | MACSEC_SECY_STATS_ATTR_PAD) || |
---|
2346 | 2947 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG, |
---|
2347 | | - sum.OutPktsTooLong, |
---|
| 2948 | + sum->OutPktsTooLong, |
---|
2348 | 2949 | MACSEC_SECY_STATS_ATTR_PAD) || |
---|
2349 | 2950 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG, |
---|
2350 | | - sum.InPktsNoTag, |
---|
| 2951 | + sum->InPktsNoTag, |
---|
2351 | 2952 | MACSEC_SECY_STATS_ATTR_PAD) || |
---|
2352 | 2953 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG, |
---|
2353 | | - sum.InPktsBadTag, |
---|
| 2954 | + sum->InPktsBadTag, |
---|
2354 | 2955 | MACSEC_SECY_STATS_ATTR_PAD) || |
---|
2355 | 2956 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI, |
---|
2356 | | - sum.InPktsUnknownSCI, |
---|
| 2957 | + sum->InPktsUnknownSCI, |
---|
2357 | 2958 | MACSEC_SECY_STATS_ATTR_PAD) || |
---|
2358 | 2959 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI, |
---|
2359 | | - sum.InPktsNoSCI, |
---|
| 2960 | + sum->InPktsNoSCI, |
---|
2360 | 2961 | MACSEC_SECY_STATS_ATTR_PAD) || |
---|
2361 | 2962 | nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN, |
---|
2362 | | - sum.InPktsOverrun, |
---|
| 2963 | + sum->InPktsOverrun, |
---|
2363 | 2964 | MACSEC_SECY_STATS_ATTR_PAD)) |
---|
2364 | 2965 | return -EMSGSIZE; |
---|
2365 | 2966 | |
---|
.. | .. |
---|
2369 | 2970 | static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb) |
---|
2370 | 2971 | { |
---|
2371 | 2972 | struct macsec_tx_sc *tx_sc = &secy->tx_sc; |
---|
2372 | | - struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY); |
---|
| 2973 | + struct nlattr *secy_nest = nla_nest_start_noflag(skb, |
---|
| 2974 | + MACSEC_ATTR_SECY); |
---|
2373 | 2975 | u64 csid; |
---|
2374 | 2976 | |
---|
2375 | 2977 | if (!secy_nest) |
---|
.. | .. |
---|
2377 | 2979 | |
---|
2378 | 2980 | switch (secy->key_len) { |
---|
2379 | 2981 | case MACSEC_GCM_AES_128_SAK_LEN: |
---|
2380 | | - csid = MACSEC_DEFAULT_CIPHER_ID; |
---|
| 2982 | + csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID; |
---|
2381 | 2983 | break; |
---|
2382 | 2984 | case MACSEC_GCM_AES_256_SAK_LEN: |
---|
2383 | | - csid = MACSEC_CIPHER_ID_GCM_AES_256; |
---|
| 2985 | + csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256; |
---|
2384 | 2986 | break; |
---|
2385 | 2987 | default: |
---|
2386 | 2988 | goto cancel; |
---|
.. | .. |
---|
2415 | 3017 | return 1; |
---|
2416 | 3018 | } |
---|
2417 | 3019 | |
---|
2418 | | -static int dump_secy(struct macsec_secy *secy, struct net_device *dev, |
---|
2419 | | - struct sk_buff *skb, struct netlink_callback *cb) |
---|
| 3020 | +static noinline_for_stack int |
---|
| 3021 | +dump_secy(struct macsec_secy *secy, struct net_device *dev, |
---|
| 3022 | + struct sk_buff *skb, struct netlink_callback *cb) |
---|
2420 | 3023 | { |
---|
2421 | | - struct macsec_rx_sc *rx_sc; |
---|
| 3024 | + struct macsec_tx_sc_stats tx_sc_stats = {0, }; |
---|
| 3025 | + struct macsec_tx_sa_stats tx_sa_stats = {0, }; |
---|
| 3026 | + struct macsec_rx_sc_stats rx_sc_stats = {0, }; |
---|
| 3027 | + struct macsec_rx_sa_stats rx_sa_stats = {0, }; |
---|
| 3028 | + struct macsec_dev *macsec = netdev_priv(dev); |
---|
| 3029 | + struct macsec_dev_stats dev_stats = {0, }; |
---|
2422 | 3030 | struct macsec_tx_sc *tx_sc = &secy->tx_sc; |
---|
2423 | 3031 | struct nlattr *txsa_list, *rxsc_list; |
---|
2424 | | - int i, j; |
---|
2425 | | - void *hdr; |
---|
| 3032 | + struct macsec_rx_sc *rx_sc; |
---|
2426 | 3033 | struct nlattr *attr; |
---|
| 3034 | + void *hdr; |
---|
| 3035 | + int i, j; |
---|
2427 | 3036 | |
---|
2428 | 3037 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
---|
2429 | 3038 | &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC); |
---|
.. | .. |
---|
2435 | 3044 | if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex)) |
---|
2436 | 3045 | goto nla_put_failure; |
---|
2437 | 3046 | |
---|
| 3047 | + attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD); |
---|
| 3048 | + if (!attr) |
---|
| 3049 | + goto nla_put_failure; |
---|
| 3050 | + if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload)) |
---|
| 3051 | + goto nla_put_failure; |
---|
| 3052 | + nla_nest_end(skb, attr); |
---|
| 3053 | + |
---|
2438 | 3054 | if (nla_put_secy(secy, skb)) |
---|
2439 | 3055 | goto nla_put_failure; |
---|
2440 | 3056 | |
---|
2441 | | - attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS); |
---|
| 3057 | + attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS); |
---|
2442 | 3058 | if (!attr) |
---|
2443 | 3059 | goto nla_put_failure; |
---|
2444 | | - if (copy_tx_sc_stats(skb, tx_sc->stats)) { |
---|
| 3060 | + |
---|
| 3061 | + get_tx_sc_stats(dev, &tx_sc_stats); |
---|
| 3062 | + if (copy_tx_sc_stats(skb, &tx_sc_stats)) { |
---|
2445 | 3063 | nla_nest_cancel(skb, attr); |
---|
2446 | 3064 | goto nla_put_failure; |
---|
2447 | 3065 | } |
---|
2448 | 3066 | nla_nest_end(skb, attr); |
---|
2449 | 3067 | |
---|
2450 | | - attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS); |
---|
| 3068 | + attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS); |
---|
2451 | 3069 | if (!attr) |
---|
2452 | 3070 | goto nla_put_failure; |
---|
2453 | | - if (copy_secy_stats(skb, macsec_priv(dev)->stats)) { |
---|
| 3071 | + get_secy_stats(dev, &dev_stats); |
---|
| 3072 | + if (copy_secy_stats(skb, &dev_stats)) { |
---|
2454 | 3073 | nla_nest_cancel(skb, attr); |
---|
2455 | 3074 | goto nla_put_failure; |
---|
2456 | 3075 | } |
---|
2457 | 3076 | nla_nest_end(skb, attr); |
---|
2458 | 3077 | |
---|
2459 | | - txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST); |
---|
| 3078 | + txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST); |
---|
2460 | 3079 | if (!txsa_list) |
---|
2461 | 3080 | goto nla_put_failure; |
---|
2462 | 3081 | for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) { |
---|
2463 | 3082 | struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]); |
---|
2464 | 3083 | struct nlattr *txsa_nest; |
---|
| 3084 | + u64 pn; |
---|
| 3085 | + int pn_len; |
---|
2465 | 3086 | |
---|
2466 | 3087 | if (!tx_sa) |
---|
2467 | 3088 | continue; |
---|
2468 | 3089 | |
---|
2469 | | - txsa_nest = nla_nest_start(skb, j++); |
---|
| 3090 | + txsa_nest = nla_nest_start_noflag(skb, j++); |
---|
2470 | 3091 | if (!txsa_nest) { |
---|
2471 | 3092 | nla_nest_cancel(skb, txsa_list); |
---|
2472 | 3093 | goto nla_put_failure; |
---|
2473 | 3094 | } |
---|
2474 | 3095 | |
---|
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); |
---|
| 3096 | + attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS); |
---|
2485 | 3097 | if (!attr) { |
---|
2486 | 3098 | nla_nest_cancel(skb, txsa_nest); |
---|
2487 | 3099 | nla_nest_cancel(skb, txsa_list); |
---|
2488 | 3100 | goto nla_put_failure; |
---|
2489 | 3101 | } |
---|
2490 | | - if (copy_tx_sa_stats(skb, tx_sa->stats)) { |
---|
| 3102 | + memset(&tx_sa_stats, 0, sizeof(tx_sa_stats)); |
---|
| 3103 | + get_tx_sa_stats(dev, i, tx_sa, &tx_sa_stats); |
---|
| 3104 | + if (copy_tx_sa_stats(skb, &tx_sa_stats)) { |
---|
2491 | 3105 | nla_nest_cancel(skb, attr); |
---|
2492 | 3106 | nla_nest_cancel(skb, txsa_nest); |
---|
2493 | 3107 | nla_nest_cancel(skb, txsa_list); |
---|
.. | .. |
---|
2495 | 3109 | } |
---|
2496 | 3110 | nla_nest_end(skb, attr); |
---|
2497 | 3111 | |
---|
| 3112 | + if (secy->xpn) { |
---|
| 3113 | + pn = tx_sa->next_pn; |
---|
| 3114 | + pn_len = MACSEC_XPN_PN_LEN; |
---|
| 3115 | + } else { |
---|
| 3116 | + pn = tx_sa->next_pn_halves.lower; |
---|
| 3117 | + pn_len = MACSEC_DEFAULT_PN_LEN; |
---|
| 3118 | + } |
---|
| 3119 | + |
---|
| 3120 | + if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) || |
---|
| 3121 | + nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) || |
---|
| 3122 | + nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) || |
---|
| 3123 | + (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) || |
---|
| 3124 | + nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) { |
---|
| 3125 | + nla_nest_cancel(skb, txsa_nest); |
---|
| 3126 | + nla_nest_cancel(skb, txsa_list); |
---|
| 3127 | + goto nla_put_failure; |
---|
| 3128 | + } |
---|
| 3129 | + |
---|
2498 | 3130 | nla_nest_end(skb, txsa_nest); |
---|
2499 | 3131 | } |
---|
2500 | 3132 | nla_nest_end(skb, txsa_list); |
---|
2501 | 3133 | |
---|
2502 | | - rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST); |
---|
| 3134 | + rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST); |
---|
2503 | 3135 | if (!rxsc_list) |
---|
2504 | 3136 | goto nla_put_failure; |
---|
2505 | 3137 | |
---|
.. | .. |
---|
2507 | 3139 | for_each_rxsc_rtnl(secy, rx_sc) { |
---|
2508 | 3140 | int k; |
---|
2509 | 3141 | struct nlattr *rxsa_list; |
---|
2510 | | - struct nlattr *rxsc_nest = nla_nest_start(skb, j++); |
---|
| 3142 | + struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++); |
---|
2511 | 3143 | |
---|
2512 | 3144 | if (!rxsc_nest) { |
---|
2513 | 3145 | nla_nest_cancel(skb, rxsc_list); |
---|
.. | .. |
---|
2522 | 3154 | goto nla_put_failure; |
---|
2523 | 3155 | } |
---|
2524 | 3156 | |
---|
2525 | | - attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS); |
---|
| 3157 | + attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS); |
---|
2526 | 3158 | if (!attr) { |
---|
2527 | 3159 | nla_nest_cancel(skb, rxsc_nest); |
---|
2528 | 3160 | nla_nest_cancel(skb, rxsc_list); |
---|
2529 | 3161 | goto nla_put_failure; |
---|
2530 | 3162 | } |
---|
2531 | | - if (copy_rx_sc_stats(skb, rx_sc->stats)) { |
---|
| 3163 | + memset(&rx_sc_stats, 0, sizeof(rx_sc_stats)); |
---|
| 3164 | + get_rx_sc_stats(dev, rx_sc, &rx_sc_stats); |
---|
| 3165 | + if (copy_rx_sc_stats(skb, &rx_sc_stats)) { |
---|
2532 | 3166 | nla_nest_cancel(skb, attr); |
---|
2533 | 3167 | nla_nest_cancel(skb, rxsc_nest); |
---|
2534 | 3168 | nla_nest_cancel(skb, rxsc_list); |
---|
.. | .. |
---|
2536 | 3170 | } |
---|
2537 | 3171 | nla_nest_end(skb, attr); |
---|
2538 | 3172 | |
---|
2539 | | - rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST); |
---|
| 3173 | + rxsa_list = nla_nest_start_noflag(skb, |
---|
| 3174 | + MACSEC_RXSC_ATTR_SA_LIST); |
---|
2540 | 3175 | if (!rxsa_list) { |
---|
2541 | 3176 | nla_nest_cancel(skb, rxsc_nest); |
---|
2542 | 3177 | nla_nest_cancel(skb, rxsc_list); |
---|
.. | .. |
---|
2546 | 3181 | for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) { |
---|
2547 | 3182 | struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]); |
---|
2548 | 3183 | struct nlattr *rxsa_nest; |
---|
| 3184 | + u64 pn; |
---|
| 3185 | + int pn_len; |
---|
2549 | 3186 | |
---|
2550 | 3187 | if (!rx_sa) |
---|
2551 | 3188 | continue; |
---|
2552 | 3189 | |
---|
2553 | | - rxsa_nest = nla_nest_start(skb, k++); |
---|
| 3190 | + rxsa_nest = nla_nest_start_noflag(skb, k++); |
---|
2554 | 3191 | if (!rxsa_nest) { |
---|
2555 | 3192 | nla_nest_cancel(skb, rxsa_list); |
---|
2556 | 3193 | nla_nest_cancel(skb, rxsc_nest); |
---|
.. | .. |
---|
2558 | 3195 | goto nla_put_failure; |
---|
2559 | 3196 | } |
---|
2560 | 3197 | |
---|
2561 | | - attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS); |
---|
| 3198 | + attr = nla_nest_start_noflag(skb, |
---|
| 3199 | + MACSEC_SA_ATTR_STATS); |
---|
2562 | 3200 | if (!attr) { |
---|
2563 | 3201 | nla_nest_cancel(skb, rxsa_list); |
---|
2564 | 3202 | nla_nest_cancel(skb, rxsc_nest); |
---|
2565 | 3203 | nla_nest_cancel(skb, rxsc_list); |
---|
2566 | 3204 | goto nla_put_failure; |
---|
2567 | 3205 | } |
---|
2568 | | - if (copy_rx_sa_stats(skb, rx_sa->stats)) { |
---|
| 3206 | + memset(&rx_sa_stats, 0, sizeof(rx_sa_stats)); |
---|
| 3207 | + get_rx_sa_stats(dev, rx_sc, i, rx_sa, &rx_sa_stats); |
---|
| 3208 | + if (copy_rx_sa_stats(skb, &rx_sa_stats)) { |
---|
2569 | 3209 | nla_nest_cancel(skb, attr); |
---|
2570 | 3210 | nla_nest_cancel(skb, rxsa_list); |
---|
2571 | 3211 | nla_nest_cancel(skb, rxsc_nest); |
---|
.. | .. |
---|
2574 | 3214 | } |
---|
2575 | 3215 | nla_nest_end(skb, attr); |
---|
2576 | 3216 | |
---|
| 3217 | + if (secy->xpn) { |
---|
| 3218 | + pn = rx_sa->next_pn; |
---|
| 3219 | + pn_len = MACSEC_XPN_PN_LEN; |
---|
| 3220 | + } else { |
---|
| 3221 | + pn = rx_sa->next_pn_halves.lower; |
---|
| 3222 | + pn_len = MACSEC_DEFAULT_PN_LEN; |
---|
| 3223 | + } |
---|
| 3224 | + |
---|
2577 | 3225 | if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) || |
---|
2578 | | - nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) || |
---|
| 3226 | + nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) || |
---|
2579 | 3227 | nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) || |
---|
| 3228 | + (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) || |
---|
2580 | 3229 | nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) { |
---|
2581 | 3230 | nla_nest_cancel(skb, rxsa_nest); |
---|
2582 | 3231 | nla_nest_cancel(skb, rxsc_nest); |
---|
.. | .. |
---|
2638 | 3287 | return skb->len; |
---|
2639 | 3288 | } |
---|
2640 | 3289 | |
---|
2641 | | -static const struct genl_ops macsec_genl_ops[] = { |
---|
| 3290 | +static const struct genl_small_ops macsec_genl_ops[] = { |
---|
2642 | 3291 | { |
---|
2643 | 3292 | .cmd = MACSEC_CMD_GET_TXSC, |
---|
| 3293 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2644 | 3294 | .dumpit = macsec_dump_txsc, |
---|
2645 | | - .policy = macsec_genl_policy, |
---|
2646 | 3295 | }, |
---|
2647 | 3296 | { |
---|
2648 | 3297 | .cmd = MACSEC_CMD_ADD_RXSC, |
---|
| 3298 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2649 | 3299 | .doit = macsec_add_rxsc, |
---|
2650 | | - .policy = macsec_genl_policy, |
---|
2651 | 3300 | .flags = GENL_ADMIN_PERM, |
---|
2652 | 3301 | }, |
---|
2653 | 3302 | { |
---|
2654 | 3303 | .cmd = MACSEC_CMD_DEL_RXSC, |
---|
| 3304 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2655 | 3305 | .doit = macsec_del_rxsc, |
---|
2656 | | - .policy = macsec_genl_policy, |
---|
2657 | 3306 | .flags = GENL_ADMIN_PERM, |
---|
2658 | 3307 | }, |
---|
2659 | 3308 | { |
---|
2660 | 3309 | .cmd = MACSEC_CMD_UPD_RXSC, |
---|
| 3310 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2661 | 3311 | .doit = macsec_upd_rxsc, |
---|
2662 | | - .policy = macsec_genl_policy, |
---|
2663 | 3312 | .flags = GENL_ADMIN_PERM, |
---|
2664 | 3313 | }, |
---|
2665 | 3314 | { |
---|
2666 | 3315 | .cmd = MACSEC_CMD_ADD_TXSA, |
---|
| 3316 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2667 | 3317 | .doit = macsec_add_txsa, |
---|
2668 | | - .policy = macsec_genl_policy, |
---|
2669 | 3318 | .flags = GENL_ADMIN_PERM, |
---|
2670 | 3319 | }, |
---|
2671 | 3320 | { |
---|
2672 | 3321 | .cmd = MACSEC_CMD_DEL_TXSA, |
---|
| 3322 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2673 | 3323 | .doit = macsec_del_txsa, |
---|
2674 | | - .policy = macsec_genl_policy, |
---|
2675 | 3324 | .flags = GENL_ADMIN_PERM, |
---|
2676 | 3325 | }, |
---|
2677 | 3326 | { |
---|
2678 | 3327 | .cmd = MACSEC_CMD_UPD_TXSA, |
---|
| 3328 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2679 | 3329 | .doit = macsec_upd_txsa, |
---|
2680 | | - .policy = macsec_genl_policy, |
---|
2681 | 3330 | .flags = GENL_ADMIN_PERM, |
---|
2682 | 3331 | }, |
---|
2683 | 3332 | { |
---|
2684 | 3333 | .cmd = MACSEC_CMD_ADD_RXSA, |
---|
| 3334 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2685 | 3335 | .doit = macsec_add_rxsa, |
---|
2686 | | - .policy = macsec_genl_policy, |
---|
2687 | 3336 | .flags = GENL_ADMIN_PERM, |
---|
2688 | 3337 | }, |
---|
2689 | 3338 | { |
---|
2690 | 3339 | .cmd = MACSEC_CMD_DEL_RXSA, |
---|
| 3340 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2691 | 3341 | .doit = macsec_del_rxsa, |
---|
2692 | | - .policy = macsec_genl_policy, |
---|
2693 | 3342 | .flags = GENL_ADMIN_PERM, |
---|
2694 | 3343 | }, |
---|
2695 | 3344 | { |
---|
2696 | 3345 | .cmd = MACSEC_CMD_UPD_RXSA, |
---|
| 3346 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
2697 | 3347 | .doit = macsec_upd_rxsa, |
---|
2698 | | - .policy = macsec_genl_policy, |
---|
| 3348 | + .flags = GENL_ADMIN_PERM, |
---|
| 3349 | + }, |
---|
| 3350 | + { |
---|
| 3351 | + .cmd = MACSEC_CMD_UPD_OFFLOAD, |
---|
| 3352 | + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
---|
| 3353 | + .doit = macsec_upd_offload, |
---|
2699 | 3354 | .flags = GENL_ADMIN_PERM, |
---|
2700 | 3355 | }, |
---|
2701 | 3356 | }; |
---|
.. | .. |
---|
2705 | 3360 | .hdrsize = 0, |
---|
2706 | 3361 | .version = MACSEC_GENL_VERSION, |
---|
2707 | 3362 | .maxattr = MACSEC_ATTR_MAX, |
---|
| 3363 | + .policy = macsec_genl_policy, |
---|
2708 | 3364 | .netnsok = true, |
---|
2709 | 3365 | .module = THIS_MODULE, |
---|
2710 | | - .ops = macsec_genl_ops, |
---|
2711 | | - .n_ops = ARRAY_SIZE(macsec_genl_ops), |
---|
| 3366 | + .small_ops = macsec_genl_ops, |
---|
| 3367 | + .n_small_ops = ARRAY_SIZE(macsec_genl_ops), |
---|
2712 | 3368 | }; |
---|
2713 | 3369 | |
---|
2714 | 3370 | static netdev_tx_t macsec_start_xmit(struct sk_buff *skb, |
---|
.. | .. |
---|
2718 | 3374 | struct macsec_secy *secy = &macsec->secy; |
---|
2719 | 3375 | struct pcpu_secy_stats *secy_stats; |
---|
2720 | 3376 | int ret, len; |
---|
| 3377 | + |
---|
| 3378 | + if (macsec_is_offloaded(netdev_priv(dev))) { |
---|
| 3379 | + skb->dev = macsec->real_dev; |
---|
| 3380 | + return dev_queue_xmit(skb); |
---|
| 3381 | + } |
---|
2721 | 3382 | |
---|
2722 | 3383 | /* 10.5 */ |
---|
2723 | 3384 | if (!secy->protect_frames) { |
---|
.. | .. |
---|
2756 | 3417 | |
---|
2757 | 3418 | #define MACSEC_FEATURES \ |
---|
2758 | 3419 | (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST) |
---|
2759 | | -static struct lock_class_key macsec_netdev_addr_lock_key; |
---|
2760 | 3420 | |
---|
2761 | 3421 | static int macsec_dev_init(struct net_device *dev) |
---|
2762 | 3422 | { |
---|
.. | .. |
---|
2833 | 3493 | goto clear_allmulti; |
---|
2834 | 3494 | } |
---|
2835 | 3495 | |
---|
| 3496 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 3497 | + if (macsec_is_offloaded(macsec)) { |
---|
| 3498 | + const struct macsec_ops *ops; |
---|
| 3499 | + struct macsec_context ctx; |
---|
| 3500 | + |
---|
| 3501 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 3502 | + if (!ops) { |
---|
| 3503 | + err = -EOPNOTSUPP; |
---|
| 3504 | + goto clear_allmulti; |
---|
| 3505 | + } |
---|
| 3506 | + |
---|
| 3507 | + ctx.secy = &macsec->secy; |
---|
| 3508 | + err = macsec_offload(ops->mdo_dev_open, &ctx); |
---|
| 3509 | + if (err) |
---|
| 3510 | + goto clear_allmulti; |
---|
| 3511 | + } |
---|
| 3512 | + |
---|
2836 | 3513 | if (netif_carrier_ok(real_dev)) |
---|
2837 | 3514 | netif_carrier_on(dev); |
---|
2838 | 3515 | |
---|
.. | .. |
---|
2852 | 3529 | struct net_device *real_dev = macsec->real_dev; |
---|
2853 | 3530 | |
---|
2854 | 3531 | netif_carrier_off(dev); |
---|
| 3532 | + |
---|
| 3533 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 3534 | + if (macsec_is_offloaded(macsec)) { |
---|
| 3535 | + const struct macsec_ops *ops; |
---|
| 3536 | + struct macsec_context ctx; |
---|
| 3537 | + |
---|
| 3538 | + ops = macsec_get_ops(macsec, &ctx); |
---|
| 3539 | + if (ops) { |
---|
| 3540 | + ctx.secy = &macsec->secy; |
---|
| 3541 | + macsec_offload(ops->mdo_dev_stop, &ctx); |
---|
| 3542 | + } |
---|
| 3543 | + } |
---|
2855 | 3544 | |
---|
2856 | 3545 | dev_mc_unsync(real_dev, dev); |
---|
2857 | 3546 | dev_uc_unsync(real_dev, dev); |
---|
.. | .. |
---|
2890 | 3579 | dev_uc_sync(real_dev, dev); |
---|
2891 | 3580 | } |
---|
2892 | 3581 | |
---|
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 | 3582 | static int macsec_set_mac_address(struct net_device *dev, void *p) |
---|
2899 | 3583 | { |
---|
2900 | 3584 | struct macsec_dev *macsec = macsec_priv(dev); |
---|
.. | .. |
---|
2917 | 3601 | out: |
---|
2918 | 3602 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
---|
2919 | 3603 | macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES); |
---|
| 3604 | + |
---|
| 3605 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 3606 | + if (macsec_is_offloaded(macsec)) { |
---|
| 3607 | + const struct macsec_ops *ops; |
---|
| 3608 | + struct macsec_context ctx; |
---|
| 3609 | + |
---|
| 3610 | + ops = macsec_get_ops(macsec, &ctx); |
---|
| 3611 | + if (ops) { |
---|
| 3612 | + ctx.secy = &macsec->secy; |
---|
| 3613 | + macsec_offload(ops->mdo_upd_secy, &ctx); |
---|
| 3614 | + } |
---|
| 3615 | + } |
---|
| 3616 | + |
---|
2920 | 3617 | return 0; |
---|
2921 | 3618 | } |
---|
2922 | 3619 | |
---|
.. | .. |
---|
2936 | 3633 | static void macsec_get_stats64(struct net_device *dev, |
---|
2937 | 3634 | struct rtnl_link_stats64 *s) |
---|
2938 | 3635 | { |
---|
2939 | | - int cpu; |
---|
2940 | | - |
---|
2941 | 3636 | if (!dev->tstats) |
---|
2942 | 3637 | return; |
---|
2943 | 3638 | |
---|
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 | | - } |
---|
| 3639 | + dev_fetch_sw_netstats(s, dev->tstats); |
---|
2963 | 3640 | |
---|
2964 | 3641 | s->rx_dropped = dev->stats.rx_dropped; |
---|
2965 | 3642 | s->tx_dropped = dev->stats.tx_dropped; |
---|
.. | .. |
---|
2969 | 3646 | { |
---|
2970 | 3647 | return macsec_priv(dev)->real_dev->ifindex; |
---|
2971 | 3648 | } |
---|
2972 | | - |
---|
2973 | | - |
---|
2974 | | -static int macsec_get_nest_level(struct net_device *dev) |
---|
2975 | | -{ |
---|
2976 | | - return macsec_priv(dev)->nest_level; |
---|
2977 | | -} |
---|
2978 | | - |
---|
2979 | 3649 | |
---|
2980 | 3650 | static const struct net_device_ops macsec_netdev_ops = { |
---|
2981 | 3651 | .ndo_init = macsec_dev_init, |
---|
.. | .. |
---|
2990 | 3660 | .ndo_start_xmit = macsec_start_xmit, |
---|
2991 | 3661 | .ndo_get_stats64 = macsec_get_stats64, |
---|
2992 | 3662 | .ndo_get_iflink = macsec_get_iflink, |
---|
2993 | | - .ndo_get_lock_subclass = macsec_get_nest_level, |
---|
2994 | 3663 | }; |
---|
2995 | 3664 | |
---|
2996 | 3665 | static const struct device_type macsec_type = { |
---|
.. | .. |
---|
3011 | 3680 | [IFLA_MACSEC_SCB] = { .type = NLA_U8 }, |
---|
3012 | 3681 | [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 }, |
---|
3013 | 3682 | [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 }, |
---|
| 3683 | + [IFLA_MACSEC_OFFLOAD] = { .type = NLA_U8 }, |
---|
3014 | 3684 | }; |
---|
3015 | 3685 | |
---|
3016 | 3686 | static void macsec_free_netdev(struct net_device *dev) |
---|
.. | .. |
---|
3054 | 3724 | secy->operational = tx_sa && tx_sa->active; |
---|
3055 | 3725 | } |
---|
3056 | 3726 | |
---|
3057 | | - if (data[IFLA_MACSEC_WINDOW]) |
---|
3058 | | - secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]); |
---|
3059 | | - |
---|
3060 | 3727 | if (data[IFLA_MACSEC_ENCRYPT]) |
---|
3061 | 3728 | tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]); |
---|
3062 | 3729 | |
---|
.. | .. |
---|
3083 | 3750 | case MACSEC_CIPHER_ID_GCM_AES_128: |
---|
3084 | 3751 | case MACSEC_DEFAULT_CIPHER_ID: |
---|
3085 | 3752 | secy->key_len = MACSEC_GCM_AES_128_SAK_LEN; |
---|
| 3753 | + secy->xpn = false; |
---|
3086 | 3754 | break; |
---|
3087 | 3755 | case MACSEC_CIPHER_ID_GCM_AES_256: |
---|
3088 | 3756 | secy->key_len = MACSEC_GCM_AES_256_SAK_LEN; |
---|
| 3757 | + secy->xpn = false; |
---|
| 3758 | + break; |
---|
| 3759 | + case MACSEC_CIPHER_ID_GCM_AES_XPN_128: |
---|
| 3760 | + secy->key_len = MACSEC_GCM_AES_128_SAK_LEN; |
---|
| 3761 | + secy->xpn = true; |
---|
| 3762 | + break; |
---|
| 3763 | + case MACSEC_CIPHER_ID_GCM_AES_XPN_256: |
---|
| 3764 | + secy->key_len = MACSEC_GCM_AES_256_SAK_LEN; |
---|
| 3765 | + secy->xpn = true; |
---|
3089 | 3766 | break; |
---|
3090 | 3767 | default: |
---|
3091 | 3768 | return -EINVAL; |
---|
3092 | 3769 | } |
---|
| 3770 | + } |
---|
| 3771 | + |
---|
| 3772 | + if (data[IFLA_MACSEC_WINDOW]) { |
---|
| 3773 | + secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]); |
---|
| 3774 | + |
---|
| 3775 | + /* IEEE 802.1AEbw-2013 10.7.8 - maximum replay window |
---|
| 3776 | + * for XPN cipher suites */ |
---|
| 3777 | + if (secy->xpn && |
---|
| 3778 | + secy->replay_window > MACSEC_XPN_MAX_REPLAY_WINDOW) |
---|
| 3779 | + return -EINVAL; |
---|
3093 | 3780 | } |
---|
3094 | 3781 | |
---|
3095 | 3782 | return 0; |
---|
.. | .. |
---|
3099 | 3786 | struct nlattr *data[], |
---|
3100 | 3787 | struct netlink_ext_ack *extack) |
---|
3101 | 3788 | { |
---|
| 3789 | + struct macsec_dev *macsec = macsec_priv(dev); |
---|
| 3790 | + struct macsec_tx_sc tx_sc; |
---|
| 3791 | + struct macsec_secy secy; |
---|
| 3792 | + int ret; |
---|
| 3793 | + |
---|
3102 | 3794 | if (!data) |
---|
3103 | 3795 | return 0; |
---|
3104 | 3796 | |
---|
.. | .. |
---|
3108 | 3800 | data[IFLA_MACSEC_PORT]) |
---|
3109 | 3801 | return -EINVAL; |
---|
3110 | 3802 | |
---|
3111 | | - return macsec_changelink_common(dev, data); |
---|
| 3803 | + /* Keep a copy of unmodified secy and tx_sc, in case the offload |
---|
| 3804 | + * propagation fails, to revert macsec_changelink_common. |
---|
| 3805 | + */ |
---|
| 3806 | + memcpy(&secy, &macsec->secy, sizeof(secy)); |
---|
| 3807 | + memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc)); |
---|
| 3808 | + |
---|
| 3809 | + ret = macsec_changelink_common(dev, data); |
---|
| 3810 | + if (ret) |
---|
| 3811 | + goto cleanup; |
---|
| 3812 | + |
---|
| 3813 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 3814 | + if (macsec_is_offloaded(macsec)) { |
---|
| 3815 | + const struct macsec_ops *ops; |
---|
| 3816 | + struct macsec_context ctx; |
---|
| 3817 | + |
---|
| 3818 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 3819 | + if (!ops) { |
---|
| 3820 | + ret = -EOPNOTSUPP; |
---|
| 3821 | + goto cleanup; |
---|
| 3822 | + } |
---|
| 3823 | + |
---|
| 3824 | + ctx.secy = &macsec->secy; |
---|
| 3825 | + ret = macsec_offload(ops->mdo_upd_secy, &ctx); |
---|
| 3826 | + if (ret) |
---|
| 3827 | + goto cleanup; |
---|
| 3828 | + } |
---|
| 3829 | + |
---|
| 3830 | + return 0; |
---|
| 3831 | + |
---|
| 3832 | +cleanup: |
---|
| 3833 | + memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc)); |
---|
| 3834 | + memcpy(&macsec->secy, &secy, sizeof(secy)); |
---|
| 3835 | + |
---|
| 3836 | + return ret; |
---|
3112 | 3837 | } |
---|
3113 | 3838 | |
---|
3114 | 3839 | static void macsec_del_dev(struct macsec_dev *macsec) |
---|
.. | .. |
---|
3136 | 3861 | { |
---|
3137 | 3862 | struct macsec_dev *macsec = macsec_priv(dev); |
---|
3138 | 3863 | struct net_device *real_dev = macsec->real_dev; |
---|
| 3864 | + |
---|
| 3865 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 3866 | + if (macsec_is_offloaded(macsec)) { |
---|
| 3867 | + const struct macsec_ops *ops; |
---|
| 3868 | + struct macsec_context ctx; |
---|
| 3869 | + |
---|
| 3870 | + ops = macsec_get_ops(netdev_priv(dev), &ctx); |
---|
| 3871 | + if (ops) { |
---|
| 3872 | + ctx.secy = &macsec->secy; |
---|
| 3873 | + macsec_offload(ops->mdo_del_secy, &ctx); |
---|
| 3874 | + } |
---|
| 3875 | + } |
---|
3139 | 3876 | |
---|
3140 | 3877 | unregister_netdevice_queue(dev, head); |
---|
3141 | 3878 | list_del_rcu(&macsec->secys); |
---|
.. | .. |
---|
3224 | 3961 | secy->validate_frames = MACSEC_VALIDATE_DEFAULT; |
---|
3225 | 3962 | secy->protect_frames = true; |
---|
3226 | 3963 | secy->replay_protect = false; |
---|
| 3964 | + secy->xpn = DEFAULT_XPN; |
---|
3227 | 3965 | |
---|
3228 | 3966 | secy->sci = sci; |
---|
3229 | 3967 | secy->tx_sc.active = true; |
---|
.. | .. |
---|
3235 | 3973 | |
---|
3236 | 3974 | return 0; |
---|
3237 | 3975 | } |
---|
| 3976 | + |
---|
| 3977 | +static struct lock_class_key macsec_netdev_addr_lock_key; |
---|
3238 | 3978 | |
---|
3239 | 3979 | static int macsec_newlink(struct net *net, struct net_device *dev, |
---|
3240 | 3980 | struct nlattr *tb[], struct nlattr *data[], |
---|
.. | .. |
---|
3258 | 3998 | dev->priv_flags |= IFF_MACSEC; |
---|
3259 | 3999 | |
---|
3260 | 4000 | macsec->real_dev = real_dev; |
---|
| 4001 | + |
---|
| 4002 | + if (data && data[IFLA_MACSEC_OFFLOAD]) |
---|
| 4003 | + macsec->offload = nla_get_offload(data[IFLA_MACSEC_OFFLOAD]); |
---|
| 4004 | + else |
---|
| 4005 | + /* MACsec offloading is off by default */ |
---|
| 4006 | + macsec->offload = MACSEC_OFFLOAD_OFF; |
---|
| 4007 | + |
---|
| 4008 | + /* Check if the offloading mode is supported by the underlying layers */ |
---|
| 4009 | + if (macsec->offload != MACSEC_OFFLOAD_OFF && |
---|
| 4010 | + !macsec_check_offload(macsec->offload, macsec)) |
---|
| 4011 | + return -EOPNOTSUPP; |
---|
3261 | 4012 | |
---|
3262 | 4013 | /* send_sci must be set to true when transmit sci explicitly is set */ |
---|
3263 | 4014 | if ((data && data[IFLA_MACSEC_SCI]) && |
---|
.. | .. |
---|
3284 | 4035 | if (err < 0) |
---|
3285 | 4036 | return err; |
---|
3286 | 4037 | |
---|
3287 | | - macsec->nest_level = dev_get_nest_level(real_dev) + 1; |
---|
3288 | 4038 | 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)); |
---|
| 4039 | + lockdep_set_class(&dev->addr_list_lock, |
---|
| 4040 | + &macsec_netdev_addr_lock_key); |
---|
3292 | 4041 | |
---|
3293 | 4042 | err = netdev_upper_dev_link(real_dev, dev, extack); |
---|
3294 | 4043 | if (err < 0) |
---|
.. | .. |
---|
3317 | 4066 | err = macsec_changelink_common(dev, data); |
---|
3318 | 4067 | if (err) |
---|
3319 | 4068 | goto del_dev; |
---|
| 4069 | + } |
---|
| 4070 | + |
---|
| 4071 | + /* If h/w offloading is available, propagate to the device */ |
---|
| 4072 | + if (macsec_is_offloaded(macsec)) { |
---|
| 4073 | + const struct macsec_ops *ops; |
---|
| 4074 | + struct macsec_context ctx; |
---|
| 4075 | + |
---|
| 4076 | + ops = macsec_get_ops(macsec, &ctx); |
---|
| 4077 | + if (ops) { |
---|
| 4078 | + ctx.secy = &macsec->secy; |
---|
| 4079 | + err = macsec_offload(ops->mdo_add_secy, &ctx); |
---|
| 4080 | + if (err) |
---|
| 4081 | + goto del_dev; |
---|
| 4082 | + } |
---|
3320 | 4083 | } |
---|
3321 | 4084 | |
---|
3322 | 4085 | err = register_macsec_dev(real_dev, dev); |
---|
.. | .. |
---|
3371 | 4134 | switch (csid) { |
---|
3372 | 4135 | case MACSEC_CIPHER_ID_GCM_AES_128: |
---|
3373 | 4136 | case MACSEC_CIPHER_ID_GCM_AES_256: |
---|
| 4137 | + case MACSEC_CIPHER_ID_GCM_AES_XPN_128: |
---|
| 4138 | + case MACSEC_CIPHER_ID_GCM_AES_XPN_256: |
---|
3374 | 4139 | case MACSEC_DEFAULT_CIPHER_ID: |
---|
3375 | 4140 | if (icv_len < MACSEC_MIN_ICV_LEN || |
---|
3376 | 4141 | icv_len > MACSEC_STD_ICV_LEN) |
---|
.. | .. |
---|
3444 | 4209 | |
---|
3445 | 4210 | switch (secy->key_len) { |
---|
3446 | 4211 | case MACSEC_GCM_AES_128_SAK_LEN: |
---|
3447 | | - csid = MACSEC_DEFAULT_CIPHER_ID; |
---|
| 4212 | + csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID; |
---|
3448 | 4213 | break; |
---|
3449 | 4214 | case MACSEC_GCM_AES_256_SAK_LEN: |
---|
3450 | | - csid = MACSEC_CIPHER_ID_GCM_AES_256; |
---|
| 4215 | + csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256; |
---|
3451 | 4216 | break; |
---|
3452 | 4217 | default: |
---|
3453 | 4218 | goto nla_put_failure; |
---|