Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (27.3 KB)

1 a103373f Pavel Machek
/*
2
 *        Rest in pieces - RIP protocol
3
 *
4 1b16029c Pavel Machek
 *        Copyright (c) 1998, 1999 Pavel Machek <pavel@ucw.cz>
5 16c2d48d Ondrej Filip
 *                      2004       Ondrej Filip <feela@network.cz>
6 a103373f Pavel Machek
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8 c79ec2ec Pavel Machek
 *
9 f942a589 Pavel Machek
         FIXME: IpV6 support: packet size
10 c010f4cb Pavel Machek
        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 7e61cac3 Pavel Machek

15 5ed68e46 Ondrej Filip
        FIXME: (nonurgent): fold rip_connection into rip_interface?
16 3918b1b0 Pavel Machek

17 4d4de35f Pavel Machek
        FIXME: (nonurgent) allow bigger frequencies than 1 regular update in 6 seconds (?)
18
        FIXME: propagation of metric=infinity into main routing table may or may not be good idea.
19 a103373f Pavel Machek
 */
20
21 2337ade7 Pavel Machek
/**
22 58f7d004 Martin Mares
 * DOC: Routing Information Protocol
23 2337ade7 Pavel Machek
 *
24 58f7d004 Martin Mares
 * RIP is a pretty simple protocol, so about a half of its code is interface
25
 * with the core.
26 4c5f93d7 Pavel Machek
 *
27 58f7d004 Martin Mares
 * We maintain our own linked list of &rip_entry structures -- it serves
28
 * as our small routing table. RIP never adds to this linked list upon
29
 * packet reception; instead, it lets the core know about data from the packet
30
 * and waits for the core to call rip_rte_notify().
31
 *
32
 * Within rip_tx(), the list is
33
 * walked and a packet is generated using rip_tx_prepare(). This gets
34 2337ade7 Pavel Machek
 * tricky because we may need to send more than one packet to one
35 58f7d004 Martin Mares
 * destination. Struct &rip_connection is used to hold context information such as how
36
 * many of &rip_entry's we have already sent and it's also used to protect
37
 * against two concurrent sends to one destination. Each &rip_interface has
38 2337ade7 Pavel Machek
 * at most one &rip_connection.
39
 *
40 9b63e3a5 Pavel Machek
 * We are not going to honor requests for sending part of
41 58f7d004 Martin Mares
 * routing table. That would need to turn split horizon off etc.  
42 2337ade7 Pavel Machek
 *
43 58f7d004 Martin Mares
 * About triggered updates, RFC says: when a triggered update was sent,
44
 * don't send a new one for something between 1 and 5 seconds (and send one
45
 * after that). We do something else: each 5 seconds,
46 2337ade7 Pavel Machek
 * we look for any changed routes and broadcast them.
47
 */
48
49 e5a47266 Martin Mares
#undef LOCAL_DEBUG
50 a103373f Pavel Machek
51
#include "nest/bird.h"
52
#include "nest/iface.h"
53
#include "nest/protocol.h"
54
#include "nest/route.h"
55
#include "lib/socket.h"
56
#include "lib/resource.h"
57
#include "lib/lists.h"
58
#include "lib/timer.h"
59 221135d6 Martin Mares
#include "lib/string.h"
60 a103373f Pavel Machek
61
#include "rip.h"
62 8b79c4cc Pavel Machek
#include <assert.h>
63 a103373f Pavel Machek
64 293e313e Pavel Machek
#define P ((struct rip_proto *) p)
65
#define P_CF ((struct rip_proto_config *)p->cf)
66
67 af834f86 Pavel Machek
#define TRACE(level, msg, args...) do { if (p->debug & level) { log(L_TRACE "%s: " msg, p->name , ## args); } } while(0)
68 cb822c07 Pavel Machek
69 b94bbe00 Pavel Machek
static struct rip_interface *new_iface(struct proto *p, struct iface *new, unsigned long flags, struct iface_patt *patt);
70
71 4c5f93d7 Pavel Machek
/*
72 58f7d004 Martin Mares
 * Output processing
73 b093c328 Pavel Machek
 *
74
 * This part is responsible for getting packets out to the network.
75 279f4c7b Pavel Machek
 */
76
77
static void
78
rip_tx_err( sock *s, int err )
79
{
80 502ded52 Martin Mares
  struct rip_connection *c = ((struct rip_interface *)(s->data))->busy;
81 279f4c7b Pavel Machek
  struct proto *p = c->proto;
82 5ed68e46 Ondrej Filip
  log( L_ERR "%s: Unexpected error at rip transmit: %M", p->name, err );
83 279f4c7b Pavel Machek
}
84
85 4c5f93d7 Pavel Machek
/*
86 b093c328 Pavel Machek
 * rip_tx_prepare:
87
 * @e: rip entry that needs to be translated to form suitable for network
88
 * @b: block to be filled
89
 *
90
 * Fill one rip block with info that needs to go to the network. Handle
91
 * nexthop and split horizont correctly. (Next hop is ignored for IPv6,
92
 * that could be fixed but it is not real problem).
93
 */
94 7f704c06 Pavel Machek
static int
95 c91fc9b6 Martin Mares
rip_tx_prepare(struct proto *p, struct rip_block *b, struct rip_entry *e, struct rip_interface *rif, int pos )
96 7e61cac3 Pavel Machek
{
97 18b4d6bf Pavel Machek
  int metric;
98 7e61cac3 Pavel Machek
  DBG( "." );
99
  b->tag     = htons( e->tag );
100
  b->network = e->n.prefix;
101 18b4d6bf Pavel Machek
  metric = e->metric;
102
  if (neigh_connected_to(p, &e->whotoldme, rif->iface)) {
103
    DBG( "(split horizon)" );
104
    metric = P_CF->infinity;
105
  }
106 7e61cac3 Pavel Machek
#ifndef IPV6
107 9c1a55de Pavel Machek
  b->family  = htons( 2 ); /* AF_INET */
108 7e61cac3 Pavel Machek
  b->netmask = ipa_mkmask( e->n.pxlen );
109
  ipa_hton( b->netmask );
110 d0031c5e Pavel Machek
111
  if (neigh_connected_to(p, &e->nexthop, rif->iface))
112
    b->nexthop = e->nexthop;
113
  else
114
    b->nexthop = IPA_NONE;
115 18b4d6bf Pavel Machek
  ipa_hton( b->nexthop );  
116
  b->metric  = htonl( metric );
117 7e61cac3 Pavel Machek
#else
118
  b->pxlen = e->n.pxlen;
119 18b4d6bf Pavel Machek
  b->metric  = metric; /* it is u8 */
120 7e61cac3 Pavel Machek
#endif
121 18b4d6bf Pavel Machek
122 7e61cac3 Pavel Machek
  ipa_hton( b->network );
123 7f704c06 Pavel Machek
124
  return pos+1;
125 7e61cac3 Pavel Machek
}
126
127 4c5f93d7 Pavel Machek
/*
128 b093c328 Pavel Machek
 * rip_tx - send one rip packet to the network
129
 */
