tuntap_user.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <errno.h>
  8. #include <string.h>
  9. #include <linux/if_tun.h>
  10. #include <net/if.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/socket.h>
  13. #include <sys/wait.h>
  14. #include <sys/uio.h>
  15. #include <kern_util.h>
  16. #include <os.h>
  17. #include "tuntap.h"
  18. static int tuntap_user_init(void *data, void *dev)
  19. {
  20. struct tuntap_data *pri = data;
  21. pri->dev = dev;
  22. return 0;
  23. }
  24. static void tuntap_add_addr(unsigned char *addr, unsigned char *netmask,
  25. void *data)
  26. {
  27. struct tuntap_data *pri = data;
  28. tap_check_ips(pri->gate_addr, addr);
  29. if ((pri->fd == -1) || pri->fixed_config)
  30. return;
  31. open_addr(addr, netmask, pri->dev_name);
  32. }
  33. static void tuntap_del_addr(unsigned char *addr, unsigned char *netmask,
  34. void *data)
  35. {
  36. struct tuntap_data *pri = data;
  37. if ((pri->fd == -1) || pri->fixed_config)
  38. return;
  39. close_addr(addr, netmask, pri->dev_name);
  40. }
  41. struct tuntap_pre_exec_data {
  42. int stdout_fd;
  43. int close_me;
  44. };
  45. static void tuntap_pre_exec(void *arg)
  46. {
  47. struct tuntap_pre_exec_data *data = arg;
  48. dup2(data->stdout_fd, 1);
  49. close(data->close_me);
  50. }
  51. static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote,
  52. char *buffer, int buffer_len, int *used_out)
  53. {
  54. struct tuntap_pre_exec_data data;
  55. char version_buf[sizeof("nnnnn\0")];
  56. char *argv[] = { "uml_net", version_buf, "tuntap", "up", gate,
  57. NULL };
  58. char buf[CMSG_SPACE(sizeof(*fd_out))];
  59. struct msghdr msg;
  60. struct cmsghdr *cmsg;
  61. struct iovec iov;
  62. int pid, n, err;
  63. sprintf(version_buf, "%d", UML_NET_VERSION);
  64. data.stdout_fd = remote;
  65. data.close_me = me;
  66. pid = run_helper(tuntap_pre_exec, &data, argv);
  67. if (pid < 0)
  68. return -pid;
  69. close(remote);
  70. msg.msg_name = NULL;
  71. msg.msg_namelen = 0;
  72. if (buffer != NULL) {
  73. iov = ((struct iovec) { buffer, buffer_len });
  74. msg.msg_iov = &iov;
  75. msg.msg_iovlen = 1;
  76. }
  77. else {
  78. msg.msg_iov = NULL;
  79. msg.msg_iovlen = 0;
  80. }
  81. msg.msg_control = buf;
  82. msg.msg_controllen = sizeof(buf);
  83. msg.msg_flags = 0;
  84. n = recvmsg(me, &msg, 0);
  85. *used_out = n;
  86. if (n < 0) {
  87. err = -errno;
  88. printk(UM_KERN_ERR "tuntap_open_tramp : recvmsg failed - "
  89. "errno = %d\n", errno);
  90. return err;
  91. }
  92. helper_wait(pid);
  93. cmsg = CMSG_FIRSTHDR(&msg);
  94. if (cmsg == NULL) {
  95. printk(UM_KERN_ERR "tuntap_open_tramp : didn't receive a "
  96. "message\n");
  97. return -EINVAL;
  98. }
  99. if ((cmsg->cmsg_level != SOL_SOCKET) ||
  100. (cmsg->cmsg_type != SCM_RIGHTS)) {
  101. printk(UM_KERN_ERR "tuntap_open_tramp : didn't receive a "
  102. "descriptor\n");
  103. return -EINVAL;
  104. }
  105. *fd_out = ((int *) CMSG_DATA(cmsg))[0];
  106. os_set_exec_close(*fd_out);
  107. return 0;
  108. }
  109. static int tuntap_open(void *data)
  110. {
  111. struct ifreq ifr;
  112. struct tuntap_data *pri = data;
  113. char *output, *buffer;
  114. int err, fds[2], len, used;
  115. err = tap_open_common(pri->dev, pri->gate_addr);
  116. if (err < 0)
  117. return err;
  118. if (pri->fixed_config) {
  119. pri->fd = os_open_file("/dev/net/tun",
  120. of_cloexec(of_rdwr(OPENFLAGS())), 0);
  121. if (pri->fd < 0) {
  122. printk(UM_KERN_ERR "Failed to open /dev/net/tun, "
  123. "err = %d\n", -pri->fd);
  124. return pri->fd;
  125. }
  126. memset(&ifr, 0, sizeof(ifr));
  127. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  128. strlcpy(ifr.ifr_name, pri->dev_name, sizeof(ifr.ifr_name));
  129. if (ioctl(pri->fd, TUNSETIFF, &ifr) < 0) {
  130. err = -errno;
  131. printk(UM_KERN_ERR "TUNSETIFF failed, errno = %d\n",
  132. errno);
  133. close(pri->fd);
  134. return err;
  135. }
  136. }
  137. else {
  138. err = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds);
  139. if (err) {
  140. err = -errno;
  141. printk(UM_KERN_ERR "tuntap_open : socketpair failed - "
  142. "errno = %d\n", errno);
  143. return err;
  144. }
  145. buffer = get_output_buffer(&len);
  146. if (buffer != NULL)
  147. len--;
  148. used = 0;
  149. err = tuntap_open_tramp(pri->gate_addr, &pri->fd, fds[0],
  150. fds[1], buffer, len, &used);
  151. output = buffer;
  152. if (err < 0) {
  153. printk("%s", output);
  154. free_output_buffer(buffer);
  155. printk(UM_KERN_ERR "tuntap_open_tramp failed - "
  156. "err = %d\n", -err);
  157. return err;
  158. }
  159. pri->dev_name = uml_strdup(buffer);
  160. output += IFNAMSIZ;
  161. printk("%s", output);
  162. free_output_buffer(buffer);
  163. close(fds[0]);
  164. iter_addresses(pri->dev, open_addr, pri->dev_name);
  165. }
  166. return pri->fd;
  167. }
  168. static void tuntap_close(int fd, void *data)
  169. {
  170. struct tuntap_data *pri = data;
  171. if (!pri->fixed_config)
  172. iter_addresses(pri->dev, close_addr, pri->dev_name);
  173. close(fd);
  174. pri->fd = -1;
  175. }
  176. const struct net_user_info tuntap_user_info = {
  177. .init = tuntap_user_init,
  178. .open = tuntap_open,
  179. .close = tuntap_close,
  180. .remove = NULL,
  181. .add_address = tuntap_add_addr,
  182. .delete_address = tuntap_del_addr,
  183. .mtu = ETH_MAX_PACKET,
  184. .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
  185. };