Statistics
| Branch: | Revision:

iof-bird-daemon / proto / rip / rip.c @ 3cb6c83f

History | View | Annotate | Download (27.4 KB)

1
/*
2
 *        Rest in pieces - RIP protocol
3
 *
4
 *        Copyright (c) 1998, 1999 Pavel Machek <pavel@ucw.cz>
5
 *                      2004       Ondrej Filip <feela@network.cz>
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 *
9
         FIXME: IpV6 support: packet size
10
        FIXME: (nonurgent) IpV6 support: receive "route using" blocks
11
        FIXME: (nonurgent) IpV6 support: generate "nexthop" blocks
12
                next hops are only advisory, and they are pretty ugly in IpV6.
13
                I suggest just forgetting about them.
14

15
        FIXME: (nonurgent): fold rip_connection into rip_interface?
16

17
        FIXME: (nonurgent) allow bigger frequencies than 1 regular update in 6 seconds (?)
18
        FIXME: propagation of metric=infinity into main routing table may or may not be good idea.
19
 */
20

    
21
/**
22
 * DOC: Routing Information Protocol
23
 *
24
 * RIP is a pretty simple protocol, so about a half of its code is interface
25
 * with the core.
26
 *
27
 * We maintain our own linked list of &rip_entry structures -- it serves
28
 * as our small routing table. RIP never adds to this linked list upon
29
 * packet reception; instead, it lets the core know about data from the packet
30
 * and waits for the core to call rip_rte_notify().
31
 *
32
 * Within rip_tx(), the list is
33
 * walked and a packet is generated using rip_tx_prepare(). This gets
34
 * tricky because we may need to send more than one packet to one
35
 * destination. Struct &rip_connection is used to hold context information such as how
36
 * many of &rip_entry's we have already sent and it's also used to protect
37
 * against two concurrent sends to one destination. Each &rip_interface has
38
 * at most one &rip_connection.
39
 *
40
 * We are not going to honor requests for sending part of
41
 * routing table. That would need to turn split horizon off etc.  
42
 *
43
 * About triggered updates, RFC says: when a triggered update was sent,
44
 * don't send a new one for something between 1 and 5 seconds (and send one
45
 * after that). We do something else: each 5 seconds,
46
 * we look for any changed routes and broadcast them.
47
 */
48

    
49
#undef LOCAL_DEBUG
50

    
51
#include "nest/bird.h"
52
#include "nest/iface.h"
53
#include "nest/protocol.h"
54
#include "nest/route.h"
55
#include "lib/socket.h"
56
#include "lib/resource.h"
57
#include "lib/lists.h"
58
#include "lib/timer.h"
59
#include "lib/string.h"
60

    
61
#include "rip.h"
62
#include <assert.h>
63

    
64
#define P ((struct rip_proto *) p)
65
#define P_CF ((struct rip_proto_config *)p->cf)
66

    
67
#define TRACE(level, msg, args...) do { if (p->debug & level) { log(L_TRACE "%s: " msg, p->name , ## args); } } while(0)
68

    
69
static struct rip_interface *new_iface(struct proto *p, struct iface *new, unsigned long flags, struct iface_patt *patt);
70

    
71
/*
72
 * Output processing
73
 *
74
 * This part is responsible for getting packets out to the network.
75
 */
76

    
77
static void
78
rip_tx_err( sock *s, int err )
79
{
80
  struct rip_connection *c = ((struct rip_interface *)(s->data))->busy;
81
  struct proto *p = c->proto;
82
  log( L_ERR "%s: Unexpected error at rip transmit: %M", p->name, err );
83
}
84

    
85
/*
86
 * rip_tx_prepare:
87
 * @e: rip entry that needs to be translated to form suitable for network
88
 * @b: block to be filled
89
 *
90
 * Fill one rip block with info that needs to go to the network. Handle
91
 * nexthop and split horizont correctly. (Next hop is ignored for IPv6,
92
 * that could be fixed but it is not real problem).
93
 */
94
static int
95
rip_tx_prepare(struct proto *p, struct rip_block *b, struct rip_entry *e, struct rip_interface *rif, int pos )
96
{
97
  int metric;
98
  DBG( "." );
99
  b->tag     = htons( e->tag );
100
  b->network = e->n.prefix;
101
  metric = e->metric;
102
  if (neigh_connected_to(p, &e->whotoldme, rif->iface)) {
103
    DBG( "(split horizon)" );
104
    metric = P_CF->infinity;
105
  }
106
#ifndef IPV6
107
  b->family  = htons( 2 ); /* AF_INET */
108
  b->netmask = ipa_mkmask( e->n.pxlen );
109
  ipa_hton( b->netmask );
110

    
111
  if (neigh_connected_to(p, &e->nexthop, rif->iface))
112
    b->nexthop = e->nexthop;
113
  else
114
    b->nexthop = IPA_NONE;
115
  ipa_hton( b->nexthop );  
116
  b->metric  = htonl( metric );
117
#else
118
  b->pxlen = e->n.pxlen;
119
  b->metric  = metric; /* it is u8 */
120
#endif
121

    
122
  ipa_hton( b->network );
123

    
124
  return pos+1;
125
}
126

    
127
/*
128
 * rip_tx - send one rip packet to the network
129
 */
