ospf_ase.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * OSPF AS external route calculation.
  3. * Copyright (C) 1999, 2000 Alex Zinin, 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 it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * 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 Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "thread.h"
  24. #include "memory.h"
  25. #include "hash.h"
  26. #include "linklist.h"
  27. #include "prefix.h"
  28. #include "if.h"
  29. #include "table.h"
  30. #include "vty.h"
  31. #include "log.h"
  32. #include "ospfd/ospfd.h"
  33. #include "ospfd/ospf_interface.h"
  34. #include "ospfd/ospf_ism.h"
  35. #include "ospfd/ospf_asbr.h"
  36. #include "ospfd/ospf_lsa.h"
  37. #include "ospfd/ospf_lsdb.h"
  38. #include "ospfd/ospf_neighbor.h"
  39. #include "ospfd/ospf_nsm.h"
  40. #include "ospfd/ospf_spf.h"
  41. #include "ospfd/ospf_route.h"
  42. #include "ospfd/ospf_ase.h"
  43. #include "ospfd/ospf_zebra.h"
  44. #include "ospfd/ospf_dump.h"
  45. struct ospf_route *
  46. ospf_find_asbr_route (struct ospf *ospf,
  47. struct route_table *rtrs, struct prefix_ipv4 *asbr)
  48. {
  49. struct route_node *rn;
  50. struct ospf_route *or, *best = NULL;
  51. struct listnode *node;
  52. struct list *chosen;
  53. /* Sanity check. */
  54. if (rtrs == NULL)
  55. return NULL;
  56. rn = route_node_lookup (rtrs, (struct prefix *) asbr);
  57. if (! rn)
  58. return NULL;
  59. route_unlock_node (rn);
  60. chosen = list_new ();
  61. /* First try to find intra-area non-bb paths. */
  62. if (!CHECK_FLAG (ospf->config, OSPF_RFC1583_COMPATIBLE))
  63. for (ALL_LIST_ELEMENTS_RO ((struct list *) rn->info, node, or))
  64. if (or->cost < OSPF_LS_INFINITY)
  65. if (!OSPF_IS_AREA_ID_BACKBONE (or->u.std.area_id) &&
  66. or->path_type == OSPF_PATH_INTRA_AREA)
  67. listnode_add (chosen, or);
  68. /* If none is found -- look through all. */
  69. if (listcount (chosen) == 0)
  70. {
  71. list_free (chosen);
  72. chosen = rn->info;
  73. }
  74. /* Now find the route with least cost. */
  75. for (ALL_LIST_ELEMENTS_RO (chosen, node, or))
  76. if (or->cost < OSPF_LS_INFINITY)
  77. {
  78. if (best == NULL)
  79. best = or;
  80. else if (best->cost > or->cost)
  81. best = or;
  82. else if (best->cost == or->cost &&
  83. IPV4_ADDR_CMP (&best->u.std.area_id,
  84. &or->u.std.area_id) < 0)
  85. best = or;
  86. }
  87. if (chosen != rn->info)
  88. list_delete (chosen);
  89. return best;
  90. }
  91. struct ospf_route *
  92. ospf_find_asbr_route_through_area (struct route_table *rtrs,
  93. struct prefix_ipv4 *asbr,
  94. struct ospf_area *area)
  95. {
  96. struct route_node *rn;
  97. /* Sanity check. */
  98. if (rtrs == NULL)
  99. return NULL;
  100. rn = route_node_lookup (rtrs, (struct prefix *) asbr);
  101. if (rn)
  102. {
  103. struct listnode *node;
  104. struct ospf_route *or;
  105. route_unlock_node (rn);
  106. for (ALL_LIST_ELEMENTS_RO ((struct list *) rn->info, node, or))
  107. if (IPV4_ADDR_SAME (&or->u.std.area_id, &area->area_id))
  108. return or;
  109. }
  110. return NULL;
  111. }
  112. static void
  113. ospf_ase_complete_direct_routes (struct ospf_route *ro, struct in_addr nexthop)
  114. {
  115. struct listnode *node;
  116. struct ospf_path *op;
  117. for (ALL_LIST_ELEMENTS_RO (ro->paths, node, op))
  118. if (op->nexthop.s_addr == 0)
  119. op->nexthop.s_addr = nexthop.s_addr;
  120. }
  121. static int
  122. ospf_ase_forward_address_check (struct ospf *ospf, struct in_addr fwd_addr)
  123. {
  124. struct listnode *ifn;
  125. struct ospf_interface *oi;
  126. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, ifn, oi))
  127. if (if_is_operative (oi->ifp))
  128. if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
  129. if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &fwd_addr))
  130. return 0;
  131. return 1;
  132. }
  133. #if 0
  134. /* Calculate ASBR route. */
  135. static struct ospf_route *
  136. ospf_ase_calculate_asbr_route (struct ospf *ospf,
  137. struct route_table *rt_network,
  138. struct route_table *rt_router,
  139. struct as_external_lsa *al)
  140. {
  141. struct prefix_ipv4 asbr;
  142. struct ospf_route *asbr_route;
  143. struct route_node *rn;
  144. /* Find ASBR route from Router routing table. */
  145. asbr.family = AF_INET;
  146. asbr.prefix = al->header.adv_router;
  147. asbr.prefixlen = IPV4_MAX_BITLEN;
  148. apply_mask_ipv4 (&asbr);
  149. asbr_route = ospf_find_asbr_route (ospf, rt_router, &asbr);
  150. if (asbr_route == NULL)
  151. {
  152. if (IS_DEBUG_OSPF (lsa, LSA))
  153. zlog_debug ("ospf_ase_calculate(): Route to ASBR %s not found",
  154. inet_ntoa (asbr.prefix));
  155. return NULL;
  156. }
  157. if (!(asbr_route->u.std.flags & ROUTER_LSA_EXTERNAL))
  158. {
  159. if (IS_DEBUG_OSPF (lsa, LSA))
  160. zlog_debug ("ospf_ase_calculate(): Originating router is not an ASBR");
  161. return NULL;
  162. }
  163. if (al->e[0].fwd_addr.s_addr != 0)
  164. {
  165. if (IS_DEBUG_OSPF (lsa, LSA))
  166. zlog_debug ("ospf_ase_calculate(): "
  167. "Forwarding address is not 0.0.0.0.");
  168. if (! ospf_ase_forward_address_check (ospf, al->e[0].fwd_addr))
  169. {
  170. if (IS_DEBUG_OSPF (lsa, LSA))
  171. zlog_debug ("ospf_ase_calculate(): "
  172. "Forwarding address is one of our addresses, Ignore.");
  173. return NULL;
  174. }
  175. if (IS_DEBUG_OSPF (lsa, LSA))
  176. zlog_debug ("ospf_ase_calculate(): "
  177. "Looking up in the Network Routing Table.");
  178. /* Looking up the path to the fwd_addr from Network route. */
  179. asbr.family = AF_INET;
  180. asbr.prefix = al->e[0].fwd_addr;
  181. asbr.prefixlen = IPV4_MAX_BITLEN;
  182. rn = route_node_match (rt_network, (struct prefix *) &asbr);
  183. if (rn == NULL)
  184. {
  185. if (IS_DEBUG_OSPF (lsa, LSA))
  186. zlog_debug ("ospf_ase_calculate(): "
  187. "Couldn't find a route to the forwarding address.");
  188. return NULL;
  189. }
  190. route_unlock_node (rn);
  191. if ((asbr_route = rn->info) == NULL)
  192. {
  193. if (IS_DEBUG_OSPF (lsa, LSA))
  194. zlog_debug ("ospf_ase_calculate(): "
  195. "Somehow OSPF route to ASBR is lost");
  196. return NULL;
  197. }
  198. }
  199. return asbr_route;
  200. }
  201. #endif
  202. static struct ospf_route *
  203. ospf_ase_calculate_new_route (struct ospf_lsa *lsa,
  204. struct ospf_route *asbr_route, u_int32_t metric)
  205. {
  206. struct as_external_lsa *al;
  207. struct ospf_route *new;
  208. al = (struct as_external_lsa *) lsa->data;
  209. new = ospf_route_new ();
  210. /* Set redistributed type -- does make sense? */
  211. /* new->type = type; */
  212. new->id = al->header.id;
  213. new->mask = al->mask;
  214. if (!IS_EXTERNAL_METRIC (al->e[0].tos))
  215. {
  216. if (IS_DEBUG_OSPF (lsa, LSA))
  217. zlog_debug ("Route[External]: type-1 created.");
  218. new->path_type = OSPF_PATH_TYPE1_EXTERNAL;
  219. new->cost = asbr_route->cost + metric; /* X + Y */
  220. }
  221. else
  222. {
  223. if (IS_DEBUG_OSPF (lsa, LSA))
  224. zlog_debug ("Route[External]: type-2 created.");
  225. new->path_type = OSPF_PATH_TYPE2_EXTERNAL;
  226. new->cost = asbr_route->cost; /* X */
  227. new->u.ext.type2_cost = metric; /* Y */
  228. }
  229. new->type = OSPF_DESTINATION_NETWORK;
  230. new->u.ext.origin = lsa;
  231. new->u.ext.tag = ntohl (al->e[0].route_tag);
  232. new->u.ext.asbr = asbr_route;
  233. assert (new != asbr_route);
  234. return new;
  235. }
  236. #define OSPF_ASE_CALC_INTERVAL 1
  237. int
  238. ospf_ase_calculate_route (struct ospf *ospf, struct ospf_lsa * lsa)
  239. {
  240. u_int32_t metric;
  241. struct as_external_lsa *al;
  242. struct ospf_route *asbr_route;
  243. struct prefix_ipv4 asbr, p;
  244. struct route_node *rn;
  245. struct ospf_route *new, *or;
  246. int ret;
  247. assert (lsa);
  248. al = (struct as_external_lsa *) lsa->data;
  249. if (lsa->data->type == OSPF_AS_NSSA_LSA)
  250. if (IS_DEBUG_OSPF_NSSA)
  251. zlog_debug ("ospf_ase_calc(): Processing Type-7");
  252. /* Stay away from any Local Translated Type-7 LSAs */
  253. if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
  254. {
  255. if (IS_DEBUG_OSPF_NSSA)
  256. zlog_debug ("ospf_ase_calc(): Rejecting Local Xlt'd");
  257. return 0;
  258. }
  259. if (IS_DEBUG_OSPF (lsa, LSA))
  260. zlog_debug ("Route[External]: Calculate AS-external-LSA to %s/%d",
  261. inet_ntoa (al->header.id), ip_masklen (al->mask));
  262. /* (1) If the cost specified by the LSA is LSInfinity, or if the
  263. LSA's LS age is equal to MaxAge, then examine the next LSA. */
  264. if ((metric = GET_METRIC (al->e[0].metric)) >= OSPF_LS_INFINITY)
  265. {
  266. if (IS_DEBUG_OSPF (lsa, LSA))
  267. zlog_debug ("Route[External]: Metric is OSPF_LS_INFINITY");
  268. return 0;
  269. }
  270. if (IS_LSA_MAXAGE (lsa))
  271. {
  272. if (IS_DEBUG_OSPF (lsa, LSA))
  273. zlog_debug ("Route[External]: AS-external-LSA is MAXAGE");
  274. return 0;
  275. }
  276. /* (2) If the LSA was originated by the calculating router itself,
  277. examine the next LSA. */
  278. if (IS_LSA_SELF (lsa))
  279. {
  280. if (IS_DEBUG_OSPF (lsa, LSA))
  281. zlog_debug ("Route[External]: AS-external-LSA is self originated");
  282. return 0;
  283. }
  284. /* (3) Call the destination described by the LSA N. N's address is
  285. obtained by masking the LSA's Link State ID with the
  286. network/subnet mask contained in the body of the LSA. Look
  287. up the routing table entries (potentially one per attached
  288. area) for the AS boundary router (ASBR) that originated the
  289. LSA. If no entries exist for router ASBR (i.e., ASBR is
  290. unreachable), do nothing with this LSA and consider the next
  291. in the list. */
  292. asbr.family = AF_INET;
  293. asbr.prefix = al->header.adv_router;
  294. asbr.prefixlen = IPV4_MAX_BITLEN;
  295. apply_mask_ipv4 (&asbr);
  296. asbr_route = ospf_find_asbr_route (ospf, ospf->new_rtrs, &asbr);
  297. if (asbr_route == NULL)
  298. {
  299. if (IS_DEBUG_OSPF (lsa, LSA))
  300. zlog_debug ("Route[External]: Can't find originating ASBR route");
  301. return 0;
  302. }
  303. if (!(asbr_route->u.std.flags & ROUTER_LSA_EXTERNAL))
  304. {
  305. if (IS_DEBUG_OSPF (lsa, LSA))
  306. zlog_debug ("Route[External]: Originating router is not an ASBR");
  307. return 0;
  308. }
  309. /* Else, this LSA describes an AS external path to destination
  310. N. Examine the forwarding address specified in the AS-
  311. external-LSA. This indicates the IP address to which
  312. packets for the destination should be forwarded. */
  313. if (al->e[0].fwd_addr.s_addr == 0)
  314. {
  315. /* If the forwarding address is set to 0.0.0.0, packets should
  316. be sent to the ASBR itself. Among the multiple routing table
  317. entries for the ASBR, select the preferred entry as follows.
  318. If RFC1583Compatibility is set to "disabled", prune the set
  319. of routing table entries for the ASBR as described in
  320. Section 16.4.1. In any case, among the remaining routing
  321. table entries, select the routing table entry with the least
  322. cost; when there are multiple least cost routing table
  323. entries the entry whose associated area has the largest OSPF
  324. Area ID (when considered as an unsigned 32-bit integer) is
  325. chosen. */
  326. /* asbr_route already contains the requested route */
  327. }
  328. else
  329. {
  330. /* If the forwarding address is non-zero, look up the
  331. forwarding address in the routing table.[24] The matching
  332. routing table entry must specify an intra-area or inter-area
  333. path; if no such path exists, do nothing with the LSA and
  334. consider the next in the list. */
  335. if (! ospf_ase_forward_address_check (ospf, al->e[0].fwd_addr))
  336. {
  337. if (IS_DEBUG_OSPF (lsa, LSA))
  338. zlog_debug ("Route[External]: Forwarding address is our router "
  339. "address");
  340. return 0;
  341. }
  342. asbr.family = AF_INET;
  343. asbr.prefix = al->e[0].fwd_addr;
  344. asbr.prefixlen = IPV4_MAX_BITLEN;
  345. rn = route_node_match (ospf->new_table, (struct prefix *) &asbr);
  346. if (rn == NULL || (asbr_route = rn->info) == NULL)
  347. {
  348. if (IS_DEBUG_OSPF (lsa, LSA))
  349. zlog_debug ("Route[External]: Can't find route to forwarding "
  350. "address");
  351. if (rn)
  352. route_unlock_node (rn);
  353. return 0;
  354. }
  355. route_unlock_node (rn);
  356. }
  357. /* (4) Let X be the cost specified by the preferred routing table
  358. entry for the ASBR/forwarding address, and Y the cost
  359. specified in the LSA. X is in terms of the link state
  360. metric, and Y is a type 1 or 2 external metric. */
  361. /* (5) Look up the routing table entry for the destination N. If
  362. no entry exists for N, install the AS external path to N,
  363. with next hop equal to the list of next hops to the
  364. forwarding address, and advertising router equal to ASBR.
  365. If the external metric type is 1, then the path-type is set
  366. to type 1 external and the cost is equal to X+Y. If the
  367. external metric type is 2, the path-type is set to type 2
  368. external, the link state component of the route's cost is X,
  369. and the type 2 cost is Y. */
  370. new = ospf_ase_calculate_new_route (lsa, asbr_route, metric);
  371. /* (6) Compare the AS external path described by the LSA with the
  372. existing paths in N's routing table entry, as follows. If
  373. the new path is preferred, it replaces the present paths in
  374. N's routing table entry. If the new path is of equal
  375. preference, it is added to N's routing table entry's list of
  376. paths. */
  377. /* Set prefix. */
  378. p.family = AF_INET;
  379. p.prefix = al->header.id;
  380. p.prefixlen = ip_masklen (al->mask);
  381. /* if there is a Intra/Inter area route to the N
  382. do not install external route */
  383. if ((rn = route_node_lookup (ospf->new_table,
  384. (struct prefix *) &p)))
  385. {
  386. route_unlock_node(rn);
  387. if (rn->info == NULL)
  388. zlog_info ("Route[External]: rn->info NULL");
  389. if (new)
  390. ospf_route_free (new);
  391. return 0;
  392. }
  393. /* Find a route to the same dest */
  394. /* If there is no route, create new one. */
  395. if ((rn = route_node_lookup (ospf->new_external_route,
  396. (struct prefix *) &p)))
  397. route_unlock_node(rn);
  398. if (!rn || (or = rn->info) == NULL)
  399. {
  400. if (IS_DEBUG_OSPF (lsa, LSA))
  401. zlog_debug ("Route[External]: Adding a new route %s/%d",
  402. inet_ntoa (p.prefix), p.prefixlen);
  403. ospf_route_add (ospf->new_external_route, &p, new, asbr_route);
  404. if (al->e[0].fwd_addr.s_addr)
  405. ospf_ase_complete_direct_routes (new, al->e[0].fwd_addr);
  406. return 0;
  407. }
  408. else
  409. {
  410. /* (a) Intra-area and inter-area paths are always preferred
  411. over AS external paths.
  412. (b) Type 1 external paths are always preferred over type 2
  413. external paths. When all paths are type 2 external
  414. paths, the paths with the smallest advertised type 2
  415. metric are always preferred. */
  416. ret = ospf_route_cmp (ospf, new, or);
  417. /* (c) If the new AS external path is still indistinguishable
  418. from the current paths in the N's routing table entry,
  419. and RFC1583Compatibility is set to "disabled", select
  420. the preferred paths based on the intra-AS paths to the
  421. ASBR/forwarding addresses, as specified in Section
  422. 16.4.1.
  423. (d) If the new AS external path is still indistinguishable
  424. from the current paths in the N's routing table entry,
  425. select the preferred path based on a least cost
  426. comparison. Type 1 external paths are compared by
  427. looking at the sum of the distance to the forwarding
  428. address and the advertised type 1 metric (X+Y). Type 2
  429. external paths advertising equal type 2 metrics are
  430. compared by looking at the distance to the forwarding
  431. addresses.
  432. */
  433. /* New route is better */
  434. if (ret < 0)
  435. {
  436. if (IS_DEBUG_OSPF (lsa, LSA))
  437. zlog_debug ("Route[External]: New route is better");
  438. ospf_route_subst (rn, new, asbr_route);
  439. if (al->e[0].fwd_addr.s_addr)
  440. ospf_ase_complete_direct_routes (new, al->e[0].fwd_addr);
  441. or = new;
  442. new = NULL;
  443. }
  444. /* Old route is better */
  445. else if (ret > 0)
  446. {
  447. if (IS_DEBUG_OSPF (lsa, LSA))
  448. zlog_debug ("Route[External]: Old route is better");
  449. /* do nothing */
  450. }
  451. /* Routes are equal */
  452. else
  453. {
  454. if (IS_DEBUG_OSPF (lsa, LSA))
  455. zlog_debug ("Route[External]: Routes are equal");
  456. ospf_route_copy_nexthops (or, asbr_route->paths);
  457. if (al->e[0].fwd_addr.s_addr)
  458. ospf_ase_complete_direct_routes (or, al->e[0].fwd_addr);
  459. }
  460. }
  461. /* Make sure setting newly calculated ASBR route.*/
  462. or->u.ext.asbr = asbr_route;
  463. if (new)
  464. ospf_route_free (new);
  465. lsa->route = or;
  466. return 0;
  467. }
  468. static int
  469. ospf_ase_route_match_same (struct route_table *rt, struct prefix *prefix,
  470. struct ospf_route *newor)
  471. {
  472. struct route_node *rn;
  473. struct ospf_route *or;
  474. struct ospf_path *op;
  475. struct ospf_path *newop;
  476. struct listnode *n1;
  477. struct listnode *n2;
  478. if (! rt || ! prefix)
  479. return 0;
  480. rn = route_node_lookup (rt, prefix);
  481. if (! rn)
  482. return 0;
  483. route_unlock_node (rn);
  484. or = rn->info;
  485. if (or->path_type != newor->path_type)
  486. return 0;
  487. switch (or->path_type)
  488. {
  489. case OSPF_PATH_TYPE1_EXTERNAL:
  490. if (or->cost != newor->cost)
  491. return 0;
  492. break;
  493. case OSPF_PATH_TYPE2_EXTERNAL:
  494. if ((or->cost != newor->cost) ||
  495. (or->u.ext.type2_cost != newor->u.ext.type2_cost))
  496. return 0;
  497. break;
  498. default:
  499. assert (0);
  500. return 0;
  501. }
  502. if (or->paths->count != newor->paths->count)
  503. return 0;
  504. /* Check each path. */
  505. for (n1 = listhead (or->paths), n2 = listhead (newor->paths);
  506. n1 && n2; n1 = listnextnode (n1), n2 = listnextnode (n2))
  507. {
  508. op = listgetdata (n1);
  509. newop = listgetdata (n2);
  510. if (! IPV4_ADDR_SAME (&op->nexthop, &newop->nexthop))
  511. return 0;
  512. if (op->ifindex != newop->ifindex)
  513. return 0;
  514. }
  515. return 1;
  516. }
  517. static int
  518. ospf_ase_compare_tables (struct route_table *new_external_route,
  519. struct route_table *old_external_route)
  520. {
  521. struct route_node *rn, *new_rn;
  522. struct ospf_route *or;
  523. /* Remove deleted routes */
  524. for (rn = route_top (old_external_route); rn; rn = route_next (rn))
  525. if ((or = rn->info))
  526. {
  527. if (! (new_rn = route_node_lookup (new_external_route, &rn->p)))
  528. ospf_zebra_delete ((struct prefix_ipv4 *) &rn->p, or);
  529. else
  530. route_unlock_node (new_rn);
  531. }
  532. /* Install new routes */
  533. for (rn = route_top (new_external_route); rn; rn = route_next (rn))
  534. if ((or = rn->info) != NULL)
  535. if (! ospf_ase_route_match_same (old_external_route, &rn->p, or))
  536. ospf_zebra_add ((struct prefix_ipv4 *) &rn->p, or);
  537. return 0;
  538. }
  539. static int
  540. ospf_ase_calculate_timer (struct thread *t)
  541. {
  542. struct ospf *ospf;
  543. struct ospf_lsa *lsa;
  544. struct route_node *rn;
  545. struct listnode *node;
  546. struct ospf_area *area;
  547. struct timeval start_time, stop_time;
  548. ospf = THREAD_ARG (t);
  549. ospf->t_ase_calc = NULL;
  550. if (ospf->ase_calc)
  551. {
  552. ospf->ase_calc = 0;
  553. quagga_gettime(QUAGGA_CLK_MONOTONIC, &start_time);
  554. /* Calculate external route for each AS-external-LSA */
  555. LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
  556. ospf_ase_calculate_route (ospf, lsa);
  557. /* This version simple adds to the table all NSSA areas */
  558. if (ospf->anyNSSA)
  559. for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
  560. {
  561. if (IS_DEBUG_OSPF_NSSA)
  562. zlog_debug ("ospf_ase_calculate_timer(): looking at area %s",
  563. inet_ntoa (area->area_id));
  564. if (area->external_routing == OSPF_AREA_NSSA)
  565. LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
  566. ospf_ase_calculate_route (ospf, lsa);
  567. }
  568. /* kevinm: And add the NSSA routes in ospf_top */
  569. LSDB_LOOP (NSSA_LSDB (ospf),rn,lsa)
  570. ospf_ase_calculate_route(ospf,lsa);
  571. /* Compare old and new external routing table and install the
  572. difference info zebra/kernel */
  573. ospf_ase_compare_tables (ospf->new_external_route,
  574. ospf->old_external_route);
  575. /* Delete old external routing table */
  576. ospf_route_table_free (ospf->old_external_route);
  577. ospf->old_external_route = ospf->new_external_route;
  578. ospf->new_external_route = route_table_init ();
  579. quagga_gettime(QUAGGA_CLK_MONOTONIC, &stop_time);
  580. zlog_info ("SPF Processing Time(usecs): External Routes: %ld\n",
  581. (stop_time.tv_sec - start_time.tv_sec)*1000000L+
  582. (stop_time.tv_usec - start_time.tv_usec));
  583. }
  584. return 0;
  585. }
  586. void
  587. ospf_ase_calculate_schedule (struct ospf *ospf)
  588. {
  589. if (ospf == NULL)
  590. return;
  591. ospf->ase_calc = 1;
  592. }
  593. void
  594. ospf_ase_calculate_timer_add (struct ospf *ospf)
  595. {
  596. if (ospf == NULL)
  597. return;
  598. if (! ospf->t_ase_calc)
  599. ospf->t_ase_calc = thread_add_timer (master, ospf_ase_calculate_timer,
  600. ospf, OSPF_ASE_CALC_INTERVAL);
  601. }
  602. void
  603. ospf_ase_register_external_lsa (struct ospf_lsa *lsa, struct ospf *top)
  604. {
  605. struct route_node *rn;
  606. struct prefix_ipv4 p;
  607. struct list *lst;
  608. struct as_external_lsa *al;
  609. al = (struct as_external_lsa *) lsa->data;
  610. p.family = AF_INET;
  611. p.prefix = lsa->data->id;
  612. p.prefixlen = ip_masklen (al->mask);
  613. apply_mask_ipv4 (&p);
  614. rn = route_node_get (top->external_lsas, (struct prefix *) &p);
  615. if ((lst = rn->info) == NULL)
  616. rn->info = lst = list_new();
  617. /* We assume that if LSA is deleted from DB
  618. is is also deleted from this RT */
  619. listnode_add (lst, ospf_lsa_lock (lsa)); /* external_lsas lst */
  620. }
  621. void
  622. ospf_ase_unregister_external_lsa (struct ospf_lsa *lsa, struct ospf *top)
  623. {
  624. struct route_node *rn;
  625. struct prefix_ipv4 p;
  626. struct list *lst;
  627. struct as_external_lsa *al;
  628. al = (struct as_external_lsa *) lsa->data;
  629. p.family = AF_INET;
  630. p.prefix = lsa->data->id;
  631. p.prefixlen = ip_masklen (al->mask);
  632. apply_mask_ipv4 (&p);
  633. rn = route_node_get (top->external_lsas, (struct prefix *) &p);
  634. lst = rn->info;
  635. /* XXX lst can be NULL */
  636. if (lst) {
  637. listnode_delete (lst, lsa);
  638. ospf_lsa_unlock (&lsa); /* external_lsas list */
  639. }
  640. }
  641. void
  642. ospf_ase_external_lsas_finish (struct route_table *rt)
  643. {
  644. struct route_node *rn;
  645. struct ospf_lsa *lsa;
  646. struct list *lst;
  647. struct listnode *node, *nnode;
  648. for (rn = route_top (rt); rn; rn = route_next (rn))
  649. if ((lst = rn->info) != NULL)
  650. {
  651. for (ALL_LIST_ELEMENTS (lst, node, nnode, lsa))
  652. ospf_lsa_unlock (&lsa); /* external_lsas lst */
  653. list_delete (lst);
  654. }
  655. route_table_finish (rt);
  656. }
  657. void
  658. ospf_ase_incremental_update (struct ospf *ospf, struct ospf_lsa *lsa)
  659. {
  660. struct list *lsas;
  661. struct listnode *node;
  662. struct route_node *rn, *rn2;
  663. struct prefix_ipv4 p;
  664. struct route_table *tmp_old;
  665. struct as_external_lsa *al;
  666. al = (struct as_external_lsa *) lsa->data;
  667. p.family = AF_INET;
  668. p.prefix = lsa->data->id;
  669. p.prefixlen = ip_masklen (al->mask);
  670. apply_mask_ipv4 (&p);
  671. /* if new_table is NULL, there was no spf calculation, thus
  672. incremental update is unneeded */
  673. if (!ospf->new_table)
  674. return;
  675. /* If there is already an intra-area or inter-area route
  676. to the destination, no recalculation is necessary
  677. (internal routes take precedence). */
  678. rn = route_node_lookup (ospf->new_table, (struct prefix *) &p);
  679. if (rn)
  680. {
  681. route_unlock_node (rn);
  682. if (rn->info)
  683. return;
  684. }
  685. rn = route_node_lookup (ospf->external_lsas, (struct prefix *) &p);
  686. assert (rn);
  687. assert (rn->info);
  688. lsas = rn->info;
  689. route_unlock_node (rn);
  690. for (ALL_LIST_ELEMENTS_RO (lsas, node, lsa))
  691. ospf_ase_calculate_route (ospf, lsa);
  692. /* prepare temporary old routing table for compare */
  693. tmp_old = route_table_init ();
  694. rn = route_node_lookup (ospf->old_external_route, (struct prefix *) &p);
  695. if (rn && rn->info)
  696. {
  697. rn2 = route_node_get (tmp_old, (struct prefix *) &p);
  698. rn2->info = rn->info;
  699. }
  700. /* install changes to zebra */
  701. ospf_ase_compare_tables (ospf->new_external_route, tmp_old);
  702. /* update ospf->old_external_route table */
  703. if (rn && rn->info)
  704. ospf_route_free ((struct ospf_route *) rn->info);
  705. rn2 = route_node_lookup (ospf->new_external_route, (struct prefix *) &p);
  706. /* if new route exists, install it to ospf->old_external_route */
  707. if (rn2 && rn2->info)
  708. {
  709. if (!rn)
  710. rn = route_node_get (ospf->old_external_route, (struct prefix *) &p);
  711. rn->info = rn2->info;
  712. }
  713. else
  714. {
  715. /* remove route node from ospf->old_external_route */
  716. if (rn)
  717. {
  718. rn->info = NULL;
  719. route_unlock_node (rn);
  720. route_unlock_node (rn);
  721. }
  722. }
  723. if (rn2)
  724. {
  725. /* rn2->info is stored in route node of ospf->old_external_route */
  726. rn2->info = NULL;
  727. route_unlock_node (rn2);
  728. route_unlock_node (rn2);
  729. }
  730. route_table_finish (tmp_old);
  731. }