mirror of
				https://gitlab.labs.nic.cz/labs/bird.git
				synced 2024-05-11 16:54:54 +00:00 
			
		
		
		
	Rip now includes code to reply, but it is currently broken.
This commit is contained in:
		
							
								
								
									
										224
									
								
								proto/rip/rip.c
									
									
									
									
									
								
							
							
						
						
									
										224
									
								
								proto/rip/rip.c
									
									
									
									
									
								
							@@ -27,10 +27,8 @@ int infinity = 16;
 | 
			
		||||
/* XXX should be 520 */
 | 
			
		||||
#define RIP_PORT 1520
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
rip_if_notify(struct proto *p, unsigned c, struct iface *old, struct iface *new)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
/* XXX should be 30 */
 | 
			
		||||
#define RIP_TIME 5
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
rip_reply(struct proto *p)
 | 
			
		||||
@@ -39,13 +37,9 @@ rip_reply(struct proto *p)
 | 
			
		||||
  sk_send_to( P->listen, 5, P->listen->faddr, P->listen->fport );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define BAD( x ) { log( L_WARN "RIP/%s: " x "\n", p->name ); return; }
 | 
			
		||||
 | 
			
		||||
static int
 | 
			
		||||
process_authentication( struct proto *p, struct rip_block *block )
 | 
			
		||||
{
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
 * Entries
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
static struct rip_entry *
 | 
			
		||||
find_entry( struct proto *p, ip_addr network, ip_addr netmask )
 | 
			
		||||
@@ -55,15 +49,13 @@ find_entry( struct proto *p, ip_addr network, ip_addr netmask )
 | 
			
		||||
  CHK_MAGIC;
 | 
			
		||||
  WALK_LIST( e, P->rtable ) {
 | 
			
		||||
    if (ipa_equal( network, E->network ) &&
 | 
			
		||||
	ipa_equal( netmask, E->netmask )) {
 | 
			
		||||
	(ipa_mklen( netmask ) == E->pxlen)) {
 | 
			
		||||
      return E;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define ipa_from_u32(a) (*(ip_addr *) &a)
 | 
			
		||||
 | 
			
		||||
static struct rip_entry *
 | 
			
		||||
new_entry( struct proto *p, struct rip_block *b, ip_addr whotoldme )
 | 
			
		||||
{
 | 
			
		||||
@@ -71,16 +63,40 @@ new_entry( struct proto *p, struct rip_block *b, ip_addr whotoldme )
 | 
			
		||||
 | 
			
		||||
  e = mb_alloc(p->pool, sizeof( struct rip_entry ));
 | 
			
		||||
  e->whotoldme = whotoldme;
 | 
			
		||||
  e->network = ipa_from_u32( b->network );
 | 
			
		||||
  e->netmask = ipa_from_u32( b->netmask );
 | 
			
		||||
  e->network = b->network;
 | 
			
		||||
  e->pxlen = ipa_mklen( b->netmask );
 | 
			
		||||
  /* FIXME: verify that it is really netmask */
 | 
			
		||||
  if (b->nexthop)
 | 
			
		||||
    e->nexthop = ipa_from_u32( b->nexthop );
 | 
			
		||||
  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;
 | 
			
		||||
}
 | 
			
		||||
@@ -89,6 +105,11 @@ 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 ) {
 | 
			
		||||
@@ -98,6 +119,111 @@ kill_entry( struct proto *p, struct rip_entry *e )
 | 
			
		||||
  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 = 99; /* FIXME */
 | 
			
		||||
  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( 0 ); /* Is it right? */
 | 
			
		||||
    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+1 );
 | 
			
		||||
 | 
			
		||||
  i = sk_send( s, sizeof( struct rip_packet_heading ) + (i+1)*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 send data 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
 | 
			
		||||
@@ -105,7 +231,7 @@ process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme )
 | 
			
		||||
{
 | 
			
		||||
  struct rip_entry *e;
 | 
			
		||||
  int metric = ntohl( block->metric );
 | 
			
		||||
  ip_addr network = ipa_from_u32( block->network );
 | 
			
		||||
  ip_addr network = block->network;
 | 
			
		||||
 | 
			
		||||
  CHK_MAGIC;
 | 
			
		||||
  if ((!metric) || (metric > infinity)) {
 | 
			
		||||
@@ -119,9 +245,9 @@ process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme )
 | 
			
		||||
  if (metric < infinity)
 | 
			
		||||
    metric++;
 | 
			
		||||
 | 
			
		||||
  debug( "block: %I tells me: %I available, metric %d... ", whotoldme, network, metric );
 | 
			
		||||
  debug( "block: %I tells me: %I/%I available, metric %d... ", whotoldme, network, block->netmask, metric );
 | 
			
		||||
 | 
			
		||||
  e = find_entry( p, network, ipa_from_u32( block->netmask ) );
 | 
			
		||||
  e = find_entry( p, network, block->netmask );
 | 
			
		||||
  if (e && (e->metric > metric)) /* || if same metrics and this is much newer */ 
 | 
			
		||||
    DELETE( "better metrics... " );
 | 
			
		||||
 | 
			
		||||
@@ -144,29 +270,33 @@ process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme )
 | 
			
		||||
}
 | 
			
		||||
#undef DELETE
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
#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( "Warning: I do not talk ripv1\n" ); break;
 | 
			
		||||
  case RIP_V2: break;
 | 
			
		||||
  case '!': debug( "OOk, sir!\n" ); break;
 | 
			
		||||
  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" ); break;
 | 
			
		||||
  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;
 | 
			
		||||
	    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;
 | 
			
		||||
	    return 0;
 | 
			
		||||
	  }
 | 
			
		||||
 | 
			
		||||
	  /* Should check if it is not my own packet */
 | 
			
		||||