130 7e61cac3 Pavel Machek
static void
131 279f4c7b Pavel Machek
rip_tx( sock *s )
132
{
133 c3e9b2ab Pavel Machek
  struct rip_interface *rif = s->data;
134
  struct rip_connection *c = rif->busy;
135 279f4c7b Pavel Machek
  struct proto *p = c->proto;
136
  struct rip_packet *packet = (void *) s->tbuf;
137 d3702d57 Pavel Machek
  int i, packetlen;
138 7f704c06 Pavel Machek
  int maxi, nullupdate = 1;
139 279f4c7b Pavel Machek
140 ac40c888 Pavel Machek
  DBG( "Sending to %I\n", s->daddr );
141 6996f459 Pavel Machek
  do {
142 279f4c7b Pavel Machek
143 772f4899 Pavel Machek
    if (c->done)
144
      goto done;
145 279f4c7b Pavel Machek
146 6996f459 Pavel Machek
    DBG( "Preparing packet to send: " );
147
148
    packet->heading.command = RIPCMD_RESPONSE;
149
    packet->heading.version = RIP_V2;
150
    packet->heading.unused  = 0;
151
152 1b16029c Pavel Machek
    i = !!P_CF->authtype;
153 c010f4cb Pavel Machek
#ifndef IPV6
154 7f704c06 Pavel Machek
    maxi = ((P_CF->authtype == AT_MD5) ? PACKET_MD5_MAX : PACKET_MAX);
155 c010f4cb Pavel Machek
#else
156
    maxi = 5; /* We need to have at least reserve of one at end of packet */
157
#endif
158 7f704c06 Pavel Machek
    
159 6996f459 Pavel Machek
    FIB_ITERATE_START(&P->rtable, &c->iter, z) {
160
      struct rip_entry *e = (struct rip_entry *) z;
161
162 e7f76bae Pavel Machek
      if (!rif->triggered || (!(e->updated < now-5))) {
163 7f704c06 Pavel Machek
        nullupdate = 0;
164 c91fc9b6 Martin Mares
        i = rip_tx_prepare( p, packet->block + i, e, rif, i );
165 7f704c06 Pavel Machek
        if (i >= maxi) {
166 e7f76bae Pavel Machek
          FIB_ITERATE_PUT(&c->iter, z);
167
          goto break_loop;
168
        }
169 6996f459 Pavel Machek
      }
170 ab749558 Martin Mares
    } FIB_ITERATE_END(z);
171 6996f459 Pavel Machek
    c->done = 1;
172
173
  break_loop:
174
175 d3702d57 Pavel Machek
    packetlen = rip_outgoing_authentication(p, (void *) &packet->block[0], packet, i);
176 1b16029c Pavel Machek
177 6996f459 Pavel Machek
    DBG( ", sending %d blocks, ", i );
178 7f704c06 Pavel Machek
    if (nullupdate) {
179 bd215f8b Pavel Machek
      DBG( "not sending NULL update\n" );
180 ff8ed632 Pavel Machek
      c->done = 1;
181 772f4899 Pavel Machek
      goto done;
182 bd215f8b Pavel Machek
    }
183 772f4899 Pavel Machek
    if (ipa_nonzero(c->daddr))
184
      i = sk_send_to( s, packetlen, c->daddr, c->dport );
185
    else
186
      i = sk_send( s, packetlen );
187 279f4c7b Pavel Machek
188 02933ddb Pavel Machek
    DBG( "it wants more\n" );
189 279f4c7b Pavel Machek
  
190 6996f459 Pavel Machek
  } while (i>0);
191
  
192
  if (i<0) rip_tx_err( s, i );
193 02933ddb Pavel Machek
  DBG( "blocked\n" );
194 7e61cac3 Pavel Machek
  return;
195
196 772f4899 Pavel Machek
done:
197
  DBG( "Looks like I'm" );
198
  c->rif->busy = NULL;
199
  rem_node(NODE c);
200
  mb_free(c);
201
  DBG( " done\n" );
202
  return;
203 279f4c7b Pavel Machek
}
204
205 4c5f93d7 Pavel Machek
/* 
206 b093c328 Pavel Machek
 * rip_sendto - send whole routing table to selected destination
207
 * @rif: interface to use. Notice that we lock interface so that at
208
 * most one send to one interface is done.
209
 */
210 279f4c7b Pavel Machek
static void
211 c3e9b2ab Pavel Machek
rip_sendto( struct proto *p, ip_addr daddr, int dport, struct rip_interface *rif )
212 279f4c7b Pavel Machek
{
213 c3e9b2ab Pavel Machek
  struct iface *iface = rif->iface;
214 8c86f96f Pavel Machek
  struct rip_connection *c;
215 279f4c7b Pavel Machek
  static int num = 0;
216
217 c3e9b2ab Pavel Machek
  if (rif->busy) {
218 5ed68e46 Ondrej Filip
    log (L_WARN "%s: Interface %s is much too slow, dropping request", p->name, iface->name);
219 c3e9b2ab Pavel Machek
    return;
220
  }
221 8c86f96f Pavel Machek
  c = mb_alloc( p->pool, sizeof( struct rip_connection ));
222 c3e9b2ab Pavel Machek
  rif->busy = c;
223 dc7c7494 Pavel Machek
  
224 279f4c7b Pavel Machek
  c->addr = daddr;
225
  c->proto = p;
226
  c->num = num++;
227 c3e9b2ab Pavel Machek
  c->rif = rif;
228 279f4c7b Pavel Machek
229 c3e9b2ab Pavel Machek
  c->dport = dport;
230
  c->daddr = daddr;
231
  if (c->rif->sock->data != rif)
232 852fc0af Martin Mares
    bug("not enough send magic");
233 279f4c7b Pavel Machek
234 6996f459 Pavel Machek
  c->done = 0;
235 28323d9d Martin Mares
  FIB_ITERATE_INIT( &c->iter, &P->rtable );
236 279f4c7b Pavel Machek
  add_head( &P->connections, NODE c );
237 28323d9d Martin Mares
  if (ipa_nonzero(daddr))
238
    TRACE(D_PACKETS, "Sending my routing table to %I:%d on %s", daddr, dport, rif->iface->name );
239
  else
240
    TRACE(D_PACKETS, "Broadcasting routing table to %s", rif->iface->name );
241 279f4c7b Pavel Machek
242 c3e9b2ab Pavel Machek
  rip_tx(c->rif->sock);
243 279f4c7b Pavel Machek
}
244
245 29df5739 Pavel Machek
static struct rip_interface*
246 2d9290e9 Pavel Machek
find_interface(struct proto *p, struct iface *what)
247
{
248
  struct rip_interface *i;
249 02933ddb Pavel Machek
250 2d9290e9 Pavel Machek
  WALK_LIST (i, P->interfaces)
251
    if (i->iface == what)
252
      return i;
253
  return NULL;
254
}
255
256 4c5f93d7 Pavel Machek
/*
257 58f7d004 Martin Mares
 * Input processing
258 b093c328 Pavel Machek
 *
259
 * This part is responsible for any updates that come from network 
260 279f4c7b Pavel Machek
 */
