]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/rxrpc/peer_object.c
Merge branch 'QCA8K'
[mirror_ubuntu-zesty-kernel.git] / net / rxrpc / peer_object.c
CommitLineData
be6e6707 1/* RxRPC remote transport endpoint record management
17926a79 2 *
be6e6707 3 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
17926a79
DH
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 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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
14#include <linux/module.h>
15#include <linux/net.h>
16#include <linux/skbuff.h>
17#include <linux/udp.h>
18#include <linux/in.h>
75b54cb5 19#include <linux/in6.h>
5a0e3ad6 20#include <linux/slab.h>
be6e6707 21#include <linux/hashtable.h>
17926a79
DH
22#include <net/sock.h>
23#include <net/af_rxrpc.h>
24#include <net/ip.h>
224711df 25#include <net/route.h>
75b54cb5 26#include <net/ip6_route.h>
17926a79
DH
27#include "ar-internal.h"
28
be6e6707
DH
29static DEFINE_HASHTABLE(rxrpc_peer_hash, 10);
30static DEFINE_SPINLOCK(rxrpc_peer_hash_lock);
17926a79 31
be6e6707
DH
32/*
33 * Hash a peer key.
34 */
35static unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local,
36 const struct sockaddr_rxrpc *srx)
37{
38 const u16 *p;
39 unsigned int i, size;
40 unsigned long hash_key;
41
42 _enter("");
43
44 hash_key = (unsigned long)local / __alignof__(*local);
45 hash_key += srx->transport_type;
46 hash_key += srx->transport_len;
47 hash_key += srx->transport.family;
48
49 switch (srx->transport.family) {
50 case AF_INET:
51 hash_key += (u16 __force)srx->transport.sin.sin_port;
52 size = sizeof(srx->transport.sin.sin_addr);
53 p = (u16 *)&srx->transport.sin.sin_addr;
54 break;
75b54cb5
DH
55 case AF_INET6:
56 hash_key += (u16 __force)srx->transport.sin.sin_port;
57 size = sizeof(srx->transport.sin6.sin6_addr);
58 p = (u16 *)&srx->transport.sin6.sin6_addr;
59 break;
2f9f9f52
AB
60 default:
61 WARN(1, "AF_RXRPC: Unsupported transport address family\n");
62 return 0;
be6e6707
DH
63 }
64
65 /* Step through the peer address in 16-bit portions for speed */
66 for (i = 0; i < size; i += sizeof(*p), p++)
67 hash_key += *p;
68
69 _leave(" 0x%lx", hash_key);
70 return hash_key;
71}
72
73/*
74 * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same
75 * or greater than.
76 *
77 * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
78 * buckets and mid-bucket insertion, so we don't make full use of this
79 * information at this point.
80 */
81static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
82 struct rxrpc_local *local,
83 const struct sockaddr_rxrpc *srx,
84 unsigned long hash_key)
85{
86 long diff;
87
88 diff = ((peer->hash_key - hash_key) ?:
89 ((unsigned long)peer->local - (unsigned long)local) ?:
90 (peer->srx.transport_type - srx->transport_type) ?:
91 (peer->srx.transport_len - srx->transport_len) ?:
92 (peer->srx.transport.family - srx->transport.family));
93 if (diff != 0)
94 return diff;
95
96 switch (srx->transport.family) {
97 case AF_INET:
98 return ((u16 __force)peer->srx.transport.sin.sin_port -
99 (u16 __force)srx->transport.sin.sin_port) ?:
100 memcmp(&peer->srx.transport.sin.sin_addr,
101 &srx->transport.sin.sin_addr,
102 sizeof(struct in_addr));
75b54cb5
DH
103 case AF_INET6:
104 return ((u16 __force)peer->srx.transport.sin6.sin6_port -
105 (u16 __force)srx->transport.sin6.sin6_port) ?:
106 memcmp(&peer->srx.transport.sin6.sin6_addr,
107 &srx->transport.sin6.sin6_addr,
108 sizeof(struct in6_addr));
be6e6707
DH
109 default:
110 BUG();
111 }
112}
113
114/*
115 * Look up a remote transport endpoint for the specified address using RCU.
116 */
117static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
118 struct rxrpc_local *local,
119 const struct sockaddr_rxrpc *srx,
120 unsigned long hash_key)
121{
122 struct rxrpc_peer *peer;
123
124 hash_for_each_possible_rcu(rxrpc_peer_hash, peer, hash_link, hash_key) {
125 if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0) {
126 if (atomic_read(&peer->usage) == 0)
127 return NULL;
128 return peer;
129 }
130 }
131
132 return NULL;
133}
134
135/*
136 * Look up a remote transport endpoint for the specified address using RCU.
137 */
138struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
139 const struct sockaddr_rxrpc *srx)
140{
141 struct rxrpc_peer *peer;
142 unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
143
144 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
145 if (peer) {
75b54cb5 146 _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
be6e6707
DH
147 _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
148 }
149 return peer;
150}
17926a79 151
224711df
DH
152/*
153 * assess the MTU size for the network interface through which this peer is
154 * reached
155 */
156static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
157{
75b54cb5 158 struct dst_entry *dst;
224711df 159 struct rtable *rt;
75b54cb5
DH
160 struct flowi fl;
161 struct flowi4 *fl4 = &fl.u.ip4;
162 struct flowi6 *fl6 = &fl.u.ip6;
224711df
DH
163
164 peer->if_mtu = 1500;
165
75b54cb5
DH
166 memset(&fl, 0, sizeof(fl));
167 switch (peer->srx.transport.family) {
168 case AF_INET:
169 rt = ip_route_output_ports(
170 &init_net, fl4, NULL,
171 peer->srx.transport.sin.sin_addr.s_addr, 0,
172 htons(7000), htons(7001), IPPROTO_UDP, 0, 0);
173 if (IS_ERR(rt)) {
174 _leave(" [route err %ld]", PTR_ERR(rt));
175 return;
176 }
177 dst = &rt->dst;
178 break;
179
180 case AF_INET6:
181 fl6->flowi6_iif = LOOPBACK_IFINDEX;
182 fl6->flowi6_scope = RT_SCOPE_UNIVERSE;
183 fl6->flowi6_proto = IPPROTO_UDP;
184 memcpy(&fl6->daddr, &peer->srx.transport.sin6.sin6_addr,
185 sizeof(struct in6_addr));
186 fl6->fl6_dport = htons(7001);
187 fl6->fl6_sport = htons(7000);
188 dst = ip6_route_output(&init_net, NULL, fl6);
189 if (IS_ERR(dst)) {
190 _leave(" [route err %ld]", PTR_ERR(dst));
191 return;
192 }
193 break;
194
195 default:
196 BUG();
224711df
DH
197 }
198
75b54cb5
DH
199 peer->if_mtu = dst_mtu(dst);
200 dst_release(dst);
224711df 201
a6a62b69 202 _leave(" [if_mtu %u]", peer->if_mtu);
224711df
DH
203}
204
17926a79 205/*
be6e6707 206 * Allocate a peer.
17926a79 207 */
be6e6707 208struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp)
17926a79
DH
209{
210 struct rxrpc_peer *peer;
211
212 _enter("");
213
214 peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
215 if (peer) {
be6e6707
DH
216 atomic_set(&peer->usage, 1);
217 peer->local = local;
f66d7490
DH
218 INIT_HLIST_HEAD(&peer->error_targets);
219 INIT_WORK(&peer->error_distributor,
220 &rxrpc_peer_error_distributor);
aa390bbe 221 peer->service_conns = RB_ROOT;
8496af50 222 seqlock_init(&peer->service_conn_lock);
17926a79 223 spin_lock_init(&peer->lock);
17926a79 224 peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
be6e6707
DH
225 }
226
227 _leave(" = %p", peer);
228 return peer;
229}
230
248f219c
DH
231/*
232 * Initialise peer record.
233 */
234static void rxrpc_init_peer(struct rxrpc_peer *peer, unsigned long hash_key)
235{
08a39685 236 peer->hash_key = hash_key;
248f219c
DH
237 rxrpc_assess_MTU_size(peer);
238 peer->mtu = peer->if_mtu;
239
75b54cb5
DH
240 switch (peer->srx.transport.family) {
241 case AF_INET:
248f219c 242 peer->hdrsize = sizeof(struct iphdr);
75b54cb5
DH
243 break;
244 case AF_INET6:
245 peer->hdrsize = sizeof(struct ipv6hdr);
246 break;
247 default:
248 BUG();
249 }
250
251 switch (peer->srx.transport_type) {
252 case SOCK_DGRAM:
253 peer->hdrsize += sizeof(struct udphdr);
254 break;
255 default:
248f219c
DH
256 BUG();
257 }
258
259 peer->hdrsize += sizeof(struct rxrpc_wire_header);
260 peer->maxdata = peer->mtu - peer->hdrsize;
261}
262
be6e6707
DH
263/*
264 * Set up a new peer.
265 */
266static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local,
267 struct sockaddr_rxrpc *srx,
268 unsigned long hash_key,
269 gfp_t gfp)
270{
271 struct rxrpc_peer *peer;
272
273 _enter("");
274
275 peer = rxrpc_alloc_peer(local, gfp);
276 if (peer) {
17926a79 277 memcpy(&peer->srx, srx, sizeof(*srx));
248f219c
DH
278 rxrpc_init_peer(peer, hash_key);
279 }
17926a79 280
248f219c
DH
281 _leave(" = %p", peer);
282 return peer;
283}
17926a79 284
248f219c
DH
285/*
286 * Set up a new incoming peer. The address is prestored in the preallocated
287 * peer.
288 */
289struct rxrpc_peer *rxrpc_lookup_incoming_peer(struct rxrpc_local *local,
290 struct rxrpc_peer *prealloc)
291{
292 struct rxrpc_peer *peer;
293 unsigned long hash_key;
294
295 hash_key = rxrpc_peer_hash_key(local, &prealloc->srx);
296 prealloc->local = local;
297 rxrpc_init_peer(prealloc, hash_key);
298
299 spin_lock(&rxrpc_peer_hash_lock);
300
301 /* Need to check that we aren't racing with someone else */
302 peer = __rxrpc_lookup_peer_rcu(local, &prealloc->srx, hash_key);
303 if (peer && !rxrpc_get_peer_maybe(peer))
304 peer = NULL;
305 if (!peer) {
306 peer = prealloc;
307 hash_add_rcu(rxrpc_peer_hash, &peer->hash_link, hash_key);
17926a79
DH
308 }
309
248f219c 310 spin_unlock(&rxrpc_peer_hash_lock);
17926a79
DH
311 return peer;
312}
313
314/*
315 * obtain a remote transport endpoint for the specified address
316 */
be6e6707
DH
317struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
318 struct sockaddr_rxrpc *srx, gfp_t gfp)
17926a79
DH
319{
320 struct rxrpc_peer *peer, *candidate;
be6e6707 321 unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
17926a79 322
75b54cb5 323 _enter("{%pISp}", &srx->transport);
17926a79
DH
324
325 /* search the peer list first */
be6e6707
DH
326 rcu_read_lock();
327 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
328 if (peer && !rxrpc_get_peer_maybe(peer))
329 peer = NULL;
330 rcu_read_unlock();
331
332 if (!peer) {
333 /* The peer is not yet present in hash - create a candidate
334 * for a new record and then redo the search.
335 */
336 candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
337 if (!candidate) {
338 _leave(" = NULL [nomem]");
339 return NULL;
340 }
17926a79 341
248f219c 342 spin_lock_bh(&rxrpc_peer_hash_lock);
17926a79 343
be6e6707
DH
344 /* Need to check that we aren't racing with someone else */
345 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
346 if (peer && !rxrpc_get_peer_maybe(peer))
347 peer = NULL;
348 if (!peer)
349 hash_add_rcu(rxrpc_peer_hash,
350 &candidate->hash_link, hash_key);
17926a79 351
248f219c 352 spin_unlock_bh(&rxrpc_peer_hash_lock);
17926a79 353
be6e6707
DH
354 if (peer)
355 kfree(candidate);
356 else
357 peer = candidate;
358 }
17926a79 359
75b54cb5 360 _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
17926a79 361
be6e6707 362 _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
17926a79 363 return peer;
17926a79
DH
364}
365
366/*
be6e6707 367 * Discard a ref on a remote peer record.
17926a79 368 */
be6e6707 369void __rxrpc_put_peer(struct rxrpc_peer *peer)
17926a79 370{
f66d7490 371 ASSERT(hlist_empty(&peer->error_targets));
17926a79 372
248f219c 373 spin_lock_bh(&rxrpc_peer_hash_lock);
be6e6707 374 hash_del_rcu(&peer->hash_link);
248f219c 375 spin_unlock_bh(&rxrpc_peer_hash_lock);
17926a79 376
be6e6707 377 kfree_rcu(peer, rcu);
17926a79 378}
8324f0bc
DH
379
380/**
381 * rxrpc_kernel_get_peer - Get the peer address of a call
382 * @sock: The socket on which the call is in progress.
383 * @call: The call to query
384 * @_srx: Where to place the result
385 *
386 * Get the address of the remote peer in a call.
387 */
388void rxrpc_kernel_get_peer(struct socket *sock, struct rxrpc_call *call,
389 struct sockaddr_rxrpc *_srx)
390{
391 *_srx = call->peer->srx;
392}
393EXPORT_SYMBOL(rxrpc_kernel_get_peer);