Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bgp / bgp.c @ 4cdd0784

History | View | Annotate | Download (25.4 KB)

1
/*
2
 *        BIRD -- The Border Gateway Protocol
3
 *
4
 *        (c) 2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
/**
10
 * DOC: Border Gateway Protocol
11
 *
12
 * The BGP protocol is implemented in three parts: |bgp.c| which takes care of the
13
 * connection and most of the interface with BIRD core, |packets.c| handling
14
 * both incoming and outgoing BGP packets and |attrs.c| containing functions for
15
 * manipulation with BGP attribute lists.
16
 *
17
 * As opposed to the other existing routing daemons, BIRD has a sophisticated core
18
 * architecture which is able to keep all the information needed by BGP in the
19
 * primary routing table, therefore no complex data structures like a central
20
 * BGP table are needed. This increases memory footprint of a BGP router with
21
 * many connections, but not too much and, which is more important, it makes
22
 * BGP much easier to implement.
23
 *
24
 * Each instance of BGP (corresponding to a single BGP peer) is described by a &bgp_proto
25
 * structure to which are attached individual connections represented by &bgp_connection
26
 * (usually, there exists only one connection, but during BGP session setup, there
27
 * can be more of them). The connections are handled according to the BGP state machine
28
 * defined in the RFC with all the timers and all the parameters configurable.
29
 *
30
 * In incoming direction, we listen on the connection's socket and each time we receive
31
 * some input, we pass it to bgp_rx(). It decodes packet headers and the markers and
32
 * passes complete packets to bgp_rx_packet() which distributes the packet according
33
 * to its type.
34
 *
35
 * In outgoing direction, we gather all the routing updates and sort them to buckets
36
 * (&bgp_bucket) according to their attributes (we keep a hash table for fast comparison
37
 * of &rta's and a &fib which helps us to find if we already have another route for
38
 * the same destination queued for sending, so that we can replace it with the new one
39
 * immediately instead of sending both updates). There also exists a special bucket holding
40
 * all the route withdrawals which cannot be queued anywhere else as they don't have any
41
 * attributes. If we have any packet to send (due to either new routes or the connection
42
 * tracking code wanting to send a Open, Keepalive or Notification message), we call
43
 * bgp_schedule_packet() which sets the corresponding bit in a @packet_to_send
44
 * bit field in &bgp_conn and as soon as the transmit socket buffer becomes empty,
45
 * we call bgp_fire_tx(). It inspects state of all the packet type bits and calls
46
 * the corresponding bgp_create_xx() functions, eventually rescheduling the same packet
47
 * type if we have more data of the same type to send.
48
 *
49
 * The processing of attributes consists of two functions: bgp_decode_attrs() for checking
50
 * of the attribute blocks and translating them to the language of BIRD's extended attributes
51
 * and bgp_encode_attrs() which does the converse. Both functions are built around a
52
 * @bgp_attr_table array describing all important characteristics of all known attributes.
53
 * Unknown transitive attributes are attached to the route as %EAF_TYPE_OPAQUE byte streams.
54
 */
55

    
56
#undef LOCAL_DEBUG
57

    
58
#include "nest/bird.h"
59
#include "nest/iface.h"
60
#include "nest/protocol.h"
61
#include "nest/route.h"
62
#include "nest/locks.h"
63
#include "conf/conf.h"
64
#include "lib/socket.h"
65
#include "lib/resource.h"
66
#include "lib/string.h"
67

    
68
#include "bgp.h"
69

    
70
struct linpool *bgp_linpool;                /* Global temporary pool */
71
static sock *bgp_listen_sk;                /* Global listening socket */
72
static int bgp_counter;                        /* Number of protocol instances using the listening socket */
73

    
74
static void bgp_close(struct bgp_proto *p, int apply_md5);
75
static void bgp_connect(struct bgp_proto *p);
76
static void bgp_active(struct bgp_proto *p);
77
static sock *bgp_setup_listen_sk(ip_addr addr, unsigned port, u32 flags);
78

    
79

    
80
/**
81
 * bgp_open - open a BGP instance
82
 * @p: BGP instance
83
 *
84
 * This function allocates and configures shared BGP resources.
85
 * Should be called as the last step during initialization
86
 * (when lock is acquired and neighbor is ready).
87
 * When error, state changed to PS_DOWN, -1 is returned and caller
88
 * should return immediately.
89
 */
