rxrpc: Convert rxrpc_local::services to an hlist
[linux-2.6-block.git] / net / rxrpc / call_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 #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_skb_priv *sp, *nsp;
78         struct rxrpc_call *call;
79         struct sk_buff *notification;
80         int ret;
81
82         _enter("");
83
84         sp = rxrpc_skb(skb);
85
86         /* get a notification message to send to the server app */
87         notification = alloc_skb(0, GFP_NOFS);
88         if (!notification) {
89                 _debug("no memory");
90                 ret = -ENOMEM;
91                 goto error_nofree;
92         }
93         rxrpc_new_skb(notification);
94         notification->mark = RXRPC_SKB_MARK_NEW_CALL;
95
96         conn = rxrpc_incoming_connection(local, srx, skb);
97         if (IS_ERR(conn)) {
98                 _debug("no conn");
99                 ret = PTR_ERR(conn);
100                 goto error;
101         }
102
103         call = rxrpc_incoming_call(rx, conn, skb);
104         rxrpc_put_connection(conn);
105         if (IS_ERR(call)) {
106                 _debug("no call");
107                 ret = PTR_ERR(call);
108                 goto error;
109         }
110
111         /* attach the call to the socket */
112         read_lock_bh(&local->services_lock);
113         if (rx->sk.sk_state == RXRPC_CLOSE)
114                 goto invalid_service;
115
116         write_lock(&rx->call_lock);
117         if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
118                 rxrpc_get_call(call, rxrpc_call_got);
119
120                 spin_lock(&call->conn->state_lock);
121                 if (sp->hdr.securityIndex > 0 &&
122                     call->conn->state == RXRPC_CONN_SERVICE_UNSECURED) {
123                         _debug("await conn sec");
124                         list_add_tail(&call->accept_link, &rx->secureq);
125                         call->conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
126                         set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
127                         rxrpc_queue_conn(call->conn);
128                 } else {
129                         _debug("conn ready");
130                         call->state = RXRPC_CALL_SERVER_ACCEPTING;
131                         list_add_tail(&call->accept_link, &rx->acceptq);
132                         rxrpc_get_call_for_skb(call, notification);
133                         nsp = rxrpc_skb(notification);
134                         nsp->call = call;
135
136                         ASSERTCMP(atomic_read(&call->usage), >=, 3);
137
138                         _debug("notify");
139                         spin_lock(&call->lock);
140                         ret = rxrpc_queue_rcv_skb(call, notification, true,
141                                                   false);
142                         spin_unlock(&call->lock);
143                         notification = NULL;
144                         BUG_ON(ret < 0);
145                 }
146                 spin_unlock(&call->conn->state_lock);
147
148                 _debug("queued");
149         }
150         write_unlock(&rx->call_lock);
151
152         _debug("process");
153         rxrpc_fast_process_packet(call, skb);
154
155         _debug("done");
156         read_unlock_bh(&local->services_lock);
157         rxrpc_free_skb(notification);
158         rxrpc_put_call(call, rxrpc_call_put);
159         _leave(" = 0");
160         return 0;
161
162 invalid_service:
163         _debug("invalid");
164         read_unlock_bh(&local->services_lock);
165
166         rxrpc_release_call(rx, call);
167         rxrpc_put_call(call, rxrpc_call_put);
168         ret = -ECONNREFUSED;
169 error:
170         rxrpc_free_skb(notification);
171 error_nofree:
172         _leave(" = %d", ret);
173         return ret;
174 }
175
176 /*
177  * accept incoming calls that need peer, transport and/or connection setting up
178  * - the packets we get are all incoming client DATA packets that have seq == 1
179  */
180 void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
181 {
182         struct rxrpc_skb_priv *sp;
183         struct sockaddr_rxrpc srx;
184         struct rxrpc_sock *rx;
185         struct rxrpc_wire_header whdr;
186         struct sk_buff *skb;
187         int ret;
188
189         _enter("%d", local->debug_id);
190
191         skb = skb_dequeue(&local->accept_queue);
192         if (!skb) {
193                 _leave("\n");
194                 return;
195         }
196
197         _net("incoming call skb %p", skb);
198
199         rxrpc_see_skb(skb);
200         sp = rxrpc_skb(skb);
201
202         /* Set up a response packet header in case we need it */
203         whdr.epoch      = htonl(sp->hdr.epoch);
204         whdr.cid        = htonl(sp->hdr.cid);
205         whdr.callNumber = htonl(sp->hdr.callNumber);
206         whdr.seq        = htonl(sp->hdr.seq);
207         whdr.serial     = 0;
208         whdr.flags      = 0;
209         whdr.type       = 0;
210         whdr.userStatus = 0;
211         whdr.securityIndex = sp->hdr.securityIndex;
212         whdr._rsvd      = 0;
213         whdr.serviceId  = htons(sp->hdr.serviceId);
214
215         if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
216                 goto drop;
217
218         /* get the socket providing the service */
219         read_lock_bh(&local->services_lock);
220         hlist_for_each_entry(rx, &local->services, listen_link) {
221                 if (rx->srx.srx_service == sp->hdr.serviceId &&
222                     rx->sk.sk_state != RXRPC_CLOSE)
223                         goto found_service;
224         }
225         read_unlock_bh(&local->services_lock);
226         goto invalid_service;
227
228 found_service:
229         _debug("found service %hd", rx->srx.srx_service);
230         if (sk_acceptq_is_full(&rx->sk))
231                 goto backlog_full;
232         sk_acceptq_added(&rx->sk);
233         read_unlock_bh(&local->services_lock);
234
235         ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
236         if (ret < 0)
237                 sk_acceptq_removed(&rx->sk);
238         switch (ret) {
239         case -ECONNRESET: /* old calls are ignored */
240         case -ECONNABORTED: /* aborted calls are reaborted or ignored */
241         case 0:
242                 return;
243         case -ECONNREFUSED:
244                 goto invalid_service;
245         case -EBUSY:
246                 goto busy;
247         case -EKEYREJECTED:
248                 goto security_mismatch;
249         default:
250                 BUG();
251         }
252
253 backlog_full:
254         read_unlock_bh(&local->services_lock);
255 busy:
256         rxrpc_busy(local, &srx, &whdr);
257         rxrpc_free_skb(skb);
258         return;
259
260 drop:
261         rxrpc_free_skb(skb);
262         return;
263
264 invalid_service:
265         skb->priority = RX_INVALID_OPERATION;
266         rxrpc_reject_packet(local, skb);
267         return;
268
269         /* can't change connection security type mid-flow */
270 security_mismatch:
271         skb->priority = RX_PROTOCOL_ERROR;
272         rxrpc_reject_packet(local, skb);
273         return;
274 }
275
276 /*
277  * handle acceptance of a call by userspace
278  * - assign the user call ID to the call at the front of the queue
279  */
280 struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
281                                      unsigned long user_call_ID,
282                                      rxrpc_notify_rx_t notify_rx)
283 {
284         struct rxrpc_call *call;
285         struct rb_node *parent, **pp;
286         int ret;
287
288         _enter(",%lx", user_call_ID);
289
290         ASSERT(!irqs_disabled());
291
292         write_lock(&rx->call_lock);
293
294         ret = -ENODATA;
295         if (list_empty(&rx->acceptq))
296                 goto out;
297
298         /* check the user ID isn't already in use */
299         ret = -EBADSLT;
300         pp = &rx->calls.rb_node;
301         parent = NULL;
302         while (*pp) {
303                 parent = *pp;
304                 call = rb_entry(parent, struct rxrpc_call, sock_node);
305
306                 if (user_call_ID < call->user_call_ID)
307                         pp = &(*pp)->rb_left;
308                 else if (user_call_ID > call->user_call_ID)
309                         pp = &(*pp)->rb_right;
310                 else
311                         goto out;
312         }
313
314         /* dequeue the first call and check it's still valid */
315         call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
316         list_del_init(&call->accept_link);
317         sk_acceptq_removed(&rx->sk);
318         rxrpc_see_call(call);
319
320         write_lock_bh(&call->state_lock);
321         switch (call->state) {
322         case RXRPC_CALL_SERVER_ACCEPTING:
323                 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
324                 break;
325         case RXRPC_CALL_COMPLETE:
326                 ret = call->error;
327                 goto out_release;
328         default:
329                 BUG();
330         }
331
332         /* formalise the acceptance */
333         rxrpc_get_call(call, rxrpc_call_got_userid);
334         call->notify_rx = notify_rx;
335         call->user_call_ID = user_call_ID;
336         rb_link_node(&call->sock_node, parent, pp);
337         rb_insert_color(&call->sock_node, &rx->calls);
338         if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
339                 BUG();
340         if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
341                 BUG();
342
343         write_unlock_bh(&call->state_lock);
344         write_unlock(&rx->call_lock);
345         rxrpc_queue_call(call);
346         _leave(" = %p{%d}", call, call->debug_id);
347         return call;
348
349 out_release:
350         write_unlock_bh(&call->state_lock);
351         write_unlock(&rx->call_lock);
352         _debug("release %p", call);
353         rxrpc_release_call(rx, call);
354         _leave(" = %d", ret);
355         return ERR_PTR(ret);
356 out:
357         write_unlock(&rx->call_lock);
358         _leave(" = %d", ret);
359         return ERR_PTR(ret);
360 }
361
362 /*
363  * Handle rejection of a call by userspace
364  * - reject the call at the front of the queue
365  */
366 int rxrpc_reject_call(struct rxrpc_sock *rx)
367 {
368         struct rxrpc_call *call;
369         int ret;
370
371         _enter("");
372
373         ASSERT(!irqs_disabled());
374
375         write_lock(&rx->call_lock);
376
377         ret = -ENODATA;
378         if (list_empty(&rx->acceptq)) {
379                 write_unlock(&rx->call_lock);
380                 _leave(" = -ENODATA");
381                 return -ENODATA;
382         }
383
384         /* dequeue the first call and check it's still valid */
385         call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
386         list_del_init(&call->accept_link);
387         sk_acceptq_removed(&rx->sk);
388         rxrpc_see_call(call);
389
390         write_lock_bh(&call->state_lock);
391         switch (call->state) {
392         case RXRPC_CALL_SERVER_ACCEPTING:
393                 __rxrpc_set_call_completion(call, RXRPC_CALL_SERVER_BUSY,
394                                             0, ECONNABORTED);
395                 if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
396                         rxrpc_queue_call(call);
397                 ret = 0;
398                 break;
399         case RXRPC_CALL_COMPLETE:
400                 ret = call->error;
401                 break;
402         default:
403                 BUG();
404         }
405
406         write_unlock_bh(&call->state_lock);
407         write_unlock(&rx->call_lock);
408         rxrpc_release_call(rx, call);
409         _leave(" = %d", ret);
410         return ret;
411 }
412
413 /**
414  * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
415  * @sock: The socket on which the impending call is waiting
416  * @user_call_ID: The tag to attach to the call
417  * @notify_rx: Where to send notifications instead of socket queue
418  *
419  * Allow a kernel service to accept an incoming call, assuming the incoming
420  * call is still valid.  The caller should immediately trigger their own
421  * notification as there must be data waiting.
422  */
423 struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
424                                             unsigned long user_call_ID,
425                                             rxrpc_notify_rx_t notify_rx)
426 {
427         struct rxrpc_call *call;
428
429         _enter(",%lx", user_call_ID);
430         call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID, notify_rx);
431         _leave(" = %p", call);
432         return call;
433 }
434 EXPORT_SYMBOL(rxrpc_kernel_accept_call);
435
436 /**
437  * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
438  * @sock: The socket on which the impending call is waiting
439  *
440  * Allow a kernel service to reject an incoming call with a BUSY message,
441  * assuming the incoming call is still valid.
442  */
443 int rxrpc_kernel_reject_call(struct socket *sock)
444 {
445         int ret;
446
447         _enter("");
448         ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
449         _leave(" = %d", ret);
450         return ret;
451 }
452 EXPORT_SYMBOL(rxrpc_kernel_reject_call);