gpio: pca953x: refactor pca953x_write_regs()
[linux/fpc-iii.git] / net / rxrpc / call_accept.c
blob0b2832141bd079797deb27580dc3cbf59611b3fd
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>
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"
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)
34 struct msghdr msg;
35 struct kvec iov[1];
36 size_t len;
37 int ret;
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;
48 msg.msg_flags = 0;
50 iov[0].iov_base = whdr;
51 iov[0].iov_len = sizeof(*whdr);
53 len = iov[0].iov_len;
55 _proto("Tx BUSY %%1");
57 ret = kernel_sendmsg(local->socket, &msg, iov, 1, len);
58 if (ret < 0) {
59 _leave(" = -EAGAIN [sendmsg failed: %d]", ret);
60 return -EAGAIN;
63 _leave(" = 0");
64 return 0;
68 * accept an incoming call that needs peer, transport and/or connection setting
69 * up
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)
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;
82 _enter("");
84 sp = rxrpc_skb(skb);
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;
93 rxrpc_new_skb(notification);
94 notification->mark = RXRPC_SKB_MARK_NEW_CALL;
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;
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;
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;
116 write_lock(&rx->call_lock);
117 if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
118 rxrpc_get_call(call);
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(call);
133 nsp = rxrpc_skb(notification);
134 nsp->call = call;
136 ASSERTCMP(atomic_read(&call->usage), >=, 3);
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);
146 spin_unlock(&call->conn->state_lock);
148 _debug("queued");
150 write_unlock(&rx->call_lock);
152 _debug("process");
153 rxrpc_fast_process_packet(call, skb);
155 _debug("done");
156 read_unlock_bh(&local->services_lock);
157 rxrpc_free_skb(notification);
158 rxrpc_put_call(call);
159 _leave(" = 0");
160 return 0;
162 invalid_service:
163 _debug("invalid");
164 read_unlock_bh(&local->services_lock);
166 read_lock_bh(&call->state_lock);
167 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
168 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
169 rxrpc_get_call(call);
170 rxrpc_queue_call(call);
172 read_unlock_bh(&call->state_lock);
173 rxrpc_put_call(call);
174 ret = -ECONNREFUSED;
175 error:
176 rxrpc_free_skb(notification);
177 error_nofree:
178 _leave(" = %d", ret);
179 return ret;
183 * accept incoming calls that need peer, transport and/or connection setting up
184 * - the packets we get are all incoming client DATA packets that have seq == 1
186 void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
188 struct rxrpc_skb_priv *sp;
189 struct sockaddr_rxrpc srx;
190 struct rxrpc_sock *rx;
191 struct rxrpc_wire_header whdr;
192 struct sk_buff *skb;
193 int ret;
195 _enter("%d", local->debug_id);
197 skb = skb_dequeue(&local->accept_queue);
198 if (!skb) {
199 _leave("\n");
200 return;
203 _net("incoming call skb %p", skb);
205 sp = rxrpc_skb(skb);
207 /* Set up a response packet header in case we need it */
208 whdr.epoch = htonl(sp->hdr.epoch);
209 whdr.cid = htonl(sp->hdr.cid);
210 whdr.callNumber = htonl(sp->hdr.callNumber);
211 whdr.seq = htonl(sp->hdr.seq);
212 whdr.serial = 0;
213 whdr.flags = 0;
214 whdr.type = 0;
215 whdr.userStatus = 0;
216 whdr.securityIndex = sp->hdr.securityIndex;
217 whdr._rsvd = 0;
218 whdr.serviceId = htons(sp->hdr.serviceId);
220 if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
221 goto drop;
223 /* get the socket providing the service */
224 read_lock_bh(&local->services_lock);
225 list_for_each_entry(rx, &local->services, listen_link) {
226 if (rx->srx.srx_service == sp->hdr.serviceId &&
227 rx->sk.sk_state != RXRPC_CLOSE)
228 goto found_service;
230 read_unlock_bh(&local->services_lock);
231 goto invalid_service;
233 found_service:
234 _debug("found service %hd", rx->srx.srx_service);
235 if (sk_acceptq_is_full(&rx->sk))
236 goto backlog_full;
237 sk_acceptq_added(&rx->sk);
238 sock_hold(&rx->sk);
239 read_unlock_bh(&local->services_lock);
241 ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
242 if (ret < 0)
243 sk_acceptq_removed(&rx->sk);
244 sock_put(&rx->sk);
245 switch (ret) {
246 case -ECONNRESET: /* old calls are ignored */
247 case -ECONNABORTED: /* aborted calls are reaborted or ignored */
248 case 0:
249 return;
250 case -ECONNREFUSED:
251 goto invalid_service;
252 case -EBUSY:
253 goto busy;
254 case -EKEYREJECTED:
255 goto security_mismatch;
256 default:
257 BUG();
260 backlog_full:
261 read_unlock_bh(&local->services_lock);
262 busy:
263 rxrpc_busy(local, &srx, &whdr);
264 rxrpc_free_skb(skb);
265 return;
267 drop:
268 rxrpc_free_skb(skb);
269 return;
271 invalid_service:
272 skb->priority = RX_INVALID_OPERATION;
273 rxrpc_reject_packet(local, skb);
274 return;
276 /* can't change connection security type mid-flow */
277 security_mismatch:
278 skb->priority = RX_PROTOCOL_ERROR;
279 rxrpc_reject_packet(local, skb);
280 return;
284 * handle acceptance of a call by userspace
285 * - assign the user call ID to the call at the front of the queue
287 struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
288 unsigned long user_call_ID)
290 struct rxrpc_call *call;
291 struct rb_node *parent, **pp;
292 int ret;
294 _enter(",%lx", user_call_ID);
296 ASSERT(!irqs_disabled());
298 write_lock(&rx->call_lock);
300 ret = -ENODATA;
301 if (list_empty(&rx->acceptq))
302 goto out;
304 /* check the user ID isn't already in use */
305 ret = -EBADSLT;
306 pp = &rx->calls.rb_node;
307 parent = NULL;
308 while (*pp) {
309 parent = *pp;
310 call = rb_entry(parent, struct rxrpc_call, sock_node);
312 if (user_call_ID < call->user_call_ID)
313 pp = &(*pp)->rb_left;
314 else if (user_call_ID > call->user_call_ID)
315 pp = &(*pp)->rb_right;
316 else
317 goto out;
320 /* dequeue the first call and check it's still valid */
321 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
322 list_del_init(&call->accept_link);
323 sk_acceptq_removed(&rx->sk);
325 write_lock_bh(&call->state_lock);
326 switch (call->state) {
327 case RXRPC_CALL_SERVER_ACCEPTING:
328 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
329 break;
330 case RXRPC_CALL_REMOTELY_ABORTED:
331 case RXRPC_CALL_LOCALLY_ABORTED:
332 ret = -ECONNABORTED;
333 goto out_release;
334 case RXRPC_CALL_NETWORK_ERROR:
335 ret = call->conn->error;
336 goto out_release;
337 case RXRPC_CALL_DEAD:
338 ret = -ETIME;
339 goto out_discard;
340 default:
341 BUG();
344 /* formalise the acceptance */
345 call->user_call_ID = user_call_ID;
346 rb_link_node(&call->sock_node, parent, pp);
347 rb_insert_color(&call->sock_node, &rx->calls);
348 if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
349 BUG();
350 if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
351 BUG();
352 rxrpc_queue_call(call);
354 rxrpc_get_call(call);
355 write_unlock_bh(&call->state_lock);
356 write_unlock(&rx->call_lock);
357 _leave(" = %p{%d}", call, call->debug_id);
358 return call;
360 /* if the call is already dying or dead, then we leave the socket's ref
361 * on it to be released by rxrpc_dead_call_expired() as induced by
362 * rxrpc_release_call() */
363 out_release:
364 _debug("release %p", call);
365 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
366 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
367 rxrpc_queue_call(call);
368 out_discard:
369 write_unlock_bh(&call->state_lock);
370 _debug("discard %p", call);
371 out:
372 write_unlock(&rx->call_lock);
373 _leave(" = %d", ret);
374 return ERR_PTR(ret);
378 * Handle rejection of a call by userspace
379 * - reject the call at the front of the queue
381 int rxrpc_reject_call(struct rxrpc_sock *rx)
383 struct rxrpc_call *call;
384 int ret;
386 _enter("");
388 ASSERT(!irqs_disabled());
390 write_lock(&rx->call_lock);
392 ret = -ENODATA;
393 if (list_empty(&rx->acceptq))
394 goto out;
396 /* dequeue the first call and check it's still valid */
397 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
398 list_del_init(&call->accept_link);
399 sk_acceptq_removed(&rx->sk);
401 write_lock_bh(&call->state_lock);
402 switch (call->state) {
403 case RXRPC_CALL_SERVER_ACCEPTING:
404 call->state = RXRPC_CALL_SERVER_BUSY;
405 if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
406 rxrpc_queue_call(call);
407 ret = 0;
408 goto out_release;
409 case RXRPC_CALL_REMOTELY_ABORTED:
410 case RXRPC_CALL_LOCALLY_ABORTED:
411 ret = -ECONNABORTED;
412 goto out_release;
413 case RXRPC_CALL_NETWORK_ERROR:
414 ret = call->conn->error;
415 goto out_release;
416 case RXRPC_CALL_DEAD:
417 ret = -ETIME;
418 goto out_discard;
419 default:
420 BUG();
423 /* if the call is already dying or dead, then we leave the socket's ref
424 * on it to be released by rxrpc_dead_call_expired() as induced by
425 * rxrpc_release_call() */
426 out_release:
427 _debug("release %p", call);
428 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
429 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
430 rxrpc_queue_call(call);
431 out_discard:
432 write_unlock_bh(&call->state_lock);
433 _debug("discard %p", call);
434 out:
435 write_unlock(&rx->call_lock);
436 _leave(" = %d", ret);
437 return ret;
441 * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
442 * @sock: The socket on which the impending call is waiting
443 * @user_call_ID: The tag to attach to the call
445 * Allow a kernel service to accept an incoming call, assuming the incoming
446 * call is still valid.
448 struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
449 unsigned long user_call_ID)
451 struct rxrpc_call *call;
453 _enter(",%lx", user_call_ID);
454 call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
455 _leave(" = %p", call);
456 return call;
458 EXPORT_SYMBOL(rxrpc_kernel_accept_call);
461 * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
462 * @sock: The socket on which the impending call is waiting
464 * Allow a kernel service to reject an incoming call with a BUSY message,
465 * assuming the incoming call is still valid.
467 int rxrpc_kernel_reject_call(struct socket *sock)
469 int ret;
471 _enter("");
472 ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
473 _leave(" = %d", ret);
474 return ret;
476 EXPORT_SYMBOL(rxrpc_kernel_reject_call);