1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Maintain an RxRPC server socket to do AFS communications through 3 * 4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/slab.h> 9 #include <linux/sched/signal.h> 10 11 #include <net/sock.h> 12 #include <net/af_rxrpc.h> 13 #include "internal.h" 14 #include "afs_cm.h" 15 #include "protocol_yfs.h" 16 #define RXRPC_TRACE_ONLY_DEFINE_ENUMS 17 #include <trace/events/rxrpc.h> 18 19 struct workqueue_struct *afs_async_calls; 20 21 static void afs_deferred_free_worker(struct work_struct *work); 22 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long); 23 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long); 24 static void afs_process_async_call(struct work_struct *); 25 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long); 26 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long); 27 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID); 28 static void afs_rx_notify_oob(struct sock *sk, struct sk_buff *oob); 29 static int afs_deliver_cm_op_id(struct afs_call *); 30 31 static const struct rxrpc_kernel_ops afs_rxrpc_callback_ops = { 32 .notify_new_call = afs_rx_new_call, 33 .discard_new_call = afs_rx_discard_new_call, 34 .user_attach_call = afs_rx_attach, 35 .notify_oob = afs_rx_notify_oob, 36 }; 37 38 /* asynchronous incoming call initial processing */ 39 static const struct afs_call_type afs_RXCMxxxx = { 40 .name = "CB.xxxx", 41 .deliver = afs_deliver_cm_op_id, 42 }; 43 44 /* 45 * open an RxRPC socket and bind it to be a server for callback notifications 46 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT 47 */ 48 int afs_open_socket(struct afs_net *net) 49 { 50 struct sockaddr_rxrpc srx; 51 struct socket *socket; 52 int ret; 53 54 _enter(""); 55 56 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket); 57 if (ret < 0) 58 goto error_1; 59 60 socket->sk->sk_allocation = GFP_NOFS; 61 socket->sk->sk_user_data = net; 62 63 /* bind the callback manager's address to make this a server socket */ 64 memset(&srx, 0, sizeof(srx)); 65 srx.srx_family = AF_RXRPC; 66 srx.srx_service = CM_SERVICE; 67 srx.transport_type = SOCK_DGRAM; 68 srx.transport_len = sizeof(srx.transport.sin6); 69 srx.transport.sin6.sin6_family = AF_INET6; 70 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT); 71 72 ret = rxrpc_sock_set_min_security_level(socket->sk, 73 RXRPC_SECURITY_ENCRYPT); 74 if (ret < 0) 75 goto error_2; 76 77 ret = rxrpc_sock_set_manage_response(socket->sk, true); 78 if (ret < 0) 79 goto error_2; 80 81 ret = afs_create_token_key(net, socket); 82 if (ret < 0) 83 pr_err("Couldn't create RxGK CM key: %d\n", ret); 84 85 ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx)); 86 if (ret == -EADDRINUSE) { 87 srx.transport.sin6.sin6_port = 0; 88 ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx)); 89 } 90 if (ret < 0) 91 goto error_2; 92 93 srx.srx_service = YFS_CM_SERVICE; 94 ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx)); 95 if (ret < 0) 96 goto error_2; 97 98 /* Ideally, we'd turn on service upgrade here, but we can't because 99 * OpenAFS is buggy and leaks the userStatus field from packet to 100 * packet and between FS packets and CB packets - so if we try to do an 101 * upgrade on an FS packet, OpenAFS will leak that into the CB packet 102 * it sends back to us. 103 */ 104 105 rxrpc_kernel_set_notifications(socket, &afs_rxrpc_callback_ops); 106 107 ret = kernel_listen(socket, INT_MAX); 108 if (ret < 0) 109 goto error_2; 110 111 net->socket = socket; 112 afs_charge_preallocation(&net->charge_preallocation_work); 113 _leave(" = 0"); 114 return 0; 115 116 error_2: 117 sock_release(socket); 118 error_1: 119 _leave(" = %d", ret); 120 return ret; 121 } 122 123 /* 124 * close the RxRPC socket AFS was using 125 */ 126 void afs_close_socket(struct afs_net *net) 127 { 128 _enter(""); 129 130 cancel_work_sync(&net->charge_preallocation_work); 131 kernel_listen(net->socket, 0); 132 flush_workqueue(afs_async_calls); 133 134 if (net->spare_incoming_call) { 135 afs_put_call(net->spare_incoming_call); 136 net->spare_incoming_call = NULL; 137 } 138 139 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls)); 140 wait_var_event(&net->nr_outstanding_calls, 141 !atomic_read(&net->nr_outstanding_calls)); 142 _debug("no outstanding calls"); 143 144 kernel_sock_shutdown(net->socket, SHUT_RDWR); 145 flush_workqueue(afs_async_calls); 146 net->socket->sk->sk_user_data = NULL; 147 sock_release(net->socket); 148 key_put(net->fs_cm_token_key); 149 150 _debug("dework"); 151 _leave(""); 152 } 153 154 /* 155 * Allocate a call. 156 */ 157 static struct afs_call *afs_alloc_call(struct afs_net *net, 158 const struct afs_call_type *type, 159 gfp_t gfp) 160 { 161 struct afs_call *call; 162 int o; 163 164 call = kzalloc_obj(*call, gfp); 165 if (!call) 166 return NULL; 167 168 call->type = type; 169 call->net = net; 170 call->debug_id = atomic_inc_return(&rxrpc_debug_id); 171 refcount_set(&call->ref, 1); 172 INIT_WORK(&call->async_work, type->async_rx ?: afs_process_async_call); 173 INIT_WORK(&call->work, call->type->work); 174 INIT_WORK(&call->free_work, afs_deferred_free_worker); 175 init_waitqueue_head(&call->waitq); 176 spin_lock_init(&call->state_lock); 177 call->iter = &call->def_iter; 178 179 o = atomic_inc_return(&net->nr_outstanding_calls); 180 trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o, 181 __builtin_return_address(0)); 182 return call; 183 } 184 185 static void afs_free_call(struct afs_call *call) 186 { 187 struct afs_net *net = call->net; 188 int o; 189 190 ASSERT(!work_pending(&call->async_work)); 191 192 rxrpc_kernel_put_peer(call->peer); 193 194 if (call->rxcall) { 195 rxrpc_kernel_shutdown_call(net->socket, call->rxcall); 196 rxrpc_kernel_put_call(net->socket, call->rxcall); 197 call->rxcall = NULL; 198 } 199 if (call->type->destructor) 200 call->type->destructor(call); 201 202 afs_unuse_server_notime(call->net, call->server, afs_server_trace_unuse_call); 203 kfree(call->request); 204 205 o = atomic_read(&net->nr_outstanding_calls); 206 trace_afs_call(call->debug_id, afs_call_trace_free, 0, o, 207 __builtin_return_address(0)); 208 kfree(call); 209 210 o = atomic_dec_return(&net->nr_outstanding_calls); 211 if (o == 0) 212 wake_up_var(&net->nr_outstanding_calls); 213 } 214 215 /* 216 * Dispose of a reference on a call. 217 */ 218 void afs_put_call(struct afs_call *call) 219 { 220 struct afs_net *net = call->net; 221 unsigned int debug_id = call->debug_id; 222 bool zero; 223 int r, o; 224 225 zero = __refcount_dec_and_test(&call->ref, &r); 226 o = atomic_read(&net->nr_outstanding_calls); 227 trace_afs_call(debug_id, afs_call_trace_put, r - 1, o, 228 __builtin_return_address(0)); 229 if (zero) 230 afs_free_call(call); 231 } 232 233 static void afs_deferred_free_worker(struct work_struct *work) 234 { 235 struct afs_call *call = container_of(work, struct afs_call, free_work); 236 237 afs_free_call(call); 238 } 239 240 /* 241 * Dispose of a reference on a call, deferring the cleanup to a workqueue 242 * to avoid lock recursion. 243 */ 244 void afs_deferred_put_call(struct afs_call *call) 245 { 246 struct afs_net *net = call->net; 247 unsigned int debug_id = call->debug_id; 248 bool zero; 249 int r, o; 250 251 zero = __refcount_dec_and_test(&call->ref, &r); 252 o = atomic_read(&net->nr_outstanding_calls); 253 trace_afs_call(debug_id, afs_call_trace_put, r - 1, o, 254 __builtin_return_address(0)); 255 if (zero) 256 schedule_work(&call->free_work); 257 } 258 259 /* 260 * Queue the call for actual work. 261 */ 262 static void afs_queue_call_work(struct afs_call *call) 263 { 264 if (call->type->work) { 265 afs_get_call(call, afs_call_trace_work); 266 if (!queue_work(afs_wq, &call->work)) 267 afs_put_call(call); 268 } 269 } 270 271 /* 272 * allocate a call with flat request and reply buffers 273 */ 274 struct afs_call *afs_alloc_flat_call(struct afs_net *net, 275 const struct afs_call_type *type, 276 size_t request_size, size_t reply_max) 277 { 278 struct afs_call *call; 279 280 call = afs_alloc_call(net, type, GFP_NOFS); 281 if (!call) 282 goto nomem_call; 283 284 if (request_size) { 285 call->request_size = request_size; 286 call->request = kmalloc(request_size, GFP_NOFS); 287 if (!call->request) 288 goto nomem_free; 289 } 290 291 if (reply_max) { 292 call->reply_max = reply_max; 293 call->buffer = kmalloc(reply_max, GFP_NOFS); 294 if (!call->buffer) 295 goto nomem_free; 296 } 297 298 afs_extract_to_buf(call, call->reply_max); 299 call->operation_ID = type->op; 300 init_waitqueue_head(&call->waitq); 301 return call; 302 303 nomem_free: 304 afs_put_call(call); 305 nomem_call: 306 return NULL; 307 } 308 309 /* 310 * clean up a call with flat buffer 311 */ 312 void afs_flat_call_destructor(struct afs_call *call) 313 { 314 _enter(""); 315 316 kfree(call->request); 317 call->request = NULL; 318 kfree(call->buffer); 319 call->buffer = NULL; 320 } 321 322 /* 323 * Advance the AFS call state when the RxRPC call ends the transmit phase. 324 */ 325 static void afs_notify_end_request_tx(struct sock *sock, 326 struct rxrpc_call *rxcall, 327 unsigned long call_user_ID) 328 { 329 struct afs_call *call = (struct afs_call *)call_user_ID; 330 331 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY); 332 } 333 334 /* 335 * Initiate a call and synchronously queue up the parameters for dispatch. Any 336 * error is stored into the call struct, which the caller must check for. 337 */ 338 void afs_make_call(struct afs_call *call, gfp_t gfp) 339 { 340 struct rxrpc_call *rxcall; 341 struct msghdr msg; 342 struct kvec iov[1]; 343 size_t len; 344 s64 tx_total_len; 345 int ret; 346 347 _enter(",{%pISp+%u},", rxrpc_kernel_remote_addr(call->peer), call->service_id); 348 349 ASSERT(call->type != NULL); 350 ASSERT(call->type->name != NULL); 351 352 _debug("____MAKE %p{%s,%x} [%d]____", 353 call, call->type->name, key_serial(call->key), 354 atomic_read(&call->net->nr_outstanding_calls)); 355 356 trace_afs_make_call(call); 357 358 /* Work out the length we're going to transmit. This is awkward for 359 * calls such as FS.StoreData where there's an extra injection of data 360 * after the initial fixed part. 361 */ 362 tx_total_len = call->request_size; 363 if (call->write_iter) 364 tx_total_len += iov_iter_count(call->write_iter); 365 366 /* If the call is going to be asynchronous, we need an extra ref for 367 * the call to hold itself so the caller need not hang on to its ref. 368 */ 369 if (call->async) { 370 afs_get_call(call, afs_call_trace_get); 371 call->drop_ref = true; 372 } 373 374 /* create a call */ 375 rxcall = rxrpc_kernel_begin_call(call->net->socket, call->peer, call->key, 376 (unsigned long)call, 377 tx_total_len, 378 call->max_lifespan, 379 gfp, 380 (call->async ? 381 afs_wake_up_async_call : 382 afs_wake_up_call_waiter), 383 call->service_id, 384 call->upgrade, 385 (call->intr ? RXRPC_PREINTERRUPTIBLE : 386 RXRPC_UNINTERRUPTIBLE), 387 call->debug_id); 388 if (IS_ERR(rxcall)) { 389 ret = PTR_ERR(rxcall); 390 call->error = ret; 391 goto error_kill_call; 392 } 393 394 call->rxcall = rxcall; 395 call->issue_time = ktime_get_real(); 396 397 /* send the request */ 398 iov[0].iov_base = call->request; 399 iov[0].iov_len = call->request_size; 400 401 msg.msg_name = NULL; 402 msg.msg_namelen = 0; 403 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, call->request_size); 404 msg.msg_control = NULL; 405 msg.msg_controllen = 0; 406 msg.msg_flags = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0); 407 408 ret = rxrpc_kernel_send_data(call->net->socket, rxcall, 409 &msg, call->request_size, 410 afs_notify_end_request_tx); 411 if (ret < 0) 412 goto error_do_abort; 413 414 if (call->write_iter) { 415 msg.msg_iter = *call->write_iter; 416 msg.msg_flags &= ~MSG_MORE; 417 trace_afs_send_data(call, &msg); 418 419 ret = rxrpc_kernel_send_data(call->net->socket, 420 call->rxcall, &msg, 421 iov_iter_count(&msg.msg_iter), 422 afs_notify_end_request_tx); 423 *call->write_iter = msg.msg_iter; 424 425 trace_afs_sent_data(call, &msg, ret); 426 if (ret < 0) 427 goto error_do_abort; 428 } 429 430 /* Note that at this point, we may have received the reply or an abort 431 * - and an asynchronous call may already have completed. 432 * 433 * afs_wait_for_call_to_complete(call) 434 * must be called to synchronously clean up. 435 */ 436 return; 437 438 error_do_abort: 439 if (ret != -ECONNABORTED) 440 rxrpc_kernel_abort_call(call->net->socket, rxcall, 441 RX_USER_ABORT, ret, 442 afs_abort_send_data_error); 443 if (call->async) { 444 afs_see_call(call, afs_call_trace_async_abort); 445 return; 446 } 447 448 if (ret == -ECONNABORTED) { 449 len = 0; 450 iov_iter_kvec(&msg.msg_iter, ITER_DEST, NULL, 0, 0); 451 rxrpc_kernel_recv_data(call->net->socket, rxcall, 452 &msg.msg_iter, &len, false, 453 &call->abort_code, &call->service_id); 454 call->responded = true; 455 } 456 call->error = ret; 457 trace_afs_call_done(call); 458 error_kill_call: 459 if (call->async) 460 afs_see_call(call, afs_call_trace_async_kill); 461 if (call->type->immediate_cancel) 462 call->type->immediate_cancel(call); 463 464 /* We need to dispose of the extra ref we grabbed for an async call. 465 * The call, however, might be queued on afs_async_calls and we need to 466 * make sure we don't get any more notifications that might requeue it. 467 */ 468 if (call->rxcall) 469 rxrpc_kernel_shutdown_call(call->net->socket, call->rxcall); 470 if (call->async) { 471 if (cancel_work_sync(&call->async_work)) 472 afs_put_call(call); 473 afs_set_call_complete(call, ret, 0); 474 } 475 476 call->error = ret; 477 call->state = AFS_CALL_COMPLETE; 478 _leave(" = %d", ret); 479 } 480 481 /* 482 * Log remote abort codes that indicate that we have a protocol disagreement 483 * with the server. 484 */ 485 static void afs_log_error(struct afs_call *call, s32 remote_abort) 486 { 487 static int max = 0; 488 const char *msg; 489 int m; 490 491 switch (remote_abort) { 492 case RX_EOF: msg = "unexpected EOF"; break; 493 case RXGEN_CC_MARSHAL: msg = "client marshalling"; break; 494 case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling"; break; 495 case RXGEN_SS_MARSHAL: msg = "server marshalling"; break; 496 case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling"; break; 497 case RXGEN_DECODE: msg = "opcode decode"; break; 498 case RXGEN_SS_XDRFREE: msg = "server XDR cleanup"; break; 499 case RXGEN_CC_XDRFREE: msg = "client XDR cleanup"; break; 500 case -32: msg = "insufficient data"; break; 501 default: 502 return; 503 } 504 505 m = max; 506 if (m < 3) { 507 max = m + 1; 508 pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n", 509 msg, call->type->name, 510 rxrpc_kernel_remote_addr(call->peer)); 511 } 512 } 513 514 /* 515 * deliver messages to a call 516 */ 517 void afs_deliver_to_call(struct afs_call *call) 518 { 519 enum afs_call_state state; 520 size_t len; 521 u32 abort_code, remote_abort = 0; 522 int ret; 523 524 _enter("%s", call->type->name); 525 526 while (state = READ_ONCE(call->state), 527 state == AFS_CALL_CL_AWAIT_REPLY || 528 state == AFS_CALL_SV_AWAIT_OP_ID || 529 state == AFS_CALL_SV_AWAIT_REQUEST || 530 state == AFS_CALL_SV_AWAIT_ACK 531 ) { 532 if (state == AFS_CALL_SV_AWAIT_ACK) { 533 len = 0; 534 iov_iter_kvec(&call->def_iter, ITER_DEST, NULL, 0, 0); 535 ret = rxrpc_kernel_recv_data(call->net->socket, 536 call->rxcall, &call->def_iter, 537 &len, false, &remote_abort, 538 &call->service_id); 539 trace_afs_receive_data(call, &call->def_iter, false, ret); 540 541 if (ret == -EINPROGRESS || ret == -EAGAIN) 542 return; 543 if (ret < 0 || ret == 1) { 544 if (ret == 1) 545 ret = 0; 546 goto call_complete; 547 } 548 return; 549 } 550 551 ret = call->type->deliver(call); 552 state = READ_ONCE(call->state); 553 if (ret == 0 && call->unmarshalling_error) 554 ret = -EBADMSG; 555 switch (ret) { 556 case 0: 557 call->responded = true; 558 afs_queue_call_work(call); 559 if (state == AFS_CALL_CL_PROC_REPLY) { 560 if (call->op) 561 set_bit(AFS_SERVER_FL_MAY_HAVE_CB, 562 &call->op->server->flags); 563 goto call_complete; 564 } 565 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY); 566 goto done; 567 case -EINPROGRESS: 568 case -EAGAIN: 569 goto out; 570 case -ECONNABORTED: 571 ASSERTCMP(state, ==, AFS_CALL_COMPLETE); 572 call->responded = true; 573 afs_log_error(call, call->abort_code); 574 goto done; 575 case -ENOTSUPP: 576 call->responded = true; 577 abort_code = RXGEN_OPCODE; 578 rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 579 abort_code, ret, 580 afs_abort_op_not_supported); 581 goto local_abort; 582 case -EIO: 583 pr_err("kAFS: Call %u in bad state %u\n", 584 call->debug_id, state); 585 fallthrough; 586 case -ENODATA: 587 case -EBADMSG: 588 case -EMSGSIZE: 589 case -ENOMEM: 590 case -EFAULT: 591 abort_code = RXGEN_CC_UNMARSHAL; 592 if (state != AFS_CALL_CL_AWAIT_REPLY) 593 abort_code = RXGEN_SS_UNMARSHAL; 594 rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 595 abort_code, ret, 596 afs_abort_unmarshal_error); 597 goto local_abort; 598 default: 599 abort_code = RX_CALL_DEAD; 600 rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 601 abort_code, ret, 602 afs_abort_general_error); 603 goto local_abort; 604 } 605 } 606 607 done: 608 if (call->type->done) 609 call->type->done(call); 610 out: 611 _leave(""); 612 return; 613 614 local_abort: 615 abort_code = 0; 616 call_complete: 617 afs_set_call_complete(call, ret, remote_abort); 618 goto done; 619 } 620 621 /* 622 * Wait synchronously for a call to complete. 623 */ 624 void afs_wait_for_call_to_complete(struct afs_call *call) 625 { 626 bool rxrpc_complete = false; 627 628 _enter(""); 629 630 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) { 631 DECLARE_WAITQUEUE(myself, current); 632 633 add_wait_queue(&call->waitq, &myself); 634 for (;;) { 635 set_current_state(TASK_UNINTERRUPTIBLE); 636 637 /* deliver any messages that are in the queue */ 638 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) && 639 call->need_attention) { 640 call->need_attention = false; 641 __set_current_state(TASK_RUNNING); 642 afs_deliver_to_call(call); 643 continue; 644 } 645 646 if (afs_check_call_state(call, AFS_CALL_COMPLETE)) 647 break; 648 649 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) { 650 /* rxrpc terminated the call. */ 651 rxrpc_complete = true; 652 break; 653 } 654 655 schedule(); 656 } 657 658 remove_wait_queue(&call->waitq, &myself); 659 __set_current_state(TASK_RUNNING); 660 } 661 662 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) { 663 if (rxrpc_complete) { 664 afs_set_call_complete(call, call->error, call->abort_code); 665 } else { 666 /* Kill off the call if it's still live. */ 667 _debug("call interrupted"); 668 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 669 RX_USER_ABORT, -EINTR, 670 afs_abort_interrupted)) 671 afs_set_call_complete(call, -EINTR, 0); 672 } 673 } 674 } 675 676 /* 677 * wake up a waiting call 678 */ 679 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, 680 unsigned long call_user_ID) 681 { 682 struct afs_call *call = (struct afs_call *)call_user_ID; 683 684 call->need_attention = true; 685 wake_up(&call->waitq); 686 } 687 688 /* 689 * Wake up an asynchronous call. The caller is holding the call notify 690 * spinlock around this, so we can't call afs_put_call(). 691 */ 692 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, 693 unsigned long call_user_ID) 694 { 695 struct afs_call *call = (struct afs_call *)call_user_ID; 696 int r; 697 698 trace_afs_notify_call(rxcall, call); 699 call->need_attention = true; 700 701 if (__refcount_inc_not_zero(&call->ref, &r)) { 702 trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1, 703 atomic_read(&call->net->nr_outstanding_calls), 704 __builtin_return_address(0)); 705 706 if (!queue_work(afs_async_calls, &call->async_work)) 707 afs_deferred_put_call(call); 708 } 709 } 710 711 /* 712 * Perform I/O processing on an asynchronous call. The work item carries a ref 713 * to the call struct that we either need to release or to pass on. 714 */ 715 static void afs_process_async_call(struct work_struct *work) 716 { 717 struct afs_call *call = container_of(work, struct afs_call, async_work); 718 719 _enter(""); 720 721 if (call->state < AFS_CALL_COMPLETE && call->need_attention) { 722 call->need_attention = false; 723 afs_deliver_to_call(call); 724 } 725 726 afs_put_call(call); 727 _leave(""); 728 } 729 730 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) 731 { 732 struct afs_call *call = (struct afs_call *)user_call_ID; 733 734 call->rxcall = rxcall; 735 } 736 737 /* 738 * Charge the incoming call preallocation. 739 */ 740 void afs_charge_preallocation(struct work_struct *work) 741 { 742 struct afs_net *net = 743 container_of(work, struct afs_net, charge_preallocation_work); 744 struct afs_call *call = net->spare_incoming_call; 745 746 while (READ_ONCE(net->live)) { 747 if (!call) { 748 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL); 749 if (!call) 750 break; 751 752 call->drop_ref = true; 753 call->async = true; 754 call->state = AFS_CALL_SV_AWAIT_OP_ID; 755 init_waitqueue_head(&call->waitq); 756 afs_extract_to_tmp(call); 757 } 758 759 if (rxrpc_kernel_charge_accept(net->socket, 760 afs_wake_up_async_call, 761 (unsigned long)call, 762 GFP_KERNEL, 763 call->debug_id) < 0) 764 break; 765 call = NULL; 766 } 767 net->spare_incoming_call = call; 768 } 769 770 /* 771 * Discard a preallocated call when a socket is shut down. 772 */ 773 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, 774 unsigned long user_call_ID) 775 { 776 struct afs_call *call = (struct afs_call *)user_call_ID; 777 778 call->rxcall = NULL; 779 afs_put_call(call); 780 } 781 782 /* 783 * Notification of an incoming call. 784 */ 785 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, 786 unsigned long user_call_ID) 787 { 788 struct afs_call *call = (struct afs_call *)user_call_ID; 789 struct afs_net *net = afs_sock2net(sk); 790 791 call->peer = rxrpc_kernel_get_call_peer(sk->sk_socket, call->rxcall); 792 call->server = afs_find_server(call->peer); 793 if (!call->server) 794 trace_afs_cm_no_server(call, rxrpc_kernel_remote_srx(call->peer)); 795 796 if (net->live) 797 queue_work(afs_wq, &net->charge_preallocation_work); 798 } 799 800 /* 801 * Grab the operation ID from an incoming cache manager call. The socket 802 * buffer is discarded on error or if we don't yet have sufficient data. 803 */ 804 static int afs_deliver_cm_op_id(struct afs_call *call) 805 { 806 int ret; 807 808 _enter("{%zu}", iov_iter_count(call->iter)); 809 810 /* the operation ID forms the first four bytes of the request data */ 811 ret = afs_extract_data(call, true); 812 if (ret < 0) 813 return ret; 814 815 call->operation_ID = ntohl(call->tmp); 816 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST); 817 818 /* ask the cache manager to route the call (it'll change the call type 819 * if successful) */ 820 if (!afs_cm_incoming_call(call)) 821 return -ENOTSUPP; 822 823 call->security_ix = rxrpc_kernel_query_call_security(call->rxcall, 824 &call->service_id, 825 &call->enctype); 826 827 trace_afs_cb_call(call); 828 call->work.func = call->type->work; 829 830 /* pass responsibility for the remainder of this message off to the 831 * cache manager op */ 832 return call->type->deliver(call); 833 } 834 835 /* 836 * Advance the AFS call state when an RxRPC service call ends the transmit 837 * phase. 838 */ 839 static void afs_notify_end_reply_tx(struct sock *sock, 840 struct rxrpc_call *rxcall, 841 unsigned long call_user_ID) 842 { 843 struct afs_call *call = (struct afs_call *)call_user_ID; 844 845 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK); 846 } 847 848 /* 849 * send an empty reply 850 */ 851 void afs_send_empty_reply(struct afs_call *call) 852 { 853 struct afs_net *net = call->net; 854 struct msghdr msg; 855 856 _enter(""); 857 858 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0); 859 860 msg.msg_name = NULL; 861 msg.msg_namelen = 0; 862 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0); 863 msg.msg_control = NULL; 864 msg.msg_controllen = 0; 865 msg.msg_flags = 0; 866 867 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0, 868 afs_notify_end_reply_tx)) { 869 case 0: 870 _leave(" [replied]"); 871 return; 872 873 case -ENOMEM: 874 _debug("oom"); 875 rxrpc_kernel_abort_call(net->socket, call->rxcall, 876 RXGEN_SS_MARSHAL, -ENOMEM, 877 afs_abort_oom); 878 fallthrough; 879 default: 880 _leave(" [error]"); 881 return; 882 } 883 } 884 885 /* 886 * send a simple reply 887 */ 888 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) 889 { 890 struct afs_net *net = call->net; 891 struct msghdr msg; 892 struct kvec iov[1]; 893 int n; 894 895 _enter(""); 896 897 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len); 898 899 iov[0].iov_base = (void *) buf; 900 iov[0].iov_len = len; 901 msg.msg_name = NULL; 902 msg.msg_namelen = 0; 903 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, len); 904 msg.msg_control = NULL; 905 msg.msg_controllen = 0; 906 msg.msg_flags = 0; 907 908 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len, 909 afs_notify_end_reply_tx); 910 if (n >= 0) { 911 /* Success */ 912 _leave(" [replied]"); 913 return; 914 } 915 916 if (n == -ENOMEM) { 917 _debug("oom"); 918 rxrpc_kernel_abort_call(net->socket, call->rxcall, 919 RXGEN_SS_MARSHAL, -ENOMEM, 920 afs_abort_oom); 921 } 922 _leave(" [error]"); 923 } 924 925 /* 926 * Extract a piece of data from the received data socket buffers. 927 */ 928 int afs_extract_data(struct afs_call *call, bool want_more) 929 { 930 struct afs_net *net = call->net; 931 struct iov_iter *iter = call->iter; 932 enum afs_call_state state; 933 u32 remote_abort = 0; 934 int ret; 935 936 _enter("{%s,%zu,%zu},%d", 937 call->type->name, call->iov_len, iov_iter_count(iter), want_more); 938 939 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter, 940 &call->iov_len, want_more, &remote_abort, 941 &call->service_id); 942 trace_afs_receive_data(call, call->iter, want_more, ret); 943 if (ret == 0 || ret == -EAGAIN) 944 return ret; 945 946 state = READ_ONCE(call->state); 947 if (ret == 1) { 948 switch (state) { 949 case AFS_CALL_CL_AWAIT_REPLY: 950 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY); 951 break; 952 case AFS_CALL_SV_AWAIT_REQUEST: 953 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING); 954 break; 955 case AFS_CALL_COMPLETE: 956 kdebug("prem complete %d", call->error); 957 return afs_io_error(call, afs_io_error_extract); 958 default: 959 break; 960 } 961 return 0; 962 } 963 964 afs_set_call_complete(call, ret, remote_abort); 965 return ret; 966 } 967 968 /* 969 * Log protocol error production. 970 */ 971 noinline int afs_protocol_error(struct afs_call *call, 972 enum afs_eproto_cause cause) 973 { 974 trace_afs_protocol_error(call, cause); 975 if (call) 976 call->unmarshalling_error = true; 977 return -EBADMSG; 978 } 979 980 /* 981 * Wake up OOB notification processing. 982 */ 983 static void afs_rx_notify_oob(struct sock *sk, struct sk_buff *oob) 984 { 985 struct afs_net *net = sk->sk_user_data; 986 987 schedule_work(&net->rx_oob_work); 988 } 989