mirror of
https://github.com/pi-hole/dnsmasq.git
synced 2025-12-19 18:28:25 +00:00
Clean compile, basic DHCPv6 functionality is there.
TODO
hostname handling.
update DHCP6 configs from dns
parse domain=<domain>,<IPv6 range>
pretty-print counted string options.
DECLINE messages
lease-script fro DHCPv6
This commit is contained in:
200
src/dhcp6.c
200
src/dhcp6.c
@@ -23,27 +23,40 @@ struct iface_param {
|
||||
int ind;
|
||||
};
|
||||
|
||||
struct listen_param {
|
||||
int fd_or_iface;
|
||||
struct listen_param *next;
|
||||
};
|
||||
|
||||
static int join_multicast(struct in6_addr *local, int prefix,
|
||||
int scope, int if_index, int dad, void *vparam);
|
||||
|
||||
static int complete_context6(struct in6_addr *local, int prefix,
|
||||
int scope, int if_index, int dad, void *vparam);
|
||||
|
||||
static int make_duid1(unsigned int type, unsigned int flags, char *mac,
|
||||
size_t maclen, void *parm);
|
||||
|
||||
void dhcp6_init(void)
|
||||
{
|
||||
int fd;
|
||||
struct sockaddr_in6 saddr;
|
||||
struct listen_param *listenp, listen;
|
||||
#if defined(IP_TOS) && defined(IPTOS_CLASS_CS6)
|
||||
int class = IPTOS_CLASS_CS6;
|
||||
#endif
|
||||
|
||||
if ((fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1 ||
|
||||
#if defined(IP_TOS) && defined(IPTOS_CLASS_CS6)
|
||||
setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
|
||||
#endif
|
||||
!fix_fd(fd) ||
|
||||
!set_ipv6pktinfo(fd))
|
||||
die (_("cannot create DHCPv6 socket: %s"), NULL, EC_BADNET);
|
||||
|
||||
memset(&saddr, 0, sizeof(saddr));
|
||||
#ifdef HAVE_SOCKADDR_SA_LEN
|
||||
saddr.sin6_len = sizeof(addr.in6);
|
||||
saddr.sin6_len = sizeof(struct sockaddr_in6);
|
||||
#endif
|
||||
saddr.sin6_family = AF_INET6;
|
||||
saddr.sin6_addr = in6addr_any;
|
||||
@@ -53,16 +66,18 @@ void dhcp6_init(void)
|
||||
die(_("failed to bind DHCPv6 server socket: %s"), NULL, EC_BADNET);
|
||||
|
||||
/* join multicast groups on each interface we're interested in */
|
||||
if (!iface_enumerate(AF_INET6, &fd, join_multicast))
|
||||
listen.fd_or_iface = fd;
|
||||
listen.next = NULL;
|
||||
if (!iface_enumerate(AF_INET6, &listen, join_multicast))
|
||||
die(_("failed to join DHCPv6 multicast group: %s"), NULL, EC_BADNET);
|
||||
for (listenp = listen.next; listenp; )
|
||||
{
|
||||
struct listen_param *tmp = listenp->next;
|
||||
free(listenp);
|
||||
listenp = tmp;
|
||||
}
|
||||
|
||||
daemon->dhcp6fd = fd;
|
||||
|
||||
/* If we've already inited DHCPv4, this becomes a no-op,
|
||||
othewise sizeof(struct dhcp_packet) is as good an initial
|
||||
size as any. */
|
||||
expand_buf(&daemon->dhcp_packet, sizeof(struct dhcp_packet));
|
||||
expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
|
||||
}
|
||||
|
||||
static int join_multicast(struct in6_addr *local, int prefix,
|
||||
@@ -70,20 +85,25 @@ static int join_multicast(struct in6_addr *local, int prefix,
|
||||
{
|
||||
char ifrn_name[IFNAMSIZ];
|
||||
struct ipv6_mreq mreq;
|
||||
struct in6_addr maddr;
|
||||
int fd = *((int *)vparam);
|
||||
struct listen_param *listenp, *param = vparam;
|
||||
int fd = param->fd_or_iface;
|
||||
struct dhcp_context *context;
|
||||
struct iname *tmp;
|
||||
|
||||
(void)prefix;
|
||||
(void)scope;
|
||||
(void)dad;
|
||||
|
||||
/* record which interfaces we join on, so
|
||||
that we do it at most one per interface, even when they
|
||||
have multiple addresses */
|
||||
for (listenp = param->next; listenp; listenp = listenp->next)
|
||||
if (if_index == listenp->fd_or_iface)
|
||||
return 1;
|
||||
|
||||
/* scope == link */
|
||||
if (scope != 253)
|
||||
return 1;
|
||||
|
||||
if (!indextoname(fd, if_index, ifrn_name))
|
||||
return 0;
|
||||
|
||||
|
||||
/* Are we doing DHCP on this interface? */
|
||||
if (!iface_check(AF_INET6, (struct all_addr *)local, ifrn_name))
|
||||
return 1;
|
||||
@@ -111,8 +131,12 @@ static int join_multicast(struct in6_addr *local, int prefix,
|
||||
if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1)
|
||||
return 0;
|
||||
|
||||
listenp = whine_malloc(sizeof(struct listen_param));
|
||||
listenp->fd_or_iface = if_index;
|
||||
listenp->next = param->next;
|
||||
param->next = listenp;
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -176,7 +200,7 @@ void dhcp6_packet(time_t now)
|
||||
|
||||
if (!context)
|
||||
return;
|
||||
|
||||
|
||||
/* unlinked contexts are marked by context->current == context */
|
||||
for (context = daemon->dhcp6; context; context = context->next)
|
||||
context->current = context;
|
||||
@@ -190,14 +214,15 @@ void dhcp6_packet(time_t now)
|
||||
lease_prune(NULL, now); /* lose any expired leases */
|
||||
|
||||
msg.msg_iov = &daemon->dhcp_packet;
|
||||
sz = dhcp6_reply(parm.current, sz, now);
|
||||
sz = dhcp6_reply(parm.current, ifr.ifr_name, sz, IN6_IS_ADDR_MULTICAST(&from), now);
|
||||
/* ifr.ifr_name, if_index, (size_t)sz,
|
||||
now, unicast_dest, &is_inform, pxe_fd, iface_addr); */
|
||||
lease_update_file(now);
|
||||
lease_update_dns();
|
||||
|
||||
if (sz != 0)
|
||||
send_from(daemon->dhcp6fd, 0, daemon->outpacket.iov_base, sz, &from, &dest, if_index);
|
||||
while (sendto(daemon->dhcp6fd, daemon->outpacket.iov_base, sz, 0, (struct sockaddr *)&from, sizeof(from)) &&
|
||||
retry_send());
|
||||
}
|
||||
|
||||
static int complete_context6(struct in6_addr *local, int prefix,
|
||||
@@ -205,11 +230,16 @@ static int complete_context6(struct in6_addr *local, int prefix,
|
||||
{
|
||||
struct dhcp_context *context;
|
||||
struct iface_param *param = vparam;
|
||||
|
||||
(void)scope; /* warning */
|
||||
(void)dad;
|
||||
|
||||
for (context = daemon->dhcp6; context; context = context->next)
|
||||
{
|
||||
if (prefix == context->prefix &&
|
||||
!IN6_IS_ADDR_LOOPBACK(local) &&
|
||||
!IN6_IS_ADDR_LINKLOCAL(local) &&
|
||||
!IN6_IS_ADDR_MULTICAST(local) &&
|
||||
is_same_net6(local, &context->start6, prefix) &&
|
||||
is_same_net6(local, &context->end6, prefix))
|
||||
{
|
||||
@@ -218,6 +248,7 @@ static int complete_context6(struct in6_addr *local, int prefix,
|
||||
{
|
||||
context->current = param->current;
|
||||
param->current = context;
|
||||
context->local6 = *local;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,7 +269,7 @@ struct dhcp_config *config_find_by_address6(struct dhcp_config *configs, struct
|
||||
}
|
||||
|
||||
int address6_allocate(struct dhcp_context *context, unsigned char *clid, int clid_len,
|
||||
struct dhcp_netid *netids, struct in6_addr *ans)
|
||||
int serial, struct dhcp_netid *netids, struct in6_addr *ans)
|
||||
{
|
||||
/* Find a free address: exclude anything in use and anything allocated to
|
||||
a particular hwaddr/clientid/hostname in our configuration.
|
||||
@@ -266,7 +297,7 @@ int address6_allocate(struct dhcp_context *context, unsigned char *clid, int cl
|
||||
continue;
|
||||
else
|
||||
{
|
||||
start = addr6part(&c->start6) + ((j + c->addr_epoch) % (1 + addr6part(&c->end6) - addr6part(&c->start6)));
|
||||
start = addr6part(&c->start6) + ((j + c->addr_epoch + serial) % (1 + addr6part(&c->end6) - addr6part(&c->start6)));
|
||||
|
||||
/* iterate until we find a free address. */
|
||||
addr = start;
|
||||
@@ -358,6 +389,133 @@ struct dhcp_context *narrow_context6(struct dhcp_context *context,
|
||||
return tmp;
|
||||
}
|
||||
|
||||
static int is_addr_in_context6(struct dhcp_context *context, struct dhcp_config *config)
|
||||
{
|
||||
if (!context) /* called via find_config() from lease_update_from_configs() */
|
||||
return 1;
|
||||
if (!(config->flags & CONFIG_ADDR6))
|
||||
return 1;
|
||||
for (; context; context = context->current)
|
||||
if (is_same_net6(&config->addr6, &context->start6, context->prefix))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
struct dhcp_config *find_config6(struct dhcp_config *configs,
|
||||
struct dhcp_context *context,
|
||||
unsigned char *duid, int duid_len,
|
||||
char *hostname)
|
||||
{
|
||||
int count, new;
|
||||
struct dhcp_config *config;
|
||||
struct hwaddr_config *conf_addr;
|
||||
unsigned char *hwaddr = NULL;
|
||||
int duid_type, hw_len = 0, hw_type = 0;
|
||||
|
||||
if (duid)
|
||||
{
|
||||
for (config = configs; config; config = config->next)
|
||||
if (config->flags & CONFIG_CLID)
|
||||
{
|
||||
if (config->clid_len == duid_len &&
|
||||
memcmp(config->clid, duid, duid_len) == 0 &&
|
||||
is_addr_in_context6(context, config))
|
||||
return config;
|
||||
}
|
||||
|
||||
/* DHCPv6 doesn't deal in MAC addresses per-se, but some DUIDs do include
|
||||
MAC addresses, so we try and parse them out here. Not that there is only one
|
||||
DUID per host and it's created using any one of the MACs, so this is no
|
||||
good no good for multihomed hosts. */
|
||||
hwaddr = duid;
|
||||
GETSHORT(duid_type, hwaddr);
|
||||
if (duid_type == 1 || duid_type == 3)
|
||||
{
|
||||
GETSHORT(hw_type, hwaddr);
|
||||
if (duid_type == 1)
|
||||
hwaddr += 4; /* skip time */
|
||||
hw_len = duid_len - 8;
|
||||
}
|
||||
|
||||
if (hwaddr)
|
||||
for (config = configs; config; config = config->next)
|
||||
if (config_has_mac(config, hwaddr, hw_len, hw_type) &&
|
||||
is_addr_in_context6(context, config))
|
||||
return config;
|
||||
}
|
||||
|
||||
if (hostname && context)
|
||||
for (config = configs; config; config = config->next)
|
||||
if ((config->flags & CONFIG_NAME) &&
|
||||
hostname_isequal(config->hostname, hostname) &&
|
||||
is_addr_in_context6(context, config))
|
||||
return config;
|
||||
|
||||
/* use match with fewest wildcard octets */
|
||||
if (hwaddr)
|
||||
{
|
||||
struct dhcp_config *candidate;
|
||||
|
||||
for (candidate = NULL, count = 0, config = configs; config; config = config->next)
|
||||
if (is_addr_in_context6(context, config))
|
||||
for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
|
||||
if (conf_addr->wildcard_mask != 0 &&
|
||||
conf_addr->hwaddr_len == hw_len &&
|
||||
(conf_addr->hwaddr_type == hw_type || conf_addr->hwaddr_type == 0) &&
|
||||
(new = memcmp_masked(conf_addr->hwaddr, hwaddr, hw_len, conf_addr->wildcard_mask)) > count)
|
||||
{
|
||||
count = new;
|
||||
candidate = config;
|
||||
}
|
||||
|
||||
return candidate;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void make_duid(time_t now)
|
||||
{
|
||||
/* rebase epoch to 1/1/2000 */
|
||||
time_t newnow = now - 946684800;
|
||||
iface_enumerate(AF_LOCAL, &newnow, make_duid1);
|
||||
|
||||
if (!daemon->duid)
|
||||
die("Cannot create DHCPv6 server DUID", NULL, EC_MISC);
|
||||
}
|
||||
|
||||
static int make_duid1(unsigned int type, unsigned int flags, char *mac,
|
||||
size_t maclen, void *parm)
|
||||
{
|
||||
/* create DUID as specified in RFC3315. We use the MAC of the
|
||||
first interface we find that isn't loopback or P-to-P */
|
||||
|
||||
unsigned char *p;
|
||||
|
||||
if (flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
|
||||
return 1;
|
||||
|
||||
daemon->duid = p = safe_malloc(maclen + 8);
|
||||
daemon->duid_len = maclen + 8;
|
||||
|
||||
#ifdef HAVE_BROKEN_RTC
|
||||
PUTSHORT(3, p); /* DUID_LL */
|
||||
#else
|
||||
PUTSHORT(1, p); /* DUID_LLT */
|
||||
#endif
|
||||
|
||||
PUTSHORT(type, p); /* address type */
|
||||
|
||||
#ifndef HAVE_BROKEN_RTC
|
||||
PUTLONG(*((time_t *)parm), p); /* time */
|
||||
#endif
|
||||
|
||||
memcpy(p, mac, maclen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user