summaryrefslogtreecommitdiffstats
path: root/proto/rip/rip.c
blob: 497c49030554d2051ac2035b9a2297b129af0448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 *	Rest in pieces - RIP protocol
 *
 *	(c) 1998 Pavel Machek <pavel@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#define LOCAL_DEBUG

#include <string.h>
#include <stdlib.h>

#include "nest/bird.h"
#include "nest/iface.h"
#include "nest/protocol.h"
#include "nest/route.h"
#include "lib/socket.h"
#include "lib/resource.h"
#include "lib/lists.h"
#include "lib/timer.h"

#include "rip.h"

int infinity = 16;

/* XXX should be 520 */
#define RIP_PORT 1520

/* XXX should be 30 */
#define RIP_TIME 5

static void
rip_reply(struct proto *p)
{
#if 0
  P->listen->tbuf = "ACK!";
  sk_send_to( P->listen, 5, P->listen->faddr, P->listen->fport );
#endif
}

/*
 * Entries
 */

static struct rip_entry *
find_entry( struct proto *p, ip_addr network, ip_addr netmask )
{
  struct node *e;

  CHK_MAGIC;
  WALK_LIST( e, P->rtable ) {
    if (ipa_equal( network, E->network ) &&
	(ipa_mklen( netmask ) == E->pxlen)) {
      return E;
    }
  }
  return NULL;
}

static struct rip_entry *
new_entry( struct proto *p, struct rip_block *b, ip_addr whotoldme )
{
  struct rip_entry *e;

  e = mb_alloc(p->pool, sizeof( struct rip_entry ));
  e->whotoldme = whotoldme;
  e->network = b->network;
  e->pxlen = ipa_mklen( b->netmask );
  /* FIXME: verify that it is really netmask */
  if (ipa_nonzero(b->nexthop))
    e->nexthop = b->nexthop;
  else
    e->nexthop = whotoldme;
  e->tag = ntohs( b->tag );
  e->metric = ntohl( b->metric );
  e->updated = e->changed = now;

  {
    rta *a, A;
    rte *r;
    net *n;
    
    bzero(&A, sizeof(A));
    A.proto = p;
    A.source = RTS_RIP;
    A.scope = SCOPE_UNIVERSE;
    A.cast = RTC_UNICAST;
    A.dest = RTD_ROUTER;
    A.tos = 0;
    A.flags = 0;
    A.gw = e->nexthop;
    A.from = e->whotoldme;
    A.iface = /* fixme: need to set somehow */ NULL;
    /* set to: interface of nexthop */
    a = rta_lookup(&A);
    n = net_get( &master_table, 0, e->network, e->pxlen );
    r = rte_get_temp(a);
    r->pflags = 0; /* Here go my flags */
    rte_update( n, p, r );
  }
  
  return e;
}

static void
kill_entry( struct proto *p, struct rip_entry *e )
{
  struct rip_connection *c;
  net *n;

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

  rem_node( NODE e );
  WALK_LIST( c, P->connections ) {
    if (c->sendptr == e)
      die( "kill_entry: one of connections has this as current, fix me!" );
  }
  mb_free( e );
}

/*
 * Output processing
 */

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

static void
rip_tx( sock *s )
{
  struct rip_connection *c = s->data;
  struct proto *p = c->proto;
  struct rip_packet *packet = (void *) s->tbuf;
  int i, done = 0;

givemore:

  debug( "Preparing packet to send: " );

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

  packet->heading.command = RIPCMD_RESPONSE;
  packet->heading.version = RIP_V2;
  packet->heading.unused  = 0;

  for (i = 0; i < 25; i++) {
    if (!(NODE c->sendptr)->next)
      break;
    debug( "." );
    packet->block[i].family  = htons( 2 ); /* AF_INET */
    packet->block[i].tag     = htons( 0 ); /* What should I set it to? */
    packet->block[i].network = c->sendptr->network;
    packet->block[i].netmask = ipa_mkmask( c->sendptr->pxlen );
    packet->block[i].nexthop = IPA_NONE; /* FIXME: How should I set it? */
    packet->block[i].metric  = htonl( c->sendptr->metric );
    ipa_hton( packet->block[i].network );
    ipa_hton( packet->block[i].netmask );
    ipa_hton( packet->block[i].nexthop );

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

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

  i = sk_send( s, sizeof( struct rip_packet_heading ) + i*sizeof( struct rip_block ) );
  if (i<0) rip_tx_err( s, i );
  if (i>0) {
    debug( "it wants more\n" );
    goto givemore;
  }
  
  debug( "blocked\n" );
}

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

  c->addr = daddr;
  c->proto = p;
  c->num = num++;

  c->send = sk_new( p->pool );
  c->send->type = SK_UDP;
  c->send->sport = RIP_PORT;
  c->send->dport = dport;
  c->send->daddr = daddr;
  c->send->rx_hook = NULL;
  c->send->tx_hook = rip_tx;
  c->send->err_hook = rip_tx_err;
  c->send->data = c;
  c->send->tbuf = mb_alloc( p->pool, sizeof( struct rip_packet ));
  if (sk_open(c->send)<0) {
    log( L_ERR "Could not open socket for data send to %I:%d\n", daddr, dport );
    return;
  }

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

  rip_tx( c->send );
}


