Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (26.7 KB)

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

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

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

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

    
48
#undef LOCAL_DEBUG
49

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

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

    
63
#define P ((struct rip_proto *) p)
64
#define P_CF ((struct rip_proto_config *)p->cf)
65
#define E ((struct rip_entry *) e)
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
#define P_NAME p->name
72

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

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

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

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

    
124
  ipa_hton( b->network );
125

    
126
  return pos+1;
127
}
128

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

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

    
145
    if (c->done)
146
      goto done;
147

    
148
    DBG( "Preparing packet to send: " );
149

    
150
    packet->heading.command = RIPCMD_RESPONSE;
151
    packet->heading.version = RIP_V2;
152
    packet->heading.unused  = 0;
153

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

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

    
175
  break_loop:
176

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

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

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

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

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

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

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

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

    
244
  rip_tx(c->rif->sock);
245
}
246

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

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

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

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

    
269
  old = rte_find(net, p);
270
  if (!old || p->rte_better(new, old))
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
  TRACE(D_ROUTES, "block: %I tells me: %I/??? available, metric %d... ", whotoldme, network, metric );
365
  /* FIXME: Why `???'? If prefix is unknown, just don't print it.  [mj] */
366
  if ((!metric) || (metric > P_CF->infinity)) {
367
#ifdef IPV6 /* Someone is sedning us nexthop and we are ignoring it */
368
    if (metric == 0xff)
369
      { DBG( "IpV6 nexthop ignored" ); return; }
370
#endif
371
    log( L_WARN "%s: Got metric %d from %I", P_NAME, metric, whotoldme );
372
    return;
373
  }
374

    
375
  advertise_entry( p, block, whotoldme );
376
}
377

    
378
#define BAD( x ) { log( L_REMOTE "%s: " x, P_NAME ); return 1; }
379

    
380
/*
381
 * rip_process_packet - this is main routine for incoming packets.
382
 */
383
static int
384
rip_process_packet( struct proto *p, struct rip_packet *packet, int num, ip_addr whotoldme, int port )
385
{
386
  int i;
387
  int authenticated = 0;
388
  neighbor *neighbor;
389

    
390
  switch( packet->heading.version ) {
391
  case RIP_V1: DBG( "Rip1: " ); break;
392
  case RIP_V2: DBG( "Rip2: " ); break;
393
  default: BAD( "Unknown version" );
394
  }
395

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

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

    
411
          if (!(neighbor = neigh_find( p, &whotoldme, 0 )) || neighbor->scope == SCOPE_HOST) {
412
            log( L_REMOTE "%s: %I send me routing info but he is not my neighbor", P_NAME, whotoldme );
413
            return 0;
414
          }
415

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

    
447
  return 0;
448
}
449

    
450
/*
451
 * rip_rx - Receive hook: do basic checks and pass packet to rip_process_packet
452
 */
453
static int
454
rip_rx(sock *s, int size)
455
{
456
  struct rip_interface *i = s->data;
457
  struct proto *p = i->proto;
458
  int num;
459

    
460
  /* In non-listening mode, just ignore packet */
461
  if (i->mode & IM_NOLISTEN)
462
    return 1;
463

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

    
472
#ifdef IPV6
473
  /* Try to absolutize link scope addresses */
474
  ipa_absolutize(&s->faddr, &i->iface->addr->ip);
475
#endif
476

    
477
  if (ipa_equal(i->iface->addr->ip, s->faddr)) {
478
    DBG("My own packet\n");
479
    return 1;
480
  }
481

    
482
  rip_process_packet( p, (struct rip_packet *) s->rbuf, num, s->faddr, s->fport );
483
  return 1;
484
}
485

    
486
/*
487
 * Interface to BIRD core
488
 */
489

    
490
static void
491
rip_dump_entry( struct rip_entry *e )
492
{
493
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d ", 
494
  e->whotoldme, e->updated-now, e->changed-now, e->n.prefix, e->n.pxlen, e->nexthop, e->metric );
495
  debug( "\n" );
