1 /*
2 * Copyright 2018 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 #include <arpa/inet.h>
8 #include <linux/if_tun.h>
9 #include <sys/ioctl.h>
10
11 #include <errno.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #include "crosvm.h"
18
19 /*
20 * These must match the network arguments supplied to the plugin in plugins.rs.
21 * IPv4 addresses here are in host-native byte order.
22 */
23 const uint32_t expected_ip = 0x64735c05; // 100.115.92.5
24 const uint32_t expected_netmask = 0xfffffffc; // 255.255.255.252
25 const uint8_t expected_mac[] = {0xde, 0x21, 0xe8, 0x47, 0x6b, 0x6a};
26
main(int argc,char ** argv)27 int main(int argc, char** argv) {
28 struct crosvm *crosvm;
29 struct crosvm_net_config net_config;
30 int ret = crosvm_connect(&crosvm);
31
32 if (ret) {
33 fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
34 return 1;
35 }
36
37 ret = crosvm_net_get_config(crosvm, &net_config);
38 if (ret) {
39 fprintf(stderr, "failed to get crosvm net config: %d\n", ret);
40 return 1;
41 }
42
43 if (net_config.tap_fd < 0) {
44 fprintf(stderr, "fd %d is < 0\n", net_config.tap_fd);
45 return 1;
46 }
47
48 unsigned int features;
49 if (ioctl(net_config.tap_fd, TUNGETFEATURES, &features) < 0) {
50 fprintf(stderr,
51 "failed to read tap features: %s\n",
52 strerror(errno));
53 return 1;
54 }
55
56 if (net_config.host_ip != htonl(expected_ip)) {
57 char ip_addr[INET_ADDRSTRLEN];
58 inet_ntop(AF_INET, &net_config.host_ip, ip_addr, sizeof(ip_addr));
59 fprintf(stderr, "ip %s != 100.115.92.5\n", ip_addr);
60 return 1;
61 }
62
63 if (net_config.netmask != htonl(expected_netmask)) {
64 char netmask[INET_ADDRSTRLEN];
65 inet_ntop(AF_INET, &net_config.netmask, netmask, sizeof(netmask));
66 fprintf(stderr, "netmask %s != 255.255.255.252\n", netmask);
67 return 1;
68 }
69
70 if (memcmp(net_config.host_mac_address,
71 expected_mac,
72 sizeof(expected_mac)) != 0) {
73 fprintf(stderr,
74 "mac %02X:%02X:%02X:%02X:%02X:%02X != de:21:e8:47:6b:6a\n",
75 net_config.host_mac_address[0],
76 net_config.host_mac_address[1],
77 net_config.host_mac_address[2],
78 net_config.host_mac_address[3],
79 net_config.host_mac_address[4],
80 net_config.host_mac_address[5]);
81 return 1;
82 }
83
84 return 0;
85 }
86