Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bgp / bgp.c @ a2ea1bac

History | View | Annotate | Download (26.3 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
static inline void
317
bgp_conn_set_state(struct bgp_conn *conn, unsigned new_state)
318
{
319
  if (conn->bgp->p.mrtdump & MD_STATES)
320
    mrt_dump_bgp_state_change(conn, conn->state, new_state);
321

    
322
  conn->state = new_state;
323
}
324

    
325
void
326
bgp_conn_enter_openconfirm_state(struct bgp_conn *conn)
327
{
328
  /* Really, most of the work is done in bgp_rx_open(). */
329
  bgp_conn_set_state(conn, BS_OPENCONFIRM);
330
}
331

    
332
void
333
bgp_conn_enter_established_state(struct bgp_conn *conn)
334
{
335
  struct bgp_proto *p = conn->bgp;
336
 
337
  BGP_TRACE(D_EVENTS, "BGP session established");
338
  DBG("BGP: UP!!!\n");
339

    
340
  p->conn = conn;
341
  p->last_error_class = 0;
342
  p->last_error_code = 0;
343
  bgp_attr_init(conn->bgp);
344
  bgp_conn_set_state(conn, BS_ESTABLISHED);
345
  proto_notify_state(&p->p, PS_UP);
346
}
347

    
348
static void
349
bgp_conn_leave_established_state(struct bgp_proto *p)
350
{
351
  BGP_TRACE(D_EVENTS, "BGP session closed");
352
  p->conn = NULL;
353

    
354
  if (p->p.proto_state == PS_UP)
355
    bgp_stop(p, 0);
356
}
357

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

    
364
  bgp_conn_set_state(conn, BS_CLOSE);
365
  tm_stop(conn->hold_timer);
366
  tm_stop(conn->keepalive_timer);
367
  conn->sk->rx_hook = NULL;
368

    
369
  if (os == BS_ESTABLISHED)
370
    bgp_conn_leave_established_state(p);
371
}
372

    
373
void
374
bgp_conn_enter_idle_state(struct bgp_conn *conn)
375
{
376
  struct bgp_proto *p = conn->bgp;
377
  int os = conn->state;
378

    
379
  bgp_close_conn(conn);
380
  bgp_conn_set_state(conn, BS_IDLE);
381
  ev_schedule(p->event);
382

    
383
  if (os == BS_ESTABLISHED)
384
    bgp_conn_leave_established_state(p);
385
}
386

    
387
static void
388
bgp_send_open(struct bgp_conn *conn)
389
{
390
  conn->start_state = conn->bgp->start_state;
391
  conn->want_as4_support = conn->bgp->cf->enable_as4 && (conn->start_state != BSS_CONNECT_NOCAP);
392
  conn->peer_as4_support = 0;        // Default value, possibly changed by receiving capability.
393
  conn->advertised_as = 0;
394

    
395
  DBG("BGP: Sending open\n");
396
  conn->sk->rx_hook = bgp_rx;
397
  conn->sk->tx_hook = bgp_tx;
398
  tm_stop(conn->connect_retry_timer);
399
  bgp_schedule_packet(conn, PKT_OPEN);
400
  bgp_conn_set_state(conn, BS_OPENSENT);
401
  bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
402
}
403

    
404
static void
405
bgp_connected(sock *sk)
406
{
407
  struct bgp_conn *conn = sk->data;
408
  struct bgp_proto *p = conn->bgp;
409

    
410
  BGP_TRACE(D_EVENTS, "Connected");
411
  bgp_send_open(conn);
412
}
413

    
414
static void
415
bgp_connect_timeout(timer *t)
416
{
417
  struct bgp_conn *conn = t->data;
418
  struct bgp_proto *p = conn->bgp;
419

    
420
  DBG("BGP: connect_timeout\n");
421
  if (p->p.proto_state == PS_START)
422
    {
423
      bgp_close_conn(conn);
424
      bgp_connect(p);
425
    }
426
  else
427
    bgp_conn_enter_idle_state(conn);
428
}
429

    
430
static void
431
bgp_sock_err(sock *sk, int err)
432
{
433
  struct bgp_conn *conn = sk->data;
434
  struct bgp_proto *p = conn->bgp;
435

    
436
  bgp_store_error(p, conn, BE_SOCKET, err);
437

    
438
  if (err)
439
    BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
440
  else
441
    BGP_TRACE(D_EVENTS, "Connection closed");
442

    
443
  bgp_conn_enter_idle_state(conn);
444
}
445

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

    
451
  DBG("BGP: Hold timeout\n");