261
262 14758d87 Pavel Machek
static void
263
rip_rte_update_if_better(rtable *tab, net *net, struct proto *p, rte *new)
264
{
265
  rte *old;
266
267
  old = rte_find(net, p);
268 973cf09c Ondrej Filip
  if (!old || p->rte_better(new, old) ||
269
      (ipa_equal(old->attrs->from, new->attrs->from) &&
270
      (old->u.rip.metric != new->u.rip.metric)) )
271 f98e2915 Ondrej Zajicek
    rte_update(tab, net, p, p, new);
272 14758d87 Pavel Machek
}
273
274 4c5f93d7 Pavel Machek
/*
275 b093c328 Pavel Machek
 * advertise_entry - let main routing table know about our new entry
276
 * @b: entry in network format
277
 *
278
 * This basically translates @b to format used by bird core and feeds
279 4c5f93d7 Pavel Machek
 * bird core with this route.
280 b093c328 Pavel Machek
 */
281 21580e30 Pavel Machek
static void
282
advertise_entry( struct proto *p, struct rip_block *b, ip_addr whotoldme )
283
{
284
  rta *a, A;
285
  rte *r;
286
  net *n;
287 feb6abe0 Pavel Machek
  neighbor *neighbor;
288 29df5739 Pavel Machek
  struct rip_interface *rif;
289 c79ec2ec Pavel Machek
  int pxlen;
290 c05ea56f Pavel Machek
291 21580e30 Pavel Machek
  bzero(&A, sizeof(A));
292
  A.proto = p;
293
  A.source = RTS_RIP;
294
  A.scope = SCOPE_UNIVERSE;
295
  A.cast = RTC_UNICAST;
296
  A.dest = RTD_ROUTER;
297
  A.flags = 0;
298 c79ec2ec Pavel Machek
#ifndef IPV6
299 21580e30 Pavel Machek
  A.gw = ipa_nonzero(b->nexthop) ? b->nexthop : whotoldme;
300 c79ec2ec Pavel Machek
  pxlen = ipa_mklen(b->netmask);
301
#else
302 3b0b2cb6 Martin Mares
  /* FIXME: next hop is in other packet for v6 */
303
  A.gw = whotoldme; 
304 c79ec2ec Pavel Machek
  pxlen = b->pxlen;
305
#endif
306 21580e30 Pavel Machek
  A.from = whotoldme;
307 29df5739 Pavel Machek
308 a5b583f2 Pavel Machek
  /* No need to look if destination looks valid - ie not net 0 or 127 -- core will do for us. */
309 29df5739 Pavel Machek
310 feb6abe0 Pavel Machek
  neighbor = neigh_find( p, &A.gw, 0 );
311
  if (!neighbor) {
312 eb4097e4 Pavel Machek
    log( L_REMOTE "%s: %I asked me to route %I/%d using not-neighbor %I.", p->name, A.from, b->network, pxlen, A.gw );
313 feb6abe0 Pavel Machek
    return;
314
  }
315 491cd43b Martin Mares
  if (neighbor->scope == SCOPE_HOST) {
316
    DBG("Self-destined route, ignoring.\n");
317
    return;
318
  }
319 feb6abe0 Pavel Machek
320
  A.iface = neighbor->iface;
321 29df5739 Pavel Machek
  if (!(rif = neighbor->data)) {
322
    rif = neighbor->data = find_interface(p, A.iface);
323 02933ddb Pavel Machek
  }
324 491cd43b Martin Mares
  if (!rif)
325 29df5739 Pavel Machek
    bug("Route packet using unknown interface? No.");
326
    
327 21580e30 Pavel Machek
  /* set to: interface of nexthop */
328
  a = rta_lookup(&A);
329 c79ec2ec Pavel Machek
  if (pxlen==-1)  {
330 eb4097e4 Pavel Machek
    log( L_REMOTE "%s: %I gave me invalid pxlen/netmask for %I.", p->name, A.from, b->network );
331 29df5739 Pavel Machek
    return;
332
  }
333 c79ec2ec Pavel Machek
  n = net_get( p->table, b->network, pxlen );
334 21580e30 Pavel Machek
  r = rte_get_temp(a);
335 18b4d6bf Pavel Machek
#ifndef IPV6
336 3e474192 Pavel Machek
  r->u.rip.metric = ntohl(b->metric) + rif->metric;
337 18b4d6bf Pavel Machek
#else  
338
  r->u.rip.metric = b->metric + rif->metric;
339
#endif
340
341 ec21aecf Pavel Machek
  r->u.rip.entry = NULL;
342 293e313e Pavel Machek
  if (r->u.rip.metric > P_CF->infinity) r->u.rip.metric = P_CF->infinity;
343 21580e30 Pavel Machek
  r->u.rip.tag = ntohl(b->tag);
344 feb6abe0 Pavel Machek
  r->net = n;
345 21580e30 Pavel Machek
  r->pflags = 0; /* Here go my flags */
346 14758d87 Pavel Machek
  rip_rte_update_if_better( p->table, n, p, r );
347 02933ddb Pavel Machek
  DBG( "done\n" );
348 21580e30 Pavel Machek
}
349 a103373f Pavel Machek
350 4c5f93d7 Pavel Machek
/*
351 b093c328 Pavel Machek
 * process_block - do some basic check and pass block to advertise_entry
352
 */
353 a103373f Pavel Machek
static void
354
process_block( struct proto *p, struct rip_block *block, ip_addr whotoldme )
355
{
356 07f29765 Pavel Machek
#ifndef IPV6
357 a103373f Pavel Machek
  int metric = ntohl( block->metric );
358 07f29765 Pavel Machek
#else
359
  int metric = block->metric;
360
#endif
361 279f4c7b Pavel Machek
  ip_addr network = block->network;
362 a103373f Pavel Machek
363
  CHK_MAGIC;
364 004cf4fc Ondrej Filip
#ifdef IPV6
365
  TRACE(D_ROUTES, "block: %I tells me: %I/%d available, metric %d... ",
366
      whotoldme, network, block->pxlen, metric );
367
#else
368 16c2d48d Ondrej Filip
  TRACE(D_ROUTES, "block: %I tells me: %I/%d available, metric %d... ",
369
      whotoldme, network, ipa_mklen(block->netmask), metric );
370 004cf4fc Ondrej Filip
#endif
371
372 293e313e Pavel Machek
  if ((!metric) || (metric > P_CF->infinity)) {
373 c010f4cb Pavel Machek
#ifdef IPV6 /* Someone is sedning us nexthop and we are ignoring it */
374
    if (metric == 0xff)
375 201187c5 Martin Mares
      { DBG( "IpV6 nexthop ignored" ); return; }
376 c010f4cb Pavel Machek
#endif
377 5ed68e46 Ondrej Filip
    log( L_WARN "%s: Got metric %d from %I", p->name, metric, whotoldme );
378 a103373f Pavel Machek
    return;
379
  }
380
381 21580e30 Pavel Machek
  advertise_entry( p, block, whotoldme );
382 a103373f Pavel Machek
}
383
384 5ed68e46 Ondrej Filip
#define BAD( x ) { log( L_REMOTE "%s: " x, p->name ); return 1; }
385 279f4c7b Pavel Machek
386 4c5f93d7 Pavel Machek
/*
387 b093c328 Pavel Machek
 * rip_process_packet - this is main routine for incoming packets.
388
 */