496
}
497

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

    
509
static void
510
rip_timer(timer *t)
511
{
512
  struct proto *p = t->data;
513
  struct fib_node *e, *et;
514

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

    
529
    if (now - rte->lastmod > P_CF->timeout_time) {
530
      TRACE(D_EVENTS, "entry is too old: %I", rte->net->n.prefix );
531
      if (rte->u.rip.entry) {
532
        rte->u.rip.entry->metric = P_CF->infinity;
533
        rte->u.rip.metric = P_CF->infinity;
534
      }
535
    }
536

    
537
    if (now - rte->lastmod > P_CF->garbage_time) {
538
      TRACE(D_EVENTS, "entry is much too old: %I", rte->net->n.prefix );
539
      rte_discard(p->table, rte);
540
    }
541
  }
542

    
543
  DBG( "RIP: Broadcasting routing tables\n" );
544
  {
545
    struct rip_interface *rif;
546
    WALK_LIST( rif, P->interfaces ) {
547
      struct iface *iface = rif->iface;
548

    
549
      if (!iface) continue;
550
      if (rif->mode & IM_QUIET) continue;
551
      if (!(iface->flags & IF_UP)) continue;
552

    
553
      rif->triggered = (P->tx_count % 6);
554
      rip_sendto( p, IPA_NONE, 0, rif );
555
    }
556
    P->tx_count ++;
557
  }
558

    
559
  DBG( "RIP: tick tock done\n" );
560
}
561

    
562
/*
563
 * rip_start - initialize instance of rip
564
 */
565
static int
566
rip_start(struct proto *p)
567
{
568
  struct rip_interface *rif;
569
  DBG( "RIP: starting instance...\n" );
570

    
571
  assert( sizeof(struct rip_packet_heading) == 4);
572
  assert( sizeof(struct rip_block) == 20);
573
  assert( sizeof(struct rip_block_auth) == 20);
574

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

    
592
  rip_init_instance(p);
593

    
594
  DBG( "RIP: ...done\n");
595
  return PS_UP;
596
}
597

    
598
static struct proto *
599
rip_init(struct proto_config *cfg)
600
{
601
  struct proto *p = proto_new(cfg, sizeof(struct rip_proto));
602

    
603
  return p;
604
}
605

    
606
static void
607
rip_dump(struct proto *p)
608
{
609
  int i;
610
  node *w;
611
  struct rip_interface *rif;
612
  i = 0;
613

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

    
630
static void
631
rip_get_route_info(rte *rte, byte *buf, ea_list *attrs)
632
{
633
  eattr *metric = ea_find(attrs, EA_RIP_METRIC);
634
  eattr *tag = ea_find(attrs, EA_RIP_TAG);
635

    
636
  buf += bsprintf(buf, " (%d/%d)", rte->pref, metric ? metric->u.data : 0);
637
  if (tag && tag->u.data)
638
    bsprintf(buf, " t%04x", tag->u.data);
639
}
640

    
641
static void
642
kill_iface(struct rip_interface *i)
643
{
644
  DBG( "RIP: Interface %s disappeared\n", i->iface->name);
645
  rfree(i->sock);
646
  mb_free(i);
647
}
648

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

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

    
677
  if (rif->multicast)
678
    DBG( "Doing multicasts!\n" );
679

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

    
698
  if (new) {
699
    if (new->addr->flags & IA_UNNUMBERED)
700
      log( L_WARN "%s: rip is not defined over unnumbered links", P_NAME );
701
    if (rif->multicast) {
702
#ifndef IPV6
703
      rif->sock->daddr = ipa_from_u32(0xe0000009);
704
      rif->sock->saddr = ipa_from_u32(0xe0000009);
705
#else
706
      rif->sock->daddr = ipa_build(0xff020000, 0, 0, 9);
707
      rif->sock->saddr = new->addr->ip;
708
#endif
709
    } else {
710
      rif->sock->daddr = new->addr->brd;
711
      rif->sock->saddr = new->addr->brd;
712
    }
713
  }
714

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

    
728
  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 );
729
  
730
  return rif;
