// https://syzkaller.appspot.com/bug?id=7d0e7e183df07b0c306cca5dfd022a64c302dd4f
// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <arpa/inet.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <unistd.h>

#include <linux/capability.h>
#include <linux/genetlink.h>
#include <linux/if_addr.h>
#include <linux/if_ether.h>
#include <linux/if_link.h>
#include <linux/if_tun.h>
#include <linux/in6.h>
#include <linux/ip.h>
#include <linux/neighbour.h>
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/nl80211.h>
#include <linux/rtnetlink.h>
#include <linux/tcp.h>
#include <linux/veth.h>

static unsigned long long procid;

static bool write_file(const char* file, const char* what, ...)
{
  char buf[1024];
  va_list args;
  va_start(args, what);
  vsnprintf(buf, sizeof(buf), what, args);
  va_end(args);
  buf[sizeof(buf) - 1] = 0;
  int len = strlen(buf);
  int fd = open(file, O_WRONLY | O_CLOEXEC);
  if (fd == -1)
    return false;
  if (write(fd, buf, len) != len) {
    int err = errno;
    close(fd);
    errno = err;
    return false;
  }
  close(fd);
  return true;
}

struct nlmsg {
  char* pos;
  int nesting;
  struct nlattr* nested[8];
  char buf[4096];
};

static void netlink_init(struct nlmsg* nlmsg, int typ, int flags,
                         const void* data, int size)
{
  memset(nlmsg, 0, sizeof(*nlmsg));
  struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
  hdr->nlmsg_type = typ;
  hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
  memcpy(hdr + 1, data, size);
  nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size);
}

static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data,
                         int size)
{
  struct nlattr* attr = (struct nlattr*)nlmsg->pos;
  attr->nla_len = sizeof(*attr) + size;
  attr->nla_type = typ;
  if (size > 0)
    memcpy(attr + 1, data, size);
  nlmsg->pos += NLMSG_ALIGN(attr->nla_len);
}

static void netlink_nest(struct nlmsg* nlmsg, int typ)
{
  struct nlattr* attr = (struct nlattr*)nlmsg->pos;
  attr->nla_type = typ;
  nlmsg->pos += sizeof(*attr);
  nlmsg->nested[nlmsg->nesting++] = attr;
}

static void netlink_done(struct nlmsg* nlmsg)
{
  struct nlattr* attr = nlmsg->nested[--nlmsg->nesting];
  attr->nla_len = nlmsg->pos - (char*)attr;
}

static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type,
                            int* reply_len)
{
  if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting)
    exit(1);
  struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
  hdr->nlmsg_len = nlmsg->pos - nlmsg->buf;
  struct sockaddr_nl addr;
  memset(&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  unsigned n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0,
                      (struct sockaddr*)&addr, sizeof(addr));
  if (n != hdr->nlmsg_len)
    exit(1);
  n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
  if (reply_len)
    *reply_len = 0;
  if (hdr->nlmsg_type == NLMSG_DONE)
    return 0;
  if (n < sizeof(struct nlmsghdr))
    exit(1);
  if (reply_len && hdr->nlmsg_type == reply_type) {
    *reply_len = n;
    return 0;
  }
  if (n < sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))
    exit(1);
  if (hdr->nlmsg_type != NLMSG_ERROR)
    exit(1);
  return ((struct nlmsgerr*)(hdr + 1))->error;
}

static int netlink_send(struct nlmsg* nlmsg, int sock)
{
  return netlink_send_ext(nlmsg, sock, 0, NULL);
}

static int netlink_query_family_id(struct nlmsg* nlmsg, int sock,
                                   const char* family_name)
{
  struct genlmsghdr genlhdr;
  memset(&genlhdr, 0, sizeof(genlhdr));
  genlhdr.cmd = CTRL_CMD_GETFAMILY;
  netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr));
  netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, family_name,
               strnlen(family_name, GENL_NAMSIZ - 1) + 1);
  int n = 0;
  int err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n);
  if (err < 0) {
    return -1;
  }
  uint16_t id = 0;
  struct nlattr* attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN +
                                         NLMSG_ALIGN(sizeof(genlhdr)));
  for (; (char*)attr < nlmsg->buf + n;
       attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
    if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
      id = *(uint16_t*)(attr + 1);
      break;
    }
  }
  if (!id) {
    return -1;
  }
  recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
  return id;
}

static int netlink_next_msg(struct nlmsg* nlmsg, unsigned int offset,
                            unsigned int total_len)
{
  struct nlmsghdr* hdr = (struct nlmsghdr*)(nlmsg->buf + offset);
  if (offset == total_len || offset + hdr->nlmsg_len > total_len)
    return -1;
  return hdr->nlmsg_len;
}

static void netlink_add_device_impl(struct nlmsg* nlmsg, const char* type,
                                    const char* name)
{
  struct ifinfomsg hdr;
  memset(&hdr, 0, sizeof(hdr));
  netlink_init(nlmsg, RTM_NEWLINK, NLM_F_EXCL | NLM_F_CREATE, &hdr,
               sizeof(hdr));
  if (name)
    netlink_attr(nlmsg, IFLA_IFNAME, name, strlen(name));
  netlink_nest(nlmsg, IFLA_LINKINFO);
  netlink_attr(nlmsg, IFLA_INFO_KIND, type, strlen(type));
}

