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 12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 13 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> 22 #include <linux/gfp.h> 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, 32 struct rxrpc_wire_header *whdr) 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 41 whdr->type = RXRPC_PACKET_TYPE_BUSY; 42 whdr->serial = htonl(1); 43 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 50 iov[0].iov_base = whdr; 51 iov[0].iov_len = sizeof(*whdr); 52 53 len = iov[0].iov_len; 54 55 _proto("Tx BUSY %%1"); 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; 77 struct rxrpc_transport *trans; 78 struct rxrpc_skb_priv *sp, *nsp; 79 struct rxrpc_peer *peer; 80 struct rxrpc_call *call; 81 struct sk_buff *notification; 82 int ret; 83 84 _enter(""); 85 86 sp = rxrpc_skb(skb); 87 88 /* get a notification message to send to the server app */ 89 notification = alloc_skb(0, GFP_NOFS); 90 if (!notification) { 91 _debug("no memory"); 92 ret = -ENOMEM; 93 goto error_nofree; 94 } 95 rxrpc_new_skb(notification); 96 notification->mark = RXRPC_SKB_MARK_NEW_CALL; 97 98 peer = rxrpc_get_peer(srx, GFP_NOIO); 99 if (IS_ERR(peer)) { 100 _debug("no peer"); 101 ret = -EBUSY; 102 goto error; 103 } 104 105 trans = rxrpc_get_transport(local, peer, GFP_NOIO); 106 rxrpc_put_peer(peer); 107 if (IS_ERR(trans)) { 108 _debug("no trans"); 109 ret = -EBUSY; 110 goto error; 111 } 112 113 conn = rxrpc_incoming_connection(trans, &sp->hdr); 114 rxrpc_put_transport(trans); 115 if (IS_ERR(conn)) { 116 _debug("no conn"); 117 ret = PTR_ERR(conn); 118 goto error; 119 } 120 121 call = rxrpc_incoming_call(rx, conn, &sp->hdr); 122 rxrpc_put_connection(conn); 123 if (IS_ERR(call)) { 124 _debug("no call"); 125 ret = PTR_ERR(call); 126 goto error; 127 } 128 129 /* attach the call to the socket */ 130 read_lock_bh(&local->services_lock); 131 if (rx->sk.sk_state == RXRPC_CLOSE) 132 goto invalid_service; 133 134 write_lock(&rx->call_lock); 135 if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) { 136 rxrpc_get_call(call); 137 138 spin_lock(&call->conn->state_lock); 139 if (sp->hdr.securityIndex > 0 && 140 call->conn->state == RXRPC_CONN_SERVER_UNSECURED) { 141 _debug("await conn sec"); 142 list_add_tail(&call->accept_link, &rx->secureq); 143 call->conn->state = RXRPC_CONN_SERVER_CHALLENGING; 144 atomic_inc(&call->conn->usage); 145 set_bit(RXRPC_CONN_CHALLENGE, &call->conn->events); 146 rxrpc_queue_conn(call->conn); 147 } else { 148 _debug("conn ready"); 149 call->state = RXRPC_CALL_SERVER_ACCEPTING; 150 list_add_tail(&call->accept_link, &rx->acceptq); 151 rxrpc_get_call(call); 152 nsp = rxrpc_skb(notification); 153 nsp->call = call; 154 155 ASSERTCMP(atomic_read(&call->usage), >=, 3); 156 157 _debug("notify"); 158 spin_lock(&call->lock); 159 ret = rxrpc_queue_rcv_skb(call, notification, true, 160 false); 161 spin_unlock(&call->lock); 162 notification = NULL; 163 BUG_ON(ret < 0); 164 } 165 spin_unlock(&call->conn->state_lock); 166 167 _debug("queued"); 168 } 169 write_unlock(&rx->call_lock); 170 171 _debug("process"); 172 rxrpc_fast_process_packet(call, skb); 173 174 _debug("done"); 175 read_unlock_bh(&local->services_lock); 176 rxrpc_free_skb(notification); 177 rxrpc_put_call(call); 178 _leave(" = 0"); 179 return 0; 180 181 invalid_service: 182 _debug("invalid"); 183 read_unlock_bh(&local->services_lock); 184 185 read_lock_bh(&call->state_lock); 186 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) && 187 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) { 188 rxrpc_get_call(call); 189 rxrpc_queue_call(call); 190 } 191 read_unlock_bh(&call->state_lock); 192 rxrpc_put_call(call); 193 ret = -ECONNREFUSED; 194 error: 195 rxrpc_free_skb(notification); 196 error_nofree: 197 _leave(" = %d", ret); 198 return ret; 199 } 200 201 /* 202 * accept incoming calls that need peer, transport and/or connection setting up 203 * - the packets we get are all incoming client DATA packets that have seq == 1 204 */ 205 void rxrpc_accept_incoming_calls(struct work_struct *work) 206 { 207 struct rxrpc_local *local = 208 container_of(work, struct rxrpc_local, acceptor); 209 struct rxrpc_skb_priv *sp; 210 struct sockaddr_rxrpc srx; 211 struct rxrpc_sock *rx; 212 struct rxrpc_wire_header whdr; 213 struct sk_buff *skb; 214 int ret; 215 216 _enter("%d", local->debug_id); 217 218 read_lock_bh(&rxrpc_local_lock); 219 if (atomic_read(&local->usage) > 0) 220 rxrpc_get_local(local); 221 else 222 local = NULL; 223 read_unlock_bh(&rxrpc_local_lock); 224 if (!local) { 225 _leave(" [local dead]"); 226 return; 227 } 228 229 process_next_packet: 230 skb = skb_dequeue(&local->accept_queue); 231 if (!skb) { 232 rxrpc_put_local(local); 233 _leave("\n"); 234 return; 235 } 236 237 _net("incoming call skb %p", skb); 238 239 sp = rxrpc_skb(skb); 240 241 /* Set up a response packet header in case we need it */ 242 whdr.epoch = htonl(sp->hdr.epoch); 243 whdr.cid = htonl(sp->hdr.cid); 244 whdr.callNumber = htonl(sp->hdr.callNumber); 245 whdr.seq = htonl(sp->hdr.seq); 246 whdr.serial = 0; 247 whdr.flags = 0; 248 whdr.type = 0; 249 whdr.userStatus = 0; 250 whdr.securityIndex = sp->hdr.securityIndex; 251 whdr._rsvd = 0; 252 whdr.serviceId = htons(sp->hdr.serviceId); 253 254 /* determine the remote address */ 255 memset(&srx, 0, sizeof(srx)); 256 srx.srx_family = AF_RXRPC; 257 srx.transport.family = local->srx.transport.family; 258 srx.transport_type = local->srx.transport_type; 259 switch (srx.transport.family) { 260 case AF_INET: 261 srx.transport_len = sizeof(struct sockaddr_in); 262 srx.transport.sin.sin_port = udp_hdr(skb)->source; 263 srx.transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr; 264 break; 265 default: 266 goto busy; 267 } 268 269 /* get the socket providing the service */ 270 read_lock_bh(&local->services_lock); 271 list_for_each_entry(rx, &local->services, listen_link) { 272 if (rx->srx.srx_service == sp->hdr.serviceId && 273 rx->sk.sk_state != RXRPC_CLOSE) 274 goto found_service; 275 } 276 read_unlock_bh(&local->services_lock); 277 goto invalid_service; 278 279 found_service: 280 _debug("found service %hd", rx->srx.srx_service); 281 if (sk_acceptq_is_full(&rx->sk)) 282 goto backlog_full; 283 sk_acceptq_added(&rx->sk); 284 sock_hold(&rx->sk); 285 read_unlock_bh(&local->services_lock); 286 287 ret = rxrpc_accept_incoming_call(local, rx, skb, &srx); 288 if (ret < 0) 289 sk_acceptq_removed(&rx->sk); 290 sock_put(&rx->sk); 291 switch (ret) { 292 case -ECONNRESET: /* old calls are ignored */ 293 case -ECONNABORTED: /* aborted calls are reaborted or ignored */ 294 case 0: 295 goto process_next_packet; 296 case -ECONNREFUSED: 297 goto invalid_service; 298 case -EBUSY: 299 goto busy; 300 case -EKEYREJECTED: 301 goto security_mismatch; 302 default: 303 BUG(); 304 } 305 306 backlog_full: 307 read_unlock_bh(&local->services_lock); 308 busy: 309 rxrpc_busy(local, &srx, &whdr); 310 rxrpc_free_skb(skb); 311 goto process_next_packet; 312 313 invalid_service: 314 skb->priority = RX_INVALID_OPERATION; 315 rxrpc_reject_packet(local, skb); 316 goto process_next_packet; 317 318 /* can't change connection security type mid-flow */ 319 security_mismatch: 320 skb->priority = RX_PROTOCOL_ERROR; 321 rxrpc_reject_packet(local, skb); 322 goto process_next_packet; 323 } 324 325 /* 326 * handle acceptance of a call by userspace 327 * - assign the user call ID to the call at the front of the queue 328 */ 329 struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx, 330 unsigned long user_call_ID) 331 { 332 struct rxrpc_call *call; 333 struct rb_node *parent, **pp; 334 int ret; 335 336 _enter(",%lx", user_call_ID); 337 338 ASSERT(!irqs_disabled()); 339 340 write_lock(&rx->call_lock); 341 342 ret = -ENODATA; 343 if (list_empty(&rx->acceptq)) 344 goto out; 345 346 /* check the user ID isn't already in use */ 347 ret = -EBADSLT; 348 pp = &rx->calls.rb_node; 349 parent = NULL; 350 while (*pp) { 351 parent = *pp; 352 call = rb_entry(parent, struct rxrpc_call, sock_node); 353 354 if (user_call_ID < call->user_call_ID) 355 pp = &(*pp)->rb_left; 356 else if (user_call_ID > call->user_call_ID) 357 pp = &(*pp)->rb_right; 358 else 359 goto out; 360 } 361 362 /* dequeue the first call and check it's still valid */ 363 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link); 364 list_del_init(&call->accept_link); 365 sk_acceptq_removed(&rx->sk); 366 367 write_lock_bh(&call->state_lock); 368 switch (call->state) { 369 case RXRPC_CALL_SERVER_ACCEPTING: 370 call->state = RXRPC_CALL_SERVER_RECV_REQUEST; 371 break; 372 case RXRPC_CALL_REMOTELY_ABORTED: 373 case RXRPC_CALL_LOCALLY_ABORTED: 374 ret = -ECONNABORTED; 375 goto out_release; 376 case RXRPC_CALL_NETWORK_ERROR: 377 ret = call->conn->error; 378 goto out_release; 379 case RXRPC_CALL_DEAD: 380 ret = -ETIME; 381 goto out_discard; 382 default: 383 BUG(); 384 } 385 386 /* formalise the acceptance */ 387 call->user_call_ID = user_call_ID; 388 rb_link_node(&call->sock_node, parent, pp); 389 rb_insert_color(&call->sock_node, &rx->calls); 390 if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags)) 391 BUG(); 392 if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events)) 393 BUG(); 394 rxrpc_queue_call(call); 395 396 rxrpc_get_call(call); 397 write_unlock_bh(&call->state_lock); 398 write_unlock(&rx->call_lock); 399 _leave(" = %p{%d}", call, call->debug_id); 400 return call; 401 402 /* if the call is already dying or dead, then we leave the socket's ref 403 * on it to be released by rxrpc_dead_call_expired() as induced by 404 * rxrpc_release_call() */ 405 out_release: 406 _debug("release %p", call); 407 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) && 408 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) 409 rxrpc_queue_call(call); 410 out_discard: 411 write_unlock_bh(&call->state_lock); 412 _debug("discard %p", call); 413 out: 414 write_unlock(&rx->call_lock); 415 _leave(" = %d", ret); 416 return ERR_PTR(ret); 417 } 418 419 /* 420 * Handle rejection of a call by userspace 421 * - reject the call at the front of the queue 422 */ 423 int rxrpc_reject_call(struct rxrpc_sock *rx) 424 { 425 struct rxrpc_call *call; 426 int ret; 427 428 _enter(""); 429 430 ASSERT(!irqs_disabled()); 431 432 write_lock(&rx->call_lock); 433 434 ret = -ENODATA; 435 if (list_empty(&rx->acceptq)) 436 goto out; 437 438 /* dequeue the first call and check it's still valid */ 439 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link); 440 list_del_init(&call->accept_link); 441 sk_acceptq_removed(&rx->sk); 442 443 write_lock_bh(&call->state_lock); 444 switch (call->state) { 445 case RXRPC_CALL_SERVER_ACCEPTING: 446 call->state = RXRPC_CALL_SERVER_BUSY; 447 if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events)) 448 rxrpc_queue_call(call); 449 ret = 0; 450 goto out_release; 451 case RXRPC_CALL_REMOTELY_ABORTED: 452 case RXRPC_CALL_LOCALLY_ABORTED: 453 ret = -ECONNABORTED; 454 goto out_release; 455 case RXRPC_CALL_NETWORK_ERROR: 456 ret = call->conn->error; 457 goto out_release; 458 case RXRPC_CALL_DEAD: 459 ret = -ETIME; 460 goto out_discard; 461 default: 462 BUG(); 463 } 464 465 /* if the call is already dying or dead, then we leave the socket's ref 466 * on it to be released by rxrpc_dead_call_expired() as induced by 467 * rxrpc_release_call() */ 468 out_release: 469 _debug("release %p", call); 470 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) && 471 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) 472 rxrpc_queue_call(call); 473 out_discard: 474 write_unlock_bh(&call->state_lock); 475 _debug("discard %p", call); 476 out: 477 write_unlock(&rx->call_lock); 478 _leave(" = %d", ret); 479 return ret; 480 } 481 482 /** 483 * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call 484 * @sock: The socket on which the impending call is waiting 485 * @user_call_ID: The tag to attach to the call 486 * 487 * Allow a kernel service to accept an incoming call, assuming the incoming 488 * call is still valid. 489 */ 490 struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock, 491 unsigned long user_call_ID) 492 { 493 struct rxrpc_call *call; 494 495 _enter(",%lx", user_call_ID); 496 call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID); 497 _leave(" = %p", call); 498 return call; 499 } 500 EXPORT_SYMBOL(rxrpc_kernel_accept_call); 501 502 /** 503 * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call 504 * @sock: The socket on which the impending call is waiting 505 * 506 * Allow a kernel service to reject an incoming call with a BUSY message, 507 * assuming the incoming call is still valid. 508 */ 509 int rxrpc_kernel_reject_call(struct socket *sock) 510 { 511 int ret; 512 513 _enter(""); 514 ret = rxrpc_reject_call(rxrpc_sk(sock->sk)); 515 _leave(" = %d", ret); 516 return ret; 517 } 518 EXPORT_SYMBOL(rxrpc_kernel_reject_call); 519