731
}
732

    
733
static void
734
rip_real_if_add(struct object_lock *lock)
735
{
736
  struct iface *iface = lock->iface;
737
  struct proto *p = lock->data;
738
  struct rip_interface *rif;
739
  struct iface_patt *k = iface_patt_match(&P_CF->iface_list, iface);
740

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

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

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

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

    
792
static struct ea_list *
793
rip_gen_attrs(struct linpool *pool, int metric, u16 tag)
794
{
795
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2*sizeof(eattr));
796

    
797
  l->next = NULL;
798
  l->flags = EALF_SORTED;
799
  l->count = 2;
800
  l->attrs[0].id = EA_RIP_TAG;
801
  l->attrs[0].flags = 0;
802
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
803
  l->attrs[0].u.data = tag;
804
  l->attrs[1].id = EA_RIP_METRIC;
805
  l->attrs[1].flags = 0;
806
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
807
  l->attrs[1].u.data = metric;
808
  return l;
809
}
810

    
811
static int
812
rip_import_control(struct proto *p, struct rte **rt, struct ea_list **attrs, struct linpool *pool)
813
{
814
  if ((*rt)->attrs->proto == p)        /* My own must not be touched */
815
    return 1;
816

    
817
  if ((*rt)->attrs->source != RTS_RIP) {
818
    struct ea_list *new = rip_gen_attrs(pool, 1, 0);
819
    new->next = *attrs;
820
    *attrs = new;
821
  }
822
  return 0;
823
}
824

    
825
static struct ea_list *
826
rip_make_tmp_attrs(struct rte *rt, struct linpool *pool)
827
{
828
  return rip_gen_attrs(pool, rt->u.rip.metric, rt->u.rip.tag);
829
}
830

    
831
static void 
832
rip_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
833
{
834
  rt->u.rip.tag = ea_get_int(attrs, EA_RIP_TAG, 0);
835
  rt->u.rip.metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
836
}
837

    
838
/*
839
 * rip_rt_notify - core tells us about new route (possibly our
840
 * own), so store it into our data structures. 
841
 */
842
static void
843
rip_rt_notify(struct proto *p, struct network *net, struct rte *new, struct rte *old, struct ea_list *attrs)
844
{
845
  CHK_MAGIC;
846

    
847
  if (old) {
848
    struct rip_entry *e = fib_find( &P->rtable, &net->n.prefix, net->n.pxlen );
849
    if (!e)
850
      log( L_BUG "%s: Deleting nonexistent entry?!", P_NAME );
851
    fib_delete( &P->rtable, e );
852
  }
853

    
854
  if (new) {
855
    struct rip_entry *e;
856
#if 0
857
    /* This can happen since feeding of protocols is asynchronous */
858
    if (fib_find( &P->rtable, &net->n.prefix, net->n.pxlen ))
859
      log( L_BUG "%s: Inserting entry which is already there?", P_NAME );
860
#endif
861
    e = fib_get( &P->rtable, &net->n.prefix, net->n.pxlen );
862

    
863
    e->nexthop = new->attrs->gw;
864
    e->metric = 0;
865
    e->whotoldme = IPA_NONE;
866
    new->u.rip.entry = e;
867

    
868
    e->tag = ea_get_int(attrs, EA_RIP_TAG, 0);
869
    e->metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
870
    if (e->metric > P_CF->infinity)
871
      e->metric = P_CF->infinity;
872

    
873
    if (new->attrs->proto == p)
874
      e->whotoldme = new->attrs->from;
875

    
876
    if (!e->metric)        /* That's okay: this way user can set his own value for external
877
                           routes in rip. */
878
      e->metric = 5;
879
    e->updated = e->changed = now;
880
    e->flags = 0;
881
  }
882
}
883

    
884
static int
885
rip_rte_better(struct rte *new, struct rte *old)
886
{
887
  struct proto *p = new->attrs->proto;
888

    
889
  if (ipa_equal(old->attrs->from, new->attrs->from))
890
    return 1;
891

    
892
  if (old->u.rip.metric < new->u.rip.metric)
893
    return 0;
894

    
895
  if (old->u.rip.metric > new->u.rip.metric)
896
    return 1;
897

    
898
  if (old->attrs->proto == new->attrs->proto)                /* This does not make much sense for different protocols */
899
    if ((old->u.rip.metric == new->u.rip.metric) &&
900
        ((now - old->lastmod) > (P_CF->timeout_time / 2)))
901
      return 1;
902

    
903
  return 0;
904
}
905

    
906
/*
907
 * rip_rte_insert - we maintain linked list of "our" entries in main
908
 * routing table, so that we can timeout them correctly. rip_timer()
909
 * walks the list.
910
 */
