igmp_mcastmac.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /****************************************************************************
  2. * net/igmp/igmp_mcastmac.c
  3. *
  4. * Copyright (C) 2010 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * The NuttX implementation of IGMP was inspired by the IGMP add-on for the
  8. * lwIP TCP/IP stack by Steve Reynolds:
  9. *
  10. * Copyright (c) 2002 CITEL Technologies Ltd.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. Neither the name of CITEL Technologies Ltd nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <nuttx/net/netconfig.h>
  46. #include <nuttx/net/ip.h>
  47. #include <nuttx/net/igmp.h>
  48. #include "devif/devif.h"
  49. #include "igmp/igmp.h"
  50. #ifdef CONFIG_NET_IGMP
  51. /****************************************************************************
  52. * Private Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: igmp_mcastmac
  56. *
  57. * Description:
  58. * Given an IP address (in network order), create a IGMP multicast MAC
  59. * address.
  60. *
  61. ****************************************************************************/
  62. static void igmp_mcastmac(in_addr_t *ip, FAR uint8_t *mac)
  63. {
  64. /* This mapping is from the IETF IN RFC 1700 */
  65. mac[0] = 0x01;
  66. mac[1] = 0x00;
  67. mac[2] = 0x5e;
  68. mac[3] = ip4_addr2(*ip) & 0x7f;
  69. mac[4] = ip4_addr3(*ip);
  70. mac[5] = ip4_addr4(*ip);
  71. ninfo("IP: %08x -> MAC: %02x%02x%02x%02x%02x%02x\n",
  72. *ip, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  73. }
  74. /****************************************************************************
  75. * Public Functions
  76. ****************************************************************************/
  77. /****************************************************************************
  78. * Name: igmp_addmcastmac
  79. *
  80. * Description:
  81. * Add an IGMP MAC address to the device's MAC filter table.
  82. *
  83. ****************************************************************************/
  84. void igmp_addmcastmac(FAR struct net_driver_s *dev, FAR in_addr_t *ip)
  85. {
  86. uint8_t mcastmac[6];
  87. ninfo("Adding: IP %08x\n", *ip);
  88. if (dev->d_addmac)
  89. {
  90. igmp_mcastmac(ip, mcastmac);
  91. dev->d_addmac(dev, mcastmac);
  92. }
  93. }
  94. /****************************************************************************
  95. * Name: igmp_removemcastmac
  96. *
  97. * Description:
  98. * Remove an IGMP MAC address from the device's MAC filter table.
  99. *
  100. ****************************************************************************/
  101. void igmp_removemcastmac(FAR struct net_driver_s *dev, FAR in_addr_t *ip)
  102. {
  103. uint8_t mcastmac[6];
  104. ninfo("Removing: IP %08x\n", *ip);
  105. if (dev->d_rmmac)
  106. {
  107. igmp_mcastmac(ip, mcastmac);
  108. dev->d_rmmac(dev, mcastmac);
  109. }
  110. }
  111. #endif /* CONFIG_NET_IGMP */