ospf_interface.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. * OSPF Interface functions.
  3. * Copyright (C) 1999, 2000 Toshiaki Takada
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2, or (at your
  10. * option) any later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "thread.h"
  24. #include "linklist.h"
  25. #include "prefix.h"
  26. #include "if.h"
  27. #include "table.h"
  28. #include "memory.h"
  29. #include "command.h"
  30. #include "stream.h"
  31. #include "log.h"
  32. #include "ospfd/ospfd.h"
  33. #include "ospfd/ospf_spf.h"
  34. #include "ospfd/ospf_interface.h"
  35. #include "ospfd/ospf_ism.h"
  36. #include "ospfd/ospf_asbr.h"
  37. #include "ospfd/ospf_lsa.h"
  38. #include "ospfd/ospf_lsdb.h"
  39. #include "ospfd/ospf_neighbor.h"
  40. #include "ospfd/ospf_nsm.h"
  41. #include "ospfd/ospf_packet.h"
  42. #include "ospfd/ospf_abr.h"
  43. #include "ospfd/ospf_network.h"
  44. #include "ospfd/ospf_dump.h"
  45. #ifdef HAVE_SNMP
  46. #include "ospfd/ospf_snmp.h"
  47. #endif /* HAVE_SNMP */
  48. int
  49. ospf_if_get_output_cost (struct ospf_interface *oi)
  50. {
  51. /* If all else fails, use default OSPF cost */
  52. u_int32_t cost;
  53. u_int32_t bw, refbw;
  54. bw = oi->ifp->bandwidth ? oi->ifp->bandwidth : OSPF_DEFAULT_BANDWIDTH;
  55. refbw = oi->ospf->ref_bandwidth;
  56. /* A specifed ip ospf cost overrides a calculated one. */
  57. if (OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (oi->ifp), output_cost_cmd) ||
  58. OSPF_IF_PARAM_CONFIGURED (oi->params, output_cost_cmd))
  59. cost = OSPF_IF_PARAM (oi, output_cost_cmd);
  60. /* See if a cost can be calculated from the zebra processes
  61. interface bandwidth field. */
  62. else
  63. {
  64. cost = (u_int32_t) ((double)refbw / (double)bw + (double)0.5);
  65. if (cost < 1)
  66. cost = 1;
  67. else if (cost > 65535)
  68. cost = 65535;
  69. }
  70. return cost;
  71. }
  72. void
  73. ospf_if_recalculate_output_cost (struct interface *ifp)
  74. {
  75. u_int32_t newcost;
  76. struct route_node *rn;
  77. for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
  78. {
  79. struct ospf_interface *oi;
  80. if ( (oi = rn->info) == NULL)
  81. continue;
  82. newcost = ospf_if_get_output_cost (oi);
  83. /* Is actual output cost changed? */
  84. if (oi->output_cost != newcost)
  85. {
  86. oi->output_cost = newcost;
  87. ospf_router_lsa_timer_add (oi->area);
  88. }
  89. }
  90. }
  91. /* Simulate down/up on the interface. This is needed, for example, when
  92. the MTU changes. */
  93. void
  94. ospf_if_reset(struct interface *ifp)
  95. {
  96. struct route_node *rn;
  97. for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
  98. {
  99. struct ospf_interface *oi;
  100. if ( (oi = rn->info) == NULL)
  101. continue;
  102. ospf_if_down(oi);
  103. ospf_if_up(oi);
  104. }
  105. }
  106. void
  107. ospf_if_reset_variables (struct ospf_interface *oi)
  108. {
  109. /* Set default values. */
  110. /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
  111. if (oi->vl_data)
  112. oi->type = OSPF_IFTYPE_VIRTUALLINK;
  113. else
  114. /* preserve network-type */
  115. if (oi->type != OSPF_IFTYPE_NBMA)
  116. oi->type = OSPF_IFTYPE_BROADCAST;
  117. oi->state = ISM_Down;
  118. oi->crypt_seqnum = 0;
  119. /* This must be short, (less than RxmtInterval)
  120. - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
  121. held back for too long - MAG */
  122. oi->v_ls_ack = 1;
  123. }
  124. /* lookup oi for specified prefix/ifp */
  125. static struct ospf_interface *
  126. ospf_if_table_lookup (struct interface *ifp, struct prefix *prefix)
  127. {
  128. struct prefix p;
  129. struct route_node *rn;
  130. struct ospf_interface *rninfo = NULL;
  131. p = *prefix;
  132. p.prefixlen = IPV4_MAX_PREFIXLEN;
  133. /* route_node_get implicitely locks */
  134. if ((rn = route_node_lookup (IF_OIFS (ifp), &p)))
  135. {
  136. rninfo = (struct ospf_interface *) rn->info;
  137. route_unlock_node (rn);
  138. }
  139. return rninfo;
  140. }
  141. static void
  142. ospf_add_to_if (struct interface *ifp, struct ospf_interface *oi)
  143. {
  144. struct route_node *rn;
  145. struct prefix p;
  146. p = *oi->address;
  147. p.prefixlen = IPV4_MAX_PREFIXLEN;
  148. rn = route_node_get (IF_OIFS (ifp), &p);
  149. /* rn->info should either be NULL or equal to this oi
  150. * as route_node_get may return an existing node
  151. */
  152. assert (!rn->info || rn->info == oi);
  153. rn->info = oi;
  154. }
  155. static void
  156. ospf_delete_from_if (struct interface *ifp, struct ospf_interface *oi)
  157. {
  158. struct route_node *rn;
  159. struct prefix p;
  160. p = *oi->address;
  161. p.prefixlen = IPV4_MAX_PREFIXLEN;
  162. rn = route_node_lookup (IF_OIFS (oi->ifp), &p);
  163. assert (rn);
  164. assert (rn->info);
  165. rn->info = NULL;
  166. route_unlock_node (rn);
  167. route_unlock_node (rn);
  168. }
  169. struct ospf_interface *
  170. ospf_if_new (struct ospf *ospf, struct interface *ifp, struct prefix *p)
  171. {
  172. struct ospf_interface *oi;
  173. if ((oi = ospf_if_table_lookup (ifp, p)) == NULL)
  174. {
  175. oi = XCALLOC (MTYPE_OSPF_IF, sizeof (struct ospf_interface));
  176. memset (oi, 0, sizeof (struct ospf_interface));
  177. }
  178. else
  179. return oi;
  180. /* Set zebra interface pointer. */
  181. oi->ifp = ifp;
  182. oi->address = p;
  183. ospf_add_to_if (ifp, oi);
  184. listnode_add (ospf->oiflist, oi);
  185. /* Clear self-originated network-LSA. */
  186. oi->network_lsa_self = NULL;
  187. /* Initialize neighbor list. */
  188. oi->nbrs = route_table_init ();
  189. /* Initialize static neighbor list. */
  190. oi->nbr_nbma = list_new ();
  191. /* Initialize Link State Acknowledgment list. */
  192. oi->ls_ack = list_new ();
  193. oi->ls_ack_direct.ls_ack = list_new ();
  194. /* Set default values. */
  195. ospf_if_reset_variables (oi);
  196. /* Add pseudo neighbor. */
  197. oi->nbr_self = ospf_nbr_new (oi);
  198. oi->ls_upd_queue = route_table_init ();
  199. oi->t_ls_upd_event = NULL;
  200. oi->t_ls_ack_direct = NULL;
  201. oi->crypt_seqnum = time (NULL);
  202. #ifdef HAVE_OPAQUE_LSA
  203. ospf_opaque_type9_lsa_init (oi);
  204. #endif /* HAVE_OPAQUE_LSA */
  205. oi->ospf = ospf;
  206. return oi;
  207. }
  208. /* Restore an interface to its pre UP state
  209. Used from ism_interface_down only */
  210. void
  211. ospf_if_cleanup (struct ospf_interface *oi)
  212. {
  213. struct route_node *rn;
  214. struct listnode *node, *nnode;
  215. struct ospf_neighbor *nbr;
  216. struct ospf_nbr_nbma *nbr_nbma;
  217. struct ospf_lsa *lsa;
  218. /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
  219. /* delete all static neighbors attached to this interface */
  220. for (ALL_LIST_ELEMENTS (oi->nbr_nbma, node, nnode, nbr_nbma))
  221. {
  222. OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
  223. if (nbr_nbma->nbr)
  224. {
  225. nbr_nbma->nbr->nbr_nbma = NULL;
  226. nbr_nbma->nbr = NULL;
  227. }
  228. nbr_nbma->oi = NULL;
  229. listnode_delete (oi->nbr_nbma, nbr_nbma);
  230. }
  231. /* send Neighbor event KillNbr to all associated neighbors. */
  232. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  233. if ((nbr = rn->info) != NULL)
  234. if (nbr != oi->nbr_self)
  235. OSPF_NSM_EVENT_EXECUTE (nbr, NSM_KillNbr);
  236. /* Cleanup Link State Acknowlegdment list. */
  237. for (ALL_LIST_ELEMENTS (oi->ls_ack, node, nnode, lsa))
  238. ospf_lsa_unlock (&lsa); /* oi->ls_ack */
  239. list_delete_all_node (oi->ls_ack);
  240. oi->crypt_seqnum = 0;
  241. /* Empty link state update queue */
  242. ospf_ls_upd_queue_empty (oi);
  243. /* Reset pseudo neighbor. */
  244. ospf_nbr_delete (oi->nbr_self);
  245. oi->nbr_self = ospf_nbr_new (oi);
  246. ospf_nbr_add_self (oi);
  247. ospf_lsa_unlock (&oi->network_lsa_self);
  248. oi->network_lsa_self = NULL;
  249. OSPF_TIMER_OFF (oi->t_network_lsa_self);
  250. }
  251. void
  252. ospf_if_free (struct ospf_interface *oi)
  253. {
  254. ospf_if_down (oi);
  255. assert (oi->state == ISM_Down);
  256. #ifdef HAVE_OPAQUE_LSA
  257. ospf_opaque_type9_lsa_term (oi);
  258. #endif /* HAVE_OPAQUE_LSA */
  259. /* Free Pseudo Neighbour */
  260. ospf_nbr_delete (oi->nbr_self);
  261. route_table_finish (oi->nbrs);
  262. route_table_finish (oi->ls_upd_queue);
  263. /* Free any lists that should be freed */
  264. list_free (oi->nbr_nbma);
  265. list_free (oi->ls_ack);
  266. list_free (oi->ls_ack_direct.ls_ack);
  267. ospf_delete_from_if (oi->ifp, oi);
  268. listnode_delete (oi->ospf->oiflist, oi);
  269. listnode_delete (oi->area->oiflist, oi);
  270. memset (oi, 0, sizeof (*oi));
  271. XFREE (MTYPE_OSPF_IF, oi);
  272. }
  273. /*
  274. * check if interface with given address is configured and
  275. * return it if yes. special treatment for PtP networks.
  276. */
  277. struct ospf_interface *
  278. ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
  279. {
  280. struct listnode *node, *nnode;
  281. struct ospf_interface *oi;
  282. struct prefix_ipv4 addr;
  283. addr.family = AF_INET;
  284. addr.prefix = *address;
  285. addr.prefixlen = IPV4_MAX_PREFIXLEN;
  286. for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
  287. if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
  288. {
  289. if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  290. {
  291. /* special leniency: match if addr is anywhere on peer subnet */
  292. if (prefix_match(CONNECTED_PREFIX(oi->connected),
  293. (struct prefix *)&addr))
  294. return oi;
  295. }
  296. else
  297. {
  298. if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
  299. return oi;
  300. }
  301. }
  302. return NULL;
  303. }
  304. int
  305. ospf_if_is_up (struct ospf_interface *oi)
  306. {
  307. return if_is_up (oi->ifp);
  308. }
  309. struct ospf_interface *
  310. ospf_if_exists (struct ospf_interface *oic)
  311. {
  312. struct listnode *node;
  313. struct ospf *ospf;
  314. struct ospf_interface *oi;
  315. if ((ospf = ospf_lookup ()) == NULL)
  316. return NULL;
  317. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  318. if (oi == oic)
  319. return oi;
  320. return NULL;
  321. }
  322. struct ospf_interface *
  323. ospf_if_lookup_by_local_addr (struct ospf *ospf,
  324. struct interface *ifp, struct in_addr address)
  325. {
  326. struct listnode *node;
  327. struct ospf_interface *oi;
  328. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  329. if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
  330. {
  331. if (ifp && oi->ifp != ifp)
  332. continue;
  333. if (IPV4_ADDR_SAME (&address, &oi->address->u.prefix4))
  334. return oi;
  335. }
  336. return NULL;
  337. }
  338. struct ospf_interface *
  339. ospf_if_lookup_by_prefix (struct ospf *ospf, struct prefix_ipv4 *p)
  340. {
  341. struct listnode *node;
  342. struct ospf_interface *oi;
  343. /* Check each Interface. */
  344. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  345. {
  346. if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
  347. {
  348. struct prefix ptmp;
  349. prefix_copy (&ptmp, CONNECTED_PREFIX(oi->connected));
  350. apply_mask (&ptmp);
  351. if (prefix_same (&ptmp, (struct prefix *) p))
  352. return oi;
  353. }
  354. }
  355. return NULL;
  356. }
  357. /* determine receiving interface by source of packet */
  358. struct ospf_interface *
  359. ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src)
  360. {
  361. struct listnode *node;
  362. struct prefix_ipv4 addr;
  363. struct ospf_interface *oi, *match;
  364. addr.family = AF_INET;
  365. addr.prefix = src;
  366. addr.prefixlen = IPV4_MAX_BITLEN;
  367. match = NULL;
  368. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  369. {
  370. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  371. continue;
  372. if (if_is_loopback (oi->ifp))
  373. continue;
  374. if (prefix_match (CONNECTED_PREFIX(oi->connected),
  375. (struct prefix *) &addr))
  376. {
  377. if ( (match == NULL) ||
  378. (match->address->prefixlen < oi->address->prefixlen)
  379. )
  380. match = oi;
  381. }
  382. }
  383. return match;
  384. }
  385. void
  386. ospf_if_stream_set (struct ospf_interface *oi)
  387. {
  388. /* set output fifo queue. */
  389. if (oi->obuf == NULL)
  390. oi->obuf = ospf_fifo_new ();
  391. }
  392. void
  393. ospf_if_stream_unset (struct ospf_interface *oi)
  394. {
  395. struct ospf *ospf = oi->ospf;
  396. if (oi->obuf)
  397. {
  398. ospf_fifo_free (oi->obuf);
  399. oi->obuf = NULL;
  400. if (oi->on_write_q)
  401. {
  402. listnode_delete (ospf->oi_write_q, oi);
  403. if (list_isempty(ospf->oi_write_q))
  404. OSPF_TIMER_OFF (ospf->t_write);
  405. oi->on_write_q = 0;
  406. }
  407. }
  408. }
  409. static struct ospf_if_params *
  410. ospf_new_if_params (void)
  411. {
  412. struct ospf_if_params *oip;
  413. oip = XMALLOC (MTYPE_OSPF_IF_PARAMS, sizeof (struct ospf_if_params));
  414. if (!oip)
  415. return NULL;
  416. memset (oip, 0, sizeof (struct ospf_if_params));
  417. UNSET_IF_PARAM (oip, output_cost_cmd);
  418. UNSET_IF_PARAM (oip, transmit_delay);
  419. UNSET_IF_PARAM (oip, retransmit_interval);
  420. UNSET_IF_PARAM (oip, passive_interface);
  421. UNSET_IF_PARAM (oip, v_hello);
  422. UNSET_IF_PARAM (oip, fast_hello);
  423. UNSET_IF_PARAM (oip, v_wait);
  424. UNSET_IF_PARAM (oip, priority);
  425. UNSET_IF_PARAM (oip, type);
  426. UNSET_IF_PARAM (oip, auth_simple);
  427. UNSET_IF_PARAM (oip, auth_crypt);
  428. UNSET_IF_PARAM (oip, auth_type);
  429. oip->auth_crypt = list_new ();
  430. return oip;
  431. }
  432. void
  433. ospf_del_if_params (struct ospf_if_params *oip)
  434. {
  435. list_delete (oip->auth_crypt);
  436. XFREE (MTYPE_OSPF_IF_PARAMS, oip);
  437. }
  438. void
  439. ospf_free_if_params (struct interface *ifp, struct in_addr addr)
  440. {
  441. struct ospf_if_params *oip;
  442. struct prefix_ipv4 p;
  443. struct route_node *rn;
  444. p.family = AF_INET;
  445. p.prefixlen = IPV4_MAX_PREFIXLEN;
  446. p.prefix = addr;
  447. rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
  448. if (!rn || !rn->info)
  449. return;
  450. oip = rn->info;
  451. route_unlock_node (rn);
  452. if (!OSPF_IF_PARAM_CONFIGURED (oip, output_cost_cmd) &&
  453. !OSPF_IF_PARAM_CONFIGURED (oip, transmit_delay) &&
  454. !OSPF_IF_PARAM_CONFIGURED (oip, retransmit_interval) &&
  455. !OSPF_IF_PARAM_CONFIGURED (oip, passive_interface) &&
  456. !OSPF_IF_PARAM_CONFIGURED (oip, v_hello) &&
  457. !OSPF_IF_PARAM_CONFIGURED (oip, fast_hello) &&
  458. !OSPF_IF_PARAM_CONFIGURED (oip, v_wait) &&
  459. !OSPF_IF_PARAM_CONFIGURED (oip, priority) &&
  460. !OSPF_IF_PARAM_CONFIGURED (oip, type) &&
  461. !OSPF_IF_PARAM_CONFIGURED (oip, auth_simple) &&
  462. !OSPF_IF_PARAM_CONFIGURED (oip, auth_type) &&
  463. listcount (oip->auth_crypt) == 0)
  464. {
  465. ospf_del_if_params (oip);
  466. rn->info = NULL;
  467. route_unlock_node (rn);
  468. }
  469. }
  470. struct ospf_if_params *
  471. ospf_lookup_if_params (struct interface *ifp, struct in_addr addr)
  472. {
  473. struct prefix_ipv4 p;
  474. struct route_node *rn;
  475. p.family = AF_INET;
  476. p.prefixlen = IPV4_MAX_PREFIXLEN;
  477. p.prefix = addr;
  478. rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
  479. if (rn)
  480. {
  481. route_unlock_node (rn);
  482. return rn->info;
  483. }
  484. return NULL;
  485. }
  486. struct ospf_if_params *
  487. ospf_get_if_params (struct interface *ifp, struct in_addr addr)
  488. {
  489. struct prefix_ipv4 p;
  490. struct route_node *rn;
  491. p.family = AF_INET;
  492. p.prefixlen = IPV4_MAX_PREFIXLEN;
  493. p.prefix = addr;
  494. rn = route_node_get (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
  495. if (rn->info == NULL)
  496. rn->info = ospf_new_if_params ();
  497. else
  498. route_unlock_node (rn);
  499. return rn->info;
  500. }
  501. void
  502. ospf_if_update_params (struct interface *ifp, struct in_addr addr)
  503. {
  504. struct route_node *rn;
  505. struct ospf_interface *oi;
  506. for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
  507. {
  508. if ((oi = rn->info) == NULL)
  509. continue;
  510. if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &addr))
  511. oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
  512. }
  513. }
  514. int
  515. ospf_if_new_hook (struct interface *ifp)
  516. {
  517. int rc = 0;
  518. ifp->info = XMALLOC (MTYPE_OSPF_IF_INFO, sizeof (struct ospf_if_info));
  519. memset (ifp->info, 0, sizeof (struct ospf_if_info));
  520. IF_OIFS (ifp) = route_table_init ();
  521. IF_OIFS_PARAMS (ifp) = route_table_init ();
  522. IF_DEF_PARAMS (ifp) = ospf_new_if_params ();
  523. SET_IF_PARAM (IF_DEF_PARAMS (ifp), transmit_delay);
  524. IF_DEF_PARAMS (ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
  525. SET_IF_PARAM (IF_DEF_PARAMS (ifp), retransmit_interval);
  526. IF_DEF_PARAMS (ifp)->retransmit_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT;
  527. SET_IF_PARAM (IF_DEF_PARAMS (ifp), priority);
  528. IF_DEF_PARAMS (ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
  529. IF_DEF_PARAMS (ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
  530. SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_hello);
  531. IF_DEF_PARAMS (ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
  532. SET_IF_PARAM (IF_DEF_PARAMS (ifp), fast_hello);
  533. IF_DEF_PARAMS (ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
  534. SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_wait);
  535. IF_DEF_PARAMS (ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
  536. SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_simple);
  537. memset (IF_DEF_PARAMS (ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
  538. SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_type);
  539. IF_DEF_PARAMS (ifp)->auth_type = OSPF_AUTH_NOTSET;
  540. #ifdef HAVE_OPAQUE_LSA
  541. rc = ospf_opaque_new_if (ifp);
  542. #endif /* HAVE_OPAQUE_LSA */
  543. return rc;
  544. }
  545. static int
  546. ospf_if_delete_hook (struct interface *ifp)
  547. {
  548. int rc = 0;
  549. struct route_node *rn;
  550. #ifdef HAVE_OPAQUE_LSA
  551. rc = ospf_opaque_del_if (ifp);
  552. #endif /* HAVE_OPAQUE_LSA */
  553. route_table_finish (IF_OIFS (ifp));
  554. for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn))
  555. if (rn->info)
  556. ospf_del_if_params (rn->info);
  557. route_table_finish (IF_OIFS_PARAMS (ifp));
  558. ospf_del_if_params ((struct ospf_if_params *) IF_DEF_PARAMS (ifp));
  559. XFREE (MTYPE_OSPF_IF_INFO, ifp->info);
  560. ifp->info = NULL;
  561. return rc;
  562. }
  563. int
  564. ospf_if_is_enable (struct ospf_interface *oi)
  565. {
  566. if (!if_is_loopback (oi->ifp))
  567. if (if_is_up (oi->ifp))
  568. return 1;
  569. return 0;
  570. }
  571. void
  572. ospf_if_set_multicast(struct ospf_interface *oi)
  573. {
  574. if ((oi->state > ISM_Loopback) &&
  575. (oi->type != OSPF_IFTYPE_LOOPBACK) &&
  576. (oi->type != OSPF_IFTYPE_VIRTUALLINK) &&
  577. (OSPF_IF_PARAM(oi, passive_interface) == OSPF_IF_ACTIVE))
  578. {
  579. /* The interface should belong to the OSPF-all-routers group. */
  580. if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS) &&
  581. (ospf_if_add_allspfrouters(oi->ospf, oi->address,
  582. oi->ifp->ifindex) >= 0))
  583. /* Set the flag only if the system call to join succeeded. */
  584. OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
  585. }
  586. else
  587. {
  588. /* The interface should NOT belong to the OSPF-all-routers group. */
  589. if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS))
  590. {
  591. /* Only actually drop if this is the last reference */
  592. if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
  593. ospf_if_drop_allspfrouters (oi->ospf, oi->address,
  594. oi->ifp->ifindex);
  595. /* Unset the flag regardless of whether the system call to leave
  596. the group succeeded, since it's much safer to assume that
  597. we are not a member. */
  598. OI_MEMBER_LEFT(oi,MEMBER_ALLROUTERS);
  599. }
  600. }
  601. if (((oi->type == OSPF_IFTYPE_BROADCAST) ||
  602. (oi->type == OSPF_IFTYPE_POINTOPOINT)) &&
  603. ((oi->state == ISM_DR) || (oi->state == ISM_Backup)) &&
  604. (OSPF_IF_PARAM(oi, passive_interface) == OSPF_IF_ACTIVE))
  605. {
  606. /* The interface should belong to the OSPF-designated-routers group. */
  607. if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS) &&
  608. (ospf_if_add_alldrouters(oi->ospf, oi->address,
  609. oi->ifp->ifindex) >= 0))
  610. /* Set the flag only if the system call to join succeeded. */
  611. OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
  612. }
  613. else
  614. {
  615. /* The interface should NOT belong to the OSPF-designated-routers group */
  616. if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS))
  617. {
  618. /* drop only if last reference */
  619. if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
  620. ospf_if_drop_alldrouters(oi->ospf, oi->address, oi->ifp->ifindex);
  621. /* Unset the flag regardless of whether the system call to leave
  622. the group succeeded, since it's much safer to assume that
  623. we are not a member. */
  624. OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
  625. }
  626. }
  627. }
  628. int
  629. ospf_if_up (struct ospf_interface *oi)
  630. {
  631. if (oi == NULL)
  632. return 0;
  633. if (oi->type == OSPF_IFTYPE_LOOPBACK)
  634. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_LoopInd);
  635. else
  636. {
  637. ospf_if_stream_set (oi);
  638. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp);
  639. }
  640. return 1;
  641. }
  642. int
  643. ospf_if_down (struct ospf_interface *oi)
  644. {
  645. if (oi == NULL)
  646. return 0;
  647. OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
  648. /* Shutdown packet reception and sending */
  649. ospf_if_stream_unset (oi);
  650. return 1;
  651. }
  652. /* Virtual Link related functions. */
  653. struct ospf_vl_data *
  654. ospf_vl_data_new (struct ospf_area *area, struct in_addr vl_peer)
  655. {
  656. struct ospf_vl_data *vl_data;
  657. vl_data = XMALLOC (MTYPE_OSPF_VL_DATA, sizeof (struct ospf_vl_data));
  658. memset (vl_data, 0, sizeof (struct ospf_vl_data));
  659. vl_data->vl_peer.s_addr = vl_peer.s_addr;
  660. vl_data->vl_area_id = area->area_id;
  661. vl_data->format = area->format;
  662. return vl_data;
  663. }
  664. void
  665. ospf_vl_data_free (struct ospf_vl_data *vl_data)
  666. {
  667. XFREE (MTYPE_OSPF_VL_DATA, vl_data);
  668. }
  669. u_int vlink_count = 0;
  670. struct ospf_interface *
  671. ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data)
  672. {
  673. struct ospf_interface * voi;
  674. struct interface * vi;
  675. char ifname[INTERFACE_NAMSIZ + 1];
  676. struct ospf_area *area;
  677. struct in_addr area_id;
  678. struct connected *co;
  679. struct prefix_ipv4 *p;
  680. if (IS_DEBUG_OSPF_EVENT)
  681. zlog_debug ("ospf_vl_new(): Start");
  682. if (vlink_count == OSPF_VL_MAX_COUNT)
  683. {
  684. if (IS_DEBUG_OSPF_EVENT)
  685. zlog_debug ("ospf_vl_new(): Alarm: "
  686. "cannot create more than OSPF_MAX_VL_COUNT virtual links");
  687. return NULL;
  688. }
  689. if (IS_DEBUG_OSPF_EVENT)
  690. zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
  691. snprintf (ifname, sizeof(ifname), "VLINK%d", vlink_count);
  692. vi = if_create (ifname, strnlen(ifname, sizeof(ifname)));
  693. co = connected_new ();
  694. co->ifp = vi;
  695. listnode_add (vi->connected, co);
  696. p = prefix_ipv4_new ();
  697. p->family = AF_INET;
  698. p->prefix.s_addr = 0;
  699. p->prefixlen = 0;
  700. co->address = (struct prefix *)p;
  701. voi = ospf_if_new (ospf, vi, co->address);
  702. if (voi == NULL)
  703. {
  704. if (IS_DEBUG_OSPF_EVENT)
  705. zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
  706. return NULL;
  707. }
  708. voi->connected = co;
  709. voi->vl_data = vl_data;
  710. voi->ifp->mtu = OSPF_VL_MTU;
  711. voi->type = OSPF_IFTYPE_VIRTUALLINK;
  712. vlink_count++;
  713. if (IS_DEBUG_OSPF_EVENT)
  714. zlog_debug ("ospf_vl_new(): Created name: %s", ifname);
  715. if (IS_DEBUG_OSPF_EVENT)
  716. zlog_debug ("ospf_vl_new(): set if->name to %s", vi->name);
  717. area_id.s_addr = 0;
  718. area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS);
  719. voi->area = area;
  720. if (IS_DEBUG_OSPF_EVENT)
  721. zlog_debug ("ospf_vl_new(): set associated area to the backbone");
  722. ospf_nbr_add_self (voi);
  723. ospf_area_add_if (voi->area, voi);
  724. ospf_if_stream_set (voi);
  725. if (IS_DEBUG_OSPF_EVENT)
  726. zlog_debug ("ospf_vl_new(): Stop");
  727. return voi;
  728. }
  729. static void
  730. ospf_vl_if_delete (struct ospf_vl_data *vl_data)
  731. {
  732. struct interface *ifp = vl_data->vl_oi->ifp;
  733. vl_data->vl_oi->address->u.prefix4.s_addr = 0;
  734. vl_data->vl_oi->address->prefixlen = 0;
  735. ospf_if_free (vl_data->vl_oi);
  736. if_delete (ifp);
  737. vlink_count--;
  738. }
  739. /* Look up vl_data for given peer, optionally qualified to be in the
  740. * specified area. NULL area returns first found..
  741. */
  742. struct ospf_vl_data *
  743. ospf_vl_lookup (struct ospf *ospf, struct ospf_area *area,
  744. struct in_addr vl_peer)
  745. {
  746. struct ospf_vl_data *vl_data;
  747. struct listnode *node;
  748. if (IS_DEBUG_OSPF_EVENT)
  749. {
  750. zlog_debug ("%s: Looking for %s", __func__, inet_ntoa (vl_peer));
  751. if (area)
  752. zlog_debug ("%s: in area %s", __func__, inet_ntoa (area->area_id));
  753. }
  754. for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
  755. {
  756. if (IS_DEBUG_OSPF_EVENT)
  757. zlog_debug ("%s: VL %s, peer %s", __func__,
  758. vl_data->vl_oi->ifp->name,
  759. inet_ntoa (vl_data->vl_peer));
  760. if (area && !IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
  761. continue;
  762. if (IPV4_ADDR_SAME (&vl_data->vl_peer, &vl_peer))
  763. return vl_data;
  764. }
  765. return NULL;
  766. }
  767. static void
  768. ospf_vl_shutdown (struct ospf_vl_data *vl_data)
  769. {
  770. struct ospf_interface *oi;
  771. if ((oi = vl_data->vl_oi) == NULL)
  772. return;
  773. oi->address->u.prefix4.s_addr = 0;
  774. oi->address->prefixlen = 0;
  775. UNSET_FLAG (oi->ifp->flags, IFF_UP);
  776. /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
  777. OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
  778. }
  779. void
  780. ospf_vl_add (struct ospf *ospf, struct ospf_vl_data *vl_data)
  781. {
  782. listnode_add (ospf->vlinks, vl_data);
  783. #ifdef HAVE_SNMP
  784. ospf_snmp_vl_add (vl_data);
  785. #endif /* HAVE_SNMP */
  786. }
  787. void
  788. ospf_vl_delete (struct ospf *ospf, struct ospf_vl_data *vl_data)
  789. {
  790. ospf_vl_shutdown (vl_data);
  791. ospf_vl_if_delete (vl_data);
  792. #ifdef HAVE_SNMP
  793. ospf_snmp_vl_delete (vl_data);
  794. #endif /* HAVE_SNMP */
  795. listnode_delete (ospf->vlinks, vl_data);
  796. ospf_vl_data_free (vl_data);
  797. }
  798. static int
  799. ospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v)
  800. {
  801. int changed = 0;
  802. struct ospf_interface *voi;
  803. struct listnode *node;
  804. struct vertex_parent *vp = NULL;
  805. int i;
  806. struct router_lsa *rl;
  807. voi = vl_data->vl_oi;
  808. if (voi->output_cost != v->distance)
  809. {
  810. voi->output_cost = v->distance;
  811. changed = 1;
  812. }
  813. for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp))
  814. {
  815. vl_data->nexthop.oi = vp->nexthop->oi;
  816. vl_data->nexthop.router = vp->nexthop->router;
  817. if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
  818. &vl_data->nexthop.oi->address->u.prefix4))
  819. changed = 1;
  820. voi->address->u.prefix4 = vl_data->nexthop.oi->address->u.prefix4;
  821. voi->address->prefixlen = vl_data->nexthop.oi->address->prefixlen;
  822. break; /* We take the first interface. */
  823. }
  824. rl = (struct router_lsa *)v->lsa;
  825. /* use SPF determined backlink index in struct vertex
  826. * for virtual link destination address
  827. */
  828. if (vp && vp->backlink >= 0)
  829. {
  830. if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
  831. &rl->link[vp->backlink].link_data))
  832. changed = 1;
  833. vl_data->peer_addr = rl->link[vp->backlink].link_data;
  834. }
  835. else
  836. {
  837. /* This is highly odd, there is no backlink index
  838. * there should be due to the ospf_spf_has_link() check
  839. * in SPF. Lets warn and try pick a link anyway.
  840. */
  841. zlog_warn ("ospf_vl_set_params: No backlink for %s!",
  842. vl_data->vl_oi->ifp->name);
  843. for (i = 0; i < ntohs (rl->links); i++)
  844. {
  845. switch (rl->link[i].type)
  846. {
  847. case LSA_LINK_TYPE_VIRTUALLINK:
  848. if (IS_DEBUG_OSPF_EVENT)
  849. zlog_debug ("found back link through VL");
  850. case LSA_LINK_TYPE_TRANSIT:
  851. case LSA_LINK_TYPE_POINTOPOINT:
  852. if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
  853. &rl->link[i].link_data))
  854. changed = 1;
  855. vl_data->peer_addr = rl->link[i].link_data;
  856. }
  857. }
  858. }
  859. if (IS_DEBUG_OSPF_EVENT)
  860. zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__,
  861. vl_data->vl_oi->ifp->name,
  862. inet_ntoa(vl_data->peer_addr),
  863. voi->output_cost,
  864. (changed ? " " : " un"));
  865. return changed;
  866. }
  867. void
  868. ospf_vl_up_check (struct ospf_area *area, struct in_addr rid,
  869. struct vertex *v)
  870. {
  871. struct ospf *ospf = area->ospf;
  872. struct listnode *node;
  873. struct ospf_vl_data *vl_data;
  874. struct ospf_interface *oi;
  875. if (IS_DEBUG_OSPF_EVENT)
  876. {
  877. zlog_debug ("ospf_vl_up_check(): Start");
  878. zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid));
  879. zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area->area_id));
  880. }
  881. for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
  882. {
  883. if (IS_DEBUG_OSPF_EVENT)
  884. {
  885. zlog_debug ("%s: considering VL, %s in area %s", __func__,
  886. vl_data->vl_oi->ifp->name,
  887. inet_ntoa (vl_data->vl_area_id));
  888. zlog_debug ("%s: peer ID: %s", __func__,
  889. inet_ntoa (vl_data->vl_peer));
  890. }
  891. if (IPV4_ADDR_SAME (&vl_data->vl_peer, &rid) &&
  892. IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
  893. {
  894. oi = vl_data->vl_oi;
  895. SET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
  896. if (IS_DEBUG_OSPF_EVENT)
  897. zlog_debug ("ospf_vl_up_check(): this VL matched");
  898. if (oi->state == ISM_Down)
  899. {
  900. if (IS_DEBUG_OSPF_EVENT)
  901. zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
  902. SET_FLAG (oi->ifp->flags, IFF_UP);
  903. OSPF_ISM_EVENT_EXECUTE(oi,ISM_InterfaceUp);
  904. }
  905. if (ospf_vl_set_params (vl_data, v))
  906. {
  907. if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
  908. zlog_debug ("ospf_vl_up_check: VL cost change,"
  909. " scheduling router lsa refresh");
  910. if(ospf->backbone)
  911. ospf_router_lsa_timer_add (ospf->backbone);
  912. else if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
  913. zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
  914. }
  915. }
  916. }
  917. }
  918. void
  919. ospf_vl_unapprove (struct ospf *ospf)
  920. {
  921. struct listnode *node;
  922. struct ospf_vl_data *vl_data;
  923. for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
  924. UNSET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
  925. }
  926. void
  927. ospf_vl_shut_unapproved (struct ospf *ospf)
  928. {
  929. struct listnode *node, *nnode;
  930. struct ospf_vl_data *vl_data;
  931. for (ALL_LIST_ELEMENTS (ospf->vlinks, node, nnode, vl_data))
  932. if (!CHECK_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED))
  933. ospf_vl_shutdown (vl_data);
  934. }
  935. int
  936. ospf_full_virtual_nbrs (struct ospf_area *area)
  937. {
  938. if (IS_DEBUG_OSPF_EVENT)
  939. {
  940. zlog_debug ("counting fully adjacent virtual neighbors in area %s",
  941. inet_ntoa (area->area_id));
  942. zlog_debug ("there are %d of them", area->full_vls);
  943. }
  944. return area->full_vls;
  945. }
  946. int
  947. ospf_vls_in_area (struct ospf_area *area)
  948. {
  949. struct listnode *node;
  950. struct ospf_vl_data *vl_data;
  951. int c = 0;
  952. for (ALL_LIST_ELEMENTS_RO (area->ospf->vlinks, node, vl_data))
  953. if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
  954. c++;
  955. return c;
  956. }
  957. struct crypt_key *
  958. ospf_crypt_key_new ()
  959. {
  960. struct crypt_key *ck;
  961. ck = XMALLOC (MTYPE_OSPF_CRYPT_KEY, sizeof (struct crypt_key));
  962. memset (ck, 0, sizeof (struct crypt_key));
  963. return ck;
  964. }
  965. void
  966. ospf_crypt_key_add (struct list *crypt, struct crypt_key *ck)
  967. {
  968. listnode_add (crypt, ck);
  969. }
  970. struct crypt_key *
  971. ospf_crypt_key_lookup (struct list *auth_crypt, u_char key_id)
  972. {
  973. struct listnode *node;
  974. struct crypt_key *ck;
  975. for (ALL_LIST_ELEMENTS_RO (auth_crypt, node, ck))
  976. if (ck->key_id == key_id)
  977. return ck;
  978. return NULL;
  979. }
  980. int
  981. ospf_crypt_key_delete (struct list *auth_crypt, u_char key_id)
  982. {
  983. struct listnode *node, *nnode;
  984. struct crypt_key *ck;
  985. for (ALL_LIST_ELEMENTS (auth_crypt, node, nnode, ck))
  986. {
  987. if (ck->key_id == key_id)
  988. {
  989. listnode_delete (auth_crypt, ck);
  990. XFREE (MTYPE_OSPF_CRYPT_KEY, ck);
  991. return 1;
  992. }
  993. }
  994. return 0;
  995. }
  996. u_char
  997. ospf_default_iftype(struct interface *ifp)
  998. {
  999. if (if_is_pointopoint (ifp))
  1000. return OSPF_IFTYPE_POINTOPOINT;
  1001. else if (if_is_loopback (ifp))
  1002. return OSPF_IFTYPE_LOOPBACK;
  1003. else
  1004. return OSPF_IFTYPE_BROADCAST;
  1005. }
  1006. void
  1007. ospf_if_init ()
  1008. {
  1009. /* Initialize Zebra interface data structure. */
  1010. if_init ();
  1011. om->iflist = iflist;
  1012. if_add_hook (IF_NEW_HOOK, ospf_if_new_hook);
  1013. if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);
  1014. }