]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Remove all instances of if_indextoname and add a replacement
authorRoy Marples <roy@marples.name>
Thu, 18 Sep 2014 00:35:03 +0000 (00:35 +0000)
committerRoy Marples <roy@marples.name>
Thu, 18 Sep 2014 00:35:03 +0000 (00:35 +0000)
if_findindex as we should already have the interface at this point.

dhcpcd.c
if-bsd.c
if-linux.c
if.c
if.h

index 250b9e53395ec53bf208396b256cf607db7ee73d..3a1864b137b1ab4be626b04603445d7db95c7916 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -378,7 +378,7 @@ configure_interface1(struct interface *ifp)
        if (ifo->options & DHCPCD_IPV6RS) {
                ra_global = if_checkipv6(ifp->ctx, NULL,
                    ifp->ctx->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
-               ra_iface = if_checkipv6(ifp->ctx, ifp->name,
+               ra_iface = if_checkipv6(ifp->ctx, ifp,
                    ifp->options->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
                if (ra_global == -1 || ra_iface == -1)
                        ifo->options &= ~DHCPCD_IPV6RS;
index 68e6cd20148e61413f3bd7b0f557a998c8f4323e..18f1b53646f6607c73e8b83e5cf9a0266e1e8987 100644 (file)
--- a/if-bsd.c
+++ b/if-bsd.c
@@ -769,7 +769,7 @@ int
 if_managelink(struct dhcpcd_ctx *ctx)
 {
        /* route and ifwatchd like a msg buf size of 2048 */
-       char msg[2048], *p, *e, *cp, ifname[IF_NAMESIZE];
+       char msg[2048], *p, *e, *cp;
        ssize_t bytes;
        struct rt_msghdr *rtm;
        struct if_announcemsghdr *ifan;
@@ -778,6 +778,7 @@ if_managelink(struct dhcpcd_ctx *ctx)
        struct sockaddr *sa, *rti_info[RTAX_MAX];
        int len;
        struct sockaddr_dl sdl;
+       struct interface *ifp;
 #ifdef INET
        struct rt rt;
 #endif
@@ -816,8 +817,7 @@ if_managelink(struct dhcpcd_ctx *ctx)
 #endif
                case RTM_IFINFO:
                        ifm = (struct if_msghdr *)(void *)p;
-                       memset(ifname, 0, sizeof(ifname));
-                       if (!(if_indextoname(ifm->ifm_index, ifname)))
+                       if ((ifp = if_findindex(ctx, ifm->ifm_index)) == NULL)
                                break;
                        switch (ifm->ifm_data.ifi_link_state) {
                        case LINK_STATE_DOWN:
@@ -838,7 +838,7 @@ if_managelink(struct dhcpcd_ctx *ctx)
                                break;
                        }
                        dhcpcd_handlecarrier(ctx, len,
-                           (unsigned int)ifm->ifm_flags, ifname);
+                           (unsigned int)ifm->ifm_flags, ifp->name);
                        break;
                case RTM_DELETE:
                        if (~rtm->rtm_addrs &
@@ -864,7 +864,7 @@ if_managelink(struct dhcpcd_ctx *ctx)
                case RTM_DELADDR:       /* FALLTHROUGH */
                case RTM_NEWADDR:
                        ifam = (struct ifa_msghdr *)(void *)p;
-                       if (!if_indextoname(ifam->ifam_index, ifname))
+                       if ((ifp = if_findindex(ctx, ifam->ifam_index)) == NULL)
                                break;
                        cp = (char *)(void *)(ifam + 1);
                        get_addrs(ifam->ifam_addrs, cp, rti_info);
@@ -881,7 +881,7 @@ if_managelink(struct dhcpcd_ctx *ctx)
 #endif
                                memcpy(&sdl, rti_info[RTAX_IFA],
                                    rti_info[RTAX_IFA]->sa_len);
-                               dhcpcd_handlehwaddr(ctx, ifname,
+                               dhcpcd_handlehwaddr(ctx, ifp->name,
                                    (const unsigned char*)CLLADDR(&sdl),
                                    sdl.sdl_alen);
                                break;
@@ -892,7 +892,7 @@ if_managelink(struct dhcpcd_ctx *ctx)
                                COPYOUT(rt.net, rti_info[RTAX_NETMASK]);
                                COPYOUT(rt.gate, rti_info[RTAX_BRD]);
                                ipv4_handleifa(ctx, rtm->rtm_type,
-                                   NULL, ifname,
+                                   NULL, ifp->name,
                                    &rt.dest, &rt.net, &rt.gate);
                                break;
 #endif
@@ -902,13 +902,14 @@ if_managelink(struct dhcpcd_ctx *ctx)
                                    rti_info[RTAX_IFA];
                                ia6 = sin6->sin6_addr;
                                if (rtm->rtm_type == RTM_NEWADDR) {
-                                       ifa_flags = if_addrflags6(ifname, &ia6);
+                                       ifa_flags = if_addrflags6(ifp->name,
+                                           &ia6);
                                        if (ifa_flags == -1)
                                                break;
                                } else
                                        ifa_flags = 0;
                                ipv6_handleifa(ctx, rtm->rtm_type, NULL,
-                                   ifname, &ia6, ifa_flags);
+                                   ifp->name, &ia6, ifa_flags);
                                break;
 #endif
                        }
@@ -1048,17 +1049,17 @@ if_raflush(void)
 }
 
 int
-if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
+if_checkipv6(struct dhcpcd_ctx *ctx, const struct interface *ifp, int own)
 {
        int ra;
 
-       if (ifname) {
+       if (ifp) {
 #ifdef ND6_IFF_OVERRIDE_RTADV
                int override;
 #endif
 
 #ifdef ND6_IFF_IFDISABLED
-               if (del_if_nd6_flag(ifname, ND6_IFF_IFDISABLED) == -1) {
+               if (del_if_nd6_flag(ifp->name, ND6_IFF_IFDISABLED) == -1) {
                        syslog(LOG_ERR,
                            "%s: del_if_nd6_flag: ND6_IFF_IFDISABLED: %m",
                            ifname);
@@ -1067,7 +1068,7 @@ if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
 #endif
 
 #ifdef ND6_IFF_PERFORMNUD
-               if (set_if_nd6_flag(ifname, ND6_IFF_PERFORMNUD) == -1) {
+               if (set_if_nd6_flag(ifp->name, ND6_IFF_PERFORMNUD) == -1) {
                        syslog(LOG_ERR,
                            "%s: set_if_nd6_flag: ND6_IFF_PERFORMNUD: %m",
                            ifname);
@@ -1079,24 +1080,24 @@ if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
                if (own) {
                        int all;
 
-                       all = get_if_nd6_flag(ifname, ND6_IFF_AUTO_LINKLOCAL);
+                       all = get_if_nd6_flag(ifp->name,ND6_IFF_AUTO_LINKLOCAL);
                        if (all == -1)
                                syslog(LOG_ERR,
                                    "%s: get_if_nd6_flag: "
                                    "ND6_IFF_AUTO_LINKLOCAL: %m",
-                                   ifname);
+                                   ifp->name);
                        else if (all != 0) {
                                syslog(LOG_DEBUG,
                                    "%s: disabling Kernel IPv6 "
                                    "auto link-local support",
-                                   ifname);
-                               if (del_if_nd6_flag(ifname,
+                                   ifp->name);
+                               if (del_if_nd6_flag(ifp->name,
                                    ND6_IFF_AUTO_LINKLOCAL) == -1)
                                {
                                        syslog(LOG_ERR,
                                            "%s: del_if_nd6_flag: "
                                            "ND6_IFF_AUTO_LINKLOCAL: %m",
-                                           ifname);
+                                           ifp->name);
                                        return -1;
                                }
                        }
@@ -1104,43 +1105,43 @@ if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
 #endif
 
 #ifdef ND6_IFF_OVERRIDE_RTADV
-               override = get_if_nd6_flag(ifname, ND6_IFF_OVERRIDE_RTADV);
+               override = get_if_nd6_flag(ifp->name, ND6_IFF_OVERRIDE_RTADV);
                if (override == -1)
                        syslog(LOG_ERR,
                            "%s: get_if_nd6_flag: ND6_IFF_OVERRIDE_RTADV: %m",
-                           ifname);
+                           ifp->name);
                else if (override == 0 && !own)
                        return 0;
 #endif
 
 #ifdef ND6_IFF_ACCEPT_RTADV
-               ra = get_if_nd6_flag(ifname, ND6_IFF_ACCEPT_RTADV);
+               ra = get_if_nd6_flag(ifp->name, ND6_IFF_ACCEPT_RTADV);
                if (ra == -1)
                        syslog(LOG_ERR,
                            "%s: get_if_nd6_flag: ND6_IFF_ACCEPT_RTADV: %m",
-                           ifname);
+                           ifp->name);
                else if (ra != 0 && own) {
                        syslog(LOG_DEBUG,
                            "%s: disabling Kernel IPv6 RA support",
-                           ifname);
-                       if (del_if_nd6_flag(ifname, ND6_IFF_ACCEPT_RTADV)
+                           ifp->name);
+                       if (del_if_nd6_flag(ifp->name, ND6_IFF_ACCEPT_RTADV)
                            == -1)
                        {
                                syslog(LOG_ERR,
                                    "%s: del_if_nd6_flag: "
                                    "ND6_IFF_ACCEPT_RTADV: %m",
-                                   ifname);
+                                   ifp->name);
                                return ra;
                        }
 #ifdef ND6_IFF_OVERRIDE_RTADV
                        if (override == 0 &&
-                           set_if_nd6_flag(ifname, ND6_IFF_OVERRIDE_RTADV)
+                           set_if_nd6_flag(ifp->name, ND6_IFF_OVERRIDE_RTADV)
                            == -1)
                        {
                                syslog(LOG_ERR,
                                    "%s: set_if_nd6_flag: "
                                    "ND6_IFF_OVERRIDE_RTADV: %m",
-                                   ifname);
+                                   ifp->name);
                                return ra;
                        }
 #endif
index 59a156341f31e6d6893647c4f0a90f4adecfc03e..5dbda2e2ba068706c3613587ff3376c470a2ab1c 100644 (file)
@@ -405,7 +405,7 @@ link_route(struct dhcpcd_ctx *ctx, __unused struct interface *ifp,
     struct nlmsghdr *nlm)
 {
        size_t len;
-       unsigned int idx, metric;
+       unsigned int metric;
        struct rtattr *rta;
        struct rtmsg *rtm;
        struct rt rt;
@@ -449,9 +449,8 @@ link_route(struct dhcpcd_ctx *ctx, __unused struct interface *ifp,
                            sizeof(rt.gate.s_addr));
                        break;
                case RTA_OIF:
-                       idx = *(unsigned int *)RTA_DATA(rta);
-                       if (if_indextoname(idx, ifn))
-                               rt.iface = if_find(ctx, ifn);
+                       rt.iface = if_findindex(ctx,
+                           *(unsigned int *)RTA_DATA(rta));
                        break;
                case RTA_PRIORITY:
                        metric = *(unsigned int *)RTA_DATA(rta);
@@ -495,11 +494,11 @@ link_addr(struct dhcpcd_ctx *ctx, __unused struct interface *ifp,
                return -1;
        }
        ifa = NLMSG_DATA(nlm);
-       if (if_indextoname(ifa->ifa_index, ifn) == NULL)
-               return -1;
-       iface = if_find(ctx, ifn);
-       if (iface == NULL)
+       if (if_findindex(ctx, ifa->ifa_index) == NULL) {
+               /* We don't know about the interface the address is for
+                * so it's not really an error */
                return 1;
+       }
        rta = (struct rtattr *) IFA_RTA(ifa);
        len = NLMSG_PAYLOAD(nlm, sizeof(*ifa));
        switch (ifa->ifa_family) {
@@ -1480,7 +1479,7 @@ add_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
 }
 
 static int
-if_disable_autolinklocal(struct dhcpcd_ctx *ctx, const char *ifname)
+if_disable_autolinklocal(struct dhcpcd_ctx *ctx, int ifindex)
 {
        struct nlml nlm;
        struct rtattr *afs, *afs6;
@@ -1490,7 +1489,7 @@ if_disable_autolinklocal(struct dhcpcd_ctx *ctx, const char *ifname)
        nlm.hdr.nlmsg_type = RTM_NEWLINK;
        nlm.hdr.nlmsg_flags = NLM_F_REQUEST;
        nlm.i.ifi_family = AF_INET6;
-       nlm.i.ifi_index = (int)if_nametoindex(ifname);
+       nlm.i.ifi_index = ifindex;
        afs = add_attr_nest(&nlm.hdr, sizeof(nlm), IFLA_AF_SPEC);
        afs6 = add_attr_nest(&nlm.hdr, sizeof(nlm), AF_INET6);
        add_attr_8(&nlm.hdr, sizeof(nlm), IFLA_INET6_ADDR_GEN_MODE,
@@ -1504,18 +1503,21 @@ if_disable_autolinklocal(struct dhcpcd_ctx *ctx, const char *ifname)
 static const char *prefix = "/proc/sys/net/ipv6/conf";
 
 int
-if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
+if_checkipv6(struct dhcpcd_ctx *ctx, const struct interface *ifp, int own)
 {
+       const char *ifname;
        int ra;
        char path[256];
 
-       if (ifname == NULL)
+       if (ifp == NULL)
                ifname = "all";
        else if (own) {
-               if (if_disable_autolinklocal(ctx, ifname) == -1)
+               if (if_disable_autolinklocal(ctx, ifp->index) == -1)
                        syslog(LOG_DEBUG, "%s: if_disable_autolinklocal: %m",
                            ifname);
        }
+       if (ifp)
+               ifname = ifp->name;
 
        snprintf(path, sizeof(path), "%s/%s/autoconf", prefix, ifname);
        ra = check_proc_int(path);
diff --git a/if.c b/if.c
index 7057419a6d42db119a2b3c50306b60f5d4b03268..59d2010d6b1dceff17c6c3c6d3c786f67cbcb540 100644 (file)
--- a/if.c
+++ b/if.c
@@ -479,7 +479,7 @@ if_discover(struct dhcpcd_ctx *ctx, int argc, char * const *argv)
 }
 
 struct interface *
-if_find(struct dhcpcd_ctx *ctx, const char *ifname)
+if_findindexname(struct dhcpcd_ctx *ctx, unsigned int idx, const char *name)
 {
        struct interface *ifp;
 
@@ -487,13 +487,28 @@ if_find(struct dhcpcd_ctx *ctx, const char *ifname)
                TAILQ_FOREACH(ifp, ctx->ifaces, next) {
                        if ((ifp->options == NULL ||
                            !(ifp->options->options & DHCPCD_PFXDLGONLY)) &&
-                           strcmp(ifp->name, ifname) == 0)
+                           ((name && strcmp(ifp->name, name) == 0) ||
+                           (!name && ifp->index == idx)))
                                return ifp;
                }
        }
        return NULL;
 }
 
+struct interface *
+if_find(struct dhcpcd_ctx *ctx, const char *name)
+{
+
+       return if_findindexname(ctx, 0, name);
+}
+
+struct interface *
+if_findindex(struct dhcpcd_ctx *ctx, unsigned int idx)
+{
+
+       return if_findindexname(ctx, idx, NULL);
+}
+
 int
 if_domtu(const char *ifname, short int mtu)
 {
diff --git a/if.h b/if.h
index 4fcbab7aeea1cb05ba5037ea04661db93f946418..3cdf168807445de3aee862a66e1f205743929332 100644 (file)
--- a/if.h
+++ b/if.h
@@ -94,6 +94,7 @@ int if_setflag(struct interface *ifp, short flag);
 #define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING))
 struct if_head *if_discover(struct dhcpcd_ctx *, int, char * const *);
 struct interface *if_find(struct dhcpcd_ctx *, const char *);
+struct interface *if_findindex(struct dhcpcd_ctx *, unsigned int);
 void if_free(struct interface *);
 int if_domtu(const char *, short int);
 #define if_getmtu(iface) if_domtu(iface, 0)
@@ -131,7 +132,7 @@ int if_route(const struct rt *rt, int);
 #endif
 
 #ifdef INET6
-int if_checkipv6(struct dhcpcd_ctx *ctx, const char *, int);
+int if_checkipv6(struct dhcpcd_ctx *ctx, const struct interface *, int);
 int if_nd6reachable(const char *ifname, struct in6_addr *addr);
 
 int if_address6(const struct ipv6_addr *, int);