summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/networking/udp-over-tcp.nix
blob: d4a63406f1e486f3272ee2dfea8af33f415a0a09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{
  config,
  lib,
  pkgs,
  ...
}:
let
  inherit (lib)
    attrNames
    escapeShellArgs
    filterAttrs
    getExe'
    last
    literalExpression
    maintainers
    mapAttrs'
    mkOption
    mkPackageOption
    optionals
    splitString
    toInt
    types
    ;

  cfg = config.services.udp-over-tcp;

  commonOptions = {
    openFirewall = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Open the appropriate ports in the firewall.
      '';
    };

    # Options and descriptions as indicated by `tcp2udp --help` and `udp2tcp --help`.
    forward = mkOption {
      type = types.str;
      description = ''
        The IP and port to forward all traffic to.
      '';
    };
    recvBufferSize = mkOption {
      type = types.nullOr types.ints.positive;
      description = ''
        If given, sets the SO_RCVBUF option on the TCP socket to the given number of bytes.
        Changes the size of the operating system's receive buffer associated with the socket.
      '';
      default = null;
    };
    sendBufferSize = mkOption {
      type = types.nullOr types.ints.positive;
      description = ''
        If given, sets the SO_SNDBUF option on the TCP socket to the given number of bytes.
        Changes the size of the operating system's send buffer associated with the socket.
      '';
      default = null;
    };
    recvTimeout = mkOption {
      type = types.nullOr types.ints.positive;
      description = ''
        An application timeout on receiving data from the TCP socket.
      '';
      default = null;
    };
    fwmark = mkOption {
      type = types.nullOr types.ints.u32;
      description = ''
        If given, sets the SO_MARK option on the TCP socket.
      '';
      default = null;
    };
    nodelay = mkOption {
      type = types.bool;
      description = ''
        Enables TCP_NODELAY on the TCP socket.
      '';
      default = false;
    };
  };
  tcp2udpSubmodule = {
    options = commonOptions // {
      threads = mkOption {
        type = types.nullOr types.ints.positive;
        description = ''
          Sets the number of worker threads to use.
          The default value is the number of cores available to the system.
        '';
        default = null;
      };
      bind = mkOption {
        type = types.nullOr types.str;
        description = ''
          Which local IP to bind the UDP socket to.
        '';
        default = null;
      };
    };
  };
  udp2tcpSubmodule = {
    options = commonOptions;
  };

  configToService = type: buildCmdline: listen: conf: {
    name = "${type}-${listen}";
    value = {
      description = "${type} tunnel from ${listen} to ${conf.forward}";
      after = [ "network-online.target" ];
      wants = [ "network-online.target" ];
      wantedBy = [ "multi-user.target" ];
      reloadIfChanged = true;

      serviceConfig = {
        Type = "exec";
        ExecStart = "${getExe' cfg.package type} " + escapeShellArgs (buildCmdline listen conf);

        DynamicUser = true;
        User = "udp-over-tcp";

        # CAP_NET_BIND_SERVICE in case we are binding to ports < 1024, CAP_NET_ADMIN only covers addresses.
        # CAP_NET_ADMIN for setting SO_MARK on the socket.
        AmbientCapabilities = [
          "CAP_NET_ADMIN"
          "CAP_NET_BIND_SERVICE"
        ];
        CapabilityBoundingSet = [
          "CAP_NET_ADMIN"
          "CAP_NET_BIND_SERVICE"
        ];

        Restart = "on-failure";
        RestartSec = 10;

        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateMounts = true;
        PrivateTmp = true;
        PrivateUsers = false;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        RestrictAddressFamilies = [
          "AF_INET"
          "AF_INET6"
        ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = "@system-service";
        UMask = "077";
      };
    };
  };

  buildCommonCmdline =
    listen: conf:
    optionals (conf.recvBufferSize != null) [
      "--recv-buffer"
      conf.recvBufferSize
    ]
    ++ optionals (conf.sendBufferSize != null) [
      "--send-buffer"
      conf.sendBufferSize
    ]
    ++ optionals (conf.recvTimeout != null) [
      "--tcp-recv-timeout"
      conf.recvTimeout
    ]
    ++ optionals (conf.fwmark != null) [
      "--fwmark"
      conf.fwmark
    ]
    ++ optionals conf.nodelay [
      "--nodelay"
    ];
  buildTcp2udpCmdline =
    listen: conf:
    [
      "--tcp-listen"
      listen
      "--udp-forward"
      conf.forward
    ]
    ++ optionals (conf.threads != null) [
      "--threads"
      conf.threads
    ]
    ++ optionals (conf.bind != null) [
      "--udp-bind"
      conf.bind
    ]
    ++ buildCommonCmdline listen conf;
  buildUdp2tcpCmdline =
    listen: conf:
    [
      "--udp-listen"
      listen
      "--tcp-forward"
      conf.forward
    ]
    ++ buildCommonCmdline listen conf;

  getFirewallPorts =
    instances:
    map (e: toInt (last (splitString ":" e))) (
      attrNames (filterAttrs (_: e: e.openFirewall) instances)
    );
in
{
  options.services.udp-over-tcp = {
    package = mkPackageOption pkgs "udp-over-tcp" { };
    tcp2udp = mkOption {
      type = types.attrsOf (types.submodule tcp2udpSubmodule);
      example = literalExpression ''
        {
          "0.0.0.0:443" = {
            forward = "127.0.0.1:51820";
            openFirewall = true;
          };
          "0.0.0.0:444" = {
            threads = 2;
            forward = "127.0.0.1:51821";
            bind = "127.0.0.1";
            recvBufferSize = 16384;
            sendBufferSize = 16384;
            recvTimeout = 10;
            fwmark = 1337;
            nodelay = true;
          };
        }
      '';
      description = ''
        Mapping of TCP listening ports to UDP forwarding ports or configurations.
      '';
      default = { };
    };
    udp2tcp = mkOption {
      type = types.attrsOf (types.submodule udp2tcpSubmodule);
      example = literalExpression ''
        {
          "0.0.0.0:51820" = {
            forward = "10.0.0.1:443";
            openFirewall = true;
          };
          "0.0.0.0:51821" = {
            forward = "10.0.0.1:444";
            recvBufferSize = 16384;
            sendBufferSize = 16384;
            recvTimeout = 10;
            fwmark = 1337;
            nodelay = true;
          };
        }
      '';
      description = ''
        Mapping of UDP listening ports to TCP forwarding ports or configurations.
      '';
      default = { };
    };
  };

  config = {
    systemd.services =
      (mapAttrs' (configToService "tcp2udp" buildTcp2udpCmdline) cfg.tcp2udp)
      // (mapAttrs' (configToService "udp2tcp" buildUdp2tcpCmdline) cfg.udp2tcp);

    networking.firewall.allowedTCPPorts = getFirewallPorts cfg.tcp2udp;
    networking.firewall.allowedUDPPorts = getFirewallPorts cfg.udp2tcp;
  };

  meta.maintainers = with maintainers; [ timschumi ];
}