Skip to contentSkip to conversation

Add DHCP to AP

PATCH Superseded James Prestwood 2 participants 4 messages Oct 21, 2020 – Oct 22, 2020

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add DHCP to AP

Removed all the provisioning file changes for now and left just the minimum require for a DHCP server. All default values will be chosen.

James Prestwood (2):
ap: add support for DHCPv4 server
auto-t: add AP test with DHCP server

 autotests/testAP-DHCP/connection_test.py | 76 ++++++++++++++++++++++++
 autotests/testAP-DHCP/failure_test.py    | 57 ++++++++++++++++++
 autotests/testAP-DHCP/hw.conf            |  3 +
 autotests/testAP-DHCP/main.conf          |  5 ++
 src/ap.c                                 | 70 ++++++++++++++++++++--
 5 files changed, 205 insertions(+), 6 deletions(-)
 create mode 100644 autotests/testAP-DHCP/connection_test.py
 create mode 100644 autotests/testAP-DHCP/failure_test.py
 create mode 100644 autotests/testAP-DHCP/hw.conf
 create mode 100644 autotests/testAP-DHCP/main.conf
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/2 ap: add support for DHCPv4 server

The DHCP server can be enabled by enabling network configuration with [General].EnableNetworkConfiguration. For now, the default DHCP settings will be used. The only requirement is that the AP network interface must have an IP set prior to calling Start().

---
 src/ap.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 64 insertions(+), 6 deletions(-)
src/ap.c +64 -6
index 3c4ae907..132cc44c 100644
@@ -74,6 +74,8 @@ struct ap_state {
 	uint16_t last_aid;
 	struct l_queue *sta_states;
 
+	struct l_dhcp_server *server;
+
 	bool started : 1;
 	bool gtk_set : 1;
 };
@@ -192,6 +194,9 @@ static void ap_reset(struct ap_state *ap)
 	l_queue_destroy(ap->wsc_pbc_probes, l_free);
 
 	ap->started = false;
+
+	if (ap->server)
+		l_dhcp_server_stop(ap->server);
 }
 
 static void ap_del_station(struct sta_state *sta, uint16_t reason,
@@ -1900,6 +1905,49 @@ static void ap_deauth_cb(const struct mmpdu_header *hdr, const void *body,
 	ap_sta_free(sta);
 }
 
+static void do_debug(const char *str, void *user_data)
+{
+	const char *prefix = user_data;
+
+	l_info("%s%s", prefix, str);
+}
+
+static bool ap_start_dhcp(struct ap_state *ap)
+{
+	uint32_t ifindex = netdev_get_ifindex(ap->netdev);
+	const struct l_settings *settings = iwd_get_config();
+	bool enabled = false;
+
+	/*
+	 * Reusing [General].EnableNetworkConfiguration as a switch to enable
+	 * DHCP server. If no value is found or it is false do not create a
+	 * DHCP server.
+	 */
+	if (!l_settings_get_bool(settings, "General",
+					"EnableNetworkConfiguration", &enabled))
+		return true;
+
+	if (!enabled)
+		return true;
+
+	ap->server = l_dhcp_server_new(ifindex);
+	if (!ap->server) {
+		l_error("Failed to create DHCP server on %u", ifindex);
+		return false;
+	}
+
+	if (getenv("IWD_DHCP_DEBUG"))
+		l_dhcp_server_set_debug(ap->server, do_debug,
+							"[DHCPv4 SERV] ", NULL);
+
+	/*
+	 * TODO: Add support for provisioning files where user could configure
+	 * specific DHCP sever settings.
+	 */
+
+	return l_dhcp_server_start(ap->server);
+}
+
 static void ap_start_cb(struct l_genl_msg *msg, void *user_data)
 {
 	struct ap_state *ap = user_data;
@@ -1909,16 +1957,24 @@ static void ap_start_cb(struct l_genl_msg *msg, void *user_data)
 	if (l_genl_msg_get_error(msg) < 0) {
 		l_error("START_AP failed: %i", l_genl_msg_get_error(msg));
 
-		ap->ops->handle_event(AP_EVENT_START_FAILED, NULL,
-					ap->user_data);
-		ap_reset(ap);
-		l_genl_family_free(ap->nl80211);
-		l_free(ap);
-		return;
+		goto failed;
+	}
+
+	if (!ap_start_dhcp(ap)) {
+		l_error("DHCP server failed to start");
+		goto failed;
 	}
 
 	ap->started = true;
 	ap->ops->handle_event(AP_EVENT_STARTED, NULL, ap->user_data);
+
+	return;
+
+failed:
+	ap->ops->handle_event(AP_EVENT_START_FAILED, NULL, ap->user_data);
+	ap_reset(ap);
+	l_genl_family_free(ap->nl80211);
+	l_free(ap);
 }
 
 static struct l_genl_msg *ap_build_cmd_start_ap(struct ap_state *ap)
@@ -2255,6 +2311,8 @@ void ap_free(struct ap_state *ap)
 {
 	ap_reset(ap);
 	l_genl_family_free(ap->nl80211);
+	if (ap->server)
+		l_dhcp_server_destroy(ap->server);
 	l_free(ap);
 }
 
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH v2 1/2] ap: add support for DHCPv4 server

Hi James,