389 279f4c7b Pavel Machek
static int
390 a103373f Pavel Machek
rip_process_packet( struct proto *p, struct rip_packet *packet, int num, ip_addr whotoldme, int port )
391
{
392
  int i;
393 6a57bb31 Martin Mares
  int authenticated = 0;
394 491cd43b Martin Mares
  neighbor *neighbor;
395 a103373f Pavel Machek
396
  switch( packet->heading.version ) {
397 02933ddb Pavel Machek
  case RIP_V1: DBG( "Rip1: " ); break;
398
  case RIP_V2: DBG( "Rip2: " ); break;
399 a103373f Pavel Machek
  default: BAD( "Unknown version" );
400
  }
401
402
  switch( packet->heading.command ) {
403 02933ddb Pavel Machek
  case RIPCMD_REQUEST: DBG( "Asked to send my routing table\n" ); 
404 eb4097e4 Pavel Machek
          if (P_CF->honor == HO_NEVER)
405
            BAD( "They asked me to send routing table, but I was told not to do it" );
406
407
          if ((P_CF->honor == HO_NEIGHBOR) && (!neigh_find( p, &whotoldme, 0 )))
408
            BAD( "They asked me to send routing table, but he is not my neighbor" );
409 bce8a34b Pavel Machek
              rip_sendto( p, whotoldme, port, HEAD(P->interfaces) ); /* no broadcast */
410 279f4c7b Pavel Machek
          break;
411 02933ddb Pavel Machek
  case RIPCMD_RESPONSE: DBG( "*** Rtable from %I\n", whotoldme ); 
412 293e313e Pavel Machek
          if (port != P_CF->port) {
413 5ed68e46 Ondrej Filip
            log( L_REMOTE "%s: %I send me routing info from port %d", p->name, whotoldme, port );
414 eb4097e4 Pavel Machek
            return 1;
415 a103373f Pavel Machek
          }
416
417 491cd43b Martin Mares
          if (!(neighbor = neigh_find( p, &whotoldme, 0 )) || neighbor->scope == SCOPE_HOST) {
418 5ed68e46 Ondrej Filip
            log( L_REMOTE "%s: %I send me routing info but he is not my neighbor", p->name, whotoldme );
419 279f4c7b Pavel Machek
            return 0;
420 a103373f Pavel Machek
          }
421
422
          for (i=0; i<num; i++) {
423
            struct rip_block *block = &packet->block[i];
424 9c1a55de Pavel Machek
#ifndef IPV6
425
            /* Authentication is not defined for v6 */
426 d3702d57 Pavel Machek
            if (block->family == 0xffff) {
427
              if (i)
428 1a509a63 Pavel Machek
                continue;        /* md5 tail has this family */
429 639e6285 Pavel Machek
              if (rip_incoming_authentication(p, (void *) block, packet, num, whotoldme))
430 d3702d57 Pavel Machek
                BAD( "Authentication failed" );
431
              authenticated = 1;
432
              continue;
433
            }
434 9c1a55de Pavel Machek
#endif
435 7bf19253 Pavel Machek
            if ((!authenticated) && (P_CF->authtype != AT_NONE))
436
              BAD( "Packet is not authenticated and it should be" );
437 279f4c7b Pavel Machek
            ipa_ntoh( block->network );
438 c79ec2ec Pavel Machek
#ifndef IPV6
439 279f4c7b Pavel Machek
            ipa_ntoh( block->netmask );
440
            ipa_ntoh( block->nexthop );
441 8c86f96f Pavel Machek
            if (packet->heading.version == RIP_V1)        /* FIXME (nonurgent): switch to disable this? */
442 89dc383a Martin Mares
              block->netmask = ipa_class_mask(block->network);
443 c79ec2ec Pavel Machek
#endif
444 a103373f Pavel Machek
            process_block( p, block, whotoldme );
445
          }
446
          break;
447 1d941de4 Pavel Machek
  case RIPCMD_TRACEON:
448 852fc0af Martin Mares
  case RIPCMD_TRACEOFF: BAD( "I was asked for traceon/traceoff" );
449
  case 5: BAD( "Some Sun extension around here" );
450 a103373f Pavel Machek
  default: BAD( "Unknown command" );
451
  }
452
453 279f4c7b Pavel Machek
  return 0;
454 a103373f Pavel Machek
}
455
456 4c5f93d7 Pavel Machek
/*
457 b093c328 Pavel Machek
 * rip_rx - Receive hook: do basic checks and pass packet to rip_process_packet
458
 */
459 279f4c7b Pavel Machek
static int
460 a103373f Pavel Machek
rip_rx(sock *s, int size)
461
{
462 c3e9b2ab Pavel Machek
  struct rip_interface *i = s->data;
463
  struct proto *p = i->proto;
464 a103373f Pavel Machek
  int num;
465
466 4a020137 Martin Mares
  /* In non-listening mode, just ignore packet */
467
  if (i->mode & IM_NOLISTEN)
468
    return 1;
469
470 a103373f Pavel Machek
  CHK_MAGIC;
471 28323d9d Martin Mares
  DBG( "RIP: message came: %d bytes from %I via %s\n", size, s->faddr, i->iface ? i->iface->name : "(dummy)" );
472 a103373f Pavel Machek
  size -= sizeof( struct rip_packet_heading );
473
  if (size < 0) BAD( "Too small packet" );
474
  if (size % sizeof( struct rip_block )) BAD( "Odd sized packet" );
475
  num = size / sizeof( struct rip_block );
476 7f704c06 Pavel Machek
  if (num>PACKET_MAX) BAD( "Too many blocks" );
477 a103373f Pavel Machek
478 3b0b2cb6 Martin Mares
#ifdef IPV6
479
  /* Try to absolutize link scope addresses */
480
  ipa_absolutize(&s->faddr, &i->iface->addr->ip);
481
#endif
482
483 b8524e9b Pavel Machek
  if (ipa_equal(i->iface->addr->ip, s->faddr)) {
484 0bff946c Pavel Machek
    DBG("My own packet\n");
485
    return 1;
486
  }
487
488 a103373f Pavel Machek
  rip_process_packet( p, (struct rip_packet *) s->rbuf, num, s->faddr, s->fport );
489 279f4c7b Pavel Machek
  return 1;
490 a103373f Pavel Machek
}
491
492 4c5f93d7 Pavel Machek
/*
493 58f7d004 Martin Mares
 * Interface to BIRD core
494 279f4c7b Pavel Machek
 */
495
496 a103373f Pavel Machek
static void
497
rip_dump_entry( struct rip_entry *e )
498
{
499 1d941de4 Pavel Machek
  debug( "%I told me %d/%d ago: to %I/%d go via %I, metric %d ", 
500 6996f459 Pavel Machek
  e->whotoldme, e->updated-now, e->changed-now, e->n.prefix, e->n.pxlen, e->nexthop, e->metric );
501 1d941de4 Pavel Machek
  debug( "\n" );
502 a103373f Pavel Machek
}
503
504 b093c328 Pavel Machek
/**
505 4c5f93d7 Pavel Machek
 * rip_timer
506
 * @t: timer
507
 *
508
 * Broadcast routing tables periodically (using rip_tx) and kill
509 58f7d004 Martin Mares
 * routes that are too old. RIP keeps a list of its own entries present
510
 * in the core table by a linked list (functions rip_rte_insert() and
511
 * rip_rte_delete() are responsible for that), it walks this list in the timer
512
 * and in case an entry is too old, it is discarded.
513 b093c328 Pavel Machek
 */
