net_user.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdarg.h>
  8. #include <errno.h>
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <sys/wait.h>
  13. #include <net_user.h>
  14. #include <os.h>
  15. #include <um_malloc.h>
  16. int tap_open_common(void *dev, char *gate_addr)
  17. {
  18. int tap_addr[4];
  19. if (gate_addr == NULL)
  20. return 0;
  21. if (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
  22. &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4) {
  23. printk(UM_KERN_ERR "Invalid tap IP address - '%s'\n",
  24. gate_addr);
  25. return -EINVAL;
  26. }
  27. return 0;
  28. }
  29. void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
  30. {
  31. int tap_addr[4];
  32. if ((gate_addr != NULL) &&
  33. (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
  34. &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) &&
  35. (eth_addr[0] == tap_addr[0]) &&
  36. (eth_addr[1] == tap_addr[1]) &&
  37. (eth_addr[2] == tap_addr[2]) &&
  38. (eth_addr[3] == tap_addr[3])) {
  39. printk(UM_KERN_ERR "The tap IP address and the UML eth IP "
  40. "address must be different\n");
  41. }
  42. }
  43. /* Do reliable error handling as this fails frequently enough. */
  44. void read_output(int fd, char *output, int len)
  45. {
  46. int remain, ret, expected;
  47. char c;
  48. char *str;
  49. if (output == NULL) {
  50. output = &c;
  51. len = sizeof(c);
  52. }
  53. *output = '\0';
  54. ret = read(fd, &remain, sizeof(remain));
  55. if (ret != sizeof(remain)) {
  56. if (ret < 0)
  57. ret = -errno;
  58. expected = sizeof(remain);
  59. str = "length";
  60. goto err;
  61. }
  62. while (remain != 0) {
  63. expected = (remain < len) ? remain : len;
  64. ret = read(fd, output, expected);
  65. if (ret != expected) {
  66. if (ret < 0)
  67. ret = -errno;
  68. str = "data";
  69. goto err;
  70. }
  71. remain -= ret;
  72. }
  73. return;
  74. err:
  75. if (ret < 0)
  76. printk(UM_KERN_ERR "read_output - read of %s failed, "
  77. "errno = %d\n", str, -ret);
  78. else
  79. printk(UM_KERN_ERR "read_output - read of %s failed, read only "
  80. "%d of %d bytes\n", str, ret, expected);
  81. }
  82. int net_read(int fd, void *buf, int len)
  83. {
  84. int n;
  85. n = read(fd, buf, len);
  86. if ((n < 0) && (errno == EAGAIN))
  87. return 0;
  88. else if (n == 0)
  89. return -ENOTCONN;
  90. return n;
  91. }
  92. int net_recvfrom(int fd, void *buf, int len)
  93. {
  94. int n;
  95. CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
  96. if (n < 0) {
  97. if (errno == EAGAIN)
  98. return 0;
  99. return -errno;
  100. }
  101. else if (n == 0)
  102. return -ENOTCONN;
  103. return n;
  104. }
  105. int net_write(int fd, void *buf, int len)
  106. {
  107. int n;
  108. n = write(fd, buf, len);
  109. if ((n < 0) && (errno == EAGAIN))
  110. return 0;
  111. else if (n == 0)
  112. return -ENOTCONN;
  113. return n;
  114. }
  115. int net_send(int fd, void *buf, int len)
  116. {
  117. int n;
  118. CATCH_EINTR(n = send(fd, buf, len, 0));
  119. if (n < 0) {
  120. if (errno == EAGAIN)
  121. return 0;
  122. return -errno;
  123. }
  124. else if (n == 0)
  125. return -ENOTCONN;
  126. return n;
  127. }
  128. int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
  129. {
  130. int n;
  131. CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
  132. sock_len));
  133. if (n < 0) {
  134. if (errno == EAGAIN)
  135. return 0;
  136. return -errno;
  137. }
  138. else if (n == 0)
  139. return -ENOTCONN;
  140. return n;
  141. }
  142. struct change_pre_exec_data {
  143. int close_me;
  144. int stdout_fd;
  145. };
  146. static void change_pre_exec(void *arg)
  147. {
  148. struct change_pre_exec_data *data = arg;
  149. close(data->close_me);
  150. dup2(data->stdout_fd, 1);
  151. }
  152. static int change_tramp(char **argv, char *output, int output_len)
  153. {
  154. int pid, fds[2], err;
  155. struct change_pre_exec_data pe_data;
  156. err = os_pipe(fds, 1, 0);
  157. if (err < 0) {
  158. printk(UM_KERN_ERR "change_tramp - pipe failed, err = %d\n",
  159. -err);
  160. return err;
  161. }
  162. pe_data.close_me = fds[0];
  163. pe_data.stdout_fd = fds[1];
  164. pid = run_helper(change_pre_exec, &pe_data, argv);
  165. if (pid > 0) /* Avoid hang as we won't get data in failure case. */
  166. read_output(fds[0], output, output_len);
  167. close(fds[0]);
  168. close(fds[1]);
  169. if (pid > 0)
  170. helper_wait(pid);
  171. return pid;
  172. }
  173. static void change(char *dev, char *what, unsigned char *addr,
  174. unsigned char *netmask)
  175. {
  176. char addr_buf[sizeof("255.255.255.255\0")];
  177. char netmask_buf[sizeof("255.255.255.255\0")];
  178. char version[sizeof("nnnnn\0")];
  179. char *argv[] = { "uml_net", version, what, dev, addr_buf,
  180. netmask_buf, NULL };
  181. char *output;
  182. int output_len, pid;
  183. sprintf(version, "%d", UML_NET_VERSION);
  184. sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
  185. sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
  186. netmask[2], netmask[3]);
  187. output_len = UM_KERN_PAGE_SIZE;
  188. output = uml_kmalloc(output_len, UM_GFP_KERNEL);
  189. if (output == NULL)
  190. printk(UM_KERN_ERR "change : failed to allocate output "
  191. "buffer\n");
  192. pid = change_tramp(argv, output, output_len);
  193. if (pid < 0) {
  194. kfree(output);
  195. return;
  196. }
  197. if (output != NULL) {
  198. printk("%s", output);
  199. kfree(output);
  200. }
  201. }
  202. void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
  203. {
  204. change(arg, "add", addr, netmask);
  205. }
  206. void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
  207. {
  208. change(arg, "del", addr, netmask);
  209. }
  210. char *split_if_spec(char *str, ...)
  211. {
  212. char **arg, *end;
  213. va_list ap;
  214. va_start(ap, str);
  215. while ((arg = va_arg(ap, char **)) != NULL) {
  216. if (*str == '\0')
  217. return NULL;
  218. end = strchr(str, ',');
  219. if (end != str)
  220. *arg = str;
  221. if (end == NULL)
  222. return NULL;
  223. *end++ = '\0';
  224. str = end;
  225. }
  226. va_end(ap);
  227. return str;
  228. }