mcast_user.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "mcast.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. sin->sin_addr.s_addr = in_aton(addr);
  33. sin->sin_port = htons(port);
  34. return sin;
  35. }
  36. static int mcast_user_init(void *data, void *dev)
  37. {
  38. struct mcast_data *pri = data;
  39. pri->mcast_addr = new_addr(pri->addr, pri->port);
  40. pri->dev = dev;
  41. return 0;
  42. }
  43. static void mcast_remove(void *data)
  44. {
  45. struct mcast_data *pri = data;
  46. kfree(pri->mcast_addr);
  47. pri->mcast_addr = NULL;
  48. }
  49. static int mcast_open(void *data)
  50. {
  51. struct mcast_data *pri = data;
  52. struct sockaddr_in *sin = pri->mcast_addr;
  53. struct ip_mreq mreq;
  54. int fd, yes = 1, err = -EINVAL;
  55. if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
  56. goto out;
  57. fd = socket(AF_INET, SOCK_DGRAM, 0);
  58. if (fd < 0) {
  59. err = -errno;
  60. printk(UM_KERN_ERR "mcast_open : data socket failed, "
  61. "errno = %d\n", errno);
  62. goto out;
  63. }
  64. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
  65. err = -errno;
  66. printk(UM_KERN_ERR "mcast_open: SO_REUSEADDR failed, "
  67. "errno = %d\n", errno);
  68. goto out_close;
  69. }
  70. /* set ttl according to config */
  71. if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
  72. sizeof(pri->ttl)) < 0) {
  73. err = -errno;
  74. printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_TTL failed, "
  75. "error = %d\n", errno);
  76. goto out_close;
  77. }
  78. /* set LOOP, so data does get fed back to local sockets */
  79. if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
  80. err = -errno;
  81. printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_LOOP failed, "
  82. "error = %d\n", errno);
  83. goto out_close;
  84. }
  85. /* bind socket to mcast address */
  86. if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
  87. err = -errno;
  88. printk(UM_KERN_ERR "mcast_open : data bind failed, "
  89. "errno = %d\n", errno);
  90. goto out_close;
  91. }
  92. /* subscribe to the multicast group */
  93. mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
  94. mreq.imr_interface.s_addr = 0;
  95. if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
  96. &mreq, sizeof(mreq)) < 0) {
  97. err = -errno;
  98. printk(UM_KERN_ERR "mcast_open: IP_ADD_MEMBERSHIP failed, "
  99. "error = %d\n", errno);
  100. printk(UM_KERN_ERR "There appears not to be a multicast-"
  101. "capable network interface on the host.\n");
  102. printk(UM_KERN_ERR "eth0 should be configured in order to use "
  103. "the multicast transport.\n");
  104. goto out_close;
  105. }
  106. return fd;
  107. out_close:
  108. close(fd);
  109. out:
  110. return err;
  111. }
  112. static void mcast_close(int fd, void *data)
  113. {
  114. struct ip_mreq mreq;
  115. struct mcast_data *pri = data;
  116. struct sockaddr_in *sin = pri->mcast_addr;
  117. mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
  118. mreq.imr_interface.s_addr = 0;
  119. if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
  120. &mreq, sizeof(mreq)) < 0) {
  121. printk(UM_KERN_ERR "mcast_open: IP_DROP_MEMBERSHIP failed, "
  122. "error = %d\n", errno);
  123. }
  124. close(fd);
  125. }
  126. int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
  127. {
  128. struct sockaddr_in *data_addr = pri->mcast_addr;
  129. return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
  130. }
  131. const struct net_user_info mcast_user_info = {
  132. .init = mcast_user_init,
  133. .open = mcast_open,
  134. .close = mcast_close,
  135. .remove = mcast_remove,
  136. .add_address = NULL,
  137. .delete_address = NULL,
  138. .mtu = ETH_MAX_PACKET,
  139. .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
  140. };