bgp_fsm.c 33 KB

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