90
static int
91
bgp_open(struct bgp_proto *p)
92
{
93
  struct config *cfg = p->cf->c.global;
94
  bgp_counter++;
95

    
96
  if (!bgp_listen_sk)
97
    bgp_listen_sk = bgp_setup_listen_sk(cfg->listen_bgp_addr, cfg->listen_bgp_port, cfg->listen_bgp_flags);
98

    
99
  if (!bgp_linpool)
100
    bgp_linpool = lp_new(&root_pool, 4080);
101

    
102
  if (p->cf->password)
103
    {
104
      int rv = sk_set_md5_auth(bgp_listen_sk, p->cf->remote_ip, p->cf->password);
105
      if (rv < 0)
106
        {
107
          bgp_close(p, 0);
108
          p->p.disabled = 1;
109
          bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_MD5);
110
          proto_notify_state(&p->p, PS_DOWN);
111
          return -1;
112
        }
113
    }
114

    
115
  return 0;
116
}
117

    
118
static void
119
bgp_startup(struct bgp_proto *p)
120
{
121
  BGP_TRACE(D_EVENTS, "Started");
122
  p->start_state = p->cf->capabilities ? BSS_CONNECT : BSS_CONNECT_NOCAP;
123

    
124
  if (!p->cf->passive)
125
    bgp_active(p);
126
}
127

    
128
static void
129
bgp_startup_timeout(timer *t)
130
{
131
  bgp_startup(t->data);
132
}
133

    
134

    
135
static void
136
bgp_initiate(struct bgp_proto *p)
137
{
138
  if (p->startup_delay)
139
    {
140
      BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds", p->startup_delay);
141
      bgp_start_timer(p->startup_timer, p->startup_delay);
142
    }
143
  else
144
    bgp_startup(p);
145
}
146

    
147
/**
148
 * bgp_close - close a BGP instance
149
 * @p: BGP instance
150
 * @apply_md5: 0 to disable unsetting MD5 auth
151
 *
152
 * This function frees and deconfigures shared BGP resources.
153
 * @apply_md5 is set to 0 when bgp_close is called as a cleanup
154
 * from failed bgp_open().
155
 */
156
static void
157
bgp_close(struct bgp_proto *p, int apply_md5)
158
{
159
  ASSERT(bgp_counter);
160
  bgp_counter--;
161

    
162
  if (p->cf->password && apply_md5)
163
    sk_set_md5_auth(bgp_listen_sk, p->cf->remote_ip, NULL);
164

    
165
  if (!bgp_counter)
166
    {
167
      rfree(bgp_listen_sk);
168
      bgp_listen_sk = NULL;
169
      rfree(bgp_linpool);
170
      bgp_linpool = NULL;
171
    }
172
}
173

    
174
/**
175
 * bgp_start_timer - start a BGP timer
176
 * @t: timer
177
 * @value: time to fire (0 to disable the timer)
178
 *
179
 * This functions calls tm_start() on @t with time @value and the
180
 * amount of randomization suggested by the BGP standard. Please use
181
 * it for all BGP timers.
182
 */
183
void
184
bgp_start_timer(timer *t, int value)
185
{
186
  if (value)
187
    {
188
      /* The randomization procedure is specified in RFC 1771: 9.2.3.3 */
189
      t->randomize = value / 4;
190
      tm_start(t, value - t->randomize);
191
    }
192
  else
193
    tm_stop(t);
194
}
195

    
196
/**
197
 * bgp_close_conn - close a BGP connection
198
 * @conn: connection to close
199
 *
200
 * This function takes a connection described by the &bgp_conn structure,
201
 * closes its socket and frees all resources associated with it.
202
 */
203
void
204
bgp_close_conn(struct bgp_conn *conn)
205
{
206
  struct bgp_proto *p = conn->bgp;
207

    
208
  DBG("BGP: Closing connection\n");
209
  conn->packets_to_send = 0;
210
  rfree(conn->connect_retry_timer);
211
  conn->connect_retry_timer = NULL;
212
  rfree(conn->keepalive_timer);
213
  conn->keepalive_timer = NULL;
214
  rfree(conn->hold_timer);
215
  conn->hold_timer = NULL;
216
  rfree(conn->sk);
217
  conn->sk = NULL;
218
  rfree(conn->tx_ev);
219
  conn->tx_ev = NULL;
220
}
221

    
222

    
223
/**
224
 * bgp_update_startup_delay - update a startup delay
225
 * @p: BGP instance
226
 *
227
 * This function updates a startup delay that is used to postpone next BGP connect.
228
 * It also handles disable_after_error and might stop BGP instance when error
229
 * happened and disable_after_error is on.
230
 *
231
 * It should be called when BGP protocol error happened.
232
 */
