ospf_neighbor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * OSPF Neighbor 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 "linklist.h"
  24. #include "prefix.h"
  25. #include "memory.h"
  26. #include "command.h"
  27. #include "thread.h"
  28. #include "stream.h"
  29. #include "table.h"
  30. #include "log.h"
  31. #include "ospfd/ospfd.h"
  32. #include "ospfd/ospf_interface.h"
  33. #include "ospfd/ospf_asbr.h"
  34. #include "ospfd/ospf_lsa.h"
  35. #include "ospfd/ospf_lsdb.h"
  36. #include "ospfd/ospf_neighbor.h"
  37. #include "ospfd/ospf_nsm.h"
  38. #include "ospfd/ospf_packet.h"
  39. #include "ospfd/ospf_network.h"
  40. #include "ospfd/ospf_flood.h"
  41. #include "ospfd/ospf_dump.h"
  42. /* Fill in the the 'key' as appropriate to retrieve the entry for nbr
  43. * from the ospf_interface's nbrs table. Indexed by interface address
  44. * for all cases except Virtual-link and PointToPoint interfaces, where
  45. * neighbours are indexed by router-ID instead.
  46. */
  47. static void
  48. ospf_nbr_key (struct ospf_interface *oi, struct ospf_neighbor *nbr,
  49. struct prefix *key)
  50. {
  51. key->family = AF_INET;
  52. key->prefixlen = IPV4_MAX_BITLEN;
  53. /* vlinks are indexed by router-id */
  54. if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
  55. oi->type == OSPF_IFTYPE_POINTOPOINT)
  56. key->u.prefix4 = nbr->router_id;
  57. else
  58. key->u.prefix4 = nbr->src;
  59. return;
  60. }
  61. struct ospf_neighbor *
  62. ospf_nbr_new (struct ospf_interface *oi)
  63. {
  64. struct ospf_neighbor *nbr;
  65. /* Allcate new neighbor. */
  66. nbr = XCALLOC (MTYPE_OSPF_NEIGHBOR, sizeof (struct ospf_neighbor));
  67. /* Relate neighbor to the interface. */
  68. nbr->oi = oi;
  69. /* Set default values. */
  70. nbr->state = NSM_Down;
  71. /* Set inheritance values. */
  72. nbr->v_inactivity = OSPF_IF_PARAM (oi, v_wait);
  73. nbr->v_db_desc = OSPF_IF_PARAM (oi, retransmit_interval);
  74. nbr->v_ls_req = OSPF_IF_PARAM (oi, retransmit_interval);
  75. nbr->v_ls_upd = OSPF_IF_PARAM (oi, retransmit_interval);
  76. nbr->priority = -1;
  77. /* DD flags. */
  78. nbr->dd_flags = OSPF_DD_FLAG_MS|OSPF_DD_FLAG_M|OSPF_DD_FLAG_I;
  79. /* Last received and sent DD. */
  80. nbr->last_send = NULL;
  81. nbr->nbr_nbma = NULL;
  82. ospf_lsdb_init (&nbr->db_sum);
  83. ospf_lsdb_init (&nbr->ls_rxmt);
  84. ospf_lsdb_init (&nbr->ls_req);
  85. nbr->crypt_seqnum = 0;
  86. return nbr;
  87. }
  88. void
  89. ospf_nbr_free (struct ospf_neighbor *nbr)
  90. {
  91. /* Free DB summary list. */
  92. if (ospf_db_summary_count (nbr))
  93. ospf_db_summary_clear (nbr);
  94. /* ospf_db_summary_delete_all (nbr); */
  95. /* Free ls request list. */
  96. if (ospf_ls_request_count (nbr))
  97. ospf_ls_request_delete_all (nbr);
  98. /* Free retransmit list. */
  99. if (ospf_ls_retransmit_count (nbr))
  100. ospf_ls_retransmit_clear (nbr);
  101. /* Cleanup LSDBs. */
  102. ospf_lsdb_cleanup (&nbr->db_sum);
  103. ospf_lsdb_cleanup (&nbr->ls_req);
  104. ospf_lsdb_cleanup (&nbr->ls_rxmt);
  105. /* Clear last send packet. */
  106. if (nbr->last_send)
  107. ospf_packet_free (nbr->last_send);
  108. if (nbr->nbr_nbma)
  109. {
  110. nbr->nbr_nbma->nbr = NULL;
  111. nbr->nbr_nbma = NULL;
  112. }
  113. /* Cancel all timers. */
  114. OSPF_NSM_TIMER_OFF (nbr->t_inactivity);
  115. OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
  116. OSPF_NSM_TIMER_OFF (nbr->t_ls_req);
  117. OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
  118. /* Cancel all events. *//* Thread lookup cost would be negligible. */
  119. thread_cancel_event (master, nbr);
  120. XFREE (MTYPE_OSPF_NEIGHBOR, nbr);
  121. }
  122. /* Delete specified OSPF neighbor from interface. */
  123. void
  124. ospf_nbr_delete (struct ospf_neighbor *nbr)
  125. {
  126. struct ospf_interface *oi;
  127. struct route_node *rn;
  128. struct prefix p;
  129. oi = nbr->oi;
  130. /* get appropriate prefix 'key' */
  131. ospf_nbr_key (oi, nbr, &p);
  132. rn = route_node_lookup (oi->nbrs, &p);
  133. if (rn)
  134. {
  135. /* If lookup for a NBR succeeds, the leaf route_node could
  136. * only exist because there is (or was) a nbr there.
  137. * If the nbr was deleted, the leaf route_node should have
  138. * lost its last refcount too, and be deleted.
  139. * Therefore a looked-up leaf route_node in nbrs table
  140. * should never have NULL info.
  141. */
  142. assert (rn->info);
  143. if (rn->info)
  144. {
  145. rn->info = NULL;
  146. route_unlock_node (rn);
  147. }
  148. else
  149. zlog_info ("Can't find neighbor %s in the interface %s",
  150. inet_ntoa (nbr->src), IF_NAME (oi));
  151. route_unlock_node (rn);
  152. }
  153. else
  154. {
  155. /*
  156. * This neighbor was not found, but before we move on and
  157. * free the neighbor structre, make sure that it was not
  158. * indexed incorrectly and ended up in the "worng" place
  159. */
  160. /* Reverse the lookup rules */
  161. if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
  162. oi->type == OSPF_IFTYPE_POINTOPOINT)
  163. p.u.prefix4 = nbr->src;
  164. else
  165. p.u.prefix4 = nbr->router_id;
  166. rn = route_node_lookup (oi->nbrs, &p);
  167. if (rn){
  168. /* We found the neighbor!
  169. * Now make sure it is not the exact same neighbor
  170. * structure that we are about to free
  171. */
  172. if (nbr == rn->info){
  173. /* Same neighbor, drop the reference to it */
  174. rn->info = NULL;
  175. route_unlock_node (rn);
  176. }
  177. route_unlock_node (rn);
  178. }
  179. }
  180. /* Free ospf_neighbor structure. */
  181. ospf_nbr_free (nbr);
  182. }
  183. /* Check myself is in the neighbor list. */
  184. int
  185. ospf_nbr_bidirectional (struct in_addr *router_id,
  186. struct in_addr *neighbors, int size)
  187. {
  188. int i;
  189. int max;
  190. max = size / sizeof (struct in_addr);
  191. for (i = 0; i < max; i ++)
  192. if (IPV4_ADDR_SAME (router_id, &neighbors[i]))
  193. return 1;
  194. return 0;
  195. }
  196. /* reset nbr_self */
  197. void
  198. ospf_nbr_self_reset (struct ospf_interface *oi)
  199. {
  200. if (oi->nbr_self)
  201. ospf_nbr_delete (oi->nbr_self);
  202. oi->nbr_self = ospf_nbr_new (oi);
  203. ospf_nbr_add_self (oi);
  204. }
  205. /* Add self to nbr list. */
  206. void
  207. ospf_nbr_add_self (struct ospf_interface *oi)
  208. {
  209. struct prefix p;
  210. struct route_node *rn;
  211. /* Initial state */
  212. oi->nbr_self->address = *oi->address;
  213. oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
  214. oi->nbr_self->router_id = oi->ospf->router_id;
  215. oi->nbr_self->src = oi->address->u.prefix4;
  216. oi->nbr_self->state = NSM_TwoWay;
  217. switch (oi->area->external_routing)
  218. {
  219. case OSPF_AREA_DEFAULT:
  220. SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  221. break;
  222. case OSPF_AREA_STUB:
  223. UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  224. break;
  225. case OSPF_AREA_NSSA:
  226. UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  227. SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
  228. break;
  229. }
  230. /* Add nbr_self to nbrs table */
  231. ospf_nbr_key (oi, oi->nbr_self, &p);
  232. rn = route_node_get (oi->nbrs, &p);
  233. if (rn->info)
  234. {
  235. /* There is already pseudo neighbor. */
  236. assert (oi->nbr_self == rn->info);
  237. route_unlock_node (rn);
  238. }
  239. else
  240. rn->info = oi->nbr_self;
  241. }
  242. /* Get neighbor count by status.
  243. Specify status = 0, get all neighbor other than myself. */
  244. int
  245. ospf_nbr_count (struct ospf_interface *oi, int state)
  246. {
  247. struct ospf_neighbor *nbr;
  248. struct route_node *rn;
  249. int count = 0;
  250. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  251. if ((nbr = rn->info))
  252. if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  253. if (state == 0 || nbr->state == state)
  254. count++;
  255. return count;
  256. }
  257. int
  258. ospf_nbr_count_opaque_capable (struct ospf_interface *oi)
  259. {
  260. struct ospf_neighbor *nbr;
  261. struct route_node *rn;
  262. int count = 0;
  263. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  264. if ((nbr = rn->info))
  265. if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  266. if (nbr->state == NSM_Full)
  267. if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  268. count++;
  269. return count;
  270. }
  271. /* lookup nbr by address - use this only if you know you must
  272. * otherwise use the ospf_nbr_lookup() wrapper, which deals
  273. * with virtual link and PointToPoint neighbours
  274. */
  275. struct ospf_neighbor *
  276. ospf_nbr_lookup_by_addr (struct route_table *nbrs,
  277. struct in_addr *addr)
  278. {
  279. struct prefix p;
  280. struct route_node *rn;
  281. struct ospf_neighbor *nbr;
  282. p.family = AF_INET;
  283. p.prefixlen = IPV4_MAX_BITLEN;
  284. p.u.prefix4 = *addr;
  285. rn = route_node_lookup (nbrs, &p);
  286. if (! rn)
  287. return NULL;
  288. /* See comment in ospf_nbr_delete */
  289. assert (rn->info);
  290. if (rn->info == NULL)
  291. {
  292. route_unlock_node (rn);
  293. return NULL;
  294. }
  295. nbr = (struct ospf_neighbor *) rn->info;
  296. route_unlock_node (rn);
  297. return nbr;
  298. }
  299. struct ospf_neighbor *
  300. ospf_nbr_lookup_by_routerid (struct route_table *nbrs,
  301. struct in_addr *id)
  302. {
  303. struct route_node *rn;
  304. struct ospf_neighbor *nbr;
  305. for (rn = route_top (nbrs); rn; rn = route_next (rn))
  306. if ((nbr = rn->info) != NULL)
  307. if (IPV4_ADDR_SAME (&nbr->router_id, id))
  308. {
  309. route_unlock_node(rn);
  310. return nbr;
  311. }
  312. return NULL;
  313. }
  314. void
  315. ospf_renegotiate_optional_capabilities (struct ospf *top)
  316. {
  317. struct listnode *node;
  318. struct ospf_interface *oi;
  319. struct route_table *nbrs;
  320. struct route_node *rn;
  321. struct ospf_neighbor *nbr;
  322. /* At first, flush self-originated LSAs from routing domain. */
  323. ospf_flush_self_originated_lsas_now (top);
  324. /* Revert all neighbor status to ExStart. */
  325. for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi))
  326. {
  327. if ((nbrs = oi->nbrs) == NULL)
  328. continue;
  329. for (rn = route_top (nbrs); rn; rn = route_next (rn))
  330. {
  331. if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
  332. continue;
  333. if (nbr->state < NSM_ExStart)
  334. continue;
  335. if (IS_DEBUG_OSPF_EVENT)
  336. zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id));
  337. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  338. }
  339. }
  340. return;
  341. }
  342. struct ospf_neighbor *
  343. ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph,
  344. struct ospf_header *ospfh)
  345. {
  346. if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
  347. oi->type == OSPF_IFTYPE_POINTOPOINT)
  348. return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id));
  349. else
  350. return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src));
  351. }
  352. static struct ospf_neighbor *
  353. ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh,
  354. struct prefix *p)
  355. {
  356. struct ospf_neighbor *nbr;
  357. nbr = ospf_nbr_new (oi);
  358. nbr->state = NSM_Down;
  359. nbr->src = p->u.prefix4;
  360. memcpy (&nbr->address, p, sizeof (struct prefix));
  361. nbr->nbr_nbma = NULL;
  362. if (oi->type == OSPF_IFTYPE_NBMA)
  363. {
  364. struct ospf_nbr_nbma *nbr_nbma;
  365. struct listnode *node;
  366. for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma))
  367. {
  368. if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src))
  369. {
  370. nbr_nbma->nbr = nbr;
  371. nbr->nbr_nbma = nbr_nbma;
  372. if (nbr_nbma->t_poll)
  373. OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
  374. nbr->state_change = nbr_nbma->state_change + 1;
  375. }
  376. }
  377. }
  378. /* New nbr, save the crypto sequence number if necessary */
  379. if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
  380. nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
  381. if (IS_DEBUG_OSPF_EVENT)
  382. zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
  383. inet_ntoa (nbr->router_id));
  384. return nbr;
  385. }
  386. struct ospf_neighbor *
  387. ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh,
  388. struct ip *iph, struct prefix *p)
  389. {
  390. struct route_node *rn;
  391. struct prefix key;
  392. struct ospf_neighbor *nbr;
  393. key.family = AF_INET;
  394. key.prefixlen = IPV4_MAX_BITLEN;
  395. if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
  396. oi->type == OSPF_IFTYPE_POINTOPOINT)
  397. key.u.prefix4 = ospfh->router_id;/* index vlink and ptp nbrs by router-id */
  398. else
  399. key.u.prefix4 = iph->ip_src;
  400. rn = route_node_get (oi->nbrs, &key);
  401. if (rn->info)
  402. {
  403. route_unlock_node (rn);
  404. nbr = rn->info;
  405. if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
  406. {
  407. nbr->src = iph->ip_src;
  408. memcpy (&nbr->address, p, sizeof (struct prefix));
  409. }
  410. }
  411. else
  412. {
  413. rn->info = nbr = ospf_nbr_add (oi, ospfh, p);
  414. }
  415. nbr->router_id = ospfh->router_id;
  416. return nbr;
  417. }