Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (27.6 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

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

    
60
#include "rip.h"
61

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

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

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

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

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

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

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

    
120
  ipa_hton( b->network );
121

    
122
  return pos+1;
123
}
124

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

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

    
141
    if (c->done)
142
      goto done;
143

    
144
    DBG( "Preparing packet to send: " );
145

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

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

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

    
175
  break_loop:
176

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

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

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

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

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

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

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

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

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

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

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

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

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

    
269
  old = rte_find(net, p);
270
  if (!old || p->rte_better(new, old) ||
271
      (ipa_equal(old->attrs->from, new->attrs->from) &&
272
      (old->u.rip.metric != new->u.rip.metric)) )
273
    rte_update(tab, net, p, p, new);
274
  else
275
    rte_free(new);
276
}
277

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

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

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

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

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

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

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

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

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

    
385
  advertise_entry( p, block, whotoldme, iface );
386
}
387

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

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

    
400
  switch( packet->heading.version ) {
401
  case RIP_V1: DBG( "Rip1: " ); break;
402
  case RIP_V2: DBG( "Rip2: " ); break;
403
  default: BAD( "Unknown version" );
404
  }
405

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

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

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

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

    
457
  return 0;
458
}
459

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

    
471
  /* In non-listening mode, just ignore packet */
472
  if (i->mode & IM_NOLISTEN)
473
    return 1;
474

    
475
#ifdef IPV6
476
  if (! i->iface || s->lifindex != i->iface->index)
477
    return 1;
478

    
479
  iface = i->iface;
480
#endif
481

    
482
  CHK_MAGIC;
483
  DBG( "RIP: message came: %d bytes from %I via %s\n", size, s->faddr, i->iface ? i->iface->name : "(dummy)" );
484
  size -= sizeof( struct rip_packet_heading );
485
  if (size < 0) BAD( "Too small packet" );
486
  if (size % sizeof( struct rip_block )) BAD( "Odd sized packet" );
487
  num = size / sizeof( struct rip_block );
488
  if (num>PACKET_MAX) BAD( "Too many blocks" );
489

    
490
  if (ipa_equal(i->iface->addr->ip, s->faddr)) {
491
    DBG("My own packet\n");
492
    return 1;
493
  }
494

    
495
  rip_process_packet( p, (struct rip_packet *) s->rbuf, num, s->faddr, s->fport, iface );
496
  return 1;
497
}
498

    
499
/*
500
 * Interface to BIRD core
501
 */
502

    
503
static void
504
rip_dump_entry( struct rip_entry *e )
505
{
506
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d ", 
507
  e->whotoldme, e->updated-now, e->changed-now, e->n.prefix, e->n.pxlen, e->nexthop, e->metric );
508
  debug( "\n" );