452

    
453
  /* If there is something in input queue, we are probably congested
454
     and perhaps just not processed BGP packets in time. */
455

    
456
  if (sk_rx_ready(conn->sk) > 0)
457
    bgp_start_timer(conn->hold_timer, 10);
458
  else
459
    bgp_error(conn, 4, 0, NULL, 0);
460
}
461

    
462
static void
463
bgp_keepalive_timeout(timer *t)
464
{
465
  struct bgp_conn *conn = t->data;
466

    
467
  DBG("BGP: Keepalive timer\n");
468
  bgp_schedule_packet(conn, PKT_KEEPALIVE);
469
}
470

    
471
static void
472
bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
473
{
474
  timer *t;
475

    
476
  conn->sk = NULL;
477
  conn->bgp = p;
478
  conn->packets_to_send = 0;
479

    
480
  t = conn->connect_retry_timer = tm_new(p->p.pool);
481
  t->hook = bgp_connect_timeout;
482
  t->data = conn;
483
  t = conn->hold_timer = tm_new(p->p.pool);
484
  t->hook = bgp_hold_timeout;
485
  t->data = conn;
486
  t = conn->keepalive_timer = tm_new(p->p.pool);
487
  t->hook = bgp_keepalive_timeout;
488
  t->data = conn;
489
  conn->tx_ev = ev_new(p->p.pool);
490
  conn->tx_ev->hook = bgp_kick_tx;
491
  conn->tx_ev->data = conn;
492
}
493

    
494
static void
495
bgp_setup_sk(struct bgp_proto *p, struct bgp_conn *conn, sock *s)
496
{
497
  s->data = conn;
498
  s->err_hook = bgp_sock_err;
499
  conn->sk = s;
500
}
501

    
502
static void
503
bgp_active(struct bgp_proto *p)
504
{
505
  int delay = MAX(1, p->cf->start_delay_time);
506
  struct bgp_conn *conn = &p->outgoing_conn;
507

    
508
  BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
509
  bgp_setup_conn(p, conn);
510
  bgp_conn_set_state(conn, BS_ACTIVE);
511
  bgp_start_timer(conn->connect_retry_timer, delay);
512
}
513

    
514
int
515
bgp_apply_limits(struct bgp_proto *p)
516
{
517
  if (p->cf->route_limit && (p->p.stats.imp_routes > p->cf->route_limit))
518
    {
519
      log(L_WARN "%s: Route limit exceeded, shutting down", p->p.name);
520
      bgp_store_error(p, NULL, BE_AUTO_DOWN, BEA_ROUTE_LIMIT_EXCEEDED);
521
      bgp_update_startup_delay(p);
522
      bgp_stop(p, 1); // Errcode 6, 1 - max number of prefixes reached
523
      return -1;
524
    }
525

    
526
  return 0;
527
}
528

    
529

    
530
/**
531
 * bgp_connect - initiate an outgoing connection
532
 * @p: BGP instance
533
 *
534
 * The bgp_connect() function creates a new &bgp_conn and initiates
535
 * a TCP connection to the peer. The rest of connection setup is governed
536
 * by the BGP state machine as described in the standard.
537
 */
