0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/module.h>
0011 #include <linux/slab.h>
0012 #include <linux/net.h>
0013 #include <linux/skbuff.h>
0014 #include "ar-internal.h"
0015
0016
0017
0018
0019 unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60;
0020 unsigned int __read_mostly rxrpc_closed_conn_expiry = 10;
0021
0022 static void rxrpc_destroy_connection(struct rcu_head *);
0023
0024 static void rxrpc_connection_timer(struct timer_list *timer)
0025 {
0026 struct rxrpc_connection *conn =
0027 container_of(timer, struct rxrpc_connection, timer);
0028
0029 rxrpc_queue_conn(conn);
0030 }
0031
0032
0033
0034
0035 struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp)
0036 {
0037 struct rxrpc_connection *conn;
0038
0039 _enter("");
0040
0041 conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
0042 if (conn) {
0043 INIT_LIST_HEAD(&conn->cache_link);
0044 timer_setup(&conn->timer, &rxrpc_connection_timer, 0);
0045 INIT_WORK(&conn->processor, &rxrpc_process_connection);
0046 INIT_LIST_HEAD(&conn->proc_link);
0047 INIT_LIST_HEAD(&conn->link);
0048 skb_queue_head_init(&conn->rx_queue);
0049 conn->security = &rxrpc_no_security;
0050 spin_lock_init(&conn->state_lock);
0051 conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
0052 conn->idle_timestamp = jiffies;
0053 }
0054
0055 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
0056 return conn;
0057 }
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local,
0071 struct sk_buff *skb,
0072 struct rxrpc_peer **_peer)
0073 {
0074 struct rxrpc_connection *conn;
0075 struct rxrpc_conn_proto k;
0076 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
0077 struct sockaddr_rxrpc srx;
0078 struct rxrpc_peer *peer;
0079
0080 _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK);
0081
0082 if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
0083 goto not_found;
0084
0085 if (srx.transport.family != local->srx.transport.family &&
0086 (srx.transport.family == AF_INET &&
0087 local->srx.transport.family != AF_INET6)) {
0088 pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n",
0089 srx.transport.family,
0090 local->srx.transport.family);
0091 goto not_found;
0092 }
0093
0094 k.epoch = sp->hdr.epoch;
0095 k.cid = sp->hdr.cid & RXRPC_CIDMASK;
0096
0097 if (rxrpc_to_server(sp)) {
0098
0099
0100
0101
0102 peer = rxrpc_lookup_peer_rcu(local, &srx);
0103 if (!peer)
0104 goto not_found;
0105 *_peer = peer;
0106 conn = rxrpc_find_service_conn_rcu(peer, skb);
0107 if (!conn || refcount_read(&conn->ref) == 0)
0108 goto not_found;
0109 _leave(" = %p", conn);
0110 return conn;
0111 } else {
0112
0113
0114
0115 conn = idr_find(&rxrpc_client_conn_ids,
0116 sp->hdr.cid >> RXRPC_CIDSHIFT);
0117 if (!conn || refcount_read(&conn->ref) == 0) {
0118 _debug("no conn");
0119 goto not_found;
0120 }
0121
0122 if (conn->proto.epoch != k.epoch ||
0123 conn->params.local != local)
0124 goto not_found;
0125
0126 peer = conn->params.peer;
0127 switch (srx.transport.family) {
0128 case AF_INET:
0129 if (peer->srx.transport.sin.sin_port !=
0130 srx.transport.sin.sin_port ||
0131 peer->srx.transport.sin.sin_addr.s_addr !=
0132 srx.transport.sin.sin_addr.s_addr)
0133 goto not_found;
0134 break;
0135 #ifdef CONFIG_AF_RXRPC_IPV6
0136 case AF_INET6:
0137 if (peer->srx.transport.sin6.sin6_port !=
0138 srx.transport.sin6.sin6_port ||
0139 memcmp(&peer->srx.transport.sin6.sin6_addr,
0140 &srx.transport.sin6.sin6_addr,
0141 sizeof(struct in6_addr)) != 0)
0142 goto not_found;
0143 break;
0144 #endif
0145 default:
0146 BUG();
0147 }
0148
0149 _leave(" = %p", conn);
0150 return conn;
0151 }
0152
0153 not_found:
0154 _leave(" = NULL");
0155 return NULL;
0156 }
0157
0158
0159
0160
0161
0162
0163 void __rxrpc_disconnect_call(struct rxrpc_connection *conn,
0164 struct rxrpc_call *call)
0165 {
0166 struct rxrpc_channel *chan =
0167 &conn->channels[call->cid & RXRPC_CHANNELMASK];
0168
0169 _enter("%d,%x", conn->debug_id, call->cid);
0170
0171 if (rcu_access_pointer(chan->call) == call) {
0172
0173
0174
0175 trace_rxrpc_disconnect_call(call);
0176 switch (call->completion) {
0177 case RXRPC_CALL_SUCCEEDED:
0178 chan->last_seq = call->rx_hard_ack;
0179 chan->last_type = RXRPC_PACKET_TYPE_ACK;
0180 break;
0181 case RXRPC_CALL_LOCALLY_ABORTED:
0182 chan->last_abort = call->abort_code;
0183 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
0184 break;
0185 default:
0186 chan->last_abort = RX_CALL_DEAD;
0187 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
0188 break;
0189 }
0190
0191
0192 smp_wmb();
0193 chan->last_call = chan->call_id;
0194 chan->call_id = chan->call_counter;
0195
0196 rcu_assign_pointer(chan->call, NULL);
0197 }
0198
0199 _leave("");
0200 }
0201
0202
0203
0204
0205
0206 void rxrpc_disconnect_call(struct rxrpc_call *call)
0207 {
0208 struct rxrpc_connection *conn = call->conn;
0209
0210 call->peer->cong_cwnd = call->cong_cwnd;
0211
0212 if (!hlist_unhashed(&call->error_link)) {
0213 spin_lock_bh(&call->peer->lock);
0214 hlist_del_rcu(&call->error_link);
0215 spin_unlock_bh(&call->peer->lock);
0216 }
0217
0218 if (rxrpc_is_client_call(call))
0219 return rxrpc_disconnect_client_call(conn->bundle, call);
0220
0221 spin_lock(&conn->bundle->channel_lock);
0222 __rxrpc_disconnect_call(conn, call);
0223 spin_unlock(&conn->bundle->channel_lock);
0224
0225 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
0226 conn->idle_timestamp = jiffies;
0227 }
0228
0229
0230
0231
0232 void rxrpc_kill_connection(struct rxrpc_connection *conn)
0233 {
0234 struct rxrpc_net *rxnet = conn->params.local->rxnet;
0235
0236 ASSERT(!rcu_access_pointer(conn->channels[0].call) &&
0237 !rcu_access_pointer(conn->channels[1].call) &&
0238 !rcu_access_pointer(conn->channels[2].call) &&
0239 !rcu_access_pointer(conn->channels[3].call));
0240 ASSERT(list_empty(&conn->cache_link));
0241
0242 write_lock(&rxnet->conn_lock);
0243 list_del_init(&conn->proc_link);
0244 write_unlock(&rxnet->conn_lock);
0245
0246
0247
0248
0249
0250 rxrpc_purge_queue(&conn->rx_queue);
0251
0252
0253
0254
0255
0256 call_rcu(&conn->rcu, rxrpc_destroy_connection);
0257 }
0258
0259
0260
0261
0262
0263 bool rxrpc_queue_conn(struct rxrpc_connection *conn)
0264 {
0265 const void *here = __builtin_return_address(0);
0266 int r;
0267
0268 if (!__refcount_inc_not_zero(&conn->ref, &r))
0269 return false;
0270 if (rxrpc_queue_work(&conn->processor))
0271 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_queued, r + 1, here);
0272 else
0273 rxrpc_put_connection(conn);
0274 return true;
0275 }
0276
0277
0278
0279
0280 void rxrpc_see_connection(struct rxrpc_connection *conn)
0281 {
0282 const void *here = __builtin_return_address(0);
0283 if (conn) {
0284 int n = refcount_read(&conn->ref);
0285
0286 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_seen, n, here);
0287 }
0288 }
0289
0290
0291
0292
0293 struct rxrpc_connection *rxrpc_get_connection(struct rxrpc_connection *conn)
0294 {
0295 const void *here = __builtin_return_address(0);
0296 int r;
0297
0298 __refcount_inc(&conn->ref, &r);
0299 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_got, r, here);
0300 return conn;
0301 }
0302
0303
0304
0305
0306 struct rxrpc_connection *
0307 rxrpc_get_connection_maybe(struct rxrpc_connection *conn)
0308 {
0309 const void *here = __builtin_return_address(0);
0310 int r;
0311
0312 if (conn) {
0313 if (__refcount_inc_not_zero(&conn->ref, &r))
0314 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_got, r + 1, here);
0315 else
0316 conn = NULL;
0317 }
0318 return conn;
0319 }
0320
0321
0322
0323
0324 static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
0325 unsigned long reap_at)
0326 {
0327 if (rxnet->live)
0328 timer_reduce(&rxnet->service_conn_reap_timer, reap_at);
0329 }
0330
0331
0332
0333
0334 void rxrpc_put_service_conn(struct rxrpc_connection *conn)
0335 {
0336 const void *here = __builtin_return_address(0);
0337 unsigned int debug_id = conn->debug_id;
0338 int r;
0339
0340 __refcount_dec(&conn->ref, &r);
0341 trace_rxrpc_conn(debug_id, rxrpc_conn_put_service, r - 1, here);
0342 if (r - 1 == 1)
0343 rxrpc_set_service_reap_timer(conn->params.local->rxnet,
0344 jiffies + rxrpc_connection_expiry);
0345 }
0346
0347
0348
0349
0350 static void rxrpc_destroy_connection(struct rcu_head *rcu)
0351 {
0352 struct rxrpc_connection *conn =
0353 container_of(rcu, struct rxrpc_connection, rcu);
0354
0355 _enter("{%d,u=%d}", conn->debug_id, refcount_read(&conn->ref));
0356
0357 ASSERTCMP(refcount_read(&conn->ref), ==, 0);
0358
0359 _net("DESTROY CONN %d", conn->debug_id);
0360
0361 del_timer_sync(&conn->timer);
0362 rxrpc_purge_queue(&conn->rx_queue);
0363
0364 conn->security->clear(conn);
0365 key_put(conn->params.key);
0366 rxrpc_put_bundle(conn->bundle);
0367 rxrpc_put_peer(conn->params.peer);
0368
0369 if (atomic_dec_and_test(&conn->params.local->rxnet->nr_conns))
0370 wake_up_var(&conn->params.local->rxnet->nr_conns);
0371 rxrpc_put_local(conn->params.local);
0372
0373 kfree(conn);
0374 _leave("");
0375 }
0376
0377
0378
0379
0380 void rxrpc_service_connection_reaper(struct work_struct *work)
0381 {
0382 struct rxrpc_connection *conn, *_p;
0383 struct rxrpc_net *rxnet =
0384 container_of(work, struct rxrpc_net, service_conn_reaper);
0385 unsigned long expire_at, earliest, idle_timestamp, now;
0386
0387 LIST_HEAD(graveyard);
0388
0389 _enter("");
0390
0391 now = jiffies;
0392 earliest = now + MAX_JIFFY_OFFSET;
0393
0394 write_lock(&rxnet->conn_lock);
0395 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
0396 ASSERTCMP(refcount_read(&conn->ref), >, 0);
0397 if (likely(refcount_read(&conn->ref) > 1))
0398 continue;
0399 if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
0400 continue;
0401
0402 if (rxnet->live && !conn->params.local->dead) {
0403 idle_timestamp = READ_ONCE(conn->idle_timestamp);
0404 expire_at = idle_timestamp + rxrpc_connection_expiry * HZ;
0405 if (conn->params.local->service_closed)
0406 expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ;
0407
0408 _debug("reap CONN %d { u=%d,t=%ld }",
0409 conn->debug_id, refcount_read(&conn->ref),
0410 (long)expire_at - (long)now);
0411
0412 if (time_before(now, expire_at)) {
0413 if (time_before(expire_at, earliest))
0414 earliest = expire_at;
0415 continue;
0416 }
0417 }
0418
0419
0420
0421
0422 if (!refcount_dec_if_one(&conn->ref))
0423 continue;
0424 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_reap_service, 0, NULL);
0425
0426 if (rxrpc_conn_is_client(conn))
0427 BUG();
0428 else
0429 rxrpc_unpublish_service_conn(conn);
0430
0431 list_move_tail(&conn->link, &graveyard);
0432 }
0433 write_unlock(&rxnet->conn_lock);
0434
0435 if (earliest != now + MAX_JIFFY_OFFSET) {
0436 _debug("reschedule reaper %ld", (long)earliest - (long)now);
0437 ASSERT(time_after(earliest, now));
0438 rxrpc_set_service_reap_timer(rxnet, earliest);
0439 }
0440
0441 while (!list_empty(&graveyard)) {
0442 conn = list_entry(graveyard.next, struct rxrpc_connection,
0443 link);
0444 list_del_init(&conn->link);
0445
0446 ASSERTCMP(refcount_read(&conn->ref), ==, 0);
0447 rxrpc_kill_connection(conn);
0448 }
0449
0450 _leave("");
0451 }
0452
0453
0454
0455
0456
0457 void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
0458 {
0459 struct rxrpc_connection *conn, *_p;
0460 bool leak = false;
0461
0462 _enter("");
0463
0464 atomic_dec(&rxnet->nr_conns);
0465 rxrpc_destroy_all_client_connections(rxnet);
0466
0467 del_timer_sync(&rxnet->service_conn_reap_timer);
0468 rxrpc_queue_work(&rxnet->service_conn_reaper);
0469 flush_workqueue(rxrpc_workqueue);
0470
0471 write_lock(&rxnet->conn_lock);
0472 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
0473 pr_err("AF_RXRPC: Leaked conn %p {%d}\n",
0474 conn, refcount_read(&conn->ref));
0475 leak = true;
0476 }
0477 write_unlock(&rxnet->conn_lock);
0478 BUG_ON(leak);
0479
0480 ASSERT(list_empty(&rxnet->conn_proc_list));
0481
0482
0483
0484
0485 wait_var_event(&rxnet->nr_conns, !atomic_read(&rxnet->nr_conns));
0486 _leave("");
0487 }