Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 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> |
John Fastabend | 0608c69 | 2018-12-20 11:35:35 -0800 | [diff] [blame] | 11 | #include <net/tls.h> |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 12 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 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; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 55 | sk_psock_queue_msg(psock, tmp); |
John Fastabend | 552de910 | 2018-12-20 11:35:33 -0800 | [diff] [blame] | 56 | sk_psock_data_ready(sk, psock); |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 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) { |
John Fastabend | 0608c69 | 2018-12-20 11:35:35 -0800 | [diff] [blame] | 76 | bool has_tx_ulp; |
| 77 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 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: |
John Fastabend | 0608c69 | 2018-12-20 11:35:35 -0800 | [diff] [blame] | 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 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 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 | sk_msg_free(sk, msg); |
| 143 | return 0; |
| 144 | } |
| 145 | ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) : |
| 146 | tcp_bpf_push_locked(sk, msg, bytes, flags, false); |
| 147 | sk_psock_put(sk, psock); |
| 148 | return ret; |
| 149 | } |
| 150 | EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir); |
| 151 | |
Cong Wang | 8875960 | 2021-02-23 10:49:26 -0800 | [diff] [blame] | 152 | #ifdef CONFIG_BPF_SYSCALL |
Jakub Kicinski | b6df007 | 2021-06-29 15:45:27 -0700 | [diff] [blame] | 153 | static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock, |
| 154 | long timeo) |
Cong Wang | 9f2470f | 2021-06-14 19:13:35 -0700 | [diff] [blame] | 155 | { |
| 156 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 157 | int ret = 0; |
| 158 | |
| 159 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 160 | return 1; |
| 161 | |
| 162 | if (!timeo) |
| 163 | return ret; |
| 164 | |
| 165 | add_wait_queue(sk_sleep(sk), &wait); |
| 166 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 167 | ret = sk_wait_event(sk, &timeo, |
| 168 | !list_empty(&psock->ingress_msg) || |
| 169 | !skb_queue_empty(&sk->sk_receive_queue), &wait); |
| 170 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 171 | remove_wait_queue(sk_sleep(sk), &wait); |
| 172 | return ret; |
| 173 | } |
| 174 | |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 175 | static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 176 | int nonblock, int flags, int *addr_len) |
| 177 | { |
| 178 | struct sk_psock *psock; |
| 179 | int copied, ret; |
| 180 | |
Xiyu Yang | 18f02ad | 2020-04-26 11:35:15 +0800 | [diff] [blame] | 181 | if (unlikely(flags & MSG_ERRQUEUE)) |
| 182 | return inet_recv_error(sk, msg, len, addr_len); |
| 183 | |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 184 | psock = sk_psock_get(sk); |
| 185 | if (unlikely(!psock)) |
| 186 | return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len); |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 187 | if (!skb_queue_empty(&sk->sk_receive_queue) && |
Xiyu Yang | 18f02ad | 2020-04-26 11:35:15 +0800 | [diff] [blame] | 188 | sk_psock_queue_empty(psock)) { |
| 189 | sk_psock_put(sk, psock); |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 190 | return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len); |
Xiyu Yang | 18f02ad | 2020-04-26 11:35:15 +0800 | [diff] [blame] | 191 | } |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 192 | lock_sock(sk); |
| 193 | msg_bytes_ready: |
Cong Wang | 2bc793e | 2021-03-30 19:32:33 -0700 | [diff] [blame] | 194 | copied = sk_msg_recvmsg(sk, psock, msg, len, flags); |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 195 | if (!copied) { |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 196 | long timeo; |
Cong Wang | c49661a | 2021-05-16 19:23:48 -0700 | [diff] [blame] | 197 | int data; |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 198 | |
| 199 | timeo = sock_rcvtimeo(sk, nonblock); |
Jakub Kicinski | b6df007 | 2021-06-29 15:45:27 -0700 | [diff] [blame] | 200 | data = tcp_msg_wait_data(sk, psock, timeo); |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 201 | if (data) { |
| 202 | if (!sk_psock_queue_empty(psock)) |
| 203 | goto msg_bytes_ready; |
| 204 | release_sock(sk); |
| 205 | sk_psock_put(sk, psock); |
| 206 | return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len); |
| 207 | } |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 208 | copied = -EAGAIN; |
| 209 | } |
| 210 | ret = copied; |
YueHaibing | c0fd336 | 2020-03-20 10:34:26 +0800 | [diff] [blame] | 211 | release_sock(sk); |
| 212 | sk_psock_put(sk, psock); |
| 213 | return ret; |
| 214 | } |
| 215 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 216 | static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock, |
| 217 | struct sk_msg *msg, int *copied, int flags) |
| 218 | { |
Jakub Kicinski | 031097d | 2019-11-27 12:16:41 -0800 | [diff] [blame] | 219 | bool cork = false, enospc = sk_msg_full(msg); |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 220 | struct sock *sk_redir; |
John Fastabend | 7246d8e | 2018-11-26 14:16:17 -0800 | [diff] [blame] | 221 | u32 tosend, delta = 0; |
Liu Jian | cd9733f | 2021-10-12 13:20:19 +0800 | [diff] [blame] | 222 | u32 eval = __SK_NONE; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 223 | int ret; |
| 224 | |
| 225 | more_data: |
John Fastabend | 7246d8e | 2018-11-26 14:16:17 -0800 | [diff] [blame] | 226 | if (psock->eval == __SK_NONE) { |
| 227 | /* Track delta in msg size to add/subtract it on SK_DROP from |
| 228 | * returned to user copied size. This ensures user doesn't |
| 229 | * get a positive return code with msg_cut_data and SK_DROP |
| 230 | * verdict. |
| 231 | */ |
| 232 | delta = msg->sg.size; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 233 | psock->eval = sk_psock_msg_verdict(sk, psock, msg); |
John Fastabend | 7361d44 | 2020-01-11 06:12:06 +0000 | [diff] [blame] | 234 | delta -= msg->sg.size; |
John Fastabend | 7246d8e | 2018-11-26 14:16:17 -0800 | [diff] [blame] | 235 | } |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 236 | |
| 237 | if (msg->cork_bytes && |
| 238 | msg->cork_bytes > msg->sg.size && !enospc) { |
| 239 | psock->cork_bytes = msg->cork_bytes - msg->sg.size; |
| 240 | if (!psock->cork) { |
| 241 | psock->cork = kzalloc(sizeof(*psock->cork), |
| 242 | GFP_ATOMIC | __GFP_NOWARN); |
| 243 | if (!psock->cork) |
| 244 | return -ENOMEM; |
| 245 | } |
| 246 | memcpy(psock->cork, msg, sizeof(*msg)); |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | tosend = msg->sg.size; |
| 251 | if (psock->apply_bytes && psock->apply_bytes < tosend) |
| 252 | tosend = psock->apply_bytes; |
| 253 | |
| 254 | switch (psock->eval) { |
| 255 | case __SK_PASS: |
| 256 | ret = tcp_bpf_push(sk, msg, tosend, flags, true); |
| 257 | if (unlikely(ret)) { |
| 258 | *copied -= sk_msg_free(sk, msg); |
| 259 | break; |
| 260 | } |
| 261 | sk_msg_apply_bytes(psock, tosend); |
| 262 | break; |
| 263 | case __SK_REDIRECT: |
| 264 | sk_redir = psock->sk_redir; |
| 265 | sk_msg_apply_bytes(psock, tosend); |
Liu Jian | cd9733f | 2021-10-12 13:20:19 +0800 | [diff] [blame] | 266 | if (!psock->apply_bytes) { |
| 267 | /* Clean up before releasing the sock lock. */ |
| 268 | eval = psock->eval; |
| 269 | psock->eval = __SK_NONE; |
| 270 | psock->sk_redir = NULL; |
| 271 | } |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 272 | if (psock->cork) { |
| 273 | cork = true; |
| 274 | psock->cork = NULL; |
| 275 | } |
| 276 | sk_msg_return(sk, msg, tosend); |
| 277 | release_sock(sk); |
Liu Jian | cd9733f | 2021-10-12 13:20:19 +0800 | [diff] [blame] | 278 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 279 | ret = tcp_bpf_sendmsg_redir(sk_redir, msg, tosend, flags); |
Liu Jian | cd9733f | 2021-10-12 13:20:19 +0800 | [diff] [blame] | 280 | |
| 281 | if (eval == __SK_REDIRECT) |
| 282 | sock_put(sk_redir); |
| 283 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 284 | lock_sock(sk); |
| 285 | if (unlikely(ret < 0)) { |
| 286 | int free = sk_msg_free_nocharge(sk, msg); |
| 287 | |
| 288 | if (!cork) |
| 289 | *copied -= free; |
| 290 | } |
| 291 | if (cork) { |
| 292 | sk_msg_free(sk, msg); |
| 293 | kfree(msg); |
| 294 | msg = NULL; |
| 295 | ret = 0; |
| 296 | } |
| 297 | break; |
| 298 | case __SK_DROP: |
| 299 | default: |
| 300 | sk_msg_free_partial(sk, msg, tosend); |
| 301 | sk_msg_apply_bytes(psock, tosend); |
John Fastabend | 7246d8e | 2018-11-26 14:16:17 -0800 | [diff] [blame] | 302 | *copied -= (tosend + delta); |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 303 | return -EACCES; |
| 304 | } |
| 305 | |
| 306 | if (likely(!ret)) { |
| 307 | if (!psock->apply_bytes) { |
| 308 | psock->eval = __SK_NONE; |
| 309 | if (psock->sk_redir) { |
| 310 | sock_put(psock->sk_redir); |
| 311 | psock->sk_redir = NULL; |
| 312 | } |
| 313 | } |
| 314 | if (msg && |
| 315 | msg->sg.data[msg->sg.start].page_link && |
| 316 | msg->sg.data[msg->sg.start].length) |
| 317 | goto more_data; |
| 318 | } |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) |
| 323 | { |
| 324 | struct sk_msg tmp, *msg_tx = NULL; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 325 | int copied = 0, err = 0; |
| 326 | struct sk_psock *psock; |
| 327 | long timeo; |
Jakub Kicinski | 4147766 | 2019-08-07 17:03:59 -0700 | [diff] [blame] | 328 | int flags; |
| 329 | |
| 330 | /* Don't let internal do_tcp_sendpages() flags through */ |
| 331 | flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED); |
| 332 | flags |= MSG_NO_SHARED_FRAGS; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 333 | |
| 334 | psock = sk_psock_get(sk); |
| 335 | if (unlikely(!psock)) |
| 336 | return tcp_sendmsg(sk, msg, size); |
| 337 | |
| 338 | lock_sock(sk); |
| 339 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 340 | while (msg_data_left(msg)) { |
| 341 | bool enospc = false; |
| 342 | u32 copy, osize; |
| 343 | |
| 344 | if (sk->sk_err) { |
| 345 | err = -sk->sk_err; |
| 346 | goto out_err; |
| 347 | } |
| 348 | |
| 349 | copy = msg_data_left(msg); |
| 350 | if (!sk_stream_memory_free(sk)) |
| 351 | goto wait_for_sndbuf; |
| 352 | if (psock->cork) { |
| 353 | msg_tx = psock->cork; |
| 354 | } else { |
| 355 | msg_tx = &tmp; |
| 356 | sk_msg_init(msg_tx); |
| 357 | } |
| 358 | |
| 359 | osize = msg_tx->sg.size; |
| 360 | err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1); |
| 361 | if (err) { |
| 362 | if (err != -ENOSPC) |
| 363 | goto wait_for_memory; |
| 364 | enospc = true; |
| 365 | copy = msg_tx->sg.size - osize; |
| 366 | } |
| 367 | |
| 368 | err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx, |
| 369 | copy); |
| 370 | if (err < 0) { |
| 371 | sk_msg_trim(sk, msg_tx, osize); |
| 372 | goto out_err; |
| 373 | } |
| 374 | |
| 375 | copied += copy; |
| 376 | if (psock->cork_bytes) { |
| 377 | if (size > psock->cork_bytes) |
| 378 | psock->cork_bytes = 0; |
| 379 | else |
| 380 | psock->cork_bytes -= size; |
| 381 | if (psock->cork_bytes && !enospc) |
| 382 | goto out_err; |
| 383 | /* All cork bytes are accounted, rerun the prog. */ |
| 384 | psock->eval = __SK_NONE; |
| 385 | psock->cork_bytes = 0; |
| 386 | } |
| 387 | |
| 388 | err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags); |
| 389 | if (unlikely(err < 0)) |
| 390 | goto out_err; |
| 391 | continue; |
| 392 | wait_for_sndbuf: |
| 393 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 394 | wait_for_memory: |
| 395 | err = sk_stream_wait_memory(sk, &timeo); |
| 396 | if (err) { |
| 397 | if (msg_tx && msg_tx != psock->cork) |
| 398 | sk_msg_free(sk, msg_tx); |
| 399 | goto out_err; |
| 400 | } |
| 401 | } |
| 402 | out_err: |
| 403 | if (err < 0) |
| 404 | err = sk_stream_error(sk, msg->msg_flags, err); |
| 405 | release_sock(sk); |
| 406 | sk_psock_put(sk, psock); |
| 407 | return copied ? copied : err; |
| 408 | } |
| 409 | |
| 410 | static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset, |
| 411 | size_t size, int flags) |
| 412 | { |
| 413 | struct sk_msg tmp, *msg = NULL; |
| 414 | int err = 0, copied = 0; |
| 415 | struct sk_psock *psock; |
| 416 | bool enospc = false; |
| 417 | |
| 418 | psock = sk_psock_get(sk); |
| 419 | if (unlikely(!psock)) |
| 420 | return tcp_sendpage(sk, page, offset, size, flags); |
| 421 | |
| 422 | lock_sock(sk); |
| 423 | if (psock->cork) { |
| 424 | msg = psock->cork; |
| 425 | } else { |
| 426 | msg = &tmp; |
| 427 | sk_msg_init(msg); |
| 428 | } |
| 429 | |
| 430 | /* Catch case where ring is full and sendpage is stalled. */ |
| 431 | if (unlikely(sk_msg_full(msg))) |
| 432 | goto out_err; |
| 433 | |
| 434 | sk_msg_page_add(msg, page, size, offset); |
| 435 | sk_mem_charge(sk, size); |
| 436 | copied = size; |
| 437 | if (sk_msg_full(msg)) |
| 438 | enospc = true; |
| 439 | if (psock->cork_bytes) { |
| 440 | if (size > psock->cork_bytes) |
| 441 | psock->cork_bytes = 0; |
| 442 | else |
| 443 | psock->cork_bytes -= size; |
| 444 | if (psock->cork_bytes && !enospc) |
| 445 | goto out_err; |
| 446 | /* All cork bytes are accounted, rerun the prog. */ |
| 447 | psock->eval = __SK_NONE; |
| 448 | psock->cork_bytes = 0; |
| 449 | } |
| 450 | |
| 451 | err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags); |
| 452 | out_err: |
| 453 | release_sock(sk); |
| 454 | sk_psock_put(sk, psock); |
| 455 | return copied ? copied : err; |
| 456 | } |
| 457 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 458 | enum { |
| 459 | TCP_BPF_IPV4, |
| 460 | TCP_BPF_IPV6, |
| 461 | TCP_BPF_NUM_PROTS, |
| 462 | }; |
| 463 | |
| 464 | enum { |
| 465 | TCP_BPF_BASE, |
| 466 | TCP_BPF_TX, |
| 467 | TCP_BPF_NUM_CFGS, |
| 468 | }; |
| 469 | |
| 470 | static struct proto *tcpv6_prot_saved __read_mostly; |
| 471 | static DEFINE_SPINLOCK(tcpv6_prot_lock); |
| 472 | static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS]; |
| 473 | |
| 474 | static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS], |
| 475 | struct proto *base) |
| 476 | { |
| 477 | prot[TCP_BPF_BASE] = *base; |
Lorenz Bauer | f747632 | 2020-03-09 11:12:36 +0000 | [diff] [blame] | 478 | prot[TCP_BPF_BASE].unhash = sock_map_unhash; |
| 479 | prot[TCP_BPF_BASE].close = sock_map_close; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 480 | prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg; |
Cong Wang | fb4e0a5 | 2021-10-08 13:33:04 -0700 | [diff] [blame] | 481 | prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 482 | |
| 483 | prot[TCP_BPF_TX] = prot[TCP_BPF_BASE]; |
| 484 | prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg; |
| 485 | prot[TCP_BPF_TX].sendpage = tcp_bpf_sendpage; |
| 486 | } |
| 487 | |
Lorenz Bauer | 7b219da | 2020-08-21 11:29:43 +0100 | [diff] [blame] | 488 | static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops) |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 489 | { |
Lorenz Bauer | 7b219da | 2020-08-21 11:29:43 +0100 | [diff] [blame] | 490 | if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) { |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 491 | spin_lock_bh(&tcpv6_prot_lock); |
| 492 | if (likely(ops != tcpv6_prot_saved)) { |
| 493 | tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops); |
| 494 | smp_store_release(&tcpv6_prot_saved, ops); |
| 495 | } |
| 496 | spin_unlock_bh(&tcpv6_prot_lock); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | static int __init tcp_bpf_v4_build_proto(void) |
| 501 | { |
| 502 | tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot); |
| 503 | return 0; |
| 504 | } |
John Fastabend | 228a4a7 | 2021-07-12 12:55:46 -0700 | [diff] [blame] | 505 | late_initcall(tcp_bpf_v4_build_proto); |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 506 | |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 507 | static int tcp_bpf_assert_proto_ops(struct proto *ops) |
| 508 | { |
| 509 | /* In order to avoid retpoline, we make assumptions when we call |
| 510 | * into ops if e.g. a psock is not present. Make sure they are |
| 511 | * indeed valid assumptions. |
| 512 | */ |
| 513 | return ops->recvmsg == tcp_recvmsg && |
| 514 | ops->sendmsg == tcp_sendmsg && |
| 515 | ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP; |
| 516 | } |
| 517 | |
Cong Wang | 51e0158 | 2021-04-06 20:21:11 -0700 | [diff] [blame] | 518 | int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 519 | { |
Lorenz Bauer | d19da36 | 2020-03-09 11:12:34 +0000 | [diff] [blame] | 520 | int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4; |
| 521 | int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 522 | |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 523 | if (restore) { |
| 524 | if (inet_csk_has_ulp(sk)) { |
Jakub Kicinski | 8859a44 | 2021-04-09 20:46:01 -0700 | [diff] [blame] | 525 | /* TLS does not have an unhash proto in SW cases, |
| 526 | * but we need to ensure we stop using the sock_map |
| 527 | * unhash routine because the associated psock is being |
| 528 | * removed. So use the original unhash handler. |
| 529 | */ |
| 530 | WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash); |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 531 | tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space); |
| 532 | } else { |
| 533 | sk->sk_write_space = psock->saved_write_space; |
| 534 | /* Pairs with lockless read in sk_clone_lock() */ |
| 535 | WRITE_ONCE(sk->sk_prot, psock->sk_proto); |
| 536 | } |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | if (inet_csk_has_ulp(sk)) |
| 541 | return -EINVAL; |
| 542 | |
Lorenz Bauer | 7b219da | 2020-08-21 11:29:43 +0100 | [diff] [blame] | 543 | if (sk->sk_family == AF_INET6) { |
| 544 | if (tcp_bpf_assert_proto_ops(psock->sk_proto)) |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 545 | return -EINVAL; |
Lorenz Bauer | d19da36 | 2020-03-09 11:12:34 +0000 | [diff] [blame] | 546 | |
Lorenz Bauer | 7b219da | 2020-08-21 11:29:43 +0100 | [diff] [blame] | 547 | tcp_bpf_check_v6_needs_rebuild(psock->sk_proto); |
Lorenz Bauer | d19da36 | 2020-03-09 11:12:34 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 550 | /* Pairs with lockless read in sk_clone_lock() */ |
| 551 | WRITE_ONCE(sk->sk_prot, &tcp_bpf_prots[family][config]); |
| 552 | return 0; |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 553 | } |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 554 | EXPORT_SYMBOL_GPL(tcp_bpf_update_proto); |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 555 | |
Jakub Sitnicki | e802515 | 2020-02-18 17:10:15 +0000 | [diff] [blame] | 556 | /* If a child got cloned from a listening socket that had tcp_bpf |
| 557 | * protocol callbacks installed, we need to restore the callbacks to |
| 558 | * the default ones because the child does not inherit the psock state |
| 559 | * that tcp_bpf callbacks expect. |
| 560 | */ |
| 561 | void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) |
| 562 | { |
| 563 | int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4; |
| 564 | struct proto *prot = newsk->sk_prot; |
| 565 | |
| 566 | if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE]) |
| 567 | newsk->sk_prot = sk->sk_prot_creator; |
| 568 | } |
Cong Wang | 8875960 | 2021-02-23 10:49:26 -0800 | [diff] [blame] | 569 | #endif /* CONFIG_BPF_SYSCALL */ |