Statistics
| Branch: | Revision:

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

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
        FIXME: ea_find can return NULL
20
 */
21

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

    
45

    
46
#undef LOCAL_DEBUG
47

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

    
58
#include "rip.h"
59

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

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

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

    
68
#define P_NAME p->name
69

    
70
/*
71
 * Output processing
72
 */
73

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

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

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

    
108
  return pos+1;
109
}
110

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

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

    
124
    if (c->done)
125
      goto done;
126

    
127
    DBG( "Preparing packet to send: " );
128

    
129
    packet->heading.command = RIPCMD_RESPONSE;
130
    packet->heading.version = RIP_V2;
131
    packet->heading.unused  = 0;
132

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

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

    
154
  break_loop:
155

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

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

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

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

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

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

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

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

    
224
  rip_tx(c->rif->sock);
225
}
226

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

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

    
238
/*
239
 * Input processing
240
 */
241

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

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

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

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

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

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

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

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

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

    
332
  advertise_entry( p, block, whotoldme );
333
}
334

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

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

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

    
349
  switch( packet->heading.command ) {
350
  case RIPCMD_REQUEST: DBG( "Asked to send my routing table\n" ); 
351
          if (P_CF->honor == HO_NEVER) {
352
            log( L_REMOTE "They asked me to send routing table, but I was told not to do it" );
353
            return 0;
354
          }
355
          if ((P_CF->honor == HO_NEIGHBOR) && (!neigh_find( p, &whotoldme, 0 ))) {
356
            log( L_REMOTE "They asked me to send routing table, but he is not my neighbor" );
357
            return 0;
358
          }
359
              rip_sendto( p, whotoldme, port, HEAD(P->interfaces) ); /* no broadcast */
360
          break;
361
  case RIPCMD_RESPONSE: DBG( "*** Rtable from %I\n", whotoldme ); 
362
          if (port != P_CF->port) {
363
            log( L_REMOTE "%I send me routing info from port %d", whotoldme, port );
364
            return 0;
365
          }
366

    
367
          if (!neigh_find( p, &whotoldme, 0 )) {
368
            log( L_REMOTE "%I send me routing info but he is not my neighbor", whotoldme );
369
            return 0;
370
          }
371

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

    
403
  return 0;
404
}
405

    
406
static int
407
rip_rx(sock *s, int size)
408
{
409
  struct rip_interface *i = s->data;
410
  struct proto *p = i->proto;
411
  int num;
412

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

    
421
  if (ipa_equal(i->iface->addr->ip, s->faddr)) {
422
    DBG("My own packet\n");
423
    return 1;
424
  }
425

    
426
  rip_process_packet( p, (struct rip_packet *) s->rbuf, num, s->faddr, s->fport );
427
  return 1;
428
}
429

    
430
/*
431
 * Interface to rest of bird
432
 */
433

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

    
442
static void
443
rip_timer(timer *t)
444
{
445
  struct proto *p = t->data;
446
  struct fib_node *e, *et;
447

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

    
462
    if (now - rte->u.rip.lastmodX > P_CF->timeout_time) {
463
      TRACE(D_EVENTS, "entry is too old: %I", rte->net->n.prefix );
464
      if (rte->u.rip.entry) {
465
        rte->u.rip.entry->metric = P_CF->infinity;
466
        rte->u.rip.metric = P_CF->infinity;
467
      }
468
    }
469

    
470
    if (now - rte->u.rip.lastmodX > P_CF->garbage_time) {
471
      TRACE(D_EVENTS, "entry is much too old: %I", rte->net->n.prefix );
472
      rte_discard(p->table, rte);
473
    }
474
  }
475

    
476
  DBG( "RIP: Broadcasting routing tables\n" );
477
  {
478
    struct rip_interface *rif;
479
    WALK_LIST( rif, P->interfaces ) {
480
      struct iface *iface = rif->iface;
481

    
482
      if (!iface) continue;
483
      if (rif->mode & IM_QUIET) continue;
484
      if (!(iface->flags & IF_UP)) continue;
485

    
486
      rif->triggered = (P->tx_count % 6);
487
      rip_sendto( p, IPA_NONE, 0, rif );
488
    }
489
    P->tx_count ++;
490
  }
491

    
492
  DBG( "RIP: tick tock done\n" );
493
}
494

    
495
/**
496
 * rip_start - initialize instance of rip
497
 */
