Statistics
| Branch: | Revision:

iof-bird-daemon / proto / rip / rip.c @ 05476c4d

History | View | Annotate | Download (28 KB)

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

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

17
        FIXME: 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_rt_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
#define LOCAL_DEBUG 1
50

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

    
61
#include "rip.h"
62

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

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

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

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

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

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

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

    
122
  ipa_hton( b->network );
123

    
124
  return pos+1;
125
}
126

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

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

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

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

    
148
    packet->heading.command = RIPCMD_RESPONSE;
149
#ifndef IPV6
150
    packet->heading.version = RIP_V2;
151
#else
152
    packet->heading.version = RIP_NG;
153
#endif
154
    packet->heading.unused  = 0;
155

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

    
166
      if (!rif->triggered || (!(e->updated < now-2))) {                /* FIXME: Should be probably 1 or some different algorithm */
167
        nullupdate = 0;
168
        i = rip_tx_prepare( p, packet->block + i, e, rif, i );
169
        if (i >= maxi) {
170
          FIB_ITERATE_PUT(&c->iter, z);
171
          goto break_loop;
172
        }
173
      }
174
    } FIB_ITERATE_END(z);
175
    c->done = 1;
176

    
177
  break_loop:
178

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

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

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

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

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

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

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

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

    
246
  rip_tx(c->rif->sock);
247
}
248

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

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

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

    
266
static int rip_rte_better(struct rte *new, struct rte *old);
267

    
268
static void
269
rip_rte_update_if_better(rtable *tab, net *net, struct proto *p, rte *new)
270
{
271
  rte *old;
272

    
273
  old = rte_find(net, p->main_source);
274
  if (!old || rip_rte_better(new, old) ||
275
      (ipa_equal(old->attrs->from, new->attrs->from) &&
276
      (old->u.rip.metric != new->u.rip.metric)) )
277
    rte_update(p, net, new);
278
  else
279
    rte_free(new);
280
}
281

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

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

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

    
318
  neighbor = neigh_find2( p, &A.gw, iface, 0 );
319
  if (!neighbor) {
320
    log( L_REMOTE "%s: %I asked me to route %I/%d using not-neighbor %I.", p->name, A.from, b->network, pxlen, A.gw );
321
    return;
322
  }
323
  if (neighbor->scope == SCOPE_HOST) {
324
    DBG("Self-destined route, ignoring.\n");
325
    return;
326
  }
327

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

    
349
  r->u.rip.entry = NULL;
350
  if (r->u.rip.metric > P_CF->infinity) r->u.rip.metric = P_CF->infinity;
351
  r->u.rip.tag = ntohl(b->tag);
352
  r->net = n;
353
  r->pflags = 0; /* Here go my flags */
354
  rip_rte_update_if_better( p->table, n, p, r );
355
  DBG( "done\n" );
356
}
357

    
358
/*
359
 * process_block - do some basic check and pass block to advertise_entry
360
 */
361
static void
362
process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme, struct iface *iface )
363
{
364
  int metric, pxlen;
365

    
366
#ifndef IPV6
367
  metric = ntohl( block->metric );
368
  pxlen = ipa_mklen(block->netmask);
369
#else
370
  metric = block->metric;
371
  pxlen = block->pxlen;
372
#endif
373
  ip_addr network = block->network;
374

    
375
  CHK_MAGIC;
376

    
377
  TRACE(D_ROUTES, "block: %I tells me: %I/%d available, metric %d... ",
378
      whotoldme, network, pxlen, metric );
379

    
380
  if ((!metric) || (metric > P_CF->infinity)) {
381
#ifdef IPV6 /* Someone is sending us nexthop and we are ignoring it */
382
    if (metric == 0xff)
383
      { DBG( "IPv6 nexthop ignored" ); return; }
384
#endif
385
    log( L_WARN "%s: Got metric %d from %I", p->name, metric, whotoldme );
386
    return;
387
  }
388

    
389
  advertise_entry( p, block, whotoldme, iface );
390
}
391

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

    
394
/*
395
 * rip_process_packet - this is main routine for incoming packets.
396
 */
