Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (24.5 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 pretty simple protocol so half of this code is interface
24
 * with core. We maintain our own linklist of &rip_entry - it serves
25
 * as our small routing table. Within rip_tx(), this list is
26
 * walked, and packet is generated using rip_tx_prepare(). This gets
27
 * tricky because we may need to send more than one packet to one
28
 * destination. Struct &rip_connection is used to hold info such as how
29
 * many of &rip_entry ies we already send, and is also used to protect
30
 * from two concurrent sends to one destination. Each &rip_interface has
31
 * at most one &rip_connection.
32
 *
33
 * We are not going to honor requests for sending part of
34
 * routing table. That would need to turn split horizon off,
35
 * etc.  
36
 *
37
 * Triggered updates. RFC says: when triggered update was sent, don't send
38
 * new one for something between 1 and 5 seconds (and send one
39
 * after that). We do something else: once in 5 second
40
 * we look for any changed routes and broadcast them.
41
 */
42

    
43

    
44
#undef LOCAL_DEBUG
45

    
46
#include "nest/bird.h"
47
#include "nest/iface.h"
48
#include "nest/protocol.h"
49
#include "nest/route.h"
50
#include "lib/socket.h"
51
#include "lib/resource.h"
52
#include "lib/lists.h"
53
#include "lib/timer.h"
54
#include "lib/string.h"
55

    
56
#include "rip.h"
57

    
58
#define P ((struct rip_proto *) p)
59
#define P_CF ((struct rip_proto_config *)p->cf)
60
#define E ((struct rip_entry *) e)
61

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

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

    
66
#define P_NAME p->name
67

    
68
/*
69
 * Output processing
70
 */
71

    
72
static void
73
rip_tx_err( sock *s, int err )
74
{
75
  struct rip_connection *c = s->data;
76
  struct proto *p = c->proto;
77
  log( L_ERR "%s: Unexpected error at rip transmit: %M", P_NAME, err );
78
}
79

    
80
static int
81
rip_tx_prepare(struct proto *p, ip_addr daddr, struct rip_block *b, struct rip_entry *e, struct rip_interface *rif, int pos )
82
{
83
  DBG( "." );
84
  b->tag     = htons( e->tag );
85
  b->network = e->n.prefix;
86
#ifndef IPV6
87
  b->family  = htons( 2 ); /* AF_INET */
88
  b->netmask = ipa_mkmask( e->n.pxlen );
89
  ipa_hton( b->netmask );
90

    
91
  if (neigh_connected_to(p, &e->nexthop, rif->iface))
92
    b->nexthop = e->nexthop;
93
  else
94
    b->nexthop = IPA_NONE;
95
  ipa_hton( b->nexthop );
96
#else
97
  b->pxlen = e->n.pxlen;
98
#endif
99
  b->metric  = htonl( e->metric );
100
  if (neigh_connected_to(p, &e->whotoldme, rif->iface)) {
101
    DBG( "(split horizon)" );
102
    b->metric = htonl( P_CF->infinity );
103
  }
104
  ipa_hton( b->network );
105

    
106
  return pos+1;
107
}
108

    
109
static void
110
rip_tx( sock *s )
111
{
112
  struct rip_interface *rif = s->data;
113
  struct rip_connection *c = rif->busy;
114
  struct proto *p = c->proto;
115
  struct rip_packet *packet = (void *) s->tbuf;
116
  int i, packetlen;
117
  int maxi, nullupdate = 1;
118

    
119
  DBG( "Sending to %I\n", s->daddr );
120
  do {
121

    
122
    if (c->done)
123
      goto done;
124

    
125
    DBG( "Preparing packet to send: " );
126

    
127
    packet->heading.command = RIPCMD_RESPONSE;
128
    packet->heading.version = RIP_V2;
129
    packet->heading.unused  = 0;
130

    
131
    i = !!P_CF->authtype;
132
#ifndef IPV6
133
    maxi = ((P_CF->authtype == AT_MD5) ? PACKET_MD5_MAX : PACKET_MAX);
134
#else
135
    maxi = 5; /* We need to have at least reserve of one at end of packet */
136
#endif
137
    
138
    FIB_ITERATE_START(&P->rtable, &c->iter, z) {
139
      struct rip_entry *e = (struct rip_entry *) z;
140

    
141
      if (!rif->triggered || (!(e->updated < now-5))) {
142
        nullupdate = 0;
143
        i = rip_tx_prepare( p, s->daddr, packet->block + i, e, rif, i );
144
        if (i >= maxi) {
145
          FIB_ITERATE_PUT(&c->iter, z);
146
          goto break_loop;
147
        }
148
      }
149
    } FIB_ITERATE_END(z);
150
    c->done = 1;
151

    
152
  break_loop:
153

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

    
156
    DBG( ", sending %d blocks, ", i );
157
    if (nullupdate) {
158
      DBG( "not sending NULL update\n" );
159
      c->done = 1;
160
      goto done;
161
    }
162
    if (ipa_nonzero(c->daddr))
163
      i = sk_send_to( s, packetlen, c->daddr, c->dport );
164
    else
165
      i = sk_send( s, packetlen );
166

    
167
    DBG( "it wants more\n" );
168
  
169
  } while (i>0);
170
  
171
  if (i<0) rip_tx_err( s, i );
172
  DBG( "blocked\n" );
173
  return;
174

    
175
done:
176
  DBG( "Looks like I'm" );
177
  c->rif->busy = NULL;
178
  rem_node(NODE c);
179
  mb_free(c);
180
  DBG( " done\n" );
181
  return;
182
}
183

    
184
static void
185
rip_sendto( struct proto *p, ip_addr daddr, int dport, struct rip_interface *rif )
186
{
187
  struct iface *iface = rif->iface;
188
  struct rip_connection *c;
189
  static int num = 0;
190

    
191
  if (rif->busy) {
192
    log (L_WARN "%s: Interface %s is much too slow, dropping request", P_NAME, iface->name);
193
    return;
194
  }
195
  c = mb_alloc( p->pool, sizeof( struct rip_connection ));
196
  rif->busy = c;
197
  
198
  c->addr = daddr;
199
  c->proto = p;
200
  c->num = num++;
201
  c->rif = rif;
202

    
203
  c->dport = dport;
204
  c->daddr = daddr;
205
  if (c->rif->sock->data != rif)
206
    bug("not enough send magic");
207
#if 0
208
  if (sk_open(c->send)<0) {
209
    log( L_ERR "%s Could not open socket for data send to %I:%d on %s", P_NAME, daddr, dport, rif->iface->name );
210
    return;
211
  }
212
#endif
213

    
214
  c->done = 0;
215
  FIB_ITERATE_INIT( &c->iter, &P->rtable );
216
  add_head( &P->connections, NODE c );
217
  if (ipa_nonzero(daddr))
218
    TRACE(D_PACKETS, "Sending my routing table to %I:%d on %s", daddr, dport, rif->iface->name );
219
  else
220
    TRACE(D_PACKETS, "Broadcasting routing table to %s", rif->iface->name );
221

    
222
  rip_tx(c->rif->sock);
223
}
224

    
225
static struct rip_interface*
226
find_interface(struct proto *p, struct iface *what)
227
{
228
  struct rip_interface *i;
229

    
230
  WALK_LIST (i, P->interfaces)
231
    if (i->iface == what)
232
      return i;
233
  return NULL;
234
}
235

    
236
/*
237
 * Input processing
238
 */
239

    
240
static void
241
rip_rte_update_if_better(rtable *tab, net *net, struct proto *p, rte *new)
242
{
243
  rte *old;
244

    
245
  old = rte_find(net, p);
246
  if (!old || p->rte_better(new, old))
247
    rte_update(tab, net, p, new);
248
}
249

    
250
/* Let main routing table know about our new entry */
251
static void
252
advertise_entry( struct proto *p, struct rip_block *b, ip_addr whotoldme )
253
{
254
  rta *a, A;
255
  rte *r;
256
  net *n;
257
  neighbor *neighbor;
258
  struct rip_interface *rif;
259
  int pxlen;
260

    
261
  bzero(&A, sizeof(A));
262
  A.proto = p;
263
  A.source = RTS_RIP;
264
  A.scope = SCOPE_UNIVERSE;
265
  A.cast = RTC_UNICAST;
266
  A.dest = RTD_ROUTER;
267
  A.flags = 0;
268
#ifndef IPV6
269
  A.gw = ipa_nonzero(b->nexthop) ? b->nexthop : whotoldme;
270
  pxlen = ipa_mklen(b->netmask);
271
#else
272
  A.gw = whotoldme; /* FIXME: next hop is in other packet for v6 */
273
  pxlen = b->pxlen;
274
#endif
275
  A.from = whotoldme;
276

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

    
279
  neighbor = neigh_find( p, &A.gw, 0 );
280
  if (!neighbor) {
281
    log( L_REMOTE "%s: %I asked me to route %I/%d using not-neighbor %I.", p->name, A.from, b->network, pxlen, A.gw );
282
    return;
283
  }
284

    
285
  A.iface = neighbor->iface;
286
  if (!(rif = neighbor->data)) {
287
    rif = neighbor->data = find_interface(p, A.iface);
288
  }
289
  if (!rif) {
290
    bug("Route packet using unknown interface? No.");
291
    return;
292
  }
293
    
294
  /* set to: interface of nexthop */
295
  a = rta_lookup(&A);
296
  if (pxlen==-1)  {
297
    log( L_REMOTE "%s: %I gave me invalid pxlen/netmask for %I.", p->name, A.from, b->network );
298
    return;
299
  }
300
  n = net_get( p->table, b->network, pxlen );
301
  r = rte_get_temp(a);
302
  r->u.rip.metric = ntohl(b->metric) + rif->metric;
303
  r->u.rip.entry = NULL;
304
  if (r->u.rip.metric > P_CF->infinity) r->u.rip.metric = P_CF->infinity;
305
  r->u.rip.tag = ntohl(b->tag);
306
  r->net = n;
307
  r->pflags = 0; /* Here go my flags */
308
  rip_rte_update_if_better( p->table, n, p, r );
309
  DBG( "done\n" );
310
}
311

    
312
static void
313
process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme )
314
{
315
  int metric = ntohl( block->metric );
316
  ip_addr network = block->network;
317

    
318
  CHK_MAGIC;
319
  TRACE(D_ROUTES, "block: %I tells me: %I/??? available, metric %d... ", whotoldme, network, metric );
320
  /* FIXME: Why `???'? If prefix is unknown, just don't print it.  [mj] */
321
  if ((!metric) || (metric > P_CF->infinity)) {
322
#ifdef IPV6 /* Someone is sedning us nexthop and we are ignoring it */
323
    if (metric == 0xff)
324
      { debug( "IpV6 nexthop ignored" ); return; }
325
#endif
326
    log( L_WARN "%s: Got metric %d from %I", P_NAME, metric, whotoldme );
327
    return;
328
  }
329

    
330
  advertise_entry( p, block, whotoldme );
331
}
332

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

    
335
static int
336
rip_process_packet( struct proto *p, struct rip_packet *packet, int num, ip_addr whotoldme, int port )
337
{
338
  int i;
339
  int native_class = 0, authenticated = 0;
340

    
341
  switch( packet->heading.version ) {
342
  case RIP_V1: DBG( "Rip1: " ); break;
343
  case RIP_V2: DBG( "Rip2: " ); break;
344
  default: BAD( "Unknown version" );
345
  }
346

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

    
352
          if ((P_CF->honor == HO_NEIGHBOR) && (!neigh_find( p, &whotoldme, 0 )))
353
            BAD( "They asked me to send routing table, but he is not my neighbor" );
354
              rip_sendto( p, whotoldme, port, HEAD(P->interfaces) ); /* no broadcast */
355
          break;
356
  case RIPCMD_RESPONSE: DBG( "*** Rtable from %I\n", whotoldme ); 
357
          if (port != P_CF->port) {
358
            log( L_REMOTE "%s: %I send me routing info from port %d", P_NAME, whotoldme, port );
359
            return 1;
360
          }
361

    
362
          if (!neigh_find( p, &whotoldme, 0 )) {
363
            log( L_REMOTE "%s: %I send me routing info but he is not my neighbor", P_NAME, whotoldme );
364
            return 0;
365
          }
366

    
367
          for (i=0; i<num; i++) {
368
            struct rip_block *block = &packet->block[i];
369
#ifndef IPV6
370
            /* Authentication is not defined for v6 */
371
            if (block->family == 0xffff) {
372
              if (i)
373
                continue;        /* md5 tail has this family */
374
              if (rip_incoming_authentication(p, (void *) block, packet, num, whotoldme))
375
                BAD( "Authentication failed" );
376
              authenticated = 1;
377
              continue;
378
            }
379
#endif
380
            if ((!authenticated) && (P_CF->authtype != AT_NONE))
381
              BAD( "Packet is not authenticated and it should be" );
382
            ipa_ntoh( block->network );
383
#ifndef IPV6
384
            ipa_ntoh( block->netmask );
385
            ipa_ntoh( block->nexthop );
386
            if (packet->heading.version == RIP_V1)        /* FIXME (nonurgent): switch to disable this? */
387
              block->netmask = ipa_class_mask(block->network);
388
#endif
389
            process_block( p, block, whotoldme );
390
          }
391
          break;
392
  case RIPCMD_TRACEON:
393
  case RIPCMD_TRACEOFF: BAD( "I was asked for traceon/traceoff" );
394
  case 5: BAD( "Some Sun extension around here" );
395
  default: BAD( "Unknown command" );
396
  }
