]> git.ipfire.org Git - thirdparty/bird.git/blob - proto/bgp/bgp.c
Timers: Show sub-second times in some protocol outputs
[thirdparty/bird.git] / proto / bgp / bgp.c
1 /*
2 * BIRD -- The Border Gateway Protocol
3 *
4 * (c) 2000 Martin Mares <mj@ucw.cz>
5 * (c) 2008--2016 Ondrej Zajicek <santiago@crfreenet.org>
6 * (c) 2008--2016 CZ.NIC z.s.p.o.
7 *
8 * Can be freely distributed and used under the terms of the GNU GPL.
9 */
10
11 /**
12 * DOC: Border Gateway Protocol
13 *
14 * The BGP protocol is implemented in three parts: |bgp.c| which takes care of
15 * the connection and most of the interface with BIRD core, |packets.c| handling
16 * both incoming and outgoing BGP packets and |attrs.c| containing functions for
17 * manipulation with BGP attribute lists.
18 *
19 * As opposed to the other existing routing daemons, BIRD has a sophisticated
20 * core architecture which is able to keep all the information needed by BGP in
21 * the primary routing table, therefore no complex data structures like a
22 * central BGP table are needed. This increases memory footprint of a BGP router
23 * with many connections, but not too much and, which is more important, it
24 * makes BGP much easier to implement.
25 *
26 * Each instance of BGP (corresponding to a single BGP peer) is described by a
27 * &bgp_proto structure to which are attached individual connections represented
28 * by &bgp_connection (usually, there exists only one connection, but during BGP
29 * session setup, there can be more of them). The connections are handled
30 * according to the BGP state machine defined in the RFC with all the timers and
31 * all the parameters configurable.
32 *
33 * In incoming direction, we listen on the connection's socket and each time we
34 * receive some input, we pass it to bgp_rx(). It decodes packet headers and the
35 * markers and passes complete packets to bgp_rx_packet() which distributes the
36 * packet according to its type.
37 *
38 * In outgoing direction, we gather all the routing updates and sort them to
39 * buckets (&bgp_bucket) according to their attributes (we keep a hash table for
40 * fast comparison of &rta's and a &fib which helps us to find if we already
41 * have another route for the same destination queued for sending, so that we
42 * can replace it with the new one immediately instead of sending both
43 * updates). There also exists a special bucket holding all the route
44 * withdrawals which cannot be queued anywhere else as they don't have any
45 * attributes. If we have any packet to send (due to either new routes or the
46 * connection tracking code wanting to send a Open, Keepalive or Notification
47 * message), we call bgp_schedule_packet() which sets the corresponding bit in a
48 * @packet_to_send bit field in &bgp_conn and as soon as the transmit socket
49 * buffer becomes empty, we call bgp_fire_tx(). It inspects state of all the
50 * packet type bits and calls the corresponding bgp_create_xx() functions,
51 * eventually rescheduling the same packet type if we have more data of the same
52 * type to send.
53 *
54 * The processing of attributes consists of two functions: bgp_decode_attrs()
55 * for checking of the attribute blocks and translating them to the language of
56 * BIRD's extended attributes and bgp_encode_attrs() which does the
57 * converse. Both functions are built around a @bgp_attr_table array describing
58 * all important characteristics of all known attributes. Unknown transitive
59 * attributes are attached to the route as %EAF_TYPE_OPAQUE byte streams.
60 *
61 * BGP protocol implements graceful restart in both restarting (local restart)
62 * and receiving (neighbor restart) roles. The first is handled mostly by the
63 * graceful restart code in the nest, BGP protocol just handles capabilities,
64 * sets @gr_wait and locks graceful restart until end-of-RIB mark is received.
65 * The second is implemented by internal restart of the BGP state to %BS_IDLE
66 * and protocol state to %PS_START, but keeping the protocol up from the core
67 * point of view and therefore maintaining received routes. Routing table
68 * refresh cycle (rt_refresh_begin(), rt_refresh_end()) is used for removing
69 * stale routes after reestablishment of BGP session during graceful restart.
70 *
71 * Supported standards:
72 * <itemize>
73 * <item> <rfc id="4271"> - Border Gateway Protocol 4 (BGP)
74 * <item> <rfc id="1997"> - BGP Communities Attribute
75 * <item> <rfc id="2385"> - Protection of BGP Sessions via TCP MD5 Signature
76 * <item> <rfc id="2545"> - Use of BGP Multiprotocol Extensions for IPv6
77 * <item> <rfc id="2918"> - Route Refresh Capability
78 * <item> <rfc id="3107"> - Carrying Label Information in BGP
79 * <item> <rfc id="4360"> - BGP Extended Communities Attribute
80 * <item> <rfc id="4364"> - BGP/MPLS IPv4 Virtual Private Networks
81 * <item> <rfc id="4456"> - BGP Route Reflection
82 * <item> <rfc id="4486"> - Subcodes for BGP Cease Notification Message
83 * <item> <rfc id="4659"> - BGP/MPLS IPv6 Virtual Private Networks
84 * <item> <rfc id="4724"> - Graceful Restart Mechanism for BGP
85 * <item> <rfc id="4760"> - Multiprotocol extensions for BGP
86 * <item> <rfc id="4798"> - Connecting IPv6 Islands over IPv4 MPLS
87 * <item> <rfc id="5065"> - AS confederations for BGP
88 * <item> <rfc id="5082"> - Generalized TTL Security Mechanism
89 * <item> <rfc id="5492"> - Capabilities Advertisement with BGP
90 * <item> <rfc id="5549"> - Advertising IPv4 NLRI with an IPv6 Next Hop
91 * <item> <rfc id="5575"> - Dissemination of Flow Specification Rules
92 * <item> <rfc id="5668"> - 4-Octet AS Specific BGP Extended Community
93 * <item> <rfc id="6286"> - AS-Wide Unique BGP Identifier
94 * <item> <rfc id="6608"> - Subcodes for BGP Finite State Machine Error
95 * <item> <rfc id="6793"> - BGP Support for 4-Octet AS Numbers
96 * <item> <rfc id="7313"> - Enhanced Route Refresh Capability for BGP
97 * <item> <rfc id="7606"> - Revised Error Handling for BGP UPDATE Messages
98 * <item> <rfc id="7911"> - Advertisement of Multiple Paths in BGP
99 * <item> <rfc id="7947"> - Internet Exchange BGP Route Server
100 * <item> <rfc id="8092"> - BGP Large Communities Attribute
101 * </itemize>
102 */
103
104 #undef LOCAL_DEBUG
105
106 #include <stdlib.h>
107
108 #include "nest/bird.h"
109 #include "nest/iface.h"
110 #include "nest/protocol.h"
111 #include "nest/route.h"
112 #include "nest/cli.h"
113 #include "nest/locks.h"
114 #include "conf/conf.h"
115 #include "lib/socket.h"
116 #include "lib/resource.h"
117 #include "lib/string.h"
118
119 #include "bgp.h"
120
121
122 struct linpool *bgp_linpool; /* Global temporary pool */
123 struct linpool *bgp_linpool2; /* Global temporary pool for bgp_rt_notify() */
124 static list bgp_sockets; /* Global list of listening sockets */
125
126
127 static void bgp_connect(struct bgp_proto *p);
128 static void bgp_active(struct bgp_proto *p);
129 static void bgp_update_bfd(struct bgp_proto *p, int use_bfd);
130
131 static int bgp_incoming_connection(sock *sk, uint dummy UNUSED);
132 static void bgp_listen_sock_err(sock *sk UNUSED, int err);
133
134 /**
135 * bgp_open - open a BGP instance
136 * @p: BGP instance
137 *
138 * This function allocates and configures shared BGP resources, mainly listening
139 * sockets. Should be called as the last step during initialization (when lock
140 * is acquired and neighbor is ready). When error, caller should change state to
141 * PS_DOWN and return immediately.
142 */
143 static int
144 bgp_open(struct bgp_proto *p)
145 {
146 struct bgp_socket *bs = NULL;
147 struct iface *ifa = p->cf->strict_bind ? p->cf->iface : NULL;
148 ip_addr addr = p->cf->strict_bind ? p->cf->local_ip :
149 (ipa_is_ip4(p->cf->remote_ip) ? IPA_NONE4 : IPA_NONE6);
150 uint port = p->cf->local_port;
151
152 /* FIXME: Add some global init? */
153 if (!bgp_linpool)
154 init_list(&bgp_sockets);
155
156 /* We assume that cf->iface is defined iff cf->local_ip is link-local */
157
158 WALK_LIST(bs, bgp_sockets)
159 if (ipa_equal(bs->sk->saddr, addr) && (bs->sk->iface == ifa) && (bs->sk->sport == port))
160 {
161 bs->uc++;
162 p->sock = bs;
163 return 0;
164 }
165
166 sock *sk = sk_new(proto_pool);
167 sk->type = SK_TCP_PASSIVE;
168 sk->ttl = 255;
169 sk->saddr = addr;
170 sk->sport = port;
171 sk->flags = 0;
172 sk->tos = IP_PREC_INTERNET_CONTROL;
173 sk->rbsize = BGP_RX_BUFFER_SIZE;
174 sk->tbsize = BGP_TX_BUFFER_SIZE;
175 sk->rx_hook = bgp_incoming_connection;
176 sk->err_hook = bgp_listen_sock_err;
177
178 if (sk_open(sk) < 0)
179 goto err;
180
181 bs = mb_allocz(proto_pool, sizeof(struct bgp_socket));
182 bs->sk = sk;
183 bs->uc = 1;
184 p->sock = bs;
185
186 add_tail(&bgp_sockets, &bs->n);
187
188 if (!bgp_linpool)
189 {
190 bgp_linpool = lp_new_default(proto_pool);
191 bgp_linpool2 = lp_new_default(proto_pool);
192 }
193
194 return 0;
195
196 err:
197 sk_log_error(sk, p->p.name);
198 log(L_ERR "%s: Cannot open listening socket", p->p.name);
199 rfree(sk);
200 return -1;
201 }
202
203 /**
204 * bgp_close - close a BGP instance
205 * @p: BGP instance
206 *
207 * This function frees and deconfigures shared BGP resources.
208 */
209 static void
210 bgp_close(struct bgp_proto *p)
211 {
212 struct bgp_socket *bs = p->sock;
213
214 ASSERT(bs && bs->uc);
215
216 if (--bs->uc)
217 return;
218
219 rfree(bs->sk);
220 rem_node(&bs->n);
221 mb_free(bs);
222
223 if (!EMPTY_LIST(bgp_sockets))
224 return;
225
226 rfree(bgp_linpool);
227 bgp_linpool = NULL;
228
229 rfree(bgp_linpool2);
230 bgp_linpool2 = NULL;
231 }
232
233 static inline int
234 bgp_setup_auth(struct bgp_proto *p, int enable)
235 {
236 if (p->cf->password)
237 {
238 int rv = sk_set_md5_auth(p->sock->sk,
239 p->cf->local_ip, p->cf->remote_ip, p->cf->iface,
240 enable ? p->cf->password : NULL, p->cf->setkey);
241
242 if (rv < 0)
243 sk_log_error(p->sock->sk, p->p.name);
244
245 return rv;
246 }
247 else
248 return 0;
249 }
250
251 static inline struct bgp_channel *
252 bgp_find_channel(struct bgp_proto *p, u32 afi)
253 {
254 struct bgp_channel *c;
255 WALK_LIST(c, p->p.channels)
256 if (c->afi == afi)
257 return c;
258
259 return NULL;
260 }
261
262 static void
263 bgp_startup(struct bgp_proto *p)
264 {
265 BGP_TRACE(D_EVENTS, "Started");
266 p->start_state = BSS_CONNECT;
267
268 if (!p->cf->passive)
269 bgp_active(p);
270 }
271
272 static void
273 bgp_startup_timeout(timer *t)
274 {
275 bgp_startup(t->data);
276 }
277
278
279 static void
280 bgp_initiate(struct bgp_proto *p)
281 {
282 int err_val;
283
284 if (bgp_open(p) < 0)
285 { err_val = BEM_NO_SOCKET; goto err1; }
286
287 if (bgp_setup_auth(p, 1) < 0)
288 { err_val = BEM_INVALID_MD5; goto err2; }
289
290 if (p->cf->bfd)
291 bgp_update_bfd(p, p->cf->bfd);
292
293 if (p->startup_delay)
294 {
295 p->start_state = BSS_DELAY;
296 BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds due to errors", p->startup_delay);
297 bgp_start_timer(p->startup_timer, p->startup_delay);
298 }
299 else
300 bgp_startup(p);
301
302 return;
303
304 err2:
305 bgp_close(p);
306 err1:
307 p->p.disabled = 1;
308 bgp_store_error(p, NULL, BE_MISC, err_val);
309 proto_notify_state(&p->p, PS_DOWN);
310
311 return;
312 }
313
314 /**
315 * bgp_start_timer - start a BGP timer
316 * @t: timer
317 * @value: time to fire (0 to disable the timer)
318 *
319 * This functions calls tm_start() on @t with time @value and the amount of
320 * randomization suggested by the BGP standard. Please use it for all BGP
321 * timers.
322 */
323 void
324 bgp_start_timer(timer *t, int value)
325 {
326 if (value)
327 {
328 /* The randomization procedure is specified in RFC 1771: 9.2.3.3 */
329 int randomize = random() % ((value / 4) + 1);
330 tm_start(t, value - randomize);
331 }
332 else
333 tm_stop(t);
334 }
335
336 /**
337 * bgp_close_conn - close a BGP connection
338 * @conn: connection to close
339 *
340 * This function takes a connection described by the &bgp_conn structure, closes
341 * its socket and frees all resources associated with it.
342 */
343 void
344 bgp_close_conn(struct bgp_conn *conn)
345 {
346 // struct bgp_proto *p = conn->bgp;
347
348 DBG("BGP: Closing connection\n");
349 conn->packets_to_send = 0;
350 conn->channels_to_send = 0;
351 rfree(conn->connect_timer);
352 conn->connect_timer = NULL;
353 rfree(conn->keepalive_timer);
354 conn->keepalive_timer = NULL;
355 rfree(conn->hold_timer);
356 conn->hold_timer = NULL;
357 rfree(conn->tx_ev);
358 conn->tx_ev = NULL;
359 rfree(conn->sk);
360 conn->sk = NULL;
361
362 mb_free(conn->local_caps);
363 conn->local_caps = NULL;
364 mb_free(conn->remote_caps);
365 conn->remote_caps = NULL;
366 }
367
368
369 /**
370 * bgp_update_startup_delay - update a startup delay
371 * @p: BGP instance
372 *
373 * This function updates a startup delay that is used to postpone next BGP
374 * connect. It also handles disable_after_error and might stop BGP instance
375 * when error happened and disable_after_error is on.
376 *
377 * It should be called when BGP protocol error happened.
378 */
379 void
380 bgp_update_startup_delay(struct bgp_proto *p)
381 {
382 struct bgp_config *cf = p->cf;
383
384 DBG("BGP: Updating startup delay\n");
385
386 if (p->last_proto_error && ((now - p->last_proto_error) >= (int) cf->error_amnesia_time))
387 p->startup_delay = 0;
388
389 p->last_proto_error = now;
390
391 if (cf->disable_after_error)
392 {
393 p->startup_delay = 0;
394 p->p.disabled = 1;
395 return;
396 }
397
398 if (!p->startup_delay)
399 p->startup_delay = cf->error_delay_time_min;
400 else
401 p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max);
402 }
403
404 static void
405 bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode)
406 {
407 switch (conn->state)
408 {
409 case BS_IDLE:
410 case BS_CLOSE:
411 return;
412
413 case BS_CONNECT:
414 case BS_ACTIVE:
415 bgp_conn_enter_idle_state(conn);
416 return;
417
418 case BS_OPENSENT:
419 case BS_OPENCONFIRM:
420 case BS_ESTABLISHED:
421 bgp_error(conn, 6, subcode, NULL, 0);
422 return;
423
424 default:
425 bug("bgp_graceful_close_conn: Unknown state %d", conn->state);
426 }
427 }
428
429 static void
430 bgp_down(struct bgp_proto *p)
431 {
432 if (p->start_state > BSS_PREPARE)
433 {
434 bgp_setup_auth(p, 0);
435 bgp_close(p);
436 }
437
438 BGP_TRACE(D_EVENTS, "Down");
439 proto_notify_state(&p->p, PS_DOWN);
440 }
441
442 static void
443 bgp_decision(void *vp)
444 {
445 struct bgp_proto *p = vp;
446
447 DBG("BGP: Decision start\n");
448 if ((p->p.proto_state == PS_START) &&
449 (p->outgoing_conn.state == BS_IDLE) &&
450 (p->incoming_conn.state != BS_OPENCONFIRM) &&
451 !p->cf->passive)
452 bgp_active(p);
453
454 if ((p->p.proto_state == PS_STOP) &&
455 (p->outgoing_conn.state == BS_IDLE) &&
456 (p->incoming_conn.state == BS_IDLE))
457 bgp_down(p);
458 }
459
460 void
461 bgp_stop(struct bgp_proto *p, uint subcode)
462 {
463 proto_notify_state(&p->p, PS_STOP);
464 bgp_graceful_close_conn(&p->outgoing_conn, subcode);
465 bgp_graceful_close_conn(&p->incoming_conn, subcode);
466 ev_schedule(p->event);
467 }
468
469 static inline void
470 bgp_conn_set_state(struct bgp_conn *conn, uint new_state)
471 {
472 if (conn->bgp->p.mrtdump & MD_STATES)
473 mrt_dump_bgp_state_change(conn, conn->state, new_state);
474
475 conn->state = new_state;
476 }
477
478 void
479 bgp_conn_enter_openconfirm_state(struct bgp_conn *conn)
480 {
481 /* Really, most of the work is done in bgp_rx_open(). */
482 bgp_conn_set_state(conn, BS_OPENCONFIRM);
483 }
484
485 static const struct bgp_af_caps dummy_af_caps = { };
486
487 void
488 bgp_conn_enter_established_state(struct bgp_conn *conn)
489 {
490 struct bgp_proto *p = conn->bgp;
491 struct bgp_caps *local = conn->local_caps;
492 struct bgp_caps *peer = conn->remote_caps;
493 struct bgp_channel *c;
494
495 BGP_TRACE(D_EVENTS, "BGP session established");
496
497 /* For multi-hop BGP sessions */
498 if (ipa_zero(p->source_addr))
499 p->source_addr = conn->sk->saddr;
500
501 conn->sk->fast_rx = 0;
502
503 p->conn = conn;
504 p->last_error_class = 0;
505 p->last_error_code = 0;
506
507 p->as4_session = conn->as4_session;
508
509 p->route_refresh = peer->route_refresh;
510 p->enhanced_refresh = local->enhanced_refresh && peer->enhanced_refresh;
511
512 /* Whether we may handle possible GR of peer (it has some AF GR-able) */
513 p->gr_ready = 0; /* Updated later */
514
515 /* Whether peer is ready to handle our GR recovery */
516 int peer_gr_ready = peer->gr_aware && !(peer->gr_flags & BGP_GRF_RESTART);
517
518 if (p->gr_active_num)
519 tm_stop(p->gr_timer);
520
521 /* Number of active channels */
522 int num = 0;
523
524 WALK_LIST(c, p->p.channels)
525 {
526 const struct bgp_af_caps *loc = bgp_find_af_caps(local, c->afi);
527 const struct bgp_af_caps *rem = bgp_find_af_caps(peer, c->afi);
528
529 /* Ignore AFIs that were not announced in multiprotocol capability */
530 if (!loc || !loc->ready)
531 loc = &dummy_af_caps;
532
533 if (!rem || !rem->ready)
534 rem = &dummy_af_caps;
535
536 int active = loc->ready && rem->ready;
537 c->c.disabled = !active;
538 c->c.reloadable = p->route_refresh;
539
540 c->index = active ? num++ : 0;
541
542 c->feed_state = BFS_NONE;
543 c->load_state = BFS_NONE;
544
545 /* Channels where peer may do GR */
546 c->gr_ready = active && local->gr_aware && rem->gr_able;
547 p->gr_ready = p->gr_ready || c->gr_ready;
548
549 /* Channels not able to recover gracefully */
550 if (p->p.gr_recovery && (!active || !peer_gr_ready))
551 channel_graceful_restart_unlock(&c->c);
552
553 /* Channels waiting for local convergence */
554 if (p->p.gr_recovery && loc->gr_able && peer_gr_ready)
555 c->c.gr_wait = 1;
556
557 /* Channels where peer is not able to recover gracefully */
558 if (c->gr_active && ! (c->gr_ready && (rem->gr_af_flags & BGP_GRF_FORWARDING)))
559 bgp_graceful_restart_done(c);
560
561 /* GR capability implies that neighbor will send End-of-RIB */
562 if (peer->gr_aware)
563 c->load_state = BFS_LOADING;
564
565 c->ext_next_hop = c->cf->ext_next_hop && (bgp_channel_is_ipv6(c) || rem->ext_next_hop);
566 c->add_path_rx = (loc->add_path & BGP_ADD_PATH_RX) && (rem->add_path & BGP_ADD_PATH_TX);
567 c->add_path_tx = (loc->add_path & BGP_ADD_PATH_TX) && (rem->add_path & BGP_ADD_PATH_RX);
568
569 /* Update RA mode */
570 if (c->add_path_tx)
571 c->c.ra_mode = RA_ANY;
572 else if (c->cf->secondary)
573 c->c.ra_mode = RA_ACCEPTED;
574 else
575 c->c.ra_mode = RA_OPTIMAL;
576 }
577
578 p->afi_map = mb_alloc(p->p.pool, num * sizeof(u32));
579 p->channel_map = mb_alloc(p->p.pool, num * sizeof(void *));
580 p->channel_count = num;
581
582 WALK_LIST(c, p->p.channels)
583 {
584 if (c->c.disabled)
585 continue;
586
587 p->afi_map[c->index] = c->afi;
588 p->channel_map[c->index] = c;
589 }
590
591 /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */
592
593 bgp_conn_set_state(conn, BS_ESTABLISHED);
594 proto_notify_state(&p->p, PS_UP);
595 }
596
597 static void
598 bgp_conn_leave_established_state(struct bgp_proto *p)
599 {
600 BGP_TRACE(D_EVENTS, "BGP session closed");
601 p->conn = NULL;
602
603 // XXXX free these tables to avoid memory leak during graceful restart
604 // bgp_free_prefix_table(p);
605 // bgp_free_bucket_table(p);
606
607 if (p->p.proto_state == PS_UP)
608 bgp_stop(p, 0);
609 }
610
611 void
612 bgp_conn_enter_close_state(struct bgp_conn *conn)
613 {
614 struct bgp_proto *p = conn->bgp;
615 int os = conn->state;
616
617 bgp_conn_set_state(conn, BS_CLOSE);
618 tm_stop(conn->keepalive_timer);
619 conn->sk->rx_hook = NULL;
620
621 /* Timeout for CLOSE state, if we cannot send notification soon then we just hangup */
622 bgp_start_timer(conn->hold_timer, 10);
623
624 if (os == BS_ESTABLISHED)
625 bgp_conn_leave_established_state(p);
626 }
627
628 void
629 bgp_conn_enter_idle_state(struct bgp_conn *conn)
630 {
631 struct bgp_proto *p = conn->bgp;
632 int os = conn->state;
633
634 bgp_close_conn(conn);
635 bgp_conn_set_state(conn, BS_IDLE);
636 ev_schedule(p->event);
637
638 if (os == BS_ESTABLISHED)
639 bgp_conn_leave_established_state(p);
640 }
641
642 /**
643 * bgp_handle_graceful_restart - handle detected BGP graceful restart
644 * @p: BGP instance
645 *
646 * This function is called when a BGP graceful restart of the neighbor is
647 * detected (when the TCP connection fails or when a new TCP connection
648 * appears). The function activates processing of the restart - starts routing
649 * table refresh cycle and activates BGP restart timer. The protocol state goes
650 * back to %PS_START, but changing BGP state back to %BS_IDLE is left for the
651 * caller.
652 */
653 void
654 bgp_handle_graceful_restart(struct bgp_proto *p)
655 {
656 ASSERT(p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready);
657
658 BGP_TRACE(D_EVENTS, "Neighbor graceful restart detected%s",
659 p->gr_active_num ? " - already pending" : "");
660
661 p->gr_active_num = 0;
662
663 struct bgp_channel *c;
664 WALK_LIST(c, p->p.channels)
665 {
666 if (c->gr_ready)
667 {
668 if (c->gr_active)
669 rt_refresh_end(c->c.table, &c->c);
670
671 c->gr_active = 1;
672 p->gr_active_num++;
673 rt_refresh_begin(c->c.table, &c->c);
674 }
675 else
676 {
677 /* Just flush the routes */
678 rt_refresh_begin(c->c.table, &c->c);
679 rt_refresh_end(c->c.table, &c->c);
680 }
681 }
682
683 proto_notify_state(&p->p, PS_START);
684 bgp_start_timer(p->gr_timer, p->conn->local_caps->gr_time);
685 }
686
687 /**
688 * bgp_graceful_restart_done - finish active BGP graceful restart
689 * @c: BGP channel
690 *
691 * This function is called when the active BGP graceful restart of the neighbor
692 * should be finished for channel @c - either successfully (the neighbor sends
693 * all paths and reports end-of-RIB for given AFI/SAFI on the new session) or
694 * unsuccessfully (the neighbor does not support BGP graceful restart on the new
695 * session). The function ends the routing table refresh cycle.
696 */
697 void
698 bgp_graceful_restart_done(struct bgp_channel *c)
699 {
700 struct bgp_proto *p = (void *) c->c.proto;
701
702 ASSERT(c->gr_active);
703 c->gr_active = 0;
704 p->gr_active_num--;
705
706 if (!p->gr_active_num)
707 BGP_TRACE(D_EVENTS, "Neighbor graceful restart done");
708
709 rt_refresh_end(c->c.table, &c->c);
710 }
711
712 /**
713 * bgp_graceful_restart_timeout - timeout of graceful restart 'restart timer'
714 * @t: timer
715 *
716 * This function is a timeout hook for @gr_timer, implementing BGP restart time
717 * limit for reestablisment of the BGP session after the graceful restart. When
718 * fired, we just proceed with the usual protocol restart.
719 */
720
721 static void
722 bgp_graceful_restart_timeout(timer *t)
723 {
724 struct bgp_proto *p = t->data;
725
726 BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout");
727 bgp_stop(p, 0);
728 }
729
730
731 /**
732 * bgp_refresh_begin - start incoming enhanced route refresh sequence
733 * @c: BGP channel
734 *
735 * This function is called when an incoming enhanced route refresh sequence is
736 * started by the neighbor, demarcated by the BoRR packet. The function updates
737 * the load state and starts the routing table refresh cycle. Note that graceful
738 * restart also uses routing table refresh cycle, but RFC 7313 and load states
739 * ensure that these two sequences do not overlap.
740 */
741 void
742 bgp_refresh_begin(struct bgp_channel *c)
743 {
744 struct bgp_proto *p = (void *) c->c.proto;
745
746 if (c->load_state == BFS_LOADING)
747 { log(L_WARN "%s: BEGIN-OF-RR received before END-OF-RIB, ignoring", p->p.name); return; }
748
749 c->load_state = BFS_REFRESHING;
750 rt_refresh_begin(c->c.table, &c->c);
751 }
752
753 /**
754 * bgp_refresh_end - finish incoming enhanced route refresh sequence
755 * @c: BGP channel
756 *
757 * This function is called when an incoming enhanced route refresh sequence is
758 * finished by the neighbor, demarcated by the EoRR packet. The function updates
759 * the load state and ends the routing table refresh cycle. Routes not received
760 * during the sequence are removed by the nest.
761 */
762 void
763 bgp_refresh_end(struct bgp_channel *c)
764 {
765 struct bgp_proto *p = (void *) c->c.proto;
766
767 if (c->load_state != BFS_REFRESHING)
768 { log(L_WARN "%s: END-OF-RR received without prior BEGIN-OF-RR, ignoring", p->p.name); return; }
769
770 c->load_state = BFS_NONE;
771 rt_refresh_end(c->c.table, &c->c);
772 }
773
774
775 static void
776 bgp_send_open(struct bgp_conn *conn)
777 {
778 DBG("BGP: Sending open\n");
779 conn->sk->rx_hook = bgp_rx;
780 conn->sk->tx_hook = bgp_tx;
781 tm_stop(conn->connect_timer);
782 bgp_schedule_packet(conn, NULL, PKT_OPEN);
783 bgp_conn_set_state(conn, BS_OPENSENT);
784 bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
785 }
786
787 static void
788 bgp_connected(sock *sk)
789 {
790 struct bgp_conn *conn = sk->data;
791 struct bgp_proto *p = conn->bgp;
792
793 BGP_TRACE(D_EVENTS, "Connected");
794 bgp_send_open(conn);
795 }
796
797 static void
798 bgp_connect_timeout(timer *t)
799 {
800 struct bgp_conn *conn = t->data;
801 struct bgp_proto *p = conn->bgp;
802
803 DBG("BGP: connect_timeout\n");
804 if (p->p.proto_state == PS_START)
805 {
806 bgp_close_conn(conn);
807 bgp_connect(p);
808 }
809 else
810 bgp_conn_enter_idle_state(conn);
811 }
812
813 static void
814 bgp_sock_err(sock *sk, int err)
815 {
816 struct bgp_conn *conn = sk->data;
817 struct bgp_proto *p = conn->bgp;
818
819 /*
820 * This error hook may be called either asynchronously from main
821 * loop, or synchronously from sk_send(). But sk_send() is called
822 * only from bgp_tx() and bgp_kick_tx(), which are both called
823 * asynchronously from main loop. Moreover, they end if err hook is
824 * called. Therefore, we could suppose that it is always called
825 * asynchronously.
826 */
827
828 bgp_store_error(p, conn, BE_SOCKET, err);
829
830 if (err)
831 BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
832 else
833 BGP_TRACE(D_EVENTS, "Connection closed");
834
835 if ((conn->state == BS_ESTABLISHED) && p->gr_ready)
836 bgp_handle_graceful_restart(p);
837
838 bgp_conn_enter_idle_state(conn);
839 }
840
841 static void
842 bgp_hold_timeout(timer *t)
843 {
844 struct bgp_conn *conn = t->data;
845 struct bgp_proto *p = conn->bgp;
846
847 DBG("BGP: Hold timeout\n");
848
849 /* We are already closing the connection - just do hangup */
850 if (conn->state == BS_CLOSE)
851 {
852 BGP_TRACE(D_EVENTS, "Connection stalled");
853 bgp_conn_enter_idle_state(conn);
854 return;
855 }
856
857 /* If there is something in input queue, we are probably congested
858 and perhaps just not processed BGP packets in time. */
859
860 if (sk_rx_ready(conn->sk) > 0)
861 bgp_start_timer(conn->hold_timer, 10);
862 else
863 bgp_error(conn, 4, 0, NULL, 0);
864 }
865
866 static void
867 bgp_keepalive_timeout(timer *t)
868 {
869 struct bgp_conn *conn = t->data;
870
871 DBG("BGP: Keepalive timer\n");
872 bgp_schedule_packet(conn, NULL, PKT_KEEPALIVE);
873
874 /* Kick TX a bit faster */
875 if (ev_active(conn->tx_ev))
876 ev_run(conn->tx_ev);
877 }
878
879 static void
880 bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
881 {
882 conn->sk = NULL;
883 conn->bgp = p;
884
885 conn->packets_to_send = 0;
886 conn->channels_to_send = 0;
887 conn->last_channel = 0;
888 conn->last_channel_count = 0;
889
890 conn->connect_timer = tm_new_set(p->p.pool, bgp_connect_timeout, conn, 0, 0);
891 conn->hold_timer = tm_new_set(p->p.pool, bgp_hold_timeout, conn, 0, 0);
892 conn->keepalive_timer = tm_new_set(p->p.pool, bgp_keepalive_timeout, conn, 0, 0);
893
894 conn->tx_ev = ev_new(p->p.pool);
895 conn->tx_ev->hook = bgp_kick_tx;
896 conn->tx_ev->data = conn;
897 }
898
899 static void
900 bgp_setup_sk(struct bgp_conn *conn, sock *s)
901 {
902 s->data = conn;
903 s->err_hook = bgp_sock_err;
904 s->fast_rx = 1;
905 conn->sk = s;
906 }
907
908 static void
909 bgp_active(struct bgp_proto *p)
910 {
911 int delay = MAX(1, p->cf->connect_delay_time);
912 struct bgp_conn *conn = &p->outgoing_conn;
913
914 BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
915 bgp_setup_conn(p, conn);
916 bgp_conn_set_state(conn, BS_ACTIVE);
917 bgp_start_timer(conn->connect_timer, delay);
918 }
919
920 /**
921 * bgp_connect - initiate an outgoing connection
922 * @p: BGP instance
923 *
924 * The bgp_connect() function creates a new &bgp_conn and initiates
925 * a TCP connection to the peer. The rest of connection setup is governed
926 * by the BGP state machine as described in the standard.
927 */
928 static void
929 bgp_connect(struct bgp_proto *p) /* Enter Connect state and start establishing connection */
930 {
931 struct bgp_conn *conn = &p->outgoing_conn;
932 int hops = p->cf->multihop ? : 1;
933
934 DBG("BGP: Connecting\n");
935 sock *s = sk_new(p->p.pool);
936 s->type = SK_TCP_ACTIVE;
937 s->saddr = p->source_addr;
938 s->daddr = p->cf->remote_ip;
939 s->dport = p->cf->remote_port;
940 s->iface = p->neigh ? p->neigh->iface : NULL;
941 s->ttl = p->cf->ttl_security ? 255 : hops;
942 s->rbsize = p->cf->enable_extended_messages ? BGP_RX_BUFFER_EXT_SIZE : BGP_RX_BUFFER_SIZE;
943 s->tbsize = p->cf->enable_extended_messages ? BGP_TX_BUFFER_EXT_SIZE : BGP_TX_BUFFER_SIZE;
944 s->tos = IP_PREC_INTERNET_CONTROL;
945 s->password = p->cf->password;
946 s->tx_hook = bgp_connected;
947 BGP_TRACE(D_EVENTS, "Connecting to %I%J from local address %I%J", s->daddr, p->cf->iface,
948 s->saddr, ipa_is_link_local(s->saddr) ? s->iface : NULL);
949 bgp_setup_conn(p, conn);
950 bgp_setup_sk(conn, s);
951 bgp_conn_set_state(conn, BS_CONNECT);
952
953 if (sk_open(s) < 0)
954 goto err;
955
956 /* Set minimal receive TTL if needed */
957 if (p->cf->ttl_security)
958 if (sk_set_min_ttl(s, 256 - hops) < 0)
959 goto err;
960
961 DBG("BGP: Waiting for connect success\n");
962 bgp_start_timer(conn->connect_timer, p->cf->connect_retry_time);
963 return;
964
965 err:
966 sk_log_error(s, p->p.name);
967 bgp_sock_err(s, 0);
968 return;
969 }
970
971 /**
972 * bgp_find_proto - find existing proto for incoming connection
973 * @sk: TCP socket
974 *
975 */
976 static struct bgp_proto *
977 bgp_find_proto(sock *sk)
978 {
979 struct bgp_proto *p;
980
981 WALK_LIST(p, proto_list)
982 if ((p->p.proto == &proto_bgp) &&
983 ipa_equal(p->cf->remote_ip, sk->daddr) &&
984 (!p->cf->iface || (p->cf->iface == sk->iface)) &&
985 (ipa_zero(p->cf->local_ip) || ipa_equal(p->cf->local_ip, sk->saddr)) &&
986 (p->cf->local_port == sk->sport))
987 return p;
988
989 return NULL;
990 }
991
992 /**
993 * bgp_incoming_connection - handle an incoming connection
994 * @sk: TCP socket
995 * @dummy: unused
996 *
997 * This function serves as a socket hook for accepting of new BGP
998 * connections. It searches a BGP instance corresponding to the peer
999 * which has connected and if such an instance exists, it creates a
1000 * &bgp_conn structure, attaches it to the instance and either sends
1001 * an Open message or (if there already is an active connection) it
1002 * closes the new connection by sending a Notification message.
1003 */
1004 static int
1005 bgp_incoming_connection(sock *sk, uint dummy UNUSED)
1006 {
1007 struct bgp_proto *p;
1008 int acc, hops;
1009
1010 DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
1011 p = bgp_find_proto(sk);
1012 if (!p)
1013 {
1014 log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)",
1015 sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, sk->dport);
1016 rfree(sk);
1017 return 0;
1018 }
1019
1020 /*
1021 * BIRD should keep multiple incoming connections in OpenSent state (for
1022 * details RFC 4271 8.2.1 par 3), but it keeps just one. Duplicate incoming
1023 * connections are rejected istead. The exception is the case where an
1024 * incoming connection triggers a graceful restart.
1025 */
1026
1027 acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
1028 (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
1029
1030 if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
1031 {
1032 bgp_store_error(p, NULL, BE_MISC, BEM_GRACEFUL_RESTART);
1033 bgp_handle_graceful_restart(p);
1034 bgp_conn_enter_idle_state(p->conn);
1035 acc = 1;
1036
1037 /* There might be separate incoming connection in OpenSent state */
1038 if (p->incoming_conn.state > BS_ACTIVE)
1039 bgp_close_conn(&p->incoming_conn);
1040 }
1041
1042 BGP_TRACE(D_EVENTS, "Incoming connection from %I%J (port %d) %s",
1043 sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL,
1044 sk->dport, acc ? "accepted" : "rejected");
1045
1046 if (!acc)
1047 {
1048 rfree(sk);
1049 return 0;
1050 }
1051
1052 hops = p->cf->multihop ? : 1;
1053
1054 if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0)
1055 goto err;
1056
1057 if (p->cf->ttl_security)
1058 if (sk_set_min_ttl(sk, 256 - hops) < 0)
1059 goto err;
1060
1061 if (p->cf->enable_extended_messages)
1062 {
1063 sk->rbsize = BGP_RX_BUFFER_EXT_SIZE;
1064 sk->tbsize = BGP_TX_BUFFER_EXT_SIZE;
1065 sk_reallocate(sk);
1066 }
1067
1068 bgp_setup_conn(p, &p->incoming_conn);
1069 bgp_setup_sk(&p->incoming_conn, sk);
1070 bgp_send_open(&p->incoming_conn);
1071 return 0;
1072
1073 err:
1074 sk_log_error(sk, p->p.name);
1075 log(L_ERR "%s: Incoming connection aborted", p->p.name);
1076 rfree(sk);
1077 return 0;
1078 }
1079
1080 static void
1081 bgp_listen_sock_err(sock *sk UNUSED, int err)
1082 {
1083 if (err == ECONNABORTED)
1084 log(L_WARN "BGP: Incoming connection aborted");
1085 else
1086 log(L_ERR "BGP: Error on listening socket: %M", err);
1087 }
1088
1089 static void
1090 bgp_start_neighbor(struct bgp_proto *p)
1091 {
1092 /* Called only for single-hop BGP sessions */
1093
1094 if (ipa_zero(p->source_addr))
1095 p->source_addr = p->neigh->ifa->ip;
1096
1097 if (ipa_is_link_local(p->source_addr))
1098 p->link_addr = p->source_addr;
1099 else if (p->neigh->iface->llv6)
1100 p->link_addr = p->neigh->iface->llv6->ip;
1101
1102 bgp_initiate(p);
1103 }
1104
1105 static void
1106 bgp_neigh_notify(neighbor *n)
1107 {
1108 struct bgp_proto *p = (struct bgp_proto *) n->proto;
1109 int ps = p->p.proto_state;
1110
1111 if (n != p->neigh)
1112 return;
1113
1114 if ((ps == PS_DOWN) || (ps == PS_STOP))
1115 return;
1116
1117 int prepare = (ps == PS_START) && (p->start_state == BSS_PREPARE);
1118
1119 if (n->scope <= 0)
1120 {
1121 if (!prepare)
1122 {
1123 BGP_TRACE(D_EVENTS, "Neighbor lost");
1124 bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST);
1125 /* Perhaps also run bgp_update_startup_delay(p)? */
1126 bgp_stop(p, 0);
1127 }
1128 }
1129 else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1130 {
1131 if (!prepare)
1132 {
1133 BGP_TRACE(D_EVENTS, "Link down");
1134 bgp_store_error(p, NULL, BE_MISC, BEM_LINK_DOWN);
1135 if (ps == PS_UP)
1136 bgp_update_startup_delay(p);
1137 bgp_stop(p, 0);
1138 }
1139 }
1140 else
1141 {
1142 if (prepare)
1143 {
1144 BGP_TRACE(D_EVENTS, "Neighbor ready");
1145 bgp_start_neighbor(p);
1146 }
1147 }
1148 }
1149
1150 static void
1151 bgp_bfd_notify(struct bfd_request *req)
1152 {
1153 struct bgp_proto *p = req->data;
1154 int ps = p->p.proto_state;
1155
1156 if (req->down && ((ps == PS_START) || (ps == PS_UP)))
1157 {
1158 BGP_TRACE(D_EVENTS, "BFD session down");
1159 bgp_store_error(p, NULL, BE_MISC, BEM_BFD_DOWN);
1160 if (ps == PS_UP)
1161 bgp_update_startup_delay(p);
1162 bgp_stop(p, 0);
1163 }
1164 }
1165
1166 static void
1167 bgp_update_bfd(struct bgp_proto *p, int use_bfd)
1168 {
1169 if (use_bfd && !p->bfd_req)
1170 p->bfd_req = bfd_request_session(p->p.pool, p->cf->remote_ip, p->source_addr,
1171 p->cf->multihop ? NULL : p->neigh->iface,
1172 bgp_bfd_notify, p);
1173
1174 if (!use_bfd && p->bfd_req)
1175 {
1176 rfree(p->bfd_req);
1177 p->bfd_req = NULL;
1178 }
1179 }
1180
1181 static void
1182 bgp_reload_routes(struct channel *C)
1183 {
1184 struct bgp_proto *p = (void *) C->proto;
1185 struct bgp_channel *c = (void *) C;
1186
1187 ASSERT(p->conn && p->route_refresh);
1188
1189 bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH);
1190 }
1191
1192 static void
1193 bgp_feed_begin(struct channel *C, int initial)
1194 {
1195 struct bgp_proto *p = (void *) C->proto;
1196 struct bgp_channel *c = (void *) C;
1197
1198 /* This should not happen */
1199 if (!p->conn)
1200 return;
1201
1202 if (initial && p->cf->gr_mode)
1203 c->feed_state = BFS_LOADING;
1204
1205 /* It is refeed and both sides support enhanced route refresh */
1206 if (!initial && p->enhanced_refresh)
1207 {
1208 /* BoRR must not be sent before End-of-RIB */
1209 if (c->feed_state == BFS_LOADING || c->feed_state == BFS_LOADED)
1210 return;
1211
1212 c->feed_state = BFS_REFRESHING;
1213 bgp_schedule_packet(p->conn, c, PKT_BEGIN_REFRESH);
1214 }
1215 }
1216
1217 static void
1218 bgp_feed_end(struct channel *C)
1219 {
1220 struct bgp_proto *p = (void *) C->proto;
1221 struct bgp_channel *c = (void *) C;
1222
1223 /* This should not happen */
1224 if (!p->conn)
1225 return;
1226
1227 /* Non-demarcated feed ended, nothing to do */
1228 if (c->feed_state == BFS_NONE)
1229 return;
1230
1231 /* Schedule End-of-RIB packet */
1232 if (c->feed_state == BFS_LOADING)
1233 c->feed_state = BFS_LOADED;
1234
1235 /* Schedule EoRR packet */
1236 if (c->feed_state == BFS_REFRESHING)
1237 c->feed_state = BFS_REFRESHED;
1238
1239 /* Kick TX hook */
1240 bgp_schedule_packet(p->conn, c, PKT_UPDATE);
1241 }
1242
1243
1244 static void
1245 bgp_start_locked(struct object_lock *lock)
1246 {
1247 struct bgp_proto *p = lock->data;
1248 struct bgp_config *cf = p->cf;
1249
1250 if (p->p.proto_state != PS_START)
1251 {
1252 DBG("BGP: Got lock in different state %d\n", p->p.proto_state);
1253 return;
1254 }
1255
1256 DBG("BGP: Got lock\n");
1257
1258 if (cf->multihop)
1259 {
1260 /* Multi-hop sessions do not use neighbor entries */
1261 bgp_initiate(p);
1262 return;
1263 }
1264
1265 neighbor *n = neigh_find2(&p->p, &cf->remote_ip, cf->iface, NEF_STICKY);
1266 if (!n)
1267 {
1268 log(L_ERR "%s: Invalid remote address %I%J", p->p.name, cf->remote_ip, cf->iface);
1269 /* As we do not start yet, we can just disable protocol */
1270 p->p.disabled = 1;
1271 bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_NEXT_HOP);
1272 proto_notify_state(&p->p, PS_DOWN);
1273 return;
1274 }
1275
1276 p->neigh = n;
1277
1278 if (n->scope <= 0)
1279 BGP_TRACE(D_EVENTS, "Waiting for %I%J to become my neighbor", cf->remote_ip, cf->iface);
1280 else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1281 BGP_TRACE(D_EVENTS, "Waiting for link on %s", n->iface->name);
1282 else
1283 bgp_start_neighbor(p);
1284 }
1285
1286 static int
1287 bgp_start(struct proto *P)
1288 {
1289 struct bgp_proto *p = (struct bgp_proto *) P;
1290 struct object_lock *lock;
1291
1292 DBG("BGP: Startup.\n");
1293 p->start_state = BSS_PREPARE;
1294 p->outgoing_conn.state = BS_IDLE;
1295 p->incoming_conn.state = BS_IDLE;
1296 p->neigh = NULL;
1297 p->bfd_req = NULL;
1298 p->gr_ready = 0;
1299 p->gr_active_num = 0;
1300
1301 p->event = ev_new(p->p.pool);
1302 p->event->hook = bgp_decision;
1303 p->event->data = p;
1304
1305 p->startup_timer = tm_new(p->p.pool);
1306 p->startup_timer->hook = bgp_startup_timeout;
1307 p->startup_timer->data = p;
1308
1309 p->gr_timer = tm_new(p->p.pool);
1310 p->gr_timer->hook = bgp_graceful_restart_timeout;
1311 p->gr_timer->data = p;
1312
1313 p->local_id = proto_get_router_id(P->cf);
1314 if (p->rr_client)
1315 p->rr_cluster_id = p->cf->rr_cluster_id ? p->cf->rr_cluster_id : p->local_id;
1316
1317 p->remote_id = 0;
1318 p->source_addr = p->cf->local_ip;
1319 p->link_addr = IPA_NONE;
1320
1321 /* XXXX */
1322 if (p->p.gr_recovery && p->cf->gr_mode)
1323 {
1324 struct bgp_channel *c;
1325 WALK_LIST(c, p->p.channels)
1326 channel_graceful_restart_lock(&c->c);
1327 }
1328
1329 /*
1330 * Before attempting to create the connection, we need to lock the port,
1331 * so that we are the only instance attempting to talk with that neighbor.
1332 */
1333
1334 lock = p->lock = olock_new(P->pool);
1335 lock->addr = p->cf->remote_ip;
1336 lock->port = p->cf->remote_port;
1337 lock->iface = p->cf->iface;
1338 lock->type = OBJLOCK_TCP;
1339 lock->hook = bgp_start_locked;
1340 lock->data = p;
1341 olock_acquire(lock);
1342
1343 return PS_START;
1344 }
1345
1346 extern int proto_restart;
1347
1348 static int
1349 bgp_shutdown(struct proto *P)
1350 {
1351 struct bgp_proto *p = (struct bgp_proto *) P;
1352 uint subcode = 0;
1353
1354 BGP_TRACE(D_EVENTS, "Shutdown requested");
1355
1356 switch (P->down_code)
1357 {
1358 case PDC_CF_REMOVE:
1359 case PDC_CF_DISABLE:
1360 subcode = 3; // Errcode 6, 3 - peer de-configured
1361 break;
1362
1363 case PDC_CF_RESTART:
1364 subcode = 6; // Errcode 6, 6 - other configuration change
1365 break;
1366
1367 case PDC_CMD_DISABLE:
1368 case PDC_CMD_SHUTDOWN:
1369 subcode = 2; // Errcode 6, 2 - administrative shutdown
1370 break;
1371
1372 case PDC_CMD_RESTART:
1373 subcode = 4; // Errcode 6, 4 - administrative reset
1374 break;
1375
1376 case PDC_RX_LIMIT_HIT:
1377 case PDC_IN_LIMIT_HIT:
1378 subcode = 1; // Errcode 6, 1 - max number of prefixes reached
1379 /* log message for compatibility */
1380 log(L_WARN "%s: Route limit exceeded, shutting down", p->p.name);
1381 goto limit;
1382
1383 case PDC_OUT_LIMIT_HIT:
1384 subcode = proto_restart ? 4 : 2; // Administrative reset or shutdown
1385
1386 limit:
1387 bgp_store_error(p, NULL, BE_AUTO_DOWN, BEA_ROUTE_LIMIT_EXCEEDED);
1388 if (proto_restart)
1389 bgp_update_startup_delay(p);
1390 else
1391 p->startup_delay = 0;
1392 goto done;
1393 }
1394
1395 bgp_store_error(p, NULL, BE_MAN_DOWN, 0);
1396 p->startup_delay = 0;
1397
1398 done:
1399 bgp_stop(p, subcode);
1400 return p->p.proto_state;
1401 }
1402
1403 static struct proto *
1404 bgp_init(struct proto_config *CF)
1405 {
1406 struct proto *P = proto_new(CF);
1407 struct bgp_proto *p = (struct bgp_proto *) P;
1408 struct bgp_config *cf = (struct bgp_config *) CF;
1409
1410 P->rt_notify = bgp_rt_notify;
1411 P->import_control = bgp_import_control;
1412 P->neigh_notify = bgp_neigh_notify;
1413 P->reload_routes = bgp_reload_routes;
1414 P->feed_begin = bgp_feed_begin;
1415 P->feed_end = bgp_feed_end;
1416 P->rte_better = bgp_rte_better;
1417 P->rte_mergable = bgp_rte_mergable;
1418 P->rte_recalculate = cf->deterministic_med ? bgp_rte_recalculate : NULL;
1419
1420 p->cf = cf;
1421 p->local_as = cf->local_as;
1422 p->remote_as = cf->remote_as;
1423 p->public_as = cf->local_as;
1424 p->is_internal = (cf->local_as == cf->remote_as);
1425 p->is_interior = p->is_internal || cf->confederation_member;
1426 p->rs_client = cf->rs_client;
1427 p->rr_client = cf->rr_client;
1428
1429 /* Confederation ID is used for truly external peers */
1430 if (cf->confederation && !p->is_interior)
1431 p->public_as = cf->confederation;
1432
1433 /* Add all channels */
1434 struct bgp_channel_config *cc;
1435 WALK_LIST(cc, CF->channels)
1436 proto_add_channel(P, &cc->c);
1437
1438 return P;
1439 }
1440
1441 static void
1442 bgp_channel_init(struct channel *C, struct channel_config *CF)
1443 {
1444 struct bgp_channel *c = (void *) C;
1445 struct bgp_channel_config *cf = (void *) CF;
1446
1447 c->cf = cf;
1448 c->afi = cf->afi;
1449 c->desc = cf->desc;
1450
1451 if (cf->igp_table_ip4)
1452 c->igp_table_ip4 = cf->igp_table_ip4->table;
1453
1454 if (cf->igp_table_ip6)
1455 c->igp_table_ip6 = cf->igp_table_ip6->table;
1456 }
1457
1458 static int
1459 bgp_channel_start(struct channel *C)
1460 {
1461 struct bgp_proto *p = (void *) C->proto;
1462 struct bgp_channel *c = (void *) C;
1463 ip_addr src = p->source_addr;
1464
1465 if (c->igp_table_ip4)
1466 rt_lock_table(c->igp_table_ip4);
1467
1468 if (c->igp_table_ip6)
1469 rt_lock_table(c->igp_table_ip6);
1470
1471 c->pool = p->p.pool; // XXXX
1472 bgp_init_bucket_table(c);
1473 bgp_init_prefix_table(c);
1474
1475 c->next_hop_addr = c->cf->next_hop_addr;
1476 c->link_addr = IPA_NONE;
1477 c->packets_to_send = 0;
1478
1479 /* Try to use source address as next hop address */
1480 if (ipa_zero(c->next_hop_addr))
1481 {
1482 if (bgp_channel_is_ipv4(c) && (ipa_is_ip4(src) || c->ext_next_hop))
1483 c->next_hop_addr = src;
1484
1485 if (bgp_channel_is_ipv6(c) && (ipa_is_ip6(src) || c->ext_next_hop))
1486 c->next_hop_addr = src;
1487 }
1488
1489 /* Exit if no feasible next hop address is found */
1490 if (ipa_zero(c->next_hop_addr))
1491 {
1492 log(L_WARN "%s: Missing next hop address", p->p.name);
1493 return 0;
1494 }
1495
1496 /* Set link-local address for IPv6 single-hop BGP */
1497 if (ipa_is_ip6(c->next_hop_addr) && p->neigh)
1498 {
1499 c->link_addr = p->link_addr;
1500
1501 if (ipa_zero(c->link_addr))
1502 log(L_WARN "%s: Missing link-local address", p->p.name);
1503 }
1504
1505 /* Link local address is already in c->link_addr */
1506 if (ipa_is_link_local(c->next_hop_addr))
1507 c->next_hop_addr = IPA_NONE;
1508
1509 return 0; /* XXXX: Currently undefined */
1510 }
1511
1512 static void
1513 bgp_channel_shutdown(struct channel *C)
1514 {
1515 struct bgp_channel *c = (void *) C;
1516
1517 /* XXXX: cleanup bucket and prefix tables */
1518
1519 c->next_hop_addr = IPA_NONE;
1520 c->link_addr = IPA_NONE;
1521 }
1522
1523 static void
1524 bgp_channel_cleanup(struct channel *C)
1525 {
1526 struct bgp_channel *c = (void *) C;
1527
1528 if (c->igp_table_ip4)
1529 rt_unlock_table(c->igp_table_ip4);
1530
1531 if (c->igp_table_ip6)
1532 rt_unlock_table(c->igp_table_ip6);
1533 }
1534
1535 static inline struct bgp_channel_config *
1536 bgp_find_channel_config(struct bgp_config *cf, u32 afi)
1537 {
1538 struct bgp_channel_config *cc;
1539
1540 WALK_LIST(cc, cf->c.channels)
1541 if (cc->afi == afi)
1542 return cc;
1543
1544 return NULL;
1545 }
1546
1547 struct rtable_config *
1548 bgp_default_igp_table(struct bgp_config *cf, struct bgp_channel_config *cc, u32 type)
1549 {
1550 struct bgp_channel_config *cc2;
1551 struct rtable_config *tab;
1552
1553 /* First, try table connected by the channel */
1554 if (cc->c.table->addr_type == type)
1555 return cc->c.table;
1556
1557 /* Find paired channel with the same SAFI but the other AFI */
1558 u32 afi2 = cc->afi ^ 0x30000;
1559 cc2 = bgp_find_channel_config(cf, afi2);
1560
1561 /* Second, try IGP table configured in the paired channel */
1562 if (cc2 && (tab = (type == NET_IP4) ? cc2->igp_table_ip4 : cc2->igp_table_ip6))
1563 return tab;
1564
1565 /* Third, try table connected by the paired channel */
1566 if (cc2 && (cc2->c.table->addr_type == type))
1567 return cc2->c.table;
1568
1569 /* Last, try default table of given type */
1570 if (tab = cf->c.global->def_tables[type])
1571 return tab;
1572
1573 cf_error("Undefined IGP table");
1574 }
1575
1576
1577 void
1578 bgp_postconfig(struct proto_config *CF)
1579 {
1580 struct bgp_config *cf = (void *) CF;
1581 int internal = (cf->local_as == cf->remote_as);
1582
1583 /* Do not check templates at all */
1584 if (cf->c.class == SYM_TEMPLATE)
1585 return;
1586
1587
1588 /* EBGP direct by default, IBGP multihop by default */
1589 if (cf->multihop < 0)
1590 cf->multihop = internal ? 64 : 0;
1591
1592
1593 if (!cf->local_as)
1594 cf_error("Local AS number must be set");
1595
1596 if (ipa_zero(cf->remote_ip))
1597 cf_error("Neighbor must be configured");
1598
1599 if (!cf->remote_as)
1600 cf_error("Remote AS number must be set");
1601
1602 if (ipa_is_link_local(cf->remote_ip) && !cf->iface)
1603 cf_error("Link-local neighbor address requires specified interface");
1604
1605 if (!(cf->capabilities && cf->enable_as4) && (cf->remote_as > 0xFFFF))
1606 cf_error("Neighbor AS number out of range (AS4 not available)");
1607
1608 if (!internal && cf->rr_client)
1609 cf_error("Only internal neighbor can be RR client");
1610
1611 if (internal && cf->rs_client)
1612 cf_error("Only external neighbor can be RS client");
1613
1614 if (!cf->confederation && cf->confederation_member)
1615 cf_error("Confederation ID must be set for member sessions");
1616
1617 if (cf->multihop && (ipa_is_link_local(cf->local_ip) ||
1618 ipa_is_link_local(cf->remote_ip)))
1619 cf_error("Multihop BGP cannot be used with link-local addresses");
1620
1621 if (cf->multihop && cf->iface)
1622 cf_error("Multihop BGP cannot be bound to interface");
1623
1624 if (cf->multihop && cf->check_link)
1625 cf_error("Multihop BGP cannot depend on link state");
1626
1627 if (cf->multihop && cf->bfd && ipa_zero(cf->local_ip))
1628 cf_error("Multihop BGP with BFD requires specified local address");
1629
1630
1631 struct bgp_channel_config *cc;
1632 WALK_LIST(cc, CF->channels)
1633 {
1634 /* Disable after error incompatible with restart limit action */
1635 if ((cc->c.in_limit.action == PLA_RESTART) && cf->disable_after_error)
1636 cc->c.in_limit.action = PLA_DISABLE;
1637
1638 /* Different default based on rs_client */
1639 if (!cc->missing_lladdr)
1640 cc->missing_lladdr = cf->rs_client ? MLL_IGNORE : MLL_SELF;
1641
1642 /* Different default for gw_mode */
1643 if (!cc->gw_mode)
1644 cc->gw_mode = cf->multihop ? GW_RECURSIVE : GW_DIRECT;
1645
1646 /* Default based on proto config */
1647 if (cc->gr_able == 0xff)
1648 cc->gr_able = (cf->gr_mode == BGP_GR_ABLE);
1649
1650 /* Default values of IGP tables */
1651 if ((cc->gw_mode == GW_RECURSIVE) && !cc->desc->no_igp)
1652 {
1653 if (!cc->igp_table_ip4 && (bgp_cc_is_ipv4(cc) || cc->ext_next_hop))
1654 cc->igp_table_ip4 = bgp_default_igp_table(cf, cc, NET_IP4);
1655
1656 if (!cc->igp_table_ip6 && (bgp_cc_is_ipv6(cc) || cc->ext_next_hop))
1657 cc->igp_table_ip6 = bgp_default_igp_table(cf, cc, NET_IP6);
1658
1659 if (cc->igp_table_ip4 && bgp_cc_is_ipv6(cc) && !cc->ext_next_hop)
1660 cf_error("Mismatched IGP table type");
1661
1662 if (cc->igp_table_ip6 && bgp_cc_is_ipv4(cc) && !cc->ext_next_hop)
1663 cf_error("Mismatched IGP table type");
1664 }
1665
1666 if (cf->multihop && (cc->gw_mode == GW_DIRECT))
1667 cf_error("Multihop BGP cannot use direct gateway mode");
1668
1669 if ((cc->gw_mode == GW_RECURSIVE) && cc->c.table->sorted)
1670 cf_error("BGP in recursive mode prohibits sorted table");
1671
1672 if (cf->deterministic_med && cc->c.table->sorted)
1673 cf_error("BGP with deterministic MED prohibits sorted table");
1674
1675 if (cc->secondary && !cc->c.table->sorted)
1676 cf_error("BGP with secondary option requires sorted table");
1677 }
1678 }
1679
1680 static int
1681 bgp_reconfigure(struct proto *P, struct proto_config *CF)
1682 {
1683 struct bgp_proto *p = (void *) P;
1684 struct bgp_config *new = (void *) CF;
1685 struct bgp_config *old = p->cf;
1686
1687 if (proto_get_router_id(CF) != p->local_id)
1688 return 0;
1689
1690 int same = !memcmp(((byte *) old) + sizeof(struct proto_config),
1691 ((byte *) new) + sizeof(struct proto_config),
1692 // password item is last and must be checked separately
1693 OFFSETOF(struct bgp_config, password) - sizeof(struct proto_config))
1694 && ((!old->password && !new->password)
1695 || (old->password && new->password && !strcmp(old->password, new->password)));
1696
1697 /* FIXME: Move channel reconfiguration to generic protocol code ? */
1698 struct channel *C, *C2;
1699 struct bgp_channel_config *cc;
1700
1701 WALK_LIST(C, p->p.channels)
1702 C->stale = 1;
1703
1704 WALK_LIST(cc, new->c.channels)
1705 {
1706 C = (struct channel *) bgp_find_channel(p, cc->afi);
1707 same = proto_configure_channel(P, &C, &cc->c) && same;
1708 C->stale = 0;
1709 }
1710
1711 WALK_LIST_DELSAFE(C, C2, p->p.channels)
1712 if (C->stale)
1713 same = proto_configure_channel(P, &C, NULL) && same;
1714
1715
1716 if (same && (p->start_state > BSS_PREPARE))
1717 bgp_update_bfd(p, new->bfd);
1718
1719 /* We should update our copy of configuration ptr as old configuration will be freed */
1720 if (same)
1721 p->cf = new;
1722
1723 return same;
1724 }
1725
1726 #define IGP_TABLE(cf, sym) ((cf)->igp_table_##sym ? (cf)->igp_table_##sym ->table : NULL )
1727
1728 static int
1729 bgp_channel_reconfigure(struct channel *C, struct channel_config *CC)
1730 {
1731 struct bgp_channel *c = (void *) C;
1732 struct bgp_channel_config *new = (void *) CC;
1733 struct bgp_channel_config *old = c->cf;
1734
1735 if (memcmp(((byte *) old) + sizeof(struct channel_config),
1736 ((byte *) new) + sizeof(struct channel_config),
1737 /* Remaining items must be checked separately */
1738 OFFSETOF(struct bgp_channel_config, rest) - sizeof(struct channel_config)))
1739 return 0;
1740
1741 /* Check change in IGP tables */
1742 if ((IGP_TABLE(old, ip4) != IGP_TABLE(new, ip4)) ||
1743 (IGP_TABLE(old, ip6) != IGP_TABLE(new, ip6)))
1744 return 0;
1745
1746 c->cf = new;
1747 return 1;
1748 }
1749
1750 static void
1751 bgp_copy_config(struct proto_config *dest UNUSED, struct proto_config *src UNUSED)
1752 {
1753 /* Just a shallow copy */
1754 }
1755
1756
1757 /**
1758 * bgp_error - report a protocol error
1759 * @c: connection
1760 * @code: error code (according to the RFC)
1761 * @subcode: error sub-code
1762 * @data: data to be passed in the Notification message
1763 * @len: length of the data
1764 *
1765 * bgp_error() sends a notification packet to tell the other side that a protocol
1766 * error has occurred (including the data considered erroneous if possible) and
1767 * closes the connection.
1768 */
1769 void
1770 bgp_error(struct bgp_conn *c, uint code, uint subcode, byte *data, int len)
1771 {
1772 struct bgp_proto *p = c->bgp;
1773
1774 if (c->state == BS_CLOSE)
1775 return;
1776
1777 bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, data, ABS(len));
1778 bgp_store_error(p, c, BE_BGP_TX, (code << 16) | subcode);
1779 bgp_conn_enter_close_state(c);
1780
1781 c->notify_code = code;
1782 c->notify_subcode = subcode;
1783 c->notify_data = data;
1784 c->notify_size = (len > 0) ? len : 0;
1785 bgp_schedule_packet(c, NULL, PKT_NOTIFICATION);
1786
1787 if (code != 6)
1788 {
1789 bgp_update_startup_delay(p);
1790 bgp_stop(p, 0);
1791 }
1792 }
1793
1794 /**
1795 * bgp_store_error - store last error for status report
1796 * @p: BGP instance
1797 * @c: connection
1798 * @class: error class (BE_xxx constants)
1799 * @code: error code (class specific)
1800 *
1801 * bgp_store_error() decides whether given error is interesting enough
1802 * and store that error to last_error variables of @p
1803 */
1804 void
1805 bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code)
1806 {
1807 /* During PS_UP, we ignore errors on secondary connection */
1808 if ((p->p.proto_state == PS_UP) && c && (c != p->conn))
1809 return;
1810
1811 /* During PS_STOP, we ignore any errors, as we want to report
1812 * the error that caused transition to PS_STOP
1813 */
1814 if (p->p.proto_state == PS_STOP)
1815 return;
1816
1817 p->last_error_class = class;
1818 p->last_error_code = code;
1819 }
1820
1821 static char *bgp_state_names[] = { "Idle", "Connect", "Active", "OpenSent", "OpenConfirm", "Established", "Close" };
1822 static char *bgp_err_classes[] = { "", "Error: ", "Socket: ", "Received: ", "BGP Error: ", "Automatic shutdown: ", ""};
1823 static char *bgp_misc_errors[] = { "", "Neighbor lost", "Invalid next hop", "Kernel MD5 auth failed", "No listening socket", "Link down", "BFD session down", "Graceful restart"};
1824 static char *bgp_auto_errors[] = { "", "Route limit exceeded"};
1825
1826 static const char *
1827 bgp_last_errmsg(struct bgp_proto *p)
1828 {
1829 switch (p->last_error_class)
1830 {
1831 case BE_MISC:
1832 return bgp_misc_errors[p->last_error_code];
1833 case BE_SOCKET:
1834 return (p->last_error_code == 0) ? "Connection closed" : strerror(p->last_error_code);
1835 case BE_BGP_RX:
1836 case BE_BGP_TX:
1837 return bgp_error_dsc(p->last_error_code >> 16, p->last_error_code & 0xFF);
1838 case BE_AUTO_DOWN:
1839 return bgp_auto_errors[p->last_error_code];
1840 default:
1841 return "";
1842 }
1843 }
1844
1845 static const char *
1846 bgp_state_dsc(struct bgp_proto *p)
1847 {
1848 if (p->p.proto_state == PS_DOWN)
1849 return "Down";
1850
1851 int state = MAX(p->incoming_conn.state, p->outgoing_conn.state);
1852 if ((state == BS_IDLE) && (p->start_state >= BSS_CONNECT) && p->cf->passive)
1853 return "Passive";
1854
1855 return bgp_state_names[state];
1856 }
1857
1858 static void
1859 bgp_get_status(struct proto *P, byte *buf)
1860 {
1861 struct bgp_proto *p = (struct bgp_proto *) P;
1862
1863 const char *err1 = bgp_err_classes[p->last_error_class];
1864 const char *err2 = bgp_last_errmsg(p);
1865
1866 if (P->proto_state == PS_DOWN)
1867 bsprintf(buf, "%s%s", err1, err2);
1868 else
1869 bsprintf(buf, "%-14s%s%s", bgp_state_dsc(p), err1, err2);
1870 }
1871
1872 static void
1873 bgp_show_afis(int code, char *s, u32 *afis, uint count)
1874 {
1875 buffer b;
1876 LOG_BUFFER_INIT(b);
1877
1878 buffer_puts(&b, s);
1879
1880 for (u32 *af = afis; af < (afis + count); af++)
1881 {
1882 const struct bgp_af_desc *desc = bgp_get_af_desc(*af);
1883 if (desc)
1884 buffer_print(&b, " %s", desc->name);
1885 else
1886 buffer_print(&b, " <%u/%u>", BGP_AFI(*af), BGP_SAFI(*af));
1887 }
1888
1889 if (b.pos == b.end)
1890 strcpy(b.end - 32, " ... <too long>");
1891
1892 cli_msg(code, b.start);
1893 }
1894
1895 static void
1896 bgp_show_capabilities(struct bgp_proto *p UNUSED, struct bgp_caps *caps)
1897 {
1898 struct bgp_af_caps *ac;
1899 uint any_mp_bgp = 0;
1900 uint any_gr_able = 0;
1901 uint any_add_path = 0;
1902 uint any_ext_next_hop = 0;
1903 u32 *afl1 = alloca(caps->af_count * sizeof(u32));
1904 u32 *afl2 = alloca(caps->af_count * sizeof(u32));
1905 uint afn1, afn2;
1906
1907 WALK_AF_CAPS(caps, ac)
1908 {
1909 any_mp_bgp |= ac->ready;
1910 any_gr_able |= ac->gr_able;
1911 any_add_path |= ac->add_path;
1912 any_ext_next_hop |= ac->ext_next_hop;
1913 }
1914
1915 if (any_mp_bgp)
1916 {
1917 cli_msg(-1006, " Multiprotocol");
1918
1919 afn1 = 0;
1920 WALK_AF_CAPS(caps, ac)
1921 if (ac->ready)
1922 afl1[afn1++] = ac->afi;
1923
1924 bgp_show_afis(-1006, " AF announced:", afl1, afn1);
1925 }
1926
1927 if (caps->route_refresh)
1928 cli_msg(-1006, " Route refresh");
1929
1930 if (any_ext_next_hop)
1931 {
1932 cli_msg(-1006, " Extended next hop");
1933
1934 afn1 = 0;
1935 WALK_AF_CAPS(caps, ac)
1936 if (ac->ext_next_hop)
1937 afl1[afn1++] = ac->afi;
1938
1939 bgp_show_afis(-1006, " IPv6 nexthop:", afl1, afn1);
1940 }
1941
1942 if (caps->ext_messages)
1943 cli_msg(-1006, " Extended message");
1944
1945 if (caps->gr_aware)
1946 cli_msg(-1006, " Graceful restart");
1947
1948 if (any_gr_able)
1949 {
1950 /* Continues from gr_aware */
1951 cli_msg(-1006, " Restart time: %u", caps->gr_time);
1952 if (caps->gr_flags & BGP_GRF_RESTART)
1953 cli_msg(-1006, " Restart recovery");
1954
1955 afn1 = afn2 = 0;
1956 WALK_AF_CAPS(caps, ac)
1957 {
1958 if (ac->gr_able)
1959 afl1[afn1++] = ac->afi;
1960
1961 if (ac->gr_af_flags & BGP_GRF_FORWARDING)
1962 afl2[afn2++] = ac->afi;
1963 }
1964
1965 bgp_show_afis(-1006, " AF supported:", afl1, afn1);
1966 bgp_show_afis(-1006, " AF preserved:", afl2, afn2);
1967 }
1968
1969 if (caps->as4_support)
1970 cli_msg(-1006, " 4-octet AS numbers");
1971
1972 if (any_add_path)
1973 {
1974 cli_msg(-1006, " ADD-PATH");
1975
1976 afn1 = afn2 = 0;
1977 WALK_AF_CAPS(caps, ac)
1978 {
1979 if (ac->add_path & BGP_ADD_PATH_RX)
1980 afl1[afn1++] = ac->afi;
1981
1982 if (ac->add_path & BGP_ADD_PATH_TX)
1983 afl2[afn2++] = ac->afi;
1984 }
1985
1986 bgp_show_afis(-1006, " RX:", afl1, afn1);
1987 bgp_show_afis(-1006, " TX:", afl2, afn2);
1988 }
1989
1990 if (caps->enhanced_refresh)
1991 cli_msg(-1006, " Enhanced refresh");
1992 }
1993
1994 static void
1995 bgp_show_proto_info(struct proto *P)
1996 {
1997 struct bgp_proto *p = (struct bgp_proto *) P;
1998
1999 cli_msg(-1006, " BGP state: %s", bgp_state_dsc(p));
2000 cli_msg(-1006, " Neighbor address: %I%J", p->cf->remote_ip, p->cf->iface);
2001 cli_msg(-1006, " Neighbor AS: %u", p->remote_as);
2002
2003 if (p->gr_active_num)
2004 cli_msg(-1006, " Neighbor graceful restart active");
2005
2006 if (P->proto_state == PS_START)
2007 {
2008 struct bgp_conn *oc = &p->outgoing_conn;
2009
2010 if ((p->start_state < BSS_CONNECT) &&
2011 (tm_active(p->startup_timer)))
2012 cli_msg(-1006, " Error wait: %t/%u",
2013 tm2_remains(p->startup_timer), p->startup_delay);
2014
2015 if ((oc->state == BS_ACTIVE) &&
2016 (tm_active(oc->connect_timer)))
2017 cli_msg(-1006, " Connect delay: %t/%u",
2018 tm2_remains(oc->connect_timer), p->cf->connect_delay_time);
2019
2020 if (p->gr_active_num && tm_active(p->gr_timer))
2021 cli_msg(-1006, " Restart timer: %t/-",
2022 tm2_remains(p->gr_timer));
2023 }
2024 else if (P->proto_state == PS_UP)
2025 {
2026 cli_msg(-1006, " Neighbor ID: %R", p->remote_id);
2027 cli_msg(-1006, " Local capabilities");
2028 bgp_show_capabilities(p, p->conn->local_caps);
2029 cli_msg(-1006, " Neighbor capabilities");
2030 bgp_show_capabilities(p, p->conn->remote_caps);
2031 /* XXXX
2032 cli_msg(-1006, " Session: %s%s%s%s%s%s%s%s",
2033 p->is_internal ? "internal" : "external",
2034 p->cf->multihop ? " multihop" : "",
2035 p->rr_client ? " route-reflector" : "",
2036 p->rs_client ? " route-server" : "",
2037 p->as4_session ? " AS4" : "",
2038 p->add_path_rx ? " add-path-rx" : "",
2039 p->add_path_tx ? " add-path-tx" : "",
2040 p->ext_messages ? " ext-messages" : "");
2041 */
2042 cli_msg(-1006, " Source address: %I", p->source_addr);
2043 cli_msg(-1006, " Hold timer: %t/%u",
2044 tm2_remains(p->conn->hold_timer), p->conn->hold_time);
2045 cli_msg(-1006, " Keepalive timer: %t/%u",
2046 tm2_remains(p->conn->keepalive_timer), p->conn->keepalive_time);
2047 }
2048
2049 if ((p->last_error_class != BE_NONE) &&
2050 (p->last_error_class != BE_MAN_DOWN))
2051 {
2052 const char *err1 = bgp_err_classes[p->last_error_class];
2053 const char *err2 = bgp_last_errmsg(p);
2054 cli_msg(-1006, " Last error: %s%s", err1, err2);
2055 }
2056
2057 {
2058 /* XXXX ?? */
2059 struct bgp_channel *c;
2060 WALK_LIST(c, p->p.channels)
2061 {
2062 channel_show_info(&c->c);
2063
2064 if (c->igp_table_ip4)
2065 cli_msg(-1006, " IGP IPv4 table: %s", c->igp_table_ip4->name);
2066
2067 if (c->igp_table_ip6)
2068 cli_msg(-1006, " IGP IPv6 table: %s", c->igp_table_ip6->name);
2069 }
2070 }
2071 }
2072
2073 struct channel_class channel_bgp = {
2074 .channel_size = sizeof(struct bgp_channel),
2075 .config_size = sizeof(struct bgp_channel_config),
2076 .init = bgp_channel_init,
2077 .start = bgp_channel_start,
2078 .shutdown = bgp_channel_shutdown,
2079 .cleanup = bgp_channel_cleanup,
2080 .reconfigure = bgp_channel_reconfigure,
2081 };
2082
2083 struct protocol proto_bgp = {
2084 .name = "BGP",
2085 .template = "bgp%d",
2086 .attr_class = EAP_BGP,
2087 .preference = DEF_PREF_BGP,
2088 .channel_mask = NB_IP | NB_VPN | NB_FLOW,
2089 .proto_size = sizeof(struct bgp_proto),
2090 .config_size = sizeof(struct bgp_config),
2091 .postconfig = bgp_postconfig,
2092 .init = bgp_init,
2093 .start = bgp_start,
2094 .shutdown = bgp_shutdown,
2095 .reconfigure = bgp_reconfigure,
2096 .copy_config = bgp_copy_config,
2097 .get_status = bgp_get_status,
2098 .get_attr = bgp_get_attr,
2099 .get_route_info = bgp_get_route_info,
2100 .show_proto_info = bgp_show_proto_info
2101 };