cellnet.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <getopt.h>
  4. #include <libgen.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #define LOG_TAG "Cells/cellnet"
  10. #include <cutils/log.h>
  11. #include <cutils/memory.h>
  12. #include <cutils/misc.h>
  13. #define INTERFACE "wlan0"
  14. #define VETH0 "vm_wlan_%d_0"
  15. #define VETH1 "vm_wlan_%d_1"
  16. #define VETHIFACEADDRBASE 16
  17. #define VETHMASK 16
  18. #define VETHADDRMASK "255.255.0.0"
  19. #define VETHGATEADDR "172.%d.0.0"
  20. #define VETHIFACEADDR0 "172.%d.3.2"
  21. #define VETHIFACEADDR1 "172.%d.3.3"
  22. void init_cell_net()
  23. {
  24. }
  25. void createveth(int index)
  26. {
  27. int ret = 0;
  28. char vethname0[24] = {0};
  29. char vethname1[24] = {0};
  30. char vethaddr0[64] = {0};
  31. char vethgateaddr[64] = {0};
  32. char cmd[256] = {0};
  33. snprintf(vethname0, sizeof(vethname0), VETH0, index);
  34. snprintf(vethname1, sizeof(vethname1), VETH1, index);
  35. snprintf(vethgateaddr, sizeof(vethgateaddr), VETHGATEADDR, VETHIFACEADDRBASE + index);
  36. snprintf(vethaddr0, sizeof(vethaddr0), VETHIFACEADDR0, VETHIFACEADDRBASE + index);
  37. errno = 0;
  38. memset(cmd, 0, sizeof(cmd));
  39. snprintf(cmd, sizeof(cmd), "ip link add name %s type veth peer name %s", vethname0, vethname1);
  40. ret = system(cmd);
  41. ALOGD("%s errno = %s",cmd ,strerror(errno));
  42. errno = 0;
  43. memset(cmd, 0, sizeof(cmd));
  44. snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s up", vethname0, vethaddr0, VETHADDRMASK);
  45. ret = system(cmd);
  46. ALOGD("%s errno = %s",cmd ,strerror(errno));
  47. errno = 0;
  48. memset(cmd, 0, sizeof(cmd));
  49. snprintf(cmd, sizeof(cmd), "iptables -t nat -A POSTROUTING -s %s/%d -o %s -j MASQUERADE", vethgateaddr, VETHMASK, INTERFACE);
  50. ret = system(cmd);
  51. ALOGD("%s errno = %s",cmd ,strerror(errno));
  52. errno = 0;
  53. memset(cmd, 0, sizeof(cmd));
  54. snprintf(cmd, sizeof(cmd), "iptables -t filter -A FORWARD -i %s -o %s -j ACCEPT", INTERFACE, vethname0);
  55. ret = system(cmd);
  56. ALOGD("%s errno = %s",cmd ,strerror(errno));
  57. errno = 0;
  58. memset(cmd, 0, sizeof(cmd));
  59. snprintf(cmd, sizeof(cmd), "iptables -t filter -A FORWARD -o %s -i %s -j ACCEPT", INTERFACE, vethname0);
  60. ret = system(cmd);
  61. ALOGD("%s errno = %s",cmd ,strerror(errno));
  62. }
  63. void vethtons(int pid,int index)
  64. {
  65. int ret = 0;
  66. char vethname1[24] = {0};
  67. char cmd[256];
  68. snprintf(vethname1, sizeof(vethname1), VETH1, index);
  69. errno = 0;
  70. memset(cmd,0,sizeof(cmd));
  71. snprintf(cmd, sizeof(cmd), "ip link set %s netns %d",vethname1 ,pid );
  72. ret = system(cmd);
  73. ALOGD("%s errno = %s",cmd,strerror(errno));
  74. }
  75. void rnameveth(int index)
  76. {
  77. int ret = 0;
  78. char vethname1[24] = {0};
  79. char vethaddr0[64] = {0};
  80. char vethaddr1[64] = {0};
  81. char cmd[256];
  82. snprintf(vethname1, sizeof(vethname1), VETH1, index);
  83. snprintf(vethaddr0, sizeof(vethaddr0), VETHIFACEADDR0, VETHIFACEADDRBASE + index);
  84. snprintf(vethaddr1, sizeof(vethaddr1), VETHIFACEADDR1, VETHIFACEADDRBASE + index);
  85. errno = 0;
  86. memset(cmd,0,sizeof(cmd));
  87. snprintf(cmd, sizeof(cmd), "ip link set %s name %s",vethname1 ,INTERFACE );
  88. ret = system(cmd);
  89. ALOGD("%s errno = %s",cmd,strerror(errno));
  90. errno = 0;
  91. memset(cmd,0,sizeof(cmd));
  92. snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s up",INTERFACE,vethaddr1,VETHADDRMASK);
  93. ret = system(cmd);
  94. ALOGD("%s errno = %s",cmd,strerror(errno));
  95. errno = 0;
  96. memset(cmd,0,sizeof(cmd));
  97. snprintf(cmd, sizeof(cmd), "ip ro add default via %s dev %s",vethaddr0 ,INTERFACE);
  98. ret = system(cmd);
  99. ALOGD("%s errno = %s",cmd,strerror(errno));
  100. }
  101. void starttether(int index)
  102. {
  103. int ret = 0;
  104. char vethname0[24] = {0};
  105. char vethaddr0[64] = {0};
  106. char vethgateaddr[64] = {0};
  107. char cmd[256] = {0};
  108. snprintf(vethname0, sizeof(vethname0), VETH0, index);
  109. snprintf(vethgateaddr, sizeof(vethgateaddr), VETHGATEADDR, VETHIFACEADDRBASE + index);
  110. snprintf(vethaddr0, sizeof(vethaddr0), VETHIFACEADDR0, VETHIFACEADDRBASE + index);
  111. errno = 0;
  112. memset(cmd,0,sizeof(cmd));
  113. snprintf(cmd, sizeof(cmd), "ndc interface setcfg %s %s %d up multicast broadcast",vethname0 ,vethaddr0 ,VETHMASK);
  114. ret = system(cmd);
  115. ALOGD("%s errno = %s",cmd,strerror(errno));
  116. errno = 0;
  117. memset(cmd,0,sizeof(cmd));
  118. snprintf(cmd, sizeof(cmd), "ndc tether interface add %s",vethname0);
  119. ret = system(cmd);
  120. ALOGD("%s errno = %s",cmd,strerror(errno));
  121. errno = 0;
  122. memset(cmd,0,sizeof(cmd));
  123. snprintf(cmd, sizeof(cmd), "ndc network interface add local %s",vethname0);
  124. ret = system(cmd);
  125. ALOGD("%s errno = %s",cmd,strerror(errno));
  126. errno = 0;
  127. memset(cmd,0,sizeof(cmd));
  128. snprintf(cmd, sizeof(cmd), "ndc network route add local %s %s/%d",vethname0,vethgateaddr,VETHMASK);
  129. ret = system(cmd);
  130. ALOGD("%s errno = %s",cmd,strerror(errno));
  131. errno = 0;
  132. memset(cmd,0,sizeof(cmd));
  133. snprintf(cmd, sizeof(cmd), "ndc ipfwd enable %s",vethname0);
  134. ret = system(cmd);
  135. ALOGD("%s errno = %s",cmd,strerror(errno));
  136. errno = 0;
  137. memset(cmd,0,sizeof(cmd));
  138. snprintf(cmd, sizeof(cmd), "ndc nat enable %s %s 1 %s/%d",vethname0,INTERFACE,vethgateaddr,VETHMASK);
  139. ret = system(cmd);
  140. ALOGD("%s errno = %s",cmd,strerror(errno));
  141. errno = 0;
  142. memset(cmd,0,sizeof(cmd));
  143. snprintf(cmd, sizeof(cmd), "ndc ipfwd add %s %s",vethname0,INTERFACE);
  144. ret = system(cmd);
  145. ALOGD("%s errno = %s",cmd,strerror(errno));
  146. }
  147. void stoptether(int index)
  148. {
  149. int ret = 0;
  150. char vethname0[24] = {0};
  151. char cmd[256] = {0};
  152. snprintf(vethname0, sizeof(vethname0), VETH0, index);
  153. errno = 0;
  154. memset(cmd,0,sizeof(cmd));
  155. snprintf(cmd, sizeof(cmd), "ndc tether interface remove %s",vethname0);
  156. ret = system(cmd);
  157. ALOGD("%s errno = %s",cmd,strerror(errno));
  158. errno = 0;
  159. memset(cmd,0,sizeof(cmd));
  160. snprintf(cmd, sizeof(cmd), "ndc network interface remove local %s",vethname0);
  161. ret = system(cmd);
  162. ALOGD("%s errno = %s",cmd,strerror(errno));
  163. errno = 0;
  164. memset(cmd,0,sizeof(cmd));
  165. snprintf(cmd, sizeof(cmd), "ndc ipfwd disable %s",vethname0);
  166. ret = system(cmd);
  167. ALOGD("%s errno = %s",cmd,strerror(errno));
  168. }
  169. void delveth(int index)
  170. {
  171. int ret = 0;
  172. char vethname0[24] = {0};
  173. char vethaddr0[64] = {0};
  174. char vethgateaddr[64] = {0};
  175. char cmd[256] = {0};
  176. snprintf(vethname0, sizeof(vethname0), VETH0, index);
  177. snprintf(vethgateaddr, sizeof(vethgateaddr), VETHGATEADDR, VETHIFACEADDRBASE + index);
  178. snprintf(vethaddr0, sizeof(vethaddr0), VETHIFACEADDR0, VETHIFACEADDRBASE + index);
  179. errno = 0;
  180. memset(cmd,0,sizeof(cmd));
  181. snprintf(cmd, sizeof(cmd), "iptables -t nat -D POSTROUTING -s %s/%d -o %s -j MASQUERADE",vethgateaddr,VETHMASK,INTERFACE);
  182. ret = system(cmd);
  183. ALOGD("%s errno = %s",cmd,strerror(errno));
  184. errno = 0;
  185. memset(cmd,0,sizeof(cmd));
  186. snprintf(cmd, sizeof(cmd), "iptables -t filter -D FORWARD -i %s -o %s -j ACCEPT",INTERFACE,vethname0);
  187. ret = system(cmd);
  188. ALOGD("%s errno = %s",cmd,strerror(errno));
  189. errno = 0;
  190. memset(cmd,0,sizeof(cmd));
  191. snprintf(cmd, sizeof(cmd), "iptables -t filter -D FORWARD -o %s -i %s -j ACCEPT",INTERFACE,vethname0);
  192. ret = system(cmd);
  193. ALOGD("%s errno = %s",cmd,strerror(errno));
  194. errno = 0;
  195. memset(cmd,0,sizeof(cmd));
  196. snprintf(cmd, sizeof(cmd), "ip link delete %s type veth",vethname0);
  197. ret = system(cmd);
  198. ALOGD("%s errno = %s",cmd,strerror(errno));
  199. }