pim_zebra.c 41 KB

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