Ip4Igmp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /** @file
  2. This file implements the RFC2236: IGMP v2.
  3. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Ip4Impl.h"
  7. //
  8. // Route Alert option in IGMP report to direct routers to
  9. // examine the packet more closely.
  10. //
  11. UINT32 mRouteAlertOption = 0x00000494;
  12. /**
  13. Init the IGMP control data of the IP4 service instance, configure
  14. MNP to receive ALL SYSTEM multicast.
  15. @param[in, out] IpSb The IP4 service whose IGMP is to be initialized.
  16. @retval EFI_SUCCESS IGMP of the IpSb is successfully initialized.
  17. @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to initialize IGMP.
  18. @retval Others Failed to initialize the IGMP of IpSb.
  19. **/
  20. EFI_STATUS
  21. Ip4InitIgmp (
  22. IN OUT IP4_SERVICE *IpSb
  23. )
  24. {
  25. IGMP_SERVICE_DATA *IgmpCtrl;
  26. EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
  27. IGMP_GROUP *Group;
  28. EFI_STATUS Status;
  29. IgmpCtrl = &IpSb->IgmpCtrl;
  30. //
  31. // Configure MNP to receive ALL_SYSTEM multicast
  32. //
  33. Group = AllocatePool (sizeof (IGMP_GROUP));
  34. if (Group == NULL) {
  35. return EFI_OUT_OF_RESOURCES;
  36. }
  37. Mnp = IpSb->Mnp;
  38. Group->Address = IP4_ALLSYSTEM_ADDRESS;
  39. Group->RefCnt = 1;
  40. Group->DelayTime = 0;
  41. Group->ReportByUs = FALSE;
  42. Status = Ip4GetMulticastMac (Mnp, IP4_ALLSYSTEM_ADDRESS, &Group->Mac);
  43. if (EFI_ERROR (Status)) {
  44. goto ON_ERROR;
  45. }
  46. Status = Mnp->Groups (Mnp, TRUE, &Group->Mac);
  47. if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
  48. goto ON_ERROR;
  49. }
  50. InsertHeadList (&IgmpCtrl->Groups, &Group->Link);
  51. return EFI_SUCCESS;
  52. ON_ERROR:
  53. FreePool (Group);
  54. return Status;
  55. }
  56. /**
  57. Find the IGMP_GROUP structure which contains the status of multicast
  58. group Address in this IGMP control block
  59. @param[in] IgmpCtrl The IGMP control block to search from.
  60. @param[in] Address The multicast address to search.
  61. @return NULL if the multicast address isn't in the IGMP control block. Otherwise
  62. the point to the IGMP_GROUP which contains the status of multicast group
  63. for Address.
  64. **/
  65. IGMP_GROUP *
  66. Ip4FindGroup (
  67. IN IGMP_SERVICE_DATA *IgmpCtrl,
  68. IN IP4_ADDR Address
  69. )
  70. {
  71. LIST_ENTRY *Entry;
  72. IGMP_GROUP *Group;
  73. NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) {
  74. Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link);
  75. if (Group->Address == Address) {
  76. return Group;
  77. }
  78. }
  79. return NULL;
  80. }
  81. /**
  82. Count the number of IP4 multicast groups that are mapped to the
  83. same MAC address. Several IP4 multicast address may be mapped to
  84. the same MAC address.
  85. @param[in] IgmpCtrl The IGMP control block to search in.
  86. @param[in] Mac The MAC address to search.
  87. @return The number of the IP4 multicast group that mapped to the same
  88. multicast group Mac.
  89. **/
  90. INTN
  91. Ip4FindMac (
  92. IN IGMP_SERVICE_DATA *IgmpCtrl,
  93. IN EFI_MAC_ADDRESS *Mac
  94. )
  95. {
  96. LIST_ENTRY *Entry;
  97. IGMP_GROUP *Group;
  98. INTN Count;
  99. Count = 0;
  100. NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) {
  101. Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link);
  102. if (NET_MAC_EQUAL (&Group->Mac, Mac, sizeof (EFI_MAC_ADDRESS))) {
  103. Count++;
  104. }
  105. }
  106. return Count;
  107. }
  108. /**
  109. Send an IGMP protocol message to the Dst, such as IGMP v1 membership report.
  110. @param[in] IpSb The IP4 service instance that requests the
  111. transmission.
  112. @param[in] Dst The destination to send to.
  113. @param[in] Type The IGMP message type, such as IGMP v1 membership
  114. report.
  115. @param[in] Group The group address in the IGMP message head.
  116. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to build the message.
  117. @retval EFI_SUCCESS The IGMP message is successfully send.
  118. @retval Others Failed to send the IGMP message.
  119. **/
  120. EFI_STATUS
  121. Ip4SendIgmpMessage (
  122. IN IP4_SERVICE *IpSb,
  123. IN IP4_ADDR Dst,
  124. IN UINT8 Type,
  125. IN IP4_ADDR Group
  126. )
  127. {
  128. IP4_HEAD Head;
  129. NET_BUF *Packet;
  130. IGMP_HEAD *Igmp;
  131. //
  132. // Allocate a net buffer to hold the message
  133. //
  134. Packet = NetbufAlloc (IP4_MAX_HEADLEN + sizeof (IGMP_HEAD));
  135. if (Packet == NULL) {
  136. return EFI_OUT_OF_RESOURCES;
  137. }
  138. //
  139. // Fill in the IGMP and IP header, then transmit the message
  140. //
  141. NetbufReserve (Packet, IP4_MAX_HEADLEN);
  142. Igmp = (IGMP_HEAD *)NetbufAllocSpace (Packet, sizeof (IGMP_HEAD), FALSE);
  143. if (Igmp == NULL) {
  144. return EFI_OUT_OF_RESOURCES;
  145. }
  146. Igmp->Type = Type;
  147. Igmp->MaxRespTime = 0;
  148. Igmp->Checksum = 0;
  149. Igmp->Group = HTONL (Group);
  150. Igmp->Checksum = (UINT16)(~NetblockChecksum ((UINT8 *)Igmp, sizeof (IGMP_HEAD)));
  151. Head.Tos = 0;
  152. Head.Protocol = IP4_PROTO_IGMP;
  153. Head.Ttl = 1;
  154. Head.Fragment = 0;
  155. Head.Dst = Dst;
  156. Head.Src = IP4_ALLZERO_ADDRESS;
  157. return Ip4Output (
  158. IpSb,
  159. NULL,
  160. Packet,
  161. &Head,
  162. (UINT8 *)&mRouteAlertOption,
  163. sizeof (UINT32),
  164. IP4_ALLZERO_ADDRESS,
  165. Ip4SysPacketSent,
  166. NULL
  167. );
  168. }
  169. /**
  170. Send an IGMP membership report. Depends on whether the server is
  171. v1 or v2, it will send either a V1 or V2 membership report.
  172. @param[in] IpSb The IP4 service instance that requests the
  173. transmission.
  174. @param[in] Group The group address to report.
  175. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to build the message.
  176. @retval EFI_SUCCESS The IGMP report message is successfully send.
  177. @retval Others Failed to send the report.
  178. **/
  179. EFI_STATUS
  180. Ip4SendIgmpReport (
  181. IN IP4_SERVICE *IpSb,
  182. IN IP4_ADDR Group
  183. )
  184. {
  185. if (IpSb->IgmpCtrl.Igmpv1QuerySeen != 0) {
  186. return Ip4SendIgmpMessage (IpSb, Group, IGMP_V1_MEMBERSHIP_REPORT, Group);
  187. } else {
  188. return Ip4SendIgmpMessage (IpSb, Group, IGMP_V2_MEMBERSHIP_REPORT, Group);
  189. }
  190. }
  191. /**
  192. Join the multicast group on behalf of this IP4 child
  193. @param[in] IpInstance The IP4 child that wants to join the group.
  194. @param[in] Address The group to join.
  195. @retval EFI_SUCCESS Successfully join the multicast group.
  196. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
  197. @retval Others Failed to join the multicast group.
  198. **/
  199. EFI_STATUS
  200. Ip4JoinGroup (
  201. IN IP4_PROTOCOL *IpInstance,
  202. IN IP4_ADDR Address
  203. )
  204. {
  205. EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
  206. IP4_SERVICE *IpSb;
  207. IGMP_SERVICE_DATA *IgmpCtrl;
  208. IGMP_GROUP *Group;
  209. EFI_STATUS Status;
  210. IpSb = IpInstance->Service;
  211. IgmpCtrl = &IpSb->IgmpCtrl;
  212. Mnp = IpSb->Mnp;
  213. //
  214. // If the IP service already is a member in the group, just
  215. // increase the reference count and return.
  216. //
  217. Group = Ip4FindGroup (IgmpCtrl, Address);
  218. if (Group != NULL) {
  219. Group->RefCnt++;
  220. return EFI_SUCCESS;
  221. }
  222. //
  223. // Otherwise, create a new IGMP_GROUP, Get the multicast's MAC address,
  224. // send a report, then direct MNP to receive the multicast.
  225. //
  226. Group = AllocatePool (sizeof (IGMP_GROUP));
  227. if (Group == NULL) {
  228. return EFI_OUT_OF_RESOURCES;
  229. }
  230. Group->Address = Address;
  231. Group->RefCnt = 1;
  232. Group->DelayTime = IGMP_UNSOLICIATED_REPORT;
  233. Group->ReportByUs = TRUE;
  234. Status = Ip4GetMulticastMac (Mnp, Address, &Group->Mac);
  235. if (EFI_ERROR (Status)) {
  236. goto ON_ERROR;
  237. }
  238. Status = Ip4SendIgmpReport (IpSb, Address);
  239. if (EFI_ERROR (Status)) {
  240. goto ON_ERROR;
  241. }
  242. Status = Mnp->Groups (Mnp, TRUE, &Group->Mac);
  243. if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
  244. goto ON_ERROR;
  245. }
  246. InsertHeadList (&IgmpCtrl->Groups, &Group->Link);
  247. return EFI_SUCCESS;
  248. ON_ERROR:
  249. FreePool (Group);
  250. return Status;
  251. }
  252. /**
  253. Leave the IP4 multicast group on behalf of IpInstance.
  254. @param[in] IpInstance The IP4 child that wants to leave the group
  255. address.
  256. @param[in] Address The group address to leave.
  257. @retval EFI_NOT_FOUND The IP4 service instance isn't in the group.
  258. @retval EFI_SUCCESS Successfully leave the multicast group.
  259. @retval Others Failed to leave the multicast group.
  260. **/
  261. EFI_STATUS
  262. Ip4LeaveGroup (
  263. IN IP4_PROTOCOL *IpInstance,
  264. IN IP4_ADDR Address
  265. )
  266. {
  267. EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
  268. IP4_SERVICE *IpSb;
  269. IGMP_SERVICE_DATA *IgmpCtrl;
  270. IGMP_GROUP *Group;
  271. EFI_STATUS Status;
  272. IpSb = IpInstance->Service;
  273. IgmpCtrl = &IpSb->IgmpCtrl;
  274. Mnp = IpSb->Mnp;
  275. Group = Ip4FindGroup (IgmpCtrl, Address);
  276. if (Group == NULL) {
  277. return EFI_NOT_FOUND;
  278. }
  279. //
  280. // If more than one instance is in the group, decrease
  281. // the RefCnt then return.
  282. //
  283. if (--Group->RefCnt > 0) {
  284. return EFI_SUCCESS;
  285. }
  286. //
  287. // If multiple IP4 group addresses are mapped to the same
  288. // multicast MAC address, don't configure the MNP to leave
  289. // the MAC.
  290. //
  291. if (Ip4FindMac (IgmpCtrl, &Group->Mac) == 1) {
  292. Status = Mnp->Groups (Mnp, FALSE, &Group->Mac);
  293. if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
  294. return Status;
  295. }
  296. }
  297. //
  298. // Send a leave report if the membership is reported by us
  299. // and we are talking IGMPv2.
  300. //
  301. if (Group->ReportByUs && (IgmpCtrl->Igmpv1QuerySeen == 0)) {
  302. Ip4SendIgmpMessage (IpSb, IP4_ALLROUTER_ADDRESS, IGMP_LEAVE_GROUP, Group->Address);
  303. }
  304. RemoveEntryList (&Group->Link);
  305. FreePool (Group);
  306. return EFI_SUCCESS;
  307. }
  308. /**
  309. Handle the received IGMP message for the IP4 service instance.
  310. @param[in] IpSb The IP4 service instance that received the message.
  311. @param[in] Head The IP4 header of the received message.
  312. @param[in] Packet The IGMP message, without IP4 header.
  313. @retval EFI_INVALID_PARAMETER The IGMP message is malformatted.
  314. @retval EFI_SUCCESS The IGMP message is successfully processed.
  315. **/
  316. EFI_STATUS
  317. Ip4IgmpHandle (
  318. IN IP4_SERVICE *IpSb,
  319. IN IP4_HEAD *Head,
  320. IN NET_BUF *Packet
  321. )
  322. {
  323. IGMP_SERVICE_DATA *IgmpCtrl;
  324. IGMP_HEAD Igmp;
  325. IGMP_GROUP *Group;
  326. IP4_ADDR Address;
  327. LIST_ENTRY *Entry;
  328. IgmpCtrl = &IpSb->IgmpCtrl;
  329. //
  330. // Must checksum over the whole packet, later IGMP version
  331. // may employ message longer than 8 bytes. IP's header has
  332. // already been trimmed off.
  333. //
  334. if ((Packet->TotalSize < sizeof (Igmp)) || (NetbufChecksum (Packet) != 0)) {
  335. NetbufFree (Packet);
  336. return EFI_INVALID_PARAMETER;
  337. }
  338. //
  339. // Copy the packet in case it is fragmented
  340. //
  341. NetbufCopy (Packet, 0, sizeof (IGMP_HEAD), (UINT8 *)&Igmp);
  342. switch (Igmp.Type) {
  343. case IGMP_MEMBERSHIP_QUERY:
  344. //
  345. // If MaxRespTime is zero, it is most likely that we are
  346. // talking to a V1 router
  347. //
  348. if (Igmp.MaxRespTime == 0) {
  349. IgmpCtrl->Igmpv1QuerySeen = IGMP_V1ROUTER_PRESENT;
  350. Igmp.MaxRespTime = 100;
  351. }
  352. //
  353. // Igmp is ticking once per second but MaxRespTime is in
  354. // the unit of 100ms.
  355. //
  356. Igmp.MaxRespTime /= 10;
  357. Address = NTOHL (Igmp.Group);
  358. if (Address == IP4_ALLSYSTEM_ADDRESS) {
  359. break;
  360. }
  361. NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) {
  362. Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link);
  363. //
  364. // If address is all zero, all the memberships will be reported.
  365. // otherwise only one is reported.
  366. //
  367. if ((Address == IP4_ALLZERO_ADDRESS) || (Address == Group->Address)) {
  368. //
  369. // If the timer is pending, only update it if the time left
  370. // is longer than the MaxRespTime. TODO: randomize the DelayTime.
  371. //
  372. if ((Group->DelayTime == 0) || (Group->DelayTime > Igmp.MaxRespTime)) {
  373. Group->DelayTime = MAX (1, Igmp.MaxRespTime);
  374. }
  375. }
  376. }
  377. break;
  378. case IGMP_V1_MEMBERSHIP_REPORT:
  379. case IGMP_V2_MEMBERSHIP_REPORT:
  380. Address = NTOHL (Igmp.Group);
  381. Group = Ip4FindGroup (IgmpCtrl, Address);
  382. if ((Group != NULL) && (Group->DelayTime > 0)) {
  383. Group->DelayTime = 0;
  384. Group->ReportByUs = FALSE;
  385. }
  386. break;
  387. }
  388. NetbufFree (Packet);
  389. return EFI_SUCCESS;
  390. }
  391. /**
  392. The periodical timer function for IGMP. It does the following
  393. things:
  394. 1. Decrease the Igmpv1QuerySeen to make it possible to refresh
  395. the IGMP server type.
  396. 2. Decrease the report timer for each IGMP group in "delaying
  397. member" state.
  398. @param[in] IpSb The IP4 service instance that is ticking.
  399. **/
  400. VOID
  401. Ip4IgmpTicking (
  402. IN IP4_SERVICE *IpSb
  403. )
  404. {
  405. IGMP_SERVICE_DATA *IgmpCtrl;
  406. LIST_ENTRY *Entry;
  407. IGMP_GROUP *Group;
  408. IgmpCtrl = &IpSb->IgmpCtrl;
  409. if (IgmpCtrl->Igmpv1QuerySeen > 0) {
  410. IgmpCtrl->Igmpv1QuerySeen--;
  411. }
  412. //
  413. // Decrease the report timer for each IGMP group in "delaying member"
  414. //
  415. NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) {
  416. Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link);
  417. ASSERT (Group->DelayTime >= 0);
  418. if (Group->DelayTime > 0) {
  419. Group->DelayTime--;
  420. if (Group->DelayTime == 0) {
  421. Ip4SendIgmpReport (IpSb, Group->Address);
  422. Group->ReportByUs = TRUE;
  423. }
  424. }
  425. }
  426. }
  427. /**
  428. Add a group address to the array of group addresses.
  429. The caller should make sure that no duplicated address
  430. existed in the array. Although the function doesn't
  431. assume the byte order of the both Source and Addr, the
  432. network byte order is used by the caller.
  433. @param[in] Source The array of group addresses to add to.
  434. @param[in] Count The number of group addresses in the Source.
  435. @param[in] Addr The IP4 multicast address to add.
  436. @return NULL if failed to allocate memory for the new groups,
  437. otherwise the new combined group addresses.
  438. **/
  439. IP4_ADDR *
  440. Ip4CombineGroups (
  441. IN IP4_ADDR *Source,
  442. IN UINT32 Count,
  443. IN IP4_ADDR Addr
  444. )
  445. {
  446. IP4_ADDR *Groups;
  447. Groups = AllocatePool (sizeof (IP4_ADDR) * (Count + 1));
  448. if (Groups == NULL) {
  449. return NULL;
  450. }
  451. CopyMem (Groups, Source, Count * sizeof (IP4_ADDR));
  452. Groups[Count] = Addr;
  453. return Groups;
  454. }
  455. /**
  456. Remove a group address from the array of group addresses.
  457. Although the function doesn't assume the byte order of the
  458. both Groups and Addr, the network byte order is used by
  459. the caller.
  460. @param Groups The array of group addresses to remove from.
  461. @param Count The number of group addresses in the Groups.
  462. @param Addr The IP4 multicast address to remove.
  463. @return The number of group addresses in the Groups after remove.
  464. It is Count if the Addr isn't in the Groups.
  465. **/
  466. INTN
  467. Ip4RemoveGroupAddr (
  468. IN OUT IP4_ADDR *Groups,
  469. IN UINT32 Count,
  470. IN IP4_ADDR Addr
  471. )
  472. {
  473. UINT32 Index;
  474. for (Index = 0; Index < Count; Index++) {
  475. if (Groups[Index] == Addr) {
  476. break;
  477. }
  478. }
  479. while (Index < Count - 1) {
  480. Groups[Index] = Groups[Index + 1];
  481. Index++;
  482. }
  483. return Index;
  484. }