397

    
398
  return 0;
399
}
400

    
401
static int
402
rip_rx(sock *s, int size)
403
{
404
  struct rip_interface *i = s->data;
405
  struct proto *p = i->proto;
406
  int num;
407

    
408
  CHK_MAGIC;
409
  DBG( "RIP: message came: %d bytes from %I via %s\n", size, s->faddr, i->iface ? i->iface->name : "(dummy)" );
410
  size -= sizeof( struct rip_packet_heading );
411
  if (size < 0) BAD( "Too small packet" );
412
  if (size % sizeof( struct rip_block )) BAD( "Odd sized packet" );
413
  num = size / sizeof( struct rip_block );
414
  if (num>PACKET_MAX) BAD( "Too many blocks" );
415

    
416
  if (ipa_equal(i->iface->addr->ip, s->faddr)) {
417
    DBG("My own packet\n");
418
    return 1;
419
  }
420

    
421
  rip_process_packet( p, (struct rip_packet *) s->rbuf, num, s->faddr, s->fport );
422
  return 1;
423
}
424

    
425
/*
426
 * Interface to rest of bird
427
 */
428

    
429
static void
430
rip_dump_entry( struct rip_entry *e )
431
{
432
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d ", 
433
  e->whotoldme, e->updated-now, e->changed-now, e->n.prefix, e->n.pxlen, e->nexthop, e->metric );
