Add configure option to disable link-local IP address autoconfiguration.

Change-Id: I845ec44a305e91a4880dc1266a3d212db0b4737c
Signed-off-by: Oskar Senft <osk@google.com>
diff --git a/configure.ac b/configure.ac
index bba116d..9930889 100644
--- a/configure.ac
+++ b/configure.ac
@@ -76,6 +76,18 @@
     AC_SUBST([OESDK_TESTCASE_FLAGS], [$testcase_flags])
 )
 
+# If set, auto-configure a link-local address on the NIC.
+AC_ARG_ENABLE([link-local-autoconfiguration],
+    AS_HELP_STRING([--disable-link-local-autoconfiguration], [Disable link-local IP address autoconfiguration])
+)
+
+AC_ARG_VAR(LINK_LOCAL_AUTOCONFIGURATION, [Enable link-local address autoconfiguration])
+
+AS_IF([test "x$enable_link_local_autoconfiguration" != "xno"],
+      [LINK_LOCAL_AUTOCONFIGURATION="yes"]
+      AC_DEFINE_UNQUOTED([LINK_LOCAL_AUTOCONFIGURATION], ["$LINK_LOCAL_AUTOCONFIGURATION"], [Enable link-local IP address autoconfiguration])
+)
+
 AC_ARG_VAR(DNS_ENTRY_FILE, [File having DNS entries supplied by DHCP])
 AS_IF([test "x$DNS_ENTRY_FILE" == "x"], [DNS_ENTRY_FILE="/run/systemd/netif/state"])
 AC_DEFINE_UNQUOTED([DNS_ENTRY_FILE], ["$DNS_ENTRY_FILE"], [File having DNS entries supplied by DHCP])
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index d887bf5..b70db40 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -555,7 +555,11 @@
 
     // write the network section
     stream << "[" << "Network" << "]\n";
+#ifdef LINK_LOCAL_AUTOCONFIGURATION
     stream << "LinkLocalAddressing=yes\n";
+#else
+    stream << "LinkLocalAddressing=no\n";
+#endif
     stream << "IPv6AcceptRA=false\n";
 
     // Add the VLAN entry
@@ -587,7 +591,11 @@
         // Static
         for (const auto& addr : addrs)
         {
-            if (addr.second->origin() == AddressOrigin::Static)
+            if (addr.second->origin() == AddressOrigin::Static
+#ifndef LINK_LOCAL_AUTOCONFIGURATION
+                || addr.second->origin() == AddressOrigin::LinkLocal
+#endif
+            )
             {
                 std::string address = addr.second->address() + "/" +
                                     std::to_string(addr.second->prefixLength());
diff --git a/ipaddress.cpp b/ipaddress.cpp
index 9244b24..0d1c9ae 100644
--- a/ipaddress.cpp
+++ b/ipaddress.cpp
@@ -1,3 +1,4 @@
+#include "config.h"
 #include "ipaddress.hpp"
 #include "ethernet_interface.hpp"
 #include "util.hpp"
@@ -45,6 +46,7 @@
         return;
     }
 
+#ifdef LINK_LOCAL_AUTOCONFIGURATION
     if (isLinkLocalIP(address()))
     {
         log<level::ERR>("Can not delete the LinkLocal address"),
@@ -52,6 +54,7 @@
                   parent.interfaceName().c_str(), address().c_str());
         return;
     }
+#endif
 
     parent.deleteObject(address());
 }
diff --git a/network_config.cpp b/network_config.cpp
index 84cf3dc..d492bf3 100644
--- a/network_config.cpp
+++ b/network_config.cpp
@@ -1,3 +1,4 @@
+#include "config.h"
 #include "network_config.hpp"
 #include <fstream>
 #include <string>
@@ -16,7 +17,12 @@
 
         filestream.open(filename);
         filestream << "[Match]\nName=" << interface <<
-                "\n[Network]\nDHCP=true\nLinkLocalAddressing=yes\n"
+                "\n[Network]\nDHCP=true\n"
+#ifdef LINK_LOCAL_AUTOCONFIGURATION
+                "LinkLocalAddressing=yes\n"
+#else
+                "LinkLocalAddressing=no\n"
+#endif
                 "IPv6AcceptRA=false\n"
                 "[DHCP]\nClientIdentifier=mac\n";
         filestream.close();