+
+ memset(&req, 0, sizeof(req));
+
+ req.nh.nlmsg_type = RTM_NEWADDR;
+ req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_MULTI;
+
+ req.ifmsg.ifaddr.ifa_family = AF_INET;
+ req.ifmsg.ifaddr.ifa_prefixlen = 32;
+ req.ifmsg.ifaddr.ifa_index = ifinfo.ifi_index;
+
+ ipv4_attr.rta.rta_len = RTA_LENGTH(sizeof(ipv4_attr.addr));
+ ipv4_attr.rta.rta_type = IFA_LOCAL;
+ ipv4_attr.addr.s_addr = config->bind_address ?
+ config->bind_address : 0x01010101; // 1.1.1.1
+ memcpy(NLMSG_DATA(&req.nh) + sizeof(req.ifmsg.ifaddr), &ipv4_attr, sizeof(ipv4_attr));
+
+ req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifmsg.ifaddr)
+ + ipv4_attr.rta.rta_len);
+
+ if (netlink_send(&req.nh) < 0)
+ {
+ LOG(0, 0, 0, "Error setting up tun device IPv4 address: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ // Only setup IPv6 on the tun device if we have a configured prefix
+ if (config->ipv6_prefix.s6_addr[0]) {
+ memset(&req, 0, sizeof(req));
+
+ req.nh.nlmsg_type = RTM_NEWADDR;
+ req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_MULTI;
+
+ req.ifmsg.ifaddr.ifa_family = AF_INET6;
+ req.ifmsg.ifaddr.ifa_prefixlen = 64;
+ req.ifmsg.ifaddr.ifa_scope = RT_SCOPE_LINK;
+ req.ifmsg.ifaddr.ifa_index = ifinfo.ifi_index;
+
+ ipv6_attr.rta.rta_len = RTA_LENGTH(sizeof(ipv6_attr.addr6));
+ ipv6_attr.rta.rta_type = IFA_LOCAL;
+ // Link local address is FE80::1
+ memset(&ipv6_attr.addr6, 0, sizeof(ipv6_attr.addr6));
+ ipv6_attr.addr6.s6_addr[0] = 0xFE;
+ ipv6_attr.addr6.s6_addr[1] = 0x80;
+ ipv6_attr.addr6.s6_addr[15] = 1;
+ memcpy(NLMSG_DATA(&req.nh) + sizeof(req.ifmsg.ifaddr), &ipv6_attr, sizeof(ipv6_attr));
+
+ req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifmsg.ifaddr)
+ + ipv6_attr.rta.rta_len);
+
+ if (netlink_send(&req.nh) < 0)
+ {
+ LOG(0, 0, 0, "Error setting up tun device IPv6 LL address: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ memset(&req, 0, sizeof(req));
+
+ req.nh.nlmsg_type = RTM_NEWADDR;
+ req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_MULTI;
+
+ req.ifmsg.ifaddr.ifa_family = AF_INET6;
+ req.ifmsg.ifaddr.ifa_prefixlen = 64;
+ req.ifmsg.ifaddr.ifa_scope = RT_SCOPE_UNIVERSE;
+ req.ifmsg.ifaddr.ifa_index = ifinfo.ifi_index;
+
+ ipv6_attr.rta.rta_len = RTA_LENGTH(sizeof(ipv6_attr.addr6));
+ ipv6_attr.rta.rta_type = IFA_LOCAL;
+ // Global address is prefix::1
+ ipv6_attr.addr6 = config->ipv6_prefix;
+ ipv6_attr.addr6.s6_addr[15] = 1;
+ memcpy(NLMSG_DATA(&req.nh) + sizeof(req.ifmsg.ifaddr), &ipv6_attr, sizeof(ipv6_attr));
+
+ req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifmsg.ifaddr)
+ + ipv6_attr.rta.rta_len);
+
+ if (netlink_send(&req.nh) < 0)
+ {
+ LOG(0, 0, 0, "Error setting up tun device IPv6 global address: %s\n", strerror(errno));
+ exit(1);
+ }
+ }
+
+ memset(&req, 0, sizeof(req));
+
+ req.nh.nlmsg_type = NLMSG_DONE;
+ req.nh.nlmsg_len = NLMSG_LENGTH(0);
+
+ if (netlink_send(&req.nh) < 0)
+ {
+ LOG(0, 0, 0, "Error finishing setting up tun device: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ // if we get an error for seqnum < min_initok_nlseqnum,
+ // we must exit as initialization went wrong
+ if (config->ipv6_prefix.s6_addr[0])
+ min_initok_nlseqnum = 5 + 1; // idx + if + addr + 2*addr6
+ else
+ min_initok_nlseqnum = 3 + 1; // idx + if + addr