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_transport
*trans
;
78 struct rxrpc_skb_priv
*sp
, *nsp
;
79 struct rxrpc_peer
*peer
;
80 struct rxrpc_call
*call
;
81 struct sk_buff
*notification
;
88 /* get a notification message to send to the server app */
89 notification
= alloc_skb(0, GFP_NOFS
);
95 rxrpc_new_skb(notification
);
96 notification
->mark
= RXRPC_SKB_MARK_NEW_CALL
;
98 peer
= rxrpc_lookup_peer(local
, srx
, GFP_NOIO
);
105 trans
= rxrpc_get_transport(local
, peer
, GFP_NOIO
);
106 rxrpc_put_peer(peer
);
113 conn
= rxrpc_incoming_connection(trans
, &sp
->hdr
);
114 rxrpc_put_transport(trans
);
121 call
= rxrpc_incoming_call(rx
, conn
, &sp
->hdr
);
122 rxrpc_put_connection(conn
);
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
;
134 write_lock(&rx
->call_lock
);
135 if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT
, &call
->flags
)) {
136 rxrpc_get_call(call
);
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
);
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
);
155 ASSERTCMP(atomic_read(&call
->usage
), >=, 3);
158 spin_lock(&call
->lock
);
159 ret
= rxrpc_queue_rcv_skb(call
, notification
, true,
161 spin_unlock(&call
->lock
);
165 spin_unlock(&call
->conn
->state_lock
);
169 write_unlock(&rx
->call_lock
);
172 rxrpc_fast_process_packet(call
, skb
);
175 read_unlock_bh(&local
->services_lock
);
176 rxrpc_free_skb(notification
);
177 rxrpc_put_call(call
);
183 read_unlock_bh(&local
->services_lock
);
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
);
191 read_unlock_bh(&call
->state_lock
);
192 rxrpc_put_call(call
);
195 rxrpc_free_skb(notification
);
197 _leave(" = %d", ret
);
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
205 void rxrpc_accept_incoming_calls(struct work_struct
*work
)
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
;
216 _enter("%d", local
->debug_id
);
218 read_lock_bh(&rxrpc_local_lock
);
219 if (atomic_read(&local
->usage
) > 0)
220 rxrpc_get_local(local
);
223 read_unlock_bh(&rxrpc_local_lock
);
225 _leave(" [local dead]");
230 skb
= skb_dequeue(&local
->accept_queue
);
232 rxrpc_put_local(local
);
237 _net("incoming call skb %p", skb
);
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
);
250 whdr
.securityIndex
= sp
->hdr
.securityIndex
;
252 whdr
.serviceId
= htons(sp
->hdr
.serviceId
);
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
) {
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
;
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
)
276 read_unlock_bh(&local
->services_lock
);
277 goto invalid_service
;
280 _debug("found service %hd", rx
->srx
.srx_service
);
281 if (sk_acceptq_is_full(&rx
->sk
))
283 sk_acceptq_added(&rx
->sk
);
285 read_unlock_bh(&local
->services_lock
);
287 ret
= rxrpc_accept_incoming_call(local
, rx
, skb
, &srx
);
289 sk_acceptq_removed(&rx
->sk
);
292 case -ECONNRESET
: /* old calls are ignored */
293 case -ECONNABORTED
: /* aborted calls are reaborted or ignored */
295 goto process_next_packet
;
297 goto invalid_service
;
301 goto security_mismatch
;
307 read_unlock_bh(&local
->services_lock
);
309 rxrpc_busy(local
, &srx
, &whdr
);
311 goto process_next_packet
;
314 skb
->priority
= RX_INVALID_OPERATION
;
315 rxrpc_reject_packet(local
, skb
);
316 goto process_next_packet
;
318 /* can't change connection security type mid-flow */
320 skb
->priority
= RX_PROTOCOL_ERROR
;
321 rxrpc_reject_packet(local
, skb
);
322 goto process_next_packet
;
326 * handle acceptance of a call by userspace
327 * - assign the user call ID to the call at the front of the queue
329 struct rxrpc_call
*rxrpc_accept_call(struct rxrpc_sock
*rx
,
330 unsigned long user_call_ID
)
332 struct rxrpc_call
*call
;
333 struct rb_node
*parent
, **pp
;
336 _enter(",%lx", user_call_ID
);
338 ASSERT(!irqs_disabled());
340 write_lock(&rx
->call_lock
);
343 if (list_empty(&rx
->acceptq
))
346 /* check the user ID isn't already in use */
348 pp
= &rx
->calls
.rb_node
;
352 call
= rb_entry(parent
, struct rxrpc_call
, sock_node
);
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
;
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
);
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
;
372 case RXRPC_CALL_REMOTELY_ABORTED
:
373 case RXRPC_CALL_LOCALLY_ABORTED
:
376 case RXRPC_CALL_NETWORK_ERROR
:
377 ret
= call
->conn
->error
;
379 case RXRPC_CALL_DEAD
:
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
))
392 if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED
, &call
->events
))
394 rxrpc_queue_call(call
);
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
);
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() */
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
);
411 write_unlock_bh(&call
->state_lock
);
412 _debug("discard %p", call
);
414 write_unlock(&rx
->call_lock
);
415 _leave(" = %d", ret
);
420 * Handle rejection of a call by userspace
421 * - reject the call at the front of the queue
423 int rxrpc_reject_call(struct rxrpc_sock
*rx
)
425 struct rxrpc_call
*call
;
430 ASSERT(!irqs_disabled());
432 write_lock(&rx
->call_lock
);
435 if (list_empty(&rx
->acceptq
))
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
);
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
);
451 case RXRPC_CALL_REMOTELY_ABORTED
:
452 case RXRPC_CALL_LOCALLY_ABORTED
:
455 case RXRPC_CALL_NETWORK_ERROR
:
456 ret
= call
->conn
->error
;
458 case RXRPC_CALL_DEAD
:
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() */
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
);
474 write_unlock_bh(&call
->state_lock
);
475 _debug("discard %p", call
);
477 write_unlock(&rx
->call_lock
);
478 _leave(" = %d", ret
);
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
487 * Allow a kernel service to accept an incoming call, assuming the incoming
488 * call is still valid.
490 struct rxrpc_call
*rxrpc_kernel_accept_call(struct socket
*sock
,
491 unsigned long user_call_ID
)
493 struct rxrpc_call
*call
;
495 _enter(",%lx", user_call_ID
);
496 call
= rxrpc_accept_call(rxrpc_sk(sock
->sk
), user_call_ID
);
497 _leave(" = %p", call
);
500 EXPORT_SYMBOL(rxrpc_kernel_accept_call
);
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
506 * Allow a kernel service to reject an incoming call with a BUSY message,
507 * assuming the incoming call is still valid.
509 int rxrpc_kernel_reject_call(struct socket
*sock
)
514 ret
= rxrpc_reject_call(rxrpc_sk(sock
->sk
));
515 _leave(" = %d", ret
);
518 EXPORT_SYMBOL(rxrpc_kernel_reject_call
);