Revision ef5081af proto/bgp/bgp.c

View differences:

proto/bgp/bgp.c
106 106
#undef LOCAL_DEBUG
107 107

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

  
110 111
#include "nest/bird.h"
111 112
#include "nest/iface.h"
......
121 122

  
122 123
#include "bgp.h"
123 124

  
124

  
125 125
struct linpool *bgp_linpool;		/* Global temporary pool */
126 126
struct linpool *bgp_linpool2;		/* Global temporary pool for bgp_rt_notify() */
127 127
static list bgp_sockets;		/* Global list of listening sockets */
......
146 146
static int
147 147
bgp_open(struct bgp_proto *p)
148 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;
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 154

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

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

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

  
197
  return 0;
197
    return 0;
198 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;
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 204
}
205 205

  
206 206
/**
......
212 212
static void
213 213
bgp_close(struct bgp_proto *p)
214 214
{
215
  struct bgp_socket *bs = p->sock;
215
    struct bgp_socket *bs = p->sock;
216 216

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

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

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

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

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

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

  
236 236
static inline int
237 237
bgp_setup_auth(struct bgp_proto *p, int enable)
238 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);
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 244

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

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

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

  
262
  return NULL;
262
    return NULL;
263 263
}
264 264

  
265 265
static void
266 266
bgp_startup(struct bgp_proto *p)
267 267
{
268
  BGP_TRACE(D_EVENTS, "Started");
269
  p->start_state = BSS_CONNECT;
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);
270 274

  
271
  if (!p->cf->passive)
272
    bgp_active(p);
275
    initRTmap();
276
    loadComplessivo = 0;
273 277
}
274 278

  
275 279
static void
276 280
bgp_startup_timeout(timer *t)
277 281
{
278
  bgp_startup(t->data);
282
    bgp_startup(t->data);
279 283
}
280 284

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

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

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

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

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

  
313
static void
314
bgp_mrai_timeout(timer *t)
315
{
316
    //How to extract data from the timer
317
    struct bgp_conn *conn = t->data;
318
    struct bgp_channel *c;
319

  
320
    //Simple message for the user
321
    log(L_INFO "CIAO, MRAI timer scaduto, posso schedulare i pacchetti");
322
}
281 323

  
282 324
static void
283 325
bgp_initiate(struct bgp_proto *p)
284 326
{
285
  int err_val;
327
    int err_val;
286 328

  
287
  if (bgp_open(p) < 0)
288
  { err_val = BEM_NO_SOCKET; goto err1; }
329
    if (bgp_open(p) < 0)
330
    { err_val = BEM_NO_SOCKET; goto err1; }
289 331

  
290
  if (bgp_setup_auth(p, 1) < 0)
291
  { err_val = BEM_INVALID_MD5; goto err2; }
332
    if (bgp_setup_auth(p, 1) < 0)
333
    { err_val = BEM_INVALID_MD5; goto err2; }
292 334

  
293
  if (p->cf->bfd)
294
    bgp_update_bfd(p, p->cf->bfd);
335
    if (p->cf->bfd)
336
        bgp_update_bfd(p, p->cf->bfd);
295 337

  
296
  if (p->startup_delay)
297
  {
298
    p->start_state = BSS_DELAY;
299
    BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds due to errors", p->startup_delay);
300
    bgp_start_timer(p->startup_timer, p->startup_delay);
301
  }
302
  else
303
    bgp_startup(p);
304 338

  
305
  return;
306 339

  
307
err2:
308
  bgp_close(p);
309
err1:
310
  p->p.disabled = 1;
311
  bgp_store_error(p, NULL, BE_MISC, err_val);
312
  proto_notify_state(&p->p, PS_DOWN);
340
    if (p->startup_delay)
341
    {
342
        p->start_state = BSS_DELAY;
343
        BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds due to errors", p->startup_delay);
344
        bgp_start_timer(p->startup_timer, p->startup_delay);
345
    }
346
    else
347
        bgp_startup(p);
348

  
349
    return;
313 350

  
314
  return;
351
    err2:
352
    bgp_close(p);
353
    err1:
354
    p->p.disabled = 1;
355
    bgp_store_error(p, NULL, BE_MISC, err_val);
356
    proto_notify_state(&p->p, PS_DOWN);
357

  
358
    return;
315 359
}
316 360

  
317 361
/**
......
326 370
void
327 371
bgp_start_timer(timer *t, uint value)
328 372
{
329
  if (value)
330
  {
331
    /* The randomization procedure is specified in RFC 4271 section 10 */
332
    btime time = value S;
333
    btime randomize = random() % ((time / 4) + 1);
334
    tm_start(t, time - randomize);
335
  }
336
  else
337
    tm_stop(t);
373
    if (value)
374
    {
375
        /* The randomization procedure is specified in RFC 4271 section 10 */
376
        btime time = value S;
377
        btime randomize = random() % ((time / 4) + 1);
378
        tm_start(t, time - randomize);
379
    }
380
    else
381
        tm_stop(t);
338 382
}
339 383

  
340 384
/**
......
347 391
void
348 392
bgp_close_conn(struct bgp_conn *conn)
349 393
{
350
  // struct bgp_proto *p = conn->bgp;
394
    DBG("BGP: Closing connection\n");
395

  
396
    //log(L_INFO "AS del vicino con cui si vuole chiudere la connessione: %d",conn->bgp->remote_as);
397
    const char *keyRTmap;
398
    map_iter_t iterRTmap;
399
    const char *keyNHmap_RTmap;
400
    map_iter_t iterNHmap_RTmap;
401

  
402
    //NH removing from the data structure
403
    iterRTmap = map_iter(&RTmap);
404
    while ((keyRTmap = map_next(&RTmap, &iterRTmap))) {
405
        RTable *RTmap_element = map_get(&RTmap, keyRTmap);
406
        iterNHmap_RTmap = map_iter(&RTmap_element->NH);
407
        while ((keyNHmap_RTmap = map_next(&RTmap_element->NH, &iterNHmap_RTmap))) {
408
            int *NH_AS_value = map_get(&RTmap_element->NH, keyNHmap_RTmap);
409
            if(NH_AS_value != NULL) {
410
                if (*NH_AS_value == conn->bgp->remote_as)
411
                    map_remove(&RTmap, keyRTmap);
412
            }
413
        }
414

  
415
        //Load removing from the data structure
416
        const char *keyLoadInmap_RTmap;
417
        map_iter_t iterLoadInmap_RTmap = map_iter(&RTmap_element->loadin);
418
        while ((keyLoadInmap_RTmap = map_next(&RTmap_element->loadin, &iterLoadInmap_RTmap))) {
419
            int keyValue = (int) strtol(keyLoadInmap_RTmap, (char **)NULL, 16);
420

  
421
            if(keyValue == conn->bgp->remote_as)
422
                map_remove(&RTmap_element->loadin, keyLoadInmap_RTmap);
423
        }
424
    }
351 425

  
352
  DBG("BGP: Closing connection\n");
353
  conn->packets_to_send = 0;
354
  conn->channels_to_send = 0;
355
  rfree(conn->connect_timer);
356
  conn->connect_timer = NULL;
357
  rfree(conn->keepalive_timer);
358
  conn->keepalive_timer = NULL;
359
  rfree(conn->hold_timer);
360
  conn->hold_timer = NULL;
361
  rfree(conn->tx_ev);
362
  conn->tx_ev = NULL;
363
  rfree(conn->sk);
364
  conn->sk = NULL;
426
    conn->packets_to_send = 0;
427
    conn->channels_to_send = 0;
428
    rfree(conn->connect_timer);
429
    conn->connect_timer = NULL;
430
    rfree(conn->keepalive_timer);
431
    conn->keepalive_timer = NULL;
432
    rfree(conn->hold_timer);
433
    conn->hold_timer = NULL;
365 434

  
366
  mb_free(conn->local_caps);
367
  conn->local_caps = NULL;
368
  mb_free(conn->remote_caps);
369
  conn->remote_caps = NULL;
370
}
435
    /* My MRAI timer */
