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