plugin_net_config.c 2.5 KB

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