Commit | Line | Data |
---|---|---|
e8f69799 IL |
1 | /* Copyright (c) 2018, Mellanox Technologies All rights reserved. |
2 | * | |
3 | * This software is available to you under a choice of one of two | |
4 | * licenses. You may choose to be licensed under the terms of the GNU | |
5 | * General Public License (GPL) Version 2, available from the file | |
6 | * COPYING in the main directory of this source tree, or the | |
7 | * OpenIB.org BSD license below: | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or | |
10 | * without modification, are permitted provided that the following | |
11 | * conditions are met: | |
12 | * | |
13 | * - Redistributions of source code must retain the above | |
14 | * copyright notice, this list of conditions and the following | |
15 | * disclaimer. | |
16 | * | |
17 | * - Redistributions in binary form must reproduce the above | |
18 | * copyright notice, this list of conditions and the following | |
19 | * disclaimer in the documentation and/or other materials | |
20 | * provided with the distribution. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
25 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
26 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
27 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
28 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
29 | * SOFTWARE. | |
30 | */ | |
31 | ||
32 | #include <crypto/aead.h> | |
33 | #include <linux/highmem.h> | |
34 | #include <linux/module.h> | |
35 | #include <linux/netdevice.h> | |
36 | #include <net/dst.h> | |
37 | #include <net/inet_connection_sock.h> | |
38 | #include <net/tcp.h> | |
39 | #include <net/tls.h> | |
40 | ||
58790314 | 41 | #include "tls.h" |
8538d29c JK |
42 | #include "trace.h" |
43 | ||
e8f69799 IL |
44 | /* device_offload_lock is used to synchronize tls_dev_add |
45 | * against NETDEV_DOWN notifications. | |
46 | */ | |
47 | static DECLARE_RWSEM(device_offload_lock); | |
48 | ||
7adc91e0 | 49 | static struct workqueue_struct *destruct_wq __read_mostly; |
e8f69799 | 50 | |
e8f69799 | 51 | static LIST_HEAD(tls_device_list); |
c55dcdd4 | 52 | static LIST_HEAD(tls_device_down_list); |
e8f69799 IL |
53 | static DEFINE_SPINLOCK(tls_device_lock); |
54 | ||
55 | static void tls_device_free_ctx(struct tls_context *ctx) | |
56 | { | |
5a03bc73 | 57 | if (ctx->tx_conf == TLS_HW) { |
4799ac81 | 58 | kfree(tls_offload_ctx_tx(ctx)); |
5a03bc73 JK |
59 | kfree(ctx->tx.rec_seq); |
60 | kfree(ctx->tx.iv); | |
61 | } | |
4799ac81 BP |
62 | |
63 | if (ctx->rx_conf == TLS_HW) | |
64 | kfree(tls_offload_ctx_rx(ctx)); | |
e8f69799 | 65 | |
15a7dea7 | 66 | tls_ctx_free(NULL, ctx); |
e8f69799 IL |
67 | } |
68 | ||
7adc91e0 | 69 | static void tls_device_tx_del_task(struct work_struct *work) |
e8f69799 | 70 | { |
7adc91e0 TT |
71 | struct tls_offload_context_tx *offload_ctx = |
72 | container_of(work, struct tls_offload_context_tx, destruct_work); | |
73 | struct tls_context *ctx = offload_ctx->ctx; | |
94ce3b64 MM |
74 | struct net_device *netdev; |
75 | ||
76 | /* Safe, because this is the destroy flow, refcount is 0, so | |
77 | * tls_device_down can't store this field in parallel. | |
78 | */ | |
79 | netdev = rcu_dereference_protected(ctx->netdev, | |
80 | !refcount_read(&ctx->refcount)); | |
e8f69799 | 81 | |
7adc91e0 TT |
82 | netdev->tlsdev_ops->tls_dev_del(netdev, ctx, TLS_OFFLOAD_CTX_DIR_TX); |
83 | dev_put(netdev); | |
84 | ctx->netdev = NULL; | |
85 | tls_device_free_ctx(ctx); | |
e8f69799 IL |
86 | } |
87 | ||
88 | static void tls_device_queue_ctx_destruction(struct tls_context *ctx) | |
89 | { | |
94ce3b64 | 90 | struct net_device *netdev; |
e8f69799 | 91 | unsigned long flags; |
113671b2 | 92 | bool async_cleanup; |
e8f69799 IL |
93 | |
94 | spin_lock_irqsave(&tls_device_lock, flags); | |
113671b2 TT |
95 | if (unlikely(!refcount_dec_and_test(&ctx->refcount))) { |
96 | spin_unlock_irqrestore(&tls_device_lock, flags); | |
97 | return; | |
98 | } | |
f08d8c1b | 99 | |
7adc91e0 | 100 | list_del(&ctx->list); /* Remove from tls_device_list / tls_device_down_list */ |
94ce3b64 MM |
101 | |
102 | /* Safe, because this is the destroy flow, refcount is 0, so | |
103 | * tls_device_down can't store this field in parallel. | |
104 | */ | |
105 | netdev = rcu_dereference_protected(ctx->netdev, | |
106 | !refcount_read(&ctx->refcount)); | |
107 | ||
108 | async_cleanup = netdev && ctx->tx_conf == TLS_HW; | |
113671b2 | 109 | if (async_cleanup) { |
7adc91e0 | 110 | struct tls_offload_context_tx *offload_ctx = tls_offload_ctx_tx(ctx); |
e8f69799 | 111 | |
7adc91e0 | 112 | /* queue_work inside the spinlock |
113671b2 TT |
113 | * to make sure tls_device_down waits for that work. |
114 | */ | |
7adc91e0 | 115 | queue_work(destruct_wq, &offload_ctx->destruct_work); |
113671b2 | 116 | } |
e8f69799 | 117 | spin_unlock_irqrestore(&tls_device_lock, flags); |
113671b2 TT |
118 | |
119 | if (!async_cleanup) | |
120 | tls_device_free_ctx(ctx); | |
e8f69799 IL |
121 | } |
122 | ||
123 | /* We assume that the socket is already connected */ | |
124 | static struct net_device *get_netdev_for_sock(struct sock *sk) | |
125 | { | |
126 | struct dst_entry *dst = sk_dst_get(sk); | |
127 | struct net_device *netdev = NULL; | |
128 | ||
129 | if (likely(dst)) { | |
153cbd13 | 130 | netdev = netdev_sk_get_lowest_dev(dst->dev, sk); |
e8f69799 IL |
131 | dev_hold(netdev); |
132 | } | |
133 | ||
134 | dst_release(dst); | |
135 | ||
136 | return netdev; | |
137 | } | |
138 | ||
139 | static void destroy_record(struct tls_record_info *record) | |
140 | { | |
7ccd4519 | 141 | int i; |
e8f69799 | 142 | |
7ccd4519 | 143 | for (i = 0; i < record->num_frags; i++) |
c420c989 | 144 | __skb_frag_unref(&record->frags[i], false); |
e8f69799 IL |
145 | kfree(record); |
146 | } | |
147 | ||
d80a1b9d | 148 | static void delete_all_records(struct tls_offload_context_tx *offload_ctx) |
e8f69799 IL |
149 | { |
150 | struct tls_record_info *info, *temp; | |
151 | ||
152 | list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) { | |
153 | list_del(&info->list); | |
154 | destroy_record(info); | |
155 | } | |
156 | ||
157 | offload_ctx->retransmit_hint = NULL; | |
158 | } | |
159 | ||
160 | static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq) | |
161 | { | |
162 | struct tls_context *tls_ctx = tls_get_ctx(sk); | |
163 | struct tls_record_info *info, *temp; | |
d80a1b9d | 164 | struct tls_offload_context_tx *ctx; |
e8f69799 IL |
165 | u64 deleted_records = 0; |
166 | unsigned long flags; | |
167 | ||
168 | if (!tls_ctx) | |
169 | return; | |
170 | ||
d80a1b9d | 171 | ctx = tls_offload_ctx_tx(tls_ctx); |
e8f69799 IL |
172 | |
173 | spin_lock_irqsave(&ctx->lock, flags); | |
174 | info = ctx->retransmit_hint; | |
6e3d02b6 | 175 | if (info && !before(acked_seq, info->end_seq)) |
e8f69799 | 176 | ctx->retransmit_hint = NULL; |
e8f69799 IL |
177 | |
178 | list_for_each_entry_safe(info, temp, &ctx->records_list, list) { | |
179 | if (before(acked_seq, info->end_seq)) | |
180 | break; | |
181 | list_del(&info->list); | |
182 | ||
183 | destroy_record(info); | |
184 | deleted_records++; | |
185 | } | |
186 | ||
187 | ctx->unacked_record_sn += deleted_records; | |
188 | spin_unlock_irqrestore(&ctx->lock, flags); | |
189 | } | |
190 | ||
191 | /* At this point, there should be no references on this | |
192 | * socket and no in-flight SKBs associated with this | |
193 | * socket, so it is safe to free all the resources. | |
194 | */ | |
8d5a49e9 | 195 | void tls_device_sk_destruct(struct sock *sk) |
e8f69799 IL |
196 | { |
197 | struct tls_context *tls_ctx = tls_get_ctx(sk); | |
d80a1b9d | 198 | struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); |
e8f69799 | 199 | |
4799ac81 | 200 | tls_ctx->sk_destruct(sk); |
e8f69799 | 201 | |
4799ac81 BP |
202 | if (tls_ctx->tx_conf == TLS_HW) { |
203 | if (ctx->open_record) | |
204 | destroy_record(ctx->open_record); | |
205 | delete_all_records(ctx); | |
206 | crypto_free_aead(ctx->aead_send); | |
207 | clean_acked_data_disable(inet_csk(sk)); | |
208 | } | |
e8f69799 | 209 | |
f08d8c1b | 210 | tls_device_queue_ctx_destruction(tls_ctx); |
e8f69799 | 211 | } |
8d5a49e9 | 212 | EXPORT_SYMBOL_GPL(tls_device_sk_destruct); |
e8f69799 | 213 | |
35b71a34 JK |
214 | void tls_device_free_resources_tx(struct sock *sk) |
215 | { | |
216 | struct tls_context *tls_ctx = tls_get_ctx(sk); | |
217 | ||
218 | tls_free_partial_record(sk, tls_ctx); | |
219 | } | |
220 | ||
8538d29c JK |
221 | void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq) |
222 | { | |
223 | struct tls_context *tls_ctx = tls_get_ctx(sk); | |
224 | ||
225 | trace_tls_device_tx_resync_req(sk, got_seq, exp_seq); | |
226 | WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags)); | |
227 | } | |
228 | EXPORT_SYMBOL_GPL(tls_offload_tx_resync_request); | |
229 | ||
50180074 JK |
230 | static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx, |
231 | u32 seq) | |
232 | { | |
233 | struct net_device *netdev; | |
234 | struct sk_buff *skb; | |
b5d9a834 | 235 | int err = 0; |
50180074 JK |
236 | u8 *rcd_sn; |
237 | ||
238 | skb = tcp_write_queue_tail(sk); | |
239 | if (skb) | |
240 | TCP_SKB_CB(skb)->eor = 1; | |
241 | ||
242 | rcd_sn = tls_ctx->tx.rec_seq; | |
243 | ||
8538d29c | 244 | trace_tls_device_tx_resync_send(sk, seq, rcd_sn); |
50180074 | 245 | down_read(&device_offload_lock); |
94ce3b64 MM |
246 | netdev = rcu_dereference_protected(tls_ctx->netdev, |
247 | lockdep_is_held(&device_offload_lock)); | |
50180074 | 248 | if (netdev) |
b5d9a834 DM |
249 | err = netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, |
250 | rcd_sn, | |
251 | TLS_OFFLOAD_CTX_DIR_TX); | |
50180074 | 252 | up_read(&device_offload_lock); |
b5d9a834 DM |
253 | if (err) |
254 | return; | |
50180074 JK |
255 | |
256 | clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags); | |
257 | } | |
258 | ||
e8f69799 IL |
259 | static void tls_append_frag(struct tls_record_info *record, |
260 | struct page_frag *pfrag, | |
261 | int size) | |
262 | { | |
263 | skb_frag_t *frag; | |
264 | ||
265 | frag = &record->frags[record->num_frags - 1]; | |
d8e18a51 | 266 | if (skb_frag_page(frag) == pfrag->page && |
b54c9d5b | 267 | skb_frag_off(frag) + skb_frag_size(frag) == pfrag->offset) { |
d8e18a51 | 268 | skb_frag_size_add(frag, size); |
e8f69799 IL |
269 | } else { |
270 | ++frag; | |
d8e18a51 | 271 | __skb_frag_set_page(frag, pfrag->page); |
b54c9d5b | 272 | skb_frag_off_set(frag, pfrag->offset); |
d8e18a51 | 273 | skb_frag_size_set(frag, size); |
e8f69799 IL |
274 | ++record->num_frags; |
275 | get_page(pfrag->page); | |
276 | } | |
277 | ||
278 | pfrag->offset += size; | |
279 | record->len += size; | |
280 | } | |
281 | ||
282 | static int tls_push_record(struct sock *sk, | |
283 | struct tls_context *ctx, | |
d80a1b9d | 284 | struct tls_offload_context_tx *offload_ctx, |
e8f69799 | 285 | struct tls_record_info *record, |
e7b159a4 | 286 | int flags) |
e8f69799 | 287 | { |
4509de14 | 288 | struct tls_prot_info *prot = &ctx->prot_info; |
e8f69799 | 289 | struct tcp_sock *tp = tcp_sk(sk); |
e8f69799 IL |
290 | skb_frag_t *frag; |
291 | int i; | |
292 | ||
e8f69799 | 293 | record->end_seq = tp->write_seq + record->len; |
d4774ac0 | 294 | list_add_tail_rcu(&record->list, &offload_ctx->records_list); |
e8f69799 | 295 | offload_ctx->open_record = NULL; |
50180074 JK |
296 | |
297 | if (test_bit(TLS_TX_SYNC_SCHED, &ctx->flags)) | |
298 | tls_device_resync_tx(sk, ctx, tp->write_seq); | |
299 | ||
fb0f886f | 300 | tls_advance_record_sn(sk, prot, &ctx->tx); |
e8f69799 IL |
301 | |
302 | for (i = 0; i < record->num_frags; i++) { | |
303 | frag = &record->frags[i]; | |
304 | sg_unmark_end(&offload_ctx->sg_tx_data[i]); | |
305 | sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag), | |
b54c9d5b | 306 | skb_frag_size(frag), skb_frag_off(frag)); |
d8e18a51 | 307 | sk_mem_charge(sk, skb_frag_size(frag)); |
e8f69799 IL |
308 | get_page(skb_frag_page(frag)); |
309 | } | |
310 | sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]); | |
311 | ||
312 | /* all ready, send */ | |
313 | return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags); | |
314 | } | |
315 | ||
e7b159a4 JK |
316 | static int tls_device_record_close(struct sock *sk, |
317 | struct tls_context *ctx, | |
318 | struct tls_record_info *record, | |
319 | struct page_frag *pfrag, | |
320 | unsigned char record_type) | |
321 | { | |
322 | struct tls_prot_info *prot = &ctx->prot_info; | |
323 | int ret; | |
324 | ||
325 | /* append tag | |
326 | * device will fill in the tag, we just need to append a placeholder | |
327 | * use socket memory to improve coalescing (re-using a single buffer | |
328 | * increases frag count) | |
329 | * if we can't allocate memory now, steal some back from data | |
330 | */ | |
331 | if (likely(skb_page_frag_refill(prot->tag_size, pfrag, | |
332 | sk->sk_allocation))) { | |
333 | ret = 0; | |
334 | tls_append_frag(record, pfrag, prot->tag_size); | |
335 | } else { | |
336 | ret = prot->tag_size; | |
337 | if (record->len <= prot->overhead_size) | |
338 | return -ENOMEM; | |
339 | } | |
340 | ||
341 | /* fill prepend */ | |
342 | tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]), | |
343 | record->len - prot->overhead_size, | |
6942a284 | 344 | record_type); |
e7b159a4 JK |
345 | return ret; |
346 | } | |
347 | ||
d80a1b9d | 348 | static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx, |
e8f69799 IL |
349 | struct page_frag *pfrag, |
350 | size_t prepend_size) | |
351 | { | |
352 | struct tls_record_info *record; | |
353 | skb_frag_t *frag; | |
354 | ||
355 | record = kmalloc(sizeof(*record), GFP_KERNEL); | |
356 | if (!record) | |
357 | return -ENOMEM; | |
358 | ||
359 | frag = &record->frags[0]; | |
360 | __skb_frag_set_page(frag, pfrag->page); | |
b54c9d5b | 361 | skb_frag_off_set(frag, pfrag->offset); |
e8f69799 IL |
362 | skb_frag_size_set(frag, prepend_size); |
363 | ||
364 | get_page(pfrag->page); | |
365 | pfrag->offset += prepend_size; | |
366 | ||
367 | record->num_frags = 1; | |
368 | record->len = prepend_size; | |
369 | offload_ctx->open_record = record; | |
370 | return 0; | |
371 | } | |
372 | ||
373 | static int tls_do_allocation(struct sock *sk, | |
d80a1b9d | 374 | struct tls_offload_context_tx *offload_ctx, |
e8f69799 IL |
375 | struct page_frag *pfrag, |
376 | size_t prepend_size) | |
377 | { | |
378 | int ret; | |
379 | ||
380 | if (!offload_ctx->open_record) { | |
381 | if (unlikely(!skb_page_frag_refill(prepend_size, pfrag, | |
382 | sk->sk_allocation))) { | |
d5bee737 | 383 | READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk); |
e8f69799 IL |
384 | sk_stream_moderate_sndbuf(sk); |
385 | return -ENOMEM; | |
386 | } | |
387 | ||
388 | ret = tls_create_new_record(offload_ctx, pfrag, prepend_size); | |
389 | if (ret) | |
390 | return ret; | |
391 | ||
392 | if (pfrag->size > pfrag->offset) | |
393 | return 0; | |
394 | } | |
395 | ||
396 | if (!sk_page_frag_refill(sk, pfrag)) | |
397 | return -ENOMEM; | |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
e681cc60 JK |
402 | static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i) |
403 | { | |
404 | size_t pre_copy, nocache; | |
405 | ||
406 | pre_copy = ~((unsigned long)addr - 1) & (SMP_CACHE_BYTES - 1); | |
407 | if (pre_copy) { | |
408 | pre_copy = min(pre_copy, bytes); | |
409 | if (copy_from_iter(addr, pre_copy, i) != pre_copy) | |
410 | return -EFAULT; | |
411 | bytes -= pre_copy; | |
412 | addr += pre_copy; | |
413 | } | |
414 | ||
415 | nocache = round_down(bytes, SMP_CACHE_BYTES); | |
416 | if (copy_from_iter_nocache(addr, nocache, i) != nocache) | |
417 | return -EFAULT; | |
418 | bytes -= nocache; | |
419 | addr += nocache; | |
420 | ||
421 | if (bytes && copy_from_iter(addr, bytes, i) != bytes) | |
422 | return -EFAULT; | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
c1318b39 BP |
427 | union tls_iter_offset { |
428 | struct iov_iter *msg_iter; | |
429 | int offset; | |
430 | }; | |
431 | ||
e8f69799 | 432 | static int tls_push_data(struct sock *sk, |
c1318b39 | 433 | union tls_iter_offset iter_offset, |
e8f69799 | 434 | size_t size, int flags, |
c1318b39 BP |
435 | unsigned char record_type, |
436 | struct page *zc_page) | |
e8f69799 IL |
437 | { |
438 | struct tls_context *tls_ctx = tls_get_ctx(sk); | |
4509de14 | 439 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
d80a1b9d | 440 | struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); |
3afef8c7 | 441 | struct tls_record_info *record; |
41477662 | 442 | int tls_push_record_flags; |
e8f69799 IL |
443 | struct page_frag *pfrag; |
444 | size_t orig_size = size; | |
445 | u32 max_open_record_len; | |
ea1dd3e9 | 446 | bool more = false; |
e8f69799 | 447 | bool done = false; |
ea1dd3e9 | 448 | int copy, rc = 0; |
e8f69799 IL |
449 | long timeo; |
450 | ||
451 | if (flags & | |
452 | ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_SENDPAGE_NOTLAST)) | |
4a5cdc60 | 453 | return -EOPNOTSUPP; |
e8f69799 | 454 | |
93277b25 | 455 | if (unlikely(sk->sk_err)) |
e8f69799 IL |
456 | return -sk->sk_err; |
457 | ||
41477662 JK |
458 | flags |= MSG_SENDPAGE_DECRYPTED; |
459 | tls_push_record_flags = flags | MSG_SENDPAGE_NOTLAST; | |
460 | ||
e8f69799 | 461 | timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
94850257 BP |
462 | if (tls_is_partially_sent_record(tls_ctx)) { |
463 | rc = tls_push_partial_record(sk, tls_ctx, flags); | |
464 | if (rc < 0) | |
465 | return rc; | |
466 | } | |
e8f69799 IL |
467 | |
468 | pfrag = sk_page_frag(sk); | |
469 | ||
470 | /* TLS_HEADER_SIZE is not counted as part of the TLS record, and | |
471 | * we need to leave room for an authentication tag. | |
472 | */ | |
473 | max_open_record_len = TLS_MAX_PAYLOAD_SIZE + | |
4509de14 | 474 | prot->prepend_size; |
e8f69799 | 475 | do { |
34ef1ed1 JK |
476 | rc = tls_do_allocation(sk, ctx, pfrag, prot->prepend_size); |
477 | if (unlikely(rc)) { | |
e8f69799 IL |
478 | rc = sk_stream_wait_memory(sk, &timeo); |
479 | if (!rc) | |
480 | continue; | |
481 | ||
482 | record = ctx->open_record; | |
483 | if (!record) | |
484 | break; | |
485 | handle_error: | |
486 | if (record_type != TLS_RECORD_TYPE_DATA) { | |
487 | /* avoid sending partial | |
488 | * record with type != | |
489 | * application_data | |
490 | */ | |
491 | size = orig_size; | |
492 | destroy_record(record); | |
493 | ctx->open_record = NULL; | |
4509de14 | 494 | } else if (record->len > prot->prepend_size) { |
e8f69799 IL |
495 | goto last_record; |
496 | } | |
497 | ||
498 | break; | |
499 | } | |
500 | ||
501 | record = ctx->open_record; | |
e8f69799 | 502 | |
c1318b39 BP |
503 | copy = min_t(size_t, size, max_open_record_len - record->len); |
504 | if (copy && zc_page) { | |
505 | struct page_frag zc_pfrag; | |
506 | ||
507 | zc_pfrag.page = zc_page; | |
508 | zc_pfrag.offset = iter_offset.offset; | |
509 | zc_pfrag.size = copy; | |
510 | tls_append_frag(record, &zc_pfrag, copy); | |
e539a105 JK |
511 | |
512 | iter_offset.offset += copy; | |
c1318b39 BP |
513 | } else if (copy) { |
514 | copy = min_t(size_t, copy, pfrag->size - pfrag->offset); | |
515 | ||
a0df7194 | 516 | rc = tls_device_copy_data(page_address(pfrag->page) + |
c1318b39 BP |
517 | pfrag->offset, copy, |
518 | iter_offset.msg_iter); | |
a0df7194 MM |
519 | if (rc) |
520 | goto handle_error; | |
521 | tls_append_frag(record, pfrag, copy); | |
522 | } | |
e8f69799 IL |
523 | |
524 | size -= copy; | |
525 | if (!size) { | |
526 | last_record: | |
527 | tls_push_record_flags = flags; | |
ea1dd3e9 RM |
528 | if (flags & (MSG_SENDPAGE_NOTLAST | MSG_MORE)) { |
529 | more = true; | |
e8f69799 IL |
530 | break; |
531 | } | |
532 | ||
533 | done = true; | |
534 | } | |
535 | ||
536 | if (done || record->len >= max_open_record_len || | |
537 | (record->num_frags >= MAX_SKB_FRAGS - 1)) { | |
e7b159a4 JK |
538 | rc = tls_device_record_close(sk, tls_ctx, record, |
539 | pfrag, record_type); | |
540 | if (rc) { | |
541 | if (rc > 0) { | |
542 | size += rc; | |
543 | } else { | |
544 | size = orig_size; | |
545 | destroy_record(record); | |
546 | ctx->open_record = NULL; | |
547 | break; | |
548 | } | |
549 | } | |
550 | ||
e8f69799 IL |
551 | rc = tls_push_record(sk, |
552 | tls_ctx, | |
553 | ctx, | |
554 | record, | |
e7b159a4 | 555 | tls_push_record_flags); |
e8f69799 IL |
556 | if (rc < 0) |
557 | break; | |
558 | } | |
559 | } while (!done); | |
560 | ||
ea1dd3e9 RM |
561 | tls_ctx->pending_open_record_frags = more; |
562 | ||
e8f69799 IL |
563 | if (orig_size - size > 0) |
564 | rc = orig_size - size; | |
565 | ||
566 | return rc; | |
567 | } | |
568 | ||
569 | int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) | |
570 | { | |
571 | unsigned char record_type = TLS_RECORD_TYPE_DATA; | |
79ffe608 | 572 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
c1318b39 | 573 | union tls_iter_offset iter; |
e8f69799 IL |
574 | int rc; |
575 | ||
79ffe608 | 576 | mutex_lock(&tls_ctx->tx_lock); |
e8f69799 IL |
577 | lock_sock(sk); |
578 | ||
579 | if (unlikely(msg->msg_controllen)) { | |
58790314 | 580 | rc = tls_process_cmsg(sk, msg, &record_type); |
e8f69799 IL |
581 | if (rc) |
582 | goto out; | |
583 | } | |
584 | ||
c1318b39 BP |
585 | iter.msg_iter = &msg->msg_iter; |
586 | rc = tls_push_data(sk, iter, size, msg->msg_flags, record_type, NULL); | |
e8f69799 IL |
587 | |
588 | out: | |
589 | release_sock(sk); | |
79ffe608 | 590 | mutex_unlock(&tls_ctx->tx_lock); |
e8f69799 IL |
591 | return rc; |
592 | } | |
593 | ||
594 | int tls_device_sendpage(struct sock *sk, struct page *page, | |
595 | int offset, size_t size, int flags) | |
596 | { | |
79ffe608 | 597 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
c1318b39 BP |
598 | union tls_iter_offset iter_offset; |
599 | struct iov_iter msg_iter; | |
b06c19d9 | 600 | char *kaddr; |
e8f69799 IL |
601 | struct kvec iov; |
602 | int rc; | |
603 | ||
604 | if (flags & MSG_SENDPAGE_NOTLAST) | |
605 | flags |= MSG_MORE; | |
606 | ||
79ffe608 | 607 | mutex_lock(&tls_ctx->tx_lock); |
e8f69799 IL |
608 | lock_sock(sk); |
609 | ||
610 | if (flags & MSG_OOB) { | |
4a5cdc60 | 611 | rc = -EOPNOTSUPP; |
e8f69799 IL |
612 | goto out; |
613 | } | |
614 | ||
c1318b39 BP |
615 | if (tls_ctx->zerocopy_sendfile) { |
616 | iter_offset.offset = offset; | |
617 | rc = tls_push_data(sk, iter_offset, size, | |
618 | flags, TLS_RECORD_TYPE_DATA, page); | |
619 | goto out; | |
620 | } | |
621 | ||
b06c19d9 | 622 | kaddr = kmap(page); |
e8f69799 IL |
623 | iov.iov_base = kaddr + offset; |
624 | iov.iov_len = size; | |
de4eda9d | 625 | iov_iter_kvec(&msg_iter, ITER_SOURCE, &iov, 1, size); |
c1318b39 BP |
626 | iter_offset.msg_iter = &msg_iter; |
627 | rc = tls_push_data(sk, iter_offset, size, flags, TLS_RECORD_TYPE_DATA, | |
628 | NULL); | |
e8f69799 IL |
629 | kunmap(page); |
630 | ||
631 | out: | |
632 | release_sock(sk); | |
79ffe608 | 633 | mutex_unlock(&tls_ctx->tx_lock); |
e8f69799 IL |
634 | return rc; |
635 | } | |
636 | ||
d80a1b9d | 637 | struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context, |
e8f69799 IL |
638 | u32 seq, u64 *p_record_sn) |
639 | { | |
640 | u64 record_sn = context->hint_record_sn; | |
06f5201c | 641 | struct tls_record_info *info, *last; |
e8f69799 IL |
642 | |
643 | info = context->retransmit_hint; | |
644 | if (!info || | |
645 | before(seq, info->end_seq - info->len)) { | |
646 | /* if retransmit_hint is irrelevant start | |
72a0f6d0 | 647 | * from the beginning of the list |
e8f69799 | 648 | */ |
d4774ac0 JK |
649 | info = list_first_entry_or_null(&context->records_list, |
650 | struct tls_record_info, list); | |
651 | if (!info) | |
652 | return NULL; | |
06f5201c RM |
653 | /* send the start_marker record if seq number is before the |
654 | * tls offload start marker sequence number. This record is | |
655 | * required to handle TCP packets which are before TLS offload | |
656 | * started. | |
657 | * And if it's not start marker, look if this seq number | |
658 | * belongs to the list. | |
659 | */ | |
660 | if (likely(!tls_record_is_start_marker(info))) { | |
661 | /* we have the first record, get the last record to see | |
662 | * if this seq number belongs to the list. | |
663 | */ | |
664 | last = list_last_entry(&context->records_list, | |
665 | struct tls_record_info, list); | |
666 | ||
667 | if (!between(seq, tls_record_start_seq(info), | |
668 | last->end_seq)) | |
669 | return NULL; | |
670 | } | |
e8f69799 IL |
671 | record_sn = context->unacked_record_sn; |
672 | } | |
673 | ||
d4774ac0 JK |
674 | /* We just need the _rcu for the READ_ONCE() */ |
675 | rcu_read_lock(); | |
676 | list_for_each_entry_from_rcu(info, &context->records_list, list) { | |
e8f69799 IL |
677 | if (before(seq, info->end_seq)) { |
678 | if (!context->retransmit_hint || | |
679 | after(info->end_seq, | |
680 | context->retransmit_hint->end_seq)) { | |
681 | context->hint_record_sn = record_sn; | |
682 | context->retransmit_hint = info; | |
683 | } | |
684 | *p_record_sn = record_sn; | |
d4774ac0 | 685 | goto exit_rcu_unlock; |
e8f69799 IL |
686 | } |
687 | record_sn++; | |
688 | } | |
d4774ac0 | 689 | info = NULL; |
e8f69799 | 690 | |
d4774ac0 JK |
691 | exit_rcu_unlock: |
692 | rcu_read_unlock(); | |
693 | return info; | |
e8f69799 IL |
694 | } |
695 | EXPORT_SYMBOL(tls_get_record); | |
696 | ||
697 | static int tls_device_push_pending_record(struct sock *sk, int flags) | |
698 | { | |
c1318b39 BP |
699 | union tls_iter_offset iter; |
700 | struct iov_iter msg_iter; | |
e8f69799 | 701 | |
de4eda9d | 702 | iov_iter_kvec(&msg_iter, ITER_SOURCE, NULL, 0, 0); |
c1318b39 BP |
703 | iter.msg_iter = &msg_iter; |
704 | return tls_push_data(sk, iter, 0, flags, TLS_RECORD_TYPE_DATA, NULL); | |
e8f69799 IL |
705 | } |
706 | ||
7463d3a2 BP |
707 | void tls_device_write_space(struct sock *sk, struct tls_context *ctx) |
708 | { | |
02b1fa07 | 709 | if (tls_is_partially_sent_record(ctx)) { |
7463d3a2 BP |
710 | gfp_t sk_allocation = sk->sk_allocation; |
711 | ||
02b1fa07 JK |
712 | WARN_ON_ONCE(sk->sk_write_pending); |
713 | ||
7463d3a2 | 714 | sk->sk_allocation = GFP_ATOMIC; |
41477662 JK |
715 | tls_push_partial_record(sk, ctx, |
716 | MSG_DONTWAIT | MSG_NOSIGNAL | | |
717 | MSG_SENDPAGE_DECRYPTED); | |
7463d3a2 BP |
718 | sk->sk_allocation = sk_allocation; |
719 | } | |
7463d3a2 BP |
720 | } |
721 | ||
e52972c1 | 722 | static void tls_device_resync_rx(struct tls_context *tls_ctx, |
89fec474 | 723 | struct sock *sk, u32 seq, u8 *rcd_sn) |
e52972c1 | 724 | { |
8538d29c | 725 | struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx); |
e52972c1 JK |
726 | struct net_device *netdev; |
727 | ||
8538d29c | 728 | trace_tls_device_rx_resync_send(sk, seq, rcd_sn, rx_ctx->resync_type); |
05fc8b6c | 729 | rcu_read_lock(); |
94ce3b64 | 730 | netdev = rcu_dereference(tls_ctx->netdev); |
e52972c1 | 731 | if (netdev) |
eeb2efaf JK |
732 | netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, rcd_sn, |
733 | TLS_OFFLOAD_CTX_DIR_RX); | |
05fc8b6c | 734 | rcu_read_unlock(); |
a4d26fdb | 735 | TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICERESYNC); |
e52972c1 JK |
736 | } |
737 | ||
ed9b7646 BP |
738 | static bool |
739 | tls_device_rx_resync_async(struct tls_offload_resync_async *resync_async, | |
138559b9 | 740 | s64 resync_req, u32 *seq, u16 *rcd_delta) |
ed9b7646 BP |
741 | { |
742 | u32 is_async = resync_req & RESYNC_REQ_ASYNC; | |
743 | u32 req_seq = resync_req >> 32; | |
744 | u32 req_end = req_seq + ((resync_req >> 16) & 0xffff); | |
138559b9 TT |
745 | u16 i; |
746 | ||
747 | *rcd_delta = 0; | |
ed9b7646 BP |
748 | |
749 | if (is_async) { | |
138559b9 TT |
750 | /* shouldn't get to wraparound: |
751 | * too long in async stage, something bad happened | |
752 | */ | |
753 | if (WARN_ON_ONCE(resync_async->rcd_delta == USHRT_MAX)) | |
754 | return false; | |
755 | ||
ed9b7646 BP |
756 | /* asynchronous stage: log all headers seq such that |
757 | * req_seq <= seq <= end_seq, and wait for real resync request | |
758 | */ | |
138559b9 TT |
759 | if (before(*seq, req_seq)) |
760 | return false; | |
761 | if (!after(*seq, req_end) && | |
ed9b7646 BP |
762 | resync_async->loglen < TLS_DEVICE_RESYNC_ASYNC_LOGMAX) |
763 | resync_async->log[resync_async->loglen++] = *seq; | |
764 | ||
138559b9 TT |
765 | resync_async->rcd_delta++; |
766 | ||
ed9b7646 BP |
767 | return false; |
768 | } | |
769 | ||
770 | /* synchronous stage: check against the logged entries and | |
771 | * proceed to check the next entries if no match was found | |
772 | */ | |
138559b9 TT |
773 | for (i = 0; i < resync_async->loglen; i++) |
774 | if (req_seq == resync_async->log[i] && | |
775 | atomic64_try_cmpxchg(&resync_async->req, &resync_req, 0)) { | |
776 | *rcd_delta = resync_async->rcd_delta - i; | |
ed9b7646 | 777 | *seq = req_seq; |
138559b9 TT |
778 | resync_async->loglen = 0; |
779 | resync_async->rcd_delta = 0; | |
ed9b7646 BP |
780 | return true; |
781 | } | |
138559b9 TT |
782 | |
783 | resync_async->loglen = 0; | |
784 | resync_async->rcd_delta = 0; | |
ed9b7646 BP |
785 | |
786 | if (req_seq == *seq && | |
787 | atomic64_try_cmpxchg(&resync_async->req, | |
788 | &resync_req, 0)) | |
789 | return true; | |
790 | ||
791 | return false; | |
792 | } | |
793 | ||
f953d33b | 794 | void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) |
4799ac81 BP |
795 | { |
796 | struct tls_context *tls_ctx = tls_get_ctx(sk); | |
4799ac81 | 797 | struct tls_offload_context_rx *rx_ctx; |
f953d33b | 798 | u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE]; |
acb5a07a | 799 | u32 sock_data, is_req_pending; |
f953d33b | 800 | struct tls_prot_info *prot; |
4799ac81 | 801 | s64 resync_req; |
138559b9 | 802 | u16 rcd_delta; |
4799ac81 BP |
803 | u32 req_seq; |
804 | ||
805 | if (tls_ctx->rx_conf != TLS_HW) | |
806 | return; | |
c55dcdd4 MM |
807 | if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) |
808 | return; | |
4799ac81 | 809 | |
f953d33b | 810 | prot = &tls_ctx->prot_info; |
4799ac81 | 811 | rx_ctx = tls_offload_ctx_rx(tls_ctx); |
f953d33b JK |
812 | memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size); |
813 | ||
814 | switch (rx_ctx->resync_type) { | |
815 | case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ: | |
816 | resync_req = atomic64_read(&rx_ctx->resync_req); | |
817 | req_seq = resync_req >> 32; | |
818 | seq += TLS_HEADER_SIZE - 1; | |
acb5a07a | 819 | is_req_pending = resync_req; |
f953d33b | 820 | |
acb5a07a | 821 | if (likely(!is_req_pending) || req_seq != seq || |
f953d33b JK |
822 | !atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0)) |
823 | return; | |
824 | break; | |
825 | case TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT: | |
826 | if (likely(!rx_ctx->resync_nh_do_now)) | |
827 | return; | |
828 | ||
829 | /* head of next rec is already in, note that the sock_inq will | |
830 | * include the currently parsed message when called from parser | |
831 | */ | |
8538d29c JK |
832 | sock_data = tcp_inq(sk); |
833 | if (sock_data > rcd_len) { | |
834 | trace_tls_device_rx_resync_nh_delay(sk, sock_data, | |
835 | rcd_len); | |
f953d33b | 836 | return; |
8538d29c | 837 | } |
f953d33b JK |
838 | |
839 | rx_ctx->resync_nh_do_now = 0; | |
840 | seq += rcd_len; | |
841 | tls_bigint_increment(rcd_sn, prot->rec_seq_size); | |
842 | break; | |
ed9b7646 BP |
843 | case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC: |
844 | resync_req = atomic64_read(&rx_ctx->resync_async->req); | |
845 | is_req_pending = resync_req; | |
846 | if (likely(!is_req_pending)) | |
847 | return; | |
848 | ||
849 | if (!tls_device_rx_resync_async(rx_ctx->resync_async, | |
138559b9 | 850 | resync_req, &seq, &rcd_delta)) |
ed9b7646 | 851 | return; |
138559b9 | 852 | tls_bigint_subtract(rcd_sn, rcd_delta); |
ed9b7646 | 853 | break; |
f953d33b JK |
854 | } |
855 | ||
856 | tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn); | |
857 | } | |
858 | ||
859 | static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx, | |
860 | struct tls_offload_context_rx *ctx, | |
861 | struct sock *sk, struct sk_buff *skb) | |
862 | { | |
863 | struct strp_msg *rxm; | |
864 | ||
865 | /* device will request resyncs by itself based on stream scan */ | |
866 | if (ctx->resync_type != TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT) | |
867 | return; | |
868 | /* already scheduled */ | |
869 | if (ctx->resync_nh_do_now) | |
870 | return; | |
871 | /* seen decrypted fragments since last fully-failed record */ | |
872 | if (ctx->resync_nh_reset) { | |
873 | ctx->resync_nh_reset = 0; | |
874 | ctx->resync_nh.decrypted_failed = 1; | |
875 | ctx->resync_nh.decrypted_tgt = TLS_DEVICE_RESYNC_NH_START_IVAL; | |
876 | return; | |
877 | } | |
878 | ||
879 | if (++ctx->resync_nh.decrypted_failed <= ctx->resync_nh.decrypted_tgt) | |
880 | return; | |
881 | ||
882 | /* doing resync, bump the next target in case it fails */ | |
883 | if (ctx->resync_nh.decrypted_tgt < TLS_DEVICE_RESYNC_NH_MAX_IVAL) | |
884 | ctx->resync_nh.decrypted_tgt *= 2; | |
885 | else | |
886 | ctx->resync_nh.decrypted_tgt += TLS_DEVICE_RESYNC_NH_MAX_IVAL; | |
887 | ||
888 | rxm = strp_msg(skb); | |
889 | ||
890 | /* head of next rec is already in, parser will sync for us */ | |
891 | if (tcp_inq(sk) > rxm->full_len) { | |
8538d29c | 892 | trace_tls_device_rx_resync_nh_schedule(sk); |
f953d33b JK |
893 | ctx->resync_nh_do_now = 1; |
894 | } else { | |
895 | struct tls_prot_info *prot = &tls_ctx->prot_info; | |
896 | u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE]; | |
897 | ||
898 | memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size); | |
899 | tls_bigint_increment(rcd_sn, prot->rec_seq_size); | |
900 | ||
901 | tls_device_resync_rx(tls_ctx, sk, tcp_sk(sk)->copied_seq, | |
902 | rcd_sn); | |
903 | } | |
4799ac81 BP |
904 | } |
905 | ||
541cc48b | 906 | static int |
ea7a9d88 | 907 | tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx) |
4799ac81 | 908 | { |
ea7a9d88 GP |
909 | struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx); |
910 | const struct tls_cipher_size_desc *cipher_sz; | |
8b3c59a7 JK |
911 | int err, offset, copy, data_len, pos; |
912 | struct sk_buff *skb, *skb_iter; | |
4799ac81 | 913 | struct scatterlist sg[1]; |
541cc48b | 914 | struct strp_msg *rxm; |
4799ac81 BP |
915 | char *orig_buf, *buf; |
916 | ||
ea7a9d88 GP |
917 | switch (tls_ctx->crypto_recv.info.cipher_type) { |
918 | case TLS_CIPHER_AES_GCM_128: | |
56e5a6d3 | 919 | case TLS_CIPHER_AES_GCM_256: |
ea7a9d88 GP |
920 | break; |
921 | default: | |
922 | return -EINVAL; | |
923 | } | |
924 | cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_recv.info.cipher_type]; | |
925 | ||
8b3c59a7 | 926 | rxm = strp_msg(tls_strp_msg(sw_ctx)); |
ea7a9d88 GP |
927 | orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv, |
928 | sk->sk_allocation); | |
4799ac81 BP |
929 | if (!orig_buf) |
930 | return -ENOMEM; | |
931 | buf = orig_buf; | |
932 | ||
8b3c59a7 JK |
933 | err = tls_strp_msg_cow(sw_ctx); |
934 | if (unlikely(err)) | |
4799ac81 | 935 | goto free_buf; |
8b3c59a7 JK |
936 | |
937 | skb = tls_strp_msg(sw_ctx); | |
938 | rxm = strp_msg(skb); | |
939 | offset = rxm->offset; | |
4799ac81 BP |
940 | |
941 | sg_init_table(sg, 1); | |
942 | sg_set_buf(&sg[0], buf, | |
ea7a9d88 GP |
943 | rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv); |
944 | err = skb_copy_bits(skb, offset, buf, TLS_HEADER_SIZE + cipher_sz->iv); | |
aeb11ff0 JK |
945 | if (err) |
946 | goto free_buf; | |
4799ac81 BP |
947 | |
948 | /* We are interested only in the decrypted data not the auth */ | |
541cc48b | 949 | err = decrypt_skb(sk, sg); |
4799ac81 BP |
950 | if (err != -EBADMSG) |
951 | goto free_buf; | |
952 | else | |
953 | err = 0; | |
954 | ||
ea7a9d88 | 955 | data_len = rxm->full_len - cipher_sz->tag; |
4799ac81 | 956 | |
97e1caa5 | 957 | if (skb_pagelen(skb) > offset) { |
eb3d38d5 | 958 | copy = min_t(int, skb_pagelen(skb) - offset, data_len); |
4799ac81 | 959 | |
aeb11ff0 JK |
960 | if (skb->decrypted) { |
961 | err = skb_store_bits(skb, offset, buf, copy); | |
962 | if (err) | |
963 | goto free_buf; | |
964 | } | |
4799ac81 | 965 | |
97e1caa5 JK |
966 | offset += copy; |
967 | buf += copy; | |
968 | } | |
4799ac81 | 969 | |
eb3d38d5 | 970 | pos = skb_pagelen(skb); |
4799ac81 | 971 | skb_walk_frags(skb, skb_iter) { |
eb3d38d5 JK |
972 | int frag_pos; |
973 | ||
974 | /* Practically all frags must belong to msg if reencrypt | |
975 | * is needed with current strparser and coalescing logic, | |
976 | * but strparser may "get optimized", so let's be safe. | |
977 | */ | |
978 | if (pos + skb_iter->len <= offset) | |
979 | goto done_with_frag; | |
980 | if (pos >= data_len + rxm->offset) | |
981 | break; | |
982 | ||
983 | frag_pos = offset - pos; | |
984 | copy = min_t(int, skb_iter->len - frag_pos, | |
985 | data_len + rxm->offset - offset); | |
4799ac81 | 986 | |
aeb11ff0 JK |
987 | if (skb_iter->decrypted) { |
988 | err = skb_store_bits(skb_iter, frag_pos, buf, copy); | |
989 | if (err) | |
990 | goto free_buf; | |
991 | } | |
4799ac81 BP |
992 | |
993 | offset += copy; | |
994 | buf += copy; | |
eb3d38d5 JK |
995 | done_with_frag: |
996 | pos += skb_iter->len; | |
4799ac81 BP |
997 | } |
998 | ||
999 | free_buf: | |
1000 | kfree(orig_buf); | |
1001 | return err; | |
1002 | } | |
1003 | ||
541cc48b | 1004 | int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx) |
4799ac81 | 1005 | { |
4799ac81 | 1006 | struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx); |
541cc48b JK |
1007 | struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx); |
1008 | struct sk_buff *skb = tls_strp_msg(sw_ctx); | |
1009 | struct strp_msg *rxm = strp_msg(skb); | |
4799ac81 BP |
1010 | int is_decrypted = skb->decrypted; |
1011 | int is_encrypted = !is_decrypted; | |
1012 | struct sk_buff *skb_iter; | |
86b259f6 | 1013 | int left; |
4799ac81 | 1014 | |
86b259f6 | 1015 | left = rxm->full_len - skb->len; |
4799ac81 | 1016 | /* Check if all the data is decrypted already */ |
86b259f6 JK |
1017 | skb_iter = skb_shinfo(skb)->frag_list; |
1018 | while (skb_iter && left > 0) { | |
4799ac81 BP |
1019 | is_decrypted &= skb_iter->decrypted; |
1020 | is_encrypted &= !skb_iter->decrypted; | |
86b259f6 JK |
1021 | |
1022 | left -= skb_iter->len; | |
1023 | skb_iter = skb_iter->next; | |
4799ac81 BP |
1024 | } |
1025 | ||
9ec1c6ac JK |
1026 | trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len, |
1027 | tls_ctx->rx.rec_seq, rxm->full_len, | |
1028 | is_encrypted, is_decrypted); | |
1029 | ||
c55dcdd4 MM |
1030 | if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) { |
1031 | if (likely(is_encrypted || is_decrypted)) | |
71471ca3 | 1032 | return is_decrypted; |
c55dcdd4 MM |
1033 | |
1034 | /* After tls_device_down disables the offload, the next SKB will | |
1035 | * likely have initial fragments decrypted, and final ones not | |
1036 | * decrypted. We need to reencrypt that single SKB. | |
1037 | */ | |
ea7a9d88 | 1038 | return tls_device_reencrypt(sk, tls_ctx); |
c55dcdd4 MM |
1039 | } |
1040 | ||
f953d33b | 1041 | /* Return immediately if the record is either entirely plaintext or |
4799ac81 BP |
1042 | * entirely ciphertext. Otherwise handle reencrypt partially decrypted |
1043 | * record. | |
1044 | */ | |
f953d33b JK |
1045 | if (is_decrypted) { |
1046 | ctx->resync_nh_reset = 1; | |
71471ca3 | 1047 | return is_decrypted; |
f953d33b JK |
1048 | } |
1049 | if (is_encrypted) { | |
1050 | tls_device_core_ctrl_rx_resync(tls_ctx, ctx, sk, skb); | |
1051 | return 0; | |
1052 | } | |
1053 | ||
1054 | ctx->resync_nh_reset = 1; | |
ea7a9d88 | 1055 | return tls_device_reencrypt(sk, tls_ctx); |
4799ac81 BP |
1056 | } |
1057 | ||
9e995797 JK |
1058 | static void tls_device_attach(struct tls_context *ctx, struct sock *sk, |
1059 | struct net_device *netdev) | |
1060 | { | |
1061 | if (sk->sk_destruct != tls_device_sk_destruct) { | |
1062 | refcount_set(&ctx->refcount, 1); | |
1063 | dev_hold(netdev); | |
94ce3b64 | 1064 | RCU_INIT_POINTER(ctx->netdev, netdev); |
9e995797 JK |
1065 | spin_lock_irq(&tls_device_lock); |
1066 | list_add_tail(&ctx->list, &tls_device_list); | |
1067 | spin_unlock_irq(&tls_device_lock); | |
1068 | ||
1069 | ctx->sk_destruct = sk->sk_destruct; | |
8d5a49e9 | 1070 | smp_store_release(&sk->sk_destruct, tls_device_sk_destruct); |
9e995797 JK |
1071 | } |
1072 | } | |
1073 | ||
e8f69799 IL |
1074 | int tls_set_device_offload(struct sock *sk, struct tls_context *ctx) |
1075 | { | |
4509de14 VG |
1076 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
1077 | struct tls_prot_info *prot = &tls_ctx->prot_info; | |
ea7a9d88 | 1078 | const struct tls_cipher_size_desc *cipher_sz; |
e8f69799 | 1079 | struct tls_record_info *start_marker_record; |
d80a1b9d | 1080 | struct tls_offload_context_tx *offload_ctx; |
e8f69799 IL |
1081 | struct tls_crypto_info *crypto_info; |
1082 | struct net_device *netdev; | |
1083 | char *iv, *rec_seq; | |
1084 | struct sk_buff *skb; | |
e8f69799 | 1085 | __be64 rcd_sn; |
90962b48 | 1086 | int rc; |
e8f69799 IL |
1087 | |
1088 | if (!ctx) | |
90962b48 | 1089 | return -EINVAL; |
e8f69799 | 1090 | |
90962b48 JK |
1091 | if (ctx->priv_ctx_tx) |
1092 | return -EEXIST; | |
e8f69799 | 1093 | |
b1a6f56b ZX |
1094 | netdev = get_netdev_for_sock(sk); |
1095 | if (!netdev) { | |
1096 | pr_err_ratelimited("%s: netdev not found\n", __func__); | |
1097 | return -EINVAL; | |
1098 | } | |
e8f69799 | 1099 | |
b1a6f56b ZX |
1100 | if (!(netdev->features & NETIF_F_HW_TLS_TX)) { |
1101 | rc = -EOPNOTSUPP; | |
1102 | goto release_netdev; | |
e8f69799 IL |
1103 | } |
1104 | ||
86029d10 | 1105 | crypto_info = &ctx->crypto_send.info; |
618bac45 JK |
1106 | if (crypto_info->version != TLS_1_2_VERSION) { |
1107 | rc = -EOPNOTSUPP; | |
b1a6f56b | 1108 | goto release_netdev; |
618bac45 JK |
1109 | } |
1110 | ||
e8f69799 IL |
1111 | switch (crypto_info->cipher_type) { |
1112 | case TLS_CIPHER_AES_GCM_128: | |
e8f69799 | 1113 | iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; |
e8f69799 IL |
1114 | rec_seq = |
1115 | ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; | |
1116 | break; | |
56e5a6d3 GP |
1117 | case TLS_CIPHER_AES_GCM_256: |
1118 | iv = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->iv; | |
1119 | rec_seq = | |
1120 | ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->rec_seq; | |
1121 | break; | |
e8f69799 IL |
1122 | default: |
1123 | rc = -EINVAL; | |
b1a6f56b | 1124 | goto release_netdev; |
e8f69799 | 1125 | } |
ea7a9d88 | 1126 | cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type]; |
e8f69799 | 1127 | |
89fec474 | 1128 | /* Sanity-check the rec_seq_size for stack allocations */ |
ea7a9d88 | 1129 | if (cipher_sz->rec_seq > TLS_MAX_REC_SEQ_SIZE) { |
89fec474 | 1130 | rc = -EINVAL; |
b1a6f56b | 1131 | goto release_netdev; |
89fec474 JK |
1132 | } |
1133 | ||
ab232e61 JK |
1134 | prot->version = crypto_info->version; |
1135 | prot->cipher_type = crypto_info->cipher_type; | |
ea7a9d88 GP |
1136 | prot->prepend_size = TLS_HEADER_SIZE + cipher_sz->iv; |
1137 | prot->tag_size = cipher_sz->tag; | |
4509de14 | 1138 | prot->overhead_size = prot->prepend_size + prot->tag_size; |
ea7a9d88 GP |
1139 | prot->iv_size = cipher_sz->iv; |
1140 | prot->salt_size = cipher_sz->salt; | |
1141 | ctx->tx.iv = kmalloc(cipher_sz->iv + cipher_sz->salt, GFP_KERNEL); | |
e8f69799 IL |
1142 | if (!ctx->tx.iv) { |
1143 | rc = -ENOMEM; | |
b1a6f56b | 1144 | goto release_netdev; |
e8f69799 IL |
1145 | } |
1146 | ||
ea7a9d88 | 1147 | memcpy(ctx->tx.iv + cipher_sz->salt, iv, cipher_sz->iv); |
e8f69799 | 1148 | |
ea7a9d88 GP |
1149 | prot->rec_seq_size = cipher_sz->rec_seq; |
1150 | ctx->tx.rec_seq = kmemdup(rec_seq, cipher_sz->rec_seq, GFP_KERNEL); | |
e8f69799 IL |
1151 | if (!ctx->tx.rec_seq) { |
1152 | rc = -ENOMEM; | |
1153 | goto free_iv; | |
1154 | } | |
e8f69799 | 1155 | |
b1a6f56b ZX |
1156 | start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL); |
1157 | if (!start_marker_record) { | |
1158 | rc = -ENOMEM; | |
1159 | goto free_rec_seq; | |
1160 | } | |
1161 | ||
1162 | offload_ctx = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_TX, GFP_KERNEL); | |
1163 | if (!offload_ctx) { | |
1164 | rc = -ENOMEM; | |
1165 | goto free_marker_record; | |
1166 | } | |
1167 | ||
e8f69799 IL |
1168 | rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info); |
1169 | if (rc) | |
b1a6f56b | 1170 | goto free_offload_ctx; |
e8f69799 IL |
1171 | |
1172 | /* start at rec_seq - 1 to account for the start marker record */ | |
1173 | memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn)); | |
1174 | offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1; | |
1175 | ||
1176 | start_marker_record->end_seq = tcp_sk(sk)->write_seq; | |
1177 | start_marker_record->len = 0; | |
1178 | start_marker_record->num_frags = 0; | |
1179 | ||
7adc91e0 TT |
1180 | INIT_WORK(&offload_ctx->destruct_work, tls_device_tx_del_task); |
1181 | offload_ctx->ctx = ctx; | |
1182 | ||
e8f69799 IL |
1183 | INIT_LIST_HEAD(&offload_ctx->records_list); |
1184 | list_add_tail(&start_marker_record->list, &offload_ctx->records_list); | |
1185 | spin_lock_init(&offload_ctx->lock); | |
895262d8 BP |
1186 | sg_init_table(offload_ctx->sg_tx_data, |
1187 | ARRAY_SIZE(offload_ctx->sg_tx_data)); | |
e8f69799 IL |
1188 | |
1189 | clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked); | |
1190 | ctx->push_pending_record = tls_device_push_pending_record; | |
e8f69799 IL |
1191 | |
1192 | /* TLS offload is greatly simplified if we don't send | |
1193 | * SKBs where only part of the payload needs to be encrypted. | |
1194 | * So mark the last skb in the write queue as end of record. | |
1195 | */ | |
1196 | skb = tcp_write_queue_tail(sk); | |
1197 | if (skb) | |
1198 | TCP_SKB_CB(skb)->eor = 1; | |
1199 | ||
e8f69799 IL |
1200 | /* Avoid offloading if the device is down |
1201 | * We don't want to offload new flows after | |
1202 | * the NETDEV_DOWN event | |
3544c98a JK |
1203 | * |
1204 | * device_offload_lock is taken in tls_devices's NETDEV_DOWN | |
1205 | * handler thus protecting from the device going down before | |
1206 | * ctx was added to tls_device_list. | |
e8f69799 | 1207 | */ |
3544c98a | 1208 | down_read(&device_offload_lock); |
e8f69799 IL |
1209 | if (!(netdev->flags & IFF_UP)) { |
1210 | rc = -EINVAL; | |
3544c98a | 1211 | goto release_lock; |
e8f69799 IL |
1212 | } |
1213 | ||
1214 | ctx->priv_ctx_tx = offload_ctx; | |
1215 | rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX, | |
86029d10 | 1216 | &ctx->crypto_send.info, |
e8f69799 | 1217 | tcp_sk(sk)->write_seq); |
8538d29c JK |
1218 | trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_TX, |
1219 | tcp_sk(sk)->write_seq, rec_seq, rc); | |
e8f69799 | 1220 | if (rc) |
3544c98a | 1221 | goto release_lock; |
e8f69799 | 1222 | |
4799ac81 | 1223 | tls_device_attach(ctx, sk, netdev); |
3544c98a | 1224 | up_read(&device_offload_lock); |
e8f69799 | 1225 | |
e8f69799 IL |
1226 | /* following this assignment tls_is_sk_tx_device_offloaded |
1227 | * will return true and the context might be accessed | |
1228 | * by the netdev's xmit function. | |
1229 | */ | |
4799ac81 BP |
1230 | smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb); |
1231 | dev_put(netdev); | |
90962b48 JK |
1232 | |
1233 | return 0; | |
e8f69799 | 1234 | |
e8f69799 IL |
1235 | release_lock: |
1236 | up_read(&device_offload_lock); | |
1237 | clean_acked_data_disable(inet_csk(sk)); | |
1238 | crypto_free_aead(offload_ctx->aead_send); | |
e8f69799 IL |
1239 | free_offload_ctx: |
1240 | kfree(offload_ctx); | |
1241 | ctx->priv_ctx_tx = NULL; | |
1242 | free_marker_record: | |
1243 | kfree(start_marker_record); | |
b1a6f56b ZX |
1244 | free_rec_seq: |
1245 | kfree(ctx->tx.rec_seq); | |
1246 | free_iv: | |
1247 | kfree(ctx->tx.iv); | |
1248 | release_netdev: | |
1249 | dev_put(netdev); | |
e8f69799 IL |
1250 | return rc; |
1251 | } | |
1252 | ||
4799ac81 BP |
1253 | int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx) |
1254 | { | |
8538d29c | 1255 | struct tls12_crypto_info_aes_gcm_128 *info; |
4799ac81 BP |
1256 | struct tls_offload_context_rx *context; |
1257 | struct net_device *netdev; | |
1258 | int rc = 0; | |
1259 | ||
618bac45 JK |
1260 | if (ctx->crypto_recv.info.version != TLS_1_2_VERSION) |
1261 | return -EOPNOTSUPP; | |
1262 | ||
4799ac81 BP |
1263 | netdev = get_netdev_for_sock(sk); |
1264 | if (!netdev) { | |
1265 | pr_err_ratelimited("%s: netdev not found\n", __func__); | |
3544c98a | 1266 | return -EINVAL; |
4799ac81 BP |
1267 | } |
1268 | ||
1269 | if (!(netdev->features & NETIF_F_HW_TLS_RX)) { | |
4a5cdc60 | 1270 | rc = -EOPNOTSUPP; |
4799ac81 BP |
1271 | goto release_netdev; |
1272 | } | |
1273 | ||
1274 | /* Avoid offloading if the device is down | |
1275 | * We don't want to offload new flows after | |
1276 | * the NETDEV_DOWN event | |
3544c98a JK |
1277 | * |
1278 | * device_offload_lock is taken in tls_devices's NETDEV_DOWN | |
1279 | * handler thus protecting from the device going down before | |
1280 | * ctx was added to tls_device_list. | |
4799ac81 | 1281 | */ |
3544c98a | 1282 | down_read(&device_offload_lock); |
4799ac81 BP |
1283 | if (!(netdev->flags & IFF_UP)) { |
1284 | rc = -EINVAL; | |
3544c98a | 1285 | goto release_lock; |
4799ac81 BP |
1286 | } |
1287 | ||
1288 | context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL); | |
1289 | if (!context) { | |
1290 | rc = -ENOMEM; | |
3544c98a | 1291 | goto release_lock; |
4799ac81 | 1292 | } |
f953d33b | 1293 | context->resync_nh_reset = 1; |
4799ac81 BP |
1294 | |
1295 | ctx->priv_ctx_rx = context; | |
1296 | rc = tls_set_sw_offload(sk, ctx, 0); | |
1297 | if (rc) | |
1298 | goto release_ctx; | |
1299 | ||
1300 | rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX, | |
86029d10 | 1301 | &ctx->crypto_recv.info, |
4799ac81 | 1302 | tcp_sk(sk)->copied_seq); |
8538d29c JK |
1303 | info = (void *)&ctx->crypto_recv.info; |
1304 | trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_RX, | |
1305 | tcp_sk(sk)->copied_seq, info->rec_seq, rc); | |
e49d268d | 1306 | if (rc) |
4799ac81 | 1307 | goto free_sw_resources; |
4799ac81 BP |
1308 | |
1309 | tls_device_attach(ctx, sk, netdev); | |
90962b48 JK |
1310 | up_read(&device_offload_lock); |
1311 | ||
1312 | dev_put(netdev); | |
1313 | ||
1314 | return 0; | |
4799ac81 BP |
1315 | |
1316 | free_sw_resources: | |
62ef81d5 | 1317 | up_read(&device_offload_lock); |
4799ac81 | 1318 | tls_sw_free_resources_rx(sk); |
62ef81d5 | 1319 | down_read(&device_offload_lock); |
4799ac81 BP |
1320 | release_ctx: |
1321 | ctx->priv_ctx_rx = NULL; | |
4799ac81 BP |
1322 | release_lock: |
1323 | up_read(&device_offload_lock); | |
3544c98a JK |
1324 | release_netdev: |
1325 | dev_put(netdev); | |
4799ac81 BP |
1326 | return rc; |
1327 | } | |
1328 | ||
1329 | void tls_device_offload_cleanup_rx(struct sock *sk) | |
1330 | { | |
1331 | struct tls_context *tls_ctx = tls_get_ctx(sk); | |
1332 | struct net_device *netdev; | |
1333 | ||
1334 | down_read(&device_offload_lock); | |
94ce3b64 MM |
1335 | netdev = rcu_dereference_protected(tls_ctx->netdev, |
1336 | lockdep_is_held(&device_offload_lock)); | |
4799ac81 BP |
1337 | if (!netdev) |
1338 | goto out; | |
1339 | ||
4799ac81 BP |
1340 | netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx, |
1341 | TLS_OFFLOAD_CTX_DIR_RX); | |
1342 | ||
1343 | if (tls_ctx->tx_conf != TLS_HW) { | |
1344 | dev_put(netdev); | |
94ce3b64 | 1345 | rcu_assign_pointer(tls_ctx->netdev, NULL); |
025cc2fb MM |
1346 | } else { |
1347 | set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags); | |
4799ac81 BP |
1348 | } |
1349 | out: | |
1350 | up_read(&device_offload_lock); | |
4799ac81 BP |
1351 | tls_sw_release_resources_rx(sk); |
1352 | } | |
1353 | ||
e8f69799 IL |
1354 | static int tls_device_down(struct net_device *netdev) |
1355 | { | |
1356 | struct tls_context *ctx, *tmp; | |
1357 | unsigned long flags; | |
1358 | LIST_HEAD(list); | |
1359 | ||
1360 | /* Request a write lock to block new offload attempts */ | |
1361 | down_write(&device_offload_lock); | |
1362 | ||
1363 | spin_lock_irqsave(&tls_device_lock, flags); | |
1364 | list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) { | |
94ce3b64 MM |
1365 | struct net_device *ctx_netdev = |
1366 | rcu_dereference_protected(ctx->netdev, | |
1367 | lockdep_is_held(&device_offload_lock)); | |
1368 | ||
1369 | if (ctx_netdev != netdev || | |
e8f69799 IL |
1370 | !refcount_inc_not_zero(&ctx->refcount)) |
1371 | continue; | |
1372 | ||
1373 | list_move(&ctx->list, &list); | |
1374 | } | |
1375 | spin_unlock_irqrestore(&tls_device_lock, flags); | |
1376 | ||
1377 | list_for_each_entry_safe(ctx, tmp, &list, list) { | |
c55dcdd4 MM |
1378 | /* Stop offloaded TX and switch to the fallback. |
1379 | * tls_is_sk_tx_device_offloaded will return false. | |
1380 | */ | |
1381 | WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw); | |
1382 | ||
1383 | /* Stop the RX and TX resync. | |
1384 | * tls_dev_resync must not be called after tls_dev_del. | |
1385 | */ | |
94ce3b64 | 1386 | rcu_assign_pointer(ctx->netdev, NULL); |
c55dcdd4 MM |
1387 | |
1388 | /* Start skipping the RX resync logic completely. */ | |
1389 | set_bit(TLS_RX_DEV_DEGRADED, &ctx->flags); | |
1390 | ||
1391 | /* Sync with inflight packets. After this point: | |
1392 | * TX: no non-encrypted packets will be passed to the driver. | |
1393 | * RX: resync requests from the driver will be ignored. | |
1394 | */ | |
1395 | synchronize_net(); | |
1396 | ||
1397 | /* Release the offload context on the driver side. */ | |
4799ac81 BP |
1398 | if (ctx->tx_conf == TLS_HW) |
1399 | netdev->tlsdev_ops->tls_dev_del(netdev, ctx, | |
1400 | TLS_OFFLOAD_CTX_DIR_TX); | |
025cc2fb MM |
1401 | if (ctx->rx_conf == TLS_HW && |
1402 | !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags)) | |
4799ac81 BP |
1403 | netdev->tlsdev_ops->tls_dev_del(netdev, ctx, |
1404 | TLS_OFFLOAD_CTX_DIR_RX); | |
c55dcdd4 | 1405 | |
e8f69799 | 1406 | dev_put(netdev); |
e8f69799 | 1407 | |
c55dcdd4 MM |
1408 | /* Move the context to a separate list for two reasons: |
1409 | * 1. When the context is deallocated, list_del is called. | |
1410 | * 2. It's no longer an offloaded context, so we don't want to | |
1411 | * run offload-specific code on this context. | |
1412 | */ | |
1413 | spin_lock_irqsave(&tls_device_lock, flags); | |
1414 | list_move_tail(&ctx->list, &tls_device_down_list); | |
1415 | spin_unlock_irqrestore(&tls_device_lock, flags); | |
1416 | ||
1417 | /* Device contexts for RX and TX will be freed in on sk_destruct | |
1418 | * by tls_device_free_ctx. rx_conf and tx_conf stay in TLS_HW. | |
3740651b | 1419 | * Now release the ref taken above. |
c55dcdd4 | 1420 | */ |
f6336724 MM |
1421 | if (refcount_dec_and_test(&ctx->refcount)) { |
1422 | /* sk_destruct ran after tls_device_down took a ref, and | |
1423 | * it returned early. Complete the destruction here. | |
1424 | */ | |
1425 | list_del(&ctx->list); | |
3740651b | 1426 | tls_device_free_ctx(ctx); |
f6336724 | 1427 | } |
e8f69799 IL |
1428 | } |
1429 | ||
1430 | up_write(&device_offload_lock); | |
1431 | ||
7adc91e0 | 1432 | flush_workqueue(destruct_wq); |
e8f69799 IL |
1433 | |
1434 | return NOTIFY_DONE; | |
1435 | } | |
1436 | ||
1437 | static int tls_dev_event(struct notifier_block *this, unsigned long event, | |
1438 | void *ptr) | |
1439 | { | |
1440 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
1441 | ||
c3f4a6c3 JK |
1442 | if (!dev->tlsdev_ops && |
1443 | !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX))) | |
e8f69799 IL |
1444 | return NOTIFY_DONE; |
1445 | ||
1446 | switch (event) { | |
1447 | case NETDEV_REGISTER: | |
1448 | case NETDEV_FEAT_CHANGE: | |
4e5a7332 TT |
1449 | if (netif_is_bond_master(dev)) |
1450 | return NOTIFY_DONE; | |
4799ac81 | 1451 | if ((dev->features & NETIF_F_HW_TLS_RX) && |
eeb2efaf | 1452 | !dev->tlsdev_ops->tls_dev_resync) |
4799ac81 BP |
1453 | return NOTIFY_BAD; |
1454 | ||
e8f69799 IL |
1455 | if (dev->tlsdev_ops && |
1456 | dev->tlsdev_ops->tls_dev_add && | |
1457 | dev->tlsdev_ops->tls_dev_del) | |
1458 | return NOTIFY_DONE; | |
1459 | else | |
1460 | return NOTIFY_BAD; | |
1461 | case NETDEV_DOWN: | |
1462 | return tls_device_down(dev); | |
1463 | } | |
1464 | return NOTIFY_DONE; | |
1465 | } | |
1466 | ||
1467 | static struct notifier_block tls_dev_notifier = { | |
1468 | .notifier_call = tls_dev_event, | |
1469 | }; | |
1470 | ||
3d8c51b2 | 1471 | int __init tls_device_init(void) |
e8f69799 | 1472 | { |
7adc91e0 TT |
1473 | int err; |
1474 | ||
1475 | destruct_wq = alloc_workqueue("ktls_device_destruct", 0, 0); | |
1476 | if (!destruct_wq) | |
1477 | return -ENOMEM; | |
1478 | ||
1479 | err = register_netdevice_notifier(&tls_dev_notifier); | |
1480 | if (err) | |
1481 | destroy_workqueue(destruct_wq); | |
1482 | ||
1483 | return err; | |
e8f69799 IL |
1484 | } |
1485 | ||
1486 | void __exit tls_device_cleanup(void) | |
1487 | { | |
1488 | unregister_netdevice_notifier(&tls_dev_notifier); | |
7adc91e0 | 1489 | destroy_workqueue(destruct_wq); |
494bc1d2 | 1490 | clean_acked_data_flush(); |
e8f69799 | 1491 | } |