233
void
234
bgp_update_startup_delay(struct bgp_proto *p)
235
{
236
  struct bgp_config *cf = p->cf;
237

    
238
  DBG("BGP: Updating startup delay\n");
239

    
240
  if (p->last_proto_error && ((now - p->last_proto_error) >= cf->error_amnesia_time))
241
    p->startup_delay = 0;
242

    
243
  p->last_proto_error = now;
244

    
245
  if (cf->disable_after_error)
246
    {
247
      p->startup_delay = 0;
248
      p->p.disabled = 1;
249
      return;
250
    }
251

    
252
  if (!p->startup_delay)
253
    p->startup_delay = cf->error_delay_time_min;
254
  else
255
    p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max);
256
}
257

    
258
static void
259
bgp_graceful_close_conn(struct bgp_conn *conn, unsigned subcode)
260
{
261
  switch (conn->state)
262
    {
263
    case BS_IDLE:
264
    case BS_CLOSE:
265
      return;
266
    case BS_CONNECT:
267
    case BS_ACTIVE:
268
      bgp_conn_enter_idle_state(conn);
269
      return;
270
    case BS_OPENSENT:
271
    case BS_OPENCONFIRM:
272
    case BS_ESTABLISHED:
273
      bgp_error(conn, 6, subcode, NULL, 0);
274
      return;
275
    default:
276
      bug("bgp_graceful_close_conn: Unknown state %d", conn->state);
277
    }
278
}
279

    
280
static void
281
bgp_down(struct bgp_proto *p)
282
{
283
  if (p->start_state > BSS_PREPARE)
284
    bgp_close(p, 1);
285

    
286
  BGP_TRACE(D_EVENTS, "Down");
287
  proto_notify_state(&p->p, PS_DOWN);
288
}
289

    
290
static void
291
bgp_decision(void *vp)
292
{
293
  struct bgp_proto *p = vp;
294

    
295
  DBG("BGP: Decision start\n");
296
  if ((p->p.proto_state == PS_START)
297
      && (p->outgoing_conn.state == BS_IDLE)
298
      && (!p->cf->passive))
299
    bgp_active(p);
300

    
301
  if ((p->p.proto_state == PS_STOP)
302
      && (p->outgoing_conn.state == BS_IDLE)
303
      && (p->incoming_conn.state == BS_IDLE))
304
    bgp_down(p);
305
}
306

    
307
void
308
bgp_stop(struct bgp_proto *p, unsigned subcode)
309
{
310
  proto_notify_state(&p->p, PS_STOP);
311
  bgp_graceful_close_conn(&p->outgoing_conn, subcode);
312
  bgp_graceful_close_conn(&p->incoming_conn, subcode);
313
  ev_schedule(p->event);
314
}
315

    
316
void
317
bgp_conn_enter_established_state(struct bgp_conn *conn)
318
{
319
  struct bgp_proto *p = conn->bgp;
320
 
321
  BGP_TRACE(D_EVENTS, "BGP session established");
322
  DBG("BGP: UP!!!\n");
323

    
324
  p->conn = conn;
325
  p->last_error_class = 0;
326
  p->last_error_code = 0;
327
  bgp_attr_init(conn->bgp);
328
  conn->state = BS_ESTABLISHED;
329
  proto_notify_state(&p->p, PS_UP);
330
}
331

    
332
static void
333
bgp_conn_leave_established_state(struct bgp_proto *p)
334
{
335
  BGP_TRACE(D_EVENTS, "BGP session closed");
336
  p->conn = NULL;
337

    
338
  if (p->p.proto_state == PS_UP)
339
    bgp_stop(p, 0);
340
}
341

    
342
void
343
bgp_conn_enter_close_state(struct bgp_conn *conn)
344
{
345
  struct bgp_proto *p = conn->bgp;
346
  int os = conn->state;
347

    
348
  conn->state = BS_CLOSE;
349
  tm_stop(conn->hold_timer);
350
  tm_stop(conn->keepalive_timer);
351
  conn->sk->rx_hook = NULL;
352

    
353
  if (os == BS_ESTABLISHED)
354
    bgp_conn_leave_established_state(p);
355
}
356

    
357
void
358
bgp_conn_enter_idle_state(struct bgp_conn *conn)
359
{
360
  struct bgp_proto *p = conn->bgp;
361
  int os = conn->state;
362

    
363
  bgp_close_conn(conn);
364
  conn->state = BS_IDLE;
365
  ev_schedule(p->event);
366

    
367
  if (os == BS_ESTABLISHED)
368
    bgp_conn_leave_established_state(p);
369
}
370

    
371
static void
372
bgp_send_open(struct bgp_conn *conn)
373
{
374
  conn->start_state = conn->bgp->start_state;
375
  conn->want_as4_support = conn->bgp->cf->enable_as4 && (conn->start_state != BSS_CONNECT_NOCAP);
376
  conn->peer_as4_support = 0;        // Default value, possibly changed by receiving capability.
377

    
378
  DBG("BGP: Sending open\n");
379
  conn->sk->rx_hook = bgp_rx;
380
  conn->sk->tx_hook = bgp_tx;
381
  tm_stop(conn->connect_retry_timer);
382
  bgp_schedule_packet(conn, PKT_OPEN);
383
  conn->state = BS_OPENSENT;
384
  bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
385
}
386

    
387
static void
388
bgp_connected(sock *sk)
389
{
390
  struct bgp_conn *conn = sk->data;
391
  struct bgp_proto *p = conn->bgp;
392

    
393
  BGP_TRACE(D_EVENTS, "Connected");
394
  bgp_send_open(conn);
395
}
396

    
397
static void
398
bgp_connect_timeout(timer *t)
399
{
400
  struct bgp_conn *conn = t->data;
401
  struct bgp_proto *p = conn->bgp;
402

    
403
  DBG("BGP: connect_timeout\n");
404
  if (p->p.proto_state == PS_START)
405
    {
406
      bgp_close_conn(conn);
407
      bgp_connect(p);
408
    }
409
  else
410
    bgp_conn_enter_idle_state(conn);
411
}
412

    
413
static void
414
bgp_sock_err(sock *sk, int err)
415
{
416
  struct bgp_conn *conn = sk->data;
417
  struct bgp_proto *p = conn->bgp;
418

    
419
  bgp_store_error(p, conn, BE_SOCKET, err);
420

    
421
  if (err)
422
    BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
423
  else
424
    BGP_TRACE(D_EVENTS, "Connection closed");
425

    
426
  bgp_conn_enter_idle_state(conn);
427
}
428

    
429
static void
430
bgp_hold_timeout(timer *t)
431
{
432
  struct bgp_conn *conn = t->data;
433

    
434
  DBG("BGP: Hold timeout\n");
435

    
436
  /* If there is something in input queue, we are probably congested
437
     and perhaps just not processed BGP packets in time. */
438

    
439
  if (sk_rx_ready(conn->sk) > 0)
440
    bgp_start_timer(conn->hold_timer, 10);
441
  else
442
    bgp_error(conn, 4, 0, NULL, 0);
443
}
444

    
445
static void
446
bgp_keepalive_timeout(timer *t)
447
{
448
  struct bgp_conn *conn = t->data;
449

    
450
  DBG("BGP: Keepalive timer\n");
451
  bgp_schedule_packet(conn, PKT_KEEPALIVE);
452
}
453

    
454
static void
455
bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
456
{
457
  timer *t;
458

    
459
  conn->sk = NULL;
460
  conn->bgp = p;
461
  conn->packets_to_send = 0;
462

    
463
  t = conn->connect_retry_timer = tm_new(p->p.pool);
464
  t->hook = bgp_connect_timeout;
465
  t->data = conn;
466
  t = conn->hold_timer = tm_new(p->p.pool);
467
  t->hook = bgp_hold_timeout;
468
  t->data = conn;
469
  t = conn->keepalive_timer = tm_new(p->p.pool);
470
  t->hook = bgp_keepalive_timeout;
471
  t->data = conn;
472
  conn->tx_ev = ev_new(p->p.pool);
473
  conn->tx_ev->hook = bgp_kick_tx;
474
  conn->tx_ev->data = conn;
475
}
476

    
477
static void
478
bgp_setup_sk(struct bgp_proto *p, struct bgp_conn *conn, sock *s)
479
{
480
  s->data = conn;
481
  s->err_hook = bgp_sock_err;
482
  conn->sk = s;
483
}
484

    
485
static void
486
bgp_active(struct bgp_proto *p)
487
{
488
  int delay = MAX(1, p->cf->start_delay_time);
489
  struct bgp_conn *conn = &p->outgoing_conn;
490

    
491
  BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
492
  bgp_setup_conn(p, conn);
493
  conn->state = BS_ACTIVE;
494
  bgp_start_timer(conn->connect_retry_timer, delay);
495
}
496

    
497
int
498
bgp_apply_limits(struct bgp_proto *p)
499
{
500
  if (p->cf->route_limit && (p->p.stats.imp_routes > p->cf->route_limit))
501
    {
502
      log(L_WARN "%s: Route limit exceeded, shutting down", p->p.name);
503
      bgp_store_error(p, NULL, BE_AUTO_DOWN, BEA_ROUTE_LIMIT_EXCEEDED);
504
      bgp_update_startup_delay(p);
505
      bgp_stop(p, 1); // Errcode 6, 1 - max number of prefixes reached
506
      return -1;
507
    }
508

    
509
  return 0;
510
}
511

    
512

    
513
/**
514
 * bgp_connect - initiate an outgoing connection
515
 * @p: BGP instance
516
 *
517
 * The bgp_connect() function creates a new &bgp_conn and initiates
518
 * a TCP connection to the peer. The rest of connection setup is governed
519
 * by the BGP state machine as described in the standard.
520
 */
