Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bgp / bgp.c @ 153f02da

History | View | Annotate | Download (56.1 KB)

1
/*
2
 *        BIRD -- The Border Gateway Protocol
3
 *
4
 *        (c) 2000 Martin Mares <mj@ucw.cz>
5
 *        (c) 2008--2016 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2008--2016 CZ.NIC z.s.p.o.
7
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10

    
11
/**
12
 * DOC: Border Gateway Protocol
13
 *
14
 * The BGP protocol is implemented in three parts: |bgp.c| which takes care of
15
 * the connection and most of the interface with BIRD core, |packets.c| handling
16
 * both incoming and outgoing BGP packets and |attrs.c| containing functions for
17
 * manipulation with BGP attribute lists.
18
 *
19
 * As opposed to the other existing routing daemons, BIRD has a sophisticated
20
 * core architecture which is able to keep all the information needed by BGP in
21
 * the primary routing table, therefore no complex data structures like a
22
 * central BGP table are needed. This increases memory footprint of a BGP router
23
 * with many connections, but not too much and, which is more important, it
24
 * makes BGP much easier to implement.
25
 *
26
 * Each instance of BGP (corresponding to a single BGP peer) is described by a
27
 * &bgp_proto structure to which are attached individual connections represented
28
 * by &bgp_connection (usually, there exists only one connection, but during BGP
29
 * session setup, there can be more of them). The connections are handled
30
 * according to the BGP state machine defined in the RFC with all the timers and
31
 * all the parameters configurable.
32
 *
33
 * In incoming direction, we listen on the connection's socket and each time we
34
 * receive some input, we pass it to bgp_rx(). It decodes packet headers and the
35
 * markers and passes complete packets to bgp_rx_packet() which distributes the
36
 * packet according to its type.
37
 *
38
 * In outgoing direction, we gather all the routing updates and sort them to
39
 * buckets (&bgp_bucket) according to their attributes (we keep a hash table for
40
 * fast comparison of &rta's and a &fib which helps us to find if we already
41
 * have another route for the same destination queued for sending, so that we
42
 * can replace it with the new one immediately instead of sending both
43
 * updates). There also exists a special bucket holding all the route
44
 * withdrawals which cannot be queued anywhere else as they don't have any
45
 * attributes. If we have any packet to send (due to either new routes or the
46
 * connection tracking code wanting to send a Open, Keepalive or Notification
47
 * message), we call bgp_schedule_packet() which sets the corresponding bit in a
48
 * @packet_to_send bit field in &bgp_conn and as soon as the transmit socket
49
 * buffer becomes empty, we call bgp_fire_tx(). It inspects state of all the
50
 * packet type bits and calls the corresponding bgp_create_xx() functions,
51
 * eventually rescheduling the same packet type if we have more data of the same
52
 * type to send.
53
 *
54
 * The processing of attributes consists of two functions: bgp_decode_attrs()
55
 * for checking of the attribute blocks and translating them to the language of
56
 * BIRD's extended attributes and bgp_encode_attrs() which does the
57
 * converse. Both functions are built around a @bgp_attr_table array describing
58
 * all important characteristics of all known attributes.  Unknown transitive
59
 * attributes are attached to the route as %EAF_TYPE_OPAQUE byte streams.
60
 *
61
 * BGP protocol implements graceful restart in both restarting (local restart)
62
 * and receiving (neighbor restart) roles. The first is handled mostly by the
63
 * graceful restart code in the nest, BGP protocol just handles capabilities,
64
 * sets @gr_wait and locks graceful restart until end-of-RIB mark is received.
65
 * The second is implemented by internal restart of the BGP state to %BS_IDLE
66
 * and protocol state to %PS_START, but keeping the protocol up from the core
67
 * point of view and therefore maintaining received routes. Routing table
68
 * refresh cycle (rt_refresh_begin(), rt_refresh_end()) is used for removing
69
 * stale routes after reestablishment of BGP session during graceful restart.
70
 *
71
 * Supported standards:
72
 * <itemize>
73
 * <item> <rfc id="4271"> - Border Gateway Protocol 4 (BGP)
74
 * <item> <rfc id="1997"> - BGP Communities Attribute
75
 * <item> <rfc id="2385"> - Protection of BGP Sessions via TCP MD5 Signature
76
 * <item> <rfc id="2545"> - Use of BGP Multiprotocol Extensions for IPv6
77
 * <item> <rfc id="2918"> - Route Refresh Capability
78
 * <item> <rfc id="3107"> - Carrying Label Information in BGP
79
 * <item> <rfc id="4360"> - BGP Extended Communities Attribute
80
 * <item> <rfc id="4364"> - BGP/MPLS IPv4 Virtual Private Networks
81
 * <item> <rfc id="4456"> - BGP Route Reflection
82
 * <item> <rfc id="4486"> - Subcodes for BGP Cease Notification Message
83
 * <item> <rfc id="4659"> - BGP/MPLS IPv6 Virtual Private Networks
84
 * <item> <rfc id="4724"> - Graceful Restart Mechanism for BGP
85
 * <item> <rfc id="4760"> - Multiprotocol extensions for BGP
86
 * <item> <rfc id="4798"> - Connecting IPv6 Islands over IPv4 MPLS
87
 * <item> <rfc id="5065"> - AS confederations for BGP
88
 * <item> <rfc id="5082"> - Generalized TTL Security Mechanism
89
 * <item> <rfc id="5492"> - Capabilities Advertisement with BGP
90
 * <item> <rfc id="5549"> - Advertising IPv4 NLRI with an IPv6 Next Hop
91
 * <item> <rfc id="5575"> - Dissemination of Flow Specification Rules
92
 * <item> <rfc id="5668"> - 4-Octet AS Specific BGP Extended Community
93
 * <item> <rfc id="6286"> - AS-Wide Unique BGP Identifier
94
 * <item> <rfc id="6608"> - Subcodes for BGP Finite State Machine Error
95
 * <item> <rfc id="6793"> - BGP Support for 4-Octet AS Numbers
96
 * <item> <rfc id="7313"> - Enhanced Route Refresh Capability for BGP
97
 * <item> <rfc id="7606"> - Revised Error Handling for BGP UPDATE Messages
98
 * <item> <rfc id="7911"> - Advertisement of Multiple Paths in BGP
99
 * <item> <rfc id="7947"> - Internet Exchange BGP Route Server
100
 * <item> <rfc id="8092"> - BGP Large Communities Attribute
101
 * </itemize>
102
*/
103

    
104
#undef LOCAL_DEBUG
105

    
106
#include "nest/bird.h"
107
#include "nest/iface.h"
108
#include "nest/protocol.h"
109
#include "nest/route.h"
110
#include "nest/cli.h"
111
#include "nest/locks.h"
112
#include "conf/conf.h"
113
#include "lib/socket.h"
114
#include "lib/resource.h"
115
#include "lib/string.h"
116

    
117
#include "bgp.h"
118

    
119

    
120
struct linpool *bgp_linpool;                /* Global temporary pool */
121
struct linpool *bgp_linpool2;                /* Global temporary pool for bgp_rt_notify() */
122
static list bgp_sockets;                /* Global list of listening sockets */
123

    
124

    
125
static void bgp_connect(struct bgp_proto *p);
126
static void bgp_active(struct bgp_proto *p);
127
static void bgp_update_bfd(struct bgp_proto *p, int use_bfd);
128

    
129
static int bgp_incoming_connection(sock *sk, uint dummy UNUSED);
130
static void bgp_listen_sock_err(sock *sk UNUSED, int err);
131

    
132
/**
133
 * bgp_open - open a BGP instance
134
 * @p: BGP instance
135
 *
136
 * This function allocates and configures shared BGP resources, mainly listening
137
 * sockets. Should be called as the last step during initialization (when lock
138
 * is acquired and neighbor is ready). When error, caller should change state to
139
 * PS_DOWN and return immediately.
140
 */
141
static int
142
bgp_open(struct bgp_proto *p)
143
{
144
  struct bgp_socket *bs = NULL;
145
  struct iface *ifa = p->cf->strict_bind ? p->cf->iface : NULL;
146
  ip_addr addr = p->cf->strict_bind ? p->cf->local_ip :
147
    (ipa_is_ip4(p->cf->remote_ip) ? IPA_NONE4 : IPA_NONE6);
148
  uint port = p->cf->local_port;
149

    
150
  /* FIXME: Add some global init? */
151
  if (!bgp_linpool)
152
    init_list(&bgp_sockets);
153

    
154
  /* We assume that cf->iface is defined iff cf->local_ip is link-local */
155

    
156
  WALK_LIST(bs, bgp_sockets)
157
    if (ipa_equal(bs->sk->saddr, addr) && (bs->sk->iface == ifa) && (bs->sk->sport == port))
158
    {
159
      bs->uc++;
160
      p->sock = bs;
161
      return 0;
162
    }
163

    
164
  sock *sk = sk_new(proto_pool);
165
  sk->type = SK_TCP_PASSIVE;
166
  sk->ttl = 255;
167
  sk->saddr = addr;
168
  sk->sport = port;
169
  sk->flags = 0;
170
  sk->tos = IP_PREC_INTERNET_CONTROL;
171
  sk->rbsize = BGP_RX_BUFFER_SIZE;
172
  sk->tbsize = BGP_TX_BUFFER_SIZE;
173
  sk->rx_hook = bgp_incoming_connection;
174
  sk->err_hook = bgp_listen_sock_err;
175

    
176
  if (sk_open(sk) < 0)
177
    goto err;
178

    
179
  bs = mb_allocz(proto_pool, sizeof(struct bgp_socket));
180
  bs->sk = sk;
181
  bs->uc = 1;
182
  p->sock = bs;
183

    
184
  add_tail(&bgp_sockets, &bs->n);
185

    
186
  if (!bgp_linpool)
187
  {
188
    bgp_linpool  = lp_new_default(proto_pool);
189
    bgp_linpool2 = lp_new_default(proto_pool);
190
  }
191

    
192
  return 0;
193

    
194
err:
195
  sk_log_error(sk, p->p.name);
196
  log(L_ERR "%s: Cannot open listening socket", p->p.name);
197
  rfree(sk);
198
  return -1;
199
}
200

    
201
/**
202
 * bgp_close - close a BGP instance
203
 * @p: BGP instance
204
 *
205
 * This function frees and deconfigures shared BGP resources.
206
 */
