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