521
static void
522
bgp_connect(struct bgp_proto *p)        /* Enter Connect state and start establishing connection */
523
{
524
  sock *s;
525
  struct bgp_conn *conn = &p->outgoing_conn;
526

    
527
  DBG("BGP: Connecting\n");
528
  s = sk_new(p->p.pool);
529
  s->type = SK_TCP_ACTIVE;
530
  s->saddr = p->source_addr;
531
  s->daddr = p->cf->remote_ip;
532
  s->dport = BGP_PORT;
533
  s->ttl = p->cf->multihop ? : 1;
534
  s->rbsize = BGP_RX_BUFFER_SIZE;
535
  s->tbsize = BGP_TX_BUFFER_SIZE;
536
  s->tos = IP_PREC_INTERNET_CONTROL;
537
  s->password = p->cf->password;
538
  s->tx_hook = bgp_connected;
539
  BGP_TRACE(D_EVENTS, "Connecting to %I from local address %I", s->daddr, s->saddr);
540
  bgp_setup_conn(p, conn);
541
  bgp_setup_sk(p, conn, s);
542
  conn->state = BS_CONNECT;
543
  if (sk_open(s))
544
    {
545
      bgp_sock_err(s, 0);
546
      return;
547
    }
548
  DBG("BGP: Waiting for connect success\n");
549
  bgp_start_timer(conn->connect_retry_timer, p->cf->connect_retry_time);
550
}
551

    
552
/**
553
 * bgp_incoming_connection - handle an incoming connection
554
 * @sk: TCP socket
555
 * @dummy: unused
556
 *
557
 * This function serves as a socket hook for accepting of new BGP
558
 * connections. It searches a BGP instance corresponding to the peer
559
 * which has connected and if such an instance exists, it creates a
560
 * &bgp_conn structure, attaches it to the instance and either sends
561
 * an Open message or (if there already is an active connection) it
562
 * closes the new connection by sending a Notification message.
563
 */
