igmp_send.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /****************************************************************************
  2. * net/igmp/igmp_send.c
  3. *
  4. * Copyright (C) 2010, 2015 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <debug.h>
  40. #include <arpa/inet.h>
  41. #include <nuttx/net/netconfig.h>
  42. #include <nuttx/net/netdev.h>
  43. #include <nuttx/net/netstats.h>
  44. #include <nuttx/net/ip.h>
  45. #include <nuttx/net/ipopt.h>
  46. #include <nuttx/net/tcp.h>
  47. #include <nuttx/net/igmp.h>
  48. #include "devif/devif.h"
  49. #include "inet/inet.h"
  50. #include "igmp/igmp.h"
  51. #ifdef CONFIG_NET_IGMP
  52. /****************************************************************************
  53. * Pre-processor Definitions
  54. ****************************************************************************/
  55. /* Debug */
  56. #undef IGMP_DUMPPKT /* Define to enable packet dump */
  57. #ifndef CONFIG_DEBUG_NET
  58. # undef IGMP_DUMPPKT
  59. #endif
  60. #ifdef IGMP_DUMPPKT
  61. # define igmp_dumppkt(b,n) lib_dumpbuffer("IGMP", (FAR const uint8_t*)(b), (n))
  62. #else
  63. # define igmp_dumppkt(b,n)
  64. #endif
  65. /* Buffer layout */
  66. #define RASIZE (4)
  67. #define IPv4BUF ((FAR struct igmp_iphdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  68. #define IGMPBUF(hl) ((FAR struct igmp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + (hl)])
  69. /****************************************************************************
  70. * Private Functions
  71. ****************************************************************************/
  72. static uint16_t igmp_chksum(FAR uint8_t *buffer, int buflen)
  73. {
  74. uint16_t sum = net_chksum((FAR uint16_t *)buffer, buflen);
  75. return sum ? sum : 0xffff;
  76. }
  77. /****************************************************************************
  78. * Public Functions
  79. ****************************************************************************/
  80. /****************************************************************************
  81. * Name: igmp_send
  82. *
  83. * Description:
  84. * Sends an IGMP IP packet on a network interface. This function constructs
  85. * the IP header and calculates the IP header checksum.
  86. *
  87. * Input Parameters:
  88. * dev - The device driver structure to use in the send operation.
  89. * group - Describes the multicast group member and identifies the
  90. * message to be sent.
  91. * destipaddr - The IP address of the recipient of the message
  92. * msgid - ID of message to send
  93. *
  94. * Returned Value:
  95. * None
  96. *
  97. * Assumptions:
  98. * The network is locked.
  99. *
  100. ****************************************************************************/
  101. void igmp_send(FAR struct net_driver_s *dev, FAR struct igmp_group_s *group,
  102. FAR in_addr_t *destipaddr, uint8_t msgid)
  103. {
  104. FAR struct igmp_iphdr_s *ipv4 = IPv4BUF;
  105. FAR struct igmp_hdr_s *igmp;
  106. uint16_t iphdrlen;
  107. ninfo("msgid: %02x destipaddr: %08x\n", msgid, (int)*destipaddr);
  108. /* The IGMP header immediately follows the IP header */
  109. iphdrlen = IPv4_HDRLEN + RASIZE;
  110. igmp = IGMPBUF(iphdrlen);
  111. /* The total length to send is the size of the IP and IGMP headers and 4
  112. * bytes for the ROUTER ALERT (and, eventually, the Ethernet header)
  113. */
  114. dev->d_len = iphdrlen + IGMP_HDRLEN;
  115. /* The total size of the data is the size of the IGMP header */
  116. dev->d_sndlen = IGMP_HDRLEN;
  117. /* Add the router alert option to the IPv4 header (RFC 2113) */
  118. ipv4->ra[0] = HTONS(IPOPT_RA >> 16);
  119. ipv4->ra[1] = HTONS(IPOPT_RA & 0xffff);
  120. /* Initialize the IPv4 header */
  121. ipv4->vhl = 0x46; /* 4->IP; 6->24 bytes */
  122. ipv4->tos = 0;
  123. ipv4->len[0] = (dev->d_len >> 8);
  124. ipv4->len[1] = (dev->d_len & 0xff);
  125. ++g_ipid;
  126. ipv4->ipid[0] = g_ipid >> 8;
  127. ipv4->ipid[1] = g_ipid & 0xff;
  128. ipv4->ipoffset[0] = IP_FLAG_DONTFRAG >> 8;
  129. ipv4->ipoffset[1] = IP_FLAG_DONTFRAG & 0xff;
  130. ipv4->ttl = IGMP_TTL;
  131. ipv4->proto = IP_PROTO_IGMP;
  132. net_ipv4addr_hdrcopy(ipv4->srcipaddr, &dev->d_ipaddr);
  133. net_ipv4addr_hdrcopy(ipv4->destipaddr, destipaddr);
  134. /* Calculate IP checksum. */
  135. ipv4->ipchksum = 0;
  136. ipv4->ipchksum = ~igmp_chksum((FAR uint8_t *)igmp, iphdrlen);
  137. /* Set up the IGMP message */
  138. igmp->type = msgid;
  139. igmp->maxresp = 0;
  140. net_ipv4addr_hdrcopy(igmp->grpaddr, &group->grpaddr);
  141. /* Calculate the IGMP checksum. */
  142. igmp->chksum = 0;
  143. igmp->chksum = ~igmp_chksum(&igmp->type, IGMP_HDRLEN);
  144. IGMP_STATINCR(g_netstats.igmp.poll_send);
  145. IGMP_STATINCR(g_netstats.ipv4.sent);
  146. ninfo("Outgoing IGMP packet length: %d (%d)\n",
  147. dev->d_len, (ipv4->len[0] << 8) | ipv4->len[1]);
  148. igmp_dumppkt(RA, iphdrlen + IGMP_HDRLEN);
  149. }
  150. #endif /* CONFIG_NET_IGMP */