397
static int
398
rip_process_packet( struct proto *p, struct rip_packet *packet, int num, ip_addr whotoldme, int port, struct iface *iface )
399
{
400
  int i;
401
  int authenticated = 0;
402
  neighbor *neighbor;
403

    
404
  switch( packet->heading.version ) {
405
  case RIP_V1: DBG( "Rip1: " ); break;
406
  case RIP_V2: DBG( "Rip2: " ); break;
407
  default: BAD( "Unknown version" );
408
  }
409

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

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

    
425
          if (!(neighbor = neigh_find2( p, &whotoldme, iface, 0 )) || neighbor->scope == SCOPE_HOST) {
426
            log( L_REMOTE "%s: %I send me routing info but he is not my neighbor", p->name, whotoldme );
427
            return 0;
428
          }
429

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

    
461
  return 0;
462
}
463

    
464
/*
465
 * rip_rx - Receive hook: do basic checks and pass packet to rip_process_packet
466
 */
467
static int
468
rip_rx(sock *s, int size)
469
{
470
  struct rip_interface *i = s->data;
471
  struct proto *p = i->proto;
472
  struct iface *iface = NULL;
473
  int num;
474

    
475
  /* In non-listening mode, just ignore packet */
476
  if (i->mode & IM_NOLISTEN)
477
    return 1;
478

    
479
#ifdef IPV6
480
  if (! i->iface || s->lifindex != i->iface->index)
481
    return 1;
482

    
483
  iface = i->iface;
484
#endif
485

    
486
  if (i->check_ttl && (s->rcv_ttl < 255))
487
  {
488
    log( L_REMOTE "%s: Discarding packet with TTL %d (< 255) from %I on %s",
489
         p->name, s->rcv_ttl, s->faddr, i->iface->name);
490
    return 1;
491
  }
492

    
493

    
494
  CHK_MAGIC;
495
  DBG( "RIP: message came: %d bytes from %I via %s\n", size, s->faddr, i->iface ? i->iface->name : "(dummy)" );
496
  size -= sizeof( struct rip_packet_heading );
497
  if (size < 0) BAD( "Too small packet" );
498
  if (size % sizeof( struct rip_block )) BAD( "Odd sized packet" );
499
  num = size / sizeof( struct rip_block );
500
  if (num>PACKET_MAX) BAD( "Too many blocks" );
501

    
502
  if (ipa_equal(i->iface->addr->ip, s->faddr)) {
503
    DBG("My own packet\n");
504
    return 1;
505
  }
506

    
507
  rip_process_packet( p, (struct rip_packet *) s->rbuf, num, s->faddr, s->fport, iface );
508
  return 1;
509
}
510

    
511
/*
512
 * Interface to BIRD core
513
 */
514

    
515
static void
516
rip_dump_entry( struct rip_entry *e )
517
{
518
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d ", 
519
  e->whotoldme, e->updated-now, e->changed-now, e->n.prefix, e->n.pxlen, e->nexthop, e->metric );
520
  debug( "\n" );
521
}
522

    
523
/**
524
 * rip_timer
525
 * @t: timer
526
 *
527
 * Broadcast routing tables periodically (using rip_tx) and kill
528
 * routes that are too old. RIP keeps a list of its own entries present
529
 * in the core table by a linked list (functions rip_rte_insert() and
530
 * rip_rte_delete() are responsible for that), it walks this list in the timer
531
 * and in case an entry is too old, it is discarded.
532
 */
