As such, store it at discover and stop using if_nametoindex(3).
memset(iface->clientid + 2 + ifl,
0, 4 - ifl);
} else {
- ifl = htonl(if_nametoindex(iface->name));
+ ifl = htonl(iface->index);
memcpy(iface->clientid + 2, &ifl, 4);
}
} else if (len == 0) {
/*
* dhcpcd - DHCP client daemon
- * Copyright (c) 2006-2011 Roy Marples <roy@marples.name>
+ * Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
* All rights reserved
* Redistribution and use in source and binary forms, with or without
char name[IF_NAMESIZE];
struct if_state *state;
+ unsigned int index;
int flags;
sa_family_t family;
unsigned char hwaddr[HWADDR_LEN];
nlm->hdr.nlmsg_type = RTM_NEWADDR;
} else
nlm->hdr.nlmsg_type = RTM_DELADDR;
- if (!(nlm->ifa.ifa_index = if_nametoindex(iface->name))) {
- free(nlm);
- errno = ENODEV;
- return -1;
- }
+ nlm->ifa.ifa_index = iface->index;
nlm->ifa.ifa_family = AF_INET;
nlm->ifa.ifa_prefixlen = inet_ntocidr(*netmask);
/* This creates the aliased interface */
if_route(const struct rt *rt, int action)
{
struct nlmr *nlm;
- unsigned int ifindex;
int retval = 0;
- if (!(ifindex = if_nametoindex(rt->iface->name))) {
- errno = ENODEV;
- return -1;
- }
-
nlm = xzalloc(sizeof(*nlm));
nlm->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
nlm->hdr.nlmsg_type = RTM_NEWROUTE;
add_attr_l(&nlm->hdr, sizeof(*nlm), RTA_GATEWAY,
&rt->gate.s_addr, sizeof(rt->gate.s_addr));
- add_attr_32(&nlm->hdr, sizeof(*nlm), RTA_OIF, ifindex);
+ add_attr_32(&nlm->hdr, sizeof(*nlm), RTA_OIF, rt->iface->index);
add_attr_32(&nlm->hdr, sizeof(*nlm), RTA_PRIORITY, rt->metric);
if (send_netlink(&nlm->hdr) == -1)
cm->cmsg_type = IPV6_PKTINFO;
cm->cmsg_len = CMSG_LEN(sizeof(pi));
memset(&pi, 0, sizeof(pi));
- pi.ipi6_ifindex = if_nametoindex(ifp->name);
+ pi.ipi6_ifindex = ifp->index;
memcpy(CMSG_DATA(cm), &pi, sizeof(pi));
/* Hop limit */
}
for (ifp = ifaces; ifp; ifp = ifp->next)
- if (if_nametoindex(ifp->name) == (unsigned int)pkt.ipi6_ifindex)
+ if (ifp->index == pkt.ipi6_ifindex)
break;
if (ifp == NULL) {
syslog(LOG_ERR,"received RA for unexpected interface from %s",
memset(&su, 0, sizeof(su));
su.sll.sll_family = PF_PACKET;
su.sll.sll_protocol = htons(protocol);
- if (!(su.sll.sll_ifindex = if_nametoindex(iface->name))) {
- errno = ENOENT;
- goto eexit;
- }
+ su.sll.sll_ifindex = iface->index;
/* Install the DHCP filter */
memset(&pf, 0, sizeof(pf));
if (protocol == ETHERTYPE_ARP) {
memset(&su, 0, sizeof(su));
su.sll.sll_family = AF_PACKET;
su.sll.sll_protocol = htons(protocol);
- if (!(su.sll.sll_ifindex = if_nametoindex(iface->name))) {
- errno = ENOENT;
- return -1;
- }
+ su.sll.sll_ifindex = iface->index;
su.sll.sll_hatype = htons(iface->family);
su.sll.sll_halen = iface->hwlen;
if (iface->family == ARPHRD_INFINIBAND)
return len;
}
-struct interface *
-init_interface(const char *ifname)
-{
- struct ifreq ifr;
- struct interface *iface = NULL;
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
- if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == -1)
- goto eexit;
-
- iface = xzalloc(sizeof(*iface));
- strlcpy(iface->name, ifname, sizeof(iface->name));
- iface->flags = ifr.ifr_flags;
- /* We reserve the 100 range for virtual interfaces, if and when
- * we can work them out. */
- iface->metric = 200 + if_nametoindex(iface->name);
- if (getifssid(ifname, iface->ssid) != -1) {
- iface->wireless = 1;
- iface->metric += 100;
- }
-
- if (ioctl(socket_afnet, SIOCGIFMTU, &ifr) == -1)
- goto eexit;
- /* Ensure that the MTU is big enough for DHCP */
- if (ifr.ifr_mtu < MTU_MIN) {
- ifr.ifr_mtu = MTU_MIN;
- strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
- if (ioctl(socket_afnet, SIOCSIFMTU, &ifr) == -1)
- goto eexit;
- }
-
- snprintf(iface->leasefile, sizeof(iface->leasefile),
- LEASEFILE, ifname);
- /* 0 is a valid fd, so init to -1 */
- iface->raw_fd = -1;
- iface->udp_fd = -1;
- iface->arp_fd = -1;
- goto exit;
-
-eexit:
- free(iface);
- iface = NULL;
-exit:
- return iface;
-}
-
void
free_interface(struct interface *iface)
{
continue;
p = ifa->ifa_name;
}
- if ((ifp = init_interface(p)) == NULL)
- continue;
+
+ ifp = xzalloc(sizeof(*ifp));
+ strlcpy(ifp->name, p, sizeof(ifp->name));
+ ifp->flags = ifa->ifa_flags;
/* Bring the interface up if not already */
if (!(ifp->flags & IFF_UP)
}
#endif
+ ifp->index = sdl->sdl_index;
sdl_type = sdl->sdl_type;
switch(sdl->sdl_type) {
case IFT_BRIDGE: /* FALLTHROUGH */
memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
#elif AF_PACKET
sll = (const struct sockaddr_ll *)(void *)ifa->ifa_addr;
+ ifp->index = sll->sll_index;
ifp->family = sdl_type = sll->sll_hatype;
ifp->hwlen = sll->sll_halen;
if (ifp->hwlen != 0)
continue;
}
+ /* Ensure that the MTU is big enough for DHCP */
+ if (get_mtu(ifp->name) < MTU_MIN &&
+ set_mtu(ifp->name, MTU_MIN) == -1)
+ {
+ syslog(LOG_ERR, "%s: set_mtu: %m", p);
+ free_interface(ifp);
+ continue;
+ }
+
+ /* We reserve the 100 range for virtual interfaces, if and when
+ * we can work them out. */
+ ifp->metric = 200 + ifp->index;
+ if (getifssid(ifp->name, ifp->ssid) != -1) {
+ ifp->wireless = 1;
+ ifp->metric += 100;
+ }
+ snprintf(ifp->leasefile, sizeof(ifp->leasefile),
+ LEASEFILE, ifp->name);
+ /* 0 is a valid fd, so init to -1 */
+ ifp->raw_fd = ifp->udp_fd = ifp->arp_fd = -1;
+
if (ifl)
ifl->next = ifp;
else
size_t hwaddr_aton(unsigned char *, const char *);
int getifssid(const char *, char *);
-struct interface *init_interface(const char *);
struct interface *discover_interfaces(int, char * const *);
void free_interface(struct interface *);
int do_mtu(const char *, short int);