1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * NET4: Sysctl interface to net af_unix subsystem.
4 *
5 * Authors: Mike Shaver.
6 */
7
8 #include <linux/slab.h>
9 #include <linux/string.h>
10 #include <linux/sysctl.h>
11 #include <net/af_unix.h>
12 #include <net/net_namespace.h>
13
14 #include "af_unix.h"
15
16 static struct ctl_table unix_table[] = {
17 {
18 .procname = "max_dgram_qlen",
19 .data = &init_net.unx.sysctl_max_dgram_qlen,
20 .maxlen = sizeof(int),
21 .mode = 0644,
22 .proc_handler = proc_dointvec
23 },
24 };
25
unix_sysctl_register(struct net * net)26 int __net_init unix_sysctl_register(struct net *net)
27 {
28 struct ctl_table *table;
29
30 if (net_eq(net, &init_net)) {
31 table = unix_table;
32 } else {
33 table = kmemdup(unix_table, sizeof(unix_table), GFP_KERNEL);
34 if (!table)
35 goto err_alloc;
36
37 table[0].data = &net->unx.sysctl_max_dgram_qlen;
38 }
39
40 net->unx.ctl = register_net_sysctl_sz(net, "net/unix", table,
41 ARRAY_SIZE(unix_table));
42 if (net->unx.ctl == NULL)
43 goto err_reg;
44
45 return 0;
46
47 err_reg:
48 if (!net_eq(net, &init_net))
49 kfree(table);
50 err_alloc:
51 return -ENOMEM;
52 }
53
unix_sysctl_unregister(struct net * net)54 void unix_sysctl_unregister(struct net *net)
55 {
56 const struct ctl_table *table;
57
58 table = net->unx.ctl->ctl_table_arg;
59 unregister_net_sysctl_table(net->unx.ctl);
60 if (!net_eq(net, &init_net))
61 kfree(table);
62 }
63