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