Statistics
| Branch: | Revision:

iof-bird-daemon / proto / rip / rip.c @ 1d941de4

History | View | Annotate | Download (12.6 KB)

1
/*
2
 *        Rest in pieces - RIP protocol
3
 *
4
 *        Copyright (c) 1998 Pavel Machek <pavel@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#define LOCAL_DEBUG
10

    
11
#include <string.h>
12
#include <stdlib.h>
13

    
14
#include "nest/bird.h"
15
#include "nest/iface.h"
16
#include "nest/protocol.h"
17
#include "nest/route.h"
18
#include "lib/socket.h"
19
#include "lib/resource.h"
20
#include "lib/lists.h"
21
#include "lib/timer.h"
22

    
23
#include "rip.h"
24

    
25
int infinity = 16;
26

    
27
/* FIXME: should be 520 */
28
#define RIP_PORT 1520
29

    
30
/* FIXME: should be 30 */
31
#define RIP_TIME 5
32

    
33
static void
34
rip_reply(struct proto *p)
35
{
36
#if 0
37
  P->listen->tbuf = "ACK!";
38
  sk_send_to( P->listen, 5, P->listen->faddr, P->listen->fport );
39
#endif
40
}
41

    
42
/*
43
 * Entries
44
 */
45

    
46
static struct rip_entry *
47
find_entry( struct proto *p, ip_addr network, int pxlen )
48
{
49
  struct node *e;
50

    
51
  CHK_MAGIC;
52
  WALK_LIST( e, P->rtable ) {
53
    if (ipa_equal( network, E->network ) &&
54
        (pxlen == E->pxlen)) {
55
      return E;
56
    }
57
  }
58
  return NULL;
59
}
60

    
61
/* Let main routing table know about our new entry */
62
static void
63
advertise_entry( struct proto *p, struct rip_entry *e )
64
{
65
  rta *a, A;
66
  rte *r;
67
  net *n;
68
    
69
  bzero(&A, sizeof(A));
70
  A.proto = p;
71
  A.source = RTS_RIP;
72
  A.scope = SCOPE_UNIVERSE;
73
  A.cast = RTC_UNICAST;
74
  A.dest = RTD_ROUTER;
75
  A.tos = 0;
76
  A.flags = 0;
77
  A.gw = e->nexthop;
78
  A.from = e->whotoldme;
79
  A.iface = /* FIXME: need to set somehow */ NULL;
80
  /* set to: interface of nexthop */
81
  a = rta_lookup(&A);
82
  n = net_get( &master_table, 0, e->network, e->pxlen );
83
  r = rte_get_temp(a);
84
  r->pflags = 0; /* Here go my flags */
85
  rte_update( n, p, r );
86
}
87

    
88
static struct rip_entry *
89
new_entry( struct proto *p )
90
{
91
  struct rip_entry *e;
92
  e = mb_alloc(p->pool, sizeof( struct rip_entry ));
93
  bzero( e, sizeof( struct rip_entry ));
94
  return e;
95
}
96

    
97
/* Create new entry from data rip_block */
98
static struct rip_entry *
99
new_entry_from_block( struct proto *p, struct rip_block *b, ip_addr whotoldme )
100
{
101
  struct rip_entry *e = new_entry( p );
102

    
103
  e->whotoldme = whotoldme;
104
  e->network = b->network;
105
  e->pxlen = ipa_mklen( b->netmask );
106
  /* FIXME: verify that it is really netmask */
107
  if (ipa_nonzero(b->nexthop))
108
    e->nexthop = b->nexthop;
109
  else
110
    e->nexthop = whotoldme;
111
  e->tag = ntohs( b->tag );
112
  e->metric = ntohl( b->metric );
113
  e->updated = e->changed = now;
114
  return e;
115
}
116

    
117
/* Delete one of entries */
118
static void
119
kill_entry_ourrt( struct proto *p, struct rip_entry *e )
120
{
121
  struct rip_connection *c;
122
  net *n;
123

    
124
  rem_node( NODE e );
125
  WALK_LIST( c, P->connections ) {
126
    if (c->sendptr == e) {
127
      debug( "Deleting from under someone's sendptr...\n" );
128
      c->sendptr = (void *) (NODE c->sendptr)->next;
129
    }
130
  }
131
  mb_free( e );
132
}
133

    
134
/* Delete one of entries */
135
static void
136
kill_entry_mainrt( struct proto *p, struct rip_entry *e )
137
{
138
  struct rip_connection *c;
139
  net *n;
140

    
141
  n = net_find(&master_table, 0, e->network, e->pxlen );
142
  if (!n) log( L_ERR "Could not find entry to delete in main routing table.\n" );
143
     else rte_update( n, p, NULL );
144
}
145

    
146
static void
147
kill_entry( struct proto *p, struct rip_entry *e )
148
{
149
  kill_entry_mainrt( p, e );
150
  kill_entry_ourrt( p, e );
151
}
152

    
153
/*
154
 * Output processing
155
 */