130
static void
131
rip_tx( sock *s )
132
{
133
  struct rip_interface *rif = s->data;
134
  struct rip_connection *c = rif->busy;
135
  struct proto *p = c->proto;
136
  struct rip_packet *packet = (void *) s->tbuf;
137
  int i, packetlen;
138
  int maxi, nullupdate = 1;
139

    
140
  DBG( "Sending to %I\n", s->daddr );
141
  do {
142

    
143
    if (c->done)
144
      goto done;
145

    
146
    DBG( "Preparing packet to send: " );
147

    
148
    packet->heading.command = RIPCMD_RESPONSE;
149
    packet->heading.version = RIP_V2;
150
    packet->heading.unused  = 0;
151

    
152
    i = !!P_CF->authtype;
153
#ifndef IPV6
154
    maxi = ((P_CF->authtype == AT_MD5) ? PACKET_MD5_MAX : PACKET_MAX);
155
#else
156
    maxi = 5; /* We need to have at least reserve of one at end of packet */
157
#endif
158
    
159
    FIB_ITERATE_START(&P->rtable, &c->iter, z) {
160
      struct rip_entry *e = (struct rip_entry *) z;
161

    
162
      if (!rif->triggered || (!(e->updated < now-5))) {
163
        nullupdate = 0;
164
        i = rip_tx_prepare( p, packet->block + i, e, rif, i );
165
        if (i >= maxi) {
166
          FIB_ITERATE_PUT(&c->iter, z);
167
          goto break_loop;
168
        }
169
      }
170
    } FIB_ITERATE_END(z);
171
    c->done = 1;
172

    
173
  break_loop:
174

    
175
    packetlen = rip_outgoing_authentication(p, (void *) &packet->block[0], packet, i);
176

    
177
    DBG( ", sending %d blocks, ", i );
178
    if (nullupdate) {
179
      DBG( "not sending NULL update\n" );
180
      c->done = 1;
181
      goto done;
182
    }
183
    if (ipa_nonzero(c->daddr))
184
      i = sk_send_to( s, packetlen, c->daddr, c->dport );
185
    else
186
      i = sk_send( s, packetlen );
187

    
188
    DBG( "it wants more\n" );
189
  
190
  } while (i>0);
191
  
192
  if (i<0) rip_tx_err( s, i );
193
  DBG( "blocked\n" );
194
  return;
195

    
196
done:
197
  DBG( "Looks like I'm" );
198
  c->rif->busy = NULL;
199
  rem_node(NODE c);
200
  mb_free(c);
201
  DBG( " done\n" );
202
  return;
203
}
204

    
205
/* 
206
 * rip_sendto - send whole routing table to selected destination
207
 * @rif: interface to use. Notice that we lock interface so that at
208
 * most one send to one interface is done.
209
 */
210
static void
211
rip_sendto( struct proto *p, ip_addr daddr, int dport, struct rip_interface *rif )
212
{
213
  struct iface *iface = rif->iface;
214
  struct rip_connection *c;
215
  static int num = 0;
216

    
217
  if (rif->busy) {
218
    log (L_WARN "%s: Interface %s is much too slow, dropping request", p->name, iface->name);
219
    return;
220
  }
221
  c = mb_alloc( p->pool, sizeof( struct rip_connection ));
222
  rif->busy = c;
223
  
224
  c->addr = daddr;
225
  c->proto = p;
226
  c->num = num++;
227
  c->rif = rif;
228

    
229
  c->dport = dport;
230
  c->daddr = daddr;
231
  if (c->rif->sock->data != rif)
232
    bug("not enough send magic");
233

    
234
  c->done = 0;
235
  FIB_ITERATE_INIT( &c->iter, &P->rtable );
236
  add_head( &P->connections, NODE c );
237
  if (ipa_nonzero(daddr))
238
    TRACE(D_PACKETS, "Sending my routing table to %I:%d on %s", daddr, dport, rif->iface->name );
239
  else
240
    TRACE(D_PACKETS, "Broadcasting routing table to %s", rif->iface->name );
241

    
242
  rip_tx(c->rif->sock);
243
}
244

    
245
static struct rip_interface*
246
find_interface(struct proto *p, struct iface *what)
247
{
248
  struct rip_interface *i;
249

    
250
  WALK_LIST (i, P->interfaces)
251
    if (i->iface == what)
252
      return i;
253
  return NULL;
254
}
255

    
256
/*
257
 * Input processing
258
 *
259
 * This part is responsible for any updates that come from network 
260
 */