509
}
510

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

    
522
static void
523
rip_timer(timer *t)
524
{
525
  struct proto *p = t->data;
526
  struct fib_node *e, *et;
527

    
528
  CHK_MAGIC;
529
  DBG( "RIP: tick tock\n" );
530
  
531
  WALK_LIST_DELSAFE( e, et, P->garbage ) {
532
    rte *rte;
533
    rte = SKIP_BACK( struct rte, u.rip.garbage, e );
534

    
535
    CHK_MAGIC;
536

    
537
    DBG( "Garbage: (%p)", rte ); rte_dump( rte );
538

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

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

    
553
  DBG( "RIP: Broadcasting routing tables\n" );
554
  {
555
    struct rip_interface *rif;
556

    
557
    if ( P_CF->period > 2 ) {                /* Bring some randomness into sending times */
558
      if (! (P->tx_count % P_CF->period)) P->rnd_count = random_u32() % 2;
559
    } else P->rnd_count = P->tx_count % P_CF->period;
560

    
561
    WALK_LIST( rif, P->interfaces ) {
562
      struct iface *iface = rif->iface;
563

    
564
      if (!iface) continue;
565
      if (rif->mode & IM_QUIET) continue;
566
      if (!(iface->flags & IF_UP)) continue;
567
      rif->triggered = P->rnd_count;
568

    
569
      rip_sendto( p, IPA_NONE, 0, rif );
570
    }
571
    P->tx_count++;
572
    P->rnd_count--;
573
  }
574

    
575
  DBG( "RIP: tick tock done\n" );
576
}
577

    
578
/*
579
 * rip_start - initialize instance of rip
580
 */
581
static int
582
rip_start(struct proto *p)
583
{
584
  struct rip_interface *rif;
585
  DBG( "RIP: starting instance...\n" );
586

    
587
  ASSERT(sizeof(struct rip_packet_heading) == 4);
588
  ASSERT(sizeof(struct rip_block) == 20);
589
  ASSERT(sizeof(struct rip_block_auth) == 20);
590

    
591
#ifdef LOCAL_DEBUG
592
  P->magic = RIP_MAGIC;
593
#endif
594
  fib_init( &P->rtable, p->pool, sizeof( struct rip_entry ), 0, NULL );
595
  init_list( &P->connections );
596
  init_list( &P->garbage );
597
  init_list( &P->interfaces );
598
  P->timer = tm_new( p->pool );
599
  P->timer->data = p;
600
  P->timer->recurrent = 1;
601
  P->timer->hook = rip_timer;
602
  tm_start( P->timer, 2 );
603
  rif = new_iface(p, NULL, 0, NULL);        /* Initialize dummy interface */
604
  add_head( &P->interfaces, NODE rif );
605
  CHK_MAGIC;
606

    
607
  rip_init_instance(p);
608

    
609
  DBG( "RIP: ...done\n");
610
  return PS_UP;
611
}
612

    
613
static struct proto *
614
rip_init(struct proto_config *cfg)
615
{
616
  struct proto *p = proto_new(cfg, sizeof(struct rip_proto));
617

    
618
  return p;
619
}
620

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

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

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

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

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

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

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

    
691
  if (rif->multicast)
692
    DBG( "Doing multicasts!\n" );
693

    
694
  rif->sock = sk_new( p->pool );
695
  rif->sock->type = SK_UDP;
696
  rif->sock->sport = P_CF->port;
697
  rif->sock->rx_hook = rip_rx;
698
  rif->sock->data = rif;
699
  rif->sock->rbsize = 10240;
700
  rif->sock->iface = new;                /* Automagically works for dummy interface */
701
  rif->sock->tbuf = mb_alloc( p->pool, sizeof( struct rip_packet ));
702
  rif->sock->tx_hook = rip_tx;
703
  rif->sock->err_hook = rip_tx_err;
704
  rif->sock->daddr = IPA_NONE;
705
  rif->sock->dport = P_CF->port;
706
  if (new)
707
    {
708
      rif->sock->ttl = 1;
709
      rif->sock->tos = IP_PREC_INTERNET_CONTROL;
710
      rif->sock->flags = SKF_LADDR_RX;
711
    }
712

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

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

    
733
    if (sk_open(rif->sock)<0)
734
      goto err;
735

    
736
    if (rif->multicast)
737
      {
738
        if (sk_setup_multicast(rif->sock) < 0)
739
          goto err;
740
        if (sk_join_group(rif->sock, rif->sock->daddr) < 0)
741
          goto err;
742
      }
743
    else
744
      {
745
        if (sk_set_broadcast(rif->sock, 1) < 0)
746
          goto err;
747
      }
748
  }
749

    
750
  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 );
751
  
752
  return rif;
753

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

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

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

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

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

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

    
824
static struct ea_list *
825
rip_gen_attrs(struct linpool *pool, int metric, u16 tag)
826
{
827
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2*sizeof(eattr));
828

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

    
843
static int
844
rip_import_control(struct proto *p, struct rte **rt, struct ea_list **attrs, struct linpool *pool)
845
{
846
  if ((*rt)->attrs->proto == p)        /* My own must not be touched */
847
    return 1;
848

    
849
  if ((*rt)->attrs->source != RTS_RIP) {
850
    struct ea_list *new = rip_gen_attrs(pool, 1, 0);
851
    new->next = *attrs;
852
    *attrs = new;
853
  }
854
  return 0;
855
}
856

    
857
static struct ea_list *
858
rip_make_tmp_attrs(struct rte *rt, struct linpool *pool)
859
{
860
  return rip_gen_attrs(pool, rt->u.rip.metric, rt->u.rip.tag);
861
}
862

    
863
static void 
864
rip_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
865
{
866
  rt->u.rip.tag = ea_get_int(attrs, EA_RIP_TAG, 0);
867
  rt->u.rip.metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
868
}
869

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

    
881
  e = fib_find( &P->rtable, &net->n.prefix, net->n.pxlen );
882
  if (e)
883
    fib_delete( &P->rtable, e );
