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