1 /* incoming call handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
20 #include <linux/in6.h>
21 #include <linux/icmp.h>
22 #include <linux/gfp.h>
24 #include <net/af_rxrpc.h>
26 #include "ar-internal.h"
29 * generate a connection-level abort
31 static int rxrpc_busy(struct rxrpc_local *local, struct sockaddr_rxrpc *srx,
32 struct rxrpc_wire_header *whdr)
39 _enter("%d,,", local->debug_id);
41 whdr->type = RXRPC_PACKET_TYPE_BUSY;
42 whdr->serial = htonl(1);
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;
50 iov[0].iov_base = whdr;
51 iov[0].iov_len = sizeof(*whdr);
55 _proto("Tx BUSY %%1");
57 ret = kernel_sendmsg(local->socket, &msg, iov, 1, len);
59 _leave(" = -EAGAIN [sendmsg failed: %d]", ret);
68 * accept an incoming call that needs peer, transport and/or connection setting
71 static int rxrpc_accept_incoming_call(struct rxrpc_local *local,
72 struct rxrpc_sock *rx,
74 struct sockaddr_rxrpc *srx)
76 struct rxrpc_connection *conn;
77 struct rxrpc_skb_priv *sp, *nsp;
78 struct rxrpc_peer *peer;
79 struct rxrpc_call *call;
80 struct sk_buff *notification;
87 /* get a notification message to send to the server app */
88 notification = alloc_skb(0, GFP_NOFS);
94 rxrpc_new_skb(notification);
95 notification->mark = RXRPC_SKB_MARK_NEW_CALL;
97 peer = rxrpc_lookup_peer(local, srx, GFP_NOIO);
104 conn = rxrpc_incoming_connection(local, peer, skb);
105 rxrpc_put_peer(peer);
112 call = rxrpc_incoming_call(rx, conn, skb);
113 rxrpc_put_connection(conn);
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;
125 write_lock(&rx->call_lock);
126 if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
127 rxrpc_get_call(call);
129 spin_lock(&call->conn->state_lock);
130 if (sp->hdr.securityIndex > 0 &&
131 call->conn->state == RXRPC_CONN_SERVER_UNSECURED) {
132 _debug("await conn sec");
133 list_add_tail(&call->accept_link, &rx->secureq);
134 call->conn->state = RXRPC_CONN_SERVER_CHALLENGING;
135 rxrpc_get_connection(call->conn);
136 set_bit(RXRPC_CONN_CHALLENGE, &call->conn->events);
137 rxrpc_queue_conn(call->conn);
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);
146 ASSERTCMP(atomic_read(&call->usage), >=, 3);
149 spin_lock(&call->lock);
150 ret = rxrpc_queue_rcv_skb(call, notification, true,
152 spin_unlock(&call->lock);
156 spin_unlock(&call->conn->state_lock);
160 write_unlock(&rx->call_lock);
163 rxrpc_fast_process_packet(call, skb);
166 read_unlock_bh(&local->services_lock);
167 rxrpc_free_skb(notification);
168 rxrpc_put_call(call);
174 read_unlock_bh(&local->services_lock);
176 read_lock_bh(&call->state_lock);
177 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
178 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
179 rxrpc_get_call(call);
180 rxrpc_queue_call(call);
182 read_unlock_bh(&call->state_lock);
183 rxrpc_put_call(call);
186 rxrpc_free_skb(notification);
188 _leave(" = %d", ret);
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
196 void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
198 struct rxrpc_skb_priv *sp;
199 struct sockaddr_rxrpc srx;
200 struct rxrpc_sock *rx;
201 struct rxrpc_wire_header whdr;
205 _enter("%d", local->debug_id);
207 skb = skb_dequeue(&local->accept_queue);
213 _net("incoming call skb %p", skb);
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);
226 whdr.securityIndex = sp->hdr.securityIndex;
228 whdr.serviceId = htons(sp->hdr.serviceId);
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) {
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;
245 /* get the socket providing the service */
246 read_lock_bh(&local->services_lock);
247 list_for_each_entry(rx, &local->services, listen_link) {
248 if (rx->srx.srx_service == sp->hdr.serviceId &&
249 rx->sk.sk_state != RXRPC_CLOSE)
252 read_unlock_bh(&local->services_lock);
253 goto invalid_service;
256 _debug("found service %hd", rx->srx.srx_service);
257 if (sk_acceptq_is_full(&rx->sk))
259 sk_acceptq_added(&rx->sk);
261 read_unlock_bh(&local->services_lock);
263 ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
265 sk_acceptq_removed(&rx->sk);
268 case -ECONNRESET: /* old calls are ignored */
269 case -ECONNABORTED: /* aborted calls are reaborted or ignored */
273 goto invalid_service;
277 goto security_mismatch;
283 read_unlock_bh(&local->services_lock);
285 rxrpc_busy(local, &srx, &whdr);
290 skb->priority = RX_INVALID_OPERATION;
291 rxrpc_reject_packet(local, skb);
294 /* can't change connection security type mid-flow */
296 skb->priority = RX_PROTOCOL_ERROR;
297 rxrpc_reject_packet(local, skb);
302 * handle acceptance of a call by userspace
303 * - assign the user call ID to the call at the front of the queue
305 struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
306 unsigned long user_call_ID)
308 struct rxrpc_call *call;
309 struct rb_node *parent, **pp;
312 _enter(",%lx", user_call_ID);
314 ASSERT(!irqs_disabled());
316 write_lock(&rx->call_lock);
319 if (list_empty(&rx->acceptq))
322 /* check the user ID isn't already in use */
324 pp = &rx->calls.rb_node;
328 call = rb_entry(parent, struct rxrpc_call, sock_node);
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;
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);
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;
348 case RXRPC_CALL_REMOTELY_ABORTED:
349 case RXRPC_CALL_LOCALLY_ABORTED:
352 case RXRPC_CALL_NETWORK_ERROR:
353 ret = call->conn->error;
355 case RXRPC_CALL_DEAD:
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))
368 if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
370 rxrpc_queue_call(call);
372 rxrpc_get_call(call);
373 write_unlock_bh(&call->state_lock);
374 write_unlock(&rx->call_lock);
375 _leave(" = %p{%d}", call, call->debug_id);
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() */
382 _debug("release %p", call);
383 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
384 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
385 rxrpc_queue_call(call);
387 write_unlock_bh(&call->state_lock);
388 _debug("discard %p", call);
390 write_unlock(&rx->call_lock);
391 _leave(" = %d", ret);
396 * Handle rejection of a call by userspace
397 * - reject the call at the front of the queue
399 int rxrpc_reject_call(struct rxrpc_sock *rx)
401 struct rxrpc_call *call;
406 ASSERT(!irqs_disabled());
408 write_lock(&rx->call_lock);
411 if (list_empty(&rx->acceptq))
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);
419 write_lock_bh(&call->state_lock);
420 switch (call->state) {
421 case RXRPC_CALL_SERVER_ACCEPTING:
422 call->state = RXRPC_CALL_SERVER_BUSY;
423 if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
424 rxrpc_queue_call(call);
427 case RXRPC_CALL_REMOTELY_ABORTED:
428 case RXRPC_CALL_LOCALLY_ABORTED:
431 case RXRPC_CALL_NETWORK_ERROR:
432 ret = call->conn->error;
434 case RXRPC_CALL_DEAD:
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() */
445 _debug("release %p", call);
446 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
447 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
448 rxrpc_queue_call(call);
450 write_unlock_bh(&call->state_lock);
451 _debug("discard %p", call);
453 write_unlock(&rx->call_lock);
454 _leave(" = %d", ret);
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
463 * Allow a kernel service to accept an incoming call, assuming the incoming
464 * call is still valid.
466 struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
467 unsigned long user_call_ID)
469 struct rxrpc_call *call;
471 _enter(",%lx", user_call_ID);
472 call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
473 _leave(" = %p", call);
476 EXPORT_SYMBOL(rxrpc_kernel_accept_call);
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
482 * Allow a kernel service to reject an incoming call with a BUSY message,
483 * assuming the incoming call is still valid.
485 int rxrpc_kernel_reject_call(struct socket *sock)
490 ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
491 _leave(" = %d", ret);
494 EXPORT_SYMBOL(rxrpc_kernel_reject_call);