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 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | /* |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 28 | * Retransmit terminal ACK or ABORT of the previous call. |
| 29 | */ |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 30 | static void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn, |
| 31 | struct sk_buff *skb) |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 32 | { |
| 33 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 34 | struct rxrpc_channel *chan; |
| 35 | struct msghdr msg; |
| 36 | struct kvec iov; |
| 37 | struct { |
| 38 | struct rxrpc_wire_header whdr; |
| 39 | union { |
| 40 | struct { |
| 41 | __be32 code; |
| 42 | } abort; |
| 43 | struct { |
| 44 | struct rxrpc_ackpacket ack; |
David Howells | 2266ffd | 2016-08-24 13:06:14 +0100 | [diff] [blame] | 45 | u8 padding[3]; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 46 | struct rxrpc_ackinfo info; |
| 47 | }; |
| 48 | }; |
| 49 | } __attribute__((packed)) pkt; |
| 50 | size_t len; |
| 51 | u32 serial, mtu, call_id; |
| 52 | |
| 53 | _enter("%d", conn->debug_id); |
| 54 | |
| 55 | chan = &conn->channels[sp->hdr.cid & RXRPC_CHANNELMASK]; |
| 56 | |
| 57 | /* If the last call got moved on whilst we were waiting to run, just |
| 58 | * ignore this packet. |
| 59 | */ |
| 60 | call_id = READ_ONCE(chan->last_call); |
| 61 | /* Sync with __rxrpc_disconnect_call() */ |
| 62 | smp_rmb(); |
| 63 | if (call_id != sp->hdr.callNumber) |
| 64 | return; |
| 65 | |
| 66 | msg.msg_name = &conn->params.peer->srx.transport; |
| 67 | msg.msg_namelen = conn->params.peer->srx.transport_len; |
| 68 | msg.msg_control = NULL; |
| 69 | msg.msg_controllen = 0; |
| 70 | msg.msg_flags = 0; |
| 71 | |
| 72 | pkt.whdr.epoch = htonl(sp->hdr.epoch); |
| 73 | pkt.whdr.cid = htonl(sp->hdr.cid); |
| 74 | pkt.whdr.callNumber = htonl(sp->hdr.callNumber); |
| 75 | pkt.whdr.seq = 0; |
| 76 | pkt.whdr.type = chan->last_type; |
| 77 | pkt.whdr.flags = conn->out_clientflag; |
| 78 | pkt.whdr.userStatus = 0; |
| 79 | pkt.whdr.securityIndex = conn->security_ix; |
| 80 | pkt.whdr._rsvd = 0; |
| 81 | pkt.whdr.serviceId = htons(chan->last_service_id); |
| 82 | |
| 83 | len = sizeof(pkt.whdr); |
| 84 | switch (chan->last_type) { |
| 85 | case RXRPC_PACKET_TYPE_ABORT: |
| 86 | pkt.abort.code = htonl(chan->last_abort); |
| 87 | len += sizeof(pkt.abort); |
| 88 | break; |
| 89 | |
| 90 | case RXRPC_PACKET_TYPE_ACK: |
| 91 | mtu = conn->params.peer->if_mtu; |
| 92 | mtu -= conn->params.peer->hdrsize; |
| 93 | pkt.ack.bufferSpace = 0; |
| 94 | pkt.ack.maxSkew = htons(skb->priority); |
| 95 | pkt.ack.firstPacket = htonl(chan->last_seq); |
| 96 | pkt.ack.previousPacket = htonl(chan->last_seq - 1); |
| 97 | pkt.ack.serial = htonl(sp->hdr.serial); |
| 98 | pkt.ack.reason = RXRPC_ACK_DUPLICATE; |
| 99 | pkt.ack.nAcks = 0; |
| 100 | pkt.info.rxMTU = htonl(rxrpc_rx_mtu); |
| 101 | pkt.info.maxMTU = htonl(mtu); |
| 102 | pkt.info.rwind = htonl(rxrpc_rx_window_size); |
| 103 | pkt.info.jumbo_max = htonl(rxrpc_rx_jumbo_max); |
| 104 | len += sizeof(pkt.ack) + sizeof(pkt.info); |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | /* Resync with __rxrpc_disconnect_call() and check that the last call |
| 109 | * didn't get advanced whilst we were filling out the packets. |
| 110 | */ |
| 111 | smp_rmb(); |
| 112 | if (READ_ONCE(chan->last_call) != call_id) |
| 113 | return; |
| 114 | |
| 115 | iov.iov_base = &pkt; |
| 116 | iov.iov_len = len; |
| 117 | |
| 118 | serial = atomic_inc_return(&conn->serial); |
| 119 | pkt.whdr.serial = htonl(serial); |
| 120 | |
| 121 | switch (chan->last_type) { |
| 122 | case RXRPC_PACKET_TYPE_ABORT: |
| 123 | _proto("Tx ABORT %%%u { %d } [re]", serial, conn->local_abort); |
| 124 | break; |
| 125 | case RXRPC_PACKET_TYPE_ACK: |
| 126 | _proto("Tx ACK %%%u [re]", serial); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | kernel_sendmsg(conn->params.local->socket, &msg, &iov, 1, len); |
| 131 | _leave(""); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 136 | * pass a connection-level abort onto all calls on that connection |
| 137 | */ |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 138 | static void rxrpc_abort_calls(struct rxrpc_connection *conn, |
| 139 | enum rxrpc_call_completion compl, |
| 140 | u32 abort_code, int error) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 141 | { |
| 142 | struct rxrpc_call *call; |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 143 | bool queue; |
| 144 | int i, bit; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 145 | |
| 146 | _enter("{%d},%x", conn->debug_id, abort_code); |
| 147 | |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 148 | if (compl == RXRPC_CALL_LOCALLY_ABORTED) |
| 149 | bit = RXRPC_CALL_EV_CONN_ABORT; |
| 150 | else |
| 151 | bit = RXRPC_CALL_EV_RCVD_ABORT; |
| 152 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 153 | spin_lock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 154 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 155 | for (i = 0; i < RXRPC_MAXCALLS; i++) { |
| 156 | call = rcu_dereference_protected( |
| 157 | conn->channels[i].call, |
| 158 | lockdep_is_held(&conn->channel_lock)); |
David Howells | ccbd3db | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 159 | if (call) { |
David Howells | e34d423 | 2016-08-30 09:49:29 +0100 | [diff] [blame^] | 160 | rxrpc_see_call(call); |
David Howells | ccbd3db | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 161 | write_lock_bh(&call->state_lock); |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 162 | if (rxrpc_set_call_completion(call, compl, abort_code, |
| 163 | error)) { |
| 164 | set_bit(bit, &call->events); |
| 165 | queue = true; |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 166 | } |
David Howells | ccbd3db | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 167 | write_unlock_bh(&call->state_lock); |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 168 | if (queue) |
| 169 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 170 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 171 | } |
| 172 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 173 | spin_unlock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 174 | _leave(""); |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * generate a connection-level abort |
| 179 | */ |
| 180 | static int rxrpc_abort_connection(struct rxrpc_connection *conn, |
| 181 | u32 error, u32 abort_code) |
| 182 | { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 183 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 184 | struct msghdr msg; |
| 185 | struct kvec iov[2]; |
| 186 | __be32 word; |
| 187 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 188 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 189 | int ret; |
| 190 | |
| 191 | _enter("%d,,%u,%u", conn->debug_id, error, abort_code); |
| 192 | |
| 193 | /* generate a connection-level abort */ |
| 194 | spin_lock_bh(&conn->state_lock); |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 195 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 196 | spin_unlock_bh(&conn->state_lock); |
| 197 | _leave(" = 0 [already dead]"); |
| 198 | return 0; |
| 199 | } |
| 200 | |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 201 | conn->state = RXRPC_CONN_LOCALLY_ABORTED; |
| 202 | spin_unlock_bh(&conn->state_lock); |
| 203 | |
| 204 | rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code, error); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 205 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 206 | msg.msg_name = &conn->params.peer->srx.transport; |
| 207 | msg.msg_namelen = conn->params.peer->srx.transport_len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 208 | msg.msg_control = NULL; |
| 209 | msg.msg_controllen = 0; |
| 210 | msg.msg_flags = 0; |
| 211 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 212 | whdr.epoch = htonl(conn->proto.epoch); |
| 213 | whdr.cid = htonl(conn->proto.cid); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 214 | whdr.callNumber = 0; |
| 215 | whdr.seq = 0; |
| 216 | whdr.type = RXRPC_PACKET_TYPE_ABORT; |
| 217 | whdr.flags = conn->out_clientflag; |
| 218 | whdr.userStatus = 0; |
| 219 | whdr.securityIndex = conn->security_ix; |
| 220 | whdr._rsvd = 0; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 221 | whdr.serviceId = htons(conn->params.service_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 222 | |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 223 | word = htonl(conn->local_abort); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 224 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 225 | iov[0].iov_base = &whdr; |
| 226 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 227 | iov[1].iov_base = &word; |
| 228 | iov[1].iov_len = sizeof(word); |
| 229 | |
| 230 | len = iov[0].iov_len + iov[1].iov_len; |
| 231 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 232 | serial = atomic_inc_return(&conn->serial); |
| 233 | whdr.serial = htonl(serial); |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 234 | _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 235 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 236 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 237 | if (ret < 0) { |
| 238 | _debug("sendmsg failed: %d", ret); |
| 239 | return -EAGAIN; |
| 240 | } |
| 241 | |
| 242 | _leave(" = 0"); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * mark a call as being on a now-secured channel |
| 248 | * - must be called with softirqs disabled |
| 249 | */ |
Roel Kluin | 5eaa65b | 2008-12-10 15:18:31 -0800 | [diff] [blame] | 250 | static void rxrpc_call_is_secure(struct rxrpc_call *call) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 251 | { |
| 252 | _enter("%p", call); |
| 253 | if (call) { |
| 254 | read_lock(&call->state_lock); |
| 255 | if (call->state < RXRPC_CALL_COMPLETE && |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 256 | !test_and_set_bit(RXRPC_CALL_EV_SECURED, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 257 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 258 | read_unlock(&call->state_lock); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * connection-level Rx packet processor |
| 264 | */ |
| 265 | static int rxrpc_process_event(struct rxrpc_connection *conn, |
| 266 | struct sk_buff *skb, |
| 267 | u32 *_abort_code) |
| 268 | { |
| 269 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 270 | __be32 wtmp; |
| 271 | u32 abort_code; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 272 | int loop, ret; |
| 273 | |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 274 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
| 275 | kleave(" = -ECONNABORTED [%u]", conn->state); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 276 | return -ECONNABORTED; |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 277 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 278 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 279 | _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] | 280 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 281 | switch (sp->hdr.type) { |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 282 | case RXRPC_PACKET_TYPE_DATA: |
| 283 | case RXRPC_PACKET_TYPE_ACK: |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 284 | rxrpc_conn_retransmit_call(conn, skb); |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 285 | rxrpc_free_skb(skb); |
| 286 | return 0; |
| 287 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 288 | case RXRPC_PACKET_TYPE_ABORT: |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 289 | if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 290 | return -EPROTO; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 291 | abort_code = ntohl(wtmp); |
| 292 | _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 293 | |
| 294 | conn->state = RXRPC_CONN_REMOTELY_ABORTED; |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 295 | rxrpc_abort_calls(conn, 0, RXRPC_CALL_REMOTELY_ABORTED, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 296 | abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 297 | return -ECONNABORTED; |
| 298 | |
| 299 | case RXRPC_PACKET_TYPE_CHALLENGE: |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 300 | return conn->security->respond_to_challenge(conn, skb, |
| 301 | _abort_code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 302 | |
| 303 | case RXRPC_PACKET_TYPE_RESPONSE: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 304 | ret = conn->security->verify_response(conn, skb, _abort_code); |
| 305 | if (ret < 0) |
| 306 | return ret; |
| 307 | |
| 308 | ret = conn->security->init_connection_security(conn); |
| 309 | if (ret < 0) |
| 310 | return ret; |
| 311 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 312 | ret = conn->security->prime_packet_security(conn); |
| 313 | if (ret < 0) |
| 314 | return ret; |
| 315 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 316 | spin_lock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 317 | spin_lock(&conn->state_lock); |
| 318 | |
David Howells | bba304d | 2016-06-27 10:32:02 +0100 | [diff] [blame] | 319 | if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) { |
| 320 | conn->state = RXRPC_CONN_SERVICE; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 321 | for (loop = 0; loop < RXRPC_MAXCALLS; loop++) |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 322 | rxrpc_call_is_secure( |
| 323 | rcu_dereference_protected( |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 324 | conn->channels[loop].call, |
| 325 | lockdep_is_held(&conn->channel_lock))); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | spin_unlock(&conn->state_lock); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 329 | spin_unlock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 330 | return 0; |
| 331 | |
| 332 | default: |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 333 | _leave(" = -EPROTO [%u]", sp->hdr.type); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 334 | return -EPROTO; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * set up security and issue a challenge |
| 340 | */ |
| 341 | static void rxrpc_secure_connection(struct rxrpc_connection *conn) |
| 342 | { |
| 343 | u32 abort_code; |
| 344 | int ret; |
| 345 | |
| 346 | _enter("{%d}", conn->debug_id); |
| 347 | |
| 348 | ASSERT(conn->security_ix != 0); |
| 349 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 350 | if (!conn->params.key) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 351 | _debug("set up security"); |
| 352 | ret = rxrpc_init_server_conn_security(conn); |
| 353 | switch (ret) { |
| 354 | case 0: |
| 355 | break; |
| 356 | case -ENOENT: |
| 357 | abort_code = RX_CALL_DEAD; |
| 358 | goto abort; |
| 359 | default: |
| 360 | abort_code = RXKADNOAUTH; |
| 361 | goto abort; |
| 362 | } |
| 363 | } |
| 364 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 365 | if (conn->security->issue_challenge(conn) < 0) { |
| 366 | abort_code = RX_CALL_DEAD; |
| 367 | ret = -ENOMEM; |
| 368 | goto abort; |
| 369 | } |
| 370 | |
| 371 | _leave(""); |
| 372 | return; |
| 373 | |
| 374 | abort: |
| 375 | _debug("abort %d, %d", ret, abort_code); |
| 376 | rxrpc_abort_connection(conn, -ret, abort_code); |
| 377 | _leave(" [aborted]"); |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * connection-level event processor |
| 382 | */ |
| 383 | void rxrpc_process_connection(struct work_struct *work) |
| 384 | { |
| 385 | struct rxrpc_connection *conn = |
| 386 | container_of(work, struct rxrpc_connection, processor); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 387 | struct sk_buff *skb; |
| 388 | u32 abort_code = RX_PROTOCOL_ERROR; |
| 389 | int ret; |
| 390 | |
| 391 | _enter("{%d}", conn->debug_id); |
| 392 | |
David Howells | 2c4579e | 2016-06-27 10:32:03 +0100 | [diff] [blame] | 393 | if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 394 | rxrpc_secure_connection(conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 395 | |
| 396 | /* go through the conn-level event packets, releasing the ref on this |
| 397 | * connection that each one has when we've finished with it */ |
| 398 | while ((skb = skb_dequeue(&conn->rx_queue))) { |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 399 | rxrpc_see_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 400 | ret = rxrpc_process_event(conn, skb, &abort_code); |
| 401 | switch (ret) { |
| 402 | case -EPROTO: |
| 403 | case -EKEYEXPIRED: |
| 404 | case -EKEYREJECTED: |
| 405 | goto protocol_error; |
| 406 | case -EAGAIN: |
| 407 | goto requeue_and_leave; |
| 408 | case -ECONNABORTED: |
| 409 | default: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 410 | rxrpc_free_skb(skb); |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | out: |
| 416 | rxrpc_put_connection(conn); |
| 417 | _leave(""); |
| 418 | return; |
| 419 | |
| 420 | requeue_and_leave: |
| 421 | skb_queue_head(&conn->rx_queue, skb); |
| 422 | goto out; |
| 423 | |
| 424 | protocol_error: |
| 425 | if (rxrpc_abort_connection(conn, -ret, abort_code) < 0) |
| 426 | goto requeue_and_leave; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 427 | rxrpc_free_skb(skb); |
| 428 | _leave(" [EPROTO]"); |
| 429 | goto out; |
| 430 | } |
| 431 | |
| 432 | /* |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 433 | * put a packet up for transport-level abort |
| 434 | */ |
| 435 | void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb) |
| 436 | { |
| 437 | CHECK_SLAB_OKAY(&local->usage); |
| 438 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 439 | skb_queue_tail(&local->reject_queue, skb); |
David Howells | 5acbee4 | 2016-06-27 10:32:02 +0100 | [diff] [blame] | 440 | rxrpc_queue_local(local); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 444 | * reject packets through the local endpoint |
| 445 | */ |
David Howells | 4f95dd7 | 2016-04-04 14:00:35 +0100 | [diff] [blame] | 446 | void rxrpc_reject_packets(struct rxrpc_local *local) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 447 | { |
| 448 | union { |
| 449 | struct sockaddr sa; |
| 450 | struct sockaddr_in sin; |
| 451 | } sa; |
| 452 | struct rxrpc_skb_priv *sp; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 453 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 454 | struct sk_buff *skb; |
| 455 | struct msghdr msg; |
| 456 | struct kvec iov[2]; |
| 457 | size_t size; |
| 458 | __be32 code; |
| 459 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 460 | _enter("%d", local->debug_id); |
| 461 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 462 | iov[0].iov_base = &whdr; |
| 463 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 464 | iov[1].iov_base = &code; |
| 465 | iov[1].iov_len = sizeof(code); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 466 | size = sizeof(whdr) + sizeof(code); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 467 | |
| 468 | msg.msg_name = &sa; |
| 469 | msg.msg_control = NULL; |
| 470 | msg.msg_controllen = 0; |
| 471 | msg.msg_flags = 0; |
| 472 | |
| 473 | memset(&sa, 0, sizeof(sa)); |
| 474 | sa.sa.sa_family = local->srx.transport.family; |
| 475 | switch (sa.sa.sa_family) { |
| 476 | case AF_INET: |
| 477 | msg.msg_namelen = sizeof(sa.sin); |
| 478 | break; |
| 479 | default: |
| 480 | msg.msg_namelen = 0; |
| 481 | break; |
| 482 | } |
| 483 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 484 | memset(&whdr, 0, sizeof(whdr)); |
| 485 | whdr.type = RXRPC_PACKET_TYPE_ABORT; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 486 | |
| 487 | while ((skb = skb_dequeue(&local->reject_queue))) { |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 488 | rxrpc_see_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 489 | sp = rxrpc_skb(skb); |
| 490 | switch (sa.sa.sa_family) { |
| 491 | case AF_INET: |
| 492 | sa.sin.sin_port = udp_hdr(skb)->source; |
| 493 | sa.sin.sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 494 | code = htonl(skb->priority); |
| 495 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 496 | whdr.epoch = htonl(sp->hdr.epoch); |
| 497 | whdr.cid = htonl(sp->hdr.cid); |
| 498 | whdr.callNumber = htonl(sp->hdr.callNumber); |
| 499 | whdr.serviceId = htons(sp->hdr.serviceId); |
| 500 | whdr.flags = sp->hdr.flags; |
| 501 | whdr.flags ^= RXRPC_CLIENT_INITIATED; |
| 502 | whdr.flags &= RXRPC_CLIENT_INITIATED; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 503 | |
| 504 | kernel_sendmsg(local->socket, &msg, iov, 2, size); |
| 505 | break; |
| 506 | |
| 507 | default: |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | rxrpc_free_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 512 | } |
| 513 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 514 | _leave(""); |
| 515 | } |