mld_send.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /****************************************************************************
  2. * net/mld/mld_send.c
  3. *
  4. * Copyright (C) 2018 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 <string.h>
  40. #include <debug.h>
  41. #include <arpa/inet.h>
  42. #include <nuttx/net/netconfig.h>
  43. #include <nuttx/net/netdev.h>
  44. #include <nuttx/net/netstats.h>
  45. #include <nuttx/net/ip.h>
  46. #include <nuttx/net/ipv6ext.h>
  47. #include <nuttx/net/tcp.h>
  48. #include <nuttx/net/mld.h>
  49. #include "devif/devif.h"
  50. #include "inet/inet.h"
  51. #include "utils/utils.h"
  52. #include "mld/mld.h"
  53. /****************************************************************************
  54. * Pre-processor Definitions
  55. ****************************************************************************/
  56. /* Debug */
  57. #ifdef CONFIG_NET_MLD_TXDUMP
  58. # define mld_dumppkt(b,n) lib_dumpbuffer("MLD", (FAR const uint8_t*)(b), (n))
  59. #else
  60. # define mld_dumppkt(b,n)
  61. #endif
  62. /* IPv6 header size with extensions */
  63. #define RASIZE sizeof(struct ipv6_router_alert_s)
  64. #define MLD_HDRLEN (IPv6_HDRLEN + RASIZE)
  65. /* Buffer layout */
  66. #define IPv6BUF ((FAR struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  67. #define RABUF ((FAR struct ipv6_router_alert_s *) \
  68. (&dev->d_buf[NET_LL_HDRLEN(dev)] + IPv6_HDRLEN))
  69. #define QUERYBUF ((FAR struct mld_mcast_listen_query_s *) \
  70. (&dev->d_buf[NET_LL_HDRLEN(dev)] + MLD_HDRLEN))
  71. #define V1REPORTBUF ((FAR struct mld_mcast_listen_report_v1_s *) \
  72. (&dev->d_buf[NET_LL_HDRLEN(dev)] + MLD_HDRLEN))
  73. #define V2REPORTBUF ((FAR struct mld_mcast_listen_report_v2_s *) \
  74. (&dev->d_buf[NET_LL_HDRLEN(dev)] + MLD_HDRLEN))
  75. #define DONEBUF ((FAR struct mld_mcast_listen_done_s *) \
  76. (&dev->d_buf[NET_LL_HDRLEN(dev)] + MLD_HDRLEN))
  77. /****************************************************************************
  78. * Public Functions
  79. ****************************************************************************/
  80. /****************************************************************************
  81. * Name: mld_send
  82. *
  83. * Description:
  84. * Sends an MLD 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. * msgtype - The type of the message to be sent (see enum mld_msgtype_e)
  92. *
  93. * Returned Value:
  94. * None
  95. *
  96. * Assumptions:
  97. * The network is locked.
  98. *
  99. ****************************************************************************/
  100. void mld_send(FAR struct net_driver_s *dev, FAR struct mld_group_s *group,
  101. uint8_t msgtype)
  102. {
  103. FAR struct ipv6_hdr_s *ipv6;
  104. FAR struct ipv6_router_alert_s *ra;
  105. FAR const uint16_t *destipaddr;
  106. unsigned int mldsize;
  107. /* Only a general query message can have a NULL group */
  108. DEBUGASSERT(dev != NULL);
  109. DEBUGASSERT(msgtype == MLD_SEND_GENQUERY || group != NULL);
  110. /* Select IPv6 */
  111. IFF_SET_IPv6(dev->d_flags);
  112. /* What is the size of the ICMPv6 payload? */
  113. switch (msgtype)
  114. {
  115. case MLD_SEND_GENQUERY: /* Send General Query */
  116. case MLD_SEND_MASQUERY: /* Send Multicast Address Specific (MAS) Query */
  117. {
  118. mldinfo("Send General/MAS Query, flags=%02x\n",
  119. group != NULL ? group->flags : dev->d_mld.flags);
  120. mldsize = SIZEOF_MLD_MCAST_LISTEN_QUERY_S(0);
  121. }
  122. break;
  123. case MLD_SEND_V1REPORT: /* Send MLDv1 Report message */
  124. {
  125. mldinfo("Send MLDv1 Report, flags=%02x\n", group->flags);
  126. mldsize = sizeof(struct mld_mcast_listen_report_v1_s);
  127. }
  128. break;
  129. case MLD_SEND_V2REPORT: /* Send MLDv2 Report message */
  130. {
  131. unsigned int addreclen;
  132. mldinfo("Send MLDv2 Report, flags=%02x\n", group->flags);
  133. addreclen = SIZEOF_MLD_MCAST_ADDREC_V2_S(0, 0);
  134. mldsize = SIZEOF_MLD_MCAST_LISTEN_REPORT_V2_S(addreclen);
  135. }
  136. break;
  137. case MLD_SEND_DONE: /* Send Done message */
  138. {
  139. mldinfo("Send Done message, flags=%02x\n", group->flags);
  140. mldsize = sizeof(struct mld_mcast_listen_done_s);
  141. }
  142. break;
  143. default:
  144. {
  145. mlderr("Bad msgtype: %02x \n", msgtype);
  146. DEBUGPANIC();
  147. }
  148. return;
  149. }
  150. /* The total length to send is the size of the IPv6 header, 4 bytes for the
  151. * ROUTER ALERT, and the MLD ICMPv6 payload (and, eventually, the Ethernet
  152. * header length)
  153. */
  154. dev->d_len = MLD_HDRLEN + mldsize;
  155. /* The total size of the data is the size of the ICMPv6 payload PLUS the
  156. * size of the IPv6 extension headers.
  157. */
  158. dev->d_sndlen = RASIZE + mldsize;
  159. /* Set up the IPv6 header */
  160. ipv6 = IPv6BUF;
  161. ipv6->vtc = 0x60; /* Version/traffic class (MS) */
  162. ipv6->tcf = 0; /* Traffic class(LS)/Flow label(MS) */
  163. ipv6->flow = 0; /* Flow label (LS) */
  164. ipv6->len[0] = (dev->d_sndlen >> 8); /* Length excludes the IPv6 header */
  165. ipv6->len[1] = (dev->d_sndlen & 0xff); /* but includes the extension headers */
  166. ipv6->proto = NEXT_HOPBYBOT_EH; /* Hop-to-hop extension header */
  167. ipv6->ttl = MLD_TTL; /* MLD Time-to-live */
  168. /* Select the IPv6 source address (the local interface assigned to the
  169. * network device).
  170. */
  171. net_ipv6addr_hdrcopy(ipv6->srcipaddr, dev->d_ipv6addr);
  172. /* Select the IPv6 destination address. This varies with the type of message
  173. * being sent:
  174. *
  175. * MESSAGE DESTINATION ADDRESS
  176. * General Query Message: The link-local, all nodes multicast address.
  177. * MAS Query Messages: The group multicast address.
  178. * V1 Report Message: The group multicast address.
  179. * V2 Report Message: The link-local, all MLDv2 router multicast address.
  180. * Done Message: The link-local, all routers multicast address.
  181. */
  182. switch (msgtype)
  183. {
  184. case MLD_SEND_GENQUERY: /* Send General Query */
  185. destipaddr = g_ipv6_allnodes;
  186. break;
  187. case MLD_SEND_MASQUERY: /* Send Multicast Address Specific (MAS) Query */
  188. case MLD_SEND_V1REPORT: /* Send MLDv1 Report message */
  189. destipaddr = group->grpaddr;
  190. break;
  191. case MLD_SEND_V2REPORT: /* Send MLDv2 Report message */
  192. destipaddr = g_ipv6_allmldv2routers;
  193. break;
  194. case MLD_SEND_DONE: /* Send Done message */
  195. destipaddr = g_ipv6_allrouters;
  196. break;
  197. default: /* Can't happen, but eliminates a warning */
  198. return;
  199. }
  200. mldinfo("destipaddr: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  201. destipaddr[0], destipaddr[1], destipaddr[2], destipaddr[3],
  202. destipaddr[4], destipaddr[5], destipaddr[6], destipaddr[7]);
  203. net_ipv6addr_hdrcopy(ipv6->destipaddr, destipaddr);
  204. /* Add the router alert IP header option.
  205. *
  206. * The IPv6 router alert option (type 5) is defined in RFC 2711.
  207. */
  208. ra = RABUF;
  209. memset(ra, 0, RASIZE);
  210. ra->hbyh.nxthdr = IP_PROTO_ICMP6; /* ICMPv6 payload follows extension header */
  211. ra->hbyh.len = 1; /* One 8-octet option follows */
  212. ra->type = HOPBYHOP_ROUTER_ALERT; /* Router alert */
  213. /* Format the MLD ICMPv6 payload into place after the IPv6 header (with
  214. * router alert)
  215. */
  216. switch (msgtype)
  217. {
  218. case MLD_SEND_GENQUERY: /* Send General Query */
  219. case MLD_SEND_MASQUERY: /* Send Multicast Address Specific (MAS) Query */
  220. {
  221. FAR struct mld_mcast_listen_query_s *query = QUERYBUF;
  222. /* Initialize the Query payload. In a General Query, both the
  223. * Multicast Address field and the Number of Sources (N)
  224. * field are zero.
  225. *
  226. * Careful here. In MLDv1 compatibility mode, the MRC is not
  227. * encoded and must follow the rules for MLDv1.
  228. */
  229. memset(query, 0, sizeof(struct mld_mcast_listen_query_s));
  230. query->type = ICMPV6_MCAST_LISTEN_QUERY;
  231. query->mrc = MLD_QRESP_MSEC;
  232. /* The General Query and the MAS Query differ only in that the
  233. * setting of the group multicast address field. This field
  234. * is the unspecified address for General Query, but the group
  235. * multicast address for the MAS query.
  236. */
  237. if (msgtype == MLD_SEND_GENQUERY)
  238. {
  239. net_ipv6addr_hdrcopy(query->grpaddr, g_ipv6_unspecaddr);
  240. }
  241. else
  242. {
  243. net_ipv6addr_hdrcopy(query->grpaddr, group->grpaddr);
  244. }
  245. /* Fields unique to the extended MLDv2 query */
  246. if (!IS_MLD_V1COMPAT(dev->d_mld.flags))
  247. {
  248. query->flags = MLD_ROBUSTNESS;
  249. query->qqic = MLD_QRESP_SEC;
  250. }
  251. /* Calculate the ICMPv6 checksum. */
  252. query->chksum = 0;
  253. query->chksum = ~icmpv6_chksum(dev, MLD_HDRLEN);
  254. MLD_STATINCR(g_netstats.mld.query_sent);
  255. #ifdef CONFIG_NET_MLD_ROUTER
  256. /* Save the number of members that reported in the previous query
  257. * cycle; reset the number of members that have reported in the
  258. * new query cycle.
  259. */
  260. if (msgtype == MLD_SEND_GENQUERY)
  261. {
  262. /* Update accumulated membership for all groups. */
  263. mld_new_pollcycle(dev)
  264. }
  265. else
  266. {
  267. /* Updated accumulated membership only for this group */
  268. group->lstmbrs = group->members;
  269. group->members = 0;
  270. }
  271. #endif
  272. }
  273. break;
  274. case MLD_SEND_V1REPORT: /* Send MLDv1 Report message */
  275. {
  276. FAR struct mld_mcast_listen_report_v1_s *report = V1REPORTBUF;
  277. /* Initialize the Report payload */
  278. memset(report, 0, sizeof(struct mld_mcast_listen_report_v1_s));
  279. net_ipv6addr_hdrcopy(report->mcastaddr, &group->grpaddr);
  280. report->type = ICMPV6_MCAST_LISTEN_REPORT_V1;
  281. /* Calculate the ICMPv6 checksum. */
  282. report->chksum = 0;
  283. report->chksum = ~icmpv6_chksum(dev, MLD_HDRLEN);
  284. SET_MLD_LASTREPORT(group->flags); /* Remember we were the last to report */
  285. MLD_STATINCR(g_netstats.mld.v1report_sent);
  286. }
  287. break;
  288. case MLD_SEND_V2REPORT: /* Send MLDv2 Report message */
  289. {
  290. FAR struct mld_mcast_listen_report_v2_s *report = V2REPORTBUF;
  291. FAR struct mld_mcast_addrec_v2_s *addrec;
  292. /* Initialize the Report payload */
  293. memset(report, 0, mldsize);
  294. report->type = ICMPV6_MCAST_LISTEN_REPORT_V2;
  295. report->naddrec = HTONS(1);
  296. addrec = report->addrec;
  297. addrec->rectype = MODE_IS_INCLUDE;
  298. net_ipv6addr_hdrcopy(addrec->mcast, &group->grpaddr);
  299. /* Calculate the ICMPv6 checksum. */
  300. report->chksum = 0;
  301. report->chksum = ~icmpv6_chksum(dev, MLD_HDRLEN);
  302. SET_MLD_LASTREPORT(group->flags); /* Remember we were the last to report */
  303. MLD_STATINCR(g_netstats.mld.v2report_sent);
  304. }
  305. break;
  306. case MLD_SEND_DONE: /* Send Done message */
  307. {
  308. FAR struct mld_mcast_listen_done_s *done = DONEBUF;
  309. /* Initialize the Done payload */
  310. memset(done, 0, sizeof(struct mld_mcast_listen_done_s));
  311. done->type = ICMPV6_MCAST_LISTEN_DONE;
  312. net_ipv6addr_hdrcopy(done->mcastaddr, &group->grpaddr);
  313. /* Calculate the ICMPv6 checksum. */
  314. done->chksum = 0;
  315. done->chksum = ~icmpv6_chksum(dev, MLD_HDRLEN);
  316. MLD_STATINCR(g_netstats.mld.done_sent);
  317. }
  318. break;
  319. default: /* Can't happen, but eliminates a warning */
  320. return;
  321. }
  322. MLD_STATINCR(g_netstats.icmpv6.sent);
  323. MLD_STATINCR(g_netstats.ipv6.sent);
  324. mldinfo("Outgoing ICMPv6 MLD packet length: %d (%d)\n",
  325. dev->d_len, (ipv6->len[0] << 8) | ipv6->len[1]);
  326. mld_dumppkt((FAR const uint8_t *)IPv6BUF, MLD_HDRLEN + mldsize);
  327. }
  328. /****************************************************************************
  329. * Name: mld_report_msgtype
  330. *
  331. * Description:
  332. * Determine which type of Report to send, MLDv1 or MLDv2, depending on
  333. * current state of compatibility mode flag.
  334. *
  335. ****************************************************************************/
  336. uint8_t mld_report_msgtype(FAR struct net_driver_s *dev)
  337. {
  338. if (IS_MLD_V1COMPAT(dev->d_mld.flags))
  339. {
  340. return MLD_SEND_V1REPORT;
  341. }
  342. else
  343. {
  344. return MLD_SEND_V2REPORT;
  345. }
  346. }