ospf_neighbor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. /* Free ospf_neighbor structure. */
  154. ospf_nbr_free (nbr);
  155. }
  156. /* Check myself is in the neighbor list. */
  157. int
  158. ospf_nbr_bidirectional (struct in_addr *router_id,
  159. struct in_addr *neighbors, int size)
  160. {
  161. int i;
  162. int max;
  163. max = size / sizeof (struct in_addr);
  164. for (i = 0; i < max; i ++)
  165. if (IPV4_ADDR_SAME (router_id, &neighbors[i]))
  166. return 1;
  167. return 0;
  168. }
  169. /* reset nbr_self */
  170. void
  171. ospf_nbr_self_reset (struct ospf_interface *oi)
  172. {
  173. ospf_nbr_delete (oi->nbr_self);
  174. oi->nbr_self = ospf_nbr_new (oi);
  175. ospf_nbr_add_self (oi);
  176. }
  177. /* Add self to nbr list. */
  178. void
  179. ospf_nbr_add_self (struct ospf_interface *oi)
  180. {
  181. struct prefix p;
  182. struct route_node *rn;
  183. /* Initial state */
  184. oi->nbr_self->address = *oi->address;
  185. oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
  186. oi->nbr_self->router_id = oi->ospf->router_id;
  187. oi->nbr_self->src = oi->address->u.prefix4;
  188. oi->nbr_self->state = NSM_TwoWay;
  189. switch (oi->area->external_routing)
  190. {
  191. case OSPF_AREA_DEFAULT:
  192. SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  193. break;
  194. case OSPF_AREA_STUB:
  195. UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  196. break;
  197. case OSPF_AREA_NSSA:
  198. UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  199. SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
  200. break;
  201. }
  202. /* Add nbr_self to nbrs table */
  203. ospf_nbr_key (oi, oi->nbr_self, &p);
  204. rn = route_node_get (oi->nbrs, &p);
  205. if (rn->info)
  206. {
  207. /* There is already pseudo neighbor. */
  208. assert (oi->nbr_self == rn->info);
  209. route_unlock_node (rn);
  210. }
  211. else
  212. rn->info = oi->nbr_self;
  213. }
  214. /* Get neighbor count by status.
  215. Specify status = 0, get all neighbor other than myself. */
  216. int
  217. ospf_nbr_count (struct ospf_interface *oi, int state)
  218. {
  219. struct ospf_neighbor *nbr;
  220. struct route_node *rn;
  221. int count = 0;
  222. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  223. if ((nbr = rn->info))
  224. if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  225. if (state == 0 || nbr->state == state)
  226. count++;
  227. return count;
  228. }
  229. int
  230. ospf_nbr_count_opaque_capable (struct ospf_interface *oi)
  231. {
  232. struct ospf_neighbor *nbr;
  233. struct route_node *rn;
  234. int count = 0;
  235. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  236. if ((nbr = rn->info))
  237. if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  238. if (nbr->state == NSM_Full)
  239. if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  240. count++;
  241. return count;
  242. }
  243. /* lookup nbr by address - use this only if you know you must
  244. * otherwise use the ospf_nbr_lookup() wrapper, which deals
  245. * with virtual link and PointToPoint neighbours
  246. */
  247. struct ospf_neighbor *
  248. ospf_nbr_lookup_by_addr (struct route_table *nbrs,
  249. struct in_addr *addr)
  250. {
  251. struct prefix p;
  252. struct route_node *rn;
  253. struct ospf_neighbor *nbr;
  254. p.family = AF_INET;
  255. p.prefixlen = IPV4_MAX_BITLEN;
  256. p.u.prefix4 = *addr;
  257. rn = route_node_lookup (nbrs, &p);
  258. if (! rn)
  259. return NULL;
  260. /* See comment in ospf_nbr_delete */
  261. assert (rn->info);
  262. if (rn->info == NULL)
  263. {
  264. route_unlock_node (rn);
  265. return NULL;
  266. }
  267. nbr = (struct ospf_neighbor *) rn->info;
  268. route_unlock_node (rn);
  269. return nbr;
  270. }
  271. struct ospf_neighbor *
  272. ospf_nbr_lookup_by_routerid (struct route_table *nbrs,
  273. struct in_addr *id)
  274. {
  275. struct route_node *rn;
  276. struct ospf_neighbor *nbr;
  277. for (rn = route_top (nbrs); rn; rn = route_next (rn))
  278. if ((nbr = rn->info) != NULL)
  279. if (IPV4_ADDR_SAME (&nbr->router_id, id))
  280. {
  281. route_unlock_node(rn);
  282. return nbr;
  283. }
  284. return NULL;
  285. }
  286. void
  287. ospf_renegotiate_optional_capabilities (struct ospf *top)
  288. {
  289. struct listnode *node;
  290. struct ospf_interface *oi;
  291. struct route_table *nbrs;
  292. struct route_node *rn;
  293. struct ospf_neighbor *nbr;
  294. /* At first, flush self-originated LSAs from routing domain. */
  295. ospf_flush_self_originated_lsas_now (top);
  296. /* Revert all neighbor status to ExStart. */
  297. for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi))
  298. {
  299. if ((nbrs = oi->nbrs) == NULL)
  300. continue;
  301. for (rn = route_top (nbrs); rn; rn = route_next (rn))
  302. {
  303. if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
  304. continue;
  305. if (nbr->state < NSM_ExStart)
  306. continue;
  307. if (IS_DEBUG_OSPF_EVENT)
  308. zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id));
  309. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  310. }
  311. }
  312. return;
  313. }
  314. struct ospf_neighbor *
  315. ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph,
  316. struct ospf_header *ospfh)
  317. {
  318. if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
  319. oi->type == OSPF_IFTYPE_POINTOPOINT)
  320. return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id));
  321. else
  322. return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src));
  323. }
  324. static struct ospf_neighbor *
  325. ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh,
  326. struct prefix *p)
  327. {
  328. struct ospf_neighbor *nbr;
  329. nbr = ospf_nbr_new (oi);
  330. nbr->state = NSM_Down;
  331. nbr->src = p->u.prefix4;
  332. memcpy (&nbr->address, p, sizeof (struct prefix));
  333. nbr->nbr_nbma = NULL;
  334. if (oi->type == OSPF_IFTYPE_NBMA)
  335. {
  336. struct ospf_nbr_nbma *nbr_nbma;
  337. struct listnode *node;
  338. for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma))
  339. {
  340. if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src))
  341. {
  342. nbr_nbma->nbr = nbr;
  343. nbr->nbr_nbma = nbr_nbma;
  344. if (nbr_nbma->t_poll)
  345. OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
  346. nbr->state_change = nbr_nbma->state_change + 1;
  347. }
  348. }
  349. }
  350. /* New nbr, save the crypto sequence number if necessary */
  351. if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
  352. nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
  353. if (IS_DEBUG_OSPF_EVENT)
  354. zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
  355. inet_ntoa (nbr->router_id));
  356. return nbr;
  357. }
  358. struct ospf_neighbor *
  359. ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh,
  360. struct ip *iph, struct prefix *p)
  361. {
  362. struct route_node *rn;
  363. struct prefix key;
  364. struct ospf_neighbor *nbr;
  365. key.family = AF_INET;
  366. key.prefixlen = IPV4_MAX_BITLEN;
  367. if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
  368. oi->type == OSPF_IFTYPE_POINTOPOINT)
  369. key.u.prefix4 = ospfh->router_id;/* index vlink and ptp nbrs by router-id */
  370. else
  371. key.u.prefix4 = iph->ip_src;
  372. rn = route_node_get (oi->nbrs, &key);
  373. if (rn->info)
  374. {
  375. route_unlock_node (rn);
  376. nbr = rn->info;
  377. if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
  378. {
  379. nbr->src = iph->ip_src;
  380. memcpy (&nbr->address, p, sizeof (struct prefix));
  381. }
  382. }
  383. else
  384. {
  385. rn->info = nbr = ospf_nbr_add (oi, ospfh, p);
  386. }
  387. nbr->router_id = ospfh->router_id;
  388. return nbr;
  389. }