436
    rfree(conn->mrai_timer);
437
    conn->mrai_timer = NULL;
371 438

  
439
    rfree(conn->tx_ev);
440
    conn->tx_ev = NULL;
441
    rfree(conn->sk);
442
    conn->sk = NULL;
443

  
444
    mb_free(conn->local_caps);
445
    conn->local_caps = NULL;
446
    mb_free(conn->remote_caps);
447
    conn->remote_caps = NULL;
448
}
372 449

  
373 450
/**
374 451
 * bgp_update_startup_delay - update a startup delay
......
383 460
void
384 461
bgp_update_startup_delay(struct bgp_proto *p)
385 462
{
386
  struct bgp_config *cf = p->cf;
463
    struct bgp_config *cf = p->cf;
387 464

  
388
  DBG("BGP: Updating startup delay\n");
465
    DBG("BGP: Updating startup delay\n");
389 466

  
390
  if (p->last_proto_error && ((current_time() - p->last_proto_error) >= cf->error_amnesia_time S))
467
    if (p->last_proto_error && ((current_time() - p->last_proto_error) >= cf->error_amnesia_time S))
391 468
    p->startup_delay = 0;
392 469

  
393
  p->last_proto_error = current_time();
470
    p->last_proto_error = current_time();
394 471

  
395
  if (cf->disable_after_error)
396
  {
397
    p->startup_delay = 0;
398
    p->p.disabled = 1;
399
    return;
400
  }
472
    if (cf->disable_after_error)
473
    {
474
        p->startup_delay = 0;
475
        p->p.disabled = 1;
476
        return;
477
    }
401 478

  
402
  if (!p->startup_delay)
403
    p->startup_delay = cf->error_delay_time_min;
404
  else
405
    p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max);
479
    if (!p->startup_delay)
480
        p->startup_delay = cf->error_delay_time_min;
481
    else
482
        p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max);
406 483
}
407 484

  
408 485
static void
409 486
bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode, byte *data, uint len)
410 487
{
411
  switch (conn->state)
412
  {
413
  case BS_IDLE:
414
  case BS_CLOSE:
415
    return;
416

  
417
  case BS_CONNECT:
418
  case BS_ACTIVE:
419
    bgp_conn_enter_idle_state(conn);
420
    return;
421

  
422
  case BS_OPENSENT:
423
  case BS_OPENCONFIRM:
424
  case BS_ESTABLISHED:
425
    bgp_error(conn, 6, subcode, data, len);
426
    return;
427

  
428
  default:
429
    bug("bgp_graceful_close_conn: Unknown state %d", conn->state);
430
  }
488
    switch (conn->state)
489
    {
490
        case BS_IDLE:
491
        case BS_CLOSE:
492
            return;
493

  
494
        case BS_CONNECT:
495
        case BS_ACTIVE:
496
            bgp_conn_enter_idle_state(conn);
497
            return;
498

  
499
        case BS_OPENSENT:
500
        case BS_OPENCONFIRM:
501
        case BS_ESTABLISHED:
502
            bgp_error(conn, 6, subcode, data, len);
503
            return;
504

  
505
        default:
506
            bug("bgp_graceful_close_conn: Unknown state %d", conn->state);
507
    }
431 508
}
432 509

  
433 510
static void
434 511
bgp_down(struct bgp_proto *p)
435 512
{
436
  if (p->start_state > BSS_PREPARE)
437
  {
438
    bgp_setup_auth(p, 0);
439
    bgp_close(p);
440
  }
513
    if (p->start_state > BSS_PREPARE)
514
    {
515
        bgp_setup_auth(p, 0);
516
        bgp_close(p);
517
    }
441 518

  
442
  BGP_TRACE(D_EVENTS, "Down");
443
  proto_notify_state(&p->p, PS_DOWN);
519
    BGP_TRACE(D_EVENTS, "Down");
520
    proto_notify_state(&p->p, PS_DOWN);
444 521
}
445 522

  
446 523
static void
447 524
bgp_decision(void *vp)
448 525
{
449
  struct bgp_proto *p = vp;
526
    struct bgp_proto *p = vp;
450 527

  
451
  DBG("BGP: Decision start\n");
452
  if ((p->p.proto_state == PS_START) &&
453
      (p->outgoing_conn.state == BS_IDLE) &&
454
      (p->incoming_conn.state != BS_OPENCONFIRM) &&
455
      !p->cf->passive)
456
    bgp_active(p);
528
    DBG("BGP: Decision start\n");
529
    if ((p->p.proto_state == PS_START) &&
530
        (p->outgoing_conn.state == BS_IDLE) &&
531
        (p->incoming_conn.state != BS_OPENCONFIRM) &&
532
        !p->cf->passive)
533
        bgp_active(p);
457 534

  
458
  if ((p->p.proto_state == PS_STOP) &&
459
      (p->outgoing_conn.state == BS_IDLE) &&
460
      (p->incoming_conn.state == BS_IDLE))
461
    bgp_down(p);
535
    if ((p->p.proto_state == PS_STOP) &&
536
        (p->outgoing_conn.state == BS_IDLE) &&
537
        (p->incoming_conn.state == BS_IDLE))
538
        bgp_down(p);
462 539
}
463 540

  
464 541
void
465 542
bgp_stop(struct bgp_proto *p, uint subcode, byte *data, uint len)
466 543
{
467
  proto_notify_state(&p->p, PS_STOP);
468
  bgp_graceful_close_conn(&p->outgoing_conn, subcode, data, len);
469
  bgp_graceful_close_conn(&p->incoming_conn, subcode, data, len);
470
  ev_schedule(p->event);
544
    proto_notify_state(&p->p, PS_STOP);
545
    bgp_graceful_close_conn(&p->outgoing_conn, subcode, data, len);
546
    bgp_graceful_close_conn(&p->incoming_conn, subcode, data, len);
547
    ev_schedule(p->event);
471 548
}
472 549

  
473 550
static inline void
474 551
bgp_conn_set_state(struct bgp_conn *conn, uint new_state)
475 552
{
476
  if (conn->bgp->p.mrtdump & MD_STATES)
477
    mrt_dump_bgp_state_change(conn, conn->state, new_state);
553
    if (conn->bgp->p.mrtdump & MD_STATES)
554
        mrt_dump_bgp_state_change(conn, conn->state, new_state);
478 555

  
479
  conn->state = new_state;
556
    conn->state = new_state;
480 557
}
481 558

  
482 559
void
483 560
bgp_conn_enter_openconfirm_state(struct bgp_conn *conn)
484 561
{
485
  /* Really, most of the work is done in bgp_rx_open(). */
