]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/rxrpc/conn_event.c
rxrpc: Access socket accept queue under right lock
[mirror_ubuntu-bionic-kernel.git] / net / rxrpc / conn_event.c
CommitLineData
17926a79
DH
1/* connection-level event 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>
22#include <net/sock.h>
23#include <net/af_rxrpc.h>
24#include <net/ip.h>
25#include "ar-internal.h"
26
27/*
28 * pass a connection-level abort onto all calls on that connection
29 */
30static void rxrpc_abort_calls(struct rxrpc_connection *conn, int state,
31 u32 abort_code)
32{
33 struct rxrpc_call *call;
34 struct rb_node *p;
35
36 _enter("{%d},%x", conn->debug_id, abort_code);
37
38 read_lock_bh(&conn->lock);
39
40 for (p = rb_first(&conn->calls); p; p = rb_next(p)) {
41 call = rb_entry(p, struct rxrpc_call, conn_node);
42 write_lock(&call->state_lock);
43 if (call->state <= RXRPC_CALL_COMPLETE) {
44 call->state = state;
dc44b3a0
DH
45 if (state == RXRPC_CALL_LOCALLY_ABORTED) {
46 call->local_abort = conn->local_abort;
4c198ad1 47 set_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
dc44b3a0
DH
48 } else {
49 call->remote_abort = conn->remote_abort;
4c198ad1 50 set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
dc44b3a0 51 }
651350d1 52 rxrpc_queue_call(call);
17926a79
DH
53 }
54 write_unlock(&call->state_lock);
55 }
56
57 read_unlock_bh(&conn->lock);
58 _leave("");
59}
60
61/*
62 * generate a connection-level abort
63 */
64static int rxrpc_abort_connection(struct rxrpc_connection *conn,
65 u32 error, u32 abort_code)
66{
0d12f8a4 67 struct rxrpc_wire_header whdr;
17926a79
DH
68 struct msghdr msg;
69 struct kvec iov[2];
70 __be32 word;
71 size_t len;
0d12f8a4 72 u32 serial;
17926a79
DH
73 int ret;
74
75 _enter("%d,,%u,%u", conn->debug_id, error, abort_code);
76
77 /* generate a connection-level abort */
78 spin_lock_bh(&conn->state_lock);
79 if (conn->state < RXRPC_CONN_REMOTELY_ABORTED) {
80 conn->state = RXRPC_CONN_LOCALLY_ABORTED;
81 conn->error = error;
82 spin_unlock_bh(&conn->state_lock);
83 } else {
84 spin_unlock_bh(&conn->state_lock);
85 _leave(" = 0 [already dead]");
86 return 0;
87 }
88
89 rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code);
90
85f32278
DH
91 msg.msg_name = &conn->params.peer->srx.transport;
92 msg.msg_namelen = conn->params.peer->srx.transport_len;
17926a79
DH
93 msg.msg_control = NULL;
94 msg.msg_controllen = 0;
95 msg.msg_flags = 0;
96
19ffa01c
DH
97 whdr.epoch = htonl(conn->proto.epoch);
98 whdr.cid = htonl(conn->proto.cid);
0d12f8a4
DH
99 whdr.callNumber = 0;
100 whdr.seq = 0;
101 whdr.type = RXRPC_PACKET_TYPE_ABORT;
102 whdr.flags = conn->out_clientflag;
103 whdr.userStatus = 0;
104 whdr.securityIndex = conn->security_ix;
105 whdr._rsvd = 0;
19ffa01c 106 whdr.serviceId = htons(conn->params.service_id);
17926a79 107
dc44b3a0 108 word = htonl(conn->local_abort);
17926a79 109
0d12f8a4
DH
110 iov[0].iov_base = &whdr;
111 iov[0].iov_len = sizeof(whdr);
17926a79
DH
112 iov[1].iov_base = &word;
113 iov[1].iov_len = sizeof(word);
114
115 len = iov[0].iov_len + iov[1].iov_len;
116
0d12f8a4
DH
117 serial = atomic_inc_return(&conn->serial);
118 whdr.serial = htonl(serial);
dc44b3a0 119 _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort);
17926a79 120
85f32278 121 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
17926a79
DH
122 if (ret < 0) {
123 _debug("sendmsg failed: %d", ret);
124 return -EAGAIN;
125 }
126
127 _leave(" = 0");
128 return 0;
129}
130
131/*
132 * mark a call as being on a now-secured channel
133 * - must be called with softirqs disabled
134 */
5eaa65b2 135static void rxrpc_call_is_secure(struct rxrpc_call *call)
17926a79
DH
136{
137 _enter("%p", call);
138 if (call) {
139 read_lock(&call->state_lock);
140 if (call->state < RXRPC_CALL_COMPLETE &&
4c198ad1 141 !test_and_set_bit(RXRPC_CALL_EV_SECURED, &call->events))
651350d1 142 rxrpc_queue_call(call);
17926a79
DH
143 read_unlock(&call->state_lock);
144 }
145}
146
147/*
148 * connection-level Rx packet processor
149 */
150static int rxrpc_process_event(struct rxrpc_connection *conn,
151 struct sk_buff *skb,
152 u32 *_abort_code)
153{
154 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
0d12f8a4
DH
155 __be32 wtmp;
156 u32 abort_code;
17926a79
DH
157 int loop, ret;
158
519d2567
DH
159 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
160 kleave(" = -ECONNABORTED [%u]", conn->state);
17926a79 161 return -ECONNABORTED;
519d2567 162 }
17926a79 163
0d12f8a4 164 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
519d2567 165
17926a79
DH
166 switch (sp->hdr.type) {
167 case RXRPC_PACKET_TYPE_ABORT:
0d12f8a4 168 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
17926a79 169 return -EPROTO;
0d12f8a4
DH
170 abort_code = ntohl(wtmp);
171 _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
17926a79
DH
172
173 conn->state = RXRPC_CONN_REMOTELY_ABORTED;
174 rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
0d12f8a4 175 abort_code);
17926a79
DH
176 return -ECONNABORTED;
177
178 case RXRPC_PACKET_TYPE_CHALLENGE:
e0e4d82f
DH
179 return conn->security->respond_to_challenge(conn, skb,
180 _abort_code);
17926a79
DH
181
182 case RXRPC_PACKET_TYPE_RESPONSE:
17926a79
DH
183 ret = conn->security->verify_response(conn, skb, _abort_code);
184 if (ret < 0)
185 return ret;
186
187 ret = conn->security->init_connection_security(conn);
188 if (ret < 0)
189 return ret;
190
a263629d
HX
191 ret = conn->security->prime_packet_security(conn);
192 if (ret < 0)
193 return ret;
194
17926a79
DH
195 read_lock_bh(&conn->lock);
196 spin_lock(&conn->state_lock);
197
bba304db
DH
198 if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
199 conn->state = RXRPC_CONN_SERVICE;
17926a79 200 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
dee46364
DH
201 rxrpc_call_is_secure(
202 rcu_dereference_protected(
203 conn->channels[loop],
204 lockdep_is_held(&conn->lock)));
17926a79
DH
205 }
206
207 spin_unlock(&conn->state_lock);
208 read_unlock_bh(&conn->lock);
209 return 0;
210
211 default:
519d2567 212 _leave(" = -EPROTO [%u]", sp->hdr.type);
17926a79
DH
213 return -EPROTO;
214 }
215}
216
217/*
218 * set up security and issue a challenge
219 */
220static void rxrpc_secure_connection(struct rxrpc_connection *conn)
221{
222 u32 abort_code;
223 int ret;
224
225 _enter("{%d}", conn->debug_id);
226
227 ASSERT(conn->security_ix != 0);
228
19ffa01c 229 if (!conn->params.key) {
17926a79
DH
230 _debug("set up security");
231 ret = rxrpc_init_server_conn_security(conn);
232 switch (ret) {
233 case 0:
234 break;
235 case -ENOENT:
236 abort_code = RX_CALL_DEAD;
237 goto abort;
238 default:
239 abort_code = RXKADNOAUTH;
240 goto abort;
241 }
242 }
243
17926a79
DH
244 if (conn->security->issue_challenge(conn) < 0) {
245 abort_code = RX_CALL_DEAD;
246 ret = -ENOMEM;
247 goto abort;
248 }
249
250 _leave("");
251 return;
252
253abort:
254 _debug("abort %d, %d", ret, abort_code);
255 rxrpc_abort_connection(conn, -ret, abort_code);
256 _leave(" [aborted]");
257}
258
259/*
260 * connection-level event processor
261 */
262void rxrpc_process_connection(struct work_struct *work)
263{
264 struct rxrpc_connection *conn =
265 container_of(work, struct rxrpc_connection, processor);
17926a79
DH
266 struct sk_buff *skb;
267 u32 abort_code = RX_PROTOCOL_ERROR;
268 int ret;
269
270 _enter("{%d}", conn->debug_id);
271
2c4579e4 272 if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
17926a79 273 rxrpc_secure_connection(conn);
17926a79
DH
274
275 /* go through the conn-level event packets, releasing the ref on this
276 * connection that each one has when we've finished with it */
277 while ((skb = skb_dequeue(&conn->rx_queue))) {
17926a79
DH
278 ret = rxrpc_process_event(conn, skb, &abort_code);
279 switch (ret) {
280 case -EPROTO:
281 case -EKEYEXPIRED:
282 case -EKEYREJECTED:
283 goto protocol_error;
284 case -EAGAIN:
285 goto requeue_and_leave;
286 case -ECONNABORTED:
287 default:
17926a79
DH
288 rxrpc_free_skb(skb);
289 break;
290 }
291 }
292
293out:
294 rxrpc_put_connection(conn);
295 _leave("");
296 return;
297
298requeue_and_leave:
299 skb_queue_head(&conn->rx_queue, skb);
300 goto out;
301
302protocol_error:
303 if (rxrpc_abort_connection(conn, -ret, abort_code) < 0)
304 goto requeue_and_leave;
17926a79
DH
305 rxrpc_free_skb(skb);
306 _leave(" [EPROTO]");
307 goto out;
308}
309
651350d1
DH
310/*
311 * put a packet up for transport-level abort
312 */
313void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
314{
315 CHECK_SLAB_OKAY(&local->usage);
316
651350d1 317 skb_queue_tail(&local->reject_queue, skb);
5acbee46 318 rxrpc_queue_local(local);
651350d1
DH
319}
320
17926a79
DH
321/*
322 * reject packets through the local endpoint
323 */
4f95dd78 324void rxrpc_reject_packets(struct rxrpc_local *local)
17926a79
DH
325{
326 union {
327 struct sockaddr sa;
328 struct sockaddr_in sin;
329 } sa;
330 struct rxrpc_skb_priv *sp;
0d12f8a4 331 struct rxrpc_wire_header whdr;
17926a79
DH
332 struct sk_buff *skb;
333 struct msghdr msg;
334 struct kvec iov[2];
335 size_t size;
336 __be32 code;
337
17926a79
DH
338 _enter("%d", local->debug_id);
339
0d12f8a4
DH
340 iov[0].iov_base = &whdr;
341 iov[0].iov_len = sizeof(whdr);
17926a79
DH
342 iov[1].iov_base = &code;
343 iov[1].iov_len = sizeof(code);
0d12f8a4 344 size = sizeof(whdr) + sizeof(code);
17926a79
DH
345
346 msg.msg_name = &sa;
347 msg.msg_control = NULL;
348 msg.msg_controllen = 0;
349 msg.msg_flags = 0;
350
351 memset(&sa, 0, sizeof(sa));
352 sa.sa.sa_family = local->srx.transport.family;
353 switch (sa.sa.sa_family) {
354 case AF_INET:
355 msg.msg_namelen = sizeof(sa.sin);
356 break;
357 default:
358 msg.msg_namelen = 0;
359 break;
360 }
361
0d12f8a4
DH
362 memset(&whdr, 0, sizeof(whdr));
363 whdr.type = RXRPC_PACKET_TYPE_ABORT;
17926a79
DH
364
365 while ((skb = skb_dequeue(&local->reject_queue))) {
366 sp = rxrpc_skb(skb);
367 switch (sa.sa.sa_family) {
368 case AF_INET:
369 sa.sin.sin_port = udp_hdr(skb)->source;
370 sa.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
371 code = htonl(skb->priority);
372
0d12f8a4
DH
373 whdr.epoch = htonl(sp->hdr.epoch);
374 whdr.cid = htonl(sp->hdr.cid);
375 whdr.callNumber = htonl(sp->hdr.callNumber);
376 whdr.serviceId = htons(sp->hdr.serviceId);
377 whdr.flags = sp->hdr.flags;
378 whdr.flags ^= RXRPC_CLIENT_INITIATED;
379 whdr.flags &= RXRPC_CLIENT_INITIATED;
17926a79
DH
380
381 kernel_sendmsg(local->socket, &msg, iov, 2, size);
382 break;
383
384 default:
385 break;
386 }
387
388 rxrpc_free_skb(skb);
17926a79
DH
389 }
390
17926a79
DH
391 _leave("");
392}