Statistics
| Branch: | Revision:

iof-bird-daemon / proto / rip / rip.c @ aebe06b4

History | View | Annotate | Download (27 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, new);
272
}
273

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

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

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

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

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

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

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

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

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

    
381
  advertise_entry( p, block, whotoldme );
382
}
383

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

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

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

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

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

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

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

    
453
  return 0;
454
}
455

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

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

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

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

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

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

    
492
/*
493
 * Interface to BIRD core
494
 */
495

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

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

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

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

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

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

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

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

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

    
565
  DBG( "RIP: tick tock done\n" );
566
}
567

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

    
577
  assert( sizeof(struct rip_packet_heading) == 4);
578
  assert( sizeof(struct rip_block) == 20);
579
  assert( sizeof(struct rip_block_auth) == 20);
580

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

    
598
  rip_init_instance(p);
599

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

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

    
609
  return p;
610
}
611

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

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

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

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

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

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

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

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

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

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

    
721
  if (!ipa_nonzero(rif->sock->daddr)) {
722
    if (rif->iface)
723
      log( L_WARN "%s: interface %s is too strange for me", p->name, rif->iface->name );
724
  } else if (sk_open(rif->sock)<0) {
725
    log( L_ERR "%s: could not create socket for %s", p->name, rif->iface ? rif->iface->name : "(dummy)" );
726
    if (rif->iface) {
727
      rfree(rif->sock);
728
      mb_free(rif);
729
      return NULL;
730
    }
731
    /* On dummy, we just return non-working socket, so that user gets error every time anyone requests table */
732
  }
733

    
734
  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 );
735
  
736
  return rif;
737
}
738

    
739
static void
740
rip_real_if_add(struct object_lock *lock)
741
{
742
  struct iface *iface = lock->iface;
743
  struct proto *p = lock->data;
744
  struct rip_interface *rif;
745
  struct iface_patt *k = iface_patt_match(&P_CF->iface_list, iface);
746

    
747
  if (!k)
748
    bug("This can not happen! It existed few seconds ago!" );
749
  DBG("adding interface %s\n", iface->name );
750
  rif = new_iface(p, iface, iface->flags, k);
751
  if (rif) {
752
    add_head( &P->interfaces, NODE rif );
753
    DBG("Adding object lock of %p for %p\n", lock, rif);
754
    rif->lock = lock;
755
  } else { rfree(lock); }
756
}
757

    
758
static void
759
rip_if_notify(struct proto *p, unsigned c, struct iface *iface)
760
{
761
  DBG( "RIP: if notify\n" );
762
  if (iface->flags & IF_IGNORE)
763
    return;
764
  if (c & IF_CHANGE_DOWN) {
765
    struct rip_interface *i;
766
    i = find_interface(p, iface);
767
    if (i) {
768
      rem_node(NODE i);
769
      rfree(i->lock);
770
      kill_iface(i);
771
    }
772
  }
773
  if (c & IF_CHANGE_UP) {
774
    struct iface_patt *k = iface_patt_match(&P_CF->iface_list, iface);
775
    struct object_lock *lock;
776
    struct rip_patt *PATT = (struct rip_patt *) k;
777

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

    
780
    lock = olock_new( p->pool );
781
    if (!(PATT->mode & IM_BROADCAST) && (iface->flags & IF_MULTICAST))
782
#ifndef IPV6
783
      lock->addr = ipa_from_u32(0xe0000009);
784
#else
785
      ip_pton("FF02::9", &lock->addr);
786
#endif
787
    else
788
      lock->addr = iface->addr->brd;
789
    lock->port = P_CF->port;
790
    lock->iface = iface;
791
    lock->hook = rip_real_if_add;
792
    lock->data = p;
793
    lock->type = OBJLOCK_UDP;
794
    olock_acquire(lock);
795
  }
796
}
797

    
798
static struct ea_list *
799
rip_gen_attrs(struct linpool *pool, int metric, u16 tag)
800
{
801
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2*sizeof(eattr));
802

    
803
  l->next = NULL;
