18c3e34a4SDavid Howells /* RxRPC individual remote procedure call handling 28c3e34a4SDavid Howells * 38c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 48c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com) 58c3e34a4SDavid Howells * 68c3e34a4SDavid Howells * This program is free software; you can redistribute it and/or 78c3e34a4SDavid Howells * modify it under the terms of the GNU General Public License 88c3e34a4SDavid Howells * as published by the Free Software Foundation; either version 98c3e34a4SDavid Howells * 2 of the License, or (at your option) any later version. 108c3e34a4SDavid Howells */ 118c3e34a4SDavid Howells 128c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 138c3e34a4SDavid Howells 148c3e34a4SDavid Howells #include <linux/slab.h> 158c3e34a4SDavid Howells #include <linux/module.h> 168c3e34a4SDavid Howells #include <linux/circ_buf.h> 178c3e34a4SDavid Howells #include <linux/spinlock_types.h> 188c3e34a4SDavid Howells #include <net/sock.h> 198c3e34a4SDavid Howells #include <net/af_rxrpc.h> 208c3e34a4SDavid Howells #include "ar-internal.h" 218c3e34a4SDavid Howells 228c3e34a4SDavid Howells const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = { 23999b69f8SDavid Howells [RXRPC_CALL_UNINITIALISED] = "Uninit ", 24999b69f8SDavid Howells [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn", 258c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq", 268c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl", 278c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl", 2800e90712SDavid Howells [RXRPC_CALL_SERVER_PREALLOC] = "SvPrealc", 298c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SECURING] = "SvSecure", 308c3e34a4SDavid Howells [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept", 318c3e34a4SDavid Howells [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq", 328c3e34a4SDavid Howells [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq", 338c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl", 348c3e34a4SDavid Howells [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK", 358c3e34a4SDavid Howells [RXRPC_CALL_COMPLETE] = "Complete", 36f5c17aaeSDavid Howells }; 37f5c17aaeSDavid Howells 38f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = { 39f5c17aaeSDavid Howells [RXRPC_CALL_SUCCEEDED] = "Complete", 408c3e34a4SDavid Howells [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort", 418c3e34a4SDavid Howells [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort", 42f5c17aaeSDavid Howells [RXRPC_CALL_LOCAL_ERROR] = "LocError", 438c3e34a4SDavid Howells [RXRPC_CALL_NETWORK_ERROR] = "NetError", 448c3e34a4SDavid Howells }; 458c3e34a4SDavid Howells 468c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar; 478c3e34a4SDavid Howells 48248f219cSDavid Howells static void rxrpc_call_timer_expired(unsigned long _call) 49248f219cSDavid Howells { 50248f219cSDavid Howells struct rxrpc_call *call = (struct rxrpc_call *)_call; 51248f219cSDavid Howells 52248f219cSDavid Howells _enter("%d", call->debug_id); 53248f219cSDavid Howells 54405dea1dSDavid Howells if (call->state < RXRPC_CALL_COMPLETE) 55405dea1dSDavid Howells rxrpc_set_timer(call, rxrpc_timer_expired, ktime_get_real()); 56fc7ab6d2SDavid Howells } 578c3e34a4SDavid Howells 589faaff59SDavid Howells static struct lock_class_key rxrpc_call_user_mutex_lock_class_key; 599faaff59SDavid Howells 608c3e34a4SDavid Howells /* 618c3e34a4SDavid Howells * find an extant server call 628c3e34a4SDavid Howells * - called in process context with IRQs enabled 638c3e34a4SDavid Howells */ 648c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, 658c3e34a4SDavid Howells unsigned long user_call_ID) 668c3e34a4SDavid Howells { 678c3e34a4SDavid Howells struct rxrpc_call *call; 688c3e34a4SDavid Howells struct rb_node *p; 698c3e34a4SDavid Howells 708c3e34a4SDavid Howells _enter("%p,%lx", rx, user_call_ID); 718c3e34a4SDavid Howells 728c3e34a4SDavid Howells read_lock(&rx->call_lock); 738c3e34a4SDavid Howells 748c3e34a4SDavid Howells p = rx->calls.rb_node; 758c3e34a4SDavid Howells while (p) { 768c3e34a4SDavid Howells call = rb_entry(p, struct rxrpc_call, sock_node); 778c3e34a4SDavid Howells 788c3e34a4SDavid Howells if (user_call_ID < call->user_call_ID) 798c3e34a4SDavid Howells p = p->rb_left; 808c3e34a4SDavid Howells else if (user_call_ID > call->user_call_ID) 818c3e34a4SDavid Howells p = p->rb_right; 828c3e34a4SDavid Howells else 838c3e34a4SDavid Howells goto found_extant_call; 848c3e34a4SDavid Howells } 858c3e34a4SDavid Howells 868c3e34a4SDavid Howells read_unlock(&rx->call_lock); 878c3e34a4SDavid Howells _leave(" = NULL"); 888c3e34a4SDavid Howells return NULL; 898c3e34a4SDavid Howells 908c3e34a4SDavid Howells found_extant_call: 91fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got); 928c3e34a4SDavid Howells read_unlock(&rx->call_lock); 938c3e34a4SDavid Howells _leave(" = %p [%d]", call, atomic_read(&call->usage)); 948c3e34a4SDavid Howells return call; 958c3e34a4SDavid Howells } 968c3e34a4SDavid Howells 978c3e34a4SDavid Howells /* 988c3e34a4SDavid Howells * allocate a new call 998c3e34a4SDavid Howells */ 1009faaff59SDavid Howells struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp) 1018c3e34a4SDavid Howells { 1028c3e34a4SDavid Howells struct rxrpc_call *call; 1038c3e34a4SDavid Howells 1048c3e34a4SDavid Howells call = kmem_cache_zalloc(rxrpc_call_jar, gfp); 1058c3e34a4SDavid Howells if (!call) 1068c3e34a4SDavid Howells return NULL; 1078c3e34a4SDavid Howells 108248f219cSDavid Howells call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE, 109248f219cSDavid Howells sizeof(struct sk_buff *), 1108c3e34a4SDavid Howells gfp); 111248f219cSDavid Howells if (!call->rxtx_buffer) 112248f219cSDavid Howells goto nomem; 1138c3e34a4SDavid Howells 114248f219cSDavid Howells call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp); 115248f219cSDavid Howells if (!call->rxtx_annotations) 116248f219cSDavid Howells goto nomem_2; 117248f219cSDavid Howells 118540b1c48SDavid Howells mutex_init(&call->user_mutex); 1199faaff59SDavid Howells 1209faaff59SDavid Howells /* Prevent lockdep reporting a deadlock false positive between the afs 1219faaff59SDavid Howells * filesystem and sys_sendmsg() via the mmap sem. 1229faaff59SDavid Howells */ 1239faaff59SDavid Howells if (rx->sk.sk_kern_sock) 1249faaff59SDavid Howells lockdep_set_class(&call->user_mutex, 1259faaff59SDavid Howells &rxrpc_call_user_mutex_lock_class_key); 1269faaff59SDavid Howells 127248f219cSDavid Howells setup_timer(&call->timer, rxrpc_call_timer_expired, 1288c3e34a4SDavid Howells (unsigned long)call); 1298c3e34a4SDavid Howells INIT_WORK(&call->processor, &rxrpc_process_call); 130999b69f8SDavid Howells INIT_LIST_HEAD(&call->link); 13145025bceSDavid Howells INIT_LIST_HEAD(&call->chan_wait_link); 1328c3e34a4SDavid Howells INIT_LIST_HEAD(&call->accept_link); 133248f219cSDavid Howells INIT_LIST_HEAD(&call->recvmsg_link); 134248f219cSDavid Howells INIT_LIST_HEAD(&call->sock_link); 13545025bceSDavid Howells init_waitqueue_head(&call->waitq); 1368c3e34a4SDavid Howells spin_lock_init(&call->lock); 13720acbd9aSDavid Howells spin_lock_init(&call->notify_lock); 1388c3e34a4SDavid Howells rwlock_init(&call->state_lock); 1398c3e34a4SDavid Howells atomic_set(&call->usage, 1); 1408c3e34a4SDavid Howells call->debug_id = atomic_inc_return(&rxrpc_debug_id); 141e754eba6SDavid Howells call->tx_total_len = -1; 1428c3e34a4SDavid Howells 1438c3e34a4SDavid Howells memset(&call->sock_node, 0xed, sizeof(call->sock_node)); 1448c3e34a4SDavid Howells 145248f219cSDavid Howells /* Leave space in the ring to handle a maxed-out jumbo packet */ 14675e42126SDavid Howells call->rx_winsize = rxrpc_rx_window_size; 147248f219cSDavid Howells call->tx_winsize = 16; 148248f219cSDavid Howells call->rx_expect_next = 1; 14957494343SDavid Howells 15057494343SDavid Howells call->cong_cwnd = 2; 15157494343SDavid Howells call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1; 1528c3e34a4SDavid Howells return call; 153248f219cSDavid Howells 154248f219cSDavid Howells nomem_2: 155248f219cSDavid Howells kfree(call->rxtx_buffer); 156248f219cSDavid Howells nomem: 157248f219cSDavid Howells kmem_cache_free(rxrpc_call_jar, call); 158248f219cSDavid Howells return NULL; 1598c3e34a4SDavid Howells } 1608c3e34a4SDavid Howells 1618c3e34a4SDavid Howells /* 162999b69f8SDavid Howells * Allocate a new client call. 1638c3e34a4SDavid Howells */ 1649faaff59SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx, 1659faaff59SDavid Howells struct sockaddr_rxrpc *srx, 1668c3e34a4SDavid Howells gfp_t gfp) 1678c3e34a4SDavid Howells { 1688c3e34a4SDavid Howells struct rxrpc_call *call; 16957494343SDavid Howells ktime_t now; 1708c3e34a4SDavid Howells 1718c3e34a4SDavid Howells _enter(""); 1728c3e34a4SDavid Howells 1739faaff59SDavid Howells call = rxrpc_alloc_call(rx, gfp); 1748c3e34a4SDavid Howells if (!call) 1758c3e34a4SDavid Howells return ERR_PTR(-ENOMEM); 176999b69f8SDavid Howells call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; 177999b69f8SDavid Howells call->service_id = srx->srx_service; 17871f3ca40SDavid Howells call->tx_phase = true; 17957494343SDavid Howells now = ktime_get_real(); 18057494343SDavid Howells call->acks_latest_ts = now; 18157494343SDavid Howells call->cong_tstamp = now; 182999b69f8SDavid Howells 183999b69f8SDavid Howells _leave(" = %p", call); 184999b69f8SDavid Howells return call; 185999b69f8SDavid Howells } 186999b69f8SDavid Howells 187999b69f8SDavid Howells /* 188248f219cSDavid Howells * Initiate the call ack/resend/expiry timer. 189999b69f8SDavid Howells */ 190248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call) 191999b69f8SDavid Howells { 192df0adc78SDavid Howells ktime_t now = ktime_get_real(), expire_at; 193999b69f8SDavid Howells 194df0adc78SDavid Howells expire_at = ktime_add_ms(now, rxrpc_max_call_lifetime); 195248f219cSDavid Howells call->expire_at = expire_at; 196248f219cSDavid Howells call->ack_at = expire_at; 197a5af7e1fSDavid Howells call->ping_at = expire_at; 198248f219cSDavid Howells call->resend_at = expire_at; 199df0adc78SDavid Howells call->timer.expires = jiffies + LONG_MAX / 2; 200df0adc78SDavid Howells rxrpc_set_timer(call, rxrpc_timer_begin, now); 2018c3e34a4SDavid Howells } 2028c3e34a4SDavid Howells 2038c3e34a4SDavid Howells /* 204540b1c48SDavid Howells * Set up a call for the given parameters. 205540b1c48SDavid Howells * - Called with the socket lock held, which it must release. 206540b1c48SDavid Howells * - If it returns a call, the call's lock will need releasing by the caller. 2078c3e34a4SDavid Howells */ 2088c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, 20919ffa01cSDavid Howells struct rxrpc_conn_parameters *cp, 210999b69f8SDavid Howells struct sockaddr_rxrpc *srx, 2118c3e34a4SDavid Howells unsigned long user_call_ID, 212e754eba6SDavid Howells s64 tx_total_len, 2138c3e34a4SDavid Howells gfp_t gfp) 214540b1c48SDavid Howells __releases(&rx->sk.sk_lock.slock) 2158c3e34a4SDavid Howells { 2168c3e34a4SDavid Howells struct rxrpc_call *call, *xcall; 2172baec2c3SDavid Howells struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk)); 2188c3e34a4SDavid Howells struct rb_node *parent, **pp; 219e34d4234SDavid Howells const void *here = __builtin_return_address(0); 220999b69f8SDavid Howells int ret; 2218c3e34a4SDavid Howells 222999b69f8SDavid Howells _enter("%p,%lx", rx, user_call_ID); 2238c3e34a4SDavid Howells 2249faaff59SDavid Howells call = rxrpc_alloc_client_call(rx, srx, gfp); 2258c3e34a4SDavid Howells if (IS_ERR(call)) { 226540b1c48SDavid Howells release_sock(&rx->sk); 2278c3e34a4SDavid Howells _leave(" = %ld", PTR_ERR(call)); 2288c3e34a4SDavid Howells return call; 2298c3e34a4SDavid Howells } 2308c3e34a4SDavid Howells 231e754eba6SDavid Howells call->tx_total_len = tx_total_len; 232a84a46d7SDavid Howells trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage), 233a84a46d7SDavid Howells here, (const void *)user_call_ID); 234e34d4234SDavid Howells 235540b1c48SDavid Howells /* We need to protect a partially set up call against the user as we 236540b1c48SDavid Howells * will be acting outside the socket lock. 237540b1c48SDavid Howells */ 238540b1c48SDavid Howells mutex_lock(&call->user_mutex); 239540b1c48SDavid Howells 240999b69f8SDavid Howells /* Publish the call, even though it is incompletely set up as yet */ 2418c3e34a4SDavid Howells write_lock(&rx->call_lock); 2428c3e34a4SDavid Howells 2438c3e34a4SDavid Howells pp = &rx->calls.rb_node; 2448c3e34a4SDavid Howells parent = NULL; 2458c3e34a4SDavid Howells while (*pp) { 2468c3e34a4SDavid Howells parent = *pp; 2478c3e34a4SDavid Howells xcall = rb_entry(parent, struct rxrpc_call, sock_node); 2488c3e34a4SDavid Howells 2498c3e34a4SDavid Howells if (user_call_ID < xcall->user_call_ID) 2508c3e34a4SDavid Howells pp = &(*pp)->rb_left; 2518c3e34a4SDavid Howells else if (user_call_ID > xcall->user_call_ID) 2528c3e34a4SDavid Howells pp = &(*pp)->rb_right; 2538c3e34a4SDavid Howells else 254357f5ef6SDavid Howells goto error_dup_user_ID; 2558c3e34a4SDavid Howells } 2568c3e34a4SDavid Howells 257248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 258357f5ef6SDavid Howells call->user_call_ID = user_call_ID; 259357f5ef6SDavid Howells __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); 260fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got_userid); 2618c3e34a4SDavid Howells rb_link_node(&call->sock_node, parent, pp); 2628c3e34a4SDavid Howells rb_insert_color(&call->sock_node, &rx->calls); 263248f219cSDavid Howells list_add(&call->sock_link, &rx->sock_calls); 264248f219cSDavid Howells 2658c3e34a4SDavid Howells write_unlock(&rx->call_lock); 2668c3e34a4SDavid Howells 2672baec2c3SDavid Howells write_lock(&rxnet->call_lock); 2682baec2c3SDavid Howells list_add_tail(&call->link, &rxnet->calls); 2692baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 2708c3e34a4SDavid Howells 271540b1c48SDavid Howells /* From this point on, the call is protected by its own lock. */ 272540b1c48SDavid Howells release_sock(&rx->sk); 273540b1c48SDavid Howells 274248f219cSDavid Howells /* Set up or get a connection record and set the protocol parameters, 275248f219cSDavid Howells * including channel number and call ID. 276248f219cSDavid Howells */ 277248f219cSDavid Howells ret = rxrpc_connect_call(call, cp, srx, gfp); 278999b69f8SDavid Howells if (ret < 0) 279999b69f8SDavid Howells goto error; 280999b69f8SDavid Howells 281a84a46d7SDavid Howells trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage), 28254fde423SDavid Howells here, NULL); 283a84a46d7SDavid Howells 284248f219cSDavid Howells rxrpc_start_call_timer(call); 285248f219cSDavid Howells 2868c3e34a4SDavid Howells _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); 2878c3e34a4SDavid Howells 2888c3e34a4SDavid Howells _leave(" = %p [new]", call); 2898c3e34a4SDavid Howells return call; 2908c3e34a4SDavid Howells 2918c3e34a4SDavid Howells /* We unexpectedly found the user ID in the list after taking 2928c3e34a4SDavid Howells * the call_lock. This shouldn't happen unless the user races 2938c3e34a4SDavid Howells * with itself and tries to add the same user ID twice at the 2948c3e34a4SDavid Howells * same time in different threads. 2958c3e34a4SDavid Howells */ 296357f5ef6SDavid Howells error_dup_user_ID: 2978c3e34a4SDavid Howells write_unlock(&rx->call_lock); 298540b1c48SDavid Howells release_sock(&rx->sk); 2998d94aa38SDavid Howells ret = -EEXIST; 300357f5ef6SDavid Howells 301357f5ef6SDavid Howells error: 302357f5ef6SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 303357f5ef6SDavid Howells RX_CALL_DEAD, ret); 304a84a46d7SDavid Howells trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage), 305a84a46d7SDavid Howells here, ERR_PTR(ret)); 306357f5ef6SDavid Howells rxrpc_release_call(rx, call); 307540b1c48SDavid Howells mutex_unlock(&call->user_mutex); 308357f5ef6SDavid Howells rxrpc_put_call(call, rxrpc_call_put); 309357f5ef6SDavid Howells _leave(" = %d", ret); 310357f5ef6SDavid Howells return ERR_PTR(ret); 3118c3e34a4SDavid Howells } 3128c3e34a4SDavid Howells 3138c3e34a4SDavid Howells /* 314c038a58cSDavid Howells * Retry a call to a new address. It is expected that the Tx queue of the call 315c038a58cSDavid Howells * will contain data previously packaged for an old call. 316c038a58cSDavid Howells */ 317c038a58cSDavid Howells int rxrpc_retry_client_call(struct rxrpc_sock *rx, 318c038a58cSDavid Howells struct rxrpc_call *call, 319c038a58cSDavid Howells struct rxrpc_conn_parameters *cp, 320c038a58cSDavid Howells struct sockaddr_rxrpc *srx, 321c038a58cSDavid Howells gfp_t gfp) 322c038a58cSDavid Howells { 323c038a58cSDavid Howells const void *here = __builtin_return_address(0); 324c038a58cSDavid Howells int ret; 325c038a58cSDavid Howells 326c038a58cSDavid Howells /* Set up or get a connection record and set the protocol parameters, 327c038a58cSDavid Howells * including channel number and call ID. 328c038a58cSDavid Howells */ 329c038a58cSDavid Howells ret = rxrpc_connect_call(call, cp, srx, gfp); 330c038a58cSDavid Howells if (ret < 0) 331c038a58cSDavid Howells goto error; 332c038a58cSDavid Howells 333c038a58cSDavid Howells trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage), 334c038a58cSDavid Howells here, NULL); 335c038a58cSDavid Howells 336c038a58cSDavid Howells rxrpc_start_call_timer(call); 337c038a58cSDavid Howells 338c038a58cSDavid Howells _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); 339c038a58cSDavid Howells 340c038a58cSDavid Howells if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events)) 341c038a58cSDavid Howells rxrpc_queue_call(call); 342c038a58cSDavid Howells 343c038a58cSDavid Howells _leave(" = 0"); 344c038a58cSDavid Howells return 0; 345c038a58cSDavid Howells 346c038a58cSDavid Howells error: 347c038a58cSDavid Howells rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 348c038a58cSDavid Howells RX_CALL_DEAD, ret); 349c038a58cSDavid Howells trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage), 350c038a58cSDavid Howells here, ERR_PTR(ret)); 351c038a58cSDavid Howells _leave(" = %d", ret); 352c038a58cSDavid Howells return ret; 353c038a58cSDavid Howells } 354c038a58cSDavid Howells 355c038a58cSDavid Howells /* 356248f219cSDavid Howells * Set up an incoming call. call->conn points to the connection. 357248f219cSDavid Howells * This is called in BH context and isn't allowed to fail. 3588c3e34a4SDavid Howells */ 359248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx, 360248f219cSDavid Howells struct rxrpc_call *call, 36142886ffeSDavid Howells struct sk_buff *skb) 3628c3e34a4SDavid Howells { 363248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 36442886ffeSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 365248f219cSDavid Howells u32 chan; 3668c3e34a4SDavid Howells 367248f219cSDavid Howells _enter(",%d", call->conn->debug_id); 3688c3e34a4SDavid Howells 369248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 370248f219cSDavid Howells call->call_id = sp->hdr.callNumber; 371248f219cSDavid Howells call->service_id = sp->hdr.serviceId; 372248f219cSDavid Howells call->cid = sp->hdr.cid; 373248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_ACCEPTING; 374248f219cSDavid Howells if (sp->hdr.securityIndex > 0) 375248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_SECURING; 37657494343SDavid Howells call->cong_tstamp = skb->tstamp; 3778c3e34a4SDavid Howells 378248f219cSDavid Howells /* Set the channel for this call. We don't get channel_lock as we're 379248f219cSDavid Howells * only defending against the data_ready handler (which we're called 380248f219cSDavid Howells * from) and the RESPONSE packet parser (which is only really 381248f219cSDavid Howells * interested in call_counter and can cope with a disagreement with the 382248f219cSDavid Howells * call pointer). 3838c3e34a4SDavid Howells */ 384248f219cSDavid Howells chan = sp->hdr.cid & RXRPC_CHANNELMASK; 385248f219cSDavid Howells conn->channels[chan].call_counter = call->call_id; 386248f219cSDavid Howells conn->channels[chan].call_id = call->call_id; 387a1399f8bSDavid Howells rcu_assign_pointer(conn->channels[chan].call, call); 3888c3e34a4SDavid Howells 38985f32278SDavid Howells spin_lock(&conn->params.peer->lock); 39085f32278SDavid Howells hlist_add_head(&call->error_link, &conn->params.peer->error_targets); 39185f32278SDavid Howells spin_unlock(&conn->params.peer->lock); 3928c3e34a4SDavid Howells 3938c3e34a4SDavid Howells _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id); 3948c3e34a4SDavid Howells 395248f219cSDavid Howells rxrpc_start_call_timer(call); 396248f219cSDavid Howells _leave(""); 3978c3e34a4SDavid Howells } 3988c3e34a4SDavid Howells 3998c3e34a4SDavid Howells /* 4008d94aa38SDavid Howells * Queue a call's work processor, getting a ref to pass to the work queue. 4018d94aa38SDavid Howells */ 4028d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call) 4038d94aa38SDavid Howells { 4048d94aa38SDavid Howells const void *here = __builtin_return_address(0); 4058d94aa38SDavid Howells int n = __atomic_add_unless(&call->usage, 1, 0); 4068d94aa38SDavid Howells if (n == 0) 4078d94aa38SDavid Howells return false; 4088d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 4092ab27215SDavid Howells trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL); 4108d94aa38SDavid Howells else 4118d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 4128d94aa38SDavid Howells return true; 4138d94aa38SDavid Howells } 4148d94aa38SDavid Howells 4158d94aa38SDavid Howells /* 4168d94aa38SDavid Howells * Queue a call's work processor, passing the callers ref to the work queue. 4178d94aa38SDavid Howells */ 4188d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call) 4198d94aa38SDavid Howells { 4208d94aa38SDavid Howells const void *here = __builtin_return_address(0); 4218d94aa38SDavid Howells int n = atomic_read(&call->usage); 4228d94aa38SDavid Howells ASSERTCMP(n, >=, 1); 4238d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 4242ab27215SDavid Howells trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL); 4258d94aa38SDavid Howells else 4268d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 4278d94aa38SDavid Howells return true; 4288d94aa38SDavid Howells } 4298d94aa38SDavid Howells 4308d94aa38SDavid Howells /* 431e34d4234SDavid Howells * Note the re-emergence of a call. 432e34d4234SDavid Howells */ 433e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call) 434e34d4234SDavid Howells { 435e34d4234SDavid Howells const void *here = __builtin_return_address(0); 436e34d4234SDavid Howells if (call) { 437e34d4234SDavid Howells int n = atomic_read(&call->usage); 438e34d4234SDavid Howells 4392ab27215SDavid Howells trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL); 440e34d4234SDavid Howells } 441e34d4234SDavid Howells } 442e34d4234SDavid Howells 443e34d4234SDavid Howells /* 444e34d4234SDavid Howells * Note the addition of a ref on a call. 445e34d4234SDavid Howells */ 446fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 447e34d4234SDavid Howells { 448e34d4234SDavid Howells const void *here = __builtin_return_address(0); 449e34d4234SDavid Howells int n = atomic_inc_return(&call->usage); 450e34d4234SDavid Howells 4512ab27215SDavid Howells trace_rxrpc_call(call, op, n, here, NULL); 452e34d4234SDavid Howells } 453e34d4234SDavid Howells 454e34d4234SDavid Howells /* 455248f219cSDavid Howells * Detach a call from its owning socket. 4568c3e34a4SDavid Howells */ 4578d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call) 4588c3e34a4SDavid Howells { 459a84a46d7SDavid Howells const void *here = __builtin_return_address(0); 460248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 461248f219cSDavid Howells bool put = false; 462248f219cSDavid Howells int i; 463248f219cSDavid Howells 464248f219cSDavid Howells _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage)); 465248f219cSDavid Howells 466a84a46d7SDavid Howells trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage), 467a84a46d7SDavid Howells here, (const void *)call->flags); 4688c3e34a4SDavid Howells 469a84a46d7SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 470e34d4234SDavid Howells 4718c3e34a4SDavid Howells spin_lock_bh(&call->lock); 4728c3e34a4SDavid Howells if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) 4738c3e34a4SDavid Howells BUG(); 4748c3e34a4SDavid Howells spin_unlock_bh(&call->lock); 4758c3e34a4SDavid Howells 476248f219cSDavid Howells del_timer_sync(&call->timer); 4778c3e34a4SDavid Howells 478248f219cSDavid Howells /* Make sure we don't get any more notifications */ 479248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 480e653cfe4SDavid Howells 481248f219cSDavid Howells if (!list_empty(&call->recvmsg_link)) { 4828c3e34a4SDavid Howells _debug("unlinking once-pending call %p { e=%lx f=%lx }", 4838c3e34a4SDavid Howells call, call->events, call->flags); 484248f219cSDavid Howells list_del(&call->recvmsg_link); 485248f219cSDavid Howells put = true; 486248f219cSDavid Howells } 487248f219cSDavid Howells 488248f219cSDavid Howells /* list_empty() must return false in rxrpc_notify_socket() */ 489248f219cSDavid Howells call->recvmsg_link.next = NULL; 490248f219cSDavid Howells call->recvmsg_link.prev = NULL; 491248f219cSDavid Howells 492248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 493248f219cSDavid Howells if (put) 494248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 495248f219cSDavid Howells 496248f219cSDavid Howells write_lock(&rx->call_lock); 497248f219cSDavid Howells 498248f219cSDavid Howells if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { 4998c3e34a4SDavid Howells rb_erase(&call->sock_node, &rx->calls); 5008c3e34a4SDavid Howells memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); 5018d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid); 5028c3e34a4SDavid Howells } 5038c3e34a4SDavid Howells 504248f219cSDavid Howells list_del(&call->sock_link); 505248f219cSDavid Howells write_unlock(&rx->call_lock); 5068c3e34a4SDavid Howells 507248f219cSDavid Howells _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); 5088c3e34a4SDavid Howells 509248f219cSDavid Howells if (conn) 510e653cfe4SDavid Howells rxrpc_disconnect_call(call); 511e653cfe4SDavid Howells 512248f219cSDavid Howells for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) { 51371f3ca40SDavid Howells rxrpc_free_skb(call->rxtx_buffer[i], 51471f3ca40SDavid Howells (call->tx_phase ? rxrpc_skb_tx_cleaned : 51571f3ca40SDavid Howells rxrpc_skb_rx_cleaned)); 516248f219cSDavid Howells call->rxtx_buffer[i] = NULL; 5178c3e34a4SDavid Howells } 5188c3e34a4SDavid Howells 5198c3e34a4SDavid Howells _leave(""); 5208c3e34a4SDavid Howells } 5218c3e34a4SDavid Howells 5228c3e34a4SDavid Howells /* 523c038a58cSDavid Howells * Prepare a kernel service call for retry. 524c038a58cSDavid Howells */ 525c038a58cSDavid Howells int rxrpc_prepare_call_for_retry(struct rxrpc_sock *rx, struct rxrpc_call *call) 526c038a58cSDavid Howells { 527c038a58cSDavid Howells const void *here = __builtin_return_address(0); 528c038a58cSDavid Howells int i; 529c038a58cSDavid Howells u8 last = 0; 530c038a58cSDavid Howells 531c038a58cSDavid Howells _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage)); 532c038a58cSDavid Howells 533c038a58cSDavid Howells trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage), 534c038a58cSDavid Howells here, (const void *)call->flags); 535c038a58cSDavid Howells 536c038a58cSDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 537c038a58cSDavid Howells ASSERTCMP(call->completion, !=, RXRPC_CALL_REMOTELY_ABORTED); 538c038a58cSDavid Howells ASSERTCMP(call->completion, !=, RXRPC_CALL_LOCALLY_ABORTED); 539c038a58cSDavid Howells ASSERT(list_empty(&call->recvmsg_link)); 540c038a58cSDavid Howells 541c038a58cSDavid Howells del_timer_sync(&call->timer); 542c038a58cSDavid Howells 543c038a58cSDavid Howells _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, call->conn); 544c038a58cSDavid Howells 545c038a58cSDavid Howells if (call->conn) 546c038a58cSDavid Howells rxrpc_disconnect_call(call); 547c038a58cSDavid Howells 548c038a58cSDavid Howells if (rxrpc_is_service_call(call) || 549c038a58cSDavid Howells !call->tx_phase || 550c038a58cSDavid Howells call->tx_hard_ack != 0 || 551c038a58cSDavid Howells call->rx_hard_ack != 0 || 552c038a58cSDavid Howells call->rx_top != 0) 553c038a58cSDavid Howells return -EINVAL; 554c038a58cSDavid Howells 555c038a58cSDavid Howells call->state = RXRPC_CALL_UNINITIALISED; 556c038a58cSDavid Howells call->completion = RXRPC_CALL_SUCCEEDED; 557c038a58cSDavid Howells call->call_id = 0; 558c038a58cSDavid Howells call->cid = 0; 559c038a58cSDavid Howells call->cong_cwnd = 0; 560c038a58cSDavid Howells call->cong_extra = 0; 561c038a58cSDavid Howells call->cong_ssthresh = 0; 562c038a58cSDavid Howells call->cong_mode = 0; 563c038a58cSDavid Howells call->cong_dup_acks = 0; 564c038a58cSDavid Howells call->cong_cumul_acks = 0; 565c038a58cSDavid Howells call->acks_lowest_nak = 0; 566c038a58cSDavid Howells 567c038a58cSDavid Howells for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) { 568c038a58cSDavid Howells last |= call->rxtx_annotations[i]; 569c038a58cSDavid Howells call->rxtx_annotations[i] &= RXRPC_TX_ANNO_LAST; 570c038a58cSDavid Howells call->rxtx_annotations[i] |= RXRPC_TX_ANNO_RETRANS; 571c038a58cSDavid Howells } 572c038a58cSDavid Howells 573c038a58cSDavid Howells _leave(" = 0"); 574c038a58cSDavid Howells return 0; 575c038a58cSDavid Howells } 576c038a58cSDavid Howells 577c038a58cSDavid Howells /* 5788c3e34a4SDavid Howells * release all the calls associated with a socket 5798c3e34a4SDavid Howells */ 5808c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) 5818c3e34a4SDavid Howells { 5828c3e34a4SDavid Howells struct rxrpc_call *call; 5838c3e34a4SDavid Howells 5848c3e34a4SDavid Howells _enter("%p", rx); 5858c3e34a4SDavid Howells 5860360da6dSDavid Howells while (!list_empty(&rx->to_be_accepted)) { 5870360da6dSDavid Howells call = list_entry(rx->to_be_accepted.next, 5880360da6dSDavid Howells struct rxrpc_call, accept_link); 5890360da6dSDavid Howells list_del(&call->accept_link); 5903a92789aSDavid Howells rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET); 5910360da6dSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 5920360da6dSDavid Howells } 5930360da6dSDavid Howells 594248f219cSDavid Howells while (!list_empty(&rx->sock_calls)) { 595248f219cSDavid Howells call = list_entry(rx->sock_calls.next, 596248f219cSDavid Howells struct rxrpc_call, sock_link); 597248f219cSDavid Howells rxrpc_get_call(call, rxrpc_call_got); 5983a92789aSDavid Howells rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET); 59926cb02aaSDavid Howells rxrpc_send_abort_packet(call); 6008d94aa38SDavid Howells rxrpc_release_call(rx, call); 601248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 6028c3e34a4SDavid Howells } 6038c3e34a4SDavid Howells 6048c3e34a4SDavid Howells _leave(""); 6058c3e34a4SDavid Howells } 6068c3e34a4SDavid Howells 6078c3e34a4SDavid Howells /* 6088c3e34a4SDavid Howells * release a call 6098c3e34a4SDavid Howells */ 610fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 6118c3e34a4SDavid Howells { 6122baec2c3SDavid Howells struct rxrpc_net *rxnet; 613e34d4234SDavid Howells const void *here = __builtin_return_address(0); 6142ab27215SDavid Howells int n; 615e34d4234SDavid Howells 6168c3e34a4SDavid Howells ASSERT(call != NULL); 6178c3e34a4SDavid Howells 618e34d4234SDavid Howells n = atomic_dec_return(&call->usage); 6192ab27215SDavid Howells trace_rxrpc_call(call, op, n, here, NULL); 620e34d4234SDavid Howells ASSERTCMP(n, >=, 0); 621e34d4234SDavid Howells if (n == 0) { 6228c3e34a4SDavid Howells _debug("call %d dead", call->debug_id); 623248f219cSDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 624e34d4234SDavid Howells 6252baec2c3SDavid Howells if (!list_empty(&call->link)) { 6262baec2c3SDavid Howells rxnet = rxrpc_net(sock_net(&call->socket->sk)); 6272baec2c3SDavid Howells write_lock(&rxnet->call_lock); 628248f219cSDavid Howells list_del_init(&call->link); 6292baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 6302baec2c3SDavid Howells } 631e34d4234SDavid Howells 6328d94aa38SDavid Howells rxrpc_cleanup_call(call); 633e34d4234SDavid Howells } 6348c3e34a4SDavid Howells } 6358c3e34a4SDavid Howells 6368c3e34a4SDavid Howells /* 637dee46364SDavid Howells * Final call destruction under RCU. 638dee46364SDavid Howells */ 639dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu) 640dee46364SDavid Howells { 641dee46364SDavid Howells struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu); 642dee46364SDavid Howells 643df5d8bf7SDavid Howells rxrpc_put_peer(call->peer); 644248f219cSDavid Howells kfree(call->rxtx_buffer); 645248f219cSDavid Howells kfree(call->rxtx_annotations); 646dee46364SDavid Howells kmem_cache_free(rxrpc_call_jar, call); 647dee46364SDavid Howells } 648dee46364SDavid Howells 649dee46364SDavid Howells /* 6508c3e34a4SDavid Howells * clean up a call 6518c3e34a4SDavid Howells */ 65200e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call) 6538c3e34a4SDavid Howells { 654248f219cSDavid Howells int i; 6558c3e34a4SDavid Howells 656248f219cSDavid Howells _net("DESTROY CALL %d", call->debug_id); 6578c3e34a4SDavid Howells 6588c3e34a4SDavid Howells memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); 6598c3e34a4SDavid Howells 660248f219cSDavid Howells del_timer_sync(&call->timer); 6618c3e34a4SDavid Howells 6628d94aa38SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 6638c3e34a4SDavid Howells ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); 664e653cfe4SDavid Howells ASSERTCMP(call->conn, ==, NULL); 6658c3e34a4SDavid Howells 666248f219cSDavid Howells /* Clean up the Rx/Tx buffer */ 667248f219cSDavid Howells for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) 66871f3ca40SDavid Howells rxrpc_free_skb(call->rxtx_buffer[i], 66971f3ca40SDavid Howells (call->tx_phase ? rxrpc_skb_tx_cleaned : 67071f3ca40SDavid Howells rxrpc_skb_rx_cleaned)); 6718c3e34a4SDavid Howells 67271f3ca40SDavid Howells rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned); 6738c3e34a4SDavid Howells 674dee46364SDavid Howells call_rcu(&call->rcu, rxrpc_rcu_destroy_call); 6758c3e34a4SDavid Howells } 6768c3e34a4SDavid Howells 6778c3e34a4SDavid Howells /* 6782baec2c3SDavid Howells * Make sure that all calls are gone from a network namespace. To reach this 6792baec2c3SDavid Howells * point, any open UDP sockets in that namespace must have been closed, so any 6802baec2c3SDavid Howells * outstanding calls cannot be doing I/O. 6818c3e34a4SDavid Howells */ 6822baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet) 6838c3e34a4SDavid Howells { 6848c3e34a4SDavid Howells struct rxrpc_call *call; 6858c3e34a4SDavid Howells 6868c3e34a4SDavid Howells _enter(""); 6878d94aa38SDavid Howells 6882baec2c3SDavid Howells if (list_empty(&rxnet->calls)) 6898d94aa38SDavid Howells return; 6908d94aa38SDavid Howells 6912baec2c3SDavid Howells write_lock(&rxnet->call_lock); 6928c3e34a4SDavid Howells 6932baec2c3SDavid Howells while (!list_empty(&rxnet->calls)) { 6942baec2c3SDavid Howells call = list_entry(rxnet->calls.next, struct rxrpc_call, link); 6958c3e34a4SDavid Howells _debug("Zapping call %p", call); 6968c3e34a4SDavid Howells 697e34d4234SDavid Howells rxrpc_see_call(call); 6988c3e34a4SDavid Howells list_del_init(&call->link); 6998c3e34a4SDavid Howells 700248f219cSDavid Howells pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n", 7018c3e34a4SDavid Howells call, atomic_read(&call->usage), 7028c3e34a4SDavid Howells rxrpc_call_states[call->state], 7038c3e34a4SDavid Howells call->flags, call->events); 7048c3e34a4SDavid Howells 7052baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 7068c3e34a4SDavid Howells cond_resched(); 7072baec2c3SDavid Howells write_lock(&rxnet->call_lock); 7088c3e34a4SDavid Howells } 7098c3e34a4SDavid Howells 7102baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 7118c3e34a4SDavid Howells } 712