261

    
262
static void
263
rip_rte_update_if_better(rtable *tab, net *net, struct proto *p, rte *new)
264
{
265
  rte *old;
266

    
267
  old = rte_find(net, p);
268
  if (!old || p->rte_better(new, old) ||
269
      (ipa_equal(old->attrs->from, new->attrs->from) &&
270
      (old->u.rip.metric != new->u.rip.metric)) )
271
    rte_update(tab, net, p, p, new);
272
  else
273
    rte_free(new);
274
}
275

    
276
/*
277
 * advertise_entry - let main routing table know about our new entry
278
 * @b: entry in network format
279
 *
280
 * This basically translates @b to format used by bird core and feeds
281
 * bird core with this route.
282
 */
283
static void
284
advertise_entry( struct proto *p, struct rip_block *b, ip_addr whotoldme )
285
{
286
  rta *a, A;
287
  rte *r;
288
  net *n;
289
  neighbor *neighbor;
290
  struct rip_interface *rif;
291
  int pxlen;
292

    
293
  bzero(&A, sizeof(A));
294
  A.proto = p;
295
  A.source = RTS_RIP;
296
  A.scope = SCOPE_UNIVERSE;
297
  A.cast = RTC_UNICAST;
298
  A.dest = RTD_ROUTER;
299
  A.flags = 0;
300
#ifndef IPV6
301
  A.gw = ipa_nonzero(b->nexthop) ? b->nexthop : whotoldme;
302
  pxlen = ipa_mklen(b->netmask);
303
#else
304
  /* FIXME: next hop is in other packet for v6 */
305
  A.gw = whotoldme; 
306
  pxlen = b->pxlen;
307
#endif
308
  A.from = whotoldme;
309

    
310
  /* No need to look if destination looks valid - ie not net 0 or 127 -- core will do for us. */
311

    
312
  neighbor = neigh_find( p, &A.gw, 0 );
313
  if (!neighbor) {
314
    log( L_REMOTE "%s: %I asked me to route %I/%d using not-neighbor %I.", p->name, A.from, b->network, pxlen, A.gw );
315
    return;
316
  }
317
  if (neighbor->scope == SCOPE_HOST) {
318
    DBG("Self-destined route, ignoring.\n");
319
    return;
320
  }
321

    
322
  A.iface = neighbor->iface;
323
  if (!(rif = neighbor->data)) {
324
    rif = neighbor->data = find_interface(p, A.iface);
325
  }
326
  if (!rif)
327
    bug("Route packet using unknown interface? No.");
328
    
329
  /* set to: interface of nexthop */
330
  a = rta_lookup(&A);
331
  if (pxlen==-1)  {
332
    log( L_REMOTE "%s: %I gave me invalid pxlen/netmask for %I.", p->name, A.from, b->network );
333
    return;
334
  }
335
  n = net_get( p->table, b->network, pxlen );
336
  r = rte_get_temp(a);
337
#ifndef IPV6
338
  r->u.rip.metric = ntohl(b->metric) + rif->metric;
339
#else  
340
  r->u.rip.metric = b->metric + rif->metric;
341
#endif
342

    
343
  r->u.rip.entry = NULL;
344
  if (r->u.rip.metric > P_CF->infinity) r->u.rip.metric = P_CF->infinity;
345
  r->u.rip.tag = ntohl(b->tag);
346
  r->net = n;
347
  r->pflags = 0; /* Here go my flags */
348
  rip_rte_update_if_better( p->table, n, p, r );
349
  DBG( "done\n" );
350
}
351

    
352
/*
353
 * process_block - do some basic check and pass block to advertise_entry
354
 */
355
static void
356
process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme )
357
{
358
#ifndef IPV6
359
  int metric = ntohl( block->metric );
360
#else
361
  int metric = block->metric;
362
#endif
363
  ip_addr network = block->network;
364

    
365
  CHK_MAGIC;
366
#ifdef IPV6
367
  TRACE(D_ROUTES, "block: %I tells me: %I/%d available, metric %d... ",
368
      whotoldme, network, block->pxlen, metric );
369
#else
370
  TRACE(D_ROUTES, "block: %I tells me: %I/%d available, metric %d... ",
371
      whotoldme, network, ipa_mklen(block->netmask), metric );
372
#endif
373

    
374
  if ((!metric) || (metric > P_CF->infinity)) {
375
#ifdef IPV6 /* Someone is sedning us nexthop and we are ignoring it */
376
    if (metric == 0xff)
377
      { DBG( "IpV6 nexthop ignored" ); return; }
378
#endif
379
    log( L_WARN "%s: Got metric %d from %I", p->name, metric, whotoldme );
380
    return;
381
  }
382

    
383
  advertise_entry( p, block, whotoldme );
384
}
385

    
386
#define BAD( x ) { log( L_REMOTE "%s: " x, p->name ); return 1; }
387

    
388
/*
389
 * rip_process_packet - this is main routine for incoming packets.
390
 */
