bgp_fsm.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. /* Hook function called after bgp event is occered. And vty's
  59. neighbor command invoke this function after making neighbor
  60. structure. */
  61. void
  62. bgp_timer_set (struct peer *peer)
  63. {
  64. int jitter = 0;
  65. switch (peer->status)
  66. {
  67. case Idle:
  68. /* First entry point of peer's finite state machine. In Idle
  69. status start timer is on unless peer is shutdown or peer is
  70. inactive. All other timer must be turned off */
  71. if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN)
  72. || CHECK_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW)
  73. || ! peer_active (peer))
  74. {
  75. BGP_TIMER_OFF (peer->t_start);
  76. }
  77. else
  78. {
  79. jitter = bgp_start_jitter (peer->v_start);
  80. BGP_TIMER_ON (peer->t_start, bgp_start_timer,
  81. peer->v_start + jitter);
  82. }
  83. BGP_TIMER_OFF (peer->t_connect);
  84. BGP_TIMER_OFF (peer->t_holdtime);
  85. BGP_TIMER_OFF (peer->t_keepalive);
  86. BGP_TIMER_OFF (peer->t_asorig);
  87. BGP_TIMER_OFF (peer->t_routeadv);
  88. break;
  89. case Connect:
  90. /* After start timer is expired, the peer moves to Connnect
  91. status. Make sure start timer is off and connect timer is
  92. on. */
  93. BGP_TIMER_OFF (peer->t_start);
  94. BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
  95. BGP_TIMER_OFF (peer->t_holdtime);
  96. BGP_TIMER_OFF (peer->t_keepalive);
  97. BGP_TIMER_OFF (peer->t_asorig);
  98. BGP_TIMER_OFF (peer->t_routeadv);
  99. break;
  100. case Active:
  101. /* Active is waiting connection from remote peer. And if
  102. connect timer is expired, change status to Connect. */
  103. BGP_TIMER_OFF (peer->t_start);
  104. /* If peer is passive mode, do not set connect timer. */
  105. if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)
  106. || CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
  107. {
  108. BGP_TIMER_OFF (peer->t_connect);
  109. }
  110. else
  111. {
  112. BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
  113. }
  114. BGP_TIMER_OFF (peer->t_holdtime);
  115. BGP_TIMER_OFF (peer->t_keepalive);
  116. BGP_TIMER_OFF (peer->t_asorig);
  117. BGP_TIMER_OFF (peer->t_routeadv);
  118. break;
  119. case OpenSent:
  120. /* OpenSent status. */
  121. BGP_TIMER_OFF (peer->t_start);
  122. BGP_TIMER_OFF (peer->t_connect);
  123. if (peer->v_holdtime != 0)
  124. {
  125. BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
  126. peer->v_holdtime);
  127. }
  128. else
  129. {
  130. BGP_TIMER_OFF (peer->t_holdtime);
  131. }
  132. BGP_TIMER_OFF (peer->t_keepalive);
  133. BGP_TIMER_OFF (peer->t_asorig);
  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_asorig);
  155. BGP_TIMER_OFF (peer->t_routeadv);
  156. break;
  157. case Established:
  158. /* In Established status start and connect timer is turned
  159. off. */
  160. BGP_TIMER_OFF (peer->t_start);
  161. BGP_TIMER_OFF (peer->t_connect);
  162. /* Same as OpenConfirm, if holdtime is zero then both holdtime
  163. and keepalive must be turned off. */
  164. if (peer->v_holdtime == 0)
  165. {
  166. BGP_TIMER_OFF (peer->t_holdtime);
  167. BGP_TIMER_OFF (peer->t_keepalive);
  168. }
  169. else
  170. {
  171. BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
  172. peer->v_holdtime);
  173. BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
  174. peer->v_keepalive);
  175. }
  176. BGP_TIMER_OFF (peer->t_asorig);
  177. break;
  178. }
  179. }
  180. /* BGP start timer. This function set BGP_Start event to thread value
  181. and process event. */
  182. static int
  183. bgp_start_timer (struct thread *thread)
  184. {
  185. struct peer *peer;
  186. peer = THREAD_ARG (thread);
  187. peer->t_start = NULL;
  188. if (BGP_DEBUG (fsm, FSM))
  189. zlog (peer->log, LOG_DEBUG,
  190. "%s [FSM] Timer (start timer expire).", peer->host);
  191. THREAD_VAL (thread) = BGP_Start;
  192. bgp_event (thread); /* bgp_event unlocks peer */
  193. return 0;
  194. }
  195. /* BGP connect retry timer. */
  196. static int
  197. bgp_connect_timer (struct thread *thread)
  198. {
  199. struct peer *peer;
  200. peer = THREAD_ARG (thread);
  201. peer->t_connect = NULL;
  202. if (BGP_DEBUG (fsm, FSM))
  203. zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
  204. peer->host);
  205. THREAD_VAL (thread) = ConnectRetry_timer_expired;
  206. bgp_event (thread); /* bgp_event unlocks peer */
  207. return 0;
  208. }
  209. /* BGP holdtime timer. */
  210. static int
  211. bgp_holdtime_timer (struct thread *thread)
  212. {
  213. struct peer *peer;
  214. peer = THREAD_ARG (thread);
  215. peer->t_holdtime = NULL;
  216. if (BGP_DEBUG (fsm, FSM))
  217. zlog (peer->log, LOG_DEBUG,
  218. "%s [FSM] Timer (holdtime timer expire)",
  219. peer->host);
  220. THREAD_VAL (thread) = Hold_Timer_expired;
  221. bgp_event (thread); /* bgp_event unlocks peer */
  222. return 0;
  223. }
  224. /* BGP keepalive fire ! */
  225. static int
  226. bgp_keepalive_timer (struct thread *thread)
  227. {
  228. struct peer *peer;
  229. peer = THREAD_ARG (thread);
  230. peer->t_keepalive = NULL;
  231. if (BGP_DEBUG (fsm, FSM))
  232. zlog (peer->log, LOG_DEBUG,
  233. "%s [FSM] Timer (keepalive timer expire)",
  234. peer->host);
  235. THREAD_VAL (thread) = KeepAlive_timer_expired;
  236. bgp_event (thread); /* bgp_event unlocks peer */
  237. return 0;
  238. }
  239. static int
  240. bgp_routeadv_timer (struct thread *thread)
  241. {
  242. struct peer *peer;
  243. peer = THREAD_ARG (thread);
  244. peer->t_routeadv = NULL;
  245. if (BGP_DEBUG (fsm, FSM))
  246. zlog (peer->log, LOG_DEBUG,
  247. "%s [FSM] Timer (routeadv timer expire)",
  248. peer->host);
  249. peer->synctime = time (NULL);
  250. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  251. BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer,
  252. peer->v_routeadv);
  253. return 0;
  254. }
  255. /* Reset bgp update timer */
  256. static void
  257. bgp_uptime_reset (struct peer *peer)
  258. {
  259. peer->uptime = time (NULL);
  260. }
  261. /* BGP Peer Down Cause */
  262. const char *peer_down_str[] =
  263. {
  264. "",
  265. "Router ID changed",
  266. "Remote AS changed",
  267. "Local AS change",
  268. "Cluster ID changed",
  269. "Confederation identifier changed",
  270. "Confederation peer changed",
  271. "RR client config change",
  272. "RS client config change",
  273. "Update source change",
  274. "Address family activated",
  275. "Admin. shutdown",
  276. "User reset",
  277. "BGP Notification received",
  278. "BGP Notification send",
  279. "Peer closed the session",
  280. "Neighbor deleted",
  281. "Peer-group add member",
  282. "Peer-group delete member",
  283. "Capability changed",
  284. "Passive config change",
  285. "Multihop config change",
  286. "NSF peer closed the session"
  287. };
  288. static int
  289. bgp_graceful_restart_timer_expire (struct thread *thread)
  290. {
  291. struct peer *peer;
  292. afi_t afi;
  293. safi_t safi;
  294. peer = THREAD_ARG (thread);
  295. peer->t_gr_restart = NULL;
  296. /* NSF delete stale route */
  297. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  298. for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
  299. if (peer->nsf[afi][safi])
  300. bgp_clear_stale_route (peer, afi, safi);
  301. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  302. BGP_TIMER_OFF (peer->t_gr_stale);
  303. if (BGP_DEBUG (events, EVENTS))
  304. {
  305. zlog_debug ("%s graceful restart timer expired", peer->host);
  306. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  307. }
  308. bgp_timer_set (peer);
  309. return 0;
  310. }
  311. static int
  312. bgp_graceful_stale_timer_expire (struct thread *thread)
  313. {
  314. struct peer *peer;
  315. afi_t afi;
  316. safi_t safi;
  317. peer = THREAD_ARG (thread);
  318. peer->t_gr_stale = NULL;
  319. if (BGP_DEBUG (events, EVENTS))
  320. zlog_debug ("%s graceful restart stalepath timer expired", peer->host);
  321. /* NSF delete stale route */
  322. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  323. for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
  324. if (peer->nsf[afi][safi])
  325. bgp_clear_stale_route (peer, afi, safi);
  326. return 0;
  327. }
  328. /* Called after event occured, this function change status and reset
  329. read/write and timer thread. */
  330. void
  331. bgp_fsm_change_status (struct peer *peer, int status)
  332. {
  333. bgp_dump_state (peer, peer->status, status);
  334. /* Preserve old status and change into new status. */
  335. peer->ostatus = peer->status;
  336. peer->status = status;
  337. if (BGP_DEBUG (normal, NORMAL))
  338. zlog_debug ("%s went from %s to %s",
  339. peer->host,
  340. LOOKUP (bgp_status_msg, peer->ostatus),
  341. LOOKUP (bgp_status_msg, peer->status));
  342. }
  343. /* Administrative BGP peer stop event. */
  344. int
  345. bgp_stop (struct peer *peer)
  346. {
  347. afi_t afi;
  348. safi_t safi;
  349. unsigned int i;
  350. char orf_name[BUFSIZ];
  351. /* Increment Dropped count. */
  352. if (peer->status == Established)
  353. {
  354. peer->dropped++;
  355. bgp_fsm_change_status (peer, Idle);
  356. /* bgp log-neighbor-changes of neighbor Down */
  357. if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
  358. zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
  359. peer_down_str [(int) peer->last_reset]);
  360. /* graceful restart */
  361. if (peer->t_gr_stale)
  362. {
  363. BGP_TIMER_OFF (peer->t_gr_stale);
  364. if (BGP_DEBUG (events, EVENTS))
  365. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  366. }
  367. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
  368. {
  369. if (BGP_DEBUG (events, EVENTS))
  370. {
  371. zlog_debug ("%s graceful restart timer started for %d sec",
  372. peer->host, peer->v_gr_restart);
  373. zlog_debug ("%s graceful restart stalepath timer started for %d sec",
  374. peer->host, peer->bgp->stalepath_time);
  375. }
  376. BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
  377. peer->v_gr_restart);
  378. BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
  379. peer->bgp->stalepath_time);
  380. }
  381. else
  382. {
  383. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  384. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  385. for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
  386. peer->nsf[afi][safi] = 0;
  387. }
  388. /* set last reset time */
  389. peer->resettime = time (NULL);
  390. /* Reset uptime. */
  391. bgp_uptime_reset (peer);
  392. #ifdef HAVE_SNMP
  393. bgpTrapBackwardTransition (peer);
  394. #endif /* HAVE_SNMP */
  395. /* Reset uptime. */
  396. bgp_uptime_reset (peer);
  397. /* Need of clear of peer. */
  398. bgp_clear_route_all (peer);
  399. /* Reset peer synctime */
  400. peer->synctime = 0;
  401. }
  402. /* Stop read and write threads when exists. */
  403. BGP_READ_OFF (peer->t_read);
  404. BGP_WRITE_OFF (peer->t_write);
  405. /* Stop all timers. */
  406. BGP_TIMER_OFF (peer->t_start);
  407. BGP_TIMER_OFF (peer->t_connect);
  408. BGP_TIMER_OFF (peer->t_holdtime);
  409. BGP_TIMER_OFF (peer->t_keepalive);
  410. BGP_TIMER_OFF (peer->t_asorig);
  411. BGP_TIMER_OFF (peer->t_routeadv);
  412. /* Delete all existing events of the peer,
  413. and corresponding peer ref-count */
  414. for (i = thread_cancel_event (master, peer); i > 0; i--)
  415. peer_unlock (peer); /* thread event reference */
  416. /* Stream reset. */
  417. peer->packet_size = 0;
  418. /* Clear input and output buffer. */
  419. if (peer->ibuf)
  420. stream_reset (peer->ibuf);
  421. if (peer->work)
  422. stream_reset (peer->work);
  423. if (peer->obuf)
  424. stream_fifo_clean (peer->obuf);
  425. /* Close of file descriptor. */
  426. if (peer->fd >= 0)
  427. {
  428. close (peer->fd);
  429. peer->fd = -1;
  430. }
  431. /* Connection information. */
  432. if (peer->su_local)
  433. {
  434. sockunion_free (peer->su_local);
  435. peer->su_local = NULL;
  436. }
  437. if (peer->su_remote)
  438. {
  439. sockunion_free (peer->su_remote);
  440. peer->su_remote = NULL;
  441. }
  442. /* Clear remote router-id. */
  443. peer->remote_id.s_addr = 0;
  444. /* Clear peer capability flag. */
  445. peer->cap = 0;
  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. /* TCP connection open. Next we send open message to remote peer. And
  500. add read thread for reading open message. */
  501. static int
  502. bgp_connect_success (struct peer *peer)
  503. {
  504. char buf1[BUFSIZ];
  505. if (peer->fd < 0)
  506. {
  507. zlog_err ("bgp_connect_success peer's fd is negative value %d",
  508. peer->fd);
  509. return -1;
  510. }
  511. BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
  512. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  513. bgp_getsockname (peer);
  514. if (BGP_DEBUG (normal, NORMAL))
  515. {
  516. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  517. zlog_debug ("%s open active, local address %s", peer->host,
  518. sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
  519. else
  520. zlog_debug ("%s passive open", peer->host);
  521. }
  522. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  523. bgp_open_send (peer);
  524. return 0;
  525. }
  526. /* TCP connect fail */
  527. static int
  528. bgp_connect_fail (struct peer *peer)
  529. {
  530. bgp_stop (peer);
  531. return 0;
  532. }
  533. /* This function is the first starting point of all BGP connection. It
  534. try to connect to remote peer with non-blocking IO. */
  535. int
  536. bgp_start (struct peer *peer)
  537. {
  538. int status;
  539. /* If the peer is passive mode, force to move to Active mode. */
  540. if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
  541. {
  542. BGP_EVENT_ADD (peer, TCP_connection_open_failed);
  543. return 0;
  544. }
  545. status = bgp_connect (peer);
  546. switch (status)
  547. {
  548. case connect_error:
  549. if (BGP_DEBUG (fsm, FSM))
  550. plog_debug (peer->log, "%s [FSM] Connect error", peer->host);
  551. BGP_EVENT_ADD (peer, TCP_connection_open_failed);
  552. break;
  553. case connect_success:
  554. if (BGP_DEBUG (fsm, FSM))
  555. plog_debug (peer->log, "%s [FSM] Connect immediately success",
  556. peer->host);
  557. BGP_EVENT_ADD (peer, TCP_connection_open);
  558. break;
  559. case connect_in_progress:
  560. /* To check nonblocking connect, we wait until socket is
  561. readable or writable. */
  562. if (BGP_DEBUG (fsm, FSM))
  563. plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result",
  564. peer->host);
  565. if (peer->fd < 0)
  566. {
  567. zlog_err ("bgp_start peer's fd is negative value %d",
  568. peer->fd);
  569. return -1;
  570. }
  571. BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
  572. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  573. break;
  574. }
  575. return 0;
  576. }
  577. /* Connect retry timer is expired when the peer status is Connect. */
  578. static int
  579. bgp_reconnect (struct peer *peer)
  580. {
  581. bgp_stop (peer);
  582. bgp_start (peer);
  583. return 0;
  584. }
  585. static int
  586. bgp_fsm_open (struct peer *peer)
  587. {
  588. /* Send keepalive and make keepalive timer */
  589. bgp_keepalive_send (peer);
  590. /* Reset holdtimer value. */
  591. BGP_TIMER_OFF (peer->t_holdtime);
  592. return 0;
  593. }
  594. /* Keepalive send to peer. */
  595. static int
  596. bgp_fsm_keepalive_expire (struct peer *peer)
  597. {
  598. bgp_keepalive_send (peer);
  599. return 0;
  600. }
  601. /* Hold timer expire. This is error of BGP connection. So cut the
  602. peer and change to Idle status. */
  603. static int
  604. bgp_fsm_holdtime_expire (struct peer *peer)
  605. {
  606. if (BGP_DEBUG (fsm, FSM))
  607. zlog (peer->log, LOG_DEBUG, "%s [FSM] Hold timer expire", peer->host);
  608. /* Send notify to remote peer. */
  609. bgp_notify_send (peer, BGP_NOTIFY_HOLD_ERR, 0);
  610. /* Sweep if it is temporary peer. */
  611. if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  612. {
  613. zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
  614. peer_delete (peer);
  615. return -1;
  616. }
  617. return 0;
  618. }
  619. /* Status goes to Established. Send keepalive packet then make first
  620. update information. */
  621. static int
  622. bgp_establish (struct peer *peer)
  623. {
  624. struct bgp_notify *notify;
  625. afi_t afi;
  626. safi_t safi;
  627. int nsf_af_count = 0;
  628. /* Reset capability open status flag. */
  629. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
  630. SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
  631. /* Clear last notification data. */
  632. notify = &peer->notify;
  633. if (notify->data)
  634. XFREE (MTYPE_TMP, notify->data);
  635. memset (notify, 0, sizeof (struct bgp_notify));
  636. /* Clear start timer value to default. */
  637. peer->v_start = BGP_INIT_START_TIMER;
  638. /* Increment established count. */
  639. peer->established++;
  640. bgp_fsm_change_status (peer, Established);
  641. /* bgp log-neighbor-changes of neighbor Up */
  642. if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
  643. zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
  644. /* graceful restart */
  645. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  646. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  647. for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
  648. {
  649. if (peer->afc_nego[afi][safi]
  650. && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
  651. && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
  652. {
  653. if (peer->nsf[afi][safi]
  654. && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
  655. bgp_clear_stale_route (peer, afi, safi);
  656. peer->nsf[afi][safi] = 1;
  657. nsf_af_count++;
  658. }
  659. else
  660. {
  661. if (peer->nsf[afi][safi])
  662. bgp_clear_stale_route (peer, afi, safi);
  663. peer->nsf[afi][safi] = 0;
  664. }
  665. }
  666. if (nsf_af_count)
  667. SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  668. else
  669. {
  670. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  671. if (peer->t_gr_stale)
  672. {
  673. BGP_TIMER_OFF (peer->t_gr_stale);
  674. if (BGP_DEBUG (events, EVENTS))
  675. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  676. }
  677. }
  678. if (peer->t_gr_restart)
  679. {
  680. BGP_TIMER_OFF (peer->t_gr_restart);
  681. if (BGP_DEBUG (events, EVENTS))
  682. zlog_debug ("%s graceful restart timer stopped", peer->host);
  683. }
  684. #ifdef HAVE_SNMP
  685. bgpTrapEstablished (peer);
  686. #endif /* HAVE_SNMP */
  687. /* Reset uptime, send keepalive, send current table. */
  688. bgp_uptime_reset (peer);
  689. /* Send route-refresh when ORF is enabled */
  690. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  691. for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
  692. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
  693. {
  694. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
  695. bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
  696. REFRESH_IMMEDIATE, 0);
  697. else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
  698. bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
  699. REFRESH_IMMEDIATE, 0);
  700. }
  701. if (peer->v_keepalive)
  702. bgp_keepalive_send (peer);
  703. /* First update is deferred until ORF or ROUTE-REFRESH is received */
  704. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  705. for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
  706. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
  707. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
  708. || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
  709. SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
  710. bgp_announce_route_all (peer);
  711. BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1);
  712. return 0;
  713. }
  714. /* Keepalive packet is received. */
  715. static int
  716. bgp_fsm_keepalive (struct peer *peer)
  717. {
  718. /* peer count update */
  719. peer->keepalive_in++;
  720. BGP_TIMER_OFF (peer->t_holdtime);
  721. return 0;
  722. }
  723. /* Update packet is received. */
  724. static int
  725. bgp_fsm_update (struct peer *peer)
  726. {
  727. BGP_TIMER_OFF (peer->t_holdtime);
  728. return 0;
  729. }
  730. /* This is empty event. */
  731. static int
  732. bgp_ignore (struct peer *peer)
  733. {
  734. if (BGP_DEBUG (fsm, FSM))
  735. zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
  736. return 0;
  737. }
  738. /* Finite State Machine structure */
  739. struct {
  740. int (*func) ();
  741. int next_state;
  742. } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
  743. {
  744. {
  745. /* Idle state: In Idle state, all events other than BGP_Start is
  746. ignored. With BGP_Start event, finite state machine calls
  747. bgp_start(). */
  748. {bgp_start, Connect}, /* BGP_Start */
  749. {bgp_stop, Idle}, /* BGP_Stop */
  750. {bgp_stop, Idle}, /* TCP_connection_open */
  751. {bgp_stop, Idle}, /* TCP_connection_closed */
  752. {bgp_ignore, Idle}, /* TCP_connection_open_failed */
  753. {bgp_stop, Idle}, /* TCP_fatal_error */
  754. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  755. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  756. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  757. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  758. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  759. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  760. {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
  761. },
  762. {
  763. /* Connect */
  764. {bgp_ignore, Connect}, /* BGP_Start */
  765. {bgp_stop, Idle}, /* BGP_Stop */
  766. {bgp_connect_success, OpenSent}, /* TCP_connection_open */
  767. {bgp_stop, Idle}, /* TCP_connection_closed */
  768. {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
  769. {bgp_connect_fail, Idle}, /* TCP_fatal_error */
  770. {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
  771. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  772. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  773. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  774. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  775. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  776. {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
  777. },
  778. {
  779. /* Active, */
  780. {bgp_ignore, Active}, /* BGP_Start */
  781. {bgp_stop, Idle}, /* BGP_Stop */
  782. {bgp_connect_success, OpenSent}, /* TCP_connection_open */
  783. {bgp_stop, Idle}, /* TCP_connection_closed */
  784. {bgp_ignore, Active}, /* TCP_connection_open_failed */
  785. {bgp_ignore, Idle}, /* TCP_fatal_error */
  786. {bgp_start, Connect}, /* ConnectRetry_timer_expired */
  787. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  788. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  789. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  790. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  791. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  792. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  793. },
  794. {
  795. /* OpenSent, */
  796. {bgp_ignore, OpenSent}, /* BGP_Start */
  797. {bgp_stop, Idle}, /* BGP_Stop */
  798. {bgp_stop, Idle}, /* TCP_connection_open */
  799. {bgp_stop, Active}, /* TCP_connection_closed */
  800. {bgp_ignore, Idle}, /* TCP_connection_open_failed */
  801. {bgp_stop, Idle}, /* TCP_fatal_error */
  802. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  803. {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
  804. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  805. {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
  806. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  807. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  808. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  809. },
  810. {
  811. /* OpenConfirm, */
  812. {bgp_ignore, OpenConfirm}, /* BGP_Start */
  813. {bgp_stop, Idle}, /* BGP_Stop */
  814. {bgp_stop, Idle}, /* TCP_connection_open */
  815. {bgp_stop, Idle}, /* TCP_connection_closed */
  816. {bgp_stop, Idle}, /* TCP_connection_open_failed */
  817. {bgp_stop, Idle}, /* TCP_fatal_error */
  818. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  819. {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
  820. {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
  821. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  822. {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
  823. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  824. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  825. },
  826. {
  827. /* Established, */
  828. {bgp_ignore, Established}, /* BGP_Start */
  829. {bgp_stop, Idle}, /* BGP_Stop */
  830. {bgp_stop, Idle}, /* TCP_connection_open */
  831. {bgp_stop, Idle}, /* TCP_connection_closed */
  832. {bgp_ignore, Idle}, /* TCP_connection_open_failed */
  833. {bgp_stop, Idle}, /* TCP_fatal_error */
  834. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  835. {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
  836. {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
  837. {bgp_stop, Idle}, /* Receive_OPEN_message */
  838. {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
  839. {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
  840. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  841. },
  842. };
  843. static const char *bgp_event_str[] =
  844. {
  845. NULL,
  846. "BGP_Start",
  847. "BGP_Stop",
  848. "TCP_connection_open",
  849. "TCP_connection_closed",
  850. "TCP_connection_open_failed",
  851. "TCP_fatal_error",
  852. "ConnectRetry_timer_expired",
  853. "Hold_Timer_expired",
  854. "KeepAlive_timer_expired",
  855. "Receive_OPEN_message",
  856. "Receive_KEEPALIVE_message",
  857. "Receive_UPDATE_message",
  858. "Receive_NOTIFICATION_message"
  859. };
  860. /* Execute event process. */
  861. int
  862. bgp_event (struct thread *thread)
  863. {
  864. int ret;
  865. int event;
  866. int next;
  867. struct peer *peer;
  868. peer = THREAD_ARG (thread);
  869. event = THREAD_VAL (thread);
  870. /* Logging this event. */
  871. next = FSM [peer->status -1][event - 1].next_state;
  872. if (BGP_DEBUG (fsm, FSM))
  873. plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
  874. bgp_event_str[event],
  875. LOOKUP (bgp_status_msg, peer->status),
  876. LOOKUP (bgp_status_msg, next));
  877. /* Call function. */
  878. ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
  879. /* When function do not want proceed next job return -1. */
  880. if (ret >= 0)
  881. {
  882. /* If status is changed. */
  883. if (next != peer->status)
  884. bgp_fsm_change_status (peer, next);
  885. /* Make sure timer is set. */
  886. bgp_timer_set (peer);
  887. }
  888. peer_unlock (peer); /* bgp-event peer reference */
  889. return ret;
  890. }