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