Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bgp / bgp.c @ 7ff34ca2

History | View | Annotate | Download (66.3 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
 * 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
89
 * RFC 5549 - Advertising IPv4 NLRI with an IPv6 Next Hop
90
 * RFC 5575 - Dissemination of Flow Specification Rules
91
 * RFC 5668 - 4-Octet AS Specific BGP Extended Community
92
 * RFC 6286 - AS-Wide Unique BGP Identifier
93
 * RFC 6608 - Subcodes for BGP Finite State Machine Error
94
 * RFC 6793 - BGP Support for 4-Octet AS Numbers
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
102
 * draft-ietf-idr-bgp-extended-messages-27
103
 * draft-uttaro-idr-bgp-persistence-04
104
 */
105

    
106
#undef LOCAL_DEBUG
107

    
108
#include <stdlib.h>
109

    
110
#include "nest/bird.h"
111
#include "nest/iface.h"
112
#include "nest/protocol.h"
113
#include "nest/route.h"
114
#include "nest/cli.h"
115
#include "nest/locks.h"
116
#include "conf/conf.h"
117
#include "filter/filter.h"
118
#include "lib/socket.h"
119
#include "lib/resource.h"
120
#include "lib/string.h"
121

    
122
#include "bgp.h"
123

    
124

    
125
struct linpool *bgp_linpool;                /* Global temporary pool */
126
struct linpool *bgp_linpool2;                /* Global temporary pool for bgp_rt_notify() */
127
static list bgp_sockets;                /* Global list of listening sockets */
128

    
129

    
130
static void bgp_connect(struct bgp_proto *p);
131
static void bgp_active(struct bgp_proto *p);
132
static void bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn);
133
static void bgp_setup_sk(struct bgp_conn *conn, sock *s);
134
static void bgp_send_open(struct bgp_conn *conn);
135
static void bgp_update_bfd(struct bgp_proto *p, int use_bfd);
136

    
137
static int bgp_incoming_connection(sock *sk, uint dummy UNUSED);
138
static void bgp_listen_sock_err(sock *sk UNUSED, int err);
139

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

    
158
  /* FIXME: Add some global init? */
159
  if (!bgp_linpool)
160
    init_list(&bgp_sockets);
161

    
162
  /* We assume that cf->iface is defined iff cf->local_ip is link-local */
163

    
164
  WALK_LIST(bs, bgp_sockets)
165
    if (ipa_equal(bs->sk->saddr, addr) && (bs->sk->sport == port) &&
166
        (bs->sk->iface == ifa) && (bs->sk->vrf == p->p.vrf))
167
    {
168
      bs->uc++;
169
      p->sock = bs;
170
      return 0;
171
    }
172

    
173
  sock *sk = sk_new(proto_pool);
174
  sk->type = SK_TCP_PASSIVE;
175
  sk->ttl = 255;
176
  sk->saddr = addr;
177
  sk->sport = port;
178
  sk->iface = ifa;
179
  sk->vrf = p->p.vrf;
180
  sk->flags = 0;
181
  sk->tos = IP_PREC_INTERNET_CONTROL;
182
  sk->rbsize = BGP_RX_BUFFER_SIZE;
183
  sk->tbsize = BGP_TX_BUFFER_SIZE;
184
  sk->rx_hook = bgp_incoming_connection;
185
  sk->err_hook = bgp_listen_sock_err;
186

    
187
  if (sk_open(sk) < 0)
188
    goto err;
189

    
190
  bs = mb_allocz(proto_pool, sizeof(struct bgp_socket));
191
  bs->sk = sk;
192
  bs->uc = 1;
193
  p->sock = bs;
194
  sk->data = bs;
195

    
196
  add_tail(&bgp_sockets, &bs->n);
197

    
198
  if (!bgp_linpool)
199
  {
200
    bgp_linpool  = lp_new_default(proto_pool);
201
    bgp_linpool2 = lp_new_default(proto_pool);
202
  }
203

    
204
  return 0;
205

    
206
err:
207
  sk_log_error(sk, p->p.name);
208
  log(L_ERR "%s: Cannot open listening socket", p->p.name);
209
  rfree(sk);
210
  return -1;
211
}
212

    
213
/**
214
 * bgp_close - close a BGP instance
215
 * @p: BGP instance
216
 *
217
 * This function frees and deconfigures shared BGP resources.
218
 */
219
static void
220
bgp_close(struct bgp_proto *p)
221
{
222
  struct bgp_socket *bs = p->sock;
223

    
224
  ASSERT(bs && bs->uc);
225

    
226
  if (--bs->uc)
227
    return;
228

    
229
  rfree(bs->sk);
230
  rem_node(&bs->n);
231
  mb_free(bs);
232

    
233
  if (!EMPTY_LIST(bgp_sockets))
234
    return;
235

    
236
  rfree(bgp_linpool);
237
  bgp_linpool = NULL;
238

    
239
  rfree(bgp_linpool2);
240
  bgp_linpool2 = NULL;
241
}
242

    
243
static inline int
244
bgp_setup_auth(struct bgp_proto *p, int enable)
245
{
246
  if (p->cf->password)
247
  {
248
    int rv = sk_set_md5_auth(p->sock->sk,
249
                             p->cf->local_ip, p->cf->remote_ip, p->cf->iface,
250
                             enable ? p->cf->password : NULL, p->cf->setkey);
251

    
252
    if (rv < 0)
253
      sk_log_error(p->sock->sk, p->p.name);
254

    
255
    return rv;
256
  }
257
  else
258
    return 0;
259
}
260

    
261
static inline struct bgp_channel *
262
bgp_find_channel(struct bgp_proto *p, u32 afi)
263
{
264
  struct bgp_channel *c;
265
  WALK_LIST(c, p->p.channels)
266
    if (c->afi == afi)
267
      return c;
268

    
269
  return NULL;
270
}
271

    
272
static void
273
bgp_startup(struct bgp_proto *p)
274
{
275
  BGP_TRACE(D_EVENTS, "Started");
276
  p->start_state = BSS_CONNECT;
277

    
278
  if (!p->passive)
279
    bgp_active(p);
280

    
281
  if (p->postponed_sk)
282
  {
283
    /* Apply postponed incoming connection */
284
    bgp_setup_conn(p, &p->incoming_conn);
285
    bgp_setup_sk(&p->incoming_conn, p->postponed_sk);
286
    bgp_send_open(&p->incoming_conn);
287
    p->postponed_sk = NULL;
288
  }
289
}
290

    
291
static void
292
bgp_startup_timeout(timer *t)
293
{
294
  bgp_startup(t->data);
295
}
296

    
297

    
298
static void
299
bgp_initiate(struct bgp_proto *p)
300
{
301
  int err_val;
302

    
303
  if (bgp_open(p) < 0)
304
  { err_val = BEM_NO_SOCKET; goto err1; }
305

    
306
  if (bgp_setup_auth(p, 1) < 0)
307
  { err_val = BEM_INVALID_MD5; goto err2; }
308

    
309
  if (p->cf->bfd)
310
    bgp_update_bfd(p, p->cf->bfd);
311

    
312
  if (p->startup_delay)
313
  {
314
    p->start_state = BSS_DELAY;
315
    BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds due to errors", p->startup_delay);
316
    bgp_start_timer(p->startup_timer, p->startup_delay);
317
  }
318
  else
319
    bgp_startup(p);
320

    
321
  return;
322

    
323
err2:
324
  bgp_close(p);
325
err1:
326
  p->p.disabled = 1;
327
  bgp_store_error(p, NULL, BE_MISC, err_val);
328
  proto_notify_state(&p->p, PS_DOWN);
329

    
330
  return;
331
}
332

    
333
/**
334
 * bgp_start_timer - start a BGP timer
335
 * @t: timer
336
 * @value: time (in seconds) to fire (0 to disable the timer)
337
 *
338
 * This functions calls tm_start() on @t with time @value and the amount of
339
 * randomization suggested by the BGP standard. Please use it for all BGP
340
 * timers.
341
 */
342
void
343
bgp_start_timer(timer *t, uint value)
344
{
345
  if (value)
346
  {
347
    /* The randomization procedure is specified in RFC 4271 section 10 */
348
    btime time = value S;
349
    btime randomize = random() % ((time / 4) + 1);
350
    tm_start(t, time - randomize);
351
  }
352
  else
353
    tm_stop(t);
354
}
355

    
356
/**
357
 * bgp_close_conn - close a BGP connection
358
 * @conn: connection to close
359
 *
360
 * This function takes a connection described by the &bgp_conn structure, closes
361
 * its socket and frees all resources associated with it.
362
 */
363
void
364
bgp_close_conn(struct bgp_conn *conn)
365
{
366
  // struct bgp_proto *p = conn->bgp;
367

    
368
  DBG("BGP: Closing connection\n");
369
  conn->packets_to_send = 0;
370
  conn->channels_to_send = 0;
371
  rfree(conn->connect_timer);
372
  conn->connect_timer = NULL;
373
  rfree(conn->keepalive_timer);
374
  conn->keepalive_timer = NULL;
375
  rfree(conn->hold_timer);
376
  conn->hold_timer = NULL;
377
  rfree(conn->tx_ev);
378
  conn->tx_ev = NULL;
379
  rfree(conn->sk);
380
  conn->sk = NULL;
381

    
382
  mb_free(conn->local_caps);
383
  conn->local_caps = NULL;
384
  mb_free(conn->remote_caps);
385
  conn->remote_caps = NULL;
386
}
387

    
388

    
389
/**
390
 * bgp_update_startup_delay - update a startup delay
391
 * @p: BGP instance
392
 *
393
 * This function updates a startup delay that is used to postpone next BGP
394
 * connect. It also handles disable_after_error and might stop BGP instance
395
 * when error happened and disable_after_error is on.
396
 *
397
 * It should be called when BGP protocol error happened.
398
 */
399
void
400
bgp_update_startup_delay(struct bgp_proto *p)
401
{
402
  const struct bgp_config *cf = p->cf;
403

    
404
  DBG("BGP: Updating startup delay\n");
405

    
406
  if (p->last_proto_error && ((current_time() - p->last_proto_error) >= cf->error_amnesia_time S))
407
    p->startup_delay = 0;
408

    
409
  p->last_proto_error = current_time();
410

    
411
  if (cf->disable_after_error)
412
  {
413
    p->startup_delay = 0;
414
    p->p.disabled = 1;
415
    return;
416
  }
417

    
418
  if (!p->startup_delay)
419
    p->startup_delay = cf->error_delay_time_min;
420
  else
421
    p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max);
