// WARNING in __ip6_append_data: sendfile() of datagrams larger than the MTU into a UDPv6 socket whose traffic an // xfrm tunnel state carries. MODE is XFRM_MODE_TUNNEL (1) or IP-TFS (5). #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef MODE #define MODE 5 #endif #define ATTR_SA_DIR 33 #define SA_DIR_OUT 2 static int fd; static void put_attr(char *buf, size_t *len, unsigned short type, const void *data, size_t dlen) { struct nlattr *a = (struct nlattr *)(buf + *len); a->nla_type = type; a->nla_len = NLA_HDRLEN + dlen; memcpy((char *)a + NLA_HDRLEN, data, dlen); *len += NLA_ALIGN(a->nla_len); } static int req(unsigned short type, const void *body, size_t blen, const char *attrs, size_t alen) { char msg[4096] = {0}, rb[4096]; struct nlmsghdr *nh = (struct nlmsghdr *)msg; memcpy(NLMSG_DATA(nh), body, blen); memcpy((char *)NLMSG_DATA(nh) + NLMSG_ALIGN(blen), attrs, alen); nh->nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(blen) + alen); nh->nlmsg_type = type; nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; if (send(fd, msg, nh->nlmsg_len, 0) < 0 || recv(fd, rb, sizeof(rb), 0) < 0) return -1; return ((struct nlmsgerr *)NLMSG_DATA((struct nlmsghdr *)rb))->error; } static void write_file(const char *path, const char *s) { int f = open(path, O_WRONLY); if (f < 0 || write(f, s, strlen(s)) < 0) exit(1); close(f); } int main(void) { struct { struct xfrm_algo a; char key[16]; } crypt = {0}; struct { struct xfrm_algo_auth a; char key[32]; } auth = {0}; struct xfrm_userpolicy_info pol = {0}; struct xfrm_usersa_info sa = {0}; struct xfrm_user_tmpl tmpl = {0}; struct sockaddr_in6 dst = {0}; struct ifreq ifr = {0}; static char buf[8192]; unsigned char dir = SA_DIR_OUT; char attrs[512], map[32]; size_t al = 0; int s, u, f, len, bad = 0; snprintf(map, sizeof(map), "0 %d 1", getuid()); if (unshare(CLONE_NEWUSER | CLONE_NEWNET)) return 1; write_file("/proc/self/setgroups", "deny"); write_file("/proc/self/uid_map", map); snprintf(map, sizeof(map), "0 %d 1", getgid()); write_file("/proc/self/gid_map", map); s = socket(AF_INET, SOCK_DGRAM, 0); strcpy(ifr.ifr_name, "lo"); ifr.ifr_mtu = 1280; ioctl(s, SIOCSIFMTU, &ifr); ifr.ifr_flags = IFF_UP; ioctl(s, SIOCSIFFLAGS, &ifr); fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM); /* Outbound ESP tunnel state ::1 -> ::1. */ inet_pton(AF_INET6, "::1", &sa.id.daddr); inet_pton(AF_INET6, "::1", &sa.saddr); sa.id.spi = htonl(0x100); sa.id.proto = IPPROTO_ESP; sa.family = AF_INET6; sa.mode = MODE; sa.reqid = 1; sa.lft.soft_byte_limit = sa.lft.hard_byte_limit = XFRM_INF; sa.lft.soft_packet_limit = sa.lft.hard_packet_limit = XFRM_INF; strcpy(crypt.a.alg_name, "cbc(aes)"); crypt.a.alg_key_len = 128; strcpy(auth.a.alg_name, "hmac(sha256)"); auth.a.alg_key_len = 256; auth.a.alg_trunc_len = 128; put_attr(attrs, &al, XFRMA_ALG_CRYPT, &crypt, sizeof(crypt)); put_attr(attrs, &al, XFRMA_ALG_AUTH_TRUNC, &auth, sizeof(auth)); put_attr(attrs, &al, ATTR_SA_DIR, &dir, 1); printf("newsa: %d\n", req(XFRM_MSG_NEWSA, &sa, sizeof(sa), attrs, al)); /* Outbound policy for UDP to ::1 port 9 through that state. */ pol.sel.family = AF_INET6; inet_pton(AF_INET6, "::1", &pol.sel.daddr); pol.sel.prefixlen_d = 128; pol.sel.proto = IPPROTO_UDP; pol.sel.dport = htons(9); pol.sel.dport_mask = 0xffff; pol.dir = XFRM_POLICY_OUT; pol.action = XFRM_POLICY_ALLOW; pol.lft = sa.lft; tmpl.id = sa.id; tmpl.family = AF_INET6; tmpl.saddr = sa.saddr; tmpl.reqid = 1; tmpl.mode = MODE; tmpl.aalgos = tmpl.ealgos = tmpl.calgos = ~0; al = 0; put_attr(attrs, &al, XFRMA_TMPL, &tmpl, sizeof(tmpl)); printf("newpolicy: %d\n", req(XFRM_MSG_NEWPOLICY, &pol, sizeof(pol), attrs, al)); u = socket(AF_INET6, SOCK_DGRAM, 0); dst.sin6_family = AF_INET6; dst.sin6_port = htons(9); inet_pton(AF_INET6, "::1", &dst.sin6_addr); connect(u, (struct sockaddr *)&dst, sizeof(dst)); f = memfd_create("data", 0); memset(buf, 0x33, sizeof(buf)); if (write(f, buf, sizeof(buf)) < 0) return 1; for (len = 1200; len <= 8000; len += 50) { lseek(f, 0, SEEK_SET); if (sendfile(u, f, NULL, len) < 0) bad++; } printf("done, %d sendfile errors\n", bad); return 0; }