514 4c5f93d7 Pavel Machek
515 a103373f Pavel Machek
static void
516
rip_timer(timer *t)
517
{
518
  struct proto *p = t->data;
519 ec21aecf Pavel Machek
  struct fib_node *e, *et;
520 a103373f Pavel Machek
521
  CHK_MAGIC;
522 02933ddb Pavel Machek
  DBG( "RIP: tick tock\n" );
523 feb6abe0 Pavel Machek
  
524
  WALK_LIST_DELSAFE( e, et, P->garbage ) {
525
    rte *rte;
526
    rte = SKIP_BACK( struct rte, u.rip.garbage, e );
527 0bff946c Pavel Machek
#ifdef LOCAL_DEBUG
528 10f5c47d Pavel Machek
    {
529
      struct proto *p = rte->attrs->proto;
530
      CHK_MAGIC;
531
    }
532
    DBG( "Garbage: (%p)", rte ); rte_dump( rte );
533 0bff946c Pavel Machek
#endif
534 feb6abe0 Pavel Machek
535 ad390755 Pavel Machek
    if (now - rte->lastmod > P_CF->timeout_time) {
536 28323d9d Martin Mares
      TRACE(D_EVENTS, "entry is too old: %I", rte->net->n.prefix );
537 ec21aecf Pavel Machek
      if (rte->u.rip.entry) {
538
        rte->u.rip.entry->metric = P_CF->infinity;
539
        rte->u.rip.metric = P_CF->infinity;
540
      }
541 3918b1b0 Pavel Machek
    }
542
543 ad390755 Pavel Machek
    if (now - rte->lastmod > P_CF->garbage_time) {
544 28323d9d Martin Mares
      TRACE(D_EVENTS, "entry is much too old: %I", rte->net->n.prefix );
545 4f1a6d27 Martin Mares
      rte_discard(p->table, rte);
546 feb6abe0 Pavel Machek
    }
547 a103373f Pavel Machek
  }
548 279f4c7b Pavel Machek
549 02933ddb Pavel Machek
  DBG( "RIP: Broadcasting routing tables\n" );
550 feb6abe0 Pavel Machek
  {
551 6996f459 Pavel Machek
    struct rip_interface *rif;
552
    WALK_LIST( rif, P->interfaces ) {
553
      struct iface *iface = rif->iface;
554 279f4c7b Pavel Machek
555 6996f459 Pavel Machek
      if (!iface) continue;
556 3e474192 Pavel Machek
      if (rif->mode & IM_QUIET) continue;
557 feb6abe0 Pavel Machek
      if (!(iface->flags & IF_UP)) continue;
558
559 7e61cac3 Pavel Machek
      rif->triggered = (P->tx_count % 6);
560 6996f459 Pavel Machek
      rip_sendto( p, IPA_NONE, 0, rif );
561 feb6abe0 Pavel Machek
    }
562 84a13054 Pavel Machek
    P->tx_count ++;
563 feb6abe0 Pavel Machek
  }
564 1d941de4 Pavel Machek
565 02933ddb Pavel Machek
  DBG( "RIP: tick tock done\n" );
566 a103373f Pavel Machek
}
567
568 4c5f93d7 Pavel Machek
/*
569 a769a180 Pavel Machek
 * rip_start - initialize instance of rip
570
 */
571 edf62ba1 Pavel Machek
static int
572 a103373f Pavel Machek
rip_start(struct proto *p)
573
{
574 6996f459 Pavel Machek
  struct rip_interface *rif;
575 02933ddb Pavel Machek
  DBG( "RIP: starting instance...\n" );
576 a103373f Pavel Machek
577 8b79c4cc Pavel Machek
  assert( sizeof(struct rip_packet_heading) == 4);
578
  assert( sizeof(struct rip_block) == 20);
579
  assert( sizeof(struct rip_block_auth) == 20);
580
581 3fe5f899 Ondrej Filip
#ifdef LOCAL_DEBUG
582 a103373f Pavel Machek
  P->magic = RIP_MAGIC;
583 3fe5f899 Ondrej Filip
#endif
584 6996f459 Pavel Machek
  fib_init( &P->rtable, p->pool, sizeof( struct rip_entry ), 0, NULL );
585 a103373f Pavel Machek
  init_list( &P->connections );
586 feb6abe0 Pavel Machek
  init_list( &P->garbage );
587 c3e9b2ab Pavel Machek
  init_list( &P->interfaces );
588 a103373f Pavel Machek
  P->timer = tm_new( p->pool );
589
  P->timer->data = p;
590
  P->timer->randomize = 5;
591 4d4de35f Pavel Machek
  P->timer->recurrent = (P_CF->period / 6)+1; 
592 a103373f Pavel Machek
  P->timer->hook = rip_timer;
593 279f4c7b Pavel Machek
  tm_start( P->timer, 5 );
594 1b16029c Pavel Machek
  rif = new_iface(p, NULL, 0, NULL);        /* Initialize dummy interface */
595 6996f459 Pavel Machek
  add_head( &P->interfaces, NODE rif );
596 8333431c Pavel Machek
  CHK_MAGIC;
597 a103373f Pavel Machek
598 9a38757c Pavel Machek
  rip_init_instance(p);
599
600 02933ddb Pavel Machek
  DBG( "RIP: ...done\n");
601 edf62ba1 Pavel Machek
  return PS_UP;
602 a103373f Pavel Machek
}
603
604 edf62ba1 Pavel Machek
static struct proto *
605
rip_init(struct proto_config *cfg)
606 a103373f Pavel Machek
{
607 c748cdb9 Pavel Machek
  struct proto *p = proto_new(cfg, sizeof(struct rip_proto));
608 edf62ba1 Pavel Machek
609
  return p;
610 a103373f Pavel Machek
}
611
612
static void
613
rip_dump(struct proto *p)
614
{
615
  int i;
616 6a57bb31 Martin Mares
  node *w;
617 c3e9b2ab Pavel Machek
  struct rip_interface *rif;
618 a103373f Pavel Machek
  i = 0;
619 c748cdb9 Pavel Machek
620
  CHK_MAGIC;
621 a103373f Pavel Machek
  WALK_LIST( w, P->connections ) {
622 279f4c7b Pavel Machek
    struct rip_connection *n = (void *) w;
623
    debug( "RIP: connection #%d: %I\n", n->num, n->addr );
624 a103373f Pavel Machek
  }
625
  i = 0;
626 6996f459 Pavel Machek
  FIB_WALK( &P->rtable, e ) {
627 a103373f Pavel Machek
    debug( "RIP: entry #%d: ", i++ );
628 5ed68e46 Ondrej Filip
    rip_dump_entry( (struct rip_entry *)e );
629 6996f459 Pavel Machek
  } FIB_WALK_END;
630 c3e9b2ab Pavel Machek
  i = 0;
631
  WALK_LIST( rif, P->interfaces ) {
632 06fa1453 Pavel Machek
    debug( "RIP: interface #%d: %s, %I, busy = %x\n", i++, rif->iface?rif->iface->name:"(dummy)", rif->sock->daddr, rif->busy );
633 c3e9b2ab Pavel Machek
  }
634
}
635
636 dff1f579 Pavel Machek
static void
637 ce1da96e Martin Mares
rip_get_route_info(rte *rte, byte *buf, ea_list *attrs)
638 dff1f579 Pavel Machek
{
639 ce1da96e Martin Mares
  eattr *metric = ea_find(attrs, EA_RIP_METRIC);
640
  eattr *tag = ea_find(attrs, EA_RIP_TAG);
641
642
  buf += bsprintf(buf, " (%d/%d)", rte->pref, metric ? metric->u.data : 0);
643
  if (tag && tag->u.data)
644
    bsprintf(buf, " t%04x", tag->u.data);
645 dff1f579 Pavel Machek
}
646
647 c3e9b2ab Pavel Machek
static void
648 c91fc9b6 Martin Mares
kill_iface(struct rip_interface *i)
649 c3e9b2ab Pavel Machek
{
650 02933ddb Pavel Machek
  DBG( "RIP: Interface %s disappeared\n", i->iface->name);
651 c3e9b2ab Pavel Machek
  rfree(i->sock);
652
  mb_free(i);
653
}
654
655 6c0a7174 Pavel Machek
/**
656 4c5f93d7 Pavel Machek
 * new_iface
657
 * @p: myself
658 58f7d004 Martin Mares
 * @new: interface to be created or %NULL if we are creating a magic
659
 * socket. The magic socket is used for listening and also for
660
 * sending requested responses.
661 4c5f93d7 Pavel Machek
 * @flags: interface flags
662
 * @patt: pattern this interface matched, used for access to config options
663
 *
664 58f7d004 Martin Mares
 * Create an interface structure and start listening on the interface.
665 06fa1453 Pavel Machek
 */
