umcast_user.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "umcast.h"
  18. #include <net_user.h>
  19. #include <um_malloc.h>
  20. static struct sockaddr_in *new_addr(char *addr, unsigned short port)
  21. {
  22. struct sockaddr_in *sin;
  23. sin = uml_kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL);
  24. if (sin == NULL) {
  25. printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in "
  26. "failed\n");
  27. return NULL;
  28. }
  29. sin->sin_family = AF_INET;
  30. if (addr)
  31. sin->sin_addr.s_addr = in_aton(addr);
  32. else
  33. sin->sin_addr.s_addr = INADDR_ANY;
  34. sin->sin_port = htons(port);
  35. return sin;
  36. }
  37. static int umcast_user_init(void *data, void *dev)
  38. {
  39. struct umcast_data *pri = data;
  40. pri->remote_addr = new_addr(pri->addr, pri->rport);
  41. if (pri->unicast)
  42. pri->listen_addr = new_addr(NULL, pri->lport);
  43. else
  44. pri->listen_addr = pri->remote_addr;
  45. pri->dev = dev;
  46. return 0;
  47. }
  48. static void umcast_remove(void *data)
  49. {
  50. struct umcast_data *pri = data;
  51. kfree(pri->listen_addr);
  52. if (pri->unicast)
  53. kfree(pri->remote_addr);
  54. pri->listen_addr = pri->remote_addr = NULL;
  55. }
  56. static int umcast_open(void *data)
  57. {
  58. struct umcast_data *pri = data;
  59. struct sockaddr_in *lsin = pri->listen_addr;
  60. struct sockaddr_in *rsin = pri->remote_addr;
  61. struct ip_mreq mreq;
  62. int fd, yes = 1, err = -EINVAL;
  63. if ((!pri->unicast && lsin->sin_addr.s_addr == 0) ||
  64. (rsin->sin_addr.s_addr == 0) ||
  65. (lsin->sin_port == 0) || (rsin->sin_port == 0))
  66. goto out;
  67. fd = socket(AF_INET, SOCK_DGRAM, 0);
  68. if (fd < 0) {
  69. err = -errno;
  70. printk(UM_KERN_ERR "umcast_open : data socket failed, "
  71. "errno = %d\n", errno);
  72. goto out;
  73. }
  74. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
  75. err = -errno;
  76. printk(UM_KERN_ERR "umcast_open: SO_REUSEADDR failed, "
  77. "errno = %d\n", errno);
  78. goto out_close;
  79. }
  80. if (!pri->unicast) {
  81. /* set ttl according to config */
  82. if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
  83. sizeof(pri->ttl)) < 0) {
  84. err = -errno;
  85. printk(UM_KERN_ERR "umcast_open: IP_MULTICAST_TTL "
  86. "failed, error = %d\n", errno);
  87. goto out_close;
  88. }
  89. /* set LOOP, so data does get fed back to local sockets */
  90. if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP,
  91. &yes, sizeof(yes)) < 0) {
  92. err = -errno;
  93. printk(UM_KERN_ERR "umcast_open: IP_MULTICAST_LOOP "
  94. "failed, error = %d\n", errno);
  95. goto out_close;
  96. }
  97. }
  98. /* bind socket to the address */
  99. if (bind(fd, (struct sockaddr *) lsin, sizeof(*lsin)) < 0) {
  100. err = -errno;
  101. printk(UM_KERN_ERR "umcast_open : data bind failed, "
  102. "errno = %d\n", errno);
  103. goto out_close;
  104. }
  105. if (!pri->unicast) {
  106. /* subscribe to the multicast group */
  107. mreq.imr_multiaddr.s_addr = lsin->sin_addr.s_addr;
  108. mreq.imr_interface.s_addr = 0;
  109. if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
  110. &mreq, sizeof(mreq)) < 0) {
  111. err = -errno;
  112. printk(UM_KERN_ERR "umcast_open: IP_ADD_MEMBERSHIP "
  113. "failed, error = %d\n", errno);
  114. printk(UM_KERN_ERR "There appears not to be a "
  115. "multicast-capable network interface on the "
  116. "host.\n");
  117. printk(UM_KERN_ERR "eth0 should be configured in order "
  118. "to use the multicast transport.\n");
  119. goto out_close;
  120. }
  121. }
  122. return fd;
  123. out_close:
  124. close(fd);
  125. out:
  126. return err;
  127. }
  128. static void umcast_close(int fd, void *data)
  129. {
  130. struct umcast_data *pri = data;
  131. if (!pri->unicast) {
  132. struct ip_mreq mreq;
  133. struct sockaddr_in *lsin = pri->listen_addr;
  134. mreq.imr_multiaddr.s_addr = lsin->sin_addr.s_addr;
  135. mreq.imr_interface.s_addr = 0;
  136. if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
  137. &mreq, sizeof(mreq)) < 0) {
  138. printk(UM_KERN_ERR "umcast_close: IP_DROP_MEMBERSHIP "
  139. "failed, error = %d\n", errno);
  140. }
  141. }
  142. close(fd);
  143. }
  144. int umcast_user_write(int fd, void *buf, int len, struct umcast_data *pri)
  145. {
  146. struct sockaddr_in *data_addr = pri->remote_addr;
  147. return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
  148. }
  149. const struct net_user_info umcast_user_info = {
  150. .init = umcast_user_init,
  151. .open = umcast_open,
  152. .close = umcast_close,
  153. .remove = umcast_remove,
  154. .add_address = NULL,
  155. .delete_address = NULL,
  156. .mtu = ETH_MAX_PACKET,
  157. .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
  158. };