Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bgp / bgp.c @ 67cc14ee

History | View | Annotate | Download (72.5 KB)

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

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

    
106
#undef LOCAL_DEBUG
107

    
108
#include <stdlib.h>
109
#include <time.h>
110

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

    
123
#include "bgp.h"
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_update_bfd(struct bgp_proto *p, int use_bfd);
133

    
134
static int bgp_incoming_connection(sock *sk, uint dummy UNUSED);
135
static void bgp_listen_sock_err(sock *sk UNUSED, int err);
136

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

    
155
    /* FIXME: Add some global init? */
156
    if (!bgp_linpool)
157
        init_list(&bgp_sockets);
158

    
159
    /* We assume that cf->iface is defined iff cf->local_ip is link-local */
160

    
161
    WALK_LIST(bs, bgp_sockets)
162
    if (ipa_equal(bs->sk->saddr, addr) && (bs->sk->iface == ifa) && (bs->sk->sport == port))
163
    {
164
        bs->uc++;
165
        p->sock = bs;
166
        return 0;
167
    }
168

    
169
    sock *sk = sk_new(proto_pool);
170
    sk->type = SK_TCP_PASSIVE;
171
    sk->ttl = 255;
172
    sk->saddr = addr;
173
    sk->sport = port;
174
    sk->flags = 0;
175
    sk->tos = IP_PREC_INTERNET_CONTROL;
176
    sk->rbsize = BGP_RX_BUFFER_SIZE;
177
    sk->tbsize = BGP_TX_BUFFER_SIZE;
178
    sk->rx_hook = bgp_incoming_connection;
179
    sk->err_hook = bgp_listen_sock_err;
180

    
181
    if (sk_open(sk) < 0)
182
        goto err;
183

    
184
    bs = mb_allocz(proto_pool, sizeof(struct bgp_socket));
185
    bs->sk = sk;
186
    bs->uc = 1;
187
    p->sock = bs;
188

    
189
    add_tail(&bgp_sockets, &bs->n);
190

    
191
    if (!bgp_linpool)
192
    {
193
        bgp_linpool  = lp_new_default(proto_pool);
194
        bgp_linpool2 = lp_new_default(proto_pool);
195
    }
196

    
197
    return 0;
198

    
199
    err:
200
    sk_log_error(sk, p->p.name);
201
    log(L_ERR "%s: Cannot open listening socket", p->p.name);
202
    rfree(sk);
203
    return -1;
204
}
205

    
206
/**
207
 * bgp_close - close a BGP instance
208
 * @p: BGP instance
209
 *
210
 * This function frees and deconfigures shared BGP resources.
211
 */
212
static void
213
bgp_close(struct bgp_proto *p)
214
{
215
    struct bgp_socket *bs = p->sock;
216

    
217
    ASSERT(bs && bs->uc);
218

    
219
    if (--bs->uc)
220
        return;
221

    
222
    rfree(bs->sk);
223
    rem_node(&bs->n);
224
    mb_free(bs);
225

    
226
    if (!EMPTY_LIST(bgp_sockets))
227
        return;
228

    
229
    rfree(bgp_linpool);
230
    bgp_linpool = NULL;
231

    
232
    rfree(bgp_linpool2);
233
    bgp_linpool2 = NULL;
234
}
235

    
236
static inline int
237
bgp_setup_auth(struct bgp_proto *p, int enable)
238
{
239
    if (p->cf->password)
240
    {
241
        int rv = sk_set_md5_auth(p->sock->sk,
242
                                 p->cf->local_ip, p->cf->remote_ip, p->cf->iface,
243
                                 enable ? p->cf->password : NULL, p->cf->setkey);
244

    
245
        if (rv < 0)
246
            sk_log_error(p->sock->sk, p->p.name);
247

    
248
        return rv;
249
    }
250
    else
251
        return 0;
252
}
253

    
254
static inline struct bgp_channel *
255
bgp_find_channel(struct bgp_proto *p, u32 afi)
256
{
257
    struct bgp_channel *c;
258
    WALK_LIST(c, p->p.channels)
259
    if (c->afi == afi)
260
        return c;
261

    
262
    return NULL;
263
}
264

    
265
static void
266
bgp_startup(struct bgp_proto *p)
267
{
268
    //sameMetricUpdater = 3;
269
    BGP_TRACE(D_EVENTS, "Started");
270
    p->start_state = BSS_CONNECT;
271

    
272
    if (!p->cf->passive)
273
        bgp_active(p);
274

    
275
    //initRTmap();
276
    //loadComplessivo = 0;
277
    sent_prefix_slab = NULL;
278
}
279

    
280
static void
281
bgp_startup_timeout(timer *t)
282
{
283
    bgp_startup(t->data);
284
}
285

    
286
static inline struct bgp_channel *
287
bgp_get_channel_to_send(struct bgp_proto *p, struct bgp_conn *conn)
288
{
289
    uint i = conn->last_channel;
290

    
291
    /* Try the last channel, but at most several times */
292
    if ((conn->channels_to_send & (1 << i)) &&
293
        (conn->last_channel_count < 16))
294
        goto found;
295

    
296
    /* Find channel with non-zero channels_to_send */
297
    do
298
    {
299
        i++;
300
        if (i >= p->channel_count)
301
            i = 0;
302
    }
303
    while (! (conn->channels_to_send & (1 << i)));
304

    
305
    /* Use that channel */
306
    conn->last_channel = i;
307
    conn->last_channel_count = 0;
308

    
309
    found:
310
    conn->last_channel_count++;
311
    return p->channel_map[i];
312
}
313

    
314
void
315
conn_mrai_timeout(timer *t)
316
{
317
    //How to extract data from the timer
318
    struct bgp_conn *conn = t->data;
319
    struct bgp_proto *p = conn->bgp;
320

    
321
    BGP_TRACE(D_EVENTS, "MRAI timeout");
322
    bgp_fire_tx(conn);
323
}
324

    
325
void
326
dest_mrai_timeout(timer *t)
327
{
328
    list *connections = t->data;
329
    /* If there is something into the list i will trigger an update on the first connection FCFS*/
330
    if (list_length(connections) > 0) {
331
        struct conn_list_node *head = HEAD(*connections);
332
        struct bgp_conn *conn = head->conn;
333
        struct bgp_proto *p = conn->bgp;
334
        BGP_TRACE(D_EVENTS, "MRAI timeout");
335

    
336
        bgp_fire_tx(conn);
337
        bgp_free_conn_from_prefix(head);
338
    }
339
}
340

    
341
static void
342
bgp_initiate(struct bgp_proto *p)
343
{
344
    int err_val;
345

    
346
    if (bgp_open(p) < 0)
347
    { err_val = BEM_NO_SOCKET; goto err1; }
348

    
349
    if (bgp_setup_auth(p, 1) < 0)
350
    { err_val = BEM_INVALID_MD5; goto err2; }
351

    
352
    if (p->cf->bfd)
353
        bgp_update_bfd(p, p->cf->bfd);
354

    
355

    
356

    
357
    if (p->startup_delay)
358
    {
359
        p->start_state = BSS_DELAY;
360
        BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds due to errors", p->startup_delay);
361
        bgp_start_timer(p->startup_timer, p->startup_delay);
362
    }
363
    else
364
        bgp_startup(p);
365

    
366
    return;
367

    
368
    err2:
369
    bgp_close(p);
370
    err1:
371
    p->p.disabled = 1;
372
    bgp_store_error(p, NULL, BE_MISC, err_val);
373
    proto_notify_state(&p->p, PS_DOWN);
374

    
375
    return;
376
}
377

    
378
/**
379
 * bgp_start_timer - start a BGP timer
380
 * @t: timer
381
 * @value: time (in seconds) to fire (0 to disable the timer)
382
 *
383
 * This functions calls tm_start() on @t with time @value and the amount of
384
 * randomization suggested by the BGP standard. Please use it for all BGP
385
 * timers.
386
 */
387
void
388
bgp_start_timer(timer *t, uint value)
389
{
390
    if (value)
391
    {
392
        /* The randomization procedure is specified in RFC 4271 section 10 */
393
        btime time = value S;
394
        btime randomize = random() % ((time / 4) + 1);
395
        tm_start(t, time - randomize);
396
    }
397
    else
398
        tm_stop(t);
399
}
400

    
401
void
402
bgp_start_ms_timer(timer *t, uint value)
403
{
404
    if (value)
405
    {
406
        /* The randomization procedure is specified in RFC 4271 section 10 */
407
        btime time = value MS;
408
        btime randomize = random() % ((time / 20) + 1);
409
        log(L_INFO "Timer avviato con un delay di %d ms", time - randomize);
410
        tm_start(t, time - randomize);
411
    }
412
    else
413
        tm_stop(t);
414
}
415

    
416
/**
417
 * bgp_close_conn - close a BGP connection
418
 * @conn: connection to close
419
 *
420
 * This function takes a connection described by the &bgp_conn structure, closes
421
 * its socket and frees all resources associated with it.
422
 */
423
void
424
bgp_close_conn(struct bgp_conn *conn)
425
{
426
    DBG("BGP: Closing connection\n");
427

    
428
    //log(L_INFO "AS del vicino con cui si vuole chiudere la connessione: %d",conn->bgp->remote_as);
429
    const char *keyRTmap;
430
    map_iter_t iterRTmap;
431
    const char *keyNHmap_RTmap;
432
    map_iter_t iterNHmap_RTmap;
433

    
434
    //NH removing from the data structure
435
    /*iterRTmap = map_iter(&RTmap);
436
    while ((keyRTmap = map_next(&RTmap, &iterRTmap))) {
437
        RTable *RTmap_element = map_get(&RTmap, keyRTmap);
438
        iterNHmap_RTmap = map_iter(&RTmap_element->NH);
439
        while ((keyNHmap_RTmap = map_next(&RTmap_element->NH, &iterNHmap_RTmap))) {
440
            int *NH_AS_value = map_get(&RTmap_element->NH, keyNHmap_RTmap);
441
            if(NH_AS_value != NULL) {
442
                if (*NH_AS_value == conn->bgp->remote_as)
443
                    map_remove(&RTmap, keyRTmap);
444
            }
445
        }
446

447
        //Load removing from the data structure
448
        const char *keyLoadInmap_RTmap;
449
        map_iter_t iterLoadInmap_RTmap = map_iter(&RTmap_element->loadin);
450
        while ((keyLoadInmap_RTmap = map_next(&RTmap_element->loadin, &iterLoadInmap_RTmap))) {
451
            int keyValue = (int) strtol(keyLoadInmap_RTmap, (char **)NULL, 16);
452

453
            if(keyValue == conn->bgp->remote_as)
454
                map_remove(&RTmap_element->loadin, keyLoadInmap_RTmap);
455
        }
456
    }*/
457

    
458
    conn->packets_to_send = 0;
459
    conn->channels_to_send = 0;
460
    rfree(conn->connect_timer);
461
    conn->connect_timer = NULL;
462
    rfree(conn->keepalive_timer);
463
    conn->keepalive_timer = NULL;
464
    rfree(conn->hold_timer);
465
    conn->hold_timer = NULL;
466

    
467
    /* My MRAI timer */
468
    rfree(conn->conn_mrai_timer);
469
    conn->conn_mrai_timer = NULL;
470

    
471
    rfree(conn->tx_ev);
472
    conn->tx_ev = NULL;
473
    rfree(conn->sk);
474
    conn->sk = NULL;
475

    
476
    mb_free(conn->local_caps);
477
    conn->local_caps = NULL;
478
    mb_free(conn->remote_caps);
479
    conn->remote_caps = NULL;
480
}
481

    
482
/**
483
 * bgp_update_startup_delay - update a startup delay
484
 * @p: BGP instance
485
 *
486
 * This function updates a startup delay that is used to postpone next BGP
487
 * connect. It also handles disable_after_error and might stop BGP instance
488
 * when error happened and disable_after_error is on.
489
 *
490
 * It should be called when BGP protocol error happened.
491
 */
