#include <sys/ioctl.h>
#include <net/if_media.h>
#include <ifaddrs.h>
#include <poll.h>
#include "dhcpd.h"
struct timeout timeout;
void
discover_interface(void)
{
struct ifaddrs *ifap, *ifa;
struct ifreq *tif;
int len = IFNAMSIZ + sizeof(struct sockaddr_storage);
if (getifaddrs(&ifap) != 0)
error("getifaddrs failed");
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if ((ifa->ifa_flags & IFF_LOOPBACK) ||
(ifa->ifa_flags & IFF_POINTOPOINT) ||
(!(ifa->ifa_flags & IFF_UP)))
continue;
if (strcmp(ifi->name, ifa->ifa_name))
continue;
if (ifa->ifa_addr->sa_family == AF_LINK) {
struct sockaddr_dl *foo =
(struct sockaddr_dl *)ifa->ifa_addr;
ifi->index = foo->sdl_index;
ifi->hw_address.hlen = foo->sdl_alen;
ifi->hw_address.htype = HTYPE_ETHER;
memcpy(ifi->hw_address.haddr,
LLADDR(foo), foo->sdl_alen);
}
if (!ifi->ifp) {
if ((tif = malloc(len)) == NULL)
error("no space to remember ifp");
strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
ifi->ifp = tif;
}
}
if (!ifi->ifp)
error("%s: not found", ifi->name);
if_register_receive();
if_register_send();
freeifaddrs(ifap);
}
void
dispatch(void)
{
int count, to_msec;
struct pollfd fds[2];
time_t cur_time, howlong;
void (*func)(void);
do {
another:
if (!ifi)
error("No interfaces available");
if (timeout.func) {
time(&cur_time);
if (timeout.when <= cur_time) {
func = timeout.func;
cancel_timeout();
(*(func))();
goto another;
}
howlong = timeout.when - cur_time;
if (howlong > INT_MAX / 1000)
howlong = INT_MAX / 1000;
to_msec = howlong * 1000;
} else
to_msec = -1;
if (!ifi || ifi->rfdesc == -1)
error("No live interface to poll on");
fds[0].fd = ifi->rfdesc;
fds[1].fd = routefd;
fds[0].events = fds[1].events = POLLIN;
count = poll(fds, 2, to_msec);
if (count == -1) {
if (errno == EAGAIN || errno == EINTR) {
continue;
} else
error("poll: %m");
}
if ((fds[0].revents & (POLLIN | POLLHUP))) {
ifi->linkstat = interface_status(ifi->name);
if (ifi && ifi->linkstat && ifi->rfdesc != -1)
got_one();
}
if ((fds[1].revents & (POLLIN | POLLHUP))) {
if (ifi)
routehandler();
}
} while (1);
}
void
got_one(void)
{
struct sockaddr_in from;
struct hardware hfrom;
struct iaddr ifrom;
ssize_t result;
if ((result = receive_packet(&from, &hfrom)) == -1) {
warning("receive_packet failed on %s: %s", ifi->name,
strerror(errno));
ifi->errors++;
if ((!interface_status(ifi->name)) ||
(ifi->noifmedia && ifi->errors > 20)) {
error("Interface %s no longer appears valid.",
ifi->name);
}
return;
}
if (result == 0)
return;
ifrom.len = 4;
memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
do_packet(result, from.sin_port, ifrom, &hfrom);
}
int
interface_link_forceup(char *ifname)
{
struct ifreq ifr;
struct ifmediareq ifmr;
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
error("Can't create socket");
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
close(sock);
return (-1);
}
memset(&ifmr, 0, sizeof(ifmr));
strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) != -1) {
if (IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211) {
return 0;
}
}
if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
ifr.ifr_flags |= IFF_UP;
if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
close(sock);
return (-1);
}
close(sock);
return (0);
}
close(sock);
return (1);
}
int
interface_status(char *ifname)
{
struct ifreq ifr;
struct ifmediareq ifmr;
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
error("Can't create socket");
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
error("ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
}
if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
goto inactive;
if (ifi->noifmedia)
goto active;
memset(&ifmr, 0, sizeof(ifmr));
strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
#ifdef DEBUG
if (errno != EINVAL && errno != ENOTTY)
debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
#endif
ifi->noifmedia = 1;
goto active;
}
if (ifmr.ifm_status & IFM_AVALID) {
if (ifmr.ifm_status & IFM_ACTIVE)
goto active;
else
goto inactive;
}
active:
close(sock);
return (1);
inactive:
close(sock);
return (0);
}
void
set_timeout(time_t when, void (*where)(void))
{
timeout.when = when;
timeout.func = where;
}
void
set_timeout_interval(time_t secs, void (*where)(void))
{
timeout.when = time(NULL) + secs;
timeout.func = where;
}
void
cancel_timeout(void)
{
timeout.when = 0;
timeout.func = NULL;
}
int
subnet_exists(struct client_lease *l)
{
struct ifaddrs *ifap, *ifa;
in_addr_t mymask, myaddr, mynet, hismask, hisaddr, hisnet;
bcopy(l->options[DHO_SUBNET_MASK].data, &mymask, 4);
bcopy(l->address.iabuf, &myaddr, 4);
mynet = mymask & myaddr;
if (getifaddrs(&ifap) != 0)
error("getifaddrs failed");
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (strcmp(ifi->name, ifa->ifa_name) == 0)
continue;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
hismask = ((struct sockaddr_in *)ifa->ifa_netmask)->
sin_addr.s_addr;
hisaddr = ((struct sockaddr_in *)ifa->ifa_addr)->
sin_addr.s_addr;
hisnet = hisaddr & hismask;
if (hisnet == 0)
continue;
if (mynet == (hisaddr & mymask)) {
note("interface %s already has the offered subnet!",
ifa->ifa_name);
return (1);
}
if (hisnet == (myaddr & hismask)) {
note("interface %s already has the offered subnet!",
ifa->ifa_name);
return (1);
}
}
freeifaddrs(ifap);
return (0);
}