bgp_fsm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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 = time (NULL);
  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. /* Reset bgp update timer */
  269. static void
  270. bgp_uptime_reset (struct peer *peer)
  271. {
  272. peer->uptime = time (NULL);
  273. }
  274. /* BGP Peer Down Cause */
  275. const char *peer_down_str[] =
  276. {
  277. "",
  278. "Router ID changed",
  279. "Remote AS changed",
  280. "Local AS change",
  281. "Cluster ID changed",
  282. "Confederation identifier changed",
  283. "Confederation peer changed",
  284. "RR client config change",
  285. "RS client config change",
  286. "Update source change",
  287. "Address family activated",
  288. "Admin. shutdown",
  289. "User reset",
  290. "BGP Notification received",
  291. "BGP Notification send",
  292. "Peer closed the session",
  293. "Neighbor deleted",
  294. "Peer-group add member",
  295. "Peer-group delete member",
  296. "Capability changed",
  297. "Passive config change",
  298. "Multihop config change",
  299. "NSF peer closed the session"
  300. };
  301. static int
  302. bgp_graceful_restart_timer_expire (struct thread *thread)
  303. {
  304. struct peer *peer;
  305. afi_t afi;
  306. safi_t safi;
  307. peer = THREAD_ARG (thread);
  308. peer->t_gr_restart = NULL;
  309. /* NSF delete stale route */
  310. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  311. for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
  312. if (peer->nsf[afi][safi])
  313. bgp_clear_stale_route (peer, afi, safi);
  314. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  315. BGP_TIMER_OFF (peer->t_gr_stale);
  316. if (BGP_DEBUG (events, EVENTS))
  317. {
  318. zlog_debug ("%s graceful restart timer expired", peer->host);
  319. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  320. }
  321. bgp_timer_set (peer);
  322. return 0;
  323. }
  324. static int
  325. bgp_graceful_stale_timer_expire (struct thread *thread)
  326. {
  327. struct peer *peer;
  328. afi_t afi;
  329. safi_t safi;
  330. peer = THREAD_ARG (thread);
  331. peer->t_gr_stale = NULL;
  332. if (BGP_DEBUG (events, EVENTS))
  333. zlog_debug ("%s graceful restart stalepath timer expired", peer->host);
  334. /* NSF delete stale route */
  335. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  336. for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
  337. if (peer->nsf[afi][safi])
  338. bgp_clear_stale_route (peer, afi, safi);
  339. return 0;
  340. }
  341. /* Called after event occured, this function change status and reset
  342. read/write and timer thread. */
  343. void
  344. bgp_fsm_change_status (struct peer *peer, int status)
  345. {
  346. bgp_dump_state (peer, peer->status, status);
  347. /* Transition into Clearing or Deleted must /always/ clear all routes..
  348. * (and must do so before actually changing into Deleted..
  349. */
  350. if (status >= Clearing)
  351. bgp_clear_route_all (peer);
  352. /* Preserve old status and change into new status. */
  353. peer->ostatus = peer->status;
  354. peer->status = status;
  355. if (BGP_DEBUG (normal, NORMAL))
  356. zlog_debug ("%s went from %s to %s",
  357. peer->host,
  358. LOOKUP (bgp_status_msg, peer->ostatus),
  359. LOOKUP (bgp_status_msg, peer->status));
  360. }
  361. /* Administrative BGP peer stop event. */
  362. int
  363. bgp_stop (struct peer *peer)
  364. {
  365. afi_t afi;
  366. safi_t safi;
  367. char orf_name[BUFSIZ];
  368. /* Delete all existing events of the peer */
  369. BGP_EVENT_FLUSH (peer);
  370. /* Increment Dropped count. */
  371. if (peer->status == Established)
  372. {
  373. peer->dropped++;
  374. /* bgp log-neighbor-changes of neighbor Down */
  375. if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
  376. zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
  377. peer_down_str [(int) peer->last_reset]);
  378. /* graceful restart */
  379. if (peer->t_gr_stale)
  380. {
  381. BGP_TIMER_OFF (peer->t_gr_stale);
  382. if (BGP_DEBUG (events, EVENTS))
  383. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  384. }
  385. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
  386. {
  387. if (BGP_DEBUG (events, EVENTS))
  388. {
  389. zlog_debug ("%s graceful restart timer started for %d sec",
  390. peer->host, peer->v_gr_restart);
  391. zlog_debug ("%s graceful restart stalepath timer started for %d sec",
  392. peer->host, peer->bgp->stalepath_time);
  393. }
  394. BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
  395. peer->v_gr_restart);
  396. BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
  397. peer->bgp->stalepath_time);
  398. }
  399. else
  400. {
  401. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  402. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  403. for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
  404. peer->nsf[afi][safi] = 0;
  405. }
  406. /* set last reset time */
  407. peer->resettime = time (NULL);
  408. /* Reset uptime. */
  409. bgp_uptime_reset (peer);
  410. #ifdef HAVE_SNMP
  411. bgpTrapBackwardTransition (peer);
  412. #endif /* HAVE_SNMP */
  413. /* Reset uptime. */
  414. bgp_uptime_reset (peer);
  415. /* Reset peer synctime */
  416. peer->synctime = 0;
  417. }
  418. /* Stop read and write threads when exists. */
  419. BGP_READ_OFF (peer->t_read);
  420. BGP_WRITE_OFF (peer->t_write);
  421. /* Stop all timers. */
  422. BGP_TIMER_OFF (peer->t_start);
  423. BGP_TIMER_OFF (peer->t_connect);
  424. BGP_TIMER_OFF (peer->t_holdtime);
  425. BGP_TIMER_OFF (peer->t_keepalive);
  426. BGP_TIMER_OFF (peer->t_asorig);
  427. BGP_TIMER_OFF (peer->t_routeadv);
  428. /* Stream reset. */
  429. peer->packet_size = 0;
  430. /* Clear input and output buffer. */
  431. if (peer->ibuf)
  432. stream_reset (peer->ibuf);
  433. if (peer->work)
  434. stream_reset (peer->work);
  435. if (peer->obuf)
  436. stream_fifo_clean (peer->obuf);
  437. /* Close of file descriptor. */
  438. if (peer->fd >= 0)
  439. {
  440. close (peer->fd);
  441. peer->fd = -1;
  442. }
  443. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  444. for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
  445. {
  446. /* Reset all negotiated variables */
  447. peer->afc_nego[afi][safi] = 0;
  448. peer->afc_adv[afi][safi] = 0;
  449. peer->afc_recv[afi][safi] = 0;
  450. /* peer address family capability flags*/
  451. peer->af_cap[afi][safi] = 0;
  452. /* peer address family status flags*/
  453. peer->af_sflags[afi][safi] = 0;
  454. /* Received ORF prefix-filter */
  455. peer->orf_plist[afi][safi] = NULL;
  456. /* ORF received prefix-filter pnt */
  457. sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
  458. prefix_bgp_orf_remove_all (orf_name);
  459. }
  460. /* Reset keepalive and holdtime */
  461. if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
  462. {
  463. peer->v_keepalive = peer->keepalive;
  464. peer->v_holdtime = peer->holdtime;
  465. }
  466. else
  467. {
  468. peer->v_keepalive = peer->bgp->default_keepalive;
  469. peer->v_holdtime = peer->bgp->default_holdtime;
  470. }
  471. peer->update_time = 0;
  472. /* Until we are sure that there is no problem about prefix count
  473. this should be commented out.*/
  474. #if 0
  475. /* Reset prefix count */
  476. peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
  477. peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
  478. peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
  479. peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
  480. peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
  481. #endif /* 0 */
  482. return 0;
  483. }
  484. /* BGP peer is stoped by the error. */
  485. static int
  486. bgp_stop_with_error (struct peer *peer)
  487. {
  488. /* Double start timer. */
  489. peer->v_start *= 2;
  490. /* Overflow check. */
  491. if (peer->v_start >= (60 * 2))
  492. peer->v_start = (60 * 2);
  493. bgp_stop (peer);
  494. return 0;
  495. }
  496. /* TCP connection open. Next we send open message to remote peer. And
  497. add read thread for reading open message. */
  498. static int
  499. bgp_connect_success (struct peer *peer)
  500. {
  501. char buf1[BUFSIZ];
  502. if (peer->fd < 0)
  503. {
  504. zlog_err ("bgp_connect_success peer's fd is negative value %d",
  505. peer->fd);
  506. return -1;
  507. }
  508. BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
  509. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  510. bgp_getsockname (peer);
  511. if (BGP_DEBUG (normal, NORMAL))
  512. {
  513. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  514. zlog_debug ("%s open active, local address %s", peer->host,
  515. sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
  516. else
  517. zlog_debug ("%s passive open", peer->host);
  518. }
  519. if (!CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)
  520. || bgp_option_check (BGP_OPT_ALWAYS_OPEN))
  521. bgp_open_send (peer);
  522. return 0;
  523. }
  524. /* TCP connect fail */
  525. static int
  526. bgp_connect_fail (struct peer *peer)
  527. {
  528. bgp_stop (peer);
  529. return 0;
  530. }
  531. /* This function is the first starting point of all BGP connection. It
  532. try to connect to remote peer with non-blocking IO. */
  533. int
  534. bgp_start (struct peer *peer)
  535. {
  536. int status;
  537. if (BGP_PEER_START_SUPPRESSED (peer))
  538. {
  539. if (BGP_DEBUG (fsm, FSM))
  540. plog_err (peer->log, "%s [FSM] Trying to start suppressed peer"
  541. " - this is never supposed to happen!", peer->host);
  542. return -1;
  543. }
  544. /* Scrub some information that might be left over from a previous,
  545. * session
  546. */
  547. /* Connection information. */
  548. if (peer->su_local)
  549. {
  550. sockunion_free (peer->su_local);
  551. peer->su_local = NULL;
  552. }
  553. if (peer->su_remote)
  554. {
  555. sockunion_free (peer->su_remote);
  556. peer->su_remote = NULL;
  557. }
  558. /* Clear remote router-id. */
  559. peer->remote_id.s_addr = 0;
  560. /* Clear peer capability flag. */
  561. peer->cap = 0;
  562. /* If the peer is passive mode, force to move to Active mode. */
  563. if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
  564. {
  565. BGP_EVENT_ADD (peer, TCP_connection_open_failed);
  566. return 0;
  567. }
  568. status = bgp_connect (peer);
  569. switch (status)
  570. {
  571. case connect_error:
  572. if (BGP_DEBUG (fsm, FSM))
  573. plog_debug (peer->log, "%s [FSM] Connect error", peer->host);
  574. BGP_EVENT_ADD (peer, TCP_connection_open_failed);
  575. break;
  576. case connect_success:
  577. if (BGP_DEBUG (fsm, FSM))
  578. plog_debug (peer->log, "%s [FSM] Connect immediately success",
  579. peer->host);
  580. BGP_EVENT_ADD (peer, TCP_connection_open);
  581. break;
  582. case connect_in_progress:
  583. /* To check nonblocking connect, we wait until socket is
  584. readable or writable. */
  585. if (BGP_DEBUG (fsm, FSM))
  586. plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result",
  587. peer->host);
  588. if (peer->fd < 0)
  589. {
  590. zlog_err ("bgp_start peer's fd is negative value %d",
  591. peer->fd);
  592. return -1;
  593. }
  594. BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
  595. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  596. break;
  597. }
  598. return 0;
  599. }
  600. /* Connect retry timer is expired when the peer status is Connect. */
  601. static int
  602. bgp_reconnect (struct peer *peer)
  603. {
  604. bgp_stop (peer);
  605. bgp_start (peer);
  606. return 0;
  607. }
  608. static int
  609. bgp_fsm_open (struct peer *peer)
  610. {
  611. /* Send keepalive and make keepalive timer */
  612. bgp_keepalive_send (peer);
  613. /* Reset holdtimer value. */
  614. BGP_TIMER_OFF (peer->t_holdtime);
  615. return 0;
  616. }
  617. /* Keepalive send to peer. */
  618. static int
  619. bgp_fsm_keepalive_expire (struct peer *peer)
  620. {
  621. bgp_keepalive_send (peer);
  622. return 0;
  623. }
  624. /* Hold timer expire. This is error of BGP connection. So cut the
  625. peer and change to Idle status. */
  626. static int
  627. bgp_fsm_holdtime_expire (struct peer *peer)
  628. {
  629. if (BGP_DEBUG (fsm, FSM))
  630. zlog (peer->log, LOG_DEBUG, "%s [FSM] Hold timer expire", peer->host);
  631. /* Send notify to remote peer. */
  632. bgp_notify_send (peer, BGP_NOTIFY_HOLD_ERR, 0);
  633. /* Sweep if it is temporary peer. */
  634. if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  635. {
  636. zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
  637. peer_delete (peer);
  638. return -1;
  639. }
  640. return 0;
  641. }
  642. /* Status goes to Established. Send keepalive packet then make first
  643. update information. */
  644. static int
  645. bgp_establish (struct peer *peer)
  646. {
  647. struct bgp_notify *notify;
  648. afi_t afi;
  649. safi_t safi;
  650. int nsf_af_count = 0;
  651. /* Reset capability open status flag. */
  652. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
  653. SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
  654. /* Clear last notification data. */
  655. notify = &peer->notify;
  656. if (notify->data)
  657. XFREE (MTYPE_TMP, notify->data);
  658. memset (notify, 0, sizeof (struct bgp_notify));
  659. /* Clear start timer value to default. */
  660. peer->v_start = BGP_INIT_START_TIMER;
  661. /* Increment established count. */
  662. peer->established++;
  663. bgp_fsm_change_status (peer, Established);
  664. /* bgp log-neighbor-changes of neighbor Up */
  665. if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
  666. zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
  667. /* graceful restart */
  668. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  669. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  670. for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
  671. {
  672. if (peer->afc_nego[afi][safi]
  673. && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
  674. && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
  675. {
  676. if (peer->nsf[afi][safi]
  677. && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
  678. bgp_clear_stale_route (peer, afi, safi);
  679. peer->nsf[afi][safi] = 1;
  680. nsf_af_count++;
  681. }
  682. else
  683. {
  684. if (peer->nsf[afi][safi])
  685. bgp_clear_stale_route (peer, afi, safi);
  686. peer->nsf[afi][safi] = 0;
  687. }
  688. }
  689. if (nsf_af_count)
  690. SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  691. else
  692. {
  693. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  694. if (peer->t_gr_stale)
  695. {
  696. BGP_TIMER_OFF (peer->t_gr_stale);
  697. if (BGP_DEBUG (events, EVENTS))
  698. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  699. }
  700. }
  701. if (peer->t_gr_restart)
  702. {
  703. BGP_TIMER_OFF (peer->t_gr_restart);
  704. if (BGP_DEBUG (events, EVENTS))
  705. zlog_debug ("%s graceful restart timer stopped", peer->host);
  706. }
  707. #ifdef HAVE_SNMP
  708. bgpTrapEstablished (peer);
  709. #endif /* HAVE_SNMP */
  710. /* Reset uptime, send keepalive, send current table. */
  711. bgp_uptime_reset (peer);
  712. /* Send route-refresh when ORF is enabled */
  713. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  714. for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
  715. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
  716. {
  717. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
  718. bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
  719. REFRESH_IMMEDIATE, 0);
  720. else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
  721. bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
  722. REFRESH_IMMEDIATE, 0);
  723. }
  724. if (peer->v_keepalive)
  725. bgp_keepalive_send (peer);
  726. /* First update is deferred until ORF or ROUTE-REFRESH is received */
  727. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  728. for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
  729. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
  730. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
  731. || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
  732. SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
  733. bgp_announce_route_all (peer);
  734. BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1);
  735. return 0;
  736. }
  737. /* Keepalive packet is received. */
  738. static int
  739. bgp_fsm_keepalive (struct peer *peer)
  740. {
  741. /* peer count update */
  742. peer->keepalive_in++;
  743. BGP_TIMER_OFF (peer->t_holdtime);
  744. return 0;
  745. }
  746. /* Update packet is received. */
  747. static int
  748. bgp_fsm_update (struct peer *peer)
  749. {
  750. BGP_TIMER_OFF (peer->t_holdtime);
  751. return 0;
  752. }
  753. /* This is empty event. */
  754. static int
  755. bgp_ignore (struct peer *peer)
  756. {
  757. if (BGP_DEBUG (fsm, FSM))
  758. zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
  759. return 0;
  760. }
  761. /* Finite State Machine structure */
  762. struct {
  763. int (*func) ();
  764. int next_state;
  765. } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
  766. {
  767. {
  768. /* Idle state: In Idle state, all events other than BGP_Start is
  769. ignored. With BGP_Start event, finite state machine calls
  770. bgp_start(). */
  771. {bgp_start, Connect}, /* BGP_Start */
  772. {bgp_stop, Idle}, /* BGP_Stop */
  773. {bgp_stop, Idle}, /* TCP_connection_open */
  774. {bgp_stop, Idle}, /* TCP_connection_closed */
  775. {bgp_ignore, Idle}, /* TCP_connection_open_failed */
  776. {bgp_stop, Idle}, /* TCP_fatal_error */
  777. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  778. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  779. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  780. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  781. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  782. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  783. {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
  784. {bgp_ignore, Idle}, /* Clearing_Completed */
  785. },
  786. {
  787. /* Connect */
  788. {bgp_ignore, Connect}, /* BGP_Start */
  789. {bgp_stop, Idle}, /* BGP_Stop */
  790. {bgp_connect_success, OpenSent}, /* TCP_connection_open */
  791. {bgp_stop, Idle}, /* TCP_connection_closed */
  792. {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
  793. {bgp_connect_fail, Idle}, /* TCP_fatal_error */
  794. {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
  795. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  796. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  797. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  798. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  799. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  800. {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
  801. {bgp_ignore, Idle}, /* Clearing_Completed */
  802. },
  803. {
  804. /* Active, */
  805. {bgp_ignore, Active}, /* BGP_Start */
  806. {bgp_stop, Idle}, /* BGP_Stop */
  807. {bgp_connect_success, OpenSent}, /* TCP_connection_open */
  808. {bgp_stop, Idle}, /* TCP_connection_closed */
  809. {bgp_ignore, Active}, /* TCP_connection_open_failed */
  810. {bgp_ignore, Idle}, /* TCP_fatal_error */
  811. {bgp_start, Connect}, /* ConnectRetry_timer_expired */
  812. {bgp_ignore, Idle}, /* Hold_Timer_expired */
  813. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  814. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  815. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  816. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  817. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  818. {bgp_ignore, Idle}, /* Clearing_Completed */
  819. },
  820. {
  821. /* OpenSent, */
  822. {bgp_ignore, OpenSent}, /* BGP_Start */
  823. {bgp_stop, Idle}, /* BGP_Stop */
  824. {bgp_stop, Active}, /* TCP_connection_open */
  825. {bgp_stop, Active}, /* TCP_connection_closed */
  826. {bgp_stop, Active}, /* TCP_connection_open_failed */
  827. {bgp_stop, Active}, /* TCP_fatal_error */
  828. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  829. {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
  830. {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
  831. {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
  832. {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
  833. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  834. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  835. {bgp_ignore, Idle}, /* Clearing_Completed */
  836. },
  837. {
  838. /* OpenConfirm, */
  839. {bgp_ignore, OpenConfirm}, /* BGP_Start */
  840. {bgp_stop, Idle}, /* BGP_Stop */
  841. {bgp_stop, Idle}, /* TCP_connection_open */
  842. {bgp_stop, Idle}, /* TCP_connection_closed */
  843. {bgp_stop, Idle}, /* TCP_connection_open_failed */
  844. {bgp_stop, Idle}, /* TCP_fatal_error */
  845. {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
  846. {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
  847. {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
  848. {bgp_ignore, Idle}, /* Receive_OPEN_message */
  849. {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
  850. {bgp_ignore, Idle}, /* Receive_UPDATE_message */
  851. {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
  852. {bgp_ignore, Idle}, /* Clearing_Completed */
  853. },
  854. {
  855. /* Established, */
  856. {bgp_ignore, Established}, /* BGP_Start */
  857. {bgp_stop, Clearing}, /* BGP_Stop */
  858. {bgp_stop, Clearing}, /* TCP_connection_open */
  859. {bgp_stop, Clearing}, /* TCP_connection_closed */
  860. {bgp_ignore, Clearing}, /* TCP_connection_open_failed */
  861. {bgp_stop, Clearing}, /* TCP_fatal_error */
  862. {bgp_ignore, Clearing}, /* ConnectRetry_timer_expired */
  863. {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */
  864. {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
  865. {bgp_stop, Clearing}, /* Receive_OPEN_message */
  866. {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
  867. {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
  868. {bgp_stop_with_error, Clearing}, /* Receive_NOTIFICATION_message */
  869. {bgp_ignore, Idle}, /* Clearing_Completed */
  870. },
  871. {
  872. /* Clearing, */
  873. {bgp_ignore, Clearing}, /* BGP_Start */
  874. {bgp_ignore, Clearing}, /* BGP_Stop */
  875. {bgp_ignore, Clearing}, /* TCP_connection_open */
  876. {bgp_ignore, Clearing}, /* TCP_connection_closed */
  877. {bgp_ignore, Clearing}, /* TCP_connection_open_failed */
  878. {bgp_ignore, Clearing}, /* TCP_fatal_error */
  879. {bgp_ignore, Clearing}, /* ConnectRetry_timer_expired */
  880. {bgp_ignore, Clearing}, /* Hold_Timer_expired */
  881. {bgp_ignore, Clearing}, /* KeepAlive_timer_expired */
  882. {bgp_ignore, Clearing}, /* Receive_OPEN_message */
  883. {bgp_ignore, Clearing}, /* Receive_KEEPALIVE_message */
  884. {bgp_ignore, Clearing}, /* Receive_UPDATE_message */
  885. {bgp_ignore, Clearing}, /* Receive_NOTIFICATION_message */
  886. {bgp_ignore, Idle }, /* Clearing_Completed */
  887. },
  888. {
  889. /* Deleted, */
  890. {bgp_ignore, Deleted}, /* BGP_Start */
  891. {bgp_ignore, Deleted}, /* BGP_Stop */
  892. {bgp_ignore, Deleted}, /* TCP_connection_open */
  893. {bgp_ignore, Deleted}, /* TCP_connection_closed */
  894. {bgp_ignore, Deleted}, /* TCP_connection_open_failed */
  895. {bgp_ignore, Deleted}, /* TCP_fatal_error */
  896. {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */
  897. {bgp_ignore, Deleted}, /* Hold_Timer_expired */
  898. {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */
  899. {bgp_ignore, Deleted}, /* Receive_OPEN_message */
  900. {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */
  901. {bgp_ignore, Deleted}, /* Receive_UPDATE_message */
  902. {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */
  903. {bgp_ignore, Deleted}, /* Clearing_Completed */
  904. },
  905. };
  906. static const char *bgp_event_str[] =
  907. {
  908. NULL,
  909. "BGP_Start",
  910. "BGP_Stop",
  911. "TCP_connection_open",
  912. "TCP_connection_closed",
  913. "TCP_connection_open_failed",
  914. "TCP_fatal_error",
  915. "ConnectRetry_timer_expired",
  916. "Hold_Timer_expired",
  917. "KeepAlive_timer_expired",
  918. "Receive_OPEN_message",
  919. "Receive_KEEPALIVE_message",
  920. "Receive_UPDATE_message",
  921. "Receive_NOTIFICATION_message",
  922. "Clearing_Completed",
  923. };
  924. /* Execute event process. */
  925. int
  926. bgp_event (struct thread *thread)
  927. {
  928. int ret = 0;
  929. int event;
  930. int next;
  931. struct peer *peer;
  932. peer = THREAD_ARG (thread);
  933. event = THREAD_VAL (thread);
  934. /* Logging this event. */
  935. next = FSM [peer->status -1][event - 1].next_state;
  936. if (BGP_DEBUG (fsm, FSM) && peer->status != next)
  937. plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
  938. bgp_event_str[event],
  939. LOOKUP (bgp_status_msg, peer->status),
  940. LOOKUP (bgp_status_msg, next));
  941. /* Call function. */
  942. if (FSM [peer->status -1][event - 1].func)
  943. ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
  944. /* When function do not want proceed next job return -1. */
  945. if (ret >= 0)
  946. {
  947. /* If status is changed. */
  948. if (next != peer->status)
  949. bgp_fsm_change_status (peer, next);
  950. /* Make sure timer is set. */
  951. bgp_timer_set (peer);
  952. }
  953. return ret;
  954. }