492
void
493
bgp_update_startup_delay(struct bgp_proto *p)
494
{
495
    struct bgp_config *cf = p->cf;
496

    
497
    DBG("BGP: Updating startup delay\n");
498

    
499
    if (p->last_proto_error && ((current_time() - p->last_proto_error) >= cf->error_amnesia_time S))
500
    p->startup_delay = 0;
501

    
502
    p->last_proto_error = current_time();
503

    
504
    if (cf->disable_after_error)
505
    {
506
        p->startup_delay = 0;
507
        p->p.disabled = 1;
508
        return;
509
    }
510

    
511
    if (!p->startup_delay)
512
        p->startup_delay = cf->error_delay_time_min;
513
    else
514
        p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max);
515
}
516

    
517
static void
518
bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode, byte *data, uint len)
519
{
520
    switch (conn->state)
521
    {
522
        case BS_IDLE:
523
        case BS_CLOSE:
524
            return;
525

    
526
        case BS_CONNECT:
527
        case BS_ACTIVE:
528
            bgp_conn_enter_idle_state(conn);
529
            return;
530

    
531
        case BS_OPENSENT:
532
        case BS_OPENCONFIRM:
533
        case BS_ESTABLISHED:
534
            bgp_error(conn, 6, subcode, data, len);
535
            return;
536

    
537
        default:
538
            bug("bgp_graceful_close_conn: Unknown state %d", conn->state);
539
    }
540
}
541

    
542
static void
543
bgp_down(struct bgp_proto *p)
544
{
545
    if (p->start_state > BSS_PREPARE)
546
    {
547
        bgp_setup_auth(p, 0);
548
        bgp_close(p);
549
    }
550

    
551
    BGP_TRACE(D_EVENTS, "Down");
552
    proto_notify_state(&p->p, PS_DOWN);
553
}
554

    
555
static void
556
bgp_decision(void *vp)
557
{
558
    struct bgp_proto *p = vp;
559

    
560
    DBG("BGP: Decision start\n");
561
    if ((p->p.proto_state == PS_START) &&
562
        (p->outgoing_conn.state == BS_IDLE) &&
563
        (p->incoming_conn.state != BS_OPENCONFIRM) &&
564
        !p->cf->passive)
565
        bgp_active(p);
566

    
567
    if ((p->p.proto_state == PS_STOP) &&
568
        (p->outgoing_conn.state == BS_IDLE) &&
569
        (p->incoming_conn.state == BS_IDLE))
570
        bgp_down(p);
571
}
572

    
573
void
574
bgp_stop(struct bgp_proto *p, uint subcode, byte *data, uint len)
575
{
576
    proto_notify_state(&p->p, PS_STOP);
577
    bgp_graceful_close_conn(&p->outgoing_conn, subcode, data, len);
578
    bgp_graceful_close_conn(&p->incoming_conn, subcode, data, len);
579
    ev_schedule(p->event);
580
}
581

    
582
static inline void
583
bgp_conn_set_state(struct bgp_conn *conn, uint new_state)
584
{
585
    if (conn->bgp->p.mrtdump & MD_STATES)
586
        mrt_dump_bgp_state_change(conn, conn->state, new_state);
587

    
588
    conn->state = new_state;
589
}
590

    
591
void
592
bgp_conn_enter_openconfirm_state(struct bgp_conn *conn)
593
{
594
    /* Really, most of the work is done in bgp_rx_open(). */
595
    bgp_conn_set_state(conn, BS_OPENCONFIRM);
596
}
597

    
598
static const struct bgp_af_caps dummy_af_caps = { };
599

    
600
void
601
bgp_conn_enter_established_state(struct bgp_conn *conn)
602
{
603
    struct bgp_proto *p = conn->bgp;
604
    struct bgp_caps *local = conn->local_caps;
605
    struct bgp_caps *peer = conn->remote_caps;
606
    struct bgp_channel *c;
607

    
608
    BGP_TRACE(D_EVENTS, "BGP session established");
609

    
610
    /* For multi-hop BGP sessions */
611
    if (ipa_zero(p->source_addr))
612
        p->source_addr = conn->sk->saddr;
613

    
614
    conn->sk->fast_rx = 0;
615

    
616
    p->conn = conn;
617
    p->last_error_class = 0;
618
    p->last_error_code = 0;
619

    
620
    p->as4_session = conn->as4_session;
621

    
622
    p->route_refresh = peer->route_refresh;
623
    p->enhanced_refresh = local->enhanced_refresh && peer->enhanced_refresh;
624

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

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

    
631
    if (p->gr_active_num)
632
        tm_stop(p->gr_timer);
633

    
634
    /* Number of active channels */
635
    int num = 0;
636

    
637
    WALK_LIST(c, p->p.channels)
638
    {
639
        const struct bgp_af_caps *loc = bgp_find_af_caps(local, c->afi);
640
        const struct bgp_af_caps *rem = bgp_find_af_caps(peer,  c->afi);
641

    
642
        /* Ignore AFIs that were not announced in multiprotocol capability */
643
        if (!loc || !loc->ready)
644
            loc = &dummy_af_caps;
645

    
646
        if (!rem || !rem->ready)
647
            rem = &dummy_af_caps;
648

    
649
        int active = loc->ready && rem->ready;
650
        c->c.disabled = !active;
651
        c->c.reloadable = p->route_refresh;
652

    
653
        c->index = active ? num++ : 0;
654

    
655
        c->feed_state = BFS_NONE;
656
        c->load_state = BFS_NONE;
657

    
658
        /* Channels where peer may do GR */
659
        c->gr_ready = active && local->gr_aware && rem->gr_able;
660
        p->gr_ready = p->gr_ready || c->gr_ready;
661

    
662
        /* Channels not able to recover gracefully */
663
        if (p->p.gr_recovery && (!active || !peer_gr_ready))
664
            channel_graceful_restart_unlock(&c->c);
665

    
666
        /* Channels waiting for local convergence */
667
        if (p->p.gr_recovery && loc->gr_able && peer_gr_ready)
668
            c->c.gr_wait = 1;
669

    
670
        /* Channels where peer is not able to recover gracefully */
671
        if (c->gr_active && ! (c->gr_ready && (rem->gr_af_flags & BGP_GRF_FORWARDING)))
672
            bgp_graceful_restart_done(c);
673

    
674
        /* GR capability implies that neighbor will send End-of-RIB */
675
        if (peer->gr_aware)
676
            c->load_state = BFS_LOADING;
677

    
678
        c->ext_next_hop = c->cf->ext_next_hop && (bgp_channel_is_ipv6(c) || rem->ext_next_hop);
679
        c->add_path_rx = (loc->add_path & BGP_ADD_PATH_RX) && (rem->add_path & BGP_ADD_PATH_TX);
680
        c->add_path_tx = (loc->add_path & BGP_ADD_PATH_TX) && (rem->add_path & BGP_ADD_PATH_RX);
681

    
682
        /* Update RA mode */
683
        if (c->add_path_tx)
684
            c->c.ra_mode = RA_ANY;
685
        else if (c->cf->secondary)
686
            c->c.ra_mode = RA_ACCEPTED;
687
        else
688
            c->c.ra_mode = RA_OPTIMAL;
689
    }
690

    
691
    p->afi_map = mb_alloc(p->p.pool, num * sizeof(u32));
692
    p->channel_map = mb_alloc(p->p.pool, num * sizeof(void *));
693
    p->channel_count = num;
694

    
695
    WALK_LIST(c, p->p.channels)
696
    {
697
        if (c->c.disabled)
698
            continue;
699

    
700
        p->afi_map[c->index] = c->afi;
701
        p->channel_map[c->index] = c;
702
    }
703

    
704
    /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */
705

    
706
    bgp_conn_set_state(conn, BS_ESTABLISHED);
707
    proto_notify_state(&p->p, PS_UP);
708
}
709

    
710
static void
711
bgp_conn_leave_established_state(struct bgp_proto *p)
712
{
713
    BGP_TRACE(D_EVENTS, "BGP session closed");
714
    p->conn = NULL;
715

    
716
    if (p->p.proto_state == PS_UP)
717
        bgp_stop(p, 0, NULL, 0);
718
}
719

    
720
void
721
bgp_conn_enter_close_state(struct bgp_conn *conn)
722
{
723
    struct bgp_proto *p = conn->bgp;
724
    int os = conn->state;
725

    
726
    bgp_conn_set_state(conn, BS_CLOSE);
727
    tm_stop(conn->keepalive_timer);
728
    conn->sk->rx_hook = NULL;
729

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

    
733
    if (os == BS_ESTABLISHED)
734
        bgp_conn_leave_established_state(p);
735
}
736

    
737
void
738
bgp_conn_enter_idle_state(struct bgp_conn *conn)
739
{
740
    struct bgp_proto *p = conn->bgp;
741
    int os = conn->state;
742

    
743
    bgp_close_conn(conn);
744
    bgp_conn_set_state(conn, BS_IDLE);
745
    ev_schedule(p->event);
746

    
747
    if (os == BS_ESTABLISHED)
748
        bgp_conn_leave_established_state(p);
749
}
750

    
751
/**
752
 * bgp_handle_graceful_restart - handle detected BGP graceful restart
753
 * @p: BGP instance
754
 *
755
 * This function is called when a BGP graceful restart of the neighbor is
756
 * detected (when the TCP connection fails or when a new TCP connection
757
 * appears). The function activates processing of the restart - starts routing
758
 * table refresh cycle and activates BGP restart timer. The protocol state goes
759
 * back to %PS_START, but changing BGP state back to %BS_IDLE is left for the
760
 * caller.
761
 */
