LCOV - code coverage report
Current view: top level - mptcp/ctrl.c (source / functions) Coverage Total Hit
Test: export Lines: 96.5 % 227 219
Test Date: 2026-11-30 19:02:55 Functions: 100.0 % 30 30
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 69.5 % 59 41

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: GPL-2.0
       2                 :             : /* Multipath TCP
       3                 :             :  *
       4                 :             :  * Copyright (c) 2019, Tessares SA.
       5                 :             :  */
       6                 :             : 
       7                 :             : #ifdef CONFIG_SYSCTL
       8                 :             : #include <linux/sysctl.h>
       9                 :             : #endif
      10                 :             : 
      11                 :             : #include <net/net_namespace.h>
      12                 :             : #include <net/netns/generic.h>
      13                 :             : 
      14                 :             : #include "protocol.h"
      15                 :             : #include "mib.h"
      16                 :             : 
      17                 :             : #define MPTCP_SYSCTL_PATH "net/mptcp"
      18                 :             : 
      19                 :             : static int mptcp_pernet_id;
      20                 :             : 
      21                 :             : #ifdef CONFIG_SYSCTL
      22                 :             : static int mptcp_pm_type_max = __MPTCP_PM_TYPE_MAX;
      23                 :             : static int one_day_secs = 24 * 3600;
      24                 :             : #endif
      25                 :             : 
      26                 :             : struct mptcp_pernet {
      27                 :             : #ifdef CONFIG_SYSCTL
      28                 :             :         struct ctl_table_header *ctl_table_hdr;
      29                 :             : #endif
      30                 :             : 
      31                 :             :         unsigned int add_addr_timeout;
      32                 :             :         unsigned int blackhole_timeout;
      33                 :             :         unsigned int close_timeout;
      34                 :             :         unsigned int stale_loss_cnt;
      35                 :             :         atomic_t active_disable_times;
      36                 :             :         unsigned long active_disable_stamp;
      37                 :             :         u8 syn_retrans_before_tcp_fallback;
      38                 :             :         u8 mptcp_enabled;
      39                 :             :         u8 checksum_enabled;
      40                 :             :         u8 allow_join_initial_addr_port;
      41                 :             :         u8 pm_type;
      42                 :             :         u8 add_addr_v6_port_drop_ts;
      43                 :             :         char scheduler[MPTCP_SCHED_NAME_MAX];
      44                 :             :         char path_manager[MPTCP_PM_NAME_MAX];
      45                 :             : };
      46                 :             : 
      47                 :       21629 : static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
      48                 :             : {
      49                 :       22290 :         return net_generic(net, mptcp_pernet_id);
      50                 :             : }
      51                 :             : 
      52                 :        3614 : int mptcp_is_enabled(const struct net *net)
      53                 :             : {
      54                 :        3614 :         return READ_ONCE(mptcp_get_pernet(net)->mptcp_enabled);
      55                 :             : }
      56                 :             : 
      57                 :         515 : unsigned int mptcp_get_add_addr_timeout(const struct net *net)
      58                 :             : {
      59                 :         515 :         return READ_ONCE(mptcp_get_pernet(net)->add_addr_timeout);
      60                 :             : }
      61                 :             : 
      62                 :        9852 : int mptcp_is_checksum_enabled(const struct net *net)
      63                 :             : {
      64                 :        9852 :         return READ_ONCE(mptcp_get_pernet(net)->checksum_enabled);
      65                 :             : }
      66                 :             : 
      67                 :        6893 : int mptcp_allow_join_id0(const struct net *net)
      68                 :             : {
      69                 :        6893 :         return READ_ONCE(mptcp_get_pernet(net)->allow_join_initial_addr_port);
      70                 :             : }
      71                 :             : 
      72                 :        2664 : unsigned int mptcp_stale_loss_cnt(const struct net *net)
      73                 :             : {
      74                 :        2664 :         return READ_ONCE(mptcp_get_pernet(net)->stale_loss_cnt);
      75                 :             : }
      76                 :             : 
      77                 :       15614 : unsigned int mptcp_close_timeout(const struct sock *sk)
      78                 :             : {
      79       [ + ][ + ]:       15614 :         if (sock_flag(sk, SOCK_DEAD))
      80                 :             :                 return TCP_TIMEWAIT_LEN;
      81                 :         225 :         return READ_ONCE(mptcp_get_pernet(sock_net(sk))->close_timeout);
      82                 :             : }
      83                 :             : 
      84                 :        5422 : int mptcp_get_pm_type(const struct net *net)
      85                 :             : {
      86                 :        5422 :         return READ_ONCE(mptcp_get_pernet(net)->pm_type);
      87                 :             : }
      88                 :             : 
      89                 :        5422 : const char *mptcp_get_path_manager(const struct net *net)
      90                 :             : {
      91                 :        5422 :         return mptcp_get_pernet(net)->path_manager;
      92                 :             : }
      93                 :             : 
      94                 :        3604 : const char *mptcp_get_scheduler(const struct net *net)
      95                 :             : {
      96                 :        3604 :         return mptcp_get_pernet(net)->scheduler;
      97                 :             : }
      98                 :             : 
      99                 :          16 : unsigned int mptcp_add_addr_v6_port_drop_ts(const struct net *net)
     100                 :             : {
     101                 :          16 :         return READ_ONCE(mptcp_get_pernet(net)->add_addr_v6_port_drop_ts);
     102                 :             : }
     103                 :             : 
     104                 :        1426 : static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
     105                 :             : {
     106                 :        1426 :         pernet->mptcp_enabled = 1;
     107                 :        1426 :         pernet->add_addr_timeout = TCP_RTO_MAX;
     108                 :        1426 :         pernet->blackhole_timeout = 3600;
     109                 :        1426 :         pernet->syn_retrans_before_tcp_fallback = 2;
     110                 :        1426 :         atomic_set(&pernet->active_disable_times, 0);
     111                 :        1426 :         pernet->close_timeout = TCP_TIMEWAIT_LEN;
     112                 :        1426 :         pernet->checksum_enabled = 0;
     113                 :        1426 :         pernet->allow_join_initial_addr_port = 1;
     114                 :        1426 :         pernet->stale_loss_cnt = 4;
     115                 :        1426 :         pernet->pm_type = MPTCP_PM_TYPE_KERNEL;
     116                 :        1426 :         strscpy(pernet->scheduler, "default", sizeof(pernet->scheduler));
     117                 :        1426 :         strscpy(pernet->path_manager, "kernel", sizeof(pernet->path_manager));
     118                 :        1426 :         pernet->add_addr_v6_port_drop_ts = 1;
     119                 :        1426 : }
     120                 :             : 
     121                 :             : #ifdef CONFIG_SYSCTL
     122                 :          36 : static int mptcp_set_scheduler(char *scheduler, const char *name)
     123                 :             : {
     124                 :          36 :         struct mptcp_sched_ops *sched;
     125                 :          36 :         int ret = 0;
     126                 :             : 
     127                 :          36 :         rcu_read_lock();
     128                 :          36 :         sched = mptcp_sched_find(name);
     129       [ - ][ - ]:          36 :         if (sched)
     130                 :          36 :                 strscpy(scheduler, name, MPTCP_SCHED_NAME_MAX);
     131                 :             :         else
     132                 :             :                 ret = -ENOENT;
     133                 :          36 :         rcu_read_unlock();
     134                 :             : 
     135                 :          36 :         return ret;
     136                 :             : }
     137                 :             : 
     138                 :          56 : static int proc_scheduler(const struct ctl_table *ctl, int write,
     139                 :             :                           void *buffer, size_t *lenp, loff_t *ppos)
     140                 :             : {
     141                 :          56 :         char (*scheduler)[MPTCP_SCHED_NAME_MAX] = ctl->data;
     142                 :          56 :         char val[MPTCP_SCHED_NAME_MAX];
     143                 :          56 :         struct ctl_table tbl = {
     144                 :             :                 .data = val,
     145                 :             :                 .maxlen = MPTCP_SCHED_NAME_MAX,
     146                 :             :         };
     147                 :          56 :         int ret;
     148                 :             : 
     149                 :          56 :         strscpy(val, *scheduler, MPTCP_SCHED_NAME_MAX);
     150                 :             : 
     151                 :          56 :         ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
     152            [ + ]:          56 :         if (write && ret == 0)
     153                 :          36 :                 ret = mptcp_set_scheduler(*scheduler, val);
     154                 :             : 
     155                 :          56 :         return ret;
     156                 :             : }
     157                 :             : 
     158                 :          20 : static int proc_available_schedulers(const struct ctl_table *ctl,
     159                 :             :                                      int write, void *buffer,
     160                 :             :                                      size_t *lenp, loff_t *ppos)
     161                 :             : {
     162                 :          20 :         struct ctl_table tbl = { .maxlen = MPTCP_SCHED_BUF_MAX, };
     163                 :          20 :         int ret;
     164                 :             : 
     165                 :          20 :         tbl.data = kmalloc(tbl.maxlen, GFP_USER);
     166            [ - ]:          20 :         if (!tbl.data)
     167                 :             :                 return -ENOMEM;
     168                 :             : 
     169                 :          20 :         mptcp_get_available_schedulers(tbl.data, MPTCP_SCHED_BUF_MAX);
     170                 :          20 :         ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
     171                 :          20 :         kfree(tbl.data);
     172                 :             : 
     173                 :          20 :         return ret;
     174                 :             : }
     175                 :             : 
     176                 :          32 : static int proc_blackhole_detect_timeout(const struct ctl_table *table,
     177                 :             :                                          int write, void *buffer, size_t *lenp,
     178                 :             :                                          loff_t *ppos)
     179                 :             : {
     180                 :          32 :         struct mptcp_pernet *pernet = container_of(table->data,
     181                 :             :                                                    struct mptcp_pernet,
     182                 :             :                                                    blackhole_timeout);
     183                 :          32 :         int ret;
     184                 :             : 
     185                 :          32 :         ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
     186       [ + ][ + ]:          32 :         if (write && ret == 0)
     187                 :          12 :                 atomic_set(&pernet->active_disable_times, 0);
     188                 :             : 
     189                 :          32 :         return ret;
     190                 :             : }
     191                 :             : 
     192                 :         600 : static int mptcp_set_path_manager(char *path_manager, const char *name)
     193                 :             : {
     194                 :         600 :         struct mptcp_pm_ops *pm_ops;
     195                 :         600 :         int ret = 0;
     196                 :             : 
     197                 :         600 :         rcu_read_lock();
     198                 :         600 :         pm_ops = mptcp_pm_find(name);
     199       [ + ][ + ]:         600 :         if (pm_ops)
     200                 :         598 :                 strscpy(path_manager, name, MPTCP_PM_NAME_MAX);
     201                 :             :         else
     202                 :             :                 ret = -ENOENT;
     203                 :         600 :         rcu_read_unlock();
     204                 :             : 
     205                 :         600 :         return ret;
     206                 :             : }
     207                 :             : 
     208                 :          28 : static int proc_path_manager(const struct ctl_table *ctl, int write,
     209                 :             :                              void *buffer, size_t *lenp, loff_t *ppos)
     210                 :             : {
     211                 :          28 :         struct mptcp_pernet *pernet = container_of(ctl->data,
     212                 :             :                                                    struct mptcp_pernet,
     213                 :             :                                                    path_manager);
     214                 :          28 :         char (*path_manager)[MPTCP_PM_NAME_MAX] = ctl->data;
     215                 :          28 :         char pm_name[MPTCP_PM_NAME_MAX];
     216                 :          28 :         const struct ctl_table tbl = {
     217                 :             :                 .data = pm_name,
     218                 :             :                 .maxlen = MPTCP_PM_NAME_MAX,
     219                 :             :         };
     220                 :          28 :         int ret;
     221                 :             : 
     222                 :          28 :         strscpy(pm_name, *path_manager, MPTCP_PM_NAME_MAX);
     223                 :             : 
     224                 :          28 :         ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
     225            [ + ]:          28 :         if (write && ret == 0) {
     226                 :           4 :                 ret = mptcp_set_path_manager(*path_manager, pm_name);
     227            [ + ]:           4 :                 if (ret == 0) {
     228                 :           2 :                         u8 pm_type = __MPTCP_PM_TYPE_NR;
     229                 :             : 
     230            [ - ]:           2 :                         if (strncmp(pm_name, "kernel", MPTCP_PM_NAME_MAX) == 0)
     231                 :             :                                 pm_type = MPTCP_PM_TYPE_KERNEL;
     232            [ - ]:           2 :                         else if (strncmp(pm_name, "userspace", MPTCP_PM_NAME_MAX) == 0)
     233                 :           2 :                                 pm_type = MPTCP_PM_TYPE_USERSPACE;
     234                 :           2 :                         WRITE_ONCE(pernet->pm_type, pm_type);
     235                 :             :                 }
     236                 :             :         }
     237                 :             : 
     238                 :          28 :         return ret;
     239                 :             : }
     240                 :             : 
     241                 :         624 : static int proc_pm_type(const struct ctl_table *ctl, int write,
     242                 :             :                         void *buffer, size_t *lenp, loff_t *ppos)
     243                 :             : {
     244                 :         624 :         struct mptcp_pernet *pernet = container_of(ctl->data,
     245                 :             :                                                    struct mptcp_pernet,
     246                 :             :                                                    pm_type);
     247                 :         624 :         int ret;
     248                 :             : 
     249                 :         624 :         ret = proc_dou8vec_minmax(ctl, write, buffer, lenp, ppos);
     250            [ + ]:         624 :         if (write && ret == 0) {
     251                 :         596 :                 u8 pm_type = READ_ONCE(*(u8 *)ctl->data);
     252                 :         596 :                 char *pm_name = "";
     253                 :             : 
     254            [ + ]:         596 :                 if (pm_type == MPTCP_PM_TYPE_KERNEL)
     255                 :             :                         pm_name = "kernel";
     256            [ - ]:          30 :                 else if (pm_type == MPTCP_PM_TYPE_USERSPACE)
     257                 :          30 :                         pm_name = "userspace";
     258                 :         596 :                 mptcp_set_path_manager(pernet->path_manager, pm_name);
     259                 :             :         }
     260                 :             : 
     261                 :         624 :         return ret;
     262                 :             : }
     263                 :             : 
     264                 :          20 : static int proc_available_path_managers(const struct ctl_table *ctl,
     265                 :             :                                         int write, void *buffer,
     266                 :             :                                         size_t *lenp, loff_t *ppos)
     267                 :             : {
     268                 :          20 :         struct ctl_table tbl = { .maxlen = MPTCP_PM_BUF_MAX, };
     269                 :          20 :         int ret;
     270                 :             : 
     271                 :          20 :         tbl.data = kmalloc(tbl.maxlen, GFP_USER);
     272            [ - ]:          20 :         if (!tbl.data)
     273                 :             :                 return -ENOMEM;
     274                 :             : 
     275                 :          20 :         mptcp_pm_get_available(tbl.data, MPTCP_PM_BUF_MAX);
     276                 :          20 :         ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
     277                 :          20 :         kfree(tbl.data);
     278                 :             : 
     279                 :          20 :         return ret;
     280                 :             : }
     281                 :             : 
     282                 :             : static struct ctl_table mptcp_sysctl_table[] = {
     283                 :             :         {
     284                 :             :                 .procname = "enabled",
     285                 :             :                 .maxlen = sizeof(u8),
     286                 :             :                 .mode = 0644,
     287                 :             :                 /* users with CAP_NET_ADMIN or root (not and) can change this
     288                 :             :                  * value, same as other sysctl or the 'net' tree.
     289                 :             :                  */
     290                 :             :                 .proc_handler = proc_dou8vec_minmax,
     291                 :             :                 .extra1       = SYSCTL_ZERO,
     292                 :             :                 .extra2       = SYSCTL_ONE
     293                 :             :         },
     294                 :             :         {
     295                 :             :                 .procname = "add_addr_timeout",
     296                 :             :                 .maxlen = sizeof(unsigned int),
     297                 :             :                 .mode = 0644,
     298                 :             :                 .proc_handler = proc_dointvec_jiffies,
     299                 :             :         },
     300                 :             :         {
     301                 :             :                 .procname = "checksum_enabled",
     302                 :             :                 .maxlen = sizeof(u8),
     303                 :             :                 .mode = 0644,
     304                 :             :                 .proc_handler = proc_dou8vec_minmax,
     305                 :             :                 .extra1       = SYSCTL_ZERO,
     306                 :             :                 .extra2       = SYSCTL_ONE
     307                 :             :         },
     308                 :             :         {
     309                 :             :                 .procname = "allow_join_initial_addr_port",
     310                 :             :                 .maxlen = sizeof(u8),
     311                 :             :                 .mode = 0644,
     312                 :             :                 .proc_handler = proc_dou8vec_minmax,
     313                 :             :                 .extra1       = SYSCTL_ZERO,
     314                 :             :                 .extra2       = SYSCTL_ONE
     315                 :             :         },
     316                 :             :         {
     317                 :             :                 .procname = "stale_loss_cnt",
     318                 :             :                 .maxlen = sizeof(unsigned int),
     319                 :             :                 .mode = 0644,
     320                 :             :                 .proc_handler = proc_douintvec_minmax,
     321                 :             :         },
     322                 :             :         {
     323                 :             :                 .procname = "pm_type",
     324                 :             :                 .maxlen = sizeof(u8),
     325                 :             :                 .mode = 0644,
     326                 :             :                 .proc_handler = proc_pm_type,
     327                 :             :                 .extra1       = SYSCTL_ZERO,
     328                 :             :                 .extra2       = &mptcp_pm_type_max
     329                 :             :         },
     330                 :             :         {
     331                 :             :                 .procname = "scheduler",
     332                 :             :                 .maxlen = MPTCP_SCHED_NAME_MAX,
     333                 :             :                 .mode = 0644,
     334                 :             :                 .proc_handler = proc_scheduler,
     335                 :             :         },
     336                 :             :         {
     337                 :             :                 .procname = "available_schedulers",
     338                 :             :                 .maxlen = MPTCP_SCHED_BUF_MAX,
     339                 :             :                 .mode = 0444,
     340                 :             :                 .proc_handler = proc_available_schedulers,
     341                 :             :         },
     342                 :             :         {
     343                 :             :                 .procname = "close_timeout",
     344                 :             :                 .maxlen = sizeof(unsigned int),
     345                 :             :                 .mode = 0644,
     346                 :             :                 .proc_handler = proc_dointvec_jiffies,
     347                 :             :         },
     348                 :             :         {
     349                 :             :                 .procname = "blackhole_timeout",
     350                 :             :                 .maxlen = sizeof(unsigned int),
     351                 :             :                 .mode = 0644,
     352                 :             :                 .proc_handler = proc_blackhole_detect_timeout,
     353                 :             :                 .extra1 = SYSCTL_ZERO,
     354                 :             :                 .extra2 = &one_day_secs,
     355                 :             :         },
     356                 :             :         {
     357                 :             :                 .procname = "syn_retrans_before_tcp_fallback",
     358                 :             :                 .maxlen = sizeof(u8),
     359                 :             :                 .mode = 0644,
     360                 :             :                 .proc_handler = proc_dou8vec_minmax,
     361                 :             :         },
     362                 :             :         {
     363                 :             :                 .procname = "path_manager",
     364                 :             :                 .maxlen = MPTCP_PM_NAME_MAX,
     365                 :             :                 .mode = 0644,
     366                 :             :                 .proc_handler = proc_path_manager,
     367                 :             :         },
     368                 :             :         {
     369                 :             :                 .procname = "available_path_managers",
     370                 :             :                 .maxlen = MPTCP_PM_BUF_MAX,
     371                 :             :                 .mode = 0444,
     372                 :             :                 .proc_handler = proc_available_path_managers,
     373                 :             :         },
     374                 :             :         {
     375                 :             :                 .procname = "add_addr_v6_port_drop_ts",
     376                 :             :                 .maxlen = sizeof(u8),
     377                 :             :                 .mode = 0644,
     378                 :             :                 .proc_handler = proc_dou8vec_minmax,
     379                 :             :                 .extra1       = SYSCTL_ZERO,
     380                 :             :                 .extra2       = SYSCTL_ONE
     381                 :             :         },
     382                 :             : };
     383                 :             : 
     384                 :        1426 : static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
     385                 :             : {
     386                 :        1426 :         struct ctl_table_header *hdr;
     387                 :        1426 :         struct ctl_table *table;
     388                 :             : 
     389                 :        1426 :         table = mptcp_sysctl_table;
     390            [ + ]:        1426 :         if (!net_eq(net, &init_net)) {
     391                 :        1420 :                 table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
     392            [ + ]:        1420 :                 if (!table)
     393                 :           0 :                         goto err_alloc;
     394                 :             :         }
     395                 :             : 
     396                 :        1426 :         table[0].data = &pernet->mptcp_enabled;
     397                 :        1426 :         table[1].data = &pernet->add_addr_timeout;
     398                 :        1426 :         table[2].data = &pernet->checksum_enabled;
     399                 :        1426 :         table[3].data = &pernet->allow_join_initial_addr_port;
     400                 :        1426 :         table[4].data = &pernet->stale_loss_cnt;
     401                 :        1426 :         table[5].data = &pernet->pm_type;
     402                 :        1426 :         table[6].data = &pernet->scheduler;
     403                 :             :         /* table[7] is for available_schedulers which is read-only info */
     404                 :        1426 :         table[8].data = &pernet->close_timeout;
     405                 :        1426 :         table[9].data = &pernet->blackhole_timeout;
     406                 :        1426 :         table[10].data = &pernet->syn_retrans_before_tcp_fallback;
     407                 :        1426 :         table[11].data = &pernet->path_manager;
     408                 :             :         /* table[12] is for available_path_managers which is read-only info */
     409                 :        1426 :         table[13].data = &pernet->add_addr_v6_port_drop_ts;
     410                 :             : 
     411                 :        1426 :         hdr = register_net_sysctl_sz(net, MPTCP_SYSCTL_PATH, table,
     412                 :             :                                      ARRAY_SIZE(mptcp_sysctl_table));
     413            [ + ]:        1426 :         if (!hdr)
     414                 :           0 :                 goto err_reg;
     415                 :             : 
     416                 :        1426 :         pernet->ctl_table_hdr = hdr;
     417                 :             : 
     418                 :        1426 :         return 0;
     419                 :             : 
     420                 :           0 : err_reg:
     421            [ # ]:           0 :         if (!net_eq(net, &init_net))
     422                 :           0 :                 kfree(table);
     423                 :           0 : err_alloc:
     424                 :             :         return -ENOMEM;
     425                 :             : }
     426                 :             : 
     427                 :        1344 : static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
     428                 :             : {
     429                 :        1344 :         const struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
     430                 :             : 
     431                 :        1344 :         unregister_net_sysctl_table(pernet->ctl_table_hdr);
     432                 :             : 
     433                 :        1344 :         kfree(table);
     434                 :        1344 : }
     435                 :             : 
     436                 :             : #else
     437                 :             : 
     438                 :             : static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
     439                 :             : {
     440                 :             :         return 0;
     441                 :             : }
     442                 :             : 
     443                 :             : static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
     444                 :             : 
     445                 :             : #endif /* CONFIG_SYSCTL */
     446                 :             : 
     447                 :             : /* The following code block is to deal with middle box issues with MPTCP,
     448                 :             :  * similar to what is done with TFO.
     449                 :             :  * The proposed solution is to disable active MPTCP globally when SYN+MPC are
     450                 :             :  * dropped, while SYN without MPC aren't. In this case, active side MPTCP is
     451                 :             :  * disabled globally for 1hr at first. Then if it happens again, it is disabled
     452                 :             :  * for 2h, then 4h, 8h, ...
     453                 :             :  * The timeout is reset back to 1hr when a successful active MPTCP connection is
     454                 :             :  * fully established.
     455                 :             :  */
     456                 :             : 
     457                 :             : /* Disable active MPTCP and record current jiffies and active_disable_times */
     458                 :          18 : void mptcp_active_disable(struct sock *sk)
     459                 :             : {
     460                 :          18 :         struct net *net = sock_net(sk);
     461                 :          18 :         struct mptcp_pernet *pernet;
     462                 :             : 
     463                 :          18 :         pernet = mptcp_get_pernet(net);
     464                 :             : 
     465       [ - ][ - ]:          18 :         if (!READ_ONCE(pernet->blackhole_timeout))
     466                 :             :                 return;
     467                 :             : 
     468                 :             :         /* Paired with READ_ONCE() in mptcp_active_should_disable() */
     469                 :          18 :         WRITE_ONCE(pernet->active_disable_stamp, jiffies);
     470                 :             : 
     471                 :             :         /* Paired with smp_rmb() in mptcp_active_should_disable().
     472                 :             :          * We want pernet->active_disable_stamp to be updated first.
     473                 :             :          */
     474                 :          18 :         smp_mb__before_atomic();
     475                 :          18 :         atomic_inc(&pernet->active_disable_times);
     476                 :             : 
     477       [ - ][ - ]:          18 :         MPTCP_INC_STATS(net, MPTCP_MIB_BLACKHOLE);
     478                 :             : }
     479                 :             : 
     480                 :             : /* Calculate timeout for MPTCP active disable
     481                 :             :  * Return true if we are still in the active MPTCP disable period
     482                 :             :  * Return false if timeout already expired and we should use active MPTCP
     483                 :             :  */
     484                 :        1780 : bool mptcp_active_should_disable(struct sock *ssk)
     485                 :             : {
     486                 :        1780 :         struct net *net = sock_net(ssk);
     487                 :        1780 :         unsigned int blackhole_timeout;
     488                 :        1780 :         struct mptcp_pernet *pernet;
     489                 :        1780 :         unsigned long timeout;
     490                 :        1780 :         int disable_times;
     491                 :        1780 :         int multiplier;
     492                 :        1780 :         u64 delay;
     493                 :             : 
     494                 :        1780 :         pernet = mptcp_get_pernet(net);
     495                 :        1780 :         blackhole_timeout = READ_ONCE(pernet->blackhole_timeout);
     496                 :             : 
     497       [ + ][ + ]:        1780 :         if (!blackhole_timeout)
     498                 :             :                 return false;
     499                 :             : 
     500            [ + ]:        1774 :         disable_times = atomic_read(&pernet->active_disable_times);
     501       [ + ][ + ]:        1774 :         if (!disable_times)
     502                 :             :                 return false;
     503                 :             : 
     504                 :             :         /* Paired with smp_mb__before_atomic() in mptcp_active_disable() */
     505                 :          18 :         smp_rmb();
     506                 :             : 
     507                 :             :         /* Limit timeout to max: 2^6 * initial timeout */
     508            [ + ]:          18 :         multiplier = 1 << min(disable_times - 1, 6);
     509                 :          18 :         delay = (u64)multiplier * blackhole_timeout * HZ;
     510                 :             : 
     511                 :             :         /* Paired with the WRITE_ONCE() in mptcp_active_disable(). */
     512                 :          18 :         timeout = READ_ONCE(pernet->active_disable_stamp) +
     513                 :          18 :                   min(delay, MAX_JIFFY_OFFSET);
     514                 :             : 
     515       [ + ][ + ]:          24 :         return time_before(jiffies, timeout);
     516                 :             : }
     517                 :             : 
     518                 :             : /* Enable active MPTCP and reset active_disable_times if needed */
     519                 :        1520 : void mptcp_active_enable(struct sock *sk)
     520                 :             : {
     521                 :        1520 :         struct mptcp_pernet *pernet = mptcp_get_pernet(sock_net(sk));
     522                 :             : 
     523       [ + ][ + ]:        1520 :         if (atomic_read(&pernet->active_disable_times)) {
     524                 :           6 :                 struct net_device *dev;
     525                 :           6 :                 struct dst_entry *dst;
     526                 :             : 
     527                 :           6 :                 rcu_read_lock();
     528            [ + ]:           6 :                 dst = __sk_dst_get(sk);
     529       [ + ][ + ]:           6 :                 dev = dst ? dst_dev_rcu(dst) : NULL;
     530 [ #  # ][ #  # ]:           0 :                 if (!(dev && (dev->flags & IFF_LOOPBACK)))
     531                 :           6 :                         atomic_set(&pernet->active_disable_times, 0);
     532                 :           6 :                 rcu_read_unlock();
     533                 :             :         }
     534                 :        1520 : }
     535                 :             : 
     536                 :             : /* Check the number of retransmissions, and fallback to TCP if needed */
     537                 :         943 : void mptcp_active_detect_blackhole(struct sock *ssk, bool expired)
     538                 :             : {
     539                 :         943 :         struct mptcp_subflow_context *subflow;
     540                 :         943 :         u8 timeouts, to_max;
     541                 :         943 :         struct net *net;
     542                 :             : 
     543                 :             :         /* Only check MPTCP SYN ... */
     544      [ +  -  + ]:         943 :         if (likely(!sk_is_mptcp(ssk) || ssk->sk_state != TCP_SYN_SENT))
                 [ -  + ]
     545                 :             :                 return;
     546                 :             : 
     547       [ + ][ + ]:         125 :         subflow = mptcp_subflow_ctx(ssk);
     548                 :             : 
     549                 :             :         /* ... + MP_CAPABLE */
     550       [ + ][ + ]:         125 :         if (!subflow->request_mptcp) {
     551                 :             :                 /* Mark as blackhole iif the 1st non-MPTCP SYN is accepted */
     552                 :          63 :                 subflow->mpc_drop = 0;
     553                 :          63 :                 return;
     554                 :             :         }
     555                 :             : 
     556                 :          62 :         net = sock_net(ssk);
     557                 :          62 :         timeouts = inet_csk(ssk)->icsk_retransmits;
     558                 :          62 :         to_max = READ_ONCE(mptcp_get_pernet(net)->syn_retrans_before_tcp_fallback);
     559                 :             : 
     560 [ +  + ][ +  + ]:          62 :         if (timeouts == to_max || (timeouts < to_max && expired)) {
     561                 :          30 :                 subflow->mpc_drop = 1;
     562       [ + ][ + ]:          30 :                 mptcp_early_fallback(mptcp_sk(subflow->conn), subflow,
     563                 :             :                                      MPTCP_MIB_MPCAPABLEACTIVEDROP);
     564                 :             :         }
     565                 :             : }
     566                 :             : 
     567                 :        1426 : static int __net_init mptcp_net_init(struct net *net)
     568                 :             : {
     569                 :        1426 :         struct mptcp_pernet *pernet = mptcp_get_pernet(net);
     570                 :             : 
     571                 :        1426 :         mptcp_pernet_set_defaults(pernet);
     572                 :             : 
     573                 :        1426 :         return mptcp_pernet_new_table(net, pernet);
     574                 :             : }
     575                 :             : 
     576                 :             : /* Note: the callback will only be called per extra netns */
     577                 :        1344 : static void __net_exit mptcp_net_exit(struct net *net)
     578                 :             : {
     579                 :        1344 :         struct mptcp_pernet *pernet = mptcp_get_pernet(net);
     580                 :             : 
     581                 :        1344 :         mptcp_pernet_del_table(pernet);
     582                 :        1344 : }
     583                 :             : 
     584                 :             : static struct pernet_operations mptcp_pernet_ops = {
     585                 :             :         .init = mptcp_net_init,
     586                 :             :         .exit = mptcp_net_exit,
     587                 :             :         .id = &mptcp_pernet_id,
     588                 :             :         .size = sizeof(struct mptcp_pernet),
     589                 :             : };
     590                 :             : 
     591                 :           6 : void __init mptcp_init(void)
     592                 :             : {
     593                 :           6 :         mptcp_join_cookie_init();
     594                 :           6 :         mptcp_proto_init();
     595                 :             : 
     596            [ + ]:           6 :         if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
     597                 :           0 :                 panic("Failed to register MPTCP pernet subsystem.\n");
     598                 :           6 : }
     599                 :             : 
     600                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
     601                 :           6 : int __init mptcpv6_init(void)
     602                 :             : {
     603                 :           6 :         int err;
     604                 :             : 
     605                 :           6 :         err = mptcp_proto_v6_init();
     606                 :             : 
     607                 :           6 :         return err;
     608                 :             : }
     609                 :             : #endif
        

Generated by: LCOV version 2.3.1-1