207
static void
208
bgp_close(struct bgp_proto *p)
209
{
210
  struct bgp_socket *bs = p->sock;
211

    
212
  ASSERT(bs && bs->uc);
213

    
214
  if (--bs->uc)
215
    return;
216

    
217
  rfree(bs->sk);
218
  rem_node(&bs->n);
219
  mb_free(bs);
220

    
221
  if (!EMPTY_LIST(bgp_sockets))
222
    return;
223

    
224
  rfree(bgp_linpool);
225
  bgp_linpool = NULL;
226

    
227
  rfree(bgp_linpool2);
228
  bgp_linpool2 = NULL;
229
}
230

    
231
static inline int
232
bgp_setup_auth(struct bgp_proto *p, int enable)
233
{
234
  if (p->cf->password)
235
  {
236
    int rv = sk_set_md5_auth(p->sock->sk,
237
                             p->cf->local_ip, p->cf->remote_ip, p->cf->iface,
238
                             enable ? p->cf->password : NULL, p->cf->setkey);
239

    
240
    if (rv < 0)
241
      sk_log_error(p->sock->sk, p->p.name);
242

    
243
    return rv;
244
  }
245
  else
246
    return 0;
247
}
248

    
249
static inline struct bgp_channel *
250
bgp_find_channel(struct bgp_proto *p, u32 afi)
251
{
252
  struct bgp_channel *c;
253
  WALK_LIST(c, p->p.channels)
254
    if (c->afi == afi)
255
      return c;
256

    
257
  return NULL;
258
}
259

    
260
static void
261
bgp_startup(struct bgp_proto *p)
262
{
263
  BGP_TRACE(D_EVENTS, "Started");
264
  p->start_state = BSS_CONNECT;
265

    
266
  if (!p->cf->passive)
267
    bgp_active(p);
268
}
269

    
270
static void
271
bgp_startup_timeout(timer *t)
272
{
273
  bgp_startup(t->data);
274
}
275

    
276

    
277
static void
278
bgp_initiate(struct bgp_proto *p)
279
{
280
  int err_val;
281

    
282
  if (bgp_open(p) < 0)
283
  { err_val = BEM_NO_SOCKET; goto err1; }
284

    
285
  if (bgp_setup_auth(p, 1) < 0)
286
  { err_val = BEM_INVALID_MD5; goto err2; }
287

    
288
  if (p->cf->bfd)
289
    bgp_update_bfd(p, p->cf->bfd);
290

    
291
  if (p->startup_delay)
292
  {
293
    p->start_state = BSS_DELAY;
294
    BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds due to errors", p->startup_delay);
295
    bgp_start_timer(p->startup_timer, p->startup_delay);
296
  }
297
  else
298
    bgp_startup(p);
299

    
300
  return;
301

    
302
err2:
303
  bgp_close(p);
304
err1:
305
  p->p.disabled = 1;
306
  bgp_store_error(p, NULL, BE_MISC, err_val);
307
  proto_notify_state(&p->p, PS_DOWN);
308

    
309
  return;
310
}
311

    
312
/**
313
 * bgp_start_timer - start a BGP timer
314
 * @t: timer
315
 * @value: time to fire (0 to disable the timer)
316
 *
317
 * This functions calls tm_start() on @t with time @value and the amount of
318
 * randomization suggested by the BGP standard. Please use it for all BGP
319
 * timers.
320
 */
321
void
322
bgp_start_timer(timer *t, int value)
323
{
324
  if (value)
325
  {
326
    /* The randomization procedure is specified in RFC 1771: 9.2.3.3 */
327
    t->randomize = value / 4;
328
    tm_start(t, value - t->randomize);
329
  }
330
  else
331
    tm_stop(t);
332
}
333

    
334
/**
335
 * bgp_close_conn - close a BGP connection
336
 * @conn: connection to close
337
 *
338
 * This function takes a connection described by the &bgp_conn structure, closes
339
 * its socket and frees all resources associated with it.
340
 */
341
void
342
bgp_close_conn(struct bgp_conn *conn)
343
{
344
  // struct bgp_proto *p = conn->bgp;
345

    
346
  DBG("BGP: Closing connection\n");
347
  conn->packets_to_send = 0;
348
  conn->channels_to_send = 0;
349
  rfree(conn->connect_timer);
350
  conn->connect_timer = NULL;
351
  rfree(conn->keepalive_timer);
352
  conn->keepalive_timer = NULL;
353
  rfree(conn->hold_timer);
354
  conn->hold_timer = NULL;
355
  rfree(conn->tx_ev);
356
  conn->tx_ev = NULL;
357
  rfree(conn->sk);
358
  conn->sk = NULL;
359

    
360
  mb_free(conn->local_caps);
361
  conn->local_caps = NULL;
362
  mb_free(conn->remote_caps);
363
  conn->remote_caps = NULL;
364
}
365

    
366

    
367
/**
368
 * bgp_update_startup_delay - update a startup delay
369
 * @p: BGP instance
370
 *
371
 * This function updates a startup delay that is used to postpone next BGP
372
 * connect. It also handles disable_after_error and might stop BGP instance
373
 * when error happened and disable_after_error is on.
374
 *
375
 * It should be called when BGP protocol error happened.
376
 */
377
void
378
bgp_update_startup_delay(struct bgp_proto *p)
379
{
380
  struct bgp_config *cf = p->cf;
381

    
382
  DBG("BGP: Updating startup delay\n");
383

    
384
  if (p->last_proto_error && ((now - p->last_proto_error) >= (int) cf->error_amnesia_time))
385
    p->startup_delay = 0;
386

    
387
  p->last_proto_error = now;
388

    
389
  if (cf->disable_after_error)
390
  {
391
    p->startup_delay = 0;
392
    p->p.disabled = 1;
393
    return;
394
  }
395

    
396
  if (!p->startup_delay)
397
    p->startup_delay = cf->error_delay_time_min;
398
  else
399
    p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max);