422
}
423

    
424
static void
425
bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode, byte *data, uint len)
426
{
427
  switch (conn->state)
428
  {
429
  case BS_IDLE:
430
  case BS_CLOSE:
431
    return;
432

    
433
  case BS_CONNECT:
434
  case BS_ACTIVE:
435
    bgp_conn_enter_idle_state(conn);
436
    return;
437

    
438
  case BS_OPENSENT:
439
  case BS_OPENCONFIRM:
440
  case BS_ESTABLISHED:
441
    bgp_error(conn, 6, subcode, data, len);
442
    return;
443

    
444
  default:
445
    bug("bgp_graceful_close_conn: Unknown state %d", conn->state);
446
  }
447
}
448

    
449
static void
450
bgp_down(struct bgp_proto *p)
451
{
452
  if (p->start_state > BSS_PREPARE)
453
  {
454
    bgp_setup_auth(p, 0);
455
    bgp_close(p);
456
  }
457

    
458
  BGP_TRACE(D_EVENTS, "Down");
459
  proto_notify_state(&p->p, PS_DOWN);
460
}
461

    
462
static void
463
bgp_decision(void *vp)
464
{
465
  struct bgp_proto *p = vp;
466

    
467
  DBG("BGP: Decision start\n");
468
  if ((p->p.proto_state == PS_START) &&
469
      (p->outgoing_conn.state == BS_IDLE) &&
470
      (p->incoming_conn.state != BS_OPENCONFIRM) &&
471
      !p->passive)
472
    bgp_active(p);
473

    
474
  if ((p->p.proto_state == PS_STOP) &&
475
      (p->outgoing_conn.state == BS_IDLE) &&
476
      (p->incoming_conn.state == BS_IDLE))
477
    bgp_down(p);
478
}
479

    
480
static struct bgp_proto *
481
bgp_spawn(struct bgp_proto *pp, ip_addr remote_ip)
482
{
483
  struct symbol *sym;
484
  char fmt[SYM_MAX_LEN];
485

    
486
  bsprintf(fmt, "%s%%0%dd", pp->cf->dynamic_name, pp->cf->dynamic_name_digits);
487

    
488
  /* This is hack, we would like to share config, but we need to copy it now */
489
  new_config = config;
490
  cfg_mem = config->mem;
491
  conf_this_scope = config->root_scope;
492
  sym = cf_default_name(fmt, &(pp->dynamic_name_counter));
493
  proto_clone_config(sym, pp->p.cf);
494
  new_config = NULL;
495
  cfg_mem = NULL;
496

    
497
  /* Just pass remote_ip to bgp_init() */
498
  ((struct bgp_config *) sym->def)->remote_ip = remote_ip;
499

    
500
  return (void *) proto_spawn(sym->def, 0);
501
}
502

    
503
void
504
bgp_stop(struct bgp_proto *p, uint subcode, byte *data, uint len)
505
{
506
  proto_notify_state(&p->p, PS_STOP);
507
  bgp_graceful_close_conn(&p->outgoing_conn, subcode, data, len);
508
  bgp_graceful_close_conn(&p->incoming_conn, subcode, data, len);
509
  ev_schedule(p->event);
510
}
511

    
512
static inline void
513
bgp_conn_set_state(struct bgp_conn *conn, uint new_state)
514
{
515
  if (conn->bgp->p.mrtdump & MD_STATES)
516
    bgp_dump_state_change(conn, conn->state, new_state);
517

    
518
  conn->state = new_state;
519
}
520

    
521
void
522
bgp_conn_enter_openconfirm_state(struct bgp_conn *conn)
523
{
524
  /* Really, most of the work is done in bgp_rx_open(). */
525
  bgp_conn_set_state(conn, BS_OPENCONFIRM);
526
}
527

    
528
static const struct bgp_af_caps dummy_af_caps = { };
529
static const struct bgp_af_caps basic_af_caps = { .ready = 1 };
530

    
531
void
532
bgp_conn_enter_established_state(struct bgp_conn *conn)
533
{
534
  struct bgp_proto *p = conn->bgp;
535
  struct bgp_caps *local = conn->local_caps;
536
  struct bgp_caps *peer = conn->remote_caps;
537
  struct bgp_channel *c;
538

    
539
  BGP_TRACE(D_EVENTS, "BGP session established");
540

    
541
  /* For multi-hop BGP sessions */
542
  if (ipa_zero(p->local_ip))
543
    p->local_ip = conn->sk->saddr;
544

    
545
  /* For promiscuous sessions */
546
  if (!p->remote_as)
547
    p->remote_as = conn->received_as;
548

    
549
  /* In case of LLv6 is not valid during BGP start */
550
  if (ipa_zero(p->link_addr) && p->neigh && p->neigh->iface && p->neigh->iface->llv6)
551
    p->link_addr = p->neigh->iface->llv6->ip;
552

    
553
  conn->sk->fast_rx = 0;
554

    
555
  p->conn = conn;
556
  p->last_error_class = 0;
557
  p->last_error_code = 0;
558

    
559
  p->as4_session = conn->as4_session;
560

    
561
  p->route_refresh = peer->route_refresh;
562
  p->enhanced_refresh = local->enhanced_refresh && peer->enhanced_refresh;
563

    
564
  /* Whether we may handle possible GR/LLGR of peer (it has some AF GR-able) */
565
  p->gr_ready = p->llgr_ready = 0;        /* Updated later */
566

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

    
570
  if (p->gr_active_num)
571
    tm_stop(p->gr_timer);
572

    
573
  /* Number of active channels */
574
  int num = 0;
575

    
576
  /* Summary state of ADD_PATH RX for active channels */
577
  uint summary_add_path_rx = 0;
578

    
579
  WALK_LIST(c, p->p.channels)
580
  {
581
    const struct bgp_af_caps *loc = bgp_find_af_caps(local, c->afi);
582
    const struct bgp_af_caps *rem = bgp_find_af_caps(peer,  c->afi);
583

    
584
    /* Use default if capabilities were not announced */
585
    if (!local->length && (c->afi == BGP_AF_IPV4))
586
      loc = &basic_af_caps;
587

    
588
    if (!peer->length && (c->afi == BGP_AF_IPV4))
589
      rem = &basic_af_caps;
590

    
591
    /* Ignore AFIs that were not announced in multiprotocol capability */
592
    if (!loc || !loc->ready)
593
      loc = &dummy_af_caps;
594

    
595
    if (!rem || !rem->ready)
596
      rem = &dummy_af_caps;
597

    
598
    int active = loc->ready && rem->ready;
599
    c->c.disabled = !active;
600
    c->c.reloadable = p->route_refresh || c->cf->import_table;
601

    
602
    c->index = active ? num++ : 0;
603

    
604
    c->feed_state = BFS_NONE;
605
    c->load_state = BFS_NONE;
606

    
607
    /* Channels where peer may do GR */
608
    uint gr_ready = active && local->gr_aware && rem->gr_able;
609
    uint llgr_ready = active && local->llgr_aware && rem->llgr_able;
610

    
611
    c->gr_ready = gr_ready || llgr_ready;
612
    p->gr_ready = p->gr_ready || c->gr_ready;
613
    p->llgr_ready = p->llgr_ready || llgr_ready;
614

    
615
    /* Remember last LLGR stale time */
616
    c->stale_time = local->llgr_aware ? rem->llgr_time : 0;
617

    
618
    /* Channels not able to recover gracefully */
619
    if (p->p.gr_recovery && (!active || !peer_gr_ready))
620
      channel_graceful_restart_unlock(&c->c);
621

    
622
    /* Channels waiting for local convergence */
623
    if (p->p.gr_recovery && loc->gr_able && peer_gr_ready)
624
      c->c.gr_wait = 1;
625

    
626
    /* Channels where regular graceful restart failed */
627
    if ((c->gr_active == BGP_GRS_ACTIVE) &&
628
        !(active && rem->gr_able && (rem->gr_af_flags & BGP_GRF_FORWARDING)))
629
      bgp_graceful_restart_done(c);
630

    
631
    /* Channels where regular long-lived restart failed */
632
    if ((c->gr_active == BGP_GRS_LLGR) &&
633
        !(active && rem->llgr_able && (rem->gr_af_flags & BGP_LLGRF_FORWARDING)))
634
      bgp_graceful_restart_done(c);
635

    
636
    /* GR capability implies that neighbor will send End-of-RIB */
637
    if (peer->gr_aware)
638
      c->load_state = BFS_LOADING;
639

    
640
    c->ext_next_hop = c->cf->ext_next_hop && (bgp_channel_is_ipv6(c) || rem->ext_next_hop);
641
    c->add_path_rx = (loc->add_path & BGP_ADD_PATH_RX) && (rem->add_path & BGP_ADD_PATH_TX);
642
    c->add_path_tx = (loc->add_path & BGP_ADD_PATH_TX) && (rem->add_path & BGP_ADD_PATH_RX);
643

    
644
    if (active)
645
      summary_add_path_rx |= !c->add_path_rx ? 1 : 2;
646

    
647
    /* Update RA mode */
648
    if (c->add_path_tx)
649
      c->c.ra_mode = RA_ANY;
650
    else if (c->cf->secondary)
651
      c->c.ra_mode = RA_ACCEPTED;
652
    else
653
      c->c.ra_mode = RA_OPTIMAL;
654
  }
655

    
656
  p->afi_map = mb_alloc(p->p.pool, num * sizeof(u32));
657
  p->channel_map = mb_alloc(p->p.pool, num * sizeof(void *));
658
  p->channel_count = num;
659
  p->summary_add_path_rx = summary_add_path_rx;
660

    
661
  WALK_LIST(c, p->p.channels)
662
  {
663
    if (c->c.disabled)
664
      continue;
665

    
666
    p->afi_map[c->index] = c->afi;
667
    p->channel_map[c->index] = c;
668
  }
669

    
670
  /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */
671

    
672
  bgp_conn_set_state(conn, BS_ESTABLISHED);
673
  proto_notify_state(&p->p, PS_UP);
674
}
675

    
676
static void
677
bgp_conn_leave_established_state(struct bgp_proto *p)
678
{
679
  BGP_TRACE(D_EVENTS, "BGP session closed");
680
  p->conn = NULL;
681

    
682
  if (p->p.proto_state == PS_UP)
683
    bgp_stop(p, 0, NULL, 0);
684
}
685

    
686
void
687
bgp_conn_enter_close_state(struct bgp_conn *conn)
688
{
689
  struct bgp_proto *p = conn->bgp;
690
  int os = conn->state;
691

    
692
  bgp_conn_set_state(conn, BS_CLOSE);
693
  tm_stop(conn->keepalive_timer);
694
  conn->sk->rx_hook = NULL;
695

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

    
699
  if (os == BS_ESTABLISHED)
700
    bgp_conn_leave_established_state(p);
701
}
702

    
703
void
704
bgp_conn_enter_idle_state(struct bgp_conn *conn)
705
{
706
  struct bgp_proto *p = conn->bgp;
707
  int os = conn->state;
708

    
709
  bgp_close_conn(conn);
710
  bgp_conn_set_state(conn, BS_IDLE);
711
  ev_schedule(p->event);
712

    
713
  if (os == BS_ESTABLISHED)
714
    bgp_conn_leave_established_state(p);
715
}
716

    
717
/**
718
 * bgp_handle_graceful_restart - handle detected BGP graceful restart
719
 * @p: BGP instance
720
 *
721
 * This function is called when a BGP graceful restart of the neighbor is
722
 * detected (when the TCP connection fails or when a new TCP connection
723
 * appears). The function activates processing of the restart - starts routing
724
 * table refresh cycle and activates BGP restart timer. The protocol state goes
725
 * back to %PS_START, but changing BGP state back to %BS_IDLE is left for the
726
 * caller.
727
 */
