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