summaryrefslogtreecommitdiffstats
path: root/nixos/tests/networking/router-eap.nix
blob: 3fe2cf2eaaecf6a73fc6db7b92d31211de7e5018 (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
{
  eapCerts,
  wifi ? null,
}:
{ config, pkgs, ... }:
let
  radiusDir =
    let
      eapConfig = builtins.toFile "eap.conf" ''
        eap {
            default_eap_type = tls
            timer_expire = 60
            ignore_unknown_eap_types = no

            tls {
                # Path to CA certificate
                ca_file = ''${certdir}/ca.cert

                # Path to server certificate and private key
                certificate_file = ''${certdir}/server.cert
                private_key_file = ''${certdir}/server.key

                # Enable mutual authentication
                require_client_cert = yes

                # Cipher suite (example)
                ciphers = "DEFAULT"
            }
        }
      '';
      clientsConfig = builtins.toFile "client.conf" ''
        client localhost {
          ipaddr = 127.0.0.1
          secret = insecure
          require_message_authenticator = no
        }
      '';

      # sample users file, not used for eap-tls, only for e.g. eap-ttls
      usersConfig = builtins.toFile "users.conf" ''
        testuser	Cleartext-Password := "supersecret"
      '';

      # this constructs the freeradius config directory
      # it starts with the upstream config, then overwrites certain files
      # and uses the generated certs from eapCerts
      buildScript = pkgs.writeShellApplication {
        name = "builder";

        runtimeInputs = [ pkgs.coreutils ];

        # https://www.shellcheck.net/wiki/SC2154 -- out is referenced but not assigned.
        excludeShellChecks = [ "SC2154" ];

        text = ''
          cp --recursive  ${pkgs.freeradius}/etc/* "$out"
          chmod +w -R "$out"
          cp --force ${eapCerts}/ca.cert "$out/certs/ca.cert"
          cp --force ${eapCerts}/ca.key "$out/certs/ca.key"
          cp --force ${eapCerts}/server.cert "$out/certs/server.cert"
          cp --force ${eapCerts}/server.key "$out/certs/server.key"
          cp --force ${eapConfig} "$out/mods-enabled/eap"
          cp --force ${usersConfig} "$out/users"
          cp --force ${clientsConfig} "$out/clients.conf"
        '';
      };
    in
    derivation {
      name = "radius_dir";
      builder = "${pkgs.bash}/bin/bash";
      args = [ "${buildScript}/bin/builder" ];
      inherit (pkgs) system;
    };
  inherit (pkgs) lib;
  vlanIfs = lib.range 1 (lib.length config.virtualisation.vlans);
  # The hostapd module is Wi-Fi focused, so retain a custom service for wired EAP tests.
  wiredHostapdService =
    let
      hostapdConfig = builtins.toFile "hostapd.conf" ''
        interface=eth1
        driver=wired
        logger_stdout=-1
        logger_stdout_level=1
        debug=2
        dump_file=/tmp/hostapd.dump
        ieee8021x=1
        eap_reauth_period=3600
        use_pae_group_addr=1
        ##### RADIUS configuration ####################################################
        own_ip_addr=127.0.0.1
        nas_identifier=ap.example.com
        auth_server_addr=127.0.0.1
        auth_server_port=1812
        auth_server_shared_secret=insecure
      '';
    in
    {
      description = "IEEE 802.11 Host Access-Point Daemon";
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        ExecStart = "${pkgs.hostapd}/bin/hostapd ${hostapdConfig}";
        Restart = "always";
        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
        RuntimeDirectory = "hostapd";
      };
    };
in
{
  virtualisation.vlans = [
    1
    2
    3
  ];
  boot.kernel.sysctl."net.ipv6.conf.all.forwarding" = true;
  networking = {
    useDHCP = false;
    useNetworkd = true;
    firewall.checkReversePath = true;
    firewall.allowedUDPPorts = [ 547 ];
    interfaces = lib.mkOverride 0 (
      lib.listToAttrs (
        lib.forEach vlanIfs (
          n:
          lib.nameValuePair "eth${toString n}" {
            ipv4.addresses = [
              {
                address = "192.168.${toString n}.1";
                prefixLength = 24;
              }
            ];
            ipv6.addresses = [
              {
                address = "fd00:1234:5678:${toString n}::1";
                prefixLength = 64;
              }
            ];
          }
        )
      )
    );
  };

  services.freeradius = {
    enable = true;
    configDir = radiusDir;
    debug = true;
  };

  services.hostapd = lib.mkIf (wifi != null) {
    enable = true;
    radios.${wifi.interface} = {
      channel = 1;
      networks.${wifi.interface} = {
        inherit (wifi) ssid;
        authentication.mode = "none";
        settings = {
          ieee8021x = true;
          wpa = 2;
          wpa_key_mgmt = "WPA-EAP";
          rsn_pairwise = "CCMP";
          eap_reauth_period = 3600;
          own_ip_addr = "127.0.0.1";
          nas_identifier = "ap.example.com";
          auth_server_addr = "127.0.0.1";
          auth_server_port = 1812;
          auth_server_shared_secret = "insecure";
        };
      };
    };
  };

  systemd.services.hostapd =
    if wifi == null then
      wiredHostapdService
    else
      {
        after = [
          "freeradius.service"
          "vwifi-client.service"
        ];
        wants = [
          "freeradius.service"
          "vwifi-client.service"
        ];
      };
}