FrameDecider.c 28 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 <misc/debug.h>
  32. #include <misc/offset.h>
  33. #include <misc/balloc.h>
  34. #include <misc/ethernet_proto.h>
  35. #include <misc/ipv4_proto.h>
  36. #include <misc/igmp_proto.h>
  37. #include <misc/byteorder.h>
  38. #include <misc/compare.h>
  39. #include <misc/print_macros.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/SAvl_impl.h>
  54. #include "FrameDecider_groups_tree.h"
  55. #include <structure/SAvl_impl.h>
  56. #include "FrameDecider_multicast_tree.h"
  57. #include <structure/SAvl_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. struct _FrameDecider_mac_entry *e_entry = FDMacsTree_LookupExact(&d->macs_tree, 0, mac);
  63. if (e_entry) {
  64. if (e_entry->peer == o) {
  65. // this is our MAC; only move it to the end of the used list
  66. LinkedList1_Remove(&o->mac_entries_used, &e_entry->list_node);
  67. LinkedList1_Append(&o->mac_entries_used, &e_entry->list_node);
  68. return;
  69. }
  70. // some other peer has that MAC; disassociate it
  71. FDMacsTree_Remove(&d->macs_tree, 0, e_entry);
  72. LinkedList1_Remove(&e_entry->peer->mac_entries_used, &e_entry->list_node);
  73. LinkedList1_Append(&e_entry->peer->mac_entries_free, &e_entry->list_node);
  74. }
  75. // aquire MAC address entry, if there are no free ones reuse the oldest used one
  76. LinkedList1Node *list_node;
  77. struct _FrameDecider_mac_entry *entry;
  78. if (list_node = LinkedList1_GetFirst(&o->mac_entries_free)) {
  79. entry = UPPER_OBJECT(list_node, struct _FrameDecider_mac_entry, list_node);
  80. ASSERT(entry->peer == o)
  81. // remove from free
  82. LinkedList1_Remove(&o->mac_entries_free, &entry->list_node);
  83. } else {
  84. list_node = LinkedList1_GetFirst(&o->mac_entries_used);
  85. ASSERT(list_node)
  86. entry = UPPER_OBJECT(list_node, struct _FrameDecider_mac_entry, list_node);
  87. ASSERT(entry->peer == o)
  88. // remove from used
  89. FDMacsTree_Remove(&d->macs_tree, 0, entry);
  90. LinkedList1_Remove(&o->mac_entries_used, &entry->list_node);
  91. }
  92. 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]);
  93. // set MAC in entry
  94. memcpy(entry->mac, mac, sizeof(entry->mac));
  95. // add to used
  96. LinkedList1_Append(&o->mac_entries_used, &entry->list_node);
  97. int res = FDMacsTree_Insert(&d->macs_tree, 0, entry, NULL);
  98. ASSERT_EXECUTE(res)
  99. }
  100. static uint32_t compute_sig_for_group (uint32_t group)
  101. {
  102. return hton32(ntoh32(group)&0x7FFFFF);
  103. }
  104. static uint32_t compute_sig_for_mac (uint8_t *mac)
  105. {
  106. uint32_t sig;
  107. memcpy(&sig, mac + 2, 4);
  108. sig = hton32(ntoh32(sig)&0x7FFFFF);
  109. return sig;
  110. }
  111. static void add_to_multicast (FrameDecider *d, struct _FrameDecider_group_entry *group_entry)
  112. {
  113. // compute sig
  114. uint32_t sig = compute_sig_for_group(group_entry->group);
  115. struct _FrameDecider_group_entry *master = FDMulticastTree_LookupExact(&d->multicast_tree, 0, sig);
  116. if (master) {
  117. // use existing master
  118. ASSERT(master->is_master)
  119. // set not master
  120. group_entry->is_master = 0;
  121. // insert to list
  122. LinkedList3Node_InitAfter(&group_entry->sig_list_node, &master->sig_list_node);
  123. } else {
  124. // make this entry master
  125. // set master
  126. group_entry->is_master = 1;
  127. // set sig
  128. group_entry->master.sig = sig;
  129. // insert to multicast tree
  130. int res = FDMulticastTree_Insert(&d->multicast_tree, 0, group_entry, NULL);
  131. ASSERT_EXECUTE(res)
  132. // init list node
  133. LinkedList3Node_InitLonely(&group_entry->sig_list_node);
  134. }
  135. }
  136. static void remove_from_multicast (FrameDecider *d, struct _FrameDecider_group_entry *group_entry)
  137. {
  138. // compute sig
  139. uint32_t sig = compute_sig_for_group(group_entry->group);
  140. if (group_entry->is_master) {
  141. // remove master from multicast tree
  142. FDMulticastTree_Remove(&d->multicast_tree, 0, group_entry);
  143. if (!LinkedList3Node_IsLonely(&group_entry->sig_list_node)) {
  144. // at least one more group entry for this sig; make another entry the master
  145. // get an entry
  146. LinkedList3Node *list_node = LinkedList3Node_NextOrPrev(&group_entry->sig_list_node);
  147. struct _FrameDecider_group_entry *newmaster = UPPER_OBJECT(list_node, struct _FrameDecider_group_entry, sig_list_node);
  148. ASSERT(!newmaster->is_master)
  149. // set master
  150. newmaster->is_master = 1;
  151. // set sig
  152. newmaster->master.sig = sig;
  153. // insert to multicast tree
  154. int res = FDMulticastTree_Insert(&d->multicast_tree, 0, newmaster, NULL);
  155. ASSERT_EXECUTE(res)
  156. }
  157. }
  158. // free linked list node
  159. LinkedList3Node_Free(&group_entry->sig_list_node);
  160. }
  161. static void add_group_to_peer (FrameDeciderPeer *o, uint32_t group)
  162. {
  163. FrameDecider *d = o->d;
  164. struct _FrameDecider_group_entry *group_entry = FDGroupsTree_LookupExact(&o->groups_tree, 0, group);
  165. if (group_entry) {
  166. // move to end of used list
  167. LinkedList1_Remove(&o->group_entries_used, &group_entry->list_node);
  168. LinkedList1_Append(&o->group_entries_used, &group_entry->list_node);
  169. } else {
  170. PeerLog(o, BLOG_INFO, "joined group %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
  171. ((uint8_t *)&group)[0], ((uint8_t *)&group)[1], ((uint8_t *)&group)[2], ((uint8_t *)&group)[3]
  172. );
  173. // aquire group entry, if there are no free ones reuse the earliest used one
  174. LinkedList1Node *node;
  175. if (node = LinkedList1_GetFirst(&o->group_entries_free)) {
  176. group_entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  177. // remove from free list
  178. LinkedList1_Remove(&o->group_entries_free, &group_entry->list_node);
  179. } else {
  180. node = LinkedList1_GetFirst(&o->group_entries_used);
  181. ASSERT(node)
  182. group_entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  183. // remove from multicast
  184. remove_from_multicast(d, group_entry);
  185. // remove from peer's groups tree
  186. FDGroupsTree_Remove(&o->groups_tree, 0, group_entry);
  187. // remove from used list
  188. LinkedList1_Remove(&o->group_entries_used, &group_entry->list_node);
  189. }
  190. // add entry to used list
  191. LinkedList1_Append(&o->group_entries_used, &group_entry->list_node);
  192. // set group address
  193. group_entry->group = group;
  194. // insert to peer's groups tree
  195. int res = FDGroupsTree_Insert(&o->groups_tree, 0, group_entry, NULL);
  196. ASSERT_EXECUTE(res)
  197. // add to multicast
  198. add_to_multicast(d, group_entry);
  199. }
  200. // set timer
  201. group_entry->timer_endtime = btime_gettime() + d->igmp_group_membership_interval;
  202. BReactor_SetTimerAbsolute(d->reactor, &group_entry->timer, group_entry->timer_endtime);
  203. }
  204. static void remove_group_entry (struct _FrameDecider_group_entry *group_entry)
  205. {
  206. FrameDeciderPeer *peer = group_entry->peer;
  207. FrameDecider *d = peer->d;
  208. uint32_t group = group_entry->group;
  209. PeerLog(peer, BLOG_INFO, "left group %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
  210. ((uint8_t *)&group)[0], ((uint8_t *)&group)[1], ((uint8_t *)&group)[2], ((uint8_t *)&group)[3]
  211. );
  212. // remove from multicast
  213. remove_from_multicast(d, group_entry);
  214. // remove from peer's groups tree
  215. FDGroupsTree_Remove(&peer->groups_tree, 0, group_entry);
  216. // remove from used list
  217. LinkedList1_Remove(&peer->group_entries_used, &group_entry->list_node);
  218. // add to free list
  219. LinkedList1_Append(&peer->group_entries_free, &group_entry->list_node);
  220. // stop timer
  221. BReactor_RemoveTimer(d->reactor, &group_entry->timer);
  222. }
  223. static void lower_group_timers_to_lmqt (FrameDecider *d, uint32_t group)
  224. {
  225. // have to lower all the group timers of this group down to LMQT
  226. // compute sig
  227. uint32_t sig = compute_sig_for_group(group);
  228. // look up the sig in multicast tree
  229. struct _FrameDecider_group_entry *master = FDMulticastTree_LookupExact(&d->multicast_tree, 0, sig);
  230. if (!master) {
  231. return;
  232. }
  233. ASSERT(master->is_master)
  234. // iterate all group entries with this sig
  235. LinkedList3Iterator it;
  236. LinkedList3Iterator_Init(&it, LinkedList3Node_First(&master->sig_list_node), 1);
  237. LinkedList3Node *sig_list_node;
  238. while (sig_list_node = LinkedList3Iterator_Next(&it)) {
  239. struct _FrameDecider_group_entry *group_entry = UPPER_OBJECT(sig_list_node, struct _FrameDecider_group_entry, sig_list_node);
  240. // skip wrong groups
  241. if (group_entry->group != group) {
  242. continue;
  243. }
  244. // lower timer down to LMQT
  245. btime_t now = btime_gettime();
  246. if (group_entry->timer_endtime > now + d->igmp_last_member_query_time) {
  247. group_entry->timer_endtime = now + d->igmp_last_member_query_time;
  248. BReactor_SetTimerAbsolute(d->reactor, &group_entry->timer, group_entry->timer_endtime);
  249. }
  250. }
  251. }
  252. static void group_entry_timer_handler (struct _FrameDecider_group_entry *group_entry)
  253. {
  254. DebugObject_Access(&group_entry->peer->d_obj);
  255. remove_group_entry(group_entry);
  256. }
  257. 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)
  258. {
  259. ASSERT(max_peer_macs > 0)
  260. ASSERT(max_peer_groups > 0)
  261. // init arguments
  262. o->max_peer_macs = max_peer_macs;
  263. o->max_peer_groups = max_peer_groups;
  264. o->igmp_group_membership_interval = igmp_group_membership_interval;
  265. o->igmp_last_member_query_time = igmp_last_member_query_time;
  266. o->reactor = reactor;
  267. // init peers list
  268. LinkedList1_Init(&o->peers_list);
  269. // init MAC tree
  270. FDMacsTree_Init(&o->macs_tree);
  271. // init multicast tree
  272. FDMulticastTree_Init(&o->multicast_tree);
  273. // init decide state
  274. o->decide_state = DECIDE_STATE_NONE;
  275. // set no current flood peer
  276. o->decide_flood_current = NULL;
  277. DebugObject_Init(&o->d_obj);
  278. }
  279. void FrameDecider_Free (FrameDecider *o)
  280. {
  281. ASSERT(FDMulticastTree_IsEmpty(&o->multicast_tree))
  282. ASSERT(FDMacsTree_IsEmpty(&o->macs_tree))
  283. ASSERT(LinkedList1_IsEmpty(&o->peers_list))
  284. DebugObject_Free(&o->d_obj);
  285. }
  286. void FrameDecider_AnalyzeAndDecide (FrameDecider *o, const uint8_t *frame, int frame_len)
  287. {
  288. ASSERT(frame_len >= 0)
  289. DebugObject_Access(&o->d_obj);
  290. // reset decide state
  291. switch (o->decide_state) {
  292. case DECIDE_STATE_NONE:
  293. break;
  294. case DECIDE_STATE_UNICAST:
  295. break;
  296. case DECIDE_STATE_FLOOD:
  297. break;
  298. case DECIDE_STATE_MULTICAST:
  299. LinkedList3Iterator_Free(&o->decide_multicast_it);
  300. return;
  301. default:
  302. ASSERT(0);
  303. }
  304. o->decide_state = DECIDE_STATE_NONE;
  305. o->decide_flood_current = NULL;
  306. // analyze frame
  307. const uint8_t *pos = frame;
  308. int len = frame_len;
  309. if (len < sizeof(struct ethernet_header)) {
  310. return;
  311. }
  312. struct ethernet_header eh;
  313. memcpy(&eh, pos, sizeof(eh));
  314. pos += sizeof(struct ethernet_header);
  315. len -= sizeof(struct ethernet_header);
  316. int is_igmp = 0;
  317. switch (ntoh16(eh.type)) {
  318. case ETHERTYPE_IPV4: {
  319. // check IPv4 header
  320. struct ipv4_header ipv4_header;
  321. if (!ipv4_check((uint8_t *)pos, len, &ipv4_header, (uint8_t **)&pos, &len)) {
  322. BLog(BLOG_INFO, "decide: wrong IP packet");
  323. goto out;
  324. }
  325. // check if it's IGMP
  326. if (ntoh8(ipv4_header.protocol) != IPV4_PROTOCOL_IGMP) {
  327. goto out;
  328. }
  329. // remember that it's IGMP; we have to flood IGMP frames
  330. is_igmp = 1;
  331. // check IGMP header
  332. if (len < sizeof(struct igmp_base)) {
  333. BLog(BLOG_INFO, "decide: IGMP: short packet");
  334. goto out;
  335. }
  336. struct igmp_base igmp_base;
  337. memcpy(&igmp_base, pos, sizeof(igmp_base));
  338. pos += sizeof(struct igmp_base);
  339. len -= sizeof(struct igmp_base);
  340. switch (ntoh8(igmp_base.type)) {
  341. case IGMP_TYPE_MEMBERSHIP_QUERY: {
  342. if (len == sizeof(struct igmp_v2_extra) && ntoh8(igmp_base.max_resp_code) != 0) {
  343. // V2 query
  344. struct igmp_v2_extra query;
  345. memcpy(&query, pos, sizeof(query));
  346. pos += sizeof(struct igmp_v2_extra);
  347. len -= sizeof(struct igmp_v2_extra);
  348. if (ntoh32(query.group) != 0) {
  349. // got a Group-Specific Query, lower group timers to LMQT
  350. lower_group_timers_to_lmqt(o, query.group);
  351. }
  352. }
  353. else if (len >= sizeof(struct igmp_v3_query_extra)) {
  354. // V3 query
  355. struct igmp_v3_query_extra query;
  356. memcpy(&query, pos, sizeof(query));
  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. o->decide_flood_current = LinkedList1_GetFirst(&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. struct _FrameDecider_group_entry *master = FDMulticastTree_LookupExact(&o->multicast_tree, 0, sig);
  395. if (master) {
  396. ASSERT(master->is_master)
  397. o->decide_state = DECIDE_STATE_MULTICAST;
  398. LinkedList3Iterator_Init(&o->decide_multicast_it, LinkedList3Node_First(&master->sig_list_node), 1);
  399. }
  400. return;
  401. }
  402. // look for MAC entry
  403. struct _FrameDecider_mac_entry *entry = FDMacsTree_LookupExact(&o->macs_tree, 0, eh.dest);
  404. if (entry) {
  405. o->decide_state = DECIDE_STATE_UNICAST;
  406. o->decide_unicast_peer = entry->peer;
  407. return;
  408. }
  409. // unknown destination MAC, flood
  410. o->decide_state = DECIDE_STATE_FLOOD;
  411. o->decide_flood_current = LinkedList1_GetFirst(&o->peers_list);
  412. return;
  413. }
  414. FrameDeciderPeer * FrameDecider_NextDestination (FrameDecider *o)
  415. {
  416. DebugObject_Access(&o->d_obj);
  417. switch (o->decide_state) {
  418. case DECIDE_STATE_NONE: {
  419. return NULL;
  420. } break;
  421. case DECIDE_STATE_UNICAST: {
  422. o->decide_state = DECIDE_STATE_NONE;
  423. return o->decide_unicast_peer;
  424. } break;
  425. case DECIDE_STATE_FLOOD: {
  426. if (!o->decide_flood_current) {
  427. o->decide_state = DECIDE_STATE_NONE;
  428. return NULL;
  429. }
  430. LinkedList1Node *list_node = o->decide_flood_current;
  431. o->decide_flood_current = LinkedList1Node_Next(o->decide_flood_current);
  432. FrameDeciderPeer *peer = UPPER_OBJECT(list_node, FrameDeciderPeer, list_node);
  433. return peer;
  434. } break;
  435. case DECIDE_STATE_MULTICAST: {
  436. LinkedList3Node *list_node = LinkedList3Iterator_Next(&o->decide_multicast_it);
  437. if (!list_node) {
  438. o->decide_state = DECIDE_STATE_NONE;
  439. return NULL;
  440. }
  441. struct _FrameDecider_group_entry *group_entry = UPPER_OBJECT(list_node, struct _FrameDecider_group_entry, sig_list_node);
  442. return group_entry->peer;
  443. } break;
  444. default:
  445. ASSERT(0);
  446. return NULL;
  447. }
  448. }
  449. int FrameDeciderPeer_Init (FrameDeciderPeer *o, FrameDecider *d, void *user, BLog_logfunc logfunc)
  450. {
  451. // init arguments
  452. o->d = d;
  453. o->user = user;
  454. o->logfunc = logfunc;
  455. // allocate MAC entries
  456. if (!(o->mac_entries = (struct _FrameDecider_mac_entry *)BAllocArray(d->max_peer_macs, sizeof(struct _FrameDecider_mac_entry)))) {
  457. PeerLog(o, BLOG_ERROR, "failed to allocate MAC entries");
  458. goto fail0;
  459. }
  460. // allocate group entries
  461. if (!(o->group_entries = (struct _FrameDecider_group_entry *)BAllocArray(d->max_peer_groups, sizeof(struct _FrameDecider_group_entry)))) {
  462. PeerLog(o, BLOG_ERROR, "failed to allocate group entries");
  463. goto fail1;
  464. }
  465. // insert to peers list
  466. LinkedList1_Append(&d->peers_list, &o->list_node);
  467. // init MAC entry lists
  468. LinkedList1_Init(&o->mac_entries_free);
  469. LinkedList1_Init(&o->mac_entries_used);
  470. // initialize MAC entries
  471. for (int i = 0; i < d->max_peer_macs; i++) {
  472. struct _FrameDecider_mac_entry *entry = &o->mac_entries[i];
  473. // set peer
  474. entry->peer = o;
  475. // insert to free list
  476. LinkedList1_Append(&o->mac_entries_free, &entry->list_node);
  477. }
  478. // init group entry lists
  479. LinkedList1_Init(&o->group_entries_free);
  480. LinkedList1_Init(&o->group_entries_used);
  481. // initialize group entries
  482. for (int i = 0; i < d->max_peer_groups; i++) {
  483. struct _FrameDecider_group_entry *entry = &o->group_entries[i];
  484. // set peer
  485. entry->peer = o;
  486. // insert to free list
  487. LinkedList1_Append(&o->group_entries_free, &entry->list_node);
  488. // init timer
  489. BTimer_Init(&entry->timer, 0, (BTimer_handler)group_entry_timer_handler, entry);
  490. }
  491. // initialize groups tree
  492. FDGroupsTree_Init(&o->groups_tree);
  493. DebugObject_Init(&o->d_obj);
  494. return 1;
  495. fail1:
  496. BFree(o->mac_entries);
  497. fail0:
  498. return 0;
  499. }
  500. void FrameDeciderPeer_Free (FrameDeciderPeer *o)
  501. {
  502. DebugObject_Free(&o->d_obj);
  503. FrameDecider *d = o->d;
  504. // remove decide unicast reference
  505. if (d->decide_state == DECIDE_STATE_UNICAST && d->decide_unicast_peer == o) {
  506. d->decide_state = DECIDE_STATE_NONE;
  507. }
  508. LinkedList1Node *node;
  509. // free group entries
  510. for (node = LinkedList1_GetFirst(&o->group_entries_used); node; node = LinkedList1Node_Next(node)) {
  511. struct _FrameDecider_group_entry *entry = UPPER_OBJECT(node, struct _FrameDecider_group_entry, list_node);
  512. // remove from multicast
  513. remove_from_multicast(d, entry);
  514. // stop timer
  515. BReactor_RemoveTimer(d->reactor, &entry->timer);
  516. }
  517. // remove used MAC entries from tree
  518. for (node = LinkedList1_GetFirst(&o->mac_entries_used); node; node = LinkedList1Node_Next(node)) {
  519. struct _FrameDecider_mac_entry *entry = UPPER_OBJECT(node, struct _FrameDecider_mac_entry, list_node);
  520. // remove from tree
  521. FDMacsTree_Remove(&d->macs_tree, 0, entry);
  522. }
  523. // remove from peers list
  524. if (d->decide_flood_current == &o->list_node) {
  525. d->decide_flood_current = LinkedList1Node_Next(d->decide_flood_current);
  526. }
  527. LinkedList1_Remove(&d->peers_list, &o->list_node);
  528. // free group entries
  529. BFree(o->group_entries);
  530. // free MAC entries
  531. BFree(o->mac_entries);
  532. }
  533. void FrameDeciderPeer_Analyze (FrameDeciderPeer *o, const uint8_t *frame, int frame_len)
  534. {
  535. ASSERT(frame_len >= 0)
  536. DebugObject_Access(&o->d_obj);
  537. const uint8_t *pos = frame;
  538. int len = frame_len;
  539. if (len < sizeof(struct ethernet_header)) {
  540. goto out;
  541. }
  542. struct ethernet_header eh;
  543. memcpy(&eh, pos, sizeof(eh));
  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;
  566. memcpy(&igmp_base, pos, sizeof(igmp_base));
  567. pos += sizeof(struct igmp_base);
  568. len -= sizeof(struct igmp_base);
  569. switch (ntoh8(igmp_base.type)) {
  570. case IGMP_TYPE_V2_MEMBERSHIP_REPORT: {
  571. // check extra
  572. if (len < sizeof(struct igmp_v2_extra)) {
  573. PeerLog(o, BLOG_INFO, "analyze: IGMP: short v2 report");
  574. goto out;
  575. }
  576. struct igmp_v2_extra report;
  577. memcpy(&report, pos, sizeof(report));
  578. pos += sizeof(struct igmp_v2_extra);
  579. len -= sizeof(struct igmp_v2_extra);
  580. // add to group
  581. add_group_to_peer(o, report.group);
  582. } break;
  583. case IGMP_TYPE_V3_MEMBERSHIP_REPORT: {
  584. // check extra
  585. if (len < sizeof(struct igmp_v3_report_extra)) {
  586. PeerLog(o, BLOG_INFO, "analyze: IGMP: short v3 report");
  587. goto out;
  588. }
  589. struct igmp_v3_report_extra report;
  590. memcpy(&report, pos, sizeof(report));
  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;
  602. memcpy(&record, pos, sizeof(record));
  603. pos += sizeof(struct igmp_v3_report_record);
  604. len -= sizeof(struct igmp_v3_report_record);
  605. // iterate sources
  606. uint16_t num_sources = ntoh16(record.number_of_sources);
  607. int j;
  608. for (j = 0; j < num_sources; j++) {
  609. // check source
  610. if (len < sizeof(struct igmp_source)) {
  611. PeerLog(o, BLOG_INFO, "analyze: IGMP: short source");
  612. goto out;
  613. }
  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. }