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