Mention branches and keyring.
[releases.git] / sctp / sysctl.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3  * (C) Copyright IBM Corp. 2002, 2004
4  * Copyright (c) 2002 Intel Corp.
5  *
6  * This file is part of the SCTP kernel implementation
7  *
8  * Sysctl related interfaces for SCTP.
9  *
10  * Please send any bug reports or fixes you make to the
11  * email address(es):
12  *    lksctp developers <linux-sctp@vger.kernel.org>
13  *
14  * Written or modified by:
15  *    Mingqin Liu           <liuming@us.ibm.com>
16  *    Jon Grimm             <jgrimm@us.ibm.com>
17  *    Ardelle Fan           <ardelle.fan@intel.com>
18  *    Ryan Layer            <rmlayer@us.ibm.com>
19  *    Sridhar Samudrala     <sri@us.ibm.com>
20  */
21
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <net/sctp/structs.h>
25 #include <net/sctp/sctp.h>
26 #include <linux/sysctl.h>
27
28 static int timer_max = 86400000; /* ms in one day */
29 static int sack_timer_min = 1;
30 static int sack_timer_max = 500;
31 static int addr_scope_max = SCTP_SCOPE_POLICY_MAX;
32 static int rwnd_scale_max = 16;
33 static int rto_alpha_min = 0;
34 static int rto_beta_min = 0;
35 static int rto_alpha_max = 1000;
36 static int rto_beta_max = 1000;
37 static int pf_expose_max = SCTP_PF_EXPOSE_MAX;
38 static int ps_retrans_max = SCTP_PS_RETRANS_MAX;
39 static int udp_port_max = 65535;
40
41 static unsigned long max_autoclose_min = 0;
42 static unsigned long max_autoclose_max =
43         (MAX_SCHEDULE_TIMEOUT / HZ > UINT_MAX)
44         ? UINT_MAX : MAX_SCHEDULE_TIMEOUT / HZ;
45
46 static int proc_sctp_do_hmac_alg(struct ctl_table *ctl, int write,
47                                  void *buffer, size_t *lenp, loff_t *ppos);
48 static int proc_sctp_do_rto_min(struct ctl_table *ctl, int write,
49                                 void *buffer, size_t *lenp, loff_t *ppos);
50 static int proc_sctp_do_rto_max(struct ctl_table *ctl, int write, void *buffer,
51                                 size_t *lenp, loff_t *ppos);
52 static int proc_sctp_do_udp_port(struct ctl_table *ctl, int write, void *buffer,
53                                  size_t *lenp, loff_t *ppos);
54 static int proc_sctp_do_alpha_beta(struct ctl_table *ctl, int write,
55                                    void *buffer, size_t *lenp, loff_t *ppos);
56 static int proc_sctp_do_auth(struct ctl_table *ctl, int write,
57                              void *buffer, size_t *lenp, loff_t *ppos);
58 static int proc_sctp_do_probe_interval(struct ctl_table *ctl, int write,
59                                        void *buffer, size_t *lenp, loff_t *ppos);
60
61 static struct ctl_table sctp_table[] = {
62         {
63                 .procname       = "sctp_mem",
64                 .data           = &sysctl_sctp_mem,
65                 .maxlen         = sizeof(sysctl_sctp_mem),
66                 .mode           = 0644,
67                 .proc_handler   = proc_doulongvec_minmax
68         },
69         {
70                 .procname       = "sctp_rmem",
71                 .data           = &sysctl_sctp_rmem,
72                 .maxlen         = sizeof(sysctl_sctp_rmem),
73                 .mode           = 0644,
74                 .proc_handler   = proc_dointvec,
75         },
76         {
77                 .procname       = "sctp_wmem",
78                 .data           = &sysctl_sctp_wmem,
79                 .maxlen         = sizeof(sysctl_sctp_wmem),
80                 .mode           = 0644,
81                 .proc_handler   = proc_dointvec,
82         },
83
84         { /* sentinel */ }
85 };
86
87 /* The following index defines are used in sctp_sysctl_net_register().
88  * If you add new items to the sctp_net_table, please ensure that
89  * the index values of these defines hold the same meaning indicated by
90  * their macro names when they appear in sctp_net_table.
91  */
92 #define SCTP_RTO_MIN_IDX       0
93 #define SCTP_RTO_MAX_IDX       1
94 #define SCTP_PF_RETRANS_IDX    2
95 #define SCTP_PS_RETRANS_IDX    3
96
97 static struct ctl_table sctp_net_table[] = {
98         [SCTP_RTO_MIN_IDX] = {
99                 .procname       = "rto_min",
100                 .data           = &init_net.sctp.rto_min,
101                 .maxlen         = sizeof(unsigned int),
102                 .mode           = 0644,
103                 .proc_handler   = proc_sctp_do_rto_min,
104                 .extra1         = SYSCTL_ONE,
105                 .extra2         = &init_net.sctp.rto_max
106         },
107         [SCTP_RTO_MAX_IDX] =  {
108                 .procname       = "rto_max",
109                 .data           = &init_net.sctp.rto_max,
110                 .maxlen         = sizeof(unsigned int),
111                 .mode           = 0644,
112                 .proc_handler   = proc_sctp_do_rto_max,
113                 .extra1         = &init_net.sctp.rto_min,
114                 .extra2         = &timer_max
115         },
116         [SCTP_PF_RETRANS_IDX] = {
117                 .procname       = "pf_retrans",
118                 .data           = &init_net.sctp.pf_retrans,
119                 .maxlen         = sizeof(int),
120                 .mode           = 0644,
121                 .proc_handler   = proc_dointvec_minmax,
122                 .extra1         = SYSCTL_ZERO,
123                 .extra2         = &init_net.sctp.ps_retrans,
124         },
125         [SCTP_PS_RETRANS_IDX] = {
126                 .procname       = "ps_retrans",
127                 .data           = &init_net.sctp.ps_retrans,
128                 .maxlen         = sizeof(int),
129                 .mode           = 0644,
130                 .proc_handler   = proc_dointvec_minmax,
131                 .extra1         = &init_net.sctp.pf_retrans,
132                 .extra2         = &ps_retrans_max,
133         },
134         {
135                 .procname       = "rto_initial",
136                 .data           = &init_net.sctp.rto_initial,
137                 .maxlen         = sizeof(unsigned int),
138                 .mode           = 0644,
139                 .proc_handler   = proc_dointvec_minmax,
140                 .extra1         = SYSCTL_ONE,
141                 .extra2         = &timer_max
142         },
143         {
144                 .procname       = "rto_alpha_exp_divisor",
145                 .data           = &init_net.sctp.rto_alpha,
146                 .maxlen         = sizeof(int),
147                 .mode           = 0644,
148                 .proc_handler   = proc_sctp_do_alpha_beta,
149                 .extra1         = &rto_alpha_min,
150                 .extra2         = &rto_alpha_max,
151         },
152         {
153                 .procname       = "rto_beta_exp_divisor",
154                 .data           = &init_net.sctp.rto_beta,
155                 .maxlen         = sizeof(int),
156                 .mode           = 0644,
157                 .proc_handler   = proc_sctp_do_alpha_beta,
158                 .extra1         = &rto_beta_min,
159                 .extra2         = &rto_beta_max,
160         },
161         {
162                 .procname       = "max_burst",
163                 .data           = &init_net.sctp.max_burst,
164                 .maxlen         = sizeof(int),
165                 .mode           = 0644,
166                 .proc_handler   = proc_dointvec_minmax,
167                 .extra1         = SYSCTL_ZERO,
168                 .extra2         = SYSCTL_INT_MAX,
169         },
170         {
171                 .procname       = "cookie_preserve_enable",
172                 .data           = &init_net.sctp.cookie_preserve_enable,
173                 .maxlen         = sizeof(int),
174                 .mode           = 0644,
175                 .proc_handler   = proc_dointvec,
176         },
177         {
178                 .procname       = "cookie_hmac_alg",
179                 .data           = &init_net.sctp.sctp_hmac_alg,
180                 .maxlen         = 8,
181                 .mode           = 0644,
182                 .proc_handler   = proc_sctp_do_hmac_alg,
183         },
184         {
185                 .procname       = "valid_cookie_life",
186                 .data           = &init_net.sctp.valid_cookie_life,
187                 .maxlen         = sizeof(unsigned int),
188                 .mode           = 0644,
189                 .proc_handler   = proc_dointvec_minmax,
190                 .extra1         = SYSCTL_ONE,
191                 .extra2         = &timer_max
192         },
193         {
194                 .procname       = "sack_timeout",
195                 .data           = &init_net.sctp.sack_timeout,
196                 .maxlen         = sizeof(int),
197                 .mode           = 0644,
198                 .proc_handler   = proc_dointvec_minmax,
199                 .extra1         = &sack_timer_min,
200                 .extra2         = &sack_timer_max,
201         },
202         {
203                 .procname       = "hb_interval",
204                 .data           = &init_net.sctp.hb_interval,
205                 .maxlen         = sizeof(unsigned int),
206                 .mode           = 0644,
207                 .proc_handler   = proc_dointvec_minmax,
208                 .extra1         = SYSCTL_ONE,
209                 .extra2         = &timer_max
210         },
211         {
212                 .procname       = "association_max_retrans",
213                 .data           = &init_net.sctp.max_retrans_association,
214                 .maxlen         = sizeof(int),
215                 .mode           = 0644,
216                 .proc_handler   = proc_dointvec_minmax,
217                 .extra1         = SYSCTL_ONE,
218                 .extra2         = SYSCTL_INT_MAX,
219         },
220         {
221                 .procname       = "path_max_retrans",
222                 .data           = &init_net.sctp.max_retrans_path,
223                 .maxlen         = sizeof(int),
224                 .mode           = 0644,
225                 .proc_handler   = proc_dointvec_minmax,
226                 .extra1         = SYSCTL_ONE,
227                 .extra2         = SYSCTL_INT_MAX,
228         },
229         {
230                 .procname       = "max_init_retransmits",
231                 .data           = &init_net.sctp.max_retrans_init,
232                 .maxlen         = sizeof(int),
233                 .mode           = 0644,
234                 .proc_handler   = proc_dointvec_minmax,
235                 .extra1         = SYSCTL_ONE,
236                 .extra2         = SYSCTL_INT_MAX,
237         },
238         {
239                 .procname       = "sndbuf_policy",
240                 .data           = &init_net.sctp.sndbuf_policy,
241                 .maxlen         = sizeof(int),
242                 .mode           = 0644,
243                 .proc_handler   = proc_dointvec,
244         },
245         {
246                 .procname       = "rcvbuf_policy",
247                 .data           = &init_net.sctp.rcvbuf_policy,
248                 .maxlen         = sizeof(int),
249                 .mode           = 0644,
250                 .proc_handler   = proc_dointvec,
251         },
252         {
253                 .procname       = "default_auto_asconf",
254                 .data           = &init_net.sctp.default_auto_asconf,
255                 .maxlen         = sizeof(int),
256                 .mode           = 0644,
257                 .proc_handler   = proc_dointvec,
258         },
259         {
260                 .procname       = "addip_enable",
261                 .data           = &init_net.sctp.addip_enable,
262                 .maxlen         = sizeof(int),
263                 .mode           = 0644,
264                 .proc_handler   = proc_dointvec,
265         },
266         {
267                 .procname       = "addip_noauth_enable",
268                 .data           = &init_net.sctp.addip_noauth,
269                 .maxlen         = sizeof(int),
270                 .mode           = 0644,
271                 .proc_handler   = proc_dointvec,
272         },
273         {
274                 .procname       = "prsctp_enable",
275                 .data           = &init_net.sctp.prsctp_enable,
276                 .maxlen         = sizeof(int),
277                 .mode           = 0644,
278                 .proc_handler   = proc_dointvec,
279         },
280         {
281                 .procname       = "reconf_enable",
282                 .data           = &init_net.sctp.reconf_enable,
283                 .maxlen         = sizeof(int),
284                 .mode           = 0644,
285                 .proc_handler   = proc_dointvec,
286         },
287         {
288                 .procname       = "auth_enable",
289                 .data           = &init_net.sctp.auth_enable,
290                 .maxlen         = sizeof(int),
291                 .mode           = 0644,
292                 .proc_handler   = proc_sctp_do_auth,
293         },
294         {
295                 .procname       = "intl_enable",
296                 .data           = &init_net.sctp.intl_enable,
297                 .maxlen         = sizeof(int),
298                 .mode           = 0644,
299                 .proc_handler   = proc_dointvec,
300         },
301         {
302                 .procname       = "ecn_enable",
303                 .data           = &init_net.sctp.ecn_enable,
304                 .maxlen         = sizeof(int),
305                 .mode           = 0644,
306                 .proc_handler   = proc_dointvec,
307         },
308         {
309                 .procname       = "plpmtud_probe_interval",
310                 .data           = &init_net.sctp.probe_interval,
311                 .maxlen         = sizeof(int),
312                 .mode           = 0644,
313                 .proc_handler   = proc_sctp_do_probe_interval,
314         },
315         {
316                 .procname       = "udp_port",
317                 .data           = &init_net.sctp.udp_port,
318                 .maxlen         = sizeof(int),
319                 .mode           = 0644,
320                 .proc_handler   = proc_sctp_do_udp_port,
321                 .extra1         = SYSCTL_ZERO,
322                 .extra2         = &udp_port_max,
323         },
324         {
325                 .procname       = "encap_port",
326                 .data           = &init_net.sctp.encap_port,
327                 .maxlen         = sizeof(int),
328                 .mode           = 0644,
329                 .proc_handler   = proc_dointvec_minmax,
330                 .extra1         = SYSCTL_ZERO,
331                 .extra2         = &udp_port_max,
332         },
333         {
334                 .procname       = "addr_scope_policy",
335                 .data           = &init_net.sctp.scope_policy,
336                 .maxlen         = sizeof(int),
337                 .mode           = 0644,
338                 .proc_handler   = proc_dointvec_minmax,
339                 .extra1         = SYSCTL_ZERO,
340                 .extra2         = &addr_scope_max,
341         },
342         {
343                 .procname       = "rwnd_update_shift",
344                 .data           = &init_net.sctp.rwnd_upd_shift,
345                 .maxlen         = sizeof(int),
346                 .mode           = 0644,
347                 .proc_handler   = &proc_dointvec_minmax,
348                 .extra1         = SYSCTL_ONE,
349                 .extra2         = &rwnd_scale_max,
350         },
351         {
352                 .procname       = "max_autoclose",
353                 .data           = &init_net.sctp.max_autoclose,
354                 .maxlen         = sizeof(unsigned long),
355                 .mode           = 0644,
356                 .proc_handler   = &proc_doulongvec_minmax,
357                 .extra1         = &max_autoclose_min,
358                 .extra2         = &max_autoclose_max,
359         },
360         {
361                 .procname       = "pf_enable",
362                 .data           = &init_net.sctp.pf_enable,
363                 .maxlen         = sizeof(int),
364                 .mode           = 0644,
365                 .proc_handler   = proc_dointvec,
366         },
367         {
368                 .procname       = "pf_expose",
369                 .data           = &init_net.sctp.pf_expose,
370                 .maxlen         = sizeof(int),
371                 .mode           = 0644,
372                 .proc_handler   = proc_dointvec_minmax,
373                 .extra1         = SYSCTL_ZERO,
374                 .extra2         = &pf_expose_max,
375         },
376
377         { /* sentinel */ }
378 };
379
380 static int proc_sctp_do_hmac_alg(struct ctl_table *ctl, int write,
381                                  void *buffer, size_t *lenp, loff_t *ppos)
382 {
383         struct net *net = current->nsproxy->net_ns;
384         struct ctl_table tbl;
385         bool changed = false;
386         char *none = "none";
387         char tmp[8] = {0};
388         int ret;
389
390         memset(&tbl, 0, sizeof(struct ctl_table));
391
392         if (write) {
393                 tbl.data = tmp;
394                 tbl.maxlen = sizeof(tmp);
395         } else {
396                 tbl.data = net->sctp.sctp_hmac_alg ? : none;
397                 tbl.maxlen = strlen(tbl.data);
398         }
399
400         ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
401         if (write && ret == 0) {
402 #ifdef CONFIG_CRYPTO_MD5
403                 if (!strncmp(tmp, "md5", 3)) {
404                         net->sctp.sctp_hmac_alg = "md5";
405                         changed = true;
406                 }
407 #endif
408 #ifdef CONFIG_CRYPTO_SHA1
409                 if (!strncmp(tmp, "sha1", 4)) {
410                         net->sctp.sctp_hmac_alg = "sha1";
411                         changed = true;
412                 }
413 #endif
414                 if (!strncmp(tmp, "none", 4)) {
415                         net->sctp.sctp_hmac_alg = NULL;
416                         changed = true;
417                 }
418                 if (!changed)
419                         ret = -EINVAL;
420         }
421
422         return ret;
423 }
424
425 static int proc_sctp_do_rto_min(struct ctl_table *ctl, int write,
426                                 void *buffer, size_t *lenp, loff_t *ppos)
427 {
428         struct net *net = current->nsproxy->net_ns;
429         unsigned int min = *(unsigned int *) ctl->extra1;
430         unsigned int max = *(unsigned int *) ctl->extra2;
431         struct ctl_table tbl;
432         int ret, new_value;
433
434         memset(&tbl, 0, sizeof(struct ctl_table));
435         tbl.maxlen = sizeof(unsigned int);
436
437         if (write)
438                 tbl.data = &new_value;
439         else
440                 tbl.data = &net->sctp.rto_min;
441
442         ret = proc_dointvec(&tbl, write, buffer, lenp, ppos);
443         if (write && ret == 0) {
444                 if (new_value > max || new_value < min)
445                         return -EINVAL;
446
447                 net->sctp.rto_min = new_value;
448         }
449
450         return ret;
451 }
452
453 static int proc_sctp_do_rto_max(struct ctl_table *ctl, int write,
454                                 void *buffer, size_t *lenp, loff_t *ppos)
455 {
456         struct net *net = current->nsproxy->net_ns;
457         unsigned int min = *(unsigned int *) ctl->extra1;
458         unsigned int max = *(unsigned int *) ctl->extra2;
459         struct ctl_table tbl;
460         int ret, new_value;
461
462         memset(&tbl, 0, sizeof(struct ctl_table));
463         tbl.maxlen = sizeof(unsigned int);
464
465         if (write)
466                 tbl.data = &new_value;
467         else
468                 tbl.data = &net->sctp.rto_max;
469
470         ret = proc_dointvec(&tbl, write, buffer, lenp, ppos);
471         if (write && ret == 0) {
472                 if (new_value > max || new_value < min)
473                         return -EINVAL;
474
475                 net->sctp.rto_max = new_value;
476         }
477
478         return ret;
479 }
480
481 static int proc_sctp_do_alpha_beta(struct ctl_table *ctl, int write,
482                                    void *buffer, size_t *lenp, loff_t *ppos)
483 {
484         if (write)
485                 pr_warn_once("Changing rto_alpha or rto_beta may lead to "
486                              "suboptimal rtt/srtt estimations!\n");
487
488         return proc_dointvec_minmax(ctl, write, buffer, lenp, ppos);
489 }
490
491 static int proc_sctp_do_auth(struct ctl_table *ctl, int write,
492                              void *buffer, size_t *lenp, loff_t *ppos)
493 {
494         struct net *net = current->nsproxy->net_ns;
495         struct ctl_table tbl;
496         int new_value, ret;
497
498         memset(&tbl, 0, sizeof(struct ctl_table));
499         tbl.maxlen = sizeof(unsigned int);
500
501         if (write)
502                 tbl.data = &new_value;
503         else
504                 tbl.data = &net->sctp.auth_enable;
505
506         ret = proc_dointvec(&tbl, write, buffer, lenp, ppos);
507         if (write && ret == 0) {
508                 struct sock *sk = net->sctp.ctl_sock;
509
510                 net->sctp.auth_enable = new_value;
511                 /* Update the value in the control socket */
512                 lock_sock(sk);
513                 sctp_sk(sk)->ep->auth_enable = new_value;
514                 release_sock(sk);
515         }
516
517         return ret;
518 }
519
520 static int proc_sctp_do_udp_port(struct ctl_table *ctl, int write,
521                                  void *buffer, size_t *lenp, loff_t *ppos)
522 {
523         struct net *net = current->nsproxy->net_ns;
524         unsigned int min = *(unsigned int *)ctl->extra1;
525         unsigned int max = *(unsigned int *)ctl->extra2;
526         struct ctl_table tbl;
527         int ret, new_value;
528
529         memset(&tbl, 0, sizeof(struct ctl_table));
530         tbl.maxlen = sizeof(unsigned int);
531
532         if (write)
533                 tbl.data = &new_value;
534         else
535                 tbl.data = &net->sctp.udp_port;
536
537         ret = proc_dointvec(&tbl, write, buffer, lenp, ppos);
538         if (write && ret == 0) {
539                 struct sock *sk = net->sctp.ctl_sock;
540
541                 if (new_value > max || new_value < min)
542                         return -EINVAL;
543
544                 net->sctp.udp_port = new_value;
545                 sctp_udp_sock_stop(net);
546                 if (new_value) {
547                         ret = sctp_udp_sock_start(net);
548                         if (ret)
549                                 net->sctp.udp_port = 0;
550                 }
551
552                 /* Update the value in the control socket */
553                 lock_sock(sk);
554                 sctp_sk(sk)->udp_port = htons(net->sctp.udp_port);
555                 release_sock(sk);
556         }
557
558         return ret;
559 }
560
561 static int proc_sctp_do_probe_interval(struct ctl_table *ctl, int write,
562                                        void *buffer, size_t *lenp, loff_t *ppos)
563 {
564         struct net *net = current->nsproxy->net_ns;
565         struct ctl_table tbl;
566         int ret, new_value;
567
568         memset(&tbl, 0, sizeof(struct ctl_table));
569         tbl.maxlen = sizeof(unsigned int);
570
571         if (write)
572                 tbl.data = &new_value;
573         else
574                 tbl.data = &net->sctp.probe_interval;
575
576         ret = proc_dointvec(&tbl, write, buffer, lenp, ppos);
577         if (write && ret == 0) {
578                 if (new_value && new_value < SCTP_PROBE_TIMER_MIN)
579                         return -EINVAL;
580
581                 net->sctp.probe_interval = new_value;
582         }
583
584         return ret;
585 }
586
587 int sctp_sysctl_net_register(struct net *net)
588 {
589         struct ctl_table *table;
590         int i;
591
592         table = kmemdup(sctp_net_table, sizeof(sctp_net_table), GFP_KERNEL);
593         if (!table)
594                 return -ENOMEM;
595
596         for (i = 0; table[i].data; i++)
597                 table[i].data += (char *)(&net->sctp) - (char *)&init_net.sctp;
598
599         table[SCTP_RTO_MIN_IDX].extra2 = &net->sctp.rto_max;
600         table[SCTP_RTO_MAX_IDX].extra1 = &net->sctp.rto_min;
601         table[SCTP_PF_RETRANS_IDX].extra2 = &net->sctp.ps_retrans;
602         table[SCTP_PS_RETRANS_IDX].extra1 = &net->sctp.pf_retrans;
603
604         net->sctp.sysctl_header = register_net_sysctl(net, "net/sctp", table);
605         if (net->sctp.sysctl_header == NULL) {
606                 kfree(table);
607                 return -ENOMEM;
608         }
609         return 0;
610 }
611
612 void sctp_sysctl_net_unregister(struct net *net)
613 {
614         struct ctl_table *table;
615
616         table = net->sctp.sysctl_header->ctl_table_arg;
617         unregister_net_sysctl_table(net->sctp.sysctl_header);
618         kfree(table);
619 }
620
621 static struct ctl_table_header *sctp_sysctl_header;
622
623 /* Sysctl registration.  */
624 void sctp_sysctl_register(void)
625 {
626         sctp_sysctl_header = register_net_sysctl(&init_net, "net/sctp", sctp_table);
627 }
628
629 /* Sysctl deregistration.  */
630 void sctp_sysctl_unregister(void)
631 {
632         unregister_net_sysctl_table(sctp_sysctl_header);
633 }