xref: /openbmc/linux/net/ipv4/tcp_bpf.c (revision f759f5b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3 
4 #include <linux/skmsg.h>
5 #include <linux/filter.h>
6 #include <linux/bpf.h>
7 #include <linux/init.h>
8 #include <linux/wait.h>
9 
10 #include <net/inet_common.h>
11 #include <net/tls.h>
12 
13 static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
14 			   struct sk_msg *msg, u32 apply_bytes, int flags)
15 {
16 	bool apply = apply_bytes;
17 	struct scatterlist *sge;
18 	u32 size, copied = 0;
19 	struct sk_msg *tmp;
20 	int i, ret = 0;
21 
22 	tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
23 	if (unlikely(!tmp))
24 		return -ENOMEM;
25 
26 	lock_sock(sk);
27 	tmp->sg.start = msg->sg.start;
28 	i = msg->sg.start;
29 	do {
30 		sge = sk_msg_elem(msg, i);
31 		size = (apply && apply_bytes < sge->length) ?
32 			apply_bytes : sge->length;
33 		if (!sk_wmem_schedule(sk, size)) {
34 			if (!copied)
35 				ret = -ENOMEM;
36 			break;
37 		}
38 
39 		sk_mem_charge(sk, size);
40 		sk_msg_xfer(tmp, msg, i, size);
41 		copied += size;
42 		if (sge->length)
43 			get_page(sk_msg_page(tmp, i));
44 		sk_msg_iter_var_next(i);
45 		tmp->sg.end = i;
46 		if (apply) {
47 			apply_bytes -= size;
48 			if (!apply_bytes)
49 				break;
50 		}
51 	} while (i != msg->sg.end);
52 
53 	if (!ret) {
54 		msg->sg.start = i;
55 		sk_psock_queue_msg(psock, tmp);
56 		sk_psock_data_ready(sk, psock);
57 	} else {
58 		sk_msg_free(sk, tmp);
59 		kfree(tmp);
60 	}
61 
62 	release_sock(sk);
63 	return ret;
64 }
65 
66 static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
67 			int flags, bool uncharge)
68 {
69 	bool apply = apply_bytes;
70 	struct scatterlist *sge;
71 	struct page *page;
72 	int size, ret = 0;
73 	u32 off;
74 
75 	while (1) {
76 		bool has_tx_ulp;
77 
78 		sge = sk_msg_elem(msg, msg->sg.start);
79 		size = (apply && apply_bytes < sge->length) ?
80 			apply_bytes : sge->length;
81 		off  = sge->offset;
82 		page = sg_page(sge);
83 
84 		tcp_rate_check_app_limited(sk);
85 retry:
86 		has_tx_ulp = tls_sw_has_ctx_tx(sk);
87 		if (has_tx_ulp) {
88 			flags |= MSG_SENDPAGE_NOPOLICY;
89 			ret = kernel_sendpage_locked(sk,
90 						     page, off, size, flags);
91 		} else {
92 			ret = do_tcp_sendpages(sk, page, off, size, flags);
93 		}
94 
95 		if (ret <= 0)
96 			return ret;
97 		if (apply)
98 			apply_bytes -= ret;
99 		msg->sg.size -= ret;
100 		sge->offset += ret;
101 		sge->length -= ret;
102 		if (uncharge)
103 			sk_mem_uncharge(sk, ret);
104 		if (ret != size) {
105 			size -= ret;
106 			off  += ret;
107 			goto retry;
108 		}
109 		if (!sge->length) {
110 			put_page(page);
111 			sk_msg_iter_next(msg, start);
112 			sg_init_table(sge, 1);
113 			if (msg->sg.start == msg->sg.end)
114 				break;
115 		}
116 		if (apply && !apply_bytes)
117 			break;
118 	}
119 
120 	return 0;
121 }
122 
123 static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
124 			       u32 apply_bytes, int flags, bool uncharge)
125 {
126 	int ret;
127 
128 	lock_sock(sk);
129 	ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
130 	release_sock(sk);
131 	return ret;
132 }
133 
134 int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg,
135 			  u32 bytes, int flags)
136 {
137 	bool ingress = sk_msg_to_ingress(msg);
138 	struct sk_psock *psock = sk_psock_get(sk);
139 	int ret;
140 
141 	if (unlikely(!psock))
142 		return -EPIPE;
143 
144 	ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) :
145 			tcp_bpf_push_locked(sk, msg, bytes, flags, false);
146 	sk_psock_put(sk, psock);
147 	return ret;
148 }
149 EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
150 
151 #ifdef CONFIG_BPF_SYSCALL
152 static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
153 			     long timeo)
154 {
155 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
156 	int ret = 0;
157 
158 	if (sk->sk_shutdown & RCV_SHUTDOWN)
159 		return 1;
160 
161 	if (!timeo)
162 		return ret;
163 
164 	add_wait_queue(sk_sleep(sk), &wait);
165 	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
166 	ret = sk_wait_event(sk, &timeo,
167 			    !list_empty(&psock->ingress_msg) ||
168 			    !skb_queue_empty(&sk->sk_receive_queue), &wait);
169 	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
170 	remove_wait_queue(sk_sleep(sk), &wait);
171 	return ret;
172 }
173 
174 static int tcp_bpf_recvmsg_parser(struct sock *sk,
175 				  struct msghdr *msg,
176 				  size_t len,
177 				  int flags,
178 				  int *addr_len)
179 {
180 	struct sk_psock *psock;
181 	int copied;
182 
183 	if (unlikely(flags & MSG_ERRQUEUE))
184 		return inet_recv_error(sk, msg, len, addr_len);
185 
186 	psock = sk_psock_get(sk);
187 	if (unlikely(!psock))
188 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
189 
190 	lock_sock(sk);
191 msg_bytes_ready:
192 	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
193 	if (!copied) {
194 		long timeo;
195 		int data;
196 
197 		if (sock_flag(sk, SOCK_DONE))
198 			goto out;
199 
200 		if (sk->sk_err) {
201 			copied = sock_error(sk);
202 			goto out;
203 		}
204 
205 		if (sk->sk_shutdown & RCV_SHUTDOWN)
206 			goto out;
207 
208 		if (sk->sk_state == TCP_CLOSE) {
209 			copied = -ENOTCONN;
210 			goto out;
211 		}
212 
213 		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
214 		if (!timeo) {
215 			copied = -EAGAIN;
216 			goto out;
217 		}
218 
219 		if (signal_pending(current)) {
220 			copied = sock_intr_errno(timeo);
221 			goto out;
222 		}
223 
224 		data = tcp_msg_wait_data(sk, psock, timeo);
225 		if (data && !sk_psock_queue_empty(psock))
226 			goto msg_bytes_ready;
227 		copied = -EAGAIN;
228 	}
229 out:
230 	release_sock(sk);
231 	sk_psock_put(sk, psock);
232 	return copied;
233 }
234 
235 static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
236 			   int flags, int *addr_len)
237 {
238 	struct sk_psock *psock;
239 	int copied, ret;
240 
241 	if (unlikely(flags & MSG_ERRQUEUE))
242 		return inet_recv_error(sk, msg, len, addr_len);
243 
244 	psock = sk_psock_get(sk);
245 	if (unlikely(!psock))
246 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
247 	if (!skb_queue_empty(&sk->sk_receive_queue) &&
248 	    sk_psock_queue_empty(psock)) {
249 		sk_psock_put(sk, psock);
250 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
251 	}
252 	lock_sock(sk);
253 msg_bytes_ready:
254 	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
255 	if (!copied) {
256 		long timeo;
257 		int data;
258 
259 		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
260 		data = tcp_msg_wait_data(sk, psock, timeo);
261 		if (data) {
262 			if (!sk_psock_queue_empty(psock))
263 				goto msg_bytes_ready;
264 			release_sock(sk);
265 			sk_psock_put(sk, psock);
266 			return tcp_recvmsg(sk, msg, len, flags, addr_len);
267 		}
268 		copied = -EAGAIN;
269 	}
270 	ret = copied;
271 	release_sock(sk);
272 	sk_psock_put(sk, psock);
273 	return ret;
274 }
275 
276 static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
277 				struct sk_msg *msg, int *copied, int flags)
278 {
279 	bool cork = false, enospc = sk_msg_full(msg);
280 	struct sock *sk_redir;
281 	u32 tosend, origsize, sent, delta = 0;
282 	u32 eval = __SK_NONE;
283 	int ret;
284 
285 more_data:
286 	if (psock->eval == __SK_NONE) {
287 		/* Track delta in msg size to add/subtract it on SK_DROP from
288 		 * returned to user copied size. This ensures user doesn't
289 		 * get a positive return code with msg_cut_data and SK_DROP
290 		 * verdict.
291 		 */
292 		delta = msg->sg.size;
293 		psock->eval = sk_psock_msg_verdict(sk, psock, msg);
294 		delta -= msg->sg.size;
295 	}
296 
297 	if (msg->cork_bytes &&
298 	    msg->cork_bytes > msg->sg.size && !enospc) {
299 		psock->cork_bytes = msg->cork_bytes - msg->sg.size;
300 		if (!psock->cork) {
301 			psock->cork = kzalloc(sizeof(*psock->cork),
302 					      GFP_ATOMIC | __GFP_NOWARN);
303 			if (!psock->cork)
304 				return -ENOMEM;
305 		}
306 		memcpy(psock->cork, msg, sizeof(*msg));
307 		return 0;
308 	}
309 
310 	tosend = msg->sg.size;
311 	if (psock->apply_bytes && psock->apply_bytes < tosend)
312 		tosend = psock->apply_bytes;
313 
314 	switch (psock->eval) {
315 	case __SK_PASS:
316 		ret = tcp_bpf_push(sk, msg, tosend, flags, true);
317 		if (unlikely(ret)) {
318 			*copied -= sk_msg_free(sk, msg);
319 			break;
320 		}
321 		sk_msg_apply_bytes(psock, tosend);
322 		break;
323 	case __SK_REDIRECT:
324 		sk_redir = psock->sk_redir;
325 		sk_msg_apply_bytes(psock, tosend);
326 		if (!psock->apply_bytes) {
327 			/* Clean up before releasing the sock lock. */
328 			eval = psock->eval;
329 			psock->eval = __SK_NONE;
330 			psock->sk_redir = NULL;
331 		}
332 		if (psock->cork) {
333 			cork = true;
334 			psock->cork = NULL;
335 		}
336 		sk_msg_return(sk, msg, tosend);
337 		release_sock(sk);
338 
339 		origsize = msg->sg.size;
340 		ret = tcp_bpf_sendmsg_redir(sk_redir, msg, tosend, flags);
341 		sent = origsize - msg->sg.size;
342 
343 		if (eval == __SK_REDIRECT)
344 			sock_put(sk_redir);
345 
346 		lock_sock(sk);
347 		if (unlikely(ret < 0)) {
348 			int free = sk_msg_free_nocharge(sk, msg);
349 
350 			if (!cork)
351 				*copied -= free;
352 		}
353 		if (cork) {
354 			sk_msg_free(sk, msg);
355 			kfree(msg);
356 			msg = NULL;
357 			ret = 0;
358 		}
359 		break;
360 	case __SK_DROP:
361 	default:
362 		sk_msg_free_partial(sk, msg, tosend);
363 		sk_msg_apply_bytes(psock, tosend);
364 		*copied -= (tosend + delta);
365 		return -EACCES;
366 	}
367 
368 	if (likely(!ret)) {
369 		if (!psock->apply_bytes) {
370 			psock->eval =  __SK_NONE;
371 			if (psock->sk_redir) {
372 				sock_put(psock->sk_redir);
373 				psock->sk_redir = NULL;
374 			}
375 		}
376 		if (msg &&
377 		    msg->sg.data[msg->sg.start].page_link &&
378 		    msg->sg.data[msg->sg.start].length) {
379 			if (eval == __SK_REDIRECT)
380 				sk_mem_charge(sk, tosend - sent);
381 			goto more_data;
382 		}
383 	}
384 	return ret;
385 }
386 
387 static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
388 {
389 	struct sk_msg tmp, *msg_tx = NULL;
390 	int copied = 0, err = 0;
391 	struct sk_psock *psock;
392 	long timeo;
393 	int flags;
394 
395 	/* Don't let internal do_tcp_sendpages() flags through */
396 	flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
397 	flags |= MSG_NO_SHARED_FRAGS;
398 
399 	psock = sk_psock_get(sk);
400 	if (unlikely(!psock))
401 		return tcp_sendmsg(sk, msg, size);
402 
403 	lock_sock(sk);
404 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
405 	while (msg_data_left(msg)) {
406 		bool enospc = false;
407 		u32 copy, osize;
408 
409 		if (sk->sk_err) {
410 			err = -sk->sk_err;
411 			goto out_err;
412 		}
413 
414 		copy = msg_data_left(msg);
415 		if (!sk_stream_memory_free(sk))
416 			goto wait_for_sndbuf;
417 		if (psock->cork) {
418 			msg_tx = psock->cork;
419 		} else {
420 			msg_tx = &tmp;
421 			sk_msg_init(msg_tx);
422 		}
423 
424 		osize = msg_tx->sg.size;
425 		err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
426 		if (err) {
427 			if (err != -ENOSPC)
428 				goto wait_for_memory;
429 			enospc = true;
430 			copy = msg_tx->sg.size - osize;
431 		}
432 
433 		err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
434 					       copy);
435 		if (err < 0) {
436 			sk_msg_trim(sk, msg_tx, osize);
437 			goto out_err;
438 		}
439 
440 		copied += copy;
441 		if (psock->cork_bytes) {
442 			if (size > psock->cork_bytes)
443 				psock->cork_bytes = 0;
444 			else
445 				psock->cork_bytes -= size;
446 			if (psock->cork_bytes && !enospc)
447 				goto out_err;
448 			/* All cork bytes are accounted, rerun the prog. */
449 			psock->eval = __SK_NONE;
450 			psock->cork_bytes = 0;
451 		}
452 
453 		err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
454 		if (unlikely(err < 0))
455 			goto out_err;
456 		continue;
457 wait_for_sndbuf:
458 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
459 wait_for_memory:
460 		err = sk_stream_wait_memory(sk, &timeo);
461 		if (err) {
462 			if (msg_tx && msg_tx != psock->cork)
463 				sk_msg_free(sk, msg_tx);
464 			goto out_err;
465 		}
466 	}
467 out_err:
468 	if (err < 0)
469 		err = sk_stream_error(sk, msg->msg_flags, err);
470 	release_sock(sk);
471 	sk_psock_put(sk, psock);
472 	return copied ? copied : err;
473 }
474 
475 static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
476 			    size_t size, int flags)
477 {
478 	struct sk_msg tmp, *msg = NULL;
479 	int err = 0, copied = 0;
480 	struct sk_psock *psock;
481 	bool enospc = false;
482 
483 	psock = sk_psock_get(sk);
484 	if (unlikely(!psock))
485 		return tcp_sendpage(sk, page, offset, size, flags);
486 
487 	lock_sock(sk);
488 	if (psock->cork) {
489 		msg = psock->cork;
490 	} else {
491 		msg = &tmp;
492 		sk_msg_init(msg);
493 	}
494 
495 	/* Catch case where ring is full and sendpage is stalled. */
496 	if (unlikely(sk_msg_full(msg)))
497 		goto out_err;
498 
499 	sk_msg_page_add(msg, page, size, offset);
500 	sk_mem_charge(sk, size);
501 	copied = size;
502 	if (sk_msg_full(msg))
503 		enospc = true;
504 	if (psock->cork_bytes) {
505 		if (size > psock->cork_bytes)
506 			psock->cork_bytes = 0;
507 		else
508 			psock->cork_bytes -= size;
509 		if (psock->cork_bytes && !enospc)
510 			goto out_err;
511 		/* All cork bytes are accounted, rerun the prog. */
512 		psock->eval = __SK_NONE;
513 		psock->cork_bytes = 0;
514 	}
515 
516 	err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags);
517 out_err:
518 	release_sock(sk);
519 	sk_psock_put(sk, psock);
520 	return copied ? copied : err;
521 }
522 
523 enum {
524 	TCP_BPF_IPV4,
525 	TCP_BPF_IPV6,
526 	TCP_BPF_NUM_PROTS,
527 };
528 
529 enum {
530 	TCP_BPF_BASE,
531 	TCP_BPF_TX,
532 	TCP_BPF_RX,
533 	TCP_BPF_TXRX,
534 	TCP_BPF_NUM_CFGS,
535 };
536 
537 static struct proto *tcpv6_prot_saved __read_mostly;
538 static DEFINE_SPINLOCK(tcpv6_prot_lock);
539 static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
540 
541 static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
542 				   struct proto *base)
543 {
544 	prot[TCP_BPF_BASE]			= *base;
545 	prot[TCP_BPF_BASE].destroy		= sock_map_destroy;
546 	prot[TCP_BPF_BASE].close		= sock_map_close;
547 	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
548 	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
549 
550 	prot[TCP_BPF_TX]			= prot[TCP_BPF_BASE];
551 	prot[TCP_BPF_TX].sendmsg		= tcp_bpf_sendmsg;
552 	prot[TCP_BPF_TX].sendpage		= tcp_bpf_sendpage;
553 
554 	prot[TCP_BPF_RX]			= prot[TCP_BPF_BASE];
555 	prot[TCP_BPF_RX].recvmsg		= tcp_bpf_recvmsg_parser;
556 
557 	prot[TCP_BPF_TXRX]			= prot[TCP_BPF_TX];
558 	prot[TCP_BPF_TXRX].recvmsg		= tcp_bpf_recvmsg_parser;
559 }
560 
561 static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
562 {
563 	if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
564 		spin_lock_bh(&tcpv6_prot_lock);
565 		if (likely(ops != tcpv6_prot_saved)) {
566 			tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
567 			smp_store_release(&tcpv6_prot_saved, ops);
568 		}
569 		spin_unlock_bh(&tcpv6_prot_lock);
570 	}
571 }
572 
573 static int __init tcp_bpf_v4_build_proto(void)
574 {
575 	tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
576 	return 0;
577 }
578 late_initcall(tcp_bpf_v4_build_proto);
579 
580 static int tcp_bpf_assert_proto_ops(struct proto *ops)
581 {
582 	/* In order to avoid retpoline, we make assumptions when we call
583 	 * into ops if e.g. a psock is not present. Make sure they are
584 	 * indeed valid assumptions.
585 	 */
586 	return ops->recvmsg  == tcp_recvmsg &&
587 	       ops->sendmsg  == tcp_sendmsg &&
588 	       ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
589 }
590 
591 int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
592 {
593 	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
594 	int config = psock->progs.msg_parser   ? TCP_BPF_TX   : TCP_BPF_BASE;
595 
596 	if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
597 		config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
598 	}
599 
600 	if (restore) {
601 		if (inet_csk_has_ulp(sk)) {
602 			/* TLS does not have an unhash proto in SW cases,
603 			 * but we need to ensure we stop using the sock_map
604 			 * unhash routine because the associated psock is being
605 			 * removed. So use the original unhash handler.
606 			 */
607 			WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
608 			tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
609 		} else {
610 			sk->sk_write_space = psock->saved_write_space;
611 			/* Pairs with lockless read in sk_clone_lock() */
612 			sock_replace_proto(sk, psock->sk_proto);
613 		}
614 		return 0;
615 	}
616 
617 	if (sk->sk_family == AF_INET6) {
618 		if (tcp_bpf_assert_proto_ops(psock->sk_proto))
619 			return -EINVAL;
620 
621 		tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
622 	}
623 
624 	/* Pairs with lockless read in sk_clone_lock() */
625 	sock_replace_proto(sk, &tcp_bpf_prots[family][config]);
626 	return 0;
627 }
628 EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
629 
630 /* If a child got cloned from a listening socket that had tcp_bpf
631  * protocol callbacks installed, we need to restore the callbacks to
632  * the default ones because the child does not inherit the psock state
633  * that tcp_bpf callbacks expect.
634  */
635 void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
636 {
637 	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
638 	struct proto *prot = newsk->sk_prot;
639 
640 	if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE])
641 		newsk->sk_prot = sk->sk_prot_creator;
642 }
643 #endif /* CONFIG_BPF_SYSCALL */
644