434
  debug( "\n" );
435
}
436

    
437
static void
438
rip_timer(timer *t)
439
{
440
  struct proto *p = t->data;
441
  struct fib_node *e, *et;
442

    
443
  CHK_MAGIC;
444
  DBG( "RIP: tick tock\n" );
445
  
446
  WALK_LIST_DELSAFE( e, et, P->garbage ) {
447
    rte *rte;
448
    rte = SKIP_BACK( struct rte, u.rip.garbage, e );
449
#ifdef LOCAL_DEBUG
450
    {
451
      struct proto *p = rte->attrs->proto;
452
      CHK_MAGIC;
453
    }
454
    DBG( "Garbage: (%p)", rte ); rte_dump( rte );
455
#endif
456

    
457
    if (now - rte->u.rip.lastmodX > P_CF->timeout_time) {
458
      TRACE(D_EVENTS, "entry is too old: %I", rte->net->n.prefix );
459
      if (rte->u.rip.entry) {
460
        rte->u.rip.entry->metric = P_CF->infinity;
461
        rte->u.rip.metric = P_CF->infinity;
462
      }
463
    }
464

    
465
    if (now - rte->u.rip.lastmodX > P_CF->garbage_time) {
466
      TRACE(D_EVENTS, "entry is much too old: %I", rte->net->n.prefix );
467
      rte_discard(p->table, rte);
468
    }
469
  }
470

    
471
  DBG( "RIP: Broadcasting routing tables\n" );
472
  {
473
    struct rip_interface *rif;
474
    WALK_LIST( rif, P->interfaces ) {
475
      struct iface *iface = rif->iface;
476

    
477
      if (!iface) continue;
478
      if (rif->mode & IM_QUIET) continue;
479
      if (!(iface->flags & IF_UP)) continue;
480

    
481
      rif->triggered = (P->tx_count % 6);
482
      rip_sendto( p, IPA_NONE, 0, rif );
483
    }
484
    P->tx_count ++;
485
  }
486

    
487
  DBG( "RIP: tick tock done\n" );
488
}
489

    
490
/**
491
 * rip_start - initialize instance of rip
492
 */