728
void
729
bgp_handle_graceful_restart(struct bgp_proto *p)
730
{
731
  ASSERT(p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready);
732

    
733
  BGP_TRACE(D_EVENTS, "Neighbor graceful restart detected%s",
734
            p->gr_active_num ? " - already pending" : "");
735

    
736
  p->gr_active_num = 0;
737

    
738
  struct bgp_channel *c;
739
  WALK_LIST(c, p->p.channels)
740
  {
741
    /* FIXME: perhaps check for channel state instead of disabled flag? */
742
    if (c->c.disabled)
743
      continue;
744

    
745
    if (c->gr_ready)
746
    {
747
      p->gr_active_num++;
748

    
749
      switch (c->gr_active)
750
      {
751
      case BGP_GRS_NONE:
752
        c->gr_active = BGP_GRS_ACTIVE;
753
        rt_refresh_begin(c->c.table, &c->c);
754
        break;
755

    
756
      case BGP_GRS_ACTIVE:
757
        rt_refresh_end(c->c.table, &c->c);
758
        rt_refresh_begin(c->c.table, &c->c);
759
        break;
760

    
761
      case BGP_GRS_LLGR:
762
        rt_refresh_begin(c->c.table, &c->c);
763
        rt_modify_stale(c->c.table, &c->c);
764
        break;
765
      }
766
    }
767
    else
768
    {
769
      /* Just flush the routes */
770
      rt_refresh_begin(c->c.table, &c->c);
771
      rt_refresh_end(c->c.table, &c->c);
772
    }
773

    
774
    /* Reset bucket and prefix tables */
775
    bgp_free_bucket_table(c);
776
    bgp_free_prefix_table(c);
777
    bgp_init_bucket_table(c);
778
    bgp_init_prefix_table(c);
779
    c->packets_to_send = 0;
780
  }
781

    
782
  /* p->gr_ready -> at least one active channel is c->gr_ready */
783
  ASSERT(p->gr_active_num > 0);
784

    
785
  proto_notify_state(&p->p, PS_START);
786
  tm_start(p->gr_timer, p->conn->remote_caps->gr_time S);
787
}
788

    
789
/**
790
 * bgp_graceful_restart_done - finish active BGP graceful restart
791
 * @c: BGP channel
792
 *
793
 * This function is called when the active BGP graceful restart of the neighbor
794
 * should be finished for channel @c - either successfully (the neighbor sends
795
 * all paths and reports end-of-RIB for given AFI/SAFI on the new session) or
796
 * unsuccessfully (the neighbor does not support BGP graceful restart on the new
797
 * session). The function ends the routing table refresh cycle.
798
 */
799
void
800
bgp_graceful_restart_done(struct bgp_channel *c)
801
{
802
  struct bgp_proto *p = (void *) c->c.proto;
803

    
804
  ASSERT(c->gr_active);
805
  c->gr_active = 0;
806
  p->gr_active_num--;
807

    
808
  if (!p->gr_active_num)
809
    BGP_TRACE(D_EVENTS, "Neighbor graceful restart done");
810

    
811
  tm_stop(c->stale_timer);
812
  rt_refresh_end(c->c.table, &c->c);
813
}
814

    
815
/**
816
 * bgp_graceful_restart_timeout - timeout of graceful restart 'restart timer'
817
 * @t: timer
818
 *
819
 * This function is a timeout hook for @gr_timer, implementing BGP restart time
820
 * limit for reestablisment of the BGP session after the graceful restart. When
821
 * fired, we just proceed with the usual protocol restart.
822
 */
823

    
824
static void
825
bgp_graceful_restart_timeout(timer *t)
826
{
827
  struct bgp_proto *p = t->data;
828

    
829
  BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout");
830

    
831
  if (p->llgr_ready)
832
  {
833
    struct bgp_channel *c;
834
    WALK_LIST(c, p->p.channels)
835
    {
836
      /* Channel is not in GR and is already flushed */
837
      if (!c->gr_active)
838
        continue;
839

    
840
      /* Channel is already in LLGR from past restart */
841
      if (c->gr_active == BGP_GRS_LLGR)
842
        continue;
843

    
844
      /* Channel is in GR, but does not support LLGR -> stop GR */
845
      if (!c->stale_time)
846
      {
847
        bgp_graceful_restart_done(c);
848
        continue;
849
      }
850

    
851
      /* Channel is in GR, and supports LLGR -> start LLGR */
852
      c->gr_active = BGP_GRS_LLGR;
853
      tm_start(c->stale_timer, c->stale_time S);
854
      rt_modify_stale(c->c.table, &c->c);
855
    }
856
  }
857
  else
858
    bgp_stop(p, 0, NULL, 0);
859
}
860

    
861
static void
862
bgp_long_lived_stale_timeout(timer *t)
863
{
864
  struct bgp_channel *c = t->data;
865
  struct bgp_proto *p = (void *) c->c.proto;
866

    
867
  BGP_TRACE(D_EVENTS, "Long-lived stale timeout");
868

    
869
  bgp_graceful_restart_done(c);
870
}
871

    
872

    
873
/**
874
 * bgp_refresh_begin - start incoming enhanced route refresh sequence
875
 * @c: BGP channel
876
 *
877
 * This function is called when an incoming enhanced route refresh sequence is
878
 * started by the neighbor, demarcated by the BoRR packet. The function updates
879
 * the load state and starts the routing table refresh cycle. Note that graceful
880
 * restart also uses routing table refresh cycle, but RFC 7313 and load states
881
 * ensure that these two sequences do not overlap.
882
 */
883
void
884
bgp_refresh_begin(struct bgp_channel *c)
885
{
886
  struct bgp_proto *p = (void *) c->c.proto;
887

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

    
891
  c->load_state = BFS_REFRESHING;
892
  rt_refresh_begin(c->c.table, &c->c);
893

    
894
  if (c->c.in_table)
895
    rt_refresh_begin(c->c.in_table, &c->c);
896
}
897

    
898
/**
899
 * bgp_refresh_end - finish incoming enhanced route refresh sequence
900
 * @c: BGP channel
901
 *
902
 * This function is called when an incoming enhanced route refresh sequence is
903
 * finished by the neighbor, demarcated by the EoRR packet. The function updates
904
 * the load state and ends the routing table refresh cycle. Routes not received
905
 * during the sequence are removed by the nest.
906
 */
907
void
908
bgp_refresh_end(struct bgp_channel *c)
909
{
910
  struct bgp_proto *p = (void *) c->c.proto;
911

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

    
915
  c->load_state = BFS_NONE;
916
  rt_refresh_end(c->c.table, &c->c);
917

    
918
  if (c->c.in_table)
919
    rt_prune_sync(c->c.in_table, 0);
920
}
921

    
922

    
923
static void
924
bgp_send_open(struct bgp_conn *conn)
925
{
926
  DBG("BGP: Sending open\n");
927
  conn->sk->rx_hook = bgp_rx;
928
  conn->sk->tx_hook = bgp_tx;
929
  tm_stop(conn->connect_timer);
930
  bgp_prepare_capabilities(conn);
931
  bgp_schedule_packet(conn, NULL, PKT_OPEN);
932
  bgp_conn_set_state(conn, BS_OPENSENT);
933
  bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
934
}
935

    
936
static void
937
bgp_connected(sock *sk)
938
{
939
  struct bgp_conn *conn = sk->data;
940
  struct bgp_proto *p = conn->bgp;
941

    
942
  BGP_TRACE(D_EVENTS, "Connected");
943
  bgp_send_open(conn);
944
}
945

    
946
static void
947
bgp_connect_timeout(timer *t)
948
{
949
  struct bgp_conn *conn = t->data;
950
  struct bgp_proto *p = conn->bgp;
951

    
952
  DBG("BGP: connect_timeout\n");
953
  if (p->p.proto_state == PS_START)
954
  {
955
    bgp_close_conn(conn);
956
    bgp_connect(p);
957
  }
958
  else
959
    bgp_conn_enter_idle_state(conn);
960
}
961

    
962
static void
963
bgp_sock_err(sock *sk, int err)
964
{
965
  struct bgp_conn *conn = sk->data;
966
  struct bgp_proto *p = conn->bgp;
967

    
968
  /*
969
   * This error hook may be called either asynchronously from main
970
   * loop, or synchronously from sk_send().  But sk_send() is called
971
   * only from bgp_tx() and bgp_kick_tx(), which are both called
972
   * asynchronously from main loop. Moreover, they end if err hook is
973
   * called. Therefore, we could suppose that it is always called
974
   * asynchronously.
975
   */
976

    
977
  bgp_store_error(p, conn, BE_SOCKET, err);
978

    
979
  if (err)
980
    BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
981
  else
982
    BGP_TRACE(D_EVENTS, "Connection closed");
983

    
984
  if ((conn->state == BS_ESTABLISHED) && p->gr_ready)
985
    bgp_handle_graceful_restart(p);
986

    
987
  bgp_conn_enter_idle_state(conn);
988
}
989

    
990
static void
991
bgp_hold_timeout(timer *t)
992
{
993
  struct bgp_conn *conn = t->data;
994
  struct bgp_proto *p = conn->bgp;
995

    
996
  DBG("BGP: Hold timeout\n");
997

    
998
  /* We are already closing the connection - just do hangup */
999
  if (conn->state == BS_CLOSE)
1000
  {
1001
    BGP_TRACE(D_EVENTS, "Connection stalled");
1002
    bgp_conn_enter_idle_state(conn);
1003
    return;
1004
  }
1005

    
1006
  /* If there is something in input queue, we are probably congested
1007
     and perhaps just not processed BGP packets in time. */
1008

    
1009
  if (sk_rx_ready(conn->sk) > 0)
1010
    bgp_start_timer(conn->hold_timer, 10);
1011
  else if ((conn->state == BS_ESTABLISHED) && p->llgr_ready)
1012
  {
1013
    BGP_TRACE(D_EVENTS, "Hold timer expired");
1014
    bgp_handle_graceful_restart(p);
1015
    bgp_conn_enter_idle_state(conn);
1016
  }
1017
  else
1018
    bgp_error(conn, 4, 0, NULL, 0);
1019
}
1020

    
1021
static void
1022
bgp_keepalive_timeout(timer *t)
1023
{
1024
  struct bgp_conn *conn = t->data;
1025

    
1026
  DBG("BGP: Keepalive timer\n");
1027
  bgp_schedule_packet(conn, NULL, PKT_KEEPALIVE);
1028

    
1029
  /* Kick TX a bit faster */
1030
  if (ev_active(conn->tx_ev))
1031
    ev_run(conn->tx_ev);
1032
}
1033

    
1034
static void
1035
bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
1036
{
1037
  conn->sk = NULL;
1038
  conn->bgp = p;
1039

    
1040
  conn->packets_to_send = 0;
1041
  conn->channels_to_send = 0;
1042
  conn->last_channel = 0;
1043
  conn->last_channel_count = 0;
1044

    
1045
  conn->connect_timer        = tm_new_init(p->p.pool, bgp_connect_timeout,         conn, 0, 0);
1046
  conn->hold_timer         = tm_new_init(p->p.pool, bgp_hold_timeout,         conn, 0, 0);
1047
  conn->keepalive_timer        = tm_new_init(p->p.pool, bgp_keepalive_timeout, conn, 0, 0);
1048

    
1049
  conn->tx_ev = ev_new_init(p->p.pool, bgp_kick_tx, conn);
1050
}
1051

    
1052
static void
1053
bgp_setup_sk(struct bgp_conn *conn, sock *s)
1054
{
1055
  s->data = conn;
1056
  s->err_hook = bgp_sock_err;
1057
  s->fast_rx = 1;
1058
  conn->sk = s;
1059
}
1060

    
1061
static void
1062
bgp_active(struct bgp_proto *p)
1063
{
1064
  int delay = MAX(1, p->cf->connect_delay_time);
1065
  struct bgp_conn *conn = &p->outgoing_conn;
1066

    
1067
  BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
1068
  bgp_setup_conn(p, conn);
1069
  bgp_conn_set_state(conn, BS_ACTIVE);
1070
  bgp_start_timer(conn->connect_timer, delay);
1071
}
1072

    
1073
/**
1074
 * bgp_connect - initiate an outgoing connection
1075
 * @p: BGP instance
1076
 *
1077
 * The bgp_connect() function creates a new &bgp_conn and initiates
1078
 * a TCP connection to the peer. The rest of connection setup is governed
1079
 * by the BGP state machine as described in the standard.
1080
 */
