0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/seqlock.h>
0013 #include <linux/sysctl.h>
0014 #include <linux/errno.h>
0015 #include <linux/init.h>
0016
0017 #include <net/sock.h>
0018 #include <linux/phonet.h>
0019 #include <net/phonet/phonet.h>
0020
0021 #define DYNAMIC_PORT_MIN 0x40
0022 #define DYNAMIC_PORT_MAX 0x7f
0023
0024 static DEFINE_SEQLOCK(local_port_range_lock);
0025 static int local_port_range_min[2] = {0, 0};
0026 static int local_port_range_max[2] = {1023, 1023};
0027 static int local_port_range[2] = {DYNAMIC_PORT_MIN, DYNAMIC_PORT_MAX};
0028 static struct ctl_table_header *phonet_table_hrd;
0029
0030 static void set_local_port_range(int range[2])
0031 {
0032 write_seqlock(&local_port_range_lock);
0033 local_port_range[0] = range[0];
0034 local_port_range[1] = range[1];
0035 write_sequnlock(&local_port_range_lock);
0036 }
0037
0038 void phonet_get_local_port_range(int *min, int *max)
0039 {
0040 unsigned int seq;
0041
0042 do {
0043 seq = read_seqbegin(&local_port_range_lock);
0044 if (min)
0045 *min = local_port_range[0];
0046 if (max)
0047 *max = local_port_range[1];
0048 } while (read_seqretry(&local_port_range_lock, seq));
0049 }
0050
0051 static int proc_local_port_range(struct ctl_table *table, int write,
0052 void *buffer, size_t *lenp, loff_t *ppos)
0053 {
0054 int ret;
0055 int range[2] = {local_port_range[0], local_port_range[1]};
0056 struct ctl_table tmp = {
0057 .data = &range,
0058 .maxlen = sizeof(range),
0059 .mode = table->mode,
0060 .extra1 = &local_port_range_min,
0061 .extra2 = &local_port_range_max,
0062 };
0063
0064 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
0065
0066 if (write && ret == 0) {
0067 if (range[1] < range[0])
0068 ret = -EINVAL;
0069 else
0070 set_local_port_range(range);
0071 }
0072
0073 return ret;
0074 }
0075
0076 static struct ctl_table phonet_table[] = {
0077 {
0078 .procname = "local_port_range",
0079 .data = &local_port_range,
0080 .maxlen = sizeof(local_port_range),
0081 .mode = 0644,
0082 .proc_handler = proc_local_port_range,
0083 },
0084 { }
0085 };
0086
0087 int __init phonet_sysctl_init(void)
0088 {
0089 phonet_table_hrd = register_net_sysctl(&init_net, "net/phonet", phonet_table);
0090 return phonet_table_hrd == NULL ? -ENOMEM : 0;
0091 }
0092
0093 void phonet_sysctl_exit(void)
0094 {
0095 unregister_net_sysctl_table(phonet_table_hrd);
0096 }