533

    
534
static void
535
rip_timer(timer *t)
536
{
537
  struct proto *p = t->data;
538
  struct fib_node *e, *et;
539

    
540
  CHK_MAGIC;
541
  DBG( "RIP: tick tock\n" );
542
  
543
  WALK_LIST_DELSAFE( e, et, P->garbage ) {
544
    rte *rte;
545
    rte = SKIP_BACK( struct rte, u.rip.garbage, e );
546

    
547
    CHK_MAGIC;
548

    
549
    DBG( "Garbage: (%p)", rte ); rte_dump( rte );
550

    
551
    if (now - rte->lastmod > P_CF->timeout_time) {
552
      TRACE(D_EVENTS, "entry is too old: %I", rte->net->n.prefix );
553
      if (rte->u.rip.entry) {
554
        rte->u.rip.entry->metric = P_CF->infinity;
555
        rte->u.rip.metric = P_CF->infinity;
556
      }
557
    }
558

    
559
    if (now - rte->lastmod > P_CF->garbage_time) {
560
      TRACE(D_EVENTS, "entry is much too old: %I", rte->net->n.prefix );
561
      rte_discard(p->table, rte);
562
    }
563
  }
564

    
565
  DBG( "RIP: Broadcasting routing tables\n" );
566
  {
567
    struct rip_interface *rif;
568

    
569
    if ( P_CF->period > 2 ) {                /* Bring some randomness into sending times */
570
      if (! (P->tx_count % P_CF->period)) P->rnd_count = random_u32() % 2;
571
    } else P->rnd_count = P->tx_count % P_CF->period;
572

    
573
    WALK_LIST( rif, P->interfaces ) {
574
      struct iface *iface = rif->iface;
575

    
576
      if (!iface) continue;
577
      if (rif->mode & IM_QUIET) continue;
578
      if (!(iface->flags & IF_UP)) continue;
579
      rif->triggered = P->rnd_count;
580

    
581
      rip_sendto( p, IPA_NONE, 0, rif );
582
    }
583
    P->tx_count++;
584
    P->rnd_count--;
585
  }
586

    
587
  DBG( "RIP: tick tock done\n" );
588
}
589

    
590
/*
591
 * rip_start - initialize instance of rip
592
 */
593
static int
594
rip_start(struct proto *p)
595
{
596
  struct rip_interface *rif;
597
  DBG( "RIP: starting instance...\n" );
598

    
599
  ASSERT(sizeof(struct rip_packet_heading) == 4);
600
  ASSERT(sizeof(struct rip_block) == 20);
601
  ASSERT(sizeof(struct rip_block_auth) == 20);
602

    
603
#ifdef LOCAL_DEBUG
604
  P->magic = RIP_MAGIC;
605
#endif
606
  fib_init( &P->rtable, p->pool, sizeof( struct rip_entry ), 0, NULL );
607
  init_list( &P->connections );
608
  init_list( &P->garbage );
609
  init_list( &P->interfaces );
610
  P->timer = tm_new( p->pool );
611
  P->timer->data = p;
612
  P->timer->recurrent = 1;
613
  P->timer->hook = rip_timer;
614
  tm_start( P->timer, 2 );
615
  rif = new_iface(p, NULL, 0, NULL);        /* Initialize dummy interface */
616
  add_head( &P->interfaces, NODE rif );
617
  CHK_MAGIC;
618

    
619
  DBG( "RIP: ...done\n");
620
  return PS_UP;
621
}
622

    
623
static void
624
rip_dump(struct proto *p)
625
{
626
  int i;
627
  node *w;
628
  struct rip_interface *rif;
629

    
630
  CHK_MAGIC;
631
  WALK_LIST( w, P->connections ) {
632
    struct rip_connection *n = (void *) w;
633
    debug( "RIP: connection #%d: %I\n", n->num, n->addr );
634
  }
635
  i = 0;
636
  FIB_WALK( &P->rtable, e ) {
637
    debug( "RIP: entry #%d: ", i++ );
638
    rip_dump_entry( (struct rip_entry *)e );
639
  } FIB_WALK_END;
640
  i = 0;
641
  WALK_LIST( rif, P->interfaces ) {
642
    debug( "RIP: interface #%d: %s, %I, busy = %x\n", i++, rif->iface?rif->iface->name:"(dummy)", rif->sock->daddr, rif->busy );
643
  }
644
}
645

    
646
static void
647
rip_get_route_info(rte *rte, byte *buf, ea_list *attrs)
648
{
649
  eattr *metric = ea_find(attrs, EA_RIP_METRIC);
650
  eattr *tag = ea_find(attrs, EA_RIP_TAG);
651

    
652
  buf += bsprintf(buf, " (%d/%d)", rte->pref, metric ? metric->u.data : 0);
653
  if (tag && tag->u.data)
654
    bsprintf(buf, " t%04x", tag->u.data);
655
}
656

    
657
static void
658
kill_iface(struct rip_interface *i)
659
{
660
  DBG( "RIP: Interface %s disappeared\n", i->iface->name);
661
  rfree(i->sock);
662
  mb_free(i);
663
}
664

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

    
682
  rif = mb_allocz(p->pool, sizeof( struct rip_interface ));