486
  bgp_conn_set_state(conn, BS_OPENCONFIRM);
562
    /* Really, most of the work is done in bgp_rx_open(). */
563
    bgp_conn_set_state(conn, BS_OPENCONFIRM);
487 564
}
488 565

  
489 566
static const struct bgp_af_caps dummy_af_caps = { };
......
491 568
void
492 569
bgp_conn_enter_established_state(struct bgp_conn *conn)
493 570
{
494
  struct bgp_proto *p = conn->bgp;
495
  struct bgp_caps *local = conn->local_caps;
496
  struct bgp_caps *peer = conn->remote_caps;
497
  struct bgp_channel *c;
571
    struct bgp_proto *p = conn->bgp;
572
    struct bgp_caps *local = conn->local_caps;
573
    struct bgp_caps *peer = conn->remote_caps;
574
    struct bgp_channel *c;
498 575

  
499
  BGP_TRACE(D_EVENTS, "BGP session established");
576
    BGP_TRACE(D_EVENTS, "BGP session established");
500 577

  
501
  /* For multi-hop BGP sessions */
502
  if (ipa_zero(p->source_addr))
503
    p->source_addr = conn->sk->saddr;
578
    /* For multi-hop BGP sessions */
579
    if (ipa_zero(p->source_addr))
580
        p->source_addr = conn->sk->saddr;
504 581

  
505
  conn->sk->fast_rx = 0;
582
    conn->sk->fast_rx = 0;
506 583

  
507
  p->conn = conn;
508
  p->last_error_class = 0;
509
  p->last_error_code = 0;
584
    p->conn = conn;
585
    p->last_error_class = 0;
586
    p->last_error_code = 0;
510 587

  
511
  p->as4_session = conn->as4_session;
588
    p->as4_session = conn->as4_session;
512 589

  
513
  p->route_refresh = peer->route_refresh;
514
  p->enhanced_refresh = local->enhanced_refresh && peer->enhanced_refresh;
590
    p->route_refresh = peer->route_refresh;
591
    p->enhanced_refresh = local->enhanced_refresh && peer->enhanced_refresh;
515 592

  
516
  /* Whether we may handle possible GR of peer (it has some AF GR-able) */
517
  p->gr_ready = 0;	/* Updated later */
593
    /* Whether we may handle possible GR of peer (it has some AF GR-able) */
594
    p->gr_ready = 0;	/* Updated later */
518 595

  
519
  /* Whether peer is ready to handle our GR recovery */
520
  int peer_gr_ready = peer->gr_aware && !(peer->gr_flags & BGP_GRF_RESTART);
596
    /* Whether peer is ready to handle our GR recovery */
597
    int peer_gr_ready = peer->gr_aware && !(peer->gr_flags & BGP_GRF_RESTART);
521 598

  
522
  if (p->gr_active_num)
523
    tm_stop(p->gr_timer);
599
    if (p->gr_active_num)
600
        tm_stop(p->gr_timer);
524 601

  
525
  /* Number of active channels */
526
  int num = 0;
602
    /* Number of active channels */
603
    int num = 0;
527 604

  
528
  WALK_LIST(c, p->p.channels)
529
  {
530
    const struct bgp_af_caps *loc = bgp_find_af_caps(local, c->afi);
531
    const struct bgp_af_caps *rem = bgp_find_af_caps(peer,  c->afi);
605
    WALK_LIST(c, p->p.channels)
606
    {
607
        const struct bgp_af_caps *loc = bgp_find_af_caps(local, c->afi);
608
        const struct bgp_af_caps *rem = bgp_find_af_caps(peer,  c->afi);
532 609

  
533
    /* Ignore AFIs that were not announced in multiprotocol capability */
534
    if (!loc || !loc->ready)
535
      loc = &dummy_af_caps;
610
        /* Ignore AFIs that were not announced in multiprotocol capability */
611
        if (!loc || !loc->ready)
612
            loc = &dummy_af_caps;
536 613

  
537
    if (!rem || !rem->ready)
538
      rem = &dummy_af_caps;
614
        if (!rem || !rem->ready)
615
            rem = &dummy_af_caps;
539 616

  
540
    int active = loc->ready && rem->ready;
541
    c->c.disabled = !active;
542
    c->c.reloadable = p->route_refresh;
617
        int active = loc->ready && rem->ready;
618
        c->c.disabled = !active;
619
        c->c.reloadable = p->route_refresh;
543 620

  
544
    c->index = active ? num++ : 0;
621
        c->index = active ? num++ : 0;
545 622

  
546
    c->feed_state = BFS_NONE;
547
    c->load_state = BFS_NONE;
623
        c->feed_state = BFS_NONE;
624
        c->load_state = BFS_NONE;
548 625

  
549
    /* Channels where peer may do GR */
550
    c->gr_ready = active && local->gr_aware && rem->gr_able;
551
    p->gr_ready = p->gr_ready || c->gr_ready;
626
        /* Channels where peer may do GR */
627
        c->gr_ready = active && local->gr_aware && rem->gr_able;
628
        p->gr_ready = p->gr_ready || c->gr_ready;
552 629

  
553
    /* Channels not able to recover gracefully */
554
    if (p->p.gr_recovery && (!active || !peer_gr_ready))
555
      channel_graceful_restart_unlock(&c->c);
630
        /* Channels not able to recover gracefully */
631
        if (p->p.gr_recovery && (!active || !peer_gr_ready))
632
            channel_graceful_restart_unlock(&c->c);
556 633

  
557
    /* Channels waiting for local convergence */
558
    if (p->p.gr_recovery && loc->gr_able && peer_gr_ready)
559
      c->c.gr_wait = 1;
634
        /* Channels waiting for local convergence */
635
        if (p->p.gr_recovery && loc->gr_able && peer_gr_ready)
636
            c->c.gr_wait = 1;
560 637

  
561
    /* Channels where peer is not able to recover gracefully */
562
    if (c->gr_active && ! (c->gr_ready && (rem->gr_af_flags & BGP_GRF_FORWARDING)))
563
      bgp_graceful_restart_done(c);
638
        /* Channels where peer is not able to recover gracefully */
639
        if (c->gr_active && ! (c->gr_ready && (rem->gr_af_flags & BGP_GRF_FORWARDING)))
640
            bgp_graceful_restart_done(c);
564 641

  
565
    /* GR capability implies that neighbor will send End-of-RIB */
566
    if (peer->gr_aware)
567
      c->load_state = BFS_LOADING;
642
        /* GR capability implies that neighbor will send End-of-RIB */
643
        if (peer->gr_aware)
644
            c->load_state = BFS_LOADING;
568 645

  
569
    c->ext_next_hop = c->cf->ext_next_hop && (bgp_channel_is_ipv6(c) || rem->ext_next_hop);
570
    c->add_path_rx = (loc->add_path & BGP_ADD_PATH_RX) && (rem->add_path & BGP_ADD_PATH_TX);
571
    c->add_path_tx = (loc->add_path & BGP_ADD_PATH_TX) && (rem->add_path & BGP_ADD_PATH_RX);
646
        c->ext_next_hop = c->cf->ext_next_hop && (bgp_channel_is_ipv6(c) || rem->ext_next_hop);
647
        c->add_path_rx = (loc->add_path & BGP_ADD_PATH_RX) && (rem->add_path & BGP_ADD_PATH_TX);
648
        c->add_path_tx = (loc->add_path & BGP_ADD_PATH_TX) && (rem->add_path & BGP_ADD_PATH_RX);
572 649

  
573
    /* Update RA mode */
574
    if (c->add_path_tx)
575
      c->c.ra_mode = RA_ANY;
576
    else if (c->cf->secondary)
577
      c->c.ra_mode = RA_ACCEPTED;
578
    else
579
      c->c.ra_mode = RA_OPTIMAL;
580
  }
650
        /* Update RA mode */
651
        if (c->add_path_tx)
652
            c->c.ra_mode = RA_ANY;
653
        else if (c->cf->secondary)
654
            c->c.ra_mode = RA_ACCEPTED;
655
        else
656
            c->c.ra_mode = RA_OPTIMAL;
657
    }
581 658

  
582
  p->afi_map = mb_alloc(p->p.pool, num * sizeof(u32));
583
  p->channel_map = mb_alloc(p->p.pool, num * sizeof(void *));
584
  p->channel_count = num;
659
    p->afi_map = mb_alloc(p->p.pool, num * sizeof(u32));
660
    p->channel_map = mb_alloc(p->p.pool, num * sizeof(void *));
661
    p->channel_count = num;
662

  
663
    WALK_LIST(c, p->p.channels)
664
    {
665
        if (c->c.disabled)
666
            continue;
585 667

  
586
  WALK_LIST(c, p->p.channels)
587
  {
588
    if (c->c.disabled)
589
      continue;
668
        p->afi_map[c->index] = c->afi;
669
        p->channel_map[c->index] = c;
670
    }
590 671

  
591
    p->afi_map[c->index] = c->afi;
592
    p->channel_map[c->index] = c;
593
  }
672
    /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */
594 673

  
595
  /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */
674
    bgp_conn_set_state(conn, BS_ESTABLISHED);
675
    proto_notify_state(&p->p, PS_UP);
596 676

  
597
  bgp_conn_set_state(conn, BS_ESTABLISHED);
598
  proto_notify_state(&p->p, PS_UP);
677
    //Start del timer mrai
678
    //bgp_start_timer(conn->mrai_timer, 5);
599 679
}
600 680

  
601 681
static void
602 682
bgp_conn_leave_established_state(struct bgp_proto *p)
603 683
{
604
  BGP_TRACE(D_EVENTS, "BGP session closed");
605
  p->conn = NULL;
684
    BGP_TRACE(D_EVENTS, "BGP session closed");
685
    p->conn = NULL;
606 686

  
607
  if (p->p.proto_state == PS_UP)
608
    bgp_stop(p, 0, NULL, 0);
687
    if (p->p.proto_state == PS_UP)
688
        bgp_stop(p, 0, NULL, 0);
609 689
}
610 690

  
611 691
void
612 692
bgp_conn_enter_close_state(struct bgp_conn *conn)
613 693
{
614
  struct bgp_proto *p = conn->bgp;
615
  int os = conn->state;
694
    struct bgp_proto *p = conn->bgp;
695
    int os = conn->state;
616 696

  
617
  bgp_conn_set_state(conn, BS_CLOSE);
618
  tm_stop(conn->keepalive_timer);
619
  conn->sk->rx_hook = NULL;
697
    bgp_conn_set_state(conn, BS_CLOSE);
698
    tm_stop(conn->keepalive_timer);
699
    conn->sk->rx_hook = NULL;
620 700

  
621
  /* Timeout for CLOSE state, if we cannot send notification soon then we just hangup */
622
  bgp_start_timer(conn->hold_timer, 10);
701
    /* Timeout for CLOSE state, if we cannot send notification soon then we just hangup */
702
    bgp_start_timer(conn->hold_timer, 10);
623 703

  
624
  if (os == BS_ESTABLISHED)
625
    bgp_conn_leave_established_state(p);
704
    if (os == BS_ESTABLISHED)
705
        bgp_conn_leave_established_state(p);
626 706
}
627 707

  
628 708
void
629 709
bgp_conn_enter_idle_state(struct bgp_conn *conn)
630 710
{
631
  struct bgp_proto *p = conn->bgp;
632
  int os = conn->state;
711
    struct bgp_proto *p = conn->bgp;
712
    int os = conn->state;
633 713

  
634
  bgp_close_conn(conn);
635
  bgp_conn_set_state(conn, BS_IDLE);
636
  ev_schedule(p->event);
714
    bgp_close_conn(conn);
715
    bgp_conn_set_state(conn, BS_IDLE);
716
    ev_schedule(p->event);
637 717

  
638
  if (os == BS_ESTABLISHED)
639
    bgp_conn_leave_established_state(p);
718
    if (os == BS_ESTABLISHED)
719
        bgp_conn_leave_established_state(p);
640 720
}
641 721

  
642 722
/**
......
653 733
void
654 734
bgp_handle_graceful_restart(struct bgp_proto *p)
655 735
{
656
  ASSERT(p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready);
736
    ASSERT(p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready);
657 737

  
658
  BGP_TRACE(D_EVENTS, "Neighbor graceful restart detected%s",
659
	    p->gr_active_num ? " - already pending" : "");
738
    BGP_TRACE(D_EVENTS, "Neighbor graceful restart detected%s",
739
              p->gr_active_num ? " - already pending" : "");
660 740

  
661
  p->gr_active_num = 0;
741
    p->gr_active_num = 0;
662 742

  
663
  struct bgp_channel *c;
664
  WALK_LIST(c, p->p.channels)
665
  {
666
    /* FIXME: perhaps check for channel state instead of disabled flag? */
