blob: 77a509e6003a67c6c0c6c8f31786fc1df673bd3c [file] [log] [blame]
David Howells7877a4a2016-04-04 14:00:40 +01001/* 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/*
16 * get a record of an incoming connection
17 */
18struct rxrpc_connection *rxrpc_incoming_connection(struct rxrpc_local *local,
David Howellsd991b4a2016-06-29 14:40:39 +010019 struct sockaddr_rxrpc *srx,
David Howells7877a4a2016-04-04 14:00:40 +010020 struct sk_buff *skb)
21{
22 struct rxrpc_connection *conn, *candidate = NULL;
23 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howellsd991b4a2016-06-29 14:40:39 +010024 struct rxrpc_peer *peer;
David Howells7877a4a2016-04-04 14:00:40 +010025 struct rb_node *p, **pp;
26 const char *new = "old";
27 u32 epoch, cid;
28
29 _enter("");
30
David Howellsd991b4a2016-06-29 14:40:39 +010031 peer = rxrpc_lookup_peer(local, srx, GFP_NOIO);
32 if (!peer) {
33 _debug("no peer");
34 return ERR_PTR(-EBUSY);
35 }
36
David Howells7877a4a2016-04-04 14:00:40 +010037 ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED);
38
39 epoch = sp->hdr.epoch;
40 cid = sp->hdr.cid & RXRPC_CIDMASK;
41
42 /* search the connection list first */
43 read_lock_bh(&peer->conn_lock);
44
45 p = peer->service_conns.rb_node;
46 while (p) {
47 conn = rb_entry(p, struct rxrpc_connection, service_node);
48
49 _debug("maybe %x", conn->proto.cid);
50
51 if (epoch < conn->proto.epoch)
52 p = p->rb_left;
53 else if (epoch > conn->proto.epoch)
54 p = p->rb_right;
55 else if (cid < conn->proto.cid)
56 p = p->rb_left;
57 else if (cid > conn->proto.cid)
58 p = p->rb_right;
59 else
60 goto found_extant_connection;
61 }
62 read_unlock_bh(&peer->conn_lock);
63
64 /* not yet present - create a candidate for a new record and then
65 * redo the search */
66 candidate = rxrpc_alloc_connection(GFP_NOIO);
67 if (!candidate) {
David Howellsd991b4a2016-06-29 14:40:39 +010068 rxrpc_put_peer(peer);
David Howells7877a4a2016-04-04 14:00:40 +010069 _leave(" = -ENOMEM");
70 return ERR_PTR(-ENOMEM);
71 }
72
73 candidate->proto.local = local;
74 candidate->proto.epoch = sp->hdr.epoch;
75 candidate->proto.cid = sp->hdr.cid & RXRPC_CIDMASK;
76 candidate->proto.in_clientflag = RXRPC_CLIENT_INITIATED;
77 candidate->params.local = local;
78 candidate->params.peer = peer;
79 candidate->params.service_id = sp->hdr.serviceId;
80 candidate->security_ix = sp->hdr.securityIndex;
81 candidate->out_clientflag = 0;
82 candidate->state = RXRPC_CONN_SERVICE;
83 if (candidate->params.service_id)
84 candidate->state = RXRPC_CONN_SERVICE_UNSECURED;
85
86 write_lock_bh(&peer->conn_lock);
87
88 pp = &peer->service_conns.rb_node;
89 p = NULL;
90 while (*pp) {
91 p = *pp;
92 conn = rb_entry(p, struct rxrpc_connection, service_node);
93
94 if (epoch < conn->proto.epoch)
95 pp = &(*pp)->rb_left;
96 else if (epoch > conn->proto.epoch)
97 pp = &(*pp)->rb_right;
98 else if (cid < conn->proto.cid)
99 pp = &(*pp)->rb_left;
100 else if (cid > conn->proto.cid)
101 pp = &(*pp)->rb_right;
102 else
103 goto found_extant_second;
104 }
105
106 /* we can now add the new candidate to the list */
David Howells001c1122016-06-30 10:45:22 +0100107 set_bit(RXRPC_CONN_IN_SERVICE_CONNS, &candidate->flags);
108 rb_link_node(&candidate->service_node, p, pp);
109 rb_insert_color(&candidate->service_node, &peer->service_conns);
110attached:
David Howells7877a4a2016-04-04 14:00:40 +0100111 conn = candidate;
112 candidate = NULL;
David Howells7877a4a2016-04-04 14:00:40 +0100113 rxrpc_get_peer(peer);
114 rxrpc_get_local(local);
115
116 write_unlock_bh(&peer->conn_lock);
117
118 write_lock(&rxrpc_connection_lock);
119 list_add_tail(&conn->link, &rxrpc_connections);
120 write_unlock(&rxrpc_connection_lock);
121
122 new = "new";
123
124success:
125 _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid);
126
David Howellsd991b4a2016-06-29 14:40:39 +0100127 rxrpc_put_peer(peer);
David Howells7877a4a2016-04-04 14:00:40 +0100128 _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
129 return conn;
130
131 /* we found the connection in the list immediately */
132found_extant_connection:
David Howells001c1122016-06-30 10:45:22 +0100133 if (!rxrpc_get_connection_maybe(conn)) {
134 set_bit(RXRPC_CONN_IN_SERVICE_CONNS, &candidate->flags);
135 rb_replace_node(&conn->service_node,
136 &candidate->service_node,
137 &peer->service_conns);
138 clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags);
139 goto attached;
140 }
141
David Howells7877a4a2016-04-04 14:00:40 +0100142 if (sp->hdr.securityIndex != conn->security_ix) {
143 read_unlock_bh(&peer->conn_lock);
David Howells001c1122016-06-30 10:45:22 +0100144 goto security_mismatch_put;
David Howells7877a4a2016-04-04 14:00:40 +0100145 }
David Howells7877a4a2016-04-04 14:00:40 +0100146 read_unlock_bh(&peer->conn_lock);
147 goto success;
148
149 /* we found the connection on the second time through the list */
150found_extant_second:
151 if (sp->hdr.securityIndex != conn->security_ix) {
152 write_unlock_bh(&peer->conn_lock);
153 goto security_mismatch;
154 }
155 rxrpc_get_connection(conn);
156 write_unlock_bh(&peer->conn_lock);
157 kfree(candidate);
158 goto success;
159
David Howells001c1122016-06-30 10:45:22 +0100160security_mismatch_put:
161 rxrpc_put_connection(conn);
David Howells7877a4a2016-04-04 14:00:40 +0100162security_mismatch:
163 kfree(candidate);
164 _leave(" = -EKEYREJECTED");
165 return ERR_PTR(-EKEYREJECTED);
166}
David Howells001c1122016-06-30 10:45:22 +0100167
168/*
169 * Remove the service connection from the peer's tree, thereby removing it as a
170 * target for incoming packets.
171 */
172void rxrpc_unpublish_service_conn(struct rxrpc_connection *conn)
173{
174 struct rxrpc_peer *peer = conn->params.peer;
175
176 write_lock_bh(&peer->conn_lock);
177 if (test_and_clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags))
178 rb_erase(&conn->service_node, &peer->service_conns);
179 write_unlock_bh(&peer->conn_lock);
180}