804
  l->flags = EALF_SORTED;
805
  l->count = 2;
806
  l->attrs[0].id = EA_RIP_TAG;
807
  l->attrs[0].flags = 0;
808
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
809
  l->attrs[0].u.data = tag;
810
  l->attrs[1].id = EA_RIP_METRIC;
811
  l->attrs[1].flags = 0;
812
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
813
  l->attrs[1].u.data = metric;
814
  return l;
815
}
816

    
817
static int
818
rip_import_control(struct proto *p, struct rte **rt, struct ea_list **attrs, struct linpool *pool)
819
{
820
  if ((*rt)->attrs->proto == p)        /* My own must not be touched */
821
    return 1;
822

    
823
  if ((*rt)->attrs->source != RTS_RIP) {
824
    struct ea_list *new = rip_gen_attrs(pool, 1, 0);
825
    new->next = *attrs;
826
    *attrs = new;
827
  }
828
  return 0;
829
}
830

    
831
static struct ea_list *
832
rip_make_tmp_attrs(struct rte *rt, struct linpool *pool)
833
{
834
  return rip_gen_attrs(pool, rt->u.rip.metric, rt->u.rip.tag);
835
}
836

    
837
static void 
838
rip_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
839
{
840
  rt->u.rip.tag = ea_get_int(attrs, EA_RIP_TAG, 0);
841
  rt->u.rip.metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
842
}
843

    
844
/*
845
 * rip_rt_notify - core tells us about new route (possibly our
846
 * own), so store it into our data structures. 
847
 */
848
static void
849
rip_rt_notify(struct proto *p, struct network *net, struct rte *new, struct rte *old, struct ea_list *attrs)
850
{
851
  CHK_MAGIC;
852

    
853
  if (old) {
854
    struct rip_entry *e = fib_find( &P->rtable, &net->n.prefix, net->n.pxlen );
855
    if (!e)
856
      log( L_BUG "%s: Deleting nonexistent entry?!", p->name );
857
    fib_delete( &P->rtable, e );
858
  }
859

    
860
  if (new) {
861
    struct rip_entry *e;
862
#if 0
863
    /* This can happen since feeding of protocols is asynchronous */
864
    if (fib_find( &P->rtable, &net->n.prefix, net->n.pxlen ))
865
      log( L_BUG "%s: Inserting entry which is already there?", p->name );
866
#endif
867
    e = fib_get( &P->rtable, &net->n.prefix, net->n.pxlen );
868

    
869
    e->nexthop = new->attrs->gw;
870
    e->metric = 0;
871
    e->whotoldme = IPA_NONE;
872
    new->u.rip.entry = e;
873

    
874
    e->tag = ea_get_int(attrs, EA_RIP_TAG, 0);
875
    e->metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
876
    if (e->metric > P_CF->infinity)
877
      e->metric = P_CF->infinity;
878

    
879
    if (new->attrs->proto == p)
880
      e->whotoldme = new->attrs->from;
881

    
882
    if (!e->metric)        /* That's okay: this way user can set his own value for external
883
                           routes in rip. */
884
      e->metric = 5;
885
    e->updated = e->changed = now;
886
    e->flags = 0;
887
  }
888
}
889

    
890
static int
891
rip_rte_same(struct rte *new, struct rte *old)
892
{
893
  /* new->attrs == old->attrs always */
894
  return new->u.rip.metric == old->u.rip.metric;
895
}
896

    
897

    
898
static int
899
rip_rte_better(struct rte *new, struct rte *old)
900
{
901
  struct proto *p = new->attrs->proto;
902

    
903
  if (ipa_equal(old->attrs->from, new->attrs->from))
904
    return 1;
905

    
906
  if (old->u.rip.metric < new->u.rip.metric)
907
    return 0;
908

    
909
  if (old->u.rip.metric > new->u.rip.metric)
910
    return 1;
911

    
912
  if (old->attrs->proto == new->attrs->proto)                /* This does not make much sense for different protocols */
913
    if ((old->u.rip.metric == new->u.rip.metric) &&
914
        ((now - old->lastmod) > (P_CF->timeout_time / 2)))
915
      return 1;
916

    
917
  return 0;
918
}
919

    
920
/*
921
 * rip_rte_insert - we maintain linked list of "our" entries in main
922
 * routing table, so that we can timeout them correctly. rip_timer()
923
 * walks the list.
924
 */