static void netlink_add_device(struct nlmsg* nlmsg, int sock, const char* type,
                               const char* name)
{
  netlink_add_device_impl(nlmsg, type, name);
  netlink_done(nlmsg);
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static void netlink_add_veth(struct nlmsg* nlmsg, int sock, const char* name,
                             const char* peer)
{
  netlink_add_device_impl(nlmsg, "veth", name);
  netlink_nest(nlmsg, IFLA_INFO_DATA);
  netlink_nest(nlmsg, VETH_INFO_PEER);
  nlmsg->pos += sizeof(struct ifinfomsg);
  netlink_attr(nlmsg, IFLA_IFNAME, peer, strlen(peer));
  netlink_done(nlmsg);
  netlink_done(nlmsg);
  netlink_done(nlmsg);
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static void netlink_add_hsr(struct nlmsg* nlmsg, int sock, const char* name,
                            const char* slave1, const char* slave2)
{
  netlink_add_device_impl(nlmsg, "hsr", name);
  netlink_nest(nlmsg, IFLA_INFO_DATA);
  int ifindex1 = if_nametoindex(slave1);
  netlink_attr(nlmsg, IFLA_HSR_SLAVE1, &ifindex1, sizeof(ifindex1));
  int ifindex2 = if_nametoindex(slave2);
  netlink_attr(nlmsg, IFLA_HSR_SLAVE2, &ifindex2, sizeof(ifindex2));
  netlink_done(nlmsg);
  netlink_done(nlmsg);
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static void netlink_add_linked(struct nlmsg* nlmsg, int sock, const char* type,
                               const char* name, const char* link)
{
  netlink_add_device_impl(nlmsg, type, name);
  netlink_done(nlmsg);
  int ifindex = if_nametoindex(link);
  netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static void netlink_add_vlan(struct nlmsg* nlmsg, int sock, const char* name,
                             const char* link, uint16_t id, uint16_t proto)
{
  netlink_add_device_impl(nlmsg, "vlan", name);
  netlink_nest(nlmsg, IFLA_INFO_DATA);
  netlink_attr(nlmsg, IFLA_VLAN_ID, &id, sizeof(id));
  netlink_attr(nlmsg, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
  netlink_done(nlmsg);
  netlink_done(nlmsg);
  int ifindex = if_nametoindex(link);
  netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static void netlink_add_macvlan(struct nlmsg* nlmsg, int sock, const char* name,
                                const char* link)
{
  netlink_add_device_impl(nlmsg, "macvlan", name);
  netlink_nest(nlmsg, IFLA_INFO_DATA);
  uint32_t mode = MACVLAN_MODE_BRIDGE;
  netlink_attr(nlmsg, IFLA_MACVLAN_MODE, &mode, sizeof(mode));
  netlink_done(nlmsg);
  netlink_done(nlmsg);
  int ifindex = if_nametoindex(link);
  netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static void netlink_add_geneve(struct nlmsg* nlmsg, int sock, const char* name,
                               uint32_t vni, struct in_addr* addr4,
                               struct in6_addr* addr6)
{
  netlink_add_device_impl(nlmsg, "geneve", name);
  netlink_nest(nlmsg, IFLA_INFO_DATA);
  netlink_attr(nlmsg, IFLA_GENEVE_ID, &vni, sizeof(vni));
  if (addr4)
    netlink_attr(nlmsg, IFLA_GENEVE_REMOTE, addr4, sizeof(*addr4));
  if (addr6)
    netlink_attr(nlmsg, IFLA_GENEVE_REMOTE6, addr6, sizeof(*addr6));
  netlink_done(nlmsg);
  netlink_done(nlmsg);
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

#define IFLA_IPVLAN_FLAGS 2
#define IPVLAN_MODE_L3S 2
#undef IPVLAN_F_VEPA
#define IPVLAN_F_VEPA 2

static void netlink_add_ipvlan(struct nlmsg* nlmsg, int sock, const char* name,
                               const char* link, uint16_t mode, uint16_t flags)
{
  netlink_add_device_impl(nlmsg, "ipvlan", name);
  netlink_nest(nlmsg, IFLA_INFO_DATA);
  netlink_attr(nlmsg, IFLA_IPVLAN_MODE, &mode, sizeof(mode));
  netlink_attr(nlmsg, IFLA_IPVLAN_FLAGS, &flags, sizeof(flags));
  netlink_done(nlmsg);
  netlink_done(nlmsg);
  int ifindex = if_nametoindex(link);
  netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static void netlink_device_change(struct nlmsg* nlmsg, int sock,
                                  const char* name, bool up, const char* master,
                                  const void* mac, int macsize,
                                  const char* new_name)
{
  struct ifinfomsg hdr;
  memset(&hdr, 0, sizeof(hdr));
  if (up)
    hdr.ifi_flags = hdr.ifi_change = IFF_UP;
  hdr.ifi_index = if_nametoindex(name);
  netlink_init(nlmsg, RTM_NEWLINK, 0, &hdr, sizeof(hdr));
  if (new_name)
    netlink_attr(nlmsg, IFLA_IFNAME, new_name, strlen(new_name));
  if (master) {
    int ifindex = if_nametoindex(master);
    netlink_attr(nlmsg, IFLA_MASTER, &ifindex, sizeof(ifindex));
  }
  if (macsize)
    netlink_attr(nlmsg, IFLA_ADDRESS, mac, macsize);
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static int netlink_add_addr(struct nlmsg* nlmsg, int sock, const char* dev,
                            const void* addr, int addrsize)
{
  struct ifaddrmsg hdr;
  memset(&hdr, 0, sizeof(hdr));
  hdr.ifa_family = addrsize == 4 ? AF_INET : AF_INET6;
  hdr.ifa_prefixlen = addrsize == 4 ? 24 : 120;
  hdr.ifa_scope = RT_SCOPE_UNIVERSE;
  hdr.ifa_index = if_nametoindex(dev);
  netlink_init(nlmsg, RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, &hdr,
               sizeof(hdr));
  netlink_attr(nlmsg, IFA_LOCAL, addr, addrsize);
  netlink_attr(nlmsg, IFA_ADDRESS, addr, addrsize);
  return netlink_send(nlmsg, sock);
}

static void netlink_add_addr4(struct nlmsg* nlmsg, int sock, const char* dev,
                              const char* addr)
{
  struct in_addr in_addr;
  inet_pton(AF_INET, addr, &in_addr);
  int err = netlink_add_addr(nlmsg, sock, dev, &in_addr, sizeof(in_addr));
  (void)err;
}

static void netlink_add_addr6(struct nlmsg* nlmsg, int sock, const char* dev,
                              const char* addr)
{
  struct in6_addr in6_addr;
  inet_pton(AF_INET6, addr, &in6_addr);
  int err = netlink_add_addr(nlmsg, sock, dev, &in6_addr, sizeof(in6_addr));
  (void)err;
}

static void netlink_add_neigh(struct nlmsg* nlmsg, int sock, const char* name,
                              const void* addr, int addrsize, const void* mac,
                              int macsize)
{
  struct ndmsg hdr;
  memset(&hdr, 0, sizeof(hdr));
  hdr.ndm_family = addrsize == 4 ? AF_INET : AF_INET6;
  hdr.ndm_ifindex = if_nametoindex(name);
  hdr.ndm_state = NUD_PERMANENT;
  netlink_init(nlmsg, RTM_NEWNEIGH, NLM_F_EXCL | NLM_F_CREATE, &hdr,
               sizeof(hdr));
  netlink_attr(nlmsg, NDA_DST, addr, addrsize);
  netlink_attr(nlmsg, NDA_LLADDR, mac, macsize);
  int err = netlink_send(nlmsg, sock);
  (void)err;
}

static struct nlmsg nlmsg;

static int tunfd = -1;

#define TUN_IFACE "syz_tun"
#define LOCAL_MAC 0xaaaaaaaaaaaa
#define REMOTE_MAC 0xaaaaaaaaaabb
#define LOCAL_IPV4 "172.20.20.170"
#define REMOTE_IPV4 "172.20.20.187"
#define LOCAL_IPV6 "fe80::aa"
#define REMOTE_IPV6 "fe80::bb"

#define IFF_NAPI 0x0010

static void initialize_tun(void)
{
  tunfd = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
  if (tunfd == -1) {
    printf("tun: can't open /dev/net/tun: please enable CONFIG_TUN=y\n");
    printf("otherwise fuzzing or reproducing might not work as intended\n");
    return;
  }
  const int kTunFd = 240;
  if (dup2(tunfd, kTunFd) < 0)
    exit(1);
  close(tunfd);
  tunfd = kTunFd;
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, TUN_IFACE, IFNAMSIZ);
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  if (ioctl(tunfd, TUNSETIFF, (void*)&ifr) < 0) {
    exit(1);
  }
  char sysctl[64];
  sprintf(sysctl, "/proc/sys/net/ipv6/conf/%s/accept_dad", TUN_IFACE);
  write_file(sysctl, "0");
  sprintf(sysctl, "/proc/sys/net/ipv6/conf/%s/router_solicitations", TUN_IFACE);
  write_file(sysctl, "0");
  int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sock == -1)
    exit(1);
  netlink_add_addr4(&nlmsg, sock, TUN_IFACE, LOCAL_IPV4);
  netlink_add_addr6(&nlmsg, sock, TUN_IFACE, LOCAL_IPV6);
  uint64_t macaddr = REMOTE_MAC;
  struct in_addr in_addr;
  inet_pton(AF_INET, REMOTE_IPV4, &in_addr);
  netlink_add_neigh(&nlmsg, sock, TUN_IFACE, &in_addr, sizeof(in_addr),
                    &macaddr, ETH_ALEN);
  struct in6_addr in6_addr;
  inet_pton(AF_INET6, REMOTE_IPV6, &in6_addr);
  netlink_add_neigh(&nlmsg, sock, TUN_IFACE, &in6_addr, sizeof(in6_addr),
                    &macaddr, ETH_ALEN);
  macaddr = LOCAL_MAC;
  netlink_device_change(&nlmsg, sock, TUN_IFACE, true, 0, &macaddr, ETH_ALEN,
                        NULL);
  close(sock);
}

#define DEVLINK_FAMILY_NAME "devlink"

#define DEVLINK_CMD_PORT_GET 5
#define DEVLINK_ATTR_BUS_NAME 1
#define DEVLINK_ATTR_DEV_NAME 2
#define DEVLINK_ATTR_NETDEV_NAME 7

static struct nlmsg nlmsg2;

static void initialize_devlink_ports(const char* bus_name, const char* dev_name,
                                     const char* netdev_prefix)
{
  struct genlmsghdr genlhdr;
  int len, total_len, id, err, offset;
  uint16_t netdev_index;
  int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  if (sock == -1)
    exit(1);
  int rtsock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (rtsock == -1)
    exit(1);
  id = netlink_query_family_id(&nlmsg, sock, DEVLINK_FAMILY_NAME);
  if (id == -1)
    goto error;
  memset(&genlhdr, 0, sizeof(genlhdr));
  genlhdr.cmd = DEVLINK_CMD_PORT_GET;
  netlink_init(&nlmsg, id, NLM_F_DUMP, &genlhdr, sizeof(genlhdr));
  netlink_attr(&nlmsg, DEVLINK_ATTR_BUS_NAME, bus_name, strlen(bus_name) + 1);
  netlink_attr(&nlmsg, DEVLINK_ATTR_DEV_NAME, dev_name, strlen(dev_name) + 1);
  err = netlink_send_ext(&nlmsg, sock, id, &total_len);
  if (err < 0) {
    goto error;
  }
  offset = 0;
  netdev_index = 0;
  while ((len = netlink_next_msg(&nlmsg, offset, total_len)) != -1) {
    struct nlattr* attr = (struct nlattr*)(nlmsg.buf + offset + NLMSG_HDRLEN +
                                           NLMSG_ALIGN(sizeof(genlhdr)));
    for (; (char*)attr < nlmsg.buf + offset + len;
         attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
      if (attr->nla_type == DEVLINK_ATTR_NETDEV_NAME) {
        char* port_name;
        char netdev_name[IFNAMSIZ];
        port_name = (char*)(attr + 1);
        snprintf(netdev_name, sizeof(netdev_name), "%s%d", netdev_prefix,
                 netdev_index);
        netlink_device_change(&nlmsg2, rtsock, port_name, true, 0, 0, 0,
                              netdev_name);
        break;
      }
    }
    offset += len;
    netdev_index++;
  }
error:
  close(rtsock);
  close(sock);
}

#define WIFI_INITIAL_DEVICE_COUNT 2
#define WIFI_MAC_BASE                                                          \
  {                                                                            \
    0x08, 0x02, 0x11, 0x00, 0x00, 0x00                                         \
  }
#define WIFI_IBSS_BSSID                                                        \
  {                                                                            \
    0x50, 0x50, 0x50, 0x50, 0x50, 0x50                                         \
  }
#define WIFI_IBSS_SSID                                                         \
  {                                                                            \
    0x10, 0x10, 0x10, 0x10, 0x10, 0x10                                         \
  }
#define WIFI_DEFAULT_FREQUENCY 2412
#define WIFI_DEFAULT_SIGNAL 0
#define WIFI_DEFAULT_RX_RATE 1
#define HWSIM_CMD_REGISTER 1
#define HWSIM_CMD_FRAME 2
#define HWSIM_CMD_NEW_RADIO 4
#define HWSIM_ATTR_SUPPORT_P2P_DEVICE 14
#define HWSIM_ATTR_PERM_ADDR 22

#define IF_OPER_UP 6
struct join_ibss_props {
  int wiphy_freq;
  bool wiphy_freq_fixed;
  uint8_t* mac;
  uint8_t* ssid;
  int ssid_len;
};

static int set_interface_state(const char* interface_name, int on)
{
  struct ifreq ifr;
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, interface_name);
  int ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
  if (ret < 0) {
    close(sock);
    return -1;
  }
  if (on)
    ifr.ifr_flags |= IFF_UP;
  else
    ifr.ifr_flags &= ~IFF_UP;
  ret = ioctl(sock, SIOCSIFFLAGS, &ifr);
  close(sock);
  if (ret < 0) {
    return -1;
  }
  return 0;
}

static int nl80211_set_interface(struct nlmsg* nlmsg, int sock,
                                 int nl80211_family, uint32_t ifindex,
                                 uint32_t iftype)
{
  struct genlmsghdr genlhdr;
  memset(&genlhdr, 0, sizeof(genlhdr));
  genlhdr.cmd = NL80211_CMD_SET_INTERFACE;
  netlink_init(nlmsg, nl80211_family, 0, &genlhdr, sizeof(genlhdr));
  netlink_attr(nlmsg, NL80211_ATTR_IFINDEX, &ifindex, sizeof(ifindex));
  netlink_attr(nlmsg, NL80211_ATTR_IFTYPE, &iftype, sizeof(iftype));
  int err = netlink_send(nlmsg, sock);
  if (err < 0) {
    return -1;
  }
  return 0;
}

static int nl80211_join_ibss(struct nlmsg* nlmsg, int sock, int nl80211_family,
                             uint32_t ifindex, struct join_ibss_props* props)
{
  struct genlmsghdr genlhdr;
  memset(&genlhdr, 0, sizeof(genlhdr));
  genlhdr.cmd = NL80211_CMD_JOIN_IBSS;
  netlink_init(nlmsg, nl80211_family, 0, &genlhdr, sizeof(genlhdr));
  netlink_attr(nlmsg, NL80211_ATTR_IFINDEX, &ifindex, sizeof(ifindex));
  netlink_attr(nlmsg, NL80211_ATTR_SSID, props->ssid, props->ssid_len);
  netlink_attr(nlmsg, NL80211_ATTR_WIPHY_FREQ, &(props->wiphy_freq),
               sizeof(props->wiphy_freq));
  if (props->mac)
    netlink_attr(nlmsg, NL80211_ATTR_MAC, props->mac, ETH_ALEN);
  if (props->wiphy_freq_fixed)
    netlink_attr(nlmsg, NL80211_ATTR_FREQ_FIXED, NULL, 0);
  int err = netlink_send(nlmsg, sock);
  if (err < 0) {
    return -1;
  }
  return 0;
}

static int get_ifla_operstate(struct nlmsg* nlmsg, int ifindex)
{
  struct ifinfomsg info;
  memset(&info, 0, sizeof(info));
  info.ifi_family = AF_UNSPEC;
  info.ifi_index = ifindex;
  int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sock == -1) {
    return -1;
  }
  netlink_init(nlmsg, RTM_GETLINK, 0, &info, sizeof(info));
  int n;
  int err = netlink_send_ext(nlmsg, sock, RTM_NEWLINK, &n);
  close(sock);
  if (err) {
    return -1;
  }
  struct rtattr* attr = IFLA_RTA(NLMSG_DATA(nlmsg->buf));
  for (; RTA_OK(attr, n); attr = RTA_NEXT(attr, n)) {
    if (attr->rta_type == IFLA_OPERSTATE)
      return *((int32_t*)RTA_DATA(attr));
  }
  return -1;
}

static int await_ifla_operstate(struct nlmsg* nlmsg, char* interface,
                                int operstate)
{
  int ifindex = if_nametoindex(interface);
  while (true) {
    usleep(1000);
    int ret = get_ifla_operstate(nlmsg, ifindex);
    if (ret < 0)
      return ret;
    if (ret == operstate)
      return 0;
  }
  return 0;
}

static int nl80211_setup_ibss_interface(struct nlmsg* nlmsg, int sock,
                                        int nl80211_family_id, char* interface,
                                        struct join_ibss_props* ibss_props)
{
  int ifindex = if_nametoindex(interface);
  if (ifindex == 0) {
    return -1;
  }
  int ret = nl80211_set_interface(nlmsg, sock, nl80211_family_id, ifindex,
                                  NL80211_IFTYPE_ADHOC);
  if (ret < 0) {
    return -1;
  }
  ret = set_interface_state(interface, 1);
  if (ret < 0) {
    return -1;
  }
  ret = nl80211_join_ibss(nlmsg, sock, nl80211_family_id, ifindex, ibss_props);
  if (ret < 0) {
    return -1;
  }
  return 0;
}

static int hwsim80211_create_device(struct nlmsg* nlmsg, int sock,
                                    int hwsim_family,
                                    uint8_t mac_addr[ETH_ALEN])
{
  struct genlmsghdr genlhdr;
  memset(&genlhdr, 0, sizeof(genlhdr));
  genlhdr.cmd = HWSIM_CMD_NEW_RADIO;
  netlink_init(nlmsg, hwsim_family, 0, &genlhdr, sizeof(genlhdr));
  netlink_attr(nlmsg, HWSIM_ATTR_SUPPORT_P2P_DEVICE, NULL, 0);
  netlink_attr(nlmsg, HWSIM_ATTR_PERM_ADDR, mac_addr, ETH_ALEN);
  int err = netlink_send(nlmsg, sock);
  if (err < 0) {
    return -1;
  }
  return 0;
}

static void initialize_wifi_devices(void)
{
  uint8_t mac_addr[6] = WIFI_MAC_BASE;
  int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  if (sock < 0) {
    return;
  }
  int hwsim_family_id = netlink_query_family_id(&nlmsg, sock, "MAC80211_HWSIM");
  int nl80211_family_id = netlink_query_family_id(&nlmsg, sock, "nl80211");
  uint8_t ssid[] = WIFI_IBSS_SSID;
  uint8_t bssid[] = WIFI_IBSS_BSSID;
  struct join_ibss_props ibss_props = {.wiphy_freq = WIFI_DEFAULT_FREQUENCY,
                                       .wiphy_freq_fixed = true,
                                       .mac = bssid,
                                       .ssid = ssid,
                                       .ssid_len = sizeof(ssid)};
  for (int device_id = 0; device_id < WIFI_INITIAL_DEVICE_COUNT; device_id++) {
    mac_addr[5] = device_id;
    int ret = hwsim80211_create_device(&nlmsg, sock, hwsim_family_id, mac_addr);
    if (ret < 0)
      exit(1);
    char interface[6] = "wlan0";
    interface[4] += device_id;
    if (nl80211_setup_ibss_interface(&nlmsg, sock, nl80211_family_id, interface,
                                     &ibss_props) < 0)
      exit(1);
  }
  for (int device_id = 0; device_id < WIFI_INITIAL_DEVICE_COUNT; device_id++) {
    char interface[6] = "wlan0";
    interface[4] += device_id;
    int ret = await_ifla_operstate(&nlmsg, interface, IF_OPER_UP);
    if (ret < 0)
      exit(1);
  }
  close(sock);
}

#define DEV_IPV4 "172.20.20.%d"
#define DEV_IPV6 "fe80::%02x"
#define DEV_MAC 0x00aaaaaaaaaa

static void netdevsim_add(unsigned int addr, unsigned int port_count)
{
  char buf[16];
  sprintf(buf, "%u %u", addr, port_count);
  if (write_file("/sys/bus/netdevsim/new_device", buf)) {
    snprintf(buf, sizeof(buf), "netdevsim%d", addr);
    initialize_devlink_ports("netdevsim", buf, "netdevsim");
  }
}

#define WG_GENL_NAME "wireguard"
enum wg_cmd {
  WG_CMD_GET_DEVICE,
  WG_CMD_SET_DEVICE,
};
enum wgdevice_attribute {
  WGDEVICE_A_UNSPEC,
  WGDEVICE_A_IFINDEX,
  WGDEVICE_A_IFNAME,
  WGDEVICE_A_PRIVATE_KEY,
  WGDEVICE_A_PUBLIC_KEY,
  WGDEVICE_A_FLAGS,
  WGDEVICE_A_LISTEN_PORT,
  WGDEVICE_A_FWMARK,
  WGDEVICE_A_PEERS,
};
enum wgpeer_attribute {
  WGPEER_A_UNSPEC,
  WGPEER_A_PUBLIC_KEY,
  WGPEER_A_PRESHARED_KEY,
  WGPEER_A_FLAGS,
  WGPEER_A_ENDPOINT,
  WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  WGPEER_A_LAST_HANDSHAKE_TIME,
  WGPEER_A_RX_BYTES,
  WGPEER_A_TX_BYTES,
  WGPEER_A_ALLOWEDIPS,
  WGPEER_A_PROTOCOL_VERSION,
};
enum wgallowedip_attribute {
  WGALLOWEDIP_A_UNSPEC,
  WGALLOWEDIP_A_FAMILY,
  WGALLOWEDIP_A_IPADDR,
  WGALLOWEDIP_A_CIDR_MASK,
};

static void netlink_wireguard_setup(void)
{
  const char ifname_a[] = "wg0";
  const char ifname_b[] = "wg1";
  const char ifname_c[] = "wg2";
  const char private_a[] = "\xa0\x5c\xa8\x4f\x6c\x9c\x8e\x38\x53\xe2\xfd\x7a"
                           "\x70\xae\x0f\xb2\x0f\xa1\x52\x60\x0c\xb0\x08\x45"
                           "\x17\x4f\x08\x07\x6f\x8d\x78\x43";
  const char private_b[] = "\xb0\x80\x73\xe8\xd4\x4e\x91\xe3\xda\x92\x2c\x22"
                           "\x43\x82\x44\xbb\x88\x5c\x69\xe2\x69\xc8\xe9\xd8"
                           "\x35\xb1\x14\x29\x3a\x4d\xdc\x6e";
  const char private_c[] = "\xa0\xcb\x87\x9a\x47\xf5\xbc\x64\x4c\x0e\x69\x3f"
                           "\xa6\xd0\x31\xc7\x4a\x15\x53\xb6\xe9\x01\xb9\xff"
                           "\x2f\x51\x8c\x78\x04\x2f\xb5\x42";
  const char public_a[] = "\x97\x5c\x9d\x81\xc9\x83\xc8\x20\x9e\xe7\x81\x25\x4b"
                          "\x89\x9f\x8e\xd9\x25\xae\x9f\x09\x23\xc2\x3c\x62\xf5"
                          "\x3c\x57\xcd\xbf\x69\x1c";
  const char public_b[] = "\xd1\x73\x28\x99\xf6\x11\xcd\x89\x94\x03\x4d\x7f\x41"
                          "\x3d\xc9\x57\x63\x0e\x54\x93\xc2\x85\xac\xa4\x00\x65"
                          "\xcb\x63\x11\xbe\x69\x6b";
  const char public_c[] = "\xf4\x4d\xa3\x67\xa8\x8e\xe6\x56\x4f\x02\x02\x11\x45"
                          "\x67\x27\x08\x2f\x5c\xeb\xee\x8b\x1b\xf5\xeb\x73\x37"
                          "\x34\x1b\x45\x9b\x39\x22";
  const uint16_t listen_a = 20001;
  const uint16_t listen_b = 20002;
  const uint16_t listen_c = 20003;
  const uint16_t af_inet = AF_INET;
  const uint16_t af_inet6 = AF_INET6;
  const struct sockaddr_in endpoint_b_v4 = {
      .sin_family = AF_INET,
      .sin_port = htons(listen_b),
      .sin_addr = {htonl(INADDR_LOOPBACK)}};
  const struct sockaddr_in endpoint_c_v4 = {
      .sin_family = AF_INET,
      .sin_port = htons(listen_c),
      .sin_addr = {htonl(INADDR_LOOPBACK)}};
  struct sockaddr_in6 endpoint_a_v6 = {.sin6_family = AF_INET6,
                                       .sin6_port = htons(listen_a)};
  endpoint_a_v6.sin6_addr = in6addr_loopback;
  struct sockaddr_in6 endpoint_c_v6 = {.sin6_family = AF_INET6,
                                       .sin6_port = htons(listen_c)};
  endpoint_c_v6.sin6_addr = in6addr_loopback;
  const struct in_addr first_half_v4 = {0};
  const struct in_addr second_half_v4 = {(uint32_t)htonl(128 << 24)};
  const struct in6_addr first_half_v6 = {{{0}}};
  const struct in6_addr second_half_v6 = {{{0x80}}};
  const uint8_t half_cidr = 1;
  const uint16_t persistent_keepalives[] = {1, 3, 7, 9, 14, 19};
  struct genlmsghdr genlhdr = {.cmd = WG_CMD_SET_DEVICE, .version = 1};
  int sock;
  int id, err;
  sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  if (sock == -1) {
    return;
  }
  id = netlink_query_family_id(&nlmsg, sock, WG_GENL_NAME);
  if (id == -1)
    goto error;
  netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
  netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_a, strlen(ifname_a) + 1);
  netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_a, 32);
  netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_a, 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_b, 32);
  netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_b_v4,
               sizeof(endpoint_b_v4));
  netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
               &persistent_keepalives[0], 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
               sizeof(first_half_v4));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
               sizeof(first_half_v6));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_c, 32);
  netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_c_v6,
               sizeof(endpoint_c_v6));
  netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
               &persistent_keepalives[1], 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
               sizeof(second_half_v4));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
               sizeof(second_half_v6));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  err = netlink_send(&nlmsg, sock);
  if (err < 0) {
  }
  netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
  netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_b, strlen(ifname_b) + 1);
  netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_b, 32);
  netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_b, 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_a, 32);
  netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_a_v6,
               sizeof(endpoint_a_v6));
  netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
               &persistent_keepalives[2], 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
               sizeof(first_half_v4));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
               sizeof(first_half_v6));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_c, 32);
  netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_c_v4,
               sizeof(endpoint_c_v4));
  netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
               &persistent_keepalives[3], 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
               sizeof(second_half_v4));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
               sizeof(second_half_v6));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  err = netlink_send(&nlmsg, sock);
  if (err < 0) {
  }
  netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
  netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_c, strlen(ifname_c) + 1);
  netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_c, 32);
  netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_c, 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_a, 32);
  netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_a_v6,
               sizeof(endpoint_a_v6));
  netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
               &persistent_keepalives[4], 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
               sizeof(first_half_v4));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
               sizeof(first_half_v6));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_b, 32);
  netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_b_v4,
               sizeof(endpoint_b_v4));
  netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
               &persistent_keepalives[5], 2);
  netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
               sizeof(second_half_v4));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
               sizeof(second_half_v6));
  netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  netlink_done(&nlmsg);
  err = netlink_send(&nlmsg, sock);
  if (err < 0) {
  }

