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