683
  rif->iface = new;
684
  rif->proto = p;
685
  rif->busy = NULL;
686
  if (PATT) {
687
    rif->mode = PATT->mode;
688
    rif->metric = PATT->metric;
689
    rif->multicast = (!(PATT->mode & IM_BROADCAST)) && (flags & IF_MULTICAST);
690
    rif->check_ttl = (PATT->ttl_security == 1);
691
  }
692
  /* lookup multicasts over unnumbered links - no: rip is not defined over unnumbered links */
693

    
694
  if (rif->multicast)
695
    DBG( "Doing multicasts!\n" );
696

    
697
  rif->sock = sk_new( p->pool );
698
  rif->sock->type = SK_UDP;
699
  rif->sock->sport = P_CF->port;
700
  rif->sock->rx_hook = rip_rx;
701
  rif->sock->data = rif;
702
  rif->sock->rbsize = 10240;
703
  rif->sock->iface = new;                /* Automagically works for dummy interface */
704
  rif->sock->tbuf = mb_alloc( p->pool, sizeof( struct rip_packet ));
705
  rif->sock->tx_hook = rip_tx;
706
  rif->sock->err_hook = rip_tx_err;
707
  rif->sock->daddr = IPA_NONE;
708
  rif->sock->dport = P_CF->port;
709
  if (new)
710
    {
711
      rif->sock->tos = PATT->tx_tos;
712
      rif->sock->priority = PATT->tx_priority;
713
      rif->sock->ttl = PATT->ttl_security ? 255 : 1;
714
      rif->sock->flags = SKF_LADDR_RX | (rif->check_ttl ? SKF_TTL_RX : 0);
715
    }
716

    
717
  if (new) {
718
    if (new->addr->flags & IA_PEER)
719
      log( L_WARN "%s: rip is not defined over unnumbered links", p->name );
720
    if (rif->multicast) {
721
#ifndef IPV6
722
      rif->sock->daddr = ipa_from_u32(0xe0000009);
723
#else
724
      rif->sock->daddr = ipa_build(0xff020000, 0, 0, 9);
725
#endif
726
    } else {
727
      rif->sock->daddr = new->addr->brd;
728
    }
729
  }
730

    
731
  if (!ipa_nonzero(rif->sock->daddr)) {
732
    if (rif->iface)
733
      log( L_WARN "%s: interface %s is too strange for me", p->name, rif->iface->name );
734
  } else {
735

    
736
    if (sk_open(rif->sock) < 0)
737
      goto err;
738

    
739
    if (rif->multicast)
740
      {
741
        if (sk_setup_multicast(rif->sock) < 0)
742
          goto err;
743
        if (sk_join_group(rif->sock, rif->sock->daddr) < 0)
744
          goto err;
745
      }
746
    else
747
      {
748
        if (sk_setup_broadcast(rif->sock) < 0)
749
          goto err;
750
      }
751
  }
752

    
753
  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 );
754
  
755
  return rif;
756

    
757
 err:
758
  sk_log_error(rif->sock, p->name);
759
  log(L_ERR "%s: Cannot open socket for %s", p->name, rif->iface ? rif->iface->name : "(dummy)" );
760
  if (rif->iface) {
761
    rfree(rif->sock);
762
    mb_free(rif);
763
    return NULL;
764
  }
765
  /* On dummy, we just return non-working socket, so that user gets error every time anyone requests table */
766
  return rif;