400
}
401

    
402
static void
403
bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode)
404
{
405
  switch (conn->state)
406
  {
407
  case BS_IDLE:
408
  case BS_CLOSE:
409
    return;
410

    
411
  case BS_CONNECT:
412
  case BS_ACTIVE:
413
    bgp_conn_enter_idle_state(conn);
414
    return;
415

    
416
  case BS_OPENSENT:
417
  case BS_OPENCONFIRM:
418
  case BS_ESTABLISHED:
419
    bgp_error(conn, 6, subcode, NULL, 0);
420
    return;
421

    
422
  default:
423
    bug("bgp_graceful_close_conn: Unknown state %d", conn->state);
424
  }
425
}
426

    
427
static void
428
bgp_down(struct bgp_proto *p)
429
{
430
  if (p->start_state > BSS_PREPARE)
431
  {
432
    bgp_setup_auth(p, 0);
433
    bgp_close(p);
434
  }
435

    
436
  BGP_TRACE(D_EVENTS, "Down");
437
  proto_notify_state(&p->p, PS_DOWN);
438
}
439

    
440
static void
441
bgp_decision(void *vp)
442
{
443
  struct bgp_proto *p = vp;
444

    
445
  DBG("BGP: Decision start\n");
446
  if ((p->p.proto_state == PS_START) &&
447
      (p->outgoing_conn.state == BS_IDLE) &&
448
      (p->incoming_conn.state != BS_OPENCONFIRM) &&
449
      !p->cf->passive)
450
    bgp_active(p);
451

    
452
  if ((p->p.proto_state == PS_STOP) &&
453
      (p->outgoing_conn.state == BS_IDLE) &&
454
      (p->incoming_conn.state == BS_IDLE))
455
    bgp_down(p);
456
}
457

    
458
void
459
bgp_stop(struct bgp_proto *p, uint subcode)
460
{
461
  proto_notify_state(&p->p, PS_STOP);
462
  bgp_graceful_close_conn(&p->outgoing_conn, subcode);
463
  bgp_graceful_close_conn(&p->incoming_conn, subcode);
464
  ev_schedule(p->event);
465
}
466

    
467
static inline void
468
bgp_conn_set_state(struct bgp_conn *conn, uint new_state)
469
{
470
  if (conn->bgp->p.mrtdump & MD_STATES)
471
    mrt_dump_bgp_state_change(conn, conn->state, new_state);
472

    
473
  conn->state = new_state;
474
}
475

    
476
void
477
bgp_conn_enter_openconfirm_state(struct bgp_conn *conn)
478
{
479
  /* Really, most of the work is done in bgp_rx_open(). */
480
  bgp_conn_set_state(conn, BS_OPENCONFIRM);
481
}
482

    
483
static const struct bgp_af_caps dummy_af_caps = { };
484

    
485
void
486
bgp_conn_enter_established_state(struct bgp_conn *conn)
487
{
488
  struct bgp_proto *p = conn->bgp;
489
  struct bgp_caps *local = conn->local_caps;
490
  struct bgp_caps *peer = conn->remote_caps;
491
  struct bgp_channel *c;
492

    
493
  BGP_TRACE(D_EVENTS, "BGP session established");
494

    
495
  /* For multi-hop BGP sessions */
496
  if (ipa_zero(p->source_addr))
497
    p->source_addr = conn->sk->saddr;
498

    
499
  conn->sk->fast_rx = 0;
500

    
501
  p->conn = conn;
502
  p->last_error_class = 0;
503
  p->last_error_code = 0;
504

    
505
  p->as4_session = conn->as4_session;
506

    
507
  p->route_refresh = peer->route_refresh;
508
  p->enhanced_refresh = local->enhanced_refresh && peer->enhanced_refresh;
509

    
510
  /* Whether we may handle possible GR of peer (it has some AF GR-able) */
511
  p->gr_ready = 0;        /* Updated later */
512

    
513
  /* Whether peer is ready to handle our GR recovery */
514
  int peer_gr_ready = peer->gr_aware && !(peer->gr_flags & BGP_GRF_RESTART);
515

    
516
  if (p->gr_active_num)
517
    tm_stop(p->gr_timer);
518

    
519
  /* Number of active channels */
520
  int num = 0;
521

    
522
  WALK_LIST(c, p->p.channels)
523
  {
524
    const struct bgp_af_caps *loc = bgp_find_af_caps(local, c->afi);
525
    const struct bgp_af_caps *rem = bgp_find_af_caps(peer,  c->afi);
526

    
527
    /* Ignore AFIs that were not announced in multiprotocol capability */
528
    if (!loc || !loc->ready)
529
      loc = &dummy_af_caps;
530

    
531
    if (!rem || !rem->ready)
532
      rem = &dummy_af_caps;
533

    
534
    int active = loc->ready && rem->ready;
535
    c->c.disabled = !active;
536
    c->c.reloadable = p->route_refresh;
537

    
538
    c->index = active ? num++ : 0;
539

    
540
    c->feed_state = BFS_NONE;
541
    c->load_state = BFS_NONE;
542

    
543
    /* Channels where peer may do GR */
544
    c->gr_ready = active && local->gr_aware && rem->gr_able;
545
    p->gr_ready = p->gr_ready || c->gr_ready;
546

    
547
    /* Channels not able to recover gracefully */
548
    if (p->p.gr_recovery && (!active || !peer_gr_ready))
549
      channel_graceful_restart_unlock(&c->c);
550

    
551
    /* Channels waiting for local convergence */
552
    if (p->p.gr_recovery && loc->gr_able && peer_gr_ready)
553
      c->c.gr_wait = 1;
554

    
555
    /* Channels where peer is not able to recover gracefully */
556
    if (c->gr_active && ! (c->gr_ready && (rem->gr_af_flags & BGP_GRF_FORWARDING)))
557
      bgp_graceful_restart_done(c);
558

    
559
    /* GR capability implies that neighbor will send End-of-RIB */
560
    if (peer->gr_aware)
561
      c->load_state = BFS_LOADING;
562

    
563
    c->ext_next_hop = c->cf->ext_next_hop && (bgp_channel_is_ipv6(c) || rem->ext_next_hop);
564
    c->add_path_rx = (loc->add_path & BGP_ADD_PATH_RX) && (rem->add_path & BGP_ADD_PATH_TX);
565
    c->add_path_tx = (loc->add_path & BGP_ADD_PATH_TX) && (rem->add_path & BGP_ADD_PATH_RX);
566

    
567
    /* Update RA mode */
568
    if (c->add_path_tx)
569
      c->c.ra_mode = RA_ANY;
570
    else if (c->cf->secondary)
571
      c->c.ra_mode = RA_ACCEPTED;
572
    else
573
      c->c.ra_mode = RA_OPTIMAL;
574
  }
575

    
576
  p->afi_map = mb_alloc(p->p.pool, num * sizeof(u32));
577
  p->channel_map = mb_alloc(p->p.pool, num * sizeof(void *));
578
  p->channel_count = num;
579

    
580
  WALK_LIST(c, p->p.channels)
581
  {
582
    if (c->c.disabled)
583
      continue;
584

    
585
    p->afi_map[c->index] = c->afi;
586
    p->channel_map[c->index] = c;
587
  }
588

    
589
  /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */
590

    
591
  bgp_conn_set_state(conn, BS_ESTABLISHED);
592
  proto_notify_state(&p->p, PS_UP);
593
}
594

    
595
static void
596
bgp_conn_leave_established_state(struct bgp_proto *p)
597
{
598
  BGP_TRACE(D_EVENTS, "BGP session closed");
599
  p->conn = NULL;
600

    
601
  // XXXX free these tables to avoid memory leak during graceful restart
602
  // bgp_free_prefix_table(p);
603
  // bgp_free_bucket_table(p);
604

    
605
  if (p->p.proto_state == PS_UP)
606
    bgp_stop(p, 0);
607
}
608

    
609
void
610
bgp_conn_enter_close_state(struct bgp_conn *conn)
611
{
612
  struct bgp_proto *p = conn->bgp;
613
  int os = conn->state;
614

    
615
  bgp_conn_set_state(conn, BS_CLOSE);
616
  tm_stop(conn->keepalive_timer);
617
  conn->sk->rx_hook = NULL;
618

    
619
  /* Timeout for CLOSE state, if we cannot send notification soon then we just hangup */
620
  bgp_start_timer(conn->hold_timer, 10);
621

    
622
  if (os == BS_ESTABLISHED)
623
    bgp_conn_leave_established_state(p);
624
}
625

    
626
void
627
bgp_conn_enter_idle_state(struct bgp_conn *conn)
628
{
629
  struct bgp_proto *p = conn->bgp;
630
  int os = conn->state;
631

    
632
  bgp_close_conn(conn);
633
  bgp_conn_set_state(conn, BS_IDLE);
634
  ev_schedule(p->event);
635

    
636
  if (os == BS_ESTABLISHED)
637
    bgp_conn_leave_established_state(p);
638
}
639

    
640
/**
641
 * bgp_handle_graceful_restart - handle detected BGP graceful restart
642
 * @p: BGP instance
643
 *
644
 * This function is called when a BGP graceful restart of the neighbor is
645
 * detected (when the TCP connection fails or when a new TCP connection
646
 * appears). The function activates processing of the restart - starts routing
647
 * table refresh cycle and activates BGP restart timer. The protocol state goes
648
 * back to %PS_START, but changing BGP state back to %BS_IDLE is left for the
649
 * caller.
650
 */
651
void
652
bgp_handle_graceful_restart(struct bgp_proto *p)
653
{
654
  ASSERT(p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready);
655

    
656
  BGP_TRACE(D_EVENTS, "Neighbor graceful restart detected%s",
657
            p->gr_active_num ? " - already pending" : "");
658

    
659
  p->gr_active_num = 0;
660

    
661
  struct bgp_channel *c;
662
  WALK_LIST(c, p->p.channels)
663
  {
664
    if (c->gr_ready)
665
    {
666
      if (c->gr_active)
667
        rt_refresh_end(c->c.table, &c->c);
668

    
669
      c->gr_active = 1;
670
      p->gr_active_num++;
671
      rt_refresh_begin(c->c.table, &c->c);
672
    }
673
    else
674
    {
675
      /* Just flush the routes */
676
      rt_refresh_begin(c->c.table, &c->c);
677
      rt_refresh_end(c->c.table, &c->c);
678
    }
679
  }
680

    
681
  proto_notify_state(&p->p, PS_START);
682
  bgp_start_timer(p->gr_timer, p->conn->local_caps->gr_time);
683
}
684

    
685
/**
686
 * bgp_graceful_restart_done - finish active BGP graceful restart
687
 * @c: BGP channel
688
 *
689
 * This function is called when the active BGP graceful restart of the neighbor
690
 * should be finished for channel @c - either successfully (the neighbor sends
691
 * all paths and reports end-of-RIB for given AFI/SAFI on the new session) or
692
 * unsuccessfully (the neighbor does not support BGP graceful restart on the new
693
 * session). The function ends the routing table refresh cycle.
694
 */
695
void
696
bgp_graceful_restart_done(struct bgp_channel *c)
697
{
698
  struct bgp_proto *p = (void *) c->c.proto;
699

    
700
  ASSERT(c->gr_active);
701
  c->gr_active = 0;
702
  p->gr_active_num--;
703

    
704
  if (!p->gr_active_num)
705
    BGP_TRACE(D_EVENTS, "Neighbor graceful restart done");
706

    
707
  rt_refresh_end(c->c.table, &c->c);
708
}
709

    
710
/**
711
 * bgp_graceful_restart_timeout - timeout of graceful restart 'restart timer'
712
 * @t: timer
713
 *
714
 * This function is a timeout hook for @gr_timer, implementing BGP restart time
715
 * limit for reestablisment of the BGP session after the graceful restart. When
716
 * fired, we just proceed with the usual protocol restart.
717
 */
718

    
719
static void
720
bgp_graceful_restart_timeout(timer *t)
721
{
722
  struct bgp_proto *p = t->data;
723

    
724
  BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout");
725
  bgp_stop(p, 0);
726
}
727

    
728

    
729
/**
730
 * bgp_refresh_begin - start incoming enhanced route refresh sequence
731
 * @c: BGP channel
732
 *
733
 * This function is called when an incoming enhanced route refresh sequence is
734
 * started by the neighbor, demarcated by the BoRR packet. The function updates
735
 * the load state and starts the routing table refresh cycle. Note that graceful
736
 * restart also uses routing table refresh cycle, but RFC 7313 and load states
737
 * ensure that these two sequences do not overlap.
738
 */
739
void
740
bgp_refresh_begin(struct bgp_channel *c)
741
{
742
  struct bgp_proto *p = (void *) c->c.proto;
743

    
744
  if (c->load_state == BFS_LOADING)
745
  { log(L_WARN "%s: BEGIN-OF-RR received before END-OF-RIB, ignoring", p->p.name); return; }
746

    
747
  c->load_state = BFS_REFRESHING;
748
  rt_refresh_begin(c->c.table, &c->c);
749
}
750

    
751
/**
752
 * bgp_refresh_end - finish incoming enhanced route refresh sequence
753
 * @c: BGP channel
754
 *
755
 * This function is called when an incoming enhanced route refresh sequence is
756
 * finished by the neighbor, demarcated by the EoRR packet. The function updates
757
 * the load state and ends the routing table refresh cycle. Routes not received
758
 * during the sequence are removed by the nest.
759
 */
