FrameDecider.c 28 KB

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