666 b94bbe00 Pavel Machek
static struct rip_interface *
667 1b16029c Pavel Machek
new_iface(struct proto *p, struct iface *new, unsigned long flags, struct iface_patt *patt )
668 c3e9b2ab Pavel Machek
{
669 06fa1453 Pavel Machek
  struct rip_interface *rif;
670 3e474192 Pavel Machek
  struct rip_patt *PATT = (struct rip_patt *) patt;
671 06fa1453 Pavel Machek
672 72efa4b6 Pavel Machek
  rif = mb_allocz(p->pool, sizeof( struct rip_interface ));
673 06fa1453 Pavel Machek
  rif->iface = new;
674
  rif->proto = p;
675 6996f459 Pavel Machek
  rif->busy = NULL;
676 3e474192 Pavel Machek
  if (PATT) {
677
    rif->mode = PATT->mode;
678
    rif->metric = PATT->metric;
679
    rif->multicast = (!(PATT->mode & IM_BROADCAST)) && (flags & IF_MULTICAST);
680
  }
681 1b16029c Pavel Machek
  /* lookup multicasts over unnumbered links - no: rip is not defined over unnumbered links */
682 06fa1453 Pavel Machek
683 8c86f96f Pavel Machek
  if (rif->multicast)
684 b94bbe00 Pavel Machek
    DBG( "Doing multicasts!\n" );
685
686 06fa1453 Pavel Machek
  rif->sock = sk_new( p->pool );
687 f9c799a0 Ondrej Zajicek
  rif->sock->type = SK_UDP;
688 293e313e Pavel Machek
  rif->sock->sport = P_CF->port;
689 06fa1453 Pavel Machek
  rif->sock->rx_hook = rip_rx;
690
  rif->sock->data = rif;
691
  rif->sock->rbsize = 10240;
692 6996f459 Pavel Machek
  rif->sock->iface = new;                /* Automagically works for dummy interface */
693 06fa1453 Pavel Machek
  rif->sock->tbuf = mb_alloc( p->pool, sizeof( struct rip_packet ));
694
  rif->sock->tx_hook = rip_tx;
695
  rif->sock->err_hook = rip_tx_err;
696
  rif->sock->daddr = IPA_NONE;
697 293e313e Pavel Machek
  rif->sock->dport = P_CF->port;
698 bce8a34b Pavel Machek
  if (new)
699 e24ddd9b Martin Mares
    {
700
      rif->sock->ttl = 1;
701
      rif->sock->tos = IP_PREC_INTERNET_CONTROL;
702
    }
703 06fa1453 Pavel Machek
704 ff8ed632 Pavel Machek
  if (new) {
705
    if (new->addr->flags & IA_UNNUMBERED)
706 5ed68e46 Ondrej Filip
      log( L_WARN "%s: rip is not defined over unnumbered links", p->name );
707 8c86f96f Pavel Machek
    if (rif->multicast) {
708 c010f4cb Pavel Machek
#ifndef IPV6
709 ff8ed632 Pavel Machek
      rif->sock->daddr = ipa_from_u32(0xe0000009);
710
      rif->sock->saddr = ipa_from_u32(0xe0000009);
711 c010f4cb Pavel Machek
#else
712 3b0b2cb6 Martin Mares
      rif->sock->daddr = ipa_build(0xff020000, 0, 0, 9);
713
      rif->sock->saddr = new->addr->ip;
714 c010f4cb Pavel Machek
#endif
715 28323d9d Martin Mares
    } else {
716 8c86f96f Pavel Machek
      rif->sock->daddr = new->addr->brd;
717 28323d9d Martin Mares
      rif->sock->saddr = new->addr->brd;
718
    }
719 9de840bd Pavel Machek
  }
720 06fa1453 Pavel Machek
721 1b16029c Pavel Machek
  if (!ipa_nonzero(rif->sock->daddr)) {
722 5b846de6 Pavel Machek
    if (rif->iface)
723 5ed68e46 Ondrej Filip
      log( L_WARN "%s: interface %s is too strange for me", p->name, rif->iface->name );
724 f9c799a0 Ondrej Zajicek
  } else {
725
726
    if (sk_open(rif->sock)<0)
727
      goto err;
728
729
    if (rif->multicast)
730
      {
731
        if (sk_setup_multicast(rif->sock) < 0)
732
          goto err;
733
        if (sk_join_group(rif->sock, rif->sock->daddr) < 0)
734
          goto err;
735
      }
736
    else
737
      {
738
        if (sk_set_broadcast(rif->sock, 1) < 0)
739
          goto err;
740
      }
741 4a020137 Martin Mares
  }
742 b94bbe00 Pavel Machek
743 28323d9d Martin Mares
  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 );
744 c3e9b2ab Pavel Machek
  
745 06fa1453 Pavel Machek
  return rif;
746 f9c799a0 Ondrej Zajicek
747
 err:
748
  log( L_ERR "%s: could not create socket for %s", p->name, rif->iface ? rif->iface->name : "(dummy)" );
749
  if (rif->iface) {
750
    rfree(rif->sock);
751
    mb_free(rif);
752
    return NULL;
753
  }
754
  /* On dummy, we just return non-working socket, so that user gets error every time anyone requests table */
755 e81b440f Ondrej Zajicek
  return rif;
