pim_iface.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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 "if.h"
  20. #include "log.h"
  21. #include "vty.h"
  22. #include "memory.h"
  23. #include "prefix.h"
  24. #include "pimd.h"
  25. #include "pim_iface.h"
  26. #include "pim_igmp.h"
  27. #include "pim_mroute.h"
  28. #include "pim_oil.h"
  29. #include "pim_str.h"
  30. #include "pim_pim.h"
  31. #include "pim_neighbor.h"
  32. #include "pim_ifchannel.h"
  33. #include "pim_rand.h"
  34. #include "pim_sock.h"
  35. #include "pim_time.h"
  36. #include "pim_ssmpingd.h"
  37. static void pim_if_igmp_join_del_all(struct interface *ifp);
  38. void pim_if_init()
  39. {
  40. if_init();
  41. }
  42. static void *if_list_clean(struct pim_interface *pim_ifp)
  43. {
  44. if (pim_ifp->igmp_join_list) {
  45. list_delete(pim_ifp->igmp_join_list);
  46. }
  47. if (pim_ifp->igmp_socket_list) {
  48. list_delete(pim_ifp->igmp_socket_list);
  49. }
  50. if (pim_ifp->pim_neighbor_list) {
  51. list_delete(pim_ifp->pim_neighbor_list);
  52. }
  53. if (pim_ifp->pim_ifchannel_list) {
  54. list_delete(pim_ifp->pim_ifchannel_list);
  55. }
  56. XFREE(MTYPE_PIM_INTERFACE, pim_ifp);
  57. return 0;
  58. }
  59. struct pim_interface *pim_if_new(struct interface *ifp, int igmp, int pim)
  60. {
  61. struct pim_interface *pim_ifp;
  62. zassert(ifp);
  63. zassert(!ifp->info);
  64. pim_ifp = XMALLOC(MTYPE_PIM_INTERFACE, sizeof(*pim_ifp));
  65. if (!pim_ifp) {
  66. zlog_err("PIM XMALLOC(%zu) failure", sizeof(*pim_ifp));
  67. return 0;
  68. }
  69. pim_ifp->options = 0;
  70. pim_ifp->mroute_vif_index = -1;
  71. pim_ifp->igmp_default_robustness_variable = IGMP_DEFAULT_ROBUSTNESS_VARIABLE;
  72. pim_ifp->igmp_default_query_interval = IGMP_GENERAL_QUERY_INTERVAL;
  73. pim_ifp->igmp_query_max_response_time_dsec = IGMP_QUERY_MAX_RESPONSE_TIME_DSEC;
  74. pim_ifp->igmp_specific_query_max_response_time_dsec = IGMP_SPECIFIC_QUERY_MAX_RESPONSE_TIME_DSEC;
  75. /*
  76. RFC 3376: 8.3. Query Response Interval
  77. The number of seconds represented by the [Query Response Interval]
  78. must be less than the [Query Interval].
  79. */
  80. zassert(pim_ifp->igmp_query_max_response_time_dsec < pim_ifp->igmp_default_query_interval);
  81. if (pim)
  82. PIM_IF_DO_PIM(pim_ifp->options);
  83. if (igmp)
  84. PIM_IF_DO_IGMP(pim_ifp->options);
  85. #if 0
  86. /* FIXME: Should join? */
  87. PIM_IF_DO_IGMP_LISTEN_ALLROUTERS(pim_ifp->options);
  88. #endif
  89. pim_ifp->igmp_join_list = 0;
  90. pim_ifp->igmp_socket_list = 0;
  91. pim_ifp->pim_neighbor_list = 0;
  92. pim_ifp->pim_ifchannel_list = 0;
  93. /* list of struct igmp_sock */
  94. pim_ifp->igmp_socket_list = list_new();
  95. if (!pim_ifp->igmp_socket_list) {
  96. zlog_err("%s %s: failure: igmp_socket_list=list_new()",
  97. __FILE__, __PRETTY_FUNCTION__);
  98. return if_list_clean(pim_ifp);
  99. }
  100. pim_ifp->igmp_socket_list->del = (void (*)(void *)) igmp_sock_free;
  101. /* list of struct pim_neighbor */
  102. pim_ifp->pim_neighbor_list = list_new();
  103. if (!pim_ifp->pim_neighbor_list) {
  104. zlog_err("%s %s: failure: pim_neighbor_list=list_new()",
  105. __FILE__, __PRETTY_FUNCTION__);
  106. return if_list_clean(pim_ifp);
  107. }
  108. pim_ifp->pim_neighbor_list->del = (void (*)(void *)) pim_neighbor_free;
  109. /* list of struct pim_ifchannel */
  110. pim_ifp->pim_ifchannel_list = list_new();
  111. if (!pim_ifp->pim_ifchannel_list) {
  112. zlog_err("%s %s: failure: pim_ifchannel_list=list_new()",
  113. __FILE__, __PRETTY_FUNCTION__);
  114. return if_list_clean(pim_ifp);
  115. }
  116. pim_ifp->pim_ifchannel_list->del = (void (*)(void *)) pim_ifchannel_free;
  117. ifp->info = pim_ifp;
  118. pim_sock_reset(ifp);
  119. zassert(PIM_IF_TEST_PIM(pim_ifp->options) || PIM_IF_TEST_IGMP(pim_ifp->options));
  120. if (PIM_MROUTE_IS_ENABLED) {
  121. pim_if_add_vif(ifp);
  122. }
  123. return pim_ifp;
  124. }
  125. void pim_if_delete(struct interface *ifp)
  126. {
  127. struct pim_interface *pim_ifp;
  128. zassert(ifp);
  129. pim_ifp = ifp->info;
  130. zassert(pim_ifp);
  131. if (pim_ifp->igmp_join_list) {
  132. pim_if_igmp_join_del_all(ifp);
  133. }
  134. zassert(!pim_ifp->igmp_join_list);
  135. zassert(pim_ifp->igmp_socket_list);
  136. zassert(!listcount(pim_ifp->igmp_socket_list));
  137. zassert(pim_ifp->pim_neighbor_list);
  138. zassert(!listcount(pim_ifp->pim_neighbor_list));
  139. zassert(pim_ifp->pim_ifchannel_list);
  140. zassert(!listcount(pim_ifp->pim_ifchannel_list));
  141. if (PIM_MROUTE_IS_ENABLED) {
  142. pim_if_del_vif(ifp);
  143. }
  144. list_delete(pim_ifp->igmp_socket_list);
  145. list_delete(pim_ifp->pim_neighbor_list);
  146. list_delete(pim_ifp->pim_ifchannel_list);
  147. XFREE(MTYPE_PIM_INTERFACE, pim_ifp);
  148. ifp->info = 0;
  149. }
  150. void pim_if_update_could_assert(struct interface *ifp)
  151. {
  152. struct pim_interface *pim_ifp;
  153. struct listnode *node;
  154. struct listnode *next_node;
  155. struct pim_ifchannel *ch;
  156. pim_ifp = ifp->info;
  157. zassert(pim_ifp);
  158. for (ALL_LIST_ELEMENTS(pim_ifp->pim_ifchannel_list, node, next_node, ch)) {
  159. pim_ifchannel_update_could_assert(ch);
  160. }
  161. }
  162. static void pim_if_update_my_assert_metric(struct interface *ifp)
  163. {
  164. struct pim_interface *pim_ifp;
  165. struct listnode *node;
  166. struct listnode *next_node;
  167. struct pim_ifchannel *ch;
  168. pim_ifp = ifp->info;
  169. zassert(pim_ifp);
  170. for (ALL_LIST_ELEMENTS(pim_ifp->pim_ifchannel_list, node, next_node, ch)) {
  171. pim_ifchannel_update_my_assert_metric(ch);
  172. }
  173. }
  174. static void pim_addr_change(struct interface *ifp)
  175. {
  176. struct pim_interface *pim_ifp;
  177. pim_ifp = ifp->info;
  178. zassert(pim_ifp);
  179. pim_if_dr_election(ifp); /* router's own DR Priority (addr) changes -- Done TODO T30 */
  180. pim_if_update_join_desired(pim_ifp); /* depends on DR */
  181. pim_if_update_could_assert(ifp); /* depends on DR */
  182. pim_if_update_my_assert_metric(ifp); /* depends on could_assert */
  183. pim_if_update_assert_tracking_desired(ifp); /* depends on DR, join_desired */
  184. /*
  185. RFC 4601: 4.3.1. Sending Hello Messages
  186. 1) Before an interface goes down or changes primary IP address, a
  187. Hello message with a zero HoldTime should be sent immediately
  188. (with the old IP address if the IP address changed).
  189. -- FIXME See CAVEAT C13
  190. 2) After an interface has changed its IP address, it MUST send a
  191. Hello message with its new IP address.
  192. -- DONE below
  193. 3) If an interface changes one of its secondary IP addresses, a
  194. Hello message with an updated Address_List option and a non-zero
  195. HoldTime should be sent immediately.
  196. -- FIXME See TODO T31
  197. */
  198. pim_ifp->pim_ifstat_hello_sent = 0; /* reset hello counter */
  199. if (pim_ifp->pim_sock_fd < 0)
  200. return;
  201. pim_hello_restart_now(ifp); /* send hello and restart timer */
  202. }
  203. static void on_primary_address_change(struct interface *ifp,
  204. const char *caller,
  205. struct in_addr old_addr,
  206. struct in_addr new_addr)
  207. {
  208. struct pim_interface *pim_ifp;
  209. {
  210. char old_str[100];
  211. char new_str[100];
  212. pim_inet4_dump("<old?>", old_addr, old_str, sizeof(old_str));
  213. pim_inet4_dump("<new?>", new_addr, new_str, sizeof(new_str));
  214. zlog_info("%s: %s: primary address changed from %s to %s on interface %s",
  215. __PRETTY_FUNCTION__, caller,
  216. old_str, new_str, ifp->name);
  217. }
  218. pim_ifp = ifp->info;
  219. if (!pim_ifp) {
  220. return;
  221. }
  222. if (!PIM_IF_TEST_PIM(pim_ifp->options)) {
  223. return;
  224. }
  225. pim_addr_change(ifp);
  226. }
  227. static int detect_primary_address_change(struct interface *ifp,
  228. int force_prim_as_any,
  229. const char *caller)
  230. {
  231. struct pim_interface *pim_ifp;
  232. struct in_addr new_prim_addr;
  233. int changed;
  234. pim_ifp = ifp->info;
  235. if (!pim_ifp)
  236. return 0;
  237. if (force_prim_as_any)
  238. new_prim_addr = qpim_inaddr_any;
  239. else
  240. new_prim_addr = pim_find_primary_addr(ifp);
  241. changed = new_prim_addr.s_addr != pim_ifp->primary_address.s_addr;
  242. if (PIM_DEBUG_ZEBRA) {
  243. char new_prim_str[100];
  244. char old_prim_str[100];
  245. pim_inet4_dump("<new?>", new_prim_addr, new_prim_str, sizeof(new_prim_str));
  246. pim_inet4_dump("<old?>", pim_ifp->primary_address, old_prim_str, sizeof(old_prim_str));
  247. zlog_debug("%s: old=%s new=%s on interface %s: %s",
  248. __PRETTY_FUNCTION__,
  249. old_prim_str, new_prim_str, ifp->name,
  250. changed ? "changed" : "unchanged");
  251. }
  252. if (changed) {
  253. struct in_addr old_addr = pim_ifp->primary_address;
  254. pim_ifp->primary_address = new_prim_addr;
  255. on_primary_address_change(ifp, caller, old_addr, new_prim_addr);
  256. }
  257. return changed;
  258. }
  259. static void detect_secondary_address_change(struct interface *ifp,
  260. const char *caller)
  261. {
  262. struct pim_interface *pim_ifp;
  263. int changed;
  264. pim_ifp = ifp->info;
  265. if (!pim_ifp)
  266. return;
  267. changed = 1; /* true */
  268. zlog_debug("FIXME T31 C15 %s: on interface %s: acting on any addr change",
  269. __PRETTY_FUNCTION__, ifp->name);
  270. if (PIM_DEBUG_ZEBRA) {
  271. zlog_debug("%s: on interface %s: %s",
  272. __PRETTY_FUNCTION__,
  273. ifp->name, changed ? "changed" : "unchanged");
  274. }
  275. if (!changed) {
  276. return;
  277. }
  278. if (!PIM_IF_TEST_PIM(pim_ifp->options)) {
  279. return;
  280. }
  281. pim_addr_change(ifp);
  282. }
  283. static void detect_address_change(struct interface *ifp,
  284. int force_prim_as_any,
  285. const char *caller)
  286. {
  287. int prim_changed;
  288. prim_changed = detect_primary_address_change(ifp, force_prim_as_any, caller);
  289. if (prim_changed) {
  290. /* no need to detect secondary change because
  291. the reaction would be the same */
  292. return;
  293. }
  294. detect_secondary_address_change(ifp, caller);
  295. }
  296. void pim_if_addr_add(struct connected *ifc)
  297. {
  298. struct pim_interface *pim_ifp;
  299. struct interface *ifp;
  300. struct in_addr ifaddr;
  301. zassert(ifc);
  302. ifp = ifc->ifp;
  303. zassert(ifp);
  304. pim_ifp = ifp->info;
  305. if (!pim_ifp)
  306. return;
  307. if (!if_is_operative(ifp))
  308. return;
  309. /* if (PIM_DEBUG_ZEBRA) */ {
  310. char buf[BUFSIZ];
  311. prefix2str(ifc->address, buf, BUFSIZ);
  312. zlog_debug("%s: %s ifindex=%d connected IP address %s %s",
  313. __PRETTY_FUNCTION__,
  314. ifp->name, ifp->ifindex, buf,
  315. CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY) ?
  316. "secondary" : "primary");
  317. }
  318. ifaddr = ifc->address->u.prefix4;
  319. detect_address_change(ifp, 0, __PRETTY_FUNCTION__);
  320. if (PIM_IF_TEST_IGMP(pim_ifp->options)) {
  321. struct igmp_sock *igmp;
  322. /* lookup IGMP socket */
  323. igmp = pim_igmp_sock_lookup_ifaddr(pim_ifp->igmp_socket_list,
  324. ifaddr);
  325. if (!igmp) {
  326. /* if addr new, add IGMP socket */
  327. pim_igmp_sock_add(pim_ifp->igmp_socket_list, ifaddr, ifp);
  328. }
  329. } /* igmp */
  330. if (PIM_IF_TEST_PIM(pim_ifp->options)) {
  331. /* Interface has a valid primary address ? */
  332. if (PIM_INADDR_ISNOT_ANY(pim_ifp->primary_address)) {
  333. /* Interface has a valid socket ? */
  334. if (pim_ifp->pim_sock_fd < 0) {
  335. if (pim_sock_add(ifp)) {
  336. zlog_warn("Failure creating PIM socket for interface %s",
  337. ifp->name);
  338. }
  339. }
  340. }
  341. } /* pim */
  342. if (PIM_MROUTE_IS_ENABLED) {
  343. /*
  344. PIM or IGMP is enabled on interface, and there is at least one
  345. address assigned, then try to create a vif_index.
  346. */
  347. if (pim_ifp->mroute_vif_index < 0) {
  348. pim_if_add_vif(ifp);
  349. }
  350. }
  351. }
  352. static void pim_if_addr_del_igmp(struct connected *ifc)
  353. {
  354. struct pim_interface *pim_ifp = ifc->ifp->info;
  355. struct igmp_sock *igmp;
  356. struct in_addr ifaddr;
  357. if (ifc->address->family != AF_INET) {
  358. /* non-IPv4 address */
  359. return;
  360. }
  361. if (!pim_ifp) {
  362. /* IGMP not enabled on interface */
  363. return;
  364. }
  365. ifaddr = ifc->address->u.prefix4;
  366. /* lookup IGMP socket */
  367. igmp = pim_igmp_sock_lookup_ifaddr(pim_ifp->igmp_socket_list,
  368. ifaddr);
  369. if (igmp) {
  370. /* if addr found, del IGMP socket */
  371. igmp_sock_delete(igmp);
  372. }
  373. }
  374. static void pim_if_addr_del_pim(struct connected *ifc)
  375. {
  376. struct pim_interface *pim_ifp = ifc->ifp->info;
  377. if (ifc->address->family != AF_INET) {
  378. /* non-IPv4 address */
  379. return;
  380. }
  381. if (!pim_ifp) {
  382. /* PIM not enabled on interface */
  383. return;
  384. }
  385. if (PIM_INADDR_ISNOT_ANY(pim_ifp->primary_address)) {
  386. /* Interface keeps a valid primary address */
  387. return;
  388. }
  389. if (pim_ifp->pim_sock_fd < 0) {
  390. /* Interface does not hold a valid socket any longer */
  391. return;
  392. }
  393. /*
  394. pim_sock_delete() closes the socket, stops read and timer threads,
  395. and kills all neighbors.
  396. */
  397. pim_sock_delete(ifc->ifp, "last address has been removed from interface");
  398. }
  399. void pim_if_addr_del(struct connected *ifc, int force_prim_as_any)
  400. {
  401. struct interface *ifp;
  402. zassert(ifc);
  403. ifp = ifc->ifp;
  404. zassert(ifp);
  405. /* if (PIM_DEBUG_ZEBRA) */ {
  406. char buf[BUFSIZ];
  407. prefix2str(ifc->address, buf, BUFSIZ);
  408. zlog_debug("%s: %s ifindex=%d disconnected IP address %s %s",
  409. __PRETTY_FUNCTION__,
  410. ifp->name, ifp->ifindex, buf,
  411. CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY) ?
  412. "secondary" : "primary");
  413. }
  414. detect_address_change(ifp, force_prim_as_any, __PRETTY_FUNCTION__);
  415. pim_if_addr_del_igmp(ifc);
  416. pim_if_addr_del_pim(ifc);
  417. }
  418. void pim_if_addr_add_all(struct interface *ifp)
  419. {
  420. struct connected *ifc;
  421. struct listnode *node;
  422. struct listnode *nextnode;
  423. /* PIM/IGMP enabled ? */
  424. if (!ifp->info)
  425. return;
  426. for (ALL_LIST_ELEMENTS(ifp->connected, node, nextnode, ifc)) {
  427. struct prefix *p = ifc->address;
  428. if (p->family != AF_INET)
  429. continue;
  430. pim_if_addr_add(ifc);
  431. }
  432. }
  433. void pim_if_addr_del_all(struct interface *ifp)
  434. {
  435. struct connected *ifc;
  436. struct listnode *node;
  437. struct listnode *nextnode;
  438. /* PIM/IGMP enabled ? */
  439. if (!ifp->info)
  440. return;
  441. for (ALL_LIST_ELEMENTS(ifp->connected, node, nextnode, ifc)) {
  442. struct prefix *p = ifc->address;
  443. if (p->family != AF_INET)
  444. continue;
  445. pim_if_addr_del(ifc, 1 /* force_prim_as_any=true */);
  446. }
  447. }
  448. void pim_if_addr_del_all_igmp(struct interface *ifp)
  449. {
  450. struct connected *ifc;
  451. struct listnode *node;
  452. struct listnode *nextnode;
  453. /* PIM/IGMP enabled ? */
  454. if (!ifp->info)
  455. return;
  456. for (ALL_LIST_ELEMENTS(ifp->connected, node, nextnode, ifc)) {
  457. struct prefix *p = ifc->address;
  458. if (p->family != AF_INET)
  459. continue;
  460. pim_if_addr_del_igmp(ifc);
  461. }
  462. }
  463. void pim_if_addr_del_all_pim(struct interface *ifp)
  464. {
  465. struct connected *ifc;
  466. struct listnode *node;
  467. struct listnode *nextnode;
  468. /* PIM/IGMP enabled ? */
  469. if (!ifp->info)
  470. return;
  471. for (ALL_LIST_ELEMENTS(ifp->connected, node, nextnode, ifc)) {
  472. struct prefix *p = ifc->address;
  473. if (p->family != AF_INET)
  474. continue;
  475. pim_if_addr_del_pim(ifc);
  476. }
  477. }
  478. static struct in_addr find_first_nonsec_addr(struct interface *ifp)
  479. {
  480. struct connected *ifc;
  481. struct listnode *node;
  482. struct in_addr addr;
  483. for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
  484. struct prefix *p = ifc->address;
  485. if (p->family != AF_INET)
  486. continue;
  487. if (PIM_INADDR_IS_ANY(p->u.prefix4)) {
  488. zlog_warn("%s: null IPv4 address connected to interface %s",
  489. __PRETTY_FUNCTION__, ifp->name);
  490. continue;
  491. }
  492. if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
  493. continue;
  494. return p->u.prefix4;
  495. }
  496. addr.s_addr = PIM_NET_INADDR_ANY;
  497. return addr;
  498. }
  499. struct in_addr pim_find_primary_addr(struct interface *ifp)
  500. {
  501. return find_first_nonsec_addr(ifp);
  502. }
  503. /*
  504. pim_if_add_vif() uses ifindex as vif_index
  505. see also pim_if_find_vifindex_by_ifindex()
  506. */
  507. int pim_if_add_vif(struct interface *ifp)
  508. {
  509. struct pim_interface *pim_ifp = ifp->info;
  510. struct in_addr ifaddr;
  511. zassert(pim_ifp);
  512. if (pim_ifp->mroute_vif_index > 0) {
  513. zlog_warn("%s: vif_index=%d > 0 on interface %s ifindex=%d",
  514. __PRETTY_FUNCTION__,
  515. pim_ifp->mroute_vif_index, ifp->name, ifp->ifindex);
  516. return -1;
  517. }
  518. if (ifp->ifindex < 1) {
  519. zlog_warn("%s: ifindex=%d < 1 on interface %s",
  520. __PRETTY_FUNCTION__,
  521. ifp->ifindex, ifp->name);
  522. return -2;
  523. }
  524. if (ifp->ifindex >= MAXVIFS) {
  525. zlog_warn("%s: ifindex=%d >= MAXVIFS=%d on interface %s",
  526. __PRETTY_FUNCTION__,
  527. ifp->ifindex, MAXVIFS, ifp->name);
  528. return -3;
  529. }
  530. ifaddr = pim_ifp->primary_address;
  531. if (PIM_INADDR_IS_ANY(ifaddr)) {
  532. zlog_warn("%s: could not get address for interface %s ifindex=%d",
  533. __PRETTY_FUNCTION__,
  534. ifp->name, ifp->ifindex);
  535. return -4;
  536. }
  537. if (pim_mroute_add_vif(ifp->ifindex, ifaddr)) {
  538. /* pim_mroute_add_vif reported error */
  539. return -5;
  540. }
  541. pim_ifp->mroute_vif_index = ifp->ifindex;
  542. /*
  543. Update highest vif_index
  544. */
  545. if (pim_ifp->mroute_vif_index > qpim_mroute_oif_highest_vif_index) {
  546. qpim_mroute_oif_highest_vif_index = pim_ifp->mroute_vif_index;
  547. }
  548. return 0;
  549. }
  550. static int iflist_find_highest_vif_index()
  551. {
  552. struct listnode *ifnode;
  553. struct interface *ifp;
  554. struct pim_interface *pim_ifp;
  555. int highest_vif_index = -1;
  556. for (ALL_LIST_ELEMENTS_RO(iflist, ifnode, ifp)) {
  557. pim_ifp = ifp->info;
  558. if (!pim_ifp)
  559. continue;
  560. if (pim_ifp->mroute_vif_index > highest_vif_index) {
  561. highest_vif_index = pim_ifp->mroute_vif_index;
  562. }
  563. }
  564. return highest_vif_index;
  565. }
  566. int pim_if_del_vif(struct interface *ifp)
  567. {
  568. struct pim_interface *pim_ifp = ifp->info;
  569. int old_vif_index;
  570. if (pim_ifp->mroute_vif_index < 1) {
  571. zlog_warn("%s: vif_index=%d < 1 on interface %s ifindex=%d",
  572. __PRETTY_FUNCTION__,
  573. pim_ifp->mroute_vif_index, ifp->name, ifp->ifindex);
  574. return -1;
  575. }
  576. if (pim_mroute_del_vif(pim_ifp->mroute_vif_index)) {
  577. /* pim_mroute_del_vif reported error */
  578. return -2;
  579. }
  580. /*
  581. Update highest vif_index
  582. */
  583. /* save old vif_index in order to compare with highest below */
  584. old_vif_index = pim_ifp->mroute_vif_index;
  585. pim_ifp->mroute_vif_index = -1;
  586. if (old_vif_index == qpim_mroute_oif_highest_vif_index) {
  587. qpim_mroute_oif_highest_vif_index = iflist_find_highest_vif_index();
  588. }
  589. return 0;
  590. }
  591. void pim_if_add_vif_all()
  592. {
  593. struct listnode *ifnode;
  594. struct listnode *ifnextnode;
  595. struct interface *ifp;
  596. for (ALL_LIST_ELEMENTS(iflist, ifnode, ifnextnode, ifp)) {
  597. if (!ifp->info)
  598. continue;
  599. pim_if_add_vif(ifp);
  600. }
  601. }
  602. void pim_if_del_vif_all()
  603. {
  604. struct listnode *ifnode;
  605. struct listnode *ifnextnode;
  606. struct interface *ifp;
  607. for (ALL_LIST_ELEMENTS(iflist, ifnode, ifnextnode, ifp)) {
  608. if (!ifp->info)
  609. continue;
  610. pim_if_del_vif(ifp);
  611. }
  612. }
  613. struct interface *pim_if_find_by_vif_index(int vif_index)
  614. {
  615. struct listnode *ifnode;
  616. struct interface *ifp;
  617. for (ALL_LIST_ELEMENTS_RO(iflist, ifnode, ifp)) {
  618. if (ifp->info) {
  619. struct pim_interface *pim_ifp;
  620. pim_ifp = ifp->info;
  621. if (vif_index == pim_ifp->mroute_vif_index)
  622. return ifp;
  623. }
  624. }
  625. return 0;
  626. }
  627. /*
  628. pim_if_add_vif() uses ifindex as vif_index
  629. */
  630. int pim_if_find_vifindex_by_ifindex(int ifindex)
  631. {
  632. return ifindex;
  633. }
  634. int pim_if_lan_delay_enabled(struct interface *ifp)
  635. {
  636. struct pim_interface *pim_ifp;
  637. pim_ifp = ifp->info;
  638. zassert(pim_ifp);
  639. zassert(pim_ifp->pim_number_of_nonlandelay_neighbors >= 0);
  640. return pim_ifp->pim_number_of_nonlandelay_neighbors == 0;
  641. }
  642. uint16_t pim_if_effective_propagation_delay_msec(struct interface *ifp)
  643. {
  644. if (pim_if_lan_delay_enabled(ifp)) {
  645. struct pim_interface *pim_ifp;
  646. pim_ifp = ifp->info;
  647. return pim_ifp->pim_neighbors_highest_propagation_delay_msec;
  648. }
  649. else {
  650. return PIM_DEFAULT_PROPAGATION_DELAY_MSEC;
  651. }
  652. }
  653. uint16_t pim_if_effective_override_interval_msec(struct interface *ifp)
  654. {
  655. if (pim_if_lan_delay_enabled(ifp)) {
  656. struct pim_interface *pim_ifp;
  657. pim_ifp = ifp->info;
  658. return pim_ifp->pim_neighbors_highest_override_interval_msec;
  659. }
  660. else {
  661. return PIM_DEFAULT_OVERRIDE_INTERVAL_MSEC;
  662. }
  663. }
  664. int pim_if_t_override_msec(struct interface *ifp)
  665. {
  666. int effective_override_interval_msec;
  667. int t_override_msec;
  668. effective_override_interval_msec =
  669. pim_if_effective_override_interval_msec(ifp);
  670. t_override_msec = pim_rand_next(0, effective_override_interval_msec);
  671. return t_override_msec;
  672. }
  673. uint16_t pim_if_jp_override_interval_msec(struct interface *ifp)
  674. {
  675. return pim_if_effective_propagation_delay_msec(ifp) +
  676. pim_if_effective_override_interval_msec(ifp);
  677. }
  678. /*
  679. RFC 4601: 4.1.6. State Summarization Macros
  680. The function NBR( I, A ) uses information gathered through PIM Hello
  681. messages to map the IP address A of a directly connected PIM
  682. neighbor router on interface I to the primary IP address of the same
  683. router (Section 4.3.4). The primary IP address of a neighbor is the
  684. address that it uses as the source of its PIM Hello messages.
  685. */
  686. struct pim_neighbor *pim_if_find_neighbor(struct interface *ifp,
  687. struct in_addr addr)
  688. {
  689. struct listnode *neighnode;
  690. struct pim_neighbor *neigh;
  691. struct pim_interface *pim_ifp;
  692. zassert(ifp);
  693. pim_ifp = ifp->info;
  694. if (!pim_ifp) {
  695. zlog_warn("%s: multicast not enabled on interface %s",
  696. __PRETTY_FUNCTION__,
  697. ifp->name);
  698. return 0;
  699. }
  700. for (ALL_LIST_ELEMENTS_RO(pim_ifp->pim_neighbor_list, neighnode, neigh)) {
  701. /* primary address ? */
  702. if (neigh->source_addr.s_addr == addr.s_addr)
  703. return neigh;
  704. /* secondary address ? */
  705. if (pim_neighbor_find_secondary(neigh, addr))
  706. return neigh;
  707. }
  708. if (PIM_DEBUG_PIM_TRACE) {
  709. char addr_str[100];
  710. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  711. zlog_debug("%s: neighbor not found for address %s on interface %s",
  712. __PRETTY_FUNCTION__,
  713. addr_str, ifp->name);
  714. }
  715. return 0;
  716. }
  717. long pim_if_t_suppressed_msec(struct interface *ifp)
  718. {
  719. struct pim_interface *pim_ifp;
  720. long t_suppressed_msec;
  721. pim_ifp = ifp->info;
  722. zassert(pim_ifp);
  723. /* join suppression disabled ? */
  724. if (PIM_IF_TEST_PIM_CAN_DISABLE_JOIN_SUPRESSION(pim_ifp->options))
  725. return 0;
  726. /* t_suppressed = t_periodic * rand(1.1, 1.4) */
  727. t_suppressed_msec = qpim_t_periodic * pim_rand_next(1100, 1400);
  728. return t_suppressed_msec;
  729. }
  730. static void igmp_join_free(struct igmp_join *ij)
  731. {
  732. XFREE(MTYPE_PIM_IGMP_JOIN, ij);
  733. }
  734. static struct igmp_join *igmp_join_find(struct list *join_list,
  735. struct in_addr group_addr,
  736. struct in_addr source_addr)
  737. {
  738. struct listnode *node;
  739. struct igmp_join *ij;
  740. zassert(join_list);
  741. for (ALL_LIST_ELEMENTS_RO(join_list, node, ij)) {
  742. if ((group_addr.s_addr == ij->group_addr.s_addr) &&
  743. (source_addr.s_addr == ij->source_addr.s_addr))
  744. return ij;
  745. }
  746. return 0;
  747. }
  748. static int igmp_join_sock(const char *ifname,
  749. int ifindex,
  750. struct in_addr group_addr,
  751. struct in_addr source_addr)
  752. {
  753. int join_fd;
  754. join_fd = pim_socket_raw(IPPROTO_IGMP);
  755. if (join_fd < 0) {
  756. return -1;
  757. }
  758. if (pim_socket_join_source(join_fd, ifindex, group_addr, source_addr, ifname)) {
  759. close(join_fd);
  760. return -2;
  761. }
  762. return join_fd;
  763. }
  764. static struct igmp_join *igmp_join_new(struct interface *ifp,
  765. struct in_addr group_addr,
  766. struct in_addr source_addr)
  767. {
  768. struct pim_interface *pim_ifp;
  769. struct igmp_join *ij;
  770. int join_fd;
  771. pim_ifp = ifp->info;
  772. zassert(pim_ifp);
  773. join_fd = igmp_join_sock(ifp->name, ifp->ifindex, group_addr, source_addr);
  774. if (join_fd < 0) {
  775. char group_str[100];
  776. char source_str[100];
  777. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  778. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  779. zlog_warn("%s: igmp_join_sock() failure for IGMP group %s source %s on interface %s",
  780. __PRETTY_FUNCTION__,
  781. group_str, source_str, ifp->name);
  782. return 0;
  783. }
  784. ij = XMALLOC(MTYPE_PIM_IGMP_JOIN, sizeof(*ij));
  785. if (!ij) {
  786. char group_str[100];
  787. char source_str[100];
  788. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  789. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  790. zlog_err("%s: XMALLOC(%zu) failure for IGMP group %s source %s on interface %s",
  791. __PRETTY_FUNCTION__,
  792. sizeof(*ij), group_str, source_str, ifp->name);
  793. close(join_fd);
  794. return 0;
  795. }
  796. ij->sock_fd = join_fd;
  797. ij->group_addr = group_addr;
  798. ij->source_addr = source_addr;
  799. ij->sock_creation = pim_time_monotonic_sec();
  800. listnode_add(pim_ifp->igmp_join_list, ij);
  801. return ij;
  802. }
  803. int pim_if_igmp_join_add(struct interface *ifp,
  804. struct in_addr group_addr,
  805. struct in_addr source_addr)
  806. {
  807. struct pim_interface *pim_ifp;
  808. struct igmp_join *ij;
  809. pim_ifp = ifp->info;
  810. if (!pim_ifp) {
  811. zlog_warn("%s: multicast not enabled on interface %s",
  812. __PRETTY_FUNCTION__,
  813. ifp->name);
  814. return -1;
  815. }
  816. if (!pim_ifp->igmp_join_list) {
  817. pim_ifp->igmp_join_list = list_new();
  818. if (!pim_ifp->igmp_join_list) {
  819. zlog_err("%s %s: failure: igmp_join_list=list_new()",
  820. __FILE__, __PRETTY_FUNCTION__);
  821. return -2;
  822. }
  823. pim_ifp->igmp_join_list->del = (void (*)(void *)) igmp_join_free;
  824. }
  825. ij = igmp_join_find(pim_ifp->igmp_join_list, group_addr, source_addr);
  826. if (ij) {
  827. char group_str[100];
  828. char source_str[100];
  829. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  830. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  831. zlog_warn("%s: can't re-join existing IGMP group %s source %s on interface %s",
  832. __PRETTY_FUNCTION__,
  833. group_str, source_str, ifp->name);
  834. return -3;
  835. }
  836. ij = igmp_join_new(ifp, group_addr, source_addr);
  837. if (!ij) {
  838. char group_str[100];
  839. char source_str[100];
  840. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  841. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  842. zlog_warn("%s: igmp_join_new() failure for IGMP group %s source %s on interface %s",
  843. __PRETTY_FUNCTION__,
  844. group_str, source_str, ifp->name);
  845. return -4;
  846. }
  847. {
  848. char group_str[100];
  849. char source_str[100];
  850. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  851. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  852. zlog_debug("%s: issued static igmp join for channel (S,G)=(%s,%s) on interface %s",
  853. __PRETTY_FUNCTION__,
  854. source_str, group_str, ifp->name);
  855. }
  856. return 0;
  857. }
  858. int pim_if_igmp_join_del(struct interface *ifp,
  859. struct in_addr group_addr,
  860. struct in_addr source_addr)
  861. {
  862. struct pim_interface *pim_ifp;
  863. struct igmp_join *ij;
  864. pim_ifp = ifp->info;
  865. if (!pim_ifp) {
  866. zlog_warn("%s: multicast not enabled on interface %s",
  867. __PRETTY_FUNCTION__,
  868. ifp->name);
  869. return -1;
  870. }
  871. if (!pim_ifp->igmp_join_list) {
  872. zlog_warn("%s: no IGMP join on interface %s",
  873. __PRETTY_FUNCTION__,
  874. ifp->name);
  875. return -2;
  876. }
  877. ij = igmp_join_find(pim_ifp->igmp_join_list, group_addr, source_addr);
  878. if (!ij) {
  879. char group_str[100];
  880. char source_str[100];
  881. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  882. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  883. zlog_warn("%s: could not find IGMP group %s source %s on interface %s",
  884. __PRETTY_FUNCTION__,
  885. group_str, source_str, ifp->name);
  886. return -3;
  887. }
  888. if (close(ij->sock_fd)) {
  889. int e = errno;
  890. char group_str[100];
  891. char source_str[100];
  892. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  893. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  894. zlog_warn("%s: failure closing sock_fd=%d for IGMP group %s source %s on interface %s: errno=%d: %s",
  895. __PRETTY_FUNCTION__,
  896. ij->sock_fd, group_str, source_str, ifp->name, e, safe_strerror(e));
  897. /* warning only */
  898. }
  899. listnode_delete(pim_ifp->igmp_join_list, ij);
  900. igmp_join_free(ij);
  901. if (listcount(pim_ifp->igmp_join_list) < 1) {
  902. list_delete(pim_ifp->igmp_join_list);
  903. pim_ifp->igmp_join_list = 0;
  904. }
  905. return 0;
  906. }
  907. static void pim_if_igmp_join_del_all(struct interface *ifp)
  908. {
  909. struct pim_interface *pim_ifp;
  910. struct listnode *node;
  911. struct listnode *nextnode;
  912. struct igmp_join *ij;
  913. pim_ifp = ifp->info;
  914. if (!pim_ifp) {
  915. zlog_warn("%s: multicast not enabled on interface %s",
  916. __PRETTY_FUNCTION__,
  917. ifp->name);
  918. return;
  919. }
  920. if (!pim_ifp->igmp_join_list)
  921. return;
  922. for (ALL_LIST_ELEMENTS(pim_ifp->igmp_join_list, node, nextnode, ij))
  923. pim_if_igmp_join_del(ifp, ij->group_addr, ij->source_addr);
  924. }
  925. /*
  926. RFC 4601
  927. Transitions from "I am Assert Loser" State
  928. Current Winner's GenID Changes or NLT Expires
  929. The Neighbor Liveness Timer associated with the current winner
  930. expires or we receive a Hello message from the current winner
  931. reporting a different GenID from the one it previously reported.
  932. This indicates that the current winner's interface or router has
  933. gone down (and may have come back up), and so we must assume it no
  934. longer knows it was the winner.
  935. */
  936. void pim_if_assert_on_neighbor_down(struct interface *ifp,
  937. struct in_addr neigh_addr)
  938. {
  939. struct pim_interface *pim_ifp;
  940. struct listnode *node;
  941. struct listnode *next_node;
  942. struct pim_ifchannel *ch;
  943. pim_ifp = ifp->info;
  944. zassert(pim_ifp);
  945. for (ALL_LIST_ELEMENTS(pim_ifp->pim_ifchannel_list, node, next_node, ch)) {
  946. /* Is (S,G,I) assert loser ? */
  947. if (ch->ifassert_state != PIM_IFASSERT_I_AM_LOSER)
  948. continue;
  949. /* Dead neighbor was winner ? */
  950. if (ch->ifassert_winner.s_addr != neigh_addr.s_addr)
  951. continue;
  952. assert_action_a5(ch);
  953. }
  954. }
  955. void pim_if_update_join_desired(struct pim_interface *pim_ifp)
  956. {
  957. struct listnode *ch_node;
  958. struct pim_ifchannel *ch;
  959. /* clear off flag from interface's upstreams */
  960. for (ALL_LIST_ELEMENTS_RO(pim_ifp->pim_ifchannel_list, ch_node, ch)) {
  961. PIM_UPSTREAM_FLAG_UNSET_DR_JOIN_DESIRED_UPDATED(ch->upstream->flags);
  962. }
  963. /* scan per-interface (S,G,I) state on this I interface */
  964. for (ALL_LIST_ELEMENTS_RO(pim_ifp->pim_ifchannel_list, ch_node, ch)) {
  965. struct pim_upstream *up = ch->upstream;
  966. if (PIM_UPSTREAM_FLAG_TEST_DR_JOIN_DESIRED_UPDATED(up->flags))
  967. continue;
  968. /* update join_desired for the global (S,G) state */
  969. pim_upstream_update_join_desired(up);
  970. PIM_UPSTREAM_FLAG_SET_DR_JOIN_DESIRED_UPDATED(up->flags);
  971. }
  972. }
  973. void pim_if_update_assert_tracking_desired(struct interface *ifp)
  974. {
  975. struct pim_interface *pim_ifp;
  976. struct listnode *node;
  977. struct listnode *next_node;
  978. struct pim_ifchannel *ch;
  979. pim_ifp = ifp->info;
  980. if (!pim_ifp)
  981. return;
  982. for (ALL_LIST_ELEMENTS(pim_ifp->pim_ifchannel_list, node, next_node, ch)) {
  983. pim_ifchannel_update_assert_tracking_desired(ch);
  984. }
  985. }