Skip to content

Commit ee827e8

Browse files
committed
if: if_init inits the interface from the kernel
if_init_os inits the OS from the interface
1 parent 9066456 commit ee827e8

5 files changed

Lines changed: 54 additions & 23 deletions

File tree

src/if-bsd.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ os_init(void)
138138

139139
int
140140
if_init(__unused struct interface *iface)
141+
{
142+
/* No extra init needed. */
143+
return 0;
144+
}
145+
146+
int
147+
if_init_os(__unused struct interface *iface)
141148
{
142149
/* BSD promotes secondary address by default */
143150
return 0;

src/if-linux.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,33 @@ if_writepathuint(struct dhcpcd_ctx *ctx, const char *path, unsigned int val)
299299

300300
int
301301
if_init(struct interface *ifp)
302+
{
303+
struct dhcpcd_ctx *ctx = ifp->ctx;
304+
struct ifreq ifr = { .ifr_flags = 0 };
305+
int err = 0;
306+
307+
if (ifp->index != 0)
308+
return 0;
309+
310+
/* This is a huge bug in getifaddrs(3) as there
311+
* is no reason why this can't be returned in
312+
* ifa_addr. */
313+
strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
314+
if (ioctl(ctx->pf_inet_fd, SIOCGIFHWADDR, &ifr) == -1) {
315+
logerr("%s: SIOCGIFHWADDR", ifp->name);
316+
err = -1;
317+
} else
318+
ifp->hwtype = ifr.ifr_hwaddr.sa_family;
319+
if (ioctl(ctx->pf_inet_fd, SIOCGIFINDEX, &ifr) == -1) {
320+
logerr("%s: SIOCGIFINDEX", ifp->name);
321+
err = -1;
322+
} else
323+
ifp->index = (unsigned int)ifr.ifr_ifindex;
324+
return err;
325+
}
326+
327+
int
328+
if_init_os(struct interface *ifp)
302329
{
303330
char path[sizeof(PROC_PROMOTE) + IF_NAMESIZE];
304331
int n;

src/if-sun.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ os_init(void)
116116

117117
int
118118
if_init(struct interface *ifp)
119+
{
120+
return 0;
121+
}
122+
123+
int
124+
if_init_os(struct interface *ifp)
119125
{
120126
#ifdef INET
121127
if (if_plumb(RTM_NEWADDR, ifp->ctx, AF_INET, ifp->name) == -1 &&

src/if.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ if_valid_hwaddr(const uint8_t *hwaddr, size_t hwlen)
400400
return false;
401401
}
402402

403-
#if defined(AF_PACKET) && !defined(AF_LINK)
403+
#if !defined(AF_LINK)
404404
static unsigned int
405405
if_check_arphrd(struct interface *ifp, unsigned int active, bool if_noconf)
406406
{
@@ -659,32 +659,22 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, int argc,
659659
ifp->hwlen = sll->sll_halen;
660660
if (ifp->hwlen != 0)
661661
memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
662-
active = if_check_arphrd(ifp, active, if_noconf);
663662
#endif
664663
}
665-
#ifdef __linux__
666-
else {
667-
struct ifreq ifr = { .ifr_flags = 0 };
668-
669-
/* This is a huge bug in getifaddrs(3) as there
670-
* is no reason why this can't be returned in
671-
* ifa_addr. */
672-
strlcpy(ifr.ifr_name, ifa->ifa_name,
673-
sizeof(ifr.ifr_name));
674-
if (ioctl(ctx->pf_inet_fd, SIOCGIFHWADDR, &ifr) == -1)
675-
logerr("%s: SIOCGIFHWADDR", ifa->ifa_name);
676-
ifp->hwtype = ifr.ifr_hwaddr.sa_family;
677-
if (ioctl(ctx->pf_inet_fd, SIOCGIFINDEX, &ifr) == -1)
678-
logerr("%s: SIOCGIFINDEX", ifa->ifa_name);
679-
ifp->index = (unsigned int)ifr.ifr_ifindex;
680-
if_check_arphrd(ifp, active, if_noconf);
664+
665+
if (if_init(ifp) == -1) {
666+
logerr("%s: if_init", ifp->name);
667+
if_free(ifp);
668+
continue;
681669
}
670+
#ifdef AF_PACKET
671+
active = if_check_arphrd(ifp, active, if_noconf);
682672
#endif
683-
673+
684674
if (!(ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST))) {
685675
/* Handle any platform init for the interface */
686-
if (active != IF_INACTIVE && if_init(ifp) == -1) {
687-
logerr("%s: if_init", ifp->name);
676+
if (active != IF_INACTIVE && if_init_os(ifp) == -1) {
677+
logerr("%s: if_init_os", ifp->name);
688678
if_free(ifp);
689679
continue;
690680
}

src/if.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ int if_nametospec(const char *, struct if_spec *);
206206

207207
/* The below functions are provided by if-KERNEL.c */
208208
int os_init(void);
209-
int if_conf(struct interface *);
210-
int if_init(struct interface *);
209+
int if_init(struct interface *); /* init interface from kernel */
210+
int if_init_os(struct interface *); /* init kernel from interface */
211+
int if_conf(struct interface *); /* finish configuration from kermel */
211212
int if_getssid(struct interface *);
212213
int if_ignoregroup(int, const char *);
213214
bool if_ignore(struct dhcpcd_ctx *, const char *);

0 commit comments

Comments
 (0)