blob: 3620c569275f58908eb19b735d475ef50b80bd83 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* RxRPC remote transport endpoint management
2 *
3 * Copyright (C) 2007 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 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
12#include <linux/module.h>
13#include <linux/net.h>
14#include <linux/skbuff.h>
15#include <linux/udp.h>
16#include <linux/in.h>
17#include <linux/in6.h>
18#include <linux/icmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070020#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include <net/ip.h>
David Howells224711d2007-05-04 12:41:11 -070023#include <net/route.h>
David Howells17926a72007-04-26 15:48:28 -070024#include "ar-internal.h"
25
26static LIST_HEAD(rxrpc_peers);
27static DEFINE_RWLOCK(rxrpc_peer_lock);
28static DECLARE_WAIT_QUEUE_HEAD(rxrpc_peer_wq);
29
30static void rxrpc_destroy_peer(struct work_struct *work);
31
32/*
David Howells224711d2007-05-04 12:41:11 -070033 * assess the MTU size for the network interface through which this peer is
34 * reached
35 */
36static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
37{
38 struct rtable *rt;
39 struct flowi fl;
David Howells224711d2007-05-04 12:41:11 -070040
41 peer->if_mtu = 1500;
42
43 memset(&fl, 0, sizeof(fl));
44
45 switch (peer->srx.transport.family) {
46 case AF_INET:
47 fl.oif = 0;
48 fl.proto = IPPROTO_UDP,
Changli Gao58116622010-11-12 18:43:55 +000049 fl.fl4_dst = peer->srx.transport.sin.sin_addr.s_addr;
50 fl.fl4_src = 0;
51 fl.fl4_tos = 0;
David Howells224711d2007-05-04 12:41:11 -070052 /* assume AFS.CM talking to AFS.FS */
Changli Gao58116622010-11-12 18:43:55 +000053 fl.fl_ip_sport = htons(7001);
54 fl.fl_ip_dport = htons(7000);
David Howells224711d2007-05-04 12:41:11 -070055 break;
56 default:
57 BUG();
58 }
59
David S. Millerb23dd4f2011-03-02 14:31:35 -080060 rt = ip_route_output_key(&init_net, &fl);
61 if (IS_ERR(rt)) {
62 _leave(" [route err %ld]", PTR_ERR(rt));
David Howells224711d2007-05-04 12:41:11 -070063 return;
64 }
65
Changli Gaod8d1f302010-06-10 23:31:35 -070066 peer->if_mtu = dst_mtu(&rt->dst);
67 dst_release(&rt->dst);
David Howells224711d2007-05-04 12:41:11 -070068
David Howellsa6a62b62007-05-10 03:15:25 -070069 _leave(" [if_mtu %u]", peer->if_mtu);
David Howells224711d2007-05-04 12:41:11 -070070}
71
72/*
David Howells17926a72007-04-26 15:48:28 -070073 * allocate a new peer
74 */
75static struct rxrpc_peer *rxrpc_alloc_peer(struct sockaddr_rxrpc *srx,
76 gfp_t gfp)
77{
78 struct rxrpc_peer *peer;
79
80 _enter("");
81
82 peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
83 if (peer) {
84 INIT_WORK(&peer->destroyer, &rxrpc_destroy_peer);
85 INIT_LIST_HEAD(&peer->link);
86 INIT_LIST_HEAD(&peer->error_targets);
87 spin_lock_init(&peer->lock);
88 atomic_set(&peer->usage, 1);
89 peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
90 memcpy(&peer->srx, srx, sizeof(*srx));
91
David Howells224711d2007-05-04 12:41:11 -070092 rxrpc_assess_MTU_size(peer);
93 peer->mtu = peer->if_mtu;
David Howells17926a72007-04-26 15:48:28 -070094
95 if (srx->transport.family == AF_INET) {
96 peer->hdrsize = sizeof(struct iphdr);
97 switch (srx->transport_type) {
98 case SOCK_DGRAM:
99 peer->hdrsize += sizeof(struct udphdr);
100 break;
101 default:
102 BUG();
103 break;
104 }
105 } else {
106 BUG();
107 }
108
109 peer->hdrsize += sizeof(struct rxrpc_header);
110 peer->maxdata = peer->mtu - peer->hdrsize;
111 }
112
113 _leave(" = %p", peer);
114 return peer;
115}
116
117/*
118 * obtain a remote transport endpoint for the specified address
119 */
120struct rxrpc_peer *rxrpc_get_peer(struct sockaddr_rxrpc *srx, gfp_t gfp)
121{
122 struct rxrpc_peer *peer, *candidate;
123 const char *new = "old";
124 int usage;
125
Harvey Harrison21454aa2008-10-31 00:54:56 -0700126 _enter("{%d,%d,%pI4+%hu}",
David Howells17926a72007-04-26 15:48:28 -0700127 srx->transport_type,
128 srx->transport_len,
Harvey Harrison21454aa2008-10-31 00:54:56 -0700129 &srx->transport.sin.sin_addr,
David Howells17926a72007-04-26 15:48:28 -0700130 ntohs(srx->transport.sin.sin_port));
131
132 /* search the peer list first */
133 read_lock_bh(&rxrpc_peer_lock);
134 list_for_each_entry(peer, &rxrpc_peers, link) {
135 _debug("check PEER %d { u=%d t=%d l=%d }",
136 peer->debug_id,
137 atomic_read(&peer->usage),
138 peer->srx.transport_type,
139 peer->srx.transport_len);
140
141 if (atomic_read(&peer->usage) > 0 &&
142 peer->srx.transport_type == srx->transport_type &&
143 peer->srx.transport_len == srx->transport_len &&
144 memcmp(&peer->srx.transport,
145 &srx->transport,
146 srx->transport_len) == 0)
147 goto found_extant_peer;
148 }
149 read_unlock_bh(&rxrpc_peer_lock);
150
151 /* not yet present - create a candidate for a new record and then
152 * redo the search */
153 candidate = rxrpc_alloc_peer(srx, gfp);
154 if (!candidate) {
155 _leave(" = -ENOMEM");
156 return ERR_PTR(-ENOMEM);
157 }
158
159 write_lock_bh(&rxrpc_peer_lock);
160
161 list_for_each_entry(peer, &rxrpc_peers, link) {
162 if (atomic_read(&peer->usage) > 0 &&
163 peer->srx.transport_type == srx->transport_type &&
164 peer->srx.transport_len == srx->transport_len &&
165 memcmp(&peer->srx.transport,
166 &srx->transport,
167 srx->transport_len) == 0)
168 goto found_extant_second;
169 }
170
171 /* we can now add the new candidate to the list */
172 peer = candidate;
173 candidate = NULL;
174
175 list_add_tail(&peer->link, &rxrpc_peers);
176 write_unlock_bh(&rxrpc_peer_lock);
177 new = "new";
178
179success:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700180 _net("PEER %s %d {%d,%u,%pI4+%hu}",
David Howells17926a72007-04-26 15:48:28 -0700181 new,
182 peer->debug_id,
183 peer->srx.transport_type,
184 peer->srx.transport.family,
Harvey Harrison21454aa2008-10-31 00:54:56 -0700185 &peer->srx.transport.sin.sin_addr,
David Howells17926a72007-04-26 15:48:28 -0700186 ntohs(peer->srx.transport.sin.sin_port));
187
188 _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
189 return peer;
190
191 /* we found the peer in the list immediately */
192found_extant_peer:
193 usage = atomic_inc_return(&peer->usage);
194 read_unlock_bh(&rxrpc_peer_lock);
195 goto success;
196
197 /* we found the peer on the second time through the list */
198found_extant_second:
199 usage = atomic_inc_return(&peer->usage);
200 write_unlock_bh(&rxrpc_peer_lock);
201 kfree(candidate);
202 goto success;
203}
204
205/*
206 * find the peer associated with a packet
207 */
208struct rxrpc_peer *rxrpc_find_peer(struct rxrpc_local *local,
209 __be32 addr, __be16 port)
210{
211 struct rxrpc_peer *peer;
212
213 _enter("");
214
215 /* search the peer list */
216 read_lock_bh(&rxrpc_peer_lock);
217
218 if (local->srx.transport.family == AF_INET &&
219 local->srx.transport_type == SOCK_DGRAM
220 ) {
221 list_for_each_entry(peer, &rxrpc_peers, link) {
222 if (atomic_read(&peer->usage) > 0 &&
223 peer->srx.transport_type == SOCK_DGRAM &&
224 peer->srx.transport.family == AF_INET &&
225 peer->srx.transport.sin.sin_port == port &&
226 peer->srx.transport.sin.sin_addr.s_addr == addr)
227 goto found_UDP_peer;
228 }
229
230 goto new_UDP_peer;
231 }
232
233 read_unlock_bh(&rxrpc_peer_lock);
234 _leave(" = -EAFNOSUPPORT");
235 return ERR_PTR(-EAFNOSUPPORT);
236
237found_UDP_peer:
238 _net("Rx UDP DGRAM from peer %d", peer->debug_id);
239 atomic_inc(&peer->usage);
240 read_unlock_bh(&rxrpc_peer_lock);
241 _leave(" = %p", peer);
242 return peer;
243
244new_UDP_peer:
245 _net("Rx UDP DGRAM from NEW peer %d", peer->debug_id);
246 read_unlock_bh(&rxrpc_peer_lock);
247 _leave(" = -EBUSY [new]");
248 return ERR_PTR(-EBUSY);
249}
250
251/*
252 * release a remote transport endpoint
253 */
254void rxrpc_put_peer(struct rxrpc_peer *peer)
255{
256 _enter("%p{u=%d}", peer, atomic_read(&peer->usage));
257
258 ASSERTCMP(atomic_read(&peer->usage), >, 0);
259
260 if (likely(!atomic_dec_and_test(&peer->usage))) {
261 _leave(" [in use]");
262 return;
263 }
264
David Howells651350d2007-04-26 15:50:17 -0700265 rxrpc_queue_work(&peer->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700266 _leave("");
267}
268
269/*
270 * destroy a remote transport endpoint
271 */
272static void rxrpc_destroy_peer(struct work_struct *work)
273{
274 struct rxrpc_peer *peer =
275 container_of(work, struct rxrpc_peer, destroyer);
276
277 _enter("%p{%d}", peer, atomic_read(&peer->usage));
278
279 write_lock_bh(&rxrpc_peer_lock);
280 list_del(&peer->link);
281 write_unlock_bh(&rxrpc_peer_lock);
282
283 _net("DESTROY PEER %d", peer->debug_id);
284 kfree(peer);
285
286 if (list_empty(&rxrpc_peers))
287 wake_up_all(&rxrpc_peer_wq);
288 _leave("");
289}
290
291/*
292 * preemptively destroy all the peer records from a transport endpoint rather
293 * than waiting for them to time out
294 */
295void __exit rxrpc_destroy_all_peers(void)
296{
297 DECLARE_WAITQUEUE(myself,current);
298
299 _enter("");
300
301 /* we simply have to wait for them to go away */
302 if (!list_empty(&rxrpc_peers)) {
303 set_current_state(TASK_UNINTERRUPTIBLE);
304 add_wait_queue(&rxrpc_peer_wq, &myself);
305
306 while (!list_empty(&rxrpc_peers)) {
307 schedule();
308 set_current_state(TASK_UNINTERRUPTIBLE);
309 }
310
311 remove_wait_queue(&rxrpc_peer_wq, &myself);
312 set_current_state(TASK_RUNNING);
313 }
314
315 _leave("");
316}