538
static void
539
bgp_connect(struct bgp_proto *p)        /* Enter Connect state and start establishing connection */
540
{
541
  sock *s;
542
  struct bgp_conn *conn = &p->outgoing_conn;
543

    
544
  DBG("BGP: Connecting\n");
545
  s = sk_new(p->p.pool);
546
  s->type = SK_TCP_ACTIVE;
547
  s->saddr = p->source_addr;
548
  s->daddr = p->cf->remote_ip;
549
  s->dport = BGP_PORT;
550
  s->ttl = p->cf->multihop ? : 1;
551
  s->rbsize = BGP_RX_BUFFER_SIZE;
552
  s->tbsize = BGP_TX_BUFFER_SIZE;
553
  s->tos = IP_PREC_INTERNET_CONTROL;
554
  s->password = p->cf->password;
555
  s->tx_hook = bgp_connected;
556
  BGP_TRACE(D_EVENTS, "Connecting to %I from local address %I", s->daddr, s->saddr);
557
  bgp_setup_conn(p, conn);
558
  bgp_setup_sk(p, conn, s);
559
  bgp_conn_set_state(conn, BS_CONNECT);
560
  if (sk_open(s))
561
    {
562
      bgp_sock_err(s, 0);
563
      return;
564
    }
565
  DBG("BGP: Waiting for connect success\n");
566
  bgp_start_timer(conn->connect_retry_timer, p->cf->connect_retry_time);
567
}
568

    
569
/**
570
 * bgp_incoming_connection - handle an incoming connection
571
 * @sk: TCP socket
572
 * @dummy: unused
573
 *
574
 * This function serves as a socket hook for accepting of new BGP
575
 * connections. It searches a BGP instance corresponding to the peer
576
 * which has connected and if such an instance exists, it creates a
577
 * &bgp_conn structure, attaches it to the instance and either sends
578
 * an Open message or (if there already is an active connection) it
579
 * closes the new connection by sending a Notification message.
580
 */
