Ip6Mld.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /** @file
  2. Multicast Listener Discovery support routines.
  3. Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Ip6Impl.h"
  7. /**
  8. Create a IP6_MLD_GROUP list entry node and record to IP6 service binding data.
  9. @param[in, out] IpSb Points to IP6 service binding instance.
  10. @param[in] MulticastAddr The IPv6 multicast address to be recorded.
  11. @param[in] DelayTimer The maximum allowed delay before sending a responding
  12. report, in units of milliseconds.
  13. @return The created IP6_ML_GROUP list entry or NULL.
  14. **/
  15. IP6_MLD_GROUP *
  16. Ip6CreateMldEntry (
  17. IN OUT IP6_SERVICE *IpSb,
  18. IN EFI_IPv6_ADDRESS *MulticastAddr,
  19. IN UINT32 DelayTimer
  20. )
  21. {
  22. IP6_MLD_GROUP *Entry;
  23. NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
  24. ASSERT (MulticastAddr != NULL && IP6_IS_MULTICAST (MulticastAddr));
  25. Entry = AllocatePool (sizeof (IP6_MLD_GROUP));
  26. if (Entry != NULL) {
  27. Entry->RefCnt = 1;
  28. Entry->DelayTimer = DelayTimer;
  29. Entry->SendByUs = FALSE;
  30. IP6_COPY_ADDRESS (&Entry->Address, MulticastAddr);
  31. InsertTailList (&IpSb->MldCtrl.Groups, &Entry->Link);
  32. }
  33. return Entry;
  34. }
  35. /**
  36. Search a IP6_MLD_GROUP list entry node from a list array.
  37. @param[in] IpSb Points to IP6 service binding instance.
  38. @param[in] MulticastAddr The IPv6 multicast address to be searched.
  39. @return The found IP6_ML_GROUP list entry or NULL.
  40. **/
  41. IP6_MLD_GROUP *
  42. Ip6FindMldEntry (
  43. IN IP6_SERVICE *IpSb,
  44. IN EFI_IPv6_ADDRESS *MulticastAddr
  45. )
  46. {
  47. LIST_ENTRY *Entry;
  48. IP6_MLD_GROUP *Group;
  49. NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
  50. ASSERT (MulticastAddr != NULL && IP6_IS_MULTICAST (MulticastAddr));
  51. NET_LIST_FOR_EACH (Entry, &IpSb->MldCtrl.Groups) {
  52. Group = NET_LIST_USER_STRUCT (Entry, IP6_MLD_GROUP, Link);
  53. if (EFI_IP6_EQUAL (MulticastAddr, &Group->Address)) {
  54. return Group;
  55. }
  56. }
  57. return NULL;
  58. }
  59. /**
  60. Count the number of IP6 multicast groups that are mapped to the
  61. same MAC address. Several IP6 multicast address may be mapped to
  62. the same MAC address.
  63. @param[in] MldCtrl The MLD control block to search in.
  64. @param[in] Mac The MAC address to search.
  65. @return The number of the IP6 multicast group that mapped to the same
  66. multicast group Mac.
  67. **/
  68. INTN
  69. Ip6FindMac (
  70. IN IP6_MLD_SERVICE_DATA *MldCtrl,
  71. IN EFI_MAC_ADDRESS *Mac
  72. )
  73. {
  74. LIST_ENTRY *Entry;
  75. IP6_MLD_GROUP *Group;
  76. INTN Count;
  77. Count = 0;
  78. NET_LIST_FOR_EACH (Entry, &MldCtrl->Groups) {
  79. Group = NET_LIST_USER_STRUCT (Entry, IP6_MLD_GROUP, Link);
  80. if (NET_MAC_EQUAL (&Group->Mac, Mac, sizeof (EFI_MAC_ADDRESS))) {
  81. Count++;
  82. }
  83. }
  84. return Count;
  85. }
  86. /**
  87. Generate MLD report message and send it out to MulticastAddr.
  88. @param[in] IpSb The IP service to send the packet.
  89. @param[in] Interface The IP interface to send the packet.
  90. If NULL, a system interface will be selected.
  91. @param[in] MulticastAddr The specific IPv6 multicast address to which
  92. the message sender is listening.
  93. @retval EFI_OUT_OF_RESOURCES There are not sufficient resources to complete the
  94. operation.
  95. @retval EFI_SUCCESS The MLD report message was successfully sent out.
  96. **/
  97. EFI_STATUS
  98. Ip6SendMldReport (
  99. IN IP6_SERVICE *IpSb,
  100. IN IP6_INTERFACE *Interface OPTIONAL,
  101. IN EFI_IPv6_ADDRESS *MulticastAddr
  102. )
  103. {
  104. IP6_MLD_HEAD *MldHead;
  105. NET_BUF *Packet;
  106. EFI_IP6_HEADER Head;
  107. UINT16 PayloadLen;
  108. UINTN OptionLen;
  109. UINT8 *Options;
  110. EFI_STATUS Status;
  111. UINT16 HeadChecksum;
  112. UINT16 PseudoChecksum;
  113. NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
  114. ASSERT (MulticastAddr != NULL && IP6_IS_MULTICAST (MulticastAddr));
  115. //
  116. // Generate the packet to be sent
  117. // IPv6 basic header + Hop by Hop option + MLD message
  118. //
  119. OptionLen = 0;
  120. Status = Ip6FillHopByHop (NULL, &OptionLen, IP6_ICMP);
  121. ASSERT (Status == EFI_BUFFER_TOO_SMALL);
  122. PayloadLen = (UINT16)(OptionLen + sizeof (IP6_MLD_HEAD));
  123. Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32)PayloadLen);
  124. if (Packet == NULL) {
  125. return EFI_OUT_OF_RESOURCES;
  126. }
  127. //
  128. // Create the basic IPv6 header.
  129. // RFC3590: Use link-local address as source address if it is available,
  130. // otherwise use the unspecified address.
  131. //
  132. Head.FlowLabelL = 0;
  133. Head.FlowLabelH = 0;
  134. Head.PayloadLength = HTONS (PayloadLen);
  135. Head.NextHeader = IP6_HOP_BY_HOP;
  136. Head.HopLimit = 1;
  137. IP6_COPY_ADDRESS (&Head.DestinationAddress, MulticastAddr);
  138. //
  139. // If Link-Local address is not ready, we use unspecified address.
  140. //
  141. IP6_COPY_ADDRESS (&Head.SourceAddress, &IpSb->LinkLocalAddr);
  142. NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
  143. //
  144. // Fill a IPv6 Router Alert option in a Hop-by-Hop Options Header
  145. //
  146. Options = NetbufAllocSpace (Packet, (UINT32)OptionLen, FALSE);
  147. ASSERT (Options != NULL);
  148. Status = Ip6FillHopByHop (Options, &OptionLen, IP6_ICMP);
  149. if (EFI_ERROR (Status)) {
  150. NetbufFree (Packet);
  151. Packet = NULL;
  152. return Status;
  153. }
  154. //
  155. // Fill in MLD message - Report
  156. //
  157. MldHead = (IP6_MLD_HEAD *)NetbufAllocSpace (Packet, sizeof (IP6_MLD_HEAD), FALSE);
  158. ASSERT (MldHead != NULL);
  159. ZeroMem (MldHead, sizeof (IP6_MLD_HEAD));
  160. MldHead->Head.Type = ICMP_V6_LISTENER_REPORT;
  161. MldHead->Head.Code = 0;
  162. IP6_COPY_ADDRESS (&MldHead->Group, MulticastAddr);
  163. HeadChecksum = NetblockChecksum ((UINT8 *)MldHead, sizeof (IP6_MLD_HEAD));
  164. PseudoChecksum = NetIp6PseudoHeadChecksum (
  165. &Head.SourceAddress,
  166. &Head.DestinationAddress,
  167. IP6_ICMP,
  168. sizeof (IP6_MLD_HEAD)
  169. );
  170. MldHead->Head.Checksum = (UINT16) ~NetAddChecksum (HeadChecksum, PseudoChecksum);
  171. //
  172. // Transmit the packet
  173. //
  174. return Ip6Output (IpSb, Interface, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
  175. }
  176. /**
  177. Generate MLD Done message and send it out to MulticastAddr.
  178. @param[in] IpSb The IP service to send the packet.
  179. @param[in] MulticastAddr The specific IPv6 multicast address to which
  180. the message sender is ceasing to listen.
  181. @retval EFI_OUT_OF_RESOURCES There are not sufficient resources to complete the
  182. operation.
  183. @retval EFI_SUCCESS The MLD report message was successfully sent out.
  184. **/
  185. EFI_STATUS
  186. Ip6SendMldDone (
  187. IN IP6_SERVICE *IpSb,
  188. IN EFI_IPv6_ADDRESS *MulticastAddr
  189. )
  190. {
  191. IP6_MLD_HEAD *MldHead;
  192. NET_BUF *Packet;
  193. EFI_IP6_HEADER Head;
  194. UINT16 PayloadLen;
  195. UINTN OptionLen;
  196. UINT8 *Options;
  197. EFI_STATUS Status;
  198. EFI_IPv6_ADDRESS Destination;
  199. UINT16 HeadChecksum;
  200. UINT16 PseudoChecksum;
  201. NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
  202. ASSERT (MulticastAddr != NULL && IP6_IS_MULTICAST (MulticastAddr));
  203. //
  204. // Generate the packet to be sent
  205. // IPv6 basic header + Hop by Hop option + MLD message
  206. //
  207. OptionLen = 0;
  208. Status = Ip6FillHopByHop (NULL, &OptionLen, IP6_ICMP);
  209. ASSERT (Status == EFI_BUFFER_TOO_SMALL);
  210. PayloadLen = (UINT16)(OptionLen + sizeof (IP6_MLD_HEAD));
  211. Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32)PayloadLen);
  212. if (Packet == NULL) {
  213. return EFI_OUT_OF_RESOURCES;
  214. }
  215. //
  216. // Create the basic IPv6 header.
  217. //
  218. Head.FlowLabelL = 0;
  219. Head.FlowLabelH = 0;
  220. Head.PayloadLength = HTONS (PayloadLen);
  221. Head.NextHeader = IP6_HOP_BY_HOP;
  222. Head.HopLimit = 1;
  223. //
  224. // If Link-Local address is not ready, we use unspecified address.
  225. //
  226. IP6_COPY_ADDRESS (&Head.SourceAddress, &IpSb->LinkLocalAddr);
  227. Ip6SetToAllNodeMulticast (TRUE, IP6_LINK_LOCAL_SCOPE, &Destination);
  228. IP6_COPY_ADDRESS (&Head.DestinationAddress, &Destination);
  229. NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
  230. //
  231. // Fill a IPv6 Router Alert option in a Hop-by-Hop Options Header
  232. //
  233. Options = NetbufAllocSpace (Packet, (UINT32)OptionLen, FALSE);
  234. ASSERT (Options != NULL);
  235. Status = Ip6FillHopByHop (Options, &OptionLen, IP6_ICMP);
  236. if (EFI_ERROR (Status)) {
  237. NetbufFree (Packet);
  238. Packet = NULL;
  239. return Status;
  240. }
  241. //
  242. // Fill in MLD message - Done
  243. //
  244. MldHead = (IP6_MLD_HEAD *)NetbufAllocSpace (Packet, sizeof (IP6_MLD_HEAD), FALSE);
  245. ASSERT (MldHead != NULL);
  246. ZeroMem (MldHead, sizeof (IP6_MLD_HEAD));
  247. MldHead->Head.Type = ICMP_V6_LISTENER_DONE;
  248. MldHead->Head.Code = 0;
  249. IP6_COPY_ADDRESS (&MldHead->Group, MulticastAddr);
  250. HeadChecksum = NetblockChecksum ((UINT8 *)MldHead, sizeof (IP6_MLD_HEAD));
  251. PseudoChecksum = NetIp6PseudoHeadChecksum (
  252. &Head.SourceAddress,
  253. &Head.DestinationAddress,
  254. IP6_ICMP,
  255. sizeof (IP6_MLD_HEAD)
  256. );
  257. MldHead->Head.Checksum = (UINT16) ~NetAddChecksum (HeadChecksum, PseudoChecksum);
  258. //
  259. // Transmit the packet
  260. //
  261. return Ip6Output (IpSb, NULL, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
  262. }
  263. /**
  264. Init the MLD data of the IP6 service instance. Configure
  265. MNP to receive ALL SYSTEM multicast.
  266. @param[in] IpSb The IP6 service whose MLD is to be initialized.
  267. @retval EFI_OUT_OF_RESOURCES There are not sufficient resourcet to complete the
  268. operation.
  269. @retval EFI_SUCCESS The MLD module successfully initialized.
  270. **/
  271. EFI_STATUS
  272. Ip6InitMld (
  273. IN IP6_SERVICE *IpSb
  274. )
  275. {
  276. EFI_IPv6_ADDRESS AllNodes;
  277. IP6_MLD_GROUP *Group;
  278. EFI_STATUS Status;
  279. //
  280. // Join the link-scope all-nodes multicast address (FF02::1).
  281. // This address is started in Idle Listener state and never transitions to
  282. // another state, and never sends a Report or Done for that address.
  283. //
  284. Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &AllNodes);
  285. Group = Ip6CreateMldEntry (IpSb, &AllNodes, (UINT32)IP6_INFINIT_LIFETIME);
  286. if (Group == NULL) {
  287. return EFI_OUT_OF_RESOURCES;
  288. }
  289. Status = Ip6GetMulticastMac (IpSb->Mnp, &AllNodes, &Group->Mac);
  290. if (EFI_ERROR (Status)) {
  291. goto ERROR;
  292. }
  293. //
  294. // Configure MNP to receive all-nodes multicast
  295. //
  296. Status = IpSb->Mnp->Groups (IpSb->Mnp, TRUE, &Group->Mac);
  297. if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
  298. goto ERROR;
  299. }
  300. return EFI_SUCCESS;
  301. ERROR:
  302. RemoveEntryList (&Group->Link);
  303. FreePool (Group);
  304. return Status;
  305. }
  306. /**
  307. Add a group address to the array of group addresses.
  308. The caller should make sure that no duplicated address
  309. existed in the array.
  310. @param[in, out] IpInstance Points to an IP6_PROTOCOL instance.
  311. @param[in] Group The IP6 multicast address to add.
  312. @retval EFI_OUT_OF_RESOURCES There are not sufficient resources to complete
  313. the operation.
  314. @retval EFI_SUCCESS The address is added to the group address array.
  315. **/
  316. EFI_STATUS
  317. Ip6CombineGroups (
  318. IN OUT IP6_PROTOCOL *IpInstance,
  319. IN EFI_IPv6_ADDRESS *Group
  320. )
  321. {
  322. EFI_IPv6_ADDRESS *GroupList;
  323. NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE);
  324. ASSERT (Group != NULL && IP6_IS_MULTICAST (Group));
  325. IpInstance->GroupCount++;
  326. GroupList = AllocatePool (IpInstance->GroupCount * sizeof (EFI_IPv6_ADDRESS));
  327. if (GroupList == NULL) {
  328. return EFI_OUT_OF_RESOURCES;
  329. }
  330. if (IpInstance->GroupCount > 1) {
  331. ASSERT (IpInstance->GroupList != NULL);
  332. CopyMem (
  333. GroupList,
  334. IpInstance->GroupList,
  335. (IpInstance->GroupCount - 1) * sizeof (EFI_IPv6_ADDRESS)
  336. );
  337. FreePool (IpInstance->GroupList);
  338. }
  339. IP6_COPY_ADDRESS (GroupList + (IpInstance->GroupCount - 1), Group);
  340. IpInstance->GroupList = GroupList;
  341. return EFI_SUCCESS;
  342. }
  343. /**
  344. Remove a group address from the array of group addresses.
  345. Although the function doesn't assume the byte order of Group,
  346. the network byte order is used by the caller.
  347. @param[in, out] IpInstance Points to an IP6_PROTOCOL instance.
  348. @param[in] Group The IP6 multicast address to remove.
  349. @retval EFI_NOT_FOUND Cannot find the to be removed group address.
  350. @retval EFI_SUCCESS The group address was successfully removed.
  351. **/
  352. EFI_STATUS
  353. Ip6RemoveGroup (
  354. IN OUT IP6_PROTOCOL *IpInstance,
  355. IN EFI_IPv6_ADDRESS *Group
  356. )
  357. {
  358. UINT32 Index;
  359. UINT32 Count;
  360. Count = IpInstance->GroupCount;
  361. for (Index = 0; Index < Count; Index++) {
  362. if (EFI_IP6_EQUAL (IpInstance->GroupList + Index, Group)) {
  363. break;
  364. }
  365. }
  366. if (Index == Count) {
  367. return EFI_NOT_FOUND;
  368. }
  369. while (Index < Count - 1) {
  370. IP6_COPY_ADDRESS (IpInstance->GroupList + Index, IpInstance->GroupList + Index + 1);
  371. Index++;
  372. }
  373. ASSERT (IpInstance->GroupCount > 0);
  374. IpInstance->GroupCount--;
  375. return EFI_SUCCESS;
  376. }
  377. /**
  378. Join the multicast group on behalf of this IP6 service binding instance.
  379. @param[in] IpSb The IP6 service binding instance.
  380. @param[in] Interface Points to an IP6_INTERFACE structure.
  381. @param[in] Address The group address to join.
  382. @retval EFI_SUCCESS Successfully join the multicast group.
  383. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
  384. @retval Others Failed to join the multicast group.
  385. **/
  386. EFI_STATUS
  387. Ip6JoinGroup (
  388. IN IP6_SERVICE *IpSb,
  389. IN IP6_INTERFACE *Interface,
  390. IN EFI_IPv6_ADDRESS *Address
  391. )
  392. {
  393. IP6_MLD_GROUP *Group;
  394. EFI_STATUS Status;
  395. Group = Ip6FindMldEntry (IpSb, Address);
  396. if (Group != NULL) {
  397. Group->RefCnt++;
  398. return EFI_SUCCESS;
  399. }
  400. //
  401. // Repeat the report once or twice after short delays [Unsolicited Report Interval] (default:10s)
  402. // Simulate this operation as a Multicast-Address-Specific Query was received for that address.
  403. //
  404. Group = Ip6CreateMldEntry (IpSb, Address, IP6_UNSOLICITED_REPORT_INTERVAL);
  405. if (Group == NULL) {
  406. return EFI_OUT_OF_RESOURCES;
  407. }
  408. Group->SendByUs = TRUE;
  409. Status = Ip6GetMulticastMac (IpSb->Mnp, Address, &Group->Mac);
  410. if (EFI_ERROR (Status)) {
  411. return Status;
  412. }
  413. Status = IpSb->Mnp->Groups (IpSb->Mnp, TRUE, &Group->Mac);
  414. if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
  415. goto ERROR;
  416. }
  417. //
  418. // Send unsolicited report when a node starts listening to a multicast address
  419. //
  420. Status = Ip6SendMldReport (IpSb, Interface, Address);
  421. if (EFI_ERROR (Status)) {
  422. goto ERROR;
  423. }
  424. return EFI_SUCCESS;
  425. ERROR:
  426. RemoveEntryList (&Group->Link);
  427. FreePool (Group);
  428. return Status;
  429. }
  430. /**
  431. Leave the IP6 multicast group.
  432. @param[in] IpSb The IP6 service binding instance.
  433. @param[in] Address The group address to leave.
  434. @retval EFI_NOT_FOUND The IP6 service instance isn't in the group.
  435. @retval EFI_SUCCESS Successfully leave the multicast group..
  436. @retval Others Failed to leave the multicast group.
  437. **/
  438. EFI_STATUS
  439. Ip6LeaveGroup (
  440. IN IP6_SERVICE *IpSb,
  441. IN EFI_IPv6_ADDRESS *Address
  442. )
  443. {
  444. IP6_MLD_GROUP *Group;
  445. EFI_STATUS Status;
  446. Group = Ip6FindMldEntry (IpSb, Address);
  447. if (Group == NULL) {
  448. return EFI_NOT_FOUND;
  449. }
  450. //
  451. // If more than one instance is in the group, decrease
  452. // the RefCnt then return.
  453. //
  454. if ((Group->RefCnt > 0) && (--Group->RefCnt > 0)) {
  455. return EFI_SUCCESS;
  456. }
  457. //
  458. // If multiple IP6 group addresses are mapped to the same
  459. // multicast MAC address, don't configure the MNP to leave
  460. // the MAC.
  461. //
  462. if (Ip6FindMac (&IpSb->MldCtrl, &Group->Mac) == 1) {
  463. Status = IpSb->Mnp->Groups (IpSb->Mnp, FALSE, &Group->Mac);
  464. if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
  465. return Status;
  466. }
  467. }
  468. //
  469. // Send a leave report if we are the last node to report
  470. //
  471. if (Group->SendByUs) {
  472. Status = Ip6SendMldDone (IpSb, Address);
  473. if (EFI_ERROR (Status)) {
  474. return Status;
  475. }
  476. }
  477. RemoveEntryList (&Group->Link);
  478. FreePool (Group);
  479. return EFI_SUCCESS;
  480. }
  481. /**
  482. Worker function for EfiIp6Groups(). The caller
  483. should make sure that the parameters are valid.
  484. @param[in] IpInstance The IP6 child to change the setting.
  485. @param[in] JoinFlag TRUE to join the group, otherwise leave it.
  486. @param[in] GroupAddress The target group address. If NULL, leave all
  487. the group addresses.
  488. @retval EFI_ALREADY_STARTED Wants to join the group, but is already a member of it
  489. @retval EFI_OUT_OF_RESOURCES Failed to allocate sufficient resources.
  490. @retval EFI_DEVICE_ERROR Failed to set the group configuration.
  491. @retval EFI_SUCCESS Successfully updated the group setting.
  492. @retval EFI_NOT_FOUND Try to leave the group which it isn't a member.
  493. **/
  494. EFI_STATUS
  495. Ip6Groups (
  496. IN IP6_PROTOCOL *IpInstance,
  497. IN BOOLEAN JoinFlag,
  498. IN EFI_IPv6_ADDRESS *GroupAddress OPTIONAL
  499. )
  500. {
  501. EFI_STATUS Status;
  502. IP6_SERVICE *IpSb;
  503. UINT32 Index;
  504. EFI_IPv6_ADDRESS *Group;
  505. IpSb = IpInstance->Service;
  506. if (JoinFlag) {
  507. ASSERT (GroupAddress != NULL);
  508. for (Index = 0; Index < IpInstance->GroupCount; Index++) {
  509. if (EFI_IP6_EQUAL (IpInstance->GroupList + Index, GroupAddress)) {
  510. return EFI_ALREADY_STARTED;
  511. }
  512. }
  513. Status = Ip6JoinGroup (IpSb, IpInstance->Interface, GroupAddress);
  514. if (!EFI_ERROR (Status)) {
  515. return Ip6CombineGroups (IpInstance, GroupAddress);
  516. }
  517. return Status;
  518. }
  519. //
  520. // Leave the group. Leave all the groups if GroupAddress is NULL.
  521. //
  522. for (Index = IpInstance->GroupCount; Index > 0; Index--) {
  523. Group = IpInstance->GroupList + (Index - 1);
  524. if ((GroupAddress == NULL) || EFI_IP6_EQUAL (Group, GroupAddress)) {
  525. Status = Ip6LeaveGroup (IpInstance->Service, Group);
  526. if (EFI_ERROR (Status)) {
  527. return Status;
  528. }
  529. Ip6RemoveGroup (IpInstance, Group);
  530. if (IpInstance->GroupCount == 0) {
  531. ASSERT (Index == 1);
  532. FreePool (IpInstance->GroupList);
  533. IpInstance->GroupList = NULL;
  534. }
  535. if (GroupAddress != NULL) {
  536. return EFI_SUCCESS;
  537. }
  538. }
  539. }
  540. return ((GroupAddress != NULL) ? EFI_NOT_FOUND : EFI_SUCCESS);
  541. }
  542. /**
  543. Set a random value of the delay timer for the multicast address from the range
  544. [0, Maximum Response Delay]. If a timer for any address is already
  545. running, it is reset to the new random value only if the requested
  546. Maximum Response Delay is less than the remaining value of the
  547. running timer. If the Query packet specifies a Maximum Response
  548. Delay of zero, each timer is effectively set to zero, and the action
  549. specified below for timer expiration is performed immediately.
  550. @param[in] IpSb The IP6 service binding instance.
  551. @param[in] MaxRespDelay The Maximum Response Delay, in milliseconds.
  552. @param[in] MulticastAddr The multicast address.
  553. @param[in, out] Group Points to a IP6_MLD_GROUP list entry node.
  554. @retval EFI_SUCCESS The delay timer is successfully updated or
  555. timer expiration is performed immediately.
  556. @retval Others Failed to send out MLD report message.
  557. **/
  558. EFI_STATUS
  559. Ip6UpdateDelayTimer (
  560. IN IP6_SERVICE *IpSb,
  561. IN UINT16 MaxRespDelay,
  562. IN EFI_IPv6_ADDRESS *MulticastAddr,
  563. IN OUT IP6_MLD_GROUP *Group
  564. )
  565. {
  566. UINT32 Delay;
  567. //
  568. // If the Query packet specifies a Maximum Response Delay of zero, perform timer
  569. // expiration immediately.
  570. //
  571. if (MaxRespDelay == 0) {
  572. Group->DelayTimer = 0;
  573. return Ip6SendMldReport (IpSb, NULL, MulticastAddr);
  574. }
  575. Delay = (UINT32)(MaxRespDelay / 1000);
  576. //
  577. // Sets a delay timer to a random value selected from the range [0, Maximum Response Delay]
  578. // If a timer is already running, resets it if the request Maximum Response Delay
  579. // is less than the remaining value of the running timer.
  580. //
  581. if ((Group->DelayTimer == 0) || (Delay < Group->DelayTimer)) {
  582. Group->DelayTimer = Delay / 4294967295UL * NET_RANDOM (NetRandomInitSeed ());
  583. }
  584. return EFI_SUCCESS;
  585. }
  586. /**
  587. Process the Multicast Listener Query message.
  588. @param[in] IpSb The IP service that received the packet.
  589. @param[in] Head The IP head of the MLD query packet.
  590. @param[in] Packet The content of the MLD query packet with IP head
  591. removed.
  592. @retval EFI_SUCCESS The MLD query packet processed successfully.
  593. @retval EFI_INVALID_PARAMETER The packet is invalid.
  594. @retval Others Failed to process the packet.
  595. **/
  596. EFI_STATUS
  597. Ip6ProcessMldQuery (
  598. IN IP6_SERVICE *IpSb,
  599. IN EFI_IP6_HEADER *Head,
  600. IN NET_BUF *Packet
  601. )
  602. {
  603. EFI_IPv6_ADDRESS AllNodes;
  604. IP6_MLD_GROUP *Group;
  605. IP6_MLD_HEAD MldPacket;
  606. LIST_ENTRY *Entry;
  607. EFI_STATUS Status;
  608. Status = EFI_INVALID_PARAMETER;
  609. //
  610. // Check the validity of the packet, generic query or specific query
  611. //
  612. if (!NetIp6IsUnspecifiedAddr (&Head->SourceAddress) && !NetIp6IsLinkLocalAddr (&Head->SourceAddress)) {
  613. goto Exit;
  614. }
  615. if ((Head->HopLimit != 1) || !IP6_IS_MULTICAST (&Head->DestinationAddress)) {
  616. goto Exit;
  617. }
  618. //
  619. // The Packet points to MLD report raw data without Hop-By-Hop option.
  620. //
  621. NetbufCopy (Packet, 0, sizeof (IP6_MLD_HEAD), (UINT8 *)&MldPacket);
  622. MldPacket.MaxRespDelay = NTOHS (MldPacket.MaxRespDelay);
  623. Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &AllNodes);
  624. if (!EFI_IP6_EQUAL (&Head->DestinationAddress, &AllNodes)) {
  625. //
  626. // Receives a Multicast-Address-Specific Query, check it firstly
  627. //
  628. if (!EFI_IP6_EQUAL (&Head->DestinationAddress, &MldPacket.Group)) {
  629. goto Exit;
  630. }
  631. //
  632. // The node is not listening but it receives the specific query. Just return.
  633. //
  634. Group = Ip6FindMldEntry (IpSb, &MldPacket.Group);
  635. if (Group == NULL) {
  636. Status = EFI_SUCCESS;
  637. goto Exit;
  638. }
  639. Status = Ip6UpdateDelayTimer (
  640. IpSb,
  641. MldPacket.MaxRespDelay,
  642. &MldPacket.Group,
  643. Group
  644. );
  645. goto Exit;
  646. }
  647. //
  648. // Receives a General Query, sets a delay timer for each multicast address it is listening
  649. //
  650. NET_LIST_FOR_EACH (Entry, &IpSb->MldCtrl.Groups) {
  651. Group = NET_LIST_USER_STRUCT (Entry, IP6_MLD_GROUP, Link);
  652. Status = Ip6UpdateDelayTimer (IpSb, MldPacket.MaxRespDelay, &Group->Address, Group);
  653. if (EFI_ERROR (Status)) {
  654. goto Exit;
  655. }
  656. }
  657. Status = EFI_SUCCESS;
  658. Exit:
  659. NetbufFree (Packet);
  660. return Status;
  661. }
  662. /**
  663. Process the Multicast Listener Report message.
  664. @param[in] IpSb The IP service that received the packet.
  665. @param[in] Head The IP head of the MLD report packet.
  666. @param[in] Packet The content of the MLD report packet with IP head
  667. removed.
  668. @retval EFI_SUCCESS The MLD report packet processed successfully.
  669. @retval EFI_INVALID_PARAMETER The packet is invalid.
  670. **/
  671. EFI_STATUS
  672. Ip6ProcessMldReport (
  673. IN IP6_SERVICE *IpSb,
  674. IN EFI_IP6_HEADER *Head,
  675. IN NET_BUF *Packet
  676. )
  677. {
  678. IP6_MLD_HEAD MldPacket;
  679. IP6_MLD_GROUP *Group;
  680. EFI_STATUS Status;
  681. Status = EFI_INVALID_PARAMETER;
  682. //
  683. // Validate the incoming message, if invalid, drop it.
  684. //
  685. if (!NetIp6IsUnspecifiedAddr (&Head->SourceAddress) && !NetIp6IsLinkLocalAddr (&Head->SourceAddress)) {
  686. goto Exit;
  687. }
  688. if ((Head->HopLimit != 1) || !IP6_IS_MULTICAST (&Head->DestinationAddress)) {
  689. goto Exit;
  690. }
  691. //
  692. // The Packet points to MLD report raw data without Hop-By-Hop option.
  693. //
  694. NetbufCopy (Packet, 0, sizeof (IP6_MLD_HEAD), (UINT8 *)&MldPacket);
  695. if (!EFI_IP6_EQUAL (&Head->DestinationAddress, &MldPacket.Group)) {
  696. goto Exit;
  697. }
  698. Group = Ip6FindMldEntry (IpSb, &MldPacket.Group);
  699. if (Group == NULL) {
  700. goto Exit;
  701. }
  702. //
  703. // The report is sent by another node, stop its own timer relates to the multicast address and clear
  704. //
  705. if (!Group->SendByUs) {
  706. Group->DelayTimer = 0;
  707. }
  708. Status = EFI_SUCCESS;
  709. Exit:
  710. NetbufFree (Packet);
  711. return Status;
  712. }
  713. /**
  714. The heartbeat timer of MLD module. It sends out a solicited MLD report when
  715. DelayTimer expires.
  716. @param[in] IpSb The IP6 service binding instance.
  717. **/
  718. VOID
  719. Ip6MldTimerTicking (
  720. IN IP6_SERVICE *IpSb
  721. )
  722. {
  723. IP6_MLD_GROUP *Group;
  724. LIST_ENTRY *Entry;
  725. //
  726. // Send solicited report when timer expires
  727. //
  728. NET_LIST_FOR_EACH (Entry, &IpSb->MldCtrl.Groups) {
  729. Group = NET_LIST_USER_STRUCT (Entry, IP6_MLD_GROUP, Link);
  730. if ((Group->DelayTimer > 0) && (--Group->DelayTimer == 0)) {
  731. Ip6SendMldReport (IpSb, NULL, &Group->Address);
  732. }
  733. }
  734. }