igmp_send.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 IGMPBUF ((struct igmp_iphdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  68. /****************************************************************************
  69. * Private Functions
  70. ****************************************************************************/
  71. static uint16_t igmp_chksum(FAR uint8_t *buffer, int buflen)
  72. {
  73. uint16_t sum = net_chksum((FAR uint16_t *)buffer, buflen);
  74. return sum ? sum : 0xffff;
  75. }
  76. /****************************************************************************
  77. * Public Functions
  78. ****************************************************************************/
  79. /****************************************************************************
  80. * Name: igmp_send
  81. *
  82. * Description:
  83. * Sends an IGMP IP packet on a network interface. This function constructs
  84. * the IP header and calculates the IP header checksum.
  85. *
  86. * Input Parameters:
  87. * dev - The device driver structure to use in the send operation.
  88. * group - Describes the multicast group member and identifies the
  89. * message to be sent.
  90. * destipaddr - The IP address of the recipient of the message
  91. * msgid - ID of message to send
  92. *
  93. * Returned Value:
  94. * None
  95. *
  96. * Assumptions:
  97. * The network is locked.
  98. *
  99. ****************************************************************************/
  100. void igmp_send(FAR struct net_driver_s *dev, FAR struct igmp_group_s *group,
  101. FAR in_addr_t *destipaddr, uint8_t msgid)
  102. {
  103. ninfo("msgid: %02x destipaddr: %08x\n", msgid, (int)*destipaddr);
  104. /* The total length to send is the size of the IP and IGMP headers and 4
  105. * bytes for the ROUTER ALERT (and, eventually, the Ethernet header)
  106. */
  107. dev->d_len = IPIGMP_HDRLEN;
  108. /* The total size of the data is the size of the IGMP header */
  109. dev->d_sndlen = IGMP_HDRLEN;
  110. /* Add the router alert option (RFC 2113) */
  111. IGMPBUF->ra[0] = HTONS(IPOPT_RA >> 16);
  112. IGMPBUF->ra[1] = HTONS(IPOPT_RA & 0xffff);
  113. /* Initialize the IPv4 header */
  114. IGMPBUF->vhl = 0x46; /* 4->IP; 6->24 bytes */
  115. IGMPBUF->tos = 0;
  116. IGMPBUF->len[0] = (dev->d_len >> 8);
  117. IGMPBUF->len[1] = (dev->d_len & 0xff);
  118. ++g_ipid;
  119. IGMPBUF->ipid[0] = g_ipid >> 8;
  120. IGMPBUF->ipid[1] = g_ipid & 0xff;
  121. IGMPBUF->ipoffset[0] = IP_FLAG_DONTFRAG >> 8;
  122. IGMPBUF->ipoffset[1] = IP_FLAG_DONTFRAG & 0xff;
  123. IGMPBUF->ttl = IGMP_TTL;
  124. IGMPBUF->proto = IP_PROTO_IGMP;
  125. net_ipv4addr_hdrcopy(IGMPBUF->srcipaddr, &dev->d_ipaddr);
  126. net_ipv4addr_hdrcopy(IGMPBUF->destipaddr, destipaddr);
  127. /* Calculate IP checksum. */
  128. IGMPBUF->ipchksum = 0;
  129. IGMPBUF->ipchksum = ~igmp_chksum((FAR uint8_t *)IGMPBUF, IPv4_HDRLEN + RASIZE);
  130. /* Set up the IGMP message */
  131. IGMPBUF->type = msgid;
  132. IGMPBUF->maxresp = 0;
  133. net_ipv4addr_hdrcopy(IGMPBUF->grpaddr, &group->grpaddr);
  134. /* Calculate the IGMP checksum. */
  135. IGMPBUF->chksum = 0;
  136. IGMPBUF->chksum = ~igmp_chksum(&IGMPBUF->type, IGMP_HDRLEN);
  137. IGMP_STATINCR(g_netstats.igmp.poll_send);
  138. IGMP_STATINCR(g_netstats.ipv4.sent);
  139. ninfo("Outgoing IGMP packet length: %d (%d)\n",
  140. dev->d_len, (IGMPBUF->len[0] << 8) | IGMPBUF->len[1]);
  141. igmp_dumppkt(RA, IPIGMP_HDRLEN + RASIZE);
  142. }
  143. #endif /* CONFIG_NET_IGMP */