Revision 1d941de4

View differences:

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

  
25 25
int infinity = 16;
26 26

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

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

  
33 33
static void
......
44 44
 */
45 45

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

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

  
61 88
static struct rip_entry *
62
new_entry( struct proto *p, struct rip_block *b, ip_addr whotoldme )
89
new_entry( struct proto *p )
63 90
{
64 91
  struct rip_entry *e;
65

  
66 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

  
67 103
  e->whotoldme = whotoldme;
68 104
  e->network = b->network;
69 105
  e->pxlen = ipa_mklen( b->netmask );
......
75 111
  e->tag = ntohs( b->tag );
76 112
  e->metric = ntohl( b->metric );
77 113
  e->updated = e->changed = now;
78

  
79
  {
80
    rta *a, A;
81
    rte *r;
82
    net *n;
83
    
84
    bzero(&A, sizeof(A));
85
    A.proto = p;
86
    A.source = RTS_RIP;
87
    A.scope = SCOPE_UNIVERSE;
88
    A.cast = RTC_UNICAST;
89
    A.dest = RTD_ROUTER;
90
    A.tos = 0;
91
    A.flags = 0;
92
    A.gw = e->nexthop;
93
    A.from = e->whotoldme;
94
    A.iface = /* fixme: need to set somehow */ NULL;
95
    /* set to: interface of nexthop */
96
    a = rta_lookup(&A);
97
    n = net_get( &master_table, 0, e->network, e->pxlen );
98
    r = rte_get_temp(a);
99
    r->pflags = 0; /* Here go my flags */
100
    rte_update( n, p, r );
101
  }
102
  
103 114
  return e;
104 115
}
105 116

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

  
112
  n = net_find(&master_table, 0, e->network, e->pxlen );
113
  if (!n) debug( "Could not find entry to delete in main routing table.\n" );
114
     else rte_update( n, p, NULL );
115

  
116 124
  rem_node( NODE e );
117 125
  WALK_LIST( c, P->connections ) {
118
    if (c->sendptr == e)
119
      die( "kill_entry: one of connections has this as current, fix me!" );
126
    if (c->sendptr == e) {
127
      debug( "Deleting from under someone's sendptr...\n" );
128
      c->sendptr = (void *) (NODE c->sendptr)->next;
129
    }
120 130
  }
121 131
  mb_free( e );
122 132
}
123 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

  
124 153
/*
125 154
 * Output processing
126 155
 */
......
159 188
      break;
160 189
    debug( "." );
161 190
    packet->block[i].family  = htons( 2 ); /* AF_INET */
162
    packet->block[i].tag     = htons( 0 ); /* What should I set it to? */
191
    packet->block[i].tag     = htons( 0 ); /* FIXME: What should I set it to? */
163 192
    packet->block[i].network = c->sendptr->network;
164 193
    packet->block[i].netmask = ipa_mkmask( c->sendptr->pxlen );
165 194
    packet->block[i].nexthop = IPA_NONE; /* FIXME: How should I set it? */
166 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
    }
167 201
    ipa_hton( packet->block[i].network );
168 202
    ipa_hton( packet->block[i].netmask );
169 203
    ipa_hton( packet->block[i].nexthop );
......
215 249
  rip_tx( c->send );
216 250
}
217 251

  
218

  
219 252
/*
220 253
 * Input processing
221 254
 */
......
223 256
static int
224 257
process_authentication( struct proto *p, struct rip_block *block )
225 258
{
259
  /* FIXME: Should do md5 authentication */
226 260
  return 0;
227 261
}
228 262

  
......
241 275
    return;
242 276
  }
243 277

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

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

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

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

  
......
264 298

  
265 299
  if (!e) {
266 300
    debug( "this is new" );
267
    e = new_entry( p, block, whotoldme );
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 );
268 307
    add_head( &P->rtable, NODE e );
269 308
  }
270 309

  
......
287 326
  }
288 327

  
289 328
  switch( packet->heading.command ) {
290
  case 1: debug( "Asked to send my routing table\n" ); 
329
  case RIPCMD_REQUEST: debug( "Asked to send my routing table\n" ); 
291 330
    	  rip_sendto( p, whotoldme, port );
292 331
          break;
293
  case 2: debug( "Part of routing table came\n" ); 
332
  case RIPCMD_RESPONSE: debug( "Part of routing table came\n" ); 
294 333
          if (port != RIP_PORT) {
295 334
	    log( L_AUTH "%I send me routing info from port %d\n", whotoldme, port );
296 335
	    return 0;
......
301 340
	    return 0;
302 341
	  }
303 342

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

  
306 345
          for (i=0; i<num; i++) {
307 346
	    struct rip_block *block = &packet->block[i];
......
320 359
	    process_block( p, block, whotoldme );
321 360
	  }
322 361
          break;
323
  case 3:
324
  case 4: BAD( "I was asked for traceon/traceoff\n" );
362
  case RIPCMD_TRACEON:
363
  case RIPCMD_TRACEOFF: BAD( "I was asked for traceon/traceoff\n" );
325 364
  case 5: BAD( "Some Sun extension around here\n" );
326 365
  default: BAD( "Unknown command" );
327 366
  }
......
338 377

  
339 378
  CHK_MAGIC;
340 379
  debug( "RIP: message came: %d bytes\n", size );
341
  if (size <= 0) BAD( "Zero sized packet" );
342 380
  size -= sizeof( struct rip_packet_heading );
343 381
  if (size < 0) BAD( "Too small packet" );
344 382
  if (size % sizeof( struct rip_block )) BAD( "Odd sized packet" );
......
356 394
static void
357 395
rip_dump_entry( struct rip_entry *e )
358 396
{
359
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d\n", 
397
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d ", 
360 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" );
361 401
}
362 402

  
363 403
static void
......
370 410
  debug( "RIP: tick tock\n" );
371 411

  
372 412
  WALK_LIST_DELSAFE( e, et, P->rtable ) {
373
    if ((now - E->updated) > (180+120)) {
374
      debug( "RIP: entry is too old: " );
375
      rip_dump_entry( E );
376
      kill_entry( p, E );
377
    }
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
      }
378 419
  }
379 420

  
380 421
#if 0
......
382 423
  rip_sendto( p, _MI( 0x0a000001 ), RIP_PORT );
383 424
#endif
384 425

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

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

  
......
440 483
}
441 484

  
442 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
443 517
rip_preconfig(struct protocol *x)
444 518
{
445 519
  struct proto *p = proto_new(&proto_rip, sizeof(struct rip_data));
......
448 522
  p->preference = DEF_PREF_DIRECT;
449 523
  p->start = rip_start;
450 524
  p->if_notify = rip_if_notify;
525
  p->rt_notify = rip_rt_notify;
451 526
  p->dump = rip_dump;
452 527
}
453 528

  
proto/rip/rip.h
36 36

  
37 37
struct rip_entry {
38 38
  node n;
39
  ip_addr whotoldme;
40 39

  
40
  ip_addr whotoldme;
41 41
  ip_addr network;
42 42
  int pxlen;
43 43
  ip_addr nexthop;
44

  
45 44
  int metric;
46 45
  u16 tag;
47 46

  
48 47
  bird_clock_t updated, changed;
48
  int flags;
49
#define RIP_F_EXTERNAL 1
49 50
};
50 51

  
51 52
struct rip_packet {

Also available in: Unified diff