ethertap_user.c 5.9 KB

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