1081
static void
1082
bgp_connect(struct bgp_proto *p)        /* Enter Connect state and start establishing connection */
1083
{
1084
  struct bgp_conn *conn = &p->outgoing_conn;
1085
  int hops = p->cf->multihop ? : 1;
1086

    
1087
  DBG("BGP: Connecting\n");
1088
  sock *s = sk_new(p->p.pool);
1089
  s->type = SK_TCP_ACTIVE;
1090
  s->saddr = p->local_ip;
1091
  s->daddr = p->remote_ip;
1092
  s->dport = p->cf->remote_port;
1093
  s->iface = p->neigh ? p->neigh->iface : NULL;
1094
  s->vrf = p->p.vrf;
1095
  s->ttl = p->cf->ttl_security ? 255 : hops;
1096
  s->rbsize = p->cf->enable_extended_messages ? BGP_RX_BUFFER_EXT_SIZE : BGP_RX_BUFFER_SIZE;
1097
  s->tbsize = p->cf->enable_extended_messages ? BGP_TX_BUFFER_EXT_SIZE : BGP_TX_BUFFER_SIZE;
1098
  s->tos = IP_PREC_INTERNET_CONTROL;
1099
  s->password = p->cf->password;
1100
  s->tx_hook = bgp_connected;
1101
  BGP_TRACE(D_EVENTS, "Connecting to %I%J from local address %I%J",
1102
            s->daddr, ipa_is_link_local(s->daddr) ? p->cf->iface : NULL,
1103
            s->saddr, ipa_is_link_local(s->saddr) ? s->iface : NULL);
1104
  bgp_setup_conn(p, conn);
1105
  bgp_setup_sk(conn, s);
1106
  bgp_conn_set_state(conn, BS_CONNECT);
1107

    
1108
  if (sk_open(s) < 0)
1109
    goto err;
1110

    
1111
  /* Set minimal receive TTL if needed */
1112
  if (p->cf->ttl_security)
1113
    if (sk_set_min_ttl(s, 256 - hops) < 0)
1114
      goto err;
1115

    
1116
  DBG("BGP: Waiting for connect success\n");
1117
  bgp_start_timer(conn->connect_timer, p->cf->connect_retry_time);
1118
  return;
1119

    
1120
err:
1121
  sk_log_error(s, p->p.name);
1122
  bgp_sock_err(s, 0);
1123
  return;
1124
}
1125

    
1126
static inline int bgp_is_dynamic(struct bgp_proto *p)
1127
{ return ipa_zero(p->remote_ip); }
1128

    
1129
/**
1130
 * bgp_find_proto - find existing proto for incoming connection
1131
 * @sk: TCP socket
1132
 *
1133
 */
1134
static struct bgp_proto *
1135
bgp_find_proto(sock *sk)
1136
{
1137
  struct bgp_proto *best = NULL;
1138
  struct bgp_proto *p;
1139

    
1140
  /* sk->iface is valid only if src or dst address is link-local */
1141
  int link = ipa_is_link_local(sk->saddr) || ipa_is_link_local(sk->daddr);
1142

    
1143
  WALK_LIST(p, proto_list)
1144
    if ((p->p.proto == &proto_bgp) &&
1145
        (ipa_equal(p->remote_ip, sk->daddr) || bgp_is_dynamic(p)) &&
1146
        (!p->cf->remote_range || ipa_in_netX(sk->daddr, p->cf->remote_range)) &&
1147
        (p->p.vrf == sk->vrf) &&
1148
        (p->cf->local_port == sk->sport) &&
1149
        (!link || (p->cf->iface == sk->iface)) &&
1150
        (ipa_zero(p->cf->local_ip) || ipa_equal(p->cf->local_ip, sk->saddr)))
1151
    {
1152
      best = p;
1153

    
1154
      if (!bgp_is_dynamic(p))
1155
        break;
1156
    }
1157

    
1158
  return best;
1159
}
1160

    
1161
/**
1162
 * bgp_incoming_connection - handle an incoming connection
1163
 * @sk: TCP socket
1164
 * @dummy: unused
1165
 *
1166
 * This function serves as a socket hook for accepting of new BGP
1167
 * connections. It searches a BGP instance corresponding to the peer
1168
 * which has connected and if such an instance exists, it creates a
1169
 * &bgp_conn structure, attaches it to the instance and either sends
1170
 * an Open message or (if there already is an active connection) it
1171
 * closes the new connection by sending a Notification message.
1172
 */
1173
static int
1174
bgp_incoming_connection(sock *sk, uint dummy UNUSED)
1175
{
1176
  struct bgp_proto *p;
1177
  int acc, hops;
1178

    
1179
  DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
1180
  p = bgp_find_proto(sk);
1181
  if (!p)
1182
  {
1183
    log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)",
1184
        sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, sk->dport);
1185
    rfree(sk);
1186
    return 0;
1187
  }
1188

    
1189
  /*
1190
   * BIRD should keep multiple incoming connections in OpenSent state (for
1191
   * details RFC 4271 8.2.1 par 3), but it keeps just one. Duplicate incoming
1192
   * connections are rejected istead. The exception is the case where an
1193
   * incoming connection triggers a graceful restart.
1194
   */
1195

    
1196
  acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
1197
    (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
1198

    
1199
  if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
1200
  {
1201
    bgp_store_error(p, NULL, BE_MISC, BEM_GRACEFUL_RESTART);
1202
    bgp_handle_graceful_restart(p);
1203
    bgp_conn_enter_idle_state(p->conn);
1204
    acc = 1;
1205

    
1206
    /* There might be separate incoming connection in OpenSent state */
1207
    if (p->incoming_conn.state > BS_ACTIVE)
1208
      bgp_close_conn(&p->incoming_conn);
1209
  }
1210

    
1211
  BGP_TRACE(D_EVENTS, "Incoming connection from %I%J (port %d) %s",
1212
            sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL,
1213
            sk->dport, acc ? "accepted" : "rejected");
1214

    
1215
  if (!acc)
1216
  {
1217
    rfree(sk);
1218
    return 0;
1219
  }
1220

    
1221
  hops = p->cf->multihop ? : 1;
1222

    
1223
  if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0)
1224
    goto err;
1225

    
1226
  if (p->cf->ttl_security)
1227
    if (sk_set_min_ttl(sk, 256 - hops) < 0)
1228
      goto err;
1229

    
1230
  if (p->cf->enable_extended_messages)
1231
  {
1232
    sk->rbsize = BGP_RX_BUFFER_EXT_SIZE;
1233
    sk->tbsize = BGP_TX_BUFFER_EXT_SIZE;
1234
    sk_reallocate(sk);
1235
  }
1236

    
1237
  /* For dynamic BGP, spawn new instance and postpone the socket */
1238
  if (bgp_is_dynamic(p))
1239
  {
1240
    p = bgp_spawn(p, sk->daddr);
1241
    p->postponed_sk = sk;
1242
    rmove(sk, p->p.pool);
1243
    return 0;
1244
  }
1245

    
1246
  rmove(sk, p->p.pool);
1247
  bgp_setup_conn(p, &p->incoming_conn);
1248
  bgp_setup_sk(&p->incoming_conn, sk);
1249
  bgp_send_open(&p->incoming_conn);
1250
  return 0;
1251

    
1252
err:
1253
  sk_log_error(sk, p->p.name);
1254
  log(L_ERR "%s: Incoming connection aborted", p->p.name);
1255
  rfree(sk);
1256
  return 0;
1257
}
1258

    
1259
static void
1260
bgp_listen_sock_err(sock *sk UNUSED, int err)
1261
{
1262
  if (err == ECONNABORTED)
1263
    log(L_WARN "BGP: Incoming connection aborted");
1264
  else
1265
    log(L_ERR "BGP: Error on listening socket: %M", err);
1266
}
1267

    
1268
static void
1269
bgp_start_neighbor(struct bgp_proto *p)
1270
{
1271
  /* Called only for single-hop BGP sessions */
1272

    
1273
  if (ipa_zero(p->local_ip))
1274
    p->local_ip = p->neigh->ifa->ip;
1275

    
1276
  if (ipa_is_link_local(p->local_ip))
1277
    p->link_addr = p->local_ip;
1278
  else if (p->neigh->iface->llv6)
1279
    p->link_addr = p->neigh->iface->llv6->ip;
1280

    
1281
  bgp_initiate(p);
1282
}
1283

    
1284
static void
1285
bgp_neigh_notify(neighbor *n)
1286
{
1287
  struct bgp_proto *p = (struct bgp_proto *) n->proto;
1288
  int ps = p->p.proto_state;
1289

    
1290
  if (n != p->neigh)
1291
    return;
1292

    
1293
  if ((ps == PS_DOWN) || (ps == PS_STOP))
1294
    return;
1295

    
1296
  int prepare = (ps == PS_START) && (p->start_state == BSS_PREPARE);
1297

    
1298
  if (n->scope <= 0)
1299
  {
1300
    if (!prepare)
1301
    {
1302
      BGP_TRACE(D_EVENTS, "Neighbor lost");
1303
      bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST);
1304
      /* Perhaps also run bgp_update_startup_delay(p)? */
1305
      bgp_stop(p, 0, NULL, 0);
1306
    }
1307
  }
1308
  else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1309
  {
1310
    if (!prepare)
1311
    {
1312
      BGP_TRACE(D_EVENTS, "Link down");
1313
      bgp_store_error(p, NULL, BE_MISC, BEM_LINK_DOWN);
1314
      if (ps == PS_UP)
1315
        bgp_update_startup_delay(p);
1316
      bgp_stop(p, 0, NULL, 0);
1317
    }
1318
  }
1319
  else
1320
  {
1321
    if (prepare)
1322
    {
1323
      BGP_TRACE(D_EVENTS, "Neighbor ready");
1324
      bgp_start_neighbor(p);
1325
    }
1326
  }
1327
}
1328

    
1329
static void
1330
bgp_bfd_notify(struct bfd_request *req)
1331
{
1332
  struct bgp_proto *p = req->data;
1333
  int ps = p->p.proto_state;
1334

    
1335
  if (req->down && ((ps == PS_START) || (ps == PS_UP)))
1336
  {
1337
    BGP_TRACE(D_EVENTS, "BFD session down");
1338
    bgp_store_error(p, NULL, BE_MISC, BEM_BFD_DOWN);
1339

    
1340
    if (p->cf->bfd == BGP_BFD_GRACEFUL)
1341
    {
1342
      /* Trigger graceful restart */
1343
      if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
1344
        bgp_handle_graceful_restart(p);
1345

    
1346
      if (p->incoming_conn.state > BS_IDLE)
1347
        bgp_conn_enter_idle_state(&p->incoming_conn);
1348

    
1349
      if (p->outgoing_conn.state > BS_IDLE)
1350
        bgp_conn_enter_idle_state(&p->outgoing_conn);
1351
    }
1352
    else
1353
    {
1354
      /* Trigger session down */
1355
      if (ps == PS_UP)
1356
        bgp_update_startup_delay(p);
1357
      bgp_stop(p, 0, NULL, 0);
1358
    }
1359
  }
1360
}
1361

    
1362
static void
1363
bgp_update_bfd(struct bgp_proto *p, int use_bfd)
1364
{
1365
  if (use_bfd && !p->bfd_req && !bgp_is_dynamic(p))
1366
    p->bfd_req = bfd_request_session(p->p.pool, p->remote_ip, p->local_ip,
1367
                                     p->cf->multihop ? NULL : p->neigh->iface,
1368
                                     bgp_bfd_notify, p);
1369

    
1370
  if (!use_bfd && p->bfd_req)
1371
  {
1372
    rfree(p->bfd_req);
1373
    p->bfd_req = NULL;
1374
  }
1375
}
1376

    
1377
static void
1378
bgp_reload_routes(struct channel *C)
1379
{
1380
  struct bgp_proto *p = (void *) C->proto;
1381
  struct bgp_channel *c = (void *) C;
1382

    
1383
  ASSERT(p->conn && (p->route_refresh || c->c.in_table));
1384

    
1385
  if (c->c.in_table)
1386
    channel_schedule_reload(C);
1387
  else
1388
    bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH);
