David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* incoming call handling |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/net.h> |
| 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/errqueue.h> |
| 18 | #include <linux/udp.h> |
| 19 | #include <linux/in.h> |
| 20 | #include <linux/in6.h> |
| 21 | #include <linux/icmp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/gfp.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 23 | #include <net/sock.h> |
| 24 | #include <net/af_rxrpc.h> |
| 25 | #include <net/ip.h> |
| 26 | #include "ar-internal.h" |
| 27 | |
| 28 | /* |
| 29 | * generate a connection-level abort |
| 30 | */ |
| 31 | static int rxrpc_busy(struct rxrpc_local *local, struct sockaddr_rxrpc *srx, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 32 | struct rxrpc_wire_header *whdr) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 33 | { |
| 34 | struct msghdr msg; |
| 35 | struct kvec iov[1]; |
| 36 | size_t len; |
| 37 | int ret; |
| 38 | |
| 39 | _enter("%d,,", local->debug_id); |
| 40 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 41 | whdr->type = RXRPC_PACKET_TYPE_BUSY; |
| 42 | whdr->serial = htonl(1); |
| 43 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 44 | msg.msg_name = &srx->transport.sin; |
| 45 | msg.msg_namelen = sizeof(srx->transport.sin); |
| 46 | msg.msg_control = NULL; |
| 47 | msg.msg_controllen = 0; |
| 48 | msg.msg_flags = 0; |
| 49 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 50 | iov[0].iov_base = whdr; |
| 51 | iov[0].iov_len = sizeof(*whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 52 | |
| 53 | len = iov[0].iov_len; |
| 54 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 55 | _proto("Tx BUSY %%1"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 56 | |
| 57 | ret = kernel_sendmsg(local->socket, &msg, iov, 1, len); |
| 58 | if (ret < 0) { |
| 59 | _leave(" = -EAGAIN [sendmsg failed: %d]", ret); |
| 60 | return -EAGAIN; |
| 61 | } |
| 62 | |
| 63 | _leave(" = 0"); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * accept an incoming call that needs peer, transport and/or connection setting |
| 69 | * up |
| 70 | */ |
| 71 | static int rxrpc_accept_incoming_call(struct rxrpc_local *local, |
| 72 | struct rxrpc_sock *rx, |
| 73 | struct sk_buff *skb, |
| 74 | struct sockaddr_rxrpc *srx) |
| 75 | { |
| 76 | struct rxrpc_connection *conn; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 77 | struct rxrpc_skb_priv *sp, *nsp; |
| 78 | struct rxrpc_peer *peer; |
| 79 | struct rxrpc_call *call; |
| 80 | struct sk_buff *notification; |
| 81 | int ret; |
| 82 | |
| 83 | _enter(""); |
| 84 | |
| 85 | sp = rxrpc_skb(skb); |
| 86 | |
| 87 | /* get a notification message to send to the server app */ |
| 88 | notification = alloc_skb(0, GFP_NOFS); |
Tetsuo Handa | c3824d2 | 2010-03-22 13:50:19 +0000 | [diff] [blame] | 89 | if (!notification) { |
| 90 | _debug("no memory"); |
| 91 | ret = -ENOMEM; |
| 92 | goto error_nofree; |
| 93 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 94 | rxrpc_new_skb(notification); |
| 95 | notification->mark = RXRPC_SKB_MARK_NEW_CALL; |
| 96 | |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 97 | peer = rxrpc_lookup_peer(local, srx, GFP_NOIO); |
Dan Carpenter | 0e4699e | 2016-06-18 11:44:03 +0300 | [diff] [blame] | 98 | if (!peer) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 99 | _debug("no peer"); |
| 100 | ret = -EBUSY; |
| 101 | goto error; |
| 102 | } |
| 103 | |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 104 | conn = rxrpc_incoming_connection(local, peer, skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 105 | rxrpc_put_peer(peer); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 106 | if (IS_ERR(conn)) { |
| 107 | _debug("no conn"); |
| 108 | ret = PTR_ERR(conn); |
| 109 | goto error; |
| 110 | } |
| 111 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 112 | call = rxrpc_incoming_call(rx, conn, skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 113 | rxrpc_put_connection(conn); |
| 114 | if (IS_ERR(call)) { |
| 115 | _debug("no call"); |
| 116 | ret = PTR_ERR(call); |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | /* attach the call to the socket */ |
| 121 | read_lock_bh(&local->services_lock); |
| 122 | if (rx->sk.sk_state == RXRPC_CLOSE) |
| 123 | goto invalid_service; |
| 124 | |
| 125 | write_lock(&rx->call_lock); |
| 126 | if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) { |
| 127 | rxrpc_get_call(call); |
| 128 | |
| 129 | spin_lock(&call->conn->state_lock); |
| 130 | if (sp->hdr.securityIndex > 0 && |
David Howells | bba304d | 2016-06-27 10:32:02 +0100 | [diff] [blame^] | 131 | call->conn->state == RXRPC_CONN_SERVICE_UNSECURED) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 132 | _debug("await conn sec"); |
| 133 | list_add_tail(&call->accept_link, &rx->secureq); |
David Howells | bba304d | 2016-06-27 10:32:02 +0100 | [diff] [blame^] | 134 | call->conn->state = RXRPC_CONN_SERVICE_CHALLENGING; |
David Howells | 5627cc8 | 2016-04-04 14:00:38 +0100 | [diff] [blame] | 135 | rxrpc_get_connection(call->conn); |
David Howells | bba304d | 2016-06-27 10:32:02 +0100 | [diff] [blame^] | 136 | set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 137 | rxrpc_queue_conn(call->conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 138 | } else { |
| 139 | _debug("conn ready"); |
| 140 | call->state = RXRPC_CALL_SERVER_ACCEPTING; |
| 141 | list_add_tail(&call->accept_link, &rx->acceptq); |
| 142 | rxrpc_get_call(call); |
| 143 | nsp = rxrpc_skb(notification); |
| 144 | nsp->call = call; |
| 145 | |
| 146 | ASSERTCMP(atomic_read(&call->usage), >=, 3); |
| 147 | |
| 148 | _debug("notify"); |
| 149 | spin_lock(&call->lock); |
| 150 | ret = rxrpc_queue_rcv_skb(call, notification, true, |
| 151 | false); |
| 152 | spin_unlock(&call->lock); |
| 153 | notification = NULL; |
Julia Lawall | 163e3cb | 2008-02-17 18:42:03 -0800 | [diff] [blame] | 154 | BUG_ON(ret < 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 155 | } |
| 156 | spin_unlock(&call->conn->state_lock); |
| 157 | |
| 158 | _debug("queued"); |
| 159 | } |
| 160 | write_unlock(&rx->call_lock); |
| 161 | |
| 162 | _debug("process"); |
| 163 | rxrpc_fast_process_packet(call, skb); |
| 164 | |
| 165 | _debug("done"); |
| 166 | read_unlock_bh(&local->services_lock); |
| 167 | rxrpc_free_skb(notification); |
| 168 | rxrpc_put_call(call); |
| 169 | _leave(" = 0"); |
| 170 | return 0; |
| 171 | |
| 172 | invalid_service: |
| 173 | _debug("invalid"); |
| 174 | read_unlock_bh(&local->services_lock); |
| 175 | |
| 176 | read_lock_bh(&call->state_lock); |
David Howells | e721498 | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 177 | if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) && |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 178 | !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 179 | rxrpc_get_call(call); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 180 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 181 | } |
| 182 | read_unlock_bh(&call->state_lock); |
| 183 | rxrpc_put_call(call); |
| 184 | ret = -ECONNREFUSED; |
| 185 | error: |
| 186 | rxrpc_free_skb(notification); |
Tetsuo Handa | c3824d2 | 2010-03-22 13:50:19 +0000 | [diff] [blame] | 187 | error_nofree: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 188 | _leave(" = %d", ret); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * accept incoming calls that need peer, transport and/or connection setting up |
| 194 | * - the packets we get are all incoming client DATA packets that have seq == 1 |
| 195 | */ |
David Howells | 4f95dd7 | 2016-04-04 14:00:35 +0100 | [diff] [blame] | 196 | void rxrpc_accept_incoming_calls(struct rxrpc_local *local) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 197 | { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 198 | struct rxrpc_skb_priv *sp; |
| 199 | struct sockaddr_rxrpc srx; |
| 200 | struct rxrpc_sock *rx; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 201 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 202 | struct sk_buff *skb; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 203 | int ret; |
| 204 | |
| 205 | _enter("%d", local->debug_id); |
| 206 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 207 | skb = skb_dequeue(&local->accept_queue); |
| 208 | if (!skb) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 209 | _leave("\n"); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | _net("incoming call skb %p", skb); |
| 214 | |
| 215 | sp = rxrpc_skb(skb); |
| 216 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 217 | /* Set up a response packet header in case we need it */ |
| 218 | whdr.epoch = htonl(sp->hdr.epoch); |
| 219 | whdr.cid = htonl(sp->hdr.cid); |
| 220 | whdr.callNumber = htonl(sp->hdr.callNumber); |
| 221 | whdr.seq = htonl(sp->hdr.seq); |
| 222 | whdr.serial = 0; |
| 223 | whdr.flags = 0; |
| 224 | whdr.type = 0; |
| 225 | whdr.userStatus = 0; |
| 226 | whdr.securityIndex = sp->hdr.securityIndex; |
| 227 | whdr._rsvd = 0; |
| 228 | whdr.serviceId = htons(sp->hdr.serviceId); |
| 229 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 230 | /* determine the remote address */ |
| 231 | memset(&srx, 0, sizeof(srx)); |
| 232 | srx.srx_family = AF_RXRPC; |
| 233 | srx.transport.family = local->srx.transport.family; |
| 234 | srx.transport_type = local->srx.transport_type; |
| 235 | switch (srx.transport.family) { |
| 236 | case AF_INET: |
| 237 | srx.transport_len = sizeof(struct sockaddr_in); |
| 238 | srx.transport.sin.sin_port = udp_hdr(skb)->source; |
| 239 | srx.transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 240 | break; |
| 241 | default: |
| 242 | goto busy; |
| 243 | } |
| 244 | |
| 245 | /* get the socket providing the service */ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 246 | read_lock_bh(&local->services_lock); |
| 247 | list_for_each_entry(rx, &local->services, listen_link) { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 248 | if (rx->srx.srx_service == sp->hdr.serviceId && |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 249 | rx->sk.sk_state != RXRPC_CLOSE) |
| 250 | goto found_service; |
| 251 | } |
| 252 | read_unlock_bh(&local->services_lock); |
| 253 | goto invalid_service; |
| 254 | |
| 255 | found_service: |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 256 | _debug("found service %hd", rx->srx.srx_service); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 257 | if (sk_acceptq_is_full(&rx->sk)) |
| 258 | goto backlog_full; |
| 259 | sk_acceptq_added(&rx->sk); |
| 260 | sock_hold(&rx->sk); |
| 261 | read_unlock_bh(&local->services_lock); |
| 262 | |
| 263 | ret = rxrpc_accept_incoming_call(local, rx, skb, &srx); |
| 264 | if (ret < 0) |
| 265 | sk_acceptq_removed(&rx->sk); |
| 266 | sock_put(&rx->sk); |
| 267 | switch (ret) { |
| 268 | case -ECONNRESET: /* old calls are ignored */ |
| 269 | case -ECONNABORTED: /* aborted calls are reaborted or ignored */ |
| 270 | case 0: |
David Howells | 4f95dd7 | 2016-04-04 14:00:35 +0100 | [diff] [blame] | 271 | return; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 272 | case -ECONNREFUSED: |
| 273 | goto invalid_service; |
| 274 | case -EBUSY: |
| 275 | goto busy; |
| 276 | case -EKEYREJECTED: |
| 277 | goto security_mismatch; |
| 278 | default: |
| 279 | BUG(); |
| 280 | } |
| 281 | |
| 282 | backlog_full: |
| 283 | read_unlock_bh(&local->services_lock); |
| 284 | busy: |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 285 | rxrpc_busy(local, &srx, &whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 286 | rxrpc_free_skb(skb); |
David Howells | 4f95dd7 | 2016-04-04 14:00:35 +0100 | [diff] [blame] | 287 | return; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 288 | |
| 289 | invalid_service: |
| 290 | skb->priority = RX_INVALID_OPERATION; |
| 291 | rxrpc_reject_packet(local, skb); |
David Howells | 4f95dd7 | 2016-04-04 14:00:35 +0100 | [diff] [blame] | 292 | return; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 293 | |
| 294 | /* can't change connection security type mid-flow */ |
| 295 | security_mismatch: |
| 296 | skb->priority = RX_PROTOCOL_ERROR; |
| 297 | rxrpc_reject_packet(local, skb); |
David Howells | 4f95dd7 | 2016-04-04 14:00:35 +0100 | [diff] [blame] | 298 | return; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * handle acceptance of a call by userspace |
| 303 | * - assign the user call ID to the call at the front of the queue |
| 304 | */ |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 305 | struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx, |
| 306 | unsigned long user_call_ID) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 307 | { |
| 308 | struct rxrpc_call *call; |
| 309 | struct rb_node *parent, **pp; |
| 310 | int ret; |
| 311 | |
| 312 | _enter(",%lx", user_call_ID); |
| 313 | |
| 314 | ASSERT(!irqs_disabled()); |
| 315 | |
| 316 | write_lock(&rx->call_lock); |
| 317 | |
| 318 | ret = -ENODATA; |
| 319 | if (list_empty(&rx->acceptq)) |
| 320 | goto out; |
| 321 | |
| 322 | /* check the user ID isn't already in use */ |
| 323 | ret = -EBADSLT; |
| 324 | pp = &rx->calls.rb_node; |
| 325 | parent = NULL; |
| 326 | while (*pp) { |
| 327 | parent = *pp; |
| 328 | call = rb_entry(parent, struct rxrpc_call, sock_node); |
| 329 | |
| 330 | if (user_call_ID < call->user_call_ID) |
| 331 | pp = &(*pp)->rb_left; |
| 332 | else if (user_call_ID > call->user_call_ID) |
| 333 | pp = &(*pp)->rb_right; |
| 334 | else |
| 335 | goto out; |
| 336 | } |
| 337 | |
| 338 | /* dequeue the first call and check it's still valid */ |
| 339 | call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link); |
| 340 | list_del_init(&call->accept_link); |
| 341 | sk_acceptq_removed(&rx->sk); |
| 342 | |
| 343 | write_lock_bh(&call->state_lock); |
| 344 | switch (call->state) { |
| 345 | case RXRPC_CALL_SERVER_ACCEPTING: |
| 346 | call->state = RXRPC_CALL_SERVER_RECV_REQUEST; |
| 347 | break; |
| 348 | case RXRPC_CALL_REMOTELY_ABORTED: |
| 349 | case RXRPC_CALL_LOCALLY_ABORTED: |
| 350 | ret = -ECONNABORTED; |
| 351 | goto out_release; |
| 352 | case RXRPC_CALL_NETWORK_ERROR: |
| 353 | ret = call->conn->error; |
| 354 | goto out_release; |
| 355 | case RXRPC_CALL_DEAD: |
| 356 | ret = -ETIME; |
| 357 | goto out_discard; |
| 358 | default: |
| 359 | BUG(); |
| 360 | } |
| 361 | |
| 362 | /* formalise the acceptance */ |
| 363 | call->user_call_ID = user_call_ID; |
| 364 | rb_link_node(&call->sock_node, parent, pp); |
| 365 | rb_insert_color(&call->sock_node, &rx->calls); |
| 366 | if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags)) |
| 367 | BUG(); |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 368 | if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 369 | BUG(); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 370 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 371 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 372 | rxrpc_get_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 373 | write_unlock_bh(&call->state_lock); |
| 374 | write_unlock(&rx->call_lock); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 375 | _leave(" = %p{%d}", call, call->debug_id); |
| 376 | return call; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 377 | |
| 378 | /* if the call is already dying or dead, then we leave the socket's ref |
| 379 | * on it to be released by rxrpc_dead_call_expired() as induced by |
| 380 | * rxrpc_release_call() */ |
| 381 | out_release: |
| 382 | _debug("release %p", call); |
| 383 | if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) && |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 384 | !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 385 | rxrpc_queue_call(call); |
| 386 | out_discard: |
| 387 | write_unlock_bh(&call->state_lock); |
| 388 | _debug("discard %p", call); |
| 389 | out: |
| 390 | write_unlock(&rx->call_lock); |
| 391 | _leave(" = %d", ret); |
| 392 | return ERR_PTR(ret); |
| 393 | } |
| 394 | |
| 395 | /* |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame] | 396 | * Handle rejection of a call by userspace |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 397 | * - reject the call at the front of the queue |
| 398 | */ |
| 399 | int rxrpc_reject_call(struct rxrpc_sock *rx) |
| 400 | { |
| 401 | struct rxrpc_call *call; |
| 402 | int ret; |
| 403 | |
| 404 | _enter(""); |
| 405 | |
| 406 | ASSERT(!irqs_disabled()); |
| 407 | |
| 408 | write_lock(&rx->call_lock); |
| 409 | |
| 410 | ret = -ENODATA; |
| 411 | if (list_empty(&rx->acceptq)) |
| 412 | goto out; |
| 413 | |
| 414 | /* dequeue the first call and check it's still valid */ |
| 415 | call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link); |
| 416 | list_del_init(&call->accept_link); |
| 417 | sk_acceptq_removed(&rx->sk); |
| 418 | |
| 419 | write_lock_bh(&call->state_lock); |
| 420 | switch (call->state) { |
| 421 | case RXRPC_CALL_SERVER_ACCEPTING: |
| 422 | call->state = RXRPC_CALL_SERVER_BUSY; |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 423 | if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 424 | rxrpc_queue_call(call); |
| 425 | ret = 0; |
| 426 | goto out_release; |
| 427 | case RXRPC_CALL_REMOTELY_ABORTED: |
| 428 | case RXRPC_CALL_LOCALLY_ABORTED: |
| 429 | ret = -ECONNABORTED; |
| 430 | goto out_release; |
| 431 | case RXRPC_CALL_NETWORK_ERROR: |
| 432 | ret = call->conn->error; |
| 433 | goto out_release; |
| 434 | case RXRPC_CALL_DEAD: |
| 435 | ret = -ETIME; |
| 436 | goto out_discard; |
| 437 | default: |
| 438 | BUG(); |
| 439 | } |
| 440 | |
| 441 | /* if the call is already dying or dead, then we leave the socket's ref |
| 442 | * on it to be released by rxrpc_dead_call_expired() as induced by |
| 443 | * rxrpc_release_call() */ |
| 444 | out_release: |
| 445 | _debug("release %p", call); |
| 446 | if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) && |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 447 | !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 448 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 449 | out_discard: |
| 450 | write_unlock_bh(&call->state_lock); |
| 451 | _debug("discard %p", call); |
| 452 | out: |
| 453 | write_unlock(&rx->call_lock); |
| 454 | _leave(" = %d", ret); |
| 455 | return ret; |
| 456 | } |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 457 | |
| 458 | /** |
| 459 | * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call |
| 460 | * @sock: The socket on which the impending call is waiting |
| 461 | * @user_call_ID: The tag to attach to the call |
| 462 | * |
| 463 | * Allow a kernel service to accept an incoming call, assuming the incoming |
| 464 | * call is still valid. |
| 465 | */ |
| 466 | struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock, |
| 467 | unsigned long user_call_ID) |
| 468 | { |
| 469 | struct rxrpc_call *call; |
| 470 | |
| 471 | _enter(",%lx", user_call_ID); |
| 472 | call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID); |
| 473 | _leave(" = %p", call); |
| 474 | return call; |
| 475 | } |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 476 | EXPORT_SYMBOL(rxrpc_kernel_accept_call); |
| 477 | |
| 478 | /** |
| 479 | * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call |
| 480 | * @sock: The socket on which the impending call is waiting |
| 481 | * |
| 482 | * Allow a kernel service to reject an incoming call with a BUSY message, |
| 483 | * assuming the incoming call is still valid. |
| 484 | */ |
| 485 | int rxrpc_kernel_reject_call(struct socket *sock) |
| 486 | { |
| 487 | int ret; |
| 488 | |
| 489 | _enter(""); |
| 490 | ret = rxrpc_reject_call(rxrpc_sk(sock->sk)); |
| 491 | _leave(" = %d", ret); |
| 492 | return ret; |
| 493 | } |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 494 | EXPORT_SYMBOL(rxrpc_kernel_reject_call); |