paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP-4 Finite State Machine |
| 2 | From RFC1771 [A Border Gateway Protocol 4 (BGP-4)] |
| 3 | Copyright (C) 1996, 97, 98 Kunihiro Ishiguro |
| 4 | |
| 5 | This file is part of GNU Zebra. |
| 6 | |
| 7 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | under the terms of the GNU General Public License as published by the |
| 9 | Free Software Foundation; either version 2, or (at your option) any |
| 10 | later version. |
| 11 | |
| 12 | GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | 02111-1307, USA. */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "linklist.h" |
| 25 | #include "prefix.h" |
| 26 | #include "vty.h" |
| 27 | #include "sockunion.h" |
| 28 | #include "thread.h" |
| 29 | #include "log.h" |
| 30 | #include "stream.h" |
| 31 | #include "memory.h" |
| 32 | #include "plist.h" |
| 33 | |
| 34 | #include "bgpd/bgpd.h" |
| 35 | #include "bgpd/bgp_attr.h" |
| 36 | #include "bgpd/bgp_debug.h" |
| 37 | #include "bgpd/bgp_fsm.h" |
| 38 | #include "bgpd/bgp_packet.h" |
| 39 | #include "bgpd/bgp_network.h" |
| 40 | #include "bgpd/bgp_route.h" |
| 41 | #include "bgpd/bgp_dump.h" |
| 42 | #include "bgpd/bgp_open.h" |
| 43 | #ifdef HAVE_SNMP |
| 44 | #include "bgpd/bgp_snmp.h" |
| 45 | #endif /* HAVE_SNMP */ |
| 46 | |
| 47 | /* BGP FSM (finite state machine) has three types of functions. Type |
| 48 | one is thread functions. Type two is event functions. Type three |
| 49 | is FSM functions. Timer functions are set by bgp_timer_set |
| 50 | function. */ |
| 51 | |
| 52 | /* BGP event function. */ |
| 53 | int bgp_event (struct thread *); |
| 54 | |
| 55 | /* BGP thread functions. */ |
| 56 | static int bgp_start_timer (struct thread *); |
| 57 | static int bgp_connect_timer (struct thread *); |
| 58 | static int bgp_holdtime_timer (struct thread *); |
| 59 | static int bgp_keepalive_timer (struct thread *); |
| 60 | |
| 61 | /* BGP FSM functions. */ |
| 62 | static int bgp_start (struct peer *); |
| 63 | |
| 64 | /* BGP start timer jitter. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 65 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 66 | bgp_start_jitter (int time) |
| 67 | { |
| 68 | return ((rand () % (time + 1)) - (time / 2)); |
| 69 | } |
| 70 | |
| 71 | /* Hook function called after bgp event is occered. And vty's |
| 72 | neighbor command invoke this function after making neighbor |
| 73 | structure. */ |
| 74 | void |
| 75 | bgp_timer_set (struct peer *peer) |
| 76 | { |
| 77 | int jitter = 0; |
| 78 | |
| 79 | switch (peer->status) |
| 80 | { |
| 81 | case Idle: |
| 82 | /* First entry point of peer's finite state machine. In Idle |
| 83 | status start timer is on unless peer is shutdown or peer is |
| 84 | inactive. All other timer must be turned off */ |
| 85 | if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN) |
| 86 | || CHECK_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW) |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 87 | || CHECK_FLAG (peer->sflags, PEER_STATUS_CLEARING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | || ! peer_active (peer)) |
| 89 | { |
| 90 | BGP_TIMER_OFF (peer->t_start); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | jitter = bgp_start_jitter (peer->v_start); |
| 95 | BGP_TIMER_ON (peer->t_start, bgp_start_timer, |
| 96 | peer->v_start + jitter); |
| 97 | } |
| 98 | BGP_TIMER_OFF (peer->t_connect); |
| 99 | BGP_TIMER_OFF (peer->t_holdtime); |
| 100 | BGP_TIMER_OFF (peer->t_keepalive); |
| 101 | BGP_TIMER_OFF (peer->t_asorig); |
| 102 | BGP_TIMER_OFF (peer->t_routeadv); |
| 103 | break; |
| 104 | |
| 105 | case Connect: |
| 106 | /* After start timer is expired, the peer moves to Connnect |
| 107 | status. Make sure start timer is off and connect timer is |
| 108 | on. */ |
| 109 | BGP_TIMER_OFF (peer->t_start); |
| 110 | BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect); |
| 111 | BGP_TIMER_OFF (peer->t_holdtime); |
| 112 | BGP_TIMER_OFF (peer->t_keepalive); |
| 113 | BGP_TIMER_OFF (peer->t_asorig); |
| 114 | BGP_TIMER_OFF (peer->t_routeadv); |
| 115 | break; |
| 116 | |
| 117 | case Active: |
| 118 | /* Active is waiting connection from remote peer. And if |
| 119 | connect timer is expired, change status to Connect. */ |
| 120 | BGP_TIMER_OFF (peer->t_start); |
| 121 | /* If peer is passive mode, do not set connect timer. */ |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 122 | if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE) |
| 123 | || CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | { |
| 125 | BGP_TIMER_OFF (peer->t_connect); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect); |
| 130 | } |
| 131 | BGP_TIMER_OFF (peer->t_holdtime); |
| 132 | BGP_TIMER_OFF (peer->t_keepalive); |
| 133 | BGP_TIMER_OFF (peer->t_asorig); |
| 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); |
| 151 | BGP_TIMER_OFF (peer->t_asorig); |
| 152 | BGP_TIMER_OFF (peer->t_routeadv); |
| 153 | break; |
| 154 | |
| 155 | case OpenConfirm: |
| 156 | /* OpenConfirm status. */ |
| 157 | BGP_TIMER_OFF (peer->t_start); |
| 158 | BGP_TIMER_OFF (peer->t_connect); |
| 159 | |
| 160 | /* If the negotiated Hold Time value is zero, then the Hold Time |
| 161 | timer and KeepAlive timers are not started. */ |
| 162 | if (peer->v_holdtime == 0) |
| 163 | { |
| 164 | BGP_TIMER_OFF (peer->t_holdtime); |
| 165 | BGP_TIMER_OFF (peer->t_keepalive); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer, |
| 170 | peer->v_holdtime); |
| 171 | BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer, |
| 172 | peer->v_keepalive); |
| 173 | } |
| 174 | BGP_TIMER_OFF (peer->t_asorig); |
| 175 | BGP_TIMER_OFF (peer->t_routeadv); |
| 176 | break; |
| 177 | |
| 178 | case Established: |
| 179 | /* In Established status start and connect timer is turned |
| 180 | off. */ |
| 181 | BGP_TIMER_OFF (peer->t_start); |
| 182 | BGP_TIMER_OFF (peer->t_connect); |
| 183 | |
| 184 | /* Same as OpenConfirm, if holdtime is zero then both holdtime |
| 185 | and keepalive must be turned off. */ |
| 186 | if (peer->v_holdtime == 0) |
| 187 | { |
| 188 | BGP_TIMER_OFF (peer->t_holdtime); |
| 189 | BGP_TIMER_OFF (peer->t_keepalive); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer, |
| 194 | peer->v_holdtime); |
| 195 | BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer, |
| 196 | peer->v_keepalive); |
| 197 | } |
| 198 | BGP_TIMER_OFF (peer->t_asorig); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* BGP start timer. This function set BGP_Start event to thread value |
| 204 | and process event. */ |
| 205 | static int |
| 206 | bgp_start_timer (struct thread *thread) |
| 207 | { |
| 208 | struct peer *peer; |
| 209 | |
| 210 | peer = THREAD_ARG (thread); |
| 211 | peer->t_start = NULL; |
| 212 | |
| 213 | if (BGP_DEBUG (fsm, FSM)) |
| 214 | zlog (peer->log, LOG_DEBUG, |
| 215 | "%s [FSM] Timer (start timer expire).", peer->host); |
| 216 | |
| 217 | THREAD_VAL (thread) = BGP_Start; |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 218 | bgp_event (thread); /* bgp_event unlocks peer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* BGP connect retry timer. */ |
| 224 | static int |
| 225 | bgp_connect_timer (struct thread *thread) |
| 226 | { |
| 227 | struct peer *peer; |
| 228 | |
| 229 | peer = THREAD_ARG (thread); |
| 230 | peer->t_connect = NULL; |
| 231 | |
| 232 | if (BGP_DEBUG (fsm, FSM)) |
| 233 | zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)", |
| 234 | peer->host); |
| 235 | |
| 236 | THREAD_VAL (thread) = ConnectRetry_timer_expired; |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 237 | bgp_event (thread); /* bgp_event unlocks peer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /* BGP holdtime timer. */ |
| 243 | static int |
| 244 | bgp_holdtime_timer (struct thread *thread) |
| 245 | { |
| 246 | struct peer *peer; |
| 247 | |
| 248 | peer = THREAD_ARG (thread); |
| 249 | peer->t_holdtime = NULL; |
| 250 | |
| 251 | if (BGP_DEBUG (fsm, FSM)) |
| 252 | zlog (peer->log, LOG_DEBUG, |
| 253 | "%s [FSM] Timer (holdtime timer expire)", |
| 254 | peer->host); |
| 255 | |
| 256 | THREAD_VAL (thread) = Hold_Timer_expired; |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 257 | bgp_event (thread); /* bgp_event unlocks peer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* BGP keepalive fire ! */ |
| 263 | static int |
| 264 | bgp_keepalive_timer (struct thread *thread) |
| 265 | { |
| 266 | struct peer *peer; |
| 267 | |
| 268 | peer = THREAD_ARG (thread); |
| 269 | peer->t_keepalive = NULL; |
| 270 | |
| 271 | if (BGP_DEBUG (fsm, FSM)) |
| 272 | zlog (peer->log, LOG_DEBUG, |
| 273 | "%s [FSM] Timer (keepalive timer expire)", |
| 274 | peer->host); |
| 275 | |
| 276 | THREAD_VAL (thread) = KeepAlive_timer_expired; |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 277 | bgp_event (thread); /* bgp_event unlocks peer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 282 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | bgp_routeadv_timer (struct thread *thread) |
| 284 | { |
| 285 | struct peer *peer; |
| 286 | |
| 287 | peer = THREAD_ARG (thread); |
| 288 | peer->t_routeadv = NULL; |
| 289 | |
| 290 | if (BGP_DEBUG (fsm, FSM)) |
| 291 | zlog (peer->log, LOG_DEBUG, |
| 292 | "%s [FSM] Timer (routeadv timer expire)", |
| 293 | peer->host); |
| 294 | |
| 295 | peer->synctime = time (NULL); |
| 296 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 297 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 298 | |
| 299 | BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, |
| 300 | peer->v_routeadv); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /* Reset bgp update timer */ |
| 306 | static void |
| 307 | bgp_uptime_reset (struct peer *peer) |
| 308 | { |
| 309 | peer->uptime = time (NULL); |
| 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++) |
| 352 | for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++) |
| 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++) |
| 385 | for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++) |
| 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 | |
| 399 | /* Preserve old status and change into new status. */ |
| 400 | peer->ostatus = peer->status; |
| 401 | peer->status = status; |
| 402 | |
| 403 | if (BGP_DEBUG (normal, NORMAL)) |
| 404 | zlog_debug ("%s went from %s to %s", |
| 405 | peer->host, |
| 406 | LOOKUP (bgp_status_msg, peer->ostatus), |
| 407 | LOOKUP (bgp_status_msg, peer->status)); |
| 408 | } |
| 409 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 410 | /* Administrative BGP peer stop event. */ |
| 411 | int |
| 412 | bgp_stop (struct peer *peer) |
| 413 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 414 | afi_t afi; |
| 415 | safi_t safi; |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 416 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 417 | char orf_name[BUFSIZ]; |
| 418 | |
| 419 | /* Increment Dropped count. */ |
| 420 | if (peer->status == Established) |
| 421 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 422 | peer->dropped++; |
| 423 | bgp_fsm_change_status (peer, Idle); |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 424 | |
| 425 | /* bgp log-neighbor-changes of neighbor Down */ |
| 426 | if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 427 | zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host, |
| 428 | peer_down_str [(int) peer->last_reset]); |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 429 | |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 430 | /* graceful restart */ |
| 431 | if (peer->t_gr_stale) |
| 432 | { |
| 433 | BGP_TIMER_OFF (peer->t_gr_stale); |
| 434 | if (BGP_DEBUG (events, EVENTS)) |
| 435 | zlog_debug ("%s graceful restart stalepath timer stopped", peer->host); |
| 436 | } |
| 437 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT)) |
| 438 | { |
| 439 | if (BGP_DEBUG (events, EVENTS)) |
| 440 | { |
| 441 | zlog_debug ("%s graceful restart timer started for %d sec", |
| 442 | peer->host, peer->v_gr_restart); |
| 443 | zlog_debug ("%s graceful restart stalepath timer started for %d sec", |
| 444 | peer->host, peer->bgp->stalepath_time); |
| 445 | } |
| 446 | BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire, |
| 447 | peer->v_gr_restart); |
| 448 | BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire, |
| 449 | peer->bgp->stalepath_time); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE); |
| 454 | |
| 455 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 456 | for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++) |
| 457 | peer->nsf[afi][safi] = 0; |
| 458 | } |
| 459 | |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 460 | /* set last reset time */ |
| 461 | peer->resettime = time (NULL); |
paul | c531740 | 2004-05-03 13:25:06 +0000 | [diff] [blame] | 462 | /* Reset uptime. */ |
| 463 | bgp_uptime_reset (peer); |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 464 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 465 | #ifdef HAVE_SNMP |
| 466 | bgpTrapBackwardTransition (peer); |
| 467 | #endif /* HAVE_SNMP */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 468 | |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 469 | /* Reset uptime. */ |
| 470 | bgp_uptime_reset (peer); |
| 471 | |
| 472 | /* Need of clear of peer. */ |
| 473 | bgp_clear_route_all (peer); |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 474 | |
| 475 | /* Reset peer synctime */ |
| 476 | peer->synctime = 0; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 477 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | |
| 479 | /* Stop read and write threads when exists. */ |
| 480 | BGP_READ_OFF (peer->t_read); |
| 481 | BGP_WRITE_OFF (peer->t_write); |
| 482 | |
| 483 | /* Stop all timers. */ |
| 484 | BGP_TIMER_OFF (peer->t_start); |
| 485 | BGP_TIMER_OFF (peer->t_connect); |
| 486 | BGP_TIMER_OFF (peer->t_holdtime); |
| 487 | BGP_TIMER_OFF (peer->t_keepalive); |
| 488 | BGP_TIMER_OFF (peer->t_asorig); |
| 489 | BGP_TIMER_OFF (peer->t_routeadv); |
| 490 | |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 491 | /* Delete all existing events of the peer, |
| 492 | and corresponding peer ref-count */ |
| 493 | for (i = thread_cancel_event (master, peer); i > 0; i--) |
| 494 | peer_unlock (peer); /* thread event reference */ |
| 495 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 496 | /* Stream reset. */ |
| 497 | peer->packet_size = 0; |
| 498 | |
| 499 | /* Clear input and output buffer. */ |
| 500 | if (peer->ibuf) |
| 501 | stream_reset (peer->ibuf); |
| 502 | if (peer->work) |
| 503 | stream_reset (peer->work); |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 504 | if (peer->obuf) |
| 505 | stream_fifo_clean (peer->obuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 506 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 507 | /* Close of file descriptor. */ |
| 508 | if (peer->fd >= 0) |
| 509 | { |
| 510 | close (peer->fd); |
| 511 | peer->fd = -1; |
| 512 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 513 | |
| 514 | /* Connection information. */ |
| 515 | if (peer->su_local) |
| 516 | { |
paul | 22db9de | 2005-05-19 01:50:11 +0000 | [diff] [blame] | 517 | sockunion_free (peer->su_local); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 518 | peer->su_local = NULL; |
| 519 | } |
| 520 | |
| 521 | if (peer->su_remote) |
| 522 | { |
paul | 22db9de | 2005-05-19 01:50:11 +0000 | [diff] [blame] | 523 | sockunion_free (peer->su_remote); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | peer->su_remote = NULL; |
| 525 | } |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 526 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 527 | /* Clear remote router-id. */ |
| 528 | peer->remote_id.s_addr = 0; |
| 529 | |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 530 | /* Clear peer capability flag. */ |
| 531 | peer->cap = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 532 | |
| 533 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 534 | for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) |
| 535 | { |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 536 | /* Reset all negotiated variables */ |
| 537 | peer->afc_nego[afi][safi] = 0; |
| 538 | peer->afc_adv[afi][safi] = 0; |
| 539 | peer->afc_recv[afi][safi] = 0; |
| 540 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 541 | /* peer address family capability flags*/ |
| 542 | peer->af_cap[afi][safi] = 0; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 543 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 544 | /* peer address family status flags*/ |
| 545 | peer->af_sflags[afi][safi] = 0; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 546 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | /* Received ORF prefix-filter */ |
| 548 | peer->orf_plist[afi][safi] = NULL; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 549 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 550 | /* ORF received prefix-filter pnt */ |
| 551 | sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi); |
| 552 | prefix_bgp_orf_remove_all (orf_name); |
| 553 | } |
| 554 | |
| 555 | /* Reset keepalive and holdtime */ |
| 556 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
| 557 | { |
| 558 | peer->v_keepalive = peer->keepalive; |
| 559 | peer->v_holdtime = peer->holdtime; |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | peer->v_keepalive = peer->bgp->default_keepalive; |
| 564 | peer->v_holdtime = peer->bgp->default_holdtime; |
| 565 | } |
| 566 | |
| 567 | peer->update_time = 0; |
| 568 | |
| 569 | /* Until we are sure that there is no problem about prefix count |
| 570 | this should be commented out.*/ |
| 571 | #if 0 |
| 572 | /* Reset prefix count */ |
| 573 | peer->pcount[AFI_IP][SAFI_UNICAST] = 0; |
| 574 | peer->pcount[AFI_IP][SAFI_MULTICAST] = 0; |
| 575 | peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0; |
| 576 | peer->pcount[AFI_IP6][SAFI_UNICAST] = 0; |
| 577 | peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0; |
| 578 | #endif /* 0 */ |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /* BGP peer is stoped by the error. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 584 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 585 | bgp_stop_with_error (struct peer *peer) |
| 586 | { |
| 587 | /* Double start timer. */ |
| 588 | peer->v_start *= 2; |
| 589 | |
| 590 | /* Overflow check. */ |
| 591 | if (peer->v_start >= (60 * 2)) |
| 592 | peer->v_start = (60 * 2); |
| 593 | |
| 594 | bgp_stop (peer); |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | /* TCP connection open. Next we send open message to remote peer. And |
| 600 | add read thread for reading open message. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 601 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 602 | bgp_connect_success (struct peer *peer) |
| 603 | { |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 604 | char buf1[BUFSIZ]; |
| 605 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 606 | if (peer->fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 607 | { |
| 608 | zlog_err ("bgp_connect_success peer's fd is negative value %d", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 609 | peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 610 | return -1; |
| 611 | } |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 612 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 613 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 614 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 615 | bgp_getsockname (peer); |
| 616 | |
| 617 | if (BGP_DEBUG (normal, NORMAL)) |
| 618 | { |
| 619 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 620 | zlog_debug ("%s open active, local address %s", peer->host, |
| 621 | sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN)); |
| 622 | else |
| 623 | zlog_debug ("%s passive open", peer->host); |
| 624 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 625 | |
| 626 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 627 | bgp_open_send (peer); |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /* TCP connect fail */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 633 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 634 | bgp_connect_fail (struct peer *peer) |
| 635 | { |
| 636 | bgp_stop (peer); |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | /* This function is the first starting point of all BGP connection. It |
| 641 | try to connect to remote peer with non-blocking IO. */ |
| 642 | int |
| 643 | bgp_start (struct peer *peer) |
| 644 | { |
| 645 | int status; |
| 646 | |
| 647 | /* If the peer is passive mode, force to move to Active mode. */ |
| 648 | if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)) |
| 649 | { |
| 650 | BGP_EVENT_ADD (peer, TCP_connection_open_failed); |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | status = bgp_connect (peer); |
| 655 | |
| 656 | switch (status) |
| 657 | { |
| 658 | case connect_error: |
| 659 | if (BGP_DEBUG (fsm, FSM)) |
ajs | 8c2e200 | 2004-12-08 20:08:54 +0000 | [diff] [blame] | 660 | plog_debug (peer->log, "%s [FSM] Connect error", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 661 | BGP_EVENT_ADD (peer, TCP_connection_open_failed); |
| 662 | break; |
| 663 | case connect_success: |
| 664 | if (BGP_DEBUG (fsm, FSM)) |
ajs | 8c2e200 | 2004-12-08 20:08:54 +0000 | [diff] [blame] | 665 | plog_debug (peer->log, "%s [FSM] Connect immediately success", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 666 | peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 667 | BGP_EVENT_ADD (peer, TCP_connection_open); |
| 668 | break; |
| 669 | case connect_in_progress: |
| 670 | /* To check nonblocking connect, we wait until socket is |
| 671 | readable or writable. */ |
| 672 | if (BGP_DEBUG (fsm, FSM)) |
ajs | 8c2e200 | 2004-12-08 20:08:54 +0000 | [diff] [blame] | 673 | plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 674 | peer->host); |
| 675 | if (peer->fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 676 | { |
| 677 | zlog_err ("bgp_start peer's fd is negative value %d", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 678 | peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 679 | return -1; |
| 680 | } |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 681 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
| 682 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | break; |
| 684 | } |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | /* Connect retry timer is expired when the peer status is Connect. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 689 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | bgp_reconnect (struct peer *peer) |
| 691 | { |
| 692 | bgp_stop (peer); |
| 693 | bgp_start (peer); |
| 694 | return 0; |
| 695 | } |
| 696 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 697 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 698 | bgp_fsm_open (struct peer *peer) |
| 699 | { |
| 700 | /* Send keepalive and make keepalive timer */ |
| 701 | bgp_keepalive_send (peer); |
| 702 | |
| 703 | /* Reset holdtimer value. */ |
| 704 | BGP_TIMER_OFF (peer->t_holdtime); |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | /* Keepalive send to peer. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 710 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 711 | bgp_fsm_keepalive_expire (struct peer *peer) |
| 712 | { |
| 713 | bgp_keepalive_send (peer); |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /* Hold timer expire. This is error of BGP connection. So cut the |
| 718 | peer and change to Idle status. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 719 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 720 | bgp_fsm_holdtime_expire (struct peer *peer) |
| 721 | { |
| 722 | if (BGP_DEBUG (fsm, FSM)) |
| 723 | zlog (peer->log, LOG_DEBUG, "%s [FSM] Hold timer expire", peer->host); |
| 724 | |
| 725 | /* Send notify to remote peer. */ |
| 726 | bgp_notify_send (peer, BGP_NOTIFY_HOLD_ERR, 0); |
| 727 | |
| 728 | /* Sweep if it is temporary peer. */ |
| 729 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 730 | { |
| 731 | zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host); |
| 732 | peer_delete (peer); |
| 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | /* Status goes to Established. Send keepalive packet then make first |
| 740 | update information. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 741 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 742 | bgp_establish (struct peer *peer) |
| 743 | { |
| 744 | struct bgp_notify *notify; |
| 745 | afi_t afi; |
| 746 | safi_t safi; |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 747 | int nsf_af_count = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 748 | |
| 749 | /* Reset capability open status flag. */ |
| 750 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN)) |
| 751 | SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 752 | |
| 753 | /* Clear last notification data. */ |
| 754 | notify = &peer->notify; |
| 755 | if (notify->data) |
| 756 | XFREE (MTYPE_TMP, notify->data); |
| 757 | memset (notify, 0, sizeof (struct bgp_notify)); |
| 758 | |
| 759 | /* Clear start timer value to default. */ |
| 760 | peer->v_start = BGP_INIT_START_TIMER; |
| 761 | |
| 762 | /* Increment established count. */ |
| 763 | peer->established++; |
| 764 | bgp_fsm_change_status (peer, Established); |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 765 | |
| 766 | /* bgp log-neighbor-changes of neighbor Up */ |
| 767 | if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) |
| 768 | zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host); |
| 769 | |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 770 | /* graceful restart */ |
| 771 | UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT); |
| 772 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 773 | for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++) |
| 774 | { |
| 775 | if (peer->afc_nego[afi][safi] |
| 776 | && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV) |
| 777 | && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV)) |
| 778 | { |
| 779 | if (peer->nsf[afi][safi] |
| 780 | && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV)) |
| 781 | bgp_clear_stale_route (peer, afi, safi); |
| 782 | |
| 783 | peer->nsf[afi][safi] = 1; |
| 784 | nsf_af_count++; |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | if (peer->nsf[afi][safi]) |
| 789 | bgp_clear_stale_route (peer, afi, safi); |
| 790 | peer->nsf[afi][safi] = 0; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (nsf_af_count) |
| 795 | SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE); |
| 796 | else |
| 797 | { |
| 798 | UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE); |
| 799 | if (peer->t_gr_stale) |
| 800 | { |
| 801 | BGP_TIMER_OFF (peer->t_gr_stale); |
| 802 | if (BGP_DEBUG (events, EVENTS)) |
| 803 | zlog_debug ("%s graceful restart stalepath timer stopped", peer->host); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | if (peer->t_gr_restart) |
| 808 | { |
| 809 | BGP_TIMER_OFF (peer->t_gr_restart); |
| 810 | if (BGP_DEBUG (events, EVENTS)) |
| 811 | zlog_debug ("%s graceful restart timer stopped", peer->host); |
| 812 | } |
| 813 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 814 | #ifdef HAVE_SNMP |
| 815 | bgpTrapEstablished (peer); |
| 816 | #endif /* HAVE_SNMP */ |
| 817 | |
| 818 | /* Reset uptime, send keepalive, send current table. */ |
| 819 | bgp_uptime_reset (peer); |
| 820 | |
| 821 | /* Send route-refresh when ORF is enabled */ |
| 822 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 823 | for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) |
| 824 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV)) |
| 825 | { |
| 826 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV)) |
| 827 | bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX, |
| 828 | REFRESH_IMMEDIATE, 0); |
| 829 | else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV)) |
| 830 | bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD, |
| 831 | REFRESH_IMMEDIATE, 0); |
| 832 | } |
| 833 | |
| 834 | if (peer->v_keepalive) |
| 835 | bgp_keepalive_send (peer); |
| 836 | |
| 837 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ |
| 838 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 839 | for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) |
| 840 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)) |
| 841 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV) |
| 842 | || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV)) |
| 843 | SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH); |
| 844 | |
| 845 | bgp_announce_route_all (peer); |
| 846 | |
| 847 | BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1); |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | /* Keepalive packet is received. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 853 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 854 | bgp_fsm_keepalive (struct peer *peer) |
| 855 | { |
| 856 | /* peer count update */ |
| 857 | peer->keepalive_in++; |
| 858 | |
| 859 | BGP_TIMER_OFF (peer->t_holdtime); |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | /* Update packet is received. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 864 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 865 | bgp_fsm_update (struct peer *peer) |
| 866 | { |
| 867 | BGP_TIMER_OFF (peer->t_holdtime); |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | /* This is empty event. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 872 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 873 | bgp_ignore (struct peer *peer) |
| 874 | { |
| 875 | if (BGP_DEBUG (fsm, FSM)) |
| 876 | zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host); |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | /* Finite State Machine structure */ |
| 881 | struct { |
| 882 | int (*func) (); |
| 883 | int next_state; |
| 884 | } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] = |
| 885 | { |
| 886 | { |
| 887 | /* Idle state: In Idle state, all events other than BGP_Start is |
| 888 | ignored. With BGP_Start event, finite state machine calls |
| 889 | bgp_start(). */ |
| 890 | {bgp_start, Connect}, /* BGP_Start */ |
| 891 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 892 | {bgp_stop, Idle}, /* TCP_connection_open */ |
| 893 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 894 | {bgp_ignore, Idle}, /* TCP_connection_open_failed */ |
| 895 | {bgp_stop, Idle}, /* TCP_fatal_error */ |
| 896 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ |
| 897 | {bgp_ignore, Idle}, /* Hold_Timer_expired */ |
| 898 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ |
| 899 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ |
| 900 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ |
| 901 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 902 | {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */ |
| 903 | }, |
| 904 | { |
| 905 | /* Connect */ |
| 906 | {bgp_ignore, Connect}, /* BGP_Start */ |
| 907 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 908 | {bgp_connect_success, OpenSent}, /* TCP_connection_open */ |
| 909 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 910 | {bgp_connect_fail, Active}, /* TCP_connection_open_failed */ |
| 911 | {bgp_connect_fail, Idle}, /* TCP_fatal_error */ |
| 912 | {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */ |
| 913 | {bgp_ignore, Idle}, /* Hold_Timer_expired */ |
| 914 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ |
| 915 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ |
| 916 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ |
| 917 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 918 | {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */ |
| 919 | }, |
| 920 | { |
| 921 | /* Active, */ |
| 922 | {bgp_ignore, Active}, /* BGP_Start */ |
| 923 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 924 | {bgp_connect_success, OpenSent}, /* TCP_connection_open */ |
| 925 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 926 | {bgp_ignore, Active}, /* TCP_connection_open_failed */ |
| 927 | {bgp_ignore, Idle}, /* TCP_fatal_error */ |
| 928 | {bgp_start, Connect}, /* ConnectRetry_timer_expired */ |
| 929 | {bgp_ignore, Idle}, /* Hold_Timer_expired */ |
| 930 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ |
| 931 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ |
| 932 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ |
| 933 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 934 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ |
| 935 | }, |
| 936 | { |
| 937 | /* OpenSent, */ |
| 938 | {bgp_ignore, OpenSent}, /* BGP_Start */ |
| 939 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 940 | {bgp_stop, Idle}, /* TCP_connection_open */ |
| 941 | {bgp_stop, Active}, /* TCP_connection_closed */ |
| 942 | {bgp_ignore, Idle}, /* TCP_connection_open_failed */ |
| 943 | {bgp_stop, Idle}, /* TCP_fatal_error */ |
| 944 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ |
| 945 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ |
| 946 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ |
| 947 | {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */ |
| 948 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ |
| 949 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 950 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ |
| 951 | }, |
| 952 | { |
| 953 | /* OpenConfirm, */ |
| 954 | {bgp_ignore, OpenConfirm}, /* BGP_Start */ |
| 955 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 956 | {bgp_stop, Idle}, /* TCP_connection_open */ |
| 957 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 958 | {bgp_stop, Idle}, /* TCP_connection_open_failed */ |
| 959 | {bgp_stop, Idle}, /* TCP_fatal_error */ |
| 960 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ |
| 961 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ |
| 962 | {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */ |
| 963 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ |
| 964 | {bgp_establish, Established}, /* Receive_KEEPALIVE_message */ |
| 965 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 966 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ |
| 967 | }, |
| 968 | { |
| 969 | /* Established, */ |
| 970 | {bgp_ignore, Established}, /* BGP_Start */ |
| 971 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 972 | {bgp_stop, Idle}, /* TCP_connection_open */ |
| 973 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 974 | {bgp_ignore, Idle}, /* TCP_connection_open_failed */ |
| 975 | {bgp_stop, Idle}, /* TCP_fatal_error */ |
| 976 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ |
| 977 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ |
| 978 | {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */ |
| 979 | {bgp_stop, Idle}, /* Receive_OPEN_message */ |
| 980 | {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */ |
| 981 | {bgp_fsm_update, Established}, /* Receive_UPDATE_message */ |
| 982 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ |
| 983 | }, |
| 984 | }; |
| 985 | |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 986 | static const char *bgp_event_str[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 987 | { |
| 988 | NULL, |
| 989 | "BGP_Start", |
| 990 | "BGP_Stop", |
| 991 | "TCP_connection_open", |
| 992 | "TCP_connection_closed", |
| 993 | "TCP_connection_open_failed", |
| 994 | "TCP_fatal_error", |
| 995 | "ConnectRetry_timer_expired", |
| 996 | "Hold_Timer_expired", |
| 997 | "KeepAlive_timer_expired", |
| 998 | "Receive_OPEN_message", |
| 999 | "Receive_KEEPALIVE_message", |
| 1000 | "Receive_UPDATE_message", |
| 1001 | "Receive_NOTIFICATION_message" |
| 1002 | }; |
| 1003 | |
| 1004 | /* Execute event process. */ |
| 1005 | int |
| 1006 | bgp_event (struct thread *thread) |
| 1007 | { |
| 1008 | int ret; |
| 1009 | int event; |
| 1010 | int next; |
| 1011 | struct peer *peer; |
| 1012 | |
| 1013 | peer = THREAD_ARG (thread); |
| 1014 | event = THREAD_VAL (thread); |
| 1015 | |
| 1016 | /* Logging this event. */ |
| 1017 | next = FSM [peer->status -1][event - 1].next_state; |
| 1018 | |
| 1019 | if (BGP_DEBUG (fsm, FSM)) |
ajs | 8c2e200 | 2004-12-08 20:08:54 +0000 | [diff] [blame] | 1020 | plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1021 | bgp_event_str[event], |
| 1022 | LOOKUP (bgp_status_msg, peer->status), |
| 1023 | LOOKUP (bgp_status_msg, next)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1024 | |
| 1025 | /* Call function. */ |
| 1026 | ret = (*(FSM [peer->status - 1][event - 1].func))(peer); |
| 1027 | |
| 1028 | /* When function do not want proceed next job return -1. */ |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 1029 | if (ret >= 0) |
| 1030 | { |
| 1031 | /* If status is changed. */ |
| 1032 | if (next != peer->status) |
| 1033 | bgp_fsm_change_status (peer, next); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1034 | |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 1035 | /* Make sure timer is set. */ |
| 1036 | bgp_timer_set (peer); |
| 1037 | } |
| 1038 | |
| 1039 | peer_unlock (peer); /* bgp-event peer reference */ |
| 1040 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1041 | } |