1 /* connection-level event 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>
18 #include <linux/in6.h>
19 #include <linux/icmp.h>
21 #include <net/af_rxrpc.h>
23 #include "ar-internal.h"
26 * pass a connection-level abort onto all calls on that connection
28 static void rxrpc_abort_calls(struct rxrpc_connection
*conn
, int state
,
31 struct rxrpc_call
*call
;
34 _enter("{%d},%x", conn
->debug_id
, abort_code
);
36 read_lock_bh(&conn
->lock
);
38 for (p
= rb_first(&conn
->calls
); p
; p
= rb_next(p
)) {
39 call
= rb_entry(p
, struct rxrpc_call
, conn_node
);
40 write_lock(&call
->state_lock
);
41 if (call
->state
<= RXRPC_CALL_COMPLETE
) {
43 if (state
== RXRPC_CALL_LOCALLY_ABORTED
) {
44 call
->local_abort
= conn
->local_abort
;
45 set_bit(RXRPC_CALL_EV_CONN_ABORT
, &call
->events
);
47 call
->remote_abort
= conn
->remote_abort
;
48 set_bit(RXRPC_CALL_EV_RCVD_ABORT
, &call
->events
);
50 rxrpc_queue_call(call
);
52 write_unlock(&call
->state_lock
);
55 read_unlock_bh(&conn
->lock
);
60 * generate a connection-level abort
62 static int rxrpc_abort_connection(struct rxrpc_connection
*conn
,
63 u32 error
, u32 abort_code
)
65 struct rxrpc_wire_header whdr
;
73 _enter("%d,,%u,%u", conn
->debug_id
, error
, abort_code
);
75 /* generate a connection-level abort */
76 spin_lock_bh(&conn
->state_lock
);
77 if (conn
->state
< RXRPC_CONN_REMOTELY_ABORTED
) {
78 conn
->state
= RXRPC_CONN_LOCALLY_ABORTED
;
80 spin_unlock_bh(&conn
->state_lock
);
82 spin_unlock_bh(&conn
->state_lock
);
83 _leave(" = 0 [already dead]");
87 rxrpc_abort_calls(conn
, RXRPC_CALL_LOCALLY_ABORTED
, abort_code
);
89 msg
.msg_name
= &conn
->trans
->peer
->srx
.transport
;
90 msg
.msg_namelen
= conn
->trans
->peer
->srx
.transport_len
;
91 msg
.msg_control
= NULL
;
92 msg
.msg_controllen
= 0;
95 whdr
.epoch
= htonl(conn
->epoch
);
96 whdr
.cid
= htonl(conn
->cid
);
99 whdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
100 whdr
.flags
= conn
->out_clientflag
;
102 whdr
.securityIndex
= conn
->security_ix
;
104 whdr
.serviceId
= htons(conn
->service_id
);
106 word
= htonl(conn
->local_abort
);
108 iov
[0].iov_base
= &whdr
;
109 iov
[0].iov_len
= sizeof(whdr
);
110 iov
[1].iov_base
= &word
;
111 iov
[1].iov_len
= sizeof(word
);
113 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
115 serial
= atomic_inc_return(&conn
->serial
);
116 whdr
.serial
= htonl(serial
);
117 _proto("Tx CONN ABORT %%%u { %d }", serial
, conn
->local_abort
);
119 ret
= kernel_sendmsg(conn
->trans
->local
->socket
, &msg
, iov
, 2, len
);
121 _debug("sendmsg failed: %d", ret
);
130 * mark a call as being on a now-secured channel
131 * - must be called with softirqs disabled
133 static void rxrpc_call_is_secure(struct rxrpc_call
*call
)
137 read_lock(&call
->state_lock
);
138 if (call
->state
< RXRPC_CALL_COMPLETE
&&
139 !test_and_set_bit(RXRPC_CALL_EV_SECURED
, &call
->events
))
140 rxrpc_queue_call(call
);
141 read_unlock(&call
->state_lock
);
146 * connection-level Rx packet processor
148 static int rxrpc_process_event(struct rxrpc_connection
*conn
,
152 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
157 if (conn
->state
>= RXRPC_CONN_REMOTELY_ABORTED
) {
158 kleave(" = -ECONNABORTED [%u]", conn
->state
);
159 return -ECONNABORTED
;
162 _enter("{%d},{%u,%%%u},", conn
->debug_id
, sp
->hdr
.type
, sp
->hdr
.serial
);
164 switch (sp
->hdr
.type
) {
165 case RXRPC_PACKET_TYPE_ABORT
:
166 if (skb_copy_bits(skb
, 0, &wtmp
, sizeof(wtmp
)) < 0)
168 abort_code
= ntohl(wtmp
);
169 _proto("Rx ABORT %%%u { ac=%d }", sp
->hdr
.serial
, abort_code
);
171 conn
->state
= RXRPC_CONN_REMOTELY_ABORTED
;
172 rxrpc_abort_calls(conn
, RXRPC_CALL_REMOTELY_ABORTED
,
174 return -ECONNABORTED
;
176 case RXRPC_PACKET_TYPE_CHALLENGE
:
177 return conn
->security
->respond_to_challenge(conn
, skb
,
180 case RXRPC_PACKET_TYPE_RESPONSE
:
181 ret
= conn
->security
->verify_response(conn
, skb
, _abort_code
);
185 ret
= conn
->security
->init_connection_security(conn
);
189 conn
->security
->prime_packet_security(conn
);
190 read_lock_bh(&conn
->lock
);
191 spin_lock(&conn
->state_lock
);
193 if (conn
->state
== RXRPC_CONN_SERVER_CHALLENGING
) {
194 conn
->state
= RXRPC_CONN_SERVER
;
195 for (loop
= 0; loop
< RXRPC_MAXCALLS
; loop
++)
196 rxrpc_call_is_secure(conn
->channels
[loop
]);
199 spin_unlock(&conn
->state_lock
);
200 read_unlock_bh(&conn
->lock
);
204 _leave(" = -EPROTO [%u]", sp
->hdr
.type
);
210 * set up security and issue a challenge
212 static void rxrpc_secure_connection(struct rxrpc_connection
*conn
)
217 _enter("{%d}", conn
->debug_id
);
219 ASSERT(conn
->security_ix
!= 0);
222 _debug("set up security");
223 ret
= rxrpc_init_server_conn_security(conn
);
228 abort_code
= RX_CALL_DEAD
;
231 abort_code
= RXKADNOAUTH
;
236 if (conn
->security
->issue_challenge(conn
) < 0) {
237 abort_code
= RX_CALL_DEAD
;
246 _debug("abort %d, %d", ret
, abort_code
);
247 rxrpc_abort_connection(conn
, -ret
, abort_code
);
248 _leave(" [aborted]");
252 * connection-level event processor
254 void rxrpc_process_connection(struct work_struct
*work
)
256 struct rxrpc_connection
*conn
=
257 container_of(work
, struct rxrpc_connection
, processor
);
259 u32 abort_code
= RX_PROTOCOL_ERROR
;
262 _enter("{%d}", conn
->debug_id
);
264 atomic_inc(&conn
->usage
);
266 if (test_and_clear_bit(RXRPC_CONN_CHALLENGE
, &conn
->events
)) {
267 rxrpc_secure_connection(conn
);
268 rxrpc_put_connection(conn
);
271 /* go through the conn-level event packets, releasing the ref on this
272 * connection that each one has when we've finished with it */
273 while ((skb
= skb_dequeue(&conn
->rx_queue
))) {
274 ret
= rxrpc_process_event(conn
, skb
, &abort_code
);
281 goto requeue_and_leave
;
284 rxrpc_put_connection(conn
);
291 rxrpc_put_connection(conn
);
296 skb_queue_head(&conn
->rx_queue
, skb
);
300 if (rxrpc_abort_connection(conn
, -ret
, abort_code
) < 0)
301 goto requeue_and_leave
;
302 rxrpc_put_connection(conn
);
309 * put a packet up for transport-level abort
311 void rxrpc_reject_packet(struct rxrpc_local
*local
, struct sk_buff
*skb
)
313 CHECK_SLAB_OKAY(&local
->usage
);
315 if (!atomic_inc_not_zero(&local
->usage
)) {
316 printk("resurrected on reject\n");
320 skb_queue_tail(&local
->reject_queue
, skb
);
321 rxrpc_queue_work(&local
->rejecter
);
325 * reject packets through the local endpoint
327 void rxrpc_reject_packets(struct work_struct
*work
)
331 struct sockaddr_in sin
;
333 struct rxrpc_skb_priv
*sp
;
334 struct rxrpc_wire_header whdr
;
335 struct rxrpc_local
*local
;
342 local
= container_of(work
, struct rxrpc_local
, rejecter
);
343 rxrpc_get_local(local
);
345 _enter("%d", local
->debug_id
);
347 iov
[0].iov_base
= &whdr
;
348 iov
[0].iov_len
= sizeof(whdr
);
349 iov
[1].iov_base
= &code
;
350 iov
[1].iov_len
= sizeof(code
);
351 size
= sizeof(whdr
) + sizeof(code
);
354 msg
.msg_control
= NULL
;
355 msg
.msg_controllen
= 0;
358 memset(&sa
, 0, sizeof(sa
));
359 sa
.sa
.sa_family
= local
->srx
.transport
.family
;
360 switch (sa
.sa
.sa_family
) {
362 msg
.msg_namelen
= sizeof(sa
.sin
);
369 memset(&whdr
, 0, sizeof(whdr
));
370 whdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
372 while ((skb
= skb_dequeue(&local
->reject_queue
))) {
374 switch (sa
.sa
.sa_family
) {
376 sa
.sin
.sin_port
= udp_hdr(skb
)->source
;
377 sa
.sin
.sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
378 code
= htonl(skb
->priority
);
380 whdr
.epoch
= htonl(sp
->hdr
.epoch
);
381 whdr
.cid
= htonl(sp
->hdr
.cid
);
382 whdr
.callNumber
= htonl(sp
->hdr
.callNumber
);
383 whdr
.serviceId
= htons(sp
->hdr
.serviceId
);
384 whdr
.flags
= sp
->hdr
.flags
;
385 whdr
.flags
^= RXRPC_CLIENT_INITIATED
;
386 whdr
.flags
&= RXRPC_CLIENT_INITIATED
;
388 kernel_sendmsg(local
->socket
, &msg
, iov
, 2, size
);
396 rxrpc_put_local(local
);
399 rxrpc_put_local(local
);