Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: sysctl.c |
| 3 | * |
| 4 | * Phonet /proc/sys/net/phonet interface implementation |
| 5 | * |
| 6 | * Copyright (C) 2008 Nokia Corporation. |
| 7 | * |
Rémi Denis-Courmont | 31fdc555 | 2012-06-13 22:29:03 +0000 | [diff] [blame] | 8 | * Author: Rémi Denis-Courmont |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * version 2 as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #include <linux/seqlock.h> |
| 26 | #include <linux/sysctl.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/init.h> |
| 29 | |
Rémi Denis-Courmont | 8e052e5 | 2012-04-12 03:39:17 +0000 | [diff] [blame] | 30 | #include <net/sock.h> |
| 31 | #include <linux/phonet.h> |
| 32 | #include <net/phonet/phonet.h> |
| 33 | |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 34 | #define DYNAMIC_PORT_MIN 0x40 |
| 35 | #define DYNAMIC_PORT_MAX 0x7f |
| 36 | |
| 37 | static DEFINE_SEQLOCK(local_port_range_lock); |
| 38 | static int local_port_range_min[2] = {0, 0}; |
| 39 | static int local_port_range_max[2] = {1023, 1023}; |
| 40 | static int local_port_range[2] = {DYNAMIC_PORT_MIN, DYNAMIC_PORT_MAX}; |
| 41 | static struct ctl_table_header *phonet_table_hrd; |
| 42 | |
| 43 | static void set_local_port_range(int range[2]) |
| 44 | { |
| 45 | write_seqlock(&local_port_range_lock); |
| 46 | local_port_range[0] = range[0]; |
| 47 | local_port_range[1] = range[1]; |
| 48 | write_sequnlock(&local_port_range_lock); |
| 49 | } |
| 50 | |
| 51 | void phonet_get_local_port_range(int *min, int *max) |
| 52 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 53 | unsigned int seq; |
| 54 | |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 55 | do { |
| 56 | seq = read_seqbegin(&local_port_range_lock); |
| 57 | if (min) |
| 58 | *min = local_port_range[0]; |
| 59 | if (max) |
| 60 | *max = local_port_range[1]; |
| 61 | } while (read_seqretry(&local_port_range_lock, seq)); |
| 62 | } |
| 63 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 64 | static int proc_local_port_range(struct ctl_table *table, int write, |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 65 | void __user *buffer, |
| 66 | size_t *lenp, loff_t *ppos) |
| 67 | { |
| 68 | int ret; |
| 69 | int range[2] = {local_port_range[0], local_port_range[1]}; |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 70 | struct ctl_table tmp = { |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 71 | .data = &range, |
| 72 | .maxlen = sizeof(range), |
| 73 | .mode = table->mode, |
| 74 | .extra1 = &local_port_range_min, |
| 75 | .extra2 = &local_port_range_max, |
| 76 | }; |
| 77 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 78 | ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 79 | |
| 80 | if (write && ret == 0) { |
| 81 | if (range[1] < range[0]) |
| 82 | ret = -EINVAL; |
| 83 | else |
| 84 | set_local_port_range(range); |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | static struct ctl_table phonet_table[] = { |
| 91 | { |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 92 | .procname = "local_port_range", |
| 93 | .data = &local_port_range, |
| 94 | .maxlen = sizeof(local_port_range), |
| 95 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 96 | .proc_handler = proc_local_port_range, |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 97 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 98 | { } |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 101 | int __init phonet_sysctl_init(void) |
| 102 | { |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 103 | phonet_table_hrd = register_net_sysctl(&init_net, "net/phonet", phonet_table); |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 104 | return phonet_table_hrd == NULL ? -ENOMEM : 0; |
| 105 | } |
| 106 | |
| 107 | void phonet_sysctl_exit(void) |
| 108 | { |
Eric W. Biederman | 5dd3df1 | 2012-04-19 13:24:33 +0000 | [diff] [blame] | 109 | unregister_net_sysctl_table(phonet_table_hrd); |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 110 | } |