GNU Linux-libre 4.14.266-gnu1
[releases.git] / net / rxrpc / net_ns.c
1 /* rxrpc network namespace handling.
2  *
3  * Copyright (C) 2017 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 Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11
12 #include <linux/proc_fs.h>
13 #include "ar-internal.h"
14
15 unsigned int rxrpc_net_id;
16
17 /*
18  * Initialise a per-network namespace record.
19  */
20 static __net_init int rxrpc_init_net(struct net *net)
21 {
22         struct rxrpc_net *rxnet = rxrpc_net(net);
23         int ret;
24
25         rxnet->live = true;
26         get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
27         rxnet->epoch |= RXRPC_RANDOM_EPOCH;
28
29         INIT_LIST_HEAD(&rxnet->calls);
30         rwlock_init(&rxnet->call_lock);
31
32         INIT_LIST_HEAD(&rxnet->conn_proc_list);
33         INIT_LIST_HEAD(&rxnet->service_conns);
34         rwlock_init(&rxnet->conn_lock);
35         INIT_DELAYED_WORK(&rxnet->service_conn_reaper,
36                           rxrpc_service_connection_reaper);
37
38         rxnet->nr_client_conns = 0;
39         rxnet->nr_active_client_conns = 0;
40         rxnet->kill_all_client_conns = false;
41         spin_lock_init(&rxnet->client_conn_cache_lock);
42         spin_lock_init(&rxnet->client_conn_discard_lock);
43         INIT_LIST_HEAD(&rxnet->waiting_client_conns);
44         INIT_LIST_HEAD(&rxnet->active_client_conns);
45         INIT_LIST_HEAD(&rxnet->idle_client_conns);
46         INIT_DELAYED_WORK(&rxnet->client_conn_reaper,
47                           rxrpc_discard_expired_client_conns);
48
49         INIT_LIST_HEAD(&rxnet->local_endpoints);
50         mutex_init(&rxnet->local_mutex);
51         hash_init(rxnet->peer_hash);
52         spin_lock_init(&rxnet->peer_hash_lock);
53
54         ret = -ENOMEM;
55         rxnet->proc_net = proc_net_mkdir(net, "rxrpc", net->proc_net);
56         if (!rxnet->proc_net)
57                 goto err_proc;
58
59         proc_create("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_fops);
60         proc_create("conns", 0444, rxnet->proc_net, &rxrpc_connection_seq_fops);
61         return 0;
62
63 err_proc:
64         rxnet->live = false;
65         return ret;
66 }
67
68 /*
69  * Clean up a per-network namespace record.
70  */
71 static __net_exit void rxrpc_exit_net(struct net *net)
72 {
73         struct rxrpc_net *rxnet = rxrpc_net(net);
74
75         rxnet->live = false;
76         rxrpc_destroy_all_calls(rxnet);
77         rxrpc_destroy_all_connections(rxnet);
78         rxrpc_destroy_all_locals(rxnet);
79         proc_remove(rxnet->proc_net);
80 }
81
82 struct pernet_operations rxrpc_net_ops = {
83         .init   = rxrpc_init_net,
84         .exit   = rxrpc_exit_net,
85         .id     = &rxrpc_net_id,
86         .size   = sizeof(struct rxrpc_net),
87 };