#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
+#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>
static int request_prefix = -1;
static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE, pd_mode = IA_MODE_NONE;
static bool accept_reconfig = false;
+// Server unicast address
+static struct in6_addr server_addr = IN6ADDR_ANY_INIT;
// Reconfigure key
static uint8_t reconf_key[16];
htons(DHCPV6_OPT_SIP_SERVER_A),
htons(DHCPV6_OPT_DNS_SERVERS),
htons(DHCPV6_OPT_DNS_DOMAIN),
+ htons(DHCPV6_OPT_UNICAST),
htons(DHCPV6_OPT_NTP_SERVER),
htons(DHCPV6_OPT_AFTR_NAME),
htons(DHCPV6_OPT_PD_EXCLUDE),
0, ALL_DHCPV6_RELAYS, ifindex};
struct msghdr msg = {&srv, sizeof(srv), iov, cnt, NULL, 0, 0};
- sendmsg(sock, &msg, 0);
+ switch (type) {
+ case DHCPV6_MSG_REQUEST:
+ case DHCPV6_MSG_RENEW:
+ case DHCPV6_MSG_RELEASE:
+ case DHCPV6_MSG_DECLINE:
+ if (!IN6_IS_ADDR_UNSPECIFIED(&server_addr) &&
+ odhcp6c_addr_in_scope(&server_addr)) {
+ srv.sin6_addr = server_addr;
+ if (!IN6_IS_ADDR_LINKLOCAL(&server_addr))
+ srv.sin6_scope_id = 0;
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (sendmsg(sock, &msg, 0) < 0) {
+ char in6_str[INET6_ADDRSTRLEN];
+
+ syslog(LOG_ERR, "Failed to send DHCPV6 message to %s (%s)",
+ inet_ntop(AF_INET6, (const void *)&srv.sin6_addr,
+ in6_str, sizeof(in6_str)), strerror(errno));
+ }
}
{
uint16_t olen, otype;
uint8_t *odata, pref = 0;
- struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}, NULL, NULL, 0, 0};
+ struct dhcpv6_server_cand cand = {false, false, 0, 0, {0},
+ IN6ADDR_ANY_INIT, NULL, NULL, 0, 0};
bool have_na = false;
int have_pd = 0;
} else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
cand.preference >= 0) {
cand.preference = pref = odata[0];
+ } else if (otype == DHCPV6_OPT_UNICAST && olen == sizeof(cand.server_addr)) {
+ cand.server_addr = *(struct in6_addr *)odata;
} else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
cand.wants_reconfigure = true;
} else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
odhcp6c_add_state(STATE_SERVER_ID, hdr, sizeof(hdr));
odhcp6c_add_state(STATE_SERVER_ID, c->duid, c->duid_len);
accept_reconfig = c->wants_reconfigure;
+ server_addr = c->server_addr;
if (c->ia_na_len)
odhcp6c_add_state(STATE_IA_NA, c->ia_na, c->ia_na_len);
if (c->ia_pd_len)
continue;
dhcpv6_parse_ia(ia_hdr, odata + olen + sizeof(*ia_hdr));
+ } else if (otype == DHCPV6_OPT_UNICAST && olen == sizeof(server_addr)) {
+ server_addr = *(struct in6_addr *)odata;
} else if (otype == DHCPV6_OPT_STATUS && olen >= 2) {
uint8_t *mdata = (olen > 2) ? &odata[2] : NULL;
uint16_t mlen = (olen > 2) ? olen - 2 : 0;
break;
case DHCPV6_UseMulticast:
- // TODO handle multicast status code
+ switch(orig) {
+ case DHCPV6_MSG_REQUEST:
+ case DHCPV6_MSG_RENEW:
+ case DHCPV6_MSG_RELEASE:
+ case DHCPV6_MSG_DECLINE:
+ // Message needs to be retransmitted according to RFC3315 chapter 18.1.8
+ server_addr = in6addr_any;
+ *ret = 0;
+ break;
+ default:
+ break;
+ }
break;
case DHCPV6_NoAddrsAvail:
#include <time.h>
#include <errno.h>
+#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
#include <string.h>
+#include <strings.h>
#include <stdbool.h>
#include <net/if.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <arpa/inet.h>
+#include <linux/if_addr.h>
#include "odhcp6c.h"
#include "ra.h"
#include "bfd.h"
#endif
+#ifndef IN6_IS_ADDR_UNIQUELOCAL
+#define IN6_IS_ADDR_UNIQUELOCAL(a) \
+ ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
+ == htonl (0xfc000000))
+#endif
static void sighandler(int signal);
static int usage(void);
static int urandom_fd = -1, allow_slaac_only = 0;
static bool bound = false, release = true;
static time_t last_update = 0;
-
+static char *ifname = NULL;
int main(_unused int argc, char* const argv[])
{
}
openlog("odhcp6c", logopt, LOG_DAEMON);
- const char *ifname = argv[optind];
+ ifname = argv[optind];
if (help || !ifname)
return usage();
read(urandom_fd, buf, len);
}
+
bool odhcp6c_is_bound(void)
{
return bound;
}
+
+bool odhcp6c_addr_in_scope(const struct in6_addr *addr)
+{
+ FILE *fd = fopen("/proc/net/if_inet6", "r");
+ int len;
+ char buf[256];
+
+ if (fd == NULL)
+ return false;
+
+ while (fgets(buf, sizeof(buf), fd)) {
+ struct in6_addr inet6_addr;
+ uint32_t flags, dummy;
+ unsigned int i;
+ char name[8], addr_buf[32];
+
+ len = strlen(buf);
+
+ if ((len <= 0) || buf[len - 1] != '\n')
+ return false;
+
+ buf[--len] = '\0';
+
+ if (sscanf(buf, "%s %x %x %x %x %s",
+ addr_buf, &dummy, &dummy, &dummy, &flags, name) != 6)
+ return false;
+
+ if (strcmp(name, ifname) ||
+ (flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE | IFA_F_DEPRECATED)))
+ continue;
+
+ for (i = 0; i < sizeof(addr_buf); i++) {
+ if (!isxdigit(addr_buf[i]) || isupper(addr_buf[i]))
+ return false;
+ }
+
+ memset(&inet6_addr, 0, sizeof(inet6_addr));
+ for (i = 0; i < (sizeof(addr_buf) / 2); i++) {
+ unsigned char byte;
+ static const char hex[] = "0123456789abcdef";
+ byte = ((index(hex, addr_buf[i * 2]) - hex) << 4) |
+ (index(hex, addr_buf[i * 2 + 1]) - hex);
+ inet6_addr.s6_addr[i] = byte;
+ }
+
+ if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr) == IN6_IS_ADDR_LINKLOCAL(addr)) &&
+ (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr) == IN6_IS_ADDR_UNIQUELOCAL(addr)))
+ return true;
+ }
+ return false;
+}
+
static void sighandler(int signal)
{
if (signal == SIGCHLD)