/*
 * Input processing
 */

static int
process_authentication( struct proto *p, struct rip_block *block )
{
  return 0;
}

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

static void
process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme )
{
  struct rip_entry *e;
  int metric = ntohl( block->metric );
  ip_addr network = block->network;

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

  /* XXX Check if destination looks valid - ie not net 0 or 127 */

  /* XXX Should add configurable ammount */
  if (metric < infinity)
    metric++;

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

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

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

  if (e && ipa_equal( whotoldme, e->whotoldme ) &&
      (e->metric == metric)) {
    debug( "old news, updating time" );
    e->updated = now;
  }

  if (!e) {
    debug( "this is new" );
    e = new_entry( p, block, whotoldme );
    add_head( &P->rtable, NODE e );
  }

  debug( "\n" );
}
#undef DELETE

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

static int
rip_process_packet( struct proto *p, struct rip_packet *packet, int num, ip_addr whotoldme, int port )
{
  int i;
  int native_class = 0;

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

  switch( packet->heading.command ) {
  case 1: debug( "Asked to send my routing table\n" ); 
    	  rip_sendto( p, whotoldme, port );
          break;
  case 2: debug( "Part of routing table came\n" ); 
          if (port != RIP_PORT) {
	    log( L_AUTH "%I send me routing info from port %d\n", whotoldme, port );
	    return 0;
	  }

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

	  /* Should check if it is not my own packet */

          for (i=0; i<num; i++) {
	    struct rip_block *block = &packet->block[i];
	    if (block->family == 0xffff)
	      if (!i) {
		if (process_authentication(p, block))
		  BAD( "Authentication failed\n" );
	      } else BAD( "Authentication is not the first!" );
	    ipa_ntoh( block->network );
	    ipa_ntoh( block->netmask );
	    ipa_ntoh( block->nexthop );
	    if (packet->heading.version == RIP_V1) {
	      block->netmask = block->network; /* MJ: why are macros like this?! */
	      ipa_class_mask( block->netmask );
	    }
	    process_block( p, block, whotoldme );
	  }
          break;
  case 3:
  case 4: BAD( "I was asked for traceon/traceoff\n" );
  case 5: BAD( "Some Sun extension around here\n" );
  default: BAD( "Unknown command" );
  }

  rip_reply(p);
  return 0;
}

static int
rip_rx(sock *s, int size)
{
  struct proto *p = s->data;
  int num;

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

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

/*
 * Interface to rest of bird
 */

static void
rip_dump_entry( struct rip_entry *e )
{
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d\n", 
  e->whotoldme, e->updated-now, e->changed-now, e->network, e->pxlen, e->nexthop, e->metric );
}

static void
rip_timer(timer *t)
{
  struct proto *p = t->data;
  struct rip_entry *e, *et;

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

  WALK_LIST_DELSAFE( e, et, P->rtable ) {
    if ((now - E->updated) > (180+120)) {
      debug( "RIP: entry is too old: " );
      rip_dump_entry( E );
      kill_entry( p, E );
    }
  }

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

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

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

  P->listen = sk_new( p->pool );
  P->listen->type = SK_UDP;
  P->listen->sport = RIP_PORT;
  P->listen->rx_hook = rip_rx;
  P->listen->data = p;
  P->listen->rbsize = 10240;
  P->magic = RIP_MAGIC;
  if (sk_open(P->listen)<0)
    die( "RIP/%s: could not listen\n", p->name );
  init_list( &P->rtable );
  init_list( &P->connections );
  P->timer = tm_new( p->pool );
  P->timer->data = p;
  P->timer->randomize = 5;
  P->timer->recurrent = RIP_TIME; 
  P->timer->hook = rip_timer;
  tm_start( P->timer, 5 );

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

static void
rip_init(struct protocol *p)
{
  debug( "RIP: initializing RIP...\n" );
}

static void
rip_dump(struct proto *p)
{
  int i;
  node *w, *e;
  i = 0;
  WALK_LIST( w, P->connections ) {
    struct rip_connection *n = (void *) w;
    debug( "RIP: connection #%d: %I\n", n->num, n->addr );
  }
  i = 0;
  WALK_LIST( e, P->rtable ) {
    debug( "RIP: entry #%d: ", i++ );
    rip_dump_entry( E );
  }
}

static void
rip_if_notify(struct proto *p, unsigned c, struct iface *old, struct iface *new)
{
}

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

  debug( "RIP: preconfig\n" );
  p->preference = DEF_PREF_DIRECT;
  p->start = rip_start;
  p->if_notify = rip_if_notify;
  p->dump = rip_dump;
}

static void
rip_postconfig(struct protocol *p)
{
}

struct protocol proto_rip = {
  { NULL, NULL },
  "RIP",
  0,
  rip_init,
  rip_preconfig,
  rip_postconfig
};