667
    if (c->c.disabled)
668
      continue;
669

  
670
    if (c->gr_ready)
671
    {
672
      if (c->gr_active)
673
	rt_refresh_end(c->c.table, &c->c);
674

  
675
      c->gr_active = 1;
676
      p->gr_active_num++;
677
      rt_refresh_begin(c->c.table, &c->c);
678
    }
679
    else
743
    struct bgp_channel *c;
744
    WALK_LIST(c, p->p.channels)
680 745
    {
681
      /* Just flush the routes */
682
      rt_refresh_begin(c->c.table, &c->c);
683
      rt_refresh_end(c->c.table, &c->c);
746
        /* FIXME: perhaps check for channel state instead of disabled flag? */
747
        if (c->c.disabled)
748
            continue;
749

  
750
        if (c->gr_ready)
751
        {
752
            if (c->gr_active)
753
                rt_refresh_end(c->c.table, &c->c);
754

  
755
            c->gr_active = 1;
756
            p->gr_active_num++;
757
            rt_refresh_begin(c->c.table, &c->c);
758
        }
759
        else
760
        {
761
            /* Just flush the routes */
762
            rt_refresh_begin(c->c.table, &c->c);
763
            rt_refresh_end(c->c.table, &c->c);
764
        }
765

  
766
        /* Reset bucket and prefix tables */
767
        bgp_free_bucket_table(c);
768
        bgp_free_prefix_table(c);
769
        bgp_init_bucket_table(c);
770
        bgp_init_prefix_table(c);
771
        c->packets_to_send = 0;
684 772
    }
