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