Rework code to get rid of RELAYD_MAX_PREFIXES and RELAYD_MAX_ADDRS
by using dynamic IPv6 address array allocation.
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
setup_dhcpv4_interface(iface, false);
clean_interface(iface);
setup_dhcpv4_interface(iface, false);
clean_interface(iface);
free(iface->ifname);
free(iface);
}
free(iface->ifname);
free(iface);
}
iov[IOV_CERID].iov_len = sizeof(cerid);
if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)) {
iov[IOV_CERID].iov_len = sizeof(cerid);
if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)) {
- struct odhcpd_ipaddr addrs[32];
- ssize_t len = odhcpd_get_interface_addresses(0, addrs,
- ARRAY_SIZE(addrs));
+ struct odhcpd_ipaddr *addrs;
+ ssize_t len = odhcpd_get_interface_addresses(0, &addrs);
for (ssize_t i = 0; i < len; ++i)
if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)
|| memcmp(&addrs[i].addr, &cerid.addr, sizeof(cerid.addr)) < 0)
cerid.addr = addrs[i].addr;
for (ssize_t i = 0; i < len; ++i)
if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)
|| memcmp(&addrs[i].addr, &cerid.addr, sizeof(cerid.addr)) < 0)
cerid.addr = addrs[i].addr;
memcpy(&hdr.interface_id_data, &ifindex, sizeof(ifindex));
// Detect public IP of slave interface to use as link-address
memcpy(&hdr.interface_id_data, &ifindex, sizeof(ifindex));
// Detect public IP of slave interface to use as link-address
- struct odhcpd_ipaddr ip;
- if (odhcpd_get_interface_addresses(iface->ifindex, &ip, 1) < 1) {
+ struct odhcpd_ipaddr *ip = NULL;
+ if (odhcpd_get_interface_addresses(iface->ifindex, &ip) < 1) {
// No suitable address! Is the slave not configured yet?
// Detect public IP of master interface and use it instead
// This is WRONG and probably violates the RFC. However
// otherwise we have a hen and egg problem because the
// slave-interface cannot be auto-configured.
// No suitable address! Is the slave not configured yet?
// Detect public IP of master interface and use it instead
// This is WRONG and probably violates the RFC. However
// otherwise we have a hen and egg problem because the
// slave-interface cannot be auto-configured.
- if (odhcpd_get_interface_addresses(master->ifindex, &ip, 1) < 1)
+ if (odhcpd_get_interface_addresses(master->ifindex, &ip) < 1)
return; // Could not obtain a suitable address
}
return; // Could not obtain a suitable address
}
- memcpy(&hdr.link_address, &ip.addr, sizeof(hdr.link_address));
+ memcpy(&hdr.link_address, &ip[0].addr, sizeof(hdr.link_address));
+ free(ip);
struct sockaddr_in6 dhcpv6_servers = {AF_INET6,
htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0};
struct sockaddr_in6 dhcpv6_servers = {AF_INET6,
htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0};
// Check address update
static void check_addr_updates(struct interface *iface)
{
// Check address update
static void check_addr_updates(struct interface *iface)
{
- struct odhcpd_ipaddr addr[RELAYD_MAX_ADDRS] = {{IN6ADDR_ANY_INIT, 0, 0, 0, 0}};
+ struct odhcpd_ipaddr *addr = NULL;
time_t now = odhcpd_time();
time_t now = odhcpd_time();
- ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, addr, ARRAY_SIZE(addr));
+ ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, &addr);
if (change)
dhcpv6_ia_preupdate(iface);
if (change)
dhcpv6_ia_preupdate(iface);
- memcpy(iface->ia_addr, addr, len * sizeof(*addr));
+ free(iface->ia_addr);
+ iface->ia_addr = addr;
iface->ia_addr_len = len;
if (change)
iface->ia_addr_len = len;
if (change)
#include <signal.h>
#include <stdbool.h>
#include <syslog.h>
#include <signal.h>
#include <stdbool.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <net/if.h>
struct addr_info {
int ifindex;
struct addr_info {
int ifindex;
- struct odhcpd_ipaddr *addrs;
- size_t addrs_sz;
+ struct odhcpd_ipaddr **addrs;
int pending;
ssize_t ret;
};
int pending;
ssize_t ret;
};
static int cb_valid_handler(struct nl_msg *msg, void *arg)
{
struct addr_info *ctxt = (struct addr_info *)arg;
static int cb_valid_handler(struct nl_msg *msg, void *arg)
{
struct addr_info *ctxt = (struct addr_info *)arg;
+ struct odhcpd_ipaddr *addrs = *(ctxt->addrs);
struct nlmsghdr *hdr = nlmsg_hdr(msg);
struct ifaddrmsg *ifa;
struct nlattr *nla[__IFA_MAX];
struct nlmsghdr *hdr = nlmsg_hdr(msg);
struct ifaddrmsg *ifa;
struct nlattr *nla[__IFA_MAX];
- if (hdr->nlmsg_type != RTM_NEWADDR || ctxt->ret >= (ssize_t)ctxt->addrs_sz)
+ if (hdr->nlmsg_type != RTM_NEWADDR)
return NL_SKIP;
ifa = NLMSG_DATA(hdr);
return NL_SKIP;
ifa = NLMSG_DATA(hdr);
if (!nla[IFA_ADDRESS])
return NL_SKIP;
if (!nla[IFA_ADDRESS])
return NL_SKIP;
- memset(&ctxt->addrs[ctxt->ret], 0, sizeof(ctxt->addrs[ctxt->ret]));
- ctxt->addrs[ctxt->ret].prefix = ifa->ifa_prefixlen;
+ addrs = realloc(addrs, sizeof(*addrs)*(ctxt->ret + 1));
+ if (!addrs)
+ return NL_SKIP;
+
+ memset(&addrs[ctxt->ret], 0, sizeof(addrs[ctxt->ret]));
+ addrs[ctxt->ret].prefix = ifa->ifa_prefixlen;
- nla_memcpy(&ctxt->addrs[ctxt->ret].addr, nla[IFA_ADDRESS],
- sizeof(ctxt->addrs[ctxt->ret].addr));
+ nla_memcpy(&addrs[ctxt->ret].addr, nla[IFA_ADDRESS],
+ sizeof(addrs[ctxt->ret].addr));
if (nla[IFA_CACHEINFO]) {
struct ifa_cacheinfo *ifc = nla_data(nla[IFA_CACHEINFO]);
if (nla[IFA_CACHEINFO]) {
struct ifa_cacheinfo *ifc = nla_data(nla[IFA_CACHEINFO]);
- ctxt->addrs[ctxt->ret].preferred = ifc->ifa_prefered;
- ctxt->addrs[ctxt->ret].valid = ifc->ifa_valid;
+ addrs[ctxt->ret].preferred = ifc->ifa_prefered;
+ addrs[ctxt->ret].valid = ifc->ifa_valid;
}
if (ifa->ifa_flags & IFA_F_DEPRECATED)
}
if (ifa->ifa_flags & IFA_F_DEPRECATED)
- ctxt->addrs[ctxt->ret].preferred = 0;
+ addrs[ctxt->ret].preferred = 0;
+ *(ctxt->addrs) = addrs;
}
// Detect an IPV6-address currently assigned to the given interface
}
// Detect an IPV6-address currently assigned to the given interface
-ssize_t odhcpd_get_interface_addresses(int ifindex,
- struct odhcpd_ipaddr *addrs, size_t cnt)
+ssize_t odhcpd_get_interface_addresses(int ifindex, struct odhcpd_ipaddr **addrs)
{
struct nl_msg *msg;
struct ifaddrmsg ifa = {
{
struct nl_msg *msg;
struct ifaddrmsg ifa = {
struct addr_info ctxt = {
.ifindex = ifindex,
.addrs = addrs,
struct addr_info ctxt = {
.ifindex = ifindex,
.addrs = addrs,
.ret = 0,
.pending = 1,
};
.ret = 0,
.pending = 1,
};
#define ND_OPT_DNS_SEARCH 31
#define RELAYD_BUFFER_SIZE 8192
#define ND_OPT_DNS_SEARCH 31
#define RELAYD_BUFFER_SIZE 8192
-#define RELAYD_MAX_PREFIXES 8
-#define RELAYD_MAX_ADDRS 8
#define INFINITE_VALID(x) ((x) == 0)
#define INFINITE_VALID(x) ((x) == 0)
// Runtime data
struct uloop_timeout timer_rs;
struct list_head ia_assignments;
// Runtime data
struct uloop_timeout timer_rs;
struct list_head ia_assignments;
- struct odhcpd_ipaddr ia_addr[RELAYD_MAX_ADDRS];
+ struct odhcpd_ipaddr *ia_addr;
size_t ia_addr_len;
// DHCPv4
size_t ia_addr_len;
// DHCPv4
struct iovec *iov, size_t iov_len,
const struct interface *iface);
ssize_t odhcpd_get_interface_addresses(int ifindex,
struct iovec *iov, size_t iov_len,
const struct interface *iface);
ssize_t odhcpd_get_interface_addresses(int ifindex,
- struct odhcpd_ipaddr *addrs, size_t cnt);
+ struct odhcpd_ipaddr **addrs);
int odhcpd_get_interface_dns_addr(const struct interface *iface,
struct in6_addr *addr);
struct interface* odhcpd_get_interface_by_name(const char *name);
int odhcpd_get_interface_dns_addr(const struct interface *iface,
struct in6_addr *addr);
struct interface* odhcpd_get_interface_by_name(const char *name);
static struct odhcpd_event router_event = {.uloop = {.fd = -1}, .handle_dgram = handle_icmpv6, };
static FILE *fp_route = NULL;
static struct odhcpd_event router_event = {.uloop = {.fd = -1}, .handle_dgram = handle_icmpv6, };
static FILE *fp_route = NULL;
#define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX)
#define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX)
struct nd_router_advert h;
struct icmpv6_opt lladdr;
struct nd_opt_mtu mtu;
struct nd_router_advert h;
struct icmpv6_opt lladdr;
struct nd_opt_mtu mtu;
- struct nd_opt_prefix_info prefix[sizeof(iface->ia_addr) / sizeof(*iface->ia_addr)];
} adv = {
.h = {{.icmp6_type = ND_ROUTER_ADVERT, .icmp6_code = 0}, 0, 0},
.lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
} adv = {
.h = {{.icmp6_type = ND_ROUTER_ADVERT, .icmp6_code = 0}, 0, 0},
.lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
odhcpd_get_mac(iface, adv.lladdr.data);
// If not currently shutting down
odhcpd_get_mac(iface, adv.lladdr.data);
// If not currently shutting down
- struct odhcpd_ipaddr addrs[RELAYD_MAX_ADDRS];
+ struct odhcpd_ipaddr *addrs = NULL;
ssize_t ipcnt = 0;
uint32_t minvalid = UINT32_MAX;
bool default_route = false;
ssize_t ipcnt = 0;
uint32_t minvalid = UINT32_MAX;
bool default_route = false;
// If not shutdown
if (iface->timer_rs.cb) {
// If not shutdown
if (iface->timer_rs.cb) {
+ size_t size = sizeof(*addrs) * iface->ia_addr_len;
+ addrs = alloca(size);
+ memcpy(addrs, iface->ia_addr, size);
+
ipcnt = iface->ia_addr_len;
ipcnt = iface->ia_addr_len;
- memcpy(addrs, iface->ia_addr, ipcnt * sizeof(*addrs));
// Check default route
if (iface->default_router) {
// Check default route
if (iface->default_router) {
- // Construct Prefix Information options
- size_t cnt = 0;
struct in6_addr dns_pref, *dns_addr = &dns_pref;
size_t dns_cnt = 1;
odhcpd_get_interface_dns_addr(iface, &dns_pref);
struct in6_addr dns_pref, *dns_addr = &dns_pref;
size_t dns_cnt = 1;
odhcpd_get_interface_dns_addr(iface, &dns_pref);
+ // Construct Prefix Information options
+ size_t pfxs_cnt = 0;
+ struct nd_opt_prefix_info *pfxs = NULL;
+
for (ssize_t i = 0; i < ipcnt; ++i) {
struct odhcpd_ipaddr *addr = &addrs[i];
uint32_t preferred = 0;
for (ssize_t i = 0; i < ipcnt; ++i) {
struct odhcpd_ipaddr *addr = &addrs[i];
uint32_t preferred = 0;
}
struct nd_opt_prefix_info *p = NULL;
}
struct nd_opt_prefix_info *p = NULL;
- for (size_t i = 0; i < cnt; ++i) {
- if (addr->prefix == adv.prefix[i].nd_opt_pi_prefix_len &&
- !odhcpd_bmemcmp(&adv.prefix[i].nd_opt_pi_prefix,
+ for (size_t i = 0; i < pfxs_cnt; ++i) {
+ if (addr->prefix == pfxs[i].nd_opt_pi_prefix_len &&
+ !odhcpd_bmemcmp(&pfxs[i].nd_opt_pi_prefix,
&addr->addr, addr->prefix))
&addr->addr, addr->prefix))
- if (cnt >= ARRAY_SIZE(adv.prefix))
- break;
+ struct nd_opt_prefix_info *tmp;
- p = &adv.prefix[cnt++];
+ tmp = realloc(pfxs, sizeof(*pfxs) * (pfxs_cnt + 1));
+ if (!tmp) {
+ syslog(LOG_ERR, "Realloc failed for RA prefix option on interface %s", iface->ifname);
+ continue;
+ }
+
+ pfxs = tmp;
+ p = &pfxs[pfxs_cnt++];
+ memset(p, 0, sizeof(*p));
}
if (addr->preferred > (uint32_t)now) {
}
if (addr->preferred > (uint32_t)now) {
uint32_t lifetime;
} dns = {ND_OPT_RECURSIVE_DNS, (1 + (2 * dns_cnt)), 0, 0, 0};
uint32_t lifetime;
} dns = {ND_OPT_RECURSIVE_DNS, (1 + (2 * dns_cnt)), 0, 0, 0};
// DNS Search options
uint8_t search_buf[256], *search_domain = iface->search;
size_t search_len = iface->search_len, search_padded = 0;
// DNS Search options
uint8_t search_buf[256], *search_domain = iface->search;
size_t search_len = iface->search_len, search_padded = 0;
uint8_t flags;
uint32_t lifetime;
uint32_t addr[4];
uint8_t flags;
uint32_t lifetime;
uint32_t addr[4];
- } routes[RELAYD_MAX_PREFIXES];
+ } *tmp, *routes = NULL;
for (ssize_t i = 0; i < ipcnt; ++i) {
struct odhcpd_ipaddr *addr = &addrs[i];
for (ssize_t i = 0; i < ipcnt; ++i) {
struct odhcpd_ipaddr *addr = &addrs[i];
addr->addr.s6_addr32[1] = 0;
}
addr->addr.s6_addr32[1] = 0;
}
+ tmp = realloc(routes, sizeof(*routes) * (routes_cnt + 1));
+ if (!tmp) {
+ syslog(LOG_ERR, "Realloc failed for RA route option on interface %s", iface->ifname);
+ continue;
+ }
+
+ routes = tmp;
+
+ memset(&routes[routes_cnt], 0, sizeof(*routes));
routes[routes_cnt].type = ND_OPT_ROUTE_INFO;
routes[routes_cnt].len = sizeof(*routes) / 8;
routes[routes_cnt].prefix = addr->dprefix;
routes[routes_cnt].type = ND_OPT_ROUTE_INFO;
routes[routes_cnt].len = sizeof(*routes) / 8;
routes[routes_cnt].prefix = addr->dprefix;
};
struct iovec iov[RA_IOV_LEN] = {
};
struct iovec iov[RA_IOV_LEN] = {
- {&adv, (uint8_t*)&adv.prefix[cnt] - (uint8_t*)&adv},
- {&routes, routes_cnt * sizeof(*routes)},
+ {&adv, sizeof(adv)},
+ {pfxs, pfxs_cnt * sizeof(*pfxs)},
+ {routes, routes_cnt * sizeof(*routes)},
{&dns, (dns_cnt) ? sizeof(dns) : 0},
{dns_addr, dns_cnt * sizeof(*dns_addr)},
{search, search->len * 8},
{&dns, (dns_cnt) ? sizeof(dns) : 0},
{dns_addr, dns_cnt * sizeof(*dns_addr)},
{search, search->len * 8},
odhcpd_send(router_event.uloop.fd,
&dest, iov, ARRAY_SIZE(iov), iface);
odhcpd_send(router_event.uloop.fd,
&dest, iov, ARRAY_SIZE(iov), iface);
+ free(pfxs);
+ free(routes);
+