493
static int
494
rip_start(struct proto *p)
495
{
496
  struct rip_interface *rif;
497
  DBG( "RIP: starting instance...\n" );
498

    
499
  P->magic = RIP_MAGIC;
500
  fib_init( &P->rtable, p->pool, sizeof( struct rip_entry ), 0, NULL );
501
  init_list( &P->connections );
502
  init_list( &P->garbage );
503
  init_list( &P->interfaces );
504
  P->timer = tm_new( p->pool );
505
  P->timer->data = p;
506
  P->timer->randomize = 5;
507
  P->timer->recurrent = (P_CF->period / 6)+1; 
508
  P->timer->hook = rip_timer;
509
  tm_start( P->timer, 5 );
510
  rif = new_iface(p, NULL, 0, NULL);        /* Initialize dummy interface */
511
  add_head( &P->interfaces, NODE rif );
512
  CHK_MAGIC;
513

    
514
  rip_init_instance(p);
515

    
516
  DBG( "RIP: ...done\n");
517
  return PS_UP;
518
}
519

    
520
static struct proto *
521
rip_init(struct proto_config *cfg)
522
{
523
  struct proto *p = proto_new(cfg, sizeof(struct rip_proto));
524

    
525
  return p;
526
}
527

    
528
static void
529
rip_dump(struct proto *p)
530
{
531
  int i;
532
  node *w, *e;
533
  struct rip_interface *rif;
534
  i = 0;
535

    
536
  CHK_MAGIC;
537
  WALK_LIST( w, P->connections ) {
538
    struct rip_connection *n = (void *) w;
539
    debug( "RIP: connection #%d: %I\n", n->num, n->addr );
540
  }
541
  i = 0;
542
  FIB_WALK( &P->rtable, e ) {
543
    debug( "RIP: entry #%d: ", i++ );
544
    rip_dump_entry( E );
545
  } FIB_WALK_END;
546
  i = 0;
547
  WALK_LIST( rif, P->interfaces ) {
548
    debug( "RIP: interface #%d: %s, %I, busy = %x\n", i++, rif->iface?rif->iface->name:"(dummy)", rif->sock->daddr, rif->busy );
549
  }
550
}
551

    
552
static void
553
rip_get_route_info(rte *rte, byte *buf, ea_list *attrs)
554
{
555
  eattr *metric = ea_find(attrs, EA_RIP_METRIC);
556
  eattr *tag = ea_find(attrs, EA_RIP_TAG);
557

    
558
  buf += bsprintf(buf, " (%d/%d)", rte->pref, metric ? metric->u.data : 0);
559
  if (tag && tag->u.data)
560
    bsprintf(buf, " t%04x", tag->u.data);
561
}
562

    
563
static int
564
rip_want_this_if(struct rip_interface *iface)
565
{
566
  return 1;
567
}
568

    
569
static void
570
kill_iface(struct proto *p, struct rip_interface *i)
571
{
572
  DBG( "RIP: Interface %s disappeared\n", i->iface->name);
573
  rfree(i->sock);
574
  mb_free(i);
575
}
576

    
577
/**
578
 * new_iface - actually create struct interface and start listening to it
579
 * @new: interface to be created or %NULL if we are creating magic
580
 * socket. Magic socket is used for listening, and is also used for
581
 * sending requested responses. 
582
 */