564
static int
565
bgp_incoming_connection(sock *sk, int dummy UNUSED)
566
{
567
  struct proto_config *pc;
568

    
569
  DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
570
  WALK_LIST(pc, config->protos)
571
    if (pc->protocol == &proto_bgp && pc->proto)
572
      {
573
        struct bgp_proto *p = (struct bgp_proto *) pc->proto;
574
        if (ipa_equal(p->cf->remote_ip, sk->daddr))
575
          {
576
            /* We are in proper state and there is no other incoming connection */
577
            int acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
578
              (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
579

    
580
            BGP_TRACE(D_EVENTS, "Incoming connection from %I (port %d) %s",
581
                      sk->daddr, sk->dport, acc ? "accepted" : "rejected");
582

    
583
            if (!acc)
584
              goto err;
585

    
586
            bgp_setup_conn(p, &p->incoming_conn);
587
            bgp_setup_sk(p, &p->incoming_conn, sk);
588
            sk_set_ttl(sk, p->cf->multihop ? : 1);
589
            bgp_send_open(&p->incoming_conn);
590
            return 0;
591
          }
592
      }
593

    
594
  log(L_WARN "BGP: Unexpected connect from unknown address %I (port %d)", sk->daddr, sk->dport);
595
 err:
596
  rfree(sk);
597
  return 0;
598
}
599

    
600
static sock *
601
bgp_setup_listen_sk(ip_addr addr, unsigned port, u32 flags)
602
{
603
  sock *s = sk_new(&root_pool);
604
  DBG("BGP: Creating incoming socket\n");
605
  s->type = SK_TCP_PASSIVE;
606
  s->saddr = addr;
607
  s->sport = port ? port : BGP_PORT;
608
  s->flags = flags;
609
  s->tos = IP_PREC_INTERNET_CONTROL;
610
  s->rbsize = BGP_RX_BUFFER_SIZE;
611
  s->tbsize = BGP_TX_BUFFER_SIZE;
612
  s->rx_hook = bgp_incoming_connection;
613
  if (sk_open(s))
614
    {
615
      log(L_ERR "Unable to open incoming BGP socket");
616
      rfree(s);
617
      return NULL;
618
    }
619
  else
620
    return s;
621
}
622

    
623
static void
624
bgp_start_neighbor(struct bgp_proto *p)
625
{
626
  p->local_addr = p->neigh->iface->addr->ip;
627
  p->source_addr = ipa_nonzero(p->cf->source_addr) ? p->cf->source_addr : p->local_addr;
628

    
629
  DBG("BGP: local=%I remote=%I\n", p->source_addr, p->next_hop);
630
#ifdef IPV6
631
  {
632
    struct ifa *a;
633
    p->local_link = IPA_NONE;
634
    WALK_LIST(a, p->neigh->iface->addrs)
635
      if (a->scope == SCOPE_LINK)
636
        {
637
          p->local_link = a->ip;
638
          break;
639
        }
640

    
641
    if (! ipa_nonzero(p->local_link))
642
      log(L_WARN "%s: Missing link local address on interface %s", p->p.name,  p->neigh->iface->name);
643

    
644
    DBG("BGP: Selected link-level address %I\n", p->local_link);
645
  }
646
#endif
647

    
648
  int rv = bgp_open(p);
649
  if (rv < 0)
650
    return;
651

    
652
  bgp_initiate(p);
653
}
654

    
655
static void
656
bgp_neigh_notify(neighbor *n)
657
{
658
  struct bgp_proto *p = (struct bgp_proto *) n->proto;
659

    
660
  if (n->iface)
661
    {
662
      if ((p->p.proto_state == PS_START) && (p->start_state == BSS_PREPARE))
663
        {
664
          BGP_TRACE(D_EVENTS, "Neighbor found");
665
          bgp_start_neighbor(p);
666
        }
667
    }
668
  else
669
    {
670
      if ((p->p.proto_state == PS_START) || (p->p.proto_state == PS_UP))
671
        {
672
          BGP_TRACE(D_EVENTS, "Neighbor lost");
673
          bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST);
674
          bgp_stop(p, 0);
675
        }
676
    }
677
}
678

    
679
static void
680
bgp_start_locked(struct object_lock *lock)
681
{
682
  struct bgp_proto *p = lock->data;
683
  struct bgp_config *cf = p->cf;
684

    
685
  if (p->p.proto_state != PS_START)
686
    {
687
      DBG("BGP: Got lock in different state %d\n", p->p.proto_state);
688
    return;
689
    }
690

    
691
  DBG("BGP: Got lock\n");
692
  p->local_id = proto_get_router_id(&cf->c);
693
  p->next_hop = cf->multihop ? cf->multihop_via : cf->remote_ip;
694
  p->neigh = neigh_find(&p->p, &p->next_hop, NEF_STICKY);
695

    
696
  if (cf->rr_client)
697
    {
698
      p->rr_cluster_id = cf->rr_cluster_id ? cf->rr_cluster_id : p->local_id;
699
      p->rr_client = cf->rr_client;
700
    }
701

    
702
  p->rs_client = cf->rs_client;
703

    
704
  if (!p->neigh)
705
    {
706
      log(L_ERR "%s: Invalid next hop %I", p->p.name, p->next_hop);
707
      /* As we do not start yet, we can just disable protocol */
708
      p->p.disabled = 1;
709
      bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_NEXT_HOP);
710
      proto_notify_state(&p->p, PS_DOWN);
711
      return;
712
    }
