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