From: Justin Cinkelj <justin.cink...@xlab.si>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

dhcp: remove IP from interface when DHCP release is send

Currently, when DHCP release was send, IP was not removed from VM.
So VM was still responding to ping etc.

Fix this by removing IP from iface. The in_control(, SIOCDIFADDR, ...),
called by stop_if() also removes all routes of corresponding iface.

Signed-off-by: Justin Cinkelj <justin.cink...@xlab.si>
Message-Id: <20161124202854.23662-2-justin.cink...@xlab.si>

---
diff --git a/bsd/porting/networking.cc b/bsd/porting/networking.cc
--- a/bsd/porting/networking.cc
+++ b/bsd/porting/networking.cc
@@ -117,6 +117,44 @@ int start_if(std::string if_name, std::string ip_addr, std::string mask_addr)
     return (error);
 }

+int stop_if(std::string if_name, std::string ip_addr)
+{
+    int error, success;
+    struct in_aliasreq ifra;
+    struct bsd_sockaddr_in* addr      = &ifra.ifra_addr;
+    struct ifnet* ifp;
+
+    if ((if_name.empty()) || (ip_addr.empty())) {
+        return (EINVAL);
+    }
+
+    bzero(&ifra, sizeof(struct in_aliasreq));
+
+    /* IF Name */
+    strncpy(ifra.ifra_name, if_name.c_str(), IFNAMSIZ);
+    ifp = ifunit_ref(if_name.c_str());
+    if (!ifp) {
+        return (ENOENT);
+    }
+
+    // todo check for null
+
+    /* IP Address */
+    success = inet_aton(ip_addr.c_str(), &addr->sin_addr);
+    if (!success) {
+        error = EINVAL;
+        goto out;
+    }
+    addr->sin_family = AF_INET;
+    addr->sin_len = sizeof(struct bsd_sockaddr_in);
+
+    error = in_control(NULL, SIOCDIFADDR, (caddr_t)&ifra, ifp, NULL);
+
+out:
+    if_rele(ifp);
+    return (error);
+}
+
 int ifup(std::string if_name)
 {
     int error;
diff --git a/bsd/porting/networking.hh b/bsd/porting/networking.hh
--- a/bsd/porting/networking.hh
+++ b/bsd/porting/networking.hh
@@ -19,6 +19,7 @@ namespace osv {
     int if_set_mtu(std::string if_name, u16 mtu);
     int start_if(std::string if_name, std::string ip_addr,
         std::string mask_addr);
+    int stop_if(std::string if_name, std::string ip_addr);
     int ifup(std::string if_name);
     std::string if_ip(std::string if_name);
 }
diff --git a/core/dhcp.cc b/core/dhcp.cc
--- a/core/dhcp.cc
+++ b/core/dhcp.cc
@@ -536,6 +536,8 @@ namespace dhcp {
         // Save transaction id & send
         _xid = dm.get_xid();
         _sock->dhcp_send(dm);
+        // IP and routes have to be removed
+        osv::stop_if(_ifp->if_xname, _client_addr.to_string().c_str());
// no reply/ack is expected, after send we just forget all old state
         _client_addr = _server_addr = ipv4_zero;
     }

--
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to