From: Roy Marples Date: Fri, 23 Mar 2007 10:32:11 +0000 (+0000) Subject: Check that if_nametoindex returns a valid index, thanks to d00mer. X-Git-Tag: v3.2.3~297 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b2e9b3c410a0384a24d230091dfdbbf6b17e9d86;p=thirdparty%2Fdhcpcd.git Check that if_nametoindex returns a valid index, thanks to d00mer. --- diff --git a/ChangeLog b/ChangeLog index 62dd0b1c..a0b86fe4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 diff --git a/interface.c b/interface.c index 1d17819e..d7122354 100644 --- a/interface.c +++ b/interface.c @@ -742,7 +742,12 @@ static int do_address(const char *ifname, 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 @@ static int do_route (const char *ifname, { char *dstd; char *gend; + unsigned int ifindex; struct { struct nlmsghdr hdr; @@ -826,7 +832,15 @@ static int do_route (const char *ifname, 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); diff --git a/socket.c b/socket.c index 2ad97906..a00fda99 100644 --- a/socket.c +++ b/socket.c @@ -463,7 +463,13 @@ int open_socket (interface_t *iface, bool arp) 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 @@ int send_packet (const interface_t *iface, const int type, 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));