713
  
714
  if (p->neigh->iface)
715
    bgp_start_neighbor(p);
716
  else
717
    BGP_TRACE(D_EVENTS, "Waiting for %I to become my neighbor", p->next_hop);
718
}
719

    
720
static int
721
bgp_start(struct proto *P)
722
{
723
  struct bgp_proto *p = (struct bgp_proto *) P;
724
  struct object_lock *lock;
725

    
726
  DBG("BGP: Startup.\n");
727
  p->start_state = BSS_PREPARE;
728
  p->outgoing_conn.state = BS_IDLE;
729
  p->incoming_conn.state = BS_IDLE;
730
  p->neigh = NULL;
731

    
732
  p->event = ev_new(p->p.pool);
733
  p->event->hook = bgp_decision;
734
  p->event->data = p;
735

    
736
  p->startup_timer = tm_new(p->p.pool);
737
  p->startup_timer->hook = bgp_startup_timeout;
738
  p->startup_timer->data = p;
739

    
740
  /*
741
   *  Before attempting to create the connection, we need to lock the
742
   *  port, so that are sure we're the only instance attempting to talk
743
   *  with that neighbor.
744
   */
745

    
746
  lock = p->lock = olock_new(P->pool);
747
  lock->addr = p->cf->remote_ip;
748
  lock->type = OBJLOCK_TCP;
749
  lock->port = BGP_PORT;
750
  lock->iface = NULL;
751
  lock->hook = bgp_start_locked;
752
  lock->data = p;
753
  olock_acquire(lock);
754

    
755
  return PS_START;
756
}
757

    
758
static int
759
bgp_shutdown(struct proto *P)
760
{
761
  struct bgp_proto *p = (struct bgp_proto *) P;
762
  unsigned subcode;
763

    
764
  BGP_TRACE(D_EVENTS, "Shutdown requested");
765
  bgp_store_error(p, NULL, BE_MAN_DOWN, 0);
766

    
767
  if (P->reconfiguring)
768
    {
769
      if (P->cf_new)
770
        subcode = 6; // Errcode 6, 6 - other configuration change
771
      else
772
        subcode = 3; // Errcode 6, 3 - peer de-configured
773
    }
774
  else
775
    subcode = 2; // Errcode 6, 2 - administrative shutdown
776

    
777
  p->startup_delay = 0;
778
  bgp_stop(p, subcode);
779

    
780
  return p->p.proto_state;
781
}
782

    
783
static struct proto *
784
bgp_init(struct proto_config *C)
785
{
786
  struct bgp_config *c = (struct bgp_config *) C;
787
  struct proto *P = proto_new(C, sizeof(struct bgp_proto));
788
  struct bgp_proto *p = (struct bgp_proto *) P;
789

    
790
  P->accept_ra_types = RA_OPTIMAL;
791
  P->rt_notify = bgp_rt_notify;
792
  P->rte_better = bgp_rte_better;
793
  P->import_control = bgp_import_control;
794
  P->neigh_notify = bgp_neigh_notify;
795
  p->cf = c;
796
  p->local_as = c->local_as;
797
  p->remote_as = c->remote_as;
798
  p->is_internal = (c->local_as == c->remote_as);
799
  return P;
800
}
801

    
802
/**
803
 * bgp_error - report a protocol error
804
 * @c: connection
805
 * @code: error code (according to the RFC)
806
 * @subcode: error sub-code
807
 * @data: data to be passed in the Notification message
808
 * @len: length of the data
809
 *
810
 * bgp_error() sends a notification packet to tell the other side that a protocol
811
 * error has occurred (including the data considered erroneous if possible) and
812
 * closes the connection.
813
 */
