Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <net/tcp.h>
3#include <net/strparser.h>
4#include <net/xfrm.h>
5#include <net/esp.h>
6#include <net/espintcp.h>
7#include <linux/skmsg.h>
8#include <net/inet_common.h>
9#include <trace/events/sock.h>
10#if IS_ENABLED(CONFIG_IPV6)
11#include <net/ipv6_stubs.h>
12#endif
13#include <net/hotdata.h>
14
15static void handle_nonesp(struct espintcp_ctx *ctx, struct sk_buff *skb,
16 struct sock *sk)
17{
18 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf ||
19 !sk_rmem_schedule(sk, skb, skb->truesize)) {
20 XFRM_INC_STATS(sock_net(sk), LINUX_MIB_XFRMINERROR);
21 kfree_skb(skb);
22 return;
23 }
24
25 skb_set_owner_r(skb, sk);
26
27 memset(skb->cb, 0, sizeof(skb->cb));
28 skb_queue_tail(&ctx->ike_queue, skb);
29 ctx->saved_data_ready(sk);
30}
31
32static void handle_esp(struct sk_buff *skb, struct sock *sk)
33{
34 struct tcp_skb_cb *tcp_cb = (struct tcp_skb_cb *)skb->cb;
35
36 skb_reset_transport_header(skb);
37
38 /* restore IP CB, we need at least IP6CB->nhoff */
39 memmove(skb->cb, &tcp_cb->header, sizeof(tcp_cb->header));
40
41 rcu_read_lock();
42 skb->dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
43 local_bh_disable();
44#if IS_ENABLED(CONFIG_IPV6)
45 if (sk->sk_family == AF_INET6)
46 ipv6_stub->xfrm6_rcv_encap(skb, IPPROTO_ESP, 0, TCP_ENCAP_ESPINTCP);
47 else
48#endif
49 xfrm4_rcv_encap(skb, IPPROTO_ESP, 0, TCP_ENCAP_ESPINTCP);
50 local_bh_enable();
51 rcu_read_unlock();
52}
53
54static void espintcp_rcv(struct strparser *strp, struct sk_buff *skb)
55{
56 struct espintcp_ctx *ctx = container_of(strp, struct espintcp_ctx,
57 strp);
58 struct strp_msg *rxm = strp_msg(skb);
59 int len = rxm->full_len - 2;
60 u32 nonesp_marker;
61 int err;
62
63 /* keepalive packet? */
64 if (unlikely(len == 1)) {
65 u8 data;
66
67 err = skb_copy_bits(skb, rxm->offset + 2, &data, 1);
68 if (err < 0) {
69 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
70 kfree_skb(skb);
71 return;
72 }
73
74 if (data == 0xff) {
75 kfree_skb(skb);
76 return;
77 }
78 }
79
80 /* drop other short messages */
81 if (unlikely(len <= sizeof(nonesp_marker))) {
82 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
83 kfree_skb(skb);
84 return;
85 }
86
87 err = skb_copy_bits(skb, rxm->offset + 2, &nonesp_marker,
88 sizeof(nonesp_marker));
89 if (err < 0) {
90 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
91 kfree_skb(skb);
92 return;
93 }
94
95 /* remove header, leave non-ESP marker/SPI */
96 if (!pskb_pull(skb, rxm->offset + 2)) {
97 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINERROR);
98 kfree_skb(skb);
99 return;
100 }
101
102 if (pskb_trim(skb, rxm->full_len - 2) != 0) {
103 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINERROR);
104 kfree_skb(skb);
105 return;
106 }
107
108 if (nonesp_marker == 0)
109 handle_nonesp(ctx, skb, strp->sk);
110 else
111 handle_esp(skb, strp->sk);
112}
113
114static int espintcp_parse(struct strparser *strp, struct sk_buff *skb)
115{
116 struct strp_msg *rxm = strp_msg(skb);
117 __be16 blen;
118 u16 len;
119 int err;
120
121 if (skb->len < rxm->offset + 2)
122 return 0;
123
124 err = skb_copy_bits(skb, rxm->offset, &blen, sizeof(blen));
125 if (err < 0)
126 return err;
127
128 len = be16_to_cpu(blen);
129 if (len < 2)
130 return -EINVAL;
131
132 return len;
133}
134
135static int espintcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
136 int flags, int *addr_len)
137{
138 struct espintcp_ctx *ctx = espintcp_getctx(sk);
139 struct sk_buff *skb;
140 int err = 0;
141 int copied;
142 int off = 0;
143
144 skb = __skb_recv_datagram(sk, &ctx->ike_queue, flags, &off, &err);
145 if (!skb) {
146 if (err == -EAGAIN && sk->sk_shutdown & RCV_SHUTDOWN)
147 return 0;
148 return err;
149 }
150
151 copied = len;
152 if (copied > skb->len)
153 copied = skb->len;
154 else if (copied < skb->len)
155 msg->msg_flags |= MSG_TRUNC;
156
157 err = skb_copy_datagram_msg(skb, 0, msg, copied);
158 if (unlikely(err)) {
159 kfree_skb(skb);
160 return err;
161 }
162
163 if (flags & MSG_TRUNC)
164 copied = skb->len;
165 kfree_skb(skb);
166 return copied;
167}
168
169int espintcp_queue_out(struct sock *sk, struct sk_buff *skb)
170{
171 struct espintcp_ctx *ctx = espintcp_getctx(sk);
172
173 if (skb_queue_len(&ctx->out_queue) >=
174 READ_ONCE(net_hotdata.max_backlog))
175 return -ENOBUFS;
176
177 __skb_queue_tail(&ctx->out_queue, skb);
178
179 return 0;
180}
181EXPORT_SYMBOL_GPL(espintcp_queue_out);
182
183/* espintcp length field is 2B and length includes the length field's size */
184#define MAX_ESPINTCP_MSG (((1 << 16) - 1) - 2)
185
186static int espintcp_sendskb_locked(struct sock *sk, struct espintcp_msg *emsg,
187 int flags)
188{
189 do {
190 int ret;
191
192 ret = skb_send_sock_locked(sk, emsg->skb,
193 emsg->offset, emsg->len);
194 if (ret < 0)
195 return ret;
196
197 emsg->len -= ret;
198 emsg->offset += ret;
199 } while (emsg->len > 0);
200
201 kfree_skb(emsg->skb);
202 memset(emsg, 0, sizeof(*emsg));
203
204 return 0;
205}
206
207static int espintcp_sendskmsg_locked(struct sock *sk,
208 struct espintcp_msg *emsg, int flags)
209{
210 struct msghdr msghdr = {
211 .msg_flags = flags | MSG_SPLICE_PAGES | MSG_MORE,
212 };
213 struct sk_msg *skmsg = &emsg->skmsg;
214 bool more = flags & MSG_MORE;
215 struct scatterlist *sg;
216 int done = 0;
217 int ret;
218
219 sg = &skmsg->sg.data[skmsg->sg.start];
220 do {
221 struct bio_vec bvec;
222 size_t size = sg->length - emsg->offset;
223 int offset = sg->offset + emsg->offset;
224 struct page *p;
225
226 emsg->offset = 0;
227
228 if (sg_is_last(sg) && !more)
229 msghdr.msg_flags &= ~MSG_MORE;
230
231 p = sg_page(sg);
232retry:
233 bvec_set_page(&bvec, p, size, offset);
234 iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size);
235 ret = tcp_sendmsg_locked(sk, &msghdr, size);
236 if (ret < 0) {
237 emsg->offset = offset - sg->offset;
238 skmsg->sg.start += done;
239 return ret;
240 }
241
242 if (ret != size) {
243 offset += ret;
244 size -= ret;
245 goto retry;
246 }
247
248 done++;
249 put_page(p);
250 sk_mem_uncharge(sk, sg->length);
251 sg = sg_next(sg);
252 } while (sg);
253
254 memset(emsg, 0, sizeof(*emsg));
255
256 return 0;
257}
258
259static int espintcp_push_msgs(struct sock *sk, int flags)
260{
261 struct espintcp_ctx *ctx = espintcp_getctx(sk);
262 struct espintcp_msg *emsg = &ctx->partial;
263 int err;
264
265 if (!emsg->len)
266 return 0;
267
268 if (ctx->tx_running)
269 return -EAGAIN;
270 ctx->tx_running = 1;
271
272 if (emsg->skb)
273 err = espintcp_sendskb_locked(sk, emsg, flags);
274 else
275 err = espintcp_sendskmsg_locked(sk, emsg, flags);
276 if (err == -EAGAIN) {
277 ctx->tx_running = 0;
278 return flags & MSG_DONTWAIT ? -EAGAIN : 0;
279 }
280 if (!err)
281 memset(emsg, 0, sizeof(*emsg));
282
283 ctx->tx_running = 0;
284
285 return err;
286}
287
288int espintcp_push_skb(struct sock *sk, struct sk_buff *skb)
289{
290 struct espintcp_ctx *ctx = espintcp_getctx(sk);
291 struct espintcp_msg *emsg = &ctx->partial;
292 unsigned int len;
293 int offset;
294
295 if (sk->sk_state != TCP_ESTABLISHED) {
296 kfree_skb(skb);
297 return -ECONNRESET;
298 }
299
300 offset = skb_transport_offset(skb);
301 len = skb->len - offset;
302
303 espintcp_push_msgs(sk, 0);
304
305 if (emsg->len) {
306 kfree_skb(skb);
307 return -ENOBUFS;
308 }
309
310 skb_set_owner_w(skb, sk);
311
312 emsg->offset = offset;
313 emsg->len = len;
314 emsg->skb = skb;
315
316 espintcp_push_msgs(sk, 0);
317
318 return 0;
319}
320EXPORT_SYMBOL_GPL(espintcp_push_skb);
321
322static int espintcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
323{
324 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
325 struct espintcp_ctx *ctx = espintcp_getctx(sk);
326 struct espintcp_msg *emsg = &ctx->partial;
327 struct iov_iter pfx_iter;
328 struct kvec pfx_iov = {};
329 size_t msglen = size + 2;
330 char buf[2] = {0};
331 int err, end;
332
333 if (msg->msg_flags & ~MSG_DONTWAIT)
334 return -EOPNOTSUPP;
335
336 if (size > MAX_ESPINTCP_MSG)
337 return -EMSGSIZE;
338
339 if (msg->msg_controllen)
340 return -EOPNOTSUPP;
341
342 lock_sock(sk);
343
344 err = espintcp_push_msgs(sk, msg->msg_flags & MSG_DONTWAIT);
345 if (err < 0) {
346 if (err != -EAGAIN || !(msg->msg_flags & MSG_DONTWAIT))
347 err = -ENOBUFS;
348 goto unlock;
349 }
350
351 sk_msg_init(&emsg->skmsg);
352 while (1) {
353 /* only -ENOMEM is possible since we don't coalesce */
354 err = sk_msg_alloc(sk, &emsg->skmsg, msglen, 0);
355 if (!err)
356 break;
357
358 err = sk_stream_wait_memory(sk, &timeo);
359 if (err)
360 goto fail;
361 }
362
363 *((__be16 *)buf) = cpu_to_be16(msglen);
364 pfx_iov.iov_base = buf;
365 pfx_iov.iov_len = sizeof(buf);
366 iov_iter_kvec(&pfx_iter, ITER_SOURCE, &pfx_iov, 1, pfx_iov.iov_len);
367
368 err = sk_msg_memcopy_from_iter(sk, &pfx_iter, &emsg->skmsg,
369 pfx_iov.iov_len);
370 if (err < 0)
371 goto fail;
372
373 err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, &emsg->skmsg, size);
374 if (err < 0)
375 goto fail;
376
377 end = emsg->skmsg.sg.end;
378 emsg->len = size;
379 sk_msg_iter_var_prev(end);
380 sg_mark_end(sk_msg_elem(&emsg->skmsg, end));
381
382 tcp_rate_check_app_limited(sk);
383
384 err = espintcp_push_msgs(sk, msg->msg_flags & MSG_DONTWAIT);
385 /* this message could be partially sent, keep it */
386
387 release_sock(sk);
388
389 return size;
390
391fail:
392 sk_msg_free(sk, &emsg->skmsg);
393 memset(emsg, 0, sizeof(*emsg));
394unlock:
395 release_sock(sk);
396 return err;
397}
398
399static struct proto espintcp_prot __ro_after_init;
400static struct proto_ops espintcp_ops __ro_after_init;
401static struct proto espintcp6_prot;
402static struct proto_ops espintcp6_ops;
403static DEFINE_MUTEX(tcpv6_prot_mutex);
404
405static void espintcp_data_ready(struct sock *sk)
406{
407 struct espintcp_ctx *ctx = espintcp_getctx(sk);
408
409 trace_sk_data_ready(sk);
410
411 strp_data_ready(&ctx->strp);
412}
413
414static void espintcp_tx_work(struct work_struct *work)
415{
416 struct espintcp_ctx *ctx = container_of(work,
417 struct espintcp_ctx, work);
418 struct sock *sk = ctx->strp.sk;
419
420 lock_sock(sk);
421 if (!ctx->tx_running)
422 espintcp_push_msgs(sk, 0);
423 release_sock(sk);
424}
425
426static void espintcp_write_space(struct sock *sk)
427{
428 struct espintcp_ctx *ctx = espintcp_getctx(sk);
429
430 schedule_work(&ctx->work);
431 ctx->saved_write_space(sk);
432}
433
434static void espintcp_destruct(struct sock *sk)
435{
436 struct espintcp_ctx *ctx = espintcp_getctx(sk);
437
438 ctx->saved_destruct(sk);
439 kfree(ctx);
440}
441
442bool tcp_is_ulp_esp(struct sock *sk)
443{
444 return sk->sk_prot == &espintcp_prot || sk->sk_prot == &espintcp6_prot;
445}
446EXPORT_SYMBOL_GPL(tcp_is_ulp_esp);
447
448static void build_protos(struct proto *espintcp_prot,
449 struct proto_ops *espintcp_ops,
450 const struct proto *orig_prot,
451 const struct proto_ops *orig_ops);
452static int espintcp_init_sk(struct sock *sk)
453{
454 struct inet_connection_sock *icsk = inet_csk(sk);
455 struct strp_callbacks cb = {
456 .rcv_msg = espintcp_rcv,
457 .parse_msg = espintcp_parse,
458 };
459 struct espintcp_ctx *ctx;
460 int err;
461
462 /* sockmap is not compatible with espintcp */
463 if (sk->sk_user_data)
464 return -EBUSY;
465
466 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
467 if (!ctx)
468 return -ENOMEM;
469
470 err = strp_init(&ctx->strp, sk, &cb);
471 if (err)
472 goto free;
473
474 __sk_dst_reset(sk);
475
476 strp_check_rcv(&ctx->strp);
477 skb_queue_head_init(&ctx->ike_queue);
478 skb_queue_head_init(&ctx->out_queue);
479
480 if (sk->sk_family == AF_INET) {
481 sk->sk_prot = &espintcp_prot;
482 sk->sk_socket->ops = &espintcp_ops;
483 } else {
484 mutex_lock(&tcpv6_prot_mutex);
485 if (!espintcp6_prot.recvmsg)
486 build_protos(&espintcp6_prot, &espintcp6_ops, sk->sk_prot, sk->sk_socket->ops);
487 mutex_unlock(&tcpv6_prot_mutex);
488
489 sk->sk_prot = &espintcp6_prot;
490 sk->sk_socket->ops = &espintcp6_ops;
491 }
492 ctx->saved_data_ready = sk->sk_data_ready;
493 ctx->saved_write_space = sk->sk_write_space;
494 ctx->saved_destruct = sk->sk_destruct;
495 sk->sk_data_ready = espintcp_data_ready;
496 sk->sk_write_space = espintcp_write_space;
497 sk->sk_destruct = espintcp_destruct;
498 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
499 INIT_WORK(&ctx->work, espintcp_tx_work);
500
501 /* avoid using task_frag */
502 sk->sk_allocation = GFP_ATOMIC;
503 sk->sk_use_task_frag = false;
504
505 return 0;
506
507free:
508 kfree(ctx);
509 return err;
510}
511
512static void espintcp_release(struct sock *sk)
513{
514 struct espintcp_ctx *ctx = espintcp_getctx(sk);
515 struct sk_buff_head queue;
516 struct sk_buff *skb;
517
518 __skb_queue_head_init(&queue);
519 skb_queue_splice_init(&ctx->out_queue, &queue);
520
521 while ((skb = __skb_dequeue(&queue)))
522 espintcp_push_skb(sk, skb);
523
524 tcp_release_cb(sk);
525}
526
527static void espintcp_close(struct sock *sk, long timeout)
528{
529 struct espintcp_ctx *ctx = espintcp_getctx(sk);
530 struct espintcp_msg *emsg = &ctx->partial;
531
532 strp_stop(&ctx->strp);
533
534 sk->sk_prot = &tcp_prot;
535 barrier();
536
537 cancel_work_sync(&ctx->work);
538 strp_done(&ctx->strp);
539
540 skb_queue_purge(&ctx->out_queue);
541 skb_queue_purge(&ctx->ike_queue);
542
543 if (emsg->len) {
544 if (emsg->skb)
545 kfree_skb(emsg->skb);
546 else
547 sk_msg_free(sk, &emsg->skmsg);
548 }
549
550 tcp_close(sk, timeout);
551}
552
553static __poll_t espintcp_poll(struct file *file, struct socket *sock,
554 poll_table *wait)
555{
556 __poll_t mask = datagram_poll(file, sock, wait);
557 struct sock *sk = sock->sk;
558 struct espintcp_ctx *ctx = espintcp_getctx(sk);
559
560 if (!skb_queue_empty(&ctx->ike_queue))
561 mask |= EPOLLIN | EPOLLRDNORM;
562
563 return mask;
564}
565
566static void build_protos(struct proto *espintcp_prot,
567 struct proto_ops *espintcp_ops,
568 const struct proto *orig_prot,
569 const struct proto_ops *orig_ops)
570{
571 memcpy(espintcp_prot, orig_prot, sizeof(struct proto));
572 memcpy(espintcp_ops, orig_ops, sizeof(struct proto_ops));
573 espintcp_prot->sendmsg = espintcp_sendmsg;
574 espintcp_prot->recvmsg = espintcp_recvmsg;
575 espintcp_prot->close = espintcp_close;
576 espintcp_prot->release_cb = espintcp_release;
577 espintcp_ops->poll = espintcp_poll;
578}
579
580static struct tcp_ulp_ops espintcp_ulp __read_mostly = {
581 .name = "espintcp",
582 .owner = THIS_MODULE,
583 .init = espintcp_init_sk,
584};
585
586void __init espintcp_init(void)
587{
588 build_protos(&espintcp_prot, &espintcp_ops, &tcp_prot, &inet_stream_ops);
589
590 tcp_register_ulp(&espintcp_ulp);
591}
1// SPDX-License-Identifier: GPL-2.0
2#include <net/tcp.h>
3#include <net/strparser.h>
4#include <net/xfrm.h>
5#include <net/esp.h>
6#include <net/espintcp.h>
7#include <linux/skmsg.h>
8#include <net/inet_common.h>
9#include <trace/events/sock.h>
10#if IS_ENABLED(CONFIG_IPV6)
11#include <net/ipv6_stubs.h>
12#endif
13
14static void handle_nonesp(struct espintcp_ctx *ctx, struct sk_buff *skb,
15 struct sock *sk)
16{
17 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf ||
18 !sk_rmem_schedule(sk, skb, skb->truesize)) {
19 XFRM_INC_STATS(sock_net(sk), LINUX_MIB_XFRMINERROR);
20 kfree_skb(skb);
21 return;
22 }
23
24 skb_set_owner_r(skb, sk);
25
26 memset(skb->cb, 0, sizeof(skb->cb));
27 skb_queue_tail(&ctx->ike_queue, skb);
28 ctx->saved_data_ready(sk);
29}
30
31static void handle_esp(struct sk_buff *skb, struct sock *sk)
32{
33 struct tcp_skb_cb *tcp_cb = (struct tcp_skb_cb *)skb->cb;
34
35 skb_reset_transport_header(skb);
36
37 /* restore IP CB, we need at least IP6CB->nhoff */
38 memmove(skb->cb, &tcp_cb->header, sizeof(tcp_cb->header));
39
40 rcu_read_lock();
41 skb->dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
42 local_bh_disable();
43#if IS_ENABLED(CONFIG_IPV6)
44 if (sk->sk_family == AF_INET6)
45 ipv6_stub->xfrm6_rcv_encap(skb, IPPROTO_ESP, 0, TCP_ENCAP_ESPINTCP);
46 else
47#endif
48 xfrm4_rcv_encap(skb, IPPROTO_ESP, 0, TCP_ENCAP_ESPINTCP);
49 local_bh_enable();
50 rcu_read_unlock();
51}
52
53static void espintcp_rcv(struct strparser *strp, struct sk_buff *skb)
54{
55 struct espintcp_ctx *ctx = container_of(strp, struct espintcp_ctx,
56 strp);
57 struct strp_msg *rxm = strp_msg(skb);
58 int len = rxm->full_len - 2;
59 u32 nonesp_marker;
60 int err;
61
62 /* keepalive packet? */
63 if (unlikely(len == 1)) {
64 u8 data;
65
66 err = skb_copy_bits(skb, rxm->offset + 2, &data, 1);
67 if (err < 0) {
68 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
69 kfree_skb(skb);
70 return;
71 }
72
73 if (data == 0xff) {
74 kfree_skb(skb);
75 return;
76 }
77 }
78
79 /* drop other short messages */
80 if (unlikely(len <= sizeof(nonesp_marker))) {
81 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
82 kfree_skb(skb);
83 return;
84 }
85
86 err = skb_copy_bits(skb, rxm->offset + 2, &nonesp_marker,
87 sizeof(nonesp_marker));
88 if (err < 0) {
89 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
90 kfree_skb(skb);
91 return;
92 }
93
94 /* remove header, leave non-ESP marker/SPI */
95 if (!pskb_pull(skb, rxm->offset + 2)) {
96 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINERROR);
97 kfree_skb(skb);
98 return;
99 }
100
101 if (pskb_trim(skb, rxm->full_len - 2) != 0) {
102 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINERROR);
103 kfree_skb(skb);
104 return;
105 }
106
107 if (nonesp_marker == 0)
108 handle_nonesp(ctx, skb, strp->sk);
109 else
110 handle_esp(skb, strp->sk);
111}
112
113static int espintcp_parse(struct strparser *strp, struct sk_buff *skb)
114{
115 struct strp_msg *rxm = strp_msg(skb);
116 __be16 blen;
117 u16 len;
118 int err;
119
120 if (skb->len < rxm->offset + 2)
121 return 0;
122
123 err = skb_copy_bits(skb, rxm->offset, &blen, sizeof(blen));
124 if (err < 0)
125 return err;
126
127 len = be16_to_cpu(blen);
128 if (len < 2)
129 return -EINVAL;
130
131 return len;
132}
133
134static int espintcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
135 int flags, int *addr_len)
136{
137 struct espintcp_ctx *ctx = espintcp_getctx(sk);
138 struct sk_buff *skb;
139 int err = 0;
140 int copied;
141 int off = 0;
142
143 skb = __skb_recv_datagram(sk, &ctx->ike_queue, flags, &off, &err);
144 if (!skb) {
145 if (err == -EAGAIN && sk->sk_shutdown & RCV_SHUTDOWN)
146 return 0;
147 return err;
148 }
149
150 copied = len;
151 if (copied > skb->len)
152 copied = skb->len;
153 else if (copied < skb->len)
154 msg->msg_flags |= MSG_TRUNC;
155
156 err = skb_copy_datagram_msg(skb, 0, msg, copied);
157 if (unlikely(err)) {
158 kfree_skb(skb);
159 return err;
160 }
161
162 if (flags & MSG_TRUNC)
163 copied = skb->len;
164 kfree_skb(skb);
165 return copied;
166}
167
168int espintcp_queue_out(struct sock *sk, struct sk_buff *skb)
169{
170 struct espintcp_ctx *ctx = espintcp_getctx(sk);
171
172 if (skb_queue_len(&ctx->out_queue) >= READ_ONCE(netdev_max_backlog))
173 return -ENOBUFS;
174
175 __skb_queue_tail(&ctx->out_queue, skb);
176
177 return 0;
178}
179EXPORT_SYMBOL_GPL(espintcp_queue_out);
180
181/* espintcp length field is 2B and length includes the length field's size */
182#define MAX_ESPINTCP_MSG (((1 << 16) - 1) - 2)
183
184static int espintcp_sendskb_locked(struct sock *sk, struct espintcp_msg *emsg,
185 int flags)
186{
187 do {
188 int ret;
189
190 ret = skb_send_sock_locked(sk, emsg->skb,
191 emsg->offset, emsg->len);
192 if (ret < 0)
193 return ret;
194
195 emsg->len -= ret;
196 emsg->offset += ret;
197 } while (emsg->len > 0);
198
199 kfree_skb(emsg->skb);
200 memset(emsg, 0, sizeof(*emsg));
201
202 return 0;
203}
204
205static int espintcp_sendskmsg_locked(struct sock *sk,
206 struct espintcp_msg *emsg, int flags)
207{
208 struct msghdr msghdr = {
209 .msg_flags = flags | MSG_SPLICE_PAGES | MSG_MORE,
210 };
211 struct sk_msg *skmsg = &emsg->skmsg;
212 bool more = flags & MSG_MORE;
213 struct scatterlist *sg;
214 int done = 0;
215 int ret;
216
217 sg = &skmsg->sg.data[skmsg->sg.start];
218 do {
219 struct bio_vec bvec;
220 size_t size = sg->length - emsg->offset;
221 int offset = sg->offset + emsg->offset;
222 struct page *p;
223
224 emsg->offset = 0;
225
226 if (sg_is_last(sg) && !more)
227 msghdr.msg_flags &= ~MSG_MORE;
228
229 p = sg_page(sg);
230retry:
231 bvec_set_page(&bvec, p, size, offset);
232 iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size);
233 ret = tcp_sendmsg_locked(sk, &msghdr, size);
234 if (ret < 0) {
235 emsg->offset = offset - sg->offset;
236 skmsg->sg.start += done;
237 return ret;
238 }
239
240 if (ret != size) {
241 offset += ret;
242 size -= ret;
243 goto retry;
244 }
245
246 done++;
247 put_page(p);
248 sk_mem_uncharge(sk, sg->length);
249 sg = sg_next(sg);
250 } while (sg);
251
252 memset(emsg, 0, sizeof(*emsg));
253
254 return 0;
255}
256
257static int espintcp_push_msgs(struct sock *sk, int flags)
258{
259 struct espintcp_ctx *ctx = espintcp_getctx(sk);
260 struct espintcp_msg *emsg = &ctx->partial;
261 int err;
262
263 if (!emsg->len)
264 return 0;
265
266 if (ctx->tx_running)
267 return -EAGAIN;
268 ctx->tx_running = 1;
269
270 if (emsg->skb)
271 err = espintcp_sendskb_locked(sk, emsg, flags);
272 else
273 err = espintcp_sendskmsg_locked(sk, emsg, flags);
274 if (err == -EAGAIN) {
275 ctx->tx_running = 0;
276 return flags & MSG_DONTWAIT ? -EAGAIN : 0;
277 }
278 if (!err)
279 memset(emsg, 0, sizeof(*emsg));
280
281 ctx->tx_running = 0;
282
283 return err;
284}
285
286int espintcp_push_skb(struct sock *sk, struct sk_buff *skb)
287{
288 struct espintcp_ctx *ctx = espintcp_getctx(sk);
289 struct espintcp_msg *emsg = &ctx->partial;
290 unsigned int len;
291 int offset;
292
293 if (sk->sk_state != TCP_ESTABLISHED) {
294 kfree_skb(skb);
295 return -ECONNRESET;
296 }
297
298 offset = skb_transport_offset(skb);
299 len = skb->len - offset;
300
301 espintcp_push_msgs(sk, 0);
302
303 if (emsg->len) {
304 kfree_skb(skb);
305 return -ENOBUFS;
306 }
307
308 skb_set_owner_w(skb, sk);
309
310 emsg->offset = offset;
311 emsg->len = len;
312 emsg->skb = skb;
313
314 espintcp_push_msgs(sk, 0);
315
316 return 0;
317}
318EXPORT_SYMBOL_GPL(espintcp_push_skb);
319
320static int espintcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
321{
322 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
323 struct espintcp_ctx *ctx = espintcp_getctx(sk);
324 struct espintcp_msg *emsg = &ctx->partial;
325 struct iov_iter pfx_iter;
326 struct kvec pfx_iov = {};
327 size_t msglen = size + 2;
328 char buf[2] = {0};
329 int err, end;
330
331 if (msg->msg_flags & ~MSG_DONTWAIT)
332 return -EOPNOTSUPP;
333
334 if (size > MAX_ESPINTCP_MSG)
335 return -EMSGSIZE;
336
337 if (msg->msg_controllen)
338 return -EOPNOTSUPP;
339
340 lock_sock(sk);
341
342 err = espintcp_push_msgs(sk, msg->msg_flags & MSG_DONTWAIT);
343 if (err < 0) {
344 if (err != -EAGAIN || !(msg->msg_flags & MSG_DONTWAIT))
345 err = -ENOBUFS;
346 goto unlock;
347 }
348
349 sk_msg_init(&emsg->skmsg);
350 while (1) {
351 /* only -ENOMEM is possible since we don't coalesce */
352 err = sk_msg_alloc(sk, &emsg->skmsg, msglen, 0);
353 if (!err)
354 break;
355
356 err = sk_stream_wait_memory(sk, &timeo);
357 if (err)
358 goto fail;
359 }
360
361 *((__be16 *)buf) = cpu_to_be16(msglen);
362 pfx_iov.iov_base = buf;
363 pfx_iov.iov_len = sizeof(buf);
364 iov_iter_kvec(&pfx_iter, ITER_SOURCE, &pfx_iov, 1, pfx_iov.iov_len);
365
366 err = sk_msg_memcopy_from_iter(sk, &pfx_iter, &emsg->skmsg,
367 pfx_iov.iov_len);
368 if (err < 0)
369 goto fail;
370
371 err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, &emsg->skmsg, size);
372 if (err < 0)
373 goto fail;
374
375 end = emsg->skmsg.sg.end;
376 emsg->len = size;
377 sk_msg_iter_var_prev(end);
378 sg_mark_end(sk_msg_elem(&emsg->skmsg, end));
379
380 tcp_rate_check_app_limited(sk);
381
382 err = espintcp_push_msgs(sk, msg->msg_flags & MSG_DONTWAIT);
383 /* this message could be partially sent, keep it */
384
385 release_sock(sk);
386
387 return size;
388
389fail:
390 sk_msg_free(sk, &emsg->skmsg);
391 memset(emsg, 0, sizeof(*emsg));
392unlock:
393 release_sock(sk);
394 return err;
395}
396
397static struct proto espintcp_prot __ro_after_init;
398static struct proto_ops espintcp_ops __ro_after_init;
399static struct proto espintcp6_prot;
400static struct proto_ops espintcp6_ops;
401static DEFINE_MUTEX(tcpv6_prot_mutex);
402
403static void espintcp_data_ready(struct sock *sk)
404{
405 struct espintcp_ctx *ctx = espintcp_getctx(sk);
406
407 trace_sk_data_ready(sk);
408
409 strp_data_ready(&ctx->strp);
410}
411
412static void espintcp_tx_work(struct work_struct *work)
413{
414 struct espintcp_ctx *ctx = container_of(work,
415 struct espintcp_ctx, work);
416 struct sock *sk = ctx->strp.sk;
417
418 lock_sock(sk);
419 if (!ctx->tx_running)
420 espintcp_push_msgs(sk, 0);
421 release_sock(sk);
422}
423
424static void espintcp_write_space(struct sock *sk)
425{
426 struct espintcp_ctx *ctx = espintcp_getctx(sk);
427
428 schedule_work(&ctx->work);
429 ctx->saved_write_space(sk);
430}
431
432static void espintcp_destruct(struct sock *sk)
433{
434 struct espintcp_ctx *ctx = espintcp_getctx(sk);
435
436 ctx->saved_destruct(sk);
437 kfree(ctx);
438}
439
440bool tcp_is_ulp_esp(struct sock *sk)
441{
442 return sk->sk_prot == &espintcp_prot || sk->sk_prot == &espintcp6_prot;
443}
444EXPORT_SYMBOL_GPL(tcp_is_ulp_esp);
445
446static void build_protos(struct proto *espintcp_prot,
447 struct proto_ops *espintcp_ops,
448 const struct proto *orig_prot,
449 const struct proto_ops *orig_ops);
450static int espintcp_init_sk(struct sock *sk)
451{
452 struct inet_connection_sock *icsk = inet_csk(sk);
453 struct strp_callbacks cb = {
454 .rcv_msg = espintcp_rcv,
455 .parse_msg = espintcp_parse,
456 };
457 struct espintcp_ctx *ctx;
458 int err;
459
460 /* sockmap is not compatible with espintcp */
461 if (sk->sk_user_data)
462 return -EBUSY;
463
464 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
465 if (!ctx)
466 return -ENOMEM;
467
468 err = strp_init(&ctx->strp, sk, &cb);
469 if (err)
470 goto free;
471
472 __sk_dst_reset(sk);
473
474 strp_check_rcv(&ctx->strp);
475 skb_queue_head_init(&ctx->ike_queue);
476 skb_queue_head_init(&ctx->out_queue);
477
478 if (sk->sk_family == AF_INET) {
479 sk->sk_prot = &espintcp_prot;
480 sk->sk_socket->ops = &espintcp_ops;
481 } else {
482 mutex_lock(&tcpv6_prot_mutex);
483 if (!espintcp6_prot.recvmsg)
484 build_protos(&espintcp6_prot, &espintcp6_ops, sk->sk_prot, sk->sk_socket->ops);
485 mutex_unlock(&tcpv6_prot_mutex);
486
487 sk->sk_prot = &espintcp6_prot;
488 sk->sk_socket->ops = &espintcp6_ops;
489 }
490 ctx->saved_data_ready = sk->sk_data_ready;
491 ctx->saved_write_space = sk->sk_write_space;
492 ctx->saved_destruct = sk->sk_destruct;
493 sk->sk_data_ready = espintcp_data_ready;
494 sk->sk_write_space = espintcp_write_space;
495 sk->sk_destruct = espintcp_destruct;
496 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
497 INIT_WORK(&ctx->work, espintcp_tx_work);
498
499 /* avoid using task_frag */
500 sk->sk_allocation = GFP_ATOMIC;
501 sk->sk_use_task_frag = false;
502
503 return 0;
504
505free:
506 kfree(ctx);
507 return err;
508}
509
510static void espintcp_release(struct sock *sk)
511{
512 struct espintcp_ctx *ctx = espintcp_getctx(sk);
513 struct sk_buff_head queue;
514 struct sk_buff *skb;
515
516 __skb_queue_head_init(&queue);
517 skb_queue_splice_init(&ctx->out_queue, &queue);
518
519 while ((skb = __skb_dequeue(&queue)))
520 espintcp_push_skb(sk, skb);
521
522 tcp_release_cb(sk);
523}
524
525static void espintcp_close(struct sock *sk, long timeout)
526{
527 struct espintcp_ctx *ctx = espintcp_getctx(sk);
528 struct espintcp_msg *emsg = &ctx->partial;
529
530 strp_stop(&ctx->strp);
531
532 sk->sk_prot = &tcp_prot;
533 barrier();
534
535 cancel_work_sync(&ctx->work);
536 strp_done(&ctx->strp);
537
538 skb_queue_purge(&ctx->out_queue);
539 skb_queue_purge(&ctx->ike_queue);
540
541 if (emsg->len) {
542 if (emsg->skb)
543 kfree_skb(emsg->skb);
544 else
545 sk_msg_free(sk, &emsg->skmsg);
546 }
547
548 tcp_close(sk, timeout);
549}
550
551static __poll_t espintcp_poll(struct file *file, struct socket *sock,
552 poll_table *wait)
553{
554 __poll_t mask = datagram_poll(file, sock, wait);
555 struct sock *sk = sock->sk;
556 struct espintcp_ctx *ctx = espintcp_getctx(sk);
557
558 if (!skb_queue_empty(&ctx->ike_queue))
559 mask |= EPOLLIN | EPOLLRDNORM;
560
561 return mask;
562}
563
564static void build_protos(struct proto *espintcp_prot,
565 struct proto_ops *espintcp_ops,
566 const struct proto *orig_prot,
567 const struct proto_ops *orig_ops)
568{
569 memcpy(espintcp_prot, orig_prot, sizeof(struct proto));
570 memcpy(espintcp_ops, orig_ops, sizeof(struct proto_ops));
571 espintcp_prot->sendmsg = espintcp_sendmsg;
572 espintcp_prot->recvmsg = espintcp_recvmsg;
573 espintcp_prot->close = espintcp_close;
574 espintcp_prot->release_cb = espintcp_release;
575 espintcp_ops->poll = espintcp_poll;
576}
577
578static struct tcp_ulp_ops espintcp_ulp __read_mostly = {
579 .name = "espintcp",
580 .owner = THIS_MODULE,
581 .init = espintcp_init_sk,
582};
583
584void __init espintcp_init(void)
585{
586 build_protos(&espintcp_prot, &espintcp_ops, &tcp_prot, &inet_stream_ops);
587
588 tcp_register_ulp(&espintcp_ulp);
589}