156

    
157
static void
158
rip_tx_err( sock *s, int err )
159
{
160
  struct rip_connection *c = s->data;
161
  struct proto *p = c->proto;
162
  log( L_ERR "Unexpected error at rip transmit\n" );
163
}
164

    
165
static void
166
rip_tx( sock *s )
167
{
168
  struct rip_connection *c = s->data;
169
  struct proto *p = c->proto;
170
  struct rip_packet *packet = (void *) s->tbuf;
171
  int i, done = 0;
172

    
173
givemore:
174

    
175
  debug( "Preparing packet to send: " );
176

    
177
  if (!(NODE c->sendptr)->next) {
178
    debug( "Looks like I'm done\n" );
179
    return;
180
  }
181

    
182
  packet->heading.command = RIPCMD_RESPONSE;
183
  packet->heading.version = RIP_V2;
184
  packet->heading.unused  = 0;
185

    
186
  for (i = 0; i < 25; i++) {
187
    if (!(NODE c->sendptr)->next)
188
      break;
189
    debug( "." );
190
    packet->block[i].family  = htons( 2 ); /* AF_INET */
191
    packet->block[i].tag     = htons( 0 ); /* FIXME: What should I set it to? */
192
    packet->block[i].network = c->sendptr->network;
193
    packet->block[i].netmask = ipa_mkmask( c->sendptr->pxlen );
194
    packet->block[i].nexthop = IPA_NONE; /* FIXME: How should I set it? */
195
    packet->block[i].metric  = htonl( c->sendptr->metric );
196
    if (ipa_equal(c->sendptr->whotoldme, s->daddr)) {
197
      debug( "(split horizont)" );
198
      /* FIXME: should we do it in all cases? */
199
      packet->block[i].metric = infinity;
200
    }
201
    ipa_hton( packet->block[i].network );
202
    ipa_hton( packet->block[i].netmask );
203
    ipa_hton( packet->block[i].nexthop );
204

    
205
    c->sendptr = (void *) (NODE c->sendptr)->next;
206
  }
207

    
208
  debug( ", sending %d blocks, ", i );
209

    
210
  i = sk_send( s, sizeof( struct rip_packet_heading ) + i*sizeof( struct rip_block ) );
211
  if (i<0) rip_tx_err( s, i );
212
  if (i>0) {
213
    debug( "it wants more\n" );
214
    goto givemore;
215
  }
216
  
217
  debug( "blocked\n" );
218
}
219

    
220
static void
221
rip_sendto( struct proto *p, ip_addr daddr, int dport )
222
{
223
  struct rip_connection *c = mb_alloc( p->pool, sizeof( struct rip_connection ));
224
  static int num = 0;
225

    
226
  c->addr = daddr;
227
  c->proto = p;
228
  c->num = num++;
229

    
230
  c->send = sk_new( p->pool );
231
  c->send->type = SK_UDP;
232
  c->send->sport = RIP_PORT;
233
  c->send->dport = dport;
234
  c->send->daddr = daddr;
235
  c->send->rx_hook = NULL;
236
  c->send->tx_hook = rip_tx;
237
  c->send->err_hook = rip_tx_err;
238
  c->send->data = c;
239
  c->send->tbuf = mb_alloc( p->pool, sizeof( struct rip_packet ));
240
  if (sk_open(c->send)<0) {
241
    log( L_ERR "Could not open socket for data send to %I:%d\n", daddr, dport );
242
    return;
243
  }
244

    
245
  c->sendptr = HEAD( P->rtable );  
246
  add_head( &P->connections, NODE c );
247
  debug( "Sending my routing table to %I:%d\n", daddr, dport );
248

    
249
  rip_tx( c->send );
250
}
251

    
252
/*
253
 * Input processing
254
 */
