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