linux/net/core/sysctl_net_core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* -*- linux-c -*-
   3 * sysctl_net_core.c: sysctl interface to net core subsystem.
   4 *
   5 * Begun April 1, 1996, Mike Shaver.
   6 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
   7 */
   8
   9#include <linux/mm.h>
  10#include <linux/sysctl.h>
  11#include <linux/module.h>
  12#include <linux/socket.h>
  13#include <linux/netdevice.h>
  14#include <linux/ratelimit.h>
  15#include <linux/vmalloc.h>
  16#include <linux/init.h>
  17#include <linux/slab.h>
  18
  19#include <net/ip.h>
  20#include <net/sock.h>
  21#include <net/net_ratelimit.h>
  22#include <net/busy_poll.h>
  23#include <net/pkt_sched.h>
  24
  25static int two __maybe_unused = 2;
  26static int min_sndbuf = SOCK_MIN_SNDBUF;
  27static int min_rcvbuf = SOCK_MIN_RCVBUF;
  28static int max_skb_frags = MAX_SKB_FRAGS;
  29static long long_one __maybe_unused = 1;
  30static long long_max __maybe_unused = LONG_MAX;
  31
  32static int net_msg_warn;        /* Unused, but still a sysctl */
  33
  34int sysctl_fb_tunnels_only_for_init_net __read_mostly = 0;
  35EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net);
  36
  37#ifdef CONFIG_RPS
  38static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
  39                                void __user *buffer, size_t *lenp, loff_t *ppos)
  40{
  41        unsigned int orig_size, size;
  42        int ret, i;
  43        struct ctl_table tmp = {
  44                .data = &size,
  45                .maxlen = sizeof(size),
  46                .mode = table->mode
  47        };
  48        struct rps_sock_flow_table *orig_sock_table, *sock_table;
  49        static DEFINE_MUTEX(sock_flow_mutex);
  50
  51        mutex_lock(&sock_flow_mutex);
  52
  53        orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
  54                                        lockdep_is_held(&sock_flow_mutex));
  55        size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
  56
  57        ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
  58
  59        if (write) {
  60                if (size) {
  61                        if (size > 1<<29) {
  62                                /* Enforce limit to prevent overflow */
  63                                mutex_unlock(&sock_flow_mutex);
  64                                return -EINVAL;
  65                        }
  66                        size = roundup_pow_of_two(size);
  67                        if (size != orig_size) {
  68                                sock_table =
  69                                    vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
  70                                if (!sock_table) {
  71                                        mutex_unlock(&sock_flow_mutex);
  72                                        return -ENOMEM;
  73                                }
  74                                rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
  75                                sock_table->mask = size - 1;
  76                        } else
  77                                sock_table = orig_sock_table;
  78
  79                        for (i = 0; i < size; i++)
  80                                sock_table->ents[i] = RPS_NO_CPU;
  81                } else
  82                        sock_table = NULL;
  83
  84                if (sock_table != orig_sock_table) {
  85                        rcu_assign_pointer(rps_sock_flow_table, sock_table);
  86                        if (sock_table) {
  87                                static_branch_inc(&rps_needed);
  88                                static_branch_inc(&rfs_needed);
  89                        }
  90                        if (orig_sock_table) {
  91                                static_branch_dec(&rps_needed);
  92                                static_branch_dec(&rfs_needed);
  93                                synchronize_rcu();
  94                                vfree(orig_sock_table);
  95                        }
  96                }
  97        }
  98
  99        mutex_unlock(&sock_flow_mutex);
 100
 101        return ret;
 102}
 103#endif /* CONFIG_RPS */
 104
 105#ifdef CONFIG_NET_FLOW_LIMIT
 106static DEFINE_MUTEX(flow_limit_update_mutex);
 107
 108static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
 109                                 void __user *buffer, size_t *lenp,
 110                                 loff_t *ppos)
 111{
 112        struct sd_flow_limit *cur;
 113        struct softnet_data *sd;
 114        cpumask_var_t mask;
 115        int i, len, ret = 0;
 116
 117        if (!alloc_cpumask_var(&mask, GFP_KERNEL))
 118                return -ENOMEM;
 119
 120        if (write) {
 121                ret = cpumask_parse_user(buffer, *lenp, mask);
 122                if (ret)
 123                        goto done;
 124
 125                mutex_lock(&flow_limit_update_mutex);
 126                len = sizeof(*cur) + netdev_flow_limit_table_len;
 127                for_each_possible_cpu(i) {
 128                        sd = &per_cpu(softnet_data, i);
 129                        cur = rcu_dereference_protected(sd->flow_limit,
 130                                     lockdep_is_held(&flow_limit_update_mutex));
 131                        if (cur && !cpumask_test_cpu(i, mask)) {
 132                                RCU_INIT_POINTER(sd->flow_limit, NULL);
 133                                synchronize_rcu();
 134                                kfree(cur);
 135                        } else if (!cur && cpumask_test_cpu(i, mask)) {
 136                                cur = kzalloc_node(len, GFP_KERNEL,
 137                                                   cpu_to_node(i));
 138                                if (!cur) {
 139                                        /* not unwinding previous changes */
 140                                        ret = -ENOMEM;
 141                                        goto write_unlock;
 142                                }
 143                                cur->num_buckets = netdev_flow_limit_table_len;
 144                                rcu_assign_pointer(sd->flow_limit, cur);
 145                        }
 146                }
 147write_unlock:
 148                mutex_unlock(&flow_limit_update_mutex);
 149        } else {
 150                char kbuf[128];
 151
 152                if (*ppos || !*lenp) {
 153                        *lenp = 0;
 154                        goto done;
 155                }
 156
 157                cpumask_clear(mask);
 158                rcu_read_lock();
 159                for_each_possible_cpu(i) {
 160                        sd = &per_cpu(softnet_data, i);
 161                        if (rcu_dereference(sd->flow_limit))
 162                                cpumask_set_cpu(i, mask);
 163                }
 164                rcu_read_unlock();
 165
 166                len = min(sizeof(kbuf) - 1, *lenp);
 167                len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
 168                if (!len) {
 169                        *lenp = 0;
 170                        goto done;
 171                }
 172                if (len < *lenp)
 173                        kbuf[len++] = '\n';
 174                if (copy_to_user(buffer, kbuf, len)) {
 175                        ret = -EFAULT;
 176                        goto done;
 177                }
 178                *lenp = len;
 179                *ppos += len;
 180        }
 181
 182done:
 183        free_cpumask_var(mask);
 184        return ret;
 185}
 186
 187static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
 188                                       void __user *buffer, size_t *lenp,
 189                                       loff_t *ppos)
 190{
 191        unsigned int old, *ptr;
 192        int ret;
 193
 194        mutex_lock(&flow_limit_update_mutex);
 195
 196        ptr = table->data;
 197        old = *ptr;
 198        ret = proc_dointvec(table, write, buffer, lenp, ppos);
 199        if (!ret && write && !is_power_of_2(*ptr)) {
 200                *ptr = old;
 201                ret = -EINVAL;
 202        }
 203
 204        mutex_unlock(&flow_limit_update_mutex);
 205        return ret;
 206}
 207#endif /* CONFIG_NET_FLOW_LIMIT */
 208
 209#ifdef CONFIG_NET_SCHED
 210static int set_default_qdisc(struct ctl_table *table, int write,
 211                             void __user *buffer, size_t *lenp, loff_t *ppos)
 212{
 213        char id[IFNAMSIZ];
 214        struct ctl_table tbl = {
 215                .data = id,
 216                .maxlen = IFNAMSIZ,
 217        };
 218        int ret;
 219
 220        qdisc_get_default(id, IFNAMSIZ);
 221
 222        ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
 223        if (write && ret == 0)
 224                ret = qdisc_set_default(id);
 225        return ret;
 226}
 227#endif
 228
 229static int proc_do_dev_weight(struct ctl_table *table, int write,
 230                           void __user *buffer, size_t *lenp, loff_t *ppos)
 231{
 232        int ret;
 233
 234        ret = proc_dointvec(table, write, buffer, lenp, ppos);
 235        if (ret != 0)
 236                return ret;
 237
 238        dev_rx_weight = weight_p * dev_weight_rx_bias;
 239        dev_tx_weight = weight_p * dev_weight_tx_bias;
 240
 241        return ret;
 242}
 243
 244static int proc_do_rss_key(struct ctl_table *table, int write,
 245                           void __user *buffer, size_t *lenp, loff_t *ppos)
 246{
 247        struct ctl_table fake_table;
 248        char buf[NETDEV_RSS_KEY_LEN * 3];
 249
 250        snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
 251        fake_table.data = buf;
 252        fake_table.maxlen = sizeof(buf);
 253        return proc_dostring(&fake_table, write, buffer, lenp, ppos);
 254}
 255
 256#ifdef CONFIG_BPF_JIT
 257static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
 258                                           void __user *buffer, size_t *lenp,
 259                                           loff_t *ppos)
 260{
 261        int ret, jit_enable = *(int *)table->data;
 262        struct ctl_table tmp = *table;
 263
 264        if (write && !capable(CAP_SYS_ADMIN))
 265                return -EPERM;
 266
 267        tmp.data = &jit_enable;
 268        ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
 269        if (write && !ret) {
 270                if (jit_enable < 2 ||
 271                    (jit_enable == 2 && bpf_dump_raw_ok())) {
 272                        *(int *)table->data = jit_enable;
 273                        if (jit_enable == 2)
 274                                pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
 275                } else {
 276                        ret = -EPERM;
 277                }
 278        }
 279        return ret;
 280}
 281
 282static int
 283proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
 284                                    void __user *buffer, size_t *lenp,
 285                                    loff_t *ppos)
 286{
 287        if (!capable(CAP_SYS_ADMIN))
 288                return -EPERM;
 289
 290        return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
 291}
 292
 293static int
 294proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
 295                                     void __user *buffer, size_t *lenp,
 296                                     loff_t *ppos)
 297{
 298        if (!capable(CAP_SYS_ADMIN))
 299                return -EPERM;
 300
 301        return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
 302}
 303#endif
 304
 305static struct ctl_table net_core_table[] = {
 306#ifdef CONFIG_NET
 307        {
 308                .procname       = "wmem_max",
 309                .data           = &sysctl_wmem_max,
 310                .maxlen         = sizeof(int),
 311                .mode           = 0644,
 312                .proc_handler   = proc_dointvec_minmax,
 313                .extra1         = &min_sndbuf,
 314        },
 315        {
 316                .procname       = "rmem_max",
 317                .data           = &sysctl_rmem_max,
 318                .maxlen         = sizeof(int),
 319                .mode           = 0644,
 320                .proc_handler   = proc_dointvec_minmax,
 321                .extra1         = &min_rcvbuf,
 322        },
 323        {
 324                .procname       = "wmem_default",
 325                .data           = &sysctl_wmem_default,
 326                .maxlen         = sizeof(int),
 327                .mode           = 0644,
 328                .proc_handler   = proc_dointvec_minmax,
 329                .extra1         = &min_sndbuf,
 330        },
 331        {
 332                .procname       = "rmem_default",
 333                .data           = &sysctl_rmem_default,
 334                .maxlen         = sizeof(int),
 335                .mode           = 0644,
 336                .proc_handler   = proc_dointvec_minmax,
 337                .extra1         = &min_rcvbuf,
 338        },
 339        {
 340                .procname       = "dev_weight",
 341                .data           = &weight_p,
 342                .maxlen         = sizeof(int),
 343                .mode           = 0644,
 344                .proc_handler   = proc_do_dev_weight,
 345        },
 346        {
 347                .procname       = "dev_weight_rx_bias",
 348                .data           = &dev_weight_rx_bias,
 349                .maxlen         = sizeof(int),
 350                .mode           = 0644,
 351                .proc_handler   = proc_do_dev_weight,
 352        },
 353        {
 354                .procname       = "dev_weight_tx_bias",
 355                .data           = &dev_weight_tx_bias,
 356                .maxlen         = sizeof(int),
 357                .mode           = 0644,
 358                .proc_handler   = proc_do_dev_weight,
 359        },
 360        {
 361                .procname       = "netdev_max_backlog",
 362                .data           = &netdev_max_backlog,
 363                .maxlen         = sizeof(int),
 364                .mode           = 0644,
 365                .proc_handler   = proc_dointvec
 366        },
 367        {
 368                .procname       = "netdev_rss_key",
 369                .data           = &netdev_rss_key,
 370                .maxlen         = sizeof(int),
 371                .mode           = 0444,
 372                .proc_handler   = proc_do_rss_key,
 373        },
 374#ifdef CONFIG_BPF_JIT
 375        {
 376                .procname       = "bpf_jit_enable",
 377                .data           = &bpf_jit_enable,
 378                .maxlen         = sizeof(int),
 379                .mode           = 0644,
 380                .proc_handler   = proc_dointvec_minmax_bpf_enable,
 381# ifdef CONFIG_BPF_JIT_ALWAYS_ON
 382                .extra1         = SYSCTL_ONE,
 383                .extra2         = SYSCTL_ONE,
 384# else
 385                .extra1         = SYSCTL_ZERO,
 386                .extra2         = &two,
 387# endif
 388        },
 389# ifdef CONFIG_HAVE_EBPF_JIT
 390        {
 391                .procname       = "bpf_jit_harden",
 392                .data           = &bpf_jit_harden,
 393                .maxlen         = sizeof(int),
 394                .mode           = 0600,
 395                .proc_handler   = proc_dointvec_minmax_bpf_restricted,
 396                .extra1         = SYSCTL_ZERO,
 397                .extra2         = &two,
 398        },
 399        {
 400                .procname       = "bpf_jit_kallsyms",
 401                .data           = &bpf_jit_kallsyms,
 402                .maxlen         = sizeof(int),
 403                .mode           = 0600,
 404                .proc_handler   = proc_dointvec_minmax_bpf_restricted,
 405                .extra1         = SYSCTL_ZERO,
 406                .extra2         = SYSCTL_ONE,
 407        },
 408# endif
 409        {
 410                .procname       = "bpf_jit_limit",
 411                .data           = &bpf_jit_limit,
 412                .maxlen         = sizeof(long),
 413                .mode           = 0600,
 414                .proc_handler   = proc_dolongvec_minmax_bpf_restricted,
 415                .extra1         = &long_one,
 416                .extra2         = &long_max,
 417        },
 418#endif
 419        {
 420                .procname       = "netdev_tstamp_prequeue",
 421                .data           = &netdev_tstamp_prequeue,
 422                .maxlen         = sizeof(int),
 423                .mode           = 0644,
 424                .proc_handler   = proc_dointvec
 425        },
 426        {
 427                .procname       = "message_cost",
 428                .data           = &net_ratelimit_state.interval,
 429                .maxlen         = sizeof(int),
 430                .mode           = 0644,
 431                .proc_handler   = proc_dointvec_jiffies,
 432        },
 433        {
 434                .procname       = "message_burst",
 435                .data           = &net_ratelimit_state.burst,
 436                .maxlen         = sizeof(int),
 437                .mode           = 0644,
 438                .proc_handler   = proc_dointvec,
 439        },
 440        {
 441                .procname       = "optmem_max",
 442                .data           = &sysctl_optmem_max,
 443                .maxlen         = sizeof(int),
 444                .mode           = 0644,
 445                .proc_handler   = proc_dointvec
 446        },
 447        {
 448                .procname       = "tstamp_allow_data",
 449                .data           = &sysctl_tstamp_allow_data,
 450                .maxlen         = sizeof(int),
 451                .mode           = 0644,
 452                .proc_handler   = proc_dointvec_minmax,
 453                .extra1         = SYSCTL_ZERO,
 454                .extra2         = SYSCTL_ONE
 455        },
 456#ifdef CONFIG_RPS
 457        {
 458                .procname       = "rps_sock_flow_entries",
 459                .maxlen         = sizeof(int),
 460                .mode           = 0644,
 461                .proc_handler   = rps_sock_flow_sysctl
 462        },
 463#endif
 464#ifdef CONFIG_NET_FLOW_LIMIT
 465        {
 466                .procname       = "flow_limit_cpu_bitmap",
 467                .mode           = 0644,
 468                .proc_handler   = flow_limit_cpu_sysctl
 469        },
 470        {
 471                .procname       = "flow_limit_table_len",
 472                .data           = &netdev_flow_limit_table_len,
 473                .maxlen         = sizeof(int),
 474                .mode           = 0644,
 475                .proc_handler   = flow_limit_table_len_sysctl
 476        },
 477#endif /* CONFIG_NET_FLOW_LIMIT */
 478#ifdef CONFIG_NET_RX_BUSY_POLL
 479        {
 480                .procname       = "busy_poll",
 481                .data           = &sysctl_net_busy_poll,
 482                .maxlen         = sizeof(unsigned int),
 483                .mode           = 0644,
 484                .proc_handler   = proc_dointvec_minmax,
 485                .extra1         = SYSCTL_ZERO,
 486        },
 487        {
 488                .procname       = "busy_read",
 489                .data           = &sysctl_net_busy_read,
 490                .maxlen         = sizeof(unsigned int),
 491                .mode           = 0644,
 492                .proc_handler   = proc_dointvec_minmax,
 493                .extra1         = SYSCTL_ZERO,
 494        },
 495#endif
 496#ifdef CONFIG_NET_SCHED
 497        {
 498                .procname       = "default_qdisc",
 499                .mode           = 0644,
 500                .maxlen         = IFNAMSIZ,
 501                .proc_handler   = set_default_qdisc
 502        },
 503#endif
 504#endif /* CONFIG_NET */
 505        {
 506                .procname       = "netdev_budget",
 507                .data           = &netdev_budget,
 508                .maxlen         = sizeof(int),
 509                .mode           = 0644,
 510                .proc_handler   = proc_dointvec
 511        },
 512        {
 513                .procname       = "warnings",
 514                .data           = &net_msg_warn,
 515                .maxlen         = sizeof(int),
 516                .mode           = 0644,
 517                .proc_handler   = proc_dointvec
 518        },
 519        {
 520                .procname       = "max_skb_frags",
 521                .data           = &sysctl_max_skb_frags,
 522                .maxlen         = sizeof(int),
 523                .mode           = 0644,
 524                .proc_handler   = proc_dointvec_minmax,
 525                .extra1         = SYSCTL_ONE,
 526                .extra2         = &max_skb_frags,
 527        },
 528        {
 529                .procname       = "netdev_budget_usecs",
 530                .data           = &netdev_budget_usecs,
 531                .maxlen         = sizeof(unsigned int),
 532                .mode           = 0644,
 533                .proc_handler   = proc_dointvec_minmax,
 534                .extra1         = SYSCTL_ZERO,
 535        },
 536        {
 537                .procname       = "fb_tunnels_only_for_init_net",
 538                .data           = &sysctl_fb_tunnels_only_for_init_net,
 539                .maxlen         = sizeof(int),
 540                .mode           = 0644,
 541                .proc_handler   = proc_dointvec_minmax,
 542                .extra1         = SYSCTL_ZERO,
 543                .extra2         = SYSCTL_ONE,
 544        },
 545        { }
 546};
 547
 548static struct ctl_table netns_core_table[] = {
 549        {
 550                .procname       = "somaxconn",
 551                .data           = &init_net.core.sysctl_somaxconn,
 552                .maxlen         = sizeof(int),
 553                .mode           = 0644,
 554                .extra1         = SYSCTL_ZERO,
 555                .proc_handler   = proc_dointvec_minmax
 556        },
 557        { }
 558};
 559
 560static __net_init int sysctl_core_net_init(struct net *net)
 561{
 562        struct ctl_table *tbl;
 563
 564        tbl = netns_core_table;
 565        if (!net_eq(net, &init_net)) {
 566                tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
 567                if (tbl == NULL)
 568                        goto err_dup;
 569
 570                tbl[0].data = &net->core.sysctl_somaxconn;
 571
 572                /* Don't export any sysctls to unprivileged users */
 573                if (net->user_ns != &init_user_ns) {
 574                        tbl[0].procname = NULL;
 575                }
 576        }
 577
 578        net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
 579        if (net->core.sysctl_hdr == NULL)
 580                goto err_reg;
 581
 582        return 0;
 583
 584err_reg:
 585        if (tbl != netns_core_table)
 586                kfree(tbl);
 587err_dup:
 588        return -ENOMEM;
 589}
 590
 591static __net_exit void sysctl_core_net_exit(struct net *net)
 592{
 593        struct ctl_table *tbl;
 594
 595        tbl = net->core.sysctl_hdr->ctl_table_arg;
 596        unregister_net_sysctl_table(net->core.sysctl_hdr);
 597        BUG_ON(tbl == netns_core_table);
 598        kfree(tbl);
 599}
 600
 601static __net_initdata struct pernet_operations sysctl_core_ops = {
 602        .init = sysctl_core_net_init,
 603        .exit = sysctl_core_net_exit,
 604};
 605
 606static __init int sysctl_core_init(void)
 607{
 608        register_net_sysctl(&init_net, "net/core", net_core_table);
 609        return register_pernet_subsys(&sysctl_core_ops);
 610}
 611
 612fs_initcall(sysctl_core_init);
 613