762
void
763
bgp_handle_graceful_restart(struct bgp_proto *p)
764
{
765
    ASSERT(p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready);
766

    
767
    BGP_TRACE(D_EVENTS, "Neighbor graceful restart detected%s",
768
              p->gr_active_num ? " - already pending" : "");
769

    
770
    p->gr_active_num = 0;
771

    
772
    struct bgp_channel *c;
773
    WALK_LIST(c, p->p.channels)
774
    {
775
        /* FIXME: perhaps check for channel state instead of disabled flag? */
776
        if (c->c.disabled)
777
            continue;
778

    
779
        if (c->gr_ready)
780
        {
781
            if (c->gr_active)
782
                rt_refresh_end(c->c.table, &c->c);
783

    
784
            c->gr_active = 1;
785
            p->gr_active_num++;
786
            rt_refresh_begin(c->c.table, &c->c);
787
        }
788
        else
789
        {
790
            /* Just flush the routes */
791
            rt_refresh_begin(c->c.table, &c->c);
792
            rt_refresh_end(c->c.table, &c->c);
793
        }
794

    
795
        /* Reset bucket and prefix tables */
796
        bgp_free_bucket_table(c);
797
        bgp_free_prefix_table(c);
798
        bgp_init_bucket_table(c);
799
        bgp_init_prefix_table(c);
800
        c->packets_to_send = 0;
801

    
802
        /* Reset MRAI timer */
803
        if(tm_active(p->conn->conn_mrai_timer)){
804
            tm_stop(p->conn->conn_mrai_timer);
805
        }
806
    }
807

    
808
    /* p->gr_ready -> at least one active channel is c->gr_ready */
809
    ASSERT(p->gr_active_num > 0);
810

    
811
    proto_notify_state(&p->p, PS_START);
812
    bgp_start_timer(p->gr_timer, p->conn->remote_caps->gr_time);
813
}
814

    
815
/**
816
 * bgp_graceful_restart_done - finish active BGP graceful restart
817
 * @c: BGP channel
818
 *
819
 * This function is called when the active BGP graceful restart of the neighbor
820
 * should be finished for channel @c - either successfully (the neighbor sends
821
 * all paths and reports end-of-RIB for given AFI/SAFI on the new session) or
822
 * unsuccessfully (the neighbor does not support BGP graceful restart on the new
823
 * session). The function ends the routing table refresh cycle.
824
 */
825
void
826
bgp_graceful_restart_done(struct bgp_channel *c)
827
{
828
    struct bgp_proto *p = (void *) c->c.proto;
829

    
830
    ASSERT(c->gr_active);
831
    c->gr_active = 0;
832
    p->gr_active_num--;
833

    
834
    if (!p->gr_active_num)
835
        BGP_TRACE(D_EVENTS, "Neighbor graceful restart done");
836

    
837
    rt_refresh_end(c->c.table, &c->c);
838
}
839

    
840
/**
841
 * bgp_graceful_restart_timeout - timeout of graceful restart 'restart timer'
842
 * @t: timer
843
 *
844
 * This function is a timeout hook for @gr_timer, implementing BGP restart time
845
 * limit for reestablisment of the BGP session after the graceful restart. When
846
 * fired, we just proceed with the usual protocol restart.
847
 */
848

    
849
static void
850
bgp_graceful_restart_timeout(timer *t)
851
{
852
    struct bgp_proto *p = t->data;
853

    
854
    BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout");
855
    bgp_stop(p, 0, NULL, 0);
856
}
857

    
858

    
859
/**
860
 * bgp_refresh_begin - start incoming enhanced route refresh sequence
861
 * @c: BGP channel
862
 *
863
 * This function is called when an incoming enhanced route refresh sequence is
864
 * started by the neighbor, demarcated by the BoRR packet. The function updates
865
 * the load state and starts the routing table refresh cycle. Note that graceful
866
 * restart also uses routing table refresh cycle, but RFC 7313 and load states
867
 * ensure that these two sequences do not overlap.
868
 */
869
void
870
bgp_refresh_begin(struct bgp_channel *c)
871
{
872
    struct bgp_proto *p = (void *) c->c.proto;
873

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

    
877
    c->load_state = BFS_REFRESHING;
878
    rt_refresh_begin(c->c.table, &c->c);
879
}
880

    
881
/**
882
 * bgp_refresh_end - finish incoming enhanced route refresh sequence
883
 * @c: BGP channel
884
 *
885
 * This function is called when an incoming enhanced route refresh sequence is
886
 * finished by the neighbor, demarcated by the EoRR packet. The function updates
887
 * the load state and ends the routing table refresh cycle. Routes not received
888
 * during the sequence are removed by the nest.
889
 */
890
void
891
bgp_refresh_end(struct bgp_channel *c)
892
{
893
    struct bgp_proto *p = (void *) c->c.proto;
894

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

    
898
    c->load_state = BFS_NONE;
899
    rt_refresh_end(c->c.table, &c->c);
900
}
901

    
902

    
903
static void
904
bgp_send_open(struct bgp_conn *conn)
905
{
906
    DBG("BGP: Sending open\n");
907
    conn->sk->rx_hook = bgp_rx;
908
    conn->sk->tx_hook = bgp_tx;
909
    tm_stop(conn->connect_timer);
910
    bgp_schedule_packet(conn, NULL, PKT_OPEN);
911
    bgp_conn_set_state(conn, BS_OPENSENT);
912
    bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
913
}
914

    
915
static void
916
bgp_connected(sock *sk)
917
{
918
    struct bgp_conn *conn = sk->data;
919
    struct bgp_proto *p = conn->bgp;
920

    
921
    BGP_TRACE(D_EVENTS, "Connected");
922
    bgp_send_open(conn);
923
}
924

    
925
static void
926
bgp_connect_timeout(timer *t)
927
{
928
    struct bgp_conn *conn = t->data;
929
    struct bgp_proto *p = conn->bgp;
930

    
931
    DBG("BGP: connect_timeout\n");
932
    if (p->p.proto_state == PS_START)
933
    {
934
        bgp_close_conn(conn);
935
        bgp_connect(p);
936
    }
937
    else
938
        bgp_conn_enter_idle_state(conn);
939
}
940

    
941
static void
942
bgp_sock_err(sock *sk, int err)
943
{
944
    struct bgp_conn *conn = sk->data;
945
    struct bgp_proto *p = conn->bgp;
946

    
947
    /*
948
     * This error hook may be called either asynchronously from main
949
     * loop, or synchronously from sk_send().  But sk_send() is called
950
     * only from bgp_tx() and bgp_kick_tx(), which are both called
951
     * asynchronously from main loop. Moreover, they end if err hook is
952
     * called. Therefore, we could suppose that it is always called
953
     * asynchronously.
954
     */
955

    
956
    bgp_store_error(p, conn, BE_SOCKET, err);
957

    
958
    if (err)
959
        BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
960
    else
961
        BGP_TRACE(D_EVENTS, "Connection closed");
962

    
963
    if ((conn->state == BS_ESTABLISHED) && p->gr_ready)
964
        bgp_handle_graceful_restart(p);
965

    
966
    bgp_conn_enter_idle_state(conn);
967
}
968

    
969
static void
970
bgp_hold_timeout(timer *t)
971
{
972
    struct bgp_conn *conn = t->data;
973
    struct bgp_proto *p = conn->bgp;
974

    
975
    DBG("BGP: Hold timeout\n");
976

    
977
    /* We are already closing the connection - just do hangup */
978
    if (conn->state == BS_CLOSE)
979
    {
980
        BGP_TRACE(D_EVENTS, "Connection stalled");
981
        bgp_conn_enter_idle_state(conn);
982
        return;
983
    }
984

    
985
    /* If there is something in input queue, we are probably congested
986
       and perhaps just not processed BGP packets in time. */
987

    
988
    if (sk_rx_ready(conn->sk) > 0)
989
        bgp_start_timer(conn->hold_timer, 10);
990
    else
991
        bgp_error(conn, 4, 0, NULL, 0);
992
}
993

    
994
static void
995
bgp_keepalive_timeout(timer *t)
996
{
997
    struct bgp_conn *conn = t->data;
998

    
999
    DBG("BGP: Keepalive timer\n");
1000
    bgp_schedule_packet(conn, NULL, PKT_KEEPALIVE);
1001

    
1002
    /* Kick TX a bit faster */
1003
    if (ev_active(conn->tx_ev))
1004
        ev_run(conn->tx_ev);
1005
}
1006

    
1007
static void
1008
bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
1009
{
1010
    conn->sk = NULL;
1011
    conn->bgp = p;
1012

    
1013
    conn->packets_to_send = 0;
1014
    conn->channels_to_send = 0;
1015
    conn->last_channel = 0;
1016
    conn->last_channel_count = 0;
1017
    p->number_of_update_sent = 0;
1018

    
1019
    conn->connect_timer        = tm_new_init(p->p.pool, bgp_connect_timeout,         conn, 0, 0);
1020
    conn->hold_timer         = tm_new_init(p->p.pool, bgp_hold_timeout,         conn, 0, 0);
1021
    conn->keepalive_timer        = tm_new_init(p->p.pool, bgp_keepalive_timeout, conn, 0, 0);
1022

    
1023
    //global_mrai_timer->data = (void *)p->p.pool;
1024
    /* My personal timer */
1025
    conn->conn_mrai_timer = tm_new_init(p->p.pool, conn_mrai_timeout, conn, 0, 0);
1026

    
1027
    conn->tx_ev = ev_new(p->p.pool);
1028
    conn->tx_ev->hook = bgp_kick_tx;
1029
    conn->tx_ev->data = conn;
1030
}
1031

    
1032
static void
1033
bgp_setup_sk(struct bgp_conn *conn, sock *s)
1034
{
1035
    s->data = conn;
1036
    s->err_hook = bgp_sock_err;
1037
    s->fast_rx = 1;
1038
    conn->sk = s;
1039
}
1040

    
1041
static void
1042
bgp_active(struct bgp_proto *p)
1043
{
1044
    int delay = MAX(1, p->cf->connect_delay_time);
1045
    struct bgp_conn *conn = &p->outgoing_conn;
1046

    
1047
    BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
1048
    bgp_setup_conn(p, conn);
1049
    bgp_conn_set_state(conn, BS_ACTIVE);
1050
    bgp_start_timer(conn->connect_timer, delay);
1051
}
1052

    
1053
/**
1054
 * bgp_connect - initiate an outgoing connection
1055
 * @p: BGP instance
1056
 *
1057
 * The bgp_connect() function creates a new &bgp_conn and initiates
1058
 * a TCP connection to the peer. The rest of connection setup is governed
1059
 * by the BGP state machine as described in the standard.
1060
 */
1061
static void
1062
bgp_connect(struct bgp_proto *p)        /* Enter Connect state and start establishing connection */
1063
{
1064
    struct bgp_conn *conn = &p->outgoing_conn;
1065
    int hops = p->cf->multihop ? : 1;
1066

    
1067
    srand((unsigned int)time(NULL));
1068

    
1069
    DBG("BGP: Connecting\n");
1070
    sock *s = sk_new(p->p.pool);
1071
    s->type = SK_TCP_ACTIVE;
1072
    s->saddr = p->source_addr;
1073
    s->daddr = p->cf->remote_ip;
1074
    s->dport = p->cf->remote_port;
1075
    s->iface = p->neigh ? p->neigh->iface : NULL;
1076
    s->vrf = p->p.vrf;
1077
    s->ttl = p->cf->ttl_security ? 255 : hops;
1078
    s->rbsize = p->cf->enable_extended_messages ? BGP_RX_BUFFER_EXT_SIZE : BGP_RX_BUFFER_SIZE;
1079
    s->tbsize = p->cf->enable_extended_messages ? BGP_TX_BUFFER_EXT_SIZE : BGP_TX_BUFFER_SIZE;
1080
    s->tos = IP_PREC_INTERNET_CONTROL;
1081
    s->password = p->cf->password;
1082
    s->tx_hook = bgp_connected;
1083
    BGP_TRACE(D_EVENTS, "Connecting to %I%J from local address %I%J", s->daddr, p->cf->iface,
1084
              s->saddr, ipa_is_link_local(s->saddr) ? s->iface : NULL);
1085
    bgp_setup_conn(p, conn);
1086
    bgp_setup_sk(conn, s);
1087
    bgp_conn_set_state(conn, BS_CONNECT);
1088

    
1089
    if (sk_open(s) < 0)
1090
        goto err;
1091

    
1092
    /* Set minimal receive TTL if needed */
1093
    if (p->cf->ttl_security)
1094
        if (sk_set_min_ttl(s, 256 - hops) < 0)
1095
            goto err;
1096

    
1097
    DBG("BGP: Waiting for connect success\n");
1098
    bgp_start_timer(conn->connect_timer, p->cf->connect_retry_time);
1099
    return;
1100

    
1101
    err:
1102
    sk_log_error(s, p->p.name);
1103
    bgp_sock_err(s, 0);
1104
    return;
1105
}
1106

    
1107
/**
1108
 * bgp_find_proto - find existing proto for incoming connection
1109
 * @sk: TCP socket
1110
 *
1111
 */
