1 /* Service connection management
3 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/slab.h>
13 #include "ar-internal.h"
16 * get a record of an incoming connection
18 struct rxrpc_connection *rxrpc_incoming_connection(struct rxrpc_local *local,
19 struct sockaddr_rxrpc *srx,
22 struct rxrpc_connection *conn, *candidate = NULL;
23 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
24 struct rxrpc_peer *peer;
25 struct rb_node *p, **pp;
26 const char *new = "old";
31 peer = rxrpc_lookup_peer(local, srx, GFP_NOIO);
34 return ERR_PTR(-EBUSY);
37 ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED);
39 epoch = sp->hdr.epoch;
40 cid = sp->hdr.cid & RXRPC_CIDMASK;
42 /* search the connection list first */
43 read_lock_bh(&peer->conn_lock);
45 p = peer->service_conns.rb_node;
47 conn = rb_entry(p, struct rxrpc_connection, service_node);
49 _debug("maybe %x", conn->proto.cid);
51 if (epoch < conn->proto.epoch)
53 else if (epoch > conn->proto.epoch)
55 else if (cid < conn->proto.cid)
57 else if (cid > conn->proto.cid)
60 goto found_extant_connection;
62 read_unlock_bh(&peer->conn_lock);
64 /* not yet present - create a candidate for a new record and then
66 candidate = rxrpc_alloc_connection(GFP_NOIO);
70 return ERR_PTR(-ENOMEM);
73 candidate->proto.epoch = sp->hdr.epoch;
74 candidate->proto.cid = sp->hdr.cid & RXRPC_CIDMASK;
75 candidate->params.local = local;
76 candidate->params.peer = peer;
77 candidate->params.service_id = sp->hdr.serviceId;
78 candidate->security_ix = sp->hdr.securityIndex;
79 candidate->out_clientflag = 0;
80 candidate->state = RXRPC_CONN_SERVICE;
81 if (candidate->params.service_id)
82 candidate->state = RXRPC_CONN_SERVICE_UNSECURED;
84 write_lock_bh(&peer->conn_lock);
86 pp = &peer->service_conns.rb_node;
90 conn = rb_entry(p, struct rxrpc_connection, service_node);
92 if (epoch < conn->proto.epoch)
94 else if (epoch > conn->proto.epoch)
95 pp = &(*pp)->rb_right;
96 else if (cid < conn->proto.cid)
98 else if (cid > conn->proto.cid)
99 pp = &(*pp)->rb_right;
101 goto found_extant_second;
104 /* we can now add the new candidate to the list */
105 set_bit(RXRPC_CONN_IN_SERVICE_CONNS, &candidate->flags);
106 rb_link_node(&candidate->service_node, p, pp);
107 rb_insert_color(&candidate->service_node, &peer->service_conns);
111 rxrpc_get_peer(peer);
112 rxrpc_get_local(local);
114 write_unlock_bh(&peer->conn_lock);
116 write_lock(&rxrpc_connection_lock);
117 list_add_tail(&conn->link, &rxrpc_connections);
118 write_unlock(&rxrpc_connection_lock);
123 _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid);
125 rxrpc_put_peer(peer);
126 _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
129 /* we found the connection in the list immediately */
130 found_extant_connection:
131 if (!rxrpc_get_connection_maybe(conn)) {
132 set_bit(RXRPC_CONN_IN_SERVICE_CONNS, &candidate->flags);
133 rb_replace_node(&conn->service_node,
134 &candidate->service_node,
135 &peer->service_conns);
136 clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags);
140 if (sp->hdr.securityIndex != conn->security_ix) {
141 read_unlock_bh(&peer->conn_lock);
142 goto security_mismatch_put;
144 read_unlock_bh(&peer->conn_lock);
147 /* we found the connection on the second time through the list */
149 if (sp->hdr.securityIndex != conn->security_ix) {
150 write_unlock_bh(&peer->conn_lock);
151 goto security_mismatch;
153 rxrpc_get_connection(conn);
154 write_unlock_bh(&peer->conn_lock);
158 security_mismatch_put:
159 rxrpc_put_connection(conn);
162 _leave(" = -EKEYREJECTED");
163 return ERR_PTR(-EKEYREJECTED);
167 * Remove the service connection from the peer's tree, thereby removing it as a
168 * target for incoming packets.
170 void rxrpc_unpublish_service_conn(struct rxrpc_connection *conn)
172 struct rxrpc_peer *peer = conn->params.peer;
174 write_lock_bh(&peer->conn_lock);
175 if (test_and_clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags))
176 rb_erase(&conn->service_node, &peer->service_conns);
177 write_unlock_bh(&peer->conn_lock);