mcast_user.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * user-mode-linux networking multicast transport
  3. * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
  4. *
  5. * based on the existing uml-networking code, which is
  6. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
  7. * James Leu (jleu@mindspring.net).
  8. * Copyright (C) 2001 by various other people who didn't put their name here.
  9. *
  10. * Licensed under the GPL.
  11. *
  12. */
  13. #include <errno.h>
  14. #include <unistd.h>
  15. #include <sys/socket.h>
  16. #include <sys/un.h>
  17. #include <sys/time.h>
  18. #include <netinet/in.h>
  19. #include "net_user.h"
  20. #include "mcast.h"
  21. #include "kern_util.h"
  22. #include "user_util.h"
  23. #include "user.h"
  24. #include "os.h"
  25. #include "um_malloc.h"
  26. #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
  27. static struct sockaddr_in *new_addr(char *addr, unsigned short port)
  28. {
  29. struct sockaddr_in *sin;
  30. sin = um_kmalloc(sizeof(struct sockaddr_in));
  31. if(sin == NULL){
  32. printk("new_addr: allocation of sockaddr_in failed\n");
  33. return(NULL);
  34. }
  35. sin->sin_family = AF_INET;
  36. sin->sin_addr.s_addr = in_aton(addr);
  37. sin->sin_port = htons(port);
  38. return(sin);
  39. }
  40. static void mcast_user_init(void *data, void *dev)
  41. {
  42. struct mcast_data *pri = data;
  43. pri->mcast_addr = new_addr(pri->addr, pri->port);
  44. pri->dev = dev;
  45. }
  46. static int mcast_open(void *data)
  47. {
  48. struct mcast_data *pri = data;
  49. struct sockaddr_in *sin = pri->mcast_addr;
  50. struct ip_mreq mreq;
  51. int fd, yes = 1, err = -EINVAL;
  52. if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
  53. goto out;
  54. fd = socket(AF_INET, SOCK_DGRAM, 0);
  55. if (fd < 0){
  56. err = -errno;
  57. printk("mcast_open : data socket failed, errno = %d\n",
  58. errno);
  59. goto out;
  60. }
  61. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
  62. err = -errno;
  63. printk("mcast_open: SO_REUSEADDR failed, errno = %d\n",
  64. errno);
  65. goto out_close;
  66. }
  67. /* set ttl according to config */
  68. if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
  69. sizeof(pri->ttl)) < 0) {
  70. err = -errno;
  71. printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n",
  72. errno);
  73. goto out_close;
  74. }
  75. /* set LOOP, so data does get fed back to local sockets */
  76. if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
  77. err = -errno;
  78. printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n",
  79. errno);
  80. goto out_close;
  81. }
  82. /* bind socket to mcast address */
  83. if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
  84. err = -errno;
  85. printk("mcast_open : data bind failed, errno = %d\n", errno);
  86. goto out_close;
  87. }
  88. /* subscribe to the multicast group */
  89. mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
  90. mreq.imr_interface.s_addr = 0;
  91. if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
  92. &mreq, sizeof(mreq)) < 0) {
  93. err = -errno;
  94. printk("mcast_open: IP_ADD_MEMBERSHIP failed, error = %d\n",
  95. errno);
  96. printk("There appears not to be a multicast-capable network "
  97. "interface on the host.\n");
  98. printk("eth0 should be configured in order to use the "
  99. "multicast transport.\n");
  100. goto out_close;
  101. }
  102. return fd;
  103. out_close:
  104. os_close_file(fd);
  105. out:
  106. return err;
  107. }
  108. static void mcast_close(int fd, void *data)
  109. {
  110. struct ip_mreq mreq;
  111. struct mcast_data *pri = data;
  112. struct sockaddr_in *sin = pri->mcast_addr;
  113. mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
  114. mreq.imr_interface.s_addr = 0;
  115. if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
  116. &mreq, sizeof(mreq)) < 0) {
  117. printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n",
  118. errno);
  119. }
  120. os_close_file(fd);
  121. }
  122. int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
  123. {
  124. struct sockaddr_in *data_addr = pri->mcast_addr;
  125. return(net_sendto(fd, buf, len, data_addr, sizeof(*data_addr)));
  126. }
  127. static int mcast_set_mtu(int mtu, void *data)
  128. {
  129. return(mtu);
  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 = NULL,
  136. .set_mtu = mcast_set_mtu,
  137. .add_address = NULL,
  138. .delete_address = NULL,
  139. .max_packet = MAX_PACKET - ETH_HEADER_OTHER
  140. };