255

    
256
static int
257
process_authentication( struct proto *p, struct rip_block *block )
258
{
259
  /* FIXME: Should do md5 authentication */
260
  return 0;
261
}
262

    
263
#define DELETE( msg ) { debug( msg ); kill_entry( p, e ); e = NULL; }
264

    
265
static void
266
process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme )
267
{
268
  struct rip_entry *e;
269
  int metric = ntohl( block->metric );
270
  ip_addr network = block->network;
271

    
272
  CHK_MAGIC;
273
  if ((!metric) || (metric > infinity)) {
274
    log( L_WARN "Got metric %d from %I\n", metric, whotoldme );
275
    return;
276
  }
277

    
278
  /* FIXME: Check if destination looks valid - ie not net 0 or 127 */
279

    
280
  /* FIXME: Should add configurable ammount */
281
  if (metric < infinity)
282
    metric++;
283

    
284
  debug( "block: %I tells me: %I/%I available, metric %d... ", whotoldme, network, block->netmask, metric );
285

    
286
  e = find_entry( p, network, ipa_mklen( block->netmask ));
287
  if (e && (e->metric > metric)) /* || if same metrics and this is much newer */ 
288
    DELETE( "better metrics... " );
289

    
290
  if (e && ((e->updated - now) > 180))
291
    DELETE( "more than 180 seconds old... " );
292

    
293
  if (e && ipa_equal( whotoldme, e->whotoldme ) &&
294
      (e->metric == metric)) {
295
    debug( "old news, updating time" );
296
    e->updated = now;
297
  }
298

    
299
  if (!e) {
300
    debug( "this is new" );
301
    e = new_entry_from_block( p, block, whotoldme );
302
    if (!e) {
303
      debug( "Something went wrong with new_entry?\n" );
304
      return;
305
    }
306
    advertise_entry( p, e );
307
    add_head( &P->rtable, NODE e );
308
  }
309

    
310
  debug( "\n" );
311
}
312
#undef DELETE
313

    
314
#define BAD( x ) { log( L_WARN "RIP/%s: " x "\n", p->name ); return 1; }
315

    
316
static int
317
rip_process_packet( struct proto *p, struct rip_packet *packet, int num, ip_addr whotoldme, int port )
318
{
319
  int i;
320
  int native_class = 0;
321

    
322
  switch( packet->heading.version ) {
323
  case RIP_V1: debug( "Rip1: " ); break;
324
  case RIP_V2: debug( "Rip2: " ); break;
325
  default: BAD( "Unknown version" );
326
  }
327

    
328
  switch( packet->heading.command ) {
329
  case RIPCMD_REQUEST: debug( "Asked to send my routing table\n" ); 
330
              rip_sendto( p, whotoldme, port );
331
          break;
332
  case RIPCMD_RESPONSE: debug( "Part of routing table came\n" ); 
333
          if (port != RIP_PORT) {
334
            log( L_AUTH "%I send me routing info from port %d\n", whotoldme, port );
335
            return 0;
336
          }
337

    
338
          if (!neigh_find( p, &whotoldme, 0 )) {
339
            log( L_AUTH "%I send me routing info but he is not my neighbour\n", whotoldme );
340
            return 0;
341
          }
342

    
343
          /* FIXME: Should check if it is not my own packet */
344

    
345
          for (i=0; i<num; i++) {
346
            struct rip_block *block = &packet->block[i];
347
            if (block->family == 0xffff)
348
              if (!i) {
349
                if (process_authentication(p, block))
350
                  BAD( "Authentication failed\n" );
351
              } else BAD( "Authentication is not the first!" );
352
            ipa_ntoh( block->network );
353
            ipa_ntoh( block->netmask );
354
            ipa_ntoh( block->nexthop );
355
            if (packet->heading.version == RIP_V1) {
356
              block->netmask = block->network; /* MJ: why are macros like this?! */
357
              ipa_class_mask( block->netmask );
358
            }
359
            process_block( p, block, whotoldme );
360
          }
361
          break;
362
  case RIPCMD_TRACEON:
363
  case RIPCMD_TRACEOFF: BAD( "I was asked for traceon/traceoff\n" );
364
  case 5: BAD( "Some Sun extension around here\n" );
365
  default: BAD( "Unknown command" );
366
  }
367

    
368
  rip_reply(p);
369
  return 0;
370
}
371

    
372
static int
373
rip_rx(sock *s, int size)
374
{
375
  struct proto *p = s->data;
376
  int num;
377

    
378
  CHK_MAGIC;
379
  debug( "RIP: message came: %d bytes\n", size );
380
  size -= sizeof( struct rip_packet_heading );
381
  if (size < 0) BAD( "Too small packet" );
382
  if (size % sizeof( struct rip_block )) BAD( "Odd sized packet" );
383
  num = size / sizeof( struct rip_block );
384
  if (num>25) BAD( "Too many blocks" );
385

    
386
  rip_process_packet( p, (struct rip_packet *) s->rbuf, num, s->faddr, s->fport );
387
  return 1;
388
}
389

    
390
/*
391
 * Interface to rest of bird
392
 */
393

    
394
static void
395
rip_dump_entry( struct rip_entry *e )
396
{
397
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d ", 
398
  e->whotoldme, e->updated-now, e->changed-now, e->network, e->pxlen, e->nexthop, e->metric );
399
  if (e->flags & RIP_F_EXTERNAL) debug( "[external]" );
400
  debug( "\n" );