498
static int
499
rip_start(struct proto *p)
500
{
501
  struct rip_interface *rif;
502
  DBG( "RIP: starting instance...\n" );
503

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

    
519
  rip_init_instance(p);
520

    
521
  DBG( "RIP: ...done\n");
522
  return PS_UP;
523
}
524

    
525
static struct proto *
526
rip_init(struct proto_config *cfg)
527
{
528
  struct proto *p = proto_new(cfg, sizeof(struct rip_proto));
529

    
530
  return p;
531
}
532

    
533
static void
534
rip_dump(struct proto *p)
535
{
536
  int i;
537
  node *w, *e;
538
  struct rip_interface *rif;
539
  i = 0;
540

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

    
557
static void
558
rip_get_route_info(rte *rte, byte *buf, ea_list *attrs)
559
{
560
  eattr *metric = ea_find(attrs, EA_RIP_METRIC);
561
  eattr *tag = ea_find(attrs, EA_RIP_TAG);
562

    
563
  buf += bsprintf(buf, " (%d/%d)", rte->pref, metric ? metric->u.data : 0);
564
  if (tag && tag->u.data)
565
    bsprintf(buf, " t%04x", tag->u.data);
566
}
567

    
568
static int
569
rip_want_this_if(struct rip_interface *iface)
570
{
571
  return 1;
572
}
573

    
574
static void
575
kill_iface(struct proto *p, struct rip_interface *i)
576
{
577
  DBG( "RIP: Interface %s disappeared\n", i->iface->name);
578
  rfree(i->sock);
579
  mb_free(i);
580
}
581

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

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

    
605
  if (rif->multicast)
606
    DBG( "Doing multicasts!\n" );
607

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

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

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

    
654
  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 );
655
  
656
  return rif;
