]>
Commit | Line | Data |
---|---|---|
d62a17ae | 1 | /* BGP-4 Finite State Machine |
896014f4 DL |
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 along | |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
718e3744 | 21 | |
22 | #include <zebra.h> | |
23 | ||
24 | #include "linklist.h" | |
25 | #include "prefix.h" | |
718e3744 | 26 | #include "sockunion.h" |
27 | #include "thread.h" | |
28 | #include "log.h" | |
29 | #include "stream.h" | |
74ffbfe6 | 30 | #include "ringbuf.h" |
718e3744 | 31 | #include "memory.h" |
32 | #include "plist.h" | |
f188f2c4 | 33 | #include "workqueue.h" |
3f9c7369 | 34 | #include "queue.h" |
039f3a34 | 35 | #include "filter.h" |
4dcadbef | 36 | #include "command.h" |
718e3744 | 37 | |
856ca177 | 38 | #include "lib/json.h" |
718e3744 | 39 | #include "bgpd/bgpd.h" |
40 | #include "bgpd/bgp_attr.h" | |
41 | #include "bgpd/bgp_debug.h" | |
42 | #include "bgpd/bgp_fsm.h" | |
43 | #include "bgpd/bgp_packet.h" | |
44 | #include "bgpd/bgp_network.h" | |
45 | #include "bgpd/bgp_route.h" | |
46 | #include "bgpd/bgp_dump.h" | |
47 | #include "bgpd/bgp_open.h" | |
f188f2c4 | 48 | #include "bgpd/bgp_advertise.h" |
3f9c7369 | 49 | #include "bgpd/bgp_updgrp.h" |
ffd0c037 | 50 | #include "bgpd/bgp_nht.h" |
c43ed2e4 | 51 | #include "bgpd/bgp_bfd.h" |
4a1ab8e4 | 52 | #include "bgpd/bgp_memory.h" |
03014d48 | 53 | #include "bgpd/bgp_keepalives.h" |
56257a44 | 54 | #include "bgpd/bgp_io.h" |
6b0655a2 | 55 | |
d62a17ae | 56 | DEFINE_HOOK(peer_backward_transition, (struct peer * peer), (peer)) |
57 | DEFINE_HOOK(peer_established, (struct peer * peer), (peer)) | |
3012671f | 58 | |
6403814c DS |
59 | /* Definition of display strings corresponding to FSM events. This should be |
60 | * kept consistent with the events defined in bgpd.h | |
61 | */ | |
d62a17ae | 62 | static const char *bgp_event_str[] = { |
63 | NULL, | |
64 | "BGP_Start", | |
65 | "BGP_Stop", | |
66 | "TCP_connection_open", | |
67 | "TCP_connection_closed", | |
68 | "TCP_connection_open_failed", | |
69 | "TCP_fatal_error", | |
70 | "ConnectRetry_timer_expired", | |
71 | "Hold_Timer_expired", | |
72 | "KeepAlive_timer_expired", | |
73 | "Receive_OPEN_message", | |
74 | "Receive_KEEPALIVE_message", | |
75 | "Receive_UPDATE_message", | |
76 | "Receive_NOTIFICATION_message", | |
77 | "Clearing_Completed", | |
6403814c DS |
78 | }; |
79 | ||
718e3744 | 80 | /* BGP FSM (finite state machine) has three types of functions. Type |
81 | one is thread functions. Type two is event functions. Type three | |
82 | is FSM functions. Timer functions are set by bgp_timer_set | |
83 | function. */ | |
84 | ||
85 | /* BGP event function. */ | |
d62a17ae | 86 | int bgp_event(struct thread *); |
718e3744 | 87 | |
88 | /* BGP thread functions. */ | |
d62a17ae | 89 | static int bgp_start_timer(struct thread *); |
90 | static int bgp_connect_timer(struct thread *); | |
91 | static int bgp_holdtime_timer(struct thread *); | |
718e3744 | 92 | |
93 | /* BGP FSM functions. */ | |
d62a17ae | 94 | static int bgp_start(struct peer *); |
718e3744 | 95 | |
d62a17ae | 96 | static void peer_xfer_stats(struct peer *peer_dst, struct peer *peer_src) |
1ff9a340 | 97 | { |
d62a17ae | 98 | /* Copy stats over. These are only the pre-established state stats */ |
99 | peer_dst->open_in += peer_src->open_in; | |
100 | peer_dst->open_out += peer_src->open_out; | |
101 | peer_dst->keepalive_in += peer_src->keepalive_in; | |
102 | peer_dst->keepalive_out += peer_src->keepalive_out; | |
103 | peer_dst->notify_in += peer_src->notify_in; | |
104 | peer_dst->notify_out += peer_src->notify_out; | |
105 | peer_dst->dynamic_cap_in += peer_src->dynamic_cap_in; | |
106 | peer_dst->dynamic_cap_out += peer_src->dynamic_cap_out; | |
1ff9a340 DS |
107 | } |
108 | ||
d62a17ae | 109 | static struct peer *peer_xfer_conn(struct peer *from_peer) |
1ff9a340 | 110 | { |
d62a17ae | 111 | struct peer *peer; |
112 | afi_t afi; | |
113 | safi_t safi; | |
114 | int fd; | |
115 | int status, pstatus; | |
116 | unsigned char last_evt, last_maj_evt; | |
117 | ||
118 | assert(from_peer != NULL); | |
119 | ||
120 | peer = from_peer->doppelganger; | |
121 | ||
122 | if (!peer || !CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)) | |
123 | return from_peer; | |
124 | ||
125 | if (bgp_debug_neighbor_events(peer)) | |
126 | zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)", | |
127 | from_peer->host, from_peer, from_peer->fd, peer, | |
128 | peer->fd); | |
129 | ||
424ab01d QY |
130 | bgp_writes_off(peer); |
131 | bgp_reads_off(peer); | |
132 | bgp_writes_off(from_peer); | |
133 | bgp_reads_off(from_peer); | |
d62a17ae | 134 | |
135 | BGP_TIMER_OFF(peer->t_routeadv); | |
424ab01d | 136 | BGP_TIMER_OFF(peer->t_connect); |
387f984e QY |
137 | BGP_TIMER_OFF(peer->t_connect_check_r); |
138 | BGP_TIMER_OFF(peer->t_connect_check_w); | |
d62a17ae | 139 | BGP_TIMER_OFF(from_peer->t_routeadv); |
424ab01d | 140 | BGP_TIMER_OFF(from_peer->t_connect); |
387f984e QY |
141 | BGP_TIMER_OFF(from_peer->t_connect_check_r); |
142 | BGP_TIMER_OFF(from_peer->t_connect_check_w); | |
7a86aa5a | 143 | BGP_TIMER_OFF(from_peer->t_process_packet); |
d3ecc69e | 144 | |
becedef6 QY |
145 | /* |
146 | * At this point in time, it is possible that there are packets pending | |
147 | * on various buffers. Those need to be transferred or dropped, | |
148 | * otherwise we'll get spurious failures during session establishment. | |
149 | */ | |
424ab01d QY |
150 | pthread_mutex_lock(&peer->io_mtx); |
151 | pthread_mutex_lock(&from_peer->io_mtx); | |
d3ecc69e | 152 | { |
424ab01d QY |
153 | fd = peer->fd; |
154 | peer->fd = from_peer->fd; | |
155 | from_peer->fd = fd; | |
156 | ||
157 | stream_fifo_clean(peer->ibuf); | |
d3ecc69e | 158 | stream_fifo_clean(peer->obuf); |
d3ecc69e | 159 | |
becedef6 QY |
160 | /* |
161 | * this should never happen, since bgp_process_packet() is the | |
162 | * only task that sets and unsets the current packet and it | |
163 | * runs in our pthread. | |
164 | */ | |
424ab01d QY |
165 | if (peer->curr) { |
166 | zlog_err( | |
167 | "[%s] Dropping pending packet on connection transfer:", | |
168 | peer->host); | |
169 | u_int16_t type = stream_getc_from(peer->curr, | |
170 | BGP_MARKER_SIZE + 2); | |
171 | bgp_dump_packet(peer, type, peer->curr); | |
172 | stream_free(peer->curr); | |
173 | peer->curr = NULL; | |
174 | } | |
175 | ||
176 | // copy each packet from old peer's output queue to new peer | |
727c4f87 QY |
177 | while (from_peer->obuf->head) |
178 | stream_fifo_push(peer->obuf, | |
179 | stream_fifo_pop(from_peer->obuf)); | |
424ab01d QY |
180 | |
181 | // copy each packet from old peer's input queue to new peer | |
182 | while (from_peer->ibuf->head) | |
183 | stream_fifo_push(peer->ibuf, | |
184 | stream_fifo_pop(from_peer->ibuf)); | |
7db44ec8 | 185 | |
74ffbfe6 QY |
186 | ringbuf_wipe(peer->ibuf_work); |
187 | ringbuf_copy(peer->ibuf_work, from_peer->ibuf_work, | |
188 | ringbuf_remain(from_peer->ibuf_work)); | |
d3ecc69e | 189 | } |
424ab01d QY |
190 | pthread_mutex_unlock(&from_peer->io_mtx); |
191 | pthread_mutex_unlock(&peer->io_mtx); | |
d62a17ae | 192 | |
193 | peer->as = from_peer->as; | |
194 | peer->v_holdtime = from_peer->v_holdtime; | |
195 | peer->v_keepalive = from_peer->v_keepalive; | |
196 | peer->routeadv = from_peer->routeadv; | |
197 | peer->v_routeadv = from_peer->v_routeadv; | |
198 | peer->v_gr_restart = from_peer->v_gr_restart; | |
199 | peer->cap = from_peer->cap; | |
200 | status = peer->status; | |
201 | pstatus = peer->ostatus; | |
202 | last_evt = peer->last_event; | |
203 | last_maj_evt = peer->last_major_event; | |
204 | peer->status = from_peer->status; | |
205 | peer->ostatus = from_peer->ostatus; | |
206 | peer->last_event = from_peer->last_event; | |
207 | peer->last_major_event = from_peer->last_major_event; | |
208 | from_peer->status = status; | |
209 | from_peer->ostatus = pstatus; | |
210 | from_peer->last_event = last_evt; | |
211 | from_peer->last_major_event = last_maj_evt; | |
212 | peer->remote_id = from_peer->remote_id; | |
213 | ||
214 | if (from_peer->hostname != NULL) { | |
215 | if (peer->hostname) { | |
216 | XFREE(MTYPE_BGP_PEER_HOST, peer->hostname); | |
217 | peer->hostname = NULL; | |
218 | } | |
219 | ||
220 | peer->hostname = from_peer->hostname; | |
221 | from_peer->hostname = NULL; | |
222 | } | |
223 | ||
224 | if (from_peer->domainname != NULL) { | |
225 | if (peer->domainname) { | |
226 | XFREE(MTYPE_BGP_PEER_HOST, peer->domainname); | |
227 | peer->domainname = NULL; | |
228 | } | |
229 | ||
230 | peer->domainname = from_peer->domainname; | |
231 | from_peer->domainname = NULL; | |
232 | } | |
233 | ||
05c7a1cc QY |
234 | FOREACH_AFI_SAFI (afi, safi) { |
235 | peer->af_flags[afi][safi] = from_peer->af_flags[afi][safi]; | |
236 | peer->af_sflags[afi][safi] = from_peer->af_sflags[afi][safi]; | |
237 | peer->af_cap[afi][safi] = from_peer->af_cap[afi][safi]; | |
238 | peer->afc_nego[afi][safi] = from_peer->afc_nego[afi][safi]; | |
239 | peer->afc_adv[afi][safi] = from_peer->afc_adv[afi][safi]; | |
240 | peer->afc_recv[afi][safi] = from_peer->afc_recv[afi][safi]; | |
241 | peer->orf_plist[afi][safi] = from_peer->orf_plist[afi][safi]; | |
242 | } | |
d62a17ae | 243 | |
244 | if (bgp_getsockname(peer) < 0) { | |
245 | zlog_err( | |
246 | "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)", | |
247 | (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER) | |
248 | ? "accept" | |
249 | : ""), | |
250 | peer->host, peer->fd, from_peer->fd); | |
251 | bgp_stop(peer); | |
252 | bgp_stop(from_peer); | |
253 | return NULL; | |
254 | } | |
255 | if (from_peer->status > Active) { | |
256 | if (bgp_getsockname(from_peer) < 0) { | |
257 | zlog_err( | |
258 | "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)", | |
259 | (CHECK_FLAG(from_peer->sflags, | |
260 | PEER_STATUS_ACCEPT_PEER) | |
261 | ? "accept" | |
262 | : ""), | |
263 | from_peer->host, from_peer->fd, peer->fd); | |
264 | bgp_stop(from_peer); | |
265 | from_peer = NULL; | |
266 | } | |
267 | } | |
268 | ||
0112e9e0 QY |
269 | |
270 | // Note: peer_xfer_stats() must be called with I/O turned OFF | |
271 | if (from_peer) | |
272 | peer_xfer_stats(peer, from_peer); | |
273 | ||
424ab01d QY |
274 | bgp_reads_on(peer); |
275 | bgp_writes_on(peer); | |
7a86aa5a QY |
276 | thread_add_timer_msec(bm->master, bgp_process_packet, peer, 0, |
277 | &peer->t_process_packet); | |
d62a17ae | 278 | |
d62a17ae | 279 | return (peer); |
1ff9a340 DS |
280 | } |
281 | ||
718e3744 | 282 | /* Hook function called after bgp event is occered. And vty's |
283 | neighbor command invoke this function after making neighbor | |
284 | structure. */ | |
d62a17ae | 285 | void bgp_timer_set(struct peer *peer) |
718e3744 | 286 | { |
d62a17ae | 287 | switch (peer->status) { |
288 | case Idle: | |
289 | /* First entry point of peer's finite state machine. In Idle | |
290 | status start timer is on unless peer is shutdown or peer is | |
291 | inactive. All other timer must be turned off */ | |
292 | if (BGP_PEER_START_SUPPRESSED(peer) || !peer_active(peer)) { | |
293 | BGP_TIMER_OFF(peer->t_start); | |
294 | } else { | |
295 | BGP_TIMER_ON(peer->t_start, bgp_start_timer, | |
296 | peer->v_start); | |
297 | } | |
298 | BGP_TIMER_OFF(peer->t_connect); | |
299 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 300 | bgp_keepalives_off(peer); |
d62a17ae | 301 | BGP_TIMER_OFF(peer->t_routeadv); |
302 | break; | |
303 | ||
304 | case Connect: | |
305 | /* After start timer is expired, the peer moves to Connect | |
306 | status. Make sure start timer is off and connect timer is | |
307 | on. */ | |
308 | BGP_TIMER_OFF(peer->t_start); | |
309 | BGP_TIMER_ON(peer->t_connect, bgp_connect_timer, | |
310 | peer->v_connect); | |
311 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 312 | bgp_keepalives_off(peer); |
d62a17ae | 313 | BGP_TIMER_OFF(peer->t_routeadv); |
314 | break; | |
315 | ||
316 | case Active: | |
317 | /* Active is waiting connection from remote peer. And if | |
318 | connect timer is expired, change status to Connect. */ | |
319 | BGP_TIMER_OFF(peer->t_start); | |
320 | /* If peer is passive mode, do not set connect timer. */ | |
321 | if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE) | |
322 | || CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) { | |
323 | BGP_TIMER_OFF(peer->t_connect); | |
324 | } else { | |
325 | BGP_TIMER_ON(peer->t_connect, bgp_connect_timer, | |
326 | peer->v_connect); | |
327 | } | |
328 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 329 | bgp_keepalives_off(peer); |
d62a17ae | 330 | BGP_TIMER_OFF(peer->t_routeadv); |
331 | break; | |
332 | ||
333 | case OpenSent: | |
334 | /* OpenSent status. */ | |
335 | BGP_TIMER_OFF(peer->t_start); | |
336 | BGP_TIMER_OFF(peer->t_connect); | |
337 | if (peer->v_holdtime != 0) { | |
338 | BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer, | |
339 | peer->v_holdtime); | |
340 | } else { | |
341 | BGP_TIMER_OFF(peer->t_holdtime); | |
342 | } | |
b72b6f4f | 343 | bgp_keepalives_off(peer); |
d62a17ae | 344 | BGP_TIMER_OFF(peer->t_routeadv); |
345 | break; | |
346 | ||
347 | case OpenConfirm: | |
348 | /* OpenConfirm status. */ | |
349 | BGP_TIMER_OFF(peer->t_start); | |
350 | BGP_TIMER_OFF(peer->t_connect); | |
351 | ||
352 | /* If the negotiated Hold Time value is zero, then the Hold Time | |
353 | timer and KeepAlive timers are not started. */ | |
354 | if (peer->v_holdtime == 0) { | |
355 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 356 | bgp_keepalives_off(peer); |
d62a17ae | 357 | } else { |
358 | BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer, | |
359 | peer->v_holdtime); | |
b72b6f4f | 360 | bgp_keepalives_on(peer); |
d62a17ae | 361 | } |
362 | BGP_TIMER_OFF(peer->t_routeadv); | |
363 | break; | |
364 | ||
365 | case Established: | |
366 | /* In Established status start and connect timer is turned | |
367 | off. */ | |
368 | BGP_TIMER_OFF(peer->t_start); | |
369 | BGP_TIMER_OFF(peer->t_connect); | |
370 | ||
371 | /* Same as OpenConfirm, if holdtime is zero then both holdtime | |
372 | and keepalive must be turned off. */ | |
373 | if (peer->v_holdtime == 0) { | |
374 | BGP_TIMER_OFF(peer->t_holdtime); | |
bea01226 | 375 | bgp_keepalives_off(peer); |
d62a17ae | 376 | } else { |
377 | BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer, | |
378 | peer->v_holdtime); | |
bea01226 | 379 | bgp_keepalives_on(peer); |
d62a17ae | 380 | } |
381 | break; | |
382 | case Deleted: | |
383 | BGP_TIMER_OFF(peer->t_gr_restart); | |
384 | BGP_TIMER_OFF(peer->t_gr_stale); | |
385 | BGP_TIMER_OFF(peer->t_pmax_restart); | |
386 | /* fallthru */ | |
387 | case Clearing: | |
388 | BGP_TIMER_OFF(peer->t_start); | |
389 | BGP_TIMER_OFF(peer->t_connect); | |
390 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 391 | bgp_keepalives_off(peer); |
d62a17ae | 392 | BGP_TIMER_OFF(peer->t_routeadv); |
393 | break; | |
718e3744 | 394 | } |
718e3744 | 395 | } |
396 | ||
397 | /* BGP start timer. This function set BGP_Start event to thread value | |
398 | and process event. */ | |
d62a17ae | 399 | static int bgp_start_timer(struct thread *thread) |
718e3744 | 400 | { |
d62a17ae | 401 | struct peer *peer; |
718e3744 | 402 | |
d62a17ae | 403 | peer = THREAD_ARG(thread); |
404 | peer->t_start = NULL; | |
718e3744 | 405 | |
d62a17ae | 406 | if (bgp_debug_neighbor_events(peer)) |
407 | zlog_debug("%s [FSM] Timer (start timer expire).", peer->host); | |
718e3744 | 408 | |
d62a17ae | 409 | THREAD_VAL(thread) = BGP_Start; |
410 | bgp_event(thread); /* bgp_event unlocks peer */ | |
718e3744 | 411 | |
d62a17ae | 412 | return 0; |
718e3744 | 413 | } |
414 | ||
415 | /* BGP connect retry timer. */ | |
d62a17ae | 416 | static int bgp_connect_timer(struct thread *thread) |
718e3744 | 417 | { |
d62a17ae | 418 | struct peer *peer; |
419 | int ret; | |
420 | ||
421 | peer = THREAD_ARG(thread); | |
424ab01d QY |
422 | |
423 | assert(!peer->t_write); | |
424 | assert(!peer->t_read); | |
425 | ||
d62a17ae | 426 | peer->t_connect = NULL; |
427 | ||
428 | if (bgp_debug_neighbor_events(peer)) | |
429 | zlog_debug("%s [FSM] Timer (connect timer expire)", peer->host); | |
430 | ||
431 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) { | |
432 | bgp_stop(peer); | |
433 | ret = -1; | |
434 | } else { | |
435 | THREAD_VAL(thread) = ConnectRetry_timer_expired; | |
436 | bgp_event(thread); /* bgp_event unlocks peer */ | |
437 | ret = 0; | |
438 | } | |
439 | ||
440 | return ret; | |
718e3744 | 441 | } |
442 | ||
443 | /* BGP holdtime timer. */ | |
d62a17ae | 444 | static int bgp_holdtime_timer(struct thread *thread) |
718e3744 | 445 | { |
d62a17ae | 446 | struct peer *peer; |
718e3744 | 447 | |
d62a17ae | 448 | peer = THREAD_ARG(thread); |
449 | peer->t_holdtime = NULL; | |
718e3744 | 450 | |
d62a17ae | 451 | if (bgp_debug_neighbor_events(peer)) |
452 | zlog_debug("%s [FSM] Timer (holdtime timer expire)", | |
453 | peer->host); | |
718e3744 | 454 | |
d62a17ae | 455 | THREAD_VAL(thread) = Hold_Timer_expired; |
456 | bgp_event(thread); /* bgp_event unlocks peer */ | |
718e3744 | 457 | |
d62a17ae | 458 | return 0; |
718e3744 | 459 | } |
460 | ||
d62a17ae | 461 | int bgp_routeadv_timer(struct thread *thread) |
718e3744 | 462 | { |
d62a17ae | 463 | struct peer *peer; |
718e3744 | 464 | |
d62a17ae | 465 | peer = THREAD_ARG(thread); |
466 | peer->t_routeadv = NULL; | |
718e3744 | 467 | |
d62a17ae | 468 | if (bgp_debug_neighbor_events(peer)) |
469 | zlog_debug("%s [FSM] Timer (routeadv timer expire)", | |
470 | peer->host); | |
718e3744 | 471 | |
d62a17ae | 472 | peer->synctime = bgp_clock(); |
718e3744 | 473 | |
a9794991 | 474 | thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets, peer, 0, |
424ab01d QY |
475 | &peer->t_generate_updgrp_packets); |
476 | ||
d62a17ae | 477 | /* MRAI timer will be started again when FIFO is built, no need to |
478 | * do it here. | |
479 | */ | |
480 | return 0; | |
718e3744 | 481 | } |
482 | ||
e0701b79 | 483 | /* BGP Peer Down Cause */ |
d62a17ae | 484 | const char *peer_down_str[] = {"", |
485 | "Router ID changed", | |
486 | "Remote AS changed", | |
487 | "Local AS change", | |
488 | "Cluster ID changed", | |
489 | "Confederation identifier changed", | |
490 | "Confederation peer changed", | |
491 | "RR client config change", | |
492 | "RS client config change", | |
493 | "Update source change", | |
494 | "Address family activated", | |
495 | "Admin. shutdown", | |
496 | "User reset", | |
497 | "BGP Notification received", | |
498 | "BGP Notification send", | |
499 | "Peer closed the session", | |
500 | "Neighbor deleted", | |
501 | "Peer-group add member", | |
502 | "Peer-group delete member", | |
503 | "Capability changed", | |
504 | "Passive config change", | |
505 | "Multihop config change", | |
506 | "NSF peer closed the session", | |
507 | "Intf peering v6only config change", | |
508 | "BFD down received", | |
509 | "Interface down", | |
510 | "Neighbor address lost"}; | |
511 | ||
512 | static int bgp_graceful_restart_timer_expire(struct thread *thread) | |
e0701b79 | 513 | { |
d62a17ae | 514 | struct peer *peer; |
515 | afi_t afi; | |
516 | safi_t safi; | |
517 | ||
518 | peer = THREAD_ARG(thread); | |
519 | peer->t_gr_restart = NULL; | |
520 | ||
521 | /* NSF delete stale route */ | |
522 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
a08ca0a7 | 523 | for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++) |
d62a17ae | 524 | if (peer->nsf[afi][safi]) |
525 | bgp_clear_stale_route(peer, afi, safi); | |
526 | ||
527 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT); | |
528 | BGP_TIMER_OFF(peer->t_gr_stale); | |
529 | ||
530 | if (bgp_debug_neighbor_events(peer)) { | |
531 | zlog_debug("%s graceful restart timer expired", peer->host); | |
532 | zlog_debug("%s graceful restart stalepath timer stopped", | |
533 | peer->host); | |
534 | } | |
93406d87 | 535 | |
d62a17ae | 536 | bgp_timer_set(peer); |
93406d87 | 537 | |
d62a17ae | 538 | return 0; |
93406d87 | 539 | } |
540 | ||
d62a17ae | 541 | static int bgp_graceful_stale_timer_expire(struct thread *thread) |
93406d87 | 542 | { |
d62a17ae | 543 | struct peer *peer; |
544 | afi_t afi; | |
545 | safi_t safi; | |
93406d87 | 546 | |
d62a17ae | 547 | peer = THREAD_ARG(thread); |
548 | peer->t_gr_stale = NULL; | |
93406d87 | 549 | |
d62a17ae | 550 | if (bgp_debug_neighbor_events(peer)) |
551 | zlog_debug("%s graceful restart stalepath timer expired", | |
552 | peer->host); | |
93406d87 | 553 | |
d62a17ae | 554 | /* NSF delete stale route */ |
555 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
a08ca0a7 | 556 | for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++) |
d62a17ae | 557 | if (peer->nsf[afi][safi]) |
558 | bgp_clear_stale_route(peer, afi, safi); | |
93406d87 | 559 | |
d62a17ae | 560 | return 0; |
93406d87 | 561 | } |
562 | ||
d62a17ae | 563 | static int bgp_update_delay_applicable(struct bgp *bgp) |
f188f2c4 | 564 | { |
d62a17ae | 565 | /* update_delay_over flag should be reset (set to 0) for any new |
566 | applicability of the update-delay during BGP process lifetime. | |
567 | And it should be set after an occurence of the update-delay is | |
568 | over)*/ | |
569 | if (!bgp->update_delay_over) | |
570 | return 1; | |
571 | ||
572 | return 0; | |
f188f2c4 DS |
573 | } |
574 | ||
d62a17ae | 575 | int bgp_update_delay_active(struct bgp *bgp) |
f188f2c4 | 576 | { |
d62a17ae | 577 | if (bgp->t_update_delay) |
578 | return 1; | |
f188f2c4 | 579 | |
d62a17ae | 580 | return 0; |
f188f2c4 DS |
581 | } |
582 | ||
d62a17ae | 583 | int bgp_update_delay_configured(struct bgp *bgp) |
f188f2c4 | 584 | { |
d62a17ae | 585 | if (bgp->v_update_delay) |
586 | return 1; | |
f188f2c4 | 587 | |
d62a17ae | 588 | return 0; |
f188f2c4 DS |
589 | } |
590 | ||
591 | /* Do the post-processing needed when bgp comes out of the read-only mode | |
592 | on ending the update delay. */ | |
d62a17ae | 593 | void bgp_update_delay_end(struct bgp *bgp) |
f188f2c4 | 594 | { |
d62a17ae | 595 | THREAD_TIMER_OFF(bgp->t_update_delay); |
596 | THREAD_TIMER_OFF(bgp->t_establish_wait); | |
597 | ||
598 | /* Reset update-delay related state */ | |
599 | bgp->update_delay_over = 1; | |
600 | bgp->established = 0; | |
601 | bgp->restarted_peers = 0; | |
602 | bgp->implicit_eors = 0; | |
603 | bgp->explicit_eors = 0; | |
604 | ||
605 | quagga_timestamp(3, bgp->update_delay_end_time, | |
606 | sizeof(bgp->update_delay_end_time)); | |
607 | ||
608 | /* | |
609 | * Add an end-of-initial-update marker to the main process queues so | |
610 | * that | |
611 | * the route advertisement timer for the peers can be started. Also set | |
612 | * the zebra and peer update hold flags. These flags are used to achieve | |
613 | * three stages in the update-delay post processing: | |
614 | * 1. Finish best-path selection for all the prefixes held on the | |
615 | * queues. | |
616 | * (routes in BGP are updated, and peers sync queues are populated | |
617 | * too) | |
618 | * 2. As the eoiu mark is reached in the bgp process routine, ship all | |
619 | * the | |
620 | * routes to zebra. With that zebra should see updates from BGP | |
621 | * close | |
622 | * to each other. | |
623 | * 3. Unblock the peer update writes. With that peer update packing | |
624 | * with | |
625 | * the prefixes should be at its maximum. | |
626 | */ | |
627 | bgp_add_eoiu_mark(bgp); | |
628 | bgp->main_zebra_update_hold = 1; | |
629 | bgp->main_peers_update_hold = 1; | |
630 | ||
631 | /* Resume the queue processing. This should trigger the event that would | |
632 | take | |
633 | care of processing any work that was queued during the read-only | |
634 | mode. */ | |
635 | work_queue_unplug(bm->process_main_queue); | |
f188f2c4 DS |
636 | } |
637 | ||
cb1faec9 DS |
638 | /** |
639 | * see bgp_fsm.h | |
640 | */ | |
d62a17ae | 641 | void bgp_start_routeadv(struct bgp *bgp) |
cb1faec9 | 642 | { |
d62a17ae | 643 | struct listnode *node, *nnode; |
644 | struct peer *peer; | |
cb1faec9 | 645 | |
d62a17ae | 646 | zlog_info("bgp_start_routeadv(), update hold status %d", |
647 | bgp->main_peers_update_hold); | |
4a16ae86 | 648 | |
d62a17ae | 649 | if (bgp->main_peers_update_hold) |
650 | return; | |
4a16ae86 | 651 | |
d62a17ae | 652 | quagga_timestamp(3, bgp->update_delay_peers_resume_time, |
653 | sizeof(bgp->update_delay_peers_resume_time)); | |
4a16ae86 | 654 | |
d62a17ae | 655 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { |
656 | if (peer->status != Established) | |
657 | continue; | |
658 | BGP_TIMER_OFF(peer->t_routeadv); | |
659 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0); | |
660 | } | |
cb1faec9 DS |
661 | } |
662 | ||
663 | /** | |
664 | * see bgp_fsm.h | |
665 | */ | |
d62a17ae | 666 | void bgp_adjust_routeadv(struct peer *peer) |
cb1faec9 | 667 | { |
d62a17ae | 668 | time_t nowtime = bgp_clock(); |
669 | double diff; | |
670 | unsigned long remain; | |
671 | ||
672 | /* Bypass checks for special case of MRAI being 0 */ | |
673 | if (peer->v_routeadv == 0) { | |
674 | /* Stop existing timer, just in case it is running for a | |
675 | * different | |
676 | * duration and schedule write thread immediately. | |
677 | */ | |
678 | if (peer->t_routeadv) | |
679 | BGP_TIMER_OFF(peer->t_routeadv); | |
680 | ||
681 | peer->synctime = bgp_clock(); | |
a9794991 | 682 | thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets, |
424ab01d QY |
683 | peer, 0, |
684 | &peer->t_generate_updgrp_packets); | |
d62a17ae | 685 | return; |
686 | } | |
687 | ||
688 | ||
689 | /* | |
690 | * CASE I: | |
691 | * If the last update was written more than MRAI back, expire the timer | |
692 | * instantly so that we can send the update out sooner. | |
693 | * | |
694 | * <------- MRAI ---------> | |
695 | * |-----------------|-----------------------| | |
696 | * <------------- m ------------> | |
697 | * ^ ^ ^ | |
698 | * | | | | |
699 | * | | current time | |
700 | * | timer start | |
701 | * last write | |
702 | * | |
703 | * m > MRAI | |
704 | */ | |
705 | diff = difftime(nowtime, peer->last_update); | |
706 | if (diff > (double)peer->v_routeadv) { | |
707 | BGP_TIMER_OFF(peer->t_routeadv); | |
708 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0); | |
709 | return; | |
710 | } | |
711 | ||
712 | /* | |
713 | * CASE II: | |
714 | * - Find when to expire the MRAI timer. | |
715 | * If MRAI timer is not active, assume we can start it now. | |
716 | * | |
717 | * <------- MRAI ---------> | |
718 | * |------------|-----------------------| | |
719 | * <-------- m ----------><----- r -----> | |
720 | * ^ ^ ^ | |
721 | * | | | | |
722 | * | | current time | |
723 | * | timer start | |
724 | * last write | |
725 | * | |
726 | * (MRAI - m) < r | |
727 | */ | |
728 | if (peer->t_routeadv) | |
729 | remain = thread_timer_remain_second(peer->t_routeadv); | |
730 | else | |
731 | remain = peer->v_routeadv; | |
732 | diff = peer->v_routeadv - diff; | |
733 | if (diff <= (double)remain) { | |
734 | BGP_TIMER_OFF(peer->t_routeadv); | |
735 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, diff); | |
736 | } | |
cb1faec9 DS |
737 | } |
738 | ||
d62a17ae | 739 | static int bgp_maxmed_onstartup_applicable(struct bgp *bgp) |
abc920f8 | 740 | { |
d62a17ae | 741 | if (!bgp->maxmed_onstartup_over) |
742 | return 1; | |
abc920f8 | 743 | |
d62a17ae | 744 | return 0; |
abc920f8 DS |
745 | } |
746 | ||
d62a17ae | 747 | int bgp_maxmed_onstartup_configured(struct bgp *bgp) |
abc920f8 | 748 | { |
d62a17ae | 749 | if (bgp->v_maxmed_onstartup != BGP_MAXMED_ONSTARTUP_UNCONFIGURED) |
750 | return 1; | |
abc920f8 | 751 | |
d62a17ae | 752 | return 0; |
abc920f8 DS |
753 | } |
754 | ||
d62a17ae | 755 | int bgp_maxmed_onstartup_active(struct bgp *bgp) |
abc920f8 | 756 | { |
d62a17ae | 757 | if (bgp->t_maxmed_onstartup) |
758 | return 1; | |
abc920f8 | 759 | |
d62a17ae | 760 | return 0; |
abc920f8 DS |
761 | } |
762 | ||
d62a17ae | 763 | void bgp_maxmed_update(struct bgp *bgp) |
abc920f8 | 764 | { |
d62a17ae | 765 | u_char maxmed_active; |
766 | u_int32_t maxmed_value; | |
767 | ||
768 | if (bgp->v_maxmed_admin) { | |
769 | maxmed_active = 1; | |
770 | maxmed_value = bgp->maxmed_admin_value; | |
771 | } else if (bgp->t_maxmed_onstartup) { | |
772 | maxmed_active = 1; | |
773 | maxmed_value = bgp->maxmed_onstartup_value; | |
774 | } else { | |
775 | maxmed_active = 0; | |
776 | maxmed_value = BGP_MAXMED_VALUE_DEFAULT; | |
777 | } | |
778 | ||
779 | if (bgp->maxmed_active != maxmed_active | |
780 | || bgp->maxmed_value != maxmed_value) { | |
781 | bgp->maxmed_active = maxmed_active; | |
782 | bgp->maxmed_value = maxmed_value; | |
783 | ||
784 | update_group_announce(bgp); | |
785 | } | |
abc920f8 DS |
786 | } |
787 | ||
788 | /* The maxmed onstartup timer expiry callback. */ | |
d62a17ae | 789 | static int bgp_maxmed_onstartup_timer(struct thread *thread) |
abc920f8 | 790 | { |
d62a17ae | 791 | struct bgp *bgp; |
abc920f8 | 792 | |
d62a17ae | 793 | zlog_info("Max med on startup ended - timer expired."); |
abc920f8 | 794 | |
d62a17ae | 795 | bgp = THREAD_ARG(thread); |
796 | THREAD_TIMER_OFF(bgp->t_maxmed_onstartup); | |
797 | bgp->maxmed_onstartup_over = 1; | |
abc920f8 | 798 | |
d62a17ae | 799 | bgp_maxmed_update(bgp); |
abc920f8 | 800 | |
d62a17ae | 801 | return 0; |
abc920f8 DS |
802 | } |
803 | ||
d62a17ae | 804 | static void bgp_maxmed_onstartup_begin(struct bgp *bgp) |
abc920f8 | 805 | { |
d62a17ae | 806 | /* Applicable only once in the process lifetime on the startup */ |
807 | if (bgp->maxmed_onstartup_over) | |
808 | return; | |
abc920f8 | 809 | |
d62a17ae | 810 | zlog_info("Begin maxmed onstartup mode - timer %d seconds", |
811 | bgp->v_maxmed_onstartup); | |
abc920f8 | 812 | |
d62a17ae | 813 | thread_add_timer(bm->master, bgp_maxmed_onstartup_timer, bgp, |
814 | bgp->v_maxmed_onstartup, &bgp->t_maxmed_onstartup); | |
abc920f8 | 815 | |
d62a17ae | 816 | if (!bgp->v_maxmed_admin) { |
817 | bgp->maxmed_active = 1; | |
818 | bgp->maxmed_value = bgp->maxmed_onstartup_value; | |
819 | } | |
abc920f8 | 820 | |
d62a17ae | 821 | /* Route announce to all peers should happen after this in |
822 | * bgp_establish() */ | |
abc920f8 DS |
823 | } |
824 | ||
d62a17ae | 825 | static void bgp_maxmed_onstartup_process_status_change(struct peer *peer) |
abc920f8 | 826 | { |
d62a17ae | 827 | if (peer->status == Established && !peer->bgp->established) { |
828 | bgp_maxmed_onstartup_begin(peer->bgp); | |
829 | } | |
abc920f8 DS |
830 | } |
831 | ||
f188f2c4 | 832 | /* The update delay timer expiry callback. */ |
d62a17ae | 833 | static int bgp_update_delay_timer(struct thread *thread) |
f188f2c4 | 834 | { |
d62a17ae | 835 | struct bgp *bgp; |
f188f2c4 | 836 | |
d62a17ae | 837 | zlog_info("Update delay ended - timer expired."); |
f188f2c4 | 838 | |
d62a17ae | 839 | bgp = THREAD_ARG(thread); |
840 | THREAD_TIMER_OFF(bgp->t_update_delay); | |
841 | bgp_update_delay_end(bgp); | |
f188f2c4 | 842 | |
d62a17ae | 843 | return 0; |
f188f2c4 DS |
844 | } |
845 | ||
846 | /* The establish wait timer expiry callback. */ | |
d62a17ae | 847 | static int bgp_establish_wait_timer(struct thread *thread) |
f188f2c4 | 848 | { |
d62a17ae | 849 | struct bgp *bgp; |
f188f2c4 | 850 | |
d62a17ae | 851 | zlog_info("Establish wait - timer expired."); |
f188f2c4 | 852 | |
d62a17ae | 853 | bgp = THREAD_ARG(thread); |
854 | THREAD_TIMER_OFF(bgp->t_establish_wait); | |
855 | bgp_check_update_delay(bgp); | |
f188f2c4 | 856 | |
d62a17ae | 857 | return 0; |
f188f2c4 DS |
858 | } |
859 | ||
860 | /* Steps to begin the update delay: | |
861 | - initialize queues if needed | |
862 | - stop the queue processing | |
863 | - start the timer */ | |
d62a17ae | 864 | static void bgp_update_delay_begin(struct bgp *bgp) |
f188f2c4 | 865 | { |
d62a17ae | 866 | struct listnode *node, *nnode; |
867 | struct peer *peer; | |
f188f2c4 | 868 | |
d62a17ae | 869 | /* Stop the processing of queued work. Enqueue shall continue */ |
870 | work_queue_plug(bm->process_main_queue); | |
f188f2c4 | 871 | |
d62a17ae | 872 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) |
873 | peer->update_delay_over = 0; | |
f188f2c4 | 874 | |
d62a17ae | 875 | /* Start the update-delay timer */ |
876 | thread_add_timer(bm->master, bgp_update_delay_timer, bgp, | |
877 | bgp->v_update_delay, &bgp->t_update_delay); | |
f188f2c4 | 878 | |
d62a17ae | 879 | if (bgp->v_establish_wait != bgp->v_update_delay) |
880 | thread_add_timer(bm->master, bgp_establish_wait_timer, bgp, | |
881 | bgp->v_establish_wait, &bgp->t_establish_wait); | |
f188f2c4 | 882 | |
d62a17ae | 883 | quagga_timestamp(3, bgp->update_delay_begin_time, |
884 | sizeof(bgp->update_delay_begin_time)); | |
f188f2c4 DS |
885 | } |
886 | ||
d62a17ae | 887 | static void bgp_update_delay_process_status_change(struct peer *peer) |
f188f2c4 | 888 | { |
d62a17ae | 889 | if (peer->status == Established) { |
890 | if (!peer->bgp->established++) { | |
891 | bgp_update_delay_begin(peer->bgp); | |
892 | zlog_info( | |
893 | "Begin read-only mode - update-delay timer %d seconds", | |
894 | peer->bgp->v_update_delay); | |
895 | } | |
896 | if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_BIT_RCV)) | |
897 | bgp_update_restarted_peers(peer); | |
898 | } | |
899 | if (peer->ostatus == Established | |
900 | && bgp_update_delay_active(peer->bgp)) { | |
901 | /* Adjust the update-delay state to account for this flap. | |
902 | NOTE: Intentionally skipping adjusting implicit_eors or | |
903 | explicit_eors | |
904 | counters. Extra sanity check in bgp_check_update_delay() | |
905 | should | |
906 | be enough to take care of any additive discrepancy in bgp eor | |
907 | counters */ | |
908 | peer->bgp->established--; | |
909 | peer->update_delay_over = 0; | |
910 | } | |
f188f2c4 DS |
911 | } |
912 | ||
200df115 | 913 | /* Called after event occured, this function change status and reset |
914 | read/write and timer thread. */ | |
d62a17ae | 915 | void bgp_fsm_change_status(struct peer *peer, int status) |
200df115 | 916 | { |
1ff9a340 | 917 | |
d62a17ae | 918 | bgp_dump_state(peer, peer->status, status); |
919 | ||
920 | /* Transition into Clearing or Deleted must /always/ clear all routes.. | |
921 | * (and must do so before actually changing into Deleted.. | |
922 | */ | |
923 | if (status >= Clearing) { | |
924 | bgp_clear_route_all(peer); | |
925 | ||
926 | /* If no route was queued for the clear-node processing, | |
927 | * generate the | |
928 | * completion event here. This is needed because if there are no | |
929 | * routes | |
930 | * to trigger the background clear-node thread, the event won't | |
931 | * get | |
932 | * generated and the peer would be stuck in Clearing. Note that | |
933 | * this | |
934 | * event is for the peer and helps the peer transition out of | |
935 | * Clearing | |
936 | * state; it should not be generated per (AFI,SAFI). The event | |
937 | * is | |
938 | * directly posted here without calling clear_node_complete() as | |
939 | * we | |
940 | * shouldn't do an extra unlock. This event will get processed | |
941 | * after | |
942 | * the state change that happens below, so peer will be in | |
943 | * Clearing | |
944 | * (or Deleted). | |
945 | */ | |
946 | if (!work_queue_is_scheduled(peer->clear_node_queue)) | |
947 | BGP_EVENT_ADD(peer, Clearing_Completed); | |
948 | } | |
949 | ||
950 | /* Preserve old status and change into new status. */ | |
951 | peer->ostatus = peer->status; | |
952 | peer->status = status; | |
953 | ||
954 | /* Save event that caused status change. */ | |
955 | peer->last_major_event = peer->cur_event; | |
956 | ||
957 | if (status == Established) | |
958 | UNSET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER); | |
959 | ||
960 | /* If max-med processing is applicable, do the necessary. */ | |
961 | if (status == Established) { | |
962 | if (bgp_maxmed_onstartup_configured(peer->bgp) | |
963 | && bgp_maxmed_onstartup_applicable(peer->bgp)) | |
964 | bgp_maxmed_onstartup_process_status_change(peer); | |
965 | else | |
966 | peer->bgp->maxmed_onstartup_over = 1; | |
967 | } | |
968 | ||
969 | /* If update-delay processing is applicable, do the necessary. */ | |
970 | if (bgp_update_delay_configured(peer->bgp) | |
971 | && bgp_update_delay_applicable(peer->bgp)) | |
972 | bgp_update_delay_process_status_change(peer); | |
973 | ||
974 | if (bgp_debug_neighbor_events(peer)) | |
975 | zlog_debug("%s went from %s to %s", peer->host, | |
976 | lookup_msg(bgp_status_msg, peer->ostatus, NULL), | |
977 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
200df115 | 978 | } |
979 | ||
3117b5c4 | 980 | /* Flush the event queue and ensure the peer is shut down */ |
d62a17ae | 981 | static int bgp_clearing_completed(struct peer *peer) |
3117b5c4 | 982 | { |
d62a17ae | 983 | int rc = bgp_stop(peer); |
1ff9a340 | 984 | |
d62a17ae | 985 | if (rc >= 0) |
986 | BGP_EVENT_FLUSH(peer); | |
3117b5c4 | 987 | |
d62a17ae | 988 | return rc; |
3117b5c4 SH |
989 | } |
990 | ||
718e3744 | 991 | /* Administrative BGP peer stop event. */ |
3117b5c4 | 992 | /* May be called multiple times for the same peer */ |
d62a17ae | 993 | int bgp_stop(struct peer *peer) |
718e3744 | 994 | { |
d62a17ae | 995 | afi_t afi; |
996 | safi_t safi; | |
997 | char orf_name[BUFSIZ]; | |
998 | int ret = 0; | |
999 | ||
1000 | if (peer_dynamic_neighbor(peer) | |
1001 | && !(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) { | |
1002 | if (bgp_debug_neighbor_events(peer)) | |
1003 | zlog_debug("%s (dynamic neighbor) deleted", peer->host); | |
1004 | peer_delete(peer); | |
1005 | return -1; | |
c22767d8 | 1006 | } |
848973c7 | 1007 | |
d62a17ae | 1008 | /* Can't do this in Clearing; events are used for state transitions */ |
1009 | if (peer->status != Clearing) { | |
1010 | /* Delete all existing events of the peer */ | |
1011 | BGP_EVENT_FLUSH(peer); | |
93406d87 | 1012 | } |
d62a17ae | 1013 | |
1014 | /* Increment Dropped count. */ | |
1015 | if (peer->status == Established) { | |
1016 | peer->dropped++; | |
1017 | ||
1018 | /* bgp log-neighbor-changes of neighbor Down */ | |
1019 | if (bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) { | |
1020 | struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id); | |
1021 | zlog_info( | |
1022 | "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s", | |
1023 | peer->host, | |
1024 | (peer->hostname) ? peer->hostname : "Unknown", | |
1025 | vrf ? ((vrf->vrf_id != VRF_DEFAULT) ? vrf->name | |
1026 | : "Default") | |
1027 | : "", | |
1028 | peer_down_str[(int)peer->last_reset]); | |
1029 | } | |
1030 | ||
1031 | /* graceful restart */ | |
1032 | if (peer->t_gr_stale) { | |
1033 | BGP_TIMER_OFF(peer->t_gr_stale); | |
1034 | if (bgp_debug_neighbor_events(peer)) | |
1035 | zlog_debug( | |
1036 | "%s graceful restart stalepath timer stopped", | |
1037 | peer->host); | |
1038 | } | |
1039 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) { | |
1040 | if (bgp_debug_neighbor_events(peer)) { | |
1041 | zlog_debug( | |
1042 | "%s graceful restart timer started for %d sec", | |
1043 | peer->host, peer->v_gr_restart); | |
1044 | zlog_debug( | |
1045 | "%s graceful restart stalepath timer started for %d sec", | |
1046 | peer->host, peer->bgp->stalepath_time); | |
1047 | } | |
1048 | BGP_TIMER_ON(peer->t_gr_restart, | |
1049 | bgp_graceful_restart_timer_expire, | |
1050 | peer->v_gr_restart); | |
1051 | BGP_TIMER_ON(peer->t_gr_stale, | |
1052 | bgp_graceful_stale_timer_expire, | |
1053 | peer->bgp->stalepath_time); | |
1054 | } else { | |
1055 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
1056 | ||
1057 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
1058 | for (safi = SAFI_UNICAST; | |
a08ca0a7 | 1059 | safi <= SAFI_MPLS_VPN; safi++) |
d62a17ae | 1060 | peer->nsf[afi][safi] = 0; |
1061 | } | |
1062 | ||
1063 | /* set last reset time */ | |
1064 | peer->resettime = peer->uptime = bgp_clock(); | |
1065 | ||
1066 | if (BGP_DEBUG(update_groups, UPDATE_GROUPS)) | |
1067 | zlog_debug("%s remove from all update group", | |
1068 | peer->host); | |
1069 | update_group_remove_peer_afs(peer); | |
1070 | ||
1071 | hook_call(peer_backward_transition, peer); | |
1072 | ||
1073 | /* Reset peer synctime */ | |
1074 | peer->synctime = 0; | |
1075 | ||
1076 | bgp_bfd_deregister_peer(peer); | |
93406d87 | 1077 | } |
93406d87 | 1078 | |
424ab01d | 1079 | /* stop keepalives */ |
b72b6f4f | 1080 | bgp_keepalives_off(peer); |
424ab01d QY |
1081 | |
1082 | /* Stop read and write threads. */ | |
1083 | bgp_writes_off(peer); | |
1084 | bgp_reads_off(peer); | |
1085 | ||
387f984e QY |
1086 | THREAD_OFF(peer->t_connect_check_r); |
1087 | THREAD_OFF(peer->t_connect_check_w); | |
d62a17ae | 1088 | |
1089 | /* Stop all timers. */ | |
1090 | BGP_TIMER_OFF(peer->t_start); | |
1091 | BGP_TIMER_OFF(peer->t_connect); | |
1092 | BGP_TIMER_OFF(peer->t_holdtime); | |
d62a17ae | 1093 | BGP_TIMER_OFF(peer->t_routeadv); |
d62a17ae | 1094 | |
1095 | /* Clear input and output buffer. */ | |
424ab01d | 1096 | pthread_mutex_lock(&peer->io_mtx); |
d3ecc69e | 1097 | { |
424ab01d QY |
1098 | if (peer->ibuf) |
1099 | stream_fifo_clean(peer->ibuf); | |
d3ecc69e QY |
1100 | if (peer->obuf) |
1101 | stream_fifo_clean(peer->obuf); | |
424ab01d QY |
1102 | |
1103 | if (peer->ibuf_work) | |
74ffbfe6 | 1104 | ringbuf_wipe(peer->ibuf_work); |
424ab01d QY |
1105 | if (peer->obuf_work) |
1106 | stream_reset(peer->obuf_work); | |
1107 | ||
1108 | if (peer->curr) { | |
1109 | stream_free(peer->curr); | |
1110 | peer->curr = NULL; | |
1111 | } | |
d3ecc69e | 1112 | } |
424ab01d | 1113 | pthread_mutex_unlock(&peer->io_mtx); |
d62a17ae | 1114 | |
1115 | /* Close of file descriptor. */ | |
1116 | if (peer->fd >= 0) { | |
1117 | close(peer->fd); | |
1118 | peer->fd = -1; | |
93406d87 | 1119 | } |
1120 | ||
05c7a1cc QY |
1121 | FOREACH_AFI_SAFI (afi, safi) { |
1122 | /* Reset all negotiated variables */ | |
1123 | peer->afc_nego[afi][safi] = 0; | |
1124 | peer->afc_adv[afi][safi] = 0; | |
1125 | peer->afc_recv[afi][safi] = 0; | |
1126 | ||
1127 | /* peer address family capability flags*/ | |
1128 | peer->af_cap[afi][safi] = 0; | |
1129 | ||
1130 | /* peer address family status flags*/ | |
1131 | peer->af_sflags[afi][safi] = 0; | |
1132 | ||
1133 | /* Received ORF prefix-filter */ | |
1134 | peer->orf_plist[afi][safi] = NULL; | |
1135 | ||
1136 | if ((peer->status == OpenConfirm) | |
1137 | || (peer->status == Established)) { | |
1138 | /* ORF received prefix-filter pnt */ | |
1139 | sprintf(orf_name, "%s.%d.%d", peer->host, afi, safi); | |
1140 | prefix_bgp_orf_remove_all(afi, orf_name); | |
d62a17ae | 1141 | } |
05c7a1cc | 1142 | } |
d62a17ae | 1143 | |
1144 | /* Reset keepalive and holdtime */ | |
d25e4efc | 1145 | if (PEER_OR_GROUP_TIMER_SET(peer)) { |
d62a17ae | 1146 | peer->v_keepalive = peer->keepalive; |
1147 | peer->v_holdtime = peer->holdtime; | |
1148 | } else { | |
1149 | peer->v_keepalive = peer->bgp->default_keepalive; | |
1150 | peer->v_holdtime = peer->bgp->default_holdtime; | |
1ff9a340 | 1151 | } |
d62a17ae | 1152 | |
1153 | peer->update_time = 0; | |
1154 | ||
1155 | /* Until we are sure that there is no problem about prefix count | |
1156 | this should be commented out.*/ | |
718e3744 | 1157 | #if 0 |
1158 | /* Reset prefix count */ | |
1159 | peer->pcount[AFI_IP][SAFI_UNICAST] = 0; | |
1160 | peer->pcount[AFI_IP][SAFI_MULTICAST] = 0; | |
cd1964ff | 1161 | peer->pcount[AFI_IP][SAFI_LABELED_UNICAST] = 0; |
718e3744 | 1162 | peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0; |
1163 | peer->pcount[AFI_IP6][SAFI_UNICAST] = 0; | |
1164 | peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0; | |
cd1964ff | 1165 | peer->pcount[AFI_IP6][SAFI_LABELED_UNICAST] = 0; |
718e3744 | 1166 | #endif /* 0 */ |
1167 | ||
d62a17ae | 1168 | if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE) |
1169 | && !(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) { | |
1170 | peer_delete(peer); | |
1171 | ret = -1; | |
1172 | } else { | |
1173 | bgp_peer_conf_if_to_su_update(peer); | |
1174 | } | |
1175 | ||
1176 | return ret; | |
718e3744 | 1177 | } |
1178 | ||
1179 | /* BGP peer is stoped by the error. */ | |
d62a17ae | 1180 | static int bgp_stop_with_error(struct peer *peer) |
718e3744 | 1181 | { |
d62a17ae | 1182 | /* Double start timer. */ |
1183 | peer->v_start *= 2; | |
1184 | ||
1185 | /* Overflow check. */ | |
1186 | if (peer->v_start >= (60 * 2)) | |
1187 | peer->v_start = (60 * 2); | |
1188 | ||
1189 | if (peer_dynamic_neighbor(peer)) { | |
1190 | if (bgp_debug_neighbor_events(peer)) | |
1191 | zlog_debug("%s (dynamic neighbor) deleted", peer->host); | |
1192 | peer_delete(peer); | |
1193 | return -1; | |
1194 | } | |
1195 | ||
1196 | return (bgp_stop(peer)); | |
718e3744 | 1197 | } |
1198 | ||
397b5bde LR |
1199 | |
1200 | /* something went wrong, send notify and tear down */ | |
d62a17ae | 1201 | static int bgp_stop_with_notify(struct peer *peer, u_char code, u_char sub_code) |
397b5bde | 1202 | { |
d62a17ae | 1203 | /* Send notify to remote peer */ |
1204 | bgp_notify_send(peer, code, sub_code); | |
1205 | ||
1206 | if (peer_dynamic_neighbor(peer)) { | |
1207 | if (bgp_debug_neighbor_events(peer)) | |
1208 | zlog_debug("%s (dynamic neighbor) deleted", peer->host); | |
1209 | peer_delete(peer); | |
1210 | return -1; | |
1211 | } | |
f14e6fdb | 1212 | |
d62a17ae | 1213 | /* Clear start timer value to default. */ |
1214 | peer->v_start = BGP_INIT_START_TIMER; | |
397b5bde | 1215 | |
d62a17ae | 1216 | return (bgp_stop(peer)); |
397b5bde LR |
1217 | } |
1218 | ||
07a16526 QY |
1219 | /** |
1220 | * Determines whether a TCP session has successfully established for a peer and | |
1221 | * events as appropriate. | |
1222 | * | |
1223 | * This function is called when setting up a new session. After connect() is | |
387f984e QY |
1224 | * called on the peer's socket (in bgp_start()), the fd is passed to poll() |
1225 | * to wait for connection success or failure. When poll() returns, this | |
07a16526 | 1226 | * function is called to evaluate the result. |
387f984e QY |
1227 | * |
1228 | * Due to differences in behavior of poll() on Linux and BSD - specifically, | |
1229 | * the value of .revents in the case of a closed connection - this function is | |
1230 | * scheduled both for a read and a write event. The write event is triggered | |
1231 | * when the connection is established. A read event is triggered when the | |
1232 | * connection is closed. Thus we need to cancel whichever one did not occur. | |
07a16526 QY |
1233 | */ |
1234 | static int bgp_connect_check(struct thread *thread) | |
1235 | { | |
1236 | int status; | |
1237 | socklen_t slen; | |
1238 | int ret; | |
1239 | struct peer *peer; | |
1240 | ||
1241 | peer = THREAD_ARG(thread); | |
424ab01d QY |
1242 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON)); |
1243 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON)); | |
1244 | assert(!peer->t_read); | |
1245 | assert(!peer->t_write); | |
07a16526 | 1246 | |
387f984e QY |
1247 | THREAD_OFF(peer->t_connect_check_r); |
1248 | THREAD_OFF(peer->t_connect_check_w); | |
dc1188bb | 1249 | |
07a16526 QY |
1250 | /* Check file descriptor. */ |
1251 | slen = sizeof(status); | |
1252 | ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *)&status, | |
1253 | &slen); | |
1254 | ||
1255 | /* If getsockopt is fail, this is fatal error. */ | |
1256 | if (ret < 0) { | |
1257 | zlog_info("can't get sockopt for nonblocking connect"); | |
1258 | BGP_EVENT_ADD(peer, TCP_fatal_error); | |
1259 | return -1; | |
1260 | } | |
1261 | ||
1262 | /* When status is 0 then TCP connection is established. */ | |
1263 | if (status == 0) { | |
1264 | BGP_EVENT_ADD(peer, TCP_connection_open); | |
1265 | return 1; | |
1266 | } else { | |
1267 | if (bgp_debug_neighbor_events(peer)) | |
1268 | zlog_debug("%s [Event] Connect failed (%s)", peer->host, | |
1269 | safe_strerror(errno)); | |
1270 | BGP_EVENT_ADD(peer, TCP_connection_open_failed); | |
1271 | return 0; | |
1272 | } | |
1273 | } | |
397b5bde | 1274 | |
718e3744 | 1275 | /* TCP connection open. Next we send open message to remote peer. And |
1276 | add read thread for reading open message. */ | |
d62a17ae | 1277 | static int bgp_connect_success(struct peer *peer) |
718e3744 | 1278 | { |
d62a17ae | 1279 | if (peer->fd < 0) { |
1280 | zlog_err("bgp_connect_success peer's fd is negative value %d", | |
1281 | peer->fd); | |
1282 | bgp_stop(peer); | |
1283 | return -1; | |
1284 | } | |
1285 | ||
1286 | if (bgp_getsockname(peer) < 0) { | |
1287 | zlog_err("%s: bgp_getsockname(): failed for peer %s, fd %d", | |
1288 | __FUNCTION__, peer->host, peer->fd); | |
1289 | bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, | |
1290 | 0); /* internal error */ | |
424ab01d | 1291 | bgp_writes_on(peer); |
d62a17ae | 1292 | return -1; |
1293 | } | |
1294 | ||
424ab01d | 1295 | bgp_reads_on(peer); |
d62a17ae | 1296 | |
1297 | if (bgp_debug_neighbor_events(peer)) { | |
1298 | char buf1[SU_ADDRSTRLEN]; | |
1299 | ||
1300 | if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) | |
1301 | zlog_debug("%s open active, local address %s", | |
1302 | peer->host, | |
1303 | sockunion2str(peer->su_local, buf1, | |
1304 | SU_ADDRSTRLEN)); | |
1305 | else | |
1306 | zlog_debug("%s passive open", peer->host); | |
1307 | } | |
1308 | ||
1309 | bgp_open_send(peer); | |
1310 | ||
1311 | return 0; | |
718e3744 | 1312 | } |
1313 | ||
1314 | /* TCP connect fail */ | |
d62a17ae | 1315 | static int bgp_connect_fail(struct peer *peer) |
718e3744 | 1316 | { |
d62a17ae | 1317 | if (peer_dynamic_neighbor(peer)) { |
1318 | if (bgp_debug_neighbor_events(peer)) | |
1319 | zlog_debug("%s (dynamic neighbor) deleted", peer->host); | |
1320 | peer_delete(peer); | |
1321 | return -1; | |
1322 | } | |
1323 | ||
1324 | return (bgp_stop(peer)); | |
718e3744 | 1325 | } |
1326 | ||
1327 | /* This function is the first starting point of all BGP connection. It | |
1328 | try to connect to remote peer with non-blocking IO. */ | |
d62a17ae | 1329 | int bgp_start(struct peer *peer) |
718e3744 | 1330 | { |
d62a17ae | 1331 | int status; |
1332 | int connected = 0; | |
1333 | ||
1334 | bgp_peer_conf_if_to_su_update(peer); | |
1335 | ||
1336 | if (peer->su.sa.sa_family == AF_UNSPEC) { | |
1337 | if (bgp_debug_neighbor_events(peer)) | |
1338 | zlog_debug( | |
1339 | "%s [FSM] Unable to get neighbor's IP address, waiting...", | |
1340 | peer->host); | |
1341 | return -1; | |
1342 | } | |
1343 | ||
1344 | if (BGP_PEER_START_SUPPRESSED(peer)) { | |
1345 | if (bgp_debug_neighbor_events(peer)) | |
1346 | zlog_err( | |
1347 | "%s [FSM] Trying to start suppressed peer" | |
1348 | " - this is never supposed to happen!", | |
1349 | peer->host); | |
1350 | return -1; | |
1351 | } | |
1352 | ||
1353 | /* Scrub some information that might be left over from a previous, | |
1354 | * session | |
1355 | */ | |
1356 | /* Connection information. */ | |
1357 | if (peer->su_local) { | |
1358 | sockunion_free(peer->su_local); | |
1359 | peer->su_local = NULL; | |
1360 | } | |
1361 | ||
1362 | if (peer->su_remote) { | |
1363 | sockunion_free(peer->su_remote); | |
1364 | peer->su_remote = NULL; | |
1365 | } | |
1366 | ||
1367 | /* Clear remote router-id. */ | |
1368 | peer->remote_id.s_addr = 0; | |
1369 | ||
1370 | /* Clear peer capability flag. */ | |
1371 | peer->cap = 0; | |
1372 | ||
1373 | /* If the peer is passive mode, force to move to Active mode. */ | |
1374 | if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE)) { | |
1375 | BGP_EVENT_ADD(peer, TCP_connection_open_failed); | |
1376 | return 0; | |
1377 | } | |
1378 | ||
61cf4b37 PG |
1379 | if (peer->bgp && |
1380 | peer->bgp->vrf_id == VRF_UNKNOWN) { | |
1381 | if (bgp_debug_neighbor_events(peer)) | |
1382 | zlog_err( | |
1383 | "%s [FSM] In a VRF that is not initialised yet", | |
1384 | peer->host); | |
1385 | return -1; | |
1386 | } | |
1387 | ||
d62a17ae | 1388 | /* Register to be notified on peer up */ |
1389 | if (peer->sort == BGP_PEER_EBGP && peer->ttl == 1 | |
1390 | && !CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK) | |
1391 | && !bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK)) | |
1392 | connected = 1; | |
1393 | else | |
1394 | connected = 0; | |
1395 | ||
1396 | if (!bgp_find_or_add_nexthop(peer->bgp, | |
1397 | family2afi(peer->su.sa.sa_family), NULL, | |
1398 | peer, connected)) { | |
1399 | #if defined(HAVE_CUMULUS) | |
1400 | if (bgp_debug_neighbor_events(peer)) | |
1401 | zlog_debug("%s [FSM] Waiting for NHT", peer->host); | |
1402 | ||
1403 | BGP_EVENT_ADD(peer, TCP_connection_open_failed); | |
1404 | return 0; | |
b5826a12 | 1405 | #endif |
718e3744 | 1406 | } |
d62a17ae | 1407 | |
424ab01d QY |
1408 | assert(!peer->t_write); |
1409 | assert(!peer->t_read); | |
1410 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON)); | |
1411 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON)); | |
d62a17ae | 1412 | status = bgp_connect(peer); |
1413 | ||
1414 | switch (status) { | |
1415 | case connect_error: | |
1416 | if (bgp_debug_neighbor_events(peer)) | |
1417 | zlog_debug("%s [FSM] Connect error", peer->host); | |
1418 | BGP_EVENT_ADD(peer, TCP_connection_open_failed); | |
1419 | break; | |
1420 | case connect_success: | |
1421 | if (bgp_debug_neighbor_events(peer)) | |
1422 | zlog_debug( | |
1423 | "%s [FSM] Connect immediately success, fd %d", | |
1424 | peer->host, peer->fd); | |
1425 | BGP_EVENT_ADD(peer, TCP_connection_open); | |
1426 | break; | |
1427 | case connect_in_progress: | |
1428 | /* To check nonblocking connect, we wait until socket is | |
1429 | readable or writable. */ | |
1430 | if (bgp_debug_neighbor_events(peer)) | |
1431 | zlog_debug( | |
1432 | "%s [FSM] Non blocking connect waiting result, fd %d", | |
1433 | peer->host, peer->fd); | |
1434 | if (peer->fd < 0) { | |
1435 | zlog_err("bgp_start peer's fd is negative value %d", | |
1436 | peer->fd); | |
1437 | return -1; | |
1438 | } | |
becedef6 | 1439 | /* |
387f984e QY |
1440 | * - when the socket becomes ready, poll() will signify POLLOUT |
1441 | * - if it fails to connect, poll() will signify POLLHUP | |
1442 | * - POLLHUP is handled as a 'read' event by thread.c | |
1443 | * | |
1444 | * therefore, we schedule both a read and a write event with | |
1445 | * bgp_connect_check() as the handler for each and cancel the | |
1446 | * unused event in that function. | |
becedef6 | 1447 | */ |
424ab01d | 1448 | thread_add_read(bm->master, bgp_connect_check, peer, peer->fd, |
387f984e QY |
1449 | &peer->t_connect_check_r); |
1450 | thread_add_write(bm->master, bgp_connect_check, peer, peer->fd, | |
1451 | &peer->t_connect_check_w); | |
d62a17ae | 1452 | break; |
1453 | } | |
1454 | return 0; | |
718e3744 | 1455 | } |
1456 | ||
1457 | /* Connect retry timer is expired when the peer status is Connect. */ | |
d62a17ae | 1458 | static int bgp_reconnect(struct peer *peer) |
718e3744 | 1459 | { |
d62a17ae | 1460 | if (bgp_stop(peer) < 0) |
1461 | return -1; | |
1ff9a340 | 1462 | |
d62a17ae | 1463 | bgp_start(peer); |
1464 | return 0; | |
718e3744 | 1465 | } |
1466 | ||
d62a17ae | 1467 | static int bgp_fsm_open(struct peer *peer) |
718e3744 | 1468 | { |
d62a17ae | 1469 | /* Send keepalive and make keepalive timer */ |
1470 | bgp_keepalive_send(peer); | |
718e3744 | 1471 | |
d62a17ae | 1472 | /* Reset holdtimer value. */ |
1473 | BGP_TIMER_OFF(peer->t_holdtime); | |
718e3744 | 1474 | |
d62a17ae | 1475 | return 0; |
718e3744 | 1476 | } |
1477 | ||
397b5bde LR |
1478 | /* FSM error, unexpected event. This is error of BGP connection. So cut the |
1479 | peer and change to Idle status. */ | |
d62a17ae | 1480 | static int bgp_fsm_event_error(struct peer *peer) |
397b5bde | 1481 | { |
d62a17ae | 1482 | zlog_err("%s [FSM] unexpected packet received in state %s", peer->host, |
1483 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
397b5bde | 1484 | |
d62a17ae | 1485 | return bgp_stop_with_notify(peer, BGP_NOTIFY_FSM_ERR, 0); |
397b5bde LR |
1486 | } |
1487 | ||
718e3744 | 1488 | /* Hold timer expire. This is error of BGP connection. So cut the |
1489 | peer and change to Idle status. */ | |
d62a17ae | 1490 | static int bgp_fsm_holdtime_expire(struct peer *peer) |
718e3744 | 1491 | { |
d62a17ae | 1492 | if (bgp_debug_neighbor_events(peer)) |
1493 | zlog_debug("%s [FSM] Hold timer expire", peer->host); | |
718e3744 | 1494 | |
d62a17ae | 1495 | return bgp_stop_with_notify(peer, BGP_NOTIFY_HOLD_ERR, 0); |
718e3744 | 1496 | } |
1497 | ||
727c4f87 QY |
1498 | /** |
1499 | * Transition to Established state. | |
1500 | * | |
1501 | * Convert peer from stub to full fledged peer, set some timers, and generate | |
1502 | * initial updates. | |
1503 | */ | |
d62a17ae | 1504 | static int bgp_establish(struct peer *peer) |
718e3744 | 1505 | { |
d62a17ae | 1506 | afi_t afi; |
1507 | safi_t safi; | |
1508 | int nsf_af_count = 0; | |
1509 | int ret = 0; | |
1510 | struct peer *other; | |
1511 | ||
1512 | other = peer->doppelganger; | |
1513 | peer = peer_xfer_conn(peer); | |
1514 | if (!peer) { | |
1515 | zlog_err("%%Neighbor failed in xfer_conn"); | |
1516 | return -1; | |
93406d87 | 1517 | } |
93406d87 | 1518 | |
d62a17ae | 1519 | if (other == peer) |
9d303b37 DL |
1520 | ret = |
1521 | 1; /* bgp_establish specific code when xfer_conn | |
1522 | happens. */ | |
d62a17ae | 1523 | |
1524 | /* Reset capability open status flag. */ | |
1525 | if (!CHECK_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN)) | |
1526 | SET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN); | |
1527 | ||
1528 | /* Clear start timer value to default. */ | |
1529 | peer->v_start = BGP_INIT_START_TIMER; | |
1530 | ||
1531 | /* Increment established count. */ | |
1532 | peer->established++; | |
1533 | bgp_fsm_change_status(peer, Established); | |
1534 | ||
1535 | /* bgp log-neighbor-changes of neighbor Up */ | |
1536 | if (bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) { | |
1537 | struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id); | |
1538 | zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up", | |
1539 | peer->host, | |
1540 | (peer->hostname) ? peer->hostname : "Unknown", | |
1541 | vrf ? ((vrf->vrf_id != VRF_DEFAULT) ? vrf->name | |
1542 | : "Default") | |
1543 | : ""); | |
1544 | } | |
1545 | /* assign update-group/subgroup */ | |
1546 | update_group_adjust_peer_afs(peer); | |
1547 | ||
1548 | /* graceful restart */ | |
1549 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT); | |
1550 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
a08ca0a7 | 1551 | for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++) { |
d62a17ae | 1552 | if (peer->afc_nego[afi][safi] |
1553 | && CHECK_FLAG(peer->cap, PEER_CAP_RESTART_ADV) | |
1554 | && CHECK_FLAG(peer->af_cap[afi][safi], | |
1555 | PEER_CAP_RESTART_AF_RCV)) { | |
1556 | if (peer->nsf[afi][safi] | |
1557 | && !CHECK_FLAG( | |
1558 | peer->af_cap[afi][safi], | |
1559 | PEER_CAP_RESTART_AF_PRESERVE_RCV)) | |
1560 | bgp_clear_stale_route(peer, afi, safi); | |
1561 | ||
1562 | peer->nsf[afi][safi] = 1; | |
1563 | nsf_af_count++; | |
1564 | } else { | |
1565 | if (peer->nsf[afi][safi]) | |
1566 | bgp_clear_stale_route(peer, afi, safi); | |
1567 | peer->nsf[afi][safi] = 0; | |
1568 | } | |
1569 | } | |
1570 | ||
1571 | if (nsf_af_count) | |
1572 | SET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
1573 | else { | |
1574 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
1575 | if (peer->t_gr_stale) { | |
1576 | BGP_TIMER_OFF(peer->t_gr_stale); | |
1577 | if (bgp_debug_neighbor_events(peer)) | |
1578 | zlog_debug( | |
1579 | "%s graceful restart stalepath timer stopped", | |
1580 | peer->host); | |
1581 | } | |
1582 | } | |
93406d87 | 1583 | |
d62a17ae | 1584 | if (peer->t_gr_restart) { |
1585 | BGP_TIMER_OFF(peer->t_gr_restart); | |
1586 | if (bgp_debug_neighbor_events(peer)) | |
1587 | zlog_debug("%s graceful restart timer stopped", | |
1588 | peer->host); | |
1589 | } | |
718e3744 | 1590 | |
d62a17ae | 1591 | hook_call(peer_established, peer); |
1592 | ||
9eb217ff QY |
1593 | /* Reset uptime, turn on keepalives, send current table. */ |
1594 | if (!peer->v_holdtime) | |
1595 | bgp_keepalives_on(peer); | |
1596 | ||
d62a17ae | 1597 | peer->uptime = bgp_clock(); |
1598 | ||
1599 | /* Send route-refresh when ORF is enabled */ | |
05c7a1cc QY |
1600 | FOREACH_AFI_SAFI (afi, safi) { |
1601 | if (CHECK_FLAG(peer->af_cap[afi][safi], | |
1602 | PEER_CAP_ORF_PREFIX_SM_ADV)) { | |
d62a17ae | 1603 | if (CHECK_FLAG(peer->af_cap[afi][safi], |
05c7a1cc QY |
1604 | PEER_CAP_ORF_PREFIX_RM_RCV)) |
1605 | bgp_route_refresh_send(peer, afi, safi, | |
1606 | ORF_TYPE_PREFIX, | |
1607 | REFRESH_IMMEDIATE, 0); | |
1608 | else if (CHECK_FLAG(peer->af_cap[afi][safi], | |
1609 | PEER_CAP_ORF_PREFIX_RM_OLD_RCV)) | |
1610 | bgp_route_refresh_send(peer, afi, safi, | |
1611 | ORF_TYPE_PREFIX_OLD, | |
1612 | REFRESH_IMMEDIATE, 0); | |
1613 | } | |
1614 | } | |
d62a17ae | 1615 | |
1616 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ | |
05c7a1cc QY |
1617 | FOREACH_AFI_SAFI (afi, safi) { |
1618 | if (CHECK_FLAG(peer->af_cap[afi][safi], | |
1619 | PEER_CAP_ORF_PREFIX_RM_ADV)) | |
d62a17ae | 1620 | if (CHECK_FLAG(peer->af_cap[afi][safi], |
05c7a1cc QY |
1621 | PEER_CAP_ORF_PREFIX_SM_RCV) |
1622 | || CHECK_FLAG(peer->af_cap[afi][safi], | |
1623 | PEER_CAP_ORF_PREFIX_SM_OLD_RCV)) | |
1624 | SET_FLAG(peer->af_sflags[afi][safi], | |
1625 | PEER_STATUS_ORF_WAIT_REFRESH); | |
1626 | } | |
d62a17ae | 1627 | |
1628 | bgp_announce_peer(peer); | |
1629 | ||
1630 | /* Start the route advertisement timer to send updates to the peer - if | |
1631 | * BGP | |
1632 | * is not in read-only mode. If it is, the timer will be started at the | |
1633 | * end | |
1634 | * of read-only mode. | |
1635 | */ | |
1636 | if (!bgp_update_delay_active(peer->bgp)) { | |
1637 | BGP_TIMER_OFF(peer->t_routeadv); | |
1638 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0); | |
1639 | } | |
718e3744 | 1640 | |
d62a17ae | 1641 | if (peer->doppelganger && (peer->doppelganger->status != Deleted)) { |
1642 | if (bgp_debug_neighbor_events(peer)) | |
1643 | zlog_debug( | |
1644 | "[Event] Deleting stub connection for peer %s", | |
1645 | peer->host); | |
1646 | ||
1647 | if (peer->doppelganger->status > Active) | |
1648 | bgp_notify_send(peer->doppelganger, BGP_NOTIFY_CEASE, | |
1649 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1650 | else | |
1651 | peer_delete(peer->doppelganger); | |
718e3744 | 1652 | } |
1653 | ||
d62a17ae | 1654 | bgp_bfd_register_peer(peer); |
1655 | return ret; | |
718e3744 | 1656 | } |
1657 | ||
1658 | /* Keepalive packet is received. */ | |
d62a17ae | 1659 | static int bgp_fsm_keepalive(struct peer *peer) |
718e3744 | 1660 | { |
d62a17ae | 1661 | BGP_TIMER_OFF(peer->t_holdtime); |
1662 | return 0; | |
718e3744 | 1663 | } |
1664 | ||
1665 | /* Update packet is received. */ | |
d62a17ae | 1666 | static int bgp_fsm_update(struct peer *peer) |
718e3744 | 1667 | { |
d62a17ae | 1668 | BGP_TIMER_OFF(peer->t_holdtime); |
1669 | return 0; | |
718e3744 | 1670 | } |
1671 | ||
1672 | /* This is empty event. */ | |
d62a17ae | 1673 | static int bgp_ignore(struct peer *peer) |
718e3744 | 1674 | { |
d62a17ae | 1675 | zlog_err( |
1676 | "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d", | |
1677 | peer->host, bgp_event_str[peer->cur_event], | |
1678 | lookup_msg(bgp_status_msg, peer->status, NULL), | |
1679 | bgp_event_str[peer->last_event], | |
1680 | bgp_event_str[peer->last_major_event], peer->fd); | |
1681 | return 0; | |
718e3744 | 1682 | } |
6b0655a2 | 1683 | |
6403814c | 1684 | /* This is to handle unexpected events.. */ |
d62a17ae | 1685 | static int bgp_fsm_exeption(struct peer *peer) |
6403814c | 1686 | { |
d62a17ae | 1687 | zlog_err( |
1688 | "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d", | |
1689 | peer->host, bgp_event_str[peer->cur_event], | |
1690 | lookup_msg(bgp_status_msg, peer->status, NULL), | |
1691 | bgp_event_str[peer->last_event], | |
1692 | bgp_event_str[peer->last_major_event], peer->fd); | |
1693 | return (bgp_stop(peer)); | |
6403814c DS |
1694 | } |
1695 | ||
d62a17ae | 1696 | void bgp_fsm_nht_update(struct peer *peer, int valid) |
fc9a856f | 1697 | { |
d62a17ae | 1698 | if (!peer) |
1699 | return; | |
1700 | ||
1701 | switch (peer->status) { | |
1702 | case Idle: | |
1703 | if (valid) | |
1704 | BGP_EVENT_ADD(peer, BGP_Start); | |
1705 | break; | |
1706 | case Connect: | |
1707 | if (!valid) { | |
1708 | BGP_TIMER_OFF(peer->t_connect); | |
1709 | BGP_EVENT_ADD(peer, TCP_fatal_error); | |
1710 | } | |
1711 | break; | |
1712 | case Active: | |
1713 | if (valid) { | |
1714 | BGP_TIMER_OFF(peer->t_connect); | |
1715 | BGP_EVENT_ADD(peer, ConnectRetry_timer_expired); | |
1716 | } | |
1717 | break; | |
1718 | case OpenSent: | |
1719 | case OpenConfirm: | |
1720 | case Established: | |
1721 | if (!valid && (peer->gtsm_hops == 1)) | |
1722 | BGP_EVENT_ADD(peer, TCP_fatal_error); | |
1723 | case Clearing: | |
1724 | case Deleted: | |
1725 | default: | |
1726 | break; | |
fc9a856f | 1727 | } |
fc9a856f DS |
1728 | } |
1729 | ||
1730 | ||
718e3744 | 1731 | /* Finite State Machine structure */ |
fda1d3e0 | 1732 | static const struct { |
d62a17ae | 1733 | int (*func)(struct peer *); |
1734 | int next_state; | |
1735 | } FSM[BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] = { | |
1736 | { | |
1737 | /* Idle state: In Idle state, all events other than BGP_Start is | |
1738 | ignored. With BGP_Start event, finite state machine calls | |
1739 | bgp_start(). */ | |
1740 | {bgp_start, Connect}, /* BGP_Start */ | |
1741 | {bgp_stop, Idle}, /* BGP_Stop */ | |
1742 | {bgp_stop, Idle}, /* TCP_connection_open */ | |
1743 | {bgp_stop, Idle}, /* TCP_connection_closed */ | |
1744 | {bgp_ignore, Idle}, /* TCP_connection_open_failed */ | |
1745 | {bgp_stop, Idle}, /* TCP_fatal_error */ | |
1746 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ | |
1747 | {bgp_ignore, Idle}, /* Hold_Timer_expired */ | |
1748 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ | |
1749 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ | |
1750 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ | |
1751 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ | |
1752 | {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */ | |
1753 | {bgp_ignore, Idle}, /* Clearing_Completed */ | |
1754 | }, | |
1755 | { | |
1756 | /* Connect */ | |
1757 | {bgp_ignore, Connect}, /* BGP_Start */ | |
1758 | {bgp_stop, Idle}, /* BGP_Stop */ | |
1759 | {bgp_connect_success, OpenSent}, /* TCP_connection_open */ | |
1760 | {bgp_stop, Idle}, /* TCP_connection_closed */ | |
1761 | {bgp_connect_fail, Active}, /* TCP_connection_open_failed */ | |
1762 | {bgp_connect_fail, Idle}, /* TCP_fatal_error */ | |
1763 | {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */ | |
1764 | {bgp_fsm_exeption, Idle}, /* Hold_Timer_expired */ | |
1765 | {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */ | |
1766 | {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */ | |
1767 | {bgp_fsm_exeption, Idle}, /* Receive_KEEPALIVE_message */ | |
1768 | {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */ | |
1769 | {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */ | |
1770 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
1771 | }, | |
1772 | { | |
1773 | /* Active, */ | |
1774 | {bgp_ignore, Active}, /* BGP_Start */ | |
1775 | {bgp_stop, Idle}, /* BGP_Stop */ | |
1776 | {bgp_connect_success, OpenSent}, /* TCP_connection_open */ | |
1777 | {bgp_stop, Idle}, /* TCP_connection_closed */ | |
1778 | {bgp_ignore, Active}, /* TCP_connection_open_failed */ | |
1779 | {bgp_fsm_exeption, Idle}, /* TCP_fatal_error */ | |
1780 | {bgp_start, Connect}, /* ConnectRetry_timer_expired */ | |
1781 | {bgp_fsm_exeption, Idle}, /* Hold_Timer_expired */ | |
1782 | {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */ | |
1783 | {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */ | |
1784 | {bgp_fsm_exeption, Idle}, /* Receive_KEEPALIVE_message */ | |
1785 | {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */ | |
1786 | {bgp_fsm_exeption, Idle}, /* Receive_NOTIFICATION_message */ | |
1787 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
1788 | }, | |
1789 | { | |
1790 | /* OpenSent, */ | |
1791 | {bgp_ignore, OpenSent}, /* BGP_Start */ | |
1792 | {bgp_stop, Idle}, /* BGP_Stop */ | |
1793 | {bgp_stop, Active}, /* TCP_connection_open */ | |
1794 | {bgp_stop, Active}, /* TCP_connection_closed */ | |
1795 | {bgp_stop, Active}, /* TCP_connection_open_failed */ | |
1796 | {bgp_stop, Active}, /* TCP_fatal_error */ | |
1797 | {bgp_fsm_exeption, Idle}, /* ConnectRetry_timer_expired */ | |
1798 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ | |
1799 | {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */ | |
1800 | {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */ | |
1801 | {bgp_fsm_event_error, Idle}, /* Receive_KEEPALIVE_message */ | |
1802 | {bgp_fsm_event_error, Idle}, /* Receive_UPDATE_message */ | |
1803 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ | |
1804 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
1805 | }, | |
1806 | { | |
1807 | /* OpenConfirm, */ | |
1808 | {bgp_ignore, OpenConfirm}, /* BGP_Start */ | |
1809 | {bgp_stop, Idle}, /* BGP_Stop */ | |
1810 | {bgp_stop, Idle}, /* TCP_connection_open */ | |
1811 | {bgp_stop, Idle}, /* TCP_connection_closed */ | |
1812 | {bgp_stop, Idle}, /* TCP_connection_open_failed */ | |
1813 | {bgp_stop, Idle}, /* TCP_fatal_error */ | |
1814 | {bgp_fsm_exeption, Idle}, /* ConnectRetry_timer_expired */ | |
1815 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ | |
1816 | {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */ | |
1817 | {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */ | |
1818 | {bgp_establish, Established}, /* Receive_KEEPALIVE_message */ | |
1819 | {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */ | |
1820 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ | |
1821 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
1822 | }, | |
1823 | { | |
1824 | /* Established, */ | |
1825 | {bgp_ignore, Established}, /* BGP_Start */ | |
1826 | {bgp_stop, Clearing}, /* BGP_Stop */ | |
1827 | {bgp_stop, Clearing}, /* TCP_connection_open */ | |
1828 | {bgp_stop, Clearing}, /* TCP_connection_closed */ | |
1829 | {bgp_stop, Clearing}, /* TCP_connection_open_failed */ | |
1830 | {bgp_stop, Clearing}, /* TCP_fatal_error */ | |
1831 | {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */ | |
1832 | {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */ | |
03014d48 QY |
1833 | {bgp_ignore, Established}, /* KeepAlive_timer_expired */ |
1834 | {bgp_stop, Clearing}, /* Receive_OPEN_message */ | |
d62a17ae | 1835 | {bgp_fsm_keepalive, |
1836 | Established}, /* Receive_KEEPALIVE_message */ | |
1837 | {bgp_fsm_update, Established}, /* Receive_UPDATE_message */ | |
1838 | {bgp_stop_with_error, | |
1839 | Clearing}, /* Receive_NOTIFICATION_message */ | |
1840 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
1841 | }, | |
1842 | { | |
1843 | /* Clearing, */ | |
1844 | {bgp_ignore, Clearing}, /* BGP_Start */ | |
1845 | {bgp_stop, Clearing}, /* BGP_Stop */ | |
1846 | {bgp_stop, Clearing}, /* TCP_connection_open */ | |
1847 | {bgp_stop, Clearing}, /* TCP_connection_closed */ | |
1848 | {bgp_stop, Clearing}, /* TCP_connection_open_failed */ | |
1849 | {bgp_stop, Clearing}, /* TCP_fatal_error */ | |
1850 | {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */ | |
1851 | {bgp_stop, Clearing}, /* Hold_Timer_expired */ | |
1852 | {bgp_stop, Clearing}, /* KeepAlive_timer_expired */ | |
1853 | {bgp_stop, Clearing}, /* Receive_OPEN_message */ | |
1854 | {bgp_stop, Clearing}, /* Receive_KEEPALIVE_message */ | |
1855 | {bgp_stop, Clearing}, /* Receive_UPDATE_message */ | |
1856 | {bgp_stop, Clearing}, /* Receive_NOTIFICATION_message */ | |
1857 | {bgp_clearing_completed, Idle}, /* Clearing_Completed */ | |
1858 | }, | |
1859 | { | |
1860 | /* Deleted, */ | |
1861 | {bgp_ignore, Deleted}, /* BGP_Start */ | |
1862 | {bgp_ignore, Deleted}, /* BGP_Stop */ | |
1863 | {bgp_ignore, Deleted}, /* TCP_connection_open */ | |
1864 | {bgp_ignore, Deleted}, /* TCP_connection_closed */ | |
1865 | {bgp_ignore, Deleted}, /* TCP_connection_open_failed */ | |
1866 | {bgp_ignore, Deleted}, /* TCP_fatal_error */ | |
1867 | {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */ | |
1868 | {bgp_ignore, Deleted}, /* Hold_Timer_expired */ | |
1869 | {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */ | |
1870 | {bgp_ignore, Deleted}, /* Receive_OPEN_message */ | |
1871 | {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */ | |
1872 | {bgp_ignore, Deleted}, /* Receive_UPDATE_message */ | |
1873 | {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */ | |
1874 | {bgp_ignore, Deleted}, /* Clearing_Completed */ | |
1875 | }, | |
718e3744 | 1876 | }; |
1877 | ||
718e3744 | 1878 | /* Execute event process. */ |
d62a17ae | 1879 | int bgp_event(struct thread *thread) |
718e3744 | 1880 | { |
d62a17ae | 1881 | int event; |
1882 | struct peer *peer; | |
1883 | int ret; | |
718e3744 | 1884 | |
d62a17ae | 1885 | peer = THREAD_ARG(thread); |
1886 | event = THREAD_VAL(thread); | |
718e3744 | 1887 | |
d62a17ae | 1888 | ret = bgp_event_update(peer, event); |
1ff9a340 | 1889 | |
d62a17ae | 1890 | return (ret); |
1ff9a340 DS |
1891 | } |
1892 | ||
d62a17ae | 1893 | int bgp_event_update(struct peer *peer, int event) |
1ff9a340 | 1894 | { |
d62a17ae | 1895 | int next; |
1896 | int ret = 0; | |
1897 | struct peer *other; | |
1898 | int passive_conn = 0; | |
1899 | int dyn_nbr; | |
1900 | ||
d8151687 QY |
1901 | /* default return code */ |
1902 | ret = FSM_PEER_NOOP; | |
1903 | ||
d62a17ae | 1904 | other = peer->doppelganger; |
1905 | passive_conn = | |
1906 | (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) ? 1 : 0; | |
1907 | dyn_nbr = peer_dynamic_neighbor(peer); | |
1908 | ||
1909 | /* Logging this event. */ | |
1910 | next = FSM[peer->status - 1][event - 1].next_state; | |
1911 | ||
1912 | if (bgp_debug_neighbor_events(peer) && peer->status != next) | |
1913 | zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer->host, | |
1914 | bgp_event_str[event], | |
1915 | lookup_msg(bgp_status_msg, peer->status, NULL), | |
1916 | lookup_msg(bgp_status_msg, next, NULL), peer->fd); | |
1917 | ||
1918 | peer->last_event = peer->cur_event; | |
1919 | peer->cur_event = event; | |
1920 | ||
1921 | /* Call function. */ | |
1922 | if (FSM[peer->status - 1][event - 1].func) | |
1923 | ret = (*(FSM[peer->status - 1][event - 1].func))(peer); | |
1924 | ||
d62a17ae | 1925 | if (ret >= 0) { |
1926 | if (ret == 1 && next == Established) { | |
1927 | /* The case when doppelganger swap accurred in | |
1928 | bgp_establish. | |
1929 | Update the peer pointer accordingly */ | |
d8151687 | 1930 | ret = FSM_PEER_TRANSFERRED; |
d62a17ae | 1931 | peer = other; |
1932 | } | |
1933 | ||
1934 | /* If status is changed. */ | |
d8151687 | 1935 | if (next != peer->status) { |
d62a17ae | 1936 | bgp_fsm_change_status(peer, next); |
1937 | ||
becedef6 QY |
1938 | /* |
1939 | * If we're going to ESTABLISHED then we executed a | |
1940 | * peer transfer. In this case we can either return | |
1941 | * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED. | |
1942 | * Opting for TRANSFERRED since transfer implies | |
1943 | * session establishment. | |
1944 | */ | |
d8151687 QY |
1945 | if (ret != FSM_PEER_TRANSFERRED) |
1946 | ret = FSM_PEER_TRANSITIONED; | |
1947 | } | |
1948 | ||
d62a17ae | 1949 | /* Make sure timer is set. */ |
1950 | bgp_timer_set(peer); | |
1951 | ||
bea01226 | 1952 | } else { |
becedef6 QY |
1953 | /* |
1954 | * If we got a return value of -1, that means there was an | |
1955 | * error, restart the FSM. Since bgp_stop() was called on the | |
1956 | * peer. only a few fields are safe to access here. In any case | |
1957 | * we need to indicate that the peer was stopped in the return | |
1958 | * code. | |
1959 | */ | |
bea01226 QY |
1960 | if (!dyn_nbr && !passive_conn && peer->bgp) { |
1961 | zlog_err( | |
1962 | "%s [FSM] Failure handling event %s in state %s, " | |
1963 | "prior events %s, %s, fd %d", | |
1964 | peer->host, bgp_event_str[peer->cur_event], | |
1965 | lookup_msg(bgp_status_msg, peer->status, NULL), | |
1966 | bgp_event_str[peer->last_event], | |
1967 | bgp_event_str[peer->last_major_event], | |
1968 | peer->fd); | |
1969 | bgp_stop(peer); | |
1970 | bgp_fsm_change_status(peer, Idle); | |
1971 | bgp_timer_set(peer); | |
1972 | } | |
1973 | ret = FSM_PEER_STOPPED; | |
d62a17ae | 1974 | } |
bea01226 | 1975 | |
d62a17ae | 1976 | return ret; |
718e3744 | 1977 | } |