583
static struct rip_interface *
584
new_iface(struct proto *p, struct iface *new, unsigned long flags, struct iface_patt *patt )
585
{
586
  struct rip_interface *rif;
587
  struct rip_patt *PATT = (struct rip_patt *) patt;
588

    
589
  rif = mb_allocz(p->pool, sizeof( struct rip_interface ));
590
  rif->iface = new;
591
  rif->proto = p;
592
  rif->busy = NULL;
593
  if (PATT) {
594
    rif->mode = PATT->mode;
595
    rif->metric = PATT->metric;
596
    rif->multicast = (!(PATT->mode & IM_BROADCAST)) && (flags & IF_MULTICAST);
597
  }
598
  /* lookup multicasts over unnumbered links - no: rip is not defined over unnumbered links */
599

    
600
  if (rif->multicast)
601
    DBG( "Doing multicasts!\n" );
602

    
603
  rif->sock = sk_new( p->pool );
604
  rif->sock->type = rif->multicast?SK_UDP_MC:SK_UDP;
605
  rif->sock->sport = P_CF->port;
606
  rif->sock->rx_hook = rip_rx;
607
  rif->sock->data = rif;
608
  rif->sock->rbsize = 10240;
609
  rif->sock->iface = new;                /* Automagically works for dummy interface */
610
  rif->sock->tbuf = mb_alloc( p->pool, sizeof( struct rip_packet ));
611
  rif->sock->tx_hook = rip_tx;
612
  rif->sock->err_hook = rip_tx_err;
613
  rif->sock->daddr = IPA_NONE;
614
  rif->sock->dport = P_CF->port;
615
  if (new)
616
    rif->sock->ttl = 1;
617
  else
618
    rif->sock->ttl = 30;                /* FIXME: Shouldn't we leave default TTL in this case?  [mj] */
619
  rif->sock->tos = IP_PREC_INTERNET_CONTROL; /* FIXME: Interface sockets only, I guess  [mj] */
620

    
621
  if (new) {
622
    if (new->addr->flags & IA_UNNUMBERED)
623
      log( L_WARN "%s: rip is not defined over unnumbered links", P_NAME );
624
    if (rif->multicast) {
625
#ifndef IPV6
626
      rif->sock->daddr = ipa_from_u32(0xe0000009);
627
      rif->sock->saddr = ipa_from_u32(0xe0000009);
628
#else
629
      ip_pton("FF02::9", &rif->sock->daddr); /* FIXME  [mj] */
630
      ip_pton("FF02::9", &rif->sock->saddr);
631
#endif
632
    } else {
633
      rif->sock->daddr = new->addr->brd;
634
      rif->sock->saddr = new->addr->brd;
635
    }
636
  }
637

    
638
  if (!ipa_nonzero(rif->sock->daddr)) {
639
    if (rif->iface)
640
      log( L_WARN "%s: interface %s is too strange for me", P_NAME, rif->iface->name );
641
  } else
642
    if (!(rif->mode & IM_NOLISTEN))
643
      if (sk_open(rif->sock)<0) {
644
        log( L_ERR "%s: could not listen on %s", P_NAME, rif->iface ? rif->iface->name : "(dummy)" );
645
        /* Don't try to transmit into this one? Well, why not? This should not happen, anyway :-) */
646
        /* FIXME: This is *wrong*! Try it. */
647
      }
648

    
649
  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 );