884

    
885
  if (new) {
886
    e = fib_get( &P->rtable, &net->n.prefix, net->n.pxlen );
887

    
888
    e->nexthop = new->attrs->gw;
889
    e->metric = 0;
890
    e->whotoldme = IPA_NONE;
891
    new->u.rip.entry = e;
892

    
893
    e->tag = ea_get_int(attrs, EA_RIP_TAG, 0);
894
    e->metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
895
    if (e->metric > P_CF->infinity)
896
      e->metric = P_CF->infinity;
897

    
898
    if (new->attrs->proto == p)
899
      e->whotoldme = new->attrs->from;
900

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

    
909
static int
910
rip_rte_same(struct rte *new, struct rte *old)
911
{
912
  /* new->attrs == old->attrs always */
913
  return new->u.rip.metric == old->u.rip.metric;
914
}
915

    
916

    
917
static int
918
rip_rte_better(struct rte *new, struct rte *old)
919
{
920
  struct proto *p = new->attrs->proto;
921

    
922
  if (ipa_equal(old->attrs->from, new->attrs->from))
923
    return 1;
924

    
925
  if (old->u.rip.metric < new->u.rip.metric)
926
    return 0;
927

    
928
  if (old->u.rip.metric > new->u.rip.metric)
929
    return 1;
930

    
931
  if (old->attrs->proto == new->attrs->proto)                /* This does not make much sense for different protocols */
932
    if ((old->u.rip.metric == new->u.rip.metric) &&
933
        ((now - old->lastmod) > (P_CF->timeout_time / 2)))
934
      return 1;
935

    
936
  return 0;
937
}
938

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

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

    
967
void
968
rip_init_instance(struct proto *p)
969
{
970
  p->accept_ra_types = RA_OPTIMAL;
971
  p->if_notify = rip_if_notify;
972
  p->rt_notify = rip_rt_notify;
973
  p->import_control = rip_import_control;
974
  p->make_tmp_attrs = rip_make_tmp_attrs;
975
  p->store_tmp_attrs = rip_store_tmp_attrs;
976
  p->rte_better = rip_rte_better;
977
  p->rte_same = rip_rte_same;
978
  p->rte_insert = rip_rte_insert;
979
  p->rte_remove = rip_rte_remove;
980
}
981

    
982
void
983
rip_init_config(struct rip_proto_config *c)
984
{
985
  init_list(&c->iface_list);
986
  c->infinity        = 16;
987
  c->port        = RIP_PORT;
988
  c->period        = 30;
989
  c->garbage_time = 120+180;
990
  c->timeout_time = 120;
991
  c->passwords        = NULL;
992
  c->authtype        = AT_NONE;
993
}
994

    
995
static int
996
rip_get_attr(eattr *a, byte *buf, int buflen UNUSED)
997
{
998
  switch (a->id) {
999
  case EA_RIP_METRIC: bsprintf( buf, "metric: %d", a->u.data ); return GA_FULL;
1000
  case EA_RIP_TAG:    bsprintf( buf, "tag: %d", a->u.data );    return GA_FULL;
1001
  default: return GA_UNKNOWN;
1002
  }
1003
}
1004

    
1005
static int
1006
rip_pat_compare(struct rip_patt *a, struct rip_patt *b)
1007
{
1008
  return ((a->metric == b->metric) &&
1009
          (a->mode == b->mode));
1010
}
1011

    
1012
static int
1013
rip_reconfigure(struct proto *p, struct proto_config *c)
1014
{
1015
  struct rip_proto_config *new = (struct rip_proto_config *) c;
1016
  int generic = sizeof(struct proto_config) + sizeof(list) /* + sizeof(struct password_item *) */;
1017

    
1018
  if (!iface_patts_equal(&P_CF->iface_list, &new->iface_list, (void *) rip_pat_compare))
1019
    return 0;
1020
  return !memcmp(((byte *) P_CF) + generic,
1021
                 ((byte *) new) + generic,
1022
                 sizeof(struct rip_proto_config) - generic);
1023
}
1024

    
1025
static void
1026
rip_copy_config(struct proto_config *dest, struct proto_config *src)
1027
{
1028
  /* Shallow copy of everything */
1029
  proto_copy_rest(dest, src, sizeof(struct rip_proto_config));
1030

    
1031
  /* We clean up iface_list, ifaces are non-sharable */
1032
  init_list(&((struct rip_proto_config *) dest)->iface_list);
1033

    
1034
  /* Copy of passwords is OK, it just will be replaced in dest when used */
1035
}
1036

    
1037

    
1038
struct protocol proto_rip = {
1039
  name: "RIP",
1040
  template: "rip%d",
1041
  attr_class: EAP_RIP,
1042
  preference: DEF_PREF_RIP,
1043
  get_route_info: rip_get_route_info,
1044
  get_attr: rip_get_attr,
1045

    
1046
  init: rip_init,
1047
  dump: rip_dump,
1048
  start: rip_start,
1049
  reconfigure: rip_reconfigure,
1050
  copy_config: rip_copy_config
1051
};