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