391
static int
392
rip_process_packet( struct proto *p, struct rip_packet *packet, int num, ip_addr whotoldme, int port )
393
{
394
  int i;
395
  int authenticated = 0;
396
  neighbor *neighbor;
397

    
398
  switch( packet->heading.version ) {
399
  case RIP_V1: DBG( "Rip1: " ); break;
400
  case RIP_V2: DBG( "Rip2: " ); break;
401
  default: BAD( "Unknown version" );
402
  }
403

    
404
  switch( packet->heading.command ) {
405
  case RIPCMD_REQUEST: DBG( "Asked to send my routing table\n" ); 
406
          if (P_CF->honor == HO_NEVER)
407
            BAD( "They asked me to send routing table, but I was told not to do it" );
408

    
409
          if ((P_CF->honor == HO_NEIGHBOR) && (!neigh_find( p, &whotoldme, 0 )))
410
            BAD( "They asked me to send routing table, but he is not my neighbor" );
411
              rip_sendto( p, whotoldme, port, HEAD(P->interfaces) ); /* no broadcast */
412
          break;
413
  case RIPCMD_RESPONSE: DBG( "*** Rtable from %I\n", whotoldme ); 
414
          if (port != P_CF->port) {
415
            log( L_REMOTE "%s: %I send me routing info from port %d", p->name, whotoldme, port );
416
            return 1;
417
          }
418

    
419
          if (!(neighbor = neigh_find( p, &whotoldme, 0 )) || neighbor->scope == SCOPE_HOST) {
420
            log( L_REMOTE "%s: %I send me routing info but he is not my neighbor", p->name, whotoldme );
421
            return 0;
422
          }
423

    
424
          for (i=0; i<num; i++) {
425
            struct rip_block *block = &packet->block[i];
426
#ifndef IPV6
427
            /* Authentication is not defined for v6 */
428
            if (block->family == 0xffff) {
429
              if (i)
430
                continue;        /* md5 tail has this family */
431
              if (rip_incoming_authentication(p, (void *) block, packet, num, whotoldme))
432
                BAD( "Authentication failed" );
433
              authenticated = 1;
434
              continue;
435
            }
436
#endif
437
            if ((!authenticated) && (P_CF->authtype != AT_NONE))
438
              BAD( "Packet is not authenticated and it should be" );
439
            ipa_ntoh( block->network );
440
#ifndef IPV6
441
            ipa_ntoh( block->netmask );
442
            ipa_ntoh( block->nexthop );
443
            if (packet->heading.version == RIP_V1)        /* FIXME (nonurgent): switch to disable this? */
444
              block->netmask = ipa_class_mask(block->network);
445
#endif
446
            process_block( p, block, whotoldme );
447
          }
448
          break;
449
  case RIPCMD_TRACEON:
450
  case RIPCMD_TRACEOFF: BAD( "I was asked for traceon/traceoff" );
451
  case 5: BAD( "Some Sun extension around here" );
452
  default: BAD( "Unknown command" );
453
  }
454

    
455
  return 0;
456
}
457

    
458
/*
459
 * rip_rx - Receive hook: do basic checks and pass packet to rip_process_packet
460
 */
461
static int
462
rip_rx(sock *s, int size)
463
{
464
  struct rip_interface *i = s->data;
465
  struct proto *p = i->proto;
466
  int num;
467

    
468
  /* In non-listening mode, just ignore packet */
469
  if (i->mode & IM_NOLISTEN)
470
    return 1;
471

    
472
  CHK_MAGIC;
473
  DBG( "RIP: message came: %d bytes from %I via %s\n", size, s->faddr, i->iface ? i->iface->name : "(dummy)" );
474
  size -= sizeof( struct rip_packet_heading );
475
  if (size < 0) BAD( "Too small packet" );
476
  if (size % sizeof( struct rip_block )) BAD( "Odd sized packet" );
477
  num = size / sizeof( struct rip_block );
478
  if (num>PACKET_MAX) BAD( "Too many blocks" );
479

    
480
#ifdef IPV6
481
  /* Try to absolutize link scope addresses */
482
  ipa_absolutize(&s->faddr, &i->iface->addr->ip);
483
#endif
484

    
485
  if (ipa_equal(i->iface->addr->ip, s->faddr)) {
486
    DBG("My own packet\n");
487
    return 1;
488
  }
489

    
490
  rip_process_packet( p, (struct rip_packet *) s->rbuf, num, s->faddr, s->fport );
491
  return 1;
492
}
493

    
494
/*
495
 * Interface to BIRD core
496
 */
497

    
498
static void
499
rip_dump_entry( struct rip_entry *e )
500
{
501
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d ", 
502
  e->whotoldme, e->updated-now, e->changed-now, e->n.prefix, e->n.pxlen, e->nexthop, e->metric );
503
  debug( "\n" );
504
}
505

    
506
/**
507
 * rip_timer
508
 * @t: timer
509
 *
510
 * Broadcast routing tables periodically (using rip_tx) and kill
511
 * routes that are too old. RIP keeps a list of its own entries present
512
 * in the core table by a linked list (functions rip_rte_insert() and
513
 * rip_rte_delete() are responsible for that), it walks this list in the timer
514
 * and in case an entry is too old, it is discarded.
515
 */
