net_user.c 5.2 KB

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