760
void
761
bgp_refresh_end(struct bgp_channel *c)
762
{
763
  struct bgp_proto *p = (void *) c->c.proto;
764

    
765
  if (c->load_state != BFS_REFRESHING)
766
  { log(L_WARN "%s: END-OF-RR received without prior BEGIN-OF-RR, ignoring", p->p.name); return; }
767

    
768
  c->load_state = BFS_NONE;
769
  rt_refresh_end(c->c.table, &c->c);
770
}
771

    
772

    
773
static void
774
bgp_send_open(struct bgp_conn *conn)
775
{
776
  DBG("BGP: Sending open\n");
777
  conn->sk->rx_hook = bgp_rx;
778
  conn->sk->tx_hook = bgp_tx;
779
  tm_stop(conn->connect_timer);
780
  bgp_schedule_packet(conn, NULL, PKT_OPEN);
781
  bgp_conn_set_state(conn, BS_OPENSENT);
782
  bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
783
}
784

    
785
static void
786
bgp_connected(sock *sk)
787
{
788
  struct bgp_conn *conn = sk->data;
789
  struct bgp_proto *p = conn->bgp;
790

    
791
  BGP_TRACE(D_EVENTS, "Connected");
792
  bgp_send_open(conn);
793
}
794

    
795
static void
796
bgp_connect_timeout(timer *t)
797
{
798
  struct bgp_conn *conn = t->data;
799
  struct bgp_proto *p = conn->bgp;
800

    
801
  DBG("BGP: connect_timeout\n");
802
  if (p->p.proto_state == PS_START)
803
  {
804
    bgp_close_conn(conn);
805
    bgp_connect(p);
806
  }
807
  else
808
    bgp_conn_enter_idle_state(conn);
809
}
810

    
811
static void
812
bgp_sock_err(sock *sk, int err)
813
{
814
  struct bgp_conn *conn = sk->data;
815
  struct bgp_proto *p = conn->bgp;
816

    
817
  /*
818
   * This error hook may be called either asynchronously from main
819
   * loop, or synchronously from sk_send().  But sk_send() is called
820
   * only from bgp_tx() and bgp_kick_tx(), which are both called
821
   * asynchronously from main loop. Moreover, they end if err hook is
822
   * called. Therefore, we could suppose that it is always called
823
   * asynchronously.
824
   */
825

    
826
  bgp_store_error(p, conn, BE_SOCKET, err);
827

    
828
  if (err)
829
    BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
830
  else
831
    BGP_TRACE(D_EVENTS, "Connection closed");
832

    
833
  if ((conn->state == BS_ESTABLISHED) && p->gr_ready)
834
    bgp_handle_graceful_restart(p);
835

    
836
  bgp_conn_enter_idle_state(conn);
837
}
838

    
839
static void
840
bgp_hold_timeout(timer *t)
841
{
842
  struct bgp_conn *conn = t->data;
843
  struct bgp_proto *p = conn->bgp;
844

    
845
  DBG("BGP: Hold timeout\n");
846

    
847
  /* We are already closing the connection - just do hangup */
848
  if (conn->state == BS_CLOSE)
849
  {
850
    BGP_TRACE(D_EVENTS, "Connection stalled");
851
    bgp_conn_enter_idle_state(conn);
852
    return;
853
  }
854

    
855
  /* If there is something in input queue, we are probably congested
856
     and perhaps just not processed BGP packets in time. */
857

    
858
  if (sk_rx_ready(conn->sk) > 0)
859
    bgp_start_timer(conn->hold_timer, 10);
860
  else
861
    bgp_error(conn, 4, 0, NULL, 0);
862
}
863

    
864
static void
865
bgp_keepalive_timeout(timer *t)
866
{
867
  struct bgp_conn *conn = t->data;
868

    
869
  DBG("BGP: Keepalive timer\n");
870
  bgp_schedule_packet(conn, NULL, PKT_KEEPALIVE);
871

    
872
  /* Kick TX a bit faster */
873
  if (ev_active(conn->tx_ev))
874
    ev_run(conn->tx_ev);
875
}
876

    
877
static void
878
bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
879
{
880
  conn->sk = NULL;
881
  conn->bgp = p;
882

    
883
  conn->packets_to_send = 0;
884
  conn->channels_to_send = 0;
885
  conn->last_channel = 0;
886
  conn->last_channel_count = 0;
887

    
888
  conn->connect_timer        = tm_new_set(p->p.pool, bgp_connect_timeout,        conn, 0, 0);
889
  conn->hold_timer         = tm_new_set(p->p.pool, bgp_hold_timeout,        conn, 0, 0);
890
  conn->keepalive_timer        = tm_new_set(p->p.pool, bgp_keepalive_timeout,        conn, 0, 0);
891

    
892
  conn->tx_ev = ev_new(p->p.pool);
893
  conn->tx_ev->hook = bgp_kick_tx;
894
  conn->tx_ev->data = conn;
895
}
896

    
897
static void
898
bgp_setup_sk(struct bgp_conn *conn, sock *s)
899
{
900
  s->data = conn;
901
  s->err_hook = bgp_sock_err;
902
  s->fast_rx = 1;
903
  conn->sk = s;
904
}
905

    
906
static void
907
bgp_active(struct bgp_proto *p)
908
{
909
  int delay = MAX(1, p->cf->connect_delay_time);
910
  struct bgp_conn *conn = &p->outgoing_conn;
911

    
912
  BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
913
  bgp_setup_conn(p, conn);
914
  bgp_conn_set_state(conn, BS_ACTIVE);
915
  bgp_start_timer(conn->connect_timer, delay);
916
}
917

    
918
/**
919
 * bgp_connect - initiate an outgoing connection
920
 * @p: BGP instance
921
 *
922
 * The bgp_connect() function creates a new &bgp_conn and initiates
923
 * a TCP connection to the peer. The rest of connection setup is governed
924
 * by the BGP state machine as described in the standard.
925
 */
926
static void
927
bgp_connect(struct bgp_proto *p)        /* Enter Connect state and start establishing connection */
928
{
929
  struct bgp_conn *conn = &p->outgoing_conn;
930
  int hops = p->cf->multihop ? : 1;
931

    
932
  DBG("BGP: Connecting\n");
933
  sock *s = sk_new(p->p.pool);
934
  s->type = SK_TCP_ACTIVE;
935
  s->saddr = p->source_addr;
936
  s->daddr = p->cf->remote_ip;
937
  s->dport = p->cf->remote_port;
938
  s->iface = p->neigh ? p->neigh->iface : NULL;
939
  s->ttl = p->cf->ttl_security ? 255 : hops;
940
  s->rbsize = p->cf->enable_extended_messages ? BGP_RX_BUFFER_EXT_SIZE : BGP_RX_BUFFER_SIZE;
941
  s->tbsize = p->cf->enable_extended_messages ? BGP_TX_BUFFER_EXT_SIZE : BGP_TX_BUFFER_SIZE;
942
  s->tos = IP_PREC_INTERNET_CONTROL;
943
  s->password = p->cf->password;
944
  s->tx_hook = bgp_connected;
945
  BGP_TRACE(D_EVENTS, "Connecting to %I%J from local address %I%J", s->daddr, p->cf->iface,
946
            s->saddr, ipa_is_link_local(s->saddr) ? s->iface : NULL);
947
  bgp_setup_conn(p, conn);
948
  bgp_setup_sk(conn, s);
949
  bgp_conn_set_state(conn, BS_CONNECT);
950

    
951
  if (sk_open(s) < 0)
952
    goto err;
953

    
954
  /* Set minimal receive TTL if needed */
955
  if (p->cf->ttl_security)
956
    if (sk_set_min_ttl(s, 256 - hops) < 0)
957
      goto err;
958

    
959
  DBG("BGP: Waiting for connect success\n");
960
  bgp_start_timer(conn->connect_timer, p->cf->connect_retry_time);
961
  return;
962

    
963
err:
964
  sk_log_error(s, p->p.name);
965
  bgp_sock_err(s, 0);
966
  return;
967
}
968

    
969
/**
970
 * bgp_find_proto - find existing proto for incoming connection
971
 * @sk: TCP socket
972
 *
973
 */
974
static struct bgp_proto *
975
bgp_find_proto(sock *sk)
976
{
977
  struct bgp_proto *p;
978

    
979
  WALK_LIST(p, proto_list)
980
    if ((p->p.proto == &proto_bgp) &&
981
        ipa_equal(p->cf->remote_ip, sk->daddr) &&
982
        (!p->cf->iface  || (p->cf->iface == sk->iface)) &&
983
        (ipa_zero(p->cf->local_ip) || ipa_equal(p->cf->local_ip, sk->saddr)) &&
984
        (p->cf->local_port == sk->sport))
985
      return p;
986

    
987
  return NULL;
988
}
989

    
990
/**
991
 * bgp_incoming_connection - handle an incoming connection
992
 * @sk: TCP socket
993
 * @dummy: unused
994
 *
995
 * This function serves as a socket hook for accepting of new BGP
996
 * connections. It searches a BGP instance corresponding to the peer
997
 * which has connected and if such an instance exists, it creates a
998
 * &bgp_conn structure, attaches it to the instance and either sends
999
 * an Open message or (if there already is an active connection) it
1000
 * closes the new connection by sending a Notification message.
1001
 */
1002
static int
1003
bgp_incoming_connection(sock *sk, uint dummy UNUSED)
1004
{
1005
  struct bgp_proto *p;
1006
  int acc, hops;
1007

    
1008
  DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
1009
  p = bgp_find_proto(sk);
1010
  if (!p)
1011
  {
1012
    log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)",
1013
        sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, sk->dport);
1014
    rfree(sk);
1015
    return 0;
1016
  }
1017

    
1018
  /*
1019
   * BIRD should keep multiple incoming connections in OpenSent state (for
1020
   * details RFC 4271 8.2.1 par 3), but it keeps just one. Duplicate incoming
1021
   * connections are rejected istead. The exception is the case where an
1022
   * incoming connection triggers a graceful restart.
1023
   */
1024

    
1025
  acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
1026
    (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
1027

    
1028
  if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
1029
  {
1030
    bgp_store_error(p, NULL, BE_MISC, BEM_GRACEFUL_RESTART);
1031
    bgp_handle_graceful_restart(p);
1032
    bgp_conn_enter_idle_state(p->conn);
1033
    acc = 1;
1034

    
1035
    /* There might be separate incoming connection in OpenSent state */
1036
    if (p->incoming_conn.state > BS_ACTIVE)
1037
      bgp_close_conn(&p->incoming_conn);
1038
  }
1039

    
1040
  BGP_TRACE(D_EVENTS, "Incoming connection from %I%J (port %d) %s",
1041
            sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL,
1042
            sk->dport, acc ? "accepted" : "rejected");
1043

    
1044
  if (!acc)
1045
  {
1046
    rfree(sk);
1047
    return 0;
1048
  }
1049

    
1050
  hops = p->cf->multihop ? : 1;
1051

    
1052
  if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0)
1053
    goto err;
1054

    
1055
  if (p->cf->ttl_security)
1056
    if (sk_set_min_ttl(sk, 256 - hops) < 0)
1057
      goto err;
1058

    
1059
  if (p->cf->enable_extended_messages)
1060
  {
1061
    sk->rbsize = BGP_RX_BUFFER_EXT_SIZE;
1062
    sk->tbsize = BGP_TX_BUFFER_EXT_SIZE;
1063
    sk_reallocate(sk);
1064
  }
1065

    
1066
  bgp_setup_conn(p, &p->incoming_conn);
1067
  bgp_setup_sk(&p->incoming_conn, sk);
1068
  bgp_send_open(&p->incoming_conn);
1069
  return 0;
1070

    
1071
err:
1072
  sk_log_error(sk, p->p.name);
1073
  log(L_ERR "%s: Incoming connection aborted", p->p.name);
1074
  rfree(sk);
1075
  return 0;