516

    
517
static void
518
rip_timer(timer *t)
519
{
520
  struct proto *p = t->data;
521
  struct fib_node *e, *et;
522

    
523
  CHK_MAGIC;
524
  DBG( "RIP: tick tock\n" );
525
  
526
  WALK_LIST_DELSAFE( e, et, P->garbage ) {
527
    rte *rte;
528
    rte = SKIP_BACK( struct rte, u.rip.garbage, e );
529
#ifdef LOCAL_DEBUG
530
    {
531
      struct proto *p = rte->attrs->proto;
532
      CHK_MAGIC;
533
    }
534
    DBG( "Garbage: (%p)", rte ); rte_dump( rte );
535
#endif
536

    
537
    if (now - rte->lastmod > P_CF->timeout_time) {
538
      TRACE(D_EVENTS, "entry is too old: %I", rte->net->n.prefix );
539
      if (rte->u.rip.entry) {
540
        rte->u.rip.entry->metric = P_CF->infinity;
541
        rte->u.rip.metric = P_CF->infinity;
542
      }
543
    }
544

    
545
    if (now - rte->lastmod > P_CF->garbage_time) {
546
      TRACE(D_EVENTS, "entry is much too old: %I", rte->net->n.prefix );
547
      rte_discard(p->table, rte);
548
    }
549
  }
550

    
551
  DBG( "RIP: Broadcasting routing tables\n" );
552
  {
553
    struct rip_interface *rif;
554
    WALK_LIST( rif, P->interfaces ) {
555
      struct iface *iface = rif->iface;
556

    
557
      if (!iface) continue;
558
      if (rif->mode & IM_QUIET) continue;
559
      if (!(iface->flags & IF_UP)) continue;
560

    
561
      rif->triggered = (P->tx_count % 6);
562
      rip_sendto( p, IPA_NONE, 0, rif );
563
    }
564
    P->tx_count ++;
565
  }
566

    
567
  DBG( "RIP: tick tock done\n" );
568
}
569

    
570
/*
571
 * rip_start - initialize instance of rip
572
 */
573
static int
574
rip_start(struct proto *p)
575
{
576
  struct rip_interface *rif;
577
  DBG( "RIP: starting instance...\n" );
578

    
579
  assert( sizeof(struct rip_packet_heading) == 4);
580
  assert( sizeof(struct rip_block) == 20);
581
  assert( sizeof(struct rip_block_auth) == 20);
582

    
583
#ifdef LOCAL_DEBUG
584
  P->magic = RIP_MAGIC;
585
#endif
586
  fib_init( &P->rtable, p->pool, sizeof( struct rip_entry ), 0, NULL );
587
  init_list( &P->connections );
588
  init_list( &P->garbage );
589
  init_list( &P->interfaces );
590
  P->timer = tm_new( p->pool );
591
  P->timer->data = p;
592
  P->timer->randomize = 5;
593
  P->timer->recurrent = (P_CF->period / 6)+1; 
594
  P->timer->hook = rip_timer;
595
  tm_start( P->timer, 5 );
596
  rif = new_iface(p, NULL, 0, NULL);        /* Initialize dummy interface */
597
  add_head( &P->interfaces, NODE rif );
598
  CHK_MAGIC;
599

    
600
  rip_init_instance(p);
601

    
602
  DBG( "RIP: ...done\n");
603
  return PS_UP;
604
}
605

    
606
static struct proto *
607
rip_init(struct proto_config *cfg)
608
{
609
  struct proto *p = proto_new(cfg, sizeof(struct rip_proto));
610

    
611
  return p;
612
}
613

    
614
static void
615
rip_dump(struct proto *p)
616
{
617
  int i;
618
  node *w;
619
  struct rip_interface *rif;
620
  i = 0;
621

    
622
  CHK_MAGIC;
623
  WALK_LIST( w, P->connections ) {
624
    struct rip_connection *n = (void *) w;
625
    debug( "RIP: connection #%d: %I\n", n->num, n->addr );
626
  }
627
  i = 0;
628
  FIB_WALK( &P->rtable, e ) {
629
    debug( "RIP: entry #%d: ", i++ );
630
    rip_dump_entry( (struct rip_entry *)e );
631
  } FIB_WALK_END;
632
  i = 0;
633
  WALK_LIST( rif, P->interfaces ) {
634
    debug( "RIP: interface #%d: %s, %I, busy = %x\n", i++, rif->iface?rif->iface->name:"(dummy)", rif->sock->daddr, rif->busy );
635
  }
636
}
637

    
638
static void
639
rip_get_route_info(rte *rte, byte *buf, ea_list *attrs)
640
{
641
  eattr *metric = ea_find(attrs, EA_RIP_METRIC);
642
  eattr *tag = ea_find(attrs, EA_RIP_TAG);
643

    
644
  buf += bsprintf(buf, " (%d/%d)", rte->pref, metric ? metric->u.data : 0);
645
  if (tag && tag->u.data)
646
    bsprintf(buf, " t%04x", tag->u.data);
647
}
648

    
649
static void
650
kill_iface(struct rip_interface *i)
651
{
652
  DBG( "RIP: Interface %s disappeared\n", i->iface->name);
653
  rfree(i->sock);
654
  mb_free(i);
655
}
656

    
657
/**
658
 * new_iface
659
 * @p: myself
660
 * @new: interface to be created or %NULL if we are creating a magic
661
 * socket. The magic socket is used for listening and also for
662
 * sending requested responses.
663
 * @flags: interface flags
664
 * @patt: pattern this interface matched, used for access to config options
665
 *
666
 * Create an interface structure and start listening on the interface.
667
 */
