daemon_user.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <stdint.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <sys/time.h>
  14. #include <sys/un.h>
  15. #include "daemon.h"
  16. #include "net_user.h"
  17. #include "os.h"
  18. #include "um_malloc.h"
  19. #include "user.h"
  20. enum request_type { REQ_NEW_CONTROL };
  21. #define SWITCH_MAGIC 0xfeedface
  22. struct request_v3 {
  23. uint32_t magic;
  24. uint32_t version;
  25. enum request_type type;
  26. struct sockaddr_un sock;
  27. };
  28. static struct sockaddr_un *new_addr(void *name, int len)
  29. {
  30. struct sockaddr_un *sun;
  31. sun = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
  32. if (sun == NULL) {
  33. printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
  34. "failed\n");
  35. return NULL;
  36. }
  37. sun->sun_family = AF_UNIX;
  38. memcpy(sun->sun_path, name, len);
  39. return sun;
  40. }
  41. static int connect_to_switch(struct daemon_data *pri)
  42. {
  43. struct sockaddr_un *ctl_addr = pri->ctl_addr;
  44. struct sockaddr_un *local_addr = pri->local_addr;
  45. struct sockaddr_un *sun;
  46. struct request_v3 req;
  47. int fd, n, err;
  48. pri->control = socket(AF_UNIX, SOCK_STREAM, 0);
  49. if (pri->control < 0) {
  50. err = -errno;
  51. printk(UM_KERN_ERR "daemon_open : control socket failed, "
  52. "errno = %d\n", -err);
  53. return err;
  54. }
  55. if (connect(pri->control, (struct sockaddr *) ctl_addr,
  56. sizeof(*ctl_addr)) < 0) {
  57. err = -errno;
  58. printk(UM_KERN_ERR "daemon_open : control connect failed, "
  59. "errno = %d\n", -err);
  60. goto out;
  61. }
  62. fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  63. if (fd < 0) {
  64. err = -errno;
  65. printk(UM_KERN_ERR "daemon_open : data socket failed, "
  66. "errno = %d\n", -err);
  67. goto out;
  68. }
  69. if (bind(fd, (struct sockaddr *) local_addr, sizeof(*local_addr)) < 0) {
  70. err = -errno;
  71. printk(UM_KERN_ERR "daemon_open : data bind failed, "
  72. "errno = %d\n", -err);
  73. goto out_close;
  74. }
  75. sun = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
  76. if (sun == NULL) {
  77. printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
  78. "failed\n");
  79. err = -ENOMEM;
  80. goto out_close;
  81. }
  82. req.magic = SWITCH_MAGIC;
  83. req.version = SWITCH_VERSION;
  84. req.type = REQ_NEW_CONTROL;
  85. req.sock = *local_addr;
  86. n = write(pri->control, &req, sizeof(req));
  87. if (n != sizeof(req)) {
  88. printk(UM_KERN_ERR "daemon_open : control setup request "
  89. "failed, err = %d\n", -errno);
  90. err = -ENOTCONN;
  91. goto out_free;
  92. }
  93. n = read(pri->control, sun, sizeof(*sun));
  94. if (n != sizeof(*sun)) {
  95. printk(UM_KERN_ERR "daemon_open : read of data socket failed, "
  96. "err = %d\n", -errno);
  97. err = -ENOTCONN;
  98. goto out_free;
  99. }
  100. pri->data_addr = sun;
  101. return fd;
  102. out_free:
  103. kfree(sun);
  104. out_close:
  105. close(fd);
  106. out:
  107. close(pri->control);
  108. return err;
  109. }
  110. static int daemon_user_init(void *data, void *dev)
  111. {
  112. struct daemon_data *pri = data;
  113. struct timeval tv;
  114. struct {
  115. char zero;
  116. int pid;
  117. int usecs;
  118. } name;
  119. if (!strcmp(pri->sock_type, "unix"))
  120. pri->ctl_addr = new_addr(pri->ctl_sock,
  121. strlen(pri->ctl_sock) + 1);
  122. name.zero = 0;
  123. name.pid = os_getpid();
  124. gettimeofday(&tv, NULL);
  125. name.usecs = tv.tv_usec;
  126. pri->local_addr = new_addr(&name, sizeof(name));
  127. pri->dev = dev;
  128. pri->fd = connect_to_switch(pri);
  129. if (pri->fd < 0) {
  130. kfree(pri->local_addr);
  131. pri->local_addr = NULL;
  132. return pri->fd;
  133. }
  134. return 0;
  135. }
  136. static int daemon_open(void *data)
  137. {
  138. struct daemon_data *pri = data;
  139. return pri->fd;
  140. }
  141. static void daemon_remove(void *data)
  142. {
  143. struct daemon_data *pri = data;
  144. close(pri->fd);
  145. pri->fd = -1;
  146. close(pri->control);
  147. pri->control = -1;
  148. kfree(pri->data_addr);
  149. pri->data_addr = NULL;
  150. kfree(pri->ctl_addr);
  151. pri->ctl_addr = NULL;
  152. kfree(pri->local_addr);
  153. pri->local_addr = NULL;
  154. }
  155. int daemon_user_write(int fd, void *buf, int len, struct daemon_data *pri)
  156. {
  157. struct sockaddr_un *data_addr = pri->data_addr;
  158. return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
  159. }
  160. const struct net_user_info daemon_user_info = {
  161. .init = daemon_user_init,
  162. .open = daemon_open,
  163. .close = NULL,
  164. .remove = daemon_remove,
  165. .add_address = NULL,
  166. .delete_address = NULL,
  167. .mtu = ETH_MAX_PACKET,
  168. .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
  169. };