tuntap_user.c 4.7 KB

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