668
static struct rip_interface *
669
new_iface(struct proto *p, struct iface *new, unsigned long flags, struct iface_patt *patt )
670
{
671
  struct rip_interface *rif;
672
  struct rip_patt *PATT = (struct rip_patt *) patt;
673

    
674
  rif = mb_allocz(p->pool, sizeof( struct rip_interface ));
675
  rif->iface = new;
676
  rif->proto = p;
677
  rif->busy = NULL;
678
  if (PATT) {
679
    rif->mode = PATT->mode;
680
    rif->metric = PATT->metric;
681
    rif->multicast = (!(PATT->mode & IM_BROADCAST)) && (flags & IF_MULTICAST);
682
  }
683
  /* lookup multicasts over unnumbered links - no: rip is not defined over unnumbered links */
684

    
685
  if (rif->multicast)
686
    DBG( "Doing multicasts!\n" );
687

    
688
  rif->sock = sk_new( p->pool );
689
  rif->sock->type = SK_UDP;
690
  rif->sock->sport = P_CF->port;
691
  rif->sock->rx_hook = rip_rx;
692
  rif->sock->data = rif;
693
  rif->sock->rbsize = 10240;
694
  rif->sock->iface = new;                /* Automagically works for dummy interface */
695
  rif->sock->tbuf = mb_alloc( p->pool, sizeof( struct rip_packet ));
696
  rif->sock->tx_hook = rip_tx;
697
  rif->sock->err_hook = rip_tx_err;
698
  rif->sock->daddr = IPA_NONE;
699
  rif->sock->dport = P_CF->port;
700
  if (new)
701
    {
702
      rif->sock->ttl = 1;
703
      rif->sock->tos = IP_PREC_INTERNET_CONTROL;
704
    }
705

    
706
  if (new) {
707
    if (new->addr->flags & IA_UNNUMBERED)
708
      log( L_WARN "%s: rip is not defined over unnumbered links", p->name );
709
    if (rif->multicast) {
710
#ifndef IPV6
711
      rif->sock->daddr = ipa_from_u32(0xe0000009);
712
      rif->sock->saddr = ipa_from_u32(0xe0000009);
713
#else
714
      rif->sock->daddr = ipa_build(0xff020000, 0, 0, 9);
715
      rif->sock->saddr = new->addr->ip;
716
#endif
717
    } else {
718
      rif->sock->daddr = new->addr->brd;
719
      rif->sock->saddr = new->addr->brd;
720
    }
721
  }
722

    
723
  if (!ipa_nonzero(rif->sock->daddr)) {
724
    if (rif->iface)
725
      log( L_WARN "%s: interface %s is too strange for me", p->name, rif->iface->name );
726
  } else {
727

    
728
    if (sk_open(rif->sock)<0)
729
      goto err;
730

    
731
    if (rif->multicast)
732
      {
733
        if (sk_setup_multicast(rif->sock) < 0)
734
          goto err;
735
        if (sk_join_group(rif->sock, rif->sock->daddr) < 0)
736
          goto err;
737
      }
738
    else
739
      {
740
        if (sk_set_broadcast(rif->sock, 1) < 0)
741
          goto err;
742
      }
743
  }
744

    
745
  TRACE(D_EVENTS, "Listening on %s, port %d, mode %s (%I)", rif->iface ? rif->iface->name : "(dummy)", P_CF->port, rif->multicast ? "multicast" : "broadcast", rif->sock->daddr );
746
  
747
  return rif;
748

    
749
 err:
750
  log( L_ERR "%s: could not create socket for %s", p->name, rif->iface ? rif->iface->name : "(dummy)" );
751
  if (rif->iface) {
752
    rfree(rif->sock);
753
    mb_free(rif);
754
    return NULL;
755
  }
756
  /* On dummy, we just return non-working socket, so that user gets error every time anyone requests table */
757
  return rif;
758
}
759

    
760
static void
761
rip_real_if_add(struct object_lock *lock)
762
{
763
  struct iface *iface = lock->iface;
764
  struct proto *p = lock->data;
765
  struct rip_interface *rif;
766
  struct iface_patt *k = iface_patt_find(&P_CF->iface_list, iface, iface->addr);
767

    
768
  if (!k)
769
    bug("This can not happen! It existed few seconds ago!" );
770
  DBG("adding interface %s\n", iface->name );
771
  rif = new_iface(p, iface, iface->flags, k);
772
  if (rif) {
773
    add_head( &P->interfaces, NODE rif );
774
    DBG("Adding object lock of %p for %p\n", lock, rif);
775
    rif->lock = lock;
776
  } else { rfree(lock); }
777
}
778

    
779
static void
780
rip_if_notify(struct proto *p, unsigned c, struct iface *iface)
781
{
782
  DBG( "RIP: if notify\n" );
783
  if (iface->flags & IF_IGNORE)
784
    return;
785
  if (c & IF_CHANGE_DOWN) {
786
    struct rip_interface *i;
787
    i = find_interface(p, iface);
788
    if (i) {
789
      rem_node(NODE i);
790
      rfree(i->lock);
791
      kill_iface(i);
792
    }
793
  }
794
  if (c & IF_CHANGE_UP) {
795
    struct iface_patt *k = iface_patt_find(&P_CF->iface_list, iface, iface->addr);
796
    struct object_lock *lock;
797
    struct rip_patt *PATT = (struct rip_patt *) k;
798

    
799
    if (!k) return; /* We are not interested in this interface */
800

    
801
    lock = olock_new( p->pool );
802
    if (!(PATT->mode & IM_BROADCAST) && (iface->flags & IF_MULTICAST))
803
#ifndef IPV6
804
      lock->addr = ipa_from_u32(0xe0000009);
805
#else
806
      ip_pton("FF02::9", &lock->addr);
807
#endif
808
    else
809
      lock->addr = iface->addr->brd;
810
    lock->port = P_CF->port;
811
    lock->iface = iface;
812
    lock->hook = rip_real_if_add;
813
    lock->data = p;
814
    lock->type = OBJLOCK_UDP;
815
    olock_acquire(lock);
816
  }
817
}
818

    
819
static struct ea_list *
820
rip_gen_attrs(struct linpool *pool, int metric, u16 tag)
821
{
822
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2*sizeof(eattr));
823

    
824
  l->next = NULL;
