xref: /linux/net/unix/sysctl_net_unix.c (revision c36461825469a9ceee2346a2e89286c522525da7)
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 const 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 
26 static const struct ctl_table *unix_table_dup(struct net *net)
27 {
28 	struct ctl_table *table;
29 
30 	table = kmemdup(unix_table, sizeof(unix_table), GFP_KERNEL);
31 	if (!table)
32 		return NULL;
33 
34 	table[0].data = &net->unx.sysctl_max_dgram_qlen;
35 
36 	return table;
37 }
38 
39 int __net_init unix_sysctl_register(struct net *net)
40 {
41 	const struct ctl_table *table;
42 
43 	if (net_eq(net, &init_net)) {
44 		table = unix_table;
45 	} else {
46 		table = unix_table_dup(net);
47 		if (!table)
48 			goto err_alloc;
49 	}
50 
51 	net->unx.ctl = register_net_sysctl_sz(net, "net/unix", table,
52 					      ARRAY_SIZE(unix_table));
53 	if (net->unx.ctl == NULL)
54 		goto err_reg;
55 
56 	return 0;
57 
58 err_reg:
59 	if (!net_eq(net, &init_net))
60 		kfree(table);
61 err_alloc:
62 	return -ENOMEM;
63 }
64 
65 void unix_sysctl_unregister(struct net *net)
66 {
67 	const struct ctl_table *table;
68 
69 	table = net->unx.ctl->ctl_table_arg;
70 	unregister_net_sysctl_table(net->unx.ctl);
71 	if (!net_eq(net, &init_net))
72 		kfree(table);
73 }
74