1112
static struct bgp_proto *
1113
bgp_find_proto(sock *sk)
1114
{
1115
    struct bgp_proto *p;
1116

    
1117
    WALK_LIST(p, proto_list)
1118
    if ((p->p.proto == &proto_bgp) &&
1119
        ipa_equal(p->cf->remote_ip, sk->daddr) &&
1120
        (!p->cf->iface  || (p->cf->iface == sk->iface)) &&
1121
        (ipa_zero(p->cf->local_ip) || ipa_equal(p->cf->local_ip, sk->saddr)) &&
1122
        (p->cf->local_port == sk->sport))
1123
        return p;
1124

    
1125
    return NULL;
1126
}
1127

    
1128
/**
1129
 * bgp_incoming_connection - handle an incoming connection
1130
 * @sk: TCP socket
1131
 * @dummy: unused
1132
 *
1133
 * This function serves as a socket hook for accepting of new BGP
1134
 * connections. It searches a BGP instance corresponding to the peer
1135
 * which has connected and if such an instance exists, it creates a
1136
 * &bgp_conn structure, attaches it to the instance and either sends
1137
 * an Open message or (if there already is an active connection) it
1138
 * closes the new connection by sending a Notification message.
1139
 */
1140
static int
1141
bgp_incoming_connection(sock *sk, uint dummy UNUSED)
1142
{
1143
    struct bgp_proto *p;
1144
    int acc, hops;
1145

    
1146
    DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
1147
    p = bgp_find_proto(sk);
1148
    if (!p)
1149
    {
1150
        log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)",
1151
                sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, sk->dport);
1152
        rfree(sk);
1153
        return 0;
1154
    }
1155

    
1156
    /*
1157
     * BIRD should keep multiple incoming connections in OpenSent state (for
1158
     * details RFC 4271 8.2.1 par 3), but it keeps just one. Duplicate incoming
1159
     * connections are rejected istead. The exception is the case where an
1160
     * incoming connection triggers a graceful restart.
1161
     */
1162

    
1163
    acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
1164
          (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
1165

    
1166
    if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
1167
    {
1168
        bgp_store_error(p, NULL, BE_MISC, BEM_GRACEFUL_RESTART);
1169
        bgp_handle_graceful_restart(p);
1170
        bgp_conn_enter_idle_state(p->conn);
1171
        acc = 1;
1172

    
1173
        /* There might be separate incoming connection in OpenSent state */
1174
        if (p->incoming_conn.state > BS_ACTIVE)
1175
            bgp_close_conn(&p->incoming_conn);
1176
    }
1177

    
1178
    BGP_TRACE(D_EVENTS, "Incoming connection from %I%J (port %d) %s",
1179
              sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL,
1180
              sk->dport, acc ? "accepted" : "rejected");
1181

    
1182
    if (!acc)
1183
    {
1184
        rfree(sk);
1185
        return 0;
1186
    }
1187

    
1188
    hops = p->cf->multihop ? : 1;
1189

    
1190
    if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0)
1191
        goto err;
1192

    
1193
    if (p->cf->ttl_security)
1194
        if (sk_set_min_ttl(sk, 256 - hops) < 0)
1195
            goto err;
1196

    
1197
    if (p->cf->enable_extended_messages)
1198
    {
1199
        sk->rbsize = BGP_RX_BUFFER_EXT_SIZE;
1200
        sk->tbsize = BGP_TX_BUFFER_EXT_SIZE;
1201
        sk_reallocate(sk);
1202
    }
1203

    
1204
    bgp_setup_conn(p, &p->incoming_conn);
1205
    bgp_setup_sk(&p->incoming_conn, sk);
1206
    bgp_send_open(&p->incoming_conn);
1207
    return 0;
1208

    
1209
    err:
1210
    sk_log_error(sk, p->p.name);
1211
    log(L_ERR "%s: Incoming connection aborted", p->p.name);
1212
    rfree(sk);
1213
    return 0;
1214
}
1215

    
1216
static void
1217
bgp_listen_sock_err(sock *sk UNUSED, int err)
1218
{
1219
    if (err == ECONNABORTED)
1220
        log(L_WARN "BGP: Incoming connection aborted");
1221
    else
1222
    log(L_ERR "BGP: Error on listening socket: %M", err);
1223
}
1224

    
1225
static void
1226
bgp_start_neighbor(struct bgp_proto *p)
1227
{
1228
    /* Called only for single-hop BGP sessions */
1229

    
1230
    if (ipa_zero(p->source_addr))
1231
        p->source_addr = p->neigh->ifa->ip;
1232

    
1233
    if (ipa_is_link_local(p->source_addr))
1234
        p->link_addr = p->source_addr;
1235
    else if (p->neigh->iface->llv6)
1236
        p->link_addr = p->neigh->iface->llv6->ip;
1237

    
1238
    bgp_initiate(p);
1239
}
1240

    
1241
static void
1242
bgp_neigh_notify(neighbor *n)
1243
{
1244
    struct bgp_proto *p = (struct bgp_proto *) n->proto;
1245
    int ps = p->p.proto_state;
1246

    
1247
    if (n != p->neigh)
1248
        return;
1249

    
1250
    if ((ps == PS_DOWN) || (ps == PS_STOP))
1251
        return;
1252

    
1253
    int prepare = (ps == PS_START) && (p->start_state == BSS_PREPARE);
1254

    
1255
    if (n->scope <= 0)
1256
    {
1257
        if (!prepare)
1258
        {
1259
            BGP_TRACE(D_EVENTS, "Neighbor lost");
1260
            bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST);
1261
            /* Perhaps also run bgp_update_startup_delay(p)? */
1262
            bgp_stop(p, 0, NULL, 0);
1263
        }
1264
    }
1265
    else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1266
    {
1267
        if (!prepare)
1268
        {
1269
            BGP_TRACE(D_EVENTS, "Link down");
1270
            bgp_store_error(p, NULL, BE_MISC, BEM_LINK_DOWN);
1271
            if (ps == PS_UP)
1272
                bgp_update_startup_delay(p);
1273
            bgp_stop(p, 0, NULL, 0);
1274
        }
1275
    }
1276
    else
1277
    {
1278
        if (prepare)
1279
        {
1280
            BGP_TRACE(D_EVENTS, "Neighbor ready");
1281
            bgp_start_neighbor(p);
1282
        }
1283
    }
1284
}
1285

    
1286
static void
1287
bgp_bfd_notify(struct bfd_request *req)
1288
{
1289
    struct bgp_proto *p = req->data;
1290
    int ps = p->p.proto_state;
1291

    
1292
    if (req->down && ((ps == PS_START) || (ps == PS_UP)))
1293
    {
1294
        BGP_TRACE(D_EVENTS, "BFD session down");
1295
        bgp_store_error(p, NULL, BE_MISC, BEM_BFD_DOWN);
1296
        if (ps == PS_UP)
1297
            bgp_update_startup_delay(p);
1298
        bgp_stop(p, 0, NULL, 0);
1299
    }
1300
}
1301

    
1302
static void
1303
bgp_update_bfd(struct bgp_proto *p, int use_bfd)
1304
{
1305
    if (use_bfd && !p->bfd_req)
1306
        p->bfd_req = bfd_request_session(p->p.pool, p->cf->remote_ip, p->source_addr,
1307
                                         p->cf->multihop ? NULL : p->neigh->iface,
1308
                                         bgp_bfd_notify, p);
1309

    
1310
    if (!use_bfd && p->bfd_req)
1311
    {
1312
        rfree(p->bfd_req);
1313
        p->bfd_req = NULL;
1314
    }
1315
}
1316

    
1317
static void
1318
bgp_reload_routes(struct channel *C)
1319
{
1320
    log(L_INFO "mi trovo in bgp_reload_routes");
1321
    struct bgp_proto *p = (void *) C->proto;
1322
    struct bgp_channel *c = (void *) C;
1323

    
1324
    ASSERT(p->conn && p->route_refresh);
1325

    
1326
    bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH);
1327
}
1328

    
1329
static void
1330
bgp_feed_begin(struct channel *C, int initial)
1331
{
1332
    //log(L_INFO "mi trovo in bgp_feed_begin, initial: %d", initial);
1333
    struct bgp_proto *p = (void *) C->proto;
1334
    struct bgp_channel *c = (void *) C;
1335

    
1336
    /* This should not happen */
1337
    if (!p->conn)
1338
        return;
1339

    
1340
    if (initial && p->cf->gr_mode)
1341
        c->feed_state = BFS_LOADING;
1342

    
1343
    /* It is refeed and both sides support enhanced route refresh */
1344
    if (!initial && p->enhanced_refresh)
1345
    {
1346
        /* BoRR must not be sent before End-of-RIB */
1347
        if (c->feed_state == BFS_LOADING || c->feed_state == BFS_LOADED)
1348
            return;
1349

    
1350
        c->feed_state = BFS_REFRESHING;
1351
        bgp_schedule_packet(p->conn, c, PKT_BEGIN_REFRESH);
1352
    }
1353
}
1354

    
1355
static void
1356
bgp_feed_end(struct channel *C)
1357
{
1358
    struct bgp_proto *p = (void *) C->proto;
1359
    struct bgp_channel *c = (void *) C;
1360

    
1361
    /* This should not happen */
1362
    if (!p->conn)
1363
        return;
1364

    
1365
    /* Non-demarcated feed ended, nothing to do */
1366
    if (c->feed_state == BFS_NONE)
1367
        return;
1368

    
1369
    /* Schedule End-of-RIB packet */
1370
    if (c->feed_state == BFS_LOADING)
1371
        c->feed_state = BFS_LOADED;
1372

    
1373
    /* Schedule EoRR packet */
1374
    if (c->feed_state == BFS_REFRESHING)
1375
        c->feed_state = BFS_REFRESHED;
1376

    
1377
    /* Kick TX hook */
1378
    bgp_schedule_packet(p->conn, c, PKT_UPDATE);
1379
}
1380

    
1381

    
1382
static void
1383
bgp_start_locked(struct object_lock *lock)
1384
{
1385
    struct bgp_proto *p = lock->data;
1386
    struct bgp_config *cf = p->cf;
1387

    
1388
    if (p->p.proto_state != PS_START)
1389
    {
1390
        DBG("BGP: Got lock in different state %d\n", p->p.proto_state);
1391
        return;
1392
    }
1393

    
1394
    DBG("BGP: Got lock\n");
1395

    
1396
    if (cf->multihop)
1397
    {
1398
        /* Multi-hop sessions do not use neighbor entries */
1399
        bgp_initiate(p);
1400
        return;
1401
    }
1402

    
1403
    neighbor *n = neigh_find2(&p->p, &cf->remote_ip, cf->iface, NEF_STICKY);
1404
    if (!n)
1405
    {
1406
        log(L_ERR "%s: Invalid remote address %I%J", p->p.name, cf->remote_ip, cf->iface);
1407
        /* As we do not start yet, we can just disable protocol */
1408
        p->p.disabled = 1;
1409
        bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_NEXT_HOP);
1410
        proto_notify_state(&p->p, PS_DOWN);
1411
        return;
1412
    }
