Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / rxrpc / ar-accept.c
blobf43b30c30780cb71440216157d890f2f55173714
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 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/errqueue.h>
16 #include <linux/udp.h>
17 #include <linux/in.h>
18 #include <linux/in6.h>
19 #include <linux/icmp.h>
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #include <net/ip.h>
23 #include "ar-internal.h"
26 * generate a connection-level abort
28 static int rxrpc_busy(struct rxrpc_local *local, struct sockaddr_rxrpc *srx,
29 struct rxrpc_header *hdr)
31 struct msghdr msg;
32 struct kvec iov[1];
33 size_t len;
34 int ret;
36 _enter("%d,,", local->debug_id);
38 msg.msg_name = &srx->transport.sin;
39 msg.msg_namelen = sizeof(srx->transport.sin);
40 msg.msg_control = NULL;
41 msg.msg_controllen = 0;
42 msg.msg_flags = 0;
44 hdr->seq = 0;
45 hdr->type = RXRPC_PACKET_TYPE_BUSY;
46 hdr->flags = 0;
47 hdr->userStatus = 0;
48 hdr->_rsvd = 0;
50 iov[0].iov_base = hdr;
51 iov[0].iov_len = sizeof(*hdr);
53 len = iov[0].iov_len;
55 hdr->serial = htonl(1);
56 _proto("Tx BUSY %%%u", ntohl(hdr->serial));
58 ret = kernel_sendmsg(local->socket, &msg, iov, 1, len);
59 if (ret < 0) {
60 _leave(" = -EAGAIN [sendmsg failed: %d]", ret);
61 return -EAGAIN;
64 _leave(" = 0");
65 return 0;
69 * accept an incoming call that needs peer, transport and/or connection setting
70 * up
72 static int rxrpc_accept_incoming_call(struct rxrpc_local *local,
73 struct rxrpc_sock *rx,
74 struct sk_buff *skb,
75 struct sockaddr_rxrpc *srx)
77 struct rxrpc_connection *conn;
78 struct rxrpc_transport *trans;
79 struct rxrpc_skb_priv *sp, *nsp;
80 struct rxrpc_peer *peer;
81 struct rxrpc_call *call;
82 struct sk_buff *notification;
83 int ret;
85 _enter("");
87 sp = rxrpc_skb(skb);
89 /* get a notification message to send to the server app */
90 notification = alloc_skb(0, GFP_NOFS);
91 rxrpc_new_skb(notification);
92 notification->mark = RXRPC_SKB_MARK_NEW_CALL;
94 peer = rxrpc_get_peer(srx, GFP_NOIO);
95 if (IS_ERR(peer)) {
96 _debug("no peer");
97 ret = -EBUSY;
98 goto error;
101 trans = rxrpc_get_transport(local, peer, GFP_NOIO);
102 rxrpc_put_peer(peer);
103 if (!trans) {
104 _debug("no trans");
105 ret = -EBUSY;
106 goto error;
109 conn = rxrpc_incoming_connection(trans, &sp->hdr, GFP_NOIO);
110 rxrpc_put_transport(trans);
111 if (IS_ERR(conn)) {
112 _debug("no conn");
113 ret = PTR_ERR(conn);
114 goto error;
117 call = rxrpc_incoming_call(rx, conn, &sp->hdr, GFP_NOIO);
118 rxrpc_put_connection(conn);
119 if (IS_ERR(call)) {
120 _debug("no call");
121 ret = PTR_ERR(call);
122 goto error;
125 /* attach the call to the socket */
126 read_lock_bh(&local->services_lock);
127 if (rx->sk.sk_state == RXRPC_CLOSE)
128 goto invalid_service;
130 write_lock(&rx->call_lock);
131 if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
132 rxrpc_get_call(call);
134 spin_lock(&call->conn->state_lock);
135 if (sp->hdr.securityIndex > 0 &&
136 call->conn->state == RXRPC_CONN_SERVER_UNSECURED) {
137 _debug("await conn sec");
138 list_add_tail(&call->accept_link, &rx->secureq);
139 call->conn->state = RXRPC_CONN_SERVER_CHALLENGING;
140 atomic_inc(&call->conn->usage);
141 set_bit(RXRPC_CONN_CHALLENGE, &call->conn->events);
142 rxrpc_queue_conn(call->conn);
143 } else {
144 _debug("conn ready");
145 call->state = RXRPC_CALL_SERVER_ACCEPTING;
146 list_add_tail(&call->accept_link, &rx->acceptq);
147 rxrpc_get_call(call);
148 nsp = rxrpc_skb(notification);
149 nsp->call = call;
151 ASSERTCMP(atomic_read(&call->usage), >=, 3);
153 _debug("notify");
154 spin_lock(&call->lock);
155 ret = rxrpc_queue_rcv_skb(call, notification, true,
156 false);
157 spin_unlock(&call->lock);
158 notification = NULL;
159 <<<<<<< HEAD:net/rxrpc/ar-accept.c
160 if (ret < 0)
161 BUG();
162 =======
163 BUG_ON(ret < 0);
164 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/rxrpc/ar-accept.c
166 spin_unlock(&call->conn->state_lock);
168 _debug("queued");
170 write_unlock(&rx->call_lock);
172 _debug("process");
173 rxrpc_fast_process_packet(call, skb);
175 _debug("done");
176 read_unlock_bh(&local->services_lock);
177 rxrpc_free_skb(notification);
178 rxrpc_put_call(call);
179 _leave(" = 0");
180 return 0;
182 invalid_service:
183 _debug("invalid");
184 read_unlock_bh(&local->services_lock);
186 read_lock_bh(&call->state_lock);
187 if (!test_bit(RXRPC_CALL_RELEASE, &call->flags) &&
188 !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events)) {
189 rxrpc_get_call(call);
190 rxrpc_queue_call(call);
192 read_unlock_bh(&call->state_lock);
193 rxrpc_put_call(call);
194 ret = -ECONNREFUSED;
195 error:
196 rxrpc_free_skb(notification);
197 _leave(" = %d", ret);
198 return 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 sk_buff *skb;
213 __be16 service_id;
214 int ret;
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);
221 else
222 local = NULL;
223 read_unlock_bh(&rxrpc_local_lock);
224 if (!local) {
225 _leave(" [local dead]");
226 return;
229 process_next_packet:
230 skb = skb_dequeue(&local->accept_queue);
231 if (!skb) {
232 rxrpc_put_local(local);
233 _leave("\n");
234 return;
237 _net("incoming call skb %p", skb);
239 sp = rxrpc_skb(skb);
241 /* determine the remote address */
242 memset(&srx, 0, sizeof(srx));
243 srx.srx_family = AF_RXRPC;
244 srx.transport.family = local->srx.transport.family;
245 srx.transport_type = local->srx.transport_type;
246 switch (srx.transport.family) {
247 case AF_INET:
248 srx.transport_len = sizeof(struct sockaddr_in);
249 srx.transport.sin.sin_port = udp_hdr(skb)->source;
250 srx.transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
251 break;
252 default:
253 goto busy;
256 /* get the socket providing the service */
257 service_id = sp->hdr.serviceId;
258 read_lock_bh(&local->services_lock);
259 list_for_each_entry(rx, &local->services, listen_link) {
260 if (rx->service_id == service_id &&
261 rx->sk.sk_state != RXRPC_CLOSE)
262 goto found_service;
264 read_unlock_bh(&local->services_lock);
265 goto invalid_service;
267 found_service:
268 _debug("found service %hd", ntohs(rx->service_id));
269 if (sk_acceptq_is_full(&rx->sk))
270 goto backlog_full;
271 sk_acceptq_added(&rx->sk);
272 sock_hold(&rx->sk);
273 read_unlock_bh(&local->services_lock);
275 ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
276 if (ret < 0)
277 sk_acceptq_removed(&rx->sk);
278 sock_put(&rx->sk);
279 switch (ret) {
280 case -ECONNRESET: /* old calls are ignored */
281 case -ECONNABORTED: /* aborted calls are reaborted or ignored */
282 case 0:
283 goto process_next_packet;
284 case -ECONNREFUSED:
285 goto invalid_service;
286 case -EBUSY:
287 goto busy;
288 case -EKEYREJECTED:
289 goto security_mismatch;
290 default:
291 BUG();
294 backlog_full:
295 read_unlock_bh(&local->services_lock);
296 busy:
297 rxrpc_busy(local, &srx, &sp->hdr);
298 rxrpc_free_skb(skb);
299 goto process_next_packet;
301 invalid_service:
302 skb->priority = RX_INVALID_OPERATION;
303 rxrpc_reject_packet(local, skb);
304 goto process_next_packet;
306 /* can't change connection security type mid-flow */
307 security_mismatch:
308 skb->priority = RX_PROTOCOL_ERROR;
309 rxrpc_reject_packet(local, skb);
310 goto process_next_packet;
314 * handle acceptance of a call by userspace
315 * - assign the user call ID to the call at the front of the queue
317 struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
318 unsigned long user_call_ID)
320 struct rxrpc_call *call;
321 struct rb_node *parent, **pp;
322 int ret;
324 _enter(",%lx", user_call_ID);
326 ASSERT(!irqs_disabled());
328 write_lock(&rx->call_lock);
330 ret = -ENODATA;
331 if (list_empty(&rx->acceptq))
332 goto out;
334 /* check the user ID isn't already in use */
335 ret = -EBADSLT;
336 pp = &rx->calls.rb_node;
337 parent = NULL;
338 while (*pp) {
339 parent = *pp;
340 call = rb_entry(parent, struct rxrpc_call, sock_node);
342 if (user_call_ID < call->user_call_ID)
343 pp = &(*pp)->rb_left;
344 else if (user_call_ID > call->user_call_ID)
345 pp = &(*pp)->rb_right;
346 else
347 goto out;
350 /* dequeue the first call and check it's still valid */
351 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
352 list_del_init(&call->accept_link);
353 sk_acceptq_removed(&rx->sk);
355 write_lock_bh(&call->state_lock);
356 switch (call->state) {
357 case RXRPC_CALL_SERVER_ACCEPTING:
358 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
359 break;
360 case RXRPC_CALL_REMOTELY_ABORTED:
361 case RXRPC_CALL_LOCALLY_ABORTED:
362 ret = -ECONNABORTED;
363 goto out_release;
364 case RXRPC_CALL_NETWORK_ERROR:
365 ret = call->conn->error;
366 goto out_release;
367 case RXRPC_CALL_DEAD:
368 ret = -ETIME;
369 goto out_discard;
370 default:
371 BUG();
374 /* formalise the acceptance */
375 call->user_call_ID = user_call_ID;
376 rb_link_node(&call->sock_node, parent, pp);
377 rb_insert_color(&call->sock_node, &rx->calls);
378 if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
379 BUG();
380 if (test_and_set_bit(RXRPC_CALL_ACCEPTED, &call->events))
381 BUG();
382 rxrpc_queue_call(call);
384 rxrpc_get_call(call);
385 write_unlock_bh(&call->state_lock);
386 write_unlock(&rx->call_lock);
387 _leave(" = %p{%d}", call, call->debug_id);
388 return call;
390 /* if the call is already dying or dead, then we leave the socket's ref
391 * on it to be released by rxrpc_dead_call_expired() as induced by
392 * rxrpc_release_call() */
393 out_release:
394 _debug("release %p", call);
395 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
396 !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
397 rxrpc_queue_call(call);
398 out_discard:
399 write_unlock_bh(&call->state_lock);
400 _debug("discard %p", call);
401 out:
402 write_unlock(&rx->call_lock);
403 _leave(" = %d", ret);
404 return ERR_PTR(ret);
408 * handle rejectance of a call by userspace
409 * - reject the call at the front of the queue
411 int rxrpc_reject_call(struct rxrpc_sock *rx)
413 struct rxrpc_call *call;
414 int ret;
416 _enter("");
418 ASSERT(!irqs_disabled());
420 write_lock(&rx->call_lock);
422 ret = -ENODATA;
423 if (list_empty(&rx->acceptq))
424 goto out;
426 /* dequeue the first call and check it's still valid */
427 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
428 list_del_init(&call->accept_link);
429 sk_acceptq_removed(&rx->sk);
431 write_lock_bh(&call->state_lock);
432 switch (call->state) {
433 case RXRPC_CALL_SERVER_ACCEPTING:
434 call->state = RXRPC_CALL_SERVER_BUSY;
435 if (test_and_set_bit(RXRPC_CALL_REJECT_BUSY, &call->events))
436 rxrpc_queue_call(call);
437 ret = 0;
438 goto out_release;
439 case RXRPC_CALL_REMOTELY_ABORTED:
440 case RXRPC_CALL_LOCALLY_ABORTED:
441 ret = -ECONNABORTED;
442 goto out_release;
443 case RXRPC_CALL_NETWORK_ERROR:
444 ret = call->conn->error;
445 goto out_release;
446 case RXRPC_CALL_DEAD:
447 ret = -ETIME;
448 goto out_discard;
449 default:
450 BUG();
453 /* if the call is already dying or dead, then we leave the socket's ref
454 * on it to be released by rxrpc_dead_call_expired() as induced by
455 * rxrpc_release_call() */
456 out_release:
457 _debug("release %p", call);
458 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
459 !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
460 rxrpc_queue_call(call);
461 out_discard:
462 write_unlock_bh(&call->state_lock);
463 _debug("discard %p", call);
464 out:
465 write_unlock(&rx->call_lock);
466 _leave(" = %d", ret);
467 return ret;
471 * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
472 * @sock: The socket on which the impending call is waiting
473 * @user_call_ID: The tag to attach to the call
475 * Allow a kernel service to accept an incoming call, assuming the incoming
476 * call is still valid.
478 struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
479 unsigned long user_call_ID)
481 struct rxrpc_call *call;
483 _enter(",%lx", user_call_ID);
484 call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
485 _leave(" = %p", call);
486 return call;
489 EXPORT_SYMBOL(rxrpc_kernel_accept_call);
492 * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
493 * @sock: The socket on which the impending call is waiting
495 * Allow a kernel service to reject an incoming call with a BUSY message,
496 * assuming the incoming call is still valid.
498 int rxrpc_kernel_reject_call(struct socket *sock)
500 int ret;
502 _enter("");
503 ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
504 _leave(" = %d", ret);
505 return ret;
508 EXPORT_SYMBOL(rxrpc_kernel_reject_call);