[Lguest] [RFC 1/6] lguest: Allow setting IPv4 prefix length

Sakari Ailus sakari.ailus at iki.fi
Sun Sep 2 02:07:43 EST 2012


Signed-off-by: Sakari Ailus <sakari.ailus at iki.fi>
---
 tools/lguest/lguest.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/tools/lguest/lguest.c b/tools/lguest/lguest.c
index f759f4f..a5e01b0 100644
--- a/tools/lguest/lguest.c
+++ b/tools/lguest/lguest.c
@@ -1425,10 +1425,16 @@ static void add_to_bridge(int fd, const char *if_name, const char *br_name)
  * it up so packets will flow, the copies the MAC address into the hwaddr
  * pointer.
  */
-static void configure_device(int fd, const char *tapif, u32 ipaddr)
+static void configure_device(int fd, const char *tapif, u32 ipaddr,
+			     int prefixlen)
 {
 	struct ifreq ifr;
 	struct sockaddr_in sin;
+	u32 mask = 0;
+	int i;
+
+	for (i = 0; i < prefixlen; i++)
+		mask |= 1 << (31 - i);
 
 	memset(&ifr, 0, sizeof(ifr));
 	strcpy(ifr.ifr_name, tapif);
@@ -1439,6 +1445,10 @@ static void configure_device(int fd, const char *tapif, u32 ipaddr)
 	memcpy(&ifr.ifr_addr, &sin, sizeof(sin));
 	if (ioctl(fd, SIOCSIFADDR, &ifr) != 0)
 		err(1, "Setting %s interface address", tapif);
+	sin.sin_addr.s_addr = htonl(mask);
+	memcpy(&ifr.ifr_netmask, &sin, sizeof(sin));
+	if (ioctl(fd, SIOCSIFNETMASK, &ifr) != 0)
+		err(1, "Setting %s interface netmask", tapif);
 	ifr.ifr_flags = IFF_UP;
 	if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0)
 		err(1, "Bringing interface %s up", tapif);
@@ -1533,7 +1543,7 @@ static void setup_tun_net(char *arg)
 		ip = str2ip(arg);
 
 	/* Set up the tun device. */
-	configure_device(ipfd, tapif, ip);
+	configure_device(ipfd, tapif, ip, 24);
 
 	/* Expect Guest to handle everything except UFO */
 	add_feature(dev, VIRTIO_NET_F_CSUM);
-- 
1.7.2.5



More information about the Lguest mailing list