1076
}
1077

    
1078
static void
1079
bgp_listen_sock_err(sock *sk UNUSED, int err)
1080
{
1081
  if (err == ECONNABORTED)
1082
    log(L_WARN "BGP: Incoming connection aborted");
1083
  else
1084
    log(L_ERR "BGP: Error on listening socket: %M", err);
1085
}
1086

    
1087
static void
1088
bgp_start_neighbor(struct bgp_proto *p)
1089
{
1090
  /* Called only for single-hop BGP sessions */
1091

    
1092
  if (ipa_zero(p->source_addr))
1093
    p->source_addr = p->neigh->ifa->ip;
1094

    
1095
  if (ipa_is_link_local(p->source_addr))
1096
    p->link_addr = p->source_addr;
1097
  else if (p->neigh->iface->llv6)
1098
    p->link_addr = p->neigh->iface->llv6->ip;
1099

    
1100
  bgp_initiate(p);
1101
}
1102

    
1103
static void
1104
bgp_neigh_notify(neighbor *n)
1105
{
1106
  struct bgp_proto *p = (struct bgp_proto *) n->proto;
1107
  int ps = p->p.proto_state;
1108

    
1109
  if (n != p->neigh)
1110
    return;
1111

    
1112
  if ((ps == PS_DOWN) || (ps == PS_STOP))
1113
    return;
1114

    
1115
  int prepare = (ps == PS_START) && (p->start_state == BSS_PREPARE);
1116

    
1117
  if (n->scope <= 0)
1118
  {
1119
    if (!prepare)
1120
    {
1121
      BGP_TRACE(D_EVENTS, "Neighbor lost");
1122
      bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST);
1123
      /* Perhaps also run bgp_update_startup_delay(p)? */
1124
      bgp_stop(p, 0);
1125
    }
1126
  }
1127
  else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1128
  {
1129
    if (!prepare)
1130
    {
1131
      BGP_TRACE(D_EVENTS, "Link down");
1132
      bgp_store_error(p, NULL, BE_MISC, BEM_LINK_DOWN);
1133
      if (ps == PS_UP)
1134
        bgp_update_startup_delay(p);
1135
      bgp_stop(p, 0);
1136
    }
1137
  }
1138
  else
1139
  {
1140
    if (prepare)
1141
    {
1142
      BGP_TRACE(D_EVENTS, "Neighbor ready");
1143
      bgp_start_neighbor(p);
1144
    }
1145
  }
1146
}
1147

    
1148
static void
1149
bgp_bfd_notify(struct bfd_request *req)
1150
{
1151
  struct bgp_proto *p = req->data;
1152
  int ps = p->p.proto_state;
1153

    
1154
  if (req->down && ((ps == PS_START) || (ps == PS_UP)))
1155
  {
1156
    BGP_TRACE(D_EVENTS, "BFD session down");
1157
    bgp_store_error(p, NULL, BE_MISC, BEM_BFD_DOWN);
1158
    if (ps == PS_UP)
1159
      bgp_update_startup_delay(p);
1160
    bgp_stop(p, 0);
1161
  }
1162
}
1163

    
1164
static void
1165
bgp_update_bfd(struct bgp_proto *p, int use_bfd)
1166
{
1167
  if (use_bfd && !p->bfd_req)
1168
    p->bfd_req = bfd_request_session(p->p.pool, p->cf->remote_ip, p->source_addr,
1169
                                     p->cf->multihop ? NULL : p->neigh->iface,
1170
                                     bgp_bfd_notify, p);
1171

    
1172
  if (!use_bfd && p->bfd_req)
1173
  {
1174
    rfree(p->bfd_req);
1175
    p->bfd_req = NULL;
1176
  }
1177
}
1178

    
1179
static void
1180
bgp_reload_routes(struct channel *C)
1181
{
1182
  struct bgp_proto *p = (void *) C->proto;
1183
  struct bgp_channel *c = (void *) C;
1184

    
1185
  ASSERT(p->conn && p->route_refresh);
1186

    
1187
  bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH);
1188
}
1189

    
1190
static void
1191
bgp_feed_begin(struct channel *C, int initial)
1192
{
1193
  struct bgp_proto *p = (void *) C->proto;
1194
  struct bgp_channel *c = (void *) C;
1195

    
1196
  /* This should not happen */
1197
  if (!p->conn)
1198
    return;
1199

    
1200
  if (initial && p->cf->gr_mode)
1201
    c->feed_state = BFS_LOADING;
1202

    
1203
  /* It is refeed and both sides support enhanced route refresh */
1204
  if (!initial && p->enhanced_refresh)
1205
  {
1206
    /* BoRR must not be sent before End-of-RIB */
1207
    if (c->feed_state == BFS_LOADING || c->feed_state == BFS_LOADED)
1208
      return;
1209

    
1210
    c->feed_state = BFS_REFRESHING;
1211
    bgp_schedule_packet(p->conn, c, PKT_BEGIN_REFRESH);
1212
  }
1213
}
1214

    
1215
static void
1216
bgp_feed_end(struct channel *C)
1217
{
1218
  struct bgp_proto *p = (void *) C->proto;
1219
  struct bgp_channel *c = (void *) C;
1220

    
1221
  /* This should not happen */
1222
  if (!p->conn)
1223
    return;
1224

    
1225
  /* Non-demarcated feed ended, nothing to do */
1226
  if (c->feed_state == BFS_NONE)
1227
    return;
1228

    
1229
  /* Schedule End-of-RIB packet */
1230
  if (c->feed_state == BFS_LOADING)
1231
    c->feed_state = BFS_LOADED;
1232

    
1233
  /* Schedule EoRR packet */
1234
  if (c->feed_state == BFS_REFRESHING)
1235
    c->feed_state = BFS_REFRESHED;
1236

    
1237
  /* Kick TX hook */
1238
  bgp_schedule_packet(p->conn, c, PKT_UPDATE);
1239
}
1240

    
1241

    
1242
static void
1243
bgp_start_locked(struct object_lock *lock)
1244
{
1245
  struct bgp_proto *p = lock->data;
1246
  struct bgp_config *cf = p->cf;
1247

    
1248
  if (p->p.proto_state != PS_START)
1249
  {
1250
    DBG("BGP: Got lock in different state %d\n", p->p.proto_state);
1251
    return;
1252
  }
1253

    
1254
  DBG("BGP: Got lock\n");
1255

    
1256
  if (cf->multihop)
1257
  {
1258
    /* Multi-hop sessions do not use neighbor entries */
1259
    bgp_initiate(p);
1260
    return;
1261
  }
1262

    
1263
  neighbor *n = neigh_find2(&p->p, &cf->remote_ip, cf->iface, NEF_STICKY);
1264
  if (!n)
1265
  {
1266
    log(L_ERR "%s: Invalid remote address %I%J", p->p.name, cf->remote_ip, cf->iface);
1267
    /* As we do not start yet, we can just disable protocol */
1268
    p->p.disabled = 1;
1269
    bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_NEXT_HOP);
1270
    proto_notify_state(&p->p, PS_DOWN);
1271
    return;
1272
  }
1273

    
1274
  p->neigh = n;
1275

    
1276
  if (n->scope <= 0)
1277
    BGP_TRACE(D_EVENTS, "Waiting for %I%J to become my neighbor", cf->remote_ip, cf->iface);
1278
  else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1279
    BGP_TRACE(D_EVENTS, "Waiting for link on %s", n->iface->name);
1280
  else
1281
    bgp_start_neighbor(p);