1413

    
1414
    p->neigh = n;
1415

    
1416
    if (n->scope <= 0)
1417
        BGP_TRACE(D_EVENTS, "Waiting for %I%J to become my neighbor", cf->remote_ip, cf->iface);
1418
    else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP))
1419
        BGP_TRACE(D_EVENTS, "Waiting for link on %s", n->iface->name);
1420
    else
1421
        bgp_start_neighbor(p);
1422
}
1423

    
1424
static int
1425
bgp_start(struct proto *P)
1426
{
1427
    struct bgp_proto *p = (struct bgp_proto *) P;
1428
    struct object_lock *lock;
1429

    
1430
    DBG("BGP: Startup.\n");
1431
    p->start_state = BSS_PREPARE;
1432
    p->outgoing_conn.state = BS_IDLE;
1433
    p->incoming_conn.state = BS_IDLE;
1434
    p->neigh = NULL;
1435
    p->bfd_req = NULL;
1436
    p->gr_ready = 0;
1437
    p->gr_active_num = 0;
1438

    
1439
    p->event = ev_new(p->p.pool);
1440
    p->event->hook = bgp_decision;
1441
    p->event->data = p;
1442

    
1443
    p->startup_timer = tm_new_init(p->p.pool, bgp_startup_timeout, p, 0, 0);
1444
    p->gr_timer = tm_new_init(p->p.pool, bgp_graceful_restart_timeout, p, 0, 0);
1445

    
1446
    p->local_id = proto_get_router_id(P->cf);
1447
    if (p->rr_client)
1448
        p->rr_cluster_id = p->cf->rr_cluster_id ? p->cf->rr_cluster_id : p->local_id;
1449

    
1450
    p->remote_id = 0;
1451
    p->source_addr = p->cf->local_ip;
1452
    p->link_addr = IPA_NONE;
1453

    
1454
    /* Lock all channels when in GR recovery mode */
1455
    if (p->p.gr_recovery && p->cf->gr_mode)
1456
    {
1457
        struct bgp_channel *c;
1458
        WALK_LIST(c, p->p.channels)
1459
        channel_graceful_restart_lock(&c->c);
1460
    }
1461

    
1462
    /*
1463
     * Before attempting to create the connection, we need to lock the port,
1464
     * so that we are the only instance attempting to talk with that neighbor.
1465
     */
1466

    
1467
    lock = p->lock = olock_new(P->pool);
1468
    lock->addr = p->cf->remote_ip;
1469
    lock->port = p->cf->remote_port;
1470
    lock->iface = p->cf->iface;
1471
    lock->vrf = p->cf->iface ? NULL : p->p.vrf;
1472
    lock->type = OBJLOCK_TCP;
1473
    lock->hook = bgp_start_locked;
1474
    lock->data = p;
1475
    olock_acquire(lock);
1476

    
1477
    return PS_START;
1478
}
1479

    
1480
extern int proto_restart;
1481

    
1482
static int
1483
bgp_shutdown(struct proto *P)
1484
{
1485
    struct bgp_proto *p = (struct bgp_proto *) P;
1486
    uint subcode = 0;
1487

    
1488
    char *message = NULL;
1489
    byte *data = NULL;
1490
    uint len = 0;
1491

    
1492
    BGP_TRACE(D_EVENTS, "Shutdown requested");
1493

    
1494
    switch (P->down_code)
1495
    {
1496
        case PDC_CF_REMOVE:
1497
        case PDC_CF_DISABLE:
1498
            subcode = 3; // Errcode 6, 3 - peer de-configured
1499
            break;
1500

    
1501
        case PDC_CF_RESTART:
1502
            subcode = 6; // Errcode 6, 6 - other configuration change
1503
            break;
1504

    
1505
        case PDC_CMD_DISABLE:
1506
        case PDC_CMD_SHUTDOWN:
1507
            subcode = 2; // Errcode 6, 2 - administrative shutdown
1508
            message = P->message;
1509
            break;
1510

    
1511
        case PDC_CMD_RESTART:
1512
            subcode = 4; // Errcode 6, 4 - administrative reset
1513
            message = P->message;
1514
            break;
1515

    
1516
        case PDC_RX_LIMIT_HIT:
1517
        case PDC_IN_LIMIT_HIT:
1518
            subcode = 1; // Errcode 6, 1 - max number of prefixes reached
1519
            /* log message for compatibility */
1520
            log(L_WARN "%s: Route limit exceeded, shutting down", p->p.name);
1521
            goto limit;
1522

    
1523
        case PDC_OUT_LIMIT_HIT:
1524
            subcode = proto_restart ? 4 : 2; // Administrative reset or shutdown
1525

    
1526
        limit:
1527
            bgp_store_error(p, NULL, BE_AUTO_DOWN, BEA_ROUTE_LIMIT_EXCEEDED);
1528
            if (proto_restart)
1529
                bgp_update_startup_delay(p);
1530
            else
1531
                p->startup_delay = 0;
1532
            goto done;
1533
    }
1534

    
1535
    bgp_store_error(p, NULL, BE_MAN_DOWN, 0);
1536
    p->startup_delay = 0;
1537

    
1538
    /* RFC 8203 - shutdown communication */
1539
    if (message)
1540
    {
1541
        uint msg_len = strlen(message);
1542
        msg_len = MIN(msg_len, 128);
1543

    
1544
        /* Buffer will be freed automatically by protocol shutdown */
1545
        data = mb_alloc(p->p.pool, msg_len + 1);
1546
        len = msg_len + 1;
1547

    
1548
        data[0] = msg_len;
1549
        memcpy(data+1, message, msg_len);
1550
    }
1551

    
1552
    done:
1553
    bgp_stop(p, subcode, data, len);
1554
    return p->p.proto_state;
1555
}
1556

    
1557
static struct proto *
1558
bgp_init(struct proto_config *CF)
1559
{
1560
    struct proto *P = proto_new(CF);
1561
    struct bgp_proto *p = (struct bgp_proto *) P;
1562
    struct bgp_config *cf = (struct bgp_config *) CF;
1563

    
1564
    P->rt_notify = bgp_rt_notify;
1565
    P->import_control = bgp_import_control;
1566
    P->neigh_notify = bgp_neigh_notify;
1567
    P->reload_routes = bgp_reload_routes;
1568
    P->feed_begin = bgp_feed_begin;
1569
    P->feed_end = bgp_feed_end;
1570
    P->rte_better = bgp_rte_better;
1571
    P->rte_mergable = bgp_rte_mergable;
1572
    P->rte_recalculate = cf->deterministic_med ? bgp_rte_recalculate : NULL;
1573

    
1574
    p->cf = cf;
1575
    p->local_as = cf->local_as;
1576
    p->remote_as = cf->remote_as;
1577
    p->public_as = cf->local_as;
1578
    p->is_internal = (cf->local_as == cf->remote_as);
1579
    p->is_interior = p->is_internal || cf->confederation_member;
1580
    p->rs_client = cf->rs_client;
1581
    p->rr_client = cf->rr_client;
1582

    
1583
    /* Confederation ID is used for truly external peers */
1584
    if (cf->confederation && !p->is_interior)
1585
        p->public_as = cf->confederation;
1586

    
1587
    /* Add all channels */
1588
    struct bgp_channel_config *cc;
1589
    WALK_LIST(cc, CF->channels)
1590
    proto_add_channel(P, &cc->c);
1591

    
1592
    return P;
1593
}
1594

    
1595
static void
1596
bgp_channel_init(struct channel *C, struct channel_config *CF)
1597
{
1598
    struct bgp_channel *c = (void *) C;
1599
    struct bgp_channel_config *cf = (void *) CF;
1600

    
1601
    c->cf = cf;
1602
    c->afi = cf->afi;
1603
    c->desc = cf->desc;
1604

    
1605
    if (cf->igp_table_ip4)
1606
        c->igp_table_ip4 = cf->igp_table_ip4->table;
1607

    
1608
    if (cf->igp_table_ip6)
1609
        c->igp_table_ip6 = cf->igp_table_ip6->table;
1610
}
1611

    
1612
static int
1613
bgp_channel_start(struct channel *C)
1614
{
1615
    struct bgp_proto *p = (void *) C->proto;
1616
    struct bgp_channel *c = (void *) C;
1617
    ip_addr src = p->source_addr;
1618

    
1619
    if (c->igp_table_ip4)
1620
        rt_lock_table(c->igp_table_ip4);
1621

    
1622
    if (c->igp_table_ip6)
1623
        rt_lock_table(c->igp_table_ip6);
1624

    
1625
    c->pool = p->p.pool; // XXXX
1626
    bgp_init_bucket_table(c);
1627
    bgp_init_prefix_table(c);
1628

    
1629
    c->next_hop_addr = c->cf->next_hop_addr;
1630
    c->link_addr = IPA_NONE;
1631
    c->packets_to_send = 0;
1632

    
1633
    /* Try to use source address as next hop address */
1634
    if (ipa_zero(c->next_hop_addr))
1635
    {
1636
        if (bgp_channel_is_ipv4(c) && (ipa_is_ip4(src) || c->ext_next_hop))
1637
            c->next_hop_addr = src;
1638

    
1639
        if (bgp_channel_is_ipv6(c) && (ipa_is_ip6(src) || c->ext_next_hop))
1640
            c->next_hop_addr = src;
1641
    }
1642

    
1643
    /* Use preferred addresses associated with interface / source address */
1644
    if (ipa_zero(c->next_hop_addr))
1645
    {
1646
        /* We know the iface for single-hop, we make lookup for multihop */
1647
        struct neighbor *nbr = p->neigh ?: neigh_find2(&p->p, &src, NULL, 0);
1648
        struct iface *iface = nbr ? nbr->iface : NULL;
1649

    
1650
        if (bgp_channel_is_ipv4(c) && iface && iface->addr4)
1651
            c->next_hop_addr = iface->addr4->ip;
1652

    
1653
        if (bgp_channel_is_ipv6(c) && iface && iface->addr6)
1654
            c->next_hop_addr = iface->addr6->ip;
1655
    }
1656

    
1657
    /* Exit if no feasible next hop address is found */
1658
    if (ipa_zero(c->next_hop_addr))
1659
    {
1660
        log(L_WARN "%s: Missing next hop address", p->p.name);
1661
        return 0;
1662
    }
1663

    
1664
    /* Set link-local address for IPv6 single-hop BGP */
1665
    if (ipa_is_ip6(c->next_hop_addr) && p->neigh)
1666
    {
1667
        c->link_addr = p->link_addr;
1668

    
1669
        if (ipa_zero(c->link_addr))
1670
            log(L_WARN "%s: Missing link-local address", p->p.name);
1671
    }
1672

    
1673
    /* Link local address is already in c->link_addr */
1674
    if (ipa_is_link_local(c->next_hop_addr))
1675
        c->next_hop_addr = IPA_NONE;
1676

    
1677
    return 0; /* XXXX: Currently undefined */
1678
}
1679

    
1680
static void
1681
bgp_channel_shutdown(struct channel *C)
1682
{
1683
    struct bgp_channel *c = (void *) C;
1684

    
1685
    c->next_hop_addr = IPA_NONE;
1686
    c->link_addr = IPA_NONE;
1687
    c->packets_to_send = 0;
1688
}
1689

    
1690
static void
1691
bgp_channel_cleanup(struct channel *C)
1692
{
1693
    struct bgp_channel *c = (void *) C;
1694

    
1695
    if (c->igp_table_ip4)
1696
        rt_unlock_table(c->igp_table_ip4);
1697

    
1698
    if (c->igp_table_ip6)
1699
        rt_unlock_table(c->igp_table_ip6);
1700
}
1701

    
1702
static inline struct bgp_channel_config *
1703
bgp_find_channel_config(struct bgp_config *cf, u32 afi)
1704
{
1705
    struct bgp_channel_config *cc;
1706

    
1707
    WALK_LIST(cc, cf->c.channels)
1708
    if (cc->afi == afi)
1709
        return cc;
1710

    
1711
    return NULL;
1712
}
1713

    
1714
struct rtable_config *
1715
bgp_default_igp_table(struct bgp_config *cf, struct bgp_channel_config *cc, u32 type)
1716
{
1717
    struct bgp_channel_config *cc2;
1718
    struct rtable_config *tab;
1719

    
1720
    /* First, try table connected by the channel */
1721
    if (cc->c.table->addr_type == type)
1722
        return cc->c.table;
1723

    
1724
    /* Find paired channel with the same SAFI but the other AFI */
1725
    u32 afi2 = cc->afi ^ 0x30000;
1726
    cc2 = bgp_find_channel_config(cf, afi2);
1727

    
1728
    /* Second, try IGP table configured in the paired channel */
1729
    if (cc2 && (tab = (type == NET_IP4) ? cc2->igp_table_ip4 : cc2->igp_table_ip6))
1730
        return tab;
1731

    
1732
    /* Third, try table connected by the paired channel */
1733
    if (cc2 && (cc2->c.table->addr_type == type))
1734
        return cc2->c.table;
1735

    
1736
    /* Last, try default table of given type */
1737
    if (tab = cf->c.global->def_tables[type])
1738
        return tab;
1739

    
1740
    cf_error("Undefined IGP table");
1741
}
1742

    
1743
void
1744
bgp_postconfig(struct proto_config *CF)
1745
{
1746
    struct bgp_config *cf = (void *) CF;
1747
    int internal = (cf->local_as == cf->remote_as);
1748
    int interior = internal || cf->confederation_member;
1749

    
1750
    /* Do not check templates at all */
1751
    if (cf->c.class == SYM_TEMPLATE)
1752
    return;
1753

    
1754

    
1755
    /* EBGP direct by default, IBGP multihop by default */
1756
    if (cf->multihop < 0)
1757
        cf->multihop = internal ? 64 : 0;
1758

    
1759
    /* Link check for single-hop BGP by default */
1760
    if (cf->check_link < 0)
1761
        cf->check_link = !cf->multihop;
1762

    
1763

    
1764
    if (!cf->local_as)
1765
        cf_error("Local AS number must be set");
1766

    
1767
    if (ipa_zero(cf->remote_ip))
1768
        cf_error("Neighbor must be configured");
1769

    
1770
    if (!cf->remote_as)
1771
        cf_error("Remote AS number must be set");
1772

    
1773
    if (ipa_is_link_local(cf->remote_ip) && !cf->iface)
1774
        cf_error("Link-local neighbor address requires specified interface");
1775

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

    
1779
    if (!internal && cf->rr_client)
1780
        cf_error("Only internal neighbor can be RR client");
1781

    
1782
    if (internal && cf->rs_client)
1783
        cf_error("Only external neighbor can be RS client");
1784

    
1785
    if (!cf->confederation && cf->confederation_member)
1786
        cf_error("Confederation ID must be set for member sessions");
1787

    
1788
    if (cf->multihop && (ipa_is_link_local(cf->local_ip) ||
1789
                         ipa_is_link_local(cf->remote_ip)))
1790
        cf_error("Multihop BGP cannot be used with link-local addresses");
1791

    
1792
    if (cf->multihop && cf->iface)
1793
        cf_error("Multihop BGP cannot be bound to interface");
1794

    
1795
    if (cf->multihop && cf->check_link)
1796
        cf_error("Multihop BGP cannot depend on link state");
1797

    
1798
    if (cf->multihop && cf->bfd && ipa_zero(cf->local_ip))
1799
        cf_error("Multihop BGP with BFD requires specified local address");
1800

    
1801

    
1802
    struct bgp_channel_config *cc;
1803
    WALK_LIST(cc, CF->channels)
1804
    {
1805
        /* Handle undefined import filter */
1806
        if (cc->c.in_filter == FILTER_UNDEF)
1807
            if (interior)
1808
                cc->c.in_filter = FILTER_ACCEPT;
1809
            else
1810
                cf_error("EBGP requires explicit import policy");
1811

    
1812
        /* Handle undefined export filter */
1813
        if (cc->c.out_filter == FILTER_UNDEF)
1814
            if (interior)
1815
                cc->c.out_filter = FILTER_REJECT;
1816
            else
1817
                cf_error("EBGP requires explicit export policy");
1818

    
1819
        /* Disable after error incompatible with restart limit action */
1820
        if ((cc->c.in_limit.action == PLA_RESTART) && cf->disable_after_error)
1821
            cc->c.in_limit.action = PLA_DISABLE;
1822

    
1823
        /* Different default based on rs_client */
1824
        if (!cc->missing_lladdr)
1825
            cc->missing_lladdr = cf->rs_client ? MLL_IGNORE : MLL_SELF;
1826

    
1827
        /* Different default for gw_mode */
1828
        if (!cc->gw_mode)
1829
            cc->gw_mode = cf->multihop ? GW_RECURSIVE : GW_DIRECT;
1830

    
1831
        /* Default based on proto config */
1832
        if (cc->gr_able == 0xff)
1833
            cc->gr_able = (cf->gr_mode == BGP_GR_ABLE);
1834

    
1835
        /* Default values of IGP tables */
1836
        if ((cc->gw_mode == GW_RECURSIVE) && !cc->desc->no_igp)
1837
        {
1838
            if (!cc->igp_table_ip4 && (bgp_cc_is_ipv4(cc) || cc->ext_next_hop))
1839
                cc->igp_table_ip4 = bgp_default_igp_table(cf, cc, NET_IP4);
1840

    
1841
            if (!cc->igp_table_ip6 && (bgp_cc_is_ipv6(cc) || cc->ext_next_hop))
1842
                cc->igp_table_ip6 = bgp_default_igp_table(cf, cc, NET_IP6);
1843

    
1844
            if (cc->igp_table_ip4 && bgp_cc_is_ipv6(cc) && !cc->ext_next_hop)
1845
                cf_error("Mismatched IGP table type");
1846

    
1847
            if (cc->igp_table_ip6 && bgp_cc_is_ipv4(cc) && !cc->ext_next_hop)
1848
                cf_error("Mismatched IGP table type");
1849
        }
1850

    
1851
        if (cf->multihop && (cc->gw_mode == GW_DIRECT))
1852
            cf_error("Multihop BGP cannot use direct gateway mode");
1853

    
1854
        if ((cc->gw_mode == GW_RECURSIVE) && cc->c.table->sorted)
1855
            cf_error("BGP in recursive mode prohibits sorted table");
1856

    
1857
        if (cf->deterministic_med && cc->c.table->sorted)
1858
            cf_error("BGP with deterministic MED prohibits sorted table");
1859

    
1860
        if (cc->secondary && !cc->c.table->sorted)
1861
            cf_error("BGP with secondary option requires sorted table");
1862
    }
1863
}
1864

    
1865
static int
1866
bgp_reconfigure(struct proto *P, struct proto_config *CF)
1867
{
1868
    struct bgp_proto *p = (void *) P;
1869
    struct bgp_config *new = (void *) CF;
1870
    struct bgp_config *old = p->cf;
1871

    
1872
    if (proto_get_router_id(CF) != p->local_id)
1873
        return 0;
1874

    
1875
    int same = !memcmp(((byte *) old) + sizeof(struct proto_config),
1876
    ((byte *) new) + sizeof(struct proto_config),
1877
            // password item is last and must be checked separately
1878
            OFFSETOF(struct bgp_config, password) - sizeof(struct proto_config))
1879
    && ((!old->password && !new->password)
1880
       || (old->password && new->password && !strcmp(old->password, new->password)));
1881

    
1882
    /* FIXME: Move channel reconfiguration to generic protocol code ? */
1883
    struct channel *C, *C2;
1884
    struct bgp_channel_config *cc;
1885

    
1886
    WALK_LIST(C, p->p.channels)
1887
    C->stale = 1;
1888

    
1889
    WALK_LIST(cc, new->c.channels)
1890
    {
1891
        C = (struct channel *) bgp_find_channel(p, cc->afi);
1892
        same = proto_configure_channel(P, &C, &cc->c) && same;
1893
        C->stale = 0;
1894
    }
1895

    
1896
    WALK_LIST_DELSAFE(C, C2, p->p.channels)
1897
    if (C->stale)
1898
        same = proto_configure_channel(P, &C, NULL) && same;
1899

    
1900

    
1901
    if (same && (p->start_state > BSS_PREPARE))
1902
        bgp_update_bfd(p, new->bfd);
1903

    
1904
    /* We should update our copy of configuration ptr as old configuration will be freed */
1905
    if (same)
1906
        p->cf = new;
1907

    
1908
    return same;
1909
}
1910

    
1911
#define IGP_TABLE(cf, sym) ((cf)->igp_table_##sym ? (cf)->igp_table_##sym ->table : NULL )
1912

    
1913
static int
1914
bgp_channel_reconfigure(struct channel *C, struct channel_config *CC)
1915
{
1916
    struct bgp_channel *c = (void *) C;
1917
    struct bgp_channel_config *new = (void *) CC;
1918
    struct bgp_channel_config *old = c->cf;
1919

    
1920
    if (memcmp(((byte *) old) + sizeof(struct channel_config),
1921
               ((byte *) new) + sizeof(struct channel_config),
1922
            /* Remaining items must be checked separately */
1923
            OFFSETOF(struct bgp_channel_config, rest) - sizeof(struct channel_config)))
1924
    return 0;
1925

    
1926
    /* Check change in IGP tables */
1927
    if ((IGP_TABLE(old, ip4) != IGP_TABLE(new, ip4)) ||
1928
        (IGP_TABLE(old, ip6) != IGP_TABLE(new, ip6)))
1929
        return 0;
1930

    
1931
    c->cf = new;
1932
    return 1;
1933
}
1934

    
1935
static void
1936
bgp_copy_config(struct proto_config *dest UNUSED, struct proto_config *src UNUSED)
1937
{
1938
    /* Just a shallow copy */
1939
}
1940

    
1941

    
1942
/**
1943
 * bgp_error - report a protocol error
1944
 * @c: connection
1945
 * @code: error code (according to the RFC)
1946
 * @subcode: error sub-code
1947
 * @data: data to be passed in the Notification message
1948
 * @len: length of the data
1949
 *
1950
 * bgp_error() sends a notification packet to tell the other side that a protocol
1951
 * error has occurred (including the data considered erroneous if possible) and
1952
 * closes the connection.
1953
 */