685 773

  
686
    /* Reset bucket and prefix tables */
687
    bgp_free_bucket_table(c);
688
    bgp_free_prefix_table(c);
689
    bgp_init_bucket_table(c);
690
    bgp_init_prefix_table(c);
691
    c->packets_to_send = 0;
692
  }
693

  
694
  /* p->gr_ready -> at least one active channel is c->gr_ready */
695
  ASSERT(p->gr_active_num > 0);
774
    /* p->gr_ready -> at least one active channel is c->gr_ready */
775
    ASSERT(p->gr_active_num > 0);
696 776

  
697
  proto_notify_state(&p->p, PS_START);
698
  bgp_start_timer(p->gr_timer, p->conn->remote_caps->gr_time);
777
    proto_notify_state(&p->p, PS_START);
778
    bgp_start_timer(p->gr_timer, p->conn->remote_caps->gr_time);
699 779
}
700 780

  
701 781
/**
......
711 791
void
712 792
bgp_graceful_restart_done(struct bgp_channel *c)
713 793
{
714
  struct bgp_proto *p = (void *) c->c.proto;
794
    struct bgp_proto *p = (void *) c->c.proto;
715 795

  
716
  ASSERT(c->gr_active);
717
  c->gr_active = 0;
718
  p->gr_active_num--;
796
    ASSERT(c->gr_active);
797
    c->gr_active = 0;
798
    p->gr_active_num--;
719 799

  
720
  if (!p->gr_active_num)
721
    BGP_TRACE(D_EVENTS, "Neighbor graceful restart done");
800
    if (!p->gr_active_num)
801
        BGP_TRACE(D_EVENTS, "Neighbor graceful restart done");
722 802

  
723
  rt_refresh_end(c->c.table, &c->c);
803
    rt_refresh_end(c->c.table, &c->c);
724 804
}
725 805

  
726 806
/**
......
735 815
static void
736 816
bgp_graceful_restart_timeout(timer *t)
737 817
{
738
  struct bgp_proto *p = t->data;
818
    struct bgp_proto *p = t->data;
739 819

  
740
  BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout");
741
  bgp_stop(p, 0, NULL, 0);
820
    BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout");
821
    bgp_stop(p, 0, NULL, 0);
742 822
}
743 823

  
744 824

  
......
755 835
void
756 836
bgp_refresh_begin(struct bgp_channel *c)
757 837
{
758
  struct bgp_proto *p = (void *) c->c.proto;
838
    struct bgp_proto *p = (void *) c->c.proto;
759 839

  
760
  if (c->load_state == BFS_LOADING)
761
  { log(L_WARN "%s: BEGIN-OF-RR received before END-OF-RIB, ignoring", p->p.name); return; }
840
    if (c->load_state == BFS_LOADING)
841
    { log(L_WARN "%s: BEGIN-OF-RR received before END-OF-RIB, ignoring", p->p.name); return; }
762 842

  
763
  c->load_state = BFS_REFRESHING;
764
  rt_refresh_begin(c->c.table, &c->c);
843
    c->load_state = BFS_REFRESHING;
844
    rt_refresh_begin(c->c.table, &c->c);
765 845
}
766 846

  
767 847
/**
......
776 856
void
777 857
bgp_refresh_end(struct bgp_channel *c)
778 858
{
779
  struct bgp_proto *p = (void *) c->c.proto;
859
    struct bgp_proto *p = (void *) c->c.proto;
780 860

  
781
  if (c->load_state != BFS_REFRESHING)
782
  { log(L_WARN "%s: END-OF-RR received without prior BEGIN-OF-RR, ignoring", p->p.name); return; }
861
    if (c->load_state != BFS_REFRESHING)
862
    { log(L_WARN "%s: END-OF-RR received without prior BEGIN-OF-RR, ignoring", p->p.name); return; }
783 863

  
784
  c->load_state = BFS_NONE;
785
  rt_refresh_end(c->c.table, &c->c);
864
    c->load_state = BFS_NONE;
865
    rt_refresh_end(c->c.table, &c->c);
786 866
}
787 867

  
788 868

  
789 869
static void
790 870
bgp_send_open(struct bgp_conn *conn)
791 871
{
792
  DBG("BGP: Sending open\n");
793
  conn->sk->rx_hook = bgp_rx;
794
  conn->sk->tx_hook = bgp_tx;
795
  tm_stop(conn->connect_timer);
796
  bgp_schedule_packet(conn, NULL, PKT_OPEN);
797
  bgp_conn_set_state(conn, BS_OPENSENT);
798
  bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
872
    DBG("BGP: Sending open\n");
873
    conn->sk->rx_hook = bgp_rx;
874
    conn->sk->tx_hook = bgp_tx;
875
    tm_stop(conn->connect_timer);
876
    bgp_schedule_packet(conn, NULL, PKT_OPEN);
877
    bgp_conn_set_state(conn, BS_OPENSENT);
878
    bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time);
879

  
880
    /* My timer startup */