1389
}
1390

    
1391
static void
1392
bgp_feed_begin(struct channel *C, int initial)
1393
{
1394
  struct bgp_proto *p = (void *) C->proto;
1395
  struct bgp_channel *c = (void *) C;
1396

    
1397
  /* This should not happen */
1398
  if (!p->conn)
1399
    return;
1400

    
1401
  if (initial && p->cf->gr_mode)
1402
    c->feed_state = BFS_LOADING;
1403

    
1404
  /* It is refeed and both sides support enhanced route refresh */
1405
  if (!initial && p->enhanced_refresh)
1406
  {
1407
    /* BoRR must not be sent before End-of-RIB */
1408
    if (c->feed_state == BFS_LOADING || c->feed_state == BFS_LOADED)
1409
      return;
1410

    
1411
    c->feed_state = BFS_REFRESHING;
1412
    bgp_schedule_packet(p->conn, c, PKT_BEGIN_REFRESH);
1413
  }
1414
}
1415

    
1416
static void
1417
bgp_feed_end(struct channel *C)
1418
{
1419
  struct bgp_proto *p = (void *) C->proto;
1420
  struct bgp_channel *c = (void *) C;
1421

    
1422
  /* This should not happen */
1423
  if (!p->conn)
1424
    return;
1425

    
1426
  /* Non-demarcated feed ended, nothing to do */
1427
  if (c->feed_state == BFS_NONE)
1428
    return;
1429

    
1430
  /* Schedule End-of-RIB packet */
1431
  if (c->feed_state == BFS_LOADING)
1432
    c->feed_state = BFS_LOADED;
1433

    
1434
  /* Schedule EoRR packet */
1435
  if (c->feed_state == BFS_REFRESHING)
1436
    c->feed_state = BFS_REFRESHED;
1437

    
1438
  /* Kick TX hook */
1439
  bgp_schedule_packet(p->conn, c, PKT_UPDATE);
1440
}
1441

    
1442

    
1443
static void
1444
bgp_start_locked(struct object_lock *lock)
1445
{
1446
  struct bgp_proto *p = lock->data;
1447
  const struct bgp_config *cf = p->cf;
1448

    
1449
  if (p->p.proto_state != PS_START)
1450
  {
1451
    DBG("BGP: Got lock in different state %d\n", p->p.proto_state);
1452
    return;
1453
  }
1454

    
1455
  DBG("BGP: Got lock\n");
1456

    
1457
  if (cf->multihop || bgp_is_dynamic(p))
1458
  {
1459
    /* Multi-hop sessions do not use neighbor entries */
1460
    bgp_initiate(p);
1461
    return;
1462
  }
1463

    
1464
  neighbor *n = neigh_find(&p->p, p->remote_ip, cf->iface, NEF_STICKY);
1465
  if (!n)
1466
  {
1467
    log(L_ERR "%s: Invalid remote address %I%J", p->p.name, p->remote_ip, cf->iface);
1468
    /* As we do not start yet, we can just disable protocol */
1469
    p->p.disabled = 1;
1470
    bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_NEXT_HOP);
1471
    proto_notify_state(&p->p, PS_DOWN);
1472
    return;
1473
  }
1474

    
1475
  p->neigh = n;
1476

    
1477
  if (n->scope <= 0)
1478
    BGP_TRACE(D_EVENTS, "Waiting for %I%J to become my neighbor", p->remote_ip, cf->iface);
1479
  else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1480
    BGP_TRACE(D_EVENTS, "Waiting for link on %s", n->iface->name);
1481
  else
1482
    bgp_start_neighbor(p);
