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