1954
void
1955
bgp_error(struct bgp_conn *c, uint code, uint subcode, byte *data, int len)
1956
{
1957
    struct bgp_proto *p = c->bgp;
1958

    
1959
    if (c->state == BS_CLOSE)
1960
        return;
1961

    
1962
    bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, data, ABS(len));
1963
    bgp_store_error(p, c, BE_BGP_TX, (code << 16) | subcode);
1964
    bgp_conn_enter_close_state(c);
1965

    
1966
    c->notify_code = code;
1967
    c->notify_subcode = subcode;
1968
    c->notify_data = data;
1969
    c->notify_size = (len > 0) ? len : 0;
1970
    bgp_schedule_packet(c, NULL, PKT_NOTIFICATION);
1971

    
1972
    if (code != 6)
1973
    {
1974
        bgp_update_startup_delay(p);
1975
        bgp_stop(p, 0, NULL, 0);
1976
    }
1977
}
1978

    
1979
/**
1980
 * bgp_store_error - store last error for status report
1981
 * @p: BGP instance
1982
 * @c: connection
1983
 * @class: error class (BE_xxx constants)
1984
 * @code: error code (class specific)
1985
 *
1986
 * bgp_store_error() decides whether given error is interesting enough
1987
 * and store that error to last_error variables of @p
1988
 */
