David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 1 | /* Service connection management |
| 2 | * |
| 3 | * Copyright (C) 2016 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 Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/slab.h> |
| 13 | #include "ar-internal.h" |
| 14 | |
| 15 | /* |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 16 | * Find a service connection under RCU conditions. |
| 17 | * |
| 18 | * We could use a hash table, but that is subject to bucket stuffing by an |
| 19 | * attacker as the client gets to pick the epoch and cid values and would know |
| 20 | * the hash function. So, instead, we use a hash table for the peer and from |
| 21 | * that an rbtree to find the service connection. Under ordinary circumstances |
| 22 | * it might be slower than a large hash table, but it is at least limited in |
| 23 | * depth. |
| 24 | */ |
| 25 | struct rxrpc_connection *rxrpc_find_service_conn_rcu(struct rxrpc_peer *peer, |
| 26 | struct sk_buff *skb) |
| 27 | { |
| 28 | struct rxrpc_connection *conn = NULL; |
| 29 | struct rxrpc_conn_proto k; |
| 30 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 31 | struct rb_node *p; |
| 32 | unsigned int seq = 0; |
| 33 | |
| 34 | k.epoch = sp->hdr.epoch; |
| 35 | k.cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 36 | |
| 37 | do { |
| 38 | /* Unfortunately, rbtree walking doesn't give reliable results |
| 39 | * under just the RCU read lock, so we have to check for |
| 40 | * changes. |
| 41 | */ |
| 42 | read_seqbegin_or_lock(&peer->service_conn_lock, &seq); |
| 43 | |
| 44 | p = rcu_dereference_raw(peer->service_conns.rb_node); |
| 45 | while (p) { |
| 46 | conn = rb_entry(p, struct rxrpc_connection, service_node); |
| 47 | |
| 48 | if (conn->proto.index_key < k.index_key) |
| 49 | p = rcu_dereference_raw(p->rb_left); |
| 50 | else if (conn->proto.index_key > k.index_key) |
| 51 | p = rcu_dereference_raw(p->rb_right); |
| 52 | else |
| 53 | goto done; |
| 54 | conn = NULL; |
| 55 | } |
| 56 | } while (need_seqretry(&peer->service_conn_lock, seq)); |
| 57 | |
| 58 | done: |
| 59 | done_seqretry(&peer->service_conn_lock, seq); |
| 60 | _leave(" = %d", conn ? conn->debug_id : -1); |
| 61 | return conn; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Insert a service connection into a peer's tree, thereby making it a target |
| 66 | * for incoming packets. |
| 67 | */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 68 | static void rxrpc_publish_service_conn(struct rxrpc_peer *peer, |
| 69 | struct rxrpc_connection *conn) |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 70 | { |
| 71 | struct rxrpc_connection *cursor = NULL; |
| 72 | struct rxrpc_conn_proto k = conn->proto; |
| 73 | struct rb_node **pp, *parent; |
| 74 | |
| 75 | write_seqlock_bh(&peer->service_conn_lock); |
| 76 | |
| 77 | pp = &peer->service_conns.rb_node; |
| 78 | parent = NULL; |
| 79 | while (*pp) { |
| 80 | parent = *pp; |
| 81 | cursor = rb_entry(parent, |
| 82 | struct rxrpc_connection, service_node); |
| 83 | |
| 84 | if (cursor->proto.index_key < k.index_key) |
| 85 | pp = &(*pp)->rb_left; |
| 86 | else if (cursor->proto.index_key > k.index_key) |
| 87 | pp = &(*pp)->rb_right; |
| 88 | else |
| 89 | goto found_extant_conn; |
| 90 | } |
| 91 | |
| 92 | rb_link_node_rcu(&conn->service_node, parent, pp); |
| 93 | rb_insert_color(&conn->service_node, &peer->service_conns); |
| 94 | conn_published: |
| 95 | set_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags); |
| 96 | write_sequnlock_bh(&peer->service_conn_lock); |
| 97 | _leave(" = %d [new]", conn->debug_id); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 98 | return; |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 99 | |
| 100 | found_extant_conn: |
| 101 | if (atomic_read(&cursor->usage) == 0) |
| 102 | goto replace_old_connection; |
| 103 | write_sequnlock_bh(&peer->service_conn_lock); |
| 104 | /* We should not be able to get here. rxrpc_incoming_connection() is |
| 105 | * called in a non-reentrant context, so there can't be a race to |
| 106 | * insert a new connection. |
| 107 | */ |
| 108 | BUG(); |
| 109 | |
| 110 | replace_old_connection: |
| 111 | /* The old connection is from an outdated epoch. */ |
| 112 | _debug("replace conn"); |
| 113 | rb_replace_node_rcu(&cursor->service_node, |
| 114 | &conn->service_node, |
| 115 | &peer->service_conns); |
| 116 | clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &cursor->flags); |
| 117 | goto conn_published; |
| 118 | } |
| 119 | |
| 120 | /* |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 121 | * Preallocate a service connection. The connection is placed on the proc and |
| 122 | * reap lists so that we don't have to get the lock from BH context. |
| 123 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 124 | struct rxrpc_connection *rxrpc_prealloc_service_connection(struct rxrpc_net *rxnet, |
| 125 | gfp_t gfp) |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 126 | { |
| 127 | struct rxrpc_connection *conn = rxrpc_alloc_connection(gfp); |
| 128 | |
| 129 | if (conn) { |
| 130 | /* We maintain an extra ref on the connection whilst it is on |
| 131 | * the rxrpc_connections list. |
| 132 | */ |
| 133 | conn->state = RXRPC_CONN_SERVICE_PREALLOC; |
| 134 | atomic_set(&conn->usage, 2); |
| 135 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 136 | write_lock(&rxnet->conn_lock); |
| 137 | list_add_tail(&conn->link, &rxnet->service_conns); |
| 138 | list_add_tail(&conn->proc_link, &rxnet->conn_proc_list); |
| 139 | write_unlock(&rxnet->conn_lock); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 140 | |
| 141 | trace_rxrpc_conn(conn, rxrpc_conn_new_service, |
| 142 | atomic_read(&conn->usage), |
| 143 | __builtin_return_address(0)); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return conn; |
| 147 | } |
| 148 | |
| 149 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 150 | * Set up an incoming connection. This is called in BH context with the RCU |
| 151 | * read lock held. |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 152 | */ |
David Howells | 4722974 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 153 | void rxrpc_new_incoming_connection(struct rxrpc_sock *rx, |
| 154 | struct rxrpc_connection *conn, |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 155 | struct sk_buff *skb) |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 156 | { |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 157 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 158 | |
| 159 | _enter(""); |
| 160 | |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 161 | conn->proto.epoch = sp->hdr.epoch; |
| 162 | conn->proto.cid = sp->hdr.cid & RXRPC_CIDMASK; |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 163 | conn->params.service_id = sp->hdr.serviceId; |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 164 | conn->service_id = sp->hdr.serviceId; |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 165 | conn->security_ix = sp->hdr.securityIndex; |
| 166 | conn->out_clientflag = 0; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 167 | if (conn->security_ix) |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 168 | conn->state = RXRPC_CONN_SERVICE_UNSECURED; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 169 | else |
| 170 | conn->state = RXRPC_CONN_SERVICE; |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 171 | |
David Howells | 4722974 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 172 | /* See if we should upgrade the service. This can only happen on the |
| 173 | * first packet on a new connection. Once done, it applies to all |
| 174 | * subsequent calls on that connection. |
| 175 | */ |
| 176 | if (sp->hdr.userStatus == RXRPC_USERSTATUS_SERVICE_UPGRADE && |
| 177 | conn->service_id == rx->service_upgrade.from) |
| 178 | conn->service_id = rx->service_upgrade.to; |
| 179 | |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 180 | /* Make the connection a target for incoming packets. */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 181 | rxrpc_publish_service_conn(conn->params.peer, conn); |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 182 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 183 | _net("CONNECTION new %d {%x}", conn->debug_id, conn->proto.cid); |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 184 | } |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * Remove the service connection from the peer's tree, thereby removing it as a |
| 188 | * target for incoming packets. |
| 189 | */ |
| 190 | void rxrpc_unpublish_service_conn(struct rxrpc_connection *conn) |
| 191 | { |
| 192 | struct rxrpc_peer *peer = conn->params.peer; |
| 193 | |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 194 | write_seqlock_bh(&peer->service_conn_lock); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 195 | if (test_and_clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags)) |
| 196 | rb_erase(&conn->service_node, &peer->service_conns); |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 197 | write_sequnlock_bh(&peer->service_conn_lock); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 198 | } |