650
  
651
  return rif;
652
}
653

    
654
static void
655
rip_real_if_add(struct object_lock *lock)
656
{
657
  struct iface *iface = lock->iface;
658
  struct proto *p = lock->data;
659
  struct rip_interface *rif;
660
  struct iface_patt *k = iface_patt_match(&P_CF->iface_list, iface);
661

    
662
  if (!k)
663
    bug("This can not happen! It existed few seconds ago!" );
664
  DBG("adding interface %s\n", iface->name );
665
  rif = new_iface(p, iface, iface->flags, k);
666
  add_head( &P->interfaces, NODE rif );
667
  DBG("Adding object lock of %p for %p\n", lock, rif);
668
  rif->lock = lock;
669
}
670

    
671
static void
672
rip_if_notify(struct proto *p, unsigned c, struct iface *iface)
673
{
674
  DBG( "RIP: if notify\n" );
675
  if (iface->flags & IF_IGNORE)
676
    return;
677
  if (c & IF_CHANGE_DOWN) {
678
    struct rip_interface *i;
679
    i = find_interface(p, iface);
680
    if (i) {
681
      rem_node(NODE i);
682
      rfree(i->lock);
683
      kill_iface(p, i);
684
    }
685
  }
686
  if (c & IF_CHANGE_UP) {
687
    struct iface_patt *k = iface_patt_match(&P_CF->iface_list, iface);
688
    struct object_lock *lock;
689

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

    
692
    lock = olock_new( p->pool );
693
#ifndef IPV6
694
    lock->addr = ipa_from_u32(0xe0000009);        /* This is okay: we
695
                                                   may actually use
696
                                                   other address, but
697
                                                   we do not want two
698
                                                   rips at one time,
699
                                                   anyway. */
700
#else
701
    ip_pton("FF02::9", &lock->addr);
702
#endif
703
    lock->port = P_CF->port;
704
    lock->iface = iface;
705
    lock->hook = rip_real_if_add;
706
    lock->data = p;
707
    lock->type = OBJLOCK_UDP;
708
    olock_acquire(lock);
709
  }
710
}
711

    
712
static struct ea_list *
713
rip_gen_attrs(struct proto *p, struct linpool *pool, int metric, u16 tag)
714
{
715
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2*sizeof(eattr));
716

    
717
  l->next = NULL;