581
static int
582
bgp_incoming_connection(sock *sk, int dummy UNUSED)
583
{
584
  struct proto_config *pc;
585

    
586
  DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
587
  WALK_LIST(pc, config->protos)
588
    if (pc->protocol == &proto_bgp && pc->proto)
589
      {
590
        struct bgp_proto *p = (struct bgp_proto *) pc->proto;
591
        if (ipa_equal(p->cf->remote_ip, sk->daddr))
592
          {
593
            /* We are in proper state and there is no other incoming connection */
594
            int acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
595
              (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
596

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

    
600
            if (!acc)
601
              goto err;
602

    
603
            bgp_setup_conn(p, &p->incoming_conn);
604
            bgp_setup_sk(p, &p->incoming_conn, sk);
605
            sk_set_ttl(sk, p->cf->multihop ? : 1);
606
            bgp_send_open(&p->incoming_conn);
607
            return 0;
608
          }
609
      }
610

    
611
  log(L_WARN "BGP: Unexpected connect from unknown address %I (port %d)", sk->daddr, sk->dport);
612
 err:
613
  rfree(sk);
614
  return 0;
615
}
616

    
617
static void
618
bgp_listen_sock_err(sock *sk, int err)
619
{
620
  if (err == ECONNABORTED)
621
    log(L_WARN "BGP: Incoming connection aborted");
622
  else
623
    log(L_ERR "BGP: Error on incoming socket: %M", err);
624
}
625

    
626
static sock *
627
bgp_setup_listen_sk(ip_addr addr, unsigned port, u32 flags)
628
{
629
  sock *s = sk_new(&root_pool);
630
  DBG("BGP: Creating incoming socket\n");
631
  s->type = SK_TCP_PASSIVE;
632
  s->saddr = addr;
633
  s->sport = port ? port : BGP_PORT;
634
  s->flags = flags;
635
  s->tos = IP_PREC_INTERNET_CONTROL;
636
  s->rbsize = BGP_RX_BUFFER_SIZE;
637
  s->tbsize = BGP_TX_BUFFER_SIZE;
638
  s->rx_hook = bgp_incoming_connection;
639
  s->err_hook = bgp_listen_sock_err;
640
  if (sk_open(s))
641
    {
642
      log(L_ERR "BGP: Unable to open incoming socket");
643
      rfree(s);
644
      return NULL;
645
    }
646
  else
647
    return s;
648
}
649

    
650
static void
651
bgp_start_neighbor(struct bgp_proto *p)
652
{
653
  p->local_addr = p->neigh->iface->addr->ip;
654
  p->source_addr = ipa_nonzero(p->cf->source_addr) ? p->cf->source_addr : p->local_addr;
655

    
656
  DBG("BGP: local=%I remote=%I\n", p->source_addr, p->next_hop);
657
#ifdef IPV6
658
  {
659
    struct ifa *a;
660
    p->local_link = IPA_NONE;
661
    WALK_LIST(a, p->neigh->iface->addrs)
662
      if (a->scope == SCOPE_LINK)
663
        {
664
          p->local_link = a->ip;
665
          break;
666
        }
667

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

    
671
    DBG("BGP: Selected link-level address %I\n", p->local_link);
672
  }
673
#endif
674

    
675
  int rv = bgp_open(p);
676
  if (rv < 0)
677
    return;
678

    
679
  bgp_initiate(p);
680
}
681

    
682
static void
683
bgp_neigh_notify(neighbor *n)
684
{
685
  struct bgp_proto *p = (struct bgp_proto *) n->proto;
686

    
687
  if (n->iface)
688
    {
689
      if ((p->p.proto_state == PS_START) && (p->start_state == BSS_PREPARE))
690
        {
691
          BGP_TRACE(D_EVENTS, "Neighbor found");
692
          bgp_start_neighbor(p);
693
        }
694
    }
695
  else
696
    {
697
      if ((p->p.proto_state == PS_START) || (p->p.proto_state == PS_UP))
698
        {
699
          BGP_TRACE(D_EVENTS, "Neighbor lost");
700
          bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST);
701
          bgp_stop(p, 0);
702
        }
703
    }
704
}
705

    
706
static int
707
bgp_reload_routes(struct proto *P)
708
{
709
  struct bgp_proto *p = (struct bgp_proto *) P;
710
  if (!p->conn || !p->conn->peer_refresh_support)
711
    return 0;
712

    
713
  bgp_schedule_packet(p->conn, PKT_ROUTE_REFRESH);
714
  return 1;
715
}
716

    
717
static void
718
bgp_start_locked(struct object_lock *lock)
719
{
720
  struct bgp_proto *p = lock->data;
721
  struct bgp_config *cf = p->cf;
722

    
723
  if (p->p.proto_state != PS_START)
724
    {
725
      DBG("BGP: Got lock in different state %d\n", p->p.proto_state);
726
    return;
727
    }
728

    
729
  DBG("BGP: Got lock\n");
730
  p->local_id = proto_get_router_id(&cf->c);
731
  p->next_hop = cf->multihop ? cf->multihop_via : cf->remote_ip;
732
  p->neigh = neigh_find(&p->p, &p->next_hop, NEF_STICKY);
733

    
734
  if (cf->rr_client)
735
    {
736
      p->rr_cluster_id = cf->rr_cluster_id ? cf->rr_cluster_id : p->local_id;
737
      p->rr_client = cf->rr_client;
738
    }
739

    
740
  p->rs_client = cf->rs_client;
741

    
742
  if (!p->neigh)
743
    {
744
      log(L_ERR "%s: Invalid next hop %I", p->p.name, p->next_hop);
745
      /* As we do not start yet, we can just disable protocol */
746
      p->p.disabled = 1;
747
      bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_NEXT_HOP);
748
      proto_notify_state(&p->p, PS_DOWN);
749
      return;
750
    }
751
  
752
  if (p->neigh->iface)
753
    bgp_start_neighbor(p);
754
  else
755
    BGP_TRACE(D_EVENTS, "Waiting for %I to become my neighbor", p->next_hop);