1483
}
1484

    
1485
static int
1486
bgp_start(struct proto *P)
1487
{
1488
  struct bgp_proto *p = (struct bgp_proto *) P;
1489
  const struct bgp_config *cf = p->cf;
1490

    
1491
  p->local_ip = cf->local_ip;
1492
  p->local_as = cf->local_as;
1493
  p->remote_as = cf->remote_as;
1494
  p->public_as = cf->local_as;
1495

    
1496
  /* For dynamic BGP childs, remote_ip is already set */
1497
  if (ipa_nonzero(cf->remote_ip))
1498
    p->remote_ip = cf->remote_ip;
1499

    
1500
  /* Confederation ID is used for truly external peers */
1501
  if (p->cf->confederation && !p->is_interior)
1502
    p->public_as = cf->confederation;
1503

    
1504
  p->passive = cf->passive || bgp_is_dynamic(p);
1505

    
1506
  p->start_state = BSS_PREPARE;
1507
  p->outgoing_conn.state = BS_IDLE;
1508
  p->incoming_conn.state = BS_IDLE;
1509
  p->neigh = NULL;
1510
  p->bfd_req = NULL;
1511
  p->postponed_sk = NULL;
1512
  p->gr_ready = 0;
1513
  p->gr_active_num = 0;
1514

    
1515
  p->event = ev_new_init(p->p.pool, bgp_decision, p);
1516
  p->startup_timer = tm_new_init(p->p.pool, bgp_startup_timeout, p, 0, 0);
1517
  p->gr_timer = tm_new_init(p->p.pool, bgp_graceful_restart_timeout, p, 0, 0);
1518

    
1519
  p->local_id = proto_get_router_id(P->cf);
1520
  if (p->rr_client)
1521
    p->rr_cluster_id = p->cf->rr_cluster_id ? p->cf->rr_cluster_id : p->local_id;
1522

    
1523
  p->remote_id = 0;
1524
  p->link_addr = IPA_NONE;
1525

    
1526
  /* Lock all channels when in GR recovery mode */
1527
  if (p->p.gr_recovery && p->cf->gr_mode)
1528
  {
1529
    struct bgp_channel *c;
1530
    WALK_LIST(c, p->p.channels)
1531
      channel_graceful_restart_lock(&c->c);
1532
  }
1533

    
1534
  /*
1535
   * Before attempting to create the connection, we need to lock the port,
1536
   * so that we are the only instance attempting to talk with that neighbor.
1537
   */
1538
  struct object_lock *lock;
1539
  lock = p->lock = olock_new(P->pool);
1540
  lock->addr = p->remote_ip;
1541
  lock->port = p->cf->remote_port;
1542
  lock->iface = p->cf->iface;
1543
  lock->vrf = p->cf->iface ? NULL : p->p.vrf;
1544
  lock->type = OBJLOCK_TCP;
1545
  lock->hook = bgp_start_locked;
1546
  lock->data = p;
1547
  olock_acquire(lock);
1548

    
1549
  return PS_START;
1550
}
1551

    
1552
extern int proto_restart;
1553

    
1554
static int
1555
bgp_shutdown(struct proto *P)
1556
{
1557
  struct bgp_proto *p = (struct bgp_proto *) P;
1558
  uint subcode = 0;
1559

    
1560
  char *message = NULL;
1561
  byte *data = NULL;
1562
  uint len = 0;
1563

    
1564
  BGP_TRACE(D_EVENTS, "Shutdown requested");
1565

    
1566
  switch (P->down_code)
1567
  {
1568
  case PDC_CF_REMOVE:
1569
  case PDC_CF_DISABLE:
1570
    subcode = 3; // Errcode 6, 3 - peer de-configured
1571
    break;
1572

    
1573
  case PDC_CF_RESTART:
1574
    subcode = 6; // Errcode 6, 6 - other configuration change
1575
    break;
1576

    
1577
  case PDC_CMD_DISABLE:
1578
  case PDC_CMD_SHUTDOWN:
1579
    subcode = 2; // Errcode 6, 2 - administrative shutdown
1580
    message = P->message;
1581
    break;
1582

    
1583
  case PDC_CMD_RESTART:
1584
    subcode = 4; // Errcode 6, 4 - administrative reset
1585
    message = P->message;
1586
    break;
1587

    
1588
  case PDC_RX_LIMIT_HIT:
1589
  case PDC_IN_LIMIT_HIT:
1590
    subcode = 1; // Errcode 6, 1 - max number of prefixes reached
1591
    /* log message for compatibility */
1592
    log(L_WARN "%s: Route limit exceeded, shutting down", p->p.name);
1593
    goto limit;
1594

    
1595
  case PDC_OUT_LIMIT_HIT:
1596
    subcode = proto_restart ? 4 : 2; // Administrative reset or shutdown
1597

    
1598
  limit:
1599
    bgp_store_error(p, NULL, BE_AUTO_DOWN, BEA_ROUTE_LIMIT_EXCEEDED);
1600
    if (proto_restart)
1601
      bgp_update_startup_delay(p);
1602
    else
1603
      p->startup_delay = 0;
1604
    goto done;
1605
  }
1606

    
1607
  bgp_store_error(p, NULL, BE_MAN_DOWN, 0);
1608
  p->startup_delay = 0;
1609

    
1610
  /* RFC 8203 - shutdown communication */
1611
  if (message)
1612
  {
1613
    uint msg_len = strlen(message);
1614
    msg_len = MIN(msg_len, 255);
1615

    
1616
    /* Buffer will be freed automatically by protocol shutdown */
1617
    data = mb_alloc(p->p.pool, msg_len + 1);
1618
    len = msg_len + 1;
1619

    
1620
    data[0] = msg_len;
1621
    memcpy(data+1, message, msg_len);
1622
  }
1623

    
1624
done:
1625
  bgp_stop(p, subcode, data, len);
1626
  return p->p.proto_state;
1627
}
1628

    
1629
static struct proto *
1630
bgp_init(struct proto_config *CF)
1631
{
1632
  struct proto *P = proto_new(CF);
1633
  struct bgp_proto *p = (struct bgp_proto *) P;
1634
  struct bgp_config *cf = (struct bgp_config *) CF;
1635

    
1636
  P->rt_notify = bgp_rt_notify;
1637
  P->preexport = bgp_preexport;
1638
  P->neigh_notify = bgp_neigh_notify;
1639
  P->reload_routes = bgp_reload_routes;
1640
  P->feed_begin = bgp_feed_begin;
1641
  P->feed_end = bgp_feed_end;
1642
  P->rte_better = bgp_rte_better;
1643
  P->rte_mergable = bgp_rte_mergable;
1644
  P->rte_recalculate = cf->deterministic_med ? bgp_rte_recalculate : NULL;
1645
  P->rte_modify = bgp_rte_modify_stale;
1646

    
1647
  p->cf = cf;
1648
  p->is_internal = (cf->local_as == cf->remote_as);
1649
  p->is_interior = p->is_internal || cf->confederation_member;
1650
  p->rs_client = cf->rs_client;
1651
  p->rr_client = cf->rr_client;
1652

    
1653
  p->ipv4 = ipa_nonzero(cf->remote_ip) ?
1654
    ipa_is_ip4(cf->remote_ip) :
1655
    (cf->remote_range && (cf->remote_range->type == NET_IP4));
1656

    
1657
  p->remote_ip = cf->remote_ip;
1658
  p->remote_as = cf->remote_as;
1659

    
1660
  /* Hack: We use cf->remote_ip just to pass remote_ip from bgp_spawn() */
1661
  if (cf->c.parent)
1662
    cf->remote_ip = IPA_NONE;
1663

    
1664
  /* Add all channels */
1665
  struct bgp_channel_config *cc;
1666
  WALK_LIST(cc, CF->channels)
1667
    proto_add_channel(P, &cc->c);
1668

    
1669
  return P;
1670
}
1671

    
1672
static void
1673
bgp_channel_init(struct channel *C, struct channel_config *CF)
1674
{
1675
  struct bgp_channel *c = (void *) C;
1676
  struct bgp_channel_config *cf = (void *) CF;
1677

    
1678
  c->cf = cf;
1679
  c->afi = cf->afi;
1680
  c->desc = cf->desc;
1681

    
1682
  if (cf->igp_table_ip4)
1683
    c->igp_table_ip4 = cf->igp_table_ip4->table;
1684

    
1685
  if (cf->igp_table_ip6)
1686
    c->igp_table_ip6 = cf->igp_table_ip6->table;
1687
}
1688

    
1689
static int
1690
bgp_channel_start(struct channel *C)
1691
{
1692
  struct bgp_proto *p = (void *) C->proto;
1693
  struct bgp_channel *c = (void *) C;
1694
  ip_addr src = p->local_ip;
1695

    
1696
  if (c->igp_table_ip4)
1697
    rt_lock_table(c->igp_table_ip4);
1698

    
1699
  if (c->igp_table_ip6)
1700
    rt_lock_table(c->igp_table_ip6);
1701

    
1702
  c->pool = p->p.pool; // XXXX
1703
  bgp_init_bucket_table(c);
1704
  bgp_init_prefix_table(c);
1705

    
1706
  if (c->cf->import_table)
1707
    channel_setup_in_table(C);
1708

    
1709
  c->stale_timer = tm_new_init(c->pool, bgp_long_lived_stale_timeout, c, 0, 0);
1710

    
1711
  c->next_hop_addr = c->cf->next_hop_addr;
1712
  c->link_addr = IPA_NONE;
1713
  c->packets_to_send = 0;
1714

    
1715
  /* Try to use source address as next hop address */
1716
  if (ipa_zero(c->next_hop_addr))
1717
  {
1718
    if (bgp_channel_is_ipv4(c) && (ipa_is_ip4(src) || c->ext_next_hop))
1719
      c->next_hop_addr = src;
1720

    
1721
    if (bgp_channel_is_ipv6(c) && (ipa_is_ip6(src) || c->ext_next_hop))
1722
      c->next_hop_addr = src;
1723
  }
1724

    
1725
  /* Use preferred addresses associated with interface / source address */
1726
  if (ipa_zero(c->next_hop_addr))
1727
  {
1728
    /* We know the iface for single-hop, we make lookup for multihop */
1729
    struct neighbor *nbr = p->neigh ?: neigh_find(&p->p, src, NULL, 0);
1730
    struct iface *iface = nbr ? nbr->iface : NULL;
1731

    
1732
    if (bgp_channel_is_ipv4(c) && iface && iface->addr4)
1733
      c->next_hop_addr = iface->addr4->ip;
1734

    
1735
    if (bgp_channel_is_ipv6(c) && iface && iface->addr6)
1736
      c->next_hop_addr = iface->addr6->ip;
1737
  }
1738

    
1739
  /* Exit if no feasible next hop address is found */
1740
  if (ipa_zero(c->next_hop_addr))
1741
  {
1742
    log(L_WARN "%s: Missing next hop address", p->p.name);
1743
    return 0;
1744
  }
1745

    
1746
  /* Set link-local address for IPv6 single-hop BGP */
1747
  if (ipa_is_ip6(c->next_hop_addr) && p->neigh)
1748
  {
1749
    c->link_addr = p->link_addr;
1750

    
1751
    if (ipa_zero(c->link_addr))
1752
      log(L_WARN "%s: Missing link-local address", p->p.name);
1753
  }
1754

    
1755
  /* Link local address is already in c->link_addr */
1756
  if (ipa_is_link_local(c->next_hop_addr))
1757
    c->next_hop_addr = IPA_NONE;
1758

    
1759
  return 0; /* XXXX: Currently undefined */
1760
}
1761

    
1762
static void
1763
bgp_channel_shutdown(struct channel *C)
1764
{
1765
  struct bgp_channel *c = (void *) C;
1766

    
1767
  c->next_hop_addr = IPA_NONE;
1768
  c->link_addr = IPA_NONE;
1769
  c->packets_to_send = 0;
1770
}
1771

    
1772
static void
1773
bgp_channel_cleanup(struct channel *C)
1774
{
1775
  struct bgp_channel *c = (void *) C;
1776

    
1777
  if (c->igp_table_ip4)
1778
    rt_unlock_table(c->igp_table_ip4);
1779

    
1780
  if (c->igp_table_ip6)
1781
    rt_unlock_table(c->igp_table_ip6);
1782

    
1783
  c->index = 0;
1784

    
1785
  /* Cleanup rest of bgp_channel starting at pool field */
1786
  memset(&(c->pool), 0, sizeof(struct bgp_channel) - OFFSETOF(struct bgp_channel, pool));
1787
}
1788

    
1789
static inline struct bgp_channel_config *
1790
bgp_find_channel_config(struct bgp_config *cf, u32 afi)
1791
{
1792
  struct bgp_channel_config *cc;
1793

    
1794
  WALK_LIST(cc, cf->c.channels)
1795
    if (cc->afi == afi)
1796
      return cc;
1797

    
1798
  return NULL;
1799
}
1800

    
1801
struct rtable_config *
1802
bgp_default_igp_table(struct bgp_config *cf, struct bgp_channel_config *cc, u32 type)
1803
{
1804
  struct bgp_channel_config *cc2;
1805
  struct rtable_config *tab;
1806

    
1807
  /* First, try table connected by the channel */
1808
  if (cc->c.table->addr_type == type)
1809
    return cc->c.table;
1810

    
1811
  /* Find paired channel with the same SAFI but the other AFI */
1812
  u32 afi2 = cc->afi ^ 0x30000;
1813
  cc2 = bgp_find_channel_config(cf, afi2);
1814

    
1815
  /* Second, try IGP table configured in the paired channel */
1816
  if (cc2 && (tab = (type == NET_IP4) ? cc2->igp_table_ip4 : cc2->igp_table_ip6))
1817
    return tab;
1818

    
1819
  /* Third, try table connected by the paired channel */
1820
  if (cc2 && (cc2->c.table->addr_type == type))
1821
    return cc2->c.table;
1822

    
1823
  /* Last, try default table of given type */
1824
  if (tab = cf->c.global->def_tables[type])
1825
    return tab;
1826

    
1827
  cf_error("Undefined IGP table");
1828
}
1829

    
1830

    
1831
void
1832
bgp_postconfig(struct proto_config *CF)
1833
{
1834
  struct bgp_config *cf = (void *) CF;
1835

    
1836
  /* Do not check templates at all */
1837
  if (cf->c.class == SYM_TEMPLATE)
1838
    return;
1839

    
1840

    
1841
  /* Handle undefined remote_as, zero should mean unspecified external */
1842
  if (!cf->remote_as && (cf->peer_type == BGP_PT_INTERNAL))
1843
    cf->remote_as = cf->local_as;
1844

    
1845
  int internal = (cf->local_as == cf->remote_as);
1846
  int interior = internal || cf->confederation_member;
1847

    
1848
  /* EBGP direct by default, IBGP multihop by default */
1849
  if (cf->multihop < 0)
1850
    cf->multihop = internal ? 64 : 0;
1851

    
1852
  /* LLGR mode default based on GR mode */
1853
  if (cf->llgr_mode < 0)
1854
    cf->llgr_mode = cf->gr_mode ? BGP_LLGR_AWARE : 0;
1855

    
1856
  /* Link check for single-hop BGP by default */
1857
  if (cf->check_link < 0)
1858
    cf->check_link = !cf->multihop;
1859

    
1860

    
1861
  if (!cf->local_as)
1862
    cf_error("Local AS number must be set");
1863

    
1864
  if (ipa_zero(cf->remote_ip) && !cf->remote_range)
1865
    cf_error("Neighbor must be configured");
1866

    
1867
  if (ipa_zero(cf->local_ip) && cf->strict_bind)
1868
    cf_error("Local address must be configured for strict bind");
1869

    
1870
  if (!cf->remote_as && !cf->peer_type)
1871
    cf_error("Remote AS number (or peer type) must be set");
1872

    
1873
  if ((cf->peer_type == BGP_PT_INTERNAL) && !internal)
1874
    cf_error("IBGP cannot have different ASNs");
1875

    
1876
  if ((cf->peer_type == BGP_PT_EXTERNAL) &&  internal)
1877
    cf_error("EBGP cannot have the same ASNs");
1878

    
1879
  if (!cf->iface && (ipa_is_link_local(cf->local_ip) ||
1880
                     ipa_is_link_local(cf->remote_ip)))
1881
    cf_error("Link-local addresses require defined interface");
1882

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

    
1886
  if (!internal && cf->rr_client)
1887
    cf_error("Only internal neighbor can be RR client");
1888

    
1889
  if (internal && cf->rs_client)
1890
    cf_error("Only external neighbor can be RS client");
1891

    
1892
  if (!cf->confederation && cf->confederation_member)
1893
    cf_error("Confederation ID must be set for member sessions");
1894

    
1895
  if (cf->multihop && (ipa_is_link_local(cf->local_ip) ||
1896
                       ipa_is_link_local(cf->remote_ip)))
1897
    cf_error("Multihop BGP cannot be used with link-local addresses");
1898

    
1899
  if (cf->multihop && cf->iface)
1900
    cf_error("Multihop BGP cannot be bound to interface");
1901

    
1902
  if (cf->multihop && cf->check_link)
1903
    cf_error("Multihop BGP cannot depend on link state");
1904

    
1905
  if (cf->multihop && cf->bfd && ipa_zero(cf->local_ip))
1906
    cf_error("Multihop BGP with BFD requires specified local address");
1907

    
1908
  if (!cf->gr_mode && cf->llgr_mode)
1909
    cf_error("Long-lived graceful restart requires basic graceful restart");
1910

    
1911

    
1912
  struct bgp_channel_config *cc;
1913
  WALK_LIST(cc, CF->channels)
1914
  {
1915
    /* Handle undefined import filter */
1916
    if (cc->c.in_filter == FILTER_UNDEF)
1917
      if (interior)
1918
        cc->c.in_filter = FILTER_ACCEPT;
1919
      else
1920
        cf_error("EBGP requires explicit import policy");
1921

    
1922
    /* Handle undefined export filter */
1923
    if (cc->c.out_filter == FILTER_UNDEF)
1924
      if (interior)
1925
        cc->c.out_filter = FILTER_REJECT;
1926
      else
1927
        cf_error("EBGP requires explicit export policy");
1928

    
1929
    /* Disable after error incompatible with restart limit action */
1930
    if ((cc->c.in_limit.action == PLA_RESTART) && cf->disable_after_error)
1931
      cc->c.in_limit.action = PLA_DISABLE;
1932

    
1933
    /* Different default based on rr_client, rs_client */
1934
    if (cc->next_hop_keep == 0xff)
1935
      cc->next_hop_keep = cf->rr_client ? NH_IBGP : (cf->rs_client ? NH_ALL : NH_NO);
1936

    
1937
    /* Different default based on rs_client */
1938
    if (!cc->missing_lladdr)
1939
      cc->missing_lladdr = cf->rs_client ? MLL_IGNORE : MLL_SELF;
1940

    
1941
    /* Different default for gw_mode */
1942
    if (!cc->gw_mode)
1943
      cc->gw_mode = cf->multihop ? GW_RECURSIVE : GW_DIRECT;
1944

    
1945
    /* Defaults based on proto config */
1946
    if (cc->gr_able == 0xff)
1947
      cc->gr_able = (cf->gr_mode == BGP_GR_ABLE);
1948

    
1949
    if (cc->llgr_able == 0xff)
1950
      cc->llgr_able = (cf->llgr_mode == BGP_LLGR_ABLE);
1951

    
1952
    if (cc->llgr_time == ~0U)
1953
      cc->llgr_time = cf->llgr_time;
1954

    
1955
    /* Default values of IGP tables */
1956
    if ((cc->gw_mode == GW_RECURSIVE) && !cc->desc->no_igp)
1957
    {
1958
      if (!cc->igp_table_ip4 && (bgp_cc_is_ipv4(cc) || cc->ext_next_hop))
1959
        cc->igp_table_ip4 = bgp_default_igp_table(cf, cc, NET_IP4);
1960

    
1961
      if (!cc->igp_table_ip6 && (bgp_cc_is_ipv6(cc) || cc->ext_next_hop))
1962
        cc->igp_table_ip6 = bgp_default_igp_table(cf, cc, NET_IP6);
1963

    
1964
      if (cc->igp_table_ip4 && bgp_cc_is_ipv6(cc) && !cc->ext_next_hop)
1965
        cf_error("Mismatched IGP table type");
1966

    
1967
      if (cc->igp_table_ip6 && bgp_cc_is_ipv4(cc) && !cc->ext_next_hop)
1968
        cf_error("Mismatched IGP table type");
1969
    }
1970

    
1971
    if (cf->multihop && (cc->gw_mode == GW_DIRECT))
1972
      cf_error("Multihop BGP cannot use direct gateway mode");
1973

    
1974
    if ((cc->gw_mode == GW_RECURSIVE) && cc->c.table->sorted)
1975
      cf_error("BGP in recursive mode prohibits sorted table");
1976

    
1977
    if (cf->deterministic_med && cc->c.table->sorted)
1978
      cf_error("BGP with deterministic MED prohibits sorted table");
1979

    
1980
    if (cc->secondary && !cc->c.table->sorted)
1981
      cf_error("BGP with secondary option requires sorted table");
1982
  }
1983
}
1984

    
1985
static int
1986
bgp_reconfigure(struct proto *P, struct proto_config *CF)
1987
{
1988
  struct bgp_proto *p = (void *) P;
1989
  const struct bgp_config *new = (void *) CF;
1990
  const struct bgp_config *old = p->cf;
1991

    
1992
  if (proto_get_router_id(CF) != p->local_id)
1993
    return 0;
1994

    
1995
  int same = !memcmp(((byte *) old) + sizeof(struct proto_config),
1996
                     ((byte *) new) + sizeof(struct proto_config),
1997
                     // password item is last and must be checked separately
1998
                     OFFSETOF(struct bgp_config, password) - sizeof(struct proto_config))
1999
    && ((!old->password && !new->password)
2000
        || (old->password && new->password && !strcmp(old->password, new->password)))
2001
    && net_equal(old->remote_range, new->remote_range)
2002
    && !strcmp(old->dynamic_name, new->dynamic_name)
2003
    && (old->dynamic_name_digits == new->dynamic_name_digits);
2004

    
2005
  /* FIXME: Move channel reconfiguration to generic protocol code ? */
2006
  struct channel *C, *C2;
2007
  struct bgp_channel_config *cc;
2008

    
2009
  WALK_LIST(C, p->p.channels)
2010
    C->stale = 1;
2011

    
2012
  WALK_LIST(cc, new->c.channels)
2013
  {
2014
    C = (struct channel *) bgp_find_channel(p, cc->afi);
2015
    same = proto_configure_channel(P, &C, &cc->c) && same;
2016

    
2017
    if (C)
2018
      C->stale = 0;
2019
  }
2020

    
2021
  WALK_LIST_DELSAFE(C, C2, p->p.channels)
2022
    if (C->stale)
2023
      same = proto_configure_channel(P, &C, NULL) && same;
2024

    
2025

    
2026
  if (same && (p->start_state > BSS_PREPARE))
2027
    bgp_update_bfd(p, new->bfd);
2028

    
2029
  /* We should update our copy of configuration ptr as old configuration will be freed */
2030
  if (same)
2031
    p->cf = new;
2032

    
2033
  /* Reset name counter */
2034
  p->dynamic_name_counter = 0;
2035

    
2036
  return same;
2037
}
2038

    
2039
#define IGP_TABLE(cf, sym) ((cf)->igp_table_##sym ? (cf)->igp_table_##sym ->table : NULL )
2040

    
2041
static int
2042
bgp_channel_reconfigure(struct channel *C, struct channel_config *CC)
2043
{
2044
  struct bgp_channel *c = (void *) C;
2045
  struct bgp_channel_config *new = (void *) CC;
2046
  struct bgp_channel_config *old = c->cf;
2047

    
2048
  if (memcmp(((byte *) old) + sizeof(struct channel_config),
2049
             ((byte *) new) + sizeof(struct channel_config),
2050
             /* Remaining items must be checked separately */
2051
             OFFSETOF(struct bgp_channel_config, rest) - sizeof(struct channel_config)))
2052
    return 0;
2053

    
2054
  /* Check change in IGP tables */
2055
  if ((IGP_TABLE(old, ip4) != IGP_TABLE(new, ip4)) ||
2056
      (IGP_TABLE(old, ip6) != IGP_TABLE(new, ip6)))
2057
    return 0;
2058

    
2059
  c->cf = new;
2060
  return 1;
2061
}
2062

    
2063
static void
2064
bgp_copy_config(struct proto_config *dest UNUSED, struct proto_config *src UNUSED)
2065
{
2066
  /* Just a shallow copy */
2067
}
2068

    
2069

    
2070
/**
2071
 * bgp_error - report a protocol error
2072
 * @c: connection
2073
 * @code: error code (according to the RFC)
2074
 * @subcode: error sub-code
2075
 * @data: data to be passed in the Notification message
2076
 * @len: length of the data
2077
 *
2078
 * bgp_error() sends a notification packet to tell the other side that a protocol
2079
 * error has occurred (including the data considered erroneous if possible) and
2080
 * closes the connection.
2081
 */