825
  l->flags = EALF_SORTED;
826
  l->count = 2;
827
  l->attrs[0].id = EA_RIP_TAG;
828
  l->attrs[0].flags = 0;
829
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
830
  l->attrs[0].u.data = tag;
831
  l->attrs[1].id = EA_RIP_METRIC;
832
  l->attrs[1].flags = 0;
833
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
834
  l->attrs[1].u.data = metric;
835
  return l;
836
}
837

    
838
static int
839
rip_import_control(struct proto *p, struct rte **rt, struct ea_list **attrs, struct linpool *pool)
840
{
841
  if ((*rt)->attrs->proto == p)        /* My own must not be touched */
842
    return 1;
843

    
844
  if ((*rt)->attrs->source != RTS_RIP) {
845
    struct ea_list *new = rip_gen_attrs(pool, 1, 0);
846
    new->next = *attrs;
847
    *attrs = new;
848
  }
849
  return 0;
850
}
851

    
852
static struct ea_list *
853
rip_make_tmp_attrs(struct rte *rt, struct linpool *pool)
854
{
855
  return rip_gen_attrs(pool, rt->u.rip.metric, rt->u.rip.tag);
856
}
857

    
858
static void 
859
rip_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
860
{
861
  rt->u.rip.tag = ea_get_int(attrs, EA_RIP_TAG, 0);
862
  rt->u.rip.metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
863
}
864

    
865
/*
866
 * rip_rt_notify - core tells us about new route (possibly our
867
 * own), so store it into our data structures. 
868
 */
869
static void
870
rip_rt_notify(struct proto *p, struct rtable *table UNUSED, struct network *net,
871
              struct rte *new, struct rte *old, struct ea_list *attrs)
