FrameDecider.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /**
  2. * @file FrameDecider.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stddef.h>
  31. #include <inttypes.h>
  32. #include <misc/debug.h>
  33. #include <misc/offset.h>
  34. #include <misc/balloc.h>
  35. #include <misc/ethernet_proto.h>
  36. #include <misc/ipv4_proto.h>
  37. #include <misc/igmp_proto.h>
  38. #include <misc/byteorder.h>
  39. #include <misc/compare.h>
  40. #include <client/FrameDecider.h>
  41. #include <generated/blog_channel_FrameDecider.h>
  42. #define DECIDE_STATE_NONE 1
  43. #define DECIDE_STATE_UNICAST 2
  44. #define DECIDE_STATE_FLOOD 3
  45. #define DECIDE_STATE_MULTICAST 4
  46. #define PeerLog(_o, ...) BLog_LogViaFunc((_o)->logfunc, (_o)->user, BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. static int compare_macs (const uint8_t *mac1, const uint8_t *mac2)
  48. {
  49. int c = memcmp(mac1, mac2, 6);
  50. return B_COMPARE(c, 0);
  51. }
  52. #include "FrameDecider_macs_tree.h"
  53. #include <structure/CAvl_impl.h>
  54. #include "FrameDecider_groups_tree.h"
  55. #include <structure/CAvl_impl.h>
  56. #include "FrameDecider_multicast_tree.h"
  57. #include <structure/CAvl_impl.h>
  58. static void add_mac_to_peer (FrameDeciderPeer *o, uint8_t *mac)
  59. {
  60. FrameDecider *d = o->d;
  61. // locate entry in tree
  62. FDMacsTreeRef ref = FDMacsTree_LookupExact(&d->macs_tree, 0, mac);
  63. if (FDMacsTreeIsValidRef(ref)) {
  64. struct _FrameDecider_mac_entry *entry = ref.ptr;
  65. if (entry->peer == o) {
  66. // this is our MAC; only move it to the end of the used list
  67. LinkedList2_Remove(&o->mac_entries_used, &entry->list_node);
  68. LinkedList2_Append(&o->mac_entries_used, &entry->list_node);
  69. return;
  70. }
  71. // some other peer has that MAC; disassociate it
  72. FDMacsTree_Remove(&d->macs_tree, 0, FDMacsTreeDeref(0, entry));
  73. LinkedList2_Remove(&entry->peer->mac_entries_used, &entry->list_node);
  74. LinkedList2_Append(&entry->peer->mac_entries_free, &entry->list_node);
  75. }
  76. // aquire MAC address entry, if there are no free ones reuse the oldest used one
  77. LinkedList2Node *list_node;
  78. struct _FrameDecider_mac_entry *entry;
  79. if (list_node = LinkedList2_GetFirst(&o->mac_entries_free)) {
  80. entry = UPPER_OBJECT(list_node, struct _FrameDecider_mac_entry, list_node);
  81. ASSERT(entry->peer == o)
  82. // remove from free
  83. LinkedList2_Remove(&o->mac_entries_free, &entry->list_node);
  84. } else {
  85. list_node = LinkedList2_GetFirst(&o->mac_entries_used);
  86. ASSERT(list_node)
  87. entry = UPPER_OBJECT(list_node, struct _FrameDecider_mac_entry, list_node);
  88. ASSERT(entry->peer == o)
  89. // remove from used
  90. FDMacsTree_Remove(&d->macs_tree, 0, FDMacsTreeDeref(0, entry));
  91. LinkedList2_Remove(&o->mac_entries_used, &entry->list_node);
  92. }
  93. PeerLog(o, BLOG_INFO, "adding MAC %02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8"", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  94. // set MAC in entry
  95. memcpy(entry->mac, mac, sizeof(entry->mac));
  96. // add to used
  97. LinkedList2_Append(&o->mac_entries_used, &entry->list_node);
  98. int res = FDMacsTree_Insert(&d->macs_tree, 0, FDMacsTreeDeref(0, entry), NULL);
  99. ASSERT(res)
  100. }
  101. static uint32_t compute_sig_for_group (uint32_t group)
  102. {
  103. return hton32(ntoh32(group)&0x7FFFFF);
  104. }
  105. static uint32_t compute_sig_for_mac (uint8_t *mac)
  106. {
  107. uint32_t sig;
  108. memcpy(&sig, mac + 2, 4);
  109. sig = hton32(ntoh32(sig)&0x7FFFFF);
  110. return sig;
  111. }
  112. static void add_to_multicast (FrameDecider *d, struct _FrameDecider_group_entry *group_entry)
  113. {
  114. // compute sig
  115. uint32_t sig = compute_sig_for_group(group_entry->group);
  116. FDMulticastTreeRef ref = FDMulticastTree_LookupExact(&d->multicast_tree, 0, sig);
  117. if (FDMulticastTreeIsValidRef(ref)) {
  118. // use existing master
  119. struct _FrameDecider_group_entry *master = ref.ptr;
  120. ASSERT(master->is_master)
  121. // set not master
  122. group_entry->is_master = 0;
  123. // insert to list
  124. LinkedList3Node_InitAfter(&group_entry->sig_list_node, &master->sig_list_node);
  125. } else {
  126. // make this entry master
  127. // set master
  128. group_entry->is_master = 1;
  129. // set sig
  130. group_entry->master.sig = sig;
  131. // insert to multicast tree
  132. int res = FDMulticastTree_Insert(&d->multicast_tree, 0, FDMulticastTreeDeref(0, group_entry), NULL);
  133. ASSERT(res)
  134. // init list node
  135. LinkedList3Node_InitLonely(&group_entry->sig_list_node);
  136. }
  137. }
  138. static void remove_from_multicast (FrameDecider *d, struct _FrameDecider_group_entry *group_entry)
  139. {
  140. // compute sig
  141. uint32_t sig = compute_sig_for_group(group_entry->group);
  142. if (group_entry->is_master) {
  143. // remove master from multicast tree
  144. FDMulticastTree_Remove(&d->multicast_tree, 0, FDMulticastTreeDeref(0, group_entry));
  145. if (!LinkedList3Node_IsLonely(&group_entry->sig_list_node)) {
  146. // at least one more group entry for this sig; make another entry the master
  147. // get an entry
  148. LinkedList3Node *list_node = LinkedList3Node_NextOrPrev(&group_entry->sig_list_node);
  149. struct _FrameDecider_group_entry *newmaster = UPPER_OBJECT(list_node, struct _FrameDecider_group_entry, sig_list_node);
  150. ASSERT(!newmaster->is_master)
  151. // set master
  152. newmaster->is_master = 1;
  153. // set sig
  154. newmaster->master.sig = sig;
  155. // insert to multicast tree
  156. int res = FDMulticastTree_Insert(&d->multicast_tree, 0, FDMulticastTreeDeref(0, newmaster), NULL);
  157. ASSERT(res)
  158. }
  159. }
  160. // free linked list node
  161. LinkedList3Node_Free(&group_entry->sig_list_node);
  162. }
  163. static void add_group_to_peer (FrameDeciderPeer *o, uint32_t group)
  164. {
  165. FrameDecider *d = o->d;
  166. struct _FrameDecider_group_entry *group_entry;
  167. // lookup if the peer already has that group
  168. FDGroupsTreeRef ref = FDGroupsTree_LookupExact(&o->groups_tree, 0, group);
  169. if (FDGroupsTreeIsValidRef(ref)) {
  170. group_entry = ref.ptr;
  171. // move to end of used list
  172. LinkedList2_Remove(&o->group_entries_used, &group_entry->list_node);
  173. LinkedList2_Append(&o->group_entries_used, &group_entry->list_node);
  174. } else {
  175. PeerLog(o, BLOG_INFO, "joined group %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
  176. ((uint8_t *)&group)[0], ((uint8_t *)&group)[1], ((uint8_t *)&group)[2], ((uint8_t *)&group)[3]
  177. );
  178. // aquire group entry, if there are no free ones reuse the earliest used one
  179. LinkedList2Node *node;
  180. if (node = LinkedList2_GetFirst(&o->group_entries_free)) {
  181. group_entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  182. // remove from free list
  183. LinkedList2_Remove(&o->group_entries_free, &group_entry->list_node);
  184. } else {
  185. node = LinkedList2_GetFirst(&o->group_entries_used);
  186. ASSERT(node)
  187. group_entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  188. // remove from multicast
  189. remove_from_multicast(d, group_entry);
  190. // remove from peer's groups tree
  191. FDGroupsTree_Remove(&o->groups_tree, 0, FDGroupsTreeDeref(0, group_entry));
  192. // remove from used list
  193. LinkedList2_Remove(&o->group_entries_used, &group_entry->list_node);
  194. }
  195. // add entry to used list
  196. LinkedList2_Append(&o->group_entries_used, &group_entry->list_node);
  197. // set group address
  198. group_entry->group = group;
  199. // insert to peer's groups tree
  200. int res = FDGroupsTree_Insert(&o->groups_tree, 0, FDGroupsTreeDeref(0, group_entry), NULL);
  201. ASSERT(res)
  202. // add to multicast
  203. add_to_multicast(d, group_entry);
  204. }
  205. // set timer
  206. group_entry->timer_endtime = btime_gettime() + d->igmp_group_membership_interval;
  207. BReactor_SetTimerAbsolute(d->reactor, &group_entry->timer, group_entry->timer_endtime);
  208. }
  209. static void remove_group_entry (struct _FrameDecider_group_entry *group_entry)
  210. {
  211. FrameDeciderPeer *peer = group_entry->peer;
  212. FrameDecider *d = peer->d;
  213. uint32_t group = group_entry->group;
  214. PeerLog(peer, BLOG_INFO, "left group %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
  215. ((uint8_t *)&group)[0], ((uint8_t *)&group)[1], ((uint8_t *)&group)[2], ((uint8_t *)&group)[3]
  216. );
  217. // remove from multicast
  218. remove_from_multicast(d, group_entry);
  219. // remove from peer's groups tree
  220. FDGroupsTree_Remove(&peer->groups_tree, 0, FDGroupsTreeDeref(0, group_entry));
  221. // remove from used list
  222. LinkedList2_Remove(&peer->group_entries_used, &group_entry->list_node);
  223. // add to free list
  224. LinkedList2_Append(&peer->group_entries_free, &group_entry->list_node);
  225. // stop timer
  226. BReactor_RemoveTimer(d->reactor, &group_entry->timer);
  227. }
  228. static void lower_group_timers_to_lmqt (FrameDecider *d, uint32_t group)
  229. {
  230. // have to lower all the group timers of this group down to LMQT
  231. // compute sig
  232. uint32_t sig = compute_sig_for_group(group);
  233. // look up the sig in multicast tree
  234. FDMulticastTreeRef ref = FDMulticastTree_LookupExact(&d->multicast_tree, 0, sig);
  235. if (FDMulticastTreeIsNullRef(ref)) {
  236. return;
  237. }
  238. struct _FrameDecider_group_entry *master = ref.ptr;
  239. ASSERT(master->is_master)
  240. // iterate all group entries with this sig
  241. LinkedList3Iterator it;
  242. LinkedList3Iterator_Init(&it, LinkedList3Node_First(&master->sig_list_node), 1);
  243. LinkedList3Node *sig_list_node;
  244. while (sig_list_node = LinkedList3Iterator_Next(&it)) {
  245. struct _FrameDecider_group_entry *group_entry = UPPER_OBJECT(sig_list_node, struct _FrameDecider_group_entry, sig_list_node);
  246. // skip wrong groups
  247. if (group_entry->group != group) {
  248. continue;
  249. }
  250. // lower timer down to LMQT
  251. btime_t now = btime_gettime();
  252. if (group_entry->timer_endtime > now + d->igmp_last_member_query_time) {
  253. group_entry->timer_endtime = now + d->igmp_last_member_query_time;
  254. BReactor_SetTimerAbsolute(d->reactor, &group_entry->timer, group_entry->timer_endtime);
  255. }
  256. }
  257. }
  258. static void group_entry_timer_handler (struct _FrameDecider_group_entry *group_entry)
  259. {
  260. DebugObject_Access(&group_entry->peer->d_obj);
  261. remove_group_entry(group_entry);
  262. }
  263. void FrameDecider_Init (FrameDecider *o, int max_peer_macs, int max_peer_groups, btime_t igmp_group_membership_interval, btime_t igmp_last_member_query_time, BReactor *reactor)
  264. {
  265. ASSERT(max_peer_macs > 0)
  266. ASSERT(max_peer_groups > 0)
  267. // init arguments
  268. o->max_peer_macs = max_peer_macs;
  269. o->max_peer_groups = max_peer_groups;
  270. o->igmp_group_membership_interval = igmp_group_membership_interval;
  271. o->igmp_last_member_query_time = igmp_last_member_query_time;
  272. o->reactor = reactor;
  273. // init peers list
  274. LinkedList2_Init(&o->peers_list);
  275. // init MAC tree
  276. FDMacsTree_Init(&o->macs_tree);
  277. // init multicast tree
  278. FDMulticastTree_Init(&o->multicast_tree);
  279. // init decide state
  280. o->decide_state = DECIDE_STATE_NONE;
  281. DebugObject_Init(&o->d_obj);
  282. }
  283. void FrameDecider_Free (FrameDecider *o)
  284. {
  285. ASSERT(FDMulticastTree_IsEmpty(&o->multicast_tree))
  286. ASSERT(FDMacsTree_IsEmpty(&o->macs_tree))
  287. ASSERT(LinkedList2_IsEmpty(&o->peers_list))
  288. DebugObject_Free(&o->d_obj);
  289. }
  290. void FrameDecider_AnalyzeAndDecide (FrameDecider *o, const uint8_t *frame, int frame_len)
  291. {
  292. ASSERT(frame_len >= 0)
  293. DebugObject_Access(&o->d_obj);
  294. // reset decide state
  295. switch (o->decide_state) {
  296. case DECIDE_STATE_NONE:
  297. break;
  298. case DECIDE_STATE_UNICAST:
  299. break;
  300. case DECIDE_STATE_FLOOD:
  301. LinkedList2Iterator_Free(&o->decide_flood_it);
  302. break;
  303. case DECIDE_STATE_MULTICAST:
  304. LinkedList3Iterator_Free(&o->decide_multicast_it);
  305. return;
  306. default:
  307. ASSERT(0);
  308. }
  309. o->decide_state = DECIDE_STATE_NONE;
  310. // analyze frame
  311. const uint8_t *pos = frame;
  312. int len = frame_len;
  313. if (len < sizeof(struct ethernet_header)) {
  314. return;
  315. }
  316. struct ethernet_header *eh = (struct ethernet_header *)pos;
  317. pos += sizeof(struct ethernet_header);
  318. len -= sizeof(struct ethernet_header);
  319. int is_igmp = 0;
  320. switch (ntoh16(eh->type)) {
  321. case ETHERTYPE_IPV4: {
  322. // check IPv4 header
  323. struct ipv4_header *ipv4_header;
  324. if (!ipv4_check((uint8_t *)pos, len, &ipv4_header, (uint8_t **)&pos, &len)) {
  325. BLog(BLOG_INFO, "decide: wrong IP packet");
  326. goto out;
  327. }
  328. // check if it's IGMP
  329. if (ntoh8(ipv4_header->protocol) != IPV4_PROTOCOL_IGMP) {
  330. goto out;
  331. }
  332. // remember that it's IGMP; we have to flood IGMP frames
  333. is_igmp = 1;
  334. // check IGMP header
  335. if (len < sizeof(struct igmp_base)) {
  336. BLog(BLOG_INFO, "decide: IGMP: short packet");
  337. goto out;
  338. }
  339. struct igmp_base *igmp_base = (struct igmp_base *)pos;
  340. pos += sizeof(struct igmp_base);
  341. len -= sizeof(struct igmp_base);
  342. switch (ntoh8(igmp_base->type)) {
  343. case IGMP_TYPE_MEMBERSHIP_QUERY: {
  344. if (len == sizeof(struct igmp_v2_extra) && ntoh8(igmp_base->max_resp_code) != 0) {
  345. // V2 query
  346. struct igmp_v2_extra *query = (struct igmp_v2_extra *)pos;
  347. pos += sizeof(struct igmp_v2_extra);
  348. len -= sizeof(struct igmp_v2_extra);
  349. if (ntoh32(query->group) != 0) {
  350. // got a Group-Specific Query, lower group timers to LMQT
  351. lower_group_timers_to_lmqt(o, query->group);
  352. }
  353. }
  354. else if (len >= sizeof(struct igmp_v3_query_extra)) {
  355. // V3 query
  356. struct igmp_v3_query_extra *query = (struct igmp_v3_query_extra *)pos;
  357. pos += sizeof(struct igmp_v3_query_extra);
  358. len -= sizeof(struct igmp_v3_query_extra);
  359. // iterate sources
  360. uint16_t num_sources = ntoh16(query->number_of_sources);
  361. int i;
  362. for (i = 0; i < num_sources; i++) {
  363. // check source
  364. if (len < sizeof(struct igmp_source)) {
  365. BLog(BLOG_NOTICE, "decide: IGMP: short source");
  366. goto out;
  367. }
  368. pos += sizeof(struct igmp_source);
  369. len -= sizeof(struct igmp_source);
  370. }
  371. if (ntoh32(query->group) != 0 && num_sources == 0) {
  372. // got a Group-Specific Query, lower group timers to LMQT
  373. lower_group_timers_to_lmqt(o, query->group);
  374. }
  375. }
  376. } break;
  377. }
  378. } break;
  379. }
  380. out:;
  381. const uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  382. const uint8_t multicast_mac_header[] = {0x01, 0x00, 0x5e};
  383. // if it's broadcast or IGMP, flood it
  384. if (is_igmp || !memcmp(eh->dest, broadcast_mac, sizeof(broadcast_mac))) {
  385. o->decide_state = DECIDE_STATE_FLOOD;
  386. LinkedList2Iterator_InitForward(&o->decide_flood_it, &o->peers_list);
  387. return;
  388. }
  389. // if it's multicast, forward to all peers with the given sig
  390. if (!memcmp(eh->dest, multicast_mac_header, sizeof(multicast_mac_header))) {
  391. // extract group's sig from destination MAC
  392. uint32_t sig = compute_sig_for_mac(eh->dest);
  393. // look up the sig in multicast tree
  394. FDMulticastTreeRef ref = FDMulticastTree_LookupExact(&o->multicast_tree, 0, sig);
  395. if (FDMulticastTreeIsValidRef(ref)) {
  396. struct _FrameDecider_group_entry *master = ref.ptr;
  397. ASSERT(master->is_master)
  398. o->decide_state = DECIDE_STATE_MULTICAST;
  399. LinkedList3Iterator_Init(&o->decide_multicast_it, LinkedList3Node_First(&master->sig_list_node), 1);
  400. }
  401. return;
  402. }
  403. // look for MAC entry
  404. FDMacsTreeRef ref = FDMacsTree_LookupExact(&o->macs_tree, 0, eh->dest);
  405. if (FDMacsTreeIsValidRef(ref)) {
  406. struct _FrameDecider_mac_entry *entry = ref.ptr;
  407. o->decide_state = DECIDE_STATE_UNICAST;
  408. o->decide_unicast_peer = entry->peer;
  409. return;
  410. }
  411. // unknown destination MAC, flood
  412. o->decide_state = DECIDE_STATE_FLOOD;
  413. LinkedList2Iterator_InitForward(&o->decide_flood_it, &o->peers_list);
  414. return;
  415. }
  416. FrameDeciderPeer * FrameDecider_NextDestination (FrameDecider *o)
  417. {
  418. DebugObject_Access(&o->d_obj);
  419. switch (o->decide_state) {
  420. case DECIDE_STATE_NONE: {
  421. return NULL;
  422. } break;
  423. case DECIDE_STATE_UNICAST: {
  424. o->decide_state = DECIDE_STATE_NONE;
  425. return o->decide_unicast_peer;
  426. } break;
  427. case DECIDE_STATE_FLOOD: {
  428. LinkedList2Node *list_node = LinkedList2Iterator_Next(&o->decide_flood_it);
  429. if (!list_node) {
  430. o->decide_state = DECIDE_STATE_NONE;
  431. return NULL;
  432. }
  433. FrameDeciderPeer *peer = UPPER_OBJECT(list_node, FrameDeciderPeer, list_node);
  434. return peer;
  435. } break;
  436. case DECIDE_STATE_MULTICAST: {
  437. LinkedList3Node *list_node = LinkedList3Iterator_Next(&o->decide_multicast_it);
  438. if (!list_node) {
  439. o->decide_state = DECIDE_STATE_NONE;
  440. return NULL;
  441. }
  442. struct _FrameDecider_group_entry *group_entry = UPPER_OBJECT(list_node, struct _FrameDecider_group_entry, sig_list_node);
  443. return group_entry->peer;
  444. } break;
  445. default:
  446. ASSERT(0);
  447. return NULL;
  448. }
  449. }
  450. int FrameDeciderPeer_Init (FrameDeciderPeer *o, FrameDecider *d, void *user, BLog_logfunc logfunc)
  451. {
  452. // init arguments
  453. o->d = d;
  454. o->user = user;
  455. o->logfunc = logfunc;
  456. // allocate MAC entries
  457. if (!(o->mac_entries = BAllocArray(d->max_peer_macs, sizeof(struct _FrameDecider_mac_entry)))) {
  458. PeerLog(o, BLOG_ERROR, "failed to allocate MAC entries");
  459. goto fail0;
  460. }
  461. // allocate group entries
  462. if (!(o->group_entries = BAllocArray(d->max_peer_groups, sizeof(struct _FrameDecider_group_entry)))) {
  463. PeerLog(o, BLOG_ERROR, "failed to allocate group entries");
  464. goto fail1;
  465. }
  466. // insert to peers list
  467. LinkedList2_Append(&d->peers_list, &o->list_node);
  468. // init MAC entry lists
  469. LinkedList2_Init(&o->mac_entries_free);
  470. LinkedList2_Init(&o->mac_entries_used);
  471. // initialize MAC entries
  472. for (int i = 0; i < d->max_peer_macs; i++) {
  473. struct _FrameDecider_mac_entry *entry = &o->mac_entries[i];
  474. // set peer
  475. entry->peer = o;
  476. // insert to free list
  477. LinkedList2_Append(&o->mac_entries_free, &entry->list_node);
  478. }
  479. // init group entry lists
  480. LinkedList2_Init(&o->group_entries_free);
  481. LinkedList2_Init(&o->group_entries_used);
  482. // initialize group entries
  483. for (int i = 0; i < d->max_peer_groups; i++) {
  484. struct _FrameDecider_group_entry *entry = &o->group_entries[i];
  485. // set peer
  486. entry->peer = o;
  487. // insert to free list
  488. LinkedList2_Append(&o->group_entries_free, &entry->list_node);
  489. // init timer
  490. BTimer_Init(&entry->timer, 0, (BTimer_handler)group_entry_timer_handler, entry);
  491. }
  492. // initialize groups tree
  493. FDGroupsTree_Init(&o->groups_tree);
  494. DebugObject_Init(&o->d_obj);
  495. return 1;
  496. fail1:
  497. BFree(o->mac_entries);
  498. fail0:
  499. return 0;
  500. }
  501. void FrameDeciderPeer_Free (FrameDeciderPeer *o)
  502. {
  503. DebugObject_Free(&o->d_obj);
  504. FrameDecider *d = o->d;
  505. // remove decide unicast reference
  506. if (d->decide_state == DECIDE_STATE_UNICAST && d->decide_unicast_peer == o) {
  507. d->decide_state = DECIDE_STATE_NONE;
  508. }
  509. LinkedList2Iterator it;
  510. LinkedList2Node *node;
  511. // free group entries
  512. LinkedList2Iterator_InitForward(&it, &o->group_entries_used);
  513. while (node = LinkedList2Iterator_Next(&it)) {
  514. struct _FrameDecider_group_entry *entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  515. // remove from multicast
  516. remove_from_multicast(d, entry);
  517. // stop timer
  518. BReactor_RemoveTimer(d->reactor, &entry->timer);
  519. }
  520. // remove used MAC entries from tree
  521. LinkedList2Iterator_InitForward(&it, &o->mac_entries_used);
  522. while (node = LinkedList2Iterator_Next(&it)) {
  523. struct _FrameDecider_mac_entry *entry = UPPER_OBJECT(node, struct _FrameDecider_mac_entry, list_node);
  524. // remove from tree
  525. FDMacsTree_Remove(&d->macs_tree, 0, FDMacsTreeDeref(0, entry));
  526. }
  527. // remove from peers list
  528. LinkedList2_Remove(&d->peers_list, &o->list_node);
  529. // free group entries
  530. BFree(o->group_entries);
  531. // free MAC entries
  532. BFree(o->mac_entries);
  533. }
  534. void FrameDeciderPeer_Analyze (FrameDeciderPeer *o, const uint8_t *frame, int frame_len)
  535. {
  536. ASSERT(frame_len >= 0)
  537. DebugObject_Access(&o->d_obj);
  538. const uint8_t *pos = frame;
  539. int len = frame_len;
  540. if (len < sizeof(struct ethernet_header)) {
  541. goto out;
  542. }
  543. struct ethernet_header *eh = (struct ethernet_header *)pos;
  544. pos += sizeof(struct ethernet_header);
  545. len -= sizeof(struct ethernet_header);
  546. // register source MAC address with this peer
  547. add_mac_to_peer(o, eh->source);
  548. switch (ntoh16(eh->type)) {
  549. case ETHERTYPE_IPV4: {
  550. // check IPv4 header
  551. struct ipv4_header *ipv4_header;
  552. if (!ipv4_check((uint8_t *)pos, len, &ipv4_header, (uint8_t **)&pos, &len)) {
  553. PeerLog(o, BLOG_INFO, "analyze: wrong IP packet");
  554. goto out;
  555. }
  556. // check if it's IGMP
  557. if (ntoh8(ipv4_header->protocol) != IPV4_PROTOCOL_IGMP) {
  558. goto out;
  559. }
  560. // check IGMP header
  561. if (len < sizeof(struct igmp_base)) {
  562. PeerLog(o, BLOG_INFO, "analyze: IGMP: short packet");
  563. goto out;
  564. }
  565. struct igmp_base *igmp_base = (struct igmp_base *)pos;
  566. pos += sizeof(struct igmp_base);
  567. len -= sizeof(struct igmp_base);
  568. switch (ntoh8(igmp_base->type)) {
  569. case IGMP_TYPE_V2_MEMBERSHIP_REPORT: {
  570. // check extra
  571. if (len < sizeof(struct igmp_v2_extra)) {
  572. PeerLog(o, BLOG_INFO, "analyze: IGMP: short v2 report");
  573. goto out;
  574. }
  575. struct igmp_v2_extra *report = (struct igmp_v2_extra *)pos;
  576. pos += sizeof(struct igmp_v2_extra);
  577. len -= sizeof(struct igmp_v2_extra);
  578. // add to group
  579. add_group_to_peer(o, report->group);
  580. } break;
  581. case IGMP_TYPE_V3_MEMBERSHIP_REPORT: {
  582. // check extra
  583. if (len < sizeof(struct igmp_v3_report_extra)) {
  584. PeerLog(o, BLOG_INFO, "analyze: IGMP: short v3 report");
  585. goto out;
  586. }
  587. struct igmp_v3_report_extra *report = (struct igmp_v3_report_extra *)pos;
  588. pos += sizeof(struct igmp_v3_report_extra);
  589. len -= sizeof(struct igmp_v3_report_extra);
  590. // iterate records
  591. uint16_t num_records = ntoh16(report->number_of_group_records);
  592. for (int i = 0; i < num_records; i++) {
  593. // check record
  594. if (len < sizeof(struct igmp_v3_report_record)) {
  595. PeerLog(o, BLOG_INFO, "analyze: IGMP: short record header");
  596. goto out;
  597. }
  598. struct igmp_v3_report_record *record = (struct igmp_v3_report_record *)pos;
  599. pos += sizeof(struct igmp_v3_report_record);
  600. len -= sizeof(struct igmp_v3_report_record);
  601. // iterate sources
  602. uint16_t num_sources = ntoh16(record->number_of_sources);
  603. int j;
  604. for (j = 0; j < num_sources; j++) {
  605. // check source
  606. if (len < sizeof(struct igmp_source)) {
  607. PeerLog(o, BLOG_INFO, "analyze: IGMP: short source");
  608. goto out;
  609. }
  610. struct igmp_source *source = (struct igmp_source *)pos;
  611. pos += sizeof(struct igmp_source);
  612. len -= sizeof(struct igmp_source);
  613. }
  614. // check aux data
  615. uint16_t aux_len = ntoh16(record->aux_data_len);
  616. if (len < aux_len) {
  617. PeerLog(o, BLOG_INFO, "analyze: IGMP: short record aux data");
  618. goto out;
  619. }
  620. pos += aux_len;
  621. len -= aux_len;
  622. switch (record->type) {
  623. case IGMP_RECORD_TYPE_MODE_IS_INCLUDE:
  624. case IGMP_RECORD_TYPE_CHANGE_TO_INCLUDE_MODE:
  625. if (num_sources != 0) {
  626. add_group_to_peer(o, record->group);
  627. }
  628. break;
  629. case IGMP_RECORD_TYPE_MODE_IS_EXCLUDE:
  630. case IGMP_RECORD_TYPE_CHANGE_TO_EXCLUDE_MODE:
  631. add_group_to_peer(o, record->group);
  632. break;
  633. }
  634. }
  635. } break;
  636. }
  637. } break;
  638. }
  639. out:;
  640. }