2082
void
2083
bgp_error(struct bgp_conn *c, uint code, uint subcode, byte *data, int len)
2084
{
2085
  struct bgp_proto *p = c->bgp;
2086

    
2087
  if (c->state == BS_CLOSE)
2088
    return;
2089

    
2090
  bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, data, ABS(len));
2091
  bgp_store_error(p, c, BE_BGP_TX, (code << 16) | subcode);
2092
  bgp_conn_enter_close_state(c);
2093

    
2094
  c->notify_code = code;
2095
  c->notify_subcode = subcode;
2096
  c->notify_data = data;
2097
  c->notify_size = (len > 0) ? len : 0;
2098
  bgp_schedule_packet(c, NULL, PKT_NOTIFICATION);
2099

    
2100
  if (code != 6)
2101
  {
2102
    bgp_update_startup_delay(p);
2103
    bgp_stop(p, 0, NULL, 0);
2104
  }
2105
}
2106

    
2107
/**
2108
 * bgp_store_error - store last error for status report
2109
 * @p: BGP instance
2110
 * @c: connection
2111
 * @class: error class (BE_xxx constants)
2112
 * @code: error code (class specific)
2113
 *
2114
 * bgp_store_error() decides whether given error is interesting enough
2115
 * and store that error to last_error variables of @p
2116
 */