756 a103373f Pavel Machek
}
757
758
static void
759 ff8ed632 Pavel Machek
rip_real_if_add(struct object_lock *lock)
760
{
761
  struct iface *iface = lock->iface;
762
  struct proto *p = lock->data;
763
  struct rip_interface *rif;
764 20e94fb8 Ondrej Zajicek
  struct iface_patt *k = iface_patt_find(&P_CF->iface_list, iface);
765 ff8ed632 Pavel Machek
766
  if (!k)
767
    bug("This can not happen! It existed few seconds ago!" );
768
  DBG("adding interface %s\n", iface->name );
769
  rif = new_iface(p, iface, iface->flags, k);
770 9511a483 Pavel Machek
  if (rif) {
771
    add_head( &P->interfaces, NODE rif );
772
    DBG("Adding object lock of %p for %p\n", lock, rif);
773
    rif->lock = lock;
774 dc6405d2 Martin Mares
  } else { rfree(lock); }
775 ff8ed632 Pavel Machek
}
776
777
static void
778 9a158361 Martin Mares
rip_if_notify(struct proto *p, unsigned c, struct iface *iface)
779 279f4c7b Pavel Machek
{
780 02933ddb Pavel Machek
  DBG( "RIP: if notify\n" );
781 9a158361 Martin Mares
  if (iface->flags & IF_IGNORE)
782
    return;
783
  if (c & IF_CHANGE_DOWN) {
784 c3e9b2ab Pavel Machek
    struct rip_interface *i;
785 9a158361 Martin Mares
    i = find_interface(p, iface);
786 2d9290e9 Pavel Machek
    if (i) {
787
      rem_node(NODE i);
788 ff8ed632 Pavel Machek
      rfree(i->lock);
789 c91fc9b6 Martin Mares
      kill_iface(i);
790 2d9290e9 Pavel Machek
    }
791 c3e9b2ab Pavel Machek
  }
792 9a158361 Martin Mares
  if (c & IF_CHANGE_UP) {
793 20e94fb8 Ondrej Zajicek
    struct iface_patt *k = iface_patt_find(&P_CF->iface_list, iface);
794 ff8ed632 Pavel Machek
    struct object_lock *lock;
795 53a50af5 Martin Mares
    struct rip_patt *PATT = (struct rip_patt *) k;
796 2d9290e9 Pavel Machek
797
    if (!k) return; /* We are not interested in this interface */
798 be3b6dc5 Pavel Machek
799 ff8ed632 Pavel Machek
    lock = olock_new( p->pool );
800 53a50af5 Martin Mares
    if (!(PATT->mode & IM_BROADCAST) && (iface->flags & IF_MULTICAST))
801 9c1a55de Pavel Machek
#ifndef IPV6
802 53a50af5 Martin Mares
      lock->addr = ipa_from_u32(0xe0000009);
803 9c1a55de Pavel Machek
#else
804 53a50af5 Martin Mares
      ip_pton("FF02::9", &lock->addr);
805 9c1a55de Pavel Machek
#endif
806 53a50af5 Martin Mares
    else
807
      lock->addr = iface->addr->brd;
808 ff8ed632 Pavel Machek
    lock->port = P_CF->port;
809
    lock->iface = iface;
810
    lock->hook = rip_real_if_add;
811
    lock->data = p;
812 8c86f96f Pavel Machek
    lock->type = OBJLOCK_UDP;
813 ff8ed632 Pavel Machek
    olock_acquire(lock);
814 c3e9b2ab Pavel Machek
  }
815 279f4c7b Pavel Machek
}
816
817 91c7c741 Pavel Machek
static struct ea_list *
818 c91fc9b6 Martin Mares
rip_gen_attrs(struct linpool *pool, int metric, u16 tag)
819 91c7c741 Pavel Machek
{
820
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2*sizeof(eattr));
821
822
  l->next = NULL;
823
  l->flags = EALF_SORTED;
824
  l->count = 2;
825
  l->attrs[0].id = EA_RIP_TAG;
826
  l->attrs[0].flags = 0;
827 9f4929e7 Martin Mares
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
828 91c7c741 Pavel Machek
  l->attrs[0].u.data = tag;
829 dff1f579 Pavel Machek
  l->attrs[1].id = EA_RIP_METRIC;
830 91c7c741 Pavel Machek
  l->attrs[1].flags = 0;
831 9f4929e7 Martin Mares
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
832 91c7c741 Pavel Machek
  l->attrs[1].u.data = metric;
833
  return l;
834
}
835
836
static int
837
rip_import_control(struct proto *p, struct rte **rt, struct ea_list **attrs, struct linpool *pool)
838
{
839
  if ((*rt)->attrs->proto == p)        /* My own must not be touched */
840
    return 1;
841
842
  if ((*rt)->attrs->source != RTS_RIP) {
843 c91fc9b6 Martin Mares
    struct ea_list *new = rip_gen_attrs(pool, 1, 0);
844 91c7c741 Pavel Machek
    new->next = *attrs;
845
    *attrs = new;
846
  }
847
  return 0;
848
}
849
850
static struct ea_list *
851
rip_make_tmp_attrs(struct rte *rt, struct linpool *pool)
852
{
853 c91fc9b6 Martin Mares
  return rip_gen_attrs(pool, rt->u.rip.metric, rt->u.rip.tag);
854 91c7c741 Pavel Machek
}
855
856
static void 
857
rip_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
858
{
859 4bfe4e85 Pavel Machek
  rt->u.rip.tag = ea_get_int(attrs, EA_RIP_TAG, 0);
860
  rt->u.rip.metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
861 91c7c741 Pavel Machek
}
862
863 4c5f93d7 Pavel Machek
/*
864 b093c328 Pavel Machek
 * rip_rt_notify - core tells us about new route (possibly our
865
 * own), so store it into our data structures. 
866
 */
867 279f4c7b Pavel Machek
static void
868 dca75fd7 Ondrej Zajicek
rip_rt_notify(struct proto *p, struct rtable *table UNUSED, struct network *net,
869
              struct rte *new, struct rte *old, struct ea_list *attrs)
