ip-up-vpn.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <arpa/inet.h>
  21. #include <netinet/in.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <sys/ioctl.h>
  26. #include <linux/if.h>
  27. #include <linux/route.h>
  28. #define LOG_TAG "ip-up-vpn"
  29. #include <cutils/log.h>
  30. #define DIR "/data/misc/vpn/"
  31. static const char *env(const char *name) {
  32. const char *value = getenv(name);
  33. return value ? value : "";
  34. }
  35. static int set_address(struct sockaddr *sa, const char *address) {
  36. sa->sa_family = AF_INET;
  37. errno = EINVAL;
  38. return inet_pton(AF_INET, address, &((struct sockaddr_in *)sa)->sin_addr);
  39. }
  40. /*
  41. * The primary goal is to create a file with VPN parameters. Currently they
  42. * are interface, addresses, routes, DNS servers, and search domains and VPN
  43. * server address. Each parameter occupies one line in the file, and it can be
  44. * an empty string or space-separated values. The order and the format must be
  45. * consistent with com.android.server.connectivity.Vpn. Here is an example.
  46. *
  47. * ppp0
  48. * 192.168.1.100/24
  49. * 0.0.0.0/0
  50. * 192.168.1.1 192.168.1.2
  51. * example.org
  52. * 192.0.2.1
  53. *
  54. * The secondary goal is to unify the outcome of VPN. The current baseline
  55. * is to have an interface configured with the given address and netmask
  56. * and maybe add a host route to protect the tunnel. PPP-based VPN already
  57. * does this, but others might not. Routes, DNS servers, and search domains
  58. * are handled by the framework since they can be overridden by the users.
  59. */
  60. int main(int argc, char **argv)
  61. {
  62. FILE *state = fopen(DIR ".tmp", "wb");
  63. if (!state) {
  64. ALOGE("Cannot create state: %s", strerror(errno));
  65. return 1;
  66. }
  67. if (argc >= 6) {
  68. /* Invoked by pppd. */
  69. fprintf(state, "%s\n", argv[1]);
  70. fprintf(state, "%s/32\n", argv[4]);
  71. fprintf(state, "0.0.0.0/0\n");
  72. fprintf(state, "%s %s\n", env("DNS1"), env("DNS2"));
  73. fprintf(state, "\n");
  74. fprintf(state, "\n");
  75. } else if (argc == 2) {
  76. /* Invoked by racoon. */
  77. const char *interface = env("INTERFACE");
  78. const char *address = env("INTERNAL_ADDR4");
  79. const char *routes = env("SPLIT_INCLUDE_CIDR");
  80. int s = socket(AF_INET, SOCK_DGRAM, 0);
  81. struct ifreq ifr;
  82. memset(&ifr, 0, sizeof(ifr));
  83. /* Bring up the interface. */
  84. ifr.ifr_flags = IFF_UP;
  85. strncpy(ifr.ifr_name, interface, IFNAMSIZ);
  86. if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
  87. ALOGE("Cannot bring up %s: %s", interface, strerror(errno));
  88. return 1;
  89. }
  90. /* Set the address. */
  91. if (!set_address(&ifr.ifr_addr, address) ||
  92. ioctl(s, SIOCSIFADDR, &ifr)) {
  93. ALOGE("Cannot set address: %s", strerror(errno));
  94. return 1;
  95. }
  96. /* Set the netmask. */
  97. if (set_address(&ifr.ifr_netmask, env("INTERNAL_NETMASK4"))) {
  98. if (ioctl(s, SIOCSIFNETMASK, &ifr)) {
  99. ALOGE("Cannot set netmask: %s", strerror(errno));
  100. return 1;
  101. }
  102. }
  103. /* TODO: Send few packets to trigger phase 2? */
  104. fprintf(state, "%s\n", interface);
  105. fprintf(state, "%s/%s\n", address, env("INTERNAL_CIDR4"));
  106. fprintf(state, "%s\n", routes[0] ? routes : "0.0.0.0/0");
  107. fprintf(state, "%s\n", env("INTERNAL_DNS4_LIST"));
  108. fprintf(state, "%s\n", env("DEFAULT_DOMAIN"));
  109. fprintf(state, "%s\n", env("REMOTE_ADDR"));
  110. } else {
  111. ALOGE("Cannot parse parameters");
  112. return 1;
  113. }
  114. fclose(state);
  115. if (chmod(DIR ".tmp", 0444) || rename(DIR ".tmp", DIR "state")) {
  116. ALOGE("Cannot write state: %s", strerror(errno));
  117. return 1;
  118. }
  119. return 0;
  120. }