767
}
768

    
769
static void
770
rip_real_if_add(struct object_lock *lock)
771
{
772
  struct iface *iface = lock->iface;
773
  struct proto *p = lock->data;
774
  struct rip_interface *rif;
775
  struct iface_patt *k = iface_patt_find(&P_CF->iface_list, iface, iface->addr);
776

    
777
  if (!k)
778
    bug("This can not happen! It existed few seconds ago!" );
779
  DBG("adding interface %s\n", iface->name );
780
  rif = new_iface(p, iface, iface->flags, k);
781
  if (rif) {
782
    add_head( &P->interfaces, NODE rif );
783
    DBG("Adding object lock of %p for %p\n", lock, rif);
784
    rif->lock = lock;
785
  } else { rfree(lock); }
786
}
787

    
788
static void
789
rip_if_notify(struct proto *p, unsigned c, struct iface *iface)
790
{
791
  DBG( "RIP: if notify\n" );
792
  if (iface->flags & IF_IGNORE)
793
    return;
794
  if (c & IF_CHANGE_DOWN) {
795
    struct rip_interface *i;
796
    i = find_interface(p, iface);
797
    if (i) {
798
      rem_node(NODE i);
799
      rfree(i->lock);
800
      kill_iface(i);
801
    }
802
  }
803
  if (c & IF_CHANGE_UP) {
804
    struct iface_patt *k = iface_patt_find(&P_CF->iface_list, iface, iface->addr);
805
    struct object_lock *lock;
806
    struct rip_patt *PATT = (struct rip_patt *) k;
807

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

    
810
    lock = olock_new( p->pool );
811
    if (!(PATT->mode & IM_BROADCAST) && (iface->flags & IF_MULTICAST))
812
#ifndef IPV6
813
      lock->addr = ipa_from_u32(0xe0000009);
814
#else
815
      ip_pton("FF02::9", &lock->addr);
816
#endif
817
    else
818
      lock->addr = iface->addr->brd;
819
    lock->port = P_CF->port;
820
    lock->iface = iface;
821
    lock->hook = rip_real_if_add;
822
    lock->data = p;
823
    lock->type = OBJLOCK_UDP;
824
    olock_acquire(lock);
825
  }
826
}
827

    
828
static struct ea_list *
829
rip_gen_attrs(struct linpool *pool, int metric, u16 tag)
830
{
831
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2*sizeof(eattr));
832

    
833
  l->next = NULL;
834
  l->flags = EALF_SORTED;
835
  l->count = 2;
836
  l->attrs[0].id = EA_RIP_TAG;
837
  l->attrs[0].flags = 0;
838
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
839
  l->attrs[0].u.data = tag;
840
  l->attrs[1].id = EA_RIP_METRIC;
841
  l->attrs[1].flags = 0;
842
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
843
  l->attrs[1].u.data = metric;
844
  return l;
845
}
846

    
847
static int
848
rip_import_control(struct proto *p, struct rte **rt, struct ea_list **attrs, struct linpool *pool)
849
{
850
  if ((*rt)->attrs->src->proto == p)        /* My own must not be touched */
851
    return 1;
852

    
853
  if ((*rt)->attrs->source != RTS_RIP) {
854
    struct ea_list *new = rip_gen_attrs(pool, 1, 0);
855
    new->next = *attrs;
856
    *attrs = new;
857
  }
858
  return 0;
859
}
860

    
861
static struct ea_list *
862
rip_make_tmp_attrs(struct rte *rt, struct linpool *pool)
863
{
864
  return rip_gen_attrs(pool, rt->u.rip.metric, rt->u.rip.tag);
865
}
866

    
867
static void 
868
rip_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
869
{
870
  rt->u.rip.tag = ea_get_int(attrs, EA_RIP_TAG, 0);
871
  rt->u.rip.metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
872
}
873

    
874
/*
875
 * rip_rt_notify - core tells us about new route (possibly our
876
 * own), so store it into our data structures. 
877
 */
878
static void
879
rip_rt_notify(struct proto *p, struct rtable *table UNUSED, struct network *net,
880
              struct rte *new, struct rte *old UNUSED, struct ea_list *attrs)
