Merge tag 'iwlwifi-next-for-kalle-2016-07-01' of git://git.kernel.org/pub/scm/linux...
[deliverable/linux.git] / net / rxrpc / call_accept.c
CommitLineData
17926a79
DH
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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
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>
5a0e3ad6 22#include <linux/gfp.h>
17926a79
DH
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 */
31static int rxrpc_busy(struct rxrpc_local *local, struct sockaddr_rxrpc *srx,
0d12f8a4 32 struct rxrpc_wire_header *whdr)
17926a79
DH
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
0d12f8a4
DH
41 whdr->type = RXRPC_PACKET_TYPE_BUSY;
42 whdr->serial = htonl(1);
43
17926a79
DH
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
0d12f8a4
DH
50 iov[0].iov_base = whdr;
51 iov[0].iov_len = sizeof(*whdr);
17926a79
DH
52
53 len = iov[0].iov_len;
54
0d12f8a4 55 _proto("Tx BUSY %%1");
17926a79
DH
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 */
71static 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;
17926a79
DH
77 struct rxrpc_skb_priv *sp, *nsp;
78 struct rxrpc_peer *peer;
79 struct rxrpc_call *call;
80 struct sk_buff *notification;
81 int ret;
82
83 _enter("");
84
85 sp = rxrpc_skb(skb);
86
87 /* get a notification message to send to the server app */
88 notification = alloc_skb(0, GFP_NOFS);
c3824d21
TH
89 if (!notification) {
90 _debug("no memory");
91 ret = -ENOMEM;
92 goto error_nofree;
93 }
17926a79
DH
94 rxrpc_new_skb(notification);
95 notification->mark = RXRPC_SKB_MARK_NEW_CALL;
96
be6e6707 97 peer = rxrpc_lookup_peer(local, srx, GFP_NOIO);
0e4699e4 98 if (!peer) {
17926a79
DH
99 _debug("no peer");
100 ret = -EBUSY;
101 goto error;
102 }
103
aa390bbe 104 conn = rxrpc_incoming_connection(local, peer, skb);
17926a79 105 rxrpc_put_peer(peer);
17926a79
DH
106 if (IS_ERR(conn)) {
107 _debug("no conn");
108 ret = PTR_ERR(conn);
109 goto error;
110 }
111
42886ffe 112 call = rxrpc_incoming_call(rx, conn, skb);
17926a79
DH
113 rxrpc_put_connection(conn);
114 if (IS_ERR(call)) {
115 _debug("no call");
116 ret = PTR_ERR(call);
117 goto error;
118 }
119
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;
124
125 write_lock(&rx->call_lock);
126 if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
127 rxrpc_get_call(call);
128
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;
5627cc8b 135 rxrpc_get_connection(call->conn);
17926a79 136 set_bit(RXRPC_CONN_CHALLENGE, &call->conn->events);
651350d1 137 rxrpc_queue_conn(call->conn);
17926a79
DH
138 } else {
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);
144 nsp->call = call;
145
146 ASSERTCMP(atomic_read(&call->usage), >=, 3);
147
148 _debug("notify");
149 spin_lock(&call->lock);
150 ret = rxrpc_queue_rcv_skb(call, notification, true,
151 false);
152 spin_unlock(&call->lock);
153 notification = NULL;
163e3cb7 154 BUG_ON(ret < 0);
17926a79
DH
155 }
156 spin_unlock(&call->conn->state_lock);
157
158 _debug("queued");
159 }
160 write_unlock(&rx->call_lock);
161
162 _debug("process");
163 rxrpc_fast_process_packet(call, skb);
164
165 _debug("done");
166 read_unlock_bh(&local->services_lock);
167 rxrpc_free_skb(notification);
168 rxrpc_put_call(call);
169 _leave(" = 0");
170 return 0;
171
172invalid_service:
173 _debug("invalid");
174 read_unlock_bh(&local->services_lock);
175
176 read_lock_bh(&call->state_lock);
e721498a 177 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
4c198ad1 178 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
17926a79 179 rxrpc_get_call(call);
651350d1 180 rxrpc_queue_call(call);
17926a79
DH
181 }
182 read_unlock_bh(&call->state_lock);
183 rxrpc_put_call(call);
184 ret = -ECONNREFUSED;
185error:
186 rxrpc_free_skb(notification);
c3824d21 187error_nofree:
17926a79
DH
188 _leave(" = %d", ret);
189 return ret;
190}
191
192/*
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
195 */
4f95dd78 196void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
17926a79 197{
17926a79
DH
198 struct rxrpc_skb_priv *sp;
199 struct sockaddr_rxrpc srx;
200 struct rxrpc_sock *rx;
0d12f8a4 201 struct rxrpc_wire_header whdr;
17926a79 202 struct sk_buff *skb;
17926a79
DH
203 int ret;
204
205 _enter("%d", local->debug_id);
206
17926a79
DH
207 skb = skb_dequeue(&local->accept_queue);
208 if (!skb) {
17926a79
DH
209 _leave("\n");
210 return;
211 }
212
213 _net("incoming call skb %p", skb);
214
215 sp = rxrpc_skb(skb);
216
0d12f8a4
DH
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);
222 whdr.serial = 0;
223 whdr.flags = 0;
224 whdr.type = 0;
225 whdr.userStatus = 0;
226 whdr.securityIndex = sp->hdr.securityIndex;
227 whdr._rsvd = 0;
228 whdr.serviceId = htons(sp->hdr.serviceId);
229
17926a79
DH
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) {
236 case AF_INET:
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;
240 break;
241 default:
242 goto busy;
243 }
244
245 /* get the socket providing the service */
17926a79
DH
246 read_lock_bh(&local->services_lock);
247 list_for_each_entry(rx, &local->services, listen_link) {
0d12f8a4 248 if (rx->srx.srx_service == sp->hdr.serviceId &&
17926a79
DH
249 rx->sk.sk_state != RXRPC_CLOSE)
250 goto found_service;
251 }
252 read_unlock_bh(&local->services_lock);
253 goto invalid_service;
254
255found_service:
0d12f8a4 256 _debug("found service %hd", rx->srx.srx_service);
17926a79
DH
257 if (sk_acceptq_is_full(&rx->sk))
258 goto backlog_full;
259 sk_acceptq_added(&rx->sk);
260 sock_hold(&rx->sk);
261 read_unlock_bh(&local->services_lock);
262
263 ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
264 if (ret < 0)
265 sk_acceptq_removed(&rx->sk);
266 sock_put(&rx->sk);
267 switch (ret) {
268 case -ECONNRESET: /* old calls are ignored */
269 case -ECONNABORTED: /* aborted calls are reaborted or ignored */
270 case 0:
4f95dd78 271 return;
17926a79
DH
272 case -ECONNREFUSED:
273 goto invalid_service;
274 case -EBUSY:
275 goto busy;
276 case -EKEYREJECTED:
277 goto security_mismatch;
278 default:
279 BUG();
280 }
281
282backlog_full:
283 read_unlock_bh(&local->services_lock);
284busy:
0d12f8a4 285 rxrpc_busy(local, &srx, &whdr);
17926a79 286 rxrpc_free_skb(skb);
4f95dd78 287 return;
17926a79
DH
288
289invalid_service:
290 skb->priority = RX_INVALID_OPERATION;
291 rxrpc_reject_packet(local, skb);
4f95dd78 292 return;
17926a79
DH
293
294 /* can't change connection security type mid-flow */
295security_mismatch:
296 skb->priority = RX_PROTOCOL_ERROR;
297 rxrpc_reject_packet(local, skb);
4f95dd78 298 return;
17926a79
DH
299}
300
301/*
302 * handle acceptance of a call by userspace
303 * - assign the user call ID to the call at the front of the queue
304 */
651350d1
DH
305struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
306 unsigned long user_call_ID)
17926a79
DH
307{
308 struct rxrpc_call *call;
309 struct rb_node *parent, **pp;
310 int ret;
311
312 _enter(",%lx", user_call_ID);
313
314 ASSERT(!irqs_disabled());
315
316 write_lock(&rx->call_lock);
317
318 ret = -ENODATA;
319 if (list_empty(&rx->acceptq))
320 goto out;
321
322 /* check the user ID isn't already in use */
323 ret = -EBADSLT;
324 pp = &rx->calls.rb_node;
325 parent = NULL;
326 while (*pp) {
327 parent = *pp;
328 call = rb_entry(parent, struct rxrpc_call, sock_node);
329
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;
334 else
335 goto out;
336 }
337
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);
342
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;
347 break;
348 case RXRPC_CALL_REMOTELY_ABORTED:
349 case RXRPC_CALL_LOCALLY_ABORTED:
350 ret = -ECONNABORTED;
351 goto out_release;
352 case RXRPC_CALL_NETWORK_ERROR:
353 ret = call->conn->error;
354 goto out_release;
355 case RXRPC_CALL_DEAD:
356 ret = -ETIME;
357 goto out_discard;
358 default:
359 BUG();
360 }
361
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))
367 BUG();
4c198ad1 368 if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
17926a79 369 BUG();
651350d1 370 rxrpc_queue_call(call);
17926a79 371
651350d1 372 rxrpc_get_call(call);
17926a79
DH
373 write_unlock_bh(&call->state_lock);
374 write_unlock(&rx->call_lock);
651350d1
DH
375 _leave(" = %p{%d}", call, call->debug_id);
376 return call;
377
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() */
381out_release:
382 _debug("release %p", call);
383 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
4c198ad1 384 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
651350d1
DH
385 rxrpc_queue_call(call);
386out_discard:
387 write_unlock_bh(&call->state_lock);
388 _debug("discard %p", call);
389out:
390 write_unlock(&rx->call_lock);
391 _leave(" = %d", ret);
392 return ERR_PTR(ret);
393}
394
395/*
b4f1342f 396 * Handle rejection of a call by userspace
651350d1
DH
397 * - reject the call at the front of the queue
398 */
399int rxrpc_reject_call(struct rxrpc_sock *rx)
400{
401 struct rxrpc_call *call;
402 int ret;
403
404 _enter("");
405
406 ASSERT(!irqs_disabled());
407
408 write_lock(&rx->call_lock);
409
410 ret = -ENODATA;
411 if (list_empty(&rx->acceptq))
412 goto out;
413
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);
418
419 write_lock_bh(&call->state_lock);
420 switch (call->state) {
421 case RXRPC_CALL_SERVER_ACCEPTING:
422 call->state = RXRPC_CALL_SERVER_BUSY;
4c198ad1 423 if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
651350d1
DH
424 rxrpc_queue_call(call);
425 ret = 0;
426 goto out_release;
427 case RXRPC_CALL_REMOTELY_ABORTED:
428 case RXRPC_CALL_LOCALLY_ABORTED:
429 ret = -ECONNABORTED;
430 goto out_release;
431 case RXRPC_CALL_NETWORK_ERROR:
432 ret = call->conn->error;
433 goto out_release;
434 case RXRPC_CALL_DEAD:
435 ret = -ETIME;
436 goto out_discard;
437 default:
438 BUG();
439 }
17926a79
DH
440
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() */
444out_release:
445 _debug("release %p", call);
446 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
4c198ad1 447 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
651350d1 448 rxrpc_queue_call(call);
17926a79
DH
449out_discard:
450 write_unlock_bh(&call->state_lock);
451 _debug("discard %p", call);
452out:
453 write_unlock(&rx->call_lock);
454 _leave(" = %d", ret);
455 return ret;
456}
651350d1
DH
457
458/**
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
462 *
463 * Allow a kernel service to accept an incoming call, assuming the incoming
464 * call is still valid.
465 */
466struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
467 unsigned long user_call_ID)
468{
469 struct rxrpc_call *call;
470
471 _enter(",%lx", user_call_ID);
472 call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
473 _leave(" = %p", call);
474 return call;
475}
651350d1
DH
476EXPORT_SYMBOL(rxrpc_kernel_accept_call);
477
478/**
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
481 *
482 * Allow a kernel service to reject an incoming call with a BUSY message,
483 * assuming the incoming call is still valid.
484 */
485int rxrpc_kernel_reject_call(struct socket *sock)
486{
487 int ret;
488
489 _enter("");
490 ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
491 _leave(" = %d", ret);
492 return ret;
493}
651350d1 494EXPORT_SYMBOL(rxrpc_kernel_reject_call);
This page took 0.734907 seconds and 5 git commands to generate.