ethertap_user.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
  4. * James Leu (jleu@mindspring.net).
  5. * Copyright (C) 2001 by various other people who didn't put their name here.
  6. * Licensed under the GPL.
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <sys/socket.h>
  13. #include <sys/wait.h>
  14. #include "etap.h"
  15. #include <os.h>
  16. #include <net_user.h>
  17. #include <um_malloc.h>
  18. #define MAX_PACKET ETH_MAX_PACKET
  19. static int etap_user_init(void *data, void *dev)
  20. {
  21. struct ethertap_data *pri = data;
  22. pri->dev = dev;
  23. return 0;
  24. }
  25. struct addr_change {
  26. enum { ADD_ADDR, DEL_ADDR } what;
  27. unsigned char addr[4];
  28. unsigned char netmask[4];
  29. };
  30. static void etap_change(int op, unsigned char *addr, unsigned char *netmask,
  31. int fd)
  32. {
  33. struct addr_change change;
  34. char *output;
  35. int n;
  36. change.what = op;
  37. memcpy(change.addr, addr, sizeof(change.addr));
  38. memcpy(change.netmask, netmask, sizeof(change.netmask));
  39. CATCH_EINTR(n = write(fd, &change, sizeof(change)));
  40. if (n != sizeof(change)) {
  41. printk(UM_KERN_ERR "etap_change - request failed, err = %d\n",
  42. errno);
  43. return;
  44. }
  45. output = uml_kmalloc(UM_KERN_PAGE_SIZE, UM_GFP_KERNEL);
  46. if (output == NULL)
  47. printk(UM_KERN_ERR "etap_change : Failed to allocate output "
  48. "buffer\n");
  49. read_output(fd, output, UM_KERN_PAGE_SIZE);
  50. if (output != NULL) {
  51. printk("%s", output);
  52. kfree(output);
  53. }
  54. }
  55. static void etap_open_addr(unsigned char *addr, unsigned char *netmask,
  56. void *arg)
  57. {
  58. etap_change(ADD_ADDR, addr, netmask, *((int *) arg));
  59. }
  60. static void etap_close_addr(unsigned char *addr, unsigned char *netmask,
  61. void *arg)
  62. {
  63. etap_change(DEL_ADDR, addr, netmask, *((int *) arg));
  64. }
  65. struct etap_pre_exec_data {
  66. int control_remote;
  67. int control_me;
  68. int data_me;
  69. };
  70. static void etap_pre_exec(void *arg)
  71. {
  72. struct etap_pre_exec_data *data = arg;
  73. dup2(data->control_remote, 1);
  74. close(data->data_me);
  75. close(data->control_me);
  76. }
  77. static int etap_tramp(char *dev, char *gate, int control_me,
  78. int control_remote, int data_me, int data_remote)
  79. {
  80. struct etap_pre_exec_data pe_data;
  81. int pid, err, n;
  82. char version_buf[sizeof("nnnnn\0")];
  83. char data_fd_buf[sizeof("nnnnnn\0")];
  84. char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")];
  85. char *setup_args[] = { "uml_net", version_buf, "ethertap", dev,
  86. data_fd_buf, gate_buf, NULL };
  87. char *nosetup_args[] = { "uml_net", version_buf, "ethertap",
  88. dev, data_fd_buf, NULL };
  89. char **args, c;
  90. sprintf(data_fd_buf, "%d", data_remote);
  91. sprintf(version_buf, "%d", UML_NET_VERSION);
  92. if (gate != NULL) {
  93. strncpy(gate_buf, gate, 15);
  94. args = setup_args;
  95. }
  96. else args = nosetup_args;
  97. err = 0;
  98. pe_data.control_remote = control_remote;
  99. pe_data.control_me = control_me;
  100. pe_data.data_me = data_me;
  101. pid = run_helper(etap_pre_exec, &pe_data, args);
  102. if (pid < 0)
  103. err = pid;
  104. close(data_remote);
  105. close(control_remote);
  106. CATCH_EINTR(n = read(control_me, &c, sizeof(c)));
  107. if (n != sizeof(c)) {
  108. err = -errno;
  109. printk(UM_KERN_ERR "etap_tramp : read of status failed, "
  110. "err = %d\n", -err);
  111. return err;
  112. }
  113. if (c != 1) {
  114. printk(UM_KERN_ERR "etap_tramp : uml_net failed\n");
  115. err = helper_wait(pid);
  116. }
  117. return err;
  118. }
  119. static int etap_open(void *data)
  120. {
  121. struct ethertap_data *pri = data;
  122. char *output;
  123. int data_fds[2], control_fds[2], err, output_len;
  124. err = tap_open_common(pri->dev, pri->gate_addr);
  125. if (err)
  126. return err;
  127. err = socketpair(AF_UNIX, SOCK_DGRAM, 0, data_fds);
  128. if (err) {
  129. err = -errno;
  130. printk(UM_KERN_ERR "etap_open - data socketpair failed - "
  131. "err = %d\n", errno);
  132. return err;
  133. }
  134. err = socketpair(AF_UNIX, SOCK_STREAM, 0, control_fds);
  135. if (err) {
  136. err = -errno;
  137. printk(UM_KERN_ERR "etap_open - control socketpair failed - "
  138. "err = %d\n", errno);
  139. goto out_close_data;
  140. }
  141. err = etap_tramp(pri->dev_name, pri->gate_addr, control_fds[0],
  142. control_fds[1], data_fds[0], data_fds[1]);
  143. output_len = UM_KERN_PAGE_SIZE;
  144. output = uml_kmalloc(output_len, UM_GFP_KERNEL);
  145. read_output(control_fds[0], output, output_len);
  146. if (output == NULL)
  147. printk(UM_KERN_ERR "etap_open : failed to allocate output "
  148. "buffer\n");
  149. else {
  150. printk("%s", output);
  151. kfree(output);
  152. }
  153. if (err < 0) {
  154. printk(UM_KERN_ERR "etap_tramp failed - err = %d\n", -err);
  155. goto out_close_control;
  156. }
  157. pri->data_fd = data_fds[0];
  158. pri->control_fd = control_fds[0];
  159. iter_addresses(pri->dev, etap_open_addr, &pri->control_fd);
  160. return data_fds[0];
  161. out_close_control:
  162. close(control_fds[0]);
  163. close(control_fds[1]);
  164. out_close_data:
  165. close(data_fds[0]);
  166. close(data_fds[1]);
  167. return err;
  168. }
  169. static void etap_close(int fd, void *data)
  170. {
  171. struct ethertap_data *pri = data;
  172. iter_addresses(pri->dev, etap_close_addr, &pri->control_fd);
  173. close(fd);
  174. if (shutdown(pri->data_fd, SHUT_RDWR) < 0)
  175. printk(UM_KERN_ERR "etap_close - shutdown data socket failed, "
  176. "errno = %d\n", errno);
  177. if (shutdown(pri->control_fd, SHUT_RDWR) < 0)
  178. printk(UM_KERN_ERR "etap_close - shutdown control socket "
  179. "failed, errno = %d\n", errno);
  180. close(pri->data_fd);
  181. pri->data_fd = -1;
  182. close(pri->control_fd);
  183. pri->control_fd = -1;
  184. }
  185. static void etap_add_addr(unsigned char *addr, unsigned char *netmask,
  186. void *data)
  187. {
  188. struct ethertap_data *pri = data;
  189. tap_check_ips(pri->gate_addr, addr);
  190. if (pri->control_fd == -1)
  191. return;
  192. etap_open_addr(addr, netmask, &pri->control_fd);
  193. }
  194. static void etap_del_addr(unsigned char *addr, unsigned char *netmask,
  195. void *data)
  196. {
  197. struct ethertap_data *pri = data;
  198. if (pri->control_fd == -1)
  199. return;
  200. etap_close_addr(addr, netmask, &pri->control_fd);
  201. }
  202. const struct net_user_info ethertap_user_info = {
  203. .init = etap_user_init,
  204. .open = etap_open,
  205. .close = etap_close,
  206. .remove = NULL,
  207. .add_address = etap_add_addr,
  208. .delete_address = etap_del_addr,
  209. .mtu = ETH_MAX_PACKET,
  210. .max_packet = ETH_MAX_PACKET + ETH_HEADER_ETHERTAP,
  211. };