870 1d941de4 Pavel Machek
{
871 8333431c Pavel Machek
  CHK_MAGIC;
872 1d941de4 Pavel Machek
873
  if (old) {
874 6996f459 Pavel Machek
    struct rip_entry *e = fib_find( &P->rtable, &net->n.prefix, net->n.pxlen );
875 1d941de4 Pavel Machek
    if (!e)
876 5ed68e46 Ondrej Filip
      log( L_BUG "%s: Deleting nonexistent entry?!", p->name );
877 6996f459 Pavel Machek
    fib_delete( &P->rtable, e );
878 1d941de4 Pavel Machek
  }
879
880
  if (new) {
881 6996f459 Pavel Machek
    struct rip_entry *e;
882 f7ad556f Martin Mares
#if 0
883
    /* This can happen since feeding of protocols is asynchronous */
884 6996f459 Pavel Machek
    if (fib_find( &P->rtable, &net->n.prefix, net->n.pxlen ))
885 5ed68e46 Ondrej Filip
      log( L_BUG "%s: Inserting entry which is already there?", p->name );
886 f7ad556f Martin Mares
#endif
887 6996f459 Pavel Machek
    e = fib_get( &P->rtable, &net->n.prefix, net->n.pxlen );
888 1d941de4 Pavel Machek
889 c05ea56f Pavel Machek
    e->nexthop = new->attrs->gw;
890 d0031c5e Pavel Machek
    e->metric = 0;
891
    e->whotoldme = IPA_NONE;
892 ec21aecf Pavel Machek
    new->u.rip.entry = e;
893 8d2e3eba Pavel Machek
894 6f384977 Pavel Machek
    e->tag = ea_get_int(attrs, EA_RIP_TAG, 0);
895
    e->metric = ea_get_int(attrs, EA_RIP_METRIC, 1);
896 8d2e3eba Pavel Machek
    if (e->metric > P_CF->infinity)
897
      e->metric = P_CF->infinity;
898
899
    if (new->attrs->proto == p)
900 d0031c5e Pavel Machek
      e->whotoldme = new->attrs->from;
901
902 3d0ea3a7 Pavel Machek
    if (!e->metric)        /* That's okay: this way user can set his own value for external
903 8d2e3eba Pavel Machek
                           routes in rip. */
904 dff1f579 Pavel Machek
      e->metric = 5;
905 1d941de4 Pavel Machek
    e->updated = e->changed = now;
906 21580e30 Pavel Machek
    e->flags = 0;
907 1d941de4 Pavel Machek
  }
908
}
909
910 feb6abe0 Pavel Machek
static int
911 16c2d48d Ondrej Filip
rip_rte_same(struct rte *new, struct rte *old)
912
{
913
  /* new->attrs == old->attrs always */
914
  return new->u.rip.metric == old->u.rip.metric;
915
}
916
917
918
static int
919 21580e30 Pavel Machek
rip_rte_better(struct rte *new, struct rte *old)
920
{
921 8d2e3eba Pavel Machek
  struct proto *p = new->attrs->proto;
922
923 3918b1b0 Pavel Machek
  if (ipa_equal(old->attrs->from, new->attrs->from))
924
    return 1;
925
926 21580e30 Pavel Machek
  if (old->u.rip.metric < new->u.rip.metric)
927
    return 0;
928
929
  if (old->u.rip.metric > new->u.rip.metric)
930
    return 1;
931
932 8d2e3eba Pavel Machek
  if (old->attrs->proto == new->attrs->proto)                /* This does not make much sense for different protocols */
933
    if ((old->u.rip.metric == new->u.rip.metric) &&
934 ad390755 Pavel Machek
        ((now - old->lastmod) > (P_CF->timeout_time / 2)))
935 8d2e3eba Pavel Machek
      return 1;
936 21580e30 Pavel Machek
937
  return 0;
938
}
939
940 4c5f93d7 Pavel Machek
/*
941 b093c328 Pavel Machek
 * rip_rte_insert - we maintain linked list of "our" entries in main
942 2e9b2421 Martin Mares
 * routing table, so that we can timeout them correctly. rip_timer()
943 b093c328 Pavel Machek
 * walks the list.
944
 */
945 1d941de4 Pavel Machek
static void
946 c91fc9b6 Martin Mares
rip_rte_insert(net *net UNUSED, rte *rte)
947 feb6abe0 Pavel Machek
{
948
  struct proto *p = rte->attrs->proto;
949 10f5c47d Pavel Machek
  CHK_MAGIC;
950
  DBG( "rip_rte_insert: %p\n", rte );
951 feb6abe0 Pavel Machek
  add_head( &P->garbage, &rte->u.rip.garbage );
952
}
953
954 4c5f93d7 Pavel Machek
/*
955 b093c328 Pavel Machek
 * rip_rte_remove - link list maintenance
956
 */
957 feb6abe0 Pavel Machek
static void
958 c91fc9b6 Martin Mares
rip_rte_remove(net *net UNUSED, rte *rte)
959 feb6abe0 Pavel Machek
{
960 e81b440f Ondrej Zajicek
  // struct proto *p = rte->attrs->proto;
961 10f5c47d Pavel Machek
  CHK_MAGIC;
962
  DBG( "rip_rte_remove: %p\n", rte );
963 feb6abe0 Pavel Machek
  rem_node( &rte->u.rip.garbage );
964
}
965
966 93fb60d5 Martin Mares
void
967
rip_init_instance(struct proto *p)
968 a103373f Pavel Machek
{
969 23ac9e9a Ondrej Zajicek
  p->accept_ra_types = RA_OPTIMAL;
970 a103373f Pavel Machek
  p->if_notify = rip_if_notify;
971 1d941de4 Pavel Machek
  p->rt_notify = rip_rt_notify;
972 91c7c741 Pavel Machek
  p->import_control = rip_import_control;
973 b94bbe00 Pavel Machek
  p->make_tmp_attrs = rip_make_tmp_attrs;
974
  p->store_tmp_attrs = rip_store_tmp_attrs;
975 21580e30 Pavel Machek
  p->rte_better = rip_rte_better;
976 16c2d48d Ondrej Filip
  p->rte_same = rip_rte_same;
977 feb6abe0 Pavel Machek
  p->rte_insert = rip_rte_insert;
978
  p->rte_remove = rip_rte_remove;
979 c748cdb9 Pavel Machek
}
980 2d9290e9 Pavel Machek
981 c748cdb9 Pavel Machek
void
982
rip_init_config(struct rip_proto_config *c)
983
{
984
  init_list(&c->iface_list);
985 92ef4fa7 Martin Mares
  c->c.preference = DEF_PREF_RIP;
986 c748cdb9 Pavel Machek
  c->infinity        = 16;
987
  c->port        = 520;
988
  c->period        = 30;
989
  c->garbage_time = 120+180;
990 3918b1b0 Pavel Machek
  c->timeout_time = 120;
991 fd5f8704 Pavel Machek
  c->passwords        = NULL;
992 1b16029c Pavel Machek
  c->authtype        = AT_NONE;
993 a103373f Pavel Machek
}
994
995 a769a180 Pavel Machek
static int
996 aebe06b4 Ondrej Zajicek
rip_get_attr(eattr *a, byte *buf, int buflen UNUSED)
997 a769a180 Pavel Machek
{
998
  switch (a->id) {
999
  case EA_RIP_METRIC: buf += bsprintf( buf, "metric: %d", a->u.data ); return GA_FULL;
1000
  case EA_RIP_TAG:    buf += bsprintf( buf, "tag: %d", a->u.data );    return GA_FULL;
1001
  default: return GA_UNKNOWN;
1002
  }
1003
}
1004
1005 7f5f44bb Pavel Machek
static int
1006 898fdd85 Pavel Machek
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 7f5f44bb Pavel Machek
rip_reconfigure(struct proto *p, struct proto_config *c)
1014
{
1015 2a41c8d9 Pavel Machek
  struct rip_proto_config *new = (struct rip_proto_config *) c;
1016 3e474192 Pavel Machek
  int generic = sizeof(struct proto_config) + sizeof(list) /* + sizeof(struct password_item *) */;
1017 7f5f44bb Pavel Machek
1018 898fdd85 Pavel Machek
  if (!iface_patts_equal(&P_CF->iface_list, &new->iface_list, (void *) rip_pat_compare))
1019
    return 0;
1020 30aa02d7 Pavel Machek
  return !memcmp(((byte *) P_CF) + generic,
1021
                 ((byte *) new) + generic,
1022
                 sizeof(struct rip_proto_config) - generic);
1023 7f5f44bb Pavel Machek
}
1024
1025 a103373f Pavel Machek
struct protocol proto_rip = {
1026 edf62ba1 Pavel Machek
  name: "RIP",
1027 d272fe22 Martin Mares
  template: "rip%d",
1028 dad177d7 Martin Mares
  attr_class: EAP_RIP,
1029 dff1f579 Pavel Machek
  get_route_info: rip_get_route_info,
1030 a769a180 Pavel Machek
  get_attr: rip_get_attr,
1031 edf62ba1 Pavel Machek
1032
  init: rip_init,
1033
  dump: rip_dump,
1034
  start: rip_start,
1035 7f5f44bb Pavel Machek
  reconfigure: rip_reconfigure,
1036 a103373f Pavel Machek
};