mcast_user.c 3.8 KB

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