[netfilter-cvslog] r3288 - in trunk/nfsim: core core/ipv4 tools

rusty at netfilter.org rusty at netfilter.org
Sat Nov 20 07:44:02 CET 2004


Author: rusty at netfilter.org
Date: 2004-11-20 07:44:02 +0100 (Sat, 20 Nov 2004)
New Revision: 3288

Modified:
   trunk/nfsim/core/core.c
   trunk/nfsim/core/core.h
   trunk/nfsim/core/ipv4/ipv4.c
   trunk/nfsim/core/ipv4/ipv4.h
   trunk/nfsim/tools/gen_ip.c
   trunk/nfsim/tools/ifconfig.c
Log:
Enhancements for valgrind:
1) Centralize interface creation in ifconfig.c, and use it for initial dvices.
2) Initialize all fields in devices.
3) Don't use getprotobyname in tools/gen_ip.c: valgrind gives dlclose warning on exit.


Modified: trunk/nfsim/core/core.c
===================================================================
--- trunk/nfsim/core/core.c	2004-11-20 06:41:44 UTC (rev 3287)
+++ trunk/nfsim/core/core.c	2004-11-20 06:44:02 UTC (rev 3288)
@@ -70,39 +70,40 @@
 static int queueid;
 LIST_HEAD(nfsim_queue);
 
+LIST_HEAD(interfaces);
 
-#define DEFAULT_DEVICES 3
-static const char *default_devnames[DEFAULT_DEVICES] = { "lo", "eth0", "eth1"};
+/* From tools/ifconfig.c */
+extern struct net_device *create_device(const char *name, int argc, char **);
+struct net_device *loopback_dev_p;
 
-struct net_device loopback_dev = {
-	.name                   = "lo",
-	.hard_header_len        = ETH_HLEN,     /* 14   */
-};
-
-LIST_HEAD(interfaces);
-
 int core_init(void)
 {
 	int i, h;
+	char *argv[3];
 
+	/* ifconfig lo 127.0.0.1 8 127.255.255.255 up */
+	argv[0] = "127.0.0.1";
+	argv[1] = "8";
+	argv[2] = "127.255.255.255";
+	loopback_dev_p = create_device("lo", 3, argv);
+
+	/* ifconfig eth0 192.168.0.1 24 192.168.0.255 up */
+	argv[0] = "192.168.0.1";
+	argv[1] = "24";
+	argv[2] = "192.168.0.255";
+	create_device("eth0", 3, argv);
+
+	/* ifconfig eth1 192.168.1.1 24 192.168.1.255 up */
+	argv[0] = "192.168.1.1";
+	argv[1] = "24";
+	argv[2] = "192.168.1.255";
+	create_device("eth1", 3, argv);
+
 	for (i = 0; i < NPROTO; i++) {
 		for (h = 0; h < NF_MAX_HOOKS; h++)
 			INIT_LIST_HEAD(&nf_hooks[i][h]);
 	}
 
-	list_add_tail(&loopback_dev.entry, &interfaces);
-
-	for (i = 1; i < DEFAULT_DEVICES; i++) {
-		struct net_device *dev;
-
-		dev = talloc(NULL, struct net_device);
-		strncpy(dev->name, default_devnames[i], IFNAMSIZ);
-		dev->ifindex = i;
-
-		list_add_tail(&dev->entry, &interfaces);
-
-	}
-
 	nfsim_log(LOG_UI, "core_init() completed");
 	return 0;
 }

Modified: trunk/nfsim/core/core.h
===================================================================
--- trunk/nfsim/core/core.h	2004-11-20 06:41:44 UTC (rev 3287)
+++ trunk/nfsim/core/core.h	2004-11-20 06:44:02 UTC (rev 3288)
@@ -35,7 +35,9 @@
 
 struct net_device *interface_by_name(const char *name);
 
-extern struct net_device loopback_dev;
+/* This should be enough to fool you all.  Bwahahahahah! */
+extern struct net_device *loopback_dev_p;
+#define loopback_dev (*loopback_dev_p)
 
 /**
  * allow protocols modules to send packets.

Modified: trunk/nfsim/core/ipv4/ipv4.c
===================================================================
--- trunk/nfsim/core/ipv4/ipv4.c	2004-11-20 06:41:44 UTC (rev 3287)
+++ trunk/nfsim/core/ipv4/ipv4.c	2004-11-20 06:44:02 UTC (rev 3288)
@@ -41,6 +41,18 @@
 	return 0;
 }
 
+void add_route_for_device(struct in_device *indev)
+{
+	struct ipv4_route *route;
+
+	route = talloc(indev, struct ipv4_route);
+	talloc_set_destructor(route, destroy_route);
+	route->netmask = indev->ifa_list->ifa_mask;
+	route->network = indev->ifa_list->ifa_address & indev->ifa_list->ifa_mask;
+	route->interface = indev->dev;
+	list_add_tail(&route->entry, &routes);
+}
+
 static int destroy_rtable(void *r)
 {
 	struct rtable *rt = r, **p;
@@ -61,9 +73,6 @@
  */
 int init(void)
 {
-	int i;
-	struct net_device *dev;
-
 	/* name our hooks */
 	nf_hooknames[PF_INET][0] = "NF_IP_PRE_ROUTING";
 	nf_hooknames[PF_INET][1] = "NF_IP_LOCAL_IN";
@@ -71,49 +80,6 @@
 	nf_hooknames[PF_INET][3] = "NF_IP_LOCAL_OUT";
 	nf_hooknames[PF_INET][4] = "NF_IP_POST_ROUTING";
 
-
-	/* set up a basic route for each interface */
-
-	i = 0;
-
-	list_for_each_entry(dev, &interfaces, entry) {
-		struct ipv4_route *route;
-		struct in_device *ifdev;
-		struct in_ifaddr *ifaddr;
-
-		ifdev = talloc(NULL, struct in_device);
-		dev->ip_ptr = ifdev;
-
-		ifaddr = talloc(ifdev, struct in_ifaddr);
-
-		ifdev->ifa_list = ifaddr;
-		
-		ifaddr->ifa_next = NULL;
-		ifaddr->ifa_dev = ifdev;
-
-		if (i) {
-			ifaddr->ifa_local = ifaddr->ifa_address =
-				htonl(0xc0a80001 + ((i-1) << 8));
-			ifaddr->ifa_mask = htonl(0xffffff00);
-		} else {
-			ifaddr->ifa_local = ifaddr->ifa_address =
-				htonl(0x7f000001);
-			ifaddr->ifa_mask = htonl(0xff000000);
-		}
-
-		i++;
-		ifaddr->ifa_broadcast =
-			(ifaddr->ifa_address & ifaddr->ifa_mask) +
-			(0xffffffff & ~ifaddr->ifa_mask);
-
-		route = talloc(ifdev, struct ipv4_route);
-		talloc_set_destructor(route, destroy_route);
-		route->netmask = ifaddr->ifa_mask;
-		route->network = ifaddr->ifa_address & ifaddr->ifa_mask;
-		route->interface = dev;
-		list_add_tail(&route->entry, &routes);
-	}
-		
 	return 0;
 }
 

