FrameDecider.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. static int uint32_comparator (void *user, uint32_t *v1, uint32_t *v2)
  55. {
  56. if (*v1 < *v2) {
  57. return -1;
  58. }
  59. if (*v1 > *v2) {
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. static void add_mac_to_peer (FrameDeciderPeer *o, uint8_t *mac)
  65. {
  66. FrameDecider *d = o->d;
  67. // locate entry in tree
  68. FDMacsTreeRef ref = FDMacsTree_LookupExact(&d->macs_tree, 0, mac);
  69. if (FDMacsTreeIsValidRef(ref)) {
  70. struct _FrameDecider_mac_entry *entry = ref.ptr;
  71. if (entry->peer == o) {
  72. // this is our MAC; only move it to the end of the used list
  73. LinkedList2_Remove(&o->mac_entries_used, &entry->list_node);
  74. LinkedList2_Append(&o->mac_entries_used, &entry->list_node);
  75. return;
  76. }
  77. // some other peer has that MAC; disassociate it
  78. FDMacsTree_Remove(&d->macs_tree, 0, FDMacsTreeDeref(0, entry));
  79. LinkedList2_Remove(&entry->peer->mac_entries_used, &entry->list_node);
  80. LinkedList2_Append(&entry->peer->mac_entries_free, &entry->list_node);
  81. }
  82. // aquire MAC address entry, if there are no free ones reuse the oldest used one
  83. LinkedList2Node *list_node;
  84. struct _FrameDecider_mac_entry *entry;
  85. if (list_node = LinkedList2_GetFirst(&o->mac_entries_free)) {
  86. entry = UPPER_OBJECT(list_node, struct _FrameDecider_mac_entry, list_node);
  87. ASSERT(entry->peer == o)
  88. // remove from free
  89. LinkedList2_Remove(&o->mac_entries_free, &entry->list_node);
  90. } else {
  91. list_node = LinkedList2_GetFirst(&o->mac_entries_used);
  92. ASSERT(list_node)
  93. entry = UPPER_OBJECT(list_node, struct _FrameDecider_mac_entry, list_node);
  94. ASSERT(entry->peer == o)
  95. // remove from used
  96. FDMacsTree_Remove(&d->macs_tree, 0, FDMacsTreeDeref(0, entry));
  97. LinkedList2_Remove(&o->mac_entries_used, &entry->list_node);
  98. }
  99. 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]);
  100. // set MAC in entry
  101. memcpy(entry->mac, mac, sizeof(entry->mac));
  102. // add to used
  103. LinkedList2_Append(&o->mac_entries_used, &entry->list_node);
  104. int res = FDMacsTree_Insert(&d->macs_tree, 0, FDMacsTreeDeref(0, entry), NULL);
  105. ASSERT(res)
  106. }
  107. static uint32_t compute_sig_for_group (uint32_t group)
  108. {
  109. return hton32(ntoh32(group)&0x7FFFFF);
  110. }
  111. static uint32_t compute_sig_for_mac (uint8_t *mac)
  112. {
  113. uint32_t sig;
  114. memcpy(&sig, mac + 2, 4);
  115. sig = hton32(ntoh32(sig)&0x7FFFFF);
  116. return sig;
  117. }
  118. static void add_to_multicast (FrameDecider *d, struct _FrameDecider_group_entry *group_entry)
  119. {
  120. // compute sig
  121. uint32_t sig = compute_sig_for_group(group_entry->group);
  122. BAVLNode *node;
  123. if (node = BAVL_LookupExact(&d->multicast_tree, &sig)) {
  124. // use existing master
  125. struct _FrameDecider_group_entry *master = UPPER_OBJECT(node, struct _FrameDecider_group_entry, master.tree_node);
  126. ASSERT(master->is_master)
  127. // set not master
  128. group_entry->is_master = 0;
  129. // insert to list
  130. LinkedList3Node_InitAfter(&group_entry->sig_list_node, &master->sig_list_node);
  131. } else {
  132. // make this entry master
  133. // set master
  134. group_entry->is_master = 1;
  135. // set sig
  136. group_entry->master.sig = sig;
  137. // insert to multicast tree
  138. ASSERT_EXECUTE(BAVL_Insert(&d->multicast_tree, &group_entry->master.tree_node, NULL))
  139. // init list node
  140. LinkedList3Node_InitLonely(&group_entry->sig_list_node);
  141. }
  142. }
  143. static void remove_from_multicast (FrameDecider *d, struct _FrameDecider_group_entry *group_entry)
  144. {
  145. // compute sig
  146. uint32_t sig = compute_sig_for_group(group_entry->group);
  147. if (group_entry->is_master) {
  148. // remove master from multicast tree
  149. BAVL_Remove(&d->multicast_tree, &group_entry->master.tree_node);
  150. if (!LinkedList3Node_IsLonely(&group_entry->sig_list_node)) {
  151. // at least one more group entry for this sig; make another entry the master
  152. // get an entry
  153. LinkedList3Node *list_node = LinkedList3Node_NextOrPrev(&group_entry->sig_list_node);
  154. struct _FrameDecider_group_entry *newmaster = UPPER_OBJECT(list_node, struct _FrameDecider_group_entry, sig_list_node);
  155. ASSERT(!newmaster->is_master)
  156. // set master
  157. newmaster->is_master = 1;
  158. // set sig
  159. newmaster->master.sig = sig;
  160. // insert to multicast tree
  161. ASSERT_EXECUTE(BAVL_Insert(&d->multicast_tree, &newmaster->master.tree_node, NULL))
  162. }
  163. }
  164. // free linked list node
  165. LinkedList3Node_Free(&group_entry->sig_list_node);
  166. }
  167. static void add_group_to_peer (FrameDeciderPeer *o, uint32_t group)
  168. {
  169. FrameDecider *d = o->d;
  170. struct _FrameDecider_group_entry *group_entry;
  171. // lookup if the peer already has that group
  172. BAVLNode *old_tree_node;
  173. if ((old_tree_node = BAVL_LookupExact(&o->groups_tree, &group))) {
  174. group_entry = UPPER_OBJECT(old_tree_node, struct _FrameDecider_group_entry, tree_node);
  175. // move to end of used list
  176. LinkedList2_Remove(&o->group_entries_used, &group_entry->list_node);
  177. LinkedList2_Append(&o->group_entries_used, &group_entry->list_node);
  178. } else {
  179. PeerLog(o, BLOG_INFO, "joined group %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
  180. ((uint8_t *)&group)[0], ((uint8_t *)&group)[1], ((uint8_t *)&group)[2], ((uint8_t *)&group)[3]
  181. );
  182. // aquire group entry, if there are no free ones reuse the earliest used one
  183. LinkedList2Node *node;
  184. if (node = LinkedList2_GetFirst(&o->group_entries_free)) {
  185. group_entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  186. // remove from free list
  187. LinkedList2_Remove(&o->group_entries_free, &group_entry->list_node);
  188. } else {
  189. node = LinkedList2_GetFirst(&o->group_entries_used);
  190. ASSERT(node)
  191. group_entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  192. // remove from multicast
  193. remove_from_multicast(d, group_entry);
  194. // remove from peer's groups tree
  195. BAVL_Remove(&o->groups_tree, &group_entry->tree_node);
  196. // remove from used list
  197. LinkedList2_Remove(&o->group_entries_used, &group_entry->list_node);
  198. }
  199. // add entry to used list
  200. LinkedList2_Append(&o->group_entries_used, &group_entry->list_node);
  201. // set group address
  202. group_entry->group = group;
  203. // insert to peer's groups tree
  204. ASSERT_EXECUTE(BAVL_Insert(&o->groups_tree, &group_entry->tree_node, NULL))
  205. // add to multicast
  206. add_to_multicast(d, group_entry);
  207. }
  208. // set timer
  209. group_entry->timer_endtime = btime_gettime() + d->igmp_group_membership_interval;
  210. BReactor_SetTimerAbsolute(d->reactor, &group_entry->timer, group_entry->timer_endtime);
  211. }
  212. static void remove_group_entry (struct _FrameDecider_group_entry *group_entry)
  213. {
  214. FrameDeciderPeer *peer = group_entry->peer;
  215. FrameDecider *d = peer->d;
  216. uint32_t group = group_entry->group;
  217. PeerLog(peer, BLOG_INFO, "left group %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
  218. ((uint8_t *)&group)[0], ((uint8_t *)&group)[1], ((uint8_t *)&group)[2], ((uint8_t *)&group)[3]
  219. );
  220. // remove from multicast
  221. remove_from_multicast(d, group_entry);
  222. // remove from peer's groups tree
  223. BAVL_Remove(&peer->groups_tree, &group_entry->tree_node);
  224. // remove from used list
  225. LinkedList2_Remove(&peer->group_entries_used, &group_entry->list_node);
  226. // add to free list
  227. LinkedList2_Append(&peer->group_entries_free, &group_entry->list_node);
  228. // stop timer
  229. BReactor_RemoveTimer(d->reactor, &group_entry->timer);
  230. }
  231. static void lower_group_timers_to_lmqt (FrameDecider *d, uint32_t group)
  232. {
  233. // have to lower all the group timers of this group down to LMQT
  234. // compute sig
  235. uint32_t sig = compute_sig_for_group(group);
  236. // look up the sig in multicast tree
  237. BAVLNode *tree_node;
  238. if (!(tree_node = BAVL_LookupExact(&d->multicast_tree, &sig))) {
  239. return;
  240. }
  241. struct _FrameDecider_group_entry *master = UPPER_OBJECT(tree_node, struct _FrameDecider_group_entry, master.tree_node);
  242. ASSERT(master->is_master)
  243. // iterate all group entries with this sig
  244. LinkedList3Iterator it;
  245. LinkedList3Iterator_Init(&it, LinkedList3Node_First(&master->sig_list_node), 1);
  246. LinkedList3Node *sig_list_node;
  247. while (sig_list_node = LinkedList3Iterator_Next(&it)) {
  248. struct _FrameDecider_group_entry *group_entry = UPPER_OBJECT(sig_list_node, struct _FrameDecider_group_entry, sig_list_node);
  249. // skip wrong groups
  250. if (group_entry->group != group) {
  251. continue;
  252. }
  253. // lower timer down to LMQT
  254. btime_t now = btime_gettime();
  255. if (group_entry->timer_endtime > now + d->igmp_last_member_query_time) {
  256. group_entry->timer_endtime = now + d->igmp_last_member_query_time;
  257. BReactor_SetTimerAbsolute(d->reactor, &group_entry->timer, group_entry->timer_endtime);
  258. }
  259. }
  260. }
  261. static void group_entry_timer_handler (struct _FrameDecider_group_entry *group_entry)
  262. {
  263. DebugObject_Access(&group_entry->peer->d_obj);
  264. remove_group_entry(group_entry);
  265. }
  266. 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)
  267. {
  268. ASSERT(max_peer_macs > 0)
  269. ASSERT(max_peer_groups > 0)
  270. // init arguments
  271. o->max_peer_macs = max_peer_macs;
  272. o->max_peer_groups = max_peer_groups;
  273. o->igmp_group_membership_interval = igmp_group_membership_interval;
  274. o->igmp_last_member_query_time = igmp_last_member_query_time;
  275. o->reactor = reactor;
  276. // init peers list
  277. LinkedList2_Init(&o->peers_list);
  278. // init MAC tree
  279. FDMacsTree_Init(&o->macs_tree);
  280. // init multicast tree
  281. BAVL_Init(&o->multicast_tree, OFFSET_DIFF(struct _FrameDecider_group_entry, master.sig, master.tree_node), (BAVL_comparator)uint32_comparator, NULL);
  282. // init decide state
  283. o->decide_state = DECIDE_STATE_NONE;
  284. DebugObject_Init(&o->d_obj);
  285. }
  286. void FrameDecider_Free (FrameDecider *o)
  287. {
  288. ASSERT(BAVL_IsEmpty(&o->multicast_tree))
  289. ASSERT(FDMacsTree_IsEmpty(&o->macs_tree))
  290. ASSERT(LinkedList2_IsEmpty(&o->peers_list))
  291. DebugObject_Free(&o->d_obj);
  292. }
  293. void FrameDecider_AnalyzeAndDecide (FrameDecider *o, const uint8_t *frame, int frame_len)
  294. {
  295. ASSERT(frame_len >= 0)
  296. DebugObject_Access(&o->d_obj);
  297. // reset decide state
  298. switch (o->decide_state) {
  299. case DECIDE_STATE_NONE:
  300. break;
  301. case DECIDE_STATE_UNICAST:
  302. break;
  303. case DECIDE_STATE_FLOOD:
  304. LinkedList2Iterator_Free(&o->decide_flood_it);
  305. break;
  306. case DECIDE_STATE_MULTICAST:
  307. LinkedList3Iterator_Free(&o->decide_multicast_it);
  308. return;
  309. default:
  310. ASSERT(0);
  311. }
  312. o->decide_state = DECIDE_STATE_NONE;
  313. // analyze frame
  314. const uint8_t *pos = frame;
  315. int len = frame_len;
  316. if (len < sizeof(struct ethernet_header)) {
  317. return;
  318. }
  319. struct ethernet_header *eh = (struct ethernet_header *)pos;
  320. pos += sizeof(struct ethernet_header);
  321. len -= sizeof(struct ethernet_header);
  322. int is_igmp = 0;
  323. switch (ntoh16(eh->type)) {
  324. case ETHERTYPE_IPV4: {
  325. // check IPv4 header
  326. struct ipv4_header *ipv4_header;
  327. if (!ipv4_check((uint8_t *)pos, len, &ipv4_header, (uint8_t **)&pos, &len)) {
  328. BLog(BLOG_INFO, "decide: wrong IP packet");
  329. goto out;
  330. }
  331. // check if it's IGMP
  332. if (ntoh8(ipv4_header->protocol) != IPV4_PROTOCOL_IGMP) {
  333. goto out;
  334. }
  335. // remember that it's IGMP; we have to flood IGMP frames
  336. is_igmp = 1;
  337. // check IGMP header
  338. if (len < sizeof(struct igmp_base)) {
  339. BLog(BLOG_INFO, "decide: IGMP: short packet");
  340. goto out;
  341. }
  342. struct igmp_base *igmp_base = (struct igmp_base *)pos;
  343. pos += sizeof(struct igmp_base);
  344. len -= sizeof(struct igmp_base);
  345. switch (ntoh8(igmp_base->type)) {
  346. case IGMP_TYPE_MEMBERSHIP_QUERY: {
  347. if (len == sizeof(struct igmp_v2_extra) && ntoh8(igmp_base->max_resp_code) != 0) {
  348. // V2 query
  349. struct igmp_v2_extra *query = (struct igmp_v2_extra *)pos;
  350. pos += sizeof(struct igmp_v2_extra);
  351. len -= sizeof(struct igmp_v2_extra);
  352. if (ntoh32(query->group) != 0) {
  353. // got a Group-Specific Query, lower group timers to LMQT
  354. lower_group_timers_to_lmqt(o, query->group);
  355. }
  356. }
  357. else if (len >= sizeof(struct igmp_v3_query_extra)) {
  358. // V3 query
  359. struct igmp_v3_query_extra *query = (struct igmp_v3_query_extra *)pos;
  360. pos += sizeof(struct igmp_v3_query_extra);
  361. len -= sizeof(struct igmp_v3_query_extra);
  362. // iterate sources
  363. uint16_t num_sources = ntoh16(query->number_of_sources);
  364. int i;
  365. for (i = 0; i < num_sources; i++) {
  366. // check source
  367. if (len < sizeof(struct igmp_source)) {
  368. BLog(BLOG_NOTICE, "decide: IGMP: short source");
  369. goto out;
  370. }
  371. pos += sizeof(struct igmp_source);
  372. len -= sizeof(struct igmp_source);
  373. }
  374. if (ntoh32(query->group) != 0 && num_sources == 0) {
  375. // got a Group-Specific Query, lower group timers to LMQT
  376. lower_group_timers_to_lmqt(o, query->group);
  377. }
  378. }
  379. } break;
  380. }
  381. } break;
  382. }
  383. out:;
  384. const uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  385. const uint8_t multicast_mac_header[] = {0x01, 0x00, 0x5e};
  386. // if it's broadcast or IGMP, flood it
  387. if (is_igmp || !memcmp(eh->dest, broadcast_mac, sizeof(broadcast_mac))) {
  388. o->decide_state = DECIDE_STATE_FLOOD;
  389. LinkedList2Iterator_InitForward(&o->decide_flood_it, &o->peers_list);
  390. return;
  391. }
  392. // if it's multicast, forward to all peers with the given sig
  393. if (!memcmp(eh->dest, multicast_mac_header, sizeof(multicast_mac_header))) {
  394. // extract group's sig from destination MAC
  395. uint32_t sig = compute_sig_for_mac(eh->dest);
  396. // look up the sig in multicast tree
  397. BAVLNode *node;
  398. if (node = BAVL_LookupExact(&o->multicast_tree, &sig)) {
  399. struct _FrameDecider_group_entry *master = UPPER_OBJECT(node, struct _FrameDecider_group_entry, master.tree_node);
  400. ASSERT(master->is_master)
  401. o->decide_state = DECIDE_STATE_MULTICAST;
  402. LinkedList3Iterator_Init(&o->decide_multicast_it, LinkedList3Node_First(&master->sig_list_node), 1);
  403. }
  404. return;
  405. }
  406. // look for MAC entry
  407. FDMacsTreeRef ref = FDMacsTree_LookupExact(&o->macs_tree, 0, eh->dest);
  408. if (FDMacsTreeIsValidRef(ref)) {
  409. struct _FrameDecider_mac_entry *entry = ref.ptr;
  410. o->decide_state = DECIDE_STATE_UNICAST;
  411. o->decide_unicast_peer = entry->peer;
  412. return;
  413. }
  414. // unknown destination MAC, flood
  415. o->decide_state = DECIDE_STATE_FLOOD;
  416. LinkedList2Iterator_InitForward(&o->decide_flood_it, &o->peers_list);
  417. return;
  418. }
  419. FrameDeciderPeer * FrameDecider_NextDestination (FrameDecider *o)
  420. {
  421. DebugObject_Access(&o->d_obj);
  422. switch (o->decide_state) {
  423. case DECIDE_STATE_NONE: {
  424. return NULL;
  425. } break;
  426. case DECIDE_STATE_UNICAST: {
  427. o->decide_state = DECIDE_STATE_NONE;
  428. return o->decide_unicast_peer;
  429. } break;
  430. case DECIDE_STATE_FLOOD: {
  431. LinkedList2Node *list_node = LinkedList2Iterator_Next(&o->decide_flood_it);
  432. if (!list_node) {
  433. o->decide_state = DECIDE_STATE_NONE;
  434. return NULL;
  435. }
  436. FrameDeciderPeer *peer = UPPER_OBJECT(list_node, FrameDeciderPeer, list_node);
  437. return peer;
  438. } break;
  439. case DECIDE_STATE_MULTICAST: {
  440. LinkedList3Node *list_node = LinkedList3Iterator_Next(&o->decide_multicast_it);
  441. if (!list_node) {
  442. o->decide_state = DECIDE_STATE_NONE;
  443. return NULL;
  444. }
  445. struct _FrameDecider_group_entry *group_entry = UPPER_OBJECT(list_node, struct _FrameDecider_group_entry, sig_list_node);
  446. return group_entry->peer;
  447. } break;
  448. default:
  449. ASSERT(0);
  450. return NULL;
  451. }
  452. }
  453. int FrameDeciderPeer_Init (FrameDeciderPeer *o, FrameDecider *d, void *user, BLog_logfunc logfunc)
  454. {
  455. // init arguments
  456. o->d = d;
  457. o->user = user;
  458. o->logfunc = logfunc;
  459. // allocate MAC entries
  460. if (!(o->mac_entries = BAllocArray(d->max_peer_macs, sizeof(struct _FrameDecider_mac_entry)))) {
  461. PeerLog(o, BLOG_ERROR, "failed to allocate MAC entries");
  462. goto fail0;
  463. }
  464. // allocate group entries
  465. if (!(o->group_entries = BAllocArray(d->max_peer_groups, sizeof(struct _FrameDecider_group_entry)))) {
  466. PeerLog(o, BLOG_ERROR, "failed to allocate group entries");
  467. goto fail1;
  468. }
  469. // insert to peers list
  470. LinkedList2_Append(&d->peers_list, &o->list_node);
  471. // init MAC entry lists
  472. LinkedList2_Init(&o->mac_entries_free);
  473. LinkedList2_Init(&o->mac_entries_used);
  474. // initialize MAC entries
  475. for (int i = 0; i < d->max_peer_macs; i++) {
  476. struct _FrameDecider_mac_entry *entry = &o->mac_entries[i];
  477. // set peer
  478. entry->peer = o;
  479. // insert to free list
  480. LinkedList2_Append(&o->mac_entries_free, &entry->list_node);
  481. }
  482. // init group entry lists
  483. LinkedList2_Init(&o->group_entries_free);
  484. LinkedList2_Init(&o->group_entries_used);
  485. // initialize group entries
  486. for (int i = 0; i < d->max_peer_groups; i++) {
  487. struct _FrameDecider_group_entry *entry = &o->group_entries[i];
  488. // set peer
  489. entry->peer = o;
  490. // insert to free list
  491. LinkedList2_Append(&o->group_entries_free, &entry->list_node);
  492. // init timer
  493. BTimer_Init(&entry->timer, 0, (BTimer_handler)group_entry_timer_handler, entry);
  494. }
  495. // initialize groups tree
  496. BAVL_Init(&o->groups_tree, OFFSET_DIFF(struct _FrameDecider_group_entry, group, tree_node), (BAVL_comparator)uint32_comparator, NULL);
  497. DebugObject_Init(&o->d_obj);
  498. return 1;
  499. fail1:
  500. BFree(o->mac_entries);
  501. fail0:
  502. return 0;
  503. }
  504. void FrameDeciderPeer_Free (FrameDeciderPeer *o)
  505. {
  506. DebugObject_Free(&o->d_obj);
  507. FrameDecider *d = o->d;
  508. // remove decide unicast reference
  509. if (d->decide_state == DECIDE_STATE_UNICAST && d->decide_unicast_peer == o) {
  510. d->decide_state = DECIDE_STATE_NONE;
  511. }
  512. LinkedList2Iterator it;
  513. LinkedList2Node *node;
  514. // free group entries
  515. LinkedList2Iterator_InitForward(&it, &o->group_entries_used);
  516. while (node = LinkedList2Iterator_Next(&it)) {
  517. struct _FrameDecider_group_entry *entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  518. // remove from multicast
  519. remove_from_multicast(d, entry);
  520. // stop timer
  521. BReactor_RemoveTimer(d->reactor, &entry->timer);
  522. }
  523. // remove used MAC entries from tree
  524. LinkedList2Iterator_InitForward(&it, &o->mac_entries_used);
  525. while (node = LinkedList2Iterator_Next(&it)) {
  526. struct _FrameDecider_mac_entry *entry = UPPER_OBJECT(node, struct _FrameDecider_mac_entry, list_node);
  527. // remove from tree
  528. FDMacsTree_Remove(&d->macs_tree, 0, FDMacsTreeDeref(0, entry));
  529. }
  530. // remove from peers list
  531. LinkedList2_Remove(&d->peers_list, &o->list_node);
  532. // free group entries
  533. BFree(o->group_entries);
  534. // free MAC entries
  535. BFree(o->mac_entries);
  536. }
  537. void FrameDeciderPeer_Analyze (FrameDeciderPeer *o, const uint8_t *frame, int frame_len)
  538. {
  539. ASSERT(frame_len >= 0)
  540. DebugObject_Access(&o->d_obj);
  541. const uint8_t *pos = frame;
  542. int len = frame_len;
  543. if (len < sizeof(struct ethernet_header)) {
  544. goto out;
  545. }
  546. struct ethernet_header *eh = (struct ethernet_header *)pos;
  547. pos += sizeof(struct ethernet_header);
  548. len -= sizeof(struct ethernet_header);
  549. // register source MAC address with this peer
  550. add_mac_to_peer(o, eh->source);
  551. switch (ntoh16(eh->type)) {
  552. case ETHERTYPE_IPV4: {
  553. // check IPv4 header
  554. struct ipv4_header *ipv4_header;
  555. if (!ipv4_check((uint8_t *)pos, len, &ipv4_header, (uint8_t **)&pos, &len)) {
  556. PeerLog(o, BLOG_INFO, "analyze: wrong IP packet");
  557. goto out;
  558. }
  559. // check if it's IGMP
  560. if (ntoh8(ipv4_header->protocol) != IPV4_PROTOCOL_IGMP) {
  561. goto out;
  562. }
  563. // check IGMP header
  564. if (len < sizeof(struct igmp_base)) {
  565. PeerLog(o, BLOG_INFO, "analyze: IGMP: short packet");
  566. goto out;
  567. }
  568. struct igmp_base *igmp_base = (struct igmp_base *)pos;
  569. pos += sizeof(struct igmp_base);
  570. len -= sizeof(struct igmp_base);
  571. switch (ntoh8(igmp_base->type)) {
  572. case IGMP_TYPE_V2_MEMBERSHIP_REPORT: {
  573. // check extra
  574. if (len < sizeof(struct igmp_v2_extra)) {
  575. PeerLog(o, BLOG_INFO, "analyze: IGMP: short v2 report");
  576. goto out;
  577. }
  578. struct igmp_v2_extra *report = (struct igmp_v2_extra *)pos;
  579. pos += sizeof(struct igmp_v2_extra);
  580. len -= sizeof(struct igmp_v2_extra);
  581. // add to group
  582. add_group_to_peer(o, report->group);
  583. } break;
  584. case IGMP_TYPE_V3_MEMBERSHIP_REPORT: {
  585. // check extra
  586. if (len < sizeof(struct igmp_v3_report_extra)) {
  587. PeerLog(o, BLOG_INFO, "analyze: IGMP: short v3 report");
  588. goto out;
  589. }
  590. struct igmp_v3_report_extra *report = (struct igmp_v3_report_extra *)pos;
  591. pos += sizeof(struct igmp_v3_report_extra);
  592. len -= sizeof(struct igmp_v3_report_extra);
  593. // iterate records
  594. uint16_t num_records = ntoh16(report->number_of_group_records);
  595. for (int i = 0; i < num_records; i++) {
  596. // check record
  597. if (len < sizeof(struct igmp_v3_report_record)) {
  598. PeerLog(o, BLOG_INFO, "analyze: IGMP: short record header");
  599. goto out;
  600. }
  601. struct igmp_v3_report_record *record = (struct igmp_v3_report_record *)pos;
  602. pos += sizeof(struct igmp_v3_report_record);
  603. len -= sizeof(struct igmp_v3_report_record);
  604. // iterate sources
  605. uint16_t num_sources = ntoh16(record->number_of_sources);
  606. int j;
  607. for (j = 0; j < num_sources; j++) {
  608. // check source
  609. if (len < sizeof(struct igmp_source)) {
  610. PeerLog(o, BLOG_INFO, "analyze: IGMP: short source");
  611. goto out;
  612. }
  613. struct igmp_source *source = (struct igmp_source *)pos;
  614. pos += sizeof(struct igmp_source);
  615. len -= sizeof(struct igmp_source);
  616. }
  617. // check aux data
  618. uint16_t aux_len = ntoh16(record->aux_data_len);
  619. if (len < aux_len) {
  620. PeerLog(o, BLOG_INFO, "analyze: IGMP: short record aux data");
  621. goto out;
  622. }
  623. pos += aux_len;
  624. len -= aux_len;
  625. switch (record->type) {
  626. case IGMP_RECORD_TYPE_MODE_IS_INCLUDE:
  627. case IGMP_RECORD_TYPE_CHANGE_TO_INCLUDE_MODE:
  628. if (num_sources != 0) {
  629. add_group_to_peer(o, record->group);
  630. }
  631. break;
  632. case IGMP_RECORD_TYPE_MODE_IS_EXCLUDE:
  633. case IGMP_RECORD_TYPE_CHANGE_TO_EXCLUDE_MODE:
  634. add_group_to_peer(o, record->group);
  635. break;
  636. }
  637. }
  638. } break;
  639. }
  640. } break;
  641. }
  642. out:;
  643. }