756
}
757

    
758
static int
759
bgp_start(struct proto *P)
760
{
761
  struct bgp_proto *p = (struct bgp_proto *) P;
762
  struct object_lock *lock;
763

    
764
  DBG("BGP: Startup.\n");
765
  p->start_state = BSS_PREPARE;
766
  p->outgoing_conn.state = BS_IDLE;
767
  p->incoming_conn.state = BS_IDLE;
768
  p->neigh = NULL;
769

    
770
  p->event = ev_new(p->p.pool);
771
  p->event->hook = bgp_decision;
772
  p->event->data = p;
773

    
774
  p->startup_timer = tm_new(p->p.pool);
775
  p->startup_timer->hook = bgp_startup_timeout;
776
  p->startup_timer->data = p;
777

    
778
  /*
779
   *  Before attempting to create the connection, we need to lock the
780
   *  port, so that are sure we're the only instance attempting to talk
781
   *  with that neighbor.
782
   */
783

    
784
  lock = p->lock = olock_new(P->pool);
785
  lock->addr = p->cf->remote_ip;
786
  lock->type = OBJLOCK_TCP;
787
  lock->port = BGP_PORT;
788
  lock->iface = NULL;
789
  lock->hook = bgp_start_locked;
790
  lock->data = p;
791
  olock_acquire(lock);
792

    
793
  return PS_START;
794
}
795

    
796
static int
797
bgp_shutdown(struct proto *P)
798
{
799
  struct bgp_proto *p = (struct bgp_proto *) P;
800
  unsigned subcode;
801

    
802
  BGP_TRACE(D_EVENTS, "Shutdown requested");
803
  bgp_store_error(p, NULL, BE_MAN_DOWN, 0);
804

    
805
  if (P->reconfiguring)
806
    {
807
      if (P->cf_new)
808
        subcode = 6; // Errcode 6, 6 - other configuration change
809
      else
810
        subcode = 3; // Errcode 6, 3 - peer de-configured
811
    }
812
  else
813
    subcode = 2; // Errcode 6, 2 - administrative shutdown
814

    
815
  p->startup_delay = 0;
816
  bgp_stop(p, subcode);
817

    
818
  return p->p.proto_state;
819
}
820

    
821
static struct proto *
822
bgp_init(struct proto_config *C)
823
{
824
  struct bgp_config *c = (struct bgp_config *) C;
825
  struct proto *P = proto_new(C, sizeof(struct bgp_proto));
826
  struct bgp_proto *p = (struct bgp_proto *) P;
827

    
828
  P->accept_ra_types = RA_OPTIMAL;
829
  P->rt_notify = bgp_rt_notify;
830
  P->rte_better = bgp_rte_better;
831
  P->import_control = bgp_import_control;
832
  P->neigh_notify = bgp_neigh_notify;
833
  P->reload_routes = bgp_reload_routes;
834
  p->cf = c;
835
  p->local_as = c->local_as;
836
  p->remote_as = c->remote_as;
837
  p->is_internal = (c->local_as == c->remote_as);
838
  return P;
839
}
840

    
841
/**
842
 * bgp_error - report a protocol error
843
 * @c: connection
844
 * @code: error code (according to the RFC)
845
 * @subcode: error sub-code
846
 * @data: data to be passed in the Notification message
847
 * @len: length of the data
848
 *
849
 * bgp_error() sends a notification packet to tell the other side that a protocol
850
 * error has occurred (including the data considered erroneous if possible) and
851
 * closes the connection.
852
 */
853
void
854
bgp_error(struct bgp_conn *c, unsigned code, unsigned subcode, byte *data, int len)
855
{
856
  struct bgp_proto *p = c->bgp;
857

    
858
  if (c->state == BS_CLOSE)
859
    return;
860

    
861
  bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, data, (len > 0) ? len : -len);
862
  bgp_store_error(p, c, BE_BGP_TX, (code << 16) | subcode);
863
  bgp_conn_enter_close_state(c);
864

    
865
  c->notify_code = code;
866
  c->notify_subcode = subcode;
867
  c->notify_data = data;
868
  c->notify_size = (len > 0) ? len : 0;
869
  bgp_schedule_packet(c, PKT_NOTIFICATION);
870

    
871
  if (code != 6)
872
    {
873
      bgp_update_startup_delay(p);
874
      bgp_stop(p, 0);
875
    }
876
}
877

    
878
/**
879
 * bgp_store_error - store last error for status report
880
 * @p: BGP instance
881
 * @c: connection
882
 * @class: error class (BE_xxx constants)
883
 * @code: error code (class specific)
884
 *
885
 * bgp_store_error() decides whether given error is interesting enough
886
 * and store that error to last_error variables of @p
887
 */
