1 // SPDX-License-Identifier: GPL-2.0
3 #include <net/strparser.h>
6 #include <net/espintcp.h>
7 #include <linux/skmsg.h>
8 #include <net/inet_common.h>
9 #if IS_ENABLED(CONFIG_IPV6)
10 #include <net/ipv6_stubs.h>
13 static void handle_nonesp(struct espintcp_ctx *ctx, struct sk_buff *skb,
16 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf ||
17 !sk_rmem_schedule(sk, skb, skb->truesize)) {
18 XFRM_INC_STATS(sock_net(sk), LINUX_MIB_XFRMINERROR);
23 skb_set_owner_r(skb, sk);
25 memset(skb->cb, 0, sizeof(skb->cb));
26 skb_queue_tail(&ctx->ike_queue, skb);
27 ctx->saved_data_ready(sk);
30 static void handle_esp(struct sk_buff *skb, struct sock *sk)
32 struct tcp_skb_cb *tcp_cb = (struct tcp_skb_cb *)skb->cb;
34 skb_reset_transport_header(skb);
36 /* restore IP CB, we need at least IP6CB->nhoff */
37 memmove(skb->cb, &tcp_cb->header, sizeof(tcp_cb->header));
40 skb->dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
42 #if IS_ENABLED(CONFIG_IPV6)
43 if (sk->sk_family == AF_INET6)
44 ipv6_stub->xfrm6_rcv_encap(skb, IPPROTO_ESP, 0, TCP_ENCAP_ESPINTCP);
47 xfrm4_rcv_encap(skb, IPPROTO_ESP, 0, TCP_ENCAP_ESPINTCP);
52 static void espintcp_rcv(struct strparser *strp, struct sk_buff *skb)
54 struct espintcp_ctx *ctx = container_of(strp, struct espintcp_ctx,
56 struct strp_msg *rxm = strp_msg(skb);
57 int len = rxm->full_len - 2;
61 /* keepalive packet? */
62 if (unlikely(len == 1)) {
65 err = skb_copy_bits(skb, rxm->offset + 2, &data, 1);
67 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
78 /* drop other short messages */
79 if (unlikely(len <= sizeof(nonesp_marker))) {
80 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
85 err = skb_copy_bits(skb, rxm->offset + 2, &nonesp_marker,
86 sizeof(nonesp_marker));
88 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINHDRERROR);
93 /* remove header, leave non-ESP marker/SPI */
94 if (!pskb_pull(skb, rxm->offset + 2)) {
95 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINERROR);
100 if (pskb_trim(skb, rxm->full_len - 2) != 0) {
101 XFRM_INC_STATS(sock_net(strp->sk), LINUX_MIB_XFRMINERROR);
106 if (nonesp_marker == 0)
107 handle_nonesp(ctx, skb, strp->sk);
109 handle_esp(skb, strp->sk);
112 static int espintcp_parse(struct strparser *strp, struct sk_buff *skb)
114 struct strp_msg *rxm = strp_msg(skb);
119 if (skb->len < rxm->offset + 2)
122 err = skb_copy_bits(skb, rxm->offset, &blen, sizeof(blen));
126 len = be16_to_cpu(blen);
133 static int espintcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
134 int flags, int *addr_len)
136 struct espintcp_ctx *ctx = espintcp_getctx(sk);
142 skb = __skb_recv_datagram(sk, &ctx->ike_queue, flags, &off, &err);
144 if (err == -EAGAIN && sk->sk_shutdown & RCV_SHUTDOWN)
150 if (copied > skb->len)
152 else if (copied < skb->len)
153 msg->msg_flags |= MSG_TRUNC;
155 err = skb_copy_datagram_msg(skb, 0, msg, copied);
161 if (flags & MSG_TRUNC)
167 int espintcp_queue_out(struct sock *sk, struct sk_buff *skb)
169 struct espintcp_ctx *ctx = espintcp_getctx(sk);
171 if (skb_queue_len(&ctx->out_queue) >= READ_ONCE(netdev_max_backlog))
174 __skb_queue_tail(&ctx->out_queue, skb);
178 EXPORT_SYMBOL_GPL(espintcp_queue_out);
180 /* espintcp length field is 2B and length includes the length field's size */
181 #define MAX_ESPINTCP_MSG (((1 << 16) - 1) - 2)
183 static int espintcp_sendskb_locked(struct sock *sk, struct espintcp_msg *emsg,
189 ret = skb_send_sock_locked(sk, emsg->skb,
190 emsg->offset, emsg->len);
196 } while (emsg->len > 0);
198 kfree_skb(emsg->skb);
199 memset(emsg, 0, sizeof(*emsg));
204 static int espintcp_sendskmsg_locked(struct sock *sk,
205 struct espintcp_msg *emsg, int flags)
207 struct sk_msg *skmsg = &emsg->skmsg;
208 struct scatterlist *sg;
212 flags |= MSG_SENDPAGE_NOTLAST;
213 sg = &skmsg->sg.data[skmsg->sg.start];
215 size_t size = sg->length - emsg->offset;
216 int offset = sg->offset + emsg->offset;
222 flags &= ~MSG_SENDPAGE_NOTLAST;
226 ret = do_tcp_sendpages(sk, p, offset, size, flags);
228 emsg->offset = offset - sg->offset;
229 skmsg->sg.start += done;
241 sk_mem_uncharge(sk, sg->length);
245 memset(emsg, 0, sizeof(*emsg));
250 static int espintcp_push_msgs(struct sock *sk, int flags)
252 struct espintcp_ctx *ctx = espintcp_getctx(sk);
253 struct espintcp_msg *emsg = &ctx->partial;
264 err = espintcp_sendskb_locked(sk, emsg, flags);
266 err = espintcp_sendskmsg_locked(sk, emsg, flags);
267 if (err == -EAGAIN) {
269 return flags & MSG_DONTWAIT ? -EAGAIN : 0;
272 memset(emsg, 0, sizeof(*emsg));
279 int espintcp_push_skb(struct sock *sk, struct sk_buff *skb)
281 struct espintcp_ctx *ctx = espintcp_getctx(sk);
282 struct espintcp_msg *emsg = &ctx->partial;
286 if (sk->sk_state != TCP_ESTABLISHED) {
291 offset = skb_transport_offset(skb);
292 len = skb->len - offset;
294 espintcp_push_msgs(sk, 0);
301 skb_set_owner_w(skb, sk);
303 emsg->offset = offset;
307 espintcp_push_msgs(sk, 0);
311 EXPORT_SYMBOL_GPL(espintcp_push_skb);
313 static int espintcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
315 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
316 struct espintcp_ctx *ctx = espintcp_getctx(sk);
317 struct espintcp_msg *emsg = &ctx->partial;
318 struct iov_iter pfx_iter;
319 struct kvec pfx_iov = {};
320 size_t msglen = size + 2;
324 if (msg->msg_flags & ~MSG_DONTWAIT)
327 if (size > MAX_ESPINTCP_MSG)
330 if (msg->msg_controllen)
335 err = espintcp_push_msgs(sk, msg->msg_flags & MSG_DONTWAIT);
337 if (err != -EAGAIN || !(msg->msg_flags & MSG_DONTWAIT))
342 sk_msg_init(&emsg->skmsg);
344 /* only -ENOMEM is possible since we don't coalesce */
345 err = sk_msg_alloc(sk, &emsg->skmsg, msglen, 0);
349 err = sk_stream_wait_memory(sk, &timeo);
354 *((__be16 *)buf) = cpu_to_be16(msglen);
355 pfx_iov.iov_base = buf;
356 pfx_iov.iov_len = sizeof(buf);
357 iov_iter_kvec(&pfx_iter, WRITE, &pfx_iov, 1, pfx_iov.iov_len);
359 err = sk_msg_memcopy_from_iter(sk, &pfx_iter, &emsg->skmsg,
364 err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, &emsg->skmsg, size);
368 end = emsg->skmsg.sg.end;
370 sk_msg_iter_var_prev(end);
371 sg_mark_end(sk_msg_elem(&emsg->skmsg, end));
373 tcp_rate_check_app_limited(sk);
375 err = espintcp_push_msgs(sk, msg->msg_flags & MSG_DONTWAIT);
376 /* this message could be partially sent, keep it */
383 sk_msg_free(sk, &emsg->skmsg);
384 memset(emsg, 0, sizeof(*emsg));
390 static struct proto espintcp_prot __ro_after_init;
391 static struct proto_ops espintcp_ops __ro_after_init;
392 static struct proto espintcp6_prot;
393 static struct proto_ops espintcp6_ops;
394 static DEFINE_MUTEX(tcpv6_prot_mutex);
396 static void espintcp_data_ready(struct sock *sk)
398 struct espintcp_ctx *ctx = espintcp_getctx(sk);
400 strp_data_ready(&ctx->strp);
403 static void espintcp_tx_work(struct work_struct *work)
405 struct espintcp_ctx *ctx = container_of(work,
406 struct espintcp_ctx, work);
407 struct sock *sk = ctx->strp.sk;
410 if (!ctx->tx_running)
411 espintcp_push_msgs(sk, 0);
415 static void espintcp_write_space(struct sock *sk)
417 struct espintcp_ctx *ctx = espintcp_getctx(sk);
419 schedule_work(&ctx->work);
420 ctx->saved_write_space(sk);
423 static void espintcp_destruct(struct sock *sk)
425 struct espintcp_ctx *ctx = espintcp_getctx(sk);
427 ctx->saved_destruct(sk);
431 bool tcp_is_ulp_esp(struct sock *sk)
433 return sk->sk_prot == &espintcp_prot || sk->sk_prot == &espintcp6_prot;
435 EXPORT_SYMBOL_GPL(tcp_is_ulp_esp);
437 static void build_protos(struct proto *espintcp_prot,
438 struct proto_ops *espintcp_ops,
439 const struct proto *orig_prot,
440 const struct proto_ops *orig_ops);
441 static int espintcp_init_sk(struct sock *sk)
443 struct inet_connection_sock *icsk = inet_csk(sk);
444 struct strp_callbacks cb = {
445 .rcv_msg = espintcp_rcv,
446 .parse_msg = espintcp_parse,
448 struct espintcp_ctx *ctx;
451 /* sockmap is not compatible with espintcp */
452 if (sk->sk_user_data)
455 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
459 err = strp_init(&ctx->strp, sk, &cb);
465 strp_check_rcv(&ctx->strp);
466 skb_queue_head_init(&ctx->ike_queue);
467 skb_queue_head_init(&ctx->out_queue);
469 if (sk->sk_family == AF_INET) {
470 sk->sk_prot = &espintcp_prot;
471 sk->sk_socket->ops = &espintcp_ops;
473 mutex_lock(&tcpv6_prot_mutex);
474 if (!espintcp6_prot.recvmsg)
475 build_protos(&espintcp6_prot, &espintcp6_ops, sk->sk_prot, sk->sk_socket->ops);
476 mutex_unlock(&tcpv6_prot_mutex);
478 sk->sk_prot = &espintcp6_prot;
479 sk->sk_socket->ops = &espintcp6_ops;
481 ctx->saved_data_ready = sk->sk_data_ready;
482 ctx->saved_write_space = sk->sk_write_space;
483 ctx->saved_destruct = sk->sk_destruct;
484 sk->sk_data_ready = espintcp_data_ready;
485 sk->sk_write_space = espintcp_write_space;
486 sk->sk_destruct = espintcp_destruct;
487 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
488 INIT_WORK(&ctx->work, espintcp_tx_work);
490 /* avoid using task_frag */
491 sk->sk_allocation = GFP_ATOMIC;
500 static void espintcp_release(struct sock *sk)
502 struct espintcp_ctx *ctx = espintcp_getctx(sk);
503 struct sk_buff_head queue;
506 __skb_queue_head_init(&queue);
507 skb_queue_splice_init(&ctx->out_queue, &queue);
509 while ((skb = __skb_dequeue(&queue)))
510 espintcp_push_skb(sk, skb);
515 static void espintcp_close(struct sock *sk, long timeout)
517 struct espintcp_ctx *ctx = espintcp_getctx(sk);
518 struct espintcp_msg *emsg = &ctx->partial;
520 strp_stop(&ctx->strp);
522 sk->sk_prot = &tcp_prot;
525 cancel_work_sync(&ctx->work);
526 strp_done(&ctx->strp);
528 skb_queue_purge(&ctx->out_queue);
529 skb_queue_purge(&ctx->ike_queue);
533 kfree_skb(emsg->skb);
535 sk_msg_free(sk, &emsg->skmsg);
538 tcp_close(sk, timeout);
541 static __poll_t espintcp_poll(struct file *file, struct socket *sock,
544 __poll_t mask = datagram_poll(file, sock, wait);
545 struct sock *sk = sock->sk;
546 struct espintcp_ctx *ctx = espintcp_getctx(sk);
548 if (!skb_queue_empty(&ctx->ike_queue))
549 mask |= EPOLLIN | EPOLLRDNORM;
554 static void build_protos(struct proto *espintcp_prot,
555 struct proto_ops *espintcp_ops,
556 const struct proto *orig_prot,
557 const struct proto_ops *orig_ops)
559 memcpy(espintcp_prot, orig_prot, sizeof(struct proto));
560 memcpy(espintcp_ops, orig_ops, sizeof(struct proto_ops));
561 espintcp_prot->sendmsg = espintcp_sendmsg;
562 espintcp_prot->recvmsg = espintcp_recvmsg;
563 espintcp_prot->close = espintcp_close;
564 espintcp_prot->release_cb = espintcp_release;
565 espintcp_ops->poll = espintcp_poll;
568 static struct tcp_ulp_ops espintcp_ulp __read_mostly = {
570 .owner = THIS_MODULE,
571 .init = espintcp_init_sk,
574 void __init espintcp_init(void)
576 build_protos(&espintcp_prot, &espintcp_ops, &tcp_prot, &inet_stream_ops);
578 tcp_register_ulp(&espintcp_ulp);