401
}
402

    
403
static void
404
rip_timer(timer *t)
405
{
406
  struct proto *p = t->data;
407
  struct rip_entry *e, *et;
408

    
409
  CHK_MAGIC;
410
  debug( "RIP: tick tock\n" );
411

    
412
  WALK_LIST_DELSAFE( e, et, P->rtable ) {
413
    if (!(E->flags & RIP_F_EXTERNAL))
414
      if ((now - E->updated) > (180+120)) {
415
        debug( "RIP: entry is too old: " );
416
        rip_dump_entry( E );
417
        kill_entry( p, E );
418
      }
419
  }
420

    
421
#if 0
422
  debug( "RIP: Broadcasting routing tables\n" );
423
  rip_sendto( p, _MI( 0x0a000001 ), RIP_PORT );
424
#endif
425

    
426
  /* FIXME: Should broadcast routing tables to all known interfaces every 30 seconds */
427

    
428
  debug( "RIP: tick tock done\n" );
429
}
430

    
431
static void
432
rip_start(struct proto *p)
433
{
434
  debug( "RIP: initializing instance...\n" );
435

    
436
  P->listen = sk_new( p->pool );
437
  P->listen->type = SK_UDP;
438
  P->listen->sport = RIP_PORT;
439
  P->listen->rx_hook = rip_rx;
440
  P->listen->data = p;
441
  P->listen->rbsize = 10240;
442
  P->magic = RIP_MAGIC;
443
  if (sk_open(P->listen)<0)
444
    die( "RIP/%s: could not listen\n", p->name );
445
  init_list( &P->rtable );
446
  init_list( &P->connections );
447
  P->timer = tm_new( p->pool );
448
  P->timer->data = p;
449
  P->timer->randomize = 5;
450
  P->timer->recurrent = RIP_TIME; 
451
  P->timer->hook = rip_timer;
452
  tm_start( P->timer, 5 );
453

    
454
  debug( "RIP: ...done\n");
455
}
456

    
457
static void
458
rip_init(struct protocol *p)
459
{
460
  debug( "RIP: initializing RIP...\n" );
461
}
462

    
463
static void
464
rip_dump(struct proto *p)
465
{
466
  int i;
467
  node *w, *e;
468
  i = 0;
469
  WALK_LIST( w, P->connections ) {
470
    struct rip_connection *n = (void *) w;
471
    debug( "RIP: connection #%d: %I\n", n->num, n->addr );
472
  }
473
  i = 0;
474
  WALK_LIST( e, P->rtable ) {
475
    debug( "RIP: entry #%d: ", i++ );
476
    rip_dump_entry( E );
477
  }
478
}
479

    
480
static void
481
rip_if_notify(struct proto *p, unsigned c, struct iface *old, struct iface *new)
482
{
483
}
484

    
485
static void
486
rip_rt_notify(struct proto *p, struct network *net, struct rte *new, struct rte *old)
487
{
488
  debug( "rip: new entry came\n" );
489
  /* FIXME: should add/delete that entry from my routing tables, and
490
     set it so that it never times out */
491

    
492
  if (old) {
493
    struct rip_entry *e = find_entry( p, net->n.prefix, net->n.pxlen );
494
    if (!e)
495
      log( L_ERR "Deleting nonexistent entry?!\n" );
496

    
497
    kill_entry_ourrt( p, e );
498
  }
499

    
500
  /* FIXME: what do we do if we already know route to that target? We were not prepared to that. */
501

    
502
  if (new) {
503
    struct rip_entry *e = new_entry( p );
504

    
505
    e->whotoldme = IPA_NONE;
506
    e->network = net->n.prefix;
507
    e->pxlen = net->n.pxlen;
508
    e->nexthop = IPA_NONE; /* FIXME: is it correct? */
509
    e->tag = 0;                   /* FIXME: how to set tag? */
510
    e->metric = 1;           /* FIXME: how to set metric? */
511
    e->updated = e->changed = now;
512
    e->flags = RIP_F_EXTERNAL;
513
  }
514
}
515

    
516
static void
517
rip_preconfig(struct protocol *x)
518
{
519
  struct proto *p = proto_new(&proto_rip, sizeof(struct rip_data));
520

    
521
  debug( "RIP: preconfig\n" );
522
  p->preference = DEF_PREF_DIRECT;
523
  p->start = rip_start;
524
  p->if_notify = rip_if_notify;
525
  p->rt_notify = rip_rt_notify;
526
  p->dump = rip_dump;
527
}
528

    
529
static void
530
rip_postconfig(struct protocol *p)
531
{
532
}
533

    
534
struct protocol proto_rip = {
535
  { NULL, NULL },
536
  "RIP",
537
  0,
538
  rip_init,
539
  rip_preconfig,
540
  rip_postconfig
541
};
542