Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Local endpoint object management
0003  *
0004  * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/module.h>
0011 #include <linux/net.h>
0012 #include <linux/skbuff.h>
0013 #include <linux/slab.h>
0014 #include <linux/udp.h>
0015 #include <linux/ip.h>
0016 #include <linux/hashtable.h>
0017 #include <net/sock.h>
0018 #include <net/udp.h>
0019 #include <net/udp_tunnel.h>
0020 #include <net/af_rxrpc.h>
0021 #include "ar-internal.h"
0022 
0023 static void rxrpc_local_processor(struct work_struct *);
0024 static void rxrpc_local_rcu(struct rcu_head *);
0025 
0026 /*
0027  * Compare a local to an address.  Return -ve, 0 or +ve to indicate less than,
0028  * same or greater than.
0029  *
0030  * We explicitly don't compare the RxRPC service ID as we want to reject
0031  * conflicting uses by differing services.  Further, we don't want to share
0032  * addresses with different options (IPv6), so we don't compare those bits
0033  * either.
0034  */
0035 static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
0036                 const struct sockaddr_rxrpc *srx)
0037 {
0038     long diff;
0039 
0040     diff = ((local->srx.transport_type - srx->transport_type) ?:
0041         (local->srx.transport_len - srx->transport_len) ?:
0042         (local->srx.transport.family - srx->transport.family));
0043     if (diff != 0)
0044         return diff;
0045 
0046     switch (srx->transport.family) {
0047     case AF_INET:
0048         /* If the choice of UDP port is left up to the transport, then
0049          * the endpoint record doesn't match.
0050          */
0051         return ((u16 __force)local->srx.transport.sin.sin_port -
0052             (u16 __force)srx->transport.sin.sin_port) ?:
0053             memcmp(&local->srx.transport.sin.sin_addr,
0054                    &srx->transport.sin.sin_addr,
0055                    sizeof(struct in_addr));
0056 #ifdef CONFIG_AF_RXRPC_IPV6
0057     case AF_INET6:
0058         /* If the choice of UDP6 port is left up to the transport, then
0059          * the endpoint record doesn't match.
0060          */
0061         return ((u16 __force)local->srx.transport.sin6.sin6_port -
0062             (u16 __force)srx->transport.sin6.sin6_port) ?:
0063             memcmp(&local->srx.transport.sin6.sin6_addr,
0064                    &srx->transport.sin6.sin6_addr,
0065                    sizeof(struct in6_addr));
0066 #endif
0067     default:
0068         BUG();
0069     }
0070 }
0071 
0072 /*
0073  * Allocate a new local endpoint.
0074  */
0075 static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
0076                          const struct sockaddr_rxrpc *srx)
0077 {
0078     struct rxrpc_local *local;
0079 
0080     local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
0081     if (local) {
0082         refcount_set(&local->ref, 1);
0083         atomic_set(&local->active_users, 1);
0084         local->rxnet = rxnet;
0085         INIT_HLIST_NODE(&local->link);
0086         INIT_WORK(&local->processor, rxrpc_local_processor);
0087         init_rwsem(&local->defrag_sem);
0088         skb_queue_head_init(&local->reject_queue);
0089         skb_queue_head_init(&local->event_queue);
0090         local->client_bundles = RB_ROOT;
0091         spin_lock_init(&local->client_bundles_lock);
0092         spin_lock_init(&local->lock);
0093         rwlock_init(&local->services_lock);
0094         local->debug_id = atomic_inc_return(&rxrpc_debug_id);
0095         memcpy(&local->srx, srx, sizeof(*srx));
0096         local->srx.srx_service = 0;
0097         trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
0098     }
0099 
0100     _leave(" = %p", local);
0101     return local;
0102 }
0103 
0104 /*
0105  * create the local socket
0106  * - must be called with rxrpc_local_mutex locked
0107  */
0108 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
0109 {
0110     struct udp_tunnel_sock_cfg tuncfg = {NULL};
0111     struct sockaddr_rxrpc *srx = &local->srx;
0112     struct udp_port_cfg udp_conf = {0};
0113     struct sock *usk;
0114     int ret;
0115 
0116     _enter("%p{%d,%d}",
0117            local, srx->transport_type, srx->transport.family);
0118 
0119     udp_conf.family = srx->transport.family;
0120     udp_conf.use_udp_checksums = true;
0121     if (udp_conf.family == AF_INET) {
0122         udp_conf.local_ip = srx->transport.sin.sin_addr;
0123         udp_conf.local_udp_port = srx->transport.sin.sin_port;
0124 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
0125     } else {
0126         udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
0127         udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
0128         udp_conf.use_udp6_tx_checksums = true;
0129         udp_conf.use_udp6_rx_checksums = true;
0130 #endif
0131     }
0132     ret = udp_sock_create(net, &udp_conf, &local->socket);
0133     if (ret < 0) {
0134         _leave(" = %d [socket]", ret);
0135         return ret;
0136     }
0137 
0138     tuncfg.encap_type = UDP_ENCAP_RXRPC;
0139     tuncfg.encap_rcv = rxrpc_input_packet;
0140     tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
0141     tuncfg.sk_user_data = local;
0142     setup_udp_tunnel_sock(net, local->socket, &tuncfg);
0143 
0144     /* set the socket up */
0145     usk = local->socket->sk;
0146     usk->sk_error_report = rxrpc_error_report;
0147 
0148     switch (srx->transport.family) {
0149     case AF_INET6:
0150         /* we want to receive ICMPv6 errors */
0151         ip6_sock_set_recverr(usk);
0152 
0153         /* Fall through and set IPv4 options too otherwise we don't get
0154          * errors from IPv4 packets sent through the IPv6 socket.
0155          */
0156         fallthrough;
0157     case AF_INET:
0158         /* we want to receive ICMP errors */
0159         ip_sock_set_recverr(usk);
0160 
0161         /* we want to set the don't fragment bit */
0162         ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
0163 
0164         /* We want receive timestamps. */
0165         sock_enable_timestamps(usk);
0166         break;
0167 
0168     default:
0169         BUG();
0170     }
0171 
0172     _leave(" = 0");
0173     return 0;
0174 }
0175 
0176 /*
0177  * Look up or create a new local endpoint using the specified local address.
0178  */
0179 struct rxrpc_local *rxrpc_lookup_local(struct net *net,
0180                        const struct sockaddr_rxrpc *srx)
0181 {
0182     struct rxrpc_local *local;
0183     struct rxrpc_net *rxnet = rxrpc_net(net);
0184     struct hlist_node *cursor;
0185     const char *age;
0186     long diff;
0187     int ret;
0188 
0189     _enter("{%d,%d,%pISp}",
0190            srx->transport_type, srx->transport.family, &srx->transport);
0191 
0192     mutex_lock(&rxnet->local_mutex);
0193 
0194     hlist_for_each(cursor, &rxnet->local_endpoints) {
0195         local = hlist_entry(cursor, struct rxrpc_local, link);
0196 
0197         diff = rxrpc_local_cmp_key(local, srx);
0198         if (diff != 0)
0199             continue;
0200 
0201         /* Services aren't allowed to share transport sockets, so
0202          * reject that here.  It is possible that the object is dying -
0203          * but it may also still have the local transport address that
0204          * we want bound.
0205          */
0206         if (srx->srx_service) {
0207             local = NULL;
0208             goto addr_in_use;
0209         }
0210 
0211         /* Found a match.  We want to replace a dying object.
0212          * Attempting to bind the transport socket may still fail if
0213          * we're attempting to use a local address that the dying
0214          * object is still using.
0215          */
0216         if (!rxrpc_use_local(local))
0217             break;
0218 
0219         age = "old";
0220         goto found;
0221     }
0222 
0223     local = rxrpc_alloc_local(rxnet, srx);
0224     if (!local)
0225         goto nomem;
0226 
0227     ret = rxrpc_open_socket(local, net);
0228     if (ret < 0)
0229         goto sock_error;
0230 
0231     if (cursor) {
0232         hlist_replace_rcu(cursor, &local->link);
0233         cursor->pprev = NULL;
0234     } else {
0235         hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
0236     }
0237     age = "new";
0238 
0239 found:
0240     mutex_unlock(&rxnet->local_mutex);
0241 
0242     _net("LOCAL %s %d {%pISp}",
0243          age, local->debug_id, &local->srx.transport);
0244 
0245     _leave(" = %p", local);
0246     return local;
0247 
0248 nomem:
0249     ret = -ENOMEM;
0250 sock_error:
0251     mutex_unlock(&rxnet->local_mutex);
0252     if (local)
0253         call_rcu(&local->rcu, rxrpc_local_rcu);
0254     _leave(" = %d", ret);
0255     return ERR_PTR(ret);
0256 
0257 addr_in_use:
0258     mutex_unlock(&rxnet->local_mutex);
0259     _leave(" = -EADDRINUSE");
0260     return ERR_PTR(-EADDRINUSE);
0261 }
0262 
0263 /*
0264  * Get a ref on a local endpoint.
0265  */
0266 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
0267 {
0268     const void *here = __builtin_return_address(0);
0269     int r;
0270 
0271     __refcount_inc(&local->ref, &r);
0272     trace_rxrpc_local(local->debug_id, rxrpc_local_got, r + 1, here);
0273     return local;
0274 }
0275 
0276 /*
0277  * Get a ref on a local endpoint unless its usage has already reached 0.
0278  */
0279 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
0280 {
0281     const void *here = __builtin_return_address(0);
0282     int r;
0283 
0284     if (local) {
0285         if (__refcount_inc_not_zero(&local->ref, &r))
0286             trace_rxrpc_local(local->debug_id, rxrpc_local_got,
0287                       r + 1, here);
0288         else
0289             local = NULL;
0290     }
0291     return local;
0292 }
0293 
0294 /*
0295  * Queue a local endpoint and pass the caller's reference to the work item.
0296  */
0297 void rxrpc_queue_local(struct rxrpc_local *local)
0298 {
0299     const void *here = __builtin_return_address(0);
0300     unsigned int debug_id = local->debug_id;
0301     int r = refcount_read(&local->ref);
0302 
0303     if (rxrpc_queue_work(&local->processor))
0304         trace_rxrpc_local(debug_id, rxrpc_local_queued, r + 1, here);
0305     else
0306         rxrpc_put_local(local);
0307 }
0308 
0309 /*
0310  * Drop a ref on a local endpoint.
0311  */
0312 void rxrpc_put_local(struct rxrpc_local *local)
0313 {
0314     const void *here = __builtin_return_address(0);
0315     unsigned int debug_id;
0316     bool dead;
0317     int r;
0318 
0319     if (local) {
0320         debug_id = local->debug_id;
0321 
0322         dead = __refcount_dec_and_test(&local->ref, &r);
0323         trace_rxrpc_local(debug_id, rxrpc_local_put, r, here);
0324 
0325         if (dead)
0326             call_rcu(&local->rcu, rxrpc_local_rcu);
0327     }
0328 }
0329 
0330 /*
0331  * Start using a local endpoint.
0332  */
0333 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
0334 {
0335     local = rxrpc_get_local_maybe(local);
0336     if (!local)
0337         return NULL;
0338 
0339     if (!__rxrpc_use_local(local)) {
0340         rxrpc_put_local(local);
0341         return NULL;
0342     }
0343 
0344     return local;
0345 }
0346 
0347 /*
0348  * Cease using a local endpoint.  Once the number of active users reaches 0, we
0349  * start the closure of the transport in the work processor.
0350  */
0351 void rxrpc_unuse_local(struct rxrpc_local *local)
0352 {
0353     if (local) {
0354         if (__rxrpc_unuse_local(local)) {
0355             rxrpc_get_local(local);
0356             rxrpc_queue_local(local);
0357         }
0358     }
0359 }
0360 
0361 /*
0362  * Destroy a local endpoint's socket and then hand the record to RCU to dispose
0363  * of.
0364  *
0365  * Closing the socket cannot be done from bottom half context or RCU callback
0366  * context because it might sleep.
0367  */
0368 static void rxrpc_local_destroyer(struct rxrpc_local *local)
0369 {
0370     struct socket *socket = local->socket;
0371     struct rxrpc_net *rxnet = local->rxnet;
0372 
0373     _enter("%d", local->debug_id);
0374 
0375     local->dead = true;
0376 
0377     mutex_lock(&rxnet->local_mutex);
0378     hlist_del_init_rcu(&local->link);
0379     mutex_unlock(&rxnet->local_mutex);
0380 
0381     rxrpc_clean_up_local_conns(local);
0382     rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
0383     ASSERT(!local->service);
0384 
0385     if (socket) {
0386         local->socket = NULL;
0387         kernel_sock_shutdown(socket, SHUT_RDWR);
0388         socket->sk->sk_user_data = NULL;
0389         sock_release(socket);
0390     }
0391 
0392     /* At this point, there should be no more packets coming in to the
0393      * local endpoint.
0394      */
0395     rxrpc_purge_queue(&local->reject_queue);
0396     rxrpc_purge_queue(&local->event_queue);
0397 }
0398 
0399 /*
0400  * Process events on an endpoint.  The work item carries a ref which
0401  * we must release.
0402  */
0403 static void rxrpc_local_processor(struct work_struct *work)
0404 {
0405     struct rxrpc_local *local =
0406         container_of(work, struct rxrpc_local, processor);
0407     bool again;
0408 
0409     if (local->dead)
0410         return;
0411 
0412     trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
0413               refcount_read(&local->ref), NULL);
0414 
0415     do {
0416         again = false;
0417         if (!__rxrpc_use_local(local)) {
0418             rxrpc_local_destroyer(local);
0419             break;
0420         }
0421 
0422         if (!skb_queue_empty(&local->reject_queue)) {
0423             rxrpc_reject_packets(local);
0424             again = true;
0425         }
0426 
0427         if (!skb_queue_empty(&local->event_queue)) {
0428             rxrpc_process_local_events(local);
0429             again = true;
0430         }
0431 
0432         __rxrpc_unuse_local(local);
0433     } while (again);
0434 
0435     rxrpc_put_local(local);
0436 }
0437 
0438 /*
0439  * Destroy a local endpoint after the RCU grace period expires.
0440  */
0441 static void rxrpc_local_rcu(struct rcu_head *rcu)
0442 {
0443     struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
0444 
0445     _enter("%d", local->debug_id);
0446 
0447     ASSERT(!work_pending(&local->processor));
0448 
0449     _net("DESTROY LOCAL %d", local->debug_id);
0450     kfree(local);
0451     _leave("");
0452 }
0453 
0454 /*
0455  * Verify the local endpoint list is empty by this point.
0456  */
0457 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
0458 {
0459     struct rxrpc_local *local;
0460 
0461     _enter("");
0462 
0463     flush_workqueue(rxrpc_workqueue);
0464 
0465     if (!hlist_empty(&rxnet->local_endpoints)) {
0466         mutex_lock(&rxnet->local_mutex);
0467         hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
0468             pr_err("AF_RXRPC: Leaked local %p {%d}\n",
0469                    local, refcount_read(&local->ref));
0470         }
0471         mutex_unlock(&rxnet->local_mutex);
0472         BUG();
0473     }
0474 }