881
    //bgp_start_timer(conn->mrai_timer, 10);
799 882
}
800 883

  
801 884
static void
802 885
bgp_connected(sock *sk)
803 886
{
804
  struct bgp_conn *conn = sk->data;
805
  struct bgp_proto *p = conn->bgp;
887
    struct bgp_conn *conn = sk->data;
888
    struct bgp_proto *p = conn->bgp;
806 889

  
807
  BGP_TRACE(D_EVENTS, "Connected");
808
  bgp_send_open(conn);
890
    BGP_TRACE(D_EVENTS, "Connected");
891
    bgp_send_open(conn);
809 892
}
810 893

  
811 894
static void
812 895
bgp_connect_timeout(timer *t)
813 896
{
814
  struct bgp_conn *conn = t->data;
815
  struct bgp_proto *p = conn->bgp;
897
    struct bgp_conn *conn = t->data;
898
    struct bgp_proto *p = conn->bgp;
816 899

  
817
  DBG("BGP: connect_timeout\n");
818
  if (p->p.proto_state == PS_START)
819
  {
820
    bgp_close_conn(conn);
821
    bgp_connect(p);
822
  }
823
  else
824
    bgp_conn_enter_idle_state(conn);
900
    DBG("BGP: connect_timeout\n");
901
    if (p->p.proto_state == PS_START)
902
    {
903
        bgp_close_conn(conn);
904
        bgp_connect(p);
905
    }
906
    else
907
        bgp_conn_enter_idle_state(conn);
825 908
}
826 909

  
827 910
static void
828 911
bgp_sock_err(sock *sk, int err)
829 912
{
830
  struct bgp_conn *conn = sk->data;
831
  struct bgp_proto *p = conn->bgp;
913
    struct bgp_conn *conn = sk->data;
914
    struct bgp_proto *p = conn->bgp;
832 915

  
833
  /*
834
   * This error hook may be called either asynchronously from main
835
   * loop, or synchronously from sk_send().  But sk_send() is called
836
   * only from bgp_tx() and bgp_kick_tx(), which are both called
837
   * asynchronously from main loop. Moreover, they end if err hook is
838
   * called. Therefore, we could suppose that it is always called
839
   * asynchronously.
840
   */
916
    /*
917
     * This error hook may be called either asynchronously from main
918
     * loop, or synchronously from sk_send().  But sk_send() is called
919
     * only from bgp_tx() and bgp_kick_tx(), which are both called
920
     * asynchronously from main loop. Moreover, they end if err hook is
921
     * called. Therefore, we could suppose that it is always called
922
     * asynchronously.
923
     */
841 924

  
842
  bgp_store_error(p, conn, BE_SOCKET, err);
925
    bgp_store_error(p, conn, BE_SOCKET, err);
843 926

  
844
  if (err)
845
    BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
846
  else
847
    BGP_TRACE(D_EVENTS, "Connection closed");
927
    if (err)
928
        BGP_TRACE(D_EVENTS, "Connection lost (%M)", err);
929
    else
930
        BGP_TRACE(D_EVENTS, "Connection closed");
848 931

  
849
  if ((conn->state == BS_ESTABLISHED) && p->gr_ready)
850
    bgp_handle_graceful_restart(p);
932
    if ((conn->state == BS_ESTABLISHED) && p->gr_ready)
933
        bgp_handle_graceful_restart(p);
851 934

  
852
  bgp_conn_enter_idle_state(conn);
935
    bgp_conn_enter_idle_state(conn);
853 936
}
854 937

  
855 938
static void
856 939
bgp_hold_timeout(timer *t)
857 940
{
858
  struct bgp_conn *conn = t->data;
859
  struct bgp_proto *p = conn->bgp;
941
    struct bgp_conn *conn = t->data;
942
    struct bgp_proto *p = conn->bgp;
860 943

  
861
  DBG("BGP: Hold timeout\n");
944
    DBG("BGP: Hold timeout\n");
862 945

  
863
  /* We are already closing the connection - just do hangup */
864
  if (conn->state == BS_CLOSE)
865
  {
866
    BGP_TRACE(D_EVENTS, "Connection stalled");
867
    bgp_conn_enter_idle_state(conn);
868
    return;
869
  }
946
    /* We are already closing the connection - just do hangup */
947
    if (conn->state == BS_CLOSE)
948
    {
949
        BGP_TRACE(D_EVENTS, "Connection stalled");
950
        bgp_conn_enter_idle_state(conn);
951
        return;
952
    }
870 953

  
871
  /* If there is something in input queue, we are probably congested
872
     and perhaps just not processed BGP packets in time. */
954
    /* If there is something in input queue, we are probably congested
955
       and perhaps just not processed BGP packets in time. */
873 956

  
874
  if (sk_rx_ready(conn->sk) > 0)
875
    bgp_start_timer(conn->hold_timer, 10);
876
  else
877
    bgp_error(conn, 4, 0, NULL, 0);
957
    if (sk_rx_ready(conn->sk) > 0)
958
        bgp_start_timer(conn->hold_timer, 10);
959
    else
960
        bgp_error(conn, 4, 0, NULL, 0);
878 961
}
879 962

  
880 963
static void
881 964
bgp_keepalive_timeout(timer *t)
882 965
{
883
  struct bgp_conn *conn = t->data;
966
    struct bgp_conn *conn = t->data;
884 967

  
885
  DBG("BGP: Keepalive timer\n");
886
  bgp_schedule_packet(conn, NULL, PKT_KEEPALIVE);
968
    DBG("BGP: Keepalive timer\n");
969
    bgp_schedule_packet(conn, NULL, PKT_KEEPALIVE);
887 970

  
888
  /* Kick TX a bit faster */
889
  if (ev_active(conn->tx_ev))
890
    ev_run(conn->tx_ev);
971
    /* Kick TX a bit faster */
972
    if (ev_active(conn->tx_ev))
973
        ev_run(conn->tx_ev);
891 974
}
892 975

  
893 976
static void
894 977
bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
895 978
{
896
  conn->sk = NULL;
897
  conn->bgp = p;
979
    conn->sk = NULL;
980
    conn->bgp = p;
981

  
982
    conn->packets_to_send = 0;
983
    conn->channels_to_send = 0;
984
    conn->last_channel = 0;
985
    conn->last_channel_count = 0;
986
    p->number_of_update_sent = 0;
898 987

  
899
  conn->packets_to_send = 0;
900
  conn->channels_to_send = 0;
901
  conn->last_channel = 0;
902
  conn->last_channel_count = 0;
988
    conn->connect_timer	= tm_new_init(p->p.pool, bgp_connect_timeout,	 conn, 0, 0);
989
    conn->hold_timer 	= tm_new_init(p->p.pool, bgp_hold_timeout,	 conn, 0, 0);
990
    conn->keepalive_timer	= tm_new_init(p->p.pool, bgp_keepalive_timeout, conn, 0, 0);
903 991

  
904
  conn->connect_timer	= tm_new_init(p->p.pool, bgp_connect_timeout,	 conn, 0, 0);
905
  conn->hold_timer 	= tm_new_init(p->p.pool, bgp_hold_timeout,	 conn, 0, 0);
906
  conn->keepalive_timer	= tm_new_init(p->p.pool, bgp_keepalive_timeout, conn, 0, 0);
992
    /* My personal timer */
993
    conn->mrai_timer = tm_new_init(p->p.pool, bgp_mrai_timeout, conn, 0, 0);
907 994

  
908
  conn->tx_ev = ev_new(p->p.pool);
909
  conn->tx_ev->hook = bgp_kick_tx;
910
  conn->tx_ev->data = conn;
995
    conn->tx_ev = ev_new(p->p.pool);
996
    conn->tx_ev->hook = bgp_kick_tx;
997
    conn->tx_ev->data = conn;
911 998
}
912 999

  
913 1000
static void
914 1001
bgp_setup_sk(struct bgp_conn *conn, sock *s)
915 1002
{
916
  s->data = conn;
917
  s->err_hook = bgp_sock_err;
918
  s->fast_rx = 1;
919
  conn->sk = s;
1003
    s->data = conn;
1004
    s->err_hook = bgp_sock_err;
1005
    s->fast_rx = 1;
1006
    conn->sk = s;
920 1007
}
921 1008

  
922 1009
static void
923 1010
bgp_active(struct bgp_proto *p)
924 1011
{
925
  int delay = MAX(1, p->cf->connect_delay_time);
926
  struct bgp_conn *conn = &p->outgoing_conn;
1012
    int delay = MAX(1, p->cf->connect_delay_time);
1013
    struct bgp_conn *conn = &p->outgoing_conn;
927 1014

  
928
  BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
929
  bgp_setup_conn(p, conn);
930
  bgp_conn_set_state(conn, BS_ACTIVE);
931
  bgp_start_timer(conn->connect_timer, delay);
1015
    BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay);