872
{
873
  CHK_MAGIC;
874

    
875
  if (old) {
876
    struct rip_entry *e = fib_find( &P->rtable, &net->n.prefix, net->n.pxlen );
877
    if (!e)
878
      log( L_BUG "%s: Deleting nonexistent entry?!", p->name );
879
    fib_delete( &P->rtable, e );
880
  }
881

    
882
  if (new) {
883
    struct rip_entry *e;
884
#if 0
885
    /* This can happen since feeding of protocols is asynchronous */
886
    if (fib_find( &P->rtable, &net->n.prefix, net->n.pxlen ))
887
      log( L_BUG "%s: Inserting entry which is already there?", p->name );
888
#endif
889
    e = fib_get( &P->rtable, &net->n.prefix, net->n.pxlen );
890

    
891
    e->nexthop = new->attrs->gw;
892
    e->metric = 0;
893
    e->whotoldme = IPA_NONE;
894
    new->u.rip.entry = e;
895

    
896
    e->tag = ea_get_int(attrs, EA_RIP_TAG, 0);
897
    e->metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
898
    if (e->metric > P_CF->infinity)
899
      e->metric = P_CF->infinity;
900

    
901
    if (new->attrs->proto == p)
902
      e->whotoldme = new->attrs->from;
903

    
904
    if (!e->metric)        /* That's okay: this way user can set his own value for external
905
                           routes in rip. */
906
      e->metric = 5;
907
    e->updated = e->changed = now;
908
    e->flags = 0;
909
  }
910
}
911

    
912
static int
913
rip_rte_same(struct rte *new, struct rte *old)
914
{
915
  /* new->attrs == old->attrs always */
916
  return new->u.rip.metric == old->u.rip.metric;
917
}
918

    
919

    
920
static int
921
rip_rte_better(struct rte *new, struct rte *old)
922
{
923
  struct proto *p = new->attrs->proto;
924

    
925
  if (ipa_equal(old->attrs->from, new->attrs->from))
926
    return 1;
927

    
928
  if (old->u.rip.metric < new->u.rip.metric)
929
    return 0;
930

    
931
  if (old->u.rip.metric > new->u.rip.metric)
932
    return 1;
933

    
934
  if (old->attrs->proto == new->attrs->proto)                /* This does not make much sense for different protocols */
935
    if ((old->u.rip.metric == new->u.rip.metric) &&
936
        ((now - old->lastmod) > (P_CF->timeout_time / 2)))
937
      return 1;
938

    
939
  return 0;
940
}
941

    
942
/*
943
 * rip_rte_insert - we maintain linked list of "our" entries in main
944
 * routing table, so that we can timeout them correctly. rip_timer()
945
 * walks the list.
946
 */
947
static void
948
rip_rte_insert(net *net UNUSED, rte *rte)
949
{
950
  struct proto *p = rte->attrs->proto;
951
  CHK_MAGIC;
952
  DBG( "rip_rte_insert: %p\n", rte );
953
  add_head( &P->garbage, &rte->u.rip.garbage );
954
}
955

    
956
/*
957
 * rip_rte_remove - link list maintenance
958
 */
959
static void
960
rip_rte_remove(net *net UNUSED, rte *rte)
961
{
962
  // struct proto *p = rte->attrs->proto;
963
  CHK_MAGIC;
964
  DBG( "rip_rte_remove: %p\n", rte );
965
  rem_node( &rte->u.rip.garbage );
966
}
967

    
968
void
969
rip_init_instance(struct proto *p)
970
{
971
  p->accept_ra_types = RA_OPTIMAL;
972
  p->if_notify = rip_if_notify;
973
  p->rt_notify = rip_rt_notify;
974
  p->import_control = rip_import_control;
975
  p->make_tmp_attrs = rip_make_tmp_attrs;
976
  p->store_tmp_attrs = rip_store_tmp_attrs;
977
  p->rte_better = rip_rte_better;
978
  p->rte_same = rip_rte_same;
979
  p->rte_insert = rip_rte_insert;
980
  p->rte_remove = rip_rte_remove;
981
}
982

    
983
void
984
rip_init_config(struct rip_proto_config *c)
985
{
986
  init_list(&c->iface_list);
987
  c->c.preference = DEF_PREF_RIP;
988
  c->infinity        = 16;
989
  c->port        = 520;
990
  c->period        = 30;
991
  c->garbage_time = 120+180;
992
  c->timeout_time = 120;
993
  c->passwords        = NULL;
994
  c->authtype        = AT_NONE;
995
}
996

    
997
static int
998
rip_get_attr(eattr *a, byte *buf, int buflen UNUSED)
999
{
1000
  switch (a->id) {
1001
  case EA_RIP_METRIC: buf += bsprintf( buf, "metric: %d", a->u.data ); return GA_FULL;
1002
  case EA_RIP_TAG:    buf += bsprintf( buf, "tag: %d", a->u.data );    return GA_FULL;
1003
  default: return GA_UNKNOWN;
1004
  }
1005
}
1006

    
1007
static int
1008
rip_pat_compare(struct rip_patt *a, struct rip_patt *b)
1009
{
1010
  return ((a->metric == b->metric) &&
1011
          (a->mode == b->mode));
1012
}
1013

    
1014
static int
1015
rip_reconfigure(struct proto *p, struct proto_config *c)
1016
{
1017
  struct rip_proto_config *new = (struct rip_proto_config *) c;
1018
  int generic = sizeof(struct proto_config) + sizeof(list) /* + sizeof(struct password_item *) */;
1019

    
1020
  if (!iface_patts_equal(&P_CF->iface_list, &new->iface_list, (void *) rip_pat_compare))
1021
    return 0;
1022
  return !memcmp(((byte *) P_CF) + generic,
1023
                 ((byte *) new) + generic,
1024
                 sizeof(struct rip_proto_config) - generic);
1025
}
1026

    
1027
struct protocol proto_rip = {
1028
  name: "RIP",
1029
  template: "rip%d",
1030
  attr_class: EAP_RIP,
1031
  get_route_info: rip_get_route_info,
1032
  get_attr: rip_get_attr,
1033

    
1034
  init: rip_init,
1035
  dump: rip_dump,
1036
  start: rip_start,
1037
  reconfigure: rip_reconfigure,
1038
};