814
void
815
bgp_error(struct bgp_conn *c, unsigned code, unsigned subcode, byte *data, int len)
816
{
817
  struct bgp_proto *p = c->bgp;
818

    
819
  if (c->state == BS_CLOSE)
820
    return;
821

    
822
  bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, data, (len > 0) ? len : -len);
823
  bgp_store_error(p, c, BE_BGP_TX, (code << 16) | subcode);
824
  bgp_conn_enter_close_state(c);
825

    
826
  c->notify_code = code;
827
  c->notify_subcode = subcode;
828
  c->notify_data = data;
829
  c->notify_size = (len > 0) ? len : 0;
830
  bgp_schedule_packet(c, PKT_NOTIFICATION);
831

    
832
  if (code != 6)
833
    {
834
      bgp_update_startup_delay(p);
835
      bgp_stop(p, 0);
836
    }
837
}
838

    
839
/**
840
 * bgp_store_error - store last error for status report
841
 * @p: BGP instance
842
 * @c: connection
843
 * @class: error class (BE_xxx constants)
844
 * @code: error code (class specific)
845
 *
846
 * bgp_store_error() decides whether given error is interesting enough
847
 * and store that error to last_error variables of @p
848
 */
849
void
850
bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code)
851
{
852
  /* During PS_UP, we ignore errors on secondary connection */
853
  if ((p->p.proto_state == PS_UP) && c && (c != p->conn))
854
    return;
855

    
856
  /* During PS_STOP, we ignore any errors, as we want to report
857
   * the error that caused transition to PS_STOP
858
   */
859
  if (p->p.proto_state == PS_STOP)
860
    return;
861

    
862
  p->last_error_class = class;
863
  p->last_error_code = code;
864
}
865

    
866
void
867
bgp_check(struct bgp_config *c)
868
{
869
  if (!c->local_as)
870
    cf_error("Local AS number must be set");
871

    
872
  if (!c->remote_as)
873
    cf_error("Neighbor must be configured");
874

    
875
  if (!bgp_as4_support && c->enable_as4)
876
    cf_error("AS4 support disabled globally");
877

    
878
  if (!bgp_as4_support && (c->local_as > 0xFFFF))
879
    cf_error("Local AS number out of range");
880

    
881
  if (!(c->capabilities && c->enable_as4) && (c->remote_as > 0xFFFF))
882
    cf_error("Neighbor AS number out of range (AS4 not available)");
883

    
884
  if ((c->local_as != c->remote_as) && (c->rr_client))
885
    cf_error("Only internal neighbor can be RR client");
886

    
887
  if ((c->local_as == c->remote_as) && (c->rs_client))
888
    cf_error("Only external neighbor can be RS client");
889
}
890

    
891
static char *bgp_state_names[] = { "Idle", "Connect", "Active", "OpenSent", "OpenConfirm", "Established", "Close" };
892
static char *bgp_err_classes[] = { "", "Error: ", "Socket: ", "Received: ", "BGP Error: ", "Automatic shutdown: ", ""};
893
static char *bgp_misc_errors[] = { "", "Neighbor lost", "Invalid next hop", "Kernel MD5 auth failed" };
894
static char *bgp_auto_errors[] = { "", "Route limit exceeded"};
895

    
896

    
897
static void
898
bgp_get_status(struct proto *P, byte *buf)
899
{
900
  struct bgp_proto *p = (struct bgp_proto *) P;
901

    
902
  const byte *err1 = bgp_err_classes[p->last_error_class];
903
  const byte *err2 = "";
904
  byte errbuf[32];
905

    
906
  switch (p->last_error_class)
907
    {
908
    case BE_MISC:
909
      err2 = bgp_misc_errors[p->last_error_code];
910
      break;
911
    case BE_SOCKET:
912
      err2 = (p->last_error_code == 0) ? "Connection closed" : strerror(p->last_error_code);
913
      break;
914
    case BE_BGP_RX:
915
    case BE_BGP_TX:
916
      err2 = bgp_error_dsc(errbuf, p->last_error_code >> 16, p->last_error_code & 0xFF);
917
      break;
918
    case BE_AUTO_DOWN:
919
      err2 = bgp_auto_errors[p->last_error_code];
920
      break;
921
    }
922

    
923
  if (P->proto_state == PS_DOWN)
924
    bsprintf(buf, "%s%s", err1, err2);
925
  else
926
    bsprintf(buf, "%-14s%s%s",
927
             bgp_state_names[MAX(p->incoming_conn.state, p->outgoing_conn.state)],
928
             err1, err2);
929
}
930

    
931
static int
932
bgp_reconfigure(struct proto *P, struct proto_config *C)
933
{
934
  struct bgp_config *new = (struct bgp_config *) C;
935
  struct bgp_proto *p = (struct bgp_proto *) P;
936
  struct bgp_config *old = p->cf;
937

    
938
  int same = !memcmp(((byte *) old) + sizeof(struct proto_config),
939
                     ((byte *) new) + sizeof(struct proto_config),
940
                     // password item is last and must be checked separately
941
                     OFFSETOF(struct bgp_config, password) - sizeof(struct proto_config))
942
    && ((!old->password && !new->password)
943
        || (old->password && new->password && !strcmp(old->password, new->password)));
944

    
945
  /* We should update our copy of configuration ptr as old configuration will be freed */
946
  if (same)
947
    p->cf = new;
948

    
949
  return same;
950
}
951

    
952
struct protocol proto_bgp = {
953
  name:                        "BGP",
954
  template:                "bgp%d",
955
  attr_class:                EAP_BGP,
956
  init:                        bgp_init,
957
  start:                bgp_start,
958
  shutdown:                bgp_shutdown,
959
  get_status:                bgp_get_status,
960
  get_attr:                bgp_get_attr,
961
  reconfigure:                bgp_reconfigure,
962
  get_route_info:        bgp_get_route_info,
963
};