Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #define _GNU_SOURCE
0004 
0005 #include <errno.h>
0006 #include <fcntl.h>
0007 #include <stdio.h>
0008 #include <stdlib.h>
0009 #include <string.h>
0010 #include <unistd.h>
0011 #include <net/if.h>
0012 #include <linux/if_tun.h>
0013 #include <linux/netlink.h>
0014 #include <linux/rtnetlink.h>
0015 #include <sys/ioctl.h>
0016 #include <sys/socket.h>
0017 #include <linux/virtio_net.h>
0018 #include <netinet/ip.h>
0019 #include <netinet/udp.h>
0020 #include "../kselftest_harness.h"
0021 
0022 static const char param_dev_tap_name[] = "xmacvtap0";
0023 static const char param_dev_dummy_name[] = "xdummy0";
0024 static unsigned char param_hwaddr_src[] = { 0x00, 0xfe, 0x98, 0x14, 0x22, 0x42 };
0025 static unsigned char param_hwaddr_dest[] = {
0026     0x00, 0xfe, 0x98, 0x94, 0xd2, 0x43
0027 };
0028 
0029 #define MAX_RTNL_PAYLOAD (2048)
0030 #define PKT_DATA 0xCB
0031 #define TEST_PACKET_SZ (sizeof(struct virtio_net_hdr) + ETH_HLEN + ETH_MAX_MTU)
0032 
0033 static struct rtattr *rtattr_add(struct nlmsghdr *nh, unsigned short type,
0034                  unsigned short len)
0035 {
0036     struct rtattr *rta =
0037         (struct rtattr *)((uint8_t *)nh + RTA_ALIGN(nh->nlmsg_len));
0038     rta->rta_type = type;
0039     rta->rta_len = RTA_LENGTH(len);
0040     nh->nlmsg_len = RTA_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
0041     return rta;
0042 }
0043 
0044 static struct rtattr *rtattr_begin(struct nlmsghdr *nh, unsigned short type)
0045 {
0046     return rtattr_add(nh, type, 0);
0047 }
0048 
0049 static void rtattr_end(struct nlmsghdr *nh, struct rtattr *attr)
0050 {
0051     uint8_t *end = (uint8_t *)nh + nh->nlmsg_len;
0052 
0053     attr->rta_len = end - (uint8_t *)attr;
0054 }
0055 
0056 static struct rtattr *rtattr_add_str(struct nlmsghdr *nh, unsigned short type,
0057                      const char *s)
0058 {
0059     struct rtattr *rta = rtattr_add(nh, type, strlen(s));
0060 
0061     memcpy(RTA_DATA(rta), s, strlen(s));
0062     return rta;
0063 }
0064 
0065 static struct rtattr *rtattr_add_strsz(struct nlmsghdr *nh, unsigned short type,
0066                        const char *s)
0067 {
0068     struct rtattr *rta = rtattr_add(nh, type, strlen(s) + 1);
0069 
0070     strcpy(RTA_DATA(rta), s);
0071     return rta;
0072 }
0073 
0074 static struct rtattr *rtattr_add_any(struct nlmsghdr *nh, unsigned short type,
0075                      const void *arr, size_t len)
0076 {
0077     struct rtattr *rta = rtattr_add(nh, type, len);
0078 
0079     memcpy(RTA_DATA(rta), arr, len);
0080     return rta;
0081 }
0082 
0083 static int dev_create(const char *dev, const char *link_type,
0084               int (*fill_rtattr)(struct nlmsghdr *nh),
0085               int (*fill_info_data)(struct nlmsghdr *nh))
0086 {
0087     struct {
0088         struct nlmsghdr nh;
0089         struct ifinfomsg info;
0090         unsigned char data[MAX_RTNL_PAYLOAD];
0091     } req;
0092     struct rtattr *link_info, *info_data;
0093     int ret, rtnl;
0094 
0095     rtnl = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
0096     if (rtnl < 0) {
0097         fprintf(stderr, "%s: socket %s\n", __func__, strerror(errno));
0098         return 1;
0099     }
0100 
0101     memset(&req, 0, sizeof(req));
0102     req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.info));
0103     req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
0104     req.nh.nlmsg_type = RTM_NEWLINK;
0105 
0106     req.info.ifi_family = AF_UNSPEC;
0107     req.info.ifi_type = 1;
0108     req.info.ifi_index = 0;
0109     req.info.ifi_flags = IFF_BROADCAST | IFF_UP;
0110     req.info.ifi_change = 0xffffffff;
0111 
0112     rtattr_add_str(&req.nh, IFLA_IFNAME, dev);
0113 
0114     if (fill_rtattr) {
0115         ret = fill_rtattr(&req.nh);
0116         if (ret)
0117             return ret;
0118     }
0119 
0120     link_info = rtattr_begin(&req.nh, IFLA_LINKINFO);
0121 
0122     rtattr_add_strsz(&req.nh, IFLA_INFO_KIND, link_type);
0123 
0124     if (fill_info_data) {
0125         info_data = rtattr_begin(&req.nh, IFLA_INFO_DATA);
0126         ret = fill_info_data(&req.nh);
0127         if (ret)
0128             return ret;
0129         rtattr_end(&req.nh, info_data);
0130     }
0131 
0132     rtattr_end(&req.nh, link_info);
0133 
0134     ret = send(rtnl, &req, req.nh.nlmsg_len, 0);
0135     if (ret < 0)
0136         fprintf(stderr, "%s: send %s\n", __func__, strerror(errno));
0137     ret = (unsigned int)ret != req.nh.nlmsg_len;
0138 
0139     close(rtnl);
0140     return ret;
0141 }
0142 
0143 static int dev_delete(const char *dev)
0144 {
0145     struct {
0146         struct nlmsghdr nh;
0147         struct ifinfomsg info;
0148         unsigned char data[MAX_RTNL_PAYLOAD];
0149     } req;
0150     int ret, rtnl;
0151 
0152     rtnl = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
0153     if (rtnl < 0) {
0154         fprintf(stderr, "%s: socket %s\n", __func__, strerror(errno));
0155         return 1;
0156     }
0157 
0158     memset(&req, 0, sizeof(req));
0159     req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.info));
0160     req.nh.nlmsg_flags = NLM_F_REQUEST;
0161     req.nh.nlmsg_type = RTM_DELLINK;
0162 
0163     req.info.ifi_family = AF_UNSPEC;
0164 
0165     rtattr_add_str(&req.nh, IFLA_IFNAME, dev);
0166 
0167     ret = send(rtnl, &req, req.nh.nlmsg_len, 0);
0168     if (ret < 0)
0169         fprintf(stderr, "%s: send %s\n", __func__, strerror(errno));
0170 
0171     ret = (unsigned int)ret != req.nh.nlmsg_len;
0172 
0173     close(rtnl);
0174     return ret;
0175 }
0176 
0177 static int macvtap_fill_rtattr(struct nlmsghdr *nh)
0178 {
0179     int ifindex;
0180 
0181     ifindex = if_nametoindex(param_dev_dummy_name);
0182     if (ifindex == 0) {
0183         fprintf(stderr, "%s: ifindex  %s\n", __func__, strerror(errno));
0184         return -errno;
0185     }
0186 
0187     rtattr_add_any(nh, IFLA_LINK, &ifindex, sizeof(ifindex));
0188     rtattr_add_any(nh, IFLA_ADDRESS, param_hwaddr_src, ETH_ALEN);
0189 
0190     return 0;
0191 }
0192 
0193 static int opentap(const char *devname)
0194 {
0195     int ifindex;
0196     char buf[256];
0197     int fd;
0198     struct ifreq ifr;
0199 
0200     ifindex = if_nametoindex(devname);
0201     if (ifindex == 0) {
0202         fprintf(stderr, "%s: ifindex %s\n", __func__, strerror(errno));
0203         return -errno;
0204     }
0205 
0206     sprintf(buf, "/dev/tap%d", ifindex);
0207     fd = open(buf, O_RDWR | O_NONBLOCK);
0208     if (fd < 0) {
0209         fprintf(stderr, "%s: open %s\n", __func__, strerror(errno));
0210         return -errno;
0211     }
0212 
0213     memset(&ifr, 0, sizeof(ifr));
0214     strcpy(ifr.ifr_name, devname);
0215     ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR | IFF_MULTI_QUEUE;
0216     if (ioctl(fd, TUNSETIFF, &ifr, sizeof(ifr)) < 0)
0217         return -errno;
0218     return fd;
0219 }
0220 
0221 size_t build_eth(uint8_t *buf, uint16_t proto)
0222 {
0223     struct ethhdr *eth = (struct ethhdr *)buf;
0224 
0225     eth->h_proto = htons(proto);
0226     memcpy(eth->h_source, param_hwaddr_src, ETH_ALEN);
0227     memcpy(eth->h_dest, param_hwaddr_dest, ETH_ALEN);
0228 
0229     return ETH_HLEN;
0230 }
0231 
0232 static uint32_t add_csum(const uint8_t *buf, int len)
0233 {
0234     uint32_t sum = 0;
0235     uint16_t *sbuf = (uint16_t *)buf;
0236 
0237     while (len > 1) {
0238         sum += *sbuf++;
0239         len -= 2;
0240     }
0241 
0242     if (len)
0243         sum += *(uint8_t *)sbuf;
0244 
0245     return sum;
0246 }
0247 
0248 static uint16_t finish_ip_csum(uint32_t sum)
0249 {
0250     uint16_t lo = sum & 0xffff;
0251     uint16_t hi = sum >> 16;
0252 
0253     return ~(lo + hi);
0254 
0255 }
0256 
0257 static uint16_t build_ip_csum(const uint8_t *buf, int len,
0258                   uint32_t sum)
0259 {
0260     sum += add_csum(buf, len);
0261     return finish_ip_csum(sum);
0262 }
0263 
0264 static int build_ipv4_header(uint8_t *buf, int payload_len)
0265 {
0266     struct iphdr *iph = (struct iphdr *)buf;
0267 
0268     iph->ihl = 5;
0269     iph->version = 4;
0270     iph->ttl = 8;
0271     iph->tot_len =
0272         htons(sizeof(*iph) + sizeof(struct udphdr) + payload_len);
0273     iph->id = htons(1337);
0274     iph->protocol = IPPROTO_UDP;
0275     iph->saddr = htonl((172 << 24) | (17 << 16) | 2);
0276     iph->daddr = htonl((172 << 24) | (17 << 16) | 1);
0277     iph->check = build_ip_csum(buf, iph->ihl << 2, 0);
0278 
0279     return iph->ihl << 2;
0280 }
0281 
0282 static int build_udp_packet(uint8_t *buf, int payload_len, bool csum_off)
0283 {
0284     const int ip4alen = sizeof(uint32_t);
0285     struct udphdr *udph = (struct udphdr *)buf;
0286     int len = sizeof(*udph) + payload_len;
0287     uint32_t sum = 0;
0288 
0289     udph->source = htons(22);
0290     udph->dest = htons(58822);
0291     udph->len = htons(len);
0292 
0293     memset(buf + sizeof(struct udphdr), PKT_DATA, payload_len);
0294 
0295     sum = add_csum(buf - 2 * ip4alen, 2 * ip4alen);
0296     sum += htons(IPPROTO_UDP) + udph->len;
0297 
0298     if (!csum_off)
0299         sum += add_csum(buf, len);
0300 
0301     udph->check = finish_ip_csum(sum);
0302 
0303     return sizeof(*udph) + payload_len;
0304 }
0305 
0306 size_t build_test_packet_valid_udp_gso(uint8_t *buf, size_t payload_len)
0307 {
0308     uint8_t *cur = buf;
0309     struct virtio_net_hdr *vh = (struct virtio_net_hdr *)buf;
0310 
0311     vh->hdr_len = ETH_HLEN + sizeof(struct iphdr) + sizeof(struct udphdr);
0312     vh->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
0313     vh->csum_start = ETH_HLEN + sizeof(struct iphdr);
0314     vh->csum_offset = __builtin_offsetof(struct udphdr, check);
0315     vh->gso_type = VIRTIO_NET_HDR_GSO_UDP;
0316     vh->gso_size = ETH_DATA_LEN - sizeof(struct iphdr);
0317     cur += sizeof(*vh);
0318 
0319     cur += build_eth(cur, ETH_P_IP);
0320     cur += build_ipv4_header(cur, payload_len);
0321     cur += build_udp_packet(cur, payload_len, true);
0322 
0323     return cur - buf;
0324 }
0325 
0326 size_t build_test_packet_valid_udp_csum(uint8_t *buf, size_t payload_len)
0327 {
0328     uint8_t *cur = buf;
0329     struct virtio_net_hdr *vh = (struct virtio_net_hdr *)buf;
0330 
0331     vh->flags = VIRTIO_NET_HDR_F_DATA_VALID;
0332     vh->gso_type = VIRTIO_NET_HDR_GSO_NONE;
0333     cur += sizeof(*vh);
0334 
0335     cur += build_eth(cur, ETH_P_IP);
0336     cur += build_ipv4_header(cur, payload_len);
0337     cur += build_udp_packet(cur, payload_len, false);
0338 
0339     return cur - buf;
0340 }
0341 
0342 size_t build_test_packet_crash_tap_invalid_eth_proto(uint8_t *buf,
0343                              size_t payload_len)
0344 {
0345     uint8_t *cur = buf;
0346     struct virtio_net_hdr *vh = (struct virtio_net_hdr *)buf;
0347 
0348     vh->hdr_len = ETH_HLEN + sizeof(struct iphdr) + sizeof(struct udphdr);
0349     vh->flags = 0;
0350     vh->gso_type = VIRTIO_NET_HDR_GSO_UDP;
0351     vh->gso_size = ETH_DATA_LEN - sizeof(struct iphdr);
0352     cur += sizeof(*vh);
0353 
0354     cur += build_eth(cur, 0);
0355     cur += sizeof(struct iphdr) + sizeof(struct udphdr);
0356     cur += build_ipv4_header(cur, payload_len);
0357     cur += build_udp_packet(cur, payload_len, true);
0358     cur += payload_len;
0359 
0360     return cur - buf;
0361 }
0362 
0363 FIXTURE(tap)
0364 {
0365     int fd;
0366 };
0367 
0368 FIXTURE_SETUP(tap)
0369 {
0370     int ret;
0371 
0372     ret = dev_create(param_dev_dummy_name, "dummy", NULL, NULL);
0373     EXPECT_EQ(ret, 0);
0374 
0375     ret = dev_create(param_dev_tap_name, "macvtap", macvtap_fill_rtattr,
0376              NULL);
0377     EXPECT_EQ(ret, 0);
0378 
0379     self->fd = opentap(param_dev_tap_name);
0380     ASSERT_GE(self->fd, 0);
0381 }
0382 
0383 FIXTURE_TEARDOWN(tap)
0384 {
0385     int ret;
0386 
0387     if (self->fd != -1)
0388         close(self->fd);
0389 
0390     ret = dev_delete(param_dev_tap_name);
0391     EXPECT_EQ(ret, 0);
0392 
0393     ret = dev_delete(param_dev_dummy_name);
0394     EXPECT_EQ(ret, 0);
0395 }
0396 
0397 TEST_F(tap, test_packet_valid_udp_gso)
0398 {
0399     uint8_t pkt[TEST_PACKET_SZ];
0400     size_t off;
0401     int ret;
0402 
0403     memset(pkt, 0, sizeof(pkt));
0404     off = build_test_packet_valid_udp_gso(pkt, 1021);
0405     ret = write(self->fd, pkt, off);
0406     ASSERT_EQ(ret, off);
0407 }
0408 
0409 TEST_F(tap, test_packet_valid_udp_csum)
0410 {
0411     uint8_t pkt[TEST_PACKET_SZ];
0412     size_t off;
0413     int ret;
0414 
0415     memset(pkt, 0, sizeof(pkt));
0416     off = build_test_packet_valid_udp_csum(pkt, 1024);
0417     ret = write(self->fd, pkt, off);
0418     ASSERT_EQ(ret, off);
0419 }
0420 
0421 TEST_F(tap, test_packet_crash_tap_invalid_eth_proto)
0422 {
0423     uint8_t pkt[TEST_PACKET_SZ];
0424     size_t off;
0425     int ret;
0426 
0427     memset(pkt, 0, sizeof(pkt));
0428     off = build_test_packet_crash_tap_invalid_eth_proto(pkt, 1024);
0429     ret = write(self->fd, pkt, off);
0430     ASSERT_EQ(ret, -1);
0431     ASSERT_EQ(errno, EINVAL);
0432 }
0433 
0434 TEST_HARNESS_MAIN