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>
19 #include <linux/slab.h>
21 #include <net/af_rxrpc.h>
23 #include <net/route.h>
24 #include "ar-internal.h"
26 static LIST_HEAD(rxrpc_peers
);
27 static DEFINE_RWLOCK(rxrpc_peer_lock
);
28 static DECLARE_WAIT_QUEUE_HEAD(rxrpc_peer_wq
);
30 static void rxrpc_destroy_peer(struct work_struct
*work
);
33 * assess the MTU size for the network interface through which this peer is
36 static void rxrpc_assess_MTU_size(struct rxrpc_peer
*peer
)
44 memset(&fl
, 0, sizeof(fl
));
46 switch (peer
->srx
.transport
.family
) {
49 fl
.proto
= IPPROTO_UDP
,
50 fl
.nl_u
.ip4_u
.saddr
= 0;
51 fl
.nl_u
.ip4_u
.daddr
= peer
->srx
.transport
.sin
.sin_addr
.s_addr
;
52 fl
.nl_u
.ip4_u
.tos
= 0;
53 /* assume AFS.CM talking to AFS.FS */
54 fl
.uli_u
.ports
.sport
= htons(7001);
55 fl
.uli_u
.ports
.dport
= htons(7000);
61 ret
= ip_route_output_key(&init_net
, &rt
, &fl
);
63 _leave(" [route err %d]", ret
);
67 peer
->if_mtu
= dst_mtu(&rt
->u
.dst
);
68 dst_release(&rt
->u
.dst
);
70 _leave(" [if_mtu %u]", peer
->if_mtu
);
76 static struct rxrpc_peer
*rxrpc_alloc_peer(struct sockaddr_rxrpc
*srx
,
79 struct rxrpc_peer
*peer
;
83 peer
= kzalloc(sizeof(struct rxrpc_peer
), gfp
);
85 INIT_WORK(&peer
->destroyer
, &rxrpc_destroy_peer
);
86 INIT_LIST_HEAD(&peer
->link
);
87 INIT_LIST_HEAD(&peer
->error_targets
);
88 spin_lock_init(&peer
->lock
);
89 atomic_set(&peer
->usage
, 1);
90 peer
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
91 memcpy(&peer
->srx
, srx
, sizeof(*srx
));
93 rxrpc_assess_MTU_size(peer
);
94 peer
->mtu
= peer
->if_mtu
;
96 if (srx
->transport
.family
== AF_INET
) {
97 peer
->hdrsize
= sizeof(struct iphdr
);
98 switch (srx
->transport_type
) {
100 peer
->hdrsize
+= sizeof(struct udphdr
);
110 peer
->hdrsize
+= sizeof(struct rxrpc_header
);
111 peer
->maxdata
= peer
->mtu
- peer
->hdrsize
;
114 _leave(" = %p", peer
);
119 * obtain a remote transport endpoint for the specified address
121 struct rxrpc_peer
*rxrpc_get_peer(struct sockaddr_rxrpc
*srx
, gfp_t gfp
)
123 struct rxrpc_peer
*peer
, *candidate
;
124 const char *new = "old";
127 _enter("{%d,%d,%pI4+%hu}",
130 &srx
->transport
.sin
.sin_addr
,
131 ntohs(srx
->transport
.sin
.sin_port
));
133 /* search the peer list first */
134 read_lock_bh(&rxrpc_peer_lock
);
135 list_for_each_entry(peer
, &rxrpc_peers
, link
) {
136 _debug("check PEER %d { u=%d t=%d l=%d }",
138 atomic_read(&peer
->usage
),
139 peer
->srx
.transport_type
,
140 peer
->srx
.transport_len
);
142 if (atomic_read(&peer
->usage
) > 0 &&
143 peer
->srx
.transport_type
== srx
->transport_type
&&
144 peer
->srx
.transport_len
== srx
->transport_len
&&
145 memcmp(&peer
->srx
.transport
,
147 srx
->transport_len
) == 0)
148 goto found_extant_peer
;
150 read_unlock_bh(&rxrpc_peer_lock
);
152 /* not yet present - create a candidate for a new record and then
154 candidate
= rxrpc_alloc_peer(srx
, gfp
);
156 _leave(" = -ENOMEM");
157 return ERR_PTR(-ENOMEM
);
160 write_lock_bh(&rxrpc_peer_lock
);
162 list_for_each_entry(peer
, &rxrpc_peers
, link
) {
163 if (atomic_read(&peer
->usage
) > 0 &&
164 peer
->srx
.transport_type
== srx
->transport_type
&&
165 peer
->srx
.transport_len
== srx
->transport_len
&&
166 memcmp(&peer
->srx
.transport
,
168 srx
->transport_len
) == 0)
169 goto found_extant_second
;
172 /* we can now add the new candidate to the list */
176 list_add_tail(&peer
->link
, &rxrpc_peers
);
177 write_unlock_bh(&rxrpc_peer_lock
);
181 _net("PEER %s %d {%d,%u,%pI4+%hu}",
184 peer
->srx
.transport_type
,
185 peer
->srx
.transport
.family
,
186 &peer
->srx
.transport
.sin
.sin_addr
,
187 ntohs(peer
->srx
.transport
.sin
.sin_port
));
189 _leave(" = %p {u=%d}", peer
, atomic_read(&peer
->usage
));
192 /* we found the peer in the list immediately */
194 usage
= atomic_inc_return(&peer
->usage
);
195 read_unlock_bh(&rxrpc_peer_lock
);
198 /* we found the peer on the second time through the list */
200 usage
= atomic_inc_return(&peer
->usage
);
201 write_unlock_bh(&rxrpc_peer_lock
);
207 * find the peer associated with a packet
209 struct rxrpc_peer
*rxrpc_find_peer(struct rxrpc_local
*local
,
210 __be32 addr
, __be16 port
)
212 struct rxrpc_peer
*peer
;
216 /* search the peer list */
217 read_lock_bh(&rxrpc_peer_lock
);
219 if (local
->srx
.transport
.family
== AF_INET
&&
220 local
->srx
.transport_type
== SOCK_DGRAM
222 list_for_each_entry(peer
, &rxrpc_peers
, link
) {
223 if (atomic_read(&peer
->usage
) > 0 &&
224 peer
->srx
.transport_type
== SOCK_DGRAM
&&
225 peer
->srx
.transport
.family
== AF_INET
&&
226 peer
->srx
.transport
.sin
.sin_port
== port
&&
227 peer
->srx
.transport
.sin
.sin_addr
.s_addr
== addr
)
234 read_unlock_bh(&rxrpc_peer_lock
);
235 _leave(" = -EAFNOSUPPORT");
236 return ERR_PTR(-EAFNOSUPPORT
);
239 _net("Rx UDP DGRAM from peer %d", peer
->debug_id
);
240 atomic_inc(&peer
->usage
);
241 read_unlock_bh(&rxrpc_peer_lock
);
242 _leave(" = %p", peer
);
246 _net("Rx UDP DGRAM from NEW peer %d", peer
->debug_id
);
247 read_unlock_bh(&rxrpc_peer_lock
);
248 _leave(" = -EBUSY [new]");
249 return ERR_PTR(-EBUSY
);
253 * release a remote transport endpoint
255 void rxrpc_put_peer(struct rxrpc_peer
*peer
)
257 _enter("%p{u=%d}", peer
, atomic_read(&peer
->usage
));
259 ASSERTCMP(atomic_read(&peer
->usage
), >, 0);
261 if (likely(!atomic_dec_and_test(&peer
->usage
))) {
266 rxrpc_queue_work(&peer
->destroyer
);
271 * destroy a remote transport endpoint
273 static void rxrpc_destroy_peer(struct work_struct
*work
)
275 struct rxrpc_peer
*peer
=
276 container_of(work
, struct rxrpc_peer
, destroyer
);
278 _enter("%p{%d}", peer
, atomic_read(&peer
->usage
));
280 write_lock_bh(&rxrpc_peer_lock
);
281 list_del(&peer
->link
);
282 write_unlock_bh(&rxrpc_peer_lock
);
284 _net("DESTROY PEER %d", peer
->debug_id
);
287 if (list_empty(&rxrpc_peers
))
288 wake_up_all(&rxrpc_peer_wq
);
293 * preemptively destroy all the peer records from a transport endpoint rather
294 * than waiting for them to time out
296 void __exit
rxrpc_destroy_all_peers(void)
298 DECLARE_WAITQUEUE(myself
,current
);
302 /* we simply have to wait for them to go away */
303 if (!list_empty(&rxrpc_peers
)) {
304 set_current_state(TASK_UNINTERRUPTIBLE
);
305 add_wait_queue(&rxrpc_peer_wq
, &myself
);
307 while (!list_empty(&rxrpc_peers
)) {
309 set_current_state(TASK_UNINTERRUPTIBLE
);
312 remove_wait_queue(&rxrpc_peer_wq
, &myself
);
313 set_current_state(TASK_RUNNING
);