Statistics
| Branch: | Revision:

iof-bird-daemon / proto / rip / rip.c @ 05476c4d

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