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 | |
| 412 | /* Preserve old status and change into new status. */ |
| 413 | peer->ostatus = peer->status; |
| 414 | peer->status = status; |
| 415 | |
| 416 | if (BGP_DEBUG (normal, NORMAL)) |
| 417 | zlog_debug ("%s went from %s to %s", |
| 418 | peer->host, |
| 419 | LOOKUP (bgp_status_msg, peer->ostatus), |
| 420 | LOOKUP (bgp_status_msg, peer->status)); |
| 421 | } |
| 422 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 423 | /* Administrative BGP peer stop event. */ |
| 424 | int |
| 425 | bgp_stop (struct peer *peer) |
| 426 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | afi_t afi; |
| 428 | safi_t safi; |
| 429 | char orf_name[BUFSIZ]; |
| 430 | |
Paul Jakma | dcdf399 | 2006-10-15 23:39:59 +0000 | [diff] [blame] | 431 | /* Delete all existing events of the peer */ |
| 432 | BGP_EVENT_FLUSH (peer); |
| 433 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 434 | /* Increment Dropped count. */ |
| 435 | if (peer->status == Established) |
| 436 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 437 | peer->dropped++; |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 438 | |
| 439 | /* bgp log-neighbor-changes of neighbor Down */ |
| 440 | if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 441 | zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host, |
| 442 | peer_down_str [(int) peer->last_reset]); |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 443 | |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 444 | /* graceful restart */ |
| 445 | if (peer->t_gr_stale) |
| 446 | { |
| 447 | BGP_TIMER_OFF (peer->t_gr_stale); |
| 448 | if (BGP_DEBUG (events, EVENTS)) |
| 449 | zlog_debug ("%s graceful restart stalepath timer stopped", peer->host); |
| 450 | } |
| 451 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT)) |
| 452 | { |
| 453 | if (BGP_DEBUG (events, EVENTS)) |
| 454 | { |
| 455 | zlog_debug ("%s graceful restart timer started for %d sec", |
| 456 | peer->host, peer->v_gr_restart); |
| 457 | zlog_debug ("%s graceful restart stalepath timer started for %d sec", |
| 458 | peer->host, peer->bgp->stalepath_time); |
| 459 | } |
| 460 | BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire, |
| 461 | peer->v_gr_restart); |
| 462 | BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire, |
| 463 | peer->bgp->stalepath_time); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE); |
| 468 | |
| 469 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 470 | for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++) |
| 471 | peer->nsf[afi][safi] = 0; |
| 472 | } |
| 473 | |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 474 | /* set last reset time */ |
| 475 | peer->resettime = time (NULL); |
paul | c531740 | 2004-05-03 13:25:06 +0000 | [diff] [blame] | 476 | /* Reset uptime. */ |
| 477 | bgp_uptime_reset (peer); |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 478 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 479 | #ifdef HAVE_SNMP |
| 480 | bgpTrapBackwardTransition (peer); |
| 481 | #endif /* HAVE_SNMP */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 482 | |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 483 | /* Reset uptime. */ |
| 484 | bgp_uptime_reset (peer); |
| 485 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 486 | /* Reset peer synctime */ |
| 487 | peer->synctime = 0; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 488 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | |
| 490 | /* Stop read and write threads when exists. */ |
| 491 | BGP_READ_OFF (peer->t_read); |
| 492 | BGP_WRITE_OFF (peer->t_write); |
| 493 | |
| 494 | /* Stop all timers. */ |
| 495 | BGP_TIMER_OFF (peer->t_start); |
| 496 | BGP_TIMER_OFF (peer->t_connect); |
| 497 | BGP_TIMER_OFF (peer->t_holdtime); |
| 498 | BGP_TIMER_OFF (peer->t_keepalive); |
| 499 | BGP_TIMER_OFF (peer->t_asorig); |
| 500 | BGP_TIMER_OFF (peer->t_routeadv); |
| 501 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 502 | /* Stream reset. */ |
| 503 | peer->packet_size = 0; |
| 504 | |
| 505 | /* Clear input and output buffer. */ |
| 506 | if (peer->ibuf) |
| 507 | stream_reset (peer->ibuf); |
| 508 | if (peer->work) |
| 509 | stream_reset (peer->work); |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 510 | if (peer->obuf) |
| 511 | stream_fifo_clean (peer->obuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 513 | /* Close of file descriptor. */ |
| 514 | if (peer->fd >= 0) |
| 515 | { |
| 516 | close (peer->fd); |
| 517 | peer->fd = -1; |
| 518 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 519 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 521 | for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) |
| 522 | { |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 523 | /* Reset all negotiated variables */ |
| 524 | peer->afc_nego[afi][safi] = 0; |
| 525 | peer->afc_adv[afi][safi] = 0; |
| 526 | peer->afc_recv[afi][safi] = 0; |
| 527 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 528 | /* peer address family capability flags*/ |
| 529 | peer->af_cap[afi][safi] = 0; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 530 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | /* peer address family status flags*/ |
| 532 | peer->af_sflags[afi][safi] = 0; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 533 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | /* Received ORF prefix-filter */ |
| 535 | peer->orf_plist[afi][safi] = NULL; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 536 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 537 | /* ORF received prefix-filter pnt */ |
| 538 | sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi); |
| 539 | prefix_bgp_orf_remove_all (orf_name); |
| 540 | } |
| 541 | |
| 542 | /* Reset keepalive and holdtime */ |
| 543 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
| 544 | { |
| 545 | peer->v_keepalive = peer->keepalive; |
| 546 | peer->v_holdtime = peer->holdtime; |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | peer->v_keepalive = peer->bgp->default_keepalive; |
| 551 | peer->v_holdtime = peer->bgp->default_holdtime; |
| 552 | } |
| 553 | |
| 554 | peer->update_time = 0; |
| 555 | |
| 556 | /* Until we are sure that there is no problem about prefix count |
| 557 | this should be commented out.*/ |
| 558 | #if 0 |
| 559 | /* Reset prefix count */ |
| 560 | peer->pcount[AFI_IP][SAFI_UNICAST] = 0; |
| 561 | peer->pcount[AFI_IP][SAFI_MULTICAST] = 0; |
| 562 | peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0; |
| 563 | peer->pcount[AFI_IP6][SAFI_UNICAST] = 0; |
| 564 | peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0; |
| 565 | #endif /* 0 */ |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | /* BGP peer is stoped by the error. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 571 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 572 | bgp_stop_with_error (struct peer *peer) |
| 573 | { |
| 574 | /* Double start timer. */ |
| 575 | peer->v_start *= 2; |
| 576 | |
| 577 | /* Overflow check. */ |
| 578 | if (peer->v_start >= (60 * 2)) |
| 579 | peer->v_start = (60 * 2); |
| 580 | |
| 581 | bgp_stop (peer); |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | /* TCP connection open. Next we send open message to remote peer. And |
| 587 | add read thread for reading open message. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 588 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 589 | bgp_connect_success (struct peer *peer) |
| 590 | { |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 591 | char buf1[BUFSIZ]; |
| 592 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 593 | if (peer->fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 594 | { |
| 595 | zlog_err ("bgp_connect_success peer's fd is negative value %d", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 596 | peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 597 | return -1; |
| 598 | } |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 599 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 600 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 601 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 602 | bgp_getsockname (peer); |
| 603 | |
| 604 | if (BGP_DEBUG (normal, NORMAL)) |
| 605 | { |
| 606 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 607 | zlog_debug ("%s open active, local address %s", peer->host, |
| 608 | sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN)); |
| 609 | else |
| 610 | zlog_debug ("%s passive open", peer->host); |
| 611 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 612 | |
| 613 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 614 | bgp_open_send (peer); |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /* TCP connect fail */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 620 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 621 | bgp_connect_fail (struct peer *peer) |
| 622 | { |
| 623 | bgp_stop (peer); |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* This function is the first starting point of all BGP connection. It |
| 628 | try to connect to remote peer with non-blocking IO. */ |
| 629 | int |
| 630 | bgp_start (struct peer *peer) |
| 631 | { |
| 632 | int status; |
| 633 | |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 634 | if (BGP_PEER_START_SUPPRESSED (peer)) |
| 635 | { |
| 636 | if (BGP_DEBUG (fsm, FSM)) |
| 637 | plog_err (peer->log, "%s [FSM] Trying to start suppressed peer" |
| 638 | " - this is never supposed to happen!", peer->host); |
| 639 | return -1; |
| 640 | } |
| 641 | |
Paul Jakma | 33d5ab9 | 2006-07-02 11:01:50 +0000 | [diff] [blame] | 642 | /* Scrub some information that might be left over from a previous, |
| 643 | * session |
| 644 | */ |
| 645 | /* Connection information. */ |
| 646 | if (peer->su_local) |
| 647 | { |
| 648 | sockunion_free (peer->su_local); |
| 649 | peer->su_local = NULL; |
| 650 | } |
| 651 | |
| 652 | if (peer->su_remote) |
| 653 | { |
| 654 | sockunion_free (peer->su_remote); |
| 655 | peer->su_remote = NULL; |
| 656 | } |
| 657 | |
| 658 | /* Clear remote router-id. */ |
| 659 | peer->remote_id.s_addr = 0; |
| 660 | |
| 661 | /* Clear peer capability flag. */ |
| 662 | peer->cap = 0; |
| 663 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 664 | /* If the peer is passive mode, force to move to Active mode. */ |
| 665 | if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)) |
| 666 | { |
| 667 | BGP_EVENT_ADD (peer, TCP_connection_open_failed); |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | status = bgp_connect (peer); |
| 672 | |
| 673 | switch (status) |
| 674 | { |
| 675 | case connect_error: |
| 676 | if (BGP_DEBUG (fsm, FSM)) |
ajs | 8c2e200 | 2004-12-08 20:08:54 +0000 | [diff] [blame] | 677 | plog_debug (peer->log, "%s [FSM] Connect error", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 678 | BGP_EVENT_ADD (peer, TCP_connection_open_failed); |
| 679 | break; |
| 680 | case connect_success: |
| 681 | if (BGP_DEBUG (fsm, FSM)) |
ajs | 8c2e200 | 2004-12-08 20:08:54 +0000 | [diff] [blame] | 682 | plog_debug (peer->log, "%s [FSM] Connect immediately success", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 683 | peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 684 | BGP_EVENT_ADD (peer, TCP_connection_open); |
| 685 | break; |
| 686 | case connect_in_progress: |
| 687 | /* To check nonblocking connect, we wait until socket is |
| 688 | readable or writable. */ |
| 689 | if (BGP_DEBUG (fsm, FSM)) |
ajs | 8c2e200 | 2004-12-08 20:08:54 +0000 | [diff] [blame] | 690 | plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 691 | peer->host); |
| 692 | if (peer->fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 693 | { |
| 694 | zlog_err ("bgp_start peer's fd is negative value %d", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 695 | peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 696 | return -1; |
| 697 | } |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 698 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
| 699 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 700 | break; |
| 701 | } |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /* Connect retry timer is expired when the peer status is Connect. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 706 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 707 | bgp_reconnect (struct peer *peer) |
| 708 | { |
| 709 | bgp_stop (peer); |
| 710 | bgp_start (peer); |
| 711 | return 0; |
| 712 | } |
| 713 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 714 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 715 | bgp_fsm_open (struct peer *peer) |
| 716 | { |
| 717 | /* Send keepalive and make keepalive timer */ |
| 718 | bgp_keepalive_send (peer); |
| 719 | |
| 720 | /* Reset holdtimer value. */ |
| 721 | BGP_TIMER_OFF (peer->t_holdtime); |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 726 | /* Keepalive send to peer. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 727 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 728 | bgp_fsm_keepalive_expire (struct peer *peer) |
| 729 | { |
| 730 | bgp_keepalive_send (peer); |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | /* Hold timer expire. This is error of BGP connection. So cut the |
| 735 | peer and change to Idle status. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 736 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | bgp_fsm_holdtime_expire (struct peer *peer) |
| 738 | { |
| 739 | if (BGP_DEBUG (fsm, FSM)) |
| 740 | zlog (peer->log, LOG_DEBUG, "%s [FSM] Hold timer expire", peer->host); |
| 741 | |
| 742 | /* Send notify to remote peer. */ |
| 743 | bgp_notify_send (peer, BGP_NOTIFY_HOLD_ERR, 0); |
| 744 | |
| 745 | /* Sweep if it is temporary peer. */ |
| 746 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 747 | { |
| 748 | zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host); |
| 749 | peer_delete (peer); |
| 750 | return -1; |
| 751 | } |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | /* Status goes to Established. Send keepalive packet then make first |
| 757 | update information. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 758 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 759 | bgp_establish (struct peer *peer) |
| 760 | { |
| 761 | struct bgp_notify *notify; |
| 762 | afi_t afi; |
| 763 | safi_t safi; |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 764 | int nsf_af_count = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 765 | |
| 766 | /* Reset capability open status flag. */ |
| 767 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN)) |
| 768 | SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 769 | |
| 770 | /* Clear last notification data. */ |
| 771 | notify = &peer->notify; |
| 772 | if (notify->data) |
| 773 | XFREE (MTYPE_TMP, notify->data); |
| 774 | memset (notify, 0, sizeof (struct bgp_notify)); |
| 775 | |
| 776 | /* Clear start timer value to default. */ |
| 777 | peer->v_start = BGP_INIT_START_TIMER; |
| 778 | |
| 779 | /* Increment established count. */ |
| 780 | peer->established++; |
| 781 | bgp_fsm_change_status (peer, Established); |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 782 | |
| 783 | /* bgp log-neighbor-changes of neighbor Up */ |
| 784 | if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) |
| 785 | zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host); |
| 786 | |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 787 | /* graceful restart */ |
| 788 | UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT); |
| 789 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 790 | for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++) |
| 791 | { |
| 792 | if (peer->afc_nego[afi][safi] |
| 793 | && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV) |
| 794 | && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV)) |
| 795 | { |
| 796 | if (peer->nsf[afi][safi] |
| 797 | && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV)) |
| 798 | bgp_clear_stale_route (peer, afi, safi); |
| 799 | |
| 800 | peer->nsf[afi][safi] = 1; |
| 801 | nsf_af_count++; |
| 802 | } |
| 803 | else |
| 804 | { |
| 805 | if (peer->nsf[afi][safi]) |
| 806 | bgp_clear_stale_route (peer, afi, safi); |
| 807 | peer->nsf[afi][safi] = 0; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | if (nsf_af_count) |
| 812 | SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE); |
| 813 | else |
| 814 | { |
| 815 | UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE); |
| 816 | if (peer->t_gr_stale) |
| 817 | { |
| 818 | BGP_TIMER_OFF (peer->t_gr_stale); |
| 819 | if (BGP_DEBUG (events, EVENTS)) |
| 820 | zlog_debug ("%s graceful restart stalepath timer stopped", peer->host); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | if (peer->t_gr_restart) |
| 825 | { |
| 826 | BGP_TIMER_OFF (peer->t_gr_restart); |
| 827 | if (BGP_DEBUG (events, EVENTS)) |
| 828 | zlog_debug ("%s graceful restart timer stopped", peer->host); |
| 829 | } |
| 830 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 831 | #ifdef HAVE_SNMP |
| 832 | bgpTrapEstablished (peer); |
| 833 | #endif /* HAVE_SNMP */ |
| 834 | |
| 835 | /* Reset uptime, send keepalive, send current table. */ |
| 836 | bgp_uptime_reset (peer); |
| 837 | |
| 838 | /* Send route-refresh when ORF is enabled */ |
| 839 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 840 | for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) |
| 841 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV)) |
| 842 | { |
| 843 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV)) |
| 844 | bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX, |
| 845 | REFRESH_IMMEDIATE, 0); |
| 846 | else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV)) |
| 847 | bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD, |
| 848 | REFRESH_IMMEDIATE, 0); |
| 849 | } |
| 850 | |
| 851 | if (peer->v_keepalive) |
| 852 | bgp_keepalive_send (peer); |
| 853 | |
| 854 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ |
| 855 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 856 | for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) |
| 857 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)) |
| 858 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV) |
| 859 | || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV)) |
| 860 | SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH); |
| 861 | |
| 862 | bgp_announce_route_all (peer); |
| 863 | |
| 864 | BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1); |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | /* Keepalive packet is received. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 870 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 871 | bgp_fsm_keepalive (struct peer *peer) |
| 872 | { |
| 873 | /* peer count update */ |
| 874 | peer->keepalive_in++; |
| 875 | |
| 876 | BGP_TIMER_OFF (peer->t_holdtime); |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | /* Update packet is received. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 881 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 882 | bgp_fsm_update (struct peer *peer) |
| 883 | { |
| 884 | BGP_TIMER_OFF (peer->t_holdtime); |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | /* This is empty event. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 889 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 890 | bgp_ignore (struct peer *peer) |
| 891 | { |
| 892 | if (BGP_DEBUG (fsm, FSM)) |
| 893 | zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host); |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /* Finite State Machine structure */ |
| 898 | struct { |
| 899 | int (*func) (); |
| 900 | int next_state; |
| 901 | } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] = |
| 902 | { |
| 903 | { |
| 904 | /* Idle state: In Idle state, all events other than BGP_Start is |
| 905 | ignored. With BGP_Start event, finite state machine calls |
| 906 | bgp_start(). */ |
| 907 | {bgp_start, Connect}, /* BGP_Start */ |
| 908 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 909 | {bgp_stop, Idle}, /* TCP_connection_open */ |
| 910 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 911 | {bgp_ignore, Idle}, /* TCP_connection_open_failed */ |
| 912 | {bgp_stop, Idle}, /* TCP_fatal_error */ |
| 913 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ |
| 914 | {bgp_ignore, Idle}, /* Hold_Timer_expired */ |
| 915 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ |
| 916 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ |
| 917 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ |
| 918 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 919 | {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */ |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 920 | {bgp_ignore, Idle}, /* Clearing_Completed */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 921 | }, |
| 922 | { |
| 923 | /* Connect */ |
| 924 | {bgp_ignore, Connect}, /* BGP_Start */ |
| 925 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 926 | {bgp_connect_success, OpenSent}, /* TCP_connection_open */ |
| 927 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 928 | {bgp_connect_fail, Active}, /* TCP_connection_open_failed */ |
| 929 | {bgp_connect_fail, Idle}, /* TCP_fatal_error */ |
| 930 | {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */ |
| 931 | {bgp_ignore, Idle}, /* Hold_Timer_expired */ |
| 932 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ |
| 933 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ |
| 934 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ |
| 935 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 936 | {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */ |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 937 | {bgp_ignore, Idle}, /* Clearing_Completed */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 938 | }, |
| 939 | { |
| 940 | /* Active, */ |
| 941 | {bgp_ignore, Active}, /* BGP_Start */ |
| 942 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 943 | {bgp_connect_success, OpenSent}, /* TCP_connection_open */ |
| 944 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 945 | {bgp_ignore, Active}, /* TCP_connection_open_failed */ |
| 946 | {bgp_ignore, Idle}, /* TCP_fatal_error */ |
| 947 | {bgp_start, Connect}, /* ConnectRetry_timer_expired */ |
| 948 | {bgp_ignore, Idle}, /* Hold_Timer_expired */ |
| 949 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ |
| 950 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ |
| 951 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ |
| 952 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 953 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 954 | {bgp_ignore, Idle}, /* Clearing_Completed */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 955 | }, |
| 956 | { |
| 957 | /* OpenSent, */ |
| 958 | {bgp_ignore, OpenSent}, /* BGP_Start */ |
| 959 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 960 | {bgp_stop, Idle}, /* TCP_connection_open */ |
| 961 | {bgp_stop, Active}, /* TCP_connection_closed */ |
| 962 | {bgp_ignore, Idle}, /* TCP_connection_open_failed */ |
| 963 | {bgp_stop, Idle}, /* TCP_fatal_error */ |
| 964 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ |
| 965 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ |
| 966 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ |
| 967 | {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */ |
| 968 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ |
| 969 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 970 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 971 | {bgp_ignore, Idle}, /* Clearing_Completed */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 972 | }, |
| 973 | { |
| 974 | /* OpenConfirm, */ |
| 975 | {bgp_ignore, OpenConfirm}, /* BGP_Start */ |
| 976 | {bgp_stop, Idle}, /* BGP_Stop */ |
| 977 | {bgp_stop, Idle}, /* TCP_connection_open */ |
| 978 | {bgp_stop, Idle}, /* TCP_connection_closed */ |
| 979 | {bgp_stop, Idle}, /* TCP_connection_open_failed */ |
| 980 | {bgp_stop, Idle}, /* TCP_fatal_error */ |
| 981 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ |
| 982 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ |
| 983 | {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */ |
| 984 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ |
| 985 | {bgp_establish, Established}, /* Receive_KEEPALIVE_message */ |
| 986 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ |
| 987 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 988 | {bgp_ignore, Idle}, /* Clearing_Completed */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 989 | }, |
| 990 | { |
| 991 | /* Established, */ |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 992 | {bgp_ignore, Established}, /* BGP_Start */ |
| 993 | {bgp_stop, Clearing}, /* BGP_Stop */ |
| 994 | {bgp_stop, Clearing}, /* TCP_connection_open */ |
| 995 | {bgp_stop, Clearing}, /* TCP_connection_closed */ |
| 996 | {bgp_ignore, Clearing}, /* TCP_connection_open_failed */ |
| 997 | {bgp_stop, Clearing}, /* TCP_fatal_error */ |
| 998 | {bgp_ignore, Clearing}, /* ConnectRetry_timer_expired */ |
| 999 | {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1000 | {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */ |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 1001 | {bgp_stop, Clearing}, /* Receive_OPEN_message */ |
| 1002 | {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */ |
| 1003 | {bgp_fsm_update, Established}, /* Receive_UPDATE_message */ |
| 1004 | {bgp_stop_with_error, Clearing}, /* Receive_NOTIFICATION_message */ |
| 1005 | {bgp_ignore, Idle}, /* Clearing_Completed */ |
| 1006 | }, |
| 1007 | { |
| 1008 | /* Clearing, */ |
| 1009 | {bgp_ignore, Clearing}, /* BGP_Start */ |
| 1010 | {bgp_ignore, Clearing}, /* BGP_Stop */ |
| 1011 | {bgp_ignore, Clearing}, /* TCP_connection_open */ |
| 1012 | {bgp_ignore, Clearing}, /* TCP_connection_closed */ |
| 1013 | {bgp_ignore, Clearing}, /* TCP_connection_open_failed */ |
| 1014 | {bgp_ignore, Clearing}, /* TCP_fatal_error */ |
| 1015 | {bgp_ignore, Clearing}, /* ConnectRetry_timer_expired */ |
| 1016 | {bgp_ignore, Clearing}, /* Hold_Timer_expired */ |
| 1017 | {bgp_ignore, Clearing}, /* KeepAlive_timer_expired */ |
| 1018 | {bgp_ignore, Clearing}, /* Receive_OPEN_message */ |
| 1019 | {bgp_ignore, Clearing}, /* Receive_KEEPALIVE_message */ |
| 1020 | {bgp_ignore, Clearing}, /* Receive_UPDATE_message */ |
| 1021 | {bgp_ignore, Clearing}, /* Receive_NOTIFICATION_message */ |
| 1022 | {bgp_ignore, Idle }, /* Clearing_Completed */ |
| 1023 | }, |
| 1024 | { |
| 1025 | /* Deleted, */ |
| 1026 | {bgp_ignore, Deleted}, /* BGP_Start */ |
| 1027 | {bgp_ignore, Deleted}, /* BGP_Stop */ |
| 1028 | {bgp_ignore, Deleted}, /* TCP_connection_open */ |
| 1029 | {bgp_ignore, Deleted}, /* TCP_connection_closed */ |
| 1030 | {bgp_ignore, Deleted}, /* TCP_connection_open_failed */ |
| 1031 | {bgp_ignore, Deleted}, /* TCP_fatal_error */ |
| 1032 | {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */ |
| 1033 | {bgp_ignore, Deleted}, /* Hold_Timer_expired */ |
| 1034 | {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */ |
| 1035 | {bgp_ignore, Deleted}, /* Receive_OPEN_message */ |
| 1036 | {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */ |
| 1037 | {bgp_ignore, Deleted}, /* Receive_UPDATE_message */ |
| 1038 | {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */ |
| 1039 | {bgp_ignore, Deleted}, /* Clearing_Completed */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1040 | }, |
| 1041 | }; |
| 1042 | |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1043 | static const char *bgp_event_str[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1044 | { |
| 1045 | NULL, |
| 1046 | "BGP_Start", |
| 1047 | "BGP_Stop", |
| 1048 | "TCP_connection_open", |
| 1049 | "TCP_connection_closed", |
| 1050 | "TCP_connection_open_failed", |
| 1051 | "TCP_fatal_error", |
| 1052 | "ConnectRetry_timer_expired", |
| 1053 | "Hold_Timer_expired", |
| 1054 | "KeepAlive_timer_expired", |
| 1055 | "Receive_OPEN_message", |
| 1056 | "Receive_KEEPALIVE_message", |
| 1057 | "Receive_UPDATE_message", |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 1058 | "Receive_NOTIFICATION_message", |
| 1059 | "Clearing_Completed", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1060 | }; |
| 1061 | |
| 1062 | /* Execute event process. */ |
| 1063 | int |
| 1064 | bgp_event (struct thread *thread) |
| 1065 | { |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 1066 | int ret = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1067 | int event; |
| 1068 | int next; |
| 1069 | struct peer *peer; |
| 1070 | |
| 1071 | peer = THREAD_ARG (thread); |
| 1072 | event = THREAD_VAL (thread); |
| 1073 | |
| 1074 | /* Logging this event. */ |
| 1075 | next = FSM [peer->status -1][event - 1].next_state; |
| 1076 | |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 1077 | if (BGP_DEBUG (fsm, FSM) && peer->status != next) |
ajs | 8c2e200 | 2004-12-08 20:08:54 +0000 | [diff] [blame] | 1078 | plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1079 | bgp_event_str[event], |
| 1080 | LOOKUP (bgp_status_msg, peer->status), |
| 1081 | LOOKUP (bgp_status_msg, next)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1082 | |
| 1083 | /* Call function. */ |
Paul Jakma | ca058a3 | 2006-09-14 02:58:49 +0000 | [diff] [blame] | 1084 | if (FSM [peer->status -1][event - 1].func) |
| 1085 | ret = (*(FSM [peer->status - 1][event - 1].func))(peer); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1086 | |
| 1087 | /* When function do not want proceed next job return -1. */ |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 1088 | if (ret >= 0) |
| 1089 | { |
| 1090 | /* If status is changed. */ |
| 1091 | if (next != peer->status) |
Paul Jakma | 95fdcd8 | 2006-12-08 00:31:22 +0000 | [diff] [blame] | 1092 | { |
| 1093 | /* Transition into Clearing must /always/ clear all routes.. */ |
| 1094 | if (next == Clearing) |
| 1095 | bgp_clear_route_all (peer); |
| 1096 | |
| 1097 | bgp_fsm_change_status (peer, next); |
| 1098 | } |
| 1099 | |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 1100 | /* Make sure timer is set. */ |
| 1101 | bgp_timer_set (peer); |
| 1102 | } |
| 1103 | |
paul | 200df11 | 2005-06-01 11:17:05 +0000 | [diff] [blame] | 1104 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1105 | } |