2 * BIRD -- The Border Gateway Protocol
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.
8 * Can be freely distributed and used under the terms of the GNU GPL.
12 * DOC: Border Gateway Protocol
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.
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.
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.
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.
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
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.
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.
71 * Supported standards:
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
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"
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 */
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
);
131 static int bgp_incoming_connection(sock
*sk
, uint dummy UNUSED
);
132 static void bgp_listen_sock_err(sock
*sk UNUSED
, int err
);
135 * bgp_open - open a BGP instance
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.
144 bgp_open(struct bgp_proto
*p
)
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
;
152 /* FIXME: Add some global init? */
154 init_list(&bgp_sockets
);
156 /* We assume that cf->iface is defined iff cf->local_ip is link-local */
158 WALK_LIST(bs
, bgp_sockets
)
159 if (ipa_equal(bs
->sk
->saddr
, addr
) && (bs
->sk
->iface
== ifa
) && (bs
->sk
->sport
== port
))
166 sock
*sk
= sk_new(proto_pool
);
167 sk
->type
= SK_TCP_PASSIVE
;
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
;
181 bs
= mb_allocz(proto_pool
, sizeof(struct bgp_socket
));
186 add_tail(&bgp_sockets
, &bs
->n
);
190 bgp_linpool
= lp_new_default(proto_pool
);
191 bgp_linpool2
= lp_new_default(proto_pool
);
197 sk_log_error(sk
, p
->p
.name
);
198 log(L_ERR
"%s: Cannot open listening socket", p
->p
.name
);
204 * bgp_close - close a BGP instance
207 * This function frees and deconfigures shared BGP resources.
210 bgp_close(struct bgp_proto
*p
)
212 struct bgp_socket
*bs
= p
->sock
;
214 ASSERT(bs
&& bs
->uc
);
223 if (!EMPTY_LIST(bgp_sockets
))
234 bgp_setup_auth(struct bgp_proto
*p
, int enable
)
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
);
243 sk_log_error(p
->sock
->sk
, p
->p
.name
);
251 static inline struct bgp_channel
*
252 bgp_find_channel(struct bgp_proto
*p
, u32 afi
)
254 struct bgp_channel
*c
;
255 WALK_LIST(c
, p
->p
.channels
)
263 bgp_startup(struct bgp_proto
*p
)
265 BGP_TRACE(D_EVENTS
, "Started");
266 p
->start_state
= BSS_CONNECT
;
273 bgp_startup_timeout(timer
*t
)
275 bgp_startup(t
->data
);
280 bgp_initiate(struct bgp_proto
*p
)
285 { err_val
= BEM_NO_SOCKET
; goto err1
; }
287 if (bgp_setup_auth(p
, 1) < 0)
288 { err_val
= BEM_INVALID_MD5
; goto err2
; }
291 bgp_update_bfd(p
, p
->cf
->bfd
);
293 if (p
->startup_delay
)
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
);
308 bgp_store_error(p
, NULL
, BE_MISC
, err_val
);
309 proto_notify_state(&p
->p
, PS_DOWN
);
315 * bgp_start_timer - start a BGP timer
317 * @value: time to fire (0 to disable the timer)
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
324 bgp_start_timer(timer
*t
, int value
)
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
);
337 * bgp_close_conn - close a BGP connection
338 * @conn: connection to close
340 * This function takes a connection described by the &bgp_conn structure, closes
341 * its socket and frees all resources associated with it.
344 bgp_close_conn(struct bgp_conn
*conn
)
346 // struct bgp_proto *p = conn->bgp;
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
;
362 mb_free(conn
->local_caps
);
363 conn
->local_caps
= NULL
;
364 mb_free(conn
->remote_caps
);
365 conn
->remote_caps
= NULL
;
370 * bgp_update_startup_delay - update a startup delay
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.
377 * It should be called when BGP protocol error happened.
380 bgp_update_startup_delay(struct bgp_proto
*p
)
382 struct bgp_config
*cf
= p
->cf
;
384 DBG("BGP: Updating startup delay\n");
386 if (p
->last_proto_error
&& ((now
- p
->last_proto_error
) >= (int) cf
->error_amnesia_time
))
387 p
->startup_delay
= 0;
389 p
->last_proto_error
= now
;
391 if (cf
->disable_after_error
)
393 p
->startup_delay
= 0;
398 if (!p
->startup_delay
)
399 p
->startup_delay
= cf
->error_delay_time_min
;
401 p
->startup_delay
= MIN(2 * p
->startup_delay
, cf
->error_delay_time_max
);
405 bgp_graceful_close_conn(struct bgp_conn
*conn
, uint subcode
)
415 bgp_conn_enter_idle_state(conn
);
421 bgp_error(conn
, 6, subcode
, NULL
, 0);
425 bug("bgp_graceful_close_conn: Unknown state %d", conn
->state
);
430 bgp_down(struct bgp_proto
*p
)
432 if (p
->start_state
> BSS_PREPARE
)
434 bgp_setup_auth(p
, 0);
438 BGP_TRACE(D_EVENTS
, "Down");
439 proto_notify_state(&p
->p
, PS_DOWN
);
443 bgp_decision(void *vp
)
445 struct bgp_proto
*p
= vp
;
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
) &&
454 if ((p
->p
.proto_state
== PS_STOP
) &&
455 (p
->outgoing_conn
.state
== BS_IDLE
) &&
456 (p
->incoming_conn
.state
== BS_IDLE
))
461 bgp_stop(struct bgp_proto
*p
, uint subcode
)
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
);
470 bgp_conn_set_state(struct bgp_conn
*conn
, uint new_state
)
472 if (conn
->bgp
->p
.mrtdump
& MD_STATES
)
473 mrt_dump_bgp_state_change(conn
, conn
->state
, new_state
);
475 conn
->state
= new_state
;
479 bgp_conn_enter_openconfirm_state(struct bgp_conn
*conn
)
481 /* Really, most of the work is done in bgp_rx_open(). */
482 bgp_conn_set_state(conn
, BS_OPENCONFIRM
);
485 static const struct bgp_af_caps dummy_af_caps
= { };
488 bgp_conn_enter_established_state(struct bgp_conn
*conn
)
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
;
495 BGP_TRACE(D_EVENTS
, "BGP session established");
497 /* For multi-hop BGP sessions */
498 if (ipa_zero(p
->source_addr
))
499 p
->source_addr
= conn
->sk
->saddr
;
501 conn
->sk
->fast_rx
= 0;
504 p
->last_error_class
= 0;
505 p
->last_error_code
= 0;
507 p
->as4_session
= conn
->as4_session
;
509 p
->route_refresh
= peer
->route_refresh
;
510 p
->enhanced_refresh
= local
->enhanced_refresh
&& peer
->enhanced_refresh
;
512 /* Whether we may handle possible GR of peer (it has some AF GR-able) */
513 p
->gr_ready
= 0; /* Updated later */
515 /* Whether peer is ready to handle our GR recovery */
516 int peer_gr_ready
= peer
->gr_aware
&& !(peer
->gr_flags
& BGP_GRF_RESTART
);
518 if (p
->gr_active_num
)
519 tm_stop(p
->gr_timer
);
521 /* Number of active channels */
524 WALK_LIST(c
, p
->p
.channels
)
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
);
529 /* Ignore AFIs that were not announced in multiprotocol capability */
530 if (!loc
|| !loc
->ready
)
531 loc
= &dummy_af_caps
;
533 if (!rem
|| !rem
->ready
)
534 rem
= &dummy_af_caps
;
536 int active
= loc
->ready
&& rem
->ready
;
537 c
->c
.disabled
= !active
;
538 c
->c
.reloadable
= p
->route_refresh
;
540 c
->index
= active
? num
++ : 0;
542 c
->feed_state
= BFS_NONE
;
543 c
->load_state
= BFS_NONE
;
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
;
549 /* Channels not able to recover gracefully */
550 if (p
->p
.gr_recovery
&& (!active
|| !peer_gr_ready
))
551 channel_graceful_restart_unlock(&c
->c
);
553 /* Channels waiting for local convergence */
554 if (p
->p
.gr_recovery
&& loc
->gr_able
&& peer_gr_ready
)
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
);
561 /* GR capability implies that neighbor will send End-of-RIB */
563 c
->load_state
= BFS_LOADING
;
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
);
571 c
->c
.ra_mode
= RA_ANY
;
572 else if (c
->cf
->secondary
)
573 c
->c
.ra_mode
= RA_ACCEPTED
;
575 c
->c
.ra_mode
= RA_OPTIMAL
;
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
;
582 WALK_LIST(c
, p
->p
.channels
)
587 p
->afi_map
[c
->index
] = c
->afi
;
588 p
->channel_map
[c
->index
] = c
;
591 /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */
593 bgp_conn_set_state(conn
, BS_ESTABLISHED
);
594 proto_notify_state(&p
->p
, PS_UP
);
598 bgp_conn_leave_established_state(struct bgp_proto
*p
)
600 BGP_TRACE(D_EVENTS
, "BGP session closed");
603 // XXXX free these tables to avoid memory leak during graceful restart
604 // bgp_free_prefix_table(p);
605 // bgp_free_bucket_table(p);
607 if (p
->p
.proto_state
== PS_UP
)
612 bgp_conn_enter_close_state(struct bgp_conn
*conn
)
614 struct bgp_proto
*p
= conn
->bgp
;
615 int os
= conn
->state
;
617 bgp_conn_set_state(conn
, BS_CLOSE
);
618 tm_stop(conn
->keepalive_timer
);
619 conn
->sk
->rx_hook
= NULL
;
621 /* Timeout for CLOSE state, if we cannot send notification soon then we just hangup */
622 bgp_start_timer(conn
->hold_timer
, 10);
624 if (os
== BS_ESTABLISHED
)
625 bgp_conn_leave_established_state(p
);
629 bgp_conn_enter_idle_state(struct bgp_conn
*conn
)
631 struct bgp_proto
*p
= conn
->bgp
;
632 int os
= conn
->state
;
634 bgp_close_conn(conn
);
635 bgp_conn_set_state(conn
, BS_IDLE
);
636 ev_schedule(p
->event
);
638 if (os
== BS_ESTABLISHED
)
639 bgp_conn_leave_established_state(p
);
643 * bgp_handle_graceful_restart - handle detected BGP graceful restart
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
654 bgp_handle_graceful_restart(struct bgp_proto
*p
)
656 ASSERT(p
->conn
&& (p
->conn
->state
== BS_ESTABLISHED
) && p
->gr_ready
);
658 BGP_TRACE(D_EVENTS
, "Neighbor graceful restart detected%s",
659 p
->gr_active_num
? " - already pending" : "");
661 p
->gr_active_num
= 0;
663 struct bgp_channel
*c
;
664 WALK_LIST(c
, p
->p
.channels
)
669 rt_refresh_end(c
->c
.table
, &c
->c
);
673 rt_refresh_begin(c
->c
.table
, &c
->c
);
677 /* Just flush the routes */
678 rt_refresh_begin(c
->c
.table
, &c
->c
);
679 rt_refresh_end(c
->c
.table
, &c
->c
);
683 proto_notify_state(&p
->p
, PS_START
);
684 bgp_start_timer(p
->gr_timer
, p
->conn
->local_caps
->gr_time
);
688 * bgp_graceful_restart_done - finish active BGP graceful restart
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.
698 bgp_graceful_restart_done(struct bgp_channel
*c
)
700 struct bgp_proto
*p
= (void *) c
->c
.proto
;
702 ASSERT(c
->gr_active
);
706 if (!p
->gr_active_num
)
707 BGP_TRACE(D_EVENTS
, "Neighbor graceful restart done");
709 rt_refresh_end(c
->c
.table
, &c
->c
);
713 * bgp_graceful_restart_timeout - timeout of graceful restart 'restart timer'
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.
722 bgp_graceful_restart_timeout(timer
*t
)
724 struct bgp_proto
*p
= t
->data
;
726 BGP_TRACE(D_EVENTS
, "Neighbor graceful restart timeout");
732 * bgp_refresh_begin - start incoming enhanced route refresh sequence
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.
742 bgp_refresh_begin(struct bgp_channel
*c
)
744 struct bgp_proto
*p
= (void *) c
->c
.proto
;
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; }
749 c
->load_state
= BFS_REFRESHING
;
750 rt_refresh_begin(c
->c
.table
, &c
->c
);
754 * bgp_refresh_end - finish incoming enhanced route refresh sequence
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.
763 bgp_refresh_end(struct bgp_channel
*c
)
765 struct bgp_proto
*p
= (void *) c
->c
.proto
;
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; }
770 c
->load_state
= BFS_NONE
;
771 rt_refresh_end(c
->c
.table
, &c
->c
);
776 bgp_send_open(struct bgp_conn
*conn
)
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
);
788 bgp_connected(sock
*sk
)
790 struct bgp_conn
*conn
= sk
->data
;
791 struct bgp_proto
*p
= conn
->bgp
;
793 BGP_TRACE(D_EVENTS
, "Connected");
798 bgp_connect_timeout(timer
*t
)
800 struct bgp_conn
*conn
= t
->data
;
801 struct bgp_proto
*p
= conn
->bgp
;
803 DBG("BGP: connect_timeout\n");
804 if (p
->p
.proto_state
== PS_START
)
806 bgp_close_conn(conn
);
810 bgp_conn_enter_idle_state(conn
);
814 bgp_sock_err(sock
*sk
, int err
)
816 struct bgp_conn
*conn
= sk
->data
;
817 struct bgp_proto
*p
= conn
->bgp
;
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
828 bgp_store_error(p
, conn
, BE_SOCKET
, err
);
831 BGP_TRACE(D_EVENTS
, "Connection lost (%M)", err
);
833 BGP_TRACE(D_EVENTS
, "Connection closed");
835 if ((conn
->state
== BS_ESTABLISHED
) && p
->gr_ready
)
836 bgp_handle_graceful_restart(p
);
838 bgp_conn_enter_idle_state(conn
);
842 bgp_hold_timeout(timer
*t
)
844 struct bgp_conn
*conn
= t
->data
;
845 struct bgp_proto
*p
= conn
->bgp
;
847 DBG("BGP: Hold timeout\n");
849 /* We are already closing the connection - just do hangup */
850 if (conn
->state
== BS_CLOSE
)
852 BGP_TRACE(D_EVENTS
, "Connection stalled");
853 bgp_conn_enter_idle_state(conn
);
857 /* If there is something in input queue, we are probably congested
858 and perhaps just not processed BGP packets in time. */
860 if (sk_rx_ready(conn
->sk
) > 0)
861 bgp_start_timer(conn
->hold_timer
, 10);
863 bgp_error(conn
, 4, 0, NULL
, 0);
867 bgp_keepalive_timeout(timer
*t
)
869 struct bgp_conn
*conn
= t
->data
;
871 DBG("BGP: Keepalive timer\n");
872 bgp_schedule_packet(conn
, NULL
, PKT_KEEPALIVE
);
874 /* Kick TX a bit faster */
875 if (ev_active(conn
->tx_ev
))
880 bgp_setup_conn(struct bgp_proto
*p
, struct bgp_conn
*conn
)
885 conn
->packets_to_send
= 0;
886 conn
->channels_to_send
= 0;
887 conn
->last_channel
= 0;
888 conn
->last_channel_count
= 0;
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);
894 conn
->tx_ev
= ev_new(p
->p
.pool
);
895 conn
->tx_ev
->hook
= bgp_kick_tx
;
896 conn
->tx_ev
->data
= conn
;
900 bgp_setup_sk(struct bgp_conn
*conn
, sock
*s
)
903 s
->err_hook
= bgp_sock_err
;
909 bgp_active(struct bgp_proto
*p
)
911 int delay
= MAX(1, p
->cf
->connect_delay_time
);
912 struct bgp_conn
*conn
= &p
->outgoing_conn
;
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
);
921 * bgp_connect - initiate an outgoing connection
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.
929 bgp_connect(struct bgp_proto
*p
) /* Enter Connect state and start establishing connection */
931 struct bgp_conn
*conn
= &p
->outgoing_conn
;
932 int hops
= p
->cf
->multihop
? : 1;
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
);
956 /* Set minimal receive TTL if needed */
957 if (p
->cf
->ttl_security
)
958 if (sk_set_min_ttl(s
, 256 - hops
) < 0)
961 DBG("BGP: Waiting for connect success\n");
962 bgp_start_timer(conn
->connect_timer
, p
->cf
->connect_retry_time
);
966 sk_log_error(s
, p
->p
.name
);
972 * bgp_find_proto - find existing proto for incoming connection
976 static struct bgp_proto
*
977 bgp_find_proto(sock
*sk
)
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
))
993 * bgp_incoming_connection - handle an incoming connection
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.
1005 bgp_incoming_connection(sock
*sk
, uint dummy UNUSED
)
1007 struct bgp_proto
*p
;
1010 DBG("BGP: Incoming connection from %I port %d\n", sk
->daddr
, sk
->dport
);
1011 p
= bgp_find_proto(sk
);
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
);
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.
1027 acc
= (p
->p
.proto_state
== PS_START
|| p
->p
.proto_state
== PS_UP
) &&
1028 (p
->start_state
>= BSS_CONNECT
) && (!p
->incoming_conn
.sk
);
1030 if (p
->conn
&& (p
->conn
->state
== BS_ESTABLISHED
) && p
->gr_ready
)
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
);
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
);
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");
1052 hops
= p
->cf
->multihop
? : 1;
1054 if (sk_set_ttl(sk
, p
->cf
->ttl_security
? 255 : hops
) < 0)
1057 if (p
->cf
->ttl_security
)
1058 if (sk_set_min_ttl(sk
, 256 - hops
) < 0)
1061 if (p
->cf
->enable_extended_messages
)
1063 sk
->rbsize
= BGP_RX_BUFFER_EXT_SIZE
;
1064 sk
->tbsize
= BGP_TX_BUFFER_EXT_SIZE
;
1068 bgp_setup_conn(p
, &p
->incoming_conn
);
1069 bgp_setup_sk(&p
->incoming_conn
, sk
);
1070 bgp_send_open(&p
->incoming_conn
);
1074 sk_log_error(sk
, p
->p
.name
);
1075 log(L_ERR
"%s: Incoming connection aborted", p
->p
.name
);
1081 bgp_listen_sock_err(sock
*sk UNUSED
, int err
)
1083 if (err
== ECONNABORTED
)
1084 log(L_WARN
"BGP: Incoming connection aborted");
1086 log(L_ERR
"BGP: Error on listening socket: %M", err
);
1090 bgp_start_neighbor(struct bgp_proto
*p
)
1092 /* Called only for single-hop BGP sessions */
1094 if (ipa_zero(p
->source_addr
))
1095 p
->source_addr
= p
->neigh
->ifa
->ip
;
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
;
1106 bgp_neigh_notify(neighbor
*n
)
1108 struct bgp_proto
*p
= (struct bgp_proto
*) n
->proto
;
1109 int ps
= p
->p
.proto_state
;
1114 if ((ps
== PS_DOWN
) || (ps
== PS_STOP
))
1117 int prepare
= (ps
== PS_START
) && (p
->start_state
== BSS_PREPARE
);
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)? */
1129 else if (p
->cf
->check_link
&& !(n
->iface
->flags
& IF_LINK_UP
))
1133 BGP_TRACE(D_EVENTS
, "Link down");
1134 bgp_store_error(p
, NULL
, BE_MISC
, BEM_LINK_DOWN
);
1136 bgp_update_startup_delay(p
);
1144 BGP_TRACE(D_EVENTS
, "Neighbor ready");
1145 bgp_start_neighbor(p
);
1151 bgp_bfd_notify(struct bfd_request
*req
)
1153 struct bgp_proto
*p
= req
->data
;
1154 int ps
= p
->p
.proto_state
;
1156 if (req
->down
&& ((ps
== PS_START
) || (ps
== PS_UP
)))
1158 BGP_TRACE(D_EVENTS
, "BFD session down");
1159 bgp_store_error(p
, NULL
, BE_MISC
, BEM_BFD_DOWN
);
1161 bgp_update_startup_delay(p
);
1167 bgp_update_bfd(struct bgp_proto
*p
, int use_bfd
)
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
,
1174 if (!use_bfd
&& p
->bfd_req
)
1182 bgp_reload_routes(struct channel
*C
)
1184 struct bgp_proto
*p
= (void *) C
->proto
;
1185 struct bgp_channel
*c
= (void *) C
;
1187 ASSERT(p
->conn
&& p
->route_refresh
);
1189 bgp_schedule_packet(p
->conn
, c
, PKT_ROUTE_REFRESH
);
1193 bgp_feed_begin(struct channel
*C
, int initial
)
1195 struct bgp_proto
*p
= (void *) C
->proto
;
1196 struct bgp_channel
*c
= (void *) C
;
1198 /* This should not happen */
1202 if (initial
&& p
->cf
->gr_mode
)
1203 c
->feed_state
= BFS_LOADING
;
1205 /* It is refeed and both sides support enhanced route refresh */
1206 if (!initial
&& p
->enhanced_refresh
)
1208 /* BoRR must not be sent before End-of-RIB */
1209 if (c
->feed_state
== BFS_LOADING
|| c
->feed_state
== BFS_LOADED
)
1212 c
->feed_state
= BFS_REFRESHING
;
1213 bgp_schedule_packet(p
->conn
, c
, PKT_BEGIN_REFRESH
);
1218 bgp_feed_end(struct channel
*C
)
1220 struct bgp_proto
*p
= (void *) C
->proto
;
1221 struct bgp_channel
*c
= (void *) C
;
1223 /* This should not happen */
1227 /* Non-demarcated feed ended, nothing to do */
1228 if (c
->feed_state
== BFS_NONE
)
1231 /* Schedule End-of-RIB packet */
1232 if (c
->feed_state
== BFS_LOADING
)
1233 c
->feed_state
= BFS_LOADED
;
1235 /* Schedule EoRR packet */
1236 if (c
->feed_state
== BFS_REFRESHING
)
1237 c
->feed_state
= BFS_REFRESHED
;
1240 bgp_schedule_packet(p
->conn
, c
, PKT_UPDATE
);
1245 bgp_start_locked(struct object_lock
*lock
)
1247 struct bgp_proto
*p
= lock
->data
;
1248 struct bgp_config
*cf
= p
->cf
;
1250 if (p
->p
.proto_state
!= PS_START
)
1252 DBG("BGP: Got lock in different state %d\n", p
->p
.proto_state
);
1256 DBG("BGP: Got lock\n");
1260 /* Multi-hop sessions do not use neighbor entries */
1265 neighbor
*n
= neigh_find2(&p
->p
, &cf
->remote_ip
, cf
->iface
, NEF_STICKY
);
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 */
1271 bgp_store_error(p
, NULL
, BE_MISC
, BEM_INVALID_NEXT_HOP
);
1272 proto_notify_state(&p
->p
, PS_DOWN
);
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
);
1283 bgp_start_neighbor(p
);
1287 bgp_start(struct proto
*P
)
1289 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
1290 struct object_lock
*lock
;
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
;
1299 p
->gr_active_num
= 0;
1301 p
->event
= ev_new(p
->p
.pool
);
1302 p
->event
->hook
= bgp_decision
;
1305 p
->startup_timer
= tm_new(p
->p
.pool
);
1306 p
->startup_timer
->hook
= bgp_startup_timeout
;
1307 p
->startup_timer
->data
= p
;
1309 p
->gr_timer
= tm_new(p
->p
.pool
);
1310 p
->gr_timer
->hook
= bgp_graceful_restart_timeout
;
1311 p
->gr_timer
->data
= p
;
1313 p
->local_id
= proto_get_router_id(P
->cf
);
1315 p
->rr_cluster_id
= p
->cf
->rr_cluster_id
? p
->cf
->rr_cluster_id
: p
->local_id
;
1318 p
->source_addr
= p
->cf
->local_ip
;
1319 p
->link_addr
= IPA_NONE
;
1322 if (p
->p
.gr_recovery
&& p
->cf
->gr_mode
)
1324 struct bgp_channel
*c
;
1325 WALK_LIST(c
, p
->p
.channels
)
1326 channel_graceful_restart_lock(&c
->c
);
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.
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
;
1341 olock_acquire(lock
);
1346 extern int proto_restart
;
1349 bgp_shutdown(struct proto
*P
)
1351 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
1354 BGP_TRACE(D_EVENTS
, "Shutdown requested");
1356 switch (P
->down_code
)
1359 case PDC_CF_DISABLE
:
1360 subcode
= 3; // Errcode 6, 3 - peer de-configured
1363 case PDC_CF_RESTART
:
1364 subcode
= 6; // Errcode 6, 6 - other configuration change
1367 case PDC_CMD_DISABLE
:
1368 case PDC_CMD_SHUTDOWN
:
1369 subcode
= 2; // Errcode 6, 2 - administrative shutdown
1372 case PDC_CMD_RESTART
:
1373 subcode
= 4; // Errcode 6, 4 - administrative reset
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
);
1383 case PDC_OUT_LIMIT_HIT
:
1384 subcode
= proto_restart
? 4 : 2; // Administrative reset or shutdown
1387 bgp_store_error(p
, NULL
, BE_AUTO_DOWN
, BEA_ROUTE_LIMIT_EXCEEDED
);
1389 bgp_update_startup_delay(p
);
1391 p
->startup_delay
= 0;
1395 bgp_store_error(p
, NULL
, BE_MAN_DOWN
, 0);
1396 p
->startup_delay
= 0;
1399 bgp_stop(p
, subcode
);
1400 return p
->p
.proto_state
;
1403 static struct proto
*
1404 bgp_init(struct proto_config
*CF
)
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
;
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
;
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
;
1429 /* Confederation ID is used for truly external peers */
1430 if (cf
->confederation
&& !p
->is_interior
)
1431 p
->public_as
= cf
->confederation
;
1433 /* Add all channels */
1434 struct bgp_channel_config
*cc
;
1435 WALK_LIST(cc
, CF
->channels
)
1436 proto_add_channel(P
, &cc
->c
);
1442 bgp_channel_init(struct channel
*C
, struct channel_config
*CF
)
1444 struct bgp_channel
*c
= (void *) C
;
1445 struct bgp_channel_config
*cf
= (void *) CF
;
1451 if (cf
->igp_table_ip4
)
1452 c
->igp_table_ip4
= cf
->igp_table_ip4
->table
;
1454 if (cf
->igp_table_ip6
)
1455 c
->igp_table_ip6
= cf
->igp_table_ip6
->table
;
1459 bgp_channel_start(struct channel
*C
)
1461 struct bgp_proto
*p
= (void *) C
->proto
;
1462 struct bgp_channel
*c
= (void *) C
;
1463 ip_addr src
= p
->source_addr
;
1465 if (c
->igp_table_ip4
)
1466 rt_lock_table(c
->igp_table_ip4
);
1468 if (c
->igp_table_ip6
)
1469 rt_lock_table(c
->igp_table_ip6
);
1471 c
->pool
= p
->p
.pool
; // XXXX
1472 bgp_init_bucket_table(c
);
1473 bgp_init_prefix_table(c
);
1475 c
->next_hop_addr
= c
->cf
->next_hop_addr
;
1476 c
->link_addr
= IPA_NONE
;
1477 c
->packets_to_send
= 0;
1479 /* Try to use source address as next hop address */
1480 if (ipa_zero(c
->next_hop_addr
))
1482 if (bgp_channel_is_ipv4(c
) && (ipa_is_ip4(src
) || c
->ext_next_hop
))
1483 c
->next_hop_addr
= src
;
1485 if (bgp_channel_is_ipv6(c
) && (ipa_is_ip6(src
) || c
->ext_next_hop
))
1486 c
->next_hop_addr
= src
;
1489 /* Exit if no feasible next hop address is found */
1490 if (ipa_zero(c
->next_hop_addr
))
1492 log(L_WARN
"%s: Missing next hop address", p
->p
.name
);
1496 /* Set link-local address for IPv6 single-hop BGP */
1497 if (ipa_is_ip6(c
->next_hop_addr
) && p
->neigh
)
1499 c
->link_addr
= p
->link_addr
;
1501 if (ipa_zero(c
->link_addr
))
1502 log(L_WARN
"%s: Missing link-local address", p
->p
.name
);
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
;
1509 return 0; /* XXXX: Currently undefined */
1513 bgp_channel_shutdown(struct channel
*C
)
1515 struct bgp_channel
*c
= (void *) C
;
1517 /* XXXX: cleanup bucket and prefix tables */
1519 c
->next_hop_addr
= IPA_NONE
;
1520 c
->link_addr
= IPA_NONE
;
1524 bgp_channel_cleanup(struct channel
*C
)
1526 struct bgp_channel
*c
= (void *) C
;
1528 if (c
->igp_table_ip4
)
1529 rt_unlock_table(c
->igp_table_ip4
);
1531 if (c
->igp_table_ip6
)
1532 rt_unlock_table(c
->igp_table_ip6
);
1535 static inline struct bgp_channel_config
*
1536 bgp_find_channel_config(struct bgp_config
*cf
, u32 afi
)
1538 struct bgp_channel_config
*cc
;
1540 WALK_LIST(cc
, cf
->c
.channels
)
1547 struct rtable_config
*
1548 bgp_default_igp_table(struct bgp_config
*cf
, struct bgp_channel_config
*cc
, u32 type
)
1550 struct bgp_channel_config
*cc2
;
1551 struct rtable_config
*tab
;
1553 /* First, try table connected by the channel */
1554 if (cc
->c
.table
->addr_type
== type
)
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
);
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
))
1565 /* Third, try table connected by the paired channel */
1566 if (cc2
&& (cc2
->c
.table
->addr_type
== type
))
1567 return cc2
->c
.table
;
1569 /* Last, try default table of given type */
1570 if (tab
= cf
->c
.global
->def_tables
[type
])
1573 cf_error("Undefined IGP table");
1578 bgp_postconfig(struct proto_config
*CF
)
1580 struct bgp_config
*cf
= (void *) CF
;
1581 int internal
= (cf
->local_as
== cf
->remote_as
);
1583 /* Do not check templates at all */
1584 if (cf
->c
.class == SYM_TEMPLATE
)
1588 /* EBGP direct by default, IBGP multihop by default */
1589 if (cf
->multihop
< 0)
1590 cf
->multihop
= internal
? 64 : 0;
1594 cf_error("Local AS number must be set");
1596 if (ipa_zero(cf
->remote_ip
))
1597 cf_error("Neighbor must be configured");
1600 cf_error("Remote AS number must be set");
1602 if (ipa_is_link_local(cf
->remote_ip
) && !cf
->iface
)
1603 cf_error("Link-local neighbor address requires specified interface");
1605 if (!(cf
->capabilities
&& cf
->enable_as4
) && (cf
->remote_as
> 0xFFFF))
1606 cf_error("Neighbor AS number out of range (AS4 not available)");
1608 if (!internal
&& cf
->rr_client
)
1609 cf_error("Only internal neighbor can be RR client");
1611 if (internal
&& cf
->rs_client
)
1612 cf_error("Only external neighbor can be RS client");
1614 if (!cf
->confederation
&& cf
->confederation_member
)
1615 cf_error("Confederation ID must be set for member sessions");
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");
1621 if (cf
->multihop
&& cf
->iface
)
1622 cf_error("Multihop BGP cannot be bound to interface");
1624 if (cf
->multihop
&& cf
->check_link
)
1625 cf_error("Multihop BGP cannot depend on link state");
1627 if (cf
->multihop
&& cf
->bfd
&& ipa_zero(cf
->local_ip
))
1628 cf_error("Multihop BGP with BFD requires specified local address");
1631 struct bgp_channel_config
*cc
;
1632 WALK_LIST(cc
, CF
->channels
)
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
;
1638 /* Different default based on rs_client */
1639 if (!cc
->missing_lladdr
)
1640 cc
->missing_lladdr
= cf
->rs_client
? MLL_IGNORE
: MLL_SELF
;
1642 /* Different default for gw_mode */
1644 cc
->gw_mode
= cf
->multihop
? GW_RECURSIVE
: GW_DIRECT
;
1646 /* Default based on proto config */
1647 if (cc
->gr_able
== 0xff)
1648 cc
->gr_able
= (cf
->gr_mode
== BGP_GR_ABLE
);
1650 /* Default values of IGP tables */
1651 if ((cc
->gw_mode
== GW_RECURSIVE
) && !cc
->desc
->no_igp
)
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
);
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
);
1659 if (cc
->igp_table_ip4
&& bgp_cc_is_ipv6(cc
) && !cc
->ext_next_hop
)
1660 cf_error("Mismatched IGP table type");
1662 if (cc
->igp_table_ip6
&& bgp_cc_is_ipv4(cc
) && !cc
->ext_next_hop
)
1663 cf_error("Mismatched IGP table type");
1666 if (cf
->multihop
&& (cc
->gw_mode
== GW_DIRECT
))
1667 cf_error("Multihop BGP cannot use direct gateway mode");
1669 if ((cc
->gw_mode
== GW_RECURSIVE
) && cc
->c
.table
->sorted
)
1670 cf_error("BGP in recursive mode prohibits sorted table");
1672 if (cf
->deterministic_med
&& cc
->c
.table
->sorted
)
1673 cf_error("BGP with deterministic MED prohibits sorted table");
1675 if (cc
->secondary
&& !cc
->c
.table
->sorted
)
1676 cf_error("BGP with secondary option requires sorted table");
1681 bgp_reconfigure(struct proto
*P
, struct proto_config
*CF
)
1683 struct bgp_proto
*p
= (void *) P
;
1684 struct bgp_config
*new = (void *) CF
;
1685 struct bgp_config
*old
= p
->cf
;
1687 if (proto_get_router_id(CF
) != p
->local_id
)
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
)));
1697 /* FIXME: Move channel reconfiguration to generic protocol code ? */
1698 struct channel
*C
, *C2
;
1699 struct bgp_channel_config
*cc
;
1701 WALK_LIST(C
, p
->p
.channels
)
1704 WALK_LIST(cc
, new->c
.channels
)
1706 C
= (struct channel
*) bgp_find_channel(p
, cc
->afi
);
1707 same
= proto_configure_channel(P
, &C
, &cc
->c
) && same
;
1711 WALK_LIST_DELSAFE(C
, C2
, p
->p
.channels
)
1713 same
= proto_configure_channel(P
, &C
, NULL
) && same
;
1716 if (same
&& (p
->start_state
> BSS_PREPARE
))
1717 bgp_update_bfd(p
, new->bfd
);
1719 /* We should update our copy of configuration ptr as old configuration will be freed */
1726 #define IGP_TABLE(cf, sym) ((cf)->igp_table_##sym ? (cf)->igp_table_##sym ->table : NULL )
1729 bgp_channel_reconfigure(struct channel
*C
, struct channel_config
*CC
)
1731 struct bgp_channel
*c
= (void *) C
;
1732 struct bgp_channel_config
*new = (void *) CC
;
1733 struct bgp_channel_config
*old
= c
->cf
;
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
)))
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
)))
1751 bgp_copy_config(struct proto_config
*dest UNUSED
, struct proto_config
*src UNUSED
)
1753 /* Just a shallow copy */
1758 * bgp_error - report a protocol error
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
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.
1770 bgp_error(struct bgp_conn
*c
, uint code
, uint subcode
, byte
*data
, int len
)
1772 struct bgp_proto
*p
= c
->bgp
;
1774 if (c
->state
== BS_CLOSE
)
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
);
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
);
1789 bgp_update_startup_delay(p
);
1795 * bgp_store_error - store last error for status report
1798 * @class: error class (BE_xxx constants)
1799 * @code: error code (class specific)
1801 * bgp_store_error() decides whether given error is interesting enough
1802 * and store that error to last_error variables of @p
1805 bgp_store_error(struct bgp_proto
*p
, struct bgp_conn
*c
, u8
class, u32 code
)
1807 /* During PS_UP, we ignore errors on secondary connection */
1808 if ((p
->p
.proto_state
== PS_UP
) && c
&& (c
!= p
->conn
))
1811 /* During PS_STOP, we ignore any errors, as we want to report
1812 * the error that caused transition to PS_STOP
1814 if (p
->p
.proto_state
== PS_STOP
)
1817 p
->last_error_class
= class;
1818 p
->last_error_code
= code
;
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"};
1827 bgp_last_errmsg(struct bgp_proto
*p
)
1829 switch (p
->last_error_class
)
1832 return bgp_misc_errors
[p
->last_error_code
];
1834 return (p
->last_error_code
== 0) ? "Connection closed" : strerror(p
->last_error_code
);
1837 return bgp_error_dsc(p
->last_error_code
>> 16, p
->last_error_code
& 0xFF);
1839 return bgp_auto_errors
[p
->last_error_code
];
1846 bgp_state_dsc(struct bgp_proto
*p
)
1848 if (p
->p
.proto_state
== PS_DOWN
)
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
)
1855 return bgp_state_names
[state
];
1859 bgp_get_status(struct proto
*P
, byte
*buf
)
1861 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
1863 const char *err1
= bgp_err_classes
[p
->last_error_class
];
1864 const char *err2
= bgp_last_errmsg(p
);
1866 if (P
->proto_state
== PS_DOWN
)
1867 bsprintf(buf
, "%s%s", err1
, err2
);
1869 bsprintf(buf
, "%-14s%s%s", bgp_state_dsc(p
), err1
, err2
);
1873 bgp_show_afis(int code
, char *s
, u32
*afis
, uint count
)
1880 for (u32
*af
= afis
; af
< (afis
+ count
); af
++)
1882 const struct bgp_af_desc
*desc
= bgp_get_af_desc(*af
);
1884 buffer_print(&b
, " %s", desc
->name
);
1886 buffer_print(&b
, " <%u/%u>", BGP_AFI(*af
), BGP_SAFI(*af
));
1890 strcpy(b
.end
- 32, " ... <too long>");
1892 cli_msg(code
, b
.start
);
1896 bgp_show_capabilities(struct bgp_proto
*p UNUSED
, struct bgp_caps
*caps
)
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
));
1907 WALK_AF_CAPS(caps
, ac
)
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
;
1917 cli_msg(-1006, " Multiprotocol");
1920 WALK_AF_CAPS(caps
, ac
)
1922 afl1
[afn1
++] = ac
->afi
;
1924 bgp_show_afis(-1006, " AF announced:", afl1
, afn1
);
1927 if (caps
->route_refresh
)
1928 cli_msg(-1006, " Route refresh");
1930 if (any_ext_next_hop
)
1932 cli_msg(-1006, " Extended next hop");
1935 WALK_AF_CAPS(caps
, ac
)
1936 if (ac
->ext_next_hop
)
1937 afl1
[afn1
++] = ac
->afi
;
1939 bgp_show_afis(-1006, " IPv6 nexthop:", afl1
, afn1
);
1942 if (caps
->ext_messages
)
1943 cli_msg(-1006, " Extended message");
1946 cli_msg(-1006, " Graceful restart");
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");
1956 WALK_AF_CAPS(caps
, ac
)
1959 afl1
[afn1
++] = ac
->afi
;
1961 if (ac
->gr_af_flags
& BGP_GRF_FORWARDING
)
1962 afl2
[afn2
++] = ac
->afi
;
1965 bgp_show_afis(-1006, " AF supported:", afl1
, afn1
);
1966 bgp_show_afis(-1006, " AF preserved:", afl2
, afn2
);
1969 if (caps
->as4_support
)
1970 cli_msg(-1006, " 4-octet AS numbers");
1974 cli_msg(-1006, " ADD-PATH");
1977 WALK_AF_CAPS(caps
, ac
)
1979 if (ac
->add_path
& BGP_ADD_PATH_RX
)
1980 afl1
[afn1
++] = ac
->afi
;
1982 if (ac
->add_path
& BGP_ADD_PATH_TX
)
1983 afl2
[afn2
++] = ac
->afi
;
1986 bgp_show_afis(-1006, " RX:", afl1
, afn1
);
1987 bgp_show_afis(-1006, " TX:", afl2
, afn2
);
1990 if (caps
->enhanced_refresh
)
1991 cli_msg(-1006, " Enhanced refresh");
1995 bgp_show_proto_info(struct proto
*P
)
1997 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
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
);
2003 if (p
->gr_active_num
)
2004 cli_msg(-1006, " Neighbor graceful restart active");
2006 if (P
->proto_state
== PS_START
)
2008 struct bgp_conn
*oc
= &p
->outgoing_conn
;
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
);
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
);
2020 if (p
->gr_active_num
&& tm_active(p
->gr_timer
))
2021 cli_msg(-1006, " Restart timer: %t/-",
2022 tm2_remains(p
->gr_timer
));
2024 else if (P
->proto_state
== PS_UP
)
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
);
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" : "");
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
);
2049 if ((p
->last_error_class
!= BE_NONE
) &&
2050 (p
->last_error_class
!= BE_MAN_DOWN
))
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
);
2059 struct bgp_channel
*c
;
2060 WALK_LIST(c
, p
->p
.channels
)
2062 channel_show_info(&c
->c
);
2064 if (c
->igp_table_ip4
)
2065 cli_msg(-1006, " IGP IPv4 table: %s", c
->igp_table_ip4
->name
);
2067 if (c
->igp_table_ip6
)
2068 cli_msg(-1006, " IGP IPv6 table: %s", c
->igp_table_ip6
->name
);
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
,
2083 struct protocol proto_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
,
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