The ip6ifaceid UCI interface parameter makes the interface identifier of the delegated IPv6 address configurable.
The parameter can have the following values:
eui64 : Interface identifier is generated from the interface's MAC address
random : Interface identifier is generated randomly
fixed value : Interface identifier is a fixed value (eg ::1:2)
The latter is the default value with a fixed value of ::1 for backwards compatibility
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Signed-off-by: Joeri Barbarien <joeri.barbarien@gmail.com>
}
}
+static void
+random_ifaceid(struct in6_addr *addr)
+{
+ static bool initialized = false;
+ struct timeval t;
+
+ if (!initialized) {
+ long int seed = 0;
+ gettimeofday(&t, NULL);
+ seed = t.tv_sec ^ t.tv_usec ^ getpid();
+ srand48(seed);
+ initialized = true;
+ }
+ addr->s6_addr32[2] = (uint32_t)mrand48();
+ addr->s6_addr32[3] = (uint32_t)mrand48();
+}
+
+static void
+eui64_ifaceid(struct interface *iface, struct in6_addr *addr)
+{
+ /* get mac address */
+ uint8_t *macaddr = iface->l3_dev.dev->settings.macaddr;
+ uint8_t *ifaceid = addr->s6_addr + 8;
+ memcpy(ifaceid,macaddr,3);
+ memcpy(ifaceid + 5,macaddr + 3, 3);
+ ifaceid[3] = 0xff;
+ ifaceid[4] = 0xfe;
+ ifaceid[0] ^= 0x02;
+}
+
+static void
+generate_ifaceid(struct interface *iface, struct in6_addr *addr)
+{
+ /* generate new iface id */
+ switch (iface->assignment_iface_id_selection) {
+ case IFID_FIXED:
+ /* fixed */
+ /* copy host part from assignment_fixed_iface_id */
+ memcpy(addr->s6_addr + 8, iface->assignment_fixed_iface_id.s6_addr + 8, 8);
+ break;
+ case IFID_RANDOM:
+ /* randomize last 64 bits */
+ random_ifaceid(addr);
+ break;
+ case IFID_EUI64:
+ /* eui64 */
+ eui64_ifaceid(iface, addr);
+ break;
+ }
+}
static void
interface_set_prefix_address(struct device_prefix_assignment *assignment,
struct device_addr addr;
memset(&addr, 0, sizeof(addr));
- addr.addr.in6 = prefix->addr;
- addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
- addr.addr.in6.s6_addr[15] += 1;
+
+ if (IN6_IS_ADDR_UNSPECIFIED(&assignment->addr)) {
+ addr.addr.in6 = prefix->addr;
+ addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
+ generate_ifaceid(iface, &addr.addr.in6);
+ assignment->addr = addr.addr.in6;
+ }
+ else
+ addr.addr.in6 = assignment->addr;
+
addr.mask = assignment->length;
addr.flags = DEVADDR_INET6;
addr.preferred_until = prefix->preferred_until;
c->assigned = 1 << (64 - prefix->length);
c->length = 64;
c->name[0] = 0;
+ c->addr = in6addr_any;
list_add(&c->head, &prefix->assignments);
// Excluded prefix
c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
((1 << (64 - prefix->length)) - 1);
c->length = prefix->excl_length;
+ c->addr = in6addr_any;
memcpy(c->name, name, sizeof(name));
list_add(&c->head, &prefix->assignments);
}
c = malloc(sizeof(*c) + namelen);
c->length = iface->assignment_length;
c->assigned = iface->assignment_hint;
+ c->addr = in6addr_any;
c->enabled = false;
memcpy(c->name, iface->name, namelen);
struct list_head head;
int32_t assigned;
uint8_t length;
+ struct in6_addr addr;
bool enabled;
char name[];
};
IFACE_ATTR_IP6TABLE,
IFACE_ATTR_IP6CLASS,
IFACE_ATTR_DELEGATE,
+ IFACE_ATTR_IP6IFACEID,
IFACE_ATTR_FORCE_LINK,
IFACE_ATTR_MAX
};
[IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
[IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
[IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
+ [IFACE_ATTR_IP6IFACEID] = { .name = "ip6ifaceid", .type = BLOBMSG_TYPE_STRING },
[IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
};
{
bool changed = (old->assignment_hint != new->assignment_hint ||
old->assignment_length != new->assignment_length ||
+ old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
+ (old->assignment_iface_id_selection == IFID_FIXED &&
+ memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
+ sizeof(old->assignment_fixed_iface_id))) ||
list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
struct interface_assignment_class *c;
if (changed) {
old->assignment_hint = new->assignment_hint;
old->assignment_length = new->assignment_length;
+ old->assignment_iface_id_selection = new->assignment_iface_id_selection;
+ old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
interface_refresh_assignments(true);
}
}
if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
iface->assignment_length = blobmsg_get_u32(cur);
+ /* defaults */
+ iface->assignment_iface_id_selection = IFID_FIXED;
+ iface->assignment_fixed_iface_id = in6addr_any;
+ iface->assignment_fixed_iface_id.s6_addr[15] = 1;
+
+ if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
+ const char *ifaceid = blobmsg_data(cur);
+ if (!strcmp(ifaceid, "random")) {
+ iface->assignment_iface_id_selection = IFID_RANDOM;
+ }
+ else if (!strcmp(ifaceid, "eui64")) {
+ iface->assignment_iface_id_selection = IFID_EUI64;
+ }
+ else {
+ /* we expect an IPv6 address with network id zero here -> fixed iface id
+ if we cannot parse -> revert to iface id 1 */
+ if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
+ iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
+ iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
+ iface->assignment_fixed_iface_id = in6addr_any;
+ iface->assignment_fixed_iface_id.s6_addr[15] = 1;
+ netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
+ falling back to iface id 1.\n", iface->name);
+ }
+ }
+ }
+
iface->assignment_hint = -1;
if ((cur = tb[IFACE_ATTR_IP6HINT]))
iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
IFC_REMOVE
};
+enum interface_id_selection_type {
+ IFID_FIXED,
+ IFID_RANDOM,
+ IFID_EUI64
+};
+
enum interface_update_flags {
IUF_ADDRESS = (1 << 0),
IUF_ROUTE = (1 << 1),
unsigned int ip6table;
/* IPv6 assignment parameters */
+ enum interface_id_selection_type assignment_iface_id_selection;
+ struct in6_addr assignment_fixed_iface_id;
uint8_t assignment_length;
int32_t assignment_hint;
struct list_head assignment_classes;