718
  l->flags = EALF_SORTED;
719
  l->count = 2;
720
  l->attrs[0].id = EA_RIP_TAG;
721
  l->attrs[0].flags = 0;
722
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
723
  l->attrs[0].u.data = tag;
724
  l->attrs[1].id = EA_RIP_METRIC;
725
  l->attrs[1].flags = 0;
726
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
727
  l->attrs[1].u.data = metric;
728
  return l;
729
}
730

    
731
static int
732
rip_import_control(struct proto *p, struct rte **rt, struct ea_list **attrs, struct linpool *pool)
733
{
734
  if ((*rt)->attrs->proto == p)        /* My own must not be touched */
735
    return 1;
736

    
737
  if ((*rt)->attrs->source != RTS_RIP) {
738
    struct ea_list *new = rip_gen_attrs(p, pool, 1, 0);
739
    new->next = *attrs;
740
    *attrs = new;
741
  }
742
  return 0;
743
}
744

    
745
static struct ea_list *
746
rip_make_tmp_attrs(struct rte *rt, struct linpool *pool)
747
{
748
  struct proto *p = rt->attrs->proto;
749
  return rip_gen_attrs(p, pool, rt->u.rip.metric, rt->u.rip.tag);
750
}
751

    
752
static void 
753
rip_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
754
{
755
  struct proto *p = rt->attrs->proto;
756

    
757
  rt->u.rip.tag = ea_get_int(attrs, EA_RIP_TAG, 0);
758
  rt->u.rip.metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
759
}
760

    
761
static void
762
rip_rt_notify(struct proto *p, struct network *net, struct rte *new, struct rte *old, struct ea_list *attrs)
763
{
764
  CHK_MAGIC;
765

    
766
  if (old) {
767
    struct rip_entry *e = fib_find( &P->rtable, &net->n.prefix, net->n.pxlen );
768
    if (!e)
769
      log( L_BUG "%s: Deleting nonexistent entry?!", P_NAME );
770
    fib_delete( &P->rtable, e );
771
  }
772

    
773
  if (new) {
774
    struct rip_entry *e;
775
#if 0
776
    /* This can happen since feeding of protocols is asynchronous */
777
    if (fib_find( &P->rtable, &net->n.prefix, net->n.pxlen ))
778
      log( L_BUG "%s: Inserting entry which is already there?", P_NAME );
779
#endif
780
    e = fib_get( &P->rtable, &net->n.prefix, net->n.pxlen );
781

    
782
    e->nexthop = new->attrs->gw;
783
    e->metric = 0;
784
    e->whotoldme = IPA_NONE;
785
    new->u.rip.entry = e;
786

    
787
    e->tag = ea_get_int(attrs, EA_RIP_TAG, 0);
788
    e->metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
789
    if (e->metric > P_CF->infinity)
790
      e->metric = P_CF->infinity;
791

    
792
    if (new->attrs->proto == p)
793
      e->whotoldme = new->attrs->from;
794

    
795
    if (!e->metric)        /* That's okay: this way user can set his own value for external
796
                           routes in rip. */
797
      e->metric = 5;
798
    e->updated = e->changed = now;
799
    e->flags = 0;
800
  }
801
}
802

    
803
static int
804
rip_rte_better(struct rte *new, struct rte *old)
805
{
806
  struct proto *p = new->attrs->proto;
807

    
808
  if (ipa_equal(old->attrs->from, new->attrs->from))
809
    return 1;
810

    
811
  if (old->u.rip.metric < new->u.rip.metric)
812
    return 0;
813

    
814
  if (old->u.rip.metric > new->u.rip.metric)
815
    return 1;
816

    
817
  if ((old->u.rip.metric < 16) && (new->u.rip.metric == P_CF->infinity)) {
818
    new->u.rip.lastmodX = now - P_CF->timeout_time;        /* Check this: if new metric is 16, act as it was timed out */
819
  }
820

    
821
  if (old->attrs->proto == new->attrs->proto)                /* This does not make much sense for different protocols */
822
    if ((old->u.rip.metric == new->u.rip.metric) &&
823
        ((now - old->u.rip.lastmodX) > (P_CF->timeout_time / 2)))
824
      return 1;
825

    
826
  return 0;
827
}
828

    
829
static void
830
rip_rte_insert(net *net, rte *rte)
831
{
832
  struct proto *p = rte->attrs->proto;
833
  CHK_MAGIC;
834
  DBG( "rip_rte_insert: %p\n", rte );
835
  rte->u.rip.lastmodX = now;
836
  add_head( &P->garbage, &rte->u.rip.garbage );
837
}
838

    
839
static void
840
rip_rte_remove(net *net, rte *rte)
841
{
842
  struct proto *p = rte->attrs->proto;
843
  CHK_MAGIC;
844
  DBG( "rip_rte_remove: %p\n", rte );
845
  rem_node( &rte->u.rip.garbage );
846
}
847

    
848
void
849
rip_init_instance(struct proto *p)
850
{
851
  p->if_notify = rip_if_notify;
852
  p->rt_notify = rip_rt_notify;
853
  p->import_control = rip_import_control;
854
  p->make_tmp_attrs = rip_make_tmp_attrs;
855
  p->store_tmp_attrs = rip_store_tmp_attrs;
856
  p->rte_better = rip_rte_better;
857
  p->rte_insert = rip_rte_insert;
858
  p->rte_remove = rip_rte_remove;
859
}
860

    
861
void
862
rip_init_config(struct rip_proto_config *c)
863
{
864
  init_list(&c->iface_list);
865
  c->c.preference = DEF_PREF_RIP;
866
  c->infinity        = 16;
867
  c->port        = 520;
868
  c->period        = 30;
869
  c->garbage_time = 120+180;
870
  c->timeout_time = 120;
871
  c->passwords        = NULL;
872
  c->authtype        = AT_NONE;
873
}
874

    
875
static int
876
rip_get_attr(eattr *a, byte *buf)
877
{
878
  unsigned int i = EA_ID(a->id);
879
  struct attr_desc *d;
880

    
881
  switch (a->id) {
882
  case EA_RIP_METRIC: buf += bsprintf( buf, "metric: %d", a->u.data ); return GA_FULL;
883
  case EA_RIP_TAG:    buf += bsprintf( buf, "tag: %d", a->u.data );    return GA_FULL;
884
  default: return GA_UNKNOWN;
885
  }
886
}
887

    
888
static int
889
rip_pat_compare(struct rip_patt *a, struct rip_patt *b)
890
{
891
  return ((a->metric == b->metric) &&
892
          (a->mode == b->mode));
893
}
894

    
895
static int
896
rip_reconfigure(struct proto *p, struct proto_config *c)
897
{
898
  struct rip_proto_config *new = (struct rip_proto_config *) c;
899
  int generic = sizeof(struct proto_config) + sizeof(list) /* + sizeof(struct password_item *) */;
900

    
901
  if (!iface_patts_equal(&P_CF->iface_list, &new->iface_list, (void *) rip_pat_compare))
902
    return 0;
903
  if (!password_same(P_CF->passwords, 
904
                     new->passwords))
905
    return 0;
906
  return !memcmp(((byte *) P_CF) + generic,
907
                 ((byte *) new) + generic,
908
                 sizeof(struct rip_proto_config) - generic);
909
}
910

    
911
struct protocol proto_rip = {
912
  name: "RIP",
913
  template: "rip%d",
914
  attr_class: EAP_RIP,
915
  get_route_info: rip_get_route_info,
916
  get_attr: rip_get_attr,
917

    
918
  init: rip_init,
919
  dump: rip_dump,
920
  start: rip_start,
921
  reconfigure: rip_reconfigure,
922
};