1016
    bgp_setup_conn(p, conn);
1017
    bgp_conn_set_state(conn, BS_ACTIVE);
1018
    bgp_start_timer(conn->connect_timer, delay);
932 1019
}
933 1020

  
934 1021
/**
......
942 1029
static void
943 1030
bgp_connect(struct bgp_proto *p)	/* Enter Connect state and start establishing connection */
944 1031
{
945
  struct bgp_conn *conn = &p->outgoing_conn;
946
  int hops = p->cf->multihop ? : 1;
947

  
948
  DBG("BGP: Connecting\n");
949
  sock *s = sk_new(p->p.pool);
950
  s->type = SK_TCP_ACTIVE;
951
  s->saddr = p->source_addr;
952
  s->daddr = p->cf->remote_ip;
953
  s->dport = p->cf->remote_port;
954
  s->iface = p->neigh ? p->neigh->iface : NULL;
955
  s->vrf = p->p.vrf;
956
  s->ttl = p->cf->ttl_security ? 255 : hops;
957
  s->rbsize = p->cf->enable_extended_messages ? BGP_RX_BUFFER_EXT_SIZE : BGP_RX_BUFFER_SIZE;
958
  s->tbsize = p->cf->enable_extended_messages ? BGP_TX_BUFFER_EXT_SIZE : BGP_TX_BUFFER_SIZE;
959
  s->tos = IP_PREC_INTERNET_CONTROL;
960
  s->password = p->cf->password;
961
  s->tx_hook = bgp_connected;
962
  BGP_TRACE(D_EVENTS, "Connecting to %I%J from local address %I%J", s->daddr, p->cf->iface,
963
	    s->saddr, ipa_is_link_local(s->saddr) ? s->iface : NULL);
964
  bgp_setup_conn(p, conn);
965
  bgp_setup_sk(conn, s);
966
  bgp_conn_set_state(conn, BS_CONNECT);
967

  
968
  if (sk_open(s) < 0)
969
    goto err;
970

  
971
  /* Set minimal receive TTL if needed */
972
  if (p->cf->ttl_security)
973
    if (sk_set_min_ttl(s, 256 - hops) < 0)
974
      goto err;
975

  
976
  DBG("BGP: Waiting for connect success\n");
977
  bgp_start_timer(conn->connect_timer, p->cf->connect_retry_time);
978
  return;
979

  
980
err:
981
  sk_log_error(s, p->p.name);
982
  bgp_sock_err(s, 0);
983
  return;
1032
    struct bgp_conn *conn = &p->outgoing_conn;
1033
    int hops = p->cf->multihop ? : 1;
1034

  
1035
    srand((unsigned int)time(NULL));
1036

  
1037
    DBG("BGP: Connecting\n");
1038
    sock *s = sk_new(p->p.pool);
1039
    s->type = SK_TCP_ACTIVE;
1040
    s->saddr = p->source_addr;
1041
    s->daddr = p->cf->remote_ip;
1042
    s->dport = p->cf->remote_port;
1043
    s->iface = p->neigh ? p->neigh->iface : NULL;
1044
    s->vrf = p->p.vrf;
1045
    s->ttl = p->cf->ttl_security ? 255 : hops;
1046
    s->rbsize = p->cf->enable_extended_messages ? BGP_RX_BUFFER_EXT_SIZE : BGP_RX_BUFFER_SIZE;
1047
    s->tbsize = p->cf->enable_extended_messages ? BGP_TX_BUFFER_EXT_SIZE : BGP_TX_BUFFER_SIZE;
1048
    s->tos = IP_PREC_INTERNET_CONTROL;
1049
    s->password = p->cf->password;
1050
    s->tx_hook = bgp_connected;
1051
    BGP_TRACE(D_EVENTS, "Connecting to %I%J from local address %I%J", s->daddr, p->cf->iface,
1052
              s->saddr, ipa_is_link_local(s->saddr) ? s->iface : NULL);
1053
    bgp_setup_conn(p, conn);
1054
    bgp_setup_sk(conn, s);
1055
    bgp_conn_set_state(conn, BS_CONNECT);
1056

  
1057
    if (sk_open(s) < 0)
1058
        goto err;
1059

  
1060
    /* Set minimal receive TTL if needed */
1061
    if (p->cf->ttl_security)
1062
        if (sk_set_min_ttl(s, 256 - hops) < 0)
1063
            goto err;
1064

  
1065
    DBG("BGP: Waiting for connect success\n");
1066
    bgp_start_timer(conn->connect_timer, p->cf->connect_retry_time);
1067
    return;
1068

  
1069
    err:
1070
    sk_log_error(s, p->p.name);
1071
    bgp_sock_err(s, 0);
1072
    return;
984 1073
}
985 1074

  
986 1075
/**
......
991 1080
static struct bgp_proto *
992 1081
bgp_find_proto(sock *sk)
993 1082
{
994
  struct bgp_proto *p;
1083
    struct bgp_proto *p;
995 1084

  
996
  WALK_LIST(p, proto_list)
1085
    WALK_LIST(p, proto_list)
997 1086
    if ((p->p.proto == &proto_bgp) &&
998
	ipa_equal(p->cf->remote_ip, sk->daddr) &&
999
	(!p->cf->iface  || (p->cf->iface == sk->iface)) &&
1000
	(ipa_zero(p->cf->local_ip) || ipa_equal(p->cf->local_ip, sk->saddr)) &&
1001
	(p->cf->local_port == sk->sport))
1002
      return p;
1087
        ipa_equal(p->cf->remote_ip, sk->daddr) &&
1088
        (!p->cf->iface  || (p->cf->iface == sk->iface)) &&
1089
        (ipa_zero(p->cf->local_ip) || ipa_equal(p->cf->local_ip, sk->saddr)) &&
1090
        (p->cf->local_port == sk->sport))
1091
        return p;
1003 1092

  
1004
  return NULL;
1093
    return NULL;
1005 1094
}
1006 1095

  
1007 1096
/**
......
1019 1108
static int
1020 1109
bgp_incoming_connection(sock *sk, uint dummy UNUSED)
1021 1110
{
1022
  struct bgp_proto *p;
1023
  int acc, hops;
1111
    struct bgp_proto *p;
1112
    int acc, hops;
1024 1113

  
1025
  DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
1026
  p = bgp_find_proto(sk);
1027
  if (!p)
1028
  {
1029
    log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)",
1030
	sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, sk->dport);
1031
    rfree(sk);
1032
    return 0;
1033
  }
1034

  
1035
  /*
1036
   * BIRD should keep multiple incoming connections in OpenSent state (for
1037
   * details RFC 4271 8.2.1 par 3), but it keeps just one. Duplicate incoming
1038
   * connections are rejected istead. The exception is the case where an
1039
   * incoming connection triggers a graceful restart.
1040
   */