Modified: trunk/nfsim/core/ipv4/ipv4.h
===================================================================
--- trunk/nfsim/core/ipv4/ipv4.h	2004-11-20 06:41:44 UTC (rev 3287)
+++ trunk/nfsim/core/ipv4/ipv4.h	2004-11-20 06:44:02 UTC (rev 3288)
@@ -605,6 +605,7 @@
 
 char *ipv4_describe_packet(struct sk_buff *skb);
 
+void add_route_for_device(struct in_device *indev);
 
 inline unsigned short ip_compute_csum(unsigned char * buff, int len);
 

Modified: trunk/nfsim/tools/gen_ip.c
===================================================================
--- trunk/nfsim/tools/gen_ip.c	2004-11-20 06:41:44 UTC (rev 3287)
+++ trunk/nfsim/tools/gen_ip.c	2004-11-20 06:44:02 UTC (rev 3288)
@@ -82,10 +82,11 @@
 
 	if (proto != -1) return (unsigned short)proto;
 
+#if 0 				/* Messes up valgrind for some reason? */
 	pent = getprotobyname(s);
 	if (pent != NULL)
 		return pent->p_proto;
-
+#endif
 	for (pp = &chain_protos[0]; pp->name; pp++)
 		if (!strcasecmp(s, pp->name))
 			return pp->num;

Modified: trunk/nfsim/tools/ifconfig.c
===================================================================
--- trunk/nfsim/tools/ifconfig.c	2004-11-20 06:41:44 UTC (rev 3287)
+++ trunk/nfsim/tools/ifconfig.c	2004-11-20 06:44:02 UTC (rev 3288)
@@ -24,6 +24,8 @@
 #include <ipv4/ipv4.h>
 #include <utils.h>
 
+static int ifindex_counter = 1;
+
 static bool ifshow(struct net_device *dev)
 {
 	struct in_device *indev;
@@ -52,7 +54,6 @@
 	return true;
 }
 
-
 static bool set_device(struct net_device *dev, int argc, char **argv)
 {
 	struct in_ifaddr *ifaddr;
@@ -113,7 +114,35 @@
 	return true;
 }
 
+/* Used by the core to create default devices. */
+struct net_device *create_device(const char *name, int argc, char **argv)
+{
+	struct net_device *dev;
+	struct in_device *indev;
 
+	dev = talloc(NULL, struct net_device);
+	indev = talloc(dev, struct in_device);
+
+	strncpy(dev->name, name, IFNAMSIZ);
+	dev->ifindex = ifindex_counter++;
+	dev->ip_ptr = indev;
+	dev->hard_header_len = ETH_ALEN;
+	memset(&dev->stats, 0, sizeof(dev->stats));
+
+	indev->ifa_list = talloc(indev, struct in_ifaddr);
+	indev->ifa_list->ifa_next = NULL;
+	indev->dev = dev;
+
+	if (!set_device(dev, argc, argv)) {
+		talloc_free(dev);
+		return NULL;
+	}
+
+	add_route_for_device(indev);
+	list_add_tail(&dev->entry, &interfaces);
+	return dev;
+}
+
 static bool ifconfig(int argc, char **argv)
 {
 	struct net_device *dev;
@@ -128,8 +157,6 @@
 	dev = interface_by_name(argv[1]);
 
 	if (!strncmp(argv[argc-1], "up", 2)) {
-		struct in_device *indev;
-
 		if (dev) {
 			nfsim_log(LOG_ALWAYS, "device '%s' already exists",
 				dev->name);
@@ -141,22 +168,7 @@
 				"not enough arguments to bring up device");
 			return false;
 		}
-
-		dev = talloc(NULL, struct net_device);
-		indev = talloc(dev, struct in_device);
-
-		dev->ip_ptr = indev;
-		indev->ifa_list = talloc(indev, struct in_ifaddr);
-		indev->ifa_list->ifa_next = NULL;
-
-		strncpy(dev->name, argv[1], IFNAMSIZ);
-
-		if (!set_device(dev, 3, argv + 2)) {
-			talloc_free(dev);
-			return false;
-		}
-
-		list_add_tail(&dev->entry, &interfaces);
+		create_device(argv[1], argc-2, argv+2);
 		return true;
 	}
 




More information about the netfilter-cvslog mailing list