error:
  close(sock);
}
static void initialize_netdevices(void)
{
  char netdevsim[16];
  sprintf(netdevsim, "netdevsim%d", (int)procid);
  struct {
    const char* type;
    const char* dev;
  } devtypes[] = {
      {"ip6gretap", "ip6gretap0"}, {"bridge", "bridge0"},
      {"vcan", "vcan0"},           {"bond", "bond0"},
      {"team", "team0"},           {"dummy", "dummy0"},
      {"nlmon", "nlmon0"},         {"caif", "caif0"},
      {"batadv", "batadv0"},       {"vxcan", "vxcan1"},
      {"netdevsim", netdevsim},    {"veth", 0},
      {"xfrm", "xfrm0"},           {"wireguard", "wg0"},
      {"wireguard", "wg1"},        {"wireguard", "wg2"},
  };
  const char* devmasters[] = {"bridge", "bond", "team", "batadv"};
  struct {
    const char* name;
    int macsize;
    bool noipv6;
  } devices[] = {
      {"lo", ETH_ALEN},
      {"sit0", 0},
      {"bridge0", ETH_ALEN},
      {"vcan0", 0, true},
      {"tunl0", 0},
      {"gre0", 0},
      {"gretap0", ETH_ALEN},
      {"ip_vti0", 0},
      {"ip6_vti0", 0},
      {"ip6tnl0", 0},
      {"ip6gre0", 0},
      {"ip6gretap0", ETH_ALEN},
      {"erspan0", ETH_ALEN},
      {"bond0", ETH_ALEN},
      {"veth0", ETH_ALEN},
      {"veth1", ETH_ALEN},
      {"team0", ETH_ALEN},
      {"veth0_to_bridge", ETH_ALEN},
      {"veth1_to_bridge", ETH_ALEN},
      {"veth0_to_bond", ETH_ALEN},
      {"veth1_to_bond", ETH_ALEN},
      {"veth0_to_team", ETH_ALEN},
      {"veth1_to_team", ETH_ALEN},
      {"veth0_to_hsr", ETH_ALEN},
      {"veth1_to_hsr", ETH_ALEN},
      {"hsr0", 0},
      {"dummy0", ETH_ALEN},
      {"nlmon0", 0},
      {"vxcan0", 0, true},
      {"vxcan1", 0, true},
      {"caif0", ETH_ALEN},
      {"batadv0", ETH_ALEN},
      {netdevsim, ETH_ALEN},
      {"xfrm0", ETH_ALEN},
      {"veth0_virt_wifi", ETH_ALEN},
      {"veth1_virt_wifi", ETH_ALEN},
      {"virt_wifi0", ETH_ALEN},
      {"veth0_vlan", ETH_ALEN},
      {"veth1_vlan", ETH_ALEN},
      {"vlan0", ETH_ALEN},
      {"vlan1", ETH_ALEN},
      {"macvlan0", ETH_ALEN},
      {"macvlan1", ETH_ALEN},
      {"ipvlan0", ETH_ALEN},
      {"ipvlan1", ETH_ALEN},
      {"veth0_macvtap", ETH_ALEN},
      {"veth1_macvtap", ETH_ALEN},
      {"macvtap0", ETH_ALEN},
      {"macsec0", ETH_ALEN},
      {"veth0_to_batadv", ETH_ALEN},
      {"veth1_to_batadv", ETH_ALEN},
      {"batadv_slave_0", ETH_ALEN},
      {"batadv_slave_1", ETH_ALEN},
      {"geneve0", ETH_ALEN},
      {"geneve1", ETH_ALEN},
      {"wg0", 0},
      {"wg1", 0},
      {"wg2", 0},
  };
  int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sock == -1)
    exit(1);
  unsigned i;
  for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]); i++)
    netlink_add_device(&nlmsg, sock, devtypes[i].type, devtypes[i].dev);
  for (i = 0; i < sizeof(devmasters) / (sizeof(devmasters[0])); i++) {
    char master[32], slave0[32], veth0[32], slave1[32], veth1[32];
    sprintf(slave0, "%s_slave_0", devmasters[i]);
    sprintf(veth0, "veth0_to_%s", devmasters[i]);
    netlink_add_veth(&nlmsg, sock, slave0, veth0);
    sprintf(slave1, "%s_slave_1", devmasters[i]);
    sprintf(veth1, "veth1_to_%s", devmasters[i]);
    netlink_add_veth(&nlmsg, sock, slave1, veth1);
    sprintf(master, "%s0", devmasters[i]);
    netlink_device_change(&nlmsg, sock, slave0, false, master, 0, 0, NULL);
    netlink_device_change(&nlmsg, sock, slave1, false, master, 0, 0, NULL);
  }
  netlink_device_change(&nlmsg, sock, "bridge_slave_0", true, 0, 0, 0, NULL);
  netlink_device_change(&nlmsg, sock, "bridge_slave_1", true, 0, 0, 0, NULL);
  netlink_add_veth(&nlmsg, sock, "hsr_slave_0", "veth0_to_hsr");
  netlink_add_veth(&nlmsg, sock, "hsr_slave_1", "veth1_to_hsr");
  netlink_add_hsr(&nlmsg, sock, "hsr0", "hsr_slave_0", "hsr_slave_1");
  netlink_device_change(&nlmsg, sock, "hsr_slave_0", true, 0, 0, 0, NULL);
  netlink_device_change(&nlmsg, sock, "hsr_slave_1", true, 0, 0, 0, NULL);
  netlink_add_veth(&nlmsg, sock, "veth0_virt_wifi", "veth1_virt_wifi");
  netlink_add_linked(&nlmsg, sock, "virt_wifi", "virt_wifi0",
                     "veth1_virt_wifi");
  netlink_add_veth(&nlmsg, sock, "veth0_vlan", "veth1_vlan");
  netlink_add_vlan(&nlmsg, sock, "vlan0", "veth0_vlan", 0, htons(ETH_P_8021Q));
  netlink_add_vlan(&nlmsg, sock, "vlan1", "veth0_vlan", 1, htons(ETH_P_8021AD));
  netlink_add_macvlan(&nlmsg, sock, "macvlan0", "veth1_vlan");
  netlink_add_macvlan(&nlmsg, sock, "macvlan1", "veth1_vlan");
  netlink_add_ipvlan(&nlmsg, sock, "ipvlan0", "veth0_vlan", IPVLAN_MODE_L2, 0);
  netlink_add_ipvlan(&nlmsg, sock, "ipvlan1", "veth0_vlan", IPVLAN_MODE_L3S,
                     IPVLAN_F_VEPA);
  netlink_add_veth(&nlmsg, sock, "veth0_macvtap", "veth1_macvtap");
  netlink_add_linked(&nlmsg, sock, "macvtap", "macvtap0", "veth0_macvtap");
  netlink_add_linked(&nlmsg, sock, "macsec", "macsec0", "veth1_macvtap");
  char addr[32];
  sprintf(addr, DEV_IPV4, 14 + 10);
  struct in_addr geneve_addr4;
  if (inet_pton(AF_INET, addr, &geneve_addr4) <= 0)
    exit(1);
  struct in6_addr geneve_addr6;
  if (inet_pton(AF_INET6, "fc00::01", &geneve_addr6) <= 0)
    exit(1);
  netlink_add_geneve(&nlmsg, sock, "geneve0", 0, &geneve_addr4, 0);
  netlink_add_geneve(&nlmsg, sock, "geneve1", 1, 0, &geneve_addr6);
  netdevsim_add((int)procid, 4);
  netlink_wireguard_setup();
  for (i = 0; i < sizeof(devices) / (sizeof(devices[0])); i++) {
    char addr[32];
    sprintf(addr, DEV_IPV4, i + 10);
    netlink_add_addr4(&nlmsg, sock, devices[i].name, addr);
    if (!devices[i].noipv6) {
      sprintf(addr, DEV_IPV6, i + 10);
      netlink_add_addr6(&nlmsg, sock, devices[i].name, addr);
    }
    uint64_t macaddr = DEV_MAC + ((i + 10ull) << 40);
    netlink_device_change(&nlmsg, sock, devices[i].name, true, 0, &macaddr,
                          devices[i].macsize, NULL);
  }
  close(sock);
}
static void initialize_netdevices_init(void)
{
  int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sock == -1)
    exit(1);
  struct {
    const char* type;
    int macsize;
    bool noipv6;
    bool noup;
  } devtypes[] = {
      {"nr", 7, true}, {"rose", 5, true, true},
  };
  unsigned i;
  for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]); i++) {
    char dev[32], addr[32];
    sprintf(dev, "%s%d", devtypes[i].type, (int)procid);
    sprintf(addr, "172.30.%d.%d", i, (int)procid + 1);
    netlink_add_addr4(&nlmsg, sock, dev, addr);
    if (!devtypes[i].noipv6) {
      sprintf(addr, "fe88::%02x:%02x", i, (int)procid + 1);
      netlink_add_addr6(&nlmsg, sock, dev, addr);
    }
    int macsize = devtypes[i].macsize;
    uint64_t macaddr = 0xbbbbbb +
                       ((unsigned long long)i << (8 * (macsize - 2))) +
                       (procid << (8 * (macsize - 1)));
    netlink_device_change(&nlmsg, sock, dev, !devtypes[i].noup, 0, &macaddr,
                          macsize, NULL);
  }
  close(sock);
}