881
{
882
  CHK_MAGIC;
883
  struct rip_entry *e;
884

    
885
  e = fib_find( &P->rtable, &net->n.prefix, net->n.pxlen );
886
  if (e)
887
    fib_delete( &P->rtable, e );
888

    
889
  if (new) {
890
    e = fib_get( &P->rtable, &net->n.prefix, net->n.pxlen );
891

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

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

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

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

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

    
920

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

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

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

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

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

    
940
  return 0;
941
}
942

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

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

    
971
static struct proto *
972
rip_init(struct proto_config *cfg)
973
{
974
  struct proto *p = proto_new(cfg, sizeof(struct rip_proto));
975

    
976
  p->accept_ra_types = RA_OPTIMAL;
977
  p->if_notify = rip_if_notify;
978
  p->rt_notify = rip_rt_notify;
979
  p->import_control = rip_import_control;
980
  p->make_tmp_attrs = rip_make_tmp_attrs;
981
  p->store_tmp_attrs = rip_store_tmp_attrs;
982
  p->rte_better = rip_rte_better;
983
  p->rte_same = rip_rte_same;
984
  p->rte_insert = rip_rte_insert;
985
  p->rte_remove = rip_rte_remove;
986

    
987
  return p;
988
}
989

    
990
void
991
rip_init_config(struct rip_proto_config *c)
992
{
993
  init_list(&c->iface_list);
994
  c->infinity        = 16;
995
  c->port        = RIP_PORT;
996
  c->period        = 30;
997
  c->garbage_time = 120+180;
998
  c->timeout_time = 120;
999
  c->passwords        = NULL;
1000
  c->authtype        = AT_NONE;
1001
}
1002

    
1003
static int
1004
rip_get_attr(eattr *a, byte *buf, int buflen UNUSED)
1005
{
1006
  switch (a->id) {
1007
  case EA_RIP_METRIC: bsprintf( buf, "metric: %d", a->u.data ); return GA_FULL;
1008
  case EA_RIP_TAG:    bsprintf( buf, "tag: %d", a->u.data );    return GA_FULL;
1009
  default: return GA_UNKNOWN;
1010
  }
1011
}
1012

    
1013
static int
1014
rip_pat_compare(struct rip_patt *a, struct rip_patt *b)
1015
{
1016
  return ((a->metric == b->metric) &&
1017
          (a->mode == b->mode) &&
1018
          (a->tx_tos == b->tx_tos) &&
1019
          (a->tx_priority == b->tx_priority));
1020
}
1021

    
1022
static int
1023
rip_reconfigure(struct proto *p, struct proto_config *c)
1024
{
1025
  struct rip_proto_config *new = (struct rip_proto_config *) c;
1026
  int generic = sizeof(struct proto_config) + sizeof(list) /* + sizeof(struct password_item *) */;
1027

    
1028
  if (!iface_patts_equal(&P_CF->iface_list, &new->iface_list, (void *) rip_pat_compare))
1029
    return 0;
1030
  return !memcmp(((byte *) P_CF) + generic,
1031
                 ((byte *) new) + generic,
1032
                 sizeof(struct rip_proto_config) - generic);
1033
}
1034

    
1035
static void
1036
rip_copy_config(struct proto_config *dest, struct proto_config *src)
1037
{
1038
  /* Shallow copy of everything */
1039
  proto_copy_rest(dest, src, sizeof(struct rip_proto_config));
1040

    
1041
  /* We clean up iface_list, ifaces are non-sharable */
1042
  init_list(&((struct rip_proto_config *) dest)->iface_list);
1043

    
1044
  /* Copy of passwords is OK, it just will be replaced in dest when used */
1045
}
1046

    
1047

    
1048
struct protocol proto_rip = {
1049
  name: "RIP",
1050
  template: "rip%d",
1051
  attr_class: EAP_RIP,
1052
  preference: DEF_PREF_RIP,
1053
  get_route_info: rip_get_route_info,
1054
  get_attr: rip_get_attr,
1055

    
1056
  init: rip_init,
1057
  dump: rip_dump,
1058
  start: rip_start,
1059
  reconfigure: rip_reconfigure,
1060
  copy_config: rip_copy_config
1061
};