1041

  
1042
  acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
1043
    (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
1044

  
1045
  if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
1046
  {
1047
    bgp_store_error(p, NULL, BE_MISC, BEM_GRACEFUL_RESTART);
1048
    bgp_handle_graceful_restart(p);
1049
    bgp_conn_enter_idle_state(p->conn);
1050
    acc = 1;
1051

  
1052
    /* There might be separate incoming connection in OpenSent state */
1053
    if (p->incoming_conn.state > BS_ACTIVE)
1054
      bgp_close_conn(&p->incoming_conn);
1055
  }
1056

  
1057
  BGP_TRACE(D_EVENTS, "Incoming connection from %I%J (port %d) %s",
1058
	    sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL,
1059
	    sk->dport, acc ? "accepted" : "rejected");
1060

  
1061
  if (!acc)
1062
  {
1063
    rfree(sk);
1064
    return 0;
1065
  }
1114
    DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport);
1115
    p = bgp_find_proto(sk);
1116
    if (!p)
1117
    {
1118
        log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)",
1119
                sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, sk->dport);
1120
        rfree(sk);
1121
        return 0;
1122
    }
1123

  
1124
    /*
1125
     * BIRD should keep multiple incoming connections in OpenSent state (for
1126
     * details RFC 4271 8.2.1 par 3), but it keeps just one. Duplicate incoming
1127
     * connections are rejected istead. The exception is the case where an
1128
     * incoming connection triggers a graceful restart.
1129
     */
1130

  
1131
    acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) &&
1132
          (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk);
1066 1133

  
1067
  hops = p->cf->multihop ? : 1;
1134
    if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready)
1135
    {
1136
        bgp_store_error(p, NULL, BE_MISC, BEM_GRACEFUL_RESTART);
1137
        bgp_handle_graceful_restart(p);
1138
        bgp_conn_enter_idle_state(p->conn);
1139
        acc = 1;
1140

  
1141
        /* There might be separate incoming connection in OpenSent state */
1142
        if (p->incoming_conn.state > BS_ACTIVE)
1143
            bgp_close_conn(&p->incoming_conn);
1144
    }
1145

  
1146
    BGP_TRACE(D_EVENTS, "Incoming connection from %I%J (port %d) %s",
1147
              sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL,
1148
              sk->dport, acc ? "accepted" : "rejected");
1068 1149

  
1069
  if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0)
1070
    goto err;
1150
    if (!acc)
1151
    {
1152
        rfree(sk);
1153
        return 0;
1154
    }
1071 1155

  
1072
  if (p->cf->ttl_security)
1073
    if (sk_set_min_ttl(sk, 256 - hops) < 0)
1074
      goto err;
1156
    hops = p->cf->multihop ? : 1;
1075 1157

  
1076
  if (p->cf->enable_extended_messages)
1077
  {
1078
    sk->rbsize = BGP_RX_BUFFER_EXT_SIZE;
1079
    sk->tbsize = BGP_TX_BUFFER_EXT_SIZE;
1080
    sk_reallocate(sk);
1081
  }
1158
    if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0)
1159
        goto err;
1082 1160

  
1083
  bgp_setup_conn(p, &p->incoming_conn);
1084
  bgp_setup_sk(&p->incoming_conn, sk);
1085
  bgp_send_open(&p->incoming_conn);
1086
  return 0;
1161
    if (p->cf->ttl_security)
1162
        if (sk_set_min_ttl(sk, 256 - hops) < 0)
1163
            goto err;
1087 1164

  
1088
err:
1089
  sk_log_error(sk, p->p.name);
1090
  log(L_ERR "%s: Incoming connection aborted", p->p.name);
1091
  rfree(sk);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff