changeset 77:0d143e90c3cb draft

Check that if_nametoindex returns a valid index, thanks to d00mer.
author Roy Marples <roy@marples.name>
date Fri, 23 Mar 2007 10:32:11 +0000
parents 7eccf5c307df
children 3e35e39a5fa3
files ChangeLog interface.c socket.c
diffstat 3 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Mar 23 10:09:14 2007 +0000
+++ b/ChangeLog	Fri Mar 23 10:32:11 2007 +0000
@@ -1,6 +1,7 @@
 When we get an invalid length for a DHCP option, try and continue anyway.
 When MTU is less than 576 we now ignore it instead of setting the MTU to 576.
 Build correctly on dietlibc, thanks to d00mer.
+Check that if_nametoindex returns a valid index, thanks to d00mer.
 
 dhcpcd-3.0.16
 RFC 2131 is full of confusion regarding MTU it seems as the effective minimum
--- a/interface.c	Fri Mar 23 10:09:14 2007 +0000
+++ b/interface.c	Fri Mar 23 10:32:11 2007 +0000
@@ -742,7 +742,12 @@
   if (! del)
     nlm.hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
   nlm.hdr.nlmsg_type = del ? RTM_DELADDR : RTM_NEWADDR;
-  nlm.ifa.ifa_index = if_nametoindex (ifname);
+  if (! (nlm.ifa.ifa_index = if_nametoindex (ifname)))
+    {
+      logger (LOG_ERR, "if_nametoindex: Couldn't find index for interface `%s'",
+	      ifname);
+      return -1;
+    }
   nlm.ifa.ifa_family = AF_INET;
 
   nlm.ifa.ifa_prefixlen = inet_ntocidr (netmask);
@@ -763,6 +768,7 @@
 {
   char *dstd;
   char *gend;
+  unsigned int ifindex;
   struct
     {
       struct nlmsghdr hdr;
@@ -826,7 +832,15 @@
     add_attr_l (&nlm.hdr, sizeof (nlm), RTA_GATEWAY, &gateway.s_addr,
 		sizeof (gateway.s_addr));
 
-  add_attr_32 (&nlm.hdr, sizeof (nlm), RTA_OIF, if_nametoindex (ifname));
+
+  if (! (ifindex = if_nametoindex (ifname)))
+    {
+      logger (LOG_ERR, "if_nametoindex: Couldn't find index for interface `%s'",
+	      ifname);
+      return -1;
+    }
+
+  add_attr_32 (&nlm.hdr, sizeof (nlm), RTA_OIF, ifindex);
   add_attr_32 (&nlm.hdr, sizeof (nlm), RTA_PRIORITY, metric);
 
   return send_netlink (&nlm.hdr);
--- a/socket.c	Fri Mar 23 10:09:14 2007 +0000
+++ b/socket.c	Fri Mar 23 10:32:11 2007 +0000
@@ -463,7 +463,13 @@
     sll.sll_protocol = htons (ETH_P_ARP);
   else
     sll.sll_protocol = htons (ETH_P_IP);
-  sll.sll_ifindex = if_nametoindex (iface->name);
+  if (! (sll.sll_ifindex = if_nametoindex (iface->name)))
+    {
+      logger (LOG_ERR, "if_nametoindex: Couldn't find index for interface `%s'",
+	      iface->name);
+      close (fd);
+      return -1;
+    }
 
   if (bind(fd, (struct sockaddr *) &sll, sizeof (struct sockaddr_ll)) == -1)
     {
@@ -494,7 +500,12 @@
   memset (&sll, 0, sizeof (struct sockaddr_ll));
   sll.sll_family = AF_PACKET;
   sll.sll_protocol = htons (type);
-  sll.sll_ifindex = if_nametoindex (iface->name);
+  if (! (sll.sll_ifindex = if_nametoindex (iface->name)))
+    {
+      logger (LOG_ERR, "if_nametoindex: Couldn't find index for interface `%s'",
+	      iface->name);
+      return -1;
+    }
   sll.sll_halen = ETHER_ADDR_LEN;
   memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));