xref: /openbmc/linux/net/unix/sysctl_net_unix.c (revision 8ab59da2)
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/mm.h>
9 #include <linux/slab.h>
10 #include <linux/sysctl.h>
11 
12 #include <net/af_unix.h>
13 
14 static struct ctl_table unix_table[] = {
15 	{
16 		.procname	= "max_dgram_qlen",
17 		.data		= &init_net.unx.sysctl_max_dgram_qlen,
18 		.maxlen		= sizeof(int),
19 		.mode		= 0644,
20 		.proc_handler	= proc_dointvec
21 	},
22 	{ }
23 };
24 
25 int __net_init unix_sysctl_register(struct net *net)
26 {
27 	struct ctl_table *table;
28 
29 	if (net_eq(net, &init_net)) {
30 		table = unix_table;
31 	} else {
32 		table = kmemdup(unix_table, sizeof(unix_table), GFP_KERNEL);
33 		if (!table)
34 			goto err_alloc;
35 
36 		table[0].data = &net->unx.sysctl_max_dgram_qlen;
37 	}
38 
39 	net->unx.ctl = register_net_sysctl(net, "net/unix", table);
40 	if (net->unx.ctl == NULL)
41 		goto err_reg;
42 
43 	return 0;
44 
45 err_reg:
46 	if (!net_eq(net, &init_net))
47 		kfree(table);
48 err_alloc:
49 	return -ENOMEM;
50 }
51 
52 void unix_sysctl_unregister(struct net *net)
53 {
54 	struct ctl_table *table;
55 
56 	table = net->unx.ctl->ctl_table_arg;
57 	unregister_net_sysctl_table(net->unx.ctl);
58 	if (!net_eq(net, &init_net))
59 		kfree(table);
60 }
61