slip_user.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <sys/termios.h>
  12. #include <sys/wait.h>
  13. #include <net_user.h>
  14. #include <os.h>
  15. #include "slip.h"
  16. #include <um_malloc.h>
  17. static int slip_user_init(void *data, void *dev)
  18. {
  19. struct slip_data *pri = data;
  20. pri->dev = dev;
  21. return 0;
  22. }
  23. static int set_up_tty(int fd)
  24. {
  25. int i;
  26. struct termios tios;
  27. if (tcgetattr(fd, &tios) < 0) {
  28. printk(UM_KERN_ERR "could not get initial terminal "
  29. "attributes\n");
  30. return -1;
  31. }
  32. tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL;
  33. tios.c_iflag = IGNBRK | IGNPAR;
  34. tios.c_oflag = 0;
  35. tios.c_lflag = 0;
  36. for (i = 0; i < NCCS; i++)
  37. tios.c_cc[i] = 0;
  38. tios.c_cc[VMIN] = 1;
  39. tios.c_cc[VTIME] = 0;
  40. cfsetospeed(&tios, B38400);
  41. cfsetispeed(&tios, B38400);
  42. if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
  43. printk(UM_KERN_ERR "failed to set terminal attributes\n");
  44. return -1;
  45. }
  46. return 0;
  47. }
  48. struct slip_pre_exec_data {
  49. int stdin_fd;
  50. int stdout_fd;
  51. int close_me;
  52. };
  53. static void slip_pre_exec(void *arg)
  54. {
  55. struct slip_pre_exec_data *data = arg;
  56. if (data->stdin_fd >= 0)
  57. dup2(data->stdin_fd, 0);
  58. dup2(data->stdout_fd, 1);
  59. if (data->close_me >= 0)
  60. close(data->close_me);
  61. }
  62. static int slip_tramp(char **argv, int fd)
  63. {
  64. struct slip_pre_exec_data pe_data;
  65. char *output;
  66. int pid, fds[2], err, output_len;
  67. err = os_pipe(fds, 1, 0);
  68. if (err < 0) {
  69. printk(UM_KERN_ERR "slip_tramp : pipe failed, err = %d\n",
  70. -err);
  71. goto out;
  72. }
  73. err = 0;
  74. pe_data.stdin_fd = fd;
  75. pe_data.stdout_fd = fds[1];
  76. pe_data.close_me = fds[0];
  77. err = run_helper(slip_pre_exec, &pe_data, argv);
  78. if (err < 0)
  79. goto out_close;
  80. pid = err;
  81. output_len = UM_KERN_PAGE_SIZE;
  82. output = uml_kmalloc(output_len, UM_GFP_KERNEL);
  83. if (output == NULL) {
  84. printk(UM_KERN_ERR "slip_tramp : failed to allocate output "
  85. "buffer\n");
  86. os_kill_process(pid, 1);
  87. err = -ENOMEM;
  88. goto out_close;
  89. }
  90. close(fds[1]);
  91. read_output(fds[0], output, output_len);
  92. printk("%s", output);
  93. err = helper_wait(pid);
  94. close(fds[0]);
  95. kfree(output);
  96. return err;
  97. out_close:
  98. close(fds[0]);
  99. close(fds[1]);
  100. out:
  101. return err;
  102. }
  103. static int slip_open(void *data)
  104. {
  105. struct slip_data *pri = data;
  106. char version_buf[sizeof("nnnnn\0")];
  107. char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")];
  108. char *argv[] = { "uml_net", version_buf, "slip", "up", gate_buf,
  109. NULL };
  110. int sfd, mfd, err;
  111. err = get_pty();
  112. if (err < 0) {
  113. printk(UM_KERN_ERR "slip-open : Failed to open pty, err = %d\n",
  114. -err);
  115. goto out;
  116. }
  117. mfd = err;
  118. err = open(ptsname(mfd), O_RDWR, 0);
  119. if (err < 0) {
  120. printk(UM_KERN_ERR "Couldn't open tty for slip line, "
  121. "err = %d\n", -err);
  122. goto out_close;
  123. }
  124. sfd = err;
  125. if (set_up_tty(sfd))
  126. goto out_close2;
  127. pri->slave = sfd;
  128. pri->slip.pos = 0;
  129. pri->slip.esc = 0;
  130. if (pri->gate_addr != NULL) {
  131. sprintf(version_buf, "%d", UML_NET_VERSION);
  132. strcpy(gate_buf, pri->gate_addr);
  133. err = slip_tramp(argv, sfd);
  134. if (err < 0) {
  135. printk(UM_KERN_ERR "slip_tramp failed - err = %d\n",
  136. -err);
  137. goto out_close2;
  138. }
  139. err = os_get_ifname(pri->slave, pri->name);
  140. if (err < 0) {
  141. printk(UM_KERN_ERR "get_ifname failed, err = %d\n",
  142. -err);
  143. goto out_close2;
  144. }
  145. iter_addresses(pri->dev, open_addr, pri->name);
  146. }
  147. else {
  148. err = os_set_slip(sfd);
  149. if (err < 0) {
  150. printk(UM_KERN_ERR "Failed to set slip discipline "
  151. "encapsulation - err = %d\n", -err);
  152. goto out_close2;
  153. }
  154. }
  155. return mfd;
  156. out_close2:
  157. close(sfd);
  158. out_close:
  159. close(mfd);
  160. out:
  161. return err;
  162. }
  163. static void slip_close(int fd, void *data)
  164. {
  165. struct slip_data *pri = data;
  166. char version_buf[sizeof("nnnnn\0")];
  167. char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name,
  168. NULL };
  169. int err;
  170. if (pri->gate_addr != NULL)
  171. iter_addresses(pri->dev, close_addr, pri->name);
  172. sprintf(version_buf, "%d", UML_NET_VERSION);
  173. err = slip_tramp(argv, pri->slave);
  174. if (err != 0)
  175. printk(UM_KERN_ERR "slip_tramp failed - errno = %d\n", -err);
  176. close(fd);
  177. close(pri->slave);
  178. pri->slave = -1;
  179. }
  180. int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
  181. {
  182. return slip_proto_read(fd, buf, len, &pri->slip);
  183. }
  184. int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
  185. {
  186. return slip_proto_write(fd, buf, len, &pri->slip);
  187. }
  188. static void slip_add_addr(unsigned char *addr, unsigned char *netmask,
  189. void *data)
  190. {
  191. struct slip_data *pri = data;
  192. if (pri->slave < 0)
  193. return;
  194. open_addr(addr, netmask, pri->name);
  195. }
  196. static void slip_del_addr(unsigned char *addr, unsigned char *netmask,
  197. void *data)
  198. {
  199. struct slip_data *pri = data;
  200. if (pri->slave < 0)
  201. return;
  202. close_addr(addr, netmask, pri->name);
  203. }
  204. const struct net_user_info slip_user_info = {
  205. .init = slip_user_init,
  206. .open = slip_open,
  207. .close = slip_close,
  208. .remove = NULL,
  209. .add_address = slip_add_addr,
  210. .delete_address = slip_del_addr,
  211. .mtu = BUF_SIZE,
  212. .max_packet = BUF_SIZE,
  213. };