1 /* 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>
19 #include <linux/slab.h>
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #include <net/ip.h>
23 #include <net/route.h>
24 #include "ar-internal.h"
25
26 static LIST_HEAD(rxrpc_peers);
27 static DEFINE_RWLOCK(rxrpc_peer_lock);
28 static DECLARE_WAIT_QUEUE_HEAD(rxrpc_peer_wq);
29
30 static void rxrpc_destroy_peer(struct work_struct *work);
31
32 /*
33 * assess the MTU size for the network interface through which this peer is
34 * reached
35 */
rxrpc_assess_MTU_size(struct rxrpc_peer * peer)36 static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
37 {
38 struct rtable *rt;
39
40 peer->if_mtu = 1500;
41
42 rt = ip_route_output_ports(&init_net, NULL,
43 peer->srx.transport.sin.sin_addr.s_addr, 0,
44 htons(7000), htons(7001),
45 IPPROTO_UDP, 0, 0);
46 if (IS_ERR(rt)) {
47 _leave(" [route err %ld]", PTR_ERR(rt));
48 return;
49 }
50
51 peer->if_mtu = dst_mtu(&rt->dst);
52 dst_release(&rt->dst);
53
54 _leave(" [if_mtu %u]", peer->if_mtu);
55 }
56
57 /*
58 * allocate a new peer
59 */
rxrpc_alloc_peer(struct sockaddr_rxrpc * srx,gfp_t gfp)60 static struct rxrpc_peer *rxrpc_alloc_peer(struct sockaddr_rxrpc *srx,
61 gfp_t gfp)
62 {
63 struct rxrpc_peer *peer;
64
65 _enter("");
66
67 peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
68 if (peer) {
69 INIT_WORK(&peer->destroyer, &rxrpc_destroy_peer);
70 INIT_LIST_HEAD(&peer->link);
71 INIT_LIST_HEAD(&peer->error_targets);
72 spin_lock_init(&peer->lock);
73 atomic_set(&peer->usage, 1);
74 peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
75 memcpy(&peer->srx, srx, sizeof(*srx));
76
77 rxrpc_assess_MTU_size(peer);
78 peer->mtu = peer->if_mtu;
79
80 if (srx->transport.family == AF_INET) {
81 peer->hdrsize = sizeof(struct iphdr);
82 switch (srx->transport_type) {
83 case SOCK_DGRAM:
84 peer->hdrsize += sizeof(struct udphdr);
85 break;
86 default:
87 BUG();
88 break;
89 }
90 } else {
91 BUG();
92 }
93
94 peer->hdrsize += sizeof(struct rxrpc_header);
95 peer->maxdata = peer->mtu - peer->hdrsize;
96 }
97
98 _leave(" = %p", peer);
99 return peer;
100 }
101
102 /*
103 * obtain a remote transport endpoint for the specified address
104 */
rxrpc_get_peer(struct sockaddr_rxrpc * srx,gfp_t gfp)105 struct rxrpc_peer *rxrpc_get_peer(struct sockaddr_rxrpc *srx, gfp_t gfp)
106 {
107 struct rxrpc_peer *peer, *candidate;
108 const char *new = "old";
109 int usage;
110
111 _enter("{%d,%d,%pI4+%hu}",
112 srx->transport_type,
113 srx->transport_len,
114 &srx->transport.sin.sin_addr,
115 ntohs(srx->transport.sin.sin_port));
116
117 /* search the peer list first */
118 read_lock_bh(&rxrpc_peer_lock);
119 list_for_each_entry(peer, &rxrpc_peers, link) {
120 _debug("check PEER %d { u=%d t=%d l=%d }",
121 peer->debug_id,
122 atomic_read(&peer->usage),
123 peer->srx.transport_type,
124 peer->srx.transport_len);
125
126 if (atomic_read(&peer->usage) > 0 &&
127 peer->srx.transport_type == srx->transport_type &&
128 peer->srx.transport_len == srx->transport_len &&
129 memcmp(&peer->srx.transport,
130 &srx->transport,
131 srx->transport_len) == 0)
132 goto found_extant_peer;
133 }
134 read_unlock_bh(&rxrpc_peer_lock);
135
136 /* not yet present - create a candidate for a new record and then
137 * redo the search */
138 candidate = rxrpc_alloc_peer(srx, gfp);
139 if (!candidate) {
140 _leave(" = -ENOMEM");
141 return ERR_PTR(-ENOMEM);
142 }
143
144 write_lock_bh(&rxrpc_peer_lock);
145
146 list_for_each_entry(peer, &rxrpc_peers, link) {
147 if (atomic_read(&peer->usage) > 0 &&
148 peer->srx.transport_type == srx->transport_type &&
149 peer->srx.transport_len == srx->transport_len &&
150 memcmp(&peer->srx.transport,
151 &srx->transport,
152 srx->transport_len) == 0)
153 goto found_extant_second;
154 }
155
156 /* we can now add the new candidate to the list */
157 peer = candidate;
158 candidate = NULL;
159
160 list_add_tail(&peer->link, &rxrpc_peers);
161 write_unlock_bh(&rxrpc_peer_lock);
162 new = "new";
163
164 success:
165 _net("PEER %s %d {%d,%u,%pI4+%hu}",
166 new,
167 peer->debug_id,
168 peer->srx.transport_type,
169 peer->srx.transport.family,
170 &peer->srx.transport.sin.sin_addr,
171 ntohs(peer->srx.transport.sin.sin_port));
172
173 _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
174 return peer;
175
176 /* we found the peer in the list immediately */
177 found_extant_peer:
178 usage = atomic_inc_return(&peer->usage);
179 read_unlock_bh(&rxrpc_peer_lock);
180 goto success;
181
182 /* we found the peer on the second time through the list */
183 found_extant_second:
184 usage = atomic_inc_return(&peer->usage);
185 write_unlock_bh(&rxrpc_peer_lock);
186 kfree(candidate);
187 goto success;
188 }
189
190 /*
191 * find the peer associated with a packet
192 */
rxrpc_find_peer(struct rxrpc_local * local,__be32 addr,__be16 port)193 struct rxrpc_peer *rxrpc_find_peer(struct rxrpc_local *local,
194 __be32 addr, __be16 port)
195 {
196 struct rxrpc_peer *peer;
197
198 _enter("");
199
200 /* search the peer list */
201 read_lock_bh(&rxrpc_peer_lock);
202
203 if (local->srx.transport.family == AF_INET &&
204 local->srx.transport_type == SOCK_DGRAM
205 ) {
206 list_for_each_entry(peer, &rxrpc_peers, link) {
207 if (atomic_read(&peer->usage) > 0 &&
208 peer->srx.transport_type == SOCK_DGRAM &&
209 peer->srx.transport.family == AF_INET &&
210 peer->srx.transport.sin.sin_port == port &&
211 peer->srx.transport.sin.sin_addr.s_addr == addr)
212 goto found_UDP_peer;
213 }
214
215 goto new_UDP_peer;
216 }
217
218 read_unlock_bh(&rxrpc_peer_lock);
219 _leave(" = -EAFNOSUPPORT");
220 return ERR_PTR(-EAFNOSUPPORT);
221
222 found_UDP_peer:
223 _net("Rx UDP DGRAM from peer %d", peer->debug_id);
224 atomic_inc(&peer->usage);
225 read_unlock_bh(&rxrpc_peer_lock);
226 _leave(" = %p", peer);
227 return peer;
228
229 new_UDP_peer:
230 _net("Rx UDP DGRAM from NEW peer %d", peer->debug_id);
231 read_unlock_bh(&rxrpc_peer_lock);
232 _leave(" = -EBUSY [new]");
233 return ERR_PTR(-EBUSY);
234 }
235
236 /*
237 * release a remote transport endpoint
238 */
rxrpc_put_peer(struct rxrpc_peer * peer)239 void rxrpc_put_peer(struct rxrpc_peer *peer)
240 {
241 _enter("%p{u=%d}", peer, atomic_read(&peer->usage));
242
243 ASSERTCMP(atomic_read(&peer->usage), >, 0);
244
245 if (likely(!atomic_dec_and_test(&peer->usage))) {
246 _leave(" [in use]");
247 return;
248 }
249
250 rxrpc_queue_work(&peer->destroyer);
251 _leave("");
252 }
253
254 /*
255 * destroy a remote transport endpoint
256 */
rxrpc_destroy_peer(struct work_struct * work)257 static void rxrpc_destroy_peer(struct work_struct *work)
258 {
259 struct rxrpc_peer *peer =
260 container_of(work, struct rxrpc_peer, destroyer);
261
262 _enter("%p{%d}", peer, atomic_read(&peer->usage));
263
264 write_lock_bh(&rxrpc_peer_lock);
265 list_del(&peer->link);
266 write_unlock_bh(&rxrpc_peer_lock);
267
268 _net("DESTROY PEER %d", peer->debug_id);
269 kfree(peer);
270
271 if (list_empty(&rxrpc_peers))
272 wake_up_all(&rxrpc_peer_wq);
273 _leave("");
274 }
275
276 /*
277 * preemptively destroy all the peer records from a transport endpoint rather
278 * than waiting for them to time out
279 */
rxrpc_destroy_all_peers(void)280 void __exit rxrpc_destroy_all_peers(void)
281 {
282 DECLARE_WAITQUEUE(myself,current);
283
284 _enter("");
285
286 /* we simply have to wait for them to go away */
287 if (!list_empty(&rxrpc_peers)) {
288 set_current_state(TASK_UNINTERRUPTIBLE);
289 add_wait_queue(&rxrpc_peer_wq, &myself);
290
291 while (!list_empty(&rxrpc_peers)) {
292 schedule();
293 set_current_state(TASK_UNINTERRUPTIBLE);
294 }
295
296 remove_wait_queue(&rxrpc_peer_wq, &myself);
297 set_current_state(TASK_RUNNING);
298 }
299
300 _leave("");
301 }
302