2013-04-05 04:31:04 +08:00
|
|
|
/*
|
2008-04-12 00:14:55 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2013-02-04 21:30:35 +08:00
|
|
|
* Copyright (c) 2006-2013 Roy Marples <roy@marples.name>
|
2008-04-12 00:14:55 +08:00
|
|
|
* All rights reserved
|
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2013-06-10 08:56:54 +08:00
|
|
|
#include <sys/param.h>
|
2008-04-12 00:14:55 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_arp.h>
|
2013-05-18 23:30:55 +08:00
|
|
|
#include <netinet/in.h>
|
2013-05-23 16:17:00 +08:00
|
|
|
#ifdef __FreeBSD__ /* Needed so that including netinet6/in6_var.h works */
|
|
|
|
# include <net/if_var.h>
|
|
|
|
#endif
|
2009-04-17 21:31:41 +08:00
|
|
|
#ifdef AF_LINK
|
|
|
|
# include <net/if_dl.h>
|
|
|
|
# include <net/if_types.h>
|
2013-05-18 23:30:55 +08:00
|
|
|
# include <netinet/in_var.h>
|
2009-04-17 21:31:41 +08:00
|
|
|
#endif
|
2009-04-17 20:49:25 +08:00
|
|
|
#ifdef AF_PACKET
|
|
|
|
# include <netpacket/packet.h>
|
|
|
|
#endif
|
2008-07-17 06:23:07 +08:00
|
|
|
#ifdef SIOCGIFMEDIA
|
2009-04-17 20:49:25 +08:00
|
|
|
# include <net/if_media.h>
|
2008-09-02 23:11:01 +08:00
|
|
|
#endif
|
2008-04-12 00:14:55 +08:00
|
|
|
|
2013-05-18 07:09:36 +08:00
|
|
|
#include <net/route.h>
|
|
|
|
#ifdef __linux__
|
|
|
|
# include <asm/types.h> /* for systems with broken headers */
|
|
|
|
# include <linux/rtnetlink.h>
|
|
|
|
#endif
|
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2009-04-17 20:49:25 +08:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <fnmatch.h>
|
2008-04-12 00:14:55 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-09-06 02:24:34 +08:00
|
|
|
#include <syslog.h>
|
2008-04-12 00:14:55 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "dhcp.h"
|
2012-10-12 18:31:51 +08:00
|
|
|
#include "dhcp6.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "if-options.h"
|
2013-08-20 18:29:43 +08:00
|
|
|
#include "ipv6nd.h"
|
2008-04-12 00:14:55 +08:00
|
|
|
#include "net.h"
|
|
|
|
|
2013-02-04 21:30:35 +08:00
|
|
|
int socket_afnet = -1;
|
|
|
|
|
2012-07-06 00:37:41 +08:00
|
|
|
static char hwaddr_buffer[(HWADDR_LEN * 3) + 1 + 1024];
|
2008-09-04 16:31:00 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
char *
|
|
|
|
hwaddr_ntoa(const unsigned char *hwaddr, size_t hwlen)
|
|
|
|
{
|
2008-09-04 16:31:00 +08:00
|
|
|
char *p = hwaddr_buffer;
|
2008-04-12 00:14:55 +08:00
|
|
|
size_t i;
|
|
|
|
|
2012-07-06 00:37:41 +08:00
|
|
|
for (i = 0; i < hwlen; i++) {
|
2008-04-12 00:14:55 +08:00
|
|
|
if (i > 0)
|
|
|
|
*p ++= ':';
|
|
|
|
p += snprintf(p, 3, "%.2x", hwaddr[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
*p ++= '\0';
|
|
|
|
|
2008-09-04 16:31:00 +08:00
|
|
|
return hwaddr_buffer;
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
hwaddr_aton(unsigned char *buffer, const char *addr)
|
|
|
|
{
|
|
|
|
char c[3];
|
|
|
|
const char *p = addr;
|
|
|
|
unsigned char *bp = buffer;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
c[2] = '\0';
|
|
|
|
while (*p) {
|
|
|
|
c[0] = *p++;
|
|
|
|
c[1] = *p++;
|
|
|
|
/* Ensure that digits are hex */
|
2008-04-17 18:08:48 +08:00
|
|
|
if (isxdigit((unsigned char)c[0]) == 0 ||
|
|
|
|
isxdigit((unsigned char)c[1]) == 0)
|
2008-04-17 17:57:19 +08:00
|
|
|
{
|
2008-04-12 00:14:55 +08:00
|
|
|
errno = EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-02 18:29:24 +08:00
|
|
|
/* We should have at least two entries 00:01 */
|
|
|
|
if (len == 0 && *p == '\0') {
|
|
|
|
errno = EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-02 17:48:53 +08:00
|
|
|
/* Ensure that next data is EOL or a seperator with data */
|
|
|
|
if (!(*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-29 18:55:21 +08:00
|
|
|
if (*p)
|
|
|
|
p++;
|
2008-04-12 00:14:55 +08:00
|
|
|
if (bp)
|
|
|
|
*bp++ = (unsigned char)strtol(c, NULL, 16);
|
2008-07-02 18:29:24 +08:00
|
|
|
len++;
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
void
|
2013-02-03 18:35:59 +08:00
|
|
|
free_interface(struct interface *ifp)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2013-02-03 18:35:59 +08:00
|
|
|
|
|
|
|
if (ifp == NULL)
|
2008-09-02 21:28:11 +08:00
|
|
|
return;
|
2013-02-04 06:55:45 +08:00
|
|
|
dhcp_free(ifp);
|
2013-05-18 07:09:36 +08:00
|
|
|
ipv6_free(ifp);
|
2013-02-03 18:35:59 +08:00
|
|
|
dhcp6_free(ifp);
|
2013-08-20 18:29:43 +08:00
|
|
|
ipv6nd_free(ifp);
|
2013-02-03 18:35:59 +08:00
|
|
|
free_options(ifp->options);
|
|
|
|
free(ifp);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2009-10-14 23:16:07 +08:00
|
|
|
int
|
|
|
|
carrier_status(struct interface *iface)
|
|
|
|
{
|
2009-10-15 08:30:54 +08:00
|
|
|
int ret;
|
2009-10-14 23:16:07 +08:00
|
|
|
struct ifreq ifr;
|
|
|
|
#ifdef SIOCGIFMEDIA
|
|
|
|
struct ifmediareq ifmr;
|
|
|
|
#endif
|
|
|
|
#ifdef __linux__
|
|
|
|
char *p;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
|
|
|
|
#ifdef __linux__
|
|
|
|
/* We can only test the real interface up */
|
|
|
|
if ((p = strchr(ifr.ifr_name, ':')))
|
|
|
|
*p = '\0';
|
|
|
|
#endif
|
|
|
|
|
2009-10-15 08:30:54 +08:00
|
|
|
if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == -1)
|
2013-08-18 23:35:40 +08:00
|
|
|
return LINK_UNKNOWN;
|
2009-10-14 23:16:07 +08:00
|
|
|
iface->flags = ifr.ifr_flags;
|
|
|
|
|
2013-05-30 15:51:02 +08:00
|
|
|
ret = LINK_UNKNOWN;
|
2009-10-14 23:16:07 +08:00
|
|
|
#ifdef SIOCGIFMEDIA
|
|
|
|
memset(&ifmr, 0, sizeof(ifmr));
|
|
|
|
strlcpy(ifmr.ifm_name, iface->name, sizeof(ifmr.ifm_name));
|
2009-10-15 08:30:54 +08:00
|
|
|
if (ioctl(socket_afnet, SIOCGIFMEDIA, &ifmr) != -1 &&
|
2009-10-14 23:16:07 +08:00
|
|
|
ifmr.ifm_status & IFM_AVALID)
|
2013-05-30 15:51:02 +08:00
|
|
|
ret = (ifmr.ifm_status & IFM_ACTIVE) ? LINK_UP : LINK_DOWN;
|
2009-10-14 23:16:07 +08:00
|
|
|
#endif
|
2013-05-30 15:51:02 +08:00
|
|
|
if (ret == LINK_UNKNOWN)
|
|
|
|
ret = (ifr.ifr_flags & IFF_RUNNING) ? LINK_UP : LINK_DOWN;
|
2009-10-14 23:16:07 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
up_interface(struct interface *iface)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int retval = -1;
|
|
|
|
#ifdef __linux__
|
|
|
|
char *p;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
|
|
|
|
#ifdef __linux__
|
|
|
|
/* We can only bring the real interface up */
|
|
|
|
if ((p = strchr(ifr.ifr_name, ':')))
|
|
|
|
*p = '\0';
|
|
|
|
#endif
|
2009-10-15 08:30:54 +08:00
|
|
|
if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == 0) {
|
2009-10-14 23:16:07 +08:00
|
|
|
if ((ifr.ifr_flags & IFF_UP))
|
|
|
|
retval = 0;
|
|
|
|
else {
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
2009-10-15 08:30:54 +08:00
|
|
|
if (ioctl(socket_afnet, SIOCSIFFLAGS, &ifr) == 0)
|
2009-10-14 23:16:07 +08:00
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
iface->flags = ifr.ifr_flags;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-02-19 21:37:42 +08:00
|
|
|
struct if_head *
|
2009-04-17 20:49:25 +08:00
|
|
|
discover_interfaces(int argc, char * const *argv)
|
2008-04-12 00:14:55 +08:00
|
|
|
{
|
2009-04-17 20:49:25 +08:00
|
|
|
struct ifaddrs *ifaddrs, *ifa;
|
|
|
|
char *p;
|
2012-03-20 17:19:01 +08:00
|
|
|
int i, sdl_type;
|
2013-02-19 21:37:42 +08:00
|
|
|
struct if_head *ifs;
|
|
|
|
struct interface *ifp;
|
2009-04-17 20:49:25 +08:00
|
|
|
#ifdef __linux__
|
|
|
|
char ifn[IF_NAMESIZE];
|
|
|
|
#endif
|
2013-05-18 23:30:55 +08:00
|
|
|
#ifdef INET6
|
|
|
|
const struct sockaddr_in6 *sin6;
|
|
|
|
int ifa_flags;
|
|
|
|
#endif
|
2009-04-17 20:49:25 +08:00
|
|
|
#ifdef AF_LINK
|
|
|
|
const struct sockaddr_dl *sdl;
|
2010-08-04 14:44:56 +08:00
|
|
|
#ifdef IFLR_ACTIVE
|
|
|
|
struct if_laddrreq iflr;
|
|
|
|
int socket_aflink;
|
|
|
|
|
|
|
|
socket_aflink = socket(AF_LINK, SOCK_DGRAM, 0);
|
|
|
|
if (socket_aflink == -1)
|
|
|
|
return NULL;
|
|
|
|
memset(&iflr, 0, sizeof(iflr));
|
|
|
|
#endif
|
2009-04-17 20:49:25 +08:00
|
|
|
#elif AF_PACKET
|
|
|
|
const struct sockaddr_ll *sll;
|
|
|
|
#endif
|
2013-05-18 23:30:55 +08:00
|
|
|
|
2009-04-17 20:49:25 +08:00
|
|
|
if (getifaddrs(&ifaddrs) == -1)
|
|
|
|
return NULL;
|
2008-04-12 00:14:55 +08:00
|
|
|
|
2013-02-19 21:37:42 +08:00
|
|
|
ifs = malloc(sizeof(*ifs));
|
|
|
|
if (ifs == NULL)
|
|
|
|
return NULL;
|
|
|
|
TAILQ_INIT(ifs);
|
|
|
|
|
2009-04-17 20:49:25 +08:00
|
|
|
for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
|
2009-05-11 15:16:24 +08:00
|
|
|
if (ifa->ifa_addr != NULL) {
|
2009-04-17 20:49:25 +08:00
|
|
|
#ifdef AF_LINK
|
2009-05-11 15:16:24 +08:00
|
|
|
if (ifa->ifa_addr->sa_family != AF_LINK)
|
|
|
|
continue;
|
2009-04-17 20:49:25 +08:00
|
|
|
#elif AF_PACKET
|
2009-05-11 15:16:24 +08:00
|
|
|
if (ifa->ifa_addr->sa_family != AF_PACKET)
|
|
|
|
continue;
|
2009-04-17 20:49:25 +08:00
|
|
|
#endif
|
2009-05-11 15:16:24 +08:00
|
|
|
}
|
|
|
|
|
2009-04-20 02:23:47 +08:00
|
|
|
/* It's possible for an interface to have >1 AF_LINK.
|
|
|
|
* For our purposes, we use the first one. */
|
2013-02-19 21:37:42 +08:00
|
|
|
TAILQ_FOREACH(ifp, ifs, next) {
|
2009-04-20 02:23:47 +08:00
|
|
|
if (strcmp(ifp->name, ifa->ifa_name) == 0)
|
|
|
|
break;
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
2009-04-20 02:23:47 +08:00
|
|
|
if (ifp)
|
|
|
|
continue;
|
2009-04-17 20:49:25 +08:00
|
|
|
if (argc > 0) {
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
#ifdef __linux__
|
|
|
|
/* Check the real interface name */
|
|
|
|
strlcpy(ifn, argv[i], sizeof(ifn));
|
|
|
|
p = strchr(ifn, ':');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
if (strcmp(ifn, ifa->ifa_name) == 0)
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
if (strcmp(argv[i], ifa->ifa_name) == 0)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (i == argc)
|
|
|
|
continue;
|
|
|
|
p = argv[i];
|
|
|
|
} else {
|
2013-06-07 20:44:19 +08:00
|
|
|
p = ifa->ifa_name;
|
2009-09-01 15:45:11 +08:00
|
|
|
/* -1 means we're discovering against a specific
|
|
|
|
* interface, but we still need the below rules
|
|
|
|
* to apply. */
|
|
|
|
if (argc == -1 && strcmp(argv[0], ifa->ifa_name) != 0)
|
|
|
|
continue;
|
2009-04-17 20:49:25 +08:00
|
|
|
}
|
2013-06-07 20:44:19 +08:00
|
|
|
for (i = 0; i < ifdc; i++)
|
|
|
|
if (!fnmatch(ifdv[i], p, 0))
|
|
|
|
break;
|
|
|
|
if (i < ifdc)
|
|
|
|
continue;
|
|
|
|
for (i = 0; i < ifac; i++)
|
|
|
|
if (!fnmatch(ifav[i], p, 0))
|
|
|
|
break;
|
|
|
|
if (ifac && i == ifac)
|
|
|
|
continue;
|
2012-03-30 17:37:21 +08:00
|
|
|
|
2013-02-16 05:45:08 +08:00
|
|
|
ifp = calloc(1, sizeof(*ifp));
|
|
|
|
if (ifp == NULL)
|
|
|
|
return NULL;
|
2012-03-30 17:37:21 +08:00
|
|
|
strlcpy(ifp->name, p, sizeof(ifp->name));
|
|
|
|
ifp->flags = ifa->ifa_flags;
|
2009-08-06 05:16:09 +08:00
|
|
|
|
2009-10-14 23:16:07 +08:00
|
|
|
/* Bring the interface up if not already */
|
2009-11-20 23:14:27 +08:00
|
|
|
if (!(ifp->flags & IFF_UP)
|
2009-10-14 23:16:07 +08:00
|
|
|
#ifdef SIOCGIFMEDIA
|
2013-08-18 23:35:40 +08:00
|
|
|
&& carrier_status(ifp) != LINK_UNKNOWN
|
2009-10-14 23:16:07 +08:00
|
|
|
#endif
|
2009-11-20 23:14:27 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (up_interface(ifp) == 0)
|
|
|
|
options |= DHCPCD_WAITUP;
|
|
|
|
else
|
2013-05-18 23:30:55 +08:00
|
|
|
syslog(LOG_ERR, "%s: up_interface: %m",
|
|
|
|
ifp->name);
|
2009-11-20 23:14:27 +08:00
|
|
|
}
|
2009-08-06 05:16:09 +08:00
|
|
|
|
2012-03-20 17:19:01 +08:00
|
|
|
sdl_type = 0;
|
2009-04-17 20:49:25 +08:00
|
|
|
/* Don't allow loopback unless explicit */
|
|
|
|
if (ifp->flags & IFF_LOOPBACK) {
|
2009-04-21 03:40:09 +08:00
|
|
|
if (argc == 0 && ifac == 0) {
|
2009-04-17 20:49:25 +08:00
|
|
|
free_interface(ifp);
|
|
|
|
continue;
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
2009-05-11 15:16:24 +08:00
|
|
|
} else if (ifa->ifa_addr != NULL) {
|
2009-04-17 20:49:25 +08:00
|
|
|
#ifdef AF_LINK
|
|
|
|
sdl = (const struct sockaddr_dl *)(void *)ifa->ifa_addr;
|
2010-08-04 14:44:56 +08:00
|
|
|
|
|
|
|
#ifdef IFLR_ACTIVE
|
|
|
|
/* We need to check for active address */
|
|
|
|
strlcpy(iflr.iflr_name, ifp->name,
|
|
|
|
sizeof(iflr.iflr_name));
|
|
|
|
memcpy(&iflr.addr, ifa->ifa_addr,
|
|
|
|
MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
|
|
|
|
iflr.flags = IFLR_PREFIX;
|
|
|
|
iflr.prefixlen = sdl->sdl_alen * NBBY;
|
|
|
|
if (ioctl(socket_aflink, SIOCGLIFADDR, &iflr) == -1 ||
|
|
|
|
!(iflr.flags & IFLR_ACTIVE))
|
|
|
|
{
|
|
|
|
free_interface(ifp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-30 17:37:21 +08:00
|
|
|
ifp->index = sdl->sdl_index;
|
2012-03-20 17:19:01 +08:00
|
|
|
sdl_type = sdl->sdl_type;
|
2009-04-17 20:49:25 +08:00
|
|
|
switch(sdl->sdl_type) {
|
2012-03-20 17:19:01 +08:00
|
|
|
case IFT_BRIDGE: /* FALLTHROUGH */
|
|
|
|
case IFT_L2VLAN: /* FALLTHOUGH */
|
|
|
|
case IFT_L3IPVLAN: /* FALLTHROUGH */
|
2009-04-17 20:49:25 +08:00
|
|
|
case IFT_ETHER:
|
|
|
|
ifp->family = ARPHRD_ETHER;
|
2008-04-12 00:14:55 +08:00
|
|
|
break;
|
2009-04-17 20:49:25 +08:00
|
|
|
case IFT_IEEE1394:
|
|
|
|
ifp->family = ARPHRD_IEEE1394;
|
|
|
|
break;
|
2012-03-22 03:11:07 +08:00
|
|
|
#ifdef IFT_INFINIBAND
|
2012-03-20 17:19:01 +08:00
|
|
|
case IFT_INFINIBAND:
|
|
|
|
ifp->family = ARPHRD_INFINIBAND;
|
|
|
|
break;
|
2012-03-22 03:11:07 +08:00
|
|
|
#endif
|
2009-04-17 20:49:25 +08:00
|
|
|
}
|
|
|
|
ifp->hwlen = sdl->sdl_alen;
|
2009-04-17 21:42:44 +08:00
|
|
|
#ifndef CLLADDR
|
|
|
|
# define CLLADDR(s) ((const char *)((s)->sdl_data + (s)->sdl_nlen))
|
|
|
|
#endif
|
2009-04-17 21:31:41 +08:00
|
|
|
memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
|
2009-04-17 20:49:25 +08:00
|
|
|
#elif AF_PACKET
|
|
|
|
sll = (const struct sockaddr_ll *)(void *)ifa->ifa_addr;
|
2012-03-30 17:42:01 +08:00
|
|
|
ifp->index = sll->sll_ifindex;
|
2012-03-20 17:19:01 +08:00
|
|
|
ifp->family = sdl_type = sll->sll_hatype;
|
2009-04-17 20:49:25 +08:00
|
|
|
ifp->hwlen = sll->sll_halen;
|
|
|
|
if (ifp->hwlen != 0)
|
|
|
|
memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
|
|
|
|
#endif
|
2009-08-06 05:39:42 +08:00
|
|
|
}
|
2013-05-31 05:01:48 +08:00
|
|
|
#ifdef __linux__
|
|
|
|
/* PPP addresses on Linux don't have hardware addresses */
|
|
|
|
else
|
|
|
|
ifp->index = if_nametoindex(ifp->name);
|
|
|
|
#endif
|
2009-08-06 05:39:42 +08:00
|
|
|
|
2009-10-15 15:32:49 +08:00
|
|
|
/* We only work on ethernet by default */
|
|
|
|
if (!(ifp->flags & IFF_POINTOPOINT) &&
|
|
|
|
ifp->family != ARPHRD_ETHER)
|
|
|
|
{
|
|
|
|
if (argc == 0 && ifac == 0) {
|
|
|
|
free_interface(ifp);
|
|
|
|
continue;
|
|
|
|
}
|
2010-01-14 07:54:45 +08:00
|
|
|
switch (ifp->family) {
|
|
|
|
case ARPHRD_IEEE1394: /* FALLTHROUGH */
|
|
|
|
case ARPHRD_INFINIBAND:
|
|
|
|
/* We don't warn for supported families */
|
|
|
|
break;
|
|
|
|
default:
|
2009-08-06 05:39:42 +08:00
|
|
|
syslog(LOG_WARNING,
|
2012-03-20 17:19:01 +08:00
|
|
|
"%s: unsupported interface type %.2x"
|
|
|
|
", falling back to ethernet",
|
|
|
|
ifp->name, sdl_type);
|
|
|
|
ifp->family = ARPHRD_ETHER;
|
|
|
|
break;
|
2010-01-14 07:54:45 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
2010-02-27 04:37:30 +08:00
|
|
|
|
|
|
|
/* Handle any platform init for the interface */
|
|
|
|
if (if_init(ifp) == -1) {
|
|
|
|
syslog(LOG_ERR, "%s: if_init: %m", p);
|
|
|
|
free_interface(ifp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-03-30 17:37:21 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2013-02-19 21:37:42 +08:00
|
|
|
TAILQ_INSERT_TAIL(ifs, ifp, next);
|
2009-04-17 20:49:25 +08:00
|
|
|
}
|
2013-05-18 07:09:36 +08:00
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
|
|
|
|
if (ifa->ifa_addr != NULL &&
|
|
|
|
ifa->ifa_addr->sa_family == AF_INET6)
|
|
|
|
{
|
2013-05-18 07:24:07 +08:00
|
|
|
sin6 = (const struct sockaddr_in6 *)
|
|
|
|
(void *)ifa->ifa_addr;
|
2013-06-09 15:31:08 +08:00
|
|
|
ifa_flags = in6_addr_flags(ifa->ifa_name,
|
|
|
|
&sin6->sin6_addr);
|
|
|
|
if (ifa_flags != -1)
|
|
|
|
ipv6_handleifa(RTM_NEWADDR, ifs,
|
|
|
|
ifa->ifa_name,
|
|
|
|
&sin6->sin6_addr, ifa_flags);
|
2013-05-18 07:09:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-17 20:49:25 +08:00
|
|
|
freeifaddrs(ifaddrs);
|
2010-08-04 14:44:56 +08:00
|
|
|
|
|
|
|
#ifdef IFLR_ACTIVE
|
|
|
|
close(socket_aflink);
|
|
|
|
#endif
|
|
|
|
|
2009-04-17 20:49:25 +08:00
|
|
|
return ifs;
|
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
do_mtu(const char *ifname, short int mtu)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
|
|
|
|
ifr.ifr_mtu = mtu;
|
2009-10-15 08:30:54 +08:00
|
|
|
r = ioctl(socket_afnet, mtu ? SIOCSIFMTU : SIOCGIFMTU, &ifr);
|
2008-04-12 00:14:55 +08:00
|
|
|
if (r == -1)
|
|
|
|
return -1;
|
|
|
|
return ifr.ifr_mtu;
|
|
|
|
}
|