657
}
658

    
659
static void
660
rip_real_if_add(struct object_lock *lock)
661
{
662
  struct iface *iface = lock->iface;
663
  struct proto *p = lock->data;
664
  struct rip_interface *rif;
665
  struct iface_patt *k = iface_patt_match(&P_CF->iface_list, iface);
666

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

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

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

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

    
717
static struct ea_list *
718
rip_gen_attrs(struct proto *p, struct linpool *pool, int metric, u16 tag)
719
{
720
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2*sizeof(eattr));
721

    
722
  l->next = NULL;
723
  l->flags = EALF_SORTED;
724
  l->count = 2;
725
  l->attrs[0].id = EA_RIP_TAG;
726
  l->attrs[0].flags = 0;
727
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
728
  l->attrs[0].u.data = tag;
729
  l->attrs[1].id = EA_RIP_METRIC;
730
  l->attrs[1].flags = 0;
731
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
732
  l->attrs[1].u.data = metric;
733
  return l;
734
}
735

    
736
static int
737
rip_import_control(struct proto *p, struct rte **rt, struct ea_list **attrs, struct linpool *pool)
738
{
739
  if ((*rt)->attrs->proto == p)        /* My own must not be touched */
740
    return 1;
741

    
742
  if ((*rt)->attrs->source != RTS_RIP) {
743
    struct ea_list *new = rip_gen_attrs(p, pool, 1, 0);
744
    new->next = *attrs;
745
    *attrs = new;
746
  }
747
  return 0;
748
}
749

    
750
static struct ea_list *
751
rip_make_tmp_attrs(struct rte *rt, struct linpool *pool)
752
{
753
  struct proto *p = rt->attrs->proto;
754
  return rip_gen_attrs(p, pool, rt->u.rip.metric, rt->u.rip.tag);
755
}
756

    
757
static void 
758
rip_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
759
{
760
  struct proto *p = rt->attrs->proto;
761

    
762
  rt->u.rip.tag = ea_get_int(attrs, EA_RIP_TAG, 0);
763
  rt->u.rip.metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
764
}
765

    
766
static void
767
rip_rt_notify(struct proto *p, struct network *net, struct rte *new, struct rte *old, struct ea_list *attrs)
768
{
769
  CHK_MAGIC;
770

    
771
  if (old) {
772
    struct rip_entry *e = fib_find( &P->rtable, &net->n.prefix, net->n.pxlen );
773
    if (!e)
774
      log( L_BUG "Deleting nonexistent entry?!" );
775
    fib_delete( &P->rtable, e );
776
  }
777

    
778
  if (new) {
779
    struct rip_entry *e;
780
#if 0
781
    /* This can happen since feeding of protocols is asynchronous */
782
    if (fib_find( &P->rtable, &net->n.prefix, net->n.pxlen ))
783
      log( L_BUG "Inserting entry which is already there?" );
784
#endif
785
    e = fib_get( &P->rtable, &net->n.prefix, net->n.pxlen );
786

    
787
    e->nexthop = new->attrs->gw;
788
    e->metric = 0;
789
    e->whotoldme = IPA_NONE;
790
    new->u.rip.entry = e;
791

    
792
    e->tag = ea_get_int(attrs, EA_RIP_TAG, 0);
793
    e->metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
794
    if (e->metric > P_CF->infinity)
795
      e->metric = P_CF->infinity;
796

    
797
    if (new->attrs->proto == p)
798
      e->whotoldme = new->attrs->from;
799

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

    
808
static int
809
rip_rte_better(struct rte *new, struct rte *old)
810
{
811
  struct proto *p = new->attrs->proto;
812

    
813
  if (ipa_equal(old->attrs->from, new->attrs->from))
814
    return 1;
815

    
816
  if (old->u.rip.metric < new->u.rip.metric)
817
    return 0;
818

    
819
  if (old->u.rip.metric > new->u.rip.metric)
820
    return 1;
821

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

    
826
  if (old->attrs->proto == new->attrs->proto)                /* This does not make much sense for different protocols */
827
    if ((old->u.rip.metric == new->u.rip.metric) &&
828
        ((now - old->u.rip.lastmodX) > (P_CF->timeout_time / 2)))
829
      return 1;
830

    
831
  return 0;
832
}
833

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

    
844
static void
845
rip_rte_remove(net *net, rte *rte)
846
{
847
  struct proto *p = rte->attrs->proto;
848
  CHK_MAGIC;
849
  DBG( "rip_rte_remove: %p\n", rte );
850
  rem_node( &rte->u.rip.garbage );
851
}
852

    
853
void
854
rip_init_instance(struct proto *p)
855
{
856
  p->if_notify = rip_if_notify;
857
  p->rt_notify = rip_rt_notify;
858
  p->import_control = rip_import_control;
859
  p->make_tmp_attrs = rip_make_tmp_attrs;
860
  p->store_tmp_attrs = rip_store_tmp_attrs;
861
  p->rte_better = rip_rte_better;
862
  p->rte_insert = rip_rte_insert;
863
  p->rte_remove = rip_rte_remove;
864
}
865

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

    
880
static int
881
rip_get_attr(eattr *a, byte *buf)
882
{
883
  unsigned int i = EA_ID(a->id);
884
  struct attr_desc *d;
885

    
886
  switch (a->id) {
887
  case EA_RIP_METRIC: buf += bsprintf( buf, "metric: %d", a->u.data ); return GA_FULL;
888
  case EA_RIP_TAG:    buf += bsprintf( buf, "tag: %d", a->u.data );    return GA_FULL;
889
  default: return GA_UNKNOWN;
890
  }
891
}
892

    
893
static int
894
rip_pat_compare(struct rip_patt *a, struct rip_patt *b)
895
{
896
  return ((a->metric == b->metric) &&
897
          (a->mode == b->mode));
898
}
899

    
900
static int
901
rip_reconfigure(struct proto *p, struct proto_config *c)
902
{
903
  struct rip_proto_config *new = (struct rip_proto_config *) c;
904
  int generic = sizeof(struct proto_config) + sizeof(list) /* + sizeof(struct password_item *) */;
905

    
906
  if (!iface_patts_equal(&P_CF->iface_list, &new->iface_list, (void *) rip_pat_compare))
907
    return 0;
908
  if (!password_same(P_CF->passwords, 
909
                     new->passwords))
910
    return 0;
911
  return !memcmp(((byte *) P_CF) + generic,
912
                 ((byte *) new) + generic,
913
                 sizeof(struct rip_proto_config) - generic);
914
}
915

    
916
struct protocol proto_rip = {
917
  name: "RIP",
918
  template: "rip%d",
919
  attr_class: EAP_RIP,
920
  get_route_info: rip_get_route_info,
921
  get_attr: rip_get_attr,
922

    
923
  init: rip_init,
924
  dump: rip_dump,
925
  start: rip_start,
926
  reconfigure: rip_reconfigure,
927
};