888
void
889
bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code)
890
{
891
  /* During PS_UP, we ignore errors on secondary connection */
892
  if ((p->p.proto_state == PS_UP) && c && (c != p->conn))
893
    return;
894

    
895
  /* During PS_STOP, we ignore any errors, as we want to report
896
   * the error that caused transition to PS_STOP
897
   */
898
  if (p->p.proto_state == PS_STOP)
899
    return;
900

    
901
  p->last_error_class = class;
902
  p->last_error_code = code;
903
}
904

    
905
void
906
bgp_check(struct bgp_config *c)
907
{
908
  if (!c->local_as)
909
    cf_error("Local AS number must be set");
910

    
911
  if (!c->remote_as)
912
    cf_error("Neighbor must be configured");
913

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

    
917
  if ((c->local_as != c->remote_as) && (c->rr_client))
918
    cf_error("Only internal neighbor can be RR client");
919

    
920
  if ((c->local_as == c->remote_as) && (c->rs_client))
921
    cf_error("Only external neighbor can be RS client");
922

    
923
  /* Different default based on rs_client */
924
  if (c->missing_lladdr == 0)
925
    c->missing_lladdr = c->rs_client ? MLL_DROP : MLL_SELF;
926
}
927

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

    
933

    
934
static void
935
bgp_get_status(struct proto *P, byte *buf)
936
{
937
  struct bgp_proto *p = (struct bgp_proto *) P;
938

    
939
  const byte *err1 = bgp_err_classes[p->last_error_class];
940
  const byte *err2 = "";
941
  byte errbuf[32];
942

    
943
  switch (p->last_error_class)
944
    {
945
    case BE_MISC:
946
      err2 = bgp_misc_errors[p->last_error_code];
947
      break;
948
    case BE_SOCKET:
949
      err2 = (p->last_error_code == 0) ? "Connection closed" : strerror(p->last_error_code);
950
      break;
951
    case BE_BGP_RX:
952
    case BE_BGP_TX:
953
      err2 = bgp_error_dsc(errbuf, p->last_error_code >> 16, p->last_error_code & 0xFF);
954
      break;
955
    case BE_AUTO_DOWN:
956
      err2 = bgp_auto_errors[p->last_error_code];
957
      break;
958
    }
959

    
960
  if (P->proto_state == PS_DOWN)
961
    bsprintf(buf, "%s%s", err1, err2);
962
  else
963
    bsprintf(buf, "%-14s%s%s",
964
             bgp_state_names[MAX(p->incoming_conn.state, p->outgoing_conn.state)],
965
             err1, err2);
966
}
967

    
968
static int
969
bgp_reconfigure(struct proto *P, struct proto_config *C)
970
{
971
  struct bgp_config *new = (struct bgp_config *) C;
972
  struct bgp_proto *p = (struct bgp_proto *) P;
973
  struct bgp_config *old = p->cf;
974

    
975
  int same = !memcmp(((byte *) old) + sizeof(struct proto_config),
976
                     ((byte *) new) + sizeof(struct proto_config),
977
                     // password item is last and must be checked separately
978
                     OFFSETOF(struct bgp_config, password) - sizeof(struct proto_config))
979
    && ((!old->password && !new->password)
980
        || (old->password && new->password && !strcmp(old->password, new->password)));
981

    
982
  /* We should update our copy of configuration ptr as old configuration will be freed */
983
  if (same)
984
    p->cf = new;
985

    
986
  return same;
987
}
988

    
989
struct protocol proto_bgp = {
990
  name:                        "BGP",
991
  template:                "bgp%d",
992
  attr_class:                EAP_BGP,
993
  init:                        bgp_init,
994
  start:                bgp_start,
995
  shutdown:                bgp_shutdown,
996
  get_status:                bgp_get_status,
997
  get_attr:                bgp_get_attr,
998
  reconfigure:                bgp_reconfigure,
999
  get_route_info:        bgp_get_route_info,
1000
};