Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * File: sysctl.c |
| 4 | * |
| 5 | * Phonet /proc/sys/net/phonet interface implementation |
| 6 | * |
| 7 | * Copyright (C) 2008 Nokia Corporation. |
| 8 | * |
Rémi Denis-Courmont | 31fdc55 | 2012-06-13 22:29:03 +0000 | [diff] [blame] | 9 | * Author: Rémi Denis-Courmont |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/seqlock.h> |
| 13 | #include <linux/sysctl.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/init.h> |
| 16 | |
Rémi Denis-Courmont | 8e052e5 | 2012-04-12 03:39:17 +0000 | [diff] [blame] | 17 | #include <net/sock.h> |
| 18 | #include <linux/phonet.h> |
| 19 | #include <net/phonet/phonet.h> |
| 20 | |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 21 | #define DYNAMIC_PORT_MIN 0x40 |
| 22 | #define DYNAMIC_PORT_MAX 0x7f |
| 23 | |
| 24 | static DEFINE_SEQLOCK(local_port_range_lock); |
| 25 | static int local_port_range_min[2] = {0, 0}; |
| 26 | static int local_port_range_max[2] = {1023, 1023}; |
| 27 | static int local_port_range[2] = {DYNAMIC_PORT_MIN, DYNAMIC_PORT_MAX}; |
| 28 | static struct ctl_table_header *phonet_table_hrd; |
| 29 | |
| 30 | static void set_local_port_range(int range[2]) |
| 31 | { |
| 32 | write_seqlock(&local_port_range_lock); |
| 33 | local_port_range[0] = range[0]; |
| 34 | local_port_range[1] = range[1]; |
| 35 | write_sequnlock(&local_port_range_lock); |
| 36 | } |
| 37 | |
| 38 | void phonet_get_local_port_range(int *min, int *max) |
| 39 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 40 | unsigned int seq; |
| 41 | |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 42 | do { |
| 43 | seq = read_seqbegin(&local_port_range_lock); |
| 44 | if (min) |
| 45 | *min = local_port_range[0]; |
| 46 | if (max) |
| 47 | *max = local_port_range[1]; |
| 48 | } while (read_seqretry(&local_port_range_lock, seq)); |
| 49 | } |
| 50 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 51 | 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] | 52 | void __user *buffer, |
| 53 | size_t *lenp, loff_t *ppos) |
| 54 | { |
| 55 | int ret; |
| 56 | int range[2] = {local_port_range[0], local_port_range[1]}; |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 57 | struct ctl_table tmp = { |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 58 | .data = &range, |
| 59 | .maxlen = sizeof(range), |
| 60 | .mode = table->mode, |
| 61 | .extra1 = &local_port_range_min, |
| 62 | .extra2 = &local_port_range_max, |
| 63 | }; |
| 64 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 65 | ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 66 | |
| 67 | if (write && ret == 0) { |
| 68 | if (range[1] < range[0]) |
| 69 | ret = -EINVAL; |
| 70 | else |
| 71 | set_local_port_range(range); |
| 72 | } |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | static struct ctl_table phonet_table[] = { |
| 78 | { |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 79 | .procname = "local_port_range", |
| 80 | .data = &local_port_range, |
| 81 | .maxlen = sizeof(local_port_range), |
| 82 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 83 | .proc_handler = proc_local_port_range, |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 84 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 85 | { } |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 88 | int __init phonet_sysctl_init(void) |
| 89 | { |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 90 | 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] | 91 | return phonet_table_hrd == NULL ? -ENOMEM : 0; |
| 92 | } |
| 93 | |
| 94 | void phonet_sysctl_exit(void) |
| 95 | { |
Eric W. Biederman | 5dd3df1 | 2012-04-19 13:24:33 +0000 | [diff] [blame] | 96 | unregister_net_sysctl_table(phonet_table_hrd); |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 97 | } |