Skip to content

Commit

Permalink
Add support for WPA-EAP networks
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorRigby committed May 9, 2019
1 parent 32dbec9 commit f969acd
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/vintage_net/technology/wifi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ defmodule VintageNet.Technology.WiFi do

defp key_mgmt_to_string(key) when key in [:none, :wep], do: "NONE"
defp key_mgmt_to_string(:wpa_psk), do: "WPA-PSK"
defp key_mgmt_to_string(:wpa_eap), do: "WPA-EAP"

defp into_wifi_network_config(%{networks: networks}) do
Enum.reduce(networks, "", fn network, config ->
Expand All @@ -120,6 +121,24 @@ defmodule VintageNet.Technology.WiFi do
"""
end

defp into_wifi_network_config(%{key_mgmt: :wpa_eap} = wifi) do
"""
network={
#{into_config_string(wifi, :ssid)}
#{into_config_string(wifi, :key_mgmt)}
#{into_config_string(wifi, :scan_ssid)}
#{into_config_string(wifi, :priority)}
#{into_config_string(wifi, :pairwise)}
#{into_config_string(wifi, :group)}
#{into_config_string(wifi, :eap)}
#{into_config_string(wifi, :identity)}
#{into_config_string(wifi, :password)}
#{into_config_string(wifi, :phase1)}
#{into_config_string(wifi, :phase2)}
}
"""
end

defp into_wifi_network_config(wifi) do
"""
network={
Expand Down Expand Up @@ -160,6 +179,34 @@ defmodule VintageNet.Technology.WiFi do
"priority=#{value}"
end

defp wifi_opt_to_config_string(_wifi, :identity, value) do
"identity=#{value}"
end

defp wifi_opt_to_config_string(_wifi, :password, value) do
"password=#{value}"
end

defp wifi_opt_to_config_string(_wifi, :phase1, value) do
"phase1=#{value}"
end

defp wifi_opt_to_config_string(_wifi, :phase2, value) do
"phase2=#{value}"
end

defp wifi_opt_to_config_string(_wifi, :pairwise, value) do
"pairwise=#{value}"
end

defp wifi_opt_to_config_string(_wifi, :group, value) do
"group=#{value}"
end

defp wifi_opt_to_config_string(_wifi, :eap, value) do
"eap=#{value}"
end

# TODO: Remove duplication with ethernet!!
defp dhcp_options(hostname) do
"""
Expand Down
61 changes: 61 additions & 0 deletions test/vintage_net/config_wifi_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,67 @@ defmodule VintageNet.ConfigWiFiTest do
assert {:ok, output} == WiFi.to_raw_config("wlan0", input, default_opts())
end

test "create a EAP network" do
input = %{
type: VintageNet.Technology.WiFi,
wifi: %{
ssid: "testing",
mode: :client,
key_mgmt: :wpa_eap,
scan_ssid: 1,
pairwise: "CCMP TKIP",
group: "CCMP TKIP",
eap: "PEAP",
identity: "user1",
password: "supersecret",
phase1: "peapver=auto",
phase2: "MSCHAPV2"
},
ipv4: %{method: :dhcp},
hostname: "unit_test"
}

output = %RawConfig{
ifname: "wlan0",
type: VintageNet.Technology.WiFi,
source_config: input,
child_specs: [{VintageNet.Interface.ConnectivityChecker, "wlan0"}],
files: [
{"/tmp/vintage_net/network_interfaces.wlan0", dhcp_interface("wlan0", "unit_test")},
{"/tmp/vintage_net/wpa_supplicant.conf.wlan0",
"""
ctrl_interface=/tmp/vintage_net/wpa_supplicant
country=00
network={
ssid="testing"
key_mgmt=WPA-EAP
scan_ssid=1
pairwise=CCMP TKIP
group=CCMP TKIP
eap=PEAP
identity=user1
password=supersecret
phase1=peapver=auto
phase2=MSCHAPV2
}
"""}
],
up_cmds: [
{:run, "/usr/sbin/wpa_supplicant",
["-B", "-i", "wlan0", "-c", "/tmp/vintage_net/wpa_supplicant.conf.wlan0", "-dd"]},
{:run, "/sbin/ifup", ["-i", "/tmp/vintage_net/network_interfaces.wlan0", "wlan0"]}
],
down_cmds: [
{:run, "/sbin/ifdown", ["-i", "/tmp/vintage_net/network_interfaces.wlan0", "wlan0"]},
{:run, "/usr/bin/killall", ["-q", "wpa_supplicant"]}
],
cleanup_files: ["/tmp/vintage_net/wpa_supplicant/wlan0"]
}

assert {:ok, output} == WiFi.to_raw_config("wlan0", input, default_opts())
end

test "create a multi-network WiFi configuration" do
# All of the IPv4 settings need to be the same for this configuration. This is
# probably "good enough". `nerves_network` does better, though.
Expand Down

0 comments on commit f969acd

Please sign in to comment.