blob: 538e9831c69907b8d5a1756a77dc83ebfa2eff18 [file] [log] [blame]
David Howellsbe6e6702016-04-04 14:00:32 +01001/* RxRPC remote transport endpoint record management
David Howells17926a72007-04-26 15:48:28 -07002 *
David Howellsbe6e6702016-04-04 14:00:32 +01003 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
David Howells17926a72007-04-26 15:48:28 -07004 * 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 License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/module.h>
15#include <linux/net.h>
16#include <linux/skbuff.h>
17#include <linux/udp.h>
18#include <linux/in.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
David Howellsbe6e6702016-04-04 14:00:32 +010020#include <linux/hashtable.h>
David Howells17926a72007-04-26 15:48:28 -070021#include <net/sock.h>
22#include <net/af_rxrpc.h>
23#include <net/ip.h>
David Howells224711d2007-05-04 12:41:11 -070024#include <net/route.h>
David Howells17926a72007-04-26 15:48:28 -070025#include "ar-internal.h"
26
David Howellsbe6e6702016-04-04 14:00:32 +010027static DEFINE_HASHTABLE(rxrpc_peer_hash, 10);
28static DEFINE_SPINLOCK(rxrpc_peer_hash_lock);
David Howells17926a72007-04-26 15:48:28 -070029
David Howellsbe6e6702016-04-04 14:00:32 +010030/*
31 * Hash a peer key.
32 */
33static unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local,
34 const struct sockaddr_rxrpc *srx)
35{
36 const u16 *p;
37 unsigned int i, size;
38 unsigned long hash_key;
39
40 _enter("");
41
42 hash_key = (unsigned long)local / __alignof__(*local);
43 hash_key += srx->transport_type;
44 hash_key += srx->transport_len;
45 hash_key += srx->transport.family;
46
47 switch (srx->transport.family) {
48 case AF_INET:
49 hash_key += (u16 __force)srx->transport.sin.sin_port;
50 size = sizeof(srx->transport.sin.sin_addr);
51 p = (u16 *)&srx->transport.sin.sin_addr;
52 break;
Arnd Bergmann2f9f9f52016-06-17 11:55:22 +020053 default:
54 WARN(1, "AF_RXRPC: Unsupported transport address family\n");
55 return 0;
David Howellsbe6e6702016-04-04 14:00:32 +010056 }
57
58 /* Step through the peer address in 16-bit portions for speed */
59 for (i = 0; i < size; i += sizeof(*p), p++)
60 hash_key += *p;
61
62 _leave(" 0x%lx", hash_key);
63 return hash_key;
64}
65
66/*
67 * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same
68 * or greater than.
69 *
70 * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
71 * buckets and mid-bucket insertion, so we don't make full use of this
72 * information at this point.
73 */
74static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
75 struct rxrpc_local *local,
76 const struct sockaddr_rxrpc *srx,
77 unsigned long hash_key)
78{
79 long diff;
80
81 diff = ((peer->hash_key - hash_key) ?:
82 ((unsigned long)peer->local - (unsigned long)local) ?:
83 (peer->srx.transport_type - srx->transport_type) ?:
84 (peer->srx.transport_len - srx->transport_len) ?:
85 (peer->srx.transport.family - srx->transport.family));
86 if (diff != 0)
87 return diff;
88
89 switch (srx->transport.family) {
90 case AF_INET:
91 return ((u16 __force)peer->srx.transport.sin.sin_port -
92 (u16 __force)srx->transport.sin.sin_port) ?:
93 memcmp(&peer->srx.transport.sin.sin_addr,
94 &srx->transport.sin.sin_addr,
95 sizeof(struct in_addr));
96 default:
97 BUG();
98 }
99}
100
101/*
102 * Look up a remote transport endpoint for the specified address using RCU.
103 */
104static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
105 struct rxrpc_local *local,
106 const struct sockaddr_rxrpc *srx,
107 unsigned long hash_key)
108{
109 struct rxrpc_peer *peer;
110
111 hash_for_each_possible_rcu(rxrpc_peer_hash, peer, hash_link, hash_key) {
112 if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0) {
113 if (atomic_read(&peer->usage) == 0)
114 return NULL;
115 return peer;
116 }
117 }
118
119 return NULL;
120}
121
122/*
123 * Look up a remote transport endpoint for the specified address using RCU.
124 */
125struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
126 const struct sockaddr_rxrpc *srx)
127{
128 struct rxrpc_peer *peer;
129 unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
130
131 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
132 if (peer) {
133 switch (srx->transport.family) {
134 case AF_INET:
135 _net("PEER %d {%d,%u,%pI4+%hu}",
136 peer->debug_id,
137 peer->srx.transport_type,
138 peer->srx.transport.family,
139 &peer->srx.transport.sin.sin_addr,
140 ntohs(peer->srx.transport.sin.sin_port));
141 break;
142 }
143
144 _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
145 }
146 return peer;
147}
David Howells17926a72007-04-26 15:48:28 -0700148
149/*
David Howells224711d2007-05-04 12:41:11 -0700150 * assess the MTU size for the network interface through which this peer is
151 * reached
152 */
153static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
154{
155 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -0700156 struct flowi4 fl4;
David Howells224711d2007-05-04 12:41:11 -0700157
158 peer->if_mtu = 1500;
159
David S. Miller31e45432011-05-03 20:25:42 -0700160 rt = ip_route_output_ports(&init_net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500161 peer->srx.transport.sin.sin_addr.s_addr, 0,
162 htons(7000), htons(7001),
163 IPPROTO_UDP, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800164 if (IS_ERR(rt)) {
165 _leave(" [route err %ld]", PTR_ERR(rt));
David Howells224711d2007-05-04 12:41:11 -0700166 return;
167 }
168
Changli Gaod8d1f302010-06-10 23:31:35 -0700169 peer->if_mtu = dst_mtu(&rt->dst);
170 dst_release(&rt->dst);
David Howells224711d2007-05-04 12:41:11 -0700171
David Howellsa6a62b62007-05-10 03:15:25 -0700172 _leave(" [if_mtu %u]", peer->if_mtu);
David Howells224711d2007-05-04 12:41:11 -0700173}
174
175/*
David Howellsbe6e6702016-04-04 14:00:32 +0100176 * Allocate a peer.
David Howells17926a72007-04-26 15:48:28 -0700177 */
David Howellsbe6e6702016-04-04 14:00:32 +0100178struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp)
David Howells17926a72007-04-26 15:48:28 -0700179{
180 struct rxrpc_peer *peer;
181
182 _enter("");
183
184 peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
185 if (peer) {
David Howellsbe6e6702016-04-04 14:00:32 +0100186 atomic_set(&peer->usage, 1);
187 peer->local = local;
David Howellsf66d7492016-04-04 14:00:34 +0100188 INIT_HLIST_HEAD(&peer->error_targets);
189 INIT_WORK(&peer->error_distributor,
190 &rxrpc_peer_error_distributor);
David Howellsaa390bb2016-06-17 10:06:56 +0100191 peer->service_conns = RB_ROOT;
David Howells8496af52016-07-01 07:51:50 +0100192 seqlock_init(&peer->service_conn_lock);
David Howells17926a72007-04-26 15:48:28 -0700193 spin_lock_init(&peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700194 peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howellsbe6e6702016-04-04 14:00:32 +0100195 }
196
197 _leave(" = %p", peer);
198 return peer;
199}
200
201/*
202 * Set up a new peer.
203 */
204static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local,
205 struct sockaddr_rxrpc *srx,
206 unsigned long hash_key,
207 gfp_t gfp)
208{
209 struct rxrpc_peer *peer;
210
211 _enter("");
212
213 peer = rxrpc_alloc_peer(local, gfp);
214 if (peer) {
215 peer->hash_key = hash_key;
David Howells17926a72007-04-26 15:48:28 -0700216 memcpy(&peer->srx, srx, sizeof(*srx));
217
David Howells224711d2007-05-04 12:41:11 -0700218 rxrpc_assess_MTU_size(peer);
219 peer->mtu = peer->if_mtu;
David Howells17926a72007-04-26 15:48:28 -0700220
221 if (srx->transport.family == AF_INET) {
222 peer->hdrsize = sizeof(struct iphdr);
223 switch (srx->transport_type) {
224 case SOCK_DGRAM:
225 peer->hdrsize += sizeof(struct udphdr);
226 break;
227 default:
228 BUG();
229 break;
230 }
231 } else {
232 BUG();
233 }
234
David Howells0d12f8a2016-03-04 15:53:46 +0000235 peer->hdrsize += sizeof(struct rxrpc_wire_header);
David Howells17926a72007-04-26 15:48:28 -0700236 peer->maxdata = peer->mtu - peer->hdrsize;
237 }
238
239 _leave(" = %p", peer);
240 return peer;
241}
242
243/*
244 * obtain a remote transport endpoint for the specified address
245 */
David Howellsbe6e6702016-04-04 14:00:32 +0100246struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
247 struct sockaddr_rxrpc *srx, gfp_t gfp)
David Howells17926a72007-04-26 15:48:28 -0700248{
249 struct rxrpc_peer *peer, *candidate;
David Howellsbe6e6702016-04-04 14:00:32 +0100250 unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
David Howells17926a72007-04-26 15:48:28 -0700251
Harvey Harrison21454aa2008-10-31 00:54:56 -0700252 _enter("{%d,%d,%pI4+%hu}",
David Howells17926a72007-04-26 15:48:28 -0700253 srx->transport_type,
254 srx->transport_len,
Harvey Harrison21454aa2008-10-31 00:54:56 -0700255 &srx->transport.sin.sin_addr,
David Howells17926a72007-04-26 15:48:28 -0700256 ntohs(srx->transport.sin.sin_port));
257
258 /* search the peer list first */
David Howellsbe6e6702016-04-04 14:00:32 +0100259 rcu_read_lock();
260 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
261 if (peer && !rxrpc_get_peer_maybe(peer))
262 peer = NULL;
263 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700264
David Howellsbe6e6702016-04-04 14:00:32 +0100265 if (!peer) {
266 /* The peer is not yet present in hash - create a candidate
267 * for a new record and then redo the search.
268 */
269 candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
270 if (!candidate) {
271 _leave(" = NULL [nomem]");
272 return NULL;
273 }
David Howells17926a72007-04-26 15:48:28 -0700274
David Howellsbe6e6702016-04-04 14:00:32 +0100275 spin_lock(&rxrpc_peer_hash_lock);
276
277 /* Need to check that we aren't racing with someone else */
278 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
279 if (peer && !rxrpc_get_peer_maybe(peer))
280 peer = NULL;
281 if (!peer)
282 hash_add_rcu(rxrpc_peer_hash,
283 &candidate->hash_link, hash_key);
284
285 spin_unlock(&rxrpc_peer_hash_lock);
286
287 if (peer)
288 kfree(candidate);
289 else
290 peer = candidate;
David Howells17926a72007-04-26 15:48:28 -0700291 }
292
David Howellsbe6e6702016-04-04 14:00:32 +0100293 _net("PEER %d {%d,%pI4+%hu}",
David Howells17926a72007-04-26 15:48:28 -0700294 peer->debug_id,
295 peer->srx.transport_type,
Harvey Harrison21454aa2008-10-31 00:54:56 -0700296 &peer->srx.transport.sin.sin_addr,
David Howells17926a72007-04-26 15:48:28 -0700297 ntohs(peer->srx.transport.sin.sin_port));
298
David Howellsbe6e6702016-04-04 14:00:32 +0100299 _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
David Howells17926a72007-04-26 15:48:28 -0700300 return peer;
David Howells17926a72007-04-26 15:48:28 -0700301}
302
303/*
David Howellsbe6e6702016-04-04 14:00:32 +0100304 * Discard a ref on a remote peer record.
David Howells17926a72007-04-26 15:48:28 -0700305 */
David Howellsbe6e6702016-04-04 14:00:32 +0100306void __rxrpc_put_peer(struct rxrpc_peer *peer)
David Howells17926a72007-04-26 15:48:28 -0700307{
David Howellsf66d7492016-04-04 14:00:34 +0100308 ASSERT(hlist_empty(&peer->error_targets));
David Howells17926a72007-04-26 15:48:28 -0700309
David Howellsbe6e6702016-04-04 14:00:32 +0100310 spin_lock(&rxrpc_peer_hash_lock);
311 hash_del_rcu(&peer->hash_link);
312 spin_unlock(&rxrpc_peer_hash_lock);
David Howells17926a72007-04-26 15:48:28 -0700313
David Howellsbe6e6702016-04-04 14:00:32 +0100314 kfree_rcu(peer, rcu);
David Howells17926a72007-04-26 15:48:28 -0700315}