911
static void
912
rip_rte_insert(net *net UNUSED, rte *rte)
913
{
914
  struct proto *p = rte->attrs->proto;
915
  CHK_MAGIC;
916
  DBG( "rip_rte_insert: %p\n", rte );
917
  add_head( &P->garbage, &rte->u.rip.garbage );
918
}
919

    
920
/*
921
 * rip_rte_remove - link list maintenance
922
 */
923
static void
924
rip_rte_remove(net *net UNUSED, rte *rte)
925
{
926
  struct proto *p = rte->attrs->proto;
927
  CHK_MAGIC;
928
  DBG( "rip_rte_remove: %p\n", rte );
929
  rem_node( &rte->u.rip.garbage );
930
}
931

    
932
void
933
rip_init_instance(struct proto *p)
934
{
935
  p->if_notify = rip_if_notify;
936
  p->rt_notify = rip_rt_notify;
937
  p->import_control = rip_import_control;
938
  p->make_tmp_attrs = rip_make_tmp_attrs;
939
  p->store_tmp_attrs = rip_store_tmp_attrs;
940
  p->rte_better = rip_rte_better;
941
  p->rte_insert = rip_rte_insert;
942
  p->rte_remove = rip_rte_remove;
943
}
944

    
945
void
946
rip_init_config(struct rip_proto_config *c)
947
{
948
  init_list(&c->iface_list);
949
  c->c.preference = DEF_PREF_RIP;
950
  c->infinity        = 16;
951
  c->port        = 520;
952
  c->period        = 30;
953
  c->garbage_time = 120+180;
954
  c->timeout_time = 120;
955
  c->passwords        = NULL;
956
  c->authtype        = AT_NONE;
957
}
958

    
959
static int
960
rip_get_attr(eattr *a, byte *buf)
961
{
962
  switch (a->id) {
963
  case EA_RIP_METRIC: buf += bsprintf( buf, "metric: %d", a->u.data ); return GA_FULL;
964
  case EA_RIP_TAG:    buf += bsprintf( buf, "tag: %d", a->u.data );    return GA_FULL;
965
  default: return GA_UNKNOWN;
966
  }
967
}
968

    
969
static int
970
rip_pat_compare(struct rip_patt *a, struct rip_patt *b)
971
{
972
  return ((a->metric == b->metric) &&
973
          (a->mode == b->mode));
974
}
975

    
976
static int
977
rip_reconfigure(struct proto *p, struct proto_config *c)
978
{
979
  struct rip_proto_config *new = (struct rip_proto_config *) c;
980
  int generic = sizeof(struct proto_config) + sizeof(list) /* + sizeof(struct password_item *) */;
981

    
982
  if (!iface_patts_equal(&P_CF->iface_list, &new->iface_list, (void *) rip_pat_compare))
983
    return 0;
984
  if (!password_same(P_CF->passwords, 
985
                     new->passwords))
986
    return 0;
987
  return !memcmp(((byte *) P_CF) + generic,
988
                 ((byte *) new) + generic,
989
                 sizeof(struct rip_proto_config) - generic);
990
}
991

    
992
struct protocol proto_rip = {
993
  name: "RIP",
994
  template: "rip%d",
995
  attr_class: EAP_RIP,
996
  get_route_info: rip_get_route_info,
997
  get_attr: rip_get_attr,
998

    
999
  init: rip_init,
1000
  dump: rip_dump,
1001
  start: rip_start,
1002
  reconfigure: rip_reconfigure,
1003
};