1282
}
1283

    
1284
static int
1285
bgp_start(struct proto *P)
1286
{
1287
  struct bgp_proto *p = (struct bgp_proto *) P;
1288
  struct object_lock *lock;
1289

    
1290
  DBG("BGP: Startup.\n");
1291
  p->start_state = BSS_PREPARE;
1292
  p->outgoing_conn.state = BS_IDLE;
1293
  p->incoming_conn.state = BS_IDLE;
1294
  p->neigh = NULL;
1295
  p->bfd_req = NULL;
1296
  p->gr_ready = 0;
1297
  p->gr_active_num = 0;
1298

    
1299
  p->event = ev_new(p->p.pool);
1300
  p->event->hook = bgp_decision;
1301
  p->event->data = p;
1302

    
1303
  p->startup_timer = tm_new(p->p.pool);
1304
  p->startup_timer->hook = bgp_startup_timeout;
1305
  p->startup_timer->data = p;
1306

    
1307
  p->gr_timer = tm_new(p->p.pool);
1308
  p->gr_timer->hook = bgp_graceful_restart_timeout;
1309
  p->gr_timer->data = p;
1310

    
1311
  p->local_id = proto_get_router_id(P->cf);
1312
  if (p->rr_client)
1313
    p->rr_cluster_id = p->cf->rr_cluster_id ? p->cf->rr_cluster_id : p->local_id;
1314

    
1315
  p->remote_id = 0;
1316
  p->source_addr = p->cf->local_ip;
1317
  p->link_addr = IPA_NONE;
1318

    
1319
  /* XXXX */
1320
  if (p->p.gr_recovery && p->cf->gr_mode)
1321
  {
1322
    struct bgp_channel *c;
1323
    WALK_LIST(c, p->p.channels)
1324
      channel_graceful_restart_lock(&c->c);
1325
  }
1326

    
1327
  /*
1328
   * Before attempting to create the connection, we need to lock the port,
1329
   * so that we are the only instance attempting to talk with that neighbor.
1330
   */
1331

    
1332
  lock = p->lock = olock_new(P->pool);
1333
  lock->addr = p->cf->remote_ip;
1334
  lock->port = p->cf->remote_port;
1335
  lock->iface = p->cf->iface;
1336
  lock->type = OBJLOCK_TCP;
1337
  lock->hook = bgp_start_locked;
1338
  lock->data = p;
1339
  olock_acquire(lock);
1340

    
1341
  return PS_START;
1342
}
1343

    
1344
extern int proto_restart;
1345

    
1346
static int
1347
bgp_shutdown(struct proto *P)
1348
{
1349
  struct bgp_proto *p = (struct bgp_proto *) P;
1350
  uint subcode = 0;
1351

    
1352
  BGP_TRACE(D_EVENTS, "Shutdown requested");
1353

    
1354
  switch (P->down_code)
1355
  {
1356
  case PDC_CF_REMOVE:
1357
  case PDC_CF_DISABLE:
1358
    subcode = 3; // Errcode 6, 3 - peer de-configured
1359
    break;
1360

    
1361
  case PDC_CF_RESTART:
1362
    subcode = 6; // Errcode 6, 6 - other configuration change
1363
    break;
1364

    
1365
  case PDC_CMD_DISABLE:
1366
  case PDC_CMD_SHUTDOWN:
1367
    subcode = 2; // Errcode 6, 2 - administrative shutdown
1368
    break;
1369

    
1370
  case PDC_CMD_RESTART:
1371
    subcode = 4; // Errcode 6, 4 - administrative reset
1372
    break;
1373

    
1374
  case PDC_RX_LIMIT_HIT:
1375
  case PDC_IN_LIMIT_HIT:
1376
    subcode = 1; // Errcode 6, 1 - max number of prefixes reached
1377
    /* log message for compatibility */
1378
    log(L_WARN "%s: Route limit exceeded, shutting down", p->p.name);
1379
    goto limit;
1380

    
1381
  case PDC_OUT_LIMIT_HIT:
1382
    subcode = proto_restart ? 4 : 2; // Administrative reset or shutdown
1383

    
1384
  limit:
1385
    bgp_store_error(p, NULL, BE_AUTO_DOWN, BEA_ROUTE_LIMIT_EXCEEDED);
1386
    if (proto_restart)
1387
      bgp_update_startup_delay(p);
1388
    else
1389
      p->startup_delay = 0;
1390
    goto done;
1391
  }
1392

    
1393
  bgp_store_error(p, NULL, BE_MAN_DOWN, 0);
1394
  p->startup_delay = 0;
1395

    
1396
done:
1397
  bgp_stop(p, subcode);
1398
  return p->p.proto_state;
1399
}
1400

    
1401
static struct proto *
1402
bgp_init(struct proto_config *CF)
1403
{
1404
  struct proto *P = proto_new(CF);
1405
  struct bgp_proto *p = (struct bgp_proto *) P;
1406
  struct bgp_config *cf = (struct bgp_config *) CF;
1407

    
1408
  P->rt_notify = bgp_rt_notify;
1409
  P->import_control = bgp_import_control;
1410
  P->neigh_notify = bgp_neigh_notify;
1411
  P->reload_routes = bgp_reload_routes;
1412
  P->feed_begin = bgp_feed_begin;
1413
  P->feed_end = bgp_feed_end;
1414
  P->rte_better = bgp_rte_better;
1415
  P->rte_mergable = bgp_rte_mergable;
1416
  P->rte_recalculate = cf->deterministic_med ? bgp_rte_recalculate : NULL;
1417

    
1418
  p->cf = cf;
1419
  p->local_as = cf->local_as;
1420
  p->remote_as = cf->remote_as;
1421
  p->public_as = cf->local_as;
1422
  p->is_internal = (cf->local_as == cf->remote_as);
1423
  p->is_interior = p->is_internal || cf->confederation_member;
1424
  p->rs_client = cf->rs_client;
1425
  p->rr_client = cf->rr_client;
1426

    
1427
  /* Confederation ID is used for truly external peers */
1428
  if (cf->confederation && !p->is_interior)
1429
    p->public_as = cf->confederation;
1430

    
1431
  /* Add all channels */
1432
  struct bgp_channel_config *cc;
1433
  WALK_LIST(cc, CF->channels)
1434
    proto_add_channel(P, &cc->c);
1435

    
1436
  return P;
1437
}
1438

    
1439
static void
1440
bgp_channel_init(struct channel *C, struct channel_config *CF)
1441
{
1442
  struct bgp_channel *c = (void *) C;
1443
  struct bgp_channel_config *cf = (void *) CF;
1444

    
1445
  c->cf = cf;
1446
  c->afi = cf->afi;
1447
  c->desc = cf->desc;
1448

    
1449
  if (cf->igp_table_ip4)
1450
    c->igp_table_ip4 = cf->igp_table_ip4->table;
1451

    
1452
  if (cf->igp_table_ip6)
1453
    c->igp_table_ip6 = cf->igp_table_ip6->table;
1454
}
1455

    
1456
static int
1457
bgp_channel_start(struct channel *C)
1458
{
1459
  struct bgp_proto *p = (void *) C->proto;
1460
  struct bgp_channel *c = (void *) C;
1461
  ip_addr src = p->source_addr;
1462

    
1463
  if (c->igp_table_ip4)
1464
    rt_lock_table(c->igp_table_ip4);
1465

    
1466
  if (c->igp_table_ip6)
1467
    rt_lock_table(c->igp_table_ip6);
1468

    
1469
  c->pool = p->p.pool; // XXXX
1470
  bgp_init_bucket_table(c);
1471
  bgp_init_prefix_table(c);
1472

    
1473
  c->next_hop_addr = c->cf->next_hop_addr;
1474
  c->link_addr = IPA_NONE;
1475
  c->packets_to_send = 0;
1476

    
1477
  /* Try to use source address as next hop address */
1478
  if (ipa_zero(c->next_hop_addr))
1479
  {
1480
    if (bgp_channel_is_ipv4(c) && (ipa_is_ip4(src) || c->ext_next_hop))
1481
      c->next_hop_addr = src;
1482

    
1483
    if (bgp_channel_is_ipv6(c) && (ipa_is_ip6(src) || c->ext_next_hop))
1484
      c->next_hop_addr = src;
1485
  }
1486

    
1487
  /* Exit if no feasible next hop address is found */
1488
  if (ipa_zero(c->next_hop_addr))
1489
  {
1490
    log(L_WARN "%s: Missing next hop address", p->p.name);
1491
    return 0;
1492
  }
1493

    
1494
  /* Set link-local address for IPv6 single-hop BGP */
1495
  if (ipa_is_ip6(c->next_hop_addr) && p->neigh)
1496
  {
1497
    c->link_addr = p->link_addr;
1498

    
1499
    if (ipa_zero(c->link_addr))
1500
      log(L_WARN "%s: Missing link-local address", p->p.name);
1501
  }
1502

    
1503
  /* Link local address is already in c->link_addr */
1504
  if (ipa_is_link_local(c->next_hop_addr))
1505
    c->next_hop_addr = IPA_NONE;
1506

    
1507
  return 0; /* XXXX: Currently undefined */
1508
}
1509

    
1510
static void
1511
bgp_channel_shutdown(struct channel *C)
1512
{
1513
  struct bgp_channel *c = (void *) C;
1514

    
1515
  /* XXXX: cleanup bucket and prefix tables */
1516

    
1517
  c->next_hop_addr = IPA_NONE;
1518
  c->link_addr = IPA_NONE;
1519
}
1520

    
1521
static void
1522
bgp_channel_cleanup(struct channel *C)
1523
{
1524
  struct bgp_channel *c = (void *) C;
1525

    
1526
  if (c->igp_table_ip4)
1527
    rt_unlock_table(c->igp_table_ip4);
1528

    
1529
  if (c->igp_table_ip6)
1530
    rt_unlock_table(c->igp_table_ip6);
1531
}
1532

    
1533
static inline struct bgp_channel_config *
1534
bgp_find_channel_config(struct bgp_config *cf, u32 afi)
1535
{
1536
  struct bgp_channel_config *cc;
1537

    
1538
  WALK_LIST(cc, cf->c.channels)
1539
    if (cc->afi == afi)
1540
      return cc;
1541

    
1542
  return NULL;
1543
}
1544

    
1545
struct rtable_config *
1546
bgp_default_igp_table(struct bgp_config *cf, struct bgp_channel_config *cc, u32 type)
1547
{
1548
  struct bgp_channel_config *cc2;
1549
  struct rtable_config *tab;
1550

    
1551
  /* First, try table connected by the channel */
1552
  if (cc->c.table->addr_type == type)
1553
    return cc->c.table;
1554

    
1555
  /* Find paired channel with the same SAFI but the other AFI */
1556
  u32 afi2 = cc->afi ^ 0x30000;
1557
  cc2 = bgp_find_channel_config(cf, afi2);
1558

    
1559
  /* Second, try IGP table configured in the paired channel */
1560
  if (cc2 && (tab = (type == NET_IP4) ? cc2->igp_table_ip4 : cc2->igp_table_ip6))
1561
    return tab;
1562

    
1563
  /* Third, try table connected by the paired channel */
1564
  if (cc2 && (cc2->c.table->addr_type == type))
1565
    return cc2->c.table;
1566

    
1567
  /* Last, try default table of given type */
1568
  if (tab = cf->c.global->def_tables[type])
1569
    return tab;
1570

    
1571
  cf_error("Undefined IGP table");
1572
}
1573

    
1574

    
1575
void
1576
bgp_postconfig(struct proto_config *CF)
1577
{
1578
  struct bgp_config *cf = (void *) CF;
1579
  int internal = (cf->local_as == cf->remote_as);
1580

    
1581
  /* Do not check templates at all */
1582
  if (cf->c.class == SYM_TEMPLATE)
1583
    return;
1584

    
1585

    
1586
  /* EBGP direct by default, IBGP multihop by default */
1587
  if (cf->multihop < 0)
1588
    cf->multihop = internal ? 64 : 0;
1589

    
1590

    
1591
  if (!cf->local_as)
1592
    cf_error("Local AS number must be set");
1593

    
1594
  if (ipa_zero(cf->remote_ip))
1595
    cf_error("Neighbor must be configured");
1596

    
1597
  if (!cf->remote_as)
1598
    cf_error("Remote AS number must be set");
1599

    
1600
  if (ipa_is_link_local(cf->remote_ip) && !cf->iface)
1601
    cf_error("Link-local neighbor address requires specified interface");
1602

    
1603
  if (!(cf->capabilities && cf->enable_as4) && (cf->remote_as > 0xFFFF))
1604
    cf_error("Neighbor AS number out of range (AS4 not available)");
1605

    
1606
  if (!internal && cf->rr_client)
1607
    cf_error("Only internal neighbor can be RR client");
1608

    
1609
  if (internal && cf->rs_client)
1610
    cf_error("Only external neighbor can be RS client");
1611

    
1612
  if (!cf->confederation && cf->confederation_member)
1613
    cf_error("Confederation ID must be set for member sessions");
1614

    
1615
  if (cf->multihop && (ipa_is_link_local(cf->local_ip) ||
1616
                       ipa_is_link_local(cf->remote_ip)))
1617
    cf_error("Multihop BGP cannot be used with link-local addresses");
1618

    
1619
  if (cf->multihop && cf->iface)
1620
    cf_error("Multihop BGP cannot be bound to interface");
1621

    
1622
  if (cf->multihop && cf->check_link)
1623
    cf_error("Multihop BGP cannot depend on link state");
1624

    
1625
  if (cf->multihop && cf->bfd && ipa_zero(cf->local_ip))
1626
    cf_error("Multihop BGP with BFD requires specified local address");
1627

    
1628

    
1629
  struct bgp_channel_config *cc;
1630
  WALK_LIST(cc, CF->channels)
1631
  {
1632
    /* Disable after error incompatible with restart limit action */
1633
    if ((cc->c.in_limit.action == PLA_RESTART) && cf->disable_after_error)
1634
      cc->c.in_limit.action = PLA_DISABLE;
1635

    
1636
    /* Different default based on rs_client */
1637
    if (!cc->missing_lladdr)
1638
      cc->missing_lladdr = cf->rs_client ? MLL_IGNORE : MLL_SELF;
1639

    
1640
    /* Different default for gw_mode */
1641
    if (!cc->gw_mode)
1642
      cc->gw_mode = cf->multihop ? GW_RECURSIVE : GW_DIRECT;
1643

    
1644
    /* Default based on proto config */
1645
    if (cc->gr_able == 0xff)
1646
      cc->gr_able = (cf->gr_mode == BGP_GR_ABLE);
1647

    
1648
    /* Default values of IGP tables */
1649
    if ((cc->gw_mode == GW_RECURSIVE) && !cc->desc->no_igp)
1650
    {
1651
      if (!cc->igp_table_ip4 && (bgp_cc_is_ipv4(cc) || cc->ext_next_hop))
1652
        cc->igp_table_ip4 = bgp_default_igp_table(cf, cc, NET_IP4);
1653

    
1654
      if (!cc->igp_table_ip6 && (bgp_cc_is_ipv6(cc) || cc->ext_next_hop))
1655
        cc->igp_table_ip6 = bgp_default_igp_table(cf, cc, NET_IP6);
1656

    
1657
      if (cc->igp_table_ip4 && bgp_cc_is_ipv6(cc) && !cc->ext_next_hop)
1658
        cf_error("Mismatched IGP table type");
1659

    
1660
      if (cc->igp_table_ip6 && bgp_cc_is_ipv4(cc) && !cc->ext_next_hop)
1661
        cf_error("Mismatched IGP table type");
1662
    }
1663

    
1664
    if (cf->multihop && (cc->gw_mode == GW_DIRECT))
1665
      cf_error("Multihop BGP cannot use direct gateway mode");
1666

    
1667
    if ((cc->gw_mode == GW_RECURSIVE) && cc->c.table->sorted)
1668
      cf_error("BGP in recursive mode prohibits sorted table");
1669

    
1670
    if (cf->deterministic_med && cc->c.table->sorted)
1671
      cf_error("BGP with deterministic MED prohibits sorted table");
1672

    
1673
    if (cc->secondary && !cc->c.table->sorted)
1674
      cf_error("BGP with secondary option requires sorted table");
1675
  }
1676
}
1677

    
1678
static int
1679
bgp_reconfigure(struct proto *P, struct proto_config *CF)
1680
{
1681
  struct bgp_proto *p = (void *) P;
1682
  struct bgp_config *new = (void *) CF;
1683
  struct bgp_config *old = p->cf;
1684

    
1685
  if (proto_get_router_id(CF) != p->local_id)
1686
    return 0;
1687

    
1688
  int same = !memcmp(((byte *) old) + sizeof(struct proto_config),
1689
                     ((byte *) new) + sizeof(struct proto_config),
1690
                     // password item is last and must be checked separately
1691
                     OFFSETOF(struct bgp_config, password) - sizeof(struct proto_config))
1692
    && ((!old->password && !new->password)
1693
        || (old->password && new->password && !strcmp(old->password, new->password)));
1694

    
1695
  /* FIXME: Move channel reconfiguration to generic protocol code ? */
1696
  struct channel *C, *C2;
1697
  struct bgp_channel_config *cc;
1698

    
1699
  WALK_LIST(C, p->p.channels)
1700
    C->stale = 1;
1701

    
1702
  WALK_LIST(cc, new->c.channels)
1703
  {
1704
    C = (struct channel *) bgp_find_channel(p, cc->afi);
1705
    same = proto_configure_channel(P, &C, &cc->c) && same;
1706
    C->stale = 0;
1707
  }
1708

    
1709
  WALK_LIST_DELSAFE(C, C2, p->p.channels)
1710
    if (C->stale)
1711
      same = proto_configure_channel(P, &C, NULL) && same;
1712

    
1713

    
1714
  if (same && (p->start_state > BSS_PREPARE))
1715
    bgp_update_bfd(p, new->bfd);
1716

    
1717
  /* We should update our copy of configuration ptr as old configuration will be freed */
1718
  if (same)
1719
    p->cf = new;
1720

    
1721
  return same;
1722
}
1723

    
1724
#define IGP_TABLE(cf, sym) ((cf)->igp_table_##sym ? (cf)->igp_table_##sym ->table : NULL )
1725

    
1726
static int
1727
bgp_channel_reconfigure(struct channel *C, struct channel_config *CC)
1728
{
1729
  struct bgp_channel *c = (void *) C;
1730
  struct bgp_channel_config *new = (void *) CC;
1731
  struct bgp_channel_config *old = c->cf;
1732

    
1733
  if (memcmp(((byte *) old) + sizeof(struct channel_config),
1734
             ((byte *) new) + sizeof(struct channel_config),
1735
             /* Remaining items must be checked separately */
1736
             OFFSETOF(struct bgp_channel_config, rest) - sizeof(struct channel_config)))
1737
    return 0;
1738

    
1739
  /* Check change in IGP tables */
1740
  if ((IGP_TABLE(old, ip4) != IGP_TABLE(new, ip4)) ||
1741
      (IGP_TABLE(old, ip6) != IGP_TABLE(new, ip6)))
1742
    return 0;
1743

    
1744
  c->cf = new;
1745
  return 1;
1746
}
1747

    
1748
static void
1749
bgp_copy_config(struct proto_config *dest UNUSED, struct proto_config *src UNUSED)
1750
{
1751
  /* Just a shallow copy */
1752
}
1753

    
1754

    
1755
/**
1756
 * bgp_error - report a protocol error
1757
 * @c: connection
1758
 * @code: error code (according to the RFC)
1759
 * @subcode: error sub-code
1760
 * @data: data to be passed in the Notification message
1761
 * @len: length of the data
1762
 *
1763
 * bgp_error() sends a notification packet to tell the other side that a protocol
1764
 * error has occurred (including the data considered erroneous if possible) and
1765
 * closes the connection.
1766
 */