1989
void
1990
bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code)
1991
{
1992
    /* During PS_UP, we ignore errors on secondary connection */
1993
    if ((p->p.proto_state == PS_UP) && c && (c != p->conn))
1994
        return;
1995

    
1996
    /* During PS_STOP, we ignore any errors, as we want to report
1997
     * the error that caused transition to PS_STOP
1998
     */
1999
    if (p->p.proto_state == PS_STOP)
2000
        return;
2001

    
2002
    p->last_error_class = class;
2003
    p->last_error_code = code;
2004
}
2005

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

    
2011
static const char *
2012
bgp_last_errmsg(struct bgp_proto *p)
2013
{
2014
    switch (p->last_error_class)
2015
    {
2016
        case BE_MISC:
2017
            return bgp_misc_errors[p->last_error_code];
2018
        case BE_SOCKET:
2019
            return (p->last_error_code == 0) ? "Connection closed" : strerror(p->last_error_code);
2020
        case BE_BGP_RX:
2021
        case BE_BGP_TX:
2022
            return bgp_error_dsc(p->last_error_code >> 16, p->last_error_code & 0xFF);
2023
        case BE_AUTO_DOWN:
2024
            return bgp_auto_errors[p->last_error_code];
2025
        default:
2026
            return "";
2027
    }
2028
}
2029

    
2030
static const char *
2031
bgp_state_dsc(struct bgp_proto *p)
2032
{
2033
    if (p->p.proto_state == PS_DOWN)
2034
        return "Down";
2035

    
2036
    int state = MAX(p->incoming_conn.state, p->outgoing_conn.state);
2037
    if ((state == BS_IDLE) && (p->start_state >= BSS_CONNECT) && p->cf->passive)
2038
        return "Passive";
2039

    
2040
    return bgp_state_names[state];
2041
}
2042

    
2043
static void
2044
bgp_get_status(struct proto *P, byte *buf)
2045
{
2046
    struct bgp_proto *p = (struct bgp_proto *) P;
2047

    
2048
    const char *err1 = bgp_err_classes[p->last_error_class];
2049
    const char *err2 = bgp_last_errmsg(p);
2050

    
2051
    if (P->proto_state == PS_DOWN)
2052
        bsprintf(buf, "%s%s", err1, err2);
2053
    else
2054
        bsprintf(buf, "%-14s%s%s", bgp_state_dsc(p), err1, err2);
2055
}
2056

    
2057
static void
2058
bgp_show_afis(int code, char *s, u32 *afis, uint count)
2059
{
2060
    buffer b;
2061
    LOG_BUFFER_INIT(b);
2062

    
2063
    buffer_puts(&b, s);
2064

    
2065
    for (u32 *af = afis; af < (afis + count); af++)
2066
    {
2067
        const struct bgp_af_desc *desc = bgp_get_af_desc(*af);
2068
        if (desc)
2069
            buffer_print(&b, " %s", desc->name);
2070
        else
2071
            buffer_print(&b, " <%u/%u>", BGP_AFI(*af), BGP_SAFI(*af));
2072
    }
2073

    
2074
    if (b.pos == b.end)
2075
        strcpy(b.end - 32, " ... <too long>");
2076

    
2077
    cli_msg(code, b.start);
2078
}
2079

    
2080
static void
2081
bgp_show_capabilities(struct bgp_proto *p UNUSED, struct bgp_caps *caps)
2082
{
2083
    struct bgp_af_caps *ac;
2084
    uint any_mp_bgp = 0;
2085
    uint any_gr_able = 0;
2086
    uint any_add_path = 0;
2087
    uint any_ext_next_hop = 0;
2088
    u32 *afl1 = alloca(caps->af_count * sizeof(u32));
2089
    u32 *afl2 = alloca(caps->af_count * sizeof(u32));
2090
    uint afn1, afn2;
2091

    
2092
    WALK_AF_CAPS(caps, ac)
2093
    {
2094
        any_mp_bgp |= ac->ready;
2095
        any_gr_able |= ac->gr_able;
2096
        any_add_path |= ac->add_path;
2097
        any_ext_next_hop |= ac->ext_next_hop;
2098
    }
2099

    
2100
    if (any_mp_bgp)
2101
    {
2102
        cli_msg(-1006, "      Multiprotocol");
2103

    
2104
        afn1 = 0;
2105
        WALK_AF_CAPS(caps, ac)
2106
            if (ac->ready)
2107
                afl1[afn1++] = ac->afi;
2108

    
2109
        bgp_show_afis(-1006, "        AF announced:", afl1, afn1);
2110
    }
2111

    
2112
    if (caps->route_refresh)
2113
        cli_msg(-1006, "      Route refresh");
2114

    
2115
    if (any_ext_next_hop)
2116
    {
2117
        cli_msg(-1006, "      Extended next hop");
2118

    
2119
        afn1 = 0;
2120
        WALK_AF_CAPS(caps, ac)
2121
            if (ac->ext_next_hop)
2122
                afl1[afn1++] = ac->afi;
2123

    
2124
        bgp_show_afis(-1006, "        IPv6 nexthop:", afl1, afn1);
2125
    }
2126

    
2127
    if (caps->ext_messages)
2128
        cli_msg(-1006, "      Extended message");
2129

    
2130
    if (caps->gr_aware)
2131
        cli_msg(-1006, "      Graceful restart");
2132

    
2133
    if (any_gr_able)
2134
    {
2135
        /* Continues from gr_aware */
2136
        cli_msg(-1006, "        Restart time: %u", caps->gr_time);
2137
        if (caps->gr_flags & BGP_GRF_RESTART)
2138
            cli_msg(-1006, "        Restart recovery");
2139

    
2140
        afn1 = afn2 = 0;
2141
        WALK_AF_CAPS(caps, ac)
2142
        {
2143
            if (ac->gr_able)
2144
                afl1[afn1++] = ac->afi;
2145

    
2146
            if (ac->gr_af_flags & BGP_GRF_FORWARDING)
2147
                afl2[afn2++] = ac->afi;
2148
        }
2149

    
2150
        bgp_show_afis(-1006, "        AF supported:", afl1, afn1);
2151
        bgp_show_afis(-1006, "        AF preserved:", afl2, afn2);
2152
    }
2153

    
2154
    if (caps->as4_support)
2155
        cli_msg(-1006, "      4-octet AS numbers");
2156

    
2157
    if (any_add_path)
2158
    {
2159
        cli_msg(-1006, "      ADD-PATH");
2160

    
2161
        afn1 = afn2 = 0;
2162
        WALK_AF_CAPS(caps, ac)
2163
        {
2164
            if (ac->add_path & BGP_ADD_PATH_RX)
2165
                afl1[afn1++] = ac->afi;
2166

    
2167
            if (ac->add_path & BGP_ADD_PATH_TX)
2168
                afl2[afn2++] = ac->afi;
2169
        }
2170

    
2171
        bgp_show_afis(-1006, "        RX:", afl1, afn1);
2172
        bgp_show_afis(-1006, "        TX:", afl2, afn2);
2173
    }
2174

    
2175
    if (caps->enhanced_refresh)
2176
        cli_msg(-1006, "      Enhanced refresh");
2177
}
2178

    
2179
static void
2180
bgp_show_proto_info(struct proto *P)
2181
{
2182
    struct bgp_proto *p = (struct bgp_proto *) P;
2183

    
2184
    cli_msg(-1006, "  BGP state:          %s", bgp_state_dsc(p));
2185
    cli_msg(-1006, "    Neighbor address: %I%J", p->cf->remote_ip, p->cf->iface);
2186
    cli_msg(-1006, "    Neighbor AS:      %u", p->remote_as);
2187

    
2188
    if (p->gr_active_num)
2189
        cli_msg(-1006, "    Neighbor graceful restart active");
2190

    
2191
    if (P->proto_state == PS_START)
2192
    {
2193
        struct bgp_conn *oc = &p->outgoing_conn;
2194

    
2195
        if ((p->start_state < BSS_CONNECT) &&
2196
            (tm_active(p->startup_timer)))
2197
            cli_msg(-1006, "    Error wait:       %t/%u",
2198
                    tm_remains(p->startup_timer), p->startup_delay);
2199

    
2200
        if ((oc->state == BS_ACTIVE) &&
2201
            (tm_active(oc->connect_timer)))
2202
            cli_msg(-1006, "    Connect delay:    %t/%u",
2203
                    tm_remains(oc->connect_timer), p->cf->connect_delay_time);
2204

    
2205
        if (p->gr_active_num && tm_active(p->gr_timer))
2206
            cli_msg(-1006, "    Restart timer:    %t/-",
2207
                    tm_remains(p->gr_timer));
2208
    }
2209
    else if (P->proto_state == PS_UP)
2210
    {
2211
        cli_msg(-1006, "    Neighbor ID:      %R", p->remote_id);
2212
        cli_msg(-1006, "    Local capabilities");
2213
        bgp_show_capabilities(p, p->conn->local_caps);
2214
        cli_msg(-1006, "    Neighbor capabilities");
2215
        bgp_show_capabilities(p, p->conn->remote_caps);
2216
        cli_msg(-1006, "    Session:          %s%s%s%s%s",
2217
                p->is_internal ? "internal" : "external",
2218
                p->cf->multihop ? " multihop" : "",
2219
                p->rr_client ? " route-reflector" : "",
2220
                p->rs_client ? " route-server" : "",
2221
                p->as4_session ? " AS4" : "");
2222
        cli_msg(-1006, "    Source address:   %I", p->source_addr);
2223
        cli_msg(-1006, "    Hold timer:       %t/%u",
2224
                tm_remains(p->conn->hold_timer), p->conn->hold_time);
2225
        cli_msg(-1006, "    Keepalive timer:  %t/%u",
2226
                tm_remains(p->conn->keepalive_timer), p->conn->keepalive_time);
2227
    }
2228

    
2229
    if ((p->last_error_class != BE_NONE) &&
2230
        (p->last_error_class != BE_MAN_DOWN))
2231
    {
2232
        const char *err1 = bgp_err_classes[p->last_error_class];
2233
        const char *err2 = bgp_last_errmsg(p);
2234
        cli_msg(-1006, "    Last error:       %s%s", err1, err2);
2235
    }
2236

    
2237
    {
2238
        struct bgp_channel *c;
2239
        WALK_LIST(c, p->p.channels)
2240
        {
2241
            channel_show_info(&c->c);
2242

    
2243
            if (c->c.channel_state == CS_UP)
2244
            {
2245
                if (ipa_zero(c->link_addr))
2246
                    cli_msg(-1006, "    BGP Next hop:   %I", c->next_hop_addr);
2247
                else
2248
                    cli_msg(-1006, "    BGP Next hop:   %I %I", c->next_hop_addr, c->link_addr);
2249
            }
2250

    
2251
            if (c->igp_table_ip4)
2252
                cli_msg(-1006, "    IGP IPv4 table: %s", c->igp_table_ip4->name);
2253

    
2254
            if (c->igp_table_ip6)
2255
                cli_msg(-1006, "    IGP IPv6 table: %s", c->igp_table_ip6->name);
2256
        }
2257
    }
2258
}
2259

    
2260
// Next three function could be usefull in some situations
2261
/*void
2262
channel_show_info_mine(struct channel *c)
2263
{
2264
    log(L_INFO "  Channel %s", c->name);
2265
    //log(L_INFO "    State:          %s", c_states[c->channel_state]);
2266
    log(L_INFO "    Table:          %s", c->table->name);
2267
    log(L_INFO "    Preference:     %d", c->preference);
2268
    log(L_INFO "    Input filter:   %s", filter_name(c->in_filter));
2269
    log(L_INFO "    Output filter:  %s", filter_name(c->out_filter));
2270

2271
    ///*if (graceful_restart_state == GRS_ACTIVE)
2272
        log(L_INFO "    GR recovery:   %s%s",
2273
                c->gr_lock ? " pending" : "",
2274
                c->gr_wait ? " waiting" : "");//
2275

2276
    //channel_show_limit(&c->rx_limit, "Receive limit:");
2277
    //channel_show_limit(&c->in_limit, "Import limit:");
2278
    //channel_show_limit(&c->out_limit, "Export limit:");
2279

2280
    //if (c->channel_state != CS_DOWN)
2281
    //    channel_show_stats(c);
2282
}*/
2283

    
2284
/*static void
2285
bgp_show_capabilities_mine(struct bgp_proto *p UNUSED, struct bgp_caps *caps)
2286
{
2287
    struct bgp_af_caps *ac;
2288
    uint any_mp_bgp = 0;
2289
    uint any_gr_able = 0;
2290
    uint any_add_path = 0;
2291
    uint any_ext_next_hop = 0;
2292
    u32 *afl1 = alloca(caps->af_count * sizeof(u32));
2293
    u32 *afl2 = alloca(caps->af_count * sizeof(u32));
2294
    uint afn1, afn2;
2295

2296
    WALK_AF_CAPS(caps, ac)
2297
    {
2298
        any_mp_bgp |= ac->ready;
2299
        any_gr_able |= ac->gr_able;
2300
        any_add_path |= ac->add_path;
2301
        any_ext_next_hop |= ac->ext_next_hop;
2302
    }
2303

2304
    if (any_mp_bgp)
2305
    {
2306
        log(L_INFO "      Multiprotocol");
2307

2308
        afn1 = 0;
2309
        WALK_AF_CAPS(caps, ac)
2310
            if (ac->ready)
2311
                afl1[afn1++] = ac->afi;
2312

2313
        //bgp_show_afis(-1006, "        AF announced:", afl1, afn1);
2314
    }
2315

2316
    if (caps->route_refresh)
2317
        log(L_INFO "      Route refresh");
2318

2319
    if (any_ext_next_hop)
2320
    {
2321
        log(L_INFO "      Extended next hop");
2322

2323
        afn1 = 0;
2324
        WALK_AF_CAPS(caps, ac)
2325
            if (ac->ext_next_hop)
2326
                afl1[afn1++] = ac->afi;
2327

2328
        //bgp_show_afis(-1006, "        IPv6 nexthop:", afl1, afn1);
2329
    }
2330

2331
    if (caps->ext_messages)
2332
        log(L_INFO "      Extended message");
2333

2334
    if (caps->gr_aware)
2335
        log(L_INFO "      Graceful restart");
2336

2337
    if (any_gr_able)
2338
    {
2339
        // Continues from gr_aware
2340
        log(L_INFO "        Restart time: %u", caps->gr_time);
2341
        if (caps->gr_flags & BGP_GRF_RESTART)
2342
            log(L_INFO "        Restart recovery");
2343

2344
        afn1 = afn2 = 0;
2345
        WALK_AF_CAPS(caps, ac)
2346
        {
2347
            if (ac->gr_able)
2348
                afl1[afn1++] = ac->afi;
2349

2350
            if (ac->gr_af_flags & BGP_GRF_FORWARDING)
2351
                afl2[afn2++] = ac->afi;
2352
        }
2353

2354
        //bgp_show_afis(-1006, "        AF supported:", afl1, afn1);
2355
        //bgp_show_afis(-1006, "        AF preserved:", afl2, afn2);
2356
    }
2357

2358
    if (caps->as4_support)
2359
        log(L_INFO "      4-octet AS numbers");
2360

2361
    if (any_add_path)
2362
    {
2363
        log(L_INFO "      ADD-PATH");
2364

2365
        afn1 = afn2 = 0;
2366
        WALK_AF_CAPS(caps, ac)
2367
        {
2368
            if (ac->add_path & BGP_ADD_PATH_RX)
2369
                afl1[afn1++] = ac->afi;
2370

2371
            if (ac->add_path & BGP_ADD_PATH_TX)
2372
                afl2[afn2++] = ac->afi;
2373
        }
2374

2375
        //bgp_show_afis(-1006, "        RX:", afl1, afn1);
2376
        //bgp_show_afis(-1006, "        TX:", afl2, afn2);
2377
    }
2378

2379
    if (caps->enhanced_refresh)
2380
        log(L_INFO "      Enhanced refresh");
2381
}
2382
*/
2383

    
2384
/*void
2385
bgp_show_proto_info_mine(struct bgp_proto *P)
2386
{
2387
    struct bgp_proto *p = P;
2388

2389
    log(L_INFO "  BGP state:          %s", bgp_state_dsc(p));
2390
    log(L_INFO "    Neighbor address: %I%J", p->cf->remote_ip, p->cf->iface);
2391
    log(L_INFO "    Neighbor AS:      %u", p->remote_as);
2392

2393
    if (p->gr_active_num)
2394
        log(L_INFO "    Neighbor graceful restart active");
2395

2396
    if (P->p.proto_state == PS_START)
2397
    {
2398
        struct bgp_conn *oc = &p->outgoing_conn;
2399

2400
        if ((p->start_state < BSS_CONNECT) &&
2401
            (tm_active(p->startup_timer)))
2402
            log(L_INFO "    Error wait:       %t/%u",
2403
                    tm_remains(p->startup_timer), p->startup_delay);
2404

2405
        if ((oc->state == BS_ACTIVE) &&
2406
            (tm_active(oc->connect_timer)))
2407
            log(L_INFO "    Connect delay:    %t/%u",
2408
                    tm_remains(oc->connect_timer), p->cf->connect_delay_time);
2409

2410
        if (p->gr_active_num && tm_active(p->gr_timer))
2411
            log(L_INFO "    Restart timer:    %t/-",
2412
                    tm_remains(p->gr_timer));
2413
    }
2414
    else if (P->p.proto_state == PS_UP)
2415
    {
2416
        log(L_INFO "    Neighbor ID:      %R", p->remote_id);
2417
        log(L_INFO "    Local capabilities");
2418
        bgp_show_capabilities_mine(p, p->conn->local_caps);
2419
        log(L_INFO "    Neighbor capabilities");
2420
        bgp_show_capabilities_mine(p, p->conn->remote_caps);
2421
        log(L_INFO "    Session:          %s%s%s%s%s",
2422
                p->is_internal ? "internal" : "external",
2423
                p->cf->multihop ? " multihop" : "",
2424
                p->rr_client ? " route-reflector" : "",
2425
                p->rs_client ? " route-server" : "",
2426
                p->as4_session ? " AS4" : "");
2427
        log(L_INFO "    Source address:   %I", p->source_addr);
2428
        log(L_INFO "    Hold timer:       %t/%u",
2429
                tm_remains(p->conn->hold_timer), p->conn->hold_time);
2430
        log(L_INFO "    Keepalive timer:  %t/%u",
2431
                tm_remains(p->conn->keepalive_timer), p->conn->keepalive_time);
2432
    }
2433

2434
    if ((p->last_error_class != BE_NONE) &&
2435
        (p->last_error_class != BE_MAN_DOWN))
2436
    {
2437
        const char *err1 = bgp_err_classes[p->last_error_class];
2438
        const char *err2 = bgp_last_errmsg(p);
2439
        log(L_INFO "    Last error:       %s%s", err1, err2);
2440
    }
2441

2442
    {
2443
        struct bgp_channel *c;
2444
        WALK_LIST(c, p->p.channels)
2445
        {
2446
            channel_show_info_mine(&c->c);
2447

2448
            if (c->c.channel_state == CS_UP)
2449
            {
2450
                if (ipa_zero(c->link_addr))
2451
                    log(L_INFO "    BGP Next hop:   %I", c->next_hop_addr);
2452
                else
2453
                    log(L_INFO "    BGP Next hop:   %I %I", c->next_hop_addr, c->link_addr);
2454
            }
2455

2456
            if (c->igp_table_ip4)
2457
                log(L_INFO "    IGP IPv4 table: %s", c->igp_table_ip4->name);
2458

2459
            if (c->igp_table_ip6)
2460
                log(L_INFO "    IGP IPv6 table: %s", c->igp_table_ip6->name);
2461
        }
2462
    }
2463
}*/
2464

    
2465
struct channel_class channel_bgp = {
2466
        .channel_size =        sizeof(struct bgp_channel),
2467
        .config_size =        sizeof(struct bgp_channel_config),
2468
        .init =                bgp_channel_init,
2469
        .start =                bgp_channel_start,
2470
        .shutdown =                bgp_channel_shutdown,
2471
        .cleanup =                bgp_channel_cleanup,
2472
        .reconfigure =        bgp_channel_reconfigure,
2473
};
2474

    
2475
struct protocol proto_bgp = {
2476
        .name =                 "BGP",
2477
        .template =                 "bgp%d",
2478
        .attr_class =         EAP_BGP,
2479
        .preference =         DEF_PREF_BGP,
2480
        .channel_mask =        NB_IP | NB_VPN | NB_FLOW,
2481
        .proto_size =                sizeof(struct bgp_proto),
2482
        .config_size =        sizeof(struct bgp_config),
2483
        .postconfig =                bgp_postconfig,
2484
        .init =                 bgp_init,
2485
        .start =                 bgp_start,
2486
        .shutdown =                 bgp_shutdown,
2487
        .reconfigure =         bgp_reconfigure,
2488
        .copy_config =         bgp_copy_config,
2489
        .get_status =         bgp_get_status,
2490
        .get_attr =                 bgp_get_attr,
2491
        .get_route_info =         bgp_get_route_info,
2492
        .show_proto_info =         bgp_show_proto_info
2493
};