+ memcpy(&ifinfo, NLMSG_DATA(resp_nh), sizeof(ifinfo));
+ // got index
+ tunidx = ifinfo.ifi_index;
+ }
+ else
+ {
+ LOG(0, 0, 0, "Malformed answer getting tun ifindex\n");
+ exit(1);
+ }
+ }
+ {
+ ssize_t err;
+ struct {
+ // interface setting
+ struct nlmsghdr nh;
+ struct ifinfomsg ifinfo __attribute__ ((aligned(NLMSG_ALIGNTO)));
+ struct rtattr txqlen_rta __attribute__ ((aligned(RTA_ALIGNTO)));
+ uint32_t txqlen;
+ struct rtattr mtu_rta __attribute__ ((aligned(RTA_ALIGNTO)));
+ uint32_t mtu;
+
+ // IPv4 address
+ struct nlmsghdr addr_nh __attribute __ ((aligned(NLMSG_ALIGNTO)));
+ struct ifaddrmsg ifaddr __attribute __ ((aligned(NLMSG_ALIGNTO)));
+ struct rtattr addr_rta __attribute __ ((aligned(RTA_ALIGNTO)));
+ struct in_addr addr;
+
+ // IPv6 LL address
+ struct nlmsghdr lladdr6_nh __attribute __ ((aligned(NLMSG_ALIGNTO)));
+ struct ifaddrmsg llifaddr6 __attribute __ ((aligned(NLMSG_ALIGNTO)));
+ struct rtattr lladdr6_rta __attribute __ ((aligned(RTA_ALIGNTO)));
+ struct in6_addr lladdr6;
+
+ // IPv6 global address
+ struct nlmsghdr addr6_nh __attribute __ ((aligned(NLMSG_ALIGNTO)));
+ struct ifaddrmsg ifaddr6 __attribute __ ((aligned(NLMSG_ALIGNTO)));
+ struct rtattr addr6_rta __attribute __ ((aligned(RTA_ALIGNTO)));
+ struct in6_addr addr6;
+
+ // end header
+ struct nlmsghdr end_nh __attribute __ ((aligned(NLMSG_ALIGNTO)));
+ } req;
+ char buf[4096];
+
+ memset(&req, 0, sizeof(req));
+
+ req.nh.nlmsg_type = RTM_NEWLINK;
+ req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_MULTI;
+
+ req.ifinfo = ifinfo;
+ req.ifinfo.ifi_flags |= IFF_UP; // set interface up
+ req.ifinfo.ifi_change = IFF_UP; // only change this flag
+
+ req.txqlen_rta.rta_len = RTA_LENGTH(sizeof(req.txqlen));
+ req.txqlen_rta.rta_type = IFLA_TXQLEN;
+ /* Bump up the qlen to deal with bursts from the network */
+ req.txqlen = 1000;
+
+ req.mtu_rta.rta_len = RTA_LENGTH(sizeof(req.txqlen));
+ req.mtu_rta.rta_type = IFLA_MTU;
+ /* set MTU to modem MRU */
+ req.mtu = MRU;
+
+ req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifinfo)
+ + req.txqlen_rla.rta_len
+ + req.mtu_rta.rta_len);
+
+ req.addr_nh.nlmsg_type = RTM_NEWADDR;
+ req.addr_nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_MULTI;
+
+ req.ifaddr.ifa_family = AF_INET;
+ req.ifaddr.ifa_prefixlen = 32;
+ req.ifaddr.ifa_index = ifinfo.ifi_index;
+
+ req.addr_rta.rta_len = RTA_LENGTH(sizeof(req.addr));
+ req.addr_rta.rta_type = IFA_ADDRESS;
+ req.addr.s_addr = config->bind_address ?
+ config->bind_address : 0x01010101; // 1.1.1.1
+
+ req.addr_nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifaddr)
+ + req.addr_rta.rta_len);
+
+ // Only setup IPv6 on the tun device if we have a configured prefix
+ if (config->ipv6_prefix.s6_addr[0]) {
+ req.lladdr6_nh.nlmsg_type = RTM_NEWADDR;
+ req.lladdr6_nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_MULTI;
+
+ req.llifaddr6.ifa_family = AF_INET6;
+ req.llifaddr6.ifa_prefixlen = 64;
+ req.llifaddr6.ifa_scope = RT_SCOPE_LINK;
+ req.llifaddr6.ifa_index = ifinfo.ifi_index;
+
+ req.lladdr6_rta.rta_len = RTA_LENGTH(sizeof(req.lladdr6));
+ req.lladdr6_rta.rta_type = IFA_LOCAL;
+ // Link local address is FE80::1
+ memset(&req.lladdr6, 0, sizeof(req.lladdr6));
+ req.lladdr6.s6_addr[0] = 0xFE;
+ req.lladdr6.s6_addr[1] = 0x80;
+ req.lladdr6.s6_addr[15] = 1;
+
+ req.lladdr6_nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifaddr)
+ + req.lladdr6_rta.rta_len);
+
+ req.addr6_nh.nlmsg_type = RTM_NEWADDR;
+ req.addr6_nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_MULTI;
+
+ req.ifaddr6.ifa_family = AF_INET6;
+ req.ifaddr6.ifa_prefixlen = 64;
+ req.ifaddr6.ifa_scope = RT_SCOPE_UNIVERSE;
+ req.ifaddr6.ifa_index = ifinfo.ifi_index;
+
+ req.addr6_rta.rta_len = RTA_LENGTH(sizeof(req.addr6));
+ req.addr6_rta.rta_type = IFA_LOCAL;
+ // Global address is prefix::1
+ req.addr6 = config->ipv6_prefix;
+ req.addr6.s6_addr[15] = 1;
+
+ req.lladdr6_nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifaddr)
+ + req.lladdr6_rta.rta_len);
+
+ req.end_nh.nlmsg_type = NLMSG_DONE;
+ req.end_nh.nlmsg_len = NLMSG_LENGTH(0);
+ }
+ else
+ {
+ // we terminate at the beginning of IPv6 stuff
+ req.lladdr6_nh.nlmsg_type = NLMSG_DONE;
+ req.lladdr6_nh.nlmsg_len = NLMSG_LENGTH(0);
+ }
+
+ if (netlink_send(&req.nh, 1) < 0)
+ LOG(0, 0, 0, "Error setting up tun device: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ if (netlink_recv(buf, sizeof(buf)) < 0)
+ {
+ LOG(0, 0, 0, "Error waiting ack for setting up tun device: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ resp_nh = (struct nlmsghdr *)buf;
+ if ((err = netlink_check_ack(resp_nh, 4)))
+ {
+ if (err < 0)
+ LOG(0, 0, 0, "Error while receiving tun device ack: %s\n", strerror(errno));
+ else
+ LOG(0, 0, 0, "Error while receiving tun device ack\n", strerror(errno));
+ exit(1);