Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Multipath TCP |
| 3 | * |
| 4 | * Copyright (c) 2019, Tessares SA. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/sysctl.h> |
| 8 | |
| 9 | #include <net/net_namespace.h> |
| 10 | #include <net/netns/generic.h> |
| 11 | |
| 12 | #include "protocol.h" |
| 13 | |
| 14 | #define MPTCP_SYSCTL_PATH "net/mptcp" |
| 15 | |
| 16 | static int mptcp_pernet_id; |
| 17 | struct mptcp_pernet { |
| 18 | struct ctl_table_header *ctl_table_hdr; |
| 19 | |
| 20 | int mptcp_enabled; |
Geliang Tang | 93f323b | 2020-11-03 11:05:07 -0800 | [diff] [blame] | 21 | unsigned int add_addr_timeout; |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | static struct mptcp_pernet *mptcp_get_pernet(struct net *net) |
| 25 | { |
| 26 | return net_generic(net, mptcp_pernet_id); |
| 27 | } |
| 28 | |
| 29 | int mptcp_is_enabled(struct net *net) |
| 30 | { |
| 31 | return mptcp_get_pernet(net)->mptcp_enabled; |
| 32 | } |
| 33 | |
Geliang Tang | 93f323b | 2020-11-03 11:05:07 -0800 | [diff] [blame] | 34 | unsigned int mptcp_get_add_addr_timeout(struct net *net) |
| 35 | { |
| 36 | return mptcp_get_pernet(net)->add_addr_timeout; |
| 37 | } |
| 38 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 39 | static struct ctl_table mptcp_sysctl_table[] = { |
| 40 | { |
| 41 | .procname = "enabled", |
| 42 | .maxlen = sizeof(int), |
| 43 | .mode = 0644, |
| 44 | /* users with CAP_NET_ADMIN or root (not and) can change this |
| 45 | * value, same as other sysctl or the 'net' tree. |
| 46 | */ |
| 47 | .proc_handler = proc_dointvec, |
| 48 | }, |
Geliang Tang | 93f323b | 2020-11-03 11:05:07 -0800 | [diff] [blame] | 49 | { |
| 50 | .procname = "add_addr_timeout", |
| 51 | .maxlen = sizeof(unsigned int), |
| 52 | .mode = 0644, |
| 53 | .proc_handler = proc_dointvec_jiffies, |
| 54 | }, |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 55 | {} |
| 56 | }; |
| 57 | |
| 58 | static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet) |
| 59 | { |
| 60 | pernet->mptcp_enabled = 1; |
Geliang Tang | 93f323b | 2020-11-03 11:05:07 -0800 | [diff] [blame] | 61 | pernet->add_addr_timeout = TCP_RTO_MAX; |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet) |
| 65 | { |
| 66 | struct ctl_table_header *hdr; |
| 67 | struct ctl_table *table; |
| 68 | |
| 69 | table = mptcp_sysctl_table; |
| 70 | if (!net_eq(net, &init_net)) { |
| 71 | table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL); |
| 72 | if (!table) |
| 73 | goto err_alloc; |
| 74 | } |
| 75 | |
| 76 | table[0].data = &pernet->mptcp_enabled; |
Geliang Tang | 93f323b | 2020-11-03 11:05:07 -0800 | [diff] [blame] | 77 | table[1].data = &pernet->add_addr_timeout; |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 78 | |
| 79 | hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table); |
| 80 | if (!hdr) |
| 81 | goto err_reg; |
| 82 | |
| 83 | pernet->ctl_table_hdr = hdr; |
| 84 | |
| 85 | return 0; |
| 86 | |
| 87 | err_reg: |
| 88 | if (!net_eq(net, &init_net)) |
| 89 | kfree(table); |
| 90 | err_alloc: |
| 91 | return -ENOMEM; |
| 92 | } |
| 93 | |
| 94 | static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) |
| 95 | { |
| 96 | struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg; |
| 97 | |
| 98 | unregister_net_sysctl_table(pernet->ctl_table_hdr); |
| 99 | |
| 100 | kfree(table); |
| 101 | } |
| 102 | |
| 103 | static int __net_init mptcp_net_init(struct net *net) |
| 104 | { |
| 105 | struct mptcp_pernet *pernet = mptcp_get_pernet(net); |
| 106 | |
| 107 | mptcp_pernet_set_defaults(pernet); |
| 108 | |
| 109 | return mptcp_pernet_new_table(net, pernet); |
| 110 | } |
| 111 | |
| 112 | /* Note: the callback will only be called per extra netns */ |
| 113 | static void __net_exit mptcp_net_exit(struct net *net) |
| 114 | { |
| 115 | struct mptcp_pernet *pernet = mptcp_get_pernet(net); |
| 116 | |
| 117 | mptcp_pernet_del_table(pernet); |
| 118 | } |
| 119 | |
| 120 | static struct pernet_operations mptcp_pernet_ops = { |
| 121 | .init = mptcp_net_init, |
| 122 | .exit = mptcp_net_exit, |
| 123 | .id = &mptcp_pernet_id, |
| 124 | .size = sizeof(struct mptcp_pernet), |
| 125 | }; |
| 126 | |
| 127 | void __init mptcp_init(void) |
| 128 | { |
Florian Westphal | 9466a1c | 2020-07-30 21:25:56 +0200 | [diff] [blame] | 129 | mptcp_join_cookie_init(); |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 130 | mptcp_proto_init(); |
| 131 | |
| 132 | if (register_pernet_subsys(&mptcp_pernet_ops) < 0) |
| 133 | panic("Failed to register MPTCP pernet subsystem.\n"); |
| 134 | } |
| 135 | |
| 136 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 137 | int __init mptcpv6_init(void) |
| 138 | { |
| 139 | int err; |
| 140 | |
| 141 | err = mptcp_proto_v6_init(); |
| 142 | |
| 143 | return err; |
| 144 | } |
| 145 | #endif |