umcast_user.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * user-mode-linux networking multicast transport
  3. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
  5. *
  6. * based on the existing uml-networking code, which is
  7. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
  8. * James Leu (jleu@mindspring.net).
  9. * Copyright (C) 2001 by various other people who didn't put their name here.
  10. *
  11. * Licensed under the GPL.
  12. *
  13. */
  14. #include <unistd.h>
  15. #include <errno.h>
  16. #include <netinet/in.h>
  17. #include "kern_constants.h"
  18. #include "umcast.h"
  19. #include "net_user.h"
  20. #include "um_malloc.h"
  21. #include "user.h"
  22. static struct sockaddr_in *new_addr(char *addr, unsigned short port)
  23. {
  24. struct sockaddr_in *sin;
  25. sin = uml_kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL);
  26. if (sin == NULL) {
  27. printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in "
  28. "failed\n");
  29. return NULL;
  30. }
  31. sin->sin_family = AF_INET;
  32. if (addr)
  33. sin->sin_addr.s_addr = in_aton(addr);
  34. else
  35. sin->sin_addr.s_addr = INADDR_ANY;
  36. sin->sin_port = htons(port);
  37. return sin;
  38. }
  39. static int umcast_user_init(void *data, void *dev)
  40. {
  41. struct umcast_data *pri = data;
  42. pri->remote_addr = new_addr(pri->addr, pri->rport);
  43. if (pri->unicast)
  44. pri->listen_addr = new_addr(NULL, pri->lport);
  45. else
  46. pri->listen_addr = pri->remote_addr;
  47. pri->dev = dev;
  48. return 0;
  49. }
  50. static void umcast_remove(void *data)
  51. {
  52. struct umcast_data *pri = data;
  53. kfree(pri->listen_addr);
  54. if (pri->unicast)
  55. kfree(pri->remote_addr);
  56. pri->listen_addr = pri->remote_addr = NULL;
  57. }
  58. static int umcast_open(void *data)
  59. {
  60. struct umcast_data *pri = data;
  61. struct sockaddr_in *lsin = pri->listen_addr;
  62. struct sockaddr_in *rsin = pri->remote_addr;
  63. struct ip_mreq mreq;
  64. int fd, yes = 1, err = -EINVAL;
  65. if ((!pri->unicast && lsin->sin_addr.s_addr == 0) ||
  66. (rsin->sin_addr.s_addr == 0) ||
  67. (lsin->sin_port == 0) || (rsin->sin_port == 0))
  68. goto out;
  69. fd = socket(AF_INET, SOCK_DGRAM, 0);
  70. if (fd < 0) {
  71. err = -errno;
  72. printk(UM_KERN_ERR "umcast_open : data socket failed, "
  73. "errno = %d\n", errno);
  74. goto out;
  75. }
  76. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
  77. err = -errno;
  78. printk(UM_KERN_ERR "umcast_open: SO_REUSEADDR failed, "
  79. "errno = %d\n", errno);
  80. goto out_close;
  81. }
  82. if (!pri->unicast) {
  83. /* set ttl according to config */
  84. if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
  85. sizeof(pri->ttl)) < 0) {
  86. err = -errno;
  87. printk(UM_KERN_ERR "umcast_open: IP_MULTICAST_TTL "
  88. "failed, error = %d\n", errno);
  89. goto out_close;
  90. }
  91. /* set LOOP, so data does get fed back to local sockets */
  92. if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP,
  93. &yes, sizeof(yes)) < 0) {
  94. err = -errno;
  95. printk(UM_KERN_ERR "umcast_open: IP_MULTICAST_LOOP "
  96. "failed, error = %d\n", errno);
  97. goto out_close;
  98. }
  99. }
  100. /* bind socket to the address */
  101. if (bind(fd, (struct sockaddr *) lsin, sizeof(*lsin)) < 0) {
  102. err = -errno;
  103. printk(UM_KERN_ERR "umcast_open : data bind failed, "
  104. "errno = %d\n", errno);
  105. goto out_close;
  106. }
  107. if (!pri->unicast) {
  108. /* subscribe to the multicast group */
  109. mreq.imr_multiaddr.s_addr = lsin->sin_addr.s_addr;
  110. mreq.imr_interface.s_addr = 0;
  111. if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
  112. &mreq, sizeof(mreq)) < 0) {
  113. err = -errno;
  114. printk(UM_KERN_ERR "umcast_open: IP_ADD_MEMBERSHIP "
  115. "failed, error = %d\n", errno);
  116. printk(UM_KERN_ERR "There appears not to be a "
  117. "multicast-capable network interface on the "
  118. "host.\n");
  119. printk(UM_KERN_ERR "eth0 should be configured in order "
  120. "to use the multicast transport.\n");
  121. goto out_close;
  122. }
  123. }
  124. return fd;
  125. out_close:
  126. close(fd);
  127. out:
  128. return err;
  129. }
  130. static void umcast_close(int fd, void *data)
  131. {
  132. struct umcast_data *pri = data;
  133. if (!pri->unicast) {
  134. struct ip_mreq mreq;
  135. struct sockaddr_in *lsin = pri->listen_addr;
  136. mreq.imr_multiaddr.s_addr = lsin->sin_addr.s_addr;
  137. mreq.imr_interface.s_addr = 0;
  138. if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
  139. &mreq, sizeof(mreq)) < 0) {
  140. printk(UM_KERN_ERR "umcast_close: IP_DROP_MEMBERSHIP "
  141. "failed, error = %d\n", errno);
  142. }
  143. }
  144. close(fd);
  145. }
  146. int umcast_user_write(int fd, void *buf, int len, struct umcast_data *pri)
  147. {
  148. struct sockaddr_in *data_addr = pri->remote_addr;
  149. return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
  150. }
  151. const struct net_user_info umcast_user_info = {
  152. .init = umcast_user_init,
  153. .open = umcast_open,
  154. .close = umcast_close,
  155. .remove = umcast_remove,
  156. .add_address = NULL,
  157. .delete_address = NULL,
  158. .mtu = ETH_MAX_PACKET,
  159. .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
  160. };