bgp_fsm.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /* BGP-4 Finite State Machine
  2. From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
  3. Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
  4. This file is part of GNU Zebra.
  5. GNU Zebra is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option) any
  8. later version.
  9. GNU Zebra is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Zebra; see the file COPYING. If not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  16. 02111-1307, USA. */
  17. #include <zebra.h>
  18. #include "linklist.h"
  19. #include "prefix.h"
  20. #include "vty.h"
  21. #include "sockunion.h"
  22. #include "thread.h"
  23. #include "log.h"
  24. #include "stream.h"
  25. #include "memory.h"
  26. #include "plist.h"
  27. #include "workqueue.h"
  28. #include "filter.h"
  29. #include "bgpd/bgpd.h"
  30. #include "bgpd/bgp_attr.h"
  31. #include "bgpd/bgp_debug.h"
  32. #include "bgpd/bgp_fsm.h"
  33. #include "bgpd/bgp_packet.h"
  34. #include "bgpd/bgp_network.h"
  35. #include "bgpd/bgp_route.h"
  36. #include "bgpd/bgp_dump.h"
  37. #include "bgpd/bgp_open.h"
  38. #ifdef HAVE_SNMP
  39. #include "bgpd/bgp_snmp.h"
  40. #endif /* HAVE_SNMP */
  41. /* BGP FSM (finite state machine) has three types of functions. Type
  42. one is thread functions. Type two is event functions. Type three
  43. is FSM functions. Timer functions are set by bgp_timer_set
  44. function. */
  45. /* BGP event function. */
  46. int bgp_event (struct thread *);
  47. /* BGP thread functions. */
  48. static int bgp_start_timer (struct thread *);
  49. static int bgp_connect_timer (struct thread *);
  50. static int bgp_holdtime_timer (struct thread *);
  51. static int bgp_keepalive_timer (struct thread *);
  52. /* BGP FSM functions. */
  53. static int bgp_start (struct peer *);
  54. /* BGP start timer jitter. */
  55. static int
  56. bgp_start_jitter (int time)
  57. {
  58. return ((random () % (time + 1)) - (time / 2));
  59. }
  60. /* Check if suppress start/restart of sessions to peer. */
  61. #define BGP_PEER_START_SUPPRESSED(P) \
  62. (CHECK_FLAG ((P)->flags, PEER_FLAG_SHUTDOWN) \
  63. || CHECK_FLAG ((P)->sflags, PEER_STATUS_PREFIX_OVERFLOW))
  64. /* Hook function called after bgp event is occered. And vty's
  65. neighbor command invoke this function after making neighbor
  66. structure. */
  67. void
  68. bgp_timer_set (struct peer *peer)
  69. {
  70. int jitter = 0;
  71. switch (peer->status)
  72. {
  73. case Idle:
  74. /* First entry point of peer's finite state machine. In Idle
  75. status start timer is on unless peer is shutdown or peer is
  76. inactive. All other timer must be turned off */
  77. if (BGP_PEER_START_SUPPRESSED (peer) || ! peer_active (peer))
  78. {
  79. BGP_TIMER_OFF (peer->t_start);
  80. }
  81. else
  82. {
  83. jitter = bgp_start_jitter (peer->v_start);
  84. BGP_TIMER_ON (peer->t_start, bgp_start_timer,
  85. peer->v_start + jitter);
  86. }
  87. BGP_TIMER_OFF (peer->t_connect);
  88. BGP_TIMER_OFF (peer->t_holdtime);
  89. BGP_TIMER_OFF (peer->t_keepalive);
  90. BGP_TIMER_OFF (peer->t_routeadv);
  91. break;
  92. case Connect:
  93. /* After start timer is expired, the peer moves to Connnect
  94. status. Make sure start timer is off and connect timer is
  95. on. */
  96. BGP_TIMER_OFF (peer->t_start);
  97. BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
  98. BGP_TIMER_OFF (peer->t_holdtime);
  99. BGP_TIMER_OFF (peer->t_keepalive);
  100. BGP_TIMER_OFF (peer->t_routeadv);
  101. break;
  102. case Active:
  103. /* Active is waiting connection from remote peer. And if
  104. connect timer is expired, change status to Connect. */
  105. BGP_TIMER_OFF (peer->t_start);
  106. /* If peer is passive mode, do not set connect timer. */
  107. if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)
  108. || CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
  109. {
  110. BGP_TIMER_OFF (peer->t_connect);
  111. }
  112. else
  113. {
  114. BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
  115. }
  116. BGP_TIMER_OFF (peer->t_holdtime);
  117. BGP_TIMER_OFF (peer->t_keepalive);
  118. BGP_TIMER_OFF (peer->t_routeadv);
  119. break;
  120. case OpenSent:
  121. /* OpenSent status. */
  122. BGP_TIMER_OFF (peer->t_start);
  123. BGP_TIMER_OFF (peer->t_connect);
  124. if (peer->v_holdtime != 0)
  125. {
  126. BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
  127. peer->v_holdtime);
  128. }
  129. else
  130. {
  131. BGP_TIMER_OFF (peer->t_holdtime);
  132. }
  133. BGP_TIMER_OFF (peer->t_keepalive);
  134. BGP_TIMER_OFF (peer->t_routeadv);
  135. break;
  136. case OpenConfirm:
  137. /* OpenConfirm status. */
  138. BGP_TIMER_OFF (peer->t_start);
  139. BGP_TIMER_OFF (peer->t_connect);
  140. /* If the negotiated Hold Time value is zero, then the Hold Time
  141. timer and KeepAlive timers are not started. */
  142. if (peer->v_holdtime == 0)
  143. {
  144. BGP_TIMER_OFF (peer->t_holdtime);
  145. BGP_TIMER_OFF (peer->t_keepalive);
  146. }
  147. else
  148. {
  149. BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
  150. peer->v_holdtime);
  151. BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
  152. peer->v_keepalive);
  153. }
  154. BGP_TIMER_OFF (peer->t_routeadv);
  155. break;
  156. case Established:
  157. /* In Established status start and connect timer is turned
  158. off. */
  159. BGP_TIMER_OFF (peer->t_start);
  160. BGP_TIMER_OFF (peer->t_connect);
  161. /* Same as OpenConfirm, if holdtime is zero then both holdtime
  162. and keepalive must be turned off. */
  163. if (peer->v_holdtime == 0)
  164. {
  165. BGP_TIMER_OFF (peer->t_holdtime);
  166. BGP_TIMER_OFF (peer->t_keepalive);
  167. }
  168. else
  169. {
  170. BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
  171. peer->v_holdtime);
  172. BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
  173. peer->v_keepalive);
  174. }
  175. break;
  176. case Deleted:
  177. BGP_TIMER_OFF (peer->t_gr_restart);
  178. BGP_TIMER_OFF (peer->t_gr_stale);
  179. BGP_TIMER_OFF (peer->t_pmax_restart);
  180. case Clearing:
  181. BGP_TIMER_OFF (peer->t_start);
  182. BGP_TIMER_OFF (peer->t_connect);
  183. BGP_TIMER_OFF (peer->t_holdtime);
  184. BGP_TIMER_OFF (peer->t_keepalive);
  185. BGP_TIMER_OFF (peer->t_routeadv);
  186. }
  187. }
  188. /* BGP start timer. This function set BGP_Start event to thread value
  189. and process event. */
  190. static int
  191. bgp_start_timer (struct thread *thread)
  192. {
  193. struct peer *peer;
  194. peer = THREAD_ARG (thread);
  195. peer->t_start = NULL;
  196. if (BGP_DEBUG (fsm, FSM))
  197. zlog (peer->log, LOG_DEBUG,
  198. "%s [FSM] Timer (start timer expire).", peer->host);
  199. THREAD_VAL (thread) = BGP_Start;
  200. bgp_event (thread); /* bgp_event unlocks peer */
  201. return 0;
  202. }
  203. /* BGP connect retry timer. */
  204. static int
  205. bgp_connect_timer (struct thread *thread)
  206. {
  207. struct peer *peer;
  208. peer = THREAD_ARG (thread);
  209. peer->t_connect = NULL;
  210. if (BGP_DEBUG (fsm, FSM))
  211. zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
  212. peer->host);
  213. THREAD_VAL (thread) = ConnectRetry_timer_expired;
  214. bgp_event (thread); /* bgp_event unlocks peer */
  215. return 0;
  216. }
  217. /* BGP holdtime timer. */
  218. static int
  219. bgp_holdtime_timer (struct thread *thread)
  220. {
  221. struct peer *peer;
  222. peer = THREAD_ARG (thread);
  223. peer->t_holdtime = NULL;
  224. if (BGP_DEBUG (fsm, FSM))
  225. zlog (peer->log, LOG_DEBUG,
  226. "%s [FSM] Timer (holdtime timer expire)",
  227. peer->host);
  228. THREAD_VAL (thread) = Hold_Timer_expired;
  229. bgp_event (thread); /* bgp_event unlocks peer */
  230. return 0;
  231. }
  232. /* BGP keepalive fire ! */
  233. static int
  234. bgp_keepalive_timer (struct thread *thread)
  235. {
  236. struct peer *peer;
  237. peer = THREAD_ARG (thread);
  238. peer->t_keepalive = NULL;
  239. if (BGP_DEBUG (fsm, FSM))
  240. zlog (peer->log, LOG_DEBUG,
  241. "%s [FSM] Timer (keepalive timer expire)",
  242. peer->host);
  243. THREAD_VAL (thread) = KeepAlive_timer_expired;
  244. bgp_event (thread); /* bgp_event unlocks peer */
  245. return 0;
  246. }
  247. static int
  248. bgp_routeadv_timer (struct thread *thread)
  249. {
  250. struct peer *peer;
  251. peer = THREAD_ARG (thread);
  252. peer->t_routeadv = NULL;
  253. if (BGP_DEBUG (fsm, FSM))
  254. zlog (peer->log, LOG_DEBUG,
  255. "%s [FSM] Timer (routeadv timer expire)",
  256. peer->host);
  257. peer->synctime = bgp_clock ();
  258. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  259. BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer,
  260. peer->v_routeadv);
  261. return 0;
  262. }
  263. /* BGP Peer Down Cause */
  264. const char *peer_down_str[] =
  265. {
  266. "",
  267. "Router ID changed",
  268. "Remote AS changed",
  269. "Local AS change",
  270. "Cluster ID changed",
  271. "Confederation identifier changed",
  272. "Confederation peer changed",
  273. "RR client config change",
  274. "RS client config change",
  275. "Update source change",
  276. "Address family activated",
  277. "Admin. shutdown",
  278. "User reset",
  279. "BGP Notification received",
  280. "BGP Notification send",
  281. "Peer closed the session",
  282. "Neighbor deleted",
  283. "Peer-group add member",
  284. "Peer-group delete member",
  285. "Capability changed",
  286. "Passive config change",
  287. "Multihop config change",
  288. "NSF peer closed the session"
  289. };
  290. static int
  291. bgp_graceful_restart_timer_expire (struct thread *thread)
  292. {
  293. struct peer *peer;
  294. afi_t afi;
  295. safi_t safi;
  296. peer = THREAD_ARG (thread);
  297. peer->t_gr_restart = NULL;
  298. /* NSF delete stale route */
  299. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  300. for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
  301. if (peer->nsf[afi][safi])
  302. bgp_clear_stale_route (peer, afi, safi);
  303. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  304. BGP_TIMER_OFF (peer->t_gr_stale);
  305. if (BGP_DEBUG (events, EVENTS))
  306. {
  307. zlog_debug ("%s graceful restart timer expired", peer->host);
  308. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  309. }
  310. bgp_timer_set (peer);
  311. return 0;
  312. }
  313. static int
  314. bgp_graceful_stale_timer_expire (struct thread *thread)
  315. {
  316. struct peer *peer;
  317. afi_t afi;
  318. safi_t safi;
  319. peer = THREAD_ARG (thread);
  320. peer->t_gr_stale = NULL;
  321. if (BGP_DEBUG (events, EVENTS))
  322. zlog_debug ("%s graceful restart stalepath timer expired", peer->host);
  323. /* NSF delete stale route */
  324. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  325. for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
  326. if (peer->nsf[afi][safi])
  327. bgp_clear_stale_route (peer, afi, safi);
  328. return 0;
  329. }
  330. /* Called after event occured, this function change status and reset
  331. read/write and timer thread. */
  332. void
  333. bgp_fsm_change_status (struct peer *peer, int status)
  334. {
  335. bgp_dump_state (peer, peer->status, status);
  336. /* Transition into Clearing or Deleted must /always/ clear all routes..
  337. * (and must do so before actually changing into Deleted..
  338. */
  339. if (status >= Clearing)
  340. {
  341. bgp_clear_route_all (peer);
  342. /* If no route was queued for the clear-node processing, generate the
  343. * completion event here. This is needed because if there are no routes
  344. * to trigger the background clear-node thread, the event won't get
  345. * generated and the peer would be stuck in Clearing. Note that this
  346. * event is for the peer and helps the peer transition out of Clearing
  347. * state; it should not be generated per (AFI,SAFI). The event is
  348. * directly posted here without calling clear_node_complete() as we
  349. * shouldn't do an extra unlock. This event will get processed after
  350. * the state change that happens below, so peer will be in Clearing
  351. * (or Deleted).
  352. */
  353. if (!work_queue_is_scheduled (peer->clear_node_queue))
  354. BGP_EVENT_ADD (peer, Clearing_Completed);
  355. }
  356. /* Preserve old status and change into new status. */
  357. peer->ostatus = peer->status;
  358. peer->status = status;
  359. if (BGP_DEBUG (normal, NORMAL))
  360. zlog_debug ("%s went from %s to %s",
  361. peer->host,
  362. LOOKUP (bgp_status_msg, peer->ostatus),
  363. LOOKUP (bgp_status_msg, peer->status));
  364. }
  365. /* Flush the event queue and ensure the peer is shut down */
  366. static int
  367. bgp_clearing_completed (struct peer *peer)
  368. {
  369. int rc = bgp_stop(peer);
  370. BGP_EVENT_FLUSH (peer);
  371. return rc;
  372. }
  373. /* Administrative BGP peer stop event. */
  374. /* May be called multiple times for the same peer */
  375. int
  376. bgp_stop (struct peer *peer)
  377. {
  378. afi_t afi;
  379. safi_t safi;
  380. char orf_name[BUFSIZ];
  381. /* Can't do this in Clearing; events are used for state transitions */
  382. if (peer->status != Clearing)
  383. {
  384. /* Delete all existing events of the peer */
  385. BGP_EVENT_FLUSH (peer);
  386. }
  387. /* Increment Dropped count. */
  388. if (peer->status == Established)
  389. {
  390. peer->dropped++;
  391. /* bgp log-neighbor-changes of neighbor Down */
  392. if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
  393. zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
  394. peer_down_str [(int) peer->last_reset]);
  395. /* graceful restart */
  396. if (peer->t_gr_stale)
  397. {
  398. BGP_TIMER_OFF (peer->t_gr_stale);
  399. if (BGP_DEBUG (events, EVENTS))
  400. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  401. }
  402. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
  403. {
  404. if (BGP_DEBUG (events, EVENTS))
  405. {
  406. zlog_debug ("%s graceful restart timer started for %d sec",
  407. peer->host, peer->v_gr_restart);
  408. zlog_debug ("%s graceful restart stalepath timer started for %d sec",
  409. peer->host, peer->bgp->stalepath_time);
  410. }
  411. BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
  412. peer->v_gr_restart);
  413. BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
  414. peer->bgp->stalepath_time);
  415. }
  416. else
  417. {
  418. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  419. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  420. for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
  421. peer->nsf[afi][safi] = 0;
  422. }
  423. /* set last reset time */
  424. peer->resettime = peer->uptime = bgp_clock ();
  425. #ifdef HAVE_SNMP
  426. bgpTrapBackwardTransition (peer);
  427. #endif /* HAVE_SNMP */
  428. /* Reset peer synctime */
  429. peer->synctime = 0;
  430. }
  431. /* Stop read and write threads when exists. */
  432. BGP_READ_OFF (peer->t_read);
  433. BGP_WRITE_OFF (peer->t_write);
  434. /* Stop all timers. */
  435. BGP_TIMER_OFF (peer->t_start);
  436. BGP_TIMER_OFF (peer->t_connect);
  437. BGP_TIMER_OFF (peer->t_holdtime);
  438. BGP_TIMER_OFF (peer->t_keepalive);
  439. BGP_TIMER_OFF (peer->t_routeadv);
  440. /* Stream reset. */
  441. peer->packet_size = 0;
  442. /* Clear input and output buffer. */
  443. if (peer->ibuf)
  444. stream_reset (peer->ibuf);
  445. if (peer->work)
  446. stream_reset (peer->work);
  447. if (peer->obuf)
  448. stream_fifo_clean (peer->obuf);
  449. /* Close of file descriptor. */
  450. if (peer->fd >= 0)
  451. {
  452. close (peer->fd);
  453. peer->fd = -1;
  454. }
  455. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  456. for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
  457. {
  458. /* Reset all negotiated variables */
  459. peer->afc_nego[afi][safi] = 0;
  460. peer->afc_adv[afi][safi] = 0;
  461. peer->afc_recv[afi][safi] = 0;
  462. /* peer address family capability flags*/
  463. peer->af_cap[afi][safi] = 0;
  464. /* peer address family status flags*/
  465. peer->af_sflags[afi][safi] = 0;
  466. /* Received ORF prefix-filter */
  467. peer->orf_plist[afi][safi] = NULL;
  468. /* ORF received prefix-filter pnt */
  469. sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
  470. prefix_bgp_orf_remove_all (afi, orf_name);
  471. }
  472. /* Reset keepalive and holdtime */
  473. if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
  474. {
  475. peer->v_keepalive = peer->keepalive;
  476. peer->v_holdtime = peer->holdtime;
  477. }
  478. else
  479. {
  480. peer->v_keepalive = peer->bgp->default_keepalive;
  481. peer->v_holdtime = peer->bgp->default_holdtime;
  482. }
  483. peer->update_time = 0;
  484. /* Until we are sure that there is no problem about prefix count
  485. this should be commented out.*/
  486. #if 0
  487. /* Reset prefix count */
  488. peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
  489. peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
  490. peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
  491. peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
  492. peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
  493. #endif /* 0 */
  494. return 0;
  495. }
  496. /* BGP peer is stoped by the error. */
  497. static int
  498. bgp_stop_with_error (struct peer *peer)
  499. {
  500. /* Double start timer. */
  501. peer->v_start *= 2;
  502. /* Overflow check. */
  503. if (peer->v_start >= (60 * 2))
  504. peer->v_start = (60 * 2);
  505. bgp_stop (peer);
  506. return 0;
  507. }
  508. /* something went wrong, send notify and tear down */
  509. static int
  510. bgp_stop_with_notify (struct peer *peer, u_char code, u_char sub_code)
  511. {
  512. /* Send notify to remote peer */
  513. bgp_notify_send (peer, code, sub_code);
  514. /* Sweep if it is temporary peer. */
  515. if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  516. {
  517. zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
  518. peer_delete (peer);
  519. return -1;
  520. }
  521. /* Clear start timer value to default. */
  522. peer->v_start = BGP_INIT_START_TIMER;
  523. /* bgp_stop needs to be invoked while in Established state */
  524. bgp_stop(peer);
  525. return 0;
  526. }
  527. /* TCP connection open. Next we send open message to remote peer. And
  528. add read thread for reading open message. */
  529. static int
  530. bgp_connect_success (struct peer *peer)
  531. {
  532. if (peer->fd < 0)
  533. {
  534. zlog_err ("bgp_connect_success peer's fd is negative value %d",
  535. peer->fd);
  536. return -1;
  537. }
  538. BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
  539. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  540. bgp_getsockname (peer);
  541. if (BGP_DEBUG (normal, NORMAL))
  542. {
  543. char buf1[SU_ADDRSTRLEN];
  544. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  545. zlog_debug ("%s open active, local address %s", peer->host,
  546. sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
  547. else
  548. zlog_debug ("%s passive open", peer->host);
  549. }
  550. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  551. bgp_open_send (peer);
  552. return 0;
  553. }
  554. /* TCP connect fail */
  555. static int
  556. bgp_connect_fail (struct peer *peer)
  557. {
  558. bgp_stop (peer);
  559. return 0;
  560. }
  561. /* This function is the first starting point of all BGP connection. It
  562. try to connect to remote peer with non-blocking IO. */
  563. int
  564. bgp_start (struct peer *peer)
  565. {
  566. int status;
  567. if (BGP_PEER_START_SUPPRESSED (peer))
  568. {
  569. if (BGP_DEBUG (fsm, FSM))
  570. plog_err (peer->log, "%s [FSM] Trying to start suppressed peer"
  571. " - this is never supposed to happen!", peer->host);
  572. return -1;
  573. }
  574. /* Scrub some information that might be left over from a previous,
  575. * session
  576. */
  577. /* Connection information. */
  578. if (peer->su_local)
  579. {
  580. sockunion_free (peer->su_local);
  581. peer->su_local = NULL;
  582. }
  583. if (peer->su_remote)
  584. {
  585. sockunion_free (peer->su_remote);
  586. peer->su_remote = NULL;
  587. }
  588. /* Clear remote router-id. */
  589. peer->remote_id.s_addr = 0;
  590. /* Clear peer capability flag. */
  591. peer->cap = 0;
  592. /* If the peer is passive mode, force to move to Active mode. */
  593. if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
  594. {
  595. BGP_EVENT_ADD (peer, TCP_connection_open_failed);
  596. return 0;
  597. }
  598. status = bgp_connect (peer);
  599. switch (status)
  600. {
  601. case connect_error:
  602. if (BGP_DEBUG (fsm, FSM))
  603. plog_debug (peer->log, "%s [FSM] Connect error", peer->host);
  604. BGP_EVENT_ADD (peer, TCP_connection_open_failed);
  605. break;
  606. case connect_success:
  607. if (BGP_DEBUG (fsm, FSM))
  608. plog_debug (peer->log, "%s [FSM] Connect immediately success",
  609. peer->host);
  610. BGP_EVENT_ADD (peer, TCP_connection_open);
  611. break;
  612. case connect_in_progress:
  613. /* To check nonblocking connect, we wait until socket is
  614. readable or writable. */
  615. if (BGP_DEBUG (fsm, FSM))
  616. plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result",
  617. peer->host);
  618. if (peer->fd < 0)
  619. {
  620. zlog_err ("bgp_start peer's fd is negative value %d",
  621. peer->fd);
  622. return -1;
  623. }
  624. BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
  625. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  626. break;
  627. }
  628. return 0;
  629. }
  630. /* Connect retry timer is expired when the peer status is Connect. */
  631. static int
  632. bgp_reconnect (struct peer *peer)
  633. {
  634. bgp_stop (peer);
  635. bgp_start (peer);
  636. return 0;
  637. }
  638. static int
  639. bgp_fsm_open (struct peer *peer)
  640. {
  641. /* Send keepalive and make keepalive timer */
  642. bgp_keepalive_send (peer);
  643. /* Reset holdtimer value. */
  644. BGP_TIMER_OFF (peer->t_holdtime);
  645. return 0;
  646. }
  647. /* Keepalive send to peer. */
  648. static int
  649. bgp_fsm_keepalive_expire (struct peer *peer)
  650. {
  651. bgp_keepalive_send (peer);
  652. return 0;
  653. }
  654. /* FSM error, unexpected event. This is error of BGP connection. So cut the
  655. peer and change to Idle status. */
  656. static int
  657. bgp_fsm_event_error (struct peer *peer)
  658. {
  659. plog_err (peer->log, "%s [FSM] unexpected packet received in state %s",
  660. peer->host, LOOKUP (bgp_status_msg, peer->status));
  661. return bgp_stop_with_notify (peer, BGP_NOTIFY_FSM_ERR, 0);
  662. }
  663. /* Hold timer expire. This is error of BGP connection. So cut the
  664. peer and change to Idle status. */
  665. static int
  666. bgp_fsm_holdtime_expire (struct peer *peer)
  667. {
  668. if (BGP_DEBUG (fsm, FSM))
  669. plog_debug (peer->log, "%s [FSM] Hold timer expire", peer->host);
  670. return bgp_stop_with_notify (peer, BGP_NOTIFY_HOLD_ERR, 0);
  671. }
  672. /* Status goes to Established. Send keepalive packet then make first
  673. update information. */
  674. static int
  675. bgp_establish (struct peer *peer)
  676. {
  677. struct bgp_notify *notify;
  678. afi_t afi;
  679. safi_t safi;
  680. int nsf_af_count = 0;
  681. /* Reset capability open status flag. */
  682. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
  683. SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
  684. /* Clear last notification data. */
  685. notify = &peer->notify;
  686. if (notify->data)
  687. XFREE (MTYPE_TMP, notify->data);
  688. memset (notify, 0, sizeof (struct bgp_notify));
  689. /* Clear start timer value to default. */
  690. peer->v_start = BGP_INIT_START_TIMER;
  691. /* Increment established count. */
  692. peer->established++;
  693. bgp_fsm_change_status (peer, Established);
  694. /* bgp log-neighbor-changes of neighbor Up */
  695. if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
  696. zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
  697. /* graceful restart */
  698. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  699. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  700. for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
  701. {
  702. if (peer->afc_nego[afi][safi]
  703. && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
  704. && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
  705. {
  706. if (peer->nsf[afi][safi]
  707. && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
  708. bgp_clear_stale_route (peer, afi, safi);
  709. peer->nsf[afi][safi] = 1;
  710. nsf_af_count++;
  711. }
  712. else
  713. {
  714. if (peer->nsf[afi][safi])
  715. bgp_clear_stale_route (peer, afi, safi);
  716. peer->nsf[afi][safi] = 0;
  717. }
  718. }
  719. if (nsf_af_count)
  720. SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  721. else
  722. {
  723. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  724. if (peer->t_gr_stale)
  725. {
  726. BGP_TIMER_OFF (peer->t_gr_stale);
  727. if (BGP_DEBUG (events, EVENTS))
  728. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  729. }
  730. }
  731. if (peer->t_gr_restart)
  732. {
  733. BGP_TIMER_OFF (peer->t_gr_restart);
  734. if (BGP_DEBUG (events, EVENTS))
  735. zlog_debug ("%s graceful restart timer stopped", peer->host);
  736. }
  737. #ifdef HAVE_SNMP
  738. bgpTrapEstablished (peer);
  739. #endif /* HAVE_SNMP */
  740. /* Reset uptime, send keepalive, send current table. */
  741. peer->uptime = bgp_clock ();
  742. /* Send route-refresh when ORF is enabled */
  743. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  744. for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
  745. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
  746. {
  747. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
  748. bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
  749. REFRESH_IMMEDIATE, 0);
  750. else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
  751. bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
  752. REFRESH_IMMEDIATE, 0);
  753. }
  754. if (peer->v_keepalive)
  755. bgp_keepalive_send (peer);
  756. /* First update is deferred until ORF or ROUTE-REFRESH is received */
  757. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  758. for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
  759. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
  760. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
  761. || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
  762. SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
  763. bgp_announce_route_all (peer);
  764. BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1);
  765. return 0;
  766. }
  767. /* Keepalive packet is received. */
  768. static int
  769. bgp_fsm_keepalive (struct peer *peer)
  770. {
  771. /* peer count update */
  772. peer->keepalive_in++;
  773. BGP_TIMER_OFF (peer->t_holdtime);
  774. return 0;
  775. }
  776. /* Update packet is received. */
  777. static int
  778. bgp_fsm_update (struct peer *peer)
  779. {
  780. BGP_TIMER_OFF (peer->t_holdtime);
  781. return 0;
  782. }
  783. /* This is empty event. */
  784. static int
  785. bgp_ignore (struct peer *peer)
  786. {
  787. if (BGP_DEBUG (fsm, FSM))
  788. zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
  789. return 0;
  790. }
  791. /* Finite State Machine structure */
  792. static const struct {
  793. int (*func) (struct peer *);
  794. int next_state;
  795. } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
  796. {
  797. {
  798. /* Idle state: In Idle state, all events other than BGP_Start is
  799. ignored. With BGP_Start event, finite state machine calls
  800. bgp_start(). */
  801. {bgp_start, Connect}, /* BGP_Start */
  802. {bgp_stop, Idle}, /* BGP_Stop */
  803. {bgp_stop, Idle}, /* TCP_connection_open */
  804. {bgp_stop, Idle}, /* TCP_connection_closed */
  805. {bgp_ignore, Idle}, /* TCP_connection_open_failed */
  806. {bgp_stop, Idle}, /* TCP_fatal_error */
  807. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  808. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  809. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  810. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  811. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  812. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  813. {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
  814. {bgp_ignore, Idle}, /* Clearing_Completed */
  815. },
  816. {
  817. /* Connect */
  818. {bgp_ignore, Connect}, /* BGP_Start */
  819. {bgp_stop, Idle}, /* BGP_Stop */
  820. {bgp_connect_success, OpenSent}, /* TCP_connection_open */
  821. {bgp_stop, Idle}, /* TCP_connection_closed */
  822. {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
  823. {bgp_connect_fail, Idle}, /* TCP_fatal_error */
  824. {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
  825. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  826. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  827. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  828. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  829. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  830. {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
  831. {bgp_ignore, Idle}, /* Clearing_Completed */
  832. },
  833. {
  834. /* Active, */
  835. {bgp_ignore, Active}, /* BGP_Start */
  836. {bgp_stop, Idle}, /* BGP_Stop */
  837. {bgp_connect_success, OpenSent}, /* TCP_connection_open */
  838. {bgp_stop, Idle}, /* TCP_connection_closed */
  839. {bgp_ignore, Active}, /* TCP_connection_open_failed */
  840. {bgp_ignore, Idle}, /* TCP_fatal_error */
  841. {bgp_start, Connect}, /* ConnectRetry_timer_expired */
  842. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  843. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  844. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  845. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  846. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  847. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  848. {bgp_ignore, Idle}, /* Clearing_Completed */
  849. },
  850. {
  851. /* OpenSent, */
  852. {bgp_ignore, OpenSent}, /* BGP_Start */
  853. {bgp_stop, Idle}, /* BGP_Stop */
  854. {bgp_stop, Active}, /* TCP_connection_open */
  855. {bgp_stop, Active}, /* TCP_connection_closed */
  856. {bgp_stop, Active}, /* TCP_connection_open_failed */
  857. {bgp_stop, Active}, /* TCP_fatal_error */
  858. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  859. {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
  860. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  861. {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
  862. {bgp_fsm_event_error, Idle}, /* Receive_KEEPALIVE_message */
  863. {bgp_fsm_event_error, Idle}, /* Receive_UPDATE_message */
  864. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  865. {bgp_ignore, Idle}, /* Clearing_Completed */
  866. },
  867. {
  868. /* OpenConfirm, */
  869. {bgp_ignore, OpenConfirm}, /* BGP_Start */
  870. {bgp_stop, Idle}, /* BGP_Stop */
  871. {bgp_stop, Idle}, /* TCP_connection_open */
  872. {bgp_stop, Idle}, /* TCP_connection_closed */
  873. {bgp_stop, Idle}, /* TCP_connection_open_failed */
  874. {bgp_stop, Idle}, /* TCP_fatal_error */
  875. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  876. {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
  877. {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
  878. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  879. {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
  880. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  881. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  882. {bgp_ignore, Idle}, /* Clearing_Completed */
  883. },
  884. {
  885. /* Established, */
  886. {bgp_ignore, Established}, /* BGP_Start */
  887. {bgp_stop, Clearing}, /* BGP_Stop */
  888. {bgp_stop, Clearing}, /* TCP_connection_open */
  889. {bgp_stop, Clearing}, /* TCP_connection_closed */
  890. {bgp_stop, Clearing}, /* TCP_connection_open_failed */
  891. {bgp_stop, Clearing}, /* TCP_fatal_error */
  892. {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
  893. {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */
  894. {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
  895. {bgp_stop, Clearing}, /* Receive_OPEN_message */
  896. {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
  897. {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
  898. {bgp_stop_with_error, Clearing}, /* Receive_NOTIFICATION_message */
  899. {bgp_ignore, Idle}, /* Clearing_Completed */
  900. },
  901. {
  902. /* Clearing, */
  903. {bgp_ignore, Clearing}, /* BGP_Start */
  904. {bgp_stop, Clearing}, /* BGP_Stop */
  905. {bgp_stop, Clearing}, /* TCP_connection_open */
  906. {bgp_stop, Clearing}, /* TCP_connection_closed */
  907. {bgp_stop, Clearing}, /* TCP_connection_open_failed */
  908. {bgp_stop, Clearing}, /* TCP_fatal_error */
  909. {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
  910. {bgp_stop, Clearing}, /* Hold_Timer_expired */
  911. {bgp_stop, Clearing}, /* KeepAlive_timer_expired */
  912. {bgp_stop, Clearing}, /* Receive_OPEN_message */
  913. {bgp_stop, Clearing}, /* Receive_KEEPALIVE_message */
  914. {bgp_stop, Clearing}, /* Receive_UPDATE_message */
  915. {bgp_stop, Clearing}, /* Receive_NOTIFICATION_message */
  916. {bgp_clearing_completed, Idle}, /* Clearing_Completed */
  917. },
  918. {
  919. /* Deleted, */
  920. {bgp_ignore, Deleted}, /* BGP_Start */
  921. {bgp_ignore, Deleted}, /* BGP_Stop */
  922. {bgp_ignore, Deleted}, /* TCP_connection_open */
  923. {bgp_ignore, Deleted}, /* TCP_connection_closed */
  924. {bgp_ignore, Deleted}, /* TCP_connection_open_failed */
  925. {bgp_ignore, Deleted}, /* TCP_fatal_error */
  926. {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */
  927. {bgp_ignore, Deleted}, /* Hold_Timer_expired */
  928. {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */
  929. {bgp_ignore, Deleted}, /* Receive_OPEN_message */
  930. {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */
  931. {bgp_ignore, Deleted}, /* Receive_UPDATE_message */
  932. {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */
  933. {bgp_ignore, Deleted}, /* Clearing_Completed */
  934. },
  935. };
  936. static const char *bgp_event_str[] =
  937. {
  938. NULL,
  939. "BGP_Start",
  940. "BGP_Stop",
  941. "TCP_connection_open",
  942. "TCP_connection_closed",
  943. "TCP_connection_open_failed",
  944. "TCP_fatal_error",
  945. "ConnectRetry_timer_expired",
  946. "Hold_Timer_expired",
  947. "KeepAlive_timer_expired",
  948. "Receive_OPEN_message",
  949. "Receive_KEEPALIVE_message",
  950. "Receive_UPDATE_message",
  951. "Receive_NOTIFICATION_message",
  952. "Clearing_Completed",
  953. };
  954. /* Execute event process. */
  955. int
  956. bgp_event (struct thread *thread)
  957. {
  958. int ret = 0;
  959. int event;
  960. int next;
  961. struct peer *peer;
  962. peer = THREAD_ARG (thread);
  963. event = THREAD_VAL (thread);
  964. /* Logging this event. */
  965. next = FSM [peer->status -1][event - 1].next_state;
  966. if (BGP_DEBUG (fsm, FSM) && peer->status != next)
  967. plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
  968. bgp_event_str[event],
  969. LOOKUP (bgp_status_msg, peer->status),
  970. LOOKUP (bgp_status_msg, next));
  971. /* Call function. */
  972. if (FSM [peer->status -1][event - 1].func)
  973. ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
  974. /* When function do not want proceed next job return -1. */
  975. if (ret >= 0)
  976. {
  977. /* If status is changed. */
  978. if (next != peer->status)
  979. bgp_fsm_change_status (peer, next);
  980. /* Make sure timer is set. */
  981. bgp_timer_set (peer);
  982. }
  983. return ret;
  984. }