#define MAX_FDS 30

static void setup_common()
{
  if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
  }
}

static void loop();

static void sandbox_common()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setsid();
  struct rlimit rlim;
  rlim.rlim_cur = rlim.rlim_max = (200 << 20);
  setrlimit(RLIMIT_AS, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 32 << 20;
  setrlimit(RLIMIT_MEMLOCK, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 136 << 20;
  setrlimit(RLIMIT_FSIZE, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 1 << 20;
  setrlimit(RLIMIT_STACK, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 0;
  setrlimit(RLIMIT_CORE, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 256;
  setrlimit(RLIMIT_NOFILE, &rlim);
  if (unshare(CLONE_NEWNS)) {
  }
  if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
  }
  if (unshare(CLONE_NEWIPC)) {
  }
  if (unshare(0x02000000)) {
  }
  if (unshare(CLONE_NEWUTS)) {
  }
  if (unshare(CLONE_SYSVSEM)) {
  }
  typedef struct {
    const char* name;
    const char* value;
  } sysctl_t;
  static const sysctl_t sysctls[] = {
      {"/proc/sys/kernel/shmmax", "16777216"},
      {"/proc/sys/kernel/shmall", "536870912"},
      {"/proc/sys/kernel/shmmni", "1024"},
      {"/proc/sys/kernel/msgmax", "8192"},
      {"/proc/sys/kernel/msgmni", "1024"},
      {"/proc/sys/kernel/msgmnb", "1024"},
      {"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
  };
  unsigned i;
  for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++)
    write_file(sysctls[i].name, sysctls[i].value);
}

static int wait_for_loop(int pid)
{
  if (pid < 0)
    exit(1);
  int status = 0;
  while (waitpid(-1, &status, __WALL) != pid) {
  }
  return WEXITSTATUS(status);
}

static void drop_caps(void)
{
  struct __user_cap_header_struct cap_hdr = {};
  struct __user_cap_data_struct cap_data[2] = {};
  cap_hdr.version = _LINUX_CAPABILITY_VERSION_3;
  cap_hdr.pid = getpid();
  if (syscall(SYS_capget, &cap_hdr, &cap_data))
    exit(1);
  const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE);
  cap_data[0].effective &= ~drop;
  cap_data[0].permitted &= ~drop;
  cap_data[0].inheritable &= ~drop;
  if (syscall(SYS_capset, &cap_hdr, &cap_data))
    exit(1);
}

static int do_sandbox_none(void)
{
  if (unshare(CLONE_NEWPID)) {
  }
  int pid = fork();
  if (pid != 0)
    return wait_for_loop(pid);
  setup_common();
  sandbox_common();
  drop_caps();
  initialize_netdevices_init();
  if (unshare(CLONE_NEWNET)) {
  }
  initialize_tun();
  initialize_netdevices();
  initialize_wifi_devices();
  loop();
  exit(1);
}

static void close_fds()
{
  for (int fd = 3; fd < MAX_FDS; fd++)
    close(fd);
}

uint64_t r[1] = {0xffffffffffffffff};

void loop(void)
{
  intptr_t res = 0;
  res = syscall(__NR_socket, 0x10ul, 3ul, 9);
  if (res != -1)
    r[0] = res;
  *(uint64_t*)0x20001400 = 0;
  *(uint32_t*)0x20001408 = 0;
  *(uint64_t*)0x20001410 = 0x200013c0;
  *(uint64_t*)0x200013c0 = 0x20000380;
  *(uint32_t*)0x20000380 = 0xec4;
  *(uint16_t*)0x20000384 = 0x453;
  *(uint16_t*)0x20000386 = 0;
  *(uint32_t*)0x20000388 = 0;
  *(uint32_t*)0x2000038c = 0;
  memcpy(
      (void*)0x20000390,
      "\xd0\xb9\x77\xef\x71\xf5\x0f\xfb\x81\x01\xa8\x58\x88\xa3\xa7\x10\x2a\xfa"
      "\xcc\x46\x13\xc4\x08\x87\x4f\xc9\x18\xc4\x6c\x6a\xa7\x05\xd4\x5c\x32\x7c"
      "\xd9\x32\x45\xde\xbf\x81\x17\xea\xb6\x60\xb9\xb0\x86\xed\x89\xd4\xe0\x9b"
      "\xa6\xe7\x45\x7d\x78\x40\xdf\xea\xb7\x4d\x6a\x18\x98\x8a\x40\x70\x7f\xb7"
      "\xd4\xf3\x34\x91\xc9\xfd\x4e\xac\xc5\xb8\xe2\x26\x22\xb9\x78\x6e\x87\x8b"
      "\xb3\x64\x5f\x22\xbb\x16\x63\xaa\x73\x8c\x3f\x9e\x22\x1e\x4a\xdd\x38\x05"
      "\xe5\x9e\x9d\xbc\xf4\x58\x17\xb1\x93\x54\x0d\xaf\x33\xe0\xb7\x78\x9f\x7a"
      "\xee\x73\xe4\x28\x4a\x02\xce\x9d\x0e\x36\x96\x59\x9b\xda\xc6\x4d\x25\x75"
      "\x29\x95\x5e\x7e\x26\x2b\xbc\xb1\x96\xe2\xfd\x47\x9f\x14\xaa\xb4\x6d\x66"
      "\x2d\x28\x5b\x8a\x36\xf8\xcf\x67\xcb\xf5\x59\xa4\x98\xb0\x56\xe5\xe4\xa0"
      "\x83\x3c\x2c\xcb\xd1\xfd\xd7\x6d\x93\x4a\x05\xe8\xcb\xbb\x8b\x02\xc5\xd4"
      "\xe5\xc4\xb2\x73\xca\xad\xe8\x9b\x49\x09\xa9\x0f\xf5\xbc\xd7\xa0\x95\xbf"
      "\xe5\x16\xfd\x78\x82\xad\x60\x2d\x44\x84\x6b\xd2\x10\xe9\xcb\x47\x47\x6e"
      "\xef\xca\xe8\x90\xa8\x53\x0e\x11\xb5\x7c\xa4\x0c\xb7\x9d\xda\x0d\x15\x2d"
      "\x86\x9c\x2d\x93\xe5\x80\xb2\xd9\xf5\xfb\xfa\x82\xea\x09\x57\x2c\xa1\xe4"
      "\x7b\xd6\x8e\xd4\xc4\xf1\xa3\x1f\xef\x24\x6e\x46\xde\x48\xc4\x7d\x3a\x43"
      "\xa0\xe8\x5a\x76\x05\x55\x2f\xcb\xf3\xd9\x5b\x76\x40\xee\x34\x15\x7a\xc6"
      "\xe6\x33\xb8\x21\xcf\x86\xea\x36\x7e\x37\x68\x25\x05\x27\xe8\x34\xbe\x4b"
      "\x4e\x28\x72\x2e\x18\xd2\x81\xb4\x81\x35\x67\x28\x75\x55\xd6\x75\x08\xe6"
      "\x1d\x6b\x32\x38\xcb\x95\xe7\x97\x42\xda\x3c\x6d\xd4\x8b\xd0\x26\x33\xff"
      "\x9b\x37\xe8\x54\x65\xc6\x9c\x7e\x89\x97\xac\xdd\x30\xd0\x3c\xaf\x7f\xa5"
      "\xb4\xd9\xa2\x35\x96\x62\xd1\x28\x30\xf5\x9a\x1a\xd0\xaa\x04\x38\x9f\x62"
      "\x9b\x84\x76\x9f\x7a\x85\x1d\x39\xb5\x68\x73\xda\x66\x57\x54\x0e\x58\x47"
      "\x30\x88\x6a\x91\xb1\xe8\xfd\x4d\x75\xa5\x26\x52\x9b\xec\xfe\x95\xbb\xdf"
      "\x40\x8c\x90\x50\xdb\x0a\x15\xd9\x05\xf3\xd4\x48\x0c\x64\xe2\xd2\x74\x57"
      "\x1a\x0b\x7e\xae\xd6\x11\x3b\x09\x6b\xba\x73\xe8\x1a\x70\xfa\x43\x03\x5a"
      "\x0a\x81\x41\x2e\xa0\x64\x8e\xb0\x5e\xc2\xbb\xff\x55\x28\x20\xe7\x0b\xe0"
      "\x18\x9c\x65\x44\xfe\xc1\xbe\x2e\xf7\xe0\x60\x3f\xef\x42\x15\xe4\xc3\xfd"
      "\x32\x21\xce\x09\x45\x6e\x69\x4b\xcb\xfa\xa3\xd7\x94\x4e\x20\x8a\x11\x77"
      "\x94\xbc\x0f\x7a\x15\xf8\x2f\x13\x05\xba\x03\xdf\x99\xf5\xd7\x1a\xcc\x8f"
      "\x82\x41\x18\xfe\xa7\x6b\x16\xa1\x13\xc6\xc0\x60\x1c\x66\x86\x3a\xfb\xb6"
      "\x1f\xef\x74\x78\xe9\x0f\xfa\xd7\x80\x3a\x6c\xc9\xf2\xe1\xa6\x3e\xd1\x85"
      "\xd7\x79\xa1\xee\xa2\x69\xd1\x1c\xae\xe7\xdd\xc5\xcd\x24\x16\xa5\xf9\xb3"
      "\x30\x55\x77\x8b\xa8\x29\x53\x70\xec\x7c\xdf\xad\x16\x38\xe2\xec\x44\x47"
      "\x6a\x18\xe1\x9e\x6d\xdb\x90\x54\x39\x8f\xa7\x6d\xd5\x09\xb3\xe8\x4f\xd8"
      "\xad\x82\xc9\xb4\xfb\x54\xbf\x0c\xcc\x70\xda\xbc\x14\x85\x0f\x76\xaf\xfb"
      "\x31\x96\x90\x6a\x39\x5c\xe6\x1c\x4f\x5b\xfd\x10\x90\x40\x01\x16\x27\x7c"
      "\x79\x7e\x4d\x53\xd2\xe1\xb8\x12\x10\x5e\xa9\x34\x1f\x47\xce\x9a\xe8\x1d"
      "\x0c\xa7\xf4\xf5\x40\x4d\xb8\xd3\xa1\x76\xbb\xfd\x31\xd9\xb7\xfa\x0f\xb9"
      "\xac\x1e\x89\xb3\x18\xd0\xef\xfd\x29\x5f\xb6\x3e\xf8\xba\xe7\xaa\x21\xd7"
      "\x2a\x8c\x08\xae\x5f\x8c\x5d\x0a\xf2\xf2\x82\x0a\x34\x62\x5b\xff\x98\x44"
      "\x46\xd1\xf7\x19\xb8\xd8\x88\x3b\x3e\x54\xfe\x83\x37\xe5\x94\xb4\xa7\x47"
      "\x13\xc7\x92\xae\x7e\x62\x8c\x9d\xd5\x69\xe2\x64\x46\x8e\x67\x4a\xcd\x4b"
      "\xdd\x92\x65\xfd\x16\x28\xad\xdd\x1f\xc0\x30\xaf\x9a\x27\x13\xc0\x1a\xf2"
      "\x87\xe8\xe4\x28\x07\x14\x62\xe0\xb4\xbb\x16\xcb\x8f\xe8\xa4\x4b\x69\xa1"
      "\xc7\xb4\x2a\xcc\x4a\xc2\x0e\x9c\x27\xed\x42\xa9\x86\xa5\x76\x1a\xde\x09"
      "\xe7\x5c\xe4\x63\x74\xe0\x4a\xfa\xf8\xc6\x1e\xc7\xa8\x5b\x94\xda\x71\x4e"
      "\x92\xdf\x97\x8c\xf5\x45\xfa\x5a\x07\xac\x2a\xc3\x15\x63\x61\xfb\xb8\xfc"
      "\x26\xac\x8d\x26\x50\x72\x0e\x92\x32\xab\x9c\x8d\x61\x5b\x84\xa5\x0d\xa6"
      "\xdd\xe7\x66\x59\x3c\x50\xe4\x4b\x23\x55\xd1\x73\xd3\x48\x55\x2f\x7f\xb9"
      "\x3d\x7f\xa4\xa0\x5c\x25\x2a\x3b\x43\xf4\x22\x47\x36\xc1\xef\xcc\xa7\xde"
      "\xfc\x9a\x31\xaf\xcd\xf8\x90\x3e\x84\xcf\x51\x0e\xa6\x4b\x39\x03\x6e\x1a"
      "\xe8\xa8\x30\xfa\xb4\xb5\x7b\x47\x36\x8e\x87\x18\x45\xc6\xa0\x52\x7f\x9a"
      "\x2f\xd0\xb5\x4e\x81\x31\x63\x14\xd8\x88\xfa\xd5\x76\x56\x34\x19\x2e\x1c"
      "\xc1\xab\x38\x36\xb6\x35\x91\x49\xa0\x2b\xad\xa8\x05\x6b\x17\x9b\xf0\x7d"
      "\x0c\xf2\x00\x5b\x81\xa2\xc8\x30\x36\x61\x42\x91\x55\x1d\x8f\xab\x02\xd6"
      "\x85\xb6\x7e\xfb\x3a\x6b\xf4\x1e\x2f\xdc\x4a\x96\x9c\x2f\x0b\xa2\x77\x46"
      "\x9c\xa5\x94\x89\x6d\xb9\x01\x1a\x4e\x8a\x33\x90\x48\x23\xf0\xd6\xde\x43"
      "\x55\xe2\x25\xe3\x86\x3a\x46\x23\x8c\x90\xaf\x94\x17\x4a\xbb\x18\xd5\xe9"
      "\xd1\x56\xfc\xf2\xa3\x58\x10\x21\x85\x5c\x24\xb8\x15\x21\x4d\xd0\x16\x44"
      "\xb0\xf7\x52\x26\x76\xe6\xcc\xa0\xea\x2e\x84\x33\xce\x5b\x45\xeb\xea\x74"
      "\xdb\x9d\x37\xdf\x3c\x44\xa3\x63\x22\x5f\xdf\xbb\xc3\x50\xe9\x77\x65\xd9"
      "\x49\xda\x99\x5e\xd9\x3a\x63\x89\x94\x1b\xe4\x18\x23\xbf\x80\x24\xdd\x76"
      "\xf4\xe8\x0f\x2b\x66\x90\x05\xe9\x68\xe5\xac\xfe\x3b\x1f\xcf\x7a\x5a\x2b"
      "\x10\x23\x47\xf1\xae\x26\xba\x58\xb6\x6b\x86\x1f\x0c\xf0\x63\x47\xc1\x3f"
      "\x3e\x7a\x90\xbd\xba\x11\x6a\x4e\x06\xd1\x7a\x15\x79\xf9\xc1\xd8\xaf\x91"
      "\x35\x78\xfb\xaa\x81\x0e\x74\x9b\x1d\xa7\xe3\x7e\x83\x81\x63\x1e\xd7\xe1"
      "\x2c\xc2\x8a\xd5\x25\x10\xa8\xcb\xed\x5d\x33\x7b\xd5\xf2\x30\x27\x65\x0d"
      "\x2d\x0f\xee\x68\x60\x5d\xbc\x57\x3a\x17\xfd\x28\x60\xa8\x57\x40\x0a\x2f"
      "\x6e\xed\x29\x8e\x1b\x13\xf2\xca\xbb\xa0\xbf\x50\x26\xde\x3f\x95\xd6\x50"
      "\xf8\xd6\x9a\x5c\x4d\xfd\x6b\x12\xe2\xc9\x46\x5d\xde\xbc\x10\xa2\x71\x61"
      "\x8d\xba\xe9\x95\x15\x27\x2f\x5a\xb2\xc3\xb8\x17\xf9\x56\x3d\x91\x24\x4b"
      "\xf0\x2f\x1a\x3e\x1b\x4f\x86\x9e\x93\xb9\x14\x5d\x44\xe0\xae\xd2\xd7\x09"
      "\xa6\xcd\x40\x7e\x42\xdc\x21\x7f\xf4\x2d\xce\xad\x67\xda\x20\x1f\xf9\x1a"
      "\xa4\x32\xf3\xc3\x48\x7e\xfd\xd8\xbb\x6c\x21\x1e\xa1\x6a\x8a\x0d\x3b\x05"
      "\xa8\x9f\x80\xef\x4c\x51\xaa\xae\xcc\x61\x54\x91\x76\x98\xfb\x61\x09\xc4"
      "\x4e\x77\x12\xb0\x07\x1a\x43\x83\xd3\x0b\xae\xef\xab\xbc\x4e\x28\x4e\x00"
      "\xa0\x13\xb6\x69\x9b\xca\x0e\x74\x61\x77\x4a\xaf\x0d\x6b\x01\x07\xe7\x87"
      "\x39\x3f\x57\xa9\xa3\x26\xf6\xf6\x79\xaa\x35\xd1\x87\x74\xb2\x65\xeb\x1a"
      "\x3c\x68\xc8\x38\x10\x8a\x9a\x34\xcc\x30\x72\x95\x74\x6e\x7c\xf0\x54\xd7"
      "\x28\x0b\x3a\x37\x40\xaf\x85\x63\x6c\xfe\x29\x77\x1f\x0c\xf4\x10\x86\xb6"
      "\x1d\xaa\xec\x3c\x25\x25\x6e\x97\x71\xd6\x96\xd5\xa3\x0a\x2a\x10\xa8\xb4"
      "\x90\xe5\x74\x5e\x6d\x26\x63\x86\x6b\x2d\x70\xfa\x03\x11\x63\x3b\x4c\x6c"
      "\x77\x49\x59\x51\x65\xc0\xdc\x34\xa5\x12\x86\xb9\xd0\x3a\xa8\x05\x48\xb4"
      "\x65\x34\x6b\xb2\xe1\x4c\xd5\x52\xd4\xe2\x96\x62\x7e\xbb\xea\x58\x8e\x0d"
      "\x8e\x66\x8e\x7d\xa5\xeb\x6a\xf4\x49\xa5\x15\x51\xe4\x2e\x06\x0f\xb6\xc4"
      "\x58\xec\xa9\xc4\x48\x71\xad\x5a\xee\xd0\xac\x32\x5d\xe8\xe8\xaf\xa5\xbf"
      "\xc2\x88\x60\xc9\xb7\x14\x13\x92\xa5\xbb\xb8\xc1\xd5\xd6\x09\x7d\x17\x65"
      "\x08\x68\x52\x47\xf8\xee\x27\x47\x48\x38\x1b\xe5\xaa\xdb\xa0\x67\x35\xa2"
      "\xc2\xc1\x4a\xdb\x94\x36\x55\x9e\x9c\x8c\x84\xbd\x43\xa6\xd4\x1f\x65\xc5"
      "\x78\x30\x23\xd8\x7b\xca\x85\xe8\x80\xb6\x51\x43\xa7\xf0\x36\x0a\x21\x69"
      "\x1a\x0b\xde\x9c\x01\x46\x41\x4f\x3d\x34\x5a\xb1\x4e\x04\xd2\xd1\x9f\xd5"
      "\x62\x94\x23\x2c\xa4\x66\xfd\xb2\x15\xc9\x8d\x37\x60\x1b\x05\x09\x5e\xa4"
      "\x60\xf7\xa9\xbe\x9c\x7b\x61\xb8\x48\x44\xc7\x90\xef\xe6\xc8\xda\x9a\x29"
      "\x4c\xb7\xba\x26\x96\x4d\x6b\xdf\xe5\xdf\x20\xb7\xa5\x4d\x32\xab\x4d\xb9"
      "\x35\xf8\x3b\x4a\x6b\x19\xf2\x04\x00\x6e\xdb\xd2\xd7\x73\x52\xb9\x6a\xce"
      "\x4b\xea\x40\xb2\xc5\xd0\x55\x48\xe4\xee\xbb\x4e\x4c\x8b\xbe\xb3\x35\x91"
      "\xa1\xad\x9e\xf8\xcb\x21\x5c\xa8\x46\x32\x00\xc5\x59\x81\x1d\x4b\x02\x11"
      "\x62\x06\xc5\x2d\xb5\xdf\x71\x71\xb1\x3f\x7c\x66\x51\xc5\x34\x60\xf8\x62"
      "\x16\xb3\x02\x6b\x05\xe5\xc7\x5a\x45\x97\x2a\x84\x74\xcc\xdb\x79\x65\x6c"
      "\x66\x03\xa8\xab\x0e\x72\xfb\x28\x6d\xcd\x12\x21\x87\x41\x13\x6e\x03\x2b"
      "\x1f\x5f\x90\x45\x49\xdc\x3f\x1d\x63\xde\x81\x53\xde\xda\x42\x4b\x51\x49"
      "\x0c\x61\x16\xfc\x9f\x5d\xf1\x18\xbc\x6b\xd2\xaf\xeb\x27\xae\xff\x65\x24"
      "\x90\x0a\x2f\xc3\x08\x11\x97\x1f\xcc\x66\xdd\x1e\x89\x13\xbc\x44\x37\xed"
      "\x65\xa1\xb4\xa5\x8b\x91\x38\x72\xf2\x65\x41\x0d\xee\x11\x77\x2c\x3b\xea"
      "\x47\x95\x1f\x2b\xf3\xdc\xef\x75\x21\xc3\x7a\xa5\x01\x41\x72\xc1\x2f\x52"
      "\x59\xa8\x07\xad\xc4\xf3\xfa\x46\x26\xef\x22\x14\x3e\x00\x7f\x8f\x82\x15"
      "\xad\xc1\x55\x57\xc1\xf2\xda\x0b\x40\x1e\xd2\x0e\xe3\x32\x69\x54\xdf\x4e"
      "\x88\x5b\x7c\x1b\x64\xcb\xdc\x3d\xe5\xec\xc6\x83\x49\x70\x2f\x6f\xd5\x14"
      "\x2b\x44\xd8\x18\x19\x0e\xb7\x0d\xe5\x67\x40\xee\xa4\x6e\x2e\xb8\xfe\x39"
      "\xc6\x91\x3c\x08\x03\xea\x3a\x16\xd6\x41\x2d\x4e\x6e\x7b\xe6\x8e\xdc\xed"
      "\x83\x79\x43\xad\x61\x8a\x61\xd3\x3c\x56\x50\xcc\x76\xf5\x8e\xa4\x34\x61"
      "\xd9\x2c\x87\x1a\x58\xd3\xc9\x52\x95\x0f\x0e\x31\x4c\x3c\x2d\x21\xb9\x2e"
      "\xd5\x9c\x46\x2f\x36\x37\xc3\x29\x4d\xed\x61\x05\x17\xa4\x72\x8c\xfc\x75"
      "\x10\x7e\xc5\x83\xb1\x2c\xe2\x25\xf2\xbc\x6b\xcc\x4d\x48\x2e\x60\x08\x00"
      "\x9f\xd4\x03\xd8\xd9\x3f\x81\xb1\x7c\x25\x39\x15\x81\xa3\x07\xfb\x9f\x81"
      "\x95\x66\xb2\x8b\x8c\x00\x40\x4d\xed\xa3\x59\xaf\x4a\xf8\x9d\x37\x2b\xa7"
      "\x0b\x2d\x7d\xb2\x74\x9d\xb6\x70\x98\x9a\x1d\x05\x1f\x37\xe4\x56\xd9\x90"
      "\x4b\x3a\x77\x65\xec\xc6\x26\x87\xcd\x82\x77\x5a\x73\x6b\x85\x18\x90\x61"
      "\x2f\xa2\xe2\xaf\x47\xc9\xf6\x6f\xc0\x3e\x81\xa9\x29\x99\xf9\x97\xd6\xc0"
      "\x9d\xe9\x44\x35\x65\xcc\x5e\x50\xc5\xd8\x24\xc7\x82\x3c\x2d\x62\x0a\x2a"
      "\xd6\x43\xe4\xff\x63\xc0\x6e\x99\xca\x03\xc3\x3e\x55\x2a\x7d\x90\x48\x78"
      "\x7d\xa9\x5c\xf5\xf9\x70\x3d\x6c\xce\x7a\x73\x76\xc0\x94\xd0\x2e\x0d\xfd"
      "\x8a\x34\xec\x24\xe8\x01\xd8\xa7\x71\xb1\x5d\x4a\xb1\x3e\xb3\x6d\x48\xaa"
      "\x3a\x5b\xfd\xab\xfb\x29\x5b\xc0\xff\xee\x17\x06\xd0\x5a\xf9\x83\x0b\x30"
      "\xfe\x3f\x89\x03\xc6\x96\xba\x51\xee\x0d\xe7\x76\x17\x90\xe2\x92\xa8\x8a"
      "\xf4\xda\x9f\x99\x42\xd1\xe4\xc1\xa6\x67\xdd\x9b\xfb\xb0\xc4\x20\xb3\x2a"
      "\x20\x96\x1a\x22\x70\x4e\xaa\x47\xba\x92\x2d\x30\x38\x5b\xe9\x07\x63\xc4"
      "\x6f\x25\x0d\xbb\x7c\x66\xe6\xdc\x85\xf5\xd5\x9b\xa6\x97\xfe\x52\xee\xdd"
      "\xa6\xef\x79\x10\xed\xa1\x55\x89\xa0\x35\xdf\x5d\x8b\xb6\xb6\x8e\xfa\xf4"
      "\x30\x5e\x49\x84\xc2\xbf\x27\x26\xca\x3f\x65\xb2\xb6\xda\xc3\x1f\x9b\xd2"
      "\x1d\xfc\x2a\xdb\x99\xb2\x50\xe5\x82\x5e\x98\xf0\xb5\xbc\xb5\x0f\x26\x52"
      "\x16\x9c\x18\xbc\x6d\xf9\x18\xc5\x62\xea\xd1\x91\xfc\x33\xb8\x22\xa3\xea"
      "\x3c\xa0\xeb\x91\xd2\xa1\xe9\x60\x92\x01\x01\x33\xef\xde\x5d\x1b\x17\x93"
      "\x8a\x8c\x8a\x9c\x70\xf4\xd0\xce\x0a\x0a\xde\xc3\x6a\xa1\x08\xb9\xdf\x43"
      "\x74\x16\xde\x00\xc7\xc5\x89\x43\xa6\xa0\x9c\x16\x21\x30\x29\x99\x38\xcd"
      "\xc0\xfc\x43\x92\xaf\xb6\xaf\x4e\xe7\x1d\x99\x2f\x28\x53\x48\x84\xed\x85"
      "\xa2\xe0\x98\x49\x10\x3a\x14\x83\x84\xf9\xb0\x06\x68\xd9\x18\x74\x39\x5b"
      "\xbf\x45\x70\x7c\x9a\x2e\x32\xd6\x12\x50\xd3\xac\x9c\xa5\xdf\xd0\x6a\xa6"
      "\x49\x38\x1f\x5b\x1e\x77\x50\xfc\x6b\xce\x82\x2e\x40\x71\xaf\xd7\x08\xbd"
      "\x77\x06\x76\x25\x2f\xab\x18\x04\x97\x3a\xe1\x70\x22\x95\x37\x6c\xe8\xa4"
      "\x85\x82\x96\x35\xf8\x72\xa7\x26\x03\x4d\x15\x86\xb6\x66\xf7\x78\xcc\x28"
      "\xeb\x90\x68\x05\x82\xe1\xa5\xb9\xbe\xd9\xb7\x8f\xa7\x58\x75\x10\x73\x02"
      "\x15\x1a\xd1\x54\x8c\x4d\xaa\x21\xd5\x15\x94\x83\x2f\x9b\x2c\xe3\x9f\x01"
      "\x3c\x29\xf4\x7c\x30\xa7\x5e\x5b\x1c\x3c\x17\x7d\x22\x63\xd6\xce\x63\x17"
      "\x18\x5c\xc8\x6d\xb5\x9a\x51\x39\xb4\xb5\xca\xfe\xa1\x3e\xdf\x3e\xf4\x1b"
      "\xf3\x65\x25\xb3\x0b\x39\x0d\xe6\xef\x7e\x07\x3b\x8d\x6e\x8c\xfb\xeb\xec"
      "\x47\xe9\x7c\x63\x8a\x7a\xc2\x84\x9d\x8b\x52\x5e\x5f\xe1\x0f\xc2\xb6\x6a"
      "\x4f\x86\x10\x2e\x5d\x56\x5d\x20\xe9\x18\xc0\x9b\x02\xa1\x07\xf2\xb7\x37"
      "\x5a\x98\x44\x07\xb1\xc4\xb2\xf1\x98\xd8\xca\xaa\x8b\x73\x2a\x1c\x2c\x95"
      "\xba\x20\x91\x62\xb9\x38\xb0\x12\xbe\x26\x7a\xd1\x48\xa8\x44\x9e\xe2\x9d"
      "\x36\xef\x3d\x59\x96\x60\x98\xcf\xee\x94\xa4\xc1\xae\xe5\x6a\x9f\xb9\x34"
      "\x4b\x13\x0d\x6f\x62\x0f\xa4\x83\xa6\x99\x9b\x15\x5b\xe0\xa8\x4a\x88\x5f"
      "\x3d\x30\xcb\x7f\x84\xf1\x25\xc3\xc7\x2c\x6e\xe9\x11\x71\x96\xe0\xdb\x45"
      "\x9c\xc8\xf7\x0f\x10\x43\x22\xa1\xb4\x7e\x5f\x76\xf9\x58\x72\x25\x70\x12"
      "\x98\x77\x57\x41\x09\x3a\x63\x84\xd5\x95\x94\xdf\xb2\x5e\x1a\x7f\x8b\x31"
      "\xdc\x25\x10\x53\xfd\x09\xab\xb9\x07\x77\x54\x11\x4b\xa0\xbb\xa3\x8b\xdf"
      "\x79\xa5\xd0\x5c\x42\xc6\x50\x33\x1f\x59\x27\x07\x83\xf1\xea\xa5\x03\x70"
      "\x3a\xcf\xc3\x14\x09\xe9\x5b\x5c\xa8\xa3\xa4\xe0\x45\x92\xc3\xb9\x9f\xf2"
      "\x79\x5a\x53\x2a\x36\xdc\xf2\x48\x37\x41\xee\xc4\xc6\x0b\x57\xc2\xfc\x18"
      "\x9f\x47\xd9\xbe\xc4\xa6\x08\xb0\x4a\x15\x5a\x68\xe0\xea\x94\xbc\x60\x4f"
      "\x8c\x9f\x63\xae\xf7\x52\xe3\x43\x94\xf7\xed\x52\x43\x76\x61\x9a\x29\x09"
      "\x62\xd2\xc8\xcc\xdf\x51\xfe\x30\x11\xc0\xd6\x8c\x68\xf8\x4d\xd9\xf2\xfc"
      "\x63\x0c\xf3\xef\xbb\xef\x8b\xe8\x0d\x14\xae\xe4\x0d\xf1\xf9\x4c\x26\xca"
      "\xb2\xde\x5d\x24\xd8\xa9\xba\x68\x9e\x75\x11\x34\xab\xe7\xa6\xb5\x4f\xc4"
      "\xcf\x7d\x7b\x3c\x7e\x2a\x52\x6e\xf4\x66\x22\xe4\x79\x90\x6e\x61\x65\xb1"
      "\x97\xfb\x63\x98\xd9\x0f\xea\x93\x94\x51\xf2\x91\x7f\xe0\x62\x99\x76\x3c"
      "\x41\xa2\x95\xdd\x15\x0e\xa2\x51\xee\x02\x9f\xbd\x2c\x26\xa1\xe5\x7d\x18"
      "\x3f\x59\x2d\x48\x3a\xa9\x43\xee\x92\x2e\x7d\xa7\x6f\xe2\xff\xef\x72\x5d"
      "\xa6\x64\x88\x43\x69\x93\x26\x32\x74\x4c\x3a\xed\x4b\x20\x73\x3c\xeb\x21"
      "\xdc\x1f\x60\x80\x95\xf1\x71\x32\x8e\xab\x09\xc3\x1b\x36\xbf\x4a\xa8\x2b"
      "\xb9\x9f\xb4\x61\x1e\xc1\x59\x65\xe3\xc4\x2d\x41\xd3\x14\x57\xbc\x1c\x55"
      "\xd1\xbd\xb6\x8c\xf6\xa4\x09\xde\x6b\x30\x04\x8a\x00\x70\x3f\xa3\x4c\x09"
      "\x1c\x54\x6d\xa6\x9e\x45\x43\x49\x5a\x0a\x34\xb0\x61\x1c\xe6\xbc\xb7\xf2"
      "\x64\xb7\xf6\x11\x95\x34\x9b\x59\x72\x29\xb4\x1f\xe6\x73\x46\x5c\x41\x50"
      "\x34\xa3\x3e\xa3\x95\xef\x44\x7b\x0c\xff\x28\x17\x3d\x45\xf9\x49\xf0\x17"
      "\x9d\x06\x09\x4b\x79\x1a\x3d\x9f\xab\x59\x54\x07\xf9\x02\x6d\x15\xcb\xee"
      "\xe8\xd3\x2a\x37\xe7\x5d\x50\x0f\xed\x2b\x13\xb1\xc2\x13\xb5\x5d\x74\x7e"
      "\x81\x51\x58\x80\x48\x0a\x6c\xdd\x52\x61\xb6\x70\x2b\xc7\x41\xd5\x5e\xaf"
      "\xe6\x77\x04\x67\x94\x28\x23\x79\xe8\x99\xa6\x93\x90\x05\x99\xd0\x9a\xb1"
      "\xee\x5c\xaa\xbf\xfb\x83\x78\x0b\x98\xdf\x69\xdf\x52\x6a\xce\xf5\xf4\xf8"
      "\xdc\x27\x35\x35\x86\xb5\xf7\x39\xf0\xfa\x56\x4b\xbb\x41\x18\x8f\x0f\xbf"
      "\xf0\xd4\x5c\x2b\xba\x55\x79\x18\xb0\xaa\x44\xa5\x39\xf9\x55\x71\x2d\x53"
      "\xa9\x2f\x0a\xd7\x32\xf8\xf2\xc6\x1d\x6e\x0b\xbf\xc3\x38\xc7\x00\x44\xb8"
      "\x27\x22\x70\x64\x8b\x03\x14\xb1\x3c\x6a\xd1\x56\xe9\x71\xdc\x89\x79\x09"
      "\x9d\x84\xf1\xca\xb3\x05\x41\x43\xd6\xdb\xd2\x47\xe0\x54\xd7\x29\xa6\x22"
      "\x5a\x4c\xae\x5f\x39\x4f\x76\x79\xa2\x02\xaf\x6b\x23\x6b\x71\x9f\x94\xdb"
      "\xae\x42\x19\x99\xe9\x25\x75\x07\x15\xa0\xbe\x47\x75\xa1\x53\xf7\x7f\x50"
      "\xe7\x6f\xaa\xd1\xd3\xcd\x1d\x97\x9e\xb5\x6f\xca\x1c\xcd\x27\xb9\x5d\xf0"
      "\xf6\x1b\x77\x3a\x4d\x8b\xf0\x48\x93\xb6\xd8\xfd\x5b\x91\x44\xf7\xc7\x44"
      "\x66\xed\x09\x3c\x1b\x93\xb5\xe1\x8f\x00\x1e\x98\xde\xa2\xbc\x0e\xa2\x3b"
      "\x10\x41\x4b\xbd\x5f\x30\x53\xfd\x0b\x28\x44\xaf\xc9\x8f\xd7\xbd\x17\x76"
      "\xfe\x26\x4a\x91\x77\xcf\x18\x2d\xef\x14\xc8\x8c\x1e\x8d\x0c\x15\x3e\xec"
      "\x39\x67\xc7\xb8\x92\x46\x44\x0f\x16\xc4\x9f\x4e\x7a\x56\xb8\x05\x85\x05"
      "\x7c\x67\x5f\x31\x8c\x5e\xd0\x75\x13\xb7\x36\x49\xe9\x33\x32\xf3\xb4\x91"
      "\x23\x7e\xf5\x83\x53\x98\x11\x3f\xfa\xda\xa6\xc2\xe8\xcc\xb1\x1b\x37\xad"
      "\x47\xf3\x29\xed\xe8\x0c\xc1\xef\xae\x45\x13\xe3\xf7\xba\x57\xaf\x31\xd3"
      "\x7f\x9a\xa7\xfd\xe8\x0b\x0f\xc8\x47\x73\x64\xd6\x74\xf7\x83\x97\x1e\x2d"
      "\x1b\xe8\xbb\x48\xa5\x1c\x0b\xdd\xdf\x99\x7b\x6c\x33\xda\x3c\x3f\x75\x45"
      "\x2a\x97\x27\x1b\x88\x92\x49\x05\x94\x83\x13\xa1\x1c\x69\x52\x4d\x0a\xb6"
      "\x98\x64\x9d\x32\xd5\xe3\x5c\x33\xe6\xa3\xdd\x4d\x18\x13\xe9\x75\x32\xec"
      "\x38\x49\x74\x48\x4e\x4d\x1c\x65\x75\x17\x5f\x20\x1a\xef\xc5\x51\xb3\xac"
      "\x48\xd8\xca\x40\x70\x96\x40\x59\x07\x75\xf7\x87\xfe\x65\xac\xfd\xe4\xbc"
      "\x8b\xa6\x82\x5b\xbb\xe5\xbe\x1e\xed\x90\xd4\x8a\xd7\x4b\xea\x7d\x4d\xbd"
      "\xab\xcf\x23\x84\xc6\x80\xfe\x68\xc2\xb0\x92\x3b\xe1\x92\x9d\x0f\xf2\x63"
      "\xb9\x20\xa9\x49\xb1\x37\x7d\xee\xa7\x0c\x9d\xac\x55\xf0\x55\x95\x2e\xb2"
      "\xb2\xac\x1a\x7c\x71\x4f\x47\x5f\xa1\x92\xa0\x57\x32\x61\x03\x37\x35\x75"
      "\x03\x5a\xc0\x59\x31\x7f\x42\xd9\xeb\xff\xe9\xa7\x06\x2d\x44\x14\x9e\xa1"
      "\x4a\xda\x45\xab\x91\x7b\xd8\x79\xba\x8b\x6b\x57\x15\xd4\xf2\xb8\xf9",
      3761);
  *(uint64_t*)0x200013c8 = 0xec4;
  *(uint64_t*)0x20001418 = 1;
  *(uint64_t*)0x20001420 = 0;
  *(uint64_t*)0x20001428 = 0;
  *(uint32_t*)0x20001430 = 0;
  syscall(__NR_sendmsg, r[0], 0x20001400ul, 0ul);
  close_fds();
}
int main(void)
{
  syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  do_sandbox_none();
  return 0;
}