Quoted 8 lines · James Prestwood

The DHCP server can be enabled by enabling network configuration with [General].EnableNetworkConfiguration. For now, the default DHCP settings will be used. The only requirement is that the AP network interface must have an IP set prior to calling Start().

---
  src/ap.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 64 insertions(+), 6 deletions(-)

<snip>

Quoted 13 lines
src/ap.c +13 -0
+static bool ap_start_dhcp(struct ap_state *ap)
+{
+	uint32_t ifindex = netdev_get_ifindex(ap->netdev);
+	const struct l_settings *settings = iwd_get_config();
+	bool enabled = false;
+
+	/*
+	 * Reusing [General].EnableNetworkConfiguration as a switch to enable
+	 * DHCP server. If no value is found or it is false do not create a
+	 * DHCP server.
+	 */
+	if (!l_settings_get_bool(settings, "General",
+					"EnableNetworkConfiguration", &enabled))

src/ap.c static void ap_deauth_cb(const struct mmpdu_header *hdr, const void *body, · line 1937

Quoted 10 lines
src/ap.c +10 -0
+		return true;
+
+	if (!enabled)
+		return true;
+
+	ap->server = l_dhcp_server_new(ifindex);
+	if (!ap->server) {
+		l_error("Failed to create DHCP server on %u", ifindex);
+		return false;
+	}

Hmm, looks like the server is relying on some external entity to setup the IP address. I thought we decided to have ap allocate this and set the address all by itself?

Regards,
-Denis

#3 lore raw

PATCH v2 2/2 auto-t: add AP test with DHCP server

In reply to James Prestwood
---
 autotests/testAP-DHCP/connection_test.py | 76 ++++++++++++++++++++++++
 autotests/testAP-DHCP/failure_test.py    | 57 ++++++++++++++++++
 autotests/testAP-DHCP/hw.conf            |  3 +
 autotests/testAP-DHCP/main.conf          |  5 ++
 4 files changed, 141 insertions(+)
 create mode 100644 autotests/testAP-DHCP/connection_test.py
 create mode 100644 autotests/testAP-DHCP/failure_test.py
 create mode 100644 autotests/testAP-DHCP/hw.conf
 create mode 100644 autotests/testAP-DHCP/main.conf
autotests/testAP-DHCP/connection_test.py new file +76 -0
new file mode 100644
index 00000000..7416b195
@@ -0,0 +1,76 @@
+#! /usr/bin/python3
+
+import unittest
+import sys, os
+
+import iwd
+from iwd import IWD
+from iwd import PSKAgent
+from iwd import NetworkType
+from config import ctx
+import testutil
+
+class Test(unittest.TestCase):
+    def test_connection_success(self):
+        wd = IWD(True)
+
+        dev1, dev2 = wd.list_devices(2)
+
+        ctx.start_process(['ifconfig', dev1.name, "192.168.1.1", "netmask",
+                            "255.255.255.0", "up"], wait=True)
+
+        dev1.start_ap('TestAP2', "Password2")
+
+        try:
+            condition = 'not obj.scanning'
+            wd.wait_for_object_condition(dev2, condition)
+            dev2.scan()
+            condition = 'obj.scanning'
+            wd.wait_for_object_condition(dev2, condition)
+            condition = 'not obj.scanning'
+            wd.wait_for_object_condition(dev2, condition)
+
+            ordered_networks = dev2.get_ordered_networks()
+
+            networks = { n.name: n for n in ordered_networks }
+            self.assertEqual(networks['TestAP2'].type, NetworkType.psk)
+
+            psk_agent = PSKAgent('Password2')
+            wd.register_psk_agent(psk_agent)
+
+            try:
+                dev2.disconnect()
+
+                condition = 'not obj.connected'
+                wd.wait_for_object_condition(dev2, condition)
+            except:
+                pass
+
+            networks['TestAP2'].network_object.connect()
+
+            condition = 'obj.state == DeviceState.connected'
+            wd.wait_for_object_condition(dev2, condition)
+
+            testutil.test_iface_operstate(dev2.name)
+            testutil.test_ifaces_connected(dev1.name, dev2.name, group=False)
+
+            wd.unregister_psk_agent(psk_agent)
+
+            dev2.disconnect()
+
+            condition = 'not obj.connected'
+            wd.wait_for_object_condition(networks['TestAP2'].network_object,
+                                         condition)
+        finally:
+            dev1.stop_ap()
+
+    @classmethod
+    def setUpClass(cls):
+        pass
+
+    @classmethod
+    def tearDownClass(cls):
+        IWD.clear_storage()
+
+if __name__ == '__main__':
+    unittest.main(exit=True)
autotests/testAP-DHCP/failure_test.py new file +57 -0

57 lines not on this page · raw message

autotests/testAP-DHCP/hw.conf new file +3 -0
new file mode 100644
index 00000000..c6553537
@@ -0,0 +1,3 @@
+[SETUP]
+num_radios=2
+start_iwd=0
autotests/testAP-DHCP/main.conf new file +5 -0
new file mode 100644
index 00000000..25ebe4ff
@@ -0,0 +1,5 @@
+[Scan]
+DisableMacAddressRandomization=true
+
+[General]
+EnableNetworkConfiguration=true
Signature · 1 line
2.26.2