daemon_user.c 4.2 KB

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