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