1767
void
1768
bgp_error(struct bgp_conn *c, uint code, uint subcode, byte *data, int len)
1769
{
1770
  struct bgp_proto *p = c->bgp;
1771

    
1772
  if (c->state == BS_CLOSE)
1773
    return;
1774

    
1775
  bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, data, ABS(len));
1776
  bgp_store_error(p, c, BE_BGP_TX, (code << 16) | subcode);
1777
  bgp_conn_enter_close_state(c);
1778

    
1779
  c->notify_code = code;
1780
  c->notify_subcode = subcode;
1781
  c->notify_data = data;
1782
  c->notify_size = (len > 0) ? len : 0;
1783
  bgp_schedule_packet(c, NULL, PKT_NOTIFICATION);
1784

    
1785
  if (code != 6)
1786
  {
1787
    bgp_update_startup_delay(p);
1788
    bgp_stop(p, 0);
1789
  }
1790
}
1791

    
1792
/**
1793
 * bgp_store_error - store last error for status report
1794
 * @p: BGP instance
1795
 * @c: connection
1796
 * @class: error class (BE_xxx constants)
1797
 * @code: error code (class specific)
1798
 *
1799
 * bgp_store_error() decides whether given error is interesting enough
1800
 * and store that error to last_error variables of @p
1801
 */
