]> git.ipfire.org Git - thirdparty/bird.git/blame - proto/bgp/bgp.c
Trivial code cleanups
[thirdparty/bird.git] / proto / bgp / bgp.c
CommitLineData
2638249d
MM
1/*
2 * BIRD -- The Border Gateway Protocol
3 *
4 * (c) 2000 Martin Mares <mj@ucw.cz>
d15b0b0a
OZ
5 * (c) 2008--2016 Ondrej Zajicek <santiago@crfreenet.org>
6 * (c) 2008--2016 CZ.NIC z.s.p.o.
2638249d
MM
7 *
8 * Can be freely distributed and used under the terms of the GNU GPL.
9 */
10
54e55169
MM
11/**
12 * DOC: Border Gateway Protocol
13 *
d15b0b0a
OZ
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
54e55169
MM
16 * both incoming and outgoing BGP packets and |attrs.c| containing functions for
17 * manipulation with BGP attribute lists.
18 *
d15b0b0a
OZ
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.
54e55169 25 *
d15b0b0a
OZ
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.
54e55169 32 *
d15b0b0a
OZ
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.
54e55169 37 *
d15b0b0a
OZ
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.
54e55169 53 *
d15b0b0a
OZ
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.
6eda3f13
OZ
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.
c49e4a65
OZ
70 *
71 * Supported standards:
0f40405f
OZ
72 * RFC 4271 - Border Gateway Protocol 4 (BGP)
73 * RFC 1997 - BGP Communities Attribute
74 * RFC 2385 - Protection of BGP Sessions via TCP MD5 Signature
75 * RFC 2545 - Use of BGP Multiprotocol Extensions for IPv6
76 * RFC 2918 - Route Refresh Capability
77 * RFC 3107 - Carrying Label Information in BGP
78 * RFC 4360 - BGP Extended Communities Attribute
79 * RFC 4364 - BGP/MPLS IPv4 Virtual Private Networks
80 * RFC 4456 - BGP Route Reflection
81 * RFC 4486 - Subcodes for BGP Cease Notification Message
82 * RFC 4659 - BGP/MPLS IPv6 Virtual Private Networks
83 * RFC 4724 - Graceful Restart Mechanism for BGP
84 * RFC 4760 - Multiprotocol extensions for BGP
85 * RFC 4798 - Connecting IPv6 Islands over IPv4 MPLS
86 * RFC 5065 - AS confederations for BGP
87 * RFC 5082 - Generalized TTL Security Mechanism
88 * RFC 5492 - Capabilities Advertisement with BGP
0f40405f
OZ
89 * RFC 5575 - Dissemination of Flow Specification Rules
90 * RFC 5668 - 4-Octet AS Specific BGP Extended Community
91 * RFC 6286 - AS-Wide Unique BGP Identifier
92 * RFC 6608 - Subcodes for BGP Finite State Machine Error
93 * RFC 6793 - BGP Support for 4-Octet AS Numbers
09ee846d 94 * RFC 7311 - Accumulated IGP Metric Attribute for BGP
0f40405f
OZ
95 * RFC 7313 - Enhanced Route Refresh Capability for BGP
96 * RFC 7606 - Revised Error Handling for BGP UPDATE Messages
97 * RFC 7911 - Advertisement of Multiple Paths in BGP
98 * RFC 7947 - Internet Exchange BGP Route Server
99 * RFC 8092 - BGP Large Communities Attribute
100 * RFC 8203 - BGP Administrative Shutdown Communication
101 * RFC 8212 - Default EBGP Route Propagation Behavior without Policies
be7c1aef 102 * RFC 8654 - Extended Message Support for BGP
9bf20484 103 * RFC 8950 - Advertising IPv4 NLRI with an IPv6 Next Hop
913ec57f 104 * RFC 9072 - Extended Optional Parameters Length for BGP OPEN Message
1f2eb2ac 105 * RFC 9117 - Revised Validation Procedure for BGP Flow Specifications
c73b5d2d 106 * RFC 9234 - Route Leak Prevention and Detection Using Roles
0f40405f 107 * draft-uttaro-idr-bgp-persistence-04
71423871 108 * draft-walton-bgp-hostname-capability-02
0f40405f 109 */
54e55169 110
48d79d52 111#undef LOCAL_DEBUG
2638249d 112
02552526
OZ
113#include <stdlib.h>
114
2638249d
MM
115#include "nest/bird.h"
116#include "nest/iface.h"
117#include "nest/protocol.h"
118#include "nest/route.h"
b8113a5e 119#include "nest/cli.h"
1ec52253 120#include "nest/locks.h"
2638249d 121#include "conf/conf.h"
3831b619 122#include "filter/filter.h"
c01e3741 123#include "lib/socket.h"
973399ae 124#include "lib/resource.h"
7d875e09 125#include "lib/string.h"
2638249d
MM
126
127#include "bgp.h"
a848dad4 128#include "proto/bmp/bmp.h"
2638249d 129
e7d2ac44 130
06ece326 131static list STATIC_LIST_INIT(bgp_sockets); /* Global list of listening sockets */
d15b0b0a 132
c01e3741 133
c01e3741 134static void bgp_connect(struct bgp_proto *p);
dd91e467 135static void bgp_active(struct bgp_proto *p);
e0835db4
OZ
136static void bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn);
137static void bgp_setup_sk(struct bgp_conn *conn, sock *s);
138static void bgp_send_open(struct bgp_conn *conn);
9d3fc306 139static void bgp_update_bfd(struct bgp_proto *p, const struct bfd_options *bfd);
2638249d 140
d15b0b0a
OZ
141static int bgp_incoming_connection(sock *sk, uint dummy UNUSED);
142static void bgp_listen_sock_err(sock *sk UNUSED, int err);
11cb6202 143
11b32d91
OZ
144/**
145 * bgp_open - open a BGP instance
146 * @p: BGP instance
147 *
d15b0b0a
OZ
148 * This function allocates and configures shared BGP resources, mainly listening
149 * sockets. Should be called as the last step during initialization (when lock
150 * is acquired and neighbor is ready). When error, caller should change state to
151 * PS_DOWN and return immediately.
11b32d91
OZ
152 */
153static int
154bgp_open(struct bgp_proto *p)
155{
d15b0b0a
OZ
156 struct bgp_socket *bs = NULL;
157 struct iface *ifa = p->cf->strict_bind ? p->cf->iface : NULL;
158 ip_addr addr = p->cf->strict_bind ? p->cf->local_ip :
e0835db4 159 (p->ipv4 ? IPA_NONE4 : IPA_NONE6);
d15b0b0a 160 uint port = p->cf->local_port;
60e9def9
OZ
161 uint flags = p->cf->free_bind ? SKF_FREEBIND : 0;
162 uint flag_mask = SKF_FREEBIND;
d15b0b0a 163
d15b0b0a 164 /* We assume that cf->iface is defined iff cf->local_ip is link-local */
11b32d91 165
d15b0b0a 166 WALK_LIST(bs, bgp_sockets)
60e9def9
OZ
167 if (ipa_equal(bs->sk->saddr, addr) &&
168 (bs->sk->sport == port) &&
169 (bs->sk->iface == ifa) &&
170 (bs->sk->vrf == p->p.vrf) &&
171 ((bs->sk->flags & flag_mask) == flags))
a34b0934 172 {
d15b0b0a
OZ
173 bs->uc++;
174 p->sock = bs;
175 return 0;
a34b0934
OZ
176 }
177
d15b0b0a
OZ
178 sock *sk = sk_new(proto_pool);
179 sk->type = SK_TCP_PASSIVE;
180 sk->ttl = 255;
181 sk->saddr = addr;
182 sk->sport = port;
e19d0805
OZ
183 sk->iface = ifa;
184 sk->vrf = p->p.vrf;
60e9def9 185 sk->flags = flags;
d15b0b0a
OZ
186 sk->tos = IP_PREC_INTERNET_CONTROL;
187 sk->rbsize = BGP_RX_BUFFER_SIZE;
188 sk->tbsize = BGP_TX_BUFFER_SIZE;
189 sk->rx_hook = bgp_incoming_connection;
190 sk->err_hook = bgp_listen_sock_err;
191
192 if (sk_open(sk) < 0)
193 goto err;
11b32d91 194
d15b0b0a
OZ
195 bs = mb_allocz(proto_pool, sizeof(struct bgp_socket));
196 bs->sk = sk;
197 bs->uc = 1;
198 p->sock = bs;
470740f9 199 sk->data = bs;
05476c4d 200
d15b0b0a
OZ
201 add_tail(&bgp_sockets, &bs->n);
202
11b32d91 203 return 0;
b1b19433
OZ
204
205err:
d15b0b0a
OZ
206 sk_log_error(sk, p->p.name);
207 log(L_ERR "%s: Cannot open listening socket", p->p.name);
208 rfree(sk);
b1b19433 209 return -1;
11b32d91
OZ
210}
211
d15b0b0a
OZ
212/**
213 * bgp_close - close a BGP instance
214 * @p: BGP instance
215 *
216 * This function frees and deconfigures shared BGP resources.
217 */
218static void
219bgp_close(struct bgp_proto *p)
220{
221 struct bgp_socket *bs = p->sock;
222
223 ASSERT(bs && bs->uc);
224
225 if (--bs->uc)
226 return;
227
228 rfree(bs->sk);
229 rem_node(&bs->n);
230 mb_free(bs);
d15b0b0a
OZ
231}
232
233static inline int
234bgp_setup_auth(struct bgp_proto *p, int enable)
235{
236 if (p->cf->password)
237 {
757cab18
OZ
238 ip_addr prefix = p->cf->remote_ip;
239 int pxlen = -1;
240
241 if (p->cf->remote_range)
242 {
243 prefix = net_prefix(p->cf->remote_range);
244 pxlen = net_pxlen(p->cf->remote_range);
245 }
246
d15b0b0a 247 int rv = sk_set_md5_auth(p->sock->sk,
757cab18 248 p->cf->local_ip, prefix, pxlen, p->cf->iface,
d15b0b0a
OZ
249 enable ? p->cf->password : NULL, p->cf->setkey);
250
251 if (rv < 0)
252 sk_log_error(p->sock->sk, p->p.name);
253
254 return rv;
255 }
256 else
257 return 0;
258}
259
260static inline struct bgp_channel *
261bgp_find_channel(struct bgp_proto *p, u32 afi)
262{
263 struct bgp_channel *c;
54430df9 264 BGP_WALK_CHANNELS(p, c)
d15b0b0a
OZ
265 if (c->afi == afi)
266 return c;
267
268 return NULL;
269}
270
dd91e467
OZ
271static void
272bgp_startup(struct bgp_proto *p)
273{
274 BGP_TRACE(D_EVENTS, "Started");
d15b0b0a 275 p->start_state = BSS_CONNECT;
be6e39eb 276
e0835db4 277 if (!p->passive)
be6e39eb 278 bgp_active(p);
e0835db4
OZ
279
280 if (p->postponed_sk)
281 {
282 /* Apply postponed incoming connection */
283 bgp_setup_conn(p, &p->incoming_conn);
284 bgp_setup_sk(&p->incoming_conn, p->postponed_sk);
285 bgp_send_open(&p->incoming_conn);
286 p->postponed_sk = NULL;
287 }
dd91e467
OZ
288}
289
290static void
291bgp_startup_timeout(timer *t)
292{
293 bgp_startup(t->data);
294}
295
296
297static void
298bgp_initiate(struct bgp_proto *p)
299{
d15b0b0a
OZ
300 int err_val;
301
302 if (bgp_open(p) < 0)
303 { err_val = BEM_NO_SOCKET; goto err1; }
304
305 if (bgp_setup_auth(p, 1) < 0)
306 { err_val = BEM_INVALID_MD5; goto err2; }
9be9a264 307
1ec52253
OZ
308 if (p->cf->bfd)
309 bgp_update_bfd(p, p->cf->bfd);
310
dd91e467 311 if (p->startup_delay)
d15b0b0a
OZ
312 {
313 p->start_state = BSS_DELAY;
314 BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds due to errors", p->startup_delay);
315 bgp_start_timer(p->startup_timer, p->startup_delay);
316 }
dd91e467
OZ
317 else
318 bgp_startup(p);
dd91e467 319
d15b0b0a 320 return;
d51aa281 321
d15b0b0a
OZ
322err2:
323 bgp_close(p);
324err1:
325 p->p.disabled = 1;
326 bgp_store_error(p, NULL, BE_MISC, err_val);
91d04583
OZ
327
328 p->neigh = NULL;
d15b0b0a 329 proto_notify_state(&p->p, PS_DOWN);
d51aa281 330
d15b0b0a 331 return;
c01e3741
MM
332}
333
54e55169
MM
334/**
335 * bgp_start_timer - start a BGP timer
336 * @t: timer
cc881bd1 337 * @value: time (in seconds) to fire (0 to disable the timer)
54e55169 338 *
d15b0b0a
OZ
339 * This functions calls tm_start() on @t with time @value and the amount of
340 * randomization suggested by the BGP standard. Please use it for all BGP
341 * timers.
54e55169 342 */
3fdbafb6 343void
cc881bd1 344bgp_start_timer(timer *t, uint value)
c01e3741 345{
3fdbafb6 346 if (value)
d15b0b0a 347 {
cc881bd1
OZ
348 /* The randomization procedure is specified in RFC 4271 section 10 */
349 btime time = value S;
350 btime randomize = random() % ((time / 4) + 1);
a6f79ca5 351 tm_start(t, time - randomize);
d15b0b0a 352 }
b552ecc4 353 else
a6f79ca5 354 tm_stop(t);
b552ecc4
MM
355}
356
54e55169
MM
357/**
358 * bgp_close_conn - close a BGP connection
359 * @conn: connection to close
360 *
d15b0b0a
OZ
361 * This function takes a connection described by the &bgp_conn structure, closes
362 * its socket and frees all resources associated with it.
54e55169 363 */
b552ecc4
MM
364void
365bgp_close_conn(struct bgp_conn *conn)
366{
e81b440f 367 // struct bgp_proto *p = conn->bgp;
b552ecc4
MM
368
369 DBG("BGP: Closing connection\n");
370 conn->packets_to_send = 0;
d15b0b0a
OZ
371 conn->channels_to_send = 0;
372 rfree(conn->connect_timer);
373 conn->connect_timer = NULL;
b552ecc4
MM
374 rfree(conn->keepalive_timer);
375 conn->keepalive_timer = NULL;
376 rfree(conn->hold_timer);
377 conn->hold_timer = NULL;
bcf23274
KK
378 rfree(conn->send_hold_timer);
379 conn->send_hold_timer = NULL;
11b32d91
OZ
380 rfree(conn->tx_ev);
381 conn->tx_ev = NULL;
d15b0b0a
OZ
382 rfree(conn->sk);
383 conn->sk = NULL;
384
1be0be1b
OZ
385 mb_free(conn->local_open_msg);
386 conn->local_open_msg = NULL;
387 mb_free(conn->remote_open_msg);
388 conn->remote_open_msg = NULL;
389 conn->local_open_length = 0;
390 conn->remote_open_length = 0;
391
d15b0b0a
OZ
392 mb_free(conn->local_caps);
393 conn->local_caps = NULL;
394 mb_free(conn->remote_caps);
395 conn->remote_caps = NULL;
4558adab
OZ
396
397 conn->notify_data = NULL;
398 conn->notify_size = 0;
11b32d91
OZ
399}
400
401
402/**
403 * bgp_update_startup_delay - update a startup delay
404 * @p: BGP instance
11b32d91 405 *
d15b0b0a
OZ
406 * This function updates a startup delay that is used to postpone next BGP
407 * connect. It also handles disable_after_error and might stop BGP instance
408 * when error happened and disable_after_error is on.
11b32d91
OZ
409 *
410 * It should be called when BGP protocol error happened.
411 */
412void
b99d3786 413bgp_update_startup_delay(struct bgp_proto *p)
11b32d91 414{
a22c3e59 415 const struct bgp_config *cf = p->cf;
11b32d91 416
b99d3786 417 DBG("BGP: Updating startup delay\n");
11b32d91 418
cc881bd1 419 if (p->last_proto_error && ((current_time() - p->last_proto_error) >= cf->error_amnesia_time S))
72382626
OZ
420 p->startup_delay = 0;
421
cc881bd1 422 p->last_proto_error = current_time();
11b32d91
OZ
423
424 if (cf->disable_after_error)
d15b0b0a
OZ
425 {
426 p->startup_delay = 0;
427 p->p.disabled = 1;
428 return;
429 }
11b32d91 430
11b32d91
OZ
431 if (!p->startup_delay)
432 p->startup_delay = cf->error_delay_time_min;
433 else
b99d3786 434 p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max);
c01e3741
MM
435}
436
11b32d91 437static void
8a68316e 438bgp_graceful_close_conn(struct bgp_conn *conn, int subcode, byte *data, uint len)
48e842cc 439{
11b32d91 440 switch (conn->state)
d15b0b0a
OZ
441 {
442 case BS_IDLE:
443 case BS_CLOSE:
444 return;
445
446 case BS_CONNECT:
447 case BS_ACTIVE:
448 bgp_conn_enter_idle_state(conn);
449 return;
450
451 case BS_OPENSENT:
452 case BS_OPENCONFIRM:
453 case BS_ESTABLISHED:
8a68316e
OZ
454 if (subcode < 0)
455 {
456 bgp_conn_enter_close_state(conn);
457 bgp_schedule_packet(conn, NULL, PKT_SCHEDULE_CLOSE);
458 }
459 else
460 bgp_error(conn, 6, subcode, data, len);
d15b0b0a
OZ
461 return;
462
463 default:
464 bug("bgp_graceful_close_conn: Unknown state %d", conn->state);
465 }
48e842cc
MM
466}
467
11b32d91
OZ
468static void
469bgp_down(struct bgp_proto *p)
470{
471 if (p->start_state > BSS_PREPARE)
d15b0b0a
OZ
472 {
473 bgp_setup_auth(p, 0);
474 bgp_close(p);
475 }
11b32d91 476
91d04583
OZ
477 p->neigh = NULL;
478
b99d3786 479 BGP_TRACE(D_EVENTS, "Down");
11b32d91
OZ
480 proto_notify_state(&p->p, PS_DOWN);
481}
482
483static void
484bgp_decision(void *vp)
485{
486 struct bgp_proto *p = vp;
487
488 DBG("BGP: Decision start\n");
d15b0b0a
OZ
489 if ((p->p.proto_state == PS_START) &&
490 (p->outgoing_conn.state == BS_IDLE) &&
491 (p->incoming_conn.state != BS_OPENCONFIRM) &&
e0835db4 492 !p->passive)
dd91e467 493 bgp_active(p);
11b32d91 494
d15b0b0a
OZ
495 if ((p->p.proto_state == PS_STOP) &&
496 (p->outgoing_conn.state == BS_IDLE) &&
497 (p->incoming_conn.state == BS_IDLE))
11b32d91
OZ
498 bgp_down(p);
499}
500
e0835db4
OZ
501static struct bgp_proto *
502bgp_spawn(struct bgp_proto *pp, ip_addr remote_ip)
503{
504 struct symbol *sym;
505 char fmt[SYM_MAX_LEN];
506
507 bsprintf(fmt, "%s%%0%dd", pp->cf->dynamic_name, pp->cf->dynamic_name_digits);
508
509 /* This is hack, we would like to share config, but we need to copy it now */
510 new_config = config;
511 cfg_mem = config->mem;
51f2e7af
MM
512 config->current_scope = config->root_scope;
513 sym = cf_default_name(config, fmt, &(pp->dynamic_name_counter));
e0835db4
OZ
514 proto_clone_config(sym, pp->p.cf);
515 new_config = NULL;
516 cfg_mem = NULL;
517
518 /* Just pass remote_ip to bgp_init() */
eac9250f 519 ((struct bgp_config *) sym->proto)->remote_ip = remote_ip;
e0835db4 520
eac9250f 521 return (void *) proto_spawn(sym->proto, 0);
e0835db4
OZ
522}
523
b99d3786 524void
8a68316e 525bgp_stop(struct bgp_proto *p, int subcode, byte *data, uint len)
11b32d91 526{
9d456d53
OZ
527 proto_shutdown_mpls_map(&p->p, 1);
528
11b32d91 529 proto_notify_state(&p->p, PS_STOP);
cd1d9961
OZ
530 bgp_graceful_close_conn(&p->outgoing_conn, subcode, data, len);
531 bgp_graceful_close_conn(&p->incoming_conn, subcode, data, len);
11b32d91
OZ
532 ev_schedule(p->event);
533}
534
cf31112f 535static inline void
d15b0b0a 536bgp_conn_set_state(struct bgp_conn *conn, uint new_state)
cf31112f
OZ
537{
538 if (conn->bgp->p.mrtdump & MD_STATES)
863ecfc7 539 bgp_dump_state_change(conn, conn->state, new_state);
cf31112f
OZ
540
541 conn->state = new_state;
542}
543
544void
545bgp_conn_enter_openconfirm_state(struct bgp_conn *conn)
546{
547 /* Really, most of the work is done in bgp_rx_open(). */
548 bgp_conn_set_state(conn, BS_OPENCONFIRM);
549}
550
11b32d91
OZ
551void
552bgp_conn_enter_established_state(struct bgp_conn *conn)
553{
554 struct bgp_proto *p = conn->bgp;
d15b0b0a
OZ
555 struct bgp_caps *local = conn->local_caps;
556 struct bgp_caps *peer = conn->remote_caps;
557 struct bgp_channel *c;
523f020b 558
11b32d91 559 BGP_TRACE(D_EVENTS, "BGP session established");
21d09632
OZ
560 p->last_established = current_time();
561 p->stats.fsm_established_transitions++;
11b32d91 562
9be9a264 563 /* For multi-hop BGP sessions */
a22c3e59
OZ
564 if (ipa_zero(p->local_ip))
565 p->local_ip = conn->sk->saddr;
9be9a264 566
23ee6b1c
OZ
567 /* For promiscuous sessions */
568 if (!p->remote_as)
569 p->remote_as = conn->received_as;
570
e16b0aef
OZ
571 /* In case of LLv6 is not valid during BGP start */
572 if (ipa_zero(p->link_addr) && p->neigh && p->neigh->iface && p->neigh->iface->llv6)
573 p->link_addr = p->neigh->iface->llv6->ip;
574
9e7b3ebd
OZ
575 conn->sk->fast_rx = 0;
576
11b32d91
OZ
577 p->conn = conn;
578 p->last_error_class = 0;
579 p->last_error_code = 0;
094d2bdb 580
d15b0b0a
OZ
581 p->as4_session = conn->as4_session;
582
583 p->route_refresh = peer->route_refresh;
584 p->enhanced_refresh = local->enhanced_refresh && peer->enhanced_refresh;
0c791f87 585
5bd73431
OZ
586 /* Whether we may handle possible GR/LLGR of peer (it has some AF GR-able) */
587 p->gr_ready = p->llgr_ready = 0; /* Updated later */
0c791f87 588
d15b0b0a
OZ
589 /* Whether peer is ready to handle our GR recovery */
590 int peer_gr_ready = peer->gr_aware && !(peer->gr_flags & BGP_GRF_RESTART);
0c791f87 591
d15b0b0a 592 if (p->gr_active_num)
a6f79ca5 593 tm_stop(p->gr_timer);
0c791f87 594
d15b0b0a
OZ
595 /* Number of active channels */
596 int num = 0;
597
863ecfc7
OZ
598 /* Summary state of ADD_PATH RX for active channels */
599 uint summary_add_path_rx = 0;
600
54430df9 601 BGP_WALK_CHANNELS(p, c)
d15b0b0a
OZ
602 {
603 const struct bgp_af_caps *loc = bgp_find_af_caps(local, c->afi);
604 const struct bgp_af_caps *rem = bgp_find_af_caps(peer, c->afi);
605
d15b0b0a
OZ
606 int active = loc->ready && rem->ready;
607 c->c.disabled = !active;
682d3f7d 608 c->c.reloadable = p->route_refresh || c->cf->import_table;
d15b0b0a
OZ
609
610 c->index = active ? num++ : 0;
611
612 c->feed_state = BFS_NONE;
613 c->load_state = BFS_NONE;
614
615 /* Channels where peer may do GR */
5bd73431
OZ
616 uint gr_ready = active && local->gr_aware && rem->gr_able;
617 uint llgr_ready = active && local->llgr_aware && rem->llgr_able;
618
619 c->gr_ready = gr_ready || llgr_ready;
d15b0b0a 620 p->gr_ready = p->gr_ready || c->gr_ready;
5bd73431
OZ
621 p->llgr_ready = p->llgr_ready || llgr_ready;
622
623 /* Remember last LLGR stale time */
624 c->stale_time = local->llgr_aware ? rem->llgr_time : 0;
0c791f87 625
d15b0b0a
OZ
626 /* Channels not able to recover gracefully */
627 if (p->p.gr_recovery && (!active || !peer_gr_ready))
628 channel_graceful_restart_unlock(&c->c);
9aed29e6 629
d15b0b0a
OZ
630 /* Channels waiting for local convergence */
631 if (p->p.gr_recovery && loc->gr_able && peer_gr_ready)
632 c->c.gr_wait = 1;
633
5bd73431
OZ
634 /* Channels where regular graceful restart failed */
635 if ((c->gr_active == BGP_GRS_ACTIVE) &&
636 !(active && rem->gr_able && (rem->gr_af_flags & BGP_GRF_FORWARDING)))
637 bgp_graceful_restart_done(c);
638
639 /* Channels where regular long-lived restart failed */
640 if ((c->gr_active == BGP_GRS_LLGR) &&
641 !(active && rem->llgr_able && (rem->gr_af_flags & BGP_LLGRF_FORWARDING)))
d15b0b0a
OZ
642 bgp_graceful_restart_done(c);
643
644 /* GR capability implies that neighbor will send End-of-RIB */
645 if (peer->gr_aware)
646 c->load_state = BFS_LOADING;
647
d8022d26 648 c->ext_next_hop = c->cf->ext_next_hop && (bgp_channel_is_ipv6(c) || rem->ext_next_hop);
d15b0b0a
OZ
649 c->add_path_rx = (loc->add_path & BGP_ADD_PATH_RX) && (rem->add_path & BGP_ADD_PATH_TX);
650 c->add_path_tx = (loc->add_path & BGP_ADD_PATH_TX) && (rem->add_path & BGP_ADD_PATH_RX);
651
863ecfc7
OZ
652 if (active)
653 summary_add_path_rx |= !c->add_path_rx ? 1 : 2;
654
f8aad5d5 655 /* Update RA mode */
d15b0b0a
OZ
656 if (c->add_path_tx)
657 c->c.ra_mode = RA_ANY;
f8aad5d5
OZ
658 else if (c->cf->secondary)
659 c->c.ra_mode = RA_ACCEPTED;
660 else
661 c->c.ra_mode = RA_OPTIMAL;
d15b0b0a
OZ
662 }
663
664 p->afi_map = mb_alloc(p->p.pool, num * sizeof(u32));
665 p->channel_map = mb_alloc(p->p.pool, num * sizeof(void *));
666 p->channel_count = num;
863ecfc7 667 p->summary_add_path_rx = summary_add_path_rx;
d15b0b0a 668
54430df9 669 BGP_WALK_CHANNELS(p, c)
d15b0b0a
OZ
670 {
671 if (c->c.disabled)
672 continue;
673
674 p->afi_map[c->index] = c->afi;
675 p->channel_map[c->index] = c;
676 }
677
bcf23274
KK
678 /* Breaking rx_hook for simulating receive problem */
679 if (p->cf->disable_rx)
680 {
681 conn->sk->rx_hook = NULL;
682 tm_stop(conn->hold_timer);
683 }
684
d15b0b0a 685 /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */
9aed29e6 686
cf31112f 687 bgp_conn_set_state(conn, BS_ESTABLISHED);
11b32d91 688 proto_notify_state(&p->p, PS_UP);
aa3c3549
OZ
689 bmp_peer_up(p, conn->local_open_msg, conn->local_open_length,
690 conn->remote_open_msg, conn->remote_open_length);
11b32d91
OZ
691}
692
693static void
4558adab 694bgp_conn_leave_established_state(struct bgp_conn *conn, struct bgp_proto *p)
11b32d91
OZ
695{
696 BGP_TRACE(D_EVENTS, "BGP session closed");
21d09632 697 p->last_established = current_time();
11b32d91
OZ
698 p->conn = NULL;
699
700 if (p->p.proto_state == PS_UP)
cd1d9961 701 bgp_stop(p, 0, NULL, 0);
4558adab
OZ
702
703 bmp_peer_down(p, p->last_error_class,
704 conn->notify_code, conn->notify_subcode,
705 conn->notify_data, conn->notify_size);
11b32d91
OZ
706}
707
708void
709bgp_conn_enter_close_state(struct bgp_conn *conn)
710{
711 struct bgp_proto *p = conn->bgp;
712 int os = conn->state;
713
cf31112f 714 bgp_conn_set_state(conn, BS_CLOSE);
a6f79ca5 715 tm_stop(conn->keepalive_timer);
11b32d91
OZ
716 conn->sk->rx_hook = NULL;
717
48b15ef1
OZ
718 /* Timeout for CLOSE state, if we cannot send notification soon then we just hangup */
719 bgp_start_timer(conn->hold_timer, 10);
720
11b32d91 721 if (os == BS_ESTABLISHED)
4558adab 722 bgp_conn_leave_established_state(conn, p);
11b32d91
OZ
723}
724
725void
726bgp_conn_enter_idle_state(struct bgp_conn *conn)
727{
728 struct bgp_proto *p = conn->bgp;
729 int os = conn->state;
730
731 bgp_close_conn(conn);
cf31112f 732 bgp_conn_set_state(conn, BS_IDLE);
11b32d91
OZ
733 ev_schedule(p->event);
734
735 if (os == BS_ESTABLISHED)
4558adab 736 bgp_conn_leave_established_state(conn, p);
11b32d91
OZ
737}
738
6eda3f13
OZ
739/**
740 * bgp_handle_graceful_restart - handle detected BGP graceful restart
741 * @p: BGP instance
742 *
743 * This function is called when a BGP graceful restart of the neighbor is
744 * detected (when the TCP connection fails or when a new TCP connection
745 * appears). The function activates processing of the restart - starts routing
746 * table refresh cycle and activates BGP restart timer. The protocol state goes
747 * back to %PS_START, but changing BGP state back to %BS_IDLE is left for the
748 * caller.
749 */
0c791f87
OZ
750void
751bgp_handle_graceful_restart(struct bgp_proto *p)
752{
753 ASSERT(p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready);
754
755 BGP_TRACE(D_EVENTS, "Neighbor graceful restart detected%s",
d15b0b0a
OZ
756 p->gr_active_num ? " - already pending" : "");
757
758 p->gr_active_num = 0;
0c791f87 759
d15b0b0a 760 struct bgp_channel *c;
54430df9 761 BGP_WALK_CHANNELS(p, c)
d15b0b0a 762 {
7fc55925
OZ
763 /* FIXME: perhaps check for channel state instead of disabled flag? */
764 if (c->c.disabled)
765 continue;
766
d15b0b0a
OZ
767 if (c->gr_ready)
768 {
5bd73431
OZ
769 p->gr_active_num++;
770
771 switch (c->gr_active)
772 {
773 case BGP_GRS_NONE:
774 c->gr_active = BGP_GRS_ACTIVE;
775 rt_refresh_begin(c->c.table, &c->c);
776 break;
777
778 case BGP_GRS_ACTIVE:
d15b0b0a 779 rt_refresh_end(c->c.table, &c->c);
5bd73431
OZ
780 rt_refresh_begin(c->c.table, &c->c);
781 break;
0c791f87 782
5bd73431
OZ
783 case BGP_GRS_LLGR:
784 rt_refresh_begin(c->c.table, &c->c);
785 rt_modify_stale(c->c.table, &c->c);
786 break;
787 }
d15b0b0a
OZ
788 }
789 else
790 {
791 /* Just flush the routes */
792 rt_refresh_begin(c->c.table, &c->c);
793 rt_refresh_end(c->c.table, &c->c);
794 }
7fc55925
OZ
795
796 /* Reset bucket and prefix tables */
797 bgp_free_bucket_table(c);
798 bgp_free_prefix_table(c);
799 bgp_init_bucket_table(c);
800 bgp_init_prefix_table(c);
801 c->packets_to_send = 0;
d15b0b0a
OZ
802 }
803
e62cd033
OZ
804 /* p->gr_ready -> at least one active channel is c->gr_ready */
805 ASSERT(p->gr_active_num > 0);
806
d15b0b0a 807 proto_notify_state(&p->p, PS_START);
5bd73431 808 tm_start(p->gr_timer, p->conn->remote_caps->gr_time S);
0c791f87
OZ
809}
810
6eda3f13
OZ
811/**
812 * bgp_graceful_restart_done - finish active BGP graceful restart
d15b0b0a 813 * @c: BGP channel
6eda3f13
OZ
814 *
815 * This function is called when the active BGP graceful restart of the neighbor
d15b0b0a
OZ
816 * should be finished for channel @c - either successfully (the neighbor sends
817 * all paths and reports end-of-RIB for given AFI/SAFI on the new session) or
818 * unsuccessfully (the neighbor does not support BGP graceful restart on the new
819 * session). The function ends the routing table refresh cycle.
6eda3f13 820 */
0c791f87 821void
d15b0b0a 822bgp_graceful_restart_done(struct bgp_channel *c)
0c791f87 823{
d15b0b0a
OZ
824 struct bgp_proto *p = (void *) c->c.proto;
825
826 ASSERT(c->gr_active);
827 c->gr_active = 0;
828 p->gr_active_num--;
829
830 if (!p->gr_active_num)
831 BGP_TRACE(D_EVENTS, "Neighbor graceful restart done");
832
5bd73431 833 tm_stop(c->stale_timer);
d15b0b0a 834 rt_refresh_end(c->c.table, &c->c);
0c791f87
OZ
835}
836
6eda3f13
OZ
837/**
838 * bgp_graceful_restart_timeout - timeout of graceful restart 'restart timer'
839 * @t: timer
840 *
841 * This function is a timeout hook for @gr_timer, implementing BGP restart time
842 * limit for reestablisment of the BGP session after the graceful restart. When
843 * fired, we just proceed with the usual protocol restart.
844 */
845
0c791f87
OZ
846static void
847bgp_graceful_restart_timeout(timer *t)
848{
849 struct bgp_proto *p = t->data;
850
851 BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout");
5bd73431
OZ
852
853 if (p->llgr_ready)
854 {
855 struct bgp_channel *c;
54430df9 856 BGP_WALK_CHANNELS(p, c)
5bd73431
OZ
857 {
858 /* Channel is not in GR and is already flushed */
859 if (!c->gr_active)
860 continue;
861
862 /* Channel is already in LLGR from past restart */
863 if (c->gr_active == BGP_GRS_LLGR)
864 continue;
865
866 /* Channel is in GR, but does not support LLGR -> stop GR */
867 if (!c->stale_time)
868 {
869 bgp_graceful_restart_done(c);
870 continue;
871 }
872
873 /* Channel is in GR, and supports LLGR -> start LLGR */
874 c->gr_active = BGP_GRS_LLGR;
875 tm_start(c->stale_timer, c->stale_time S);
876 rt_modify_stale(c->c.table, &c->c);
877 }
878 }
879 else
880 bgp_stop(p, 0, NULL, 0);
881}
882
883static void
884bgp_long_lived_stale_timeout(timer *t)
885{
886 struct bgp_channel *c = t->data;
887 struct bgp_proto *p = (void *) c->c.proto;
888
889 BGP_TRACE(D_EVENTS, "Long-lived stale timeout");
890
891 bgp_graceful_restart_done(c);
0c791f87
OZ
892}
893
9aed29e6
OZ
894
895/**
896 * bgp_refresh_begin - start incoming enhanced route refresh sequence
d15b0b0a 897 * @c: BGP channel
9aed29e6
OZ
898 *
899 * This function is called when an incoming enhanced route refresh sequence is
900 * started by the neighbor, demarcated by the BoRR packet. The function updates
901 * the load state and starts the routing table refresh cycle. Note that graceful
902 * restart also uses routing table refresh cycle, but RFC 7313 and load states
903 * ensure that these two sequences do not overlap.
904 */
905void
d15b0b0a 906bgp_refresh_begin(struct bgp_channel *c)
9aed29e6 907{
d15b0b0a
OZ
908 struct bgp_proto *p = (void *) c->c.proto;
909
910 if (c->load_state == BFS_LOADING)
911 { log(L_WARN "%s: BEGIN-OF-RR received before END-OF-RIB, ignoring", p->p.name); return; }
9aed29e6 912
d15b0b0a
OZ
913 c->load_state = BFS_REFRESHING;
914 rt_refresh_begin(c->c.table, &c->c);
682d3f7d
OZ
915
916 if (c->c.in_table)
917 rt_refresh_begin(c->c.in_table, &c->c);
9aed29e6
OZ
918}
919
920/**
921 * bgp_refresh_end - finish incoming enhanced route refresh sequence
d15b0b0a 922 * @c: BGP channel
9aed29e6
OZ
923 *
924 * This function is called when an incoming enhanced route refresh sequence is
925 * finished by the neighbor, demarcated by the EoRR packet. The function updates
926 * the load state and ends the routing table refresh cycle. Routes not received
927 * during the sequence are removed by the nest.
928 */
929void
d15b0b0a 930bgp_refresh_end(struct bgp_channel *c)
9aed29e6 931{
d15b0b0a 932 struct bgp_proto *p = (void *) c->c.proto;
9aed29e6 933
d15b0b0a
OZ
934 if (c->load_state != BFS_REFRESHING)
935 { log(L_WARN "%s: END-OF-RR received without prior BEGIN-OF-RR, ignoring", p->p.name); return; }
936
937 c->load_state = BFS_NONE;
938 rt_refresh_end(c->c.table, &c->c);
682d3f7d
OZ
939
940 if (c->c.in_table)
941 rt_prune_sync(c->c.in_table, 0);
9aed29e6
OZ
942}
943
944
c01e3741
MM
945static void
946bgp_send_open(struct bgp_conn *conn)
947{
948 DBG("BGP: Sending open\n");
949 conn->sk->rx_hook = bgp_rx;
b552ecc4 950 conn->sk->tx_hook = bgp_tx;
a6f79ca5 951 tm_stop(conn->connect_timer);
4a50c8bd 952 bgp_prepare_capabilities(conn);
d15b0b0a 953 bgp_schedule_packet(conn, NULL, PKT_OPEN);
cf31112f 954 bgp_conn_set_state(conn, BS_OPENSENT);
3fdbafb6 955 bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
c01e3741
MM
956}
957
3fdbafb6
MM
958static void
959bgp_connected(sock *sk)
c01e3741
MM
960{
961 struct bgp_conn *conn = sk->data;
85368cd4 962 struct bgp_proto *p = conn->bgp;
c01e3741 963
85368cd4 964 BGP_TRACE(D_EVENTS, "Connected");
c01e3741 965 bgp_send_open(conn);
c01e3741
MM
966}
967
968static void
969bgp_connect_timeout(timer *t)
970{
3fdbafb6 971 struct bgp_conn *conn = t->data;
85368cd4 972 struct bgp_proto *p = conn->bgp;
c01e3741 973
85368cd4 974 DBG("BGP: connect_timeout\n");
11b32d91 975 if (p->p.proto_state == PS_START)
d15b0b0a
OZ
976 {
977 bgp_close_conn(conn);
978 bgp_connect(p);
979 }
11b32d91
OZ
980 else
981 bgp_conn_enter_idle_state(conn);
c01e3741
MM
982}
983
984static void
3fdbafb6 985bgp_sock_err(sock *sk, int err)
c01e3741
MM
986{
987 struct bgp_conn *conn = sk->data;
85368cd4 988 struct bgp_proto *p = conn->bgp;
c01e3741 989
47597724
OZ
990 /*
991 * This error hook may be called either asynchronously from main
992 * loop, or synchronously from sk_send(). But sk_send() is called
993 * only from bgp_tx() and bgp_kick_tx(), which are both called
994 * asynchronously from main loop. Moreover, they end if err hook is
995 * called. Therefore, we could suppose that it is always called
996 * asynchronously.
997 */
998
11b32d91
OZ
999 bgp_store_error(p, conn, BE_SOCKET, err);
1000
53943a00
MM
1001 if (err)
1002 BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
1003 else
1004 BGP_TRACE(D_EVENTS, "Connection closed");
11b32d91 1005
0c791f87
OZ
1006 if ((conn->state == BS_ESTABLISHED) && p->gr_ready)
1007 bgp_handle_graceful_restart(p);
1008
11b32d91 1009 bgp_conn_enter_idle_state(conn);
c01e3741
MM
1010}
1011
3fdbafb6
MM
1012static void
1013bgp_hold_timeout(timer *t)
1014{
1015 struct bgp_conn *conn = t->data;
48b15ef1 1016 struct bgp_proto *p = conn->bgp;
3fdbafb6 1017
ea89da38
OZ
1018 DBG("BGP: Hold timeout\n");
1019
48b15ef1
OZ
1020 /* We are already closing the connection - just do hangup */
1021 if (conn->state == BS_CLOSE)
1022 {
1023 BGP_TRACE(D_EVENTS, "Connection stalled");
1024 bgp_conn_enter_idle_state(conn);
1025 return;
1026 }
1027
ea89da38
OZ
1028 /* If there is something in input queue, we are probably congested
1029 and perhaps just not processed BGP packets in time. */
1030
1031 if (sk_rx_ready(conn->sk) > 0)
1032 bgp_start_timer(conn->hold_timer, 10);
5bd73431
OZ
1033 else if ((conn->state == BS_ESTABLISHED) && p->llgr_ready)
1034 {
1035 BGP_TRACE(D_EVENTS, "Hold timer expired");
1036 bgp_handle_graceful_restart(p);
1037 bgp_conn_enter_idle_state(conn);
1038 }
ea89da38
OZ
1039 else
1040 bgp_error(conn, 4, 0, NULL, 0);
3fdbafb6
MM
1041}
1042
1043static void
1044bgp_keepalive_timeout(timer *t)
1045{
1046 struct bgp_conn *conn = t->data;
1047
1048 DBG("BGP: Keepalive timer\n");
d15b0b0a 1049 bgp_schedule_packet(conn, NULL, PKT_KEEPALIVE);
bd22d7f4
OZ
1050
1051 /* Kick TX a bit faster */
1052 if (ev_active(conn->tx_ev))
1053 ev_run(conn->tx_ev);
3fdbafb6
MM
1054}
1055
bcf23274
KK
1056void
1057bgp_send_hold_timeout(timer *t)
1058{
1059 struct bgp_conn *conn = t->data;
1060 struct bgp_proto *p = conn->bgp;
1061
1062 if (conn->state == BS_CLOSE)
1063 return;
1064
1065 /* Error codes not yet assigned by IANA */
1066 uint code = 4;
1067 uint subcode = 1;
1068
1069 /* Like bgp_error() but without NOTIFICATION */
1070 bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, NULL, 0);
1071 bgp_store_error(p, conn, BE_BGP_TX, (code << 16) | subcode);
1072 bgp_conn_enter_idle_state(conn);
1073 bgp_update_startup_delay(p);
1074 bgp_stop(p, 0, NULL, 0);
1075}
1076
c01e3741 1077static void
6fd766c1 1078bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
c01e3741 1079{
6fd766c1 1080 conn->sk = NULL;
c01e3741 1081 conn->bgp = p;
d15b0b0a 1082
72a6ef11 1083 conn->packets_to_send = 0;
d15b0b0a
OZ
1084 conn->channels_to_send = 0;
1085 conn->last_channel = 0;
1086 conn->last_channel_count = 0;
1087
a6f79ca5
OZ
1088 conn->connect_timer = tm_new_init(p->p.pool, bgp_connect_timeout, conn, 0, 0);
1089 conn->hold_timer = tm_new_init(p->p.pool, bgp_hold_timeout, conn, 0, 0);
1090 conn->keepalive_timer = tm_new_init(p->p.pool, bgp_keepalive_timeout, conn, 0, 0);
bcf23274 1091 conn->send_hold_timer = tm_new_init(p->p.pool, bgp_send_hold_timeout, conn, 0, 0);
c01e3741 1092
961671c0 1093 conn->tx_ev = ev_new_init(p->p.pool, bgp_kick_tx, conn);
c01e3741
MM
1094}
1095
6fd766c1 1096static void
e81b440f 1097bgp_setup_sk(struct bgp_conn *conn, sock *s)
6fd766c1
MM
1098{
1099 s->data = conn;
6fd766c1 1100 s->err_hook = bgp_sock_err;
9e7b3ebd 1101 s->fast_rx = 1;
6fd766c1
MM
1102 conn->sk = s;
1103}
1104
11b32d91 1105static void
dd91e467 1106bgp_active(struct bgp_proto *p)
11b32d91 1107{
6cf72d7a 1108 int delay = MAX(1, p->cf->connect_delay_time);
11b32d91
OZ
1109 struct bgp_conn *conn = &p->outgoing_conn;
1110
1111 BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
1112 bgp_setup_conn(p, conn);
cf31112f 1113 bgp_conn_set_state(conn, BS_ACTIVE);
d15b0b0a 1114 bgp_start_timer(conn->connect_timer, delay);
11b32d91
OZ
1115}
1116
54e55169
MM
1117/**
1118 * bgp_connect - initiate an outgoing connection
1119 * @p: BGP instance
1120 *
1121 * The bgp_connect() function creates a new &bgp_conn and initiates
1122 * a TCP connection to the peer. The rest of connection setup is governed
1123 * by the BGP state machine as described in the standard.
1124 */
c01e3741
MM
1125static void
1126bgp_connect(struct bgp_proto *p) /* Enter Connect state and start establishing connection */
1127{
b552ecc4 1128 struct bgp_conn *conn = &p->outgoing_conn;
13c6cf8a 1129 int hops = p->cf->multihop ?: 1;
c01e3741
MM
1130
1131 DBG("BGP: Connecting\n");
d15b0b0a 1132 sock *s = sk_new(p->p.pool);
c01e3741 1133 s->type = SK_TCP_ACTIVE;
a22c3e59
OZ
1134 s->saddr = p->local_ip;
1135 s->daddr = p->remote_ip;
dcde7ae5 1136 s->dport = p->cf->remote_port;
53ffbff3 1137 s->iface = p->neigh ? p->neigh->iface : NULL;
943478b0 1138 s->vrf = p->p.vrf;
b1b19433 1139 s->ttl = p->cf->ttl_security ? 255 : hops;
06e0d1b6
OZ
1140 s->rbsize = p->cf->enable_extended_messages ? BGP_RX_BUFFER_EXT_SIZE : BGP_RX_BUFFER_SIZE;
1141 s->tbsize = p->cf->enable_extended_messages ? BGP_TX_BUFFER_EXT_SIZE : BGP_TX_BUFFER_SIZE;
a39b165e
OZ
1142 s->tos = IP_PREC_INTERNET_CONTROL;
1143 s->password = p->cf->password;
1144 s->tx_hook = bgp_connected;
2b712554 1145 s->flags = p->cf->free_bind ? SKF_FREEBIND : 0;
470740f9
OZ
1146 BGP_TRACE(D_EVENTS, "Connecting to %I%J from local address %I%J",
1147 s->daddr, ipa_is_link_local(s->daddr) ? p->cf->iface : NULL,
88a183c6 1148 s->saddr, ipa_is_link_local(s->saddr) ? s->iface : NULL);
6fd766c1 1149 bgp_setup_conn(p, conn);
e81b440f 1150 bgp_setup_sk(conn, s);
cf31112f 1151 bgp_conn_set_state(conn, BS_CONNECT);
b1b19433
OZ
1152
1153 if (sk_open(s) < 0)
05476c4d 1154 goto err;
b1b19433
OZ
1155
1156 /* Set minimal receive TTL if needed */
1157 if (p->cf->ttl_security)
b1b19433 1158 if (sk_set_min_ttl(s, 256 - hops) < 0)
05476c4d 1159 goto err;
b1b19433 1160
c01e3741 1161 DBG("BGP: Waiting for connect success\n");
d15b0b0a 1162 bgp_start_timer(conn->connect_timer, p->cf->connect_retry_time);
05476c4d
OZ
1163 return;
1164
d15b0b0a 1165err:
05476c4d
OZ
1166 sk_log_error(s, p->p.name);
1167 bgp_sock_err(s, 0);
1168 return;
c01e3741
MM
1169}
1170
e0835db4
OZ
1171static inline int bgp_is_dynamic(struct bgp_proto *p)
1172{ return ipa_zero(p->remote_ip); }
1173
374917ad
OZ
1174/**
1175 * bgp_find_proto - find existing proto for incoming connection
1176 * @sk: TCP socket
1177 *
1178 */
1179static struct bgp_proto *
1180bgp_find_proto(sock *sk)
1181{
e0835db4 1182 struct bgp_proto *best = NULL;
d15b0b0a 1183 struct bgp_proto *p;
374917ad 1184
470740f9
OZ
1185 /* sk->iface is valid only if src or dst address is link-local */
1186 int link = ipa_is_link_local(sk->saddr) || ipa_is_link_local(sk->daddr);
1187
d15b0b0a
OZ
1188 WALK_LIST(p, proto_list)
1189 if ((p->p.proto == &proto_bgp) &&
e0835db4
OZ
1190 (ipa_equal(p->remote_ip, sk->daddr) || bgp_is_dynamic(p)) &&
1191 (!p->cf->remote_range || ipa_in_netX(sk->daddr, p->cf->remote_range)) &&
1192 (p->p.vrf == sk->vrf) &&
1193 (p->cf->local_port == sk->sport) &&
470740f9
OZ
1194 (!link || (p->cf->iface == sk->iface)) &&
1195 (ipa_zero(p->cf->local_ip) || ipa_equal(p->cf->local_ip, sk->saddr)))
e0835db4
OZ
1196 {
1197 best = p;
374917ad 1198
e0835db4
OZ
1199 if (!bgp_is_dynamic(p))
1200 break;
1201 }
1202
1203 return best;
374917ad
OZ
1204}
1205
54e55169
MM
1206/**
1207 * bgp_incoming_connection - handle an incoming connection
1208 * @sk: TCP socket
1209 * @dummy: unused
1210 *
1211 * This function serves as a socket hook for accepting of new BGP
1212 * connections. It searches a BGP instance corresponding to the peer
1213 * which has connected and if such an instance exists, it creates a
1214 * &bgp_conn structure, attaches it to the instance and either sends
1215 * an Open message or (if there already is an active connection) it
1216 * closes the new connection by sending a Notification message.
1217 */
48e842cc 1218static int
3e236955 1219bgp_incoming_connection(sock *sk, uint dummy UNUSED)
c01e3741 1220{
374917ad
OZ
1221 struct bgp_proto *p;
1222 int acc, hops;
c01e3741 1223
48e842cc 1224 DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
374917ad
OZ
1225 p = bgp_find_proto(sk);
1226 if (!p)
d15b0b0a
OZ
1227 {
1228 log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)",
1229 sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, sk->dport);
1230 rfree(sk);
1231 return 0;
1232 }
374917ad 1233
487c6961
OZ
1234 /*
1235 * BIRD should keep multiple incoming connections in OpenSent state (for
1236 * details RFC 4271 8.2.1 par 3), but it keeps just one. Duplicate incoming
1237 * connections are rejected istead. The exception is the case where an
1238 * incoming connection triggers a graceful restart.
1239 */
1240
374917ad
OZ
1241 acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
1242 (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
dd91e467 1243
374917ad 1244 if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
d15b0b0a
OZ
1245 {
1246 bgp_store_error(p, NULL, BE_MISC, BEM_GRACEFUL_RESTART);
1247 bgp_handle_graceful_restart(p);
1248 bgp_conn_enter_idle_state(p->conn);
1249 acc = 1;
1250
1251 /* There might be separate incoming connection in OpenSent state */
1252 if (p->incoming_conn.state > BS_ACTIVE)
1253 bgp_close_conn(&p->incoming_conn);
1254 }
374917ad
OZ
1255
1256 BGP_TRACE(D_EVENTS, "Incoming connection from %I%J (port %d) %s",
1257 sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL,
1258 sk->dport, acc ? "accepted" : "rejected");
1259
1260 if (!acc)
d15b0b0a
OZ
1261 {
1262 rfree(sk);
1263 return 0;
1264 }
374917ad 1265
13c6cf8a 1266 hops = p->cf->multihop ?: 1;
374917ad
OZ
1267
1268 if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0)
1269 goto err;
1270
1271 if (p->cf->ttl_security)
1272 if (sk_set_min_ttl(sk, 256 - hops) < 0)
1273 goto err;
1274
06e0d1b6 1275 if (p->cf->enable_extended_messages)
d15b0b0a
OZ
1276 {
1277 sk->rbsize = BGP_RX_BUFFER_EXT_SIZE;
1278 sk->tbsize = BGP_TX_BUFFER_EXT_SIZE;
1279 sk_reallocate(sk);
1280 }
06e0d1b6 1281
e0835db4
OZ
1282 /* For dynamic BGP, spawn new instance and postpone the socket */
1283 if (bgp_is_dynamic(p))
1284 {
1285 p = bgp_spawn(p, sk->daddr);
1286 p->postponed_sk = sk;
1287 rmove(sk, p->p.pool);
1288 return 0;
1289 }
1290
1291 rmove(sk, p->p.pool);
374917ad
OZ
1292 bgp_setup_conn(p, &p->incoming_conn);
1293 bgp_setup_sk(&p->incoming_conn, sk);
1294 bgp_send_open(&p->incoming_conn);
1295 return 0;
1296
1297err:
1298 sk_log_error(sk, p->p.name);
1299 log(L_ERR "%s: Incoming connection aborted", p->p.name);
48e842cc
MM
1300 rfree(sk);
1301 return 0;
1302}
1303
2af25a97 1304static void
e81b440f 1305bgp_listen_sock_err(sock *sk UNUSED, int err)
2af25a97
OZ
1306{
1307 if (err == ECONNABORTED)
1308 log(L_WARN "BGP: Incoming connection aborted");
1309 else
a34b0934 1310 log(L_ERR "BGP: Error on listening socket: %M", err);
2af25a97
OZ
1311}
1312
acfce55c
MM
1313static void
1314bgp_start_neighbor(struct bgp_proto *p)
1315{
9be9a264
OZ
1316 /* Called only for single-hop BGP sessions */
1317
a22c3e59
OZ
1318 if (ipa_zero(p->local_ip))
1319 p->local_ip = p->neigh->ifa->ip;
ad440a57 1320
a22c3e59
OZ
1321 if (ipa_is_link_local(p->local_ip))
1322 p->link_addr = p->local_ip;
153f02da
OZ
1323 else if (p->neigh->iface->llv6)
1324 p->link_addr = p->neigh->iface->llv6->ip;
11b32d91 1325
6fd766c1 1326 bgp_initiate(p);
48e842cc
MM
1327}
1328
1329static void
1330bgp_neigh_notify(neighbor *n)
1331{
1332 struct bgp_proto *p = (struct bgp_proto *) n->proto;
523f020b
OZ
1333 int ps = p->p.proto_state;
1334
1335 if (n != p->neigh)
1336 return;
48e842cc 1337
523f020b 1338 if ((ps == PS_DOWN) || (ps == PS_STOP))
b21955e0
OZ
1339 return;
1340
523f020b
OZ
1341 int prepare = (ps == PS_START) && (p->start_state == BSS_PREPARE);
1342
1343 if (n->scope <= 0)
d15b0b0a
OZ
1344 {
1345 if (!prepare)
48e842cc 1346 {
d15b0b0a
OZ
1347 BGP_TRACE(D_EVENTS, "Neighbor lost");
1348 bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST);
1349 /* Perhaps also run bgp_update_startup_delay(p)? */
830ba75e 1350 bgp_stop(p, 0, NULL, 0);
523f020b 1351 }
d15b0b0a 1352 }
523f020b 1353 else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
d15b0b0a
OZ
1354 {
1355 if (!prepare)
523f020b 1356 {
d15b0b0a
OZ
1357 BGP_TRACE(D_EVENTS, "Link down");
1358 bgp_store_error(p, NULL, BE_MISC, BEM_LINK_DOWN);
1359 if (ps == PS_UP)
1360 bgp_update_startup_delay(p);
830ba75e 1361 bgp_stop(p, 0, NULL, 0);
48e842cc 1362 }
d15b0b0a 1363 }
48e842cc 1364 else
d15b0b0a
OZ
1365 {
1366 if (prepare)
48e842cc 1367 {
d15b0b0a
OZ
1368 BGP_TRACE(D_EVENTS, "Neighbor ready");
1369 bgp_start_neighbor(p);
48e842cc 1370 }
d15b0b0a 1371 }
48e842cc
MM
1372}
1373
1ec52253
OZ
1374static void
1375bgp_bfd_notify(struct bfd_request *req)
1376{
1377 struct bgp_proto *p = req->data;
1378 int ps = p->p.proto_state;
1379
1380 if (req->down && ((ps == PS_START) || (ps == PS_UP)))
d15b0b0a
OZ
1381 {
1382 BGP_TRACE(D_EVENTS, "BFD session down");
1383 bgp_store_error(p, NULL, BE_MISC, BEM_BFD_DOWN);
5bd73431 1384
9d3fc306 1385 if (req->opts.mode == BGP_BFD_GRACEFUL)
5bd73431
OZ
1386 {
1387 /* Trigger graceful restart */
1388 if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
1389 bgp_handle_graceful_restart(p);
1390
1391 if (p->incoming_conn.state > BS_IDLE)
1392 bgp_conn_enter_idle_state(&p->incoming_conn);
1393
1394 if (p->outgoing_conn.state > BS_IDLE)
1395 bgp_conn_enter_idle_state(&p->outgoing_conn);
1396 }
1397 else
1398 {
1399 /* Trigger session down */
1400 if (ps == PS_UP)
1401 bgp_update_startup_delay(p);
1402 bgp_stop(p, 0, NULL, 0);
1403 }
d15b0b0a 1404 }
1ec52253
OZ
1405}
1406
1407static void
9d3fc306 1408bgp_update_bfd(struct bgp_proto *p, const struct bfd_options *bfd)
1ec52253 1409{
9d3fc306
OZ
1410 if (bfd && p->bfd_req)
1411 bfd_update_request(p->bfd_req, bfd);
1412
1413 if (bfd && !p->bfd_req && !bgp_is_dynamic(p))
a22c3e59 1414 p->bfd_req = bfd_request_session(p->p.pool, p->remote_ip, p->local_ip,
1ec52253 1415 p->cf->multihop ? NULL : p->neigh->iface,
9d3fc306 1416 p->p.vrf, bgp_bfd_notify, p, bfd);
1ec52253 1417
9d3fc306 1418 if (!bfd && p->bfd_req)
d15b0b0a
OZ
1419 {
1420 rfree(p->bfd_req);
1421 p->bfd_req = NULL;
1422 }
1ec52253
OZ
1423}
1424
d15b0b0a
OZ
1425static void
1426bgp_reload_routes(struct channel *C)
bf47fe4b 1427{
d15b0b0a
OZ
1428 struct bgp_proto *p = (void *) C->proto;
1429 struct bgp_channel *c = (void *) C;
bf47fe4b 1430
d8130da8
OZ
1431 /* For MPLS channel, reload all MPLS-aware channels */
1432 if (C == p->p.mpls_channel)
1433 {
1434 BGP_WALK_CHANNELS(p, c)
1435 if ((c->desc->mpls) && (p->route_refresh || c->c.in_table))
1436 bgp_reload_routes(&c->c);
1437
1438 return;
1439 }
1440
54430df9
OZ
1441 /* Ignore non-BGP channels */
1442 if (C->channel != &channel_bgp)
1443 return;
1444
682d3f7d 1445 ASSERT(p->conn && (p->route_refresh || c->c.in_table));
d15b0b0a 1446
682d3f7d
OZ
1447 if (c->c.in_table)
1448 channel_schedule_reload(C);
1449 else
1450 bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH);
bf47fe4b
OZ
1451}
1452
0c791f87 1453static void
d15b0b0a 1454bgp_feed_begin(struct channel *C, int initial)
0c791f87 1455{
d15b0b0a
OZ
1456 struct bgp_proto *p = (void *) C->proto;
1457 struct bgp_channel *c = (void *) C;
9aed29e6 1458
54430df9
OZ
1459 /* Ignore non-BGP channels */
1460 if (C->channel != &channel_bgp)
1461 return;
1462
9aed29e6
OZ
1463 /* This should not happen */
1464 if (!p->conn)
0c791f87
OZ
1465 return;
1466
9aed29e6 1467 if (initial && p->cf->gr_mode)
d15b0b0a 1468 c->feed_state = BFS_LOADING;
9aed29e6
OZ
1469
1470 /* It is refeed and both sides support enhanced route refresh */
d15b0b0a
OZ
1471 if (!initial && p->enhanced_refresh)
1472 {
1473 /* BoRR must not be sent before End-of-RIB */
1474 if (c->feed_state == BFS_LOADING || c->feed_state == BFS_LOADED)
1475 return;
9aed29e6 1476
d15b0b0a
OZ
1477 c->feed_state = BFS_REFRESHING;
1478 bgp_schedule_packet(p->conn, c, PKT_BEGIN_REFRESH);
1479 }
9aed29e6
OZ
1480}
1481
1482static void
d15b0b0a 1483bgp_feed_end(struct channel *C)
9aed29e6 1484{
d15b0b0a
OZ
1485 struct bgp_proto *p = (void *) C->proto;
1486 struct bgp_channel *c = (void *) C;
9aed29e6 1487
54430df9
OZ
1488 /* Ignore non-BGP channels */
1489 if (C->channel != &channel_bgp)
1490 return;
1491
9aed29e6
OZ
1492 /* This should not happen */
1493 if (!p->conn)
1494 return;
1495
1496 /* Non-demarcated feed ended, nothing to do */
d15b0b0a 1497 if (c->feed_state == BFS_NONE)
9aed29e6
OZ
1498 return;
1499
1500 /* Schedule End-of-RIB packet */
d15b0b0a
OZ
1501 if (c->feed_state == BFS_LOADING)
1502 c->feed_state = BFS_LOADED;
9aed29e6
OZ
1503
1504 /* Schedule EoRR packet */
d15b0b0a
OZ
1505 if (c->feed_state == BFS_REFRESHING)
1506 c->feed_state = BFS_REFRESHED;
9aed29e6
OZ
1507
1508 /* Kick TX hook */
d15b0b0a 1509 bgp_schedule_packet(p->conn, c, PKT_UPDATE);
0c791f87
OZ
1510}
1511
9aed29e6 1512
48e842cc
MM
1513static void
1514bgp_start_locked(struct object_lock *lock)
1515{
1516 struct bgp_proto *p = lock->data;
a22c3e59 1517 const struct bgp_config *cf = p->cf;
48e842cc 1518
11b32d91 1519 if (p->p.proto_state != PS_START)
d15b0b0a
OZ
1520 {
1521 DBG("BGP: Got lock in different state %d\n", p->p.proto_state);
1522 return;
1523 }
11b32d91 1524
48e842cc 1525 DBG("BGP: Got lock\n");
4847a894 1526
e0835db4 1527 if (cf->multihop || bgp_is_dynamic(p))
d15b0b0a
OZ
1528 {
1529 /* Multi-hop sessions do not use neighbor entries */
1530 bgp_initiate(p);
1531 return;
1532 }
4847a894 1533
a22c3e59 1534 neighbor *n = neigh_find(&p->p, p->remote_ip, cf->iface, NEF_STICKY);
523f020b 1535 if (!n)
d15b0b0a 1536 {
a22c3e59 1537 log(L_ERR "%s: Invalid remote address %I%J", p->p.name, p->remote_ip, cf->iface);
d15b0b0a
OZ
1538 /* As we do not start yet, we can just disable protocol */
1539 p->p.disabled = 1;
1540 bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_NEXT_HOP);
1541 proto_notify_state(&p->p, PS_DOWN);
1542 return;
1543 }
523f020b
OZ
1544
1545 p->neigh = n;
1546
1547 if (n->scope <= 0)
a22c3e59 1548 BGP_TRACE(D_EVENTS, "Waiting for %I%J to become my neighbor", p->remote_ip, cf->iface);
523f020b
OZ
1549 else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1550 BGP_TRACE(D_EVENTS, "Waiting for link on %s", n->iface->name);
1551 else
1552 bgp_start_neighbor(p);
c01e3741
MM
1553}
1554
2638249d
MM
1555static int
1556bgp_start(struct proto *P)
1557{
c01e3741 1558 struct bgp_proto *p = (struct bgp_proto *) P;
a22c3e59
OZ
1559 const struct bgp_config *cf = p->cf;
1560
1561 p->local_ip = cf->local_ip;
a22c3e59
OZ
1562 p->local_as = cf->local_as;
1563 p->remote_as = cf->remote_as;
1564 p->public_as = cf->local_as;
1565
e0835db4
OZ
1566 /* For dynamic BGP childs, remote_ip is already set */
1567 if (ipa_nonzero(cf->remote_ip))
1568 p->remote_ip = cf->remote_ip;
1569
a22c3e59
OZ
1570 /* Confederation ID is used for truly external peers */
1571 if (p->cf->confederation && !p->is_interior)
1572 p->public_as = cf->confederation;
c01e3741 1573
e0835db4
OZ
1574 p->passive = cf->passive || bgp_is_dynamic(p);
1575
11b32d91 1576 p->start_state = BSS_PREPARE;
b552ecc4
MM
1577 p->outgoing_conn.state = BS_IDLE;
1578 p->incoming_conn.state = BS_IDLE;
bcbdcbb6 1579 p->neigh = NULL;
1ec52253 1580 p->bfd_req = NULL;
e0835db4 1581 p->postponed_sk = NULL;
0c791f87 1582 p->gr_ready = 0;
d15b0b0a 1583 p->gr_active_num = 0;
cfe34a31 1584
21d09632
OZ
1585 /* Reset some stats */
1586 p->stats.rx_messages = p->stats.tx_messages = 0;
1587 p->stats.rx_updates = p->stats.tx_updates = 0;
1588 p->stats.rx_bytes = p->stats.tx_bytes = 0;
1589 p->last_rx_update = 0;
1590
961671c0 1591 p->event = ev_new_init(p->p.pool, bgp_decision, p);
a6f79ca5
OZ
1592 p->startup_timer = tm_new_init(p->p.pool, bgp_startup_timeout, p, 0, 0);
1593 p->gr_timer = tm_new_init(p->p.pool, bgp_graceful_restart_timeout, p, 0, 0);
0c791f87 1594
4ef09506
OZ
1595 p->local_id = proto_get_router_id(P->cf);
1596 if (p->rr_client)
1597 p->rr_cluster_id = p->cf->rr_cluster_id ? p->cf->rr_cluster_id : p->local_id;
1598
9be9a264 1599 p->remote_id = 0;
ef57b70f 1600 p->link_addr = IPA_NONE;
9be9a264 1601
9d456d53
OZ
1602 proto_setup_mpls_map(P, RTS_BGP, 1);
1603
7fc55925 1604 /* Lock all channels when in GR recovery mode */
6eda3f13 1605 if (p->p.gr_recovery && p->cf->gr_mode)
d15b0b0a
OZ
1606 {
1607 struct bgp_channel *c;
54430df9 1608 BGP_WALK_CHANNELS(p, c)
d15b0b0a
OZ
1609 channel_graceful_restart_lock(&c->c);
1610 }
0c791f87 1611
c01e3741 1612 /*
d15b0b0a
OZ
1613 * Before attempting to create the connection, we need to lock the port,
1614 * so that we are the only instance attempting to talk with that neighbor.
c01e3741 1615 */
a22c3e59 1616 struct object_lock *lock;
c01e3741 1617 lock = p->lock = olock_new(P->pool);
a22c3e59 1618 lock->addr = p->remote_ip;
dcde7ae5 1619 lock->port = p->cf->remote_port;
53ffbff3 1620 lock->iface = p->cf->iface;
9f4908fe 1621 lock->vrf = p->cf->iface ? NULL : p->p.vrf;
c01e3741 1622 lock->type = OBJLOCK_TCP;
c01e3741
MM
1623 lock->hook = bgp_start_locked;
1624 lock->data = p;
eb1e43a9
OZ
1625
1626 /* For dynamic BGP, we use inst 1 to avoid collisions with regular BGP */
1627 if (bgp_is_dynamic(p))
1628 {
1629 lock->addr = net_prefix(p->cf->remote_range);
1630 lock->inst = 1;
1631 }
1632
c01e3741 1633 olock_acquire(lock);
d51aa281 1634
c01e3741 1635 return PS_START;
2638249d
MM
1636}
1637
d9b77cc2
OZ
1638extern int proto_restart;
1639
2638249d
MM
1640static int
1641bgp_shutdown(struct proto *P)
1642{
c01e3741 1643 struct bgp_proto *p = (struct bgp_proto *) P;
8a68316e 1644 int subcode = 0;
c01e3741 1645
cd1d9961
OZ
1646 char *message = NULL;
1647 byte *data = NULL;
1648 uint len = 0;
c01e3741 1649
85368cd4 1650 BGP_TRACE(D_EVENTS, "Shutdown requested");
b99d3786 1651
ebecb6f6 1652 switch (P->down_code)
d15b0b0a
OZ
1653 {
1654 case PDC_CF_REMOVE:
1655 case PDC_CF_DISABLE:
1656 subcode = 3; // Errcode 6, 3 - peer de-configured
1657 break;
1658
1659 case PDC_CF_RESTART:
1660 subcode = 6; // Errcode 6, 6 - other configuration change
1661 break;
1662
1663 case PDC_CMD_DISABLE:
1664 case PDC_CMD_SHUTDOWN:
8a68316e 1665 shutdown:
d15b0b0a 1666 subcode = 2; // Errcode 6, 2 - administrative shutdown
830ba75e 1667 message = P->message;
d15b0b0a
OZ
1668 break;
1669
1670 case PDC_CMD_RESTART:
1671 subcode = 4; // Errcode 6, 4 - administrative reset
830ba75e 1672 message = P->message;
d15b0b0a
OZ
1673 break;
1674
8a68316e
OZ
1675 case PDC_CMD_GR_DOWN:
1676 if ((p->cf->gr_mode != BGP_GR_ABLE) &&
1677 (p->cf->llgr_mode != BGP_LLGR_ABLE))
1678 goto shutdown;
1679
1680 subcode = -1; // Do not send NOTIFICATION, just close the connection
1681 break;
1682
d15b0b0a
OZ
1683 case PDC_RX_LIMIT_HIT:
1684 case PDC_IN_LIMIT_HIT:
1685 subcode = 1; // Errcode 6, 1 - max number of prefixes reached
1686 /* log message for compatibility */
1687 log(L_WARN "%s: Route limit exceeded, shutting down", p->p.name);
1688 goto limit;
1689
1690 case PDC_OUT_LIMIT_HIT:
1691 subcode = proto_restart ? 4 : 2; // Administrative reset or shutdown
1692
1693 limit:
1694 bgp_store_error(p, NULL, BE_AUTO_DOWN, BEA_ROUTE_LIMIT_EXCEEDED);
1695 if (proto_restart)
1696 bgp_update_startup_delay(p);
1697 else
1698 p->startup_delay = 0;
1699 goto done;
1700 }
b99d3786 1701
ebecb6f6 1702 bgp_store_error(p, NULL, BE_MAN_DOWN, 0);
11b32d91 1703 p->startup_delay = 0;
c01e3741 1704
cd1d9961
OZ
1705 /* RFC 8203 - shutdown communication */
1706 if (message)
1707 {
1708 uint msg_len = strlen(message);
7ff34ca2 1709 msg_len = MIN(msg_len, 255);
cd1d9961
OZ
1710
1711 /* Buffer will be freed automatically by protocol shutdown */
1712 data = mb_alloc(p->p.pool, msg_len + 1);
1713 len = msg_len + 1;
1714
1715 data[0] = msg_len;
1716 memcpy(data+1, message, msg_len);
1717 }
1718
d15b0b0a 1719done:
cd1d9961 1720 bgp_stop(p, subcode, data, len);
11b32d91 1721 return p->p.proto_state;
2638249d
MM
1722}
1723
48e842cc 1724static struct proto *
d15b0b0a 1725bgp_init(struct proto_config *CF)
48e842cc 1726{
d15b0b0a 1727 struct proto *P = proto_new(CF);
48e842cc 1728 struct bgp_proto *p = (struct bgp_proto *) P;
d15b0b0a 1729 struct bgp_config *cf = (struct bgp_config *) CF;
48e842cc
MM
1730
1731 P->rt_notify = bgp_rt_notify;
14375237 1732 P->preexport = bgp_preexport;
48e842cc 1733 P->neigh_notify = bgp_neigh_notify;
bf47fe4b 1734 P->reload_routes = bgp_reload_routes;
9aed29e6
OZ
1735 P->feed_begin = bgp_feed_begin;
1736 P->feed_end = bgp_feed_end;
094d2bdb 1737 P->rte_better = bgp_rte_better;
8d9eef17 1738 P->rte_mergable = bgp_rte_mergable;
d15b0b0a 1739 P->rte_recalculate = cf->deterministic_med ? bgp_rte_recalculate : NULL;
5bd73431 1740 P->rte_modify = bgp_rte_modify_stale;
d471d5fc 1741 P->rte_igp_metric = bgp_rte_igp_metric;
d15b0b0a
OZ
1742
1743 p->cf = cf;
d15b0b0a
OZ
1744 p->is_internal = (cf->local_as == cf->remote_as);
1745 p->is_interior = p->is_internal || cf->confederation_member;
1746 p->rs_client = cf->rs_client;
1747 p->rr_client = cf->rr_client;
1748
e0835db4
OZ
1749 p->ipv4 = ipa_nonzero(cf->remote_ip) ?
1750 ipa_is_ip4(cf->remote_ip) :
1751 (cf->remote_range && (cf->remote_range->type == NET_IP4));
1752
1753 p->remote_ip = cf->remote_ip;
1754 p->remote_as = cf->remote_as;
1755
1756 /* Hack: We use cf->remote_ip just to pass remote_ip from bgp_spawn() */
1757 if (cf->c.parent)
1758 cf->remote_ip = IPA_NONE;
1759
9d456d53 1760 /* Add all BGP channels */
d15b0b0a 1761 struct bgp_channel_config *cc;
54430df9 1762 BGP_CF_WALK_CHANNELS(cf, cc)
d15b0b0a 1763 proto_add_channel(P, &cc->c);
9be9a264 1764
9d456d53
OZ
1765 /* Add MPLS channel */
1766 proto_configure_channel(P, &P->mpls_channel, proto_cf_mpls_channel(CF));
1767
48e842cc
MM
1768 return P;
1769}
1770
d15b0b0a
OZ
1771static void
1772bgp_channel_init(struct channel *C, struct channel_config *CF)
1773{
1774 struct bgp_channel *c = (void *) C;
1775 struct bgp_channel_config *cf = (void *) CF;
1776
d15b0b0a
OZ
1777 c->cf = cf;
1778 c->afi = cf->afi;
ef57b70f
OZ
1779 c->desc = cf->desc;
1780
1781 if (cf->igp_table_ip4)
1782 c->igp_table_ip4 = cf->igp_table_ip4->table;
1783
1784 if (cf->igp_table_ip6)
1785 c->igp_table_ip6 = cf->igp_table_ip6->table;
1f2eb2ac
OZ
1786
1787 if (cf->base_table)
1788 c->base_table = cf->base_table->table;
d15b0b0a
OZ
1789}
1790
1791static int
1792bgp_channel_start(struct channel *C)
1793{
1794 struct bgp_proto *p = (void *) C->proto;
1795 struct bgp_channel *c = (void *) C;
a22c3e59 1796 ip_addr src = p->local_ip;
d15b0b0a 1797
ef57b70f
OZ
1798 if (c->igp_table_ip4)
1799 rt_lock_table(c->igp_table_ip4);
1800
1801 if (c->igp_table_ip6)
1802 rt_lock_table(c->igp_table_ip6);
d15b0b0a 1803
1f2eb2ac
OZ
1804 if (c->base_table)
1805 {
1806 rt_lock_table(c->base_table);
1807 rt_flowspec_link(c->base_table, c->c.table);
1808 }
1809
d15b0b0a
OZ
1810 c->pool = p->p.pool; // XXXX
1811 bgp_init_bucket_table(c);
1812 bgp_init_prefix_table(c);
1813
682d3f7d
OZ
1814 if (c->cf->import_table)
1815 channel_setup_in_table(C);
1816
b7d7599c
OZ
1817 if (c->cf->export_table)
1818 channel_setup_out_table(C);
1819
5bd73431
OZ
1820 c->stale_timer = tm_new_init(c->pool, bgp_long_lived_stale_timeout, c, 0, 0);
1821
d15b0b0a
OZ
1822 c->next_hop_addr = c->cf->next_hop_addr;
1823 c->link_addr = IPA_NONE;
1824 c->packets_to_send = 0;
1825
1826 /* Try to use source address as next hop address */
1827 if (ipa_zero(c->next_hop_addr))
1828 {
ef57b70f 1829 if (bgp_channel_is_ipv4(c) && (ipa_is_ip4(src) || c->ext_next_hop))
d15b0b0a
OZ
1830 c->next_hop_addr = src;
1831
ef57b70f 1832 if (bgp_channel_is_ipv6(c) && (ipa_is_ip6(src) || c->ext_next_hop))
d15b0b0a
OZ
1833 c->next_hop_addr = src;
1834 }
1835
ccee67ca
OZ
1836 /* Use preferred addresses associated with interface / source address */
1837 if (ipa_zero(c->next_hop_addr))
1838 {
1839 /* We know the iface for single-hop, we make lookup for multihop */
586c1800 1840 struct neighbor *nbr = p->neigh ?: neigh_find(&p->p, src, NULL, 0);
ccee67ca
OZ
1841 struct iface *iface = nbr ? nbr->iface : NULL;
1842
1843 if (bgp_channel_is_ipv4(c) && iface && iface->addr4)
1844 c->next_hop_addr = iface->addr4->ip;
1845
1846 if (bgp_channel_is_ipv6(c) && iface && iface->addr6)
1847 c->next_hop_addr = iface->addr6->ip;
1848 }
1849
ef57b70f
OZ
1850 /* Exit if no feasible next hop address is found */
1851 if (ipa_zero(c->next_hop_addr))
1852 {
1853 log(L_WARN "%s: Missing next hop address", p->p.name);
1854 return 0;
1855 }
1856
d15b0b0a 1857 /* Set link-local address for IPv6 single-hop BGP */
ef57b70f 1858 if (ipa_is_ip6(c->next_hop_addr) && p->neigh)
d15b0b0a
OZ
1859 {
1860 c->link_addr = p->link_addr;
1861
1862 if (ipa_zero(c->link_addr))
1863 log(L_WARN "%s: Missing link-local address", p->p.name);
1864 }
1865
ef57b70f
OZ
1866 /* Link local address is already in c->link_addr */
1867 if (ipa_is_link_local(c->next_hop_addr))
1868 c->next_hop_addr = IPA_NONE;
d15b0b0a
OZ
1869
1870 return 0; /* XXXX: Currently undefined */
1871}
1872
1873static void
1874bgp_channel_shutdown(struct channel *C)
1875{
1876 struct bgp_channel *c = (void *) C;
1877
d15b0b0a
OZ
1878 c->next_hop_addr = IPA_NONE;
1879 c->link_addr = IPA_NONE;
7fc55925 1880 c->packets_to_send = 0;
d15b0b0a
OZ
1881}
1882
1883static void
1884bgp_channel_cleanup(struct channel *C)
1885{
1886 struct bgp_channel *c = (void *) C;
1887
ef57b70f
OZ
1888 if (c->igp_table_ip4)
1889 rt_unlock_table(c->igp_table_ip4);
1890
1891 if (c->igp_table_ip6)
1892 rt_unlock_table(c->igp_table_ip6);
b8a3608a 1893
1f2eb2ac
OZ
1894 if (c->base_table)
1895 {
1896 rt_flowspec_unlink(c->base_table, c->c.table);
1897 rt_unlock_table(c->base_table);
1898 }
1899
b8a3608a
OZ
1900 c->index = 0;
1901
1902 /* Cleanup rest of bgp_channel starting at pool field */
1903 memset(&(c->pool), 0, sizeof(struct bgp_channel) - OFFSETOF(struct bgp_channel, pool));
ef57b70f
OZ
1904}
1905
1906static inline struct bgp_channel_config *
1907bgp_find_channel_config(struct bgp_config *cf, u32 afi)
1908{
1909 struct bgp_channel_config *cc;
1910
54430df9 1911 BGP_CF_WALK_CHANNELS(cf, cc)
ef57b70f
OZ
1912 if (cc->afi == afi)
1913 return cc;
1914
1915 return NULL;
d15b0b0a 1916}
a7f23f58 1917
ef57b70f
OZ
1918struct rtable_config *
1919bgp_default_igp_table(struct bgp_config *cf, struct bgp_channel_config *cc, u32 type)
1920{
1921 struct bgp_channel_config *cc2;
1922 struct rtable_config *tab;
1923
1924 /* First, try table connected by the channel */
1925 if (cc->c.table->addr_type == type)
1926 return cc->c.table;
1927
1928 /* Find paired channel with the same SAFI but the other AFI */
1929 u32 afi2 = cc->afi ^ 0x30000;
1930 cc2 = bgp_find_channel_config(cf, afi2);
1931
1932 /* Second, try IGP table configured in the paired channel */
1933 if (cc2 && (tab = (type == NET_IP4) ? cc2->igp_table_ip4 : cc2->igp_table_ip6))
1934 return tab;
1935
1936 /* Third, try table connected by the paired channel */
1937 if (cc2 && (cc2->c.table->addr_type == type))
1938 return cc2->c.table;
1939
1940 /* Last, try default table of given type */
1941 if (tab = cf->c.global->def_tables[type])
1942 return tab;
1943
1944 cf_error("Undefined IGP table");
1945}
1946
1f2eb2ac
OZ
1947static struct rtable_config *
1948bgp_default_base_table(struct bgp_config *cf, struct bgp_channel_config *cc)
1949{
1950 /* Expected table type */
1951 u32 type = (cc->afi == BGP_AF_FLOW4) ? NET_IP4 : NET_IP6;
1952
1953 /* First, try appropriate IP channel */
1954 u32 afi2 = BGP_AF(BGP_AFI(cc->afi), BGP_SAFI_UNICAST);
1955 struct bgp_channel_config *cc2 = bgp_find_channel_config(cf, afi2);
1956 if (cc2 && (cc2->c.table->addr_type == type))
1957 return cc2->c.table;
1958
1959 /* Last, try default table of given type */
1960 struct rtable_config *tab = cf->c.global->def_tables[type];
1961 if (tab)
1962 return tab;
1963
1964 cf_error("Undefined base table");
1965}
ef57b70f 1966
a7f23f58 1967void
d15b0b0a 1968bgp_postconfig(struct proto_config *CF)
a7f23f58 1969{
d15b0b0a 1970 struct bgp_config *cf = (void *) CF;
a7f23f58
OZ
1971
1972 /* Do not check templates at all */
d15b0b0a 1973 if (cf->c.class == SYM_TEMPLATE)
a7f23f58
OZ
1974 return;
1975
f3e59178 1976
23ee6b1c
OZ
1977 /* Handle undefined remote_as, zero should mean unspecified external */
1978 if (!cf->remote_as && (cf->peer_type == BGP_PT_INTERNAL))
1979 cf->remote_as = cf->local_as;
1980
1981 int internal = (cf->local_as == cf->remote_as);
1982 int interior = internal || cf->confederation_member;
1983
f3e59178 1984 /* EBGP direct by default, IBGP multihop by default */
d15b0b0a
OZ
1985 if (cf->multihop < 0)
1986 cf->multihop = internal ? 64 : 0;
f3e59178 1987
5bd73431
OZ
1988 /* LLGR mode default based on GR mode */
1989 if (cf->llgr_mode < 0)
1990 cf->llgr_mode = cf->gr_mode ? BGP_LLGR_AWARE : 0;
1991
dea98864
OZ
1992 /* Link check for single-hop BGP by default */
1993 if (cf->check_link < 0)
1994 cf->check_link = !cf->multihop;
1995
f3e59178 1996
d15b0b0a 1997 if (!cf->local_as)
a7f23f58
OZ
1998 cf_error("Local AS number must be set");
1999
e0835db4 2000 if (ipa_zero(cf->remote_ip) && !cf->remote_range)
a7f23f58
OZ
2001 cf_error("Neighbor must be configured");
2002
e0835db4
OZ
2003 if (ipa_zero(cf->local_ip) && cf->strict_bind)
2004 cf_error("Local address must be configured for strict bind");
2005
23ee6b1c
OZ
2006 if (!cf->remote_as && !cf->peer_type)
2007 cf_error("Remote AS number (or peer type) must be set");
2008
2009 if ((cf->peer_type == BGP_PT_INTERNAL) && !internal)
2010 cf_error("IBGP cannot have different ASNs");
2011
2012 if ((cf->peer_type == BGP_PT_EXTERNAL) && internal)
2013 cf_error("EBGP cannot have the same ASNs");
a1beb8f3 2014
470740f9
OZ
2015 if (!cf->iface && (ipa_is_link_local(cf->local_ip) ||
2016 ipa_is_link_local(cf->remote_ip)))
2017 cf_error("Link-local addresses require defined interface");
a1beb8f3 2018
d15b0b0a 2019 if (!(cf->capabilities && cf->enable_as4) && (cf->remote_as > 0xFFFF))
a7f23f58
OZ
2020 cf_error("Neighbor AS number out of range (AS4 not available)");
2021
d15b0b0a 2022 if (!internal && cf->rr_client)
a7f23f58
OZ
2023 cf_error("Only internal neighbor can be RR client");
2024
d15b0b0a 2025 if (internal && cf->rs_client)
a7f23f58
OZ
2026 cf_error("Only external neighbor can be RS client");
2027
c73b5d2d
EB
2028 if (internal && (cf->local_role != BGP_ROLE_UNDEFINED))
2029 cf_error("Local role cannot be set on IBGP sessions");
2030
971721c9
OZ
2031 if (interior && (cf->local_role != BGP_ROLE_UNDEFINED))
2032 log(L_WARN "BGP roles are not recommended to be used within AS confederations");
2033
3fb06fea
OZ
2034 if (cf->require_enhanced_refresh && !(cf->enable_refresh && cf->enable_enhanced_refresh))
2035 cf_warn("Enhanced refresh required but disabled");
2036
2037 if (cf->require_as4 && !cf->enable_as4)
2038 cf_warn("AS4 support required but disabled");
2039
2040 if (cf->require_extended_messages && !cf->enable_extended_messages)
2041 cf_warn("Extended messages required but not enabled");
2042
2043 if (cf->require_gr && !cf->gr_mode)
2044 cf_warn("Graceful restart required but not enabled");
2045
2046 if (cf->require_llgr && !cf->llgr_mode)
2047 cf_warn("Long-lived graceful restart required but not enabled");
2048
c73b5d2d
EB
2049 if (cf->require_roles && (cf->local_role == BGP_ROLE_UNDEFINED))
2050 cf_error("Local role must be set if roles are required");
2051
d15b0b0a
OZ
2052 if (!cf->confederation && cf->confederation_member)
2053 cf_error("Confederation ID must be set for member sessions");
a7f23f58 2054
d15b0b0a
OZ
2055 if (cf->multihop && (ipa_is_link_local(cf->local_ip) ||
2056 ipa_is_link_local(cf->remote_ip)))
53ffbff3
OZ
2057 cf_error("Multihop BGP cannot be used with link-local addresses");
2058
e919601a 2059 if (cf->multihop && cf->iface)
33b6c292
OZ
2060 cf_error("Multihop BGP cannot be bound to interface");
2061
d15b0b0a 2062 if (cf->multihop && cf->check_link)
523f020b
OZ
2063 cf_error("Multihop BGP cannot depend on link state");
2064
d15b0b0a
OZ
2065 if (cf->multihop && cf->bfd && ipa_zero(cf->local_ip))
2066 cf_error("Multihop BGP with BFD requires specified local address");
2067
5bd73431
OZ
2068 if (!cf->gr_mode && cf->llgr_mode)
2069 cf_error("Long-lived graceful restart requires basic graceful restart");
2070
0b228fca
OZ
2071 if (internal && cf->enforce_first_as)
2072 cf_error("Enforce first AS check is requires EBGP sessions");
2073
3859e4ef
OZ
2074 if (cf->keepalive_time > cf->hold_time)
2075 cf_error("Keepalive time must be at most hold time");
2076
2077 if (cf->keepalive_time > (cf->hold_time / 2))
2078 log(L_WARN "Keepalive time should be at most 1/2 of hold time");
2079
2080 if (cf->min_hold_time > cf->hold_time)
2081 cf_error("Min hold time (%u) exceeds hold time (%u)",
2082 cf->min_hold_time, cf->hold_time);
2083
2084 uint keepalive_time = cf->keepalive_time ?: cf->hold_time / 3;
2085 if (cf->min_keepalive_time > keepalive_time)
2086 cf_error("Min keepalive time (%u) exceeds keepalive time (%u)",
2087 cf->min_keepalive_time, keepalive_time);
2088
d15b0b0a
OZ
2089
2090 struct bgp_channel_config *cc;
54430df9 2091 BGP_CF_WALK_CHANNELS(cf, cc)
d15b0b0a 2092 {
3831b619
OZ
2093 /* Handle undefined import filter */
2094 if (cc->c.in_filter == FILTER_UNDEF)
2095 if (interior)
2096 cc->c.in_filter = FILTER_ACCEPT;
2097 else
2098 cf_error("EBGP requires explicit import policy");
2099
2100 /* Handle undefined export filter */
2101 if (cc->c.out_filter == FILTER_UNDEF)
2102 if (interior)
2103 cc->c.out_filter = FILTER_REJECT;
2104 else
2105 cf_error("EBGP requires explicit export policy");
2106
d15b0b0a
OZ
2107 /* Disable after error incompatible with restart limit action */
2108 if ((cc->c.in_limit.action == PLA_RESTART) && cf->disable_after_error)
2109 cc->c.in_limit.action = PLA_DISABLE;
2110
1cab2b4a
OZ
2111 /* Different default based on rr_client, rs_client */
2112 if (cc->next_hop_keep == 0xff)
2113 cc->next_hop_keep = cf->rr_client ? NH_IBGP : (cf->rs_client ? NH_ALL : NH_NO);
2114
d15b0b0a
OZ
2115 /* Different default for gw_mode */
2116 if (!cc->gw_mode)
2117 cc->gw_mode = cf->multihop ? GW_RECURSIVE : GW_DIRECT;
1ec52253 2118
8f79e6b9
OZ
2119 /* Different default for next_hop_prefer */
2120 if (!cc->next_hop_prefer)
2121 cc->next_hop_prefer = (cc->gw_mode == GW_DIRECT) ? NHP_GLOBAL : NHP_LOCAL;
2122
5bd73431 2123 /* Defaults based on proto config */
d15b0b0a
OZ
2124 if (cc->gr_able == 0xff)
2125 cc->gr_able = (cf->gr_mode == BGP_GR_ABLE);
26822d8f 2126
5bd73431
OZ
2127 if (cc->llgr_able == 0xff)
2128 cc->llgr_able = (cf->llgr_mode == BGP_LLGR_ABLE);
2129
2130 if (cc->llgr_time == ~0U)
2131 cc->llgr_time = cf->llgr_time;
2132
09ee846d
OZ
2133 /* AIGP enabled by default on interior sessions */
2134 if (cc->aigp == 0xff)
2135 cc->aigp = interior;
2136
6fe11c99 2137 /* Default values of IGP tables */
ef57b70f
OZ
2138 if ((cc->gw_mode == GW_RECURSIVE) && !cc->desc->no_igp)
2139 {
2140 if (!cc->igp_table_ip4 && (bgp_cc_is_ipv4(cc) || cc->ext_next_hop))
2141 cc->igp_table_ip4 = bgp_default_igp_table(cf, cc, NET_IP4);
2142
2143 if (!cc->igp_table_ip6 && (bgp_cc_is_ipv6(cc) || cc->ext_next_hop))
2144 cc->igp_table_ip6 = bgp_default_igp_table(cf, cc, NET_IP6);
6fe11c99
OZ
2145
2146 if (cc->igp_table_ip4 && bgp_cc_is_ipv6(cc) && !cc->ext_next_hop)
2147 cf_error("Mismatched IGP table type");
2148
2149 if (cc->igp_table_ip6 && bgp_cc_is_ipv4(cc) && !cc->ext_next_hop)
2150 cf_error("Mismatched IGP table type");
ef57b70f
OZ
2151 }
2152
1f2eb2ac
OZ
2153 /* Default value of base table */
2154 if ((BGP_SAFI(cc->afi) == BGP_SAFI_FLOW) && cc->validate && !cc->base_table)
2155 cc->base_table = bgp_default_base_table(cf, cc);
2156
2157 if (cc->base_table && !cc->base_table->trie_used)
2158 cf_error("Flowspec validation requires base table (%s) with trie",
2159 cc->base_table->name);
2160
d15b0b0a
OZ
2161 if (cf->multihop && (cc->gw_mode == GW_DIRECT))
2162 cf_error("Multihop BGP cannot use direct gateway mode");
26822d8f 2163
d15b0b0a
OZ
2164 if ((cc->gw_mode == GW_RECURSIVE) && cc->c.table->sorted)
2165 cf_error("BGP in recursive mode prohibits sorted table");
2166
2167 if (cf->deterministic_med && cc->c.table->sorted)
2168 cf_error("BGP with deterministic MED prohibits sorted table");
2169
2170 if (cc->secondary && !cc->c.table->sorted)
2171 cf_error("BGP with secondary option requires sorted table");
3fb06fea
OZ
2172
2173 if (cc->require_ext_next_hop && !cc->ext_next_hop)
2174 cf_warn("Extended next hop required but not enabled");
2175
2176 if (cc->require_add_path && !cc->add_path)
2177 cf_warn("ADD-PATH required but not enabled");
d15b0b0a 2178 }
a7f23f58
OZ
2179}
2180
2181static int
d15b0b0a 2182bgp_reconfigure(struct proto *P, struct proto_config *CF)
a7f23f58 2183{
d15b0b0a 2184 struct bgp_proto *p = (void *) P;
a22c3e59
OZ
2185 const struct bgp_config *new = (void *) CF;
2186 const struct bgp_config *old = p->cf;
a7f23f58 2187
d15b0b0a 2188 if (proto_get_router_id(CF) != p->local_id)
79b4e12e
OZ
2189 return 0;
2190
a7f23f58
OZ
2191 int same = !memcmp(((byte *) old) + sizeof(struct proto_config),
2192 ((byte *) new) + sizeof(struct proto_config),
2193 // password item is last and must be checked separately
2194 OFFSETOF(struct bgp_config, password) - sizeof(struct proto_config))
15b0a922 2195 && !bstrcmp(old->password, new->password)
d35fb9d7
OZ
2196 && ((!old->remote_range && !new->remote_range)
2197 || (old->remote_range && new->remote_range && net_equal(old->remote_range, new->remote_range)))
15b0a922 2198 && !bstrcmp(old->dynamic_name, new->dynamic_name)
e0835db4 2199 && (old->dynamic_name_digits == new->dynamic_name_digits);
d15b0b0a
OZ
2200
2201 /* FIXME: Move channel reconfiguration to generic protocol code ? */
2202 struct channel *C, *C2;
2203 struct bgp_channel_config *cc;
2204
2205 WALK_LIST(C, p->p.channels)
2206 C->stale = 1;
2207
9d456d53 2208 /* Reconfigure BGP channels */
54430df9 2209 BGP_CF_WALK_CHANNELS(new, cc)
d15b0b0a
OZ
2210 {
2211 C = (struct channel *) bgp_find_channel(p, cc->afi);
2212 same = proto_configure_channel(P, &C, &cc->c) && same;
d15b0b0a
OZ
2213 }
2214
9d456d53
OZ
2215 /* Reconfigure MPLS channel */
2216 same = proto_configure_channel(P, &P->mpls_channel, proto_cf_mpls_channel(CF)) && same;
2217
d15b0b0a
OZ
2218 WALK_LIST_DELSAFE(C, C2, p->p.channels)
2219 if (C->stale)
2220 same = proto_configure_channel(P, &C, NULL) && same;
2221
3fb06fea
OZ
2222 /* Reset name counter */
2223 p->dynamic_name_counter = 0;
9d456d53 2224
3fb06fea
OZ
2225 if (!same)
2226 return 0;
1ec52253 2227
a7f23f58 2228 /* We should update our copy of configuration ptr as old configuration will be freed */
3fb06fea 2229 p->cf = new;
a7f23f58 2230
3fb06fea
OZ
2231 /* Check whether existing connections are compatible with required capabilities */
2232 struct bgp_conn *ci = &p->incoming_conn;
2233 if (((ci->state == BS_OPENCONFIRM) || (ci->state == BS_ESTABLISHED)) && !bgp_check_capabilities(ci))
2234 return 0;
2235
2236 struct bgp_conn *co = &p->outgoing_conn;
2237 if (((co->state == BS_OPENCONFIRM) || (co->state == BS_ESTABLISHED)) && !bgp_check_capabilities(co))
2238 return 0;
2239
2240 proto_setup_mpls_map(P, RTS_BGP, 1);
2241
2242 if (p->start_state > BSS_PREPARE)
2243 bgp_update_bfd(p, new->bfd);
e0835db4 2244
3fb06fea 2245 return 1;
a7f23f58
OZ
2246}
2247
13c6cf8a 2248#define TABLE(cf, NAME) ((cf)->NAME ? (cf)->NAME->table : NULL)
ffb38dfb 2249
d15b0b0a 2250static int
e2b530aa 2251bgp_channel_reconfigure(struct channel *C, struct channel_config *CC, int *import_changed, int *export_changed)
d15b0b0a 2252{
6c9cda6f 2253 struct bgp_proto *p = (void *) C->proto;
d15b0b0a
OZ
2254 struct bgp_channel *c = (void *) C;
2255 struct bgp_channel_config *new = (void *) CC;
2256 struct bgp_channel_config *old = c->cf;
2257
e2b530aa 2258 if ((new->secondary != old->secondary) ||
1f2eb2ac 2259 (new->validate != old->validate) ||
e2b530aa
OZ
2260 (new->gr_able != old->gr_able) ||
2261 (new->llgr_able != old->llgr_able) ||
2262 (new->llgr_time != old->llgr_time) ||
2263 (new->ext_next_hop != old->ext_next_hop) ||
2264 (new->add_path != old->add_path) ||
2265 (new->import_table != old->import_table) ||
b7d7599c 2266 (new->export_table != old->export_table) ||
1f2eb2ac
OZ
2267 (TABLE(new, igp_table_ip4) != TABLE(old, igp_table_ip4)) ||
2268 (TABLE(new, igp_table_ip6) != TABLE(old, igp_table_ip6)) ||
2269 (TABLE(new, base_table) != TABLE(old, base_table)))
d15b0b0a
OZ
2270 return 0;
2271
e2b530aa 2272 if (new->mandatory && !old->mandatory && (C->channel_state != CS_UP))
d15b0b0a
OZ
2273 return 0;
2274
09ee846d 2275 if ((new->gw_mode != old->gw_mode) ||
8f79e6b9 2276 (new->next_hop_prefer != old->next_hop_prefer) ||
09ee846d
OZ
2277 (new->aigp != old->aigp) ||
2278 (new->cost != old->cost))
6c9cda6f
OZ
2279 {
2280 /* import_changed itself does not force ROUTE_REFRESH when import_table is active */
2281 if (c->c.in_table && (c->c.channel_state == CS_UP))
2282 bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH);
2283
e2b530aa 2284 *import_changed = 1;
6c9cda6f 2285 }
e2b530aa
OZ
2286
2287 if (!ipa_equal(new->next_hop_addr, old->next_hop_addr) ||
2288 (new->next_hop_self != old->next_hop_self) ||
2289 (new->next_hop_keep != old->next_hop_keep) ||
09ee846d
OZ
2290 (new->aigp != old->aigp) ||
2291 (new->aigp_originate != old->aigp_originate))
e2b530aa
OZ
2292 *export_changed = 1;
2293
d15b0b0a
OZ
2294 c->cf = new;
2295 return 1;
2296}
2297
a7f23f58 2298static void
9d3fc306 2299bgp_copy_config(struct proto_config *dest, struct proto_config *src)
a7f23f58 2300{
9d3fc306
OZ
2301 struct bgp_config *d = (void *) dest;
2302 struct bgp_config *s = (void *) src;
2303
2304 /* Copy BFD options */
2305 if (s->bfd)
2306 {
2307 struct bfd_options *opts = cfg_alloc(sizeof(struct bfd_options));
2308 memcpy(opts, s->bfd, sizeof(struct bfd_options));
2309 d->bfd = opts;
2310 }
a7f23f58
OZ
2311}
2312
2313
54e55169
MM
2314/**
2315 * bgp_error - report a protocol error
2316 * @c: connection
2317 * @code: error code (according to the RFC)
2e9b2421 2318 * @subcode: error sub-code
54e55169
MM
2319 * @data: data to be passed in the Notification message
2320 * @len: length of the data
2321 *
2322 * bgp_error() sends a notification packet to tell the other side that a protocol
2e9b2421 2323 * error has occurred (including the data considered erroneous if possible) and
54e55169
MM
2324 * closes the connection.
2325 */
3fdbafb6 2326void
d15b0b0a 2327bgp_error(struct bgp_conn *c, uint code, uint subcode, byte *data, int len)
3fdbafb6 2328{
b99d3786
OZ
2329 struct bgp_proto *p = c->bgp;
2330
11b32d91 2331 if (c->state == BS_CLOSE)
3fdbafb6 2332 return;
11b32d91 2333
d15b0b0a 2334 bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, data, ABS(len));
b99d3786 2335 bgp_store_error(p, c, BE_BGP_TX, (code << 16) | subcode);
11b32d91 2336
3fdbafb6
MM
2337 c->notify_code = code;
2338 c->notify_subcode = subcode;
efcece2d
MM
2339 c->notify_data = data;
2340 c->notify_size = (len > 0) ? len : 0;
4558adab
OZ
2341
2342 bgp_conn_enter_close_state(c);
d15b0b0a 2343 bgp_schedule_packet(c, NULL, PKT_NOTIFICATION);
b99d3786
OZ
2344
2345 if (code != 6)
d15b0b0a
OZ
2346 {
2347 bgp_update_startup_delay(p);
830ba75e 2348 bgp_stop(p, 0, NULL, 0);
d15b0b0a 2349 }
3fdbafb6
MM
2350}
2351
11b32d91
OZ
2352/**
2353 * bgp_store_error - store last error for status report
2354 * @p: BGP instance
2355 * @c: connection
2356 * @class: error class (BE_xxx constants)
2357 * @code: error code (class specific)
2358 *
2359 * bgp_store_error() decides whether given error is interesting enough
2360 * and store that error to last_error variables of @p
2361 */
2362void
2363bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code)
2364{
2365 /* During PS_UP, we ignore errors on secondary connection */
2366 if ((p->p.proto_state == PS_UP) && c && (c != p->conn))
2367 return;
2368
2369 /* During PS_STOP, we ignore any errors, as we want to report
2370 * the error that caused transition to PS_STOP
2371 */
2372 if (p->p.proto_state == PS_STOP)
2373 return;
2374
2375 p->last_error_class = class;
2376 p->last_error_code = code;
2377}
2378
11b32d91 2379static char *bgp_state_names[] = { "Idle", "Connect", "Active", "OpenSent", "OpenConfirm", "Established", "Close" };
13c6cf8a
OZ
2380static char *bgp_err_classes[] = { "", "Error: ", "Socket: ", "Received: ", "BGP Error: ", "Automatic shutdown: ", "" };
2381static char *bgp_misc_errors[] = { "", "Neighbor lost", "Invalid next hop", "Kernel MD5 auth failed", "No listening socket", "Link down", "BFD session down", "Graceful restart" };
2382static char *bgp_auto_errors[] = { "", "Route limit exceeded" };
2383static char *bgp_gr_states[] = { "None", "Regular", "Long-lived" };
11b32d91 2384
b8113a5e
OZ
2385static const char *
2386bgp_last_errmsg(struct bgp_proto *p)
973399ae 2387{
11b32d91 2388 switch (p->last_error_class)
d15b0b0a
OZ
2389 {
2390 case BE_MISC:
2391 return bgp_misc_errors[p->last_error_code];
2392 case BE_SOCKET:
2393 return (p->last_error_code == 0) ? "Connection closed" : strerror(p->last_error_code);
2394 case BE_BGP_RX:
2395 case BE_BGP_TX:
2396 return bgp_error_dsc(p->last_error_code >> 16, p->last_error_code & 0xFF);
2397 case BE_AUTO_DOWN:
2398 return bgp_auto_errors[p->last_error_code];
2399 default:
2400 return "";
2401 }
b8113a5e
OZ
2402}
2403
2404static const char *
2405bgp_state_dsc(struct bgp_proto *p)
2406{
51947659
OZ
2407 if (p->p.proto_state == PS_DOWN)
2408 return "Down";
b8113a5e
OZ
2409
2410 int state = MAX(p->incoming_conn.state, p->outgoing_conn.state);
e0835db4 2411 if ((state == BS_IDLE) && (p->start_state >= BSS_CONNECT) && p->passive)
b8113a5e
OZ
2412 return "Passive";
2413
2414 return bgp_state_names[state];
2415}
2416
2417static void
2418bgp_get_status(struct proto *P, byte *buf)
2419{
2420 struct bgp_proto *p = (struct bgp_proto *) P;
2421
2422 const char *err1 = bgp_err_classes[p->last_error_class];
2423 const char *err2 = bgp_last_errmsg(p);
11b32d91 2424
f4ab2317 2425 if (P->proto_state == PS_DOWN)
11b32d91 2426 bsprintf(buf, "%s%s", err1, err2);
f4ab2317 2427 else
b8113a5e
OZ
2428 bsprintf(buf, "%-14s%s%s", bgp_state_dsc(p), err1, err2);
2429}
2430
256cc8ee
OZ
2431static void
2432bgp_show_afis(int code, char *s, u32 *afis, uint count)
2433{
2434 buffer b;
2435 LOG_BUFFER_INIT(b);
2436
2437 buffer_puts(&b, s);
2438
2439 for (u32 *af = afis; af < (afis + count); af++)
2440 {
2441 const struct bgp_af_desc *desc = bgp_get_af_desc(*af);
2442 if (desc)
2443 buffer_print(&b, " %s", desc->name);
2444 else
2445 buffer_print(&b, " <%u/%u>", BGP_AFI(*af), BGP_SAFI(*af));
2446 }
2447
2448 if (b.pos == b.end)
2449 strcpy(b.end - 32, " ... <too long>");
2450
2451 cli_msg(code, b.start);
2452}
2453
af611f93 2454const char *
c73b5d2d
EB
2455bgp_format_role_name(u8 role)
2456{
2457 static const char *bgp_role_names[] = { "provider", "rs_server", "rs_client", "customer", "peer" };
2458 if (role == BGP_ROLE_UNDEFINED) return "undefined";
971721c9 2459 if (role < ARRAY_SIZE(bgp_role_names)) return bgp_role_names[role];
c73b5d2d
EB
2460 return "?";
2461}
2462
256cc8ee
OZ
2463static void
2464bgp_show_capabilities(struct bgp_proto *p UNUSED, struct bgp_caps *caps)
2465{
2466 struct bgp_af_caps *ac;
2467 uint any_mp_bgp = 0;
2468 uint any_gr_able = 0;
2469 uint any_add_path = 0;
d8022d26 2470 uint any_ext_next_hop = 0;
5bd73431 2471 uint any_llgr_able = 0;
256cc8ee
OZ
2472 u32 *afl1 = alloca(caps->af_count * sizeof(u32));
2473 u32 *afl2 = alloca(caps->af_count * sizeof(u32));
2474 uint afn1, afn2;
2475
2476 WALK_AF_CAPS(caps, ac)
2477 {
2478 any_mp_bgp |= ac->ready;
2479 any_gr_able |= ac->gr_able;
2480 any_add_path |= ac->add_path;
d8022d26 2481 any_ext_next_hop |= ac->ext_next_hop;
5bd73431 2482 any_llgr_able |= ac->llgr_able;
256cc8ee
OZ
2483 }
2484
2485 if (any_mp_bgp)
2486 {
2487 cli_msg(-1006, " Multiprotocol");
2488
2489 afn1 = 0;
2490 WALK_AF_CAPS(caps, ac)
2491 if (ac->ready)
2492 afl1[afn1++] = ac->afi;
2493
2494 bgp_show_afis(-1006, " AF announced:", afl1, afn1);
2495 }
2496
2497 if (caps->route_refresh)
2498 cli_msg(-1006, " Route refresh");
2499
d8022d26
OZ
2500 if (any_ext_next_hop)
2501 {
2502 cli_msg(-1006, " Extended next hop");
2503
2504 afn1 = 0;
2505 WALK_AF_CAPS(caps, ac)
2506 if (ac->ext_next_hop)
2507 afl1[afn1++] = ac->afi;
2508
2509 bgp_show_afis(-1006, " IPv6 nexthop:", afl1, afn1);
2510 }
2511
256cc8ee
OZ
2512 if (caps->ext_messages)
2513 cli_msg(-1006, " Extended message");
2514
2515 if (caps->gr_aware)
2516 cli_msg(-1006, " Graceful restart");
2517
2518 if (any_gr_able)
2519 {
2520 /* Continues from gr_aware */
2521 cli_msg(-1006, " Restart time: %u", caps->gr_time);
2522 if (caps->gr_flags & BGP_GRF_RESTART)
2523 cli_msg(-1006, " Restart recovery");
2524
2525 afn1 = afn2 = 0;
2526 WALK_AF_CAPS(caps, ac)
2527 {
2528 if (ac->gr_able)
2529 afl1[afn1++] = ac->afi;
2530
2531 if (ac->gr_af_flags & BGP_GRF_FORWARDING)
2532 afl2[afn2++] = ac->afi;
2533 }
2534
2535 bgp_show_afis(-1006, " AF supported:", afl1, afn1);
2536 bgp_show_afis(-1006, " AF preserved:", afl2, afn2);
2537 }
2538
2539 if (caps->as4_support)
2540 cli_msg(-1006, " 4-octet AS numbers");
2541
2542 if (any_add_path)
2543 {
2544 cli_msg(-1006, " ADD-PATH");
2545
2546 afn1 = afn2 = 0;
2547 WALK_AF_CAPS(caps, ac)
2548 {
2549 if (ac->add_path & BGP_ADD_PATH_RX)
2550 afl1[afn1++] = ac->afi;
2551
2552 if (ac->add_path & BGP_ADD_PATH_TX)
2553 afl2[afn2++] = ac->afi;
2554 }
2555
2556 bgp_show_afis(-1006, " RX:", afl1, afn1);
2557 bgp_show_afis(-1006, " TX:", afl2, afn2);
2558 }
2559
2560 if (caps->enhanced_refresh)
2561 cli_msg(-1006, " Enhanced refresh");
5bd73431
OZ
2562
2563 if (caps->llgr_aware)
2564 cli_msg(-1006, " Long-lived graceful restart");
2565
2566 if (any_llgr_able)
2567 {
2568 u32 stale_time = 0;
2569
2570 afn1 = afn2 = 0;
2571 WALK_AF_CAPS(caps, ac)
2572 {
2573 stale_time = MAX(stale_time, ac->llgr_time);
2574
2575 if (ac->llgr_able && ac->llgr_time)
2576 afl1[afn1++] = ac->afi;
2577
2578 if (ac->llgr_flags & BGP_GRF_FORWARDING)
2579 afl2[afn2++] = ac->afi;
2580 }
2581
2582 /* Continues from llgr_aware */
2583 cli_msg(-1006, " LL stale time: %u", stale_time);
2584
2585 bgp_show_afis(-1006, " AF supported:", afl1, afn1);
2586 bgp_show_afis(-1006, " AF preserved:", afl2, afn2);
2587 }
71423871
VB
2588
2589 if (caps->hostname)
2590 cli_msg(-1006, " Hostname: %s", caps->hostname);
c73b5d2d
EB
2591
2592 if (caps->role != BGP_ROLE_UNDEFINED)
2593 cli_msg(-1006, " Role: %s", bgp_format_role_name(caps->role));
256cc8ee
OZ
2594}
2595
b8113a5e
OZ
2596static void
2597bgp_show_proto_info(struct proto *P)
2598{
2599 struct bgp_proto *p = (struct bgp_proto *) P;
b8113a5e 2600
b8113a5e 2601 cli_msg(-1006, " BGP state: %s", bgp_state_dsc(p));
e0835db4
OZ
2602
2603 if (bgp_is_dynamic(p) && p->cf->remote_range)
2604 cli_msg(-1006, " Neighbor range: %N", p->cf->remote_range);
2605 else
2606 cli_msg(-1006, " Neighbor address: %I%J", p->remote_ip, p->cf->iface);
2607
a9c19b92
OZ
2608 if ((p->conn == &p->outgoing_conn) && (p->cf->remote_port != BGP_PORT))
2609 cli_msg(-1006, " Neighbor port: %u", p->cf->remote_port);
2610
e0835db4 2611 cli_msg(-1006, " Neighbor AS: %u", p->remote_as);
0b1e1e1a 2612 cli_msg(-1006, " Local AS: %u", p->cf->local_as);
b8113a5e 2613
d15b0b0a 2614 if (p->gr_active_num)
0c791f87
OZ
2615 cli_msg(-1006, " Neighbor graceful restart active");
2616
b8113a5e 2617 if (P->proto_state == PS_START)
d15b0b0a
OZ
2618 {
2619 struct bgp_conn *oc = &p->outgoing_conn;
b8113a5e 2620
d15b0b0a 2621 if ((p->start_state < BSS_CONNECT) &&
a6f79ca5 2622 (tm_active(p->startup_timer)))
d3fa9e84 2623 cli_msg(-1006, " Error wait: %t/%u",
a6f79ca5 2624 tm_remains(p->startup_timer), p->startup_delay);
b8113a5e 2625
d15b0b0a 2626 if ((oc->state == BS_ACTIVE) &&
a6f79ca5 2627 (tm_active(oc->connect_timer)))
d3fa9e84 2628 cli_msg(-1006, " Connect delay: %t/%u",
a6f79ca5 2629 tm_remains(oc->connect_timer), p->cf->connect_delay_time);
0c791f87 2630
a6f79ca5 2631 if (p->gr_active_num && tm_active(p->gr_timer))
d3fa9e84 2632 cli_msg(-1006, " Restart timer: %t/-",
a6f79ca5 2633 tm_remains(p->gr_timer));
d15b0b0a 2634 }
b8113a5e 2635 else if (P->proto_state == PS_UP)
d15b0b0a
OZ
2636 {
2637 cli_msg(-1006, " Neighbor ID: %R", p->remote_id);
256cc8ee
OZ
2638 cli_msg(-1006, " Local capabilities");
2639 bgp_show_capabilities(p, p->conn->local_caps);
2640 cli_msg(-1006, " Neighbor capabilities");
2641 bgp_show_capabilities(p, p->conn->remote_caps);
7fc55925
OZ
2642 cli_msg(-1006, " Session: %s%s%s%s%s",
2643 p->is_internal ? "internal" : "external",
2644 p->cf->multihop ? " multihop" : "",
2645 p->rr_client ? " route-reflector" : "",
2646 p->rs_client ? " route-server" : "",
2647 p->as4_session ? " AS4" : "");
a22c3e59 2648 cli_msg(-1006, " Source address: %I", p->local_ip);
d3fa9e84 2649 cli_msg(-1006, " Hold timer: %t/%u",
a6f79ca5 2650 tm_remains(p->conn->hold_timer), p->conn->hold_time);
d3fa9e84 2651 cli_msg(-1006, " Keepalive timer: %t/%u",
a6f79ca5 2652 tm_remains(p->conn->keepalive_timer), p->conn->keepalive_time);
bcf23274
KK
2653 cli_msg(-1006, " Send hold timer: %t/%u",
2654 tm_remains(p->conn->send_hold_timer), p->conn->send_hold_time);
2655}
b8113a5e 2656
5a6e8380 2657#if 0
21d09632
OZ
2658 struct bgp_stats *s = &p->stats;
2659 cli_msg(-1006, " FSM established transitions: %u",
2660 s->fsm_established_transitions);
2661 cli_msg(-1006, " Rcvd messages: %u total / %u updates / %lu bytes",
2662 s->rx_messages, s->rx_updates, s->rx_bytes);
2663 cli_msg(-1006, " Sent messages: %u total / %u updates / %lu bytes",
2664 s->tx_messages, s->tx_updates, s->tx_bytes);
2665 cli_msg(-1006, " Last rcvd update elapsed time: %t s",
2666 p->last_rx_update ? (current_time() - p->last_rx_update) : 0);
5a6e8380 2667#endif
21d09632 2668
523f020b 2669 if ((p->last_error_class != BE_NONE) &&
b8113a5e 2670 (p->last_error_class != BE_MAN_DOWN))
d15b0b0a
OZ
2671 {
2672 const char *err1 = bgp_err_classes[p->last_error_class];
2673 const char *err2 = bgp_last_errmsg(p);
2674 cli_msg(-1006, " Last error: %s%s", err1, err2);
2675 }
2676
2677 {
ef57b70f 2678 struct bgp_channel *c;
d15b0b0a 2679 WALK_LIST(c, p->p.channels)
ef57b70f
OZ
2680 {
2681 channel_show_info(&c->c);
2682
54430df9
OZ
2683 if (c->c.channel != &channel_bgp)
2684 continue;
2685
5bd73431
OZ
2686 if (p->gr_active_num)
2687 cli_msg(-1006, " Neighbor GR: %s", bgp_gr_states[c->gr_active]);
2688
0db7a1d6 2689 if (c->stale_timer && tm_active(c->stale_timer))
5bd73431
OZ
2690 cli_msg(-1006, " LL stale timer: %t/-", tm_remains(c->stale_timer));
2691
7fc55925
OZ
2692 if (c->c.channel_state == CS_UP)
2693 {
2694 if (ipa_zero(c->link_addr))
2695 cli_msg(-1006, " BGP Next hop: %I", c->next_hop_addr);
2696 else
2697 cli_msg(-1006, " BGP Next hop: %I %I", c->next_hop_addr, c->link_addr);
2698 }
ccee67ca 2699
ef57b70f
OZ
2700 if (c->igp_table_ip4)
2701 cli_msg(-1006, " IGP IPv4 table: %s", c->igp_table_ip4->name);
2702
2703 if (c->igp_table_ip6)
2704 cli_msg(-1006, " IGP IPv6 table: %s", c->igp_table_ip6->name);
1f2eb2ac
OZ
2705
2706 if (c->base_table)
2707 cli_msg(-1006, " Base table: %s", c->base_table->name);
ef57b70f 2708 }
d15b0b0a 2709 }
973399ae
MM
2710}
2711
f4deef89 2712const struct channel_class channel_bgp = {
d15b0b0a
OZ
2713 .channel_size = sizeof(struct bgp_channel),
2714 .config_size = sizeof(struct bgp_channel_config),
2715 .init = bgp_channel_init,
2716 .start = bgp_channel_start,
2717 .shutdown = bgp_channel_shutdown,
2718 .cleanup = bgp_channel_cleanup,
2719 .reconfigure = bgp_channel_reconfigure,
2720};
2721
2638249d 2722struct protocol proto_bgp = {
4a591d4b
PT
2723 .name = "BGP",
2724 .template = "bgp%d",
ee7e2ffd 2725 .class = PROTOCOL_BGP,
4a591d4b 2726 .preference = DEF_PREF_BGP,
9d456d53 2727 .channel_mask = NB_IP | NB_VPN | NB_FLOW | NB_MPLS,
d15b0b0a 2728 .proto_size = sizeof(struct bgp_proto),
2bbc3083 2729 .config_size = sizeof(struct bgp_config),
d15b0b0a 2730 .postconfig = bgp_postconfig,
4a591d4b
PT
2731 .init = bgp_init,
2732 .start = bgp_start,
2733 .shutdown = bgp_shutdown,
4a591d4b
PT
2734 .reconfigure = bgp_reconfigure,
2735 .copy_config = bgp_copy_config,
2736 .get_status = bgp_get_status,
2737 .get_attr = bgp_get_attr,
2738 .get_route_info = bgp_get_route_info,
2739 .show_proto_info = bgp_show_proto_info
2638249d 2740};
4a23ede2
MM
2741
2742void bgp_build(void)
2743{
2744 proto_build(&proto_bgp);
2745}