slip_user.c 5.0 KB

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