1802
void
1803
bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code)
1804
{
1805
  /* During PS_UP, we ignore errors on secondary connection */
1806
  if ((p->p.proto_state == PS_UP) && c && (c != p->conn))
1807
    return;
1808

    
1809
  /* During PS_STOP, we ignore any errors, as we want to report
1810
   * the error that caused transition to PS_STOP
1811
   */
1812
  if (p->p.proto_state == PS_STOP)
1813
    return;
1814

    
1815
  p->last_error_class = class;
1816
  p->last_error_code = code;
1817
}
1818

    
1819
static char *bgp_state_names[] = { "Idle", "Connect", "Active", "OpenSent", "OpenConfirm", "Established", "Close" };
1820
static char *bgp_err_classes[] = { "", "Error: ", "Socket: ", "Received: ", "BGP Error: ", "Automatic shutdown: ", ""};
1821
static char *bgp_misc_errors[] = { "", "Neighbor lost", "Invalid next hop", "Kernel MD5 auth failed", "No listening socket", "Link down", "BFD session down", "Graceful restart"};
1822
static char *bgp_auto_errors[] = { "", "Route limit exceeded"};
1823

    
1824
static const char *
1825
bgp_last_errmsg(struct bgp_proto *p)
1826
{
1827
  switch (p->last_error_class)
1828
  {
1829
  case BE_MISC:
1830
    return bgp_misc_errors[p->last_error_code];
1831
  case BE_SOCKET:
1832
    return (p->last_error_code == 0) ? "Connection closed" : strerror(p->last_error_code);
1833
  case BE_BGP_RX:
1834
  case BE_BGP_TX:
1835
    return bgp_error_dsc(p->last_error_code >> 16, p->last_error_code & 0xFF);
1836
  case BE_AUTO_DOWN:
1837
    return bgp_auto_errors[p->last_error_code];
1838
  default:
1839
    return "";
1840
  }
1841
}
1842

    
1843
static const char *
1844
bgp_state_dsc(struct bgp_proto *p)
1845
{
1846
  if (p->p.proto_state == PS_DOWN)
1847
    return "Down";
1848

    
1849
  int state = MAX(p->incoming_conn.state, p->outgoing_conn.state);
1850
  if ((state == BS_IDLE) && (p->start_state >= BSS_CONNECT) && p->cf->passive)
1851
    return "Passive";
1852

    
1853
  return bgp_state_names[state];
1854
}
1855

    
1856
static void
1857
bgp_get_status(struct proto *P, byte *buf)
1858
{
1859
  struct bgp_proto *p = (struct bgp_proto *) P;
1860

    
1861
  const char *err1 = bgp_err_classes[p->last_error_class];
1862
  const char *err2 = bgp_last_errmsg(p);
1863

    
1864
  if (P->proto_state == PS_DOWN)
1865
    bsprintf(buf, "%s%s", err1, err2);
1866
  else
1867
    bsprintf(buf, "%-14s%s%s", bgp_state_dsc(p), err1, err2);
1868
}
1869

    
1870
static void
1871
bgp_show_afis(int code, char *s, u32 *afis, uint count)
1872
{
1873
  buffer b;
1874
  LOG_BUFFER_INIT(b);
1875

    
1876
  buffer_puts(&b, s);
1877

    
1878
  for (u32 *af = afis; af < (afis + count); af++)
1879
  {
1880
    const struct bgp_af_desc *desc = bgp_get_af_desc(*af);
1881
    if (desc)
1882
      buffer_print(&b, " %s", desc->name);
1883
    else
1884
      buffer_print(&b, " <%u/%u>", BGP_AFI(*af), BGP_SAFI(*af));
1885
  }
1886

    
1887
  if (b.pos == b.end)
1888
    strcpy(b.end - 32, " ... <too long>");
1889

    
1890
  cli_msg(code, b.start);
1891
}
1892

    
1893
static void
1894
bgp_show_capabilities(struct bgp_proto *p UNUSED, struct bgp_caps *caps)
1895
{
1896
  struct bgp_af_caps *ac;
1897
  uint any_mp_bgp = 0;
1898
  uint any_gr_able = 0;
1899
  uint any_add_path = 0;
1900
  uint any_ext_next_hop = 0;
1901
  u32 *afl1 = alloca(caps->af_count * sizeof(u32));
1902
  u32 *afl2 = alloca(caps->af_count * sizeof(u32));
1903
  uint afn1, afn2;
1904

    
1905
  WALK_AF_CAPS(caps, ac)
1906
  {
1907
    any_mp_bgp |= ac->ready;
1908
    any_gr_able |= ac->gr_able;
1909
    any_add_path |= ac->add_path;
1910
    any_ext_next_hop |= ac->ext_next_hop;
1911
  }
1912

    
1913
  if (any_mp_bgp)
1914
  {
1915
    cli_msg(-1006, "      Multiprotocol");
1916

    
1917
    afn1 = 0;
1918
    WALK_AF_CAPS(caps, ac)
1919
      if (ac->ready)
1920
        afl1[afn1++] = ac->afi;
1921

    
1922
    bgp_show_afis(-1006, "        AF announced:", afl1, afn1);
1923
  }
1924

    
1925
  if (caps->route_refresh)
1926
    cli_msg(-1006, "      Route refresh");
1927

    
1928
  if (any_ext_next_hop)
1929
  {
1930
    cli_msg(-1006, "      Extended next hop");
1931

    
1932
    afn1 = 0;
1933
    WALK_AF_CAPS(caps, ac)
1934
      if (ac->ext_next_hop)
1935
        afl1[afn1++] = ac->afi;
1936

    
1937
    bgp_show_afis(-1006, "        IPv6 nexthop:", afl1, afn1);
1938
  }
1939

    
1940
  if (caps->ext_messages)
1941
    cli_msg(-1006, "      Extended message");
1942

    
1943
  if (caps->gr_aware)
1944
    cli_msg(-1006, "      Graceful restart");
1945

    
1946
  if (any_gr_able)
1947
  {
1948
    /* Continues from gr_aware */
1949
    cli_msg(-1006, "        Restart time: %u", caps->gr_time);
1950
    if (caps->gr_flags & BGP_GRF_RESTART)
1951
      cli_msg(-1006, "        Restart recovery");
1952

    
1953
    afn1 = afn2 = 0;
1954
    WALK_AF_CAPS(caps, ac)
1955
    {
1956
      if (ac->gr_able)
1957
        afl1[afn1++] = ac->afi;
1958

    
1959
      if (ac->gr_af_flags & BGP_GRF_FORWARDING)
1960
        afl2[afn2++] = ac->afi;
1961
    }
1962

    
1963
    bgp_show_afis(-1006, "        AF supported:", afl1, afn1);
1964
    bgp_show_afis(-1006, "        AF preserved:", afl2, afn2);
1965
  }
1966

    
1967
  if (caps->as4_support)
1968
    cli_msg(-1006, "      4-octet AS numbers");
1969

    
1970
  if (any_add_path)
1971
  {
1972
    cli_msg(-1006, "      ADD-PATH");
1973

    
1974
    afn1 = afn2 = 0;
1975
    WALK_AF_CAPS(caps, ac)
1976
    {
1977
      if (ac->add_path & BGP_ADD_PATH_RX)
1978
        afl1[afn1++] = ac->afi;
1979

    
1980
      if (ac->add_path & BGP_ADD_PATH_TX)
1981
        afl2[afn2++] = ac->afi;
1982
    }
1983

    
1984
    bgp_show_afis(-1006, "        RX:", afl1, afn1);
1985
    bgp_show_afis(-1006, "        TX:", afl2, afn2);
1986
  }
1987

    
1988
  if (caps->enhanced_refresh)
1989
    cli_msg(-1006, "      Enhanced refresh");
1990
}
1991

    
1992
static void
1993
bgp_show_proto_info(struct proto *P)
1994
{
1995
  struct bgp_proto *p = (struct bgp_proto *) P;
1996

    
1997
  cli_msg(-1006, "  BGP state:          %s", bgp_state_dsc(p));
1998
  cli_msg(-1006, "    Neighbor address: %I%J", p->cf->remote_ip, p->cf->iface);
1999
  cli_msg(-1006, "    Neighbor AS:      %u", p->remote_as);
2000

    
2001
  if (p->gr_active_num)
2002
    cli_msg(-1006, "    Neighbor graceful restart active");
2003

    
2004
  if (P->proto_state == PS_START)
2005
  {
2006
    struct bgp_conn *oc = &p->outgoing_conn;
2007

    
2008
    if ((p->start_state < BSS_CONNECT) &&
2009
        (p->startup_timer->expires))
2010
      cli_msg(-1006, "    Error wait:       %d/%d",
2011
              p->startup_timer->expires - now, p->startup_delay);
2012

    
2013
    if ((oc->state == BS_ACTIVE) &&
2014
        (oc->connect_timer->expires))
2015
      cli_msg(-1006, "    Connect delay:    %d/%d",
2016
              oc->connect_timer->expires - now, p->cf->connect_delay_time);
2017

    
2018
    if (p->gr_active_num && p->gr_timer->expires)
2019
      cli_msg(-1006, "    Restart timer:    %d/-", p->gr_timer->expires - now);
2020
  }
2021
  else if (P->proto_state == PS_UP)
2022
  {
2023
    cli_msg(-1006, "    Neighbor ID:      %R", p->remote_id);
2024
    cli_msg(-1006, "    Local capabilities");
2025
    bgp_show_capabilities(p, p->conn->local_caps);
2026
    cli_msg(-1006, "    Neighbor capabilities");
2027
    bgp_show_capabilities(p, p->conn->remote_caps);
2028
/* XXXX
2029
      cli_msg(-1006, "    Session:          %s%s%s%s%s%s%s%s",
2030
              p->is_internal ? "internal" : "external",
2031
              p->cf->multihop ? " multihop" : "",
2032
              p->rr_client ? " route-reflector" : "",
2033
              p->rs_client ? " route-server" : "",
2034
              p->as4_session ? " AS4" : "",
2035
              p->add_path_rx ? " add-path-rx" : "",
2036
              p->add_path_tx ? " add-path-tx" : "",
2037
              p->ext_messages ? " ext-messages" : "");
2038
*/
2039
    cli_msg(-1006, "    Source address:   %I", p->source_addr);
2040
    cli_msg(-1006, "    Hold timer:       %d/%d",
2041
            tm_remains(p->conn->hold_timer), p->conn->hold_time);
2042
    cli_msg(-1006, "    Keepalive timer:  %d/%d",
2043
            tm_remains(p->conn->keepalive_timer), p->conn->keepalive_time);
2044
  }
2045

    
2046
  if ((p->last_error_class != BE_NONE) &&
2047
      (p->last_error_class != BE_MAN_DOWN))
2048
  {
2049
    const char *err1 = bgp_err_classes[p->last_error_class];
2050
    const char *err2 = bgp_last_errmsg(p);
2051
    cli_msg(-1006, "    Last error:       %s%s", err1, err2);
2052
  }
2053

    
2054
  {
2055
    /* XXXX ?? */
2056
    struct bgp_channel *c;
2057
    WALK_LIST(c, p->p.channels)
2058
    {
2059
      channel_show_info(&c->c);
2060

    
2061
      if (c->igp_table_ip4)
2062
        cli_msg(-1006, "    IGP IPv4 table: %s", c->igp_table_ip4->name);
2063

    
2064
      if (c->igp_table_ip6)
2065
        cli_msg(-1006, "    IGP IPv6 table: %s", c->igp_table_ip6->name);
2066
    }
2067
  }
2068
}
2069

    
2070
struct channel_class channel_bgp = {
2071
  .channel_size =        sizeof(struct bgp_channel),
2072
  .config_size =        sizeof(struct bgp_channel_config),
2073
  .init =                bgp_channel_init,
2074
  .start =                bgp_channel_start,
2075
  .shutdown =                bgp_channel_shutdown,
2076
  .cleanup =                bgp_channel_cleanup,
2077
  .reconfigure =        bgp_channel_reconfigure,
2078
};
2079

    
2080
struct protocol proto_bgp = {
2081
  .name =                 "BGP",
2082
  .template =                 "bgp%d",
2083
  .attr_class =         EAP_BGP,
2084
  .preference =         DEF_PREF_BGP,
2085
  .channel_mask =        NB_IP | NB_VPN | NB_FLOW,
2086
  .proto_size =                sizeof(struct bgp_proto),
2087
  .config_size =        sizeof(struct bgp_config),
2088
  .postconfig =                bgp_postconfig,
2089
  .init =                 bgp_init,
2090
  .start =                 bgp_start,
2091
  .shutdown =                 bgp_shutdown,
2092
  .reconfigure =         bgp_reconfigure,
2093
  .copy_config =         bgp_copy_config,
2094
  .get_status =         bgp_get_status,
2095
  .get_attr =                 bgp_get_attr,
2096
  .get_route_info =         bgp_get_route_info,
2097
  .show_proto_info =         bgp_show_proto_info
2098
};