2117
void
2118
bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code)
2119
{
2120
  /* During PS_UP, we ignore errors on secondary connection */
2121
  if ((p->p.proto_state == PS_UP) && c && (c != p->conn))
2122
    return;
2123

    
2124
  /* During PS_STOP, we ignore any errors, as we want to report
2125
   * the error that caused transition to PS_STOP
2126
   */
2127
  if (p->p.proto_state == PS_STOP)
2128
    return;
2129

    
2130
  p->last_error_class = class;
2131
  p->last_error_code = code;
2132
}
2133

    
2134
static char *bgp_state_names[] = { "Idle", "Connect", "Active", "OpenSent", "OpenConfirm", "Established", "Close" };
2135
static char *bgp_err_classes[] = { "", "Error: ", "Socket: ", "Received: ", "BGP Error: ", "Automatic shutdown: ", ""};
2136
static char *bgp_misc_errors[] = { "", "Neighbor lost", "Invalid next hop", "Kernel MD5 auth failed", "No listening socket", "Link down", "BFD session down", "Graceful restart"};
2137
static char *bgp_auto_errors[] = { "", "Route limit exceeded"};
2138
static char *bgp_gr_states[] = { "None", "Regular", "Long-lived"};
2139

    
2140
static const char *
2141
bgp_last_errmsg(struct bgp_proto *p)
2142
{
2143
  switch (p->last_error_class)
2144
  {
2145
  case BE_MISC:
2146
    return bgp_misc_errors[p->last_error_code];
2147
  case BE_SOCKET:
2148
    return (p->last_error_code == 0) ? "Connection closed" : strerror(p->last_error_code);
2149
  case BE_BGP_RX:
2150
  case BE_BGP_TX:
2151
    return bgp_error_dsc(p->last_error_code >> 16, p->last_error_code & 0xFF);
2152
  case BE_AUTO_DOWN:
2153
    return bgp_auto_errors[p->last_error_code];
2154
  default:
2155
    return "";
2156
  }
2157
}
2158

    
2159
static const char *
2160
bgp_state_dsc(struct bgp_proto *p)
2161
{
2162
  if (p->p.proto_state == PS_DOWN)
2163
    return "Down";
2164

    
2165
  int state = MAX(p->incoming_conn.state, p->outgoing_conn.state);
2166
  if ((state == BS_IDLE) && (p->start_state >= BSS_CONNECT) && p->passive)
2167
    return "Passive";
2168

    
2169
  return bgp_state_names[state];
2170
}
2171

    
2172
static void
2173
bgp_get_status(struct proto *P, byte *buf)
2174
{
2175
  struct bgp_proto *p = (struct bgp_proto *) P;
2176

    
2177
  const char *err1 = bgp_err_classes[p->last_error_class];
2178
  const char *err2 = bgp_last_errmsg(p);
2179

    
2180
  if (P->proto_state == PS_DOWN)
2181
    bsprintf(buf, "%s%s", err1, err2);
2182
  else
2183
    bsprintf(buf, "%-14s%s%s", bgp_state_dsc(p), err1, err2);
2184
}
2185

    
2186
static void
2187
bgp_show_afis(int code, char *s, u32 *afis, uint count)
2188
{
2189
  buffer b;
2190
  LOG_BUFFER_INIT(b);
2191

    
2192
  buffer_puts(&b, s);
2193

    
2194
  for (u32 *af = afis; af < (afis + count); af++)
2195
  {
2196
    const struct bgp_af_desc *desc = bgp_get_af_desc(*af);
2197
    if (desc)
2198
      buffer_print(&b, " %s", desc->name);
2199
    else
2200
      buffer_print(&b, " <%u/%u>", BGP_AFI(*af), BGP_SAFI(*af));
2201
  }
2202

    
2203
  if (b.pos == b.end)
2204
    strcpy(b.end - 32, " ... <too long>");
2205

    
2206
  cli_msg(code, b.start);
2207
}
2208

    
2209
static void
2210
bgp_show_capabilities(struct bgp_proto *p UNUSED, struct bgp_caps *caps)
2211
{
2212
  struct bgp_af_caps *ac;
2213
  uint any_mp_bgp = 0;
2214
  uint any_gr_able = 0;
2215
  uint any_add_path = 0;
2216
  uint any_ext_next_hop = 0;
2217
  uint any_llgr_able = 0;
2218
  u32 *afl1 = alloca(caps->af_count * sizeof(u32));
2219
  u32 *afl2 = alloca(caps->af_count * sizeof(u32));
2220
  uint afn1, afn2;
2221

    
2222
  WALK_AF_CAPS(caps, ac)
2223
  {
2224
    any_mp_bgp |= ac->ready;
2225
    any_gr_able |= ac->gr_able;
2226
    any_add_path |= ac->add_path;
2227
    any_ext_next_hop |= ac->ext_next_hop;
2228
    any_llgr_able |= ac->llgr_able;
2229
  }
2230

    
2231
  if (any_mp_bgp)
2232
  {
2233
    cli_msg(-1006, "      Multiprotocol");
2234

    
2235
    afn1 = 0;
2236
    WALK_AF_CAPS(caps, ac)
2237
      if (ac->ready)
2238
        afl1[afn1++] = ac->afi;
2239

    
2240
    bgp_show_afis(-1006, "        AF announced:", afl1, afn1);
2241
  }
2242

    
2243
  if (caps->route_refresh)
2244
    cli_msg(-1006, "      Route refresh");
2245

    
2246
  if (any_ext_next_hop)
2247
  {
2248
    cli_msg(-1006, "      Extended next hop");
2249

    
2250
    afn1 = 0;
2251
    WALK_AF_CAPS(caps, ac)
2252
      if (ac->ext_next_hop)
2253
        afl1[afn1++] = ac->afi;
2254

    
2255
    bgp_show_afis(-1006, "        IPv6 nexthop:", afl1, afn1);
2256
  }
2257

    
2258
  if (caps->ext_messages)
2259
    cli_msg(-1006, "      Extended message");
2260

    
2261
  if (caps->gr_aware)
2262
    cli_msg(-1006, "      Graceful restart");
2263

    
2264
  if (any_gr_able)
2265
  {
2266
    /* Continues from gr_aware */
2267
    cli_msg(-1006, "        Restart time: %u", caps->gr_time);
2268
    if (caps->gr_flags & BGP_GRF_RESTART)
2269
      cli_msg(-1006, "        Restart recovery");
2270

    
2271
    afn1 = afn2 = 0;
2272
    WALK_AF_CAPS(caps, ac)
2273
    {
2274
      if (ac->gr_able)
2275
        afl1[afn1++] = ac->afi;
2276

    
2277
      if (ac->gr_af_flags & BGP_GRF_FORWARDING)
2278
        afl2[afn2++] = ac->afi;
2279
    }
2280

    
2281
    bgp_show_afis(-1006, "        AF supported:", afl1, afn1);
2282
    bgp_show_afis(-1006, "        AF preserved:", afl2, afn2);
2283
  }
2284

    
2285
  if (caps->as4_support)
2286
    cli_msg(-1006, "      4-octet AS numbers");
2287

    
2288
  if (any_add_path)
2289
  {
2290
    cli_msg(-1006, "      ADD-PATH");
2291

    
2292
    afn1 = afn2 = 0;
2293
    WALK_AF_CAPS(caps, ac)
2294
    {
2295
      if (ac->add_path & BGP_ADD_PATH_RX)
2296
        afl1[afn1++] = ac->afi;
2297

    
2298
      if (ac->add_path & BGP_ADD_PATH_TX)
2299
        afl2[afn2++] = ac->afi;
2300
    }
2301

    
2302
    bgp_show_afis(-1006, "        RX:", afl1, afn1);
2303
    bgp_show_afis(-1006, "        TX:", afl2, afn2);
2304
  }
2305

    
2306
  if (caps->enhanced_refresh)
2307
    cli_msg(-1006, "      Enhanced refresh");
2308

    
2309
  if (caps->llgr_aware)
2310
    cli_msg(-1006, "      Long-lived graceful restart");
2311

    
2312
  if (any_llgr_able)
2313
  {
2314
    u32 stale_time = 0;
2315

    
2316
    afn1 = afn2 = 0;
2317
    WALK_AF_CAPS(caps, ac)
2318
    {
2319
      stale_time = MAX(stale_time, ac->llgr_time);
2320

    
2321
      if (ac->llgr_able && ac->llgr_time)
2322
        afl1[afn1++] = ac->afi;
2323

    
2324
      if (ac->llgr_flags & BGP_GRF_FORWARDING)
2325
        afl2[afn2++] = ac->afi;
2326
    }
2327

    
2328
    /* Continues from llgr_aware */
2329
    cli_msg(-1006, "        LL stale time: %u", stale_time);
2330

    
2331
    bgp_show_afis(-1006, "        AF supported:", afl1, afn1);
2332
    bgp_show_afis(-1006, "        AF preserved:", afl2, afn2);
2333
  }
2334
}
2335

    
2336
static void
2337
bgp_show_proto_info(struct proto *P)
2338
{
2339
  struct bgp_proto *p = (struct bgp_proto *) P;
2340

    
2341
  cli_msg(-1006, "  BGP state:          %s", bgp_state_dsc(p));
2342

    
2343
  if (bgp_is_dynamic(p) && p->cf->remote_range)
2344
    cli_msg(-1006, "    Neighbor range:   %N", p->cf->remote_range);
2345
  else
2346
    cli_msg(-1006, "    Neighbor address: %I%J", p->remote_ip, p->cf->iface);
2347

    
2348
  cli_msg(-1006, "    Neighbor AS:      %u", p->remote_as);
2349
  cli_msg(-1006, "    Local AS:         %u", p->cf->local_as);
2350

    
2351
  if (p->gr_active_num)
2352
    cli_msg(-1006, "    Neighbor graceful restart active");
2353

    
2354
  if (P->proto_state == PS_START)
2355
  {
2356
    struct bgp_conn *oc = &p->outgoing_conn;
2357

    
2358
    if ((p->start_state < BSS_CONNECT) &&
2359
        (tm_active(p->startup_timer)))
2360
      cli_msg(-1006, "    Error wait:       %t/%u",
2361
              tm_remains(p->startup_timer), p->startup_delay);
2362

    
2363
    if ((oc->state == BS_ACTIVE) &&
2364
        (tm_active(oc->connect_timer)))
2365
      cli_msg(-1006, "    Connect delay:    %t/%u",
2366
              tm_remains(oc->connect_timer), p->cf->connect_delay_time);
2367

    
2368
    if (p->gr_active_num && tm_active(p->gr_timer))
2369
      cli_msg(-1006, "    Restart timer:    %t/-",
2370
              tm_remains(p->gr_timer));
2371
  }
2372
  else if (P->proto_state == PS_UP)
2373
  {
2374
    cli_msg(-1006, "    Neighbor ID:      %R", p->remote_id);
2375
    cli_msg(-1006, "    Local capabilities");
2376
    bgp_show_capabilities(p, p->conn->local_caps);
2377
    cli_msg(-1006, "    Neighbor capabilities");
2378
    bgp_show_capabilities(p, p->conn->remote_caps);
2379
    cli_msg(-1006, "    Session:          %s%s%s%s%s",
2380
            p->is_internal ? "internal" : "external",
2381
            p->cf->multihop ? " multihop" : "",
2382
            p->rr_client ? " route-reflector" : "",
2383
            p->rs_client ? " route-server" : "",
2384
            p->as4_session ? " AS4" : "");
2385
    cli_msg(-1006, "    Source address:   %I", p->local_ip);
2386
    cli_msg(-1006, "    Hold timer:       %t/%u",
2387
            tm_remains(p->conn->hold_timer), p->conn->hold_time);
2388
    cli_msg(-1006, "    Keepalive timer:  %t/%u",
2389
            tm_remains(p->conn->keepalive_timer), p->conn->keepalive_time);
2390
  }
2391

    
2392
  if ((p->last_error_class != BE_NONE) &&
2393
      (p->last_error_class != BE_MAN_DOWN))
2394
  {
2395
    const char *err1 = bgp_err_classes[p->last_error_class];
2396
    const char *err2 = bgp_last_errmsg(p);
2397
    cli_msg(-1006, "    Last error:       %s%s", err1, err2);
2398
  }
2399

    
2400
  {
2401
    struct bgp_channel *c;
2402
    WALK_LIST(c, p->p.channels)
2403
    {
2404
      channel_show_info(&c->c);
2405

    
2406
      if (p->gr_active_num)
2407
        cli_msg(-1006, "    Neighbor GR:    %s", bgp_gr_states[c->gr_active]);
2408

    
2409
      if (c->stale_timer && tm_active(c->stale_timer))
2410
        cli_msg(-1006, "    LL stale timer: %t/-", tm_remains(c->stale_timer));
2411

    
2412
      if (c->c.channel_state == CS_UP)
2413
      {
2414
        if (ipa_zero(c->link_addr))
2415
          cli_msg(-1006, "    BGP Next hop:   %I", c->next_hop_addr);
2416
        else
2417
          cli_msg(-1006, "    BGP Next hop:   %I %I", c->next_hop_addr, c->link_addr);
2418
      }
2419

    
2420
      if (c->igp_table_ip4)
2421
        cli_msg(-1006, "    IGP IPv4 table: %s", c->igp_table_ip4->name);
2422

    
2423
      if (c->igp_table_ip6)
2424
        cli_msg(-1006, "    IGP IPv6 table: %s", c->igp_table_ip6->name);
2425
    }
2426
  }
2427
}
2428

    
2429
struct channel_class channel_bgp = {
2430
  .channel_size =        sizeof(struct bgp_channel),
2431
  .config_size =        sizeof(struct bgp_channel_config),
2432
  .init =                bgp_channel_init,
2433
  .start =                bgp_channel_start,
2434
  .shutdown =                bgp_channel_shutdown,
2435
  .cleanup =                bgp_channel_cleanup,
2436
  .reconfigure =        bgp_channel_reconfigure,
2437
};
2438

    
2439
struct protocol proto_bgp = {
2440
  .name =                 "BGP",
2441
  .template =                 "bgp%d",
2442
  .class =                PROTOCOL_BGP,
2443
  .preference =         DEF_PREF_BGP,
2444
  .channel_mask =        NB_IP | NB_VPN | NB_FLOW,
2445
  .proto_size =                sizeof(struct bgp_proto),
2446
  .config_size =        sizeof(struct bgp_config),
2447
  .postconfig =                bgp_postconfig,
2448
  .init =                 bgp_init,
2449
  .start =                 bgp_start,
2450
  .shutdown =                 bgp_shutdown,
2451
  .reconfigure =         bgp_reconfigure,
2452
  .copy_config =         bgp_copy_config,
2453
  .get_status =         bgp_get_status,
2454
  .get_attr =                 bgp_get_attr,
2455
  .get_route_info =         bgp_get_route_info,
2456
  .show_proto_info =         bgp_show_proto_info
2457
};