FrameDecider.c 29 KB

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