David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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; |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 43 | if (state == RXRPC_CALL_LOCALLY_ABORTED) { |
| 44 | call->local_abort = conn->local_abort; |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 45 | set_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events); |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 46 | } else { |
| 47 | call->remote_abort = conn->remote_abort; |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 48 | set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events); |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 49 | } |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 50 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 65 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 66 | struct msghdr msg; |
| 67 | struct kvec iov[2]; |
| 68 | __be32 word; |
| 69 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 70 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | |
David Howells | 6dd050f | 2016-04-07 17:23:44 +0100 | [diff] [blame] | 89 | msg.msg_name = &conn->trans->peer->srx.transport; |
| 90 | msg.msg_namelen = conn->trans->peer->srx.transport_len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 91 | msg.msg_control = NULL; |
| 92 | msg.msg_controllen = 0; |
| 93 | msg.msg_flags = 0; |
| 94 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 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); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 105 | |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 106 | word = htonl(conn->local_abort); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 107 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 108 | iov[0].iov_base = &whdr; |
| 109 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 115 | serial = atomic_inc_return(&conn->serial); |
| 116 | whdr.serial = htonl(serial); |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 117 | _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | */ |
Roel Kluin | 5eaa65b | 2008-12-10 15:18:31 -0800 | [diff] [blame] | 133 | static void rxrpc_call_is_secure(struct rxrpc_call *call) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 134 | { |
| 135 | _enter("%p", call); |
| 136 | if (call) { |
| 137 | read_lock(&call->state_lock); |
| 138 | if (call->state < RXRPC_CALL_COMPLETE && |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 139 | !test_and_set_bit(RXRPC_CALL_EV_SECURED, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 140 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 153 | __be32 wtmp; |
| 154 | u32 abort_code; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 155 | int loop, ret; |
| 156 | |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 157 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
| 158 | kleave(" = -ECONNABORTED [%u]", conn->state); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 159 | return -ECONNABORTED; |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 160 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 161 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 162 | _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial); |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 163 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 164 | switch (sp->hdr.type) { |
| 165 | case RXRPC_PACKET_TYPE_ABORT: |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 166 | if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 167 | return -EPROTO; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 168 | abort_code = ntohl(wtmp); |
| 169 | _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 170 | |
| 171 | conn->state = RXRPC_CONN_REMOTELY_ABORTED; |
| 172 | rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 173 | abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 174 | return -ECONNABORTED; |
| 175 | |
| 176 | case RXRPC_PACKET_TYPE_CHALLENGE: |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 177 | return conn->security->respond_to_challenge(conn, skb, |
| 178 | _abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 179 | |
| 180 | case RXRPC_PACKET_TYPE_RESPONSE: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 181 | ret = conn->security->verify_response(conn, skb, _abort_code); |
| 182 | if (ret < 0) |
| 183 | return ret; |
| 184 | |
| 185 | ret = conn->security->init_connection_security(conn); |
| 186 | if (ret < 0) |
| 187 | return ret; |
| 188 | |
| 189 | conn->security->prime_packet_security(conn); |
| 190 | read_lock_bh(&conn->lock); |
| 191 | spin_lock(&conn->state_lock); |
| 192 | |
| 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]); |
| 197 | } |
| 198 | |
| 199 | spin_unlock(&conn->state_lock); |
| 200 | read_unlock_bh(&conn->lock); |
| 201 | return 0; |
| 202 | |
| 203 | default: |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 204 | _leave(" = -EPROTO [%u]", sp->hdr.type); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 205 | return -EPROTO; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * set up security and issue a challenge |
| 211 | */ |
| 212 | static void rxrpc_secure_connection(struct rxrpc_connection *conn) |
| 213 | { |
| 214 | u32 abort_code; |
| 215 | int ret; |
| 216 | |
| 217 | _enter("{%d}", conn->debug_id); |
| 218 | |
| 219 | ASSERT(conn->security_ix != 0); |
| 220 | |
| 221 | if (!conn->key) { |
| 222 | _debug("set up security"); |
| 223 | ret = rxrpc_init_server_conn_security(conn); |
| 224 | switch (ret) { |
| 225 | case 0: |
| 226 | break; |
| 227 | case -ENOENT: |
| 228 | abort_code = RX_CALL_DEAD; |
| 229 | goto abort; |
| 230 | default: |
| 231 | abort_code = RXKADNOAUTH; |
| 232 | goto abort; |
| 233 | } |
| 234 | } |
| 235 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 236 | if (conn->security->issue_challenge(conn) < 0) { |
| 237 | abort_code = RX_CALL_DEAD; |
| 238 | ret = -ENOMEM; |
| 239 | goto abort; |
| 240 | } |
| 241 | |
| 242 | _leave(""); |
| 243 | return; |
| 244 | |
| 245 | abort: |
| 246 | _debug("abort %d, %d", ret, abort_code); |
| 247 | rxrpc_abort_connection(conn, -ret, abort_code); |
| 248 | _leave(" [aborted]"); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * connection-level event processor |
| 253 | */ |
| 254 | void rxrpc_process_connection(struct work_struct *work) |
| 255 | { |
| 256 | struct rxrpc_connection *conn = |
| 257 | container_of(work, struct rxrpc_connection, processor); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 258 | struct sk_buff *skb; |
| 259 | u32 abort_code = RX_PROTOCOL_ERROR; |
| 260 | int ret; |
| 261 | |
| 262 | _enter("{%d}", conn->debug_id); |
| 263 | |
| 264 | atomic_inc(&conn->usage); |
| 265 | |
| 266 | if (test_and_clear_bit(RXRPC_CONN_CHALLENGE, &conn->events)) { |
| 267 | rxrpc_secure_connection(conn); |
| 268 | rxrpc_put_connection(conn); |
| 269 | } |
| 270 | |
| 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))) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 274 | ret = rxrpc_process_event(conn, skb, &abort_code); |
| 275 | switch (ret) { |
| 276 | case -EPROTO: |
| 277 | case -EKEYEXPIRED: |
| 278 | case -EKEYREJECTED: |
| 279 | goto protocol_error; |
| 280 | case -EAGAIN: |
| 281 | goto requeue_and_leave; |
| 282 | case -ECONNABORTED: |
| 283 | default: |
| 284 | rxrpc_put_connection(conn); |
| 285 | rxrpc_free_skb(skb); |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | out: |
| 291 | rxrpc_put_connection(conn); |
| 292 | _leave(""); |
| 293 | return; |
| 294 | |
| 295 | requeue_and_leave: |
| 296 | skb_queue_head(&conn->rx_queue, skb); |
| 297 | goto out; |
| 298 | |
| 299 | protocol_error: |
| 300 | if (rxrpc_abort_connection(conn, -ret, abort_code) < 0) |
| 301 | goto requeue_and_leave; |
| 302 | rxrpc_put_connection(conn); |
| 303 | rxrpc_free_skb(skb); |
| 304 | _leave(" [EPROTO]"); |
| 305 | goto out; |
| 306 | } |
| 307 | |
| 308 | /* |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 309 | * put a packet up for transport-level abort |
| 310 | */ |
| 311 | void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb) |
| 312 | { |
| 313 | CHECK_SLAB_OKAY(&local->usage); |
| 314 | |
| 315 | if (!atomic_inc_not_zero(&local->usage)) { |
| 316 | printk("resurrected on reject\n"); |
| 317 | BUG(); |
| 318 | } |
| 319 | |
| 320 | skb_queue_tail(&local->reject_queue, skb); |
| 321 | rxrpc_queue_work(&local->rejecter); |
| 322 | } |
| 323 | |
| 324 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 325 | * reject packets through the local endpoint |
| 326 | */ |
| 327 | void rxrpc_reject_packets(struct work_struct *work) |
| 328 | { |
| 329 | union { |
| 330 | struct sockaddr sa; |
| 331 | struct sockaddr_in sin; |
| 332 | } sa; |
| 333 | struct rxrpc_skb_priv *sp; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 334 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 335 | struct rxrpc_local *local; |
| 336 | struct sk_buff *skb; |
| 337 | struct msghdr msg; |
| 338 | struct kvec iov[2]; |
| 339 | size_t size; |
| 340 | __be32 code; |
| 341 | |
| 342 | local = container_of(work, struct rxrpc_local, rejecter); |
| 343 | rxrpc_get_local(local); |
| 344 | |
| 345 | _enter("%d", local->debug_id); |
| 346 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 347 | iov[0].iov_base = &whdr; |
| 348 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 349 | iov[1].iov_base = &code; |
| 350 | iov[1].iov_len = sizeof(code); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 351 | size = sizeof(whdr) + sizeof(code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 352 | |
| 353 | msg.msg_name = &sa; |
| 354 | msg.msg_control = NULL; |
| 355 | msg.msg_controllen = 0; |
| 356 | msg.msg_flags = 0; |
| 357 | |
| 358 | memset(&sa, 0, sizeof(sa)); |
| 359 | sa.sa.sa_family = local->srx.transport.family; |
| 360 | switch (sa.sa.sa_family) { |
| 361 | case AF_INET: |
| 362 | msg.msg_namelen = sizeof(sa.sin); |
| 363 | break; |
| 364 | default: |
| 365 | msg.msg_namelen = 0; |
| 366 | break; |
| 367 | } |
| 368 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 369 | memset(&whdr, 0, sizeof(whdr)); |
| 370 | whdr.type = RXRPC_PACKET_TYPE_ABORT; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 371 | |
| 372 | while ((skb = skb_dequeue(&local->reject_queue))) { |
| 373 | sp = rxrpc_skb(skb); |
| 374 | switch (sa.sa.sa_family) { |
| 375 | case AF_INET: |
| 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); |
| 379 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 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; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 387 | |
| 388 | kernel_sendmsg(local->socket, &msg, iov, 2, size); |
| 389 | break; |
| 390 | |
| 391 | default: |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | rxrpc_free_skb(skb); |
| 396 | rxrpc_put_local(local); |
| 397 | } |
| 398 | |
| 399 | rxrpc_put_local(local); |
| 400 | _leave(""); |
| 401 | } |