1 // SPDX-License-Identifier: GPL-2.0-or-later 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Service connection management 2 /* Service connection management 3 * 3 * 4 * Copyright (C) 2016 Red Hat, Inc. All Rights 4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.c 5 * Written by David Howells (dhowells@redhat.com) 6 */ 6 */ 7 7 8 #include <linux/slab.h> 8 #include <linux/slab.h> 9 #include "ar-internal.h" 9 #include "ar-internal.h" 10 10 >> 11 static struct rxrpc_bundle rxrpc_service_dummy_bundle = { >> 12 .usage = ATOMIC_INIT(1), >> 13 .debug_id = UINT_MAX, >> 14 .channel_lock = __SPIN_LOCK_UNLOCKED(&rxrpc_service_dummy_bundle.channel_lock), >> 15 }; >> 16 11 /* 17 /* 12 * Find a service connection under RCU conditi 18 * Find a service connection under RCU conditions. 13 * 19 * 14 * We could use a hash table, but that is subj 20 * We could use a hash table, but that is subject to bucket stuffing by an 15 * attacker as the client gets to pick the epo 21 * attacker as the client gets to pick the epoch and cid values and would know 16 * the hash function. So, instead, we use a h 22 * the hash function. So, instead, we use a hash table for the peer and from 17 * that an rbtree to find the service connecti 23 * that an rbtree to find the service connection. Under ordinary circumstances 18 * it might be slower than a large hash table, 24 * it might be slower than a large hash table, but it is at least limited in 19 * depth. 25 * depth. 20 */ 26 */ 21 struct rxrpc_connection *rxrpc_find_service_co 27 struct rxrpc_connection *rxrpc_find_service_conn_rcu(struct rxrpc_peer *peer, 22 28 struct sk_buff *skb) 23 { 29 { 24 struct rxrpc_connection *conn = NULL; 30 struct rxrpc_connection *conn = NULL; 25 struct rxrpc_conn_proto k; 31 struct rxrpc_conn_proto k; 26 struct rxrpc_skb_priv *sp = rxrpc_skb( 32 struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 27 struct rb_node *p; 33 struct rb_node *p; 28 unsigned int seq = 1; !! 34 unsigned int seq = 0; 29 35 30 k.epoch = sp->hdr.epoch; 36 k.epoch = sp->hdr.epoch; 31 k.cid = sp->hdr.cid & RXRPC_CIDMASK; 37 k.cid = sp->hdr.cid & RXRPC_CIDMASK; 32 38 33 do { 39 do { 34 /* Unfortunately, rbtree walki 40 /* Unfortunately, rbtree walking doesn't give reliable results 35 * under just the RCU read loc 41 * under just the RCU read lock, so we have to check for 36 * changes. 42 * changes. 37 */ 43 */ 38 seq++; /* 2 on the 1st/lockles << 39 read_seqbegin_or_lock(&peer->s 44 read_seqbegin_or_lock(&peer->service_conn_lock, &seq); 40 45 41 p = rcu_dereference_raw(peer-> 46 p = rcu_dereference_raw(peer->service_conns.rb_node); 42 while (p) { 47 while (p) { 43 conn = rb_entry(p, str 48 conn = rb_entry(p, struct rxrpc_connection, service_node); 44 49 45 if (conn->proto.index_ 50 if (conn->proto.index_key < k.index_key) 46 p = rcu_derefe 51 p = rcu_dereference_raw(p->rb_left); 47 else if (conn->proto.i 52 else if (conn->proto.index_key > k.index_key) 48 p = rcu_derefe 53 p = rcu_dereference_raw(p->rb_right); 49 else 54 else 50 break; 55 break; 51 conn = NULL; 56 conn = NULL; 52 } 57 } 53 } while (need_seqretry(&peer->service_ 58 } while (need_seqretry(&peer->service_conn_lock, seq)); 54 59 55 done_seqretry(&peer->service_conn_lock 60 done_seqretry(&peer->service_conn_lock, seq); 56 _leave(" = %d", conn ? conn->debug_id 61 _leave(" = %d", conn ? conn->debug_id : -1); 57 return conn; 62 return conn; 58 } 63 } 59 64 60 /* 65 /* 61 * Insert a service connection into a peer's t 66 * Insert a service connection into a peer's tree, thereby making it a target 62 * for incoming packets. 67 * for incoming packets. 63 */ 68 */ 64 static void rxrpc_publish_service_conn(struct 69 static void rxrpc_publish_service_conn(struct rxrpc_peer *peer, 65 struct 70 struct rxrpc_connection *conn) 66 { 71 { 67 struct rxrpc_connection *cursor = NULL 72 struct rxrpc_connection *cursor = NULL; 68 struct rxrpc_conn_proto k = conn->prot 73 struct rxrpc_conn_proto k = conn->proto; 69 struct rb_node **pp, *parent; 74 struct rb_node **pp, *parent; 70 75 71 write_seqlock(&peer->service_conn_lock !! 76 write_seqlock_bh(&peer->service_conn_lock); 72 77 73 pp = &peer->service_conns.rb_node; 78 pp = &peer->service_conns.rb_node; 74 parent = NULL; 79 parent = NULL; 75 while (*pp) { 80 while (*pp) { 76 parent = *pp; 81 parent = *pp; 77 cursor = rb_entry(parent, 82 cursor = rb_entry(parent, 78 struct rxrpc 83 struct rxrpc_connection, service_node); 79 84 80 if (cursor->proto.index_key < 85 if (cursor->proto.index_key < k.index_key) 81 pp = &(*pp)->rb_left; 86 pp = &(*pp)->rb_left; 82 else if (cursor->proto.index_k 87 else if (cursor->proto.index_key > k.index_key) 83 pp = &(*pp)->rb_right; 88 pp = &(*pp)->rb_right; 84 else 89 else 85 goto found_extant_conn 90 goto found_extant_conn; 86 } 91 } 87 92 88 rb_link_node_rcu(&conn->service_node, 93 rb_link_node_rcu(&conn->service_node, parent, pp); 89 rb_insert_color(&conn->service_node, & 94 rb_insert_color(&conn->service_node, &peer->service_conns); 90 conn_published: 95 conn_published: 91 set_bit(RXRPC_CONN_IN_SERVICE_CONNS, & 96 set_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags); 92 write_sequnlock(&peer->service_conn_lo !! 97 write_sequnlock_bh(&peer->service_conn_lock); 93 _leave(" = %d [new]", conn->debug_id); 98 _leave(" = %d [new]", conn->debug_id); 94 return; 99 return; 95 100 96 found_extant_conn: 101 found_extant_conn: 97 if (refcount_read(&cursor->ref) == 0) !! 102 if (atomic_read(&cursor->usage) == 0) 98 goto replace_old_connection; 103 goto replace_old_connection; 99 write_sequnlock(&peer->service_conn_lo !! 104 write_sequnlock_bh(&peer->service_conn_lock); 100 /* We should not be able to get here. 105 /* We should not be able to get here. rxrpc_incoming_connection() is 101 * called in a non-reentrant context, 106 * called in a non-reentrant context, so there can't be a race to 102 * insert a new connection. 107 * insert a new connection. 103 */ 108 */ 104 BUG(); 109 BUG(); 105 110 106 replace_old_connection: 111 replace_old_connection: 107 /* The old connection is from an outda 112 /* The old connection is from an outdated epoch. */ 108 _debug("replace conn"); 113 _debug("replace conn"); 109 rb_replace_node_rcu(&cursor->service_n 114 rb_replace_node_rcu(&cursor->service_node, 110 &conn->service_nod 115 &conn->service_node, 111 &peer->service_con 116 &peer->service_conns); 112 clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, 117 clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &cursor->flags); 113 goto conn_published; 118 goto conn_published; 114 } 119 } 115 120 116 /* 121 /* 117 * Preallocate a service connection. The conn 122 * Preallocate a service connection. The connection is placed on the proc and 118 * reap lists so that we don't have to get the 123 * reap lists so that we don't have to get the lock from BH context. 119 */ 124 */ 120 struct rxrpc_connection *rxrpc_prealloc_servic 125 struct rxrpc_connection *rxrpc_prealloc_service_connection(struct rxrpc_net *rxnet, 121 126 gfp_t gfp) 122 { 127 { 123 struct rxrpc_connection *conn = rxrpc_ !! 128 struct rxrpc_connection *conn = rxrpc_alloc_connection(gfp); 124 129 125 if (conn) { 130 if (conn) { 126 /* We maintain an extra ref on 131 /* We maintain an extra ref on the connection whilst it is on 127 * the rxrpc_connections list. 132 * the rxrpc_connections list. 128 */ 133 */ 129 conn->state = RXRPC_CONN_SERVI 134 conn->state = RXRPC_CONN_SERVICE_PREALLOC; 130 refcount_set(&conn->ref, 2); !! 135 atomic_set(&conn->usage, 2); >> 136 conn->bundle = rxrpc_get_bundle(&rxrpc_service_dummy_bundle); 131 137 132 atomic_inc(&rxnet->nr_conns); 138 atomic_inc(&rxnet->nr_conns); 133 write_lock(&rxnet->conn_lock); 139 write_lock(&rxnet->conn_lock); 134 list_add_tail(&conn->link, &rx 140 list_add_tail(&conn->link, &rxnet->service_conns); 135 list_add_tail(&conn->proc_link 141 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list); 136 write_unlock(&rxnet->conn_lock 142 write_unlock(&rxnet->conn_lock); 137 143 138 rxrpc_see_connection(conn, rxr !! 144 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_service, >> 145 atomic_read(&conn->usage), >> 146 __builtin_return_address(0)); 139 } 147 } 140 148 141 return conn; 149 return conn; 142 } 150 } 143 151 144 /* 152 /* 145 * Set up an incoming connection. This is cal 153 * Set up an incoming connection. This is called in BH context with the RCU 146 * read lock held. 154 * read lock held. 147 */ 155 */ 148 void rxrpc_new_incoming_connection(struct rxrp 156 void rxrpc_new_incoming_connection(struct rxrpc_sock *rx, 149 struct rxrp 157 struct rxrpc_connection *conn, 150 const struc 158 const struct rxrpc_security *sec, 151 struct sk_b 159 struct sk_buff *skb) 152 { 160 { 153 struct rxrpc_skb_priv *sp = rxrpc_skb( 161 struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 154 162 155 _enter(""); 163 _enter(""); 156 164 157 conn->proto.epoch = sp->hdr.epoc 165 conn->proto.epoch = sp->hdr.epoch; 158 conn->proto.cid = sp->hdr.cid 166 conn->proto.cid = sp->hdr.cid & RXRPC_CIDMASK; 159 conn->orig_service_id = sp->hdr.serv !! 167 conn->params.service_id = sp->hdr.serviceId; 160 conn->service_id = sp->hdr.serv 168 conn->service_id = sp->hdr.serviceId; 161 conn->security_ix = sp->hdr.secu 169 conn->security_ix = sp->hdr.securityIndex; 162 conn->out_clientflag = 0; 170 conn->out_clientflag = 0; 163 conn->security = sec; 171 conn->security = sec; 164 if (conn->security_ix) 172 if (conn->security_ix) 165 conn->state = RXRPC_CONN_S 173 conn->state = RXRPC_CONN_SERVICE_UNSECURED; 166 else 174 else 167 conn->state = RXRPC_CONN_S 175 conn->state = RXRPC_CONN_SERVICE; 168 176 169 /* See if we should upgrade the servic 177 /* See if we should upgrade the service. This can only happen on the 170 * first packet on a new connection. 178 * first packet on a new connection. Once done, it applies to all 171 * subsequent calls on that connection 179 * subsequent calls on that connection. 172 */ 180 */ 173 if (sp->hdr.userStatus == RXRPC_USERST 181 if (sp->hdr.userStatus == RXRPC_USERSTATUS_SERVICE_UPGRADE && 174 conn->service_id == rx->service_up 182 conn->service_id == rx->service_upgrade.from) 175 conn->service_id = rx->service 183 conn->service_id = rx->service_upgrade.to; 176 184 177 atomic_set(&conn->active, 1); << 178 << 179 /* Make the connection a target for in 185 /* Make the connection a target for incoming packets. */ 180 rxrpc_publish_service_conn(conn->peer, !! 186 rxrpc_publish_service_conn(conn->params.peer, conn); >> 187 >> 188 _net("CONNECTION new %d {%x}", conn->debug_id, conn->proto.cid); 181 } 189 } 182 190 183 /* 191 /* 184 * Remove the service connection from the peer 192 * Remove the service connection from the peer's tree, thereby removing it as a 185 * target for incoming packets. 193 * target for incoming packets. 186 */ 194 */ 187 void rxrpc_unpublish_service_conn(struct rxrpc 195 void rxrpc_unpublish_service_conn(struct rxrpc_connection *conn) 188 { 196 { 189 struct rxrpc_peer *peer = conn->peer; !! 197 struct rxrpc_peer *peer = conn->params.peer; 190 198 191 write_seqlock(&peer->service_conn_lock !! 199 write_seqlock_bh(&peer->service_conn_lock); 192 if (test_and_clear_bit(RXRPC_CONN_IN_S 200 if (test_and_clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags)) 193 rb_erase(&conn->service_node, 201 rb_erase(&conn->service_node, &peer->service_conns); 194 write_sequnlock(&peer->service_conn_lo !! 202 write_sequnlock_bh(&peer->service_conn_lock); 195 } 203 } 196 204
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.