1 /* RxRPC remote transport endpoint management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/udp.h>
17 #include <linux/in6.h>
18 #include <linux/icmp.h>
20 #include <net/af_rxrpc.h>
22 #include <net/route.h>
23 #include "ar-internal.h"
25 static LIST_HEAD(rxrpc_peers
);
26 static DEFINE_RWLOCK(rxrpc_peer_lock
);
27 static DECLARE_WAIT_QUEUE_HEAD(rxrpc_peer_wq
);
29 static void rxrpc_destroy_peer(struct work_struct
*work
);
32 * assess the MTU size for the network interface through which this peer is
35 static void rxrpc_assess_MTU_size(struct rxrpc_peer
*peer
)
43 memset(&fl
, 0, sizeof(fl
));
45 switch (peer
->srx
.transport
.family
) {
48 fl
.proto
= IPPROTO_UDP
,
49 fl
.nl_u
.ip4_u
.saddr
= 0;
50 fl
.nl_u
.ip4_u
.daddr
= peer
->srx
.transport
.sin
.sin_addr
.s_addr
;
51 fl
.nl_u
.ip4_u
.tos
= 0;
52 /* assume AFS.CM talking to AFS.FS */
53 fl
.uli_u
.ports
.sport
= htons(7001);
54 fl
.uli_u
.ports
.dport
= htons(7000);
60 ret
= ip_route_output_key(&rt
, &fl
);
62 _leave(" [route err %d]", ret
);
66 peer
->if_mtu
= dst_mtu(&rt
->u
.dst
);
67 dst_release(&rt
->u
.dst
);
69 _leave(" [if_mtu %u]", peer
->if_mtu
);
75 static struct rxrpc_peer
*rxrpc_alloc_peer(struct sockaddr_rxrpc
*srx
,
78 struct rxrpc_peer
*peer
;
82 peer
= kzalloc(sizeof(struct rxrpc_peer
), gfp
);
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
));
92 rxrpc_assess_MTU_size(peer
);
93 peer
->mtu
= peer
->if_mtu
;
95 if (srx
->transport
.family
== AF_INET
) {
96 peer
->hdrsize
= sizeof(struct iphdr
);
97 switch (srx
->transport_type
) {
99 peer
->hdrsize
+= sizeof(struct udphdr
);
109 peer
->hdrsize
+= sizeof(struct rxrpc_header
);
110 peer
->maxdata
= peer
->mtu
- peer
->hdrsize
;
113 _leave(" = %p", peer
);
118 * obtain a remote transport endpoint for the specified address
120 struct rxrpc_peer
*rxrpc_get_peer(struct sockaddr_rxrpc
*srx
, gfp_t gfp
)
122 struct rxrpc_peer
*peer
, *candidate
;
123 const char *new = "old";
126 _enter("{%d,%d,%u.%u.%u.%u+%hu}",
129 NIPQUAD(srx
->transport
.sin
.sin_addr
),
130 ntohs(srx
->transport
.sin
.sin_port
));
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 }",
137 atomic_read(&peer
->usage
),
138 peer
->srx
.transport_type
,
139 peer
->srx
.transport_len
);
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
,
146 srx
->transport_len
) == 0)
147 goto found_extant_peer
;
149 read_unlock_bh(&rxrpc_peer_lock
);
151 /* not yet present - create a candidate for a new record and then
153 candidate
= rxrpc_alloc_peer(srx
, gfp
);
155 _leave(" = -ENOMEM");
156 return ERR_PTR(-ENOMEM
);
159 write_lock_bh(&rxrpc_peer_lock
);
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
,
167 srx
->transport_len
) == 0)
168 goto found_extant_second
;
171 /* we can now add the new candidate to the list */
175 list_add_tail(&peer
->link
, &rxrpc_peers
);
176 write_unlock_bh(&rxrpc_peer_lock
);
180 _net("PEER %s %d {%d,%u,%u.%u.%u.%u+%hu}",
183 peer
->srx
.transport_type
,
184 peer
->srx
.transport
.family
,
185 NIPQUAD(peer
->srx
.transport
.sin
.sin_addr
),
186 ntohs(peer
->srx
.transport
.sin
.sin_port
));
188 _leave(" = %p {u=%d}", peer
, atomic_read(&peer
->usage
));
191 /* we found the peer in the list immediately */
193 usage
= atomic_inc_return(&peer
->usage
);
194 read_unlock_bh(&rxrpc_peer_lock
);
197 /* we found the peer on the second time through the list */
199 usage
= atomic_inc_return(&peer
->usage
);
200 write_unlock_bh(&rxrpc_peer_lock
);
206 * find the peer associated with a packet
208 struct rxrpc_peer
*rxrpc_find_peer(struct rxrpc_local
*local
,
209 __be32 addr
, __be16 port
)
211 struct rxrpc_peer
*peer
;
215 /* search the peer list */
216 read_lock_bh(&rxrpc_peer_lock
);
218 if (local
->srx
.transport
.family
== AF_INET
&&
219 local
->srx
.transport_type
== SOCK_DGRAM
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
)
233 read_unlock_bh(&rxrpc_peer_lock
);
234 _leave(" = -EAFNOSUPPORT");
235 return ERR_PTR(-EAFNOSUPPORT
);
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
);
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
);
252 * release a remote transport endpoint
254 void rxrpc_put_peer(struct rxrpc_peer
*peer
)
256 _enter("%p{u=%d}", peer
, atomic_read(&peer
->usage
));
258 ASSERTCMP(atomic_read(&peer
->usage
), >, 0);
260 if (likely(!atomic_dec_and_test(&peer
->usage
))) {
265 rxrpc_queue_work(&peer
->destroyer
);
270 * destroy a remote transport endpoint
272 static void rxrpc_destroy_peer(struct work_struct
*work
)
274 struct rxrpc_peer
*peer
=
275 container_of(work
, struct rxrpc_peer
, destroyer
);
277 _enter("%p{%d}", peer
, atomic_read(&peer
->usage
));
279 write_lock_bh(&rxrpc_peer_lock
);
280 list_del(&peer
->link
);
281 write_unlock_bh(&rxrpc_peer_lock
);
283 _net("DESTROY PEER %d", peer
->debug_id
);
286 if (list_empty(&rxrpc_peers
))
287 wake_up_all(&rxrpc_peer_wq
);
292 * preemptively destroy all the peer records from a transport endpoint rather
293 * than waiting for them to time out
295 void __exit
rxrpc_destroy_all_peers(void)
297 DECLARE_WAITQUEUE(myself
,current
);
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
);
306 while (!list_empty(&rxrpc_peers
)) {
308 set_current_state(TASK_UNINTERRUPTIBLE
);
311 remove_wait_queue(&rxrpc_peer_wq
, &myself
);
312 set_current_state(TASK_RUNNING
);