@@ -178,20 +308,27 @@ rip_process_packet( struct proto *p, struct rip_packet *packet, int num, ip_addr
 | 
			
		||||
		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" );
 | 
			
		||||
  case '!': debug( "He gave me!\n" ); break;
 | 
			
		||||
  default: BAD( "Unknown command" );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  rip_reply(p);
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
static int
 | 
			
		||||
rip_rx(sock *s, int size)
 | 
			
		||||
{
 | 
			
		||||
  struct proto *p = s->data;
 | 
			
		||||
@@ -207,13 +344,18 @@ rip_rx(sock *s, int size)
 | 
			
		||||
  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/%I go via %I, metric %d\n", 
 | 
			
		||||
  e->whotoldme, e->updated-now, e->changed-now, e->network, e->netmask, e->nexthop, e->metric );
 | 
			
		||||
  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
 | 
			
		||||
@@ -232,6 +374,11 @@ rip_timer(timer *t)
 | 
			
		||||
      kill_entry( p, E );
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  debug( "RIP: Broadcasting routing tables\n" );
 | 
			
		||||
  rip_sendto( p, _MI( 0x0a000001 ), RIP_PORT );
 | 
			
		||||
 | 
			
		||||
  debug( "RIP: tick tock done\n" );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -253,9 +400,9 @@ rip_start(struct proto *p)
 | 
			
		||||
  P->timer = tm_new( p->pool );
 | 
			
		||||
  P->timer->data = p;
 | 
			
		||||
  P->timer->randomize = 5;
 | 
			
		||||
  P->timer->recurrent = 30; 
 | 
			
		||||
  P->timer->recurrent = RIP_TIME; 
 | 
			
		||||
  P->timer->hook = rip_timer;
 | 
			
		||||
  tm_start( P->timer, 30 );
 | 
			
		||||
  tm_start( P->timer, 5 );
 | 
			
		||||
 | 
			
		||||
  debug( "RIP: ...done\n");
 | 
			
		||||
}
 | 
			
		||||
@@ -273,8 +420,8 @@ rip_dump(struct proto *p)
 | 
			
		||||
  node *w, *e;
 | 
			
		||||
  i = 0;
 | 
			
		||||
  WALK_LIST( w, P->connections ) {
 | 
			
		||||
    struct rip_connection *n = w;
 | 
			
		||||
    debug( "RIP: connection #%d: %I\n", i, n->addr );
 | 
			
		||||
    struct rip_connection *n = (void *) w;
 | 
			
		||||
    debug( "RIP: connection #%d: %I\n", n->num, n->addr );
 | 
			
		||||
  }
 | 
			
		||||
  i = 0;
 | 
			
		||||
  WALK_LIST( e, P->rtable ) {
 | 
			
		||||
@@ -283,6 +430,11 @@ rip_dump(struct proto *p)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
rip_if_notify(struct proto *p, unsigned c, struct iface *old, struct iface *new)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
rip_preconfig(struct protocol *x)
 | 
			
		||||
{
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,10 @@
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
struct rip_connection {
 | 
			
		||||
  node n;
 | 
			
		||||
 | 
			
		||||
  int num;
 | 
			
		||||
  struct proto *proto;
 | 
			
		||||
  ip_addr addr;
 | 
			
		||||
  struct rip_entry *sendptr;
 | 
			
		||||
  sock *send;
 | 
			
		||||
@@ -10,6 +14,11 @@ struct rip_connection {
 | 
			
		||||
 | 
			
		||||
struct rip_packet_heading {
 | 
			
		||||
  u8 command;
 | 
			
		||||
#define RIPCMD_REQUEST          1       /* want info */
 | 
			
		||||
#define RIPCMD_RESPONSE         2       /* responding to request */
 | 
			
		||||
#define RIPCMD_TRACEON          3       /* turn tracing on */
 | 
			
		||||
#define RIPCMD_TRACEOFF         4       /* turn it off */
 | 
			
		||||
#define RIPCMD_MAX              5
 | 
			
		||||
  u8 version;
 | 
			
		||||
#define RIP_V1 1
 | 
			
		||||
#define RIP_V2 2
 | 
			
		||||
@@ -19,9 +28,9 @@ struct rip_packet_heading {
 | 
			
		||||
struct rip_block {
 | 
			
		||||
  u16 family;	/* 0xffff on first message means this is authentication */
 | 
			
		||||
  u16 tag;
 | 
			
		||||
  u32 network;
 | 
			
		||||
  u32 netmask;
 | 
			
		||||
  u32 nexthop;
 | 
			
		||||
  ip_addr network;
 | 
			
		||||
  ip_addr netmask;
 | 
			
		||||
  ip_addr nexthop;
 | 
			
		||||
  u32 metric;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -30,7 +39,7 @@ struct rip_entry {
 | 
			
		||||
  ip_addr whotoldme;
 | 
			
		||||
 | 
			
		||||
  ip_addr network;
 | 
			
		||||
  ip_addr netmask;
 | 
			
		||||
  int pxlen;
 | 
			
		||||
  ip_addr nexthop;
 | 
			
		||||
 | 
			
		||||
  int metric;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user