925
static void
926
rip_rte_insert(net *net UNUSED, rte *rte)
927
{
928
  struct proto *p = rte->attrs->proto;
929
  CHK_MAGIC;
930
  DBG( "rip_rte_insert: %p\n", rte );
931
  add_head( &P->garbage, &rte->u.rip.garbage );
932
}
933

    
934
/*
935
 * rip_rte_remove - link list maintenance
936
 */
937
static void
938
rip_rte_remove(net *net UNUSED, rte *rte)
939
{
940
  struct proto *p = rte->attrs->proto;
941
  CHK_MAGIC;
942
  DBG( "rip_rte_remove: %p\n", rte );
943
  rem_node( &rte->u.rip.garbage );
944
}
945

    
946
void
947
rip_init_instance(struct proto *p)
948
{
949
  p->if_notify = rip_if_notify;
950
  p->rt_notify = rip_rt_notify;
951
  p->import_control = rip_import_control;
952
  p->make_tmp_attrs = rip_make_tmp_attrs;
953
  p->store_tmp_attrs = rip_store_tmp_attrs;
954
  p->rte_better = rip_rte_better;
955
  p->rte_same = rip_rte_same;
956
  p->rte_insert = rip_rte_insert;
957
  p->rte_remove = rip_rte_remove;
958
}
959

    
960
void
961
rip_init_config(struct rip_proto_config *c)
962
{
963
  init_list(&c->iface_list);
964
  c->c.preference = DEF_PREF_RIP;
965
  c->infinity        = 16;
966
  c->port        = 520;
967
  c->period        = 30;
968
  c->garbage_time = 120+180;
969
  c->timeout_time = 120;
970
  c->passwords        = NULL;
971
  c->authtype        = AT_NONE;
972
}
973

    
974
static int
975
rip_get_attr(eattr *a, byte *buf, int buflen UNUSED)
976
{
977
  switch (a->id) {
978
  case EA_RIP_METRIC: buf += bsprintf( buf, "metric: %d", a->u.data ); return GA_FULL;
979
  case EA_RIP_TAG:    buf += bsprintf( buf, "tag: %d", a->u.data );    return GA_FULL;
980
  default: return GA_UNKNOWN;
981
  }
982
}
983

    
984
static int
985
rip_pat_compare(struct rip_patt *a, struct rip_patt *b)
986
{
987
  return ((a->metric == b->metric) &&
988
          (a->mode == b->mode));
989
}
990

    
991
static int
992
rip_reconfigure(struct proto *p, struct proto_config *c)
993
{
994
  struct rip_proto_config *new = (struct rip_proto_config *) c;
995
  int generic = sizeof(struct proto_config) + sizeof(list) /* + sizeof(struct password_item *) */;
996

    
997
  if (!iface_patts_equal(&P_CF->iface_list, &new->iface_list, (void *) rip_pat_compare))
998
    return 0;
999
  return !memcmp(((byte *) P_CF) + generic,
1000
                 ((byte *) new) + generic,
1001
                 sizeof(struct rip_proto_config) - generic);
1002
}
1003

    
1004
struct protocol proto_rip = {
1005
  name: "RIP",
1006
  template: "rip%d",
1007
  attr_class: EAP_RIP,
1008
  get_route_info: rip_get_route_info,
1009
  get_attr: rip_get_attr,
1010

    
1011
  init: rip_init,
1012
  dump: rip_dump,
1013
  start: rip_start,
1014
  reconfigure: rip_reconfigure,
1015
};