ospf_neighbor.c 11 KB

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