pim_zebra.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <zebra.h>
  19. #include "zebra/rib.h"
  20. #include "if.h"
  21. #include "log.h"
  22. #include "prefix.h"
  23. #include "zclient.h"
  24. #include "stream.h"
  25. #include "network.h"
  26. #include "pimd.h"
  27. #include "pim_pim.h"
  28. #include "pim_zebra.h"
  29. #include "pim_iface.h"
  30. #include "pim_str.h"
  31. #include "pim_oil.h"
  32. #include "pim_rpf.h"
  33. #include "pim_time.h"
  34. #include "pim_join.h"
  35. #include "pim_zlookup.h"
  36. #include "pim_ifchannel.h"
  37. #undef PIM_DEBUG_IFADDR_DUMP
  38. #define PIM_DEBUG_IFADDR_DUMP
  39. static int fib_lookup_if_vif_index(struct in_addr addr);
  40. static int del_oif(struct channel_oil *channel_oil,
  41. struct interface *oif,
  42. uint32_t proto_mask);
  43. static void zclient_broken(struct zclient *zclient)
  44. {
  45. struct listnode *ifnode;
  46. struct interface *ifp;
  47. zlog_warn("%s %s: broken zclient connection",
  48. __FILE__, __PRETTY_FUNCTION__);
  49. for (ALL_LIST_ELEMENTS_RO(iflist, ifnode, ifp)) {
  50. pim_if_addr_del_all(ifp);
  51. }
  52. /* upon return, zclient will discard connected addresses */
  53. }
  54. /* Router-id update message from zebra. */
  55. static int pim_router_id_update_zebra(int command, struct zclient *zclient,
  56. zebra_size_t length)
  57. {
  58. struct prefix router_id;
  59. zebra_router_id_update_read(zclient->ibuf, &router_id);
  60. return 0;
  61. }
  62. static int pim_zebra_if_add(int command, struct zclient *zclient,
  63. zebra_size_t length)
  64. {
  65. struct interface *ifp;
  66. /*
  67. zebra api adds/dels interfaces using the same call
  68. interface_add_read below, see comments in lib/zclient.c
  69. */
  70. ifp = zebra_interface_add_read(zclient->ibuf);
  71. if (!ifp)
  72. return 0;
  73. if (PIM_DEBUG_ZEBRA) {
  74. zlog_debug("%s: %s index %d flags %ld metric %d mtu %d operative %d",
  75. __PRETTY_FUNCTION__,
  76. ifp->name, ifp->ifindex, (long)ifp->flags, ifp->metric,
  77. ifp->mtu, if_is_operative(ifp));
  78. }
  79. if (if_is_operative(ifp))
  80. pim_if_addr_add_all(ifp);
  81. return 0;
  82. }
  83. static int pim_zebra_if_del(int command, struct zclient *zclient,
  84. zebra_size_t length)
  85. {
  86. struct interface *ifp;
  87. /*
  88. zebra api adds/dels interfaces using the same call
  89. interface_add_read below, see comments in lib/zclient.c
  90. */
  91. ifp = zebra_interface_add_read(zclient->ibuf);
  92. if (!ifp)
  93. return 0;
  94. if (PIM_DEBUG_ZEBRA) {
  95. zlog_debug("%s: %s index %d flags %ld metric %d mtu %d operative %d",
  96. __PRETTY_FUNCTION__,
  97. ifp->name, ifp->ifindex, (long)ifp->flags, ifp->metric,
  98. ifp->mtu, if_is_operative(ifp));
  99. }
  100. if (!if_is_operative(ifp))
  101. pim_if_addr_del_all(ifp);
  102. return 0;
  103. }
  104. static int pim_zebra_if_state_up(int command, struct zclient *zclient,
  105. zebra_size_t length)
  106. {
  107. struct interface *ifp;
  108. /*
  109. zebra api notifies interface up/down events by using the same call
  110. interface_add_read below, see comments in lib/zclient.c
  111. */
  112. ifp = zebra_interface_state_read(zclient->ibuf);
  113. if (!ifp)
  114. return 0;
  115. zlog_info("INTERFACE UP: %s", ifp->name);
  116. if (PIM_DEBUG_ZEBRA) {
  117. zlog_debug("%s: %s index %d flags %ld metric %d mtu %d operative %d",
  118. __PRETTY_FUNCTION__,
  119. ifp->name, ifp->ifindex, (long)ifp->flags, ifp->metric,
  120. ifp->mtu, if_is_operative(ifp));
  121. }
  122. if (if_is_operative(ifp)) {
  123. /*
  124. pim_if_addr_add_all() suffices for bringing up both IGMP and PIM
  125. */
  126. pim_if_addr_add_all(ifp);
  127. }
  128. return 0;
  129. }
  130. static int pim_zebra_if_state_down(int command, struct zclient *zclient,
  131. zebra_size_t length)
  132. {
  133. struct interface *ifp;
  134. /*
  135. zebra api notifies interface up/down events by using the same call
  136. interface_add_read below, see comments in lib/zclient.c
  137. */
  138. ifp = zebra_interface_state_read(zclient->ibuf);
  139. if (!ifp)
  140. return 0;
  141. zlog_info("INTERFACE DOWN: %s", ifp->name);
  142. if (PIM_DEBUG_ZEBRA) {
  143. zlog_debug("%s: %s index %d flags %ld metric %d mtu %d operative %d",
  144. __PRETTY_FUNCTION__,
  145. ifp->name, ifp->ifindex, (long)ifp->flags, ifp->metric,
  146. ifp->mtu, if_is_operative(ifp));
  147. }
  148. if (!if_is_operative(ifp)) {
  149. /*
  150. pim_if_addr_del_all() suffices for shutting down IGMP,
  151. but not for shutting down PIM
  152. */
  153. pim_if_addr_del_all(ifp);
  154. /*
  155. pim_sock_delete() closes the socket, stops read and timer threads,
  156. and kills all neighbors.
  157. */
  158. if (ifp->info) {
  159. pim_sock_delete(ifp, "link down");
  160. }
  161. }
  162. return 0;
  163. }
  164. #ifdef PIM_DEBUG_IFADDR_DUMP
  165. static void dump_if_address(struct interface *ifp)
  166. {
  167. struct connected *ifc;
  168. struct listnode *node;
  169. zlog_debug("%s %s: interface %s addresses:",
  170. __FILE__, __PRETTY_FUNCTION__,
  171. ifp->name);
  172. for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
  173. struct prefix *p = ifc->address;
  174. if (p->family != AF_INET)
  175. continue;
  176. zlog_debug("%s %s: interface %s address %s %s",
  177. __FILE__, __PRETTY_FUNCTION__,
  178. ifp->name,
  179. inet_ntoa(p->u.prefix4),
  180. CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY) ?
  181. "secondary" : "primary");
  182. }
  183. }
  184. #endif
  185. static int pim_zebra_if_address_add(int command, struct zclient *zclient,
  186. zebra_size_t length)
  187. {
  188. struct connected *c;
  189. struct prefix *p;
  190. zassert(command == ZEBRA_INTERFACE_ADDRESS_ADD);
  191. /*
  192. zebra api notifies address adds/dels events by using the same call
  193. interface_add_read below, see comments in lib/zclient.c
  194. zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, ...)
  195. will add address to interface list by calling
  196. connected_add_by_prefix()
  197. */
  198. c = zebra_interface_address_read(command, zclient->ibuf);
  199. if (!c)
  200. return 0;
  201. p = c->address;
  202. if (p->family != AF_INET)
  203. return 0;
  204. if (PIM_DEBUG_ZEBRA) {
  205. char buf[BUFSIZ];
  206. prefix2str(p, buf, BUFSIZ);
  207. zlog_debug("%s: %s connected IP address %s flags %u %s",
  208. __PRETTY_FUNCTION__,
  209. c->ifp->name, buf, c->flags,
  210. CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY) ? "secondary" : "primary");
  211. #ifdef PIM_DEBUG_IFADDR_DUMP
  212. dump_if_address(c->ifp);
  213. #endif
  214. }
  215. if (!CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY)) {
  216. /* trying to add primary address */
  217. struct in_addr primary_addr = pim_find_primary_addr(c->ifp);
  218. if (primary_addr.s_addr != p->u.prefix4.s_addr) {
  219. /* but we had a primary address already */
  220. char buf[BUFSIZ];
  221. char old[100];
  222. prefix2str(p, buf, BUFSIZ);
  223. pim_inet4_dump("<old?>", primary_addr, old, sizeof(old));
  224. zlog_warn("%s: %s primary addr old=%s: forcing secondary flag on new=%s",
  225. __PRETTY_FUNCTION__,
  226. c->ifp->name, old, buf);
  227. SET_FLAG(c->flags, ZEBRA_IFA_SECONDARY);
  228. }
  229. }
  230. pim_if_addr_add(c);
  231. return 0;
  232. }
  233. static int pim_zebra_if_address_del(int command, struct zclient *client,
  234. zebra_size_t length)
  235. {
  236. struct connected *c;
  237. struct prefix *p;
  238. zassert(command == ZEBRA_INTERFACE_ADDRESS_DELETE);
  239. /*
  240. zebra api notifies address adds/dels events by using the same call
  241. interface_add_read below, see comments in lib/zclient.c
  242. zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, ...)
  243. will remove address from interface list by calling
  244. connected_delete_by_prefix()
  245. */
  246. c = zebra_interface_address_read(command, client->ibuf);
  247. if (!c)
  248. return 0;
  249. p = c->address;
  250. if (p->family != AF_INET)
  251. return 0;
  252. if (PIM_DEBUG_ZEBRA) {
  253. char buf[BUFSIZ];
  254. prefix2str(p, buf, BUFSIZ);
  255. zlog_debug("%s: %s disconnected IP address %s flags %u %s",
  256. __PRETTY_FUNCTION__,
  257. c->ifp->name, buf, c->flags,
  258. CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY) ? "secondary" : "primary");
  259. #ifdef PIM_DEBUG_IFADDR_DUMP
  260. dump_if_address(c->ifp);
  261. #endif
  262. }
  263. pim_if_addr_del(c, 0);
  264. return 0;
  265. }
  266. static void scan_upstream_rpf_cache()
  267. {
  268. struct listnode *up_node;
  269. struct listnode *up_nextnode;
  270. struct pim_upstream *up;
  271. for (ALL_LIST_ELEMENTS(qpim_upstream_list, up_node, up_nextnode, up)) {
  272. struct in_addr old_rpf_addr;
  273. enum pim_rpf_result rpf_result;
  274. rpf_result = pim_rpf_update(up, &old_rpf_addr);
  275. if (rpf_result == PIM_RPF_FAILURE)
  276. continue;
  277. if (rpf_result == PIM_RPF_CHANGED) {
  278. if (up->join_state == PIM_UPSTREAM_JOINED) {
  279. /*
  280. RFC 4601: 4.5.7. Sending (S,G) Join/Prune Messages
  281. Transitions from Joined State
  282. RPF'(S,G) changes not due to an Assert
  283. The upstream (S,G) state machine remains in Joined
  284. state. Send Join(S,G) to the new upstream neighbor, which is
  285. the new value of RPF'(S,G). Send Prune(S,G) to the old
  286. upstream neighbor, which is the old value of RPF'(S,G). Set
  287. the Join Timer (JT) to expire after t_periodic seconds.
  288. */
  289. /* send Prune(S,G) to the old upstream neighbor */
  290. pim_joinprune_send(up->rpf.source_nexthop.interface,
  291. old_rpf_addr,
  292. up->source_addr,
  293. up->group_addr,
  294. 0 /* prune */);
  295. /* send Join(S,G) to the current upstream neighbor */
  296. pim_joinprune_send(up->rpf.source_nexthop.interface,
  297. up->rpf.rpf_addr,
  298. up->source_addr,
  299. up->group_addr,
  300. 1 /* join */);
  301. pim_upstream_join_timer_restart(up);
  302. } /* up->join_state == PIM_UPSTREAM_JOINED */
  303. /* FIXME can join_desired actually be changed by pim_rpf_update()
  304. returning PIM_RPF_CHANGED ? */
  305. pim_upstream_update_join_desired(up);
  306. } /* PIM_RPF_CHANGED */
  307. } /* for (qpim_upstream_list) */
  308. }
  309. void pim_scan_oil()
  310. {
  311. struct listnode *node;
  312. struct listnode *nextnode;
  313. struct channel_oil *c_oil;
  314. qpim_scan_oil_last = pim_time_monotonic_sec();
  315. ++qpim_scan_oil_events;
  316. for (ALL_LIST_ELEMENTS(qpim_channel_oil_list, node, nextnode, c_oil)) {
  317. int old_vif_index;
  318. int input_iface_vif_index = fib_lookup_if_vif_index(c_oil->oil.mfcc_origin);
  319. if (input_iface_vif_index < 1) {
  320. char source_str[100];
  321. char group_str[100];
  322. pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  323. pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  324. zlog_warn("%s %s: could not find input interface for (S,G)=(%s,%s)",
  325. __FILE__, __PRETTY_FUNCTION__,
  326. source_str, group_str);
  327. continue;
  328. }
  329. if (input_iface_vif_index == c_oil->oil.mfcc_parent) {
  330. /* RPF unchanged */
  331. continue;
  332. }
  333. if (PIM_DEBUG_ZEBRA) {
  334. struct interface *old_iif = pim_if_find_by_vif_index(c_oil->oil.mfcc_parent);
  335. struct interface *new_iif = pim_if_find_by_vif_index(input_iface_vif_index);
  336. char source_str[100];
  337. char group_str[100];
  338. pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  339. pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  340. zlog_debug("%s %s: (S,G)=(%s,%s) input interface changed from %s vif_index=%d to %s vif_index=%d",
  341. __FILE__, __PRETTY_FUNCTION__,
  342. source_str, group_str,
  343. old_iif ? old_iif->name : "<old_iif?>", c_oil->oil.mfcc_parent,
  344. new_iif ? new_iif->name : "<new_iif?>", input_iface_vif_index);
  345. }
  346. /* new iif loops to existing oif ? */
  347. if (c_oil->oil.mfcc_ttls[input_iface_vif_index]) {
  348. struct interface *new_iif = pim_if_find_by_vif_index(input_iface_vif_index);
  349. if (PIM_DEBUG_ZEBRA) {
  350. char source_str[100];
  351. char group_str[100];
  352. pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  353. pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  354. zlog_debug("%s %s: (S,G)=(%s,%s) new iif loops to existing oif: %s vif_index=%d",
  355. __FILE__, __PRETTY_FUNCTION__,
  356. source_str, group_str,
  357. new_iif ? new_iif->name : "<new_iif?>", input_iface_vif_index);
  358. }
  359. del_oif(c_oil, new_iif, PIM_OIF_FLAG_PROTO_ANY);
  360. }
  361. /* update iif vif_index */
  362. old_vif_index = c_oil->oil.mfcc_parent;
  363. c_oil->oil.mfcc_parent = input_iface_vif_index;
  364. /* update kernel multicast forwarding cache (MFC) */
  365. if (pim_mroute_add(&c_oil->oil)) {
  366. /* just log warning */
  367. struct interface *old_iif = pim_if_find_by_vif_index(old_vif_index);
  368. struct interface *new_iif = pim_if_find_by_vif_index(input_iface_vif_index);
  369. char source_str[100];
  370. char group_str[100];
  371. pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  372. pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  373. zlog_warn("%s %s: (S,G)=(%s,%s) failure updating input interface from %s vif_index=%d to %s vif_index=%d",
  374. __FILE__, __PRETTY_FUNCTION__,
  375. source_str, group_str,
  376. old_iif ? old_iif->name : "<old_iif?>", c_oil->oil.mfcc_parent,
  377. new_iif ? new_iif->name : "<new_iif?>", input_iface_vif_index);
  378. continue;
  379. }
  380. } /* for (qpim_channel_oil_list) */
  381. }
  382. static int on_rpf_cache_refresh(struct thread *t)
  383. {
  384. zassert(t);
  385. zassert(qpim_rpf_cache_refresher);
  386. qpim_rpf_cache_refresher = 0;
  387. /* update PIM protocol state */
  388. scan_upstream_rpf_cache();
  389. /* update kernel multicast forwarding cache (MFC) */
  390. pim_scan_oil();
  391. qpim_rpf_cache_refresh_last = pim_time_monotonic_sec();
  392. ++qpim_rpf_cache_refresh_events;
  393. return 0;
  394. }
  395. static void sched_rpf_cache_refresh()
  396. {
  397. ++qpim_rpf_cache_refresh_requests;
  398. if (qpim_rpf_cache_refresher) {
  399. /* Refresh timer is already running */
  400. return;
  401. }
  402. /* Start refresh timer */
  403. if (PIM_DEBUG_ZEBRA) {
  404. zlog_debug("%s: triggering %ld msec timer",
  405. __PRETTY_FUNCTION__,
  406. qpim_rpf_cache_refresh_delay_msec);
  407. }
  408. THREAD_TIMER_MSEC_ON(master, qpim_rpf_cache_refresher,
  409. on_rpf_cache_refresh,
  410. 0, qpim_rpf_cache_refresh_delay_msec);
  411. }
  412. static int redist_read_ipv4_route(int command, struct zclient *zclient,
  413. zebra_size_t length)
  414. {
  415. struct stream *s;
  416. struct zapi_ipv4 api;
  417. unsigned long ifindex;
  418. struct in_addr nexthop;
  419. struct prefix_ipv4 p;
  420. int min_len = 4;
  421. if (length < min_len) {
  422. zlog_warn("%s %s: short buffer: length=%d min=%d",
  423. __FILE__, __PRETTY_FUNCTION__,
  424. length, min_len);
  425. return -1;
  426. }
  427. s = zclient->ibuf;
  428. ifindex = 0;
  429. nexthop.s_addr = 0;
  430. /* Type, flags, message. */
  431. api.type = stream_getc(s);
  432. api.flags = stream_getc(s);
  433. api.message = stream_getc(s);
  434. /* IPv4 prefix length. */
  435. memset(&p, 0, sizeof(struct prefix_ipv4));
  436. p.family = AF_INET;
  437. p.prefixlen = stream_getc(s);
  438. min_len +=
  439. PSIZE(p.prefixlen) +
  440. CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP) ? 5 : 0 +
  441. CHECK_FLAG(api.message, ZAPI_MESSAGE_IFINDEX) ? 5 : 0 +
  442. CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE) ? 1 : 0 +
  443. CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC) ? 4 : 0;
  444. if (PIM_DEBUG_ZEBRA) {
  445. zlog_debug("%s %s: length=%d min_len=%d flags=%s%s%s%s",
  446. __FILE__, __PRETTY_FUNCTION__,
  447. length, min_len,
  448. CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP) ? "nh" : "",
  449. CHECK_FLAG(api.message, ZAPI_MESSAGE_IFINDEX) ? " ifi" : "",
  450. CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE) ? " dist" : "",
  451. CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC) ? " metr" : "");
  452. }
  453. if (length < min_len) {
  454. zlog_warn("%s %s: short buffer: length=%d min_len=%d flags=%s%s%s%s",
  455. __FILE__, __PRETTY_FUNCTION__,
  456. length, min_len,
  457. CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP) ? "nh" : "",
  458. CHECK_FLAG(api.message, ZAPI_MESSAGE_IFINDEX) ? " ifi" : "",
  459. CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE) ? " dist" : "",
  460. CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC) ? " metr" : "");
  461. return -1;
  462. }
  463. /* IPv4 prefix. */
  464. stream_get(&p.prefix, s, PSIZE(p.prefixlen));
  465. /* Nexthop, ifindex, distance, metric. */
  466. if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
  467. api.nexthop_num = stream_getc(s);
  468. nexthop.s_addr = stream_get_ipv4(s);
  469. }
  470. if (CHECK_FLAG(api.message, ZAPI_MESSAGE_IFINDEX)) {
  471. api.ifindex_num = stream_getc(s);
  472. ifindex = stream_getl(s);
  473. }
  474. api.distance = CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE) ?
  475. stream_getc(s) :
  476. 0;
  477. api.metric = CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC) ?
  478. stream_getl(s) :
  479. 0;
  480. switch (command) {
  481. case ZEBRA_IPV4_ROUTE_ADD:
  482. if (PIM_DEBUG_ZEBRA) {
  483. char buf[2][INET_ADDRSTRLEN];
  484. zlog_debug("%s: add %s %s/%d "
  485. "nexthop %s ifindex %ld metric%s %u distance%s %u",
  486. __PRETTY_FUNCTION__,
  487. zebra_route_string(api.type),
  488. inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
  489. p.prefixlen,
  490. inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
  491. ifindex,
  492. CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC) ? "-recv" : "-miss",
  493. api.metric,
  494. CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE) ? "-recv" : "-miss",
  495. api.distance);
  496. }
  497. break;
  498. case ZEBRA_IPV4_ROUTE_DELETE:
  499. if (PIM_DEBUG_ZEBRA) {
  500. char buf[2][INET_ADDRSTRLEN];
  501. zlog_debug("%s: delete %s %s/%d "
  502. "nexthop %s ifindex %ld metric%s %u distance%s %u",
  503. __PRETTY_FUNCTION__,
  504. zebra_route_string(api.type),
  505. inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
  506. p.prefixlen,
  507. inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
  508. ifindex,
  509. CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC) ? "-recv" : "-miss",
  510. api.metric,
  511. CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE) ? "-recv" : "-miss",
  512. api.distance);
  513. }
  514. break;
  515. default:
  516. zlog_warn("%s: unknown command=%d", __PRETTY_FUNCTION__, command);
  517. return -1;
  518. }
  519. sched_rpf_cache_refresh();
  520. return 0;
  521. }
  522. void pim_zebra_init(char *zebra_sock_path)
  523. {
  524. int i;
  525. if (zebra_sock_path)
  526. zclient_serv_path_set(zebra_sock_path);
  527. #ifdef HAVE_TCP_ZEBRA
  528. zlog_notice("zclient update contacting ZEBRA daemon at socket TCP %s,%d", "127.0.0.1", ZEBRA_PORT);
  529. #else
  530. zlog_notice("zclient update contacting ZEBRA daemon at socket UNIX %s", zclient_serv_path_get());
  531. #endif
  532. /* Socket for receiving updates from Zebra daemon */
  533. qpim_zclient_update = zclient_new();
  534. qpim_zclient_update->zclient_broken = zclient_broken;
  535. qpim_zclient_update->router_id_update = pim_router_id_update_zebra;
  536. qpim_zclient_update->interface_add = pim_zebra_if_add;
  537. qpim_zclient_update->interface_delete = pim_zebra_if_del;
  538. qpim_zclient_update->interface_up = pim_zebra_if_state_up;
  539. qpim_zclient_update->interface_down = pim_zebra_if_state_down;
  540. qpim_zclient_update->interface_address_add = pim_zebra_if_address_add;
  541. qpim_zclient_update->interface_address_delete = pim_zebra_if_address_del;
  542. qpim_zclient_update->ipv4_route_add = redist_read_ipv4_route;
  543. qpim_zclient_update->ipv4_route_delete = redist_read_ipv4_route;
  544. zclient_init(qpim_zclient_update, ZEBRA_ROUTE_PIM);
  545. if (PIM_DEBUG_PIM_TRACE) {
  546. zlog_info("zclient_init cleared redistribution request");
  547. }
  548. zassert(qpim_zclient_update->redist_default == ZEBRA_ROUTE_PIM);
  549. /* Request all redistribution */
  550. for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
  551. if (i == qpim_zclient_update->redist_default)
  552. continue;
  553. qpim_zclient_update->redist[i] = 1;
  554. if (PIM_DEBUG_PIM_TRACE) {
  555. zlog_debug("%s: requesting redistribution for %s (%i)",
  556. __PRETTY_FUNCTION__, zebra_route_string(i), i);
  557. }
  558. }
  559. /* Request default information */
  560. qpim_zclient_update->default_information = 1;
  561. if (PIM_DEBUG_PIM_TRACE) {
  562. zlog_info("%s: requesting default information redistribution",
  563. __PRETTY_FUNCTION__);
  564. zlog_notice("%s: zclient update socket initialized",
  565. __PRETTY_FUNCTION__);
  566. }
  567. zassert(!qpim_zclient_lookup);
  568. qpim_zclient_lookup = zclient_lookup_new();
  569. zassert(qpim_zclient_lookup);
  570. }
  571. void igmp_anysource_forward_start(struct igmp_group *group)
  572. {
  573. /* Any source (*,G) is forwarded only if mode is EXCLUDE {empty} */
  574. zassert(group->group_filtermode_isexcl);
  575. zassert(listcount(group->group_source_list) < 1);
  576. if (PIM_DEBUG_IGMP_TRACE) {
  577. zlog_debug("%s %s: UNIMPLEMENTED",
  578. __FILE__, __PRETTY_FUNCTION__);
  579. }
  580. }
  581. void igmp_anysource_forward_stop(struct igmp_group *group)
  582. {
  583. /* Any source (*,G) is forwarded only if mode is EXCLUDE {empty} */
  584. zassert((!group->group_filtermode_isexcl) || (listcount(group->group_source_list) > 0));
  585. if (PIM_DEBUG_IGMP_TRACE) {
  586. zlog_debug("%s %s: UNIMPLEMENTED",
  587. __FILE__, __PRETTY_FUNCTION__);
  588. }
  589. }
  590. static int fib_lookup_if_vif_index(struct in_addr addr)
  591. {
  592. struct pim_zlookup_nexthop nexthop_tab[PIM_NEXTHOP_IFINDEX_TAB_SIZE];
  593. int num_ifindex;
  594. int vif_index;
  595. int first_ifindex;
  596. num_ifindex = zclient_lookup_nexthop(qpim_zclient_lookup, nexthop_tab,
  597. PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr,
  598. PIM_NEXTHOP_LOOKUP_MAX);
  599. if (num_ifindex < 1) {
  600. char addr_str[100];
  601. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  602. zlog_warn("%s %s: could not find nexthop ifindex for address %s",
  603. __FILE__, __PRETTY_FUNCTION__,
  604. addr_str);
  605. return -1;
  606. }
  607. first_ifindex = nexthop_tab[0].ifindex;
  608. if (num_ifindex > 1) {
  609. char addr_str[100];
  610. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  611. zlog_debug("%s %s: FIXME ignoring multiple nexthop ifindex'es num_ifindex=%d for address %s (using only ifindex=%d)",
  612. __FILE__, __PRETTY_FUNCTION__,
  613. num_ifindex, addr_str, first_ifindex);
  614. /* debug warning only, do not return */
  615. }
  616. if (PIM_DEBUG_ZEBRA) {
  617. char addr_str[100];
  618. pim_inet4_dump("<ifaddr?>", addr, addr_str, sizeof(addr_str));
  619. zlog_debug("%s %s: found nexthop ifindex=%d (interface %s) for address %s",
  620. __FILE__, __PRETTY_FUNCTION__,
  621. first_ifindex, ifindex2ifname(first_ifindex), addr_str);
  622. }
  623. vif_index = pim_if_find_vifindex_by_ifindex(first_ifindex);
  624. if (vif_index < 1) {
  625. char addr_str[100];
  626. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  627. zlog_warn("%s %s: low vif_index=%d < 1 nexthop for address %s",
  628. __FILE__, __PRETTY_FUNCTION__,
  629. vif_index, addr_str);
  630. return -2;
  631. }
  632. zassert(qpim_mroute_oif_highest_vif_index < MAXVIFS);
  633. if (vif_index > qpim_mroute_oif_highest_vif_index) {
  634. char addr_str[100];
  635. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  636. zlog_warn("%s %s: high vif_index=%d > highest_vif_index=%d nexthop for address %s",
  637. __FILE__, __PRETTY_FUNCTION__,
  638. vif_index, qpim_mroute_oif_highest_vif_index, addr_str);
  639. zlog_warn("%s %s: pim disabled on interface %s vif_index=%d ?",
  640. __FILE__, __PRETTY_FUNCTION__,
  641. ifindex2ifname(vif_index),
  642. vif_index);
  643. return -3;
  644. }
  645. return vif_index;
  646. }
  647. static int add_oif(struct channel_oil *channel_oil,
  648. struct interface *oif,
  649. uint32_t proto_mask)
  650. {
  651. struct pim_interface *pim_ifp;
  652. int old_ttl;
  653. zassert(channel_oil);
  654. pim_ifp = oif->info;
  655. if (PIM_DEBUG_MROUTE) {
  656. char group_str[100];
  657. char source_str[100];
  658. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  659. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  660. zlog_debug("%s %s: (S,G)=(%s,%s): proto_mask=%u OIF=%s vif_index=%d",
  661. __FILE__, __PRETTY_FUNCTION__,
  662. source_str, group_str,
  663. proto_mask, oif->name, pim_ifp->mroute_vif_index);
  664. }
  665. if (pim_ifp->mroute_vif_index < 1) {
  666. zlog_warn("%s %s: interface %s vif_index=%d < 1",
  667. __FILE__, __PRETTY_FUNCTION__,
  668. oif->name, pim_ifp->mroute_vif_index);
  669. return -1;
  670. }
  671. #ifdef PIM_ENFORCE_LOOPFREE_MFC
  672. /*
  673. Prevent creating MFC entry with OIF=IIF.
  674. This is a protection against implementation mistakes.
  675. PIM protocol implicitely ensures loopfree multicast topology.
  676. IGMP must be protected against adding looped MFC entries created
  677. by both source and receiver attached to the same interface. See
  678. TODO T22.
  679. */
  680. if (pim_ifp->mroute_vif_index == channel_oil->oil.mfcc_parent) {
  681. char group_str[100];
  682. char source_str[100];
  683. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  684. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  685. zlog_warn("%s %s: refusing protocol mask %u request for IIF=OIF=%s (vif_index=%d) for channel (S,G)=(%s,%s)",
  686. __FILE__, __PRETTY_FUNCTION__,
  687. proto_mask, oif->name, pim_ifp->mroute_vif_index,
  688. source_str, group_str);
  689. return -2;
  690. }
  691. #endif
  692. zassert(qpim_mroute_oif_highest_vif_index < MAXVIFS);
  693. zassert(pim_ifp->mroute_vif_index <= qpim_mroute_oif_highest_vif_index);
  694. /* Prevent single protocol from subscribing same interface to
  695. channel (S,G) multiple times */
  696. if (channel_oil->oif_flags[pim_ifp->mroute_vif_index] & proto_mask) {
  697. char group_str[100];
  698. char source_str[100];
  699. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  700. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  701. zlog_warn("%s %s: existing protocol mask %u requested OIF %s (vif_index=%d, min_ttl=%d) for channel (S,G)=(%s,%s)",
  702. __FILE__, __PRETTY_FUNCTION__,
  703. proto_mask, oif->name, pim_ifp->mroute_vif_index,
  704. channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index],
  705. source_str, group_str);
  706. return -3;
  707. }
  708. /* Allow other protocol to request subscription of same interface to
  709. channel (S,G) multiple times, by silently ignoring further
  710. requests */
  711. if (channel_oil->oif_flags[pim_ifp->mroute_vif_index] & PIM_OIF_FLAG_PROTO_ANY) {
  712. /* Check the OIF really exists before returning, and only log
  713. warning otherwise */
  714. if (channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] < 1) {
  715. char group_str[100];
  716. char source_str[100];
  717. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  718. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  719. zlog_warn("%s %s: new protocol mask %u requested nonexistent OIF %s (vif_index=%d, min_ttl=%d) for channel (S,G)=(%s,%s)",
  720. __FILE__, __PRETTY_FUNCTION__,
  721. proto_mask, oif->name, pim_ifp->mroute_vif_index,
  722. channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index],
  723. source_str, group_str);
  724. }
  725. return 0;
  726. }
  727. old_ttl = channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index];
  728. if (old_ttl > 0) {
  729. char group_str[100];
  730. char source_str[100];
  731. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  732. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  733. zlog_warn("%s %s: interface %s (vif_index=%d) is existing output for channel (S,G)=(%s,%s)",
  734. __FILE__, __PRETTY_FUNCTION__,
  735. oif->name, pim_ifp->mroute_vif_index,
  736. source_str, group_str);
  737. return -4;
  738. }
  739. channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] = PIM_MROUTE_MIN_TTL;
  740. if (pim_mroute_add(&channel_oil->oil)) {
  741. char group_str[100];
  742. char source_str[100];
  743. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  744. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  745. zlog_warn("%s %s: could not add output interface %s (vif_index=%d) for channel (S,G)=(%s,%s)",
  746. __FILE__, __PRETTY_FUNCTION__,
  747. oif->name, pim_ifp->mroute_vif_index,
  748. source_str, group_str);
  749. channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] = old_ttl;
  750. return -5;
  751. }
  752. channel_oil->oif_creation[pim_ifp->mroute_vif_index] = pim_time_monotonic_sec();
  753. ++channel_oil->oil_size;
  754. channel_oil->oif_flags[pim_ifp->mroute_vif_index] |= proto_mask;
  755. if (PIM_DEBUG_MROUTE) {
  756. char group_str[100];
  757. char source_str[100];
  758. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  759. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  760. zlog_debug("%s %s: (S,G)=(%s,%s): proto_mask=%u OIF=%s vif_index=%d: DONE",
  761. __FILE__, __PRETTY_FUNCTION__,
  762. source_str, group_str,
  763. proto_mask, oif->name, pim_ifp->mroute_vif_index);
  764. }
  765. return 0;
  766. }
  767. static int del_oif(struct channel_oil *channel_oil,
  768. struct interface *oif,
  769. uint32_t proto_mask)
  770. {
  771. struct pim_interface *pim_ifp;
  772. int old_ttl;
  773. zassert(channel_oil);
  774. pim_ifp = oif->info;
  775. zassert(pim_ifp->mroute_vif_index >= 1);
  776. zassert(qpim_mroute_oif_highest_vif_index < MAXVIFS);
  777. zassert(pim_ifp->mroute_vif_index <= qpim_mroute_oif_highest_vif_index);
  778. if (PIM_DEBUG_MROUTE) {
  779. char group_str[100];
  780. char source_str[100];
  781. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  782. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  783. zlog_debug("%s %s: (S,G)=(%s,%s): proto_mask=%u OIF=%s vif_index=%d",
  784. __FILE__, __PRETTY_FUNCTION__,
  785. source_str, group_str,
  786. proto_mask, oif->name, pim_ifp->mroute_vif_index);
  787. }
  788. /* Prevent single protocol from unsubscribing same interface from
  789. channel (S,G) multiple times */
  790. if (!(channel_oil->oif_flags[pim_ifp->mroute_vif_index] & proto_mask)) {
  791. char group_str[100];
  792. char source_str[100];
  793. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  794. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  795. zlog_warn("%s %s: nonexistent protocol mask %u removed OIF %s (vif_index=%d, min_ttl=%d) from channel (S,G)=(%s,%s)",
  796. __FILE__, __PRETTY_FUNCTION__,
  797. proto_mask, oif->name, pim_ifp->mroute_vif_index,
  798. channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index],
  799. source_str, group_str);
  800. return -2;
  801. }
  802. /* Mark that protocol is no longer interested in this OIF */
  803. channel_oil->oif_flags[pim_ifp->mroute_vif_index] &= ~proto_mask;
  804. /* Allow multiple protocols to unsubscribe same interface from
  805. channel (S,G) multiple times, by silently ignoring requests while
  806. there is at least one protocol interested in the channel */
  807. if (channel_oil->oif_flags[pim_ifp->mroute_vif_index] & PIM_OIF_FLAG_PROTO_ANY) {
  808. /* Check the OIF keeps existing before returning, and only log
  809. warning otherwise */
  810. if (channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] < 1) {
  811. char group_str[100];
  812. char source_str[100];
  813. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  814. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  815. zlog_warn("%s %s: protocol mask %u removing nonexistent OIF %s (vif_index=%d, min_ttl=%d) from channel (S,G)=(%s,%s)",
  816. __FILE__, __PRETTY_FUNCTION__,
  817. proto_mask, oif->name, pim_ifp->mroute_vif_index,
  818. channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index],
  819. source_str, group_str);
  820. }
  821. return 0;
  822. }
  823. old_ttl = channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index];
  824. if (old_ttl < 1) {
  825. char group_str[100];
  826. char source_str[100];
  827. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  828. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  829. zlog_warn("%s %s: interface %s (vif_index=%d) is not output for channel (S,G)=(%s,%s)",
  830. __FILE__, __PRETTY_FUNCTION__,
  831. oif->name, pim_ifp->mroute_vif_index,
  832. source_str, group_str);
  833. return -3;
  834. }
  835. channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] = 0;
  836. if (pim_mroute_add(&channel_oil->oil)) {
  837. char group_str[100];
  838. char source_str[100];
  839. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  840. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  841. zlog_warn("%s %s: could not remove output interface %s (vif_index=%d) from channel (S,G)=(%s,%s)",
  842. __FILE__, __PRETTY_FUNCTION__,
  843. oif->name, pim_ifp->mroute_vif_index,
  844. source_str, group_str);
  845. channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] = old_ttl;
  846. return -4;
  847. }
  848. --channel_oil->oil_size;
  849. if (channel_oil->oil_size < 1) {
  850. if (pim_mroute_del(&channel_oil->oil)) {
  851. /* just log a warning in case of failure */
  852. char group_str[100];
  853. char source_str[100];
  854. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  855. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  856. zlog_warn("%s %s: failure removing OIL for channel (S,G)=(%s,%s)",
  857. __FILE__, __PRETTY_FUNCTION__,
  858. source_str, group_str);
  859. }
  860. }
  861. if (PIM_DEBUG_MROUTE) {
  862. char group_str[100];
  863. char source_str[100];
  864. pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
  865. pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
  866. zlog_debug("%s %s: (S,G)=(%s,%s): proto_mask=%u OIF=%s vif_index=%d: DONE",
  867. __FILE__, __PRETTY_FUNCTION__,
  868. source_str, group_str,
  869. proto_mask, oif->name, pim_ifp->mroute_vif_index);
  870. }
  871. return 0;
  872. }
  873. void igmp_source_forward_start(struct igmp_source *source)
  874. {
  875. struct igmp_group *group;
  876. if (PIM_DEBUG_IGMP_TRACE) {
  877. char source_str[100];
  878. char group_str[100];
  879. pim_inet4_dump("<source?>", source->source_addr, source_str, sizeof(source_str));
  880. pim_inet4_dump("<group?>", source->source_group->group_addr, group_str, sizeof(group_str));
  881. zlog_debug("%s: (S,G)=(%s,%s) igmp_sock=%d oif=%s fwd=%d",
  882. __PRETTY_FUNCTION__,
  883. source_str, group_str,
  884. source->source_group->group_igmp_sock->fd,
  885. source->source_group->group_igmp_sock->interface->name,
  886. IGMP_SOURCE_TEST_FORWARDING(source->source_flags));
  887. }
  888. /* Prevent IGMP interface from installing multicast route multiple
  889. times */
  890. if (IGMP_SOURCE_TEST_FORWARDING(source->source_flags)) {
  891. return;
  892. }
  893. group = source->source_group;
  894. if (!source->source_channel_oil) {
  895. struct pim_interface *pim_oif;
  896. int input_iface_vif_index = fib_lookup_if_vif_index(source->source_addr);
  897. if (input_iface_vif_index < 1) {
  898. char source_str[100];
  899. pim_inet4_dump("<source?>", source->source_addr, source_str, sizeof(source_str));
  900. zlog_warn("%s %s: could not find input interface for source %s",
  901. __FILE__, __PRETTY_FUNCTION__,
  902. source_str);
  903. return;
  904. }
  905. /*
  906. Protect IGMP against adding looped MFC entries created by both
  907. source and receiver attached to the same interface. See TODO
  908. T22.
  909. */
  910. pim_oif = source->source_group->group_igmp_sock->interface->info;
  911. if (!pim_oif) {
  912. zlog_warn("%s: multicast not enabled on oif=%s ?",
  913. __PRETTY_FUNCTION__,
  914. source->source_group->group_igmp_sock->interface->name);
  915. return;
  916. }
  917. if (pim_oif->mroute_vif_index < 1) {
  918. zlog_warn("%s %s: oif=%s vif_index=%d < 1",
  919. __FILE__, __PRETTY_FUNCTION__,
  920. source->source_group->group_igmp_sock->interface->name,
  921. pim_oif->mroute_vif_index);
  922. return;
  923. }
  924. if (input_iface_vif_index == pim_oif->mroute_vif_index) {
  925. /* ignore request for looped MFC entry */
  926. if (PIM_DEBUG_IGMP_TRACE) {
  927. char source_str[100];
  928. char group_str[100];
  929. pim_inet4_dump("<source?>", source->source_addr, source_str, sizeof(source_str));
  930. pim_inet4_dump("<group?>", source->source_group->group_addr, group_str, sizeof(group_str));
  931. zlog_debug("%s: ignoring request for looped MFC entry (S,G)=(%s,%s): igmp_sock=%d oif=%s vif_index=%d",
  932. __PRETTY_FUNCTION__,
  933. source_str, group_str,
  934. source->source_group->group_igmp_sock->fd,
  935. source->source_group->group_igmp_sock->interface->name,
  936. input_iface_vif_index);
  937. }
  938. return;
  939. }
  940. source->source_channel_oil = pim_channel_oil_add(group->group_addr,
  941. source->source_addr,
  942. input_iface_vif_index);
  943. if (!source->source_channel_oil) {
  944. char group_str[100];
  945. char source_str[100];
  946. pim_inet4_dump("<group?>", group->group_addr, group_str, sizeof(group_str));
  947. pim_inet4_dump("<source?>", source->source_addr, source_str, sizeof(source_str));
  948. zlog_warn("%s %s: could not create OIL for channel (S,G)=(%s,%s)",
  949. __FILE__, __PRETTY_FUNCTION__,
  950. source_str, group_str);
  951. return;
  952. }
  953. }
  954. if (add_oif(source->source_channel_oil,
  955. group->group_igmp_sock->interface,
  956. PIM_OIF_FLAG_PROTO_IGMP)) {
  957. return;
  958. }
  959. /*
  960. Feed IGMPv3-gathered local membership information into PIM
  961. per-interface (S,G) state.
  962. */
  963. pim_ifchannel_local_membership_add(group->group_igmp_sock->interface,
  964. source->source_addr, group->group_addr);
  965. IGMP_SOURCE_DO_FORWARDING(source->source_flags);
  966. }
  967. void igmp_source_forward_stop(struct igmp_source *source)
  968. {
  969. struct igmp_group *group;
  970. if (PIM_DEBUG_IGMP_TRACE) {
  971. char source_str[100];
  972. char group_str[100];
  973. pim_inet4_dump("<source?>", source->source_addr, source_str, sizeof(source_str));
  974. pim_inet4_dump("<group?>", source->source_group->group_addr, group_str, sizeof(group_str));
  975. zlog_debug("%s: (S,G)=(%s,%s) igmp_sock=%d oif=%s fwd=%d",
  976. __PRETTY_FUNCTION__,
  977. source_str, group_str,
  978. source->source_group->group_igmp_sock->fd,
  979. source->source_group->group_igmp_sock->interface->name,
  980. IGMP_SOURCE_TEST_FORWARDING(source->source_flags));
  981. }
  982. /* Prevent IGMP interface from removing multicast route multiple
  983. times */
  984. if (!IGMP_SOURCE_TEST_FORWARDING(source->source_flags)) {
  985. return;
  986. }
  987. group = source->source_group;
  988. if (del_oif(source->source_channel_oil,
  989. group->group_igmp_sock->interface,
  990. PIM_OIF_FLAG_PROTO_IGMP)) {
  991. return;
  992. }
  993. /*
  994. Feed IGMPv3-gathered local membership information into PIM
  995. per-interface (S,G) state.
  996. */
  997. pim_ifchannel_local_membership_del(group->group_igmp_sock->interface,
  998. source->source_addr, group->group_addr);
  999. IGMP_SOURCE_DONT_FORWARDING(source->source_flags);
  1000. }
  1001. void pim_forward_start(struct pim_ifchannel *ch)
  1002. {
  1003. struct pim_upstream *up = ch->upstream;
  1004. if (PIM_DEBUG_PIM_TRACE) {
  1005. char source_str[100];
  1006. char group_str[100];
  1007. pim_inet4_dump("<source?>", ch->source_addr, source_str, sizeof(source_str));
  1008. pim_inet4_dump("<group?>", ch->group_addr, group_str, sizeof(group_str));
  1009. zlog_debug("%s: (S,G)=(%s,%s) oif=%s",
  1010. __PRETTY_FUNCTION__,
  1011. source_str, group_str, ch->interface->name);
  1012. }
  1013. if (!up->channel_oil) {
  1014. int input_iface_vif_index = fib_lookup_if_vif_index(up->source_addr);
  1015. if (input_iface_vif_index < 1) {
  1016. char source_str[100];
  1017. pim_inet4_dump("<source?>", up->source_addr, source_str, sizeof(source_str));
  1018. zlog_warn("%s %s: could not find input interface for source %s",
  1019. __FILE__, __PRETTY_FUNCTION__,
  1020. source_str);
  1021. return;
  1022. }
  1023. up->channel_oil = pim_channel_oil_add(up->group_addr, up->source_addr,
  1024. input_iface_vif_index);
  1025. if (!up->channel_oil) {
  1026. char group_str[100];
  1027. char source_str[100];
  1028. pim_inet4_dump("<group?>", up->group_addr, group_str, sizeof(group_str));
  1029. pim_inet4_dump("<source?>", up->source_addr, source_str, sizeof(source_str));
  1030. zlog_warn("%s %s: could not create OIL for channel (S,G)=(%s,%s)",
  1031. __FILE__, __PRETTY_FUNCTION__,
  1032. source_str, group_str);
  1033. return;
  1034. }
  1035. }
  1036. add_oif(up->channel_oil,
  1037. ch->interface,
  1038. PIM_OIF_FLAG_PROTO_PIM);
  1039. }
  1040. void pim_forward_stop(struct pim_ifchannel *ch)
  1041. {
  1042. struct pim_upstream *up = ch->upstream;
  1043. if (PIM_DEBUG_PIM_TRACE) {
  1044. char source_str[100];
  1045. char group_str[100];
  1046. pim_inet4_dump("<source?>", ch->source_addr, source_str, sizeof(source_str));
  1047. pim_inet4_dump("<group?>", ch->group_addr, group_str, sizeof(group_str));
  1048. zlog_debug("%s: (S,G)=(%s,%s) oif=%s",
  1049. __PRETTY_FUNCTION__,
  1050. source_str, group_str, ch->interface->name);
  1051. }
  1052. if (!up->channel_oil) {
  1053. char source_str[100];
  1054. char group_str[100];
  1055. pim_inet4_dump("<source?>", ch->source_addr, source_str, sizeof(source_str));
  1056. pim_inet4_dump("<group?>", ch->group_addr, group_str, sizeof(group_str));
  1057. zlog_warn("%s: (S,G)=(%s,%s) oif=%s missing channel OIL",
  1058. __PRETTY_FUNCTION__,
  1059. source_str, group_str, ch->interface->name);
  1060. return;
  1061. }
  1062. del_oif(up->channel_oil,
  1063. ch->interface,
  1064. PIM_OIF_FLAG_PROTO_PIM);
  1065. }